Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
// Reuse one retention oracle across desktop, windowless and local-CLI hosts.
// Require both original inventory bindings and replies from the same PID with exactly one READY line; replacements must fail.
import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { expect, type Page } from '@stablyai/playwright-test'
import { quotePosixShell } from '../../../src/shared/shell-argument-quoting'
export type RuntimeRpcCall = <TResult>(method: string, params: unknown) => Promise<TResult>
export type HostCreatedTerminal = {
handle: string
tabId: string
leafId: string
ptyId: string
pid: string
sinkPath: string
}
export type HostTerminalInventory = {
publicationEpoch: string
tabIds: string[]
ptyIdByTabId: Record<string, string | null>
}
type SessionTabsListResult = {
publicationEpoch: string
tabs: {
id: string
type: string
parentTabId?: string
ptyId?: string | null
}[]
}
const HOST_TERMINAL_SURFACE_SEPARATOR = '::'
// Use daemon ids deliberately outside serve-/ssh- classification so the test reaches host-created binding retention.
function isDaemonPtyIdForm(ptyId: string, worktreeId: string): boolean {
return (
ptyId.startsWith(`${worktreeId}@@`) &&
!ptyId.startsWith('serve-') &&
!ptyId.startsWith('ssh-') &&
ptyId.slice(`${worktreeId}@@`.length).length > 0
)
}
export function createRetentionFixtureDirectory(): string {
return mkdtempSync(path.join(os.tmpdir(), 'orca-host-terminal-retention-'))
}
// Keep the agent alive so PTY death cannot explain a pruned tab.
export function writeRetentionFixture(
directory: string,
options: { processTitle?: string } = {}
): string {
const fixturePath = path.join(directory, 'host-created-agent-terminal.mjs')
writeFileSync(
fixturePath,
[
"import { appendFileSync } from 'node:fs'",
...(options.processTitle ? [`process.title = ${JSON.stringify(options.processTitle)}`] : []),
'const sink = process.argv[2]',
'const record = (line) => appendFileSync(sink, `${line}\\n`)',
'record(`READY:${process.pid}`)',
'process.stdout.write(`READY:${process.pid}\\r\\n`)',
"process.stdin.setEncoding('utf8')",
"let pending = ''",
"process.stdin.on('data', (data) => {",
' pending += data',
' const lines = pending.split(/\\r\\n|\\r|\\n/)',
" pending = lines.pop() ?? ''",
' for (const line of lines) {',
' record(`LINE:${line}`)',
' process.stdout.write(`LINE:${line}\\r\\n`)',
' }',
'})',
'process.stdin.resume()'
].join('\n')
)
return fixturePath
}
export function retentionFixtureCommand(fixturePath: string, sinkPath: string): string {
const command = [process.execPath, fixturePath, sinkPath]
return process.platform === 'win32'
? // PowerShell needs the call operator when the executable is quoted; cmd.exe also accepts it.
`& ${command.map((value) => `"${value.replaceAll('"', '""')}"`).join(' ')}`
: command.map(quotePosixShell).join(' ')
}
export function readSink(sinkPath: string): string {
try {
return readFileSync(sinkPath, 'utf8')
} catch {
return ''
}
}
function readySignatures(sinkPath: string): string[] {
return readSink(sinkPath)
.split(/\r?\n/)
.filter((line) => line.startsWith('READY:'))
}
// Use terminal.create: session.tabs.createTerminal already preserves its binding and would bypass the tested seam.
export async function createHostCliTerminal(
call: RuntimeRpcCall,
worktreeId: string,
fixturePath: string,
sinkPath: string
): Promise<HostCreatedTerminal> {
const created = await call<{
terminal: { handle: string; tabId?: string; ptyId?: string | null }
}>('terminal.create', {
worktree: `id:${worktreeId}`,
command: retentionFixtureCommand(fixturePath, sinkPath),
focus: false,
activate: false,
presentation: 'background'
})
const { handle, tabId } = created.terminal
if (!tabId) {
throw new Error('Host did not report a tab id for the CLI-created terminal')
}
// Read host-minted pane identity before a renderer may have materialized the tab.
const summary = (
await call<{ terminal: { ptyId: string | null; leafId: string } }>('terminal.show', {
terminal: handle
})
).terminal
const ptyId = created.terminal.ptyId ?? summary.ptyId
if (!ptyId) {
throw new Error('Host did not report a PTY for the CLI-created terminal')
}
if (!summary.leafId) {
throw new Error('Host did not report a leaf id for the CLI-created terminal')
}
expect(
isDaemonPtyIdForm(ptyId, worktreeId),
`CLI terminal ${ptyId} must carry the daemon id shape this seam excludes from classification`
).toBe(true)
await expect
.poll(() => readySignatures(sinkPath).length, {
timeout: 30_000,
message: 'CLI-created terminal never started its fixture process'
})
.toBe(1)
return {
handle,
tabId,
leafId: summary.leafId,
ptyId,
pid: readySignatures(sinkPath)[0]!.slice('READY:'.length),
sinkPath
}
}
export async function readHostTerminalInventory(
call: RuntimeRpcCall,
worktreeId: string
): Promise<HostTerminalInventory> {
const snapshot = await call<SessionTabsListResult>('session.tabs.list', {
worktree: `id:${worktreeId}`
})
const terminals = snapshot.tabs.filter((tab) => tab.type === 'terminal')
const ptyIdByTabId: Record<string, string | null> = {}
for (const tab of terminals) {
const parentTabId = tab.parentTabId ?? tab.id.split(HOST_TERMINAL_SURFACE_SEPARATOR)[0]!
ptyIdByTabId[parentTabId] = tab.ptyId ?? null
}
return {
publicationEpoch: snapshot.publicationEpoch,
tabIds: Object.keys(ptyIdByTabId),
ptyIdByTabId
}
}
// Require a reply from the original PID with one READY line so a respawned terminal cannot pass.
export async function proveSameLivePty(
call: RuntimeRpcCall,
terminal: HostCreatedTerminal,
marker: string
): Promise<void> {
await call('terminal.send', { terminal: terminal.handle, text: marker, enter: true })
await expect
.poll(() => readSink(terminal.sinkPath), {
timeout: 30_000,
message: `CLI terminal ${terminal.ptyId} did not deliver ${marker} to its original process`
})
.toContain(`LINE:${marker}`)
expect(
readySignatures(terminal.sinkPath),
'the surviving terminal must be the original process, not a respawned replacement'
).toEqual([`READY:${terminal.pid}`])
}
export async function createHostRendererTerminalTab(
page: Page,
worktreeId: string
): Promise<string> {
const tabId = await page.evaluate((id) => {
const store = window.__store
if (!store) {
throw new Error('Host renderer store is unavailable')
}
store.getState().setActiveView('terminal')
store.getState().setActiveWorktree(id)
const tab = store.getState().createTab(id)
store.getState().setActiveTab(tab.id)
store.getState().setActiveTabType('terminal', store.getState().activeWorktreeId)
return tab.id
}, worktreeId)
await expect
.poll(
() =>
page.evaluate(
({ worktreeId, tabId }) =>
(window.__store?.getState().tabsByWorktree[worktreeId] ?? []).find(
(tab) => tab.id === tabId
)?.ptyId ?? null,
{ worktreeId, tabId }
),
{ timeout: 60_000, message: `Host renderer tab ${tabId} never spawned a PTY` }
)
.not.toBeNull()
return tabId
}
/** The host inventory exactly as it read on the poll tick that first carried `tabId`. */
export async function readHostInventoryWhenTabAppears(
call: RuntimeRpcCall,
worktreeId: string,
tabId: string,
message: string
): Promise<HostTerminalInventory> {
let inventory: HostTerminalInventory | null = null
await expect
.poll(
async () => {
inventory = await readHostTerminalInventory(call, worktreeId)
return inventory.tabIds.includes(tabId)
},
{ timeout: 60_000, message }
)
.toBe(true)
return inventory!
}