Answer the first round: resume the worker with the records' answer to its question, read docs/decisions.md alone, scope --help's promise

This commit is contained in:
2026-10-03 00:35:40 +02:00
parent 6d5d19bd69
commit 6340e5a993
11 changed files with 100 additions and 54 deletions
+12 -5
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, Unfinished, Verdict
from loop_claude.cycle.verdicts import Finished, Interrupt, Recorded, 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, so the prompt fits a 200,000-token context; the largest session rendered here was 364,000 on 2026-10-03.
# 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.
JUDGED_TRANSCRIPT_TAIL = 400000
JUDGE_RULES = (
@@ -29,6 +29,8 @@ JUDGE_RULES = (
"is still open, never a question they already answered there.\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 "
"in answer, verbatim; the agent is then given that passage in place of the human's reply.\n"
"finished=true when the chunk is complete: merged, or nothing remains that the agent could do on "
"its own. finished=false when work remains that a further run can continue: an open PR, a review "
"not yet run, CI not yet reported."
@@ -36,6 +38,7 @@ JUDGE_RULES = (
@dataclasses.dataclass(frozen=True)
class Reply:
answer: str = dataclasses.field(metadata={"description": "The passage of README.md or the project's records that answers the agent's question, quoted verbatim. Empty when nothing there answers it, or when interrupt is false."})
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
@@ -63,10 +66,10 @@ def judge(root: str, task: str, premises: str, records: str, rendered_transcript
"--tools", "",
]
prompt = judge_prompt(task, premises, records, rendered_transcript)
return retried("judge", lambda: run_judge(root, args, prompt, rendered_transcript, effects), effects)
return retried("judge", lambda: run_judge(root, args, prompt, rendered_transcript, f"{premises}\n\n{records}", effects), effects)
def run_judge(root: str, args: list[str], prompt: str, rendered_transcript: str, effects: Effects) -> Verdict | Failure | Limited:
def run_judge(root: str, args: list[str], prompt: str, rendered_transcript: str, recorded: str, effects: Effects) -> Verdict | Failure | Limited:
try:
proc = effects.claude(args, root, prompt)
except OSError as failure:
@@ -81,7 +84,9 @@ 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) else ""))
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 answered {verdict.question!r} with {verdict.answer!r}, which README.md and the project's records do not hold")
return verdict
@@ -106,6 +111,8 @@ def verdict_from(output: object) -> Verdict | Failure:
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 Interrupt(output["evidence"], output["question"])
return Finished(output["evidence"]) if output["finished"] else Unfinished(output["evidence"])
+5 -5
View File
@@ -52,9 +52,9 @@ def stopped(cycle: Cycle, stop: Stop, why: str) -> Stop:
def step(cycle: Cycle, state: Active) -> Event | Stop:
match state:
case Working():
return work(cycle)
case Judging(transcript_path):
case Working(answer):
return work(cycle, answer)
case Judging(transcript_path, _):
return rule(cycle, transcript_path)
case Interrupting(transcript_path, question):
return ask(cycle, transcript_path, question)
@@ -64,9 +64,9 @@ def step(cycle: Cycle, state: Active) -> Event | Stop:
typing.assert_never(state)
def work(cycle: Cycle) -> WorkerRan | Stop:
def work(cycle: Cycle, answer: str | None) -> WorkerRan | Stop:
# 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)), 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), answer), 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}")
+13 -6
View File
@@ -1,16 +1,18 @@
import dataclasses
from loop_claude.cycle.verdicts import Finished, Interrupt, Unfinished, Verdict
from loop_claude.cycle.verdicts import Finished, Interrupt, Recorded, Unfinished, Verdict
@dataclasses.dataclass(frozen=True)
class Working:
"""The worker runs."""
"""The worker runs, resumed with the records' answer when there is one."""
answer: str | None = None
@dataclasses.dataclass(frozen=True)
class Judging:
transcript_path: str
answered_from_records: bool = False
@dataclasses.dataclass(frozen=True)
@@ -59,11 +61,16 @@ Event = WorkerRan | Ruled | Answered | Archived
def transition(state: State, event: Event) -> State:
match state, event:
case Working(), WorkerRan(transcript_path):
return Judging(transcript_path)
case Judging(transcript_path), Ruled(Interrupt(question=question)):
case Working(answer), WorkerRan(transcript_path):
return Judging(transcript_path, answer is not None)
case Judging(_, False), Ruled(Recorded(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(Finished() | Unfinished() as verdict):
case Judging(transcript_path, _), Ruled(Interrupt(question=question)):
return Interrupting(transcript_path, question)
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():
+9 -1
View File
@@ -7,6 +7,14 @@ class Interrupt:
question: str
@dataclasses.dataclass(frozen=True)
class Recorded:
"""The agent asked a question that README.md or the project's records answer, quoted in answer."""
evidence: str
question: str
answer: str
@dataclasses.dataclass(frozen=True)
class Finished:
evidence: str
@@ -17,4 +25,4 @@ class Unfinished:
evidence: str
Verdict = Interrupt | Finished | Unfinished
Verdict = Interrupt | Recorded | Finished | Unfinished
+3 -2
View File
@@ -37,9 +37,10 @@ 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) -> list[str]:
def worker_args(task: str, session: Session, model: str, resuming: bool, answer: str | None = None) -> list[str]:
if resuming:
start = ["-p", "Continue the task; the previous run stopped before it was finished.", "--resume", str(session.id)]
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)]
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 [
+8 -5
View File
@@ -4,9 +4,11 @@ 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,
docs/decisions.md and docs/decisions/*.md where they exist, and rules whether the chunk, the work of
one PR, is finished. A choice the premises or the records settle is not asked again.
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; the
worker's next question in the cycle goes to the human.
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.
@@ -36,7 +38,7 @@ Exit 1, saying why, when:
- no task is given
- git cannot start or finds no checkout
- README.md holds no premises
- AGENTS.md or a decision file cannot be read, or is not UTF-8
- AGENTS.md or docs/decisions.md cannot be read, or is not UTF-8
- the models file cannot be read or written
- claude leaves no transcript
- a transcript cannot be read or archived
@@ -48,7 +50,8 @@ 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, or quotes a line the transcript does not hold. A usage limit is
interrupts without a question, 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
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
+3 -6
View File
@@ -4,14 +4,11 @@ from loop_claude.stop import Stop
def read_records(root: str) -> str | Stop:
"""AGENTS.md and the decision log, each headed by its path; a file the project lacks is left out."""
project = pathlib.Path(root)
paths = [project / "AGENTS.md", project / "docs" / "decisions.md", *sorted(project.glob("docs/decisions/*.md"))]
"""AGENTS.md and docs/decisions.md, each headed by its path; a file the project lacks is left out."""
records = []
for path in paths:
name = path.relative_to(project)
for name in ("AGENTS.md", "docs/decisions.md"):
try:
text = path.read_text(encoding="utf-8")
text = pathlib.Path(root, name).read_text(encoding="utf-8")
except FileNotFoundError:
continue
except OSError as failure: