|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Block unsupported AI-agent success claims before they become evidence.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any |
| 10 | + |
| 11 | + |
| 12 | +SUCCESS_TOKENS = { |
| 13 | + "done", |
| 14 | + "ready", |
| 15 | + "complete", |
| 16 | + "completed", |
| 17 | + "success", |
| 18 | + "succeeded", |
| 19 | + "pass", |
| 20 | + "passed", |
| 21 | + "local_validated", |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +def _as_list(value: Any) -> list[Any]: |
| 26 | + if value is None: |
| 27 | + return [] |
| 28 | + if isinstance(value, list): |
| 29 | + return value |
| 30 | + return [value] |
| 31 | + |
| 32 | + |
| 33 | +def _text_tokens(case: dict[str, Any]) -> set[str]: |
| 34 | + values = _as_list(case.get("success_claims")) |
| 35 | + if case.get("agent_claim"): |
| 36 | + values.append(case["agent_claim"]) |
| 37 | + return {str(value).lower().replace("-", "_") for value in values} |
| 38 | + |
| 39 | + |
| 40 | +def _is_success_claim(case: dict[str, Any]) -> bool: |
| 41 | + tokens = _text_tokens(case) |
| 42 | + return any(token in SUCCESS_TOKENS for token in tokens) or any( |
| 43 | + word in token for token in tokens for word in SUCCESS_TOKENS |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def _has_passing_evidence(case: dict[str, Any]) -> bool: |
| 48 | + for item in _as_list(case.get("evidence")): |
| 49 | + if not isinstance(item, dict): |
| 50 | + continue |
| 51 | + status = str(item.get("status", "")).upper() |
| 52 | + has_pointer = any(item.get(key) for key in ("command", "file", "artifact", "url", "value")) |
| 53 | + if status == "PASS" and item.get("type") and has_pointer: |
| 54 | + return True |
| 55 | + return False |
| 56 | + |
| 57 | + |
| 58 | +def evaluate_case(case: dict[str, Any]) -> tuple[str, list[str]]: |
| 59 | + reasons: list[str] = [] |
| 60 | + |
| 61 | + if _is_success_claim(case) and not _has_passing_evidence(case): |
| 62 | + reasons.append("missing_passing_evidence") |
| 63 | + |
| 64 | + if not _as_list(case.get("cannot_claim")): |
| 65 | + reasons.append("missing_cannot_claim") |
| 66 | + |
| 67 | + if reasons: |
| 68 | + return "BLOCKED", reasons |
| 69 | + return "PASS", ["evidence_and_boundaries_present"] |
| 70 | + |
| 71 | + |
| 72 | +def load_case(path: Path) -> dict[str, Any]: |
| 73 | + return json.loads(path.read_text(encoding="utf-8")) |
| 74 | + |
| 75 | + |
| 76 | +def format_case_result(path: Path, actual: str, expected: str | None, reasons: list[str]) -> str: |
| 77 | + case_id = load_case(path).get("case_id", path.stem) |
| 78 | + fields = [ |
| 79 | + f"case_id={case_id}", |
| 80 | + f"actual={actual}", |
| 81 | + f"reasons={','.join(reasons)}", |
| 82 | + ] |
| 83 | + if expected: |
| 84 | + fields.append(f"expected={expected}") |
| 85 | + return " ".join(fields) |
| 86 | + |
| 87 | + |
| 88 | +def run_case(path: Path) -> int: |
| 89 | + case = load_case(path) |
| 90 | + actual, reasons = evaluate_case(case) |
| 91 | + print(format_case_result(path, actual, case.get("expected_verdict"), reasons)) |
| 92 | + return 0 if actual == "PASS" else 1 |
| 93 | + |
| 94 | + |
| 95 | +def run_self_test(fixtures_dir: Path) -> int: |
| 96 | + paths = sorted(fixtures_dir.glob("*.json")) |
| 97 | + failures: list[str] = [] |
| 98 | + |
| 99 | + for path in paths: |
| 100 | + case = load_case(path) |
| 101 | + actual, reasons = evaluate_case(case) |
| 102 | + expected = str(case.get("expected_verdict", "")).upper() |
| 103 | + print(format_case_result(path, actual, expected, reasons)) |
| 104 | + if actual != expected: |
| 105 | + failures.append(path.name) |
| 106 | + |
| 107 | + if failures: |
| 108 | + print(f"self_test=FAIL cases={len(paths)} failures={','.join(failures)}") |
| 109 | + return 1 |
| 110 | + |
| 111 | + print(f"self_test=PASS cases={len(paths)}") |
| 112 | + return 0 |
| 113 | + |
| 114 | + |
| 115 | +def parse_args() -> argparse.Namespace: |
| 116 | + parser = argparse.ArgumentParser(description="Check false-pass evidence gates.") |
| 117 | + group = parser.add_mutually_exclusive_group(required=True) |
| 118 | + group.add_argument("--case", type=Path, help="Evaluate one JSON case file.") |
| 119 | + group.add_argument("--self-test", type=Path, help="Evaluate all JSON fixtures in a directory.") |
| 120 | + return parser.parse_args() |
| 121 | + |
| 122 | + |
| 123 | +def main() -> int: |
| 124 | + args = parse_args() |
| 125 | + if args.case: |
| 126 | + return run_case(args.case) |
| 127 | + return run_self_test(args.self_test) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + raise SystemExit(main()) |
0 commit comments