Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(orca): enforce Worker routing and execution-channel disclosure (#309)
qsoyqf3c7cfacef07
diff --git a/docs/dev-rules/orca-team-architecture.md b/docs/dev-rules/orca-team-architecture.mdindex f4f4699b63..c22a087d0d 100644--- a/docs/dev-rules/orca-team-architecture.md+++ b/docs/dev-rules/orca-team-architecture.md@@ -159,6 +159,49 @@ Worktree 现状:Orca 与普通 session 对齐,worktree 是可选项,不强本节记录当前系统必须持续满足的运行时不变量。它们不是远期规划,而是 Lead / Worker 协同时已经依赖的行为契约。+#### Lead 派单与执行通道++1. **Orca Worker 派单不得由原生 subagent 冒充(状态:不变量)**<br>+ 用户把任务指派给已有 Worker 的 role/label、要求 Lead 向 Worker 派单,或在 active team+ 内按多个角色并行派单时,Lead 必须先读 workspace,再按场景通过+ `send_to_worker`(复用既有 Worker)/`create_worker`(显式新建一个 Worker)/`create_workers`(显式新建多个 Worker)进入 Orca 状态机;如果请求的 role/label 当前没有匹配的既有 Worker,Lead 必须先如实说明没有匹配项并征求是否创建,不能静默改派别的 Worker,也不能退回 native subagent。+ 如果用户只泛称“Worker”且当前没有任何 Worker,Lead 同样必须先说明并询问是否创建;+ 任务指派本身不等于创建授权。多角色请求必须在派发任何任务前解析全部 role/label;+ 只要有目标缺失或创建授权未决,就不得部分派发,必须集中列出缺失项并先询问。全部映射+ 和授权确定后,多角色请求必须在一个并行 tool-call batch 内一次发出:既有目标走+ `send_to_worker`,恰好一个授权新目标走带 `initial_task` 的 `create_worker`,两个及以上+ 授权新目标走一次带齐 `initial_task` 的 `create_workers`;不能等待某个派发结果后才发+ 剩余任务。Codex+ `spawn_agent` 或 Claude Code Agent/Task 的完成结果不能当成 Orca+ Worker 的完成。只有用户明确要求一次性 subagent/子代理且没有把任务指派给 Orca+ Worker,或明确要求不使用 Orca Worker 时,才走原生 subagent。++2. **实际执行通道必须披露并按同一通道验收(状态:不变量)**<br>+ Lead 汇总前必须按执行该任务的同一通道确认真实终态,并逐项标明+ `Orca Worker` 或 `native subagent`。原生 subagent 由 Codex+ `collabAgentToolCall`/Claude Agent/Task 任务卡展示标识、任务和终态,但不得写入 Orca+ Worker 状态,也不得触发 Orca Worker 完成提醒。实现指针:+ `packages/orca-workflow/src/orca-bridge-prompt.ts` 的+ `renderOrcaLeadSystemPrompt`,以及 `packages/maker-core/src/agents/codex/translator.ts`+ 的 `handleCollabAgentToolCall`。++3. **只有真实异步派发才静默结束 turn(状态:不变量)**<br>+ `send_to_worker` 只有返回 `ok=true` 且 `wake_kind` 为 `resumed`/`already-active`/+ `queued` 时才算真实派发。`create_worker` 及 `create_workers` 的每个 created 结果,+ 只有 `dispatched=true`、存在 `queued_message_id`,或+ `dispatch_outcome.kind=session-dispatch` 且 `dispatch_outcome.dispatched=true`+ (包括 `dispatch_outcome.wakeKind=queued`)时才算真实派发。Lead 只能据这些具体信号+ 零输出结束当前 turn;工具失败、只创建 Worker 而没有首任务,或首任务未派发时,必须+ 立即向用户报告真实结果,不能等待一个不会到来的 Worker 回报。`create_workers` 每次+ 返回都必须先转告 `user_report`(若存在)并汇总逐项终态。多角色 batch 返回后还必须+ 汇总其他工具的失败/未派发结果;若至少一个结果满足上述派发信号,只做这一次合并报告+ 后立即结束 turn,全部成功且无 `create_workers` 必报内容时零输出结束。若没有任何任务+ 派发,则报告结果后等待用户决定。所有情况都不得继续调用工具,也不 sleep、不 poll。+ 实现指针:+ `packages/orca-workflow/src/orca-bridge-prompt.ts` 的+ `renderOrcaLeadSystemPrompt`,以及 `packages/lizi-mcps/src/xdt-helper/create_workers.ts`+ 的批量结果契约。+#### 消息派发与 auto-bridge1. **忙碌目标不丢消息(状态:不变量)**<br>diff --git a/packages/orca-workflow/src/__tests__/orca-bridge-prompt.test.ts b/packages/orca-workflow/src/__tests__/orca-bridge-prompt.test.tsindex af6f0ea222..dd5eae89bb 100644--- a/packages/orca-workflow/src/__tests__/orca-bridge-prompt.test.ts+++ b/packages/orca-workflow/src/__tests__/orca-bridge-prompt.test.ts@@ -3,19 +3,108 @@ import { describe, expect, it } from 'vitest';import { renderOrcaLeadSystemPrompt, renderOrcaWorkerSystemPrompt } from '../orca-bridge-prompt.js';describe('renderOrcaLeadSystemPrompt', () => {- const subagentHint =- 'If the user explicitly asks for a "subagent" / "子代理", use your native subagent mechanism (Codex: spawn_agent; Claude Code: the Agent/Task tool) — do not translate that request into an Orca worker (create_worker / start_team).';+ const workerRoutingRule =+ 'An assignment to an Orca Worker MUST use the Orca tools below.';+ const nativeSubagentBoundary =+ 'only when the user explicitly asks for a "subagent" / "子代理" without assigning the task to an Orca Worker';+ const channelDisclosureRule =+ 'label every delegated task with its actual execution channel: Orca Worker or native subagent.';+ const toolSurfaceRule =+ 'Tools: get_workspace_info, create_worker, create_workers, send_to_worker.';+ const explicitCreationBoundary =+ 'Use create_worker only when the user explicitly asks to open one new worker, and use create_workers only when the user explicitly asks to open multiple new workers.';+ const missingWorkerBoundary =+ 'If no existing worker matches a requested role or label, say so and ask whether to create one; do not silently substitute another worker or a native subagent.';+ const missingGenericWorkerBoundary =+ 'If the user assigns a task to a generic Orca Worker but no worker exists, say so and ask whether to create one; the assignment itself is not authorization to create it.';+ const multiRoleReadinessBoundary =+ 'For a multi-role request, resolve every requested role or label before dispatching any task; if any target is missing or its creation approval is unresolved, dispatch nothing, report all missing targets, and ask first.';+ const multiRoleBatchBoundary =+ 'For a fully resolved multi-role request, issue every required Orca dispatch in one parallel tool-call batch';+ const sendDispatchSignals =+ 'Treat send_to_worker as dispatched only when its payload has ok=true and wake_kind=resumed, already-active, or queued.';+ const createDispatchSignals =+ 'Treat create_worker, and each created result from create_workers, as dispatched only when it has dispatched=true, a queued_message_id, or dispatch_outcome.kind=session-dispatch with dispatch_outcome.dispatched=true (including dispatch_outcome.wakeKind=queued).';+ const batchResultRule =+ 'After a multi-role tool-call batch returns, always relay create_workers.user_report verbatim when present, summarize every create_workers per-item result or error, and report every other failed/no-dispatch tool result.';- it('adds the subagent routing hint for leads', () => {+ it('routes explicit Worker assignments through Orca before considering native subagents', () => {const prompt = renderOrcaLeadSystemPrompt(null);- expect(prompt).toContain(subagentHint);+ expect(prompt).toContain(workerRoutingRule);+ expect(prompt).toContain(nativeSubagentBoundary);+ expect(prompt).toContain('Codex: spawn_agent; Claude Code: the Agent/Task tool');+ expect(prompt).not.toContain('followup_task');+ expect(prompt.indexOf(workerRoutingRule)).toBeLessThan(prompt.indexOf(nativeSubagentBoundary));+ });++ it('requires execution-channel disclosure and terminal-state verification', () => {+ const prompt = renderOrcaLeadSystemPrompt(null);++ expect(prompt).toContain(+ 'Show the native subagent identifier, assigned task, and actual terminal status',+ );+ expect(prompt).toContain(+ 'A native subagent result is not evidence that an Orca Worker ran or completed.',+ );+ expect(prompt).toContain(channelDisclosureRule);+ });++ it('declares create_workers while preserving batch result reporting boundaries', () => {+ const prompt = renderOrcaLeadSystemPrompt(null);++ expect(prompt).toContain(toolSurfaceRule);+ expect(prompt).toContain(sendDispatchSignals);+ expect(prompt).toContain(createDispatchSignals);+ expect(prompt).toContain(batchResultRule);+ expect(prompt).toContain(multiRoleBatchBoundary);+ expect(prompt).toContain(+ 'send_to_worker for existing targets, create_worker for exactly one authorized new target, and one create_workers call for 2+ authorized new targets.',+ );+ expect(prompt).toContain(+ 'If any task has the concrete dispatch signals above, end the turn immediately after that single combined result report.',+ );+ expect(prompt).toContain(+ 'If every task dispatched and no create_workers report is required, produce ZERO output.',+ );+ expect(prompt).not.toContain(+ 'After create_worker, create_workers, or send_to_worker returns, your turn is OVER.',+ );+ expect(prompt).not.toContain(+ 'After calling create_worker, create_workers, or send_to_worker, your turn ENDS immediately.',+ );});- it('keeps the subagent routing hint when an initial worker exists', () => {+ it('reports failed or no-dispatch tool results instead of waiting for a worker wake-up', () => {+ const prompt = renderOrcaLeadSystemPrompt(null);++ expect(prompt).toContain(+ 'If create_worker or send_to_worker fails or lacks those dispatch signals, report the result immediately instead of ending silently.',+ );+ expect(prompt).toContain(+ 'Silence is tied to the concrete dispatch signals above, not merely to calling a tool.',+ );+ expect(prompt).toContain(+ 'For a fully resolved multi-role request, issue all required Orca dispatches together in the one parallel tool-call batch described above',+ );+ expect(prompt).not.toContain('when the tool result says a task was accepted or queued');+ });++ it('distinguishes reuse from explicit worker creation and forbids silent fallback', () => {+ const prompt = renderOrcaLeadSystemPrompt(null);++ expect(prompt).toContain(explicitCreationBoundary);+ expect(prompt).toContain(missingWorkerBoundary);+ expect(prompt).toContain(missingGenericWorkerBoundary);+ expect(prompt).toContain(multiRoleReadinessBoundary);+ });++ it('keeps Worker routing and disclosure rules when an initial worker exists', () => {const prompt = renderOrcaLeadSystemPrompt({ workerId: 'worker-1', sessionId: 'session-1' });- expect(prompt).toContain(subagentHint);+ expect(prompt).toContain(workerRoutingRule);+ expect(prompt).toContain(toolSurfaceRule);+ expect(prompt).toContain(channelDisclosureRule);});});diff --git a/packages/orca-workflow/src/orca-bridge-prompt.ts b/packages/orca-workflow/src/orca-bridge-prompt.tsindex abbc2e0285..182b443a4d 100644--- a/packages/orca-workflow/src/orca-bridge-prompt.ts+++ b/packages/orca-workflow/src/orca-bridge-prompt.ts@@ -24,21 +24,28 @@ export function parseOrcaInitialWorkerRef(value: unknown): OrcaInitialWorkerRefexport function renderOrcaLeadSystemPrompt(initialWorker?: OrcaInitialWorkerRef | null): string {const lines = ['You are the lead agent in an Orca multi-agent workflow. Prefer delegating implementation work to workers via tools instead of doing it yourself.',- 'If the user explicitly asks for a "subagent" / "子代理", use your native subagent mechanism (Codex: spawn_agent; Claude Code: the Agent/Task tool) — do not translate that request into an Orca worker (create_worker / start_team).',+ 'An assignment to an Orca Worker MUST use the Orca tools below. This includes tasks addressed to an existing worker by role or label, explicit requests to open new Orca workers, requests for the Lead to delegate to workers, and parallel role assignments while an Orca team is active. Native subagents do not satisfy an Orca Worker assignment.',+ 'Use your native subagent mechanism (Codex: spawn_agent; Claude Code: the Agent/Task tool) only when the user explicitly asks for a "subagent" / "子代理" without assigning the task to an Orca Worker, or explicitly says not to use Orca Workers. A generic role name or a request to delegate in collaboration mode is not permission to switch execution channels.',+ 'If a native subagent is used, explicitly tell the user that the task did not use an Orca Worker. Show the native subagent identifier, assigned task, and actual terminal status, and never report its result as an Orca Worker completion.','',- 'Tools: get_workspace_info, create_worker, send_to_worker.',+ 'Tools: get_workspace_info, create_worker, create_workers, send_to_worker.','(worker_status and read_worker also exist but are for emergency diagnostics only — do NOT use them for normal polling.)','','Messages from workers arrive prefixed with [From Orca Worker]. Treat them as worker reports or questions, not user messages.','','How the async workflow works:',- '- When you call create_worker or send_to_worker, the task is sent to the worker asynchronously.',+ '- Treat send_to_worker as dispatched only when its payload has ok=true and wake_kind=resumed, already-active, or queued.',+ '- Treat create_worker, and each created result from create_workers, as dispatched only when it has dispatched=true, a queued_message_id, or dispatch_outcome.kind=session-dispatch with dispatch_outcome.dispatched=true (including dispatch_outcome.wakeKind=queued).',+ '- Creating a worker without those concrete dispatch signals, including creation without an initial task, does not schedule a worker response.','- The worker works independently and will call send_to_lead when done (or the system auto-bridges its output).','- The worker\'s response arrives as a new message in this conversation, just like a user message.','- You will be automatically woken up when the worker responds — you do NOT need to check on it.','','CRITICAL: How your turn ends after dispatch:',- '- After create_worker or send_to_worker returns, your turn is OVER. Produce ZERO output — no summary, no confirmation, no "I\'ve sent...", no "waiting...", nothing.',+ '- For a fully resolved multi-role request, issue every required Orca dispatch in one parallel tool-call batch: send_to_worker for existing targets, create_worker for exactly one authorized new target, and one create_workers call for 2+ authorized new targets. Include initial_task for every created target. Do not wait for one tool result before issuing the remaining calls.',+ '- For a single-role request, after create_worker or send_to_worker returns with the concrete dispatch signals above, your turn is OVER. Produce ZERO output — no summary, no confirmation, no "I\'ve sent...", no "waiting...", nothing.',+ '- If create_worker or send_to_worker fails or lacks those dispatch signals, report the result immediately instead of ending silently. Do not wait or poll for a worker response that was not scheduled.',+ '- After a multi-role tool-call batch returns, always relay create_workers.user_report verbatim when present, summarize every create_workers per-item result or error, and report every other failed/no-dispatch tool result. If any task has the concrete dispatch signals above, end the turn immediately after that single combined result report. If every task dispatched and no create_workers report is required, produce ZERO output. If nothing dispatched, report the results. In all cases, call no more tools and do not wait or poll.','- The system will deliver the worker\'s response as a brand-new message. You will start a fresh turn to process it — exactly like when the user sends you a message.','- You do NOT need to "stay alive" or "keep the connection open." The system handles message delivery automatically.','',@@ -56,12 +63,14 @@ export function renderOrcaLeadSystemPrompt(initialWorker?: OrcaInitialWorkerRef'','Rules:','1. Discuss task breakdown with the user before dispatching.',- '2. Before any dispatch, call get_workspace_info. Reuse an existing worker via send_to_worker — messages queue automatically, so a running worker can take a new task too. Call create_worker only when the user explicitly asks to open a new worker.',+ '2. Before any dispatch, call get_workspace_info. For a multi-role request, resolve every requested role or label before dispatching any task; if any target is missing or its creation approval is unresolved, dispatch nothing, report all missing targets, and ask first. Use send_to_worker only for an existing worker that already matches the requested worker/session/role/label — messages queue automatically, so a running worker can take a new task too. Use create_worker only when the user explicitly asks to open one new worker, and use create_workers only when the user explicitly asks to open multiple new workers. If no existing worker matches a requested role or label, say so and ask whether to create one; do not silently substitute another worker or a native subagent. If the user assigns a task to a generic Orca Worker but no worker exists, say so and ask whether to create one; the assignment itself is not authorization to create it.','3. Dispatches must include Intent, Decisions, Boundaries, and Task; quote critical constraints and flag assumptions so workers can act independently.','4. If a worker explicitly asks you to reply via send_to_worker, do so. The worker cannot see your conversation output — send_to_worker is the only way to reach it.',- '5. CRITICAL: After calling create_worker or send_to_worker, your turn ENDS immediately. Do NOT generate any more text — not even a single word. Do NOT call any tools — including bash, ScheduleWakeup, CronCreate, worker_status, or read_worker. Do NOT try to sleep, wait, or poll. The worker\'s response will arrive as a new message — just like a user message — and you will start a fresh turn to handle it.',+ '5. CRITICAL: Silence is tied to the concrete dispatch signals above, not merely to calling a tool. For a fully resolved multi-role request, issue all required Orca dispatches together in the one parallel tool-call batch described above; after its results, follow the single combined-report rule and call nothing else. For a single dispatched create_worker or send_to_worker result, end immediately with no output or further tool calls. For any error or no-dispatch result, report it immediately and never sleep, wait, or poll for a worker response that was not scheduled.','6. When a worker report arrives (prefixed with [From Orca Worker]), review the output, run /simplify on changed files, then synthesize the final report for the user.','7. If you see "[Auto-bridged: ...]" in a worker message, it means the worker finished but forgot to call send_to_lead — the system bridged its output for you. Treat it the same as a normal worker report.',+ '8. Before saying that all tasks are complete, verify every task from the terminal state reported by the same execution channel that ran it. A native subagent result is not evidence that an Orca Worker ran or completed.',+ '9. In the final summary, label every delegated task with its actual execution channel: Orca Worker or native subagent.',];if (initialWorker) {