Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/guardrails/.claude-plugin/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,5 @@
"min": 1
}
},
"version": "0.32.18"
"version": "0.32.19"
}
28 changes: 28 additions & 0 deletions plugins/guardrails/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<sub>` 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
Expand Down
22 changes: 19 additions & 3 deletions plugins/guardrails/hooks/block-convention-violation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" ;;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
106 changes: 106 additions & 0 deletions plugins/guardrails/hooks/block-convention-violation.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
# `<unfinished ...>` / `<... clone resumed>` splits are handled by keying on
# any clone/fork line that ends in the child pid.
trace_sites() { # <trace log> -> "<label> <yes|NO>" per external command
awk '
{ pid = $1 }
(/clone\(|clone3\(|vfork\(|[^e]fork\(/ || /clone resumed|fork resumed/) && / = [0-9]+$/ {
n = split($0, a, " = "); child = a[n]
if (child ~ /^[0-9]+$/) { parent[child] = pid; kids[pid]++ }
}
/execve\(/ {
execed[pid] = 1
line[pid] = $0
}
END {
for (p in line) {
l = line[p]
lab = ""
if (l ~ /resolve-convention-pattern\.sh/) lab = "resolver"
else if (l ~ /"rev-parse", "--absolute-git-dir"/) lab = "sequencer-probe"
else if (l ~ /"config", "--get", "alias\./) lab = "alias-probe"
if (lab == "") continue
q = (p in parent) ? parent[p] : "-"
printf "%s %s %s\n", lab, (q in execed ? "yes" : "NO"), (q == "-" ? 0 : kids[q]+0)
}
}
' "$1" | sort
}
# trace_run <label> <repo> <plugin-data> <command> -> TRACE_LOG
trace_run() {
local repo="$2" pdata="$3" cmd="$4" json
TRACE_LOG="$TEST_TMPDIR/strace-$1.log"
json=$(jq -n --arg c "$cmd" --arg d "$repo" \
'{tool_name:"Bash",tool_input:{command:$c},cwd:$d}')
CLAUDE_PROJECT_DIR="$repo" CLAUDE_PLUGIN_DATA="$pdata" HOOK_TELEMETRY_SINK="" \
strace -f -qq -s 512 -o "$TRACE_LOG" -e trace=clone,clone3,fork,vfork,execve \
bash "$HOOK" <<<"$json" >/dev/null 2>/dev/null
return 0
}
# assert_site <site label> <trace output> — every occurrence must be direct.
assert_site() {
local site="$1" rows="$2" seen extra
seen=$(printf '%s\n' "$rows" | grep -c "^$site " || true)
if ((seen == 0)); then
bad "budget: $site never ran in the traced scenario, so its budget is untested"
return
fi
extra=$(printf '%s\n' "$rows" | grep "^$site " | grep -cv ' yes ' || true)
if ((extra == 0)); then
ok "budget: $site execs in the substitution's own subshell ($seen call(s), no extra fork)"
else
bad "budget: $site pays an extra fork in $extra of $seen call(s) — the redirect is back inside the substitution"
fi
}

# Cold cache + a stdin-form commit: both resolver forks and the sequencer probe.
rt="$(newrepo "$TICKET")"
rt_data="$TEST_TMPDIR/pdata-budget"
rm -rf "$rt_data"
trace_run commit "$rt" "$rt_data" "$GOOD_COMMIT"
BUDGET_ROWS="$(trace_sites "$TRACE_LOG")"
assert_site resolver "$BUDGET_ROWS"
assert_site sequencer-probe "$BUDGET_ROWS"

# A non-builtin subcommand is the alias probe's only trigger, and it is the
# one of the three that sits on the per-tool-call path.
trace_run alias "$rt" "$rt_data" "git wibble --dry-run"
BUDGET_ROWS="$(trace_sites "$TRACE_LOG")"
assert_site alias-probe "$BUDGET_ROWS"
fi

report