Skip to content

fix(#963): pre-tool.sh No-Changes Guard resolves cd targets instead of silently checking the wrong directory - #966

Merged
admarble merged 4 commits into
mainfrom
feature/963-bug-hooks-pre-tool-sh-no-changes-guard-only-re
Aug 25, 2026
Merged

admarble merged 4 commits into
mainfrom
feature/963-bug-hooks-pre-tool-sh-no-changes-guard-only-re

Conversation

@admarble

@admarble admarble commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • resolve_cd_target() (new helper in pre-tool.sh) scans the raw multi-line TOOL_INPUT for the last cd line, resolves quoted or unquoted static-literal targets, and fails open (skips the no-changes check) on anything dynamic (a shell variable, command substitution) or nonexistent — git commit itself already rejects a genuinely empty commit, so a skipped check costs one harmless error, while checking the wrong directory silently strands finished work.
  • The No-Changes Guard's no-cd fallback now reads HOOK_CWD (the command's actual cwd from the PreToolUse payload) instead of the hook process's own $PWD — the same field the feat(locks): per-issue locks give no mutual exclusion on the shared working tree — checkout-scoped protection #901 checkout-lock guard already used correctly.
  • Applied the same fix to the feat(locks): per-issue locks give no mutual exclusion on the shared working tree — checkout-scoped protection #901 checkout-lock guard's _CO_CD extraction (same root-cause pattern, same file — recommended as in-scope in the /spec plan rather than filing a follow-up).
  • Fixed identically across all three hook mirrors (templates/hooks/, hooks/, .claude/hooks/).

Root cause correction: the issue filed this as "only matches single-line cd path && git commit" — that theory doesn't hold; the guard's grep '^cd ' already matches per-line, not just line 1. Empirically verified (see spec comment) the actual failure modes are: a cd "$WT" or cd "/quoted" target keeps its quotes/variable text through extraction and fails the -d test, and the fallback then checks the hook process's own cwd rather than the command's.

