Name loop_claude's modules and words by what each holds, explain the reload, and tie each exit-1 clause of --help to its Stop
This commit is contained in:
@@ -2,11 +2,12 @@ import dataclasses
|
||||
import json
|
||||
import typing
|
||||
|
||||
from loop_claude.claude_code.formats import structured_output
|
||||
from loop_claude.claude_code.entries 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
|
||||
from loop_claude.failure import Failure
|
||||
from loop_claude.stop import Stop
|
||||
|
||||
JUDGE_RULES = (
|
||||
"Decide whether the human must now be interrupted. interrupt=true when any of these appear:\n"
|
||||
@@ -45,7 +46,7 @@ JUDGE_SCHEMA = {
|
||||
}
|
||||
|
||||
|
||||
def judge(root: str, task: str, premises: str, transcript: str, model: str, effects: Effects) -> Verdict | Stop:
|
||||
def judge(root: str, task: str, premises: str, rendered_transcript: str, model: str, 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,11 +57,11 @@ def judge(root: str, task: str, premises: str, transcript: str, model: str, effe
|
||||
"--permission-mode", "bypassPermissions",
|
||||
"--tools", "",
|
||||
]
|
||||
prompt = judge_prompt(task, premises, transcript)
|
||||
return retried("judge", lambda: run_judge(root, args, prompt, transcript, effects), effects)
|
||||
prompt = judge_prompt(task, premises, rendered_transcript)
|
||||
return retried("judge", lambda: run_judge(root, args, prompt, rendered_transcript, effects), effects)
|
||||
|
||||
|
||||
def run_judge(root: str, args: list[str], prompt: str, transcript: str, effects: Effects) -> Verdict | Failure:
|
||||
def run_judge(root: str, args: list[str], prompt: str, rendered_transcript: str, effects: Effects) -> Verdict | Failure:
|
||||
try:
|
||||
proc = effects.claude(args, root, prompt)
|
||||
except OSError as failure:
|
||||
@@ -70,27 +71,27 @@ def run_judge(root: str, args: list[str], prompt: str, transcript: str, effects:
|
||||
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.evidence, transcript):
|
||||
if not grounded(verdict.evidence, rendered_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
|
||||
|
||||
|
||||
def judge_prompt(task: str, premises: str, transcript: str) -> str:
|
||||
def judge_prompt(task: str, premises: str, rendered_transcript: str) -> str:
|
||||
return (
|
||||
f"From README.md:\n{premises}\n\n"
|
||||
f"The task an agent was given:\n{task}\n\n"
|
||||
# The tail, where an interrupt or a finish lands; the session's transcript grows cycle over cycle.
|
||||
f"What the agent did and said, and what the human typed on [human] lines, latest last:\n{transcript[-40000:]}\n\n"
|
||||
f"What the agent did and said, and what the human typed on [human] lines, latest last:\n{rendered_transcript[-40000:]}\n\n"
|
||||
f"{JUDGE_RULES}"
|
||||
)
|
||||
|
||||
|
||||
def verdict_from(output: object) -> Verdict | Failure:
|
||||
if not isinstance(output, dict):
|
||||
return Failure("the judge returned no verdict")
|
||||
return Failure("the judge returned no reply")
|
||||
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]}")
|
||||
return Failure(f"the judge's reply has no {name} of type {JSON_TYPES[kind]}")
|
||||
# An interrupt outranks finished: the human answers before the chunk can close.
|
||||
if output["interrupt"]:
|
||||
if not output["question"].strip():
|
||||
@@ -99,9 +100,9 @@ def verdict_from(output: object) -> Verdict | Failure:
|
||||
return Finished(output["evidence"]) if output["finished"] else Unfinished(output["evidence"])
|
||||
|
||||
|
||||
def grounded(evidence: str, transcript: str) -> bool:
|
||||
def grounded(evidence: str, rendered_transcript: str) -> bool:
|
||||
quoted = plain(evidence)
|
||||
return quoted != "" and quoted in plain(transcript)
|
||||
return quoted != "" and quoted in plain(rendered_transcript)
|
||||
|
||||
|
||||
def plain(text: str) -> str:
|
||||
|
||||
Reference in New Issue
Block a user