Show every waiting loop-claude in loop-inbox and take each one's answer there

This commit is contained in:
2026-10-28 23:24:29 +01:00
parent d13c4fb0db
commit a7fe878631
24 changed files with 938 additions and 81 deletions
+106 -27
View File
@@ -22,7 +22,7 @@ from loop_claude.project import checkout, model_choice, records
from chunk_pick import DefaultPlan, LastCut
from loop_claude.effects import Effects, PYTHON_TIMEOUT, run_python, say, system_effects
from claude_code import entries, reply, session
from loop_claude import escalation_mark, paths, routing
from loop_claude import escalation_mark, paths, routing, waiting
from failure import Failure
from loop_claude.verdicts import Finished, Interrupt, QuotedAnswer, Unfinished
from loop_claude.cycle import interrupt, judge, prompts, retry, run, states, worker
@@ -81,6 +81,7 @@ class FakeEffects:
execv=self.execv,
git=lambda *args, cwd=None, timeout=None, guarded=False, errors="strict": self.answer("git", list(args)),
input=lambda prompt: self.answer("input", prompt),
input_or=self.input_or,
local_time=lambda seconds: f"<{seconds}>",
memory=self.memory,
now=lambda: NOW,
@@ -94,6 +95,14 @@ class FakeEffects:
self.calls.append(("memory", None))
return self.free_memory.pop(0) if self.free_memory else memory.Memory(available=16, total=16)
def input_or(self, prompt, other):
"""Answers from the input script as input does, but hands a callable in it other, which loop-inbox's answer comes from."""
self.calls.append(("input", prompt))
answer = self.scripts["input"].pop(0)
if isinstance(answer, BaseException):
raise answer
return answer(other) if callable(answer) else answer
def on_terminal_claude(self, args, cwd, env):
self.terminal_environments.append(env)
return self.answer("claude_on_terminal", args)
@@ -178,10 +187,13 @@ 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(), states.Talking("/t")),
(states.Interrupting("/t", Interrupt("e", "q"), 3), states.TalkChosen(), states.Talking("/t", 3)),
(states.Talking("/t"), states.HumanTalked(), states.Judging("/t")),
(states.Talking("/t", 3), states.HumanTalked(), states.Judging("/t", 3)),
(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.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)),
@@ -191,8 +203,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"), states.Talking("/t", 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(), 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", "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())
for state in every_state:
for event in every_event:
if (state, event) not in [(state, event) for state, event, _ in legal]:
@@ -207,7 +219,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(), states.Talking("/t")),
(states.Interrupting("/t", interrupt, 0, step_up), states.TalkChosen("q"), states.Talking("/t", "q")),
(states.Working(TYPED, 0, step_up), states.WorkerRan("/t"), states.Judging("/t")),
):
with self.subTest(state=state, event=event):
@@ -638,11 +650,22 @@ class MemoryWait(unittest.TestCase):
class Interrupts(unittest.TestCase):
def put(self, fake, answer_here=True):
def setUp(self):
self.home = pathlib.Path(tempfile.mkdtemp())
self.addCleanup(shutil.rmtree, self.home)
self.paths = paths.paths_from(self.home)
self.transcript = self.home / "t.jsonl"
self.transcript.write_text(json.dumps(human("a")) + "\n")
self.session = interrupt.WorkerSession(str(self.home), uuid.uuid7(), Rung("m", "high"), str(self.transcript), {"LOOP_CLAUDE_WORKER_RUNG": "m at high effort"})
def put(self, fake, answer_here=True, effects=None):
with contextlib.redirect_stderr(io.StringIO()) as said:
put = interrupt.put_question("Which one?", answer_here, fake.effects())
put = interrupt.put_question("Which one?", answer_here, self.session, self.paths, effects or fake.effects())
return put, said.getvalue()
def asked(self):
return waiting.waiting(self.paths.waiting)
def test_stops_off_a_terminal_before_asking(self):
fake = FakeEffects(on_terminal=False)
self.assertEqual(self.put(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"), f"\n{STAMP} Which one?\n"))
@@ -665,24 +688,80 @@ class Interrupts(unittest.TestCase):
for typed, why, said in ((" ", "nothing was typed", ""), (EOFError(), "the input ended", "\n")):
with self.subTest(why=why):
self.assertEqual(self.put(FakeEffects(inputs=[typed])), (Stop(2, f"the human is needed to answer the question above, but {why}"), f"\n{STAMP} Which one?\n{said}"))
self.assertEqual(list(self.paths.waiting.iterdir()), [])
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"}))
return "Use B."
self.assertEqual(self.put(FakeEffects(inputs=[typed]))[0], "Use B.")
self.assertEqual(list(self.paths.waiting.iterdir()), [])
def test_takes_a_line_typed_in_loop_inbox_for_this_question_and_no_other(self):
def answered(_, other):
[ask] = self.asked()
waiting.send(self.paths.waiting, dataclasses.replace(ask, id="an earlier ask"), waiting.Typed("Use A."))
self.assertIsNone(other())
waiting.send(self.paths.waiting, ask, waiting.Typed(" Use C. "))
return other()
fake = FakeEffects()
effects = dataclasses.replace(fake.effects(), input_or=answered)
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):
[ask] = self.asked()
waiting.send(self.paths.waiting, ask, waiting.Talking(inbox))
return other()
def claude_ran(_):
[ask] = self.asked()
self.assertEqual((ask.talk, ask.talking), (None, 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()), [])
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"))
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))
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.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):
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=[""])
stop = interrupt.talk(home, uuid.uuid7(), Rung("m", "high"), str(path), 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(on_terminal_claude=[0], git=[""])
with contextlib.redirect_stderr(io.StringIO()):
stop = interrupt.talk("Which one?", self.session, self.paths, 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):
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=[""])
handed = {"LOOP_CLAUDE_PROJECT_RUNG": "claude-opus-5-5 at medium effort", "LOOP_CLAUDE_WORKER_RUNG": "claude-haiku-5-5 at max effort"}
interrupt.talk(home, uuid.uuid7(), Rung("claude-haiku-5-5", "max"), str(path), paths.paths_from(pathlib.Path(home)), handed, fake.effects())
self.assertEqual(fake.terminal_environments, [handed])
fake = FakeEffects(on_terminal_claude=[0], git=[""])
with contextlib.redirect_stderr(io.StringIO()):
interrupt.talk("Which one?", self.session, self.paths, fake.effects())
self.assertEqual(fake.terminal_environments, [{"LOOP_CLAUDE_WORKER_RUNG": "m at high effort"}])
class RoutingCheck(unittest.TestCase):
@@ -1579,9 +1658,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", "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/interrupt.py", "opened", "1", "f'could not start claude for the interrupt:", "claude cannot start for an interrupt"),
("cycle/interrupt.py", "human_typed", "1", "entries.reason", read),
("cycle/interrupt.py", "human_typed", "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", "archived_stop", "1", "f'{failure.reason}", read),
("cycle/run.py", "work", "1", "f'claude left no transcript for session", "claude leaves no transcript"),