From f4a552bda69cca13a763020204e57b0cff5abbdb Mon Sep 17 00:00:00 2001 From: Harenome Ranaivoarivony-Razanajato Date: Wed, 25 Nov 2026 16:18:41 +0100 Subject: [PATCH] fix(judge): store a batch's judgements in the workspace that holds the job --- app/ui_tests/test_judge_other_workspace.py | 16 ++++++++++++++++ app/web/judge.py | 16 +++++++++------- app/web/judge_tasks.py | 4 ++-- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/ui_tests/test_judge_other_workspace.py b/app/ui_tests/test_judge_other_workspace.py index 558349fc..127823f6 100644 --- a/app/ui_tests/test_judge_other_workspace.py +++ b/app/ui_tests/test_judge_other_workspace.py @@ -8,6 +8,7 @@ import unittest from contextlib import ExitStack from pathlib import Path +from types import SimpleNamespace as NS from unittest.mock import patch from flask import url_for @@ -28,6 +29,7 @@ def setUp(self): tmp = Path(stack.enter_context(tempfile.TemporaryDirectory(prefix="nullius-judge-ws-"))) shutil.copytree(JOB, tmp / "a" / "job-x") shutil.copytree(JOB, tmp / "b" / "job-y") + (tmp / "b" / "job-y" / TRIAL / "agent" / "trajectory.json").write_text("{}") stack.enter_context(patch.object(web.shared, "WS_HOME", str(tmp / "workspaces"))) stack.enter_context(patch.dict(web.shared.STATE, {"workspace": None, "roots": [], "label": "", "job_roots": []}, clear=True)) self.a = workspace.create(web.shared.WS_HOME, [str(tmp / "a")], "A") @@ -53,6 +55,20 @@ def test_judging_that_trial_writes_into_its_own_workspace(self): self.assertNotIn("judgements", os.listdir(self.a.dir), "a judgement of B's trial was written into A") self.assertIsNotNone(store.load(self.b.dir, "job-y", TRIAL)) + def test_judge_all_and_task_judging_write_into_its_own_workspace(self): + rec = {"questions": Q.VERSION, "answers": {"q": {"choice": "x"}}, "judge": {"model": "m"}} + client = web.app.test_client() + with client.session_transaction() as sess: + sess["csrf"] = "t" + with patch.dict(os.environ, TYPESAFE_API_KEY="ts-fake"), patch.object(web.judge.batch, "ask_states", side_effect=lambda *a, **k: dict(rec)), \ + patch.object(web.judge.trajectory, "load", lambda path: NS(error=None)), patch.object(web.judge, "_trial_ids_and_states", lambda trial, traj: {}), \ + patch.object(web.judge, "_task_instruction", lambda trial: "Say hello."): + for route in ("job", "tasks"): + self.assertEqual(client.post(f"/judge/{route}/job-y?workspace={self.b.name}", data={"csrf": "t"}).status_code, 302) + self.assertNotIn("judgements", os.listdir(self.a.dir), "Judge all on B's job wrote into A") + self.assertIsNotNone(store.load(self.b.dir, "job-y", TRIAL)) + self.assertTrue(any(k.startswith("task_") for k in os.listdir(Path(self.b.dir) / "judgements")), os.listdir(Path(self.b.dir) / "judgements")) + def test_the_judge_forms_post_back_to_that_workspace(self): with web.app.test_request_context(self.url): for endpoint, args in (("judge_trial_route", {"trial_name": TRIAL}), ("judge_job_route", {}), ("judge_tasks_route", {})): diff --git a/app/web/judge.py b/app/web/judge.py index 3247a817..e7984d5f 100644 --- a/app/web/judge.py +++ b/app/web/judge.py @@ -253,16 +253,17 @@ def _trial_ids_and_states(trial, traj) -> dict: return _states_for(trial, traj, ids) -def judge_trial(job, trial, api_key: str, post=None, calibration=None) -> dict: +def judge_trial(job, trial, api_key: str, post=None, calibration=None, d=None) -> dict: """Ask every question the trial page shows, one call per state, and keep the merged record. Returns the record as stored. `calibration`: a - batch's, read once (`store.calibration`); the workspace's when none.""" + batch's, read once (`store.calibration`); the workspace's when none. + `d`: a batch's store, resolved in the request; its threads have none.""" if not trial.has_trajectory: raise client.JudgeError(0, "this trial has no trajectory to judge") traj = trajectory.load(trial.trajectory_path) if traj.error: raise client.JudgeError(0, "the trajectory could not be read: " + traj.error) - return batch.ask_and_store(ws_dir(), job.job_id, trial.trial_name, _trial_ids_and_states(trial, traj), api_key, post=post, + return batch.ask_and_store(ws_dir() if d is None else d, job.job_id, trial.trial_name, _trial_ids_and_states(trial, traj), api_key, post=post, calibration=calibration, started=started_of(trial)) @@ -307,7 +308,7 @@ def judge_job_route(job_id): todo = [t for t in job.trials if t.has_trajectory and t.trial_name not in judged] _guard() k, cal = key(lane), batch_calibration() - done, failed = run_batch(todo, lambda t: judge_trial(job, t, k, calibration=cal), lambda t: t.trial_name) + done, failed = run_batch(todo, lambda t: judge_trial(job, t, k, calibration=cal, d=d), lambda t: t.trial_name) from flask import flash flash(f"{'LLM judge' if lane == 'llm' else 'Decision model'}: judged {done} of {len(todo)} trial(s)." + (" Failed: " + "; ".join(failed[:3]) if failed else ""), "error" if failed else "") return redirect(url_for("job", job_id=job_id) + "#judgement") @@ -651,11 +652,12 @@ def task_summary(job) -> dict: return {"tasks": len(tasks), "judged": judged, "unjudged": len(tasks_todo(job)), "keyed": bool(key())} -def judge_task(job, trial, api_key: str, post=None, calibration=None) -> dict | None: +def judge_task(job, trial, api_key: str, post=None, calibration=None, d=None) -> dict | None: """Score one task's instruction over the `task` state alone, stored under - the dataset's task slot and the instruction's digest - never under the trial.""" + the dataset's task slot and the instruction's digest - never under the trial. + `d` as for `judge_trial`.""" instruction = _task_instruction(trial) if not instruction: raise client.JudgeError(0, f"no instruction can be read for {trial.task_base}") states = batch.states_for(Q.group_ids("task"), lambda kind: judge_state.build(kind, None, instruction=instruction)) - return batch.ask_and_store(ws_dir(), task_key(job), task_slot(trial), states, api_key, post=post, calibration=calibration) + return batch.ask_and_store(ws_dir() if d is None else d, task_key(job), task_slot(trial), states, api_key, post=post, calibration=calibration) diff --git a/app/web/judge_tasks.py b/app/web/judge_tasks.py index 13e26e9c..acee2672 100644 --- a/app/web/judge_tasks.py +++ b/app/web/judge_tasks.py @@ -19,8 +19,8 @@ def judge_tasks_route(job_id): abort(404) todo = judge.tasks_todo(job) judge._guard() - k, cal = judge.key(), judge.batch_calibration() - done, failed = judge.run_batch(todo, lambda t: judge.judge_task(job, t, k, calibration=cal), lambda t: t.task_base) + k, cal, d = judge.key(), judge.batch_calibration(), judge.ws_dir() + done, failed = judge.run_batch(todo, lambda t: judge.judge_task(job, t, k, calibration=cal, d=d), lambda t: t.task_base) flash(f"Judged {done} of {len(todo)} task instruction(s)." + (" Failed: " + "; ".join(failed[:3]) if failed else ""), "error" if failed else "") return redirect(url_for("job", job_id=job_id) + "#task-quality")