Skip to content

Resume merged branch cleanup before removing the worktree - #1760

Closed
eowca wants to merge 5 commits into
mainfrom
lean178/1116-guarded-cleanup
Closed

eowca wants to merge 5 commits into
mainfrom
lean178/1116-guarded-cleanup

Conversation

@eowca

@eowca eowca commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Resume merged branch cleanup before removing the worktree

Context and goal

  • Goal: An interrupted cleanup can finish on a subsequent merge invocation.
  • Problem: Removing the task worktree before deleting the local branch loses the checkout needed for retry.
  • Context: The earlier Finish merged branch cleanup before removing its worktree #1192 ruling uses a retained detached worktree and live merged-PR evidence as recovery proof. The documented residue branches were already removed.

Closes #1116. Replaces #1724 with the reviewed cleanup design and fixes for all four findings.

Acceptance

  • Check the exact merged PR head and local branch before detaching the clean linked worktree. Delete the branch at the expected head before removing that worktree.
  • A detached retry must find an exact merged PR for the previous branch and HEAD. Its worktree remains intact following branch deletion errors, and retry also works when the branch is already gone.
  • Preserve existing protections for moved refs, another worktree's checkout, ignored user files, and normal dirty-worktree behavior.

Notes

  • The focused cleanup suites passed, including interruption before ref deletion, interruption before worktree removal, a moved ref, and missing merged-PR proof. The size budget passed with three fewer test lines.
    Preserve the verified reference transaction and existing managed-worktree checks. The previous checkout name is a lookup hint. Only matching live merged-PR and ref evidence allows deletion.

Base

main @ 4e1c0a4

Validation

Recorded for c2a3dac04826 (not current head) (unverified, run loopzero check)

  • uv run --group dev ruff check src tests: exit 0
  • uv run --group dev tach check: exit 0
  • uv run --python 3.14 python scripts/check_architecture_docs.py: exit 0
  • scripts/size_budget.sh: exit 0
  • uv run --group dev pytest -q: exit 0

Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Review

eowca and others added 3 commits November 26, 2026 09:46
Co-Authored-By: Codex <noreply@openai.com>
Architecture-Review: unchanged; detached recovery still requires exact merged PR proof and now guards the local HEAD until removal.
Co-Authored-By: Codex <noreply@openai.com>

@loopzero-feder-positronics loopzero-feder-positronics Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loopzero primary review by claude on c2a3dac04826808f14696aabec960652e99e8e2c, 72.2s: approve (0 critical, 0 important, 4 suggestion)

Settings per native call (launch configuration and CLI claims, not backend attestation):

  • Call 1: requested model claude-opus-5-5 (effort medium); CLI-reported model claude-opus-5-5 (effort unknown); configured model claude-opus-5-5 (effort medium)

Native Claude output is retained in the restricted local review receipt.

Comment thread src/loopzero/github.py
return None


def _merged_pr_from_history(repo: str, branch: str, sha: str) -> PR | None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Closed-PR history paging duplicates merged_pr_for_branch with weaker validation

_merged_pr_from_history reimplements the pulls?state=closed&head=owner:branch paging loop that merged_pr_for_branch (github.py:750) already has. The new copy is missing some of the original's checks. It does not reject pages with more than PAGE rows, it does not dedupe repeated PR numbers, and it skips the row validation in _closed_history_number. Also, _matching_merged_pr raises a generic ("gh","api") error that does not name the endpoint. Because _pr_detail and the exact head/branch match recheck every candidate, this does not create a safety hole. It does mean two history readers can drift apart. Smallest fix: run the closed-history walk through the existing validation (_closed_history_number plus the page-size check), or add an optional exact-sha filter to merged_pr_for_branch instead of keeping a parallel loop.

Comment thread src/loopzero/cli.py Outdated

def _pr_readiness(
wt: Path, config: Config, *, allow_merged: bool = False, workflow_wait: bool = False
wt: Path, config: Config, *, allow_merged: bool = False, workflow_wait: bool = False,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: New branch parameter on _pr_readiness is an unneeded shim

_merge_pr_readiness calls worktree.branch(wt) and passes the result as branch=. Without the argument, _pr_readiness computes exactly the same value, and no other caller uses the new parameter. Remove the parameter and keep the original branch, head = worktree.branch(wt), worktree.head(wt) line. In _merge_pr_readiness, return the branch it already computed.

Comment thread src/loopzero/worktree.py
raise WorktreeError("local checkout changed during merge cleanup")
_require_clean_worktree(wt)
_delete_merged_branch(gd, name, expected_head)
_require_no_ignored_user_files(wt)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Ignored-file scan runs twice under the HEAD lock

_require_clean_worktree(wt) at line 345 already calls _require_no_ignored_user_files. Line 347 then runs the same scan again with nothing in between except the branch-ref deletion, which does not touch the working tree. Drop line 347, or keep it and add a comment explaining why the second scan is needed.

Comment thread src/loopzero/cli.py Outdated
def _merge_pr_readiness(
wt: Path, config: Config, *, workflow_wait: bool
) -> tuple[str, github.PR, github.Readiness | None]:
if previous := worktree.detached_from(wt):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Detached worktrees whose previous checkout is not a task branch now get a misleading error

Before this change, loopzero merge on any detached HEAD failed with "HEAD is detached. Switch to a branch and retry." Now detached_from returns whatever branch the reflog shows. If someone detached from the base branch at the same SHA, they get "branch 'main' is the configured base; use a task branch". For any other branch, the command makes GitHub API calls and then reports "no merged PR proves cleanup". This is safe, but the message points users the wrong way. Consider falling back to the original detached-HEAD error when the previous branch fails _require_task_branch or no proof is found.

Architecture-Review: unchanged; attached merge uses the captured branch and detached recovery still requires exact merged proof.
Co-Authored-By: Codex <noreply@openai.com>

@loopzero-feder-positronics loopzero-feder-positronics Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loopzero delta review by claude on 8e40527651ca7b33f9e61280a44a7f3ffc6b94f7, 10.5s: approve (0 critical, 0 important, 0 suggestion)

Settings per native call (launch configuration and CLI claims, not backend attestation):

  • Call 1: requested model claude-opus-5-5 (effort medium); CLI-reported model claude-opus-5-5 (effort unknown); configured model claude-opus-5-5 (effort medium)

Native Claude output is retained in the restricted local review receipt.

@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Parked at b592248. Hosted CI caught a duplicate branch lookup on ordinary merges, and main added an assertion in the same cleanup test. The unpublished local commit 147fecc8 retains both tests and restores the single lookup. All 20 focused cases passed. This PR has used both review rounds, and integrating the advisory PR #1781 ahead of this repair would exceed the source cap.

@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

After #1781 landed, the local follow-up b9e8054a passed the 20 focused cases and the complexity budget. Against current main 3f60c7f, it measures 8,334 source lines against the 8,250 cap and 13,849 test lines against 13,850. It remains unpublished, and this PR stays parked at b592248.

@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Closing on Lean's decision from the test-line supply audit (#575 (comment)): this PR's test and source cost on a full cap outweighs its measured benefit. The approved work stays in the branch history. Reopen or replace it only with new evidence of a material, measured saving or failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resume local branch cleanup after merge removes the worktree

1 participant