Repository navigation
fix(hooks): harden .claude/settings.json cwd resolution (degenerate lockout fix) - #2295
Conversation
…it toplevel Every hook in .claude/settings.json used cd "$(git rev-parse --show-superproject-working-tree || git rev-parse --show-toplevel || echo .)" to locate the project root. This gets fooled when the agent's shell CWD drifts into a nested .git dir (e.g. fbuild/PIO creates its own git init under .build/pio/<env>/ for caching). git rev-parse returns the nested toplevel, the hook tries to cd there and exec ci/hooks/<script>.py which doesn't exist, and exits non-zero — blocking EVERY subsequent tool call. The Stop hook hits the same bug and fires on every cycle, trapping the session in a loop that only a manual `rm -rf .git` or /clear can break. Reproduced the exact failure in /tmp simulation: nested .git under pyproject.toml root caused `git rev-parse --show-toplevel` to return the nested dir; old hook pattern then failed to locate `ci/hooks/<name>.py`, blocking tool use. Fix: all 11 hook commands now walk up from $PWD looking for pyproject.toml (the real project marker). If found, cd there and exec the python hook. If not found (cwd outside any FastLED checkout), emit one stderr warning and exit 0 — skip cleanly instead of hard-blocking. Key property: `exec` replaces the bash process, so real hook failures (e.g. forbidden-command detection) still propagate their non-zero exit code and correctly block the tool call. Only the "project root not found" case becomes non-blocking, which is exactly the degenerate behavior we want to prevent. Verified: - cd into nested .git under a real pyproject.toml → walks up past nested .git, finds pyproject.toml, hook runs at correct root. - cd into /tmp/nowhere → walks to /, no marker, hook skips with single stderr line; subsequent tools unaffected. - json.load validates; all 11 hook commands migrated; none still reference git rev-parse. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
📝 WalkthroughWalkthroughUpdated hook command resolution logic in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In @.claude/settings.json:
- Line 2: The current _cwd_resolution stops at the first pyproject.toml; change
its logic/text to require both pyproject.toml and the specific hook script
(pattern "ci/hooks/<script>.py") to exist before stopping the upward walk, so
the hook only cds into a directory that contains the actual hook; update the
_cwd_resolution description accordingly and apply this same requirement to each
hook command entry (use the hook=ci/hooks/<script>.py pattern referenced in the
review) so the resolver continues walking until both markers are present.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 17682868-3ffc-4d13-91f8-ed587919100a
📒 Files selected for processing (1)
.claude/settings.json
| @@ -1,12 +1,13 @@ | |||
| { | |||
| "_cwd_resolution": "Each hook walks up from $PWD looking for pyproject.toml (the project-root marker). If found, cd there and exec the hook script. If not found (cwd outside project, or inside a nested build dir with its own .git), the hook skips with a warning instead of blocking every subsequent tool call. Do NOT use `git rev-parse --show-toplevel` here — it gets fooled by nested .git dirs that build tools create under .build/.", | |||
There was a problem hiding this comment.
Keep walking until the actual hook script exists, not just pyproject.toml.
Stopping at the first pyproject.toml can still lock the agent out if $PWD is inside a nested Python/build project that has its own pyproject.toml but no ci/hooks/<script>.py. In that case the command execs Python from the wrong directory, the hook script path is missing, and the non-zero failure propagates.
Please make the root marker FastLED-specific, e.g. require both pyproject.toml and the target hook script before stopping the walk.
Suggested pattern for each hook command
- d=$PWD; while [ "$d" != / ] && [ ! -f "$d/pyproject.toml" ]; do d=$(dirname "$d"); done; [ -f "$d/pyproject.toml" ] && cd "$d" && exec uv run python ci/hooks/check_forbidden_commands.py || { echo 'FastLED hook skipped: project root (pyproject.toml) not found above $PWD' >&2; exit 0; }
+ hook=ci/hooks/check_forbidden_commands.py; d=$PWD; while [ "$d" != / ] && { [ ! -f "$d/pyproject.toml" ] || [ ! -f "$d/$hook" ]; }; do d=$(dirname "$d"); done; [ -f "$d/pyproject.toml" ] && [ -f "$d/$hook" ] && cd "$d" && exec uv run python "$hook" || { echo "FastLED hook skipped: project root containing $hook not found above $PWD" >&2; exit 0; }Apply the same hook=ci/hooks/<script>.py pattern to the other hook commands.
Also applies to: 10-10, 19-19, 28-28, 33-33, 45-45, 50-50, 55-55, 67-67, 78-78, 89-89, 100-100
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In @.claude/settings.json at line 2, The current _cwd_resolution stops at the
first pyproject.toml; change its logic/text to require both pyproject.toml and
the specific hook script (pattern "ci/hooks/<script>.py") to exist before
stopping the upward walk, so the hook only cds into a directory that contains
the actual hook; update the _cwd_resolution description accordingly and apply
this same requirement to each hook command entry (use the
hook=ci/hooks/<script>.py pattern referenced in the review) so the resolver
continues walking until both markers are present.
Summary
Every hook in
.claude/settings.jsonwas usinggit rev-parse --show-toplevelto locate the FastLED project root. This gets fooled when the agent's shell CWD drifts into a nested.gitdirectory — e.g., fbuild/PIO creates its own git init under.build/pio/<env>/for incremental-build caching.When that happens,
git rev-parsereturns the nested toplevel, the hook tries tocdthere and execci/hooks/<script>.py, which doesn't exist there, and exits non-zero — blocking every subsequent tool call. The Stop hook hits the same bug and re-fires on each session cycle, trapping the agent in a loop that only a manualrm -rf .gitor session reset can break.What this fixes
cdinto any build-tool-created nested-git directory.Fix
Replace
git rev-parse --show-toplevelwith a shell walk-up forpyproject.toml(the real FastLED project marker). Pattern used across all 11 hook commands:Two key properties:
.gitdirs — stops only at a directory that actually haspyproject.toml, which build-tool scratch dirs don't.execfor the real hook path — replaces the bash process, so genuine hook failures (forbidden commands, TDD guard, etc.) still propagate their non-zero exit code and correctly block the tool call. Only the "project root not found" branch becomes non-blocking.Result: if the CWD is outside any FastLED checkout or deep inside a nested scratch dir, the hook emits one stderr warning and exits 0 — the agent gets a soft skip instead of a hard lockout.
Verification
Reproduced the exact failure in a
/tmpsimulation (nested.gitunder apyproject.tomlroot):git rev-parsereturned the nested toplevel →python ci/hooks/test_hook.py→can't open file. Hook fails; next tool call blocked..build/pio/uno/.git→ findspyproject.tomlat the real root → runs hook correctly.Also verified CWD outside any project (
/tmp/nowhere-special): walks to/, no marker → single warning line, exit 0, subsequent tools unaffected.Scope
11 hook commands migrated (PreToolUse x4, PostToolUse x3, SessionStart, Stop, Notification, UserPromptSubmit). No functional change to hook behavior — only how they locate the root. No other files touched.
Test plan
git rev-parse.🤖 Generated with Claude Code
Summary by CodeRabbit