State Management Architecture
Overview
Claude Code uses a centralized store pattern for state management, similar to Zustand or Redux but custom-built. State is immutable and updates flow through a single pipeline.
Shared from "Claude-Code" on Inkdown
Claude Code uses a centralized store pattern for state management, similar to Zustand or Redux but custom-built. State is immutable and updates flow through a single pipeline.
| File | Purpose |
|---|---|
state/AppStateStore.ts | State type definitions + default state |
state/AppState.tsx | React provider + useAppState hook |
state/store.ts | Store implementation (subscribe/getState/setState) |
bootstrap/state.ts | Pre-React bootstrap state |
Custom store provides:
useAppStateuseSetAppStateClaude Code uses DeepImmutable types for safety:
This prevents accidental mutations at compile time.
Messages are the primary source of truth for conversation state.
Background tasks have their own state machine:
Tasks write output to files on disk (outputFile), not memory:
| Mode | Description |
|---|---|
default | Ask for dangerous tools |
auto | Allow after automated safety checks |
bypass | Allow everything (use with caution!) |
Model Context Protocol servers have state:
MCP tools are merged into the main tool list dynamically.
Smart suggestions use a speculation system:
This lets Claude "think ahead" and pre-compute responses.
Conversations are saved to ~/.claude/sessions/{sessionId}.jsonl
Stored in ~/.claude/config.json