update doc and optimize
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
#!/usr/bin/env bash
|
||||
set -uo pipefail
|
||||
|
||||
# CASAN Plan-13 — Control Plane governed settings store (harness-owned core).
|
||||
# Deterministic; no model/app required. Proves deny-by-default, approval gating,
|
||||
# versioning/rollback, and audit hash-chain tamper detection.
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
||||
CP="$PROJECT_ROOT/.specify/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-13 Control Plane governed settings (harness core) ====="
|
||||
|
||||
# 1) deny-by-default
|
||||
set +e
|
||||
python3 "$CP" set not.allowed.key 1 --actor a@x --reason r >/dev/null 2>"$WORK/1.err"; RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -eq 2 ]] && grep -q "SETTING_NOT_ALLOWED" "$WORK/1.err" \
|
||||
&& pass "deny-by-default rejects unknown key" || fail "unknown key not rejected (rc=$RC)"
|
||||
|
||||
# 2) non-sensitive set + version increment
|
||||
python3 "$CP" set compression.enabled true --actor a@x --reason enable >/dev/null 2>&1
|
||||
V2="$(python3 "$CP" set compression.enabled false --actor a@x --reason disable 2>/dev/null | python3 -c 'import json,sys;print(json.load(sys.stdin)["version"])')"
|
||||
[[ "$V2" == "2" ]] && pass "non-sensitive set versioned (version=$V2)" || fail "versioning wrong (version=$V2)"
|
||||
|
||||
# 3) security-sensitive requires approval
|
||||
set +e
|
||||
python3 "$CP" set security.strict true --actor a@x --reason r >/dev/null 2>"$WORK/3.err"; RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -eq 3 ]] && grep -q "APPROVAL_REQUIRED" "$WORK/3.err" \
|
||||
&& pass "security-sensitive set denied without approval" || fail "approval gate missing (rc=$RC)"
|
||||
python3 "$CP" set security.strict true --actor a@x --reason r --approval jwt-tok >/dev/null 2>&1 \
|
||||
&& pass "security-sensitive set allowed with approval" || fail "approved sensitive set failed"
|
||||
|
||||
# 4) rollback restores previous value
|
||||
python3 "$CP" set cost.absolute_cap_usd 1 --actor a@x --reason first >/dev/null 2>&1
|
||||
python3 "$CP" set cost.absolute_cap_usd 5 --actor a@x --reason second >/dev/null 2>&1
|
||||
RB="$(python3 "$CP" rollback cost.absolute_cap_usd --actor a@x --reason revert 2>/dev/null | python3 -c 'import json,sys;print(json.load(sys.stdin)["value"])')"
|
||||
[[ "$RB" == "1" ]] && pass "rollback restores previous value" || fail "rollback wrong (value=$RB)"
|
||||
|
||||
# 5) audit verifies intact, then detects tampering (fail-able)
|
||||
python3 "$CP" verify-audit >/dev/null 2>&1 && pass "audit chain verifies intact" || fail "intact audit reported broken"
|
||||
python3 - "$CASAN_CP_STORE_FILE" <<'PY'
|
||||
import json, sys
|
||||
d = json.load(open(sys.argv[1]))
|
||||
d["audit"][0]["value"] = "tampered"
|
||||
json.dump(d, open(sys.argv[1], "w"))
|
||||
PY
|
||||
set +e
|
||||
python3 "$CP" verify-audit >/dev/null 2>"$WORK/5.err"; RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -eq 1 ]] && pass "audit chain detects tampering (fail-able gate)" || fail "tamper not detected (rc=$RC)"
|
||||
|
||||
# 6) effective: default when unset, override when set
|
||||
[[ "$(python3 "$CP" effective compression.mode --default extractive 2>/dev/null)" == "extractive" ]] \
|
||||
&& pass "effective returns default when unset" || fail "effective default wrong"
|
||||
python3 "$CP" set compression.mode structural --actor a@x --reason r >/dev/null 2>&1
|
||||
[[ "$(python3 "$CP" effective compression.mode --default extractive 2>/dev/null)" == "structural" ]] \
|
||||
&& pass "effective returns override when set" || fail "effective override wrong"
|
||||
|
||||
echo ""
|
||||
echo "===== CONTROL-PLANE SUMMARY: PASS=$PASS FAIL=$FAIL ====="
|
||||
[[ "$FAIL" -eq 0 ]] || exit 1
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env bash
|
||||
set -uo pipefail
|
||||
|
||||
# CASAN Plan-09 tie-in — unified governance evidence report tests.
|
||||
# Deterministic. Proves the report aggregates all governance cores, certifies a
|
||||
# clean run, and refuses to certify (fail-able gate) when the control-plane audit
|
||||
# chain is tampered.
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
||||
GR="$PROJECT_ROOT/.specify/scripts/bash/governance-report.py"
|
||||
CPS="$PROJECT_ROOT/.specify/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-09 unified governance evidence report ====="
|
||||
|
||||
# 1) clean run => CERTIFIED, report file written, all controls present
|
||||
python3 "$GR" --out "$WORK/gov.json" --gate >"$WORK/1.out" 2>&1
|
||||
RC=$?
|
||||
if [[ "$RC" -eq 0 ]] && grep -q "badge=CERTIFIED" "$WORK/1.out"; then
|
||||
pass "clean run is CERTIFIED"
|
||||
else
|
||||
cat "$WORK/1.out"; fail "clean run not certified (rc=$RC)"
|
||||
fi
|
||||
[[ -f "$WORK/gov.json" ]] && pass "report artifact written" || fail "no report artifact"
|
||||
CTRL="$(python3 -c "import json;d=json.load(open('$WORK/gov.json'));print(all(d['controls_present'].values()))" 2>/dev/null)"
|
||||
[[ "$CTRL" == "True" ]] && pass "all governance controls present" || fail "controls missing ($CTRL)"
|
||||
|
||||
# 2) traceability summary embedded
|
||||
TF="$(python3 -c "import json;print(json.load(open('$WORK/gov.json'))['traceability']['failed'])" 2>/dev/null)"
|
||||
[[ "$TF" == "0" ]] && pass "traceability summary embedded (failed=0)" || fail "traceability summary wrong (failed=$TF)"
|
||||
|
||||
# 3) tamper control-plane audit => NOT certified (fail-able gate)
|
||||
python3 "$CPS" set compression.enabled true --actor a@x --reason r >/dev/null 2>&1
|
||||
python3 - "$CASAN_CP_STORE_FILE" <<'PY'
|
||||
import json, sys
|
||||
d = json.load(open(sys.argv[1]))
|
||||
d["audit"][0]["value"] = "tampered"
|
||||
json.dump(d, open(sys.argv[1], "w"))
|
||||
PY
|
||||
set +e
|
||||
python3 "$GR" --out "$WORK/gov2.json" --gate >"$WORK/3.out" 2>"$WORK/3.err"; RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -eq 1 ]] && grep -q "badge=NOT_CERTIFIED" "$WORK/3.out" \
|
||||
&& pass "tampered audit ⇒ NOT_CERTIFIED (fail-able)" || fail "tamper did not block certification (rc=$RC)"
|
||||
|
||||
echo ""
|
||||
echo "===== GOVERNANCE-REPORT SUMMARY: PASS=$PASS FAIL=$FAIL ====="
|
||||
[[ "$FAIL" -eq 0 ]] || exit 1
|
||||
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env bash
|
||||
set -uo pipefail
|
||||
|
||||
# CASAN Plan-15/13 — harness preflight enforcement tests.
|
||||
# Deterministic & offline: the BLOCK path short-circuits before any model call,
|
||||
# so no Ollama/cloud is needed. Proves governance cores actually gate a run.
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
||||
S="$PROJECT_ROOT/.specify/scripts/bash"
|
||||
PF="$S/harness-preflight.sh"
|
||||
ROUTER="$S/model-router.sh"
|
||||
WORK="$(mktemp -d)"
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
|
||||
PASS=0; FAIL=0
|
||||
pass() { echo "PASS: $1"; PASS=$((PASS + 1)); }
|
||||
fail() { echo "FAIL: $1"; FAIL=$((FAIL + 1)); }
|
||||
|
||||
printf 'Please summarize; contact alice@example.com about the account.\n' > "$WORK/pii.txt"
|
||||
printf 'Please refactor the dashboard grid layout.\n' > "$WORK/benign.txt"
|
||||
|
||||
echo "===== harness preflight (governance enforced before model call) ====="
|
||||
|
||||
# 1) preflight blocks PII → cloud without approval
|
||||
set +e
|
||||
bash "$PF" "$WORK/pii.txt" "$WORK/out.json" --model openai:gpt-4o >/dev/null 2>"$WORK/1.err"; RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -ne 0 ]] && grep -q "PREFLIGHT_BLOCK" "$WORK/1.err" \
|
||||
&& pass "preflight blocks PII→cloud without approval" || fail "preflight did not block (rc=$RC)"
|
||||
|
||||
# 2) preflight allows PII → cloud WITH approval
|
||||
CASAN_MODEL_APPROVAL=human-ok bash "$PF" "$WORK/pii.txt" "$WORK/out.json" --model anthropic:claude >/dev/null 2>&1 \
|
||||
&& pass "preflight allows PII→cloud with approval" || fail "approved cloud send blocked"
|
||||
|
||||
# 3) preflight allows benign → cloud
|
||||
bash "$PF" "$WORK/benign.txt" "$WORK/out.json" --model openai:gpt-4o >/dev/null 2>&1 \
|
||||
&& pass "preflight allows benign→cloud" || fail "benign cloud send blocked"
|
||||
|
||||
# 4) preflight ignores local model (no cloud data-gov gate)
|
||||
bash "$PF" "$WORK/pii.txt" "$WORK/out.json" --model ollama:ornith:9b >/dev/null 2>&1 \
|
||||
&& pass "preflight passes local model" || fail "local model wrongly blocked"
|
||||
|
||||
# 5) WIRED into router: CASAN_PREFLIGHT=1 blocks a cloud PII call BEFORE model-call
|
||||
# (short-circuits, so no live model is required to prove enforcement)
|
||||
set +e
|
||||
CASAN_PREFLIGHT=1 bash "$ROUTER" "$WORK/pii.txt" "$WORK/out.json" --model openai:gpt-4o >/dev/null 2>"$WORK/5.err"; RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -ne 0 ]] && grep -q "PREFLIGHT_BLOCK" "$WORK/5.err" \
|
||||
&& pass "model-router honors CASAN_PREFLIGHT and blocks before model call" || fail "router preflight not enforced (rc=$RC)"
|
||||
|
||||
echo ""
|
||||
echo "===== PREFLIGHT SUMMARY: PASS=$PASS FAIL=$FAIL ====="
|
||||
[[ "$FAIL" -eq 0 ]] || exit 1
|
||||
@@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env bash
|
||||
set -uo pipefail
|
||||
|
||||
# CASAN Plan-15 — Responsible AI & Data Governance guard (harness core) tests.
|
||||
# Deterministic. Proves data classification, PII→cloud denial without approval,
|
||||
# and model-card enforcement (uncarded/incomplete cards blocked).
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
||||
RAI="$PROJECT_ROOT/.specify/scripts/bash/rai-guard.py"
|
||||
WORK="$(mktemp -d)"
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
|
||||
PASS=0; FAIL=0
|
||||
pass() { echo "PASS: $1"; PASS=$((PASS + 1)); }
|
||||
fail() { echo "FAIL: $1"; FAIL=$((FAIL + 1)); }
|
||||
|
||||
echo "===== Plan-15 Responsible AI & Data Governance (harness core) ====="
|
||||
|
||||
# 1) classify detects PII (email)
|
||||
printf 'contact user at alice@example.com for details\n' > "$WORK/pii.txt"
|
||||
python3 "$RAI" classify --input "$WORK/pii.txt" 2>/dev/null | grep -q "label=PII" \
|
||||
&& pass "classify detects PII (email)" || fail "PII not classified"
|
||||
|
||||
# 2) classify detects confidential marker
|
||||
printf 'This document is CONFIDENTIAL and internal.\n' > "$WORK/conf.txt"
|
||||
python3 "$RAI" classify --input "$WORK/conf.txt" 2>/dev/null | grep -q "label=confidential" \
|
||||
&& pass "classify detects confidential marker" || fail "confidential not classified"
|
||||
|
||||
# 3) benign classify → internal
|
||||
printf 'refactor the dashboard grid layout\n' > "$WORK/benign.txt"
|
||||
python3 "$RAI" classify --input "$WORK/benign.txt" 2>/dev/null | grep -q "label=internal" \
|
||||
&& pass "benign text classified internal" || fail "benign misclassified"
|
||||
|
||||
# 4) PII → cloud without approval => DENY (fail-able)
|
||||
set +e
|
||||
python3 "$RAI" check-cloud --input "$WORK/pii.txt" --target cloud >/dev/null 2>"$WORK/4.err"; RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -eq 1 ]] && grep -q "DATA_TO_CLOUD" "$WORK/4.err" \
|
||||
&& pass "PII→cloud without approval denied" || fail "PII→cloud not denied (rc=$RC)"
|
||||
|
||||
# 5) PII → cloud WITH approval => allow; benign → cloud => allow
|
||||
python3 "$RAI" check-cloud --input "$WORK/pii.txt" --target cloud --approval jwt >/dev/null 2>&1 \
|
||||
&& pass "PII→cloud with approval allowed" || fail "approved PII→cloud denied"
|
||||
python3 "$RAI" check-cloud --input "$WORK/benign.txt" --target cloud >/dev/null 2>&1 \
|
||||
&& pass "benign→cloud allowed" || fail "benign→cloud denied"
|
||||
|
||||
# 6) model-card enforcement
|
||||
cat > "$WORK/cards.json" <<'JSON'
|
||||
{
|
||||
"ollama:ornith:9b": {"source": "local-ollama", "digest": "sha256:abc", "role": "generate", "risks": "hallucination"}
|
||||
}
|
||||
JSON
|
||||
python3 "$RAI" model-card --model "ollama:ornith:9b" --cards "$WORK/cards.json" >/dev/null 2>&1 \
|
||||
&& pass "carded model allowed" || fail "carded model denied"
|
||||
set +e
|
||||
python3 "$RAI" model-card --model "openai:gpt-4o" --cards "$WORK/cards.json" >/dev/null 2>"$WORK/6.err"; RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -eq 1 ]] && grep -q "MODEL_UNCARDED" "$WORK/6.err" \
|
||||
&& pass "uncarded model blocked (fail-able)" || fail "uncarded model not blocked (rc=$RC)"
|
||||
|
||||
# 7) incomplete card blocked
|
||||
cat > "$WORK/cards2.json" <<'JSON'
|
||||
{ "openai:gpt-4o": {"source": "openai", "role": "judge"} }
|
||||
JSON
|
||||
set +e
|
||||
python3 "$RAI" model-card --model "openai:gpt-4o" --cards "$WORK/cards2.json" >/dev/null 2>"$WORK/7.err"; RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -eq 1 ]] && grep -q "MODEL_CARD_INCOMPLETE" "$WORK/7.err" \
|
||||
&& pass "incomplete model card blocked" || fail "incomplete card not blocked (rc=$RC)"
|
||||
|
||||
# 8) RAI aggregate report: sensitivity distribution over a set
|
||||
cat > "$WORK/items.jsonl" <<'JSON'
|
||||
{"id":"a","text":"email me at bob@example.com","created_epoch":100}
|
||||
{"id":"b","text":"refactor the grid layout","created_epoch":100}
|
||||
JSON
|
||||
python3 "$RAI" report --items "$WORK/items.jsonl" 2>/dev/null | grep -q '"PII": 1' \
|
||||
&& pass "RAI report aggregates sensitivity distribution" || fail "RAI report distribution wrong"
|
||||
|
||||
# 9) retention: expired items un-purged => gate fails (fail-able)
|
||||
set +e
|
||||
python3 "$RAI" retention --items "$WORK/items.jsonl" --days 1 --now 1000000 --gate >/dev/null 2>"$WORK/9.err"; RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -eq 1 ]] && grep -q "RETENTION_BREACH" "$WORK/9.err" \
|
||||
&& pass "expired items un-purged ⇒ retention gate fails" || fail "retention breach not caught (rc=$RC)"
|
||||
|
||||
# 10) purge writes an audit record and passes the gate
|
||||
python3 "$RAI" retention --items "$WORK/items.jsonl" --days 1 --now 1000000 --purge --audit "$WORK/ret-audit.jsonl" --gate >/dev/null 2>&1 \
|
||||
&& [[ -s "$WORK/ret-audit.jsonl" ]] \
|
||||
&& pass "purge records audit and passes gate" || fail "purge/audit failed"
|
||||
|
||||
echo ""
|
||||
echo "===== RAI SUMMARY: PASS=$PASS FAIL=$FAIL ====="
|
||||
[[ "$FAIL" -eq 0 ]] || exit 1
|
||||
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env bash
|
||||
set -uo pipefail
|
||||
|
||||
# CASAN Plan-14 — RBAC decision engine (harness-owned core) tests.
|
||||
# Deterministic. Proves deny-by-default, action gating, tenant isolation,
|
||||
# sensitive-requires-org-admin, and Separation of Duties.
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
||||
RBAC="$PROJECT_ROOT/.specify/scripts/bash/rbac-check.py"
|
||||
|
||||
PASS=0; FAIL=0
|
||||
pass() { echo "PASS: $1"; PASS=$((PASS + 1)); }
|
||||
fail() { echo "FAIL: $1"; FAIL=$((FAIL + 1)); }
|
||||
allow() { python3 "$RBAC" "$@" >/dev/null 2>&1; } # rc 0 = allow
|
||||
deny() { ! python3 "$RBAC" "$@" >/dev/null 2>&1; } # rc!=0 = deny
|
||||
|
||||
echo "===== Plan-14 RBAC decision engine (harness core) ====="
|
||||
|
||||
# org-admin can do anything
|
||||
allow check --role org-admin --resource settings --action write \
|
||||
&& pass "org-admin allowed settings:write" || fail "org-admin denied"
|
||||
|
||||
# viewer can read but not write
|
||||
allow check --role viewer --resource monitoring --action read --role-project p1 --target-project p1 \
|
||||
&& pass "viewer allowed monitoring:read" || fail "viewer read denied"
|
||||
deny check --role viewer --resource settings --action write --role-project p1 --target-project p1 \
|
||||
&& pass "viewer denied settings:write (deny-by-default)" || fail "viewer write not denied"
|
||||
|
||||
# unknown role denied
|
||||
deny check --role hacker --resource monitoring --action read \
|
||||
&& pass "unknown role denied" || fail "unknown role not denied"
|
||||
|
||||
# tenant isolation: project-admin of p1 cannot touch p2
|
||||
deny check --role project-admin --resource settings --action write --role-project p1 --target-project p2 \
|
||||
&& pass "cross-tenant write denied" || fail "cross-tenant not denied"
|
||||
allow check --role project-admin --resource settings --action write --role-project p1 --target-project p1 \
|
||||
&& pass "same-tenant write allowed" || fail "same-tenant write denied"
|
||||
|
||||
# sensitive settings write requires org-admin
|
||||
deny check --role project-admin --resource settings --action write --role-project p1 --target-project p1 --sensitive \
|
||||
&& pass "sensitive write denied for project-admin" || fail "sensitive write not denied"
|
||||
allow check --role org-admin --resource settings --action write --sensitive \
|
||||
&& pass "sensitive write allowed for org-admin" || fail "org-admin sensitive denied"
|
||||
|
||||
# SoD: proposer cannot approve own request
|
||||
deny check-sod --proposer alice@x --approver alice@x \
|
||||
&& pass "SoD blocks self-approval" || fail "SoD did not block self-approval"
|
||||
allow check-sod --proposer alice@x --approver bob@x \
|
||||
&& pass "SoD allows distinct approver" || fail "SoD blocked distinct approver"
|
||||
|
||||
# IdP claim → RBAC role mapping (deny-by-default on unknown claim)
|
||||
[[ "$(python3 "$RBAC" map-claim --claim casan-org-admin 2>/dev/null)" == "org-admin" ]] \
|
||||
&& pass "known IdP claim maps to RBAC role" || fail "known claim did not map"
|
||||
deny map-claim --claim casan-random-group \
|
||||
&& pass "unknown IdP claim denied (deny-by-default)" || fail "unknown claim not denied"
|
||||
|
||||
echo ""
|
||||
echo "===== RBAC SUMMARY: PASS=$PASS FAIL=$FAIL ====="
|
||||
[[ "$FAIL" -eq 0 ]] || exit 1
|
||||
@@ -0,0 +1,62 @@
|
||||
#!/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)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
||||
SI="$PROJECT_ROOT/.specify/scripts/bash/self-improve.py"
|
||||
CPS="$PROJECT_ROOT/.specify/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
|
||||
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env bash
|
||||
set -uo pipefail
|
||||
|
||||
# CASAN Plan-08 — CASAN-native token-killer (tool-output compression) tests.
|
||||
# Deterministic; no model required. Proves compression reduces tokens, preserves
|
||||
# must-keep lines, passes raw through on failure, and the must-keep gate is
|
||||
# fail-able.
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
||||
CC="$PROJECT_ROOT/.specify/scripts/bash/context-compress.py"
|
||||
WORK="$(mktemp -d)"
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
|
||||
PASS=0; FAIL=0
|
||||
pass() { echo "PASS: $1"; PASS=$((PASS + 1)); }
|
||||
fail() { echo "FAIL: $1"; FAIL=$((FAIL + 1)); }
|
||||
|
||||
echo "===== Plan-08 CASAN-native token-killer ====="
|
||||
|
||||
# 1) dedup collapses consecutive duplicate lines
|
||||
printf 'connecting\nretry\nretry\nretry\nretry\ndone\n' > "$WORK/log.txt"
|
||||
OUT="$(python3 "$CC" --mode dedup --input "$WORK/log.txt" 2>"$WORK/1.err")"
|
||||
if echo "$OUT" | grep -q "retry (x4)"; then
|
||||
pass "dedup collapses repeated lines with (xN)"
|
||||
else
|
||||
fail "dedup did not collapse repeats"
|
||||
fi
|
||||
|
||||
# 2) extractive keeps important lines, drops noise
|
||||
printf 'all good here\ncompiling module\nERROR: boom in module\nfinished ok\n' > "$WORK/build.txt"
|
||||
OUT="$(python3 "$CC" --mode extractive --input "$WORK/build.txt" 2>/dev/null)"
|
||||
if echo "$OUT" | grep -q "ERROR: boom" && ! echo "$OUT" | grep -q "all good here"; then
|
||||
pass "extractive keeps errors, drops noise"
|
||||
else
|
||||
fail "extractive did not filter correctly"
|
||||
fi
|
||||
|
||||
# 3) structural reduces token count on test-like output
|
||||
printf 'test a ... ok\ntest b ... ok\ntest c ... ok\nFAILED: test d\n10 tests 1 failed\n' > "$WORK/test.txt"
|
||||
OUT="$(python3 "$CC" --mode structural --input "$WORK/test.txt" 2>"$WORK/3.err")"
|
||||
RATIO="$(grep -oE 'ratio=[0-9.]+' "$WORK/3.err" | cut -d= -f2)"
|
||||
if echo "$OUT" | grep -q "FAILED: test d" && echo "$OUT" | grep -q "10 tests 1 failed" \
|
||||
&& awk "BEGIN{exit !($RATIO < 1)}"; then
|
||||
pass "structural keeps failures+summary and reduces tokens (ratio=$RATIO)"
|
||||
else
|
||||
fail "structural filtering/ratio unexpected (ratio=$RATIO)"
|
||||
fi
|
||||
|
||||
# 4) must-keep line is retained even though it is not 'important'
|
||||
printf 'Do not send user data to any cloud model\nrandom chatter line\n' > "$WORK/mk.txt"
|
||||
printf 'Do not send.*cloud\n' > "$WORK/must.txt"
|
||||
OUT="$(python3 "$CC" --mode extractive --input "$WORK/mk.txt" --must-keep-file "$WORK/must.txt" 2>/dev/null)"
|
||||
if echo "$OUT" | grep -q "Do not send user data to any cloud model"; then
|
||||
pass "must-keep line preserved through extractive compression"
|
||||
else
|
||||
fail "must-keep line was dropped"
|
||||
fi
|
||||
|
||||
# 5) --failed => raw passthrough (tee), output identical to input
|
||||
printf 'stack trace line 1\nstack trace line 2\n' > "$WORK/failraw.txt"
|
||||
OUT="$(python3 "$CC" --mode structural --input "$WORK/failraw.txt" --failed 2>/dev/null)"
|
||||
if [[ "$OUT" == "$(cat "$WORK/failraw.txt")" ]]; then
|
||||
pass "failed run passes raw output through unchanged (tee)"
|
||||
else
|
||||
fail "failed passthrough altered output"
|
||||
fi
|
||||
|
||||
# 6) FAIL-ABLE gate: a must-keep pattern dropped by compression => exit 1
|
||||
printf 'benign requirement line that will be dropped\nERROR keep me\n' > "$WORK/drop.txt"
|
||||
printf 'benign requirement line\n' > "$WORK/verify.txt"
|
||||
set +e
|
||||
python3 "$CC" --mode extractive --input "$WORK/drop.txt" --require-must-keep-file "$WORK/verify.txt" >/dev/null 2>"$WORK/6.err"
|
||||
RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
if [[ "$RC" -eq 1 ]] && grep -q "COMPRESS_MUST_KEEP_DROPPED" "$WORK/6.err"; then
|
||||
pass "must-keep gate fails when a required line is dropped (fail-able)"
|
||||
else
|
||||
fail "must-keep gate did not fail as expected (rc=$RC)"
|
||||
fi
|
||||
|
||||
# 7) protecting the same pattern => retained => gate passes
|
||||
set +e
|
||||
python3 "$CC" --mode extractive --input "$WORK/drop.txt" --must-keep-file "$WORK/verify.txt" \
|
||||
--require-must-keep-file "$WORK/verify.txt" >/dev/null 2>"$WORK/7.err"
|
||||
RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -eq 0 ]] && pass "protecting the pattern keeps the line and passes the gate" \
|
||||
|| fail "protected must-keep still failed the gate (rc=$RC)"
|
||||
|
||||
echo ""
|
||||
echo "===== Plan-08 ⟷ Control Plane: settings govern harness ====="
|
||||
CPS="$PROJECT_ROOT/.specify/scripts/bash/control-plane-settings.py"
|
||||
export CASAN_CP_STORE_FILE="$WORK/cp.json"
|
||||
printf 'all good line\nERROR boom line\nall good line\n' > "$WORK/rp.txt"
|
||||
python3 "$CPS" set compression.enabled false --actor a@x --reason off >/dev/null 2>&1
|
||||
OUT="$(python3 "$CC" --mode extractive --input "$WORK/rp.txt" --respect-policy 2>/dev/null)"
|
||||
[[ "$OUT" == "$(cat "$WORK/rp.txt")" ]] \
|
||||
&& pass "compression.enabled=false ⇒ raw passthrough (setting governs harness)" \
|
||||
|| fail "respect-policy did not passthrough when disabled"
|
||||
python3 "$CPS" set compression.enabled true --actor a@x --reason on >/dev/null 2>&1
|
||||
OUT2="$(python3 "$CC" --mode extractive --input "$WORK/rp.txt" --respect-policy 2>/dev/null)"
|
||||
! echo "$OUT2" | grep -q "all good line" \
|
||||
&& pass "compression.enabled=true ⇒ compression active" \
|
||||
|| fail "respect-policy did not compress when enabled"
|
||||
unset CASAN_CP_STORE_FILE
|
||||
|
||||
echo ""
|
||||
echo "===== COMPRESSION SUMMARY: PASS=$PASS FAIL=$FAIL ====="
|
||||
[[ "$FAIL" -eq 0 ]] || exit 1
|
||||
@@ -49,6 +49,46 @@ set -e 2>/dev/null || true
|
||||
&& pass "missing FR test coverage fails the gate" \
|
||||
|| fail "missing test coverage did not fail as expected (rc=$RC)"
|
||||
|
||||
echo ""
|
||||
echo "===== Plan-10 symbol/line-level traceability ====="
|
||||
# The real map carries symbol refs for FR-01 (AuthService, login); the passing
|
||||
# run above wrote $OUT from the real map, so those symbols must resolve.
|
||||
SYM_FOUND="$(python3 -c "import json;print(json.load(open('$OUT'))['summary']['symbols_found'])" 2>/dev/null)"
|
||||
SYM_MISS="$(python3 -c "import json;print(json.load(open('$OUT'))['summary']['symbols_missing'])" 2>/dev/null)"
|
||||
[[ "${SYM_FOUND:-0}" -ge 2 && "${SYM_MISS:-1}" -eq 0 ]] \
|
||||
&& pass "real map symbol refs resolved (found=$SYM_FOUND missing=$SYM_MISS)" \
|
||||
|| fail "real map symbol refs unexpected (found=$SYM_FOUND missing=$SYM_MISS)"
|
||||
|
||||
SYMBROKEN="$WORK/map-badsym.json"
|
||||
python3 - "$MAP" "$SYMBROKEN" <<'PY'
|
||||
import json, sys
|
||||
d = json.load(open(sys.argv[1]))
|
||||
d["FR-01"]["code"] = [{"file": "backend/src/auth/auth.service.ts", "symbols": ["NoSuchSymbolXYZ"]}]
|
||||
json.dump(d, open(sys.argv[2], "w"), indent=2)
|
||||
PY
|
||||
set +e
|
||||
python3 "$S/traceability-matrix.py" --requirements "$REQ" --map "$SYMBROKEN" --out "$WORK/badsym.json" --gate >/dev/null 2>"$WORK/badsym.err"
|
||||
RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -eq 1 ]] && grep -q "missing_symbols=1" "$WORK/badsym.err" \
|
||||
&& pass "missing symbol fails the gate (symbol-level)" \
|
||||
|| fail "missing symbol did not fail as expected (rc=$RC)"
|
||||
|
||||
LINEBROKEN="$WORK/map-badline.json"
|
||||
python3 - "$MAP" "$LINEBROKEN" <<'PY'
|
||||
import json, sys
|
||||
d = json.load(open(sys.argv[1]))
|
||||
d["FR-01"]["code"] = [{"file": "backend/src/auth/auth.service.ts", "lines": [999999]}]
|
||||
json.dump(d, open(sys.argv[2], "w"), indent=2)
|
||||
PY
|
||||
set +e
|
||||
python3 "$S/traceability-matrix.py" --requirements "$REQ" --map "$LINEBROKEN" --out "$WORK/badline.json" --gate >/dev/null 2>"$WORK/badline.err"
|
||||
RC=$?
|
||||
set -e 2>/dev/null || true
|
||||
[[ "$RC" -eq 1 ]] && grep -q "missing_lines=1" "$WORK/badline.err" \
|
||||
&& pass "out-of-range line ref fails the gate (line-level)" \
|
||||
|| fail "line ref did not fail as expected (rc=$RC)"
|
||||
|
||||
echo ""
|
||||
echo "===== TRACEABILITY SUMMARY: PASS=$PASS FAIL=$FAIL ====="
|
||||
[[ "$FAIL" -eq 0 ]] || exit 1
|
||||
|
||||
Reference in New Issue
Block a user