Repository navigation
Description
Summary
.claude/hooks/pre-tool.sh's "No-Changes Guard" (line ~780-801, templates/hooks/pre-tool.sh and mirrors) extracts the target directory of a git commit by regex-matching TOOL_INPUT for a literal single-line cd /path && git commit (or ;-separated) pattern:
TARGET_DIR=""
if echo "$TOOL_INPUT" | grep -qE '^cd [^;&|]+'; then
TARGET_DIR=$(echo "$TOOL_INPUT" | grep -oE '^cd [^;&|]+' | head -1 | sed 's/^cd //' | tr -d ' ')
fi
if [[ -n "$TARGET_DIR" && -d "$TARGET_DIR" ]]; then
CHANGES=$(cd "$TARGET_DIR" && git status --porcelain 2>/dev/null | wc -l | tr -d ' ')
else
CHANGES=$(git status --porcelain 2>/dev/null | wc -l | tr -d ' ')
fiWhen a phase agent's commit command spans multiple lines — a common shape when composing a shell block with earlier variable setup:
WT="/path/to/worktree"
cd "$WT"
git commit -m "..."— the ^cd [^;&|]+ regex only matches the first line of TOOL_INPUT, so it never sees the cd on line 2, TARGET_DIR stays empty, and the guard falls through to git status --porcelain in the hook's own process cwd (not the worktree the agent is actually operating in). If that cwd has zero changes, the guard fires:
HOOK_BLOCKED: No changes to commit. Stage files with 'git add' first.
— even though real, staged changes exist in the target worktree.
Impact
Any phase agent that writes a multi-line cd $VAR; ...; git commit sequence silently loses its commit under this guard, with real implementation work stranded uncommitted in the worktree. Reproduced live during a sequant run exec phase in a downstream project: a complete, tested implementation (timeout/recycle fix + new test file, 41/41 tests passing) never got committed because of this guard, reported by the CLI as exec left 21 file(s) uncommitted and made no commits.
Repro
From a repo with an existing worktree:
WT="$(git worktree list --porcelain | grep worktree | sed -n 2p | cut -d' ' -f2)"
cd "$WT"
git commit --allow-empty -m "test"Blocked by the guard. Collapsing to a single line:
cd "$WT" && git commit --allow-empty -m "test"Succeeds.
Related, but distinct from #763
#763 fixed a different pre-tool.sh guard family — the ^gh (issue|pr) prefix-skip bypass/false-positive on the five destructive-command guards (templates/hooks/pre-tool.sh:108 and siblings). This is the No-Changes Guard's separate TARGET_DIR extraction for git commit, a different code path that #763's fix did not touch — confirmed still present with the single-line-only regex after #763 merged.
Suggested fix
Either:
- Scan all lines of
TOOL_INPUTfor the lastcd <path>occurrence (not just line 1), or - Use the tool call's actual
cwdfield from the hook payload if available, instead of parsing it out of the command string, or - Check
git status --porcelainin every directory acdin the input touches, rather than assuming a single-line command shape.
Discovery context
Found while dogfooding #960's /loop fix against real downstream projects (admarble/ad-motion). Originally mis-filed there as ad-motion#229 before confirming the guard code is byte-identical to sequant-io/sequant's own current template — this is a sequant hook-template bug, not project-local drift.
Activity
admarble commentedon Aug 24, 2026
Spec Analysis (Plan)
Complexity: Standard (5 derived ACs, 2 directories)
Diagnosis correction (empirically verified)
The guard's
grep -E '^cd 'is line-based, so "only matches the first line" is not the actual mechanism — a multi-line command with a literal unquotedcd /pathon any line extracts fine. Verified by running the extraction logic (templates/hooks/pre-tool.sh:787-797) against six input shapes. The real failure modes:cd "$WT"extracts the literal string"$WT", which fails the-dtest, silently falling into the fallback. This is the shape that stranded the ad-motion commit.cd "/path"keeps its quotes after extraction (tr -d ' 'doesn't strip them), fails-d, falls into the fallback.git status --porcelainin the hook process's own cwd, not the command's shell cwd. The payload'scwdfield is already parsed asHOOK_CWD(pre-tool.sh:29,39) and already used correctly by the feat(locks): per-issue locks give no mutual exclusion on the shared working tree — checkout-scoped protection #901 checkout-lock guard (_CO_CWD="${HOOK_CWD:-$PWD}", line 554) — the No-Changes Guard predates that and never adopted it.head -1picks the firstcdline; a command that cds twice checks the wrong directory.Note: the issue's Repro section uses
--allow-empty, which line 784 exempts from this guard entirely — that repro as written cannot trip this code path. The Impact section's incident shape (WT=.../cd "$WT"/git commit -m ..., no--allow-empty) is the real repro and reproduces via failure mode 1 + 3.Acceptance Criteria
cdtarget is a resolvable literal path (quoted or unquoted) run the no-changes check in that directory, using the lastcdline when several are present. Evidence: integration test in__tests__/pre-tool-hook.integration.test.ts.cdtarget is statically unresolvable (contains$,`, or$() or does not exist as a directory, the guard fails open (allows the command) instead of checking a different directory —git commititself already rejects truly-empty commits. Evidence: integration test using the incident's verbatimWT=.../cd "$WT"/git commitshape with staged changes in the worktree and a clean hook cwd.cdappears in the command, the fallback checksgit -C "${HOOK_CWD:-$PWD}" status --porcelain(the payload'scwd), not the hook process's cwd. Evidence: integration test that varies payloadcwdand process cwd independently (a test that sets them together cannot catch this — the feat(locks): per-issue locks give no mutual exclusion on the shared working tree — checkout-scoped protection #901 lesson).cd /path && git commitstill resolves the target, a genuinely change-freegit commitis still blocked withHOOK_BLOCKED: No changes to commit, and--amend/--allow-emptystill bypass. Evidence: existing suite stays green + regression assertions.templates/hooks/,hooks/,.claude/hooks/) receive the identical fix; new tests parametrize over all three like the existing suite. Evidence: suite's 3-copy parametrization.Implementation Plan
Phase 1 — extraction helper (all 3 pre-tool.sh mirrors)
resolve_cd_target()helper nearseg_match: scan raw$TOOL_INPUT(not$SEGMENTS—emit_segmentsdrops double-quoted regions, socd "$WT"becomescdin segments), take the last line matching^[[:space:]]*cd[[:space:]]+, strip a trailing[;&|].*tail, strip surrounding single/double quotes.cdwas found and the resolved target is a directory →git -C <target> status; if acdwas found but the target is unresolvable/nonexistent → skip the guard (fail open); if nocd→git -C "${HOOK_CWD:-$PWD}" status.Phase 2 — tests
3. Extend
__tests__/pre-tool-hook.integration.test.tswith the four new cases (AC-1/2/3 shapes + first-vs-last cd), parametrized over the 3 mirrors, with the incident shape as a verbatim fixture.4. Mutation-verify: run the new tests against the unfixed guard (revert the hook edit) and confirm they fail, then restore; record the
Mutation-verified:marker in the PR body per repo policy.Fail-open rationale: this guard is UX (clearer message, saved roundtrip), not safety — git natively refuses empty commits with "nothing to commit". A false block strands finished work uncommitted (the incident); a false pass costs one harmless git error message. When in doubt, don't block.
Rejected alternative: resolving shell variables in
cdtargets — reimplements the shell, disproportionate; fail-open covers those shapes safely.Sibling site (user decision needed)
The #901 checkout-lock guard (pre-tool.sh:556-558) has the same first-line, quote-retaining
cdextraction (_CO_CD). Lower severity — its fallback is alreadyHOOK_CWD— but same root-cause pattern in the same file. Option A (recommended): have it call the newresolve_cd_target()helper in this PR (trivially co-located, strictly reduces duplication). Option B: scope out, file follow-up. Not silently widening scope — say which.Open Questions
cdtargets, degrading toHOOK_CWD(mostly harmless, occasionally checks the wrong repo's lock).grep-basedTOOL_INPUTextraction (line 42) truncates at the first escaped quote in multi-line JSON, so on jq-less machines the guard sees a partial command. Recommendation: out of scope (pre-existing, jq is effectively always present); note only.Recommended Workflow
Phases: exec → qa
Quality Loop: disabled
Reasoning:
buglabel, shell-only fix with an existing integration harness; testgen skipped per bug-label rule (tests extend an existing suite rather than needing new stubs).admarble commentedon Aug 24, 2026
/fullsolve Complete
Issue: #963
Status: NEEDS_VERIFICATION (all AC met; one CI check still completing)
Summary
pre-tool.sh's No-Changes Guard silently checked the wrong directory when a commit command'scdtarget was quoted or a shell variable, stranding real staged commits behind a false "no changes to commit" block. Newresolve_cd_target()scans the raw multi-line command for the lastcdline, resolves static-literal targets (quoted or unquoted), and fails open on anything dynamic or nonexistent — applied to both the No-Changes Guard and the sibling #901 checkout-lock guard, identically across all 3 hook mirrors.Root-cause correction: the issue's own theory ("only matches single-line commands") didn't hold up under testing — the guard's
grep '^cd 'already matches per-line. The real bugs were quote-retention and a wrong fallback directory (hook process cwd instead of the command's actual cwd).AC Coverage
cdtarget (quoted/unquoted), last-of-several winscdfallback uses the payload's cwd, not the hook process's own5/5 AC met.
Progress Table
ui/frontend/adminlabelQuality Metrics
Risk Assessment
resolve_cd_target()can misbehave is failing open on a target it could have resolved, which is the safe direction.Known, unrelated gap
npm test's full run surfaces 3 failures inscripts/qa/tautology-detector-cli.test.ts— an advisory-only CI job (#955) whose static heuristic doesn't recognizespawnSync-based shell-script testing as "calling a production function." It flags all 74it()blocks in the touched test file, including ~62 pre-existing ones from earlier issues (#564/#570/#763/#899/#901), unrelated to this fix. Confirmed by running the detector directly and by confirming the same tests pass cleanly onmain(nothing to flag there). Not a merge gate.PR: #966 — #966
Left open for human review —
build (22.x)CI check was still in progress at QA time; all other checks (typecheck, hooks validation, plugin validation, skill validation, tautology-advisory) pass. Re-checkgh pr checks 966before merging.admarble commentedon Aug 24, 2026
CI verification complete
build (22.x)finished after the prior QA pass — all 9 CI checks now pass. PR #966 isMERGEABLE, merge stateCLEAN.Verdict: READY_FOR_MERGE
All 5 AC met, full test suite green, mutation-verified, security scan clean, no remaining blockers. Ready for human review and merge.
fix(#963): harden resolve_cd_target and jq-less extraction; fix tauto…
admarble commentedon Aug 25, 2026
Gap fixes pushed (commit 7dd2710)
All three residual gaps documented at QA time are now fixed on PR #966:
resolve_cd_target()fails open on backslash-bearingcdtargets (a\could escape a$/backtick past the literal-string check). 3 new tests using a real directory literally namedfoo\bar.TOOL_INPUTextraction no longer truncates at the first embedded\"; escape-aware sed capture + single-scan awk unescaper. 12 new tests behind a filtered-PATH symlink farm makingjqgenuinely unavailable, with a HOOK_LOG assertion that distinguishes full extraction from the old truncation.collectBuildOutputVarsmissed type-annotated declarations;describe.eachcallback params had no link to their table'shooks/*.shliterals). 74/74 falsely-flagged blocks → 4 advisory stragglers that need call-site argument-value tracking (structurally larger change, left as follow-up). The 3 previously-failingtautology-detector-cli.test.tstests now pass.Verification: implemented by two sub-agents on disjoint files, then independently verified — diffs reviewed, probes re-run, mutation spot-check (backslash rejection removed → exactly the 3 gap-A tests failed → restored), full suite 5673 passed / 0 failed, build + lint clean, all 3 hook mirrors md5-identical.
Verdict pending only the new commit's CI run; will confirm READY_FOR_MERGE when it reports.
admarble commentedon Aug 25, 2026
CI green on gap-fix head
All 9 checks pass on
a7e512f4(gap fixes + merge of origin/main resolving the #964 CHANGELOG conflict). PR #966 isMERGEABLE/CLEAN.Verdict: READY_FOR_MERGE
fix(#963): pre-tool.sh No-Changes Guard resolves cd targets instead o…