141 lines
8.6 KiB
Python
141 lines
8.6 KiB
Python
"""A chunk parked while its pull request waits on a gate (docs/decisions.md → `do-epic: drives one Jira epic …`): `parked_through` keeps it parked from the end of a cycle until its worker has something to do, and `news_after` says what a chunk that isn't parked tells its worker."""
|
|
import dataclasses
|
|
import pathlib
|
|
|
|
from do_epic import chunk_prompt, gates, parked, scheduler
|
|
from do_epic.epic_run import chunks_started, EpicRun, POLL_SECONDS, scheduled_epic
|
|
from project_config.main import located as config_located
|
|
from failure import Failure
|
|
from chunk_driver.this_loop import Loop
|
|
from loop_effects.effects import say
|
|
from agent_cycle.stop import Stop
|
|
from agent_cycle import tmux
|
|
|
|
# How the news for a chunk that was parked opens.
|
|
DRIVER_PARKED = f"{chunk_prompt.DRIVER} parked the chunk while"
|
|
|
|
|
|
def parked_through(repo: str, loop: Loop, epic: EpicRun, told: str | None, finished: bool, based: str | None) -> str | Stop:
|
|
"""Keeps the chunk in loop's window parked. Its work is in the repo called repo. It stays parked for as long as its pull request waits on a gate, and returns once its worker has something to do: what to tell that worker, or empty where the chunk was never parked. told is the news its last cycle's worker got, None where the window just started, and finished says the judge ruled the chunk finished. based is the epic's working branch that `epic_based` recorded as the chunk's base when the window started, None where it recorded none. A window that just started parks the chunk only if its file says so: the forge isn't read for a chunk that starts afresh.
|
|
|
|
Where a person merges, a worker ends its run with the pull request open, so the judge may rule the chunk finished before it is. For such a finished chunk, a gate that cannot be read is read again until it can (`Poll.first_gate`), and what the worker must still do is news (`news_after`).
|
|
|
|
A chunk parks in three steps: its file is written, its window is shown as parked, and the epic's next chunks start in the slot it frees. It resumes in the reverse order: `scheduler.resume` gives it a running slot, then its file is removed. So a window that ends in between finds the chunk parked when it starts again.
|
|
"""
|
|
key, folder = loop.window.name, epic.paths.home / scheduler.STATE / epic.key
|
|
recorded_as = parked.parked_chunks(folder)
|
|
if isinstance(recorded_as, Failure):
|
|
return Stop(1, recorded_as.reason)
|
|
config = config_located(loop.root, epic.paths.home)
|
|
if isinstance(config, Failure):
|
|
return Stop(1, config.reason)
|
|
# A pull request into an epic's working branch merges without a person, whatever `merge` says. The base recorded says so, not the config: the gate fails where the forge merges the pull request elsewhere.
|
|
poll = Poll(key, repo, config.merge == "human" and based is None, folder, loop, epic)
|
|
if key in recorded_as:
|
|
return resumed_news(poll, recorded_as[key].on, shown=None)
|
|
entered = None if told is None else poll.first_gate(again=finished)
|
|
if not isinstance(entered, gates.Gate):
|
|
if isinstance(entered, Failure):
|
|
say(f"cannot tell whether the chunk's pull request waits on a gate, so the chunk isn't parked: {entered.reason}", epic.effects, stdout=True)
|
|
return news_after(entered, finished, poll.by_human, told)
|
|
if failed := poll.record(entered):
|
|
return Stop(1, failed.reason)
|
|
if based is not None and (status := epic.config.pull_open_status):
|
|
# The team's automation moves an issue only from a pull request into the default branch.
|
|
if isinstance(moved := epic.store.move_issue(key, status), Failure):
|
|
say(f"{key} did not move to {status!r} though its pull request is open: {moved.reason}", epic.effects, stdout=True)
|
|
tmux.park(loop.window, entered.on, epic.effects.tmux)
|
|
# Once the window shows as parked, so the chunks that start count it as running nowhere.
|
|
issues = epic.store.read()
|
|
started = issues if isinstance(issues, Failure) else chunks_started(epic, issues, key)
|
|
say(f"could not start the next chunk: {started.reason}" if isinstance(started, Failure) else f"started {', '.join(started.started) or 'no other'}", epic.effects, stdout=True)
|
|
epic.effects.sleep(POLL_SECONDS)
|
|
return resumed_news(poll, entered.on, shown=entered.on)
|
|
|
|
|
|
def news_after(entered: gates.Open | Failure | None, finished: bool, by_human: bool, told: str | None) -> str:
|
|
"""What to tell the worker of a chunk that isn't parked as its cycle ends, empty where there isn't anything. entered is what was read instead of a gate, None where the window just started, and told is the news the last worker got.
|
|
|
|
Only a chunk the judge ruled finished, whose pull request a person merges, has news, of one of two kinds:
|
|
- its pull request doesn't merge into its base: news after every cycle that leaves it so, and the worker levels the branch;
|
|
- its worktree stands though no pull request is open: news once, unless told says so already, and the worker removes the worktree."""
|
|
if not (finished and by_human):
|
|
return ""
|
|
if isinstance(entered, gates.Unmergeable):
|
|
return f"{chunk_prompt.DRIVER} keeps the chunk open while its pull request doesn't merge into its base: {entered.news}."
|
|
if isinstance(entered, gates.Unopened):
|
|
uncleaned = f"{chunk_prompt.DRIVER} keeps the chunk open while its worktree stands: {entered.news}."
|
|
return "" if uncleaned in (told or "") else uncleaned
|
|
return ""
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Poll:
|
|
"""What each read of a parked chunk's gate needs: the chunk's key, the repo its work is in, whether a person merges its pull request, the folder of the epic's parked chunks, and the chunk's loop and epic."""
|
|
key: str
|
|
repo: str
|
|
by_human: bool
|
|
folder: pathlib.Path
|
|
loop: Loop
|
|
epic: EpicRun
|
|
|
|
def gate(self) -> gates.Gate | gates.Open | Failure:
|
|
return gates.gate(self.key, self.loop.root, self.epic.forge_at, self.by_human)
|
|
|
|
def first_gate(self, again: bool) -> gates.Gate | gates.Open | Failure:
|
|
"""The gate as a cycle ends. Where again holds and a person merges, one that cannot be read is read again every POLL_SECONDS until it can."""
|
|
now = self.gate()
|
|
if not again:
|
|
return now
|
|
if self.by_human and isinstance(now, Failure):
|
|
say(f"cannot read the gate of a pull request a person merges: {now.reason}; trying again every {POLL_SECONDS} seconds", self.epic.effects, stdout=True)
|
|
while self.by_human and isinstance(now, Failure):
|
|
self.epic.effects.sleep(POLL_SECONDS)
|
|
now = self.gate()
|
|
return now
|
|
|
|
def record(self, gate: gates.Gate) -> Failure | None:
|
|
"""Writes the chunk's file as parked on gate, and says so."""
|
|
failed = parked.park(self.folder, parked.Parked(self.key, gate.worktree, gate.pull, str(self.loop.session.id), gate.on))
|
|
if failed is None:
|
|
say(f"parked: pull request #{gate.pull} awaits {gate.on}", self.epic.effects, stdout=True)
|
|
return failed
|
|
|
|
|
|
def resumed_news(poll: Poll, on: str, shown: str | None) -> str | Stop:
|
|
"""Reads the gate of the parked chunk every POLL_SECONDS until its worker has something to do and a running slot is free, then removes its file: what to tell that worker. on is the gate the chunk's file says it is parked on, and shown what its window's name says it awaits, None where nothing yet. A gate that cannot be read leaves the chunk parked as it was."""
|
|
# The last reason said for staying parked.
|
|
said = None
|
|
while True:
|
|
now = poll.gate()
|
|
if isinstance(now, gates.Gate):
|
|
awaits, stays = now.on, None
|
|
if now.on != on:
|
|
if failed := poll.record(now):
|
|
return Stop(1, failed.reason)
|
|
on = now.on
|
|
elif isinstance(now, Failure):
|
|
awaits, stays = shown or on, f"cannot read the gate: {now.reason}"
|
|
else:
|
|
full = resumed(poll.repo, poll.loop, poll.epic)
|
|
if full is None:
|
|
failed = parked.unpark(poll.folder, poll.key)
|
|
return Stop(1, failed.reason) if failed is not None else f"{DRIVER_PARKED} its pull request awaited {"a person's merge" if on == gates.MERGE else on}. It resumes now: {now.news}."
|
|
awaits, stays = parked.SLOT, f"{now.news}, but the chunk stays parked: {full}"
|
|
if awaits != shown:
|
|
tmux.park(poll.loop.window, awaits, poll.epic.effects.tmux)
|
|
shown = awaits
|
|
if stays is not None and stays != said:
|
|
say(f"{stays}; trying again every {POLL_SECONDS} seconds", poll.epic.effects, stdout=True)
|
|
said = stays
|
|
poll.epic.effects.sleep(POLL_SECONDS)
|
|
|
|
|
|
def resumed(repo: str, loop: Loop, epic: EpicRun) -> str | None:
|
|
"""scheduler.resume for the parked chunk in loop's window, whose work is in the repo called repo, on the epic's cap and repos read afresh: None once it holds a running slot, else why it stays parked."""
|
|
# A chunk that resumes starts no other, so it needs no start order.
|
|
scheduled = scheduled_epic(epic, ())
|
|
if isinstance(scheduled, Failure):
|
|
return scheduled.reason
|
|
return scheduler.resume(scheduled, repo, loop.window, epic.effects.tmux)
|