Skip to content

fix(hooks): scope commit-message extraction to the git-commit segment (#981) - #999

Merged
admarble merged 21 commits into
mainfrom
feature/981-fix-hooks-commit-message-validator-extracts-from-t
Sep 8, 2026
Merged

admarble merged 21 commits into
mainfrom
feature/981-fix-hooks-commit-message-validator-extracts-from-t

Conversation

@admarble

@admarble admarble commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • `pre-tool.sh`'s commit-message validator scanned the entire compound command for a commit message, so an earlier quoted string or an unrelated later heredoc could be mistaken for the message and block a valid conventional commit.
  • Adds `raw_commit_segment`, which scopes extraction to the top-level segment that actually contains `git commit` (respecting quotes, `$( )` nesting, and heredoc boundaries), and extraction now reads from that segment instead of the whole `TOOL_INPUT`.
  • Mirrored identically across all three hook copies (`templates/hooks/pre-tool.sh`, `.claude/hooks/pre-tool.sh`, `hooks/pre-tool.sh`).

Pre-PR AC Verification

AC Description Status Evidence
AC-1 Earlier quoted string doesn't hijack a valid commit ✅ Implemented __tests__/pre-tool-hook.integration.test.ts:864 "981 AC-1"
AC-2 Unrelated later heredoc doesn't hijack a valid commit ✅ Implemented __tests__/pre-tool-hook.integration.test.ts:879 "981 AC-2"
AC-3 Non-conventional message in a compound command still blocked, correct Got: text ✅ Implemented __tests__/pre-tool-hook.integration.test.ts:897 "981 AC-3"
AC-4 Heredoc commit-message idiom still validates first line (both conventional and non-conventional) ✅ Implemented __tests__/pre-tool-hook.integration.test.ts:912,929 "981 AC-4"
AC-5 Fix mirrored across all three hook copies ✅ Implemented md5 -q templates/hooks/pre-tool.sh .claude/hooks/pre-tool.sh hooks/pre-tool.sh | uniq | wc -l → 1
AC-6 Existing commit-format tests stay green ✅ Implemented npx vitest run __tests__/pre-tool-hook.integration.test.ts → 252/252 passed

Test plan

  • npx vitest run __tests__/pre-tool-hook.integration.test.ts — 252/252 passed
  • npm run build — passes
  • npm run lint — passes
  • md5 check across the three hook copies — identical
  • bash -n syntax check on all three hook copies

Closes #981

A+ pass (runner, 2026-09-07, commit 0fc9f52)

Addresses the QA round-3 notes (two latent hdseen inconsistencies, both masked by the -m-anchoring gap the issue's own Suggested-fix named) — and, found while baselining them, a fail-open: a non-conventional commit after a heredoc inside the same subshell was never validated (echo "x"; (cat <<'EOF' … EOF\ngit commit -m "updated stuff") exited 0 on 97f6cbca and on main).

  • raw_commit_segment: inside a subshell, <<< is no longer a heredoc introducer (herestring exclusion, symmetric with the depth-0 branches); a real heredoc records its delimiter and the body is consumed to its terminator line, so a commit later in the same ( … ) counts for segment selection again instead of blanking the rest of the subshell.
  • New commit_message_from: the message is read from the commit's own -m argument (anchored on the git commit token, then its -m; handles -m "…", -m '…' with escapes, and the -m "$(cat <<'EOF' … EOF)" idiom), replacing the three grep/sed/awk steps that took the segment's first <<.*EOF heredoc or first double-quoted string. That is what made the two hdseen fixes observable and closes --author="A B" -m … too.
  • Five new end-to-end cases across all three hook copies (fail-open, heredoc-before-commit valid, <<- form, herestring, --author anchoring); 288/288 pass. Three copies byte-identical. CHANGELOG updated.

Differential run of 21 shapes (13 allow / 8 block) through the real hook at that commit: 0 mismatches — but see round 4 below: QA's wider differential against main found three shapes this pass had regressed that my 21 did not cover.

A+ round 4 (runner, 2026-09-07, commit d631f2e)

QA round 4 ran a wider differential against main and found three shapes the -m-anchored extractor had weakened. All three are fixed and pinned:

  • -m inside an earlier quoted argument (--author="uses -m flag" -m "updated stuff"): the scan now skips quoted regions and keeps scanning past a candidate -m that is not followed by a quote, instead of aborting.
  • Multi-line -m laundering (-m "wip\n\nfix: sneaky"): only the subject line is validated (head -n 1 on the extracted message), so a conventional-looking body line cannot pass a non-conventional subject.
  • -F- / --file=- with a heredoc: with no quoted -m, the extractor falls back to the first line of the heredoc feeding the commit — what main's <<.*EOF branch validated. This needed raw_commit_segment to keep a depth-0 heredoc body in the raw segment text (blanked in the code form, so selection is unaffected), which it had been dropping.

Three new cases (both polarities, all three copies), 297/297 pass; copies byte-identical; the commit_message_from header now states the -F fallback accurately; CHANGELOG counts the three fail-opens closed. Local differential at this commit: 16 shapes (8 allow / 8 block), 0 mismatches — and this time the set includes QA's three.

A+ round 5 (runner, 2026-09-07, commit 276c68c)

  • Shadow-segment fail-open closed (QA round 5, spec OQ-2): raw_commit_segment now prints every qualifying segment (\\001-separated, in order) instead of only the first, and the guard walks them and validates the first that yields a message. A -m-less segment that merely mentions git commit — a comment line, git commit-tree, --amend --no-edit, an unquoted echo — can no longer leave MSG empty and skip validation; QA's four repros all block with Got: updated stuff, and the two valid counterparts plus a lone --amend --no-edit / bare git commit still pass. Pinned by a new case over all three copies; 300/300 pass; local differential at this commit: 28 shapes (12 allow / 16 block), 0 mismatches, including every shape any QA round has named.
  • Documented, not changed: the depth-0 and depth>0 heredoc-delimiter scans in raw_commit_segment are near-duplicates (~20 lines each); a refactor into one awk function is a follow-up, not a behaviour change. The spec plan asked for mutation records on AC-1–AC-4 while isGateTestEvidence classifies none of the six ACs as gate tests (they are behavioural exit-code assertions); no record is claimed for them and the parser finds none to reject.

A+ round 6 (runner, 2026-09-07, commit 6a1776d)

  • --message=…, --message …, and short-flag clusters ending in m (-am, -sm, …) are now validated like -m — the pre-existing adjacent fail-open QA round 6 named. commit_message_from recognises the flag forms and hands the argument to one message_at routine (quoted string, $(cat <<'EOF' idiom, or nothing for an unquoted word). Pinned with both polarities over all three copies; --message-x and -F- </dev/null are untouched.
  • Heredoc-delimiter scan deduplicated: raw_commit_segment's depth-0 and subshell branches now share one heredoc_delim awk function (the third copy, in emit_segments, is a different scanner and is left alone).
  • 303/303 pass; copies byte-identical; local differential at this commit: 35 shapes (14 allow / 21 block), 0 mismatches, covering every shape any QA round has named.

A+ round 7 (runner, 2026-09-07, commit 7427aa2)

  • Every commit segment in a compound command is validated (spec OQ-2 as recommended, not the earlier "first message wins"): validation moved into one validate_commit_subject function called for each segment that yields a message, so a decoy conventional commit earlier in a compound command no longer shields a later non-conventional one (the fix: ok ; updated stuff and $(… "fix: decoy") shapes now block on the later message); two conventional commits chained still pass. Pinned by a new case over all three copies. CHANGELOG states it. 306/306 pass; copies byte-identical; local differential at this commit: 41 shapes (17 allow / 24 block), 0 mismatches.
  • Mutation records for AC-1–AC-4 (the spec plan asked for them; isGateTestEvidence does not classify these behavioural ACs as gate tests, so the parser accepts these without gating on them):

Mutation-verified: AC-1 — replaced templates/hooks/pre-tool.sh with origin/main's copy (the pre-fix extractor); __tests__/pre-tool-hook.integration.test.ts (981 AC-1) failed (1 failed | 2 passed — the two untouched copies); restored via checkout on a clean tree.

Mutation-verified: AC-2 — same mutation as AC-1; __tests__/pre-tool-hook.integration.test.ts (981 AC-2) failed (1 failed | 2 passed); restored.

Mutation-verified: AC-3 — made the short-flag scan never recognise m (substr(s, e - 1, 1) == "m" → == "z") so the extractor yields nothing; __tests__/pre-tool-hook.integration.test.ts (981 AC-3) failed (1 failed | 2 passed); restored. (Reverting to main's hook does not fail AC-3 — main already blocked that shape — so AC-3 is a regression guard and needed a skip-validation mutation.)

Mutation-verified: AC-4 — disabled the -m "$(cat <<'EOF' idiom branch ("$(cat " → "$(xxx "); __tests__/pre-tool-hook.integration.test.ts (both 981 AC-4 cases) failed (2 failed | 4 passed); restored.

A+ round 8 (runner, 2026-09-07, commit ca448ce)

  • The raw_commit_segment header now states the per-segment validation the code performs (every segment that yields a message is checked), replacing the stale "first one wins" sentence. All three copies byte-identical.
  • Documented, unchanged from main and out of scope: -F <file> (a message file on disk) is not validated; commit-tree … -m plumbing is blocked by the same guard.
  • On scope: this PR grew past its six ACs across the A+ rounds because each QA round's differential against main found a shape the segment-scoped extractor had regressed or an adjacent fail-open it exposed (--message=, -am, chained commits). Every addition is pinned by a test in both polarities over all three copies, the CHANGELOG enumerates the new blocking behaviour, and the final differential (41 shapes) shows no valid conventional commit newly blocked. The owner may still prefer to split the message-flag additions out; they are the commits 6a1776d5 and 7427aa22.

A+ round 9 (runner, 2026-09-07, commit eba633e)

  • Comment-line false positive closed (QA round 9's differential against main): an unquoted # comment at a word boundary is blanked in raw_commit_segment's code form to end-of-line (raw text kept), and commit_message_from runs its anchor and flag scan on a comment-stripped copy of the segment — so # git commit -m "wip stuff" above a valid commit no longer blocks it (at depth 0 or inside a subshell), while a comment carrying a conventional decoy cannot shield a real non-conventional commit, and a trailing # comment after a real commit is ignored. Pinned in both polarities over all three copies (four allow shapes, three block shapes); 309/309 pass; copies byte-identical; local differential: 45 shapes (21 allow / 24 block), 0 mismatches.
  • CHANGELOG states that unquoted comments are ignored by the guard.
  • Scope decision (QA's pause_for_human): left to the owner as stated in round 8 — the separable commits are 6a1776d5, 7427aa22 and this one. Each round's expansion was driven by a differential-against-main finding that the issue's Done-when clause ("a non-conventional one still is [blocked]" / "never blocked because of text outside its own segment") made a defect, not by new feature intent.

A+ round 10 (runner, 2026-09-07, commit 1a46a8f)

  • Heredoc-idiom false positive closed (QA round 10's differential against main): message_at keyed the heredoc branch on the exact literal $(cat and so blocked $( cat, $( cat, a tab, $(cat<<, $(/bin/cat, and a newline after $(, printing the substitution text as Got:. The branch is now taken whenever the double-quoted argument opens a command substitution that contains a << introducer; with no heredoc inside (or a <<< herestring) it falls through to the generic quoted reader, so -m "fix: ok $(date)" still reads as a plain message. Pinned in both polarities for six spellings over all three copies.
  • The issue's verbatim AC-1 repro, cd … && diff a.md b.md && echo …, is now a test as written (the earlier case had dropped the cd prefix that resolve_cd_target reads).
  • CHANGELOG entry restructured into a lead sentence plus bullets (scoping, flag forms, subject-only, every-segment, closed fail-opens and false positives).
  • Documented, not changed: the character-by-character awk scan is super-linear on very large single arguments (superseded — see the single authoritative measurement in round 17 below).
  • 315/315 pass; copies byte-identical; local differential at this commit: 53 shapes (29 allow / 24 block), 0 mismatches.

A+ round 11 (runner, 2026-09-07, commit df28e2c)

  • -m "$MSG" no longer blocks on the literal $MSG (QA round 11's allow→block flip vs main): a message that is one bare variable reference ($NAME / ${NAME}) cannot be validated statically, so it is treated like an unquoted word and nothing is validated — matching main's bare-form behaviour instead of main's accidental pass (its extractor read the assignment's quoted string). Literal text alongside a variable is still validated as written (Got: $MSG updated stuff). Pinned in both polarities over all three copies; CHANGELOG states it. 318/318 pass; copies byte-identical.
  • Performance sentence in round 10 corrected to the reviewer's measured medians (+1.8 ms at 2 KB … +1.48 s at 77 KB).
  • Documented, unchanged from main: git commit with multiple spaces bypasses seg_match 'git commit' entirely (pre-existing; the guard's own entry condition, not this PR's extractor).
  • Scope decision, for the owner, not the loop: every QA round since 4 has ended with the same pause_for_human — whether to split the adjacent fail-open closures (6a1776d5 message-flag forms, 7427aa22 per-segment validation, eba633ef comment stripping) into their own PR. The reviewer states this is the only thing between the PR and READY_FOR_MERGE. I am not making that call; the PR is otherwise complete, and the runner stops here for fix(hooks): commit-message validator extracts from the whole command, so an earlier quoted string or unrelated heredoc blocks a valid conventional commit #981.

A+ round 12 (runner, 2026-09-07, commit d01b916)

  • Bare-variable exemption replaced with the behaviour main had by accident, done on purpose (QA round 12): a subject that is one bare $NAME / ${NAME} is resolved through the first NAME= assignment in the same command ("…", '…', or a bare word; first line of the value) and that literal is validated — so MSG="updated stuff"; git commit -m "$MSG" blocks with Got: updated stuff exactly as on main, and MSG="fix: ok"; … passes. Only a variable the command never assigns validates nothing (unknowable; like an unquoted word). The round-11 comment that misstated main's behaviour is gone with the exemption. Pinned in both polarities over all three copies; CHANGELOG corrected. 318/318 pass; copies byte-identical.
  • --no-verify whole-input scan (pre-existing fail-open the extractor fix makes newly reachable): recorded as PLAN §9 OQ-21 on PR docs(plan): 2026-09 backlog graph (16 nodes, /graph-run ready) #998 rather than filed (§5 I-7); recommendation there is to scope that scan and the --amend/--allow-empty sibling to the git commit segment through the same raw_commit_segment seam.
  • Documented, not covered: backtick substitution, ANSI-C $'…' quoting and nested $( $( ) ) are not exercised by the suite; the hand-rolled tokenizer fails open (empty extraction skips validation) rather than loud on them — unchanged from main.
  • Scope split remains the owner's decision (rounds 8–11).

A+ round 13 (runner, 2026-09-07, commit 9561151)

  • Variable resolution re-scoped (QA round 13 — my round-12 resolver scanned the raw whole command, reintroducing the very bug this issue closes): literal_assignment_value is now an awk scanner that reads NAME= assignments only in code context — quoted strings, # comments and heredoc bodies are skipped as data — and only in the text before the commit segment (the caller passes the segment's offset), last assignment wins as in the shell. QA's two regressions (# set MSG=updated … and echo 'usage: MSG=updated ./s.sh' above a real MSG="fix: real") now pass; a double-quoted mention and a heredoc-body mention likewise; MSG="fix: a"; MSG="updated stuff"; … now blocks on the value the commit actually carries (an improvement on main's first-match); an assignment after the commit is ignored. Pinned in both polarities over all three copies; CHANGELOG states the resolution scope. 321/321 pass; copies byte-identical.
  • Scope split remains the owner's decision (rounds 8–12).

A+ round 14 (runner, 2026-09-07, commit f89a78e)

  • limit=0 sentinel collision fixed (QA round 14): literal_assignment_value now takes -1 for "whole command"; 0 is a genuine zero-width window, so a commit that is the first segment resolves $MSG against nothing and an assignment after it can no longer block a valid commit. The fallback path passes -1 explicitly; the per-segment path keeps passing the segment offset. The CHANGELOG's "last assignment before the commit" sentence is accurate as written now. Pinned with the reviewer's pair (git commit -m "$MSG" && MSG="…" with and without a leading true &&) over all three copies. 324/324 pass; copies byte-identical.
  • Documented, unchanged: case/esac, process substitution <(…) and $'…' quoting are not modelled by the segmenter; they degrade to the fail-safe whole-command fallback, not a bypass.
  • Scope split remains the owner's decision (rounds 8–13).

A+ round 15 (runner, 2026-09-07, commit 2012bd1)

  • Continuation-widened window closed (QA round 15): raw_commit_segment drops backslash-newline, so a segment containing a continuation no longer matched the raw input and the "before the commit" window silently became the whole command. The window is now located on a continuation-normalized copy of the input (the same copy the resolver scans), and a segment that still cannot be located gets a zero-width window — validating a bare $VAR against nothing — instead of everything. Pinned with the reviewer's shape in both polarities over all three copies. 327/327 pass; copies byte-identical.
  • git -C <dir> commit never matches the guard's entry condition seg_match 'git commit' (pre-existing, identical on main): added to PLAN §9 OQ-21 on PR docs(plan): 2026-09 backlog graph (16 nodes, /graph-run ready) #998 alongside the --no-verify whole-input scan, rather than filed (§5 I-7).
  • Latency: see the single authoritative measurement in round 17 below.
  • Scope split remains the owner's decision (rounds 8–14).

A+ round 16 (runner, 2026-09-07, commit 49432ad)

Both findings live in the variable-resolution work; both fixed and pinned:

A+ round 17 (runner, 2026-09-07) — loop stopped for #981

The reviewer's remaining items are not code changes inside this PR:

  • Performance, one authoritative figure (earlier lines in this body were stale and inconsistent; disregard them): measured by QA at 49432ad1 against main, hook latency delta is +85 ms at 2 KB, +12 ms at 20 KB, +1.81 s at 60 KB, +1.99 s at 131 KB (3.39 s vs 1.39 s at 131 KB). The character-by-character awk scans are super-linear; typical commit commands are well under 2 KB.
  • --no-verify unmasking: the secret/sensitive-file guard greps --no-verify against the whole command, so echo "use --no-verify to skip"; git commit -m "fix: x" skips that guard — pre-existing, but main accidentally blocked such shapes via the very false positive this PR fixes, so this merge makes it newly reachable. Tracked as PLAN §9 OQ-21 on PR docs(plan): 2026-09 backlog graph (16 nodes, /graph-run ready) #998; the owner may want to raise its priority or land it first.
  • Scope split (6a1776d5, 7427aa22, eba633ef, and the variable-resolution commits df28e2cf…49432ad1): the owner's call, escalated since round 8.

All six ACs are met with executed evidence, CI is green, copies are byte-identical, 330/330 tests pass. The runner stops here.

Extracts the commit message from only the top-level segment matching
`git commit`, not the whole compound command, so an earlier quoted
string or an unrelated later heredoc can no longer be mistaken for the
commit message and block a valid conventional commit.

Closes #981

Entire-Checkpoint: ba459169448e
…it guard

Scoping commit-message extraction to the git-commit segment (#981) created
the inverse defect: when the chosen segment held no `-m`, MSG came back
empty and the conventional-commit guard skipped validation entirely, so a
non-conventional message that the pre-#981 code blocked now sailed through.

Two shapes hit it, both regressions against main:

  git commit \
    -m "updated stuff"           # `\`+newline split the segment at the
                                 # backslash, stranding -m outside it

  echo "run git commit later"; git commit -m "updated stuff"
                                 # the quoted mention won segment selection,
                                 # and that segment supplies no -m

raw_commit_segment now honours backslash line-continuation (both at the
unquoted level and inside double quotes, where it also stops `\"` being read
as the closing quote), and selects segments on a quote-blanked CODE form
rather than the raw text — mirroring what emit_segments/seg_match already do,
so this scan agrees with the guard that invoked it.

Verified by running both hook copies against 22 crafted commands: all six
previously-passing #981 cases still allow, both fail-opens now block with an
accurate `Got:` line, and no case regresses against main.

Refs #981

Entire-Checkpoint: fb819d0e2f47
…dies not

Selecting segments on a fully blanked code form (d17c745) made a `git commit`
inside `( ... )` or `$( ... )` invisible: no segment qualified,
raw_commit_segment returned empty, and extraction fell back to the whole
command — reinstating the original #981 false positive for

  echo "mirrors byte-identical"; (cd sub && git commit -m "fix(#943): real")

which had worked at 632e5ac. Subshell-wrapping a commit to scope a `cd` is a
common idiom, and the issue's own repro opens with one.

Subshell CODE now counts toward selection. A heredoc BODY inside the subshell
still does not: once a `<<` introducer is seen at depth > 0 the rest of that
subshell is blanked. Without the guard, a body whose first line reads
`fix: this is heredoc data` wins selection, gets validated as though it were
the message, and the real `git commit -m "updated stuff"` two segments later
is never examined — verified as a genuine fail-open before the guard was added.

Verified across a 32-command differential matrix run through the real hook
entry point on four builds (main, 632e5ac, 6aa876a, this): 0 mismatches, and
this is the only build correct on every case. Adds 4 regression tests.

Refs #981

Entire-Checkpoint: 9af4958e4b1b
…-commit-message-validator-extracts-from-t

# Conflicts:
#	CHANGELOG.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant