How andytonglove/claude-code-leak works
1. Overall shape
- This is a Bun + TypeScript terminal application built around a React/Ink UI.
- The architecture is not a simple command runner: it is an interactive agent workspace with a persistent app state, a tool-execution loop, many slash commands, and multiple integration surfaces.
2. Startup and bootstrapping
- src/main.tsx, src/entrypoints/, and src/bootstrap/ form the startup path.
- Startup does early environment setup, settings loading, keychain/prefetch work, feature-flag decisions, and then routes into either the main REPL/session UI or specialized entrypoints like CLI, MCP, or sandbox-related modes.
3. Core orchestration
- src/QueryEngine.ts, src/Task.ts, src/Tool.ts, and src/tools.ts are the central engine boundary.
- The query engine manages the LLM conversation loop, streaming assistant responses, tool calls, retries, and turn completion.
- Tools are typed modules with schemas, permissions, and result rendering; tasks represent running work units across local, remote, shell, and teammate contexts.
4. Interactive terminal UI
- src/components/, src/screens/, src/ink/, src/context/, and src/state/ provide the terminal application shell.
- Ink is the rendering/runtime layer; React components implement prompt input, message timelines, dialogs, status lines, diff views, permission prompts, onboarding, and background task views.
- Global app state is centralized and shared through state/context hooks rather than ad hoc component-local state.
5. User input to agent loop
- User input flows through prompt handling, slash-command processing, and tool/request creation.
- The main path is: terminal input → command/prompt parsing → context assembly → query engine call → streamed assistant output → tool invocation(s) → UI updates.
- This is the primary architecture boundary between interactive front-end behavior and backend agent execution.
6. Tool execution and safety
- src/tools/ contains the operational surface area: shell, file edit/read/write, grep/glob, web fetch/search, MCP, LSP, notebook editing, agent spawning, task management, messaging, scheduling, and plan/worktree controls.
- Tool execution is mediated by src/services/tools/ and extensive validation in src/utils/permissions/, src/hooks/toolPermission/, and tool-specific validation helpers.
- Permissioning is a first-class boundary: every potentially dangerous action can be rejected, auto-resolved, or routed through a UI approval dialog.
7. Command system
- src/commands/ is the user-facing slash-command layer.
- Commands cover session lifecycle, config, login/logout, memory, tasks, review, diff, cost, theme, sandbox, plugins, MCP, resume, and many workflow-oriented operations.
- Many commands are thin orchestration wrappers around shared services and UI flows rather than self-contained business logic.
8. External integrations and service layer
- src/services/ contains the integrations that leave the local process boundary: Anthropic API, OAuth, analytics, plugin loading, LSP server management, MCP connection management, compacting/context reduction, policy limits, and remote-managed settings.
- This layer is where the app talks to network services, background daemons, auth providers, and external plugin or protocol servers.
- It is the main separation between core agent logic and infrastructure concerns.
9. Bridge and remote-control path
- src/bridge/, src/remote/, and src/server/ implement remote/IDE-connected operation.
- The bridge subsystem provides bidirectional messaging, session control, permissions callbacks, and transport adaptation for IDE integrations and REPL-style remote control.
- Remote sessions, direct-connect server mode, and session resume are separate from the local terminal UI but reuse the same underlying task/query model.
10. Multi-agent and task coordination
- src/coordinator/, src/tasks/, src/tools/AgentTool/, src/utils/swarm/, and src/components/tasks/ together implement parallel work and agent swarms.
- A “leader” session can spawn subagents, create teams, send messages, track background shell jobs, and surface progress in the UI.
- These flows introduce a second execution dimension besides the main conversation: asynchronous work items with their own state, outputs, and lifecycle.
11. Plugins, skills, and MCP extensibility
- src/plugins/, src/skills/, and src/services/mcp/ are the extension system.
- Skills are reusable workflows invoked by tools/commands; plugins contribute commands, tools, hooks, and output styles; MCP connects Claude Code to external tool servers.
- The architecture treats these as loadable capability bundles, with trust, validation, and startup checks forming the boundary before activation.
12. Memory, context, and session persistence
- src/context.ts, src/history.ts, src/assistant/sessionHistory.ts, src/memdir/, src/services/SessionMemory/, and src/services/extractMemories/ manage long-lived conversational and project memory.
- Context is assembled from user input, file history, repository state, memory files, session history, and tool outputs before each model call.
- Compacting, extraction, and memory sync are designed to reduce context growth while preserving useful project knowledge.
13. Configuration, permissions, and policy
- src/utils/settings/, src/migrations/, src/constants/, and permission-related utilities govern runtime behavior.
- Settings are versioned and migrated; policy limits and managed settings can override local choices; permission rules constrain shell/filesystem/network actions.
- This is a strong architectural boundary because it determines what the agent is allowed to do, not just what it can do.
14. Observability and operational support
- src/services/analytics/, src/utils/telemetry/, src/cost-tracker.ts, and diagnostic/doctor commands provide runtime visibility.
- The app tracks usage, token/cost metrics, internal events, and health checks, and exposes them through commands and status views.
- Operational modules are deliberately separate from agent logic so they can be enabled, disabled, or gated independently.
15. What an architecture diagram should emphasize
- The central hub is the query/tool loop: UI input and session state feed the query engine, which delegates to tools, permissions, and services, then streams results back into the UI.
- The major surrounding rings are interactive UI, integrations/services, remote/bridge connectivity, and extension mechanisms.
- The most important boundaries are: terminal UI vs. agent engine, engine vs. external services, and local session vs. remote/IDE-connected session.
Main entry points: services Services · QueryEngine.ts Query Engine · commands Commands · main.tsx main
Components 24
Startup
Bootstrapping, entrypoints, and session launch paths.
- main · Top-level process startup and mode selection.
src/main.tsx - Entrypoints · CLI, MCP, init, and SDK entry surfaces.
src/entrypoints - Bootstrap · Early environment, settings, and keychain/session preflight.
src/bootstrap
Terminal UI
Ink/React rendering, screens, components, state, and context.
- REPL Screen · Primary interactive session screen and resume flow.
src/screens/REPL.tsx - App Shell · Root terminal app composition and top-level layout.
src/components/App.tsx - App State · Centralized application state and selectors/hooks.
src/state - Prompt Flow · User input, slash commands, and prompt handling.
src/utils/processUserInput - Dialogs · Permission prompts, settings, diffs, onboarding, and status dialogs.
src/components
Agent Engine
Query loop, task model, tools, permissions, memory, and command parsing.
- Query Engine · Conversation loop, streaming, tool calls, retries, and turn completion.
src/QueryEngine.ts - Task Model · Running work across local, remote, shell, and teammate contexts.
src/Task.ts - Tool API · Typed tool definitions, permissions, and result rendering boundary.
src/Tool.ts - Tool Catalog · Registry of operational tools available to the agent.
src/tools.ts - Tool Runtime · Streaming execution, orchestration, and tool hooks.
src/services/tools - Permissions · Approval, validation, and permission-mode enforcement.
src/utils/permissions - Context & Memory · Conversation context, history, memories, and compaction.
src/context.ts - Commands · User-facing workflow, config, resume, review, and utility commands.
src/commands
Integrations
External services, remote control, plugins, MCP, LSP, analytics, and auth.
- Services · API, auth, analytics, LSP, MCP, plugins, limits, and remote settings.
src/services - Bridge · Bidirectional messaging for IDE and remote session control.
src/bridge - Remote Sessions · Remote session manager and websocket/session coordination.
src/remote - Direct Connect · Direct-connect session creation and server-side session management.
src/server
Extensibility
Plugins, skills, multi-agent workflows, and swarm coordination.
- Plugins · Builtin and loadable plugins and their activation surface.
src/plugins - Skills · Reusable skill workflows and bundled capabilities.
src/skills - Coordinator · Leader/session coordination for parallel work and swarms.
src/coordinator - Swarm Utils · Spawn, permission sync, teammate layout, and in-process/back-end support.
src/utils/swarm
Connections 26
- main → Bootstrap: initializes · Bootstraps environment and runtime state before app launch.
- main → Entrypoints: routes · Dispatches to CLI, MCP, init, or other entry modes.
- main → REPL Screen: opens · Starts the interactive terminal session UI.
- Entrypoints → REPL Screen: launches · Entry modes can create or resume the session UI.
- REPL Screen → App Shell: renders · REPL screen is composed through the app shell.
- App Shell → App State: uses · UI reads and writes centralized shared state.
- App Shell → Prompt Flow: accepts input · Prompt handling feeds user commands and text into the system.
- Prompt Flow → Commands: parses slash cmds · Slash commands are resolved through the command layer.
- Prompt Flow → Query Engine: submits prompt · Regular user input becomes a query-engine turn.
- Query Engine → Tool API: invokes · The engine dispatches typed tool calls.
- Tool API → Tool Catalog: implemented by · Concrete tools are registered in the catalog.
- Tool Catalog → Tool Runtime: executes via · Tool definitions run through the tool execution services.
- Tool Runtime → Permissions: checks · Dangerous actions pass through permission and validation gates.
- Query Engine → Context & Memory: builds context · Conversation state, history, and memory are assembled into each turn.
- Commands → Services: orchestrates · Commands wrap service-layer operations and background flows.
- Services → Bridge: feeds · Remote control and IDE integration use bridge messaging.
- Services → Remote Sessions: manages · Remote session lifecycle is handled by remote services.
- Remote Sessions → Direct Connect: connects · Remote sessions can be created through direct-connect server mode.
- Services → Plugins: loads · Plugin loading, policy, and startup checks come from services.
- Services → Skills: enables · Skill loading and skill-based workflows are activated through services.
- Coordinator → Swarm Utils: uses · Coordinator flows depend on swarm/spawn utilities.
- Coordinator → Task Model: spawns · Parallel work creates additional task units.
- Query Engine → Task Model: tracks · Agent turns and background work are tracked as tasks.
- Commands → Bridge: controls · Bridge-related commands expose session control and diagnostics.
- Commands → Coordinator: manages · Agent/team commands surface multi-agent coordination.
- Permissions → Dialogs: surfaces · Permission decisions are often resolved in UI dialogs.