List every running loop in loop-inbox, with the ID and tagline of each item its chunk takes

This commit is contained in:
2026-10-29 19:57:11 +01:00
parent 8bddfdc282
commit 9bcfaebb83
16 changed files with 389 additions and 50 deletions
+85 -3
View File
@@ -18,9 +18,10 @@ from loop_claude.hand_over import ClaudeCommand
from failure import Failure
from loop_inbox import inbox, main
from loop_claude.paths import paths_from
from loop_claude import running_loops, waiting
from loop_claude.running_loops import RunningLoop, Taken
from loop_claude import session_lock
from loop_claude.effects import system_effects
from loop_claude import waiting
ASKING = Asking(ClaudeCommand(["--resume", "s"], "/c", {}))
# The pid of this loop-inbox.
@@ -122,6 +123,10 @@ class Answers(unittest.TestCase):
waiting.publish(self.folder, ask)
self.assertEqual(main.refreshed(Listing(note="Sent to /a."), self.folder), Listing((log,), "", "Sent to /a."))
def test_the_list_shows_the_loops_running_now(self):
self.enterContext(running_loops.shown_running(self.folder, "s9", "/code/web", system_effects()))
self.assertEqual(main.refreshed(Listing(), self.folder), Listing(running=(RunningLoop("s9", "/code/web", ()),)))
def test_a_number_picks_the_loop_it_had_when_the_human_began_typing_it(self):
log, bender = asked("s1", "/code/log", at=100), asked("s2", "/code/bender", at=200)
typing = keyed(Listing((log, bender)), "2")
@@ -288,6 +293,10 @@ class Keys(unittest.TestCase):
with self.subTest(keys=keys):
self.assertEqual(keyed(Listing((ask,)), *keys), after)
def test_a_number_typed_keeps_the_running_loops_shown(self):
running = (RunningLoop("s1", "/code/log", ()),)
self.assertEqual(keyed(Listing(running=running), "1", "2", BACKSPACE), Listing((), "1", "", running))
def test_an_answer_is_sent_stripped_talk_opens_claude_and_enter_alone_or_esc_goes_back(self):
ask = asked("s1", "/code/log")
for keys, after in (
@@ -399,6 +408,50 @@ class Waiting(unittest.TestCase):
self.assertEqual((waiting.answered(folder, another), waiting.claude_ended(folder, another)), (None, False))
class RunningLoops(unittest.TestCase):
def setUp(self):
self.folder = pathlib.Path(tempfile.mkdtemp())
self.addCleanup(shutil.rmtree, self.folder)
self.effects = dataclasses.replace(system_effects(), sleep=lambda seconds: None)
def test_lists_a_loop_with_the_items_it_shows_until_it_ends(self):
with running_loops.shown_running(self.folder, "s1", "/code/log", self.effects) as show:
self.assertEqual(running_loops.running_loops(self.folder), [RunningLoop("s1", "/code/log", ())])
show((Taken(255, "Have loop-inbox list every running loop."),))
self.assertEqual(running_loops.running_loops(self.folder), [RunningLoop("s1", "/code/log", (Taken(255, "Have loop-inbox list every running loop."),))])
self.assertEqual(running_loops.running_loops(self.folder), [])
def test_lists_the_loops_by_checkout_and_leaves_out_one_that_was_killed_or_whose_file_cannot_be_read(self):
for session, root in (("s1", "/code/web"), ("s2", "/code/c")):
self.enterContext(running_loops.shown_running(self.folder, session, root, self.effects))
# A killed loop leaves its file behind, and its lock free.
(self.folder / "s3.running").write_text(json.dumps({"root": "/code/old", "items": []}))
for session, content in (("s4", "{"), ("s5", json.dumps({"root": "/code/x", "items": [{"id": "1", "tagline": ""}]})), ("s6", json.dumps({"root": "/code/x"}))):
self.enterContext(session_lock.held(self.folder, running_loops.running_lock(self.folder, session)))
(self.folder / f"{session}.running").write_text(content)
self.assertEqual(running_loops.running_loops(self.folder), [RunningLoop("s2", "/code/c", ()), RunningLoop("s1", "/code/web", ())])
def test_a_second_loop_on_the_session_warns_and_leaves_the_first_listed(self):
with running_loops.shown_running(self.folder, "s1", "/code/log", self.effects):
with contextlib.redirect_stderr(io.StringIO()) as said:
with running_loops.shown_running(self.folder, "s1", "/code/log", self.effects) as show:
show((Taken(1, "Do it."),))
self.assertIn(f"loop-inbox cannot list this loop as running: another process holds {running_loops.running_lock(self.folder, 's1')}", said.getvalue())
self.assertEqual(running_loops.running_loops(self.folder), [RunningLoop("s1", "/code/log", ())])
def test_takes_the_lock_once_loop_inbox_lets_go_of_it(self):
# loop-inbox holds the lock for a moment as it looks whether the loop runs.
looking = contextlib.ExitStack()
looking.enter_context(session_lock.held(self.folder, running_loops.running_lock(self.folder, "s1")))
with running_loops.shown_running(self.folder, "s1", "/code/log", dataclasses.replace(self.effects, sleep=lambda seconds: looking.close())):
self.assertEqual(running_loops.running_loops(self.folder), [RunningLoop("s1", "/code/log", ())])
def test_a_chunk_takes_its_routed_item_first_then_those_taken_in_once_each(self):
self.assertEqual(running_loops.chunk_items(12, (14, 12, 3)), (12, 14, 3))
self.assertEqual(running_loops.chunk_items(0, (14,)), (14,))
self.assertEqual(running_loops.chunk_items(0, ()), ())
class Shown(unittest.TestCase):
def drawn(self, screen, window):
"""The lines draw puts on window for screen, 3700 s after the epoch."""
@@ -421,14 +474,43 @@ class Shown(unittest.TestCase):
" Cut 0.4.0?",
" Its changelog is ready.",
"",
"No loop runs.",
], [
"Type a number and Enter to answer that loop; q",
"quits loop-inbox, and the loops keep waiting.",
"> 1",
]))
def test_an_empty_list_says_no_loop_waits(self):
self.assertEqual(inbox.listing_lines(Listing(note="Sent to /a."), 0, str, 79), (["No loop waits for you.", ""], ["Sent to /a.", "q quits loop-inbox, and the loops keep waiting.", "> "]))
def test_the_running_loops_follow_the_waiting_ones_each_with_the_items_its_chunk_takes(self):
log = asked("s1", "/code/log", "Which one?", at=0)
running = (
RunningLoop("s2", "/code/c", (Taken(12, "Accept undefined in MetadataValue and drop such keys on output."), Taken(14, ""))),
RunningLoop("s1", "/code/log", (Taken(255, "Have loop-inbox list every running loop-claude on the machine."),)),
RunningLoop("s3", "/code/web", ()),
)
lines, _ = inbox.listing_lines(Listing((log,), running=running), 120, lambda seconds: f"<{seconds}>", 50)
self.assertEqual(lines, [
"1 loop waits for you.",
"",
"[1] /code/log, waiting since <0> (2 min)",
" Which one?",
"",
"Running: 3 loops",
"- /code/c",
" item 12: Accept undefined in MetadataValue…",
" item 14",
"- /code/log, waiting as [1]",
" item 255: Have loop-inbox list every running…",
"- /code/web",
" no todo.md item yet",
])
def test_the_running_section_counts_one_loop(self):
lines, _ = inbox.listing_lines(Listing(running=(RunningLoop("s1", "/code/log", ()),)), 0, str, 79)
self.assertEqual(lines, ["No loop waits for you.", "", "Running: 1 loop", "- /code/log", " no todo.md item yet"])
def test_an_empty_list_says_no_loop_waits_or_runs(self):
self.assertEqual(inbox.listing_lines(Listing(note="Sent to /a."), 0, str, 79), (["No loop waits for you.", "", "No loop runs."], ["Sent to /a.", "q quits loop-inbox, and the loops keep waiting.", "> "]))
def test_the_answer_screen_at_80_columns_shows_every_character_of_each_line_and_says_what_enter_alone_does(self):
# The question's first line has 80 characters, one more than a line drawn at 80 columns holds.