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
+54 -44
View File
@@ -51,6 +51,8 @@ NOW = 1790000000
STAMP = f"<{NOW}>"
# How git answers in a project's checkout it cannot read, such as one that is gone.
UNREAD = Failure("not a git repository")
# The id FakeEffects gives each new ask, and its pid for loop-claude's process.
ASK_ID, PID = "ask-1", 4242
def assistant(*parts):
@@ -64,8 +66,10 @@ def human(content):
class FakeEffects:
"""Effects that record each call and answer from a script, one answer per call."""
def __init__(self, claude=(), on_terminal_claude=(), git=(), inputs=(), python=(), streams=(), on_terminal=True, free_memory=()):
def __init__(self, claude=(), on_terminal_claude=(), git=(), inputs=(), python=(), streams=(), on_terminal=True, free_memory=(), gone=()):
self.calls = []
# The pids of processes that no longer run.
self.gone = set(gone)
self.scripts = {"claude": list(claude), "claude_on_terminal": list(on_terminal_claude), "git": list(git), "input": list(inputs), "python": list(python), "stream_claude": list(streams)}
# Without a script, every read finds all memory available.
self.free_memory = list(free_memory)
@@ -75,6 +79,7 @@ class FakeEffects:
def effects(self):
return Effects(
alive=lambda pid: pid not in self.gone,
claude=lambda args, cwd, stdin: self.answer("claude", (args, stdin)),
claude_on_terminal=self.on_terminal_claude,
discard_typed=lambda: self.calls.append(("discard_typed", None)),
@@ -84,8 +89,10 @@ class FakeEffects:
input_or=self.input_or,
local_time=lambda seconds: f"<{seconds}>",
memory=self.memory,
new_id=lambda: ASK_ID,
now=lambda: NOW,
on_terminal=lambda: self.terminal,
pid=lambda: PID,
python=lambda args, cwd: self.answer("python", (args, cwd)),
sleep=lambda seconds: self.calls.append(("sleep", seconds)),
stream_claude=self.stream,
@@ -170,6 +177,8 @@ class Streamed:
TYPED = states.TypedAnswer("Which one?", "Use B.")
# The human chose to answer in the worker's claude session.
TALKED = states.Talk(waiting.Ask("a1", "s1", "/c", "q", NOW, 7, waiting.OpenOnTerminal()), waiting.OpenOnTerminal())
class Transitions(unittest.TestCase):
@@ -187,13 +196,10 @@ class Transitions(unittest.TestCase):
(states.Judging("/t", 3), states.Ruled(Finished("e")), states.Closing("/t", Finished("e"))),
(states.Judging("/t"), states.Ruled(Unfinished("e")), states.Closing("/t", Unfinished("e"))),
(states.Judging("/t", 3), states.Ruled(Unfinished("e")), states.Closing("/t", Unfinished("e"))),
(states.Interrupting("/t", Interrupt("e", "q")), states.TalkChosen("q"), states.Talking("/t", "q")),
(states.Interrupting("/t", Interrupt("e", "q"), 3), states.TalkChosen("q"), states.Talking("/t", "q", 3)),
(states.Talking("/t", "q"), states.HumanTalked(), states.Judging("/t")),
(states.Talking("/t", "q", 3), states.HumanTalked(), states.Judging("/t", 3)),
# The human talked in the claude loop-inbox opened.
(states.Interrupting("/t", Interrupt("e", "q")), states.HumanTalked(), states.Judging("/t")),
(states.Interrupting("/t", Interrupt("e", "q"), 3), states.HumanTalked(), states.Judging("/t", 3)),
(states.Interrupting("/t", Interrupt("e", "q")), states.TalkChosen(TALKED), states.Talking("/t", TALKED)),
(states.Interrupting("/t", Interrupt("e", "q"), 3), states.TalkChosen(TALKED), states.Talking("/t", TALKED, 3)),
(states.Talking("/t", TALKED), states.HumanTalked(), states.Judging("/t")),
(states.Talking("/t", TALKED, 3), states.HumanTalked(), states.Judging("/t", 3)),
(states.Interrupting("/t", Interrupt("e", "q")), states.HumanTyped(TYPED), states.Working(TYPED)),
(states.Interrupting("/t", Interrupt("e", "q"), 3), states.HumanTyped(TYPED), states.Working(TYPED, 3)),
(states.Working(TYPED, 3), states.WorkerRan("/t"), states.Judging("/t", 3)),
@@ -203,8 +209,8 @@ class Transitions(unittest.TestCase):
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.Working(TYPED, 3), states.Judging("/t"), states.Judging("/t", 3), states.Interrupting("/t", Interrupt("e", "q")), states.Interrupting("/t", Interrupt("e", "q"), 3), states.Talking("/t", "q"), states.Talking("/t", "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.TalkChosen("q"), states.HumanTalked(), states.HumanTyped(TYPED), 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.Talking("/t", TALKED), states.Talking("/t", TALKED, 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.TalkChosen(TALKED), states.HumanTalked(), states.HumanTyped(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]:
@@ -219,7 +225,7 @@ class Transitions(unittest.TestCase):
(states.Judging("/t", 3), states.Ruled(quoted, step_up), states.Interrupting("/t", quoted, 3, step_up)),
(states.Judging("/t"), states.Ruled(interrupt, step_up), states.Interrupting("/t", interrupt, 0, step_up)),
(states.Interrupting("/t", interrupt, 0, step_up), states.HumanTyped(TYPED), states.Working(TYPED, 0, step_up)),
(states.Interrupting("/t", interrupt, 0, step_up), states.TalkChosen("q"), states.Talking("/t", "q")),
(states.Interrupting("/t", interrupt, 0, step_up), states.TalkChosen(TALKED), states.Talking("/t", TALKED)),
(states.Working(TYPED, 0, step_up), states.WorkerRan("/t"), states.Judging("/t")),
):
with self.subTest(state=state, event=event):
@@ -664,7 +670,12 @@ class Interrupts(unittest.TestCase):
return put, said.getvalue()
def asked(self):
return waiting.waiting(self.paths.waiting)
return waiting.waiting(self.paths.waiting, lambda pid: True)
def ask(self, question="Which one?", state=None):
"""The ask put_question makes for question, in state; by default it waits for an answer, and /talk resumes the worker's session."""
resumed = waiting.ClaudeCommand(["--model", "m", "--effort", "high", "--permission-mode", "bypassPermissions", "--resume", str(self.session.id)], str(self.home), {"LOOP_CLAUDE_WORKER_RUNG": "m at high effort"})
return waiting.Ask(ASK_ID, str(self.session.id), str(self.home), question, NOW, PID, state or waiting.Asking(resumed))
def test_stops_off_a_terminal_before_asking(self):
fake = FakeEffects(on_terminal=False)
@@ -680,7 +691,7 @@ class Interrupts(unittest.TestCase):
for typed, answer_here in (([" /talk "], True), ([], False)):
with self.subTest(typed=typed):
fake = FakeEffects(inputs=typed)
self.assertEqual(self.put(fake, answer_here)[0], interrupt.Talk())
self.assertEqual(self.put(fake, answer_here)[0], states.Talk(self.ask(), waiting.OpenOnTerminal()))
self.assertEqual(len(fake.made("input")), len(typed))
self.assertEqual(fake.calls[0], ("discard_typed", None))
@@ -692,9 +703,7 @@ class Interrupts(unittest.TestCase):
def test_shows_the_question_in_loop_inbox_while_it_waits_and_only_then(self):
def typed(_):
[ask] = self.asked()
self.assertEqual((ask.session, ask.root, ask.question, ask.asked, ask.pid, ask.talking), (str(self.session.id), str(self.home), "Which one?", NOW, os.getpid(), None))
self.assertEqual(ask.talk, waiting.TalkCommand(["--model", "m", "--effort", "high", "--permission-mode", "bypassPermissions", "--resume", str(self.session.id)], str(self.home), {"LOOP_CLAUDE_WORKER_RUNG": "m at high effort"}))
self.assertEqual(self.asked(), [self.ask()])
return "Use B."
self.assertEqual(self.put(FakeEffects(inputs=[typed]))[0], "Use B.")
self.assertEqual(list(self.paths.waiting.iterdir()), [])
@@ -711,56 +720,57 @@ class Interrupts(unittest.TestCase):
self.assertEqual(self.put(fake, effects=effects), ("Use C.", f"\n{STAMP} Which one?\n{STAMP} Answered in loop-inbox: Use C. \n"))
self.assertEqual(list(self.paths.waiting.iterdir()), [])
def test_waits_off_the_terminal_while_loop_inbox_has_claude_open_and_goes_on_once_the_human_typed_there(self):
inbox = os.getpid()
def talking(other):
def talk(self, chosen, effects):
with contextlib.redirect_stderr(io.StringIO()) as said:
stop = interrupt.talk(chosen, self.session, self.paths, effects)
return stop, said.getvalue()
def test_makes_way_for_the_claude_loop_inbox_opens_and_goes_on_once_the_human_typed_there(self):
inbox = 4343
def opening(other):
[ask] = self.asked()
waiting.send(self.paths.waiting, ask, waiting.Talking(inbox))
waiting.send(self.paths.waiting, ask, waiting.ClaudeOpening(inbox))
return other()
fake = FakeEffects(inputs=[opening])
self.assertEqual(self.put(fake), (states.Talk(self.ask(), waiting.OpenInInbox(inbox)), f"\n{STAMP} Which one?\n"))
def claude_ran(_):
[ask] = self.asked()
self.assertEqual((ask.talk, ask.talking), (None, inbox))
# loop-inbox opens claude only once the loop lists it as open there.
self.assertEqual(self.asked(), [self.ask(state=waiting.OpenInInbox(inbox))])
with open(self.transcript, "a") as handle:
handle.write(json.dumps(human("cut it")) + "\n")
waiting.send(self.paths.waiting, ask, waiting.Talked())
fake = FakeEffects(inputs=[talking])
self.assertEqual(self.put(fake, effects=dataclasses.replace(fake.effects(), sleep=claude_ran)), (interrupt.Talked(), f"\n{STAMP} Which one?\n{STAMP} claude is open for this question in loop-inbox; waiting for it to end\n"))
self.assertEqual(list(self.paths.waiting.iterdir()), [])
waiting.send(self.paths.waiting, self.ask(), waiting.ClaudeEnded())
effects = dataclasses.replace(fake.effects(), sleep=claude_ran)
self.assertEqual(self.talk(states.Talk(self.ask(), waiting.OpenInInbox(inbox)), effects), (None, f"{STAMP} claude is open for this question in loop-inbox; waiting for it to end\n"))
self.assertEqual((fake.made("claude_on_terminal"), list(self.paths.waiting.iterdir())), ([], []))
def test_stops_when_loop_inbox_ends_its_claude_or_is_gone_with_nothing_typed(self):
gone = subprocess.run([sys.executable, "-c", "import os; print(os.getpid())"], capture_output=True, text=True).stdout.strip()
for pid, ended in ((os.getpid(), True), (int(gone), False)):
with self.subTest(ended=ended):
def talking(other):
[ask] = self.asked()
waiting.send(self.paths.waiting, ask, waiting.Talking(pid))
if ended:
waiting.send(self.paths.waiting, ask, waiting.Talked())
return other()
self.assertEqual(self.put(FakeEffects(inputs=[talking]))[0], Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed"))
inbox = 4343
def ended(_):
waiting.send(self.paths.waiting, self.ask(), waiting.ClaudeEnded())
# loop-inbox's claude ends while the loop waits, or loop-inbox is gone before the loop looks.
for gone in ((), (inbox,)):
with self.subTest(gone=gone):
stop, _ = self.talk(states.Talk(self.ask(), waiting.OpenInInbox(inbox)), dataclasses.replace(FakeEffects(gone=gone).effects(), sleep=ended))
self.assertEqual(stop, Stop(2, "the human is needed to answer the question above, but the interrupt ended with nothing typed"))
def test_lists_the_claude_open_on_its_terminal_in_loop_inbox_while_it_runs(self):
def ran(args):
[ask] = self.asked()
self.assertEqual((ask.question, ask.talk, ask.talking), ("Cut 0.4.0?", None, None))
self.assertEqual(self.asked(), [self.ask("Cut 0.4.0?", waiting.OpenOnTerminal())])
return 0
fake = FakeEffects(on_terminal_claude=[ran], git=[""])
with contextlib.redirect_stderr(io.StringIO()):
interrupt.talk("Cut 0.4.0?", self.session, self.paths, fake.effects())
self.talk(states.Talk(self.ask("Cut 0.4.0?"), waiting.OpenOnTerminal()), fake.effects())
self.assertEqual(len(fake.made("claude_on_terminal")), 1)
self.assertEqual(list(self.paths.waiting.iterdir()), [])
def test_stops_when_nothing_was_typed_in_the_session(self):
fake = FakeEffects(on_terminal_claude=[0], git=[""])
with contextlib.redirect_stderr(io.StringIO()):
stop = interrupt.talk("Which one?", self.session, self.paths, fake.effects())
stop, _ = self.talk(states.Talk(self.ask(), waiting.OpenOnTerminal()), 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"))
def test_hands_the_claude_that_takes_the_terminal_the_workers_environment(self):
fake = FakeEffects(on_terminal_claude=[0], git=[""])
with contextlib.redirect_stderr(io.StringIO()):
interrupt.talk("Which one?", self.session, self.paths, fake.effects())
self.talk(states.Talk(self.ask(), waiting.OpenOnTerminal()), fake.effects())
self.assertEqual(fake.terminal_environments, [{"LOOP_CLAUDE_WORKER_RUNG": "m at high effort"}])