|
|
|
@@ -24,7 +24,7 @@ from agent_cycle import tmux
|
|
|
|
|
|
|
|
|
|
# Under the home folder, a folder per project key holds LOCK.
|
|
|
|
|
STATE = ".local/state/loop-claude"
|
|
|
|
|
# The file a loop keeps locked with flock while it lists the project's windows and starts new ones, so two loops whose chunks finish together don't start a chunk twice.
|
|
|
|
|
# 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"
|
|
|
|
@@ -52,15 +52,24 @@ def chunk_session(key: str, chunk: str, claude_code_transcripts: pathlib.Path) -
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
|
def starting(home: pathlib.Path, key: str) -> typing.Iterator[None]:
|
|
|
|
|
"""Holds the project's LOCK. Raises OSError where the file cannot be opened or locked."""
|
|
|
|
|
folder = home / STATE / key
|
|
|
|
|
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."""
|
|
|
|
|
return [placed for placed in tmux.everywhere(run) if placed.session.startswith(f"{tmux.EPIC}-") and placed.root == root and not placed.window.dead]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
@@ -128,40 +137,50 @@ def next_plan(root: str, home: pathlib.Path, effects: Effects) -> NextChunk | No
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 plan that cannot be read starts NO_ITEM, as a plan without an item does. A Failure says why a chunk could not start; those before it run."""
|
|
|
|
|
"""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
|
|
|
|
|
key = project_key(loop_run.root, paths.home)
|
|
|
|
|
session = tmux.session_name(tmux.LOOP, key)
|
|
|
|
|
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(paths.home, key):
|
|
|
|
|
with starting(lock):
|
|
|
|
|
windows = tmux.listed(session, effects.tmux)
|
|
|
|
|
if windows and (owned := tmux.owner(session, effects.tmux)) != loop_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")
|
|
|
|
|
room = room_of(windows, loop_run.chunk, leaving, config.parallel_chunks)
|
|
|
|
|
chunks = list(room.restart)
|
|
|
|
|
if room.left:
|
|
|
|
|
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, key, paths.claude_code_transcripts)
|
|
|
|
|
chunks += [chunk for chunk in may_start(plan, room.running + room.restart, held, room.left) if chunk not in room.stopped]
|
|
|
|
|
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:
|
|
|
|
|
stale = [window for window in windows if window.name == chunk and window.dead]
|
|
|
|
|
for window in stale:
|
|
|
|
|
tmux.close(window, effects.tmux)
|
|
|
|
|
windows = [window for window in windows if window not in stale]
|
|
|
|
|
command = [str(launcher), CHUNK_FLAG, chunk, loop_run.task]
|
|
|
|
|
if failed := tmux.start_window(tmux.Window(session, chunk, ""), loop_run.root, handed, command, not windows, effects.tmux):
|
|
|
|
|
return failed
|
|
|
|
|
windows.append(tmux.Listed(chunk, "", False))
|
|
|
|
|
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 {paths.home / STATE / key / LOCK}: {failure.strerror}")
|
|
|
|
|
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 starts none there until they 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]:
|
|
|
|
@@ -174,5 +193,5 @@ def held_beside(chunk: str, root: str, paths: Paths, effects: Effects) -> list[i
|
|
|
|
|
|
|
|
|
|
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, key):
|
|
|
|
|
with contextlib.suppress(OSError), starting(home / STATE / key):
|
|
|
|
|
pass
|
|
|
|
|