Skip to content

perf(ai-vault): count recent sessions for scan cutoffs - #20301

Merged
nwparker merged 1 commit into
mainfrom
OrcaWin/codex-perf-09-11-session-scan-cutoff
Sep 13, 2026
Merged

nwparker merged 1 commit into
mainfrom
OrcaWin/codex-perf-09-11-session-scan-cutoff

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Sep 12, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​198 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​198
Prod 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​241 $\color{#cf222e}{\Huge{\mathbf{−}}}$​35 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​206

Summary

Replace repeated timestamp sorting in the native and remote AI Vault scan cutoff with an equivalent count: the Nth-largest timestamp is strictly newer than the next candidate mtime exactly when at least N timestamps are strictly newer.

Stacked on #20255. The base branch is OrcaWin/codex-perf-09-11-session-dedup-batches, so this PR contains only the cutoff improvement. Merge after that PR; retarget to main if needed. This is a separate improvement, not a combined performance PR.

The identical native/remote cutoff implementations now share session-scan-cutoff.ts. Every call examines the current collection, so alias replacements that lower a winning timestamp remain correct without new cached state. Equal mtimes still do not stop the scan. Unlimited/insufficient-session scans still return before visiting rows.

Preserves the exact original sort for NaN timestamps and unusual numeric limits. Parses timestamps once and reuses the same temporary numeric array for that fallback. No change to discovery bounds, batch sizes, cancellation/yields, scope backfill, returned sessions/order, SSH execution ownership, or wire content. No git-worktree assumption.

Measured result

Actual baseline private cutoff selected from the base source using the TypeScript AST; actual current cutoff, timestamp parser and session collection bundled with esbuild. Node 26.6.0 / macOS arm64, eight counterbalanced pairs. Median CPU time:

Workload Before After
8 ordered timestamps, stop 1.096 µs 1.026 µs
1,000 ordered timestamps, continue 0.116 ms 0.113 ms
1,000 shuffled timestamps, continue 0.195 ms 0.119 ms
2,000 shuffled timestamps, continue 0.428 ms 0.234 ms
10,000 shuffled timestamps, continue 2.456 ms 1.126 ms
2,000-candidate scan cutoff + admission, limit 1,000 311.9 ms 175.0 ms

The scanner already permits up to 2 * limit candidates and checks the cutoff after every post-limit candidate. The cumulative workload uses that existing policy. These are synthetic cutoff/admission timings, excluding discovery, file/network reads, parsing and final sorting—not an app-wide latency claim. Nearly ordered timestamps benefit much less than shuffled ones.

Malformed-input disclosure: 1,000 timestamps with an invalid first timestamp: 0.128 -> 0.126 ms; invalid last timestamp: 0.119 -> 0.126 ms. The initial allocation-free prototype reparsed earlier timestamps on that fallback and regressed to 0.228 ms; that prototype was replaced before publication. Numeric-array allocation/retention remains the same as the baseline.

Verification

  • 888 tests / 114 AI Vault files pass, including native/remote cold+warm scanner tests.
  • Deterministic scanner regression: original cutoff performs 64 timestamp sorts across 128 candidates; current performs zero, with exactly the same selected sessions. Final result sorting is unchanged.
  • 40,000 differential batch cutoffs preserve decisions and collection order across duplicate objects, preferred/losing aliases, lowered timestamps, multiple execution hosts, equal mtimes, invalid/empty dates, nullish fallback, negative/extended-year dates, non-finite mtimes and numeric-limit edge cases.
  • Unit regressions cover failed early-stop with an open cutoff, preferred older alias replacements, repeated winner occurrences, and parsing timestamps only once on a late invalid date.
  • pnpm tc:node, changed-code quality gate against fdda0e835b, and formatting pass.
  • All tests use ORCA_BACKGROUND_LAUNCH=1; no app windows launched.

Reproduce:

git show fdda0e835b:src/main/ai-vault/session-scanner.ts | ORCA_BACKGROUND_LAUNCH=1 node config/scripts/session-scan-cutoff-benchmark.mjs
ORCA_BACKGROUND_LAUNCH=1 pnpm test src/main/ai-vault
ORCA_BACKGROUND_LAUNCH=1 pnpm tc:node
ORCA_BACKGROUND_LAUNCH=1 ORCA_CODE_QUALITY_BASE=fdda0e835b pnpm run check:code-quality:changed

Tracking: #20206. Does not close the broader performance audit.

Independent review follow-up

This branch now includes #20255's occurrence-index memory mitigation via forward update 1239f0830432479b80ec2334b475f54154d74c36; history was not rewritten. The diff against the updated #20255 branch remains the same six cutoff files.

Fresh verification of the combined dedup + cutoff branch: 616 tests across 90 AI Vault files, 40,000 cutoff differential cases, Node typecheck, and changed-code quality gate against the updated base all pass. No app windows launched.

Merge order remains mandatory: merge #20255 first, then retarget/revalidate this PR against main. This review did not merge either PR. The memory mitigation is tested here already, but it does not remove that operational dependency.

@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

  • Shared scan cutoff helper — new src/main/ai-vault/session-scan-cutoff.ts replaces the per-candidate sort with an O(n) count (newerCount >= limit), which is exactly the Nth-largest > nextCandidate condition.
  • Native + remote wiring — session-scanner.ts and remote-session-scanner.ts drop their duplicate private cutoff functions and import the shared one, preserving the sessions.size < limit / undefined-mtime early return.
  • Fallback fidelity — non-integer / <= 0 / NaN / Infinity limits and any NaN timestamp route back to the exact legacy sort, so equal mtimes still never stop the scan and unusual limits keep their original semantics.
  • Differential tests — session-scan-cutoff.test.ts cross-checks the new function against a sort-based reference across invalid dates, extended years, negative limits and lowered-alias replacements; session-scanner-cutoff.test.ts asserts zero numeric sorts across a real native and remote scan. I confirmed the scanner test genuinely fails against the baseline implementation (64 numeric sorts), so it is not a tautology.
  • Benchmark script — config/scripts/session-scan-cutoff-benchmark.mjs runs 40k randomized differential cutoff calls and reports counterbalanced CPU timings.

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

@nwparker
nwparker changed the base branch from OrcaWin/codex-perf-09-11-session-dedup-batches to main September 13, 2026 01:18
@nwparker
nwparker force-pushed the OrcaWin/codex-perf-09-11-session-scan-cutoff branch from 1239f08 to 674376b Compare September 13, 2026 01:24
@coderabbitai

coderabbitai Bot commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 36 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: c4056648-923b-415a-9f5c-9f071cf31e27

📥 Commits

Reviewing files that changed from the base of the PR and between fbab61e and 674376b.

📒 Files selected for processing (6)
  • config/scripts/session-scan-cutoff-benchmark.mjs
  • src/main/ai-vault/remote-session-scanner.ts
  • src/main/ai-vault/session-scan-cutoff.test.ts
  • src/main/ai-vault/session-scan-cutoff.ts
  • src/main/ai-vault/session-scanner-cutoff.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.

@nwparker
nwparker merged commit 25a1259 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