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
+38 -24
View File
@@ -1,22 +1,25 @@
"""The files by which a loop-claude shows loop-inbox that it runs, and the todo.md items its chunk takes.
A running loop holds <session>.running.lock in Paths.waiting for its whole run, and keeps <session>.running beside it:
its checkout, and the ID and tagline of each item its chunk takes. loop-inbox lists a loop only while its lock is held,
so a killed loop drops off the list though its file stays. The file is replaced whole on each change, which gives it a
new inode, so the lock is a file of its own.
A running loop holds its session's lock (session_lock.py) in Paths.waiting for its whole run, and keeps <session>.running
beside it: its checkout, and the ID and tagline of each item its chunk takes. Those are the item it was routed on, then
the items the judge's latest ruling found the worker took in, each once, with its tagline on the default branch as last
fetched. loop-inbox lists a loop, and its questions (waiting.py), only while its lock is held, so a killed loop drops off
the list though its files stay; loop-inbox removes them.
"""
import contextlib
import dataclasses
import json
import pathlib
import shutil
import typing
from loop_claude.effects import Effects, say
from failure import Failure
from loop_claude.session_lock import held, taken
from loop_claude.session_lock import held, running
from todo_table import items
from file_writes import replace_json
# How many times, 0.1 s apart, a loop tries to take its lock: loop-inbox takes it for a moment each time it looks whether the loop runs.
# How many times, 0.1 s apart, a loop tries to take its lock: loop-inbox takes it for a moment each time it looks whether the loop has ended.
LOCK_TRIES = 10
@@ -40,39 +43,47 @@ class RunningLoop:
Show = typing.Callable[[tuple[Taken, ...]], None]
@dataclasses.dataclass(frozen=True)
class Listed:
"""What a running loop shows loop-inbox through."""
# Where the loop keeps its questions: Paths.waiting while it holds its lock. Else a folder of its own, which loop-inbox doesn't read, so the loop never touches the questions of the loop that holds the lock.
folder: pathlib.Path
show: Show
def running_file(folder: pathlib.Path, session: str) -> pathlib.Path:
return folder / f"{session}.running"
def running_lock(folder: pathlib.Path, session: str) -> pathlib.Path:
return folder / f"{session}.running.lock"
@contextlib.contextmanager
def shown_running(folder: pathlib.Path, session: str, root: str, effects: Effects) -> typing.Iterator[Show]:
"""Lists the loop in loop-inbox as running while the with block runs, at first with no item, and yields what lists the items its chunk takes. A failure only warns, and the loop runs unlisted."""
def shown_running(folder: pathlib.Path, session: str, root: str, effects: Effects) -> typing.Iterator[Listed]:
"""Lists the loop in loop-inbox as running while the with block runs, at first with no item. A failure only warns: the loop runs unlisted, and its questions are answered on its terminal."""
with contextlib.ExitStack() as stack:
if unlocked := locked_soon(stack, folder, running_lock(folder, session), effects):
say(f"loop-inbox cannot list this loop as running: {unlocked.reason}", effects)
yield lambda items: None
if unlocked := locked_soon(stack, folder, session, effects):
say(f"loop-inbox cannot list this loop or its questions: {unlocked.reason}", effects)
unlisted = folder / "unlisted" / effects.new_id()
try:
yield Listed(unlisted, lambda items: None)
finally:
shutil.rmtree(unlisted, ignore_errors=True)
return
def show(items: tuple[Taken, ...]) -> None:
if unshown := replace_json(running_file(folder, session), {"root": root, "items": [dataclasses.asdict(item) for item in items]}):
say(f"loop-inbox cannot list this loop's items: {unshown.reason}", effects)
show(())
try:
yield show
yield Listed(folder, show)
finally:
# A file this fails to remove stays, and loop-inbox leaves it out once the lock is let go.
# A file this fails to remove stays until loop-inbox removes it, once the lock is let go.
with contextlib.suppress(OSError):
running_file(folder, session).unlink(missing_ok=True)
def locked_soon(stack: contextlib.ExitStack, folder: pathlib.Path, path: pathlib.Path, effects: Effects) -> Failure | None:
"""Holds the lock at path until stack closes, trying LOCK_TRIES times; the last failure where every try failed."""
def locked_soon(stack: contextlib.ExitStack, folder: pathlib.Path, session: str, effects: Effects) -> Failure | None:
"""Holds the session's lock until stack closes, trying LOCK_TRIES times; the last failure where every try failed."""
for _ in range(LOCK_TRIES):
attempt = contextlib.ExitStack()
unlocked = attempt.enter_context(held(folder, path))
unlocked = attempt.enter_context(held(folder, session))
if not unlocked:
stack.enter_context(attempt)
return None
@@ -87,7 +98,7 @@ def running_loops(folder: pathlib.Path) -> list[RunningLoop]:
paths = list(folder.glob("*.running"))
except OSError:
return []
loops = [loop for path in paths if (loop := read_running(path)) and taken(running_lock(folder, loop.session))]
loops = [loop for path in paths if (loop := read_running(path)) and running(folder, loop.session)]
return sorted(loops, key=lambda loop: loop.root)
@@ -110,6 +121,9 @@ def is_item(item: object) -> bool:
return False
def chunk_items(routed: int, taken_in: tuple[int, ...]) -> tuple[int, ...]:
"""The IDs of the items a chunk takes: routed, the item it was routed on, 0 for none, then those the judge found it took in, once each."""
return tuple(dict.fromkeys(([routed] if routed else []) + list(taken_in)))
def taken_items(todo: str | Failure, routed: int, taken_in: tuple[int, ...]) -> tuple[Taken, ...]:
"""The items a chunk takes, as the module docstring says, with their taglines from todo, the default branch's todo.md. routed is 0 for none."""
found = items(todo) if isinstance(todo, str) else []
taglines = {item.id: item.tagline.removeprefix("**").removesuffix("**") for item in found} if isinstance(found, list) else {}
ids = dict.fromkeys(([routed] if routed else []) + list(taken_in))
return tuple(Taken(id_, taglines.get(id_, "")) for id_ in ids)