|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Self-test for guard-dispatch-tier.sh — run it after touching that hook: |
| 3 | +# |
| 4 | +# .claude/hooks/guard-dispatch-tier.selftest.sh |
| 5 | +# |
| 6 | +# Feeds the hook the JSON payload shape Claude Code delivers on PreToolUse for an Agent call |
| 7 | +# and asserts the block/allow verdict per case. Needs jq (to build payloads and to read |
| 8 | +# settings.json) and nothing else: no install, no build, no network. Exit 0 = all cases hold. |
| 9 | +# |
| 10 | +# The first case is the measured shape: an os-dev dispatch with neither `model` nor `effort`. |
| 11 | + |
| 12 | +set -uo pipefail |
| 13 | + |
| 14 | +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 15 | +hook="$here/guard-dispatch-tier.sh" |
| 16 | +settings="$here/../settings.json" |
| 17 | +pass=0 |
| 18 | +fail=0 |
| 19 | + |
| 20 | +command -v jq >/dev/null 2>&1 || { echo "selftest needs jq to build payloads" >&2; exit 1; } |
| 21 | + |
| 22 | +LINE='Container & model: `M`, `mode:subagent`, `model: opus (no path-derived mandate)`, `effort: high (M card)`' |
| 23 | +PROMPT="No-card order. |
| 24 | +
|
| 25 | +$LINE |
| 26 | +Branch: claude/issue-7-x" |
| 27 | + |
| 28 | +payload() { # payload <tool> <subagent_type> <model> <effort> <prompt> — an empty value omits the key |
| 29 | + jq -nc --arg t "$1" --arg s "$2" --arg m "$3" --arg e "$4" --arg p "$5" \ |
| 30 | + '{tool_name:$t, tool_input:({description:"d", prompt:$p} |
| 31 | + + (if $s == "" then {} else {subagent_type:$s} end) |
| 32 | + + (if $m == "" then {} else {model:$m} end) |
| 33 | + + (if $e == "" then {} else {effort:$e} end))}' |
| 34 | +} |
| 35 | + |
| 36 | +expect() { # expect <block|allow> <label> <tool> <subagent_type> <model> <effort> <prompt> [env…] |
| 37 | + local want="$1" label="$2"; shift 2 |
| 38 | + local body rc got; body="$(payload "$1" "$2" "$3" "$4" "$5")"; shift 5 |
| 39 | + printf '%s' "$body" | env "$@" "$hook" >/dev/null 2>&1; rc=$? |
| 40 | + case "$rc" in 0) got=allow ;; 2) got=block ;; *) got="exit$rc" ;; esac |
| 41 | + if [ "$got" = "$want" ]; then pass=$((pass + 1)); printf ' ok %-5s %s\n' "$got" "$label" |
| 42 | + else fail=$((fail + 1)); printf ' FAIL want=%s got=%s %s\n' "$want" "$got" "$label"; fi |
| 43 | +} |
| 44 | + |
| 45 | +says() { # says <needle> <label> <subagent_type> <model> <effort> <prompt> |
| 46 | + local needle="$1" label="$2" out; shift 2 |
| 47 | + out="$(payload Agent "$1" "$2" "$3" "$4" | "$hook" 2>&1 >/dev/null)" |
| 48 | + case "$out" in |
| 49 | + *"$needle"*) pass=$((pass + 1)); printf ' ok says %s\n' "$label" ;; |
| 50 | + *) fail=$((fail + 1)); printf ' FAIL missing "%s" %s\n' "$needle" "$label" ;; |
| 51 | + esac |
| 52 | +} |
| 53 | + |
| 54 | +echo "== THE MEASURED SHAPE: an os-dev dispatch with no model and no effort ==" |
| 55 | +expect block 'no model, no effort, no line' Agent os-dev '' '' 'Implement the card.' |
| 56 | +expect block 'no model, no effort — even with the line in the prompt' Agent os-dev '' '' "$PROMPT" |
| 57 | + |
| 58 | +echo "== each half alone is not enough ==" |
| 59 | +expect block 'effort without model' Agent os-dev '' high "$PROMPT" |
| 60 | +expect block 'model without effort' Agent os-dev opus '' "$PROMPT" |
| 61 | +expect block 'both parameters, no Container & model line in the prompt' Agent os-dev opus high 'Implement the card.' |
| 62 | +expect block 'both parameters, the key off the line start is prose' Agent os-dev opus high "as said, $LINE" |
| 63 | +expect block 'both parameters, the line carries no effort slot' Agent os-dev opus high 'Container & model: `M`, `mode:subagent`, `model: opus`' |
| 64 | +expect block 'both parameters, the effort slot is the unfilled placeholder' Agent os-dev opus high 'Container & model: `M`, `model: opus`, `effort: <medium | high>`' |
| 65 | +expect block 'the line says high and the call passes medium' Agent os-dev opus medium "$PROMPT" |
| 66 | +expect block 'the line says medium and the call passes high' Agent os-dev opus high 'Container & model: `S`, `mode:subagent`, `model: opus`, `effort: medium`' |
| 67 | +expect block 'the same through the older tool name' Task os-dev '' '' "$PROMPT" |
| 68 | + |
| 69 | +echo "== the dispatch that carries both, and says the same on its line ==" |
| 70 | +expect allow 'model + effort, line effort equal' Agent os-dev opus high "$PROMPT" |
| 71 | +expect allow 'the older tool name' Task os-dev opus high "$PROMPT" |
| 72 | +expect allow 'undecorated line' Agent os-dev opus medium 'Container & model: S, mode:subagent, model: opus, effort: medium' |
| 73 | +expect allow 'bulleted, bold key' Agent os-dev opus high '- **Container & model:** `L`, `mode:cloud`, `model: opus`, `effort: high`' |
| 74 | +expect allow 'the effort compares case-insensitively' Agent os-dev opus HIGH 'Container & model: `M`, `model: opus`, `effort: High`' |
| 75 | +expect allow 'a reasoned exception is the claim-time table'"'"'s to judge, not this hook'"'"'s' Agent os-dev opus high 'Container & model: `S`, `model: opus`, `effort: high, reason: not mechanical`' |
| 76 | + |
| 77 | +echo "== precision: only os-dev dispatches are judged ==" |
| 78 | +expect allow 'another subagent type with nothing set' Agent general-purpose '' '' 'Look something up.' |
| 79 | +expect allow 'no subagent_type at all' Agent '' '' '' 'Look something up.' |
| 80 | +expect allow 'a longer name that merely starts with os-dev' Agent os-dev-helper '' '' 'x' |
| 81 | + |
| 82 | +echo "== the escape hatch, read from the hook's own environment ==" |
| 83 | +expect allow 'OS_ALLOW_UNTIERED_DISPATCH=1' Agent os-dev '' '' 'Implement the card.' OS_ALLOW_UNTIERED_DISPATCH=1 |
| 84 | +expect block 'any other value does not open it' Agent os-dev '' '' 'Implement the card.' OS_ALLOW_UNTIERED_DISPATCH=yes |
| 85 | + |
| 86 | +echo "== the refusal says what is missing and where the rule lives ==" |
| 87 | +says 'execution-duties.md' 'names the rulebook file' os-dev '' '' 'x' |
| 88 | +says 'no `model` parameter' 'names the missing model' os-dev '' high "$PROMPT" |
| 89 | +says 'no `effort` parameter' 'names the missing effort' os-dev opus '' "$PROMPT" |
| 90 | +says 'says `effort: high` and the call passes `effort: medium`' 'names both sides of a mismatch' os-dev opus medium "$PROMPT" |
| 91 | +says 'hook itself runs in' 'names the environment the escape hatch is read from' os-dev '' '' 'x' |
| 92 | + |
| 93 | +echo "== fails OPEN on payloads it cannot parse ==" |
| 94 | +printf '%s' 'not json at all' | "$hook" >/dev/null 2>&1 |
| 95 | +if [ $? -eq 0 ]; then pass=$((pass + 1)); printf ' ok allow <malformed payload>\n' |
| 96 | +else fail=$((fail + 1)); printf ' FAIL want=allow <malformed payload>\n'; fi |
| 97 | +printf '%s' '{"tool_name":"Agent"}' | "$hook" >/dev/null 2>&1 |
| 98 | +if [ $? -eq 0 ]; then pass=$((pass + 1)); printf ' ok allow <no tool_input>\n' |
| 99 | +else fail=$((fail + 1)); printf ' FAIL want=allow <no tool_input>\n'; fi |
| 100 | + |
| 101 | +echo "== wiring: settings.json routes Agent and Task to this hook on PreToolUse ==" |
| 102 | +for tool in Agent Task; do |
| 103 | + routed="$(jq -r --arg t "$tool" '.hooks.PreToolUse[]? | select(.matcher | split("|") | index($t)) | .hooks[]?.command' "$settings" 2>/dev/null)" |
| 104 | + case "$routed" in |
| 105 | + *guard-dispatch-tier.sh*) pass=$((pass + 1)); printf ' ok wired %s\n' "$tool" ;; |
| 106 | + *) fail=$((fail + 1)); printf ' FAIL settings.json routes no %s call to guard-dispatch-tier.sh\n' "$tool" ;; |
| 107 | + esac |
| 108 | +done |
| 109 | + |
| 110 | +echo |
| 111 | +echo "guard-dispatch-tier selftest: $pass passed, $fail failed" |
| 112 | +[ "$fail" -eq 0 ] || exit 1 |
| 113 | +# A floor, so a matrix that ran nothing cannot read as a pass. |
| 114 | +[ "$pass" -ge 31 ] || { echo "guard-dispatch-tier selftest: only $pass case(s) ran — the matrix is 31" >&2; exit 1; } |
| 115 | +exit 0 |
0 commit comments