你给的这个:
实际上是整个 learn-claude-code 教学仓库里的:
“终局 Capstone”
也就是:
把前面 s01 ~ s12 的所有 Agent Harness 机制,真正融合成一个完整 Claude Code 风格 runtime。
这个文件本质上已经不是:
一个agent demo而是:
一个微型 Claude Code Runtime
它体现的不是:
“prompt engineering”
而是:
Harness Engineering(Agent运行时工程)
这一点是整个项目最重要的思想。 (GitHub)
一、先建立整体认知(非常重要)
很多人分析 Agent 代码会陷入:
这里调了什么API
这里写了什么prompt但这份代码真正重要的是:
Runtime Architecture
也就是:
Agent如何长期运行
Agent如何管理上下文
Agent如何调用工具
Agent如何组织团队
Agent如何并行执行
Agent如何治理权限
Agent如何隔离环境Claude Code 的核心从来不是:
“调用一下Claude API”而是:
“一个长期运行的Agent操作系统”
二、s20_comprehensive 的总体架构
先给你最核心的宏观图。
1. 总体 Runtime 架构
┌─────────────────────┐
│ User Input │
└─────────┬───────────┘
│
▼
┌────────────────────────┐
│ Main Agent Loop │
│ perception-reason-act │
└─────────┬──────────────┘
│
┌─────────────────┼─────────────────┐
│ │ │
▼ ▼ ▼
┌────────────┐ ┌──────────────┐ ┌──────────────┐
│ Tool Calls │ │ Task System │ │ Context Mgmt │
└─────┬──────┘ └──────┬───────┘ └──────┬───────┘
│ │ │
▼ ▼ ▼
┌─────────────┐ ┌──────────────┐ ┌──────────────┐
│ Bash/Files │ │ Dependency │ │ Compression │
│ Edit/Grep │ │ Graph │ │ Summarize │
└─────────────┘ └──────────────┘ └──────────────┘
│
▼
┌────────────────────┐
│ Multi-Agent Teams │
└─────────┬──────────┘
│
┌──────────────────┼──────────────────┐
▼ ▼ ▼
┌────────────┐ ┌────────────┐ ┌────────────┐
│ Worker A │ │ Worker B │ │ Worker C │
└─────┬──────┘ └─────┬──────┘ └─────┬──────┘
│ │ │
▼ ▼ ▼
┌────────────┐ ┌────────────┐ ┌────────────┐
│ Worktree A │ │ Worktree B │ │ Worktree C │
└────────────┘ └────────────┘ └────────────┘三、核心设计思想(最关键)
s20 的真正核心思想:
“Agent ≠ Prompt”
而是:
“Agent = Runtime”
它把 Agent 看成:
LLM
+ Tool Runtime
+ Context Runtime
+ Task Runtime
+ Memory Runtime
+ Team Runtime
+ Sandbox Runtime这和:
Anthropic 那篇:
的思想几乎完全一致。
四、它为什么是 Claude Code 风格
Claude Code 最大特点:
“模型主导”
而不是:
“代码主导”
即:
代码不决定流程
模型决定流程代码只提供:
tool
context
workspace
permission
memory
这就是:
Harness Philosophy
README 已经明确强调:
The model decides when to call tools and when to stop.
The code just executes what the model asks for.(GitHub)
五、s20 的核心模块拆解
现在开始深入。
1. Main Agent Loop(最核心)
这是整个系统的大脑。
本质:
while True:
ask_llm()
execute_tools()
append_results()Claude Code/OpenAI Codex/OpenHands/Devin:
底层本质都一样。
它体现的思想
这里最重要的是:
“LLM负责决策”
Runtime 只负责:
执行动作
反馈环境即:
Perception
→ Reasoning
→ Action
→ Environment Feedback
→ Next Step这是经典:
ReAct Loop
六、消息系统(messages[])
这是第二个核心。
为什么 message 很重要
很多人以为:
messages = []只是聊天记录。
实际上:
messages[] = Agent Working Memory
Claude Code 的真正秘密之一:
整个 agent 的“思维状态”都在 messages[] 中。
包括:
当前任务
tool结果
历史决策
失败原因
teammate通信
task状态
summary
compression结果
七、Tool System(工具系统)
这是 Claude Code 的真正战斗力来源。
1. Tool Registry
大概率会有类似:
TOOLS = {
"bash": bash_tool,
"read": read_tool,
"write": write_tool,
}核心思想:
“工具只是函数”
非常 Unix Philosophy。
2. Tool Dispatch
一般:
tool_name = tool_use.name
handler = TOOLS[tool_name]
handler(args)这里体现:
Tool Runtime 是插件系统
Claude Code/OpenHands/Cursor:
本质都一样。
八、为什么 Bash 是核心
项目一直强调:
Bash is all you need这是非常深的思想。
因为:
Bash = Universal Action Space
一旦 agent 能:
grep
find
git
curl
python
npm
cargo
它已经拥有:
通用软件工程能力
所以 Claude Code 不需要:
1000个专用API只需要:
Terminal九、Context Compression(极其关键)
这是生产级 Agent 最重要部分之一。
为什么必须压缩
因为:
context window 永远有限长任务会:
爆 token
丢历史
推理退化
所以:
Agent 必须“遗忘”
s20 的设计思想
一般是:
原始消息
↓
summary
↓
压缩摘要
↓
保留关键状态类似:
L1: recent raw messages
L2: summarized history
L3: distilled memoryREADME 里也提到:
three-layer compression strategy(GitHub)
十、Subagents(非常重要)
Claude Code 最大创新之一:
“上下文隔离”
为什么需要 Subagent
如果一个 agent:
改代码
查bug
写测试
查文档
全在一个 context:
会迅速污染。
因此:
每个子任务独立 context
即:
main agent
├── subagent A
├── subagent B
└── subagent C十一、Task Graph(任务图)
这是 s20 非常高级的部分。
它不是 Todo List
而是:
DAG Runtime
例如:
Task A
↓
Task B
↓
Task C或者:
┌── Task B
Task A ─┤
└── Task C为什么重要
因为:
Agent开始具备“工程组织能力”
而不是:
单线程聊天机器人十二、Task Persistence
Claude Code 非常强调:
“任务状态落盘”
因为:
Agent 可能:
崩溃
超时
中断
重启
所以任务不能只在内存。
因此:
tasks/
task_001.json
task_002.json类似:
mini distributed scheduler
十三、Multi-Agent Team(团队系统)
这是最惊艳部分。
1. Team Runtime
结构类似:
Lead Agent
↓
spawn workers
↓
workers execute
↓
mailbox communication2. Mailbox(邮箱)
核心思想:
Agent之间异步通信
类似:
Agent A → mailbox → Agent B而不是:
同步函数调用为什么非常重要
因为:
Agent团队天然是分布式系统
这其实已经很像:
actor model
Erlang
distributed scheduler
了。
十四、Autonomous Task Claiming
README 提到:
scan the board and claim tasks themselves(GitHub)
这非常高级。
即:
worker自主抢任务
不是:
leader派发而是:
worker发现适合自己的任务
→ claim
→ execute已经很接近:
swarm intelligence
十五、Worktree Isolation(生产级核心)
这是 Claude Code 非常关键但很多人忽略的设计。
为什么需要 Worktree
多个 agent:
如果都操作:
same working directory会:
git冲突
文件覆盖
race condition
因此:
每个任务独立 git worktree
repo/
worktree_task_1/
worktree_task_2/本质
这是:
Sandbox Runtime
Agent 开始拥有:
隔离执行环境非常关键。
十六、权限系统(Permission Governance)
Claude Code 最大工程价值之一。
Agent 不能:
rm -rf /所以:
Harness 必须做权限治理
一般包括:
allowlist
confirm mode
sandbox
restricted shell
tool policy
十七、整个系统的真正本质
s20 最本质的东西:
“把 LLM 放进操作系统”
即:
LLM 不再只是聊天而是:
在一个长期运行环境中持续行动十八、它其实已经非常接近:
微型 AI Operating System
你会发现:
| 模块 | 类比OS |
|---|---|
| messages | memory |
| tasks | process |
| mailbox | IPC |
| worktree | process isolation |
| tools | syscalls |
| compression | memory management |
| agent loop | scheduler |
| subagents | subprocess |
| permissions | security model |
这其实已经:
不再是Prompt Engineering
而是:
AI Runtime Engineering
十九、这份代码真正高级的地方
不是:
用了什么prompt而是:
它把Agent当“持续运行的软件系统”
而不是:
一次性chat completion二十、它和传统 LangChain 的差异
LangChain 时代:
Prompt → Tool → Prompt而 s20:
Persistent Runtime核心差异:
| LangChain | s20 |
|---|---|
| chain | runtime |
| stateless | stateful |
| single call | long running |
| tool wrapper | operating environment |
| prompt centric | environment centric |
二十一、这份代码最大的启发(非常重要)
它真正说明:
Agent工程的核心不是“智能”
而是:
“环境设计”
即:
模型负责智能
Runtime负责世界这正是:
Anthropic / Claude Code / OpenHands / Devin
现在共同的路线。
二十二、如果你自己实现生产级 Agent
我会建议:
Runtime 分层
Application Layer
↓
Agent Layer
↓
Task Runtime
↓
Tool Runtime
↓
Context Runtime
↓
Sandbox Runtime
↓
OS / Container / Kubernetes二十三、你真正应该学习的(重点)
很多人看这种代码:
会关注:
怎么调用LLM API但真正重要的是:
Runtime Design
包括:
context lifecycle
task orchestration
async execution
environment isolation
memory compaction
permission governance
multi-agent coordination
这些才是: