Skip to content

fix(runtime): table rows shaped like a Postgres result keep every column - #359

Merged
allisonmahmood merged 2 commits into
mainfrom
fix/320-runtime-success-encoding
Sep 24, 2026
Merged

allisonmahmood merged 2 commits into
mainfrom
fix/320-runtime-success-encoding

Conversation

@allisonmahmood

@allisonmahmood allisonmahmood commented Sep 23, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #357 (branch fix/317-runtime-mutation-deadline).

If a table had user columns named ok (set to true) and rows, tables.insert, tables.get and tables.update returned a 200 without the row's id, timestamps or other columns. The generated API client's call dropped them the same way on decode. The stored row was fine, but a tool couldn't use the returned id for its next update or delete. Closes #320.

RuntimeSuccess in packages/api described value as a union of every operation's response. RuntimeApi encoded through that union after Runtime.handler had already encoded the value with its own operation's schema, and the client decoded through it. In both steps the Postgres { ok, rows } struct matched first and dropped the row's other keys. RuntimeSuccess's value is now Schema.Json, and the server and client both use that shared schema. Each operation is still validated in Runtime.handler, and the wire shape is unchanged. docs/API.md is regenerated, so RuntimeSuccess.value now renders as unknown. The endpoint description already lists what each operation returns.

Tests:

  • New RuntimeApi test: real per-operation Runtime.handler codecs for tables.insert, tables.get and tables.update return the ticket's row. The test calls each one through a client built from the real RuntimeGroup and requires the decoded result to keep every column. It failed on the private-envelope commit with only ok and rows left after decode.
  • The boundary test used to rely on the union rejecting a raw true from a handler that skips Runtime.handler. It now returns 1n and checks that a value that isn't JSON is still a 503 source_unavailable.
  • pnpm typecheck and pnpm lint are clean. In a full pnpm test at load average 8, 7 tests in 4 unrelated files failed: two were confirmed 5s timeouts, and I didn't check the others' reasons. None of the four files goes through this path, and together they pass when run alone (214/214).

Implemented by Claude Opus 5.5 via Claude Code (foreman run orchestrated by Claude Fable 5.1).

@github-actions github-actions Bot added area:server Touches the server area size:s Under 100 changed lines labels Sep 23, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Astra review

Reviewed d5511f0 against origin/fix/317-runtime-mutation-deadline at 7d84a29, using the three-dot diff and issue #320. One pass, no subagents. Only merge-blocking findings below.

Standards

P2 · Method: fix the shared success schema instead of adding a route-local replacement

RuntimeApi.ts:19-21 introduces a second success-envelope schema while leaving the lossy RuntimeSuccess union in packages/api/src/runtime.ts:321-324. The repo's AGENTS.md, "Hit every surface / The wire contract", requires: "The api package is the source; the rest mirror it."

This is observable data loss, not just duplication. The generated makeClient(...).call(...) still decodes through RuntimeSuccess, as declared by RuntimeGroup. I supplied the complete HTTP 200 body through an in-memory transport and exercised that client. It returned only { ok: true, value: { ok: true, rows: [...] } }, losing id, createdAt, updatedAt and title again. The new test uses responseMode: "response-only", so it cannot catch this decoder loss.

Make the existing shared RuntimeSuccess envelope lossless and reuse it here. Keep operation-specific validation in Runtime.handler, update the generated API documentation, and cover the generated client's decoded result. That fixes both directions without introducing a competing schema.

No merge-blocking YAGNI finding.

Spec

No separate intent finding. Issue #320 explicitly permits "a lossless JSON envelope after operation-specific encoding." The raw HTTP path now follows that approach, preserves the wire shape, and leaves operation-specific validation in place. No unrequested functionality found.

Verification

  • pnpm exec vitest run packages/runtime/src/RuntimeApi.test.ts: 12 tests passed.
  • Codec probe: the new envelope preserves all fields for tables.get, tables.insert and tables.update; the unchanged shared decoder drops the same four fields for each.
  • Generated-client probe reproduced that loss with a complete response body and an in-memory HTTP transport. No browser or network end-to-end claim.

Summary: Standards 1 blocker, the shared API decoder still loses row fields; Spec 0 separate blockers.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

Reviewed d5511f0 against issue #320, on the base 7d84a29e (fix/317-runtime-mutation-deadline). Separate passes covered standards, the issue, the encoding method, YAGNI, the wire contract, the test, Schema.Json, and whether handleRaw encodes the body a second time.

Standards

One finding.

encodeSuccess in packages/runtime/src/RuntimeApi.ts describes the 200 body with a private struct, { ok: true, value: Schema.Json }. RuntimeSuccess in packages/api is still the union of every operation response. Both runtime endpoints still declare success: RuntimeSuccess. docs/API.md is rendered from that union.

ADR-0002 decision 1 says every body the server sends on /api/* is described by schemas in packages/api, and the server encodes responses through those schemas. CLAUDE.md, "Hit every surface", says the api package is the source and handlers mirror it. The effect-service review spec says /api/* wire schemas live in packages/api.

RuntimeFailure in the same file still uses the api schema. Success encodes through the private struct. That struct accepts any JSON. The previous boundary test rejected true because true is outside the union. It now rejects 1n.

The union strip is real. PostgresRows ({ ok: true, rows }) is an earlier member than TableRow, and struct encode drops the other keys. Issue #320 allows this envelope, so the private struct is the method the ticket names. It is still a second description of the success body.

Replacing RuntimeSuccess's value with Schema.Json would move that description into packages/api and would rewrite docs/API.md so every success value is arbitrary JSON. Honest handlers still return their operation schemas. The repair that keeps those schemas is to encode value with runtimeOperations[op].response, which already lives in packages/api.

Effect service conventions otherwise hold. Namespace imports, Runtime.Runtime from the environment, no new service, and the new it.effect is a focused behavior test on an alternate layer.

No separate Fowler smell. The duplicated success shape is the ADR finding.

The shell needs no matching edit. packages/serving/src/broker.ts parses the body with JSON.parse and returns result.value.

Spec

Compared with issue #320.

(a) Missing or partial. tables.insert, tables.get, and tables.update all return through runtime.call and this one encodeSuccess. Production handlers still encode first with TableRow or NullOr(TableRow), so a row is no longer matched to a Postgres { ok, rows } struct and stripped.

The proof is partial. The new test calls tables.get only, with a stub Runtime.handler and a synthetic row. It does not call tables.insert or tables.update, and it does not provision a table through TableOperations. The issue says "Proven affected operations: tables.insert, tables.get and tables.update." The optional cursor column is absent. The issue says "With an optional cursor column, cursor is retained too; it is not required to trigger the bug."

(b) Scope creep. None. { ok: true, value: Schema.Json } is the envelope the issue allows: "Keep the selected operation codec through serialization, or use a lossless JSON envelope after operation-specific encoding." The same function also wraps the other JSON successes. Schema.Json checks the value. The body stays { ok: true, value }. Retargeting the old boundary test from true to 1n follows that envelope.

(c) Implemented but wrong. None found. handleRaw returns HttpServerResponse.jsonUnsafe, and HttpApiBuilder returns that response before it applies the endpoint success schema. The test asserts id, createdAt, updatedAt, title, ok, and rows on the parsed body. Per-operation validation still runs in Runtime.handler before the envelope. The issue says "Preserve per-operation validation and the current wire shape."

tables.list, tables.getMany, and tables.insertMany stay complete. Their values did not match the Postgres result structs, and Schema.Json returns valid JSON unchanged. The issue says "The same rows remain complete through tables.list, tables.getMany and tables.insertMany."

Method

The envelope is the right method for this bug.

Schema.Json is a predicate. Finite numbers, strings, booleans, null, plain objects, and arrays pass through as the same value, extra keys included. bigint, undefined, NaN, Date, and Uint8Array fail the check, and that failure is still source_unavailable 503 through the existing SourceUnavailable mapping.

Every honest operation response is already JSON after Runtime.handler. files.get returns HttpServerResponse.uint8Array and never reaches this encoder. A payload that survived the operation codec still returns 200. A row that does not collide keeps the same keys, types, and { ok, value } wrapper. A colliding row now keeps id, the timestamps, and title.

The second walk checks that the value is JSON, so JSON.stringify inside jsonUnsafe cannot throw. It does not repeat the operation schema. That is what the issue's envelope is for.

Encoding with runtimeOperations[op].response would still 503 a bare true, and it would encode through schemas that already live in packages/api. runtime.call returns unknown, and input.op sits in the outer flatMap, so the call site has to carry op into the encoder. putFile is always null. Handlers that already went through Runtime.handler never hit the stricter check. The envelope is the smaller patch, and the ticket allows it.

Schema.decode(RuntimeSuccess) still strips this row, and makeClient would do that for call and putFile. No production caller uses those methods. The shell, the CLI transport, and the SDK read result.value from JSON.parse. docs/API.md still describes the union, which is what honest handlers send.

YAGNI

The runtime change is already small. No new type, helper, parameter, or branch. The comment states the constraint: the union matches PostgresRows first. Both call sites are the ones that already existed.

The test is one collision row through api.call, with deepStrictEqual on the full body. tables.get is enough to lock the bug, because insert and update share encodeSuccess. The status check alone would stay green on the old encoder. The deep equality would not. The Postgres template is the existing HTTP harness. The row is never written.

The Runtime.handler fixture is slightly more ceremony than the assertion needs. A read handler that returns the row hits the same encoder, and TableRow encode of this row is identity. Leave it. It is the production path, and it is still one test.

Summary

Standards: 1 finding, the private success schema beside RuntimeSuccess (ADR-0002). Spec: 1 partial, the new test covers tables.get only. Worst on standards is that second schema. Worst on spec is the single-operation proof. The behavior issue #320 asks for is in the diff.

@github-actions github-actions Bot added the area:docs Touches the docs area label Sep 23, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Review response, pushed as 60c539b.

Fixed:

  • Private success schema next to RuntimeSuccess (ADR-0002), and the client decode still losing columns. RuntimeSuccess.value in packages/api is now Schema.Json. RuntimeApi.ts encodes through RuntimeSuccess again, so the diff against the base no longer touches that file. docs/API.md is regenerated.
  • Test covered only tables.get and only the raw body. The test now runs tables.insert, tables.get and tables.update through a client built from the real RuntimeGroup, which decodes through RuntimeSuccess the same way makeClient does. It asserts the whole decoded result. It failed on d5511f0 with only ok and rows after decode, and it passes now.

Kept as is:

  • Per-operation validation stays in Runtime.handler. I didn't add a second per-operation encode at the route (carrying op into the encoder). Every production JSON handler already encodes with runtimeOperations[op].response, so repeating it at the route would only add plumbing.
  • The boundary test returns 1n instead of true. true is valid JSON under the shared schema now. The test still checks that a value that isn't JSON is refused with a 503 source_unavailable and no correlation id.
  • The test uses stubbed handlers and no provisioned table. The bug was in the envelope schema, not in TableOperations. Real per-operation codecs plus the real client decode cover that path without a provisioned table.
  • No optional cursor column in the row. The issue says cursor isn't needed to trigger the bug, and Schema.Json keeps any extra key.
  • docs/API.md renders RuntimeSuccess.value as unknown. That is the honest description of the envelope. The endpoint description already lists what each operation returns.

@allisonmahmood
allisonmahmood force-pushed the fix/317-runtime-mutation-deadline branch from 7d84a29 to 13b39dd Compare September 24, 2026 13:29
Base automatically changed from fix/317-runtime-mutation-deadline to main September 24, 2026 13:37
allisonmahmood and others added 2 commits September 24, 2026 15:37
A row with user columns `ok: true` and `rows` lost its id, timestamps and
other columns in the 200 body. Runtime.handler had already encoded it with
the operation's own response schema, then RuntimeApi encoded it again
through RuntimeSuccess, whose value is a union of every operation's
response; the Postgres result struct matched first and stripped the rest.

The success body now goes through a lossless envelope that only checks the
value is JSON. Per-operation validation stays in Runtime.handler and the
wire shape is unchanged.

Closes #320

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit only fixed the server's encode. RuntimeSuccess in
packages/api still unioned every operation response, so the generated
client's `call` decoded a row with `ok` and `rows` columns as a Postgres
result and dropped its id, timestamps and other columns.

RuntimeSuccess's value is now Schema.Json, and RuntimeApi encodes through
the shared schema again instead of a private envelope. Per-operation
validation stays in Runtime.handler. docs/API.md is regenerated. The test
now decodes insert, get and update through the real runtime group's client.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@allisonmahmood
allisonmahmood force-pushed the fix/320-runtime-success-encoding branch from 60c539b to 0c56d74 Compare September 24, 2026 13:37
@allisonmahmood
allisonmahmood merged commit 66364ac into main Sep 24, 2026
8 checks passed
@allisonmahmood
allisonmahmood deleted the fix/320-runtime-success-encoding branch September 24, 2026 13:44
allisonmahmood added a commit that referenced this pull request Sep 24, 2026
…across versions (#360)

Stacked on #359 (branch fix/320-runtime-success-encoding).

Omitting a table from a later manifest keeps it in the inventory, but that manifest could still add a file store with the same name (or add a table with an omitted store's name). The publish succeeded and left one name holding both a table and a store, which the product rule forbids. Closes #333.

Now, before running any DDL, the provisioner's diff refuses a new store whose name belongs to a table in the inventory, and a new table whose name belongs to a store. It reports this with the existing `not_additive` error and tells the user to pick a different name. The check inside a single manifest is unchanged, and omitted resources are still kept. I also updated the publish route description, `docs/API.md`, `docs/product.md` and the `patchy-tables` project skill.

**Test evidence**
- New contract test `crossKindNames`, run on Postgres and PGlite. It failed on both before the fix, where version 2 provisioned the store and kept the table. After the fix it passes and shows no DDL and an unchanged inventory.
- `Tables.test.ts` 18/18, `pnpm typecheck` and `pnpm lint` clean.
- Full `pnpm test`: 947 passed, 1 failed. The failure was `packages/integrations/src/postgres/Runtime.test.ts` hitting its 5s timeout under machine load, which is unrelated to this change. It passes when run alone.

Implemented by Claude Opus 5.5 via Claude Code (foreman run orchestrated by Claude Fable 5.1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:docs Touches the docs area area:server Touches the server area size:s Under 100 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

runtime: success encoding drops valid table row fields

1 participant