Title: fix(mothership): bound stream recovery with a per-run restart budget by waleedlatif1 · Pull Request #8546 · simstudioai/sim · GitHub
Open Graph Title: fix(mothership): bound stream recovery with a per-run restart budget by waleedlatif1 · Pull Request #8546 · simstudioai/sim
X Title: fix(mothership): bound stream recovery with a per-run restart budget by waleedlatif1 · Pull Request #8546 · simstudioai/sim
Description: Summary Problem. A recovered Chat controller that failed to persist a replay event for any reason other than a budget refusal threw StreamControllerSupersededError and left its run recoverable without finalizing. Every reconnect poll (tail, batch GET, replay hold, every open tab, every pod) then claimed the run again and re-POSTed it to the worker. Nothing bounded it: no cooldown, no attempt cap, and since the tail no longer ends at 60 minutes, a storm ran as long as a tab stayed open. Each re-POST made the worker re-hand the run, so a single stuck run could drive sustained worker load. Root cause. Recovery treated "this controller failed" as "another controller took over", and readChatStream claimed any non-terminal run with a free chat lock on every read. The only guards were the chat lock (one live controller at a time) and the controller-token compare-and-swap, neither of which limits how often a dying controller is replaced. Fix (restart limit with exponential backoff, the CrashLoopBackOff / supervisor pattern): planRecovery + claimRunController: each run carries a recoveryBackoff (attempts, claimedAt, notBefore) in its request context, written in the same token-compared claim UPDATE, so it is as atomic as the claim and covers every pod, tab and caller. The first takeover is immediate; later ones wait backoffWithJitter (1 s base, 60 s cap). A takeover more than 5 minutes after the previous one starts a fresh budget. The claim is the last step before the stream starts, after the replay read, billing and access checks. A takeover that fails before it can start leaves the run untouched: it spends no attempt and does not refresh the run, so the orphan sweep can still settle a run that can never start. After MAX_RECOVERY_ATTEMPTS (5) takeovers, the next claim ends the run as stream_recovery_exhausted through the existing terminal path: the run is settled as an error and the worker gets the same explicit abort a replay refusal sends. The worker is not re-POSTed for that claim. Only a lease another controller holds is a hand-off. An unreadable lease proves nothing, so the fenced writes that follow (leased append, token-checked run updates) decide ownership instead of the controller leaving the run to a successor. A failed leased append that is not a budget refusal still hands off, as before, so a short Redis blip (failover, connection reset) is still completed by a successor; the budget bounds the persistent case. StreamTurnFailure is the shared base for replay refusals and recovery exhaustion, so the lifecycle classifies both as errors, never as a cancellation or a hand-off. One Claimed stream run for recovery log line per claim, with the attempt number. Before this, recovery claims were invisible on the Sim side. Behaviour changes A run whose controllers keep dying now ends as an error after 5 takeovers (about 30 s of backoff in total) instead of being re-POSTed indefinitely. The user sees "This response was stopped because it was interrupted repeatedly…" and the transcript up to that point is kept. A second takeover of the same run within 5 minutes waits 1–2 s; later ones wait longer, capped at 60 s. Unchanged: the first recovery after a deploy or crash (immediate, still gated by the lock TTL), parked / permission-wait / client-tool runs (their controller keeps the chat lock, so they are never claimed), replay-budget refusals, worker 5xx and socket-drop retries. Deploy / compat No migration. recoveryBackoff is a new key in the existing requestContext jsonb; runs without it parse as a fresh budget, so in-flight runs are unaffected on rollout and an older pod simply ignores the key (jsonb || keeps the other keys). No worker change is required. Closes the Sim side of the storm; the worker-side re-hand coalescing ships separately. Type of Change Bug fix Testing New recovery-storm.integration.ts (real Postgres + Redis, the real chat stream GET route and chat lifecycle, a stub worker): immediate first takeover, one failed append handed off and completed by the next controller, persistent append failure / thrown tool / lost lease exhausting after MAX_RECOVERY_ATTEMPTS with exponential gaps and exactly one worker stop (1 and 3 tails), waiting out a recent takeover's backoff, a takeover that fails before it starts leaving the run untouched and the next one ending the exhausted run, budget reset after the window, and a parked run with a live controller never being taken over. Red-first: each guard was reverted on its own and its tests went red (never-exhausted, no reset, claim drops the backoff, no wait gate, failure not pre-aborted, unreadable lease treated as superseded). Storm harness against the real chat stream GET route, before vs after, same scenarios: Persist failure on the first frame: before, about 0.6 worker POSTs/s with the run left active indefinitely; after, 6 POSTs in total, then the run is error and the worker is stopped. Append failure after a persisted frame (the shape that looped): before, about 3.5 POSTs/s with one tab and about 7/s with three, growing the replay ring on every cycle; after, 6 POSTs in total with one or three tabs, then terminal. Roughly a 35–70x reduction over the first minute, and bounded instead of unbounded. Healthy recovery, worker 5xx and socket drops: unchanged (1, 4 and 4 POSTs). bun run test:integration for recovery-storm, replay-budget, replay-gap, stream-recovery, buffer-ttl, workflow-client-settlement, orphaned-runs: 90/90 passed (77/77 for the mothership suites after the review round). bun run --cwd apps/sim test lib/mothership/request lib/mothership/chat: 1198 passed. bun run lint, bun run check:audits (54 audits), docs-manifest:check, block-registry check, apps/sim type-check: all pass. Checklist Code follows project style guidelines Self-reviewed my changes Tests added/updated and passing (new tests pass the test-audit authoring gate) No new warnings introduced I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)
Open Graph Description: Summary Problem. A recovered Chat controller that failed to persist a replay event for any reason other than a budget refusal threw StreamControllerSupersededError and left its run recoverable with...
X Description: Summary Problem. A recovered Chat controller that failed to persist a replay event for any reason other than a budget refusal threw StreamControllerSupersededError and left its run recoverable with...
Opengraph URL: https://github.com/simstudioai/sim/pull/8546
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:fa0f0266-7c4c-0c2e-35b6-a61ad73b34ce |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D2B4:349B3D:29A7DE:3A5DA9:6AFB6F06 |
| html-safe-nonce | 4419e3ec1e3483f1b00af93d07a10a332c1fca1fb9e13a09ecd3ff4e7aa381f0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMkI0OjM0OUIzRDoyOUE3REU6M0E1REE5OjZBRkI2RjA2IiwidmlzaXRvcl9pZCI6IjM2Nzk3ODE0MjgxMzM1ODg3NDIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0577a83f277a683c9638e552cdc49ce0d811c2e6d37d7da69f12ed7b51144ca6 |
| hovercard-subject-tag | pull_request:4709905604 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/simstudioai/sim/pull/8546/files |
| twitter:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/40672544?s=400&v=4 |
| og:image:alt | Summary Problem. A recovered Chat controller that failed to persist a replay event for any reason other than a budget refusal threw StreamControllerSupersededError and left its run recoverable with... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0e29e7974fdda2feeaf5c891efd4b23b0734b23480232b62c7bdef13f40b5b48 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/simstudioai/sim git https://github.com/simstudioai/sim.git |
| octolytics-dimension-user_id | 199344406 |
| octolytics-dimension-user_login | simstudioai |
| octolytics-dimension-repository_id | 912559512 |
| octolytics-dimension-repository_nwo | simstudioai/sim |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 912559512 |
| octolytics-dimension-repository_network_root_nwo | simstudioai/sim |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2e8872b770cc4d5d0cc1f2026afab747a268e2ce |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width