Answer the architecture pass's first round

This commit is contained in:
2026-10-02 08:52:20 +02:00
parent 211524196b
commit d42c99c37d
15 changed files with 97 additions and 72 deletions
+2 -2
View File
@@ -2,7 +2,7 @@
import json
def parse(raw: str | bytes) -> dict | None:
def parse_entry(raw: str | bytes) -> dict | None:
try:
entry = json.loads(raw)
except ValueError:
@@ -91,4 +91,4 @@ def ends_stream(event: dict) -> bool:
# {"type": "result", "structured_output": {"evidence": "...", ...}, ...}
def structured_output(stdout: str) -> object:
return (parse(stdout) or {}).get("structured_output")
return (parse_entry(stdout) or {}).get("structured_output")
+3 -3
View File
@@ -1,15 +1,15 @@
import json
from loop_claude.claude_code.entries import assistant_parts, human_text, parse, part_text, tool_call
from loop_claude.claude_code.entries import assistant_parts, human_text, parse_entry, part_text, tool_call
from loop_claude.failure import Failure
JUDGED_TOOL_INPUT = 400
def read(path: str) -> list[dict] | Failure:
def read_transcript(path: str) -> list[dict] | Failure:
try:
with open(path, "rb") as handle:
return [entry for raw in handle if (entry := parse(raw))]
return [entry for raw in handle if (entry := parse_entry(raw))]
except OSError as failure:
return Failure(f"could not read the transcript {path}: {failure.strerror}")
+5 -5
View File
@@ -1,12 +1,12 @@
import sys
import uuid
from loop_claude.claude_code.transcript import human_turns, read
from loop_claude.claude_code.transcript import human_turns, read_transcript
from loop_claude.claude_code.trust import trust
from loop_claude.effects import Effects
from loop_claude.failure import Failure
from loop_claude.paths import Paths
from loop_claude.project.checkout import checkouts
from loop_claude.project.checkout import worktrees
from loop_claude.stop import Stop, needs_human, needs_terminal
ANSWER_QUESTION = "answer the question above"
@@ -16,8 +16,8 @@ def interrupt(root: str, session_id: uuid.UUID, model: str, path: str, question:
print(f"\n{question}", file=sys.stderr)
if stop := needs_terminal(ANSWER_QUESTION, effects.on_terminal()):
return stop
trust(paths.claude_json, checkouts(root, effects))
entries = read(path)
trust(paths.claude_json, worktrees(root, effects))
entries = read_transcript(path)
if isinstance(entries, Failure):
return Stop(1, entries.reason)
before = human_turns(entries)
@@ -25,7 +25,7 @@ def interrupt(root: str, session_id: uuid.UUID, model: str, path: str, question:
effects.claude_on_terminal(["--model", model, "--permission-mode", "bypassPermissions", "--resume", str(session_id)], root)
except OSError as failure:
return Stop(1, f"could not start claude for the interrupt: {failure.strerror}")
entries = read(path)
entries = read_transcript(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 needs_human(ANSWER_QUESTION, "the interrupt ended with nothing typed")
+3 -1
View File
@@ -9,6 +9,8 @@ from loop_claude.effects import Effects
from loop_claude.failure import Failure
from loop_claude.stop import Stop
JUDGED_TRANSCRIPT_TAIL = 40000
JUDGE_RULES = (
"Decide whether the human must now be interrupted. interrupt=true when any of these appear:\n"
"- the agent asks the human anything, seeks approval, or says a decision is the human's\n"
@@ -81,7 +83,7 @@ def judge_prompt(task: str, premises: str, rendered_transcript: str) -> str:
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{rendered_transcript[-40000:]}\n\n"
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"{JUDGE_RULES}"
)
+4 -2
View File
@@ -5,6 +5,8 @@ from loop_claude.effects import Effects
from loop_claude.failure import Failure
from loop_claude.stop import Stop
ATTEMPTS = 3
RETRY_DELAY_SECONDS = 60
T = typing.TypeVar("T")
@@ -15,7 +17,7 @@ def retried(who: str, attempt: typing.Callable[[], T | Failure], effects: Effect
result = attempt()
if not isinstance(result, Failure):
return result
if failures == 3:
if failures == ATTEMPTS:
return Stop(1, f"the {who} failed three times running: {result.reason}")
print(f"retrying the {who} in a minute: {result.reason}", file=sys.stderr)
effects.sleep(60)
effects.sleep(RETRY_DELAY_SECONDS)
+3 -3
View File
@@ -2,7 +2,7 @@ import dataclasses
import typing
from loop_claude.claude_code.session import Session, delete_session, session_open, transcript_file
from loop_claude.claude_code.transcript import read, render
from loop_claude.claude_code.transcript import read_transcript, render
from loop_claude.cycle.archive import archive
from loop_claude.cycle.interrupt import interrupt
from loop_claude.cycle.judge import judge
@@ -12,7 +12,7 @@ from loop_claude.cycle.worker import run_worker, worker_args
from loop_claude.effects import Effects
from loop_claude.failure import Failure
from loop_claude.paths import Paths
from loop_claude.project.models import Models
from loop_claude.project.model_choice import Models
from loop_claude.stop import Stop
@@ -61,7 +61,7 @@ def work(cycle: Cycle) -> WorkerRan | Stop:
def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop:
entries = read(transcript_path)
entries = read_transcript(transcript_path)
if isinstance(entries, Failure):
return Stop(1, entries.reason)
verdict = judge(cycle.root, cycle.task, cycle.premises, render(entries), cycle.models.judge, cycle.effects)
+1
View File
@@ -66,6 +66,7 @@ def transition(state: State, event: Event) -> State:
return Finishing(transcript_path)
case Judging(), Ruled(Unfinished()):
return Ended()
# The interrupt is a whole claude session on the terminal, so the judge rules again on what it left.
case Interrupting(transcript_path), Answered():
return Judging(transcript_path)
case Finishing(), Archived():
+2 -2
View File
@@ -1,4 +1,4 @@
from loop_claude.claude_code.entries import assistant_parts, ends_stream, parse
from loop_claude.claude_code.entries import assistant_parts, ends_stream, parse_entry
from loop_claude.claude_code.session import Session
from loop_claude.claude_code.transcript import part_line
from loop_claude.effects import Effects
@@ -14,7 +14,7 @@ def run_worker(root: str, args: list[str], effects: Effects) -> Failure | None:
return Failure(f"could not start claude: {failure.strerror}")
done = False
for raw in proc.lines():
event = parse(raw) or {}
event = parse_entry(raw) or {}
done = done or ends_stream(event)
for part in assistant_parts(event):
if line := part_line(part, PRINTED_TOOL_INPUT):
+5 -5
View File
@@ -30,12 +30,12 @@ Exit 2: the human is needed, to pick the model or answer an interrupt, and stdin
terminal, the input ends, or the interrupt ended with nothing typed. Rerunning resumes any
open session.
Each project has one session, "A <path> loop-claude", <path> being the checkout relative to ~/code,
Each project has one session, "A <key> loop-claude", <key> being the checkout relative to ~/code,
or to / outside it, deleted when the chunk finishes. At every interrupt and when the chunk finishes
its transcript is copied to $LOOP_CLAUDE_TRANSCRIPTS/<path>/<sha256>.jsonl (default
its transcript is copied to $LOOP_CLAUDE_TRANSCRIPTS/<key>/<sha256>.jsonl (default
~/.claude/loop-claude-transcripts), the verdict beside it in <sha256>.verdict.md.
Every claude it starts runs the project's model, saved per <path> in ~/.claude/loop-claude-models.json
Every claude it starts runs the project's model, saved per <key> in ~/.claude/loop-claude-models.json
and read each cycle; a project without an entry, as on its first run, is asked for one.
LOOP_CLAUDE_JUDGE_MODEL overrides it for the judge, LOOP_CLAUDE_WORKER_MODEL for the worker and the
interrupt; with both set, nothing is asked.
@@ -54,7 +54,7 @@ from loop_claude.effects import Effects
from loop_claude.loop.reload import RELOAD_FLAG, LoopRun, decode_handed, exec_if_edited, fingerprint
from loop_claude.paths import Paths
from loop_claude.project.checkout import checkout_root, project_key
from loop_claude.project.models import models_for
from loop_claude.project.model_choice import get_or_ask_models
from loop_claude.project.premises import read_premises
from loop_claude.stop import Stop
@@ -83,7 +83,7 @@ def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launc
premises = read_premises(loop_run.root)
if isinstance(premises, Stop):
return premises
models = models_for(key, paths, env, effects)
models = get_or_ask_models(key, paths, env, effects)
if isinstance(models, Stop):
return models
print(f"\n=== cycle {loop_run.cycle_number} ===", flush=True)
+1 -1
View File
@@ -1,4 +1,3 @@
"""A loop runs unattended for hours, often on its own checkout: a reload puts an edit to loop-claude to work at the next cycle without the human rerunning it."""
import dataclasses
import hashlib
import json
@@ -46,6 +45,7 @@ def fingerprint(launcher: pathlib.Path) -> str:
return digest.hexdigest()
# A loop runs unattended for hours, often on its own checkout: a reload puts an edit to loop-claude to work without the human rerunning it.
def exec_if_edited(launcher: pathlib.Path, baseline: str, loop_run: LoopRun, effects: Effects) -> Stop | None:
if fingerprint(launcher) == baseline:
return None
+1 -1
View File
@@ -14,7 +14,7 @@ def checkout_root(cwd: pathlib.Path, effects: Effects) -> str | Stop:
return found.stdout.strip()
def checkouts(root: str, effects: Effects) -> list[str]:
def worktrees(root: str, effects: Effects) -> list[str]:
found = [root]
try:
listing = effects.git(["worktree", "list", "--porcelain"], root)
@@ -4,7 +4,7 @@ import pathlib
import sys
import typing
from loop_claude.claude_code.catalog import catalog_models
from loop_claude.claude_code.model_catalog import catalog_models
from loop_claude.effects import Effects
from loop_claude.failure import Failure
from loop_claude.file_writes import replace_json
@@ -18,17 +18,17 @@ class Models:
worker: str
def models_for(project_key: str, paths: Paths, env: typing.Mapping[str, str], effects: Effects) -> Models | Stop:
def get_or_ask_models(project_key: str, paths: Paths, env: typing.Mapping[str, str], effects: Effects) -> Models | Stop:
judge, worker = env.get("LOOP_CLAUDE_JUDGE_MODEL"), env.get("LOOP_CLAUDE_WORKER_MODEL")
if judge and worker:
return Models(judge, worker)
chosen = saved_model(project_key, paths, effects)
chosen = get_or_ask_model(project_key, paths, effects)
if isinstance(chosen, Stop):
return chosen
return Models(judge or chosen, worker or chosen)
def saved_model(project_key: str, paths: Paths, effects: Effects) -> str | Stop:
def get_or_ask_model(project_key: str, paths: Paths, effects: Effects) -> str | Stop:
saved = paths.models_file
models = read_models_file(saved)
if isinstance(models, Stop):