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:
Jinjing
2026-07-13 19:41:24 -07:00
committed by GitHub
co-authored by bbingz
parent 6e1aa8503c
commit 1d2aaf1bf5
20 changed files with 1075 additions and 76 deletions
+30 -5
View File
@@ -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,
+16 -1
View File
@@ -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',
+31 -1
View File
@@ -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: