1- # AGENTS .md
1+ # CLAUDE .md
22
3- This file provides guidance to Codex (Codex .ai/code) when working with code in this repository.
3+ This file provides guidance to Claude Code (claude .ai/code) and other AI coding agents when working with code in this repository.
44
55## Project Overview
66
7- This is a ** reverse-engineered / decompiled** version of Anthropic's official Codex CLI tool. The goal is to restore core functionality while trimming secondary capabilities. Many modules are stubbed or feature-flagged off. TypeScript strict mode is enforced — ** ` bunx tsc --noEmit ` must pass with zero errors** .
7+ This is a ** reverse-engineered / decompiled** version of Anthropic's official Claude Code CLI tool. The goal is to restore core functionality while trimming secondary capabilities. Many modules are stubbed or feature-flagged off. TypeScript strict mode is enforced — ** ` bunx tsc --noEmit ` must pass with zero errors** .
88
99## Git Commit Message Convention
1010
@@ -39,10 +39,13 @@ echo "say hello" | bun run src/entrypoints/cli.tsx -p
3939# Build (code splitting, outputs dist/cli.js + chunk files)
4040bun run build
4141
42+ # Build with Vite (alternative build pipeline)
43+ bun run build:vite
44+
4245# Test
43- bun test # run all tests (2453 tests / 137 files / 0 fail)
46+ bun test # run all tests
4447bun test src/utils/__tests__/hash.test.ts # run single file
45- bun test --coverage # with coverage report
48+ bun test --coverage # with coverage report
4649
4750# Lint & Format (Biome)
4851bun run lint # check only
@@ -55,6 +58,10 @@ bun run health
5558# Check unused exports
5659bun run check:unused
5760
61+ # Full check (typecheck + lint + test) — run after completing any task
62+ bun run test:all
63+ bun run typecheck
64+
5865# Remote Control Server
5966bun run rcs
6067
@@ -72,17 +79,17 @@ bun run docs:dev
7279- ** Build** : ` build.ts ` 执行 ` Bun.build() ` with ` splitting: true ` ,入口 ` src/entrypoints/cli.tsx ` ,输出 ` dist/cli.js ` + chunk files。Build 默认启用 19 个 feature(见下方 Feature Flag 段)。构建后自动替换 ` import.meta.require ` 为 Node.js 兼容版本(产物 bun/node 都可运行)。
7380- ** Dev mode** : ` scripts/dev.ts ` 通过 Bun ` -d ` flag 注入 ` MACRO.* ` defines,运行 ` src/entrypoints/cli.tsx ` 。默认启用全部 feature。
7481- ** Module system** : ESM (` "type": "module" ` ), TSX with ` react-jsx ` transform.
75- - ** Monorepo** : Bun workspaces — 14 个 internal packages in ` packages/ ` resolved via ` workspace:* ` 。
82+ - ** Monorepo** : Bun workspaces — 15 个 workspace packages + 若干辅助目录 in ` packages/ ` resolved via ` workspace:* ` 。
7683- ** Lint/Format** : Biome (` biome.json ` )。` bun run lint ` / ` bun run lint:fix ` / ` bun run format ` 。
7784- ** Defines** : 集中管理在 ` scripts/defines.ts ` 。当前版本 ` 2.1.888 ` 。
7885- ** CI** : GitHub Actions — ` ci.yml ` (构建+测试)、` release-rcs.yml ` (RCS 发布)、` update-contributors.yml ` (自动更新贡献者)。
7986
8087### Entry & Bootstrap
8188
82- 1 . ** ` src/entrypoints/cli.tsx ` ** (323 行) — True entrypoint。` main() ` 函数按优先级处理多条快速路径:
89+ 1 . ** ` src/entrypoints/cli.tsx ` ** — True entrypoint。` main() ` 函数按优先级处理多条快速路径:
8390 - ` --version ` / ` -v ` — 零模块加载
8491 - ` --dump-system-prompt ` — feature-gated (DUMP_SYSTEM_PROMPT)
85- - ` --Codex -in-chrome-mcp ` / ` --chrome-native-host `
92+ - ` --claude -in-chrome-mcp ` / ` --chrome-native-host `
8693 - ` --computer-use-mcp ` — 独立 MCP server 模式
8794 - ` --daemon-worker=<kind> ` — feature-gated (DAEMON)
8895 - ` remote-control ` / ` rc ` / ` remote ` / ` sync ` / ` bridge ` — feature-gated (BRIDGE_MODE)
@@ -92,34 +99,34 @@ bun run docs:dev
9299 - ` environment-runner ` / ` self-hosted-runner ` — BYOC runner
93100 - ` --tmux ` + ` --worktree ` 组合
94101 - 默认路径:加载 ` main.tsx ` 启动完整 CLI
95- 2 . ** ` src/main.tsx ` ** (~ 6970 行) — Commander.js CLI definition。注册大量 subcommands:` mcp ` (serve/add/remove/list...)、` server ` 、` ssh ` 、` open ` 、` auth ` 、` plugin ` 、` agents ` 、` auto-mode ` 、` doctor ` 、` update ` 等。主 ` .action() ` 处理器负责权限、MCP、会话恢复、REPL/Headless 模式分发。
102+ 2 . ** ` src/main.tsx ` ** (~ 6981 行) — Commander.js CLI definition。注册大量 subcommands:` mcp ` (serve/add/remove/list...)、` server ` 、` ssh ` 、` open ` 、` auth ` 、` plugin ` 、` agents ` 、` auto-mode ` 、` doctor ` 、` update ` 等。主 ` .action() ` 处理器负责权限、MCP、会话恢复、REPL/Headless 模式分发。
961033 . ** ` src/entrypoints/init.ts ` ** — One-time initialization (telemetry, config, trust dialog)。
97104
98105### Core Loop
99106
100- - ** ` src/query.ts ` ** — The main API query function. Sends messages to Codex API, handles streaming responses, processes tool calls, and manages the conversation turn loop.
107+ - ** ` src/query.ts ` ** — The main API query function. Sends messages to Claude API, handles streaming responses, processes tool calls, and manages the conversation turn loop.
101108- ** ` src/QueryEngine.ts ` ** — Higher-level orchestrator wrapping ` query() ` . Manages conversation state, compaction, file history snapshots, attribution, and turn-level bookkeeping. Used by the REPL screen.
102109- ** ` src/screens/REPL.tsx ` ** — The interactive REPL screen (React/Ink component). Handles user input, message display, tool permission prompts, and keyboard shortcuts.
103110
104111### API Layer
105112
106- - ** ` src/services/api/Codex .ts ` ** — Core API client. Builds request params (system prompt, messages, tools, betas), calls the Anthropic SDK streaming endpoint, and processes ` BetaRawMessageStreamEvent ` events.
113+ - ** ` src/services/api/claude .ts ` ** — Core API client. Builds request params (system prompt, messages, tools, betas), calls the Anthropic SDK streaming endpoint, and processes ` BetaRawMessageStreamEvent ` events.
107114- ** 7 providers** : ` firstParty ` (Anthropic direct), ` bedrock ` (AWS), ` vertex ` (Google Cloud), ` foundry ` , ` openai ` , ` gemini ` , ` grok ` (xAI)。
108115- Provider selection in ` src/utils/model/providers.ts ` 。优先级:modelType 参数 > 环境变量 > 默认 firstParty。
109116
110117### Tool System
111118
112119- ** ` src/Tool.ts ` ** — Tool interface definition (` Tool ` type) and utilities (` findToolByName ` , ` toolMatchesName ` ).
113- - ** ` src/tools.ts ` ** (387 行) — Tool registry. Assembles the tool list; some tools are conditionally loaded via ` feature() ` flags or ` process.env.USER_TYPE ` .
114- - ** ` src/ tools/<ToolName>/ ` ** — 55 个 tool 目录 。主要分类:
120+ - ** ` src/tools.ts ` ** — Tool registry. Assembles the tool list; tools are imported from ` @claude-code-best/builtin-tools ` package. Some tools are conditionally loaded via ` feature() ` flags or ` process.env.USER_TYPE ` .
121+ - ** ` packages/builtin- tools/src/tools/ ` ** — 59 个子目录(含 shared/testing 等工具目录),通过 ` @claude-code-best/builtin-tools ` 包导出 。主要分类:
115122 - ** 文件操作** : FileEditTool, FileReadTool, FileWriteTool, GlobTool, GrepTool
116123 - ** Shell/执行** : BashTool, PowerShellTool, REPLTool
117124 - ** Agent 系统** : AgentTool, TaskCreateTool, TaskUpdateTool, TaskListTool, TaskGetTool
118125 - ** 规划** : EnterPlanModeTool, ExitPlanModeV2Tool, VerifyPlanExecutionTool
119126 - ** Web/MCP** : WebFetchTool, WebSearchTool, MCPTool, McpAuthTool
120127 - ** 调度** : CronCreateTool, CronDeleteTool, CronListTool
121128 - ** 其他** : LSPTool, ConfigTool, SkillTool, EnterWorktreeTool, ExitWorktreeTool 等
122- - ** ` src/tools/shared/ ` ** — Tool 共享工具函数。
129+ - ** ` src/tools/shared/ ` ** / ** ` packages/builtin-tools/src/tools/shared/ ` ** — Tool 共享工具函数。
123130
124131### UI Layer (Ink)
125132
@@ -149,10 +156,18 @@ bun run docs:dev
149156| ` packages/@ant/computer-use-mcp/ ` | Computer Use MCP server(截图/键鼠/剪贴板/应用管理) |
150157| ` packages/@ant/computer-use-input/ ` | 键鼠模拟(dispatcher + darwin/win32/linux backend) |
151158| ` packages/@ant/computer-use-swift/ ` | 截图 + 应用管理(dispatcher + per-platform backend) |
152- | ` packages/@ant/Codex-for-chrome-mcp/ ` | Chrome 浏览器控制(通过 ` --chrome ` 启用) |
153- | ` packages/remote-control-server/ ` | 自托管 Remote Control Server(Docker 部署,含 Web UI) |
154- | ` packages/swarm/ ` | Swarm 解耦模块 |
155- | ` packages/shell/ ` | Shell 抽象 |
159+ | ` packages/@ant/claude-for-chrome-mcp/ ` | Chrome 浏览器控制(通过 ` --chrome ` 启用) |
160+ | ` packages/@ant/model-provider/ ` | Model provider 抽象层 |
161+ | ` packages/builtin-tools/ ` | 内置工具集(60 个 tool 实现,通过 ` @claude-code-best/builtin-tools ` 导出) |
162+ | ` packages/agent-tools/ ` | Agent 工具集 |
163+ | ` packages/acp-link/ ` | ACP 代理服务器(WebSocket → ACP agent 桥接) |
164+ | ` packages/cc-knowledge/ ` | Claude Code 知识库(非 workspace 包) |
165+ | ` packages/langfuse-dashboard/ ` | Langfuse 可观测性面板(非 workspace 包) |
166+ | ` packages/mcp-client/ ` | MCP 客户端库 |
167+ | ` packages/mcp-server/ ` | MCP 服务端库(非 workspace 包) |
168+ | ` packages/remote-control-server/ ` | 自托管 Remote Control Server(Docker 部署,含 Web UI)— Web UI 已重构为 React + Vite + Radix UI,支持 ACP agent 接入 |
169+ | ` packages/swarm/ ` | Swarm 解耦模块(非 workspace 包) |
170+ | ` packages/shell/ ` | Shell 抽象(非 workspace 包) |
156171| ` packages/audio-capture-napi/ ` | 原生音频捕获(已恢复) |
157172| ` packages/color-diff-napi/ ` | 颜色差异计算(完整实现,11 tests) |
158173| ` packages/image-processor-napi/ ` | 图像处理(已恢复) |
@@ -161,19 +176,26 @@ bun run docs:dev
161176
162177### Bridge / Remote Control
163178
164- - ** ` src/bridge/ ` ** ( ~ 37 files) — Remote Control / Bridge 模式。feature-gated by ` BRIDGE_MODE ` 。包含 bridge API、会话管理、JWT 认证、消息传输、权限回调等。Entry: ` bridgeMain.ts ` 。
165- - ** ` packages/remote-control-server/ ` ** — 自托管 RCS,支持 Docker 部署,含 Web UI 控制面板。通过 ` bun run rcs ` 启动。
166- - CLI 快速路径: ` Codex remote-control` / ` Codex rc` / ` Codex bridge` 。
179+ - ** ` src/bridge/ ` ** — Remote Control / Bridge 模式。feature-gated by ` BRIDGE_MODE ` 。包含 bridge API、会话管理、JWT 认证、消息传输、权限回调等。Entry: ` bridgeMain.ts ` 。
180+ - ** ` packages/remote-control-server/ ` ** — 自托管 RCS,支持 Docker 部署,含 Web UI 控制面板(React 19 + Vite + Radix UI)。支持 ACP agent 通过 acp-link 接入(ACP WebSocket handler、relay handler、SSE event stream) 。通过 ` bun run rcs ` 启动。
181+ - CLI 快速路径: ` claude remote-control` / ` claude rc` / ` claude bridge` 。
167182- 详见 ` docs/features/remote-control-self-hosting.md ` 。
168183
184+ ### ACP Protocol (Agent Client Protocol)
185+
186+ - ** ` src/services/acp/ ` ** — ACP agent 实现,包含 ` agent.ts ` (AcpAgent 类)、` bridge.ts ` (Claude Code ↔ ACP 桥接)、` permissions.ts ` (权限处理)、` entry.ts ` (入口)。
187+ - ** ` packages/acp-link/ ` ** — ACP 代理服务器,将 WebSocket 客户端桥接到 ACP agent。提供 ` acp-link ` CLI 命令,支持自定义端口/HTTPS/认证/会话管理、RCS 集成(REST 注册 + WS identify 两步流程)、权限模式透传(fallback: 客户端传值 > config > ` ACP_PERMISSION_MODE ` 环境变量)。
188+ - ACP 权限管道改进:` createAcpCanUseTool ` 统一权限流水线,` applySessionMode ` 模式同步,` bypassPermissions ` 可用性检测(非 root/sandbox 环境)。
189+ - ACP Plan 可视化已支持 ` session/update plan ` 类型的消息展示(PlanView 组件,含进度条/状态图标/优先级标签)。
190+
169191### Daemon Mode
170192
171193- ** ` src/daemon/ ` ** — Daemon 模式(长驻 supervisor)。feature-gated by ` DAEMON ` 。包含 ` main.ts ` (entry)和 ` workerRegistry.ts ` (worker 管理)。
172194
173195### Context & System Prompt
174196
175- - ** ` src/context.ts ` ** — Builds system/user context for the API call (git status, date, AGENTS .md contents, memory files).
176- - ** ` src/utils/claudemd.ts ` ** — Discovers and loads AGENTS .md files from project hierarchy.
197+ - ** ` src/context.ts ` ** — Builds system/user context for the API call (git status, date, CLAUDE .md contents, memory files).
198+ - ** ` src/utils/claudemd.ts ` ** — Discovers and loads CLAUDE .md files from project hierarchy.
177199
178200### Feature Flag System
179201
@@ -196,7 +218,7 @@ Feature flags control which functionality is enabled at runtime. 代码中统一
196218
197219### Multi-API 兼容层
198220
199- 所有兼容层均采用流适配器模式:将第三方 API 格式转为 Anthropic 内部格式,下游代码完全不改。
221+ 所有兼容层均采用流适配器模式:将第三方 API 格式转为 Anthropic 内部格式,下游代码完全不改。通过 ` /login ` 命令配置。
200222
201223#### OpenAI 兼容层
202224
@@ -221,6 +243,12 @@ Feature flags control which functionality is enabled at runtime. 代码中统一
221243
222244详见各兼容层的 docs 文档。
223245
246+ ### 穷鬼模式(Budget Mode)
247+
248+ - 通过 ` /poor ` 命令切换,持久化到 ` settings.json ` 。
249+ - 启用后跳过 ` extract_memories ` 、` prompt_suggestion ` 和 ` verification_agent ` ,显著减少 token 消耗。
250+ - 实现在 ` src/commands/poor/poorMode.ts ` 。
251+
224252### Stubbed/Deleted Modules
225253
226254| Module | Status |
@@ -245,20 +273,40 @@ Feature flags control which functionality is enabled at runtime. 代码中统一
245273## Testing
246274
247275- ** 框架** : ` bun:test ` (内置断言 + mock)
248- - ** 当前状态** : 2472 tests / 138 files / 0 fail
249276- ** 单元测试** : 就近放置于 ` src/**/__tests__/ ` ,文件名 ` <module>.test.ts `
250277- ** 集成测试** : ` tests/integration/ ` — 4 个文件(cli-arguments, context-build, message-pipeline, tool-chain)
251278- ** 共享 mock/fixture** : ` tests/mocks/ ` (api-responses, file-system, fixtures/)
252279- ** 命名** : ` describe("functionName") ` + ` test("behavior description") ` ,英文
253- - ** Mock 模式** : 对重依赖模块使用 ` mock.module() ` + ` await import() ` 解锁(必须内联在测试文件中,不能从共享 helper 导入)
254280- ** 包测试** : ` packages/ ` 下各包也有独立测试(如 ` color-diff-napi ` 11 tests)
255281
282+ ### Mock 使用规范
283+
284+ ** 只 mock 有副作用的依赖链,不 mock 纯函数/纯数据模块。**
285+
286+ 被迫 mock 的根源:` log.ts ` / ` debug.ts ` → ` bootstrap/state.ts ` (模块级 ` realpathSync ` / ` randomUUID ` 副作用)。必须 mock 的模块:` log.ts ` 、` debug.ts ` 、` bun:bundle ` 、` settings/settings.js ` 、` config.ts ` 、` auth.ts ` 、第三方网络库。
287+
288+ ** ` log.ts ` 和 ` debug.ts ` 使用共享 mock** (` tests/mocks/log.ts ` / ` tests/mocks/debug.ts ` ),不要在测试文件中内联 mock 定义。使用方式:
289+
290+ ``` ts
291+ import { logMock } from " ../../../tests/mocks/log" ;
292+ mock .module (" src/utils/log.ts" , logMock );
293+
294+ import { debugMock } from " ../../../../tests/mocks/debug" ;
295+ mock .module (" src/utils/debug.ts" , debugMock );
296+ ```
297+
298+ 源文件导出变更时只需更新 ` tests/mocks/ ` 下的对应文件,不需要逐个修改测试。
299+
300+ 不要 mock:纯函数模块(` errors.ts ` 、` stringUtils.js ` )、mock 值与真实实现相同的模块、mock 路径与实际 import 不匹配的模块。
301+
302+ 路径规则:统一用 ` .ts ` 扩展名 + ` src/* ` 别名路径,禁止双重 mock 同一模块。
303+
256304### 类型检查
257305
258306项目使用 TypeScript strict 模式,** tsc 必须零错误** 。每次修改后运行:
259307
260308``` bash
261- bunx tsc --noEmit
309+ bun run typecheck
262310```
263311
264312** 类型规范** :
@@ -271,7 +319,7 @@ bunx tsc --noEmit
271319
272320## Working with This Codebase
273321
274- - ** tsc must pass** — ` bunx tsc --noEmit ` 必须零错误,任何修改都不能引入新的类型错误。
322+ - ** tsc must pass** — ` bun run typecheck ` 必须零错误,任何修改都不能引入新的类型错误。
275323- ** Feature flags** — 默认全部关闭(` feature() ` 返回 ` false ` )。Dev/build 各有自己的默认启用列表。不要在 ` cli.tsx ` 中重定义 ` feature ` 函数。
276324- ** React Compiler output** — Components have decompiled memoization boilerplate (` const $ = _c(N) ` ). This is normal.
277325- ** ` bun:bundle ` import** — ` import { feature } from 'bun:bundle' ` 是 Bun 内置模块,由运行时/构建器解析。不要用自定义函数替代它。** ` feature() ` 只能直接用在 ` if ` 语句或三元表达式的条件位置** (Bun 编译器限制),不能赋值给变量、不能放在箭头函数体里、不能作为 ` && ` 链的一部分。正确:` if (feature('X')) {} ` 或 ` feature('X') ? a : b ` 。
@@ -281,3 +329,29 @@ bunx tsc --noEmit
281329- ** Biome 配置** — 大量 lint 规则被关闭(decompiled 代码不适合严格 lint)。` .tsx ` 文件用 120 行宽 + 强制分号;其他文件 80 行宽 + 按需分号。
282330- ** Ink 框架在 ` packages/@ant/ink/ ` ** — 不是 ` src/ink/ ` (该目录不存在)。Ink 相关的组件、hooks、keybindings 都在 packages 中。
283331- ** Provider 优先级** — ` modelType ` 参数 > 环境变量 > 默认 ` firstParty ` 。新增 provider 需在 ` src/utils/model/providers.ts ` 注册。
332+
333+ ## Design Context
334+
335+ Impeccable 设计上下文保存在 ` .impeccable.md ` 中。设计 Web UI(RCS 控制面板、文档站、着陆页)时必须参考该文件。
336+
337+ ### 核心设计原则
338+
339+ 1 . ** Considered over clever** — 每个设计选择都应感觉有意为之,而非追逐潮流
340+ 2 . ** Warmth through subtlety** — 通过橙色色调的中性色、留白布局、有温度的文案来传达温暖
341+ 3 . ** Density with clarity** — 技术用户需要信息密度,但不能混乱
342+ 4 . ** Community voice** — 设计应感觉是由使用者创造的,而非遥远的设计团队
343+ 5 . ** Anthropic's shadow** — 遵循 Anthropic 的设计直觉:干净的布局、充足的间距、温暖的色温
344+
345+ ### 品牌色
346+
347+ - 主色:Claude Orange ` #D77757 ` (terra cotta)
348+ - 辅色:Claude Blue ` #5769F7 `
349+ - 暗色模式使用温暖的深色表面(非冷蓝黑色)
350+
351+ ### 目标用户
352+
353+ 技术团队/企业,在专业工作流中使用 AI 辅助编程。友好的开源社区氛围,非企业 SaaS 风格。
354+
355+ ### 视觉参考
356+
357+ Anthropic 公司的设计风格 — 干净、考究、温暖的底色。大量留白,以排版为核心。避免 AI 产品常见的设计套路(渐变文字、玻璃态、霓虹色)。
0 commit comments