Implement robust orchestration primitives and connected-server workers (#9925)

This commit is contained in:
OrcaWin
2026-07-27 12:31:37 -07:00
committed by GitHub
parent 8b154d686c
commit cd05f2ff93
127 changed files with 23196 additions and 887 deletions
+226 -12
View File
@@ -1012,6 +1012,7 @@ class InMemoryOrchestrationMessages {
this.sequence += 1
const row: MessageRow = {
id: `msg_${this.sequence}`,
run_id: 'run_test',
from_handle: msg.from,
to_handle: msg.to,
subject: msg.subject,
@@ -12468,6 +12469,102 @@ describe('OrcaRuntimeService', () => {
})
})
it('observes setup command completion without waiting for its interactive shell to exit', async () => {
const runtime = new OrcaRuntimeService(store)
runtime.setPtyController({
spawn: vi.fn().mockResolvedValue({ id: 'pty-setup' }),
write: () => true,
kill: () => true,
getForegroundProcess: async () => null
})
runtime.attachWindow(1)
runtime.syncWindowGraph(1, { tabs: [], leaves: [] })
const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`)
;(
runtime as unknown as { setupCompletionTokenByPtyId: Map<string, string> }
).setupCompletionTokenByPtyId.set('pty-setup', 'token-live')
const waiting = runtime.waitForSetupTerminalCompletion(handle)
runtime.onPtyData(
'pty-setup',
'setup failed\r\n__ORCA_SETUP_COMPLETE__:token-live:17\r\nPS>',
100
)
await expect(waiting).resolves.toEqual({ exitCode: 17 })
await expect(runtime.readTerminal(handle)).resolves.toMatchObject({ status: 'running' })
})
it('replays fast setup completion emitted before its observer is registered', async () => {
const runtime = new OrcaRuntimeService(store)
runtime.setPtyController({
spawn: vi.fn().mockResolvedValue({ id: 'pty-fast-setup' }),
write: () => true,
kill: () => true,
getForegroundProcess: async () => null
})
runtime.attachWindow(1)
runtime.syncWindowGraph(1, { tabs: [], leaves: [] })
const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`)
;(
runtime as unknown as { setupCompletionTokenByPtyId: Map<string, string> }
).setupCompletionTokenByPtyId.set('pty-fast-setup', 'token-fast')
runtime.onPtyData(
'pty-fast-setup',
'__ORCA_SETUP_COMPLETE__:wrong:9\r\n__ORCA_SETUP_COMPLETE__:token-fast:0\r\n$',
100
)
await expect(runtime.waitForSetupTerminalCompletion(handle)).resolves.toEqual({ exitCode: 0 })
})
it('falls back to setup terminal exit when no completion signal is available', async () => {
const runtime = new OrcaRuntimeService(store)
runtime.setPtyController({
spawn: vi.fn().mockResolvedValue({ id: 'pty-legacy-setup' }),
write: () => true,
kill: () => true,
getForegroundProcess: async () => null
})
runtime.attachWindow(1)
runtime.syncWindowGraph(1, { tabs: [], leaves: [] })
const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`)
const waiting = runtime.waitForSetupTerminalCompletion(handle)
runtime.onPtyExit('pty-legacy-setup', 9)
await expect(waiting).resolves.toEqual({ exitCode: 9 })
})
it('keeps observing after an uncertain setup terminal status', async () => {
const runtime = new OrcaRuntimeService(store)
runtime.setPtyController({
spawn: vi.fn().mockResolvedValue({ id: 'pty-uncertain-setup' }),
write: () => true,
kill: () => true,
getForegroundProcess: async () => null
})
runtime.attachWindow(1)
runtime.syncWindowGraph(1, { tabs: [], leaves: [] })
const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`)
;(
runtime as unknown as { setupCompletionTokenByPtyId: Map<string, string> }
).setupCompletionTokenByPtyId.set('pty-uncertain-setup', 'token-uncertain')
vi.spyOn(runtime, 'waitForTerminal').mockResolvedValue({
handle,
condition: 'exit',
satisfied: false,
status: 'unknown',
exitCode: null
})
const waiting = runtime.waitForSetupTerminalCompletion(handle)
await Promise.resolve()
runtime.onPtyData('pty-uncertain-setup', '__ORCA_SETUP_COMPLETE__:token-uncertain:0\r\n', 100)
await expect(waiting).resolves.toEqual({ exitCode: 0 })
})
it('drops retained PTY transcript memory when a background terminal exits', async () => {
const runtime = new OrcaRuntimeService(store)
runtime.setPtyController({
@@ -17290,6 +17387,83 @@ describe('OrcaRuntimeService', () => {
expect(writes).toEqual(['still writable'])
})
it('preserves runtime-created PTY process identity after graph unavailable', async () => {
const runtime = new OrcaRuntimeService(store)
runtime.setPtyController({
spawn: vi.fn().mockResolvedValue({ id: 'pty-bg' }),
write: () => true,
kill: () => true,
getForegroundProcess: async () => null
})
runtime.attachWindow(1)
runtime.syncWindowGraph(1, { tabs: [], leaves: [] })
const { handle } = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`)
const incarnation = runtime.getTerminalProcessIncarnation(handle)
runtime.markGraphUnavailable(1)
expect(runtime.getTerminalProcessIncarnation(handle)).toBe(incarnation)
})
it('preserves PTY process identity while a renderer surface detaches and reattaches', async () => {
const runtime = new OrcaRuntimeService(store)
runtime.setPtyController({
spawn: vi.fn().mockResolvedValue({
id: 'pty-bg',
incarnationId: 'incarnation-bg'
}),
write: () => true,
kill: () => true,
getForegroundProcess: async () => null
})
runtime.attachWindow(1)
runtime.syncWindowGraph(1, { tabs: [], leaves: [] })
const created = await runtime.createTerminal(`path:${TEST_WORKTREE_PATH}`)
const [tabId, leafId] = created.paneKey?.split(':') ?? []
if (!tabId || !leafId) {
throw new Error('expected stable pane identity')
}
const syncSurface = (ptyId: string | null): void => {
runtime.syncWindowGraph(1, {
tabs: [
{
tabId,
worktreeId: TEST_WORKTREE_ID,
title: 'Codex',
activeLeafId: leafId,
layout: null
}
],
leaves: [
{
tabId,
worktreeId: TEST_WORKTREE_ID,
leafId,
paneRuntimeId: 1,
ptyId,
paneTitle: 'Codex'
}
]
})
}
syncSurface('pty-bg')
await runtime.listTerminals()
const before = runtime.getTerminalProcessIncarnation(created.handle)
syncSurface(null)
syncSurface('pty-bg')
await runtime.listTerminals()
expect(runtime.getTerminalProcessIncarnation(created.handle)).toBe(before)
runtime.registerPty('pty-bg', TEST_WORKTREE_ID, null, {
tabId,
leafId,
incarnationId: 'incarnation-replacement'
})
expect(runtime.getTerminalProcessIncarnation(created.handle)).not.toBe(before)
})
it('recognizes runtime-created PTY handles with agent launch titles', async () => {
const runtime = new OrcaRuntimeService(store)
runtime.setPtyController({
@@ -27108,7 +27282,7 @@ describe('OrcaRuntimeService', () => {
const waitPromise = runtime.waitForMessage('term_abc', { timeoutMs: 5000 })
runtime.notifyMessageArrived('term_abc')
await waitPromise
await expect(waitPromise).resolves.toBe('notified')
})
it('does not resolve type-filtered message waiters for unrelated message types', async () => {
@@ -27149,13 +27323,38 @@ describe('OrcaRuntimeService', () => {
})
it('resolves message waiters on timeout when no message arrives', async () => {
const runtime = new OrcaRuntimeService(store)
vi.useFakeTimers()
try {
const runtime = new OrcaRuntimeService(store)
const wait = runtime.waitForMessage('term_abc', { timeoutMs: 100 })
const start = Date.now()
await runtime.waitForMessage('term_abc', { timeoutMs: 100 })
const elapsed = Date.now() - start
expect(elapsed).toBeGreaterThanOrEqual(90)
expect(elapsed).toBeLessThan(500)
await vi.advanceTimersByTimeAsync(99)
let settled = false
void wait.then(() => {
settled = true
})
await Promise.resolve()
expect(settled).toBe(false)
await vi.advanceTimersByTimeAsync(1)
await expect(wait).resolves.toBe('timed_out')
} finally {
vi.useRealTimers()
}
})
it('allows only one exclusive mailbox waiter and supports explicit cancellation', async () => {
const runtime = new OrcaRuntimeService(store)
const first = runtime.waitForMessage('run:run_1', {
timeoutMs: 5000,
exclusive: true
})
await expect(
runtime.waitForMessage('run:run_1', { timeoutMs: 5000, exclusive: true })
).resolves.toBe('waiter_exists')
runtime.cancelMessageWaiters('run:run_1')
await expect(first).resolves.toBe('cancelled')
})
it('rejects leaf PTY waits when the request signal aborts', async () => {
@@ -32908,11 +33107,13 @@ describe('OrcaRuntimeService', () => {
}
])
await runtime.createManagedWorktree({
const result = await runtime.createManagedWorktree({
repoSelector: 'id:repo-1',
name: 'runtime-headless-parallel',
setupDecision: 'run',
startup: { command: 'claude' }
startup: { command: 'claude' },
observeSetupCompletion: true,
awaitTerminalProvisioning: true
})
// Why: setup now spawns fire-and-forget on a later tick; wait for both PTYs.
@@ -32920,8 +33121,14 @@ describe('OrcaRuntimeService', () => {
expect(spawn).toHaveBeenNthCalledWith(1, expect.objectContaining({ command: 'claude' }))
expect(spawn).toHaveBeenNthCalledWith(
2,
expect.objectContaining({ command: 'bash /tmp/repo/.git/orca/setup-runner.sh' })
expect.objectContaining({
command: expect.stringContaining('__ORCA_SETUP_COMPLETE__:')
})
)
expect(result.setupReceipt).toMatchObject({
state: 'running',
terminalHandle: expect.stringMatching(/^term_/)
})
})
it('creates the first terminal for CLI-created worktrees without activating them', async () => {
@@ -33133,10 +33340,12 @@ describe('OrcaRuntimeService', () => {
const result = await runtime.createManagedWorktree({
repoSelector: 'id:repo-1',
name: 'runtime-cli-setup-skip',
setupDecision: 'skip'
setupDecision: 'skip',
awaitTerminalProvisioning: true
})
expect(result.warning).toBeUndefined()
expect(result.setupReceipt).toMatchObject({ requested: 'skip', state: 'skipped' })
expect(createSetupRunnerScript).not.toHaveBeenCalled()
expect(spawn).toHaveBeenCalledTimes(1)
})
@@ -33694,7 +33903,8 @@ describe('OrcaRuntimeService', () => {
name: 'runtime-startup-setup-split',
startupDraft: 'https://github.com/stablyai/orca/issues/123',
setupDecision: 'run',
activate: true
activate: true,
awaitTerminalProvisioning: true
})
await vi.waitFor(() => expect(spawn).toHaveBeenCalledTimes(2))
@@ -33732,6 +33942,10 @@ describe('OrcaRuntimeService', () => {
const mainEnv = (spawn.mock.calls[0]![0] as { env?: Record<string, string> }).env ?? {}
const setupEnv = (spawn.mock.calls[1]![0] as { env?: Record<string, string> }).env ?? {}
expect(result.setup).toBeUndefined()
expect(result.setupReceipt).toMatchObject({
state: 'running',
terminalHandle: expect.stringMatching(/^term_/)
})
expect(mainEnv.ORCA_TAB_ID).toBeDefined()
expect(mainEnv.ORCA_PANE_KEY).toBeDefined()
expect(setupEnv.ORCA_TAB_ID).toBe(mainEnv.ORCA_TAB_ID)
+399 -22
View File
@@ -96,7 +96,23 @@ import { mkdir, readFile, readdir, rm, stat } from 'node:fs/promises'
import { resolveWorktreeCreateBase } from '../worktree-create-base'
import { resolveWorktreeAddBaseRef } from '../../shared/worktree-base-ref'
import { OrchestrationDb } from './orchestration/db'
import { OrchestrationError } from './orchestration/orchestration-error'
import {
buildObservedSetupCommand,
createSetupCompletionScanner
} from './orchestration/setup-completion-signal'
import type { RuntimeOrchestrationEnvelope } from '../../shared/runtime-rpc-envelope'
import {
isOrchestrationMutation,
orchestrationMigrationData
} from '../../shared/orchestration-rpc-contract'
import type {
OrchestrationEnvironmentTransport,
OrchestrationWorkerServer
} from './orchestration/environment-transport'
import { syncFederatedDispatch } from './orchestration/federation-sync'
import { formatMessagesForInjection } from './orchestration/formatter'
import { selectExactWorkerProviderSession } from './orchestration/worker-provider-session'
import type {
Automation,
AutomationCreateInput,
@@ -183,6 +199,7 @@ import type {
AgentProviderSessionMetadata,
SleepingAgentLaunchConfig
} from '../../shared/agent-session-resume'
import type { ExactWorkerProviderSession } from '../../shared/orchestration-worker-output'
import type { RuntimeClientEvent } from '../../shared/runtime-client-events'
import { toRuntimeActivateWorktreeEvent } from '../../shared/runtime-client-events'
import {
@@ -332,6 +349,8 @@ import {
BROWSER_HEADLESS_RUNTIME_CAPABILITY,
BROWSER_CERTIFICATE_TRUST_RUNTIME_CAPABILITY,
MIN_COMPATIBLE_RUNTIME_CLIENT_VERSION,
ORCHESTRATION_CONTRACT_RUNTIME_CAPABILITY,
ORCHESTRATION_CONTRACT_VERSION,
REMOTE_RUNTIME_SHARED_CONTROL_CAPABILITY,
RUNTIME_CAPABILITIES,
RUNTIME_PROTOCOL_VERSION,
@@ -1698,11 +1717,13 @@ type TerminalWaiter = {
type MessageWaiter = {
handle: string
typeFilter: string[] | undefined
resolve: (result: void) => void
resolve: (result: MessageWaitResult) => void
timeout: NodeJS.Timeout | null
abortCleanup: (() => void) | null
}
export type MessageWaitResult = 'notified' | 'timed_out' | 'cancelled' | 'waiter_exists'
function omitUndefinedProperties<T extends Record<string, unknown>>(value: T): Partial<T> {
return Object.fromEntries(
Object.entries(value).filter(([, entry]) => entry !== undefined)
@@ -2438,6 +2459,10 @@ export class OrcaRuntimeService {
private readonly runtimeId = randomUUID()
private readonly startedAt = Date.now()
private readonly store: RuntimeStore | null
private readonly orchestrationEnvironmentTransport: OrchestrationEnvironmentTransport | null
private readonly orchestrationFederationTimers = new Map<string, ReturnType<typeof setInterval>>()
private readonly orchestrationFederationSyncs = new Map<string, Promise<void>>()
private readonly orchestrationFederationWarnings = new Set<string>()
private rendererGraphEpoch = 0
private graphStatus: RuntimeGraphStatus = 'unavailable'
private authoritativeWindowId: number | null = null
@@ -2563,6 +2588,7 @@ export class OrcaRuntimeService {
// Why: startup draft paste can subscribe after the agent already emitted its
// ready marker. Keep a bounded raw buffer so fast startup output is replayed.
private recentPtyOutputById = new Map<string, RecentPtyOutputBuffer>()
private setupCompletionTokenByPtyId = new Map<string, string>()
// Why: mobile clients need to know when the desktop restores a terminal
// from mobile-fit so they can update their UI. These listeners are
// invoked from resizeForClient and onClientDisconnected/onPtyExit.
@@ -2969,6 +2995,7 @@ export class OrcaRuntimeService {
buildAgentHookPtyEnv?: () => Record<string, string>
getDesktopWindowStatus?: () => RuntimeDesktopWindowStatus
agentSessionClaimSigner?: AgentSessionClaimSigner
orchestrationEnvironmentTransport?: OrchestrationEnvironmentTransport
}
) {
this.store = store
@@ -2980,6 +3007,7 @@ export class OrcaRuntimeService {
this.clientSessionTabSelections.setPersistListener((state) => {
this.store?.setMobileClientTabSelections?.(state)
})
this.orchestrationEnvironmentTransport = deps?.orchestrationEnvironmentTransport ?? null
if (stats) {
this.stats = stats
this.agentDetector = new AgentDetector(stats)
@@ -3440,6 +3468,136 @@ export class OrcaRuntimeService {
return this.runtimeId
}
resolveOrchestrationWorkerServer(selector: string): OrchestrationWorkerServer {
if (!this.orchestrationEnvironmentTransport) {
throw new OrchestrationError(
'server_required',
'Connected-server orchestration is unavailable in this runtime.'
)
}
return this.orchestrationEnvironmentTransport.resolve(selector)
}
async callOrchestrationWorkerServer(
selector: string,
method: string,
params: unknown,
timeoutMs?: number,
envelope?: RuntimeOrchestrationEnvelope
): Promise<unknown> {
if (!this.orchestrationEnvironmentTransport) {
throw new OrchestrationError(
'server_required',
'Connected-server orchestration is unavailable in this runtime.'
)
}
if (isOrchestrationMutation(method, params)) {
const statusResponse = await this.orchestrationEnvironmentTransport.call(
selector,
'status.get',
undefined,
timeoutMs
)
if (statusResponse.ok === false) {
throw new OrchestrationError(
statusResponse.error.code,
statusResponse.error.message,
statusResponse.error.data
)
}
const status = statusResponse.result as RuntimeStatus
if (!status.capabilities?.includes(ORCHESTRATION_CONTRACT_RUNTIME_CAPABILITY)) {
throw new OrchestrationError(
'orchestration_migration_required',
'The connected worker server does not support the current orchestration contract. No effects were applied.',
orchestrationMigrationData('runtime_capability_missing')
)
}
}
const response = await this.orchestrationEnvironmentTransport.call(
selector,
method,
params,
timeoutMs,
method.startsWith('orchestration.')
? { ...envelope, orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION }
: envelope
)
if (response.ok === false) {
throw new OrchestrationError(response.error.code, response.error.message, response.error.data)
}
return response.result
}
async syncOrchestrationFederation(runId?: string): Promise<void> {
if (!this.orchestrationEnvironmentTransport) {
return
}
const dispatches = this.getOrchestrationDb().listActiveFederatedDispatches(runId)
await Promise.allSettled(
dispatches.map((dispatch) => this.syncOrchestrationFederatedDispatch(dispatch.dispatch_id))
)
}
private syncOrchestrationFederatedDispatch(dispatchId: string): Promise<void> {
const current = this.orchestrationFederationSyncs.get(dispatchId)
if (current) {
return current
}
const sync = syncFederatedDispatch(this, dispatchId)
.then(() => {
this.orchestrationFederationWarnings.delete(dispatchId)
})
.catch((error: unknown) => {
if (!this.orchestrationFederationWarnings.has(dispatchId)) {
console.warn(`[orchestration] Federation sync failed for ${dispatchId}:`, error)
this.orchestrationFederationWarnings.add(dispatchId)
}
throw error
})
.finally(() => {
this.orchestrationFederationSyncs.delete(dispatchId)
})
this.orchestrationFederationSyncs.set(dispatchId, sync)
return sync
}
ensureOrchestrationFederationRelay(runId?: string): void {
if (!this.orchestrationEnvironmentTransport) {
return
}
for (const dispatch of this.getOrchestrationDb().listActiveFederatedDispatches(runId)) {
if (this.orchestrationFederationTimers.has(dispatch.dispatch_id)) {
continue
}
const tick = () => {
const worker = this.getOrchestrationDb().getWorkerDispatch(dispatch.dispatch_id)
if (!worker || !['starting', 'ready', 'stopping'].includes(worker.state)) {
const activeTimer = this.orchestrationFederationTimers.get(dispatch.dispatch_id)
if (activeTimer) {
clearInterval(activeTimer)
}
this.orchestrationFederationTimers.delete(dispatch.dispatch_id)
this.orchestrationFederationWarnings.delete(dispatch.dispatch_id)
return
}
void this.syncOrchestrationFederatedDispatch(dispatch.dispatch_id).catch(() => undefined)
}
const timer = setInterval(tick, 1_000)
timer.unref?.()
this.orchestrationFederationTimers.set(dispatch.dispatch_id, timer)
tick()
}
}
stopOrchestrationFederationRelay(): void {
for (const timer of this.orchestrationFederationTimers.values()) {
clearInterval(timer)
}
this.orchestrationFederationTimers.clear()
this.orchestrationFederationWarnings.clear()
}
getStartedAt(): number {
return this.startedAt
}
@@ -10901,6 +11059,7 @@ export class OrcaRuntimeService {
this.resizeListeners.delete(ptyId)
this.lastRendererSizes.delete(ptyId)
this.recentPtyOutputById.delete(ptyId)
this.setupCompletionTokenByPtyId.delete(ptyId)
this.clearWaitBlockedCheckState(ptyId)
this.recentPtyPathCandidatesById.delete(ptyId)
this.ptyOutputSequenceById.delete(ptyId)
@@ -13568,6 +13727,64 @@ export class OrcaRuntimeService {
}
}
getTerminalProcessIncarnation(handle: string): string | null {
const live = this.getLivePtyForHandle(handle)
const record = live?.record ?? this.handles.get(handle)
if (!record?.ptyId) {
return null
}
const incarnationId = live?.pty.incarnationId ?? this.ptysById.get(record.ptyId)?.incarnationId
if (incarnationId) {
return `${record.ptyId}:${incarnationId}`
}
// Why: legacy providers may omit process incarnation; retain the prior restart-degraded fence.
return `${this.runtimeId}:${record.ptyId}:${record.ptyGeneration}`
}
getExactWorkerProviderSession(
handle: string,
observedAfter: number
): ExactWorkerProviderSession | null {
const paneKey = this.getTerminalPaneKey(handle)
const processIncarnation = this.getTerminalProcessIncarnation(handle)
if (!paneKey || !processIncarnation) {
return null
}
let connectionId: string | null | undefined
let launchToken: string | null | undefined
try {
const ptyId = this.getTerminalAgentStatusPtyId(handle)
const pty = this.ptysById.get(ptyId)
connectionId = pty?.connectionId ?? null
launchToken = pty?.launchToken ?? null
} catch {
// Exact worker validation rejects this in production; test/legacy providers may not expose PTY metadata.
connectionId = undefined
launchToken = undefined
}
return selectExactWorkerProviderSession({
paneKey,
processIncarnation,
connectionId,
launchToken,
observedAfter,
statuses: this.getAgentStatusSnapshotFn?.() ?? []
})
}
validateOrchestrationAgentLauncher(agent: TuiAgent): void {
const settings = this.store?.getSettings()
if (!settings) {
throw new Error('runtime_unavailable')
}
if (!isTuiAgentEnabled(agent, settings.disabledTuiAgents)) {
throw new OrchestrationError(
'agent_unconfigured',
`Agent launcher ${agent} is disabled or unavailable.`
)
}
}
resolveTerminalPane(paneKey: string, expectedWorktreeId?: string): RuntimeTerminalResolvePane {
// Why: the renderer context menu only knows the stable pane key; main owns
// the runtime terminal handle that agents and CLI commands can address.
@@ -14485,6 +14702,62 @@ export class OrcaRuntimeService {
})
}
async waitForSetupTerminalCompletion(handle: string): Promise<{ exitCode: number | null }> {
const ptyId = this.getLivePtyForHandle(handle)?.pty.ptyId
if (!ptyId) {
throw new Error('terminal_handle_stale')
}
const completionToken = this.setupCompletionTokenByPtyId.get(ptyId)
const exitAbort = new AbortController()
return await new Promise<{ exitCode: number | null }>((resolve, reject) => {
let settled = false
let unsubscribe: (() => void) | null = null
const cleanup = (): void => {
unsubscribe?.()
exitAbort.abort()
}
const finish = (exitCode: number | null): void => {
if (settled) {
return
}
settled = true
cleanup()
this.setupCompletionTokenByPtyId.delete(ptyId)
resolve({ exitCode })
}
const fail = (error: unknown): void => {
if (settled) {
return
}
settled = true
cleanup()
reject(error)
}
const scanner = completionToken ? createSetupCompletionScanner(completionToken, finish) : null
if (scanner) {
unsubscribe = this.subscribeToTerminalData(ptyId, scanner.scan)
}
// Why: setup can finish before the observer is registered on fast local worktrees.
const replay = this.recentPtyOutputById.get(ptyId)?.read()
if (scanner && replay) {
scanner.scan(replay)
}
if (!settled) {
void this.waitForTerminal(handle, {
condition: 'exit',
signal: exitAbort.signal
})
.then((wait) => {
if (wait.satisfied && wait.condition === 'exit' && wait.status === 'exited') {
finish(wait.exitCode)
}
})
.catch(fail)
}
})
}
async getWorktreePs(limit = DEFAULT_WORKTREE_PS_LIMIT): Promise<{
worktrees: RuntimeWorktreePsSummary[]
totalCount: number
@@ -18172,16 +18445,18 @@ export class OrcaRuntimeService {
primaryTerminalHandle?: string | null
hasStartupTerminal: boolean
setupCommandPlatform: 'windows' | 'posix'
observeSetupCompletion?: boolean
// Why: when the agent startup is sequenced to wait for setup
// (waitForAgentStartup), the startup PTY runs a wrapper that already embeds
// the setup command. Pass that wrapped command through so the Setup tab runs
// the same script the agent is waiting on instead of a bare runner.
wrappedSetupCommand?: string
}): Promise<{ setupSpawned: boolean }> {
}): Promise<{ setupSpawned: boolean; setupTerminalHandle: string | null }> {
if (!this.ptyController?.spawn) {
return { setupSpawned: false }
return { setupSpawned: false, setupTerminalHandle: null }
}
let setupSpawned = false
let setupTerminalHandle: string | null = null
try {
const defaultTabHandles = await this.createDefaultTabTerminals(
args.worktreeSelector,
@@ -18200,25 +18475,41 @@ export class OrcaRuntimeService {
primaryTerminalHandle = terminal.handle
}
if (args.setup) {
const completionToken =
args.observeSetupCompletion && !args.wrappedSetupCommand ? randomUUID() : null
const observedCommand = completionToken
? buildObservedSetupCommand(
args.setup.runnerScriptPath,
args.setupCommandPlatform,
completionToken
)
: null
const setupCommand =
args.wrappedSetupCommand ??
observedCommand?.command ??
buildSetupRunnerCommand(args.setup.runnerScriptPath, args.setupCommandPlatform)
const setupEnv = { ...args.setup.envVars, ...observedCommand?.env }
const shouldSplitSetup =
primaryTerminalHandle &&
(setupLaunchMode === 'split-vertical' || setupLaunchMode === 'split-horizontal')
await (shouldSplitSetup
const setupTerminal = await (shouldSplitSetup
? this.splitTerminal(primaryTerminalHandle!, {
direction: setupLaunchMode === 'split-horizontal' ? 'horizontal' : 'vertical',
command: setupCommand,
env: args.setup.envVars,
env: setupEnv,
activate: false
})
: this.createTerminal(args.worktreeSelector, {
title: 'Setup',
command: setupCommand,
env: args.setup.envVars
env: setupEnv
}))
setupTerminalHandle = setupTerminal.handle
setupSpawned = true
const ptyId = this.getLivePtyForHandle(setupTerminal.handle)?.pty.ptyId
if (completionToken && ptyId) {
this.setupCompletionTokenByPtyId.set(ptyId, completionToken)
}
}
} catch (err) {
const message = err instanceof Error ? err.message : String(err)
@@ -18226,7 +18517,7 @@ export class OrcaRuntimeService {
`[worktree-create] Failed to create setup/default terminals for ${args.worktreePath}: ${message}`
)
}
return { setupSpawned }
return { setupSpawned, setupTerminalHandle }
}
private async waitForStartupFollowupReady(
@@ -18360,6 +18651,8 @@ export class OrcaRuntimeService {
runHooks?: boolean
activate?: boolean
setupDecision?: 'run' | 'skip' | 'inherit'
awaitTerminalProvisioning?: boolean
observeSetupCompletion?: boolean
createdWithAgent?: TuiAgent
startupAgent?: TuiAgent
startupPrompt?: string
@@ -19160,6 +19453,7 @@ export class OrcaRuntimeService {
// RPC return value must omit setup so the client does not spawn it a second
// time. Mirrors the wait-for-agent setup contract from #6298.
let didSpawnSetup = false
let setupTerminalHandle: string | null = null
let startupTerminalHandle: string | null = null
let startupTerminalTabId: string | null = null
let startupTerminalPaneKey: string | null = null
@@ -19250,11 +19544,13 @@ export class OrcaRuntimeService {
? 'windows'
: 'posix'
: 'posix',
observeSetupCompletion: args.observeSetupCompletion,
// Why: carry the wait-for-agent wrapped setup command (#6298) so the
// Setup tab runs the same script the sequenced agent waits on.
...(wrappedSetupCommandStr ? { wrappedSetupCommand: wrappedSetupCommandStr } : {})
})
didSpawnSetup = provisioned.setupSpawned
setupTerminalHandle = provisioned.setupTerminalHandle
}
// Why: when runtime spawned setup, omit it from activation. When setup
// spawn failed, fall through with the wrapped command so renderer
@@ -19290,7 +19586,7 @@ export class OrcaRuntimeService {
} else if (this.ptyController?.spawn && (setup || defaultTabs || didSpawnStartup)) {
// Why: inactive terminal materialization matches normal worktree creation,
// but setup/default tab failures must not gate automation dispatch.
void this.provisionManagedWorktreeTerminals({
const provisioning = this.provisionManagedWorktreeTerminals({
worktreeSelector: `id:${worktree.id}`,
worktreeId: worktree.id,
worktreePath,
@@ -19303,12 +19599,20 @@ export class OrcaRuntimeService {
? 'windows'
: 'posix'
: 'posix',
observeSetupCompletion: args.observeSetupCompletion,
...(wrappedSetupCommandStr ? { wrappedSetupCommand: wrappedSetupCommandStr } : {})
})
// Why: runtime owns setup spawning here, so the RPC result must omit setup
// to keep the headless/mobile caller from launching it a second time.
if (setup) {
didSpawnSetup = true
if (args.awaitTerminalProvisioning) {
const provisioned = await provisioning
didSpawnSetup = provisioned.setupSpawned
setupTerminalHandle = provisioned.setupTerminalHandle
} else {
void provisioning
if (setup) {
didSpawnSetup = true
}
}
} else if (this.ptyController?.spawn) {
try {
@@ -19348,6 +19652,25 @@ export class OrcaRuntimeService {
},
...(lineageInput ? { lineage, workspaceLineage, warnings: lineageWarnings } : {}),
...(returnedSetup ? { setup: returnedSetup } : {}),
...(args.awaitTerminalProvisioning
? {
setupReceipt: {
requested: effectiveDecision,
hookFound: Boolean(hooks?.scripts.setup),
startupPolicy: setup?.waitForAgentStartup
? ('wait-for-setup' as const)
: ('start-immediately' as const),
state: !hooks?.scripts.setup
? ('not_configured' as const)
: effectiveDecision === 'skip' || !shouldRunSetup
? ('skipped' as const)
: didSpawnSetup
? ('running' as const)
: ('spawn_failed' as const),
...(setupTerminalHandle ? { terminalHandle: setupTerminalHandle } : {})
}
}
: {}),
...(defaultTabs ? { defaultTabs } : {}),
...(warning ? { warning } : {}),
...(addResult.localBaseRefRefresh
@@ -19397,6 +19720,8 @@ export class OrcaRuntimeService {
runHooks?: boolean
activate?: boolean
setupDecision?: 'run' | 'skip' | 'inherit'
awaitTerminalProvisioning?: boolean
observeSetupCompletion?: boolean
createdWithAgent?: TuiAgent
pendingFirstAgentMessageRename?: boolean
automationProvenance?: AutomationWorkspaceProvenance
@@ -19474,6 +19799,7 @@ export class OrcaRuntimeService {
// Why: same no-double-spawn contract as the local path — once runtime
// provisions setup, omit it from activation and the RPC result.
let didSpawnSetup = false
let setupTerminalHandle: string | null = null
let startupTerminalHandle: string | null = null
let startupTerminalTabId: string | null = null
let startupTerminalPaneKey: string | null = null
@@ -19557,11 +19883,13 @@ export class OrcaRuntimeService {
? 'windows'
: 'posix'
: 'posix',
observeSetupCompletion: args.observeSetupCompletion,
// Why: carry the wait-for-agent wrapped setup command (#6298) so the
// remote Setup tab runs the same script the sequenced agent waits on.
...(wrappedSetupCommandStr ? { wrappedSetupCommand: wrappedSetupCommandStr } : {})
})
didSpawnSetup = provisioned.setupSpawned
setupTerminalHandle = provisioned.setupTerminalHandle
}
// Why: omit setup from activation when runtime spawned it; on spawn
// failure fall through with the wrapped command so renderer retries.
@@ -19602,7 +19930,7 @@ export class OrcaRuntimeService {
) {
// Why: inactive terminal materialization matches normal worktree creation,
// but setup/default tab failures must not gate automation dispatch.
void this.provisionManagedWorktreeTerminals({
const provisioning = this.provisionManagedWorktreeTerminals({
worktreeSelector: `path:${result.worktree.path}`,
worktreeId: result.worktree.id,
worktreePath: result.worktree.path,
@@ -19615,12 +19943,20 @@ export class OrcaRuntimeService {
? 'windows'
: 'posix'
: 'posix',
observeSetupCompletion: args.observeSetupCompletion,
...(wrappedSetupCommandStr ? { wrappedSetupCommand: wrappedSetupCommandStr } : {})
})
// Why: runtime owns setup spawning here, so omit setup from the RPC result
// to keep the headless/mobile caller from launching it a second time.
if (result.setup) {
didSpawnSetup = true
if (args.awaitTerminalProvisioning) {
const provisioned = await provisioning
didSpawnSetup = provisioned.setupSpawned
setupTerminalHandle = provisioned.setupTerminalHandle
} else {
void provisioning
if (result.setup) {
didSpawnSetup = true
}
}
} else if (!shouldActivate && this.ptyController?.spawn) {
try {
@@ -19665,7 +20001,27 @@ export class OrcaRuntimeService {
}
: resultForRenderer
return warning ? { ...resultWithStartupTerminal, warning } : resultWithStartupTerminal
const requestedSetupDecision = args.runHooks ? 'run' : (args.setupDecision ?? 'inherit')
const setupReceipt = {
requested: requestedSetupDecision,
hookFound: Boolean(result.setup),
startupPolicy: result.setup?.waitForAgentStartup
? ('wait-for-setup' as const)
: ('start-immediately' as const),
state:
requestedSetupDecision === 'skip'
? ('skipped' as const)
: !result.setup
? ('not_configured' as const)
: didSpawnSetup
? ('running' as const)
: ('spawn_failed' as const),
...(setupTerminalHandle ? { terminalHandle: setupTerminalHandle } : {})
}
const resultWithSetupReceipt = args.awaitTerminalProvisioning
? { ...resultWithStartupTerminal, setupReceipt }
: resultWithStartupTerminal
return warning ? { ...resultWithSetupReceipt, warning } : resultWithSetupReceipt
}
/**
@@ -25598,6 +25954,7 @@ export class OrcaRuntimeService {
this.advancePtyLifecycleGeneration(ptyId)
this.ptysById.delete(ptyId)
this.recentPtyOutputById.delete(ptyId)
this.setupCompletionTokenByPtyId.delete(ptyId)
this.clearWaitBlockedCheckState(ptyId)
this.recentPtyPathCandidatesById.delete(ptyId)
this.ptyOutputSequenceById.delete(ptyId)
@@ -27057,15 +27414,25 @@ export class OrcaRuntimeService {
if (messageType && waiter.typeFilter && !waiter.typeFilter.includes(messageType)) {
continue
}
this.resolveMessageWaiter(waiter)
this.resolveMessageWaiter(waiter, 'notified')
}
}
waitForMessage(
handle: string,
options?: { typeFilter?: string[]; timeoutMs?: number; signal?: AbortSignal }
): Promise<void> {
options?: {
typeFilter?: string[]
timeoutMs?: number
signal?: AbortSignal
exclusive?: boolean
}
): Promise<MessageWaitResult> {
return new Promise((resolve) => {
const currentWaiters = this.messageWaitersByHandle.get(handle)
if (options?.exclusive && currentWaiters && currentWaiters.size > 0) {
resolve('waiter_exists')
return
}
const timeoutMs = options?.timeoutMs ?? MESSAGE_WAIT_DEFAULT_TIMEOUT_MS
const waiter: MessageWaiter = {
@@ -27080,11 +27447,11 @@ export class OrcaRuntimeService {
const signal = options?.signal
const onAbort = (): void => {
this.removeMessageWaiter(waiter)
resolve()
resolve('cancelled')
}
if (signal) {
if (signal.aborted) {
resolve()
resolve('cancelled')
return
}
waiter.abortCleanup = () => signal.removeEventListener('abort', onAbort)
@@ -27093,7 +27460,7 @@ export class OrcaRuntimeService {
waiter.timeout = setTimeout(() => {
this.removeMessageWaiter(waiter)
resolve()
resolve('timed_out')
}, timeoutMs)
let waiters = this.messageWaitersByHandle.get(handle)
@@ -27105,9 +27472,19 @@ export class OrcaRuntimeService {
})
}
private resolveMessageWaiter(waiter: MessageWaiter): void {
cancelMessageWaiters(handle: string): void {
const waiters = this.messageWaitersByHandle.get(handle)
if (!waiters) {
return
}
for (const waiter of [...waiters]) {
this.resolveMessageWaiter(waiter, 'cancelled')
}
}
private resolveMessageWaiter(waiter: MessageWaiter, result: MessageWaitResult): void {
this.removeMessageWaiter(waiter)
waiter.resolve()
waiter.resolve(result)
}
private removeMessageWaiter(waiter: MessageWaiter): void {
@@ -19,7 +19,7 @@ import { spawn } from 'node:child_process'
import { existsSync, mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { OrcaRuntimeService } from './orca-runtime'
import { OrchestrationDb } from './orchestration/db'
import { OrcaRuntimeRpcServer } from './runtime-rpc'
@@ -198,6 +198,15 @@ describeIfBuilt('orca orchestration reset subprocess', () => {
const runtime = new OrcaRuntimeService()
const db = new OrchestrationDb(':memory:')
runtime.setOrchestrationDb(db)
const coordinatorPaneKey = 'tab_cli:11111111-1111-4111-8111-111111111111'
vi.spyOn(runtime, 'getTerminalPaneKey').mockImplementation((handle) =>
handle === 'term_cli' ? coordinatorPaneKey : null
)
db.createRun({
objective: 'CLI reset subprocess fixture',
coordinatorHandle: 'term_cli',
coordinatorPaneKey
})
const server = new OrcaRuntimeRpcServer({ runtime, userDataPath })
await server.start()
@@ -218,6 +227,8 @@ describeIfBuilt('orca orchestration reset subprocess', () => {
'task-create',
'--spec',
'throwaway task',
'--from',
'term_cli',
'--json'
])
expect(create.exitCode, create.stderr).toBe(0)
@@ -253,18 +264,41 @@ describeIfBuilt('orca orchestration reset subprocess', () => {
expect(db.getInbox()).toHaveLength(1)
expect(db.listTasks()).toHaveLength(0)
// Why: task resets intentionally remove Runs, so recreate the caller's
// normal binding before exercising task creation again.
db.createRun({
objective: 'CLI reset subprocess fixture after reset',
coordinatorHandle: 'term_cli',
coordinatorPaneKey
})
const recreate = await runBuiltCli(userDataPath, [
'orchestration',
'task-create',
'--spec',
'throwaway task after partial reset',
'--from',
'term_cli',
'--json'
])
expect(recreate.exitCode, recreate.stderr).toBe(0)
expect(db.getInbox()).toHaveLength(1)
expect(db.listTasks()).toHaveLength(1)
const resetAll = await runBuiltCli(userDataPath, ['orchestration', 'reset', '--json'])
const bareReset = await runBuiltCli(userDataPath, ['orchestration', 'reset', '--json'])
expect(bareReset.exitCode).toBe(1)
expect(JSON.parse(bareReset.stdout)).toMatchObject({
ok: false,
error: { code: 'invalid_argument' }
})
expect(db.getInbox()).toHaveLength(1)
expect(db.listTasks()).toHaveLength(1)
const resetAll = await runBuiltCli(userDataPath, [
'orchestration',
'reset',
'--all',
'--json'
])
expect(resetAll.exitCode, resetAll.stderr).toBe(0)
expect(JSON.parse(resetAll.stdout)).toMatchObject({ ok: true, result: { reset: 'all' } })
expect(db.getInbox()).toHaveLength(0)
@@ -10,7 +10,7 @@ Slack, GitHub comments, or any other channel to reach a human during the run.
=== CLI COMMANDS ===
# Report task completion (REQUIRED when done — even on failure).
# Report the terminal task outcome (REQUIRED exactly once).
#
# RULE: --body must be a 3-sentence executive summary (what you did,
# what you found, what's left). Never send an empty body; the coordinator
@@ -18,14 +18,15 @@ Slack, GitHub comments, or any other channel to reach a human during the run.
# If you produced a long-form artifact, include its path as
# payload.reportPath so the coordinator can find it without a file search.
#
# RULE: send worker_done exactly once. Failure is still a worker_done
# with subject like "Failed: <reason>" — never silently exit.
# RULE: send worker_done exactly once. Use --outcome succeeded when the
# requested work is done, or replace it with --outcome failed when it is not.
# Never encode failure only in prose and never silently exit.
# Include BOTH taskId and dispatchId in the payload so a late completion
# from a failed retry cannot complete the current dispatch.
orca orchestration send --to term_COORD --from term_WORKER \\
orca orchestration send --from term_WORKER \\
--type worker_done --subject "<short status>" \\
--body "<3-sentence summary: what you did, what you found, what's left>" \\
--task-id task_SNAP --dispatch-id ctx_SNAP \\
--task-id task_SNAP --dispatch-id ctx_SNAP --outcome succeeded \\
--files-modified "path/a,path/b" \\
--report-path "<optional: path to the full artifact>"
@@ -39,7 +40,7 @@ Slack, GitHub comments, or any other channel to reach a human during the run.
# attributes the heartbeat to the specific dispatch context, not just
# the task, so a straggler heartbeat from a previously-failed dispatch
# cannot mask a hung retry.
orca orchestration send --to term_COORD --from term_WORKER \\
orca orchestration send --from term_WORKER \\
--type heartbeat --subject "alive" \\
--task-id task_SNAP --dispatch-id ctx_SNAP \\
--phase "<short: investigating|implementing|reviewing|waiting>"
@@ -52,18 +53,18 @@ Slack, GitHub comments, or any other channel to reach a human during the run.
# coordinator cannot see and cannot answer — your session will hang forever
# waiting on a human. Every interactive question goes through \`ask\` below.
#
# The \`ask\` verb is a thin wrapper: it sends a decision_gate message and
# blocks on \`check --wait\` until the coordinator replies, then prints the
# reply body. Use it anywhere you would otherwise have reached for
# AskUserQuestion.
orca orchestration ask --to term_COORD --from term_WORKER \\
# The \`ask\` verb durably records a question in this Dispatch's Run and
# blocks until the coordinator replies, then prints the reply body. If the
# call times out or disconnects, resume with the returned message ID instead
# of creating a duplicate question.
orca orchestration ask --from term_WORKER \\
--question "<your question>" \\
--options "<optional,comma,separated>" \\
--timeout-ms 600000
# Escalate a blocker or failure (pre-completion, when you need the
# coordinator to do something before you can continue):
orca orchestration send --to term_COORD --from term_WORKER \\
orca orchestration send --from term_WORKER \\
--type escalation --subject "Blocked: <reason>" \\
--body "<details>" \\
--task-id task_SNAP
@@ -98,6 +98,7 @@ function insertWorkerDone(
payload: JSON.stringify({
taskId: params.taskId,
dispatchId,
outcome: 'succeeded',
...(params.filesModified ? { filesModified: params.filesModified } : {})
}),
senderPaneKey:
@@ -192,7 +193,7 @@ describe('Coordinator', () => {
to: 'coord',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id })
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id, outcome: 'succeeded' })
})
reconcileLifecycleMessage(db, msg)
@@ -214,7 +215,11 @@ describe('Coordinator', () => {
const task = db.createTask({ spec: 'duplicate completion' })
const dispatch = db.createDispatchContext(task.id, 'term_a')
const payload = JSON.stringify({ taskId: task.id, dispatchId: dispatch.id })
const payload = JSON.stringify({
taskId: task.id,
dispatchId: dispatch.id,
outcome: 'succeeded'
})
const first = db.insertMessage({
from: 'term_a',
to: 'coord',
@@ -569,7 +574,11 @@ describe('Coordinator', () => {
to: 'coord',
subject: 'Late done',
type: 'worker_done',
payload: JSON.stringify({ taskId: task.id, dispatchId: staleCtx.id })
payload: JSON.stringify({
taskId: task.id,
dispatchId: staleCtx.id,
outcome: 'succeeded'
})
})
const staleCoordinator = new Coordinator(db, runtime, {
@@ -621,7 +630,7 @@ describe('Coordinator', () => {
to: 'coord',
subject: 'Done after restart',
type: 'worker_done',
payload: JSON.stringify({ taskId: task.id, dispatchId: ctx.id }),
payload: JSON.stringify({ taskId: task.id, dispatchId: ctx.id, outcome: 'succeeded' }),
senderPaneKey: `tab_after:${leafId}`
})
@@ -242,6 +242,7 @@ export class Coordinator {
case 'dispatch':
case 'handoff':
case 'merge_ready':
case 'question':
break
}
}
@@ -255,6 +256,10 @@ export class Coordinator {
if (!this.state.completedTasks.includes(result.taskId)) {
this.state.completedTasks.push(result.taskId)
}
return
}
if (result.action === 'failed' && !this.state.failedTasks.includes(result.taskId)) {
this.state.failedTasks.push(result.taskId)
}
}
+3 -1
View File
@@ -3,7 +3,7 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import Database from '../../sqlite/sync-database'
import { OrchestrationDb } from './db'
import { LEGACY_RUN_ID, OrchestrationDb } from './db'
import type { MessageType } from './db'
// Overwrites the datetime('now')-seeded timestamps with explicit fixture values
@@ -942,6 +942,8 @@ describe('OrchestrationDb', () => {
// v1 data preserved
expect(d.getMessageById('msg_v1')?.subject).toBe('pre-migration')
expect(d.getMessageById('msg_v1')?.run_id).toBe(LEGACY_RUN_ID)
expect(d.getRun(LEGACY_RUN_ID)).toMatchObject({ legacy: 1 })
})
it('adds pane-identity columns (v6) and persists them', () => {
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,26 @@
import { createHash } from 'node:crypto'
import type {
RuntimeOrchestrationEnvelope,
RuntimeRpcResponse
} from '../../../shared/runtime-rpc-envelope'
export type OrchestrationWorkerServer = {
environmentId: string
name: string
peerFingerprint: string
}
export type OrchestrationEnvironmentTransport = {
resolve(selector: string): OrchestrationWorkerServer
call(
selector: string,
method: string,
params: unknown,
timeoutMs?: number,
envelope?: RuntimeOrchestrationEnvelope
): Promise<RuntimeRpcResponse<unknown>>
}
export function fingerprintOrchestrationPeer(publicKeyB64: string): string {
return createHash('sha256').update(Buffer.from(publicKeyB64, 'base64')).digest('base64url')
}
@@ -0,0 +1,94 @@
import { MESSAGE_TYPES, type MessagePriority, type MessageType } from './types'
import type { OrchestrationDb } from './db'
import { OrchestrationError } from './orchestration-error'
const MESSAGE_TYPE_SET = new Set<MessageType>(MESSAGE_TYPES)
export type FederatedControlMessage = {
from: string
subject: string
body: string
type: MessageType
priority: MessagePriority
threadId: string | null
payload: string | null
}
export function encodeFederatedControlMessage(message: FederatedControlMessage): string {
return JSON.stringify(message)
}
export function parseFederatedControlMessage(payload: string): FederatedControlMessage {
let parsed: unknown
try {
parsed = JSON.parse(payload)
} catch {
throw new OrchestrationError('invalid_argument', 'Federated control message is invalid JSON.')
}
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new OrchestrationError('invalid_argument', 'Federated control message is invalid.')
}
const message = parsed as Partial<FederatedControlMessage>
if (
typeof message.from !== 'string' ||
typeof message.subject !== 'string' ||
typeof message.body !== 'string' ||
typeof message.type !== 'string' ||
!MESSAGE_TYPE_SET.has(message.type as MessageType)
) {
throw new OrchestrationError('invalid_argument', 'Federated control message is incomplete.')
}
return {
from: message.from,
subject: message.subject,
body: message.body,
type: message.type as MessageType,
priority:
message.priority === 'high' || message.priority === 'urgent' ? message.priority : 'normal',
threadId: typeof message.threadId === 'string' ? message.threadId : null,
payload: typeof message.payload === 'string' ? message.payload : null
}
}
export function importFederatedControlMessage(
db: OrchestrationDb,
params: {
dispatchId: string
messageId: string
payload: string
}
): { imported: boolean; type: MessageType } {
const message = parseFederatedControlMessage(params.payload)
const recipient = `dispatch:${params.dispatchId}`
const existing = db.getMessageById(params.messageId)
if (existing) {
if (
existing.to_handle !== recipient ||
existing.from_handle !== message.from ||
existing.subject !== message.subject ||
existing.body !== message.body ||
existing.type !== message.type ||
existing.priority !== message.priority ||
existing.thread_id !== message.threadId ||
existing.payload !== message.payload
) {
throw new OrchestrationError(
'request_mismatch',
`Federated control message ${params.messageId} conflicts with an existing message.`
)
}
return { imported: false, type: message.type }
}
db.insertMessage({
id: params.messageId,
from: message.from,
to: recipient,
subject: message.subject,
body: message.body,
type: message.type,
priority: message.priority,
threadId: message.threadId ?? undefined,
payload: message.payload ?? undefined
})
return { imported: true, type: message.type }
}
@@ -0,0 +1,18 @@
import { describe, expect, it } from 'vitest'
import { parseRelayedMessage } from './federation-sync'
describe('federation relay parsing', () => {
it('accepts a supported message type', () => {
expect(
parseRelayedMessage(
JSON.stringify({ subject: 'done', body: 'Finished', type: 'worker_done' })
)
).toMatchObject({ type: 'worker_done', priority: 'normal' })
})
it('rejects an unsupported type before it reaches the database constraint', () => {
expect(() =>
parseRelayedMessage(JSON.stringify({ subject: 'bad', body: 'Blocked', type: 'invented' }))
).toThrowError('Federated relay message type invented is not supported.')
})
})
@@ -0,0 +1,253 @@
import {
MESSAGE_TYPES,
type MessagePriority,
type MessageType,
type WorkerReportOutcome
} from './types'
import type { OrcaRuntimeService } from '../orca-runtime'
import { OrchestrationError } from './orchestration-error'
const MESSAGE_TYPE_SET = new Set<MessageType>(MESSAGE_TYPES)
function isMessageType(value: unknown): value is MessageType {
return typeof value === 'string' && MESSAGE_TYPE_SET.has(value as MessageType)
}
type PulledRelayItem = {
dispatch_id: string
direction: 'to_home'
sequence: number
message_id: string
kind: string
payload: string
}
type RelayedMessage = {
from: string
subject: string
body: string
type: MessageType
priority: MessagePriority
threadId: string | null
payload: string | null
}
export async function syncFederatedDispatch(
runtime: OrcaRuntimeService,
dispatchId: string
): Promise<{ imported: number; acknowledgedThrough: number }> {
const db = runtime.getOrchestrationDb()
const federated = db.getFederatedDispatch(dispatchId)
const dispatch = db.getDispatchContextById(dispatchId)
if (!federated || !dispatch) {
throw new OrchestrationError(
'dispatch_not_found',
`Federated Dispatch ${dispatchId} was not found.`
)
}
const currentServer = runtime.resolveOrchestrationWorkerServer(federated.environment_id)
if (currentServer.peerFingerprint !== federated.peer_fingerprint) {
throw new OrchestrationError(
'peer_changed',
`Saved environment ${federated.environment_name} now identifies a different Orca server.`
)
}
const pulled = (await runtime.callOrchestrationWorkerServer(
federated.environment_id,
'orchestration.federationPull',
{
dispatchId,
afterSequence: federated.to_home_imported_sequence,
limit: 50
},
15_000
)) as { runtimeEpoch: string; items: PulledRelayItem[] }
let cursor = federated.to_home_imported_sequence
let imported = 0
for (const item of pulled.items) {
if (item.dispatch_id !== dispatchId || item.sequence !== cursor + 1) {
throw new OrchestrationError(
'operation_unknown',
`Federated relay for ${dispatchId} is not contiguous after sequence ${cursor}.`
)
}
const message = parseRelayedMessage(item.payload)
const stored = db.importFederatedRelayItem({
dispatchId,
sequence: item.sequence,
message: {
id: item.message_id,
runId: dispatch.run_id,
from: `dispatch:${dispatchId}`,
to: `run:${dispatch.run_id}`,
subject: message.subject,
body: message.body,
type: message.type,
priority: message.priority,
threadId: message.threadId ?? undefined,
payload: message.payload ?? undefined
},
lifecycle: parseFederatedLifecycle(message, item.message_id, dispatchId, dispatch.task_id)
})
cursor = item.sequence
runtime.notifyMessageArrived(stored.message.to_handle, stored.message.type)
imported += stored.duplicate ? 0 : 1
}
if (cursor > 0) {
await runtime.callOrchestrationWorkerServer(
federated.environment_id,
'orchestration.federationAck',
{ dispatchId, throughSequence: cursor },
15_000,
{ orchestrationRequestId: `relay_ack_${dispatchId}_${cursor}` }
)
}
const toWorker =
db.getWorkerDispatch(dispatchId)?.state === 'ready'
? db.listPendingFederationRelay(dispatchId, 'to_worker')
: []
if (toWorker.length > 0) {
const delivered = (await runtime.callOrchestrationWorkerServer(
federated.environment_id,
'orchestration.federationImport',
{ dispatchId, items: toWorker },
15_000,
{
orchestrationRequestId: `relay_import_${dispatchId}_${toWorker.at(-1)?.sequence ?? 0}`
}
)) as { acknowledgedThrough: number }
db.acknowledgeFederationRelay({
dispatchId,
direction: 'to_worker',
throughSequence: delivered.acknowledgedThrough
})
}
return { imported, acknowledgedThrough: cursor }
}
export function parseRelayedMessage(payload: string): RelayedMessage {
let parsed: unknown
try {
parsed = JSON.parse(payload)
} catch {
throw new OrchestrationError('invalid_argument', 'Federated relay payload is invalid JSON.')
}
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new OrchestrationError('invalid_argument', 'Federated relay payload is not a message.')
}
const message = parsed as Partial<RelayedMessage>
if (typeof message.subject !== 'string' || typeof message.body !== 'string') {
throw new OrchestrationError('invalid_argument', 'Federated relay message is incomplete.')
}
if (!isMessageType(message.type)) {
throw new OrchestrationError(
'invalid_argument',
`Federated relay message type ${String(message.type)} is not supported.`
)
}
return {
from: typeof message.from === 'string' ? message.from : 'remote-worker',
subject: message.subject,
body: message.body,
type: message.type,
priority:
message.priority === 'high' || message.priority === 'urgent' ? message.priority : 'normal',
threadId: typeof message.threadId === 'string' ? message.threadId : null,
payload: typeof message.payload === 'string' ? message.payload : null
}
}
function parseFederatedLifecycle(
message: RelayedMessage,
messageId: string,
dispatchId: string,
taskId: string
):
| { kind: 'none' }
| { kind: 'heartbeat'; at: string }
| {
kind: 'worker_report'
taskId: string
outcome: WorkerReportOutcome
result: string
}
| { kind: 'rejected'; code: string; reason: string } {
if (message.type === 'heartbeat') {
return { kind: 'heartbeat', at: new Date().toISOString() }
}
if (message.type !== 'worker_done') {
return { kind: 'none' }
}
let payload
try {
payload = parseWorkerReportPayload(message.payload)
} catch (error) {
return {
kind: 'rejected',
code: 'invalid_payload',
reason: error instanceof Error ? error.message : String(error)
}
}
if (payload.dispatchId !== dispatchId || payload.taskId !== taskId) {
return {
kind: 'rejected',
code: 'task_dispatch_mismatch',
reason: `Federated report does not match Dispatch ${dispatchId}.`
}
}
const result = JSON.stringify({
provenance: 'worker_report',
outcome: payload.outcome,
messageId,
reportedBy: `dispatch:${dispatchId}`,
subject: message.subject,
body: message.body,
completedBy: `dispatch:${dispatchId}`,
filesModified: payload.filesModified,
reportPath: payload.reportPath,
completedAt: new Date().toISOString()
})
return {
kind: 'worker_report',
taskId: payload.taskId,
outcome: payload.outcome,
result
}
}
function parseWorkerReportPayload(payload: string | null): {
taskId: string
dispatchId: string
outcome: WorkerReportOutcome
filesModified: string[]
reportPath: string | null
} {
let parsed: unknown
try {
parsed = payload ? JSON.parse(payload) : null
} catch {
parsed = null
}
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
throw new OrchestrationError('invalid_argument', 'Federated worker report is invalid.')
}
const report = parsed as Record<string, unknown>
if (
typeof report.taskId !== 'string' ||
typeof report.dispatchId !== 'string' ||
(report.outcome !== 'succeeded' && report.outcome !== 'failed')
) {
throw new OrchestrationError('invalid_argument', 'Federated worker report is incomplete.')
}
return {
taskId: report.taskId,
dispatchId: report.dispatchId,
outcome: report.outcome,
filesModified: Array.isArray(report.filesModified)
? report.filesModified.filter((file): file is string => typeof file === 'string')
: [],
reportPath: typeof report.reportPath === 'string' ? report.reportPath : null
}
}
@@ -5,6 +5,7 @@ import type { MessageRow } from './types'
function makeMessage(overrides: Partial<MessageRow> = {}): MessageRow {
return {
id: 'msg_test1',
run_id: 'run_test',
from_handle: 'term_abc123',
to_handle: 'term_coord',
subject: 'Auth API implementation complete',
@@ -17,7 +17,7 @@ describe('lifecycle reconciliation', () => {
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id })
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id, outcome: 'succeeded' })
})
expect(reconcileLifecycleMessage(db, message, (line) => logs.push(line))).toMatchObject({
@@ -43,7 +43,7 @@ describe('lifecycle reconciliation', () => {
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id }),
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id, outcome: 'succeeded' }),
senderPaneKey: `tab_w:${LEAF_A}`
})
@@ -51,6 +51,94 @@ describe('lifecycle reconciliation', () => {
expect(db.getTask(task.id)?.status).toBe('completed')
})
it('fails both the dispatch and task from an authenticated failed worker report', () => {
db = new OrchestrationDb(':memory:')
const task = db.createTask({ spec: 'work' })
const dispatch = db.createDispatchContext(task.id, 'term_worker', `tab_w:${LEAF_A}`)
const message = db.insertMessage({
from: 'term_worker',
to: 'term_coordinator',
subject: 'Failed: tests cannot start',
body: 'I attempted the work. The required service is unavailable. No files changed.',
type: 'worker_done',
payload: JSON.stringify({
taskId: task.id,
dispatchId: dispatch.id,
outcome: 'failed',
filesModified: []
}),
senderPaneKey: `tab_w:${LEAF_A}`
})
expect(reconcileLifecycleMessage(db, message)).toEqual({
action: 'failed',
taskId: task.id,
dispatchId: dispatch.id
})
expect(db.getTask(task.id)).toMatchObject({ status: 'failed' })
expect(db.getDispatchContextById(dispatch.id)).toMatchObject({ status: 'failed' })
expect(JSON.parse(db.getTask(task.id)?.result ?? '{}')).toMatchObject({
provenance: 'worker_report',
outcome: 'failed',
messageId: message.id
})
})
it('replays an identical terminal outcome without mutating settled state', () => {
db = new OrchestrationDb(':memory:')
const task = db.createTask({ spec: 'work' })
const dispatch = db.createDispatchContext(task.id, 'term_worker')
const makeMessage = () =>
db.insertMessage({
from: 'term_worker',
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({
taskId: task.id,
dispatchId: dispatch.id,
outcome: 'succeeded'
})
})
expect(reconcileLifecycleMessage(db, makeMessage()).action).toBe('completed')
const result = db.getTask(task.id)?.result
expect(reconcileLifecycleMessage(db, makeMessage()).action).toBe('completed')
expect(db.getTask(task.id)?.result).toBe(result)
})
it.each([
{ payload: undefined, code: 'invalid_payload' },
{ payload: '{', code: 'invalid_payload' },
{
payload: JSON.stringify({ dispatchId: 'ctx_1', outcome: 'succeeded' }),
code: 'missing_task_id'
},
{
payload: JSON.stringify({ taskId: 'task_1', outcome: 'succeeded' }),
code: 'missing_dispatch_id'
},
{
payload: JSON.stringify({ taskId: 'task_1', dispatchId: 'ctx_1', outcome: 'maybe' }),
code: 'invalid_outcome'
}
])('rejects malformed worker reports with $code', ({ payload, code }) => {
db = new OrchestrationDb(':memory:')
const message = db.insertMessage({
from: 'term_worker',
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload
})
expect(reconcileLifecycleMessage(db, message)).toMatchObject({ action: 'rejected', code })
expect(db.getMessageById(message.id)).toMatchObject({
priority: 'high',
subject: 'Rejected worker_done: Done'
})
})
it('completes worker_done from the same leaf after a pane break-out changed the tab half', () => {
db = new OrchestrationDb(':memory:')
const task = db.createTask({ spec: 'work' })
@@ -62,7 +150,7 @@ describe('lifecycle reconciliation', () => {
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id }),
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id, outcome: 'succeeded' }),
senderPaneKey: `tab_old:${LEAF_A}`
})
@@ -79,7 +167,7 @@ describe('lifecycle reconciliation', () => {
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id }),
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id, outcome: 'succeeded' }),
senderPaneKey: 'tab_w:42'
})
@@ -96,7 +184,7 @@ describe('lifecycle reconciliation', () => {
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id }),
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id, outcome: 'succeeded' }),
senderPaneKey: `tab_w2:${LEAF_B}`
})
@@ -142,6 +230,7 @@ describe('lifecycle reconciliation', () => {
payload: JSON.stringify({
taskId: task.id,
dispatchId: dispatch.id,
outcome: 'succeeded',
_orcaLifecycleRejection: {
code: 'sender_not_assignee',
reason: 'caller supplied'
@@ -167,7 +256,7 @@ describe('lifecycle reconciliation', () => {
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id })
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id, outcome: 'succeeded' })
})
expect(reconcileLifecycleMessage(db, message)).toMatchObject({
@@ -186,7 +275,11 @@ describe('lifecycle reconciliation', () => {
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({ taskId: acceptedTask.id, dispatchId: acceptedDispatch.id })
payload: JSON.stringify({
taskId: acceptedTask.id,
dispatchId: acceptedDispatch.id,
outcome: 'succeeded'
})
})
expect(reconcileLifecycleMessage(db, accepted).action).toBe('completed')
@@ -197,7 +290,11 @@ describe('lifecycle reconciliation', () => {
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({ taskId: rejectedTask.id, dispatchId: rejectedDispatch.id })
payload: JSON.stringify({
taskId: rejectedTask.id,
dispatchId: rejectedDispatch.id,
outcome: 'succeeded'
})
})
expect(reconcileLifecycleMessage(db, rejected)).toMatchObject({
action: 'rejected',
@@ -211,7 +308,11 @@ describe('lifecycle reconciliation', () => {
const parent = db.createTask({ spec: 'parent' })
const child = db.createTask({ spec: 'child', deps: [parent.id] })
const dispatch = db.createDispatchContext(parent.id, 'term_worker', `tab_w:${LEAF_A}`)
const payload = JSON.stringify({ taskId: parent.id, dispatchId: dispatch.id })
const payload = JSON.stringify({
taskId: parent.id,
dispatchId: dispatch.id,
outcome: 'succeeded'
})
const foreign = db.insertMessage({
from: 'term_coordinator',
@@ -243,7 +344,11 @@ describe('lifecycle reconciliation', () => {
db = new OrchestrationDb(':memory:')
const task = db.createTask({ spec: 'work' })
const dispatch = db.createDispatchContext(task.id, 'term_worker', `tab_w:${LEAF_A}`)
const payload = JSON.stringify({ taskId: task.id, dispatchId: dispatch.id })
const payload = JSON.stringify({
taskId: task.id,
dispatchId: dispatch.id,
outcome: 'succeeded'
})
const owner = db.insertMessage({
from: 'term_worker',
to: 'term_coordinator',
@@ -280,7 +385,7 @@ describe('lifecycle reconciliation', () => {
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id }),
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id, outcome: 'succeeded' }),
senderPaneKey: `tab_w2:${LEAF_B}`
})
@@ -390,7 +495,7 @@ describe('lifecycle reconciliation', () => {
to: 'term_coordinator',
subject: 'Done',
type: 'worker_done',
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id })
payload: JSON.stringify({ taskId: task.id, dispatchId: dispatch.id, outcome: 'succeeded' })
})
reconcileLifecycleMessage(db, done)
@@ -1,5 +1,5 @@
import type { OrchestrationDb } from './db'
import type { MessageRow } from './types'
import type { MessageRow, WorkerReportOutcome } from './types'
import { parsePaneKey } from '../../../shared/stable-pane-id'
// Why: the tab half can change on pane break-out, while opaque legacy keys
@@ -35,11 +35,25 @@ export type LifecycleReconciliationResult =
| { action: 'suppressed' }
| LifecycleRejectionResult
| { action: 'completed'; taskId: string; dispatchId: string }
| { action: 'failed'; taskId: string; dispatchId: string }
| { action: 'heartbeat_recorded'; dispatchId: string }
export type LifecycleRejectionCode =
| 'sender_not_assignee'
| 'dispatch_capability_invalid'
| 'invalid_payload'
| 'missing_task_id'
| 'missing_dispatch_id'
| 'invalid_outcome'
| 'unknown_task'
| 'unknown_dispatch'
| 'task_dispatch_mismatch'
| 'inactive_dispatch'
| 'stale_dispatch'
export type LifecycleRejectionResult = {
action: 'rejected'
code: 'sender_not_assignee'
code: LifecycleRejectionCode
reason: string
}
@@ -54,7 +68,11 @@ function parseObjectPayload(msg: MessageRow, onInvalidJson: () => void): Record<
try {
const parsed: unknown = JSON.parse(msg.payload)
return parsed && typeof parsed === 'object' ? (parsed as Record<string, unknown>) : {}
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
return parsed as Record<string, unknown>
}
onInvalidJson()
return {}
} catch {
onInvalidJson()
return {}
@@ -68,7 +86,7 @@ function getPersistedLifecycleRejection(
if (
!rejection ||
typeof rejection !== 'object' ||
(rejection as { code?: unknown }).code !== 'sender_not_assignee' ||
typeof (rejection as { code?: unknown }).code !== 'string' ||
typeof (rejection as { reason?: unknown }).reason !== 'string'
) {
return undefined
@@ -77,7 +95,7 @@ function getPersistedLifecycleRejection(
// also prevents caller-supplied markers from turning lifecycle sends into success.
return {
action: 'rejected',
code: 'sender_not_assignee',
code: (rejection as { code: LifecycleRejectionCode }).code,
reason: (rejection as { reason: string }).reason
}
}
@@ -98,6 +116,7 @@ export function reconcileLifecycleMessage(
case 'escalation':
case 'handoff':
case 'decision_gate':
case 'question':
return { action: 'ignored' }
}
}
@@ -142,7 +161,7 @@ function reconcileHeartbeatMessage(
// a hung assignee behind another agent's timer.
const reason = buildLifecycleAuthorityRejectionReason(dispatchId, dispatch, msg)
onLog(`Heartbeat rejected: ${reason}`)
db.convertLifecycleMessageToRejection(msg.id, reason)
db.convertLifecycleMessageToRejection(msg.id, 'sender_not_assignee', reason)
return { action: 'rejected', code: 'sender_not_assignee', reason }
}
@@ -159,7 +178,9 @@ function reconcileWorkerDoneMessage(
): LifecycleReconciliationResult {
onLog(`Worker done: ${msg.from_handle} — ${msg.subject}`)
let invalidPayload = false
const payload = parseObjectPayload(msg, () => {
invalidPayload = true
onLog(`Warning: invalid payload in worker_done from ${msg.from_handle}`)
})
const persistedRejection = getPersistedLifecycleRejection(payload)
@@ -169,58 +190,83 @@ function reconcileWorkerDoneMessage(
onLog(`Warning: worker_done rejected: ${persistedRejection.reason}`)
return persistedRejection
}
if (invalidPayload || !msg.payload) {
return rejectLifecycleMessage(
db,
msg,
'invalid_payload',
'worker_done requires a JSON object payload.',
onLog
)
}
const taskId = payload.taskId
if (typeof taskId !== 'string' || taskId.length === 0) {
onLog(`Warning: worker_done without taskId from ${msg.from_handle}`)
return { action: 'ignored' }
return rejectLifecycleMessage(db, msg, 'missing_task_id', 'worker_done requires taskId.', onLog)
}
const dispatchId = payload.dispatchId
if (typeof dispatchId !== 'string' || dispatchId.length === 0) {
onLog(`Warning: worker_done without dispatchId from ${msg.from_handle}`)
return { action: 'ignored' }
return rejectLifecycleMessage(
db,
msg,
'missing_dispatch_id',
'worker_done requires dispatchId.',
onLog
)
}
const outcome = payload.outcome
if (outcome !== 'succeeded' && outcome !== 'failed') {
return rejectLifecycleMessage(
db,
msg,
'invalid_outcome',
'worker_done requires outcome=succeeded or outcome=failed.',
onLog
)
}
const task = db.getTask(taskId)
if (!task) {
onLog(`Warning: worker_done for unknown task ${taskId}`)
return { action: 'ignored' }
return rejectLifecycleMessage(
db,
msg,
'unknown_task',
`worker_done references unknown task ${taskId}.`,
onLog
)
}
// Why: taskId alone is not a completion authority; retried tasks can have
// stale worker_done messages racing the current active dispatch.
const dispatch = db.getDispatchContextById(dispatchId)
if (!dispatch) {
onLog(`Warning: worker_done for unknown dispatch ${dispatchId}`)
return { action: 'ignored' }
return rejectLifecycleMessage(
db,
msg,
'unknown_dispatch',
`worker_done references unknown dispatch ${dispatchId}.`,
onLog
)
}
if (dispatch.task_id !== taskId) {
onLog(
`Warning: worker_done dispatch ${dispatchId} belongs to ${dispatch.task_id}, not ${taskId}`
return rejectLifecycleMessage(
db,
msg,
'task_dispatch_mismatch',
`worker_done dispatch ${dispatchId} belongs to ${dispatch.task_id}, not ${taskId}.`,
onLog
)
return { action: 'ignored' }
}
if (!hasLifecycleAuthority(dispatch, msg)) {
const reason = buildLifecycleAuthorityRejectionReason(dispatchId, dispatch, msg)
onLog(`Warning: worker_done rejected: ${reason}`)
db.convertLifecycleMessageToRejection(msg.id, reason)
db.convertLifecycleMessageToRejection(msg.id, 'sender_not_assignee', reason)
return { action: 'rejected', code: 'sender_not_assignee', reason }
}
// Why: `orchestration.send` can release the DB lock before waking the
// coordinator; the later coordinator read still needs to observe completion.
if (dispatch.status === 'completed' && task.status === 'completed') {
return { action: 'completed', taskId, dispatchId }
}
if (dispatch.status !== 'dispatched') {
onLog(`Warning: worker_done for inactive dispatch ${dispatchId} ignored`)
return { action: 'ignored' }
}
if (db.getDispatchContext(taskId)?.id !== dispatchId || task.status !== 'dispatched') {
onLog(`Warning: worker_done for stale dispatch ${dispatchId} ignored`)
return { action: 'ignored' }
}
const filesModified =
Array.isArray(payload.filesModified) &&
payload.filesModified.every((file) => typeof file === 'string')
@@ -228,17 +274,48 @@ function reconcileWorkerDoneMessage(
: []
const result = JSON.stringify({
provenance: 'worker_report',
outcome,
messageId: msg.id,
reportedBy: msg.from_handle,
subject: msg.subject,
body: msg.body,
completedBy: msg.from_handle,
filesModified,
reportPath: typeof payload.reportPath === 'string' ? payload.reportPath : null,
completedAt: new Date().toISOString()
})
db.updateTaskStatus(taskId, 'completed', result)
const settlement = db.settleWorkerReport({
taskId,
dispatchId,
outcome: outcome as WorkerReportOutcome,
result
})
if (settlement.action === 'rejected') {
return rejectLifecycleMessage(db, msg, settlement.code, settlement.reason, onLog)
}
suppressEarlierHeartbeats(db, msg, dispatchId)
onLog(`Task ${taskId} completed`)
if (outcome === 'failed') {
onLog(`Task ${taskId} failed by worker report`)
return { action: 'failed', taskId, dispatchId }
}
onLog(`Task ${taskId} completed by worker report`)
return { action: 'completed', taskId, dispatchId }
}
function rejectLifecycleMessage(
db: OrchestrationDb,
msg: MessageRow,
code: LifecycleRejectionCode,
reason: string,
onLog: LogFn
): LifecycleRejectionResult {
onLog(`Warning: ${msg.type} rejected: ${reason}`)
db.convertLifecycleMessageToRejection(msg.id, code, reason)
return { action: 'rejected', code, reason }
}
function buildLifecycleAuthorityRejectionReason(
dispatchId: string,
dispatch: { assignee_handle: string | null; assignee_pane_key: string | null },
@@ -0,0 +1,27 @@
import { mkdtempSync, rmSync, statSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { OrchestrationDb } from './db'
describe.skipIf(process.platform === 'win32')('orchestration database permissions', () => {
let directory: string | undefined
let db: OrchestrationDb | undefined
afterEach(() => {
db?.close()
if (directory) {
rmSync(directory, { recursive: true, force: true })
}
})
it('restricts the database and live SQLite sidecars to the current user', () => {
directory = mkdtempSync(join(tmpdir(), 'orca-orchestration-permissions-'))
const dbPath = join(directory, 'orchestration.db')
db = new OrchestrationDb(dbPath)
for (const path of [dbPath, `${dbPath}-wal`, `${dbPath}-shm`]) {
expect(statSync(path).mode & 0o777).toBe(0o600)
}
})
})
@@ -0,0 +1,11 @@
export class OrchestrationError extends Error {
readonly code: string
readonly data?: unknown
constructor(code: string, message: string, data?: unknown) {
super(message)
this.name = 'OrchestrationError'
this.code = code
this.data = data
}
}
@@ -0,0 +1,167 @@
import { afterEach, describe, expect, it } from 'vitest'
import { OrchestrationDb } from './db'
describe('OrchestrationDb mutation and question state', () => {
let db: OrchestrationDb | undefined
afterEach(() => {
db?.close()
})
function createDb(): OrchestrationDb {
db = new OrchestrationDb(':memory:')
return db
}
describe('durable mutation receipts', () => {
it('replays completed input and rejects request ID reuse with changed input', () => {
const d = createDb()
const started = d.beginMutationReceipt({
callerFingerprint: 'caller_a',
requestId: 'request_1',
method: 'orchestration.send',
payloadHash: 'hash_a'
})
expect(started.disposition).toBe('started')
d.completeMutationReceipt({
callerFingerprint: 'caller_a',
requestId: 'request_1',
method: 'orchestration.send',
payloadHash: 'hash_a',
receipt: '{"messageId":"msg_1"}'
})
expect(
d.beginMutationReceipt({
callerFingerprint: 'caller_a',
requestId: 'request_1',
method: 'orchestration.send',
payloadHash: 'hash_a'
})
).toMatchObject({
disposition: 'completed',
row: { receipt: '{"messageId":"msg_1"}' }
})
expect(() =>
d.beginMutationReceipt({
callerFingerprint: 'caller_a',
requestId: 'request_1',
method: 'orchestration.send',
payloadHash: 'hash_b'
})
).toThrow('already used with different input')
})
it('keeps caller namespaces separate and can discard only pending work', () => {
const d = createDb()
for (const callerFingerprint of ['caller_a', 'caller_b']) {
d.beginMutationReceipt({
callerFingerprint,
requestId: 'same_request',
method: 'orchestration.send',
payloadHash: 'same_hash'
})
}
expect(d.getMutationReceipt('caller_a', 'same_request')?.state).toBe('pending')
expect(d.getMutationReceipt('caller_b', 'same_request')?.state).toBe('pending')
d.discardPendingMutationReceipt('caller_a', 'same_request')
expect(d.getMutationReceipt('caller_a', 'same_request')).toBeUndefined()
expect(d.getMutationReceipt('caller_b', 'same_request')?.state).toBe('pending')
})
})
describe('question threads', () => {
it('accepts a question message in the fresh canonical schema', () => {
const d = createDb()
const message = d.insertMessage({
from: 'worker',
to: 'run:run_1',
subject: 'Need input',
type: 'question'
})
expect(message.type).toBe('question')
})
it('uses the original message ID and records one durable answer', () => {
const d = createDb()
const run = d.createRun({
objective: 'Questions',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:11111111-1111-4111-8111-111111111111'
})
const task = d.createTask({ spec: 'ask', runId: run.id })
const dispatch = d.createDispatchContext(task.id, 'term_worker')
const created = d.createQuestion({
runId: run.id,
dispatchId: dispatch.id,
askerHandle: 'term_worker',
question: 'Which format?',
options: ['old', 'new']
})
expect(created.question.message_id).toBe(created.message.id)
expect(created.message).toMatchObject({
run_id: run.id,
to_handle: `run:${run.id}`,
type: 'question',
thread_id: created.message.id
})
const answer = d.answerQuestion({
messageId: created.message.id,
runId: run.id,
consumerGeneration: run.consumer_generation,
body: 'old'
})
const replay = d.answerQuestion({
messageId: created.message.id,
runId: run.id,
consumerGeneration: run.consumer_generation,
body: 'old'
})
expect(answer.message.to_handle).toBe(`dispatch:${dispatch.id}`)
expect(answer.question.status).toBe('answered')
expect(replay.message.id).toBe(answer.message.id)
expect(replay.duplicate).toBe(true)
expect(() =>
d.answerQuestion({
messageId: created.message.id,
runId: run.id,
consumerGeneration: run.consumer_generation,
body: 'new'
})
).toThrow(/different answer/)
})
it('closes pending questions with their Dispatch', () => {
const d = createDb()
const run = d.createRun({
objective: 'Close questions',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:11111111-1111-4111-8111-111111111111'
})
const task = d.createTask({ spec: 'ask', runId: run.id })
const dispatch = d.createDispatchContext(task.id, 'term_worker')
const created = d.createQuestion({
runId: run.id,
dispatchId: dispatch.id,
askerHandle: 'term_worker',
question: 'Still active?'
})
expect(d.closeQuestionsForDispatch(dispatch.id)).toEqual([created.message.id])
expect(d.getQuestion(created.message.id)?.status).toBe('closed')
expect(() =>
d.answerQuestion({
messageId: created.message.id,
runId: run.id,
consumerGeneration: run.consumer_generation,
body: 'late'
})
).toThrow(/inactive/)
})
})
})
@@ -0,0 +1,101 @@
import { afterEach, describe, expect, it } from 'vitest'
import { LEGACY_RUN_ID, OrchestrationDb } from './db'
describe('OrchestrationDb reset scopes', () => {
let db: OrchestrationDb | undefined
afterEach(() => db?.close())
function createState() {
db = new OrchestrationDb(':memory:')
const run = db.createRun({
objective: 'Reset contract',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:11111111-1111-4111-8111-111111111111'
})
const task = db.createTask({ spec: 'work', runId: run.id })
const started = db.createStartingWorkerDispatch({
taskId: task.id,
startOptions: { worktree: 'current' },
runtimeEpoch: 'runtime_1',
federation: {
environmentId: 'environment_1',
environmentName: 'Windows',
peerFingerprint: 'peer_1',
protocolVersion: 1
},
mutationReceipt: {
callerFingerprint: 'caller_1',
requestId: 'request_1',
method: 'orchestration.workerStart',
payloadHash: 'hash_1'
}
})
const message = db.insertMessage({
runId: run.id,
from: 'worker',
to: `run:${run.id}`,
subject: 'status'
})
db.enqueueFederationRelay({
dispatchId: started.dispatch.id,
direction: 'to_home',
kind: 'question',
payload: '{}',
messageId: 'question_1',
remoteQuestion: true
})
return { run, task, started, message }
}
it('resetAll clears Runs, worker/federation state, and messages', () => {
const state = createState()
db!.resetAll()
expect(db!.listRuns()).toEqual([expect.objectContaining({ id: LEGACY_RUN_ID, legacy: 1 })])
expect(db!.getTask(state.task.id)).toBeUndefined()
expect(db!.getWorkerDispatch(state.started.dispatch.id)).toBeUndefined()
expect(db!.getFederatedDispatch(state.started.dispatch.id)).toBeUndefined()
// The ledger survives so a lost reset response cannot replay as a new mutation.
expect(db!.getMutationReceipt('caller_1', 'request_1')).toBeDefined()
expect(db!.getInbox()).toEqual([])
expect(
db!.listFederationRelay({
dispatchId: state.started.dispatch.id,
direction: 'to_home',
afterSequence: 0
})
).toEqual([])
})
it('resetTasks preserves Runs and messages while clearing every worker attachment', () => {
const state = createState()
db!.resetTasks()
expect(db!.getRun(state.run.id)).toBeDefined()
expect(db!.getMessageById(state.message.id)).toBeDefined()
expect(db!.getTask(state.task.id)).toBeUndefined()
expect(db!.getWorkerDispatch(state.started.dispatch.id)).toBeUndefined()
expect(db!.getFederatedDispatch(state.started.dispatch.id)).toBeUndefined()
expect(db!.getRemoteQuestion('question_1')).toBeUndefined()
})
it('resetMessages preserves active relay cursors while clearing the Run inbox', () => {
const state = createState()
db!.resetMessages()
expect(db!.getTask(state.task.id)).toBeDefined()
expect(db!.getInbox()).toEqual([])
expect(db!.getRemoteQuestion('question_1')).toBeDefined()
expect(
db!.listFederationRelay({
dispatchId: state.started.dispatch.id,
direction: 'to_home',
afterSequence: 0
})
).toHaveLength(1)
})
})
@@ -0,0 +1,279 @@
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import { LEGACY_RUN_ID, OrchestrationDb } from './db'
describe('OrchestrationDb Run state', () => {
let db: OrchestrationDb | undefined
afterEach(() => {
db?.close()
})
function createDb(): OrchestrationDb {
db = new OrchestrationDb(':memory:')
return db
}
function createBoundRun(d: OrchestrationDb) {
return d.createRun({
objective: 'Mailbox test',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:11111111-1111-4111-8111-111111111111'
})
}
describe('Run deliveries', () => {
it('returns one bounded FIFO batch and replays it until acknowledgment', () => {
const d = createDb()
const run = createBoundRun(d)
for (let index = 0; index < 55; index++) {
d.insertMessage({
from: 'worker',
to: `run:${run.id}`,
subject: `message ${index}`,
runId: run.id
})
}
const first = d.getOrCreateRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation
})
const replay = d.getOrCreateRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation
})
expect(first?.messages).toHaveLength(50)
expect(first?.messages[0].subject).toBe('message 0')
expect(first?.messages[49].subject).toBe('message 49')
expect(replay?.delivery.id).toBe(first?.delivery.id)
expect(replay?.replayed).toBe(true)
d.acknowledgeRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation,
deliveryId: first!.delivery.id
})
const next = d.getOrCreateRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation
})
expect(next?.messages.map((message) => message.subject)).toEqual([
'message 50',
'message 51',
'message 52',
'message 53',
'message 54'
])
})
it('acknowledges the whole batch idempotently without consuming newer mail', () => {
const d = createDb()
const run = createBoundRun(d)
d.insertMessage({ from: 'a', to: `run:${run.id}`, subject: 'first', runId: run.id })
const delivery = d.getOrCreateRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation
})!
d.insertMessage({ from: 'b', to: `run:${run.id}`, subject: 'newer', runId: run.id })
const firstAck = d.acknowledgeRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation,
deliveryId: delivery.delivery.id
})
const duplicateAck = d.acknowledgeRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation,
deliveryId: delivery.delivery.id
})
expect(firstAck.duplicate).toBe(false)
expect(duplicateAck.duplicate).toBe(true)
expect(
d
.getOrCreateRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation
})
?.messages.map((message) => message.subject)
).toEqual(['newer'])
})
it('uses type filters only as wake predicates and returns the full oldest batch', () => {
const d = createDb()
const run = createBoundRun(d)
d.insertMessage({ from: 'a', to: `run:${run.id}`, subject: 'status', runId: run.id })
expect(
d.getOrCreateRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation,
wakeTypes: ['worker_done']
})
).toBeUndefined()
d.insertMessage({
from: 'b',
to: `run:${run.id}`,
subject: 'done',
type: 'worker_done',
runId: run.id
})
const delivery = d.getOrCreateRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation,
wakeTypes: ['worker_done']
})
expect(delivery?.messages.map((message) => message.subject)).toEqual(['status', 'done'])
})
it('fences an outstanding batch when the Run consumer changes', () => {
const d = createDb()
const run = createBoundRun(d)
d.insertMessage({ from: 'a', to: `run:${run.id}`, subject: 'one', runId: run.id })
const oldDelivery = d.getOrCreateRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation
})!
const rebound = d.bindRun({
runId: run.id,
coordinatorHandle: 'term_new',
coordinatorPaneKey: 'tab_new:22222222-2222-4222-9222-222222222222'
})!
let fencedError: unknown
try {
d.acknowledgeRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation,
deliveryId: oldDelivery.delivery.id
})
} catch (error) {
fencedError = error
}
expect(fencedError).toMatchObject({ code: 'consumer_fenced' })
const replacement = d.getOrCreateRunDelivery({
runId: run.id,
consumerGeneration: rebound.consumer_generation
})
expect(replacement?.delivery.id).not.toBe(oldDelivery.delivery.id)
expect(replacement?.messages.map((message) => message.subject)).toEqual(['one'])
})
it('replays an outstanding batch after reopening the database', () => {
const dir = mkdtempSync(join(tmpdir(), 'orca-delivery-'))
const dbPath = join(dir, 'orchestration.db')
try {
const firstDb = new OrchestrationDb(dbPath)
const run = createBoundRun(firstDb)
firstDb.insertMessage({
from: 'a',
to: `run:${run.id}`,
subject: 'survives',
runId: run.id
})
const first = firstDb.getOrCreateRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation
})!
firstDb.close()
const reopened = new OrchestrationDb(dbPath)
db = reopened
const replay = reopened.getOrCreateRunDelivery({
runId: run.id,
consumerGeneration: run.consumer_generation
})
expect(replay?.delivery.id).toBe(first.delivery.id)
expect(replay?.messages[0].subject).toBe('survives')
} finally {
db?.close()
db = undefined
rmSync(dir, { recursive: true, force: true })
}
})
})
describe('lightweight Run scope', () => {
it('binds creation to one pane and fences that pane when it creates another Run', () => {
const d = createDb()
const first = d.createRun({
objective: 'First objective',
coordinatorHandle: 'term_first',
coordinatorPaneKey: 'tab_a:11111111-1111-4111-8111-111111111111'
})
expect(first).toMatchObject({ consumer_generation: 1, legacy: 0 })
expect(d.getCurrentRunForPane('tab_reminted:11111111-1111-4111-8111-111111111111')?.id).toBe(
first.id
)
const second = d.createRun({
objective: 'Second objective',
coordinatorHandle: 'term_second',
coordinatorPaneKey: 'tab_b:11111111-1111-4111-8111-111111111111'
})
expect(d.getRun(first.id)).toMatchObject({
coordinator_handle: null,
coordinator_pane_key: null,
consumer_generation: 2
})
expect(d.getCurrentRunForPane('tab_b:11111111-1111-4111-8111-111111111111')?.id).toBe(
second.id
)
})
it('rebinds a Run by incrementing its consumer generation', () => {
const d = createDb()
const run = d.createRun({
objective: 'Move coordinator',
coordinatorHandle: 'term_old',
coordinatorPaneKey: 'tab_old:11111111-1111-4111-8111-111111111111'
})
expect(
d.bindRun({
runId: run.id,
coordinatorHandle: 'term_new',
coordinatorPaneKey: 'tab_new:22222222-2222-4222-9222-222222222222'
})
).toMatchObject({
coordinator_handle: 'term_new',
consumer_generation: 2
})
expect(d.getCurrentRunForPane('tab_old:11111111-1111-4111-8111-111111111111')).toBeUndefined()
expect(
d.bindRun({
runId: LEGACY_RUN_ID,
coordinatorHandle: 'term_new',
coordinatorPaneKey: 'tab_new:22222222-2222-4222-9222-222222222222'
})
).toBeUndefined()
})
it('associates task, dispatch, message, and gate rows with the selected Run', () => {
const d = createDb()
const run = d.createRun({
objective: 'Scoped work',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:11111111-1111-4111-8111-111111111111'
})
const task = d.createTask({ spec: 'work', runId: run.id })
const dispatch = d.createDispatchContext(task.id, 'term_worker')
const message = d.insertMessage({
runId: run.id,
from: 'term_worker',
to: 'term_coord',
subject: 'status'
})
const gate = d.createGate({ taskId: task.id, question: 'Continue?' })
expect(task.run_id).toBe(run.id)
expect(dispatch.run_id).toBe(run.id)
expect(message.run_id).toBe(run.id)
expect(gate.run_id).toBe(run.id)
})
})
})
@@ -0,0 +1,274 @@
import { afterEach, describe, expect, it } from 'vitest'
import { OrchestrationDb } from './db'
describe('OrchestrationDb worker Dispatch state', () => {
let db: OrchestrationDb | undefined
afterEach(() => {
db?.close()
})
function createDb(): OrchestrationDb {
db = new OrchestrationDb(':memory:')
return db
}
it('creates and activates a composed worker Dispatch transactionally', () => {
const d = createDb()
const task = d.createTask({ spec: 'worker' })
const started = d.createStartingWorkerDispatch({
taskId: task.id,
startOptions: { topology: 'current', agent: 'codex' }
})
expect(started).toMatchObject({
dispatch: { status: 'pending' },
worker: { state: 'starting', stage: 'accepted' }
})
expect(d.getTask(task.id)?.status).toBe('dispatched')
const capability = d.prepareStartingWorkerAuthority({
dispatchId: started.dispatch.id,
handle: 'term_worker',
paneKey: 'tab_worker:leaf_worker',
processIncarnation: 'runtime:pty:1',
worktreeId: 'repo::worktree',
setupState: 'not_applicable',
effects: [{ kind: 'terminal', action: 'created', id: 'term_worker' }]
})
expect(capability).toMatch(/^dcap_/)
expect(d.markWorkerDispatchReady(started.dispatch.id)).toMatchObject({
state: 'ready',
stage: 'input_accepted'
})
expect(d.getDispatchContextById(started.dispatch.id)).toMatchObject({
status: 'dispatched',
assignee_handle: 'term_worker'
})
})
it('commits worker-start mutation acceptance with the starting Dispatch', () => {
const d = createDb()
const task = d.createTask({ spec: 'atomic acceptance' })
const mutationReceipt = {
callerFingerprint: 'caller_fingerprint',
requestId: 'worker_start_request',
method: 'orchestration.workerStart',
payloadHash: 'payload_hash'
}
const started = d.createStartingWorkerDispatch({
taskId: task.id,
startOptions: { topology: 'current' },
mutationReceipt
})
expect(d.getMutationReceipt('caller_fingerprint', 'worker_start_request')).toMatchObject({
state: 'pending',
method: 'orchestration.workerStart'
})
expect(d.getWorkerDispatch(started.dispatch.id)).toMatchObject({
state: 'starting',
stage: 'accepted'
})
expect(d.getTask(task.id)?.status).toBe('dispatched')
})
it('rolls back worker-start mutation acceptance when the Task cannot start', () => {
const d = createDb()
expect(() =>
d.createStartingWorkerDispatch({
taskId: 'task_missing',
startOptions: {},
mutationReceipt: {
callerFingerprint: 'caller_fingerprint',
requestId: 'invalid_worker_start',
method: 'orchestration.workerStart',
payloadHash: 'payload_hash'
}
})
).toThrow('was not found')
expect(d.getMutationReceipt('caller_fingerprint', 'invalid_worker_start')).toBeUndefined()
})
it('fails a composed start without losing residual resource receipts', () => {
const d = createDb()
const task = d.createTask({ spec: 'worker' })
const started = d.createStartingWorkerDispatch({ taskId: task.id, startOptions: {} })
d.recordWorkerStage({
dispatchId: started.dispatch.id,
stage: 'terminal_created',
effects: [{ kind: 'terminal', action: 'created', id: 'term_worker' }],
residualResources: [{ kind: 'terminal', id: 'term_worker' }]
})
expect(d.failWorkerStart(started.dispatch.id, 'agent_readiness', 'timed out')).toMatchObject({
state: 'failed',
stage: 'agent_readiness',
last_error: 'timed out',
residual_resources: expect.stringContaining('term_worker')
})
expect(d.getTask(task.id)?.status).toBe('failed')
})
it('allows retry only from the Task current terminal Dispatch', () => {
const d = createDb()
const task = d.createTask({ spec: 'retry current' })
const first = d.createStartingWorkerDispatch({ taskId: task.id, startOptions: {} })
d.failWorkerStart(first.dispatch.id, 'agent_readiness', 'first failed')
const second = d.createStartingWorkerDispatch({
taskId: task.id,
retryOf: first.dispatch.id,
startOptions: {}
})
d.failWorkerStart(second.dispatch.id, 'agent_readiness', 'second failed')
expect(() =>
d.createStartingWorkerDispatch({
taskId: task.id,
retryOf: first.dispatch.id,
startOptions: {}
})
).toThrow('cannot retry')
expect(
d.createStartingWorkerDispatch({
taskId: task.id,
retryOf: second.dispatch.id,
startOptions: {}
}).worker.state
).toBe('starting')
})
it('treats abandon of a superseded Dispatch as a no-op', () => {
const d = createDb()
const task = d.createTask({ spec: 'stale abandon' })
const first = d.createStartingWorkerDispatch({ taskId: task.id, startOptions: {} })
d.failWorkerStart(first.dispatch.id, 'agent_readiness', 'first failed')
const second = d.createStartingWorkerDispatch({
taskId: task.id,
retryOf: first.dispatch.id,
startOptions: {}
})
d.prepareStartingWorkerAuthority({
dispatchId: second.dispatch.id,
handle: 'term_replacement',
paneKey: 'tab_replacement:leaf_replacement',
processIncarnation: 'runtime:pty:2',
worktreeId: 'repo::worktree',
setupState: 'not_applicable',
effects: []
})
d.markWorkerDispatchReady(second.dispatch.id)
expect(d.abandonWorkerDispatch(first.dispatch.id)).toMatchObject({
disposition: 'stale',
worker: { state: 'failed' }
})
expect(d.getTask(task.id)?.status).toBe('dispatched')
expect(d.getWorkerDispatch(second.dispatch.id)?.state).toBe('ready')
expect(
d.settleWorkerReport({
taskId: task.id,
dispatchId: second.dispatch.id,
outcome: 'succeeded',
result: '{}'
})
).toMatchObject({ action: 'settled' })
expect(d.getTask(task.id)?.status).toBe('completed')
})
it('lets the stop fence win before a late worker completion', () => {
const d = createDb()
const task = d.createTask({ spec: 'race' })
const started = d.createStartingWorkerDispatch({ taskId: task.id, startOptions: {} })
d.prepareStartingWorkerAuthority({
dispatchId: started.dispatch.id,
handle: 'term_worker',
paneKey: 'tab_worker:leaf_worker',
processIncarnation: 'runtime:pty:1',
worktreeId: 'repo::worktree',
setupState: 'not_applicable',
effects: []
})
d.markWorkerDispatchReady(started.dispatch.id)
expect(d.beginWorkerStop(started.dispatch.id).disposition).toBe('stopping')
expect(
d.settleWorkerReport({
taskId: task.id,
dispatchId: started.dispatch.id,
outcome: 'succeeded',
result: '{}'
})
).toMatchObject({ action: 'rejected', code: 'inactive_dispatch' })
expect(d.settleWorkerStop(started.dispatch.id).state).toBe('stopped')
expect(d.getTask(task.id)?.status).toBe('blocked')
})
it('allows explicit stop recovery from uncertain local and remote starts', () => {
const d = createDb()
const task = d.createTask({ spec: 'uncertain local start' })
const started = d.createStartingWorkerDispatch({ taskId: task.id, startOptions: {} })
d.markWorkerStartUnknown(started.dispatch.id, 'agent_readiness', 'connection lost')
expect(d.beginWorkerStop(started.dispatch.id)).toMatchObject({
disposition: 'stopping',
worker: { state: 'stopping' }
})
d.createRemoteDispatchAttachment({
dispatchId: 'ctx_remote_unknown',
taskId: 'task_remote_unknown',
homePeerFingerprint: 'home_peer',
protocolVersion: 1,
runtimeEpoch: 'worker_epoch',
mutationReceipt: {
callerFingerprint: 'home_peer',
requestId: 'remote_unknown_start',
method: 'orchestration.federationAttachStart',
payloadHash: 'remote_unknown_payload'
}
})
d.recordRemoteAttachmentStage({
dispatchId: 'ctx_remote_unknown',
stage: 'agent_readiness',
state: 'start_unknown',
terminalHandle: 'term_remote_worker'
})
expect(d.beginRemoteAttachmentStop('ctx_remote_unknown')).toMatchObject({
state: 'stopping',
stage: 'stop_requested',
capability_hash: null
})
})
it('returns already-settled when completion wins before stop', () => {
const d = createDb()
const task = d.createTask({ spec: 'race' })
const started = d.createStartingWorkerDispatch({ taskId: task.id, startOptions: {} })
d.prepareStartingWorkerAuthority({
dispatchId: started.dispatch.id,
handle: 'term_worker',
paneKey: 'tab_worker:leaf_worker',
processIncarnation: 'runtime:pty:1',
worktreeId: 'repo::worktree',
setupState: 'not_applicable',
effects: []
})
d.markWorkerDispatchReady(started.dispatch.id)
expect(
d.settleWorkerReport({
taskId: task.id,
dispatchId: started.dispatch.id,
outcome: 'succeeded',
result: '{}'
})
).toMatchObject({ action: 'settled' })
expect(d.beginWorkerStop(started.dispatch.id)).toMatchObject({
disposition: 'already_settled',
worker: { state: 'succeeded' }
})
})
})
+19 -10
View File
@@ -45,9 +45,12 @@ describe('buildDispatchPreamble', () => {
expect(result).toContain('reportPath')
expect(result).toContain('--task-id task_abc123')
expect(result).toContain('--dispatch-id ctx_def456')
expect(result).toContain('--outcome succeeded')
expect(result).toContain('replace it with --outcome failed')
expect(result).toContain('--files-modified "path/a,path/b"')
expect(result).toContain('--report-path "<optional: path to the full artifact>"')
expect(result).toMatch(/orchestration send --to term_coord --from term_worker/)
expect(result).toMatch(/orchestration send --from term_worker/)
expect(result).not.toContain('orchestration send --to term_coord')
})
it(
@@ -85,12 +88,12 @@ describe('buildDispatchPreamble', () => {
expect(result).toContain('--task-id task_abc123')
expect(result).toContain('--dispatch-id ctx_def456')
expect(result).toContain('--phase "<short: investigating|implementing|reviewing|waiting>"')
expect(result).toMatch(/orchestration send --to term_coord --from term_worker/)
expect(result).toMatch(/orchestration send --from term_worker/)
})
it('includes ask block with BEHAVIOR RULE #1 forbidding AskUserQuestion', () => {
const result = buildDispatchPreamble(baseParams())
expect(result).toMatch(/orchestration ask --to term_coord --from term_worker/)
expect(result).toMatch(/orchestration ask --from term_worker/)
expect(result).toContain('--question')
expect(result).toContain('--timeout-ms 600000')
// Why: the exact phrase is asserted so the rule can't be trimmed away by
@@ -101,21 +104,27 @@ describe('buildDispatchPreamble', () => {
// else (e.g., not in an example payload or header). Count occurrences
// of the exact token as a sanity check.
const occurrences = (result.match(/AskUserQuestion/g) ?? []).length
// Three mentions: the one-liner ban, the TUI-prompt rationale, and the
// "when tempted to reach for AskUserQuestion" closing line.
expect(occurrences).toBe(3)
expect(occurrences).toBe(2)
})
it('binds every injected worker command to the dispatched terminal', () => {
const result = buildDispatchPreamble(baseParams())
expect(result).toMatch(/orchestration ask --to term_coord --from term_worker/)
expect(result).toMatch(
/orchestration send --to term_coord --from term_worker \\\n --type escalation/
)
expect(result).toMatch(/orchestration ask --from term_worker/)
expect(result).toMatch(/orchestration send --from term_worker \\\n --type escalation/)
expect(result).toContain('orchestration check --terminal term_worker')
})
it('carries the minted Dispatch capability on lifecycle and question commands', () => {
const result = buildDispatchPreamble({
...baseParams(),
dispatchCapability: 'dcap_test_secret'
})
expect(result.match(/--dispatch-capability dcap_test_secret/g)).toHaveLength(4)
expect(result).not.toContain('"dispatchCapability"')
})
it('tells prompt-returning workers to idle without post-done polling', () => {
const result = buildDispatchPreamble(baseParams())
const section = afterWorkerDoneSection(result)
+17 -12
View File
@@ -8,6 +8,7 @@ export type PreambleParams = {
// prevents stale messages from a previously-failed dispatch from completing
// or refreshing the retry.
dispatchId: string
dispatchCapability?: string
taskSpec: string
coordinatorHandle: string
workerHandle: string
@@ -52,6 +53,9 @@ export function buildDispatchPreamble(params: PreambleParams): string {
cli,
workerKind: params.workerKind ?? 'prompt-returning-agent'
})
const capabilityFlag = params.dispatchCapability
? ` --dispatch-capability ${params.dispatchCapability}`
: ''
const header = `You are working inside Orca, a multi-agent IDE. You are a dispatched worker.
Your coordinator's terminal handle is: ${params.coordinatorHandle}
@@ -62,7 +66,7 @@ Slack, GitHub comments, or any other channel to reach a human during the run.
=== CLI COMMANDS ===
# Report task completion (REQUIRED when done — even on failure).
# Report the terminal task outcome (REQUIRED exactly once).
#
# RULE: --body must be a 3-sentence executive summary (what you did,
# what you found, what's left). Never send an empty body; the coordinator
@@ -70,14 +74,15 @@ Slack, GitHub comments, or any other channel to reach a human during the run.
# If you produced a long-form artifact, include its path as
# payload.reportPath so the coordinator can find it without a file search.
#
# RULE: send worker_done exactly once. Failure is still a worker_done
# with subject like "Failed: <reason>" — never silently exit.
# RULE: send worker_done exactly once. Use --outcome succeeded when the
# requested work is done, or replace it with --outcome failed when it is not.
# Never encode failure only in prose and never silently exit.
# Include BOTH taskId and dispatchId in the payload so a late completion
# from a failed retry cannot complete the current dispatch.
${cli} orchestration send --to ${params.coordinatorHandle} --from ${params.workerHandle} \\
${cli} orchestration send --from ${params.workerHandle}${capabilityFlag} \\
--type worker_done --subject "<short status>" \\
--body "<3-sentence summary: what you did, what you found, what's left>" \\
--task-id ${params.taskId} --dispatch-id ${params.dispatchId} \\
--task-id ${params.taskId} --dispatch-id ${params.dispatchId} --outcome succeeded \\
--files-modified "path/a,path/b" \\
--report-path "<optional: path to the full artifact>"
@@ -91,7 +96,7 @@ Slack, GitHub comments, or any other channel to reach a human during the run.
# attributes the heartbeat to the specific dispatch context, not just
# the task, so a straggler heartbeat from a previously-failed dispatch
# cannot mask a hung retry.
${cli} orchestration send --to ${params.coordinatorHandle} --from ${params.workerHandle} \\
${cli} orchestration send --from ${params.workerHandle}${capabilityFlag} \\
--type heartbeat --subject "alive" \\
--task-id ${params.taskId} --dispatch-id ${params.dispatchId} \\
--phase "<short: investigating|implementing|reviewing|waiting>"
@@ -104,18 +109,18 @@ Slack, GitHub comments, or any other channel to reach a human during the run.
# coordinator cannot see and cannot answer — your session will hang forever
# waiting on a human. Every interactive question goes through \`ask\` below.
#
# The \`ask\` verb is a thin wrapper: it sends a decision_gate message and
# blocks on \`check --wait\` until the coordinator replies, then prints the
# reply body. Use it anywhere you would otherwise have reached for
# AskUserQuestion.
${cli} orchestration ask --to ${params.coordinatorHandle} --from ${params.workerHandle} \\
# The \`ask\` verb durably records a question in this Dispatch's Run and
# blocks until the coordinator replies, then prints the reply body. If the
# call times out or disconnects, resume with the returned message ID instead
# of creating a duplicate question.
${cli} orchestration ask --from ${params.workerHandle}${capabilityFlag} \\
--question "<your question>" \\
--options "<optional,comma,separated>" \\
--timeout-ms 600000
# Escalate a blocker or failure (pre-completion, when you need the
# coordinator to do something before you can continue):
${cli} orchestration send --to ${params.coordinatorHandle} --from ${params.workerHandle} \\
${cli} orchestration send --from ${params.workerHandle}${capabilityFlag} \\
--type escalation --subject "Blocked: <reason>" \\
--body "<details>" \\
--task-id ${params.taskId}
@@ -0,0 +1,59 @@
import { describe, expect, it, vi } from 'vitest'
import { buildObservedSetupCommand, createSetupCompletionScanner } from './setup-completion-signal'
describe('orchestration setup completion signal', () => {
it('preserves a POSIX setup exit code in a visible completion signal', () => {
const { command } = buildObservedSetupCommand(
'/repo/.git/orca/setup-runner.sh',
'posix',
'token-posix'
)
expect(command).toContain('bash /repo/.git/orca/setup-runner.sh')
expect(command).toContain('__ORCA_SETUP_COMPLETE__:token-posix:%s\\n')
expect(command).toContain('"$status"')
expect(command).toContain('exit "$status"')
})
it('preserves a native Windows setup path and exit code without shell interpolation', () => {
const runnerPath = 'C:\\repo %name%!^&\\.git\\orca\\setup-runner.cmd'
const observed = buildObservedSetupCommand(runnerPath, 'windows', 'token-windows')
const encodedCommand = observed.command.split(' ').at(-1)
const script = Buffer.from(encodedCommand ?? '', 'base64').toString('utf16le')
expect(observed.command).toContain('powershell.exe -NoLogo -NoProfile -NonInteractive')
expect(observed.env).toEqual({ ORCA_SETUP_RUNNER_PATH: runnerPath })
expect(script).toContain('& $runner')
expect(script).toContain('__ORCA_SETUP_COMPLETE__:token-windows:')
expect(script).toContain('exit $status')
expect(script).not.toContain(runnerPath)
})
it('keeps a WSL runner on the POSIX completion path', () => {
const { command } = buildObservedSetupCommand(
'\\\\wsl.localhost\\Ubuntu\\repo\\.git\\orca\\setup-runner.sh',
'windows',
'token-wsl'
)
expect(command).toContain('bash /repo/.git/orca/setup-runner.sh')
expect(command).toContain('__ORCA_SETUP_COMPLETE__:token-wsl:%s\\n')
expect(command).toContain('exit "$status"')
})
it('recognizes one completion signal across output chunk boundaries', () => {
const onComplete = vi.fn()
const scanner = createSetupCompletionScanner('token-chunks', onComplete)
scanner.scan('installing...\r\n__ORCA_SETUP_COMPLETE__:wrong:0\r\n__ORCA_SETUP_COMP')
scanner.scan('LETE__:token-chunks:1')
expect(onComplete).not.toHaveBeenCalled()
scanner.scan('7\r')
expect(onComplete).not.toHaveBeenCalled()
scanner.scan('\nPS C:\\repo>')
scanner.scan('__ORCA_SETUP_COMPLETE__:token-chunks:0\r\n')
expect(onComplete).toHaveBeenCalledOnce()
expect(onComplete).toHaveBeenCalledWith(17)
})
})
@@ -0,0 +1,80 @@
import {
resolveSetupRunnerCommand,
type SetupRunnerCommandPlatform
} from '../../../shared/setup-runner-command'
const SETUP_COMPLETION_PREFIX = '__ORCA_SETUP_COMPLETE__:'
const SETUP_COMPLETION_CARRY_LENGTH = SETUP_COMPLETION_PREFIX.length + 96
const WINDOWS_SETUP_RUNNER_ENV = 'ORCA_SETUP_RUNNER_PATH'
export function buildObservedSetupCommand(
runnerScriptPath: string,
platform: SetupRunnerCommandPlatform,
completionToken: string
): { command: string; env?: Record<string, string> } {
const resolution = resolveSetupRunnerCommand(runnerScriptPath, platform)
if (resolution.shell === 'windows') {
const script = [
`$runner = $env:${WINDOWS_SETUP_RUNNER_ENV}`,
'& $runner',
'$succeeded = $?',
'$status = $LASTEXITCODE',
'if ($null -eq $status) { $status = if ($succeeded) { 0 } else { 1 } }',
`Write-Output ('${completionPrefix(completionToken)}' + $status)`,
'exit $status'
].join('; ')
return {
command: `powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand ${Buffer.from(
script,
'utf16le'
).toString('base64')}`,
env: { [WINDOWS_SETUP_RUNNER_ENV]: resolution.runnerScriptPathForShell }
}
}
const script = [
`( ${resolution.command} )`,
'status=$?',
`printf '\\n${completionPrefix(completionToken)}%s\\n' "$status"`,
'exit "$status"'
].join('; ')
return { command: `bash -lc ${quotePosixArg(script)}` }
}
export function createSetupCompletionScanner(
completionToken: string,
onComplete: (exitCode: number) => void
): {
scan: (data: string) => void
} {
const expectedPrefix = completionPrefix(completionToken)
let carry = ''
let completed = false
return {
scan(data: string): void {
if (completed || data.length === 0) {
return
}
const combined = `${carry}${data}`
const markerIndex = combined.lastIndexOf(expectedPrefix)
if (markerIndex >= 0) {
const suffix = combined.slice(markerIndex + expectedPrefix.length)
const match = suffix.match(/^(-?\d+)\r?\n/)
if (match) {
completed = true
onComplete(Number.parseInt(match[1], 10))
return
}
}
carry = combined.slice(-SETUP_COMPLETION_CARRY_LENGTH)
}
}
}
function completionPrefix(completionToken: string): string {
return `${SETUP_COMPLETION_PREFIX}${completionToken}:`
}
function quotePosixArg(value: string): string {
return `'${value.replace(/'/g, `'\\''`)}'`
}
+165 -9
View File
@@ -1,12 +1,16 @@
export type MessageType =
| 'status'
| 'dispatch'
| 'worker_done'
| 'merge_ready'
| 'escalation'
| 'handoff'
| 'decision_gate'
| 'heartbeat'
export const MESSAGE_TYPES = [
'status',
'dispatch',
'worker_done',
'merge_ready',
'escalation',
'handoff',
'decision_gate',
'question',
'heartbeat'
] as const
export type MessageType = (typeof MESSAGE_TYPES)[number]
export type MessagePriority = 'normal' | 'high' | 'urgent'
@@ -14,12 +18,158 @@ export type TaskStatus = 'pending' | 'ready' | 'dispatched' | 'completed' | 'fai
export type DispatchStatus = 'pending' | 'dispatched' | 'completed' | 'failed' | 'circuit_broken'
export type WorkerReportOutcome = 'succeeded' | 'failed'
export type WorkerReportSettlement =
| { action: 'settled'; outcome: WorkerReportOutcome; duplicate: boolean }
| {
action: 'rejected'
code:
| 'unknown_task'
| 'unknown_dispatch'
| 'task_dispatch_mismatch'
| 'inactive_dispatch'
| 'stale_dispatch'
reason: string
}
export type GateStatus = 'pending' | 'resolved' | 'timeout'
export type CoordinatorStatus = 'idle' | 'running' | 'completed' | 'failed'
export type RunRow = {
id: string
objective: string
home_database: string
coordinator_handle: string | null
coordinator_pane_key: string | null
consumer_generation: number
legacy: number
created_at: string
updated_at: string
}
export type DeliveryStatus = 'outstanding' | 'acknowledged' | 'fenced'
export type DeliveryRow = {
id: string
run_id: string
consumer_generation: number
message_ids: string
status: DeliveryStatus
created_at: string
acknowledged_at: string | null
}
export type QuestionStatus = 'pending' | 'answered' | 'closed'
export type QuestionRow = {
message_id: string
run_id: string
dispatch_id: string
asker_handle: string
status: QuestionStatus
answer_message_id: string | null
answer_body: string | null
answered_by_generation: number | null
created_at: string
answered_at: string | null
closed_at: string | null
}
export type MutationState = 'pending' | 'completed'
export type MutationReceiptRow = {
caller_fingerprint: string
request_id: string
method: string
payload_hash: string
state: MutationState
receipt: string | null
created_at: string
updated_at: string
}
export type WorkerDispatchState =
| 'starting'
| 'ready'
| 'start_unknown'
| 'failed'
| 'succeeded'
| 'stopping'
| 'stop_unknown'
| 'stopped'
| 'abandoned'
export type WorkerDispatchRow = {
dispatch_id: string
runtime_epoch: string | null
state: WorkerDispatchState
stage: string
worktree_id: string | null
agent_terminal_handle: string | null
setup_state: string
effects: string
residual_resources: string
start_options: string
last_error: string | null
created_at: string
updated_at: string
}
export type FederatedDispatchRow = {
dispatch_id: string
environment_id: string
environment_name: string
peer_fingerprint: string
remote_runtime_epoch: string | null
protocol_version: number
remote_worktree_id: string | null
remote_terminal_handle: string | null
to_home_imported_sequence: number
created_at: string
updated_at: string
}
export type RemoteDispatchAttachmentRow = {
dispatch_id: string
task_id: string
home_peer_fingerprint: string
protocol_version: number
runtime_epoch: string
capability_hash: string | null
pane_key: string | null
process_incarnation: string | null
state: WorkerDispatchState
stage: string
worktree_id: string | null
terminal_handle: string | null
setup_state: string
effects: string
residual_resources: string
to_worker_imported_sequence: number
last_error: string | null
created_at: string
updated_at: string
}
export type FederationRelayDirection = 'to_home' | 'to_worker'
export type FederationRelayItemRow = {
dispatch_id: string
direction: FederationRelayDirection
sequence: number
message_id: string
kind: string
payload: string
byte_count: number
acked_at: string | null
created_at: string
}
export type MessageRow = {
id: string
run_id: string
from_handle: string
to_handle: string
subject: string
@@ -37,6 +187,7 @@ export type MessageRow = {
export type TaskRow = {
id: string
run_id: string
parent_id: string | null
created_by_terminal_handle: string | null
task_title: string | null
@@ -51,9 +202,13 @@ export type TaskRow = {
export type DispatchContextRow = {
id: string
run_id: string
task_id: string
assignee_handle: string | null
assignee_pane_key: string | null
capability_hash: string | null
process_incarnation: string | null
capability_revoked_at: string | null
status: DispatchStatus
failure_count: number
last_failure: string | null
@@ -65,6 +220,7 @@ export type DispatchContextRow = {
export type DecisionGateRow = {
id: string
run_id: string
task_id: string
question: string
options: string
@@ -0,0 +1,42 @@
import { describe, expect, it } from 'vitest'
import { decodeWorkerOutputCursor, encodeWorkerOutputCursor } from './worker-output-cursor'
describe('worker output cursors', () => {
it('round-trips a source-pinned cursor without exposing source details', () => {
const cursor = encodeWorkerOutputCursor('dispatch_1', 'transcript', 'source_digest', 42)
expect(cursor).toMatch(/^owr1_/)
expect(cursor).not.toContain('source_digest')
expect(decodeWorkerOutputCursor(cursor, 'dispatch_1')).toEqual({
source: 'transcript',
sourceIdentity: 'source_digest',
position: 42,
legacy: false
})
})
it('accepts legacy numeric terminal cursors', () => {
expect(decodeWorkerOutputCursor(0, 'dispatch_1')).toEqual({
source: 'terminal',
sourceIdentity: null,
position: 0,
legacy: true
})
expect(decodeWorkerOutputCursor('17', 'dispatch_1')).toMatchObject({
source: 'terminal',
position: 17,
legacy: true
})
})
it('rejects another Dispatch and malformed cursor data', () => {
const cursor = encodeWorkerOutputCursor('dispatch_1', 'terminal', 'terminal_digest', 1)
expect(() => decodeWorkerOutputCursor(cursor, 'dispatch_2')).toThrow(
expect.objectContaining({ code: 'cursor_dispatch_mismatch' })
)
expect(() => decodeWorkerOutputCursor('owr1_not-json', 'dispatch_1')).toThrow(
expect.objectContaining({ code: 'cursor_invalid' })
)
})
})
@@ -0,0 +1,122 @@
import { createHash } from 'node:crypto'
import { OrchestrationError } from './orchestration-error'
const WORKER_OUTPUT_CURSOR_PREFIX = 'owr1_'
const WORKER_OUTPUT_CURSOR_MAX_LENGTH = 2_048
type WorkerOutputCursorPayload = {
v: 1
d: string
s: 'terminal' | 'transcript'
i: string
p: number
}
export type DecodedWorkerOutputCursor =
| {
source: 'terminal'
sourceIdentity: string | null
position: number
legacy: boolean
}
| {
source: 'transcript'
sourceIdentity: string
position: number
legacy: false
}
export function createWorkerOutputSourceIdentity(fields: readonly string[]): string {
return createHash('sha256').update(JSON.stringify(fields)).digest('base64url').slice(0, 32)
}
export function encodeWorkerOutputCursor(
dispatchId: string,
source: WorkerOutputCursorPayload['s'],
sourceIdentity: string,
position: number
): string {
const payload: WorkerOutputCursorPayload = {
v: 1,
d: dispatchId,
s: source,
i: sourceIdentity,
p: position
}
return `${WORKER_OUTPUT_CURSOR_PREFIX}${Buffer.from(JSON.stringify(payload)).toString('base64url')}`
}
export function decodeWorkerOutputCursor(
cursor: string | number | undefined,
dispatchId: string
): DecodedWorkerOutputCursor | null {
if (cursor === undefined) {
return null
}
if (typeof cursor === 'number') {
return decodeLegacyTerminalCursor(cursor)
}
if (/^\d+$/.test(cursor)) {
return decodeLegacyTerminalCursor(Number.parseInt(cursor, 10))
}
if (
cursor.length > WORKER_OUTPUT_CURSOR_MAX_LENGTH ||
!cursor.startsWith(WORKER_OUTPUT_CURSOR_PREFIX)
) {
throw invalidCursor()
}
let parsed: unknown
try {
parsed = JSON.parse(
Buffer.from(cursor.slice(WORKER_OUTPUT_CURSOR_PREFIX.length), 'base64url').toString('utf8')
)
} catch {
throw invalidCursor()
}
if (!isWorkerOutputCursorPayload(parsed)) {
throw invalidCursor()
}
if (parsed.d !== dispatchId) {
throw new OrchestrationError(
'cursor_dispatch_mismatch',
'The worker-read cursor belongs to a different Dispatch.'
)
}
return {
source: parsed.s,
sourceIdentity: parsed.i,
position: parsed.p,
legacy: false
}
}
function decodeLegacyTerminalCursor(position: number): DecodedWorkerOutputCursor {
if (!Number.isSafeInteger(position) || position < 0) {
throw invalidCursor()
}
return { source: 'terminal', sourceIdentity: null, position, legacy: true }
}
function isWorkerOutputCursorPayload(value: unknown): value is WorkerOutputCursorPayload {
if (!value || typeof value !== 'object' || Array.isArray(value)) {
return false
}
const payload = value as Record<string, unknown>
return (
payload.v === 1 &&
typeof payload.d === 'string' &&
payload.d.length > 0 &&
payload.d.length <= 512 &&
(payload.s === 'terminal' || payload.s === 'transcript') &&
typeof payload.i === 'string' &&
payload.i.length > 0 &&
payload.i.length <= 128 &&
typeof payload.p === 'number' &&
Number.isSafeInteger(payload.p) &&
payload.p >= 0
)
}
function invalidCursor(): OrchestrationError {
return new OrchestrationError('cursor_invalid', 'The worker-read cursor is invalid.')
}
@@ -0,0 +1,79 @@
import { describe, expect, it } from 'vitest'
import type { AgentStatusIpcPayload } from '../../../shared/agent-status-types'
import { selectExactWorkerProviderSession } from './worker-provider-session'
function status(
paneKey: string,
sessionId: string,
overrides: Partial<AgentStatusIpcPayload> = {}
): AgentStatusIpcPayload {
return {
paneKey,
connectionId: null,
receivedAt: 200,
stateStartedAt: 190,
state: 'working',
prompt: '',
agentType: 'codex',
providerSession: { key: 'session_id', id: sessionId },
...overrides
}
}
describe('exact worker provider session selection', () => {
it('selects only the current pane, connection, and observation window', () => {
const selected = selectExactWorkerProviderSession({
paneKey: 'tab:worker',
processIncarnation: 'pty:incarnation',
connectionId: 'ssh-windows',
launchToken: undefined,
observedAfter: 150,
statuses: [
status('tab:sibling', 'sibling', { connectionId: 'ssh-windows', receivedAt: 300 }),
status('tab:worker', 'old', { connectionId: 'ssh-windows', receivedAt: 100 }),
status('tab:worker', 'wrong-host', { connectionId: 'ssh-mac', receivedAt: 400 }),
status('tab:worker', 'exact', { connectionId: 'ssh-windows', receivedAt: 250 })
]
})
expect(selected).toEqual({
paneKey: 'tab:worker',
processIncarnation: 'pty:incarnation',
agent: 'codex',
providerSession: { key: 'session_id', id: 'exact' },
observedAt: 250
})
})
it('rejects stale and provider-session-only rows', () => {
expect(
selectExactWorkerProviderSession({
paneKey: 'tab:worker',
processIncarnation: 'pty:incarnation',
connectionId: null,
launchToken: undefined,
observedAfter: 300,
statuses: [
status('tab:worker', 'stale', { receivedAt: 200 }),
status('tab:worker', 'identity-only', {
receivedAt: 400,
providerSessionOnly: true
})
]
})
).toBeNull()
})
it('rejects a prior process snapshot when the launch token changed', () => {
expect(
selectExactWorkerProviderSession({
paneKey: 'tab:worker',
processIncarnation: 'pty:new-incarnation',
connectionId: null,
launchToken: 'launch-new',
observedAfter: 0,
statuses: [status('tab:worker', 'prior', { launchToken: 'launch-old' })]
})
).toBeNull()
})
})
@@ -0,0 +1,34 @@
import type { AgentStatusIpcPayload } from '../../../shared/agent-status-types'
import type { ExactWorkerProviderSession } from '../../../shared/orchestration-worker-output'
export function selectExactWorkerProviderSession(args: {
paneKey: string
processIncarnation: string
connectionId: string | null | undefined
launchToken: string | null | undefined
observedAfter: number
statuses: readonly AgentStatusIpcPayload[]
}): ExactWorkerProviderSession | null {
const status = args.statuses
.filter(
(entry) =>
entry.paneKey === args.paneKey &&
(args.connectionId === undefined || entry.connectionId === args.connectionId) &&
(!args.launchToken || entry.launchToken === args.launchToken) &&
entry.providerSessionOnly !== true &&
entry.providerSession !== undefined &&
entry.agentType !== undefined &&
entry.receivedAt >= args.observedAfter
)
.sort((left, right) => right.receivedAt - left.receivedAt)[0]
if (!status?.providerSession || !status.agentType) {
return null
}
return {
paneKey: args.paneKey,
processIncarnation: args.processIncarnation,
agent: status.agentType,
providerSession: { ...status.providerSession },
observedAt: status.receivedAt
}
}
@@ -0,0 +1,101 @@
import { describe, expect, it } from 'vitest'
import {
boundWorkerTranscriptMessages,
redactWorkerTerminalLines
} from './worker-transcript-payload'
describe('worker transcript wire bounds', () => {
it('clips oversized blocks and omits local image paths', () => {
const result = boundWorkerTranscriptMessages([
{
id: 'message-1',
role: 'assistant',
timestamp: null,
source: 'transcript',
blocks: [
{ type: 'text', text: 'x'.repeat(5_000) },
{ type: 'image-ref', path: 'C:\\Users\\worker\\secret.png', alt: 'screenshot' }
]
}
])
expect(result.messages[0]?.blocks[0]).toMatchObject({
type: 'text',
text: expect.stringContaining('… (truncated)')
})
expect(result.messages[0]?.blocks[1]).toEqual({
type: 'image-ref',
alt: 'screenshot'
})
expect(JSON.stringify(result)).not.toContain('C:\\\\Users')
expect(result.warnings).toContain('Local image paths were omitted from transcript output.')
})
it('keeps fallback identifiers stable without exposing the transcript path', () => {
const transcriptPath = 'C:\\Users\\worker\\.codex\\session.jsonl'
const message = {
id: `${transcriptPath}:0000000000000042`,
turnId: `${transcriptPath}:0000000000000001`,
role: 'assistant' as const,
timestamp: null,
source: 'transcript' as const,
blocks: [{ type: 'image-ref' as const, url: `file:///${transcriptPath}` }]
}
const first = boundWorkerTranscriptMessages([message], transcriptPath)
const second = boundWorkerTranscriptMessages([message], transcriptPath)
expect(first.messages).toEqual(second.messages)
expect(first.messages[0]?.id).toMatch(/^worker-message-/)
expect(first.messages[0]?.turnId).toMatch(/^worker-message-/)
expect(first.messages[0]?.blocks[0]).toEqual({ type: 'image-ref' })
expect(JSON.stringify(first)).not.toContain('Users')
expect(first.warnings).toEqual(
expect.arrayContaining([
'Transcript-backed message identifiers were made opaque.',
'Local image paths were omitted from transcript output.'
])
)
})
it('redacts dispatch capabilities from prose and tool payloads', () => {
const capability = `dcap_${'A'.repeat(43)}`
const result = boundWorkerTranscriptMessages([
{
id: 'message-secret',
role: 'assistant',
timestamp: null,
source: 'transcript',
blocks: [
{ type: 'text', text: `Use --dispatch-capability ${capability}` },
{
type: 'tool-call',
name: 'exec_command',
input: {
cmd: `orca orchestration send --dispatch-capability ${capability}`,
[capability]: 'secret key'
}
},
{ type: 'tool-result', output: `echoed ${capability}` }
]
}
])
expect(JSON.stringify(result)).not.toContain(capability)
expect(JSON.stringify(result.messages)).toContain('[dispatch capability redacted]')
expect(result.warnings).toContain(
'Dispatch capability tokens were redacted from transcript output.'
)
})
it('redacts dispatch capabilities from terminal fallback lines', () => {
const capability = `dcap_${'A'.repeat(43)}`
expect(redactWorkerTerminalLines([`send --dispatch-capability ${capability}`, 'safe'])).toEqual(
{
lines: ['send --dispatch-capability [dispatch capability redacted]', 'safe'],
warnings: ['Dispatch capability tokens were redacted from terminal output.']
}
)
})
})
@@ -0,0 +1,226 @@
import { createHash } from 'node:crypto'
import type { NativeChatBlock, NativeChatMessage } from '../../../shared/native-chat-types'
export const DEFAULT_WORKER_TRANSCRIPT_MESSAGE_LIMIT = 40
export const MAX_WORKER_TRANSCRIPT_MESSAGE_LIMIT = 50
const MAX_WORKER_TRANSCRIPT_BLOCKS = 6
const MAX_WORKER_TRANSCRIPT_BLOCK_CHARS = 1_200
const MAX_WORKER_TRANSCRIPT_INPUT_ITEMS = 20
const MAX_WORKER_TRANSCRIPT_INPUT_NODES = 100
const MAX_WORKER_TRANSCRIPT_RESPONSE_BYTES = 512 * 1024
const TRUNCATION_MARKER = '\n… (truncated)'
const DISPATCH_CAPABILITY_PATTERN = /\bdcap_[A-Za-z0-9_-]{20,}\b/g
const DISPATCH_CAPABILITY_REDACTION = '[dispatch capability redacted]'
export function clampWorkerTranscriptLimit(limit: number | undefined): number {
if (!Number.isFinite(limit) || (limit ?? 0) <= 0) {
return DEFAULT_WORKER_TRANSCRIPT_MESSAGE_LIMIT
}
return Math.min(Math.floor(limit!), MAX_WORKER_TRANSCRIPT_MESSAGE_LIMIT)
}
export function redactWorkerTerminalLines(lines: readonly string[]): {
lines: string[]
warnings: string[]
} {
let redacted = false
const bounded = lines.map((line) => {
const result = replaceDispatchCapabilities(line)
redacted ||= result.redacted
return result.value
})
return {
lines: bounded,
warnings: redacted ? ['Dispatch capability tokens were redacted from terminal output.'] : []
}
}
export function boundWorkerTranscriptMessages(
messages: readonly NativeChatMessage[],
transcriptPath?: string
): {
messages: NativeChatMessage[]
limited: boolean
warnings: string[]
} {
const warnings = new Set<string>()
const bounded: NativeChatMessage[] = []
let bytes = 2
for (const message of messages) {
const next = boundMessage(message, transcriptPath, warnings)
const serializedBytes = Buffer.byteLength(JSON.stringify(next), 'utf8') + 1
if (bounded.length > 0 && bytes + serializedBytes > MAX_WORKER_TRANSCRIPT_RESPONSE_BYTES) {
warnings.add('Transcript response was clipped to the wire-size limit.')
return { messages: bounded, limited: true, warnings: [...warnings] }
}
bounded.push(next)
bytes += serializedBytes
}
return { messages: bounded, limited: false, warnings: [...warnings] }
}
function boundMessage(
message: NativeChatMessage,
transcriptPath: string | undefined,
warnings: Set<string>
): NativeChatMessage {
const blocks = message.blocks.slice(0, MAX_WORKER_TRANSCRIPT_BLOCKS)
if (blocks.length < message.blocks.length) {
warnings.add('Some transcript blocks were omitted from oversized messages.')
}
return {
...message,
id: boundIdentifier(message.id, transcriptPath, warnings),
...(message.turnId
? { turnId: boundIdentifier(message.turnId, transcriptPath, warnings) }
: {}),
blocks: blocks.map((block) => boundBlock(block, warnings))
}
}
function boundBlock(block: NativeChatBlock, warnings: Set<string>): NativeChatBlock {
if (block.type === 'text') {
return { ...block, text: clipText(block.text, warnings) }
}
if (block.type === 'tool-result') {
return { ...block, output: clipText(block.output, warnings) }
}
if (block.type === 'tool-call') {
const budget = {
remaining: MAX_WORKER_TRANSCRIPT_BLOCK_CHARS,
nodes: MAX_WORKER_TRANSCRIPT_INPUT_NODES
}
return {
...block,
name: clipMetadata(block.name, warnings),
input: boundToolInput(block.input, budget, 0, warnings)
}
}
if (block.path || (block.url && isLocalFileLocator(block.url))) {
warnings.add('Local image paths were omitted from transcript output.')
return {
type: 'image-ref',
...(block.alt ? { alt: clipText(block.alt, warnings) } : {})
}
}
return {
...block,
...(block.url ? { url: clipMetadata(block.url, warnings) } : {}),
...(block.alt ? { alt: clipText(block.alt, warnings) } : {})
}
}
function boundIdentifier(
value: string,
transcriptPath: string | undefined,
warnings: Set<string>
): string {
if (transcriptPath && value.includes(transcriptPath)) {
warnings.add('Transcript-backed message identifiers were made opaque.')
return `worker-message-${createHash('sha256').update(value).digest('base64url').slice(0, 32)}`
}
return clipMetadata(value, warnings)
}
function isLocalFileLocator(value: string): boolean {
return (
/^file:/i.test(value) ||
/^[a-z]:[\\/]/i.test(value) ||
value.startsWith('/') ||
value.startsWith('\\\\')
)
}
function clipMetadata(value: string, warnings: Set<string>): string {
const redacted = redactSensitiveText(value, warnings)
if (redacted.length <= 512) {
return redacted
}
warnings.add('Oversized transcript metadata was clipped.')
return redacted.slice(0, 512)
}
function clipText(value: string, warnings: Set<string>): string {
const redacted = redactSensitiveText(value, warnings)
if (redacted.length <= MAX_WORKER_TRANSCRIPT_BLOCK_CHARS) {
return redacted
}
warnings.add('Oversized transcript text was clipped.')
return `${redacted.slice(0, MAX_WORKER_TRANSCRIPT_BLOCK_CHARS)}${TRUNCATION_MARKER}`
}
function boundToolInput(
value: unknown,
budget: { remaining: number; nodes: number },
depth: number,
warnings: Set<string>
): unknown {
budget.nodes--
if (budget.nodes < 0 || budget.remaining <= 0) {
warnings.add('Oversized tool input was clipped.')
return '… (truncated)'
}
if (typeof value === 'string') {
const redacted = redactSensitiveText(value, warnings)
const length = Math.min(redacted.length, budget.remaining)
budget.remaining -= length
if (length < redacted.length) {
warnings.add('Oversized tool input was clipped.')
return `${redacted.slice(0, length)}… (truncated)`
}
return redacted
}
if (!value || typeof value !== 'object') {
return value
}
if (depth >= 5) {
warnings.add('Deep tool input was clipped.')
return '… (truncated)'
}
if (Array.isArray(value)) {
const result = value
.slice(0, MAX_WORKER_TRANSCRIPT_INPUT_ITEMS)
.map((item) => boundToolInput(item, budget, depth + 1, warnings))
if (value.length > MAX_WORKER_TRANSCRIPT_INPUT_ITEMS) {
warnings.add('Oversized tool input was clipped.')
result.push('… (truncated)')
}
return result
}
const result: Record<string, unknown> = Object.create(null)
let count = 0
for (const [rawKey, entry] of Object.entries(value)) {
if (count >= MAX_WORKER_TRANSCRIPT_INPUT_ITEMS || budget.remaining <= 0) {
warnings.add('Oversized tool input was clipped.')
result['…'] = 'truncated'
break
}
const redactedKey = redactSensitiveText(rawKey, warnings)
const key = redactedKey.slice(0, Math.min(redactedKey.length, budget.remaining, 128))
budget.remaining -= key.length
result[key] = boundToolInput(entry, budget, depth + 1, warnings)
count++
}
return result
}
function redactSensitiveText(value: string, warnings: Set<string>): string {
const result = replaceDispatchCapabilities(value)
if (!result.redacted) {
return result.value
}
warnings.add('Dispatch capability tokens were redacted from transcript output.')
return result.value
}
function replaceDispatchCapabilities(value: string): { value: string; redacted: boolean } {
DISPATCH_CAPABILITY_PATTERN.lastIndex = 0
const redacted = DISPATCH_CAPABILITY_PATTERN.test(value)
DISPATCH_CAPABILITY_PATTERN.lastIndex = 0
return {
value: redacted
? value.replace(DISPATCH_CAPABILITY_PATTERN, DISPATCH_CAPABILITY_REDACTION)
: value,
redacted
}
}
@@ -0,0 +1,188 @@
import { appendFile, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { readWorkerTranscript } from './worker-transcript-read'
function codexMessage(id: string, text: string): string {
return JSON.stringify({
timestamp: '2026-07-24T12:00:00.000Z',
type: 'event_msg',
payload: { id, type: 'agent_message', message: text }
})
}
function grokMessage(id: string, text: string): string {
return JSON.stringify({
id,
timestamp: '2026-07-24T12:00:00.000Z',
type: 'assistant',
content: text
})
}
describe('worker transcript reads', () => {
let directory: string
let transcriptPath: string
beforeEach(async () => {
directory = await mkdtemp(join(tmpdir(), 'orca-worker-transcript-'))
transcriptPath = join(directory, 'rollout-session.jsonl')
})
afterEach(async () => {
await rm(directory, { recursive: true, force: true })
})
it('returns a bounded tail followed by new messages from the exact file', async () => {
await writeFile(
transcriptPath,
[codexMessage('one', 'first'), codexMessage('two', 'second'), codexMessage('three', 'third')]
.join('\n')
.concat('\n')
)
const initial = await readWorkerTranscript({
agent: 'codex',
sessionId: 'session-exact',
transcriptPath,
limit: 2
})
expect(initial).toMatchObject({
ok: true,
messages: [
{ id: 'two', blocks: [{ type: 'text', text: 'second' }] },
{ id: 'three', blocks: [{ type: 'text', text: 'third' }] }
],
limited: true
})
if (!initial.ok) {
throw new Error('Expected the initial transcript page')
}
await appendFile(transcriptPath, `{malformed}\n${codexMessage('four', 'fourth')}\n`)
const appended = await readWorkerTranscript({
agent: 'codex',
sessionId: 'session-exact',
transcriptPath,
offset: initial.nextOffset,
limit: 2
})
expect(appended).toMatchObject({
ok: true,
messages: [{ id: 'four', blocks: [{ type: 'text', text: 'fourth' }] }],
limited: false,
warnings: ['1 malformed transcript record(s) were skipped.']
})
})
it('reports source changes and unsupported providers without guessing', async () => {
await writeFile(transcriptPath, `${codexMessage('one', 'first')}\n`)
await expect(
readWorkerTranscript({
agent: 'codex',
sessionId: 'session-exact',
transcriptPath,
offset: 10_000,
limit: 2
})
).resolves.toMatchObject({ ok: false, reason: 'source_changed' })
await expect(
readWorkerTranscript({
agent: 'gemini',
sessionId: 'session-other',
transcriptPath,
limit: 2
})
).resolves.toEqual({ ok: false, reason: 'provider_unsupported', warnings: [] })
})
it('reuses the Native Chat Grok decoder', async () => {
await writeFile(transcriptPath, `${grokMessage('grok-one', 'Grok structured output')}\n`)
await expect(
readWorkerTranscript({
agent: 'grok',
sessionId: 'session-grok',
transcriptPath,
limit: 2
})
).resolves.toMatchObject({
ok: true,
messages: [
{
role: 'assistant',
blocks: [{ type: 'text', text: 'Grok structured output' }]
}
]
})
})
it('makes file-position fallback IDs opaque', async () => {
await writeFile(
transcriptPath,
`${JSON.stringify({
timestamp: '2026-07-24T12:00:00.000Z',
type: 'event_msg',
payload: { type: 'agent_message', message: 'no provider id' }
})}\n`
)
const result = await readWorkerTranscript({
agent: 'codex',
sessionId: 'session-exact',
transcriptPath,
limit: 2
})
expect(result).toMatchObject({
ok: true,
messages: [{ id: expect.stringMatching(/^worker-message-/) }],
warnings: ['Transcript-backed message identifiers were made opaque.']
})
expect(result.ok && JSON.stringify(result.messages)).not.toContain(transcriptPath)
})
it('advances past a record larger than the forward scan window', async () => {
await writeFile(transcriptPath, 'x'.repeat(8 * 1024 * 1024 + 10))
const oversized = await readWorkerTranscript({
agent: 'codex',
sessionId: 'session-exact',
transcriptPath,
offset: 0,
limit: 2
})
expect(oversized).toMatchObject({
ok: true,
messages: [],
limited: true,
warnings: expect.arrayContaining([
'1 oversized transcript record(s) were skipped.',
'Transcript scanning stopped at the bounded byte limit; continue with the cursor.'
])
})
if (!oversized.ok) {
throw new Error('Expected the oversized transcript page')
}
expect(oversized.nextOffset).toBe(8 * 1024 * 1024)
await appendFile(transcriptPath, `\n${codexMessage('after', 'after oversized')}\n`)
const continued = await readWorkerTranscript({
agent: 'codex',
sessionId: 'session-exact',
transcriptPath,
offset: oversized.nextOffset,
limit: 2
})
expect(continued).toMatchObject({
ok: true,
messages: [{ id: 'after', blocks: [{ type: 'text', text: 'after oversized' }] }],
limited: false
})
})
})
@@ -0,0 +1,263 @@
import { open, stat } from 'node:fs/promises'
import type { AgentType, NativeChatMessage } from '../../../shared/native-chat-types'
import { resolveNativeChatTranscriptAgent } from '../../../shared/native-chat-agent-support'
import type { OrchestrationWorkerReadFallbackReason } from '../../../shared/orchestration-worker-output'
import { resolveSessionFilePath } from '../../native-chat/session-file-resolver'
import {
MAX_NATIVE_CHAT_TRANSCRIPT_RECORD_BYTES,
nativeChatLineDecoderForAgent,
readNativeChatTranscriptTailFile,
type NativeChatLineDecoder
} from '../../native-chat/transcript-tail-reader'
import { transcriptFallbackId } from '../../native-chat/transcript-fallback-id'
import {
boundWorkerTranscriptMessages,
clampWorkerTranscriptLimit
} from './worker-transcript-payload'
const MAX_FORWARD_TRANSCRIPT_SCAN_BYTES = 8 * 1024 * 1024
type WorkerTranscriptReadFailure = {
ok: false
reason: OrchestrationWorkerReadFallbackReason | 'source_changed'
warnings: string[]
}
type WorkerTranscriptReadSuccess = {
ok: true
filePath: string
messages: NativeChatMessage[]
nextOffset: number
limited: boolean
warnings: string[]
}
export type WorkerTranscriptReadResult = WorkerTranscriptReadFailure | WorkerTranscriptReadSuccess
export async function readWorkerTranscript(args: {
agent: AgentType
sessionId: string
transcriptPath?: string
offset?: number
limit?: number
}): Promise<WorkerTranscriptReadResult> {
const transcriptAgent = resolveNativeChatTranscriptAgent(args.agent)
if (!transcriptAgent) {
return { ok: false, reason: 'provider_unsupported', warnings: [] }
}
const decode = nativeChatLineDecoderForAgent(args.agent)
if (!decode) {
return { ok: false, reason: 'provider_unsupported', warnings: [] }
}
let filePath: string | null
try {
filePath = await resolveSessionFilePath(args.agent, args.sessionId, {
transcriptPath: args.transcriptPath
})
} catch {
return { ok: false, reason: 'transcript_unreadable', warnings: [] }
}
if (!filePath) {
return { ok: false, reason: 'transcript_missing', warnings: [] }
}
const limit = clampWorkerTranscriptLimit(args.limit)
try {
const page =
args.offset === undefined
? await readInitialPage(filePath, limit, decode)
: await readForwardPage(filePath, args.offset, limit, decode)
if (!page.ok) {
return page
}
const bounded = boundWorkerTranscriptMessages(page.messages, filePath)
return {
ok: true,
filePath,
messages: bounded.messages,
nextOffset: page.nextOffset,
limited: page.limited || bounded.limited,
warnings: [...page.warnings, ...bounded.warnings]
}
} catch (error) {
const code = (error as NodeJS.ErrnoException | null)?.code
return {
ok: false,
reason:
code === 'ENOENT'
? 'transcript_missing'
: code === 'EACCES' || code === 'EPERM'
? 'transcript_unreadable'
: 'transcript_parse_failed',
warnings: []
}
}
}
async function readInitialPage(
filePath: string,
limit: number,
decode: NativeChatLineDecoder
): Promise<WorkerTranscriptReadSuccess> {
const page = await readNativeChatTranscriptTailFile(filePath, limit, decode, false)
return {
ok: true,
filePath,
messages: page.messages,
nextOffset: page.consumedTo,
limited: page.hasMore,
warnings: recordWarnings(page.malformedRecordCount, page.oversizedRecordCount)
}
}
async function readForwardPage(
filePath: string,
startOffset: number,
limit: number,
decode: NativeChatLineDecoder
): Promise<WorkerTranscriptReadResult> {
const fileSize = (await stat(filePath)).size
if (startOffset > fileSize) {
return { ok: false, reason: 'source_changed', warnings: [] }
}
if (startOffset === fileSize) {
return {
ok: true,
filePath,
messages: [],
nextOffset: startOffset,
limited: false,
warnings: []
}
}
const scanEnd = Math.min(fileSize, startOffset + MAX_FORWARD_TRANSCRIPT_SCAN_BYTES)
const handle = await open(filePath, 'r')
const messages: NativeChatMessage[] = []
let pendingChunks: Buffer[] = []
let pendingBytes = 0
let pendingStart = startOffset
let droppingOversizedRecord = await startsInsideRecord(handle, startOffset)
let malformedRecordCount = 0
let oversizedRecordCount = 0
let nextOffset = startOffset
try {
const stream = handle.createReadStream({
start: startOffset,
end: scanEnd - 1,
autoClose: false
})
let absoluteOffset = startOffset
for await (const rawChunk of stream) {
const chunk = Buffer.isBuffer(rawChunk) ? rawChunk : Buffer.from(rawChunk)
let segmentStart = 0
let newline = chunk.indexOf(0x0a)
while (newline >= 0) {
retainPart(chunk.subarray(segmentStart, newline))
const lineEnd = absoluteOffset + newline + 1
if (!droppingOversizedRecord) {
decodeLine()
}
resetLine(lineEnd)
nextOffset = lineEnd
if (messages.length >= limit) {
return successfulPage(lineEnd < fileSize)
}
segmentStart = newline + 1
newline = chunk.indexOf(0x0a, segmentStart)
}
if (segmentStart < chunk.length) {
retainPart(chunk.subarray(segmentStart))
}
absoluteOffset += chunk.length
}
if (droppingOversizedRecord) {
nextOffset = scanEnd
}
return successfulPage(scanEnd < fileSize, scanEnd < fileSize)
} finally {
await handle.close()
}
function retainPart(part: Buffer): void {
if (droppingOversizedRecord) {
return
}
pendingBytes += part.length
if (pendingBytes > MAX_NATIVE_CHAT_TRANSCRIPT_RECORD_BYTES) {
pendingChunks = []
droppingOversizedRecord = true
oversizedRecordCount++
return
}
pendingChunks.push(part)
}
function resetLine(nextStart: number): void {
pendingChunks = []
pendingBytes = 0
droppingOversizedRecord = false
pendingStart = nextStart
}
function decodeLine(): void {
let line = Buffer.concat(pendingChunks).toString('utf8')
if (line.endsWith('\r')) {
line = line.slice(0, -1)
}
if (!line) {
return
}
try {
JSON.parse(line)
} catch {
malformedRecordCount++
return
}
const message = decode(line, transcriptFallbackId(filePath, pendingStart))
if (message) {
messages.push(message)
}
}
function successfulPage(limited: boolean, scanLimited = false): WorkerTranscriptReadSuccess {
return {
ok: true,
filePath,
messages,
nextOffset,
limited,
warnings: recordWarnings(malformedRecordCount, oversizedRecordCount, scanLimited)
}
}
}
async function startsInsideRecord(
handle: Awaited<ReturnType<typeof open>>,
offset: number
): Promise<boolean> {
if (offset === 0) {
return false
}
const previousByte = Buffer.allocUnsafe(1)
const { bytesRead } = await handle.read(previousByte, 0, 1, offset - 1)
return bytesRead === 1 && previousByte[0] !== 0x0a
}
function recordWarnings(
malformedRecordCount = 0,
oversizedRecordCount = 0,
scanLimited = false
): string[] {
const warnings: string[] = []
if (malformedRecordCount > 0) {
warnings.push(`${malformedRecordCount} malformed transcript record(s) were skipped.`)
}
if (oversizedRecordCount > 0) {
warnings.push(`${oversizedRecordCount} oversized transcript record(s) were skipped.`)
}
if (scanLimited) {
warnings.push(
'Transcript scanning stopped at the bounded byte limit; continue with the cursor.'
)
}
return warnings
}
+16
View File
@@ -44,6 +44,9 @@ export type RpcRequest = {
authToken: string
method: string
params?: unknown
orchestrationCapability?: string
orchestrationContractVersion?: number
orchestrationRequestId?: string
}
export type RpcContext = {
@@ -60,6 +63,19 @@ export type RpcContext = {
pairedDeviceId?: string
// Why: lets handlers gate mobile payload truncation to phones only; undefined for in-process callers → treat as full-class (no clip).
clientKind?: 'mobile' | 'runtime'
// Why: Dispatch authority rides in the authenticated RPC envelope, never in user payload fields.
orchestrationCapability?: string
// Why: long-lived mutations such as ask can durably expose acceptance before their waiter settles.
recordMutationReceipt?: (receipt: unknown) => void
// Why: worker-start commits this identity with its starting Dispatch so crash recovery always has an inspectable operation.
orchestrationMutation?: {
callerFingerprint: string
requestId: string
method: string
payloadHash: string
}
// Why: federation pins the authenticated saved-environment caller without exposing its token to handlers or storage.
authenticatedCallerFingerprint?: string
pairing?: PairingRpcContext
// Why: mobile terminal traffic bypasses JSON streaming; undefined on Unix/socket and non-E2EE WebSocket paths.
sendBinary?: (bytes: Uint8Array<ArrayBufferLike>) => boolean | void
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from 'vitest'
import { z } from 'zod'
import type { PersistedUIState } from '../../../shared/types'
import { getDefaultUIState } from '../../../shared/constants'
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../shared/protocol-version'
import {
ORCA_RUNTIME_RPC_BROWSER_UI_SOURCE,
ORCA_RUNTIME_RPC_FEATURE_INTERACTION_SOURCE_KEY
@@ -11,7 +12,15 @@ import { defineMethod, defineStreamingMethod, type RpcRequest } from './core'
import type { OrcaRuntimeService } from '../orca-runtime'
function makeRequest(method: string, params: unknown = {}): RpcRequest {
return { id: 'req-1', authToken: 'tok', method, params }
return {
id: 'req-1',
authToken: 'tok',
method,
params,
...(method.startsWith('orchestration.')
? { orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION }
: {})
}
}
function makeRuntime(ui: PersistedUIState = getDefaultUIState()): OrcaRuntimeService {
+50 -64
View File
@@ -15,9 +15,9 @@ import {
type RpcRequest,
type RpcResponse
} from './core'
import type { TerminalStreamFrame } from '../../../shared/terminal-stream-protocol'
import type { FeatureInteractionId } from '../../../shared/feature-interactions'
import { isBrowserPaneUiRuntimeRpcParams } from '../../../shared/runtime-rpc-feature-interaction-source'
import {
computerErrorData,
errorResponse,
@@ -29,6 +29,13 @@ import {
import { ALL_RPC_METHODS } from './methods'
import { emulatorProbe, emulatorProbeError } from '../../emulator/emulator-probe'
import type { OrcaRuntimeService } from '../orca-runtime'
import {
OrchestrationMutationExecutor,
authenticatedCallerFingerprint,
type DurableMutationInvocation
} from './orchestration-mutation-executor'
import { orchestrationMigrationFence } from './orchestration-contract-fence'
import { getRuntimeFeatureInteractionId } from './runtime-feature-interaction'
export type DispatcherOptions = {
runtime: OrcaRuntimeService
@@ -38,10 +45,12 @@ export type DispatcherOptions = {
export class RpcDispatcher {
private readonly runtime: OrcaRuntimeService
private readonly registry: RpcRegistry
private readonly orchestrationMutations: OrchestrationMutationExecutor
constructor({ runtime, methods = ALL_RPC_METHODS }: DispatcherOptions) {
this.runtime = runtime
this.registry = buildRegistry(methods)
this.orchestrationMutations = new OrchestrationMutationExecutor(runtime)
}
async dispatch(request: RpcRequest, options?: { signal?: AbortSignal }): Promise<RpcResponse> {
@@ -56,6 +65,11 @@ export class RpcDispatcher {
)
}
const migrationFence = orchestrationMigrationFence(request, meta)
if (migrationFence) {
return migrationFence
}
const parsedParams = this.parseParams(request, method, meta)
if (parsedParams.error) {
return parsedParams.error
@@ -78,10 +92,17 @@ export class RpcDispatcher {
emulatorProbe(`rpc ${request.method}`, request.params)
}
try {
const result = await method.handler(parsedParams.value, {
runtime: this.runtime,
signal: options?.signal
})
const invoke = (mutation?: DurableMutationInvocation) =>
method.handler(parsedParams.value, {
runtime: this.runtime,
signal: options?.signal,
requestId: request.id,
orchestrationCapability: request.orchestrationCapability,
authenticatedCallerFingerprint: authenticatedCallerFingerprint(request),
recordMutationReceipt: mutation?.recordReceipt,
orchestrationMutation: mutation?.identity
})
const result = await this.orchestrationMutations.run(request, parsedParams.value, invoke)
this.recordRuntimeFeatureInteraction(request.method, result, undefined, request.params)
return successResponse(request.id, meta, result)
} catch (error) {
@@ -123,6 +144,12 @@ export class RpcDispatcher {
return
}
const migrationFence = orchestrationMigrationFence(request, meta)
if (migrationFence) {
reply(JSON.stringify(migrationFence))
return
}
const parsedParams = this.parseParams(request, method, meta)
if (parsedParams.error) {
reply(JSON.stringify(parsedParams.error))
@@ -131,18 +158,24 @@ export class RpcDispatcher {
if (!isStreamingMethod(method)) {
try {
const result = await method.handler(parsedParams.value, {
runtime: this.runtime,
signal: options?.signal,
requestId: request.id,
connectionId: options?.connectionId,
clientId: options?.clientId,
pairedDeviceId: options?.pairedDeviceId,
clientKind: options?.clientKind,
pairing: options?.pairing,
sendBinary: options?.sendBinary,
registerBinaryStreamHandler: options?.registerBinaryStreamHandler
})
const invoke = (mutation?: DurableMutationInvocation) =>
method.handler(parsedParams.value, {
runtime: this.runtime,
signal: options?.signal,
requestId: request.id,
connectionId: options?.connectionId,
clientId: options?.clientId,
pairedDeviceId: options?.pairedDeviceId,
clientKind: options?.clientKind,
orchestrationCapability: request.orchestrationCapability,
authenticatedCallerFingerprint: authenticatedCallerFingerprint(request),
recordMutationReceipt: mutation?.recordReceipt,
orchestrationMutation: mutation?.identity,
pairing: options?.pairing,
sendBinary: options?.sendBinary,
registerBinaryStreamHandler: options?.registerBinaryStreamHandler
})
const result = await this.orchestrationMutations.run(request, parsedParams.value, invoke)
this.recordRuntimeFeatureInteraction(request.method, result, undefined, request.params)
reply(JSON.stringify(successResponse(request.id, meta, result)))
} catch (error) {
@@ -270,50 +303,3 @@ export class RpcDispatcher {
}
}
}
function getRuntimeFeatureInteractionId(
method: string,
result: unknown,
rawParams?: unknown
): FeatureInteractionId | null {
if (method === 'browser.profileImportFromBrowser') {
return hasBooleanResult(result, 'ok') ? 'cookie-import' : null
}
if (method === 'browser.profileClearDefaultCookies') {
return hasBooleanResult(result, 'cleared') ? 'cookie-import' : null
}
if (method === 'browser.screencast.unsubscribe') {
return null
}
if (method.startsWith('browser.') && isBrowserPaneUiRuntimeRpcParams(rawParams)) {
return null
}
if (method.startsWith('browser.') && !method.startsWith('browser.profile')) {
return 'agent-browser-use'
}
if (method.startsWith('emulator.')) {
// Emulator commands are allowed from terminal/CLI (workspace-scoped, like other automation).
// Return null to indicate no special feature-interaction restriction (or add 'emulator-use' later).
return null
}
if (method === 'computer.permissions') {
return 'computer-use-setup'
}
if (
method.startsWith('computer.') &&
method !== 'computer.capabilities' &&
method !== 'computer.permissionsStatus'
) {
return 'computer-use'
}
if (method.startsWith('orchestration.')) {
return 'agent-orchestration'
}
return null
}
function hasBooleanResult(value: unknown, key: string): boolean {
return (
value !== null && typeof value === 'object' && (value as Record<string, unknown>)[key] === true
)
}
+12
View File
@@ -30,6 +30,18 @@ describe('mapRuntimeError', () => {
})
})
it.each(['remote_runtime_unavailable', 'runtime_timeout', 'invalid_runtime_response'])(
'preserves structured remote transport failure %s',
(code) => {
const error = Object.assign(new Error(`Remote transport failed: ${code}`), { code })
expect(mapRuntimeError('req_1', { runtimeId: 'runtime-1' }, error)).toMatchObject({
ok: false,
error: { code, message: `Remote transport failed: ${code}` }
})
}
)
it.each([
['window_not_focused', 'keyboard input requires focus', 'restore-window'],
['permission_denied', 'missing DBUS_SESSION_BUS_ADDRESS', 'permissions'],
+35 -1
View File
@@ -60,7 +60,41 @@ const RUNTIME_PASSTHROUGH_CODES: ReadonlySet<string> = new Set([
const COMPUTER_PASSTHROUGH_CODES: ReadonlySet<string> = new Set(Object.values(COMPUTER_ERROR_CODES))
const LINEAR_PASSTHROUGH_CODES: ReadonlySet<string> = new Set(LINEAR_ERROR_CODES)
const STRUCTURED_RUNTIME_PASSTHROUGH_CODES: ReadonlySet<string> = new Set([
'worktree_id_requires_full_path'
'worktree_id_requires_full_path',
'run_not_found',
'run_required',
'stable_pane_required',
'consumer_fenced',
'task_not_found',
'task_not_startable',
'dispatch_not_found',
'dispatch_run_mismatch',
'dispatch_inactive',
'worker_identity_changed',
'cursor_invalid',
'cursor_dispatch_mismatch',
'source_changed',
'transcript_required',
'server_required',
'worktree_not_found_on_server',
'resource_server_mismatch',
'peer_changed',
'remote_runtime_unavailable',
'runtime_timeout',
'invalid_runtime_response',
'capability_unsupported',
'relay_quota_exceeded',
'dispatch_capability_invalid',
'agent_unconfigured',
'terminal_worktree_mismatch',
'request_mismatch',
'orchestration_migration_required',
'operation_unknown',
'question_not_found',
'answer_conflict',
'stale_delivery',
'waiter_exists',
'invalid_argument'
])
export function mapRuntimeError(id: string, meta: RpcEnvelopeMeta, error: unknown): RpcFailure {
@@ -0,0 +1,104 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../../shared/protocol-version'
import { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationDb } from '../../orchestration/db'
import type { RpcRequest } from '../core'
import { RpcDispatcher } from '../dispatcher'
import { ORCHESTRATION_METHODS } from './orchestration'
describe('orchestration federated message targeting', () => {
let db: OrchestrationDb | undefined
let runtime: OrcaRuntimeService | undefined
afterEach(() => {
runtime?.stopOrchestrationFederationRelay()
db?.close()
})
it('rejects explicit send and ask targets without enqueueing a relay', async () => {
db = new OrchestrationDb(':memory:')
runtime = new OrcaRuntimeService()
runtime.setOrchestrationDb(db)
const paneKey = 'tab_worker:leaf_worker'
const processIncarnation = 'worker_epoch:pty:1'
const dispatchId = 'ctx_remote_targeting'
vi.spyOn(runtime, 'getTerminalPaneKey').mockReturnValue(paneKey)
vi.spyOn(runtime, 'getTerminalProcessIncarnation').mockReturnValue(processIncarnation)
db.createRemoteDispatchAttachment({
dispatchId,
taskId: 'task_remote_targeting',
homePeerFingerprint: 'home_peer',
protocolVersion: 1,
runtimeEpoch: runtime.getRuntimeId(),
mutationReceipt: {
callerFingerprint: 'home_peer',
requestId: 'attach_request',
method: 'orchestration.federationAttachStart',
payloadHash: 'attach_payload'
}
})
const capability = db.prepareRemoteAttachmentAuthority({
dispatchId,
paneKey,
processIncarnation,
worktreeId: 'repo::remote-worktree',
terminalHandle: 'term_remote_worker',
setupState: 'not_applicable',
effects: []
})
db.markRemoteAttachmentReady(dispatchId)
const dispatcher = new RpcDispatcher({ runtime, methods: ORCHESTRATION_METHODS })
const requests: RpcRequest[] = [
request('send_to', capability, 'orchestration.send', {
from: 'term_remote_worker',
to: 'run:explicit',
subject: 'Wrong explicit target'
}),
request('send_run', capability, 'orchestration.send', {
from: 'term_remote_worker',
run: 'run_explicit',
subject: 'Wrong explicit Run'
}),
request('ask_to', capability, 'orchestration.ask', {
from: 'term_remote_worker',
to: 'run:explicit',
question: 'Wrong explicit target?'
}),
request('ask_run', capability, 'orchestration.ask', {
from: 'term_remote_worker',
run: 'run_explicit',
question: 'Wrong explicit Run?'
})
]
for (const item of requests) {
await expect(dispatcher.dispatch(item)).resolves.toMatchObject({
ok: false,
error: {
code: 'invalid_argument',
message: 'Federated Dispatch messages route to their Run home; omit --to and --run.'
}
})
}
expect(
db.listFederationRelay({ dispatchId, direction: 'to_home', afterSequence: 0 })
).toHaveLength(0)
})
})
function request(
id: string,
capability: string,
method: 'orchestration.send' | 'orchestration.ask',
params: Record<string, unknown>
): RpcRequest {
return {
id: `rpc_${id}`,
authToken: 'worker-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: `request_${id}`,
orchestrationCapability: capability,
method,
params
}
}
@@ -0,0 +1,292 @@
import { isTuiAgent } from '../../../../shared/tui-agent-config'
import type { RuntimeStatus } from '../../../../shared/runtime-types'
import {
ORCHESTRATION_CONTRACT_RUNTIME_CAPABILITY,
ORCHESTRATION_FEDERATION_CONTROL_MAIL_PROTOCOL_VERSION,
ORCHESTRATION_FEDERATION_CONTROL_MAIL_RUNTIME_CAPABILITY,
ORCHESTRATION_FEDERATION_RUNTIME_CAPABILITY
} from '../../../../shared/protocol-version'
import { orchestrationMigrationData } from '../../../../shared/orchestration-rpc-contract'
import type { OrcaRuntimeService } from '../../orca-runtime'
import type { OrchestrationDb } from '../../orchestration/db'
import { OrchestrationError } from '../../orchestration/orchestration-error'
import type { WorkerStartInput } from './orchestration-worker-start-schema'
export async function startFederatedWorker(args: {
params: WorkerStartInput
runtime: OrcaRuntimeService
db: OrchestrationDb
runId: string
task: { id: string; spec: string; status: string }
orchestrationMutation?: {
callerFingerprint: string
requestId: string
method: string
payloadHash: string
}
}): Promise<unknown> {
const { params, runtime, db, task, runId, orchestrationMutation } = args
if (!orchestrationMutation) {
throw new OrchestrationError(
'invalid_argument',
'Remote worker-start requires a durable retry request.'
)
}
const worktree = params.worktree ?? 'current'
if (worktree === 'current' || worktree === 'new-child') {
throw new OrchestrationError(
'invalid_argument',
'--on requires an exact remote worktree selector or new-top-level.'
)
}
const createsWorktree = worktree === 'new-top-level'
validateRemoteWorkerStart(params, createsWorktree)
const server = runtime.resolveOrchestrationWorkerServer(params.on as string)
const status = (await runtime.callOrchestrationWorkerServer(
server.environmentId,
'status.get',
undefined,
params.timeoutMs
)) as RuntimeStatus
if (!status.capabilities?.includes(ORCHESTRATION_CONTRACT_RUNTIME_CAPABILITY)) {
throw new OrchestrationError(
'orchestration_migration_required',
`Connected server ${server.name} does not support the current orchestration contract. No effects were applied.`,
orchestrationMigrationData('runtime_capability_missing')
)
}
if (!status.capabilities?.includes(ORCHESTRATION_FEDERATION_RUNTIME_CAPABILITY)) {
throw new OrchestrationError(
'capability_unsupported',
`Connected server ${server.name} does not support orchestration federation.`
)
}
const federationProtocolVersion = status.capabilities?.includes(
ORCHESTRATION_FEDERATION_CONTROL_MAIL_RUNTIME_CAPABILITY
)
? ORCHESTRATION_FEDERATION_CONTROL_MAIL_PROTOCOL_VERSION
: 1
const setupDecision = createsWorktree ? (params.setup ?? 'run') : 'not_applicable'
const started = db.createStartingWorkerDispatch({
taskId: task.id,
retryOf: params.retryOf,
startOptions: {
on: server.environmentId,
serverName: server.name,
worktree,
name: params.name ?? null,
repo: params.repo ?? null,
baseBranch: params.baseBranch ?? null,
terminal: params.terminal ?? null,
agent: params.agent ?? null,
timeoutMs: params.timeoutMs ?? 60_000,
setup: setupDecision,
setupSource: createsWorktree
? params.setup
? 'explicit_request'
: 'orchestration_default'
: 'existing_worktree'
},
runtimeEpoch: runtime.getRuntimeId(),
mutationReceipt: orchestrationMutation,
federation: {
environmentId: server.environmentId,
environmentName: server.name,
peerFingerprint: server.peerFingerprint,
protocolVersion: federationProtocolVersion
}
})
db.recordWorkerStage({ dispatchId: started.dispatch.id, stage: 'remote_attach_requested' })
try {
const remote = (await runtime.callOrchestrationWorkerServer(
server.environmentId,
'orchestration.federationAttachStart',
{
dispatchId: started.dispatch.id,
taskId: task.id,
taskSpec: task.spec,
protocolVersion: federationProtocolVersion,
worktree,
name: params.name,
repo: params.repo,
baseBranch: params.baseBranch,
displayName: params.displayName,
comment: params.comment,
setup: createsWorktree ? (params.setup ?? 'run') : undefined,
setupSource: createsWorktree
? params.setup
? 'explicit_request'
: 'orchestration_default'
: undefined,
terminal: params.terminal,
agent: params.agent,
timeoutMs: params.timeoutMs,
devMode: params.devMode
},
(params.timeoutMs ?? 60_000) + 15_000,
{ orchestrationRequestId: orchestrationMutation.requestId }
)) as RemoteStartReceipt
if (remote.dispatchId !== started.dispatch.id) {
throw new OrchestrationError(
'resource_server_mismatch',
'The worker server returned a different Dispatch attachment.'
)
}
if (remote.state === 'ready' && remote.worktreeId && remote.terminalHandle) {
db.updateFederatedDispatchResources({
dispatchId: started.dispatch.id,
remoteRuntimeEpoch: remote.runtimeEpoch,
worktreeId: remote.worktreeId,
terminalHandle: remote.terminalHandle
})
db.recordWorkerStage({
dispatchId: started.dispatch.id,
stage: 'remote_input_accepted',
worktreeId: remote.worktreeId,
terminalHandle: remote.terminalHandle,
setupState: remote.setup?.state,
effects: remote.effects,
residualResources: remote.residualResources
})
const readyWorker = db.markWorkerDispatchReady(started.dispatch.id)
runtime.ensureOrchestrationFederationRelay(runId)
return {
runId,
taskId: task.id,
dispatchId: started.dispatch.id,
state: 'ready',
stage: readyWorker.stage,
server: { environmentId: server.environmentId, name: server.name },
setup: remote.setup,
timeoutMs: params.timeoutMs ?? 60_000,
effects: remote.effects ?? [],
residualResources: remote.residualResources ?? []
}
}
if (remote.state === 'outcome_unknown') {
const worker = db.markWorkerStartUnknown(
started.dispatch.id,
remote.failedStage ?? 'remote_attach',
remote.lastError ?? 'The worker server reported an unknown start outcome.'
)
return federatedUnknownReceipt(worker, task.id, server.name)
}
const worker = db.failWorkerStart(
started.dispatch.id,
remote.failedStage ?? 'remote_attach',
remote.lastError ?? `The worker server returned ${remote.state}.`
)
return {
runId,
taskId: task.id,
dispatchId: started.dispatch.id,
state: worker.state,
stage: worker.stage,
server: { environmentId: server.environmentId, name: server.name },
failedStage: worker.stage,
lastError: worker.last_error,
setup: remote.setup,
effects: remote.effects ?? [],
residualResources: remote.residualResources ?? []
}
} catch (error) {
const reason = error instanceof Error ? error.message : String(error)
if (error instanceof OrchestrationError && isKnownRemoteStartFailure(error.code)) {
const worker = db.failWorkerStart(started.dispatch.id, 'remote_attach', reason)
return {
runId,
taskId: task.id,
dispatchId: started.dispatch.id,
state: worker.state,
stage: worker.stage,
server: { environmentId: server.environmentId, name: server.name },
failedStage: worker.stage,
lastError: worker.last_error,
effects: [],
residualResources: []
}
}
const worker = db.markWorkerStartUnknown(started.dispatch.id, 'remote_attach', reason)
return federatedUnknownReceipt(worker, task.id, server.name)
}
}
type RemoteStartReceipt = {
dispatchId: string
state: string
runtimeEpoch: string
worktreeId?: string
terminalHandle?: string
setup?: { state: string }
effects?: unknown[]
residualResources?: unknown[]
failedStage?: string
lastError?: string
}
function validateRemoteWorkerStart(params: WorkerStartInput, createsWorktree: boolean): void {
if (createsWorktree && (!params.name || !params.repo)) {
throw new OrchestrationError(
'invalid_argument',
'Remote new-top-level requires --name and an explicit --repo from remote discovery.'
)
}
if (createsWorktree && params.terminal) {
throw new OrchestrationError(
'invalid_argument',
'--terminal cannot combine with remote new-worktree creation.'
)
}
if (!createsWorktree && (params.name || params.repo || params.baseBranch || params.setup)) {
throw new OrchestrationError(
'invalid_argument',
'Creation and setup options apply only to remote new-top-level worktrees.'
)
}
if (params.terminal && params.agent) {
throw new OrchestrationError(
'invalid_argument',
'--terminal reuses an existing agent and cannot combine with --agent.'
)
}
if (!params.terminal && (!params.agent || !isTuiAgent(params.agent))) {
throw new OrchestrationError(
'agent_unconfigured',
'A configured --agent is required when remote worker-start creates a terminal.'
)
}
}
function isKnownRemoteStartFailure(code: string): boolean {
return [
'invalid_argument',
'agent_unconfigured',
'worktree_not_found_on_server',
'terminal_worktree_mismatch',
'capability_unsupported'
].includes(code)
}
function federatedUnknownReceipt(
worker: { dispatch_id: string; state: string; stage: string; last_error: string | null },
taskId: string,
serverName: string
): unknown {
return {
taskId,
dispatchId: worker.dispatch_id,
state: 'outcome_unknown',
stage: worker.stage,
server: { name: serverName },
failedStage: worker.stage,
lastError: worker.last_error,
effects: [],
residualResources: [],
nextCommands: [
`orca orchestration worker-show --dispatch ${worker.dispatch_id} --json`,
`orca orchestration worker-abandon --dispatch ${worker.dispatch_id} --json`
]
}
}
@@ -0,0 +1,345 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../../shared/protocol-version'
import type { RuntimeRpcResponse } from '../../../../shared/runtime-rpc-envelope'
import { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationDb } from '../../orchestration/db'
import type { OrchestrationEnvironmentTransport } from '../../orchestration/environment-transport'
import type { RpcRequest } from '../core'
import { RpcDispatcher } from '../dispatcher'
import { authenticatedCallerFingerprint } from '../orchestration-mutation-executor'
import { ORCHESTRATION_METHODS } from './orchestration'
describe('orchestration federation control mail', () => {
const homeToken = 'run-home-device-token'
const workerToken = 'worker-local-token'
const workerPeerFingerprint = 'worker-peer'
const coordinatorPaneKey = 'tab_coord:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
const workerPaneKey = 'tab_worker:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
const processIncarnation = 'worker-runtime:pty:1'
let homeDb: OrchestrationDb
let workerDb: OrchestrationDb
let homeRuntime: OrcaRuntimeService
let workerRuntime: OrcaRuntimeService
let homeDispatcher: RpcDispatcher
let workerDispatcher: RpcDispatcher
let dispatchId: string
let runId: string
beforeEach(() => {
workerDb = new OrchestrationDb(':memory:')
workerRuntime = new OrcaRuntimeService()
workerRuntime.setOrchestrationDb(workerDb)
vi.spyOn(workerRuntime, 'getTerminalPaneKey').mockImplementation((handle) =>
handle === 'term_worker' ? workerPaneKey : null
)
vi.spyOn(workerRuntime, 'getTerminalProcessIncarnation').mockImplementation((handle) =>
handle === 'term_worker' ? processIncarnation : null
)
workerDispatcher = new RpcDispatcher({
runtime: workerRuntime,
methods: ORCHESTRATION_METHODS
})
const transport: OrchestrationEnvironmentTransport = {
resolve: () => ({
environmentId: 'environment_worker',
name: 'worker',
peerFingerprint: workerPeerFingerprint
}),
call: async (_selector, method, params, _timeoutMs, envelope) => {
if (method === 'status.get') {
return {
id: 'status',
ok: true,
result: workerRuntime.getStatus(),
_meta: { runtimeId: workerRuntime.getRuntimeId() }
}
}
const response = (await workerDispatcher.dispatch({
id: `remote_${method}`,
authToken: homeToken,
method,
params,
orchestrationContractVersion: envelope?.orchestrationContractVersion,
orchestrationRequestId: envelope?.orchestrationRequestId
})) as RuntimeRpcResponse<unknown>
return response
}
}
homeDb = new OrchestrationDb(':memory:')
homeRuntime = new OrcaRuntimeService(null, undefined, {
orchestrationEnvironmentTransport: transport
})
homeRuntime.setOrchestrationDb(homeDb)
vi.spyOn(homeRuntime, 'getTerminalPaneKey').mockImplementation((handle) =>
handle === 'term_coord' ? coordinatorPaneKey : null
)
homeDispatcher = new RpcDispatcher({
runtime: homeRuntime,
methods: ORCHESTRATION_METHODS
})
const run = homeDb.createRun({
objective: 'Federated control mail',
coordinatorHandle: 'term_coord',
coordinatorPaneKey
})
runId = run.id
const task = homeDb.createTask({ spec: 'Wait for coordinator guidance', runId })
const started = homeDb.createStartingWorkerDispatch({
taskId: task.id,
startOptions: {},
federation: {
environmentId: 'environment_worker',
environmentName: 'worker',
peerFingerprint: workerPeerFingerprint,
protocolVersion: 2
}
})
dispatchId = started.dispatch.id
homeDb.markWorkerDispatchReady(dispatchId)
const homeFingerprint = authenticatedCallerFingerprint({
id: 'home',
authToken: homeToken,
method: 'orchestration.federationImport'
})
workerDb.createRemoteDispatchAttachment({
dispatchId,
taskId: task.id,
homePeerFingerprint: homeFingerprint,
protocolVersion: 2,
runtimeEpoch: workerRuntime.getRuntimeId(),
mutationReceipt: {
callerFingerprint: homeFingerprint,
requestId: 'attach-worker',
method: 'orchestration.federationAttachStart',
payloadHash: 'attach-worker-payload'
}
})
workerDb.prepareRemoteAttachmentAuthority({
dispatchId,
paneKey: workerPaneKey,
processIncarnation,
worktreeId: 'repo::worker',
terminalHandle: 'term_worker',
setupState: 'not_applicable',
effects: []
})
workerDb.markRemoteAttachmentReady(dispatchId)
})
afterEach(() => {
homeRuntime.stopOrchestrationFederationRelay()
homeDb.close()
workerDb.close()
})
it('routes an exact Dispatch message through the durable relay', async () => {
vi.spyOn(homeRuntime, 'ensureOrchestrationFederationRelay').mockImplementation(() => {})
const sent = await homeDispatcher.dispatch({
id: 'send-control',
authToken: 'coordinator-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'send-control-request',
method: 'orchestration.send',
params: {
from: 'term_coord',
to: `dispatch:${dispatchId}`,
subject: 'Continue',
body: 'Run the focused follow-up.',
type: 'status'
}
})
expect(sent).toMatchObject({
ok: true,
result: { relay: { dispatchId, accepted: true } }
})
expect(homeDb.listPendingFederationRelay(dispatchId, 'to_worker')).toHaveLength(1)
await homeRuntime.syncOrchestrationFederation()
const checked = await workerDispatcher.dispatch(checkRequest('check-imported'))
expect(checked).toMatchObject({
ok: true,
result: {
dispatchId,
count: 1,
messages: [
{
to_handle: `dispatch:${dispatchId}`,
subject: 'Continue',
body: 'Run the focused follow-up.'
}
]
}
})
expect(homeDb.listPendingFederationRelay(dispatchId, 'to_worker')).toHaveLength(0)
})
it('wakes a remote worker waiter when control mail imports', async () => {
const waiting = workerDispatcher.dispatch(checkRequest('wait-for-control', true))
await Promise.resolve()
const imported = await workerDispatcher.dispatch(
importRequest('import-control', 1, 'relay-control')
)
expect(imported).toMatchObject({
ok: true,
result: { acknowledgedThrough: 1, imported: 1 }
})
await expect(waiting).resolves.toMatchObject({
ok: true,
result: {
dispatchId,
count: 1,
messages: [{ id: 'relay-control', subject: 'Continue' }]
}
})
})
it('accepts a repeated import after a lost acknowledgment without duplicating mail', async () => {
const first = await workerDispatcher.dispatch(importRequest('first-import', 1, 'relay-control'))
const repeated = await workerDispatcher.dispatch(
importRequest('repeated-import', 1, 'different-message-id')
)
expect(first).toMatchObject({ ok: true, result: { imported: 1 } })
expect(repeated).toMatchObject({ ok: true, result: { imported: 0 } })
expect(workerDb.getUnreadMessages(`dispatch:${dispatchId}`)).toHaveLength(1)
})
it('does not deliver pending control mail after worker completion', async () => {
vi.spyOn(homeRuntime, 'ensureOrchestrationFederationRelay').mockImplementation(() => {})
await homeDispatcher.dispatch({
id: 'send-stale-control',
authToken: 'coordinator-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'send-stale-control-request',
method: 'orchestration.send',
params: {
from: 'term_coord',
to: `dispatch:${dispatchId}`,
subject: 'Stale follow-up',
body: 'This must not arrive after completion.',
type: 'status'
}
})
const waiting = workerDispatcher.dispatch(checkRequest('wait-before-completion', true, 30))
await Promise.resolve()
const taskId = homeDb.getDispatchContextById(dispatchId)!.task_id
workerDb.enqueueFederationRelay({
dispatchId,
direction: 'to_home',
kind: 'worker_done',
payload: JSON.stringify({
from: `dispatch:${dispatchId}`,
subject: 'Done',
body: 'Completed before the follow-up arrived.',
type: 'worker_done',
priority: 'normal',
threadId: null,
payload: JSON.stringify({
taskId,
dispatchId,
outcome: 'succeeded',
filesModified: []
})
}),
settleRemoteOutcome: 'succeeded'
})
await homeRuntime.syncOrchestrationFederation()
expect(homeDb.getWorkerDispatch(dispatchId)?.state).toBe('succeeded')
expect(workerDb.getUnreadMessages(`dispatch:${dispatchId}`)).toHaveLength(0)
expect(homeDb.listPendingFederationRelay(dispatchId, 'to_worker')).toHaveLength(1)
await expect(
workerDispatcher.dispatch(importRequest('late-direct-import', 1, 'late-control'))
).resolves.toMatchObject({
ok: false,
error: { code: 'dispatch_inactive' }
})
await expect(waiting).resolves.toMatchObject({
ok: true,
result: { count: 0, timedOut: true }
})
})
it('wakes only waiters whose filter matches an imported control message', async () => {
const escalationWaiter = workerDispatcher.dispatch(
checkRequest('wait-escalation', true, 1_000, 'escalation')
)
const statusWaiter = workerDispatcher.dispatch(checkRequest('wait-status', true, 30, 'status'))
await Promise.resolve()
await workerDispatcher.dispatch(
importRequest('import-escalation', 1, 'relay-escalation', 'escalation')
)
await expect(escalationWaiter).resolves.toMatchObject({
ok: true,
result: {
count: 1,
messages: [{ id: 'relay-escalation', type: 'escalation' }]
}
})
await expect(statusWaiter).resolves.toMatchObject({
ok: true,
result: { count: 0, timedOut: true }
})
})
function checkRequest(id: string, wait = false, timeoutMs = 5_000, types?: string): RpcRequest {
return {
id,
authToken: workerToken,
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
method: 'orchestration.check',
params: {
terminal: 'term_worker',
wait,
timeoutMs,
types
}
}
}
function importRequest(
id: string,
sequence: number,
messageId: string,
type = 'status'
): RpcRequest {
return {
id,
authToken: homeToken,
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
method: 'orchestration.federationImport',
params: {
dispatchId,
items: [
{
dispatch_id: dispatchId,
direction: 'to_worker',
sequence,
message_id: messageId,
kind: 'control_message',
payload: JSON.stringify({
from: `run:${runId}`,
subject: 'Continue',
body: 'Run the focused follow-up.',
type,
priority: 'normal',
threadId: null,
payload: null
})
}
]
}
}
}
})
@@ -0,0 +1,207 @@
import { z } from 'zod'
import { ORCHESTRATION_WORKER_READ_SOURCES } from '../../../../shared/orchestration-worker-output'
import type { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationError } from '../../orchestration/orchestration-error'
import type { RemoteDispatchAttachmentRow } from '../../orchestration/types'
import { defineMethod, type RpcMethod } from '../core'
import { OptionalFiniteNumber, requiredString } from '../schemas'
import { readExactWorkerOutput } from './orchestration-worker-output'
const FederationDispatchParams = z.object({
dispatchId: requiredString('Missing Dispatch ID')
})
const FederationReadParams = FederationDispatchParams.extend({
cursor: OptionalFiniteNumber,
limit: OptionalFiniteNumber
})
const FederationOutputReadParams = FederationDispatchParams.extend({
cursor: z.union([z.number().int().nonnegative(), z.string().min(1).max(2_048)]).optional(),
limit: OptionalFiniteNumber,
source: z.enum(ORCHESTRATION_WORKER_READ_SOURCES).optional()
})
export const ORCHESTRATION_FEDERATION_CONTROL_METHODS: RpcMethod[] = [
defineMethod({
name: 'orchestration.federationShow',
params: FederationDispatchParams,
handler: async (params, { runtime, authenticatedCallerFingerprint }) => {
const attachment = requireHomeAttachment(
runtime,
params.dispatchId,
authenticatedCallerFingerprint
)
const observation = await inspectRemoteAttachment(runtime, params.dispatchId)
return {
dispatchId: params.dispatchId,
runtimeEpoch: runtime.getRuntimeId(),
attachment: exposeRemoteAttachment(attachment),
terminal: observation.exact ? observation.terminal : null,
observation: { status: observation.status, exactWorker: observation.exact }
}
}
}),
defineMethod({
name: 'orchestration.federationRead',
params: FederationReadParams,
handler: async (params, { runtime, authenticatedCallerFingerprint }) => {
requireHomeAttachment(runtime, params.dispatchId, authenticatedCallerFingerprint)
const observation = await inspectRemoteAttachment(runtime, params.dispatchId)
if (!observation.exact || !observation.terminal || observation.status !== 'running') {
throw new OrchestrationError(
'worker_identity_changed',
`Remote Dispatch ${params.dispatchId} no longer resolves to its exact process.`
)
}
return {
dispatchId: params.dispatchId,
runtimeEpoch: runtime.getRuntimeId(),
terminal: await runtime.readTerminal(observation.terminal.handle, {
cursor: params.cursor,
limit: params.limit
})
}
}
}),
defineMethod({
name: 'orchestration.federationReadOutput',
params: FederationOutputReadParams,
handler: async (params, { runtime, authenticatedCallerFingerprint }) => {
const attachment = requireHomeAttachment(
runtime,
params.dispatchId,
authenticatedCallerFingerprint
)
const observation = await inspectRemoteAttachment(runtime, params.dispatchId)
if (!observation.exact || !observation.terminal) {
throw new OrchestrationError(
'worker_identity_changed',
`Remote Dispatch ${params.dispatchId} no longer resolves to its exact process.`
)
}
const output = await readExactWorkerOutput({
runtime,
dispatchId: params.dispatchId,
terminalHandle: observation.terminal.handle,
workerState: attachment.state,
terminalStatus: observation.status === 'exited' ? 'exited' : 'running',
attachedAt: attachment.created_at,
source: params.source,
cursor: params.cursor,
limit: params.limit
})
const afterRead = await inspectRemoteAttachment(runtime, params.dispatchId)
if (!afterRead.exact) {
throw new OrchestrationError(
'worker_identity_changed',
`Remote Dispatch ${params.dispatchId} changed process while output was read.`
)
}
return {
dispatchId: params.dispatchId,
runtimeEpoch: runtime.getRuntimeId(),
output
}
}
}),
defineMethod({
name: 'orchestration.federationStop',
params: FederationDispatchParams,
handler: async (params, { runtime, authenticatedCallerFingerprint }) => {
requireHomeAttachment(runtime, params.dispatchId, authenticatedCallerFingerprint)
const db = runtime.getOrchestrationDb()
const begun = db.beginRemoteAttachmentStop(params.dispatchId)
if (['succeeded', 'failed', 'stopped', 'abandoned'].includes(begun.state)) {
return {
dispatchId: params.dispatchId,
state: begun.state,
alreadySettled: true,
processAction: 'none'
}
}
const observation = await inspectRemoteAttachment(runtime, params.dispatchId)
if (!observation.exact || !observation.terminal) {
const attachment = db.markRemoteAttachmentStopUnknown(
params.dispatchId,
`The recorded worker process is ${observation.status}; no terminal was closed.`
)
return {
dispatchId: params.dispatchId,
state: attachment.state,
alreadySettled: false,
processAction: 'none',
lastError: attachment.last_error
}
}
try {
const close = await runtime.closeTerminal(observation.terminal.handle)
const attachment = db.settleRemoteAttachmentStop(params.dispatchId)
return {
dispatchId: params.dispatchId,
state: attachment.state,
alreadySettled: false,
processAction: 'closed_agent_terminal',
close
}
} catch (error) {
const reason = error instanceof Error ? error.message : String(error)
const attachment = db.markRemoteAttachmentStopUnknown(params.dispatchId, reason)
return {
dispatchId: params.dispatchId,
state: attachment.state,
alreadySettled: false,
processAction: 'unknown',
lastError: reason
}
}
}
})
]
function requireHomeAttachment(
runtime: OrcaRuntimeService,
dispatchId: string,
callerFingerprint: string | undefined
): RemoteDispatchAttachmentRow {
const attachment = runtime.getOrchestrationDb().getRemoteDispatchAttachment(dispatchId)
if (!attachment || attachment.home_peer_fingerprint !== callerFingerprint) {
throw new OrchestrationError(
'dispatch_not_found',
`Remote Dispatch ${dispatchId} was not found for this Run home.`
)
}
return attachment
}
async function inspectRemoteAttachment(runtime: OrcaRuntimeService, dispatchId: string) {
const db = runtime.getOrchestrationDb()
const attachment = db.getRemoteDispatchAttachment(dispatchId)
if (!attachment?.terminal_handle) {
return { terminal: null, exact: false, status: 'unattached' as const }
}
const terminal = await runtime.showTerminal(attachment.terminal_handle).catch(() => null)
if (!terminal) {
return { terminal: null, exact: false, status: 'missing' as const }
}
const exact = db.isRemoteAttachmentProcessCurrent({
dispatchId,
paneKey: runtime.getTerminalPaneKey(attachment.terminal_handle),
processIncarnation: runtime.getTerminalProcessIncarnation(attachment.terminal_handle)
})
return {
terminal,
exact,
status: exact
? terminal.connected === false
? ('exited' as const)
: ('running' as const)
: ('identity_changed' as const)
}
}
function exposeRemoteAttachment(attachment: RemoteDispatchAttachmentRow) {
return {
...attachment,
effects: JSON.parse(attachment.effects) as unknown[],
residualResources: JSON.parse(attachment.residual_resources) as unknown[]
}
}
@@ -0,0 +1,39 @@
import { describe, expect, it } from 'vitest'
import {
appendFederationSetupEffect,
appendFederationTerminalEffects,
type FederationEffect
} from './orchestration-federation-effects'
describe('orchestration federation effects', () => {
it('uses exact terminal handles instead of display titles for setup identity', () => {
const effects: FederationEffect[] = []
appendFederationTerminalEffects(
effects,
[
{ handle: 'term_agent', title: 'Codex' },
{ handle: 'term_configured', title: 'Setup' },
{ handle: 'term_setup', title: 'PowerShell' }
],
'term_agent',
'term_setup'
)
appendFederationSetupEffect(effects, {
requested: 'run',
effective: 'run',
source: 'orchestration_default',
hookFound: true,
startupPolicy: 'start-immediately',
state: 'running'
})
expect(effects).toEqual(
expect.arrayContaining([
expect.objectContaining({ id: 'term_configured', role: 'configured_tab' }),
expect.objectContaining({ id: 'term_setup', role: 'setup' }),
expect.objectContaining({ kind: 'setup', terminalId: 'term_setup' })
])
)
})
})
@@ -0,0 +1,79 @@
export type FederationEffect = {
kind: 'worktree' | 'terminal' | 'setup' | 'dispatch_input'
action?: string
role?: string
id?: string
state?: string
tabId?: string
leafId?: string
requested?: string
effective?: string
source?: string
hookFound?: boolean
startupPolicy?: string
terminalId?: string
}
export function appendFederationTerminalEffects(
effects: FederationEffect[],
terminals: { handle: string; title: string | null; tabId?: string; leafId?: string }[],
agentHandle: string,
setupHandle?: string
): void {
for (const terminal of terminals) {
effects.push({
kind: 'terminal',
role:
terminal.handle === agentHandle
? 'agent'
: terminal.handle === setupHandle
? 'setup'
: 'configured_tab',
action: terminal.handle === agentHandle ? 'reused_agent_terminal' : 'created',
id: terminal.handle,
tabId: terminal.tabId,
leafId: terminal.leafId
})
}
}
export function appendFederationSetupEffect(
effects: FederationEffect[],
setup: {
requested: string
effective: string
source: string
hookFound: boolean
startupPolicy: string
state: string
}
): void {
const setupTerminal = effects.find(
(effect) => effect.kind === 'terminal' && effect.role === 'setup'
)
effects.push({
kind: 'setup',
action: setup.requested,
...setup,
terminalId: setupTerminal?.id
})
}
export function isFederationResidualEffect(effect: FederationEffect): boolean {
return Boolean(effect.action?.startsWith('created') || effect.action === 'reused_agent_terminal')
}
export function isFederationEffectUnknown(error: unknown, stage: string): boolean {
const code =
error && typeof error === 'object' && typeof (error as { code?: unknown }).code === 'string'
? (error as { code: string }).code
: ''
if (code === 'operation_unknown') {
return true
}
if (!['worktree_create', 'terminal_create', 'dispatch_input'].includes(stage)) {
return false
}
const message = error instanceof Error ? error.message : String(error)
return /connection|disconnect|timed?\s*out|runtime changed|outcome unknown/i.test(message)
}
@@ -0,0 +1,57 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationDb } from '../../orchestration/db'
import { ORCHESTRATION_METHODS } from './orchestration'
describe('orchestration federated folder placement', () => {
let db: OrchestrationDb | undefined
afterEach(() => db?.close())
it('rejects a new folder workspace before accepting the remote attachment', async () => {
db = new OrchestrationDb(':memory:')
const runtime = new OrcaRuntimeService()
runtime.setOrchestrationDb(db)
vi.spyOn(runtime, 'validateOrchestrationAgentLauncher').mockImplementation(() => {})
vi.spyOn(runtime, 'showRepo').mockResolvedValue({
id: 'folder-repo',
kind: 'folder'
} as never)
const method = ORCHESTRATION_METHODS.find(
(candidate) => candidate.name === 'orchestration.federationAttachStart'
)
if (!method) {
throw new Error('federationAttachStart method is not registered')
}
await expect(
method.handler(
method.params!.parse({
dispatchId: 'ctx_folder',
taskId: 'task_folder',
taskSpec: 'work in folder',
protocolVersion: 1,
worktree: 'new-top-level',
repo: 'folder-repo',
name: 'folder-worker',
agent: 'codex'
}),
{
runtime,
orchestrationMutation: {
callerFingerprint: 'home_peer',
requestId: 'request_folder',
method: 'orchestration.federationAttachStart',
payloadHash: 'folder_payload'
}
}
)
).rejects.toMatchObject({
code: 'invalid_argument',
message:
'Folder projects cannot create orchestration worktrees; use an exact existing folder workspace.'
})
expect(db.getRemoteDispatchAttachment('ctx_folder')).toBeUndefined()
expect(db.getMutationReceipt('home_peer', 'request_folder')).toBeUndefined()
})
})
@@ -0,0 +1,10 @@
import type { RpcMethod } from '../core'
import { ORCHESTRATION_FEDERATION_CONTROL_METHODS } from './orchestration-federation-control'
import { ORCHESTRATION_FEDERATION_RELAY_METHODS } from './orchestration-federation-relay'
import { ORCHESTRATION_FEDERATION_ATTACH_METHODS } from './orchestration-federation'
export const ORCHESTRATION_FEDERATION_METHODS: RpcMethod[] = [
...ORCHESTRATION_FEDERATION_ATTACH_METHODS,
...ORCHESTRATION_FEDERATION_RELAY_METHODS,
...ORCHESTRATION_FEDERATION_CONTROL_METHODS
]
@@ -0,0 +1,312 @@
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { RuntimeRpcResponse } from '../../../../shared/runtime-rpc-envelope'
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../../shared/protocol-version'
import { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationDb } from '../../orchestration/db'
import type { OrchestrationEnvironmentTransport } from '../../orchestration/environment-transport'
import type { RpcRequest } from '../core'
import { RpcDispatcher } from '../dispatcher'
import { ORCHESTRATION_METHODS } from './orchestration'
describe('orchestration federated worker output', () => {
const databases: OrchestrationDb[] = []
let homeDb: OrchestrationDb
let workerDb: OrchestrationDb
let homeRuntime: OrcaRuntimeService
let workerRuntime: OrcaRuntimeService
let homeDispatcher: RpcDispatcher
let workerDispatcher: RpcDispatcher
let workerSupportsStructuredRead: boolean
beforeEach(() => {
homeDb = new OrchestrationDb(':memory:')
workerDb = new OrchestrationDb(':memory:')
databases.push(homeDb, workerDb)
workerRuntime = new OrcaRuntimeService()
workerRuntime.setOrchestrationDb(workerDb)
workerDispatcher = new RpcDispatcher({
runtime: workerRuntime,
methods: ORCHESTRATION_METHODS
})
workerSupportsStructuredRead = true
const transport: OrchestrationEnvironmentTransport = {
resolve: () => ({
environmentId: 'environment_windows',
name: 'windows',
peerFingerprint: 'windows_peer_fingerprint'
}),
call: async (_selector, method, params, _timeoutMs, envelope) => {
if (method === 'status.get') {
return {
id: 'status',
ok: true,
result: workerRuntime.getStatus(),
_meta: { runtimeId: workerRuntime.getRuntimeId() }
}
}
if (method === 'orchestration.federationReadOutput' && !workerSupportsStructuredRead) {
return {
id: `remote_${method}`,
ok: false,
error: { code: 'method_not_found', message: `Unknown method: ${method}` }
}
}
return (await workerDispatcher.dispatch({
id: `remote_${method}`,
authToken: 'run-home-device-token',
method,
params,
orchestrationContractVersion: envelope?.orchestrationContractVersion,
orchestrationRequestId: envelope?.orchestrationRequestId,
orchestrationCapability: envelope?.orchestrationCapability
})) as RuntimeRpcResponse<unknown>
}
}
homeRuntime = new OrcaRuntimeService(null, undefined, {
orchestrationEnvironmentTransport: transport
})
homeRuntime.setOrchestrationDb(homeDb)
homeDispatcher = new RpcDispatcher({
runtime: homeRuntime,
methods: ORCHESTRATION_METHODS
})
vi.spyOn(homeRuntime, 'getTerminalPaneKey').mockImplementation((handle) =>
handle === 'term_coord' ? 'tab_coord:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa' : null
)
configureWorkerRuntime(workerRuntime)
})
afterEach(() => {
homeRuntime.stopOrchestrationFederationRelay()
for (const db of databases.splice(0)) {
db.close()
}
})
function createHomeTask() {
const run = homeDb.createRun({
objective: 'Mac to Windows output',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
})
return homeDb.createTask({ spec: 'Read Windows worker output', runId: run.id })
}
function startRequest(taskId: string): RpcRequest {
return {
id: 'rpc_worker_start',
authToken: 'coordinator-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'request_windows_worker',
method: 'orchestration.workerStart',
params: {
task: taskId,
from: 'term_coord',
on: 'windows',
worktree: 'new-top-level',
repo: 'id:windows-repo',
name: 'windows-output',
agent: 'codex'
}
}
}
function configureWorkerRuntime(runtime: OrcaRuntimeService): void {
vi.spyOn(runtime, 'validateOrchestrationAgentLauncher').mockImplementation(() => {})
vi.spyOn(runtime, 'showRepo').mockResolvedValue({
id: 'windows-repo',
kind: 'git'
} as never)
vi.spyOn(runtime, 'createManagedWorktree').mockResolvedValue({
worktree: { id: 'repo::windows-worktree', repoId: 'repo' },
startupTerminal: { spawned: true, handle: 'term_windows_worker' },
setupReceipt: {
requested: 'run',
hookFound: false,
startupPolicy: 'start-immediately',
state: 'not_configured'
}
} as never)
vi.spyOn(runtime, 'listTerminals').mockResolvedValue({
terminals: [{ handle: 'term_windows_worker', title: 'Codex' }],
totalCount: 1,
truncated: false
} as never)
vi.spyOn(runtime, 'waitForTerminal').mockResolvedValue({
handle: 'term_windows_worker',
condition: 'tui-idle',
satisfied: true,
status: 'running',
exitCode: null
})
vi.spyOn(runtime, 'getTerminalPaneKey').mockReturnValue(
'tab_worker:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
)
vi.spyOn(runtime, 'getTerminalProcessIncarnation').mockReturnValue('windows_runtime:pty:1')
vi.spyOn(runtime, 'getTerminalOrchestrationCliCommand').mockReturnValue('orca')
vi.spyOn(runtime, 'sendTerminalAgentPrompt').mockResolvedValue({
handle: 'term_windows_worker',
accepted: true,
bytesWritten: 1
})
vi.spyOn(runtime, 'showTerminal').mockResolvedValue({
handle: 'term_windows_worker',
worktreeId: 'repo::windows-worktree',
status: 'running'
} as never)
vi.spyOn(runtime, 'readTerminal').mockResolvedValue({
handle: 'term_windows_worker',
status: 'running',
tail: ['remote output'],
truncated: false,
nextCursor: '1'
})
}
async function startRemoteWorker(): Promise<string> {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
return homeDb.getDispatchContext(task.id)!.id
}
it('routes show and read by Dispatch without repeating the worker server', async () => {
const dispatchId = await startRemoteWorker()
const shown = await homeDispatcher.dispatch({
id: 'rpc_remote_show',
authToken: 'coordinator-token',
method: 'orchestration.workerShow',
params: { dispatch: dispatchId }
})
const read = await homeDispatcher.dispatch({
id: 'rpc_remote_read',
authToken: 'coordinator-token',
method: 'orchestration.workerRead',
params: { dispatch: dispatchId, limit: 20 }
})
expect(shown).toMatchObject({
ok: true,
result: {
server: { environmentId: 'environment_windows', name: 'windows' },
observation: { status: 'running', exactWorker: true },
terminal: { handle: 'term_windows_worker' }
}
})
expect(read).toMatchObject({
ok: true,
result: {
source: 'terminal',
fallbackReason: 'session_not_reported',
server: { environmentId: 'environment_windows', name: 'windows' },
terminal: { tail: ['remote output'] }
}
})
})
it('keeps an opaque terminal cursor across mixed server versions', async () => {
const dispatchId = await startRemoteWorker()
workerSupportsStructuredRead = false
const automatic = await homeDispatcher.dispatch({
id: 'rpc_remote_legacy_read',
authToken: 'coordinator-token',
method: 'orchestration.workerRead',
params: { dispatch: dispatchId }
})
const cursor = (automatic as { result: { cursor: string } }).result.cursor
const continued = await homeDispatcher.dispatch({
id: 'rpc_remote_legacy_continue',
authToken: 'coordinator-token',
method: 'orchestration.workerRead',
params: { dispatch: dispatchId, cursor }
})
const required = await homeDispatcher.dispatch({
id: 'rpc_remote_legacy_transcript',
authToken: 'coordinator-token',
method: 'orchestration.workerRead',
params: { dispatch: dispatchId, source: 'transcript' }
})
expect(automatic).toMatchObject({
ok: true,
result: {
source: 'terminal',
fallbackReason: 'remote_capability_unavailable',
terminal: { tail: ['remote output'] }
}
})
expect(cursor).toMatch(/^owr1_/)
expect(continued).toMatchObject({
ok: true,
result: {
source: 'terminal',
fallbackReason: 'remote_capability_unavailable'
}
})
expect((continued as { result: { cursor: string } }).result.cursor).toMatch(/^owr1_/)
expect(required).toMatchObject({
ok: false,
error: {
code: 'transcript_required',
data: { reason: 'remote_capability_unavailable' }
}
})
})
it('reads the exact transcript on the worker server without leaking its path home', async () => {
const dispatchId = await startRemoteWorker()
const directory = await mkdtemp(join(tmpdir(), 'orca-federated-worker-output-'))
const transcriptPath = join(directory, 'windows-session.jsonl')
await writeFile(
transcriptPath,
`${JSON.stringify({
type: 'event_msg',
payload: { id: 'remote-message', type: 'agent_message', message: 'Windows result' }
})}\n`
)
vi.spyOn(workerRuntime, 'getExactWorkerProviderSession').mockReturnValue({
paneKey: 'tab_worker:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb',
processIncarnation: 'windows_runtime:pty:1',
agent: 'codex',
providerSession: {
key: 'session_id',
id: 'windows-session',
transcriptPath
},
observedAt: Date.now()
})
try {
const response = await homeDispatcher.dispatch({
id: 'rpc_remote_transcript_read',
authToken: 'coordinator-token',
method: 'orchestration.workerRead',
params: { dispatch: dispatchId }
})
expect(response).toMatchObject({
ok: true,
result: {
source: 'transcript',
provider: 'codex',
server: { environmentId: 'environment_windows' },
transcript: {
messages: [
{
id: 'remote-message',
blocks: [{ type: 'text', text: 'Windows result' }]
}
]
}
}
})
expect(JSON.stringify(response)).not.toContain(transcriptPath)
} finally {
await rm(directory, { recursive: true, force: true })
}
})
})
@@ -0,0 +1,179 @@
import { z } from 'zod'
import { ORCHESTRATION_FEDERATION_CONTROL_MAIL_PROTOCOL_VERSION } from '../../../../shared/protocol-version'
import { importFederatedControlMessage } from '../../orchestration/federation-control-message'
import { OrchestrationError } from '../../orchestration/orchestration-error'
import { defineMethod, type RpcMethod } from '../core'
import { OptionalFiniteNumber, requiredString } from '../schemas'
const FederationPullParams = z.object({
dispatchId: requiredString('Missing Dispatch ID'),
afterSequence: OptionalFiniteNumber,
limit: OptionalFiniteNumber
})
const FederationAckParams = z.object({
dispatchId: requiredString('Missing Dispatch ID'),
throughSequence: z.number().int().nonnegative()
})
const FederationImportParams = z.object({
dispatchId: requiredString('Missing Dispatch ID'),
items: z.array(
z.object({
dispatch_id: requiredString('Missing item Dispatch ID'),
direction: z.literal('to_worker'),
sequence: z.number().int().positive(),
message_id: requiredString('Missing relay message ID'),
kind: requiredString('Missing relay kind'),
payload: requiredString('Missing relay payload')
})
)
})
export const ORCHESTRATION_FEDERATION_RELAY_METHODS: RpcMethod[] = [
defineMethod({
name: 'orchestration.federationPull',
params: FederationPullParams,
handler: (params, { runtime, authenticatedCallerFingerprint }) => {
requireHomeAttachment(runtime, params.dispatchId, authenticatedCallerFingerprint)
return {
dispatchId: params.dispatchId,
runtimeEpoch: runtime.getRuntimeId(),
items: runtime.getOrchestrationDb().listFederationRelay({
dispatchId: params.dispatchId,
direction: 'to_home',
afterSequence: params.afterSequence ?? 0,
limit: params.limit
})
}
}
}),
defineMethod({
name: 'orchestration.federationAck',
params: FederationAckParams,
handler: (params, { runtime, authenticatedCallerFingerprint }) => {
requireHomeAttachment(runtime, params.dispatchId, authenticatedCallerFingerprint)
runtime.getOrchestrationDb().acknowledgeFederationRelay({
dispatchId: params.dispatchId,
direction: 'to_home',
throughSequence: params.throughSequence
})
return { dispatchId: params.dispatchId, acknowledgedThrough: params.throughSequence }
}
}),
defineMethod({
name: 'orchestration.federationImport',
params: FederationImportParams,
handler: (params, { runtime, authenticatedCallerFingerprint }) => {
const db = runtime.getOrchestrationDb()
const attachment = requireHomeAttachment(
runtime,
params.dispatchId,
authenticatedCallerFingerprint
)
let cursor = attachment.to_worker_imported_sequence
let imported = 0
for (const item of params.items) {
if (item.dispatch_id !== params.dispatchId || item.sequence > cursor + 1) {
throw new OrchestrationError(
'operation_unknown',
`Home relay for ${params.dispatchId} is not contiguous after sequence ${cursor}.`
)
}
if (item.sequence <= cursor) {
continue
}
const currentAttachment = requireHomeAttachment(
runtime,
params.dispatchId,
authenticatedCallerFingerprint
)
if (currentAttachment.state !== 'ready') {
throw new OrchestrationError(
'dispatch_inactive',
`Remote Dispatch ${params.dispatchId} is not active.`
)
}
if (item.kind === 'reply') {
const reply = parseFederatedReply(item.payload)
db.answerRemoteQuestion({
messageId: reply.questionId,
dispatchId: params.dispatchId,
answerMessageId: reply.answerMessageId,
body: reply.body
})
runtime.notifyMessageArrived(`dispatch:${params.dispatchId}`, 'status')
} else if (item.kind === 'control_message') {
if (
currentAttachment.protocol_version <
ORCHESTRATION_FEDERATION_CONTROL_MAIL_PROTOCOL_VERSION
) {
throw new OrchestrationError(
'capability_unsupported',
`Remote Dispatch ${params.dispatchId} does not support coordinator control mail.`
)
}
const controlMessage = importFederatedControlMessage(db, {
dispatchId: params.dispatchId,
messageId: item.message_id,
payload: item.payload
})
imported += controlMessage.imported ? 1 : 0
if (controlMessage.imported) {
runtime.notifyMessageArrived(`dispatch:${params.dispatchId}`, controlMessage.type)
}
} else {
throw new OrchestrationError(
'invalid_argument',
`Federated worker relay kind ${item.kind} is not supported.`
)
}
cursor = item.sequence
db.setRemoteWorkerImportSequence(params.dispatchId, cursor)
}
return { dispatchId: params.dispatchId, acknowledgedThrough: cursor, imported }
}
})
]
function requireHomeAttachment(
runtime: Parameters<RpcMethod['handler']>[1]['runtime'],
dispatchId: string,
callerFingerprint: string | undefined
) {
const attachment = runtime.getOrchestrationDb().getRemoteDispatchAttachment(dispatchId)
if (!attachment || attachment.home_peer_fingerprint !== callerFingerprint) {
throw new OrchestrationError(
'dispatch_not_found',
`Remote Dispatch ${dispatchId} was not found for this Run home.`
)
}
return attachment
}
function parseFederatedReply(payload: string): {
questionId: string
answerMessageId: string
body: string
} {
let parsed: unknown
try {
parsed = JSON.parse(payload)
} catch {
throw new OrchestrationError('invalid_argument', 'Federated reply payload is invalid JSON.')
}
const reply = parsed as Record<string, unknown> | null
if (
!reply ||
typeof reply.questionId !== 'string' ||
typeof reply.answerMessageId !== 'string' ||
typeof reply.body !== 'string'
) {
throw new OrchestrationError('invalid_argument', 'Federated reply payload is incomplete.')
}
return {
questionId: reply.questionId,
answerMessageId: reply.answerMessageId,
body: reply.body
}
}
@@ -0,0 +1,196 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationDb } from '../../orchestration/db'
import { ORCHESTRATION_METHODS } from './orchestration'
import { monitorFederatedSetup } from './orchestration-federation-setup'
describe('orchestration federated setup evidence', () => {
const databases: OrchestrationDb[] = []
const runtimes: OrcaRuntimeService[] = []
afterEach(() => {
for (const runtime of runtimes.splice(0)) {
runtime.stopOrchestrationFederationRelay()
}
for (const db of databases.splice(0)) {
db.close()
}
})
function createRuntime(): { db: OrchestrationDb; runtime: OrcaRuntimeService } {
const db = new OrchestrationDb(':memory:')
const runtime = new OrcaRuntimeService()
runtime.setOrchestrationDb(db)
databases.push(db)
runtimes.push(runtime)
return { db, runtime }
}
it('records remote setup evidence once without changing attachment lifecycle', async () => {
const { db, runtime } = createRuntime()
const dispatchId = 'ctx_remote_setup'
const effects = [
{
kind: 'terminal' as const,
role: 'setup',
action: 'created',
id: 'term_remote_setup'
},
{
kind: 'setup' as const,
action: 'run',
state: 'running'
},
{
kind: 'dispatch_input' as const,
role: 'agent',
id: 'term_remote_worker',
state: 'accepted'
}
]
db.createRemoteDispatchAttachment({
dispatchId,
taskId: 'task_remote_setup',
homePeerFingerprint: 'home_peer',
protocolVersion: 1,
runtimeEpoch: runtime.getRuntimeId(),
mutationReceipt: {
callerFingerprint: 'home_peer',
requestId: 'request_remote_setup',
method: 'orchestration.federationAttachStart',
payloadHash: 'remote_setup_payload'
}
})
db.prepareRemoteAttachmentAuthority({
dispatchId,
paneKey: 'tab_worker:leaf_worker',
processIncarnation: 'worker_epoch:pty:1',
worktreeId: 'repo::remote-worktree',
terminalHandle: 'term_remote_worker',
setupState: 'running',
effects
})
db.markRemoteAttachmentReady(dispatchId)
vi.spyOn(runtime, 'waitForSetupTerminalCompletion').mockResolvedValue({ exitCode: 1 })
const monitorArgs = {
runtime,
db,
dispatchId,
worktreeId: 'repo::remote-worktree',
terminalHandle: 'term_remote_worker',
setup: {
requested: 'run' as const,
effective: 'run' as const,
source: 'orchestration_default',
hookFound: true,
startupPolicy: 'start-immediately' as const,
state: 'running' as const
},
effects
}
monitorFederatedSetup(monitorArgs)
monitorFederatedSetup(monitorArgs)
await vi.waitFor(() =>
expect(db.getRemoteDispatchAttachment(dispatchId)).toMatchObject({
state: 'ready',
stage: 'input_accepted',
setup_state: 'failed'
})
)
expect(
db.listFederationRelay({ dispatchId, direction: 'to_home', afterSequence: 0 })
).toHaveLength(1)
expect(JSON.parse(db.getRemoteDispatchAttachment(dispatchId)?.effects ?? '[]')).toEqual(
expect.arrayContaining([
expect.objectContaining({ kind: 'dispatch_input', state: 'accepted' })
])
)
})
it('refreshes home setup evidence without changing a ready Dispatch lifecycle', async () => {
const { db, runtime } = createRuntime()
const run = db.createRun({
objective: 'Observe remote setup',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:leaf_coord'
})
const task = db.createTask({ spec: 'remote setup', runId: run.id })
const started = db.createStartingWorkerDispatch({
taskId: task.id,
startOptions: {},
runtimeEpoch: runtime.getRuntimeId(),
federation: {
environmentId: 'environment_windows',
environmentName: 'windows',
peerFingerprint: 'windows_peer',
protocolVersion: 1
}
})
db.recordWorkerStage({
dispatchId: started.dispatch.id,
stage: 'terminal_readying',
setupState: 'running',
effects: [
{ kind: 'setup', action: 'run', state: 'running' },
{
kind: 'dispatch_input',
role: 'agent',
id: 'term_remote_worker',
state: 'accepted'
}
]
})
db.markWorkerDispatchReady(started.dispatch.id)
vi.spyOn(runtime, 'resolveOrchestrationWorkerServer').mockReturnValue({
environmentId: 'environment_windows',
name: 'windows',
peerFingerprint: 'windows_peer'
})
vi.spyOn(runtime, 'callOrchestrationWorkerServer').mockResolvedValue({
runtimeEpoch: 'windows_epoch',
attachment: {
state: 'ready',
stage: 'input_accepted',
last_error: null,
worktree_id: 'repo::remote-worktree',
terminal_handle: 'term_remote_worker',
setup_state: 'failed',
effects: [
{ kind: 'setup', action: 'run', state: 'failed' },
{
kind: 'dispatch_input',
role: 'agent',
id: 'term_remote_worker',
state: 'accepted'
}
],
residualResources: []
},
terminal: { handle: 'term_remote_worker', connected: true },
observation: { status: 'running', exactWorker: true }
})
const workerShow = ORCHESTRATION_METHODS.find(
(method) => method.name === 'orchestration.workerShow'
)
if (!workerShow) {
throw new Error('workerShow method is not registered')
}
await expect(
workerShow.handler(workerShow.params!.parse({ dispatch: started.dispatch.id }), { runtime })
).resolves.toMatchObject({
worker: {
state: 'ready',
stage: 'input_accepted',
setup_state: 'failed',
effects: expect.arrayContaining([
expect.objectContaining({ kind: 'setup', state: 'failed' }),
expect.objectContaining({ kind: 'dispatch_input', state: 'accepted' })
])
}
})
expect(db.getTask(task.id)?.status).toBe('dispatched')
})
})
@@ -0,0 +1,99 @@
import type { OrcaRuntimeService } from '../../orca-runtime'
import type { OrchestrationDb } from '../../orchestration/db'
import { applyWaitForSetupOutcome, type WorkerSetupReceipt } from './orchestration-worker-topology'
import {
isFederationResidualEffect,
type FederationEffect
} from './orchestration-federation-effects'
type FederationSetupStageArgs = {
db: OrchestrationDb
dispatchId: string
worktreeId: string
terminalHandle: string
setup: WorkerSetupReceipt
effects: FederationEffect[]
}
function recordStage(args: FederationSetupStageArgs, stage: string): void {
args.db.recordRemoteAttachmentStage({
dispatchId: args.dispatchId,
stage,
worktreeId: args.worktreeId,
terminalHandle: args.terminalHandle,
setupState: args.setup.state,
effects: args.effects,
residualResources: args.effects.filter(isFederationResidualEffect)
})
}
export function persistFederatedReadinessStage(args: FederationSetupStageArgs): void {
recordStage(args, 'terminal_readying')
}
export function persistFederatedSetupSpawnFailure(args: FederationSetupStageArgs): boolean {
if (args.setup.startupPolicy !== 'wait-for-setup' || args.setup.state !== 'spawn_failed') {
return false
}
recordStage(args, 'setup_start')
return true
}
export function persistFederatedSetupWaitOutcome(
args: FederationSetupStageArgs & { wait: { satisfied: boolean; status: string } }
): void {
applyWaitForSetupOutcome(args.setup, args.effects, args.wait)
if (args.setup.startupPolicy === 'wait-for-setup') {
recordStage(args, args.setup.state === 'failed' ? 'setup_failed' : 'setup_settled')
}
}
export function monitorFederatedSetup(
args: FederationSetupStageArgs & { runtime: OrcaRuntimeService }
): void {
const setupTerminal = args.effects.find(
(effect) => effect.kind === 'terminal' && effect.role === 'setup' && effect.id
)
if (
!setupTerminal?.id ||
args.setup.startupPolicy !== 'start-immediately' ||
args.setup.state !== 'running'
) {
return
}
void args.runtime
.waitForSetupTerminalCompletion(setupTerminal.id)
.then((completion) => {
const setupState = completion.exitCode === 0 ? 'succeeded' : 'failed'
const effects = args.effects.map((effect) =>
effect.kind === 'setup' ? { ...effect, state: setupState } : effect
)
const evidence = args.db.updateRemoteAttachmentSetupEvidence({
dispatchId: args.dispatchId,
setupState,
effects
})
if (!evidence.changed) {
return
}
args.db.enqueueFederationRelay({
dispatchId: args.dispatchId,
direction: 'to_home',
kind: 'status',
payload: JSON.stringify({
from: `dispatch:${args.dispatchId}`,
subject: `Setup ${setupState} for worker ${args.dispatchId}`,
body: '',
type: 'status',
priority: setupState === 'failed' ? 'high' : 'normal',
threadId: null,
payload: JSON.stringify({
dispatchId: args.dispatchId,
setupState,
terminalHandle: setupTerminal.id
})
})
})
})
.catch(() => undefined)
}
@@ -0,0 +1,32 @@
import type { OrchestrationDb } from '../../orchestration/db'
import { isFederationEffectUnknown } from './orchestration-federation-effects'
import type { WorkerSetupReceipt } from './orchestration-worker-topology'
export function failFederatedAttachmentWithReceipt(args: {
db: OrchestrationDb
dispatchId: string
runtimeEpoch: string
failedStage: string
error: unknown
setup: WorkerSetupReceipt
}): unknown {
const reason = args.error instanceof Error ? args.error.message : String(args.error)
const unknown = isFederationEffectUnknown(args.error, args.failedStage)
const attachment = args.db.failRemoteAttachment(
args.dispatchId,
args.failedStage,
reason,
unknown
)
return {
dispatchId: args.dispatchId,
state: attachment.state === 'start_unknown' ? 'outcome_unknown' : attachment.state,
stage: attachment.stage,
runtimeEpoch: args.runtimeEpoch,
failedStage: args.failedStage,
lastError: reason,
setup: args.setup,
effects: JSON.parse(attachment.effects) as unknown[],
residualResources: JSON.parse(attachment.residual_resources) as unknown[]
}
}
@@ -0,0 +1,21 @@
import { z } from 'zod'
import { OptionalFiniteNumber, OptionalString, requiredString } from '../schemas'
export const FederationAttachStartParams = z.object({
dispatchId: requiredString('Missing Dispatch ID'),
taskId: requiredString('Missing Task ID'),
taskSpec: requiredString('Missing Task spec'),
protocolVersion: z.union([z.literal(1), z.literal(2)]),
worktree: requiredString('Missing remote worktree selector'),
name: OptionalString,
repo: OptionalString,
baseBranch: OptionalString,
displayName: OptionalString,
comment: OptionalString,
setup: z.enum(['run', 'skip', 'inherit']).optional(),
setupSource: z.enum(['explicit_request', 'orchestration_default']).optional(),
terminal: OptionalString,
agent: OptionalString,
timeoutMs: OptionalFiniteNumber,
devMode: z.boolean().optional()
})
@@ -0,0 +1,862 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { RuntimeRpcResponse } from '../../../../shared/runtime-rpc-envelope'
import {
ORCHESTRATION_CONTRACT_VERSION,
ORCHESTRATION_FEDERATION_CONTROL_MAIL_RUNTIME_CAPABILITY
} from '../../../../shared/protocol-version'
import { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationDb } from '../../orchestration/db'
import type { OrchestrationEnvironmentTransport } from '../../orchestration/environment-transport'
import { RpcDispatcher } from '../dispatcher'
import type { RpcRequest } from '../core'
import { ORCHESTRATION_METHODS } from './orchestration'
describe('orchestration federation', () => {
const databases: OrchestrationDb[] = []
let homeDb: OrchestrationDb
let workerDb: OrchestrationDb
let homeRuntime: OrcaRuntimeService
let workerRuntime: OrcaRuntimeService
let homeDispatcher: RpcDispatcher
let workerDispatcher: RpcDispatcher
let workerCapabilities: string[]
let workerPeerFingerprint: string
let loseNextAckResponse: boolean
beforeEach(() => {
homeDb = new OrchestrationDb(':memory:')
workerDb = new OrchestrationDb(':memory:')
databases.push(homeDb, workerDb)
workerRuntime = new OrcaRuntimeService()
workerRuntime.setOrchestrationDb(workerDb)
workerDispatcher = new RpcDispatcher({
runtime: workerRuntime,
methods: ORCHESTRATION_METHODS
})
workerCapabilities = [...(workerRuntime.getStatus().capabilities ?? [])]
workerPeerFingerprint = 'windows_peer_fingerprint'
loseNextAckResponse = false
const transport: OrchestrationEnvironmentTransport = {
resolve: () => ({
environmentId: 'environment_windows',
name: 'windows',
peerFingerprint: workerPeerFingerprint
}),
call: async (_selector, method, params, _timeoutMs, envelope) => {
if (method === 'status.get') {
return {
id: 'status',
ok: true,
result: { ...workerRuntime.getStatus(), capabilities: workerCapabilities },
_meta: { runtimeId: workerRuntime.getRuntimeId() }
}
}
const response = (await workerDispatcher.dispatch({
id: `remote_${method}`,
authToken: 'run-home-device-token',
method,
params,
orchestrationContractVersion: envelope?.orchestrationContractVersion,
orchestrationRequestId: envelope?.orchestrationRequestId,
orchestrationCapability: envelope?.orchestrationCapability
})) as RuntimeRpcResponse<unknown>
if (method === 'orchestration.federationAck' && loseNextAckResponse) {
loseNextAckResponse = false
throw new Error('connection lost after acknowledgment')
}
return response
}
}
homeRuntime = new OrcaRuntimeService(null, undefined, {
orchestrationEnvironmentTransport: transport
})
homeRuntime.setOrchestrationDb(homeDb)
homeDispatcher = new RpcDispatcher({
runtime: homeRuntime,
methods: ORCHESTRATION_METHODS
})
vi.spyOn(homeRuntime, 'getTerminalPaneKey').mockImplementation((handle) =>
handle === 'term_coord' ? 'tab_coord:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa' : null
)
configureWorkerRuntime(workerRuntime)
})
afterEach(() => {
homeRuntime.stopOrchestrationFederationRelay()
for (const db of databases.splice(0)) {
db.close()
}
})
function createHomeTask() {
const run = homeDb.createRun({
objective: 'Mac to Windows',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
})
return homeDb.createTask({ spec: 'Audit Windows behavior', runId: run.id })
}
function startRequest(taskId: string, overrides: Record<string, unknown> = {}): RpcRequest {
return {
id: 'rpc_worker_start',
authToken: 'coordinator-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'request_windows_worker',
method: 'orchestration.workerStart',
params: {
task: taskId,
from: 'term_coord',
on: 'windows',
worktree: 'new-top-level',
repo: 'id:windows-repo',
name: 'windows-audit',
agent: 'codex',
...overrides
}
}
}
function configureWorkerRuntime(runtime: OrcaRuntimeService): void {
vi.spyOn(runtime, 'validateOrchestrationAgentLauncher').mockImplementation(() => {})
vi.spyOn(runtime, 'showRepo').mockResolvedValue({
id: 'windows-repo',
kind: 'git'
} as never)
vi.spyOn(runtime, 'createManagedWorktree').mockResolvedValue({
worktree: { id: 'repo::windows-worktree', repoId: 'repo' },
startupTerminal: { spawned: true, handle: 'term_windows_worker' },
setupReceipt: {
requested: 'run',
hookFound: true,
startupPolicy: 'start-immediately',
state: 'running'
}
} as never)
vi.spyOn(runtime, 'listTerminals').mockResolvedValue({
terminals: [
{ handle: 'term_windows_worker', title: 'Codex' },
{ handle: 'term_windows_setup', title: 'Setup' }
],
totalCount: 2,
truncated: false
} as never)
vi.spyOn(runtime, 'waitForTerminal').mockResolvedValue({
handle: 'term_windows_worker',
condition: 'tui-idle',
satisfied: true,
status: 'running',
exitCode: null
})
vi.spyOn(runtime, 'getTerminalPaneKey').mockReturnValue(
'tab_worker:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
)
vi.spyOn(runtime, 'getTerminalProcessIncarnation').mockReturnValue('windows_runtime:pty:1')
vi.spyOn(runtime, 'getTerminalOrchestrationCliCommand').mockReturnValue('orca')
vi.spyOn(runtime, 'sendTerminalAgentPrompt').mockResolvedValue({
handle: 'term_windows_worker',
accepted: true,
bytesWritten: 1
})
vi.spyOn(runtime, 'showTerminal').mockResolvedValue({
handle: 'term_windows_worker',
worktreeId: 'repo::windows-worktree',
status: 'running'
} as never)
vi.spyOn(runtime, 'readTerminal').mockResolvedValue({
handle: 'term_windows_worker',
status: 'running',
entries: [{ cursor: 1, text: 'remote output' }],
nextCursor: '1',
limited: false
} as never)
vi.spyOn(runtime, 'closeTerminal').mockResolvedValue({
handle: 'term_windows_worker',
closed: true
} as never)
}
function restartWorkerRuntime(): void {
workerRuntime = new OrcaRuntimeService()
workerRuntime.setOrchestrationDb(workerDb)
configureWorkerRuntime(workerRuntime)
workerDispatcher = new RpcDispatcher({
runtime: workerRuntime,
methods: ORCHESTRATION_METHODS
})
workerCapabilities = [...(workerRuntime.getStatus().capabilities ?? [])]
}
it('starts a remote worker while keeping authoritative Task state at home', async () => {
const task = createHomeTask()
const response = await homeDispatcher.dispatch(startRequest(task.id))
expect(response).toMatchObject({
ok: true,
result: {
taskId: task.id,
state: 'ready',
server: { environmentId: 'environment_windows', name: 'windows' },
setup: { source: 'orchestration_default' },
mutation: { requestId: 'request_windows_worker' }
}
})
const dispatch = homeDb.getDispatchContext(task.id)!
expect(homeDb.getTask(task.id)?.status).toBe('dispatched')
expect(homeDb.getFederatedDispatch(dispatch.id)).toMatchObject({
environment_id: 'environment_windows',
environment_name: 'windows',
peer_fingerprint: 'windows_peer_fingerprint',
remote_worktree_id: 'repo::windows-worktree',
remote_terminal_handle: 'term_windows_worker'
})
expect(workerDb.getRemoteDispatchAttachment(dispatch.id)).toMatchObject({
task_id: task.id,
protocol_version: 2,
state: 'ready',
worktree_id: 'repo::windows-worktree',
terminal_handle: 'term_windows_worker'
})
expect(JSON.parse(workerDb.getRemoteDispatchAttachment(dispatch.id)?.effects ?? '[]')).toEqual(
expect.arrayContaining([
expect.objectContaining({ kind: 'dispatch_input', state: 'accepted' })
])
)
expect(workerDb.listTasks()).toHaveLength(0)
expect(workerRuntime.sendTerminalAgentPrompt).toHaveBeenCalledWith(
'term_windows_worker',
expect.stringContaining(`Your task ID is: ${task.id}`)
)
})
it('preserves wait-for-setup gating on the connected worker server', async () => {
vi.mocked(workerRuntime.createManagedWorktree).mockResolvedValueOnce({
worktree: { id: 'repo::windows-worktree', repoId: 'repo' },
startupTerminal: { spawned: true, handle: 'term_windows_worker' },
setupReceipt: {
requested: 'run',
hookFound: true,
startupPolicy: 'wait-for-setup',
state: 'running'
}
} as never)
const task = createHomeTask()
const response = await homeDispatcher.dispatch(startRequest(task.id, { setup: 'run' }))
expect(response).toMatchObject({
ok: true,
result: {
state: 'ready',
setup: { startupPolicy: 'wait-for-setup', state: 'succeeded' },
effects: expect.arrayContaining([
expect.objectContaining({ kind: 'setup', state: 'succeeded' }),
expect.objectContaining({ kind: 'dispatch_input', state: 'accepted' })
])
}
})
expect(response).toHaveProperty('result.setup.source', 'explicit_request')
expect(workerRuntime.sendTerminalAgentPrompt).toHaveBeenCalledOnce()
})
it('fails before remote task input when wait-for-setup fails', async () => {
vi.mocked(workerRuntime.createManagedWorktree).mockResolvedValueOnce({
worktree: { id: 'repo::windows-worktree', repoId: 'repo' },
startupTerminal: { spawned: true, handle: 'term_windows_worker' },
setupReceipt: {
requested: 'run',
hookFound: true,
startupPolicy: 'wait-for-setup',
state: 'running'
}
} as never)
vi.mocked(workerRuntime.waitForTerminal).mockResolvedValueOnce({
handle: 'term_windows_worker',
condition: 'tui-idle',
satisfied: false,
status: 'exited',
exitCode: 1
})
const task = createHomeTask()
const response = await homeDispatcher.dispatch(startRequest(task.id))
expect(response).toMatchObject({
ok: true,
result: {
state: 'failed',
failedStage: 'setup_wait',
setup: { state: 'failed' },
effects: expect.arrayContaining([
expect.objectContaining({ kind: 'setup', state: 'failed' })
])
}
})
expect(homeDb.getTask(task.id)?.status).toBe('failed')
expect(workerRuntime.sendTerminalAgentPrompt).not.toHaveBeenCalled()
})
it('rejects control mail before queueing when the worker lacks that capability', async () => {
workerCapabilities = workerCapabilities.filter(
(capability) => capability !== ORCHESTRATION_FEDERATION_CONTROL_MAIL_RUNTIME_CAPABILITY
)
const task = createHomeTask()
const started = await homeDispatcher.dispatch(startRequest(task.id))
expect(started).toMatchObject({ ok: true, result: { state: 'ready' } })
const dispatch = homeDb.getDispatchContext(task.id)!
const sent = await homeDispatcher.dispatch({
id: 'send-control-to-old-worker',
authToken: 'coordinator-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'send-control-to-old-worker-request',
method: 'orchestration.send',
params: {
from: 'term_coord',
to: `dispatch:${dispatch.id}`,
subject: 'Continue',
body: 'This worker cannot receive control mail yet.',
type: 'status'
}
})
expect(sent).toMatchObject({
ok: false,
error: { code: 'capability_unsupported' }
})
expect(homeDb.listPendingFederationRelay(dispatch.id, 'to_worker')).toHaveLength(0)
})
it('durably relays remote completion into the home Run and acknowledges it', async () => {
const task = createHomeTask()
const started = await homeDispatcher.dispatch(startRequest(task.id))
expect(started.ok).toBe(true)
const dispatch = homeDb.getDispatchContext(task.id)!
const prompt = vi.mocked(workerRuntime.sendTerminalAgentPrompt).mock.calls[0]?.[1] ?? ''
const capability = prompt.match(/--dispatch-capability (dcap_[A-Za-z0-9_-]+)/)?.[1]
expect(capability).toBeTruthy()
const sent = await workerDispatcher.dispatch({
id: 'rpc_worker_done',
authToken: 'worker-local-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'worker_done_request',
orchestrationCapability: capability,
method: 'orchestration.send',
params: {
from: 'term_windows_worker',
subject: 'Windows audit complete',
body: 'Audited Windows behavior. Found no blocker. Nothing remains.',
type: 'worker_done',
payload: JSON.stringify({
taskId: task.id,
dispatchId: dispatch.id,
outcome: 'succeeded',
filesModified: []
})
}
})
expect(sent).toMatchObject({
ok: true,
result: { relay: { dispatchId: dispatch.id, accepted: true } }
})
expect(homeDb.getTask(task.id)?.status).toBe('dispatched')
await homeRuntime.syncOrchestrationFederation()
expect(homeDb.getTask(task.id)?.status).toBe('completed')
expect(homeDb.getWorkerDispatch(dispatch.id)?.state).toBe('succeeded')
expect(homeDb.getRunMailboxHistory(task.run_id, 10)).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.stringMatching(/^relay_/),
type: 'worker_done',
subject: 'Windows audit complete'
})
])
)
expect(
workerDb.listFederationRelay({
dispatchId: dispatch.id,
direction: 'to_home',
afterSequence: 0
})[0]
).toMatchObject({ acked_at: expect.any(String) })
})
it('relays a worker question home and the coordinator answer back', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
const dispatch = homeDb.getDispatchContext(task.id)!
const prompt = vi.mocked(workerRuntime.sendTerminalAgentPrompt).mock.calls[0]?.[1] ?? ''
const capability = prompt.match(/--dispatch-capability (dcap_[A-Za-z0-9_-]+)/)?.[1]
const ask = workerDispatcher.dispatch({
id: 'rpc_remote_ask',
authToken: 'worker-local-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'remote_question_request',
orchestrationCapability: capability,
method: 'orchestration.ask',
params: {
from: 'term_windows_worker',
question: 'Should I include slow integration tests?',
options: 'yes,no',
timeoutMs: 60_000
}
})
await vi.waitFor(() =>
expect(
workerDb.listFederationRelay({
dispatchId: dispatch.id,
direction: 'to_home',
afterSequence: 0
})
).toHaveLength(1)
)
await homeRuntime.syncOrchestrationFederation()
const question = homeDb
.getRunMailboxHistory(task.run_id, 10)
.find((message) => message.type === 'question')
expect(question).toMatchObject({
body: 'Should I include slow integration tests?'
})
const reply = await homeDispatcher.dispatch({
id: 'rpc_home_reply',
authToken: 'coordinator-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'home_reply_request',
method: 'orchestration.reply',
params: {
id: question!.id,
body: 'yes',
from: 'term_coord'
}
})
expect(reply).toMatchObject({ ok: true, result: { question: { status: 'answered' } } })
await homeRuntime.syncOrchestrationFederation()
await expect(ask).resolves.toMatchObject({
ok: true,
result: {
answer: 'yes',
messageId: question!.id,
timedOut: false
}
})
expect(
homeDb.listFederationRelay({
dispatchId: dispatch.id,
direction: 'to_worker',
afterSequence: 0
})[0]
).toMatchObject({ acked_at: expect.any(String) })
})
it('keeps a timed-out remote question resumable', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
const prompt = vi.mocked(workerRuntime.sendTerminalAgentPrompt).mock.calls[0]?.[1] ?? ''
const capability = prompt.match(/--dispatch-capability (dcap_[A-Za-z0-9_-]+)/)?.[1]
const timedOut = await workerDispatcher.dispatch({
id: 'rpc_remote_ask_timeout',
authToken: 'worker-local-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'remote_question_timeout_request',
orchestrationCapability: capability,
method: 'orchestration.ask',
params: {
from: 'term_windows_worker',
question: 'Resume this later?',
timeoutMs: 1
}
})
expect(timedOut).toMatchObject({
ok: true,
result: { timedOut: true, messageId: expect.stringMatching(/^relay_/) }
})
const questionId = (timedOut as { result: { messageId: string } }).result.messageId
await homeRuntime.syncOrchestrationFederation()
await homeDispatcher.dispatch({
id: 'rpc_home_late_reply',
authToken: 'coordinator-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'home_late_reply_request',
method: 'orchestration.reply',
params: { id: questionId, body: 'yes', from: 'term_coord' }
})
restartWorkerRuntime()
const resumed = workerDispatcher.dispatch({
id: 'rpc_remote_ask_resume',
authToken: 'worker-local-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'remote_question_resume_request',
orchestrationCapability: capability,
method: 'orchestration.ask',
params: { from: 'term_windows_worker', resume: questionId, timeoutMs: 5_000 }
})
await homeRuntime.syncOrchestrationFederation()
await expect(resumed).resolves.toMatchObject({
ok: true,
result: { answer: 'yes', messageId: questionId, timedOut: false }
})
})
it('retries a lost relay acknowledgment without duplicating the home message', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
const dispatch = homeDb.getDispatchContext(task.id)!
const prompt = vi.mocked(workerRuntime.sendTerminalAgentPrompt).mock.calls[0]?.[1] ?? ''
const capability = prompt.match(/--dispatch-capability (dcap_[A-Za-z0-9_-]+)/)?.[1]
await workerDispatcher.dispatch({
id: 'rpc_remote_status',
authToken: 'worker-local-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'remote_status_request',
orchestrationCapability: capability,
method: 'orchestration.send',
params: {
from: 'term_windows_worker',
subject: 'Checkpoint',
body: 'One durable update',
type: 'status'
}
})
loseNextAckResponse = true
await expect(homeRuntime.syncOrchestrationFederation()).resolves.toBeUndefined()
await homeRuntime.syncOrchestrationFederation()
expect(
homeDb
.getRunMailboxHistory(task.run_id, 10)
.filter((message) => message.subject === 'Checkpoint')
).toHaveLength(1)
expect(homeDb.getFederatedDispatch(dispatch.id)?.to_home_imported_sequence).toBe(1)
})
it('rejects a reordered relay gap, then converges without loss or duplication', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
const dispatch = homeDb.getDispatchContext(task.id)!
expect(() =>
homeDb.importFederatedRelayItem({
dispatchId: dispatch.id,
sequence: 2,
message: {
id: 'relay_gap',
runId: task.run_id,
from: `dispatch:${dispatch.id}`,
to: `run:${task.run_id}`,
subject: 'Gap',
body: 'Out of order',
type: 'status',
priority: 'normal'
},
lifecycle: { kind: 'none' }
})
).toThrow(/not contiguous/)
expect(homeDb.getMessageById('relay_gap')).toBeUndefined()
expect(homeDb.getFederatedDispatch(dispatch.id)?.to_home_imported_sequence).toBe(0)
homeDb.importFederatedRelayItem({
dispatchId: dispatch.id,
sequence: 1,
message: {
id: 'relay_first',
runId: task.run_id,
from: `dispatch:${dispatch.id}`,
to: `run:${task.run_id}`,
subject: 'First',
body: 'Arrived after the gap was rejected',
type: 'status',
priority: 'normal'
},
lifecycle: { kind: 'none' }
})
const recovered = homeDb.importFederatedRelayItem({
dispatchId: dispatch.id,
sequence: 2,
message: {
id: 'relay_gap',
runId: task.run_id,
from: `dispatch:${dispatch.id}`,
to: `run:${task.run_id}`,
subject: 'Gap',
body: 'Out of order',
type: 'status',
priority: 'normal'
},
lifecycle: { kind: 'none' }
})
const duplicate = homeDb.importFederatedRelayItem({
dispatchId: dispatch.id,
sequence: 2,
message: {
id: 'relay_gap',
runId: task.run_id,
from: `dispatch:${dispatch.id}`,
to: `run:${task.run_id}`,
subject: 'Gap',
body: 'Out of order',
type: 'status',
priority: 'normal'
},
lifecycle: { kind: 'none' }
})
expect(recovered.duplicate).toBe(false)
expect(duplicate.duplicate).toBe(true)
expect(homeDb.getFederatedDispatch(dispatch.id)?.to_home_imported_sequence).toBe(2)
expect(
homeDb
.getRunMailboxHistory(task.run_id, 10)
.filter((message) => ['relay_first', 'relay_gap'].includes(message.id))
).toHaveLength(2)
})
it('restarts relay polling when a federated worker is shown', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
const dispatch = homeDb.getDispatchContext(task.id)!
const prompt = vi.mocked(workerRuntime.sendTerminalAgentPrompt).mock.calls[0]?.[1] ?? ''
const capability = prompt.match(/--dispatch-capability (dcap_[A-Za-z0-9_-]+)/)?.[1]
homeRuntime.stopOrchestrationFederationRelay()
await workerDispatcher.dispatch({
id: 'rpc_restart_status',
authToken: 'worker-local-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'restart_status_request',
orchestrationCapability: capability,
method: 'orchestration.send',
params: {
from: 'term_windows_worker',
subject: 'After home restart',
body: 'Relay me after worker-show',
type: 'status'
}
})
await homeDispatcher.dispatch({
id: 'rpc_restart_show',
authToken: 'coordinator-token',
method: 'orchestration.workerShow',
params: { dispatch: dispatch.id }
})
await vi.waitFor(() =>
expect(
homeDb
.getRunMailboxHistory(task.run_id, 10)
.some((message) => message.subject === 'After home restart')
).toBe(true)
)
})
it('treats a worker runtime ID change as an epoch, not a new server', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
const dispatch = homeDb.getDispatchContext(task.id)!
const oldEpoch = homeDb.getFederatedDispatch(dispatch.id)?.remote_runtime_epoch
restartWorkerRuntime()
const shown = await homeDispatcher.dispatch({
id: 'rpc_worker_restart_show',
authToken: 'coordinator-token',
method: 'orchestration.workerShow',
params: { dispatch: dispatch.id }
})
expect(shown).toMatchObject({
ok: true,
result: { observation: { status: 'running', exactWorker: true } }
})
expect(homeDb.getFederatedDispatch(dispatch.id)?.remote_runtime_epoch).not.toBe(oldEpoch)
expect(homeDb.getFederatedDispatch(dispatch.id)?.peer_fingerprint).toBe(
'windows_peer_fingerprint'
)
})
it('stops only the exact remote agent terminal', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
const dispatch = homeDb.getDispatchContext(task.id)!
const stopped = await homeDispatcher.dispatch({
id: 'rpc_remote_stop',
authToken: 'coordinator-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'request_remote_stop',
method: 'orchestration.workerStop',
params: { dispatch: dispatch.id }
})
expect(stopped).toMatchObject({
ok: true,
result: { state: 'stopped', processAction: 'closed_agent_terminal' }
})
expect(workerRuntime.closeTerminal).toHaveBeenCalledTimes(1)
expect(workerRuntime.closeTerminal).toHaveBeenCalledWith('term_windows_worker')
expect(homeDb.getTask(task.id)?.status).toBe('blocked')
vi.mocked(workerRuntime.showTerminal).mockResolvedValue({
handle: 'term_windows_worker',
worktreeId: 'repo::windows-worktree',
connected: false,
writable: false
} as never)
const shown = await homeDispatcher.dispatch({
id: 'rpc_remote_show_after_stop',
authToken: 'coordinator-token',
method: 'orchestration.workerShow',
params: { dispatch: dispatch.id }
})
expect(shown).toMatchObject({
ok: true,
result: { observation: { status: 'exited', exactWorker: true } }
})
})
it('rejects a re-paired server before show or stop effects', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
const dispatch = homeDb.getDispatchContext(task.id)!
workerPeerFingerprint = 'replacement_windows_peer'
const shown = await homeDispatcher.dispatch({
id: 'rpc_changed_peer_show',
authToken: 'coordinator-token',
method: 'orchestration.workerShow',
params: { dispatch: dispatch.id }
})
const stopped = await homeDispatcher.dispatch({
id: 'rpc_changed_peer_stop',
authToken: 'coordinator-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'request_changed_peer_stop',
method: 'orchestration.workerStop',
params: { dispatch: dispatch.id }
})
expect(shown).toMatchObject({ ok: false, error: { code: 'peer_changed' } })
expect(stopped).toMatchObject({ ok: false, error: { code: 'peer_changed' } })
expect(homeDb.getWorkerDispatch(dispatch.id)?.state).toBe('ready')
expect(workerRuntime.closeTerminal).not.toHaveBeenCalled()
})
it('coalesces overlapping relay polls for the same Dispatch', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
await homeRuntime.syncOrchestrationFederation()
homeRuntime.stopOrchestrationFederationRelay()
let releasePull!: () => void
const blockedPull = new Promise<void>((resolve) => {
releasePull = resolve
})
let pullCount = 0
vi.spyOn(homeRuntime, 'callOrchestrationWorkerServer').mockImplementation(
async (_selector, method) => {
if (method !== 'orchestration.federationPull') {
throw new Error(`Unexpected relay method ${method}`)
}
pullCount += 1
await blockedPull
return { runtimeEpoch: workerRuntime.getRuntimeId(), items: [] }
}
)
const first = homeRuntime.syncOrchestrationFederation()
const second = homeRuntime.syncOrchestrationFederation()
await vi.waitFor(() => expect(pullCount).toBe(1))
releasePull()
await Promise.all([first, second])
expect(pullCount).toBe(1)
})
it('warns once while a federated Dispatch remains unreachable', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
await homeRuntime.syncOrchestrationFederation()
homeRuntime.stopOrchestrationFederationRelay()
vi.spyOn(homeRuntime, 'callOrchestrationWorkerServer').mockRejectedValue(
new Error('worker server offline')
)
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
await homeRuntime.syncOrchestrationFederation()
await homeRuntime.syncOrchestrationFederation()
expect(warn).toHaveBeenCalledTimes(1)
expect(warn).toHaveBeenCalledWith(
expect.stringContaining('Federation sync failed'),
expect.any(Error)
)
warn.mockRestore()
})
it('returns stop_unknown when the worker server disconnects after the home fence', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
const dispatch = homeDb.getDispatchContext(task.id)!
vi.spyOn(homeRuntime, 'callOrchestrationWorkerServer').mockRejectedValueOnce(
new Error('connection lost')
)
const stopped = await homeDispatcher.dispatch({
id: 'rpc_disconnected_stop',
authToken: 'coordinator-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'request_disconnected_stop',
method: 'orchestration.workerStop',
params: { dispatch: dispatch.id }
})
expect(stopped).toMatchObject({
ok: true,
result: { state: 'stop_unknown', processAction: 'unknown' }
})
expect(homeDb.getTask(task.id)?.status).toBe('blocked')
expect(workerRuntime.closeTerminal).not.toHaveBeenCalled()
})
it('never reads or closes a same-looking replacement process', async () => {
const task = createHomeTask()
await homeDispatcher.dispatch(startRequest(task.id))
const dispatch = homeDb.getDispatchContext(task.id)!
vi.mocked(workerRuntime.getTerminalProcessIncarnation).mockReturnValue(
'windows_runtime:pty:replacement'
)
const read = await homeDispatcher.dispatch({
id: 'rpc_replacement_read',
authToken: 'coordinator-token',
method: 'orchestration.workerRead',
params: { dispatch: dispatch.id }
})
const stopped = await homeDispatcher.dispatch({
id: 'rpc_replacement_stop',
authToken: 'coordinator-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'request_replacement_stop',
method: 'orchestration.workerStop',
params: { dispatch: dispatch.id }
})
expect(read).toMatchObject({
ok: false,
error: { code: 'worker_identity_changed' }
})
expect(stopped).toMatchObject({
ok: true,
result: { state: 'stop_unknown', processAction: 'none' }
})
expect(workerRuntime.closeTerminal).not.toHaveBeenCalled()
})
})
@@ -0,0 +1,297 @@
import { isTuiAgent } from '../../../../shared/tui-agent-config'
import type { TuiAgent } from '../../../../shared/types'
import { buildDispatchPreamble } from '../../orchestration/preamble'
import { OrchestrationError } from '../../orchestration/orchestration-error'
import { defineMethod, type RpcMethod } from '../core'
import { assertOrchestrationWorktreeCreationSupported } from './orchestration-folder-worktree-placement'
import {
appendFederationSetupEffect,
appendFederationTerminalEffects,
type FederationEffect
} from './orchestration-federation-effects'
import type { WorkerSetupReceipt } from './orchestration-worker-topology'
import {
monitorFederatedSetup,
persistFederatedReadinessStage,
persistFederatedSetupSpawnFailure,
persistFederatedSetupWaitOutcome
} from './orchestration-federation-setup'
import { FederationAttachStartParams } from './orchestration-federation-start-schema'
import { failFederatedAttachmentWithReceipt } from './orchestration-federation-start-receipt'
export const ORCHESTRATION_FEDERATION_ATTACH_METHODS: RpcMethod[] = [
defineMethod({
name: 'orchestration.federationAttachStart',
params: FederationAttachStartParams,
handler: async (params, { runtime, orchestrationMutation }) => {
if (!orchestrationMutation) {
throw new OrchestrationError(
'invalid_argument',
'Federated worker attachment requires a durable retry request.'
)
}
if (params.worktree === 'current' || params.worktree === 'new-child') {
throw new OrchestrationError(
'invalid_argument',
'A remote worker requires an exact existing worktree or new-top-level.'
)
}
const createsWorktree = params.worktree === 'new-top-level'
if (createsWorktree && (!params.name || !params.repo)) {
throw new OrchestrationError(
'invalid_argument',
'A remote new-top-level worktree requires --name and an explicit --repo.'
)
}
if (createsWorktree && params.terminal) {
throw new OrchestrationError(
'invalid_argument',
'--terminal cannot combine with remote new-worktree creation.'
)
}
if (
!createsWorktree &&
(params.name || params.repo || params.baseBranch || params.setup || params.setupSource)
) {
throw new OrchestrationError(
'invalid_argument',
'Creation and setup options apply only to remote new-top-level worktrees.'
)
}
if (params.terminal && params.agent) {
throw new OrchestrationError(
'invalid_argument',
'--terminal reuses an existing agent and cannot combine with --agent.'
)
}
const agent = params.agent
if (!params.terminal && (!agent || !isTuiAgent(agent))) {
throw new OrchestrationError(
'agent_unconfigured',
'A configured --agent is required when federated worker-start creates a terminal.'
)
}
if (agent) {
runtime.validateOrchestrationAgentLauncher(agent as TuiAgent)
}
if (createsWorktree) {
await assertOrchestrationWorktreeCreationSupported({
runtime,
repoSelector: params.repo as string,
existingPlacement: 'an exact existing folder workspace'
})
}
const db = runtime.getOrchestrationDb()
db.createRemoteDispatchAttachment({
dispatchId: params.dispatchId,
taskId: params.taskId,
homePeerFingerprint: orchestrationMutation.callerFingerprint,
protocolVersion: params.protocolVersion,
runtimeEpoch: runtime.getRuntimeId(),
mutationReceipt: orchestrationMutation
})
const effects: FederationEffect[] = []
let failedStage = createsWorktree ? 'worktree_create' : 'worktree_resolve'
let worktree
let terminalHandle = params.terminal
const setupSource = createsWorktree
? (params.setupSource ?? (params.setup ? 'explicit_request' : 'orchestration_default'))
: 'existing_worktree'
let setup: WorkerSetupReceipt = {
requested: createsWorktree ? (params.setup ?? 'run') : 'not_applicable',
effective: createsWorktree ? (params.setup ?? 'run') : 'not_applicable',
source: setupSource,
hookFound: false,
startupPolicy: 'start-immediately',
state: createsWorktree ? 'not_configured' : 'not_applicable'
}
try {
if (createsWorktree) {
db.recordRemoteAttachmentStage({
dispatchId: params.dispatchId,
stage: 'worktree_creating'
})
const setupDecision = params.setup ?? 'run'
const created = await runtime.createManagedWorktree({
repoSelector: params.repo as string,
name: params.name as string,
baseBranch: params.baseBranch,
displayName: params.displayName,
comment: params.comment,
runHooks: setupDecision === 'run',
setupDecision,
awaitTerminalProvisioning: true,
observeSetupCompletion: true,
createdWithAgent: agent as TuiAgent,
startupAgent: agent as TuiAgent,
activate: false,
lineage: { noParent: true }
})
worktree = created.worktree
terminalHandle = created.startupTerminal?.handle
effects.push({
kind: 'worktree',
action: 'created_top_level',
id: created.worktree.id
})
setup = {
requested: setupDecision,
effective: setupDecision,
source: setupSource,
hookFound: created.setupReceipt?.hookFound ?? false,
startupPolicy: created.setupReceipt?.startupPolicy ?? 'start-immediately',
state: created.setupReceipt?.state ?? 'not_configured'
}
if (!terminalHandle) {
throw new Error(
created.warning ?? 'Agent-first worktree creation returned no terminal.'
)
}
const listed = await runtime.listTerminals(`id:${created.worktree.id}`)
appendFederationTerminalEffects(
effects,
listed.terminals,
terminalHandle,
created.setupReceipt?.terminalHandle
)
appendFederationSetupEffect(effects, setup)
} else {
worktree = await runtime.showManagedWorktree(params.worktree).catch(() => {
throw new OrchestrationError(
'worktree_not_found_on_server',
`Worktree ${params.worktree} was not found on the selected worker server.`
)
})
effects.push(
{ kind: 'worktree', action: 'reused', id: worktree.id },
{ kind: 'setup', action: 'not_applicable', state: 'not_applicable' }
)
if (terminalHandle) {
const terminal = await runtime.showTerminal(terminalHandle)
if (terminal.worktreeId !== worktree.id) {
throw new OrchestrationError(
'terminal_worktree_mismatch',
`Terminal ${terminalHandle} does not belong to worktree ${worktree.id}.`
)
}
if (!(await runtime.isTerminalRunningAgent(terminalHandle))) {
throw new OrchestrationError(
'agent_unconfigured',
`Terminal ${terminalHandle} is not running a recognized agent.`
)
}
effects.push({
kind: 'terminal',
role: 'agent',
action: 'reused',
id: terminalHandle
})
} else {
failedStage = 'terminal_create'
const terminal = await runtime.createTerminal(`id:${worktree.id}`, {
command: agent,
title: `worker-${params.taskId}`,
presentation: 'background'
})
terminalHandle = terminal.handle
effects.push({
kind: 'terminal',
role: 'agent',
action: 'created',
id: terminal.handle
})
}
}
if (!worktree || !terminalHandle) {
throw new Error('Federated worker topology did not resolve.')
}
const setupStage = {
db,
dispatchId: params.dispatchId,
worktreeId: worktree.id,
terminalHandle,
setup,
effects
}
if (persistFederatedSetupSpawnFailure(setupStage)) {
failedStage = 'setup_start'
throw new Error('Setup terminal failed to start before the gated agent launch.')
}
persistFederatedReadinessStage(setupStage)
failedStage = 'agent_readiness'
const wait = await runtime.waitForTerminal(terminalHandle, {
condition: 'tui-idle',
timeoutMs: params.timeoutMs ?? 60_000
})
persistFederatedSetupWaitOutcome({ ...setupStage, wait })
if (!wait.satisfied) {
if (setup.state === 'failed') {
failedStage = 'setup_wait'
}
throw new Error(
wait.blockedReason
? `Agent startup blocked: ${wait.blockedReason}`
: `Agent did not become ready (${wait.status}).`
)
}
const paneKey = runtime.getTerminalPaneKey(terminalHandle)
const processIncarnation = runtime.getTerminalProcessIncarnation(terminalHandle)
if (!paneKey || !processIncarnation) {
throw new Error('stable_pane_required')
}
const capability = db.prepareRemoteAttachmentAuthority({
dispatchId: params.dispatchId,
paneKey,
processIncarnation,
worktreeId: worktree.id,
terminalHandle,
setupState: setup.state,
effects
})
failedStage = 'dispatch_input'
await runtime.sendTerminalAgentPrompt(
terminalHandle,
buildDispatchPreamble({
taskId: params.taskId,
dispatchId: params.dispatchId,
taskSpec: params.taskSpec,
coordinatorHandle: 'Run home (relayed by Orca)',
workerHandle: terminalHandle,
dispatchCapability: capability,
devMode: params.devMode,
cliCommand: runtime.getTerminalOrchestrationCliCommand(terminalHandle)
})
)
effects.push({
kind: 'dispatch_input',
role: 'agent',
id: terminalHandle,
state: 'accepted'
})
const attachment = db.markRemoteAttachmentReady(params.dispatchId, effects)
monitorFederatedSetup({ ...setupStage, runtime })
return {
dispatchId: params.dispatchId,
state: attachment.state,
stage: attachment.stage,
runtimeEpoch: runtime.getRuntimeId(),
worktreeId: worktree.id,
terminalHandle,
setup,
effects,
residualResources: []
}
} catch (error) {
return failFederatedAttachmentWithReceipt({
db,
dispatchId: params.dispatchId,
runtimeEpoch: runtime.getRuntimeId(),
failedStage,
error,
setup
})
}
}
})
]
@@ -0,0 +1,17 @@
import { isFolderRepo } from '../../../../shared/repo-kind'
import type { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationError } from '../../orchestration/orchestration-error'
export async function assertOrchestrationWorktreeCreationSupported(args: {
runtime: OrcaRuntimeService
repoSelector: string
existingPlacement: string
}): Promise<void> {
if (!isFolderRepo(await args.runtime.showRepo(args.repoSelector))) {
return
}
throw new OrchestrationError(
'invalid_argument',
`Folder projects cannot create orchestration worktrees; use ${args.existingPlacement}.`
)
}
@@ -0,0 +1,181 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
ORCHESTRATION_CONTRACT_RUNTIME_CAPABILITY,
ORCHESTRATION_FEDERATION_RUNTIME_CAPABILITY
} from '../../../../shared/protocol-version'
import { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationDb } from '../../orchestration/db'
import { RpcDispatcher } from '../dispatcher'
import { ORCHESTRATION_METHODS } from './orchestration'
import { startFederatedWorker } from './orchestration-federated-worker-start'
describe('orchestration migration behavior', () => {
const databases: OrchestrationDb[] = []
afterEach(() => {
for (const database of databases.splice(0)) {
database.close()
}
})
function createRuntime(): { db: OrchestrationDb; runtime: OrcaRuntimeService } {
const db = new OrchestrationDb(':memory:')
const runtime = new OrcaRuntimeService()
runtime.setOrchestrationDb(db)
databases.push(db)
return { db, runtime }
}
it('lists an explicitly selected legacy Run without binding or mutation', async () => {
const { db, runtime } = createRuntime()
const task = db.createTask({ spec: 'pre-upgrade work' })
const taskList = ORCHESTRATION_METHODS.find(
(method) => method.name === 'orchestration.taskList'
)!
const listed = (await taskList.handler(taskList.params!.parse({ run: 'run_legacy_local' }), {
runtime
})) as {
runId: string
legacyReadOnly: boolean
tasks: { id: string }[]
}
expect(listed).toMatchObject({
runId: 'run_legacy_local',
legacyReadOnly: true,
tasks: [{ id: task.id }]
})
expect(db.getTask(task.id)?.status).toBe('ready')
})
it('rejects a pre-contract worker_done before message or lifecycle mutation', async () => {
const { db, runtime } = createRuntime()
const run = db.createRun({
objective: 'legacy worker',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:leaf_coord'
})
const task = db.createTask({ spec: 'legacy worker', runId: run.id })
const dispatch = db.createDispatchContext(task.id, 'term_worker', 'tab_worker:leaf_worker')
const dispatcher = new RpcDispatcher({ runtime, methods: ORCHESTRATION_METHODS })
const response = await dispatcher.dispatch({
id: 'legacy_worker_done',
authToken: 'worker-token',
method: 'orchestration.send',
params: {
from: 'term_worker',
subject: 'done',
type: 'worker_done',
payload: JSON.stringify({
taskId: task.id,
dispatchId: dispatch.id,
outcome: 'succeeded'
})
}
})
expect(response).toMatchObject({
ok: false,
error: {
code: 'orchestration_migration_required',
data: { effectsApplied: false }
}
})
expect(db.getInbox(100)).toHaveLength(0)
expect(db.getTask(task.id)?.status).toBe('dispatched')
expect(db.getDispatchContextById(dispatch.id)?.status).toBe('dispatched')
})
it('rejects a connected server missing the contract before home or remote effects', async () => {
const { db, runtime } = createRuntime()
const run = db.createRun({
objective: 'mixed-version worker',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:leaf_coord'
})
const task = db.createTask({ spec: 'remote work', runId: run.id })
vi.spyOn(runtime, 'resolveOrchestrationWorkerServer').mockReturnValue({
environmentId: 'environment_windows',
name: 'windows',
peerFingerprint: 'windows_peer'
})
vi.spyOn(runtime, 'callOrchestrationWorkerServer').mockResolvedValue({
capabilities: [ORCHESTRATION_FEDERATION_RUNTIME_CAPABILITY]
})
await expect(
startFederatedWorker({
params: {
task: task.id,
from: 'term_coord',
on: 'windows',
worktree: 'new-top-level',
repo: 'id:windows-repo',
name: 'remote-work',
agent: 'codex'
},
runtime,
db,
runId: run.id,
task,
orchestrationMutation: {
callerFingerprint: 'caller',
requestId: 'remote_start',
method: 'orchestration.workerStart',
payloadHash: 'payload'
}
})
).rejects.toMatchObject({
code: 'orchestration_migration_required',
data: { reason: 'runtime_capability_missing', effectsApplied: false }
})
expect(db.getTask(task.id)?.status).toBe('ready')
expect(db.getDispatchContext(task.id)).toBeUndefined()
})
it('rejects a connected server missing federation support before Task mutation', async () => {
const { db, runtime } = createRuntime()
const run = db.createRun({
objective: 'unsupported worker',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:leaf_coord'
})
const task = db.createTask({ spec: 'remote work', runId: run.id })
vi.spyOn(runtime, 'resolveOrchestrationWorkerServer').mockReturnValue({
environmentId: 'environment_windows',
name: 'windows',
peerFingerprint: 'windows_peer'
})
vi.spyOn(runtime, 'callOrchestrationWorkerServer').mockResolvedValue({
capabilities: [ORCHESTRATION_CONTRACT_RUNTIME_CAPABILITY]
})
await expect(
startFederatedWorker({
params: {
task: task.id,
from: 'term_coord',
on: 'windows',
worktree: 'new-top-level',
repo: 'id:windows-repo',
name: 'remote-work',
agent: 'codex'
},
runtime,
db,
runId: run.id,
task,
orchestrationMutation: {
callerFingerprint: 'caller',
requestId: 'remote_start',
method: 'orchestration.workerStart',
payloadHash: 'payload'
}
})
).rejects.toMatchObject({ code: 'capability_unsupported' })
expect(db.getTask(task.id)?.status).toBe('ready')
expect(db.getDispatchContext(task.id)).toBeUndefined()
})
})
@@ -0,0 +1,100 @@
import { z } from 'zod'
import { defineMethod, type RpcMethod } from '../core'
import { OptionalString, requiredString } from '../schemas'
import type { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationError } from '../../orchestration/orchestration-error'
const RunCreateParams = z.object({
objective: requiredString('Missing --objective'),
from: requiredString('Missing coordinator terminal')
})
const RunUseParams = z.object({
id: requiredString('Missing --id'),
from: requiredString('Missing coordinator terminal')
})
const RunCurrentParams = z.object({ from: requiredString('Missing coordinator terminal') })
const RunListParams = z.object({})
const RunShowParams = z.object({ id: requiredString('Missing --id'), from: OptionalString })
function requireCallerPane(runtime: OrcaRuntimeService, handle: string): string {
const paneKey = runtime.getTerminalPaneKey(handle)
if (!paneKey) {
throw new OrchestrationError(
'stable_pane_required',
'The coordinator terminal has no stable pane identity. Run this command inside a live Orca terminal.'
)
}
return paneKey
}
export const ORCHESTRATION_RUN_METHODS: RpcMethod[] = [
defineMethod({
name: 'orchestration.runCreate',
params: RunCreateParams,
handler: (params, { runtime }) => {
const paneKey = requireCallerPane(runtime, params.from)
const db = runtime.getOrchestrationDb()
const priorRun = db.getCurrentRunForPane(paneKey)
const run = db.createRun({
objective: params.objective,
coordinatorHandle: params.from,
coordinatorPaneKey: paneKey
})
if (priorRun) {
runtime.cancelMessageWaiters(`run:${priorRun.id}`)
}
return { run, binding: { consumerGeneration: run.consumer_generation } }
}
}),
defineMethod({
name: 'orchestration.runUse',
params: RunUseParams,
handler: (params, { runtime }) => {
const paneKey = requireCallerPane(runtime, params.from)
const db = runtime.getOrchestrationDb()
const priorRun = db.getCurrentRunForPane(paneKey)
const run = db.bindRun({
runId: params.id,
coordinatorHandle: params.from,
coordinatorPaneKey: paneKey
})
if (!run) {
throw new OrchestrationError(
'run_not_found',
`Run ${params.id} was not found or is inspect-only.`
)
}
runtime.cancelMessageWaiters(`run:${params.id}`)
if (priorRun && priorRun.id !== params.id) {
runtime.cancelMessageWaiters(`run:${priorRun.id}`)
}
return { run, binding: { consumerGeneration: run.consumer_generation } }
}
}),
defineMethod({
name: 'orchestration.runCurrent',
params: RunCurrentParams,
handler: (params, { runtime }) => {
const paneKey = requireCallerPane(runtime, params.from)
return { run: runtime.getOrchestrationDb().getCurrentRunForPane(paneKey) ?? null }
}
}),
defineMethod({
name: 'orchestration.runList',
params: RunListParams,
handler: (_params, { runtime }) => ({ runs: runtime.getOrchestrationDb().listRuns() })
}),
defineMethod({
name: 'orchestration.runShow',
params: RunShowParams,
handler: (params, { runtime }) => {
const run = runtime.getOrchestrationDb().getRun(params.id)
if (!run) {
throw new OrchestrationError('run_not_found', `Run ${params.id} was not found.`)
}
return { run }
}
})
]
@@ -0,0 +1,294 @@
import { z } from 'zod'
import {
ORCHESTRATION_WORKER_READ_SOURCES,
type OrchestrationWorkerReadResult
} from '../../../../shared/orchestration-worker-output'
import type { RuntimeTerminalRead } from '../../../../shared/runtime-types'
import { OrchestrationError } from '../../orchestration/orchestration-error'
import { syncFederatedDispatch } from '../../orchestration/federation-sync'
import {
createWorkerOutputSourceIdentity,
decodeWorkerOutputCursor,
encodeWorkerOutputCursor
} from '../../orchestration/worker-output-cursor'
import { defineMethod, type RpcMethod } from '../core'
import { OptionalFiniteNumber, requiredString } from '../schemas'
import {
callFederatedWorkerShow,
exposeWorker,
inspectWorkerTerminal,
resolvePinnedFederatedServer
} from './orchestration-worker-observation'
import { readExactWorkerOutput } from './orchestration-worker-output'
const WorkerDispatchParams = z.object({ dispatch: requiredString('Missing --dispatch') })
const WorkerReadParams = WorkerDispatchParams.extend({
cursor: z.union([z.number().int().nonnegative(), z.string().min(1).max(2_048)]).optional(),
limit: OptionalFiniteNumber,
source: z.enum(ORCHESTRATION_WORKER_READ_SOURCES).optional()
})
export const ORCHESTRATION_WORKER_CONTROL_METHODS: RpcMethod[] = [
defineMethod({
name: 'orchestration.workerShow',
params: WorkerDispatchParams,
handler: async (params, { runtime }) => {
const db = runtime.getOrchestrationDb()
const dispatch = db.getDispatchContextById(params.dispatch)
let worker = db.getWorkerDispatch(params.dispatch)
if (!dispatch || !worker) {
throw new OrchestrationError(
'dispatch_not_found',
`Worker Dispatch ${params.dispatch} was not found.`
)
}
const federated = db.getFederatedDispatch(params.dispatch)
if (federated) {
const server = resolvePinnedFederatedServer(runtime, federated)
runtime.ensureOrchestrationFederationRelay(dispatch.run_id)
const remote = await callFederatedWorkerShow(runtime, federated)
const attachment = remote.attachment
worker = db.updateWorkerSetupEvidence({
dispatchId: params.dispatch,
setupState: attachment.setup_state,
effects: attachment.effects
}).worker
if (
attachment.state === 'succeeded' ||
(attachment.state === 'failed' && attachment.stage === 'worker_report_queued')
) {
await syncFederatedDispatch(runtime, params.dispatch).catch(() => undefined)
} else if (
attachment.state === 'stopped' &&
['stopping', 'stop_unknown'].includes(worker.state)
) {
worker = db.reconcileFederatedWorkerStop(params.dispatch)
} else if (['ready', 'failed', 'stopped', 'start_unknown'].includes(attachment.state)) {
worker = db.reconcileFederatedWorkerStart({
dispatchId: params.dispatch,
state: attachment.state as 'ready' | 'failed' | 'stopped' | 'start_unknown',
stage: attachment.stage,
lastError: attachment.last_error,
worktreeId: attachment.worktree_id,
terminalHandle: attachment.terminal_handle,
setupState: attachment.setup_state,
effects: attachment.effects,
residualResources: attachment.residualResources
})
if (
attachment.state === 'ready' &&
attachment.worktree_id &&
attachment.terminal_handle
) {
db.updateFederatedDispatchResources({
dispatchId: params.dispatch,
remoteRuntimeEpoch: remote.runtimeEpoch,
worktreeId: attachment.worktree_id,
terminalHandle: attachment.terminal_handle
})
}
}
worker = db.getWorkerDispatch(params.dispatch)
if (!worker) {
throw new OrchestrationError(
'dispatch_not_found',
`Worker Dispatch ${params.dispatch} was not found after remote reconciliation.`
)
}
return {
dispatch: db.getDispatchContextById(params.dispatch),
worker: exposeWorker(worker),
server: { environmentId: server.environmentId, name: server.name },
remoteRuntimeEpoch: remote.runtimeEpoch,
terminal: remote.terminal,
observation: remote.observation
}
}
if (worker.runtime_epoch && worker.runtime_epoch !== runtime.getRuntimeId()) {
if (worker.state === 'starting') {
worker = db.markWorkerStartUnknown(
params.dispatch,
worker.stage,
'The runtime restarted before worker-start reached a terminal receipt.'
)
} else if (worker.state === 'stopping') {
worker = db.markWorkerStopUnknown(
params.dispatch,
'The runtime restarted before worker-stop reached a terminal receipt.'
)
}
}
const observation = await inspectWorkerTerminal(runtime, db, params.dispatch)
return {
dispatch,
worker: exposeWorker(worker),
terminal: observation.exact ? observation.terminal : null,
observation: { status: observation.status, exactWorker: observation.exact }
}
}
}),
defineMethod({
name: 'orchestration.workerRead',
params: WorkerReadParams,
handler: async (params, { runtime }) => {
const db = runtime.getOrchestrationDb()
const federated = db.getFederatedDispatch(params.dispatch)
if (federated) {
const server = resolvePinnedFederatedServer(runtime, federated)
try {
const remote = (await runtime.callOrchestrationWorkerServer(
server.environmentId,
'orchestration.federationReadOutput',
{
dispatchId: params.dispatch,
cursor: params.cursor,
limit: params.limit,
source: params.source
},
15_000
)) as { runtimeEpoch: string; output: OrchestrationWorkerReadResult }
return {
...remote.output,
server: { environmentId: server.environmentId, name: server.name },
remoteRuntimeEpoch: remote.runtimeEpoch
}
} catch (error) {
if (!(error instanceof OrchestrationError) || error.code !== 'method_not_found') {
throw error
}
return readLegacyFederatedTerminal({
runtime,
server,
federated,
workerState: db.getWorkerDispatch(params.dispatch)?.state ?? 'unknown',
dispatchId: params.dispatch,
source: params.source,
cursor: params.cursor,
limit: params.limit
})
}
}
const worker = db.getWorkerDispatch(params.dispatch)
if (!worker?.agent_terminal_handle) {
throw new OrchestrationError(
'dispatch_not_found',
`Worker Dispatch ${params.dispatch} has no agent terminal.`
)
}
const observation = await inspectWorkerTerminal(runtime, db, params.dispatch)
if (!observation.exact) {
throw new OrchestrationError(
'worker_identity_changed',
`Worker Dispatch ${params.dispatch} no longer resolves to its exact process.`
)
}
const output = await readExactWorkerOutput({
runtime,
dispatchId: params.dispatch,
terminalHandle: worker.agent_terminal_handle,
workerState: worker.state,
terminalStatus: observation.status === 'exited' ? 'exited' : 'running',
attachedAt: worker.created_at,
source: params.source,
cursor: params.cursor,
limit: params.limit
})
const afterRead = await inspectWorkerTerminal(runtime, db, params.dispatch)
if (!afterRead.exact) {
throw new OrchestrationError(
'worker_identity_changed',
`Worker Dispatch ${params.dispatch} changed process while output was read.`
)
}
return output
}
}),
defineMethod({
name: 'orchestration.workerAbandon',
params: WorkerDispatchParams,
handler: (params, { runtime }) => {
const abandoned = runtime.getOrchestrationDb().abandonWorkerDispatch(params.dispatch)
const worker = abandoned.worker
if (abandoned.disposition === 'abandoned') {
runtime.notifyMessageArrived(`dispatch:${params.dispatch}`, 'status')
}
return {
dispatchId: params.dispatch,
state: worker.state,
alreadySettled: abandoned.disposition !== 'abandoned',
stale: abandoned.disposition === 'stale',
processAction: 'none',
warning:
abandoned.disposition === 'stale'
? 'The Dispatch is no longer current; no state or process changed.'
: 'Possibly-live resources were retained; no process was stopped or deleted.',
residualResources: JSON.parse(worker.residual_resources) as unknown[]
}
}
})
]
async function readLegacyFederatedTerminal(args: {
runtime: Parameters<typeof resolvePinnedFederatedServer>[0]
server: ReturnType<typeof resolvePinnedFederatedServer>
federated: Parameters<typeof resolvePinnedFederatedServer>[1]
workerState: string
dispatchId: string
source: (typeof ORCHESTRATION_WORKER_READ_SOURCES)[number] | undefined
cursor: string | number | undefined
limit: number | undefined
}) {
const cursor = decodeWorkerOutputCursor(args.cursor, args.dispatchId)
if (args.source === 'transcript' || cursor?.source === 'transcript') {
throw new OrchestrationError(
'transcript_required',
`Connected server ${args.server.name} does not support structured worker output.`,
{ reason: 'remote_capability_unavailable' }
)
}
const remote = (await args.runtime.callOrchestrationWorkerServer(
args.server.environmentId,
'orchestration.federationRead',
{
dispatchId: args.dispatchId,
cursor: cursor?.source === 'terminal' ? cursor.position : undefined,
limit: args.limit
},
15_000
)) as { runtimeEpoch: string; terminal: RuntimeTerminalRead }
const sourceIdentity = createWorkerOutputSourceIdentity([
'legacy-remote-terminal',
args.federated.peer_fingerprint,
args.dispatchId,
remote.runtimeEpoch
])
if (
cursor?.source === 'terminal' &&
cursor.sourceIdentity !== null &&
cursor.sourceIdentity !== sourceIdentity
) {
throw new OrchestrationError(
'source_changed',
'The worker output source changed. Start a fresh worker-read without the old cursor.'
)
}
const nextPosition =
remote.terminal.nextCursor !== null && /^\d+$/.test(remote.terminal.nextCursor)
? Number.parseInt(remote.terminal.nextCursor, 10)
: null
return {
dispatchId: args.dispatchId,
source: 'terminal' as const,
sourceIdentity,
terminal: remote.terminal,
cursor:
nextPosition === null
? null
: encodeWorkerOutputCursor(args.dispatchId, 'terminal', sourceIdentity, nextPosition),
status: { worker: args.workerState, terminal: remote.terminal.status },
fallbackReason: 'remote_capability_unavailable' as const,
warnings: [],
server: { environmentId: args.server.environmentId, name: args.server.name },
remoteRuntimeEpoch: remote.runtimeEpoch
}
}
@@ -0,0 +1,10 @@
import type { RpcMethod } from '../core'
import { ORCHESTRATION_WORKER_CONTROL_METHODS } from './orchestration-worker-control'
import { ORCHESTRATION_WORKER_STOP_METHODS } from './orchestration-worker-stop'
import { ORCHESTRATION_WORKER_START_METHODS } from './orchestration-workers'
export const ORCHESTRATION_WORKER_METHODS: RpcMethod[] = [
...ORCHESTRATION_WORKER_START_METHODS,
...ORCHESTRATION_WORKER_CONTROL_METHODS,
...ORCHESTRATION_WORKER_STOP_METHODS
]
@@ -0,0 +1,82 @@
import type { OrcaRuntimeService } from '../../orca-runtime'
import type { OrchestrationDb } from '../../orchestration/db'
import { OrchestrationError } from '../../orchestration/orchestration-error'
import type { FederatedDispatchRow, WorkerDispatchRow } from '../../orchestration/types'
export async function inspectWorkerTerminal(
runtime: OrcaRuntimeService,
db: OrchestrationDb,
dispatchId: string
): Promise<{
terminal: Awaited<ReturnType<OrcaRuntimeService['showTerminal']>> | null
exact: boolean
status: 'unattached' | 'missing' | 'identity_changed' | 'running' | 'exited'
}> {
const worker = db.getWorkerDispatch(dispatchId)
if (!worker?.agent_terminal_handle) {
return { terminal: null, exact: false, status: 'unattached' }
}
const terminal = await runtime.showTerminal(worker.agent_terminal_handle).catch(() => null)
if (!terminal) {
return { terminal: null, exact: false, status: 'missing' }
}
const exact = db.isDispatchProcessCurrent({
dispatchId,
paneKey: runtime.getTerminalPaneKey(worker.agent_terminal_handle),
processIncarnation: runtime.getTerminalProcessIncarnation(worker.agent_terminal_handle)
})
return {
terminal,
exact,
status: exact ? (terminal.connected === false ? 'exited' : 'running') : 'identity_changed'
}
}
export function exposeWorker(worker: WorkerDispatchRow) {
return {
...worker,
effects: JSON.parse(worker.effects) as unknown[],
residualResources: JSON.parse(worker.residual_resources) as unknown[],
startOptions: JSON.parse(worker.start_options) as unknown
}
}
export function resolvePinnedFederatedServer(
runtime: OrcaRuntimeService,
federated: FederatedDispatchRow
) {
const server = runtime.resolveOrchestrationWorkerServer(federated.environment_id)
if (server.peerFingerprint !== federated.peer_fingerprint) {
throw new OrchestrationError(
'peer_changed',
`Saved environment ${federated.environment_name} now identifies a different Orca server.`
)
}
return server
}
export async function callFederatedWorkerShow(
runtime: OrcaRuntimeService,
federated: FederatedDispatchRow
): Promise<{
runtimeEpoch: string
attachment: {
state: string
stage: string
last_error: string | null
worktree_id: string | null
terminal_handle: string | null
setup_state: string
effects: unknown[]
residualResources: unknown[]
}
terminal: unknown
observation: { status: string; exactWorker: boolean }
}> {
return (await runtime.callOrchestrationWorkerServer(
federated.environment_id,
'orchestration.federationShow',
{ dispatchId: federated.dispatch_id },
15_000
)) as Awaited<ReturnType<typeof callFederatedWorkerShow>>
}
@@ -0,0 +1,205 @@
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { OrcaRuntimeService } from '../../orca-runtime'
import { readExactWorkerOutput } from './orchestration-worker-output'
function codexMessage(id: string, text: string): string {
return JSON.stringify({
type: 'event_msg',
payload: { id, type: 'agent_message', message: text }
})
}
describe('exact orchestration worker output', () => {
let directory: string
let transcriptA: string
let transcriptB: string
let providerSession: ReturnType<OrcaRuntimeService['getExactWorkerProviderSession']>
let runtime: OrcaRuntimeService
const readTerminal = vi.fn()
beforeEach(async () => {
directory = await mkdtemp(join(tmpdir(), 'orca-worker-output-'))
transcriptA = join(directory, 'session-a.jsonl')
transcriptB = join(directory, 'session-b.jsonl')
await writeFile(transcriptA, `${codexMessage('a', 'worker A only')}\n`)
await writeFile(transcriptB, `${codexMessage('b', 'worker B only')}\n`)
providerSession = {
paneKey: 'tab:worker',
processIncarnation: 'pty:incarnation-1',
agent: 'codex',
providerSession: {
key: 'session_id',
id: 'session-a',
transcriptPath: transcriptA
},
observedAt: Date.now()
}
readTerminal.mockReset()
readTerminal.mockResolvedValue({
handle: 'term_worker',
status: 'running',
tail: ['terminal output'],
truncated: false,
nextCursor: '9'
})
runtime = {
getExactWorkerProviderSession: vi.fn(() => providerSession),
getTerminalProcessIncarnation: vi.fn(() => 'pty:incarnation-1'),
getTerminalPaneKey: vi.fn(() => 'tab:worker'),
readTerminal
} as unknown as OrcaRuntimeService
})
afterEach(async () => {
await rm(directory, { recursive: true, force: true })
})
const read = (overrides: Partial<Parameters<typeof readExactWorkerOutput>[0]> = {}) =>
readExactWorkerOutput({
runtime,
dispatchId: 'dispatch_1',
terminalHandle: 'term_worker',
workerState: 'ready',
terminalStatus: 'running',
attachedAt: '2026-07-24 00:00:00',
...overrides
})
it('reads only the exact pane session and keeps its local path private', async () => {
const result = await read()
expect(result).toMatchObject({
source: 'transcript',
provider: 'codex',
transcript: {
messages: [{ id: 'a', blocks: [{ type: 'text', text: 'worker A only' }] }]
}
})
expect(JSON.stringify(result)).not.toContain(transcriptA)
expect(JSON.stringify(result)).not.toContain('worker B only')
expect(readTerminal).not.toHaveBeenCalled()
})
it('reads Grok through the shared Native Chat transcript decoder', async () => {
await writeFile(
transcriptA,
`${JSON.stringify({
id: 'grok-a',
type: 'assistant',
content: 'Grok worker only'
})}\n`
)
providerSession = {
...providerSession!,
agent: 'grok',
providerSession: {
key: 'session_id',
id: 'session-grok',
transcriptPath: transcriptA
}
}
const result = await read()
expect(result).toMatchObject({
source: 'transcript',
provider: 'grok',
transcript: {
messages: [{ role: 'assistant', blocks: [{ type: 'text', text: 'Grok worker only' }] }]
}
})
expect(readTerminal).not.toHaveBeenCalled()
})
it('labels OpenCode as a terminal fallback when no transcript decoder exists', async () => {
const capability = `dcap_${'A'.repeat(43)}`
readTerminal.mockResolvedValue({
handle: 'term_worker',
status: 'running',
tail: [`opencode --dispatch-capability ${capability}`],
truncated: false,
nextCursor: '9'
})
providerSession = {
...providerSession!,
agent: 'opencode',
providerSession: {
key: 'session_id',
id: 'session-opencode',
transcriptPath: transcriptA
}
}
const result = await read()
expect(result).toMatchObject({
source: 'terminal',
fallbackReason: 'provider_unsupported',
terminal: { tail: ['opencode --dispatch-capability [dispatch capability redacted]'] },
warnings: ['Dispatch capability tokens were redacted from terminal output.']
})
expect(JSON.stringify(result)).not.toContain(capability)
})
it('rejects an old cursor after the exact provider session changes', async () => {
const initial = await read()
if (initial.source !== 'transcript') {
throw new Error('Expected transcript output')
}
providerSession = {
...providerSession!,
providerSession: {
key: 'session_id',
id: 'session-b',
transcriptPath: transcriptB
}
}
await expect(read({ cursor: initial.cursor })).rejects.toMatchObject({
code: 'source_changed'
})
})
it('uses a labeled terminal fallback and keeps its cursor pinned', async () => {
providerSession = null
const fallback = await read()
expect(fallback).toMatchObject({
source: 'terminal',
fallbackReason: 'session_not_reported',
terminal: { tail: ['terminal output'] }
})
expect(fallback.cursor).toMatch(/^owr1_/)
providerSession = {
paneKey: 'tab:worker',
processIncarnation: 'pty:incarnation-1',
agent: 'codex',
providerSession: {
key: 'session_id',
id: 'session-a',
transcriptPath: transcriptA
},
observedAt: Date.now()
}
await read({ cursor: fallback.cursor ?? undefined })
expect(readTerminal).toHaveBeenLastCalledWith('term_worker', {
cursor: 9,
limit: undefined
})
})
it('fails instead of falling back when transcript output is required', async () => {
providerSession = null
await expect(read({ source: 'transcript' })).rejects.toMatchObject({
code: 'transcript_required',
data: { reason: 'session_not_reported' }
})
expect(readTerminal).not.toHaveBeenCalled()
})
})
@@ -0,0 +1,208 @@
import type {
OrchestrationWorkerReadFallbackReason,
OrchestrationWorkerReadResult,
OrchestrationWorkerReadSource
} from '../../../../shared/orchestration-worker-output'
import type { RuntimeTerminalState } from '../../../../shared/runtime-types'
import type { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationError } from '../../orchestration/orchestration-error'
import {
createWorkerOutputSourceIdentity,
decodeWorkerOutputCursor,
encodeWorkerOutputCursor
} from '../../orchestration/worker-output-cursor'
import { redactWorkerTerminalLines } from '../../orchestration/worker-transcript-payload'
import { readWorkerTranscript } from '../../orchestration/worker-transcript-read'
export async function readExactWorkerOutput(args: {
runtime: OrcaRuntimeService
dispatchId: string
terminalHandle: string
workerState: string
terminalStatus: RuntimeTerminalState
attachedAt: string
source?: OrchestrationWorkerReadSource
cursor?: string | number
limit?: number
}): Promise<OrchestrationWorkerReadResult> {
const source = args.source ?? 'auto'
const cursor = decodeWorkerOutputCursor(args.cursor, args.dispatchId)
assertCursorSourceMatchesRequest(cursor?.source, source)
if (cursor?.source === 'terminal' || source === 'terminal') {
return readTerminalOutput(args, cursor)
}
const observedAfter = orchestrationTimestampToMs(args.attachedAt)
const session = args.runtime.getExactWorkerProviderSession(args.terminalHandle, observedAfter)
if (!session) {
if (cursor?.source === 'transcript') {
throw sourceChanged()
}
return fallbackOrThrow(args, 'session_not_reported')
}
const transcript = await readWorkerTranscript({
agent: session.agent,
sessionId: session.providerSession.id,
transcriptPath: session.providerSession.transcriptPath,
offset: cursor?.source === 'transcript' ? cursor.position : undefined,
limit: args.limit
})
if (!transcript.ok) {
if (transcript.reason === 'source_changed') {
throw sourceChanged()
}
if (cursor?.source === 'transcript') {
throw transcriptRequired(args.dispatchId, transcript.reason)
}
return fallbackOrThrow(args, transcript.reason, transcript.warnings)
}
const sourceIdentity = createWorkerOutputSourceIdentity([
'transcript',
session.processIncarnation,
session.agent,
session.providerSession.key,
session.providerSession.id,
transcript.filePath
])
if (cursor?.source === 'transcript' && cursor.sourceIdentity !== sourceIdentity) {
throw sourceChanged()
}
const sessionAfterRead = args.runtime.getExactWorkerProviderSession(
args.terminalHandle,
observedAfter
)
if (
!sessionAfterRead ||
sessionAfterRead.processIncarnation !== session.processIncarnation ||
sessionAfterRead.agent !== session.agent ||
sessionAfterRead.providerSession.key !== session.providerSession.key ||
sessionAfterRead.providerSession.id !== session.providerSession.id ||
sessionAfterRead.providerSession.transcriptPath !== session.providerSession.transcriptPath
) {
throw sourceChanged()
}
const nextCursor = encodeWorkerOutputCursor(
args.dispatchId,
'transcript',
sourceIdentity,
transcript.nextOffset
)
return {
dispatchId: args.dispatchId,
source: 'transcript',
sourceIdentity,
provider: session.agent,
transcript: {
messages: transcript.messages,
nextCursor,
limited: transcript.limited,
returnedMessageCount: transcript.messages.length
},
cursor: nextCursor,
status: { worker: args.workerState, terminal: args.terminalStatus },
fallbackReason: null,
warnings: transcript.warnings
}
}
async function readTerminalOutput(
args: Parameters<typeof readExactWorkerOutput>[0],
cursor: ReturnType<typeof decodeWorkerOutputCursor>
): Promise<OrchestrationWorkerReadResult> {
const processIncarnation = args.runtime.getTerminalProcessIncarnation(args.terminalHandle)
const paneKey = args.runtime.getTerminalPaneKey(args.terminalHandle)
if (!processIncarnation || !paneKey) {
throw new OrchestrationError(
'worker_identity_changed',
`Worker Dispatch ${args.dispatchId} no longer resolves to its exact process.`
)
}
const sourceIdentity = createWorkerOutputSourceIdentity(['terminal', processIncarnation, paneKey])
if (
cursor?.source === 'terminal' &&
cursor.sourceIdentity !== null &&
cursor.sourceIdentity !== sourceIdentity
) {
throw sourceChanged()
}
const terminal = await args.runtime.readTerminal(args.terminalHandle, {
cursor: cursor?.source === 'terminal' ? cursor.position : undefined,
limit: args.limit
})
const redactedTerminal = redactWorkerTerminalLines(terminal.tail)
const position =
terminal.nextCursor !== null && /^\d+$/.test(terminal.nextCursor)
? Number.parseInt(terminal.nextCursor, 10)
: null
const nextCursor =
position === null
? null
: encodeWorkerOutputCursor(args.dispatchId, 'terminal', sourceIdentity, position)
return {
dispatchId: args.dispatchId,
source: 'terminal',
sourceIdentity,
terminal: { ...terminal, tail: redactedTerminal.lines },
cursor: nextCursor,
status: { worker: args.workerState, terminal: terminal.status },
fallbackReason: null,
warnings: redactedTerminal.warnings
}
}
async function fallbackOrThrow(
args: Parameters<typeof readExactWorkerOutput>[0],
reason: OrchestrationWorkerReadFallbackReason,
warnings: string[] = []
): Promise<OrchestrationWorkerReadResult> {
if (args.source === 'transcript') {
throw transcriptRequired(args.dispatchId, reason)
}
const fallback = await readTerminalOutput(args, null)
return fallback.source === 'terminal'
? {
...fallback,
fallbackReason: reason,
warnings: [...new Set([...fallback.warnings, ...warnings])]
}
: fallback
}
function assertCursorSourceMatchesRequest(
cursorSource: 'terminal' | 'transcript' | undefined,
requestedSource: OrchestrationWorkerReadSource
): void {
if (cursorSource && requestedSource !== 'auto' && cursorSource !== requestedSource) {
throw new OrchestrationError(
'cursor_invalid',
`The worker-read cursor is pinned to ${cursorSource} output.`
)
}
}
function orchestrationTimestampToMs(value: string): number {
const normalized = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/.test(value)
? `${value.replace(' ', 'T')}Z`
: value
const parsed = Date.parse(normalized)
return Number.isFinite(parsed) ? parsed : 0
}
function sourceChanged(): OrchestrationError {
return new OrchestrationError(
'source_changed',
'The worker output source changed. Start a fresh worker-read without the old cursor.'
)
}
function transcriptRequired(
dispatchId: string,
reason: OrchestrationWorkerReadFallbackReason
): OrchestrationError {
return new OrchestrationError(
'transcript_required',
`Structured output is unavailable for Dispatch ${dispatchId}: ${reason}.`,
{ reason }
)
}
@@ -0,0 +1,67 @@
import type { OrchestrationDb } from '../../orchestration/db'
import {
applyWaitForSetupOutcome,
type WorkerEffect,
type WorkerSetupReceipt
} from './orchestration-worker-topology'
function residualWorkerEffects(effects: WorkerEffect[]): WorkerEffect[] {
return effects.filter(
(effect) => effect.action?.startsWith('created') || effect.action === 'reused_agent_terminal'
)
}
type WorkerSetupStageArgs = {
db: OrchestrationDb
dispatchId: string
worktreeId: string
terminalHandle: string
setup: WorkerSetupReceipt
effects: WorkerEffect[]
}
export function persistWorkerReadinessStage(args: WorkerSetupStageArgs): void {
args.db.recordWorkerStage({
dispatchId: args.dispatchId,
stage: 'terminal_readying',
worktreeId: args.worktreeId,
terminalHandle: args.terminalHandle,
setupState: args.setup.state,
effects: args.effects,
residualResources: residualWorkerEffects(args.effects)
})
}
export function persistGatedSetupSpawnFailure(args: WorkerSetupStageArgs): boolean {
if (args.setup.startupPolicy !== 'wait-for-setup' || args.setup.state !== 'spawn_failed') {
return false
}
args.db.recordWorkerStage({
dispatchId: args.dispatchId,
stage: 'setup_start',
worktreeId: args.worktreeId,
terminalHandle: args.terminalHandle,
setupState: args.setup.state,
effects: args.effects,
residualResources: residualWorkerEffects(args.effects)
})
return true
}
export function persistWorkerSetupWaitOutcome(
args: WorkerSetupStageArgs & { wait: { satisfied: boolean; status: string } }
): void {
applyWaitForSetupOutcome(args.setup, args.effects, args.wait)
if (args.setup.startupPolicy !== 'wait-for-setup') {
return
}
args.db.recordWorkerStage({
dispatchId: args.dispatchId,
stage: args.setup.state === 'failed' ? 'setup_failed' : 'setup_settled',
worktreeId: args.worktreeId,
terminalHandle: args.terminalHandle,
setupState: args.setup.state,
effects: args.effects,
residualResources: residualWorkerEffects(args.effects)
})
}
@@ -0,0 +1,41 @@
import type { OrchestrationDb } from '../../orchestration/db'
import {
isUnknownWorkerStartOutcome,
type WorkerSetupReceipt
} from './orchestration-worker-topology'
export function failWorkerStartWithReceipt(args: {
db: OrchestrationDb
runId: string
taskId: string
dispatchId: string
failedStage: string
error: unknown
setup: WorkerSetupReceipt
}): unknown {
const reason = args.error instanceof Error ? args.error.message : String(args.error)
const unknown = isUnknownWorkerStartOutcome(args.error, args.failedStage)
const worker = unknown
? args.db.markWorkerStartUnknown(args.dispatchId, args.failedStage, reason)
: args.db.failWorkerStart(args.dispatchId, args.failedStage, reason)
return {
runId: args.runId,
taskId: args.taskId,
dispatchId: args.dispatchId,
state: worker.state === 'start_unknown' ? 'outcome_unknown' : worker.state,
stage: worker.stage,
failedStage: args.failedStage,
lastError: reason,
setup: args.setup,
effects: JSON.parse(worker.effects) as unknown[],
residualResources: JSON.parse(worker.residual_resources) as unknown[],
...(unknown
? {
nextCommands: [
`orca orchestration worker-show --dispatch ${args.dispatchId} --json`,
`orca orchestration worker-abandon --dispatch ${args.dispatchId} --json`
]
}
: {})
}
}
@@ -0,0 +1,23 @@
import { z } from 'zod'
import { OptionalFiniteNumber, OptionalString, requiredString } from '../schemas'
export const WorkerStartParams = z.object({
task: requiredString('Missing --task'),
on: OptionalString,
run: OptionalString,
from: requiredString('Missing --from'),
worktree: OptionalString,
name: OptionalString,
repo: OptionalString,
baseBranch: OptionalString,
displayName: OptionalString,
comment: OptionalString,
setup: z.enum(['run', 'skip', 'inherit']).optional(),
terminal: OptionalString,
agent: OptionalString,
retryOf: OptionalString,
timeoutMs: OptionalFiniteNumber,
devMode: z.boolean().optional()
})
export type WorkerStartInput = z.infer<typeof WorkerStartParams>
@@ -0,0 +1,148 @@
import { z } from 'zod'
import { OrchestrationError } from '../../orchestration/orchestration-error'
import { syncFederatedDispatch } from '../../orchestration/federation-sync'
import { defineMethod, type RpcMethod } from '../core'
import { requiredString } from '../schemas'
import {
inspectWorkerTerminal,
resolvePinnedFederatedServer
} from './orchestration-worker-observation'
const WorkerDispatchParams = z.object({ dispatch: requiredString('Missing --dispatch') })
export const ORCHESTRATION_WORKER_STOP_METHODS: RpcMethod[] = [
defineMethod({
name: 'orchestration.workerStop',
params: WorkerDispatchParams,
handler: async (params, { runtime, orchestrationMutation }) => {
const db = runtime.getOrchestrationDb()
const federated = db.getFederatedDispatch(params.dispatch)
if (federated) {
if (!orchestrationMutation) {
throw new OrchestrationError(
'invalid_argument',
'Remote worker-stop requires a durable retry request.'
)
}
const server = resolvePinnedFederatedServer(runtime, federated)
const begun = db.beginWorkerStop(params.dispatch)
if (begun.disposition === 'already_settled') {
return settledReceipt(params.dispatch, begun.worker.state)
}
try {
const remote = (await runtime.callOrchestrationWorkerServer(
server.environmentId,
'orchestration.federationStop',
{ dispatchId: params.dispatch },
30_000,
{ orchestrationRequestId: orchestrationMutation.requestId }
)) as RemoteStopReceipt
if (remote.state === 'stopped') {
const worker = db.reconcileFederatedWorkerStop(params.dispatch)
return {
dispatchId: params.dispatch,
state: worker.state,
alreadySettled: remote.alreadySettled,
processAction: remote.processAction,
close: remote.close
}
}
if (remote.state === 'succeeded' || remote.state === 'failed') {
db.resumeFederatedWorkerForTerminalRelay(params.dispatch)
await syncFederatedDispatch(runtime, params.dispatch).catch(() => undefined)
return {
dispatchId: params.dispatch,
state: db.getWorkerDispatch(params.dispatch)?.state ?? remote.state,
alreadySettled: true,
processAction: 'none'
}
}
return unknownReceipt(
params.dispatch,
db.markWorkerStopUnknown(
params.dispatch,
remote.lastError ?? `The worker server returned ${remote.state}.`
),
remote.processAction
)
} catch (error) {
const reason = error instanceof Error ? error.message : String(error)
return unknownReceipt(
params.dispatch,
db.markWorkerStopUnknown(params.dispatch, reason),
'unknown'
)
}
}
const begun = db.beginWorkerStop(params.dispatch)
if (begun.disposition === 'already_settled') {
return settledReceipt(params.dispatch, begun.worker.state)
}
const handle = begun.worker.agent_terminal_handle
if (!handle) {
return unknownReceipt(
params.dispatch,
db.markWorkerStopUnknown(params.dispatch, 'The Dispatch has no recorded agent terminal.'),
'unknown'
)
}
const observation = await inspectWorkerTerminal(runtime, db, params.dispatch)
if (!observation.exact || observation.status !== 'running') {
return unknownReceipt(
params.dispatch,
db.markWorkerStopUnknown(
params.dispatch,
`The recorded worker process is ${observation.status}; no terminal was closed.`
),
'none'
)
}
try {
const close = await runtime.closeTerminal(handle)
const worker = db.settleWorkerStop(params.dispatch)
runtime.notifyMessageArrived(`dispatch:${params.dispatch}`, 'status')
return {
dispatchId: params.dispatch,
state: worker.state,
alreadySettled: false,
processAction: 'closed_agent_terminal',
close
}
} catch (error) {
const reason = error instanceof Error ? error.message : String(error)
return unknownReceipt(
params.dispatch,
db.markWorkerStopUnknown(params.dispatch, reason),
'unknown'
)
}
}
})
]
type RemoteStopReceipt = {
state: string
alreadySettled: boolean
processAction: string
close?: unknown
lastError?: string | null
}
function settledReceipt(dispatchId: string, state: string) {
return { dispatchId, state, alreadySettled: true, processAction: 'none' }
}
function unknownReceipt(
dispatchId: string,
worker: { state: string; last_error: string | null },
processAction: string
) {
return {
dispatchId,
state: worker.state,
alreadySettled: false,
processAction,
lastError: worker.last_error
}
}
@@ -0,0 +1,228 @@
import type { TuiAgent } from '../../../../shared/types'
import type { OrcaRuntimeService } from '../../orca-runtime'
import type { OrchestrationDb } from '../../orchestration/db'
export type WorkerEffect = {
kind: 'worktree' | 'terminal' | 'setup' | 'dispatch_input'
action?: string
role?: string
id?: string
state?: string
tabId?: string
leafId?: string
requested?: string
effective?: string
source?: string
hookFound?: boolean
startupPolicy?: string
terminalId?: string
}
export type WorkerSetupReceipt = {
requested: 'run' | 'skip' | 'inherit' | 'not_applicable'
effective: 'run' | 'skip' | 'inherit' | 'not_applicable'
source: string
hookFound: boolean
startupPolicy: 'start-immediately' | 'wait-for-setup'
state:
| 'running'
| 'succeeded'
| 'failed'
| 'skipped'
| 'not_configured'
| 'spawn_failed'
| 'not_applicable'
}
export function applyWaitForSetupOutcome(
receipt: WorkerSetupReceipt,
effects: WorkerEffect[],
wait: { satisfied: boolean; status: string }
): void {
if (receipt.startupPolicy !== 'wait-for-setup' || receipt.state !== 'running') {
return
}
if (wait.satisfied) {
receipt.state = 'succeeded'
} else if (wait.status === 'exited') {
receipt.state = 'failed'
} else {
return
}
const setupEffect = effects.find((effect) => effect.kind === 'setup')
if (setupEffect) {
setupEffect.state = receipt.state
}
}
export async function createWorkerWorktree(args: {
runtime: OrcaRuntimeService
db: OrchestrationDb
dispatchId: string
requestedWorktree: string
coordinatorWorktree: Awaited<ReturnType<OrcaRuntimeService['showManagedWorktree']>>
params: {
repo?: string
name?: string
baseBranch?: string
displayName?: string
comment?: string
setup?: 'run' | 'skip' | 'inherit'
from: string
}
agent: TuiAgent
effects: WorkerEffect[]
}): Promise<{
worktree: Awaited<ReturnType<OrcaRuntimeService['showManagedWorktree']>>
terminalHandle: string
setupReceipt: WorkerSetupReceipt
}> {
const { runtime, db, dispatchId, requestedWorktree, coordinatorWorktree, params, effects } = args
const setupDecision = params.setup ?? 'run'
db.recordWorkerStage({ dispatchId, stage: 'worktree_creating', effects })
const created = await runtime.createManagedWorktree({
repoSelector: params.repo ?? coordinatorWorktree.repoId,
name: params.name as string,
baseBranch: params.baseBranch,
displayName: params.displayName,
comment: params.comment,
runHooks: setupDecision === 'run',
setupDecision,
awaitTerminalProvisioning: true,
observeSetupCompletion: true,
createdWithAgent: args.agent,
startupAgent: args.agent,
activate: false,
lineage: {
parentWorktree: requestedWorktree === 'new-child' ? coordinatorWorktree.id : undefined,
noParent: requestedWorktree === 'new-top-level',
callerTerminalHandle: params.from
}
})
const terminalHandle = created.startupTerminal?.handle
effects.push({
kind: 'worktree',
action: requestedWorktree === 'new-child' ? 'created_child' : 'created_top_level',
id: created.worktree.id
})
db.recordWorkerStage({
dispatchId,
stage: 'worktree_created',
worktreeId: created.worktree.id,
effects,
residualResources: effects
})
const setupReceipt = {
requested: setupDecision,
effective: setupDecision,
source: params.setup ? 'explicit_request' : 'orchestration_default',
hookFound: created.setupReceipt?.hookFound ?? false,
startupPolicy: created.setupReceipt?.startupPolicy ?? 'start-immediately',
state: created.setupReceipt?.state ?? 'not_configured'
}
if (!terminalHandle) {
throw new Error(created.warning ?? 'Agent-first worktree creation returned no terminal.')
}
const listed = await runtime.listTerminals(`id:${created.worktree.id}`)
const setupTerminalHandle = created.setupReceipt?.terminalHandle
for (const terminal of listed.terminals) {
effects.push({
kind: 'terminal',
role:
terminal.handle === terminalHandle
? 'agent'
: terminal.handle === setupTerminalHandle
? 'setup'
: 'configured_tab',
action: terminal.handle === terminalHandle ? 'reused_agent_terminal' : 'created',
id: terminal.handle,
tabId: terminal.tabId,
leafId: terminal.leafId
})
}
const setupTerminal = effects.find(
(effect) => effect.kind === 'terminal' && effect.role === 'setup'
)
effects.push({
kind: 'setup',
action: setupDecision,
requested: setupReceipt.requested,
effective: setupReceipt.effective,
source: setupReceipt.source,
hookFound: setupReceipt.hookFound,
startupPolicy: setupReceipt.startupPolicy,
state: setupReceipt.state,
terminalId: setupTerminalHandle ?? setupTerminal?.id
})
return {
worktree: created.worktree as Awaited<ReturnType<OrcaRuntimeService['showManagedWorktree']>>,
terminalHandle,
setupReceipt
}
}
export function monitorWorkerSetup(args: {
runtime: OrcaRuntimeService
db: OrchestrationDb
runId: string
dispatchId: string
setupReceipt: WorkerSetupReceipt
effects: WorkerEffect[]
}): void {
const setupTerminal = args.effects.find(
(effect) => effect.kind === 'terminal' && effect.role === 'setup' && effect.id
)
if (
!setupTerminal?.id ||
args.setupReceipt.startupPolicy !== 'start-immediately' ||
args.setupReceipt.state !== 'running'
) {
return
}
// Why: setup is intentionally non-gating, but command completion remains durable evidence.
void args.runtime
.waitForSetupTerminalCompletion(setupTerminal.id)
.then((completion) => {
const setupState = completion.exitCode === 0 ? 'succeeded' : 'failed'
const evidence = args.db.updateWorkerSetupEvidence({
dispatchId: args.dispatchId,
setupState,
effects: args.effects.map((effect) =>
effect.kind === 'setup' ? { ...effect, state: setupState } : effect
)
})
if (!evidence.changed) {
return
}
const message = args.db.insertMessage({
runId: args.runId,
from: `dispatch:${args.dispatchId}`,
to: `run:${args.runId}`,
subject: `Setup ${setupState} for worker ${args.dispatchId}`,
type: 'status',
priority: setupState === 'failed' ? 'high' : 'normal',
payload: JSON.stringify({
dispatchId: args.dispatchId,
setupState,
terminalHandle: setupTerminal.id
})
})
args.runtime.notifyMessageArrived(message.to_handle, message.type)
})
.catch(() => undefined)
}
export function isUnknownWorkerStartOutcome(error: unknown, stage: string): boolean {
const code =
error && typeof error === 'object' && typeof (error as { code?: unknown }).code === 'string'
? (error as { code: string }).code
: ''
if (code === 'operation_unknown') {
return true
}
if (stage !== 'worktree_create') {
return false
}
const message = error instanceof Error ? error.message : String(error)
return /connection|disconnect|timed?\s*out|runtime changed|outcome unknown/i.test(message)
}
@@ -0,0 +1,614 @@
import { createHash } from 'node:crypto'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../../shared/protocol-version'
import { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationDb } from '../../orchestration/db'
import { RpcDispatcher } from '../dispatcher'
import type { RpcRequest } from '../core'
import { ORCHESTRATION_METHODS } from './orchestration'
describe('orchestration new-worktree workers', () => {
type CreateWorktreeResult = Awaited<ReturnType<OrcaRuntimeService['createManagedWorktree']>>
const coordinatorPaneKey = 'tab_coord:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
let db: OrchestrationDb
let runtime: OrcaRuntimeService
let runId: string
beforeEach(() => {
db = new OrchestrationDb(':memory:')
runtime = new OrcaRuntimeService()
runtime.setOrchestrationDb(db)
runId = db.createRun({
objective: 'Test new-worktree workers',
coordinatorHandle: 'term_coord',
coordinatorPaneKey
}).id
vi.spyOn(runtime, 'getTerminalPaneKey').mockImplementation((handle) =>
handle === 'term_coord'
? coordinatorPaneKey
: handle === 'term_worker'
? 'tab_worker:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
: null
)
vi.spyOn(runtime, 'getTerminalProcessIncarnation').mockImplementation((handle) =>
handle === 'term_worker' ? 'runtime_test:term_worker:1' : null
)
vi.spyOn(runtime, 'validateOrchestrationAgentLauncher').mockImplementation(() => {})
vi.spyOn(runtime, 'showTerminal').mockResolvedValue({
handle: 'term_coord',
worktreeId: 'repo::parent',
status: 'running'
} as never)
vi.spyOn(runtime, 'showManagedWorktree').mockResolvedValue({
id: 'repo::parent',
repoId: 'repo'
} as never)
vi.spyOn(runtime, 'showRepo').mockResolvedValue({
id: 'repo',
kind: 'git'
} as never)
vi.spyOn(runtime, 'createTerminal')
vi.spyOn(runtime, 'listTerminals').mockResolvedValue({
terminals: [{ handle: 'term_worker', title: 'Codex' }],
totalCount: 1,
truncated: false
} as never)
vi.spyOn(runtime, 'waitForTerminal').mockResolvedValue({
handle: 'term_worker',
condition: 'tui-idle',
satisfied: true,
status: 'running',
exitCode: null
})
vi.spyOn(runtime, 'waitForSetupTerminalCompletion').mockReturnValue(
new Promise(() => undefined)
)
vi.spyOn(runtime, 'getTerminalOrchestrationCliCommand').mockReturnValue('orca')
vi.spyOn(runtime, 'sendTerminalAgentPrompt').mockResolvedValue({
handle: 'term_worker',
accepted: true,
bytesWritten: 1
})
})
afterEach(() => db.close())
async function startWorker(overrides: Record<string, unknown> = {}) {
const task = db.createTask({ spec: 'new-worktree task', runId })
const method = ORCHESTRATION_METHODS.find(
(candidate) => candidate.name === 'orchestration.workerStart'
)
if (!method) {
throw new Error('workerStart method is not registered')
}
const params = method.params!.parse({
task: task.id,
from: 'term_coord',
worktree: 'new-child',
name: 'new-worker',
agent: 'codex',
...overrides
})
const result = await method.handler(params, { runtime })
return { result, task }
}
function mockCreatedWorktree(options?: {
hookFound?: boolean
startupPolicy?: 'start-immediately' | 'wait-for-setup'
state?: 'running' | 'skipped' | 'not_configured' | 'spawn_failed'
terminals?: { handle: string; title: string }[]
setupTerminalHandle?: string
}) {
const hookFound = options?.hookFound ?? true
const state = options?.state ?? (hookFound ? 'running' : 'not_configured')
vi.spyOn(runtime, 'createManagedWorktree').mockResolvedValue({
worktree: { id: 'repo::created', repoId: 'repo' },
startupTerminal: { spawned: true, handle: 'term_worker' },
setupReceipt: {
requested: state === 'skipped' ? 'skip' : 'run',
hookFound,
startupPolicy: options?.startupPolicy ?? 'start-immediately',
state,
terminalHandle:
options?.setupTerminalHandle ??
options?.terminals?.find((terminal) => terminal.title === 'Setup')?.handle
}
} as never)
if (options?.terminals) {
vi.mocked(runtime.listTerminals).mockResolvedValue({
terminals: options.terminals,
totalCount: options.terminals.length,
truncated: false
} as never)
}
}
it('creates an independent top-level worktree and reuses its agent terminal', async () => {
mockCreatedWorktree()
const { result } = await startWorker({ worktree: 'new-top-level' })
expect(runtime.createManagedWorktree).toHaveBeenCalledWith(
expect.objectContaining({
startupAgent: 'codex',
awaitTerminalProvisioning: true,
observeSetupCompletion: true,
lineage: expect.objectContaining({ noParent: true, parentWorktree: undefined })
})
)
expect(result).toMatchObject({ state: 'ready' })
expect(result).toHaveProperty(
'effects',
expect.arrayContaining([
expect.objectContaining({
kind: 'worktree',
action: 'created_top_level',
id: 'repo::created'
}),
expect.objectContaining({
kind: 'terminal',
role: 'agent',
action: 'reused_agent_terminal',
id: 'term_worker'
})
])
)
expect(runtime.createTerminal).not.toHaveBeenCalled()
})
it('rejects a new worktree for a folder project before creating effects', async () => {
vi.mocked(runtime.showRepo).mockResolvedValue({
id: 'repo',
kind: 'folder'
} as never)
const createWorktree = vi.spyOn(runtime, 'createManagedWorktree')
const task = db.createTask({ spec: 'folder task', runId })
const method = ORCHESTRATION_METHODS.find(
(candidate) => candidate.name === 'orchestration.workerStart'
)
if (!method) {
throw new Error('workerStart method is not registered')
}
await expect(
method.handler(
method.params!.parse({
task: task.id,
from: 'term_coord',
worktree: 'new-child',
name: 'folder-worker',
agent: 'codex'
}),
{ runtime }
)
).rejects.toMatchObject({
code: 'invalid_argument',
message:
'Folder projects cannot create orchestration worktrees; use current or an exact existing folder workspace.'
})
expect(createWorktree).not.toHaveBeenCalled()
expect(db.getTask(task.id)?.status).toBe('ready')
expect(db.getDispatchContext(task.id)).toBeUndefined()
})
it('injects the execution host CLI command and Dispatch capability together', async () => {
mockCreatedWorktree()
vi.mocked(runtime.getTerminalOrchestrationCliCommand).mockReturnValue('orca-ide')
await startWorker({ worktree: 'new-top-level' })
const prompt = vi.mocked(runtime.sendTerminalAgentPrompt).mock.calls[0]?.[1] ?? ''
expect(prompt).toContain('orca-ide orchestration send')
expect(prompt).toMatch(/--dispatch-capability dcap_[A-Za-z0-9_-]+/)
expect(prompt).not.toMatch(/(^|\s)orca orchestration send/)
})
it('passes exact repo, base, metadata, lineage, and setup choices to worktree creation', async () => {
mockCreatedWorktree({ state: 'skipped' })
await startWorker({
worktree: 'new-top-level',
repo: 'id:repo-explicit',
baseBranch: 'origin/release',
displayName: 'Windows release audit',
comment: 'Created for a supervised audit',
setup: 'skip'
})
expect(runtime.createManagedWorktree).toHaveBeenCalledWith(
expect.objectContaining({
repoSelector: 'id:repo-explicit',
baseBranch: 'origin/release',
displayName: 'Windows release audit',
comment: 'Created for a supervised audit',
setupDecision: 'skip',
runHooks: false,
lineage: expect.objectContaining({ noParent: true, parentWorktree: undefined })
})
)
})
it('reports an absent setup hook as not configured without failing the start', async () => {
mockCreatedWorktree({ hookFound: false })
const { result } = await startWorker()
expect(result).toMatchObject({
state: 'ready',
setup: {
requested: 'run',
effective: 'run',
source: 'orchestration_default',
hookFound: false,
state: 'not_configured'
}
})
})
it.each([
['skip', 'skipped'],
['inherit', 'not_configured'],
['run', 'running']
] as const)('passes explicit setup=%s through with a truthful receipt', async (setup, state) => {
mockCreatedWorktree({ hookFound: setup === 'run', state })
const { result } = await startWorker({ setup })
expect(runtime.createManagedWorktree).toHaveBeenCalledWith(
expect.objectContaining({ setupDecision: setup, runHooks: setup === 'run' })
)
expect(result).toMatchObject({
state: 'ready',
setup: { requested: setup, effective: setup, source: 'explicit_request', state }
})
})
it('records a later setup failure without gating a start-immediately worker', async () => {
mockCreatedWorktree({
terminals: [
{ handle: 'term_worker', title: 'Codex' },
{ handle: 'term_setup', title: 'Setup' }
]
})
let finishSetup: ((result: { exitCode: number | null }) => void) | undefined
vi.mocked(runtime.waitForSetupTerminalCompletion).mockImplementation(
async () =>
await new Promise((resolve) => {
finishSetup = resolve
})
)
const { result, task } = await startWorker()
const dispatchId = (result as { dispatchId: string }).dispatchId
expect(result).toMatchObject({ state: 'ready', setup: { state: 'running' } })
expect(
db.settleWorkerReport({
taskId: task.id,
dispatchId,
outcome: 'succeeded',
result: '{}'
})
).toMatchObject({ action: 'settled' })
finishSetup?.({ exitCode: 1 })
await vi.waitFor(() => expect(db.getWorkerDispatch(dispatchId)?.setup_state).toBe('failed'))
expect(db.getWorkerDispatch(dispatchId)).toMatchObject({
state: 'succeeded',
stage: 'settled',
setup_state: 'failed'
})
expect(JSON.parse(db.getWorkerDispatch(dispatchId)?.effects ?? '[]')).toEqual(
expect.arrayContaining([
expect.objectContaining({ kind: 'dispatch_input', state: 'accepted' })
])
)
expect(runtime.sendTerminalAgentPrompt).toHaveBeenCalledOnce()
expect(db.getInbox(10).filter((message) => message.run_id === runId)).toEqual(
expect.arrayContaining([expect.objectContaining({ type: 'status', priority: 'high' })])
)
})
it('uses the exact setup handle instead of a configured tab title', async () => {
mockCreatedWorktree({
setupTerminalHandle: 'term_actual_setup',
terminals: [
{ handle: 'term_worker', title: 'Codex' },
{ handle: 'term_configured_setup', title: 'Setup' },
{ handle: 'term_actual_setup', title: 'PowerShell' }
]
})
const { result } = await startWorker()
expect(result).toMatchObject({
effects: expect.arrayContaining([
expect.objectContaining({
kind: 'terminal',
id: 'term_configured_setup',
role: 'configured_tab'
}),
expect.objectContaining({ kind: 'terminal', id: 'term_actual_setup', role: 'setup' }),
expect.objectContaining({ kind: 'setup', terminalId: 'term_actual_setup' })
])
})
})
it('records wait-for-setup success before task input is accepted', async () => {
mockCreatedWorktree({ startupPolicy: 'wait-for-setup', state: 'running' })
const { result } = await startWorker()
const dispatchId = (result as { dispatchId: string }).dispatchId
expect(result).toMatchObject({
state: 'ready',
setup: { startupPolicy: 'wait-for-setup', state: 'succeeded' },
effects: expect.arrayContaining([
expect.objectContaining({ kind: 'setup', state: 'succeeded' }),
expect.objectContaining({ kind: 'dispatch_input', state: 'accepted' })
])
})
expect(vi.mocked(runtime.waitForTerminal).mock.invocationCallOrder[0]).toBeLessThan(
vi.mocked(runtime.sendTerminalAgentPrompt).mock.invocationCallOrder[0]!
)
expect(JSON.parse(db.getWorkerDispatch(dispatchId)?.effects ?? '[]')).toEqual(
expect.arrayContaining([
expect.objectContaining({ kind: 'dispatch_input', state: 'accepted' })
])
)
})
it('does not inject task input when the gated setup terminal fails to start', async () => {
mockCreatedWorktree({ startupPolicy: 'wait-for-setup', state: 'spawn_failed' })
vi.mocked(runtime.waitForTerminal).mockResolvedValue({
handle: 'term_worker',
condition: 'tui-idle',
satisfied: false,
status: 'exited',
exitCode: 1
})
const { result, task } = await startWorker()
expect(result).toMatchObject({
state: 'failed',
failedStage: 'setup_start',
setup: { startupPolicy: 'wait-for-setup', state: 'spawn_failed' },
effects: expect.arrayContaining([
expect.objectContaining({ kind: 'setup', state: 'spawn_failed' })
])
})
expect(db.getTask(task.id)?.status).toBe('failed')
expect(runtime.sendTerminalAgentPrompt).not.toHaveBeenCalled()
})
it('does not inject task input when the gated setup script fails', async () => {
mockCreatedWorktree({ startupPolicy: 'wait-for-setup', state: 'running' })
vi.mocked(runtime.waitForTerminal).mockResolvedValue({
handle: 'term_worker',
condition: 'tui-idle',
satisfied: false,
status: 'exited',
exitCode: 1
})
const { result } = await startWorker()
expect(result).toMatchObject({
state: 'failed',
failedStage: 'setup_wait',
setup: { state: 'failed' },
effects: expect.arrayContaining([expect.objectContaining({ kind: 'setup', state: 'failed' })])
})
expect(runtime.sendTerminalAgentPrompt).not.toHaveBeenCalled()
})
it('does not mislabel a wait-for-setup timeout as setup failure', async () => {
mockCreatedWorktree({ startupPolicy: 'wait-for-setup', state: 'running' })
vi.mocked(runtime.waitForTerminal).mockResolvedValue({
handle: 'term_worker',
condition: 'tui-idle',
satisfied: false,
status: 'running',
exitCode: null
})
const { result } = await startWorker()
expect(result).toMatchObject({
state: 'failed',
failedStage: 'agent_readiness',
setup: { state: 'running' }
})
expect(runtime.sendTerminalAgentPrompt).not.toHaveBeenCalled()
})
it('distinguishes no-effect failure, unknown acceptance, and durable residual effects', async () => {
vi.spyOn(runtime, 'createManagedWorktree').mockRejectedValueOnce(
new Error('repository validation failed before creation')
)
const noEffect = await startWorker({ name: 'no-effect' })
expect(noEffect.result).toMatchObject({
state: 'failed',
failedStage: 'worktree_create',
effects: [],
residualResources: []
})
vi.mocked(runtime.createManagedWorktree).mockRejectedValueOnce(
Object.assign(new Error('connection lost after possible acceptance'), {
code: 'operation_unknown'
})
)
const unknown = await startWorker({ name: 'unknown-effect' })
expect(unknown.result).toMatchObject({
state: 'outcome_unknown',
failedStage: 'worktree_create',
effects: [],
residualResources: []
})
mockCreatedWorktree()
vi.mocked(runtime.waitForTerminal).mockResolvedValueOnce({
handle: 'term_worker',
condition: 'tui-idle',
satisfied: false,
status: 'exited',
exitCode: 1
})
const durableEffect = await startWorker({ name: 'durable-effect' })
expect(durableEffect.result).toMatchObject({
state: 'failed',
failedStage: 'agent_readiness',
effects: expect.arrayContaining([
expect.objectContaining({ kind: 'worktree', id: 'repo::created' }),
expect.objectContaining({ kind: 'terminal', id: 'term_worker' })
]),
residualResources: expect.arrayContaining([
expect.objectContaining({ kind: 'worktree', id: 'repo::created' }),
expect.objectContaining({ kind: 'terminal', id: 'term_worker' })
])
})
})
it('returns outcome unknown when worktree creation may have been accepted remotely', async () => {
vi.spyOn(runtime, 'createManagedWorktree').mockRejectedValue(
Object.assign(new Error('connection closed after request acceptance'), {
code: 'operation_unknown'
})
)
const { result, task } = await startWorker()
expect(result).toMatchObject({
state: 'outcome_unknown',
failedStage: 'worktree_create',
nextCommands: expect.arrayContaining([
expect.stringContaining('worker-show --dispatch'),
expect.stringContaining('worker-abandon --dispatch')
])
})
expect(db.getTask(task.id)?.status).toBe('blocked')
})
it('persists the retry request with the starting Dispatch before worktree effects', async () => {
const task = db.createTask({ spec: 'atomic worker acceptance', runId })
let finishCreate: ((value: CreateWorktreeResult) => void) | undefined
vi.spyOn(runtime, 'createManagedWorktree').mockImplementation(
async () =>
await new Promise((resolve) => {
finishCreate = resolve
})
)
const dispatcher = new RpcDispatcher({ runtime, methods: ORCHESTRATION_METHODS })
const request: RpcRequest = {
id: 'rpc_worker_start',
authToken: 'caller-token',
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'worker_start_request',
method: 'orchestration.workerStart',
params: {
task: task.id,
from: 'term_coord',
worktree: 'new-child',
name: 'atomic-worker',
agent: 'codex'
}
}
const pending = dispatcher.dispatch(request)
await vi.waitFor(() => expect(db.getDispatchContext(task.id)).toBeDefined())
const acceptedDispatch = db.getDispatchContext(task.id)!
const callerFingerprint = createHash('sha256').update('caller-token').digest('hex')
const receipt = db.getMutationReceipt(callerFingerprint, 'worker_start_request')
expect(receipt).toMatchObject({
request_id: 'worker_start_request',
method: 'orchestration.workerStart',
state: 'pending'
})
expect(db.getWorkerDispatch(acceptedDispatch.id)).toMatchObject({
state: 'starting',
stage: 'worktree_creating'
})
finishCreate?.({
worktree: { id: 'repo::created', repoId: 'repo' },
startupTerminal: { spawned: true, handle: 'term_worker' },
setupReceipt: {
requested: 'run',
hookFound: false,
startupPolicy: 'start-immediately',
state: 'not_configured'
}
} as CreateWorktreeResult)
await expect(pending).resolves.toMatchObject({
ok: true,
result: { state: 'ready', mutation: { requestId: 'worker_start_request' } }
})
expect(db.getMutationReceipt(callerFingerprint, 'worker_start_request')).toMatchObject({
state: 'completed'
})
})
it('persists pre-effect, post-effect, and post-input stages in order', async () => {
mockCreatedWorktree({ hookFound: false })
let finishWait:
| ((value: Awaited<ReturnType<OrcaRuntimeService['waitForTerminal']>>) => void)
| undefined
let finishPrompt:
| ((value: Awaited<ReturnType<OrcaRuntimeService['sendTerminalAgentPrompt']>>) => void)
| undefined
vi.mocked(runtime.waitForTerminal).mockImplementationOnce(
async () =>
await new Promise((resolve) => {
finishWait = resolve
})
)
vi.mocked(runtime.sendTerminalAgentPrompt).mockImplementationOnce(
async () =>
await new Promise((resolve) => {
finishPrompt = resolve
})
)
const pending = startWorker({ name: 'staged-worker' })
await vi.waitFor(() => {
const task = db.listTasks()[0]
const dispatch = task ? db.getDispatchContext(task.id) : undefined
expect(dispatch && db.getWorkerDispatch(dispatch.id)).toMatchObject({
state: 'starting',
stage: 'terminal_readying',
worktree_id: 'repo::created',
agent_terminal_handle: 'term_worker'
})
})
const dispatch = db.getDispatchContext(db.listTasks()[0]!.id)!
expect(JSON.parse(db.getWorkerDispatch(dispatch.id)!.residual_resources)).toEqual(
expect.arrayContaining([expect.objectContaining({ kind: 'worktree', id: 'repo::created' })])
)
finishWait?.({
handle: 'term_worker',
condition: 'tui-idle',
satisfied: true,
status: 'running',
exitCode: null
})
await vi.waitFor(() =>
expect(db.getWorkerDispatch(dispatch.id)).toMatchObject({
state: 'starting',
stage: 'authority_attached'
})
)
finishPrompt?.({ handle: 'term_worker', accepted: true, bytesWritten: 1 })
await expect(pending).resolves.toMatchObject({
result: { state: 'ready', stage: 'input_accepted' }
})
expect(db.getWorkerDispatch(dispatch.id)).toMatchObject({
state: 'ready',
stage: 'input_accepted'
})
})
})
@@ -0,0 +1,246 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { OrcaRuntimeService } from '../../orca-runtime'
import { OrchestrationDb } from '../../orchestration/db'
import { ORCHESTRATION_METHODS } from './orchestration'
describe('orchestration worker recovery', () => {
let db: OrchestrationDb
let runtime: OrcaRuntimeService
beforeEach(() => {
db = new OrchestrationDb(':memory:')
runtime = new OrcaRuntimeService()
runtime.setOrchestrationDb(db)
vi.spyOn(runtime, 'getTerminalPaneKey').mockReturnValue(
'tab_worker:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
)
vi.spyOn(runtime, 'getTerminalProcessIncarnation').mockReturnValue('runtime:pty:1')
vi.spyOn(runtime, 'showTerminal').mockResolvedValue({
handle: 'term_worker',
worktreeId: 'repo::worktree',
connected: true,
status: 'running'
} as never)
vi.spyOn(runtime, 'readTerminal').mockResolvedValue({
handle: 'term_worker',
status: 'running',
tail: ['working'],
truncated: false,
nextCursor: null
})
vi.spyOn(runtime, 'closeTerminal').mockResolvedValue({
handle: 'term_worker',
closed: true
} as never)
})
afterEach(() => db.close())
async function call(name: string, params: Record<string, unknown>) {
const method = ORCHESTRATION_METHODS.find((candidate) => candidate.name === name)
if (!method) {
throw new Error(`Method not found: ${name}`)
}
return method.handler(method.params!.parse(params), { runtime })
}
function createWorker(runtimeEpoch = runtime.getRuntimeId(), ready = true) {
const run = db.createRun({
objective: 'Recovery',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
})
const task = db.createTask({ spec: 'recover worker', runId: run.id })
const started = db.createStartingWorkerDispatch({
taskId: task.id,
startOptions: {},
runtimeEpoch
})
db.prepareStartingWorkerAuthority({
dispatchId: started.dispatch.id,
handle: 'term_worker',
paneKey: 'tab_worker:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb',
processIncarnation: 'runtime:pty:1',
worktreeId: 'repo::worktree',
setupState: 'not_applicable',
effects: [{ kind: 'terminal', action: 'created', id: 'term_worker' }]
})
if (ready) {
db.markWorkerDispatchReady(started.dispatch.id)
} else {
db.markWorkerStartUnknown(started.dispatch.id, 'dispatch_input', 'connection lost')
}
return { run, task, dispatch: started.dispatch }
}
it('shows and reads only the exact attached worker process', async () => {
const { dispatch } = createWorker()
await expect(
call('orchestration.workerShow', { dispatch: dispatch.id })
).resolves.toMatchObject({
worker: { state: 'ready' },
observation: { status: 'running', exactWorker: true },
terminal: { handle: 'term_worker' }
})
await expect(
call('orchestration.workerRead', { dispatch: dispatch.id })
).resolves.toMatchObject({
dispatchId: dispatch.id,
terminal: { tail: ['working'] }
})
})
it('stops an exact worker whose start receipt is unknown', async () => {
const { task, dispatch } = createWorker(runtime.getRuntimeId(), false)
await expect(
call('orchestration.workerStop', { dispatch: dispatch.id })
).resolves.toMatchObject({
state: 'stopped',
processAction: 'closed_agent_terminal'
})
expect(runtime.closeTerminal).toHaveBeenCalledWith('term_worker')
expect(db.getTask(task.id)?.status).toBe('blocked')
})
it('does not adopt or stop a same-looking pane with a new process incarnation', async () => {
const { task, dispatch } = createWorker()
vi.mocked(runtime.getTerminalProcessIncarnation).mockReturnValue('runtime:pty:2')
await expect(
call('orchestration.workerShow', { dispatch: dispatch.id })
).resolves.toMatchObject({
worker: { state: 'ready' },
observation: { status: 'identity_changed', exactWorker: false },
terminal: null
})
await expect(call('orchestration.workerRead', { dispatch: dispatch.id })).rejects.toMatchObject(
{
code: 'worker_identity_changed'
}
)
await expect(
call('orchestration.workerStop', { dispatch: dispatch.id })
).resolves.toMatchObject({
state: 'stop_unknown',
processAction: 'none'
})
expect(runtime.closeTerminal).not.toHaveBeenCalled()
expect(db.getTask(task.id)?.status).toBe('blocked')
})
it('labels an exact but disconnected worker as exited and does not close it again', async () => {
const { task, dispatch } = createWorker()
vi.mocked(runtime.showTerminal).mockResolvedValue({
handle: 'term_worker',
worktreeId: 'repo::worktree',
connected: false,
writable: false
} as never)
await expect(
call('orchestration.workerShow', { dispatch: dispatch.id })
).resolves.toMatchObject({
observation: { status: 'exited', exactWorker: true },
terminal: { handle: 'term_worker', connected: false }
})
await expect(
call('orchestration.workerStop', { dispatch: dispatch.id })
).resolves.toMatchObject({
state: 'stop_unknown',
processAction: 'none'
})
expect(runtime.closeTerminal).not.toHaveBeenCalled()
expect(db.getTask(task.id)?.status).toBe('blocked')
})
it('turns an interrupted start into inspectable unknown after runtime restart', async () => {
const run = db.createRun({
objective: 'Interrupted start',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
})
const task = db.createTask({ spec: 'interrupted', runId: run.id })
const started = db.createStartingWorkerDispatch({
taskId: task.id,
startOptions: {},
runtimeEpoch: 'previous_runtime'
})
db.recordWorkerStage({
dispatchId: started.dispatch.id,
stage: 'worktree_creating'
})
await expect(
call('orchestration.workerShow', { dispatch: started.dispatch.id })
).resolves.toMatchObject({
worker: { state: 'start_unknown', stage: 'worktree_creating' },
observation: { status: 'unattached', exactWorker: false }
})
expect(db.getTask(task.id)?.status).toBe('blocked')
})
it('turns an interrupted stop into unknown after runtime restart', async () => {
const { task, dispatch } = createWorker('previous_runtime')
db.beginWorkerStop(dispatch.id)
await expect(
call('orchestration.workerShow', { dispatch: dispatch.id })
).resolves.toMatchObject({
worker: { state: 'stop_unknown' }
})
expect(db.getTask(task.id)?.status).toBe('blocked')
})
it('reconciles a stop_unknown Dispatch from an authoritative remote stopped receipt', async () => {
const run = db.createRun({
objective: 'Lost remote stop response',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
})
const task = db.createTask({ spec: 'stop remote worker', runId: run.id })
const started = db.createStartingWorkerDispatch({
taskId: task.id,
startOptions: {},
runtimeEpoch: runtime.getRuntimeId(),
federation: {
environmentId: 'environment_windows',
environmentName: 'windows',
peerFingerprint: 'windows_peer',
protocolVersion: 1
}
})
db.markWorkerStartUnknown(started.dispatch.id, 'remote_attach', 'response lost')
db.beginWorkerStop(started.dispatch.id)
db.markWorkerStopUnknown(started.dispatch.id, 'stop response lost')
vi.spyOn(runtime, 'resolveOrchestrationWorkerServer').mockReturnValue({
environmentId: 'environment_windows',
name: 'windows',
peerFingerprint: 'windows_peer'
})
vi.spyOn(runtime, 'callOrchestrationWorkerServer').mockResolvedValue({
runtimeEpoch: 'windows_epoch',
attachment: {
state: 'stopped',
stage: 'process_stopped',
last_error: null,
worktree_id: 'repo::windows-worktree',
terminal_handle: 'term_windows_worker',
setup_state: 'running',
effects: [],
residualResources: []
},
terminal: { handle: 'term_windows_worker', connected: false },
observation: { status: 'exited', exactWorker: true }
})
await expect(
call('orchestration.workerShow', { dispatch: started.dispatch.id })
).resolves.toMatchObject({
worker: { state: 'stopped', stage: 'process_stopped', last_error: null },
observation: { status: 'exited', exactWorker: true }
})
expect(db.getTask(task.id)?.status).toBe('blocked')
})
})
@@ -0,0 +1,305 @@
import { isTuiAgent } from '../../../../shared/tui-agent-config'
import type { TuiAgent } from '../../../../shared/types'
import { buildDispatchPreamble } from '../../orchestration/preamble'
import { OrchestrationError } from '../../orchestration/orchestration-error'
import { defineMethod, type RpcMethod } from '../core'
import { startFederatedWorker } from './orchestration-federated-worker-start'
import { assertOrchestrationWorktreeCreationSupported } from './orchestration-folder-worktree-placement'
import { WorkerStartParams } from './orchestration-worker-start-schema'
import {
createWorkerWorktree,
monitorWorkerSetup,
type WorkerEffect,
type WorkerSetupReceipt
} from './orchestration-worker-topology'
import {
persistGatedSetupSpawnFailure,
persistWorkerReadinessStage,
persistWorkerSetupWaitOutcome
} from './orchestration-worker-setup-gate'
import { failWorkerStartWithReceipt } from './orchestration-worker-start-receipt'
export const ORCHESTRATION_WORKER_START_METHODS: RpcMethod[] = [
defineMethod({
name: 'orchestration.workerStart',
params: WorkerStartParams,
handler: async (params, { runtime, orchestrationMutation }) => {
const db = runtime.getOrchestrationDb()
const coordinatorPane = runtime.getTerminalPaneKey(params.from)
const run = coordinatorPane ? db.getCurrentRunForPane(coordinatorPane) : undefined
if (!run || (params.run && params.run !== run.id)) {
throw new OrchestrationError(
'consumer_fenced',
'worker-start requires the coordinator terminal currently bound to the Task Run.'
)
}
const task = db.getTask(params.task)
if (!task || task.run_id !== run.id) {
throw new OrchestrationError(
'task_not_found',
`Task ${params.task} was not found in Run ${run.id}.`
)
}
if (params.on) {
return startFederatedWorker({
params,
runtime,
db,
runId: run.id,
task,
orchestrationMutation
})
}
const requestedWorktree = params.worktree ?? 'current'
const createsWorktree =
requestedWorktree === 'new-child' || requestedWorktree === 'new-top-level'
if (params.terminal && params.agent) {
throw new OrchestrationError(
'invalid_argument',
'--terminal reuses an existing agent and cannot combine with --agent.'
)
}
if (createsWorktree && params.terminal) {
throw new OrchestrationError(
'invalid_argument',
'--terminal cannot combine with new-worktree creation.'
)
}
if (createsWorktree && !params.name) {
throw new OrchestrationError('invalid_argument', 'New worktrees require --name.')
}
if (!createsWorktree && (params.name || params.repo || params.baseBranch || params.setup)) {
throw new OrchestrationError(
'invalid_argument',
'Creation and setup options apply only to new-child or new-top-level worktrees.'
)
}
const agent = params.agent
if (!params.terminal && (!agent || !isTuiAgent(agent))) {
throw new OrchestrationError(
'agent_unconfigured',
'A configured --agent is required when worker-start creates a terminal.'
)
}
if (agent) {
runtime.validateOrchestrationAgentLauncher(agent as TuiAgent)
}
const coordinatorTerminal = await runtime.showTerminal(params.from)
const coordinatorWorktree = await runtime.showManagedWorktree(
`id:${coordinatorTerminal.worktreeId}`
)
if (createsWorktree) {
await assertOrchestrationWorktreeCreationSupported({
runtime,
repoSelector: params.repo ?? coordinatorWorktree.repoId,
existingPlacement: 'current or an exact existing folder workspace'
})
}
let resolvedWorktree = createsWorktree
? undefined
: requestedWorktree === 'current'
? coordinatorWorktree
: await runtime.showManagedWorktree(requestedWorktree)
let explicitTerminal
if (params.terminal) {
explicitTerminal = await runtime.showTerminal(params.terminal)
if (explicitTerminal.worktreeId !== resolvedWorktree?.id) {
throw new OrchestrationError(
'terminal_worktree_mismatch',
`Terminal ${params.terminal} does not belong to worktree ${resolvedWorktree?.id}.`
)
}
if (!(await runtime.isTerminalRunningAgent(params.terminal))) {
throw new OrchestrationError(
'agent_unconfigured',
`Terminal ${params.terminal} is not running a recognized agent.`
)
}
}
const startOptions = {
worktree: requestedWorktree,
resolvedWorktreeId: resolvedWorktree?.id ?? null,
name: params.name ?? null,
repo: params.repo ?? (createsWorktree ? coordinatorWorktree.repoId : null),
baseBranch: params.baseBranch ?? null,
terminal: params.terminal ?? null,
agent: agent ?? null,
timeoutMs: params.timeoutMs ?? 60_000,
setup: createsWorktree ? (params.setup ?? 'run') : 'not_applicable',
setupSource: createsWorktree
? params.setup
? 'explicit_request'
: 'orchestration_default'
: 'existing_worktree'
}
const started = db.createStartingWorkerDispatch({
taskId: task.id,
retryOf: params.retryOf,
startOptions,
runtimeEpoch: runtime.getRuntimeId(),
mutationReceipt: orchestrationMutation
})
const effects: WorkerEffect[] = []
if (resolvedWorktree) {
effects.push(
{ kind: 'worktree', action: 'reused', id: resolvedWorktree.id },
{ kind: 'setup', action: 'not_applicable', state: 'not_applicable' }
)
}
let terminalHandle = params.terminal
let failedStage = 'terminal_create'
let setupReceipt: WorkerSetupReceipt = {
requested: 'not_applicable',
effective: 'not_applicable',
source: 'existing_worktree',
hookFound: false,
startupPolicy: 'start-immediately',
state: 'not_applicable'
}
try {
if (createsWorktree) {
failedStage = 'worktree_create'
const created = await createWorkerWorktree({
runtime,
db,
dispatchId: started.dispatch.id,
requestedWorktree,
coordinatorWorktree,
params,
agent: agent as TuiAgent,
effects
})
resolvedWorktree = created.worktree
terminalHandle = created.terminalHandle
setupReceipt = created.setupReceipt
} else if (!terminalHandle) {
db.recordWorkerStage({
dispatchId: started.dispatch.id,
stage: 'terminal_creating',
worktreeId: resolvedWorktree!.id,
effects
})
const terminal = await runtime.createTerminal(`id:${resolvedWorktree!.id}`, {
command: agent,
title: `worker-${task.id}`,
presentation: 'background'
})
terminalHandle = terminal.handle
effects.push({
kind: 'terminal',
role: 'agent',
action: 'created',
id: terminal.handle
})
} else {
effects.push({
kind: 'terminal',
role: 'agent',
action: 'reused',
id: terminalHandle
})
}
if (!resolvedWorktree || !terminalHandle) {
throw new Error('Worker topology did not resolve an agent terminal and worktree.')
}
const setupStage = {
db,
dispatchId: started.dispatch.id,
worktreeId: resolvedWorktree.id,
terminalHandle,
setup: setupReceipt,
effects
}
if (persistGatedSetupSpawnFailure(setupStage)) {
failedStage = 'setup_start'
throw new Error('Setup terminal failed to start before the gated agent launch.')
}
persistWorkerReadinessStage(setupStage)
failedStage = 'agent_readiness'
const wait = await runtime.waitForTerminal(terminalHandle, {
condition: 'tui-idle',
timeoutMs: params.timeoutMs ?? 60_000
})
persistWorkerSetupWaitOutcome({ ...setupStage, wait })
if (!wait.satisfied) {
if (setupReceipt.state === 'failed') {
failedStage = 'setup_wait'
}
throw new Error(
wait.blockedReason
? `Agent startup blocked: ${wait.blockedReason}`
: `Agent did not become ready (${wait.status}).`
)
}
const paneKey = runtime.getTerminalPaneKey(terminalHandle)
const processIncarnation = runtime.getTerminalProcessIncarnation(terminalHandle)
if (!paneKey || !processIncarnation) {
throw new Error('stable_pane_required')
}
const capability = db.prepareStartingWorkerAuthority({
dispatchId: started.dispatch.id,
handle: terminalHandle,
paneKey,
processIncarnation,
worktreeId: resolvedWorktree.id,
effects,
setupState: setupReceipt.state
})
failedStage = 'dispatch_input'
const preamble = buildDispatchPreamble({
taskId: task.id,
dispatchId: started.dispatch.id,
taskSpec: task.spec,
coordinatorHandle: params.from,
workerHandle: terminalHandle,
dispatchCapability: capability,
devMode: params.devMode,
cliCommand: runtime.getTerminalOrchestrationCliCommand(terminalHandle)
})
await runtime.sendTerminalAgentPrompt(terminalHandle, preamble)
effects.push({
kind: 'dispatch_input',
role: 'agent',
id: terminalHandle,
state: 'accepted'
})
const worker = db.markWorkerDispatchReady(started.dispatch.id, effects)
monitorWorkerSetup({
runtime,
db,
runId: run.id,
dispatchId: started.dispatch.id,
setupReceipt,
effects
})
return {
runId: run.id,
taskId: task.id,
dispatchId: started.dispatch.id,
state: worker.state,
stage: worker.stage,
setup: setupReceipt,
timeoutMs: params.timeoutMs ?? 60_000,
effects,
residualResources: []
}
} catch (error) {
return failWorkerStartWithReceipt({
db,
runId: run.id,
taskId: task.id,
dispatchId: started.dispatch.id,
failedStage,
error,
setup: setupReceipt
})
}
}
})
]
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,138 @@
import { createHash } from 'node:crypto'
import { z } from 'zod'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../shared/protocol-version'
import { OrcaRuntimeService } from '../orca-runtime'
import { OrchestrationDb } from '../orchestration/db'
import { defineMethod, type RpcRequest } from './core'
import { RpcDispatcher } from './dispatcher'
describe('orchestration contract fence', () => {
const databases: OrchestrationDb[] = []
afterEach(() => {
for (const database of databases.splice(0)) {
database.close()
}
})
function createHarness(method = 'orchestration.send') {
const database = new OrchestrationDb(':memory:')
databases.push(database)
const runtime = new OrcaRuntimeService()
runtime.setOrchestrationDb(database)
const effect = vi.fn(() => ({ accepted: true }))
const dispatcher = new RpcDispatcher({
runtime,
methods: [
defineMethod({
name: method,
params: z.object({ subject: z.string() }),
handler: effect
})
]
})
return { database, dispatcher, effect }
}
function request(overrides: Partial<RpcRequest> = {}): RpcRequest {
return {
id: 'rpc_1',
authToken: 'caller-token',
method: 'orchestration.send',
params: { subject: 'hello' },
orchestrationRequestId: 'mutation_1',
...overrides
}
}
it.each([
[undefined, 'client_contract_missing'],
[0, 'client_contract_unsupported'],
[ORCHESTRATION_CONTRACT_VERSION + 1, 'client_contract_unsupported']
])(
'rejects contract version %s before parsing, receipts, or effects',
async (version, reason) => {
const { database, dispatcher, effect } = createHarness()
const response = await dispatcher.dispatch(
request({
params: { malformed: true },
orchestrationContractVersion: version
})
)
expect(response).toMatchObject({
ok: false,
error: {
code: 'orchestration_migration_required',
data: {
reason,
effectsApplied: false,
nextCommandArgs: ['skills', 'get', 'orchestration', '--full']
}
}
})
expect(effect).not.toHaveBeenCalled()
const callerFingerprint = createHash('sha256').update('caller-token').digest('hex')
expect(database.getMutationReceipt(callerFingerprint, 'mutation_1')).toBeUndefined()
}
)
it('allows the current contract to reach the mutation executor', async () => {
const { dispatcher, effect } = createHarness()
const response = await dispatcher.dispatch(
request({ orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION })
)
expect(response).toMatchObject({ ok: true, result: { accepted: true } })
expect(effect).toHaveBeenCalledOnce()
})
it('keeps read-only orchestration inspection available without a contract', async () => {
const { dispatcher, effect } = createHarness('orchestration.taskList')
const response = await dispatcher.dispatch(
request({
method: 'orchestration.taskList',
params: { subject: 'read' },
orchestrationRequestId: undefined
})
)
expect(response).toMatchObject({ ok: true, result: { accepted: true } })
expect(effect).toHaveBeenCalledOnce()
})
it.each(['orchestration.run', 'orchestration.runStop'])(
'retires %s even when the caller sends the current contract',
async (method) => {
const { dispatcher, effect } = createHarness(method)
const response = await dispatcher.dispatch(
request({
method,
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION
})
)
expect(response).toMatchObject({
ok: false,
error: {
code: 'orchestration_migration_required',
data: { reason: 'command_retired', effectsApplied: false }
}
})
expect(effect).not.toHaveBeenCalled()
}
)
it('applies the same pre-effect fence on WebSocket dispatch', async () => {
const { dispatcher, effect } = createHarness()
const replies: string[] = []
await dispatcher.dispatchStreaming(request(), (reply) => replies.push(reply))
expect(JSON.parse(replies[0] ?? '{}')).toMatchObject({
ok: false,
error: { code: 'orchestration_migration_required' }
})
expect(effect).not.toHaveBeenCalled()
})
})
@@ -0,0 +1,36 @@
import {
isOrchestrationMutation,
isRetiredOrchestrationMethod,
orchestrationMigrationData,
type OrchestrationMigrationReason
} from '../../../shared/orchestration-rpc-contract'
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../shared/protocol-version'
import type { RpcEnvelopeMeta, RpcRequest, RpcResponse } from './core'
import { errorResponse } from './errors'
export function orchestrationMigrationFence(
request: RpcRequest,
meta: RpcEnvelopeMeta
): RpcResponse | undefined {
if (!isOrchestrationMutation(request.method, request.params)) {
return undefined
}
let reason: OrchestrationMigrationReason | undefined
if (isRetiredOrchestrationMethod(request.method)) {
reason = 'command_retired'
} else if (request.orchestrationContractVersion === undefined) {
reason = 'client_contract_missing'
} else if (request.orchestrationContractVersion !== ORCHESTRATION_CONTRACT_VERSION) {
reason = 'client_contract_unsupported'
}
if (!reason) {
return undefined
}
return errorResponse(
request.id,
meta,
'orchestration_migration_required',
'This orchestration mutation uses an obsolete contract. No effects were applied.',
orchestrationMigrationData(reason)
)
}
@@ -0,0 +1,152 @@
import { createHash } from 'node:crypto'
import { isOrchestrationMutation } from '../../../shared/orchestration-rpc-contract'
import type { OrcaRuntimeService } from '../orca-runtime'
import { OrchestrationError } from '../orchestration/orchestration-error'
import type { RpcRequest } from './core'
export type DurableMutationInvocation = {
identity: {
callerFingerprint: string
requestId: string
method: string
payloadHash: string
}
recordReceipt: (receipt: unknown) => void
}
export class OrchestrationMutationExecutor {
private readonly inFlight = new Map<string, Promise<unknown>>()
constructor(private readonly runtime: OrcaRuntimeService) {}
async run(
request: RpcRequest,
params: unknown,
invoke: (mutation?: DurableMutationInvocation) => Promise<unknown> | unknown
): Promise<unknown> {
const requestId = request.orchestrationRequestId
if (!requestId || !isOrchestrationMutation(request.method, params)) {
return await invoke()
}
const callerFingerprint = authenticatedCallerFingerprint(request)
const payloadHash = createHash('sha256')
.update(JSON.stringify(canonicalize({ method: request.method, params })))
.digest('hex')
const key = `${callerFingerprint}:${requestId}`
const db = this.runtime.getOrchestrationDb()
const identity = { callerFingerprint, requestId, method: request.method, payloadHash }
const atomicWorkerAcceptance =
request.method === 'orchestration.workerStart' ||
request.method === 'orchestration.federationAttachStart'
const begun = atomicWorkerAcceptance
? (() => {
const row = db.getMutationReceipt(callerFingerprint, requestId)
if (!row) {
return { disposition: 'started' as const }
}
if (row.method !== request.method || row.payload_hash !== payloadHash) {
throw new OrchestrationError(
'request_mismatch',
`Mutation request ${requestId} was already used with different input.`
)
}
return { disposition: row.state, row }
})()
: db.beginMutationReceipt(identity)
if (begun.disposition === 'completed') {
return attachMutationReceipt(JSON.parse(begun.row.receipt ?? 'null'), requestId, true)
}
if (begun.disposition === 'pending') {
const active = this.inFlight.get(key)
if (active) {
return attachMutationReceipt(await active, requestId, true)
}
const recovery = getPendingWorkerStartRecovery(request.method, begun.row.receipt)
throw new OrchestrationError(
'operation_unknown',
recovery
? `Worker start ${requestId} was accepted as Dispatch ${recovery.dispatchId} before restart. Inspect that Dispatch; do not start another worker.`
: `Mutation ${requestId} may have been accepted before restart. Retry inspection or recovery with the same request ID.`,
recovery
? {
requestId,
dispatchId: recovery.dispatchId,
recoveryCommand: `orca orchestration worker-show --dispatch ${recovery.dispatchId} --json`
}
: { requestId }
)
}
const recordReceipt = (result: unknown): void => {
db.completeMutationReceipt({
...identity,
receipt: JSON.stringify(attachMutationReceipt(result, requestId, false))
})
}
const active = Promise.resolve().then(() => invoke({ identity, recordReceipt }))
this.inFlight.set(key, active)
try {
const result = await active
const receipted = attachMutationReceipt(result, requestId, false)
db.completeMutationReceipt({ ...identity, receipt: JSON.stringify(receipted) })
return receipted
} catch (error) {
if (!(error instanceof OrchestrationError && error.code === 'operation_unknown')) {
db.discardPendingMutationReceipt(callerFingerprint, requestId)
}
throw error
} finally {
this.inFlight.delete(key)
}
}
}
export function authenticatedCallerFingerprint(request: RpcRequest): string {
const callerToken =
request.authToken ||
(request as RpcRequest & { deviceToken?: string }).deviceToken ||
'authenticated_transport'
return createHash('sha256').update(callerToken).digest('hex')
}
function canonicalize(value: unknown): unknown {
if (Array.isArray(value)) {
return value.map(canonicalize)
}
if (!value || typeof value !== 'object') {
return value
}
const source = value as Record<string, unknown>
const result: Record<string, unknown> = {}
for (const key of Object.keys(source).sort()) {
if (source[key] !== undefined) {
result[key] = canonicalize(source[key])
}
}
return result
}
function attachMutationReceipt(result: unknown, requestId: string, replayed: boolean): unknown {
if (!result || typeof result !== 'object' || Array.isArray(result)) {
return { result, mutation: { requestId, replayed } }
}
return { ...(result as Record<string, unknown>), mutation: { requestId, replayed } }
}
function getPendingWorkerStartRecovery(
method: string,
receipt: string | null
): { dispatchId: string } | undefined {
if (method !== 'orchestration.workerStart' || !receipt) {
return undefined
}
try {
const parsed = JSON.parse(receipt) as { accepted?: { dispatchId?: unknown } }
return typeof parsed.accepted?.dispatchId === 'string'
? { dispatchId: parsed.accepted.dispatchId }
: undefined
} catch {
return undefined
}
}
@@ -0,0 +1,309 @@
import { createHash } from 'node:crypto'
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { z } from 'zod'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { ORCHESTRATION_CONTRACT_VERSION } from '../../../shared/protocol-version'
import { OrcaRuntimeService } from '../orca-runtime'
import { OrchestrationDb } from '../orchestration/db'
import { defineMethod, type RpcRequest } from './core'
import { RpcDispatcher } from './dispatcher'
import { ORCHESTRATION_METHODS } from './methods/orchestration'
const Params = z.object({ subject: z.string() })
function request(params: {
rpcId: string
mutationId: string
subject: string
authToken?: string
}): RpcRequest {
return {
id: params.rpcId,
authToken: params.authToken ?? 'caller-token',
method: 'orchestration.send',
params: { subject: params.subject },
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: params.mutationId
}
}
describe('durable orchestration mutation ledger', () => {
const paths: string[] = []
afterEach(() => {
for (const path of paths.splice(0)) {
rmSync(path, { recursive: true, force: true })
}
})
function createHarness(dbPath: string | ':memory:' = ':memory:') {
const db = new OrchestrationDb(dbPath)
const runtime = new OrcaRuntimeService()
runtime.setOrchestrationDb(db)
const effect = vi.fn((subject: string) =>
db.insertMessage({ from: 'caller', to: 'recipient', subject })
)
const dispatcher = new RpcDispatcher({
runtime,
methods: [
defineMethod({
name: 'orchestration.send',
params: Params,
handler: ({ subject }) => ({ message: effect(subject) })
})
]
})
return { db, runtime, dispatcher, effect }
}
it('replays one completed receipt without repeating the effect', async () => {
const { db, dispatcher, effect } = createHarness()
const first = await dispatcher.dispatch(
request({ rpcId: 'rpc_1', mutationId: 'mutation_1', subject: 'hello' })
)
const replay = await dispatcher.dispatch(
request({ rpcId: 'rpc_2', mutationId: 'mutation_1', subject: 'hello' })
)
expect(first).toMatchObject({
ok: true,
result: { message: { subject: 'hello' }, mutation: { replayed: false } }
})
expect(replay).toMatchObject({
ok: true,
result: { message: { subject: 'hello' }, mutation: { replayed: true } }
})
expect(effect).toHaveBeenCalledTimes(1)
expect(db.getInbox(10)).toHaveLength(1)
db.close()
})
it('rejects changed input for the same caller and request ID', async () => {
const { db, dispatcher } = createHarness()
await dispatcher.dispatch(
request({ rpcId: 'rpc_1', mutationId: 'mutation_1', subject: 'hello' })
)
const mismatch = await dispatcher.dispatch(
request({ rpcId: 'rpc_2', mutationId: 'mutation_1', subject: 'changed' })
)
expect(mismatch).toMatchObject({ ok: false, error: { code: 'request_mismatch' } })
db.close()
})
it('applies the same ledger on authenticated WebSocket dispatch', async () => {
const { db, dispatcher, effect } = createHarness()
const replies: string[] = []
const firstRequest = request({
rpcId: 'rpc_1',
mutationId: 'mutation_remote',
subject: 'remote'
}) as RpcRequest & { deviceToken?: string }
firstRequest.authToken = ''
firstRequest.deviceToken = 'paired-device'
await dispatcher.dispatchStreaming(firstRequest, (reply) => replies.push(reply))
const replayRequest = { ...firstRequest, id: 'rpc_2' }
await dispatcher.dispatchStreaming(replayRequest, (reply) => replies.push(reply))
expect(JSON.parse(replies[0] ?? '{}')).toMatchObject({
ok: true,
result: { mutation: { replayed: false } }
})
expect(JSON.parse(replies[1] ?? '{}')).toMatchObject({
ok: true,
result: { mutation: { replayed: true } }
})
expect(effect).toHaveBeenCalledTimes(1)
db.close()
})
it('joins concurrent identical mutations', async () => {
const db = new OrchestrationDb(':memory:')
const runtime = new OrcaRuntimeService()
runtime.setOrchestrationDb(db)
let release: (() => void) | undefined
const gate = new Promise<void>((resolve) => {
release = resolve
})
const effect = vi.fn(async () => {
await gate
return { accepted: true }
})
const dispatcher = new RpcDispatcher({
runtime,
methods: [
defineMethod({
name: 'orchestration.send',
params: Params,
handler: effect
})
]
})
const first = dispatcher.dispatch(
request({ rpcId: 'rpc_1', mutationId: 'mutation_join', subject: 'same' })
)
await Promise.resolve()
const second = dispatcher.dispatch(
request({ rpcId: 'rpc_2', mutationId: 'mutation_join', subject: 'same' })
)
release?.()
expect(await first).toMatchObject({ ok: true, result: { mutation: { replayed: false } } })
expect(await second).toMatchObject({ ok: true, result: { mutation: { replayed: true } } })
expect(effect).toHaveBeenCalledTimes(1)
db.close()
})
it('replays a completed receipt after database and dispatcher restart', async () => {
const dir = mkdtempSync(join(tmpdir(), 'orca-mutation-ledger-'))
paths.push(dir)
const dbPath = join(dir, 'orchestration.db')
const first = createHarness(dbPath)
await first.dispatcher.dispatch(
request({ rpcId: 'rpc_1', mutationId: 'mutation_1', subject: 'durable' })
)
first.db.close()
const second = createHarness(dbPath)
const replay = await second.dispatcher.dispatch(
request({ rpcId: 'rpc_2', mutationId: 'mutation_1', subject: 'durable' })
)
expect(replay).toMatchObject({
ok: true,
result: { message: { subject: 'durable' }, mutation: { replayed: true } }
})
expect(second.effect).not.toHaveBeenCalled()
second.db.close()
})
it('returns unknown for a pending receipt left by a previous process', async () => {
const { db, dispatcher } = createHarness()
db.beginMutationReceipt({
callerFingerprint: createHash('sha256').update('caller-token').digest('hex'),
requestId: 'mutation_1',
method: 'orchestration.send',
payloadHash: createHash('sha256')
.update('{"method":"orchestration.send","params":{"subject":"hello"}}')
.digest('hex')
})
const result = await dispatcher.dispatch(
request({ rpcId: 'rpc_1', mutationId: 'mutation_1', subject: 'hello' })
)
expect(result).toMatchObject({ ok: false, error: { code: 'operation_unknown' } })
db.close()
})
it('returns the accepted Dispatch when worker-start was interrupted by restart', async () => {
const db = new OrchestrationDb(':memory:')
const runtime = new OrcaRuntimeService()
runtime.setOrchestrationDb(db)
const params = { from: 'term_coord', task: db.createTask({ spec: 'restart' }).id }
const callerFingerprint = createHash('sha256').update('caller-token').digest('hex')
const payloadHash = createHash('sha256')
.update(JSON.stringify({ method: 'orchestration.workerStart', params }))
.digest('hex')
const started = db.createStartingWorkerDispatch({
taskId: params.task,
startOptions: {},
mutationReceipt: {
callerFingerprint,
requestId: 'mutation_worker_start',
method: 'orchestration.workerStart',
payloadHash
}
})
const effect = vi.fn()
const dispatcher = new RpcDispatcher({
runtime,
methods: [
defineMethod({
name: 'orchestration.workerStart',
params: z.object({ from: z.string(), task: z.string() }),
handler: effect
})
]
})
const result = await dispatcher.dispatch({
id: 'rpc_worker_start_retry',
authToken: 'caller-token',
method: 'orchestration.workerStart',
params,
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'mutation_worker_start'
})
expect(result).toMatchObject({
ok: false,
error: {
code: 'operation_unknown',
data: {
requestId: 'mutation_worker_start',
dispatchId: started.dispatch.id,
recoveryCommand: `orca orchestration worker-show --dispatch ${started.dispatch.id} --json`
}
}
})
expect(effect).not.toHaveBeenCalled()
db.close()
})
it('recovers a lost ask acceptance without creating a second question', async () => {
const db = new OrchestrationDb(':memory:')
const runtime = new OrcaRuntimeService()
runtime.setOrchestrationDb(db)
vi.spyOn(runtime, 'getTerminalPaneKey').mockReturnValue('tab_worker:leaf_worker')
vi.spyOn(runtime, 'getTerminalProcessIncarnation').mockReturnValue('runtime:pty:1')
vi.spyOn(runtime, 'notifyMessageArrived').mockImplementation(() => {})
vi.spyOn(runtime, 'waitForMessage').mockImplementation(
async (_address, options) =>
await new Promise<'cancelled'>((resolve) => {
options?.signal?.addEventListener('abort', () => resolve('cancelled'), { once: true })
})
)
const run = db.createRun({
objective: 'Ask recovery',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:leaf_coord'
})
const task = db.createTask({ spec: 'ask', runId: run.id })
const dispatch = db.createDispatchContext(task.id, 'term_worker', 'tab_worker:leaf_worker')
const capability = db.mintDispatchCapability({
dispatchId: dispatch.id,
paneKey: 'tab_worker:leaf_worker',
processIncarnation: 'runtime:pty:1'
})
const askRequest: RpcRequest = {
id: 'rpc_ask_1',
authToken: 'caller-token',
method: 'orchestration.ask',
params: { from: 'term_worker', question: 'Proceed?', timeoutMs: 60_000 },
orchestrationCapability: capability,
orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION,
orchestrationRequestId: 'mutation_ask'
}
const controller = new AbortController()
const firstDispatcher = new RpcDispatcher({ runtime, methods: ORCHESTRATION_METHODS })
const first = firstDispatcher.dispatch(askRequest, { signal: controller.signal })
await vi.waitFor(() => expect(db.getInbox(10)).toHaveLength(1))
const restartedDispatcher = new RpcDispatcher({ runtime, methods: ORCHESTRATION_METHODS })
const recovered = await restartedDispatcher.dispatch({ ...askRequest, id: 'rpc_ask_2' })
expect(recovered).toMatchObject({
ok: true,
result: {
accepted: true,
messageId: expect.stringMatching(/^msg_/),
mutation: { requestId: 'mutation_ask', replayed: true }
}
})
expect(db.getInbox(10)).toHaveLength(1)
controller.abort()
await first
db.close()
})
})
@@ -0,0 +1,44 @@
import type { FeatureInteractionId } from '../../../shared/feature-interactions'
import { isBrowserPaneUiRuntimeRpcParams } from '../../../shared/runtime-rpc-feature-interaction-source'
export function getRuntimeFeatureInteractionId(
method: string,
result: unknown,
rawParams?: unknown
): FeatureInteractionId | null {
if (method === 'browser.profileImportFromBrowser') {
return hasBooleanResult(result, 'ok') ? 'cookie-import' : null
}
if (method === 'browser.profileClearDefaultCookies') {
return hasBooleanResult(result, 'cleared') ? 'cookie-import' : null
}
if (method === 'browser.screencast.unsubscribe') {
return null
}
if (method.startsWith('browser.') && isBrowserPaneUiRuntimeRpcParams(rawParams)) {
return null
}
if (method.startsWith('browser.') && !method.startsWith('browser.profile')) {
return 'agent-browser-use'
}
if (method.startsWith('emulator.')) {
return null
}
if (method === 'computer.permissions') {
return 'computer-use-setup'
}
if (
method.startsWith('computer.') &&
method !== 'computer.capabilities' &&
method !== 'computer.permissionsStatus'
) {
return 'computer-use'
}
return method.startsWith('orchestration.') ? 'agent-orchestration' : null
}
function hasBooleanResult(value: unknown, key: string): boolean {
return (
value !== null && typeof value === 'object' && (value as Record<string, unknown>)[key] === true
)
}
+55 -15
View File
@@ -26,6 +26,7 @@ import {
import { decrypt, deriveSharedKey, encrypt, generateKeyPair } from './rpc/e2ee-crypto'
import { DeviceRegistry } from './device-registry'
import { DEVICE_REGISTRY_FILENAME, E2EE_KEYPAIR_FILENAME } from './mobile-pairing-files'
import { ORCHESTRATION_CONTRACT_VERSION } from '../../shared/protocol-version'
vi.mock('../git/worktree', () => ({
listWorktrees: vi.fn().mockResolvedValue([
@@ -60,7 +61,7 @@ async function sendRequest(
resolve(JSON.parse(message) as Record<string, unknown>)
})
socket.on('connect', () => {
socket.write(`${JSON.stringify(request)}\n`)
socket.write(`${JSON.stringify(withCurrentOrchestrationContract(request))}\n`)
})
})
}
@@ -111,12 +112,20 @@ function openFramedSession(endpoint: string, request: Record<string, unknown>):
}
})
socket.on('connect', () => {
socket.write(`${JSON.stringify(request)}\n`)
socket.write(`${JSON.stringify(withCurrentOrchestrationContract(request))}\n`)
})
})
return { socket, frames, done }
}
function withCurrentOrchestrationContract(
request: Record<string, unknown>
): Record<string, unknown> {
return typeof request.method === 'string' && request.method.startsWith('orchestration.')
? { ...request, orchestrationContractVersion: ORCHESTRATION_CONTRACT_VERSION }
: request
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
@@ -131,6 +140,18 @@ async function waitFor(predicate: () => boolean, timeoutMs = 2_000): Promise<voi
}
}
function seedSupervisedAskWorkers(db: OrchestrationDb, workerHandles: string[]): void {
const run = db.createRun({
objective: 'Exercise ask admission',
coordinatorHandle: 'term_coord',
coordinatorPaneKey: 'tab_coord:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
})
for (const workerHandle of workerHandles) {
const task = db.createTask({ spec: 'Wait for coordinator input', runId: run.id })
db.createDispatchContext(task.id, workerHandle)
}
}
function connectWs(endpoint: string): Promise<WebSocket> {
return new Promise((resolve, reject) => {
const ws = new WebSocket(endpoint)
@@ -1285,12 +1306,14 @@ describe('OrcaRuntimeRpcServer', () => {
try {
const first = server['handleWebSocketMessage'](
JSON.stringify({
id: 'req_wait',
method: 'orchestration.check',
deviceToken: entry.token,
params: { terminal: 'term_wait', wait: true, timeoutMs: 10_000 }
}),
JSON.stringify(
withCurrentOrchestrationContract({
id: 'req_wait',
method: 'orchestration.check',
deviceToken: entry.token,
params: { terminal: 'term_wait', wait: true, timeoutMs: 10_000 }
})
),
(response) => replies.push(JSON.parse(response) as Record<string, unknown>),
() => {},
undefined,
@@ -1300,12 +1323,14 @@ describe('OrcaRuntimeRpcServer', () => {
await waitFor(() => server['activeLongPolls'] === 1)
await server['handleWebSocketMessage'](
JSON.stringify({
id: 'req_busy',
method: 'orchestration.check',
deviceToken: entry.token,
params: { terminal: 'term_busy', wait: true, timeoutMs: 10_000 }
}),
JSON.stringify(
withCurrentOrchestrationContract({
id: 'req_busy',
method: 'orchestration.check',
deviceToken: entry.token,
params: { terminal: 'term_busy', wait: true, timeoutMs: 10_000 }
})
),
(response) => replies.push(JSON.parse(response) as Record<string, unknown>),
() => {},
undefined,
@@ -1338,6 +1363,7 @@ describe('OrcaRuntimeRpcServer', () => {
const runtime = new OrcaRuntimeService()
const db = new OrchestrationDb(':memory:')
runtime.setOrchestrationDb(db)
seedSupervisedAskWorkers(db, ['term_w0', 'term_w1', 'term_w2'])
// Why: cap 4 → ask sub-cap 2, so the third ask must be shed while waits keep the other half.
const server = new OrcaRuntimeRpcServer({
runtime,
@@ -1358,7 +1384,9 @@ describe('OrcaRuntimeRpcServer', () => {
}
const dispatch = (id: string, method: string, params: unknown): Promise<void> =>
server['handleWebSocketMessage'](
JSON.stringify({ id, method, deviceToken: entry.token, params }),
JSON.stringify(
withCurrentOrchestrationContract({ id, method, deviceToken: entry.token, params })
),
push,
() => {},
undefined,
@@ -4032,6 +4060,17 @@ describe('OrcaRuntimeRpcServer', () => {
const runtime = new OrcaRuntimeService()
const db = new OrchestrationDb(':memory:')
runtime.setOrchestrationDb(db)
const askerPaneKey = 'tab_asker:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
vi.spyOn(runtime, 'getTerminalPaneKey').mockImplementation((handle) =>
handle === 'term_asker' ? askerPaneKey : null
)
const run = db.createRun({
objective: 'Keepalive test',
coordinatorHandle: 'term_nobody',
coordinatorPaneKey: 'tab_coord:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
})
const task = db.createTask({ spec: 'Wait for an answer', runId: run.id })
db.createDispatchContext(task.id, 'term_asker', askerPaneKey)
const server = new OrcaRuntimeRpcServer({
runtime,
userDataPath,
@@ -4380,6 +4419,7 @@ describe('OrcaRuntimeRpcServer', () => {
const runtime = new OrcaRuntimeService()
const db = new OrchestrationDb(':memory:')
runtime.setOrchestrationDb(db)
seedSupervisedAskWorkers(db, ['term_w0', 'term_w1', 'term_w2', 'term_w3'])
// Why: cap 4 → ask sub-cap 2, so 4 concurrent asks can only take half the budget.
const server = new OrcaRuntimeRpcServer({
runtime,