Skip to content

[Bug]: Storage cleanup never removes task-folder checkouts whose branch was renamed or switched #46515

Done

Description

@gshahbazian

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/server

Steps to reproduce

  1. Add a project whose folder holds several git repos, so new-worktree threads get a task folder: ~/.t3/worktrees/<project>/<id>/{repoA,repoB}.
  2. Start a thread in a new worktree and let it settle.
  3. In one of the checkouts, rename or switch the branch (git branch -m new-name or git checkout -b other). Agents do this often.
  4. In Settings → Maintenance, set settled-thread cleanup to 0 days and click Delete now.

Expected behavior

The settled thread's task folder is removed. The checkout is on some branch, the tree is clean, and the branch survives removal.

Actual behavior

The checkout is kept with "Repository or branch changed", on every run. In my case the result was 0 removed and 24 kept, with every thread settled.

This is #17813 / #22312 again, but for task folders. Those fixes (followBranch in cleanup, WorktreeBranchSync) only cover single worktrees:

  • storageCleanup.ts inspectCheckout calls branchChanged(checkoutPath, thread.branch), which does a strict name !== branch check. Unlike the single-worktree path, it never calls followBranch, so the next run doesn't catch up.
  • WorktreeBranchSync matches threads on thread.worktreePath === cwd. For a task folder that path is the root, while git runs in root/<repo>, so thread.branch is never updated.
  • A single thread.branch also can't describe checkouts that are on different branches.

Suggested fix

The branch-name check isn't needed for task folders:

  • They are only removed by age, settling or deletion; the merged/unchanged rules that compare branches don't apply (see the comment above the taskFolder branch in storageCleanup.ts).
  • taskFolders.remove is called without deleteBranch, so the branches survive.

In inspectCheckout, drop the name comparison and keep only the safety checks: repo missing, an index lock, an operation in progress, or a detached HEAD with commits no branch or remote holds. One way is to let branchChanged take branch: undefined meaning "any branch is fine" (return branch !== undefined && name !== branch) and pass undefined from inspectCheckout.

Impact

Major degradation or frequent failure

Version or commit

0.1.13

Environment

macOS, desktop app, multi-repo project (task folders)

Logs or stack traces

# thread branch (statev2.sqlite) vs. actual checkout HEAD, clean trees
<id-1>/repoB  thread=feature/foo          head=feature/foo-renamed
<id-2>/repoB  thread=feature/bar          head=feature/bar-v2
<id-3>/repoA  thread=feature/baz          head=feature/baz-split
<id-4>/repoA  thread=feature/qux          head=feature/qux-stack-1
# ...about 12 more, all "Repository or branch changed"

Workaround

Remove the checkouts by hand with git worktree remove.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions