Skip to content

perf(guardrails): cut the Write/Edit PreToolUse row's spawns from 14 to 7 - #11064

Merged
kyle-sexton merged 5 commits into
mainfrom
host-run/10208-guardrails-spawns
Oct 29, 2026
Merged

kyle-sexton merged 5 commits into
mainfrom
host-run/10208-guardrails-spawns

Conversation

@kyle-sexton

Copy link
Copy Markdown
Member

Closes #10208

Summary

A clean Write, Edit or NotebookEdit cost the guardrails PreToolUse row 14 processes on Linux. It now costs 7. No guard verdict changes. The guardrails-pretooluse-write-edit-spawns ceiling drops from 14 to 7 through ratchet.py propose-tighten --write.

Spawns per guard, from scripts/hook-census.sh on the counter's payload, attributed per pid with strace -ff and bash xtrace:

Guard Before After What remains
Launcher (exec-bash.mjs → run-guards.sh) 3 3 node exec, its fork, bash exec
secret-pattern-detection 5 4 grep fork + exec, its <(printf …) process substitution, one path-resolving subshell (only because the census target sits under a temp root)
hardcoded-path-check 6 0 nothing on clean content
block-windows-drive-tmp 0 0
Row 14 (9 creations, 5 execs) 7 (4 creations, 3 execs)

What was removed:

  • hardcoded-path-check, 6 → 0. Both git probes (git rev-parse and git check-ignore) are now skipped on clean content, and so is the VIOLATIONS=$(…) capture. Every branch after the project-dir check ends in exit 0 when the content is clean. The only difference between those branches is that the clean scan emits an ok telemetry envelope. So when no envelope can be written (the default, telemetry off), the guard decides clean content first, with the builtin prefilter that hpp::scan_text already had. That prefilter now lives in hpp::prefilter and both callers use it. It runs with the project dir as the root. A clean verdict with a root is also clean without one, so the verdict holds whichever scan root git would have chosen. On the dirty path, the rev-parse runs as $(exec git …), which saves the extra fork bash adds for a redirected command in a command substitution. Its exit status (127 when git is missing) and its output are unchanged.
  • secret-pattern-detection, 5 → 4. secrets::any_match (the combined grep that secrets::scan_text already opened with) decides clean content in the parent shell. The scan_out=$(…) capture subshell now runs only on a hit. secrets::scan_text keeps its stdout contract for git-hooks/pre-commit-content.sh.

Left in place on purpose:

  • The <(printf …) process substitution in the secret oracle. A here-string deadlocks at the pipe-size boundary (documented in lib/path-detection/hardcoded-path-patterns.sh), and printf | grep -q under pipefail turns a hit into a miss.
  • The secret grep -E. Replacing it with a builtin was not attempted.

Fix

  • plugins/guardrails/lib/path-detection/hardcoded-path-patterns.sh: moved the builtin gate out of hpp::scan_text into hpp::prefilter. The code moved; it was not copied.
  • plugins/guardrails/hooks/hardcoded-path-check.sh: extracts the content earlier, exits early on a clean prefilter when telemetry is off, runs exec git in the rev-parse captures, and wraps the scan capture in a prefilter check.
  • plugins/guardrails/lib/secret-detection/secret-patterns.sh: adds secrets::any_match, which secrets::scan_text now calls.
  • plugins/guardrails/hooks/secret-pattern-detection.sh: secret_scan calls secrets::any_match before the capture.
  • .performance/ratchets.json: ceiling 14 → 7 via python3 plugins/performance/scripts/ratchet.py propose-tighten --write. The counter's goal text now names the 3 remaining execs. The same run also proposed js-complexity-share 2.54 → 2.04 from this host's lizard. That change is unrelated and was not kept.
  • plugins/guardrails/reference/edit-write-guards/PLAN.md: the table row and the claim paragraph now give the new count.
  • .changes/guardrails/ fragment, patch.

Verification

  • scripts/hook-census.sh on the counter's payload: spawns=14 creations=9 execs=5 before, spawns=7 creations=4 execs=3 after. The Edit form of the payload also measures 7. ratchet.py check: spawns=7 = ceiling. Host versions: bash 5.3.9, dash as sh, git 2.53.0, strace 6.19.
  • Guard suites, with expected verdicts unchanged: secret-pattern-detection PASS=312, hardcoded-path-check PASS=157, block-windows-drive-tmp PASS=843. The shared libraries' other consumers also pass: run-guards PASS=701, abort-boundary PASS=235, pre-commit-content 34 passed.
  • Old-vs-new differential, 144 comparisons, 0 differences. It ran origin/main's guardrails against this branch's on 24 payloads, each through the dispatcher and through each changed guard alone, with telemetry off and on (envelopes compared), under both a temp-root and a non-temp base. It compared exit code, stdout, stderr and envelopes. Payloads covered:
    • clean Write, Edit and NotebookEdit
    • AWS, GitHub-token and PEM hits
    • Linux, Windows and macOS user paths
    • a gitignored file, a non-git project and a file outside the project
    • an allowlisted .claude/hooks path
    • non-ASCII content, with and without a path
    • the root segment in content, in its own and in other case
    • a trailing-slash project dir, empty content, and a dev literal
  • scripts/check-affected.sh --tests on the committed head: 13 passed, 3 skipped.
  • All three guards stay synchronous and fail-closed. hooks/hooks.json is untouched and nothing is async.

