From a0e143daf6a862bd7b24df69c84a2298c6bf11c3 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 04:46:59 +0100 Subject: [PATCH 1/4] Preserve ignored user files during merged worktree cleanup Refuse local cleanup if Git reports ignored files other than task text and receipts. This keeps local notes and settings where the developer left them. Co-Authored-By: Codex --- docs/ARCHITECTURE.md | 2 +- src/loopzero/worktree.py | 13 ++++++++++++- tests/test_cli.py | 2 +- tests/test_worktree.py | 14 +++++++------- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index c474e6e7..9732d25e 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -268,7 +268,7 @@ Git configuration so URL rewrites cannot change its destination. After confirmed remote deletion or absence, cleanup deletes the matching `origin/lz/` tracking ref only if it still points to the merged head. An already-merged PR can resume cleanup from its task worktree. Local removal -requires a clean worktree at the merged PR head. +requires a clean worktree at the merged PR head with no ignored user files. Hosted eligibility trusts the configured publisher's projection and live identity, state, head and latest ordering. It cannot recompute private raw or defeat a coherent forgery by a trusted publisher/editor; native claims do not attest the diff --git a/src/loopzero/worktree.py b/src/loopzero/worktree.py index 008a7cde..0c3909fc 100644 --- a/src/loopzero/worktree.py +++ b/src/loopzero/worktree.py @@ -260,12 +260,23 @@ def merges_since(wt: Path, sha: str) -> list[str]: def cleanup(wt: Path) -> None: - """Remove the worktree and its branch. Refuses when the worktree is dirty.""" + """Remove the worktree and branch if there are no local user files.""" wt = Path(wt).absolute() if not wt.exists(): raise WorktreeError(f"no such worktree: {wt}") if is_dirty(wt): raise WorktreeError(f"worktree is dirty, refusing to remove: {wt}") + ignored = _git( + wt, "ls-files", "--others", "--ignored", "--exclude-standard", "-z", preserve_newlines=True + ) + for path in ignored.split("\0")[:-1]: + if ( + (path in {".loopzero/task.md", ".loopzero/checks.json"} + or re.fullmatch(r"\.loopzero/review-[0-9a-f]{12}-(primary|delta)\.json", path)) + and not (wt / path).is_symlink() + ): + continue + raise WorktreeError(f"worktree contains ignored file {path!r}, refusing to remove: {wt}") name, gd = branch(wt), (wt / _git(wt, "rev-parse", "--git-common-dir")).resolve() _git(gd, f"--git-dir={gd}", "worktree", "remove", str(wt)) # never the primary checkout try: diff --git a/tests/test_cli.py b/tests/test_cli.py index fda149c0..9c1dd5f8 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4715,7 +4715,7 @@ def interrupt(staged, dest): def test_already_merged_finishes_partial_cleanup(wt, repo, gh, capsys, monkeypatch, head): - (wt / ".loopzero" / "review-existing.json").write_text("{broken") + cli._review_file(wt, head, "primary").write_text("{broken") arm_pr(gh, head, isDraft=False, state="MERGED") gh.append(f"api repos/{REPO}/pulls/7", merge_json(state="MERGED", head=head, sha="c" * 40)) ref = f"api repos/{REPO}/git/ref/heads/lz/t1" diff --git a/tests/test_worktree.py b/tests/test_worktree.py index 230adc02..5bb9f003 100644 --- a/tests/test_worktree.py +++ b/tests/test_worktree.py @@ -133,13 +133,11 @@ def interrupt_after_add(cwd, *args, **kwargs): def test_start_excludes_worktrees_and_task_dir(repo: Path) -> None: exclude = repo / ".git" / "info" / "exclude" - exclude.parent.mkdir(parents=True, exist_ok=True) exclude.write_text("*.log") # no trailing newline on purpose old_inode = exclude.stat().st_ino wt = worktree.start(repo, "t1", "main") assert exclude.stat().st_ino != old_inode - lines = exclude.read_text().splitlines() - assert lines == ["*.log", ".worktrees/", ".loopzero/"] + assert exclude.read_text().splitlines() == ["*.log", ".worktrees/", ".loopzero/"] assert not worktree.is_dirty(wt), "fresh worktree with task.md is clean" assert git(repo, "status", "--porcelain") == "" installed = exclude.stat() @@ -238,12 +236,14 @@ def test_cleanup_removes_worktree_and_branch(repo: Path, monkeypatch) -> None: assert not wt.exists() and not git(repo, "branch", "--list", "lz/done") and "done" not in git(repo, "worktree", "list") -def test_cleanup_refuses_dirty(repo: Path) -> None: +@pytest.mark.parametrize("file", ["scratch.txt", "local.env", ".loopzero/user-notes.md"]) +def test_cleanup_preserves_user_files(repo: Path, file: str) -> None: + (repo / ".git/info/exclude").write_text("local.env\n") wt = worktree.start(repo, "dirty", "main") - (wt / "scratch.txt").write_text("x") - with pytest.raises(worktree.WorktreeError, match="dirty"): + (wt / file).write_text("x") + with pytest.raises(worktree.WorktreeError, match="worktree is dirty" if file == "scratch.txt" else "ignored file"): worktree.cleanup(wt) - assert wt.exists() and "lz/dirty" in git(repo, "branch", "--list", "lz/dirty") + assert (wt / file).read_text() == "x" and "lz/dirty" in git(repo, "branch", "--list", "lz/dirty") def test_cleanup_missing_worktree(repo: Path) -> None: From 5355953fa8a292ab7d6ba33afde0ae899573e730 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 07:53:02 +0100 Subject: [PATCH 2/4] Resume delivery from the current PR stage Architecture-Review: unchanged; the documented behavior matches the current CLI without changing the delivery interface. Co-Authored-By: Codex --- core/CONTRACT.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/CONTRACT.md b/core/CONTRACT.md index 01f99ca4..67fbaa61 100644 --- a/core/CONTRACT.md +++ b/core/CONTRACT.md @@ -43,10 +43,10 @@ own delivery state; `.loopzero/` holds task text and local receipts. Merge is authorized by default under [merge authority](#merge-authority). -On every start/resume, run `loopzero status`, then rerun the last `--wait` -command if any; both are idempotent. Status derives the branch's stage from -Git, GitHub and the check receipt. Never end a turn waiting for notice of an -event that may already have happened; notifications never replace the waiter. +On start/resume, run `loopzero status` if the worktree exists. Resume a pending +`--wait`; after queue ejection, read the departure reason before requeueing. +If cleanup removed the worktree, verify the merge in GitHub. Notifications +never replace the waiter. Before yielding, record head, last exit code, awaited event and next command in one PR comment per owner. Post on first yield; update the existing comment ID with the full replacement Markdown from `body_file` on later yields: From a6871b96790c2ddf60adfaf1292e9273a07b29de Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 07:53:02 +0100 Subject: [PATCH 3/4] Clarify trusted and delegated check execution Architecture-Review: unchanged; the documented behavior matches the current CLI without changing the delivery interface. Co-Authored-By: Codex --- README.md | 2 +- docs/ARCHITECTURE.md | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4a50355e..cc1cfd90 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ ownership and the contributor routine that keeps documentation current. | Command | What it does | | --- | --- | | `loopzero start ` | Create a worktree and branch from base; write the Context/Problem/Goal task template. | -| `loopzero check` | Run `workflow.toml` checks in a bwrap sandbox; exit code is the verdict. | +| `loopzero check` | Run trusted-base checks with local bwrap or the configured runner. | | `loopzero pr` | Push and open a draft PR from the task file, with its Validation section rendered. | | `loopzero review` | Run one Claude or Codex review on the exact head; post it and append its summary under Review. Exits 0 on approval, 5 on changes requested. | | `loopzero ready [--wait[=SECONDS]]` | Verify head, findings and CI; optionally wait for required checks on that head. | diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 1d67cb81..e45b53ee 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -157,7 +157,7 @@ For a Codex executable installed inside protected native state, the implicit runtime mount exposes only the resolved executable and its existing sibling `codex-code-mode-host`, read-only. Neighboring history and configuration remain hidden; helper aliases into protected state and explicit state mounts fail closed. -Check, review, ready, merge, and status use the workflow at one remote-tracking +Check, pr, review, ready, merge, and status use the workflow at one remote-tracking delivery revision selected by the workflow at Git's local `origin/HEAD`. Missing trusted refs or workflows block these commands. An explicit `--config` supplies validated policy for bootstrap or operator overrides. @@ -497,8 +497,8 @@ command, version, sandbox configuration and the resolved base SHA. A stage runs fresh when a matched path cannot be identified as a readable regular file. The receipt fingerprint also binds the base SHA when reuse is configured, so advancing the base invalidates a pass at the same task head. -If the remote ref is absent, checks use the task's recorded base. Without -either base, local stages run fresh. +With explicit `--config`, reuse can fall back to the recorded task base if the +remote ref is absent. Without either base, local stages run fresh. The `"*"` entry adds shared inputs to listed commands. Unlisted commands reuse only when `"*"` includes `"."` (the whole tree); a partial or empty `"*"` leaves them unkeyed. Consumers own complete input declarations. From 96bf7c0631276ed8d5387a7323f2615637e76eac Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 07:57:39 +0100 Subject: [PATCH 4/4] Preserve the pending-wait and notification rules Architecture-Review: unchanged; resume guidance preserves the existing waiter and Mergify recovery behavior. Co-Authored-By: Codex --- core/CONTRACT.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/core/CONTRACT.md b/core/CONTRACT.md index 67fbaa61..d385fb8d 100644 --- a/core/CONTRACT.md +++ b/core/CONTRACT.md @@ -43,10 +43,11 @@ own delivery state; `.loopzero/` holds task text and local receipts. Merge is authorized by default under [merge authority](#merge-authority). -On start/resume, run `loopzero status` if the worktree exists. Resume a pending -`--wait`; after queue ejection, read the departure reason before requeueing. -If cleanup removed the worktree, verify the merge in GitHub. Notifications -never replace the waiter. +On start/resume, use `loopzero status` in an existing worktree to read Git, GitHub +and the receipt. Rerun the last `--wait` if pending. After ejection, read the +reason and follow [Mergify recovery](../SETUP.md#mergify). If cleanup removed the +worktree, verify the merge in GitHub. Never yield waiting for a notice that +may already have fired. Notifications never replace the waiter. Before yielding, record head, last exit code, awaited event and next command in one PR comment per owner. Post on first yield; update the existing comment ID with the full replacement Markdown from `body_file` on later yields: