#!/usr/bin/env bash # Per-policy joined_analysis driver for a completed B3 sweep. # # For each policy directory under : # - slice engine_state by run_window.json # - run joined_analysis.py to emit interference / hotspot / reuse # / failure breakdown # Then emit b3_policy_comparison.json aggregating one row per policy. set -euo pipefail ROOT="${ROOT:-/home/admin/cpfs/wjh/agentic-kv}" VENV="$ROOT/.venv/bin" SWEEP_DIR="${1:?usage: $0 }" WORKER_MAP="http://127.0.0.1:8000=engine_0,http://127.0.0.1:8001=engine_1,http://127.0.0.1:8002=engine_2,http://127.0.0.1:8003=engine_3,http://127.0.0.1:8004=engine_4,http://127.0.0.1:8005=engine_5,http://127.0.0.1:8006=engine_6,http://127.0.0.1:8007=engine_7" _has_engine_data() { # Return 0 (true) if $1/*.jsonl contains any non-empty file. local dir="$1" [ -d "$dir" ] || return 1 local f for f in "$dir"/engine_*.jsonl; do if [ -s "$f" ]; then return 0; fi done return 1 } for policy_dir in "$SWEEP_DIR"/*/; do policy=$(basename "$policy_dir") case "$policy" in engine_state|logs) continue ;; esac if [ ! -f "$policy_dir/run_window.json" ]; then continue fi echo "=== $policy ===" # Isolated policies write engine_state into their own dir; hot-sweep # policies share the sweep-root engine_state and need slicing. if _has_engine_data "$policy_dir/engine_state"; then echo " using policy-local engine_state ($(du -sh "$policy_dir/engine_state" | cut -f1))" else PYTHONPATH="$ROOT" "$VENV/python" \ "$ROOT/scripts/slice_engine_state.py" \ --input-dir "$SWEEP_DIR/engine_state" \ --output-dir "$policy_dir/engine_state" \ --window "$policy_dir/run_window.json" fi PYTHONPATH="$ROOT" "$VENV/python" \ "$ROOT/analysis/characterization/joined_analysis.py" \ --metrics "$policy_dir/metrics.jsonl" \ --breakdown "$policy_dir/breakdown.json" \ --worker-state "$policy_dir/worker_state.json" \ --engine-state-dir "$policy_dir/engine_state" \ --worker-map "$WORKER_MAP" \ --out-dir "$policy_dir/joined" done # Aggregate per-policy summary "$VENV/python" - <