# njengah/claude-code-source-code-leak architecture

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

- Interactive diagram: https://gitdiagram.com/njengah/claude-code-source-code-leak
- Repository: https://github.com/njengah/claude-code-source-code-leak

## Overview

1. **Overall shape**
   - This is a terminal-first agentic assistant implemented in **TypeScript on Bun**, with a **React + Ink** UI layer for full-screen terminal rendering.
   - The architecture centers on a loop: **user input → command/tool selection → model/query execution → tool results/permissions → UI/state updates**.

2. **Startup and entrypoints**
   - `src/main.tsx`, `src/entrypoints/*`, and `src/bootstrap/*` form the boot path.
   - Startup does early environment/config work, then selects the correct runtime mode: interactive CLI, REPL, MCP/server-related entrypoints, or bridge/remote modes.
   - Heavy subsystems are loaded lazily or behind feature flags, which matters for diagramming boot-time dependencies.

3. **Interactive shell and rendering**
   - The primary UI is built in `src/components/` and rendered through the custom Ink implementation in `src/ink/`.
   - `src/screens/` contains larger full-screen modes such as Doctor, REPL, and Resume Conversation.
   - `src/context/` and `src/state/` hold app-wide React state, modal/overlay state, notifications, queued messages, and session-visible status.

4. **Command system**
   - `src/commands.ts` and `src/commands/` implement the slash-command layer.
   - Commands are user-facing operational actions like config, login/logout, resume, compact, review, tasks, memory, skills, plugins, bridge, and diagnostics.
   - Commands are a control plane separate from the agent loop: they mutate app/session settings, open wizards/dialogs, or trigger maintenance workflows.

5. **Query engine and agent loop**
   - `src/QueryEngine.ts` is the core orchestration engine for model interaction.
   - It manages prompt construction, streaming model responses, tool-call loops, retry behavior, token accounting, and turn completion.
   - `src/query/` and `src/constants/prompts.ts` support prompt/context budgeting and query policy, while `src/cost-tracker.ts` tracks usage impact.

6. **Tool subsystem**
   - `src/tools/` contains the executable capabilities the model can invoke: shell, file read/write/edit, grep/glob, web fetch/search, MCP, LSP, notebook edits, agent spawning, task/team operations, plan/worktree toggles, and remote triggers.
   - `src/Tool.ts` and `src/tools.ts` define the shared tool types, registration, and execution contracts.
   - Tool modules usually pair three concerns: input schema, UI/progress rendering, and execution/validation logic.

7. **Permission and safety boundary**
   - `src/hooks/toolPermission/` and `src/components/permissions/` implement the approval boundary for tool use.
   - Tool requests are checked against permission modes and rules before execution; user prompts can approve, deny, or apply persistent policies.
   - Safety-specific validation exists in tool code and utilities, especially for shell/powershell/file operations and sandboxing.

8. **External service layer**
   - `src/services/` is the integration boundary to Anthropic APIs, OAuth, analytics/telemetry, LSP, MCP, plugin operations, compacting, memory extraction, and remote-managed settings.
   - `src/services/api/` is the main network/API client layer.
   - `src/services/oauth/`, `src/services/analytics/`, and `src/services/mcp/` are separate cross-cutting integration domains with their own auth, transport, and policy logic.

9. **Session, history, memory, and compaction**
   - Conversation state is persisted and resumed through `src/history.ts`, `src/assistant/sessionHistory.ts`, `src/utils/session*`, and related storage helpers.
   - `src/services/compact/` and `src/services/SessionMemory/` reduce or summarize context when sessions grow large.
   - `src/memdir/`, `src/services/extractMemories/`, and `src/services/teamMemorySync/` manage longer-lived memory artifacts outside the immediate chat transcript.

10. **Multi-agent and task orchestration**
   - `src/coordinator/`, `src/tasks/`, `src/utils/swarm/`, and `src/tools/AgentTool/` support multi-agent execution and background work.
   - Tasks represent durable units of work with local, remote, shell, dream, or teammate-backed implementations.
   - The coordinator and swarm layers handle delegation, teammate lifecycle, layout/backends, and permission propagation across agent instances.

11. **Bridge, IDE, and remote-control boundary**
   - `src/bridge/` is the bidirectional control plane for IDE integration and remote sessions.
   - It includes messaging, session creation/runner logic, permissions callbacks, attachment handling, REPL bridge transport, and trusted-device/auth helpers.
   - `src/remote/`, `src/server/`, and `src/upstreamproxy/` extend this into remote sessions, direct-connect sessions, and proxy/relay behavior.

12. **Plugin and skill extensibility**
   - `src/plugins/`, `src/services/plugins/`, and `src/utils/plugins/` implement plugin discovery, loading, trust/policy checks, versioning, and marketplace integration.
   - `src/skills/` and `src/utils/skills/` provide reusable workflows that can be bundled with the app or loaded from disk.
   - This is a major extensibility boundary: commands and tools can be added dynamically, and plugin loading affects both the UI and the available agent capabilities.

13. **Input, keybinding, and terminal behavior**
   - `src/keybindings/`, `src/vim/`, `src/interactiveHelpers.tsx`, and many hooks under `src/hooks/` shape how terminal input is interpreted.
   - The app supports modal input behaviors, command history/search, vim-like editing, global shortcuts, and command queueing.
   - `src/utils/terminal.ts` and the Ink runtime provide the terminal abstraction beneath the UI.

14. **Configuration, policy, and environment**
   - `src/utils/settings/`, `src/schemas/`, `src/migrations/`, and `src/utils/config.ts` manage user/org settings, migrations, validation, and managed policy overlays.
   - `src/utils/permissions/`, `src/services/policyLimits/`, and `src/services/remoteManagedSettings/` are important because they constrain what the agent and tools can do.
   - For an architecture diagram, treat these as a control/policy plane that influences nearly every runtime path.

15. **Telemetry, diagnostics, and reliability**
   - `src/services/analytics/`, `src/utils/telemetry/`, `src/services/internalLogging.ts`, and `src/utils/diagLogs.ts` collect usage and diagnostic signals.
   - `src/screens/Doctor.tsx`, `src/commands/doctor/`, and related utilities expose environment and health checks.
   - Reliability features include auto-updates, error boundaries, sandbox checks, and startup/preflight checks.

16. **Key data-flow summary**
   - The main flow is: **CLI/Ink input → command or prompt processing → QueryEngine → tool invocation → permission/sandbox check → tool execution / external service call → streamed results and state updates → transcript/history/storage**.
   - Parallel side channels feed into the same UI/state model: **bridge/remote events, plugin hooks, task updates, memory sync, and telemetry**.
   - The most important boundaries are between **UI vs. execution**, **model orchestration vs. tool execution**, **policy/permission vs. action**, and **local session state vs. remote/bridge integrations**.

