Claude Code reverse-engineering wiki
This wiki documents the extracted @anthropic-ai/claude-code runtime with source-anchored implementation notes. The primary readable artifact is:
claude-code-pkg/src/entrypoints/cli.js
The wiki is organized around source-anchored internals questions: how the Bun standalone runtime starts, routes command-line modes, shapes model context, exposes tools, persists sessions, supports remote control, runs agents, and exposes operational/debug/native surfaces.
Because cli.js is bundled/minified, symbol names are unstable. Source anchors are intended for searching the analyzed bundle, not as public API names. Implementation pages use Semantic alias as the first source-anchor table column, followed by the concrete Source, approximate location, and exact string/symbol for this build.
Semantic alias and minified anchor mapping
This home page is a navigation index, not a direct cli.js implementation analysis. Concrete topic pages map stable semantic aliases to version-specific minified anchors.
| Semantic alias | Minified anchor | Scope |
|---|---|---|
| Wiki home | N/A — navigation page | Orients readers to the canonical sections and reading paths. |
| Section indexes | N/A — see linked section README pages | Curated reader routes through source-anchored implementation pages. |
| Topic implementation pages | See page-level source anchor tables | Bundle-specific anchors live in focused implementation documents. |
Internals wiki map
flowchart TD Home[Wiki home] --> Start[00 Start here] Home --> Runtime[01 Runtime lifecycle] Home --> Loop[02 Context and model loop] Home --> Tools[03 Tools, integrations, and security] Home --> Sessions[04 Sessions, persistence, and remote] Home --> Hosted[05 Hosted agent ops] Home --> Agents[06 Agents and automation]
Start --> Runtime Runtime --> Loop Loop --> Tools Runtime --> Sessions Sessions --> Hosted Loop --> Agents
click Home "./" "Open wiki home" click Start "./00-start-here/" "Open Start here" click Runtime "./01-runtime-lifecycle/" "Open Runtime lifecycle" click Loop "./02-context-model-loop/" "Open Context and model loop" click Tools "./03-tools-integrations-security/" "Open Tools, integrations, and security" click Sessions "./04-sessions-persistence-remote/" "Open Sessions, persistence, and remote" click Hosted "./05-hosted-agent-ops/" "Open Hosted agent ops" click Agents "./06-agents-automation/" "Open Agents and automation"Canonical sections
| Section | Purpose |
|---|---|
| Start here | Minimal orientation: what the bundle is, how to read anchors, and the first path through the runtime. |
| Runtime lifecycle | Package/Bun bootstrap, CLI flags, root command routing, headless/interactive modes, and subcommands. |
| Context and model loop | Prompt/context sources, memory/settings, model/provider/auth selection, and headless stream-JSON turns. |
| Tools, integrations, and security | Built-in tools, permissions, MCP, plugins, hooks, settings policy, IDE/Chrome/file integrations. |
| Sessions, persistence, and remote | Local JSONL transcripts, resume/continue/fork/rewind, remote sessions, teleport, and Remote Control. |
| Hosted agent ops | Debug logs, telemetry/traffic policy, doctor/update, hosted review signals, and native image/audio modules. |
| Agents and automation | Custom/background agents, task/subagent tools, lifecycle hooks, slash commands, and auto-mode. |
Recommended reading paths
Cross-cutting implementation matrix
Mechanism-level deep dives
| Mechanism | Start here |
|---|---|
| Semantic aliases, minified anchors, and terminology | Glossary and aliases |
| System module boundaries and data/control flow | System architecture |
| Runtime protocol families across modules, agents, bridges, and remote servers | Runtime communication protocols |
| Command-line flags, subcommands, and aliases | Command-line reference |
| Per-module architecture deep dives | Runtime lifecycle architecture, Context and model loop architecture, Tool runtime and security architecture, Session and remote-control architecture, Operations and native-support architecture, Agent and automation architecture |
| Headless stream/control event loop | Headless streaming and resilience |
| Prompt/template extraction catalog | Prompt template catalog |
| Major prompt assembly scenarios | Prompt assembly scenarios |
| Context compaction, memory selection, checkpoints, and rewind | Context, memory, compaction, checkpoints, and rewind |
| Dynamic model selection, provider calls, usage, quota, and billing | Model selection, calls, usage, quota, and billing |
| Tool runtime, events, shell execution, SDK/LSP/Web, context exclusion, settings, and persistence | Tool runtime, events, and integration flows |
| Tool names, schema owners, and tool-family reference | Tool inventory and schemas |
| Tool authorization and execution boundary | Built-in tools and permissions |
| Hook names, lifecycle events, and stream/control frames | Hooks and events reference |
| Known settings roots, keys, and policy groups | Settings schema reference |
| Command sandbox design and platform isolation | Sandbox and isolation |
| MCP runtime connection and elicitation | MCP, plugins, and hooks |
| Resume/continue/fork state restoration | Session resume and transcripts |
| Session API, event, storage, and remote-control reference | Session API, events, and storage |
| Session data models, transcript records, and frame schemas | Data models and frame schemas |
| Diagnostics and debug logs | Diagnostics and debug logs |
| Telemetry and tracing | Telemetry and tracing |
| Feature gates | Feature gates reference |
| Updater and doctor | Updater and doctor |
| Environment variable reference | Environment variables reference |
| Task/subagent runtime behavior | Agents, tasks, and subagents |
| Agent task scheduling, completion, and cron | Agent runtime, scheduling, and completion |
| Voice capture, transcription stream, and prompt injection | Audio capture and voice mode |
Source policy
- Treat
claude-code-pkg/src/entrypoints/cli.jsas the primary readable source artifact. - Use approximate line numbers plus byte offsets because the file is bundled and many logical modules sit on very long lines.
- Preserve exact strings, flags, environment variables, and symbols in source-anchor tables.
- Do not treat
.jscbytecode dumps as recoverable JavaScript source. - Keep broad synthesis pages, implementation pages, and reference pages distinct; promote confirmed findings into the focused section page that owns the behavior.
Important caveat
These pages document a bundled/minified production artifact, not clean source. Semantic names in prose and diagrams are explanatory aliases. Generated/minified symbols are retained only when useful as search anchors for the analyzed bundle.
Created and maintained by Yingting Huang.