CLI 指令參考

一個 binary 同時是安裝器、索引器與 MCP server
codegraph · 所有輸出附本站實測

完整指令表

codegraph                         # 跑互動安裝器
codegraph install                 # 跑安裝器(明確指令)
codegraph uninstall               # 從你的 agent 移除 CodeGraph,也移除 CLI(--keep-cli 只移除設定)
codegraph init [path]             # 初始化專案 + 建圖(一步完成)
codegraph uninit [path]           # 從專案移除 CodeGraph(--force 略過確認)
codegraph index [path]            # 全量索引(--force 重跑,--quiet 少輸出)
codegraph sync [path]             # 增量更新
codegraph status [path]           # 顯示統計
codegraph unlock [path]           # 移除阻擋索引的過期 lock 檔
codegraph query <search>          # 依名稱搜尋符號(--kind、--limit、--json)
codegraph explore <query>         # 相關符號原始碼 + 呼叫路徑一次拿(= codegraph_explore MCP 工具)
codegraph node <symbol|file>      # 單一符號原始碼 + caller,或帶行號讀檔
codegraph files [path]            # 顯示檔案結構(--format、--filter、--max-depth、--json)
codegraph callers <symbol>        # 找誰呼叫某函式(--limit、--json)
codegraph callees <symbol>        # 找某函式呼叫誰(--limit、--json)
codegraph impact <symbol>         # 分析改動影響(--depth、--json)
codegraph affected [files...]     # 找受改動影響的測試檔
codegraph daemon                  # 管理背景 daemon(別名:daemons)
codegraph telemetry [on|off]      # 顯示或改匿名使用統計
codegraph upgrade [version]       # 更新到最新(--check、--force)
codegraph version                 # 印出版本(-v、--version)
codegraph help [command]          # 顯示說明

codegraph affected

傳遞式追蹤 import 依賴,找出「改動的原始檔」影響哪些測試檔:

codegraph affected src/utils.ts src/api.ts         # 傳檔名
git diff --name-only | codegraph affected --stdin  # 從 git diff 管線輸入
codegraph affected src/auth.ts --filter "e2e/*"    # 自訂測試檔 pattern
Option說明預設
--stdin從 stdin 讀檔名清單false
-d, --depth <n>最大依賴遍歷深度5
-f, --filter <glob>自訂識別測試檔的 globauto-detect
-j, --json輸出 JSONfalse
-q, --quiet只輸出檔路徑false
CI / hook 範例
#!/usr/bin/env bash
AFFECTED=$(git diff --name-only HEAD | codegraph affected --stdin --quiet)
if [ -n "$AFFECTED" ]; then
  npx vitest run $AFFECTED
fi

本站實測:真實輸出

query

codegraph query ExtractionOrchestrator --limit 5
Search Results for "ExtractionOrchestrator":
class       ExtractionOrchestrator
  src/extraction/index.ts:1435
method      constructor
  src/extraction/index.ts:1446
  (rootDir: string, queries: QueryBuilder)
method      indexFiles
  src/extraction/index.ts:2114
  (filePaths: string[]): Promise<IndexResult>
method      indexFile
  src/extraction/index.ts:2161
  (relativePath: string): Promise<ExtractionResult>

callers

codegraph callers buildContext --limit 5
Callers of "buildContext" (3):
method      buildContext
  src/index.ts:1842
file        context-ranking.test.ts
  __tests__/context-ranking.test.ts:1
file        context.test.ts
  __tests__/context.test.ts:1

impact

codegraph impact GraphTraverser --depth 1
Impact of changing "GraphTraverser" — 39 affected symbols:
src/graph/traversal.ts
  class       GraphTraverser:34
  method      traverseBFS:48
  method      traverseDFS:154
  method      getCallers:261
  method      getCallersRecursive:270
  method      getCallees:319
  method      getTypeHierarchy:412
  method      getImpactRadius:520
  method      findPath:617
  ...(共 39 個受影響符號)

files

codegraph files --max-depth 1
Project Structure (570 files):
├── __tests__
├── .github
├── assets
├── codegraph-kernel
├── scripts
├── site
├── src
├── telemetry-dashboard
├── telemetry-worker
└── vitest.config.ts (typescript, 2 symbols)
看完這頁你應該能說出:init/uninit、index/sync、status 的差別;affected 的用途與 stdin/CI 用法;每個查詢類指令(query/callers/callees/impact)都有 --json。

🎓 教學解說

CodeGraph CLI 的設計哲學是「一個 binary,多種用途」。同一個 codegraph 執行檔同時是:

指令的分類邏輯:

類型指令用途
生命週期init / uninit初始化或移除專案的 CodeGraph 支援
索引管理index / sync / status全量建構、增量更新、查看統計
查詢分析query / explore / node / callers / callees / impact搜尋、探索、分析關係
CI/CD 整合affected找出受改動影響的測試檔

Worked Example:建立 CI/CD Pipeline

假設你想在 CI 中只跑受改動影響的測試:

CI 整合實作
# 步驟 1:建立 hook 腳本
cat > .git/hooks/pre-push << 'EOF'
#!/usr/bin/env bash
AFFECTED=$(git diff --name-only HEAD | codegraph affected --stdin --quiet)
if [ -n "$AFFECTED" ]; then
  echo "Running affected tests: $AFFECTED"
  npx vitest run $AFFECTED
fi
EOF
chmod +x .git/hooks/pre-push

# 步驟 2:測試 hook
git add .
git commit -m "test: add user service"
git push  # 自動只跑 affected tests

關鍵觀察:--stdin 讓你可以從任何來源(git diff、PR list、file watcher)輸入檔名清單,非常靈活。

常見錯誤與診斷

錯誤訊息原因解法
command not found: codegraph未全域安裝執行 npm i -g @colbymchenry/codegraph
No such file: .codegraph/專案未初始化執行 codegraph init
Lock unavailable另一 process 正在索引等待或執行 codegraph unlock
query returned 0 results名稱拼錯或未索引用 codegraph query --limit 20 搜尋相似名稱

練習:驗收清單


🔍 進階真實情境 Worked Example:On-call 快速定位

前面的範例是 CI/CD 整合。換一個完全不同場景:凌晨 on-call,production 結帳失敗。你只有一個 terminal,目標是在幾分鐘內從「現象」走到「根因」,且每一步都能留下可稽核的證據。

on-call 工作流
# ① 從錯誤訊息反向找:誰在呼叫 checkout?
codegraph callers checkout --json | jq '.callers[].symbol'

# ② 用 explore 一次拿「結帳鏈」的原始碼 + 呼叫路徑
codegraph explore "checkout 失敗的完整呼叫鏈"

# ③ 鎖定可疑函式,看它呼叫誰(callees)
codegraph callees validatePayment --limit 20

# ④ 評估「改這裡」的爆炸半徑
codegraph impact validatePayment --depth 2

# ⑤ 找受影響的測試,確認修復不會漏測
git diff --name-only | codegraph affected --stdin --quiet

為什麼走這條路徑:on-call 的時間成本極高,explore 一次把「相關符號原始碼 + 呼叫路徑 + blast radius」全部拿回,取代幾十次 grep+Read;--json 讓每一步都能接進 jq/shell 管線自動化;最後用 affected 把「我要改什麼」轉成「我要跑哪些測試」——這正是把圖譜當「可查詢的結構」而非「搜尋結果」來用。

🔬 深入原理擴充

內部架構:CLI 是薄殼。每個子指令的實作都極薄——把參數整理好 → 呼叫 CodeGraph class 對應方法 → 排版輸出。explore 與 node 的輸出與 MCP 工具逐字元一致:它們就是同一段組裝邏輯的 CLI 入口(非 MCP harness 的對等介面)。查詢類指令都帶 --json,這是與腳本世界接軌的正式契約。node-version-check.ts 在 Node <20 或 ≥25 時硬性退出——CLI/MCP 跑在 self-contained bundle runtime(不受影響),從 source 跑才需要 Node ≥22.5(node:sqlite)。

大家以為分析正確但其實有誤:以為 codegraph query 是「全文搜尋」(像 grep 一樣搜檔案內容)。實際上是 FTS5 名稱/符號搜尋——比對的是 symbol 的 name 與 qualified_name,不是檔案內文。想找「誰用了這個字串」要換工具;想找「這個符號在哪、誰碰它」才用 query/explore。另一個誤解:以為 uninstall 會刪掉 CLI,其實 --keep-cli 只移除 agent 設定。

🩺 診斷式疑難排解表

症狀可能原因解決方案
codegraph affected --stdin 沒輸出管線上游沒餵入檔名,或 depth 太小先 echo src/a.ts | codegraph affected --stdin 測管線;再加大 --depth
--json 輸出被 jq 解析失敗stderr 與 stdout 混雜,或引號沒包好加 2>/dev/null;用單引號包 filter
剛裝完卻 command not foundnpm global bin 不在 PATH檢查 npm prefix -g/bin 並加入 PATH
codegraph version 與預期不符多份安裝(global + local)打架which -a codegraph 找出全部,保留一份
node-version-check 硬退出Node <20 或 ≥25用 bundle runtime,或用 nvm 切到支援版本

