Skip to content

Run quality checks in the worktree where files were edited - #8

Open
joshuanapoli wants to merge 1 commit into
mainfrom
jn/worktree-aware-hooks
Open

joshuanapoli wants to merge 1 commit into
mainfrom
jn/worktree-aware-hooks

Conversation

@joshuanapoli

Copy link
Copy Markdown
Member

Problem

The Claude Code desktop app starts a session in the main checkout and then does its work in a git worktree (e.g. .claude/worktrees/<name>). CLAUDE_PROJECT_DIR keeps pointing at the main checkout, and every Stop hook ran with cwd=CLAUDE_PROJECT_DIR. So:

  • Edits made in the worktree were never formatted, linted, type-checked, or tested, and failing changes got pushed.
  • Failures caused by other work in the main checkout were fed back to the agent. The agent couldn't fix them from its worktree, so the Stop hook kept blocking, in a loop.

Change

  • checkout_dir(path, project_dir) finds the checkout that contains an edited file by walking up to the nearest .git entry. That entry is a file in a worktree, so worktrees nested inside the main checkout are handled. If the project is a subdirectory of its repository, the same subdirectory of the worktree is used.
  • ruff format, ruff check, mypy, and pytest group the tracked files by checkout and run once in each checkout, using only that checkout's files. Checkouts this session didn't edit are not checked. Only files in checkouts that still fail stay tracked.
  • When the checkouts give different exit codes, the result is 2 if any checkout returned 2, otherwise the first non-zero code.
  • git status runs in the checkout of the session cwd from the hook input.
  • Session tracking files, logs, and toggle state stay under CLAUDE_PROJECT_DIR/.claude, so the edited and Stop hooks always use the same state.
  • The version is bumped to 0.5.1. This is a bug fix and is backward compatible: when no worktree is involved, files resolve to CLAUDE_PROJECT_DIR exactly as before.

Testing

  • New tests/test_checkout.py covers checkout resolution (main checkout, nested worktree, project in a subdirectory, file outside any repo) and each hook's cwd and per-checkout file grouping. It also checks that a failure in one checkout keeps only that checkout's files tracked.
  • I also ran checkout_dir against a real git worktree add .claude/worktrees/feat.
  • ruff, mypy, and pytest all pass (63 tests).

🤖 Generated with Claude Code

The Claude Code desktop app starts a session in the main checkout and then works in a git worktree. The Stop hooks ran in CLAUDE_PROJECT_DIR, so worktree edits were never checked, and unrelated failures in the main checkout were fed back to the agent, which could not fix them and looped.

Group tracked files by the checkout that contains them (nearest .git entry, keeping the project's subdirectory within its repository) and run each check there. Only checkouts that still fail stay tracked. git status uses the session cwd from the hook input. Session state and toggles stay in CLAUDE_PROJECT_DIR.

Co-Authored-By: Claude Opus 5.5 <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.

1 participant