Description
Related: #4373 (hook fan-out umbrella), #6486 and #6488 (docs-fetcher gate), #3686 (exec-form), #6244.
Problem
Every Bash call pays about 160 ms of hook time, serial on the critical path of every shell command:
- guardrails runs its 10-guard bundle through
node exec-bash.mjs run-guards.sh, a node-to-bash hop: about 150 ms. - discovery and multi-agent each register a node PreToolUse gate on every Bash call (
docs-fetcher-gate.mjs), about 60 ms each. The gate only does work inside thedocs-fetcheragents; "Every other agent and the main thread pass through untouched" (plugins/discovery/lib/docs-fetcher-gate.mjs:15). So in the main thread they spend a node start-up to approve every call.
Measured
Method: direct timing on the windows lane, date +%s%N before and after, stdin from a canned PreToolUse payload, 3 runs, latest cached plugin versions (guardrails 0.48.1, discovery 0.32.4). OTEL context from go-faster run 20261008T152359Z: Bash tool wait is the largest elapsed-time bucket (65 calls, 2,640,274 ms in the sampled session), and #4373 records PreToolUse:Bash n=17,644, p50 301 ms, p95 933 ms.
| hook | runs (ms) |
|---|---|
guardrails bundle: node exec-bash.mjs run-guards.sh --lib ... block-no-verify.sh block-dangerous-git.sh (2 of the 10 guards) |
152 / 143 / 141 |
discovery node lib/docs-fetcher-gate.mjs discovery:docs-fetcher |
62 / 58 / 60 |
multi-agent docs-fetcher-gate.mjs (same file) |
same code; I did not time it separately |
bare node -e 0 |
53 / 52 / 52 |
bare bash -c true |
29 / 28 / 29 |
So the gates are almost entirely node start-up (60 ms against 52 ms bare). The guardrails figure was measured with 2 of 10 guards, so the full bundle is at least this.
Where
plugins/discovery/hooks/hooks.json:17-29(PreToolUse, matcherBash,node lib/docs-fetcher-gate.mjs discovery:docs-fetcher)plugins/multi-agent/hooks/hooks.json:30-46(same gate, with the host allow-list args)plugins/guardrails/hooks/hooks.json:43-77(matcherBash|PowerShell,node hooks/exec-bash.mjs hooks/run-guards.sh ...)
Example command line (discovery): node ${CLAUDE_PLUGIN_ROOT}/lib/docs-fetcher-gate.mjs discovery:docs-fetcher
Proposed fix
- Scope the two gates to the agents. Agent-frontmatter hooks are the documented mechanism: "Claude Code runs them only while that subagent is running and removes them when it finishes" (https://code.claude.com/docs/en/hooks#hooks-in-skills-and-agents). BUT plugin agents ignore that field: "For security reasons, plugin subagents don't support the
hooks,mcpServers, orpermissionModefrontmatter fields" (https://code.claude.com/docs/en/sub-agents, "Choose the subagent scope").plugins/discovery/agents/docs-fetcher.mdand the multi-agent copy are plugin agents, so frontmatter does not work for them as shipped.
Options to evaluate, in order:
a. Narrow withif. The hookiffield takes permission-rule syntax such asBash(git *)on PreToolUse (https://code.claude.com/docs/en/hooks#common-fields). It cannot test the agent, but the gate's allowed command is one fixedbash .../docs-raw.shshape; anifcannot invert to "everything else", so this does not remove the cost for ordinary calls. Not sufficient alone.
b. Replace the node gate with a bash builtin check: readagent_idfrom the payload andexit 0before any fork when it is not a docs-fetcher agent. Bash start-up is 29 ms against node 52 ms; saves about 30 ms per gate per call.
c. Restrict the agent's own tool grant instead of a hook, if thetools/ permission-rule form can pin Bash to the one script (check https://code.claude.com/docs/en/sub-agents tool restrictions; the gate's marker-per-agent once-only rule would still need a hook).
d. Ship the agent as a user-scope agent (documented escape hatch: copy to~/.claude/agents/), which loses plugin packaging. - guardrails: drop the node-to-bash hop for the Bash/PowerShell bundle by registering
bashdirectly with a resolved Git Bash path (see Adopt exec-form hook rows fleet-wide once upstream #90495 is ruled out on Windows #3686/check-hook-exec-form: no gate-legal way to name Git Bash on Windows, which blocks every bash exec-form row #3708 for why barebashwas avoided on Windows, WSL relay), or fold the 3 duplicated gates (discovery, multi-agent, guardrails) so only one process starts per call.
Basis: process costs measured above; docs URLs above. The plugin-agent limitation is the reason this is not a one-line fix.
Expected saving
- Gates (a or b): about 2 x 60 ms per Bash call removed (option b: about 2 x 30 ms).
- guardrails hop: the node start-up share, about 50 ms of the 150 ms per call.
- At 17,644 PreToolUse:Bash fires per 7 days (perf(hooks): Stop and per-tool-call hook fan-out exceed budget on Windows (Stop p50 2.9 s, grows with session length) #4373) that is about 35 to 60 CPU-minutes per week for the gates alone.
Verify after the fix
- Re-run the three timing commands above; the gate rows should be under 35 ms (bash) or absent.
/harness-ops:observability latencyPreToolUse:Bash p50 before and after, 7 days each.- Inside a docs-fetcher run a second
docs-raw.shcall and a piped command are still denied (existing tests for fix(discovery): the docs-fetcher gate checks only the literal host #6486 must stay green), and a Bash call in the main thread spawns no gate process.
Metadata
Metadata
Assignees
Labels
Type
Fields
Work status
Activity
github-actions commentedon Oct 8, 2026
Proposed work class: work-class: scoped
Basis: automated intake triage; the lanes App applied this class.
Size: Small
Complexity: Low
Uncertainty: Known
Flags: none
Blocked by: none
#6634 (comment)
This decision settles the open questions. What remains is two measurement checks: a Windows host timing run and a 7-day p50 comparison. Neither is something a lane run can do, so admission routes them.
Automated triage, applied by the lanes App.
kyle-sexton commentedon Nov 2, 2026
Claimed by a host-run session. This host is timing the docs-fetcher gates and the guardrails
Bash|PowerShellrow (baseline first, then after), and building a plugin-local cut in the docs-fetcher gate only (no cross-plugin merge, per ADR 0019), on branchhost-run/6634-docs-fetcher-gate-fast-path. A record follows here when the run ends.This was generated by AI.
kyle-sexton commentedon Nov 2, 2026
Host run. Timed the docs-fetcher gates and the guardrails
Bash|PowerShellrow before and after a plugin-local cut to the gate, on a WSL2 Linux host.What ran
edef3f697a(before) and the branch's first commite985a06fe7(after). The branch head0955877c54differs from it only in a code comment.hooks.jsonrow registers it (node <script> <args>), stdin from a cannedPreToolUsepayload, from a scratch git repository, timed withEPOCHREALTIME. All rows interleaved in each run, withbash -c :(S) andnode -e 0(the node floor) in the same run.node --test lib/docs-fetcher-gate.test.mjsandlib/docs-fetcher-gate.test.sh(26 of 26 pass each), andscripts/check-affected.sh --testson the committed head (13 passed, 3 skipped: shellcheck, ruff and changed-skills, none applicable). On the head,scripts/check-affected.sh --testsran 12 checks green. Invalidate-plugins, one mod test of 152 failed during a 198 s run under host load. A rerun ofscripts/validate-plugins.sh --only "discovery multi-agent"passed 152 of 152 in 4.6 s.sync-shared-copies.sh --checkand--check-bump origin/mainpass.Results (WSL2, ms, medians; valid: every run exited 0 with the expected allow or deny)
Baseline, n=30: S 1.9, node floor 17.8, discovery gate main thread 25.1, multi-agent gate main thread 24.8, docs-fetcher allowed call 26.7, docs-fetcher denied call 25.9 (deny), guardrails
Bash|PowerShell51.0.A/B, three rounds of n=30 per arm:
node -e 0)BashBashSo each gate's time above the node floor fell from about 7.5 ms to about 4 ms on a main-thread call, about 3.5 ms per gate per Bash call. The ranges of the two arms do not overlap in any round. Before the change, an import probe put most of the gap on
node:crypto(about 3.5 ms) andnode:fs(about 2 ms) as ESM imports.These are WSL2 figures. The issue's figures come from the Windows lane, where node starts in about 52 ms, so the two are not comparable.
The change
The gate (canonical
lib/docs-fetcher-gate.mjs, regenerated intoplugins/discovery/lib/andplugins/multi-agent/lib/byscripts/sync-shared-copies.sh) loadsnode:fsandnode:cryptothroughcreateRequireinsideprivateDirandclaimOnce. Only the named agent's allowed call reaches those functions. Routing still uses the parsedagent_type, with no raw-text shortcut. Exports, decisions,hooks.jsonandonFailure: "block"are unchanged..claude/rules/mod-authoring.mddoes not apply: nohooks.jsonor hooks module changed.Options rejected, with the reason
bashascommandresolves to the WSL relay on Windows (header oflib/exec-bash.mjs), andscripts/check-hook-exec-form.shrejects it. Shell form is reserved for the node-notice rows (docs/contracts/hook-budget/README.md, Exec-form fleet sweep, Scope).if: it takes one permission rule over the tool input and cannot testagent_type(https://code.claude.com/docs/en/hooks, Common fields).disallowedTools: Bash(...)removes the whole tool, and plugin agents ignorehooksandpermissionMode(https://code.claude.com/docs/en/sub-agents, Choose the subagent scope).onFailure: "block"security guard. A mod would add a path without removing the hook.docs/adr/0019-share-code-across-plugins-by-vendoring-with-a-sync-gate.md:8-28). The direct-bash variant for guardrails fails on the Windows reason above.Done-when
/harness-ops:observability latencyPreToolUse:Bash p50, 7 days before and after: unmet. It needs a week of telemetry after release.docs-raw.shcall and a piped command are still denied: met. The tests "the hook process denies a second docs-raw call from the same agent run" and "must-deny: anything else inside the docs-fetcher is denied" pass, and the timing run's piped-command row returned deny in every run.exec-bash.mjsprovides.Branch
host-run/6634-docs-fetcher-gate-fast-pathat0955877c543a0d9634e1ccd938bb5c61a53e5fc8. No PR yet.Target labels: add none; remove none.
kyle-sexton commentedon Nov 2, 2026
Decision recorded. (agent; not an owner decision) What is left after #12234 (merged,
d671942df6; the 2026-11-02 host run said "No PR yet" before it opened), checked against the body's "Verify after the fix" list:docs-raw.shcall and a piped command are still denied. The gate tests "the hook process denies a second docs-raw call from the same agent run" and "must-deny: anything else inside the docs-fetcher is denied" pass, and the piped row denied in every timing run (the 2026-11-02 host-run record on this issue).Remaining:
/harness-ops:observability latencyPreToolUse:Bash p50, 7 days before and 7 days after. This needs a week of telemetry after the release.34 remaining items
kyle-sexton commentedon Nov 4, 2026
kyle-sexton commentedon Nov 5, 2026
Claim released. The Backlog HITL session (hitl-backlog) has no live work, branch or PR on this issue. It is free for any lane to take.
kyle-sexton commentedon Nov 5, 2026
kyle-sexton commentedon Nov 6, 2026
kyle-sexton commentedon Nov 6, 2026
Measured 2026-11-06 (Lane 5). The gate items are settled; a guardrails regression is what remains.
Docs-fetcher gates on Windows (laptop: Windows 10.0.26300.9711, Claude Code 2.1.316, Node 24.21.0, Git Bash 5.3.15)
node -e 052.0 ms,bash -c :25.3 ms.PreToolUse:Bash latency, before and after #12234 reached this machine (cut 2026-11-02T23:49:30Z, from the plugin cache dates). The source is the shared OTel store, queried with both window ends fixed.
Remaining: a guardrails Bash-row regression. On Windows the whole guardrails
Bash|PowerShellrow now takes about 300 ms, against 194 ms at 7a68f95 (the commit the desktop timed earlier). The 106 ms gap held in every round. The guardrails node-to-bash hop itself is 42.5 ms above node. The cause is unverified; the timing points at a change merged around 11-02, possibly #12896. Lane 5 is bisecting it under this issue.kyle-sexton commentedon Nov 6, 2026
kyle-sexton commentedon Nov 6, 2026
#13663 merged; the jq-removal follow-up is measured and dropped.
bash -ccalls go from 36 to 15 process creations andwsl.exe -e bash -lccalls from 64 to 22, with identical hook output on a 34-command parity set.Remaining on this issue:
exec-bash.mjswould avoid re-spawning per guard. Neither removes the shfmt process itself.kyle-sexton commentedon Nov 6, 2026
Option (a), trimming the shfmt parse plumbing: built, measured, not shipped.
I set the ship rule before timing. Windows Git Bash, paired runs in alternating order, 5 rounds × 15, 4 command shapes, under the heavy-run lock. A variant ships only if plain's median is lower with all 5 per-round differences below 0, and compound, heredoc and words40 are each at most +5 ms on the median.
Medians are after minus base, n = 75 per arm. Base medians were 292–318 ms.
Remaining on this issue (nothing Lane 5 can still do in this round):
exec-bash.mjs, a design change to how the row runs.kyle-sexton commentedon Nov 6, 2026
kyle-sexton commentedon Nov 6, 2026
Hub H62: parse-once is built and measured. Its PR waits for the owner, because it changes what the guards trust. The p50 rise is traced to sessions continued after
/clear, not to guardrails.1. Parse once in
exec-bash.mjs(branchperf/6634-parse-once, e117172, no PR yet)On Windows only,
exec-bash.mjs --bps-seed <hook-utils.sh>buffers stdin and, for an ASCII Bash or PowerShell command under 8,192 characters, runs the same shfmt and jq, using the library's ownHOOK_BPS_JQ. It passes the list inHOOK_BPS_SEED_KEY/VAL.run-guards.shputs the pair in its parse memo only under--bps-seeded, which only the hooks.json Bash|PowerShell row passes. The launcher always drops an inherited pair, and a git-hook launch ofrun-guards.shignores one. On any failure nothing is seeded and bash parses as before, so its refusals are unchanged. A stalled stdin stays open to the script, which still refuses (exit 2, the same message).Every timing run, paired ABBA, 5 × 15 per shape, after − base medians:
bash -candwsl.exeeach above +5 mslib/exec-bash.seed.test.mjs(the jq program extracted byte for byte, every fallback, the inherited pair dropped) and 5run-guards.test.shcases (a forged pair ignored without--bps-seeded, a seeded call runs no jq, a seed keyed to other text unused). No CI lane runs the seeded path, since CI is Linux, so the Windows corpus run above is the evidence for it.2. The PreToolUse:Bash p50 rise from 2026-11-02 ~21:40Z
/clear. Across the last 25 lane sessions, those launched fresh (SessionStart:startup) run PostToolBatch at a p50 of 52–54 ms. Those continued after/clear(SessionStart:clear) run 79–115 ms, with one exception at 40. Fresh headless sessions run at the pre-step level today. The step came with a restart wave at 21:42–21:46Z (three sessions silent from 21:38 came back slow), and the lanes have continued through/clearsince.Remaining on this issue:
Refs: #6634and a## Remainingsection./clearhook-cost finding: the owner decides whether to report it upstream.fix(guardrails): run the seed's shfmt and jq from absolute PATH entri…
test(guardrails): a shfmt or jq planted in the cwd is never run by th…