Implement robust orchestration primitives and connected-server workers (#9925)
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import type { RpcResponse } from '../runtime/rpc/core'
|
||||
|
||||
export function buildRemoteCliError(message: string, code = 'runtime_error'): RpcResponse {
|
||||
return {
|
||||
id: 'remote-cli-local',
|
||||
ok: false,
|
||||
error: { code, message },
|
||||
_meta: { runtimeId: 'unknown' }
|
||||
}
|
||||
}
|
||||
@@ -79,7 +79,8 @@ describe('runRemoteOrcaCli', () => {
|
||||
message.read_at = new Date(0).toISOString()
|
||||
}
|
||||
}
|
||||
})
|
||||
}),
|
||||
getActiveDispatchForIdentity: vi.fn(() => undefined)
|
||||
}
|
||||
const runtime = {
|
||||
getRuntimeId: () => 'runtime-test',
|
||||
@@ -155,7 +156,7 @@ describe('runRemoteOrcaCli', () => {
|
||||
expect(db.getUnreadMessages('term_windows')[0]?.from_handle).toBe('term_ssh')
|
||||
})
|
||||
|
||||
it('forwards remote pane identity through the legacy orchestration fallback', async () => {
|
||||
it('does not trust caller-supplied remote pane identity in the legacy fallback', async () => {
|
||||
const { runtime, db } = createRuntime()
|
||||
|
||||
const result = await runRemoteOrcaCli(
|
||||
@@ -173,7 +174,7 @@ describe('runRemoteOrcaCli', () => {
|
||||
|
||||
expect(result.exitCode).toBe(0)
|
||||
expect(db.insertMessage).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ senderPaneKey: 'tab_ssh:leaf_ssh' })
|
||||
expect.objectContaining({ senderPaneKey: undefined })
|
||||
)
|
||||
})
|
||||
|
||||
@@ -183,8 +184,14 @@ describe('runRemoteOrcaCli', () => {
|
||||
runtime.setOrchestrationDb(db)
|
||||
vi.spyOn(runtime, 'deliverPendingMessagesForHandle').mockImplementation(() => {})
|
||||
vi.spyOn(runtime, 'notifyMessageArrived').mockImplementation(() => {})
|
||||
const task = db.createTask({ spec: 'remote work' })
|
||||
const run = db.createRun({
|
||||
objective: 'Remote lifecycle rejection',
|
||||
coordinatorHandle: 'term_coord',
|
||||
coordinatorPaneKey: 'tab_coord:leaf_coord'
|
||||
})
|
||||
const task = db.createTask({ spec: 'remote work', runId: run.id })
|
||||
const dispatch = db.createDispatchContext(task.id, 'term_ssh', 'tab_owner:leaf_owner')
|
||||
vi.spyOn(runtime, 'getTerminalPaneKey').mockReturnValue('tab_foreign:leaf_foreign')
|
||||
|
||||
try {
|
||||
const result = await runRemoteOrcaCli(
|
||||
@@ -202,7 +209,11 @@ describe('runRemoteOrcaCli', () => {
|
||||
'--type',
|
||||
'worker_done',
|
||||
'--payload',
|
||||
JSON.stringify({ taskId: task.id, dispatchId: dispatch.id }),
|
||||
JSON.stringify({
|
||||
taskId: task.id,
|
||||
dispatchId: dispatch.id,
|
||||
outcome: 'succeeded'
|
||||
}),
|
||||
'--json'
|
||||
],
|
||||
cwd: '/home/alice/repo',
|
||||
@@ -231,8 +242,14 @@ describe('runRemoteOrcaCli', () => {
|
||||
runtime.setOrchestrationDb(db)
|
||||
vi.spyOn(runtime, 'deliverPendingMessagesForHandle').mockImplementation(() => {})
|
||||
vi.spyOn(runtime, 'notifyMessageArrived').mockImplementation(() => {})
|
||||
const task = db.createTask({ spec: 'remote work' })
|
||||
const run = db.createRun({
|
||||
objective: 'Remote lifecycle success',
|
||||
coordinatorHandle: 'term_coord',
|
||||
coordinatorPaneKey: 'tab_coord:leaf_coord'
|
||||
})
|
||||
const task = db.createTask({ spec: 'remote work', runId: run.id })
|
||||
const dispatch = db.createDispatchContext(task.id, 'term_ssh', 'tab_owner:leaf_owner')
|
||||
vi.spyOn(runtime, 'getTerminalPaneKey').mockReturnValue('tab_owner:leaf_owner')
|
||||
|
||||
try {
|
||||
const result = await runRemoteOrcaCli(
|
||||
@@ -251,6 +268,8 @@ describe('runRemoteOrcaCli', () => {
|
||||
task.id,
|
||||
'--dispatch-id',
|
||||
dispatch.id,
|
||||
'--outcome',
|
||||
'succeeded',
|
||||
'--files-modified',
|
||||
'src/a.ts, src/b.ts',
|
||||
'--json'
|
||||
@@ -274,6 +293,70 @@ describe('runRemoteOrcaCli', () => {
|
||||
}
|
||||
})
|
||||
|
||||
it('carries the Dispatch capability through the SSH envelope', async () => {
|
||||
const db = new OrchestrationDb(':memory:')
|
||||
const runtime = new OrcaRuntimeService()
|
||||
runtime.setOrchestrationDb(db)
|
||||
vi.spyOn(runtime, 'deliverPendingMessagesForHandle').mockImplementation(() => {})
|
||||
vi.spyOn(runtime, 'notifyMessageArrived').mockImplementation(() => {})
|
||||
vi.spyOn(runtime, 'getTerminalPaneKey').mockReturnValue('tab_ssh:leaf_ssh')
|
||||
vi.spyOn(runtime, 'getTerminalProcessIncarnation').mockReturnValue('ssh_runtime:pty:1')
|
||||
const run = db.createRun({
|
||||
objective: 'SSH capability transport',
|
||||
coordinatorHandle: 'term_coord',
|
||||
coordinatorPaneKey: 'tab_coord:leaf_coord'
|
||||
})
|
||||
const task = db.createTask({ spec: 'remote work', runId: run.id })
|
||||
const started = db.createStartingWorkerDispatch({ taskId: task.id, startOptions: {} })
|
||||
const capability = db.prepareStartingWorkerAuthority({
|
||||
dispatchId: started.dispatch.id,
|
||||
handle: 'term_ssh',
|
||||
paneKey: 'tab_ssh:leaf_ssh',
|
||||
processIncarnation: 'ssh_runtime:pty:1',
|
||||
worktreeId: 'repo::/home/alice/repo',
|
||||
setupState: 'not_applicable',
|
||||
effects: []
|
||||
})
|
||||
db.markWorkerDispatchReady(started.dispatch.id)
|
||||
|
||||
try {
|
||||
const result = await runRemoteOrcaCli(
|
||||
runtime,
|
||||
{
|
||||
argv: [
|
||||
'orchestration',
|
||||
'send',
|
||||
'--type',
|
||||
'worker_done',
|
||||
'--subject',
|
||||
'Done',
|
||||
'--task-id',
|
||||
task.id,
|
||||
'--dispatch-id',
|
||||
started.dispatch.id,
|
||||
'--outcome',
|
||||
'succeeded',
|
||||
'--dispatch-capability',
|
||||
capability,
|
||||
'--json'
|
||||
],
|
||||
cwd: '/home/alice/repo',
|
||||
env: {
|
||||
ORCA_TERMINAL_HANDLE: 'term_ssh',
|
||||
ORCA_PANE_KEY: 'tab_ssh:leaf_ssh'
|
||||
}
|
||||
},
|
||||
LEGACY_FALLBACK_OPTIONS
|
||||
)
|
||||
|
||||
expect(result.exitCode).toBe(0)
|
||||
expect(db.getTask(task.id)).toMatchObject({ status: 'completed' })
|
||||
expect(db.getWorkerDispatch(started.dispatch.id)).toMatchObject({ state: 'succeeded' })
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
})
|
||||
|
||||
it('rejects identity-less lifecycle sends in the legacy fallback', async () => {
|
||||
const { runtime, db } = createRuntime()
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import type { CliStatusResult, RuntimeStatus } from '../../shared/runtime-types'
|
||||
import type { RuntimeOrchestrationEnvelope } from '../../shared/runtime-rpc-envelope'
|
||||
import { ORCHESTRATION_CONTRACT_VERSION } from '../../shared/protocol-version'
|
||||
import { RpcDispatcher } from '../runtime/rpc/dispatcher'
|
||||
import type { RpcResponse } from '../runtime/rpc/core'
|
||||
import type { OrcaRuntimeService } from '../runtime/orca-runtime'
|
||||
@@ -18,6 +20,7 @@ import {
|
||||
requiredRemoteCliString,
|
||||
resolveRemoteCliHandle
|
||||
} from './ssh-remote-cli-args'
|
||||
import { buildRemoteCliError } from './ssh-remote-cli-error-response'
|
||||
import { getRemoteLinearHelp, tryDispatchRemoteLinearCli } from './ssh-remote-linear-cli'
|
||||
import {
|
||||
getRemoteOrchestrationPayload,
|
||||
@@ -51,7 +54,7 @@ export async function runRemoteOrcaCli(
|
||||
if (interactiveMessage) {
|
||||
if (json) {
|
||||
return {
|
||||
stdout: `${JSON.stringify(buildLocalError(interactiveMessage, 'unsupported_over_ssh'), null, 2)}\n`,
|
||||
stdout: `${JSON.stringify(buildRemoteCliError(interactiveMessage, 'unsupported_over_ssh'), null, 2)}\n`,
|
||||
stderr: '',
|
||||
exitCode: 1
|
||||
}
|
||||
@@ -117,7 +120,7 @@ async function runLegacyRemoteOrcaCli(
|
||||
: 'runtime_error'
|
||||
if (json) {
|
||||
return {
|
||||
stdout: `${JSON.stringify(buildLocalError(message, code), null, 2)}\n`,
|
||||
stdout: `${JSON.stringify(buildRemoteCliError(message, code), null, 2)}\n`,
|
||||
stderr: '',
|
||||
exitCode: 1
|
||||
}
|
||||
@@ -167,19 +170,27 @@ async function dispatchRemoteCli(
|
||||
})
|
||||
case 'orchestration send': {
|
||||
const type = optionalRemoteCliString(parsed.flags, 'type')
|
||||
return await call(dispatcher, 'orchestration.send', {
|
||||
from: resolveRemoteOrchestrationSender(parsed.flags, env, type),
|
||||
to: requiredRemoteCliString(parsed.flags, 'to'),
|
||||
subject: requiredRemoteCliString(parsed.flags, 'subject'),
|
||||
body: optionalRemoteCliString(parsed.flags, 'body'),
|
||||
type,
|
||||
priority: optionalRemoteCliString(parsed.flags, 'priority'),
|
||||
threadId: optionalRemoteCliString(parsed.flags, 'thread-id'),
|
||||
payload: getRemoteOrchestrationPayload(parsed.flags),
|
||||
// Why: the legacy in-process bridge must preserve the same pane
|
||||
// authority as the full host CLI passthrough.
|
||||
senderPaneKey: env.ORCA_PANE_KEY || undefined
|
||||
})
|
||||
return await call(
|
||||
dispatcher,
|
||||
'orchestration.send',
|
||||
{
|
||||
from: resolveRemoteOrchestrationSender(parsed.flags, env, type),
|
||||
to: optionalRemoteCliString(parsed.flags, 'to'),
|
||||
subject: requiredRemoteCliString(parsed.flags, 'subject'),
|
||||
body: optionalRemoteCliString(parsed.flags, 'body'),
|
||||
type,
|
||||
priority: optionalRemoteCliString(parsed.flags, 'priority'),
|
||||
threadId: optionalRemoteCliString(parsed.flags, 'thread-id'),
|
||||
payload: getRemoteOrchestrationPayload(parsed.flags),
|
||||
// Why: the legacy in-process bridge must preserve the same pane
|
||||
// authority as the full host CLI passthrough.
|
||||
senderPaneKey: env.ORCA_PANE_KEY || undefined
|
||||
},
|
||||
{
|
||||
orchestrationCapability: optionalRemoteCliString(parsed.flags, 'dispatch-capability'),
|
||||
orchestrationRequestId: optionalRemoteCliString(parsed.flags, 'retry-request')
|
||||
}
|
||||
)
|
||||
}
|
||||
case 'orchestration check':
|
||||
return await call(dispatcher, 'orchestration.check', {
|
||||
@@ -215,21 +226,18 @@ async function dispatchRemoteCli(
|
||||
async function call(
|
||||
dispatcher: RpcDispatcher,
|
||||
method: string,
|
||||
params?: Record<string, unknown>
|
||||
params?: Record<string, unknown>,
|
||||
envelope?: RuntimeOrchestrationEnvelope
|
||||
): Promise<RpcResponse> {
|
||||
return await dispatcher.dispatch({
|
||||
id: `remote-cli-${Date.now()}`,
|
||||
authToken: 'remote-cli',
|
||||
method,
|
||||
params
|
||||
params,
|
||||
orchestrationCapability: envelope?.orchestrationCapability,
|
||||
orchestrationContractVersion: method.startsWith('orchestration.')
|
||||
? ORCHESTRATION_CONTRACT_VERSION
|
||||
: undefined,
|
||||
orchestrationRequestId: envelope?.orchestrationRequestId
|
||||
})
|
||||
}
|
||||
|
||||
function buildLocalError(message: string, code = 'runtime_error'): RpcResponse {
|
||||
return {
|
||||
id: 'remote-cli-local',
|
||||
ok: false,
|
||||
error: { code, message },
|
||||
_meta: { runtimeId: 'unknown' }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ describe('remote orchestration send compatibility', () => {
|
||||
new Map([
|
||||
['task-id', 'task_1'],
|
||||
['dispatch-id', 'ctx_1'],
|
||||
['outcome', 'succeeded'],
|
||||
['files-modified', 'src/a.ts, src/b.ts,'],
|
||||
['report-path', 'report.md'],
|
||||
['phase', 'reviewing']
|
||||
@@ -47,6 +48,7 @@ describe('remote orchestration send compatibility', () => {
|
||||
expect(JSON.parse(payload ?? '{}')).toEqual({
|
||||
taskId: 'task_1',
|
||||
dispatchId: 'ctx_1',
|
||||
outcome: 'succeeded',
|
||||
filesModified: ['src/a.ts', 'src/b.ts'],
|
||||
reportPath: 'report.md',
|
||||
phase: 'reviewing'
|
||||
|
||||
@@ -37,10 +37,11 @@ export function getRemoteOrchestrationPayload(flags: RemoteFlags): string | unde
|
||||
const rawPayload = optionalString(flags, 'payload')
|
||||
const taskId = optionalString(flags, 'task-id')
|
||||
const dispatchId = optionalString(flags, 'dispatch-id')
|
||||
const outcome = optionalString(flags, 'outcome')
|
||||
const filesModified = optionalString(flags, 'files-modified')
|
||||
const reportPath = optionalString(flags, 'report-path')
|
||||
const phase = optionalString(flags, 'phase')
|
||||
const hasStructuredPayload = [taskId, dispatchId, filesModified, reportPath, phase].some(
|
||||
const hasStructuredPayload = [taskId, dispatchId, outcome, filesModified, reportPath, phase].some(
|
||||
(value) => value !== undefined
|
||||
)
|
||||
if (!hasStructuredPayload) {
|
||||
@@ -62,6 +63,15 @@ export function getRemoteOrchestrationPayload(flags: RemoteFlags): string | unde
|
||||
if (dispatchId) {
|
||||
payload.dispatchId = dispatchId
|
||||
}
|
||||
if (outcome) {
|
||||
if (outcome !== 'succeeded' && outcome !== 'failed') {
|
||||
throw new RemoteCliArgumentError(
|
||||
'invalid_argument',
|
||||
'Invalid --outcome. Expected succeeded or failed.'
|
||||
)
|
||||
}
|
||||
payload.outcome = outcome
|
||||
}
|
||||
if (filesModified) {
|
||||
payload.filesModified = filesModified
|
||||
.split(',')
|
||||
|
||||
Reference in New Issue
Block a user