️ Architecture Overview
Deep dive into Claude Code's 394K lines of TypeScript
Module Dependency Structure
┌─────────────────────────────────────┐
│ Entry Layer │
│ entrypoints/cli.tsx → main.tsx │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ Core Layer │
│ query.ts + QueryEngine │
│ tools.ts + Tool.ts │
│ commands.ts + commands/ │
│ state/ + AppState │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ Service Layer │
│ services/ (api/, mcp/, lsp/) │
└─────────────────────────────────────┘
↓
┌─────────────────────────────────────┐
│ Tool Layer │
│ 54 tools in tools/ │
└─────────────────────────────────────┘
Startup System (cli.tsx)
Three-phase fast path optimization:
Phase 1: Zero-Import Check (<5ms)
// Before any module import
if (args.includes('--version')) {
console.log(VERSION); // Compile-time constant
process.exit(0);
}
Phase 2: Mode Routing (10+ Fast Paths)
--daemon-worker- Daemon worker processremote-control / rc / bridge- Bridge mode--dump-system-prompt- Debug exportmcp- MCP server modesdk- SDK entry mode
Phase 3: Full Initialization
init() → setup() → main()
Compile-Time Feature Flags
Using Bun's bun:bundle module for dead code elimination:
import { feature } from 'bun:bundle';
if (feature('KAIROS')) {
// This code physically doesn't exist in external builds
const assistantModule = require('./assistant/index.js');
}
16 Major Feature Flags
| Flag | Occurrences | Files |
|---|---|---|
| KAIROS | 158 | 57 |
| COORDINATOR_MODE | 32 | 15 |
| BRIDGE_MODE | ~15 | 8 |
| VOICE_MODE | ~10 | 5 |
| PROACTIVE | ~20 | 10 |
| KAIROS_DREAM | ~3 | 2 |
Total: 89 feature flags (16 major + 73 minor/internal)
10 Runtime Modes
- Interactive - Default TUI mode
- Headless/Print -
-pflag, stdout output - Bridge Remote - Remote control via WebSocket
- KAIROS Assistant - 7×24 autonomous mode
- Coordinator - Leader-Worker multi-Agent
- Daemon - Background resident process
- Voice - Real-time voice conversation
- SDK - Programmatic API
- MCP Server - Claude Code as MCP server
- SSH Remote - Remote host sessions
main.tsx Structure (4,683 lines)
- Lines 1-100: Module imports with lazy require
- Lines 100-500: CLI parsing with Commander.js
- Lines 500-1000: Parallel initialization
- Lines 1000-2000: Mode activation (KAIROS, Coordinator)
- Lines 2000-3000: Tool pool construction
- Lines 3000-4000: REPL launch
- Lines 4000-4683: Helper functions
💡 See also: Architecture Reference (MD) for complete source code analysis.