Answer stability round 1: 0 🔴, 1 🟡 (0 misread); a loop takes the lowest free slot whose session is open first, so a rerun resumes a chunk left open

This commit is contained in:
2026-11-15 14:40:43 +01:00
parent 4854ea0624
commit e8988b5192
4 changed files with 32 additions and 17 deletions
+11 -10
View File
@@ -1,27 +1,28 @@
"""The slot a loop holds in its project, so that loops of one project, each on its own chunk, run side by side. Each loop names its worker's session after its slot, and the archive and the chunk's route follow the session's transcript (bin/transcript_archive/__init__.py).""" """The slot a loop holds in its project, so that loops of one project, each on its own chunk, run side by side, each in a session named after its slot."""
import dataclasses import dataclasses
import os import os
import pathlib import pathlib
from failure import Failure from failure import Failure
from machine_load.slots import first_free_slot
from loop_claude.loop.reload import LoopRun from loop_claude.loop.reload import LoopRun
from loop_state.paths import Paths from loop_state.paths import Paths
from transcript_archive.project_key import project_key from transcript_archive.project_key import project_key
from claude_code.session import Session, session_named from claude_code.session import Session, session_named, session_open
from machine_load.slots import taken_slot
# Under the home folder, a folder per project key holds a file per slot, which a running loop keeps locked with flock. # Under the home folder, a folder per project key holds a file per slot, which a running loop keeps locked with flock.
SLOTS = ".local/state/loop-claude" SLOTS = ".local/state/loop-claude"
# More loops than one project runs at once: todo.md item 375 runs at most the number the project's config allows. # A bound no project reaches. Once todo.md item 375 lands, the project's config sets how many loops run at once.
MOST = 64 MOST = 64
def held_slot(home: pathlib.Path, key: str) -> int | Failure: def held_slot(home: pathlib.Path, key: str, claude_code_transcripts: pathlib.Path) -> int | Failure:
"""The lowest slot no running loop of the project holds, which this loop now holds until it exits, a reload's exec included; or why it could take none.""" """The slot this loop takes, or why it could take none: the lowest free slot whose session is open, so that a rerun resumes a chunk left open, or else the lowest free slot. The loop holds it until it exits; a reload's exec keeps the lock."""
folder = home / SLOTS / key folder = home / SLOTS / key
order = sorted(range(MOST), key=lambda slot: not session_open(session_named(session_name(key, slot), claude_code_transcripts)))
try: try:
folder.mkdir(parents=True, exist_ok=True) folder.mkdir(parents=True, exist_ok=True)
taken = taken_slot(folder, MOST) taken = first_free_slot(folder, order)
except OSError as failure: except OSError as failure:
return Failure(f"cannot take a slot in {folder}: {failure.strerror}") return Failure(f"cannot take a slot in {folder}: {failure.strerror}")
if taken is None: if taken is None:
@@ -32,14 +33,14 @@ def held_slot(home: pathlib.Path, key: str) -> int | Failure:
def session_name(key: str, slot: int) -> str: def session_name(key: str, slot: int) -> str:
""""A " is the human's cue in the session picker that the session is the loop's. Slot 0 keeps the name every loop had before slots, so a chunk left open then resumes in it.""" """"A " is the human's cue in the session picker that the session is the loop's. Slot 0 keeps the name every loop had before slots, so a chunk left open before slots resumes in it."""
return f"A {key} loop-claude" if slot == 0 else f"A {key} loop-claude {slot + 1}" return f"A {key} loop-claude" if slot == 0 else f"A {key} loop-claude {slot + 1}"
def slotted(loop_run: LoopRun, paths: Paths) -> tuple[LoopRun, Session] | Failure: def slotted(loop_run: LoopRun, paths: Paths) -> tuple[LoopRun, Session] | Failure:
"""loop_run holding its slot, and the worker's session, named after the slot. A reload's exec keeps the slot's lock open, so the edited script keeps the slot it is handed.""" """loop_run holding its slot, and the worker's session, named after the slot. A reloaded script keeps the slot it is handed."""
key = project_key(loop_run.root, paths.home) key = project_key(loop_run.root, paths.home)
slot = held_slot(paths.home, key) if loop_run.slot is None else loop_run.slot slot = held_slot(paths.home, key, paths.claude_code_transcripts) if loop_run.slot is None else loop_run.slot
if isinstance(slot, Failure): if isinstance(slot, Failure):
return slot return slot
return dataclasses.replace(loop_run, slot=slot), session_named(session_name(key, slot), paths.claude_code_transcripts) return dataclasses.replace(loop_run, slot=slot), session_named(session_name(key, slot), paths.claude_code_transcripts)
+6 -5
View File
@@ -43,10 +43,11 @@ what it prints in the first prompt (prompts.preflight_facts in bin/loop_claude/c
chunk-preflight --help says what it finds and makes. A preflight that fails, or is still running chunk-preflight --help says what it finds and makes. A preflight that fails, or is still running
after 600 s and is stopped, leaves the worker to find the facts itself. after 600 s and is stopped, leaves the worker to find the facts itself.
Each loop runs one chunk at a time, in a session named "A <key> loop-claude", where <key> is the Loops of one project run side by side, each on one chunk at a time. Each holds a slot of the
checkout's path relative to ~/code, or to / outside it. Loops of one project run side by side: each project, the file slot-<n> under ~/.local/state/loop-claude/<key>/, locked while the loop runs.
holds the lowest slot no other holds, a file locked under ~/.local/state/loop-claude/<key>/, and <key> is the checkout's path relative to ~/code, or to / outside it. A loop takes the lowest free
from the second slot on the session's name ends in the slot's number, as "A <key> loop-claude 2". slot whose session is open, so a rerun resumes a chunk left open, or else the lowest free slot.
slot-0's session is named "A <key> loop-claude", and slot-<n>'s "A <key> loop-claude <n+1>".
The session is deleted when its chunk finishes. At every The session is deleted when its chunk finishes. At every
interrupt, and as every cycle ends, Ctrl-C and a failure included, the session's transcript is interrupt, and as every cycle ends, Ctrl-C and a failure included, the session's transcript is
copied into a folder per chunk under $LOOP_CLAUDE_TRANSCRIPTS/<key> (default root copied into a folder per chunk under $LOOP_CLAUDE_TRANSCRIPTS/<key> (default root
@@ -127,4 +128,4 @@ Exit 2, when the human is needed to go on:
- the claude opened for /talk, or for a release ready to cut, ends without - the claude opened for /talk, or for a release ready to cut, ends without
anything typed anything typed
- the judge rules a chunk unfinished 6 cycles running; rerun to give it 6 more - the judge rules a chunk unfinished 6 cycles running; rerun to give it 6 more
Rerunning resumes the open session of the slot the rerun takes.""" Rerunning resumes an open session that no running loop holds, the lowest one first."""
+8
View File
@@ -1240,6 +1240,14 @@ class ChunkSlots(unittest.TestCase):
folder = self.home / ".local" / "state" / "loop-claude" / "proj" folder = self.home / ".local" / "state" / "loop-claude" / "proj"
self.assertEqual(self.held(), {str(folder / "slot-0"): True, str(folder / "slot-1"): True}) self.assertEqual(self.held(), {str(folder / "slot-0"): True, str(folder / "slot-1"): True})
def test_a_rerun_takes_the_lowest_free_slot_whose_session_is_open_before_a_lower_closed_one(self):
loop_run = main.LoopRun(cycle_number=0, root=str(self.home / "code" / "proj"), task="do it", unfinished_evidence="")
for name in ("A proj loop-claude 3", "A proj loop-claude 4"):
transcript = self.paths.claude_code_transcripts / "-home-proj" / f"{session.session_named(name, self.paths.claude_code_transcripts).id}.jsonl"
transcript.parent.mkdir(parents=True, exist_ok=True)
transcript.write_text("")
self.assertEqual([chunk_slot.slotted(loop_run, self.paths)[0].slot for _ in range(4)], [2, 3, 0, 1])
def test_a_reload_keeps_the_slot_it_is_handed_and_takes_no_other(self): def test_a_reload_keeps_the_slot_it_is_handed_and_takes_no_other(self):
loop_run = main.LoopRun(cycle_number=3, root="/srv/proj", task="do it", unfinished_evidence="", slot=2) loop_run = main.LoopRun(cycle_number=3, root="/srv/proj", task="do it", unfinished_evidence="", slot=2)
self.assertEqual(chunk_slot.slotted(loop_run, self.paths), (loop_run, session.session_named("A srv/proj loop-claude 3", self.paths.claude_code_transcripts))) self.assertEqual(chunk_slot.slotted(loop_run, self.paths), (loop_run, session.session_named("A srv/proj loop-claude 3", self.paths.claude_code_transcripts)))
+7 -2
View File
@@ -27,8 +27,13 @@ def slot_folder(env: typing.Mapping[str, str], home: pathlib.Path) -> pathlib.Pa
def taken_slot(folder: pathlib.Path, slots: int) -> tuple[int, int] | None: def taken_slot(folder: pathlib.Path, slots: int) -> tuple[int, int] | None:
"""The number and open file of a slot this call locked; None while every slot is held. Raises OSError where a slot file cannot be opened or locked.""" """The number and open file of the lowest slot this call locked; None while every slot is held. Raises OSError where a slot file cannot be opened or locked."""
for slot in range(slots): return first_free_slot(folder, range(slots))
def first_free_slot(folder: pathlib.Path, order: typing.Iterable[int]) -> tuple[int, int] | None:
"""The number and open file of the first slot in order that this call locked; None while every slot in order is held. Raises OSError where a slot file cannot be opened or locked."""
for slot in order:
fd = os.open(folder / f"slot-{slot}", os.O_RDWR | os.O_CREAT, 0o644) fd = os.open(folder / f"slot-{slot}", os.O_RDWR | os.O_CREAT, 0o644)
try: try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)