Skip to content

perf(worktrees): speed up concurrent partial-clone creation - #167944

Merged
steipete merged 2 commits into
mainfrom
steipete/perf-micro-partial-clone-worktree-checkout-20260919
Oct 9, 2026
Merged

steipete merged 2 commits into
mainfrom
steipete/perf-micro-partial-clone-worktree-checkout-20260919

Conversation

@steipete

@steipete steipete commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Concurrent worktree creations in a partial clone repeatedly scan the same missing-object inventory. In a synthetic repository with 52,000 files and 4,700 packs, 40 creations spent 88.6 seconds of Git process time repeating that scan. Local main also tried to fast-forward before the new base's blobs were available.

Related: #167691.

User Impact

Concurrent default-base creations share object preparation before local main advances. Worktree materialization cannot start a lazy fetch. Failed prefetches report the missing-object count. Existing explicit-base and restore behavior stays under the same capacity owner; no configuration or schema migration is required.

Why This Change Was Made

The existing shared base preparation assigns a fresh generation key. The existing bounded Git-worker cache reuses hydration only for that generation, repository identity, and commit, with replacement refs still checked on each call. New generations and changed commits recheck missing objects; replacement refs retain full preparation. Registration continues to own the actual checkout commit.

Production LOC: +31 (excluding tests and docs). The growth threads the creation generation through the existing worker contract and preserves new-generation missing-object detection and replacement-ref sizing, plus the requested failure warning and no-lazy-fetch boundary. There is no second hydration implementation, queue, cache, or persistent state.

Evidence

Blacksmith Testbox (blacksmith-testbox, profile openclaw-check), lease tbx_01m4gwng8xdg3wavd47gv6gjmd, Git 2.55.0 / Node 24.19.0. Both runs used 40 simultaneous calls to the real ManagedWorktreeService.create, 52,000 tracked files, 4,700 initial packs, and a base move leaving 130 blobs missing. Every checkout's commit and representative changed/unchanged contents were verified.

Measurement Before After
Successful creations 40/40 40/40
Median creation 113.4 s 66.0 s
p95 creation 150.4 s 84.4 s
Slowest creation 156.1 s 87.1 s
Missing-object scans 40 1
Missing-object scan process time, total 88.60 s 2.55 s
Blob fetches 1 1
Median Git file materialization 3.20 s 3.39 s

Median creation improved 1.72× and p95 1.78×. Parent Node peak RSS was approximately 8.34 → 7.98 GiB (cumulative process high-water marks; Git children excluded). This is a synthetic file:// promisor remote, not a WAN or saturated production-host reproduction. Neither measured version hit the reported 300-second timeout. The measured win is eliminating repeated inventory work; filesystem checkout and lifecycle waits remain.

The partial-clone regression fails on the original code because local main remains at the old commit after successful worktree creation. The final candidate passed eight focused suites (181 passed, one platform skip), including shared fetch/cancellation, capacity, creation, profiles, source-only filters, missing-tip recovery, and Git-worker lifecycle. Independent autoreview found no actionable P0–P2 findings. The moved regression was also confirmed failing at the intended local-main commit assertion against unchanged production source. Final gate and cost evidence:

  • node scripts/check-changed.mjs --staged passed on AWS Crabbox (aws), lease cbx_c8b301d7d065, including all 27 core-test TypeScript graphs, lint, dead-code, docs, and ownership guards. The combined gate/timing payload completed successfully in 37m41s. Testbox had disconnected with exit 255 after 26 graphs; the entire gate was rerun on AWS.
  • pnpm test src/agents/worktrees/service-branches.test.ts --maxWorkers=1: 54 passed, one platform skip; 78.87 s wall time, 58.94 s Vitest duration. This existing real-Git integration suite exceeds 30 seconds; the change only allows the new private preparation fact in its result assertion.
  • pnpm test src/agents/worktrees/capacity.test.ts --maxWorkers=1: 17 passed; 29.13 s wall time, 26.28 s Vitest duration. The new regression reuses the partial-clone fixture and exercises actual Git hydration and fast-forward behavior.
  • Existing line-count warning in the branch suite is unchanged; the growth ratchet passes. All validation leases were stopped after use.

The 40-creation benchmark ran with node --import ./scripts/tsx.mjs .local/lane715/benchmark.mts. Focused proof used node scripts/run-vitest.mjs for service-branches, capacity, service, service.source-only-filters, service.profiles, service.capacity, git-fetch, and src/infra/git-worker.lifecycle.test.ts.

Remote validation used base 3e9038981322; the publication rebase was patch-identical (git range-diff matched), with no overlapping production changes in this patch.