🧩 進階挑戰題

  1. 寫一行 bash:把 git diff --name-only 的結果接給 codegraph affected --stdin --quiet,且只在有輸出時執行 vitest。說明為什麼 --quiet 是關鍵。
  2. 假設 codegraph callers foo --json 的輸出要送進 jq 統計 caller 數量。寫出 filter;若結果是 0,用哪個指令進一步診斷?
  3. 為什麼 explore 與 node 的輸出要和 MCP 工具「逐位元組一致」?列出至少兩類依賴這個保證的 consumer。

🏗️ 專案級端到端 Worked Example:CI/CD + on-call 的完整 CLI 工作流

將 CLI 整合進團隊日常:CI 用 affected 精準跑測試,on-call 用 explore 快速定位根因。以下是從設定到實際使用的端到端流程:

CI + on-call 整合
# ═══ Part A:CI/CD 整合 ═══

# ① 建立 pre-push hook,只跑受影響的測試
cat > .git/hooks/pre-push << 'EOF'
#!/usr/bin/env bash
AFFECTED=$(git diff --name-only HEAD | codegraph affected --stdin --quiet)
if [ -n "$AFFECTED" ]; then
  echo "Running affected tests: $AFFECTED"
  npx vitest run $AFFECTED
else
  echo "No affected tests — skipping"
fi
EOF
chmod +x .git/hooks/pre-push

# ② 建立 GitHub Actions workflow
cat > .github/workflows/affected-tests.yml << 'EOF'
name: Affected Tests
on: [pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci
      - run: npx @colbymchenry/codegraph init
      - run: |
          AFFECTED=$(git diff --name-only origin/main | codegraph affected --stdin --quiet)
          if [ -n "$AFFECTED" ]; then
            npx vitest run $AFFECTED
          fi
EOF

# ═══ Part B:On-call 快速定位 ═══

# ③ production 報錯,從錯誤訊息反向追蹤
codegraph callers checkout --json | jq '.callers[].symbol'

# ④ 一次拿完整呼叫路徑
codegraph explore "checkout 從 API entry → validatePayment → stripe charge 的完整鏈"

# ⑤ 鎖定可疑函式,看它呼叫誰
codegraph callees validatePayment --limit 20

# ⑥ 評估修復的爆炸半徑
codegraph impact validatePayment --depth 2

# ⑦ 確認修復不會漏測
git diff --name-only | codegraph affected --stdin --quiet

關鍵觀察:CLI 每個指令都是薄殼——參數整理好 → 呼叫 CodeGraph class → 排版輸出。explore 與 MCP 工具逐字元一致,CI 腳本與 agent session 拿到的是同一份答案。

⚡ 效能 / 品質 / 安全深度

面向考量實務建議
效能affected --stdin 的 depth 參數影響掃描範圍depth 太小會漏測、太大會掃太多;建議先用預設值 5,再依實際結果調整
品質--json 輸出是與腳本世界接軌的正式契約所有查詢指令都帶 --json,可接 jq/shell 管線自動化;避免 parse stderr
安全uninstall 行為與 --keep-cli 的差異codegraph uninstall 移除 agent 設定 + CLI;--keep-cli 只移除設定保留 CLI

📄 文件間比較對照表

面向本文(CLI 指令)相關文差異說明
explore 輸出CLI 的 codegraph explore 輸出MCP 工具兩者逐字元一致,但 MCP 工具多了 initialize response 指引
affected 指令CI/CD 整合的 affected --stdin自動同步affected 是被動查詢,auto-sync 是主動監聽
init/index/sync生命週期指令的操作手冊運作原理CLI 講怎麼用,運作原理講背後做什麼
--json 輸出所有查詢指令的結構化輸出Library 用法CLI --json vs Library 的 JS 物件返回值

✅ 進階互動式檢核清單

- [ ] 能建立一個 pre-push hook,自動只跑受影響的測試 - [ ] 能用 git diff | codegraph affected --stdin --quiet 管線找出所有受影響的測試 - [ ] 能用 --json + jq 做自動化的 caller 數量統計 - [ ] 能區分 init、index、sync 三個指令的適用時機 - [ ] 能用 codegraph explore 在 3 步內從錯誤訊息走到根因 - [ ] 能解釋 explore 與 MCP 工具「逐字元一致」的設計意義