Skip to content

bug(hooks): commit validator strips # from a git commit -F - heredoc subject, blocking every type(#N): message sent via stdin #1064

Closed

Description

@admarble

Finding

pre-tool.sh's conventional-commit validator mis-parses one commit-message form: git commit -F - <<'EOF' … EOF (message on stdin via heredoc). When the subject contains # — i.e. the repo's own type(#N): … convention — everything from # onward is dropped before validation, and the hook blocks a valid message.

Reproduced 2026-09-15 by feeding the hook its stdin JSON directly (CLAUDE_PROJECT_DIR set, one file staged so the no-changes guard passes):

Form Subject Result
A. git commit -q -F - <<'EOF' … fix(#497): … has # HOOK_BLOCKED — Got: fix( followed by whitespace
B. same, fix(codex): … no # exit 0
C. git commit -m "$(cat <<'EOF' … EOF)" … fix(#497): … has # exit 0
D. git commit -m "fix(#497): …" -m "body" has # exit 0
E. git commit -F /tmp/msg.txt (file contains fix(#497): …) has # exit 0

So the $(cat <<EOF) idiom that #981's segment parser special-cases is fine; the bare -F - + heredoc path is not treated as data and gets the code-side #-comment stripping (#763 class). The Got: text — fix( plus a run of spaces — is consistent with the #… tail being blanked rather than removed.

Impact

Repro

export CLAUDE_PROJECT_DIR=$PWD; touch scratch && git add scratch
printf '%s' $'git commit -q -F - <<\'EOF\'\nfix(#497): subject\n\nbody\nEOF' \
  | python3 -c 'import json,sys;print(json.dumps({"tool_name":"Bash","session_id":"x","cwd":"'"$PWD"'","tool_input":{"command":sys.stdin.read()}}))' \
  | bash .claude/hooks/pre-tool.sh; echo "exit=$?"
git rm --cached scratch && rm scratch

Expected: exit 0. Actual: exit 2, Got: fix(.

Suggested fix

In the segment parser (pre-tool.sh ~L322–460), treat -F - / --file - as "message is the heredoc body of this segment" exactly as the $(cat <<EOF) idiom is treated, so the body is data and # survives. Add forms A–E above to __tests__/pre-tool-hook.integration.test.ts (A is the regression; B–E pin the paths that already work). Mirror to templates/hooks/ and the plugin copy.

Related

#981 (segment extraction — fixed the earlier-quoted-string and unrelated-heredoc paths, not this one) · #763 (comment stripping) · #963 · memory note reference_commit_hook_hash_in_heredoc


Release-graph plan — pre-2.16.0 blockers (2026-09-17)

Graph: pre-2.16.0 release blockers, node 1 of 4 Tier: judgment Wave: 1
Executed by: #1060 gate run 2 (codex) — this issue is the gate's subject; the gate's
PR closes it. Fallback: a Claude run of this same issue if run 2 fails or is NO-GO.
May touch: hooks/pre-tool.sh, .claude/hooks/pre-tool.sh, templates/hooks/pre-tool.sh, __tests__/pre-tool-hook.integration.test.ts, CHANGELOG.md

Root cause (confirmed 2026-09-17)

Not an unhandled path. emit_segments already keeps a -F - heredoc body raw
(pre-tool.sh:537-556). The validator then re-strips it — s = strip_comments($0)
at pre-tool.sh:1273 — and heredoc_first_line reads that stripped copy, so
fix(#497): subject reaches the check as fix( plus blanks.

Prior attempt — salvage the code, discard the test

origin/archive/1064-gate-run1 (gate run 1 of #1060; exec could not commit because of
#1076, since fixed by #1077) threads a raw variable beside s through message_at
and both heredoc_first_line call sites, in all three copies. Verified 2026-09-17
against the matrix below: passes A–D and still blocks a non-conventional message
(no fail-open). Its added test exercises the $(cat <<EOF) path with a # in the
message body — not form A — so it does not gate this bug. Start from that diff;
write the test fresh.

Acceptance Criteria

  • AC-1: git commit -F - with a heredoc subject containing # passes the hook (exit 0) in all three copies, via a form-A integration case — Evidence: npx vitest run __tests__/pre-tool-hook.integration.test.ts -t "1064" 2>&1 | grep -E "Tests +(1[5-9]|[2-9][0-9]+) passed" prints a passed line (5 cases × 3 copies = 15 minimum; zero matches or any failure prints nothing)
  • AC-2: the same -F - heredoc path still BLOCKS a non-conventional subject such as wip (exit 2, no fail-open) — Evidence: npx vitest run __tests__/pre-tool-hook.integration.test.ts -t "1064 blocks" 2>&1 | grep -E "Tests +[3-9] passed" prints a passed line (1 case × 3 copies)
  • AC-3: forms B (-F -, no #), C (-m "$(cat <<EOF)" with #) and D (-m literal with #) stay exit 0 as regression pins, one case each — Evidence: npx vitest run __tests__/pre-tool-hook.integration.test.ts -t "1064 pins" 2>&1 | grep -E "Tests +(9|[1-9][0-9]+) passed" prints a passed line (3 cases × 3 copies)
  • AC-4: the three hook copies are byte-identical — Evidence: shasum hooks/pre-tool.sh .claude/hooks/pre-tool.sh templates/hooks/pre-tool.sh | awk '{print $1}' | sort -u | wc -l prints 1
  • AC-5: the full hook suite passes with no failures — Evidence: npx vitest run __tests__/pre-tool-hook.integration.test.ts 2>&1 | grep -E "Tests +[0-9]+ passed \(" prints a passed line with no failed count
  • AC-6: the PR body records which pre-tool.sh a Claude Code session with the sequant plugin installed actually runs — the repo's .claude/hooks/ copy or ~/.claude/plugins/cache/sequant/sequant/<ver>/hooks/ — with the command and output that settled it — Evidence: gh pr view --json body -q .body | grep -c "Hook precedence:" prints 1

AC-1 and AC-2 are gate tests: mutation-verify per CLAUDE.md §Testing (delete the raw
threading in one copy → AC-1 fails; make the validator print nothing for -F - → AC-2
fails) and record the Mutation-verified: markers in the PR body.

Test naming contract for the selectors above: the form-A case's name contains 1064,
the negative case's contains 1064 blocks, the three pins' contain 1064 pins.

Repro matrix (paste-able; run from the repo root)

export CLAUDE_PROJECT_DIR=$PWD; touch scratch-1064 && git add scratch-1064
run_case () {
  printf '%s' "$2" \
  | python3 -c 'import json,sys,os;print(json.dumps({"tool_name":"Bash","session_id":"x","cwd":os.environ["PWD"],"tool_input":{"command":sys.stdin.read()}}))' \
  | bash .claude/hooks/pre-tool.sh >/dev/null 2>&1; echo "[$1] exit=$?"
}
run_case "A must be 0" "$(printf 'git commit -q -F - <<'"'"'EOF'"'"'\nfix(#497): subject\n\nbody\nEOF')"
run_case "B must be 0" "$(printf 'git commit -q -F - <<'"'"'EOF'"'"'\nfix(codex): subject\n\nbody\nEOF')"
run_case "C must be 0" "$(printf 'git commit -m "$(cat <<'"'"'EOF'"'"'\nfix(#497): subject\nEOF\n)"')"
run_case "D must be 0" 'git commit -m "fix(#497): subject" -m "body"'
run_case "E must be 2" 'git commit -m "wip"'
git rm --cached -q scratch-1064 && rm -f scratch-1064

Today: A=2, B=0, C=0, D=0, E=2. Required: A=0, B=0, C=0, D=0, E=2.

Execution note

In Claude Code sessions the plugin-cache copy of the hook may keep firing until release
(sequant update), so commit with -m + -m, never -F - with a heredoc. Under codex
the worktree's own .claude/hooks copy runs, so the fix is live as soon as it is written.

Activity

  1. added
    bug
    on Sep 15, 2026
  2. added a commit that references this issue on Sep 16, 2026
  3. admarble commented on Sep 18, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (6 ACs, 2 directories)

    AC Quality Check

    Five ACs are clear and testable. AC-2 is lint-flagged solely for “such as”; implementation should pin the stated subject wip (rather than treating it as an open-ended set).


    Scope Assessment

    Non-Goals: ⚠️ Non-Goals section not found. Scope remains limited to the commit-message extractor, its three mirrored hook copies, hook integration tests, and required PR evidence.

    Metric Value Status
    Feature count 1 ✅
    AC items 6 ⚠️
    Directory spread 2 ✅

    Verdict: ⚠️ SCOPE_WARNING — six ACs and a missing Non-Goals boundary warrant the quality loop, but this is one localized bug fix, not a split candidate.


    Acceptance Criteria

    AC-1: Form-A git commit -F - heredoc with # is allowed in all mirrors

    Verification: Unit Test
    Scenario: Given a staged temp repo and git commit -q -F - <<'EOF' with subject fix(#497): subject → When each hook copy runs → Then exit is 0 (five #1064 cases × three copies: at least 15 assertions).
    Assumptions: The existing describe.each(HOOK_COPIES) matrix remains the mirror coverage mechanism.

    AC-2: The same stdin-heredoc path remains fail-closed for wip

    Verification: Unit Test
    Scenario: Given the same command shape with subject wip → When each copy runs → Then exit is 2 and the diagnostic exposes the true subject.

    AC-3: Existing working forms B, C, and D remain allowed

    Verification: Unit Test
    Scenario: Given respectively -F - without #, -m "$(cat <<EOF) with #, and literal -m with # → When hooks run → Then each exits 0 across all copies.

    AC-4: Hook mirrors stay synchronized

    Verification: Integration Test
    Scenario: Given the edited three scripts → When their SHA-1 hashes are compared → Then exactly one unique digest is printed.

    AC-5: The complete hook integration suite is green

    Verification: Unit Test
    Scenario: Given the updated suite → When the full pre-tool integration test file runs → Then Vitest reports no failures.

    AC-6: The eventual PR records active Claude Code hook precedence

    Verification: Integration Test
    Scenario: Given a Claude Code session with the Sequant plugin installed → When hook resolution is inspected experimentally → Then the PR body has exactly one Hook precedence: record with command and output.
    Assumptions: No PR exists yet; this is a release/PR handoff requirement, not evidence that can be completed during implementation alone.


    Implementation Plan

    Phase 1: Correct the parser without weakening comment handling

    1. Start from the archived origin/archive/1064-gate-run1 approach: retain raw = $0, derive s = strip_comments(raw) for anchoring/flag scanning, and pass both through message_at.
    2. Read heredoc subjects with heredoc_first_line(raw, ...) at both call sites (-m "$(...<<EOF)" and the -F - fallback), while retaining s for all code-context recognition.
    3. Apply the exact same patch to hooks/pre-tool.sh, .claude/hooks/pre-tool.sh, and templates/hooks/pre-tool.sh; do not change segment extraction, which already preserves -F - heredoc data.

    Phase 2: Add the regression matrix and verify

    1. Add a new describe.each(HOOK_COPIES) bug(hooks): commit validator strips # from a git commit -F - heredoc subject, blocking every type(#N): message sent via stdin #1064 block beside the fix(hooks): commit-message validator extracts from the whole command, so an earlier quoted string or unrelated heredoc blocks a valid conventional commit #981 extractor tests, using its staged-repo helper. Name the cases so selectors satisfy the contract: one form-A allow case containing 1064, one 1064 blocks negative case, and three 1064 pins allow cases.
    2. Run the three targeted selectors, the full hook integration suite, and the mirror hash command. Mutation-check AC-1 by removing raw-threading in one copy, and AC-2 by suppressing validation for -F -; record both results in the PR body.
    3. Before opening the PR, perform the Claude plugin precedence experiment and append one Hook precedence: section with exact command/output. Update CHANGELOG.md only if the project’s release policy requires this hook fix to be listed.

    Design Review

    1. Where does this logic belong? commit_message_from owns the distinction: sanitized text for shell-code parsing, raw text for the extracted commit-message payload.
    2. What’s the simplest correct approach? Thread a raw sibling string into existing heredoc reads. Replacing/relaxing strip_comments would reopen fix(hooks): ^gh (issue|pr) prefix skip disarms all 5 pre-tool guards for compound commands, and re-breaks on any cd prefix #763-style comment false positives.
    3. What existing pattern does this follow? The existing fix(hooks): commit-message validator extracts from the whole command, so an earlier quoted string or unrelated heredoc blocks a valid conventional commit #981 segment parser already deliberately retains raw commit segments and its tests use the shared three-copy matrix.
    4. What would a senior reviewer challenge? Whether raw text lets a comment masquerade as a commit. It does not: anchors and flag detection still use s; only a heredoc body reached from recognized commit syntax is read raw.

    Feature Quality Planning

    • Test names are part of the AC contract; selector counts depend on the three mirror parameterizations.
    • The archived test must not be reused as the sole regression: it covers -m "$(cat ...)" with # in a body, not form A.
    • The issue’s requested plugin-precedence evidence cannot be automated reliably in a hermetic Vitest run; make it an explicit PR checklist item.

    Open Questions

    1. Should CHANGELOG.md change? — Recommendation: follow the repository’s current release policy; the issue says it may touch it, but no AC requires it. Impact: avoid unrelated churn.
    2. Which hook wins in a real Claude Code + plugin session? — Recommendation: settle empirically before PR submission and record it once. Impact: AC-6 is blocked until that environment check occurs.

    Recommended Workflow

    Phases: spec → testgen → exec → qa
    Quality Loop: enabled
    Reasoning: AC-1/2/3/5 declare unit-test evidence, so testgen should establish the selector-compatible matrix before implementation. This is non-UI shell behavior; browser testing is not applicable. Scope warning enables the quality loop.

    Label Review

    Current: bug
    Recommended: bug, planned
    Reason: This remains a localized bug fix; no UI, security, refactor, or breaking label is supported.
    Quality Loop: Will auto-enable due to the scope warning.

  4. added
    planned
    on Sep 18, 2026
  5. admarble commented on Sep 18, 2026

    @admarble
    ContributorAuthor

    Test Stubs Generated

    AC Verification Happy Path Failure Paths
    AC-1 Unit Test 1 matrix case (3 assertions) #-stripping regression; raw-threading mutation
    AC-2 Unit Test — 1 matrix case (3 assertions): wip must exit 2 and show the true subject
    AC-3 Unit Test 3 matrix cases (9 assertions) regressions of forms B/C/D; selector-name contract
    AC-4 Integration Test 1 SHA-1 parity check mirror drift produces more than one digest
    AC-5 Unit Test 1 full-suite invocation any Vitest failure
    AC-6 Integration Test 1 PR handoff experiment missing/duplicate/incomplete Hook precedence: record

    File status

    The intended test target already exists: __tests__/pre-tool-hook.integration.test.ts. It contains the shared describe.each(HOOK_COPIES) staged-repository matrix and the adjacent #981 extractor cases. Per testgen safety rules, no existing file was overwritten.

    Implementation should add the #1064 matrix block beside #981 with these selector-compatible cases:

    • 1064: Form A, staged repo, git commit -q -F - <<'EOF' and subject fix(#497): subject; assert exit 0 in every mirror.
    • 1064 blocks: same Form A with subject wip; assert exit 2 and diagnostic Got: wip in every mirror.
    • 1064 pins: three allowed forms—-F - without #, -m "$(cat <<EOF)" with #, and literal -m with #; assert exit 0 in every mirror.

    This yields five #1064 cases × three hook copies = at least 15 matrix assertions. Keep code-context anchoring/flag scanning comment-sanitized; read raw text only for a recognized heredoc payload.

    AC-4 verification stub

    shasum hooks/pre-tool.sh .claude/hooks/pre-tool.sh templates/hooks/pre-tool.sh | awk '{print $1}' | sort -u | wc -l
    # expected: 1

    AC-5 verification stub

    npx vitest run __tests__/pre-tool-hook.integration.test.ts
    # expected: no failures

    AC-6 manual integration handoff

    Before creating the PR, inspect active Claude Code hook resolution in a session with the Sequant plugin installed. Put exactly one Hook precedence: section in the PR body, including the command and unedited output. This cannot be made hermetic in the Vitest suite because no PR/session is available yet.


    Generated with Sequant testgen.

  6. added a commit that references this issue on Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions