Skip to content

fix(#763): segment-aware pre-tool guards; drop gh-prefix carve-outs - #771

Merged
admarble merged 3 commits into
mainfrom
feature/763-fix-hooks-gh-issue-pr-prefix-skip-disarms-all-5-pr
Jul 16, 2026
Merged

admarble merged 3 commits into
mainfrom
feature/763-fix-hooks-gh-issue-pr-prefix-skip-disarms-all-5-pr

Conversation

@admarble

@admarble admarble commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #763. The five catastrophic guards in pre-tool.sh were each wrapped with a ! grep -qE '^gh (issue|pr) ' carve-out (added by #564, extended to four more sites by #570) that tested the command's prefix, not its content. Any compound command starting with a real gh issue/gh pr invocation disarmed the guard — including the load-bearing force-push protection — and the same ^ anchor re-broke the false-positive fix on any cd <dir> && gh issue … prefix.

  • Root cause dissolved, not re-patched. All 10 carve-outs are removed. Guards now match against the command words of each shell segment (split on ; && || | and newline, quotes stripped via a small pure-awk/bash splitter). A token inside a quoted --body/--title argument is never a command word (allowed); a real command chained after an allowed one is its own segment (still blocks).
  • rm alternation deleted (AC-1). Redundant with Claude Code's native dangerous-rm analyzer (fires even under bypassPermissions); its rm -rf / fragment substring-matched every absolute path, blocking ordinary worktree/scratch deletes. :108 is now a dedicated command-word sudo guard (not natively covered).
  • Blocked-command logging (AC-5). Every HOOK_BLOCKED logs the offending command + the rule that fired to a single rotated, secret-redacted sink (.sequant/logs fallback instead of the macOS-purged $TMPDIR).

Pre-PR AC Verification

AC Description Status Evidence
AC-1 Delete the rm alternation from :108 ✅ Guard removed; tests assert absolute-path deletes now allowed
AC-2 :108 → command-word sudo guard, distinct message ✅ echo/grep -r sudoku/commit allowed; sudo … blocks (sudo command)
AC-3 All 10 ^gh (issue|pr) carve-outs removed; anchored bypass blocks ✅ 5 anchored-bypass tests block; :295 warn-only asserts no exit-2
AC-4 False positives fixed without the carve-out, incl. cd-prefix ✅ body-text + cd <dir> && gh issue create … both allowed
AC-5 Log command text + rule for every block; redaction + rotation + single sink ✅ log_block/redact_secrets/rotate_log; redaction test
AC-6 Correct the miscalibrated echo gh issue && … tests ✅ Rewritten to anchored gh issue list && …; sudo-message churn fixed
AC-7 Regression corpus = real-hook-reproduced table rows ✅ Every row driven end-to-end through the real hook harness
AC-8 Three copies byte-identical; dist/ regenerated ✅ md5 ef380228… × 3; npm run prepare:marketplace run
AC-9 No material latency regression ✅ 20-run mean: 65ms new vs 84ms old (deleting the rm regex is net-faster)
AC-10 Document Claude Code ≥ 2.1.208 floor in README ✅ Prerequisites note (recommendation, not enforced — plugins can't declare a floor)

Out of scope (per issue): in-workspace destruction guard, the full argv parser + block-list, rm -fr false-negatives, version-floor enforcement.

Test plan

  • npx vitest run __tests__/pre-tool-hook.integration.test.ts — 135 passed (parametrized across all 3 copies)
  • npm run build — clean
  • npm run lint — clean
  • hook-sync + skill-sync integration tests pass
  • Manual: every evidence-table row reproduced against the real hook

Closes #763


Update — QA gaps fixed (57d44b7)

QA found AC-5 not met and one coverage regression; all closed. 10/10 ACs met. The AC-5 and AC-2 rows above are superseded by this section.

AC-5c — log sink. The .sequant/logs fallback was cwd-relative, so it scattered stray dirs, and post-tool.sh was left on $TMPDIR while sharing claude-timing.log with pre-tool.sh — splitting every START/END pair. Worst, the hook created the dir before the no-changes guard read git status --porcelain, so the untracked dir made the tree look dirty and the guard allowed the empty commits it exists to block:

pre-fix (69cc750):  exit=0  (empty commit allowed)              git status: '?? .sequant/'
fixed:              exit=2  HOOK_BLOCKED: No changes to commit   git status: ''

Both hooks now resolve CLAUDE_PLUGIN_DATA/logs → $HOME/.sequant/logs → $TMPDIR with an identical block — absolute, outside every repo, one sink across all worktrees. This departs from AC-5c's suggested repo-local wording (which produced the bug) to meet its stated requirement, "a single non-purged sink".

AC-2 — command-word coverage. The guard was narrower than the code it replaced. emit_segments now covers every position the shell executes: chained, ( … ), $( … ), and $( … ) inside double quotes. Heredoc bodies are skipped as the stdin data they are — without that, git commit -m "$(cat <<'EOF' … EOF)" would block on a message that merely mentions a guarded command, reintroducing the #564/#570 FP class. xargs … / bash -c "…" stay allowed (argument, not command word) and are now pinned by tests.

Verification: 183 hook tests (was 135) · 31/31 real-hook probe matrix · new assertions mutation-checked against the pre-fix hook · AC-9 latency 61.7ms vs 80.4ms on main (n=30) · pre-tool ×3 identical 4fd209d6…, dist/ regenerated · docs corrected in 4 places · 8/8 CI green.

Replace the five `! grep -qE '^gh (issue|pr) '` prefix carve-outs (10 sites,
added by #564/#570) with command-word matching over quote-stripped shell
segments. A real command chained after `gh issue`/`gh pr` (e.g.
`gh issue list && git push --force`) now blocks; body text and `cd <dir> &&`
prefixes are allowed without any carve-out.

- AC-1: delete the `rm -rf /|~|$HOME` alternation (redundant with Claude
  Code's native analyzer; its `/` fragment blocked ordinary absolute deletes)
- AC-2: `:108` is now a command-word `sudo` guard with its own message
- AC-3/4: all carve-outs removed; anchored bypass blocks, false positives stay
  fixed including the `cd`-prefix case
- AC-5: every HOOK_BLOCKED logs command text + rule id to a single rotated,
  secret-redacted sink (.sequant/logs fallback instead of purge-prone $TMPDIR)
- AC-6/7: correct the miscalibrated `echo gh issue && ...` tests to the anchored
  `gh issue list && ...` shape; encode the evidence table across all 3 copies
- AC-8: three hook copies byte-identical; dist marketplace regenerated
- AC-10: document recommended Claude Code >= 2.1.208 in the README

Entire-Checkpoint: c7c3504d285b
admarble added 2 commits July 15, 2026 22:09
…osition

Addresses the QA gaps on AC-5 (log sink) and the guard-coverage narrowing.

Log sink (AC-5c). The fallback was a *relative* `.sequant/logs`, which resolved
against the hook's cwd rather than any fixed root. Three consequences:

  1. It scattered stray `.sequant/` dirs into whatever directory the hook ran
     in, so the AC-7 regression corpus was one sink per directory, not one.
  2. `pre-tool.sh` moved but `post-tool.sh` did not, and the two share
     `claude-timing.log` (START from pre, END from post) plus
     `claude-quality.log`. With CLAUDE_PLUGIN_DATA unset — the npm/CI path —
     every START/END pair split across two files, silently breaking the timing
     instrumentation this hook exists to feed.
  3. Worst: the hook created the directory *before* the no-changes guard read
     `git status --porcelain`. The untracked dir made the repo look dirty, so
     the guard concluded there were changes and allowed the empty commits it
     exists to block. The hook's own side effect turned the hook's guard off.

Both hooks now resolve, with an identical block: CLAUDE_PLUGIN_DATA/logs ->
HOME/.sequant/logs -> TMPDIR. Always absolute, always outside the repo, so it
cannot touch `git status`, and one sink is shared by the main repo and every
worktree. This departs from AC-5c's suggested repo-local wording, which is what
produced the bug; it meets AC-5c's actual requirement ("a single non-purged
sink") more closely.

Guard coverage. emit_segments now treats every position the shell would really
execute as a command word: chained, `( ... )`, `$( ... )`, and `$( ... )` inside
double quotes (double quotes do not disable command substitution). This matters
because the escalation guard has no native Claude Code backstop, and the prior
anchoring left it narrower than the code it replaced.

Heredoc bodies are stdin *data*, not code, and are skipped. Without this, making
`$( )` live inside double quotes would block any commit whose message merely
mentions a guarded command via the standard `cat <<'EOF'` idiom — reintroducing
the #564/#570 false-positive class through a new door. Verified: removing the
skip flips that case to blocked.

Argument positions (`xargs ...`, `bash -c "..."`) stay allowed and are now
locked in by tests: treating an argument as code is the original #564/#570
mistake.

Tests. The prior suite forced CLAUDE_PLUGIN_DATA everywhere, so the fallback
branch real users hit was never exercised — the blind spot both defects shipped
through. Adds a log-sink block driving the fallback across all three copies
(single sink, START/END pairing, guard not self-defeated) plus command-word and
heredoc cases. 135 -> 183 tests. Each new assertion was mutation-checked against
the pre-fix hook.

Docs corrected where the change falsified them: plugin-distribution, worktree-
isolation, what-weve-built, and the mirrored verification-criteria/fullsolve
skill references.

Latency unchanged in the good direction (AC-9): 61.7ms vs 80.4ms on main, n=30.
…-gh-issue-pr-prefix-skip-disarms-all-5-pr

# Conflicts:
#	CHANGELOG.md
@admarble
admarble merged commit 8cdc197 into main Jul 16, 2026
8 checks passed
@admarble
admarble deleted the feature/763-fix-hooks-gh-issue-pr-prefix-skip-disarms-all-5-pr branch July 16, 2026 03:16
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.

fix(hooks): ^gh (issue|pr) prefix skip disarms all 5 pre-tool guards for compound commands, and re-breaks on any cd prefix

1 participant