Core Architecture

main.tsx

The Entry Point - Where Everything Begins

4,683 Lines
803KB Size
src/ Location
TSX Language
01

What This File Does

main.tsx is the primary entry point for Claude Code. When you run claude in your terminal, this is the first file that executes. It's responsible for the entire application lifecycle - from parsing CLI arguments to rendering the interactive UI.

CLI Bootstrap

Parses arguments, sets up Commander.js commands

🎨

React + Ink UI

Renders the terminal interface using React and Ink

🔒

Auth & Security

Loads API keys from Keychain, manages trust dialogs

🚀

Performance

Parallel pre-loading saves ~135ms on startup

02

Startup Sequence (Line by Line)

main.tsx - Lines 1-30: Critical Side Effects
// STEP 1: Performance Profiling (Before ANY imports)
import { profileCheckpoint } from './utils/startupProfiler'
profileCheckpoint('main_module_eval_start')

// STEP 2: Parallel Pre-Loading (MDM + Keychain)
// These fire BEFORE heavy module evaluation (~135ms)
import { startMdmRawRead } from './utils/mdm'
startMdmRawRead()  // Spawns subprocess to read MDM config

import { startKeychainPrefetch } from './utils/keychainPrefetch'
startKeychainPrefetch()  // Reads OAuth + API key from macOS Keychain

// STEP 3: Heavy Module Evaluation (~135ms of imports)
import { CommanderCommand } from '@commander-js/extra-typings'
import React from 'react'
import { feature } from 'bun:bundle'  // Build-time feature gating
// ... 100+ more imports
Performance Insight

The ordering here is critical for performance. By firing MDM reads and Keychain prefetch BEFORE the heavy imports, the I/O happens in parallel with JavaScript module evaluation, saving ~135ms on startup.

03

Feature Gating System

main.tsx - Conditional Feature Loading
// Build-time dead code elimination
if (feature('COORDINATOR_MODE')) {
  // This entire block is removed from the bundle
  // if COORDINATOR_MODE is not enabled at build time
  const { coordinatorMode } = require('./coordinator')
}

if (feature('KAIROS')) {
  // Proactive assistant mode - conditionally loaded
  const { kairosInit } = require('./assistant')
}

if (feature('VOICE_MODE')) {
  // Voice input/output - Speech-to-Text support
  const { voiceInit } = require('./services/voice')
}
📚
Build-Time vs Runtime

Bun's feature() function enables build-time dead code elimination. If a feature flag is false at build time, the bundler completely removes the code block, reducing bundle size. This is different from runtime feature flags (GrowthBook) which check at execution time.

COORDINATOR_MODE Multi-agent orchestration
KAIROS Proactive assistant
VOICE_MODE Speech-to-Text input
DREAM Background maintenance
ULTRAPLAN Advanced planning
UNDERCOVER Stealth mode
04

Key Functions

init()

Initializes the session: sets up telemetry, loads trust dialogs, prepares the working directory.

Lifecycle
fetchBootstrapData()

Loads initial configuration from Anthropic's first-party API: model options, feature flags, remote settings.

Network
getTools()

Assembles the complete tool list (45+ tools) with feature-gated conditional loading.

Tools
renderAndRun()

Starts the main interactive loop: renders the Ink terminal UI and enters the REPL (Read-Eval-Print Loop).

UI
showSetupScreens()

Displays onboarding, trust dialogs, and configuration screens for first-time users.

Onboarding
💡 Try This Experiment

Clone the repo and open src/main.tsx. Count how many imports happen before line 30. Can you spot the parallel pre-loading optimization?

git clone https://github.com/anthropics/claude-code.git wc -l src/main.tsx head -30 src/main.tsx