Merge origin/main into item-412

This commit is contained in:
2026-11-19 02:17:59 +01:00
26 changed files with 945 additions and 120 deletions
+81 -30
View File
@@ -9,7 +9,7 @@ import sys
import typing
from loop_claude.loop.chunk_start import archived_route, routed
from do_epic import chunk_prompt, findings, gates, parked, scheduler, status
from do_epic import chunk_prompt, findings, finish, gates, parked, scheduler, split, status
from do_epic.repo_pick import ClaudeWork, HumanTask, recorded, Repo
from loop_claude.loop.close import closed_cycle
from loop_claude.project.records import CONSEQUENCE, read_records
@@ -42,6 +42,7 @@ from agent_cycle import tmux
HELP = f"""usage: do-epic <EPIC-KEY> [--status]
{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>.
An open issue of the epic runs as a chunk once it has a label and no issue blocks it.
@@ -54,6 +55,7 @@ on once the checks pass, the approval comes or a reviewer comments, and a place
name says `awaits a slot`. 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.
{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"
@@ -68,7 +70,7 @@ 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), EpicRecord(EpicDescription(epic, config.env_file))
return JiraEpic(epic, config.env_file, finish=config.finish), EpicRecord(EpicDescription(epic, config.env_file))
@dataclasses.dataclass(frozen=True)
@@ -112,9 +114,10 @@ def run(argv: list[str], env: typing.Mapping[str, str], paths: Paths, launcher:
words = words[:1] if status_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
if (len(words) != 1 and not writes) or not ISSUE_KEY.fullmatch(words[0]) or (chunk is not None and not ISSUE_KEY.fullmatch(chunk)):
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:
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 reason := tmux.unusable(effects.tmux, chunk_prompt.DRIVER):
@@ -136,23 +139,27 @@ def status_printed(epic: EpicRun) -> Stop:
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: tuple[str, str, str | None], paths: Paths, backend: Backend, stdin: typing.TextIO) -> Stop:
"""Files a finding in the epic, or records a decision in its description. It needs no tmux, so a chunk's worker may run it anywhere."""
def epic_written(key: str, writes: tuple[str, str, tuple[str, ...]] | 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)
done = findings.written(key, *writes, *backend(config, key), stdin, paths.home / scheduler.STATE / key)
store, record = backend(config, key)
done = findings.written(key, *writes, store, record, stdin, paths.home / scheduler.STATE / key) if writes else finish.closed(key, store, stdin)
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: 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 = chunks_started(epic, None)
issues = epic.store.read()
started = issues if isinstance(issues, Failure) else chunks_started(epic, issues, None)
if isinstance(started, Failure):
return Stop(1, started.reason)
say(f"started {', '.join(started.started)} in the tmux session {epic.session}" if started.started else "started 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)
if not tmux.listed(epic.session, epic.effects.tmux):
return Stop(0, "")
if not epic.effects.on_terminal():
@@ -162,13 +169,16 @@ def windows_opened(epic: EpicRun) -> Stop:
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 chunks_started(epic: EpicRun, own: str | None) -> scheduler.Started | Failure:
"""scheduler.start_chunks on the epic's issues and cap, read afresh; own is the issue of the chunk that asks."""
issues = epic.store.read()
if isinstance(issues, Failure):
return issues
def chunks_started(epic: EpicRun, issues: list[EpicIssue], own: str | None) -> scheduler.Started | Failure:
"""scheduler.start_chunks on the epic's issues, as read, and its cap, read afresh; own is the issue of the chunk that asks. Where no issue is open, the chunk to start is the epic's finishing, unless the epic is closed."""
closed = finish.due(issues) and epic.store.closed()
if isinstance(closed, Failure):
return closed
scheduled = scheduled_epic(epic)
return scheduled if isinstance(scheduled, Failure) else scheduler.start_chunks(scheduled, issues, own, epic.effects.tmux)
if isinstance(scheduled, Failure):
return scheduled
started = scheduler.start_chunks(scheduled, issues, finish.due(issues) and not closed, own, epic.effects.tmux)
return started if isinstance(started, Failure) else dataclasses.replace(started, closed=closed)
def scheduled_epic(epic: EpicRun) -> scheduler.Epic | Failure:
@@ -218,10 +228,7 @@ def chunk_run(key: str, epic: EpicRun) -> Stop:
pane = epic.env.get("TMUX_PANE")
if not pane:
return Stop(1, f"{scheduler.CHUNK_FLAG} is how do-epic starts itself in a tmux window; run do-epic <EPIC-KEY>")
issue = issue_read(key, epic)
if isinstance(issue, Stop):
return issue
place = chunk_root(issue, epic)
place = chunk_root(key, epic)
if isinstance(place, Stop):
return place
root, repo = place
@@ -292,7 +299,8 @@ def parked_through(repo: str, loop: Loop, epic: EpicRun, starts: bool) -> str |
shown = awaits
if on is None:
# Once the window shows as parked, so the chunks that start count it as running nowhere.
started = chunks_started(epic, key)
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)
on = now.on if isinstance(now, gates.Gate) else on
if stays is not None and stays != said:
@@ -322,9 +330,12 @@ def issue_read(key: str, epic: EpicRun) -> EpicIssue | Stop:
return found[0] if found else Stop(1, f"{key} is no issue of the epic {epic.key}")
def chunk_root(issue: EpicIssue, epic: EpicRun) -> tuple[str, str | None] | Stop:
"""The folder the issue's chunk works in, and the name of the repo whose checkout it is: None for the epic's folder, which holds work without a repo. The chunk keeps both to its end, whatever the issue's labels become. The stop where the labels make the issue no chunk."""
pick = recorded(issue)
def chunk_root(key: str, 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. The chunk keeps both to its end, whatever its issue's labels become. The stop where the labels make the issue no chunk."""
issue = None if key == epic.key else issue_read(key, epic)
if isinstance(issue, Stop):
return issue
pick = recorded(issue) if issue else ClaudeWork()
if isinstance(pick, Failure):
return Stop(1, pick.reason)
if isinstance(pick, ClaudeWork):
@@ -332,25 +343,45 @@ def chunk_root(issue: EpicIssue, epic: EpicRun) -> tuple[str, str | None] | Stop
folder.mkdir(parents=True, exist_ok=True)
return str(folder), None
if not isinstance(pick, Repo):
return Stop(1, f"{issue.key} is no chunk: {'it is a human\'s task' if isinstance(pick, HumanTask) else 'no label names its repo'}")
return Stop(1, f"{key} is no chunk: {'it is a human\'s task' if isinstance(pick, HumanTask) else '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 handed_on(key: str, epic: EpicRun) -> Stop:
"""The stop of a do-epic whose chunk finished, once it has started each chunk that may start now. Its window closes as it exits."""
started = chunks_started(epic, key)
"""The stop of a do-epic whose chunk finished. It first moves the chunk's issue to done (the epic's finishing has none), and its split item too where that was the last of the item's sub-tasks, so that no finished chunk starts again and what they block may start, then starts each chunk that may start now. Its window closes as it exits."""
# The epic's finishing has no issue to move: the epic is closed by `--close-epic`.
moved = None if key == epic.key else 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()
if isinstance(issues, Failure):
return Stop(1, f"the chunk finished, but the next could not start: {issues.reason}")
# A search may not show the move yet, so the issue counts as done here whatever the read says.
issues = [dataclasses.replace(each, done=True) if each.key == key else each for each in issues]
closed = split_items_closed(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 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(loop_run: LoopRun, loop: Loop, in_repo: bool, epic: EpicRun, news: str = "") -> Cycle | Stop:
"""The cycle loop_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. 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."""
issue = issue_read(loop.window.name, epic)
if isinstance(issue, Stop):
return issue
premises = premises_read(loop.root if in_repo else None, epic)
if isinstance(premises, Failure):
return Stop(1, premises.reason)
@@ -360,6 +391,10 @@ def next_cycle(loop_run: LoopRun, loop: Loop, in_repo: bool, epic: EpicRun, news
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):
return work
task, routing = work
models = get_or_ask_models(project_key(loop.root, epic.paths.home), epic.paths, epic.env, chunk_prompt.DRIVER, epic.effects) if in_repo else repoless_models(epic)
if isinstance(models, Stop):
return models
@@ -371,9 +406,25 @@ def next_cycle(loop_run: LoopRun, loop: Loop, in_repo: bool, epic: EpicRun, news
return Stop(1, ladder.reason)
archive_folder = epic.paths.home / ARCHIVE / epic.key
project = ladder.project_rung(models.worker)
route = routed(loop_run.route, project, models.worker, lambda: archived_route(archive_folder, loop), lambda: RoutingIssue(issue) if "L" in issue.scores else None)
route = routed(loop_run.route, project, models.worker, lambda: archived_route(archive_folder, loop), lambda: routing)
say(f"\n=== cycle {loop_run.cycle_number}, worker on {route.rung} ===", epic.effects, stdout=True)
return Cycle(config=setup_version(epic.paths.claude_dir, epic.effects.git), effects=epic.effects, facts="", judge=judge_rung(ladder, models.judge), premises=premises, project=project, project_archive=archive_folder, records=records, root=loop.root, route=route, session=loop.session, task=chunk_prompt.task(issue, epic.key, loop.root if in_repo else None, epic.launcher, language or epic.config.language), prompts=chunk_prompt.unparked(news) if news else chunk_prompt.WORKER, taken_in=lambda entries: TakenIn(), unfinished_evidence=loop_run.unfinished_evidence, window=loop.window)
return Cycle(config=setup_version(epic.paths.claude_dir, epic.effects.git), effects=epic.effects, facts="", judge=judge_rung(ladder, models.judge), premises=premises, project=project, project_archive=archive_folder, records=records, root=loop.root, route=route, session=loop.session, task=task, prompts=chunk_prompt.unparked(news) if news else chunk_prompt.restating(task) if loop_run.chunk == epic.key else chunk_prompt.WORKER, taken_in=lambda entries: TakenIn(), unfinished_evidence=loop_run.unfinished_evidence, window=loop.window)
def chunk_work(key: str, checkout: str | None, language: str, epic: EpicRun) -> tuple[str, RoutingIssue | None] | 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 it ends here once the epic holds an open issue again, since working that issue comes first."""
if key != epic.key:
issue = issue_read(key, epic)
if isinstance(issue, Stop):
return issue
return chunk_prompt.task(issue, epic.key, checkout, epic.launcher, language), RoutingIssue(issue) if "L" in issue.scores else None
issues = epic.store.read()
if isinstance(issues, Failure):
return Stop(1, issues.reason)
if not finish.due(issues):
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)
return finish.task(epic.key, issues, epic.launcher, language), None
def repoless_models(epic: EpicRun) -> Models: