Skip to content

fix(server): storage cleanup removes worktrees of bare-repository projects - #47403

Merged
juliusmarminge merged 2 commits into
pingdotgg:mainfrom
Mezzle:fix/storage-cleanup-bare-repo-worktrees
Dec 2, 2026
Merged

juliusmarminge merged 2 commits into
pingdotgg:mainfrom
Mezzle:fix/storage-cleanup-bare-repo-worktrees

Conversation

@Mezzle

@Mezzle Mezzle commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Problem

Storage cleanup never removes a worktree whose project's repository is bare, such as the repo/.bare layout with a .git file containing gitdir: ./.bare beside it. Each sweep picks the worktree, fails, and tries again next time. On my Mac 62 of 79 worktrees in t3-worktrees belonged to bare-repo projects, none had ever been removed, and the disk filled up. The server log had 227 of these:

storage cleanup failed for worktree
  { _tag: 'GitCommandError', operation: 'StorageCleanup.cleanupGit',
    cwd: '/Volumes/Development/…/newt-storefront/.bare', argumentCount: 2, exitCode: 128 }

Storage cleanup passes removeLinkedWorktree the repository that owns the worktree. For a bare repository that is the bare folder itself. removeLinkedWorktree then ran git rev-parse --git-common-dir with that folder as cwd, which means git has to discover a bare repository. The server sets safe.bareRepository=explicit, so git refuses:

$ cd repo/.bare && git -c safe.bareRepository=explicit rev-parse --git-common-dir
fatal: cannot use bare repository '…/repo/.bare' (safe.bareRepository is 'explicit')

The stderr length in every logged failure matches that message plus the path.

Change

removeLinkedWorktree now asks the worktree for the common git dir. Git opens the repository through the worktree's .git file, which safe.bareRepository=explicit allows. The setting is unchanged.

The check that the worktree belongs to the repository the caller named stays:

-commonDir = rev-parse --git-common-dir   (cwd: input.cwd)
+commonDir = rev-parse --git-common-dir   (cwd: worktreePath)
+if input.cwd is not commonDir:
+  require rev-parse --git-common-dir (cwd: input.cwd) == commonDir, else "not a working tree"
 require adminDir under <commonDir>/worktrees and its gitdir pointing back   (unchanged)

When the caller passes the bare folder itself (storage cleanup), git never runs there. Any other cwd is discovered from as before, so a project folder laid out like a bare repository is still never opened.

Left alone on purpose: the early branch for a worktree without a .git file still runs git worktree remove from the caller's cwd. Storage cleanup only reaches it if the .git file disappears mid-sweep, and every other caller passes a working tree. Passing --git-dir=<cwd> there would break normal repositories and would explicitly open a caller-supplied folder as a bare repository.

Scope

A small fix for an obvious bug, so there's no issue. Cleanup picks these worktrees and then can never remove them, and nothing else touches them, so they pile up. I searched issues and PRs. #36366 refuses a folder that only holds a bare repository and its worktrees as a project. It doesn't cover this, because adding one of the worktrees (repo/main) as the project is still allowed, and that project's worktrees hit this failure. #34834 changes repository detection and doesn't touch removal.

Verification

Tests, both added in the modules' existing test files:

  • storageCleanup.test.ts, "removes a worktree of a .bare folder beside a gitfile and its metadata" and "removes a worktree of a plain bare repository and its metadata". These go through the real GitVcsDriver, so safe.bareRepository=explicit is in force. On main both fail with outcome "failed". With the change they pass, and the worktree's entry under <bare>/worktrees is gone.
  • GitVcsDriverCore.test.ts, "refuses to remove a worktree through a repository that does not own it". This guards the ownership check this change moves. It fails if I delete that check.
  • The existing "does not open a project folder laid out as a bare repository" still passes. So do all 253 tests in storageCleanup.test.ts and the 104 worktree and bare tests in GitVcsDriverCore.test.ts.
  • vp run check:changed passes: format, lint, knip, typecheck, tests.

Runtime, in a real dev server (vp run dev) on Linux with git 2.53.0 and isolated state. I made a bare clone at bareproj/.bare with a .git file and a main worktree, then added bareproj/main as the project. Next I ran t3 thread start --provider devReplay --model showcase --worktree main --branch t3/bare-proof, turned on "Delete a thread's worktree" (worktreeOnDelete), and deleted the thread.

On main the sweep failed the same way as on my Mac, and the worktree stayed registered:

WARN storage cleanup failed for worktree
  { threadId: 'mcp:127a655b-…', error: { _tag: 'GitCommandError', operation: 'StorageCleanup.cleanupGit',
    cwd: '<scratch>/proof/bareproj/.bare', argumentCount: 2, exitCode: 128 } }
$ git --git-dir=bareproj/.bare worktree list
<scratch>/proof/bareproj/.bare                       (bare)
<repo>/.t3/worktrees/main/t3-bare-proof-b42ad41f…    9f11410 [t3/bare-proof]
<scratch>/proof/bareproj/main                        9f11410 [main]

Restarted on this branch with the same state, the startup sweep removed it:

INFO storage cleanup removed worktree  { threadId: 'mcp:127a655b-…' }
$ git --git-dir=bareproj/.bare worktree list
<scratch>/proof/bareproj/.bare                       (bare)
<scratch>/proof/bareproj/main                        9f11410 [main]
$ ls bareproj/.bare/worktrees
main

Settings → Maintenance → Cleanup, last run:

Before (main) After (this branch)
Before: last run shows 0 removed, 0 kept, 1 failed; t3-bare-proof reports a git error After: last run shows 1 removed, 8 KB recovered; t3-bare-proof removed because its thread was deleted

