diff --git a/.claude/commands/clean-branches.md b/.claude/commands/clean-branches.md index 05b5fba..3df4605 100644 --- a/.claude/commands/clean-branches.md +++ b/.claude/commands/clean-branches.md @@ -1,8 +1,8 @@ --- -description: Find and delete local branches whose squash-merge already landed on the default branch. Safe — only deletes branches whose subject matches a merged commit. +description: Find and delete local branches already landed on the default branch. Safe — asks GitHub which branch each merged PR came from, falls back to a subject match, and reports anything it cannot prove. --- -The user wants to clean up local branches that have been squash-merged into the default branch. Squash-merge means `git branch --merged` won't recognize them (the merged commit has a different SHA than the branch tip), so the standard cleanup needs a content-aware match. +The user wants to clean up local branches whose work has already landed on the default branch. Squash-merge means `git branch --merged` won't recognize them (the merged commit has a different SHA than the branch tip), so the cleanup needs a stronger match. ## What to do @@ -24,31 +24,66 @@ The user wants to clean up local branches that have been squash-merged into the | grep -vE '^(main|master|working)$' ``` -4. **For each candidate, decide if it's safely deletable.** A branch is safely deletable if EITHER: - - **Its head commit's subject matches an already-landed commit on the default branch** (squash-merge case). Read the branch's head subject (`git log -1 --format='%s' `) and grep `git log "origin/$DEFAULT" --format='%s'` for that exact line. The match is exact — squash-merge preserves the title. - - **`git branch --merged "$DEFAULT"` lists it** (regular merge case, rare in this repo). +4. **For each candidate, try three checks in order.** The first hit wins; stop there. - Branches that match NEITHER are surfaced as "ambiguous, leaving alone" — don't delete. + **A — ask GitHub which branch the PR came from (authoritative, use first):** + ```bash + gh pr list --state merged --head "" --json number,title --jq '.[0]' + ``` + A merged PR whose head ref IS this branch proves the branch landed, **whatever the PR was titled**. This is the check that matters: a branch's head *commit subject* and its *PR title* are different strings, and they only coincide when the branch had exactly one commit. Any multi-commit branch whose PR was retitled on the way in fails check B and passes this one. + + **B — head subject matches a landed commit (offline fallback):** + ```bash + git log -1 --format='%s' # the subject + git log "origin/$DEFAULT" --format='%s' | grep -Fxq "" + ``` + Use when `gh` is unavailable/unauthenticated, or the PR record is gone. Exact match — a squash-merge preserves the PR title, so this fires when the branch's last commit *was* what the PR was called. + + **C — `git branch --merged "$DEFAULT"` lists it** (regular merge case, rare). + + Anything matching none of the three is **ambiguous — never delete it.** -5. **Build the deletion plan** as a list: +5. **For each ambiguous branch, gather evidence so the human can judge in one read.** This is the important half: the checks above are *incomplete by construction* (see below), so the report has to carry enough to settle the question, not just say "dunno". + ```bash + git branch -r --contains # empty => never pushed + git rev-list --count ..origin/$DEFAULT # how far behind + git log -1 --format='%ad' --date=short # how stale + git cherry origin/$DEFAULT | grep -c '^-' # commits already on the default, by patch-id + ``` + Report each as: never-pushed?, N commits behind, head date, and how many of its commits are already on the default branch by patch-id. + +6. **Show the plan.** No y/n prompt — the user has standing authorization to auto-clean any branch a check above proved landed. Print the plan, then delete straight away. Ambiguous branches are always skipped. ``` safe to delete (N): - - feat/x: matches squash-merge "" on the default branch - - fix/y: ... + - feat/x: merged PR #123 "" (head-ref match) + - fix/y: matches squash-merge "" on the default branch ambiguous (M, leaving alone): - - feat/wip-z: head subject not found on the default branch; might be unmerged work + - feat/wip-z: never pushed · 107 behind · head 2026-06-30 · 0/11 commits on the default branch ``` -6. **Show the plan to the user.** No y/n prompt — the user has standing authorization to auto-clean any branch whose head subject matches a squash-merged commit on the default branch. Print the plan, then proceed straight to deletion. (Ambiguous branches are still skipped; only the "safe to delete" list is touched.) +7. **Delete each safe-to-delete branch with `git branch -D `** (capital D — squash-merged branches aren't "fully merged" so `-d` refuses them). Tail the output of each. + +8. **Final state.** `git branch | head -10` and report. Leave `working` alone either way. + +## Why the checks cannot be complete (read before "improving" this) + +A local branch can be genuinely, fully merged and still fail **every** mechanical check. Real case that motivated this file (coreciv `trade-caravans-b4`, 2026-07-15): + +- Its work shipped as `Trade caravans (B4): spatial, contestable trade routes (#42)` — every symbol and doc it introduced is on the default branch. +- **Check A missed it**: the PR's head ref was `trade-caravans-b4-v2`. The local branch was an earlier attempt; the work was redone on a `-v2` branch and shipped from there. +- **Check B missed it**: its head subject was its 11th commit (`feat(engine): AI trades via caravans instead of instant routes`), not the PR title. +- **Patch-id missed it**: `git cherry` marked all 11 commits `+` (not present), because the rework changed the patches. +- **A SHA search missed it**: the commits were **never pushed**, so GitHub had never seen them. -7. **Delete each safe-to-delete branch with `git branch -D `** (capital D — these are squash-merged so `-d` would refuse them as "not fully merged"). Tail the output of each. +Proving it merged took *content* judgment — are the symbols on the default branch, are the docs there (they were, under **renamed** paths). No heuristic gets there. -8. **Final state.** Run `git branch | head -10` and report. If `working` exists, leave it; if it didn't before, leave it (you created it in step 2 to enable cleanup). +So: **do not chase completeness.** When a branch is ambiguous, the job is to hand the user the evidence in step 5 and let them decide. A false "safe to delete" destroys work; a false "ambiguous" costs one look. ## Edge cases - **No candidates at all:** report "Nothing to clean — already tidy" and stop. -- **Branch checked out by another worktree:** `git branch -D` will fail with `used by worktree at ''`. Surface that branch in the "ambiguous" list, don't delete. +- **`gh` unavailable / not authenticated / no remote:** skip check A silently, fall through to B and C. Say so in the summary so a thin result isn't mistaken for a clean one. +- **Branch checked out by another worktree:** `git branch -D` fails with `used by worktree at ''`. Surface in "ambiguous", don't delete. - **You're in a worktree (not the main checkout):** the user does this often. `working` lives in the worktree fine; just make sure step 2 succeeds. ## Output diff --git a/.claude/commands/prime.md b/.claude/commands/prime.md index 31d0c04..0e33e36 100644 --- a/.claude/commands/prime.md +++ b/.claude/commands/prime.md @@ -4,6 +4,14 @@ description: Read the project's canonical docs and get primed to work — load s The user is starting a working session and wants you primed on the current state of the project before they hand you a task — so you don't burn context re-reading mid-task. +## First, check the checkout is current + +A stale checkout gives stale numbers. Before you read anything: + +1. Run `git fetch origin` (skip silently if there is no remote or no network). +2. Find the default branch (`git symbolic-ref --short refs/remotes/origin/HEAD`) and count the commits `HEAD` is behind it (`git rev-list --count HEAD..origin/`). +3. If `HEAD` is behind, say so first in the readback: how many commits, and the version or build the checkout holds (from `pubspec.yaml` or the repo's equivalent). Do not pull or switch branches; the user decides. + ## What to read This command is project-agnostic. Read what exists, in this order; skip silently what doesn't. @@ -19,7 +27,7 @@ This command is project-agnostic. Read what exists, in this order; skip silently - `ROADMAP.md`, `README.md` — if the repo leads with these instead of a `docs/` tree 3. **Index files, not leaves.** If `docs/` has per-topic subtrees (e.g. `docs/rulesets/`, `docs/action_types/`), read only their `README.md` index. Open individual leaf files later, when a task makes one relevant — never the whole tree up front. -**Don't** re-read the fresh-machine bootstrap (`docs/claude/`) — it's already mirrored in `CLAUDE.md`. +**Don't** front-load the per-area detail files `CLAUDE.md`'s pointer table names (here, `docs/claude/`) — open the matching one when a task lands in that area. To discover the doc surface cheaply, prefer one Glob (`docs/**/*.md`) over reading blindly. @@ -30,6 +38,7 @@ Tight readback so the user can confirm you loaded the right context. Under 200 w - **Current phase / focus** — which plan phase is active and the next item on it - **Open priorities** — top 2-3 items that look like the next logical work - **Recent landings** — anything that landed recently, so you don't propose to redo it +- **Checkout** — current, or N commits behind the default branch (name the build it holds) - **Any drift you spotted** — places where the status docs disagree, or reference code that may have moved End with: **"Ready. What are we working on?"**