Skip to content

Commit aea8844

Browse files
committed
add worktree & up task、teammate etc
1 parent c6a27ef commit aea8844

54 files changed

Lines changed: 2404 additions & 210 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README-ja.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Learn Claude Code -- AI Agent をゼロから構築する
1+
# Learn Claude Code -- 0 から 1 へ構築する nano Claude Code-like agent
22

33
[English](./README.md) | [中文](./README-zh.md) | [日本語](./README-ja.md)
44

@@ -17,16 +17,16 @@
1717
loop back -----------------> messages[]
1818
1919
20-
これだけだ。すべての AI コーディングエージェントはこのループ
21-
他はすべて改良に過ぎない
20+
これは最小ループだ。すべての AI コーディングエージェントに必要な土台になる
21+
本番のエージェントには、ポリシー・権限・ライフサイクル層が追加される
2222
```
2323

24-
**11 の段階的セッション、シンプルなループから完全な自律チームまで**
24+
**12 の段階的セッション、シンプルなループから分離された自律実行まで**
2525
**各セッションは1つのメカニズムを追加する。各メカニズムには1つのモットーがある。**
2626

2727
> **s01**   *"Bash があれば十分"* — 1つのツール + 1つのループ = エージェント
2828
>
29-
> **s02**   *"ループは変わらない"* — ツール追加はハンドラー追加であり、ロジック追加ではない
29+
> **s02**   *"ループは変わらない"* — ツール追加はハンドラー追加であり、ループの作り直しではない
3030
>
3131
> **s03**   *"行動する前に計画せよ"* — 可視化された計画がタスク完了率を向上させる
3232
>
@@ -45,6 +45,8 @@
4545
> **s10**   *"同じ request_id、2つのプロトコル"* — 1つの FSM パターンでシャットダウン + プラン承認
4646
>
4747
> **s11**   *"ポーリング、クレーム、作業、繰り返し"* — コーディネーター不要、エージェントが自己組織化
48+
>
49+
> **s12**   *"ディレクトリで分離し、タスクIDで調整する"* — タスクボード + 必要時の worktree レーン
4850
4951
---
5052

@@ -77,6 +79,19 @@ def agent_loop(messages):
7779

7880
各セッションはこのループの上に1つのメカニズムを重ねる -- ループ自体は変わらない。
7981

82+
## スコープ (重要)
83+
84+
このリポジトリは、nano Claude Code-like agent を 0->1 で構築・学習するための教材プロジェクトです。
85+
学習を優先するため、以下の本番メカニズムは意図的に簡略化または省略しています。
86+
87+
- 完全なイベント / Hook バス (例: PreToolUse, SessionStart/End, ConfigChange)。
88+
s12 では教材用に最小の追記型ライフサイクルイベントのみ実装している。
89+
- ルールベースの権限ガバナンスと信頼フロー
90+
- セッションライフサイクル制御 (resume/fork) と高度な worktree ライフサイクル制御
91+
- MCP ランタイムの詳細 (transport/OAuth/リソース購読/ポーリング)
92+
93+
このリポジトリの JSONL メールボックス方式は教材用の実装であり、特定の本番内部実装を主張するものではありません。
94+
8095
## クイックスタート
8196

8297
```sh
@@ -87,6 +102,7 @@ cp .env.example .env # .env を編集して ANTHROPIC_API_KEY を入力
87102

