Python
Keep a cached grade only for the same selection (#45)
Andrew Barnes14fefe7fd33a
diff --git a/harness2/cli/judge.py b/harness2/cli/judge.pyindex ab0edb2..fe18877 100644--- a/harness2/cli/judge.py+++ b/harness2/cli/judge.py@@ -16,8 +16,10 @@Only the run the blind terminal selector chose is scored. A judge failure is recorded asungraded (score=None), never as a zero. Re-running re-judges every selection, but-adapters serve cached judge details and a failed re-judge never replaces an existing-graded row.+adapters serve cached judge details and a failed re-judge never replaces the existing+graded row of the same selection. A task that was re-run (a partial task on resume, say)+has a fresh selection record under the same run id, and a failed judge of its new rollout+leaves it ungraded rather than keeping the replaced rollout's score.python3 -m harness2.cli.judge --bench jb --substrate oc --k 2 --modes parallel@@ -63,8 +65,11 @@ def _load_rows(path: Path) -> list[J.Scored]:def _merge(old: list[J.Scored], new: list[J.Scored]) -> list[J.Scored]:- """Per (task, mode, k): a changed chosen run replaces; for the same run_id the graded- row wins, so a failed re-judge never erases an existing score."""+ """Per (task, mode, k): a changed chosen run or a re-selected task replaces; for the same+ selection the graded row wins, so a failed re-judge never erases an existing score.++ Run ids are fixed labels, so a re-run task writes its new rollout under the old run id;+ `selected_at` is what tells the two apart."""by_key = {(r.task_id, r.mode, r.k): r for r in old}for r in new:key = (r.task_id, r.mode, r.k)@@ -72,6 +77,7 @@ def _merge(old: list[J.Scored], new: list[J.Scored]) -> list[J.Scored]:if (prev is not Noneand prev.run_id == r.run_id+ and prev.selected_at == r.selected_atand prev.gradedand not r.gradedand not r.error.startswith("selection")diff --git a/harness2/core/judge.py b/harness2/core/judge.pyindex e74037b..37ad093 100644--- a/harness2/core/judge.py+++ b/harness2/core/judge.py@@ -42,6 +42,7 @@ class Scored:label: strrun_id: strchosen_is_base: bool+ selected_at: str = ""n_passed: int | None = Nonen_criteria: int | None = Noneall_pass: bool = False@@ -73,9 +74,16 @@ def judge_one(run_dir: Path,*,chosen_is_base: bool = False,+ selected_at: str = "",) -> Scored:s = Scored(- task_id=task_id, mode=mode, k=k, label=label, run_id=run_id, chosen_is_base=chosen_is_base+ task_id=task_id,+ mode=mode,+ k=k,+ label=label,+ run_id=run_id,+ chosen_is_base=chosen_is_base,+ selected_at=selected_at,)try:got = adapter.score(run_dir, task_id)@@ -112,6 +120,7 @@ def judge_selections(label=sel.get("chosen_label", ""),run_id=sel.get("chosen_run_id", ""),chosen_is_base=bool(sel.get("chosen_is_base")),+ selected_at=sel.get("selected_at", ""),error="selection did not complete successfully",))@@ -127,6 +136,7 @@ def judge_selections(sel.get("chosen_run_id", ""),Path(rd),chosen_is_base=bool(sel.get("chosen_is_base")),+ selected_at=sel.get("selected_at", ""),)] = selfor f in as_completed(futs):@@ -142,6 +152,7 @@ def judge_selections(label=sel.get("chosen_label", ""),run_id=sel.get("chosen_run_id", ""),chosen_is_base=bool(sel.get("chosen_is_base")),+ selected_at=sel.get("selected_at", ""),error=f"{type(exc).__name__}: {exc}",))diff --git a/harness2/core/schedule.py b/harness2/core/schedule.pyindex 1be3844..db1f20d 100644--- a/harness2/core/schedule.py+++ b/harness2/core/schedule.py@@ -537,6 +537,7 @@ def _run_one_mode(decisions={stage.label: stage.notes or stage.error for stage in r.stages},)d = sel.to_dict()+ d["selected_at"] = datetime.now(timezone.utc).isoformat()d["chosen_run_dir"] = str(candidates[sel.chosen_label].run_dir if sel.chosen_label in candidates else "")diff --git a/tests/test_workflow.py b/tests/test_workflow.pyindex 2b7ebeb..62c6277 100644--- a/tests/test_workflow.py+++ b/tests/test_workflow.py@@ -224,6 +224,32 @@ class WorkflowTests(unittest.TestCase):self.adapter.score.assert_not_called()self.assertEqual((rows[0].graded, rows[0].n_passed), (False, None))+ def test_failed_rejudge_keeps_grade_only_for_same_selection(self):+ from harness2.cli import judge as judge_cli++ def selections():+ return {s["task_id"]: s for s in judge_cli.collect_selections(ns_dir, ("agg",))}++ self.run_stream(k=1)+ ns_dir = self.adapter.spec.runs_root / "demo-oc-agg1"+ first = selections()+ self.assertTrue(all(sel["selected_at"] for sel in first.values()))+ self.adapter.score = Mock(return_value={"n_passed": 1, "n_criteria": 1})+ graded = judge.judge_selections(self.adapter, list(first.values()))+ self.adapter.score = Mock(side_effect=RuntimeError("judge endpoint unavailable"))+ failed = judge.judge_selections(self.adapter, list(first.values()))+ self.assertTrue(all(r.graded for r in judge_cli._merge(graded, failed)))++ (Path(first["ledger-1"]["chosen_run_dir"]) / "answer.txt").unlink()+ self.run_stream(k=1)+ again = selections()+ self.assertEqual(again["ledger-1"]["chosen_run_id"], first["ledger-1"]["chosen_run_id"])+ self.assertNotEqual(again["ledger-1"]["selected_at"], first["ledger-1"]["selected_at"])+ self.assertEqual(again["ledger-2"]["selected_at"], first["ledger-2"]["selected_at"])+ failed = judge.judge_selections(self.adapter, list(again.values()))+ merged = {r.task_id: r.graded for r in judge_cli._merge(graded, failed)}+ self.assertEqual(merged, {"ledger-1": False, "ledger-2": True})+if __name__ == "__main__":unittest.main()