Main entry points: [`src/QueryEngine.ts`](https://github.com/njengah/claude-code-source-code-leak/blob/main/src/QueryEngine.ts) (Query engine), [`src/bootstrap`](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/bootstrap) (Bootstrap), [`src/commands.ts`](https://github.com/njengah/claude-code-source-code-leak/blob/main/src/commands.ts) (Commands), [`src/tools`](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/tools) (Tools)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_boot["Boot & runtimes"]
  node_main(("Main<br/>entrypoint<br/>[main.tsx]"))
  node_entrypoints["Entrypoints<br/>runtime selectors"]
  node_bootstrap["Bootstrap<br/>startup init"]
end

subgraph group_ui["Terminal UI"]
  node_ink["Ink runtime<br/>terminal renderer"]
  node_components["Components<br/>ui views"]
  node_screens["Screens<br/>full-screen modes"]
  node_state[("App state<br/>react state")]
  node_input["Input flow<br/>keybindings & parsing"]
end

subgraph group_agent["Agent core"]
  node_commands["Commands<br/>slash command plane<br/>[commands.ts]"]
  node_query{{"Query engine<br/>model orchestrator<br/>[QueryEngine.ts]"}}
  node_tools["Tools<br/>capabilities"]
  node_permissions[("Permissions<br/>approval boundary")]
  node_tasks["Tasks<br/>work orchestration"]
end

subgraph group_integrations["Integrations"]
  node_api["API services<br/>network clients"]
  node_ext["Extensibility<br/>plugins & skills"]
  node_memory["Memory & compaction<br/>context management"]
  node_bridge["Bridge & remote<br/>control plane"]
end

subgraph group_infra["Platform & policy"]
  node_policy[("Config & policy<br/>control plane")]
  node_observability["Observability<br/>telemetry & diagnostics"]
end

node_main -->|"initializes"| node_bootstrap
node_main -->|"dispatches"| node_entrypoints
node_entrypoints -->|"starts UI"| node_ink
node_bootstrap -->|"loads settings"| node_policy
node_bootstrap -->|"seeds"| node_state
node_ink -->|"renders"| node_components
node_components -->|"opens"| node_screens
node_components -->|"consumes"| node_state
node_input -->|"parses slash cmds"| node_commands
node_input -->|"submits prompts"| node_query
node_commands -->|"mutates settings"| node_policy
node_commands -->|"controls remote"| node_bridge
node_query -->|"invokes"| node_tools
node_query -->|"calls model api"| node_api
node_query -->|"compresses context"| node_memory
node_tools -->|"requests approval"| node_permissions
node_permissions -->|"evaluates rules"| node_policy
node_tools -->|"spawns work"| node_tasks
node_tasks -->|"syncs remote work"| node_bridge
node_ext -->|"extends ui"| node_commands
node_ext -->|"extends capabilities"| node_tools
node_memory -->|"updates session"| node_state
node_bridge -->|"pushes events"| node_state
node_observability -->|"preflight"| node_bootstrap
node_observability -->|"tracks usage"| node_query

click node_main "https://github.com/njengah/claude-code-source-code-leak/blob/main/src/main.tsx"
click node_entrypoints "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/entrypoints"
click node_bootstrap "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/bootstrap"
click node_ink "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/ink"
click node_components "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/components"
click node_screens "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/screens"
click node_state "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/state"
click node_input "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/keybindings"
click node_commands "https://github.com/njengah/claude-code-source-code-leak/blob/main/src/commands.ts"
click node_query "https://github.com/njengah/claude-code-source-code-leak/blob/main/src/QueryEngine.ts"
click node_tools "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/tools"
click node_permissions "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/hooks/toolPermission"
click node_tasks "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/tasks"
click node_api "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/services/api"
click node_ext "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/plugins"
click node_memory "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/services/compact"
click node_bridge "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/bridge"
click node_policy "https://github.com/njengah/claude-code-source-code-leak/tree/main/src/utils/settings"
click node_observability "https://github.com/njengah/claude-code-source-code-leak/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_main,node_entrypoints,node_bootstrap toneBlue
class node_ink,node_components,node_screens,node_state,node_input toneAmber
class node_commands,node_query,node_tools,node_permissions,node_tasks toneMint
class node_api,node_ext,node_memory,node_bridge toneRose
class node_policy,node_observability toneIndigo
```

## Components

### Boot & runtimes

Startup, entrypoints, and runtime selection for CLI, REPL, bridge, and remote modes.

- [Main](https://github.com/njengah/claude-code-source-code-leak/blob/main/src/main.tsx): entrypoint · Top-level process bootstrap and mode dispatch. · `src/main.tsx`
- [Entrypoints](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/entrypoints): runtime selectors · CLI, MCP, init, and SDK entry paths that choose execution mode. · `src/entrypoints`
- [Bootstrap](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/bootstrap): startup init · Early initialization of state, environment, and runtime wiring. · `src/bootstrap`

### Terminal UI

Ink rendering, screens, components, input handling, and shared app state.

- [Ink runtime](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/ink): terminal renderer · Custom Ink implementation and terminal abstractions for full-screen rendering. · `src/ink`
- [Components](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/components): ui views · React terminal UI, dialogs, prompts, and message rendering. · `src/components`
- [Screens](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/screens): full-screen modes · Doctor, REPL, and resume-style dedicated terminal screens. · `src/screens`
- [App state](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/state): react state · Global app context, overlays, notifications, queued messages, and session-visible UI state. · `src/state`
- [Input flow](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/keybindings): keybindings & parsing · Keybinding resolution, terminal input, and slash-command/user-text processing. · `src/keybindings`

### Agent core

Query orchestration, commands, tools, permissions, and task/multi-agent execution.

- [Commands](https://github.com/njengah/claude-code-source-code-leak/blob/main/src/commands.ts): slash command plane · User-facing operational commands for config, login, resume, maintenance, and diagnostics. · `src/commands.ts`
- [Query engine](https://github.com/njengah/claude-code-source-code-leak/blob/main/src/QueryEngine.ts): model orchestrator · Constructs prompts, streams responses, handles tool loops, retries, and token accounting. · `src/QueryEngine.ts`
- [Tools](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/tools): capabilities · Executable actions available to the model: shell, files, search, web, MCP, LSP, tasks, agents, and remote triggers. · `src/tools`
- [Permissions](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/hooks/toolPermission): approval boundary · Tool approval UX and policy enforcement for destructive or sensitive actions. · `src/hooks/toolPermission`
- [Tasks](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/tasks): work orchestration · Durable local, remote, shell, dream, and teammate-backed work units. · `src/tasks`

### Integrations

Model APIs and cross-cutting services for MCP, plugins, memory, LSP, analytics, and remote control.

- [API services](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/services/api): network clients · Anthropic/API clients and service adapters for model, usage, and session ingress calls. · `src/services/api`
- [Extensibility](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/plugins): plugins & skills · Plugin discovery/loading, trust policy, marketplace flows, and reusable skills. · `src/plugins`
- [Memory & compaction](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/services/compact): context management · Session history, compaction, memory extraction, and long-lived memory artifacts. · `src/services/compact`
- [Bridge & remote](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/bridge): control plane · IDE bridge, remote sessions, direct-connect server, and relay/proxy behavior. · `src/bridge`

### Platform & policy

Configuration, storage, history, telemetry, sandboxing, and repository/environment utilities.

- [Config & policy](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/utils/settings): control plane · Settings, schema validation, migrations, permission rules, sandbox, and managed overlays. · `src/utils/settings`
- [Observability](https://github.com/njengah/claude-code-source-code-leak/tree/main/src/services/analytics): telemetry & diagnostics · Analytics, logging, diagnostics, doctor checks, and reliability signals. · `src/services/analytics`

## Connections

- Main → Bootstrap: initializes · Bootstraps runtime state and environment before mode selection.
- Main → Entrypoints: dispatches · Selects CLI, MCP, REPL, bridge, or remote entry path.
- Entrypoints → Ink runtime: starts UI · Interactive paths mount the terminal renderer.
- Bootstrap → Config & policy: loads settings · Reads config, schema, migrations, and policy overlays early.
- Bootstrap → App state: seeds · Initializes app/session state stores and contexts.
- Ink runtime → Components: renders · Ink hosts the React terminal component tree.
- Components → Screens: opens · UI can switch into dedicated screens for doctor, REPL, and resume.
- Components → App state: consumes · Views subscribe to app state, overlays, notifications, and session status.
- Input flow → Commands: parses slash cmds · User input can be routed into command handlers.
- Input flow → Query engine: submits prompts · Plain text input enters the agent loop.
- Commands → Config & policy: mutates settings · Commands can change config, permissions, sandbox, and runtime preferences.
- Commands → Bridge & remote: controls remote · Bridge-related commands manage IDE/remote connectivity.
- Query engine → Tools: invokes · The model can request tool execution during turns.
- Query engine → API services: calls model api · Streams model responses and related service requests.
- Query engine → Memory & compaction: compresses context · Long sessions trigger compaction and memory updates.
- Tools → Permissions: requests approval · Sensitive tool actions pass through permission checks before execution.
- Permissions → Config & policy: evaluates rules · Approval decisions are constrained by settings and policy.
- Tools → Tasks: spawns work · Tools can create or manage background and multi-agent tasks.
- Tasks → Bridge & remote: syncs remote work · Remote and IDE-connected tasks report through bridge/control channels.
- Extensibility → Commands: extends ui · Plugins can contribute commands and menu flows.
- Extensibility → Tools: extends capabilities · Plugins and skills can add or influence tool behavior.
- Memory & compaction → App state: updates session · Compaction and extraction feed visible session state and history.
- Bridge & remote → App state: pushes events · Bridge and remote events update UI/session state.
- Observability → Bootstrap: preflight · Startup checks, diagnostics, and reliability signals inform boot.
- Observability → Query engine: tracks usage · Model usage, errors, and performance are recorded during runs.
