Files
claude-config/bin/loop_state/running_loops.py
T

138 lines
5.8 KiB
Python

"""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 its session's lock (session_lock.py) in Paths.waiting for its whole run. Beside the lock it keeps
<session>.running, which holds the loop's checkout and each item its chunk takes, by ID and by its tagline on the default
branch as last fetched. The items are the one the chunk was routed on, then those the worker names on its latest
`Chunk items:` line, each listed once. loop-inbox lists a loop, and its question (waiting/), only while the loop holds its
lock. So a killed loop drops off the list, though its files stay until loop-inbox removes them: <session>.running
through drop_ended_loops here, and the question through waiting.listing.drop_ended.
"""
import contextlib
import dataclasses
import json
import pathlib
import shutil
import typing
from loop_effects.effects import Effects, say
from loop_state.session_lock import ended, held, running
from failure import Failure
from release_plan.todo_table import items
from records.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 checks whether the loop has ended.
LOCK_TRIES = 10
@dataclasses.dataclass(frozen=True)
class Taken:
"""A todo.md item a chunk takes."""
id: int
# The item's tagline without its bold marks; empty where the default branch's todo.md can't be read or holds no such item.
tagline: str
@dataclasses.dataclass(frozen=True)
class RunningLoop:
session: str
# The checkout the loop runs in.
root: str
# The routed item first; empty until the chunk has an item.
items: tuple[Taken, ...]
Show = typing.Callable[[tuple[Taken, ...]], None]
@dataclasses.dataclass(frozen=True)
class Listed:
"""How a running loop shows itself in loop-inbox."""
# Where the loop keeps its questions: Paths.waiting where it holds its session's lock. Where another loop on the session holds the lock, a folder of its own that loop-inbox doesn't read, so this loop never touches the other's questions.
folder: pathlib.Path
show: Show
def running_file(folder: pathlib.Path, session: str) -> pathlib.Path:
return folder / f"{session}.running"
@contextlib.contextmanager
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, 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 Listed(folder, show)
finally:
# 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, session: str, effects: Effects) -> Failure | None:
"""Holds the session's lock until stack closes, trying LOCK_TRIES times; returns None once it holds the lock, else the last try's failure."""
for _ in range(LOCK_TRIES):
attempt = contextlib.ExitStack()
unlocked = attempt.enter_context(held(folder, session))
if not unlocked:
stack.enter_context(attempt)
return None
attempt.close()
effects.sleep(0.1)
return unlocked
def running_loops(folder: pathlib.Path) -> list[RunningLoop]:
"""The loops that hold their lock, by checkout. A file that cannot be read or parsed is left out."""
try:
paths = list(folder.glob("*.running"))
except OSError:
return []
loops = [loop for path in paths if (loop := read_running(path)) and running(folder, loop.session)]
return sorted(loops, key=lambda loop: loop.root)
def drop_ended_loops(folder: pathlib.Path) -> None:
"""Removes each running file whose session's lock no loop holds: a loop that was killed leaves it behind. A file it cannot remove stays on disk, and running_loops leaves it out all the same."""
for session in ended(folder, "*.running"):
with contextlib.suppress(OSError):
running_file(folder, session).unlink(missing_ok=True)
def read_running(path: pathlib.Path) -> RunningLoop | None:
try:
data = json.loads(path.read_text(encoding="utf-8"))
except (OSError, ValueError):
return None
match data:
case {"root": str(root), "items": list(items)} if all(is_item(item) for item in items):
return RunningLoop(path.name.removesuffix(".running"), root, tuple(Taken(item["id"], item["tagline"]) for item in items))
return None
def is_item(item: object) -> bool:
match item:
# type(), not isinstance(): JSON true is no ID.
case {"id": id_, "tagline": str()} if type(id_) is int:
return True
return False
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, the item the chunk was routed on, 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)