Test plan

  • 12 new test cases in __tests__/pre-tool-hook.integration.test.ts (parametrized over all 3 hook mirrors = 36 assertions), covering: multi-line unquoted/quoted cd targets, last-of-two-cd-lines wins, the verbatim incident shape (WT=.../cd "$WT"/git commit), a nonexistent cd target, the payload-cwd-vs-process-cwd fallback (varied independently per feat(locks): per-issue locks give no mutual exclusion on the shared working tree — checkout-scoped protection #901's lesson), and regression preservation (single-line cd && commit, --allow-empty).
  • Full existing suite green: 222/222 in pre-tool-hook.integration.test.ts (195 pre-existing + new), 89/89 in the checkout-lock suites (shares the sibling-site fix).
  • npm run build and npm run lint clean.
  • npm test (full suite): 5653 passed, 3 failed — see Known gap below (unrelated, pre-existing).

Pre-PR AC Verification

AC Description Status Evidence
AC-1 Multi-line commits with a resolvable literal cd target (quoted/unquoted) check that directory; last cd wins when several are present ✅ Implemented 3 new tests: unquoted, quoted, last-of-two-lines
AC-2 Unresolvable cd target (dynamic or nonexistent) fails open instead of checking a different directory ✅ Implemented 2 new tests: verbatim incident shape, nonexistent path
AC-3 No-cd fallback checks the payload's cwd, not the hook process's own cwd ✅ Implemented 2 new tests, payload cwd and process cwd varied independently
AC-4 Existing behavior preserved: single-line cd && commit still resolves, empty commit still blocked, --amend/--allow-empty still bypass ✅ Implemented 195 pre-existing tests unchanged + 2 new explicit regression tests
AC-5 Fix applied identically to all 3 hook mirrors; new tests parametrized over all 3 ✅ Implemented md5 identical across all 3 files; describe.each(HOOK_COPIES)

Sibling site (from spec's Open Question): the #901 checkout-lock guard had the identical cd-extraction blind spot. Took Option A (recommended) — fixed in this PR via the shared resolve_cd_target() helper — rather than filing a follow-up, since it was trivially co-located and strictly reduces duplication.

Mutation verification

Mutation-verified: AC-1/AC-2/AC-3 — reverted resolve_cd_target() and its two call sites to the prior single-line-only, quote-retaining extraction across all 3 mirrors; 18/21 new test cases failed (the 3 that still passed are pure regression-preservation checks for behavior the old code already got right — single-line unquoted cd and --allow-empty, which aren't meant to distinguish old from new); restored the fix, full suite green again (222/222).

Gap fixes (second commit, 7dd2710e)

The three gaps originally documented in the Confidence Check below are now fixed in this PR:

  1. Backslash hardening (was "weakest part"): resolve_cd_target() now fails open on any backslash-bearing cd target — a \ can escape a following $/backtick past the literal-string check, and carries shell meaning the function never resolves. 3 new tests (a real directory literally named foo\bar, clean, must not be resolved-and-blocked).
  2. jq-less truncation (was Open Question 2, originally scoped out): the fallback TOOL_INPUT extraction truncated commands at the first embedded \". Now an escape-aware sed capture + single-scan awk JSON-string unescaper (ordering-safe: a literal \\n stays backslash+n). 12 new tests behind a filtered-PATH symlink farm that makes jq genuinely unavailable, including a HOOK_LOG assertion distinguishing full extraction from the old truncation.
  3. Tautology-detector false positives (was "coverage gaps"): root-caused to two detector defects, both fixed in src/lib/test-tautology-detector.ts — collectBuildOutputVars missed type-annotated declarations (const X: T = ...), and nothing linked a describe.each table's callback parameters back to the table's hooks/*.sh literals. pre-tool-hook.integration.test.ts: 74/74 falsely flagged → 4 advisory-only stragglers (3 pre-existing HOOK_PAIRS blocks + the new jq-sanity test) that need call-site argument-value tracking, a structurally larger detector change deliberately left for a follow-up. The 3 previously-failing tautology-detector-cli.test.ts tests now pass; full suite is 5673 passed / 0 failed.

Mutation-verified: gap fixes — removed the backslash rejection from all 3 mirrors; exactly the 3 gap-A tests failed; restored, suite green. Reverted the jq-less extraction; the HOOK_LOG test failed with the exact predicted truncation (git commit -m \); restored. Reverted both detector fixes; the 2 new detector tests failed; restored.

Pre-PR Confidence Check (original, superseded by Gap fixes above)

  • Weakest part (fixed by gap 1): resolve_cd_target()'s dynamic-content detection was a case glob for $/backtick only.
  • Coverage gaps (fixed by gap 3): npm test's 3 failures in scripts/qa/tautology-detector-cli.test.ts were detector false-positives on spawn-driven shell-script suites.

Closes #963

🤖 Generated with Claude Code

…raction

The guard's TARGET_DIR extraction silently mis-resolved multi-line commit
commands whose cd target was quoted or a shell variable, falling through
to check the hook process's own cwd instead of the command's actual cwd -
stranding real, staged commits behind a false "no changes to commit" block.

resolve_cd_target() scans the raw TOOL_INPUT (not the quote-stripping
SEGMENTS) for the last cd line, resolves quoted/unquoted static literals,
and fails open (skips the check) on anything dynamic - git itself already
rejects a genuinely empty commit, so a missed check costs one harmless
error, while a wrong check strands finished work. Also applied to the
#901 checkout-lock guard's cd extraction (same root-cause pattern, same
file).
…logy-detector describe.each false positives

Gap A: resolve_cd_target() also fails open on backslash-bearing cd targets -
a backslash can escape a following dollar/backtick past the literal-string
check, and carries shell meaning the function never resolves.

Gap B: the jq-less fallback's TOOL_INPUT extraction truncated any command at
its first embedded escaped quote, so on jq-less machines the guards saw
'git commit -m \' instead of the full command. Replaced with an escape-aware
sed capture plus a single-scan awk JSON-string unescaper (ordering-safe for
literal backslash-n sequences). Both mirrored identically across the three
hook copies; 15 new integration tests including a filtered-PATH symlink farm
that makes jq genuinely unavailable.

Detector: collectBuildOutputVars required '=' immediately after the name, so
type-annotated table declarations hid their hooks/*.sh literals; and helpers
spawning a describe.each callback parameter had no link to the table it was
destructured from. Declaration matcher now tolerates a bounded type
annotation, and collectDescribeEachParams propagates a build-output table's
callback params. pre-tool-hook.integration.test.ts: 74/74 falsely flagged ->
3 (remainder needs call-site argument tracking; follow-up). Un-breaks
tautology-detector-cli.test.ts on branches touching spawn-driven shell
suites.
…-pre-tool-sh-no-changes-guard-only-re

# Conflicts:
#	CHANGELOG.md
@admarble
admarble merged commit e8e8728 into main Aug 25, 2026
9 checks passed
@admarble
admarble deleted the feature/963-bug-hooks-pre-tool-sh-no-changes-guard-only-re branch August 25, 2026 12:48
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(hooks): pre-tool.sh No-Changes Guard only recognizes single-line 'cd path && git commit', silently blocking real multi-line commits

1 participant