Files
CASAN/AINative_OKR_CASAN5/packages/casan-harness/tests/phase-loop-trace-tests.sh
T
thanhnvandClaude Opus 4.8 664bd1f00c feat(plan-01): Phase 1 — relocate harness code to packages/casan-harness (symlink facade)
Physically move the pure-code subtrees out of .specify into the package, leaving
compat symlinks at the old .specify/<dir> paths so every existing reference (internal
CASAN_HARNESS_ROOT + external CI/docker/mjs) keeps resolving. Runtime state stays put.

Moved (git mv): scripts/ tests/ security/ templates/ config/ governance/ memory/
  .specify/<dir>  ->  packages/casan-harness/<dir>   (+ .specify/<dir> symlink)
Stays in .specify (state/governance/domain, handled later): logs/ agentops/ level5/
  init-options.json traceability-map.json

Python `.resolve()` self-location followed the compat symlink into packages and lost
the app root; generate-casan-demo-context.py, generate-agentops-dashboard.py and
dashboard-server.py now walk UP for the `.specify` state marker instead of a fixed
parent depth (fixes "missing trace files" in run-casan4).

Full gate: PASS=64 FAIL=0 SKIP=3 (CASAN_CI_STEP_TIMEOUT_SEC=1200 — track-a ~450s runs
close to the 600s default and can tip over under load; this is timing variance, not a
regression — it passed cleanly with headroom). Runtime log/audit artifacts kept unstaged.

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

109 lines
4.5 KiB
Bash

#!/usr/bin/env bash
set -uo pipefail
# CASAN Plan-17 Track 4 — Loop Trace / Replay.
#
# Proves:
# * record appends hash-linked Iteration records (append-only),
# * show renders the loop view (JSON on stdout),
# * verify-chain confirms an intact chain (exit 0),
# * editing a recorded record BREAKs the chain (exit 3),
# * replay re-verifies recorded artifacts and MATCHes when unchanged,
# * tampering a recorded artifact makes replay DRIFT (verdict mismatch, exit 3),
# * a corrupt trace file fails closed (verify-chain BREAK),
# * state redirected -> repo .specify/state stays clean.
#
# Deterministic; hermetic; H4 uses the offline pattern layer (no model / network).
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../scripts/bash/casan-paths.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
TRACE="$CASAN_HARNESS_ROOT/scripts/bash/loop-trace.py"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
export CASAN_LOOP_STATE_ROOT="$WORK/state"
export CASAN_LOOP_POLICY_FILE="$WORK/no-policy.yaml" # strict ceiling; not needed for trace
PASS=0; FAIL=0
pass() { echo "PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo "FAIL: $1"; FAIL=$((FAIL + 1)); }
tr_cmd() {
# tr_cmd <expect_rc> <desc> -- <args...>; stdout captured in $OUT
local expect="$1"; shift
local desc="$1"; shift
[[ "$1" == "--" ]] && shift
set +e
OUT="$(python3 "$TRACE" "$@" 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 T4: Loop Trace / Replay ====="
# fixtures: a good artifact that meets criteria (loop-gate PASS)
printf 'REQ-01 persists OKR to the database. Dashboard renders 3 widgets.\n' > "$WORK/art1.txt"
printf '{"must_contain":["REQ-01","Dashboard"]}\n' > "$WORK/crit.json"
RUN=trace-run-1
# 1) record three iterations
tr_cmd 0 "record step 1" -- record --run-id "$RUN" --step 1 --intent "persist" \
--action write --tool db --gate-verdict PASS --progress 0.3 --decision CONTINUE \
--artifact "$WORK/art1.txt" --success-criteria "$WORK/crit.json"
tr_cmd 0 "record step 2" -- record --run-id "$RUN" --step 2 --intent "render" \
--action ui --gate-verdict PASS --progress 0.6 --decision CONTINUE
tr_cmd 0 "record step 3" -- record --run-id "$RUN" --step 3 --intent "done" \
--gate-verdict PASS --progress 1.0 --decision DONE
# 2) show -> JSON loop view with 3 iterations
tr_cmd 0 "show loop view" -- show --run-id "$RUN"
grep -q '"count": 3' <<<"$OUT" && pass "show reports 3 iterations" || fail "show wrong count"
# 3) verify-chain intact
tr_cmd 0 "verify-chain intact -> OK" -- verify-chain --run-id "$RUN"
grep -q '"decision": "OK"' <<<"$OUT" && pass "chain OK decision" || fail "chain not OK"
# 4) replay unchanged artifact -> MATCH
tr_cmd 0 "replay unchanged -> MATCH" -- replay --run-id "$RUN"
grep -q '"decision": "MATCH"' <<<"$OUT" && pass "replay MATCH" || fail "replay not MATCH"
# 5) tamper the recorded artifact -> replay DRIFT (recorded PASS, now FAIL)
printf 'unrelated content, requirement removed.\n' > "$WORK/art1.txt"
tr_cmd 3 "replay after artifact tamper -> DRIFT" -- replay --run-id "$RUN"
grep -q '"decision": "DRIFT"' <<<"$OUT" && pass "replay DRIFT on tamper" || fail "tamper not detected by replay"
# 6) tamper a trace record -> verify-chain BREAK
TRACE_FILE="$CASAN_LOOP_STATE_ROOT/runs/$RUN/trace.jsonl"
# flip the progress of the 2nd record without recomputing its hash
python3 - "$TRACE_FILE" <<'PY'
import json, sys
p=sys.argv[1]
lines=[l for l in open(p) if l.strip()]
e=json.loads(lines[1]); e["iteration"]["progress"]=0.999
lines[1]=json.dumps(e, sort_keys=True, ensure_ascii=False)+"\n"
open(p,"w").writelines(lines)
PY
tr_cmd 3 "edited record -> chain BREAK" -- verify-chain --run-id "$RUN"
grep -q '"reason": "chain_broken"' <<<"$OUT" && pass "chain_broken reason" || fail "chain break not reported"
# 7) corrupt trace file -> fail-closed BREAK
RUN2=trace-broken
mkdir -p "$CASAN_LOOP_STATE_ROOT/runs/$RUN2"
printf 'not-json{{{\n' > "$CASAN_LOOP_STATE_ROOT/runs/$RUN2/trace.jsonl"
tr_cmd 3 "corrupt trace -> fail-closed BREAK" -- verify-chain --run-id "$RUN2"
grep -q '"reason": "fail_closed"' <<<"$OUT" && pass "corrupt trace fails closed" || fail "corrupt trace not fail-closed"
# 8) 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 "===== T4 SUMMARY: PASS=$PASS FAIL=$FAIL ====="
[[ "$FAIL" -eq 0 ]] || exit 1