Files
CASAN/AINative_OKR_CASAN5/packages/casan-harness/tests/phase08-compression-tests.sh
T
thanhnvandClaude Opus 4.8 3adc48ed82 feat(plan-01): Phase 4a — make harness location-independent (facade-free capable)
Resolve every root by marker walk-up instead of a fixed depth that only lands on the
app via the .specify compat symlink, so the harness runs correctly when invoked by its
real packages/casan-harness path — proven by a full gate run via that path: 64/0/0.

- 95 scripts/tests: PROJECT_ROOT/ROOT "$SCRIPT_DIR/../.."-style computations -> $CASAN_APP_ROOT.
- 6 leaf scripts (infra-lab, context-validate, secrets-scan, path-guard, toolchain-verify,
  phase2-sourcegen) now source casan-paths + use CASAN_APP_ROOT.
- run-casan4: source casan-paths as a package sibling (facade-independent), PROJECT_ROOT=CASAN_APP_ROOT.
- 8 Python files: project_root()/REPO_ROOT/bundle_root walk UP for the .specify marker
  (control-plane-settings, loop_common, model-call, context-compress, test-integrity,
  bundle-integrity, traceability-matrix; generate-* fixed earlier).
- evidence-pack-build.py + traceability-matrix.py: domain refs -> apps/okr/domain
  (input/, corpus/redteam-vectors.jsonl, traceability-map.json).
- ci-harness-gate.sh: export CASAN_TESTS_DIR/CASAN_TEST_MANIFEST/CASAN_BUNDLE_ROOT so the
  integrity Python resolves via the harness root regardless of invocation path; ROOT=CASAN_APP_ROOT.
- Remove the domain compat symlinks from packages/casan-harness/security (redteam-corpus,
  redteam-vectors, benign-corpus) — packages now holds NO domain data.

Both invocation paths pass (compat facade still present): .specify/... and packages/...

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 10:50:50 +09:00

112 lines
5.0 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"
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="$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