Skip to content

Fix worktree pruning: lock worktrees + disable agent auto-gc#144

Merged
that-github-user merged 1 commit into
mainfrom
fix-136-worktree-locking
Mar 29, 2026
Merged

Fix worktree pruning: lock worktrees + disable agent auto-gc#144
that-github-user merged 1 commit into
mainfrom
fix-136-worktree-locking

Conversation

@that-github-user

Copy link
Copy Markdown
Owner

Summary

Root cause found: concurrent agents' git commit triggers gc --autogit worktree prune → deletes other agents' worktree metadata.

Fix:

  1. git worktree lock immediately after creation — locked worktrees survive prune
  2. GIT_CONFIG_COUNT=1 gc.auto=0 env vars on agent processes — prevents gc entirely
  3. Verify worktree metadata directory in getDiff (not just .git pointer file)

Verified: worktree lock file appears in .git/worktrees/NAME/locked, removeWorktree still works.

Change type

  • Bug fix

Related issue

Closes #136

How to test

npm test  # 237 tests pass
thinktank run --attempts 3 --model opus -t "npm test" "review task"
# Agents should show actual file changes, not 0

Breaking changes

  • This PR introduces breaking changes

🤖 Generated with Claude Code

Root cause: concurrent agents' git commits trigger gc --auto, which
calls git worktree prune, deleting metadata for other agents' worktrees.

Fix (belt and suspenders):
1. Lock worktrees immediately after creation — locked worktrees survive
   gc prune. Unlock before removal.
2. Disable auto-gc in agent environment via GIT_CONFIG env vars — prevents
   agent git commands from triggering gc entirely.
3. Verify worktree metadata directory (not just .git file) in getDiff.

Removes old .git file backup approach (addressed symptom not cause).

Closes #136

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@that-github-user that-github-user merged commit c2862b0 into main Mar 29, 2026
4 checks passed
@that-github-user that-github-user deleted the fix-136-worktree-locking branch March 29, 2026 04:07
that-github-user pushed a commit that referenced this pull request Mar 29, 2026
The worktree lock (#144) protects .git/worktrees/NAME/ from gc pruning.
But agents with Bash/Write access can still delete the .git POINTER FILE
from the worktree directory itself. This backup/restore catches that case.

Both protections together:
- Lock → prevents gc from pruning metadata directory
- Backup → restores pointer file if agent deletes it

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
that-github-user added a commit that referenced this pull request Mar 29, 2026
The worktree lock (#144) protects .git/worktrees/NAME/ from gc pruning.
But agents with Bash/Write access can still delete the .git POINTER FILE
from the worktree directory itself. This backup/restore catches that case.

Both protections together:
- Lock → prevents gc from pruning metadata directory
- Backup → restores pointer file if agent deletes it

Co-authored-by: unknown <that-github-user@github.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: getDiff fails with 'not a git repository' when agents cd out of worktree

1 participant