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

58 lines
2.7 KiB
Python

"""A human's task that holds up other issues of an epic (docs/decisions.md → `do-epic: drives one Jira epic …`, "Human tasks").
A window that starts again, as after a reboot, DMs again, and so does one whose DM failed, on its next read."""
import pathlib
from project_config.settings import by_name, ProjectConfig
from loop_effects.effects import Effects, say
from do_epic.backend.protocol import EpicIssue, EpicStore
from failure import Failure
from do_epic.repo_pick import recorded, Repo
from do_epic import scheduler
from agent_cycle.direct_message import tell
from agent_cycle import tmux
# What the window of a human's task awaits, in its name.
AWAITS = "a human's task"
def holds_up(key: str, issues: list[EpicIssue]) -> list[str]:
"""The keys of the open issues that the human's task called key holds up; none where it is no such task, is closed, or an issue blocks it."""
if not any(chunk.human and chunk.issue == key for chunk in scheduler.chunks(issues)[0]):
return []
return scheduler.held_up(issues)[key]
def message(task: EpicIssue, epic: str, held: list[str]) -> str:
return f"do-epic {epic} awaits you: {task.key}, \"{task.summary}\", blocks {' and '.join(held)}. do-epic goes on once you close {task.key} in Jira."
def checkout_of(held: list[EpicIssue], home: pathlib.Path) -> str | None:
"""The checkout of the first issue of held whose repo a project config names: where the DM finds its notifier, since the window runs in no checkout. None where there is none, and the DM goes by the current directory."""
for issue in held:
pick = recorded(issue)
found = by_name(pick.name, home) if isinstance(pick, Repo) else None
if isinstance(found, ProjectConfig):
return str(found.top)
return None
def awaited(task: EpicIssue, epic: str, store: EpicStore, window: tmux.Window, poll: int, home: pathlib.Path, effects: Effects) -> None:
"""Waits in window for the human to close the human's task `task` of the epic called epic, and returns once it no longer holds up any issue. A read of the epic that fails is said once and tried again."""
tmux.park(window, AWAITS, effects.tmux)
told, said = False, None
while True:
issues = store.read()
if isinstance(issues, Failure):
if issues.reason != said:
say(f"cannot read the epic, so cannot tell whether {task.key} is closed; trying again every {poll} seconds: {issues.reason}", effects, stdout=True)
said = issues.reason
elif not (held := holds_up(task.key, issues)):
return
elif not told:
text = message(task, epic, held)
say(text, effects, stdout=True)
by_key = {issue.key: issue for issue in issues}
told = tell(text, effects, checkout_of([by_key[key] for key in held], home))
effects.sleep(poll)