Files
CASAN/packages/casan-harness/tests/phase-loop-trace-tests.sh
T
thanhnvandClaude Opus 4.8 36a4812ef3 refactor(structure): promote app to repo root + remove redundant workspace cruft
Standard production layout: the OKR app (was nested under AINative_OKR_CASAN5/) is now
the repository root. No more wrapper directory.

- Promote AINative_OKR_CASAN5/* -> repo root (backend/ frontend/ packages/ apps/
  .specify/ docs/ infra/ nginx/ scripts/ + configs). Merge tool dirs: .gitea (kept the
  active deploy ci.yml, added harness-ci.yml + runbooks), .claude (agents/commands +
  launch.json), .github moved up.
- Remove redundant: 00_SUBMISSION_PACKAGE, scattered root notes (FPT_CASAN_Full.md,
  tu-tuong-casan.md, casan-tu-sinh..., casan_harness_assessment.md, source-review...,
  README_CASAN5_REFINED.md), casan-next-plans/ and optimize-docs/ (competition/planning
  artifacts — roadmap + design history preserved in git log / commit messages).
- Update all references to the old layout:
  - .gitea/workflows/{ci,harness-ci}.yml, .github/workflows/{ci,deploy}.yml:
    working-directory .; drop AINative_OKR_CASAN5/ prefix; .specify/{tests,scripts}
    -> packages/casan-harness/... (.specify/logs state kept)
  - .claude/launch.json, .gitea/*-runbook.md: path prefixes
  - CLAUDE.md, README.md: docs/input -> apps/okr/domain/input
  - policy-bundle.yaml: 8 policy paths -> packages/casan-harness/...; manifest re-signed
- secrets-scan.sh: fixture excludes -> new package/domain paths.

Full gate from the new root: PASS=64 FAIL=0 SKIP=3.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 13:26:36 +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