Skip to content

fix(worktrees): avoid checkout stalls during background pack maintenance - #195968

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-checkout-lease-held-by-pack-maintenance-20260919
Nov 9, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-checkout-lease-held-by-pack-maintenance-20260919

Conversation

@steipete

@steipete steipete commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes worktree preparation waiting for a shared project's entire background pack-consolidation pass. The pack writer already released the ref-mutation queue during rewriting, but an outer project checkout lease still blocked preparation.

User Impact

Foreground preparation can acquire the checkout while Git verifies and writes packs. Lease-wait diagnostics identify the holder's operation alongside its UUID. No configuration, schema migration, or operator action is required.

Why This Change Was Made

A separate maintenance lease serializes background writers. Optional pack-index invalidation and retirement take the checkout lease without waiting; busy retirement defers without suspending the repository or consuming its hourly slot. Retirement stops starting pack deletions after 250 ms, finishing the current pack's sidecars. Replaced input packs can remain as valid duplicates for a later sweep. Removal of an entire project may overlap background IO and cause a recoverable maintenance failure for that removed clone.

This completes the separation started in #191152. Base refresh and source templates are unchanged. Production code grows by 36 lines to retain background mutual exclusion, protect the short checkout mutations, and bound retirement.

Evidence

Acceptance was declared in the benchmark before measured runs: lease wait below one second while consolidation runs, unchanged HEAD and original objects, and no 20% or greater owner CPU/allocation regression. The reproducible script is scripts/perf/worktree-checkout-lease-measure.ts; it overlays all three original owners, records source hashes, and observes a real Git consolidation launch before requesting the real checkout lease and registering a worktree.

The script header supplies fixture preparation and baseline capture instructions. Run the comparison with node --import ./scripts/tsx.mjs scripts/perf/worktree-checkout-lease-measure.ts --seed <seed> --output <new-output> --base-file <baseline-maintenance> --base-checkout-file <baseline-checkout> --base-lease-file <baseline-state-lease> --pairs 5.

Five interleaved baseline/candidate pairs on a synthetic macOS/APFS fixture: 1,025 packs (1 kept base, 1,024 promisor revision packs), 62,000 files / 1.143 GB checkout, and 537 MB raw revision data. Warm filesystem caches; fresh process and clone per sample. Main-isolate allocation uses V8's cumulative allocation counter. Native Git CPU/RSS uses process receipts.

Per-pass metric Before median / p95 After median / p95
Foreground checkout-lease wait 2,647.6 / 2,665.0 ms 7.0 / 10.4 ms
Foreground registration 2,672.2 / 2,689.6 ms 68.8 / 72.0 ms
Complete maintenance 13,580.8 / 14,697.8 ms 13,474.3 / 13,915.9 ms
Main-thread CPU 234.6 / 247.1 ms 182.6 / 185.0 ms
Process CPU 2,522.7 / 2,827.1 ms 2,378.1 / 2,625.6 ms
Native Git CPU 10,020 / 10,510 ms 9,890 / 10,200 ms
Main-isolate allocation 114.24 / 129.43 MB 103.99 / 118.05 MB
Native Git peak RSS 623.69 / 645.50 MB 623.49 / 632.86 MB

All five candidate requests acquired during consolidation; no baseline request did. Both arms launched 14 Git commands and preserved every original object without fetching. Baseline ended with two packs; bounded candidate retirement left 254–307. These are one-pass measurements, not a claim of lower total CPU to reach complete pack convergence. With five samples, p95 is the maximum. Five A/A control pairs used the same baseline code in both arms: median waits 2,667.5 / 2,669.5 ms and p95 7,089.6 / 5,140.8 ms. Host load increased during controls; no samples were discarded. One baseline control recovered from a native lease-renewal SQLite-contention warning. Smaller CPU/wall differences should not be read as a production throughput guarantee. Separate baseline/candidate CPU profiles were recorded outside the timing samples.

Registration after lease acquisition was slower under concurrent maintenance: median 24.5 → 62.3 ms. The cause is not isolated; overlap and the different pack count may both contribute. Total foreground latency improved by about 97%.

