Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/skills/work-issue/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ Before publication, inspect the issue and links for its lifecycle blueprint and
include required completion in the closing delivery. A child does not own its parent's blueprint.
Recheck collisions, live acceptance, prior delivery and ownership before
publication and merge. Reconcile changes before proceeding.
On approval, run readiness at the approved head and defer optional changes.
Defer optional changes after approval. With a current passing receipt, follow
[contract Step 5](../../CONTRACT.md#the-six-steps) at the approved head before
investigating missing or skipped draft checks. Use its scheduling guidance if
checks remain missing after activation.
Before queueing, check the candidate merged with the current base against
every repository size cap, including the combined effect of any batch.
Verify the replacement is installed and works before retiring a deployment workaround.
Expand Down
3 changes: 2 additions & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ Git configuration so URL rewrites cannot change its destination.
After confirmed remote deletion or absence, cleanup deletes the matching
`origin/lz/<slug>` 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 with no ignored user files. A Git ref lock keeps the
allows ignored files in a clean worktree and refuses to remove nested worktrees.
Git removal stays non-forced. A Git ref lock keeps the
worktree at the merged PR head during removal. Cleanup then deletes the branch
only if it still points at that head.
Hosted eligibility trusts the configured publisher's projection and live identity,
Expand Down
30 changes: 19 additions & 11 deletions src/loopzero/worktree.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,19 +383,27 @@ def _lock_verified_ref(wt: Path, ref: str, expected_head: str) -> Iterator[None]
transaction.terminate()


def _require_no_ignored_user_files(wt: Path) -> None:
ignored = _git(
wt, "ls-files", "--others", "--ignored", "--exclude-standard", "-z", preserve_newlines=True
)
for path in ignored.split("\0")[:-1]:
if not ((path in {".loopzero/task.md", ".loopzero/checks.json"}
or re.fullmatch(r"\.loopzero/review(?:er-output)?-[0-9a-f]{12}-(primary|delta)\.json", path))
and not (wt / path).is_symlink()):
raise WorktreeError(f"worktree contains ignored file {path!r}, refusing to remove: {wt}")
def _require_no_nested_worktree(wt: Path, git_dir: Path) -> None:
root = wt.resolve()
records = _git(git_dir, f"--git-dir={git_dir}", "worktree", "list", "--porcelain", "-z",
preserve_whitespace=True)
for field in records.split("\0"):
if field.startswith("worktree "):
path = Path(field.removeprefix("worktree "))
if path != root and path.is_relative_to(root):
raise WorktreeError(f"nested worktree {path}, refusing to remove: {wt}")

def fail_scan(exc: OSError) -> None:
raise WorktreeError(f"could not inspect nested worktrees in {wt}: {exc}") from exc

# Other repositories' checkouts are absent from this Git registry.
for path, directories, files in root.walk(on_error=fail_scan):
if path != root and (".git" in files or ".git" in directories):
raise WorktreeError(f"nested Git worktree at {path}, refusing to remove: {wt}")


def cleanup(wt: Path, expected_head: str) -> None:
"""Remove a clean merged worktree and its branch if it has no local user files."""
"""Remove a clean verified worktree and its branch, preserving nested worktrees."""
wt = Path(wt).absolute()
if not wt.exists():
raise WorktreeError(f"no such worktree: {wt}")
Expand All @@ -409,7 +417,7 @@ def cleanup(wt: Path, expected_head: str) -> None:
raise WorktreeError("local branch changed during merge cleanup")
if is_dirty(wt):
raise WorktreeError(f"worktree is dirty, refusing to remove: {wt}")
_require_no_ignored_user_files(wt)
_require_no_nested_worktree(wt, gd)
_git(gd, f"--git-dir={gd}", "worktree", "remove", str(wt)) # never the primary checkout
worktrees = _git(gd, f"--git-dir={gd}", "worktree", "list", "--porcelain", "-z", preserve_whitespace=True)
if f"branch {ref}" in worktrees.split("\0"):
Expand Down
39 changes: 34 additions & 5 deletions tests/test_worktree.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,43 @@ 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")


@pytest.mark.parametrize("file", ["scratch.txt", "local.env", ".loopzero/user-notes.md"])
def test_cleanup_preserves_user_files(repo: Path, file: str) -> None:
def test_cleanup_removes_ignored_artifacts(repo: Path) -> None:
(repo / ".git/info/exclude").write_text(".venv/\n")
wt = worktree.start(repo, "cached", "main")
(wt / ".venv").mkdir()
(wt / ".venv/dependency").write_text("generated")
worktree.cleanup(wt, worktree.head(wt))
assert not wt.exists()
assert not git(repo, "branch", "--list", "lz/cached")


def test_cleanup_preserves_untracked_work_with_ignored_artifacts(repo: Path) -> None:
(repo / ".git/info/exclude").write_text("local.env\n")
wt = worktree.start(repo, "dirty", "main")
(wt / file).write_text("x")
with pytest.raises(worktree.WorktreeError, match="worktree is dirty" if file == "scratch.txt" else "ignored file"):
(wt / "local.env").write_text("ignored")
(wt / "scratch.txt").write_text("keep my work")
with pytest.raises(worktree.WorktreeError, match="worktree is dirty"):
worktree.cleanup(wt, worktree.head(wt))
assert (wt / "scratch.txt").read_text() == "keep my work"
assert (wt / "local.env").read_text() == "ignored"
assert "lz/dirty" in git(repo, "branch", "--list", "lz/dirty")


@pytest.mark.parametrize("kind", ["same repo", "other repo", "clone"])
def test_cleanup_preserves_ignored_nested_worktrees(repo: Path, tmp_path: Path, kind: str) -> None:
(repo / ".git/info/exclude").write_text("nested/\n")
wt = worktree.start(repo, "parent", "main")
nested = wt / "nested"
if kind == "clone":
git(repo, "clone", str(repo), str(nested))
else:
source = init_git_repo(tmp_path / "other") if kind == "other repo" else repo
git(source, "worktree", "add", "--detach", str(nested))
assert not worktree.is_dirty(wt)
with pytest.raises(worktree.WorktreeError, match=r"nested (Git )?worktree"):
worktree.cleanup(wt, worktree.head(wt))
assert (wt / file).read_text() == "x" and "lz/dirty" in git(repo, "branch", "--list", "lz/dirty")
assert (nested / ".git").exists()
assert "lz/parent" in git(repo, "branch", "--list", "lz/parent")


def test_cleanup_missing_worktree(repo: Path) -> None:
Expand Down
Loading