Files
claude-config/bin/chunk_driver/claims.py
T

79 lines
4.1 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 build-release 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 `handed` picks.
"""
import contextlib
import fcntl
import pathlib
import typing
from failure import Failure
from loop_state.paths import Paths, XDG_CONFIG_HOME, XDG_STATE_HOME
from transcript_archive.project_key import project_key
from agent_cycle import tmux
# 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 build-release does.
LOCK = "starting"
# The flag that tells the entry point in a window its chunk, as `--chunk item-372` for build-release 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.
OWN = "BUILD_RELEASE_"
# A window gets the tmux server's environment, not the starting terminal's. Without the two XDG variables, a window would lock and write in other folders than the entry point that started it.
def handed(env: typing.Mapping[str, str]) -> dict[str, str]:
"""The variables of env that an entry point hands each window it starts: those both entry points read, and the two that move the config folder and the state folder."""
return {name: value for name, value in env.items() if name.startswith(OWN) or name in (XDG_CONFIG_HOME, XDG_STATE_HOME)}
@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 claims_folder(state: pathlib.Path) -> pathlib.Path:
"""The folder, in the state folder, that holds a folder per project key for its LOCK."""
return state / "claims"
def project_lock(root: str, paths: Paths) -> pathlib.Path:
"""The folder of the LOCK that everything starting a chunk in the checkout root holds."""
return claims_folder(paths.state_folder) / project_key(root, paths.home)
def loop_session(root: str, home: pathlib.Path) -> str:
"""The tmux session that holds the windows of build-release'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 build-release'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]