Skip to content

Refresh AI Vault session list on window refocus - #7075

Merged
AmethystLiang merged 4 commits into
mainfrom
ai-vault-refresh-on-refocus
Jul 2, 2026
Merged

AmethystLiang merged 4 commits into
mainfrom
ai-vault-refresh-on-refocus

Conversation

@AmethystLiang

Copy link
Copy Markdown
Contributor

Summary

Refreshes the AI Vault session list when the application window regains focus or the document becomes visible again. This ensures sessions started while the app was backgrounded are visible when returning.

To keep the experience smooth:

  • Background refocus refreshes do not set the loading state, avoiding spinner flashes.
  • Hook skips updating state if the main process re-scans return a cache hit with the same scannedAt timestamp.

Screenshots

No visual change

Testing

  • pnpm lint
  • pnpm typecheck
  • pnpm test
  • pnpm build
  • Added or updated high-quality tests that would catch regressions, or explained why tests were not needed

Added unit tests in ai-vault-session-refresh.test.ts to test window/document listeners, tab visibility change, unmount cleanup, and cache hit state-skipping.

AI Review Report

Reviewed focus behavior. Addressed the risk of redundant React renders during rapid window focus toggles by tracking lastAppliedScanRef (scope paths and scannedAt timestamp) to drop state updates on cache hits. Cleaned up listeners on unmount. Standard browser/DOM APIs are compatible across macOS, Windows, and Linux.

Security Audit

No security impact. Simply invokes the existing window.api.aiVault.listSessions IPC channel. No inputs are collected, and no shell commands or path operations are performed.

Notes

None.

AmethystLiang and others added 2 commits July 1, 2026 22:29
Sessions started after the panel mounted never appeared until a manual
refresh, since the hook only scanned on mount and scope changes. Listen
for window focus and visibilitychange (to visible) while the panel is
mounted and trigger a non-force re-scan, letting the main process's 15s
scan cache rate-limit rapid focus flips. The manual refresh button keeps
its force (cache-bypassing) behavior.

Co-authored-by: Orca <help@stably.ai>
Refocus refreshes now run as background: the loading flag stays down (no
spinner flash on every alt-tab back), and when the main process replays
the cached snapshot (same scope key + scannedAt) the state updates are
skipped entirely so nothing re-renders. Fresh scans and the manual force
refresh apply results exactly as before.

Co-authored-by: Orca <help@stably.ai>
@coderabbitai

coderabbitai Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: e0ca5731-4bf8-4fed-aade-1963afb1a4c5

📥 Commits

Reviewing files that changed from the base of the PR and between 13739ae and 9366658.

📒 Files selected for processing (6)
  • src/main/ipc/ai-vault.ts
  • src/preload/api-types.ts
  • src/preload/index.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-refresh.test.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-refresh.ts
  • src/renderer/src/web/web-preload-api.ts

📝 Walkthrough

Walkthrough

The AI Vault refresh hook now supports background refresh calls, forced-rescan throttling, and cache short-circuiting for repeated scans in the same scope. A new IPC path sends aiVault:windowFocused from the main process through preload to the renderer, where the hook uses it alongside visibilitychange to trigger background rescans. The test suite adds coverage for focus/visibility behavior, cleanup, throttling, cache reuse, and agent-session-driven rescans.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description is mostly complete, but the AI Review Report omits the required explicit cross-platform compatibility checks. Add explicit review notes for macOS, Linux, and Windows, including shortcuts, labels, paths, shell behavior, and any Electron-specific differences.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: refreshing the AI Vault session list on window refocus.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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
📝 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.

Caution

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

⚠️ Outside diff range comments (1)
src/renderer/src/components/right-sidebar/ai-vault-session-refresh.ts (1)

30-68: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Prevent stale scoped results from applying after scopePaths changes.

A scope change during an in-flight scan only queues another refresh; it does not invalidate the active refreshId. The old scan can still call setScanResult/setSessions, briefly showing sessions for the previous scope before the queued scan finishes.

