Files
orca/src/cli/runtime/status.test.ts
T
Neil 95c77502d2 fix(cli): stop reporting an unsignalable Orca pid as a stale bootstrap
`orca status` falls back to a `kill(pid, 0)` probe when `status.get` cannot be
reached, and a bare catch read every refusal as absence. EPERM means the pid
exists under another uid -- an Orca reached via ORCA_USER_DATA_PATH, or one
started with sudo -- so a live app was reported `running: false`, `pid: null`,
`runtime.state: stale_bootstrap`, `graph.state: not_running`.

Only ESRCH proves the pid is gone, which is the rule every other liveness probe
in the repo already applies (`isProcessAlive` in relay/pty-shell-utils.ts,
pack-refs-lock-ownership.ts, runtime-metadata-ownership-watch.ts, and
agent-session-process-identity-probe.ts). See
docs/reference/ssh-execution-boundary.md.
2026-09-11 04:47:36 -07:00

197 lines
7.0 KiB
TypeScript

import { mkdtempSync, writeFileSync } from 'node:fs'
import { createServer, type Socket } from 'node:net'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { getRuntimeMetadataPath } from '../../shared/runtime-bootstrap'
import type { RuntimeStatus } from '../../shared/runtime-types'
import { RuntimeClient } from './client'
import { projectRemoteAppStatus } from './status'
const servers = new Set<ReturnType<typeof createServer>>()
const sockets = new Set<Socket>()
afterEach(async () => {
for (const socket of sockets) {
socket.destroy()
}
sockets.clear()
await Promise.all(
[...servers].map(
(server) =>
new Promise<void>((resolve) => {
server.close(() => resolve())
})
)
)
servers.clear()
})
// Why: legacy runtime metadata compatibility only applies to local Unix socket
// metadata; Windows uses named pipes and cannot run this fixture directly.
describe.skipIf(process.platform === 'win32')('CLI runtime status', () => {
it('uses the legacy singular runtime transport when reporting status', async () => {
const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-status-'))
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-legacy',
rendererGraphEpoch: 1,
graphStatus: 'ready',
authoritativeWindowId: null,
liveTabCount: 0,
degradations: [
{
code: 'browser_unavailable',
capability: 'browser.headless.v1',
message: 'Browser automation is unavailable.'
}
]
},
_meta: { runtimeId: 'runtime-legacy' }
})}\n`
)
})
})
servers.add(server)
await new Promise<void>((resolve) => server.listen(endpoint, resolve))
writeFileSync(
getRuntimeMetadataPath(userDataPath),
JSON.stringify({
runtimeId: 'runtime-legacy',
pid: process.pid,
transport: { kind: 'unix', endpoint },
authToken: 'token',
startedAt: Date.now()
})
)
const status = await new RuntimeClient(userDataPath).getCliStatus()
expect(status.result.runtime).toMatchObject({
reachable: true,
connectionState: 'connected',
runtimeId: 'runtime-legacy',
state: 'ready',
degradations: [expect.objectContaining({ code: 'browser_unavailable' })]
})
})
})
// Why: `kill(pid, 0)` answers EPERM when the pid exists under another uid — an Orca the
// CLI was pointed at with ORCA_USER_DATA_PATH, or one started with sudo. Reading that
// refusal as absence reports a live app as a dead one
// (docs/reference/ssh-execution-boundary.md).
describe.skipIf(process.platform === 'win32')('CLI status pid fallback', () => {
async function statusWithUnreachableRuntime(
killError: NodeJS.ErrnoException
): Promise<Awaited<ReturnType<RuntimeClient['getCliStatus']>>> {
const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-status-probe-'))
writeFileSync(
getRuntimeMetadataPath(userDataPath),
JSON.stringify({
runtimeId: 'runtime-unreachable',
pid: 424242,
// Nothing is listening here, so `status.get` fails and the pid probe decides.
transport: { kind: 'unix', endpoint: join(userDataPath, 'absent.sock') },
authToken: 'token',
startedAt: Date.now()
})
)
const killSpy = vi.spyOn(process, 'kill').mockImplementation(() => {
throw killError
})
try {
return await new RuntimeClient(userDataPath).getCliStatus()
} finally {
killSpy.mockRestore()
}
}
it('keeps an unsignalable app running rather than calling the bootstrap stale', async () => {
const status = await statusWithUnreachableRuntime(
Object.assign(new Error('kill EPERM'), { code: 'EPERM' })
)
expect(status.result.app).toMatchObject({ running: true, pid: 424242 })
expect(status.result.runtime.state).toBe('starting')
expect(status.result.graph.state).toBe('starting')
})
it('still reports a stale bootstrap when the host proves the pid is gone', async () => {
const status = await statusWithUnreachableRuntime(
Object.assign(new Error('kill ESRCH'), { code: 'ESRCH' })
)
expect(status.result.app).toMatchObject({ running: false, pid: null })
expect(status.result.runtime.state).toBe('stale_bootstrap')
expect(status.result.graph.state).toBe('not_running')
})
})
describe('projectRemoteAppStatus', () => {
function remoteStatus(overrides: Partial<RuntimeStatus> = {}): RuntimeStatus {
return {
runtimeId: 'remote-runtime',
graphStatus: 'ready',
authoritativeWindowId: null,
...overrides
} as RuntimeStatus
}
// STA-4792 defect 4: the reported output was running:false alongside desktopWindowStatus
// 'available', while the target's GUI was demonstrably up.
it('reports the target app as running when a renderer owns the graph there', () => {
expect(projectRemoteAppStatus(remoteStatus({ desktopWindowStatus: 'available' }))).toEqual({
running: true,
pid: null,
desktopWindowStatus: 'available'
})
})
it.each(['openable', 'initializing', 'blocked'] as const)(
'does not claim a running desktop for window status %s',
(desktopWindowStatus) => {
expect(projectRemoteAppStatus(remoteStatus({ desktopWindowStatus })).running).toBe(false)
}
)
// A headless `serve` reports no window at all.
it('reports not running when the target has no desktop window status', () => {
expect(projectRemoteAppStatus(remoteStatus())).toEqual({ running: false, pid: null })
})
// Why: old runtimes predate the explicit status but a positive window id still proves a window.
it('honors the authoritativeWindowId fallback for older runtimes', () => {
expect(projectRemoteAppStatus(remoteStatus({ authoritativeWindowId: 3 }))).toEqual({
running: true,
pid: null,
desktopWindowStatus: 'available'
})
})
// Why: the SSH host-passthrough answers for the Orca host the caller reached, and used to
// claim running:true unconditionally. Both transports now share this projection.
it('does not claim a desktop app for a headless serve on any transport', () => {
expect(projectRemoteAppStatus(remoteStatus({ desktopWindowStatus: 'openable' }))).toEqual({
running: false,
pid: null,
desktopWindowStatus: 'openable'
})
})
it('never reports a remote pid', () => {
expect(
projectRemoteAppStatus(remoteStatus({ desktopWindowStatus: 'available' })).pid
).toBeNull()
})
})