Related

🤖 Generated with Claude Code

kyle-sexton and others added 4 commits October 29, 2026 17:34
…to 7

hardcoded-path-check decides clean content with a builtin prefilter
(hpp::prefilter, extracted from hpp::scan_text) before either git probe
when no telemetry envelope can be written, runs rev-parse as
$(exec git ...), and skips the scan capture on clean content.
secret-pattern-detection decides clean content in the parent shell with
secrets::any_match and opens the capture subshell only on a hit.

Lower the guardrails-pretooluse-write-edit-spawns ceiling to 7 with
ratchet.py propose-tighten --write.

Refs: #10208

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kyle-sexton
kyle-sexton marked this pull request as ready for review October 29, 2026 21:44
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T21:49:18.285623Z a258cc8 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

claude-security-review reviewed 801bd524a9da32d5426230e8fa3f2c37fbcdd539: 0 finding(s), 0 as inline comments.

Incremental security review of plugins/guardrails/hooks/hardcoded-path-check.test.sh, the only file changed since a258cc8. I found no security issues.

The change adds two test cases for the clean-content early exit:

  • With telemetry on, the hook still writes exactly one ok envelope.
  • With telemetry off, the hook exits without calling git. This case uses a stub git script in a fresh mktemp -d directory under $TEST_TMPDIR, placed first on PATH for that one env invocation only.

Why nothing is reportable:

  • Where it runs: this is test-only code. It writes only to mktemp paths under $TEST_TMPDIR.
  • Inputs: it reads no secrets and no untrusted input. The fixture strings are literals, and the shell variables are quoted.
  • Production code: the hook's own block list and allowlist are untouched, so there is no instruction-surface deletion.
  • Trust controls: no trust control is touched, and the diff changes no manifest.

Skip gate 2 applies: the change has no security-relevant surface. No cost or spend implications.

critical-findings: 0

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude-review: 1 inline finding(s) on a258cc85c0f27ee4237f537c78068888db240601; the summary is in this lane's pull request comment.

claude-lane: claude-review

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

claude-review reviewed 801bd524a9da32d5426230e8fa3f2c37fbcdd539: 0 finding(s), 0 as inline comments.

Incremental review of plugins/guardrails/hooks/hardcoded-path-check.test.sh: no findings.

The change adds two cases that pin the telemetry condition on the clean-content early exit. The exit is at hardcoded-path-check.sh:347-350.

  • Telemetry on: the test sets HOOK_TELEMETRY_SINK, sends clean content and expects exactly one ok envelope. If the early exit fired with telemetry enabled, no envelope would be written and the test would fail.
  • Telemetry off: the test unsets the sink and puts a failing git shim first on PATH. It asserts exit 0, no stderr, and that the shim's called marker was never created. The marker check is what catches a missing early exit. Without it, the hook's own non-work-tree exit 0 at line 366 would pass the exit-code and silence assertions anyway.

I read hook::telemetry_enabled, which only checks that HOOK_TELEMETRY_SINK is non-empty, and hpp::prefilter. The fixture content echo "hello world" contains none of the gate literals or the project-root segment, so it should be classified clean. I did not run the test suite.

Not tested: the -z "$start" half of the condition, which applies when EPOCHREALTIME is unset. This is minor and I did not count it as a finding.

No cost or security hold applies.

@kyle-sexton
kyle-sexton enabled auto-merge October 29, 2026 21:47
…rly exit

With telemetry on, clean content must still emit exactly one ok envelope;
with telemetry off, it must exit before any git probe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kyle-sexton
kyle-sexton added this pull request to the merge queue Oct 29, 2026
Merged via the queue into main with commit 6bcb6e5 Oct 29, 2026
51 checks passed
@kyle-sexton
kyle-sexton deleted the host-run/10208-guardrails-spawns branch October 29, 2026 22:18
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.

perf(guardrails): cut the Write/Edit PreToolUse row's spawns and tighten its ratchet

1 participant