Repository navigation
Description
Part of #6244
Problem
Every Bash and PowerShell tool call on the audited Windows machine waits on two separate guard stacks before it runs:
- guardrails' PreToolUse row (
plugins/guardrails/hooks/hooks.json:53, matcherBash|PowerShell):node exec-bash.mjs run-guards.shwith ten guards sourced into one bash (run-guards.shheader). That is two processes per call, node then Git Bash. - A user-scope Python guard (
guards.py, three engines in one interpreter) registered in the machine's own~/.claude/settings.json. That is a third process, and a second interpreter, per call.
The audit item asks to consolidate these into one process per call. Because these are blocking safety guards, the route is the owner's decision. This issue asks for that decision. It does not propose removing or weakening any guard.
Evidence
Verified this pass (origin/main 26f98e156, docs fetched 2026-10-04):
- The guardrails row and the dispatcher header (
plugins/guardrails/hooks/run-guards.sh:1-60): the ten guards already run in one bash process, which perf(guardrails): run the guard chain in-process and answer payload fields without jq #4185 and guardrails dispatcher is the whole per-tool-call budget miss after the hook-performance program #3685 delivered. - The launcher's cost has been measured. In the 2026-10-01 A/B on melo-lap-001 (
docs/conventions/hook-budget/README.md, "Launcher A/B"), the guardrails PreToolUse Bash row took 166 ms with the node launcher and 114 ms in shell form: +52 ms (2.3 S). The$(...)sample was +40 ms. code.claude.com/docs/en/hooks.md:412: "All matching hooks run in parallel. If you define the same handler in more than one settings file, it runs once. A plugin's or skill's copy of the same handler stays separate." The audit lane suspectedguards.pydouble-fired in home-rooted sessions, where~/.claude/settings.jsonloads as both user and project settings. Per this line it runs once.- Mods (
code.claude.com/docs/en/plugins/mods/events.md:298-314):- A
tool.callhook adds no process per event, though$.process.runinside a mod still spawns one. - A hook with no
.catchthat throws or times out is skipped, so it fails open. - Plugin
PreToolUsesettings hooks run after the last mod callsnext. - ADR 0052 (accepted 2026-10-03) allows mods, with three conditions:
- it keeps settings hooks "where they must run with mods off";
- it requires a conversion to match every behavior;
- it allows one implementation of a behavior at a time.
- A
From the 2026-10-04 audit (Claude Code 2.1.289, 5 to 10 concurrent sessions; E1 attributed measurement, E2 aggregate with cause inferred, E3 inference):
- Transcript hook attachments for the guardrails Bash row: n=1,702, p50 1,280 to 1,583 ms across three status-message variants, p95 3,739 to 11,088 ms, max 31,631 ms (E2). Claude Code writes these attachments only for some firings, those with output or cancelled, so the sample is biased toward slow and blocking fires.
- Across 151,280 Bash calls in about a month, the guard bundle was estimated at 55 to 70 hours of wall time (E2).
- Measured spawn costs on that host: node 95 ms, Git Bash 53 ms, python 85 ms (MEDIUM).
- What this means: the idle-host row costs 114 to 166 ms. The 1.3 to 1.6 s p50 is mostly host contention: concurrent sessions, Defender scanning unsigned Git Bash binaries, and the Windows Token leak (perf(host): Windows kernel Token leak, one per child-creating process, amplified by hook and Bash fan-out (melo-lap-001, melo-desk-001) #4372). Consolidation would remove one interpreter start (about 85 to 150 ms on that host). It would not remove the 1.3 s.
Proposed approach
Options:
- A. Recommended: consolidate through feat(guardrails): generalize machine-local safety guards into plugin guards #5943, keep the guardrails row as it is.
- The second interpreter exists because machine-local guards live outside the plugin.
- feat(guardrails): generalize machine-local safety guards into plugin guards #5943 (open,
status: needs-decision) already plans to port those guards' behavior into guardrails, behind its dispatcher and per-guard kill switches, and then retire the local copies. - When those children land, each Bash call runs one guard stack.
- Retiring the user-scope
guards.pyis the owner's step outside this repository. - Nothing changes in this repo beyond feat(guardrails): generalize machine-local safety guards into plugin guards #5943's children.
- B. Drop the node launcher on the Bash row, back to shell form with
"shell": "bash".- Saves about 40 to 52 ms per call (measured above).
- It reverses the fleet exec-form sweep (Adopt exec-form hook rows fleet-wide once upstream #90495 is ruled out on Windows #3686, feat(hooks): complete exec-form fleet (zero shell-form rows) #5153).
- It needs its own decision on why the launcher exists. The launcher refuses the WSL relay bash and names a failed launch in one stderr line (
lib/exec-bash.mjsheader). - Not recommended for a saving of about 2 S.
- C. Move the Bash and PowerShell guard bundle into a guardrails mod
tool.callhook. This adds no per-event process. Not recommended now:- It is a rewrite of ten bash guards into TypeScript, with every behavior matched.
- It must fail closed through
.catch. - Guards must still run where mods are off. ADR 0052 lists those cases:
allowManagedModsOnlyfor a non-organization plugin, and below 2.1.287. Running both a mod and a settings hook is what ADR 0052 rules out.
- D. An
httphook to a long-lived local guard server. Not recommended: the hooks reference says a connection failure is a "non-blocking error, execution continues" (hooks.md:902), which fails open for a blocking guard.
Recommendation: A. Basis:
- The measured launcher saving is about 2 S per call. The audit's large p50 comes from host contention, which none of these options removes.
- feat(guardrails): generalize machine-local safety guards into plugin guards #5943 already owns moving the machine-local guards into the plugin.
- C and D carry fail-open risks on a security surface.
Acceptance criteria
- The owner records the choice among A, B, C and D on this issue.
- If A: this issue closes with a pointer to feat(guardrails): generalize machine-local safety guards into plugin guards #5943, and feat(guardrails): generalize machine-local safety guards into plugin guards #5943's children say that retiring the local guard also removes the second interpreter per call.
- If B or C: a child issue is filed with a Windows harness before/after (dotfiles#873, interleaved, S stated). It must also show that every guard's verdict is byte-identical on the existing guardrails test corpus, and that a launch failure still yields exit 1 with a one-line stderr diagnosis and no stdout.
Constraints and gotchas
- Owner rule: guards change only when the owner says so. Consolidate or gate; never remove a guard. This issue is decision-only.
- Do not treat the transcript p50 as what consolidation would save; see Evidence.
- Any probe of a guard feeds it the command text as JSON on stdin; never run a destructive command to test one.
- perf(guardrails): Edit/Write guards spawn 25-87 processes per fire #4390 (Edit/Write guards) is parked on the owner's k x S targets. This issue covers only the Bash/PowerShell row.
Context
Source: local handoff item 20261004-150216-claude-perf-audit-plugin-fixes.md, fix 6. Related: #5943 (machine-local guards into guardrails), #3685 and #4185 (one-process dispatcher), #3686 and #5691 (launcher A/B), #4372 (Token leak), ADR 0052 (mods), #4373 (fan-out container).
Activity
github-actions commentedon Oct 4, 2026
Thanks for the report. Automated triage handed this issue to a maintainer.
Automated triage. A maintainer reviews it.
kyle-sexton commentedon Oct 5, 2026
Owner decision (Kyle, 2026-10-04): Option A, no code change in this repo. On main the Bash row already runs all ten guards in one bash process behind the node launcher, so each call costs two processes. Measured on Windows (25 interleaved runs, median, contended host): launcher 288 ms, bash no-op 106 ms, run-guards.sh with ten guards 268 ms; the launcher adds about 180 ms. Dropping the launcher (option B) would break the exec-form check in exec-bash.test.sh and the hook-budget exec-form convention, and Linux sh -c cannot run run-guards.sh. The remaining consolidation continues under #5943.