架構概覽

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. 模式路由 — 將執行路由到六種模式之一: local, remote, ssh, teleport, direct-connect, or deep-link.
  7. 查詢引擎提交迴圈 — 將控制權交給查詢引擎以進行互動式回合迴圈。

查詢引擎 — query_engine.py

查詢引擎是核心協作中心。它管理使用者、LLM 和工具系統之間的對話迴圈。關鍵配置值定義在 QueryEngineConfig dataclass:

每個回合產生一個 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. 這種事件驅動設計允許終端渲染器在 token 到達時逐步繪製輸出。

執行環境 — runtime.py

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

命令 — commands.py

命令清單從 reference_data/commands_snapshot.json 載入。它暴露 CommandExecution dataclass with five fields: name, source_hint, prompt, handled, and message. 四個公開函式提供命令 API:

工具 — tools.py

工具清單鏡像了命令系統,但用於工具呼叫。 It loads from reference_data/tools_snapshot.json and defines a ToolExecution dataclass. Key functions include:

核心資料模型 — models.py

The 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 封裝了與 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 是 Rust 工作區中最大的,包含 16 個模組,涵蓋從 bash 執行到 OAuth 流程的所有內容:

bootstrap

啟動(12 個階段)

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

conversation

對話執行環境

核心回合迴圈,使用 ApiClient + ToolExecutor traits. 每個對話回合的最大迭代次數上限為 16

compact

壓縮配置

preserve_recent = 4 條訊息,max_estimated_tokens = 10000。保持上下文視窗精簡而不丟失關鍵狀態。

config

配置(3 個來源)

ConfigSources: 使用者、專案和本地。探索 settings.json 每個層級的檔案,具有級聯優先順序。

file_ops

檔案操作

讀取、寫入、編輯、glob 和 grep,搜尋結果的 head_limit 為 250glob 截斷上限為 100。

permissions

權限系統

PermissionMode: 允許、拒絕或提示。 PermissionPolicy 支援逐工具權限模式。

prompt

系統提示建構器

MAX_INSTRUCTION_FILE_CHARS = 4000, MAX_TOTAL_INSTRUCTION_CHARS = 12000. 探索並組裝 CLAUDE.md files.

mcp

MCP 整合

名稱標準化,使用 mcp__{server}__{tool} 慣例。6 種傳輸類型: Stdio, SSE, HTTP, WebSocket, SDK, and ClaudeAiProxy.

額外的執行環境模組包括:

tools Crate — 19 個工具規格

The tools crate 定義了 19 個工具規格,每個都有完整的 JSON Schema 用於參數驗證。這些 schema 是 LLM 決定呼叫哪個工具時所看到的:

工具分類描述
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 實現了包含 15 個內建命令的斜線命令系統。 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 是面向使用者的執行檔。它預設使用 claude-sonnet-4-20250514 作為模型,並提供豐富的終端體驗:

compat-harness Crate

The compat-harness crate 作為 Python 協作層和 Rust 效能層之間的相容性橋樑。它確保資料結構、函式簽名和呼叫慣例在語言邊界之間保持穩定。

資料流:從提示到回應

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

  1. 使用者在 rusty-claude-cli REPL 中輸入提示。
  2. The CLI passes input to the Python layer, where runtime.py 進行分詞並呼叫 route_prompt 以確定它是否匹配斜線命令或應發送到 LLM。
  3. 如果是斜線命令, commands crate 直接處理它。否則, query engine 接管。
  4. 查詢引擎建構 API 請求 — 包括系統提示 (built by the Rust prompt module from discovered CLAUDE.md files), 對話歷史 (managed by transcript.py), 和可用工具 (filtered by tool_pool.py, max 15).
  5. The Rust api crate 向 Anthropic 的 API 傳送啟用串流的請求,透過 Python 層傳回 SSE 事件。
  6. 如果 LLM 回應包含工具呼叫,Rust runtime crate 執行它(bash 命令、檔案操作等),透過 PermissionPolicy.
  7. 工具結果回饋到下一個回合。此迴圈最多持續 8 turns (Python query engine) or 16 iterations (Rust conversation runtime).
  8. 當對話累積超過 12 個回合時,對話記錄會被 壓縮 — Rust 壓縮模組保留最近的 4 條訊息並將估計 token 限制為 10,000。

工作階段與對話記錄管理

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

在 Rust 端, session module 定義訊息 schema,使用 MessageRole (System, User, Assistant, Tool) and ContentBlock (Text, ToolUse, ToolResult). This shared schema ensures both layers interpret 對話歷史 identically.

Python-TypeScript 一致性審計

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