Skip to content

fix(core): defer snapshot repository discovery - #36290

Merged
kitlangton merged 3 commits into
v2from
location-profile
Jul 10, 2026
Merged

fix(core): defer snapshot repository discovery#36290
kitlangton merged 3 commits into
v2from
location-profile

Conversation

@kitlangton

@kitlangtonkitlangton commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • defer Snapshot Git repository discovery until the first snapshot operation
  • memoize the discovered repository/worktree state so later captures and comparisons retain existing behavior
  • avoid making every cold location graph pay snapshot setup during daemon replacement

Bottleneck

Per-node acquisition timing identified Snapshot setup as the dominant location-readiness cost during concurrent cold starts:

LocationTotal location bootSnapshot acquisition
opencode803 ms552 ms
life-hub2,477 ms2,077 ms
browser-control3,143 ms2,301 ms
openeval3,144 ms2,270 ms
experiments3,214 ms1,706 ms

Snapshot eagerly called git.repo.discover() and canonicalized the worktree while the location layer was acquired. Most reconnected locations need config/session data immediately but do not perform a snapshot operation.

flowchart LR
subgraph Before
L1[Acquire location] --> G1[Discover Git repository]
G1 --> R1[Location ready]
end
subgraph After
L2[Acquire location] --> R2[Location ready]
R2 --> C[First snapshot operation]
C --> G2[Discover and memoize Git repository]
end
Loading

Benchmark

An isolated Bun harness launched a fresh local server for each trial with temporary database/data/state paths, waited for authenticated health, then measured /api/location for one real repository followed by four concurrent cold repositories. The active managed daemon was never contacted or restarted.

Two seven-run baseline series bracketed the candidate run to control for cache warming:

Metric (median)Baseline beforeLazy SnapshotBaseline afterCandidate vs nearest baseline
Single cold location426 ms137 ms496 ms-68% to -72%
Slowest concurrent location1,656 ms919 ms1,987 ms-44% to -54%

The benchmark used one warmup plus five preliminary measurements, then two warmups plus seven measured A/B runs. Raw spreads were noisy, but both bracketing baselines remained materially slower than every candidate median.

Semantics

The first call to capture, files, diff, preview, restore, or checkout resolves the same repository/worktree state as before. Effect.cached shares one Location-scope-owned initialization fiber across concurrent and subsequent operations. Acquiring that shared fiber is uninterruptible, while waiting for its result remains interruptible, so cancelling the first caller neither blocks cancellation nor memoizes an interrupted result.

When snapshots are disabled, capture can now return without performing Git discovery at all.

Follow-up

This removes the largest measured node from the reconnect herd in #36285. Remaining measured costs are concurrent project resolution, config discovery, and eager Shell output setup; they should be optimized in separate benchmarked changes.

Verification

  • bun typecheck from packages/core: passed
  • bun run test test/snapshot.test.ts test/session-runner.test.ts from packages/core: 121 passed
  • full Core suite: 1268 passed and 6 skipped; existing filesystem watcher timeouts reproduce unchanged on the untouched baseline worktree

@kitlangton

Copy link
Copy Markdown
ContributorAuthor

Independent verification confirmed the source cause and benchmark direction.

  • Baseline Snapshot.boot eagerly runs Git discovery, which performs filesystem ascent and three git rev-parse calls.
  • Candidate 0d3915f5 moves that work behind lazy cached state; instrumented Snapshot acquisition drops from 552-2,301 ms to 0 ms.
  • Reconstructed server-log medians were approximately 423 / 1,646 ms (baseline A), 134 / 707 ms (candidate), and 463 / 1,954 ms (baseline B) for single / slowest-concurrent location boot.

Caveats: trial blocks were sequential rather than interleaved, real user plugins/MCP/config were active, and the change defers rather than removes discovery, so the first actual snapshot operation pays it. The post-candidate baseline returning to the original range makes the location-readiness win credible despite those confounders.

@kitlangton
kitlangton merged commit 0bb24a4 into v2Jul 10, 2026
8 checks passed
@kitlangton
kitlangton deleted the location-profile branch July 10, 2026 21:34
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@kitlangton