Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# CoreCoder

**The nanoGPT of coding agents. A 1.6k-line engine inside 3,707 readable lines of pure Python: understand how a coding agent actually works, then fork your own.**
**The nanoGPT of coding agents. A 1.6k-line engine inside 3,727 readable lines of pure Python: understand how a coding agent actually works, then fork your own.**

*learn from it · fork it · ship something better*

Expand All @@ -12,7 +12,7 @@
[![Python](https://img.shields.io/badge/python-3.10+-blue)](https://python.org)
[![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE)
[![Tests](https://github.com/he-yufeng/CoreCoder/actions/workflows/ci.yml/badge.svg)](https://github.com/he-yufeng/CoreCoder/actions)
[![engine](https://img.shields.io/badge/engine-1655_LoC-blue)](article/00-index_EN.md)
[![engine](https://img.shields.io/badge/engine-1667_LoC-blue)](article/00-index_EN.md)
[![essays](https://img.shields.io/badge/source--reading-8_bilingual-orange)](article/00-index_EN.md)

</div>
Expand All @@ -25,7 +25,7 @@

| | CoreCoder | Claude Code | aider | nanoGPT |
|---|---|---|---|---|
| Lines of code | ~1,655 engine / 3,707 total | hundreds of thousands (closed) | tens of thousands of Python | ~600 (two files) |
| Lines of code | ~1,667 engine / 3,727 total | hundreds of thousands (closed) | tens of thousands of Python | ~600 (two files) |
| Time to read it all | one afternoon | can't (closed) | a few days of slogging | one afternoon |
| Breakpoint, change, rerun? | yes, every line | no | yes, but there's a lot | yes |
| What it's for | understand one, then fork your own | production coding assistant | terminal pair-programming | minimal GPT for teaching |
Expand All @@ -36,7 +36,7 @@ The nanoGPT column is there as a reference point: minimal, readable, but it teac

I've always felt coding agents get talked about as if they were arcane. Strip a tool like Claude Code or Cursor all the way down and the core is a `while` loop wrapped around a large model, plus seven or eight tools that let it actually do things. The hard part was never the loop; it's everything the loop has to cope with once it meets the real world. CoreCoder is the minimal version that writes that core out honestly.

The engine (loop, model interface, context, tools, sessions) is 1,655 lines once you drop blank lines and comments. Counting the outer CLI, config and packaging too, the whole package is 26 files: 3,707 physical lines, 3,101 net, every one short enough to read in a single sitting. The growth since the original 1,161-line snapshot went into visible features: plan mode, hooks and checkpoints, each documented below.
The engine (loop, model interface, context, tools, sessions) is 1,667 lines once you drop blank lines and comments. Counting the outer CLI, config and packaging too, the whole package is 26 files: 3,727 physical lines, 3,119 net, every one short enough to read in a single sitting. The growth since the original 1,161-line snapshot went into visible features: plan mode, hooks and checkpoints, each documented below.

And it really runs: reads and writes files, executes shell, spawns sub-agents, compacts context in three tiers, and tells you the tokens and dollars a run burned whenever you ask. Anything that would mutate your disk or run a command stops for your consent first. The offline suite runs on Linux, macOS and Windows, with a separate opt-in live smoke test. But the point of it running isn't to become your daily driver. It runs so the walkthrough can't lie: a reference that shows how an agent works has to actually work.

Expand Down Expand Up @@ -94,14 +94,14 @@ corecoder/
├── agent.py agent loop + parallel tool exec 240 lines ← start here
├── llm.py streaming client + retry + cost 407 lines
├── context.py three-tier context compaction 265 lines
├── session.py save / resume / fork 178 lines
├── session.py save / resume / fork / export 191 lines
├── permissions.py consent for mutating tools 81 lines
├── hooks.py Pre/PostToolUse shell hooks 87 lines
├── shell.py POSIX shell routing (Git Bash on Windows) 61 lines
├── mcp.py MCP tools, resources, prompt templates 413 lines
├── mcp_http.py remote Streamable HTTP transport 229 lines
├── prompt.py system prompt 41 lines
├── cli.py REPL + slash commands + one-shot 472 lines
├── cli.py REPL + slash commands + one-shot 486 lines
├── config.py env-var config 61 lines
├── checkpoints.py /undo snapshot and restore 125 lines
├── demo.py offline end-to-end demo 101 lines
Expand Down Expand Up @@ -237,6 +237,8 @@ If the session is no longer in the recent list, try `corecoder --list-sessions "

For scripts, add `--json` to `--list-sessions`. The command writes an array of `id`, `model`, `saved_at` and `preview` records to stdout, or `[]` when nothing matches. The search, preview and 20-result limit are the same as the text view.

To review a conversation without resuming it, run `corecoder --export-session ID > dialogue.md`. The UTF-8 Markdown includes retained user and assistant text in order. It leaves out internal summaries and structured tool data, does not change the archive, and needs no API key. Compacted turns are not recovered, and the text is not redacted.

To try the last prompt again without showing the model the first attempt, check the turn count and fork just before that prompt. If the original has three retained turns:

```bash
Expand Down
14 changes: 8 additions & 6 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# CoreCoder

**编程 agent 里的 nanoGPT。1.6k 行引擎、整包 3707 行纯 Python 全部一口气可读,读懂一个 coding agent 到底怎么运作,再 fork 出你自己的。**
**编程 agent 里的 nanoGPT。1.6k 行引擎、整包 3727 行纯 Python 全部一口气可读,读懂一个 coding agent 到底怎么运作,再 fork 出你自己的。**

*learn from it · fork it · ship something better*

Expand All @@ -12,7 +12,7 @@
[![Python](https://img.shields.io/badge/python-3.10+-blue)](https://python.org)
[![License: MIT](https://img.shields.io/badge/license-MIT-green)](LICENSE)
[![Tests](https://github.com/he-yufeng/CoreCoder/actions/workflows/ci.yml/badge.svg)](https://github.com/he-yufeng/CoreCoder/actions)
[![engine](https://img.shields.io/badge/engine-1655_LoC-blue)](article/)
[![engine](https://img.shields.io/badge/engine-1667_LoC-blue)](article/)
[![源码导读](https://img.shields.io/badge/源码导读-8篇双语-orange)](article/)

</div>
Expand All @@ -25,7 +25,7 @@

| | CoreCoder | Claude Code | aider | nanoGPT |
|---|---|---|---|---|
| 代码量 | 引擎约 1655 行 / 整包 3707 行 | 几十万行(闭源) | 数万行 Python | 约 600 行(两个文件) |
| 代码量 | 引擎约 1667 行 / 整包 3727 行 | 几十万行(闭源) | 数万行 Python | 约 600 行(两个文件) |
| 读完要多久 | 一个下午 | 读不了(闭源) | 得啃几天 | 一个下午 |
| 能不能下断点改了再跑 | 能,每一行 | 不能 | 能,但量大 | 能 |
| 定位 | 读懂并 fork 出你自己的 agent | 生产级编程助手 | 终端结对编程 | 教学用最小 GPT |
Expand All @@ -36,7 +36,7 @@ nanoGPT 那一列是拿来对照的:它最小、可读,但教的是训一个

我一直觉得 coding agent 被讲得太玄了。把 Claude Code、Cursor 这类工具扒到底,核心是一个 while 循环套着一个大模型,外加七八个让它能真正动手的工具。难的从来不是这个循环,而是循环跑进真实世界以后要兜的那些底。CoreCoder 就是把这个核心老老实实写出来的最小版本。

引擎部分(循环、模型接口、上下文、工具、会话)去掉空行和注释是 1655 行。连最外层的 CLI、配置、打包一起算,整个包 26 个文件、物理 3707 行、净 3101 行,每个文件都短到能一口气读完。自 1161 行快照之后的增长都花在了看得见的功能上:plan mode、hooks、checkpoints,下文各有交代。
引擎部分(循环、模型接口、上下文、工具、会话)去掉空行和注释是 1667 行。连最外层的 CLI、配置、打包一起算,整个包 26 个文件、物理 3727 行、净 3119 行,每个文件都短到能一口气读完。自 1161 行快照之后的增长都花在了看得见的功能上:plan mode、hooks、checkpoints,下文各有交代。

它真能跑:读写文件、执行 shell、派子 agent、分三层压上下文,还能随时把这趟烧掉的 token 和美元数报给你。任何要动你磁盘、要跑命令的调用,都会先停下来等你点头。离线测试在 Linux、macOS 和 Windows 上都会跑,真实 API 冒烟则按需开启。但能跑不是为了劝你拿去日用,而是为了让这份「注释」不撒谎:一个解释 agent 怎么运作的范例,自己得真能运作。

Expand Down Expand Up @@ -94,14 +94,14 @@ corecoder/
├── agent.py agent 主循环 + 并行工具执行 240 行 ← 从这里开始读
├── llm.py 流式客户端 + 重试 + 成本统计 407 行
├── context.py 三层上下文压缩 265 行
├── session.py 会话存盘 / 续聊 + 路径穿越防护 178 行
├── session.py 会话存盘 / 续聊 / 导出 191 行
├── permissions.py 改动类工具的用户授权 81 行
├── hooks.py 工具调用前后的用户 shell 钩子 87 行
├── shell.py POSIX shell 路由(Windows 走 Git Bash) 61 行
├── mcp.py MCP 工具、文本资源和提示模板 413 行
├── mcp_http.py 远程 Streamable HTTP 连接 229 行
├── prompt.py 系统提示词 41 行
├── cli.py REPL + 斜杠命令 + 一次性模式 472 行
├── cli.py REPL + 斜杠命令 + 一次性模式 486 行
├── config.py 环境变量配置 61 行
├── checkpoints.py /undo 快照与回滚 125 行
├── demo.py 离线端到端演示 101 行
Expand Down Expand Up @@ -237,6 +237,8 @@ quit / exit 退出(Ctrl+C 取消当前回合)

脚本里用 `corecoder --list-sessions "发布检查" --json`。标准输出是包含 `id`、`model`、`saved_at` 和 `preview` 的数组;没有匹配就输出 `[]`。搜索范围、预览和最多 20 条的限制不变。

想复盘对话但不想继续运行时,用 `corecoder --export-session ID > dialogue.md` 导出 UTF-8 Markdown。它按原序保留存档里仍有的用户和助手正文,不包括内部摘要和结构化工具数据,不修改存档,也不用 API key。已经被压缩掉的回合不会恢复,正文也不会自动脱敏。

如果最后一次的方案走偏了,可以在那条请求之前另存一份,再换个问法,不把第一次的回答带给模型。比如原档显示 `Retained turns: 3`:

```bash
Expand Down
2 changes: 1 addition & 1 deletion article/00-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

因为 Claude Code 本体太大了。它的代码量在几十万行的量级,光一个负责跑 shell 命令的 BashTool 就上千行,真要逐行读完,多数人会在第三个文件就关掉编辑器。可 agent 的核心其实没那么复杂,复杂的是工程化之后那些为真实世界兜底的边角:模型半路被打断怎么办、上下文塞满了怎么办、十个工具调用同时回来怎么办、provider 偶尔抽风返回 500 又怎么办。这些东西摊在几十万行里,你很难一眼看到骨架。

CoreCoder 做的事,是把这套骨架压到一千六百行出头的纯 Python。准确说,agent 引擎(循环、模型接口、上下文、工具、会话)去掉空行和注释是 1650 行;连最外层的 CLI 终端、配置和打包都算上,整个包去掉空行和注释 3082 行,物理代码 3688 行。每个文件都短到能一口气读完,跑起来又确实是个能改代码、能跑命令、能自己开子任务、能压缩上下文、能算钱的 agent。它不是玩具。它是把生产级 agent 的每个设计决策,拣出最核心的那版,用最少的代码诚实地写一遍。
CoreCoder 做的事,是把这套骨架压到一千六百行出头的纯 Python。准确说,agent 引擎(循环、模型接口、上下文、工具、会话)去掉空行和注释是 1667 行;连最外层的 CLI 终端、配置和打包都算上,整个包去掉空行和注释 3119 行,物理代码 3727 行。每个文件都短到能一口气读完,跑起来又确实是个能改代码、能跑命令、能自己开子任务、能压缩上下文、能算钱的 agent。它不是玩具。它是把生产级 agent 的每个设计决策,拣出最核心的那版,用最少的代码诚实地写一遍。

读它,约等于读一份 Claude Code 的「可运行注释版」。区别在于,CoreCoder 的每一行你都能在自己机器上断点、改、跑、看它出什么效果。本系列里我引用的每一个行数、每一段代码,都是从仓库里现读现核的,不是凭印象。这点我后面会反复较真,因为编码 agent 这个领域,太多文章在凭感觉编数字。

Expand Down
2 changes: 1 addition & 1 deletion article/00-index_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ First, use CoreCoder, an open source project whose core runs to just over sixtee

Because Claude Code itself is too big. Its codebase runs into the hundreds of thousands of lines, and the BashTool alone, the part that runs shell commands, is over a thousand. Read it line by line and most people close the editor by the third file. Yet the core of an agent isn't really that complicated. What's complicated is everything engineered around it to survive the real world: what to do when the model gets interrupted mid-stream, when the context window fills up, when ten tool calls come back at once, when a provider hiccups and returns a 500. Spread across hundreds of thousands of lines, that skeleton is hard to see.

What CoreCoder does is squeeze the skeleton down to just over sixteen hundred lines of pure Python. To be precise, the agent engine (the loop, model interface, context, tools, session) is 1,650 lines once you drop blank lines and comments; add the outermost CLI terminal, config, and packaging and the whole package is 3,082 lines without blanks and comments, 3,688 physical. Every file is short enough to read in one sitting, and what runs is genuinely an agent: it edits code, runs commands, spins up its own subtasks, compresses context, tracks cost. It is not a toy. It takes every design decision a production agent makes, picks the most essential version of each, and writes it out honestly in the least code it can.
What CoreCoder does is squeeze the skeleton down to just over sixteen hundred lines of pure Python. To be precise, the agent engine (the loop, model interface, context, tools, session) is 1,667 lines once you drop blank lines and comments; add the outermost CLI terminal, config, and packaging and the whole package is 3,119 lines without blanks and comments, 3,727 physical. Every file is short enough to read in one sitting, and what runs is genuinely an agent: it edits code, runs commands, spins up its own subtasks, compresses context, tracks cost. It is not a toy. It takes every design decision a production agent makes, picks the most essential version of each, and writes it out honestly in the least code it can.

Reading it is close to reading a runnable, annotated edition of Claude Code. The difference is that every line of CoreCoder is something you can breakpoint, change, run, and watch on your own machine. Every line count and every snippet I quote in this series is read straight out of the repository, not recalled from memory. I'll keep being pedantic about that, because in the coding-agent space far too many write-ups just make the numbers up.

Expand Down
2 changes: 1 addition & 1 deletion article/06-session-and-cli.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 把它跑成一个真正的命令行工具

前五篇拆的是 agent 的内脏:循环、工具、模型接口、上下文压缩、并发与子 agent。这些零件再漂亮,你也没法直接用,因为还缺一层皮,一个让人能坐下来跟它对话、能存盘、能续聊、能查状态的命令行界面。这一篇讲这层皮,对应 `cli.py`(479 行)和 `session.py`(178 行)。
前五篇拆的是 agent 的内脏:循环、工具、模型接口、上下文压缩、并发与子 agent。这些零件再漂亮,你也没法直接用,因为还缺一层皮,一个让人能坐下来跟它对话、能存盘、能续聊、能查状态的命令行界面。这一篇讲这层皮,对应 `cli.py`(486 行)和 `session.py`(191 行)。

它不光是「锦上添花的 UI」。这层皮里藏着一个很值得讲的安全细节,我们留到后面压轴。

Expand Down
2 changes: 1 addition & 1 deletion article/06-session-and-cli_EN.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Turning it into a real command-line tool

The first five pieces dissected the agent's internals: the loop, the tools, the model interface, context compression, parallelism and sub-agents. As lovely as these parts are, you can't use them directly, because one more layer is missing, a skin, a command-line interface where someone can sit down and talk to it, save, resume, and check status. This piece covers that skin, corresponding to `cli.py` (479 lines) and `session.py` (173 lines).
The first five pieces dissected the agent's internals: the loop, the tools, the model interface, context compression, parallelism and sub-agents. As lovely as these parts are, you can't use them directly, because one more layer is missing, a skin, a command-line interface where someone can sit down and talk to it, save, resume, and check status. This piece covers that skin, corresponding to `cli.py` (486 lines) and `session.py` (191 lines).

It's not just "a nice-to-have UI." Hidden in this skin is a security detail well worth discussing, which we save for the finale.

Expand Down
2 changes: 1 addition & 1 deletion article/07-build-your-own.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ CoreCoder 是个起点,不是终点。它故意留白了不少地方,每一

如果这个系列只让你记住一件事,我希望是这个:编码 agent 没有想象中那么神秘,它就是一摞你完全够得着的工程决定堆起来的。

往回看这六篇,它其实就几块东西。一个有上限的循环(第一篇),外加一组接口干净的工具让它能动手(第二篇)。模型接口是薄薄一层 provider 包装(第三篇),上下文靠分层压缩对抗遗忘(第四篇)。它能分身、能并发,靠的是对共享状态的克制(第五篇);最外面那层 CLI 会出事件、管呈现,还顺手堵了路径穿越那个口子(第六篇)。引擎加起来一千六百行出头,连这层 CLI 外壳算上整个包也才 3688 行,没有一处是你看不懂的。
往回看这六篇,它其实就几块东西。一个有上限的循环(第一篇),外加一组接口干净的工具让它能动手(第二篇)。模型接口是薄薄一层 provider 包装(第三篇),上下文靠分层压缩对抗遗忘(第四篇)。它能分身、能并发,靠的是对共享状态的克制(第五篇);最外面那层 CLI 会出事件、管呈现,还顺手堵了路径穿越那个口子(第六篇)。引擎加起来一千六百行出头,连这层 CLI 外壳算上整个包也才 3727 行,没有一处是你看不懂的。

现在你不光看懂了,你还 fork 了它,接了自己的模型,加了自己的工具,调了自己的脾气。它是你的了。

Expand Down
Loading
Loading