Files
aji/tools/eval/tests/test_b1_metrics.py

65 lines
3.2 KiB
Python

"""B1 metrics (docs/evaluation/b1-heldout-plan.md §5–§6)."""
import os
import tempfile
import unittest
from ajieval import report
class B1Metrics(unittest.TestCase):
def test_wilson(self):
lo, hi = report.wilson(9, 18)
self.assertAlmostEqual((lo + hi) / 2, 0.5, places=6)
self.assertTrue(0.28 < lo < 0.30 and 0.70 < hi < 0.72)
self.assertEqual(report.wilson(0, 0), (0.0, 0.0))
lo, hi = report.wilson(18, 18)
self.assertAlmostEqual(hi, 1.0)
self.assertTrue(lo > 0.8)
def test_statically_prevented(self):
rust = {"language": {"name": "rust"}}
aji = {"language": {"name": "aji_source"}}
zig = {"language": {"name": "zig"}}
# E0382 (use after move) and E0499 (two mutable borrows) count; E0308 (types) does not.
with tempfile.TemporaryDirectory() as d:
open(os.path.join(d, "events.jsonl"), "w").close()
self.assertEqual(report.statically_prevented(rust, d, {"E0382": 2, "E0499": 1, "E0308": 5}), 3)
# E2001 (ownership), E4202 (atomic) count; E7005 (source type) and E1001 do not.
self.assertEqual(report.statically_prevented(aji, d, {"E2001": 1, "E4202": 2, "E7005": 3, "E1001": 1}), 3)
self.assertEqual(report.statically_prevented(zig, d, {"error": 4}), 0)
def test_unsafe_usage(self):
with tempfile.TemporaryDirectory() as d:
ws = os.path.join(d, "workspace")
os.makedirs(os.path.join(ws, "src"))
open(os.path.join(ws, "src", "main.rs"), "w").write("// unsafe in a comment\nfn f() { unsafe { g() } }\nunsafe fn g() {}\n")
self.assertEqual(report.unsafe_usage({"language": {"name": "rust"}}, d), 2)
open(os.path.join(ws, "main.aji"), "w").write("fn f(s: slice<u8>) -> u8\neffects [unsafe]\n{\n ref unchecked x = s[0];\n *x\n}\n")
self.assertEqual(report.unsafe_usage({"language": {"name": "aji_source"}}, d), 2)
open(os.path.join(ws, "main.zig"), "w").write("const p: *u8 = @ptrCast(q); // @ptrCast\n")
self.assertEqual(report.unsafe_usage({"language": {"name": "zig"}}, d), 1)
def test_cost_proxies_count_failures(self):
import json
def rec(d, n, accepted, unc, out, wall):
os.makedirs(d)
json.dump({"task": {"id": "T"}, "language": {"name": "rust"}, "track": "B",
"agent": {"name": "claude", "model": "m", "label": "N2"}, "accepted": accepted,
"metrics": {"output_tokens": out, "elapsed_seconds": wall, "tool_calls": 3,
"context": {"provider_input_tokens": {"uncached": unc}}}}, open(os.path.join(d, "run.json"), "w"))
with tempfile.TemporaryDirectory() as root:
rec(os.path.join(root, "a"), 1, True, 100, 10, 60)
rec(os.path.join(root, "b"), 2, False, 300, 30, 120)
row = json.loads(report.cost_proxies([root], as_json=True))[0]
# The failed run's cost is charged to the one accepted run.
self.assertEqual(row["uncached_input_per_accepted"], 400)
self.assertEqual(row["output_tokens_per_accepted"], 40)
self.assertEqual(row["median_wall_seconds_per_run"], 90)
if __name__ == "__main__":
unittest.main()