import dataclasses import sys import uuid from loop_claude.effects import Effects from failure import Failure from claude_code.transcript import human_turns, read_transcript from loop_claude.stop import needs_human, needs_terminal, Stop from loop_claude.paths import Paths from model_routing.ladder import Rung 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): " @dataclasses.dataclass(frozen=True) class Talk: """The human answers in the worker's claude session, which talk opens.""" def put_question(question: str, answer_here: bool, effects: Effects) -> str | Talk | Stop: """Prints question for the human. Where answer_here, returns the line they type at loop-claude's terminal, stripped, or Talk where they type TALK; else Talk at once. A Stop where loop-claude is off a terminal, or they type nothing, or the input ends.""" print(f"\n{question}", file=sys.stderr) if stop := needs_terminal(ANSWER_QUESTION, effects.on_terminal()): return stop # Keys typed while the worker ran wait on the terminal, so they would answer a question no one has read, here or in claude's session, which reads them too. A paste's lines after the first are dropped with them at the next question, never taken as its answer; a longer answer goes through TALK. effects.discard_typed() if not answer_here: return Talk() 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") return Talk() if answer == TALK else answer 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) before = human_turns(entries) if unstarted := hand_over(worker, ["--resume", str(session_id)], root, paths, env, effects): return Stop(1, f"could not start claude for the interrupt: {unstarted.reason}") 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") def hand_over(rung: Rung, args: list[str], root: str, paths: Paths, env: dict[str, str], effects: Effects) -> Failure | None: """Marks the checkout and each of its worktrees trusted, then runs claude on the terminal in root, on rung, with env added to its environment; the failure is why claude could not start.""" trust(paths.claude_json, worktrees(root, effects)) try: effects.claude_on_terminal([*rung.flags(), "--permission-mode", "bypassPermissions", *args], root, env) except OSError as failure: return Failure(str(failure.strerror)) return None