4.9 KiB
4.9 KiB
End-to-end workflows
!!! warning "Recovered source"
Behavior described here is inferred from src/; the shipping product may differ by version or build flags.
Interactive terminal session
- Process start —
main.tsxevaluates side-effect imports (profiler, MDM, keychain prefetch), then parses argv with Commander. preActionhook — Trust dialog state, global config, remote-managed settings, policy limits, GrowthBook/feature flags, OAuth/API key paths, MCP official registry prefetch, plugin initialization, and similar steps run before the subcommand handler.- REPL launch —
replLauncher.tsxcreates the Ink root and renders providers (AppState, keybindings, MCP manager, etc.) and mountsscreens/REPL.tsx. - Input — User typing, paste, voice (if
VOICE_MODE), teammate messages, and task notifications enqueue work. - Queue —
utils/queueProcessor.tsdequeues slash commands and bash lines individually; batches other same-mode items and callsexecuteInput. - Query —
query.tsqueryLoopstreams model output, schedules tool calls, applies permission checks, and updates transcript state. - Tools —
services/tools/toolExecution.ts/StreamingToolExecutorand per-tool modules undertools/*perform filesystem, shell, web, MCP, and agent operations. - Compaction — When context limits approach,
services/compact/*reduces history per compaction policy.
Official parallels: Interactive mode, How Claude Code works.
Print mode / stream-json / programmatic control
- Routing —
main.tsxselects the print/headless path instead of the full REPL when-p/ related flags are used. - Structured I/O —
cli/structuredIO.tsparses lines into typed events;cli/print.tsrunHeadlessStreamingruns the async loop that consumes user/control messages and emits stdout messages. - Engine —
QueryEngine.submitMessagebuildsprocessUserInputContextfor non-interactive sessions and streams SDK-style messages.
Official parallels: Run Claude Code programmatically, Agent SDK overview (external platform docs).
Teammates / agent teams
- Spawn — Swarm backends (
utils/swarm/backends/*) create tmux panes, iTerm panes, or in-process runners. - Teammate loop —
utils/swarm/inProcessRunner.tsruns awhileloop: build prompt messages, check compaction/token limits, call into shared query/tool paths, sync permissions vialeaderPermissionBridge, etc. - REPL integration —
REPL.tsxwiresuseInboxPoller,useMailboxBridge, andhandleIncomingPromptso teammate messages become user turns.
Official parallels: Agent teams, Subagents.
Hooks (user-configured)
- Session start —
utils/sessionStart.tsprocessSessionStartHooks/processSetupHooksrun after configuration is known; wired from startup paths inmain.tsxand related modules. - Per-event hooks — Additional hook types align with the schema described in the official Hooks reference; implementation is spread across hook runners and tool lifecycle code.
Official parallels: Hooks guide, Hooks reference.
Compaction and context
- Triggers — Token estimation and session memory modules decide when to compact.
- Pipeline —
services/compact/compact.ts,autoCompact.ts,microCompact.ts, and related files rewrite or summarize message history. - Post-compact — Cleanup hooks update UI and storage.
Official parallels: Context window, Checkpointing, Costs.
MCP and external tools
- Config —
services/mcp/config.ts, env expansion, enterprise allowlists. - Connection —
MCPConnectionManager.tsx, stdio and SDK transports. - Tool surface —
tools/MCPTool,ReadMcpResourceTool,ListMcpResourcesTool,McpAuthTool.
Official parallels: MCP, Channels.
Optional sequence (interactive turn)
sequenceDiagram
participant User
participant REPL as REPL.tsx
participant Queue as queueProcessor
participant Query as queryLoop
participant API as services/api
participant Tool as tools/*
User->>REPL: input / slash command
REPL->>Queue: enqueue
Queue->>REPL: executeInput
REPL->>Query: onQuery messages
Query->>API: stream completion
API-->>Query: tool_use
Query->>Tool: execute
Tool-->>Query: result
Query-->>REPL: update transcript