Files
CASAN/AINative_OKR_CASAN5/.specify/tests/phase-loop-convergence-tests.sh
T
thanhnvandClaude Opus 4.8 2c765c9a45 feat(plan-01): Phase 0.5 — path indirection via casan-paths.sh (no file moves)
Task 1.2: introduce a single path resolver so no harness script hardcodes
`.specify/...` scattered across the tree. casan-paths.sh resolves four roots
(HARNESS/STATE/GOVERNANCE/APP) by marker-based walk-up from its own location —
never `git rev-parse` (git root is the repo PARENT here, not the app dir).

- 101 bash scripts/tests: 238 hardcoded `$PROJECT_ROOT/.specify/...` refs rewritten
  to CASAN_HARNESS_ROOT (code) / CASAN_STATE_ROOT (logs,state) / CASAN_GOVERNANCE_ROOT.
  Sandbox test vars ($WORK/$TP/$FP/$T1_WORK) left untouched.
- Roots are NOT exported: each script/subprocess self-resolves from its own tree,
  matching the original per-script semantics and preserving hermetic sandbox isolation
  (node casan-step.mjs, copied telemetry/rollback scripts must not inherit real roots).
- Sandbox tests that copy a harness script now also copy casan-paths.sh (its new
  sibling dependency): adversarial (verify-audit-chain/verify-tool-audit/rollback) +
  track-a (security-check/telemetry-integrity).
- control-plane-settings.json reclassified as STATE (untracked runtime store).

Roots all still resolve to `.specify` in this monolithic layout, so behavior is
unchanged. Full gate: PASS=64 FAIL=0 SKIP=3 (adversarial 44/0, track-a 25/0).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-07 22:07:41 +09:00

118 lines
4.5 KiB
Bash

