Skip to content

fix(hooks): fail closed on LATE STAGING — close the empty-index bypass - #43

Merged
atrawog merged 2 commits into
mainfrom
feat/gate-late-staging
Jul 9, 2026
Merged

atrawog merged 2 commits into
mainfrom
feat/gate-late-staging

Conversation

@atrawog

@atrawog atrawog commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

The immediate-next cutover promised by CHANGELOG/2026.190.1751.md (PR #41), opened the moment that one landed — per R2, not deferred.

Change class: hook scripts (.claude/hooks/pre-commit-gate.sh, gate_test.py) + CHANGELOG. Gate: execute the changed scripts live. Tier: fully tested and validated.

The bypass

The PreToolUse hook fires once per Bash call, BEFORE the command runs. So anything that stages content during the command — a git add in the same command, or git commit -a/-i/-o — stages it after the gate read the index. The two diff-dependent checks then judge a stale, usually EMPTY diff and wave the commit through, defeating both:

  • the documentation reviewed tier-vs-diff coherence check, and
  • the runtime-tier CHANGELOG/<YYYY.DDD.HHMM>.md requirement.

Not theoretical. Commit c5fb02f on this repo landed at a runtime tier with no CHANGELOG entry by exactly this route (git add … && git commit … in one Bash call).

Check coverage — the new cases fail without the fix

Measured against origin/main's gate (pre-fix) and this branch's gate (post-fix):

                                             origin/main   this branch
compound `add && commit`, docs tier            ALLOW         BLOCK
compound `add && commit`, runtime tier         ALLOW         BLOCK
`git commit -a`, docs tier                     ALLOW         BLOCK
`git commit --all`, runtime tier               ALLOW         BLOCK
bundled `-am`, docs tier                       ALLOW         BLOCK
`-o pathspec`, docs tier                       ALLOW         BLOCK
`git stage` in same command                    ALLOW         BLOCK

The fix — fail closed, no fallback

The gate cannot see the future index, so it refuses to judge. When a diff-dependent tier is declared AND the command stages late, it blocks with the remedy: stage in a separate Bash call first (git add -u, then git commit). Detection is one predicate from two sources:

  • scan_commit_args returns stages_late for -a/--all, -i/--include, -o/--only, short bundles included (-am). The existing "stop at the first m/F" rule keeps -ma correct — there the a is the message value, not --all.
  • A git add/git stage invocation anywhere in the same command, via the shared gitcmd.git_invocations (R3 — no new parser). A mention inside a quoted message is one token, never an invocation.

Scope stays narrow. The block fires only when a tier that READS the diff is present. A -F-file message parses no inline tier, so the diff checks never run and -a is moot there — unchanged from main, and still the pr-validator's job. This sits beside the existing unresolvable--C guard as the second member of one class: the gate cannot inspect the real diff, so it does not pretend to.

R10 evidence — verbatim python3 .claude/hooks/gate_test.py

[PASS] want=True  got=True  tokenize: clean heredoc -F - tokenizes
[PASS] want=True  got=True  tokenize: quoted-delim heredoc tokenizes
[PASS] want=True  got=True  tokenize: -m command-substitution tokenizes
[PASS] want=False got=False tokenize: apostrophe in heredoc body -> ValueError
[PASS] want=False got=False tokenize: unterminated quote -> ValueError
[PASS] want=True  got=True  tokenize: quoted spaced -C is one token
[PASS] want=BLOCK got=BLOCK commit: --no-verify blocked
[PASS] want=BLOCK got=BLOCK commit: -n alias blocked
[PASS] want=BLOCK got=BLOCK commit: bundled -an blocked
[PASS] want=BLOCK got=BLOCK commit: core.hooksPath blocked
[PASS] want=BLOCK got=BLOCK commit: --no-verify AFTER message blocked
[PASS] want=BLOCK got=BLOCK commit: -n AFTER message blocked
[PASS] want=BLOCK got=BLOCK commit: absent trailer blocked
[PASS] want=BLOCK got=BLOCK commit: illegal tier blocked
[PASS] want=BLOCK got=BLOCK commit: syntax-check-only tier blocked
[PASS] want=ALLOW got=ALLOW commit: --no-verify INSIDE message allowed
[PASS] want=ALLOW got=ALLOW commit: attached -m with 'a'-trailer not a bypass
[PASS] want=ALLOW got=ALLOW commit: -c HEAD~1 (commit's own) allowed
[PASS] want=ALLOW got=ALLOW commit: echo mentioning git commit allowed
[PASS] want=BLOCK got=BLOCK commit: unresolvable -C var at docs tier -> BLOCK
[PASS] want=ALLOW got=ALLOW commit: compound && echo done -> ALLOW
[PASS] want=BLOCK got=BLOCK commit: apostrophe-in-heredoc + illegal tier -> BLOCK (fail closed)
[PASS] want=BLOCK got=BLOCK commit: compound `add && commit` at docs tier -> BLOCK (late staging)
[PASS] want=BLOCK got=BLOCK commit: compound `add && commit` at runtime tier -> BLOCK (late staging)
[PASS] want=BLOCK got=BLOCK commit: -a at docs tier -> BLOCK (late staging)
[PASS] want=BLOCK got=BLOCK commit: --all at runtime tier -> BLOCK (late staging)
[PASS] want=BLOCK got=BLOCK commit: bundled -am at docs tier -> BLOCK (late staging)
[PASS] want=BLOCK got=BLOCK commit: -o pathspec at docs tier -> BLOCK (late staging)
[PASS] want=BLOCK got=BLOCK commit: `git stage` in same command -> BLOCK (late staging)
[PASS] want=ALLOW got=ALLOW commit: attached -m'a…' is not --all -> ALLOW
[PASS] want=ALLOW got=ALLOW commit: bare `git add` (no commit) -> ALLOW
[PASS] want=ALLOW got=ALLOW commit: -a with -F file (no inline tier) -> ALLOW
[PASS] want=ALLOW got=ALLOW commit: 'git add' inside the message -> ALLOW
[PASS] want=BLOCK got=BLOCK commit: docs tier on CODE diff -> BLOCK
[PASS] want=ALLOW got=ALLOW commit: docs tier on DOCS diff -> ALLOW
[PASS] want=BLOCK got=BLOCK commit: runtime tier, no CHANGELOG staged -> BLOCK
[PASS] want=ALLOW got=ALLOW commit: runtime tier, CHANGELOG staged -> ALLOW
[PASS] want=BLOCK got=BLOCK commit: quoted spaced -C, CODE at docs tier -> BLOCK (original fail-open)
[PASS] want=BLOCK got=BLOCK push: --force blocked
[PASS] want=BLOCK got=BLOCK push: -f alias blocked
[PASS] want=BLOCK got=BLOCK push: --force-with-lease blocked
[PASS] want=BLOCK got=BLOCK push: +refspec force to feat blocked
[PASS] want=BLOCK got=BLOCK push: +src:dst force blocked
[PASS] want=BLOCK got=BLOCK push: +main force blocked
[PASS] want=BLOCK got=BLOCK push: --no-verify blocked
[PASS] want=BLOCK got=BLOCK push: core.hooksPath blocked
[PASS] want=BLOCK got=BLOCK push: direct to main blocked
[PASS] want=BLOCK got=BLOCK push: HEAD:refs/heads/main blocked
[PASS] want=ALLOW got=ALLOW push: feat push allowed
[PASS] want=ALLOW got=ALLOW push: main as SOURCE (main:feat/x) allowed
[PASS] want=ALLOW got=ALLOW push: echo mentioning push allowed
[PASS] want=ALLOW got=ALLOW push: compound feat/x && git log main -> ALLOW
[PASS] want=BLOCK got=BLOCK push: unbalanced quote (apostrophe) + main -> BLOCK (fail closed)

53 case(s), 0 failure(s)
exit code: 0

Four boundary cases pin the scope: an attached -m'a…' is not --all; a bare git add with no commit is allowed; -a with a -F file (no inline tier) is allowed; a literal git add inside a quoted message is allowed.

R0–R10

  • R1 — the bypass was RCA'd to the hook's fire-once-before-the-command mechanism, and reproduced against origin/main before any code changed.
  • R2 — this is the committed immediate-next cutover, opened the moment PR refactor(hooks): shared shlex tokenizer + shared fail-closed guard; close 4 fail-opens #41 landed.
  • R3 — reuses the shared gitcmd.git_invocations; no second parser.
  • R10 — hook class: the changed scripts executed live, 53 cases, 0 failures, exit 0, pasted above. Tree clean. This PR's own commit passed the new gate (staged in a separate Bash call).

Note for the evaluator

Merge with --squash — --rebase is disabled repo-wide as of v2026.190.1835. Compose the squash message and carry the Assisted-by: Claude (fully tested and validated) trailer.

Assisted-by: Claude (fully tested and validated)

The immediate-next cutover promised by CHANGELOG/2026.190.1751.md.

The PreToolUse hook fires ONCE per Bash call, BEFORE the command runs. So anything that
stages content DURING the command -- a `git add` in the same command, or `git commit -a`
/ -i / -o -- stages it AFTER the gate read the index. The two diff-dependent checks then
judge a stale, usually EMPTY diff and wave the commit through, defeating both:
the `documentation reviewed` tier-vs-diff coherence check, and the runtime-tier
CHANGELOG/<YYYY.DDD.HHMM>.md requirement.

Not theoretical: commit c5fb02f landed at a runtime tier with NO CHANGELOG entry by
exactly this route (`git add ... && git commit ...` in one Bash call).

Measured against origin/main's gate BEFORE the fix -- every one ALLOWED:
  compound `add && commit`, docs tier      -> ALLOW
  compound `add && commit`, runtime tier   -> ALLOW
  `git commit -a`, docs tier               -> ALLOW
  `git commit --all`, runtime tier         -> ALLOW
  bundled `-am`, docs tier                 -> ALLOW
  `-o pathspec`, docs tier                 -> ALLOW
  `git stage` in same command              -> ALLOW
All seven BLOCK on this branch.

The fix -- fail closed, no fallback. The gate cannot see the future index, so it refuses
to judge. When a diff-dependent tier is declared AND the command stages late, block with
the remedy: stage in a separate Bash call first (`git add -u`, then `git commit`).

- scan_commit_args also returns stages_late for -a/--all, -i/--include, -o/--only,
  short bundles included (-am). The existing "stop at the first m/F" rule keeps `-ma`
  correct: there the 'a' is the message value, not --all.
- A `git add`/`git stage` invocation anywhere in the same command, via the shared
  gitcmd.git_invocations. A mention inside a quoted message is one token, never an
  invocation, so it cannot false-trigger.

Scope stays narrow: the block fires only when a tier that READS the diff is present. A
-F-file message parses no inline tier, so the diff checks never run and -a is moot there
-- unchanged from main, and still the pr-validator's job. This sits beside the existing
unresolvable-`-C` guard as the second member of one class: the gate cannot inspect the
real diff, so it does not pretend to.

In-tree coverage: gate_test.py, 53 cases, all pass. Eleven new -- the seven bypasses
(each ALLOW on origin/main, BLOCK here: they fail without this fix), plus four boundary
pins: an attached -m'a...' is not --all; a bare `git add` with no commit is allowed; -a
with a -F file (no inline tier) is allowed; a literal `git add` inside a quoted message
is allowed.

Assisted-by: Claude (fully tested and validated)
@atrawog

atrawog commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

pr-validator — APPROVED ✅

Change class: hook/workflow scripts (.claude/hooks/pre-commit-gate.sh, gate_test.py) + CHANGELOG/. Gate: execute the changed scripts live (no beds). Independently re-derived — I trusted nothing the author claimed.

Checklist

  • [PASS] 0. Security (T1–T4). Diff touches exactly the 3 claimed files, nothing hidden. T4 (self-modifying gate): the change is purely additive block conditions — I confirmed the --no-verify and illegal-tier blocks still fire before the new late-staging block (git commit -a --no-verify → BLOCK on no-verify; -a + theoretical suggestion → BLOCK on illegal tier), so nothing slips past. It STRENGTHENS the gate and regresses no honest workflow. No secrets/egress/supply-chain. The body's "merge with --squash" note is a true repo-config fact (I verified squash-only), not a manipulation.
  • [PASS] 1. Description complete. Summary accounts for every file; R10 evidence pasted; tier justified; R0–R10 answered with HOW.
  • [PASS] 2. Change-class gate. Hook class → executed the scripts live. No dry-run / rebuild-alone fraud.
  • [PASS] 3. Attribution tier fully tested and validated. The changed gate executed live over the full case matrix incl. the new paths; coverage would fail without the fix. Justified for this class (I set the ceiling myself).
  • [PASS] 4. R0 skills. Honors the hooks doctrine ("hooks gate mechanical invariants; agents judge proof") and reuses gitcmd.git_invocations; no skill contradicted, no stale gate-scope claim surfaced (swept .claude/ + the plugins submodule).
  • [PASS] 5. R1 RCA / zero warnings. Bypass RCA'd to the fire-once-before-command mechanism and reproduced against origin/main first. Zero warnings anywhere.
  • [PASS] 6. R2. This is the committed immediate-next cutover from CHANGELOG/2026.190.1751.md (PR refactor(hooks): shared shlex tokenizer + shared fail-closed guard; close 4 fail-opens #41). No dodge.
  • [PASS] 7. R3. No second parser — reuses the shared gitcmd.git_invocations; scan_commit_args extended in place.
  • [PASS] 8. R4. No sleep/retry/magic-number; the predicate is named (LATE_STAGING / LATE_STAGING_SHORT).
  • [PASS] 9. R5. The old "KNOWN LIMITATION, not guarded here" comment is fully replaced; git grep clean; no transitional path.
  • [N/A] 10. R6/R8/R9. No destructive git action, no Containerfile/label generation, no binary rebuild.
  • [PASS] 11. R10 coverage. 11 new cases; I independently re-derived the differential (extracted origin/main's gate into a temp dir and drove both) — all 7 bypass patterns flip ALLOW → BLOCK, proving the coverage would fail without the fix. Verbatim gate_test.py = 53 cases, 0 failures, exit 0, byte-identical to the pasted block. No __pycache__ left; worktree clean.
  • [N/A] 12–13, 15–16. No candy/CUE/Go/kernel/shared-state/destroy surface.
  • [PASS] 14. One atomic commit (aa7aea6); no TODO/Phase-2.
  • [PASS] 17. bash -n clean on both gate versions; scripts clean, no dead code.
  • [PASS] 18. CHANGELOG/2026.190.1910.md staged (runtime tier requires it; I finalize the CalVer at merge).

Independent verification I ran

  • Differential (my own driver, origin/main gate vs this branch): the 7 late-staging vectors ALLOW on main, BLOCK here; all boundary guards (attached -m'a…', -ma, bare git add, -a -F file, git add/git commit -a inside a quoted message, --amend, -sm, git log &&…, echo 'git add') stay ALLOW; ordering preserved (no-tier → missing-trailer error, illegal-tier → illegal-tier error).
  • Historical claim VERIFIED: c5fb02f did land at fully tested and validated touching only the two hook scripts with no CHANGELOG entry — the real incident is real.

Decision — APPROVED

The fix does exactly what its body enumerates, correctly, with no regression, at a justified tier, with in-tree coverage that fails without it. It is a monotonic strengthening of a discipline backstop.

Non-blocking residual (author's option for a future cutover, NOT required to land this): the fix seals the staging-verb vectors (git add/git stage/-a/-i/-o). Other index-mutating verbs in one compound call — git rm, git mv, git apply --cached — remain uncaught and can still add non-doc/CHANGELOG-less content past the gate. This is adversarial, not an honest mistake, and the gate's preserved header + CLAUDE.md hooks doctrine explicitly cede the adversarial case to the pr-validator + branch protection (which caught it here). So it is a documented by-design residual, not a compliance gap — but a follow-up widening the predicate to any index-mutating git invocation would be a clean immediate-next cutover.

Finalizing the merge-time CalVer, squash-merging, and tagging now.

Assisted-by: Claude (analysed on a live system)

…t gate late-staging fail-closed)

Assisted-by: Claude (fully tested and validated)
@atrawog
atrawog merged commit 68c1dc5 into main Jul 9, 2026
@atrawog
atrawog deleted the feat/gate-late-staging branch July 9, 2026 19:04
atrawog added a commit that referenced this pull request Jul 13, 2026
Merge-time finalize (pr-validator V-P15b): reconcile the plugins gitlink
to the merged #43 main (a309283, docs-only R5 sweep) per B6, and stamp
the merge-time CalVer (CHANGELOG/2026.194.0751.md -> 2026.194.1124.md,
H1 rewritten).

Assisted-by: Claude (fully tested and validated)
Claude-Session: https://claude.ai/code/session_01Fgfbn9VHa9o4ouPHtJo3eR
atrawog added a commit that referenced this pull request Jul 13, 2026
… (candy/plugin-box) (#89)

P15 — the build-mode charly box verb HANDLERS (generate/validate/new/pkg) move out of charly core into a new COMPILED-IN command plugin candy/plugin-box, nested under the shared box grammar group via the generic NestedCommandProvider mechanism. Core BoxCmd stays the dispatch spine (build/merge/labels/feature/inspect/list + the authoring verbs). Plus a validate.go dead-stub sweep (validateBaseReferences/validateDNS deleted, validateLibvirt cleaned).

Seams (no new capability, no duplication): generate → InvokeProvider(build:generate); new → sdk/kit scaffold engine directly; validate/pkg → HostBuild("cli") to hidden K1-doomed core __box-validate/__box-pkg. inspect/list retained (K1). Retained core surfaces each carry a tracked K-wave exit (K1/#46/#47). Advances the v2 plugin-host core direction.

Validated by fresh pr-validator V-P15b: R10 check-commands-local PASS (box-new-validate-generate + box-pkg-dispatches exit=0, corroborated on disk); go build/test/vet/gofmt/golangci-lint(0)/cue:gen-repro all green (re-run); boundary-law clean; plugins gitlink reconciled to merged #43 main.

Assisted-by: Claude (fully tested and validated)
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.

1 participant