Answer prose round 1: 0 🔴, 5 🟡 (0 misread); drop the warning that loop-inbox may still list a question, and make the lock's docstrings, decision, test names and items 253 and 254 true and plain

This commit is contained in:
2026-10-29 18:33:29 +01:00
parent 74a4a90b10
commit bf78fedc54
7 changed files with 25 additions and 26 deletions
+5 -6
View File
@@ -1,8 +1,7 @@
"""The lock a loop-claude holds on its worker's session while loop-inbox lists its question, which tells loop-inbox the loop still runs.
"""The lock a loop-claude holds on its worker's session while loop-inbox lists the session's question. loop-inbox reads a held lock as a loop that still runs.
The lock is <session>.lock, held with flock. The kernel frees it however the loop ends, killed included, where a process
id the system may hand out again cannot tell a loop that runs from one that was killed. The lock file stays once the
loop ends: docs/decisions.md → `loop-inbox: …` says why.
The lock is <session>.lock, held with flock, which the kernel frees however the loop ends, killed included. The lock
file stays once the loop ends: docs/decisions.md → `loop-inbox: …` says why.
"""
import contextlib
import fcntl
@@ -19,7 +18,7 @@ def lock_file(folder: pathlib.Path, session: str) -> pathlib.Path:
@contextlib.contextmanager
def locked(folder: pathlib.Path, session: str) -> typing.Iterator[Failure | None]:
"""Holds the session's lock while the with block runs; a Failure where it cannot. loop-inbox holds it for a moment where it finds the session's question still listed, so a loop starting after another was killed may meet it held."""
"""Holds the session's lock while the with block runs, or yields a Failure where it cannot. loop-inbox's drop_ended takes the lock for a moment on each ask still on disk, so a loop may find it held."""
try:
make_private(folder)
except OSError as failure:
@@ -44,7 +43,7 @@ def locked(folder: pathlib.Path, session: str) -> typing.Iterator[Failure | None
def running(folder: pathlib.Path, session: str) -> bool:
"""Whether a loop holds the session's lock; False also where the lock cannot be read."""
"""Whether a loop holds the session's lock; False also where the lock file cannot be opened or locked."""
try:
with open(lock_file(folder, session)) as handle:
fcntl.flock(handle, fcntl.LOCK_SH | fcntl.LOCK_NB)
+2 -2
View File
@@ -68,7 +68,7 @@ def human(content):
def asks_and_answers(folder):
"""What loop-inbox could still read in folder; the session's lock file stays once the loop lets it go."""
"""folder's files but the session's lock file, which stays once the loop lets go of the lock."""
return [path for path in sorted(folder.iterdir()) if path.suffix != ".lock"]
@@ -1285,7 +1285,7 @@ class SessionLock(unittest.TestCase):
self.addCleanup(shutil.rmtree, self.folder)
self.path = session_lock.lock_file(self.folder, "s1")
def test_a_loop_cannot_take_the_lock_another_holds_and_runs_until_it_lets_it_go(self):
def test_a_held_lock_refuses_a_second_taker_and_reads_as_running_until_let_go(self):
self.assertFalse(session_lock.running(self.folder, "s1"))
with session_lock.locked(self.folder, "s1") as first:
self.assertIsNone(first)
+10 -10
View File
@@ -1,12 +1,12 @@
"""The files by which a loop-claude shows its interrupt in loop-inbox, and takes an answer typed there.
A waiting loop keeps its ask in <session>.json in Paths.waiting: the question, with an id new for each question.
<session> is the worker's session id, so a loop lists one question at a time. The loop holds the session's lock, as
session_lock.py describes, while it lists the ask, and removes the ask once the question is answered or the loop stops.
loop-inbox lists an ask only while its lock is held, and removes one whose lock is free. The ask's state says whether
the loop waits for an answer, or has claude open for it, on the loop's own terminal or in loop-inbox. loop-inbox
answers by replacing <session>.answer, which names the ask's id. So an answer meant for an earlier question, or sent as
the loop took an answer on its terminal, never answers a later one.
<session> is the worker's session id, so a loop lists one question at a time. The loop holds the session's lock while it
lists the ask, and removes the ask once the question is answered or the loop stops. loop-inbox lists an ask only while
its lock is held, and removes one whose lock is free. The ask's state says whether the loop waits for an answer, or has
claude open for it, on the loop's own terminal or in loop-inbox. loop-inbox answers by replacing <session>.answer, which
names the ask's id. So an answer meant for an earlier question, or sent as the loop took an answer on its terminal,
never answers a later one.
/talk from loop-inbox goes in three steps, so that only one claude ever holds the worker's session:
1. loop-inbox's claim_talk sends ClaudeOpening, with its pid.
@@ -121,7 +121,7 @@ def shown(folder: pathlib.Path, ask: Ask, effects: Effects) -> None:
@contextlib.contextmanager
def listed(folder: pathlib.Path, ask: Ask, effects: Effects) -> typing.Iterator[None]:
"""Lists ask in loop-inbox while the with block runs, then takes it out, with any answer. A failure only warns: the question is still answered on the loop's terminal, and loop-inbox drops the ask once the loop's process ends."""
"""Lists ask in loop-inbox while the with block runs, then takes it out, with any answer. A failure to list it only warns: the question is still answered on the loop's terminal."""
with locked(folder, ask.session) as unlocked:
if unlocked:
say(f"loop-inbox cannot show this question: {unlocked.reason}", effects)
@@ -131,8 +131,8 @@ def listed(folder: pathlib.Path, ask: Ask, effects: Effects) -> typing.Iterator[
try:
yield
finally:
if unwithdrawn := withdraw(folder, ask.session):
say(f"loop-inbox may still list this question: {unwithdrawn.reason}", effects)
# An ask this fails to remove is left to loop-inbox, which stops listing it once the lock is let go, and removes it.
withdraw(folder, ask.session)
def waiting(folder: pathlib.Path) -> list[Ask]:
@@ -209,7 +209,7 @@ def received(folder: pathlib.Path, ask: Ask) -> Message | None:
match data:
case {"kind": "Typed", "text": str(text)}:
return Typed(text)
# A pid of 0 or below would name a process group to the loop's alive.
# alive is always True for a pid of 0 or below, which names a process group or every process, so the loop would wait for claude forever.
case {"kind": "ClaudeOpening", "pid": int(pid)} if pid > 0:
return ClaudeOpening(pid)
case {"kind": "ClaudeEnded"}: