Remove a merged chunk's worktree and branch and fast-forward the main checkout with chunk-pr clean
CI / tests (pull_request) Successful in 2m13s
CI / tests (pull_request) Successful in 2m13s
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
## Unreleased
|
||||
|
||||
- `chunk-pr clean` removes a merged chunk's worktree and branch and fast-forwards the main checkout, which it leaves as it was, saying why, while that is dirty or on another branch.
|
||||
- `review-replay` now replays each round at the commit with the newest author time at or before the round's time, and takes the clone's base the same way by the time the chunk began. It took the first such commit in log order, so a commit authored early but merged late, such as a dependency bump, could win over the commits the round reviewed.
|
||||
- The shared goal every project pursues, code health, now asks that code stay easy to read, change safely and debug, tomorrow and after seven years of development.
|
||||
- `rank_todo.py` now lets an item that adds a test, a gate leg or a probe name the maintain goal it guards, by how much more confident the project becomes that the goal holds, 1 to 10. Each point is worth 0.5.
|
||||
|
||||
@@ -95,7 +95,7 @@ The rules only reviewers apply, for quality, comments, READMEs and prose, live i
|
||||
- Branch for a ticket: exactly the ticket ID, nothing more. `ABC-123`, never `ABC-123-some-description`.
|
||||
- Branch for a `todo.md` item without a ticket: `item-<ID>`, in the worktree `<checkout>-item-<ID>` beside the checkout, as `chunk-preflight` makes them.
|
||||
- Use `gh` on GitHub remotes and `tea` on Gitea remotes (e.g. `gitea.larvit.se`) — same command shape: `tea pr list|create`, `tea issue`, `tea release`, `tea api`. Everything in this section applies to both.
|
||||
- On GitHub and Gitea, open a chunk's pull request as a draft, mark it ready and merge it with `~/.claude/bin/chunk-pr`, which picks `gh` or `tea` from the remote; `chunk-pr --help` says how.
|
||||
- On GitHub and Gitea, open a chunk's pull request as a draft, mark it ready and merge it with `~/.claude/bin/chunk-pr`, which picks `gh` or `tea` from the remote; `chunk-pr --help` says how. Once it has merged, run `chunk-pr clean` in the chunk's worktree: it removes the worktree and the branch and fast-forwards the main checkout.
|
||||
|
||||
## Handoffs
|
||||
- Write no handoff document. The repo holds everything the next session needs: the plan, the decisions, and every script, sketch or measurement those depend on. A file outside the repo is unreachable from another machine's checkout.
|
||||
|
||||
@@ -93,7 +93,7 @@ It renames `## Unreleased` in `CHANGELOG.md` to the version and merges that thro
|
||||
|
||||
Install puts only `loop-claude` and `loop-inbox` on `PATH`; call the others by their path, such as `~/.claude/bin/metric-bench`, from the project's checkout. Those under `~/.claude` are the release's; test a change to one from `~/code/claude-config/bin`.
|
||||
|
||||
- `chunk-pr open|ready|merge` opens a chunk's pull request as a draft, marks it ready and merges it, with `gh` on a GitHub origin and `tea` elsewhere; `chunk-pr --help` states the steps.
|
||||
- `chunk-pr open|ready|merge|clean` opens a chunk's pull request as a draft, marks it ready and merges it, with `gh` on a GitHub origin and `tea` elsewhere; once it is merged, `clean` removes the chunk's worktree and branch and fast-forwards the main checkout. `chunk-pr --help` states the steps.
|
||||
- `chunk-preflight` finds the mechanical facts a chunk starts from, and with `--worktree` makes its worktree; `loop-claude` runs it with `--worktree` before it routes each chunk, and `chunk-preflight --help` lists the facts.
|
||||
- `command-patterns [<version>]` prints a table of the shell command patterns a release's archived chunks ran, such as `tea pr create`, for an audit of the release; `command-patterns --help` states its columns.
|
||||
- `deploy-release [--cut] [<checkout>]` moves `~/.claude`, or the checkout named, to the newest release, as Update says; with `--cut`, run in the development clone, it first cuts the next release, as Release says.
|
||||
|
||||
+59
-2
@@ -1,8 +1,9 @@
|
||||
"""Open the chunk's pull request as a draft, mark it ready, or merge it, with gh where origin is on GitHub and tea elsewhere.
|
||||
"""Open the chunk's pull request as a draft, mark it ready, or merge it, with gh where origin is on GitHub and tea elsewhere; then clean up after it.
|
||||
|
||||
usage: chunk-pr open <title> [<body>]
|
||||
chunk-pr ready
|
||||
chunk-pr merge --gate-passed
|
||||
chunk-pr clean
|
||||
|
||||
Run it in the chunk's worktree, on the chunk's branch. ready and merge act on the branch's open
|
||||
pull request only while its head is the commit checked out, so push before you run them.
|
||||
@@ -18,6 +19,11 @@ merge merges the pull request, and only while its head is still the commit ch
|
||||
a merge commit, fast-forward only, a rebase followed by a merge commit, a rebase, a squash.
|
||||
GitHub offers only the merge commit, the rebase and the squash. --gate-passed says the
|
||||
merge gate passed on that commit; without it, chunk-pr refuses.
|
||||
clean fetches origin and, once origin's default branch holds the commit checked out, removes
|
||||
the chunk's worktree and its branch, then fast-forwards the main checkout to origin's
|
||||
default branch. It leaves the main checkout as it was where that has uncommitted or
|
||||
untracked files or another branch checked out, and fails saying so: a dirty checkout is
|
||||
for its owner to look at. A worktree with uncommitted changes git keeps, branch and all.
|
||||
|
||||
It prints what it did, exit 0, or why it failed, exit 1.
|
||||
"""
|
||||
@@ -30,7 +36,7 @@ from chunk_preflight.gathered import Forge
|
||||
from chunk_preflight.choices import forge_of
|
||||
from deploy_release.forge import gitea, open_pull, Run, runner
|
||||
|
||||
USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr ready | chunk-pr merge --gate-passed"
|
||||
USAGE = "usage: chunk-pr open <title> [<body>] | chunk-pr ready | chunk-pr merge --gate-passed | chunk-pr clean"
|
||||
|
||||
# Each forge's merge methods in the order merge prefers them, as the repository setting that allows the method and the method's name in the forge's merge call. A merge commit and a fast-forward keep the branch's commits as they are; a rebase or a squash rewrites them.
|
||||
METHODS: dict[Forge, tuple[tuple[str, str], ...]] = {
|
||||
@@ -64,6 +70,8 @@ def step(argv: list[str], run: Run) -> str | Failure:
|
||||
return merged(run)
|
||||
case ["merge"]:
|
||||
return Failure("merge only once the gate has passed, and say so with --gate-passed")
|
||||
case ["clean"]:
|
||||
return cleaned(run)
|
||||
return Failure(USAGE)
|
||||
|
||||
|
||||
@@ -156,3 +164,52 @@ def merge_method(forge: Forge, run: Run) -> str | Failure:
|
||||
return settings
|
||||
allowed = next((method for setting, method in METHODS[forge] if isinstance(settings, dict) and settings.get(setting) is True), None)
|
||||
return allowed if allowed is not None else Failure(f"the repository allows none of the merge methods chunk-pr knows: {', '.join(method for _, method in METHODS[forge])}")
|
||||
|
||||
|
||||
def cleaned(run: Run) -> str | Failure:
|
||||
found = checkout(run)
|
||||
if isinstance(found, Failure):
|
||||
return found
|
||||
_, branch, head = found
|
||||
answers = [run(["git", *args]) for args in (["symbolic-ref", "--short", "refs/remotes/origin/HEAD"], ["worktree", "list", "--porcelain"], ["rev-parse", "--show-toplevel"])]
|
||||
failed = next((answer for answer in answers if isinstance(answer, Failure)), None)
|
||||
if failed is not None:
|
||||
return failed
|
||||
base, listed, worktree = (str(answer).strip() for answer in answers)
|
||||
# git lists the main checkout first.
|
||||
main = listed.splitlines()[0].removeprefix("worktree ")
|
||||
if worktree == main:
|
||||
return Failure(f"{main} is the main checkout; run clean in the chunk's worktree")
|
||||
fetched = run(["git", "fetch", "--quiet", "origin"])
|
||||
if isinstance(fetched, Failure):
|
||||
return fetched
|
||||
if isinstance(run(["git", "merge-base", "--is-ancestor", head, base]), Failure):
|
||||
return Failure(f"{base} doesn't hold {head[:12]}: {branch} is not merged, so its worktree and branch stay")
|
||||
at_main = runner(main)
|
||||
left = kept_as_it_was(at_main, base.removeprefix("origin/"))
|
||||
if left is None:
|
||||
forwarded = at_main(["git", "merge", "--quiet", "--ff-only", base])
|
||||
if isinstance(forwarded, Failure):
|
||||
return forwarded
|
||||
removed = at_main(["git", "worktree", "remove", worktree])
|
||||
if isinstance(removed, Failure):
|
||||
return removed
|
||||
# Not -d: the check above already found the branch merged, and -d would compare it with the main checkout's HEAD or a stale upstream instead.
|
||||
deleted = at_main(["git", "branch", "--quiet", "-D", branch])
|
||||
if isinstance(deleted, Failure):
|
||||
return deleted
|
||||
done = f"Removed worktree {worktree} and branch {branch}"
|
||||
if left is not None:
|
||||
return Failure(f"{done}, but left {main} as it was: {left}")
|
||||
return f"{done}, and fast-forwarded {main} to {base}."
|
||||
|
||||
|
||||
def kept_as_it_was(at_main: Run, default: str) -> str | None:
|
||||
"""Why the main checkout must not move, or None where it may fast-forward."""
|
||||
branch = at_main(["git", "symbolic-ref", "--quiet", "--short", "HEAD"])
|
||||
if isinstance(branch, Failure) or branch.strip() != default:
|
||||
return f"it has {'no branch' if isinstance(branch, Failure) else branch.strip()} checked out, not {default}"
|
||||
status = at_main(["git", "status", "--porcelain"])
|
||||
if isinstance(status, Failure):
|
||||
return status.reason
|
||||
return f"it has uncommitted or untracked files:\n{status.rstrip()}" if status.strip() else None
|
||||
|
||||
@@ -2,6 +2,7 @@ import json
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
@@ -172,6 +173,93 @@ class Merge(unittest.TestCase):
|
||||
self.assertEqual(checkout.acted(), [])
|
||||
|
||||
|
||||
class Clean(unittest.TestCase):
|
||||
"""A real origin whose default branch is main, its clone project, and the worktree project-item-7 on item-7, one commit ahead and pushed."""
|
||||
|
||||
def setUp(self):
|
||||
folder = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(folder.cleanup)
|
||||
root = pathlib.Path(folder.name)
|
||||
self.origin, self.project, self.worktree = root / "origin.git", root / "project", root / "project-item-7"
|
||||
self.git(root, "init", "--quiet", "--bare", "--initial-branch=main", str(self.origin))
|
||||
self.git(root, "clone", "--quiet", str(self.origin), str(self.project))
|
||||
self.commit(self.project, "first")
|
||||
self.git(self.project, "push", "--quiet", "origin", "main")
|
||||
self.git(self.project, "remote", "set-head", "origin", "--auto")
|
||||
self.git(self.project, "worktree", "add", "--quiet", "-b", "item-7", str(self.worktree), "origin/main")
|
||||
self.commit(self.worktree, "the chunk")
|
||||
self.git(self.worktree, "push", "--quiet", "--set-upstream", "origin", "item-7")
|
||||
|
||||
def git(self, folder: pathlib.Path, *args: str) -> str:
|
||||
return subprocess.run(["git", "-c", "user.name=Test", "-c", "user.email=test@example.org", *args], cwd=folder, check=True, capture_output=True, encoding="utf-8", env=isolated_env()).stdout.strip()
|
||||
|
||||
def commit(self, folder: pathlib.Path, message: str):
|
||||
self.git(folder, "commit", "--quiet", "--allow-empty", "-m", message)
|
||||
|
||||
def merge_on_origin(self):
|
||||
self.git(self.worktree, "push", "--quiet", "origin", "item-7:main")
|
||||
|
||||
def clean(self) -> subprocess.CompletedProcess:
|
||||
return subprocess.run([str(TOOL), "clean"], cwd=self.worktree, capture_output=True, encoding="utf-8", env=isolated_env())
|
||||
|
||||
def branches(self) -> list[str]:
|
||||
return self.git(self.project, "branch", "--format=%(refname:short)").splitlines()
|
||||
|
||||
def test_a_merged_chunk_loses_its_worktree_and_branch_and_the_checkout_fast_forwards(self):
|
||||
self.merge_on_origin()
|
||||
merged = self.git(self.worktree, "rev-parse", "HEAD")
|
||||
done = self.clean()
|
||||
self.assertEqual(done.returncode, 0, done.stderr)
|
||||
self.assertFalse(self.worktree.exists())
|
||||
self.assertEqual(self.branches(), ["main"])
|
||||
self.assertEqual(self.git(self.project, "rev-parse", "HEAD"), merged)
|
||||
self.assertIn("item-7", done.stdout)
|
||||
|
||||
def test_an_unmerged_chunk_keeps_everything(self):
|
||||
done = self.clean()
|
||||
self.assertEqual(done.returncode, 1)
|
||||
self.assertIn("not merged", done.stderr)
|
||||
self.assertTrue(self.worktree.exists())
|
||||
self.assertEqual(sorted(self.branches()), ["item-7", "main"])
|
||||
|
||||
def test_a_dirty_checkout_is_left_as_it_was_and_reported(self):
|
||||
self.merge_on_origin()
|
||||
before = self.git(self.project, "rev-parse", "HEAD")
|
||||
(self.project / "notes.txt").write_text("mine\n")
|
||||
done = self.clean()
|
||||
self.assertEqual(done.returncode, 1)
|
||||
self.assertIn("uncommitted", done.stderr)
|
||||
self.assertIn(str(self.project), done.stderr)
|
||||
self.assertEqual(self.git(self.project, "rev-parse", "HEAD"), before)
|
||||
self.assertEqual((self.project / "notes.txt").read_text(), "mine\n")
|
||||
self.assertFalse(self.worktree.exists())
|
||||
self.assertEqual(self.branches(), ["main"])
|
||||
|
||||
def test_a_checkout_on_another_branch_is_left_as_it_was_and_reported(self):
|
||||
self.merge_on_origin()
|
||||
self.git(self.project, "switch", "--quiet", "-c", "elsewhere")
|
||||
before = self.git(self.project, "rev-parse", "HEAD")
|
||||
done = self.clean()
|
||||
self.assertEqual(done.returncode, 1)
|
||||
self.assertIn("elsewhere", done.stderr)
|
||||
self.assertEqual(self.git(self.project, "rev-parse", "HEAD"), before)
|
||||
self.assertFalse(self.worktree.exists())
|
||||
|
||||
def test_a_worktree_with_uncommitted_changes_is_kept(self):
|
||||
self.merge_on_origin()
|
||||
(self.worktree / "draft.txt").write_text("unsaved\n")
|
||||
done = self.clean()
|
||||
self.assertEqual(done.returncode, 1)
|
||||
self.assertTrue((self.worktree / "draft.txt").exists())
|
||||
self.assertIn("item-7", self.branches())
|
||||
|
||||
def test_the_main_checkout_itself_is_refused(self):
|
||||
done = subprocess.run([str(TOOL), "clean"], cwd=self.project, capture_output=True, encoding="utf-8", env=isolated_env())
|
||||
self.assertEqual(done.returncode, 1)
|
||||
self.assertIn("worktree", done.stderr)
|
||||
self.assertTrue(self.worktree.exists())
|
||||
|
||||
|
||||
class Launcher(unittest.TestCase):
|
||||
def test_help_exits_0_and_a_wrong_step_exits_1(self):
|
||||
self.assertEqual(subprocess.run([str(TOOL), "--help"], capture_output=True, env=isolated_env()).returncode, 0)
|
||||
|
||||
@@ -1453,3 +1453,20 @@ Read most (opens and searches):
|
||||
- `bin/chunk_preflight/main.py` 2
|
||||
- `bin/release_plan/todo_table.py` 2
|
||||
- `docs/decisions.md` 2
|
||||
|
||||
## Reading cost, chunk db83bf6d8fd5, finished 2026-11-10T09:46:42Z
|
||||
|
||||
83308 new input tokens before the first commit.
|
||||
|
||||
Read most (opens and searches):
|
||||
|
||||
- `docs/decisions.md` 11
|
||||
- `AGENTS.md` 2
|
||||
- `CHANGELOG.md` 2
|
||||
- `README.md` 2
|
||||
- `bin/review_replay/main.py` 2
|
||||
- `ci.sh` 2
|
||||
- `bin/loop_claude/effects.py` 1
|
||||
- `bin/review-replay` 1
|
||||
- `bin/review_replay/rounds.py` 1
|
||||
- `bin/review_replay/test_review_replay.py` 1
|
||||
|
||||
@@ -195,7 +195,8 @@ Launch prose-reviewer with what the diff touched and every section this review c
|
||||
2. Run the comprehension check (§3) where it runs; it runs once. When `origin/<default branch>` moves after that, return to §4, and keep the ruling and scoring run already recorded.
|
||||
3. Aligned, every pass done and everything green → **merge** with `~/.claude/bin/chunk-pr merge --gate-passed`, which takes the first merge method the repository allows on its forge. A repo whose `AGENTS.md` names another method merges that way with the forge's CLI instead. A repo withholds this in its `AGENTS.md` → leave the PR ready and unmerged, and say which line withheld it.
|
||||
4. Merged → remove the chunk's items from `todo.md` per `~/.claude/CLAUDE.md` → "Working loop".
|
||||
5. Stop. The next chunk gets a fresh session.
|
||||
5. Run `~/.claude/bin/chunk-pr clean` in the chunk's worktree. It removes the worktree and the branch and fast-forwards the main checkout; a failure it prints, such as a dirty main checkout, goes in the summary.
|
||||
6. Stop. The next chunk gets a fresh session.
|
||||
|
||||
## 6. Wrap up
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||
| 277 | 0.7.0 | | question | **Map the size of the code an item names onto S, on a scale the human picks, and have `score-outcomes` print that S for the item being scored and for each finished item it lists.** | 3 | 2 | 2 | 5 | 4 | 1.1 by 4, 1.4 by 3 | 2.4 |
|
||||
| 1 | 0.7.0 | | question | **Say in README → Install how the transcript archive is backed up, once bender-infra backs up ai-sandbox's data volume.** | 1 | 2 | 1 | 2 | 1 | | 0.0 |
|
||||
| 281 | 0.7.0 | | | **Remove a merged chunk's worktree and branch and fast-forward the main checkout with one command.** | 3 | 2 | 3 | 6 | 3 | 1.4 by 2, 1.5 by 2 | 1.6 |
|
||||
| 290 | 0.7.0 | | defect | **Have `rule-hook overrides` read the branch's reflog when a file or folder shares the branch's name.** | 1 | 2 | 2 | 3 | 1 | 1.1 by 2 | 1.0 |
|
||||
| 286 | 0.7.0 | | | **Lower the reading cost of the files a chunk reads most before its first commit.** | 3 | 2 | 2 | 5 | 5 | 1.4 by 4 | 1.0 |
|
||||
| 287 | 0.8.0 | | | **Move the forge helpers `Forge`, `forge_of`, `runner`, `open_pull` and `gitea` into one module that `chunk_preflight`, `chunk_pr`, `deploy_release` and `larv_review` all import from, so no tool imports another tool's internals for them.** | 3 | 1 | 3 | 5 | 5 | code health by 3 | 3.4 |
|
||||
@@ -72,10 +71,6 @@ It lives under `~/.claude`, which `.gitignore` keeps from the remote, so a lost
|
||||
|
||||
The README line covers the bench's transcripts in `workflow-bench/logs/` too (goal 3).
|
||||
|
||||
### 281. Remove a merged chunk's worktree and branch and fast-forward the main checkout with one command.
|
||||
|
||||
Audit of release 0.2.0's chunks (item 162): 31 chunks ran `git worktree remove`, `git branch -d` and `git pull --ff-only` by hand, 56 calls and about 5k command tokens. The program refuses a dirty main checkout, as `CLAUDE.md` → "Working with the codebase" says, and reports it.
|
||||
|
||||
### 290. Have `rule-hook overrides` read the branch's reflog when a file or folder shares the branch's name.
|
||||
|
||||
`bin/rule_hook/override_log.py:98` runs `git reflog show --format=%H <branch>` without a `--` after the branch. When a tracked path has the branch's name, git stops with "ambiguous argument … both revision and filename", and `rule-hook overrides` exits saying it cannot tell which overrides are the chunk's. larv-review's Overrides pass then has no list to judge, so an override in such a chunk would go unreviewed. Found on 2026-11-09 in PR #456, whose branch and new folder were both `reviewer-merge-trials`. Fix by passing `--` after the branch, and test it with a branch named after a tracked folder.
|
||||
|
||||
Reference in New Issue
Block a user