This is local owner-level proof, not a full Gateway, btrfs, or production benchmark. This owner does not read agent databases, so a padded agent database would not add exercised work.

Regression proof covers foreground preparation during rewriting, maintenance mutual exclusion, yielding retirement to foreground ownership, bounded retirement, and operation names in real contention logs. Foreground admission and owner-name regressions failed before the fix. Initial focused run: 47 passing tests, one Linux-only cleanup skip. The final five targeted cases also passed (44 s wrapper wall time, about 3 s test execution). The initial four-file run took 198 s including worker compilation. Final Codex P0–P2 autoreview against 518e8a7d43f3 was scoped-clean; the allocation-only follow-up was separately reviewed. The 115 added test lines use a 110-line grant plus five lines of existing headroom.

pnpm check:changed --staged --base HEAD --timed completed with 36 checks passing, including affected type/lint/format and database/schema guards. Two unchanged main regressions failed: historical changelog attribution rows introduced by #194364 (266a7aa0fe69), and the unused authorDisplayName dashboard export introduced by #194455 (cde34f111b4d). Both failures were reproduced from exact parent 518e8a7d43f3; the introducing source changes were verified. The attribution gate was already red on different rows before #194364; the current 20-row signature is new to that commit. These remain main regressions for follow-up, not passing checks. The PR does not change either owner.

Prepared for review only; intentionally left unmerged.

Serialize long Git maintenance passes separately from foreground checkout preparation. Keep checkout ownership only for nonwaiting pack-index invalidation and bounded pack retirement, preserving verified replacements when retirement defers. Include operation names in lease holder diagnostics.
@clawsweeper

clawsweeper Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review ended

ClawSweeper stopped reviewing this revision because the pull request closed.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation scripts Repository scripts agents Agent runtime and tooling size: L labels Nov 9, 2026
@steipete
steipete marked this pull request as ready for review November 9, 2026 12:41
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T12:45:10.170179Z e2f4d48 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e2f4d48b30

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +426 to +427
if (performance.now() >= deadline) {
break;

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.

P2 Badge Resume deferred retirement independently of pack selection

When the retirement budget expires partway through a batch, the remaining duplicate packs can become permanently ineligible for cleanup rather than retiring on a later sweep. Subsequent passes rerun the dominant-pack filter at lines 356–362, which excludes the replacement when the remaining inputs are smaller than it. For example, consolidating two independent 64 KiB packs and retiring only the first leaves packs of 65,597 and 131,162 bytes; the next pass selects only one and therefore neither rewrites nor retires anything. Larger batches can likewise converge to a replacement plus a redundant subset, retaining substantial duplicate disk usage indefinitely. Resume the unfinished retirement independently of whether those packs qualify for another consolidation batch.

Useful? React with 👍 / 👎.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 9, 2026
@steipete

steipete commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Landing note (maintainer review): on Team (r301, 2026-11-09 11:14–11:16Z) a startup send waited ~88 s for the project checkout lease while worktree git maintenance consolidated packs (a 354 MB promisor pack) in the shared base clone. Maintenance now serializes pack writers under its own projects.git-maintenance state lease. It takes the checkout lease only for the short MIDX invalidation and pack-retirement sections (waitMs 0, 250 ms retirement budget; leftover duplicates wait for a later sweep). Lease-wait logs name the holder's operation. Synthetic 1,025-pack / 62k-file fixture: median foreground lease wait 2,648 → 7 ms. Production +36 lines; exact-head CI green.

@steipete
steipete merged commit 280e1de into main Nov 9, 2026
214 of 218 checks passed
@steipete
steipete deleted the steipete/perf-micro-checkout-lease-held-by-pack-maintenance-20260919 branch November 9, 2026 12:54
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 10, 2026
…nce (openclaw#195968)

* fix(worktrees): avoid checkout stalls during background pack maintenance

Serialize long Git maintenance passes separately from foreground checkout preparation. Keep checkout ownership only for nonwaiting pack-index invalidation and bounded pack retirement, preserving verified replacements when retirement defers. Include operation names in lease holder diagnostics.

* test(worktrees): use the exact checkout regression line allowance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. docs Improvements or additions to documentation scripts Repository scripts size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant