Repository navigation
Description
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:
shapeis valid only on ajsonkey withincrementsemantics. A shape needscolumns, andcolumnsneed a shape. Anything else answers 422.- Column types:
string,number,integer,boolean,datetime. Constraints:required(default false) andenum(string columns only). - A table value is
{"rows": [...], "total_rows"?: int}. Each row has one cell per column, in column order.total_rowsis 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,enumand the caps (at most 8 columns and 20 rows, so a table stays readable as Markdown). - An
hrefmust be anhttps://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,semanticsandgrainapplies to them too. GET /factories/{id}/outcome-keysreturnsshapeandcolumns, 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}acceptsshape: "table.v1"withcolumns, and answers 422 for each invalid combination listed above. -
POST /outcomesstores a valid table and answers 422, naming the cell, for a wrong cell count, a wrong type, a null in arequiredcolumn, a value outsideenum, a badhref, too many rows and a badtotal_rows. - A key that has facts answers 409 when its shape or columns change.
-
GET .../outcome-keysreturnsshapeandcolumns. - A
jsonkey with no shape behaves as before. - The new rules are covered by unit tests, and
uv run pre-commit run --all-filespasses.
Additional context
- Builds on the key registry from feat: store automation outcome values for analytics #770 and the key
semanticsin 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
lastaggregation that returns the latest table, so a card can read a snapshot; the metric card display layer; adurationcolumn type; a feature flag in/capabilitiesfor shape support; more shapes such aslist.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
totalsemantics, to get one table per period? Atotalmust be a number today. This issue leaves tables asincrementkeys. - 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: [...]}andGET /v1/factories/{factory_id}/outcome-keysechoes the key withshapeandcolumnsunchanged;GET /v1/factories/{factory_id}includes them too. - That
PUTanswers 422 with a specific message for each of: ashapeon a non-jsonkey; ashapeorcolumnswithout the other; ashapevalue other thantable.v1; acolumnswith a duplicate or invalidname; an unknown columntypeor unsupported constraint key;enumon a non-string column; and more than 8 columns. -
POST /v1/outcomeswithvaluesstores a valid table against atable.v1key and returns 201;GET .../outcomes/valuesreturns the samerowsandtotal_rowsinjson_value; a stored cell{v, href}round-trips unchanged. -
POST /v1/outcomesanswers 422 naming the offending cell as<key>.rows[<i>][<j>](for exampleopen_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 arequiredcolumn; a string outsideenum; anhrefthat is nothttps://or a/-prefixed path; more than 20 rows; andtotal_rowsbelow 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, finitenumberwithin 2^53,datetimean ISO string,booleanstrict). - A key that has facts answers 409 when a
PUTchanges itsshapeorcolumns(the same rule astype,semanticsandgrain), and answers 200 when the definition is resubmitted unchanged. - A
jsonkey with noshapekeeps 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 pytestcovers the new registry and submission rules (both key registration and table validation), including the 422, 409 and no-shape cases above, anduv run pre-commit run --all-filespasses. -
.agents/skills/verify-automation/references/feature-map/F20-outcomes.mddocuments thetable.v1shape, its column types and constraints, the caps and the error naming, alongside the existing sub-features.
Activity