69 lines
3.6 KiB
Python
69 lines
3.6 KiB
Python
"""How entry points claim a checkout before they start a chunk in it, so that two never start the same chunk and do-epic never starts one where loop-claude runs.
|
|
|
|
An entry point holds `starting` on `project_lock` while it lists the windows of the checkout's chunks and starts new ones. Where the lock cannot be taken, `starting` yields a Failure, and the entry point doesn't start any chunk. It starts each chunk in a tmux window with CHUNK_FLAG, and hands that window the variables named with HANDED.
|
|
"""
|
|
import contextlib
|
|
import fcntl
|
|
import pathlib
|
|
import typing
|
|
|
|
from failure import Failure
|
|
from transcript_archive.project_key import project_key
|
|
from agent_cycle import tmux
|
|
|
|
# Under the home folder, a folder per project key holds LOCK.
|
|
CLAIMS = ".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 entry point in a window its chunk, as `--chunk item-372` for loop-claude and `--chunk ABC-34 ABC-12` for do-epic. Nobody types it, so no panel picked it.
|
|
CHUNK_FLAG = "--chunk"
|
|
# The prefix of the variables both entry points read. 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_"
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def starting(folder: pathlib.Path) -> typing.Iterator[Failure | None]:
|
|
"""Holds the LOCK in folder: project_lock's for a project, or do-epic's for an epic. It yields a Failure, and doesn't hold any lock, where the file cannot be opened or locked."""
|
|
with locked(folder / LOCK) as failure:
|
|
yield failure
|
|
|
|
|
|
@contextlib.contextmanager
|
|
def locked(file: pathlib.Path) -> typing.Iterator[Failure | None]:
|
|
"""Holds a flock on file, which it makes with its folder. It yields a Failure, and doesn't hold any lock, where the file cannot be opened or locked."""
|
|
try:
|
|
file.parent.mkdir(parents=True, exist_ok=True)
|
|
lock = open(file, "a")
|
|
except OSError as failure:
|
|
yield Failure(f"cannot lock {failure.filename or file}: {failure.strerror}")
|
|
return
|
|
with lock:
|
|
try:
|
|
fcntl.flock(lock, fcntl.LOCK_EX)
|
|
except OSError as failure:
|
|
yield Failure(f"cannot lock {file}: {failure.strerror}")
|
|
return
|
|
yield None
|
|
|
|
|
|
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 / CLAIMS / project_key(root, home)
|
|
|
|
|
|
def loop_session(root: str, home: pathlib.Path) -> str:
|
|
"""The tmux session that holds the windows of loop-claude's chunks of the checkout root."""
|
|
return tmux.session_name(tmux.LOOP, project_key(root, home))
|
|
|
|
|
|
def loop_chunks(root: str, home: pathlib.Path, run: tmux.Tmux) -> list[tmux.Listed]:
|
|
"""The windows of loop-claude's chunks that run in the checkout root. A session that runs another checkout's chunks under the same name doesn't hold any."""
|
|
session = loop_session(root, home)
|
|
running = [window for window in tmux.listed(session, run) if not window.dead]
|
|
return running if running and tmux.owner(session, run) == root else []
|
|
|
|
|
|
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]
|