Answer the stability and product passes: print each records answer, name its question, drop a misquote to an interrupt, budget the whole prompt

This commit is contained in:
2026-10-03 01:42:37 +02:00
parent 74d9df412e
commit 09fb7dcdd5
10 changed files with 62 additions and 53 deletions
+9 -9
View File
@@ -10,8 +10,8 @@ from loop_claude.failure import Failure
from loop_claude.limited import Limited
from loop_claude.stop import Stop
# 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
# At 3 to 4 characters a token, 100,000 to 133,000 tokens, which leaves a 200,000-token context room for the reply; the largest session rendered here was 364,000 characters on 2026-10-03.
JUDGE_PROMPT_CHARS = 400000
JUDGE_RULES = (
"Decide whether the agent has something open that the human, or README.md and the project's records, "
@@ -88,20 +88,22 @@ def run_judge(root: str, args: list[str], prompt: str, rendered_transcript: str,
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 | AnsweredFromRecords) else ""))
# An answer the records do not hold verbatim is dropped, and the question goes to the human.
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 Interrupt(verdict.evidence, verdict.question)
return verdict
def judge_prompt(task: str, premises: str, records: str, rendered_transcript: str) -> str:
return (
head = (
f"From README.md:\n{premises}\n\n"
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, 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}"
"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"
)
room = max(0, JUDGE_PROMPT_CHARS - len(head) - len(JUDGE_RULES) - 2)
# The tail, where an interrupt or a finish lands; the session's transcript grows cycle over cycle.
return f"{head}{rendered_transcript[max(0, len(rendered_transcript) - room):]}\n\n{JUDGE_RULES}"
def verdict_from(output: object) -> Verdict | Failure:
@@ -110,8 +112,6 @@ 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]}")
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():
+7 -5
View File
@@ -8,7 +8,7 @@ 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, Archived, Closing, Ended, Event, HumanAnswered, Interrupting, Judging, Ruled, State, WorkerRan, Working, transition
from loop_claude.cycle.verdicts import Finished, Unfinished
from loop_claude.cycle.verdicts import AnsweredFromRecords, Finished, Unfinished
from loop_claude.cycle.worker import run_worker, worker_args
from loop_claude.effects import Effects
from loop_claude.failure import Failure
@@ -52,8 +52,8 @@ def stopped(cycle: Cycle, stop: Stop, why: str) -> Stop:
def step(cycle: Cycle, state: Active) -> Event | Stop:
match state:
case Working(answer, _):
return work(cycle, answer)
case Working(answered, _):
return work(cycle, answered)
case Judging(transcript_path, _):
return rule(cycle, transcript_path)
case Interrupting(transcript_path, question, _):
@@ -64,9 +64,11 @@ def step(cycle: Cycle, state: Active) -> Event | Stop:
typing.assert_never(state)
def work(cycle: Cycle, answer: str | None) -> WorkerRan | Stop:
def work(cycle: Cycle, answered: AnsweredFromRecords | None) -> WorkerRan | Stop:
if answered is not None:
print(f"\n=== the records answered: {answered.question} ===\n{answered.answer}", flush=True)
# session_open runs inside the lambda: a failed attempt may have opened the session.
if stop := retried("worker", lambda: run_worker(cycle.root, worker_args(cycle.task, cycle.session, cycle.models.worker, session_open(cycle.session), answer), cycle.effects), cycle.effects):
if stop := retried("worker", lambda: run_worker(cycle.root, worker_args(cycle.task, cycle.session, cycle.models.worker, session_open(cycle.session), answered), cycle.effects), cycle.effects):
return stop
path = transcript_file(cycle.session)
return WorkerRan(path) if path is not None else Stop(1, f"claude left no transcript for session {cycle.session.id}")
+4 -4
View File
@@ -9,7 +9,7 @@ RECORDS_ANSWERS_PER_CYCLE = 3
@dataclasses.dataclass(frozen=True)
class Working:
"""The worker runs, resumed with the records' answer when there is one."""
answer: str | None = None
answered: AnsweredFromRecords | None = None
records_answers: int = 0
@@ -69,10 +69,10 @@ def transition(state: State, event: Event) -> State:
match state, event:
case Working(_, records_answers), WorkerRan(transcript_path):
return Judging(transcript_path, records_answers)
case Judging(_, records_answers), Ruled(AnsweredFromRecords(answer=answer)) if records_answers < RECORDS_ANSWERS_PER_CYCLE:
return Working(answer, records_answers + 1)
case Judging(_, records_answers), Ruled(AnsweredFromRecords() as answered) if records_answers < RECORDS_ANSWERS_PER_CYCLE:
return Working(answered, records_answers + 1)
case Judging(transcript_path, records_answers), Ruled(AnsweredFromRecords(question=question, answer=answer)):
return Interrupting(transcript_path, f"{question}\n\nREADME.md or the project's records say:\n{answer}", records_answers)
return Interrupting(transcript_path, f"{question}\n\nREADME.md or the project's records say:\n{answer}\n\nThe records answered {records_answers} questions this cycle, so this one comes to you.", records_answers)
case Judging(transcript_path, records_answers), Ruled(Interrupt(question=question)):
return Interrupting(transcript_path, question, records_answers)
case Judging(transcript_path, _), Ruled(Finished() | Unfinished() as verdict):
+6 -4
View File
@@ -1,6 +1,7 @@
from loop_claude.claude_code.entries import assistant_parts, ends_stream, hit_limit, limit_said, parse_entry, rejected_until
from loop_claude.claude_code.session import Session
from loop_claude.claude_code.transcript import part_line
from loop_claude.cycle.verdicts import AnsweredFromRecords
from loop_claude.effects import Effects
from loop_claude.failure import Failure
from loop_claude.limited import Limited
@@ -37,10 +38,11 @@ def worker_outcome(code: int, result: dict | None, resets: list[int]) -> Failure
return None if result is not None else Failure("claude exited 0 without a result event")
def worker_args(task: str, session: Session, model: str, resuming: bool, answer: str | None = None) -> list[str]:
if resuming:
prompt = f"README.md or the project's records answer your question:\n\n{answer}\n\nContinue the task." if answer is not None else "Continue the task; the previous run stopped before it was finished."
start = ["-p", prompt, "--resume", str(session.id)]
def worker_args(task: str, session: Session, model: str, resuming: bool, answered: AnsweredFromRecords | None = None) -> list[str]:
if answered is not None:
start = ["-p", f"Your question: {answered.question}\n\nREADME.md or the project's records answer it:\n\n{answered.answer}\n\nContinue the task.", "--resume", str(session.id)]
elif resuming:
start = ["-p", "Continue the task; the previous run stopped before it was finished.", "--resume", str(session.id)]
else:
start = ["-p", f"{task}\n\nContinue any open draft PR or worktree for the chunk before creating a new one.", "--name", session.name, "--session-id", str(session.id)]
return [
+9 -8
View File
@@ -4,11 +4,12 @@ usage: loop-claude <task>
Run it anywhere inside a project checkout whose README.md has ## Goals and ## Audience, the
premises; the task runs at the checkout root. Each cycle a worker runs the task; a judge then reads
the transcript's last 400,000 characters against the premises and the project's records, AGENTS.md
and docs/decisions.md where they exist, and rules whether the chunk, the work of one PR, is finished.
A choice the worker made that the premises or the records settle is not asked about. When the worker
asks a question they answer, the judge quotes the answer and the worker resumes with it, up to three
times a cycle; a question asked again after its answer, or a fourth, goes to the human.
as much of the transcript's end as fits a 400,000-character prompt against the premises and the
project's records, AGENTS.md and docs/decisions.md where they exist, and rules whether the chunk,
the work of one PR, is finished. A choice the worker made that the premises or the records settle is
not asked about. When the worker asks a question they answer, the judge quotes the answer, printed
on stdout, and the worker resumes with it, up to three times a cycle; a question asked again after
its answer goes to the human, and a fourth with the quote.
A question only the human can answer, or a release ready to cut, is an interrupt: `claude --resume`
on the terminal, every worktree first marked trusted in ~/.claude.json. An unfinished chunk resumes
its session next cycle; the next chunk starts a fresh session.
@@ -50,9 +51,9 @@ Exit 1, saying why, when:
- the worker or the judge fails three times running, retried a minute apart within the cycle. A
worker fails when claude cannot start, exits non-zero, is killed, or ends without a result event;
a judge when claude cannot start or exits non-zero, or its reply misses or mistypes a field,
interrupts without a question, answers without interrupting, quotes a line the transcript does
not hold, or quotes an answer the premises and the records do not hold. A usage limit is not a
failure, and restarts the count of three: the worker or the judge is retried 2 minutes
interrupts without a question, or quotes a line the transcript does not hold; an answer it
quotes that the premises and the records do not hold is dropped, and the question goes to the
human. A usage limit is not a failure, and restarts the count of three: the worker or the judge is retried 2 minutes
after the limit resets, or every 10 minutes when the reset time is unknown or more than 2
minutes past. The judge's reply gives its reset time only as text, so the judge is always
retried every 10 minutes. stderr says when the next try is due.
+1 -1
View File
@@ -36,4 +36,4 @@ def read_checkout_file(root: str, name: str, consequence: str) -> str | Stop:
return Stop(1, f"{name} in {root}: {failure.strerror} - {consequence}.")
except UnicodeDecodeError as failure:
line = failure.object.count(b"\n", 0, failure.start) + 1
return Stop(1, f"{name} is not UTF-8 on line {line} - {consequence}.")
return Stop(1, f"{name} in {root} is not UTF-8 on line {line} - {consequence}.")