diff --git a/plugins/guardrails/.claude-plugin/plugin.json b/plugins/guardrails/.claude-plugin/plugin.json index 261e85c9bd0..b3ebb78bebd 100644 --- a/plugins/guardrails/.claude-plugin/plugin.json +++ b/plugins/guardrails/.claude-plugin/plugin.json @@ -147,5 +147,5 @@ "min": 1 } }, - "version": "0.32.18" + "version": "0.32.19" } diff --git a/plugins/guardrails/CHANGELOG.md b/plugins/guardrails/CHANGELOG.md index 61d960f3a29..325de658c36 100644 --- a/plugins/guardrails/CHANGELOG.md +++ b/plugins/guardrails/CHANGELOG.md @@ -3,6 +3,34 @@ All notable changes to the `guardrails` plugin are documented here. Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/); this plugin uses semantic versioning. +## [0.32.19] + +### Changed + +- **`hooks/block-convention-violation.sh` hoists three `2>/dev/null` redirects + off the command substitutions they sat inside.** GNU Bash execs the body of a + command substitution in the substitution's own subshell, instead of forking a + second time, only when that body carries no redirection of its own (Command + Substitution, Bash Reference Manual; + https://mywiki.wooledge.org/CommandSubstitution). So `v=$(cmd 2>/dev/null)` + costs two process creations to run one program where + `{ v=$(cmd); } 2>/dev/null` costs one, and on the Windows Git Bash hosts this + marketplace targets a fork is a full process creation. The three sites are the + convention resolver fork, the `rev-parse --absolute-git-dir` sequencer probe, + and the `config --get alias.` probe that runs for every non-builtin git + subcommand, which is the one of the three on the per-tool-call path. Counted + from the kernel with `strace -f -e trace=clone,clone3,fork,vfork,execve` + (a PATH shim cannot see a fork that never execs, and `bash -x` prints one line + either way), process creations per invocation: cold-cache stdin-form commit + **34 → 31**, warm-cache commit **19 → 18**, non-builtin git subcommand + **14 → 13**, `echo hello` and `git status --short` unchanged at 11. `execve` + is unchanged at every one of those, which is the evidence this drops latency + rather than work. Verdicts are unchanged: each redirect now sits on a group + holding exactly one command, so the group's status is still that command's and + nothing extra is silenced. The contract test gains a per-site kernel-trace + budget assertion that fails when a redirect moves back inside its + substitution. + ## [0.32.18] ### Changed diff --git a/plugins/guardrails/hooks/block-convention-violation.sh b/plugins/guardrails/hooks/block-convention-violation.sh index 53a2070d11c..acfd1bc440a 100755 --- a/plugins/guardrails/hooks/block-convention-violation.sh +++ b/plugins/guardrails/hooks/block-convention-violation.sh @@ -279,7 +279,14 @@ ensure_convention_patterns() { fi done for conv_key in subject_pattern pr_title_pattern; do - conv_val=$(bash "$RESOLVER" "$REPO_ROOT" "$conv_key" 2>/dev/null) || conv_val="" + # The redirect sits on a single-command group, NOT inside the + # substitution. Bash execs the body in the substitution's own subshell + # only when that body carries no redirection of its own, so + # `$(cmd 2>/dev/null)` pays a second fork that `{ v=$(cmd); } 2>/dev/null` + # does not (measured with `strace -f -e trace=clone,clone3,execve`). The + # group holds exactly one command, so its status is still the resolver's + # and the `|| conv_val=""` fallback fires on exactly the same failures. + { conv_val=$(bash "$RESOLVER" "$REPO_ROOT" "$conv_key"); } 2>/dev/null || conv_val="" case "$conv_key" in subject_pattern) SUBJECT_ERE="$conv_val" ;; *) TITLE_ERE="$conv_val" ;; @@ -449,7 +456,11 @@ effective_dir() { # shellcheck disable=SC2329 # reached via the hook::bash_parse_segments callback chain sequencer_in_progress() { local dir repo="$1" f - dir=$(git -C "$repo" rev-parse --absolute-git-dir 2>/dev/null) || return 1 + # Redirect on a single-command group, not inside the substitution — see the + # note in ensure_convention_patterns. One command in the group, so the + # `|| return 1` still reads git's own status and a sequencer probe that fails + # is still "no sequencer", exactly as before. + { dir=$(git -C "$repo" rev-parse --absolute-git-dir); } 2>/dev/null || return 1 [[ -n "$dir" ]] || return 1 for f in MERGE_HEAD CHERRY_PICK_HEAD REVERT_HEAD rebase-merge rebase-apply; do [[ -e "$dir/$f" ]] && return 0 @@ -582,7 +593,12 @@ check_segment() { local pexp [[ -n "$seg_dir" ]] || seg_dir="$(effective_dir ${wrapper_cd[@]+"${wrapper_cd[@]}"} "${w[@]:gi:sub_idx-gi}")" - pexp=$(git -C "$seg_dir" config --get "alias.$sub" 2>/dev/null) + # Redirect on a single-command group, not inside the substitution — see + # the note in ensure_convention_patterns. This is the one of the three + # that sits on the per-tool-call path: every non-builtin git subcommand + # is probed for an alias. `pexp` empty (git found nothing, or git is + # absent) still means "no alias", unchanged. + { pexp=$(git -C "$seg_dir" config --get "alias.$sub"); } 2>/dev/null if [[ -n "$pexp" ]]; then if [[ "$pexp" == '!'* ]]; then local preparse pa diff --git a/plugins/guardrails/hooks/block-convention-violation.test.sh b/plugins/guardrails/hooks/block-convention-violation.test.sh index 3669e6238d6..2c3b9e29e52 100755 --- a/plugins/guardrails/hooks/block-convention-violation.test.sh +++ b/plugins/guardrails/hooks/block-convention-violation.test.sh @@ -495,4 +495,110 @@ CLAUDE_PROJECT_DIR="$r" bash "$HOOK" <<<"$(jq -n --arg d "$r" \ '{tool_name:"Bash",tool_input:{command:("git status" + ([0]|implode))},cwd:$d}')" >/dev/null 2>&1 || nul_rc=$? assert_exit "NUL in command (blocked)" 2 "$nul_rc" +# --- process-creation budget on this file's three external call sites --------- +# WHY A KERNEL TRACE AND NOT A PATH SHIM OR xtrace. The cost this asserts on is +# a fork that never execs, so neither of the counters this repo already uses can +# see it: a PATH shim (plugins/performance/scripts/spawn-census.sh) only fires +# when something is EXECUTED, and `bash -x` prints one line per command in +# command position whether that command cost one process or two. Only +# clone/execve from the kernel distinguishes them. +# +# THE MECHANIC. GNU Bash runs a command substitution in a forked subshell, and +# execs the body IN that subshell instead of forking a second time only when the +# body is a single command carrying NO REDIRECTION OF ITS OWN. So +# `v=$(cmd 2>/dev/null)` costs two process creations for one exec, while +# `{ v=$(cmd); } 2>/dev/null` costs one. On the Windows Git Bash hosts this +# marketplace targets a fork is a full process creation, which is the whole +# reason the difference is worth a test. +# +# THE ASSERTION is per call site, not a total, so it does not go stale when the +# synced `hook-utils.sh` or the vendored `resolve-convention-pattern.sh` change +# their own spawn shape (neither is this file's to fix, and both still show the +# wasted-fork signature in the same trace). For each external command THIS FILE +# starts, the process that execs it must have a parent that itself execve'd — +# that is, the exec landed in the substitution's own subshell. A parent that +# never execs and has exactly one child is the extra fork, and is a failure. +# +# NON-VACUITY is asserted, not assumed: each site must actually appear in the +# trace, so a scenario that stops reaching a site fails here rather than passing +# by absence. +STRACE_OK=0 +if command -v strace >/dev/null 2>&1 && strace -f -qq -o /dev/null -e trace=execve true >/dev/null 2>&1; then + STRACE_OK=1 +fi +if ((STRACE_OK == 0)); then + echo "skip: strace is unavailable or not permitted here, so the process-creation budget is UNVERIFIED in this run" +else + # Parent map + exec set from one trace. `-s 512` matters: strace truncates + # strings at 32 bytes by default, which would cut every argv this matches on. + # `` / `<... clone resumed>` splits are handled by keying on + # any clone/fork line that ends in the child pid. + trace_sites() { # -> "