fix(server): new worktrees no longer fail when their generated folder already exists #28944
main fromrefs/pull/28944/head·opened View on GitHubProblem#
When T3 Code generates a worktree path and anything already sits there (a folder an earlier worktree for the same branch left behind, or one the user made), the launch fails with path_already_exists, and the user cannot see why or pick another folder. #32934 made generated names distinct, but an occupied name can still block a worktree on main.
Change#
When T3 Code generates the path, it now claims the folder with a non-recursive, atomic mkdir before Git checks out into it, and tries -2, -3 and so on if the folder exists. An existing folder is never reused or touched, and a free generated name is unchanged.
Scope#
A bug fix for an error: the launch fails for a reason the user cannot see or control. Fixes #35108. It does not take up the broader naming ideas in #27589.
Verification#
To see it on Linux web:
- Create a worktree thread for a branch, then remove that worktree and its branch but leave a file in its folder.
- Launch a new worktree thread for the same branch.
- On main it fails with
path_already_exists; with this PR it is created in the-2folder.
The existing real-Git case in GitVcsDriverCore.test.ts does this, run 2026-11-12 on this PR merged locally with main dbfbe3993f (not pushed), with GitVcsDriverCore.ts from that main and from this PR (selected lines, unchanged):
GitVcsDriverCore.ts from main dbfbe3993f:
× creates worktrees under the configured worktrees directory 223ms
Test Files 1 failed (1)
Tests 1 failed | 273 passed (274)
GitVcsDriverCore.ts from this PR:
Test Files 1 passed (1)
Tests 274 passed (274)
No screenshots on this head. The earlier captures staged collisions that #32934 now prevents, so they were removed. maria-rcks verified the earlier head ee2982b1bd in a real dev environment (main failed with path_already_exists, the PR launched into feature-foo-2); that head still carried the naming changes this PR later dropped.
Limits:
- An outside
git worktree addthat races T3 into the same empty claimed folder still makes T3's launch fail withpath_already_exists, as before. Nothing is shared or deleted.
The change in full and every check
When T3 Code generates a worktree path, it runs git worktree add straight into <worktrees root>/<project folder>/<name>. If anything already sits at that path, the launch fails with path_already_exists. Examples are the folder an earlier worktree for the same branch left behind, holding files it kept, or a folder the user made. The user cannot see why or pick another folder.
#32934 made generated names distinct for slash-versus-dash branches and same-named repository folders. A name that is already occupied can still block a worktree on main.
When T3 Code generates the path, it now claims the folder with a non-recursive mkdir before Git checks out into it. If the folder exists, it tries -2, -3, and so on. The suffix replaces trailing characters of the #32934 hash, so the name stays within its 40-byte bound. mkdir is atomic, so two concurrent launches never pick the same folder, and an existing folder is never reused or touched. If creating the worktree then fails or is cancelled, the claimed folder is removed again, but only while it is still empty.
What stays the same:
- A free generated name is unchanged.
- Explicit and recorded paths (recovery, checkpoint restore, reused PR worktrees) never reach this code and fail as before when occupied.
- An empty folder already at the generated name now gets
-2instead of being checked out into, because it may be another launch's claim.
This is a narrow fix for an obvious bug: a launch fails for a reason the user cannot see or control, and nothing about naming changes for paths that do not collide. It does not take up the broader naming ideas in #27589.
The existing case "creates worktrees under the configured worktrees directory" in
GitVcsDriverCore.test.ts(real Git) now removes the first worktree and its branch, leaves a user file in its folder, and creates the same branch again. It then runs a failed create for a branch that already exists.- On main, the second create fails with
git worktree add failed (path_already_exists). - With the fix, the worktree is created in the
-2folder on its branch, the user file is untouched, and the failed create leaves no folder behind.
- On main, the second create fails with
vp test run src/vcs/GitVcsDriverCore.test.tsat 1d20ba78c3: 245 passed. Again on 2026-11-12, on this PR merged locally with maindbfbe3993f(not pushed), withGitVcsDriverCore.tsfrom that main, and with this PR's version (selected lines, copied without changes):vp run check:changed: format, lint, knip, typecheck and tests pass.The worktree, Git manager, MCP worktree, checkpoint rollback, turn start, thread launch, runtime layer, checkout cache and managed project folder suites pass.
Prior PRs#
- #32934 by StiensWout made generated names distinct per repository and branch; this PR only handles a name that is already taken.
- #25212 by maria-rcks added the long-name shortening, building on #22469 by SunkenInTime.
- #17197 by jakeleventhal (closed unmerged) retried bulk launches that failed on Git races and documented the occupied-path failure.
- #28373 by juliusmarminge removes the branch a failed add leaves behind; this PR removes the claimed folder the same way and does not touch that rollback.
Generated with Claude Opus 5.5 and GPT-6.1 Sol 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
ee2982bMacroscope's review found this PR approvable — New worktrees claim an unused folder and add a numbered suffix when the generated name is occupied. Explicit paths remain unchanged, and failure cleanup removes only empty directories.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).No actionable comments were generated in the recent review.
Walkthrough
-
createWorktreenow claims an available generated folder, trying numbered suffixes when a candidate already exists. If the scoped operation fails, it removes the claimed empty folder. A test covers a collision with a folder that still contains a file.Changes: Worktree folder claiming
Layer / File(s) Summary Claim numbered worktree folders
apps/server/src/vcs/GitVcsDriverCore.ts,apps/server/src/vcs/GitVcsDriverCore.test.tscreateWorktreetries numbered candidate folders and reports directory-creation failures asGitCommandErrors. It removes a claimed empty folder if the scoped operation fails. The test checks that an existing file remains, a-2path is used, and another creation for the same branch fails.Estimated code review effort: 2 (Simple) | ~12 minutes
Change: Bug fix
Possibly related issues
- pingdotgg/t3code#29515: This PR addresses the occupied generated-folder collision, but does not address the issue’s remaining branch-cleanup problem.
Suggested reviewers:
t3dotgg
Merge Risk:
Minimal · up to1d20b-
The occupied-folder change is mergeable after normal checks; no unresolved issue has been established.
Pre-merge checks:
4-
Check name Status Explanation 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. Title check The title names the main change: new worktrees no longer fail when their generated folder already exists. This matches the summary, which adds a numbered-suffix claim of the generated folder in create… Description check The description follows the repository template closely. It has the Problem, Change, Scope, and Verification sections, and each one has substantive content. The Problem section states the failure (pat… Full details: Title check
-
Explanation
The title names the main change: new worktrees no longer fail when their generated folder already exists. This matches the summary, which adds a numbered-suffix claim of the generated folder in createWorktree and a test for the occupied-folder case. The title uses the conventional commit format with the fix(server) scope.
Full details: Description check
-
Explanation
The description follows the repository template closely. It has the Problem, Change, Scope, and Verification sections, and each one has substantive content. The Problem section states the failure (path_already_exists when a generated worktree path is occupied) and the user impact. The Change section explains the non-recursive atomic mkdir claim, the -2/-3 suffixing, and the rule that existing folders are never reused or touched. The Scope section explains why this is a bug fix and links
#35108. The Verification section gives concrete reproduction steps and before/after results from the real-Git test on main and on the PR, and it states a limit (an outside git worktree add racing into the same claimed folder still fails). The author also names the model and harness used, which the template requests. Minor gaps: the title is not shown in the provided text, so conventional-commit formatting cannot be confirmed; the Verification section cites a test count of 245 in one place and 217 in the objectives, which should be reconciled; and the description reports that the UI-free Linux web path was tested only locally, not on a pushed head. These gaps are non-critical.
-
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: c6ca605
- Layers: Repository TS config
- Review profile: CHILL
- Plan: Advanced
- Run ID:
f6b1a509-7958-4f5a-8bd2-397a60c8c350
Commits-
Reviewing files that changed from the base of the PR and between ee2982b1bdd3b1c595aa527f9ecf850a49840df8 and 1d20ba78c3221ea967a37d018c1e868b7ed425dd.
Files selected for processing: 2-
apps/server/src/vcs/GitVcsDriverCore.test.tsapps/server/src/vcs/GitVcsDriverCore.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
-
-
Note
Written by
gpt-6.1-solon behalf of MariaVerified in a real dev environment#
verified as-is at
ee2982b1bdd3b1c595aa527f9ecf850a49840df8on a disposable linux vm. no source changes or pushes.started
github/main(d8a829e5ec) in a separate worktree, then stopped it before starting the exact pr head. both used isolated empty state, real git repositories added through the web app, and real codexgpt-6.1-solturns through the local proxy. chrome was driven with playwright from the repository dependencies.- main: a named-worktree thread on
feature/foocompleted. launchingfeature-foothen failed withpath_already_exists. - pr: the same launches completed in
feature-fooandfeature-foo-2, on the requested branches. named launches usedt3 thread startagainst the running dev server. - a second project also named
apilaunchedfeature/fooinfeature-foo-3and completed its provider turn. - a pre-existing user folder was preserved: codex created
taken/notes.txt, then thetakenworktree launch usedtaken-2. its real provider turn read back the unchangedkeep-user-datacontents. - a duplicate branch still failed with
branch_already_exists, and the failed claim left no numbered folder behind. - two follow-up messages submitted through the web composer completed in the existing
feature-foo-2worktree. the recording shows submission, the real provider turn, and its directory/branch reply.
vp i && cd apps/server && vp test run src/vcs/GitVcsDriverCore.test.tspassed on blacksmith: 217 tests, including the concurrent collision case. github checks are green on the verified head.main, colliding launch:

pr, matching launch:


Not verified#
native ios/android clients, packaged electron, macos/windows filesystems, explicit-path recovery/checkpoint restore, and github pr checkout were not exercised on this linux vm. simultaneous launches were covered by the focused real-git test, not by simultaneous live provider turns.
- main: a named-worktree thread on
juliusmarminge requested changes
Thanks, @only21mil! #32934 (564b2f96b2) now distinguishes generated paths for slash-versus-dash branches and same-named repository folders. Could you rebase and cut this down to handling an already occupied generated path, including safe claiming and cleanup? That case can still block a worktree on main.
Note
🤖 Opus 5.5 replying on behalf of Victor.
Thanks! Done in 1d20ba78c3. I merged main into the branch instead of rebasing, because our contributor rule is to update an open PR with one merge commit on its existing head.
The PR now covers only an already occupied generated path. When something is already at the folder name from #32934, T3 Code claims the first free folder with a non-recursive
mkdir. On a collision it uses-2,-3, …, which replace trailing hash characters so the name stays within 40 bytes. It never reuses or touches the existing folder. If the create then fails or is cancelled, the claimed folder is removed again, but only while it is still empty. The slash-versus-dash and same-named repository parts are gone, along with their test. The existing "creates worktrees under the configured worktrees directory" case now covers the occupied folder and the cleanup, and fails on main withpath_already_exists.only21mil(GitHub profile, opens in a new tab) changed the title
tanstack-source added labelsbug
Note
🤖 Opus 5.5 replying on behalf of Victor.
Main fixes this as of e6a8b48389 (#35121): a new worktree whose generated folder is left over from an earlier one now gets the next free folder instead of failing, so I am closing this PR.
only21mil(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes