Files
claude-config/bin/chunk_preflight/facts.py
T

132 lines
7.0 KiB
Python

"""The lines the preflight prints, stated from what it gathered."""
from release_plan.todo_table import bundle_candidates, BUNDLE_MAX_R, Item, ships_alone
from chunk_preflight.choices import cut_name, PULLS_LIST
from release_plan.release_standing import dated_cut, LastCut, unreleased_entries, UNSEEN_RELEASES
from failure import Failure
from chunk_preflight.gathered import Forge, Gathered, ItemFacts, NamedPath, NoItem, Plan, Ran
from text_files.markdown_text import listed, quoted
from release_plan.release import version
# CLAUDE.md → "Working loop" lets up to ten tiny items share a chunk, so a worker never needs more. A release with 24 joinable items would otherwise put about 155 lines of detail in every prompt.
JOINING_DETAILS = 10
def render(found: Gathered) -> list[str]:
"""One `- ` line per fact, then the item's tagline and detail, the detail of each item that may join its chunk, and what each tool printed."""
note = [f"- {found.note}"] if found.note else []
unseen = [f"- {UNSEEN_RELEASES}"] if found.plan_outside else []
head = [*note, f"- {last_release(found.default, found.cut)}", *unseen]
paths = f"- The project's config puts its files at: {'; '.join(f'{setting} {path}' for setting, path in found.paths.items())}."
if isinstance(found.item, NoItem):
return [*head, f"- No plan item: {found.item.why} No worktree was made.", paths]
lines = item_lines(found.plan, found.item)
return [*head, *lines[:2], paths, *lines[2:]]
def last_release(default: str, release: LastCut) -> str:
if isinstance(release.tag, Failure):
return f"Last version cut: cannot be read: {release.tag.reason}"
named = cut_name(release)
cut = f"Last version cut: {named 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."
headed = dated_cut(release.tag, release.changelog)
since = f"since {release.tag}" if release.tag else f"since CHANGELOG.md dated {version(headed)}" if headed 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(plan: 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 {plan}: {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)}",
*joining_details(item.plan),
*(f"\n{printed(shown, done)}" for shown, done in item.tools),
]
def joining_details(plan: Plan) -> list[str]:
"""The detail of each of the first JOINING_DETAILS items that may join the chunk, in the table's order, and what the worker lacks: an item with no detail, the items left out, or details that cannot be read."""
joining = bundle_candidates(plan.items, plan.first)
if not joining:
return []
if isinstance(plan.details, Failure):
return [f"\nThe details of the items that may join cannot be read: {plan.details.reason}"]
shown, left = joining[:JOINING_DETAILS], joining[JOINING_DETAILS:]
lines = [f"\nItem {item.id}'s detail:\n\n{quoted(detail)}" if (detail := plan.details.get(item.id)) else f"\nItem {item.id} has no detail." for item in shown]
if left:
lines.append(f"\nThe details of items {listed([str(item.id) for item in left], 'and')} are not quoted: only the first {JOINING_DETAILS} are. Open `todo.md` for them.")
return lines
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 `#<number> <URL>`: 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 no output."
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}"