corecoder 0.2.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {corecoder-0.2.0 → corecoder-0.3.0}/PKG-INFO +61 -20
- {corecoder-0.2.0 → corecoder-0.3.0}/README.md +50 -18
- {corecoder-0.2.0 → corecoder-0.3.0}/README_CN.md +27 -17
- corecoder-0.3.0/article/00-index_EN.md +35 -0
- corecoder-0.3.0/article/01-architecture-overview_EN.md +144 -0
- corecoder-0.3.0/article/02-agent-loop_EN.md +242 -0
- corecoder-0.3.0/article/03-tool-system_EN.md +163 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/article/04-context-compression.md +2 -0
- corecoder-0.3.0/article/04-context-compression_EN.md +145 -0
- corecoder-0.3.0/article/05-streaming-executor_EN.md +213 -0
- corecoder-0.3.0/article/06-multi-agent_EN.md +192 -0
- corecoder-0.3.0/article/07-hidden-features_EN.md +98 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/__init__.py +1 -1
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/cli.py +47 -7
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/config.py +2 -0
- corecoder-0.3.0/corecoder/llm.py +327 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/tools/edit.py +4 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/tools/write.py +2 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/pyproject.toml +10 -2
- {corecoder-0.2.0 → corecoder-0.3.0}/tests/test_core.py +49 -1
- corecoder-0.3.0/tests/test_litellm.py +245 -0
- corecoder-0.2.0/corecoder/llm.py +0 -156
- {corecoder-0.2.0 → corecoder-0.3.0}/.github/workflows/ci.yml +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/.github/workflows/publish.yml +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/.gitignore +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/LICENSE +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/article/00-index.md +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/article/01-architecture-overview.md +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/article/02-agent-loop.md +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/article/03-tool-system.md +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/article/05-streaming-executor.md +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/article/06-multi-agent.md +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/article/07-hidden-features.md +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/__main__.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/agent.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/context.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/prompt.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/session.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/tools/__init__.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/tools/agent.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/tools/base.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/tools/bash.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/tools/glob_tool.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/tools/grep.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/corecoder/tools/read.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/tests/__init__.py +0 -0
- {corecoder-0.2.0 → corecoder-0.3.0}/tests/test_tools.py +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: corecoder
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary: Minimal AI coding agent (~
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Minimal AI coding agent (~1400 LoC) inspired by Claude Code. Works with any LLM. (formerly NanoCoder)
|
|
5
5
|
Project-URL: Homepage, https://github.com/he-yufeng/CoreCoder
|
|
6
6
|
Project-URL: Repository, https://github.com/he-yufeng/CoreCoder
|
|
7
7
|
Project-URL: Issues, https://github.com/he-yufeng/CoreCoder/issues
|
|
@@ -13,8 +13,15 @@ Classifier: Development Status :: 4 - Beta
|
|
|
13
13
|
Classifier: Environment :: Console
|
|
14
14
|
Classifier: Intended Audience :: Developers
|
|
15
15
|
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
16
17
|
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
22
|
Classifier: Topic :: Software Development
|
|
23
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
24
|
+
Classifier: Topic :: Terminals
|
|
18
25
|
Requires-Python: >=3.10
|
|
19
26
|
Requires-Dist: openai>=1.0
|
|
20
27
|
Requires-Dist: prompt-toolkit>=3.0
|
|
@@ -22,6 +29,8 @@ Requires-Dist: python-dotenv>=1.0
|
|
|
22
29
|
Requires-Dist: rich>=13.0
|
|
23
30
|
Provides-Extra: dev
|
|
24
31
|
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
32
|
+
Provides-Extra: litellm
|
|
33
|
+
Requires-Dist: litellm<2.0.0,>=1.60.0; extra == 'litellm'
|
|
25
34
|
Description-Content-Type: text/markdown
|
|
26
35
|
|
|
27
36
|
# CoreCoder
|
|
@@ -36,7 +45,7 @@ Description-Content-Type: text/markdown
|
|
|
36
45
|
|
|
37
46
|
[中文](README_CN.md) | [English](README.md) | [Claude Code Architecture Deep Dive (7 articles)](article/)
|
|
38
47
|
|
|
39
|
-
**512,000 lines of TypeScript →
|
|
48
|
+
**512,000 lines of TypeScript → ~1,400 lines of Python.**
|
|
40
49
|
|
|
41
50
|
I spent two days reverse-engineering the leaked Claude Code source — all half a million lines. Then I stripped it down to the load-bearing walls and rebuilt them in Python. The result: **every key architectural pattern from Claude Code, in a codebase you can read in one sitting.**
|
|
42
51
|
|
|
@@ -63,7 +72,7 @@ Fixed: halper → helper.
|
|
|
63
72
|
|
|
64
73
|
## What You Get
|
|
65
74
|
|
|
66
|
-
Claude Code's 512K lines distilled
|
|
75
|
+
Claude Code's 512K lines distilled into ~1,400 lines across 7 patterns that actually matter:
|
|
67
76
|
|
|
68
77
|
| Pattern | Claude Code | CoreCoder |
|
|
69
78
|
|---|---|---|
|
|
@@ -114,27 +123,42 @@ corecoder -m qwen3:32b
|
|
|
114
123
|
corecoder -p "add error handling to parse_config()"
|
|
115
124
|
```
|
|
116
125
|
|
|
126
|
+
### Non-OpenAI providers (Bedrock, Vertex, Cohere, …)
|
|
127
|
+
|
|
128
|
+
For providers without an OpenAI-compatible endpoint, install the optional LiteLLM extra:
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
pip install 'corecoder[litellm]'
|
|
132
|
+
|
|
133
|
+
export CORECODER_PROVIDER=litellm
|
|
134
|
+
export CORECODER_MODEL=anthropic/claude-3-haiku # any LiteLLM model string
|
|
135
|
+
export ANTHROPIC_API_KEY=sk-ant-...
|
|
136
|
+
corecoder
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
LiteLLM routes through to 100+ providers (Bedrock, Vertex AI, Cohere, Groq, Replicate, Anyscale, etc.) using one model-string convention. The default `openai` backend is unchanged.
|
|
140
|
+
|
|
117
141
|
## Architecture
|
|
118
142
|
|
|
119
143
|
The whole thing fits in your head:
|
|
120
144
|
|
|
121
145
|
```
|
|
122
146
|
corecoder/
|
|
123
|
-
├── cli.py REPL + commands
|
|
124
|
-
├── agent.py Agent loop + parallel tools
|
|
125
|
-
├── llm.py Streaming client + retry
|
|
126
|
-
├── context.py 3-layer compression
|
|
127
|
-
├── session.py Save/resume
|
|
128
|
-
├── prompt.py System prompt
|
|
129
|
-
├── config.py Env config
|
|
147
|
+
├── cli.py REPL + commands 218 lines
|
|
148
|
+
├── agent.py Agent loop + parallel tools 122 lines
|
|
149
|
+
├── llm.py Streaming client + retry 156 lines
|
|
150
|
+
├── context.py 3-layer compression 196 lines
|
|
151
|
+
├── session.py Save/resume 68 lines
|
|
152
|
+
├── prompt.py System prompt 33 lines
|
|
153
|
+
├── config.py Env config 55 lines
|
|
130
154
|
└── tools/
|
|
131
|
-
├── bash.py Shell + safety + cd tracking
|
|
132
|
-
├── edit.py Search-replace + diff
|
|
133
|
-
├── read.py File reading
|
|
134
|
-
├── write.py File writing
|
|
135
|
-
├── glob_tool.py File search
|
|
136
|
-
├── grep.py Content search
|
|
137
|
-
└── agent.py Sub-agent spawning
|
|
155
|
+
├── bash.py Shell + safety + cd tracking 115 lines
|
|
156
|
+
├── edit.py Search-replace + diff 85 lines
|
|
157
|
+
├── read.py File reading 53 lines
|
|
158
|
+
├── write.py File writing 36 lines
|
|
159
|
+
├── glob_tool.py File search 47 lines
|
|
160
|
+
├── grep.py Content search 78 lines
|
|
161
|
+
└── agent.py Sub-agent spawning 58 lines
|
|
138
162
|
```
|
|
139
163
|
|
|
140
164
|
## Use as a Library
|
|
@@ -165,9 +189,11 @@ class HttpTool(Tool):
|
|
|
165
189
|
## Commands
|
|
166
190
|
|
|
167
191
|
```
|
|
192
|
+
/model Show current model
|
|
168
193
|
/model <name> Switch model mid-conversation
|
|
169
194
|
/compact Compress context (like Claude Code's /compact)
|
|
170
|
-
/tokens Token usage
|
|
195
|
+
/tokens Token usage + cost estimate
|
|
196
|
+
/diff Show files modified this session
|
|
171
197
|
/save Save session to disk
|
|
172
198
|
/sessions List saved sessions
|
|
173
199
|
/reset Clear history
|
|
@@ -178,7 +204,7 @@ quit Exit
|
|
|
178
204
|
|
|
179
205
|
| | Claude Code | Claw-Code | Aider | CoreCoder |
|
|
180
206
|
|---|---|---|---|---|
|
|
181
|
-
| Code | 512K lines (closed) | 100K+ lines | 50K+ lines |
|
|
207
|
+
| Code | 512K lines (closed) | 100K+ lines | 50K+ lines | **~1,400 lines** |
|
|
182
208
|
| Models | Anthropic only | Multi | Multi | **Any OpenAI-compatible** |
|
|
183
209
|
| Readable? | No | Hard | Medium | **One afternoon** |
|
|
184
210
|
| Purpose | Use it | Use it | Use it | **Understand it, build yours** |
|
|
@@ -187,6 +213,21 @@ quit Exit
|
|
|
187
213
|
|
|
188
214
|
I wrote [7 articles](article/) breaking down Claude Code's architecture — the agent loop, tool system, context compression, streaming executor, multi-agent, and 44 hidden feature flags. If you want to understand *why* CoreCoder is designed this way, start there.
|
|
189
215
|
|
|
216
|
+
## FAQ
|
|
217
|
+
|
|
218
|
+
**Does CoreCoder support Skills / Subagents / MCP?**
|
|
219
|
+
|
|
220
|
+
No, and that's intentional. CoreCoder is the minimal runnable core — agent loop, tools, streaming, compaction. Skills, Subagents, MCP, hooks, and plugins are upper-layer features that Claude Code layers on top; if CoreCoder had them too it would stop being a teaching artifact. The architecture articles above cover how those systems work in Claude Code, so you can add them yourself if you need to.
|
|
221
|
+
|
|
222
|
+
If you want Skills specifically, the recipe is small: scan `~/.claude/skills/*.md` at startup, list their titles in the system prompt, and let the agent ask for a skill by name before you inline that file's body into the conversation.
|
|
223
|
+
|
|
224
|
+
## Related Projects
|
|
225
|
+
|
|
226
|
+
- **[CodeJoust](https://github.com/he-yufeng/CodeJoust)** — a CLI arena that races Claude Code, aider, Codex, and Gemini (Cursor + OpenHands next) on the same bug in isolated git worktrees, scores by tests+cost+diff+time, hands you the winning patch. If you ever wondered *which* AI coding CLI is actually better for your task, CodeJoust answers it empirically.
|
|
227
|
+
- **[AnyCoder](https://github.com/he-yufeng/AnyCoder)** — a practical terminal AI coding agent built on the same architecture as CoreCoder but with litellm, session persistence, and 100+ model support. Use this one if you want a tool; use CoreCoder if you want to read source.
|
|
228
|
+
- **[LiteBench](https://github.com/he-yufeng/LiteBench)** — one-command LLM / agent benchmark. Ships 7 built-in tasks (HumanEval/GSM8K/MMLU/...) and YAML-defined custom tasks, with a single-file HTML dashboard.
|
|
229
|
+
- **[RepoWiki](https://github.com/he-yufeng/RepoWiki)** — open-source DeepWiki alternative. `pip install repowiki`, one command to turn any local or GitHub repo into a wiki with dependency graph, architecture diagram, and LLM-generated module pages.
|
|
230
|
+
|
|
190
231
|
## License
|
|
191
232
|
|
|
192
233
|
MIT. Fork it, learn from it, ship something better. A mention of this project is appreciated.
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
[中文](README_CN.md) | [English](README.md) | [Claude Code Architecture Deep Dive (7 articles)](article/)
|
|
12
12
|
|
|
13
|
-
**512,000 lines of TypeScript →
|
|
13
|
+
**512,000 lines of TypeScript → ~1,400 lines of Python.**
|
|
14
14
|
|
|
15
15
|
I spent two days reverse-engineering the leaked Claude Code source — all half a million lines. Then I stripped it down to the load-bearing walls and rebuilt them in Python. The result: **every key architectural pattern from Claude Code, in a codebase you can read in one sitting.**
|
|
16
16
|
|
|
@@ -37,7 +37,7 @@ Fixed: halper → helper.
|
|
|
37
37
|
|
|
38
38
|
## What You Get
|
|
39
39
|
|
|
40
|
-
Claude Code's 512K lines distilled
|
|
40
|
+
Claude Code's 512K lines distilled into ~1,400 lines across 7 patterns that actually matter:
|
|
41
41
|
|
|
42
42
|
| Pattern | Claude Code | CoreCoder |
|
|
43
43
|
|---|---|---|
|
|
@@ -88,27 +88,42 @@ corecoder -m qwen3:32b
|
|
|
88
88
|
corecoder -p "add error handling to parse_config()"
|
|
89
89
|
```
|
|
90
90
|
|
|
91
|
+
### Non-OpenAI providers (Bedrock, Vertex, Cohere, …)
|
|
92
|
+
|
|
93
|
+
For providers without an OpenAI-compatible endpoint, install the optional LiteLLM extra:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
pip install 'corecoder[litellm]'
|
|
97
|
+
|
|
98
|
+
export CORECODER_PROVIDER=litellm
|
|
99
|
+
export CORECODER_MODEL=anthropic/claude-3-haiku # any LiteLLM model string
|
|
100
|
+
export ANTHROPIC_API_KEY=sk-ant-...
|
|
101
|
+
corecoder
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
LiteLLM routes through to 100+ providers (Bedrock, Vertex AI, Cohere, Groq, Replicate, Anyscale, etc.) using one model-string convention. The default `openai` backend is unchanged.
|
|
105
|
+
|
|
91
106
|
## Architecture
|
|
92
107
|
|
|
93
108
|
The whole thing fits in your head:
|
|
94
109
|
|
|
95
110
|
```
|
|
96
111
|
corecoder/
|
|
97
|
-
├── cli.py REPL + commands
|
|
98
|
-
├── agent.py Agent loop + parallel tools
|
|
99
|
-
├── llm.py Streaming client + retry
|
|
100
|
-
├── context.py 3-layer compression
|
|
101
|
-
├── session.py Save/resume
|
|
102
|
-
├── prompt.py System prompt
|
|
103
|
-
├── config.py Env config
|
|
112
|
+
├── cli.py REPL + commands 218 lines
|
|
113
|
+
├── agent.py Agent loop + parallel tools 122 lines
|
|
114
|
+
├── llm.py Streaming client + retry 156 lines
|
|
115
|
+
├── context.py 3-layer compression 196 lines
|
|
116
|
+
├── session.py Save/resume 68 lines
|
|
117
|
+
├── prompt.py System prompt 33 lines
|
|
118
|
+
├── config.py Env config 55 lines
|
|
104
119
|
└── tools/
|
|
105
|
-
├── bash.py Shell + safety + cd tracking
|
|
106
|
-
├── edit.py Search-replace + diff
|
|
107
|
-
├── read.py File reading
|
|
108
|
-
├── write.py File writing
|
|
109
|
-
├── glob_tool.py File search
|
|
110
|
-
├── grep.py Content search
|
|
111
|
-
└── agent.py Sub-agent spawning
|
|
120
|
+
├── bash.py Shell + safety + cd tracking 115 lines
|
|
121
|
+
├── edit.py Search-replace + diff 85 lines
|
|
122
|
+
├── read.py File reading 53 lines
|
|
123
|
+
├── write.py File writing 36 lines
|
|
124
|
+
├── glob_tool.py File search 47 lines
|
|
125
|
+
├── grep.py Content search 78 lines
|
|
126
|
+
└── agent.py Sub-agent spawning 58 lines
|
|
112
127
|
```
|
|
113
128
|
|
|
114
129
|
## Use as a Library
|
|
@@ -139,9 +154,11 @@ class HttpTool(Tool):
|
|
|
139
154
|
## Commands
|
|
140
155
|
|
|
141
156
|
```
|
|
157
|
+
/model Show current model
|
|
142
158
|
/model <name> Switch model mid-conversation
|
|
143
159
|
/compact Compress context (like Claude Code's /compact)
|
|
144
|
-
/tokens Token usage
|
|
160
|
+
/tokens Token usage + cost estimate
|
|
161
|
+
/diff Show files modified this session
|
|
145
162
|
/save Save session to disk
|
|
146
163
|
/sessions List saved sessions
|
|
147
164
|
/reset Clear history
|
|
@@ -152,7 +169,7 @@ quit Exit
|
|
|
152
169
|
|
|
153
170
|
| | Claude Code | Claw-Code | Aider | CoreCoder |
|
|
154
171
|
|---|---|---|---|---|
|
|
155
|
-
| Code | 512K lines (closed) | 100K+ lines | 50K+ lines |
|
|
172
|
+
| Code | 512K lines (closed) | 100K+ lines | 50K+ lines | **~1,400 lines** |
|
|
156
173
|
| Models | Anthropic only | Multi | Multi | **Any OpenAI-compatible** |
|
|
157
174
|
| Readable? | No | Hard | Medium | **One afternoon** |
|
|
158
175
|
| Purpose | Use it | Use it | Use it | **Understand it, build yours** |
|
|
@@ -161,6 +178,21 @@ quit Exit
|
|
|
161
178
|
|
|
162
179
|
I wrote [7 articles](article/) breaking down Claude Code's architecture — the agent loop, tool system, context compression, streaming executor, multi-agent, and 44 hidden feature flags. If you want to understand *why* CoreCoder is designed this way, start there.
|
|
163
180
|
|
|
181
|
+
## FAQ
|
|
182
|
+
|
|
183
|
+
**Does CoreCoder support Skills / Subagents / MCP?**
|
|
184
|
+
|
|
185
|
+
No, and that's intentional. CoreCoder is the minimal runnable core — agent loop, tools, streaming, compaction. Skills, Subagents, MCP, hooks, and plugins are upper-layer features that Claude Code layers on top; if CoreCoder had them too it would stop being a teaching artifact. The architecture articles above cover how those systems work in Claude Code, so you can add them yourself if you need to.
|
|
186
|
+
|
|
187
|
+
If you want Skills specifically, the recipe is small: scan `~/.claude/skills/*.md` at startup, list their titles in the system prompt, and let the agent ask for a skill by name before you inline that file's body into the conversation.
|
|
188
|
+
|
|
189
|
+
## Related Projects
|
|
190
|
+
|
|
191
|
+
- **[CodeJoust](https://github.com/he-yufeng/CodeJoust)** — a CLI arena that races Claude Code, aider, Codex, and Gemini (Cursor + OpenHands next) on the same bug in isolated git worktrees, scores by tests+cost+diff+time, hands you the winning patch. If you ever wondered *which* AI coding CLI is actually better for your task, CodeJoust answers it empirically.
|
|
192
|
+
- **[AnyCoder](https://github.com/he-yufeng/AnyCoder)** — a practical terminal AI coding agent built on the same architecture as CoreCoder but with litellm, session persistence, and 100+ model support. Use this one if you want a tool; use CoreCoder if you want to read source.
|
|
193
|
+
- **[LiteBench](https://github.com/he-yufeng/LiteBench)** — one-command LLM / agent benchmark. Ships 7 built-in tasks (HumanEval/GSM8K/MMLU/...) and YAML-defined custom tasks, with a single-file HTML dashboard.
|
|
194
|
+
- **[RepoWiki](https://github.com/he-yufeng/RepoWiki)** — open-source DeepWiki alternative. `pip install repowiki`, one command to turn any local or GitHub repo into a wiki with dependency graph, architecture diagram, and LLM-generated module pages.
|
|
195
|
+
|
|
164
196
|
## License
|
|
165
197
|
|
|
166
198
|
MIT. Fork it, learn from it, ship something better. A mention of this project is appreciated.
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
[](LICENSE)
|
|
11
11
|
[](https://github.com/he-yufeng/CoreCoder/actions)
|
|
12
12
|
|
|
13
|
-
**51万行 TypeScript →
|
|
13
|
+
**51万行 TypeScript → ~1,400 行 Python。**
|
|
14
14
|
|
|
15
15
|
我逆向了 Claude Code 泄露的全部源码,然后把不承重的部分全扔掉,用 Python 重建了核心。成果:**Claude Code 的每一个关键架构模式,浓缩在一个下午能读完的代码库里。**
|
|
16
16
|
|
|
@@ -94,21 +94,21 @@ corecoder -p "给 parse_config() 加上错误处理"
|
|
|
94
94
|
|
|
95
95
|
```
|
|
96
96
|
corecoder/
|
|
97
|
-
├── cli.py REPL + 命令
|
|
98
|
-
├── agent.py Agent 循环 + 并行执行
|
|
99
|
-
├── llm.py 流式客户端 + 重试
|
|
100
|
-
├── context.py 三层压缩
|
|
101
|
-
├── session.py 会话保存/恢复
|
|
102
|
-
├── prompt.py 系统提示词
|
|
103
|
-
├── config.py 环境变量配置
|
|
97
|
+
├── cli.py REPL + 命令 218 行
|
|
98
|
+
├── agent.py Agent 循环 + 并行执行 122 行
|
|
99
|
+
├── llm.py 流式客户端 + 重试 156 行
|
|
100
|
+
├── context.py 三层压缩 196 行
|
|
101
|
+
├── session.py 会话保存/恢复 68 行
|
|
102
|
+
├── prompt.py 系统提示词 33 行
|
|
103
|
+
├── config.py 环境变量配置 55 行
|
|
104
104
|
└── tools/
|
|
105
|
-
├── bash.py Shell + 安全 + cd 追踪
|
|
106
|
-
├── edit.py 搜索替换 + diff
|
|
107
|
-
├── read.py 文件读取
|
|
108
|
-
├── write.py 文件写入
|
|
109
|
-
├── glob_tool.py 文件搜索
|
|
110
|
-
├── grep.py 内容搜索
|
|
111
|
-
└── agent.py 子代理生成
|
|
105
|
+
├── bash.py Shell + 安全 + cd 追踪 115 行
|
|
106
|
+
├── edit.py 搜索替换 + diff 85 行
|
|
107
|
+
├── read.py 文件读取 53 行
|
|
108
|
+
├── write.py 文件写入 36 行
|
|
109
|
+
├── glob_tool.py 文件搜索 47 行
|
|
110
|
+
├── grep.py 内容搜索 78 行
|
|
111
|
+
└── agent.py 子代理生成 58 行
|
|
112
112
|
```
|
|
113
113
|
|
|
114
114
|
## 当库用
|
|
@@ -139,9 +139,11 @@ class HttpTool(Tool):
|
|
|
139
139
|
## 命令
|
|
140
140
|
|
|
141
141
|
```
|
|
142
|
+
/model 查看当前模型
|
|
142
143
|
/model <名称> 切换模型
|
|
143
144
|
/compact 压缩上下文(对标 Claude Code 的 /compact)
|
|
144
|
-
/tokens 查看 token 用量
|
|
145
|
+
/tokens 查看 token 用量 + 费用估算
|
|
146
|
+
/diff 查看本次会话修改的文件
|
|
145
147
|
/save 保存会话
|
|
146
148
|
/sessions 列出已保存的会话
|
|
147
149
|
/reset 清空历史
|
|
@@ -152,7 +154,7 @@ quit 退出
|
|
|
152
154
|
|
|
153
155
|
| | Claude Code | Claw-Code | Aider | CoreCoder |
|
|
154
156
|
|---|---|---|---|---|
|
|
155
|
-
| 代码量 | 51万行(闭源) | 10万+行 | 5万+行 |
|
|
157
|
+
| 代码量 | 51万行(闭源) | 10万+行 | 5万+行 | **~1,400 行** |
|
|
156
158
|
| 模型 | 仅 Anthropic | 多模型 | 多模型 | **任意 OpenAI 兼容** |
|
|
157
159
|
| 能通读吗? | 不能 | 很难 | 有点费劲 | **一个下午** |
|
|
158
160
|
| 适合 | 直接用 | 直接用 | 直接用 | **先看懂,再造自己的** |
|
|
@@ -161,6 +163,14 @@ quit 退出
|
|
|
161
163
|
|
|
162
164
|
我还写了 [7 篇 Claude Code 架构深度导读](article/):Agent 循环、工具系统、上下文压缩、流式执行、多 Agent、隐藏功能。想知道 CoreCoder 为什么这样设计,从那里开始。
|
|
163
165
|
|
|
166
|
+
## FAQ
|
|
167
|
+
|
|
168
|
+
**CoreCoder 支持 Skill / Subagent / MCP 吗?**
|
|
169
|
+
|
|
170
|
+
不支持,这是刻意的。CoreCoder 只保留可运行的最小核心 —— agent 循环、工具、流式、压缩。Skill、Subagent、MCP、hook、plugin 都是 Claude Code 在上层加的特性;如果 CoreCoder 也全都做了,就不再是一个可读的教学产物。上面的架构导读系列讲了 Claude Code 里这些系统是怎么工作的,你可以照着自己加。
|
|
171
|
+
|
|
172
|
+
如果你只是想要 Skill,配方很简单:启动时扫 `~/.claude/skills/*.md`,把标题列进 system prompt,让 agent 按名字请求某个 skill,再把那个文件的内容 inline 进对话就行了。
|
|
173
|
+
|
|
164
174
|
## License
|
|
165
175
|
|
|
166
176
|
MIT。Fork,然后拿去造更好的东西,如果能标注此出处就更好了。
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Claude Code Source Code Guide
|
|
2
|
+
|
|
3
|
+
On March 31, 2026, a residual `.map` file in Anthropic's npm package leaked the entire source code of Claude Code. 1903 files, 512,664 lines of TypeScript.
|
|
4
|
+
|
|
5
|
+
This is a series of articles I wrote after reading the entire source code. It's not a comprehensive document (that 160,000-word complete version), but it rather focuses on what I believe are the 7 most important aspects for developers to understand. Each article revolves around a core question with accurate code references.
|
|
6
|
+
|
|
7
|
+
If you're working on AI Agent-related projects, you'll inevitably use these design patterns sooner or later.
|
|
8
|
+
|
|
9
|
+
## Table of Contents
|
|
10
|
+
|
|
11
|
+
1. **[The 510,000 Lines Codebase](01-architecture-overview_EN.md)** — Claude Code's technology stack, directory structure, and ten design philosophies. Building a global mental model.
|
|
12
|
+
|
|
13
|
+
2. **[The while(true) on line 1729](02-agent-loop_EN.md)** — The core loop of the AI Agent: how query.ts drives tool calls, message orchestration, and interrupt recovery.
|
|
14
|
+
|
|
15
|
+
3. **[Let AI Safely Modify Your Code](03-tool-system_EN.md)** — The ingenuity of the tool system's interface design, two-phase gating, and search-replace-edit.
|
|
16
|
+
|
|
17
|
+
4. **[Finite Window, Infinite Tasks](04-context-compression_EN.md)** — Engineering details of the four-layer context compression strategy, and why it's not simply "truncating old messages".
|
|
18
|
+
|
|
19
|
+
5. **[Think and Do](05-streaming-executor_EN.md)** — How StreamingToolExecutor starts executing the tool before the model has finished speaking.
|
|
20
|
+
|
|
21
|
+
6. **[When One Claude Isn't Enough](06-multi-agent_EN.md)** — Multi-Agent Collaboration System: Sub-Agent Generation, Worktree Isolation, Team Orchestration.
|
|
22
|
+
|
|
23
|
+
7. **[The Secret Behind Feature Flags](07-hidden-features_EN.md)** — Technical Details of 44 Unreleased Features: KAIROS Persistent Mode, Buddy Pet System, Voice Mode, Bridge Mode.
|
|
24
|
+
|
|
25
|
+
## Supporting Project
|
|
26
|
+
|
|
27
|
+
I've created a working reference implementation of the core architectural patterns discussed in these articles in 1300 lines of Python: [CoreCoder](https://github.com/he-yufeng/CoreCoder). You can compare the code and the articles.
|
|
28
|
+
|
|
29
|
+
## Full Version
|
|
30
|
+
|
|
31
|
+
You can find a comprehensive guide (16 articles, 160,000 words, covering the build system to every subsystem of the MCP protocol) [here](https://github.com/he-yufeng/CoreCoder/tree/main/docs).
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
Author: [He Yufeng](https://github.com/he-yufeng) · [Zhihu: Claude Code Source Code Analysis (170,000+ views)](https://zhuanlan.zhihu.com/p/1898797658343862272)
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# Part 1: The 510,000 Lines Codebase
|
|
2
|
+
|
|
3
|
+
`find . -name '*.ts' | wc -l` returns 1903 files. `cloc --include-lang=TypeScript .` returns 512,664 lines.
|
|
4
|
+
|
|
5
|
+
To be honest, my first reaction was to close the terminal—this thing even lags for a few seconds when opened with VS Code. But I work on AI Agents (Moonshot AI / Kimi) and use Claude Code heavily every day, so I really wanted to know what was inside. So I gritted my teeth and started reading it.
|
|
6
|
+
|
|
7
|
+
This is the first part of a series, and it won't delve into any specific modules. The goal is to build a mental map for you: what is Claude Code, why was its technology stack chosen this way, where are the 510,000 lines of code distributed, and the ten recurring design patterns I summarized after reading the entire code.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Technology Stack: Bun + TypeScript + React
|
|
12
|
+
|
|
13
|
+
Yes, you read that right. A terminal CLI tool uses React.
|
|
14
|
+
|
|
15
|
+
Using React for the TUI layer isn't new; Ink has been around since 2017, and it's used in Gatsby CLI and Prisma CLI.
|
|
16
|
+
|
|
17
|
+
However, Claude Code's scenario is significantly more complex than a typical CLI: multiple agents running in parallel, streaming output, user interruptions during tool execution, and permission pop-ups. At this level of complexity in state management, using React is indeed more reasonable than manual development.
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
Technology Choices:
|
|
21
|
+
|
|
22
|
+
Bun → Fast startup (4x faster than Node.js), native TypeScript support
|
|
23
|
+
TypeScript → 510,000 lines without a type system is practically suicide
|
|
24
|
+
React + Ink → Declarative terminal UI, complex state management
|
|
25
|
+
Commander.js → CLI parameter parsing (most mature in the Node ecosystem)
|
|
26
|
+
Zod → Runtime data validation + automatic JSON schema generation
|
|
27
|
+
ripgrep → Search engine written in Rust (GrepTool directly calls binary code)
|
|
28
|
+
GrowthBook → Remote feature flags and A/B testing
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
TypeScript is a self-explanatory choice. Without a type system, changing a single function signature in 510,000 lines of code would cause countless system crashes. Zod is a clever choice too — it performs both runtime data validation and type inference, essentially using one schema to do two things: tell the TypeScript compiler "what the input looks like," and intercept invalid input at runtime.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Directory Structure: Four Layers
|
|
37
|
+
|
|
38
|
+
After two days of painstakingly studying Claude Code, I think it can be understood by dividing it into four layers. This isn't an official division, but rather my own mental model after reading it.
|
|
39
|
+
|
|
40
|
+
### First Layer: Entry Point and UI
|
|
41
|
+
|
|
42
|
+
This is the layer with things users can directly see and touch. Everything is distributed across:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
src/screens/ → React pages (REPL main interface, Onboarding, Doctor diagnostics)
|
|
46
|
+
src/components/ → UI components (permission pop-ups, tool execution progress, diff preview)
|
|
47
|
+
src/commands/ → Forward-slash commands (/commit, /compact, /model, /review)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`src/screens/REPL.tsx` is the main interface, over 3000 lines long, essentially the "shell" of the entire application. It handles input capture, message rendering, and command dispatch. Everything you see in the terminal originates from here.
|
|
51
|
+
|
|
52
|
+
### Second Layer: The Engine
|
|
53
|
+
|
|
54
|
+
The engine layer contains two files that can be thought of as Claude Code's brain.
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
src/query.ts → Core agentic loop (line 1729), while(true) loop
|
|
58
|
+
src/QueryEngine.ts → Session state manager (line 1295), one instance per conversation
|
|
59
|
+
```
|
|
60
|
+
`QueryEngine` is the outer "container", managing state across rounds (message history, token usage, file cache). `query.ts` is the inner "engine," triggering `queryLoop()` once per user input. The latter contains the `while(true)` loop that calls the LLM, executes tools, handles errors, and continues until the LLM provides a plain text response.
|
|
61
|
+
|
|
62
|
+
[Part two of this series](02-agent-loop_EN.md) will delve deeper into `query.ts`.
|
|
63
|
+
|
|
64
|
+
### Third Layer: Tools
|
|
65
|
+
|
|
66
|
+
Claude Code has 30+ tools that can be seen as all capabilities that the LLM can call. A non-exhaustive list is:
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
src/tools/BashTool/ → Line 1143, the most complex single tool
|
|
70
|
+
src/tools/AgentTool/ → Line 1397 (total 6700 lines in the directory), sub-agent generation
|
|
71
|
+
src/tools/FileEditTool/ → Search, replace, and edit
|
|
72
|
+
src/tools/FileReadTool/ → File reading
|
|
73
|
+
src/tools/GrepTool/ → Ripgrep wrapper
|
|
74
|
+
src/tools/GlobTool/ → File pattern matching
|
|
75
|
+
src/tools/WebFetchTool/ → Web page content fetching
|
|
76
|
+
src/tools/WebSearchTool/ → Web page search
|
|
77
|
+
src/tools/NotebookEditTool/ → Jupyter Notebook editing
|
|
78
|
+
src/tools/LSPTool/ → Language server protocol interaction
|
|
79
|
+
|
|
80
|
+
...and a dozen more
|
|
81
|
+
```
|
|
82
|
+
Each tool is completely self-contained: schema definition, permission checks, execution logic, UI rendering, and context compression summarization are all in one directory. There's no global registry, no base class inheritance; everything is a pure object generated by the `buildTool()` factory function. This will be discussed in detail in Part 3.
|
|
83
|
+
|
|
84
|
+
### Fourth Layer: Infrastructure
|
|
85
|
+
|
|
86
|
+
Infrastructure is the underlying system supporting the operation of the above three layers.
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
src/services/ → API client, MCP protocol, OAuth, caching
|
|
90
|
+
src/utils/ → Permission system, Feature Flag, model configuration, event tracking
|
|
91
|
+
src/memdir/ → Memory system (cross-session persistence)
|
|
92
|
+
src/skills/ → Skill system (reusable task templates)
|
|
93
|
+
src/buddy/ → Pet system (unreleased, but the code is all there)
|
|
94
|
+
src/voice/ → Voice mode (codename Amber Quartz)
|
|
95
|
+
src/bridge/ → Remote control mode (31 files)
|
|
96
|
+
src/coordinator/ → Multi-Agent orchestration
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The permission system deserves a separate mention: Claude Code has five permission levels, from "fully automatic" to "always ask". Tools calls go through two stages: first, `validateInput()` checks the input's validity (if invalid, it comes back to the LLM querying for a new input without a pop-up); then, `checkPermissions()` checks permissions (possibly prompting the user for confirmation). This two-phase design is clever — most rejected operations are due to invalid input, not insufficient permissions, and the two-phase design greatly reduces pop-up interruptions.
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## Ten Design Philosophies
|
|
104
|
+
|
|
105
|
+
After reading 500,000 lines of code, some patterns recurred. I've tried to summarize them into ten, which will be cited in each subsequent article.
|
|
106
|
+
|
|
107
|
+
**1. Externalize State.** All external dependencies of `QueryEngine` are injected through `QueryEngineConfig` (20+ fields), not imported internally. This means you can pass a mock list of tools and API clients into unit tests without starting the entire application.
|
|
108
|
+
|
|
109
|
+
**2. Incremental Complexity.** Keep simple things simple. `FileReadTool` has only a few dozen lines; `BashTool` has 1143 lines. Not every tool needs the same complexity; complexity should be concentrated where it's needed.
|
|
110
|
+
|
|
111
|
+
**3. Feature Flag Gating.** 44 compile-time/runtime flags. A feature failed internal validation? It's deleted at compile time, leaving no string. This isn't a "if (false)" style fake deletion; it's a physical deletion via DCE (Dead Code Elimination) in Bundle.
|
|
112
|
+
|
|
113
|
+
**4. Type as Documentation.** The Zod schema performs both validation and type inference. The field names in the `ToolDef` type are the most accurate interface documentation. Newcomers can understand which methods a tool needs to implement just by looking at the type definition.
|
|
114
|
+
|
|
115
|
+
**5. Context Economics.** Tokens are like money. Tool output exceeding a threshold is written to disk, but only a digest is left in the context. The context has four layers of compression. The `getToolUseSummary()` method defines the digest strategy for each tool during compression.
|
|
116
|
+
|
|
117
|
+
**6. Security Layering.** Two-phase gating (authentication + permissions), five-level permission mode, BashTool's command classifier (parses commands into search/read/write categories to match permission rules), macOS sandbox-exec / Linux seccomp sandbox. Security isn't a single wall, it's multiple walls.
|
|
118
|
+
|
|
119
|
+
**7. Graceful Degradation.** API timeout retries (exponential backoff, maximum 5 times). If the model doesn't support `stream_options`, remove that parameter and retry. Sub-Agent crashes don't affect the main loop. Output hitting `max_output_tokens` limits automatic retries to a maximum of 3 times.
|
|
120
|
+
|
|
121
|
+
**8. Declarative Configuration.** Tools, commands, Skills, and Agent types are all declarative. A Skill is simply a `.md` file with a YAML frontmatter. An Agent type is a configuration object. Assembled on demand at runtime, no registration required in the code.
|
|
122
|
+
|
|
123
|
+
**9. Streaming Everything.** **API response streaming:** StreamingToolExecutor begins executing the tool while the model is still outputting, pushing progress information to the terminal in real time. Users always see progress, instead of waiting for a long task to complete.
|
|
124
|
+
|
|
125
|
+
**10. Experimental Progress:** Ablative testing infrastructure (`ABLATION_BASELINE` allows for one-click shutdown of thinking/compact/memory/background), A/B testing flags, and the ability to run controlled experiments to quantify the value of each feature before launch. It's not about "launching because it feels useful," but about "launching only when the data says it's useful."
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## About this series
|
|
130
|
+
|
|
131
|
+
The following six articles will delve deeper into each of these:
|
|
132
|
+
|
|
133
|
+
- Article 2: The while(true) loop on line 1729 of `query.ts`
|
|
134
|
+
- Article 3: The design of the tool system and search, replace, and edit
|
|
135
|
+
- Article 4: Four-layer context compression
|
|
136
|
+
- Article 5: Parallelism of streaming tools in StreamingToolExecutor
|
|
137
|
+
- Article 6: Multi-Agent Collaboration System
|
|
138
|
+
- Article 7: Unreleased features behind Feature Flag
|
|
139
|
+
|
|
140
|
+
I also replicated these core design patterns in 1300 lines of Python: [CoreCoder](https://github.com/he-yufeng/CoreCoder). Each article will explain the source code and the CoreCoder implementation.
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
> This article is the first in the [Claude Code Source Code Guide](00-index.md) series.
|