Return a typed verdict from the judge
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import dataclasses
|
||||
import itertools
|
||||
import json
|
||||
import subprocess
|
||||
@@ -26,6 +27,14 @@ JUDGE_RULES = (
|
||||
"not yet run, CI not yet reported."
|
||||
)
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Verdict:
|
||||
evidence: str
|
||||
finished: bool
|
||||
interrupt: bool
|
||||
question: str
|
||||
|
||||
|
||||
JUDGE_SCHEMA = {
|
||||
"properties": {
|
||||
"evidence": {"description": "A verbatim quote of the transcript line the verdict rests on: what triggers the interrupt, else what shows the chunk finished or the work remaining.", "type": "string"},
|
||||
@@ -54,10 +63,8 @@ def judge(root, task, premises, transcript, model):
|
||||
verdict = run_judge(root, args, prompt)
|
||||
if isinstance(verdict, Failure):
|
||||
reason = verdict.reason
|
||||
elif reason := incomplete(verdict):
|
||||
pass
|
||||
elif not grounded(verdict, transcript):
|
||||
reason = f"the judge quoted {verdict['evidence']!r}, nothing in the transcript" + (f", for the question: {verdict['question']}" if verdict["interrupt"] else "")
|
||||
reason = f"the judge quoted {verdict.evidence!r}, nothing in the transcript" + (f", for the question: {verdict.question}" if verdict.interrupt else "")
|
||||
else:
|
||||
return verdict
|
||||
if stop := stop_or_sleep_a_minute("judge", failures, reason):
|
||||
@@ -71,7 +78,7 @@ def run_judge(root, args, prompt):
|
||||
return Failure(f"could not start claude: {failure.strerror}")
|
||||
if proc.returncode != 0:
|
||||
return Failure(proc.stderr.strip() or f"the judge exited {proc.returncode}")
|
||||
return structured_output(proc.stdout)
|
||||
return verdict_from(structured_output(proc.stdout))
|
||||
|
||||
|
||||
def judge_prompt(task, premises, transcript):
|
||||
@@ -84,18 +91,17 @@ def judge_prompt(task, premises, transcript):
|
||||
)
|
||||
|
||||
|
||||
def incomplete(verdict):
|
||||
if not isinstance(verdict, dict):
|
||||
return "the judge returned no verdict"
|
||||
def verdict_from(output):
|
||||
if not isinstance(output, dict):
|
||||
return Failure("the judge returned no verdict")
|
||||
for name in JUDGE_SCHEMA["required"]:
|
||||
kind = JUDGE_SCHEMA["properties"][name]["type"]
|
||||
if not isinstance(verdict.get(name), bool if kind == "boolean" else str):
|
||||
return f"the judge's verdict has no {name} of type {kind}"
|
||||
if verdict["interrupt"] and not verdict["question"].strip():
|
||||
return "the judge interrupted without a question"
|
||||
return None
|
||||
if not isinstance(output.get(name), bool if kind == "boolean" else str):
|
||||
return Failure(f"the judge's verdict has no {name} of type {kind}")
|
||||
if output["interrupt"] and not output["question"].strip():
|
||||
return Failure("the judge interrupted without a question")
|
||||
return Verdict(**{name: output[name] for name in JUDGE_SCHEMA["required"]})
|
||||
|
||||
|
||||
def grounded(verdict, transcript):
|
||||
evidence = verdict["evidence"]
|
||||
return evidence.strip() != "" and evidence in transcript
|
||||
return verdict.evidence.strip() != "" and verdict.evidence in transcript
|
||||
|
||||
@@ -18,7 +18,7 @@ def run_cycle(root, project, task, premises, models, paths):
|
||||
verdict = judge_through_interrupts(root, project, task, premises, models, session, path, paths)
|
||||
if isinstance(verdict, Stop):
|
||||
return verdict
|
||||
if verdict["finished"]:
|
||||
if verdict.finished:
|
||||
if failure := archive(project, path, f"Task: {task}\n\nVerdict: finished", paths):
|
||||
return Stop(1, failure.reason)
|
||||
delete_session(session)
|
||||
@@ -32,10 +32,10 @@ def judge_through_interrupts(root, project, task, premises, models, session, pat
|
||||
if isinstance(entries, Failure):
|
||||
return Stop(1, entries.reason)
|
||||
verdict = judge(root, task, premises, render(entries), models.judge)
|
||||
if isinstance(verdict, Stop) or not verdict["interrupt"]:
|
||||
if isinstance(verdict, Stop) or not verdict.interrupt:
|
||||
return verdict
|
||||
if failure := archive(project, path, f"Task: {task}\n\nVerdict: interrupt\n\n{verdict['question']}", paths):
|
||||
if failure := archive(project, path, f"Task: {task}\n\nVerdict: interrupt\n\n{verdict.question}", paths):
|
||||
return Stop(1, failure.reason)
|
||||
if stop := interrupt(root, session.id, models.worker, path, verdict["question"], paths):
|
||||
if stop := interrupt(root, session.id, models.worker, path, verdict.question, paths):
|
||||
return stop
|
||||
|
||||
|
||||
+11
-9
@@ -88,19 +88,21 @@ class Parse(unittest.TestCase):
|
||||
class Grounded(unittest.TestCase):
|
||||
def test_every_verdict_must_quote_the_transcript(self):
|
||||
for interrupting in (False, True):
|
||||
self.assertTrue(judge.grounded({"interrupt": interrupting, "evidence": "Needs you: x"}, "so.\n\nNeeds you: x\n"))
|
||||
self.assertFalse(judge.grounded({"interrupt": interrupting, "evidence": "invented"}, "the transcript"))
|
||||
self.assertFalse(judge.grounded({"interrupt": interrupting, "evidence": ""}, "the transcript"))
|
||||
self.assertFalse(judge.grounded({"interrupt": interrupting, "evidence": " "}, "the transcript"))
|
||||
def verdict(evidence):
|
||||
return judge.Verdict(evidence=evidence, finished=False, interrupt=interrupting, question="q")
|
||||
self.assertTrue(judge.grounded(verdict("Needs you: x"), "so.\n\nNeeds you: x\n"))
|
||||
self.assertFalse(judge.grounded(verdict("invented"), "the transcript"))
|
||||
self.assertFalse(judge.grounded(verdict(""), "the transcript"))
|
||||
self.assertFalse(judge.grounded(verdict(" "), "the transcript"))
|
||||
|
||||
|
||||
class Verdict(unittest.TestCase):
|
||||
def test_needs_every_field_the_schema_requires(self):
|
||||
self.assertIsNone(judge.incomplete({"interrupt": False, "evidence": "", "finished": True, "question": ""}))
|
||||
self.assertEqual(judge.incomplete({"interrupt": "no", "evidence": "", "finished": True, "question": ""}), "the judge's verdict has no interrupt of type boolean")
|
||||
self.assertEqual(judge.incomplete({"interrupt": False, "evidence": None, "finished": True, "question": ""}), "the judge's verdict has no evidence of type string")
|
||||
self.assertEqual(judge.incomplete({"interrupt": True, "evidence": "x", "finished": False, "question": " "}), "the judge interrupted without a question")
|
||||
self.assertEqual(judge.incomplete(None), "the judge returned no verdict")
|
||||
self.assertEqual(judge.verdict_from({"interrupt": False, "evidence": "", "finished": True, "question": ""}), judge.Verdict(evidence="", finished=True, interrupt=False, question=""))
|
||||
self.assertEqual(judge.verdict_from({"interrupt": "no", "evidence": "", "finished": True, "question": ""}), Failure("the judge's verdict has no interrupt of type boolean"))
|
||||
self.assertEqual(judge.verdict_from({"interrupt": False, "evidence": None, "finished": True, "question": ""}), Failure("the judge's verdict has no evidence of type string"))
|
||||
self.assertEqual(judge.verdict_from({"interrupt": True, "evidence": "x", "finished": False, "question": " "}), Failure("the judge interrupted without a question"))
|
||||
self.assertEqual(judge.verdict_from(None), Failure("the judge returned no verdict"))
|
||||
|
||||
|
||||
class LoopState(unittest.TestCase):
|
||||
|
||||
@@ -8,7 +8,6 @@ The rebuild lifts every dimension, one chunk per item, until the last item opens
|
||||
|
||||
The places the panel on `rebuild` at 7f9de5c named (`docs/decisions.md`), capping dimension first:
|
||||
|
||||
- **Return a typed verdict from `judge`, so `cycle.py` reads its fields and only `judge.py` knows the schema's keys.** Three seats: `judge_through_interrupts` indexes a dict that only `incomplete` checked.
|
||||
- **Make `mark_trusted` return the updated `~/.claude.json`, so `trust` writes what it was handed and `formats.py` mutates nothing.** Three seats: `trust_records` `setdefault`s into the config and `trust` writes `data` changed through an alias.
|
||||
- **Make `decode_state` check the fields' types without relying on the annotations being runtime types.** Two seats: `from __future__ import annotations` in `main.py` would fail every reload.
|
||||
- **State once, in `outcome.py`, that a `Failure` is retried and a `Stop` exits.** Three seats inferred it from `run_loop`.
|
||||
|
||||
Reference in New Issue
Block a user