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
+28 -3
View File
@@ -3,12 +3,15 @@
How it shows the loops and a question was picked on 2026-10-28 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: a block per
loop with its checkout's path and how long it has waited, and an answer prompt saying that Enter alone doesn't stop the loop.
A second bench, on 2026-10-29, picked the running loops' section: below the waiting loops, a line per loop with its
checkout and the number it waits as, then a line per item its chunk takes, with its ID and tagline.
"""
import dataclasses
import textwrap
import typing
from loop_claude.waiting import Ask, Asking, OpenInInbox, OpenOnTerminal, TALK
from loop_claude.running_loops import RunningLoop, Taken
ENTER = "enter"
BACKSPACE = "backspace"
@@ -24,10 +27,11 @@ NARROWEST = 20
@dataclasses.dataclass(frozen=True)
class Listing:
"""The waiting loops as they were when the human began typing a number, so a loop's number doesn't change while they type it."""
"""The waiting loops as they were when the human began typing a number, so a loop's number doesn't change while they type it, and the running loops."""
shown: tuple[Ask, ...] = ()
typed: str = ""
note: str = ""
running: tuple[RunningLoop, ...] = ()
@dataclasses.dataclass(frozen=True)
@@ -65,9 +69,9 @@ def pressed(screen: Screen, key: str) -> Screen | Send | Open | Quit:
if key == ENTER:
return picked(shown, typed)
if key == BACKSPACE:
return Listing(shown, typed[:-1])
return dataclasses.replace(screen, typed=typed[:-1])
# isdigit would also take "²", which int() rejects.
return Listing(shown, typed + key) if key.isdecimal() else screen
return dataclasses.replace(screen, typed=typed + key) if key.isdecimal() else screen
case Answering(ask, typed):
if key == ESCAPE or (key == ENTER and not typed.strip()):
return Listing()
@@ -106,10 +110,31 @@ def listing_lines(screen: Listing, now: float, local_time: typing.Callable[[floa
lines += wrapped(f"[{number}] {ask.root}, waiting since {local_time(ask.asked)} ({waited(now - ask.asked)}){state}", width)
lines += cut(wrapped(ask.question, width - len(INDENT) - len(ELLIPSIS)), SHOWN_LINES)
lines.append("")
lines += running_lines(screen.running, screen.shown, width)
ask_for = "Type a number and Enter to answer that loop; " if count else ""
return lines, [*wrapped(screen.note, width), *wrapped(f"{ask_for}{QUIT} quits loop-inbox, and the loops keep waiting.", width), *typed_after(">", screen.typed, width)]
def running_lines(running: tuple[RunningLoop, ...], shown: tuple[Ask, ...], width: int) -> list[str]:
"""Each running loop's checkout, with the number it waits as, and a line per item its chunk takes."""
if not running:
return ["No loop runs."]
numbers = {ask.session: number for number, ask in enumerate(shown, 1)}
lines = wrapped(f"Running: {len(running)} loop{'s' if len(running) > 1 else ''}", width)
for loop in running:
waits_as = f", waiting as [{numbers[loop.session]}]" if loop.session in numbers else ""
lines += wrapped(f"- {loop.root}{waits_as}", width)
for item in loop.items or (None,):
lines += cut(wrapped(item_line(item), width - len(INDENT) - len(ELLIPSIS)), 1)
return lines
def item_line(item: Taken | None) -> str:
if item is None:
return "no todo.md item yet"
return f"item {item.id}: {item.tagline}" if item.tagline else f"item {item.id}"
def answering_lines(screen: Answering, now: float, local_time: typing.Callable[[float], str], width: int) -> tuple[list[str], list[str]]:
"""The question, in lines of at most width characters, and its foot, the prompt for the answer."""
ask = screen.ask
+11 -3
View File
@@ -1,4 +1,5 @@
"""Show the interrupts of every loop-claude on this machine, oldest first, and answer each one here.
Below them, list every loop-claude that runs and the todo.md items its chunk takes.
usage: loop-inbox
@@ -20,8 +21,14 @@ loop-inbox running. A loop with claude open, for a release ready to cut, the rou
asking for its project's model, as on a project's first run, asks on its own terminal only. q quits
loop-inbox; the loops keep waiting.
Below the questions, each running loop shows its checkout, the number it waits as, if any, and a
line per todo.md item its chunk takes: the item it was routed on, then those the judge found the
worker took in, each with its tagline from the default branch. A loop whose process is gone drops
off this list too.
The loops' questions are files in ~/.local/state/loop-claude, read and written as
bin/loop_claude/waiting.py describes.
bin/loop_claude/waiting.py describes; the running loops are files beside them, as
bin/loop_claude/running_loops.py describes.
Exit 2 when stdin or stdout is not a terminal, or on an unknown argument.
"""
@@ -39,6 +46,7 @@ from failure import Failure
from loop_claude.hand_over import hand_over
from loop_claude.paths import Paths, paths_from
from loop_claude.session_lock import running
from loop_claude.running_loops import running_loops
USAGE = "usage: loop-inbox"
# Milliseconds between refreshes of the list while no key is pressed.
@@ -103,10 +111,10 @@ def key_name(key: str | int) -> str:
def refreshed(screen: Screen, folder: pathlib.Path) -> Screen:
"""screen with the loops as they wait now, unless the human is typing a number; a question that no longer waits leaves the answer screen."""
"""screen with the loops as they wait and run now, unless the human is typing a number; a question that no longer waits leaves the answer screen."""
match screen:
case Listing(_, "", note):
return Listing(tuple(waiting(folder)), "", note)
return Listing(tuple(waiting(folder)), "", note, tuple(running_loops(folder)))
case Answering(ask) if not still_waits(folder, ask):
return Listing(note=no_longer_waits(ask))
return screen
+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.