563 lines
36 KiB
Python
563 lines
36 KiB
Python
"""The do-epic command (docs/decisions.md → `do-epic: drives one Jira epic …`).
|
|
|
|
`do-epic <EPIC-KEY>` first plans the epic's open issues (`planning`): each that no label places is split by repo or gets its pick, and the scored issues do-epic filed are re-ranked. Then it moves each parent whose sub-tasks a person filed, where those sub-tasks have landed or are done (`parent_moves`). Then it starts each of the epic's chunks that may start now, each in a window of the epic's tmux session, as `scheduler` picks them, and puts that session on the terminal. Each window runs `do-epic --chunk <ISSUE-KEY> <EPIC-KEY>`: the cycles of one issue's chunk, run by the code that runs loop-claude's (`agent_cycle`), until the judge rules it finished. Then it starts the chunks that may start now, and its window closes. The window of a human's task runs no cycle: it waits until the task no longer holds up an issue (`human_tasks`), then does the same.
|
|
|
|
After each cycle of a chunk in a repo, the window reads what the chunk's pull request waits on (`gates`). While that is a gate only time or a person passes, the chunk is parked (`parked`): the window frees its running slot and polls the forge, and the chunk's next cycle starts once the worker has something to do and a slot is free. `do-epic <EPIC-KEY> --status` prints what each open issue does."""
|
|
import dataclasses
|
|
import enum
|
|
import pathlib
|
|
import sys
|
|
import typing
|
|
|
|
from chunk_driver.cycle_start import announced_route, cycle_of, project_routing, routing_on
|
|
from chunk_driver.claims import CHUNK_FLAG
|
|
from do_epic import chunk_prompt, dry_run, findings, finish, human_tasks, parent_moves, planning, scheduler, split, status
|
|
from chunk_driver.chunk_run import ChunkRun, closed_cycle
|
|
from do_epic.epic_run import chunks_started, EpicRun, planned_epic, POLL_SECONDS, real
|
|
from do_epic.repo_pick import ClaudeWork, HumanTask, recorded, Repo
|
|
from project_config.main import located as config_located
|
|
from chunk_driver.project.records import CONSEQUENCE, read_records
|
|
from agent_cycle.run import Cycle, run_cycle
|
|
from do_epic.config import DoEpicConfig, load, PULL_MERGED_STATUS
|
|
from loop_effects.effects import Effects, say
|
|
from project_config.settings import Epic
|
|
from git_cli import epic_branch
|
|
from record_store.jira_description import EpicDescription
|
|
from do_epic.backend.protocol import EpicIssue, EpicStore
|
|
from do_epic.epic_record import EpicRecord
|
|
from failure import Failure
|
|
from agent_cycle.verdicts import Finished, TakenIn
|
|
from atlassian.jira import ISSUE_KEY
|
|
from do_epic.backend.jira import JiraEpic
|
|
from claude_code.reply import Limited
|
|
from text_files.markdown_text import listed
|
|
from do_epic.repos import locate
|
|
from chunk_driver.this_loop import Loop
|
|
from chunk_driver.project.model_choice import Models, overridden
|
|
from do_epic.parking import parked_through
|
|
from loop_state.paths import Paths
|
|
from chunk_driver.project.premises import read_premises
|
|
from git_cli.git_repo import record_chunk_base
|
|
from forge.commands import Run, runner
|
|
from claude_code.session import session_named
|
|
from model_routing.ladder_file import SHIPPED
|
|
from agent_cycle.stop import Stop
|
|
from agent_cycle.direct_message import tell
|
|
from project_config import templates
|
|
from agent_cycle import tmux
|
|
|
|
HELP = f"""usage: do-epic <EPIC-KEY> [--status | --dry-run]
|
|
{findings.USAGE}
|
|
{finish.USAGE}
|
|
|
|
Runs the chunks of one Jira epic, such as ABC-12, each in its own window of the tmux session epic-<EPIC-KEY>.
|
|
Before it starts chunks, do-epic <EPIC-KEY> plans each open issue that has none of these labels:
|
|
`repo:<name>` for its repo, `do-epic-claude` for work of Claude's that changes no repo, `do-epic-human` for a task of yours.
|
|
A label that `[label-repos]` in config.toml maps to a repo counts as that repo's.
|
|
Where such an issue changes several repos, it files a sub-task per repo. Otherwise it gives the issue its label, as repo-rule.md says.
|
|
An issue under which a person filed a sub-task is not planned: its sub-tasks run, each needs a label of its own, and do-epic moves the issue once they have landed or are done, as config.toml names how.
|
|
Where the rule leaves the label unclear, it asks you on the terminal. Answer with the label, or with Enter alone to leave the issue waiting.
|
|
Where two or more issues may start, it then scores each of them that holds no scores, and tells the issue's team the scores in a comment on the issue. It scores by the `Score = ...` line under the epic's ## Scoring, and moves the issues do-epic itself filed into that order on the board.
|
|
An open issue labelled `repo:<name>` or `do-epic-claude` runs as a chunk once no issue blocks it.
|
|
The issues start in order of score, highest first, whatever their order on the board. An issue it could not score starts after the others. As its chunk starts, an issue is assigned in Jira to the account do-epic works as, unless someone else holds it. Where config.toml names a start transition or a sprint board, the issue is also moved by that transition and joins that board's active sprint.
|
|
A `do-epic-human` issue that blocks another gets a window too. The window DMs you once, then waits without taking a place until you close the issue in Jira.
|
|
As many run at once as the epic's description allows with a line such as `**Max parallel chunks:** 2`, one where it has none.
|
|
A repo takes as many chunks as its `parallel-chunks`, and none while loop-claude runs chunks in it.
|
|
A chunk whose worker ends its run while its pull request waits on its checks, an approval or a new review
|
|
is parked: it frees its place for the next chunk, and its window's name says what it awaits. Its worker goes
|
|
on once the checks pass, the approval comes or a reviewer comments, and a place is free; until one is, the
|
|
name says `awaits a slot`. In a repo whose config sets `merge = "human"`, a chunk never merges: once its pull
|
|
request is ready, with nothing else to wait on, the window's name says `awaits your merge`, and its worker goes on once you merged it. A parked chunk survives a reboot: run do-epic <EPIC-KEY> again to go on polling.
|
|
--status doesn't start any chunk. It prints a line per open issue: that its chunk runs or asks you a question, or why it waits, such as the gate a parked chunk awaits and since when.
|
|
--dry-run runs the planning and the scheduling against the epic as Jira holds it. It doesn't write anything to Jira, start a window or ask a question, and it doesn't start any issue in Jira as a real run's chunk does. It prints each label, sub-task, comment, score and rank move that planning would write, each move of a parent issue whose sub-tasks a person filed, the order the issues would start in, each chunk that would start and why each other issue waits. A sub-task not yet filed shows a key such as ABC-new1. A question planning would ask is printed instead of asked. Planning's claude calls still run.
|
|
A repo that is missing isn't cloned. Planning then stops there, so its writes are only those made before it, and the issues of that repo wait, and a chunk of another repo may start in the place a real run gives them; a real run would clone it and go on.
|
|
{findings.HELP}
|
|
{finish.HELP}
|
|
The Jira site and its credentials are set in ~/.claude-projects/do-epic/config.toml."""
|
|
# Under the home folder, where a chunk's cycles are archived, a folder per epic. It is not loop-claude's archive, since that archive's readers, such as judge-replay and the routing figures, read loop-claude's prompts.
|
|
ARCHIVE = ".claude/do-epic-transcripts"
|
|
# The flag that prints what each open issue does.
|
|
STATUS_FLAG = "--status"
|
|
# The flag that prints what planning and the scheduler would do, without doing it.
|
|
DRY_RUN_FLAG = "--dry-run"
|
|
# A call to origin that levels an epic's working branch is stopped after this long, chunk-preflight's limit for the same calls: one that hangs would hold the chunk's running slot before any cycle, with nothing said.
|
|
LEVEL_SECONDS = 60
|
|
|
|
# The store of an epic's issues and its record, for the epic with this key.
|
|
Backend = typing.Callable[[DoEpicConfig, str], tuple[EpicStore, EpicRecord]]
|
|
|
|
|
|
def jira_backend(config: DoEpicConfig, key: str) -> tuple[EpicStore, EpicRecord]:
|
|
epic = Epic(config.jira_url, key)
|
|
return JiraEpic(epic, config.env_file, finish=config.finish, finding_type=config.finding_type, review_type=config.review_type, start=config.start), EpicRecord(EpicDescription(epic, config.env_file))
|
|
|
|
|
|
def main(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, effects: Effects, backend: Backend = jira_backend, stdin: typing.TextIO = sys.stdin, forge_at: typing.Callable[[str], Run] = runner) -> int:
|
|
try:
|
|
stop = run(argv, env, paths, launcher, effects, backend, stdin, forge_at)
|
|
except KeyboardInterrupt:
|
|
stop = Stop(130, "")
|
|
except Exception as crash:
|
|
tell(f"do-epic {' '.join(argv)} crashed: {crash!r}", effects)
|
|
raise
|
|
if stop.reason:
|
|
say(stop.reason, effects, stdout=not stop.code)
|
|
# A chunk's stop is DMed, as loop-claude's is, so a chunk left overnight never ends unseen.
|
|
if stop.reason and argv[:1] == [CHUNK_FLAG]:
|
|
tell(f"do-epic {' '.join(argv)} stopped: {stop.reason}", effects)
|
|
return stop.code
|
|
|
|
|
|
def run(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher: pathlib.Path, effects: Effects, backend: Backend, stdin: typing.TextIO, forge_at: typing.Callable[[str], Run]) -> Stop:
|
|
chunk, words = (argv[1], argv[2:]) if len(argv) >= 2 and argv[0] == CHUNK_FLAG else (None, argv)
|
|
status_asked = chunk is None and words[1:] == [STATUS_FLAG]
|
|
dry_run_asked = chunk is None and words[1:] == [DRY_RUN_FLAG]
|
|
words = words[:1] if status_asked or dry_run_asked else words
|
|
# What the words after the epic's key write into the epic, where they are a filing or a decision.
|
|
writes = findings.parsed(words[1:]) if chunk is None else None
|
|
closes = chunk is None and words[1:] == [finish.CLOSE]
|
|
if (len(words) != 1 and not writes and not closes) or not ISSUE_KEY.fullmatch(words[0]) or (chunk is not None and not ISSUE_KEY.fullmatch(chunk)):
|
|
return Stop(0 if words in (["-h"], ["--help"]) else 1, HELP)
|
|
if writes or closes:
|
|
return epic_written(words[0], writes, paths, backend, stdin)
|
|
# Before anything starts: a clone, a claude session or a tmux window.
|
|
if not dry_run_asked and (reason := tmux.unusable(effects.tmux, chunk_prompt.DRIVER)):
|
|
return Stop(1, reason)
|
|
config = load(paths.home)
|
|
if isinstance(config, Failure):
|
|
return Stop(1, config.reason)
|
|
store, record = backend(config, words[0])
|
|
epic = EpicRun(words[0], config, store, record, paths, env, launcher, effects, forge_at)
|
|
if dry_run_asked:
|
|
return dry_run_printed(epic)
|
|
if status_asked:
|
|
return status_printed(epic)
|
|
return windows_opened(epic) if chunk is None else chunk_run(chunk, epic)
|
|
|
|
|
|
def status_printed(epic: EpicRun) -> Stop:
|
|
"""What `do-epic <EPIC-KEY> --status` does: prints a line per open issue, and doesn't start anything."""
|
|
issues = epic.store.read()
|
|
# Reading a repo's goals may clone it, which a status doesn't do.
|
|
reading = dataclasses.replace(epic, effects=dataclasses.replace(epic.effects, git=dry_run.without_clones(epic.effects.git)))
|
|
order = issues if isinstance(issues, Failure) else planning.start_order(planned_epic(reading), issues)
|
|
# Where the issues cannot be put in order of score, they start in the backlog's order, as a run says.
|
|
lines = issues if isinstance(issues, Failure) else status.lines(epic.key, epic.paths.home, issues, epic.config.label_repos, epic.config.unblocking, epic.effects.tmux, () if isinstance(order, Failure) else tuple(order))
|
|
return Stop(1, lines.reason) if isinstance(lines, Failure) else Stop(0, "\n".join(lines) or f"the epic {epic.key} has no open issue")
|
|
|
|
|
|
def epic_written(key: str, writes: findings.Write | None, paths: Paths, backend: Backend, stdin: typing.TextIO) -> Stop:
|
|
"""Files a finding in the epic or records a decision in its description, as writes says; closes the epic where writes is None. It needs no tmux, so a chunk's worker may run it anywhere."""
|
|
config = load(paths.home)
|
|
if isinstance(config, Failure):
|
|
return Stop(1, config.reason)
|
|
store, record = backend(config, key)
|
|
done = findings.written(key, writes, store, config.label_repos, record, stdin, paths.home / scheduler.STATE / key) if writes else finish.closed(key, store, config.label_repos, stdin, config.pull_merged_status)
|
|
return Stop(1, done.reason) if isinstance(done, Failure) else Stop(0, done)
|
|
|
|
|
|
def windows_opened(epic: EpicRun) -> Stop:
|
|
"""What the do-epic the human runs does: plans the open issues (`planning`), starts each chunk that may start now, says why each other open issue waits, then puts the epic's session on the terminal until the human detaches."""
|
|
started = planned_and_started(epic)
|
|
if isinstance(started, Stop):
|
|
return started
|
|
chunks_said(epic, started, "started", f" in the tmux session {epic.session}")
|
|
if not tmux.listed(epic.session, epic.effects.tmux):
|
|
return Stop(0, "")
|
|
if not epic.effects.on_terminal():
|
|
say(f"not on a terminal; see the chunks with `tmux attach -t ={epic.session}`", epic.effects, stdout=True)
|
|
return Stop(0, "")
|
|
shown = tmux.show(epic.session, "TMUX" in epic.env)
|
|
return Stop(1, f"`tmux {' '.join(shown)}` failed; the chunks go on in the tmux session") if epic.effects.tmux_on_terminal(shown) else Stop(0, "")
|
|
|
|
|
|
def planned_and_started(epic: EpicRun, dry: bool = False) -> scheduler.Started | Stop:
|
|
"""Plans the epic's open issues, then starts each chunk that may start now. Where dry, a planning that stops early says so on stdout, and that the writes shown are those made before it stopped."""
|
|
read = epic.store.read()
|
|
if isinstance(read, Failure):
|
|
return Stop(1, read.reason)
|
|
planned, stopped = planning.planned(planned_epic(epic), read)
|
|
# After the planning, which reads the epic again and would drop a move a search doesn't show yet.
|
|
issues = parents_moved(epic, planned)
|
|
if isinstance(stopped, Limited):
|
|
return Stop(1, f"the epic is not planned, so no chunk started: {stopped.said}")
|
|
if stopped is not None:
|
|
# One issue or repo that cannot be planned holds back only itself: each chunk that may start still does.
|
|
say(f"the epic {epic.key} is not planned in full" + (", so the writes below are those it made before it stopped" if dry else "") + f": {stopped.reason}", epic.effects, stdout=dry)
|
|
started = chunks_started(epic, issues, None, stopped.reason if stopped else "")
|
|
return Stop(1, started.reason) if isinstance(started, Failure) else started
|
|
|
|
|
|
def chunks_said(epic: EpicRun, started: scheduler.Started, verb: str, where: str) -> None:
|
|
"""Prints `<verb> <the chunks>` and where, or `<verb> no chunk`, then why each other open issue waits, and that the epic is closed where it is."""
|
|
say(f"{verb} {', '.join(started.started)}{where}" if started.started else f"{verb} no chunk", epic.effects, stdout=True)
|
|
for wait in started.waits:
|
|
say(f"{wait.issue} waits: {wait.reason}", epic.effects, stdout=True)
|
|
if started.closed:
|
|
say(f"the epic {epic.key} is closed", epic.effects, stdout=True)
|
|
|
|
|
|
def dry_run_printed(epic: EpicRun) -> Stop:
|
|
"""What `do-epic <EPIC-KEY> --dry-run` does: plans and starts as `windows_opened` does, on a copy of the epic, a tmux that starts no window and a git that clones no repo, then prints each Jira write planning made on the copy, the scores among them, the order the scored issues would start in, each chunk that would start and why each other issue waits. It doesn't put the session on the terminal or ask a question."""
|
|
dry = dry_run.seeded(epic.store)
|
|
if isinstance(dry, Failure):
|
|
return Stop(1, dry.reason)
|
|
effects = dataclasses.replace(epic.effects, tmux=dry_run.recording(epic.effects.tmux), git=dry_run.without_clones(epic.effects.git), on_terminal=lambda: False)
|
|
rehearsal = dataclasses.replace(epic, store=dry, effects=effects)
|
|
say("dry run: nothing is written to Jira and no window starts, so a line below that says an issue is labelled, split or scored shows what a real run would do. A real run would also start in Jira the issue of each chunk it starts: assign it and, where config.toml says so, move it and add it to a sprint", effects, stdout=True)
|
|
started = planned_and_started(rehearsal, dry=True)
|
|
writes = "".join("\n- " + write.replace("\n", "\n ") for write in dry.writes)
|
|
say(f"planning would write to Jira:{writes}" if dry.writes else "planning wouldn't write anything to Jira", effects, stdout=True)
|
|
if isinstance(started, Stop):
|
|
return started
|
|
chunks_said(rehearsal, started, "would start", "")
|
|
return Stop(0, "")
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class RoutingIssue:
|
|
"""An issue as what routes its chunk (agent_cycle.routing.Scored)."""
|
|
issue: EpicIssue
|
|
|
|
@property
|
|
def id(self) -> int:
|
|
"""The number in the issue's key. A Route keeps a number, and one chunk's archive holds one issue, so the number tells the issue without its project's letters."""
|
|
return int(self.issue.key.rpartition("-")[2])
|
|
|
|
@property
|
|
def likelihood(self) -> int:
|
|
return self.issue.scores["L"]
|
|
|
|
@property
|
|
def escape_and_impact(self) -> int:
|
|
return self.issue.scores.get("E", 0) + self.issue.scores.get("I", 0)
|
|
|
|
|
|
def chunk_run(key: str, epic: EpicRun) -> Stop:
|
|
"""Runs the cycles of the chunk of the issue called key until one stops the loop, or the judge rules the chunk finished. For a human's task it runs no cycle: it waits until the task no longer holds up an issue (`human_tasks.awaited`), then starts the next chunks. A chunk in a repo parks after each cycle that leaves its pull request waiting on a gate, and before its first where it was parked when its window last ended, as at a reboot."""
|
|
pane = epic.env.get("TMUX_PANE")
|
|
if not pane:
|
|
return Stop(1, f"{CHUNK_FLAG} is how do-epic starts itself in a tmux window; run do-epic <EPIC-KEY>")
|
|
# None for the epic's finishing, which is no issue.
|
|
issue = None if key == epic.key else issue_read(key, epic)
|
|
if isinstance(issue, Stop):
|
|
return issue
|
|
if issue and isinstance(recorded(issue, epic.config.label_repos), HumanTask):
|
|
human_tasks.awaited(issue, epic.key, epic.store, epic.config.label_repos, epic.config.unblocking, tmux.Window(epic.session, key, pane), POLL_SECONDS, epic.paths.home, epic.effects)
|
|
return handed_on(key, epic, Landed.WITH_A_HUMAN)
|
|
place = chunk_root(key, issue, epic)
|
|
if isinstance(place, Stop):
|
|
return place
|
|
root, repo = place
|
|
if unmade := folder_made(root):
|
|
return Stop(1, unmade.reason)
|
|
# The epic's working branch the chunk merges into; None where its repo keeps none, as for a chunk without a repo.
|
|
based = epic_based(key, root, epic) if repo is not None else None
|
|
if isinstance(based, Stop):
|
|
return based
|
|
if issue:
|
|
issue_started(key, epic)
|
|
session = session_named(f"A {epic.key} do-epic {key}", epic.paths.claude_code_transcripts)
|
|
loop = Loop(root=root, session=session, window=tmux.Window(epic.session, key, pane), paths=epic.paths, env=epic.env, launcher=epic.launcher, effects=epic.effects)
|
|
run = ChunkRun()
|
|
# What a chunk that was parked is told as it resumes; empty for a chunk that wasn't.
|
|
news = parked_through(repo, loop, epic, told=None, finished=False, based=based) if repo is not None else ""
|
|
while True:
|
|
if isinstance(news, Stop):
|
|
return news
|
|
run = dataclasses.replace(run, cycle_number=run.cycle_number + 1)
|
|
cycle = next_cycle(run, loop, repo is not None, epic, news)
|
|
if isinstance(cycle, Stop):
|
|
return cycle
|
|
if isinstance(cycle, EpicReopened):
|
|
say(f"the epic {epic.key} holds an open issue again, so its finishing chunk ends until none is open", epic.effects, stdout=True)
|
|
return handed_on(key, epic)
|
|
concluded = run_cycle(cycle)
|
|
if isinstance(concluded, Stop):
|
|
return concluded
|
|
closed = closed_cycle(run, cycle.task, cycle.project, concluded, cycle.project_archive, chunk_prompt.DRIVER, loop)
|
|
if isinstance(closed, Stop):
|
|
return closed
|
|
run = closed
|
|
news = parked_through(repo, loop, epic, told=news, finished=isinstance(concluded.verdict, Finished), based=based) if repo is not None else ""
|
|
# A chunk that was parked has work again, whatever the judge ruled before its pull request passed the gate.
|
|
if isinstance(concluded.verdict, Finished) and news == "":
|
|
return handed_on(key, epic, Landed.ON_EPIC_BRANCH if based is not None else Landed.ON_DEFAULT_BRANCH if repo is not None else Landed.NOWHERE)
|
|
|
|
|
|
def epic_based(key: str, root: str, epic: EpicRun) -> str | None | Stop:
|
|
"""Readies the epic's working branch for the chunk called key, whose repo is checked out at root: the branch, or None where the repo's config sets no `epic-branch`. It makes the branch on origin, or brings it level with the default branch, and records it as the base of the chunk's branch. A default branch that doesn't merge into it cleanly stops the loop, since a person resolves the conflict (docs/decisions.md → `epic-branch: an epic's chunks branch off and merge into a working branch …`)."""
|
|
config = config_located(root, epic.paths.home)
|
|
if isinstance(config, Failure):
|
|
return Stop(1, config.reason)
|
|
if config.epic_branch is None:
|
|
return None
|
|
branch = templates.epic_branch(config.epic_branch, epic.key)
|
|
default = epic.effects.git("symbolic-ref", "--short", "refs/remotes/origin/HEAD", cwd=root)
|
|
level = default if isinstance(default, Failure) else epic_branch.levelled(branch, default.strip(), cwd=root, timeout=LEVEL_SECONDS, run=epic.effects.git)
|
|
readied = level if isinstance(level, Failure) else record_chunk_base(key, branch, cwd=root, run=epic.effects.git)
|
|
if isinstance(readied, Failure):
|
|
return Stop(1, f"cannot ready the epic's working branch {branch} in {root} for {key}: {readied.reason}; once that is settled, run `do-epic {epic.key}` to start the chunk again")
|
|
say(f"{str(level).rstrip('.')}; {key}'s pull request goes into {branch}", epic.effects, stdout=True)
|
|
return branch
|
|
|
|
|
|
def issue_started(key: str, epic: EpicRun) -> None:
|
|
"""Starts the issue called key in the tracker, and says who else holds it, or why it didn't start. The chunk runs either way: the tracker shows the team the work, and the work doesn't read it."""
|
|
other = epic.store.start_issue(key)
|
|
if isinstance(other, Failure):
|
|
say(f"not every step that starts {key} in the tracker was made, and its chunk runs all the same: {other.reason}", epic.effects, stdout=True)
|
|
elif other:
|
|
say(f"{key} stays assigned to {other}", epic.effects, stdout=True)
|
|
|
|
|
|
def issue_read(key: str, epic: EpicRun) -> EpicIssue | Stop:
|
|
"""The epic's issue called key, read afresh."""
|
|
issues = epic.store.read()
|
|
if isinstance(issues, Failure):
|
|
return Stop(1, issues.reason)
|
|
found = [issue for issue in issues if issue.key == key]
|
|
return found[0] if found else Stop(1, f"{key} is no issue of the epic {epic.key}")
|
|
|
|
|
|
def chunk_root(key: str, issue: EpicIssue | None, epic: EpicRun) -> tuple[str, str | None] | Stop:
|
|
"""The folder the chunk called key works in, and the name of the repo whose checkout it is: None for the epic's folder, which holds work without a repo, as the epic's finishing is. issue is None for the epic's finishing. The chunk keeps both to its end, whatever its issue's labels become. The stop where the labels make the issue no chunk."""
|
|
pick = recorded(issue, epic.config.label_repos) if issue else ClaudeWork()
|
|
if isinstance(pick, Failure):
|
|
return Stop(1, pick.reason)
|
|
if isinstance(pick, ClaudeWork):
|
|
return str(epic.paths.home / scheduler.STATE / epic.key), None
|
|
if not isinstance(pick, Repo):
|
|
return Stop(1, f"{key} is no chunk: no label names its repo")
|
|
found = locate(pick.name, [], epic.config, epic.paths.home, epic.effects.git)
|
|
return Stop(1, found.reason) if isinstance(found, Failure) else (str(real(found.checkout)), pick.name)
|
|
|
|
|
|
def folder_made(root: str) -> Failure | None:
|
|
"""Makes the folder a chunk works in where it is missing, as the epic's folder is before its first chunk without a repo."""
|
|
try:
|
|
pathlib.Path(root).mkdir(parents=True, exist_ok=True)
|
|
except OSError as failure:
|
|
return Failure(f"could not make the chunk's folder {root}: {failure.strerror}")
|
|
return None
|
|
|
|
|
|
class Landed(enum.Enum):
|
|
"""Where a finished chunk's work went, which decides who moves its issue."""
|
|
# No repo took it, as for the epic's finishing and a chunk that changes none.
|
|
NOWHERE = enum.auto()
|
|
# A human did the task.
|
|
WITH_A_HUMAN = enum.auto()
|
|
ON_DEFAULT_BRANCH = enum.auto()
|
|
ON_EPIC_BRANCH = enum.auto()
|
|
|
|
|
|
class How(enum.Enum):
|
|
"""How a finished chunk's issue moves when it doesn't move to a status the config names."""
|
|
# do-epic neither moves nor awaits it.
|
|
LEFT = enum.auto()
|
|
# Nobody would move it, so the loop stops.
|
|
STUCK = enum.auto()
|
|
# The team's automation moves it, and do-epic waits for that.
|
|
AWAITED = enum.auto()
|
|
# do-epic moves it to done.
|
|
TO_DONE = enum.auto()
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class ToStatus:
|
|
"""do-epic moves a finished chunk's issue to status. Where that move fails, it moves the issue to through first and tries again; through is None where the config names no open status."""
|
|
status: str
|
|
through: str | None
|
|
|
|
|
|
# The move of a finished chunk's issue.
|
|
Move = How | ToStatus
|
|
|
|
|
|
def issue_move(landed: Landed, automated: bool, merged: str | None, opened: str | None, finishing: bool) -> Move:
|
|
"""The move of a finished chunk's issue. landed says where the chunk's work landed, automated says the team's automation moves issues, merged and opened are the statuses `jira-pull-request-merged-status` and `jira-pull-request-open-status` name, each None where the config names none, and finishing says the chunk is the epic's finishing. The first row that fits:
|
|
|
|
| The chunk | The automation moves issues | A merged status is named | Move |
|
|
|---|---|---|---|
|
|
| the epic's finishing, which has no issue | either | either | LEFT |
|
|
| a human's task | yes | either | LEFT: the human moves it |
|
|
| landed on the epic's working branch | either | yes | ToStatus merged, through opened |
|
|
| landed on the epic's working branch | yes | no | STUCK: the automation doesn't see that merge |
|
|
| landed on the default branch | yes | either | AWAITED |
|
|
| any other | either | either | TO_DONE |
|
|
|
|
A chunk that never parked left its issue before the open status, from which a workflow may offer no way to the merged one: hence through."""
|
|
if finishing or (landed is Landed.WITH_A_HUMAN and automated):
|
|
return How.LEFT
|
|
if landed is Landed.ON_EPIC_BRANCH and merged:
|
|
return ToStatus(merged, opened or None)
|
|
if landed is Landed.ON_EPIC_BRANCH and automated:
|
|
return How.STUCK
|
|
if landed is Landed.ON_DEFAULT_BRANCH and automated:
|
|
return How.AWAITED
|
|
return How.TO_DONE
|
|
|
|
|
|
def handed_on(key: str, epic: EpicRun, landed: Landed = Landed.NOWHERE) -> Stop:
|
|
"""The stop of a do-epic whose chunk finished. It first moves the chunk's issue as `issue_move` says, and the issue's parent too where the parent's sub-tasks now let it move (`parents_settled`), so that no finished chunk starts again and what they block may start. Then it starts each chunk that may start now. Where the team's automation moves the issue, the stop waits for that move (`moved_by_automation`). do-epic's window closes as it exits."""
|
|
move = issue_move(landed, epic.config.automation_moves, epic.config.pull_merged_status, epic.config.pull_open_status, finishing=key == epic.key)
|
|
issues: list[EpicIssue] | Failure
|
|
match move:
|
|
case How.LEFT:
|
|
issues = epic.store.read()
|
|
case ToStatus(status, through):
|
|
moved = epic.store.move_issue(key, status)
|
|
if isinstance(moved, Failure) and through and not isinstance(epic.store.move_issue(key, through), Failure):
|
|
moved = epic.store.move_issue(key, status)
|
|
if isinstance(moved, Failure):
|
|
return Stop(1, f"the chunk finished, but its issue could not move to {status!r}: {moved.reason}; once it has that status in Jira, run `do-epic {epic.key}` to start the chunks it blocks")
|
|
issues = epic.store.read()
|
|
# A search may not show the move yet, so the issue counts as moved here whatever the read says.
|
|
issues = issues if isinstance(issues, Failure) else [dataclasses.replace(each, status=status) if each.key == key else each for each in issues]
|
|
case How.STUCK:
|
|
return Stop(1, f"the chunk finished, but its issue stays as it is: its pull request merged into the epic's working branch, where the team's automation moves no issue, and do-epic's config names no `{PULL_MERGED_STATUS}` to move it to. Move {key} in Jira, then run `do-epic {epic.key}` to start the chunks it blocks")
|
|
case How.AWAITED:
|
|
issues = moved_by_automation(key, epic)
|
|
case How.TO_DONE:
|
|
moved = epic.store.finish_issue(key)
|
|
if isinstance(moved, Failure):
|
|
return Stop(1, f"the chunk finished, but its issue could not move to done: {moved.reason}; once it is done in Jira, run `do-epic {epic.key}` to start the chunks it blocks")
|
|
issues = epic.store.read()
|
|
# A search may not show the move yet, so the issue counts as done here whatever the read says.
|
|
issues = issues if isinstance(issues, Failure) else [dataclasses.replace(each, done=True) if each.key == key else each for each in issues]
|
|
if isinstance(issues, Failure):
|
|
return Stop(1, f"the chunk finished, but the next could not start: {issues.reason}")
|
|
closed = parents_settled(epic, issues)
|
|
if isinstance(closed, Stop):
|
|
return closed
|
|
started = chunks_started(epic, closed, key)
|
|
if isinstance(started, Failure):
|
|
return Stop(1, f"the chunk finished, but the next could not start: {started.reason}")
|
|
say(f"the chunk finished; started {', '.join(started.started) or 'no other'}", epic.effects, stdout=True)
|
|
return Stop(0, "")
|
|
|
|
|
|
def moved_by_automation(key: str, epic: EpicRun) -> list[EpicIssue]:
|
|
"""The epic's issues once the issue called key no longer holds up another: read every POLL_SECONDS until the team's automation, or a person, has moved it. Until then a read would show it open, and its chunk would start again. A read that fails is made again."""
|
|
told, unread = False, ""
|
|
while True:
|
|
issues = epic.store.read()
|
|
if isinstance(issues, Failure):
|
|
if issues.reason != unread:
|
|
say(f"cannot read the epic, so cannot tell whether {key} has moved; trying again every {POLL_SECONDS} seconds: {issues.reason}", epic.effects, stdout=True)
|
|
unread = issues.reason
|
|
epic.effects.sleep(POLL_SECONDS)
|
|
continue
|
|
by_key = {each.key: each for each in issues}
|
|
if key not in by_key or scheduler.settled(by_key[key], by_key, epic.config.unblocking):
|
|
return issues
|
|
if not told:
|
|
say(f"{key} is {by_key[key].status}: it waits for the team's automation to move it to {listed(epic.config.unblocking, 'or')}; move it in Jira if the automation doesn't", epic.effects, stdout=True)
|
|
told = True
|
|
epic.effects.sleep(POLL_SECONDS)
|
|
|
|
|
|
def parents_moved(epic: EpicRun, issues: list[EpicIssue]) -> list[EpicIssue]:
|
|
"""The issues as they read once `parent_moves.moved` has moved each parent whose sub-tasks a person filed; a search may not show those moves yet. A move that was not made is said, and the run goes on: the parent holds back only what it blocks."""
|
|
issues, refused = parent_moves.moved(epic.store, issues, epic.config.unblocking, epic.config.sub_tasks_landed_transition, epic.config.finish is not None)
|
|
for reason in refused:
|
|
say(reason, epic.effects, stdout=True)
|
|
return issues
|
|
|
|
|
|
def parents_settled(epic: EpicRun, issues: list[EpicIssue]) -> list[EpicIssue] | Stop:
|
|
"""The issues once each parent has moved as its sub-tasks let it: one whose sub-tasks a person filed (`parents_moved`), then one do-epic split (`split_items_closed`); the stop where a split item cannot move."""
|
|
return split_items_closed(epic, parents_moved(epic, issues))
|
|
|
|
|
|
def split_items_closed(epic: EpicRun, issues: list[EpicIssue]) -> list[EpicIssue] | Stop:
|
|
"""The issues, with each item split by repo whose sub-tasks are all done moved to done in the tracker and read as done; the stop where one cannot move. Its last sub-task to finish closes it, and one an earlier run left open closes with it."""
|
|
for item in issues:
|
|
if split.SPLIT_LABEL in item.labels and not item.done and split.finished(issues, item.key):
|
|
moved = epic.store.finish_issue(item.key)
|
|
if isinstance(moved, Failure):
|
|
return Stop(1, f"the chunk finished, but its split item {item.key} could not move to done: {moved.reason}; once it is done in Jira, run `do-epic {epic.key}` to start the chunks it blocks")
|
|
issues = [dataclasses.replace(each, done=True) if each.key == item.key else each for each in issues]
|
|
return issues
|
|
|
|
|
|
def next_cycle(run: ChunkRun, loop: Loop, in_repo: bool, epic: EpicRun, news: str = "") -> Cycle | EpicReopened | Stop:
|
|
"""The cycle run runs next, from the issue, the premises, the records, the models and the routing ladder read afresh; the stop where one of them cannot be read, and EpicReopened where the epic's finishing has no cycle to run. news is what `parked_through` tells the worker of a chunk that was parked, and opens its prompt. A chunk in a repo routes on that repo's model and ladder, and one without a repo on the machine setting `repoless-model` and the shipped ladder."""
|
|
premises = premises_read(loop.root if in_repo else None, epic)
|
|
if isinstance(premises, Failure):
|
|
return Stop(1, premises.reason)
|
|
language = epic.record.language()
|
|
if isinstance(language, Failure):
|
|
return Stop(1, language.reason)
|
|
records = records_read(loop.root if in_repo else None, epic)
|
|
if isinstance(records, Failure):
|
|
return Stop(1, records.reason)
|
|
work = chunk_work(loop.window.name, loop.root if in_repo else None, language or epic.config.language, epic)
|
|
if isinstance(work, (Stop, EpicReopened)):
|
|
return work
|
|
task, issue = work
|
|
routing = project_routing(loop, chunk_prompt.DRIVER) if in_repo else routing_on(repoless_models(epic), SHIPPED)
|
|
if isinstance(routing, Stop):
|
|
return routing
|
|
archive_folder = epic.paths.home / ARCHIVE / epic.key
|
|
route = announced_route(run.cycle_number, run.route, routing, archive_folder, lambda: issue, loop)
|
|
prompts = chunk_prompt.unparked(news) if news else chunk_prompt.restating(task) if loop.window.name == epic.key else chunk_prompt.WORKER
|
|
return cycle_of(loop, routing, route, archive_folder, task=task, facts="", premises=premises, records=records, unfinished_evidence=run.unfinished_evidence, prompts=prompts, taken_in=lambda entries: TakenIn())
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class EpicReopened:
|
|
"""The epic holds an open issue again, so its finishing chunk has no work until none is open: working that issue comes first."""
|
|
|
|
|
|
def chunk_work(key: str, checkout: str | None, language: str, epic: EpicRun) -> tuple[str, RoutingIssue | None] | EpicReopened | Stop:
|
|
"""The task of the chunk called key, and the issue that routes it, or None where that issue has no scores: both from the epic's issues, read afresh. The epic's finishing has no issue, and its work is EpicReopened once the epic holds an open issue again."""
|
|
if key != epic.key:
|
|
issue = issue_read(key, epic)
|
|
if isinstance(issue, Stop):
|
|
return issue
|
|
config = config_located(checkout, epic.paths.home) if checkout else None
|
|
if isinstance(config, Failure):
|
|
return Stop(1, config.reason)
|
|
return chunk_prompt.task(issue, epic.key, epic.config.label_repos, checkout, epic.launcher, language, config), RoutingIssue(issue) if "L" in issue.scores else None
|
|
issues = epic.store.read()
|
|
if isinstance(issues, Failure):
|
|
return Stop(1, issues.reason)
|
|
issues = finish.landed(issues, epic.config.pull_merged_status)
|
|
if not finish.due(issues):
|
|
return EpicReopened()
|
|
return finish.task(epic.key, issues, epic.config.label_repos, epic.launcher, language), None
|
|
|
|
|
|
def repoless_models(epic: EpicRun) -> Models:
|
|
"""The models of a chunk without a repo: `repoless-model`, unless the environment names another for the run, as it may for loop-claude."""
|
|
return overridden(epic.env, epic.config.repoless_model)
|
|
|
|
|
|
def records_read(root: str | None, epic: EpicRun) -> str | Failure:
|
|
"""The epic's Scoring and Decisions, from its description, then the records of the repo checked out at root, where the chunk has one, then the human's rules."""
|
|
sections: list[tuple[str, str]] = []
|
|
for name in ("Scoring", "Decisions"):
|
|
body = epic.record.section(name)
|
|
if isinstance(body, Failure):
|
|
return Failure(f"the epic {epic.key}'s description: {body.reason} - {CONSEQUENCE}.")
|
|
sections.append((f"the epic {epic.key}'s description, ## {name}", body))
|
|
return read_records(root, epic.paths.home, epic.paths.claude_dir, tuple(sections))
|
|
|
|
|
|
def premises_read(root: str | None, epic: EpicRun) -> str | Failure:
|
|
"""The epic's Goals and Audience, from its description, then those of the repo checked out at root, where the chunk has one. A Failure where the epic lacks either: no chunk runs without premises."""
|
|
bodies: list[str] = []
|
|
for name in ("Goals", "Audience"):
|
|
body = epic.record.section(name)
|
|
if isinstance(body, Failure):
|
|
return Failure(f"the epic {epic.key}'s description: {body.reason} - no premises.")
|
|
if not body.strip():
|
|
return Failure(f"the epic {epic.key}'s description has no text under a `## {name}` heading - no premises.")
|
|
bodies.append(body)
|
|
theirs = read_premises(root, epic.paths.home) if root else ""
|
|
if isinstance(theirs, Failure):
|
|
return theirs
|
|
return f"the epic {epic.key}'s description:\n## Goals\n{bodies[0]}\n\n## Audience\n{bodies[1]}" + (f"\n\nThe repo's goals, which rank after the epic's and after code health, are from {theirs}" if theirs else "")
|