claude-coder 1.0.0

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.
package/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # Claude Coder
2
+
3
+ **中文** | [English](docs/README.en.md)
4
+
5
+ 受 [Anthropic: Effective harnesses for long-running agents](https://www.anthropic.com/engineering/effective-harnesses-for-long-running-agents) 启发,Claude Coder 是一个**自主编码 harness**,依托 Claude Agent SDK 的 `query()` 接口,提供项目扫描、任务分解、多 session 编排、自动校验与 git 回滚的能力,支持一句话需求或 `requirements.md` 驱动,兼容所有 Anthropic API 兼容的模型提供商。
6
+
7
+ **核心思路**:AI Agent 单次会话上下文有限,大型需求容易丢失进度或产出不可用代码。本工具将 Agent 包装为一个**可靠的、可重试的函数** — harness 管理任务状态、校验每次产出、失败时自动回滚,Agent 只需专注于编码。
8
+
9
+ ---
10
+
11
+ ## 快速上手
12
+
13
+ ```bash
14
+ # 前置:安装 Claude Agent SDK
15
+ npm install -g @anthropic-ai/claude-agent-sdk
16
+
17
+ # 安装
18
+ npm install -g claude-coder
19
+
20
+ # 配置模型
21
+ claude-coder setup
22
+
23
+ # 开始自动编码
24
+ cd your-project
25
+ claude-coder run "实现用户注册和登录功能"
26
+ ```
27
+
28
+ ## 工作原理
29
+
30
+ ```
31
+ 需求输入 ─→ 项目扫描 ─→ 任务分解 ─→ 编码循环
32
+
33
+ ┌──────┴──────┐
34
+ │ Session N │
35
+ │ Claude SDK │
36
+ │ 6 步流程 │
37
+ └──────┬──────┘
38
+
39
+ harness 校验
40
+
41
+ 通过 → 下一个任务
42
+ 失败 → git 回滚 + 重试
43
+ ```
44
+
45
+ 每个 session 内,Agent 自主执行 6 步:恢复上下文 → 环境检查 → 选任务 → 编码 → 测试 → 收尾(git commit)。
46
+
47
+ ## 命令
48
+
49
+ | 命令 | 说明 |
50
+ |------|------|
51
+ | `claude-coder setup` | 交互式模型配置 |
52
+ | `claude-coder run [需求]` | 自动编码循环 |
53
+ | `claude-coder run --dry-run` | 预览模式 |
54
+ | `claude-coder init` | 初始化项目环境 |
55
+ | `claude-coder view [需求]` | 观测模式(交互式单次) |
56
+ | `claude-coder add "指令"` | 追加任务 |
57
+ | `claude-coder validate` | 手动校验 |
58
+ | `claude-coder status` | 查看进度和成本 |
59
+ | `claude-coder config sync` | 同步配置到 ~/.claude/ |
60
+
61
+ **选项**:`--max N` 限制 session 数(默认 50),`--pause N` 每 N 个 session 暂停(默认 5)。
62
+
63
+ ## 使用场景
64
+
65
+ **新项目**:`claude-coder run "用 Express + React 做 Todo 应用"` — 自动搭建脚手架、分解任务、逐个实现。
66
+
67
+ **已有项目**:`claude-coder run "新增头像上传功能"` — 先扫描现有代码和技术栈,再增量开发。
68
+
69
+ **需求文档驱动**:在项目根目录创建 `requirements.md`(可从模板复制),运行 `claude-coder run` — 修改需求后再次运行,自动同步新任务。
70
+
71
+ **追加任务**:`claude-coder add "新增管理员后台"` — 仅追加到任务列表,下次 run 时执行。
72
+
73
+ ## 模型支持
74
+
75
+ | 提供商 | 说明 |
76
+ |--------|------|
77
+ | Claude 官方 | 默认,Anthropic 原版 API |
78
+ | GLM (智谱/Z.AI) | GLM 4.7 / GLM 5 |
79
+ | 阿里云百炼 | qwen3-coder-plus / glm-5 |
80
+ | DeepSeek | deepseek-chat / reasoner |
81
+ | 自定义 | 任何 Anthropic 兼容 API |
82
+
83
+ ## 项目结构
84
+
85
+ ```
86
+ your-project/
87
+ .claude-coder/ # 运行时数据(gitignored)
88
+ .env # 模型配置
89
+ project_profile.json # 项目扫描结果
90
+ tasks.json # 任务列表 + 状态
91
+ session_result.json # session 结果 + 历史
92
+ progress.json # 会话日志 + 成本
93
+ tests.json # 验证记录
94
+ .runtime/ # 临时文件
95
+ requirements.md # 需求文档(可选)
96
+ ```
97
+
98
+ ## 常见问题
99
+
100
+ **"Credit balance is too low"**:运行 `claude-coder setup` 重新配置 API Key。
101
+
102
+ **中断恢复**:直接重新运行 `claude-coder run`,会从上次中断处继续。
103
+
104
+ **跳过任务**:将 `.claude-coder/tasks.json` 中该任务的 `status` 改为 `done`。
105
+
106
+ **Windows 支持**:完全支持,纯 Node.js 实现。
107
+
108
+ ## 文档
109
+
110
+ - [技术架构](docs/ARCHITECTURE.md) — 模块职责、提示语注入架构、注意力机制、Hook 数据流、后续优化方向
111
+
112
+ ## License
113
+
114
+ MIT
package/bin/cli.js ADDED
@@ -0,0 +1,140 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const pkg = require('../package.json');
5
+
6
+ const COMMANDS = {
7
+ run: { desc: '自动编码循环', usage: 'claude-coder run [需求] [--max N] [--pause N] [--dry-run]' },
8
+ setup: { desc: '交互式模型配置', usage: 'claude-coder setup' },
9
+ init: { desc: '初始化项目环境', usage: 'claude-coder init' },
10
+ view: { desc: '观测模式(交互式单次)', usage: 'claude-coder view [需求]' },
11
+ add: { desc: '追加任务到 tasks.json', usage: 'claude-coder add "指令"' },
12
+ validate: { desc: '手动校验上次 session', usage: 'claude-coder validate' },
13
+ status: { desc: '查看任务进度和成本', usage: 'claude-coder status' },
14
+ config: { desc: '配置管理', usage: 'claude-coder config sync' },
15
+ };
16
+
17
+ function showHelp() {
18
+ console.log(`\nClaude Coder v${pkg.version}\n`);
19
+ console.log('用法: claude-coder <command> [options]\n');
20
+ console.log('命令:');
21
+ for (const [name, info] of Object.entries(COMMANDS)) {
22
+ console.log(` ${name.padEnd(10)} ${info.desc}`);
23
+ }
24
+ console.log('\n示例:');
25
+ console.log(' claude-coder setup 配置模型和 API Key');
26
+ console.log(' claude-coder run "实现用户登录" 开始自动编码');
27
+ console.log(' claude-coder run --max 5 --dry-run 预览模式');
28
+ console.log(' claude-coder status 查看进度和成本');
29
+ console.log(`\n前置条件: npm install -g @anthropic-ai/claude-agent-sdk`);
30
+ }
31
+
32
+ function parseArgs(argv) {
33
+ const args = argv.slice(2);
34
+ const command = args[0];
35
+ const opts = { max: 50, pause: 5, dryRun: false };
36
+ const positional = [];
37
+
38
+ for (let i = 1; i < args.length; i++) {
39
+ switch (args[i]) {
40
+ case '--max':
41
+ opts.max = parseInt(args[++i], 10) || 50;
42
+ break;
43
+ case '--pause':
44
+ opts.pause = parseInt(args[++i], 10) || 5;
45
+ break;
46
+ case '--dry-run':
47
+ opts.dryRun = true;
48
+ break;
49
+ case '--help':
50
+ case '-h':
51
+ showHelp();
52
+ process.exit(0);
53
+ break;
54
+ default:
55
+ if (!args[i].startsWith('--')) {
56
+ positional.push(args[i]);
57
+ }
58
+ break;
59
+ }
60
+ }
61
+
62
+ return { command, positional, opts };
63
+ }
64
+
65
+ async function main() {
66
+ const { command, positional, opts } = parseArgs(process.argv);
67
+
68
+ if (!command || command === '--help' || command === '-h') {
69
+ showHelp();
70
+ process.exit(0);
71
+ }
72
+
73
+ if (command === '--version' || command === '-v') {
74
+ console.log(pkg.version);
75
+ process.exit(0);
76
+ }
77
+
78
+ switch (command) {
79
+ case 'run': {
80
+ const runner = require('../src/runner');
81
+ await runner.run(positional[0] || null, opts);
82
+ break;
83
+ }
84
+ case 'setup': {
85
+ const setup = require('../src/setup');
86
+ await setup.setup();
87
+ break;
88
+ }
89
+ case 'init': {
90
+ const { init } = require('../src/init');
91
+ await init();
92
+ break;
93
+ }
94
+ case 'view': {
95
+ const runner = require('../src/runner');
96
+ await runner.view(positional[0] || null, opts);
97
+ break;
98
+ }
99
+ case 'add': {
100
+ if (!positional[0]) {
101
+ console.error('用法: claude-coder add "任务描述"');
102
+ process.exit(1);
103
+ }
104
+ const runner = require('../src/runner');
105
+ await runner.add(positional[0], opts);
106
+ break;
107
+ }
108
+ case 'validate': {
109
+ const validator = require('../src/validator');
110
+ const result = await validator.validate();
111
+ process.exit(result.fatal ? 1 : 0);
112
+ break;
113
+ }
114
+ case 'status': {
115
+ const tasks = require('../src/tasks');
116
+ tasks.showStatus();
117
+ break;
118
+ }
119
+ case 'config': {
120
+ const config = require('../src/config');
121
+ if (positional[0] === 'sync') {
122
+ config.syncToGlobal();
123
+ } else {
124
+ console.error('用法: claude-coder config sync');
125
+ process.exit(1);
126
+ }
127
+ break;
128
+ }
129
+ default:
130
+ console.error(`未知命令: ${command}`);
131
+ showHelp();
132
+ process.exit(1);
133
+ }
134
+ }
135
+
136
+ main().catch(err => {
137
+ console.error(`\n错误: ${err.message}`);
138
+ if (process.env.DEBUG) console.error(err.stack);
139
+ process.exit(1);
140
+ });
@@ -0,0 +1,319 @@
1
+ # Claude Coder — 技术架构文档
2
+
3
+ > 本文件面向开发者和 AI,用于快速理解本工具的设计、文件结构、提示语架构和扩展方向。
4
+
5
+ ---
6
+
7
+ ## 一句话定位
8
+
9
+ 一个基于 Claude Agent SDK 的**自主编码 harness**:自动扫描项目 → 拆解任务 → 逐个实现 → 校验 → 回滚/重试 → 推送,全程无需人工干预。
10
+
11
+ ---
12
+
13
+ ## 1. 核心架构
14
+
15
+ ```mermaid
16
+ flowchart TB
17
+ subgraph Harness["bin/cli.js → src/runner.js (Harness 主控)"]
18
+ direction TB
19
+ scan["scanner.scan()<br/>首次扫描"]
20
+ coding["session.runCodingSession()<br/>编码循环"]
21
+ validate["validator.validate()<br/>校验"]
22
+ indicator["Indicator 类<br/>setInterval 500ms 刷新"]
23
+ end
24
+
25
+ subgraph SDK["Claude Agent SDK"]
26
+ query["query() 函数"]
27
+ hook_sys["PreToolUse hook<br/>内联回调"]
28
+ end
29
+
30
+ subgraph Files["文件系统 (.claude-coder/)"]
31
+ direction TB
32
+ profile["project_profile.json<br/>tasks.json"]
33
+ runtime["session_result.json<br/>progress.json"]
34
+ phase[".runtime/<br/>phase / step / activity.log"]
35
+ end
36
+
37
+ scan -->|"systemPrompt =<br/>CLAUDE.md + SCAN_PROTOCOL.md"| query
38
+ coding -->|"systemPrompt = CLAUDE.md"| query
39
+
40
+ query -->|PreToolUse 事件| hook_sys
41
+ hook_sys -->|inferPhaseStep()| indicator
42
+
43
+ query -->|Agent 工具调用| Files
44
+ validate -->|读取| runtime
45
+ validate -->|"pass → 下一 session<br/>fail → rollback"| coding
46
+ ```
47
+
48
+ **核心特征:**
49
+ - **项目无关**:项目信息由 Agent 扫描后存入 `project_profile.json`,harness 不含项目特定逻辑
50
+ - **可恢复**:通过 `session_result.json` 跨会话记忆,任意 session 可断点续跑
51
+ - **可观测**:SDK 内联 `PreToolUse` hook 实时显示 Agent 当前步骤和工具调用
52
+ - **跨平台**:纯 Node.js 实现,macOS / Linux / Windows 通用
53
+ - **零依赖**:`dependencies` 为空,Claude Agent SDK 作为 peerDependency
54
+
55
+ ---
56
+
57
+ ## 2. 执行流程
58
+
59
+ ```mermaid
60
+ flowchart LR
61
+ start(["claude-coder run ..."]) --> mode{模式?}
62
+
63
+ mode -->|view| view["runViewSession()"]
64
+ view --> exit_view([exit 0])
65
+
66
+ mode -->|"add 指令"| add["runAddSession()"]
67
+ add --> exit_add([exit 0])
68
+
69
+ mode -->|run| check{profile<br/>存在?}
70
+
71
+ check -->|否| req["读取<br/>requirements.md"]
72
+ req --> scan["scanner.scan()"]
73
+ scan --> profile_out["生成<br/>profile + tasks.json"]
74
+ profile_out --> loop
75
+
76
+ check -->|是| loop
77
+
78
+ loop["编码循环"] --> session["runCodingSession(N)"]
79
+ session --> val["validator.validate()"]
80
+ val -->|pass| push["git push"]
81
+ push --> done_check{所有任务<br/>done?}
82
+ done_check -->|否| pause_check{每N个session<br/>暂停?}
83
+ pause_check -->|继续| session
84
+ done_check -->|是| finish([完成])
85
+
86
+ val -->|fail| rollback["git reset --hard"]
87
+ rollback --> retry_check{连续失败<br/>≥3次?}
88
+ retry_check -->|否| session
89
+ retry_check -->|是| mark_failed["标记 task failed"]
90
+ mark_failed --> session
91
+ ```
92
+
93
+ ---
94
+
95
+ ## 3. 模块职责
96
+
97
+ ```
98
+ bin/cli.js CLI 入口:参数解析、命令路由、SDK peerDep 检查
99
+ src/
100
+ config.js 配置管理:.env 加载、模型映射、环境变量构建、全局同步
101
+ runner.js 主循环:scan → session → validate → retry/rollback
102
+ session.js SDK 交互:query() 调用、hook 绑定、日志流
103
+ prompts.js 提示语构建:系统 prompt 组合 + 条件 hint + 任务分解指导
104
+ init.js 环境初始化:读取 profile 执行依赖安装、服务启动、健康检查
105
+ scanner.js 初始化扫描:调用 runScanSession + 重试
106
+ validator.js 校验引擎:session_result 结构校验 + git 检查 + 测试覆盖
107
+ tasks.js 任务管理:CRUD + 状态机 + 进度展示
108
+ indicator.js 进度指示:终端 spinner + phase/step 文件写入
109
+ setup.js 交互式配置:模型选择、API Key、MCP 工具
110
+ templates/
111
+ CLAUDE.md Agent 协议(注入为 systemPrompt)
112
+ SCAN_PROTOCOL.md 首次扫描协议(与 CLAUDE.md 拼接注入)
113
+ requirements.example.md 需求文件模板
114
+ ```
115
+
116
+ ---
117
+
118
+ ## 4. 文件清单
119
+
120
+ ### npm 包分发内容
121
+
122
+ | 文件 | 用途 |
123
+ |------|------|
124
+ | `bin/cli.js` | CLI 入口 |
125
+ | `src/config.js` | .env 加载、模型映射 |
126
+ | `src/runner.js` | Harness 主循环 |
127
+ | `src/session.js` | SDK query() 封装 + hook |
128
+ | `src/prompts.js` | 提示语构建(系统 prompt + 条件 hint + 任务分解指导) |
129
+ | `src/init.js` | 环境初始化(依赖安装、服务启动) |
130
+ | `src/scanner.js` | 项目初始化扫描 |
131
+ | `src/validator.js` | 校验引擎 |
132
+ | `src/tasks.js` | 任务 CRUD + 状态机 |
133
+ | `src/indicator.js` | 终端进度指示器 |
134
+ | `src/setup.js` | 交互式配置向导 |
135
+ | `templates/CLAUDE.md` | Agent 协议 |
136
+ | `templates/SCAN_PROTOCOL.md` | 首次扫描协议 |
137
+
138
+ ### 用户项目运行时数据(.claude-coder/)
139
+
140
+ | 文件 | 生成时机 | 用途 |
141
+ |------|----------|------|
142
+ | `.env` | `claude-coder setup` | 模型配置 + API Key(gitignored) |
143
+ | `project_profile.json` | 首次扫描 | 项目元数据 |
144
+ | `tasks.json` | 首次扫描 | 任务列表 + 状态跟踪 |
145
+ | `progress.json` | 每次 session 结束 | 结构化会话日志 + 成本记录 |
146
+ | `session_result.json` | 每次 session 结束 | 当前 + 历史 session 结果 |
147
+ | `tests.json` | 首次测试时 | 验证记录(防止反复测试) |
148
+ | `.runtime/` | 运行时 | 临时文件(phase、step、activity.log、logs/) |
149
+
150
+ ---
151
+
152
+ ## 5. Prompt 注入架构
153
+
154
+ ### 架构图
155
+
156
+ ```mermaid
157
+ flowchart TB
158
+ subgraph Templates["templates/ (静态模板)"]
159
+ claude_md["CLAUDE.md<br/>Agent 协议 ~255 行"]
160
+ scan_md["SCAN_PROTOCOL.md<br/>扫描协议 ~133 行"]
161
+ end
162
+
163
+ subgraph Prompts["src/prompts.js (动态构建)"]
164
+ sys_p["buildSystemPrompt()<br/>组合系统提示"]
165
+ coding_p["buildCodingPrompt()<br/>编码 session prompt"]
166
+ task_g["buildTaskGuide()<br/>任务分解指导"]
167
+ scan_p["buildScanPrompt()<br/>扫描 session prompt"]
168
+ view_p["buildViewPrompt()"]
169
+ add_p["buildAddPrompt()"]
170
+ end
171
+
172
+ subgraph Session["src/session.js (SDK 调用)"]
173
+ query["SDK query()<br/>systemPrompt + prompt"]
174
+ end
175
+
176
+ claude_md --> sys_p
177
+ scan_md --> sys_p
178
+ sys_p --> query
179
+ coding_p --> query
180
+ task_g --> scan_p
181
+ task_g --> add_p
182
+ scan_p --> query
183
+ view_p --> query
184
+ add_p --> query
185
+ ```
186
+
187
+ ### Session 类型与注入内容
188
+
189
+ | Session 类型 | systemPrompt | user prompt | 触发条件 |
190
+ |---|---|---|---|
191
+ | **编码** | CLAUDE.md | `buildCodingPrompt()` + 6 个条件 hint | 主循环每次迭代 |
192
+ | **扫描** | CLAUDE.md + SCAN_PROTOCOL.md | `buildScanPrompt()` + 任务分解指导 | 首次运行 |
193
+ | **观测** | CLAUDE.md (± SCAN_PROTOCOL.md) | `buildViewPrompt()` | `claude-coder view` |
194
+ | **追加** | CLAUDE.md | `buildAddPrompt()` + 任务分解指导 | `claude-coder add` |
195
+
196
+ ### 编码 Session 的 6 个条件 Hint
197
+
198
+ | Hint | 触发条件 | 影响 |
199
+ |---|---|---|
200
+ | `reqSyncHint` | 需求 hash 变化 | Step 1:追加新任务 |
201
+ | `mcpHint` | MCP_PLAYWRIGHT=true | Step 5:可用 Playwright |
202
+ | `testHint` | tests.json 有记录 | Step 5:避免重复验证 |
203
+ | `docsHint` | profile.existing_docs 非空 | Step 4:读文档后再编码,完成后更新文档 |
204
+ | `envHint` | 连续成功且 session>1 | Step 2:跳过 init |
205
+ | `retryContext` | 上次校验失败 | 全局:避免同样错误 |
206
+
207
+ ---
208
+
209
+ ## 6. 注意力机制与设计决策
210
+
211
+ ### U 型注意力优化
212
+
213
+ CLAUDE.md 的内容按 LLM 注意力 U 型曲线排列:
214
+
215
+ ```
216
+ 顶部 (primacy zone) → 铁律(约束规则) → 最高遵循率
217
+ 中部 (低注意力区) → 参考数据(文件格式等) → 按需查阅
218
+ 底部 (recency zone) → 6 步工作流(行动指令) → 最高行为合规率
219
+ ```
220
+
221
+ ### 关键设计决策
222
+
223
+ | 决策 | 理由 |
224
+ |------|------|
225
+ | **静态规则 vs 动态上下文分离** | CLAUDE.md 是"宪法"(低频修改),hints 依赖运行时状态(动态生成) |
226
+ | **扫描协议单独文件** | 仅首次注入,编码 session 不需要,节省 ~2000 token |
227
+ | **任务分解指导在 user prompt** | 从系统 prompt 中部(低注意力)迁移到 user prompt(recency zone),提升遵循率 |
228
+ | **docsHint 动态注入** | 当 profile.existing_docs 非空时,在 user prompt 提醒 Agent 读文档再编码。CLAUDE.md Step 4 有静态指令,docsHint 在 recency zone 强化 |
229
+ | **tests.json 保留 last_run_session** | Agent 判断是否需要重新验证的依据(代码可能在中间 session 被修改) |
230
+ | **prompts.js 集中管理** | 所有 prompt 文本一处可见,与 session.js 的 SDK 交互职责分离 |
231
+
232
+ ### 学术依据
233
+
234
+ | 优化项 | 理论来源 |
235
+ |---|---|
236
+ | U 型注意力布局 | Anthropic Context Engineering |
237
+ | DAG 依赖约束 | ACONIC (2025, arXiv 2510.07772) |
238
+ | 反面案例排除 | SCoT (2023) + Expert Context Framework |
239
+ | scan/add 复用 taskGuide | User Story Decomposition (SSRN 2025) |
240
+
241
+ ---
242
+
243
+ ## 7. Hook 数据流
244
+
245
+ SDK 的 hooks 是**进程内回调**(非独立进程),零延迟、无 I/O 开销:
246
+
247
+ ```mermaid
248
+ sequenceDiagram
249
+ participant SDK as Claude Agent SDK
250
+ participant Hook as inferPhaseStep()
251
+ participant Ind as Indicator (setInterval)
252
+ participant Term as 终端
253
+
254
+ SDK->>Hook: PreToolUse 回调<br/>{tool_name: "Edit", tool_input: {path: "src/app.tsx"}}
255
+ Hook->>Hook: 推断阶段: Edit → coding
256
+ Hook->>Ind: updatePhase("coding")
257
+ Hook->>Ind: appendActivity("Edit", "src/app.tsx")
258
+
259
+ Note over SDK,Hook: 同步回调,return {decision: "allow"}
260
+
261
+ loop 每 500ms
262
+ Ind->>Term: ⠋ [Session 3] 编码中 02:15 | Git 操作
263
+ end
264
+ ```
265
+
266
+ ---
267
+
268
+ ## 8. 评分
269
+
270
+ | 维度 | 评分 | 说明 |
271
+ |------|------|------|
272
+ | **CLAUDE.md 系统提示** | 8/10 | U 型注意力设计;铁律清晰;状态机和 6 步流程是核心竞争力 |
273
+ | **动态 prompt** | 8/10 | 5 个条件 hint 精准注入,不浪费 token |
274
+ | **SCAN_PROTOCOL.md** | 8.5/10 | 新旧项目分支完整,profile 格式全面 |
275
+ | **tests.json 设计** | 7.5/10 | 精简字段,核心目的(防反复测试)明确 |
276
+ | **注入时机** | 9/10 | 静态规则 vs 动态上下文分离干净 |
277
+ | **整体架构** | 8/10 | 文件组织清晰,prompts.js 分离提升可维护性 |
278
+
279
+ ---
280
+
281
+ ## 9. 后续优化方向
282
+
283
+ ### P0 — 近期
284
+
285
+ | 方向 | 说明 |
286
+ |------|------|
287
+ | **文件保护 Deny-list** | PreToolUse hook 拦截对保护文件的写入(比文字规则更硬性) |
288
+ | **TUI 终端监控** | 基于 ANSI 的全屏界面,替代单行 spinner |
289
+ | **成本预算控制** | `.env` 新增 `MAX_COST_USD`,超预算自动停止 |
290
+
291
+ ### P1 — 中期
292
+
293
+ | 方向 | 说明 |
294
+ |------|------|
295
+ | **TCR 纪律** | Test && Commit \|\| Revert,可配置 strict/smart/off |
296
+ | **配置分层** | defaults.env → .env → .env.local 三层合并 |
297
+ | **Reminders 注入** | 用户自定义提醒文件,拼接到编码 prompt |
298
+ | **MCP 工具自动检测** | `claude mcp list` 自动启用已安装工具 |
299
+
300
+ ### P2 — 远期
301
+
302
+ | 方向 | 说明 |
303
+ |------|------|
304
+ | **Web UI 监控** | 可选插件包 `@claude-coder/web-ui` |
305
+ | **PR/CI 集成** | Session 完成后自动创建 PR、监控 CI |
306
+ | **Prompt A/B 测试** | 多版本 CLAUDE.md 并行对比效果 |
307
+ | **并行 Worktree** | 多任务在不同 git worktree 中并行执行 |
308
+
309
+ ---
310
+
311
+ ## 设计原则
312
+
313
+ 1. **SDK 原生集成**:通过 `query()` 调用 Claude,内联 hooks,原生 cost tracking
314
+ 2. **零硬依赖**:Claude Agent SDK 作为 peerDependency
315
+ 3. **Agent 自治**:Agent 通过 CLAUDE.md 协议自主决策,harness 只负责调度和校验
316
+ 4. **幂等设计**:所有入口可重复执行,不产生副作用
317
+ 5. **跨平台**:纯 Node.js + `child_process` 调用 git,无平台特定脚本
318
+ 6. **运行时隔离**:每个项目的 `.claude-coder/` 独立,不同项目互不干扰
319
+ 7. **Prompt 架构分离**:静态规则在 `templates/`,动态上下文在 `src/prompts.js`
@@ -0,0 +1,94 @@
1
+ # Claude Coder
2
+
3
+ [中文](../README.md) | **English**
4
+
5
+ Inspired by [Anthropic: Effective harnesses for long-running agents](https://www.anthropic.com/engineering/effective-harnesses-for-long-running-agents), Claude Coder is an **autonomous coding harness** built on the Claude Agent SDK's `query()` interface. It provides project scanning, task decomposition, multi-session orchestration, automatic validation, and git rollback — driven by a one-liner requirement or a `requirements.md` file, compatible with all Anthropic API-compatible model providers.
6
+
7
+ **Core idea**: A single AI session has limited context. For large requirements, agents tend to lose progress or produce broken code. This tool wraps the agent in a **reliable, retryable function** — the harness manages task state, validates every output, and auto-rolls back on failure. The agent just focuses on coding.
8
+
9
+ ---
10
+
11
+ ## Quick Start
12
+
13
+ ```bash
14
+ # Prerequisites: Install Claude Agent SDK
15
+ npm install -g @anthropic-ai/claude-agent-sdk
16
+
17
+ # Install
18
+ npm install -g claude-coder
19
+
20
+ # Configure model
21
+ claude-coder setup
22
+
23
+ # Start auto-coding
24
+ cd your-project
25
+ claude-coder run "Implement user registration and login"
26
+ ```
27
+
28
+ ## How It Works
29
+
30
+ ```
31
+ Requirement ─→ Project scan ─→ Task decomposition ─→ Coding loop
32
+
33
+ ┌─────┴─────┐
34
+ │ Session N │
35
+ │ Claude SDK │
36
+ │ 6-step flow│
37
+ └─────┬─────┘
38
+
39
+ Harness validate
40
+
41
+ Pass → next task
42
+ Fail → git rollback + retry
43
+ ```
44
+
45
+ Each session, the agent autonomously follows 6 steps: restore context → env check → pick task → code → test → commit.
46
+
47
+ ## Commands
48
+
49
+ | Command | Description |
50
+ |---------|-------------|
51
+ | `claude-coder setup` | Interactive model configuration |
52
+ | `claude-coder run [requirement]` | Auto-coding loop |
53
+ | `claude-coder run --dry-run` | Preview mode |
54
+ | `claude-coder init` | Initialize project environment |
55
+ | `claude-coder view [requirement]` | Observation mode (single session) |
56
+ | `claude-coder add "instruction"` | Append tasks |
57
+ | `claude-coder validate` | Manually validate last session |
58
+ | `claude-coder status` | View progress and costs |
59
+ | `claude-coder config sync` | Sync config to ~/.claude/ |
60
+
61
+ **Options**: `--max N` limit sessions (default 50), `--pause N` pause every N sessions (default 5).
62
+
63
+ ## Model Support
64
+
65
+ | Provider | Description |
66
+ |----------|-------------|
67
+ | Claude (Official) | Default, Anthropic native API |
68
+ | GLM (Zhipu/Z.AI) | GLM 4.7 / GLM 5 |
69
+ | Aliyun Bailian | qwen3-coder-plus / glm-5 |
70
+ | DeepSeek | deepseek-chat / reasoner |
71
+ | Custom | Any Anthropic-compatible API |
72
+
73
+ ## Project Structure
74
+
75
+ ```
76
+ your-project/
77
+ .claude-coder/ # Runtime data (gitignored)
78
+ .env # Model config
79
+ project_profile.json # Project scan results
80
+ tasks.json # Task list + status
81
+ session_result.json # Session results + history
82
+ progress.json # Session log + costs
83
+ tests.json # Verification records
84
+ .runtime/ # Temp files
85
+ requirements.md # Requirements (optional)
86
+ ```
87
+
88
+ ## Documentation
89
+
90
+ - [Architecture](ARCHITECTURE.md) — Module responsibilities, prompt injection architecture, attention mechanism, hook data flow, future roadmap
91
+
92
+ ## License
93
+
94
+ MIT