diff --git a/AGENTS.md b/AGENTS.md index f0296173..e3c42093 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -5,7 +5,7 @@ Decisions live in `docs/decisions.md`: - larv-review: a project lifting itself to the comprehension floor starts at the lowest dimension - loop-claude: a judge decides each interrupt and each quoted answer from the transcript, the README's premises, `AGENTS.md`, `docs/decisions.md`, `~/.claude/CLAUDE.md`, `~/.claude/rules/` and larv-review's standards, and holds a chunk open while an answer the human typed in it is recorded nowhere; the worker's own account of which goal settles a choice decides nothing - loop-claude: one session per project, its id derived from the checkout's path relative to `~/code`, or to `/` outside it -- loop-claude: before an interrupt, it marks the checkout and each of its worktrees trusted in `~/.claude.json` +- loop-claude: before it opens claude on the terminal, it marks the checkout and each of its worktrees trusted in `~/.claude.json` - bin/: the tests rerun themselves on a `uv`-fetched Python at README Install's floor - bin/: the tools and their tests run on the host's Python, with no container - comprehension floor: every dimension and the overall at 7.0 or above is the target, and blocks no merge or release diff --git a/CHANGELOG.md b/CHANGELOG.md index f6100774..d9d008a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- `loop-claude` asks an interrupt's question on its own terminal and resumes the worker headless with your one-line answer, so the loop no longer waits for you to leave a claude session. Type `/talk` instead to open the worker's session in claude, as every interrupt did before, to read the diff or discuss first. A release ready to cut opens that session at once. Pressing Enter on an empty answer stops the loop, exit 2. The judge reads your answer as typed by you, so the chunk stays open until the worker records it. - When a worker's run ends with a question and a request for a higher rung, `loop-claude` now steps the worker one rung up as the judge interrupts, or as it answers the question with a quote from `README.md` or another file it may quote. The judge must quote a passage showing that the escalation trigger the run cites fired. Before, the step was lost for an answered question, and the worker carried on from the answer on its old rung. The step has the same limits as the step on an unfinished verdict, and no later ruling of that cycle carries a second escalation. The archive's interrupt and quoted-answer outcomes now name the worker's rung. - A scoring run due because a release's items ran out costs four more seats: it is done twice on one head. `larv-review agreement` compares how often Mid A and the inherited architect vote alike in paired rulings with how often their overall scores move the same way between the two runs. After 10 such pairs it gives a verdict, and on `Assumption fails:` larv-review asks you whether to keep the paired ruling. - `workflow-bench/bench.py` gives each run a fixed list of allowed tools. Before, it skipped every permission check. A run may launch agents, run `/security-review`, edit its clone (not the clone's `.git/` or `.claude/`) and its report, and read its clone and the copy of `~/.claude` it benches. For git it gets one command, `workflow-bench/review_git.py`, and no bare `git`. The wrapper runs `log`, `diff`, `show`, `status`, `ls-files`, `add` and `commit -m` on the run's clone, whatever the working directory. It allows only the options it lists for each, and refuses an argument that names a path outside the clone. git runs with no global or system config, hook, pager or editor. A run loads no settings or MCP servers from `~/.claude` or from the case's clone, so their hooks and extra directories do not apply to it. Every other call is denied. Expect a benched review that needs another git command, or to run a project's code or tests, to say in its report that it could not. diff --git a/bin/claude_code/test_claude_code.py b/bin/claude_code/test_claude_code.py index a07d331e..bd18d1e2 100644 --- a/bin/claude_code/test_claude_code.py +++ b/bin/claude_code/test_claude_code.py @@ -23,7 +23,7 @@ def queued(prompt, kind): class Render(unittest.TestCase): - def test_joins_assistant_text_tool_calls_human_turns_and_loop_prompts_and_skips_the_rest(self): + def test_joins_assistant_text_tool_calls_human_turns_and_loop_prompts_labelling_a_typed_answer_the_humans_and_skips_the_rest(self): said = transcript.render([ human("do it"), assistant({"type": "text", "text": "Starting."}, {"type": "tool_use", "name": "Bash", "input": {"command": "ls"}}), @@ -34,8 +34,9 @@ class Render(unittest.TestCase): queued("B, while you work", "human"), queued("origin/main moved", "coordinator"), {**human("Continue the task; the previous run stopped before it was finished."), "promptSource": "sdk"}, - ]) - self.assertEqual(said, '[human] do it\n\nStarting.\n\n[tool Bash] {"command": "ls"}\n\n[human] yes, merge it\n\n[human] B, while you work\n\n[loop] Continue the task; the previous run stopped before it was finished.') + {**human("Answered: use B"), "promptSource": "sdk"}, + ], lambda prompt: prompt.removeprefix("Answered: ") if prompt.startswith("Answered: ") else None) + self.assertEqual(said, '[human] do it\n\nStarting.\n\n[tool Bash] {"command": "ls"}\n\n[human] yes, merge it\n\n[human] B, while you work\n\n[loop] Continue the task; the previous run stopped before it was finished.\n\n[human] use B') class FirstTimestampedLine(unittest.TestCase): diff --git a/bin/claude_code/transcript.py b/bin/claude_code/transcript.py index 0991bf16..07ce9fde 100644 --- a/bin/claude_code/transcript.py +++ b/bin/claude_code/transcript.py @@ -1,4 +1,5 @@ import json +import typing from claude_code.entries import assistant_parts, human_text, loop_text, parse_entry, part_text, tool_call from failure import Failure @@ -24,14 +25,16 @@ def human_turns(entries: list[dict]) -> int: return sum(1 for entry in entries if human_text(entry) is not None) -def render(entries: list[dict]) -> str: +def render(entries: list[dict], typed_answer: typing.Callable[[str], str | None]) -> str: + """The transcript as the judge reads it. typed_answer finds an answer the human typed in a -p prompt of loop-claude's, which reads as the human's, like one typed in claude.""" lines = [] for entry in entries: 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) + typed = typed_answer(text) + lines.append("[loop] " + text if typed is None else "[human] " + typed) return "\n\n".join(lines) diff --git a/bin/judge_replay/main.py b/bin/judge_replay/main.py index 3c406497..7d7c021b 100644 --- a/bin/judge_replay/main.py +++ b/bin/judge_replay/main.py @@ -38,7 +38,7 @@ from loop_claude.cycle.judge import judge_once, Verdict from claude_code.reply import Limited from loop_claude.project.premises import read_premises from loop_claude.project.records import read_records -from claude_code.transcript import read_transcript, render +from claude_code.transcript import read_transcript from model_routing.ladder import Rung from loop_claude.stop import Stop @@ -91,5 +91,5 @@ def rule(case: Case, rung: Rung, claude_dir: pathlib.Path, effects: Effects) -> return verdict_kind(Failure(records.reason)) if isinstance(entries, Failure): return verdict_kind(entries) - verdict: Verdict | Failure | Limited = first_reply(lambda: judge_once(case.root, case.task, premises, records, render(entries), rung, effects)) + verdict: Verdict | Failure | Limited = first_reply(lambda: judge_once(case.root, case.task, premises, records, entries, rung, effects)) return verdict if isinstance(verdict, Limited) else verdict_kind(verdict) diff --git a/bin/judge_replay/test_judge_replay.py b/bin/judge_replay/test_judge_replay.py index faf513b1..a30aa1a5 100644 --- a/bin/judge_replay/test_judge_replay.py +++ b/bin/judge_replay/test_judge_replay.py @@ -122,7 +122,7 @@ class Main(unittest.TestCase): @staticmethod def verdict(interrupt, evidence): - output = {"answer": "", "bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "evidence": evidence, "finished": not interrupt, "interrupt": interrupt, "question": "Which?" if interrupt else ""} + output = {"answer": "", "bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "evidence": evidence, "finished": not interrupt, "interrupt": interrupt, "question": "Which?" if interrupt else "", "release_ready": False} return {"type": "result", "structured_output": output} def test_replays_the_models_projects_on_the_reference_then_from_the_projects_rung_down_and_prints_the_lowest_that_returns_all(self): diff --git a/bin/loop_claude/cycle/interrupt.py b/bin/loop_claude/cycle/interrupt.py index 9408f0fc..8e7adeb9 100644 --- a/bin/loop_claude/cycle/interrupt.py +++ b/bin/loop_claude/cycle/interrupt.py @@ -11,12 +11,31 @@ from claude_code.trust import trust from loop_claude.project.checkout import worktrees ANSWER_QUESTION = "answer the question above" +# The word and the line were picked on 2026-10-17 by a bench of three readers seated as the README's audience, as ~/.claude/CLAUDE.md → "Working loop" → "Bench a syntax with the people who write it." has it. +TALK = "/talk" +ASK_ANSWER = f"Answer ({TALK} opens claude, Enter stops the loop): " -def interrupt(root: str, session_id: uuid.UUID, worker: Rung, path: str, question: str, paths: Paths, env: dict[str, str], effects: Effects) -> Stop | None: +def interrupt(root: str, session_id: uuid.UUID, worker: Rung, path: str, question: str, release_ready: bool, paths: Paths, env: dict[str, str], effects: Effects) -> Stop | str | None: + """Puts question to the human on loop-claude's terminal and returns the answer they typed. Typing TALK instead opens the worker's session in claude on the terminal, and so does a release ready to cut, at once, since only the human may cut it; None is returned once they typed there.""" print(f"\n{question}", file=sys.stderr) if stop := needs_terminal(ANSWER_QUESTION, effects.on_terminal()): return stop + if not release_ready: + try: + answer = effects.input(ASK_ANSWER).strip() + except EOFError: + print(file=sys.stderr) + return needs_human(ANSWER_QUESTION, "the input ended") + if not answer: + return needs_human(ANSWER_QUESTION, "nothing was typed") + if answer != TALK: + return answer + return talk(root, session_id, worker, path, paths, env, effects) + + +def talk(root: str, session_id: uuid.UUID, worker: Rung, path: str, paths: Paths, env: dict[str, str], effects: Effects) -> Stop | None: + """Resumes the worker's session in claude on the terminal; the human must type something there.""" entries = read_transcript(path) if isinstance(entries, Failure): return Stop(1, entries.reason) diff --git a/bin/loop_claude/cycle/judge.py b/bin/loop_claude/cycle/judge.py index 0bc893d6..8564176f 100644 --- a/bin/loop_claude/cycle/judge.py +++ b/bin/loop_claude/cycle/judge.py @@ -3,7 +3,7 @@ import json import re import typing -from loop_claude.cycle.prompts import ANSWER_DESTINATIONS, ANSWERS_RECORDED +from loop_claude.cycle.prompts import ANSWER_DESTINATIONS, ANSWERS_RECORDED, typed_answer from claude_code.reply import claude_reply, Limited from loop_claude.effects import Effects from failure import Failure @@ -11,6 +11,7 @@ from loop_claude.project.records import HUMAN_RULE_NAMES from todo_table import LEVELS from markdown_text import listed from model_routing.route import on_its_scale +from claude_code.transcript import render from loop_claude.cycle.retry import retried from model_routing.ladder import Rung from loop_claude.stop import Stop @@ -18,7 +19,8 @@ from claude_code.entries import structured_output # 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 = ( +# The rules close the judge's prompt, in three parts: when to interrupt and what to ask; when README.md or the records answer the agent in the human's place; and when the chunk is finished. +INTERRUPT_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, and neither " "README.md nor the records settle the matter\n" @@ -37,11 +39,15 @@ JUDGE_RULES = ( "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 records.\n\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\n" + "wording; never construct another from facts it stated." +) +QUOTE_RULES = ( "Where README.md or the records answer the agent's own question and no [loop] line has " "quoted that answer yet, set interrupt=false and quote the passage that answers it; the agent is " "then given that passage in place of the human's reply. An interrupt outranks a quoted answer, and either " - "outranks finished.\n\n" + "outranks finished." +) +FINISHED_RULES = ( "The chunk is the piece of work the agent is on, shipped as one pull request. " "finished=true when the chunk is complete: merged, or nothing remains that the agent could do on " f"its own. Even then, it is finished only once {ANSWERS_RECORDED}. An answer counts as recorded " @@ -50,6 +56,7 @@ JUDGE_RULES = ( f"{listed(HUMAN_RULE_NAMES, 'or')} does not count. finished=false when work remains that a further run can " "continue: an open PR, a review not yet run, CI not yet reported, an answer not yet recorded." ) +JUDGE_RULES = "\n\n".join((INTERRUPT_RULES, QUOTE_RULES, FINISHED_RULES)) @dataclasses.dataclass(frozen=True) @@ -65,6 +72,8 @@ class Reply: finished: bool = dataclasses.field(metadata={"description": "Whether the chunk is finished."}) interrupt: bool = dataclasses.field(metadata={"description": "Whether the human must now be interrupted."}) question: str = dataclasses.field(metadata={"description": "With interrupt true, the single question to put to the human. With a quoted answer, the agent's question that the answer field answers. Empty otherwise."}) + # Opens claude's session for the human at once, in place of asking for a one-line answer: interrupt.interrupt. + release_ready: bool = dataclasses.field(metadata={"description": "Whether interrupt is true for a release ready to cut, which only the human may publish."}) REPLY_TYPES = typing.get_type_hints(Reply) @@ -91,6 +100,7 @@ class Interrupt: escalation: str = "" # The bundled L the judge saw, 0 for none, as on Unfinished: an escalation steps up from the rung that item picks. bundled: int = 0 + release_ready: bool = False @dataclasses.dataclass(frozen=True) @@ -135,7 +145,7 @@ def verdict_from(output: object) -> Verdict | Failure: if output["interrupt"]: if not output["question"].strip(): return Failure("the judge interrupted without a question") - return Interrupt(output["evidence"], output["question"], output["escalation"], bundled) + return Interrupt(output["evidence"], output["question"], output["escalation"], bundled, output["release_ready"]) if output["answer"].strip(): if not output["question"].strip(): return Failure("the judge quoted an answer without a question") @@ -146,13 +156,13 @@ def verdict_from(output: object) -> Verdict | Failure: return Unfinished(output["evidence"], bundled, output["escalation"], bundled_escape_and_impact) -def judge(root: str, task: str, premises: str, records: str, rendered_transcript: str, rung: Rung, effects: Effects, keep: typing.Callable[[Rejected], Stop | None]) -> Verdict | Stop: - return retried("judge", lambda: judge_once(root, task, premises, records, rendered_transcript, rung, effects), effects, lambda failure: keep(failure) if isinstance(failure, Rejected) else None) +def judge(root: str, task: str, premises: str, records: str, entries: list[dict], rung: Rung, effects: Effects, keep: typing.Callable[[Rejected], Stop | None]) -> Verdict | Stop: + return retried("judge", lambda: judge_once(root, task, premises, records, entries, rung, effects), effects, lambda failure: keep(failure) if isinstance(failure, Rejected) else None) -def judge_once(root: str, task: str, premises: str, records: str, rendered_transcript: str, rung: Rung, effects: Effects) -> Verdict | Failure | Limited: - """One judge run, as judge retries it; judge-replay replays it.""" - prompt, shown = judge_prompt(task, premises, records, rendered_transcript) +def judge_once(root: str, task: str, premises: str, records: str, entries: list[dict], rung: Rung, effects: Effects) -> Verdict | Failure | Limited: + """One judge run on the transcript's entries, as judge retries it; judge-replay replays it.""" + prompt, shown = judge_prompt(task, premises, records, render(entries, typed_answer)) return run_judge(root, judge_args(rung), prompt, shown, f"{premises}\n\n{records}", effects) @@ -167,7 +177,12 @@ def judge_args(rung: Rung) -> list[str]: def run_judge(root: str, args: list[str], prompt: str, shown: str, quotable: str, effects: Effects) -> Verdict | Failure | Limited: - """One judge run. Its evidence is checked against shown, its fragments in order: a misquote is a Failure, which judge retries.""" + """One judge run. Each quote in its verdict is checked word for word, since stitched fragments can say what their source does not: + - the evidence, against shown, the transcript as the judge saw it, fragment by fragment in order: a misquote is Rejected, which judge retries; + - a quoted answer, against quotable: a misquote puts the question to the human; + - an escalation, against shown: a misquote drops it, so the worker's rung holds. + quotes_checked checks the last two. Neither misquote is a Failure, which would otherwise exit an unattended loop. + """ reply = claude_reply(effects.claude, args, root, prompt, "the judge") if isinstance(reply, Failure | Limited): return reply @@ -181,11 +196,7 @@ def run_judge(root: str, args: list[str], prompt: str, shown: str, quotable: str def quotes_checked(verdict: Verdict, shown: str, quotable: str) -> Verdict: - """The verdict with its quotes checked word for word, since stitched fragments can say what the records or the transcript do not: - - a quoted answer, against quotable: a misquote puts the question to the human, with the escalation it carried; - - an escalation, on any ruling but a finished one, against shown: a misquote drops it, so the worker's rung holds. - A misquote is no Failure, which would otherwise exit an unattended loop. - """ + """The verdict with its quoted answer and its escalation checked, as run_judge says; a quoted answer put to the human keeps the escalation it carried.""" if isinstance(verdict, QuotedAnswer) and not verbatim(verdict.answer, quotable): verdict = Interrupt(verdict.evidence, verdict.question, verdict.escalation, verdict.bundled) if isinstance(verdict, Interrupt | QuotedAnswer | Unfinished) and verdict.escalation and not verbatim(verdict.escalation, shown): diff --git a/bin/loop_claude/cycle/prompts.py b/bin/loop_claude/cycle/prompts.py index 7f144d5a..ee34cfd4 100644 --- a/bin/loop_claude/cycle/prompts.py +++ b/bin/loop_claude/cycle/prompts.py @@ -124,9 +124,28 @@ def rungs_held(route: Route, anchor: Rung | None) -> str: def answered(question: str, answer: str, route: Route, anchor: Rung | None, stepped: bool) -> str: - """stepped is whether the answer's ruling stepped the worker up, so the prompt names its new rung, as resumed does.""" - on = f" Your request for a higher rung went through: loop-claude runs you on {route.rung}.{rungs_held(route, anchor)} A further request for a higher rung steps you up no more in this cycle." if stepped else "" - return f"Your question: {question}\n\n{QUOTE_SOURCES} answers it:\n\n{answer}\n\nContinue the task.{on}" + """stepped is whether the answer's ruling stepped the worker up.""" + return f"Your question: {question}\n\n{QUOTE_SOURCES} answers it:\n\n{answer}\n\nContinue the task.{stepped_to(route, anchor, stepped)}" + + +def typed(question: str, answer: str, route: Route, anchor: Rung | None, stepped: bool) -> str: + """Resumes the worker with the human's answer to an interrupt. answer is one line, as the terminal reads it, so typed_answer finds where it ends.""" + return f"Your question: {question}\n\n{TYPED_HERE}\n\n{answer}\n\nContinue the task.{stepped_to(route, anchor, stepped)}" + + +# Precedes the answer in typed's prompt. +TYPED_HERE = "The human answers it at loop-claude's terminal:" + + +def typed_answer(prompt: str) -> str | None: + """The answer typed put in prompt, so the judge reads it as the human's; None for any other prompt. The last TYPED_HERE is typed's: the one-line answer cannot hold it, and only the sentences on a stepped rung follow.""" + _, here, after = prompt.rpartition(f"\n\n{TYPED_HERE}\n\n") + return after.split("\n", 1)[0] if here and prompt.startswith("Your question: ") else None + + +def stepped_to(route: Route, anchor: Rung | None, stepped: bool) -> str: + """Where the ruling that brought the answer stepped the worker up, names its new rung, as resumed does.""" + return f" Your request for a higher rung went through: loop-claude runs you on {route.rung}.{rungs_held(route, anchor)} A further request for a higher rung steps you up no more in this cycle." if stepped else "" def past_the_cap(question: str, answer: str, cap: int) -> str: diff --git a/bin/loop_claude/cycle/run.py b/bin/loop_claude/cycle/run.py index 9ac9d7dc..5e5b6045 100644 --- a/bin/loop_claude/cycle/run.py +++ b/bin/loop_claude/cycle/run.py @@ -2,7 +2,7 @@ import dataclasses import pathlib import typing -from loop_claude.cycle.states import Active, Archived, Closing, Ended, Event, HumanAnswered, Interrupting, Judging, QUOTED_ANSWERS_PER_CYCLE, Ruled, State, transition, WorkerRan, Working +from loop_claude.cycle.states import Active, Archived, Closing, Ended, Event, HumanAnswered, Interrupting, Judging, QUOTED_ANSWERS_PER_CYCLE, Ruled, State, transition, TypedAnswer, WorkerRan, Working from transcript_archive.write import archive from claude_code.session import delete_session, Session, session_open, transcript_file from loop_claude.effects import Effects @@ -13,7 +13,7 @@ from loop_claude.routing import judge_rung, stepped_by_ruling from transcript_archive import outcomes from loop_claude.paths import Paths from loop_claude.cycle import prompts -from claude_code.transcript import read_transcript, render +from claude_code.transcript import read_transcript from loop_claude.cycle.retry import retried from model_routing.route import Route from loop_claude.cycle.worker import run_worker, worker_args @@ -41,7 +41,7 @@ class Cycle: # Empty when the previous cycle's verdict was not Unfinished. unfinished_evidence: str # Only an interrupt's or a quoted answer's escalation changes the route within a cycle. No transition reads the route or the fields below, so they stay out of the states. - # The route before that ruling stepped it up, for ask and keep_answer to archive; None unless the latest event stepped the route up. + # The route before that ruling stepped it up, for ask and keep_answer to archive and for ask to tell the worker; None unless the latest event stepped the route up. stepped_from: Route | None = None # Whether an interrupt or a quoted answer of this cycle took an escalation, after which no ruling of the cycle carries one. escalation_spent: bool = False @@ -103,8 +103,8 @@ def step(cycle: Cycle, state: Active) -> Event | Stop: typing.assert_never(state) -def work(cycle: Cycle, answered: QuotedAnswer | None) -> WorkerRan | Stop: - if answered is not None: +def work(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None) -> WorkerRan | Stop: + if isinstance(answered, QuotedAnswer): print(f"\n=== {prompts.QUOTE_SOURCES} answered: {answered.question} ===\n{answered.answer}", flush=True) if stop := keep_answer(cycle, answered): return stop @@ -120,9 +120,11 @@ def worker_environment(cycle: Cycle) -> dict[str, str]: return worker_rungs.environment(cycle.anchor, cycle.route) -def worker_start(cycle: Cycle, answered: QuotedAnswer | None) -> list[str]: - if answered is not None: +def worker_start(cycle: Cycle, answered: QuotedAnswer | TypedAnswer | None) -> list[str]: + if isinstance(answered, QuotedAnswer): return worker_args(cycle.session, cycle.route.rung, prompts.answered(answered.question, answered.answer, cycle.route, cycle.anchor, cycle.stepped_from is not None), resume=True) + if isinstance(answered, TypedAnswer): + return worker_args(cycle.session, cycle.route.rung, prompts.typed(answered.question, answered.answer, cycle.route, cycle.anchor, answered.stepped), resume=True) if session_open(cycle.session): return worker_args(cycle.session, cycle.route.rung, prompts.resumed(cycle.unfinished_evidence, cycle.route, cycle.anchor), resume=True) return worker_args(cycle.session, cycle.route.rung, prompts.fresh(cycle.task, cycle.premises, cycle.route, cycle.anchor, cycle.facts), resume=False) @@ -143,7 +145,7 @@ def rule(cycle: Cycle, transcript_path: str) -> Ruled | Stop: entries = read_transcript(transcript_path) if isinstance(entries, Failure): return Stop(1, entries.reason) - verdict = judge(cycle.root, cycle.task, cycle.premises, cycle.records, render(entries), judge_of(cycle), cycle.effects, lambda rejected: keep_rejection(cycle, transcript_path, rejected)) + verdict = judge(cycle.root, cycle.task, cycle.premises, cycle.records, entries, judge_of(cycle), cycle.effects, lambda rejected: keep_rejection(cycle, transcript_path, rejected)) return verdict if isinstance(verdict, Stop) else Ruled(unspent(verdict, cycle.escalation_spent)) @@ -168,7 +170,12 @@ def ask(cycle: Cycle, transcript_path: str, ruling: Interrupt | QuotedAnswer) -> interrupted = outcomes.interrupted(question, ruling.evidence, str(judge_of(cycle)), ruling.escalation, str(cycle.stepped_from or cycle.route)) if failure := archive_stepped(cycle, transcript_path, interrupted): return Stop(1, failure.reason) - return interrupt(cycle.root, cycle.session.id, cycle.route.rung, transcript_path, question, cycle.paths, worker_environment(cycle), cycle.effects) or HumanAnswered() + release_ready = isinstance(ruling, Interrupt) and ruling.release_ready + answer = interrupt(cycle.root, cycle.session.id, cycle.route.rung, transcript_path, question, release_ready, cycle.paths, worker_environment(cycle), cycle.effects) + if isinstance(answer, Stop): + return answer + # The step is told with the answer, since the worker resumes after the next event, which clears stepped_from. + return HumanAnswered(TypedAnswer(question, answer, cycle.stepped_from is not None) if answer is not None else None) def archive_stepped(cycle: Cycle, transcript_path: str, outcome: str) -> Failure | None: diff --git a/bin/loop_claude/cycle/states.py b/bin/loop_claude/cycle/states.py index b4a68b5a..2636f322 100644 --- a/bin/loop_claude/cycle/states.py +++ b/bin/loop_claude/cycle/states.py @@ -6,10 +6,18 @@ from loop_claude.cycle.judge import Finished, Interrupt, QuotedAnswer, Unfinishe QUOTED_ANSWERS_PER_CYCLE = 3 +@dataclasses.dataclass(frozen=True) +class TypedAnswer: + """The human's answer to an interrupt, typed at loop-claude's terminal; stepped is whether the interrupt stepped the worker up.""" + question: str + answer: str + stepped: bool + + @dataclasses.dataclass(frozen=True) class Working: - """The worker runs, resumed with an answer quoted from README.md or the records when there is one.""" - answered: QuotedAnswer | None = None + """The worker runs, resumed with an answer quoted from README.md or the records, or typed by the human, when there is one.""" + answered: QuotedAnswer | TypedAnswer | None = None quoted_answers: int = 0 @@ -55,7 +63,8 @@ class Ruled: @dataclasses.dataclass(frozen=True) class HumanAnswered: - pass + """typed is None where the human answered in the interrupt's claude session.""" + typed: TypedAnswer | None = None @dataclasses.dataclass(frozen=True) @@ -78,8 +87,10 @@ def transition(state: State, event: Event) -> State: return Interrupting(transcript_path, ruling, quoted_answers) 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, _, quoted_answers), HumanAnswered(): + case Interrupting(_, _, quoted_answers), HumanAnswered(TypedAnswer() as typed): + return Working(typed, quoted_answers) + # The human worked in the interrupt's claude session, so the judge rules again on the transcript it leaves. + case Interrupting(transcript_path, _, quoted_answers), HumanAnswered(None): return Judging(transcript_path, quoted_answers) case Closing(_, verdict), Archived(): return Ended(verdict) diff --git a/bin/loop_claude/loop/main.py b/bin/loop_claude/loop/main.py index 6f3d1757..ccf921dc 100644 --- a/bin/loop_claude/loop/main.py +++ b/bin/loop_claude/loop/main.py @@ -16,13 +16,19 @@ worker made that the premises or the records settle is not asked about. When the question they answer, the judge quotes the answer, loop-claude prints it, and the worker resumes with it. A quote they do not hold word for word is dropped, and the question goes to the human. So does a question asked again after its answer. The fourth question they answer in one cycle goes to -the human too, 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, and the worker is shown the passage the judge quoted as the work remaining. -The next chunk starts a fresh session. +the human too, with the quote. An unfinished chunk resumes its session next cycle, and the worker +is shown the passage the judge quoted as the work remaining. The next chunk starts a fresh session. Cycles continue until an exit below or Ctrl-C. Words: ~/.claude/CLAUDE.md → "Vocabulary". +A question only the human can answer is an interrupt (bin/loop_claude/cycle/interrupt.py). +loop-claude prints the question and reads a one-line answer on its terminal. The worker resumes +headless with the answer. The judge reads it as the human's, so the chunk stays unfinished until the +answer is recorded. An empty answer stops the loop. Typing /talk instead opens the worker's session +on the terminal with `claude --resume`, to read the diff or discuss first; once that claude exits, +the judge rules on what was typed there. A release ready to cut opens that session at once, since +only the human may cut it. Before claude opens, the checkout and each of its worktrees are marked +trusted in ~/.claude.json. + As a chunk starts, before it is routed, loop-claude runs bin/chunk-preflight (bin/chunk_preflight/), which finds the chunk's facts and makes its worktree; chunk-preflight --help lists them, and prompts.preflight_facts in bin/loop_claude/cycle/prompts.py quotes them in the first prompt. A @@ -147,7 +153,8 @@ Exit 1, saying why, when: Exit 2, when the human is needed and cannot answer: - stdin or stdout is not a terminal, when a model must be picked or an interrupt answered - the input ends while a model is picked -- an interrupt ends with nothing typed +- an interrupt's answer is empty or the input ends, or the claude that /talk opened ends with + nothing typed Rerunning resumes any open session. """ diff --git a/bin/loop_claude/test_black_box.py b/bin/loop_claude/test_black_box.py index d5702a90..70a1972e 100644 --- a/bin/loop_claude/test_black_box.py +++ b/bin/loop_claude/test_black_box.py @@ -125,8 +125,8 @@ def worker(**step): return {"role": "worker", **step} -def judge(interrupt=False, finished=False, evidence="working", question="", bundled=0, bundled_escape_and_impact=0, escalation="", **step): - verdict = {"answer": "", "bundled": bundled, "bundled_escape_and_impact": bundled_escape_and_impact, "escalation": escalation, "evidence": evidence, "finished": finished, "interrupt": interrupt, "question": question} +def judge(interrupt=False, finished=False, evidence="working", question="", bundled=0, bundled_escape_and_impact=0, escalation="", release_ready=False, **step): + verdict = {"answer": "", "bundled": bundled, "bundled_escape_and_impact": bundled_escape_and_impact, "escalation": escalation, "evidence": evidence, "finished": finished, "interrupt": interrupt, "question": question, "release_ready": release_ready} return {"role": "judge", "verdict": verdict, **step} @@ -467,7 +467,7 @@ class Cycles(unittest.TestCase): worker(say=f"{cited} Trigger 3 fired; I ask for a higher rung. Shall I use A or B?"), judge(interrupt=True, evidence="Shall I use A or B?", question="A or B?", escalation=cited), interrupt(type="B"), judge(evidence="B", escalation="Trigger 3 fired; I ask for a higher rung."), worker(say="carrying on"), - ], terminal_input="") + ], terminal_input="/talk\n") self.assertEqual(run.code, 130) self.assertEqual(run.roles(), ["worker", "judge", "interrupt", "judge", "worker", "judge"]) self.assertEqual([(argument(call, "--model"), argument(call, "--effort")) for call in run.calls if call["role"] != "judge"], [("claude-opus-5-5", "high"), ("claude-opus-5-5", "xhigh"), ("claude-opus-5-5", "xhigh")]) @@ -655,6 +655,24 @@ class Interrupt(unittest.TestCase): self.assertEqual(run.code, 2) self.assertEqual(len(checkout.archived()), 2) + def test_resumes_the_worker_headless_with_the_answer_typed_at_its_terminal_and_has_the_judge_read_it_as_the_humans(self): + checkout = Checkout(self) + asked = [worker(say="Shall I use A or B?"), judge(interrupt=True, evidence="Shall I use A or B?", question="A or B?")] + run = checkout.run([*asked, worker(say="Using B."), judge(evidence="Using B.")], terminal_input="B\n") + self.assertEqual(run.code, 130) + self.assertEqual(run.roles(), ["worker", "judge", "worker", "judge", "worker"]) + self.assertIn("A or B?\nAnswer (/talk opens claude, Enter stops the loop): ", run.stderr) + resumed = run.calls[2]["argv"] + self.assertEqual(argument(run.calls[2], "--resume"), argument(run.calls[0], "--session-id")) + self.assertEqual(resumed[resumed.index("-p") + 1], "Your question: A or B?\n\nThe human answers it at loop-claude's terminal:\n\nB\n\nContinue the task.") + self.assertIn("[human] B\n\nUsing B.", run.calls[3]["stdin"]) + + for transcript in checkout.transcripts(): + transcript.unlink() + run = checkout.run(asked, terminal_input="\n") + self.assertEqual((run.code, run.roles()), (2, ["worker", "judge"])) + self.assertTrue(run.stderr.strip().endswith("Answer (/talk opens claude, Enter stops the loop): the human is needed to answer the question above, but nothing was typed")) + def test_hands_the_terminal_over_and_exits_2_when_nothing_is_typed(self): checkout = Checkout(self) checkout.git("commit", "--allow-empty", "--quiet", "-m", "start") @@ -663,7 +681,7 @@ class Interrupt(unittest.TestCase): checkout.claude_json().chmod(0o644) asked = [worker(say="Shall I use A or B?"), judge(interrupt=True, evidence="Shall I use A or B?", question="A or B?")] (checkout.root / "sub").mkdir() - run = checkout.run([*asked, interrupt(type="B"), judge(evidence="B"), worker()], cwd=checkout.root / "sub", terminal_input="") + run = checkout.run([*asked, interrupt(type="B"), judge(evidence="B"), worker()], cwd=checkout.root / "sub", terminal_input="/talk\n") self.assertEqual(run.code, 130) self.assertEqual(run.roles(), ["worker", "judge", "interrupt", "judge", "worker", "judge"]) handed = run.calls[2]["argv"] @@ -681,13 +699,13 @@ class Interrupt(unittest.TestCase): for transcript in checkout.transcripts(): transcript.unlink() - run = checkout.run([*asked, interrupt()], terminal_input="") + run = checkout.run([*asked, interrupt()], terminal_input="/talk\n") self.assertEqual((run.code, run.roles()), (2, ["worker", "judge", "interrupt"])) - self.assertEqual(run.stderr.strip().splitlines()[-1], "the human is needed to answer the question above, but the interrupt ended with nothing typed") + self.assertTrue(run.stderr.strip().endswith("the human is needed to answer the question above, but the interrupt ended with nothing typed")) - run = checkout.run([worker(say="A or B?"), judge(interrupt=True, evidence="A or B?", question="A or B?", vanish=True)], terminal_input="") + run = checkout.run([worker(say="A or B?"), judge(interrupt=True, evidence="A or B?", question="A or B?", vanish=True)], terminal_input="/talk\n") self.assertEqual((run.code, run.roles()), (1, ["worker", "judge"])) - self.assertEqual(run.stderr.strip().splitlines()[-1], "could not start claude for the interrupt: No such file or directory") + self.assertTrue(run.stderr.strip().endswith("could not start claude for the interrupt: No such file or directory")) def test_says_why_the_trust_file_cannot_be_read_and_leaves_it_as_it_was(self): checkout = Checkout(self) @@ -705,14 +723,14 @@ class Interrupt(unittest.TestCase): checkout.claude_json().unlink(missing_ok=True) if text is not None: checkout.claude_json().write_text(text) - run = checkout.run(asked, terminal_input="") + run = checkout.run(asked, terminal_input="/talk\n") self.assertEqual(run.code, 2) self.assertIn(f"could not read {checkout.claude_json()}: {said}\n", run.stderr) self.assertEqual(checkout.claude_json().read_text() if checkout.claude_json().exists() else None, text) trusted = json.dumps({"projects": {str(checkout.root): {"hasTrustDialogAccepted": True}}}) checkout.claude_json().write_text(trusted) - run = checkout.run(asked, terminal_input="") + run = checkout.run(asked, terminal_input="/talk\n") self.assertNotIn(str(checkout.claude_json()), run.stderr) self.assertNotIn("trusted for the interrupt", run.stderr) self.assertEqual(checkout.claude_json().read_text(), trusted) @@ -725,7 +743,7 @@ class Interrupt(unittest.TestCase): checkout.run(asked[:2]) checkout.home.chmod(0o500) self.addCleanup(checkout.home.chmod, 0o700) - run = checkout.run([worker(), *asked[1:]], terminal_input="") + run = checkout.run([worker(), *asked[1:]], terminal_input="/talk\n") self.assertEqual(run.code, 2) self.assertIn(f"could not write {checkout.claude_json()}: Permission denied\n", run.stderr) self.assertEqual(checkout.claude_json().read_text(), "{}") @@ -733,7 +751,7 @@ class Interrupt(unittest.TestCase): @unittest.skipIf(os.geteuid() == 0, "root reads an unreadable file") def test_exits_1_when_the_transcript_cannot_be_read_after_the_interrupt(self): checkout = Checkout(self) - run = checkout.run([worker(say="A or B?"), judge(interrupt=True, evidence="A or B?", question="A or B?"), interrupt(type="B", unreadable=True)], terminal_input="") + run = checkout.run([worker(say="A or B?"), judge(interrupt=True, evidence="A or B?", question="A or B?"), interrupt(type="B", unreadable=True)], terminal_input="/talk\n") self.assertEqual((run.code, run.roles()), (1, ["worker", "judge", "interrupt"])) self.assertRegex(run.stderr.strip().splitlines()[-1], r"^could not tell whether you typed anything: could not read the transcript .*\.jsonl: Permission denied$") diff --git a/bin/loop_claude/test_loop_claude.py b/bin/loop_claude/test_loop_claude.py index ae2dc662..e01ddfe4 100644 --- a/bin/loop_claude/test_loop_claude.py +++ b/bin/loop_claude/test_loop_claude.py @@ -105,16 +105,18 @@ def ran(stdout="", code=0, stderr=""): return subprocess.CompletedProcess([], code, stdout, stderr) -def judged(verdict, evidence="working", answer="", escalation=""): - reply = {"answer": answer, "bundled": 0, "bundled_escape_and_impact": 0, "escalation": escalation, "evidence": evidence, "finished": verdict == "finished", "interrupt": verdict == "interrupt", "question": "Which one?" if verdict in ("interrupt", "quoted") else ""} +def judged(verdict, evidence="working", answer="", escalation="", release_ready=False): + reply = {"answer": answer, "bundled": 0, "bundled_escape_and_impact": 0, "escalation": escalation, "evidence": evidence, "finished": verdict == "finished", "interrupt": verdict == "interrupt", "question": "Which one?" if verdict in ("interrupt", "quoted") else "", "release_ready": release_ready} return ran(json.dumps({"structured_output": reply})) class Worker: """A worker claude: writes a line to the session's transcript, then streams a result.""" - def __init__(self, home): + def __init__(self, home, prompted=False): self.home = home + # Whether it records its -p prompt in the transcript first, as claude does. + self.prompted = prompted def __call__(self, args): flag = "--session-id" if "--session-id" in args else "--resume" @@ -123,6 +125,8 @@ class Worker: with open(path, "a") as handle: if flag == "--session-id": handle.write(json.dumps({"type": "custom-title"}) + "\n" + json.dumps({"type": "queue-operation", "timestamp": str(uuid7())}) + "\n") + if self.prompted: + handle.write(json.dumps({**human(args[args.index("-p") + 1]), "promptSource": "sdk"}) + "\n") handle.write(json.dumps(assistant({"type": "text", "text": "working"})) + "\n") return Streamed(0) @@ -139,6 +143,9 @@ class Streamed: return self.code +TYPED = states.TypedAnswer("Which one?", "Use B.", False) + + class Transitions(unittest.TestCase): def test_each_state_moves_on_its_events_and_only_on_them(self): answered = states.Ruled(QuotedAnswer("e", "Which one?", "A.")) @@ -156,14 +163,17 @@ class Transitions(unittest.TestCase): (states.Judging("/t", 3), states.Ruled(Unfinished("e")), states.Closing("/t", Unfinished("e"))), (states.Interrupting("/t", Interrupt("e", "q")), states.HumanAnswered(), states.Judging("/t")), (states.Interrupting("/t", Interrupt("e", "q"), 3), states.HumanAnswered(), states.Judging("/t", 3)), + (states.Interrupting("/t", Interrupt("e", "q")), states.HumanAnswered(TYPED), states.Working(TYPED)), + (states.Interrupting("/t", Interrupt("e", "q"), 3), states.HumanAnswered(TYPED), states.Working(TYPED, 3)), + (states.Working(TYPED, 3), states.WorkerRan("/t"), states.Judging("/t", 3)), (states.Closing("/t", Finished("e")), states.Archived(), states.Ended(Finished("e"))), (states.Closing("/t", Unfinished("e")), states.Archived(), states.Ended(Unfinished("e"))), ) for state, event, after in legal: with self.subTest(state=state, event=event): self.assertEqual(states.transition(state, event), after) - every_state = (states.Working(), states.Working(QuotedAnswer("e", "Which one?", "A."), 3), states.Judging("/t"), states.Judging("/t", 3), states.Interrupting("/t", Interrupt("e", "q")), states.Interrupting("/t", Interrupt("e", "q"), 3), states.Closing("/t", Finished("e")), states.Ended(Finished("e"))) - every_event = (states.WorkerRan("/t"), states.Ruled(Interrupt("e", "Which one?")), answered, states.Ruled(Finished("e")), states.Ruled(Unfinished("e")), states.HumanAnswered(), states.Archived()) + every_state = (states.Working(), states.Working(QuotedAnswer("e", "Which one?", "A."), 3), states.Working(TYPED, 3), states.Judging("/t"), states.Judging("/t", 3), states.Interrupting("/t", Interrupt("e", "q")), states.Interrupting("/t", Interrupt("e", "q"), 3), states.Closing("/t", Finished("e")), states.Ended(Finished("e"))) + every_event = (states.WorkerRan("/t"), states.Ruled(Interrupt("e", "Which one?")), answered, states.Ruled(Finished("e")), states.Ruled(Unfinished("e")), states.HumanAnswered(), states.HumanAnswered(TYPED), states.Archived()) for state in every_state: for event in every_event: if (state, event) not in [(state, event) for state, event, _ in legal]: @@ -218,9 +228,10 @@ class Cycles(unittest.TestCase): with open(session.transcript_file(self.session), "a") as handle: handle.write(json.dumps(human("the second")) + "\n") return 0 - fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt"), judged("finished")], on_terminal_claude=[answer], git=[ran()]) + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt"), judged("finished")], inputs=["/talk"], on_terminal_claude=[answer], git=[ran()]) cycle = dataclasses.replace(self.cycle(fake), anchor=Rung("claude-opus-5-5", "medium"), route=Route(Rung("claude-haiku-5-5", "max"), 163, 1, 5)) - self.assertEqual(run.run_cycle(cycle).verdict, Finished("working")) + with contextlib.redirect_stderr(io.StringIO()): + self.assertEqual(run.run_cycle(cycle).verdict, Finished("working")) self.assertEqual(fake.terminal_environments, fake.worker_environments) self.assertEqual(fake.terminal_environments[0]["LOOP_CLAUDE_WORKER_RUNG"], "claude-haiku-5-5 at max effort") @@ -308,13 +319,14 @@ class Cycles(unittest.TestCase): fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("unfinished")]) self.assertEqual(run.run_cycle(self.cycle(fake)), Stop(1, f"could not archive the transcript: {locked}: Permission denied")) - def test_an_interrupt_is_archived_answered_and_judged_again(self): + def test_an_interrupt_talked_over_in_claude_is_archived_answered_and_judged_again(self): def answer(args): with open(session.transcript_file(self.session), "a") as handle: handle.write(json.dumps(human("the second")) + "\n") return 0 - fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt"), judged("unfinished")], on_terminal_claude=[answer], git=[ran()]) - self.assertEqual(run.run_cycle(self.cycle(fake)).verdict, Unfinished("working")) + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt"), judged("unfinished")], inputs=["/talk"], on_terminal_claude=[answer], git=[ran()]) + with contextlib.redirect_stderr(io.StringIO()): + self.assertEqual(run.run_cycle(self.cycle(fake)).verdict, Unfinished("working")) self.assertEqual(self.archived_outcomes(), ["Task: Do it.\n\n2026-09-21T14:13:20Z Verdict: interrupt\n\nWhich one?\n\nEvidence:\n> working\n\nJudge: judge-model\nWorker: worker-model\n", "Task: Do it.\n\n2026-09-21T14:13:20Z Verdict: unfinished\n\nEvidence:\n> working\n\nJudge: judge-model\nWorker: worker-model\n"]) self.assertIn(str(self.session.id), fake.made("claude_on_terminal")[0]) judge_prompts = [stdin for args, stdin in fake.made("claude")] @@ -326,16 +338,16 @@ class Cycles(unittest.TestCase): def test_a_question_the_records_answer_resumes_the_worker_with_the_answer_three_times_a_cycle(self): answered = judged("quoted", answer="Use *A*.") - fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)] * 4, claude=[answered] * 4, on_terminal_claude=[0], git=[ran()]) + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)] * 4, claude=[answered] * 4, inputs=[""]) with contextlib.redirect_stderr(io.StringIO()) as said, contextlib.redirect_stdout(io.StringIO()) as printed: - self.assertEqual(run.run_cycle(self.cycle(fake, records="From AGENTS.md:\nUse A.")), Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed")) + self.assertEqual(run.run_cycle(self.cycle(fake, records="From AGENTS.md:\nUse A.")), Stop(2, "the human is needed to answer the question above, but nothing was typed")) resumed = fake.made("stream_claude")[1] self.assertEqual(resumed[resumed.index("-p") + 1], "Your question: Which one?\n\nREADME.md's Goals and Audience, AGENTS.md, docs/decisions.md, ~/.claude/CLAUDE.md, ~/.claude/rules/ or ~/.claude/skills/larv-review/standards/ answers it:\n\nUse *A*.\n\nContinue the task.") self.assertEqual(len(fake.made("stream_claude")), 4) self.assertEqual(printed.getvalue().count("=== README.md's Goals and Audience, AGENTS.md, docs/decisions.md, ~/.claude/CLAUDE.md, ~/.claude/rules/ or ~/.claude/skills/larv-review/standards/ answered: Which one? ===\nUse *A*."), 3) self.assertIn("Which one?\n\nREADME.md's Goals and Audience, AGENTS.md, docs/decisions.md, ~/.claude/CLAUDE.md, ~/.claude/rules/ or ~/.claude/skills/larv-review/standards/ says:\nUse *A*.\n\nThose files have answered 3 questions this cycle", said.getvalue()) quoted = "2026-09-21T14:13:20Z Verdict: quoted answer\n\nWhich one?\n\nREADME.md's Goals and Audience, AGENTS.md, docs/decisions.md, ~/.claude/CLAUDE.md, ~/.claude/rules/ or ~/.claude/skills/larv-review/standards/ says:\n> Use *A*.\n\nEvidence:\n> working\n\nJudge: judge-model\nWorker: worker-model\n" - self.assertEqual(self.archived_outcomes(), [f"Task: Do it.\n\n2026-09-21T14:13:20Z Verdict: interrupt\n\nWhich one?\n\nREADME.md's Goals and Audience, AGENTS.md, docs/decisions.md, ~/.claude/CLAUDE.md, ~/.claude/rules/ or ~/.claude/skills/larv-review/standards/ says:\nUse *A*.\n\nThose files have answered 3 questions this cycle, the most allowed, so this one comes to you.\n\nEvidence:\n> working\n\nJudge: judge-model\nWorker: worker-model\n\n2026-09-21T14:13:20Z Stopped: the human is needed to answer the question above, but the interrupt ended with nothing typed\n\nWorker: worker-model\n", *[f"Task: Do it.\n\n{quoted}"] * 3]) + self.assertEqual(self.archived_outcomes(), [f"Task: Do it.\n\n2026-09-21T14:13:20Z Verdict: interrupt\n\nWhich one?\n\nREADME.md's Goals and Audience, AGENTS.md, docs/decisions.md, ~/.claude/CLAUDE.md, ~/.claude/rules/ or ~/.claude/skills/larv-review/standards/ says:\nUse *A*.\n\nThose files have answered 3 questions this cycle, the most allowed, so this one comes to you.\n\nEvidence:\n> working\n\nJudge: judge-model\nWorker: worker-model\n\n2026-09-21T14:13:20Z Stopped: the human is needed to answer the question above, but nothing was typed\n\nWorker: worker-model\n", *[f"Task: Do it.\n\n{quoted}"] * 3]) def test_a_quoted_answer_that_cites_an_escalation_trigger_steps_the_worker_up_before_it_resumes_and_archives_the_step(self): fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)] * 2, claude=[judged("quoted", answer="Use A.", escalation="working"), judged("finished")]) @@ -355,21 +367,56 @@ class Cycles(unittest.TestCase): def test_a_quoted_answer_past_the_cap_archives_the_escalation_it_carried_with_the_interrupt_and_steps_up(self): steps = [judged("quoted", answer="Use A.")] * 3 + [judged("quoted", answer="Use A.", escalation="working")] - fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)] * 4, claude=steps, on_terminal_claude=[0], git=[ran()]) + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)] * 4, claude=steps, inputs=[""]) cycle = dataclasses.replace(self.cycle(fake, records="From AGENTS.md:\nUse A."), anchor=Routing.OPUS, route=Route(Routing.SONNET, 179, 2, 5)) with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): - self.assertEqual(run.run_cycle(cycle), Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed")) + self.assertEqual(run.run_cycle(cycle), Stop(2, "the human is needed to answer the question above, but nothing was typed")) kept = "".join(self.archived_outcomes()) self.assertIn(f"Evidence:\n> working\n\nEscalation:\n> working\n\nJudge: judge-model\nWorker: {Route(Routing.SONNET, 179, 2, 5)}\n", kept) self.assertIn(f"Stepped up\n\nWorker: {Route(Routing.OPUS, 179, 2, 5)}\n", kept) + def test_an_answer_typed_at_the_terminal_resumes_the_worker_headless_and_the_judge_reads_it_as_the_humans(self): + worker = Worker(self.paths.claude_code_transcripts.parent, prompted=True) + fake = FakeEffects(streams=[worker, worker], claude=[judged("interrupt"), judged("unfinished")], inputs=["Use B."]) + with contextlib.redirect_stderr(io.StringIO()) as said: + self.assertEqual(run.run_cycle(self.cycle(fake)).verdict, Unfinished("working")) + self.assertIn("\nWhich one?\n", said.getvalue()) + self.assertEqual(fake.made("claude_on_terminal"), []) + resumed = fake.made("stream_claude")[1] + self.assertEqual(resumed[resumed.index("--resume") + 1], str(self.session.id)) + self.assertEqual(resumed[resumed.index("-p") + 1], "Your question: Which one?\n\nThe human answers it at loop-claude's terminal:\n\nUse B.\n\nContinue the task.") + judge_prompts = [stdin for args, stdin in fake.made("claude")] + self.assertTrue(judge_prompts[1].split("\n\nDecide whether")[0].endswith("[human] Use B.\n\nworking")) + self.assertNotIn("[loop] Your question", judge_prompts[1]) + + def test_an_interrupt_that_steps_the_worker_up_names_its_new_rung_in_the_prompt_that_resumes_it_with_the_typed_answer(self): + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)] * 2, claude=[judged("interrupt", escalation="working"), judged("finished")], inputs=["Use B."]) + cycle = dataclasses.replace(self.cycle(fake), anchor=Routing.OPUS, route=Route(Routing.SONNET, 179, 2, 5)) + with contextlib.redirect_stderr(io.StringIO()): + concluded = run.run_cycle(cycle) + self.assertEqual(concluded.route, Route(Routing.OPUS, 179, 2, 5)) + first, resumed = fake.made("stream_claude") + self.assertEqual([args[args.index("--effort") + 1] for args in (first, resumed)], ["high", "medium"]) + self.assertTrue(resumed[resumed.index("-p") + 1].endswith(f"Use B.\n\nContinue the task. Your request for a higher rung went through: loop-claude runs you on {Routing.OPUS}.{prompts.rungs_held(concluded.route, Routing.OPUS)} A further request for a higher rung steps you up no more in this cycle.")) + + def test_a_release_ready_to_cut_opens_claudes_session_without_asking_for_an_answer(self): + def answer(args): + with open(session.transcript_file(self.session), "a") as handle: + handle.write(json.dumps(human("cut")) + "\n") + return 0 + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt", release_ready=True), judged("finished")], on_terminal_claude=[answer], git=[ran()]) + with contextlib.redirect_stderr(io.StringIO()): + self.assertEqual(run.run_cycle(self.cycle(fake)).verdict, Finished("working")) + self.assertEqual(fake.made("input"), []) + self.assertEqual(len(fake.made("claude_on_terminal")), 1) + def test_stops_when_an_interrupt_cannot_be_archived(self): self.archive.parent.mkdir(parents=True) self.archive.write_text("") - fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt")], on_terminal_claude=[0]) + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("interrupt")], inputs=["Use B."]) stop = run.run_cycle(self.cycle(fake)) self.assertRegex(stop.reason, f"^could not archive the transcript: {self.archive}/[0-9a-f]{{64}}: Not a directory$") - self.assertEqual(fake.made("claude_on_terminal"), []) + self.assertEqual(fake.made("input"), []) def test_stops_when_a_quoted_answer_cannot_be_archived(self): self.archive.parent.mkdir(parents=True) @@ -381,9 +428,9 @@ class Cycles(unittest.TestCase): self.assertEqual(len(fake.made("stream_claude")), 1) def test_an_answer_the_records_do_not_hold_is_dropped_and_the_question_goes_to_the_human(self): - fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("quoted", answer="Use ... A.")], on_terminal_claude=[0], git=[ran()]) + fake = FakeEffects(streams=[Worker(self.paths.claude_code_transcripts.parent)], claude=[judged("quoted", answer="Use ... A.")], inputs=[""]) with contextlib.redirect_stderr(io.StringIO()) as said: - self.assertEqual(run.run_cycle(self.cycle(fake, records="From AGENTS.md:\nUse B. Use A.")), Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed")) + self.assertEqual(run.run_cycle(self.cycle(fake, records="From AGENTS.md:\nUse B. Use A.")), Stop(2, "the human is needed to answer the question above, but nothing was typed")) self.assertEqual(len(fake.made("stream_claude")), 1) self.assertIn("\nWhich one?\n", said.getvalue()) self.assertNotIn("Use ... A.", said.getvalue()) @@ -466,28 +513,59 @@ class Limits(unittest.TestCase): class Interrupts(unittest.TestCase): - def test_stops_off_a_terminal_before_starting_claude(self): + def ask(self, fake, home="/h", path="/t", rung=Rung("m"), env=None, release_ready=False): + with contextlib.redirect_stderr(io.StringIO()) as said: + asked = interrupt.interrupt(home, uuid7(), rung, path, "Which one?", release_ready, paths.paths_from(pathlib.Path(home)), env or {}, fake.effects()) + self.said = said.getvalue() + return asked + + def test_stops_off_a_terminal_before_asking(self): fake = FakeEffects(on_terminal=False) - stop = interrupt.interrupt("/r", uuid7(), Rung("m"), "/t", "Which one?", paths.paths_from(pathlib.Path("/h")), {}, fake.effects()) - self.assertEqual(stop, Stop(2, "the human is needed to answer the question above, but stdin or stdout is not a terminal; rerun loop-claude on a terminal")) + self.assertEqual(self.ask(fake), Stop(2, "the human is needed to answer the question above, but stdin or stdout is not a terminal; rerun loop-claude on a terminal")) self.assertEqual(fake.calls, []) - def test_stops_when_nothing_was_typed_in_the_handover(self): + def test_returns_the_answer_typed_at_the_terminal_without_starting_claude(self): + fake = FakeEffects(inputs=[" Use B. "]) + self.assertEqual(self.ask(fake), "Use B.") + self.assertEqual(self.said, "\nWhich one?\n") + self.assertEqual(fake.made("input"), ["Answer (/talk opens claude, Enter stops the loop): "]) + self.assertEqual(fake.made("claude_on_terminal"), []) + + def test_stops_on_an_empty_answer_or_the_inputs_end(self): + for typed, why in ((" ", "nothing was typed"), (EOFError(), "the input ended")): + with self.subTest(why=why): + fake = FakeEffects(inputs=[typed]) + self.assertEqual(self.ask(fake), Stop(2, f"the human is needed to answer the question above, but {why}")) + self.assertEqual(fake.made("claude_on_terminal"), []) + + def test_opens_claudes_session_on_talk_and_at_once_for_a_release_ready_to_cut(self): + for typed, release_ready in ((["/talk"], False), ([" /talk "], False), ([], True)): + with self.subTest(typed=typed), tempfile.TemporaryDirectory() as home: + path = pathlib.Path(home, "t.jsonl") + path.write_text(json.dumps(human("a")) + "\n") + def answer(args): + with open(path, "a") as handle: + handle.write(json.dumps(human("B")) + "\n") + return 0 + fake = FakeEffects(inputs=typed, on_terminal_claude=[answer], git=[ran()]) + self.assertIsNone(self.ask(fake, home, str(path), Rung("m", "high"), release_ready=release_ready)) + self.assertEqual(fake.made("claude_on_terminal")[0][:4], ["--model", "m", "--effort", "high"]) + self.assertEqual(len(fake.made("input")), len(typed)) + + def test_stops_when_nothing_was_typed_in_the_session(self): with tempfile.TemporaryDirectory() as home: path = pathlib.Path(home, "t.jsonl") path.write_text(json.dumps(human("a")) + "\n") - fake = FakeEffects(on_terminal_claude=[0], git=[ran()]) - stop = interrupt.interrupt(home, uuid7(), Rung("m", "high"), str(path), "Which one?", paths.paths_from(pathlib.Path(home)), {}, fake.effects()) - self.assertEqual(fake.made("claude_on_terminal")[0][:4], ["--model", "m", "--effort", "high"]) - self.assertEqual(stop, Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed")) + fake = FakeEffects(inputs=["/talk"], on_terminal_claude=[0], git=[ran()]) + self.assertEqual(self.ask(fake, home, str(path)), Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed")) def test_hands_the_claude_that_takes_the_terminal_the_workers_environment(self): with tempfile.TemporaryDirectory() as home: path = pathlib.Path(home, "t.jsonl") path.write_text(json.dumps(human("a")) + "\n") - fake = FakeEffects(on_terminal_claude=[0], git=[ran()]) + fake = FakeEffects(inputs=["/talk"], on_terminal_claude=[0], git=[ran()]) handed = {"LOOP_CLAUDE_PROJECT_RUNG": "claude-opus-5-5 at medium effort", "LOOP_CLAUDE_WORKER_RUNG": "claude-haiku-5-5 at max effort"} - interrupt.interrupt(home, uuid7(), Rung("claude-haiku-5-5", "max"), str(path), "Which one?", paths.paths_from(pathlib.Path(home)), handed, fake.effects()) + self.ask(fake, home, str(path), Rung("claude-haiku-5-5", "max"), handed) self.assertEqual(fake.terminal_environments, [handed]) @@ -897,6 +975,15 @@ class PythonEffect(unittest.TestCase): self.assertIn(gone, done.stderr) +class TypedAnswers(unittest.TestCase): + def test_finds_the_answer_in_the_prompt_that_hands_it_over_and_in_no_other_prompt(self): + route = Route(Rung("worker-model")) + self.assertEqual(prompts.typed_answer(prompts.typed("Which one?\n\nThe human answers it at loop-claude's terminal:\n\nA?", "Use B.", route, None, False)), "Use B.") + self.assertEqual(prompts.typed_answer(prompts.typed("Which one?", "Use B.", Route(Routing.SONNET, 179, 2, 5), Routing.OPUS, True)), "Use B.") + self.assertIsNone(prompts.typed_answer(prompts.answered("Which one?", "Use B.", route, None, False))) + self.assertIsNone(prompts.typed_answer(prompts.resumed("Your question: A\n\nThe human answers it at loop-claude's terminal:\n\nB", route, None))) + + class WorkerArgs(unittest.TestCase): SCAN = " Run a full scan only from such a subagent, since the scan's agents run on the model that starts them." SEATS = " Launch each comprehension seat and the security pass's subagent with the Agent tool set to model opus, effort medium, the project's rung." @@ -1084,19 +1171,21 @@ class Grounded(unittest.TestCase): class Verdict(unittest.TestCase): def test_needs_every_field_the_schema_requires(self): - self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": False, "evidence": "e", "finished": True, "question": ""}), Finished("e")) - self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": False, "evidence": "e", "finished": False, "question": "ignored"}), Unfinished("e")) - self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": True, "evidence": "e", "finished": True, "question": "Which one?"}), Interrupt("e", "Which one?")) - self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "A.", "interrupt": False, "evidence": "e", "finished": False, "question": "Which one?"}), QuotedAnswer("e", "Which one?", "A.")) - self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "A.", "interrupt": True, "evidence": "e", "finished": False, "question": "Which one?"}), Interrupt("e", "Which one?")) - self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "A.", "interrupt": False, "evidence": "e", "finished": True, "question": ""}), Failure("the judge quoted an answer without a question")) - self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": "no", "evidence": "", "finished": True, "question": ""}), Failure("the judge's reply has no interrupt of type boolean")) - self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": False, "evidence": None, "finished": True, "question": ""}), Failure("the judge's reply has no evidence of type string")) - self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": True, "evidence": "x", "finished": False, "question": " "}), Failure("the judge interrupted without a question")) + self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": False, "evidence": "e", "finished": True, "question": "", "release_ready": False}), Finished("e")) + self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": False, "evidence": "e", "finished": False, "question": "ignored", "release_ready": False}), Unfinished("e")) + self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": True, "evidence": "e", "finished": True, "question": "Which one?", "release_ready": False}), Interrupt("e", "Which one?")) + self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": True, "evidence": "e", "finished": False, "question": "Cut 0.2.0?", "release_ready": True}), Interrupt("e", "Cut 0.2.0?", release_ready=True)) + self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": True, "evidence": "e", "finished": False, "question": "Cut 0.2.0?"}), Failure("the judge's reply has no release_ready of type boolean")) + self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "A.", "interrupt": False, "evidence": "e", "finished": False, "question": "Which one?", "release_ready": False}), QuotedAnswer("e", "Which one?", "A.")) + self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "A.", "interrupt": True, "evidence": "e", "finished": False, "question": "Which one?", "release_ready": False}), Interrupt("e", "Which one?")) + self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "A.", "interrupt": False, "evidence": "e", "finished": True, "question": "", "release_ready": False}), Failure("the judge quoted an answer without a question")) + self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": "no", "evidence": "", "finished": True, "question": "", "release_ready": False}), Failure("the judge's reply has no interrupt of type boolean")) + self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": False, "evidence": None, "finished": True, "question": "", "release_ready": False}), Failure("the judge's reply has no evidence of type string")) + self.assertEqual(verdict_from({"bundled": 0, "bundled_escape_and_impact": 0, "escalation": "", "answer": "", "interrupt": True, "evidence": "x", "finished": False, "question": " ", "release_ready": False}), Failure("the judge interrupted without a question")) self.assertEqual(verdict_from(None), Failure("the judge returned no reply")) def test_takes_a_bundled_l_on_its_scale_and_counts_any_other_as_none(self): - reply = {"answer": "", "bundled_escape_and_impact": 0, "escalation": "", "interrupt": False, "evidence": "e", "finished": False, "question": ""} + reply = {"answer": "", "bundled_escape_and_impact": 0, "escalation": "", "interrupt": False, "evidence": "e", "finished": False, "question": "", "release_ready": False} self.assertEqual(verdict_from({**reply, "bundled": 4}), Unfinished("e", 4)) self.assertEqual(verdict_from({**reply, "bundled": True}), Failure("the judge's reply has no bundled of type integer")) for bundled in (5, 13, -1): @@ -1105,7 +1194,7 @@ class Verdict(unittest.TestCase): def test_takes_a_bundled_e_plus_i_on_its_scale_and_counts_any_other_as_none(self): - reply = {"answer": "", "bundled": 0, "escalation": "", "interrupt": False, "evidence": "e", "finished": False, "question": ""} + reply = {"answer": "", "bundled": 0, "escalation": "", "interrupt": False, "evidence": "e", "finished": False, "question": "", "release_ready": False} self.assertEqual(verdict_from({**reply, "bundled_escape_and_impact": 6}), Unfinished("e", 0, bundled_escape_and_impact=6)) self.assertEqual(verdict_from({**reply, "bundled_escape_and_impact": 2}), Unfinished("e", 0, bundled_escape_and_impact=2)) self.assertEqual(verdict_from({**reply, "bundled_escape_and_impact": False}), Failure("the judge's reply has no bundled_escape_and_impact of type integer")) @@ -1114,13 +1203,13 @@ class Verdict(unittest.TestCase): self.assertEqual(verdict_from({**reply, "bundled_escape_and_impact": bundled}), Unfinished("e", 0)) def test_takes_an_escalation_quote_on_an_unfinished_verdict(self): - reply = {"answer": "", "bundled": 0, "bundled_escape_and_impact": 0, "escalation": "round 2: 🔴", "interrupt": False, "evidence": "e", "finished": False, "question": ""} + reply = {"answer": "", "bundled": 0, "bundled_escape_and_impact": 0, "escalation": "round 2: 🔴", "interrupt": False, "evidence": "e", "finished": False, "question": "", "release_ready": False} self.assertEqual(verdict_from(reply), Unfinished("e", 0, "round 2: 🔴")) self.assertEqual(verdict_from({**reply, "finished": True}), Finished("e")) self.assertEqual(verdict_from({**reply, "escalation": None}), Failure("the judge's reply has no escalation of type string")) def test_takes_an_escalation_quote_on_an_interrupt(self): - reply = {"answer": "", "bundled": 0, "bundled_escape_and_impact": 0, "escalation": "round 2: 🔴", "interrupt": True, "evidence": "e", "finished": False, "question": "Which one?"} + reply = {"answer": "", "bundled": 0, "bundled_escape_and_impact": 0, "escalation": "round 2: 🔴", "interrupt": True, "evidence": "e", "finished": False, "question": "Which one?", "release_ready": False} self.assertEqual(verdict_from(reply), Interrupt("e", "Which one?", "round 2: 🔴")) self.assertEqual(verdict_from({**reply, "bundled": 4}), Interrupt("e", "Which one?", "round 2: 🔴", 4)) self.assertEqual(verdict_from({**reply, "bundled": 5}), Interrupt("e", "Which one?", "round 2: 🔴")) @@ -1132,7 +1221,7 @@ class Verdict(unittest.TestCase): self.assertEqual(judge.quotes_checked(Unfinished("e", 0, "round 3: 🔴"), shown, ""), Unfinished("e")) def test_takes_an_escalation_quote_and_a_bundled_l_on_a_quoted_answer(self): - reply = {"answer": "A.", "bundled": 4, "bundled_escape_and_impact": 0, "escalation": "round 2: 🔴", "interrupt": False, "evidence": "e", "finished": False, "question": "Which one?"} + reply = {"answer": "A.", "bundled": 4, "bundled_escape_and_impact": 0, "escalation": "round 2: 🔴", "interrupt": False, "evidence": "e", "finished": False, "question": "Which one?", "release_ready": False} self.assertEqual(verdict_from(reply), QuotedAnswer("e", "Which one?", "A.", "round 2: 🔴", 4)) self.assertEqual(verdict_from({**reply, "bundled": 5}), QuotedAnswer("e", "Which one?", "A.", "round 2: 🔴")) @@ -1227,9 +1316,9 @@ class ExitContract(unittest.TestCase): unreadable = f"README.md, or {', '.join(names[:-1])} or {names[-1]} where they exist, cannot be read or is not UTF-8" # Every Stop whose code is not a literal 0, 2 or 130, in source order: path, function, code, start of reason, its --help clause. clauses = [ - ("cycle/interrupt.py", "interrupt", "1", "entries.reason", read), - ("cycle/interrupt.py", "interrupt", "1", "f'could not start claude for the interrupt:", "claude cannot start for an interrupt"), - ("cycle/interrupt.py", "interrupt", "1", "f'could not tell whether you typed anything:", read), + ("cycle/interrupt.py", "talk", "1", "entries.reason", read), + ("cycle/interrupt.py", "talk", "1", "f'could not start claude for the interrupt:", "claude cannot start for an interrupt"), + ("cycle/interrupt.py", "talk", "1", "f'could not tell whether you typed anything:", read), ("cycle/retry.py", "retried", "1", "f'the {who} failed three times running:", "the worker or the judge fails three times running"), ("cycle/run.py", "stopped", "1", "f'{failure.reason}", read), ("cycle/run.py", "work", "1", "f'claude left no transcript for session", "claude leaves no transcript"), diff --git a/docs/decisions.md b/docs/decisions.md index 0131a346..0c15e7c4 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -22,11 +22,13 @@ 2026-10-13, implementing todo item 104: the judge reads each file under `~/.claude/skills/larv-review/standards/` whole too, for the same reason, as the `worker:` decision moved rules there. +2026-10-17, implementing todo item 168: the human answers an interrupt on loop-claude's terminal, and the worker resumes headless with the answer in a `-p` prompt. The judge reads that answer as a `[human]` line, not a `[loop]` one, so it holds the chunk open until the answer is recorded, as it does for an answer typed in claude. + ## loop-claude: one session per project, its id derived from the checkout's path relative to `~/code`, or to `/` outside it 2026-09-21, lilleman; revised 2026-09-30 by lilleman. Serves goal 1. One fixed id per project keeps the session picker at one entry per project and makes any transcript under that id loop-claude's own to delete. A checkout outside `~/code` mirroring a path inside it shares that id, and a second loop on it is not refused. Once `todo.md` item 5 lands, the id comes from the full path, so no two checkouts share one, and `loop-claude` refuses to start while another session on that id is active. Moving the checkout orphans its open sessions, archive folder and models entry: sessions stay in the picker until the human deletes them, migration code not being worth a one-human audience. Valid while claude takes `--session-id` and resumes by it. -## loop-claude: before an interrupt, it marks the checkout and each of its worktrees trusted in `~/.claude.json` +## loop-claude: before it opens claude on the terminal, it marks the checkout and each of its worktrees trusted in `~/.claude.json` 2026-09-21, lilleman; the lost-write risk accepted 2026-09-24 by larv-review. Serves goal 1. A write by another claude inside `trust`'s read-modify-write window is lost. @@ -34,6 +36,8 @@ 2026-09-30, lilleman: Claude Code owns and writes that file, so `loop-claude` is to move out of it, bringing each case that must stay to the human. Stands until `todo.md` item 6 ends it. +2026-10-17, implementing todo item 168: the human answers an interrupt on loop-claude's own terminal, so claude opens on the terminal only when they type `/talk`, for a release ready to cut, and for the routing calls; the trust is marked only then. + ## bin/: the tests rerun themselves on a `uv`-fetched Python at README Install's floor 2026-09-26, larv-review. Serves goal 2. The suite reruns itself on a `uv`-fetched interpreter of the version Install names: `ast.parse(feature_version=)` on the host's Python accepts the nested f-string 186181a fixed, a compile check passes a call newer than the floor, like 3.12's `itertools.batched`, and a `python3.N` on `PATH` is installed by hand on every machine and again at each bump. Valid while `uv` can fetch the floor interpreter and no machine taking the checkout ships an older Python. @@ -288,7 +292,7 @@ Valid while the ladder orders its rungs by competence. 2026-10-15, lilleman asked for it. Serves goals 1.1 and 1.4: when the hard part is the rest of the chunk, handing it to subagents piece by piece leaves the worker, which reads and checks each of their diffs, on a rung that proved too low. The worker climbs only once its run records a trigger. -loop-claude's prompt offers the step beside the escalated subagent of `routing: a worker hands a task one rung up`, as long as the limit leaves a rung that subagent may take without the human's yes. The worker then ends its run asking loop-claude for a higher rung and naming the trigger. The judge quotes an earlier passage that shows the trigger fired, such as a review round's commit title or the worker's report of the failing test, never the request itself. The trigger must have fired since any earlier such request, so one trigger buys one step. The judge sees the worker's text and tool calls but no tool results, so the quote shows that the run recorded the trigger, not that a tool's output bears it out. loop-claude checks the quote word for word, as it checks a quoted answer, since stitched fragments can make up a trigger. Without such a quote, the rung holds. A misquote is no failure, since holding the rung is safe and three failures stop an unattended loop. The next cycle's prompt names the worker's rung, so the worker learns whether the step went through. A worker that a quoted answer stepped up learns it sooner, from the prompt that resumes it with the answer. +loop-claude's prompt offers the step beside the escalated subagent of `routing: a worker hands a task one rung up`, as long as the limit leaves a rung that subagent may take without the human's yes. The worker then ends its run asking loop-claude for a higher rung and naming the trigger. The judge quotes an earlier passage that shows the trigger fired, such as a review round's commit title or the worker's report of the failing test, never the request itself. The trigger must have fired since any earlier such request, so one trigger buys one step. The judge sees the worker's text and tool calls but no tool results, so the quote shows that the run recorded the trigger, not that a tool's output bears it out. loop-claude checks the quote word for word, as it checks a quoted answer, since stitched fragments can make up a trigger. Without such a quote, the rung holds. A misquote is no failure, since holding the rung is safe and three failures stop an unattended loop. The next cycle's prompt names the worker's rung, so the worker learns whether the step went through. A worker that a quoted answer, or an interrupt the human answers on loop-claude's terminal, stepped up learns it sooner, from the prompt that resumes it with the answer. A run that asks for a higher rung may also ask a question. The judge then interrupts, or quotes the answer from README.md or the records, and either ruling carries the escalation quote too, checked the same way. loop-claude steps the rung as the ruling comes in, before the human is asked or the worker resumes with the quoted answer, since the work after the answer is work the stepped rung should do. An interrupt or a quoted answer is no unfinished verdict, so it adds to no count. The judge may quote a different passage for the trigger that already stepped the rung, and loop-claude cannot tell that from a new trigger. So once an interrupt or a quoted answer has taken an escalation, no later ruling of that cycle carries one. A trigger that fires after the answer steps nothing in that cycle. diff --git a/docs/reading-cost.md b/docs/reading-cost.md index 5ae353ef..ee66058d 100644 --- a/docs/reading-cost.md +++ b/docs/reading-cost.md @@ -632,3 +632,7 @@ Read most (opens and searches): Read most (opens and searches): - `rules/todo-items.md` 1 + +## Reading cost, chunk 055546870eaf, finished 2026-10-17T20:46:45Z + +100998 new input tokens before the first commit. diff --git a/todo.md b/todo.md index 9ab4e3d5..d0b522e8 100644 --- a/todo.md +++ b/todo.md @@ -13,7 +13,6 @@ | ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score | |---|---|---|---|---|---|---|---|---|---|---|---| | 180 | 0.2.0 | | question | **Settle how a trigger that fires after a step-up at an interrupt or a quoted answer steps the worker, which today it never does.** | 2 | 2 | 2 | 4 | 3 | 1.1 by 3 | 4.1 | -| 168 | 0.2.0 | | | **Have the human answer an interrupt on loop-claude's terminal and resume the worker headless with the answer, opening the interactive session only when they ask for it.** | 4 | 2 | 3 | 7 | 5 | 1.5 by 8 | 1.8 | | 73 | 0.2.0 | | | **Have each reviewer write its full report to a file and return the worker only its findings' one-line summaries.** | 3 | 2 | 3 | 6 | 5 | 1.4 by 4 | 1.1 | | 159 | 0.2.0 | | decision | **Run each review pass on the lowest rung that a replay of its archived rounds shows finding every 🔴 and 🟡 the project's rung finds, even below the worker's rung.** | 4 | 2 | 3 | 7 | 6 | 1.4 by 7 | 1.1 | | 80 | 0.3.0 | | defect | **State in README → Install that every machine taking this checkout must be a sandbox, as `settings.json`'s auto mode assumes.** | 1 | 2 | 1 | 2 | 1 | 2 by 4 | 17.5 | @@ -95,12 +94,6 @@ Options: Only the human can choose, and the answer revises that decision. Found by the stability review of PR #307. -### 168. Have the human answer an interrupt on loop-claude's terminal and resume the worker headless with the answer, opening the interactive session only when they ask for it. - -Today an interrupt runs `claude --resume` on the terminal and waits for it to exit (`bin/loop_claude/cycle/interrupt.py`). Once the human answers, the worker carries on in that interactive session, so the loop waits until the human exits it, often for the rest of the chunk. Instead, the loop prints the question and reads the answer on its own terminal. The next cycle resumes the worker headless with a prompt like `prompts.answered`'s, saying the human gave the answer. A word typed in place of an answer, such as `/talk`, opens today's interactive session, for when the human wants to read the diff or discuss first. An empty answer stops the loop, as an interrupt with nothing typed does today. A release ready to cut still opens the interactive session. Bench the word, and the line that asks for the answer, as `CLAUDE.md` says. - -The judge must treat an answer typed at the loop's terminal as the human's. `bin/claude_code/transcript.py`'s `render` labels every `-p` prompt `[loop]`, so an answer passed on headless would read as one quoted from the records. The judge would then never hold the chunk open until that answer is recorded. The change touches two places the comprehension seats found hard: `bin/loop_claude/cycle/judge.py`'s `run_judge` and `bin/loop_claude/loop/main.py`'s help. Leave each easier to read. When the interrupt stepped the worker's rung up (`Cycle.stepped_from`), have the headless resume prompt name the new rung and repeat `prompts.rungs_held`, as `prompts.answered` does after a quoted answer's step. Otherwise the worker learns of the step only next cycle, though `docs/decisions.md` says the next prompt names it. - ### 73. Have each reviewer write its full report to a file and return the worker only its findings' one-line summaries. 2026-10-08, lilleman: rescored from A10/G8 to A4/G4 on the measurement below, so other items that lower token use go first. @@ -370,7 +363,7 @@ While `rank_todo.py` ranks two shapes, projects follow two sets of scoring rules ### 155. Lower the reading cost of the files a chunk reads most before its first commit. -`bin/reading-cost` on 2026-10-16: the median of the last 5 chunks is 189,487 new input tokens before their first commit, up from 146,584 for the 5 before them. Read most over the last 5, by opens and searches: `todo.md` 51, `README.md` 12, `CHANGELOG.md` 11, `bin/loop_claude/test_loop_claude.py` 11, `AGENTS.md` 7, `bin/todo_table.py` 7, `docs/decisions.md` 7, `agents/stability-reviewer.md` 6, `bin/loop_claude/cycle/judge.py` 5, `bin/rule_hook/test_rule_hook.py` 5. The chunk that ships this item runs `bin/reading-cost --reset`. +`bin/reading-cost` on 2026-10-17: the median of the last 5 chunks is 117,108 new input tokens before their first commit, up from 107,738 for the 5 before them. Read most over the last 5, by opens and searches: `bin/loop_claude/test_loop_claude.py` 14, `todo.md` 11, `bin/loop_claude/test_black_box.py` 10, `bin/rank_todo.py` 6, `CHANGELOG.md` 5, `README.md` 5, `docs/decisions.md` 4, `.gitignore` 3, `bin/loop_claude/routing.py` 2, `bin/markdown_text.py` 2. The chunk that ships this item runs `bin/reading-cost --reset`. ### 1. Say in README → Install how the transcript archive is backed up, once bender-infra backs up ai-sandbox's data volume.