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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
bump: patch
---

### Fixed

- The `reference-verify` pre-push entry no longer judges lines that a merged branch brings in when one of the remote's tracking refs already holds them, for any branch. Before, it skipped such lines only for a merge of the default branch, so a push that merged a long-lived integration branch ran the verifiers on every file that branch changed. That could take minutes, long enough for the remote to drop the idle ssh connection. The entry now uses the same rule as `pre-push-content`, and the README row also names the `Connection to <host> closed by remote host.` failure.
2 changes: 1 addition & 1 deletion plugins/guardrails/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ bash beside git's sh.
| `commit-msg-convention` | commit-msg | The subject against the team-tracked `subject_pattern` in `docs/conventions/source-control.yaml`, else the folder-form `docs/conventions/source-control/commit-convention.yml`, the pattern `block-convention-violation` reads; where the team tracks none, the operator's own in a git-ignored `docs/conventions/source-control.local.yaml`. No tracked or personal pattern, no enforcement. |
| `pre-commit-content` | pre-commit | Secrets and machine-specific paths on the lines the commit adds (for a merge, the lines its conflict resolution adds against every parent), with the same pattern libraries and path exemptions as `secret-pattern-detection` and `hardcoded-path-check`, so a file a shell command or script wrote meets the same checks as a Write. It also skips path findings, never secrets, in each path listed in the repository's `docs/conventions/guardrails-content-exempt.tsv` as read from the default branch (`origin/HEAD`), never the index or work tree, so a change cannot exempt its own lines: one `path<TAB>reason` row per file, or per directory with a trailing `/`, `#` starting a comment. No `origin/HEAD` or no file exempts nothing. |
| `pre-push-content` | pre-push | The same scan as `pre-commit-content` (it shares that entry's `git-hooks/content-scan.sh`), on every pushed ref, for commits that check never judged: made before the hooks were installed, by a tool or clone without them, or rebased or cherry-picked in. Each pushed commit is judged on the lines it adds to its parent, so a secret one commit adds and a later commit removes still fails the push, which would publish it in history; a merge is judged on what it adds against every parent. A new branch starts from its merge-base with the default branch, and commits a remote-tracking ref already holds are not judged again. Each finding names its commit, and the fix is to rewrite that commit. |
| `reference-verify` | pre-push | `cli-flag-verify`, `skill-reference-verify` and `stale-path-verify` on the lines the pushed commits add, through `hooks/run-guards.sh`. A new branch is judged from its merge-base with the default branch. Any pushed range then starts instead at the nearest of the remote's tracking refs between its base and its tip, so a stacked branch's push does not re-verify the pushed branches below it. Those tracking refs are trusted as verified without a check, including another author's branch, fetched into this clone, that was pushed with `--no-verify`. It skips files in a `samples/` folder below an `evals/` folder, which hold recorded model output; the rest of an eval suite stays checked. A cli-flag or skill-reference finding fails the push; a stale-path finding is printed and the push goes ahead. A file whose verifier input it cannot build or whose verifier run fails, or a pushed tip it cannot check out, is reported as a finding and fails the push, since it went unchecked. It runs at push rather than commit because it costs about 10 s on a 44-file change, over the commit budget (the check-placement convention's measured rows). Needs `jq`; the scanned binaries follow `CLAUDE_PLUGIN_OPTION_CLI_FLAG_VERIFY_BINS` and `CLAUDE_PLUGIN_OPTION_CLI_FLAG_VERIFY_SKIP_BINS` when set in the pushing environment. A cli-flag finding is unverified, not proven wrong, so its failure names the way out: decline the entry. The entry reads that decline itself, so it also stops on the lefthook remote, where no registration is removed. Each finding names the line holding the flag, path or reference. `git -c checks.skipmissing=jq` skips it only while `jq` is missing. Over ssh, the connection to the remote stays idle while the hook runs. A remote that drops an idle connection leaves a push that reports success but creates no ref. To keep the connection open, set `ServerAliveInterval` (for example `15`) for that host in the ssh config, or set it for one push: `git -c core.sshCommand="ssh -o ServerAliveInterval=15" push`. |
| `reference-verify` | pre-push | `cli-flag-verify`, `skill-reference-verify` and `stale-path-verify` on the lines the pushed commits add, through `hooks/run-guards.sh`. A new branch is judged from its merge-base with the default branch. Any pushed range then starts instead at the nearest of the remote's tracking refs between its base and its tip, so a stacked branch's push does not re-verify the pushed branches below it. Lines that a merged branch brings in are not judged when one of the remote's tracking refs holds them, whether that branch is the default branch or another one; `pre-push-content` uses the same rule. Lines that the merge resolution or the branch's own commits add are still judged, unless a merged tracking ref already holds the same line. Those tracking refs are trusted as verified without a check, including another author's branch, fetched into this clone, that was pushed with `--no-verify`. A tracking ref is only as current as the last fetch, and the hook never fetches. A ref behind the remote leaves more lines to judge. A ref for a branch the remote deleted or force-pushed, not yet pruned, still counts its commits as pushed, so their lines go unjudged; `git fetch --prune` keeps the refs current. It skips files in a `samples/` folder below an `evals/` folder, which hold recorded model output; the rest of an eval suite stays checked. A cli-flag or skill-reference finding fails the push; a stale-path finding is printed and the push goes ahead. A file whose verifier input it cannot build or whose verifier run fails, or a pushed tip it cannot check out, is reported as a finding and fails the push, since it went unchecked. It runs at push rather than commit because it costs about 10 s on a 44-file change, over the commit budget (the check-placement convention's measured rows). Needs `jq`; the scanned binaries follow `CLAUDE_PLUGIN_OPTION_CLI_FLAG_VERIFY_BINS` and `CLAUDE_PLUGIN_OPTION_CLI_FLAG_VERIFY_SKIP_BINS` when set in the pushing environment. A cli-flag finding is unverified, not proven wrong, so its failure names the way out: decline the entry. The entry reads that decline itself, so it also stops on the lefthook remote, where no registration is removed. Each finding names the line holding the flag, path or reference. `git -c checks.skipmissing=jq` skips it only while `jq` is missing. Over ssh, the connection to the remote stays idle while the hook runs. A remote that drops an idle connection leaves either a push that reports success but creates no ref, or a failed push that prints `Connection to <host> closed by remote host.` To keep the connection open, set `ServerAliveInterval` (for example `15`) for that host in the ssh config, or set it for one push: `git -c core.sshCommand="ssh -o ServerAliveInterval=15" push`. |