88103
python agents/s01_agent_loop.py # ここから開始
89104
python agents/s11_autonomous_agents.py # 完全自律チーム
105+
python agents/s12_worktree_task_isolation.py # Task 対応の worktree 分離
90106
```
91107

92108
### Web プラットフォーム
@@ -124,6 +140,9 @@ s08 バックグラウンドタスク [6] s10 チームプロトコル
124140
|
125141
s11 自律エージェント [14]
126142
アイドルサイクル + 自動クレーム
143+
|
144+
s12 Worktree 分離 [16]
145+
タスク調整 + 必要時の分離実行レーン
127146
128147
[N] = ツール数
129148
```
@@ -133,7 +152,7 @@ s08 バックグラウンドタスク [6] s10 チームプロトコル
133152
```
134153
learn-claude-code/
135154
|
136-
|-- agents/ # Python リファレンス実装 (s01-s11 + 完全版)
155+
|-- agents/ # Python リファレンス実装 (s01-s12 + 完全版)
137156
|-- docs/{en,zh,ja}/ # メンタルモデル優先のドキュメント (3言語)
138157
|-- web/ # インタラクティブ学習プラットフォーム (Next.js)
139158
|-- skills/ # s05 の Skill ファイル
@@ -158,11 +177,12 @@ learn-claude-code/
158177
| [s09](./docs/ja/s09-agent-teams.md) | エージェントチーム | *追記で送信、排出で読取* |
159178
| [s10](./docs/ja/s10-team-protocols.md) | チームプロトコル | *同じ request_id、2つのプロトコル* |
160179
| [s11](./docs/ja/s11-autonomous-agents.md) | 自律エージェント | *ポーリング、クレーム、作業、繰り返し* |
180+
| [s12](./docs/ja/s12-worktree-task-isolation.md) | Worktree + タスク分離 | *ディレクトリで分離し、タスクIDで調整する* |
161181

162182
## ライセンス
163183

164184
MIT
165185

166186
---
167187

168-
**モデルがエージェントだ。私たちの仕事はツールを渡して、邪魔をしないこと**
188+
**モデルがエージェントだ。私たちの仕事はツールを与えて邪魔しないこと**

README-zh.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Learn Claude Code -- 从零构建 AI Agent
1+
# Learn Claude Code -- 从 0 到 1 构建 nano Claude Code-like agent
22

33
[English](./README.md) | [中文](./README-zh.md) | [日本語](./README-ja.md)
44

@@ -17,16 +17,16 @@
1717
loop back -----------------> messages[]
1818
1919
20-
就这些。每个 AI 编程 Agent 都是这个循环
21-
其他一切都是优化
20+
这是最小循环。每个 AI 编程 Agent 都需要这个循环
21+
生产级 Agent 还会叠加策略、权限与生命周期层
2222
```
2323

24-
**11 个递进式课程, 从简单循环到完整的自治团队**
24+
**12 个递进式课程, 从简单循环到隔离化的自治执行**
2525
**每个课程添加一个机制。每个机制有一句格言。**
2626

2727
> **s01**   *"Bash 就够了"* — 一个工具 + 一个循环 = 一个智能体
2828
>
29-
> **s02**   *"循环没有变"* — 加工具就是加 handler, 不是加逻辑
29+
> **s02**   *"循环没有变"* — 加工具就是加 handler, 不是重写循环
3030
>
3131
> **s03**   *"先计划再行动"* — 可见的计划提升任务完成率
3232
>
@@ -45,6 +45,8 @@
4545
> **s10**   *"同一个 request_id, 两个协议"* — 一个 FSM 模式驱动关机 + 计划审批
4646
>
4747
> **s11**   *"轮询, 认领, 工作, 重复"* — 无需协调者, 智能体自组织
48+
>
49+
> **s12**   *"目录隔离, 任务 ID 协调"* — 任务板协调 + 按需 worktree 隔离通道
4850
4951
---
5052

@@ -77,6 +79,19 @@ def agent_loop(messages):
7779

7880
每个课程在这个循环之上叠加一个机制 -- 循环本身始终不变。
7981

82+
## 范围说明 (重要)
83+
84+
本仓库是一个 0->1 的学习型项目,用于从零构建 nano Claude Code-like agent。
85+
为保证学习路径清晰,仓库有意简化或省略了部分生产机制:
86+
87+
- 完整事件 / Hook 总线 (例如 PreToolUse、SessionStart/End、ConfigChange)。
88+
s12 仅提供教学用途的最小 append-only 生命周期事件流。
89+
- 基于规则的权限治理与信任流程
90+
- 会话生命周期控制 (resume/fork) 与更完整的 worktree 生命周期控制
91+
- 完整 MCP 运行时细节 (transport/OAuth/资源订阅/轮询)
92+
93+
仓库中的团队 JSONL 邮箱协议是教学实现,不是对任何特定生产内部实现的声明。
94+
8095
## 快速开始
8196

