Files
CASAN/packages/casan-harness/tests/phase08-compression-tests.sh
T
thanhnvandClaude Opus 4.8 36a4812ef3 refactor(structure): promote app to repo root + remove redundant workspace cruft
Standard production layout: the OKR app (was nested under AINative_OKR_CASAN5/) is now
the repository root. No more wrapper directory.

- Promote AINative_OKR_CASAN5/* -> repo root (backend/ frontend/ packages/ apps/
  .specify/ docs/ infra/ nginx/ scripts/ + configs). Merge tool dirs: .gitea (kept the
  active deploy ci.yml, added harness-ci.yml + runbooks), .claude (agents/commands +
  launch.json), .github moved up.
- Remove redundant: 00_SUBMISSION_PACKAGE, scattered root notes (FPT_CASAN_Full.md,
  tu-tuong-casan.md, casan-tu-sinh..., casan_harness_assessment.md, source-review...,
  README_CASAN5_REFINED.md), casan-next-plans/ and optimize-docs/ (competition/planning
  artifacts — roadmap + design history preserved in git log / commit messages).
- Update all references to the old layout:
  - .gitea/workflows/{ci,harness-ci}.yml, .github/workflows/{ci,deploy}.yml:
    working-directory .; drop AINative_OKR_CASAN5/ prefix; .specify/{tests,scripts}
    -> packages/casan-harness/... (.specify/logs state kept)
  - .claude/launch.json, .gitea/*-runbook.md: path prefixes
  - CLAUDE.md, README.md: docs/input -> apps/okr/domain/input
  - policy-bundle.yaml: 8 policy paths -> packages/casan-harness/...; manifest re-signed
- secrets-scan.sh: fixture excludes -> new package/domain paths.

Full gate from the new root: PASS=64 FAIL=0 SKIP=3.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 13:26:36 +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