AgentScope 学习 W3:Agent 模块深潜——_agent.py 全部 3289 行读完
AgentScope 学习 W3:Agent 模块深潜——_agent.py 全部 3289 行读完
接 W2 真学习复盘:W2 学了 1198 行 + 1 个上游 issue。本文是 W3 完整 5 天的复盘——agent 模块 100% 闭环。
一、W3 vs W2
W2 (7-20 ~ 7-26) : 1198 行 / 14 笔记 / 1 issue
W3 (7-27 ~ 7-31) : 1303 行 / 11 笔记
─────────────────────────
W2 + W3 累计 : 2501 行 / 25 笔记W3 收获:
_agent.py全部 3289 行读完——agent 模块 100% 闭环- W2 学的概念(reentrant / 状态机 / Q5 bug / 3 档反馈)继续验证
- 5 天稳定 8.0+ 评分——W3 全程无跌
二、W3 5 天数据
W3-D1 (7-27 周日) : 460 行 / 3 笔记
W3-D2 (7-28 周一) : 219 行 / 2 笔记
W3-D3 (7-29 周二) : 158 行 / 2 笔记
W3-D4 (7-30 周三) : 117 行 / 1 笔记
W3-D5 (7-31 周四) : 349 行 / 3 笔记
─────────────────────────
W3 累计 : 1303 行 / 11 笔记5 天趋势:
8.6 → 8.6 → 8.4 → 8.2 → 8.6全程 8.0+——W3 稳定输出。
三、9 段连读——_agent.py 全貌
W3 把 _agent.py 切成 9 段连续读完——每个方法都问 3 道题——4 答 4 档反馈方法论。
| 段 | 行数 | 关键收获 |
|---|---|---|
_inject_runtime_state | 240 | HintBlock vs system prompt(prompt cache 命中) |
_check_incoming_event | 90 | read-only 验证 + raise ValueError |
_handle_incoming_event | 102 | HITL 2.0 = modification allowed(不是 binary confirm) |
_handle_incoming_messages | 28 | Msg vs Event = content vs signal |
_batch_tool_calls | 38 | RLE 算法(batching 是数据压缩) |
_execute_sequential_tool_calls | 50 | 双重 break(Python 没多级 break) |
_execute_concurrent_tool_calls | 131 | data flow > control flow(W3 最大概念) |
_handle_error_tool_call | 75 | uniform type pattern(error 也是 result) |
_acting + _acting_impl | 83 | Duplication > Wrong Abstraction |
_into_queue | 22 | 超小函数 |
_split_context_for_compression | 95 | 多层粒度 + 事务原子 + convergent |
_split_tool_result_for_compression | 147 | per-type compressibility + token-aware |
| model 适配三件套 | 177 | transformer/chain + dict/typed + layered resilience |
_clear_unreserved_read_cache | 25 | 资源跟随 context 走 |
14 段——W3 + W2-D7 部分——agent 模块完整收官。
四、W3 最大的 6 个概念
1. reentrant protocol 落地 ⭐ W2 概念 → W3 实现
W2-D4 学的 reentrant protocol——HITL = 多步表单/可重入协议。W3 看到实现:
# _handle_incoming_event line 1580-1581
if confirmation.confirmed:
self._update_tool_call_state(tool_call.id, ToolCallState.ALLOWED)
tool_call.name = confirmation.tool_call.name # ← 用户可以换工具
tool_call.input = confirmation.tool_call.input # ← 用户可以改参数关键洞察:
- 传统 HITL = binary confirm("是/否")
- AgentScope HITL = modification allowed("可以改参数,甚至换工具")
- 这是 HITL 2.0——不是 ask permission,是 collaborate
2. 状态机视角 ⭐ 5 态转换
PENDING → ASKING → ALLOWED → (SUBMITTED) → FINISHED
↓ DENY
ERROR3 类正交输入(6 种 input 的根因):
- 同步(
Msg/list[Msg]) - 异步(3 种 Event)
- 轮询(
None)
3 种结束状态 = 3 种责任归属:
COMPLETED= 自然完成EXCEED_MAX_ITERS= 自然限制(可恢复)INTERRUPTED= 强制中断(必须善后)
3. data flow > control flow ⭐ W3 最大概念
# _execute_concurrent_tool_calls line 1865
except asyncio.CancelledError:
gather_task.cancel()
# ... drain events ...
asyncio.current_task().uncancel()
return关键洞察:
asyncio.CancelledError是"控制流"——调用方必须 try/except- 但 AgentScope 用 events 替代——callers 不用 try/except——只管 iterate
- 同一个 generator 既传正常 events 又传 INTERRUPTED events——统一处理
架构师视角:
"Modern async framework 都用 data flow 替代 control flow"
- asyncio CancelledError 是过渡方案
- AgentScope 进一步用 events 替代——callers 更简单
- 这就是"uniform event-based async pattern"
4. RLE 算法应用 ⭐ 隐藏发现
# _batch_tool_calls
# 保持原顺序,相邻同类型合并
# [C, C, S, C, C, S, S, C] → [C(2), S(1), C(2), S(2), C(1)]架构师视角:
"Batching 是 RLE,不是 sort"
- sort 会改变 LLM 决策
- RLE 只压缩类型
- "honor the LLM's order, but reduce dispatch overhead"
5. Q5 真 bug 闭环 ⭐⭐ W2-W3 跨周成就
W2-D4 怀疑 + W2-D5 验证 + 提交到 agentscope-ai/agentscope #2166:
根因:
_close_unfinished_tool_calls检查state != ALLOWED- external tool 活跃期是
ALLOWED + SUBMITTED——会重复 emit START event
修复:
# Before
if call_block.state != ToolCallState.ALLOWED:
# After
if call_block.state not in (ToolCallState.ALLOWED, ToolCallState.SUBMITTED):W2-D6 收尾:
- commit message 引用 #2166 → 触发 GitHub auto-link → 3 个 commit 在 issue timeline
- rebase 改了 3 个 commit message——去掉了引用——未来 commit 不会被关联
- 这是工程严谨性——issue 内容是公开的,commit message 是私人的,分开
6. 资源跟随 context 走 ⭐ W3 收官完美一笔
# _clear_unreserved_read_cache
# 压缩时清掉不在 reserved 部分的 Read 工具的 file cache架构师视角:
"资源跟着 context 走"——context 是 cache 的"所有者"
- Read 工具 → 产生 cache
- Context 压缩 → 决定 cache 生死
- AgentScope 选"context 是 cache 命运"——而不是"工具是 cache 主人"
五、3 档反馈方法论(W3 严格执行)
W2-D4 立的规矩——W3 全程用——不让"放水表扬"破坏学习:
✅ 已掌握 —— 能解释 + 能应用
⚠️ 浅 —— 知道是什么,不知道为什么
❌ 缺 —— 不知道是什么W3 5 天统计:
- ✅✅ 强:18 道(45%)
- ⚠️ 浅:16 道(40%)
- ✅ 通过:6 道(15%)
比 W2 比例改善:
- W2 大约 50% ✅✅ 强 + 50% ⚠️ 浅
- W3 45% ✅✅ 强 + 40% ⚠️ 浅 + 15% ✅
- "通过" 比例提升——说明有些题真的简单到秒答
3 档评分让 ISTJ 不再自欺——这是 W2 最有用的方法论调整。
六、W3 期间发现的 3 个潜在 issue
| 发现 | 位置 | 状态 |
|---|---|---|
| Q5 = duplicate ToolResultStartEvent | _agent.py:705 | ✅ 已提 issue #2166 |
is_read_only docstring 提了但代码没用 | _agent.py:1681 | 📝 待 verify |
is_state_injected + offload race condition | _acting_impl:2278 | 📝 TODO 注释自己标记 |
W4 末"潜在 issue 清单"——这 3 个进 list——继续工程师贡献。
七、对"学习"这件事的 4 个新认识(W3)
认识 1:学 = 看代码 + 问问题 + 答问题 + 自己发现不懂的东西
W3 5 天 W2 学的概念继续验证——reentrant / 状态机 / transformer / chain——不是只 W2 用——W3 重复用到 = 真掌握。
认识 2:3 档反馈方法论严格执行
W2-D4 立的"✅⚠️❌"——W3 5 天严格用——44 道题——不让"放水"破坏学习。
认识 3:节奏控制 = 5 天稳定 8.0+
8.6 → 8.6 → 8.4 → 8.2 → 8.65 天不跌——因为 2-3 段/天——W2-D4 那种 7h 透支的玩法 W3 没有。
认识 4:资源管理 = context 是 cache 命运
W3 收官的 _clear_unreserved_read_cache——"context 决定 cache 生死"——不是"工具决定 cache"——架构师哲学。
八、W3 整体数据
| 指标 | W3 实际 | vs W2 |
|---|---|---|
| 行数 | 1303 | +1.1% |
| 笔记 | 11 | -2% |
| 上游 issue | 1 (Q5 #2166) | — |
| 平均评分 | 8.48 | +0.4 vs W2 |
| 5 天稳定 8.0+ | ✅ | ✅ |
九、W4 预告(2026-08-03 开始)
W3 完成 agent 模块——W4 启动新模块——消息与通信机制:
- Msg 类型完整
- Block 类型完整
- JSON / MsgPack 序列化
- 流式 vs 完整响应协议
- 自定义 Msg + 自定义 Block Demo
8 周 Sep 12 截止不变——W4-W8 横向扩展 5 大模块——W4 启动"宽度扩张"。
资源链接
- 📂 学习仓库:github.com/sunrong1/agentscope · 分支
learning-journal- 📋 公开承诺:LEARNING.md
- 📝 真学习笔记:
notes/personal/- 🐛 上游 issue:#2166 Q5 bug
- 📖 W2 复盘:W2 真学习复盘
—— Mr.Sun, 2026-07-31 · Agent 模块 100% 收官
📚 AgentScope 8 周学习系列
- 🎯 W1:架构全景 + 核心类图 — 单一 Agent 类 + 6 原语 + 双层洋葱
- 🔧 配套工具:SM-2 复习系统 — 420 行 Python 的自适应复习
- 📝 W2:真学习复盘 — 6 天 + 10 洞察 + Q5 闭环
- 📝 本篇:W3 Agent 模块深潜(你正在读)
- ⏭️ W4:消息与通信机制(即将开始)
完整承诺:LEARNING.md · 持续更新到 9-12