How njengah/claude-code-source-code-leak works
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: QueryEngine.ts Query engine · bootstrap Bootstrap · commands.ts Commands · tools Tools
Components 19
Boot & runtimes
Startup, entrypoints, and runtime selection for CLI, REPL, bridge, and remote modes.
- Main · Top-level process bootstrap and mode dispatch.
src/main.tsx - Entrypoints · CLI, MCP, init, and SDK entry paths that choose execution mode.
src/entrypoints - Bootstrap · Early initialization of state, environment, and runtime wiring.
src/bootstrap
Terminal UI
Ink rendering, screens, components, input handling, and shared app state.
- Ink runtime · Custom Ink implementation and terminal abstractions for full-screen rendering.
src/ink - Components · React terminal UI, dialogs, prompts, and message rendering.
src/components - Screens · Doctor, REPL, and resume-style dedicated terminal screens.
src/screens - App state · Global app context, overlays, notifications, queued messages, and session-visible UI state.
src/state - Input flow · 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 · User-facing operational commands for config, login, resume, maintenance, and diagnostics.
src/commands.ts - Query engine · Constructs prompts, streams responses, handles tool loops, retries, and token accounting.
src/QueryEngine.ts - Tools · Executable actions available to the model: shell, files, search, web, MCP, LSP, tasks, agents, and remote triggers.
src/tools - Permissions · Tool approval UX and policy enforcement for destructive or sensitive actions.
src/hooks/toolPermission - Tasks · 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 · Anthropic/API clients and service adapters for model, usage, and session ingress calls.
src/services/api - Extensibility · Plugin discovery/loading, trust policy, marketplace flows, and reusable skills.
src/plugins - Memory & compaction · Session history, compaction, memory extraction, and long-lived memory artifacts.
src/services/compact - Bridge & remote · 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 · Settings, schema validation, migrations, permission rules, sandbox, and managed overlays.
src/utils/settings - Observability · Analytics, logging, diagnostics, doctor checks, and reliability signals.
src/services/analytics
Connections 25
- 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.