Skip to content

fix(runtime): table and file mutations now stop at their logged deadline - #357

Merged
allisonmahmood merged 1 commit into
mainfrom
fix/317-runtime-mutation-deadline
Sep 24, 2026
Merged

allisonmahmood merged 1 commit into
mainfrom
fix/317-runtime-mutation-deadline

Conversation

@allisonmahmood

Copy link
Copy Markdown
Owner

Stacked on #356 (branch fix/328-runtime-body-cap).

Table and file mutations logged a 30-second deadline but nothing enforced it. Only integration calls had a timeout, so a mutation could keep running after its own log row already read unknown. Tier 2 will hold company-database transactions open for a hosted mutation, and that plan counts on this deadline being real.

Every logged call now runs under the same timeoutOrElse. The timeout uses the deadlineMs that goes into the runtime log, so a mutation past PATCHY_RUNTIME_MUTATION_DEADLINE_MS fails with timeout (504) and its log row records failure/timeout. Behaviour change: a file PUT is logged before its body is read, so the upload itself now has to finish inside the 30-second (configurable) mutation deadline. docs/API.md says so. The Timeout message no longer says "integration".

Tests:

  • New Runtime.test.ts case: a never-returning tables.insert with a 5 s configured deadline gets a 504 timeout at exactly 5,000 ms, its handler is interrupted, and the log row has deadlineMs and durationMs of 5,000. On the unmodified code the call never returned.
  • The existing "remains unknown after interruption" case now interrupts before the deadline, then checks pending → unknown. Its old version advanced past the deadline first, which only worked because the deadline wasn't enforced.
  • pnpm typecheck and pnpm lint are clean. pnpm test: 944/945. The one failure was a 5 s timeout in integrations/src/postgres/Runtime.test.ts while other test runs were loading the machine; it passes when run alone.

Closes #317

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 area:docs Touches the docs area size:s Under 100 changed lines labels Sep 23, 2026
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Astra review

Reviewed 7d84a29 against origin/fix/328-runtime-body-cap at c241f9e, using git diff origin/fix/328-runtime-body-cap...HEAD. Spec: issue #317. One pass, no subagents; merge blockers only.

Standards

No merge-blocking method or YAGNI findings. Removing the integration-only condition reuses the existing Effect.timeoutOrElse path without adding another timeout mechanism or future tier-2 machinery. The API declaration and API docs both describe the changed behavior.

Spec

No merge-blocking intent findings. Mutations now use PATCHY_RUNTIME_MUTATION_DEADLINE_MS for both the timeout and the logged deadline. Reads remain outside the timeout. The new regression exercises a configured 5-second mutation deadline, handler interruption, the correlated 504 response, and the recorded timeout failure. The interruption test still checks that a lost caller leaves an unknown outcome rather than a claimed failure.

Verification: pnpm exec vitest run packages/runtime/src/Runtime.test.ts packages/runtime/src/RuntimeApi.test.ts packages/primitives/src/FilesApi.test.ts passed, 3 files and 28 tests.

Summary: Standards 0 blockers; Spec 0 blockers. No blocking issue in either axis.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

Reviewed 7d84a29 against origin/fix/328-runtime-body-cap and against issue 317. CI on that commit is green.

Standards

The diff follows the Effect service spec, the wire-contract mirror, and the test house style. No hard violations, and no baseline smell worth acting on.

Runtime.ts wraps every non-read in the same timeoutOrElse, using the deadlineMs already passed to the log. Timeout.message still comes only from deadlineMs. The message dropped the word "integration" because RuntimeApi.ts sends error.message as the JSON error field, and a table or file timeout would otherwise be described as an integration call. Reads return before the timer. putFile reads the body inside run, so the PUT deadline includes the body. That is what packages/api/src/api.ts and docs/API.md now say, in the same words, which is the ADR-0002 mirror.

The new test copies the neighboring integration-timeout case. Copying the suite next to it is the house style. It checks status 504, code timeout, the correlation id, that the handler was interrupted, and that the log's deadlineMs and durationMs are the configured 5000. The older interrupt case now interrupts before the clock passes 30 seconds. That reorder is required. Advancing the clock first would finish the row as failure, and the pending-to-unknown path would never run.

No CLI command, skill, or product doc described the old "logged but not enforced" behavior. The shell's existing 35-second abort in packages/serving/src/broker.ts is outside this diff and sits past the 30-second default.

Spec

Issue 317 is implemented as specified. I found no missing requirement, no extra behavior, and no wrong implementation.

The issue says a mutation's runWithDeadline was the bare execute, so runtime_calls.deadline_ms was only descriptive, and the fix is to apply the same timeoutOrElse to mutations with PATCHY_RUNTIME_MUTATION_DEADLINE_MS, keeping the log's deadline_ms equal to what is enforced. Reads return before that path. Mutations take settings.mutationDeadlineMs. Integrations still take integrationDeadlineMs. The same number is passed to options.record. A non-interrupt failure is finished as failure with the error code. An interrupt returns without finishing, so a lost client stays pending and later reads as unknown.

File mutations use that path. files.put and files.delete are mutations. files.get stays a read. For PUT, readBytes is inside the timed effect, so the deadline includes the body. The new test hangs tables.insert at a 5 second deadline, checks it is still running at 4999 ms, then expects 504 timeout, an interrupted handler, and a log row with deadlineMs and durationMs of 5000.

The issue says this fiber timeout is one of three places a future hosted mutation must enforce the deadline, and that the fix for this issue is this timeout. The Timeout message change keeps that shared error accurate now that mutations time out too.

Method

One timeoutOrElse around execute is the right method. The real differences stay where they were. Integrations still admit inside execute, so their admission stays on the clock. Mutations still admit before the timer. Reads never enter it. Postgres integrations keep their own 10-second statement timeout and 15-second deadline.

timeoutOrElse races the call against a sleep. When the sleep wins, the loser's interrupt is discarded and orElse fails with a fresh Timeout. Cause.hasInterrupts is false on that exit, so record writes failure / timeout and the handler returns 504. When the client disconnects, the race itself is interrupted, orElse does not run, and the row stays pending. The reordered test checks that split. Asserting pending right after the interrupt would fail if the timeout had been recorded instead.

A table write interrupted this way rolls back through the existing transaction finalizer, and the pool cancels the in-flight statement. A file PUT interrupted during readBytes never reaches operation.run. Interrupted later, during the object write or the index upsert, the previous index row remains, the same rule as a lost upload. Counting the body against the 30 seconds matches the log. The row is already inserted, so a separate write clock would let that row read as unknown during an upload that was still inside its budget.

at is stored in begin before the timer starts. find reports unknown while a row is still pending and at + deadline_ms is in the past. That gap is about one insert. It already existed for integrations, and it is small next to 15 and 30 seconds.

The company database still sets statement_timeout to 30s in PgCompanyDatabases.ts. That matches the default mutation deadline. If PATCHY_RUNTIME_MUTATION_DEADLINE_MS is raised above 30 seconds, Postgres can cancel the statement first, and the log records that failure rather than timeout. The shell still aborts the browser call at 35 seconds, after the 30-second default, so the client receives the 504.

YAGNI

This is the small change. The branch that timed out only integrations is gone. There is no new type, helper, config, or error. Both new doc sentences are the behavior change, and both files had to move because packages/api is the wire source and docs/API.md mirrors it.

I would keep the one-line comment. It states the invariant this bug was: the number in the log is the number that stops the call.

I would not add a second test that hangs files.put's body read. putFile passes readBytes into the same dispatch. Another Effect.never mutation would repeat this test.

Standards has 0 findings. Spec has 0 findings. Nothing on either axis to change.

Reviewed by Grok 4.7 via Grok Build.

@allisonmahmood

Copy link
Copy Markdown
Owner Author

Both reviews (Astra and Grok) are clean on 7d84a29, with 0 Standards and 0 Spec findings, so nothing needed fixing and no new commits went in.

Things raised in passing that I left alone:

  • Grok noted that the company database's 30s statement_timeout in PgCompanyDatabases.ts equals the default mutation deadline. If PATCHY_RUNTIME_MUTATION_DEADLINE_MS were raised above 30s, Postgres would cancel the statement first and the log would record that failure instead of timeout. That is a config interaction that already existed outside this diff, and it isn't a problem at the defaults, so it's out of scope for runtime: the mutation deadline is logged but never enforced #317.
  • Grok also advised against adding a separate files.put hang test. Agreed: putFile goes through the same dispatch, so it would repeat the new mutation-timeout test.

Checks on 7d84a29 all pass: lint, test (22), test (24), cli-smoke, clerk-live, postgres-concurrency, dependency-review and label. The PR is mergeable (merge state CLEAN) into fix/328-runtime-body-cap, so it should merge after #356.

@allisonmahmood
allisonmahmood force-pushed the fix/328-runtime-body-cap branch from 3558237 to 3c35c41 Compare September 24, 2026 13:20
Base automatically changed from fix/328-runtime-body-cap to main September 24, 2026 13:28
…eadline

Mutations logged PATCHY_RUNTIME_MUTATION_DEADLINE_MS as their deadline but only
integration calls were wrapped in the timeout, so a mutation could keep running
after its log row read as unknown. Every logged call now runs under the same
timeoutOrElse, failing `timeout` (504) at the deadline its log records.

Closes #317

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@allisonmahmood
allisonmahmood force-pushed the fix/317-runtime-mutation-deadline branch from 7d84a29 to 13b39dd Compare September 24, 2026 13:29
@allisonmahmood
allisonmahmood merged commit cb2b65a into main Sep 24, 2026
8 checks passed
@allisonmahmood
allisonmahmood deleted the fix/317-runtime-mutation-deadline branch September 24, 2026 13:37
allisonmahmood added a commit that referenced this pull request Sep 24, 2026
…umn (#359)

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).
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: the mutation deadline is logged but never enforced

1 participant