CI found one additional exact-object assertion in git.test.ts that rejected the new private preparation key. It now matches the four meaningful base fields while retaining serialization, exact fetch arguments/count, and unrelated-ref preservation checks. No production code changed in this correction.

Repair proof on AWS lease cbx_85be33641150 (Git 2.53.0, Bun 1.4.0):

  • OPENCLAW_VITEST_RUNTIME=bun node scripts/run-vitest.mjs src/agents/worktrees/checkout-apfs.test.ts src/agents/worktrees/git.test.ts src/agents/worktrees/service.gc-performance.test.ts: 37 passed, 9 platform skips; 44.52 s. Bun emitted a nonfatal directory-mismatch diagnostic during teardown; the runner exited 0.
  • pnpm test src/agents/worktrees/git.test.ts --maxWorkers=1: 24 passed, one platform skip; 27.74 s wall time.
  • node scripts/check-changed.mjs --staged: passed for the assertion-only correction, with zero lint warnings/errors. Fresh independent autoreview also passed through P2.

Final exact-head CI run passed on ce2049bcef1ead37d90434e09fd6039c4bf020ca in 635 seconds wall time. Summed reported CI test-case times, excluding shared startup: capacity.test.ts 3.832 s (17 cases), service-branches.test.ts 34.687 s (54 cases), and git.test.ts 0.977 s (24 cases). The landed squash patch matches the reviewed branch patch exactly.

Share hydration through the existing default-base preparation and bounded
Git-worker cache, with a fresh key for each creation group. Preserve checks
for changed commits, replacement refs, and later preparation groups.
Hydrate before local-main fast-forward and disable checkout lazy fetching.

A 40-create synthetic partial-clone run with 52,000 files and 4,700 packs
reduced missing-object scans from 40 to 1 and median creation from 113.4s
to 66.0s. Cover partial-clone fast-forward and preparation invalidation;
validate focused suites and the complete changed-file gate remotely.
@clawsweeper

clawsweeper Bot commented Oct 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 complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation agents Agent runtime and tooling size: S labels Oct 9, 2026
@steipete
steipete marked this pull request as ready for review October 9, 2026 19:52
@steipete
steipete enabled auto-merge (squash) October 9, 2026 19:53
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 9, 2026
@clawsweeper

clawsweeper Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

Managed worktrees are isolated Git checkouts for agent sessions; this PR shares missing-object preparation across concurrent creations and downloads required files before advancing the local default branch.

Example: Create a default-base worktree from a partial clone whose remote main adds large.txt.

  • Before: Creation can succeed while local main remains behind because the new file's blobs were unavailable during fast-forward.
  • After: Preparation downloads the blobs first, allowing local main to advance and materialize large.txt with its 5,000-character contents.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) Useful and supported by real Git service-boundary measurements, with one concrete assertion repair remaining and limited application-level proof.
Proof confidence 🦐 gold shrimp (3/6) Sufficient (live_output): The recorded Testbox comparison exercises the changed worktree service and real Git transport against deliberately missing partial-clone blobs, verifies all 40 checkout commits and contents, and observes inventory scans falling from 40 to 1. Its synthetic local-remote environment and validation-base limits are explicit; the generation is runtime-only and changes no stored-data contract.
Patch quality 🦐 gold shrimp (3/6) 1 actionable review finding remain.

Product

Kind: Performance · Worth it: Yes
User problem: Concurrent isolated checkouts take longer because they repeatedly inspect the same partial-clone objects, and the local default branch can remain behind after successful creation.
Reason: The measured improvement addresses an existing workflow with modest production growth and no new configuration. Prior merged area history establishes owner direction for the existing preparation and sizing contracts.

Merge readiness

⛔ Needs changes before merge - 1 item remains

This PR provides a measured, distinct improvement that current main still needs. The production approach is coherent, but one existing test fails because the preparation result gained a private field.

Priority: P2
Reviewed head: 1501c783c5818f8ddbac44af38ea1d32adc20caf

Before merge

  • Update the remaining exact base-result assertion (P2) - Assigning preparationKey adds a fifth field to every automatic base result, but src/agents/worktrees/git.test.ts:336 still uses resolves.toEqual against four fields. The existing “serializes bounded default-branch fetching without pruning unrelated refs” scenario therefore fails deterministically, as exact-head CI confirms. Update that assertion to match the meaningful result fields, as this PR already does in service-branches.test.ts, while preserving its ref-serialization and unrelated-ref checks. This mismatch was missed in the previous review of the same unchanged head.

Findings

  • [P2] Update the remaining exact base-result assertion — src/agents/worktrees/base-ref.ts:206

