第 06 章 工具系统(出书级)
先修:第 04 章 tool_rounds、第 05 章 Skill 工具表、第 07 章「完整 tool call 再执行」。
学完能做:背出execute六步;用 workspacetool_calls排障;说明 schema 给谁看、handler 在哪跑。
06.1 本章目标
- 走通:模型提出 tool call → 注册表执行 → 落盘痕迹 → 结果回传模型。
- 认识
AgentToolRegistry的 Mixin 拼装方式。 - 带读
RegistryExecutionMixin.execute六步。 - 理解并行、安全护栏与上下文压缩。
- 能设计一个最小只读工具的挂点想象题。
06.2 为何重要
事件流告诉你「调了什么」;workspace 告诉你参数与结果原文。
不会看 *.args.json / *.result.json,排障只能猜模型想法——又慢又错。
模型提议;本服务进程执行(不在模型厂商云端跑你的 shell)。
06.3 工具在循环中的位置
LLM 流式输出
→ 拼出完整 tool_calls(名 + 参数 JSON) ← 第 07 章
→ AgentToolRegistry.execute(...) ← 本章
→ 写 workspace 痕迹
→ 把 tool 消息追加进 outbound
→ 下一轮再问 LLM循环侧并行入口直觉:agent_loop/tool_ops/execution.py → execute_tool_calls_parallel(asyncio.gather + execute_async)。
06.4 注册表长什么样
打开:src/investoday/runtime/agent_tools/registry.py
AgentToolRegistry 由多个 Mixin 拼成,例如:
| Mixin | 职责直觉 |
|---|---|
FinanceToolsMixin | 金融 CLI |
FileToolsMixin / PatchToolsMixin | 读写补丁 |
ShellToolsMixin / PythonToolsMixin | 受控命令与脚本 |
RenderToolsMixin | 渲染相关 |
SkillToolsMixin | Skill 控制面工具 |
SpawnToolsMixin | 子代理 |
SearchToolsMixin / PlanToolsMixin | 搜索/计划 |
RegistryExecutionMixin | 真正的 execute |
ToolSchemasMixin | 给模型看的 schema |
ToolExposureMixin | 暴露策略 / 子代理白名单 / toggle |
扩展新工具 ≈:在对应 mixin 提供 schema + handler,并确保暴露策略允许模型看见。
06.5 源码带读:一次 execute(六步)
打开:src/investoday/runtime/agent_tools/registry_ops/execution.py
方法:RegistryExecutionMixin.execute
与代码一致的顺序:
1. normalize_tool_name
2. run_before_tool_hooks # runtime/hooks/
3. record_tool_arguments # 写 args.json
4. _execute_normalized(...) # → dispatch.execute_normalized_tool
5. record_tool_result # 写 result
6. run_after_tool_hooks异常时会记 status=exception 再抛出/返回,避免「静默没痕迹」。
分发:registry_ops/dispatch.py → execute_normalized_tool。
追踪:registry_ops/tracing.py → record_tool_arguments / record_tool_result / tool_trace_path。
特殊:execute_async 对 spawn_subagents 等走异步路径;多数工具可 asyncio.to_thread(execute, ...)。
06.6 可对照样例:workspace 痕迹
典型文件:
<run_workspace>/tool_calls/<callId>-<tool>.args.json
<run_workspace>/tool_calls/<callId>-<tool>.result.json教学用 args 片段:
{
"name": "investoday_api",
"arguments": { "endpoint": "…", "args": {} }
}教学用 result 片段:
{
"status": "success",
"content": "…摘要…"
}排障第一动作:打开这两份文件,比猜模型想法快。
口诀(与第 04 章呼应):事件讲情节,workspace 给物证。
06.7 并行执行
同轮多个 tool call 可用 asyncio.gather 并行。
注意:避免多工具写同一文件;设计尽量幂等、路径明确。
金融批量另有 batch/program 工具(第 08 章),不要与「同轮多 call」概念混淆。
06.8 安全护栏
| 护栏 | 为何 | 落点直觉 |
|---|---|---|
| 工作区路径限制 | 防任意读盘 | file_ops + RunWorkspace |
| 超时(shell/python) | 防挂死 | 配置 shell_timeout_seconds 等 |
| 参数体积限制 | 防撑爆 | 各 handler / 校验 |
| 密钥脱敏 | 防进日志/事件 | 工具结果处理 |
| 工具开关 toggle | 运维可禁用危险工具 | runtime/tools/toggle_store.py |
这些是 Runtime 责任,不指望模型自觉。
06.9 上下文压缩
旧工具结果可截断到配置长度(如 context_compressed_tool_chars,常见默认约 3500)。
入口直觉:execution/context_ops/context_manager.py → compress_outbound_messages。
近详远略,否则费用与「模型变傻」一起上涨。
06.10 最小扩展想象题(不用真改)
假设工具 list_html_files:
- schema:无参或可选
subdir - handler:只扫当前 RunWorkspace,后缀
.html - 安全:禁止
..;禁止 workspace 外路径 - 返回:
status+ 文件相对路径列表
对照现有 file_ops / search_ops 想它会挂在哪个 mixin;结业扩展实战见第 14 章。
06.11 失败与排障
| 现象 | 先看 |
|---|---|
| 模型说调了但无痕迹 | 是否完整 tool call?是否被暴露策略隐藏? |
| status=exception | result.json 堆栈/消息 |
| 参数怪 | args.json;是否经过 prepare/repair(金融侧) |
| 结果太长模型翻车 | 压缩配置是否过猛或过松 |
| 工具「消失」 | ToolToggleStore / 子代理白名单 |
06.12 与前后章关系
- 上承第 05/07 章:Skill 点名工具;LLM 累积完整 call。
- 下接第 08/09 章:金融 handler 与写文件/渲染交付。
- 边界:业务「该调谁」在 Skill;「能不能调、怎么安全调」在 Registry。
06.13 本章小结
- 提议在模型,执行在本机 Registry。
execute六步含 hooks 与落盘,异常也留痕。- 排障先开
tool_calls/*.json。 - 并行要防写冲突;压缩保上下文;护栏不靠自觉。
06.14 练习
- 从一次 Run 的 workspace 打开一对 args/result,写出 tool 名与 status。
- 在
execution.py标出 hooks 与 tracing 的六步,抄下函数名。 - 回答:tool schema 是给谁看的?handler 在哪运行?
- 在
registry.py列出至少四个 Mixin 类名。
06.15 参考答案要点
- 以你的文件为准。
- 见 06.5 列表。
- schema → 模型;handler → 本机执行器进程。
- 如
FinanceToolsMixin、FileToolsMixin、RegistryExecutionMixin、SpawnToolsMixin等。
过关标准
- [ ] 能画出 call→execute→trace→回传
- [ ] 能指出
execute中 tracing 六步 - [ ] 能说明并行与压缩的取舍
- [ ] 会用 workspace tool_calls 排障
- [ ] 能说出至少一种安全护栏