Skip to content

Stop evicted and expired worktree preparations - #18951

Merged
nwparker merged 2 commits into
mainfrom
nwparker/cancel-obsolete-worktree-preparation
Sep 6, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/cancel-obsolete-worktree-preparation

Conversation

@nwparker

@nwparker nwparker commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​377 $\color{#cf222e}{\Huge{\mathbf{−}}}$​3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​374
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​26 $\color{#cf222e}{\Huge{\mathbf{−}}}$​7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​19

Changing the composer base could evict a checkout from the preparation pool while its Git checkout continued to completion. Repeated base changes therefore spent CPU and disk on trees that Create could no longer claim.

Cancel preparation when its entry is evicted or expires. Keep claimed entries running, preserve caller cancellation, and wait for Git to settle before cleanup. Work canceled before checkout begins skips removal of a nonexistent registration. Shared host cleanup and the options used for finalization remain independent of entry cancellation.

Validation:

  • Five cancellation policy tests cover eviction, expiry, delayed shared cleanup, claimed in-flight work, and caller signal composition.
  • A real-Git test cancels after observing checkout write a payload file, verifies no later lock command, and verifies both partial files and registration are removed.
  • 79 focused tests passed across preparation, WSL root resolution, real Git, and existing Windows/WSL runner cancellation. After adding the no-checkout cleanup guard, the affected 42 tests passed again.
  • Node typecheck, formatting, lint, and diff checks passed. Independent review and elegance pass found no required correction.

A controlled registry probe before the change admitted ten blocked checkout operations while only three remained selectable and none was canceled. This verifies obsolete work admission, not a quantitative cold-create speedup. The new delayed-cleanup test proves evicted work never reaches checkout.

Assessed merge risk: low. Full CI passed at deab74e7ff109eb0f43571c3c5f12467733bf1c1, including all eight test shards, Git compatibility, static analysis, typecheck, and Windows/main packaging. Live Windows/WSL interruption cleanup remains less directly verified than macOS; Linux CI passed; live Windows/WSL interrupted-reset cleanup remains unverified. No identified user-facing negative trade-off: evicted/expired entries were already unavailable to Create. Cancellation is not a hard bound on transient disk use while cleanup finishes.

This is an independent follow-up to the instant-create work, based on main; it does not expand #18876. SSH/folder repositories remain outside this local/WSL preparation pool. No new Git command, wire field, dependency, UI, or mobile-facing surface. No merges are authorized; the user reviews and decides.

Current composer-free trial: v1.4.198-adhoc.20260906024508, source 6d989819c5ef5d85b8253d763450de9df4c88d57, includes current main 681119dc05b (including A), cancellation #18951, background stale cleanup #18967, and fetch/checkout overlap #18998. Parked draft composer #18876 is excluded.

Combined full typecheck, 60 focused tests, and background Electron existing Create flow passed. Downloaded ARM64 DMG passed SHA512/size, extraction identity, exact source/version, code signature and notarization. The actual background packaged app passed immediate Create, prepared Create, cancellation without additional workspaces/terminals, and real shell input/output. No draft editor appeared. Owned workspaces were clean and removed, terminal inventory returned to zero, the owned app stopped, and its remaining owned detached preparation was removed.

On the 23,608-file fixture: immediate Create took 7.317 s to observed selection; a second Create after nine seconds of composer dwell took 683 ms. Backend spans were 6.644 s and 515 ms. These are individual potentially warm-cache samples, with a blank terminal and no setup hook. They demonstrate the prepared path, not a universal cold speedup or agent readiness. Screenshots inspected. Windows release packaging is still running; no live Windows/WSL UI validation.


Post-review measurement (readiness review, 2026-09-05)

The original submission had no isolated latency number for this change. Measured with real Git on macOS (6000 × 48 KiB unique files, 5 trials per cell, alternating order, medians). Metric: wall time of a fresh prepareWorktreeCreateCheckout — the next Create's critical path — while obsolete checkouts are left running (before) versus aborted (this PR).

Obsolete checkouts in flight Before (left running) After (aborted) Speedup
1 1104 ms 782 ms 1.41×
3 1723 ms 766 ms 2.25×

Aborted samples are tight: 749–795 ms across all 10 runs. Caveat: this measures Git-level contention on one machine; in-app numbers will differ with the admission limiter and disk. An opt-in bench (ORCA_WORKTREE_PREPARATION_CANCEL_BENCH=1) is included and skipped by default.

Defect found and fixed during review

Every evicted/expired in-flight checkout was double-discarding. Abort makes prepareWorktreeCreateCheckout self-discard (src/main/git/worktree-create-preparation.ts:110), then the pool's discardEntry runs git worktree remove again on the already-removed registration, which exits 128 is not a working tree. That error was enrolled in pendingDiscards, retried across the next 3 preparations for the host (a wasted Git spawn each), and then logged a misleading "stays registered until this process exits" warning. Pre-existing for genuine checkout failures, but this PR makes it the common eviction path. Fixed by skipping retry on isOrphanedWorktreeError, with a regression test confirmed red (3 discards) without the guard and green (1) with it.

Residual risk

  • Abort landing mid-worktree add --no-checkout (millisecond window) could leave a half-written .git/worktrees/<id> that remove --force rejects; only git worktree prune clears it. Unproven; stale cleanup does not cover it.
  • Windows/WSL interrupted-reset cleanup remains unverified live; CI Windows shard passed.

@coderabbitai

coderabbitai Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The worktree preparation pool now combines caller and internal abort signals. It aborts in-flight checkouts when entries expire or are evicted, skips discard work before checkout starts, and checks cancellation before preparation steps. Discard retries stop for orphaned worktrees. Tests cover cleanup, shared cleanup, claimed entries, expiry, caller cancellation, and real-Git materialization aborts. An opt-in benchmark compares checkout latency with running and aborted obsolete preparations.

Merge Risk: 🔵 Low · up to 8c249

Worktree preparation cancellation behavior is covered by tests, but the new opt-in benchmark can report misleading cancellation performance because obsolete checkouts may not have begun materializing before measurement.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 10.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 5 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The description explains the change, motivation, testing, scope, risks, and validation results, but it does not follow the repository template. It omits the required section headings, a clear Linked I… Rewrite the description using all template headings. Add the linked issue using the required Fixes #... format, state N/A with a reason for Visual Proof if applicable, complete or explain the Testing and checklist items, and provide the…
✅ Passed checks (3 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies the primary change: stopping worktree preparations when pool entries are evicted or expire.
Full details: Description check

Explanation

The description explains the change, motivation, testing, scope, risks, and validation results, but it does not follow the repository template. It omits the required section headings, a clear Linked Issue entry in the required format, checklist status, AI Disclosure, Review, Agent skill upstream boundary, and explicit Visual Proof status.

Resolution

Rewrite the description using all template headings. Add the linked issue using the required Fixes #... format, state N/A with a reason for Visual Proof if applicable, complete or explain the Testing and checklist items, and provide the required scope, review, AI Disclosure, and Agent skill upstream boundary information.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: 0cc9fc44-34cf-4c30-9361-94104052688f

📥 Commits

Reviewing files that changed from the base of the PR and between 22a7bfd and deab74e.

📒 Files selected for processing (3)
  • src/main/git/worktree-create-preparation-real-git.test.ts
  • src/main/worktree-create-preparation-pool.ts
  • src/main/worktree-create-preparation.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 1 remains after this review.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ℹ️ No critical issues — one minor rough edge inline.

Reviewed changes

  • Cancel evicted/expired preparations — startPreparation composes a per-entry AbortController with the caller signal (AbortSignal.any) and both expireEntry and enforcePreparationLimit abort it, so an obsolete checkout stops instead of finishing a tree Create can no longer claim.
  • Skip discard for work that never started — discardEntry awaits entry.ready, then returns early when checkoutStarted is false, preserving claimed in-flight work and keeping cleanup (and the options it uses) independent of entry cancellation.
  • Cancellation-policy + real-Git tests — five focused unit tests cover eviction, expiry, delayed shared cleanup, claimed in-flight work, and caller-signal composition; a new real-Git test aborts mid-materialization and verifies no later lock command plus removal of partial files and registration.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

…kout already removed

An evicted or expired preparation now aborts its checkout, which self-discards
the registration before the pool's own discard runs. That second discard failed
with "is not a working tree" and was enrolled for up to three retries on later
preparations for the same host, spawning Git only to fail again and warning that
the path stays registered when it was already gone.

Also accept fs.watch events without a filename in the abort real-Git test, and
add an opt-in bench (ORCA_WORKTREE_PREPARATION_CANCEL_BENCH=1) that measures a
fresh checkout's wall time with obsolete checkouts left running versus aborted.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: e0493185-4262-4056-845f-2a5b9918fa86

📥 Commits

Reviewing files that changed from the base of the PR and between deab74e and 8c24914.

📒 Files selected for processing (4)
  • src/main/git/worktree-create-preparation-real-git.test.ts
  • src/main/git/worktree-preparation-cancel-latency.bench.test.ts
  • src/main/worktree-create-preparation.test.ts
  • src/main/worktree-preparation-discard-retry.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 1 remains after this review.

Comment on lines +59 to +65
const obsoleteWork = obsoletePaths.map((path, index) =>
checkout(path, controllers[index].signal).catch(() => {})
)
if (variant === 'aborted') {
// Eviction aborts in the same turn the incoming preparation is armed, so abort before the
// fresh checkout starts.
controllers.forEach((controller) => controller.abort())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Synchronize obsolete checkout materialization before timing.

checkout() is asynchronous and first awaits base-ref resolution. These lines do not prove that any obsolete checkout reached materialization before aborting or starting the fresh checkout. Line 60 also hides failures in the running variant.

Wait until each obsolete worktree has entered materialization before the variant branch. Fail a running trial when an obsolete checkout rejects. Otherwise, the report can compare pre-start cancellation or no contention against normal checkout work.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ No new issues found — prior feedback addressed cleanly.

Reviewed changes (delta against the prior Pullfrog review)

  • Stop retrying discards for already-removed registrations — runDiscard now returns early on isOrphanedWorktreeError, so the pool's discard of an evicted/aborted preparation no longer spawns up to three doomed git worktree unlock/remove --force attempts plus a console.warn when the checkout's own self-discard already cleared the registration.
  • Robust real-Git abort test on Linux — the materialization watcher now treats a null filename event as materialization too, since fs.watch delivers no filename on inotify and only the reset writes into the watched path.
  • Opt-in cancellation-latency benchmark — a new describe.skip-gated bench compares fresh-checkout wall time with obsolete preparations running vs aborted.
  • Preserve the real isOrphanedWorktreeError in the pool test mock — the mock now passes through the real predicate, plus a new test asserting a discard that fails as is not a working tree is attempted exactly once and never warned.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@nwparker
nwparker merged commit e2270fe into main Sep 6, 2026
27 checks passed
nwparker added a commit that referenced this pull request Sep 6, 2026
Resolves #18951 squash-merge conflicts by keeping the backgrounded stale
cleanup alongside main's discard-retry review fix. Moves the checkout
cancellation tests into worktree-create-preparation-cancellation.test.ts
to keep the registry suite under the test-file line limit.
nwparker added a commit that referenced this pull request Sep 6, 2026
Resolve squash-rewrite conflicts from #18951/#18967: keep main's
cancellation test split and fs.watch null-filename fix, plus this
branch's fetch/checkout overlap real-git tests.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
* Stop obsolete worktree preparations when evicted or expired

* fix(worktree): skip discard retries for registrations an aborted checkout already removed

An evicted or expired preparation now aborts its checkout, which self-discards
the registration before the pool's own discard runs. That second discard failed
with "is not a working tree" and was enrolled for up to three retries on later
preparations for the same host, spawning Git only to fail again and warning that
the path stays registered when it was already gone.

Also accept fs.watch events without a filename in the abort real-Git test, and
add an opt-in bench (ORCA_WORKTREE_PREPARATION_CANCEL_BENCH=1) that measures a
fresh checkout's wall time with obsolete checkouts left running versus aborted.
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.

1 participant