···121121122122## Documentation (GitHub Pages)
123123124124-Full internals documentation (architecture, workflows, official-docs cross-reference, subsystem reference, appendices) is built with **MkDocs Material** from [`docs-site/`](docs-site/).
124124+Full internals documentation is built with **MkDocs Material** from [`docs-site/`](docs-site/). The site includes **system design** (layers, state flow, security/trust), **architecture** overview and **workflows**, a **developer hub** (editing docs, navigating `src/`, Bun feature flags), **guides** for greenfield agentic CLI and docs/CI patterns, **reference** pages per subsystem, **official docs map**, and **appendices** (directory layout, tools, env vars, glossary).
125125126126- **Live site:** [https://mehmoodosman.github.io/claude-code-source-code/](https://mehmoodosman.github.io/claude-code-source-code/)
127127- **Local preview:** `cd docs-site && python3 -m venv .venv && source .venv/bin/activate && pip install -r requirements.txt && mkdocs serve`
+20
docs-site/docs/appendix/glossary.md
···11+# Glossary
22+33+Short definitions for terms used across this site and the `src/` tree. Authoritative product wording remains in [official Claude Code docs](https://code.claude.com/docs/en/overview).
44+55+| Term | Meaning (in this codebase) |
66+| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
77+| **Agent / teammate** | Secondary model actors coordinated with the main session; implemented largely in `utils/swarm/`. |
88+| **Compaction** | Reducing or rewriting transcript/history to fit context limits; `services/compact/`. |
99+| **Coordinator** | Internal multi-agent coordination feature gated by `COORDINATOR_MODE`; `coordinator/`. |
1010+| **GrowthBook** | Feature-flag / experiment client for product behaviors; `services/analytics/growthbook.ts`. |
1111+| **Headless / print** | Non-TUI mode with structured stdin/stdout; `cli/print.ts`, `QueryEngine.ts`. |
1212+| **KAIROS** | Bundle-time gate for assistant-oriented code paths; `assistant/`. |
1313+| **MCP** | Model Context Protocol; `services/mcp/`, MCP tools under `tools/`. |
1414+| **REPL** | Interactive terminal UI session; `screens/REPL.tsx`. |
1515+| **Session hooks** | User-configured shell hooks on lifecycle events; see `utils/sessionStart.ts` and official [Hooks](https://code.claude.com/docs/en/hooks). |
1616+| **Swarm** | Implementation umbrella for teammate spawning (tmux, iTerm, in-process); `utils/swarm/`. |
1717+| **Teleport** | Session handoff / remote resume flows; `utils/teleport/`, related API modules. |
1818+| **Tool loop** | Alternation between model streaming and tool execution until the turn completes; centered on `query.ts`. |
1919+| **Trust** | User acknowledgement of workspace risk before applying project config; wired from `main.tsx` / config utilities. |
2020+| **VOICE_MODE** | Compile-time gate for voice dictation in the REPL; `voice/`. |
+19-36
docs-site/docs/architecture.md
···11-# Architecture
11+# Architecture overview
2233!!! warning "Recovered source"
44Paths refer to `src/` in this repository. Line-level accuracy is best-effort from the source map reconstruction.
···3737 qe --> compact
3838```
39394040-## Entry and startup
4040+## System design (deeper dives)
41414242-`src/main.tsx` is the **Commander**-based CLI. Before the command handler runs, module evaluation triggers:
4343-4444-- Startup profiling checkpoints (`utils/startupProfiler.ts`)
4545-- MDM raw reads in parallel (`utils/settings/mdm/rawRead.ts`)
4646-- macOS keychain prefetch (`utils/secureStorage/keychainPrefetch.ts`)
4747-4848-The `preAction` hook loads trust, settings, telemetry gates, MCP prefetch, policy limits, and related startup work before interactive or print mode begins.
4949-5050-## Interactive mode
4242+| Topic | Page |
4343+| --------------------------------- | ----------------------------------------------------------------- |
4444+| Layering and dependency direction | [Architectural layers](system-design/layers.md) |
4545+| State, messages, and persistence | [State and data flow](system-design/state-and-data-flow.md) |
4646+| Trust, permissions, MCP policy | [Security and trust model](system-design/security-trust-model.md) |
51475252-- `replLauncher.tsx` mounts the Ink/React app.
5353-- `screens/REPL.tsx` owns the main session UI, input queue, model turns, tool execution UI, teammate hooks, voice (when compiled in), and scheduled-task integration.
5454-- `utils/queueProcessor.ts` drains slash commands, bash-mode lines, and batched prompts into `executeInput`.
4848+## Short orientation
55495656-## Headless / print / SDK-style I/O
5757-5858-- `cli/print.ts` implements structured stdin/stdout loops (`runHeadlessStreaming`, NDJSON-style control).
5959-- `QueryEngine.ts` supports non-REPL paths where messages are submitted programmatically and tool permission context is updated without full UI.
6060-6161-## Tools and MCP
6262-6363-- `tools.ts` aggregates built-in tools (filtered by permission context) and merges MCP-derived tools.
6464-- `services/mcp/` implements config parsing, transports, OAuth, channel permissions, and the in-process MCP client used by `MCPConnectionManager` in the UI.
6565-6666-## Compaction and memory
6767-6868-- `services/compact/` implements context window management aligned with user-facing [context window](https://code.claude.com/docs/en/context-window) and [costs](https://code.claude.com/docs/en/costs) documentation.
6969-7070-## Multi-agent (swarm / teammates)
7171-7272-- `utils/swarm/` contains backends (tmux, iTerm, in-process), spawn utilities, permission sync, and `inProcessRunner.ts` teammate loop.
7373-7474-## IDE integration
7575-7676-- `bridge/` plus hooks such as `hooks/useDiffInIDE.ts` connect to VS Code / JetBrains surfaces described in official [VS Code](https://code.claude.com/docs/en/vs-code) and [JetBrains](https://code.claude.com/docs/en/jetbrains) docs.
5050+- **Entry** — `main.tsx`: Commander CLI, `preAction` (trust, settings, telemetry gates), side-effect imports (profiler, MDM, keychain prefetch).
5151+- **Interactive host** — `replLauncher.tsx`, `screens/REPL.tsx`, `utils/queueProcessor.ts`.
5252+- **Headless host** — `cli/print.ts`, `QueryEngine.ts`, `cli/structuredIO.ts`.
5353+- **Tools & MCP** — `tools.ts`, `tools/*`, `services/mcp/`, `services/tools/`.
5454+- **IDE / OS** — `bridge/`, `utils/deepLink/`, `utils/claudeInChrome/`.
77557856## Key files (quick index)
7957···9068| `services/compact/` | Compaction pipeline |
9169| `utils/sessionStart.ts` | Session / setup hooks |
92709393-See also [Workflows](workflows.md) and the [official docs map](official-docs-map.md).
7171+## Further reading
7272+7373+- [Workflows](workflows.md) — end-to-end sequences.
7474+- [Official docs map](official-docs-map.md) — product docs ↔ `src/`.
7575+- [Reference](reference/cli-entry.md) — subsystem reference pages.
7676+- [Glossary](appendix/glossary.md).
···11+# Bun bundle and feature flags
22+33+The shipping Claude Code CLI is built with **Bun** and internal **`bun:bundle`** integration. In the recovered source, `src/main.tsx` uses compile-time feature gates such as:
44+55+- `feature('KAIROS')` — lazy `require` of `assistant/` (assistant / Agent SDK–oriented paths).
66+- `feature('COORDINATOR_MODE')` — lazy `require` of `coordinator/coordinatorMode`.
77+- `feature('VOICE_MODE')` — conditional `useVoiceIntegration` in `REPL.tsx`.
88+99+## What `feature()` means here
1010+1111+In Anthropic’s pipeline, `feature('…')` is almost certainly a **constant** resolved at bundle time: dead branches are stripped from the published `cli.js`. The reconstruction **contains all branches** as TypeScript, so you can read “internal” code paths that might not ship in every npm artifact.
1212+1313+## Implications for “building from source”
1414+1515+A normal `tsc` or `bun build` **without** Anthropic’s bundler will not reproduce:
1616+1717+- The same dead-code elimination.
1818+- The same `bun:bundle` API semantics.
1919+- The same proprietary dependency closure.
2020+2121+That is one reason [Reproducibility and limits](../reproducibility.md) states you cannot rebuild the shipping binary from this mirror.
2222+2323+## See also
2424+2525+- [System design: layers](../system-design/layers.md)
2626+- [CLI entry reference](../reference/cli-entry.md)
+29
docs-site/docs/developer/editing-documentation.md
···11+# Editing this documentation
22+33+## Local setup
44+55+```bash
66+cd docs-site
77+python3 -m venv .venv
88+source .venv/bin/activate # Windows: .venv\Scripts\activate
99+pip install -r requirements.txt
1010+mkdocs serve
1111+```
1212+1313+Open `http://127.0.0.1:8000` and edit files under `docs-site/docs/`. The site reloads on save.
1414+1515+## Conventions
1616+1717+- Use **admonitions** for legal and safety notes (`!!! warning` for proprietary / leak context).
1818+- Prefer **relative links** between pages (`../reference/cli-entry.md` in source becomes sibling URLs when built).
1919+- Add **mermaid** only where diagrams clarify flow; keep [mermaid syntax](https://mermaid.js.org/) compatible with [Material diagrams](https://squidfunk.github.io/mkdocs-material/reference/diagrams/).
2020+2121+## After structural changes
2222+2323+If you add a **new top-level topic** that appears in [Anthropic’s docs index](https://code.claude.com/docs/llms.txt), add a row to [Official docs map](../official-docs-map.md).
2424+2525+## Publishing
2626+2727+Pushing to `main` runs the GitHub Action that deploys to `gh-pages`. Ensure `site_url` in `mkdocs.yml` matches your GitHub Pages base URL.
2828+2929+See also: [Documentation and CI for your own projects](../guides/documentation-and-ci-for-docs.md).
+25
docs-site/docs/developer/index.md
···11+# Developer hub (this repository)
22+33+!!! warning "Not an open-source product repo"
44+This tree is **reconstructed proprietary source**. You can maintain **documentation**, trace **read-only** flow in `src/`, and run small **helper scripts**. You **cannot** legally ship Claude Code from this mirror, and there is no supported way to compile the full CLI here.
55+66+## What you can do
77+88+| Activity | How |
99+| ---------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
1010+| **Preview or extend docs** | [Editing documentation](editing-documentation.md) |
1111+| **Understand control flow** | [Navigating the source](navigating-the-source.md) |
1212+| **Learn build-time gates** | [Bun bundle and feature flags](bun-bundle-and-feature-flags.md) |
1313+| **Regenerate appendix data** | `scripts/gen-appendices.sh` at repo root (see [Appendix: environment variables](../appendix/environment-variables.md)) |
1414+1515+## What you cannot do (from this mirror alone)
1616+1717+- Run `npm install` / `bun build` for the full product (no root `package.json`, private graph, `bun:bundle` feature flags).
1818+- Execute a project-wide automated test suite for the CLI (no harness shipped in the reconstruction).
1919+2020+Details: [Reproducibility and limits](../reproducibility.md).
2121+2222+## Related
2323+2424+- [System design: layers](../system-design/layers.md)
2525+- [Official docs map](../official-docs-map.md)
+37
docs-site/docs/developer/navigating-the-source.md
···11+# Navigating the `src/` tree
22+33+!!! warning "Read-only study"
44+Use this guide to **read** the reconstruction. Do not treat the tree as a template for redistributing Anthropic’s product.
55+66+## Suggested reading order
77+88+1. **`main argv` and lifecycle** — `src/main.tsx` (Commander, `preAction`, mode routing).
99+2. **Interactive path** — `src/replLauncher.tsx`, `src/screens/REPL.tsx` (queue, `onQuery`, UI).
1010+3. **Headless path** — `src/cli/print.ts`, `src/cli/structuredIO.ts`, `src/QueryEngine.ts`.
1111+4. **Core loop** — `src/query.ts` (`queryLoop`), then `src/services/api/client.ts` / `claude.ts`.
1212+5. **Tools** — `src/tools.ts`, `src/Tool.ts`, then a single tool package e.g. `src/tools/BashTool/` and `src/services/tools/`.
1313+6. **MCP** — `src/services/mcp/` and `src/tools/MCPTool/`.
1414+1515+## Maps and indexes
1616+1717+- Top-level layout: [Appendix: directory structure](../appendix/directory-structure.md) (and repo `docs/directory-structure.md`).
1818+- Tool packages: [Appendix: tool packages](../appendix/tool-packages.md).
1919+- Official doc crosswalk: [Official docs map](../official-docs-map.md).
2020+2121+## Search tips
2222+2323+From the repository root:
2424+2525+```bash
2626+# Example: find where a symbol is used
2727+rg "queryLoop" src/query.ts src/ -n
2828+2929+# Example: MCP channel permissions
3030+rg "channelAllowlist" src/services/mcp -n
3131+```
3232+3333+IDE “go to definition” may be incomplete: there is **no** root `tsconfig.json` or `package.json` mirroring Anthropic’s build, so path aliases like `src/...` might not resolve everywhere.
3434+3535+## Deeper subsystems
3636+3737+Use the [Reference](../reference/cli-entry.md) section for per-topic entry points (permissions, compaction, telemetry, bridge, etc.).
···11+# Building your own agentic coding CLI (greenfield)
22+33+!!! danger "Do not clone proprietary code"
44+This page describes how to build **new** tooling from **public** APIs and OSS patterns. It is **not** instructions to copy or redistribute the leaked `src/` tree as “open-source Claude Code.” Anthropic’s Claude Code remains **proprietary**.
55+66+## What “agentic CLI” means
77+88+A minimal agent loop:
99+1010+1. **Context** — project files, git status, user prompt.
1111+2. **Model** — calls a capable LLM API with tools/functions defined in schema form.
1212+3. **Tools** — read/write files, grep, run shell (with guardrails), call HTTP, etc.
1313+4. **Loop** — model requests tools → host executes → results appended → model continues until done.
1414+1515+Claude Code implements this pattern in TypeScript with extra product layers (trust, compaction, MCP, IDE bridge). Your project can be far smaller.
1616+1717+## Stack options (all legitimate OSS / vendor SDKs)
1818+1919+| Piece | Common choices |
2020+| ----------------- | -------------------------------------------------------------------------------------------------------------------- |
2121+| **Runtime** | Node.js, Bun, Deno, Go, Python |
2222+| **Terminal UI** | None (pure stdin/stdout), [Ink](https://github.com/vadimdemedes/ink) (React), blessed, bubbletea |
2323+| **CLI parsing** | Commander, yargs, clap (Rust), Typer (Python) |
2424+| **Anthropic API** | Official [Anthropic SDK](https://docs.anthropic.com/) and [Messages API](https://docs.anthropic.com/en/api/messages) |
2525+| **MCP** | [Model Context Protocol](https://modelcontextprotocol.io) SDKs and server examples |
2626+| **Sandboxes** | Containers, `firejail`, allow-listed commands, separate VMs (depends on threat model) |
2727+2828+## OSS / public projects to study (examples, not endorsements)
2929+3030+Look for **actively maintained** agent or coding assistants under licenses you accept. Examples people often cite in the ecosystem include **Aider**, terminal agents built on **Continue** or **Codex-style** CLIs, and **MCP servers** in the official registry. **Compare licenses and security posture yourself**—do not assume parity with Claude Code.
3131+3232+## Relationship to this repository
3333+3434+Use the `src/` mirror to **learn patterns** (how compaction, MCP, or permissions _can_ be structured). Re-implement ideas in **your own codebase** with your own naming and design; do not paste Anthropic’s source into a public repo.
3535+3636+## Next
3737+3838+- [MCP and tool-loop patterns](mcp-and-tool-loop-patterns.md) — abstract loop without copying this tree.
3939+- [Documentation and CI](documentation-and-ci-for-docs.md) — ship docs like this site.
···11+# MCP and tool-loop patterns (abstract)
22+33+!!! note "Conceptual only"
44+This page describes **common agent architectures**. It references `src/` modules only as **illustrations** of where similar ideas appear in Claude Code—not as code to copy.
55+66+## Tool loop (functions / tools)
77+88+1. Define tools with **names, descriptions, and JSON Schema** (or equivalent) for arguments.
99+2. Send user + conversation + tool definitions to the model.
1010+3. If the model returns a **tool call**, validate arguments, **execute** on the host, append a **tool_result** message (or provider-specific equivalent).
1111+4. Repeat until the model finishes without further tool calls or a cap is hit.
1212+1313+In this reconstruction, much of that orchestration lives in `query.ts` and adjacent helpers, while tool implementations live under `tools/*` and `services/tools/`.
1414+1515+## MCP (Model Context Protocol)
1616+1717+**MCP** standardizes how tools and resources are exposed by **servers** the host spawns and talks to over stdio or other transports. Claude Code’s `services/mcp/` implements configuration, transports, OAuth, and channel-style push—see **[MCP reference](../reference/mcp.md)** for file-level mapping.
1818+1919+For **your** project:
2020+2121+- Start from [MCP documentation](https://modelcontextprotocol.io) and official SDKs.
2222+- Implement **one** small MCP server (e.g. filesystem or git) before designing a full IDE-quality integration.
2323+2424+## Parallel: skills and plugins
2525+2626+Claude Code layers **skills** (project-scoped instructions + commands) and **plugins** on top of the core loop. Greenfield designs might use: simple slash-commands, `SKILL.md` conventions, or dynamic imports—without reproducing this product’s plugin manifest.
2727+2828+## See also
2929+3030+- [Building an agentic CLI overview](building-an-agentic-cli-overview.md)
3131+- [Official: MCP](https://code.claude.com/docs/en/mcp)
+12-9
docs-site/docs/index.md
···15151616## How to use this site
17171818-| Section | Purpose |
1919-| ------------------------------------------- | -------------------------------------------------------------------------------------- |
2020-| [Installation](installation.md) | Build **this documentation** locally; install the **real** Claude Code from Anthropic. |
2121-| [Reproducibility](reproducibility.md) | Why you **cannot** rebuild the shipping npm package from this tree alone. |
2222-| [Architecture](architecture.md) | High-level components and data flow. |
2323-| [Workflows](workflows.md) | Interactive REPL, print/headless, swarm, compaction paths. |
2424-| [Official docs map](official-docs-map.md) | Every indexed doc page ↔ `src/` entry points. |
2525-| [Reference](reference/cli-entry.md) | Deeper dives per subsystem. |
2626-| [Appendix](appendix/directory-structure.md) | Directory layout, tool packages, environment variables. |
1818+| Section | Purpose |
1919+| ---------------------------------------------------- | ----------------------------------------------------------------------------------------- |
2020+| [Installation](installation.md) | Build **this documentation** locally; install the **real** Claude Code from Anthropic. |
2121+| [Reproducibility](reproducibility.md) | Why you **cannot** rebuild the shipping npm package from this tree alone. |
2222+| [System design](system-design/layers.md) | Layers, state/data flow, security and trust model. |
2323+| [Architecture](architecture.md) | Overview diagram and links into system design + reference. |
2424+| [Workflows](workflows.md) | Interactive REPL, print/headless, swarm, compaction paths. |
2525+| [Developer hub](developer/index.md) | Working on docs, navigating `src/`, Bun feature flags. |
2626+| [Guides](guides/building-an-agentic-cli-overview.md) | **Greenfield** agentic CLI and docs/CI patterns—not cloning this leak. |
2727+| [Official docs map](official-docs-map.md) | Every indexed doc page ↔ `src/` entry points. |
2828+| [Reference](reference/cli-entry.md) | Deeper dives per subsystem. |
2929+| [Appendix](appendix/directory-structure.md) | Directory layout, tool packages, environment variables, [glossary](appendix/glossary.md). |
27302831## Repository
2932
+68
docs-site/docs/system-design/layers.md
···11+# Architectural layers
22+33+!!! warning "Recovered proprietary source"
44+Descriptions are for **study** of the `src/` mirror only. This is not an open-source distribution of Claude Code.
55+66+The Claude Code CLI is organized as **downward dependencies**: the shell and UI call into the query core and services; core logic does not depend on React/Ink components for correctness (headless paths prove that).
77+88+## Layer stack (conceptual)
99+1010+| Layer | Responsibility | Primary `src/` locations |
1111+| ------------------------ | ------------------------------------------------------------ | ---------------------------------------------------------------------------------------- |
1212+| **CLI shell** | argv parsing, global flags, subcommands, `preAction` startup | `main.tsx`, `commands/`, `cli/handlers/` |
1313+| **Session host** | Interactive TUI vs structured print/SDK transport | `replLauncher.tsx`, `screens/REPL.tsx`, `cli/print.ts`, `cli/structuredIO.ts` |
1414+| **Query core** | Turn loop, streaming, tool round-trips, context assembly | `query.ts`, `QueryEngine.ts`, `utils/processUserInput/` |
1515+| **Transport** | Anthropic (and provider) HTTP/streaming | `services/api/` |
1616+| **Tooling** | Registry, execution, hooks | `tools.ts`, `Tool.ts`, `tools/*`, `services/tools/` |
1717+| **Integrations** | MCP, LSP, OAuth, IDE bridge | `services/mcp/`, `services/lsp/`, `services/oauth/`, `bridge/` |
1818+| **Policy & persistence** | Settings, compaction, session storage, telemetry | `utils/settings/`, `services/compact/`, `utils/sessionStorage.ts`, `services/analytics/` |
1919+2020+## Dependency direction
2121+2222+```mermaid
2323+flowchart TB
2424+ subgraph presentation [Presentation]
2525+ Main[main.tsx]
2626+ REPL[REPL.tsx]
2727+ Print[cli/print.ts]
2828+ end
2929+ subgraph domain [Domain_core]
3030+ Query[query.ts]
3131+ QE[QueryEngine.ts]
3232+ end
3333+ subgraph ports [Ports]
3434+ API[services/api]
3535+ Tools[tools plus services/tools]
3636+ MCP[services/mcp]
3737+ end
3838+ subgraph infra [Infrastructure]
3939+ Settings[utils/settings]
4040+ Compact[services/compact]
4141+ Store[sessionStorage]
4242+ end
4343+ Main --> REPL
4444+ Main --> Print
4545+ REPL --> Query
4646+ Print --> QE
4747+ Query --> API
4848+ Query --> Tools
4949+ QE --> API
5050+ QE --> Tools
5151+ Tools --> MCP
5252+ Query --> Compact
5353+ QE --> Compact
5454+ Query --> Settings
5555+ Main --> Settings
5656+```
5757+5858+**Rule of thumb:** `components/` and `ink/` sit under the REPL branch; `cli/print.ts` bypasses most of that stack but still shares `QueryEngine`, tools, API, and compaction.
5959+6060+## Feature gates
6161+6262+Some “layers” exist only in certain shipping builds: `assistant/` (KAIROS), `coordinator/` (`COORDINATOR_MODE`), voice (`VOICE_MODE`). See [Bun bundle and feature flags](../developer/bun-bundle-and-feature-flags.md).
6363+6464+## See also
6565+6666+- [State and data flow](state-and-data-flow.md)
6767+- [Security and trust model](security-trust-model.md)
6868+- [Architecture overview](../architecture.md)
···11+# Security and trust model
22+33+!!! warning "Recovered proprietary source"
44+This page summarizes **code structure**, not a formal security audit. Follow [official Security](https://code.claude.com/docs/en/security) and [Permissions](https://code.claude.com/docs/en/permissions) guidance for the product.
55+66+## Trust and workspace
77+88+Before destructive work runs, startup paths in `main.tsx` consult trust and workspace state (global config helpers under `utils/config.ts`, managed env under `utils/managedEnv.ts`). The intent is to avoid applying repo-supplied config or hooks until the user has acknowledged risk—public discussion of past ordering bugs is summarized in vendor advisories; always run an **up-to-date** official Claude Code build for production use.
99+1010+## Permission modes
1111+1212+`utils/permissions/` defines modes (manual approval, auto with classifiers, plan-only variants, etc.). `permissionSetup.ts` and related modules:
1313+1414+- Parse CLI flags (`--permission-mode`, internal aliases).
1515+- Strip or gate “dangerous” capabilities when using auto mode.
1616+- Feed **always-allow** tool lists into `toolPermissionContext`.
1717+1818+User-facing reference: [Permission modes](https://code.claude.com/docs/en/permission-modes).
1919+2020+## Bash and sandboxing
2121+2222+Shell execution flows through `utils/shell/` (bash and PowerShell providers, output limits, read-only validation) and `tools/BashTool/`. Enterprise and product docs describe [Sandboxing](https://code.claude.com/docs/en/sandboxing) behavior; the source tree implements isolation and validation at the tool layer.
2323+2424+## MCP and enterprise policy
2525+2626+- **Config** — `services/mcp/config.ts` parses MCP server lists, env expansion, deduplication, and enterprise allowlists.
2727+- **Channels** — `services/mcp/channelAllowlist.ts` and related modules gate inbound push notifications.
2828+2929+Official: [MCP](https://code.claude.com/docs/en/mcp), [Channels](https://code.claude.com/docs/en/channels).
3030+3131+## Hooks
3232+3333+User-defined hooks (session start, post-tool, etc.) are wired through `utils/sessionStart.ts` and related runners; they execute shell commands with the privileges of the CLI process. Treat untrusted projects as **untrusted code** until you understand hook content.
3434+3535+Official: [Hooks](https://code.claude.com/docs/en/hooks).
3636+3737+## Trust check order (conceptual)
3838+3939+```mermaid
4040+flowchart TD
4141+ start[Process_start]
4242+ config[Load_safe_config_order]
4343+ trust[Workspace_trust]
4444+ perms[Permission_mode_setup]
4545+ hooks[Session_hooks]
4646+ tools[Tool_execution]
4747+ start --> config
4848+ config --> trust
4949+ trust --> perms
5050+ perms --> hooks
5151+ hooks --> tools
5252+```
5353+5454+Exact ordering evolves by version; correlate with `main.tsx` `preAction` and `entrypoints/init` when reading the mirror.
5555+5656+## See also
5757+5858+- [Permissions reference](../reference/permissions.md)
5959+- [MCP reference](../reference/mcp.md)
6060+- [Reproducibility and limits](../reproducibility.md)
···11+# State and data flow
22+33+!!! warning "Recovered proprietary source"
44+For authoritative product behavior, use [official Claude Code docs](https://code.claude.com/docs/en/overview).
55+66+## End-to-end message path (interactive)
77+88+1. **Input** — User text, slash commands, teammate notifications, or task items reach `screens/REPL.tsx`.
99+2. **Queue** — `utils/queueProcessor.ts` serializes or batches commands and calls `executeInput`.
1010+3. **Query** — `query.ts` `queryLoop` streams the model, surfaces `tool_use` blocks, and applies permission checks using `toolPermissionContext` from app state.
1111+4. **Execution** — `services/tools/` (e.g. `StreamingToolExecutor`, `toolExecution.ts`) dispatches to modules under `tools/*` or MCP-backed tools.
1212+5. **Persistence** — Transcript updates, session metadata, and compaction boundaries flow through `utils/sessionStorage.ts`, conversation recovery helpers, and related hooks.
1313+1414+## State ownership
1515+1616+| Concern | Typical modules |
1717+| ----------------------- | ------------------------------------------------------------------------------------------ |
1818+| **Global app state** | `state/` (providers, stores such as `AppStateStore.ts`) |
1919+| **Context / stats** | `context/` |
2020+| **Messages** | `utils/messages.ts`, message types under `types/message` (and generated types) |
2121+| **Permissions** | `utils/permissions/`, fields on tool permission context updated from CLI and REPL |
2222+| **File / edit history** | Wired through process-user-input and tool hooks (see compaction and attribution utilities) |
2323+2424+## Simplified sequence (one turn)
2525+2626+```mermaid
2727+sequenceDiagram
2828+ participant UI as REPL_or_print
2929+ participant Q as queryLoop
3030+ participant API as services_api
3131+ participant Exec as tool_executor
3232+ participant Store as session_storage
3333+ UI->>Q: user_or_control_messages
3434+ Q->>API: stream_completion
3535+ API-->>Q: assistant_tool_use
3636+ Q->>Exec: run_tool
3737+ Exec-->>Q: tool_result
3838+ Q->>Store: persist_transcript_updates
3939+ Q-->>UI: render_updates
4040+```
4141+4242+## Headless differences
4343+4444+In print/SDK paths: `QueryEngine.ts` builds a `processUserInputContext` with `isNonInteractiveSession: true`, streams SDK-style messages, and bridges permission prompts over structured I/O instead of Ink dialogs. The **same** underlying message and tool model is reused.
4545+4646+## See also
4747+4848+- [Workflows](../workflows.md)
4949+- [Query loop and streaming](../reference/query-engine.md)
5050+- [Compaction](../reference/compaction.md)
+6
docs-site/docs/workflows.md
···7575 Tool-->>Query: result
7676 Query-->>REPL: update transcript
7777```
7878+7979+## See also
8080+8181+- [Architecture overview](architecture.md)
8282+- [State and data flow](system-design/state-and-data-flow.md)
8383+- [Official docs map](official-docs-map.md)
···66- Local preview: `cd docs-site && python3 -m venv .venv && source .venv/bin/activate && pip install -r requirements.txt && mkdocs serve`
7788The file **`directory-structure.md`** in this folder remains a quick `src/` layout reference duplicated in the site under **Appendix → Directory structure**.
99+1010+The published site also includes **System design**, **Developer**, **Guides** (greenfield agent CLI + docs CI), and **Glossary**; see navigation in `docs-site/mkdocs.yml`.