Answer principles round 1: 0 🔴, 6 🟡; read the plan from the fetched default branch for routing and the preflight alike and drop the fast-forward, name no item where the release after the last tag has none, tell an empty items table from none, time out the fetch, return run_python's errors as exit codes, test the error paths and the gh rows, assert facts in place of sentences, keep the open-PR sentence in every fresh prompt, name the item branch in CLAUDE.md, and measure item 175 against five chunks with counter-signals
This commit is contained in:
+73
-65
@@ -5,14 +5,17 @@ usage: chunk-preflight
|
||||
Run it inside a project checkout. loop-claude runs it at the checkout root before each chunk's
|
||||
first worker, before it routes the chunk, and quotes what it prints in the worker's prompt. It
|
||||
fetches origin, when the checkout has that remote, and takes origin's default branch, or else the
|
||||
local main or master, as larv-review does. It fast-forwards the checkout to that branch where the
|
||||
checkout is on it, behind it and holds no uncommitted change; else it says why not. It then reads
|
||||
the checkout's own todo.md, the file loop-claude routes on, and prints:
|
||||
local main or master, as larv-review does. It leaves the checkout as it is: it reads todo.md as
|
||||
that branch holds it, the plan loop-claude routes on, and prints:
|
||||
|
||||
- the newest v* tag on the default branch, and how many entries the default branch's CHANGELOG.md
|
||||
holds under ## Unreleased, or else how many commits followed the tag;
|
||||
- the next release in todo.md, and that release's first item that does not wait, the item
|
||||
loop-claude routes on, with its L and E + I;
|
||||
- the earliest release in todo.md, and that release's first item that does not wait, the item
|
||||
loop-claude routes on, with its L and E + I. It names that item only where the tag names no
|
||||
version x.y.z, or the release is x.y.z+1, x.y+1.0 or x+1.0.0. For a later release, or a todo.md
|
||||
that holds no item, it says the release after the tag holds none: finished and ready to cut where
|
||||
commits followed the tag, and else to be planned first. For x.y.z or an earlier release, it says
|
||||
todo.md files items under a release already cut;
|
||||
- the item's worktree: a sibling of the checkout named <checkout>-item-<ID>, on the branch
|
||||
item-<ID>. It continues a worktree already on that branch; else it adds one on the local branch,
|
||||
else on a new branch tracking origin's, else on a new branch off the default branch;
|
||||
@@ -27,84 +30,97 @@ the checkout's own todo.md, the file loop-claude routes on, and prints:
|
||||
`larv-review places` print in the worktree, each with its exit code. reading-cost appends to the
|
||||
worktree's docs/reading-cost.md, for the chunk to commit.
|
||||
|
||||
A fact it cannot find it says it cannot, and why, and goes on. A tool or forge CLI still running
|
||||
after TIMEOUT seconds is stopped. With no item, it makes no worktree and runs none of those four
|
||||
tools.
|
||||
A fact it cannot find it says it cannot, and why, and goes on. A fetch, forge CLI or tool still
|
||||
running after {timeout} seconds, or after the seconds CHUNK_PREFLIGHT_TIMEOUT sets, is stopped; a
|
||||
stopped fetch reads origin as last fetched. With no item, it makes no worktree and runs none of
|
||||
those four tools.
|
||||
|
||||
Exit 1, saying why, on any argument, or when git cannot start or finds no checkout.
|
||||
Exit 1, saying why, on any argument, on a CHUNK_PREFLIGHT_TIMEOUT that is no number of seconds
|
||||
above 0, or when git cannot start or finds no checkout or no default branch.
|
||||
"""
|
||||
import pathlib
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import typing
|
||||
|
||||
from git_repo import commit_exists, default_branch, file_at, git, newest_release_tag, top
|
||||
from git_repo import commit_exists, commits_since, default_branch, default_branch_todo, file_at, git, newest_release_tag, top
|
||||
from failure import Failure
|
||||
from chunk_preflight.gathered import Forge, forge_of, Gathered, held_back, ItemFacts, named_paths, NamedPath, Plan, planned, pulls_command, Ran, Release, Worktree, worktree_add, worktree_on
|
||||
from chunk_preflight.gathered import Forge, forge_of, Gathered, ItemFacts, named_paths, NamedPath, NoItem, Plan, planned, pulls_command, Ran, Release, Worktree, worktree_add, worktree_on
|
||||
from chunk_preflight.facts import render
|
||||
|
||||
BIN = pathlib.Path(__file__).resolve().parents[1]
|
||||
# Each tool run in the item's worktree, {item} standing for the item's ID.
|
||||
TOOLS = (("rank_todo.py", "--check", "todo.md"), ("larv-review", "needs-scoring-run", "{item}"), ("reading-cost",), ("larv-review", "places"))
|
||||
# Seconds before a tool or forge CLI is stopped, so a hung one leaves the facts the others find.
|
||||
# Seconds before a fetch, forge CLI or tool is stopped, so a hung one leaves the facts the others find; TIMEOUT_SETTING sets others.
|
||||
TIMEOUT = 60
|
||||
TIMEOUT_SETTING = "CHUNK_PREFLIGHT_TIMEOUT"
|
||||
|
||||
|
||||
def main(argv: list[str]) -> int:
|
||||
def main(argv: list[str], env: typing.Mapping[str, str]) -> int:
|
||||
if argv in (["-h"], ["--help"]):
|
||||
print(__doc__.strip())
|
||||
print(__doc__.strip().format(timeout=TIMEOUT))
|
||||
return 0
|
||||
if argv:
|
||||
print(f"unknown arguments {' '.join(argv)}; usage: chunk-preflight", file=sys.stderr)
|
||||
return 1
|
||||
root = top()
|
||||
if isinstance(root, Failure):
|
||||
print(root.reason, file=sys.stderr)
|
||||
printed = run(env.get(TIMEOUT_SETTING))
|
||||
if isinstance(printed, Failure):
|
||||
print(printed.reason, file=sys.stderr)
|
||||
return 1
|
||||
print("\n".join(preflight(pathlib.Path(root))))
|
||||
print("\n".join(printed))
|
||||
return 0
|
||||
|
||||
|
||||
def preflight(root: pathlib.Path) -> list[str]:
|
||||
"""Gathers the facts step by step, each step taking what the ones before it found, and renders them."""
|
||||
default = default_branch(stale_ok=True)
|
||||
def run(setting: str | None) -> list[str] | Failure:
|
||||
"""The preflight's lines, given what TIMEOUT_SETTING holds; a Failure where that is no number of seconds, or git finds no checkout or no default branch."""
|
||||
timeout = seconds(setting)
|
||||
if isinstance(timeout, Failure):
|
||||
return timeout
|
||||
root = top()
|
||||
return root if isinstance(root, Failure) else preflight(pathlib.Path(root), timeout)
|
||||
|
||||
|
||||
def seconds(setting: str | None) -> float | Failure:
|
||||
"""The seconds a TIMEOUT_SETTING of setting gives, TIMEOUT where it is unset; a Failure where it is no number above 0."""
|
||||
if setting is None:
|
||||
return TIMEOUT
|
||||
if re.fullmatch(r"[0-9]+(\.[0-9]+)?", setting) and float(setting) > 0:
|
||||
return float(setting)
|
||||
return Failure(f"{TIMEOUT_SETTING}={setting!r} is no number of seconds above 0")
|
||||
|
||||
|
||||
def preflight(root: pathlib.Path, timeout: float) -> list[str] | Failure:
|
||||
"""Gathers the facts step by step, each step taking what the ones before it found, and renders them; a Failure where the checkout has no default branch."""
|
||||
default = default_branch(stale_ok=True, timeout=timeout)
|
||||
if isinstance(default, Failure):
|
||||
return [f"- No default branch: {default.reason}"]
|
||||
return default
|
||||
ref, note = default
|
||||
# First, so the plan read next is the fast-forwarded checkout's.
|
||||
kept_as_is = fast_forward(ref)
|
||||
plan = planned(text_of(root / "todo.md"))
|
||||
item = plan if isinstance(plan, str) else item_facts(root, ref, plan)
|
||||
return render(Gathered(ref, note, kept_as_is, release(ref), item))
|
||||
cut = release(ref, root)
|
||||
plan = planned(default_branch_todo(str(root), timeout), cut, ref)
|
||||
item = plan if isinstance(plan, NoItem) else item_facts(root, ref, plan, timeout)
|
||||
return render(Gathered(ref, note, cut, item))
|
||||
|
||||
|
||||
def fast_forward(default: str) -> str | None:
|
||||
"""Fast-forwards the checkout to default unless held_back says why not; None once it has."""
|
||||
held = held_back(default, git("symbolic-ref", "--quiet", "--short", "HEAD"), git("status", "--porcelain", "--untracked-files=no"), git("rev-list", "--count", f"HEAD..{default}"))
|
||||
if held is not None:
|
||||
return held
|
||||
merged = git("merge", "--ff-only", "--quiet", default)
|
||||
return merged.reason if isinstance(merged, Failure) else None
|
||||
|
||||
|
||||
def release(default: str) -> Release:
|
||||
def release(default: str, root: pathlib.Path) -> Release:
|
||||
tag = newest_release_tag(default)
|
||||
commits = git("rev-list", "--count", f"{tag}..{default}" if isinstance(tag, str) else default)
|
||||
return Release(tag, file_at(default, "CHANGELOG.md"), commits if isinstance(commits, Failure) else int(commits))
|
||||
commits = tag if isinstance(tag, Failure) else commits_since(tag, default)
|
||||
return Release(tag, file_at(default, "CHANGELOG.md", str(root)), commits)
|
||||
|
||||
|
||||
def item_facts(root: pathlib.Path, default: str, plan: Plan) -> ItemFacts:
|
||||
def item_facts(root: pathlib.Path, default: str, plan: Plan, timeout: float) -> ItemFacts:
|
||||
worktree = item_worktree(root, plan.branch, default)
|
||||
made = worktree.path if isinstance(worktree, Worktree) else None
|
||||
return ItemFacts(plan, worktree, pull_listing(root, plan.branch), named_files(plan.text, made or root), tools_run(plan.first.id, made) if made else [])
|
||||
path = worktree.path if isinstance(worktree, Worktree) else None
|
||||
return ItemFacts(plan, worktree, pull_listing(root, plan.branch, timeout), named_files(plan.text, path or root), tools_run(plan.first.id, path, timeout) if path else [])
|
||||
|
||||
|
||||
def item_worktree(root: pathlib.Path, branch: str, default: str) -> Worktree | Failure:
|
||||
"""The worktree on branch, made where none is; a Failure where git cannot make it, or its path holds something else."""
|
||||
held = git("worktree", "list", "--porcelain")
|
||||
if isinstance(held, Failure):
|
||||
return held
|
||||
if (path := worktree_on(held, branch)) is not None:
|
||||
return Worktree(path, f"on the branch {branch}, which it already held.")
|
||||
listing = git("worktree", "list", "--porcelain")
|
||||
if isinstance(listing, Failure):
|
||||
return listing
|
||||
if (path := worktree_on(listing, branch)) is not None:
|
||||
return Worktree(path, f"continued, on the branch {branch}.")
|
||||
path = root.parent / f"{root.name}-{branch}"
|
||||
if path.exists():
|
||||
return Failure(f"none made, since {path} exists and holds no worktree on the branch {branch}")
|
||||
@@ -113,12 +129,12 @@ def item_worktree(root: pathlib.Path, branch: str, default: str) -> Worktree | F
|
||||
return Failure(f"none made: {added.reason}") if isinstance(added, Failure) else Worktree(path, how)
|
||||
|
||||
|
||||
def pull_listing(root: pathlib.Path, branch: str) -> tuple[Forge, Ran | Failure] | None:
|
||||
def pull_listing(root: pathlib.Path, branch: str, timeout: float) -> tuple[Forge, Ran | Failure] | None:
|
||||
url = git("remote", "get-url", "origin")
|
||||
if isinstance(url, Failure):
|
||||
return None
|
||||
forge = forge_of(url)
|
||||
return forge, ran(pulls_command(forge, branch), root)
|
||||
return forge, ran(pulls_command(forge, branch), root, timeout)
|
||||
|
||||
|
||||
def named_files(text: str, base: pathlib.Path) -> list[NamedPath] | Failure:
|
||||
@@ -132,28 +148,20 @@ def line_count(path: pathlib.Path) -> int | Failure | None:
|
||||
"""A file's lines, or why they cannot be read; None for a folder."""
|
||||
if not path.is_file():
|
||||
return None
|
||||
text = text_of(path)
|
||||
return text if isinstance(text, Failure) else len(text.splitlines())
|
||||
|
||||
|
||||
def tools_run(item_id: int, worktree: pathlib.Path) -> list[tuple[str, Ran | Failure]]:
|
||||
commands = [[part.format(item=item_id) for part in tool] for tool in TOOLS]
|
||||
return [(" ".join(command), ran([sys.executable, str(BIN / command[0]), *command[1:]], worktree)) for command in commands]
|
||||
|
||||
|
||||
def text_of(path: pathlib.Path) -> str | Failure:
|
||||
"""A file's text; empty where there is no such file."""
|
||||
try:
|
||||
return path.read_text(encoding="utf-8")
|
||||
except FileNotFoundError:
|
||||
return ""
|
||||
return len(path.read_text(encoding="utf-8").splitlines())
|
||||
except (OSError, UnicodeDecodeError) as failure:
|
||||
return Failure(str(failure))
|
||||
|
||||
|
||||
def ran(command: list[str], cwd: pathlib.Path) -> Ran | Failure:
|
||||
def tools_run(item_id: int, worktree: pathlib.Path, timeout: float) -> list[tuple[str, Ran | Failure]]:
|
||||
commands = [[part.format(item=item_id) for part in tool] for tool in TOOLS]
|
||||
return [(" ".join(command), ran([sys.executable, str(BIN / command[0]), *command[1:]], worktree, timeout)) for command in commands]
|
||||
|
||||
|
||||
def ran(command: list[str], cwd: pathlib.Path, timeout: float) -> Ran | Failure:
|
||||
try:
|
||||
done = subprocess.run(command, capture_output=True, cwd=cwd, stdin=subprocess.DEVNULL, text=True, timeout=TIMEOUT)
|
||||
done = subprocess.run(command, capture_output=True, cwd=cwd, encoding="utf-8", errors="replace", stdin=subprocess.DEVNULL, timeout=timeout)
|
||||
except (OSError, subprocess.TimeoutExpired) as failure:
|
||||
return Failure(f"could not finish: {failure}")
|
||||
return Ran(done.returncode, done.stdout, done.stderr)
|
||||
|
||||
Reference in New Issue
Block a user