Claude Code first-launch initialization
Overview
This scenario traces the cold-start path from the claude CLI binary through fast-path dispatch, config/auth/settings loading, telemetry bootstrap, MCP discovery, and into the first interactive turn . The entry is the bootstrap shim that defers all heavy imports until after version and special-flag fast paths .
Steps
- The process starts in the bootstrap shim which sets
COREPACK_ENABLE_AUTO_PIN=0and optionalNODE_OPTIONSheap flags at module top-level before any dynamic import runs . It then callsmain()withprocess.argv.slice(2). main()short-circuits on--version, then loads the startup profiler and markscli_entryfor later timing analysis . Subsequent branches dispatch fast-paths like--daemon-worker,remote-control,daemon,ps/logs/attach/kill, andenvironment-runnerbefore the normal Commander program is built .- For a normal launch, Commander's
preActionrunsenableConfigs()and kicks off managed-settings download, then the settings loader merges plugin base, user, project, local, flag, and policy sources into the session cache . Each source file is parsed and schema-validated with a per-file cache . - Managed/MDM and HKCU layers are layered in, with the remote-managed layer consulted first for policy settings; a cache flush guards against poisoned merged reads from an earlier
getSettings_DEPRECATEDcall . Settings env vars are then applied intoprocess.envso downstream code sees user-configured variables . - Auth is resolved: if
CLAUDE_CODE_OAUTH_REFRESH_TOKENis present the refresh token is exchanged and persisted viainstallOAuthTokens, otherwise the stored OAuth tokens or API key source are used; subscription type is derived from those stored tokens . - Telemetry is initialized:
bootstrapTelemetrycopiesANT_OTEL_*vars to theirOTEL_*counterparts and defaults metrics temporality to delta .initializeTelemetrythen strips console exporters when stdout is the SDK channel, builds resource attributes viagetPlatform, and constructs meter/logger/tracer providers registered through state setters . - Telemetry readers are selected:
getOtlpReaders/getOtlpLogExporters/getOtlpTraceExportersparse exporter types and protocols; BigQuery metrics are additionally enabled for 1P API, C4E, and Team users . A shutdown hook is registered so providers flush on exit . - MCP server discovery reads project/user/local/enterprise scopes, walking parent directories for
.mcp.jsonproject configs . Each configured server may have aheadersHelperthat is gated on trust-dialog acceptance before being invoked . - Workspace trust is resolved via
TrustDialog, which inspects MCP servers, hooks, bash permission sources, apiKeyHelper, OTEL headers helper, and dangerous env var sources to decide whether to prompt . Policy limits are also fetched and cached for gating features like remote control . - In headless/print mode,
runHeadlesssubscribes to settings changes, initializes sandbox, loads initial messages, and dispatches torunHeadlessStreamingwhich registers a SIGINT handler and drains the command queue . The SDKinitializecontrol request is answered with commands, agents, models, and account info before the first prompt is accepted . - The first turn's
run()awaits deferred plugin install, refreshes plugin state, assembles MCP clients from app state plus SDK and dynamic clients, and callsdrainCommandQueuewhich forwards the prompt toQueryEngine.submitMessagefor the first interactive response .
State touched
Decisions
(none in whitelist)