ruvector/docs/research/claude-code-rvsource/03-agent-loop-and-execution-flow.md
rUv 930fca916f feat(sse): decouple SSE to mcp.pi.ruv.io proxy + Claude Code source research
SSE Proxy Decoupling (ADR-130):
- Fix ruvbrain-sse proxy: proper MCP handshake, session creation, drain polling
- Fix internal queue endpoints: session_create keeps receiver, drain returns buffered messages
- Add response_queues to AppState for SSE proxy communication
- Skip sparsifier for >5M edge graphs (was crashing on 16M edges)
- Add SSE_DISABLED/MAX_SSE env vars for configurable connection limits
- Route SSE to dedicated mcp.pi.ruv.io subdomain (Cloudflare CNAME)
- Serve SSE at root / path on proxy (no /sse needed)
- Update all references from pi.ruv.io/sse to mcp.pi.ruv.io
- Fix Dockerfile consciousness crate build (feature/version mismatches)

Claude Code CLI Source Research (ADR-133):
- 19 research documents analyzing Claude Code internals (3000+ lines)
- Decompiler script + RVF corpus builder for all major versions
- Binary RVF containers for v0.2, v1.0, v2.0, v2.1 (300-2068 vectors each)
- Call graphs, class hierarchies, state machines from minified source

Integration Strategy (ADR-134):
- 6-tier integration plan: WASM MCP, agents, hooks, cache, SDK, plugin
- Integration guide with architecture diagrams and performance targets

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-04-02 23:39:56 +00:00

5.9 KiB

Claude Code CLI: Agent Loop and Execution Flow

Entry Points

CLI Entry

The binary boots through Bun's SEA mechanism into a Commander.js-style CLI:

claude [options] [command] [prompt]

Subcommands (24 registered):

  • auth, login, logout -- Authentication
  • config (list/show/set/defaults) -- Configuration management
  • mcp (add-from-claude-desktop, list, serve, status, setup) -- MCP server management
  • doctor -- Diagnostics
  • update -- Self-update
  • agents -- Agent management
  • marketplace, plugin -- Plugin/skill management
  • remote-control, serve -- Remote/SSE modes
  • auto-mode -- Auto permission mode
  • setup-token, xaa -- Auth helpers
  • critique, reset-project-choices, clear -- Misc utilities

VS Code Extension Entry

extension.js registers VS Code commands and creates a WebView panel. The WebView (webview/index.js) is a React 18.3.1 application that communicates with the extension host via postMessage.

VS Code commands (21 registered):

  • claude-vscode.editor.open / openLast / primaryEditor.open
  • claude-vscode.window.open
  • claude-vscode.createWorktree
  • claude-vscode.insertAtMention
  • claude-vscode.focus / blur
  • claude-vscode.terminal.open.keyboard

Main Loop Architecture

Core Loop Functions

Function Purpose
mainLoop Primary conversation loop
mainLoopModel Model selection for current session
mainLoopModelForSession Session-level model override
mainLoopModelOverride CLI/env model override
mainLoopTokens Token tracking for main loop
processMessagesForTeleportResume Handle session teleport/resume

Execution Flow

1. CLI Parse / VS Code Activation
     |
2. Configuration Loading
   - User settings (~/.claude/settings.json)
   - Project settings (.claude/settings.json, .claude/settings.local.json)
   - CLAUDE.md files (auto-discovered up directory tree)
   - Environment variables (498+ recognized)
     |
3. Authentication
   - AnthropicApiKeyAuth (API key from env/helper)
   - ConsoleOAuthFlow (Anthropic Console OAuth)
   - BedrockClient (AWS Bedrock)
   - VertexAI (Google Cloud)
   - Foundry (Azure)
     |
4. MCP Server Connection
   - Read .mcp.json, settings, --mcp-config
   - Connect to MCP servers (stdio, SSE, HTTP transports)
   - Discover tools via tools/list
     |
5. Session Init / Resume
   - Create new session or resume existing
   - Load conversation history
   - Apply CLAUDE.md system prompt
     |
6. Main Loop (per turn):
   a. Collect user input (interactive) or read prompt (--print)
   b. Build message array with system prompt + conversation history
   c. Call Anthropic Messages API (streaming)
   d. Process response stream:
      - Text blocks -> render to terminal/UI
      - Thinking blocks -> display if enabled
      - tool_use blocks -> queue for execution
   e. For each tool_use:
      - Check permissions (PreToolUse hooks)
      - Execute tool
      - Run PostToolUse hooks
      - Append tool_result to messages
   f. If model stopped with tool_use (not end_turn):
      - Loop back to step (c) with updated messages
   g. If model stopped with end_turn:
      - Display final response
      - Wait for next user input
     |
7. Context Management
   - Track token usage
   - Auto-compact when context window fills
   - File checkpointing for recovery
     |
8. Session End
   - Run session-end hooks
   - Persist session to disk
   - Cleanup MCP connections

Streaming Architecture

The API response is streamed using Server-Sent Events (SSE):

StreamEvent -> StreamEventBuffer -> StreamHandler -> UI Renderer

Key streaming patterns:

  • StreamFriendlyUIEnabled -- Controls rich terminal rendering
  • StreamInterval -- Configurable update frequency
  • StreamFinished / StreamFinishing -- Completion states
  • Partial message chunks available via --include-partial-messages

Conversation Management

  • ConversationManager -- Manages conversation state
  • ConversationChain -- Message chain/history
  • MessageBuffer / MessageBufferTracker -- Buffer management
  • MessageByUuid -- Message lookup
  • MessageExistInSession -- Deduplication

Slash Commands (39 Built-in)

Interactive commands processed before sending to the API:

Command Purpose
/compact Force context compaction
/clear Clear conversation
/model Switch model
/effort Set reasoning effort level
/fast Toggle fast mode
/permissions View/edit permission rules
/hooks View configured hooks
/mcp MCP server status
/memory View/edit auto-memory
/status Session status
/review-pr PR review workflow
/commit Commit workflow
/init Project initialization
/login / /logout Authentication
/doctor Run diagnostics
/bug Report a bug
/cost Show cost information
/extra-usage Usage details
/feedback Submit feedback
/config Configuration
/skills List available skills
/agents Agent management
/tasks Task/subagent list
/worker Background workers
/branch Create feature branch
/teleport Session teleport
/rewind Rewind conversation
/resume Resume session
/loop Recurring command
/chrome Chrome integration
/powerup Power-up features
/deploy Deployment workflow
/dev Development mode
/issue Issue tracker
/metrics Performance metrics
/dashboard Dashboard view
/ultrareview Deep code review
/prompt Prompt management
/install-github-app GitHub App setup

Output Modes

--output-format text         # Default: human-readable
--output-format json         # Single JSON result
--output-format stream-json  # Real-time streaming JSON

Input modes:

--input-format text          # Default: plain text prompt
--input-format stream-json   # Real-time streaming JSON input