"""The lines the preflight prints, stated from what it gathered.""" from todo_table import bundle_candidates, BUNDLE_MAX_R, Item, ships_alone, version from failure import Failure from chunk_preflight.gathered import Forge, Gathered, ItemFacts, NamedPath, NoItem, Plan, Ran from chunk_pick import LastCut, unreleased_entries from chunk_preflight.choices import PULLS_LIST from markdown_text import quoted def render(found: Gathered) -> list[str]: """One `- ` line per fact, then the item's tagline and detail, and what each tool printed.""" note = [f"- {found.note}"] if found.note else [] head = [*note, f"- {last_release(found.default, found.cut)}"] if isinstance(found.item, NoItem): return [*head, f"- No plan item: {found.item.why} No worktree was made."] return [*head, *item_lines(found.default, found.item)] def last_release(default: str, release: LastCut) -> str: if isinstance(release.tag, Failure): return f"Last version cut: cannot be read: {release.tag.reason}" cut = f"Last version cut: {release.tag or 'none'}." entries = unreleased_entries(release.changelog) if isinstance(release.changelog, str) else None if entries is not None: return f"{cut} Unreleased: {counted(entries, 'entry', 'entries')} under CHANGELOG.md → ## Unreleased." since = f"since {release.tag}" if release.tag else f"on {default}" commits = f"the commits {since} cannot be counted: {release.commits.reason}" if isinstance(release.commits, Failure) else f"{counted(release.commits, 'commit', 'commits')} {since}" changelog = f"CHANGELOG.md cannot be read: {release.changelog.reason}" if isinstance(release.changelog, Failure) else "no ## Unreleased in CHANGELOG.md" return f"{cut} Unreleased: {commits}; {changelog}." def item_lines(default: str, item: ItemFacts) -> list[str]: first = item.plan.first worktree = item.worktree.reason if isinstance(item.worktree, Failure) else f"{item.worktree.path}, {item.worktree.how}" return [ f"- The earliest release in {default}'s todo.md: {version(first.release)}. Its first item that does not wait: item {first.id}, {levels(first)}.", f"- Worktree: {worktree}", f"- Open pull requests from {item.plan.branch}: {pulls_text(item.pulls, item.plan.branch)}", *bundled(item.plan), *named_lines(item.named), f"\nItem {first.id}'s tagline and detail:\n\n{quoted(item.plan.text)}", *(f"\n{printed(shown, done)}" for shown, done in item.tools), ] def pulls_text(pulls: tuple[Forge, Ran | Failure] | None, branch: str) -> str: """Each open pull request from branch, by its number and URL, or why none can be listed. No title: on a public forge anyone may open one, and its title would reach the worker's prompt.""" if pulls is None: return "none, as the checkout has no origin remote." forge, done = pulls shown = " ".join(PULLS_LIST[forge]) if isinstance(done, Failure): return f"cannot be listed: `{shown}` {done.reason}" if done.code: return f"cannot be listed: `{shown}` exited {done.code}: {done.stderr.strip()}" return ", ".join(open_pulls(done.stdout, forge, branch)) or "none." def open_pulls(stdout: str, forge: Forge, branch: str) -> list[str]: """Each open pull request from branch in the rows pulls_command printed, as `# `: gh prints only branch's pull requests, as number and URL; tea prints every pull request, as index, head and URL, under a header row.""" rows = [line.split("\t") for line in stdout.splitlines()] if forge == "gh": return [f"#{row[0]} {row[1]}" for row in rows if len(row) == 2] return [f"#{row[0]} {row[2]}" for row in rows if len(row) == 3 and row[1] == branch] def bundled(plan: Plan) -> list[str]: first, candidates = plan.first, bundle_candidates(plan.items, plan.first) if ships_alone(first): return [f"- Item {first.id} has R above {BUNDLE_MAX_R}, so it ships alone."] if not candidates: return [f"- No other item of release {version(first.release)} may join item {first.id}'s chunk."] listed = [f" - item {item.id}, {levels(item)}, S {item.scores['S']}: {item.tagline}" for item in candidates] return [f'- The items of release {version(first.release)} that may join the chunk of item {first.id}, where `~/.claude/CLAUDE.md` → "Working loop" lets them share it:', *listed] def named_lines(named: list[NamedPath] | Failure) -> list[str]: if isinstance(named, Failure): return [f"- The files the item names: cannot be listed: {named.reason}"] if not named: return ["- The item names no file or folder git tracks."] return ["- The files and folders the item names:", *(f" - `{path.span}`: {size(path)}" for path in named)] def size(path: NamedPath) -> str: if path.lines is None: return counted(path.files, "file", "files") if isinstance(path.lines, Failure): return f"cannot be read: {path.lines.reason}" return counted(path.lines, "line", "lines") def printed(shown: str, done: Ran | Failure) -> str: """What a tool printed and its exit code, under its command line.""" if isinstance(done, Failure): return f"`{shown}` {done.reason}" output = "\n".join(part.strip() for part in (done.stdout, done.stderr) if part.strip()) return f"`{shown}` exited {done.code}, printing:\n\n{quoted(output)}" if output else f"`{shown}` exited {done.code}, printing nothing." def levels(item: Item) -> str: return f"L {item.likelihood or 'unscored'}, E + I {item.escape_and_impact or 'unscored'}" def counted(count: int, one: str, many: str) -> str: return f"{count} {one if count == 1 else many}"