198 lines
12 KiB
Python
198 lines
12 KiB
Python
"""The chunks of one project that run at once, each in its own tmux window: which may start now, and how each starts (docs/decisions.md → `loop-claude: each chunk runs in its own tmux window`)."""
|
|
import contextlib
|
|
import dataclasses
|
|
import fcntl
|
|
import pathlib
|
|
import re
|
|
import typing
|
|
|
|
from touch_predictions.beside import Beside, chunks_beside
|
|
from loop_claude.cycle import chunk_items
|
|
from git_cli.git_repo import default_ref
|
|
from loop_effects.effects import Effects, say
|
|
from failure import Failure
|
|
from project_config.main import located
|
|
from loop_claude.loop.reload import LoopRun
|
|
from release_plan.next_chunk import NextChunk, NoItem, planned
|
|
from plan.touches import overlaps, predicted, Touches
|
|
from loop_state.paths import Paths
|
|
from transcript_archive.project_key import project_key
|
|
from release_plan.release_standing import project_plan
|
|
from claude_code.transcript import read_transcript
|
|
from claude_code.session import Session, session_named, transcript_file
|
|
from agent_cycle import tmux
|
|
|
|
# Under the home folder, a folder per project key holds LOCK.
|
|
STATE = ".local/state/loop-claude"
|
|
# The file an entry point keeps locked with flock while it lists the windows of a project's chunks and starts new ones. Two loops whose chunks finish together then don't start a chunk twice, and do-epic, which holds the same lock, never starts a chunk in a checkout while loop-claude does.
|
|
LOCK = "starting"
|
|
# The flag that tells the loop in a window its chunk, as `--chunk item-372`. Nobody types it, so no panel picked it.
|
|
CHUNK_FLAG = "--chunk"
|
|
# The chunk of a plan that names no item for the next chunk: a release to plan, a release review, or a task outside the plan.
|
|
NO_ITEM = "no-item"
|
|
# Seconds a fetch of origin may take before a chunk starts; past them, the plan reads as last fetched.
|
|
FETCH_TIMEOUT = 60
|
|
# The prefix of the variables loop-claude reads. A window gets the tmux server's environment, not the starting terminal's, so they are handed to each window by name.
|
|
HANDED = "LOOP_CLAUDE_"
|
|
|
|
|
|
def window_name(item_id: int) -> str:
|
|
return f"item-{item_id}"
|
|
|
|
|
|
def item_of(chunk: str) -> int | None:
|
|
"""The ID of the plan item a chunk starts from; None for NO_ITEM."""
|
|
named = re.fullmatch(r"item-([0-9]+)", chunk)
|
|
return int(named[1]) if named else None
|
|
|
|
|
|
def chunk_session(key: str, chunk: str, claude_code_transcripts: pathlib.Path) -> Session:
|
|
"""The worker's session for a chunk. A rerun finds a chunk left open by this name. "A " is the human's cue in the session picker that the session is the loop's."""
|
|
return session_named(f"A {key} loop-claude {chunk}", claude_code_transcripts)
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def starting(folder: pathlib.Path) -> typing.Iterator[None]:
|
|
"""Holds the LOCK in folder: project_lock's for a project, or do-epic's for an epic. Raises OSError where the file cannot be opened or locked."""
|
|
folder.mkdir(parents=True, exist_ok=True)
|
|
with open(folder / LOCK, "a") as lock:
|
|
fcntl.flock(lock, fcntl.LOCK_EX)
|
|
yield
|
|
|
|
|
|
def project_lock(root: str, home: pathlib.Path) -> pathlib.Path:
|
|
"""The folder of the LOCK that everything starting a chunk in the checkout root holds."""
|
|
return home / STATE / project_key(root, home)
|
|
|
|
|
|
def epic_chunks(root: str, run: tmux.Tmux) -> list[tmux.Placed]:
|
|
"""The windows of do-epic's sessions whose chunks run in the checkout root. A parked chunk doesn't run."""
|
|
return [placed for placed in tmux.everywhere(run) if placed.session.startswith(f"{tmux.EPIC}-") and placed.root == root and not placed.window.dead and not placed.window.parked]
|
|
|
|
|
|
def held_items(chunks: typing.Iterable[str], key: str, claude_code_transcripts: pathlib.Path) -> set[int]:
|
|
"""The plan items these chunks hold: the one each started from, and those its worker names on its latest `Chunk items:` line so far. A chunk whose transcript cannot be read holds the item it started from."""
|
|
held: set[int] = set()
|
|
for chunk in chunks:
|
|
held.update(filter(None, [item_of(chunk)]))
|
|
transcript = transcript_file(chunk_session(key, chunk, claude_code_transcripts))
|
|
entries = read_transcript(transcript) if transcript else []
|
|
held.update([] if isinstance(entries, Failure) else chunk_items.named(entries))
|
|
return held
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Room:
|
|
"""What a project's windows leave room for, under its limit of chunks at once."""
|
|
# The chunks whose windows run.
|
|
running: list[str]
|
|
# The chunks whose windows are dead: their loop stopped or failed, and the window keeps its last lines and its items.
|
|
stopped: list[str]
|
|
# The stopped chunks to start again, before any chunk from the plan.
|
|
restart: list[str]
|
|
# How many chunks from the plan may start beside them.
|
|
left: int
|
|
|
|
|
|
def room_of(windows: list[tmux.Listed], own: str | None, leaving: bool, limit: int) -> Room:
|
|
"""The room windows leave under limit. own is the chunk of the loop that asks, None for the human's loop-claude <task>. leaving is True where own's chunk finished: its window no longer counts as running, and no stopped chunk starts again, since only a loop that isn't leaving, such as the human's loop-claude <task>, restarts one."""
|
|
running = [window.name for window in windows if not window.dead and not (leaving and window.name == own)]
|
|
# Below 0 where the config lowered parallel-chunks while more chunks ran.
|
|
room = max(limit - len(running), 0)
|
|
stopped = [window.name for window in windows if window.dead]
|
|
restart = [] if leaving else stopped[:room]
|
|
return Room(running, stopped, restart, room - len(restart))
|
|
|
|
|
|
def may_start(plan: NextChunk | NoItem, running: list[str], held: set[int], room: int) -> list[str]:
|
|
"""The chunks to start now beside the running ones, whose chunks hold the items in held: the plan's next item and the items touch_predictions picks to run beside it, in the plan's order, up to room of them. Each leaves out an item in held, and one whose `Touches:` prediction overlaps a held item's, or where either has none. A plan without an item starts NO_ITEM, and only alone."""
|
|
if isinstance(plan, NoItem):
|
|
return [] if running or room < 1 else [NO_ITEM]
|
|
beside = chunks_beside(plan.items, plan.first)
|
|
picked = [plan.first.id, *(beside.picked if isinstance(beside, Beside) else [])]
|
|
details = {item.id: item.detail for item in plan.items}
|
|
# chunks_beside checks overlaps against the plan's next item only, and a running chunk's item may no longer be that one once an item merged. A held item that left the plan overlaps nothing.
|
|
theirs = [predicted(details[item_id]) for item_id in sorted(held) if item_id in details]
|
|
free = [item_id for item_id in picked if item_id not in held and clear_of(predicted(details[item_id]), theirs)]
|
|
return [window_name(item_id) for item_id in free][: max(room, 0)]
|
|
|
|
|
|
def clear_of(ours: Touches | Failure | None, theirs: list[Touches | Failure | None]) -> bool:
|
|
"""Whether a prediction overlaps none of theirs; not where one of them is missing or unreadable, unless theirs is empty."""
|
|
return not theirs or (isinstance(ours, dict) and all(isinstance(touches, dict) and not overlaps(ours, touches) for touches in theirs))
|
|
|
|
|
|
def next_plan(root: str, home: pathlib.Path, effects: Effects) -> NextChunk | NoItem | Failure:
|
|
"""The plan on the default branch, fetched now where origin answers, with its next item, as chunk-preflight reads it."""
|
|
config = located(root, home)
|
|
if isinstance(config, Failure):
|
|
return config
|
|
# A chunk that just merged left the plan on origin. A fetch that fails leaves the plan as last fetched, which chunk-preflight then reads too.
|
|
effects.git("fetch", "--quiet", "origin", cwd=root, timeout=FETCH_TIMEOUT)
|
|
ref = default_ref(root, FETCH_TIMEOUT, run=effects.git)
|
|
if isinstance(ref, Failure):
|
|
return ref
|
|
held = project_plan(config, ref, root, effects.git)
|
|
return planned(held.plan, held.cut, ref, config.branch, None if config.tracked("plan") else config.plan_label)
|
|
|
|
|
|
def start_chunks(loop_run: LoopRun, leaving: bool, paths: Paths, env: typing.Mapping[str, str], launcher: pathlib.Path, effects: Effects) -> list[str] | Failure:
|
|
"""Starts a loop-claude in a new window for each chunk of loop_run's project that may start now, and returns the chunks started. leaving says that loop_run's own chunk finished, so its window no longer counts as running; without it, the human started loop-claude. A Failure says why no chunk may start, or why one could not; those before it run."""
|
|
config = located(loop_run.root, paths.home)
|
|
if isinstance(config, Failure):
|
|
return config
|
|
session = tmux.session_name(tmux.LOOP, project_key(loop_run.root, paths.home))
|
|
handed = {name: value for name, value in env.items() if name.startswith(HANDED)}
|
|
lock = project_lock(loop_run.root, paths.home)
|
|
try:
|
|
with starting(lock):
|
|
windows = tmux.listed(session, effects.tmux)
|
|
if refused := refusal(session, windows, loop_run.root, effects.tmux):
|
|
return refused
|
|
chunks = chunks_to_start(windows, loop_run, leaving, config.parallel_chunks, paths, effects)
|
|
for chunk in chunks:
|
|
started = tmux.start_chunk(tmux.Window(session, chunk, ""), loop_run.root, handed, [str(launcher), CHUNK_FLAG, chunk, loop_run.task], windows, True, effects.tmux)
|
|
if isinstance(started, Failure):
|
|
return started
|
|
windows = started
|
|
return chunks
|
|
except OSError as failure:
|
|
return Failure(f"cannot lock {lock / LOCK}: {failure.strerror}")
|
|
|
|
|
|
def refusal(session: str, windows: list[tmux.Listed], root: str, run: tmux.Tmux) -> Failure | None:
|
|
"""Why no chunk of the checkout root may start in session, whose windows are windows: another checkout's chunks run in the session, or do-epic runs chunks in root."""
|
|
if windows and (owned := tmux.owner(session, run)) != root:
|
|
return Failure(f"the tmux session {session} runs the chunks of {owned or 'another checkout'}, whose key maps to the same session name; rename one of the two checkouts' folders")
|
|
if theirs := epic_chunks(root, run):
|
|
return Failure(f"do-epic runs chunks in {root}: {', '.join(f'{placed.window.name} of {placed.session}' for placed in theirs)}; loop-claude doesn't start a chunk there until those chunks finish")
|
|
return None
|
|
|
|
|
|
def chunks_to_start(windows: list[tmux.Listed], loop_run: LoopRun, leaving: bool, limit: int, paths: Paths, effects: Effects) -> list[str]:
|
|
"""The chunks to start beside windows, the session's: the stopped ones room_of restarts, then those of the plan that may_start picks. A plan that cannot be read starts NO_ITEM, as a plan without an item does."""
|
|
room = room_of(windows, loop_run.chunk, leaving, limit)
|
|
if not room.left:
|
|
return room.restart
|
|
plan = next_plan(loop_run.root, paths.home, effects)
|
|
if isinstance(plan, Failure):
|
|
# Such as in a checkout without a default branch, where chunk-preflight fails too: the worker is told why and finds the facts itself.
|
|
say(f"starting a chunk without a plan item, since the plan cannot be read: {plan.reason}", effects)
|
|
plan = NoItem(plan.reason)
|
|
held = held_items(room.running + room.stopped, project_key(loop_run.root, paths.home), paths.claude_code_transcripts)
|
|
return room.restart + [chunk for chunk in may_start(plan, room.running + room.restart, held, room.left) if chunk not in room.stopped]
|
|
|
|
|
|
def held_beside(chunk: str, root: str, paths: Paths, effects: Effects) -> list[int]:
|
|
"""The plan items that the project's other chunks hold now, running or stopped, which a chunk starting as `chunk` leaves to them. It waits for a loop that is starting windows, so chunks started together see each other. Where the lock cannot be taken, it reads the windows without it."""
|
|
key = project_key(root, paths.home)
|
|
wait_for_starts(paths.home, key)
|
|
others = [window.name for window in tmux.listed(tmux.session_name(tmux.LOOP, key), effects.tmux) if window.name != chunk]
|
|
return sorted(held_items(others, key, paths.claude_code_transcripts))
|
|
|
|
|
|
def wait_for_starts(home: pathlib.Path, key: str) -> None:
|
|
"""Returns once no loop of the project is starting windows: start_chunks holds LOCK while it starts them. Where the lock cannot be taken, it returns at once."""
|
|
with contextlib.suppress(OSError), starting(home / STATE / key):
|
|
pass
|