Answer architecture round 1: 0 🔴, 7 🟡 (0 misread); hold one lock per loop for its whole run, group the judge's readings of the items taken in, list them as given

This commit is contained in:
2026-10-29 20:23:07 +01:00
parent 9bcfaebb83
commit dcdb8b5868
16 changed files with 274 additions and 278 deletions
+26 -19
View File
@@ -18,9 +18,8 @@ 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 import running_loops, session_lock, waiting
from loop_claude.running_loops import RunningLoop, Taken
from loop_claude import session_lock
from loop_claude.effects import system_effects
ASKING = Asking(ClaudeCommand(["--resume", "s"], "/c", {}))
@@ -34,9 +33,9 @@ def asked(session, root, question="Which one?", at=100.0, state=ASKING):
def loops_hold(test, folder, *sessions):
"""Holds each session's lock until the test ends, as the loop that lists its ask does."""
"""Holds each session's lock until the test ends, as a running loop does."""
for session in sessions:
test.enterContext(session_lock.locked(folder, session))
test.enterContext(session_lock.held(folder, session))
def asks_and_answers(folder):
@@ -331,7 +330,7 @@ class Waiting(unittest.TestCase):
def test_drops_and_removes_the_ask_a_killed_loop_left_behind(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
lists_and_waits = "import pathlib, sys, time\nfrom test_loop_inbox import asked, system_effects, waiting\nwith waiting.listed(pathlib.Path(sys.argv[1]), asked('s1', '/a'), system_effects()):\n\tprint(flush=True); time.sleep(60)"
lists_and_waits = "import pathlib, sys, time\nfrom test_loop_inbox import asked, session_lock, system_effects, waiting\nwith session_lock.held(pathlib.Path(sys.argv[1]), 's1'), waiting.listed(pathlib.Path(sys.argv[1]), asked('s1', '/a'), system_effects()):\n\tprint(flush=True); time.sleep(60)"
loop = subprocess.Popen([sys.executable, "-c", lists_and_waits, temp], cwd=pathlib.Path(__file__).parent, stdout=subprocess.PIPE)
self.addCleanup(loop.wait)
self.addCleanup(loop.kill)
@@ -354,15 +353,17 @@ class Waiting(unittest.TestCase):
main.run(Window("q"), contextlib.nullcontext, paths, system_effects())
self.assertEqual(asks_and_answers(paths.waiting), [])
def test_removes_only_the_asks_and_answers_whose_loop_has_ended(self):
def test_removes_only_the_asks_answers_and_running_files_whose_loop_has_ended(self):
with tempfile.TemporaryDirectory() as temp:
folder = pathlib.Path(temp)
loops_hold(self, folder, "s1")
for ask in (asked("s1", "/a"), asked("s2", "/b")):
waiting.publish(folder, ask)
waiting.send(folder, ask, Typed("Use A."))
for session in ("s1", "s2", "s3"):
running_loops.running_file(folder, session).write_text(json.dumps({"root": "/a", "items": []}))
waiting.drop_ended(folder)
self.assertEqual(asks_and_answers(folder), ["s1.answer", "s1.json"])
self.assertEqual(asks_and_answers(folder), ["s1.answer", "s1.json", "s1.running"])
def test_a_talk_claim_from_pid_0_or_below_is_no_answer_since_it_would_name_a_process_group(self):
with tempfile.TemporaryDirectory() as temp:
@@ -415,9 +416,9 @@ class RunningLoops(unittest.TestCase):
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:
with running_loops.shown_running(self.folder, "s1", "/code/log", self.effects) as listed:
self.assertEqual(running_loops.running_loops(self.folder), [RunningLoop("s1", "/code/log", ())])
show((Taken(255, "Have loop-inbox list every running loop."),))
listed.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), [])
@@ -427,29 +428,35 @@ class RunningLoops(unittest.TestCase):
# 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.enterContext(session_lock.held(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):
def test_a_second_loop_on_the_session_warns_and_leaves_the_first_listed_with_its_question(self):
ask = asked("s1", "/code/log")
with running_loops.shown_running(self.folder, "s1", "/code/log", self.effects) as first:
waiting.publish(first.folder, ask)
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())
with running_loops.shown_running(self.folder, "s1", "/code/log", self.effects) as second:
second.show((Taken(1, "Do it."),))
with waiting.listed(second.folder, asked("s1", "/code/log", question="Mine?"), self.effects):
self.assertEqual(waiting.waiting(self.folder), [ask])
self.assertIn(f"loop-inbox cannot list this loop or its questions: another process holds {session_lock.lock_file(self.folder, 's1')}", said.getvalue())
self.assertEqual(running_loops.running_loops(self.folder), [RunningLoop("s1", "/code/log", ())])
self.assertEqual(waiting.waiting(self.folder), [ask])
self.assertEqual(list((self.folder / "unlisted").iterdir()), [])
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")))
looking.enter_context(session_lock.held(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, ()), ())
self.assertEqual(running_loops.taken_items(Failure("no todo.md"), 12, (14, 12, 3)), (Taken(12, ""), Taken(14, ""), Taken(3, "")))
self.assertEqual(running_loops.taken_items(Failure("no todo.md"), 0, (14,)), (Taken(14, ""),))
self.assertEqual(running_loops.taken_items(Failure("no todo.md"), 0, ()), ())
class Shown(unittest.TestCase):