diff --git a/src/renderer/src/lib/orca-cloud-session-events.ts b/src/renderer/src/lib/orca-cloud-session-events.ts index fa63ee5a314..d49eb80492e 100644 --- a/src/renderer/src/lib/orca-cloud-session-events.ts +++ b/src/renderer/src/lib/orca-cloud-session-events.ts @@ -22,7 +22,12 @@ import { findWorktreeById } from '@/store/github/refresh/worktree-refresh' import { runSleepWorktree } from '@/components/sidebar/workspace-actions/sleep/sleep-worktree-flow' import { openPlansDialog } from '@/components/billing/plans/plans-dialog-store' import { activateWorktreeFromSidebar } from '@/lib/workspace/activation/sidebar-worktree-activation' -import { installOrcaCloudWorkspaceStore } from '@/lib/orca-cloud/orca-cloud-workspace-store' +import { + getCloudWorkspaceSnapshot, + installOrcaCloudWorkspaceStore, + refreshCloudWorkspaceStatus +} from '@/lib/orca-cloud/orca-cloud-workspace-store' +import { installCloudActivityRefresh } from '@/lib/orca-cloud/cloud-guard-kill-refresh' import { installCloudPredictiveWake } from '@/lib/orca-cloud/install-cloud-predictive-wake' import { installCloudSetupSession } from '@/lib/orca-cloud/install-cloud-setup-session' import { notePredictiveWakeIdleSlept } from '@/lib/orca-cloud/cloud-predictive-wake' @@ -105,6 +110,13 @@ export function installOrcaCloudSessionEvents(): () => void { const disposeRunActionWake = window.api?.orcaCloudWorkspace?.onWakeProgress?.(syncRunActionsWithWake) ?? (() => {}) const disposeStore = installOrcaCloudWorkspaceStore() + // MEMGUARD: someone came back (focus) or is typing: re-read the active cloud workspace so a guard + // kill's banner shows now (throttled to one read per 15 s). + const disposeActivity = installCloudActivityRefresh({ + activeWorkspaceId: () => useAppStore.getState().activeWorktreeId, + isRunningCloud: (id) => getCloudWorkspaceSnapshot().status[id]?.status?.state === 'ready', + refresh: (id) => void refreshCloudWorkspaceStatus(id) + }) const disposePaired = installPairedCloudWorkspaceRegistry() const disposePredictive = installCloudPredictiveWake() const disposeSetup = installCloudSetupSession() @@ -114,6 +126,7 @@ export function installOrcaCloudSessionEvents(): () => void { disposeState() disposeIdle() disposeStore() + disposeActivity() disposePaired() disposeRunActionWake() disposePredictive() diff --git a/src/renderer/src/lib/orca-cloud/cloud-guard-kill-refresh.test.ts b/src/renderer/src/lib/orca-cloud/cloud-guard-kill-refresh.test.ts new file mode 100644 index 00000000000..4571700e106 --- /dev/null +++ b/src/renderer/src/lib/orca-cloud/cloud-guard-kill-refresh.test.ts @@ -0,0 +1,32 @@ +import { describe, expect, it, vi } from 'vitest' +import { ACTIVITY_REFRESH_MS, installCloudActivityRefresh } from './cloud-guard-kill-refresh' + +describe('installCloudActivityRefresh', () => { + it('re-reads the active running cloud workspace on focus or typing, throttled', () => { + const target = new EventTarget() + let now = 0 + let running = true + const refresh = vi.fn() + const dispose = installCloudActivityRefresh({ + activeWorkspaceId: () => 'ws', + isRunningCloud: () => running, + refresh, + target, + now: () => now + }) + target.dispatchEvent(new Event('focus')) + target.dispatchEvent(new Event('keydown')) + expect(refresh).toHaveBeenCalledTimes(1) + now = ACTIVITY_REFRESH_MS + target.dispatchEvent(new Event('keydown')) + expect(refresh).toHaveBeenCalledTimes(2) + now = 3 * ACTIVITY_REFRESH_MS + running = false + target.dispatchEvent(new Event('focus')) + expect(refresh).toHaveBeenCalledTimes(2) + dispose() + running = true + target.dispatchEvent(new Event('focus')) + expect(refresh).toHaveBeenCalledTimes(2) + }) +}) diff --git a/src/renderer/src/lib/orca-cloud/cloud-guard-kill-refresh.ts b/src/renderer/src/lib/orca-cloud/cloud-guard-kill-refresh.ts new file mode 100644 index 00000000000..99db125d1a6 --- /dev/null +++ b/src/renderer/src/lib/orca-cloud/cloud-guard-kill-refresh.ts @@ -0,0 +1,40 @@ +// MEMGUARD: the guard-kill banner should follow the kill closely, not the next minute's poll. The +// VM rewrites the guard's state at the kill, and main's OOM tracker observes every status read +// (hosted-workspace-service getStatus), so all it takes is a read soon after: re-read the active +// cloud workspace when the window regains focus or the user types, throttled. + +export const ACTIVITY_REFRESH_MS = 15_000 + +/** + * Re-reads the active cloud workspace's status on window focus and on typing, at most once per + * ACTIVITY_REFRESH_MS per workspace, and only while it runs (a sleeping VM is never probed). + */ +export function installCloudActivityRefresh(deps: { + activeWorkspaceId: () => string | null + isRunningCloud: (workspaceId: string) => boolean + refresh: (workspaceId: string) => void + target?: Pick + now?: () => number +}): () => void { + const target = deps.target ?? window + const now = deps.now ?? Date.now + const lastAt = new Map() + const onActivity = (): void => { + const id = deps.activeWorkspaceId() + if (!id || !deps.isRunningCloud(id)) { + return + } + const at = now() + if (at - (lastAt.get(id) ?? -Infinity) < ACTIVITY_REFRESH_MS) { + return + } + lastAt.set(id, at) + deps.refresh(id) + } + target.addEventListener('focus', onActivity) + target.addEventListener('keydown', onActivity, { capture: true, passive: true }) + return () => { + target.removeEventListener('focus', onActivity) + target.removeEventListener('keydown', onActivity, { capture: true }) + } +}