你的 agent(Claude Code、Cursor、Codex、opencode…)啟動 codegraph serve --mcp 時,三層機制讓索引與你的程式碼保持同步——並確保在「編輯完 → 下一次同步」的短暫窗口內,agent 不會拿到安靜的錯誤答案。
原生 FSEvents / inotify / ReadDirectoryChangesW watcher 捕捉每個原始檔的 create / modify / delete,在 debounce 窗口(預設 2000ms,可用 CODEGRAPH_WATCH_DEBOUNCE_MS 調,範圍 [100ms, 60s])後觸發 re-index。一陣編輯連發會被折疊成一次同步。
在短暫的 debounce 窗口內,codegraph_explore 回應裡若引用了尚未入索引的檔案,會前置一個 ⚠️ 橫幅,指名該檔案並叫 agent 直接 Read 它。沒被回應引用的 pending 檔則以小 footer 呈現。無論哪種,agent 都拿到明確訊號——已用 Claude Code 驗證:agent 會先說「Reading the file directly for the live content」再開檔。
MCP server(重新)連線時,codegraph 會在回答第一個問題前,先對 working tree 做一次快速 (size, mtime) + content-hash 對帳——所以「沒有 MCP server 在跑期間」的編輯(終端 git pull、別的 editor 的編輯、前一個 agent session 退出)都會在下次 session 的第一次工具呼叫時被吸收。
agent writes src/Widget.ts
→ watcher fires (<100ms)
→ debounce (default 2s)
→ sync; Widget.ts is in the index
→ next agent query sees it
任何時候可用 codegraph status(CLI)驗證;有 pending 會看到 ### Pending sync: 區段,列出檔案與編輯年齡。
很少。手動 sync 有意義的時機:watcher 被停用(sandbox 環境,或 CODEGRAPH_NO_DAEMON=1),或你在 agent session 外寫腳本對索引做 pre-flight sync。
$ codegraph sync
┌ Syncing CodeGraph
Scanning files...
Parsing code...
Resolving refs...
◆ Synced 3 changed files
● Added: 3 — 0 nodes in 169ms
└ Done
上級 repo 對 sync 的實測目標:中型 repo 單檔編輯 ~0.3s,27,000 檔 Swift compiler repo ~0.4s;「永遠不需要手動 re-run」的承諾來自於只同步變動部分。
自動同步的設計目標是「零維護」——你只管寫程式,圖譜會自動保持最新。三層機制分別處理不同場景:
# 步驟 1:啟動 MCP server(背景)
codegraph serve --mcp &
# 步驟 2:查看初始狀態
codegraph status
# Nodes: 12,733 | Edges: 44,903 | Last sync: 0s ago
# 步驟 3:修改一個檔案
echo "export function newHelper() {}" >> src/utils.ts
# 步驟 4:立即查看(debounce 期間)
codegraph status
# Pending sync: src/utils.ts (modified 0.5s ago)
# 步驟 5:等待 debounce 後再查
sleep 3
codegraph status
# Nodes: 12,734 | Edges: 44,910 | Last sync: 1s ago
關鍵觀察:修改後的 2 秒內,status 會顯示 pending;2 秒後自動同步完成。這就是 debounce 的作用——避免頻繁的檔案操作導致不斷重建索引。
| 症狀 | 原因 | 解法 |
|---|---|---|
| 新檔案未被索引 | debounce 期間或 watcher 未啟動 | 等待 2 秒或手動 codegraph sync |
| staleness 警告一直出現 | 檔案持續變動中 | 停止編輯,等待 sync 完成 |
| auto-sync 完全不動 | watcher 被停用或 sandbox 環境 | 設 CODEGRAPH_NO_DAEMON=0 或手動 sync |
| 同步後缺少某些符號 | 新檔案尚未被 resolver 處理 | 執行 codegraph sync --force 重跑解析 |
codegraph status 的 pending 狀態前面的範例是觀察單一檔案的自動同步。這裡是兩件工程實務:在 27,000 檔的 Swift compiler repo 上把 debounce 調到不會「每次儲存都重同步」,以及 sandbox CI 環境的 pre-flight sync。
# ① 大型 repo:把 debounce 調大,避免編輯連發觸發多次重同步
export CODEGRAPH_WATCH_DEBOUNCE_MS=8000 # 合法範圍 [100ms, 60s]
# ② 排除 build 產物,減少 watcher 雜訊
# (swift build 產物、.build/ 已是預設排除,確認無誤)
# ③ 驗證 pending 狀態
codegraph status
# ### Pending sync:
# .build/... (excluded) ← 確認排除生效
# Sources/Model.swift (modified 2s ago)
# ④ sandbox / CI(watcher 常不可用):pre-flight sync
codegraph sync # 在 agent 工作前先對帳一次
# ⑤ 大型 repo 單檔編輯的預期成本
# 中型 repo ~0.3s;27,000 檔 Swift repo ~0.4s
為什麼走這條路徑:debounce 是「新鮮度 vs 重同步成本」的旋鈕——repo 越大、編輯越密集,旋鈕越該調大;CI 沒有長駐 watcher,所以「手動 sync 一次」是成本最低的對帳。兩者都是「把三層機制用對」的實例:watcher 處理互動編輯、catch-up 處理離線編輯、pre-flight sync 處理沒有 watcher 的環境。
內部架構:三層各自防住的缺口。① watcher(FSEvents/inotify/RDCW)捕抓 create/modify/delete,debounce 折疊編輯連發;② staleness banner 誠實標出「debounce 窗口內被引用但未入索引」的檔案——agent 看到 ⚠️ 會先 Read 再作答(已用 Claude Code 驗證);③ connect-time catch-up 在 MCP server 重連時,用 (size, mtime) + content-hash 對帳,吸收「沒有 server 期間」的編輯(git pull、別的 editor、前一個 session)。sync 不是「重新掃描」:它只載變動檔的 unresolved refs、重試先前 failed 的 refs(#1240)、修復定義增減造成的 stale 邊(CG-33 rebind)、清掃被 kill 的解析 pass 留下的 orphan rows(#1187)。
| 症狀 | 可能原因 | 解決方案 |
|---|---|---|
| 新檔一直顯示 pending | watcher 未啟動(sandbox)或 debounce 過大 | 檢查 CODEGRAPH_NO_DAEMON;縮小 debounce 或手動 sync |
| sync 很快但圖還是錯 | failed-ref 重試沒成功 | 再 codegraph sync 一次,或 codegraph index --force |
| 編輯連發造成多次重同步 | debounce 太小(如 100ms) | 調高 CODEGRAPH_WATCH_DEBOUNCE_MS |
| 停機期間的編輯沒進圖 | connect-time catch-up 未觸發 | 重連後第一次呼叫應吸收;確認 daemon 有重啟 |
| 大型 repo 索引吃滿 CPU | build 產物沒排除、debounce 過小 | 排除 build 目錄 + 調大 debounce |
(size, mtime) + content-hash 對帳,為何不直接全量 re-index?content-hash 碰撞的風險如何被吸收?一個團隊從「每次改完手動 codegraph sync」遷移到「watcher 全自動 + CI pre-flight sync」的完整過程:
# ═══ Phase 1:現狀——手動 sync(容易忘記) ═══
codegraph sync # 每次改完都要記得跑
codegraph status # 看 Last sync 是否太舊
# ═══ Phase 2:啟動 watcher——全自動 ═══
# ① 啟動 MCP server(自動帶 watcher)
codegraph serve --mcp &
# ② 確認 watcher 在跑
codegraph status
# Daemon: running
# Watcher: FSEvents (active)
# ③ 測試:改一支檔案,等 2 秒
echo "// auto-sync test" >> src/utils.ts
sleep 3
codegraph status # Pending sync 已清空
# ═══ Phase 3:大型 repo 調校 ═══
# ④ 大型 repo 把 debounce 調大
export CODEGRAPH_WATCH_DEBOUNCE_MS=8000 # [100ms, 60s]
# ⑤ 確認 build 產物被排除
codegraph status | grep -i excluded
# ═══ Phase 4:CI 環境——pre-flight sync ═══
# ⑥ CI 沒有 watcher,手動 sync 一次
codegraph sync
# 確認 Last sync 是最新的
codegraph status
# ⑦ 用 connect-time catch-up 驗證
# 重啟 MCP server 後第一次呼叫會自動對帳
關鍵觀察:三層機制各防住不同缺口——watcher 防「正在編輯」、staleness banner 防「debounce 窗口」、catch-up 防「離線編輯」。debounce 是「新鮮度 vs 重同步成本」的旋鈕。
| 面向 | 考量 | 實務建議 |
|---|---|---|
| 效能 | debounce 過小導致頻繁重同步 | 大型 repo 調到 8s+;中型 repo 2-4s;編輯密集時調大 |
| 品質 | staleness banner 的誠實性 | agent 看到 ⚠️ 會先 Read 該檔再作答,避免拿到過期答案 |
| 安全 | watcher 的資源消耗 | FSEvents/inotify 有 kernel 層限制;sandbox 環境 watcher 可能被停用,需手動 sync |
| 面向 | 本文(自動同步) | 相關文 | 差異說明 |
|---|---|---|---|
| 三層機制 | watcher + banner + catch-up 的觸發時機 | 運作原理 | auto-sync 講觸發細節,運作原理講「四階段中的第四階段」 |
| debounce | 可調 [100ms, 60s] 的環境變數 | 設定 | auto-sync 講行為,設定頁列環境變數速查 |
| staleness | debounce 窗口內的誠實標記 | MCP 工具 | auto-sync 講 banner 機制,MCP 工具講 agent 如何處理 banner |
| CI sync | 手動 sync + connect-time catch-up | CLI 指令 | auto-sync 講 watcher 關閉時的 fallback,CLI 講 sync 指令用法 |
codegraph status 的 pending → 清空過程,驗證 debounce 行為
- [ ] 能根據 repo 規模選擇適當的 CODEGRAPH_WATCH_DEBOUNCE_MS 值
- [ ] 能在 CI 環境(無 watcher)正確執行 pre-flight sync
- [ ] 能解釋 connect-time catch-up 的 (size, mtime) + content-hash 對帳機制
- [ ] 能區分「自動同步」與「手動 sync」的適用時機