How jihaoyb/claude-source-leaked works
1. What this repo is
- This repository is primarily a source-analysis companion to Claude Code, but it also contains a full TypeScript source snapshot under src/.
- Architecturally, the product is a terminal-first AI agent platform: a CLI/REPL, an Ink-based TUI, a tool executor, session/memory systems, and a large integration layer around Anthropic APIs and local machine control.
2. Top-level runtime shape
- The main executable path starts from src/entrypoints/cli.tsx and src/main.tsx, which bootstrap the terminal app.
- The UI is built with React + Ink in src/ink/ and src/components/, not a browser DOM. That means most “screens” are terminal-rendered components, dialogs, and status lines rather than web routes.
3. Command and interaction layer
- src/commands.ts plus the large src/commands/ tree define slash commands, setup flows, diagnostics, and internal tools.
- These commands act as the user-facing control surface for session management, auth, config, plugins, model selection, permissions, remote control, and agent features.
- Input is processed through command parsing and user-input handlers in src/utils/processUserInput/, then routed into the main query/session loop.
4. Conversation orchestration
- The core AI loop is centered on src/QueryEngine.ts, with supporting task abstractions in src/Task.ts and src/tasks/.
- This layer is responsible for building the request, sending it to Claude APIs, handling streaming responses, managing tool calls, and maintaining the turn lifecycle.
- src/assistant/sessionHistory.ts, src/history.ts, src/session* utilities, and src/state/ hold the session timeline and application state that the UI renders.
5. Tool execution boundary
- Tooling is a first-class subsystem in src/tools/ and the lower-level orchestration in src/services/tools/.
- Tools are not just helper functions; they are the controlled execution boundary between model output and real side effects: file operations, shell commands, web fetch/search, MCP calls, LSP queries, tasks, agent spawning, and more.
- The architecture separates tool definition, UI presentation, prompt text, permissions, and execution flow, which is important for diagramming the boundary between “model reasoning” and “system actions.”
6. Permission and safety model
- Permission logic is spread across src/hooks/toolPermission/, src/components/permissions/, and src/utils/permissions/.
- The repo implements multiple approval modes and action classes, with dedicated UI for permission prompts, rule management, sandboxing, and dangerous-command blocking.
- Shell and file tools have additional validation layers in src/tools/BashTool/, src/tools/PowerShellTool/, src/tools/FileEditTool/, src/tools/FileWriteTool/, and related utilities.
7. Context, memory, and compression
- The assistant’s context is assembled from system prompt material, workspace files, memory files, and session history.
- src/context.ts, src/memdir/, src/services/SessionMemory/, and src/services/compact/ form the main context-management subsystem.
- This layer handles memory loading, extraction, compaction, token budgeting, and restoring relevant files after compaction. It is a major architectural boundary because it directly affects what the model can “see.”
8. Local machine and shell integration
- Shell execution, file access, and repository inspection are core local capabilities, implemented through utilities in src/utils/shell/, src/utils/fsOperations.ts, src/utils/fileRead.ts, src/utils/git.ts, and tool-specific code.
- The app is designed to prefer structured local tools over raw shell usage where possible, but shell access remains a major execution path for developer workflows.
- There is also explicit sandboxing support in src/utils/sandbox/ and related UI in src/components/sandbox/.
9. Remote, bridge, and background control
- A significant subsystem exists for remote operation and session bridging: src/bridge/, src/remote/, src/server/, and remote-related commands.
- These files support session creation, remote control transports, websocket/session managers, trusted-device handling, and bridge permissions.
- This is the architecture boundary that turns the local CLI assistant into something that can be resumed, observed, or controlled from other environments.
10. Multi-agent and swarm execution
- The repo has a real multi-agent model, not just a single assistant loop.
- src/tools/AgentTool/, src/utils/swarm/, src/coordinator/, and task types like remote/local agents implement subagents, team workflows, and worker orchestration.
- From an architecture-diagram standpoint, this is a separate lane: the main session can spawn isolated agent contexts with their own models, prompts, and execution state.
11. Integrations and extensibility
- Major integration surfaces include MCP (src/services/mcp/, src/tools/MCPTool/), plugins (src/services/plugins/, src/utils/plugins/), LSP (src/services/lsp/), OAuth/Auth (src/services/oauth/, src/services/api/), and voice (src/services/voice*, src/voice/).
- These are not all equivalent: MCP and plugins extend capabilities, LSP improves code intelligence, OAuth/API services connect to Anthropic backends, and voice adds alternate input/output channels.
- The architecture is modular, with each integration having its own manager, UI, validation, and settings surfaces.
12. Telemetry, settings, and remote policy
- src/services/analytics/ handles event logging and metrics; src/services/settingsSync/ and src/services/remoteManagedSettings/ support centrally managed configuration and policy updates.
- These subsystems matter because they can change runtime behavior remotely, including feature gates, sampling, throttles, and safety-related toggles.
- The repo also contains explicit killswitch and policy-related utilities under src/utils/ and src/services/, which are part of the operational control plane.
13. UI structure
- The terminal UI is composed from reusable React components in src/components/, with many specialized dialogs for onboarding, permissions, tasks, plugins, sessions, and diagnostics.
- State is centralized in src/state/ and consumed by hooks in src/hooks/, while src/context/ provides cross-cutting providers for modal state, notifications, overlays, voice, and mailbox-style messaging.
- For a diagram, it helps to show the UI as a terminal-rendered front end sitting on top of the state/store layer, not directly on top of the model.
14. Key architectural boundaries to draw
- User input/UI boundary: Ink React terminal UI vs. core session logic.
- Model boundary: QueryEngine and prompt assembly vs. Claude API calls.
- Tool boundary: model intents vs. side-effectful local/remote operations.
- Policy boundary: permissions, sandbox, and remote settings vs. unrestricted execution.
- Extension boundary: plugins/MCP/LSP/voice/agents vs. built-in core behavior.
- Control-plane boundary: telemetry, sync, bridge, and remote management vs. local interactive use.
15. Repo organization note
- The analysis/, architecture/, comparison/, and practical/ directories are documentation built from the source snapshot; they are useful for understanding the system, but they are not the runtime architecture itself.
- The runtime architecture lives in src/, with TypeScript as the primary implementation language and a strong bias toward modular, feature-gated subsystems.
Main entry points: QueryEngine.ts Query Engine · tools Tool Orchestration · components Components · analytics Ops Control
Components 21
Runtime shell
Terminal entrypoints, Ink UI, state, and interaction loop.
- Entrypoints · Starts the terminal app and CLI modes from the main executable paths.
src/entrypoints/cli.tsx - Ink UI · React/Ink terminal rendering layer for screens, dialogs, and prompts.
src/ink - Components · Reusable terminal components for messages, dialogs, pickers, and overlays.
src/components - App State · Central application state and selectors consumed by UI and hooks.
src/state - Input Flow · Processes slash commands, prompt text, and user input into actions.
src/utils/processUserInput - Commands · User-facing command surface for setup, diagnostics, sessions, plugins, and agent controls.
src/commands.ts
Core agent
Conversation orchestration, context, memory, and session history.
- Query Engine · Builds requests, streams model responses, and manages turn lifecycle.
src/QueryEngine.ts - Tasks · Task types and execution contexts for local, remote, and assistant work.
src/Task.ts - Session History · Conversation timeline, session history, and reusable session utilities.
src/assistant/sessionHistory.ts - Context & Memory · Assembles system prompt, workspace context, memory files, and budgeting.
src/context.ts
Tools & execution
Controlled side-effect boundary and local/remote action execution.
- Tool Catalog · Declared tools and their prompts/UI wrappers that model outputs can invoke.
src/tools - Tool Orchestration · Executes streamed tool calls and coordinates tool hooks and results.
src/services/tools - Shell & Files · Shell, filesystem, git, and repository inspection capabilities.
src/utils/shell
Policy & permissions
Approval, sandboxing, and safety enforcement.
- Permissions · Permission modes, rule matching, prompt handling, and denial tracking.
src/utils/permissions - Sandboxing · Sandbox adapters and UI for constrained or protected execution.
src/utils/sandbox
Integrations
Pluggable external capabilities and remote control surfaces.
- Remote Bridge · Session bridging, transports, trusted-device flows, and remote session control.
src/bridge - Multi-Agent · Agent spawning, team workflows, worker coordination, and swarm execution.
src/utils/swarm - MCP · Model Context Protocol connection management and tool/resource exposure.
src/services/mcp - Plugins · Plugin loading, installation, policy, marketplace, and lifecycle management.
src/services/plugins - LSP · Language server integration for diagnostics, symbols, and recommendations.
src/services/lsp
Operations
Telemetry, sync, settings, and background control-plane subsystems.
- Ops Control · Analytics, settings sync, managed policy, and remote control-plane updates.
src/services/analytics
Connections 22
- Entrypoints → Ink UI: boots · Initializes the terminal app and rendering runtime.
- Ink UI → Components: renders · Composes the TUI from reusable components.
- Components → App State: reads · Displays application state and selection data.
- Input Flow → Commands: routes · Maps user input to slash commands and command handlers.
- Commands → Query Engine: starts · Commands can open or steer the main agent loop.
- Components → Input Flow: collects · Prompt and dialog widgets capture terminal input.
- Query Engine → Session History: records · Persists conversation turns and session timeline.
- Query Engine → Context & Memory: assembles · Builds the model context from prompt, files, memory, and history.
- Query Engine → Tool Orchestration: invokes · Sends model tool calls into the execution boundary.
- Tool Orchestration → Tool Catalog: dispatches · Resolves and runs declared tools.
- Tool Catalog → Shell & Files: uses · Many tools ultimately depend on local shell and filesystem operations.
- Tool Orchestration → Permissions: checks · Tool execution is gated by approval and rule evaluation.
- Tool Orchestration → Sandboxing: constrains · Some actions are executed through sandboxed paths.
- Permissions → Sandboxing: selects · Policy can route risky actions into sandboxed execution.
- Query Engine → Multi-Agent: spawns · Main sessions can create isolated subagents and team workflows.
- Query Engine → MCP: extends · Model interactions can reach external MCP tools and resources.
- Query Engine → Plugins: extends · Plugin-defined commands, tools, hooks, and output styles augment the core loop.
- Query Engine → LSP: consults · Code intelligence feeds diagnostics and symbol-aware assistance.
- Remote Bridge → Query Engine: bridges · Remote sessions can resume or control the same conversation state.
- Remote Bridge → Ops Control: syncs · Remote control is governed by settings, telemetry, and policy updates.
- Ops Control → Permissions: updates · Managed settings and policy can alter approval behavior.
- Ops Control → App State: observes · Operational services feed status, usage, and notifications into app state.