Answer architecture round 1: 1 🔴, 6 🟡 (0 misread); open claude through one hand_over, route loop-inbox's /talk through the Talking state, give Ask one state

This commit is contained in:
2026-10-28 23:57:33 +01:00
parent a7fe878631
commit 3409ab3d07
17 changed files with 374 additions and 258 deletions
+53 -51
View File
@@ -1,16 +1,16 @@
import dataclasses
import os
import sys
import typing
import uuid
from loop_claude.waiting import alive, answered, Ask, publish, TalkCommand, Talked, Talking, Typed, withdraw
from loop_claude.waiting import answered, Ask, Asking, claude_ended, ClaudeCommand, ClaudeOpening, OpenInInbox, OpenOnTerminal, POLL_SECONDS, publish, Typed, withdraw
from loop_claude.effects import Effects, say
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 loop_claude.cycle.states import Talk
from claude_code.trust import trust
from loop_claude.project.checkout import worktrees
@@ -18,13 +18,6 @@ ANSWER_QUESTION = "answer the question above"
# TALK and ASK_ANSWER 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): "
# Seconds between looks at whether the claude loop-inbox opened has ended.
TALK_POLL = 0.5
@dataclasses.dataclass(frozen=True)
class Talk:
"""The human answers in the worker's claude session, which talk opens on loop-claude's terminal."""
@dataclasses.dataclass(frozen=True)
@@ -37,50 +30,50 @@ class WorkerSession:
# What the worker's claude adds to its environment, as the claude /talk opens does.
env: dict[str, str]
def talk_command(self) -> TalkCommand:
return TalkCommand(claude_args(self.rung, ["--resume", str(self.id)]), self.root, self.env)
def claude_command(self) -> ClaudeCommand:
return ClaudeCommand(claude_args(self.rung, ["--resume", str(self.id)]), self.root, self.env)
def put_question(question: str, answer_here: bool, session: WorkerSession, paths: Paths, effects: Effects) -> str | Talk | Talked | Stop:
"""Prints question for the human. Where answer_here, shows it in loop-inbox too and returns the first answer: the line they type at loop-claude's terminal, stripped, or Talk where they type TALK; a line typed in loop-inbox; or Talked once the claude loop-inbox opened ends with something typed. Else Talk at once. A Stop where loop-claude is off a terminal, or they type nothing, or the input ends."""
def put_question(question: str, answer_here: bool, session: WorkerSession, paths: Paths, effects: Effects) -> str | Talk | Stop:
"""Prints question for the human. Where answer_here, shows it in loop-inbox too and returns the first answer, typed at loop-claude's terminal or in loop-inbox: the line, stripped, or a Talk where they type TALK. Else a Talk at once, on the terminal. A Stop where loop-claude is off a terminal, or they type nothing, or the input ends."""
say(f"\n{question}", effects)
if stop := needs_terminal(ANSWER_QUESTION, effects.on_terminal()):
return stop
# Keys typed while the worker ran wait on the terminal, and would answer a question no one has read, here or in claude's session, which reads them too. Of a pasted answer, the first line answers; the rest wait, and the next question drops them. A longer answer goes through TALK.
effects.discard_typed()
ask = Ask(effects.new_id(), str(session.id), session.root, question, effects.now(), effects.pid(), Asking(session.claude_command()))
if not answer_here:
return Talk()
ask = Ask(str(uuid.uuid4()), str(session.id), session.root, question, effects.now(), os.getpid(), session.talk_command())
return Talk(ask, OpenOnTerminal())
shown(paths, ask, effects)
answer: str | Talk | Stop | None = None
try:
answer = first_answer(ask, paths, effects)
return answer
finally:
# A Talk keeps ask listed, under its id, for talk to show loop-inbox where claude opens.
if not isinstance(answer, Talk):
withdraw(paths.waiting, ask.session)
def first_answer(ask: Ask, paths: Paths, effects: Effects) -> str | Talk | Stop:
try:
answer = effects.input_or(ASK_ANSWER, lambda: answered(paths.waiting, ask))
if isinstance(answer, Talking | Talked):
return talked_in_inbox(ask, answer, session.transcript_path, paths, effects)
if isinstance(answer, Typed):
say(f"Answered in loop-inbox: {answer.text}", effects)
answer = answer.text
except EOFError:
print(file=sys.stderr) # Ends the prompt's line. It isn't a message, so it gets no time.
return needs_human(ANSWER_QUESTION, "the input ended")
finally:
withdraw(paths.waiting, ask.session)
answer = answer.strip()
if not answer:
match answer:
case ClaudeOpening(pid):
return Talk(ask, OpenInInbox(pid))
case Typed(text):
say(f"Answered in loop-inbox: {text}", effects)
case str(text):
pass
case _:
typing.assert_never(answer)
text = text.strip()
if not text:
return needs_human(ANSWER_QUESTION, "nothing was typed")
return Talk() if answer == TALK else answer
def talked_in_inbox(ask: Ask, answer: Talking | Talked, transcript_path: str, paths: Paths, effects: Effects) -> Talked | Stop:
"""Waits, off the terminal, while the claude that loop-inbox opened on the worker's session runs; the human must type something there."""
def waited() -> None:
if isinstance(answer, Talked):
return
# Shows loop-inbox that the loop stopped reading its terminal, so loop-inbox may open claude.
shown(paths, dataclasses.replace(ask, talk=None, talking=answer.pid), effects)
say("claude is open for this question in loop-inbox; waiting for it to end", effects)
while not isinstance(answered(paths.waiting, ask), Talked) and alive(answer.pid):
effects.sleep(TALK_POLL)
return human_typed(transcript_path, waited) or Talked()
return Talk(ask, OpenOnTerminal()) if text == TALK else text
def shown(paths: Paths, ask: Ask, effects: Effects) -> None:
@@ -89,19 +82,28 @@ def shown(paths: Paths, ask: Ask, effects: Effects) -> None:
say(f"loop-inbox cannot show this question: {unshown.reason}", effects)
def talk(question: str, session: WorkerSession, paths: Paths, effects: Effects) -> Stop | None:
"""Resumes the worker's session in claude on the terminal, listed in loop-inbox while it runs; the human must type something there."""
ask = Ask(str(uuid.uuid4()), str(session.id), session.root, question, effects.now(), os.getpid(), talk=None)
shown(paths, ask, effects)
def talk(chosen: Talk, session: WorkerSession, paths: Paths, effects: Effects) -> Stop | None:
"""Holds the loop while the human answers in the worker's claude session, listed in loop-inbox as open where it is; the human must type something there."""
try:
return human_typed(session.transcript_path, lambda: opened(session, paths, effects))
return human_typed(session.transcript_path, lambda: opened(chosen, session, paths, effects))
finally:
withdraw(paths.waiting, ask.session)
withdraw(paths.waiting, chosen.ask.session)
def opened(session: WorkerSession, paths: Paths, effects: Effects) -> Stop | None:
if unstarted := hand_over(session.rung, ["--resume", str(session.id)], session.root, paths, session.env, effects):
return Stop(1, f"could not start claude for the interrupt: {unstarted.reason}")
def opened(chosen: Talk, session: WorkerSession, paths: Paths, effects: Effects) -> Stop | None:
"""Returns once the claude the human answers in has ended: the one it opens on the terminal, or the one loop-inbox opened."""
# For loop-inbox, this shows that the loop has stopped reading its terminal, so it may open claude.
shown(paths, dataclasses.replace(chosen.ask, state=chosen.where), effects)
match chosen.where:
case OpenOnTerminal():
if unstarted := hand_over(session.claude_command(), paths, effects):
return Stop(1, f"could not start claude for the interrupt: {unstarted.reason}")
case OpenInInbox(pid):
say("claude is open for this question in loop-inbox; waiting for it to end", effects)
while not claude_ended(paths.waiting, chosen.ask) and effects.alive(pid):
effects.sleep(POLL_SECONDS)
case _:
typing.assert_never(chosen.where)
return None
@@ -119,11 +121,11 @@ def human_typed(transcript_path: str, held_open: typing.Callable[[], Stop | None
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), lambda text: say(text, effects))
def hand_over(command: ClaudeCommand, paths: Paths, effects: Effects) -> Failure | None:
"""Marks the checkout command runs in, and each of its worktrees, trusted, then runs claude on the terminal as command says; the failure is why claude could not start."""
trust(paths.claude_json, worktrees(command.cwd, effects), lambda text: say(text, effects))
try:
effects.claude_on_terminal(claude_args(rung, args), root, env)
effects.claude_on_terminal(command.args, command.cwd, command.env)
except OSError as failure:
return Failure(str(failure.strerror))
return None