### Enforceability tiers

Expand Down
106 changes: 67 additions & 39 deletions plugins/guardrails/git-hooks/reference-verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
# current branch against its upstream. Whichever way it came, a range's base
# then moves up to the nearest of the remote's tracking refs between it and
# the tip (nearest_pushed_base), so a new branch stacked on a pushed branch is
# judged only on what it adds to that branch. A range whose tip merged the default
# branch is narrowed to the lines the branch itself adds (narrow_to_branch).
# judged only on what it adds to that branch. A range whose tip merged a branch
# one of the remote's tracking refs holds (the default branch or any other) is
# narrowed to the lines the branch itself adds (narrow_to_branch).
# Each range is checked on its own, in
# that range's tip: the checkout when HEAD is the tip and the tree is clean,
# else a temporary detached worktree of the tip, removed afterwards.
Expand Down Expand Up @@ -85,21 +86,26 @@ fi
[[ -f "$run_guards" ]] || exit 0

# The tip and base of the range being checked; both empty for --worktree.
# MAIN_BASE is the tip's merge-base with the remote's default branch when the
# range carries commits merged in from it (see narrow_to_branch), else empty.
# MERGE_BASES holds the tip's merge-bases with the remote's tracking refs when
# the range carries commits merged in from them (see pushed_merge_bases), else
# nothing.
TIP=""
BASE=""
MAIN_BASE=""
MERGE_BASES=()

