Files
claude-config/bin/do_epic/parked.py
T

76 lines
3.4 KiB
Python

"""The parked chunks of an epic: each keeps its worktree, pull request and session while that pull request waits on a long gate, in a file that survives a reboot (docs/decisions.md → `do-epic: drives one Jira epic …`, "Long gates").
A chunk is parked from the moment its file is written until the file is removed, whatever its window does: `scheduler` starts a window again for a parked chunk that has none, and that window goes on polling."""
import dataclasses
import json
import os
import pathlib
from failure import Failure
# Under the epic's folder, the folder that holds one file per parked chunk, named for its issue.
FOLDER = "parked"
# What a parked chunk awaits, in its window's name, once its worker has something to do but no running slot is free.
SLOT = "a slot"
@dataclasses.dataclass(frozen=True)
class Parked:
"""A parked chunk."""
# The key of the issue it works.
issue: str
worktree: str
# Its pull request's number.
pull: str
# The ID of its worker's claude session, which `claude --resume` takes.
session: str
# What its pull request waits on, as the words after "awaits" (`gates.CHECKS`, `gates.APPROVAL`, `gates.NEW_REVIEW`, `gates.MERGE`).
on: str
def waits(self, shown: str) -> str:
"""Why the chunk doesn't run, for the human. shown is what the name of its live window says it awaits, which is newer than the file's `on`; empty where no window lives."""
if shown == SLOT:
return f"it is parked: its worker has work again on pull request #{self.pull} and awaits {SLOT}"
return f"it is parked: its pull request #{self.pull} awaits {shown or self.on}"
def file_of(folder: pathlib.Path, issue: str) -> pathlib.Path:
return folder / FOLDER / f"{issue}.json"
def park(folder: pathlib.Path, chunk: Parked) -> Failure | None:
"""Records chunk as parked under the epic's folder, in place of what was recorded for its issue. The file is whole or absent, whenever the machine stops."""
file = file_of(folder, chunk.issue)
partial = file.with_suffix(".partial")
try:
file.parent.mkdir(parents=True, exist_ok=True)
partial.write_text(json.dumps(dataclasses.asdict(chunk), indent="\t") + "\n", encoding="utf-8")
os.replace(partial, file)
except OSError as failure:
return Failure(f"cannot record the parked chunk in {file}: {failure.strerror}")
return None
def unpark(folder: pathlib.Path, issue: str) -> Failure | None:
"""Records that the issue's chunk is parked no longer."""
try:
file_of(folder, issue).unlink(missing_ok=True)
except OSError as failure:
return Failure(f"cannot remove {file_of(folder, issue)}: {failure.strerror}")
return None
def parked_chunks(folder: pathlib.Path) -> dict[str, Parked] | Failure:
"""The epic's parked chunks, by issue. A Failure where a file cannot be read as one, since a chunk taken as not parked would start a second time."""
found: dict[str, Parked] = {}
for file in sorted((folder / FOLDER).glob("*.json")):
try:
fields = json.loads(file.read_text(encoding="utf-8"))
chunk = Parked(**fields)
except (OSError, ValueError, TypeError) as failure:
return Failure(f"cannot read the parked chunk in {file}: {failure}; fix or remove the file")
if chunk.issue != file.stem or not all(isinstance(value, str) for value in fields.values()):
return Failure(f"cannot read the parked chunk in {file}: its fields are not those of {file.stem}'s chunk; fix or remove the file")
found[chunk.issue] = chunk
return found