Not checked: macOS, where I first hit this, or Windows. I didn't rerun the sweep against the 62 real worktrees on my Mac. The code path has nothing platform-specific.

Reviewed with review-loop: 1 round with GPT-6.2 Sol on high. Verdict: approved. One nit (two test cases share one code path) declined: the .bare case is the reported layout.

Created with Claude Opus 5.5 in Claude Code (T3 Code).

🤖 Generated with Claude Code

…jects

removeLinkedWorktree asked the caller's cwd for the common git dir. Storage
cleanup passes the owning repository, which for a bare repository is the
bare folder itself, and safe.bareRepository=explicit refuses to discover it,
so every such removal failed with exit 128. The common dir now comes from the
worktree, and cwd only has to be it or discover it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:XS 0-9 changed lines (additions + deletions). labels Dec 2, 2026
@macroscopeapp

macroscopeapp Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at 858277f

Macroscope's review found this PR approvable — Restores bare-repository worktree cleanup when Git restricts bare-repository discovery. Existing repository-identity, lock, local-change, and cleanup-policy safeguards remain in place; defaults and retention rules are unchanged.

You can add or adjust custom eligibility rules. Learn more.

@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 858277f and 52c3b0d.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Worktree removal now resolves the common Git directory from the worktree path and checks repository ownership. Tests cover rejection through a different repository and cleanup of worktrees backed by two bare-repository layouts.

Changes: Worktree Removal and Cleanup

Layer / File(s) Summary
Resolve and validate worktree repository
apps/server/src/vcs/worktreeRemoval.ts, apps/server/src/vcs/GitVcsDriverCore.test.ts, apps/server/src/storageCleanup.test.ts
Removal resolves the common Git directory from the worktree path and checks that the supplied repository matches. Tests verify that a mismatched repository cannot remove the worktree and that cleanup removes worktrees and metadata for both bare-repository layouts.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: lucenx9, smirea

Merge Risk: Minimal · previous review 85827

This change lets storage cleanup remove worktrees owned by bare repositories when safe.bareRepository=explicit is set, and it keeps the check that the repository owns the worktree. New tests cover the bare layouts and the ownership rejection. No merge-blocking risk is identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: storage cleanup no longer fails to remove worktrees of bare-repository projects. It matches the changeset (worktreeRemoval.ts now resolves the common Git dir from work…
Description check Passed The description follows the template. It has Problem, Change, Scope, and Verification sections. The Problem section gives the bug, the reproduction, and the error output. The Change section explains t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: storage cleanup no longer fails to remove worktrees of bare-repository projects. It matches the changeset (worktreeRemoval.ts now resolves the common Git dir from worktreePath, with new tests for bare layouts). It follows the conventional commit form and is specific enough for a reviewer scanning history.

Full details: Description check

Explanation

The description follows the template. It has Problem, Change, Scope, and Verification sections. The Problem section gives the bug, the reproduction, and the error output. The Change section explains the fix and the ownership check that stays. The Scope section explains why no issue is linked and names related issues. The Verification section lists the focused tests, the Linux runtime proof, and the checks not run (macOS, Windows). The template's HTML comments are removed, which is acceptable. The author also names the model and harness used, as the template asks. The description is complete and specific.

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

Commits

Reviewing files that changed from the base of the PR and between 8e72ce7 and 858277f.

Files selected for processing: 3

  • apps/server/src/storageCleanup.test.ts
  • apps/server/src/vcs/GitVcsDriverCore.test.ts
  • apps/server/src/vcs/worktreeRemoval.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.

@juliusmarminge juliusmarminge added the takeover:in-progress A takeover lead is working on this PR label Dec 2, 2026
@juliusmarminge

Copy link
Copy Markdown
Member

Evidence

Environment: Linux web dev app, Chromium, 1280×800, light theme, isolated fresh databases and Replay (dev) on both commits.

On each commit, created a fresh server database and synthetic one-commit repository, cloned it into bareproj/.bare, wrote the .git gitfile, and added main/main. Added bareproj/main through the real CLI and started a devReplay/showcase thread with --worktree main --branch t3/bare-repro. Enabled Settings → Worktrees → Delete worktrees with deleted threads. Opened the thread, chose Delete from its sidebar context menu, confirmed, and waited for the row to disappear. Opened Settings → Maintenance, clicked Delete now, and captured the cleanup summary. On head, also checked that the checkout directory and its Git worktree registration were absent.

Scenario: Cleanup removes a deleted thread's bare-repository worktree

Proven: the scenario fails on base e573bc473e and passes on head 52c3b0df88.

Base e573bc473e Head 52c3b0df88
Result ❌ failed
ASSERTION_FAILED: expect.not.toBeVisible failed
locator: getByText(/item couldn't be removed/)
expected: not visible
observed: default states (match count 1)
✅ passed
bare repository cleanup result Base e573bc473e: bare repository cleanup result Head 52c3b0df88: bare repository cleanup result
failure Base e573bc473e: failure

Before recording · After recording · Scenario source

The head sweep shows 0 removed because deletion already removed the checkout; the scenario also verifies that its directory and Git registration are absent.

Not shown:

  • Plain bare-repository layout without the .bare plus gitfile wrapper was not exercised separately.
  • Repository ownership rejection paths and other cleanup eligibility rules were not exercised; this proof covers the reported eligible deleted-thread worktree.

@juliusmarminge juliusmarminge added takeover:ready Takeover done; auto-lands when gates pass and removed takeover:in-progress A takeover lead is working on this PR labels Dec 2, 2026
@juliusmarminge
juliusmarminge merged commit 29fc7fc into pingdotgg:main Dec 2, 2026
56 checks passed
@juliusmarminge juliusmarminge removed the takeover:ready Takeover done; auto-lands when gates pass label Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS 0-9 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants