Skip to content

perf(ai-vault): reuse session list identity on reminted scannedAt - #14245

Merged
nwparker merged 2 commits into
mainfrom
nwparker/ai-vault-session-list-identity
Aug 13, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/ai-vault-session-list-identity

Conversation

@nwparker

@nwparker nwparker commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Agent History (useAiVaultSessionRefresh) only skipped apply when scannedAt matched. That stamp is a wall-clock scan time, not a content fingerprint:

  1. IPC listSessions always structured-clones nested rows (previewMessages, subagent).
  2. After the 15s all-host / 60s local TTL, a real rescan remints scannedAt even when every transcript is unchanged.
  3. mergeAiVaultListResults then stamped new Date().toISOString() on every all-host merge, so a cache-hit 'all' result looked fresh even inside per-leg TTL.

Alt-tab with the vault panel open therefore always setSessions'd a new array. AiVaultPanel depends on sessions for sessionProjectById, useAiVaultSessionWorktreeMap (path-match per session × worktree), filter/sort/groups, and the virtual list.

This reconciles nested session rows structurally before setState and keeps the latest input scannedAt on merge. A reminted no-op keeps the previous result, row, and previewMessages refs so those memos stay cold.

Follow-up to the store-catalog identity work in #13744 / #14211–#14217.

Why a partial fix is inert

Identity has to survive every link from fetch to setState:

  1. IPC always returns freshly cloned nested records.
  2. All-host merge reminted scannedAt even when every leg was a cache hit.
  3. The panel guard compared only scannedAt.

Fixing only (2) still misses a TTL-expired rescan of identical disk contents. Fixing only (3) with Object.is never matches after structuredClone. The apply path has to structurally reuse rows (and the whole result when nothing moved).

scannedAt is not shown in the UI. Keeping the previous result object on a no-op (old stamp included) is what makes React bail out. lastAppliedScanRef still records the incoming stamp so the cheap string guard works on the next identical-stamp hit.

Reference compare fails on IPC clones

AiVaultSession is deeply nested. After structuredClone, sessions === previous and row === previousRow never hold even when every field is unchanged. The write path uses reuseEqualCatalogRows / areValuesEqual and returns current when sessions, issues, and cancelled are unchanged.

Tests

src/renderer/src/components/right-sidebar/ai-vault-session-identity.test.ts and ai-vault-session-refresh.test.ts:

  • Production-shaped nested fixtures (previewMessages[], subagent, previewMessagesTruncated) cloned as distinct objects via structuredClone with a reminted scannedAt.
  • Assert scanResult === previous, sessions === previous, and nested previewMessages identity.
  • A real nested change (previewMessages[0].text) produces a new array but reuses the unchanged sibling row.
  • Empty reminted snapshot (the previous test expected this to churn — that was the bug) now keeps identity.

src/main/ai-vault/session-list-results.test.ts:

  • All-host merge keeps the latest input scannedAt instead of restamping Date.now().

Scalar {id, title} fixtures are not the only coverage. Reverting the reuse write or the merge stamp makes these assertions go red.

Measured impact

How often the churn fired. Every window focus / visibilitychange while the panel is open re-issues listSessions, and pre-change the only thing standing between that and a full re-apply was scannedAt equality:

  • Local-only scope — the local scan cache (60s, cached-session-list.ts:20) replays its original stamp on a hit, so the list only churned when a refocus landed past that TTL.
  • All-host scope — the merged result is cached 15s (ai-vault-host-leg-cache.ts:8), but every fresh merge restamped Date.now(). So any alt-tab back more than 15s after the last one re-applied the whole list, even when every leg was a cache hit and nothing on disk had changed. For someone tabbing between Orca and a browser while agents run, that is effectively every return — order of 100-200 times per hour of active use.

What each churn cost. Marginal cost, excluding the structuredClone the IPC boundary pays either way:

sessions x worktrees reconcile (new) sessionProjectById session->worktree map net saved
250 x 40 (default limit) 0.43 ms 0.49 ms 0.44 ms ~0.5 ms
500 x 100 0.94 ms 1.46 ms 1.54 ms ~2.1 ms
1000 x 200 1.8 ms 5.96 ms 6.14 ms ~10.3 ms

