Skip to content

perf(ai-vault): deduplicate session scan batches incrementally - #20255

Merged
nwparker merged 2 commits into
mainfrom
OrcaWin/codex-perf-09-11-session-dedup-batches
Sep 13, 2026
Merged

nwparker merged 2 commits into
mainfrom
OrcaWin/codex-perf-09-11-session-dedup-batches

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Sep 12, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​269 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​269
Prod 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​257 $\color{#cf222e}{\Huge{\mathbf{−}}}$​24 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​233

Summary

Local and SSH session scanners currently deduplicate the entire accumulated result after every batch of eight parsed files. Use a scan-local collection to admit each parsed row once, reusing the existing alias-key and canonical-root comparison functions. The standalone batch deduplicator remains unchanged.

An insertion-ordered map preserves surviving row order; replacements delete prior winning occurrences instead of moving another row into their position. Identical-object duplicates, distinct tied objects, invalid dates, canonical root/time/path preferences, native/WSL namespaces, execution-host isolation, unique-session parse budgets, cancellation and remote event-loop yields retain their existing behavior. Parsed sessions and title refreshes are replaced/cloned, not mutated after admission.

No new persisted cache, wire fields, execution ownership, agent status, workspace assumptions, or provider-specific behavior. Independent performance improvement 9; tracks #20206.

Measurement

The benchmark bundles the unchanged production batch deduplicator and the new collection, exercising the scanners' eight-row accumulation policy. Eight counterbalanced pairs, Node v26.6.0/macOS arm64, after all local tests and typechecks finished. Median milliseconds:

Workload Before After
100 unique Codex sessions 0.422 0.033
1,000 unique Codex sessions 36.7 0.331
5,000 unique Codex sessions 933 1.79
10,000 unique Codex sessions 3,735 3.70
1,000 Claude sessions 0.558 0.018
1,000 sessions followed by preferred-root aliases 102.7 0.742
1,000 sessions followed by losing-root aliases 106.3 0.664
5,000 sessions followed by preferred-root aliases 2,729 4.08
5,000 sessions followed by losing-root aliases 3,237 4.33

Synthetic deduplication CPU only: no disk/network, parser, final sorting or app-wide latency claim. Eight-row Claude batches change from 0.132 to 0.181 microseconds; ordinary 100+ row workloads improve. The collection retains O(surviving rows) scan-local indexing metadata, releases losing session objects, and becomes unreachable when the scan returns its array. Forced-GC retained heap growth while holding the index was about 221 KiB for 1,000 unique rows and 2.70 MiB for 10,000; this is not a peak-RSS measurement.

ORCA_BACKGROUND_LAUNCH=1 node --expose-gc config/scripts/session-dedup-batches-benchmark.mjs

Verification

  • All 880 AI Vault tests pass across 112 files.
  • 40,000 seeded differential batches compare count, ordering and object identities against the unchanged batch deduplicator, including same-reference duplicates and frozen inputs.
  • Real native/remote cold and warm scanner operation tests: baseline fails with 2,688/2,560 rollout checks for 128 files; the new version satisfies the linear bound of 1,024 checks.
  • Cross-batch SSH test verifies eight later preferred aliases do not consume the unique-session budget and older candidates still early-stop.
  • pnpm tc:node and changed-code quality gate pass.
  • All tests use ORCA_BACKGROUND_LAUNCH=1; no app windows launched. Actual OS validation is macOS; SSH tests use an in-memory provider, with Windows/WSL paths covered by unit/differential tests.

Visual Proof

N/A — session contents, ordering and UI behavior are unchanged.

Independent review follow-up

Commit 7fd3aae222178e993d3598e7d406bb5e9984bd81 reduces the scan-local index's memory cost. A unique winning session stores one numeric occurrence index; an array is allocated only when the identical winning object occurs repeatedly. Winner selection, insertion order, duplicate-object handling, host/WSL isolation, cancellation and scan budgets are unchanged.

Eight-pair forced-GC measurements with the same pre-created input rows held constant, comparing the original PR head with the applied fix:

Unique Codex rows Original PR index Fixed index
1,000 217,080 bytes 161,080 bytes
10,000 2,517,576 bytes 1,957,688 bytes
100,000 23,345,808 bytes 17,746,296 bytes

That removes approximately 56 bytes per ordinary unique row (22–26% in these fixtures). This is retained collection overhead, not peak RSS; the necessary O(surviving rows) temporary index remains a real trade-off.

Fresh verification: 608 tests across 88 AI Vault files; an additional alternating single/repeated-winner regression; 40,000 unchanged-batch-oracle cases; another 40,000 original-PR-vs-fix batches; Node typecheck, changed-code gate and commit hooks all pass. All checks use ORCA_BACKGROUND_LAUNCH=1; no app/native windows launched.

The dependent #20301 branch also includes this mitigation. Merge this PR first, then retarget/revalidate #20301 against main. No PR has been merged by this review.

@coderabbitai

coderabbitai Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 7 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 924f5b5b-c8c1-48d4-9c1c-a05dc2efe799

📥 Commits

Reviewing files that changed from the base of the PR and between fa3d29f and 7fd3aae.

📒 Files selected for processing (6)
  • config/scripts/session-dedup-batches-benchmark.mjs
  • src/main/ai-vault/codex-session-collection.test.ts
  • src/main/ai-vault/codex-session-root-dedup.ts
  • src/main/ai-vault/remote-session-scanner.ts
  • src/main/ai-vault/session-scanner-dedup-batches.test.ts
  • src/main/ai-vault/session-scanner.ts

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.

@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.

Reviewed changes

  • Incremental session dedup (codex-session-root-dedup.ts) — adds CodexSessionCollection, which admits each parsed row once and tracks every retained occurrence of the winning row per alias key, replacing the per-batch whole-array re-dedup. The standalone dedupeCodexSessionsBySessionId is unchanged.
  • Local scanner (session-scanner.ts) — parseSessionCandidates accumulates into the collection and canStopParsingSessions reads it; the array is materialized once at return.
  • Remote scanner (remote-session-scanner.ts) — same change for parseRemoteSessionCandidates / canStopParsingRemoteSessions.
  • Tests — codex-session-collection.test.ts checks parity against the batch deduplicator across ties, invalid dates, canonical roots, WSL namespaces and execution hosts; session-scanner-dedup-batches.test.ts verifies native and remote scans do not re-derive retained aliases per batch and that cross-batch remote replacements do not consume the unique-session budget.
  • Benchmark — config/scripts/session-dedup-batches-benchmark.mjs compares baseline vs incremental and runs a 40,000-batch differential.

Verification I ran locally: all 880 src/main/ai-vault tests pass, node config/scripts/session-dedup-batches-benchmark.mjs --verify-only reports 40,000 differential batches, and oxlint is clean on the six changed files. The collection selects the same champion as the batch function (identical left-to-right comparison against the current winner), and its index-keyed Map preserves survivor insertion order, so the returned set, order and object identities are preserved.

Pullfrog  | View workflow run | Using openrouter/deepseek/deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@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.

Reviewed changes

  • Reduced per-row occurrence metadata in CodexSessionCollection — bestByKey entries now store indices: number | number[], so an ordinary unique winner keeps a single numeric index and an array is allocated only once the identical winning object repeats. Both delete paths branch on the same typeof guard, so winner selection, insertion order, duplicate-object retention, host/WSL isolation and scan budgets are unchanged. The classes' previous one-element-array allocation per surviving row goes away.
  • Added an ordering regression test — codex-session-collection.test.ts now exercises a batch sequence that alternates single and repeated winning occurrences, asserting parity (set, order, identity) against the unchanged batch deduplicator after every batch; the test can fail if the numeric/array collapse mishandled ordering.

I re-ran codex-session-collection.test.ts (7 pass) and session-scanner-dedup-batches.test.ts (3 pass) against 7fd3aae; both green. The scanners' call sites (size, values(), Array.from(sessions.values(), …)) are unchanged in behavior and covered by the existing suite.

Pullfrog  | View workflow run | Using openrouter/deepseek/deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@nwparker
nwparker merged commit 59d643c into main Sep 13, 2026
28 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.

2 participants