Files
CASAN/packages/casan-harness/tests/phase08-compression-tests.sh
T
2026-07-29 18:53:08 +07:00

285 lines
12 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)"
source "$SCRIPT_DIR/../scripts/bash/casan-paths.sh"
PROJECT_ROOT="$CASAN_APP_ROOT"
CC="$CASAN_HARNESS_ROOT/scripts/bash/context-compress.py"
FIXTURES="$SCRIPT_DIR/fixtures/context-compression/nehops"
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)"
# 8) Realistic NEHOPS regression: abbreviated [WRN] tags and domain invariants
# survive structural compression, with hash-bound evidence that labels token
# figures as whitespace estimates.
set +e
python3 "$CC" --mode structural \
--input "$FIXTURES/analyzer-sanitized.log" \
--must-keep-file "$FIXTURES/must-keep.patterns" \
--require-must-keep-file "$FIXTURES/must-keep.patterns" \
--report-json "$WORK/nehops-report.json" \
>"$WORK/nehops-compressed.log" 2>"$WORK/nehops.err"
RC=$?
set -e 2>/dev/null || true
RAW_WARNINGS="$(grep -c '\[WRN\]' "$FIXTURES/analyzer-sanitized.log")"
OUT_WARNINGS="$(grep -c '\[WRN\]' "$WORK/nehops-compressed.log" || true)"
if [[ "$RC" -eq 0 && "$RAW_WARNINGS" -eq 2 && "$OUT_WARNINGS" -eq "$RAW_WARNINGS" ]] \
&& grep -q "FRR04700_CL.*common code tracing skipped" "$WORK/nehops-compressed.log" \
&& grep -q "FRR04700_SV.*common code tracing skipped" "$WORK/nehops-compressed.log" \
&& grep -q "Error Messages: 0" "$WORK/nehops-compressed.log"; then
pass "NEHOPS [WRN] + CL/SV/domain invariants survive compression"
else
fail "NEHOPS warning preservation failed (rc=$RC raw=$RAW_WARNINGS out=$OUT_WARNINGS)"
fi
set +e
python3 - "$WORK/nehops-report.json" <<'PY'
import json
import sys
report = json.load(open(sys.argv[1], encoding="utf-8"))
assert report["decision"] == "compressed"
assert report["preservation"]["status"] == "passed"
assert report["preservation"]["protected_line_missing"] == 0
assert report["preservation"]["required_pattern_deficits"] == []
assert report["measurement_source"] == "whitespace_estimate"
assert report["provider_telemetry"] is None
assert report["saving_status"] == "estimate_only_quality_gate_required"
assert report["severity"]["raw"]["warning"] == 2
assert report["severity"]["candidate"]["warning"] == 2
assert len(report["hashes"]["raw_sha256"]) == 64
assert len(report["hashes"]["candidate_sha256"]) == 64
PY
RC=$?
set -e 2>/dev/null || true
[[ "$RC" -eq 0 ]] \
&& pass "JSON evidence binds hashes and labels estimate/provider status" \
|| fail "JSON evidence contract invalid"
# 9) Required-pattern multiplicity: retaining one of two matching lines is not
# sufficient. Rejection defaults to raw fallback and reports zero savings.
printf 'TRACE-MUST plain line\nERROR TRACE-MUST protected line\n' > "$WORK/multi.txt"
printf 'TRACE-MUST\n' > "$WORK/multi.patterns"
set +e
python3 "$CC" --mode extractive --input "$WORK/multi.txt" \
--require-must-keep-file "$WORK/multi.patterns" \
>"$WORK/multi.out" 2>"$WORK/multi.err"
RC=$?
set -e 2>/dev/null || true
if [[ "$RC" -eq 1 ]] \
&& cmp -s "$WORK/multi.txt" "$WORK/multi.out" \
&& grep -q "TRACE-MUST(1/2)" "$WORK/multi.err" \
&& grep -q "saving_status=rejected" "$WORK/multi.err" \
&& grep -q "saved=0" "$WORK/multi.err"; then
pass "partial must-keep loss is rejected with raw fallback and no saving claim"
else
fail "multiplicity/fallback contract failed (rc=$RC)"
fi
# 10) A caller may choose halt instead of raw fallback. Halt returns non-zero
# and emits no candidate bytes.
set +e
python3 "$CC" --mode extractive --input "$WORK/multi.txt" \
--require-must-keep-file "$WORK/multi.patterns" \
--on-preservation-failure halt \
>"$WORK/halt.out" 2>"$WORK/halt.err"
RC=$?
set -e 2>/dev/null || true
if [[ "$RC" -eq 1 && ! -s "$WORK/halt.out" ]] \
&& grep -q "COMPRESS_REJECTED fallback=halt" "$WORK/halt.err" \
&& grep -q "saving_status=rejected" "$WORK/halt.err" \
&& grep -q "saved=0" "$WORK/halt.err"; then
pass "halt policy emits no lossy candidate on preservation failure"
else
fail "halt policy leaked output or returned success (rc=$RC)"
fi
# 11) Required patterns absent from raw are not fabricated and do not create a
# false failure; preservation checks only claims that raw evidence contained.
printf 'ERROR real failure line\n' > "$WORK/absent.txt"
printf 'NEVER_PRESENT_IN_RAW\n' > "$WORK/absent.patterns"
set +e
python3 "$CC" --mode extractive --input "$WORK/absent.txt" \
--require-must-keep-file "$WORK/absent.patterns" \
>"$WORK/absent.out" 2>"$WORK/absent.err"
RC=$?
set -e 2>/dev/null || true
[[ "$RC" -eq 0 ]] \
&& pass "pattern absent from raw does not create a false preservation failure" \
|| fail "absent raw pattern incorrectly failed preservation (rc=$RC)"
# 12) Invalid project regex is a configuration defect and fails closed without
# emitting a candidate.
printf '[unterminated\n' > "$WORK/invalid.patterns"
set +e
python3 "$CC" --mode structural --input "$WORK/absent.txt" \
--must-keep-file "$WORK/invalid.patterns" \
>"$WORK/invalid.out" 2>"$WORK/invalid.err"
RC=$?
set -e 2>/dev/null || true
if [[ "$RC" -eq 1 && ! -s "$WORK/invalid.out" ]] \
&& grep -q "COMPRESS_FAIL invalid_must_keep_regex" "$WORK/invalid.err"; then
pass "invalid must-keep regex fails closed"
else
fail "invalid regex did not fail closed (rc=$RC)"
fi
# 13) Non-empty raw input may not silently compress to an empty view.
printf 'ordinary informational chatter\n' > "$WORK/empty-loss.txt"
set +e
python3 "$CC" --mode structural --input "$WORK/empty-loss.txt" \
>"$WORK/empty-loss.out" 2>"$WORK/empty-loss.err"
RC=$?
set -e 2>/dev/null || true
if [[ "$RC" -eq 1 ]] \
&& cmp -s "$WORK/empty-loss.txt" "$WORK/empty-loss.out" \
&& grep -q "COMPRESS_REJECTED fallback=raw" "$WORK/empty-loss.err"; then
pass "empty compressed view is rejected and raw is preserved"
else
fail "non-empty raw input was allowed to disappear (rc=$RC)"
fi
# 14) Dedup may collapse repeated warnings only when the multiplicity marker
# proves how many occurrences were present.
printf '[WRN] repeated warning\n[WRN] repeated warning\n[WRN] repeated warning\n' > "$WORK/repeated-warning.txt"
set +e
python3 "$CC" --mode structural --input "$WORK/repeated-warning.txt" \
>"$WORK/repeated-warning.out" 2>"$WORK/repeated-warning.err"
RC=$?
set -e 2>/dev/null || true
if [[ "$RC" -eq 0 ]] \
&& grep -q '\[WRN\] repeated warning (x3)' "$WORK/repeated-warning.out"; then
pass "dedup preserves repeated-warning multiplicity"
else
fail "repeated-warning multiplicity was lost (rc=$RC)"
fi
# 15) A literal line ending in "(xN)" is content, not necessarily a dedup
# marker. It must survive in both non-dedup and dedup modes.
printf 'ERROR literal marker (x3)\n' > "$WORK/literal-marker.txt"
set +e
python3 "$CC" --mode extractive --input "$WORK/literal-marker.txt" \
>"$WORK/literal-marker.out" 2>"$WORK/literal-marker.err"
RC=$?
set -e 2>/dev/null || true
if [[ "$RC" -eq 0 ]] \
&& cmp -s "$WORK/literal-marker.txt" "$WORK/literal-marker.out"; then
pass "extractive mode does not misread a literal (xN) suffix"
else
fail "literal (xN) content triggered a false preservation failure (rc=$RC)"
fi
set +e
python3 "$CC" --mode structural --input "$WORK/literal-marker.txt" \
>"$WORK/literal-marker-structural.out" 2>"$WORK/literal-marker-structural.err"
RC=$?
set -e 2>/dev/null || true
if [[ "$RC" -eq 0 ]] \
&& cmp -s "$WORK/literal-marker.txt" "$WORK/literal-marker-structural.out"; then
pass "structural mode disambiguates a literal (xN) suffix"
else
fail "structural mode misread literal (xN) content (rc=$RC)"
fi
echo ""
echo "===== Plan-08 ⟷ Control Plane: settings govern harness ====="
CPS="$CASAN_HARNESS_ROOT/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