shareAI-lab在github上有一系列的源码,通过研究Claude Code帮助学习智能体开发。仓库在这里。
本系列文章就是对这些源码的学习,本篇是第一篇。
v0: Bash 就是一切
终极简化:~50 行代码,1 个工具,完整的 Agent 能力。
Agent 的本质到底是什么?
v0 通过反向思考来回答——剥离一切,直到只剩下核心。
核心洞察
Unix 哲学:一切皆文件,一切皆可管道。Bash 是这个世界的入口:
| 你需要 | Bash 命令 |
|---|---|
| 读文件 | cat, head, grep |
| 写文件 | echo '...' > file |
| 搜索 | find, grep, rg |
| 执行 | python, npm, make |
| 子代理 | python v0_bash_agent.py "task" |
最后一行是关键洞察:通过 bash 调用自身就实现了子代理。不需要 Task 工具,不需要 Agent Registry——只需要递归。
源码
1 | from anthropic import Anthropic |
子代理工作原理
1 | 主代理 |
进程隔离 = 上下文隔离
- 子进程有自己的
history=[] - 父进程捕获 stdout 作为工具结果
- 递归调用实现无限嵌套
v0 牺牲了什么
| 特性 | v0 | v3 |
|---|---|---|
| 代理类型 | 无 | explore/code/plan |
| 工具过滤 | 无 | 白名单 |
| 进度显示 | 普通 stdout | 行内更新 |
| 代码复杂度 | ~50 行 | ~450 行 |
v0 证明了什么
复杂能力从简单规则中涌现:
- 一个工具足够 — Bash 是通往一切的入口
- 递归 = 层级 — 自我调用实现子代理
- 进程 = 隔离 — 操作系统提供上下文分离
- 提示词 = 约束 — 指令塑造行为
核心模式从未改变:
1 | while True: |
其他一切——待办、子代理、权限——都是围绕这个循环的精化。
带print中间结果的源码
1 | #!/usr/bin/env python |
观察运行结果
1 | >> who are you |
第二次:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132>> 查一下有多个py文件在该文件夹
[debug] history (after user append)
[{'role': 'user', 'content': '查一下有多个py文件在该文件夹'}]
[debug] ---
[debug] response.stop_reason
tool_use
[debug] ---
[debug] response.content
[TextBlock(citations=None, text='我来查看一下当前文件夹中有多少个Python文件。', type='text'), ToolUseBlock(id='toolu_0156QhdnZRPGSaYqvumccv1a', input={'command': 'find . -name "*.py" -type f'}, name='bash', type='tool_use')]
[debug] ---
[debug] history (after assistant append)
[{'role': 'user', 'content': '查一下有多个py文件在该文件夹'}, {'role': 'assistant', 'content': [{'type': 'text', 'text': '我来查看一下当前文件夹中有多少个Python文件。'}, {'type': 'tool_use', 'id': 'toolu_0156QhdnZRPGSaYqvumccv1a', 'name': 'bash', 'input': {'command': 'find . -name "*.py" -type f'}}]}]
[debug] ---
[debug] tool_use
{'id': 'toolu_0156QhdnZRPGSaYqvumccv1a', 'name': 'bash', 'input': {'command': 'find . -name "*.py" -type f'}}
[debug] ---
$ find . -name "*.py" -type f
[debug] tool_result
./v0_bash_agent_mini.py
./v1_basic_agent.py
./v4_skills_agent.py
./v3_subagent.py
./v2_todo_agent.py
./v0_bash_agent.py
./skills/agent-builder/references/subagent-pattern.py
./skills/agent-builder/references/tool-templates.py
./skills/agent-builder/references/minimal-agent.py
./skills/agent-builder/scripts/init_agent.py
[debug] ---
./v0_bash_agent_mini.py
./v1_basic_agent.py
./v4_skills_agent.py
./v3_subagent.py
./v2_todo_agent.py
./v0_bash_agent.py
./skills/agent-builder/references/subagent-pattern.py
./skills/agent-builder/references/tool-templates.py
./skills/agent-builder/references/minimal-agent.py
./skills/agent-builder/scripts/init_agent.py
[debug] results (after append)
[{'type': 'tool_result', 'tool_use_id': 'toolu_0156QhdnZRPGSaYqvumccv1a', 'content': './v0_bash_agent_mini.py\n./v1_basic_agent.py\n./v4_skills_agent.py\n./v3_subagent.py\n./v2_todo_agent.py\n./v0_bash_agent.py\n./skills/agent-builder/references/subagent-pattern.py\n./skills/agent-builder/references/tool-templates.py\n./skills/agent-builder/references/minimal-agent.py\n./skills/agent-builder/scripts/init_agent.py\n'}]
[debug] ---
[debug] history (after tool results append)
[{'role': 'user', 'content': '查一下有多个py文件在该文件夹'}, {'role': 'assistant', 'content': [{'type': 'text', 'text': '我来查看一下当前文件夹中有多少个Python文件。'}, {'type': 'tool_use', 'id': 'toolu_0156QhdnZRPGSaYqvumccv1a', 'name': 'bash', 'input': {'command': 'find . -name "*.py" -type f'}}]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_0156QhdnZRPGSaYqvumccv1a', 'content': './v0_bash_agent_mini.py\n./v1_basic_agent.py\n./v4_skills_agent.py\n./v3_subagent.py\n./v2_todo_agent.py\n./v0_bash_agent.py\n./skills/agent-builder/references/subagent-pattern.py\n./skills/agent-builder/references/tool-templates.py\n./skills/agent-builder/references/minimal-agent.py\n./skills/agent-builder/scripts/init_agent.py\n'}]}]
[debug] ---
[debug] response.stop_reason
tool_use
[debug] ---
[debug] response.content
[ToolUseBlock(id='toolu_01ChrTdDzP34cMu6a2Dn8hmK', input={'command': 'find . -name "*.py" -type f | wc -l'}, name='bash', type='tool_use')]
[debug] ---
[debug] history (after assistant append)
[{'role': 'user', 'content': '查一下有多个py文件在该文件夹'}, {'role': 'assistant', 'content': [{'type': 'text', 'text': '我来查看一下当前文件夹中有多少个Python文件。'}, {'type': 'tool_use', 'id': 'toolu_0156QhdnZRPGSaYqvumccv1a', 'name': 'bash', 'input': {'command': 'find . -name "*.py" -type f'}}]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_0156QhdnZRPGSaYqvumccv1a', 'content': './v0_bash_agent_mini.py\n./v1_basic_agent.py\n./v4_skills_agent.py\n./v3_subagent.py\n./v2_todo_agent.py\n./v0_bash_agent.py\n./skills/agent-builder/references/subagent-pattern.py\n./skills/agent-builder/references/tool-templates.py\n./skills/agent-builder/references/minimal-agent.py\n./skills/agent-builder/scripts/init_agent.py\n'}]}, {'role': 'assistant', 'content': [{'type': 'tool_use', 'id': 'toolu_01ChrTdDzP34cMu6a2Dn8hmK', 'name': 'bash', 'input': {'command': 'find . -name "*.py" -type f | wc -l'}}]}]
[debug] ---
[debug] tool_use
{'id': 'toolu_01ChrTdDzP34cMu6a2Dn8hmK', 'name': 'bash', 'input': {'command': 'find . -name "*.py" -type f | wc -l'}}
[debug] ---
$ find . -name "*.py" -type f | wc -l
[debug] tool_result
10
[debug] ---
10
[debug] results (after append)
[{'type': 'tool_result', 'tool_use_id': 'toolu_01ChrTdDzP34cMu6a2Dn8hmK', 'content': ' 10\n'}]
[debug] ---
[debug] history (after tool results append)
[{'role': 'user', 'content': '查一下有多个py文件在该文件夹'}, {'role': 'assistant', 'content': [{'type': 'text', 'text': '我来查看一下当前文件夹中有多少个Python文件。'}, {'type': 'tool_use', 'id': 'toolu_0156QhdnZRPGSaYqvumccv1a', 'name': 'bash', 'input': {'command': 'find . -name "*.py" -type f'}}]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_0156QhdnZRPGSaYqvumccv1a', 'content': './v0_bash_agent_mini.py\n./v1_basic_agent.py\n./v4_skills_agent.py\n./v3_subagent.py\n./v2_todo_agent.py\n./v0_bash_agent.py\n./skills/agent-builder/references/subagent-pattern.py\n./skills/agent-builder/references/tool-templates.py\n./skills/agent-builder/references/minimal-agent.py\n./skills/agent-builder/scripts/init_agent.py\n'}]}, {'role': 'assistant', 'content': [{'type': 'tool_use', 'id': 'toolu_01ChrTdDzP34cMu6a2Dn8hmK', 'name': 'bash', 'input': {'command': 'find . -name "*.py" -type f | wc -l'}}]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01ChrTdDzP34cMu6a2Dn8hmK', 'content': ' 10\n'}]}]
[debug] ---
[debug] response.stop_reason
end_turn
[debug] ---
[debug] response.content
[TextBlock(citations=None, text='在当前文件夹中总共有 **10个** Python文件:\n\n**根目录下的文件:**\n1. v0_bash_agent_mini.py\n2. v1_basic_agent.py\n3. v4_skills_agent.py\n4. v3_subagent.py\n5. v2_todo_agent.py\n6. v0_bash_agent.py\n\n**子目录下的文件:**\n7. skills/agent-builder/references/subagent-pattern.py\n8. skills/agent-builder/references/tool-templates.py\n9. skills/agent-builder/references/minimal-agent.py\n10. skills/agent-builder/scripts/init_agent.py', type='text')]
[debug] ---
[debug] history (after assistant append)
[{'role': 'user', 'content': '查一下有多个py文件在该文件夹'}, {'role': 'assistant', 'content': [{'type': 'text', 'text': '我来查看一下当前文件夹中有多少个Python文件。'}, {'type': 'tool_use', 'id': 'toolu_0156QhdnZRPGSaYqvumccv1a', 'name': 'bash', 'input': {'command': 'find . -name "*.py" -type f'}}]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_0156QhdnZRPGSaYqvumccv1a', 'content': './v0_bash_agent_mini.py\n./v1_basic_agent.py\n./v4_skills_agent.py\n./v3_subagent.py\n./v2_todo_agent.py\n./v0_bash_agent.py\n./skills/agent-builder/references/subagent-pattern.py\n./skills/agent-builder/references/tool-templates.py\n./skills/agent-builder/references/minimal-agent.py\n./skills/agent-builder/scripts/init_agent.py\n'}]}, {'role': 'assistant', 'content': [{'type': 'tool_use', 'id': 'toolu_01ChrTdDzP34cMu6a2Dn8hmK', 'name': 'bash', 'input': {'command': 'find . -name "*.py" -type f | wc -l'}}]}, {'role': 'user', 'content': [{'type': 'tool_result', 'tool_use_id': 'toolu_01ChrTdDzP34cMu6a2Dn8hmK', 'content': ' 10\n'}]}, {'role': 'assistant', 'content': [{'type': 'text', 'text': '在当前文件夹中总共有 **10个** Python文件:\n\n**根目录下的文件:**\n1. v0_bash_agent_mini.py\n2. v1_basic_agent.py\n3. v4_skills_agent.py\n4. v3_subagent.py\n5. v2_todo_agent.py\n6. v0_bash_agent.py\n\n**子目录下的文件:**\n7. skills/agent-builder/references/subagent-pattern.py\n8. skills/agent-builder/references/tool-templates.py\n9. skills/agent-builder/references/minimal-agent.py\n10. skills/agent-builder/scripts/init_agent.py'}]}]
[debug] ---
[debug] final_text
在当前文件夹中总共有 **10个** Python文件:
**根目录下的文件:**
1. v0_bash_agent_mini.py
2. v1_basic_agent.py
3. v4_skills_agent.py
4. v3_subagent.py
5. v2_todo_agent.py
6. v0_bash_agent.py
**子目录下的文件:**
7. skills/agent-builder/references/subagent-pattern.py
8. skills/agent-builder/references/tool-templates.py
9. skills/agent-builder/references/minimal-agent.py
10. skills/agent-builder/scripts/init_agent.py
[debug] ---
在当前文件夹中总共有 **10个** Python文件:
**根目录下的文件:**
1. v0_bash_agent_mini.py
2. v1_basic_agent.py
3. v4_skills_agent.py
4. v3_subagent.py
5. v2_todo_agent.py
6. v0_bash_agent.py
**子目录下的文件:**
7. skills/agent-builder/references/subagent-pattern.py
8. skills/agent-builder/references/tool-templates.py
9. skills/agent-builder/references/minimal-agent.py
10. skills/agent-builder/scripts/init_agent.py