| 1 | #!/usr/bin/env python3 |
| 2 | """Measure the provider-free model-facing runtime contract. |
| 3 | |
| 4 | Combines the serialized tool catalog and the rendered system prompt into a |
| 5 | single reproducible receipt. No API keys or live providers are required. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import json |
| 11 | import re |
| 12 | import subprocess |
| 13 | import sys |
| 14 | |
| 15 | |
| 16 | METRIC_TEST_MODULE = "core::engine::tests" |
| 17 | |
| 18 | |
| 19 | def metric_test_name(test_name: str) -> str: |
| 20 | return f"{METRIC_TEST_MODULE}::{test_name}" |
| 21 | |
| 22 | |
| 23 | def metric_command(test_name: str) -> list[str]: |
| 24 | exact_test_name = metric_test_name(test_name) |
| 25 | return [ |
| 26 | "cargo", |
| 27 | "test", |
| 28 | "--locked", |
| 29 | "-p", |
| 30 | "codewhale-tui", |
| 31 | "--lib", |
| 32 | exact_test_name, |
| 33 | "--", |
| 34 | "--ignored", |
| 35 | "--exact", |
| 36 | "--nocapture", |
| 37 | "--test-threads=1", |
| 38 | ] |
| 39 | |
| 40 | |
| 41 | def run_metric(test_name: str, marker: str) -> dict: |
| 42 | cmd = metric_command(test_name) |
| 43 | proc = subprocess.run(cmd, text=True, capture_output=True, check=False) |
| 44 | sys.stderr.write(proc.stderr) |
| 45 | if proc.returncode != 0: |
| 46 | sys.stdout.write(proc.stdout) |
| 47 | proc.check_returncode() |
| 48 | |
| 49 | combined = proc.stdout.splitlines() + proc.stderr.splitlines() |
| 50 | if re.search(r"\brunning\s+0\s+tests?\b", "\n".join(combined)): |
| 51 | sys.stdout.write(proc.stdout) |
| 52 | raise RuntimeError( |
| 53 | f"exact library metric test {metric_test_name(test_name)} ran zero tests" |
| 54 | ) |
| 55 | for line in combined: |
| 56 | if marker in line: |
| 57 | return json.loads(line.split(marker, 1)[1]) |
| 58 | |
| 59 | sys.stdout.write(proc.stdout) |
| 60 | raise RuntimeError( |
| 61 | f"missing {marker.rstrip()} marker from exact library metric test " |
| 62 | f"{metric_test_name(test_name)}" |
| 63 | ) |
| 64 | |
| 65 | |
| 66 | def main() -> int: |
| 67 | tool_metrics = run_metric( |
| 68 | "print_mode_tool_catalog_metrics", |
| 69 | "TOOL_CATALOG_METRICS ", |
| 70 | ) |
| 71 | prompt_metrics = run_metric( |
| 72 | "print_mode_runtime_contract_metrics", |
| 73 | "RUNTIME_CONTRACT_METRICS ", |
| 74 | ) |
| 75 | representative_context_metrics = run_metric( |
| 76 | "print_representative_runtime_context_metrics", |
| 77 | "REPRESENTATIVE_CONTEXT_METRICS ", |
| 78 | ) |
| 79 | skill_discovery_metrics = run_metric( |
| 80 | "print_skill_discovery_turn_metrics", |
| 81 | "SKILL_DISCOVERY_METRICS ", |
| 82 | ) |
| 83 | |
| 84 | receipt = { |
| 85 | "document_kind": "codewhale.runtime_contract_receipt", |
| 86 | "schema_version": 1, |
| 87 | "representative_context": representative_context_metrics, |
| 88 | "skill_discovery": skill_discovery_metrics, |
| 89 | "tool_catalog": tool_metrics, |
| 90 | "system_prompt": prompt_metrics, |
| 91 | } |
| 92 | print(json.dumps(receipt, indent=2, sort_keys=True)) |
| 93 | return 0 |
| 94 | |
| 95 | |
| 96 | if __name__ == "__main__": |
| 97 | raise SystemExit(main()) |
| 98 |