111 lines
4.9 KiB
Bash
111 lines
4.9 KiB
Bash
#!/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
|