Answer the third round: show the judge loop-claude's prompts, one records answer per cycle through an interrupt, name the answer's two sources apart

This commit is contained in:
2026-10-03 00:54:16 +02:00
parent 160b1dae5f
commit 2f46a13e5d
10 changed files with 53 additions and 38 deletions
+7
View File
@@ -65,6 +65,13 @@ def human_text(entry: dict) -> str | None:
return text if isinstance(text, str) else "".join(part_text(part) or "" for part in parts(entry))
# {"type": "user", "promptSource": "sdk", "message": {"content": "Continue the task; the previous run stopped before it was finished."}}
def loop_text(entry: dict) -> str | None:
"""What a -p prompt of loop-claude's told the agent."""
text = content(entry)
return text if entry.get("type") == "user" and entry.get("promptSource") == "sdk" and isinstance(text, str) else None
# What the human types while claude works, written only as this entry:
# {"type": "attachment", "attachment": {"type": "queued_command", "commandMode": "prompt", "origin": {"kind": "human"}, "prompt": "B"}}
def queued_human_text(entry: dict) -> str | None:
+3 -1
View File
@@ -1,6 +1,6 @@
import json
from loop_claude.claude_code.entries import assistant_parts, human_text, parse_entry, part_text, tool_call
from loop_claude.claude_code.entries import assistant_parts, human_text, loop_text, parse_entry, part_text, tool_call
from loop_claude.failure import Failure
JUDGED_TOOL_INPUT = 400
@@ -30,6 +30,8 @@ def render(entries: list[dict]) -> str:
lines += [line for part in assistant_parts(entry) if (line := part_line(part, JUDGED_TOOL_INPUT))]
if (text := human_text(entry)) is not None:
lines.append("[human] " + text)
elif (text := loop_text(entry)) is not None:
lines.append("[loop] " + text)
return "\n\n".join(lines)
+10 -8
View File
@@ -4,13 +4,13 @@ import typing
from loop_claude.claude_code.entries import hit_limit, limit_said, parse_entry, structured_output
from loop_claude.cycle.retry import retried
from loop_claude.cycle.verdicts import Finished, Interrupt, Recorded, Unfinished, Verdict
from loop_claude.cycle.verdicts import AnsweredFromRecords, Finished, Interrupt, Unfinished, Verdict
from loop_claude.effects import Effects
from loop_claude.failure import Failure
from loop_claude.limited import Limited
from loop_claude.stop import Stop
# About 100,000 tokens, leaving a 200,000-token context room for the rest of the prompt; the largest session rendered here was 364,000 characters on 2026-10-03.
# About 100,000 tokens, half a 200,000-token context; the largest session rendered here was 364,000 characters on 2026-10-03.
JUDGED_TRANSCRIPT_TAIL = 400000
JUDGE_RULES = (
@@ -27,7 +27,9 @@ JUDGE_RULES = (
"between - neither rules the rejected one out - and the choice materially changes the deliverable\n"
"interrupt=false otherwise. Bookkeeping the agent settled silently - what to document, wording, "
"where a note goes - is not on its own a reason to interrupt. The human may have replied inside the transcript, on a [human] line: judge only what "
"is still open, never a question they already answered there.\n"
"is still open, never a question they already answered there. A [loop] line is what loop-claude told "
"the agent, such as an answer quoted from README.md or the project's records; a question it answered "
"is no longer open.\n"
"Where the agent asked or flagged something itself, the question is that one, in the agent's own "
"wording; never construct another from facts it stated.\n"
"Where README.md or the project's records answer that question, quote the passage that answers it "
@@ -85,8 +87,8 @@ def run_judge(root: str, args: list[str], prompt: str, rendered_transcript: str,
if isinstance(verdict, Failure):
return Failure(f"{verdict.reason}; it replied: {proc.stdout.strip() or 'nothing'}")
if not grounded(verdict.evidence, rendered_transcript):
return Failure(f"the judge quoted {verdict.evidence!r}, which is not in the transcript" + (f", for the question: {verdict.question}" if isinstance(verdict, Interrupt | Recorded) else ""))
if isinstance(verdict, Recorded) and not grounded(verdict.answer, recorded):
return Failure(f"the judge quoted {verdict.evidence!r}, which is not in the transcript" + (f", for the question: {verdict.question}" if isinstance(verdict, Interrupt | AnsweredFromRecords) else ""))
if isinstance(verdict, AnsweredFromRecords) and not grounded(verdict.answer, recorded):
return Failure(f"the judge answered {verdict.question!r} with {verdict.answer!r}, which README.md and the project's records do not hold")
return verdict
@@ -97,7 +99,7 @@ def judge_prompt(task: str, premises: str, records: str, rendered_transcript: st
f"The project's records, its working rules and decisions:\n{records or 'none'}\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{rendered_transcript[-JUDGED_TRANSCRIPT_TAIL:]}\n\n"
f"What the agent did and said, what the human typed on [human] lines and what loop-claude told it on [loop] lines, latest last:\n{rendered_transcript[-JUDGED_TRANSCRIPT_TAIL:]}\n\n"
f"{JUDGE_RULES}"
)
@@ -108,14 +110,14 @@ def verdict_from(output: object) -> Verdict | Failure:
for name, kind in REPLY_TYPES.items():
if not isinstance(output.get(name), 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["answer"].strip() and not output["interrupt"]:
return Failure("the judge answered without interrupting")
# 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")
if output["answer"].strip():
return Recorded(output["evidence"], output["question"], output["answer"])
return AnsweredFromRecords(output["evidence"], output["question"], output["answer"])
return Interrupt(output["evidence"], output["question"])
return Finished(output["evidence"]) if output["finished"] else Unfinished(output["evidence"])
+4 -4
View File
@@ -7,7 +7,7 @@ from loop_claude.cycle.archive import archive
from loop_claude.cycle.interrupt import interrupt
from loop_claude.cycle.judge import judge
from loop_claude.cycle.retry import retried
from loop_claude.cycle.states import Active, Answered, Archived, Closing, Ended, Event, Interrupting, Judging, Ruled, State, WorkerRan, Working, transition
from loop_claude.cycle.states import Active, Archived, Closing, Ended, Event, HumanAnswered, Interrupting, Judging, Ruled, State, WorkerRan, Working, transition
from loop_claude.cycle.verdicts import Finished, Unfinished
from loop_claude.cycle.worker import run_worker, worker_args
from loop_claude.effects import Effects
@@ -56,7 +56,7 @@ def step(cycle: Cycle, state: Active) -> Event | Stop:
return work(cycle, answer)
case Judging(transcript_path, _):
return rule(cycle, transcript_path)
case Interrupting(transcript_path, question):
case Interrupting(transcript_path, question, _):
return ask(cycle, transcript_path, question)
case Closing(transcript_path, verdict):
return close(cycle, transcript_path, verdict)
@@ -80,11 +80,11 @@ def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop:
return verdict if isinstance(verdict, Stop) else Ruled(verdict)
def ask(cycle: Cycle, transcript_path: str, question: str) -> Answered | Stop:
def ask(cycle: Cycle, transcript_path: str, question: str) -> HumanAnswered | Stop:
# Archived now, so the transcript the question was asked on is kept with it; the human's answer then grows the transcript.
if failure := archive(cycle.project_key, transcript_path, cycle.task, f"Verdict: interrupt\n\n{question}", cycle.paths):
return Stop(1, failure.reason)
return interrupt(cycle.root, cycle.session.id, cycle.models.worker, transcript_path, question, cycle.paths, cycle.effects) or Answered()
return interrupt(cycle.root, cycle.session.id, cycle.models.worker, transcript_path, question, cycle.paths, cycle.effects) or HumanAnswered()
def close(cycle: Cycle, transcript_path: str, verdict: Finished | Unfinished) -> Archived | Stop:
+13 -11
View File
@@ -1,6 +1,6 @@
import dataclasses
from loop_claude.cycle.verdicts import Finished, Interrupt, Recorded, Unfinished, Verdict
from loop_claude.cycle.verdicts import AnsweredFromRecords, Finished, Interrupt, Unfinished, Verdict
@dataclasses.dataclass(frozen=True)
@@ -11,6 +11,7 @@ class Working:
@dataclasses.dataclass(frozen=True)
class Judging:
"""The judge rules; answered_from_records holds once the records answered a question this cycle."""
transcript_path: str
answered_from_records: bool = False
@@ -19,6 +20,7 @@ class Judging:
class Interrupting:
transcript_path: str
question: str
answered_from_records: bool = False
@dataclasses.dataclass(frozen=True)
@@ -47,7 +49,7 @@ class Ruled:
@dataclasses.dataclass(frozen=True)
class Answered:
class HumanAnswered:
pass
@@ -56,25 +58,25 @@ class Archived:
pass
Event = WorkerRan | Ruled | Answered | Archived
Event = WorkerRan | Ruled | HumanAnswered | Archived
def transition(state: State, event: Event) -> State:
match state, event:
case Working(answer), WorkerRan(transcript_path):
return Judging(transcript_path, answer is not None)
case Judging(_, False), Ruled(Recorded(answer=answer)):
case Judging(_, False), Ruled(AnsweredFromRecords(answer=answer)):
return Working(answer)
# The worker asked again after the records' answer, so the answer did not settle it.
case Judging(transcript_path, True), Ruled(Recorded(question=question)):
return Interrupting(transcript_path, question)
case Judging(transcript_path, _), Ruled(Interrupt(question=question)):
return Interrupting(transcript_path, question)
# One records answer per cycle, so a worker the answer did not satisfy cannot loop unattended.
case Judging(transcript_path, True), Ruled(AnsweredFromRecords(question=question)):
return Interrupting(transcript_path, question, True)
case Judging(transcript_path, answered_from_records), Ruled(Interrupt(question=question)):
return Interrupting(transcript_path, question, answered_from_records)
case Judging(transcript_path, _), Ruled(Finished() | Unfinished() as verdict):
return Closing(transcript_path, verdict)
# The human works in the interrupt's claude session, so the judge rules again on the transcript it leaves.
case Interrupting(transcript_path), Answered():
return Judging(transcript_path)
case Interrupting(transcript_path, _, answered_from_records), HumanAnswered():
return Judging(transcript_path, answered_from_records)
case Closing(), Archived():
return Ended()
raise ValueError(f"no transition from {state} on {event}")
+2 -2
View File
@@ -8,7 +8,7 @@ class Interrupt:
@dataclasses.dataclass(frozen=True)
class Recorded:
class AnsweredFromRecords:
"""The agent asked a question that README.md or the project's records answer, quoted in answer."""
evidence: str
question: str
@@ -25,4 +25,4 @@ class Unfinished:
evidence: str
Verdict = Interrupt | Recorded | Finished | Unfinished
Verdict = Interrupt | AnsweredFromRecords | Finished | Unfinished