Merge pull request #788 from nwparker/cloud/memguard-prompt-banner

feat(cloud): guard-kill banner follows the kill (focus/typing status refresh)
This commit is contained in:
Neil
2026-11-15 04:12:27 -08:00
committed by GitHub
3 changed files with 86 additions and 1 deletions
@@ -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()
@@ -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)
})
})
@@ -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<EventTarget, 'addEventListener' | 'removeEventListener'>
now?: () => number
}): () => void {
const target = deps.target ?? window
const now = deps.now ?? Date.now
const lastAt = new Map<string, number>()
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 })
}
}