import type { ToolCallContext, ToolResult } from '../../types.js'; import { belongsToDelivery } from '../../runtime/deliveryGate.js'; import { agentNeedsResolution, dependencyReceipt, readAgentLedgerStrict } from '../agent/agentTaskService.js'; /** Consultation is not delivery: independent criticism can run in parallel. * Default to the current delivery's dependencies; early independent criticism * is an explicit choice, never an accidental bypass of unfinished handoffs. * Return control to the parent instead of blocking a tool while tasks need help. */ export async function advisorCoordination( input: { purpose?: 'independent' | 'synthesis'; depends_on?: string[] }, ctx?: ToolCallContext, ): Promise { const synthesis = input.purpose === 'synthesis' || (input.purpose === undefined && Boolean(ctx?.deliveryId)); const ids = [...new Set(input.depends_on ?? [])]; if (!synthesis && !ids.length) { const running = [...(ctx?.sessionState?.agentTasks.tasks.values() ?? [])] .filter(task => task.status === 'running').length; return { ok: true, content: running ? '本次是独立并行复核,仍有后台子任务未返回;它们的结论未包含在本次材料中,不代表最终汇总验收。' : '本次是独立第二意见,不代表主 Agent 已完成子任务验收。' }; } if (!ctx) return { ok: false, disposition: 'deferred', error: '依赖子任务的咨询需要会话上下文;请提供宿主上下文,或明确改为 independent 独立复核。' }; const ledger = await readAgentLedgerStrict(ctx); if (ledger.kind === 'unknown') return { ok: false, disposition: 'deferred', error: `顾问汇总暂缓:${ledger.reason}。先恢复账本,不假定依赖已完成。` }; if (synthesis && !ids.length && !ctx.deliveryId) return { ok: false, disposition: 'deferred', error: '顾问汇总缺少当前交付标识;请用 depends_on 明确依赖任务,或 independent 做独立复核。' }; const selected = ids.length ? ids.map(id => ledger.records.find(task => task.id === id)) : ledger.records.filter(task => belongsToDelivery(task, ctx.deliveryId!) && task.required !== false); const pending = selected.flatMap((task, index) => { if (!task) return [`${ids[index]}:未找到`]; if (!ids.length && !agentNeedsResolution(task, ledger.records) && task.resolution?.action === 'waive') return []; if (task.status === 'running' || !dependencyReceipt(task, ctx, 1200)) return [`${task.id}:${task.status},需要读取报告、核实后 AgentResolve accept/complete`]; return []; }); if (pending.length) return { ok: false, disposition: 'deferred', error: `顾问汇总暂缓,尚未发出模型请求:${pending.join(';')}。先 AgentWait/TaskOutput 获取结果;受阻时补充材料或接管。独立复核不依赖这些结论时可用 purpose=independent 且不填 depends_on。` }; const receipts = selected.flatMap(task => { const receipt = task && dependencyReceipt(task, ctx, 1200); return receipt ? [receipt] : []; }); return { ok: true, content: `本次汇总的主 Agent 验收记录(不是子任务完整原文;关键证据仍须由主历史或 note 提供):\n${JSON.stringify(receipts)}` }; }