"""What a do-epic cycle's worker is told: its task, from the issue, and its first prompt. The resumed prompt and the rung sentences are the ones loop-claude's cycles use, naming do-epic; a chunk that was parked is first told why it resumes, and one whose task follows the epic's state is told that task as it resumes.""" import dataclasses from chunk_driver.drivers import DO_EPIC from do_epic.backend.protocol import EpicIssue from do_epic import findings from chunk_driver.project.records import HUMAN_RULE_NAMES, RECORD_FILES from do_epic.repo_pick import LabelRepos from text_files.markdown_text import listed import pathlib from project_config.settings import ProjectConfig from model_routing.ladder import ProjectRung from chunk_driver import prompts from model_routing.route import Route from project_config import templates from agent_cycle.worker_prompts import WorkerPrompts # The entry point's name as the worker, the judge and the human read it. DRIVER = DO_EPIC def shipping(config: ProjectConfig) -> str: """How a chunk in the repo that config names ships its change. An epic's issue isn't an item of the repo's plan, so `chunk-pr merge --finish`, which adds a finished item's changelog bullet, doesn't apply: the worker writes the bullet itself, where the repo publishes something others consume. (docs/decisions.md → `do-epic: drives one Jira epic …`, "Releases"). Where the repo's config leaves the merge to a person, the worker ends its run on `chunk-pr merge`'s word, since do-epic parks the chunk until that merge ("Long gates").""" opening = "The issue isn't an item of the repo's plan, so run `chunk-pr merge` without `--finish`." if config.merge == "human" and config.epic_branch is None: opening += " The repo's team merges its pull requests itself, so `chunk-pr merge` leaves the pull request ready for a person to merge: end your run when it says so." if config.epic: return f"{opening} The repo's plan is a Jira epic, and the history of its issues serves as the changelog, so leave the changelog as it is." changelog = config.paths["changelog"].relative_to(config.top) if "changelog" in config.paths and config.paths["changelog"].is_relative_to(config.top) else config.places["changelog"] return f"{opening} Where the repo publishes something others consume, add one bullet to `## Unreleased` in `{changelog}` that tells the repo's consumers what changed; otherwise leave the changelog as it is." def epic_branch_rule(config: ProjectConfig, epic: str) -> str: """What a chunk is told where the repo's config sets `epic-branch`, empty where it doesn't: its base, that its merge needs no person, how a later push asks for a review, and which branch the epic's other repos are tested at (docs/decisions.md → `epic-branch: an epic's chunks branch off and merge into a working branch …`).""" if config.epic_branch is None: return "" branch = templates.epic_branch(config.epic_branch, epic) pushed = " After `chunk-pr ready`, push each commit with `chunk-pr push`, which asks the repo's auto-reviewer to review the new head, and wait for that review." if config.review_request_comment else "" return f"The chunk's pull request goes into the epic's working branch `{branch}`, not into the default branch: {DRIVER} has made that branch level with the default branch and recorded it as the base of the chunk's branch, so make the chunk's worktree and branch off `origin/{branch}`. `chunk-pr merge` merges into `{branch}` without waiting for a person.{pushed} Where the chunk's tests run other repos of the epic, run each at its branch `{branch}` where it has one, else at its default branch, and name in the chunk's summary each repo that ran at its default branch." def task(issue: EpicIssue, epic: str, team: LabelRepos, checkout: str | None, launcher: pathlib.Path, language: str, config: ProjectConfig | None) -> str: """The task of the chunk that works issue, of the epic with the key epic: in checkout, or without a repo where it is None. team is the config's LABEL_REPOS table. launcher is the do-epic that runs the chunk, language the one the epic's Jira text is in, and config the project config of the repo's checkout, None without a repo.""" shipped = " ".join(filter(None, [shipping(config), epic_branch_rule(config, epic)])) if config else "" where = f"in the checkout {checkout}, on the branch {issue.key}" if checkout else "which changes no repo: work in the folder you start in" return f"Work the Jira issue {issue.key} of the epic {epic} as one chunk, {where}. {" ".join(filter(None, [findings.instructions(epic, launcher, language, team, issue), shipped]))} Its summary: {issue.summary}" + (f"\n\nIts description:\n\n{issue.detail}" if issue.detail else "") CODE_HEALTH = 'the setup\'s CLAUDE.md → "Shared goal"' def fresh(task: str, premises: str, route: Route, project: ProjectRung | None, facts: str) -> str: """A chunk's first prompt. facts is empty: do-epic runs no preflight.""" routed = f"{DRIVER} runs you on {route.rung}, routed by the issue's L of {route.likelihood}. Where a sentence below names item {route.item}, it means the issue." if route.item else "" opening = " ".join(filter(None, [routed, *prompts.rung_sentences(DRIVER, route, project)])) return f"{task}\n\nContinue any open draft PR or worktree for the chunk before creating a new one. {prompts.READING_RULE}\n\n{opening and f'{opening}{chr(10)}{chr(10)}'}The issue sets the chunk's scope. The goals below choose between ways to deliver that scope, and never widen it. The epic's goals rank first, then code health ({CODE_HEALTH}), then the repo's.\n\nFrom {premises}" # The worker names no plan items: the chunk's one item is its issue. # The epic's records come first, as its goals do; a chunk without a repo is handed only those and the human's rules, so the names that mention a repo's then name nothing it reads. NAMES = prompts.RecordNames( quote_sources=listed(("the epic's description", "the repo's goals file's Goals and Audience", *RECORD_FILES, *HUMAN_RULE_NAMES), "or"), answer_destinations="the epic's description under ## Decisions, the repo's goals file's Goals or Audience, " + ", ".join(RECORD_FILES) + ", the repo's plan or a comment", records_named="the epic's scoring and decisions, then the repo's working rules and decisions, then the human's rules for every project", ) WORKER = prompts.worker_prompts(DRIVER, fresh, "", NAMES) # What a worker that was parked does: with a reviewer's feedback, and with a pull request a person merged, as the decision asks (docs/decisions.md → `do-epic: drives one Jira epic …`, "Long gates"). FEEDBACK = 'Where a reviewer left feedback, fix what is reasonable and push the fix, answer each other comment in its thread, and change the setup so our review would have caught the finding (the setup\'s CLAUDE.md → "Working loop", "Change the setup for each finding an outside reviewer raises on a PR, deterministic tools first"). Then merge the pull request as before. Where the pull request is merged already, run `chunk-pr clean` in its worktree instead, and finish the chunk; where it was closed without a merge, say so and end your run.' def unparked(news: str) -> WorkerPrompts: """The prompts of the first cycle after a parked chunk resumes: WORKER's, with each prompt that starts a worker opening with news, which says what the chunk waited on and what changed. The fresh prompt opens with it too, since a chunk the judge ruled finished before it parked has no session left to resume.""" opening = f"{news} {FEEDBACK}\n\n" return dataclasses.replace(WORKER, fresh=lambda *task: opening + WORKER.fresh(*task), resumed=lambda *evidence: opening + WORKER.resumed(*evidence)) def restating(task: str) -> WorkerPrompts: """WORKER for a chunk whose task follows the epic's state, as its finishing's does: the prompt that resumes its worker states task, which may not be the one its session began with.""" return dataclasses.replace(WORKER, resumed=lambda *evidence: f"{WORKER.resumed(*evidence)}\n\nThe task follows the epic's state, so it may have changed since you began. It is now:\n\n{task}")