Repository navigation
fix(worktrees): preserve work during creation and cleanup races - #149914
Conversation
Pin checkout sizing and materialization to the commit Git actually registers, while retaining native upstream tracking and submodule behavior. Keep rollback under allocation ownership after caller cancellation and fence it after lease loss or a changed Git registration. Serialize orphan cleanup and snapshot expiry with creation and restore, recheck activity before cleanup, and use the actual allocation outcome for session rollback. Recover Git locks after registry errors, handle stale origin/HEAD and nested working directories, and remove unused branch and lease machinery.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: blocked before merge. Reviewed September 16, 2026, 5:10 AM ET / 09:10 UTC. ClawSweeper reviewWhat this changesThe PR pins worktree creation to its selected commit, coordinates cleanup with allocation, and limits failed-session rollback to checkouts that operation created or restored. Merge readiness⛔ Blocked before merge - 1 item remains This remains a useful lifecycle repair: current main retains the affected creation and cleanup paths, and no actionable introduced defect was found. The maintainer-protected PR is not a cleanup-close candidate. Priority: P2 Review scores
Verification
How this fits togetherManaged worktrees give agent sessions isolated Git checkouts. The worktree service turns creation, restoration, and cleanup requests into filesystem operations and registry records consumed by the Gateway. flowchart TD
A[Session or CLI request] --> B[Worktree service]
B --> C[Shared allocation lease]
C --> D[Register and capture commit]
D --> E[Check capacity and materialize files]
E --> F[Publish registry and session outcome]
C --> G[Cleanup with current ownership checks]
G --> H[Preserve work or remove safely]
Before merge
Agent review detailsSecurityNone. PR surfaceSource +74, Tests +537, Docs +2. Total +613 across 20 files. View PR surface stats
Review metrics
Stored data modelPersistent data-model change detected: Technical reviewBest possible solution: Keep allocation and destructive cleanup under one live lease, with commit-pinned materialization and owner-produced rollback outcomes. Do we have a high-confidence way to reproduce the issue? Yes, source establishes the moving-ref and cleanup interleavings, and focused regressions encode them through the production service. This read-only review did not execute a failing current-main reproduction. Is this the best way to solve the issue? Yes. Extending the existing allocation owner and returning its actual outcome repairs the race at its source while preserving public results, configuration, schemas, and retention periods. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 6cfec0530a3b. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
|
Checked the data-model compatibility flag against The passing 373-test worktree run includes registry persistence and existing-registry migration coverage, plus the regressions for reacquiring a Git guard after a registry-read failure and retrying a failed registry read during release. These exercise existing records through the unchanged SQLite owner and real Git locks. The tested source tree is exactly the pushed commit tree ( |
|
Merged via squash.
|
…claw#149914) Pin checkout sizing and materialization to the commit Git actually registers, while retaining native upstream tracking and submodule behavior. Keep rollback under allocation ownership after caller cancellation and fence it after lease loss or a changed Git registration. Serialize orphan cleanup and snapshot expiry with creation and restore, recheck activity before cleanup, and use the actual allocation outcome for session rollback. Recover Git locks after registry errors, handle stale origin/HEAD and nested working directories, and remove unused branch and lease machinery.
Related #101721.
What Problem This Solves
Fixes managed worktree creation using stale disk estimates after a source branch moves, and overlapping creation, restoration, cleanup, or failed session setup removing work owned by another operation.
User Impact
New worktrees retain native Git upstream tracking and submodule behavior while checking capacity for the commit actually selected. Cleanup preserves unfinished clones, active work, recovery snapshots, and checkouts reused by another session operation. Cancellation cleans up a fresh allocation; loss of allocation ownership preserves it for recovery.
Also fixes stale
origin/HEADfallback, nested working directories such as..notes, and Git-lock recovery after registry-read failures. Existing configuration, public create results, schemas, and retention periods stay unchanged.Why This Change Was Made
Git registers the branch without checking out files; the allocation owner then captures its commit and materializes that exact source. Rollback uses live allocation authority and the original Git registration, independently of caller cancellation. Snapshot expiry and orphan cleanup share that ownership, and session preparation consumes the service's actual allocation outcome.
Source-template keys exclude branch-tracking metadata so remote-based restores can reuse their templates. The unused caller-owned branch mode and dead lease bookkeeping are removed.
Evidence
git diff --checkpassed.service.apfs.test.ts,filesystem-apfs.test.ts,filesystem-native.test.ts, andcheckout-apfs.test.tspassed with no skips at landed commit23fb1c04eaf6ccf995edc4c99008bc3229cf3272(Node 26.8.2, 32.88 seconds). This verifies real clone identities, independent writes, modes/symlinks, ACL preservation and fallback, Git index edit detection, cancellation and authority fencing, plus creation, provisioning, removal, restore, invalidation, and cleanup. Temporary repositories, state, and worktrees used an APFS volume with sufficient free space; capacity checks remained enabled.Native ReFS block-clone integration remains unverified on a ReFS volume.