#!/usr/bin/env bash
set -uo pipefail
# CASAN Plan-17 Track 2 — No-progress / Oscillation Detector.
#
# Proves:
# * a strictly improving progress series -> CONVERGING (exit 0),
# * N identical consecutive actions -> OSCILLATING (repeat),
# * A,B,A,B... alternation -> OSCILLATING (thrash),
# * flat progress over the window -> STALLED,
# * on_stall=escalate -> ESCALATE (exit 4); on_stall=halt -> HALT (exit 3),
# * a corrupt observation stream fails closed (HALT, not silent CONVERGING),
# * verdicts write a hash-linked audit record,
# * state is redirected -> repo .specify/state stays clean.
#
# Deterministic; hermetic; no model / network / docker.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../scripts/bash/casan-paths.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
CONV="$CASAN_HARNESS_ROOT/scripts/bash/loop-convergence.py"
POLICY="$CASAN_HARNESS_ROOT/config/loop-policy.yaml"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
export CASAN_LOOP_STATE_ROOT="$WORK/state"
export CASAN_LOOP_POLICY_FILE="$POLICY"
PASS=0; FAIL=0
pass() { echo "PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo "FAIL: $1"; FAIL=$((FAIL + 1)); }
obs() { python3 "$CONV" observe --run-id "$1" --step "$2" --action-hash "$3" --progress "$4" >/dev/null; }
verdict() {
# verdict <expect_rc> <desc> <run-id> [profile]
local expect="$1" desc="$2" run="$3" prof="${4:-prod}"
set +e
OUT="$(python3 "$CONV" verdict --run-id "$run" --profile "$prof" 2>/dev/null)"
local rc=$?
set -e 2>/dev/null || true
if [[ "$rc" -eq "$expect" ]]; then pass "$desc (rc=$rc)"; else fail "$desc (rc=$rc, want $expect)"; fi
}
echo "===== Plan-17 T2: Convergence Detector ====="
# 1) improving progress -> CONVERGING
obs conv-ok 1 aaa 0.1; obs conv-ok 2 bbb 0.4; obs conv-ok 3 ccc 0.7; obs conv-ok 4 ddd 0.95
verdict 0 "improving progress -> CONVERGING" conv-ok
grep -q '"verdict": "CONVERGING"' <<<"$OUT" && pass "CONVERGING in envelope" || fail "missing CONVERGING"
# 2) N identical consecutive actions -> OSCILLATING (repeat). prod N=3.
obs osc-rep 1 same 0.2; obs osc-rep 2 same 0.3; obs osc-rep 3 same 0.4
verdict 4 "repeat action -> OSCILLATING (escalate)" osc-rep
grep -q '"verdict": "OSCILLATING"' <<<"$OUT" && pass "OSCILLATING (repeat) in envelope" || fail "missing OSCILLATING repeat"
# 3) A,B,A,B thrash over window (prod thrash_window=4) -> OSCILLATING
obs osc-thr 1 A 0.1; obs osc-thr 2 B 0.2; obs osc-thr 3 A 0.3; obs osc-thr 4 B 0.4
verdict 4 "thrash A,B,A,B -> OSCILLATING" osc-thr
grep -q '"pattern": "thrash"' <<<"$OUT" && pass "thrash pattern detected" || fail "thrash not detected"
# 4) flat progress over window (prod no_progress_window=3) -> STALLED
obs stall 1 a 0.5; obs stall 2 b 0.5; obs stall 3 c 0.5; obs stall 4 d 0.5
verdict 4 "flat progress -> STALLED (escalate)" stall
grep -q '"verdict": "STALLED"' <<<"$OUT" && pass "STALLED in envelope" || fail "missing STALLED"
# 5) on_stall=halt -> HALT (exit 3)
cat > "$WORK/halt-stall.yaml" <<'YAML'
version: 1
profiles:
prod:
convergence:
oscillation_repeat: 3
no_progress_window: 3
on_stall: halt
YAML
obs halt-run 1 x 0.5; obs halt-run 2 x 0.5; obs halt-run 3 x 0.5
CASAN_LOOP_POLICY_FILE="$WORK/halt-stall.yaml" verdict 3 "on_stall=halt -> HALT" halt-run
# 6) corrupt observation stream -> fail-closed HALT
mkdir -p "$CASAN_LOOP_STATE_ROOT/runs/broken"
printf 'not-json{{{\n' > "$CASAN_LOOP_STATE_ROOT/runs/broken/convergence.jsonl"
verdict 3 "corrupt observation stream -> fail-closed HALT" broken
grep -q '"reason": "fail_closed"' <<<"$OUT" && pass "corrupt stream fails closed" || fail "corrupt stream not fail-closed"
# 7) insufficient data -> CONVERGING (governor is the hard stop)
obs few 1 a 0.1
verdict 0 "insufficient data -> CONVERGING" few
# 8) verdict wrote a hash-linked audit record chaining to genesis
AUDIT="$CASAN_LOOP_STATE_ROOT/audit/loop-audit.jsonl"
if [[ -s "$AUDIT" ]]; then
pass "audit log written on verdict"
python3 - "$AUDIT" <<'PY' && pass "audit chain links to genesis" || fail "audit chain broken"
import json, sys
lines=[l for l in open(sys.argv[1]) if l.strip()]
prev="0"*64
for l in lines:
e=json.loads(l)
if e.get("prev_hash")!=prev: raise SystemExit(1)
prev=e.get("hash")
raise SystemExit(0 if lines else 1)
PY
else
fail "audit log not written"
fi
# 9) no repo pollution
if [[ -d "$CASAN_STATE_ROOT/state" ]] && [[ -n "$(ls -A "$CASAN_STATE_ROOT/state" 2>/dev/null)" ]]; then
fail "repo .specify/state was polluted"
else
pass "repo .specify/state stays clean"
fi
echo ""
echo "===== T2 SUMMARY: PASS=$PASS FAIL=$FAIL ====="
[[ "$FAIL" -eq 0 ]] || exit 1