Skip to content

fix(cloud-agent-next): recover queued messages after a DO reset - #12872

Open
eshurakov wants to merge 13 commits into
mainfrom
eshurakov/session-recovery-backoff
Open

eshurakov wants to merge 13 commits into
mainfrom
eshurakov/session-recovery-backoff

Conversation

@eshurakov

@eshurakov eshurakov commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

After a Sandbox DO or Session DO reset, a queued message could wait 45 min 10 s and then fail with a generic preparation_timeout. The Session DO tried transport recovery only once and did not re-arm it. After that, only the 45-minute backstop could settle the message. In one burst of resets, 7 of 30 affected sessions hung this way: 5 timed out and 2 were stopped by the user.

This PR keeps recovery armed while a message is queued, with a bounded schedule:

  1. sandbox_unreachable failure reason (951c0be). It names the case where queued recovery gets no answer from the Sandbox DO. Telemetry classifies it as a pre-dispatch sandbox-connect failure, so external callbacks still get the existing sandbox_connect_failed code. Mobile maps it to the existing "connection lost" copy, because a new key needs all 87 catalogs.
  2. Bounded re-arm (Part 1) (6a099c6). Recovery passes run 15 s, 30 s and 60 s apart. The first unanswered pass at least 90 s after the first failure fails the queued work as sandbox_unreachable. Each pass has a 2 s deadline. The policy is in a new pure module, session/transport-recovery.ts, which has unit tests. session-do.ts has only the wiring.
  3. Ready status after give-up (8f39477). When the route is ready or reconnecting, an unanswered recovery does not send a cloud.status error, because an accepted turn can still run there.
  4. Slow passive status check (option C) (d5d78fe). While a message is queued, a status read runs 60 s after the last preparing or reconnecting view, or after the last check. This settles a lost failed or ready notification without a reset.

Follow-up commits from review:

  • 2b5c1a9 ends the recovery window when the Sandbox answers without progress (not_ready then a ready view, or a prepare that returns the same failed attempt). Before this commit, those answers extended the window and could fail the message as sandbox_unreachable, which is wrong because the Sandbox did answer. Queued work on those paths now waits on the 60 s status check and the queued backstop, as on main.
  • 375389b logs a pass as answered only after a Sandbox RPC returns.
  • 2dac405 and its revert 5d12455 cancel out. The reverted change would have run a status read every minute while the Sandbox DO is busy preparing.

The 45-minute backstop is unchanged and stays the last resort.

Rebased head and verification

  • Current head: e5cf8884f66705ddac53a62e76dc5e1f3d99e783. Rebased onto c7e978863f83895d76505f252c8f4b0762397cdf without changing the ten recovery patches.
  • The head commit, chore(db): refresh ACL fingerprints for session recovery files, refreshes only the five reviewed Browser ACL fingerprints. All five retain disposition none. No SQL access changed.
  • Local checks passed: Cloud Agent unit tests (119), Durable Object integration tests (80, one worker), Cloud Agent typecheck, changed-file oxlint, mobile copy tests (110), mobile typecheck and lint, Browser ACL checks and tests (45), and git diff --check.
  • Changed-file oxlint is the Cloud Agent lint gate. The existing full-package lint error in test/e2e/fake-llm-core.ts:1455 is outside this PR.
  • Mobile checks passed after rebuilding local tRPC declarations and restoring two incomplete locked dependencies. No mobile verification gap remains from that setup issue.

Historical verification (before rebase)

No manual test and no test on a deployed stack. The tests move a fake clock. They do not reproduce a real storage stall or output-gate behavior. These earlier results came from running the ci.yml and kilo-app-ci.yml steps for the changed paths locally (Bun 1.4.4). Every failure below also occurs on origin/main (5cba3273a1) or has a local cause.

  • The case 1 integration test (queued message after a reset) failed before the fix and passes now.
  • The 5 option C tests fail on the Part 1 sources and pass with option C. 2b5c1a9 adds transport tests for the two answered-without-progress paths.
  • build:wrapper, test:aws-runtime-bundle, test:checkers, check:duplication, test:runtime-ci and the wrapper process.test.ts (7/7) pass.
  • pnpm --filter cloud-agent-next test: 8,767/8,768. The Sentry source-map test passes when TMPDIR is a real path.
  • test:integration: 1,320/1,321. control-plane-workspace-snapshots "lets a new sibling restore…" also fails on origin/main.
  • The other wrapper bun test step: 26 failures, the same as on origin/main.
  • test:postgres (58/58) and the web jest suite (35,474/35,474), each on a database for this branch.
  • Mobile: typecheck, lint, check:i18n, check:classes and check:unused pass. The mobile tests have 3 time-zone failures that pass with TZ=UTC.
  • scripts/typecheck-all.sh --group web and --group packages, scripts/lint-all.sh --group rest, pnpm run format:check and check:md-table-padding pass.
  • pnpm --filter cloud-agent-next lint exits 1 on test/e2e/fake-llm-core.ts:1455. The same error occurs on origin/main, and the file is not in this diff.
  • The full chain ran on 2dac40582b. The final tree equals 375389b560. On it, the Session DO suites (375/375), typecheck and format-changed.sh pass.

Failure UX (wall-clock numbers come from the timers, not from a real stall):

Case Before After
Transient reset 45 min 10 s About 50 s. Pass 1 at about 17 s hits the stall, and pass 2 at about 49 s reaches the new instance.
Genuine failure 45 min 10 s, preparation_timeout About 1 min 51 s after the first failure, as sandbox_unreachable. If the Sandbox already has a terminal reason, the message gets that reason.
Lost notification while preparing 45 min 10 s About 62 s after the lost outcome. Worst case about 2 min 51 s, if that check also gets no answer.

Visual Changes

N/A. Mobile reuses existing copy for the new reason.

Reviewer Notes

  • Option C is required. 2b5c1a9 relies on the 60 s status check to retry the answered-without-progress paths, so option C (d5d78fe) can no longer be dropped alone.
  • No deploy order. sandbox_unreachable is used only inside cloud-agent-next and in the mobile copy map, which takes a string key. A search found no other strict consumer of CONTROL_PLANE_FAILURE_REASON_VALUES.
  • Any answer ends the window. That includes an answer without progress: not_ready then a ready view, or a prepare that returns the same failed attempt. On those paths, sandbox_unreachable would be wrong, so queued work waits on the 60 s status check and the queued backstop instead. The first retry after not_ready→ready moves from 15 s to at most 60 s. If the Sandbox keeps answering without progress, the message still waits up to 45 minutes, as on main. A specific reason for that case would need a new failure reason. Notifications never move dueAt or since.
  • The bound is per window. A message sent late in an open window can fail within seconds.
  • A message sent after a give-up gets no preparation row. Delivery and data are not affected.
  • Deliver replays: when the deliver call hangs, the message can be delivered up to 4 times (the original send plus one per pass). The spec accepts duplicates. A failed message could still run if one delivery arrived.
  • Cost of option C: one status read per queued session per minute while notifications are silent. Grant issuance can hold the Sandbox queue for about 11 s, so one check can fail and open a window. A give-up still needs at least 90 s without an answer.
  • Route after give-up: the local route stays preparing, and the list status is failed. A late preparing notification can show "preparing" with nothing queued. The same thing happens after cancelQueuedMessage.
  • Rollback: to Part 1 or to base, the stored value reads as "no obligation", so those sessions fall back to the backstop.
  • Intended test changes: Part 1 changes five transport tests that asserted "no self-rearm", and two reports tests that set the renamed private field. Option C changes the "recovers prepare exhaustion", "cancels the recovery obligation" and "adopts authoritative attempt B" cases and the {dueAt: 20} unit case. The spec sentences "no self-rearm" and "not periodic preparation polling" change for the same reason.
  • session-do.ts is about 5,100 lines, and this PR adds about 135 lines of wiring to it. Proposed follow-up: move scheduleTransportRecovery, failUnansweredQueued, the clear function and the alarm branch into a small recovery controller.
  • Not checked: whether a reset Sandbox DO returns unknown or a stale preparing (option C handles both by design), and whether the wrapper drops a repeated session.prompt.

@eshurakov
eshurakov force-pushed the eshurakov/session-recovery-backoff branch 2 times, most recently from 201e3fd to 7f67f68 Compare December 1, 2026 19:24

@rgbkrk rgbkrk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bounded window (15s/30s/60s passes, give up at 90s into the window) with since carried across passes is the right shape, and transport-recovery.ts as a pure module with the alarm wiring thin on top is easy to reason about. The answered-without-progress path (not_ready → ready, or same-attempt prepare) correctly ends the window instead of failing the message, and every exit path leaves exactly one of {open window, status check, backstop} armed. Docs, telemetry, mobile copy and the protocol enum are updated consistently.

Worth a staging soak before relying on the ~2 minute figure — the wall-clock numbers come from the fake clock, not a real stall.

Name the case where queued transport recovery gets no answer from the
Sandbox DO. The reason has user text, a pre-dispatch sandbox-connect
classification, and maps to existing connection copy in the mobile app.
…ded window

A recovery pass that the Sandbox DO did not answer was consumed and never
re-armed, so a queued message waited for the 45 min queued backstop after a
single Sandbox or Session DO reset.

An unanswered pass now arms the next one, 15 s, 30 s and then 60 s apart. The
stored obligation keeps `since`, the start of the window, so the delay comes
from `now - since` and not from a counter. The first unanswered pass at least
90 s into the window fails queued work as `sandbox_unreachable`. Accepted work
keeps its backstop. A bare due time from an earlier deploy reads as a window
opened one delay earlier. Each pass logs one bounded
`session_transport_recovery` diagnostic.

The policy lives in `transport-recovery.ts`; `session-do.ts` only wires it.
The tests and the spec that asserted "no self-rearm" change on purpose.
…ails queued work

The give-up after an unanswered recovery window emitted a `cloud.status` error
even when the route was ready or reconnecting. An accepted turn can still run
on that route, so the status must stay ready. The failed queued message carries
its own terminal reason.
…read

Option C of the recovery handover. A lost route notification, for example a
terminal `failed` while preparing, or a rolled-back acceptance left queued
work waiting for the 45 min queued backstop.

While work is queued and no recovery pass is due, the one recovery obligation
is now a status check 60 s later, stored without `since`. It reads the passive
view and acts on it like a recovery pass. The same preparing or reconnecting
attempt only arms the next check, so its status and step detail stay. An
unanswered check opens the bounded recovery window. An `unknown` notification
now arms the alarm, so queued work on a ready route is prepared again.

The spec sentence "not periodic preparation polling" and the tests that
expected no obligation while work stays queued change on purpose.
…rs without progress

A not_ready delivery followed by a ready prepare, and a prepare that
returns the same failed attempt, are answers from the Sandbox DO. They
extended the unanswered window and could fail queued work as
sandbox_unreachable. They now end any open window, and queued work
waits on the status check and the queued backstop.
…dbox RPC returns

A window pass that made no RPC, because the backstop or a retired
attempt settled the queued work first, logged outcome answered. The
pass now records answered where a status, prepare or deliver call
returns, and unanswered stays final.
…notifications

Each preparing or reconnecting view deleted the stored obligation, and
the next alarm arm wrote a new 60 s check, so every step notification
cost two extra storage writes. An owner view now ends only an open
window; a stored status check keeps its due time.
…eparing notifications"

This reverts commit 2dac405. A preparing or reconnecting view must
move the 60 s status check forward: then no status read reaches a busy
Sandbox DO while notifications already show progress. Two local storage
writes per notification cost less than one cross-DO read per minute
during preparation.
@eshurakov
eshurakov force-pushed the eshurakov/session-recovery-backoff branch from 7f67f68 to 550b54f Compare December 1, 2026 21:08

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants