#!/usr/bin/env bash set -uo pipefail # CASAN C7 — Incident response + kill-switch tests (V23). # # Proves: a detected event is graded (severity map), recorded, and for HIGH/CRIT # the scoped kill-switch auto-engages (gates honoring it then stop); MED/LOW only # record. Kill-switch check/clear and global scope work. Deterministic, no infra. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../scripts/bash/casan-paths.sh" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" S="$CASAN_HARNESS_ROOT/scripts/bash" WORK="$(mktemp -d)"; trap 'rm -rf "$WORK"' EXIT export CASAN_KILLSWITCH_DIR="$WORK/ks" # isolate the kill-switch state PASS=0; FAIL=0 pass() { echo "PASS: $1"; PASS=$((PASS + 1)); } fail() { echo "FAIL: $1"; FAIL=$((FAIL + 1)); } expect_rc() { local want="$1" desc="$2"; shift 2 local got=0; { "$@" >/dev/null 2>&1; } || got=$? [[ "$got" -eq "$want" ]] && pass "$desc (rc=$got)" || fail "$desc (got rc=$got, want $want)" } INC() { bash "$S/incident.sh" "$@"; } KS() { bash "$S/kill-switch.sh" "$@"; } echo "===== C7: severity classification + auto kill-switch =====" # CRIT event -> exit 2 + kill-switch engaged for its scope OUT="$(INC raise secret-to-cloud "key in prompt" --scope model --id m1 2>/dev/null)"; RC=$? { [[ "$RC" -eq 2 ]] && printf '%s' "$OUT" | grep -q "sev=CRIT" && printf '%s' "$OUT" | grep -q "kill_switch_engaged"; } \ && pass "CRIT event (secret-to-cloud) → exit 2 + kill-switch engaged" \ || fail "CRIT handling wrong (rc=$RC out=$OUT)" expect_rc 2 "kill-switch now blocks that scope (model/m1)" KS check model m1 # HIGH event also engages expect_rc 2 "HIGH event (audit-chain-broken) → exit 2" INC raise audit-chain-broken "line 1" --scope project --id p1 expect_rc 2 "kill-switch blocks project/p1 after HIGH" KS check project p1 # MED event: recorded only, no kill-switch expect_rc 0 "MED event (cost-budget-exceeded) → exit 0 (recorded, no kill)" INC raise cost-budget-exceeded "3x budget" --scope model --id m2 expect_rc 0 "kill-switch stays clear for a MED-only scope (model/m2)" KS check model m2 # Unknown event → default severity (MED) → recorded, no kill expect_rc 0 "unknown event → default MED (recorded, no kill)" INC raise some-unmapped-thing --scope model --id m3 echo "===== C7: kill-switch lifecycle + global scope =====" expect_rc 0 "clear an engaged switch" KS clear model m1 "resolved-in-test" expect_rc 0 "cleared scope is unblocked again" KS check model m1 KS engage global all "org-wide freeze" >/dev/null 2>&1 expect_rc 2 "global kill-switch blocks ANY scope" KS check model brand-new KS clear global all "unfreeze" >/dev/null 2>&1 expect_rc 0 "after clearing global, scopes flow again" KS check model brand-new echo "===== C7: incident record is structured (severity + owner) =====" REC="$(INC raise private-key-exposure "id_rsa in output" --scope provider --id prov1 2>/dev/null)" || true LOGF="$CASAN_STATE_ROOT/logs/level5/incidents.jsonl" if tail -5 "$LOGF" 2>/dev/null | grep -qE '"severity": ?"CRIT"' && tail -5 "$LOGF" 2>/dev/null | grep -qE '"owner": ?"security-oncall"'; then pass "incident recorded with severity + owner (routable)" else fail "incident record missing severity/owner" fi KS clear provider prov1 "test-cleanup" >/dev/null 2>&1 || true echo "===== C7: production wrapper honors the kill-switch =====" printf 'benign task input\n' > "$WORK/w.txt" # switch clear → wrapper runs normally expect_rc 0 "wrapper runs when kill-switch is clear (enforce on)" \ env CASAN_KILLSWITCH_ENFORCE=1 CASAN_KILLSWITCH_SCOPE=project CASAN_KILLSWITCH_ID=wf1 \ bash "$S/casan-harness.sh" "$WORK/w.txt" "$WORK/wo.txt" agent_step KS engage project wf1 "drill" >/dev/null 2>&1 expect_rc 2 "wrapper REFUSES to run when kill-switch engaged" \ env CASAN_KILLSWITCH_ENFORCE=1 CASAN_KILLSWITCH_SCOPE=project CASAN_KILLSWITCH_ID=wf1 \ bash "$S/casan-harness.sh" "$WORK/w.txt" "$WORK/wo.txt" agent_step expect_rc 0 "wrapper ignores engaged switch when enforcement is OFF (backward compat)" \ env CASAN_KILLSWITCH_SCOPE=project CASAN_KILLSWITCH_ID=wf1 \ bash "$S/casan-harness.sh" "$WORK/w.txt" "$WORK/wo.txt" agent_step KS clear project wf1 "cleanup" >/dev/null 2>&1 echo "" echo "===== C7 INCIDENT SUMMARY: PASS=$PASS FAIL=$FAIL =====" [[ "$FAIL" -eq 0 ]] || exit 1