模块 supervisor — 对子代理的监督
🟢 该模块让父代理能够启动子代理,
订阅其子代理的选定事件,并在事件匹配时获得注入到自身上下文的内容——可选择暂停
子代理。子代理中无需钩子:这是事件总线
(events.subscribers)和公共端口 agent_os.Agents 之上的外部 control-plane。
工具 supervisor
一个带有 action 字段的工具 kind: supervisor:
action |
字段 | 作用 |
|---|---|---|
spawn |
template, task?, session_key? |
非阻塞启动子代理;返回 pid |
subscribe |
event?, inject, child?, role?, pause_child?, resume_after?, run_turn?, once? |
父代理订阅子代理事件;返回 id |
unsubscribe |
id |
取消订阅 |
subscriptions |
— | 活动订阅列表 |
send |
pid, content, role? |
向子代理注入消息 |
pause / resume |
pid |
暂停/恢复子代理 |
stop |
pid, signal? |
pause | interrupt | terminate(默认 interrupt) |
status |
pid |
子代理进程快照 |
session |
pid, limit? |
运行中子代理的实时会话:其上下文的最新消息(role, content, tool_call_id, tool_calls);limit 默认为 50 |
订阅与投递
- 选择器
event(所有字段均可选):{ kind?, tool?, success?, contains? }—kind来自ProcessEvent::event_type(tool_call,tool_result,error, …)。 inject— MiniJinja(上下文中为event),注入到父代理的上下文中。pause_child— 在注入前暂停子代理;resume_after— 之后立即 恢复;run_turn— 非阻塞地启动父代理的一轮 (否则父代理将在下一轮看到注入);once— 首次触发后 取消订阅。- 定向:
child: <pid>— 特定子代理;不带child— 父代理的任意子代理 (通过Agents.process(pid).parent解析)。 - 针对特定子代理的操作(
send/pause/resume/stop/status/session)仅允许对调用者的直接子代理执行。
随附代理 observer
该模块通过 config 捆绑包提供一个现成的观察者代理 observer
(agents/observer/):它声明 tool_files: [tools/supervisor.yaml] 并拥有
所有 supervise 操作 — spawn, subscribe, session, send,
pause/resume/stop。在模块启用时随 config/agents/ 一起加载(来自 config/agents 的同名代理
优先)。典型场景:
observer 启动 coder 并监视其崩溃。
示例
该工具也可以在自己的代理中声明:
# config/agents/supervisor-agent/tools/supervisor.yaml
kind: supervisor
name: supervise
description: Запускай код-агента и следи за его падениями.
{ "action": "spawn", "template": "coder", "task": "Почини сборку" }→pid。{ "action": "subscribe", "child": "<pid>", "event": { "kind": "tool_result", "success": false }, "inject": "崩溃了:{{ event.content }}。帮忙修复。", "pause_child": true, "resume_after": true, "run_turn": true }。- 父代理独立工作;当子代理崩溃时,获得注入到自身 上下文的内容(子代理可选暂停)。
{ "action": "session", "pid": "<pid>", "limit": 50 }— 查看 子代理当前正在做什么。
全局规则(替代方案)
对于不绑定到特定父代理的声明式规则,有
reactions 模块:操作 inject(包括 target: parent)和
signal 允许从规则中通知/停止进程。