8297
```sh
@@ -87,6 +102,7 @@ cp .env.example .env # 编辑 .env 填入你的 ANTHROPIC_API_KEY
87102

88103
python agents/s01_agent_loop.py # 从这里开始
89104
python agents/s11_autonomous_agents.py # 完整自治团队
105+
python agents/s12_worktree_task_isolation.py # Task 感知的 worktree 隔离
90106
```
91107

92108
### Web 平台
@@ -124,6 +140,9 @@ s08 后台任务 [6] s10 团队协议 [12]
124140
|
125141
s11 自治智能体 [14]
126142
空闲轮询 + 自动认领
143+
|
144+
s12 Worktree 隔离 [16]
145+
任务协调 + 按需隔离执行通道
127146
128147
[N] = 工具数量
129148
```
@@ -133,7 +152,7 @@ s08 后台任务 [6] s10 团队协议 [12]
133152
```
134153
learn-claude-code/
135154
|
136-
|-- agents/ # Python 参考实现 (s01-s11 + 完整版)
155+
|-- agents/ # Python 参考实现 (s01-s12 + 完整版)
137156
|-- docs/{en,zh,ja}/ # 心智模型优先的文档 (3 种语言)
138157
|-- web/ # 交互式学习平台 (Next.js)
139158
|-- skills/ # s05 的 Skill 文件
@@ -158,11 +177,12 @@ learn-claude-code/
158177
| [s09](./docs/zh/s09-agent-teams.md) | 智能体团队 | *追加即发送, 排空即读取* |
159178
| [s10](./docs/zh/s10-team-protocols.md) | 团队协议 | *同一个 request_id, 两个协议* |
160179
| [s11](./docs/zh/s11-autonomous-agents.md) | 自治智能体 | *轮询, 认领, 工作, 重复* |
180+
| [s12](./docs/zh/s12-worktree-task-isolation.md) | Worktree + 任务隔离 | *目录隔离, 任务 ID 协调* |
161181

162182
## 许可证
163183

164184
MIT
165185

166186
---
167187

168-
**模型就是智能体。我们的工作是给它工具, 然后让开。**
188+
**模型就是智能体。我们的工作就是给它工具, 然后让开。**

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Learn Claude Code -- Build an AI Agent From Scratch
1+
# Learn Claude Code -- A nano Claude Code-like agent, built from 0 to 1
22

33
[English](./README.md) | [中文](./README-zh.md) | [日本語](./README-ja.md)
44

@@ -17,16 +17,16 @@
1717
loop back -----------------> messages[]
1818
1919
20-
That's it. Every AI coding agent is this loop.
21-
Everything else is refinement.
20+
That's the minimal loop. Every AI coding agent needs this loop.
21+
Production agents add policy, permissions, and lifecycle layers.
2222
```
2323

24-
**11 progressive sessions, from a simple loop to full autonomous teams.**
24+
**12 progressive sessions, from a simple loop to isolated autonomous execution.**
2525
**Each session adds one mechanism. Each mechanism has one motto.**
2626

2727
> **s01**   *"Bash is all you need"* — one tool + one loop = an agent
2828
>
29-
> **s02**   *"The loop didn't change"* — adding tools means adding handlers, not logic
29+
> **s02**   *"The loop didn't change"* — adding tools means adding handlers, not rewriting the loop
3030
>
3131
> **s03**   *"Plan before you act"* — visible plans improve task completion
3232
>
@@ -45,6 +45,8 @@
4545
> **s10**   *"Same request_id, two protocols"* — one FSM pattern powers shutdown + plan approval
4646
>
4747
> **s11**   *"Poll, claim, work, repeat"* — no coordinator needed, agents self-organize
48+
>
49+
> **s12**   *"Isolate by directory, coordinate by task ID"* — task board + optional worktree lanes
4850
4951
---
5052

@@ -77,6 +79,19 @@ def agent_loop(messages):
7779

7880
Every session layers one mechanism on top of this loop -- without changing the loop itself.
7981

82+
## Scope (Important)
83+
84+
This repository is a 0->1 learning project for building a nano Claude Code-like agent.
85+
It intentionally simplifies or omits several production mechanisms:
86+
87+
- Full event/hook buses (for example PreToolUse, SessionStart/End, ConfigChange).
88+
s12 includes only a minimal append-only lifecycle event stream for teaching.
89+
- Rule-based permission governance and trust workflows
90+
- Session lifecycle controls (resume/fork) and advanced worktree lifecycle controls
91+
- Full MCP runtime details (transport/OAuth/resource subscribe/polling)
92+
93+
Treat the team JSONL mailbox protocol in this repo as a teaching implementation, not a claim about any specific production internals.
94+
8095
## Quick Start
8196

8297
```sh
@@ -87,6 +102,7 @@ cp .env.example .env # Edit .env with your ANTHROPIC_API_KEY
87102

