56 lines
3.5 KiB
Python
56 lines
3.5 KiB
Python
"""What `do-epic <EPIC-KEY> --status` prints: a line per open issue of the epic (docs/decisions.md → `do-epic: drives one Jira epic …`)."""
|
|
import datetime
|
|
import pathlib
|
|
|
|
from do_epic.scheduler import chunks, HUMAN_WAIT, STATE
|
|
from do_epic.backend.protocol import EpicIssue
|
|
from failure import Failure
|
|
from do_epic.parked import file_of, Parked, parked_chunks, SLOT
|
|
from do_epic.repo_pick import LabelRepos
|
|
from agent_cycle import tmux
|
|
|
|
UNSCORED = ", after each issue that holds every score, since it doesn't"
|
|
|
|
|
|
def since(folder: pathlib.Path, chunk: Parked) -> str:
|
|
"""`, since <time>`: when the chunk's pull request began to await the gate its file records, to the minute in UTC, since the machine may be one the human reaches over ssh. The file is written once per gate. Empty where the file cannot be read."""
|
|
try:
|
|
return f", since {datetime.datetime.fromtimestamp(file_of(folder, chunk.issue).stat().st_mtime, datetime.timezone.utc).strftime('%Y-%m-%d %H:%M')} UTC"
|
|
except OSError:
|
|
return ""
|
|
|
|
|
|
def lines(key: str, home: pathlib.Path, issues: list[EpicIssue], team: LabelRepos, unblocking: tuple[str, ...], run: tmux.Tmux, order: tuple[str, ...]) -> list[str] | Failure:
|
|
"""A line per open issue of the epic called key, in the tracker's rank order: that its chunk runs or asks the human, or why it waits. order holds the keys of the issues that start by score, as planning.start_order puts them; a chunk it leaves out says that it starts after them. It doesn't start anything or check any limit, so the line of a chunk that has yet to start doesn't say which limit holds it back."""
|
|
found, waits = chunks(issues, team, unblocking)
|
|
folder = home / STATE / key
|
|
parked = parked_chunks(folder)
|
|
if isinstance(parked, Failure):
|
|
return parked
|
|
session = tmux.session_name(tmux.EPIC, key)
|
|
windows = tmux.listed(session, run)
|
|
live = {window.name: window for window in windows if not window.dead}
|
|
again = f"run `do-epic {key}` to start it again"
|
|
reasons = {wait.issue: wait.reason for wait in waits}
|
|
# Where no issue starts by score, each starts in the tracker's rank order, which needs no reason.
|
|
bare = {chunk.issue for chunk in found if chunk.issue not in order} if order else set()
|
|
for chunk in found:
|
|
if chunk.issue in parked:
|
|
shown = live[chunk.issue].awaits if chunk.issue in live else ""
|
|
# Past its gate, a chunk that awaits a slot has no time the file knows: the file's is when its gate began.
|
|
when = "" if shown == SLOT else since(folder, parked[chunk.issue])
|
|
stopped = "" if chunk.issue in live else f", and its window stopped; {again}"
|
|
reasons[chunk.issue] = parked[chunk.issue].waits(shown) + when + stopped
|
|
elif chunk.human and chunk.issue in live:
|
|
reasons[chunk.issue] = HUMAN_WAIT
|
|
elif chunk.issue not in live:
|
|
reasons[chunk.issue] = f"its window stopped; {again}" if chunk.issue in {window.name for window in windows} else f"it has not started; `do-epic {key}` starts it" + ("" if chunk.human else " once the epic and its repo have room") + ("" if chunk.human or chunk.issue not in bare else UNSCORED)
|
|
return [line(issue.key, reasons, live, session) for issue in issues if issue.key in reasons or issue.key in live]
|
|
|
|
|
|
def line(issue: str, reasons: dict[str, str], live: dict[str, tmux.Listed], session: str) -> str:
|
|
"""The status line of issue, whose chunk has a live window or a reason not to run."""
|
|
if issue in reasons:
|
|
return f"{issue} waits: {reasons[issue]}"
|
|
return f"{issue} asks you: answer its question in the tmux window {session}:{issue}" if live[issue].asks else f"{issue} runs"
|