The shape matters more than the absolute numbers. Both maps are O(sessions x worktree candidates) — resolveWorktreeInfoFromCandidates runs two full .filter() passes plus a .sort() over every candidate, per session — while the reconcile is O(sessions x fields). So the gain is roughly 2x at the default limit and widens fast for heavy users.

Two caveats: this covers only the two map memos, and the part users actually feel — the React re-render and re-measure of the virtualized list plus every row's props — is not in the table, so the real saving is larger than it shows.

Made with Orca 🐋

Refocus after the host-cache TTL remints scannedAt even when every
session row is unchanged, and all-host merge always stamped Date.now().
The panel only skipped apply on scannedAt equality, so Agent History
rebuilt sessionProjectById and the worktree path map on every alt-tab.

Reconcile nested session rows structurally before setState, and keep
the latest input scannedAt on merge so a cache-hit all-host result
stays a no-op.

Co-authored-by: Orca <[email protected]>
@coderabbitai

coderabbitai Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

AI Vault list merging now preserves the latest valid, non-future input scannedAt timestamp and uses the current time when no valid timestamp exists. Identity helpers reuse unchanged results, session rows, nested preview messages, and issues. Refresh flows use the shared publication helper for cached and fresh results. Tests cover timestamp validation, structural sharing, selective row replacement, and appended sessions.

Mergeability Score: 🟡 Moderate · up to 38ff0

The PR improves session identity reuse, but timestamp handling can still misorder or incorrectly accept session-list scans when timestamps include offsets, leading to stale or incorrect merged results. This should be corrected and covered by regression tests before merge.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the problem, solution, and tests, but it omits required template sections, including the linked issue and visual proof. Use the repository template and add the linked issue, visual proof or N/A reason, testing checklist, review considerations, and required checklist items.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main performance change: reusing AI Vault session-list identity when scannedAt is reminted.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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.

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

🧹 Nitpick comments (1)
src/renderer/src/components/right-sidebar/ai-vault-session-identity.ts (1)

5-11: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Replace stale refresh rationale.

These comments describe reminted scannedAt as the only no-op mechanism. This PR adds structural identity reuse, and mergeAiVaultListResults no longer remints all-host timestamps. Keep each comment to one current invariant.

  • src/renderer/src/components/right-sidebar/ai-vault-session-identity.ts#L5-L11: remove the claim that all-host merges always remint scannedAt.
  • src/main/ai-vault/session-list-results.ts#L78-L80: remove the claim that the renderer only skips apply when scannedAt matches.

As per coding guidelines, comments must be concise, non-obvious, and brief—prefer one line; do not explain obvious behavior or walk through code.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 16dc060d-0bab-4e00-8322-a6b982ef7419

📥 Commits

Reviewing files that changed from the base of the PR and between 0ed6db7 and c268649.

📒 Files selected for processing (6)
  • src/main/ai-vault/session-list-results.test.ts
  • src/main/ai-vault/session-list-results.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-identity.test.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-identity.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-refresh.test.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-refresh.ts

…esult

A remote leg whose clock is ahead, or whose stamp is not ISO, would pin
the all-host max above every local rescan and freeze the renderer's
scannedAt equality guard — including a manual force refresh. Ignore
future and unparsable stamps.

Also drop the duplicate sessions useState so apply walks the list once
and sessions === scanResult.sessions again.

Co-authored-by: Orca <[email protected]>

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/main/ai-vault/session-list-results.ts (1)

78-100: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Compare parsed timestamps instead of timestamp strings.

Date.parse(stamp) validates an instant, but stamp > now and stamp > latest compare text. A future offset timestamp can pass the future check, and a past offset timestamp can be skipped or selected incorrectly. Compare parsed millisecond values and add offset-form regression cases.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 467dcfd3-1597-4721-8aa6-8a58838343c7

📥 Commits

Reviewing files that changed from the base of the PR and between c268649 and 38ff06d.

📒 Files selected for processing (6)
  • src/main/ai-vault/session-list-results.test.ts
  • src/main/ai-vault/session-list-results.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-identity.test.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-identity.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-refresh.test.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-refresh.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/renderer/src/components/right-sidebar/ai-vault-session-identity.test.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-refresh.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-refresh.test.ts

