Name the loop's states with one transition function, and inject its side effects
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import dataclasses
|
||||
import json
|
||||
import subprocess
|
||||
import typing
|
||||
|
||||
from loop_claude.claude_code.formats import structured_output
|
||||
from loop_claude.cycle.retry import retried
|
||||
from loop_claude.cycle.verdicts import Finished, Interrupt, Unfinished, Verdict
|
||||
from loop_claude.effects import Effects
|
||||
from loop_claude.outcome import Failure, Stop
|
||||
|
||||
JUDGE_RULES = (
|
||||
@@ -27,24 +28,25 @@ JUDGE_RULES = (
|
||||
"not yet run, CI not yet reported."
|
||||
)
|
||||
|
||||
# The reply's shape, which the schema below hands claude; verdict_from reads it into a Verdict.
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Verdict:
|
||||
class Reply:
|
||||
evidence: str = dataclasses.field(metadata={"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."})
|
||||
finished: bool
|
||||
interrupt: bool
|
||||
question: str = dataclasses.field(metadata={"description": "The single question to put to the human. Empty when interrupt is false."})
|
||||
|
||||
|
||||
VERDICT_TYPES = typing.get_type_hints(Verdict)
|
||||
REPLY_TYPES = typing.get_type_hints(Reply)
|
||||
JSON_TYPES = {bool: "boolean", str: "string"}
|
||||
JUDGE_SCHEMA = {
|
||||
"properties": {field.name: {**field.metadata, "type": JSON_TYPES[VERDICT_TYPES[field.name]]} for field in dataclasses.fields(Verdict)},
|
||||
"required": list(VERDICT_TYPES),
|
||||
"properties": {field.name: {**field.metadata, "type": JSON_TYPES[REPLY_TYPES[field.name]]} for field in dataclasses.fields(Reply)},
|
||||
"required": list(REPLY_TYPES),
|
||||
"type": "object",
|
||||
}
|
||||
|
||||
|
||||
def judge(root, task, premises, transcript, model) -> Verdict | Stop:
|
||||
def judge(root, task, premises, transcript, model, effects: Effects) -> Verdict | Stop:
|
||||
# No tools, so the verdict rests on the transcript grounded() checks it against; no persistence, so its runs stay out of the session picker.
|
||||
args = [
|
||||
"-p",
|
||||
@@ -56,12 +58,12 @@ def judge(root, task, premises, transcript, model) -> Verdict | Stop:
|
||||
"--tools", "",
|
||||
]
|
||||
prompt = judge_prompt(task, premises, transcript)
|
||||
return retried("judge", lambda: run_judge(root, args, prompt, transcript))
|
||||
return retried("judge", lambda: run_judge(root, args, prompt, transcript, effects), effects)
|
||||
|
||||
|
||||
def run_judge(root, args, prompt, transcript) -> Verdict | Failure:
|
||||
def run_judge(root, args, prompt, transcript, effects: Effects) -> Verdict | Failure:
|
||||
try:
|
||||
proc = subprocess.run(["claude", *args], capture_output=True, cwd=root, input=prompt, text=True)
|
||||
proc = effects.run(["claude", *args], capture_output=True, cwd=root, input=prompt, text=True)
|
||||
except OSError as failure:
|
||||
return Failure(f"could not start claude: {failure.strerror}")
|
||||
if proc.returncode != 0:
|
||||
@@ -69,8 +71,8 @@ def run_judge(root, args, prompt, transcript) -> Verdict | Failure:
|
||||
verdict = verdict_from(structured_output(proc.stdout))
|
||||
if isinstance(verdict, Failure):
|
||||
return Failure(f"{verdict.reason}; it replied: {proc.stdout.strip() or 'nothing'}")
|
||||
if not grounded(verdict, transcript):
|
||||
return Failure(f"the judge quoted {verdict.evidence!r}, nothing in the transcript" + (f", for the question: {verdict.question}" if verdict.interrupt else ""))
|
||||
if not grounded(verdict.evidence, transcript):
|
||||
return Failure(f"the judge quoted {verdict.evidence!r}, nothing in the transcript" + (f", for the question: {verdict.question}" if isinstance(verdict, Interrupt) else ""))
|
||||
return verdict
|
||||
|
||||
|
||||
@@ -87,17 +89,20 @@ def judge_prompt(task, premises, transcript) -> str:
|
||||
def verdict_from(output) -> Verdict | Failure:
|
||||
if not isinstance(output, dict):
|
||||
return Failure("the judge returned no verdict")
|
||||
for name, kind in VERDICT_TYPES.items():
|
||||
for name, kind in REPLY_TYPES.items():
|
||||
if not isinstance(output.get(name), kind):
|
||||
return Failure(f"the judge's verdict has no {name} of type {JSON_TYPES[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 VERDICT_TYPES})
|
||||
# An interrupt outranks finished: the human answers before the chunk can close.
|
||||
if output["interrupt"]:
|
||||
if not output["question"].strip():
|
||||
return Failure("the judge interrupted without a question")
|
||||
return Interrupt(output["evidence"], output["question"])
|
||||
return Finished(output["evidence"]) if output["finished"] else Unfinished(output["evidence"])
|
||||
|
||||
|
||||
def grounded(verdict, transcript) -> bool:
|
||||
evidence = plain(verdict.evidence)
|
||||
return evidence != "" and evidence in plain(transcript)
|
||||
def grounded(evidence, transcript) -> bool:
|
||||
quoted = plain(evidence)
|
||||
return quoted != "" and quoted in plain(transcript)
|
||||
|
||||
|
||||
def plain(text) -> str:
|
||||
|
||||
Reference in New Issue
Block a user