# is_new <path>: the range adds the file rather than changing it.
# is_new <path>: the range adds the file rather than changing it: neither BASE
# nor any of MERGE_BASES holds it.
is_new() {
if [[ "$CHECKS_MODE" == worktree ]]; then
! git ls-files --error-unmatch -- ":(literal)$1" >/dev/null 2>&1 ||
! git cat-file -e "HEAD:$1" 2>/dev/null
return
fi
! git cat-file -e "$BASE:$1" 2>/dev/null &&
{ [[ -z "$MAIN_BASE" ]] || ! git cat-file -e "$MAIN_BASE:$1" 2>/dev/null; }
local m
for m in "$BASE" ${MERGE_BASES[@]+"${MERGE_BASES[@]}"}; do
git cat-file -e "$m:$1" 2>/dev/null && return 1
done
return 0
}

# changed_lines: the lib's ranges, except that a file the range adds as a copy
Expand Down Expand Up @@ -161,27 +167,31 @@ changed_lines() {
mv "$CHECKS_TMP/ranges.new" "$CHECKS_TMP/ranges"
}

# narrow_to_branch: a range whose tip merged the default branch since the last
# push holds the lines that merge brought in, which the branch did not write.
# Keep only the lines added both since the last push and against the tip's
# merge-base with the default branch, so a check never judges more than the
# plain range.
# narrow_to_branch: a range whose tip merged a pushed branch since the last push
# holds the lines that merge brought in, which the branch did not write. Keep
# only the lines added both since the last push and against each of
# MERGE_BASES, so a check never judges more than the plain range. A line the
# branch adds that a merged base already holds at the same place is dropped too.
narrow_to_branch() {
[[ -n "$MAIN_BASE" ]] || return 0
mv "$CHECKS_TMP/ranges" "$CHECKS_TMP/ranges.push"
printf '%s %s\n' "$MAIN_BASE" "$TIP" >"$CHECKS_TMP/push-ranges"
changed_lines
mv "$CHECKS_TMP/ranges" "$CHECKS_TMP/ranges.main"
local m s
((${#MERGE_BASES[@]})) || return 0
cp "$CHECKS_TMP/ranges" "$CHECKS_TMP/ranges.push"
for m in "${MERGE_BASES[@]}"; do
mv "$CHECKS_TMP/ranges" "$CHECKS_TMP/ranges.kept"
printf '%s %s\n' "$m" "$TIP" >"$CHECKS_TMP/push-ranges"
changed_lines
mv "$CHECKS_TMP/ranges" "$CHECKS_TMP/ranges.merged"
checks_intersect_ranges "$CHECKS_TMP/ranges.merged" "$CHECKS_TMP/ranges.kept" >"$CHECKS_TMP/ranges"
s=$?
# A failed awk fails the hook and keeps the plain range, never fewer lines.
if ((s != 0)); then
printf '%s %s\n' "$BASE" "$TIP" >"$CHECKS_TMP/push-ranges"
checks_tool_failed awk "$s" 'narrowing the changed lines to the branch'
mv "$CHECKS_TMP/ranges.push" "$CHECKS_TMP/ranges"
return 1
fi
done
printf '%s %s\n' "$BASE" "$TIP" >"$CHECKS_TMP/push-ranges"
checks_intersect_ranges "$CHECKS_TMP/ranges.main" "$CHECKS_TMP/ranges.push" >"$CHECKS_TMP/ranges.new"
local s=$?
# A failed awk fails the hook and keeps the plain range, never fewer lines.
if ((s != 0)); then
checks_tool_failed awk "$s" 'narrowing the changed lines to the branch'
mv "$CHECKS_TMP/ranges.push" "$CHECKS_TMP/ranges"
return 1
fi
mv "$CHECKS_TMP/ranges.new" "$CHECKS_TMP/ranges"
}

# nearest_pushed_base: moves BASE to the commit of a tracking ref of $remote
Expand All @@ -200,17 +210,35 @@ nearest_pushed_base() {
[[ -z "$best" ]] || BASE="$best"
}

# main_base_for_range: sets MAIN_BASE for the current BASE..TIP, empty unless
# the default branch's merge-base is not already behind BASE.
main_base_for_range() {
local head m
MAIN_BASE=""
head="$(checks_remote_head "$remote")"
[[ -n "$head" ]] || return 0
m="$(git merge-base "$TIP" "$head" 2>/dev/null)" || return 0
[[ -n "$m" && "$m" != "$BASE" ]] || return 0
git merge-base --is-ancestor "$m" "$BASE" 2>/dev/null && return 0
MAIN_BASE="$m"
# pushed_merge_bases: sets MERGE_BASES for the current BASE..TIP to the commits
# already on the remote that the tip merged in: pre-push-content's rule, that a
# commit one of the remote's tracking refs holds is on the remote already. One
# walk lists the commits the remote lacks and, as its boundary, the held
# commits they sit on, the default branch's included; BASE and any held commit
# BASE or another one holds are dropped. With no commit the remote lacks, the
# remote holds the tip itself, so the tip is the one base. A git failure
# leaves no base, which narrows nothing and judges the plain range.
pushed_merge_bases() {
local walk line own=0
local -a held=()
MERGE_BASES=()
walk="$(git rev-list --boundary "$TIP" --not "$BASE" --remotes="$remote" 2>/dev/null)" || return 0
while IFS= read -r line; do
if [[ "$line" == -* ]]; then
[[ "${line#-}" == "$BASE" ]] || held+=("${line#-}")
elif [[ -n "$line" ]]; then
own=1
fi
done <<<"$walk"
if ((!own)); then
[[ "$TIP" == "$BASE" ]] || MERGE_BASES=("$TIP")
return 0
fi
((${#held[@]})) || return 0
walk="$(git merge-base --independent "$BASE" "${held[@]}" 2>/dev/null)" || return 0
while IFS= read -r line; do
[[ -z "$line" || "$line" == "$BASE" ]] || MERGE_BASES+=("$line")
done <<<"$walk"
}

# added_lines <path>: the lines the range adds, in order.
Expand Down Expand Up @@ -402,7 +430,7 @@ else
while read -r BASE TIP <&3; do
nearest_pushed_base
printf '%s %s\n' "$BASE" "$TIP" >"$CHECKS_TMP/push-ranges"
main_base_for_range
pushed_merge_bases
if tip_is_checkout; then
check_range
continue
Expand Down
65 changes: 62 additions & 3 deletions plugins/guardrails/git-hooks/reference-verify.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
# unchanged line, in a file type the verifiers do not read, or in an eval
# suite's samples/ folder (recorded model output), does not; a new
# branch ranges from its merge-base with the remote's default branch, or from a
# pushed branch it is stacked on, a pushed branch that merged main is judged
# only on its own lines, and a deletion checks nothing; the failure names the
# way out (decline the entry; the skip key covers only a missing jq);
# pushed branch it is stacked on, a pushed branch that merged main or another
# pushed branch is judged only on its own lines, and a deletion checks nothing;
# the failure names the way out (decline the entry; the skip key covers only a
# missing jq);
# --worktree covers uncommitted files;
# and the plugin's vendored installer wires the entry as a pre-push hook, so
# `git commit` does not run it and `git push` does.
Expand Down Expand Up @@ -558,6 +559,64 @@ expect "an awk that fails while narrowing to the branch's lines fails the push"
"$(push_failing 'NR == FNR { n++; f[n]' 1 refs/heads/topic "$(git -C "$REPO" rev-parse HEAD)" "$pushed")"
git -C "$REPO" checkout -q main

# A pushed branch that merged another pushed branch, not the default one: the
# lines that branch brought in are on the remote already under its tracking ref,
# so they are not judged, and a file it added is not judged whole. The lines the
# merge resolution or the branch's own commits add still are. A plugin copy
# whose dispatcher logs each file it is handed counts the verifier runs.
cp -R "$PLUGIN" "$WORK/counting"
mv "$WORK/counting/hooks/run-guards.sh" "$WORK/counting/hooks/run-guards-real.sh"
cat >"$WORK/counting/hooks/run-guards.sh" <<'EOF'
#!/usr/bin/env bash
payload="$(cat)"
jq -r '.tool_input.file_path' <<<"$payload" | sed 's#.*/##' >>"$JUDGED_LOG"
exec bash "${BASH_SOURCE[0]%/*}/run-guards-real.sh" "$@" <<<"$payload"
EOF
judged_files() { # <ref> <local-sha> <remote-sha> -> the files the verifiers ran on, sorted
: >"$WORK/judged"
(cd "$REPO" && printf '%s %s %s %s\n' "$1" "$2" "$1" "$3" |
JUDGED_LOG="$WORK/judged" bash "$WORK/counting/git-hooks/reference-verify.sh" origin "$WORK/r$n.git" >/dev/null 2>&1)
sort "$WORK/judged" | tr '\n' ' ' | sed 's/ $//'
}
new_repo
git -C "$REPO" checkout -q -b integ
printf '#!/usr/bin/env bash\nfaketool --real\nfaketool --bogus\n' >"$REPO/run.sh"
printf '#!/usr/bin/env bash\nfaketool --bogus\n' >"$REPO/integ-new.sh"
commit_all integ-bogus
git -C "$REPO" push -q --no-verify origin integ 2>/dev/null
git -C "$REPO" checkout -q -b delivery main
printf '#!/usr/bin/env bash\nfaketool --real\n' >"$REPO/delivery.sh"
commit_all delivery
git -C "$REPO" push -q --no-verify origin delivery 2>/dev/null
pushed="$(git -C "$REPO" rev-parse HEAD)"
git -C "$REPO" merge -q --no-ff --no-edit --no-verify integ >/dev/null
merged="$(git -C "$REPO" rev-parse HEAD)"
expect "a branch that merged a pushed non-default branch is not judged on that branch's lines" "0|" \
"$(push_check refs/heads/delivery "$merged" "$pushed")"
expect "the merge push runs the verifiers on no file the merged branch changed" "" \
"$(judged_files refs/heads/delivery "$merged" "$pushed")"
git -C "$REPO" reset -q --hard "$pushed"
git -C "$REPO" merge -q --no-ff --no-commit integ >/dev/null 2>&1
printf 'faketool --merged\n' >>"$REPO/integ-new.sh"
commit_all merge-resolution
expect "a line the merge resolution adds to a merged branch's file is judged, at that line" \
"1|integ-new.sh:3: ${FLAG_FINDING/in run.sh/in integ-new.sh}" \
"$(push_check refs/heads/delivery "$(git -C "$REPO" rev-parse HEAD)" "$pushed")"
git -C "$REPO" reset -q --hard "$merged"
printf '#!/usr/bin/env bash\nfaketool --bogus\n' >"$REPO/own-new.sh"
printf 'echo more\n' >>"$REPO/delivery.sh"
commit_all delivery-own
tip="$(git -C "$REPO" rev-parse HEAD)"
expect "after merging a pushed branch, a file the branch adds is judged" "1|own-new.sh:2: ${FLAG_FINDING/in run.sh/in own-new.sh}" \
"$(push_check refs/heads/delivery "$tip" "$pushed")"
expect "the push runs the verifiers only on the files the branch's own commits change" "delivery.sh own-new.sh" \
"$(judged_files refs/heads/delivery "$tip" "$pushed")"
# A branch reset to the other pushed branch's tip adds no commit the remote lacks.
git -C "$REPO" reset -q --hard integ
expect "a branch reset to a pushed branch's tip is not judged on its lines" "0|" \
"$(push_check refs/heads/delivery "$(git -C "$REPO" rev-parse HEAD)" "$pushed")"
git -C "$REPO" checkout -q main

new_repo
printf '#!/usr/bin/env bash\nfaketool --real\nfaketool --bogus\n' >"$REPO/run.sh"
expect "--worktree: an uncommitted unknown flag blocks" 1 "$(c="$(worktree_check)" && printf '%s' "${c%%|*}")"
Expand Down
Loading