Tests

  • Missing end-to-end proof: The optional matched concurrent-creation benchmark through the shipped CLI or Gateway RPC remains absent; the supplied benchmark drives the real service and native Git directly.
Agent review details

How this fits together

Managed worktree creation turns repository and session requests into isolated, owned Git checkouts. Its preparation path selects a base, downloads required objects, checks capacity, registers the branch, and materializes files.

flowchart TD
 A[Concurrent creation requests] --> B[Shared default-base preparation]
 B --> C[Git worker downloads missing objects]
 C --> D[Safely advance local default]
 D --> E[Register checkout and capture commit]
 E --> F{Same prepared commit?}
 F -->|Yes| G[Reuse preparation and materialize files]
 F -->|No| C
Loading

Technical review

Best possible solution:

Keep generation-scoped sharing within the existing preparation and Git-worker owners, with registration determining the final checkout commit.

Do we have a high-confidence way to reproduce the issue?

Yes: current-main source advances the local default before hydrating missing blobs, and the supplied native-Git negative control fails at the local-main commit assertion. This review inspected the source and CI evidence without executing target code.

Is this the best way to solve the issue?

Yes: the patch reuses existing preparation and sizing owners, preserves commit-specific admission and replacement-ref checks, and leaves explicit-base and restore hydration intact.

Full review comments:

  • [P2] Update the remaining exact base-result assertion — src/agents/worktrees/base-ref.ts:206
    Assigning preparationKey adds a fifth field to every automatic base result, but src/agents/worktrees/git.test.ts:336 still uses resolves.toEqual against four fields. The existing “serializes bounded default-branch fetching without pruning unrelated refs” scenario therefore fails deterministically, as exact-head CI confirms. Update that assertion to match the meaningful result fields, as this PR already does in service-branches.test.ts, while preserving its ref-serialization and unrelated-ref checks. This mismatch was missed in the previous review of the same unchanged head.
    Confidence: 1
    Late finding: first raised on code an earlier review cycle already covered.

Overall correctness: patch is incorrect
Overall confidence: 0.99

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against d6158cc48033.

Provenance checked

Testing

Proof path: in-process harness.

Security

None.

Evidence

Acceptance criteria:

  • pnpm test src/agents/worktrees/git.test.ts --maxWorkers=1
  • git diff --check

What I checked:

Likely related people:

  • Peter Steinberger: Raw commit 4ad94b5 adds src/agents/worktrees/base-ref.ts:161 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 4ad94b56c5b1; files: src/agents/worktrees/base-ref.ts)

PR surface

Source +31, Tests +25, Docs 0. Total +56 across 10 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 7 50 19 +31
Tests 2 38 13 +25
Docs 1 1 1 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 10 89 33 +56

Review metrics

Metric Value Why it matters
Production versus test growth Production net +31 lines; tests net +25 lines Production growth is justified by carrying preparation facts through existing owners and preserving invalidation and checkout boundaries.

Labels

Label changes:

  • add status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.
  • remove status: 👀 ready for maintainer look: Current PR status label is status: ⏳ waiting on author.

Label justifications:

  • P2: This improves concurrent partial-clone creation and local-default advancement within a bounded managed-worktree workflow.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🦐 gold shrimp.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only; repair and merge need explicit maintainer commands such as @clawsweeper autofix or @clawsweeper automerge.

History

Review history (1 earlier review cycle)
  • reviewed 2026-10-09T19:57:56.054Z sha 1501c78 :: needs maintainer review before merge. :: none

Reviewed October 9, 2026, 4:04 PM ET / 20:04 UTC (Revision 2).

@clawsweeper clawsweeper Bot added status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 9, 2026
@steipete
steipete merged commit 2e246d3 into main Oct 9, 2026
131 of 133 checks passed
@steipete
steipete deleted the steipete/perf-micro-partial-clone-worktree-checkout-20260919 branch October 9, 2026 20:29
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 10, 2026
…#167944)

* perf(worktrees): speed up concurrent partial-clone creation

Share hydration through the existing default-base preparation and bounded
Git-worker cache, with a fresh key for each creation group. Preserve checks
for changed commits, replacement refs, and later preparation groups.
Hydrate before local-main fast-forward and disable checkout lazy fetching.

A 40-create synthetic partial-clone run with 52,000 files and 4,700 packs
reduced missing-object scans from 40 to 1 and median creation from 113.4s
to 66.0s. Cover partial-clone fast-forward and preparation invalidation;
validate focused suites and the complete changed-file gate remotely.

* test(worktrees): allow private base preparation metadata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant