Skip to content

Outcomes: named shapes for JSON keys, starting with a table shape #823

Backlog

Description

@jpshackelford

Problem or use case

A key with type json accepts any JSON value. The service checks only that the value is strict JSON and under 64 KB. It does not check what is inside.

Metric cards need a table: for example, the open pull requests of a factory, one row each. A card cannot draw a table from an unchecked blob. If an agent writes a malformed table, the card breaks at read time, and each reader must defend against every bad shape.

The contract for a table must belong to the key, not to the card. Then the service rejects a bad table when the agent writes it, and everyone who reads the key can trust it.

Desired behavior

A key can declare a shape: a named, versioned type that the service owns and enforces on every write. The first shape is table.v1.

Register a table key:

PUT /v1/factories/{factory_id}/outcome-keys/open_prs
{
  "type": "json",
  "shape": "table.v1",
  "columns": [
    {"name": "pr",     "title": "PR",     "type": "string"},
    {"name": "status", "type": "string", "constraints": {"required": true, "enum": ["open", "merged", "stale"]}},
    {"name": "lines",  "type": "integer"}
  ]
}

Submit one table as one fact. A cell is a plain value, or {"v": value, "f": "text to show", "href": "https://…"}:

{"values": [{"key": "open_prs", "type": "json", "value": {
  "rows": [
    [{"v": "#818", "href": "https://github.com/OpenHands/automation/pull/818"}, "open", 412],
    [{"v": "#817", "f": "#817 (merged)"}, "merged", null]
  ],
  "total_rows": 140
}}]}

Rules:

  • shape is valid only on a json key with increment semantics. A shape needs columns, and columns need a shape. Anything else answers 422.
  • Column types: string, number, integer, boolean, datetime. Constraints: required (default false) and enum (string columns only).
  • A table value is {"rows": [...], "total_rows"?: int}. Each row has one cell per column, in column order. total_rows is at least the number of rows. It tells a reader that the writer sent only part of the table.
  • The service rejects a write with 422 and names the bad cell, for example open_prs.rows[1][1]. It checks the number of cells, the type, required, enum and the caps (at most 8 columns and 20 rows, so a table stays readable as Markdown).
  • An href must be an https:// URL or a path that starts with /. Cell text is plain text. No HTML or Markdown is interpreted.
  • Shape and columns do not change once the key has facts. The existing 409 rule for type, semantics and grain applies to them too.
  • GET /factories/{id}/outcome-keys returns shape and columns, so a client or an agent can read the contract without reading the plugin.

Where the format comes from

The table format is not invented from nothing. It combines two published formats. We keep a strict subset of each, and we do not claim full conformance to either.

Part Source
The columns array: name, title, type, constraints.required, constraints.enum Frictionless Table Schema (Open Knowledge Foundation). Our columns array is a valid Table Schema fields array. We allow 5 of its types and 2 of its constraints. We do not implement format, primaryKey, missingValues and the others.
Rows as arrays of cells, matched to the columns by position The array-of-arrays form of a Frictionless Tabular Data Resource. We omit the header row, because the key declares the columns.
A cell as {"v": raw value, "f": formatted text} Google Charts DataTable JSON, where a cell has a raw value v and a formatted value f. We take v and f. We do not take p (custom properties).
href on a cell, the shape name, the table.v1 caps Ours.

Not part of this issue: how a card draws the table (units, decimals, colours by value, link style). That is the display layer. Grafana field config is the reference we plan to borrow from there. We looked at Adaptive Cards Table (a UI tree, not data), CSV on the Web metadata (aimed at CSV files) and AG Grid or Tabulator column definitions (tied to one library), and chose not to use them.

Acceptance criteria

  • PUT .../outcome-keys/{key} accepts shape: "table.v1" with columns, and answers 422 for each invalid combination listed above.
  • POST /outcomes stores a valid table and answers 422, naming the cell, for a wrong cell count, a wrong type, a null in a required column, a value outside enum, a bad href, too many rows and a bad total_rows.
  • A key that has facts answers 409 when its shape or columns change.
  • GET .../outcome-keys returns shape and columns.
  • A json key with no shape behaves as before.
  • The new rules are covered by unit tests, and uv run pre-commit run --all-files passes.

Additional context

  • Builds on the key registry from feat: store automation outcome values for analytics #770 and the key semantics in feat: add periods and dedupe keys to outcomes #818. It needs no database migration, because a key definition is already a JSON object.
  • Follow-ups, each its own change: a last aggregation that returns the latest table, so a card can read a snapshot; the metric card display layer; a duration column type; a feature flag in /capabilities for shape support; more shapes such as list.v1; rendering a table as Markdown (Outcomes: render a table.v1 value as a Markdown table #829).
  • Open question: should a table be allowed with total semantics, to get one table per period? A total must be a number today. This issue leaves tables as increment keys.
  • Alternative we rejected: a free-form JSON Schema on the key. It brings $ref, regular-expression cost, schema size and evolution problems, and a card cannot draw arbitrary JSON anyway.

This issue was drafted by an AI agent on behalf of the user.


OpenHands AI triage

The following comments and acceptance criteria were added by the OpenHands AI agent.

Triage

The issue records the problem, the desired behavior with concrete request examples, the validation rules, the format provenance (a strict subset of Frictionless Table Schema plus Google Charts DataTable cells), and explicit non-goals. Verified against the repository: outcome key definitions are JSON objects on the Factory model (openhands/automation/models.py), so no migration is needed; the key routes are PUT/GET/DELETE /v1/factories/{factory_id}/outcome-keys/{key} in openhands/automation/factory_router.py; the existing 409 rule already compares the whole key definition once the key has facts; cell-level scalar rules (finite numbers, ISO datetime strings, size caps) exist in openhands/automation/outcome_values.py. The table validator can be a new strict shape module applied in normalize_outcome_values when the submitted key declares shape, without reshaping existing flows.

Scope is the table.v1 shape on the key registry and submission validation, on top of the semantics work from #818 (open PR outcome-time-and-totals); criteria that name semantics apply to a tree that includes that change.

Non-goals, as written in the issue: no card or display layer, no last aggregation returning a table snapshot, no duration column type, no list.v1 or other shapes, no /capabilities feature flag, no change to how a json key without shape validates, and no decision on tables under total semantics — a shaped key stays increment-only in this change.

Acceptance Criteria

  • PUT /v1/factories/{factory_id}/outcome-keys/{key} accepts {type: "json", shape: "table.v1", columns: [...]} and GET /v1/factories/{factory_id}/outcome-keys echoes the key with shape and columns unchanged; GET /v1/factories/{factory_id} includes them too.
  • That PUT answers 422 with a specific message for each of: a shape on a non-json key; a shape or columns without the other; a shape value other than table.v1; a columns with a duplicate or invalid name; an unknown column type or unsupported constraint key; enum on a non-string column; and more than 8 columns.
  • POST /v1/outcomes with values stores a valid table against a table.v1 key and returns 201; GET .../outcomes/values returns the same rows and total_rows in json_value; a stored cell {v, href} round-trips unchanged.
  • POST /v1/outcomes answers 422 naming the offending cell as <key>.rows[<i>][<j>] (for example open_prs.rows[1][1]) for: a row with the wrong cell count; a cell whose value does not match its column type; a null in a required column; a string outside enum; an href that is not https:// or a /-prefixed path; more than 20 rows; and total_rows below the row count or non-integer. Cells are validated in row order so the first bad cell is named.
  • The 64 KB strict-JSON cap still applies to a shaped table value, and a cell value obeys the same scalar limits the service already applies (string ≤ 2048 characters, finite number within 2^53, datetime an ISO string, boolean strict).
  • A key that has facts answers 409 when a PUT changes its shape or columns (the same rule as type, semantics and grain), and answers 200 when the definition is resubmitted unchanged.
  • A json key with no shape keeps its current behavior: any strict JSON value under the cap is accepted, and keys registered before this change keep accepting facts.
  • uv run python -m pytest covers the new registry and submission rules (both key registration and table validation), including the 422, 409 and no-shape cases above, and uv run pre-commit run --all-files passes.
  • .agents/skills/verify-automation/references/feature-map/F20-outcomes.md documents the table.v1 shape, its column types and constraints, the caps and the error naming, alongside the existing sub-features.

Activity

  1. added
    enhancement
    on Nov 26, 2026
  2. added
    ready-for-dev
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementpriority:lowready-for-dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions