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] # 顯示說明
傳遞式追蹤 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> | 自訂識別測試檔的 glob | auto-detect |
-j, --json | 輸出 JSON | false |
-q, --quiet | 只輸出檔路徑 | false |
#!/usr/bin/env bash
AFFECTED=$(git diff --name-only HEAD | codegraph affected --stdin --quiet)
if [ -n "$AFFECTED" ]; then
npx vitest run $AFFECTED
fiSearch 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 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 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 個受影響符號)
Project Structure (570 files):
├── __tests__
├── .github
├── assets
├── codegraph-kernel
├── scripts
├── site
├── src
├── telemetry-dashboard
├── telemetry-worker
└── vitest.config.ts (typescript, 2 symbols)
CodeGraph CLI 的設計哲學是「一個 binary,多種用途」。同一個 codegraph 執行檔同時是:
指令的分類邏輯:
| 類型 | 指令 | 用途 |
|---|---|---|
| 生命週期 | init / uninit | 初始化或移除專案的 CodeGraph 支援 |
| 索引管理 | index / sync / status | 全量建構、增量更新、查看統計 |
| 查詢分析 | query / explore / node / callers / callees / impact | 搜尋、探索、分析關係 |
| CI/CD 整合 | affected | 找出受改動影響的測試檔 |
假設你想在 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 搜尋相似名稱 |
init、index、sync 的用途codegraph affected 找出受影響的測試檔--stdin 從 git diff 輸入檔名--json 取得結構化輸出前面的範例是 CI/CD 整合。換一個完全不同場景:凌晨 on-call,production 結帳失敗。你只有一個 terminal,目標是在幾分鐘內從「現象」走到「根因」,且每一步都能留下可稽核的證據。
# ① 從錯誤訊息反向找:誰在呼叫 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 found | npm global bin 不在 PATH | 檢查 npm prefix -g/bin 並加入 PATH |
codegraph version 與預期不符 | 多份安裝(global + local)打架 | which -a codegraph 找出全部,保留一份 |
node-version-check 硬退出 | Node <20 或 ≥25 | 用 bundle runtime,或用 nvm 切到支援版本 |
git diff --name-only 的結果接給 codegraph affected --stdin --quiet,且只在有輸出時執行 vitest。說明為什麼 --quiet 是關鍵。codegraph callers foo --json 的輸出要送進 jq 統計 caller 數量。寫出 filter;若結果是 0,用哪個指令進一步診斷?將 CLI 整合進團隊日常:CI 用 affected 精準跑測試,on-call 用 explore 快速定位根因。以下是從設定到實際使用的端到端流程:
# ═══ 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 物件返回值 |
git diff | codegraph affected --stdin --quiet 管線找出所有受影響的測試
- [ ] 能用 --json + jq 做自動化的 caller 數量統計
- [ ] 能區分 init、index、sync 三個指令的適用時機
- [ ] 能用 codegraph explore 在 3 步內從錯誤訊息走到根因
- [ ] 能解釋 explore 與 MCP 工具「逐字元一致」的設計意義