diff --git a/bin/fm-nm-base-skip-pretool-check.sh b/bin/fm-nm-base-skip-pretool-check.sh new file mode 100755 index 00000000000..af3d95467df --- /dev/null +++ b/bin/fm-nm-base-skip-pretool-check.sh @@ -0,0 +1,116 @@ +#!/usr/bin/env bash +# Fork-only: PreToolUse check that makes a new no-mistakes run follow its PR's +# base branch (applypass fork). +# +# Decision (2026-10-26): dev-bound PRs merge on green GitHub CI, which already +# runs the unit and integration suites, so the local final run skips the test +# step and keeps review, document and lint. PRs to any other base (main) keep +# the full run with tests. A local test step on a loaded machine hung for 30-45 +# minutes while GitHub CI was green. +# +# Rule, applied to a command that starts a NEW run (`no-mistakes axi run` with +# --intent, as in bin/fm-nm-mirror-pretool-check.sh): +# base branch dev: the command must pass `--skip test` (alone or inside a +# comma list), else it is refused. +# any other base: the command must not skip `test`, else it is refused. +# base unknown: allowed unchanged, so the full run with tests stays the default. +# The base is the command's --base-branch when given, else the base of the open +# PR for the current branch read from the forge (gh, 20s bound). A bare `axi +# run`, `axi status`, `axi respond` and every other command are never checked. +# Only Claude workers get this check (bin/fm-op-guard-install.sh); other workers +# keep the full run with tests. +# +# Usage: +# | bin/fm-nm-base-skip-pretool-check.sh [--claude] +# bin/fm-nm-base-skip-pretool-check.sh --command '' [--cwd ] [--claude] +# +# Exit/output contract: +# ALLOW - exit 0 and no output. +# DENY - exit 2 and a Claude-shaped deny object on stderr naming the base and +# the exact flag to add or drop; stdout stays empty. +# FAIL OPEN - malformed or empty stdin, missing jq, or an unreadable base. +set -u + +CMD="" +CMD_SET=0 +CWD="" + +usage() { + sed -n '2,/^set -u/p' "$0" | sed '$d' | sed 's/^# \{0,1\}//' +} + +while [ "$#" -gt 0 ]; do + case "$1" in + --command) + [ "$#" -gt 1 ] || { echo "error: --command requires a value" >&2; exit 2; } + CMD=$2; CMD_SET=1; shift 2 ;; + --command=*) CMD=${1#--command=}; CMD_SET=1; shift ;; + --cwd) + [ "$#" -gt 1 ] || { echo "error: --cwd requires a value" >&2; exit 2; } + CWD=$2; shift 2 ;; + --cwd=*) CWD=${1#--cwd=}; shift ;; + --claude) shift ;; + -h|--help) usage; exit 0 ;; + *) + echo "error: unknown argument: $1" >&2 + usage >&2 + exit 2 ;; + esac +done + +if [ "$CMD_SET" -eq 0 ]; then + PAYLOAD=$(cat 2>/dev/null || true) + [ -n "$PAYLOAD" ] || exit 0 + command -v jq >/dev/null 2>&1 || exit 0 + # shellcheck source=bin/fm-hook-host-lib.sh + . "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/fm-hook-host-lib.sh" + if fm_hook_payload_is_foreign_host "$PAYLOAD"; then + exit 0 + fi + CMD=$(printf '%s' "$PAYLOAD" | jq -r '(.toolInput.command // .tool_input.command // empty)' 2>/dev/null) || exit 0 + [ -n "$CWD" ] || CWD=$(printf '%s' "$PAYLOAD" | jq -r '(.cwd // empty)' 2>/dev/null) || exit 0 +fi +[ -n "$CMD" ] || exit 0 +[ -n "$CWD" ] || CWD=$PWD + +case "$CMD" in + *no-mistakes*axi*run*--intent*) ;; + *) exit 0 ;; +esac + +# Base branch: the command's own --base-branch, else the open PR's base. +BASE=$(printf '%s' "$CMD" | sed -n 's/.*--base-branch[= ]\{1,\}["'\'']\{0,1\}\([^ "'\'']*\).*/\1/p' | head -1) +if [ -z "$BASE" ]; then + command -v gh >/dev/null 2>&1 || exit 0 + TIMEOUT_BIN=$(command -v timeout || command -v gtimeout || true) + if [ -n "$TIMEOUT_BIN" ]; then + BASE=$(cd "$CWD" 2>/dev/null && "$TIMEOUT_BIN" 20 gh pr view --json baseRefName -q .baseRefName 2>/dev/null) || BASE="" + else + BASE=$(cd "$CWD" 2>/dev/null && gh pr view --json baseRefName -q .baseRefName 2>/dev/null) || BASE="" + fi +fi +BASE=$(printf '%s' "$BASE" | tr -d '[:space:]') +[ -n "$BASE" ] || exit 0 + +# Steps named by --skip, as ",a,b,". +SKIPS=$(printf '%s' "$CMD" | sed -n 's/.*--skip[= ]\{1,\}["'\'']\{0,1\}\([^ "'\'']*\).*/\1/p' | head -1) +SKIPS=",$SKIPS," +SKIPS_TEST=0 +case "$SKIPS" in *,test,*) SKIPS_TEST=1 ;; esac + +IS_SKIP_BASE=0 +[ "$BASE" = "dev" ] && IS_SKIP_BASE=1 + +REASON="" +if [ "$IS_SKIP_BASE" -eq 1 ] && [ "$SKIPS_TEST" -eq 0 ]; then + REASON="This PR targets $BASE, which merges on green GitHub CI that already runs the tests, so the final no-mistakes run skips the local test step. Re-run the same command with the test step added to --skip (for example '--skip test', or '--skip push,pr,ci,test' when the brief already skips those). Review, document and lint still run." +elif [ "$IS_SKIP_BASE" -eq 0 ] && [ "$SKIPS_TEST" -eq 1 ]; then + REASON="This PR targets $BASE, so the final no-mistakes run keeps the test step. Re-run the same command without 'test' in --skip." +fi +[ -n "$REASON" ] || exit 0 + +json_escape() { + printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' | tr '\n' ' ' +} +printf '{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny"},"systemMessage":"[nm-base-skip] %s"}\n' "$(json_escape "$REASON")" >&2 +exit 2 diff --git a/bin/fm-op-guard-install.sh b/bin/fm-op-guard-install.sh index d98075d443d..dcfd9e4c9fb 100755 --- a/bin/fm-op-guard-install.sh +++ b/bin/fm-op-guard-install.sh @@ -4,7 +4,8 @@ # usage: fm-op-guard-install.sh # # Merges PreToolUse Bash hooks that run bin/fm-op-pretool-check.sh and -# bin/fm-nm-mirror-pretool-check.sh (diverged no-mistakes mirror) into +# bin/fm-nm-mirror-pretool-check.sh (diverged no-mistakes mirror) and +# bin/fm-nm-base-skip-pretool-check.sh (test step follows the PR base) into # /.claude/settings.local.json, keeping every other setting. Idempotent: a # hook already pointing at this script is left alone. bin/fm-spawn.sh runs it for # every Claude worker and secondmate worktree it wires; run it once by hand in @@ -16,6 +17,7 @@ set -u command -v jq >/dev/null 2>&1 || { echo "fm-op-guard-install: jq is required" >&2; exit 1; } SCRIPT_DIR=$(CDPATH='' cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P) || exit 1 +BASE_HOOK_CMD="$(printf %q "$SCRIPT_DIR/fm-nm-base-skip-pretool-check.sh") --claude" HOOK_CMD="$(printf %q "$SCRIPT_DIR/fm-op-pretool-check.sh") --claude" NM_HOOK_CMD="$(printf %q "$SCRIPT_DIR/fm-nm-mirror-pretool-check.sh") --claude" DIR=$(CDPATH='' cd -- "$1" && pwd -P) || exit 1 @@ -24,7 +26,7 @@ FILE="$DIR/.claude/settings.local.json" mkdir -p "$DIR/.claude" || exit 1 [ -f "$FILE" ] || printf '{}\n' >"$FILE" TMP=$(mktemp "$FILE.XXXXXX") || exit 1 -if ! jq --arg cmd "$HOOK_CMD" --arg nm "$NM_HOOK_CMD" ' +if ! jq --arg cmd "$HOOK_CMD" --arg nm "$NM_HOOK_CMD" --arg base "$BASE_HOOK_CMD" ' .hooks //= {} | .hooks.PreToolUse //= [] | if any(.hooks.PreToolUse[]?.hooks[]?; (.command // "") | contains("fm-op-pretool-check.sh")) @@ -34,6 +36,10 @@ if ! jq --arg cmd "$HOOK_CMD" --arg nm "$NM_HOOK_CMD" ' | if any(.hooks.PreToolUse[]?.hooks[]?; (.command // "") | contains("fm-nm-mirror-pretool-check.sh")) then . else .hooks.PreToolUse += [{"matcher":"Bash","hooks":[{"type":"command","command":$nm}]}] + end + | if any(.hooks.PreToolUse[]?.hooks[]?; (.command // "") | contains("fm-nm-base-skip-pretool-check.sh")) + then . + else .hooks.PreToolUse += [{"matcher":"Bash","hooks":[{"type":"command","command":$base}]}] end' "$FILE" >"$TMP"; then rm -f "$TMP" echo "fm-op-guard-install: could not merge into $FILE" >&2 diff --git a/docs/scripts.md b/docs/scripts.md index 1b806311fe3..cda39d60b4a 100644 --- a/docs/scripts.md +++ b/docs/scripts.md @@ -61,6 +61,7 @@ The shared no-mistakes gate lifecycle boundary is summarized in [architecture.md | `fm-op-pretool-check.sh` | Fork-only: PreToolUse transport that refuses a bare 1Password `op` call unless OP_SERVICE_ACCOUNT_TOKEN is loaded (decision in `fm-op-command-policy.mjs`) | | `fm-op-guard-install.sh` | Fork-only: register the op guard and the no-mistakes mirror check in a directory's untracked `.claude/settings.local.json`; `fm-spawn.sh` runs it for every Claude worker and secondmate | | `fm-nm-mirror-pretool-check.sh` | Fork-only: PreToolUse check that refuses a new `no-mistakes axi run --intent` when the private mirror branch holds commits whose patches local HEAD lacks | +| `fm-nm-base-skip-pretool-check.sh` | Fork-only: PreToolUse check that makes a new `no-mistakes axi run --intent` skip the `test` step when the PR base is `dev` and keep it for any other base; an unknown base allows the full run. It covers Claude workers only; other workers keep the full run with tests | | `fm-op-service-account-token.sh` | Fork-only: print the 1Password service account token for a home (bash entry point over `fm-op-service-account-lib.sh`, named by the op guard's deny reason so the remedy works from any shell) | | `fm-daily-quota-stop.sh` | Fork-only: daily Claude quota stop gate (`check`); refuses new launches and no-mistakes runs at 20% of the weekly quota used today and holds them at 25% (docs/configuration.md) | | `fm-daily-quota-lib.sh` | Fork-only: quota-axi read, day-start baseline, and verdict logic sourced by `fm-daily-quota-stop.sh` | diff --git a/tests/fm-nm-base-skip-pretool-check.test.sh b/tests/fm-nm-base-skip-pretool-check.test.sh new file mode 100755 index 00000000000..3f942c16e10 --- /dev/null +++ b/tests/fm-nm-base-skip-pretool-check.test.sh @@ -0,0 +1,117 @@ +#!/usr/bin/env bash +# shellcheck disable=SC1091,SC2016 +# Fork-only: behavior tests for bin/fm-nm-base-skip-pretool-check.sh. +# +# The forge is a stub `gh` on PATH that prints the PR base from $STUB_BASE (or +# fails when unset), so no network or real no-mistakes is involved. +set -u + +# shellcheck source=tests/lib.sh +. "$(dirname "${BASH_SOURCE[0]}")/lib.sh" + +TMP_ROOT=$(fm_test_tmproot fm-nm-base-skip) +CHECK="$ROOT/bin/fm-nm-base-skip-pretool-check.sh" +BIN="$TMP_ROOT/bin" +mkdir -p "$BIN" +cat > "$BIN/gh" <<'STUB' +#!/usr/bin/env bash +[ -n "${STUB_BASE:-}" ] || exit 1 +printf '%s\n' "$STUB_BASE" +STUB +chmod +x "$BIN/gh" +export PATH="$BIN:$PATH" + +RUN='no-mistakes axi run --intent "ship it"' + +run_check() { # + CHECK_OUT=$(STUB_BASE="$1" "$CHECK" --command "$2" --cwd "$TMP_ROOT" --claude 2>&1) + CHECK_RC=$? +} + +test_dev_base_without_skip_refused() { + run_check dev "$RUN" + expect_code 2 "$CHECK_RC" "dev base without --skip test is refused" + assert_contains "$CHECK_OUT" '[nm-base-skip]' "names the check" + assert_contains "$CHECK_OUT" 'skips the local test step' "says why" + pass "dev-base run without --skip test is refused" +} + +test_dev_base_with_skip_allowed() { + run_check dev "$RUN --skip test" + expect_code 0 "$CHECK_RC" "dev base with --skip test" + [ -z "$CHECK_OUT" ] || fail "allowed case printed output: $CHECK_OUT" + run_check dev "$RUN --skip push,pr,ci,test" + expect_code 0 "$CHECK_RC" "dev base with a skip list holding test" + pass "dev-base run that skips test is allowed" +} + +test_dev_base_with_other_skips_refused() { + run_check dev "$RUN --skip push,pr,ci" + expect_code 2 "$CHECK_RC" "dev base skipping other steps only" + run_check dev "$RUN --skip pr,ci,testing" + expect_code 2 "$CHECK_RC" "a step merely named like test does not count" + pass "a skip list without the test step is refused on dev" +} + +test_main_base_keeps_tests() { + run_check main "$RUN" + expect_code 0 "$CHECK_RC" "main base without skip" + [ -z "$CHECK_OUT" ] || fail "allowed case printed output: $CHECK_OUT" + run_check main "$RUN --skip test" + expect_code 2 "$CHECK_RC" "main base skipping test" + assert_contains "$CHECK_OUT" 'keeps the test step' "says main keeps tests" + pass "main-base run keeps the test step" +} + +test_unknown_base_keeps_full_run() { + run_check "" "$RUN" + expect_code 0 "$CHECK_RC" "unknown base, no skip" + run_check "" "$RUN --skip test" + expect_code 0 "$CHECK_RC" "unknown base is left alone" + pass "an unreadable base leaves the command alone" +} + +test_base_branch_flag_wins() { + run_check main "$RUN --base-branch dev" + expect_code 2 "$CHECK_RC" "--base-branch dev overrides the PR base" + run_check dev "$RUN --base-branch main" + expect_code 0 "$CHECK_RC" "--base-branch main overrides the PR base" + pass "--base-branch decides the base" +} + +test_only_new_runs_checked() { + local c + for c in 'no-mistakes axi run' 'no-mistakes axi status' 'no-mistakes axi respond --run 1' 'git status'; do + run_check dev "$c" + expect_code 0 "$CHECK_RC" "not a new run: $c" + done + pass "only a new run is checked" +} + +test_hook_payload() { + local out + out=$(printf '{"tool_input":{"command":"%s"},"cwd":"%s"}' 'no-mistakes axi run --intent x' "$TMP_ROOT" \ + | STUB_BASE=dev "$CHECK" --claude 2>&1) + expect_code 2 $? "payload command is checked" + assert_contains "$out" '"permissionDecision":"deny"' "deny object" + pass "the PreToolUse payload drives the check" +} + +test_installer_registers_hook() { + local dir="$TMP_ROOT/inst" + mkdir -p "$dir" + HOME="$TMP_ROOT" "$ROOT/bin/fm-op-guard-install.sh" "$dir" || fail "installer failed" + jq -e '[.hooks.PreToolUse[].hooks[].command | select(contains("fm-nm-base-skip-pretool-check.sh"))] | length == 1' \ + "$dir/.claude/settings.local.json" >/dev/null || fail "installer did not register the check once" + pass "the op guard installer registers the base-skip check" +} + +test_dev_base_without_skip_refused +test_dev_base_with_skip_allowed +test_dev_base_with_other_skips_refused +test_main_base_keeps_tests +test_unknown_base_keeps_full_run +test_base_branch_flag_wins +test_only_new_runs_checked +test_hook_payload +test_installer_registers_hook diff --git a/tests/fm-op-pretool-check.test.sh b/tests/fm-op-pretool-check.test.sh index ad71fb66376..e4d3e584604 100755 --- a/tests/fm-op-pretool-check.test.sh +++ b/tests/fm-op-pretool-check.test.sh @@ -270,7 +270,7 @@ test_installer() { || fail "installer did not register the Bash PreToolUse hook" HOME="$SCRATCH_HOME" "$ROOT/bin/fm-op-guard-install.sh" "$dir" || fail "second install failed" n=$(jq '.hooks.PreToolUse | length' "$file") - [ "$n" = 2 ] || fail "installer is not idempotent: $n hook entries" + [ "$n" = 3 ] || fail "installer is not idempotent: $n hook entries" # The registered command, run as the harness would, refuses a bare op call. local cmd out cmd=$(jq -r '.hooks.PreToolUse[0].hooks[0].command' "$file") @@ -280,7 +280,7 @@ test_installer() { mkdir -p "$TMP_ROOT/fresh" assert_outside_real_home "$TMP_ROOT/fresh" HOME="$SCRATCH_HOME" "$ROOT/bin/fm-op-guard-install.sh" "$TMP_ROOT/fresh" || fail "fresh install failed" - jq -e '.hooks.PreToolUse | length == 2' "$TMP_ROOT/fresh/.claude/settings.local.json" >/dev/null || fail "fresh install wrote no hook" + jq -e '.hooks.PreToolUse | length == 3' "$TMP_ROOT/fresh/.claude/settings.local.json" >/dev/null || fail "fresh install wrote no hook" # A checkout under a path with a space registers a hook that still launches. local spaced="$TMP_ROOT/with space/bin" target="$TMP_ROOT/spaced-target" mkdir -p "$spaced" "$target"