diff --git a/README.md b/README.md index f2cbf0fe..401c539e 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/core/CONTRACT.md b/core/CONTRACT.md index 878373f8..3dac1a17 100644 --- a/core/CONTRACT.md +++ b/core/CONTRACT.md @@ -44,10 +44,11 @@ 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, 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: diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 99bb7af6..f5acbb17 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -161,7 +161,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. @@ -282,7 +282,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 @@ -510,8 +510,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. diff --git a/src/loopzero/worktree.py b/src/loopzero/worktree.py index 720e78b8..7a994ac3 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 c2beef91..53b0e0ba 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4755,7 +4755,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 d4c30478..8b6c5fc2 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() @@ -269,12 +267,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: