fix(server): turns recreate a worktree folder that lost its checkout #34894
main fromrefs/pull/34894/head·merged by juliusmarminge View on GitHubThe failing Agent context budget check was not caused by this change. The PR does not change any agent context text. After the previous merge, main lowered apps/server/src/provider/agentContextBudget.json by 3 bytes in 0568a63aac (#30371), so the branch's older copy looked like growth. Merging origin/main again (merge commit, no conflicts) brings the budget file to main's values. The PR diff is unchanged: four server files covering worktree recovery in the shared restoreMissingWorktree helper (clears a folder only when it has no .git and holds only node_modules/.next directories), storage cleanup keeping such folders, and their tests.
Fixes pingdotgg/t3code#24390
Reproduction on main#
In the app, create a worktree thread in a pnpm project and let setup install dependencies. Stop the turn, record its branch, then delete everything in that worktree except node_modules, including .git; optionally run git worktree prune in the project repository. Send another message to the same thread and ask the agent to report its current directory, branch, and tracked files. On main, recovery is skipped and the folder remains without a checkout; with this change, the leftover directory is removed and the thread's checkout is recreated before provider startup. Separately, trigger storage cleanup for an eligible archived thread in the same broken-folder state: main reports a missing-.git failure, whereas this change reports the folder as kept. These steps were read from the issue and PR, not executed.
Evidence#
Evidence captured in Chromium at 1280×800, light theme, against fresh isolated servers with synthetic data and Replay. Create a worktree, install pnpm dependencies, leave only node_modules, remove .git, prune, and resume the thread. Separately archive another broken-folder thread and run storage cleanup. Filesystem observations are measured by the driver and displayed in chat, not generated by Replay. Absolute paths are redacted.
| Before b5c0ee56fa | After 6facbfe5e1 | |
|---|---|---|
| Resumed checkout | ![]() | ![]() |
| Archived-folder cleanup | ![]() | ![]() |
| Recording | Before | After |
Scenario source · Capture redaction script
scenarios:prove exited 0: failed on base and passed on head.
Not shown:
- Automatic project setup and interrupting an active turn were not exercised: pnpm was invoked directly in the app-created checkout, and the initial Replay turn finished before folder corruption. A live provider's directory report was not exercised; the driver measured the actual filesystem and git state.
Tests#
Compared agentContextBudget.json on the branch and origin/main (5597/7327 vs 5594/7324) and found the main commit that lowered it. After the merge, vp run check:changed passed: format, lint, knip, typecheck (3 packages), tests (3 files). The budget check itself was not rerun locally; CI runs it on the new head.
Not in this change#
- Already-broken folders that hold user files besides node_modules/.next are still not recovered; turns keep running there. Deciding what to do with them needs a maintainer decision.
Who did the work#
- Written by Claude Opus 5.5, in Claude Code
- Reviewed by GPT-6.1 Sol, in Codex, and by Claude Opus 5.5, each on its own
- Proof captured by GPT-6.1 Sol and checked by Claude Opus 5.5
Run by the t3code factory in T3 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
- @@ -219,3 +219,4 @@219219 readonly branch: string;220220 }) {221221 const { cwd, worktreePath, branch } = input;222+ yield* fileSystem.remove(worktreePath, { recursive: true, force: true });
🔴 Critical
orchestration-v2/ProviderTurnStartService.ts:222When a managed worktree has lost only its
.gitentry, the next turn recursively deletes the entireworktreePath, permanently removing any remaining modified, untracked, or ignored files. The.gitcheck detects the missing checkout metadata, whileisManagedWorktreePathverifies only the path location; preserve the directory by moving it aside before recreation, or refuse recovery when its contents are not safe to discard.No longer relevant as of 72db8758b1c64b9ccadc37eeb7e9ae0d6b18a8ac
Approvability#
Verdict: Approved at
72db875Macroscope's review found this PR approvable — Restores managed worktrees whose checkout disappeared but left dependency or build directories behind. Recovery preserves existing checkouts and folders containing other entries, while cleanup safely skips folders missing .git.
No code changes detected at
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).6facbfe. Prior analysis still applies.Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅ Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅ Codex Live turn WebSocket decoded 21.5 KiB 21.5 KiB 0 B (0.0%) 24.4 KiB ✅ Codex Live turn messages 1 1 0 (0.0%) 8 ✅ Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅ Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅ Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅ Claude Live turn WebSocket decoded 21.9 KiB 21.9 KiB 0 B (0.0%) 24.4 KiB ✅ Claude Live turn messages 1 1 0 (0.0%) 8 ✅ Other scenarios#
All 64 scenario measurements match
main.All 64 scenario measurements
Provider Scenario Metric Main baseline This PR Impact PR ceiling Codex short turn resumeWebSocket decoded10.5 KiB 10.5 KiB 0 B (0.0%) 12.7 KiB ✅ Codex short turn resumeWebSocket messages2 2 0 (0.0%) 2 ✅ Codex long turn running, cold openHTTP decoded281.7 KiB 281.7 KiB 0 B (0.0%) 302.7 KiB ✅ Codex long turn running, cold openrows75 75 0 (0.0%) 75 ✅ Codex long turn running, history pageHTTP decoded92.4 KiB 92.4 KiB 0 B (0.0%) 107.4 KiB ✅ Codex long turn running, history pagerows75 75 0 (0.0%) 75 ✅ Codex long turn finished, cold openHTTP decoded381.5 KiB 381.5 KiB 0 B (0.0%) 410.2 KiB ✅ Codex long turn finished, cold openrows151 151 0 (0.0%) 151 ✅ Codex long turn finished, socket cold openWebSocket decoded381.6 KiB 381.6 KiB 0 B (0.0%) 410.2 KiB ✅ Codex long turn finished, socket cold openWebSocket messages2 2 0 (0.0%) 2 ✅ Codex long turn finished, socket cold openrows151 151 0 (0.0%) 151 ✅ Codex long turn finished, work pageHTTP decoded186.2 KiB 186.2 KiB 0 B (0.0%) 200.2 KiB ✅ Codex long turn finished, work pagerows150 150 0 (0.0%) 150 ✅ Codex idle thread resumeWebSocket decoded65 B 65 B 0 B (0.0%) 200 B ✅ Codex idle thread resumeWebSocket messages1 1 0 (0.0%) 1 ✅ Codex fleet shell snapshotHTTP decoded42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅ Codex fleet shell snapshotrows27 27 0 (0.0%) 27 ✅ Codex fleet shell subscribeWebSocket decoded65 B 65 B 0 B (0.0%) 200 B ✅ Codex fleet shell subscribeWebSocket messages1 1 0 (0.0%) 1 ✅ Codex fleet shell resume checkHTTP decoded18 B 18 B 0 B (0.0%) 100 B ✅ Codex fleet shell resume checkrows0 0 0 0 ✅ Codex stale shell reloadHTTP decoded42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅ Codex stale shell reloadrows27 27 0 (0.0%) 27 ✅ Codex stale shell resumeWebSocket decoded140 B 140 B 0 B (0.0%) 400 B ✅ Codex stale shell resumeWebSocket messages2 2 0 (0.0%) 2 ✅ Codex stale todo list cold openHTTP decoded72.4 KiB 72.4 KiB 0 B (0.0%) 83.0 KiB ✅ Codex stale todo list cold openrows75 75 0 (0.0%) 75 ✅ Codex stale todo list cold opennodes2 2 0 (0.0%) 4 ✅ Codex stale todo list cold opensubagents2 2 0 (0.0%) 4 ✅ Codex stale todo list cold openplans1 1 0 (0.0%) 2 ✅ Codex stale todo list cold openproviderTurns1 1 0 (0.0%) 2 ✅ Codex stale todo list cold opencheckpoints1 1 0 (0.0%) 2 ✅ Claude short turn resumeWebSocket decoded10.6 KiB 10.6 KiB 0 B (0.0%) 12.7 KiB ✅ Claude short turn resumeWebSocket messages2 2 0 (0.0%) 2 ✅ Claude long turn running, cold openHTTP decoded283.3 KiB 283.3 KiB 0 B (0.0%) 302.7 KiB ✅ Claude long turn running, cold openrows75 75 0 (0.0%) 75 ✅ Claude long turn running, history pageHTTP decoded93.4 KiB 93.4 KiB 0 B (0.0%) 107.4 KiB ✅ Claude long turn running, history pagerows75 75 0 (0.0%) 75 ✅ Claude long turn finished, cold openHTTP decoded384.2 KiB 384.2 KiB 0 B (0.0%) 410.2 KiB ✅ Claude long turn finished, cold openrows151 151 0 (0.0%) 151 ✅ Claude long turn finished, socket cold openWebSocket decoded384.3 KiB 384.3 KiB 0 B (0.0%) 410.2 KiB ✅ Claude long turn finished, socket cold openWebSocket messages2 2 0 (0.0%) 2 ✅ Claude long turn finished, socket cold openrows151 151 0 (0.0%) 151 ✅ Claude long turn finished, work pageHTTP decoded188.1 KiB 188.1 KiB 0 B (0.0%) 200.2 KiB ✅ Claude long turn finished, work pagerows150 150 0 (0.0%) 150 ✅ Claude idle thread resumeWebSocket decoded66 B 66 B 0 B (0.0%) 200 B ✅ Claude idle thread resumeWebSocket messages1 1 0 (0.0%) 1 ✅ Claude fleet shell snapshotHTTP decoded43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅ Claude fleet shell snapshotrows27 27 0 (0.0%) 27 ✅ Claude fleet shell subscribeWebSocket decoded66 B 66 B 0 B (0.0%) 200 B ✅ Claude fleet shell subscribeWebSocket messages1 1 0 (0.0%) 1 ✅ Claude fleet shell resume checkHTTP decoded18 B 18 B 0 B (0.0%) 100 B ✅ Claude fleet shell resume checkrows0 0 0 0 ✅ Claude stale shell reloadHTTP decoded43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅ Claude stale shell reloadrows27 27 0 (0.0%) 27 ✅ Claude stale shell resumeWebSocket decoded140 B 140 B 0 B (0.0%) 400 B ✅ Claude stale shell resumeWebSocket messages2 2 0 (0.0%) 2 ✅ Claude stale todo list cold openHTTP decoded73.4 KiB 73.4 KiB 0 B (0.0%) 83.0 KiB ✅ Claude stale todo list cold openrows75 75 0 (0.0%) 75 ✅ Claude stale todo list cold opennodes2 2 0 (0.0%) 4 ✅ Claude stale todo list cold opensubagents2 2 0 (0.0%) 4 ✅ Claude stale todo list cold openplans1 1 0 (0.0%) 2 ✅ Claude stale todo list cold openproviderTurns1 1 0 (0.0%) 2 ✅ Claude stale todo list cold opencheckpoints1 1 0 (0.0%) 2 ✅ Baseline:
b5c0ee5· PR result:6facbfe· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
- Codex decoded thread snapshot: 108.5 KiB
- Claude decoded thread snapshot: 108.8 KiB
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.
No actionable comments were generated in the recent review.
Walkthrough
-
Managed-worktree recovery identifies existing directories without
.gitthat contain onlynode_modulesor.nextdirectories, removes eligible managed leftovers, and recreates the worktree. Storage cleanup treats a missing.gitentry as a non-linked worktree and propagates other stat errors.Changes: Managed worktree recovery and cleanup
Layer / File(s) Summary Detect and recreate incomplete worktrees
apps/server/src/workspace/restoreMissingWorktree.ts,apps/server/src/orchestration-v2/ProviderTurnStartService.test.tsRecovery recognizes eligible managed directories without .git, removes them before recreation, and leaves paths with other contents untouched. Tests cover leftover recovery and preservation of a user file.Handle missing worktree links during cleanup
apps/server/src/storageCleanup.ts,apps/server/src/storageCleanup.test.tsA missing .gitentry is treated as a non-linked worktree. Other stat errors still propagate. A test verifies that cleanup keeps the directory and itsnode_modulescontents.Estimated code review effort: 2 (Simple) | ~12 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
t3dotgg,stienswout,tonybynmp4
Merge Risk:
Minimal · up to72db8-
This change lets turn start recover a managed worktree folder that lost its checkout. It also stops storage cleanup from failing when .git is missing. No actionable merge-blocking risk remains.
Pre-merge checks:
3 · 1-
Failed checks: 1 inconclusiveCheck name Status Explanation Resolution Linked Issues check [ #24390] Requirement A, turn-start recovery: the whole-PR diff (merge base f313d0a6 to head 72db8758) changes apps/server/src/workspace/restoreMissingWorktree.ts, not ProviderTurnStartService.ts. The …1. Confirm that the turn-start path reaches makeRestoreMissingWorktree for an existing managed folder without .git. Name the call site in the explanation or the PR text. 2. Update the PR description to name restoreMissingWorktree.ts and rem… Passed checks: 3 passed-
Check name Status Explanation Title check The title names the main change: turn starts recreate a managed worktree folder that lost its checkout. It is specific and conventional, and matches the code, tests, and objectives. It does not descri… Out of Scope Changes check Every changed file maps to #24390. restoreMissingWorktree.ts adds leftover detection, a managed-path guard, and recursive removal before recreation. storageCleanup.ts handles NotFound for the .git sta…Description check The description covers the required sections: Problem, Change, Scope, and Verification. It explains that the failing Agent context budget check comes from an older copy of agentContextBudget.json, not… Full details: Title check
-
Explanation
The title names the main change: turn starts recreate a managed worktree folder that lost its checkout. It is specific and conventional, and matches the code, tests, and objectives. It does not describe the storage cleanup change, but that is a secondary aspect.
Full details: Out of Scope Changes check
-
Explanation
Every changed file maps to
#24390. restoreMissingWorktree.ts adds leftover detection, a managed-path guard, and recursive removal before recreation. storageCleanup.ts handles NotFound for the .git stat. The tests cover both behaviors. The change to restoreMissingWorktree.ts also includes a comment and a path import, which support the same behavior. No unrelated file changes appear in the whole-PR diff for these paths. The PR description is stale about ProviderTurnStartService.ts, but this is a description mismatch, not an out-of-scope code change.
Full details: Description check
-
Explanation
The description covers the required sections: Problem, Change, Scope, and Verification. It explains that the failing Agent context budget check comes from an older copy of agentContextBudget.json, not from this change, and that a merge of origin/main resolves it. It states the problem and reproduction steps, links the issue
#24390, and describes the change to restoreMissingWorktree and storage cleanup. Verification lists focused commands, the observed results, and the items not exercised. Evidence includes before and after captures. The title is a conventional commit form in the linked PR objectives. The description has minor gaps: it does not state a conventional commit title in the description itself, and the Verification section is detailed but the wording is long. The author also states that the reproduction steps were not executed. The Who did the work section names models and harnesses, as the template asks.
-
Full details: Linked Issues check
-
Explanation
[
#24390] Requirement A, turn-start recovery: the whole-PR diff (merge base f313d0a6 to head 72db8758) changes apps/server/src/workspace/restoreMissingWorktree.ts, not ProviderTurnStartService.ts. The head adds isLeftoverFolder. It treats a folder as a leftover only when it has no .git entry and every top-level entry is a directory named node_modules or .next. Such folders are removed with fileSystem.remove(recursive) before recreateMissingWorktree runs. This matches the issue's node_modules-only reproduction. The PR description and the previous assessment cite ProviderTurnStartService.ts changes (path.join(worktreePath, ".git") check, forced remove inside recreateMissingWorktree). Those changes are absent from the reviewed diff, so the description is stale. I did not confirm that the turn-start path calls makeRestoreMissingWorktree. Requirement B, storage cleanup: storageCleanup.ts now uses Effect.catchReason("PlatformError", "NotFound") on stat(<worktree>/.git) and returns 'not a linked worktree'. This meets the issue's requirement. Open question: the issue's expected behavior covers any managed path that is not a valid checkout. A managed folder without .git that holds any other entry, such as a user file, is still not recovered. The test 'keeps user file' confirms this is intended. The issue text does not settle whether that is in scope. Tests: the test changes are reported as passing. I did not run them.Resolution
- Confirm that the turn-start path reaches makeRestoreMissingWorktree for an existing managed folder without .git. Name the call site in the explanation or the PR text. 2. Update the PR description to name restoreMissingWorktree.ts and remove the ProviderTurnStartService.ts claims, or move the change to the file the description names. 3. State explicitly whether a managed folder without .git that holds non-leftover content must stay unrecovered. If the issue requires recovery for it, extend the rule and add a test.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Commit to this branch
- 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: factory/issue-24390
- File: .coderabbit.config.ts
- Commit: 72db875
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Team
- Run ID:
d2c05c45-70da-4a5a-b420-9f9df2ebe065
Commits-
Reviewing files that changed from the base of the PR and between 2ae1676f7d6195792bd1da0c4f2c52579e6be5db and 72db8758b1c64b9ccadc37eeb7e9ae0d6b18a8ac.
Files selected for processing: 4-
apps/server/src/orchestration-v2/ProviderTurnStartService.test.tsapps/server/src/storageCleanup.test.tsapps/server/src/storageCleanup.tsapps/server/src/workspace/restoreMissingWorktree.ts
Limit details: You’ve used all 10 included reviews currently available.
-
-
tanstack-source added labelsbug
juliusmarminge added labelstakeover:in-progress
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
juliusmarminge added labelstakeover:ready
juliusmarminge removed labelstakeover:in-progress
juliusmarminge merged this5aea99a
juliusmarminge closed this
juliusmarminge removed labelstakeover:ready
Sign in to join this conversation.




macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed