src/index.ts — CodeGraph class

對外唯一入口:library、CLI、MCP server 全部驅動這一個 class
檔案:src/index.ts · 分層 pipeline 的「總電源開關」

大方向

src/index.ts 定義 CodeGraph class,把四層接起來:db → extraction → resolution → graph/context,並 re-export 全部型別與底層積木(DatabaseConnection、QueryBuilder、FileWatcher…)。constructor 是 private,一律用 static factory(init/open/recreate)。關鍵設計:同一份 API 給 library 用、也給 CLI 與 MCP server 用,所以這裡的每個方法你都能在 CLI 指令與 MCP 工具上看到對應。


1. 生命週期:init / open / recreate / close

CodeGraph.init / open static建新專案(可含初始索引)或開既有專案(可含 sync)。

init:initGrammars() 載入 tree-sitter grammars → 檢查是否已初始化(是則 throw)→ createDirectory → DatabaseConnection.initialize 建 .codegraph/codegraph.db → 依 options.index 決定要不要立刻 indexAll。

open:先 validateDirectory 驗證結構,再 DatabaseConnection.open,依 options.sync 決定要不要先增量同步。一個先後差異:init 預設不索引,open 預設不 sync——都要明確傳旗標。

教學重點:這解釋了 CLI 的 codegraph init 為何「建目錄 + 建圖一步完成」——init 呼叫內部會傳 { index: true }。
reopenIfReplaced src/index.ts在途修復「.codegraph 被刪重建」後的陳舊 DB handle。

如果 .codegraph/ 被移除又在同一路徑重建(git worktree 移除重加、rm -rf .codegraph + codegraph init),我們開著的 fd 指向已 unlink 的 inode,永遠看不到新索引。此方法偵測到就先開新檔(失敗則舊 handle 留著、下次再試),成功後在途交換,MCP daemon 的既有 connection 不用重啟就自癒。POSIX-only(Windows 上已開檔無法 unlink)。

close src/index.ts釋放資源:unwatch、release file lock、close db。

三個動作依序:停止 watcher → 釋放跨 process 的 file lock → 關 SQLite。順序很重要——先停 watcher 才不會在 close 過程中又觸發 sync。

2. 索引:indexAll / sync

indexAll src/index.ts全量索引——用 mutex + file lock 防並發,WAL 延遲 checkpoint 提速。

核心流程(大量工程細節都藏在這裡):

  • 並發防護:indexMutex(in-process)+ fileLock(跨 process,CLI/MCP/git hooks 共用)——拿不到 lock 就回傳「另一 process 正在索引」的錯誤結果。
  • WAL 延遲 checkpoint:預設 1000-page 的 autocheckpoint 在大量寫入時會重寫熱頁(實測 ~95% 的磁碟 I/O)。WalCheckpointValve 在 worker thread 背景被動 checkpoint,runMaintenance 收尾。CODEGRAPH_NO_WAL_DEFER=1 可關。
  • Fast-init:全新 DB 時暫時用 journal_mode=MEMORY + synchronous=OFF 換速度(可棄置,crash 就重跑)。
  • 階段順序:標記 index_state=indexing → bulk parse(FTS trigger 延後重建)→ resolver re-init + runPostExtract → batch resolution → chained/conformance 第二輪 → 重算節點數 → 標記 complete/partial/failed。
  • 完整性對帳:discovered ≠ indexed+skipped+errored 時標 partial 並附警告。
教學重點:`index_state` metadata 讓 codegraph status 能分辨「完整索引」vs「被 kill 的截斷索引」,不會靜默提供部分結果。
sync src/index.ts增量同步——只處理變動檔,含 failed-ref 重試與 orphan 清掃。

與 indexAll 同鎖、同 WAL 延遲策略。差異在範圍:

  • git fast path:有 git 資訊時只載 變動檔案的 unresolved refs 來解析;沒 git 就全量 batch。
  • failed-ref 重試(#1240):變動檔新增了 symbol,讓沒變動的檔先前解析失敗的 refs 現在能成——依變動檔的 symbol 名查回並重解析。
  • CG-33 rebind:定義增減會讓「沒被 sync 碰到」的檔的邊失效——用 definitionDelta 復活 stale resolution edges(CODEGRAPH_NO_REBIND=1 可關)。
  • orphan 清掃(#1187):被 kill 的解析 pass 留下 pending rows;healthy index 上這只是一次 COUNT query。
教學重點:sync 不是「重新掃描」,是「只補變動」+「重試失敗」+「修復側邊影響」三件事。

3. 查詢與上下文

searchNodes / getCallers / getImpactRadius src/index.ts對 GraphQueryManager / GraphTraverser 的高階包裝。

這些方法把底層查詢包成簡潔 API:searchNodes(name) 走 FTS5;getCallers(id)/getCallees(id) 走 traverser;getImpactRadius(id, depth) 算影響半徑。MCP 的 codegraph_node/callers/impact 工具與 CLI 同名指令就是它們的薄殼。

buildContext src/index.ts把圖變成「給 AI 讀的上下文」(markdown/JSON)。

組出回答某個任務需要的相關節點原始碼 + 關係,格式可由參數選(markdown/JSON)。這是 codegraph explore 輸出的資料來源——「一次呼叫拿到相關符號原始碼 + 呼叫路徑 + blast radius」的組裝點。

4. 檔案監看

watch / unwatch src/index.ts用原生 OS 檔案事件自動同步;watch 回呼把 sync 結果回傳。

watch 建 FileWatcher(FSEvents/inotify/RDCW)並接一個 async 回呼:事件 → sync({ paths }) → 回傳變動數與耗時。特別處理「sync 回傳零 shape = lock 拿不到」→ throw LockUnavailableError,讓 watcher 保留 pending files 並重排。getPendingFiles() 就是 MCP 工具附加 staleness banner 的資料來源。

看完這頁你應該能說出:constructor 為什麼是 private(全走 static factory)、init 與 open 的預設行為差異、indexAll 的 WAL 延遲與 fast-init 兩個加速、sync 比「重新掃描」多了哪三件事、以及 watch 回呼如何處理 lock 失敗。

🎓 教學解說

CodeGraph class 的設計哲學是「單一職責、明確介面」:

Worked Example:比較 init 與 open 的行為

init vs open 實測
# 場景 1:新專案用 init
mkdir new-project && cd new-project
codegraph init
# ◆ Initialized CodeGraph
# (不自動索引,需手動 codegraph index)

# 場景 2:既有專案用 open(透過 library)
const cg = await CodeGraph.open('/path/to/existing');
// 不自動 sync,需手動 cg.sync()

# 場景 3:init + 自動索引
codegraph init --index
# ◆ Initialized CodeGraph
# ◆ Indexed 570 files
# ● 12,733 nodes, 44,903 edges in 1.7s

# 場景 4:open + 自動 sync
const cg = await CodeGraph.open('/path/to/existing', { sync: true });
// 自動同步變動

關鍵觀察:init 和 open 的預設行為不同,這讓你可以根據場景選擇最適合的方式。

常見錯誤與診斷

錯誤訊息原因解法
Already initialized重複執行 init用 codegraph open 開啟既有專案
Not initialized用 open 開啟未初始化的專案先執行 codegraph init
Lock unavailable另一 process 正在索引等待或執行 codegraph unlock
Database is locked並發寫入衝突確認只有一個 CodeGraph 實例在跑

練習:驗收清單


🔍 進階真實情境 Worked Example:長壽 MCP daemon 的 .codegraph 被刪重建自癒

前面的範例比較 init 與 open 的預設行為。這裡是長壽進程的真實災難:git worktree 被移除再重加,或有人 rm -rf .codegraph 後重新 codegraph init——你開著的 fd 指向已 unlink 的 inode,永遠看不到新索引。

自癒工作流
# ① 災難:.codegraph 被刪重建
rm -rf .codegraph && codegraph init

# ② 舊的 MCP daemon connection 還開著——但它不自知
codegraph status      # 統計還是「舊的」嗎?

# ③ reopenIfReplaced 接手:偵測到 isReplacedOnDisk()
#    → 先開新檔(失敗則舊 handle 留著、下次再試)
#    → 成功後在途交換 handle
#    → 既有 connection 不用重啟就自癒(POSIX-only)

# ④ 驗證自癒
codegraph status      # 應該看到新索引的統計
codegraph query ExtractionOrchestrator --limit 5

為什麼走這條路徑:重啟 server 是治標,reopenIfReplaced 是治本——它把「.codegraph 被刪重建」當成正常事件而非例外,MCP daemon 的既有 connection 在途交換 handle 後繼續服務。這是「長壽進程 × 可變索引目錄」這個組合下,最容易忽略的穩健性細節。

🔬 深入原理擴充

內部架構:indexAll 的加速細節。① WAL 延遲 checkpoint:預設 1000-page autocheckpoint 在大量寫入時會重寫熱頁(實測 ~95% 磁碟 I/O),WalCheckpointValve 在 worker thread 背景 PASSIVE checkpoint;② Fast-init:全新 DB 暫時用 journal_mode=MEMORY + synchronous=OFF(可棄置,crash 就重跑);③ index_state 標記 indexing/complete/partial/failed——讓 status 能分辨「完整索引」vs「被 kill 的截斷索引」,不會靜默提供部分結果。sync 則有三件「比重新掃描多的事」:failed-ref 重試(#1240)、CG-33 rebind(定義增減讓未變動檔的邊失效的修復)、orphan 清掃(#1187)。

大家以為分析正確但其實有誤:以為「init 會自動建圖」。實際init 預設不索引——是 CLI 的 codegraph init 內部傳了 { index: true };library 呼叫 CodeGraph.init(root) 預設只建目錄與 DB。另一個誤解:以為 sync 是 index 的迷你版(跑同一套邏輯、範圍小一點)。實際 sync 是不同的演算法——它針對「變動檔」設計,還做 rebind 與 orphan 清掃,這些全量 index 反而沒有。

🩺 診斷式疑難排解表

症狀可能原因解決方案
Already initialized重複 init用 codegraph open 或先 uninit
索引顯示 partial 而非 complete被 kill 的截斷索引看 index_state;codegraph index --force 重跑
刪了 .codegraph 後查詢無視新索引fd 指向 unlink 的 inodereopenIfReplaced 會自癒;Windows 上無法 unlink,需重啟
watch 回呼 throw LockUnavailableError另一 process 正在索引這是設計行為——保留 pending files 重排
大型專案 index 卡在磁碟 I/OWAL autocheckpoint 重寫熱頁確認 valve + WAL 延遲啟用(#1231)

🧩 進階挑戰題

  1. reopenIfReplaced 為何是 POSIX-only?Windows 上「已開檔無法 unlink」的後果是什麼,替代方案為何?
  2. 拆解 sync 的「三件額外的事」:failed-ref 重試、rebind、orphan 清掃各自解決哪種 drift?請各給一個具體情境。
  3. Fast-init 用 journal_mode=MEMORY + synchronous=OFF——為什麼對「全新 DB」安全?如果 crash 發生在窗口內,資料會如何被恢復?