@nwparker
nwparker merged commit 0add035 into main Aug 13, 2026
86 of 88 checks passed
@nwparker
nwparker deleted the nwparker/ai-vault-session-list-identity branch August 13, 2026 08:55
nwparker added a commit that referenced this pull request Aug 13, 2026
…#14261)

Follow-up to #14245. That PR made `scannedAt` meaningful again by keeping
the newest leg stamp on an all-host merge instead of reminting it, and made
the renderer reconcile session rows structurally. Three loose ends:

- An 'all' result is a merge of legs on independent clocks stamped with the
  newest leg, so the renderer's `scannedAt` equality guard could hard-skip a
  real change: a paired host whose clock lags the desktop can return new
  sessions while the merged stamp repeats, hiding them for up to the local
  leg's 60s TTL. Restrict the guard to single-host scopes, where the stamp is
  minted by one scanner at scan completion and equality does imply equal
  content. Merged scopes now let the structural reconcile decide, which costs
  one deep compare per refocus and keeps the identity reuse intact.
  The scope test routes through `normalizeExecutionHostScope` so it matches
  what the main process merges on, including an empty or unrecognized scope.

- `latestAiVaultScannedAt` compared stamps lexicographically after only
  checking `Date.parse` validity. `scannedAt` is `z.string()` on the wire, so
  a legal ISO variant orders wrongly: '...:05Z' sorts after '...:05.500Z',
  and a '-05:00' offset five hours in the future sorts below the local stamp
  and would have been accepted. Compare parsed instants instead.

- `EMPTY_AI_VAULT_SESSIONS` is shared by every mounted hook; freeze it and
  return `readonly AiVaultSession[]` so no consumer can mutate the sentinel.

Each test fails against the pre-fix logic.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…ablyai#14245)

* perf(ai-vault): reuse session list identity on reminted scannedAt

Refocus after the host-cache TTL remints scannedAt even when every
session row is unchanged, and all-host merge always stamped Date.now().
The panel only skipped apply on scannedAt equality, so Agent History
rebuilt sessionProjectById and the worktree path map on every alt-tab.

Reconcile nested session rows structurally before setState, and keep
the latest input scannedAt on merge so a cache-hit all-host result
stays a no-op.

Co-authored-by: Orca <[email protected]>

* perf(ai-vault): clamp merged scannedAt and derive sessions from scanResult

A remote leg whose clock is ahead, or whose stamp is not ISO, would pin
the all-host max above every local rescan and freeze the renderer's
scannedAt equality guard — including a manual force refresh. Ignore
future and unparsable stamps.

Also drop the duplicate sessions useState so apply walks the list once
and sessions === scanResult.sessions again.

Co-authored-by: Orca <[email protected]>

---------

Co-authored-by: Orca <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…stablyai#14261)

Follow-up to stablyai#14245. That PR made `scannedAt` meaningful again by keeping
the newest leg stamp on an all-host merge instead of reminting it, and made
the renderer reconcile session rows structurally. Three loose ends:

- An 'all' result is a merge of legs on independent clocks stamped with the
  newest leg, so the renderer's `scannedAt` equality guard could hard-skip a
  real change: a paired host whose clock lags the desktop can return new
  sessions while the merged stamp repeats, hiding them for up to the local
  leg's 60s TTL. Restrict the guard to single-host scopes, where the stamp is
  minted by one scanner at scan completion and equality does imply equal
  content. Merged scopes now let the structural reconcile decide, which costs
  one deep compare per refocus and keeps the identity reuse intact.
  The scope test routes through `normalizeExecutionHostScope` so it matches
  what the main process merges on, including an empty or unrecognized scope.

- `latestAiVaultScannedAt` compared stamps lexicographically after only
  checking `Date.parse` validity. `scannedAt` is `z.string()` on the wire, so
  a legal ISO variant orders wrongly: '...:05Z' sorts after '...:05.500Z',
  and a '-05:00' offset five hours in the future sorts below the local stamp
  and would have been accepted. Compare parsed instants instead.

- `EMPTY_AI_VAULT_SESSIONS` is shared by every mounted hook; freeze it and
  return `readonly AiVaultSession[]` so no consumer can mutate the sentinel.

Each test fails against the pre-fix logic.
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