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

84 lines
3.9 KiB
Python

"""One run of do-epic on an epic (`EpicRun`), and how it starts the epic's chunks that may start now (`chunks_started`)."""
import dataclasses
import pathlib
import typing
from do_epic.config import DoEpicConfig
from loop_effects.effects import Effects, say
from do_epic.backend.protocol import EpicIssue, EpicStore
from do_epic.epic_record import EpicRecord
from failure import Failure
from do_epic import finish, planning, scheduler
from do_epic.repos import locate
from loop_state.paths import Paths
from forge.commands import Run, runner
from agent_cycle import tmux
# How often a parked chunk reads its pull request's gate, and a human task's window reads the epic: a person answers in hours, and each read is a few calls.
POLL_SECONDS = 60
@dataclasses.dataclass(frozen=True)
class EpicRun:
"""What stays the same through one run of do-epic on an epic."""
key: str
config: DoEpicConfig
store: EpicStore
record: EpicRecord
paths: Paths
env: typing.Mapping[str, str]
launcher: pathlib.Path
effects: Effects
# The runner of git's and the forge's commands in a folder.
forge_at: typing.Callable[[str], Run] = runner
@property
def session(self) -> str:
return tmux.session_name(tmux.EPIC, self.key)
def chunks_started(epic: EpicRun, issues: list[EpicIssue], own: str | None, told: str = "") -> scheduler.Started | Failure:
"""scheduler.start_chunks on the epic's issues, as read, in order of score, and its cap, read afresh; own is the issue of the chunk that asks, and told is the reason the planning just gave for stopping, which isn't said twice. Where no issue is open, the chunk to start is the epic's finishing, unless the epic is closed."""
due = finish.due(finish.landed(issues, epic.config.pull_merged_status))
closed = due and epic.store.closed()
if isinstance(closed, Failure):
return closed
order = planning.start_order(planned_epic(epic), issues)
if isinstance(order, Failure):
say(f"the issues of {epic.key} start in Jira's backlog order, since they cannot be put in order of score" + ("" if order.reason == told else f": {order.reason}"), epic.effects, stdout=True)
elif len(order) > 1:
say(f"by score, the issues of {epic.key} start in the order {', '.join(order)}", epic.effects, stdout=True)
scheduled = scheduled_epic(epic, () if isinstance(order, Failure) else tuple(order))
if isinstance(scheduled, Failure):
return scheduled
started = scheduler.start_chunks(scheduled, issues, due and not closed, own, epic.effects.tmux)
return started if isinstance(started, Failure) else dataclasses.replace(started, closed=closed)
def planned_epic(epic: EpicRun) -> planning.Planned:
"""The epic as planning reads and writes it."""
return planning.Planned(epic.key, epic.config, epic.store, epic.record, epic.paths.home, epic.effects)
def scheduled_epic(epic: EpicRun, order: tuple[str, ...]) -> scheduler.Epic | Failure:
"""The epic as scheduler limits its chunks: with its cap, read afresh, each repo's checkout, and the order its scored issues start in."""
cap = epic.record.parallel_chunks()
if isinstance(cap, Failure):
return cap
# The checkouts located so far, from whose remotes repos.locate tells where to clone a repo that is missing.
seen: list[pathlib.Path] = []
def located(name: str) -> tuple[pathlib.Path, int] | Failure:
found = locate(name, [checkout for checkout in seen], epic.config, epic.paths.home, epic.effects.git)
if isinstance(found, Failure):
return found
seen.append(found.checkout)
return real(found.checkout), found.config.parallel_chunks
return scheduler.Epic(epic.key, cap, located, epic.paths.home, epic.launcher, epic.env, epic.config.label_repos, order, epic.config.unblocking)
def real(checkout: pathlib.Path) -> pathlib.Path:
"""The checkout as loop-claude names it, which is as git does: through no symlink or `..`. Each keys the checkout's lock and its chunks by that path, so a project config that spells it another way hides neither from the other."""
return checkout.resolve()