Claude Code CLI——逆向工程设计文档
实施就绪 Anthropic的逆向工程规范 克劳德代码 CLI工具。 通过npm包的源代码映射分析提取(2026年3月)。
______________________________________________________________________
概览
┌─────────────────────────────────────────────────────────────────────┐
│ Claude Code CLI │
│ │
│ ~1,900 files · 512,000+ LOC · TypeScript Strict · Bun │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ React │ │ Ink │ │ Yoga │ │Commander │ │
│ │ 19 │ │ (custom │ │ (WASM │ │ .js │ │
│ │ │ │ fork) │ │ layout) │ │ │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
│ └──────────────┴──────────────┴──────────────┘ │
│ Terminal UI │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ Core Engine │ │
│ │ ┌─────────┐ ┌──────────┐ ┌───────────┐ ┌──────────────┐ │ │
│ │ │ 45+ │ │ Query │ │ Streaming │ │ Cost │ │ │
│ │ │ Tools │ │ Engine │ │ Executor │ │ Tracker │ │ │
│ │ └─────────┘ └──────────┘ └───────────┘ └──────────────┘ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ Extension Layer │ │
│ │ ┌─────┐ ┌────────┐ ┌────────┐ ┌──────┐ ┌─────┐ ┌──────┐ │ │
│ │ │ MCP │ │Plugins │ │ Skills │ │Hooks │ │ LSP │ │Bridge│ │ │
│ │ └─────┘ └────────┘ └────────┘ └──────┘ └─────┘ └──────┘ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ Services │ │
│ │ ┌────────┐ ┌──────┐ ┌───────────┐ ┌────────┐ ┌─────────┐ │ │
│ │ │ API │ │ Auth │ │ Analytics │ │Settings│ │Migration│ │ │
│ │ │(4 back)│ │OAuth │ │ DD+OTel │ │5-layer │ │ chain │ │ │
│ │ └────────┘ └──────┘ └───────────┘ └────────┘ └─────────┘ │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ Security & Permissions │ │
│ │ ┌──────────┐ ┌───────────┐ ┌──────────┐ ┌──────────────┐ │ │
│ │ │ 7 Perm │ │ 23 Bash │ │ Path │ │ Auto-Mode │ │ │
│ │ │ Modes │ │ Checks │ │Validator │ │ Classifier │ │ │
│ │ └──────────┘ └───────────┘ └──────────┘ └──────────────┘ │ │
│ └─────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────┘______________________________________________________________________
设计文件
第1部分——体系结构概述
docs/01-architecture-overview.md ·1101条线路Startup Sequence (5 phases)
═══════════════════════════════════════════
Phase 0 ─► Module-level side effects (MDM, Keychain prefetch)
Phase 1 ─► main() entry, basic initialization
Phase 2 ─► Commander preAction → init() → migrations
Phase 3 ─► action() → setup() → auth/permissions
Phase 4 ─► REPL render + deferred prefetches| 主题 | 关键细节 |
|---|---|
| 模块系统 | 仅ESM, .js 需要扩展,懒惰 require() 对于循环 |
| 构建 | Bun bundler, feature() 清除死代码的标志 |
| 配置 | 7层:全局→ 项目→ 设置(5个来源)→ CLI → MDM |
| 功能标志 | 通过GrowthBook有89面旗帜, bun:bundle 编译时DCE |
| 状态 | 110个全球领域 bootstrap/state.ts, DO NOT ADD MORE 政策 |
______________________________________________________________________
第2部分——核心发动机
docs/02-core-engine.md ·1352条线路Tool Execution Pipeline
═══════════════════════════════════════════
User Input ──► QueryEngine ──► LLM API (streaming)
│
tool_use block
│
┌───────────────┼───────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Bash │ │ FileEdit │ │ Agent │
│ Tool │ │ Tool │ │ Tool │
└────┬─────┘ └────┬─────┘ └────┬─────┘
│ │ │
▼ ▼ ▼
ToolResult ToolResult ToolResult
{ data, newMessages?, contextModifier?, mcpMeta? }
│
◄─────┘
Next LLM turn with results| 主题 | 关键细节 |
|---|---|
| 工具接口 | 60+个字段, buildTool() 工厂,7个默认密钥 |
| 并发 | isConcurrencySafe flag——安全工具并行运行,不安全工具独占运行 |
| 恢复 | 8级链:流媒体回退→ 模型回退→ 坍塌排水沟→ 反应式紧凑型 |
| 成本跟踪 | 每会话美元累加器,多提供商标准化 |
______________________________________________________________________
第3部分——许可和安全
docs/03-permission-security.md ·1178条线路Permission Decision Pipeline (4 steps + auto-mode)
═══════════════════════════════════════════════════
Step 0 Config deny rules ──► deny (immediate)
Step 1 Tool.checkPermissions() ──► allow/deny/ask/passthrough
Step 2 Permanent allow rules ──► allow (skip prompt)
Step 3 passthrough → ask conversion
Step 4 Post-processing:
├── dontAsk mode ──► auto deny
├── auto mode ──► classifier pipeline:
│ ├── acceptEdits fast-path
│ ├── safe-tool allowlist
│ └── YOLO classifier (2-stage)
│ ├── allowed ──► allow
│ ├── blocked ──► deny + denial tracking
│ └── unavailable ──► iron_gate fail-closed
└── headless ──► hook or deny| 主题 | 关键细节 |
|---|---|
| 7种模式 | default, plan, acceptEdits, bypassPermissions, dontAsk, auto, bubble |
| Bash安全 | 23个检查ID,19个跨平台+7个Unix+11个仅ANT模式 |
| 路径验证 | 6步:标准化→ 符号链接解析→ 遍历→ 窗户图案→ 范围 |
| TOCTU警告 | 路径验证和文件访问不是原子竞争条件窗口 |
______________________________________________________________________
第4部分——多代理和内存
docs/04-multi-agent-memory.md ·1416条线路Agent Hierarchy
═══════════════════════════════════════════
┌───────────────┐
│ Coordinator │ (orchestrator — no direct tool use)
└───────┬───────┘
┌────────────┼────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Worker │ │ Worker │ │ Worker │
│ (fork) │ │ (spawn) │ │(in-proc) │
└──────────┘ └──────────┘ └──────────┘
Memory Hierarchy (5 layers)
═══════════════════════════════════════════
[1] Session Memory ── in-context summaries
[2] Project Memory ── .claude/memory/ files
[3] User Memory ── ~/.claude/memory/
[4] Team Memory ── server-synced shared memory
[5] External ── MCP servers, LSP, git| 主题 | 关键细节 |
|---|---|
| 3个Swarm后端 | fork (共享上下文), spawn (独立), in-process teammate |
| 任务类型 | 7种带有ID前缀的类型: b灰烬, w工作流, a绅士, teammate, r情绪化, m监视器, d令 |
| 邮箱 | 基于文件: .claude/teams/{team}/inboxes/{agent}.json |
| 团队同步 | 基于ETag的冲突检测(412),无本地文件锁定 |
______________________________________________________________________
第5部分——扩展系统
docs/05-extension-systems.md ·1709行6 Extension Points
═══════════════════════════════════════════
┌─────┐ ┌────────┐ ┌────────┐ ┌──────┐ ┌─────┐ ┌──────┐
│ MCP │ │Plugins │ │ Skills │ │Hooks │ │ LSP │ │Bridge│
│ │ │ │ │ │ │ │ │ │ │ │
│8 cfg│ │market- │ │17 bun- │ │27 │ │code │ │IDE │
│types│ │place + │ │dled + │ │events│ │intel│ │comms │
│ │ │builtin │ │custom │ │ │ │ │ │ │
└─────┘ └────────┘ └────────┘ └──────┘ └─────┘ └──────┘| 主题 | 关键细节 |
|---|---|
| MCP运输 | TransportSchema:6种; McpServerConfig:8(+ws-ide,claudeai代理) |
| 挂钩优先级 | deny > ask > allow --任何钩子的拒绝都会覆盖所有允许 |
| 插件加载 | 并行(市场+会话)→ 内置函数→ 合并→ 依赖性验证 |
| 重新连接 | MAX_ERRORS_BEFORE_RECONNECT=3,SSE最大重试次数=2,轮询超时15分钟 |
______________________________________________________________________
第6部分——UI层
docs/06-ui-layer.md ·1784条线Render Pipeline
═══════════════════════════════════════════
React Component Tree
│
▼
Custom Reconciler (React 19 HostConfig)
│
▼
Yoga Layout Engine (WASM)
│ ┌──────────────────────┐
├──│ Box (flexbox node) │
├──│ Text (measure func) │
└──│ Button (interactive) │
└──────────────────────┘
│
▼
Output Renderer (ANSI diff)
│
▼
Terminal stdout (throttled at FRAME_INTERVAL_MS)| 主题 | 关键细节 |
|---|---|
| 墨水叉 | 96张图片 src/ink/,自定义对账器,瑜伽WASM集成 |
| 组件 | 346 .tsx 文件在 src/components/ |
| 钩子 | 85+定制挂钩(useTextInput, useTerminalSize, useStdin等等) |
| 并发 | maySuspendCommit() 总是回来 false --悬架已禁用 |
______________________________________________________________________
第7部分——服务和基础设施
docs/07-services-infrastructure.md ·1845条线API Client Factory (4 backends)
═══════════════════════════════════════════
┌──────────────────┐
│ buildFetch() │
│ + withRetry() │
└────────┬─────────┘
┌────────────────┼────────────────┐──────────────┐
▼ ▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐
│ Anthropic│ │ Bedrock │ │ Foundry │ │ Vertex │
│ (Direct) │ │ (AWS) │ │ (Azure) │ │ (GCP) │
└──────────┘ └──────────┘ └──────────┘ └──────────┘
Retry Strategy: 429 → Retry-After (≤20s: sleep, >20s: 30min cooldown)
529 → MAX_529_RETRIES=3 (non-subscriber only)
401 → token refresh → retry (no max refresh limit!)| 主题 | 关键细节 |
|---|---|
| 分析 | 数据狗(44个允许的事件,16个标签字段)+开放遥测+1P记录器 |
| OAuth | PKCE流,带明文回退的macOS钥匙串(30s TTL缓存) |
| 设置合并 | 5来源: user → project → local → flag → policy (数组concat,而不是override!) |
| 迁移 | 11同步+1异步,包括 migrateLegacyOpusToCurrent (仅限蚂蚁DCE) |
______________________________________________________________________
第8部分-类型、架构和API
docs/08-types-schemas-api.md ·2194条线路Type System Layers
═══════════════════════════════════════════
┌─────────────────────────────────────────┐
│ Branded IDs │
│ SessionId, AgentId, TaskId, ToolUseId │
├─────────────────────────────────────────┤
│ Core Types │
│ Tool, ToolResult, │
│ ValidationResult, ToolUseContext (30+) │
├─────────────────────────────────────────┤
│ Zod Schemas │
│ HookCommand (discriminated union), │
│ SettingsSchema (~100 fields), │
│ TransportSchema (6 literals) │
├─────────────────────────────────────────┤
│ AppState │
│ DeepImmutable<> wrapper, │
│ ~85 fields + Store pattern │
├─────────────────────────────────────────┤
│ Environment Variables (60+) │
│ Feature Flags (89) │
│ Analytics Events (44 Datadog) │
└─────────────────────────────────────────┘| 主题 | 关键细节 |
|---|---|
| 设置json | 15个类别(身份验证、权限、MCP、钩子、模型、UI……)中的约100个字段 |
| 设置合并 | policySettings 内部优先级:远程>HKLM/plist>文件>HKCU |
| 环境变量 | 跨越7个组的60多个变量(API、模型、身份验证、调试、功能、代理、内部) |
| 功能标志 | 89 bun:bundle feature() 调用,编译时死代码消除 |
______________________________________________________________________
基础设施和部署
infrastructure/ --使用Terraform IaC完成部署蓝图Deployment Options
═══════════════════════════════════════════
┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐
│ Local Dev │ │ AWS │ │ Azure │ │ Supabase │
│ │ │ │ │ │ │ │
│ Ollama/vLLM │ │ ECS Fargate │ │ Container │ │ Edge Funcs │
│ Docker Stack │ │ Bedrock │ │ Apps │ │ Realtime │
│ RTX 4090 │ │ Lambda │ │ Azure OpenAI │ │ PostgreSQL │
│ │ │ Cognito │ │ AD B2C │ │ Auth │
│ $0-50/mo │ │ $45-180/mo │ │ $50-190/mo │ │ $0-25/mo │
└──────────────┘ └──────────────┘ └──────────────┘ └──────────────┘| 文档 | 描述 |
|---|---|
01-local-development.md | Ollama、vLLM、LM Studio、Docker Compose全栈 |
02-aws-architecture.md | VPC、ECS、基岩、Lambda沙盒、CloudWatch |
03-azure-architecture.md | 容器应用程序、Azure OpenAI、函数、监视器 |
04-supabase-backend.md | 10表模式、RLS策略、边缘函数 |
05-cost-analysis.md | 实际价格:每月9美元→ $25/开发企业 |
06-architecture-diagram.md | 7个ASCII图:系统、数据流、身份验证、代理 |
terraform/ | 10 .tf 文件--生产就绪的AWS IaC |
成本快速参考
| 缩放 | Subabase+API | AWS Full | Hybrid(本地GPU) |
|---|---|---|---|
| 独奏 (1台设备) | 9-103美元/月 | 240-280美元/月 | 50美元/月 |
| 团队 (10名开发人员) | 46-116美元/人 | 65-110美元/人 | 35-60美元/人 |
| 企业 (100名开发人员) | 不适用 | 45-111美元/人 | 25美元/每人 |
LLM代币成本占每个规模总支出的60-95%。
______________________________________________________________________
验证状态
这些文件通过了 8次严格验证通过 对照实际源代码:
Pass 1 ████████████████ 16 corrections (type/value accuracy)
Pass 2 ████ 4 fixes (blocking implementation gaps)
Pass 3 ███████ 7 corrections (line-by-line precision)
Pass 4 ███ 3 fixes (missing schemas/flows)
Pass 5 ███████ 7 additions (Bridge, MDM, Keychain, Plugin)
Pass 6 ██████████████ 42 caveats (structural problem warnings)
Pass 7 ██ 2 micro-fixes (convergence check)
Pass 8 ─────────────── CONVERGED (24/24 spot-checks verified)
═══════════════
Total: 39 corrections + 42 implementation caveats______________________________________________________________________
快速开始
# Clone the repo
git clone https://github.com/jung-wan-kim/claude-code-reverse-engineering.git
cd claude-code-reverse-engineering
# Read the design docs (start here)
open docs/01-architecture-overview.md
# Deploy infrastructure (AWS)
cd infrastructure/terraform
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars with your settings
terraform init && terraform plan______________________________________________________________________
存储库结构
claude-code-reverse-engineering/
├── README.md ← You are here
├── docs/
│ ├── 01-architecture-overview.md (1,101 lines)
│ ├── 02-core-engine.md (1,352 lines)
│ ├── 03-permission-security.md (1,178 lines)
│ ├── 04-multi-agent-memory.md (1,416 lines)
│ ├── 05-extension-systems.md (1,709 lines)
│ ├── 06-ui-layer.md (1,784 lines)
│ ├── 07-services-infrastructure.md (1,845 lines)
│ └── 08-types-schemas-api.md (2,194 lines)
└── infrastructure/
├── 01-local-development.md
├── 02-aws-architecture.md
├── 03-azure-architecture.md
├── 04-supabase-backend.md
├── 05-cost-analysis.md
├── 06-architecture-diagram.md
└── terraform/
├── main.tf
├── variables.tf
├── vpc.tf
├── compute.tf
├── database.tf
├── storage.tf
├── auth.tf
├── monitoring.tf
├── outputs.tf
└── terraform.tfvars.example______________________________________________________________________
免责声明
这是一项逆向工程工作 仅用于教育和安全研究目的原始Claude Code软件的所有权利均归 Anthropic → 安thropic(音译)或 人类中心(意译).
许可证
MIT(仅提供文档,不包括原始的Claude Code软件)
