/** Per-invocation execution policy for embedded agents, independent of host UI. */ export interface InvocationPolicy { delivery: 'model' | 'caller'; consultation: 'inherit' | 'disabled'; interactiveAsk: boolean; /** Embedded tasks must settle delegated work before returning to their caller. */ backgroundTasks?: 'allow' | 'wait'; } /** * True when the caller promised to settle delegated work before returning, so a * background request must run in the foreground instead of detaching. * * Detaching under this policy loses the work outright: these hosts have no * idle-wake listener, and the process exit hook in backgroundTaskService kills * every live task when the turn ends. */ export function mustSettleBackgroundTasks(policy?: InvocationPolicy): boolean { return policy?.backgroundTasks === 'wait'; } export function invocationToolAllowed( name: string, policy?: InvocationPolicy, ): boolean { if (!policy) return true; if (policy.delivery === 'caller' && name === 'PresentResult') return false; if (!policy.interactiveAsk && name === 'AskUserQuestion') return false; return ( policy.consultation !== 'disabled' || name !== 'Advisor' ); } export function invocationInstructions(policy?: InvocationPolicy): string { if (!policy) return ''; return [ '当前执行的是调用方委托的阶段任务。只完成本次阶段目标;遇到可恢复的工具错误,读取错误和现场,选择可用方法继续,完成后返回阶段结果。不能因已有过程叙述而声称完成。', '保留关键结论的来源身份、用户是否确认、适用版本和已有证据引用;缺少依据时说明未知,不把上游建议升级为事实。返回结果应包含调用方下一步需要的状态和引用,而非复制整段历史。', '任务明确指定了现有 Skill、工具或脚本时,优先实际调用并遵循其指令,不以自行编写替代品代替使用。尚未加载的 Skill 用 Skill 工具加载;已提供完整指令时无需重复加载。能力缺失或执行失败时说明具体原因,不假称已使用。', policy.delivery === 'caller' ? '最终交付由调用方负责。将结果、产物路径和未解决问题返回调用方,无需 PresentResult,也不要另起整个任务。' : '', policy.consultation === 'disabled' ? '本次不启用顾问咨询。' : '', policy.backgroundTasks === 'wait' ? '本阶段的委派任务必须完成后才返回;即使请求后台 Agent,宿主也会等待其完成,受本阶段时限与取消信号约束。' : '', !policy.interactiveAsk ? '本阶段不直接询问用户;缺少必须的人类决定时向调用方返回具体阻塞原因。' : '', ] .filter(Boolean) .join('\n'); }