src/graph/ · callers/callees/impact/path 的引擎圖層把 SQLite 裡的 nodes/edges 變成「可以問的圖」:GraphQueryManager 做高階查詢(誰呼叫誰、影響誰),GraphTraverser 做底層遍歷(BFS/DFS、遞迴、路徑、型別階層)。MCP 的 codegraph_callers/callees/impact 與 CLI 同名指令、buildContext 的 blast-radius 摘要,全都是在這層上面。
traverseBFS 從起點沿呼叫邊擴散,適合找「最近 / 依深度分層」的依賴;traverseDFS(含 dfsRecursive)深入單一路徑,適合找 call graph 的完整展開。兩者都支援方向(inbound callers / outbound callees)與深度上限。
getCallers(id) 沿 inbound 邊收集呼叫者;getCallersRecursive 追到傳遞閉包。注意這裡有 getCallGraph——把某個符號的呼叫圖整棵拉出來。這是「impact 分析」的圖論基礎。
getImpactRadius(id, depth) 以受限深度沿依賴方向展開,回傳受影響符號集合——就是 codegraph impact 與 explore 的 blast-radius 摘要。實測:改 GraphTraverser(depth 1)有 39 個受影響符號(見下)。
getTypeHierarchy(含 getTypeAncestors/getTypeDescendants)沿 implements/extends 邊建型別樹;findPath 找 A→Z 的呼叫路徑——explore 回傳「相關符號之間的呼叫路徑」就是它。dynamic-dispatch 合成邊也參與路徑,所以 grep 追不到的那種跳躍在這裡看得到。
把 traverser 的底層操作組合成「一個任務需要的相關符號集合」:依關鍵字/檔案/符號名找候選 → 用 call-edge 排序 → 帶出原始碼。explore 的「found 35 symbols across 2 files + blast radius + relationships」輸出就是這層的成果。
Impact of changing "GraphTraverser" — 39 affected symbols:
src/graph/traversal.ts
class GraphTraverser:34
method traverseBFS:48
method traverseDFS:154
method getCallers:261
method getCallees:319
method getImpactRadius:520
method findPath:617
...(共 39 個受影響符號)
圖層是 CodeGraph 的「查詢引擎」——它能把 SQLite 裡的 nodes/edges 變成「可以問的圖」。
兩種元件的分工:
# 步驟 1:找出改動的影響半徑
codegraph impact GraphTraverser --depth 1
# Impact of changing "GraphTraverser" — 39 affected symbols
# 步驟 2:查看具體的受影響符號
codegraph impact GraphTraverser --depth 1 --json | jq '.affected[].name'
# "traverseBFS"
# "traverseDFS"
# "getCallers"
# ...
# 步驟 3:找出最短路徑
codegraph explore "find path from init to buildContext"
# 找到:
# init → indexAll → sync → buildContext
# 或
# init → open → buildContext
# 步驟 4:查看型別階層
codegraph node ExtractionOrchestrator --show-hierarchy
# 找到:
# ExtractionOrchestrator extends BaseExtractor
關鍵觀察:從「改一個符號」到「知道會影響 39 個符號」,整個分析只需要一次查詢。
| 症狀 | 原因 | 解法 |
|---|---|---|
| 影響範圍過大 | depth 設太高 | 降低 --depth 參數 |
| 找不到路徑 | 兩符號無關聯 | 用 codegraph callers 檢查是否有連接 |
| 結果不完整 | 索引過期 | 執行 codegraph sync 更新 |
codegraph impact 分析改動影響codegraph explore 找到兩符號間的路徑--json 取得結構化的影響分析前面的範例是單一符號的影響分析。這裡是把圖層當架構健康檢查:找出循環依賴(A→B→A)與「依賴方向違反架構規則」的隱形路徑。
# ① 找循環:對每個「高風險」核心符號檢查其 callers 是否反向回自己
codegraph callers CoreEngine --depth 3 --json |
jq '.callers[] | select(.symbol | test("CoreEngine"))'
# ② 反轉依賴:domain 層不該 import infra 層
# 對 domain 符號查 callees,看有沒有連到 infra 目錄
codegraph callees PaymentDomain --depth 4 --json |
jq '.callees[] | select(.file | test("/infra/"))'
# ③ 驗證「最短路徑」連結:A 到 Z 是否真的通
codegraph explore "find path from checkout to EmailSender"
# ④ 影響半徑分層:找「距離核心最近的葉子」
codegraph impact CoreEngine --depth 1 # 第一層
codegraph impact CoreEngine --depth 2 # 第二層(對比層級)
為什麼走這條路徑:架構反轉與循環依賴是 code review 最難人工抓的——它們藏在數百層呼叫之間。callees 沿 outbound 邊下鑽、findPath 驗證可達性、impact 的分層(depth 1 vs 2)剛好對應「直接 vs 間接」依賴——把「依賴方向」變成可查詢的結構。
內部架構:BFS vs DFS 是刻意選擇。traverseBFS 沿呼叫邊分層擴散,適合「依深度分層」的 impact 分析;traverseDFS(含 dfsRecursive)深入單一路徑,適合完整展開 call graph。getCallers 回傳直接 caller,getCallersRecursive 追到傳遞閉包;getImpactRadius(id, depth) 以受限深度沿依賴方向展開——就是 explore 的 blast-radius 摘要。findPath 找 A→Z 的最短路徑,且dynamic-dispatch 合成邊也參與——所以 grep 追不到的那種跳躍(callback、interface→impl)在這裡看得到。官方實測:改 GraphTraverser(depth 1)有 39 個受影響符號。
codegraph callers 回傳的是「所有 caller(含間接)」。實際 getCallers 只回傳直接 caller,間接的要用 getCallersRecursive 或 impact 的 depth。另一個誤解:以為「BFS 一定比 DFS 好/快」。兩者是查詢形狀的選擇——找「最近的依賴」用 BFS、找「一條路的完整展開」用 DFS,效能取決於圖的 fan-out 結構。| 症狀 | 可能原因 | 解決方案 |
|---|---|---|
| impact 爆炸性大 | depth 太高,或圖有循環 | 降 --depth;用 BFS 分層觀察 |
| findPath 找不到路徑 | 兩符號無關,或 dynamic-dispatch 邊沒建 | codegraph callers 檢查直接連接;確認 resolver 有跑 |
| 型別階層缺層 | implements/extends 邊未解析 | 檢查 resolver 第二輪(chained-call 依賴它) |
| 查詢變慢 | depth 大 + 圖 fan-out 大 | 限制 depth 或縮小起點集合 |
| 結果混入測試檔 | 測試也被索引 | 過濾 kind 或用 file pattern 排除 test 路徑 |