mirror of
https://github.com/stablyai/orca.git
synced 2026-11-15 18:37:24 +00:00
Merge pull request #422 from nwparker/lc2/asleep-mark-after-held-wake
fix(sleep): tabs wake after Wake to continue on the held asleep view (#397 x #399)
This commit is contained in:
+64
@@ -0,0 +1,64 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
|
||||
vi.mock('@/store', () => ({ useAppStore: { getState: () => ({}), setState: () => {} } }))
|
||||
vi.mock('@/store/slices/browser/cleanup/browser-webview-cleanup', () => ({
|
||||
destroyWorktreeBrowserGuests: () => {}
|
||||
}))
|
||||
|
||||
const { isBackAfterHeldSleep, isBrowserSandboxUp } = await import('./browser-tab-sleep')
|
||||
|
||||
const WT = 'wt'
|
||||
type Flags = { held?: boolean; goingToSleep?: boolean; suspendCommitted?: boolean }
|
||||
const view = (flags: Flags) => ({
|
||||
held: () => Boolean(flags.held),
|
||||
goingToSleep: () => Boolean(flags.goingToSleep),
|
||||
suspendCommitted: () => Boolean(flags.suspendCommitted)
|
||||
})
|
||||
const state = (vm: 'running' | 'suspended', waking = false) => {
|
||||
const wakingIds: Record<string, boolean> = {}
|
||||
if (waking) {
|
||||
wakingIds[WT] = true
|
||||
}
|
||||
return {
|
||||
activeWorktreeId: WT,
|
||||
asleepBrowserWorktreeIds: { [WT]: 1 },
|
||||
ephemeralVmRuntimesByWorkspaceId: { [WT]: { status: vm } },
|
||||
wakingEphemeralVmWorkspaceIds: wakingIds
|
||||
}
|
||||
}
|
||||
|
||||
describe('wake pressed on the held asleep view, workspace never re-activated (d3 smoke, #399)', () => {
|
||||
it('keeps the tabs asleep through the suspend and the wake, then wakes them when the VM is up', () => {
|
||||
// Sleep in flight, Wake to continue pressed: still held, suspend being committed.
|
||||
expect(
|
||||
isBackAfterHeldSleep(state('running'), view({ held: true, suspendCommitted: true }))
|
||||
).toBeNull()
|
||||
expect(isBackAfterHeldSleep(state('suspended'), view({ held: true }))).toBeNull()
|
||||
expect(isBackAfterHeldSleep(state('suspended', true), view({}))).toBeNull()
|
||||
// Live: the VM runs and the view is released, with no activation change.
|
||||
expect(isBackAfterHeldSleep(state('running'), view({}))).toBe(WT)
|
||||
})
|
||||
|
||||
it('a lingering sleep intent no longer blocks reopening pages once the VM itself is up', () => {
|
||||
const live = {
|
||||
ephemeralVmRuntimesByWorkspaceId: { [WT]: { status: 'running' } },
|
||||
wakingEphemeralVmWorkspaceIds: {},
|
||||
asleepBrowserWorktreeIds: {}
|
||||
}
|
||||
expect(
|
||||
isBrowserSandboxUp(live, WT, {
|
||||
sleepIntent: true,
|
||||
goingToSleep: false,
|
||||
suspendCommitted: false
|
||||
})
|
||||
).toBe(true)
|
||||
// Without a VM, the sleep intent still decides.
|
||||
expect(
|
||||
isBrowserSandboxUp({ asleepBrowserWorktreeIds: {} }, WT, {
|
||||
sleepIntent: true,
|
||||
goingToSleep: false,
|
||||
suspendCommitted: false
|
||||
})
|
||||
).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -7,7 +7,12 @@
|
||||
import { useAppStore } from '@/store'
|
||||
import { destroyWorktreeBrowserGuests } from '@/store/slices/browser/cleanup/browser-webview-cleanup'
|
||||
import { hasWorktreeSleepIntent } from '@/lib/worktree-sleep-intent'
|
||||
import { isWorktreeGoingToSleep, isWorktreeSuspendCommitted } from '@/lib/worktree-sleep-view'
|
||||
import {
|
||||
isWorktreeGoingToSleep,
|
||||
isWorktreeSleepViewHeld,
|
||||
isWorktreeSuspendCommitted,
|
||||
subscribeWorktreeSleepView
|
||||
} from '@/lib/worktree-sleep-view'
|
||||
import type { BrowserWorkspace } from '../../../../../../shared/browser-workspace-types'
|
||||
|
||||
type BrowserTabsByWorktree = Record<string, BrowserWorkspace[]>
|
||||
@@ -112,7 +117,9 @@ export function isBrowserSandboxUp(
|
||||
): boolean {
|
||||
const runtime = state.ephemeralVmRuntimesByWorkspaceId?.[worktreeId]
|
||||
return (
|
||||
!sleep.sleepIntent &&
|
||||
// A VM's own state says whether it is up; the sleep intent can outlive a wake pressed on the
|
||||
// held view (#399), so it only decides for a workspace with no VM.
|
||||
(runtime !== undefined || !sleep.sleepIntent) &&
|
||||
!sleep.goingToSleep &&
|
||||
!sleep.suspendCommitted &&
|
||||
!state.asleepBrowserWorktreeIds?.[worktreeId] &&
|
||||
@@ -171,18 +178,69 @@ export function wakeWorktreeBrowserTabs(worktreeId: string): void {
|
||||
}
|
||||
|
||||
/** Wakes a workspace's asleep tabs whenever it becomes the active one. */
|
||||
/**
|
||||
* The active workspace is back after a sleep without being re-opened: a wake pressed on the
|
||||
* held asleep view (#331/#399) keeps it selected, so no activation change says so. Its VM is
|
||||
* running, nothing is waking or going to sleep, and the view is no longer held.
|
||||
*/
|
||||
export function isBackAfterHeldSleep(
|
||||
state: {
|
||||
activeWorktreeId: string | null
|
||||
asleepBrowserWorktreeIds?: Record<string, number>
|
||||
ephemeralVmRuntimesByWorkspaceId?: Record<string, { status: string } | undefined>
|
||||
wakingEphemeralVmWorkspaceIds?: Record<string, boolean | undefined>
|
||||
},
|
||||
view: {
|
||||
held: (id: string) => boolean
|
||||
goingToSleep: (id: string) => boolean
|
||||
suspendCommitted: (id: string) => boolean
|
||||
}
|
||||
): string | null {
|
||||
const id = state.activeWorktreeId
|
||||
if (!id || !state.asleepBrowserWorktreeIds?.[id]) {
|
||||
return null
|
||||
}
|
||||
const runtime = state.ephemeralVmRuntimesByWorkspaceId?.[id]
|
||||
const up =
|
||||
runtime?.status === 'running' &&
|
||||
!state.wakingEphemeralVmWorkspaceIds?.[id] &&
|
||||
!view.held(id) &&
|
||||
!view.goingToSleep(id) &&
|
||||
!view.suspendCommitted(id)
|
||||
return up ? id : null
|
||||
}
|
||||
|
||||
const SLEEP_VIEW = {
|
||||
held: (id: string) => isWorktreeSleepViewHeld(id),
|
||||
goingToSleep: isWorktreeGoingToSleep,
|
||||
suspendCommitted: isWorktreeSuspendCommitted
|
||||
}
|
||||
|
||||
/** Wakes a workspace's asleep tabs whenever it becomes the active one, or is woken in place. */
|
||||
export function installBrowserTabWakeOnOpen(): () => void {
|
||||
let previous = useAppStore.getState().activeWorktreeId
|
||||
if (previous) {
|
||||
wakeWorktreeBrowserTabs(previous)
|
||||
}
|
||||
return useAppStore.subscribe((state) => {
|
||||
if (state.activeWorktreeId === previous) {
|
||||
const wakeIfBackInPlace = (): void => {
|
||||
const id = isBackAfterHeldSleep(useAppStore.getState(), SLEEP_VIEW)
|
||||
if (id) {
|
||||
wakeWorktreeBrowserTabs(id)
|
||||
}
|
||||
}
|
||||
const stopStore = useAppStore.subscribe((state) => {
|
||||
if (state.activeWorktreeId !== previous) {
|
||||
previous = state.activeWorktreeId
|
||||
if (previous) {
|
||||
wakeWorktreeBrowserTabs(previous)
|
||||
}
|
||||
return
|
||||
}
|
||||
previous = state.activeWorktreeId
|
||||
if (previous) {
|
||||
wakeWorktreeBrowserTabs(previous)
|
||||
}
|
||||
wakeIfBackInPlace()
|
||||
})
|
||||
const stopView = subscribeWorktreeSleepView(wakeIfBackInPlace)
|
||||
return () => {
|
||||
stopStore()
|
||||
stopView()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user