Learn · 12 课源码精读

Learn Claude Code

不是教程,是拆机。从 agent loop 一行循环开始,层层剥到 worktree 隔离。12 课 × 3 个可交互组件,每节都能独立读。

12 课 36 个交互组件 基于 shareAI-lab/learn-claude-code
S01
Agent Loop
没有循环,就没有 agent
Agent loopstop_reasonmessages 历史tool_result 回传
→
S02
Tool Use
循环没变,只是工具变多了
Tool dispatch mapJSON schemasafe_path 防逃逸tool 扩展
→
S03
Todo Write
让 agent 管自己的进度
TodoManagerin_progress 单例限制nag reminder 注入结构化状态
→
S04
Subagent
把大问题切给一个新开的 agent
Subagent 派发上下文隔离CHILD_TOOLS 裁剪摘要式返回
→
S05
Skill Loading
按需加载的领域知识
两层 skill 架构YAML frontmatterload_skill tooltoken 经济
→
S06
Context Compact
上下文满了,学会砍
Micro compactAuto compact手动 compact tooltranscript 持久化
→
S07
Task System
状态写到磁盘,活过压缩
Task CRUDblockedBy 依赖图JSON 文件持久化拓扑求可执行
→
S08
Background Tasks
让活自己跑,agent 不阻塞
线程化 bg task通知队列LLM 调用前 drain回灌结果
→
S09
Agent Teams
多个 agent 通过文件邮箱通信
持久 teammateJSONL 邮箱5 种消息类型团队 config.json
→
S10
Team Protocols
两个 agent 之间的握手协议
shutdown 协议plan_approval 协议request_id 关联FSM: pending→approved/rejected
→
S11
Autonomous Agents
自己找活干的 agent
WORK / IDLE 两阶段任务板轮询auto_claimidentity 重注入
→
S12
Worktree Isolation
不同 agent 不要抢同一棵树
git worktree add任务 / worktree 绑定keep vs remove目录级隔离
→