Fix recipe serve desktop promotion (#8646)
* fix(runtime): preserve terminals during headless desktop activation * rm design doc * Fix desktop activation launch ordering and blocked-window status resolut - Check desktopWindowStatus before spawning the Orca app so a blocked runtime no longer launches a doomed second instance. - Reuse resolveDesktopWindowStatus for remote runtime status so it honors the same authoritativeWindowId fallback as local status. - Re-check the authoritative window at spawn time instead of trusting a possibly-stale snapshot, since it can be destroyed mid-await. - Harden the e2e activation spec against silent spawn failures. --------- Co-authored-by: bbingz <zzb@gxsmjx.com>
This commit is contained in:
@@ -168,6 +168,7 @@ export function formatCliStatus(status: CliStatusResult): string {
|
||||
return [
|
||||
`appRunning: ${status.app.running}`,
|
||||
`pid: ${status.app.pid ?? 'none'}`,
|
||||
`desktopWindowStatus: ${status.app.desktopWindowStatus ?? 'unknown'}`,
|
||||
`runtimeState: ${status.runtime.state}`,
|
||||
`runtimeReachable: ${status.runtime.reachable}`,
|
||||
`runtimeId: ${status.runtime.runtimeId ?? 'none'}`,
|
||||
|
||||
@@ -2,13 +2,19 @@ import { mkdtempSync, writeFileSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { createServer, type Socket } from 'node:net'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { RuntimeClient, RuntimeRpcFailureError } from './runtime-client'
|
||||
import { launchOrcaApp } from './runtime/launch'
|
||||
|
||||
vi.mock('./runtime/launch', () => ({
|
||||
launchOrcaApp: vi.fn()
|
||||
}))
|
||||
|
||||
const servers = new Set<ReturnType<typeof createServer>>()
|
||||
const sockets = new Set<Socket>()
|
||||
|
||||
afterEach(async () => {
|
||||
vi.mocked(launchOrcaApp).mockClear()
|
||||
for (const socket of sockets) {
|
||||
socket.destroy()
|
||||
}
|
||||
@@ -170,7 +176,7 @@ describe.skipIf(process.platform === 'win32')('RuntimeClient', () => {
|
||||
expect(status.result.graph.state).toBe('unavailable')
|
||||
})
|
||||
|
||||
it('openOrca succeeds immediately when the runtime is already reachable', async () => {
|
||||
it('openOrca activates the app even when a desktop runtime is already reachable', async () => {
|
||||
const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-client-'))
|
||||
const endpoint = join(userDataPath, 'runtime.sock')
|
||||
const server = createServer((socket) => {
|
||||
@@ -204,6 +210,87 @@ describe.skipIf(process.platform === 'win32')('RuntimeClient', () => {
|
||||
|
||||
expect(status.result.runtime.state).toBe('ready')
|
||||
expect(status.result.runtime.reachable).toBe(true)
|
||||
expect(launchOrcaApp).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('openOrca waits for a reachable headless runtime to expose a desktop window', async () => {
|
||||
const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-client-'))
|
||||
const endpoint = join(userDataPath, 'runtime.sock')
|
||||
let statusRequests = 0
|
||||
const server = createServer((socket) => {
|
||||
sockets.add(socket)
|
||||
socket.once('close', () => sockets.delete(socket))
|
||||
socket.once('data', (data) => {
|
||||
const request = JSON.parse(String(data).trim()) as { id: string }
|
||||
statusRequests += 1
|
||||
const available = statusRequests > 1
|
||||
socket.write(
|
||||
`${JSON.stringify({
|
||||
id: request.id,
|
||||
ok: true,
|
||||
result: {
|
||||
runtimeId: 'runtime-1',
|
||||
rendererGraphEpoch: available ? 1 : 0,
|
||||
graphStatus: available ? 'reloading' : 'ready',
|
||||
authoritativeWindowId: available ? 1 : 0,
|
||||
desktopWindowStatus: available ? 'available' : 'initializing',
|
||||
liveTabCount: 0,
|
||||
liveLeafCount: 0
|
||||
},
|
||||
_meta: { runtimeId: 'runtime-1' }
|
||||
})}\n`
|
||||
)
|
||||
})
|
||||
})
|
||||
servers.add(server)
|
||||
await new Promise<void>((resolve) => server.listen(endpoint, resolve))
|
||||
writeMetadata(userDataPath, endpoint)
|
||||
|
||||
const client = new RuntimeClient(userDataPath, 100)
|
||||
const status = await client.openOrca(1_000)
|
||||
|
||||
expect(launchOrcaApp).toHaveBeenCalledOnce()
|
||||
expect(status.result.app.desktopWindowStatus).toBe('available')
|
||||
expect(statusRequests).toBeGreaterThan(1)
|
||||
})
|
||||
|
||||
it('openOrca fails explicitly when the serve owner cannot promote safely', async () => {
|
||||
const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-client-'))
|
||||
const endpoint = join(userDataPath, 'runtime.sock')
|
||||
const server = createServer((socket) => {
|
||||
sockets.add(socket)
|
||||
socket.once('close', () => sockets.delete(socket))
|
||||
socket.once('data', (data) => {
|
||||
const request = JSON.parse(String(data).trim()) as { id: string }
|
||||
socket.write(
|
||||
`${JSON.stringify({
|
||||
id: request.id,
|
||||
ok: true,
|
||||
result: {
|
||||
runtimeId: 'runtime-1',
|
||||
rendererGraphEpoch: 0,
|
||||
graphStatus: 'ready',
|
||||
authoritativeWindowId: 0,
|
||||
desktopWindowStatus: 'blocked',
|
||||
liveTabCount: 1,
|
||||
liveLeafCount: 1
|
||||
},
|
||||
_meta: { runtimeId: 'runtime-1' }
|
||||
})}\n`
|
||||
)
|
||||
})
|
||||
})
|
||||
servers.add(server)
|
||||
await new Promise<void>((resolve) => server.listen(endpoint, resolve))
|
||||
writeMetadata(userDataPath, endpoint)
|
||||
|
||||
const client = new RuntimeClient(userDataPath, 100)
|
||||
|
||||
await expect(client.openOrca(100)).rejects.toMatchObject({
|
||||
code: 'desktop_activation_blocked'
|
||||
})
|
||||
// A blocked runtime can't promote, so we bail before spawning the app.
|
||||
expect(launchOrcaApp).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('times out if the runtime never responds', async () => {
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { CliStatusResult, RuntimeStatus } from '../../shared/runtime-types'
|
||||
import { parsePairingCode, type PairingOffer } from '../../shared/pairing'
|
||||
import { launchOrcaApp } from './launch'
|
||||
import { getDefaultUserDataPath, readMetadata } from './metadata'
|
||||
import { getCliStatus } from './status'
|
||||
import { getCliStatus, resolveDesktopWindowStatus } from './status'
|
||||
import { sendRequest } from './transport'
|
||||
import { RuntimeClientError, RuntimeRpcFailureError, type RuntimeRpcSuccess } from './types'
|
||||
import { sendWebSocketRequest } from './websocket-transport'
|
||||
@@ -115,7 +115,13 @@ export class RuntimeClient {
|
||||
// that this client machine has a local Orca desktop process.
|
||||
app: {
|
||||
running: false,
|
||||
pid: null
|
||||
pid: null,
|
||||
// Why: reuse the shared resolver so remote status honors the same
|
||||
// authoritativeWindowId fallback as local status for old runtimes.
|
||||
...(() => {
|
||||
const desktopWindowStatus = resolveDesktopWindowStatus(response.result)
|
||||
return desktopWindowStatus ? { desktopWindowStatus } : {}
|
||||
})()
|
||||
},
|
||||
runtime: {
|
||||
state: graphState === 'ready' ? 'ready' : 'graph_not_ready',
|
||||
@@ -169,15 +175,27 @@ export class RuntimeClient {
|
||||
|
||||
async openOrca(timeoutMs = 15_000): Promise<RuntimeRpcSuccess<CliStatusResult>> {
|
||||
const initial = await this.getCliStatus()
|
||||
if (initial.result.runtime.reachable) {
|
||||
if (this.remotePairing) {
|
||||
return initial
|
||||
}
|
||||
|
||||
// Why: a blocked runtime can't open a window, so spawning the app would
|
||||
// only hit the single-instance lock and exit — bail before launching.
|
||||
if (initial.result.app.desktopWindowStatus === 'blocked') {
|
||||
throwDesktopActivationBlocked()
|
||||
}
|
||||
launchOrcaApp()
|
||||
if (initial.result.app.desktopWindowStatus === 'available') {
|
||||
return initial
|
||||
}
|
||||
|
||||
const startedAt = Date.now()
|
||||
while (Date.now() - startedAt < timeoutMs) {
|
||||
const status = await this.getCliStatus()
|
||||
if (status.result.runtime.reachable) {
|
||||
if (status.result.app.desktopWindowStatus === 'blocked') {
|
||||
throwDesktopActivationBlocked()
|
||||
}
|
||||
if (status.result.app.desktopWindowStatus === 'available') {
|
||||
return status
|
||||
}
|
||||
await delay(250)
|
||||
@@ -185,11 +203,18 @@ export class RuntimeClient {
|
||||
|
||||
throw new RuntimeClientError(
|
||||
'runtime_open_timeout',
|
||||
'Timed out waiting for Orca to start. Run the Orca app manually and try again.'
|
||||
'Timed out waiting for an Orca desktop window. The runtime may still be running headlessly.'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function throwDesktopActivationBlocked(): never {
|
||||
throw new RuntimeClientError(
|
||||
'desktop_activation_blocked',
|
||||
'Orca is running headlessly, but it cannot open a desktop window safely because the persistent terminal provider is unavailable. Quit Orca normally and start the app again; do not use open -n.'
|
||||
)
|
||||
}
|
||||
|
||||
function resolveRemotePairing(
|
||||
userDataPath: string,
|
||||
pairingCode: string | null,
|
||||
|
||||
@@ -35,10 +35,12 @@ export async function getCliStatus(
|
||||
throw new RuntimeRpcFailureError(response)
|
||||
}
|
||||
const graphState = response.result.graphStatus
|
||||
const desktopWindowStatus = resolveDesktopWindowStatus(response.result)
|
||||
return buildCliStatusResponse({
|
||||
app: {
|
||||
running: true,
|
||||
pid: metadata.pid
|
||||
pid: metadata.pid,
|
||||
...(desktopWindowStatus ? { desktopWindowStatus } : {})
|
||||
},
|
||||
runtime: {
|
||||
state: graphState === 'ready' ? 'ready' : 'graph_not_ready',
|
||||
@@ -68,6 +70,19 @@ export async function getCliStatus(
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveDesktopWindowStatus(
|
||||
status: RuntimeStatus
|
||||
): CliStatusResult['app']['desktopWindowStatus'] {
|
||||
if (status.desktopWindowStatus) {
|
||||
return status.desktopWindowStatus
|
||||
}
|
||||
// Why: older desktop runtimes predate the explicit status but a positive
|
||||
// Electron id still proves that a real window owns the graph.
|
||||
return status.authoritativeWindowId !== null && status.authoritativeWindowId > 0
|
||||
? 'available'
|
||||
: undefined
|
||||
}
|
||||
|
||||
function buildCliStatusResponse(result: CliStatusResult): RuntimeRpcSuccess<CliStatusResult> {
|
||||
return {
|
||||
id: 'local-status',
|
||||
|
||||
@@ -2,7 +2,7 @@ import { createServer, type Server } from 'node:http'
|
||||
import { mkdtempSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { WebSocketServer } from 'ws'
|
||||
import { encodePairingOffer, type PairingOffer } from '../../shared/pairing'
|
||||
import {
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
publicKeyToBase64
|
||||
} from '../../shared/e2ee-crypto'
|
||||
import { RuntimeClient } from './client'
|
||||
import { launchOrcaApp } from './launch'
|
||||
import { addEnvironmentFromPairingCode } from './environments'
|
||||
import { RuntimeClientError } from './types'
|
||||
import {
|
||||
@@ -20,6 +21,10 @@ import {
|
||||
RUNTIME_PROTOCOL_VERSION
|
||||
} from '../../shared/protocol-version'
|
||||
|
||||
vi.mock('./launch', () => ({
|
||||
launchOrcaApp: vi.fn()
|
||||
}))
|
||||
|
||||
type TestRuntime = {
|
||||
endpoint: string
|
||||
publicKeyB64: string
|
||||
@@ -31,6 +36,7 @@ describe('CLI remote WebSocket transport', () => {
|
||||
const servers: TestRuntime[] = []
|
||||
|
||||
afterEach(async () => {
|
||||
vi.mocked(launchOrcaApp).mockClear()
|
||||
await Promise.all(servers.splice(0).map((server) => server.close()))
|
||||
})
|
||||
|
||||
@@ -79,6 +85,28 @@ describe('CLI remote WebSocket transport', () => {
|
||||
expect(status.result.runtime.runtimeId).toBe('runtime-ws-2')
|
||||
})
|
||||
|
||||
it('does not launch a local desktop app for remote-paired open', async () => {
|
||||
const runtime = await startTestRuntime('runtime-remote-headless', {
|
||||
desktopWindowStatus: 'initializing'
|
||||
})
|
||||
servers.push(runtime)
|
||||
const client = new RuntimeClient(
|
||||
'/tmp/unused',
|
||||
5_000,
|
||||
encodePairingOffer({
|
||||
v: 2,
|
||||
endpoint: runtime.endpoint,
|
||||
deviceToken: runtime.deviceToken,
|
||||
publicKeyB64: runtime.publicKeyB64
|
||||
})
|
||||
)
|
||||
|
||||
const status = await client.openOrca()
|
||||
|
||||
expect(status.result.app.desktopWindowStatus).toBe('initializing')
|
||||
expect(launchOrcaApp).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('connects through a saved environment selector', async () => {
|
||||
const runtime = await startTestRuntime('runtime-env-1')
|
||||
servers.push(runtime)
|
||||
@@ -128,6 +156,7 @@ async function startTestRuntime(
|
||||
statusOverrides: {
|
||||
runtimeProtocolVersion?: number
|
||||
minCompatibleRuntimeClientVersion?: number
|
||||
desktopWindowStatus?: 'available' | 'openable' | 'initializing' | 'blocked'
|
||||
} = {}
|
||||
): Promise<TestRuntime> {
|
||||
const serverKeyPair = generateKeyPair()
|
||||
@@ -177,6 +206,7 @@ async function startTestRuntime(
|
||||
rendererGraphEpoch: 1,
|
||||
graphStatus: 'ready',
|
||||
authoritativeWindowId: null,
|
||||
desktopWindowStatus: statusOverrides.desktopWindowStatus,
|
||||
liveTabCount: 0,
|
||||
liveLeafCount: 0,
|
||||
runtimeProtocolVersion:
|
||||
|
||||
Reference in New Issue
Block a user