Files
CASAN/AINative_OKR_CASAN5/.specify/tests/phase-selfimprove-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

64 lines
3.0 KiB
Bash

#!/usr/bin/env bash
set -uo pipefail
# CASAN Plan-04 — self-improve core (harness-owned) tests.
# Deterministic. Proves proposals are generated from telemetry (dry-run, no writes),
# apply is refused without approval, and an approved apply goes through the governed
# settings store (audited).
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../scripts/bash/casan-paths.sh"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
SI="$CASAN_HARNESS_ROOT/scripts/bash/self-improve.py"
CPS="$CASAN_HARNESS_ROOT/scripts/bash/control-plane-settings.py"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
export CASAN_CP_STORE_FILE="$WORK/store.json"
PASS=0; FAIL=0
pass() { echo "PASS: $1"; PASS=$((PASS + 1)); }
fail() { echo "FAIL: $1"; FAIL=$((FAIL + 1)); }
echo "===== Plan-04 self-improve core (harness) ====="
# metrics telemetry
cat > "$WORK/metrics.jsonl" <<'JSON'
{"step":"01-srs","cost_usd":0.02,"status":"ok"}
{"step":"10-impl","cost_usd":0.08,"status":"ok"}
JSON
cat > "$WORK/drift.json" <<'JSON'
{"drift": true, "entries": ["golden mismatch on 03-spec"]}
JSON
# 1) propose is dry-run (no store write) and yields a cost-cap proposal
python3 "$SI" propose --metrics "$WORK/metrics.jsonl" --drift "$WORK/drift.json" > "$WORK/props.json" 2>/dev/null
grep -q "P-COST-CAP" "$WORK/props.json" && pass "propose emits cost-cap proposal from telemetry" || fail "no cost-cap proposal"
[[ ! -f "$CASAN_CP_STORE_FILE" ]] && pass "propose is dry-run (no store written)" || fail "propose wrote store (not dry-run)"
# 2) drift → security-sensitive golden proposal present
grep -q "P-GOLDEN" "$WORK/props.json" && pass "drift yields review-required golden proposal" || fail "no golden proposal"
# 3) apply WITHOUT approval => deny (fail-able: proposal != application)
set +e
python3 "$SI" apply --proposals "$WORK/props.json" --id P-COST-CAP >/dev/null 2>"$WORK/3.err"; RC=$?
set -e 2>/dev/null || true
[[ "$RC" -eq 1 ]] && grep -q "APPROVAL_REQUIRED" "$WORK/3.err" \
&& pass "apply refused without approval" || fail "apply not refused without approval (rc=$RC)"
# 4) apply WITH approval => governed set recorded in store
python3 "$SI" apply --proposals "$WORK/props.json" --id P-COST-CAP --approval human-ok >/dev/null 2>&1 \
&& pass "approved apply succeeds" || fail "approved apply failed"
VAL="$(python3 "$CPS" get cost.absolute_cap_usd 2>/dev/null | python3 -c 'import json,sys;print(json.load(sys.stdin)["value"])' 2>/dev/null)"
[[ "$VAL" == "0.12" ]] && pass "applied change persisted via governed store (value=$VAL)" || fail "governed store value unexpected (value=$VAL)"
# 5) security-sensitive golden proposal without approval => deny
set +e
python3 "$SI" apply --proposals "$WORK/props.json" --id P-GOLDEN >/dev/null 2>"$WORK/5.err"; RC=$?
set -e 2>/dev/null || true
[[ "$RC" -eq 1 ]] && grep -q "APPROVAL_REQUIRED" "$WORK/5.err" \
&& pass "sensitive proposal refused without approval" || fail "sensitive proposal not refused (rc=$RC)"
echo ""
echo "===== SELF-IMPROVE SUMMARY: PASS=$PASS FAIL=$FAIL ====="
[[ "$FAIL" -eq 0 ]] || exit 1