# jihaoyb/claude-source-leaked architecture

> Architecture diagram of the GitHub repository jihaoyb/claude-source-leaked, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jihaoyb/claude-source-leaked
- Repository: https://github.com/jihaoyb/claude-source-leaked

## Overview

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: [`src/QueryEngine.ts`](https://github.com/jihaoyb/claude-source-leaked/blob/main/src/QueryEngine.ts) (Query Engine), [`src/services/tools`](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/tools) (Tool Orchestration), [`src/components`](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/components) (Components), [`src/services/analytics`](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/analytics) (Ops Control)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime shell"]
  node_entrypoints["Entrypoints<br/>CLI bootstrap<br/>[cli.tsx]"]
  node_inkui["Ink UI<br/>terminal UI"]
  node_components["Components"]
  node_state[("App State<br/>store/state")]
  node_inputflow["Input Flow<br/>command parsing"]
  node_commands["Commands<br/>[commands.ts]"]
end

subgraph group_core["Core agent"]
  node_queryengine["Query Engine<br/>agent loop<br/>[QueryEngine.ts]"]
  node_taskmodel["Tasks<br/>task abstractions<br/>[Task.ts]"]
  node_session[("Session History<br/>conversation state<br/>[sessionHistory.ts]")]
  node_contextmem[("Context & Memory<br/>context assembly<br/>[context.ts]")]
end

subgraph group_tools["Tools & execution"]
  node_toolcatalog["Tool Catalog<br/>tool definitions"]
  node_toolexec["Tool Orchestration<br/>tool runtime"]
  node_shellfs["Shell & Files<br/>local execution"]
end

subgraph group_policy["Policy & permissions"]
  node_permissions["Permissions<br/>approval policy"]
  node_sandbox["Sandboxing<br/>execution isolation"]
end

subgraph group_integrations["Integrations"]
  node_remote["Remote Bridge<br/>remote control"]
  node_swarm["Multi-Agent<br/>subagents"]
  node_mcp["MCP<br/>capability bridge"]
  node_plugins["Plugins<br/>extensibility"]
  node_lsp["LSP<br/>code intelligence"]
end

subgraph group_ops["Operations"]
  node_opsctrl["Ops Control<br/>telemetry/settings"]
end

node_entrypoints -->|"boots"| node_inkui
node_inkui -->|"renders"| node_components
node_components -->|"reads"| node_state
node_inputflow -->|"routes"| node_commands
node_commands -->|"starts"| node_queryengine
node_components -->|"collects"| node_inputflow
node_queryengine -->|"records"| node_session
node_queryengine -->|"assembles"| node_contextmem
node_queryengine -->|"invokes"| node_toolexec
node_toolexec -->|"dispatches"| node_toolcatalog
node_toolcatalog -->|"uses"| node_shellfs
node_toolexec -->|"checks"| node_permissions
node_toolexec -->|"constrains"| node_sandbox
node_permissions -->|"selects"| node_sandbox
node_queryengine -->|"spawns"| node_swarm
node_queryengine -->|"extends"| node_mcp
node_queryengine -->|"extends"| node_plugins
node_queryengine -->|"consults"| node_lsp
node_remote -->|"bridges"| node_queryengine
node_remote -->|"syncs"| node_opsctrl
node_opsctrl -->|"updates"| node_permissions
node_opsctrl -->|"observes"| node_state

click node_entrypoints "https://github.com/jihaoyb/claude-source-leaked/blob/main/src/entrypoints/cli.tsx"
click node_inkui "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/ink"
click node_components "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/components"
click node_state "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/state"
click node_inputflow "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/utils/processUserInput"
click node_commands "https://github.com/jihaoyb/claude-source-leaked/blob/main/src/commands.ts"
click node_queryengine "https://github.com/jihaoyb/claude-source-leaked/blob/main/src/QueryEngine.ts"
click node_taskmodel "https://github.com/jihaoyb/claude-source-leaked/blob/main/src/Task.ts"
click node_session "https://github.com/jihaoyb/claude-source-leaked/blob/main/src/assistant/sessionHistory.ts"
click node_contextmem "https://github.com/jihaoyb/claude-source-leaked/blob/main/src/context.ts"
click node_toolcatalog "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/tools"
click node_toolexec "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/tools"
click node_shellfs "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/utils/shell"
click node_permissions "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/utils/permissions"
click node_sandbox "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/utils/sandbox"
click node_remote "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/bridge"
click node_swarm "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/utils/swarm"
click node_mcp "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/mcp"
click node_plugins "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/plugins"
click node_lsp "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/lsp"
click node_opsctrl "https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/analytics"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entrypoints,node_inkui,node_components,node_state,node_inputflow,node_commands toneBlue
class node_queryengine,node_taskmodel,node_session,node_contextmem toneAmber
class node_toolcatalog,node_toolexec,node_shellfs toneMint
class node_permissions,node_sandbox toneRose
class node_remote,node_swarm,node_mcp,node_plugins,node_lsp toneIndigo
class node_opsctrl toneTeal
```

## Components

### Runtime shell

Terminal entrypoints, Ink UI, state, and interaction loop.

- [Entrypoints](https://github.com/jihaoyb/claude-source-leaked/blob/main/src/entrypoints/cli.tsx): CLI bootstrap · Starts the terminal app and CLI modes from the main executable paths. · `src/entrypoints/cli.tsx`
- [Ink UI](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/ink): terminal UI · React/Ink terminal rendering layer for screens, dialogs, and prompts. · `src/ink`
- [Components](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/components): TUI components · Reusable terminal components for messages, dialogs, pickers, and overlays. · `src/components`
- [App State](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/state): store/state · Central application state and selectors consumed by UI and hooks. · `src/state`
- [Input Flow](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/utils/processUserInput): command parsing · Processes slash commands, prompt text, and user input into actions. · `src/utils/processUserInput`
- [Commands](https://github.com/jihaoyb/claude-source-leaked/blob/main/src/commands.ts): slash 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](https://github.com/jihaoyb/claude-source-leaked/blob/main/src/QueryEngine.ts): agent loop · Builds requests, streams model responses, and manages turn lifecycle. · `src/QueryEngine.ts`
- [Tasks](https://github.com/jihaoyb/claude-source-leaked/blob/main/src/Task.ts): task abstractions · Task types and execution contexts for local, remote, and assistant work. · `src/Task.ts`
- [Session History](https://github.com/jihaoyb/claude-source-leaked/blob/main/src/assistant/sessionHistory.ts): conversation state · Conversation timeline, session history, and reusable session utilities. · `src/assistant/sessionHistory.ts`
- [Context & Memory](https://github.com/jihaoyb/claude-source-leaked/blob/main/src/context.ts): context assembly · 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](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/tools): tool definitions · Declared tools and their prompts/UI wrappers that model outputs can invoke. · `src/tools`
- [Tool Orchestration](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/tools): tool runtime · Executes streamed tool calls and coordinates tool hooks and results. · `src/services/tools`
- [Shell & Files](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/utils/shell): local execution · Shell, filesystem, git, and repository inspection capabilities. · `src/utils/shell`

### Policy & permissions

Approval, sandboxing, and safety enforcement.

- [Permissions](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/utils/permissions): approval policy · Permission modes, rule matching, prompt handling, and denial tracking. · `src/utils/permissions`
- [Sandboxing](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/utils/sandbox): execution isolation · Sandbox adapters and UI for constrained or protected execution. · `src/utils/sandbox`

### Integrations

Pluggable external capabilities and remote control surfaces.

- [Remote Bridge](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/bridge): remote control · Session bridging, transports, trusted-device flows, and remote session control. · `src/bridge`
- [Multi-Agent](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/utils/swarm): subagents · Agent spawning, team workflows, worker coordination, and swarm execution. · `src/utils/swarm`
- [MCP](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/mcp): capability bridge · Model Context Protocol connection management and tool/resource exposure. · `src/services/mcp`
- [Plugins](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/plugins): extensibility · Plugin loading, installation, policy, marketplace, and lifecycle management. · `src/services/plugins`
- [LSP](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/lsp): code intelligence · Language server integration for diagnostics, symbols, and recommendations. · `src/services/lsp`

### Operations

Telemetry, sync, settings, and background control-plane subsystems.

- [Ops Control](https://github.com/jihaoyb/claude-source-leaked/tree/main/src/services/analytics): telemetry/settings · Analytics, settings sync, managed policy, and remote control-plane updates. · `src/services/analytics`

## Connections

- 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.
