Skip to content

cherry-pick: AI Vault workspace-switch perf fixes for 1.4.161 - #11320

Merged
nwparker merged 3 commits into
1.4.161-rc.0-release-prepfrom
cherry/vault-perf-1.4.161
Jul 29, 2026
Merged

nwparker merged 3 commits into
1.4.161-rc.0-release-prepfrom
cherry/vault-perf-1.4.161

Conversation

@nwparker

Copy link
Copy Markdown
Contributor

Cherry-picks the three merged AI Vault workspace-switch perf fixes onto 1.4.161-rc.0-release-prep, on top of #11303 which is already on this branch (b85e267352).

All three applied cleanly with no conflicts, -x recorded on each.

cherry-pick from main what
8354f7ba02 c5102e1262 (#11311) regression guards + paint-latency tool
b018569303 6cc579a48c (#11314) scope-path dedupe — 189.7ms → 0.8ms
4d778b9390 b4b3bcdb84 (#11317) resume lookups via the shared worktree index

Why these belong in the release

The user-visible symptom was a 1–2s freeze when clicking a workspace card with the Agent Session History panel open, at real scale (1124 workspaces / 500 sessions).

a1a78da878 (#10841) is what made it visible — it added an unconditional .normalize('NFC') to normalizeRuntimePathForComparison, the innermost function in what were several O(sessions × worktrees) loops. #10841 shipped in 1.4.160, so the regression is live for users on the current release.

#11303 fixed the largest piece (the session map rebuilds). These three close out the rest:

Combined, vault work on a workspace switch goes from ~1s to ~2.5ms.

Verification on this branch

Not just "applied cleanly" — re-verified against the release branch itself:

  • 7988 tests pass across right-sidebar/, lib/, and shared/
  • tsc --noEmit -p config/tsconfig.tc.web.json clean
  • deriveAiVaultScopeSessionPaths measured at 0.7ms on this branch against a real 1124-worktree profile (was ~190ms)

Risk

Low. #11314 was verified byte-identical to the previous implementation (contents and ordering) across 156 scenario/option combinations — including NFD/NFC, WSL UNC, prior-path claims, and separator edge cases — and carries fail-first regression guards. #11317 is a swap to a helper already used the same way in connection-owner-resolution.ts. #11311 adds no production code.

NFC folding semantics from #10841 are untouched throughout — the same normalization runs, just not inside the hot loops.

nwparker added 3 commits July 29, 2026 00:29
#11303 removed the O(sessions x roots) path normalization from the session
worktree map, but nothing fails if that shape comes back. Adds the two
checks that were missing, plus the tool that would have caught it.

- timing guard: 1200 worktrees x 400 sessions must build in <150ms.
  Verified fail-first — restoring the pre-#11303 per-session
  buildWorktreeCandidates call takes it to 238ms; it is ~15ms as merged.
- path boundary: '/repo/alpha-sibling' must not be attributed to
  '/repo/alpha'. Hoisting the root normalization out of the loop must not
  degrade containment into a bare startsWith.

Also adds tools/benchmarks/workspace-switch-paint-latency.mjs, which
attaches over CDP and measures first-paint-after-click and max frame gap.
The existing worktree-switch-responsiveness.spec.ts only times the
synchronous click task, which stays ~1ms because the highlight is a direct
DOM mutation — that is why a ~1s stall could ship without tripping a
budget. On the affected build it read maxFrameGap p50=973ms.

(cherry picked from commit c5102e1)
* perf(vault): dedupe scope paths by key instead of rescanning

#11303 took the session maps off the workspace-switch path, but scope path
derivation still follows the active worktree and stayed quadratic.

addAiVaultWorkspaceScopePath deduped by re-normalizing every already
accepted path on each insert, so accepting K paths cost O(K^2)
normalize('NFC') calls — ~632k at 1124 workspaces. isAiVaultWorkspaceScopePathClaimed
separately rescanned every live worktree per prior id, and runs twice per
switch via activeWorktreePaths and scopePaths.

- carry a Set of comparison keys alongside the paths, so each insert is one
  normalize plus one Set lookup
- thread that accumulator from the workspace pass into the project pass
  rather than restarting deduplication against a plain array
- build one comparison-path -> worktree id map for the claim check, keeping
  first-writer-wins to match the previous some() short-circuit

deriveAiVaultScopeSessionPaths on a real 1124-workspace profile:
189.7ms -> 0.8ms. Output is unchanged, including ordering: verified against
the previous implementation across 117 scenario/option combinations
covering monorepo and mixed-repo layouts, priors both claimed and
unclaimed, duplicate paths, NFD/NFC, WSL UNC, trailing and doubled
separators, relative and blank paths, and four project-key shapes.

Adds the first test file for this module: scope semantics (priors, claimed
priors, cross-repo rejection, dedupe, NFD/NFC) plus a timing guard.
Verified fail-first — the guard reports 220ms on the previous
implementation. Path length is chosen deliberately, since normalize() cost
scales with it and short synthetic paths understate the old shape.

* fix(vault): make the claim check independent of worktree ordering

Review catch on the first pass: keying claims by comparison path meant a
duplicate path had to pick one owner, and picking the active worktree
masked a real claimant later in the list. Concretely, with the active
worktree also listed at its own prior path, the prior was reported
unclaimed where the previous some() reported it claimed.

Excludes the active worktree while building the set instead of comparing
ids at read time, so any surviving entry is a claim by construction and
ordering cannot decide the result.

Adds a test over four orderings, verified fail-first against the previous
commit. Also adds a timing guard for deriveAiVaultWorkspaceScopePaths,
which the session-scope guard did not cover.

Equivalence rerun against the pre-optimization implementation: 156
scenario/option combinations, identical paths and ordering.

(cherry picked from commit 6cc579a)
Resolving a session's resume target scanned every worktree in every repo:
two `Object.values(worktreesByRepo).flat().find(...)` calls, which also
allocate a fresh 1124-element array each time, plus an equivalent `some()`
walk. All three run per visible session row, so a panel render repeated
them ~20 times.

`getIndexedWorktreeMap` already exists for this and is WeakMap-cached on
`worktreesByRepo`, so the index is built once per store snapshot rather
than per call. `connection-owner-resolution.ts` already resolves worktrees
this way.

~1.6ms -> ~0.002ms per render pass at 1124 worktrees. Net -4 lines.

Behavior is unchanged: the map dedupes by id, which only diverges from
`find()` when one id appears twice with different objects. Worktree ids are
`repoId::path`, so a duplicate id within a repo array — the documented race
the index was built for — refers to the same worktree.

(cherry picked from commit b4b3bcd)
@greptile-apps

greptile-apps Bot commented Jul 29, 2026

Copy link
Copy Markdown

Greptile Summary

This cherry-pick lands three AI Vault workspace-switch perf fixes onto the 1.4.161 release branch, closing out a 1–2s freeze (introduced by #10841's unconditional .normalize('NFC') in normalizeRuntimePathForComparison) that was live for users on 1.4.160.

  • ai-vault-scope-paths.ts: Replaces the O(n²) dedupe in addAiVaultWorkspaceScopePath with a ScopePathAccumulator carrying a Set<string> of comparison keys; hoists buildClaimedComparisonPaths to run once per call instead of once per prior ID; threads the same accumulator through both workspace and session passes.
  • ai-vault-resume-target.ts / ai-vault-session-resume.ts: Replaces three Object.values().flat().find() / .some() scans with the existing WeakMap-cached getIndexedWorktreeMap helper.
  • tools/benchmarks/workspace-switch-paint-latency.mjs: New CDP benchmark measuring first-painted-frame latency after a real pointer event, usable as a git bisect run predicate.

Confidence Score: 5/5

Safe to merge — all three fixes are semantically equivalent substitutions for their originals, verified against 7988 tests and a clean tsc run on this branch.

The scope-path accumulator change preserves full ordering and dedup semantics: buildClaimedComparisonPaths excludes the active worktree by id up front (matching the old some() predicate regardless of list order), and the shared accumulator between workspace and session passes carries comparison keys forward so neither pass restarts normalization. The worktree index swaps are direct substitutions for the same WeakMap-cached helper already in use. Both are covered by correctness tests that include ordering-sensitive edge cases and scale regression guards.

Files Needing Attention: No files require special attention. The wall-clock regression guards use deliberately loose bounds (100ms and 150ms against operations now measured in single-digit milliseconds), so CI flakiness risk is low.

Important Files Changed

Filename Overview
src/renderer/src/components/right-sidebar/ai-vault-scope-paths.ts Introduces ScopePathAccumulator (paths array + comparisonKeys Set) to eliminate the O(n²) NFC-normalize scan on every insert, and hoists buildClaimedComparisonPaths so the live-worktree scan runs once per call instead of once per prior ID.
src/renderer/src/components/right-sidebar/ai-vault-scope-paths.test.ts New test file with correctness coverage (prior-path claiming, ordering invariants, repo-filter, WSL edge cases) and two wall-clock regression guards capped at 100ms for 1200-worktree scale inputs.
src/renderer/src/components/right-sidebar/ai-vault-session-resume.ts Replaces Object.values().some() worktree scan in isKnownAiVaultResumeWorkspaceTarget with the WeakMap-cached getIndexedWorktreeMap().has() lookup; no logic change.
src/renderer/src/lib/ai-vault-resume-target.ts Replaces two Object.values().flat().find() scans in getAiVaultResumeWorkspaceExecutionHostId and getAiVaultResumeWorkspaceTargetStatus with the cached worktree Map; behavior is identical.
src/renderer/src/components/right-sidebar/ai-vault-session-worktree-map.test.tsx Adds a path-prefix boundary test (guards against startsWith without separator) and a scale regression test (1200 worktrees × 400 sessions, bound at 150ms).
tools/benchmarks/workspace-switch-paint-latency.mjs New CDP-based benchmark tool; attaches to a running dev app, fires real pointer events, and measures first-painted-frame latency. Exits 125 on setup failure so it works as a git bisect run predicate.

Reviews (1): Last reviewed commit: "perf(vault): look up resume worktrees th..." | Re-trigger Greptile

@nwparker
nwparker merged commit 66a4897 into 1.4.161-rc.0-release-prep Jul 29, 2026
24 checks passed
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