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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user