Skip to content

perf(hooks): cut per-tool-call process spawns from Claude Code hooks - #65

Merged
tstapler merged 15 commits into
masterfrom
perf/hook-spawn-reduction
Nov 8, 2026
Merged

tstapler merged 15 commits into
masterfrom
perf/hook-spawn-reduction

Conversation

@tstapler

@tstapler tstapler commented Nov 8, 2026

Copy link
Copy Markdown
Owner

Cuts process spawns on the hot Claude Code hook paths. Evidence for the spawn problem: ~30 spawns per Bash tool call under CrowdStrike.

Measured on this machine (30 runs, mean):

Change Before After
ssq-hooks check with hookmetrics wrapper (live setting; see Follow-up) 130 ms, 2 extra spawns 72 ms
pr-review-reminder hook, non-matching Bash call 83 ms wrapped / 22 ms bare, 5 spawns 4.7 ms, 0 spawns (and 0 when the if filter matches nothing)
SDD Write|Edit PreToolUse check (in a large repo) 752 ms removed (Stop only)
SDD Stop check 757 ms 31 ms (globs instead of 3x find)
git status through the shim 3 readlink + subshell forks, 104 ms median 0 helper spawns, 68 ms median

Changes:

  • New .claude/hooks/pr-review-reminder.sh (bash builtins only before a match; same output as the untracked script on gh pr create|ready|merge, byte-identical checked), linked via cfgcaddy, registered in core-hooks with if: Bash(gh pr *) (Claude Code >= 2.1.89; local is 2.1.319).
  • core-hooks: PreToolUse SDD status check removed; Stop check rewritten with globs (verified same output as the find version across none/requirements/plan/validation states).
  • git-ssh-fallback: non-network subcommands exec the real git before _find_real_git, reusing _extract_subcommand's logic; fetch/clone/push/pull/ls-remote/submodule/remote still take the full path (verified with a stub real git, including -C/-c forms).
  • llm-sync: _effective_command now also expands $HOME and drops ~/.cargo/bin/~/.local/bin. Before this, running llm-sync against the live settings.json added $HOME/.local/bin/ssq-hooks check and $HOME/.cargo/bin/kibitzer hook copies next to the installed absolute-path entries (reproduced on a copy), doubling those hooks per tool call. Two new tests in test_claude_plugin_installer.py; all 6 pass. _HOOK_WRAPPER_RE is unchanged and still strips hookmetrics.

Follow-up (not in this diff): the hookmetrics wrapping and the untracked pr-review-reminder entry exist only in the live ~/.claude/settings.json, which llm-sync only appends to. Applying this needs a one-time edit of that file. The latency metric is dropped for ssq-hooks check and pr-review-reminder; hookmetrics remains on the rarely-run hooks.

Not verified: git-ssh-fallback.bats hangs at its second test both before and after this change (pre-existing); the 10 unit-style tests I ran (is_network_op, extract_subcommand, self-test, executable) pass.

🤖 Generated with Claude Code

tstapler and others added 15 commits October 20, 2026 07:26
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Replace the stale hard-coded Linux path with
STAPLER_SQUAD_DASHBOARDS_DIR, defaulting to
$HOME/code/github.com/tstapler/stapler-squad/docs/observability/grafana/dashboards.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
RED panels for GetVCSStatus: handler and git-computation latency
percentiles, Connect error/cancel rate, error ratio, in-flight, cache and
coalescing hit rate, slot queue wait. Queries verified against
VictoriaMetrics through Grafana's /api/ds/query.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- Guard Linux-only /home/tstapler and /home/linuxbrew entries; on macOS
  /home is autofs, so every failed PATH lookup (~24ms) spawned od_user_homes
  (130-300/s), loading opendirectoryd and endpoint security.
- Load zplug plugins statically (zplug on demand), cache completion output,
  single compinit -C, background keychain: interactive startup ~1.6s -> ~0.7s.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
- Answer the oh-my-zsh git plugin's `git version` from a cache keyed on the
  git binary instead of forking it every shell.
- Drop `uname` forks on macOS (use $OSTYPE; .zshenv runs for every zsh).
- Use ${UID} instead of `id -u`, and only look up the fallback SSH agent
  socket when the 1Password and GPG sockets are absent.
- Move zsh-only _cache_output into .shell/cache-output.zsh (allowlisted in
  .gitignore): its `*(N)` glob made `bash -n .shell/exports.sh` fail, and
  .bashrc sources that file. bash gets a stub that returns 1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ites

Resolve origin once (GIT_HOOK_ORIGIN_URL) instead of re-running ls-remote in
each hook and drop-in, and only write user.name/core.sshCommand when changed,
since writes take the .git/config lock shared by every worktree.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
(From the stash 'wip before pulling origin/master for pr-ship push'.)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…PATH guard

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…lerts

Native host collector (Prometheus text on 127.0.0.1:64891) scraped by Alloy
into VictoriaMetrics as job=host; Grafana dashboard with an Activity Monitor
style overview, contention suspects and a host-vs-app row; Grafana alert
rules for disk, swap, memory pressure and load.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
~/.cargo/config.toml sets rustc-wrapper=sccache, but a server spawned by
cargo dies with its parent shell, so later builds silently lose caching.
Run it under launchd (macOS) / systemd --user (Linux) and install sccache
from the macOS Brewfile too.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Under EDR every spawned process is expensive (~30 spawns per Bash tool call).

- pr-review-reminder: add tracked script that early-exits via a bash builtin
  `case` on the raw hook JSON (no jq/grep/python/cat on the non-matching
  path), registered with an `if: Bash(gh pr *)` filter so the process is
  not spawned at all for other commands.
- core-hooks: drop the PreToolUse sdd-status-check (kept on Stop only) and
  replace the Stop check's three `find` walks with globs.
- git shim: bypass _find_real_git (3x readlink + subshells) for local
  subcommands; network subcommands take the unchanged path.
- llm-sync: dedup plugin hooks after expanding $HOME and dropping
  ~/.cargo/bin and ~/.local/bin prefixes. Before, re-running plugin install
  re-added `$HOME/...` copies of hooks already installed as absolute paths,
  doubling ssq-hooks and kibitzer per tool call.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@tstapler
tstapler merged commit 26577b3 into master Nov 8, 2026
6 checks passed
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