Prevent worker completion reports from reaching unrelated agents (#5333)

This commit is contained in:
Brennan Benson
2026-06-13 21:28:12 -07:00
committed by GitHub
parent 36277801e4
commit 2acf8549f3
6 changed files with 254 additions and 25 deletions
+70
View File
@@ -1,14 +1,20 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
const callMock = vi.fn()
const getTerminalHandleMock = vi.hoisted(() => vi.fn())
const originalTerminalHandle = process.env.ORCA_TERMINAL_HANDLE
function lifecycleGroupRecipientError(type: 'worker_done' | 'heartbeat'): string {
return `${type} messages must be sent to a concrete coordinator terminal handle, not a group address.`
}
// Why: isolate the handler's flag-to-param mapping; printResult only writes output.
vi.mock('../format', () => ({ printResult: vi.fn() }))
vi.mock('../selectors', () => ({ getTerminalHandle: getTerminalHandleMock }))
import { ORCHESTRATION_HANDLERS } from './orchestration'
afterEach(() => {
getTerminalHandleMock.mockReset()
if (originalTerminalHandle === undefined) {
delete process.env.ORCA_TERMINAL_HANDLE
} else {
@@ -59,6 +65,7 @@ describe('orchestration reset CLI handler', () => {
describe('orchestration send structured payload flags', () => {
beforeEach(() => {
callMock.mockReset().mockResolvedValue({ result: { message: { id: 'msg_1' } } })
getTerminalHandleMock.mockReset()
delete process.env.ORCA_TERMINAL_HANDLE
})
@@ -116,6 +123,69 @@ describe('orchestration send structured payload flags', () => {
).rejects.toThrow(/structured payload/)
expect(callMock).not.toHaveBeenCalled()
})
it('rejects worker_done group sends before resolving a sender handle', async () => {
getTerminalHandleMock.mockRejectedValue(new Error('sender resolution should not run'))
await expect(
invokeSend(
new Map<string, string | boolean>([
['to', '@all'],
['subject', 'done'],
['type', 'worker_done']
])
)
).rejects.toMatchObject({
code: 'invalid_argument',
message: lifecycleGroupRecipientError('worker_done')
})
expect(getTerminalHandleMock).not.toHaveBeenCalled()
expect(callMock).not.toHaveBeenCalled()
})
it('rejects heartbeat group sends before resolving a sender handle', async () => {
getTerminalHandleMock.mockRejectedValue(new Error('sender resolution should not run'))
await expect(
invokeSend(
new Map<string, string | boolean>([
['to', '@idle'],
['subject', 'alive'],
['type', 'heartbeat']
])
)
).rejects.toMatchObject({
code: 'invalid_argument',
message: lifecycleGroupRecipientError('heartbeat')
})
expect(getTerminalHandleMock).not.toHaveBeenCalled()
expect(callMock).not.toHaveBeenCalled()
})
it('continues to allow worker_done to a concrete terminal handle', async () => {
await invokeSend(
new Map<string, string | boolean>([
['from', 'term_worker'],
['to', 'term_coord'],
['subject', 'done'],
['type', 'worker_done']
])
)
expect(callMock).toHaveBeenCalledWith('orchestration.send', {
from: 'term_worker',
to: 'term_coord',
subject: 'done',
body: undefined,
type: 'worker_done',
priority: undefined,
threadId: undefined,
payload: undefined,
devMode: false
})
})
})
describe('orchestration timeout flag validation', () => {
+15 -2
View File
@@ -14,6 +14,9 @@ import { getTerminalHandle } from '../selectors'
// parent process the subprocess is alive without flooding logs. See design
// doc §3.4.
const DEFAULT_HEARTBEAT_INTERVAL_MS = 15_000
function getLifecycleGroupRecipientError(type: 'worker_done' | 'heartbeat'): string {
return `${type} messages must be sent to a concrete coordinator terminal handle, not a group address.`
}
// Why: test-only escape hatch so subprocess tests can verify the feature in
// under 10 s rather than needing a full 15 s silence window. Production users
@@ -162,17 +165,27 @@ function getOptionalPositiveIntegerValueFlag(
return value
}
function rejectLifecycleGroupRecipient(type: string | undefined, to: string): void {
if ((type === 'worker_done' || type === 'heartbeat') && to.startsWith('@')) {
throw new RuntimeClientError('invalid_argument', getLifecycleGroupRecipientError(type))
}
}
export const ORCHESTRATION_HANDLERS: Record<string, CommandHandler> = {
'orchestration send': async ({ flags, client, cwd, json }) => {
const to = getRequiredStringFlag(flags, 'to')
const type = getOptionalStringFlag(flags, 'type')
rejectLifecycleGroupRecipient(type, to)
const from = await resolveOrchestrationTerminalHandle(flags, cwd, client, 'from')
const result = await client.call<
{ message: { id: string } } | { messages: { id: string }[]; recipients: number }
>('orchestration.send', {
from,
to: getRequiredStringFlag(flags, 'to'),
to,
subject: getRequiredStringFlag(flags, 'subject'),
body: getOptionalStringFlag(flags, 'body'),
type: getOptionalStringFlag(flags, 'type'),
type,
priority: getOptionalStringFlag(flags, 'priority'),
threadId: getOptionalStringFlag(flags, 'thread-id'),
payload: getOptionalStructuredMessagePayload(flags),
+1
View File
@@ -25,6 +25,7 @@ export const ORCHESTRATION_COMMAND_SPECS: CommandSpec[] = [
],
notes: [
'On Windows PowerShell, quote group addresses such as --to "@all" or --to "@worktree:<id>".',
'worker_done and heartbeat must target a concrete coordinator terminal handle; use status for broadcast updates.',
'Prefer --task-id/--dispatch-id/etc. over raw --payload JSON in worker commands; PowerShell strips JSON quotes easily.'
]
},