架構概覽

Claw Code 架構遵循雙層設計:管理代理工作階段、命令路由和 LLM 互動的 Python 協調層,搭配處理 API 通訊、工具執行、終端機渲染和安全性的 Rust 效能層。這種分離使高層代理邏輯保持表達力且易於修改,同時將延遲敏感的操作推入編譯的、記憶體安全的程式碼中。

在最高層級,每個使用者互動流經三個階段:啟動(環境探索、設定載入、模式路由)、查詢引擎執行(包含工具呼叫和訊息壓縮的回合迴圈),以及回應渲染(在終端機中進行語法高亮的串流 markdown)。每個階段都由兩層中定義明確的模組支援。

 User Terminal (REPL / stdin)
       |
       v
 +------------------------------------------+
 |          Rust CLI Binary                  |
 |  rusty-claude-cli  (crossterm, syntect,   |
 |  pulldown_cmark, braille spinner)         |
 +------------------------------------------+
       |
       v
 +------------------------------------------+
 |         Python Orchestration Layer        |
 |  bootstrap_graph.py  (7 stages)          |
 |  runtime.py  (route_prompt, run_turn)    |
 |  query_engine.py  (max_turns=8, stream)  |
 |  commands.py  |  tools.py  |  models.py  |
 |  context.py   |  session_store.py        |
 |  transcript.py | execution_registry.py   |
 |  tool_pool.py  | parity_audit.py         |
 |  + 50 more modules                       |
 +------------------------------------------+
       |              |              |
       v              v              v
 +-----------+  +-----------+  +-----------+
 | Rust api  |  | Rust      |  | Rust      |
 | crate     |  | runtime   |  | tools     |
 | Anthropic |  | 16 modules|  | 19 specs  |
 | client,   |  | bash,file |  | JSON      |
 | SSE,OAuth |  | ops,mcp,  |  | schemas   |
 | retry     |  | oauth,    |  |           |
 |           |  | session,  |  |           |
 |           |  | prompt,   |  |           |
 |           |  | usage     |  |           |
 +-----------+  +-----------+  +-----------+
       |              |
       v              v
 +-----------+  +---------------+
 | Rust      |  | Rust          |
 | commands  |  | compat-       |
 | 15 slash  |  | harness       |
 | commands  |  | (bridge       |
 |           |  |  layer)       |
 +-----------+  +---------------+
       |
       v
   Anthropic API  (api.anthropic.com)

Python 協調層 (src/)

Python 工作空間包含60 多個模組,組織成範圍緊密的子系統。每個模組負責單一職責,使程式碼庫易於導航和獨立測試。以下是每個主要子系統的詳細分解。

啟動圖 — bootstrap_graph.py

啟動序列是每個 claw-code 工作階段的進入點。它依序執行 7 個階段,每個階段都以前一個階段成功完成為前提:

  1. 預擷取 — 在其他任何操作執行之前預載參考資料並暖快取。
  2. 警告處理器 — 附加全域警告過濾器,使第三方函式庫的嘈雜棄用訊息不會洩漏到使用者終端機。
  3. CLI 解析器 — 解析命令列引數和旗標,儘早確定執行模式。
  4. 設定 + 命令平行載入 — 同時執行環境設定和命令快照載入以加速啟動。
  5. 延遲初始化 — 初始化依賴於已解析 CLI 狀態和已載入設定的元件。
  6. 模式路由 — 將執行路由到六種模式之一:localremotesshteleportdirect-connectdeep-link
  7. 查詢引擎提交迴圈 — 將控制權交給查詢引擎以進行互動式回合迴圈。

查詢引擎 — query_engine.py

查詢引擎是中央協調樞紐。它管理使用者、LLM 和工具系統之間的對話迴圈。關鍵設定值定義在 QueryEngineConfig 資料類別中:

Each turn produces a TurnResult, and the QueryEnginePort class exposes session management, message compaction, and streaming. The streaming interface yields six distinct event types: message_start, command_match, tool_match, permission_denial, message_delta, and message_stop. This event-driven design allows the terminal renderer to paint output incrementally as tokens arrive.

執行環境 — runtime.py

執行環境模組橋接原始使用者輸入和查詢引擎。它提供 PortRuntime,暴露兩個關鍵方法:

命令 — commands.py

命令清單在啟動時從 reference_data/commands_snapshot.json 載入。它暴露包含五個欄位的 CommandExecution 資料類別:namesource_hintprompthandledmessage。四個公開函式提供命令 API:

工具 — tools.py

工具清單反映命令系統但用於工具呼叫。它從 reference_data/tools_snapshot.json 載入並定義 ToolExecution 資料類別。關鍵函式包括:

核心資料模型 — models.py

models.py 模組定義在系統每一層流動的共享資料型別:

資料類別欄位用途
Subsystemname, path, file_count, notes表示工作空間中已發現的子系統
PortingModulename, responsibility, source_hint, status追蹤單一模組的移植進度
PermissionDenial記錄工具呼叫被權限阻擋的情況
UsageSummaryinput_tokens, output_tokens (word-count proxy)用於成本管理的 token 使用追蹤
PortingBacklog彙總所有待處理的移植工作

支援 Python 模組

除了核心子系統外,Python 層還包括數十個聚焦的模組:

Rust 效能層 (rust/)

Rust 工作空間被組織為 6 個 crate 的 Cargo 工作空間。每個 crate 獨立編譯,實現增量建構和清晰的依賴邊界。它們共同提供 Python 層呼叫的高效能基礎。

api Crate — Anthropic API 客戶端

The api crate encapsulates all communication with the Anthropic API. Its AnthropicClient handles authentication, retries, and streaming:

API 常數

DEFAULT_BASE_URL = "https://api.anthropic.com"ANTHROPIC_VERSION = "2023-06-01"DEFAULT_MAX_RETRIES = 2

runtime Crate — 16 個模組

The runtime crate is the largest in the Rust workspace with 16 modules covering everything from bash execution to OAuth flows:

bootstrap

啟動(12 個階段)

From CliEntry to MainRuntime through 12 sequential phases that progressively build the execution environment.

conversation

ConversationRuntime

Core turn loop with ApiClient + ToolExecutor traits. Max iterations capped at 16 per conversation round.

compact

CompactionConfig

preserve_recent = 4 messages, max_estimated_tokens = 10000. Keeps context window lean without losing critical state.

config

設定(3 個來源)

ConfigSources: User, Project, and Local. Discovers settings.json files at each level with cascading priority.

file_ops

檔案操作

Read, write, edit, glob, and grep with a 250 head_limit for search results and 100 glob truncation cap.

permissions

權限系統

PermissionMode: Allow, Deny, or Prompt. PermissionPolicy supports per-tool permission modes.

prompt

系統提示建構器

MAX_INSTRUCTION_FILE_CHARS = 4000, MAX_TOTAL_INSTRUCTION_CHARS = 12000. Discovers and assembles CLAUDE.md files.

mcp

MCP 整合

Name normalization with mcp__{server}__{tool} convention. 6 transport types: Stdio, SSE, HTTP, WebSocket, SDK, and ClaudeAiProxy.

其他執行環境模組包括:

tools Crate — 19 個工具規格

The tools crate defines 19 tool specifications, each with a full JSON Schema for parameter validation. These schemas are what the LLM sees when deciding which tool to call:

工具分類說明
bash執行在沙箱環境中執行 shell 命令
read_file檔案 I/O支援偏移量/限制的檔案內容讀取
write_file檔案 I/O寫入或覆寫檔案內容
edit_file檔案 I/O檔案內的目標字串替換
glob_search搜尋基於模式的檔案探索
grep_search搜尋由 ripgrep 驅動的正規表達式內容搜尋
WebFetch網路對外部 URL 的 HTTP 請求
WebSearch網路網路搜尋查詢
TodoWrite規劃結構化任務清單管理
Skill擴充呼叫已註冊的技能模組
Agent多代理產生子代理進行平行工作
ToolSearch探索按關鍵字搜尋延遲工具
NotebookEdit筆記本編輯 Jupyter 筆記本儲存格
Sleep工具暫停執行指定時間
SendUserMessage通訊向使用者發送訊息
Config設定讀取/寫入設定值
StructuredOutput輸出返回結構化 JSON 回應
REPL執行互動式語言 REPL 工作階段
PowerShell執行Windows PowerShell 命令執行

commands Crate — 15 個斜線命令

The commands crate implements the slash command system with 15 built-in commands. Each command is classified by a CommandSource enum: Builtin, InternalOnly, or FeatureGated.

// Available slash commands /help — Show help and available commands /status — Display session status and token usage /compact — Trigger manual transcript compaction /model — Switch the active model /permissions — View or modify tool permissions /clear — Clear the conversation transcript /cost — Show accumulated session costs /resume — Resume a previous session /config — View or edit configuration /memory — Manage CLAUDE.md memory files /init — Initialize a new project workspace /exit — Exit the session /diff — Show git diff of session changes /version — Print version information /export — Export conversation transcript

rusty-claude-cli — CLI 執行檔

The rusty-claude-cli crate is the user-facing binary. It defaults to claude-sonnet-4-20250514 as the model and provides a rich terminal experience:

compat-harness Crate

The compat-harness crate serves as the compatibility bridge between the Python orchestration layer and the Rust performance layer. It ensures that data structures, function signatures, and calling conventions remain stable across the language boundary.

資料流程:從提示到回應

理解 Claw Code 架構意味著追蹤單一使用者提示如何流經整個系統:

  1. The user types a prompt in the rusty-claude-cli REPL.
  2. The CLI passes input to the Python layer, where runtime.py tokenizes it and calls route_prompt to determine if it matches a slash command or should go to the LLM.
  3. If it is a slash command, the commands crate handles it directly. Otherwise, the query engine takes over.
  4. The query engine constructs the API request — including system prompt (built by the Rust prompt module from discovered CLAUDE.md files), conversation history (managed by transcript.py), and available tools (filtered by tool_pool.py, max 15).
  5. The Rust api crate sends the request to Anthropic's API with streaming enabled, yielding SSE events back through the Python layer.
  6. If the LLM response includes a tool call, the Rust runtime crate executes it (bash commands, file operations, etc.), checking permissions via the PermissionPolicy.
  7. Tool results feed back into the next turn. This loop continues for up to 8 turns (Python query engine) or 16 iterations (Rust conversation runtime).
  8. When the conversation accumulates more than 12 turns, the transcript is compacted — the Rust compaction module preserves the 4 most recent messages and limits estimated tokens to 10,000.

工作階段與對話記錄管理

Claw-code 透過兩個互補機制維護工作階段狀態:

On the Rust side, the session module defines the message schema with MessageRole (System, User, Assistant, Tool) and ContentBlock (Text, ToolUse, ToolResult). This shared schema ensures both layers interpret conversation history identically.

Python-TypeScript 一致性稽核

由於 claw-code 是 Claude Code 架構的潔淨室重寫,parity_audit.py 模組持續追蹤實作完整性。它維護 18 個根檔案對應31 個目錄對應,將 Python 模組對應到其 TypeScript 對應物,使識別差距和優先排序移植工作變得簡單。