feat(cloud): guard-kill banner follows the kill: any status read raises it; focus or typing re-reads the active cloud workspace

MEMGUARD follow-up. orca-memguard already rewrites its state file at the kill, so the next metrics
probe carries it; the desktop now asks sooner and acts on the first read that shows it.
- Any status read (panel poll, row mount, wake) that shows a newer guard kill raises the notice
  directly, with the same count main's OOM tracker uses, so a dismissal holds for both.
- Window focus or a keydown re-reads the active running cloud workspace, at most once per 15 s.
This commit is contained in:
Neil
2026-11-15 04:02:54 -08:00
parent f75ab574eb
commit 4cd19efcfd
4 changed files with 169 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,73 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { CloudWorkspaceStatus } from '../../../../shared/hosted-sandbox/cloud-workspace-api'
import {
ACTIVITY_REFRESH_MS,
guardKillFromStatus,
installCloudActivityRefresh,
resetGuardKillSeen
} from './cloud-guard-kill-refresh'
const status = (guardKills: number, lastGuardKillAt: number | null): CloudWorkspaceStatus => ({
state: 'ready',
metrics: null,
memory: {
pressure: 'ok',
guardKills,
lastGuardVictim: guardKills ? 'tsc' : null,
lastGuardKillAt,
guarded: true
},
uptimeMs: 0,
usage: { totalMs: 0, centsPerHour: 75, costCents: 0 },
archive: null,
checkedAt: 1
})
beforeEach(() => resetGuardKillSeen())
describe('guardKillFromStatus', () => {
it('raises each guard kill once, from whichever read sees it first', () => {
expect(guardKillFromStatus('ws', status(0, null))).toBeNull()
expect(guardKillFromStatus('ws', status(1, 1_000), 5)).toEqual({
workspaceId: 'ws',
oomKills: 1,
victim: 'tsc',
stoppedBy: 'guard',
at: 5
})
expect(guardKillFromStatus('ws', status(1, 1_000))).toBeNull()
expect(guardKillFromStatus('ws', status(2, 2_000))?.oomKills).toBe(2)
// Another workspace keeps its own memory.
expect(guardKillFromStatus('other', status(1, 1_000))).not.toBeNull()
})
})
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,76 @@
// MEMGUARD: the guard-kill banner should follow the kill closely, not the next minute's poll.
// Two pieces: (1) any status read that shows a newer guard kill raises the notice right away
// (main's minute-long OOM poll announces the same count later, which changes nothing); (2) the
// active cloud workspace's status is re-read when the window regains focus or the user types,
// throttled, so the read that carries the kill happens when someone is there to see it.
import type {
CloudOomEvent,
CloudWorkspaceStatus
} from '../../../../shared/hosted-sandbox/cloud-workspace-api'
const seenKillAt = new Map<string, number>()
/** The notice for a guard kill this status shows and no earlier read did; null otherwise. */
export function guardKillFromStatus(
workspaceId: string,
status: CloudWorkspaceStatus,
now = Date.now()
): CloudOomEvent | null {
const memory = status.memory
if (!memory || memory.guardKills === 0 || memory.lastGuardKillAt === null) {
return null
}
if (memory.lastGuardKillAt <= (seenKillAt.get(workspaceId) ?? 0)) {
return null
}
seenKillAt.set(workspaceId, memory.lastGuardKillAt)
return {
workspaceId,
// The count main's tracker uses (kernel + guard), so a dismissal holds for both sources.
oomKills: (status.metrics?.oomKills ?? 0) + memory.guardKills,
victim: memory.lastGuardVictim,
stoppedBy: 'guard',
at: now
}
}
/** Test seam. */
export function resetGuardKillSeen(): void {
seenKillAt.clear()
}
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 })
}
}
@@ -23,6 +23,7 @@ import type {
OrcaCloudWorkspaceApi
} from '../../../../shared/hosted-sandbox/cloud-workspace-api'
import { eachLimited } from './each-limited'
import { guardKillFromStatus } from './cloud-guard-kill-refresh'
import { noteCloudGitSetupRead, noteCloudGitSetupReady } from './cloud-git-setup-store'
import { WAKE_FAILED_STALE_MS, WAKE_STALE_MS } from './orca-cloud-workspace-state'
import { isSilentPredictiveWake, predictiveWakeStartedAt } from './cloud-predictive-wake'
@@ -182,6 +183,11 @@ function applyStatus(
result: CloudWorkspaceResult<CloudWorkspaceStatus>
): void {
noteCloudGitSetupRead(workspaceId, !result.ok || result.value.gitSetupFailed === true)
// MEMGUARD: a read that shows a new guard kill raises its notice now, not on main's next poll.
const guardKill = result.ok ? guardKillFromStatus(workspaceId, result.value) : null
if (guardKill) {
noteCloudOom(guardKill)
}
updateCloudWorkspaceSnapshot((s) => {
const previous = s.status[workspaceId]
if (!result.ok && result.code === 'not_cloud') {