Skip to content

Pin local merge cleanup to the verified PR head - #1635

Merged
mergify[bot] merged 4 commits into
mainfrom
lean178/1537-pin-local-cleanup-v2
Nov 26, 2026
Merged

mergify[bot] merged 4 commits into
mainfrom
lean178/1537-pin-local-cleanup-v2

Conversation

@eowca

@eowca eowca commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Pin local merge cleanup to the verified PR head

Context and goal

  • Goal: Developers keep commits made while merge cleanup is running.
  • Problem: A task branch can advance after merge verification or after removal of its worktree, and cleanup previously deleted the newer work.
  • Context: Git verifies the task branch under a ref lock during removal. Branch deletion checks the expected SHA again after removal.

Closes #1537

Acceptance

  • A newer local commit preserves the checkout if it arrived before removal, and preserves the branch if it arrived after removal.

Base

main @ 2a11772
Base: 2a11772

Notes

This draft supersedes #1588 and preserves the ignored-file guard landed in #1556.

Validation

Recorded for 0cb5d94001ae

  • 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

  • primary, claude, 0cb5d94, approve

@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 0cb5d94001ae34f193cd108ee815118aa244b93c, 69.7s: approve (0 critical, 0 important, 3 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/worktree.py
argv = ("git", "update-ref", "--stdin")
with subprocess.Popen(
argv, cwd=wt, env=build_env(GIT_ENV), stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8", errors="surrogateescape",

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: Merging stderr into the transaction's stdout turns any Git warning into a refusal

Stderr is merged into stdout, and the code only accepts the first line if it is exactly prepare: ok\n. If Git prints anything first, such as a config or deprecation warning, response will not match and cleanup is refused. That fails safe, but cleanup can never succeed in that environment, and the error shows only the warning line. Also, if Git exits without output, readline() returns "" and the error message has no detail. Smallest fix: send stderr to a separate pipe and read stdout until prepare: ok. On failure, include the stderr output in GitError.

Comment thread src/loopzero/worktree.py
gd = Path(_git(wt, *_ABSOLUTE_COMMON_DIR, preserve_whitespace=True))
ref = f"refs/heads/{name}"
with _lock_verified_ref(wt, ref, expected_head):
if branch(wt) != name:

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: The ref lock does not stop HEAD being detached or switched before removal

The verify lock only prevents updates to refs/heads/<name>. It does not prevent changes to the worktree's HEAD symref. The branch(wt) != name check runs once, before is_dirty, the ignored-file scan and worktree remove. If someone runs git checkout --detach and commits on the detached HEAD in that window, git worktree remove still succeeds and that commit becomes unreachable. The window is narrow and outside this task's acceptance criterion, which covers commits on the branch. A cheap way to narrow it further: re-read head(wt) and branch(wt) right before worktree remove.

Comment thread src/loopzero/worktree.py
raise WorktreeError(f"worktree is dirty, refusing to remove: {wt}")
_require_no_ignored_user_files(wt)
_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")

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: The check that the branch is checked out elsewhere can race with branch deletion

Before this change, branch -D refused atomically when the branch was checked out in another worktree. update-ref -d does not refuse in that case, so the code now runs a separate worktree list check first. Someone could run git worktree add <path> lz/<slug> between that check and update-ref -d. The new worktree's HEAD would then point to a deleted ref. The old SHA guard keeps the commits safe, but that worktree ends up on an unborn branch. The risk is low. A comment noting this trade-off would be enough.

@eowca
eowca marked this pull request as ready for review November 26, 2026 07:56
@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@Mergifyio queue

@mergify

mergify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Merged into main as 8bdefb5 at 08:01 UTC

5 min in the queue, 4 min of it CI.

So far

  • ✅ Entered the queue Thu 26 Nov, 07:57 · rule main · triggered by @eowca with the @mergifyio queue command
  • ✅ Passed · draft #1681
  • ✅ Merged 08:01 · 8bdefb5 · squash
3 merge conditions met
  • github-review-approved · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required
  • any of · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required:
    • check-success = checks
    • check-neutral = checks
    • check-skipped = checks
  • any of · required by GitHub repository ruleset rule main: PR only, Mergify queue, existing checks required:
    • check-success = Loop-zero Eligibility
    • check-neutral = Loop-zero Eligibility
    • check-skipped = Loop-zero Eligibility

Times in UTC · dashboard · how the merge queue works

@eowca

eowca commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

No description provided.

@mergify
mergify Bot merged commit 8bdefb5 into main Nov 26, 2026
18 checks passed
@eowca
eowca deleted the lean178/1537-pin-local-cleanup-v2 branch November 26, 2026 08:02
@mergify mergify Bot removed the queued label Nov 26, 2026
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.

Pin local worktree and branch cleanup to the verified merged SHA

1 participant