# ixi-enki/claw-code architecture

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

- Interactive diagram: https://gitdiagram.com/ixi-enki/claw-code
- Repository: https://github.com/ixi-enki/claw-code

## Overview

1. **Repository shape**
   - The canonical implementation is the Rust workspace under `rust/`.
   - The top-level `src/` and `tests/` tree is a companion Python/reference surface used for audit, parity, and helper tooling, not the primary runtime.
   - The repo is organized around a CLI agent harness, provider clients, runtime orchestration, plugin/hooks, and a separate RAG service.

2. **Primary user entrypoint: `claw` CLI**
   - The main executable lives in `rust/crates/rusty-claude-cli`.
   - This binary is the user-facing surface for commands like prompt, doctor, status, resume/compact flows, and setup.
   - It delegates most behavior to shared runtime and commands crates rather than implementing agent logic directly.

3. **Core orchestration layer: `runtime`**
   - `rust/crates/runtime` is the central control plane.
   - It owns session lifecycle, conversation state, prompt construction, bootstrapping, permissions, policy enforcement, sandboxing, hooks, branch/session recovery, and task orchestration.
   - Most “agent behavior” is composed here: taking user input, shaping it into a task packet/prompt, enforcing permissions, then coordinating tool use and downstream services.

4. **API and model-provider boundary**
   - `rust/crates/api` isolates outbound model/API traffic.
   - It contains provider-specific clients for Anthropic and OpenAI-compatible backends, plus SSE/streaming, prompt caching, HTTP plumbing, and request/response types.
   - This is the boundary between the local agent runtime and external LLM services, and it is also what the integration tests exercise against mock or compatibility services.

5. **Tooling and command abstraction**
   - `rust/crates/commands` and `rust/crates/tools` hold reusable command/tool definitions and helper utilities.
   - Tools are treated as a constrained execution surface, with runtime-side permission checks and path-scope enforcement.
   - This layer is the bridge between the agent and local environment capabilities such as file operations, extraction, lane completion, and other task-oriented utilities.

6. **Plugins and hooks**
   - `rust/crates/plugins` plus `rust/crates/runtime/src/plugin_lifecycle.rs` and `hooks.rs` implement plugin discovery and hook execution.
   - Bundled examples under `rust/crates/plugins/bundled/` show the expected plugin layout and pre/post hook model.
   - Architecturally, plugins extend behavior at defined lifecycle points rather than bypassing the runtime, so they sit on the edge of the orchestration layer.

7. **Security, policy, and workspace boundaries**
   - Permission handling is first-class in `runtime` via `permissions`, `permission_enforcer`, `policy_engine`, `trust_resolver`, and related modules.
   - The repo also includes branch locks, stale branch/base handling, approval tokens, and path-scope enforcement to keep agent actions constrained.
   - These controls form a boundary around filesystem access, command execution, and any potentially destructive local operations.

8. **Session, history, and state management**
   - Session persistence is spread across `session`, `session_store`, `conversation`, `history`, `transcript`, `state`, and related modules.
   - The agent’s working memory is represented locally and reloaded across resume/compact flows.
   - There are also compression and summary mechanisms to shrink conversation context while preserving task continuity.

9. **MCP, remote integration, and live tool bridges**
   - The runtime includes MCP client/server/stdIO support, OAuth, remote runtime integration, LSP client support, and a tool bridge for invoking external capabilities.
   - This makes the agent architecture multi-protocol: local CLI on one side, and remote services or tool servers on the other.
   - If you are diagramming the system, treat MCP and remote integrations as adjacent service boundaries, not core business logic.

10. **RAG service as a separate subsystem**
   - `rust/crates/claw-rag-service` is its own service, with ingest, chunking, embedding, search, DB access, and Qdrant indexing.
   - It has a standalone server entrypoint and Dockerfile, and serves both an API and a static UI.
   - This is a distinct data pipeline: documents are ingested, chunked, embedded, indexed in Qdrant, and later searched for retrieval-augmented workflows.

11. **Testing, parity, and mock infrastructure**
   - The repo contains dedicated mock services and compatibility harnesses, including `mock-anthropic-service`, `compat-harness`, and parity scripts/tests.
   - These exist to verify protocol behavior, CLI contracts, and release readiness without depending on live external services.
   - The CI workflows and contract tests are part of the architecture because they enforce the expected interaction shapes between CLI, runtime, and providers.

12. **Python reference/audit workspace**
   - The top-level Python tree mirrors many conceptual subsystems: bootstrap, bridge, coordinator, plugins, remote, server, state, tools, and more.
   - It appears to function as a reference implementation, migration aid, and audit surface for subsystem mapping and parity validation.
   - For an architecture diagram, show it as a secondary support layer, not the production runtime.

13. **Key end-to-end flow**
   - Typical flow: user invokes `claw` CLI → CLI passes into runtime/session bootstrap → runtime builds a prompt/task packet → permissions/policy are checked → tools/hooks/plugins may be invoked → outbound model traffic goes through the API/provider layer → streaming responses and events return to the CLI, while session/history state is persisted locally.
   - Retrieval-assisted flows branch from runtime into the RAG service, which queries its indexed corpus and returns relevant context before or during model interaction.

14. **Important diagram boundaries**
   - Local machine vs. external services: model providers, MCP endpoints, Qdrant/RAG backend, and any remote tool servers.
   - Control plane vs. execution plane: CLI/runtime/policy/session management versus tools/plugins/provider calls.
   - Primary codebase vs. support systems: Rust workspace is the product; Python, parity harnesses, and CI scripts are verification/migration scaffolding.

Main entry points: [`rust/crates/runtime/src/lib.rs`](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/lib.rs) (runtime), [`rust/crates/api/src/lib.rs`](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/api/src/lib.rs) (api), [`src/main.py`](https://github.com/ixi-enki/claw-code/blob/main/src/main.py) (python ref), [`.github/workflows/rust-ci.yml`](https://github.com/ixi-enki/claw-code/blob/main/.github/workflows/rust-ci.yml) (ci checks)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_rust_workspace["Rust workspace"]
  node_cli["claw CLI<br/>binary entrypoint<br/>[main.rs]"]
  node_runtime["runtime<br/>orchestration core<br/>[lib.rs]"]
  node_api["api<br/>provider boundary<br/>[lib.rs]"]
  node_commands["commands<br/>[lib.rs]"]
  node_tools["tools<br/>[lib.rs]"]
  node_plugins["plugins<br/>plugin system<br/>[lib.rs]"]
  node_policy["policy"]
  node_session["session state<br/>persistence and memory<br/>[session.rs]"]
  node_prompt["prompt pack<br/>task shaping<br/>[prompt.rs]"]
  node_mcp_bridge["MCP bridge<br/>protocol bridge<br/>[mcp_tool_bridge.rs]"]
  node_remote["remote runtime<br/>remote integration<br/>[remote.rs]"]
  node_rag[("rag service<br/>retrieval service<br/>[main.rs]")]
end

subgraph group_support_layer["Support layer"]
  node_mocksvc["mock/compat<br/>verification harness<br/>[main.rs]"]
  node_python_ref["python ref<br/>reference surface<br/>[main.py]"]
  node_ci["ci checks<br/>verification workflows<br/>[rust-ci.yml]"]
end

subgraph group_external_services["External services"]
  node_provider_anthropic(("Anthropic<br/>llm provider"))
  node_provider_openai(("OpenAI compat<br/>llm provider"))
  node_qdrant[("Qdrant<br/>vector index")]
end

node_cli -->|"invoke"| node_runtime
node_runtime -->|"shape"| node_prompt
node_runtime -->|"enforce"| node_policy
node_runtime -->|"persist"| node_session
node_runtime -->|"lifecycle"| node_plugins
node_runtime -->|"dispatch"| node_commands
node_runtime -->|"use tools"| node_tools
node_runtime -->|"model calls"| node_api
node_api -->|"request"| node_provider_anthropic
node_api -->|"request"| node_provider_openai
node_runtime -->|"bridge"| node_mcp_bridge
node_runtime -->|"remote"| node_remote
node_runtime -->|"retrieve"| node_rag
node_rag -->|"index/search"| node_qdrant
node_mocksvc -.->|"exercise"| node_api
node_ci -.->|"verify"| node_mocksvc
node_ci -.->|"contract test"| node_runtime
node_python_ref -.->|"mirror"| node_runtime
node_python_ref -.->|"mirror"| node_tools
node_runtime -->|"resume/compact"| node_session

click node_cli "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/rusty-claude-cli/src/main.rs"
click node_runtime "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/lib.rs"
click node_api "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/api/src/lib.rs"
click node_commands "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/commands/src/lib.rs"
click node_tools "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/tools/src/lib.rs"
click node_plugins "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/plugins/src/lib.rs"
click node_policy "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/permission_enforcer.rs"
click node_session "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/session.rs"
click node_prompt "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/prompt.rs"
click node_mcp_bridge "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/mcp_tool_bridge.rs"
click node_remote "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/remote.rs"
click node_rag "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/claw-rag-service/src/main.rs"
click node_mocksvc "https://github.com/ixi-enki/claw-code/blob/main/rust/crates/mock-anthropic-service/src/main.rs"
click node_python_ref "https://github.com/ixi-enki/claw-code/blob/main/src/main.py"
click node_ci "https://github.com/ixi-enki/claw-code/blob/main/.github/workflows/rust-ci.yml"

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_cli,node_runtime,node_api,node_commands,node_tools,node_plugins,node_policy,node_session,node_prompt,node_mcp_bridge,node_remote,node_rag toneBlue
class node_mocksvc,node_python_ref,node_ci toneAmber
class node_provider_anthropic,node_provider_openai,node_qdrant toneMint
```

## Components

### Rust workspace

Primary production implementation and runtime services

- [claw CLI](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/rusty-claude-cli/src/main.rs): binary entrypoint · User-facing command surface for prompt, doctor, status, resume, compact, and setup flows · `rust/crates/rusty-claude-cli/src/main.rs`
- [runtime](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/lib.rs): orchestration core · Session lifecycle, prompt shaping, permissions, hooks, recovery, and task coordination · `rust/crates/runtime/src/lib.rs`
- [api](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/api/src/lib.rs): provider boundary · Outbound model clients, streaming, HTTP plumbing, prompt cache, and request/response types · `rust/crates/api/src/lib.rs`
- [commands](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/commands/src/lib.rs): shared commands · Reusable command abstractions used by the CLI and runtime · `rust/crates/commands/src/lib.rs`
- [tools](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/tools/src/lib.rs): local tools · Constrained local execution helpers such as lane completion and extraction · `rust/crates/tools/src/lib.rs`
- [plugins](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/plugins/src/lib.rs): plugin system · Plugin discovery and bundled hook layouts executed at lifecycle points · `rust/crates/plugins/src/lib.rs`
- [policy](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/permission_enforcer.rs): permissions and policy · Permission enforcement, trust resolution, sandboxing, branch locks, and path-scope controls · `rust/crates/runtime/src/permission_enforcer.rs`
- [session state](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/session.rs): persistence and memory · Conversation, transcript, history, state, summaries, and resume/compact persistence · `rust/crates/runtime/src/session.rs`
- [prompt pack](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/prompt.rs): task shaping · Builds task packets and prompts from user input and session context · `rust/crates/runtime/src/prompt.rs`
- [MCP bridge](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/mcp_tool_bridge.rs): protocol bridge · MCP client/server/stdIO and tool-bridge integration for external capabilities · `rust/crates/runtime/src/mcp_tool_bridge.rs`
- [remote runtime](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/runtime/src/remote.rs): remote integration · Remote runtime and OAuth-connected service integration boundary · `rust/crates/runtime/src/remote.rs`
- [rag service](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/claw-rag-service/src/main.rs): retrieval service · Standalone ingest, chunking, embedding, search, and Qdrant indexing service · `rust/crates/claw-rag-service/src/main.rs`

### Support layer

Reference surface, parity harnesses, and verification scaffolding

- [mock/compat](https://github.com/ixi-enki/claw-code/blob/main/rust/crates/mock-anthropic-service/src/main.rs): verification harness · Mock provider service and compatibility harness used by tests and parity checks · `rust/crates/mock-anthropic-service/src/main.rs`
- [python ref](https://github.com/ixi-enki/claw-code/blob/main/src/main.py): reference surface · Audit and parity-oriented Python mirror of major subsystems and tooling · `src/main.py`
- [ci checks](https://github.com/ixi-enki/claw-code/blob/main/.github/workflows/rust-ci.yml): verification workflows · Release readiness, contract checks, and workflow enforcement for the repo · `.github/workflows/rust-ci.yml`

### External services

Model, retrieval, and protocol endpoints outside the local runtime

- **Anthropic**: llm provider · External model backend accessed through the API provider clients
- **OpenAI compat**: llm provider · OpenAI-compatible backend accessed through the API provider clients
- **Qdrant**: vector index · Vector store used by the RAG pipeline for retrieval

## Connections

- claw CLI → runtime: invoke · CLI hands commands and user intent into the orchestration layer
- runtime → prompt pack: shape · Runtime converts input and context into task packets and prompts
- runtime → policy: enforce · Permissions, trust, sandbox, and path controls gate runtime actions
- runtime → session state: persist · Session and history state are loaded, updated, and resumed here
- runtime → plugins: lifecycle · Runtime triggers plugin discovery and hook execution
- runtime → commands: dispatch · Shared command abstractions are used to implement user flows
- runtime → tools: use tools · Runtime brokers constrained local tools for task execution
- runtime → api: model calls · Runtime sends prompts and receives streaming responses through the API boundary
- api → Anthropic: request · Anthropic client traffic leaves the local system through this boundary
- api → OpenAI compat: request · OpenAI-compatible client traffic leaves the local system through this boundary
- runtime → MCP bridge: bridge · Runtime connects to MCP and other external tool protocols
- runtime → remote runtime: remote · Runtime can delegate to remote runtime and OAuth-backed integrations
- runtime → rag service: retrieve · Retrieval-assisted flows branch to the RAG subsystem
- rag service → Qdrant: index/search · Documents are indexed and queried in the vector store
- mock/compat → api: exercise · Compatibility harness validates provider/client behavior against the API layer
- ci checks → mock/compat: verify · CI and parity workflows run the mock and contract harnesses
- ci checks → runtime: contract test · Workflows validate runtime behavior and release readiness
- python ref → runtime: mirror · Python reference surface mirrors major runtime and subsystem concepts
- python ref → tools: mirror · Reference tooling surface parallels the Rust tools layer
- runtime → session state: resume/compact · Working memory is reduced and restored across conversational flows
