Skip to content

Storage cleanup never retries or reports a worktree whose git metadata it failed to delete #41355

Closed

Description

@VDrozd-Cyclis

What happened

Storage cleanup removed a settled thread's worktree. git worktree remove deleted the checkout but failed on the worktree's git metadata (<repo>/.git/worktrees/<id>). T3 logged one warning, and the failure appeared in Settings → Storage → Cleanup only until the next hourly sweep replaced that report. After that nothing mentioned it again. On one Linux server, three such leftovers piled up over five days and held about 519k inodes and 38 GB, unnoticed until the host ran out of inodes.

The trigger in this case was a Docker container that bind-mounted a cache folder from inside the worktree's git dir and wrote root-owned drwx------ folders there. T3 can't delete those, and shouldn't try. This issue is about what T3 does once the removal has failed.

Diagnosis

Source at v0.1.11. main (7180578) is unchanged in these spots.

  1. The failure is non-retryable by construction on Linux/macOS. removeLinkedWorktree (apps/server/src/vcs/worktreeRemoval.ts) runs plain git worktree remove [--force] there. Git deletes the checkout first and then the admin dir. It stops at the first entry it can't delete and exits 255, with the checkout already gone. The Windows branch of the same function takes care to leave something a retry can remove ("Putting .git back … leaves a worktree that a retry can remove"). The non-Windows path has no equivalent.
  2. The next sweep skips the thread silently. In cleanWorktrees (apps/server/src/storageCleanup.ts), every candidate starts with const exists = yield* fs.exists(worktreePath); if (!exists) return;. The thread keeps its worktreePath/branch on purpose ("Preserve branch/path so ProviderTurnStartService can recreate the checkout on resume"). Because the checkout is gone, every later sweep returns before reporting anything, so a half-finished removal looks exactly like a successful one.
  3. The only record is ephemeral. The catch pushes an outcome: "failed" entry into the sweep's report and logs storage cleanup failed for worktree. The report lives in a SubscriptionRef holding only the latest sweep, and the sweep runs hourly. A toast appears only for a manual "Delete now". Nothing is persisted on the thread.
  4. Nothing else finds it. reportOrphanWorktrees walks the managed worktree folders, and asks git for linked worktrees nested inside them. It never looks at a repository's .git/worktrees/*. git worktree prune, which restoreMissingWorktree runs before recreating a checkout, prints the same error but exits 0, so the failure looks like success there too. If the thread resumes, git worktree add registers the new checkout under <id>1 and the old admin dir stays behind permanently.

Suggested behaviour, most valuable first:

  • Check before destroying anything. On non-Windows, before running git worktree remove, confirm that the admin dir can be deleted by this user: every directory readable, and every parent writable. If it can't, keep the worktree with a reason naming the first offending path, e.g. "Git metadata holds files this user can't delete (…/.git/worktrees/<id>/…/fanal)". The checkout stays intact, the reason shows on every sweep, and once the user fixes ownership the next sweep removes it normally. Admin dirs are normally a handful of files, so the check is cheap.
  • Recover when the checkout is already gone. Instead of if (!exists) return;, check whether the repository still has an admin dir for this path. Match by its gitdir file, or by .git/worktrees/<basename> when git has already deleted gitdir. If one exists, run git worktree prune and then check whether the dir is actually gone, because prune's exit code says nothing. If it remains, report a failed entry on every sweep, with the leftover path and git's error. That fixes transient causes on the retry (for example NFS .nfs* silly-rename files, or a process still writing) and keeps permanent ones visible.
  • Optional: include the admin dir in the size cleanup reports for a worktree (here it held far more than the checkout), and surface failures of automatic sweeps somewhere persistent, not only after a manual run.

Retrying alone wouldn't have helped in this case, because root-owned files never become deletable. Making the failure visible, and keeping it visible, is the part that matters.

Steps to reproduce

Without T3, on a throwaway repo (git 2.47.3, Docker 29):

git init -q -b main main && git -C main commit -q --allow-empty -m init
git -C main worktree add -q ../wt -b throwaway
docker run --rm -v "$(git -C wt rev-parse --absolute-git-dir)/scratch/c:/c" alpine:3.20 \
  sh -c 'mkdir /c/fanal && chmod 700 /c/fanal && echo x > /c/fanal/f'
git -C main worktree remove --force ../wt   # exit 255: error: failed to delete '.git/worktrees/wt': Permission denied
ls wt                                       # No such file or directory: the checkout is gone
ls main/.git/worktrees                      # wt: the admin dir remains
git -C main worktree prune -v               # prints the same error, exit 0
git -C main worktree add ../wt throwaway    # succeeds and registers wt1; wt stays forever

Depending on which entries git deletes before it hits the unremovable one, the leftover either shows as prunable in git worktree list --porcelain or is missing from the list entirely: in the run above, git had already deleted gitdir and HEAD. The error text also varies (Permission denied or Directory not empty).

In T3: enable a worktree cleanup rule (for example "settled after N days"), and plant root-owned content inside a settled thread's .git/worktrees/<id> as above. Let a sweep run, then open Settings → Storage after the next hourly sweep. The failure is gone and the leftover is never reported again.

Version

0.1.11 (background service, t3 serve).

Environment

Linux x64, Debian 13 (kernel 6.12), Node v26.13.1, git 2.47.3.

Evidence

Three failures, one per worktree (18, 19 and 21 Nov). No later log line mentions these worktrees or threads:

[05:31:12.209] WARN: storage cleanup failed for worktree
  { threadId: 'mcp:launch:…', error: { _tag: 'GitCommandError', operation: 'StorageCleanup.cleanupGit',
    command: 'git worktree remove', cwd: '~/<repo>',
    detail: "error: failed to delete '.git/worktrees/t3-<id>': Directory not empty" } }

The thread rows still hold the removed worktreePath and branch, with nothing recording the failure.

Related issues

Fix applied or workaround

Fixed ownership of the three leftovers (chown -R to the user) and ran git worktree prune. The repository that bind-mounted into the git dir is changing its containers to run as the user. No T3 change was made.

Filed by

Claude Code (Claude Opus 5.5, claude-opus-5-5) via T3 Code triage.

Activity

  1. added
    via-triage
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    via-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions