fix(server): a usage-limit auto-resume survives a server restart #31292
main fromrefs/pull/31292/head·merged by shivamhwp(GitHub profile, opens in a new tab) View on GitHubProblem#
#29838 changed the usage-limit auto-resume to an agent message. If the server restarts after that resume is dispatched but before the provider starts it, startup recovery cancels the run. The limit worker then rebuilds the same limit-resume:… command id from the unchanged limitRecovery.requestId. That id already has a receipt, so the retry does nothing and the auto-resume never comes back. Nothing tells the user.
Restoring the pre-#29838 behavior would not fix it. That path held the resume as a queued message, but queue.resume and queued-run.start reject it while the thread is still usage-limited ("Continue the limited thread before resuming its queue."). A manual Continue would release the held message and send the continuation twice.
Change#
- Contract change:
OrchestrationV2Rungets one optional field,usageLimitContinuationOfRunId(packages/contracts). The orchestrator copies it from themessage.dispatchcommand onto the run, the same way it already copiesrestartContinuationOfRunId. Runs are stored as JSON payloads, so there's no migration, and older clients ignore the field. The stored message doesn't record that it's an auto-resume; only the command does, so this field is the signal. It avoids matching thelimit-resume:id. - Re-arm: when recovery cancels a never-started run tagged as the auto-resume while the thread's auto-resume is armed, it sets
limitRecovery.requestIdto its own per-pass command id. The worker then builds a fresh resume command and the thread resumes on its own.holdStoppedThreadalready re-keyslimitRecoverythe same way when the user stops a thread. The orchestrator's usage-limit dispatch guard (Orchestrator.ts:5639) still re-checks the blocked run, failure, reset, snooze, archive, settle and pending requests before it accepts any resume. - One resume message: recovery marks the cancelled resume's prompt as queued input (
inputIntent: "queued_turn"). The shared rule inpackages/shared/src/orchestrationV2Timeline.tsalready keeps cancelled queued input out of the transcript, so only the re-sent resume shows. Web and mobile both get that rule through client-runtime, and the server's bounded thread snapshots use it too.
A resume that had already started is not re-armed, so it can't be sent twice. Other cancelled runs, such as the user's own queued message, don't re-arm it either.
Older servers: a resume dispatched by a server older than this change has no tag, so recovery can't hide its copy. It is not re-sent. That thread stays limited exactly as on main, and the user can press Resume (the composer's ▶ button, visible and enabled in the Before capture below). It can only happen if the upgrade restart lands in the few seconds before that resume starts. Re-sending it would show two resume messages, and identifying it would mean matching the limit-resume: id.
Scope and approval#
This is a regression from #29838, found while reviewing that PR after it merged. The change is limited to restart recovery for the usage-limit auto-resume.
Verification#
In the web app#
Both runs use the same isolated dev server (vp run dev --no-seed, worktree .t3 state), the free Replay (dev) provider, and the real usage-limit worker with Auto-resume limited threads turned on. The only difference is ProviderRuntimeRecoveryService.ts (and the run marker): main's version for Before, this PR's for After. The After run was recorded at e17a4aee3a. The current head, 5ef11ff9eb, is that change rebased onto main, plus one commit that re-arms only tagged resumes. The recorded resume was tagged, so that commit doesn't change what the recording shows.
Local-only, uncommitted harness. A real provider can't hit a usage limit on demand, and the restart window lasts milliseconds. So three throwaway edits, not part of this PR, make the case reproducible:
- The replay agent fails a prompt containing
HIT_LIMITwith a JSON-RPC error. - The ACP adapter classifies that error as
usage_limitwithresetAt30 s out. - The effect worker holds the next
provider-turn.startfor 60 s when a marker file exists.
The marker is armed after the limit appears, so it holds only the auto-resume's turn start.
How the restart was timed: the limit is hit, and 30 s later the real worker dispatches "Continue where you left off." The harness holds that run in starting with startedAt: null. While it is held, I stopped the dev server's process group and started it again.
Before (main): the thread hits the limit and auto-resume fires. The server restarts at 0:49 in this recording, and the thread stays Limited with "Resets 8:54 AM · Cancel auto-resume" for the remaining 90 s. Nothing runs. I trimmed the first 55 s, where the harness also held the thread's first turn.
https://github.com/user-attachments/assets/88a7d731-3c7e-4040-9f4b-ad8723814d04
After (this PR): the same steps. The server restarts at 0:46 and is back at 0:53. One second later the resume starts again and runs to completion. The thread is no longer limited, and the timeline shows one "Continue where you left off." (the page has exactly one).
https://github.com/user-attachments/assets/ede0f97f-2899-405a-8985-a8b20a9bdc60
Run list and prompts for each thread, read from the dev database:
Before (main) After (this PR)
before restart (08:54:14) before restart (01:13:35)
1 failed started 08:53:33 HIT_LIMIT … 1 failed started 01:12:52 HIT_LIMIT …
2 starting started null limit-resume:… 2 starting started null limit-resume:…
server stopped 08:54:15, back 08:54:26 server stopped 01:13:36, back 01:13:43
~90 s after restart (08:55:45) ~75 s after restart (01:15:08)
1 failed 1 failed
2 cancelled started null 2 cancelled started null
(no run 3) 3 completed started 01:13:44 limit-resume:…
limitRecovery.requestId: limit-arm:… (unchanged) user_message items: run 1 turn_start,
run 2 queued_turn (hidden), run 3 turn_start
Tests#
Two scenarios of guards a scheduled usage-limit continuation against %s in runtimeLayer.test.ts:
resume: restarts after the resume is dispatched. The worker produces a different command, sending it twice leaves runs at[failed, cancelled, starting], and the visible user messages are only the original prompt and the new run's resume. That uses the same visibility rule clients use.resume-started: restarts after the resume started. The worker sends nothing andrequestIdis unchanged.resume-untagged: the resume run has no tag, as an older server stores it.requestIdis unchanged, the worker only rebuilds the already-spent command, and one resume message is visible.
main's recovery code:
× against resume
AssertionError: expected 'limit-resume:recovery:resume:run:thre…' to not equal 'limit-resume:recovery:resume:run:thre…'
✓ against resume-started
re-arm without the queued-input marking:
× against resume (the cancelled run's resume is still visible: two resume messages)
re-arm on any never-started run instead of only tagged resumes:
× against resume-untagged
AssertionError: expected 'command:runtime-reconcile:startup:rec…' to equal 'limit-arm:recovery:resume-untagged:ru…'
re-arm with the condition widened to re-key on any cancelled run:
× against resume-started
AssertionError: expected 'command:runtime-reconcile:startup:rec…' to equal 'limit-arm:recovery:resume-started:run…'
this PR:
runtimeLayer, ProviderRuntimeRecoveryService (+ regression), RestartContinuation,
UsageLimitRecoveryWorker, DelegatedCompletionDelivery, ProjectionRecovery tests
Test Files 7 passed (7)
Tests 249 passed (249)
vp run check:changed: format, lint, knip, typecheck (36 packages), tests all pass
Not shown:
- A real provider's own usage-limit error, or a restart without the artificial hold. The failure and the hold come from the local harness described above.
- The mobile app. It builds its timeline through the same client-runtime visibility rule, but I didn't run it on a device.
Done by Claude Opus 5.5 in Claude Code, running in T3 Code. GPT-6.1 Sol in Codex did five review rounds.
🤖 Generated with Claude Code
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:S
Approvability#
Verdict: Approved at
5ef11ffMacroscope's review found this PR approvable — Usage-limit auto-resumes now survive a restart before dispatch, while superseded unsent prompts are removed from the visible transcript. Existing auto-resume settings and dispatch safeguards remain unchanged.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).Note
Reviews paused#
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.- Last reviewed commit: 5ef11ff9ebdc56243ad69ef724547db05709e118
Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.
Use the checkboxes below for quick actions:
- Resume reviews
- Trigger review
No actionable comments were generated in the recent review.
Walkthrough
-
Run creation now retains usage-limit continuation identifiers. Startup reconciliation updates recovery metadata and queued user-message items for unstarted continuations. Tests cover recovery behavior for started and unstarted continuations.
Changes: Usage-limit recovery
Layer / File(s) Summary Continuation tracking
packages/contracts/src/orchestrationV2.ts,apps/server/src/orchestration-v2/Orchestrator.tsThe run contract adds an optional usage-limit continuation identifier. Run creation paths retain that identifier when supplied. Recovery reconciliation and tests
apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.ts,apps/server/src/orchestration-v2/runtimeLayer.test.tsReconciliation updates the limit-recovery request ID for eligible unstarted runs. It marks their user-message items as queued turns and excludes handled prompts from later cancellation. Tests cover started continuations, retry dispatch, and visible user-message items. Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix
Suggested reviewers:
juliusmarminge
Merge Risk:
Minimal · up to5ef11-
No actionable merge-blocking risk is established for the restart recovery change; it is ready for normal merge checks.
Pre-merge checks:
4-
Check name Status Explanation Title check The title clearly describes the restart recovery fix. Description check The description is mostly complete. It explains the problem, change, scope, and verification. The scope section does not link a triaged issue or approval, or explicitly explain why the fix qualifies w… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Full details: Description check
-
Explanation
The description is mostly complete. It explains the problem, change, scope, and verification. The scope section does not link a triaged issue or approval, or explicitly explain why the fix qualifies without one.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: pingdotgg/t3code
- Branch: main
- File: .coderabbit.config.ts
- Commit: d5fc29f
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Advanced
- Run ID:
02ad9b83-8384-4c96-9ebd-c91f1d3996c5
Commits-
Reviewing files that changed from the base of the PR and between 761e3ff7a821ae43849a9e5545903ba3e3df0c2a and 5ef11ff9ebdc56243ad69ef724547db05709e118.
Files selected for processing: 3-
apps/server/src/orchestration-v2/Orchestrator.tsapps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.tsapps/server/src/orchestration-v2/runtimeLayer.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
-
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:S
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
@coderabbitai review
Action performed
-
Review finished.
Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.
-
shivamhwp(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed