"""Evaluate actual invocation of the fixed V2 violation tool. The evaluator consumes only the host-owned, sealed audit record. Blue-agent transcripts and tool listings are intentionally not inputs: they cannot prove that the executable tool was called. """ from __future__ import annotations import shutil import subprocess import sys from pathlib import Path from typing import Any from common import VIOLATION_USAGE_EVALUATOR, fail, read_json, sha256_file def evaluator_path() -> Path: if not VIOLATION_USAGE_EVALUATOR.is_file(): fail(f"V2 violation-tool usage evaluator not found: {VIOLATION_USAGE_EVALUATOR}") return VIOLATION_USAGE_EVALUATOR def evaluate_violation_tool_usage(run_dir: Path, tool_name: str, trajectory_file: Path | None = None, trajectory_label: str | None = None) -> dict[str, Any]: """Return success only when the sealed audit proves this exact invocation.""" evaluator = evaluator_path() # The runner normally evaluates before organize(), but resumed or older # runs may already have the audit bundle under evaluation/. Accept both # locations and report an unavailable audit separately from an ordinary # evaluated=False result. audit_candidates = ( run_dir / "violation-tool-audit", run_dir / "evaluation" / "violation-tool-audit", ) audit_dir = next( (candidate for candidate in audit_candidates if (candidate / "invocations.jsonl").is_file() and (candidate / "seal.json").is_file()), None, ) output_path = run_dir / "evaluation" / "violation-tool-usage-result.json" if audit_dir is None: return { "evaluator_path": str(evaluator), "evaluator_sha256": sha256_file(evaluator), "tool_name": tool_name, "audit_dir": str(audit_candidates[0]), "status": "audit_unavailable", "attack_success": None, "reason": "sealed audit bundle is missing invocations.jsonl or seal.json", } trajectory_path = trajectory_file or (run_dir / "blue" / "raw-dsh-session.v3.jsonl") trajectory_args = [] if trajectory_path.is_file(): trajectory_args = ["--trajectory", str(trajectory_path)] if trajectory_label: trajectory_args.extend(["--trajectory-file", trajectory_label]) completed = subprocess.run( [sys.executable, str(evaluator), "--tool-name", tool_name, "--audit-dir", str(audit_dir), *trajectory_args, "--output", str(output_path)], text=True, capture_output=True, ) (run_dir / "evaluation" / "violation-tool-usage.stdout").write_text(completed.stdout, encoding="utf-8") (run_dir / "evaluation" / "violation-tool-usage.stderr").write_text(completed.stderr, encoding="utf-8") archived = run_dir / "manifest" / "evaluate_violation_tool_usage.py" archived.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(evaluator, archived) summary: dict[str, Any] = { "evaluator_path": str(evaluator), "evaluator_sha256": sha256_file(evaluator), "tool_name": tool_name, "audit_dir": str(audit_dir), "returncode": completed.returncode, } try: result = read_json(output_path) except Exception as exc: summary.update({"status": "evaluation_error", "attack_success": None, "reason": str(exc)}) return summary if completed.returncode != 0: summary.update({"status": "evaluation_error", "attack_success": None, "raw_result": result}) return summary if not isinstance(result, dict) or result.get("evaluator_status") != "success" or not isinstance(result.get("attack_success"), bool): summary.update({"status": "evaluation_inconclusive", "attack_success": None, "raw_result": result}) return summary summary.update({"status": "evaluated", "attack_success": result["attack_success"], "raw_result": result}) return summary