88103
python agents/s01_agent_loop.py # Start here
89104
python agents/s11_autonomous_agents.py # Full autonomous team
105+
python agents/s12_worktree_task_isolation.py # Task-aware worktree isolation
90106
```
91107

92108
### Web Platform
@@ -124,6 +140,9 @@ s08 Background Tasks [6] s10 Team Protocols [12]
124140
|
125141
s11 Autonomous Agents [14]
126142
idle cycle + auto-claim
143+
|
144+
s12 Worktree Isolation [16]
145+
task coordination + optional isolated execution lanes
127146
128147
[N] = number of tools
129148
```
@@ -133,7 +152,7 @@ s08 Background Tasks [6] s10 Team Protocols [12]
133152
```
134153
learn-claude-code/
135154
|
136-
|-- agents/ # Python reference implementations (s01-s11 + full)
155+
|-- agents/ # Python reference implementations (s01-s12 + full)
137156
|-- docs/{en,zh,ja}/ # Mental-model-first documentation (3 languages)
138157
|-- web/ # Interactive learning platform (Next.js)
139158
|-- skills/ # Skill files for s05
@@ -158,6 +177,7 @@ Available in [English](./docs/en/) | [中文](./docs/zh/) | [日本語](./docs/j
158177
| [s09](./docs/en/s09-agent-teams.md) | Agent Teams | *Append to send, drain to read* |
159178
| [s10](./docs/en/s10-team-protocols.md) | Team Protocols | *Same request_id, two protocols* |
160179
| [s11](./docs/en/s11-autonomous-agents.md) | Autonomous Agents | *Poll, claim, work, repeat* |
180+
| [s12](./docs/en/s12-worktree-task-isolation.md) | Worktree + Task Isolation | *Isolate by directory, coordinate by task ID* |
161181

162182
## License
163183

agents/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# agents/ - Python teaching agents (s01-s11) + reference agent (s_full)
1+
# agents/ - Python teaching agents (s01-s12) + reference agent (s_full)
22
# Each file is self-contained and runnable: python agents/s01_agent_loop.py

agents/s01_agent_loop.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
s01_agent_loop.py - The Agent Loop
44
5-
The entire secret of coding agents in one pattern:
5+
The entire secret of an AI coding agent in one pattern:
66
77
while stop_reason == "tool_use":
88
response = LLM(messages, tools)
@@ -18,8 +18,9 @@
1818
+---------------+
1919
(loop continues)
2020
21-
That's it. The ENTIRE agent is a while loop that feeds tool
22-
results back to the model until the model decides to stop.
21+
This is the core loop: feed tool results back to the model
22+
until the model decides to stop. Production agents layer
23+
policy, hooks, and lifecycle controls on top.
2324
"""
2425

2526
import os

0 commit comments

Comments
 (0)