Key loop-claude's session per slot, so two loops of one project run side by side

This commit is contained in:
2026-11-15 14:09:55 +01:00
parent 510df4b31d
commit cbf007f68c
10 changed files with 111 additions and 11 deletions
+31
View File
@@ -0,0 +1,31 @@
"""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)."""
import os
import pathlib
from failure import Failure
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.
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.
MOST = 64
def held_slot(home: pathlib.Path, key: str) -> 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."""
folder = home / SLOTS / key
try:
folder.mkdir(parents=True, exist_ok=True)
taken = taken_slot(folder, MOST)
except OSError as failure:
return Failure(f"cannot take a slot in {folder}: {failure.strerror}")
if taken is None:
return Failure(f"{MOST} loops of this project are running already; `fuser {folder}/slot-*` names them")
slot, fd = taken
os.set_inheritable(fd, True)
return slot
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."""
return f"A {key} loop-claude" if slot == 0 else f"A {key} loop-claude {slot + 1}"
+8 -2
View File
@@ -3,6 +3,7 @@ import pathlib
import typing
from transcript_archive.location import archive_root, project_archive
from loop_claude.loop.chunk_slot import held_slot, session_name
from loop_claude.loop.chunk_start import archived_route, chunk_start, fetched_todo, routed
from loop_claude.loop.routing_check import check_routing
from loop_claude.project.checkout import checkout_root
@@ -46,8 +47,13 @@ def run_loop(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launc
if isinstance(loop_run, Stop):
return loop_run
baseline = fingerprint(launcher)
# "A " is the human's cue in the session picker that the session is the loop's.
session = session_named(f"A {project_key(loop_run.root, paths.home)} loop-claude", paths.claude_code_transcripts)
key = project_key(loop_run.root, paths.home)
# A reload's exec keeps the slot's lock open, so the edited script takes the slot it is handed.
slot = held_slot(paths.home, key) if loop_run.slot is None else loop_run.slot
if isinstance(slot, Failure):
return Stop(1, slot.reason)
loop_run = dataclasses.replace(loop_run, slot=slot)
session = session_named(session_name(key, slot), paths.claude_code_transcripts)
loop = Loop(root=loop_run.root, session=session, paths=paths, env=env, launcher=launcher, effects=effects)
while True:
if stop := exec_if_edited(loop, baseline, loop_run):
+3 -1
View File
@@ -25,6 +25,8 @@ class LoopRun:
route: Route | None = None
# How many unfinished verdicts the chunk has had running; a finished chunk and a rerun each set it to 0. It stays apart from route and unfinished_evidence, though all three change together at each verdict: merging or renaming a field stops a running loop at its next reload.
unfinished_run: int = 0
# The project's slot this loop holds (chunk_slot.py); None until it takes one, and from a loop started before slots, which then takes one.
slot: int | None = None
class Unreadable(Exception):
@@ -75,7 +77,7 @@ def decoded_route(value: object) -> Route | None:
# One decoder per field of LoopRun, which a test holds to the dataclass's fields: a field missing here stops a loop at the reload after it joined.
LOOP_RUN_FIELDS = {"cycle_number": exactly(int), "root": exactly(str), "task": exactly(str), "unfinished_evidence": exactly(str), "route": decoded_route, "unfinished_run": exactly(int)}
LOOP_RUN_FIELDS = {"cycle_number": exactly(int), "root": exactly(str), "task": exactly(str), "unfinished_evidence": exactly(str), "route": decoded_route, "unfinished_run": exactly(int), "slot": lambda value: None if value is None else exactly(int)(value)}
def fingerprint(launcher: pathlib.Path) -> str:
+7 -3
View File
@@ -43,8 +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
after 600 s and is stopped, leaves the worker to find the facts itself.
Each project has one session, named "A <key> loop-claude", where <key> is the checkout's path
relative to ~/code, or to / outside it. The session is deleted when its chunk finishes. At every
Each loop runs one chunk at a time, in a session named "A <key> loop-claude", where <key> is the
checkout's path relative to ~/code, or to / outside it. Loops of one project run side by side: each
holds the lowest slot no other holds, a file locked under ~/.local/state/loop-claude/<key>/, and
from the second slot on the session's name ends in the slot's number, as "A <key> loop-claude 2".
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
copied into a folder per chunk under $LOOP_CLAUDE_TRANSCRIPTS/<key> (default root
~/.claude/loop-claude-transcripts), named by its hash, with an outcomes file beside it: the task,
@@ -95,6 +98,7 @@ loop-claude --help succeeds: the loop reloads into the edited script.
Exit 1, saying why, when:
- no task is given
- git cannot start or finds no checkout
- its slot in the project cannot be taken
- the goals file, or AGENTS.md, docs/decisions.md, ~/.claude/CLAUDE.md, ~/.claude/rules/*.md or
the project's review standards where they exist, cannot be read or is not UTF-8
- no config names the checkout, its config cannot be read, or the goals file holds no premises
@@ -123,4 +127,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
anything typed
- the judge rules a chunk unfinished 6 cycles running; rerun to give it 6 more
Rerunning resumes any open session."""
Rerunning resumes the open session of the slot the rerun takes."""