r/ClaudeCode • • 7d ago

Bug / Issue PSA: isolation: "worktree" subagents rewrite your repo's .git/config behind your back

TL;DR: if your repo uses a relative core.hooksPath (Husky's .husky/_, or any repo-tracked hooks dir), spawning a subagent with isolation: "worktree" runs git config core.hooksPath <absolute path> from inside the new worktree. Worktrees share one config. Your main repo's .git/config just got edited. Three launches, three rewrites.

How it surfaced

My agents hash git config --list --local before and after every run. One came back with "config changed, wasn't me." I restored it, and it changed again. Then I spawned a subagent whose only job was to reply "ok": rewritten about a second later.

What it does (Claude Code 2.1.321, read from the minified binary)

  1. Reads core.hooksPath.
  2. If relative, resolves it against the repo root.
  3. Runs git config core.hooksPath <abs> with the worktree as cwd and logs "Configured worktree to use hooks from main repository."

Good intent: keep hooks working in .claude/worktrees/agent-*. Wrong git. Plain git config in a linked worktree writes the shared .git/config. Only --worktree with extensions.worktreeConfig writes a per-worktree file. An already-absolute value causes no write; unset I haven't tested.

Who's exposed

  • Husky v9 (.husky/_ is relative per its source). Logic says yes; not tested.
  • Anyone running git config core.hooksPath scripts/hooks-style setups.
  • Anyone using isolation: "worktree" in Agent calls, skills or subagent defs. No warning shown.

Why it's not cosmetic

  • Wrong hooks, wrong tree. With an absolute path, a push from any worktree runs the main checkout's copy of the hook. A hook that does cd "$(dirname "$0")/../.." now validates main, not your branch. Your branch passes because main passes. (Same week, separate bug: tests escaped a fake repo via inherited GIT_DIR and rewrote my real one. Fun.)
  • Moved repo, dead hooks. The path points nowhere. As far as I know git skips a missing hooks dir quietly (untested). There goes your secret scan.
  • Integrity checks scream. You'll blame your own agents first. I did.

Catch it

git config --show-origin --get core.hooksPath   # relative? which file?
stat -f '%Sm' .git/config                       # Linux: stat -c '%y' .git/config

Run it before and after a launch. Flipped to absolute with a matching mtime: that's this bug.

Prevent it

  • Skip isolation: "worktree" in these repos. git worktree add yourself and point the agent at it, or do reviews in a throwaway git clone --no-hardlinks.
  • Enforce, don't ask. A CLAUDE.md rule is a suggestion. Use a PreToolUse hook that denies isolation: "worktree" on the Agent tool, plus git push and git config writes on Bash.
  • Gate pre-push and merge on core.hooksPath being exactly the value you expect.
  • Don't "fix" it by setting the path absolute yourself. See wrong hooks, wrong tree.

Caveats: minified code, reproduced in a throwaway clone and with a no-op subagent. Not in the changelog or docs that I found. Could be fixed in a newer build or differ by platform. Run the two commands and see. Reporting it upstream.

Your agents are probably innocent. The harness did it.

1 Upvotes

9 comments sorted by

•

u/AutoModerator 7d ago

Hey! Thanks for posting to r/ClaudeCode

While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.

For help, project discussions, tips, and general chat, join the ClaudeCode Discord.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/Far_Reindeer8428 7d ago

we saw this with hooks, the worktrees share one config file and every agent that writes it wins. pinning core.hooksPath per worktree fixed it

1

u/BigB-69 7d ago

Good call, per-worktree config is the right tool here. I wanted to see how far it goes against this specific bug, so I tried it on git 2.56: turned on extensions.worktreeConfig, pinned core.hooksPath with git config --worktree, then had a linked worktree run a plain git config core.hooksPath <abs>, the same thing the harness does.

The shared .git/config still got rewritten, since a plain git config without --worktree writes there no matter what. But every worktree kept reading the pinned value, and git worktree add even carried the pin into the new worktree on its own.

So it does neutralize the bug in practice, just not the write itself. If you hash or gate on .git/config, that’ll still trip. Thanks for the tip, I’m adding it as an extra layer.

1

u/IndependentOk9134 7d ago

yeah the shared config behavior across worktrees is such an easy trap to fall into

1

u/Far_Reindeer8428 7d ago

the trap is that it only shows up when two agents start within a second of each other, so it looks flaky

2

u/Apprehensive-Cod699 7d ago

on the husky part, v9's husky command (what the prepare script runs) does git config core.hooksPath .husky/_ every time. so every npm install puts the relative value back and the next worktree subagent rewrites it again, in husky repos it would keep flipping, not happen once

1

u/BigB-69 7d ago

Oh, good catch, that makes it worse in a sneaky way. If prepare runs husky on every install and resets core.hooksPath to .husky/_, then in Husky repos it won’t be a one-time rewrite. It flips: install sets it back to relative, the next worktree subagent makes it absolute again, and so on. So depending on when you look, the config looks fine or doesn’t, which makes it even easier to miss. Thanks for adding that.

1

u/Financial_Leader9701 6d ago

the "wasn't me" response is honestly the weirdest part

.git/config isn't even something you'd normally see in a PR diff

how did you first notice it was being changed? did the hooks just stop working?