CodeGraph 把「agent 慢慢 grep + Read 重建結構」的成本,變成一次性的索引建置:它用 tree-sitter 把整個專案解析成符號與邊,存進本機 SQLite(FTS5 全文搜尋),之後 agent 問結構問題就一次呼叫拿到答案。
CodeGraph hands the agent the exact code it needs in one call. It's a pre-built knowledge graph of every symbol, call edge, and dependency in your codebase — so instead of crawling files, the agent asks one question and gets back the relevant source, the call paths between those symbols (including dynamic-dispatch hops grep can't follow), and the blast radius of a change. Surgical context, not a file-by-file search.
原始碼 → ① 抽取 Extraction → ② 儲存 Storage → ③ 解析 Resolution → ④ 自動同步 Auto-Sync
│ │ │
Rust kernel (tree-sitter) SQLite (.codegraph/) ReferenceResolver
20 語言原生編譯 WAL + FTS5 import + 名稱比對 + 框架模式
解析引擎是原生 Rust kernel:20 個語言(TypeScript、JavaScript、Java、Python、Go、C、C++、Rust、C#、Ruby、PHP、Swift、Kotlin、Scala、Dart、R、Lua、Luau…,Metal/CUDA 走 C++ 路徑)在編譯好的程式裡解析,每個檔案只跨界一次。抽取節點(function、class、method…)與邊(calls、imports、extends、implements…)。每個語言都要先在真實 repo 上證明與參考引擎逐位元組一致才出貨。
剩下的語言(Svelte、Vue、Astro、Liquid、Delphi…)與 per-file 回退用可攜引擎執行同一套抽取邏輯,產出相同的圖。
一切進本機 SQLite(.codegraph/codegraph.db),node:sqlite + WAL 模式,附 FTS5 全文搜尋。Rust kernel 產出的邊都驗證過 byte-for-byte,且自動擴充到機器能力:worker pool、parallel resolution、analysis cache 依實際核心數(container-aware)、可用 RAM、與專案實際解析成本來決定。
抽取後把引用解析成真正的連結:function call → 定義、import → 來源檔、class 繼承、以及框架特定模式(Express route、Laravel controller、Spring annotation…)。dynamic-dispatch 跳躍(callback、React re-render、interface → impl)是 grep 追不到的,CodeGraph 補上。
MCP server 用原生 OS 檔案事件(FSEvents / inotify / ReadDirectoryChangesW)watch 專案,debounce(預設 2 秒)後做增量同步。你改程式,圖就更新,不用手動 re-run。細節見 自動同步機制。
| 種類 | 內容 |
|---|---|
| NodeKind | file module class struct interface trait protocol function method property field variable constant enum enum_member type_alias namespace parameter import export route component union |
| EdgeKind | contains calls imports exports extends implements references type_of returns instantiates overrides decorates |
在 codegraph 自己的源碼上跑 codegraph init(v1.5.0):
◆ Indexed 570 files
● 12,733 nodes, 44,903 edges in 1.7s
$ codegraph status (節錄)
Backend: node:sqlite — built-in (full WAL)
Journal: wal
Nodes by Kind:
function 2,706
import 2,304
method 1,850
class 206
route 16
看到 route: 16——CodeGraph 在自己 repo 上也偵測到了 16 個框架路由節點。
CodeGraph 的核心理念是「空間換時間」:先花一次時間把整個專案解析成結構化的知識圖譜,之後每次查詢都只需要毫秒級的圖遍歷,而不是每次都重新掃描檔案。
四階段的關鍵洞察:
# 步驟 1:選擇一個專案
cd ~/my-project
# 步驟 2:初始化(建 .codegraph/ 目錄 + 建圖)
codegraph init
# 輸出:
# ◆ Indexed 570 files
# ● 12,733 nodes, 44,903 edges in 1.7s
# 步驟 3:查看統計
codegraph status
# 輸出:
# Backend: node:sqlite — built-in (full WAL)
# Nodes by Kind:
# function 2,706
# import 2,304
# method 1,850
# 步驟 4:查詢特定符號
codegraph query UserService
# 找到 class 定義、method、相關 import
# 步驟 5:查看呼叫關係
codegraph callers UserService.findById
# 找出誰呼叫了這個 method
# 步驟 6:分析影響半徑
codegraph impact UserService --depth 2
# 找出改動 UserService 會影響哪些檔案
關鍵觀察:整個過程只需要 init 一次,之後所有查詢都在毫秒內完成。相比之下,傳統的 grep + Read 方式每次都要重新掃描檔案。
| 症狀 | 可能原因 | 診斷方法 | 解法 |
|---|---|---|---|
| 索引很慢 | 大目錄未排除 | codegraph status 查看 Nodes by Kind | 在 .gitignore 排除 node_modules、dist 等 |
| 缺少某些符號 | 語言不受支援或被 gitignore | codegraph files 查看已索引檔案 | 確認副檔名在支援清單,或用 include 強制納入 |
| 解析失敗 | 語法錯誤或不完整 | codegraph status 查看 error 計數 | 修正語法錯誤後執行 codegraph sync |
codegraph init 建立索引codegraph status 檢查索引健康度codegraph query 找到特定符號前面的範例是「從零建立圖譜」。這裡是反過來——當 MCP 給出的答案看起來不對時,如何用 CLI 逐層驗證,隔離問題出在哪一層。這是在團隊中建立對圖譜信任的關鍵流程。
# ① 圖真的新鮮嗎?(排除「過期」)
codegraph status # 看 Last sync + Pending sync
# ② 圖裡真的有這個符號嗎?(排除「漏索引」)
codegraph query UserService --limit 20
# ③ 邊真的對嗎?(排除「resolver 誤判」)
codegraph callers UserService.update
codegraph node UserService.update # 看 caller/callee 軌跡
# ④ 符號與磁碟一致嗎?(排除「切片錯位」)
codegraph node "src/services/userService.ts:45" # 帶行號讀檔
# ⑤ 才輪到「agent 是否誤讀輸出」——這是唯一需要懷疑 agent 的層
為什麼走這條路徑:MCP 與 CLI 驅動同一個 CodeGraph class,所以「CLI 驗證」與「agent 看到的」是同一份圖。逐層隔離(新鮮度 → 存在性 → 邊 → 切片)讓你在怪罪 agent 之前,先排除四個 100% 可機械驗證的層。通常問題出在前三層之一,而修正就是一個 sync 或一個 resolver 修正。
內部架構:確定性是信任的來源。四階段全部是確定性的:抽取由 tree-sitter AST 決定、儲存進 SQLite(WAL + FTS5)、解析靠 import/名稱/框架規則、同步靠檔案事件。沒有 LLM、沒有 embedding——這保證「同一個輸入一定產出同一個圖」。效能上的幾個工程細節:worker pool 依實際核心數(container-aware)與可用 RAM 決定規模;解析期 WAL 有 backpressure(4.6GB DB 解析期 WAL 曾長到 22GB);FTS 在 bulk load 期間「丟掉 per-row trigger、最後重建一次」。
foo() 呼叫 bar(),是因為 AST 裡有 call 節點,不是因為「讀懂」了程式。這同時是它的強項(可驗證、可重現)與天花板(dynamic dispatch、reflection 只能靠合成邊補)。另一個誤解:以為 FTS5 搜尋是「全文內容搜尋」像 grep——它比對的是符號名稱與 metadata,不是原始碼內文。| 症狀 | 可能原因 | 解決方案 |
|---|---|---|
| index 極慢(HDD 上) | WAL autocheckpoint 重寫熱頁 | 確認 WAL 延遲 + valve 啟用(#1231:19 分鐘 → 45 秒) |
| 符號數對不上預期 | 語言走 fallback、generated 沒排除 | status 看 language 分佈;確認 generated banner 偵測 |
| FTS 搜不到剛索引的符號 | FTS 延遲重建窗口內 | 等下次 open 自癒,或 codegraph sync |
| agent 拿到的答案與磁碟不符 | debounce 窗口(剛編輯完) | 看 staleness banner 並直接 Read 該檔 |
| 重跑 index 數字跳變 | 檔案變動,或引擎版本不同 | 對照 extraction-version 與 git state,統一環境 |
從拿到一個陌生專案到能讓 agent 即時回答架構問題,以下是零知識起點的完整流程:
# ① 克隆專案
git clone https://github.com/gin-gonic/gin.git
cd gin
# ② 一次 init 完成建圖(零設定,副檔名自動分派 Go)
codegraph init
# ◆ Indexed 487 files
# ● 8,921 nodes, 23,456 edges in 0.8s
# ③ 確認四階段都跑完
codegraph status
# Backend: node:sqlite — built-in (full WAL)
# Journal: wal
# Nodes by Kind:
# function 1,203
# method 876
# import 654
# route 18 ← 框架路由也自動偵測
# ④ 啟動 MCP server,讓 agent 可即時查詢
codegraph serve --mcp &
# ⑤ 在 agent session 中開始問問題
# agent 呼叫 codegraph_explore:
# "gin 的 HTTP handler 是怎麼被路由到的?"
# 回傳:route 節點 → handler 函式 → middleware 鏈 → 相關 test
# ⑥ 自動同步驗證:改一支檔案,等 2 秒
echo "// test change" >> gin.go
sleep 3
codegraph status # Pending sync 已清空,圖已更新
# ⑦ 用 CLI 驗證 agent 的答案
codegraph callers "gin.Default" --limit 5
codegraph impact "gin.Default" --depth 2
關鍵觀察:四階段全部是確定性的——抽取由 tree-sitter AST 決定、儲存進 SQLite WAL、解析靠 import/名稱/框架規則、同步靠檔案事件。沒有 LLM、沒有 embedding,同一個輸入一定產出同一個圖。
| 面向 | 考量 | 實務建議 |
|---|---|---|
| 效能 | 索引時間 vs 查詢時間的 trade-off | 索引是一次性成本(幾秒到幾分鐘);查詢是毫秒級。大型 repo 先排除 node_modules/dist;debounce 預設 2s 可用 CODEGRAPH_WATCH_DEBOUNCE_MS 調 |
| 品質 | 確定性抽取的可驗證性 | 同一輸入必產同一圖——可重現、可 diff、可信任。FTS5 搜的是符號名稱,不是檔案內文,搜尋範圍要搞清楚 |
| 安全 | 本地 SQLite 的資料保護 | `.codegraph/` 目錄應加入 .gitignore;WAL 模式有讀寫並行安全;多 process 寫入會觸發 lock |
| 面向 | 本文(運作原理) | 相關文 | 差異說明 |
|---|---|---|---|
| 四階段 vs 模組 | 動態執行流程(抽取→儲存→解析→同步) | 架構總覽 | 運作原理講「怎麼跑」,架構總覽講「哪些模組負責什麼」 |
| 確定性 | 強調「無 LLM、可重現」 | 隱私與安全 | 運作原理的確定性是品質保證,安全頁的確定性是資料不外洩 |
| auto-sync | 三層機制的觸發時機 | 自動同步機制 | 本文簡述,auto-sync 頁深入 debounce/banner/catch-up 細節 |
| 效能數字 | dogfooding 實測(570 檔 1.7s) | 效能基準 | 運作原理是單機數字,效能基準是 WITH/WITHOUT 對照 |
codegraph init 建立索引,並用 codegraph status 驗證四階段完成
- [ ] 能解釋 tree-sitter 比正則表達式精準在哪(巢狀結構、註解、字串中的假代碼)
- [ ] 能區分 FTS5 搜尋「符號名稱」vs grep 搜尋「檔案內文」的差異
- [ ] 能觀察 auto-sync 的 debounce 窗口(修改後 status 顯示 pending → 等待 → 清空)
- [ ] 能解釋「worker pool 依實際核心數 + 可用 RAM 決定規模」的 container-aware 設計