Module Boundaries, Layers, State Models, And Schema Contracts
Core Runtime Layers
Entry and bootstrap layer
Files: entrypoints/cli.tsx, main.tsx, entrypoints/init.ts, setup.ts.
Responsibility: process boot, trust-sensitive initialization, and branch selection between interactive, headless, and bridge modes.
Session state and config layer
Files: , , , , .
Responsibility: process-global counters, user and project settings, global config, and mutable AppState.
Integration and service layer
Files: services/*, bridge/*, remote/*, upstreamproxy/*.
Responsibility: MCP, analytics, OAuth, remote managed settings, bridge and CCR, plugin operations, policy limits, and other integrations.
Platform and helper layer
Files: utils/*, types/*, constants/*, schemas/*.
Responsibility: filesystem, git, session storage, permissions, plugins, telemetry, text processing, and shared schemas.
Most Important Shared Data Models
Tool Contract
Tool.ts defines the shared tool contract and ToolUseContext.
ToolUseContext carries the current tools, commands, MCP clients and resources, app state getters and setters, permission context, messages, and turn-scoped helpers.
ToolPermissionContext is the normalized permission state passed through the runtime.
Why it matters: every model-callable tool, permission handler, and tool execution path depends on this shape.
AppState
state/AppStateStore.ts defines AppState.
It contains session settings, command and tool state, plugin and MCP state, notifications, tasks, attribution and file-history state, bridge and remote status, and many UI toggles.
Why it matters: it is the shared contract between runtime core and both surfaces, the interactive REPL and headless session-state notifications.
Query Contracts
QueryEngineConfig in QueryEngine.ts describes the long-lived conversation owner for headless and SDK flows.
QueryParams in query.ts describes one low-level turn.
Why it matters: QueryEngine owns conversation persistence across turns; query.ts owns one agentic turn.
Settings Schemas
utils/settings/types.ts defines SettingsJson, PermissionsSchema, environment variable schema, marketplace schema, and MCP allow and deny entries.
utils/settings/settings.ts loads, merges, caches, validates, and updates those settings across sources.
Project And Global Config
utils/config.ts defines ProjectConfig and GlobalConfig.
These are file-backed client state rather than desired behavior settings. They hold onboarding, trust, worktree state, plugin metadata, preferences, and recorded session state.
MCP Configuration And Connection Types
services/mcp/types.ts defines transport schemas such as stdio, sse, http, ws, and sdk, plus scope values and runtime connection states.
services/mcp/config.ts builds the merged MCP config set.
services/mcp/client.ts turns config into live connections plus tool, resource, and command exposure.
Import Relationship Hotspots
Module
Incoming imports
ink.ts
429
utils/debug.ts
370
services/analytics/index.ts
264
bootstrap/state.ts
260
utils/errors.ts
248
utils/log.ts
243
utils/envUtils.ts
220
utils/slowOperations.ts
206
utils/config.ts
155
utils/settings/settings.ts
138
utils/messages.ts
134
services/analytics/growthbook.ts
131
state/AppState.tsx
108
utils/cwd.ts
104
utils/auth.ts
104
utils/format.ts
102
utils/fsOperations.ts
96
utils/lazySchema.ts
95
keybindings/useKeybinding.ts
94
utils/stringUtils.ts
82
Interpretation:
ink.ts is the UI facade shared throughout the interactive client.
bootstrap/state.ts, Tool.ts, and commands.ts are system-wide contracts.
utils/debug.ts, utils/errors.ts, utils/log.ts, and utils/config.ts are pervasive platform helpers.
services/analytics/index.ts is cross-cutting because feature flags and telemetry are consulted all over the runtime.
Circular Dependency Notes
Visible cycle: main.tsx -> dialogLaunchers.tsx -> interactiveHelpers.tsx -> main.tsx. This exists because startup helpers and one-off dialog launchers are split out of main.tsx but still need its deferred prefetch helper.
Visible cycle: utils/stats.ts <-> utils/statsCache.ts. Small helper and cache pair.
Several tool and UI pairs have local cycles, such as tools/RemoteTriggerTool/*, tools/SendMessageTool/*, tools/TeamCreateTool/*, and tools/TeamDeleteTool/*.
The larger dependency knot is mostly architectural density around shared command, UI, runtime, and state contracts rather than one startup-only bug.