Files
CASAN/AINative_OKR_CASAN5/packages/casan-harness/tests/phase-loop-trace-tests.sh
T
thanhnvandClaude Opus 4.8 3adc48ed82 feat(plan-01): Phase 4a — make harness location-independent (facade-free capable)
Resolve every root by marker walk-up instead of a fixed depth that only lands on the
app via the .specify compat symlink, so the harness runs correctly when invoked by its
real packages/casan-harness path — proven by a full gate run via that path: 64/0/0.

- 95 scripts/tests: PROJECT_ROOT/ROOT "$SCRIPT_DIR/../.."-style computations -> $CASAN_APP_ROOT.
- 6 leaf scripts (infra-lab, context-validate, secrets-scan, path-guard, toolchain-verify,
  phase2-sourcegen) now source casan-paths + use CASAN_APP_ROOT.
- run-casan4: source casan-paths as a package sibling (facade-independent), PROJECT_ROOT=CASAN_APP_ROOT.
- 8 Python files: project_root()/REPO_ROOT/bundle_root walk UP for the .specify marker
  (control-plane-settings, loop_common, model-call, context-compress, test-integrity,
  bundle-integrity, traceability-matrix; generate-* fixed earlier).
- evidence-pack-build.py + traceability-matrix.py: domain refs -> apps/okr/domain
  (input/, corpus/redteam-vectors.jsonl, traceability-map.json).
- ci-harness-gate.sh: export CASAN_TESTS_DIR/CASAN_TEST_MANIFEST/CASAN_BUNDLE_ROOT so the
  integrity Python resolves via the harness root regardless of invocation path; ROOT=CASAN_APP_ROOT.
- Remove the domain compat symlinks from packages/casan-harness/security (redteam-corpus,
  redteam-vectors, benign-corpus) — packages now holds NO domain data.

Both invocation paths pass (compat facade still present): .specify/... and packages/...

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 10:50:50 +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="$CASAN_APP_ROOT"
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