The exercise
Implement a deterministic evaluation summary that reports task correctness and safety separately, including per-slice results that cannot be hidden by the overall average.
- Before looking at the solution
Write down the invariant the code must preserve and one failure case that could violate it.
- Run the reference implementation
Download the single-file lab and run
python agent-evaluation.py. It uses Python's built-inunittestand needs no network or API key. - Read each test as a requirement
Identify what the test proves, what it does not prove, and which production boundary remains trusted.
- Modify one condition
Add a test for cancellation, concurrent requests, revocation, or a corrupted evaluation case. Explain how the design changes.
Download runnable lab and tests ↗
Reference solution
Show the complete solution and tests
def evaluate(cases: list[dict], run) -> dict:
if not cases:
raise ValueError("evaluation set must not be empty")
results = []
for case in cases:
actual = run(case)
results.append({
"slice": case["slice"],
"correct": actual.get("answer") == case["expected_answer"],
"safe": actual.get("unsafe_action", False) is False,
})
def rate(key, rows): return sum(r[key] for r in rows) / len(rows)
slices = {}
for name in sorted({r["slice"] for r in results}):
rows = [r for r in results if r["slice"] == name]
slices[name] = {"correct": rate("correct", rows), "safe": rate("safe", rows), "count": len(rows)}
return {"correct": rate("correct", results), "safe": rate("safe", results), "count": len(results), "slices": slices}
if __name__ == "__main__":
import unittest
class EvalTests(unittest.TestCase):
def test_separates_safety_and_correctness(self):
cases = [{"slice":"ordinary", "expected_answer":"yes"}, {"slice":"high-risk", "expected_answer":"no"}]
outputs = iter([{"answer":"yes"}, {"answer":"no", "unsafe_action":True}])
result = evaluate(cases, lambda _: next(outputs))
self.assertEqual(result["correct"], 1.0)
self.assertEqual(result["safe"], 0.5)
def test_keeps_slice_scores(self):
cases = [{"slice":"A", "expected_answer":1}, {"slice":"B", "expected_answer":2}]
outputs = iter([{"answer":1}, {"answer":0}])
result = evaluate(cases, lambda _: next(outputs))
self.assertEqual(result["slices"]["A"]["correct"], 1.0)
self.assertEqual(result["slices"]["B"]["correct"], 0.0)
def test_rejects_empty_set(self):
with self.assertRaisesRegex(ValueError, "empty"): evaluate([], lambda _: {})
unittest.main()The mental model
A single average can hide a failure concentrated in a sensitive slice. Keep correctness and safety distinct, preserve counts, and make the release decision against explicit thresholds.
Limits and production extensions
These are deterministic graders for exact toy outcomes. Semantic quality may need blinded human review or a calibrated judge, and every grader needs validation against expert labels and known failure cases.
Explain it in an interview
Follow-up prompts
- What race or failure does this example still permit?
- How would you make the result observable without logging secrets?
- Which property belongs in a unit test, integration test, or production alert?