Source archive of Claude Code and a clean-room Python rewrite research repository
- 7:40 AM Β· Apr 02, 2026: Nano Claude Code v2.0: A Minimal Python Reimplementation (~3400 Lines), support open and closed source models, skill and memory
- 8:36 AM Β· Apr 01, 2026: Nano Claude Code v1.0: A Minimal Python Reimplementation (~1300 Lines)
- 0:20 AM Β· Apr 01, 2026: Analysis of Claude Code source code (Video: In Chinese)
- 19:45 PM Β· Mar 31, 2026: Claude Code Research Report (In Chinese)
- 17:13 PM Β· Mar 31, 2026: Architecture Analysis of Claude Code (In Chinese)
- 16:43 PM Β· Mar 31, 2026: An Overview of Claude Code Features (In Chinese)
- 15:00 PM Β· Mar 31, 2026: Hacker News Community Discussion about Claude Code Leak
- 11:48 AM Β· Mar 31, 2026: How Anthropic Built 7 Layers of Memory and a Dreaming System for Claude Code
- 9:50 AM Β· Mar 31, 2026: Claude code memory analysis
- 9:48 AM Β· Mar 31, 2026: Claude Code's source code appears to have leaked: here's what we know
- 9:04 AM Β· Mar 31, 2026: Deconstructing the Claude Code Architecture (In Chinese)
- 7:07 AM Β· Mar 31, 2026: A Walkthrough of Claude Code's Source Code (In Chinese)
- 5:41 AM Β· Mar 31, 2026: Why Claude Code Outperforms Other Tools: An Analysis
- 5:03 AM Β· Mar 31, 2026: Anthropic's AI Coding Tool Leaks Its Own Source Code
- 3:28 AM Β· Mar 31, 2026: Claude Code Source Code Unveiled: Prompts, Self-Healing Mechanisms, and Multi-Agent Architecture (In Chinese)
- 3:02 AM Β· Mar 31, 2026: Community Reaction to the Claude Code Source Exposure
- 2:57 AM Β· Mar 31, 2026: The code behind Claude Code
- 1:23 AM Β· Mar 31, 2026: A Viral Post on the Claude Code Source Leak
This repository contains subprojects that study Claude Code (Anthropicβs official CLI tool: source claude code) from two different angles:
| Subproject | Language | Nature | File Count |
|---|---|---|---|
| claude-code-source-code | TypeScript | Decompiled source archive (v2.1.88) | 1,884 files |
| claw-code | Python | Clean-room architectural rewrite | 66 files |
A decompiled/unpacked source archive of Claude Code v2.1.88, reconstructed from the npm package @anthropic-ai/claude-code@2.1.88, containing approximately 163,318 lines of TypeScript code.
claude-code-source-code/
βββ src/
β βββ main.tsx # CLI entry and REPL bootstrap (4,683 lines)
β βββ query.ts # Core main agent loop (largest single file, 785KB)
β βββ QueryEngine.ts # SDK/Headless query lifecycle engine
β βββ Tool.ts # Tool interface definitions + buildTool factory
β βββ commands.ts # Slash command definitions (~25K lines)
β βββ tools.ts # Tool registration and presets
β βββ context.ts # User input context handling
β βββ history.ts # Session history management
β βββ cost-tracker.ts # API cost tracking
β βββ setup.ts # First-run initialization
β β
β βββ cli/ # CLI infrastructure (stdio, structured transports)
β βββ commands/ # ~87 slash command implementations
β βββ components/ # React/Ink terminal UI (33 subdirectories)
β βββ tools/ # 40+ tool implementations (44 subdirectories)
β βββ services/ # Business logic layer (22 subdirectories)
β βββ utils/ # Utility function library
β βββ state/ # Application state management
β βββ types/ # TypeScript type definitions
β βββ hooks/ # React Hooks
β βββ bridge/ # Claude Desktop remote bridge
β βββ remote/ # Remote mode
β βββ coordinator/ # Multi-agent coordination
β βββ tasks/ # Task management
β βββ assistant/ # KAIROS assistant mode
β βββ memdir/ # Long-term memory management
β βββ plugins/ # Plugin system
β βββ voice/ # Voice mode
β βββ vim/ # Vim mode
β
βββ docs/ # In-depth analysis docs (bilingual: Chinese/English)
β βββ en/ # English analysis
β βββ zh/ # Chinese analysis
βββ vendor/ # Third-party dependencies
βββ stubs/ # Module stubs
βββ types/ # Global type definitions
βββ utils/ # Top-level utility functions
βββ scripts/ # Build scripts
βββ package.json
User Input
β
processUserInput() # Parse /slash commands
β
query() # Main agent loop (query.ts)
βββ fetchSystemPromptParts() # Assemble system prompt
βββ StreamingToolExecutor # Parallel tool execution
βββ autoCompact() # Automatic context compression
βββ runTools() # Tool orchestration and scheduling
β
yield SDKMessage # Stream results back to the consumer
| Component | Technology |
|---|---|
| Language | TypeScript 6.0+ |
| Runtime | Bun (compiled into Node.js >= 18 bundle) |
| Claude API | Anthropic SDK |
| Terminal UI | React + Ink |
| Code Bundling | esbuild |
| Data Validation | Zod |
| Tool Protocol | MCP (Model Context Protocol) |
| Category | Tools |
|---|---|
| File Operations | FileReadTool, FileEditTool, FileWriteTool |
| Code Search | GlobTool, GrepTool |
| System Execution | BashTool |
| Web Access | WebFetchTool, WebSearchTool |
| Task Management | TaskCreateTool, TaskUpdateTool, TaskGetTool, TaskListTool |
| Sub-agents | AgentTool |
| Code Environments | NotebookEditTool, REPLTool, LSPTool |
| Git Workflow | EnterWorktreeTool, ExitWorktreeTool |
| Configuration & Permissions | ConfigTool, AskUserQuestionTool |
| Memory & Planning | TodoWriteTool, EnterPlanModeTool, ExitPlanModeTool |
| Automation | ScheduleCronTool, RemoteTriggerTool, SleepTool |
| MCP Integration | MCPTool |
/commit /commit-push-pr /review /resume /session /memory /config /skills /help /voice /desktop /mcp /permissions /theme /vim /copy and more
- Three modes:
default(ask user) /bypass(auto-allow) /strict(auto-deny) - Tool-level fine-grained control
- ML-based automated permission inference classifier
- Persistent storage of permission rules
- Automatic compression strategies (
autoCompact): reactive compression, micro-compression, trimmed compression - Context collapsing (
CONTEXT_COLLAPSE) - Token counting and estimation
- Session transcript persistence
| Document | Content |
|---|---|
| 01 - Telemetry and Privacy | Dual-layer analysis pipeline (Anthropic + Datadog), with no opt-out switch |
| 02 - Hidden Features and Model Codenames | Internal codenames such as Capybara, Tengu, Fennec, Numbat |
| 03 - Undercover Mode | Anthropic employees automatically entering undercover mode in public repositories |
| 04 - Remote Control and Emergency Switches | Hourly polling, 6+ killswitches, dangerous-change popups |
| 05 - Future Roadmap | KAIROS autonomous agent, voice mode, 17 unreleased tools |
A clean-room Python rewrite of Claude Code (without including original source copies), focused on architectural mirroring and research. Built by @instructkr (Sigrid Jin), and became one of the fastest GitHub repositories in the world to reach 30K stars.
claw-code/
βββ src/
β βββ __init__.py # Package export interface
β βββ main.py # CLI entry (~200 lines)
β βββ query_engine.py # Core query engine
β βββ runtime.py # Runtime session management
β βββ models.py # Shared data classes
β βββ commands.py # Command metadata and execution framework
β βββ tools.py # Tool metadata and execution framework
β βββ permissions.py # Permission context management
β βββ context.py # Ported context layer
β βββ setup.py # Workspace initialization
β βββ session_store.py # Session persistence
β βββ transcript.py # Session transcript storage
β βββ port_manifest.py # Workspace manifest generation
β βββ execution_registry.py # Execution registry
β βββ history.py # History logs
β βββ parity_audit.py # Parity audit against TypeScript source
β βββ remote_runtime.py # Remote mode simulation
β βββ bootstrap_graph.py # Bootstrap graph generation
β βββ command_graph.py # Command graph partitioning
β βββ tool_pool.py # Tool pool assembly
β β
β βββ reference_data/ # JSON snapshot data (drives command/tool metadata)
β β βββ commands_snapshot.json
β β βββ tools_snapshot.json
β β
β βββ commands/ # Command implementation subdirectory
β βββ tools/ # Tool implementation subdirectory
β βββ services/ # Business logic services
β βββ components/ # Terminal UI components (Python version)
β βββ state/ # State management
β βββ types/ # Type definitions
β βββ utils/ # Utility functions
β βββ remote/ # Remote mode
β βββ bridge/ # Bridge modules
β βββ hooks/ # Hook system
β βββ memdir/ # Memory management
β βββ vim/ # Vim mode
β βββ voice/ # Voice mode
β βββ plugins/ # Plugin system
β
βββ tests/ # Validation tests
| Class / Module | Responsibility |
|---|---|
QueryEnginePort |
Query engine handling message submission, streaming output, and session compression |
PortRuntime |
Runtime manager responsible for routing, session startup, and turn-loop execution |
PortManifest |
Workspace manifest that generates Markdown overviews |
ToolPermissionContext |
Tool permission context (allow / deny / ask) |
WorkspaceSetup |
Environment detection and initialization reporting |
TranscriptStore |
Session transcript storage with append, compaction, and replay support |
python3 -m src.main [COMMAND]
# Overview
summary # Markdown workspace overview
manifest # Print manifest
subsystems # List Python modules
# Routing and indexing
commands # List all commands
tools # List all tools
route [PROMPT] # Route prompt to corresponding command/tool
# Execution
bootstrap [PROMPT] # Start runtime session
turn-loop [PROMPT] # Run turn loop (--max-turns)
exec-command NAME # Execute command
exec-tool NAME # Execute tool
# Session management
flush-transcript # Persist session transcript
load-session ID # Load saved session
# Remote mode
remote-mode TARGET # Simulate remote control
ssh-mode TARGET # Simulate SSH branch
teleport-mode TARGET # Simulate Teleport branch
# Audit and config
parity-audit # Compare consistency with TypeScript source
setup-report # Startup configuration report
bootstrap-graph # Bootstrap phase graph
command-graph # Command graph partition view
tool-pool # Tool pool assembly view- Snapshot-driven: command/tool metadata is loaded through JSON snapshots without requiring full logical implementations
- Clean-room rewrite: does not include original TypeScript code; independently implemented
- Parity audit: built-in
parity_audit.pytracks gaps from the original implementation - Lightweight architecture: core framework implemented in 66 files, suitable for learning and extension
| Dimension | claude-code-source-code | claw-code |
|---|---|---|
| Language | TypeScript | Python |
| Code Size | ~163,000 lines | ~5,000 lines |
| Nature | Decompiled source archive | Clean-room architectural rewrite |
| Functional Completeness | Complete (100%) | Architectural framework (~20%) |
| Core Loop | query.ts (785KB) |
QueryEnginePort (~200 lines) |
| Tool System | 40+ fully implemented tools | Snapshot metadata + execution framework |
| Command System | ~87 fully implemented commands | Snapshot metadata + execution framework |
| Main Use Case | Deep study of full implementation details | Architectural understanding and porting research |
This repository is for academic research and educational purposes only. Both subprojects are built from publicly accessible information. Users are responsible for complying with applicable laws, regulations, and service terms.