返回 CodeWhale
measure-runtime-contract.py
根目录 / scripts / measure-runtime-contract.py
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 subprocess
12 import sys
13
14
15 def run_metric(test_name: str, marker: str) -> dict:
16 cmd = [
17 "cargo",
18 "test",
19 "--locked",
20 "-p",
21 "codewhale-tui",
22 "--bin",
23 "codewhale-tui",
24 test_name,
25 "--",
26 "--ignored",
27 "--nocapture",
28 "--test-threads=1",
29 ]
30 proc = subprocess.run(cmd, text=True, capture_output=True, check=False)
31 sys.stderr.write(proc.stderr)
32 if proc.returncode != 0:
33 sys.stdout.write(proc.stdout)
34 proc.check_returncode()
35
36 combined = proc.stdout.splitlines() + proc.stderr.splitlines()
37 for line in combined:
38 if marker in line:
39 return json.loads(line.split(marker, 1)[1])
40
41 sys.stdout.write(proc.stdout)
42 raise RuntimeError(f"missing {marker} marker")
43
44
45 def main() -> int:
46 tool_metrics = run_metric(
47 "print_mode_tool_catalog_metrics",
48 "TOOL_CATALOG_METRICS ",
49 )
50 prompt_metrics = run_metric(
51 "print_mode_runtime_contract_metrics",
52 "RUNTIME_CONTRACT_METRICS ",
53 )
54 representative_context_metrics = run_metric(
55 "print_representative_runtime_context_metrics",
56 "REPRESENTATIVE_CONTEXT_METRICS ",
57 )
58 skill_discovery_metrics = run_metric(
59 "print_skill_discovery_turn_metrics",
60 "SKILL_DISCOVERY_METRICS ",
61 )
62
63 receipt = {
64 "document_kind": "codewhale.runtime_contract_receipt",
65 "schema_version": 1,
66 "representative_context": representative_context_metrics,
67 "skill_discovery": skill_discovery_metrics,
68 "tool_catalog": tool_metrics,
69 "system_prompt": prompt_metrics,
70 }
71 print(json.dumps(receipt, indent=2, sort_keys=True))
72 return 0
73
74
75 if __name__ == "__main__":
76 raise SystemExit(main())
77
77 lines PYTHON