Proposed fix
-    const scopeKey = scopePathsRef.current.join('\n')
+    const requestScopePaths = [...scopePathsRef.current]
+    const scopeKey = requestScopePaths.join('\n')
     try {
       const result = await window.api.aiVault.listSessions({
         limit: SESSION_LIMIT,
-        scopePaths: scopePathsRef.current,
+        scopePaths: requestScopePaths,
         force: args.force
       })
       if (!mountedRef.current || refreshIdRef.current !== refreshId) {
         return
       }
+      if (scopePathsRef.current.join('\n') !== scopeKey) {
+        pendingRefreshRef.current = true
+        pendingBackgroundRef.current = false
+        return
+      }
       // A cache hit returns the snapshot already on screen; skip the state

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b2d3a02d-ea9e-4d46-a89b-71cd73e53598

📥 Commits

Reviewing files that changed from the base of the PR and between 7d3944c and 13739ae.

📒 Files selected for processing (2)
  • src/renderer/src/components/right-sidebar/ai-vault-session-refresh.test.ts
  • src/renderer/src/components/right-sidebar/ai-vault-session-refresh.ts

AmethystLiang and others added 2 commits July 1, 2026 23:06
Non-force refreshes were served the 15s-old cached snapshot, so a
session started right before re-entering the panel or refocusing the
window still didn't appear — only the manual force refresh showed it.
Panel entry and refocus now request a force scan, throttled in module
scope to one forced scan per 5s (surviving panel remounts), so rapid
tab/focus flips still resolve from the main-process cache. Manual force
refreshes count against the throttle to avoid back-to-back full scans.

Co-authored-by: Orca <help@stably.ai>
Renderer DOM focus/visibility events never fire on macOS app activation
(verified live: document.hasFocus() stays true and no focus/blur event
lands when the window loses/gains OS focus), so the refocus refresh was
inert. Main now broadcasts browser-window-focus to the renderer as
aiVault:windowFocused and the hook subscribes to that instead.

Sessions started inside Orca (window never blurs) get their own trigger
from agent hooks: re-scan only when an unseen provider session id
appears in agent status. State transitions and message pings on known
sessions are deliberately ignored — keying on them made the panel churn
on every AI message. Event-driven rescans that land inside the 5s
throttle window defer to one trailing scan instead of being dropped.

Verified end-to-end against a dev instance: a headless claude session
appeared in the panel on OS-level refocus and another on panel
re-entry, without the manual refresh button.

Co-authored-by: Orca <help@stably.ai>
@AmethystLiang
AmethystLiang merged commit 6142ec1 into main Jul 2, 2026
1 of 2 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
* Refresh AI Vault session list when the window regains focus

Sessions started after the panel mounted never appeared until a manual
refresh, since the hook only scanned on mount and scope changes. Listen
for window focus and visibilitychange (to visible) while the panel is
mounted and trigger a non-force re-scan, letting the main process's 15s
scan cache rate-limit rapid focus flips. The manual refresh button keeps
its force (cache-bypassing) behavior.

Co-authored-by: Orca <help@stably.ai>

* Keep AI Vault refocus refresh render-free on cache hits

Refocus refreshes now run as background: the loading flag stays down (no
spinner flash on every alt-tab back), and when the main process replays
the cached snapshot (same scope key + scannedAt) the state updates are
skipped entirely so nothing re-renders. Fresh scans and the manual force
refresh apply results exactly as before.

Co-authored-by: Orca <help@stably.ai>

* Bypass AI Vault scan cache on panel entry and refocus, throttled

Non-force refreshes were served the 15s-old cached snapshot, so a
session started right before re-entering the panel or refocusing the
window still didn't appear — only the manual force refresh showed it.
Panel entry and refocus now request a force scan, throttled in module
scope to one forced scan per 5s (surviving panel remounts), so rapid
tab/focus flips still resolve from the main-process cache. Manual force
refreshes count against the throttle to avoid back-to-back full scans.

Co-authored-by: Orca <help@stably.ai>

* Deliver AI Vault refocus via main-process signal; calm in-app triggers

Renderer DOM focus/visibility events never fire on macOS app activation
(verified live: document.hasFocus() stays true and no focus/blur event
lands when the window loses/gains OS focus), so the refocus refresh was
inert. Main now broadcasts browser-window-focus to the renderer as
aiVault:windowFocused and the hook subscribes to that instead.

Sessions started inside Orca (window never blurs) get their own trigger
from agent hooks: re-scan only when an unseen provider session id
appears in agent status. State transitions and message pings on known
sessions are deliberately ignored — keying on them made the panel churn
on every AI message. Event-driven rescans that land inside the 5s
throttle window defer to one trailing scan instead of being dropped.

Verified end-to-end against a dev instance: a headless claude session
appeared in the panel on OS-level refocus and another on panel
re-entry, without the manual refresh button.

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
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