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:
2026-10-02 08:42:29 +02:00
parent dc844030fa
commit 211524196b
27 changed files with 212 additions and 165 deletions
+3 -3
View File
@@ -1,13 +1,13 @@
import hashlib
import sys
from loop_claude.failure import Failure
from loop_claude.file_writes import make_private, replace_file
from loop_claude.outcome import Failure
from loop_claude.paths import Paths
def archive(project: str, path: str, note: str, paths: Paths) -> Failure | None:
folder = paths.archive / project
def archive(project_key: str, path: str, note: str, paths: Paths) -> Failure | None:
folder = paths.transcript_archive / project_key
try:
with open(path, "rb") as handle:
raw = handle.read()
+5 -5
View File
@@ -1,20 +1,20 @@
import sys
import uuid
from loop_claude import human_needed
from loop_claude.claude_code.transcript import human_turns, read
from loop_claude.claude_code.trust import trust
from loop_claude.effects import Effects
from loop_claude.outcome import Failure, Stop
from loop_claude.failure import Failure
from loop_claude.paths import Paths
from loop_claude.project.checkout import checkouts
from loop_claude.stop import Stop, needs_human, needs_terminal
TO = "answer the question above"
ANSWER_QUESTION = "answer the question above"
def interrupt(root: str, session_id: uuid.UUID, model: str, path: str, question: str, paths: Paths, effects: Effects) -> Stop | None:
print(f"\n{question}", file=sys.stderr)
if stop := human_needed.off_terminal(TO, effects.on_terminal()):
if stop := needs_terminal(ANSWER_QUESTION, effects.on_terminal()):
return stop
trust(paths.claude_json, checkouts(root, effects))
entries = read(path)
@@ -28,4 +28,4 @@ def interrupt(root: str, session_id: uuid.UUID, model: str, path: str, question:
entries = read(path)
if isinstance(entries, Failure):
return Stop(1, f"could not tell whether you typed anything: {entries.reason}")
return None if human_turns(entries) > before else human_needed.stop(TO, "the interrupt ended with nothing typed")
return None if human_turns(entries) > before else needs_human(ANSWER_QUESTION, "the interrupt ended with nothing typed")
+14 -13
View File
@@ -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:
+2 -1
View File
@@ -2,7 +2,8 @@ import sys
import typing
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
T = typing.TypeVar("T")
@@ -10,9 +10,10 @@ from loop_claude.cycle.retry import retried
from loop_claude.cycle.states import Active, Answered, Archived, Ended, Event, Finishing, Interrupting, Judging, Ruled, State, WorkerRan, Working, transition
from loop_claude.cycle.worker import run_worker, worker_args
from loop_claude.effects import Effects
from loop_claude.outcome import Failure, Stop
from loop_claude.failure import Failure
from loop_claude.paths import Paths
from loop_claude.project.models import Models
from loop_claude.stop import Stop
@dataclasses.dataclass(frozen=True)
@@ -21,7 +22,7 @@ class Cycle:
models: Models
paths: Paths
premises: str
project: str
project_key: str
root: str
session: Session
task: str
@@ -69,13 +70,13 @@ def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop:
def ask(cycle: Cycle, transcript_path: str, question: str) -> Answered | Stop:
# The interrupt appends to this transcript, so it is archived at each question.
if failure := archive(cycle.project, transcript_path, f"Task: {cycle.task}\n\nVerdict: interrupt\n\n{question}", cycle.paths):
if failure := archive(cycle.project_key, transcript_path, f"Task: {cycle.task}\n\nVerdict: 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()
def close(cycle: Cycle, transcript_path: str) -> Archived | Stop:
if failure := archive(cycle.project, transcript_path, f"Task: {cycle.task}\n\nVerdict: finished", cycle.paths):
if failure := archive(cycle.project_key, transcript_path, f"Task: {cycle.task}\n\nVerdict: finished", cycle.paths):
return Stop(1, failure.reason)
if failure := delete_session(cycle.session):
return Stop(1, failure.reason)
+5 -3
View File
@@ -1,8 +1,10 @@
from loop_claude.claude_code.formats import assistant_parts, ends_stream, parse
from loop_claude.claude_code.entries import assistant_parts, ends_stream, parse
from loop_claude.claude_code.session import Session
from loop_claude.claude_code.transcript import part_line
from loop_claude.effects import Effects
from loop_claude.outcome import Failure
from loop_claude.failure import Failure
PRINTED_TOOL_INPUT = 160
def run_worker(root: str, args: list[str], effects: Effects) -> Failure | None:
@@ -15,7 +17,7 @@ def run_worker(root: str, args: list[str], effects: Effects) -> Failure | None:
event = parse(raw) or {}
done = done or ends_stream(event)
for part in assistant_parts(event):
if line := part_line(part, 160):
if line := part_line(part, PRINTED_TOOL_INPUT):
print(line, flush=True)
code = proc.wait()
if code < 0: