Skip to content

Latest commit

 

History

History
141 lines (100 loc) · 8.82 KB

File metadata and controls

141 lines (100 loc) · 8.82 KB

中文

Claude Code Source Anatomy

License docs-check Docs Language Lines PRs Welcome

📖 Read Online: https://anneheartrecord.github.io/claude-code-docs/

What Happened

In March 2026, someone discovered that Anthropic's Claude Code client package published on npm included sourcemap files.

Sourcemaps are debug-aid files generated by frontend build tools that record the mapping between compiled code and original source. They should be excluded during publishing, but Anthropic's build pipeline missed this step.

The complete TypeScript source was reverse-engineered. 515,000 lines of code, 2,766 files.

To be clear: only the client-side code was leaked. Claude Code uses a standard client-server architecture. The client runs in your terminal, handling user interaction, tool execution, permission management, and context assembly. The server is Anthropic's API, handling model inference. The model itself and server-side logic were not leaked.

But the client-side code is valuable enough. The core competitive advantage of an Agent product lies not in model invocation itself, but in how to safely, efficiently, and reliably unleash model capabilities. This is the domain of Harness Engineering, and the client code is the complete Harness implementation.

Ironically, based on git history, this code was most likely written by AI itself. All 20 commits are from a single user claude-code-best, three commits include Co-Authored-By: Claude Opus 4.6. 515,000 lines compiled with zero errors on the first try. AI wrote the functionality flawlessly, but tripped on a basic publishing configuration detail.

What This Repository Does

I used Claude Code to systematically analyze this source, module by module, producing 13 technical documents in both Chinese and English.

From architecture design to the six-phase Agent loop implementation, from the three-tier message compaction system to the 6,300-line permission system, from four instruction scopes and auto memory to the 40+ tool execution pipeline. I also extracted Anthropic's explored feature directions from the 82 feature flags in the March 2026 snapshot: Kairos autonomous mode, Context Collapse, and Voice Mode.

The final documents include my own analysis: whether this code is truly valuable, how engineers should do Code Review in the AI Coding era, and what clues the source reveals about Claude's account ban mechanism.

Target Audience: Engineers building Agent products, developers interested in AI Agent architecture, anyone wanting to understand how a top-tier Agent system is engineered for production. Non-technical readers can also gain insights into how Agent products work and industry trends.

Documentation

Overview

Document Content
01-Architecture Overview Overall architecture, tech stack, core files, complete request journey
02-Value Debate Artifact vs Harness capability, two perspectives, code is snapshot capability is dynamic

Core Modules

Document Content
03-Agent Loop Six-phase ReAct loop, AsyncGenerator design, state management, thinking chain preservation
04-Context Engineering System prompt construction, CLAUDE.md loading, layered priorities, prefetch caching, prompt cache optimization
05-Compaction System Three-tier compaction: microcompact, session memory, full compact, circuit breaker, recursion guard
06-Permission System Six permission modes, YOLO classifier, 42 rules from the historical snapshot, filesystem sandbox, dangerous rule stripping
07-Memory System Four instruction scopes, auto memory, @include directives, MEMORY.md management, session memory
08-Tools & Skills 40+ tool registry, execution pipeline, pre/post hooks, skill fork mechanism
09-MCP Integration Four transport types (three CLI transports), OAuth, three installation scopes

Forward-Looking

Document Content
10-Future Features 82 feature flags from the historical snapshot, Kairos autonomous mode, Context Collapse, shipped voice mode
11-AI Code Review Individual/team/CICD three-layer review paradigm, Review Agent concept
12-Agent Security Design Three-layer defense system, L0-L4 security maturity model, practical guide
13-Source Code Findings AI engineering blind spots, production incidents, Claude ban mechanism analysis

Quick Start

The source repo can be cloned and run directly:

git clone https://github.com/anthropics/claude-code.git
cd claude-code

bun install
bun run build
# ✓ Bundled 5344 modules in 554ms
#   cli.js  25.89 MB

bun run dev --version
# 2.1.888 (Claude Code)

Key Stats

Metric Data
Codebase 515,498 lines TypeScript/TSX
Files 2,766
Build Output 25.89 MB, 5,344 modules
Built-in Tools 40+
Feature Flags 82
Permission Rules 42 hardcoded dangerous patterns
Compaction Threshold Context window - 13,000 tokens
Dependencies 583 packages

Version coverage

A source teardown goes stale. Rather than leave readers guessing, here is exactly which snapshot the analysis was verified against.

Item Value
Source snapshot sourcemap reconstruction dated 2026-03-31
Version that build self-reported 2.1.888
Chapter content last re-verified 2026-08-11 (Claude Code 2.1.227, upstream commit 54cc51a)
Repository last maintained 2026-08-11 (v1.1.0 — links and tooling; no chapter conclusions changed)

This review used the npm-published Claude Code 2.1.227 package and Anthropic's public repository at commit 54cc51a. Claims about source paths, line counts, and feature-flag totals from the 2026-03-31 sourcemap remain historical snapshot data, not statements about the current private implementation.

Chapter Review result
01 Architecture Overview Historical source snapshot retained; current differences are covered in 04, 05, 06, 07, 09, and 10
02 Value Debate Analytical content, no version correction needed
03 Agent Loop Core loop analysis retained as snapshot content
04 Context Engineering Current model context and memory wording updated
05 Compaction System Current configurable auto-compaction window updated
06 Permission System Current six permission modes added; 42-rule count marked as snapshot data
07 Memory System Current auto-memory availability updated
08 Tools and Skills Conservative 40+ lower bound retained, current tool changes noted
09 MCP Integration Current public transport types and configuration scope updated
10 Future Features Auto-memory, voice, and 1M context marked as shipped
11 AI Code Review Analytical content, no version correction needed
12 Agent Security Design Current permission-mode wording synced with chapter 06
13 Source Code Findings Feature flags, dependency count, and auto-memory prediction marked as snapshot or shipped

If an upstream change has invalidated something a chapter says, please open a version drift issue. It is the most welcome issue type here.

Contributing

The most valuable contribution is not adding content — it is telling me where I got it wrong. Factual corrections are the top priority, and issues get a first response within 7 days.

  • Corrections and proposals: see the contributing guide (corrections must carry evidence)
  • Change history: see CHANGELOG.md
  • Local checks: python3 scripts/check_links.py && python3 scripts/check_bilingual.py

License

MIT, covering the original material here: the 13 chapters, the illustrations, the site configuration, and scripts/.

This repository does not redistribute Claude Code source. Quoted excerpts, trademarks, and the accuracy disclaimer are covered in NOTICE.md.