fix(server): cleanup keeps worktrees waiting for a usage-limit reset #24689
main fromrefs/pull/24689/head·merged by maria-rcks View on GitHubProblem#
When the first prompt in a new worktree hits a provider usage limit, T3 Code holds the thread for the reset (snoozed, or set to auto-resume). With "Delete unchanged worktrees" on, the next storage cleanup removes that worktree anyway, because the run's failed status counts as idle and a fresh checkout has no commits beyond the default branch. The agent never got to do any work in it. Reported in #24389.
Steps: turn on Settings → Storage → "Delete unchanged worktrees", start a thread in a new worktree while the provider is at its limit, then run cleanup. The report says "No commits beyond the default branch" and the folder is gone.
Change#
storageCleanupThreadIdle now treats a thread as busy while usage-limit recovery still has work for it. The latest run failed on a usage limit, the thread is not archived or settled, and one of these holds:
- Recovery is armed for that failure with auto-resume. The hold lasts until the continuation starts a new run, and the usual running checks cover that run.
- Recovery is armed with snooze, and
resetAtis still in the future. - The recovery worker has not armed the failure yet but will on its next tick, every 5 seconds: the reset is later than the end of the failed run, and auto-resume is on in settings, or snooze is on and
resetAtis still in the future. This is the worker's own rule. A cleanup that runs first, such as the one at startup or the one a stopped provider session starts when "Delete worktrees with deleted threads" is on, would otherwise see an idle thread.
The cleanup report shows the existing "Thread is running or has pending work" reason for it.
A snooze-only hold ends at the reset, so a thread nobody resumes goes back to the normal rules. Turning recovery off for that failure releases the hold. When recovery has not been armed, both recovery settings off leave the normal cleanup rules in effect. An already armed per-thread choice still applies. Both the eligibility pass and the pre-removal recheck receive the server's recovery settings.
Scope and approval#
Fixes #24389. The issue is not triaged yet. This is a focused fix for a bug the issue shows step by step, in one predicate, with no new setting or UI. It awaits maintainer review.
Two behavior choices for maintainers to confirm. A snooze-only hold lasts until the reset time, not until the thread runs again. An auto-resume hold has no time limit: it lasts until the continuation starts, or until the thread is settled or archived. The issue also suggests never removing a worktree whose thread has not completed a run. I left that out because it would change the unchanged rule for every failed first turn, not only usage-limited ones.
Verification#
vp test run src/storageCleanup.test.ts -t "usage-limited"inapps/server: one table of nine states. Onmainthe five held states fail: snoozed, auto-resume before and after the reset, and not yet armed, with snooze before the reset or auto-resume after it. All 26 eligibility tests pass on this branch.vp fmt --checkandvp linton both changed files pass.vp run typecheckinapps/serverpasses.- Real app, web client and Linux desktop app, each built from the branch under test and run against its own isolated server: a project with a fresh unchanged worktree and a thread whose first run failed on a usage limit and is snoozed until a reset three hours out. I turned on "Delete unchanged worktrees" in Settings → Storage, which starts a cleanup. On
mainthat cleanup removed the worktree and deregistered it while its branch stayed, matching the report. On this branch it was kept, and on desktop a following "Delete now" kept it too.
Report on main:

Report on this branch:

Linux desktop app, same setup. Report on main:

Report on this branch:

The new test on main:

The new test on this branch:

Not checked:
- A real provider usage limit. The thread, its failed run and the run's usage-limit error were seeded into the database, since I had no provider at its limit. The project and worktree came from the app and git.
- Desktop on macOS and Windows. The change is in the server's cleanup rule, which runs the same on every platform.
- Mobile. It has no Storage report.
- The full
storageCleanup.test.tsfile has one failure, "compares the checked-out branch without a full status". It fails the same way onmainwith my local git 2.56 and is unrelated to this change.
Prior PRs#
- #15150 by ANSHSINGH050404 made
failedthreads count as idle for cleanup, which this narrows for the usage-limit hold. - #12687 by juliusmarminge added snoozing limited threads until the reset, the state this change reads.
Fix, tests, PR description and images written with Claude Opus 5.5 in T3 Code Nightly.
🤖 Generated with Claude Opus 5.5 in T3 Code Nightly
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
Approvability#
Verdict: Approved at
ecd4e45Macroscope's review found this PR approvable — This is a narrowly scoped server bug fix that preserves worktrees for usage-limited threads while recovery remains pending, including the brief unarmed-worker window. Existing defaults and interfaces remain unchanged, and the behavior is covered by focused tests.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Note
Written by
gpt-6.1-solon behalf of MariaVerified in a real dev environment#
partial verification at
ecd4e452424d6ea1202d1398fb7e343090b46730, with no source changes or pushes. the central usage-limit fix is unverified.- inspected the description, discussion, diff and all three commits. the pr is open and none of its commits are by juliusmarminge or t3dotgg.
- installed and ran
github/main(6575ce32508bd7908444241f3719aa33aaf77f90) in/home/user/main, then stopped it before running the exact pr head. only one dev server ran at a time. the retained pr verification used the explicit isolated home/home/user/t3code/.t3. - used the repo's playwright with
/usr/bin/google-chromeon linux. added a real git project through the web client, selected a new worktree and codexgpt-6.1-sol, and sent real prompts. no mock provider, seeded database failures or demo data. - on main, codex successfully read README.md. cleanup kept the fresh worktree because its provider session was still open. this did not reproduce the reported usage-limit bug.
- on the pr, codex successfully read README.md, created an uncommitted
keep-local.txt, then removed that file when asked. cleanup kept the worktree withHas uncommitted changes (1 file). with the file removed, cleanup kept the live shared codex session withProvider session is still open. - after restarting the isolated pr server to release the shared provider session, startup cleanup removed the clean unchanged worktree with
No commits beyond the default branch. verified that the directory and git worktree registration were gone, while its branch and conversation remained readable. - ran
cd apps/server && vp test run src/storageCleanup.test.tsthroughblacksmith-check: 131 passed. existing ci has 26 successful and 2 failed contexts (Test Server 6and its aggregateCheck). the failed job reports the cursor provider-native replay recovery test, outside this diff. this is not a claim that ci is green.
real codex turns:

manual cleanup preserves a live provider session, with playback and seeking checked after upload:
normal unchanged cleanup still removes the checkout after the session is released:

Not verified#
- the original bug on main and the changed usage-limit recovery hold on this pr. the real codex proxy completed every turn successfully; it did not produce a usage-limit failure with a future reset time. without that prerequisite, the recovery worker never arms snooze or auto-resume. the neighboring checks and passing tests do not prove this fix through the provider path. i did not fabricate that state or deliberately exhaust the account. a real limited account is needed to finish this verification.
- snooze expiry, auto-resume after reset, disabling recovery, and the unarmed-worker race through a real provider. these are covered by code/tests only in this pass.
- native desktop, ios/android, macos/windows, other providers, relay/tunnel and multi-device behavior. this pass used the linux web client and codex.
verification stopped short of approval because the changed condition was unavailable. no merge, close, rebase, force-push or label changes.
Note
Written by
gpt-6.1-solon behalf of Mariamerged current main without conflicts and pushed
4c6ab1794060348da2118ae6e9d2c2ece347f537to the existing fork branch without force.fixed one review finding: enabling usage-limit recovery during cleanup's size measurement could still remove an unarmed worktree. the final settings guard now checks both recovery preferences. both regression cases failed with
removedbefore the fix and pass afterward.blacksmith verification:
vp run check:changed: format, lint, knip, graphql, affected typechecks and 144 tests passed.cd apps/server && vp test run src/storageCleanup.test.ts src/orchestration-v2/testkit/OrchestratorReplayRecovery.integration.test.ts: 147 passed, including the cursor recovery case that failed in the original CI.- two independent scoped reviewers approved this exact head. the diff remains two files.
new-head CI is pending. real provider usage-limit/reset recovery remains unverified; this pass proves the regression through deterministic cleanup tests using real git worktrees, not a limited provider account.
maria-rcks merged thisd7d5d7c
maria-rcks closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review.
0bafeba6-c606-47b4-bb21-526bd3615ab8Reviewing files that changed from the base of the PR and between 6fc9144a60c4a5f45089316ddd5f7371bafd3bf2 and 4c6ab1794060348da2118ae6e9d2c2ece347f537.
apps/server/src/storageCleanup.test.tsapps/server/src/storageCleanup.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.
Walkthrough
Storage cleanup now retains eligible failed threads while usage-limit recovery is pending. The cleanup checks pass recovery settings to the idle predicate and recheck global recovery settings before removing a worktree.
Changes: Usage-limit recovery cleanup
apps/server/src/storageCleanup.ts,apps/server/src/storageCleanup.test.tsapps/server/src/storageCleanup.ts,apps/server/src/storageCleanup.test.tsEstimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
juliusmarmingeMerge Risk:
Minimal · up to
4c6abCleanup protects pending usage-limit recovery; no merge-blocking issue was identified.
Pre-merge checks:
4
#24389, and…#24389.storageCleanupThreadIdleexcludes failed usage-limit threads when armed auto-resume remains pending, snooze remains beforeresetAt, or recovery is eligible but not yet…Full details: Title check
Explanation
The title names the main change: storage cleanup now keeps worktrees whose threads wait for a usage-limit reset. It matches the changeset, which makes cleanup treat usage-limit-held threads as busy. It follows the conventional commit format from the template.
Full details: Description check
Explanation
The description follows the template well. It has Problem, Change, Scope and approval, and Verification sections. It states the reproduction steps, explains the new hold rules, links issue
#24389, and gives detailed verification with before/after evidence and clearly listed unchecked items. The template's sections are all present, and the author notes the issue is not triaged yet and asks maintainers to confirm two behavior choices. Under the template's scope rules, the fix does not have a clear maintainer approval link, which is a gap. The template asks for a link to triaged issue or maintainer approval. The author says the issue is not triaged and that the fix awaits review, so the scope requirement is not met yet. This keeps the description from being fully complete. The description also has no agent model line at the end, but it does include a 'Generated with Claude Opus 5.5' attribution, which covers that item.Resolution
Mention the scope-approval gap for maintainers: the linked issue
#24389is not triaged, and the template requires a triaged issue or explicit maintainer approval of direction and scope (or a justification for a small, obvious bug fix). Adding that approval link or explanation would complete the Scope and approval section.Full details: Linked Issues check
Explanation
The change satisfies
#24389.storageCleanupThreadIdleexcludes failed usage-limit threads when armed auto-resume remains pending, snooze remains beforeresetAt, or recovery is eligible but not yet armed. It excludes archived and settled threads from this hold. The cleanup eligibility pass and pre-removal recheck receive recovery settings. Tests cover nine recovery states and settings changes during measurement.Full details: Out of Scope Changes check
Explanation
The diff is limited to storage-cleanup eligibility, recovery-setting revalidation, and tests for the linked bug. The settings recheck prevents removal when recovery preferences change during cleanup. No unrelated product behavior or UI changes are present.
Finishing Touches
🧪 Generate unit tests (beta)
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment `@coderabbitai help` to get the list of available commands.