自動同步機制

watcher + debounce、staleness banner、連線時 catch-up——agent 永遠不會拿到過期的答案
FSEvents / inotify / ReadDirectoryChangesW · 預設 debounce 2000ms

為什麼需要三層

你的 agent(Claude Code、Cursor、Codex、opencode…)啟動 codegraph serve --mcp 時,三層機制讓索引與你的程式碼保持同步——並確保在「編輯完 → 下一次同步」的短暫窗口內,agent 不會拿到安靜的錯誤答案。

① File watcher with debounced auto-sync

原生 FSEvents / inotify / ReadDirectoryChangesW watcher 捕捉每個原始檔的 create / modify / delete,在 debounce 窗口(預設 2000ms,可用 CODEGRAPH_WATCH_DEBOUNCE_MS 調,範圍 [100ms, 60s])後觸發 re-index。一陣編輯連發會被折疊成一次同步。

② Per-file staleness banner

在短暫的 debounce 窗口內,codegraph_explore 回應裡若引用了尚未入索引的檔案,會前置一個 ⚠️ 橫幅,指名該檔案並叫 agent 直接 Read 它。沒被回應引用的 pending 檔則以小 footer 呈現。無論哪種,agent 都拿到明確訊號——已用 Claude Code 驗證:agent 會先說「Reading the file directly for the live content」再開檔。

③ Connect-time catch-up

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: 區段,列出檔案與編輯年齡。

什麼時候需要手動 codegraph sync

很少。手動 sync 有意義的時機:watcher 被停用(sandbox 環境,或 CODEGRAPH_NO_DAEMON=1),或你在 agent session 外寫腳本對索引做 pre-flight sync。

本站實測

codegraph 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」的承諾來自於只同步變動部分。

看完這頁你應該能說出:三層機制各自防住什麼、debounce 預設值與可調環境變數、staleness banner 為什麼對 agent 誠實很重要、以及手動 sync 的兩個適用時機。

🎓 教學解說

自動同步的設計目標是「零維護」——你只管寫程式,圖譜會自動保持最新。三層機制分別處理不同場景:

Worked Example:觀察自動同步行為

同步實測
# 步驟 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 重跑解析

練習:驗收清單


🔍 進階真實情境 Worked Example:大型 repo 的 debounce 調校 + CI pre-flight sync

前面的範例是觀察單一檔案的自動同步。這裡是兩件工程實務:在 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)。

大家以為分析正確但其實有誤:以為 debounce 是固定 2 秒。實際是可調 [100ms, 60s],且調完立刻生效。另一個誤解:以為「auto-sync 就保證圖永遠新鮮」。在 debounce 窗口內圖就是舊的——所以第三層(staleness banner)才存在,它的存在正是「圖可能舊」的誠實承認。

🩺 診斷式疑難排解表

症狀可能原因解決方案
新檔一直顯示 pendingwatcher 未啟動(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 索引吃滿 CPUbuild 產物沒排除、debounce 過小排除 build 目錄 + 調大 debounce

🧩 進階挑戰題

  1. debounce 的 trade-off:太大 → agent 拿舊答案;太小 → 每次儲存都重同步。依 repo 規模與編輯頻率,寫出你的選擇規則。
  2. catch-up 用 (size, mtime) + content-hash 對帳,為何不直接全量 re-index?content-hash 碰撞的風險如何被吸收?
  3. 畫一張「編輯 → debounce 窗口 → sync → agent 查詢」的時序圖,標出 staleness banner 會被觸發的窗口,並說明 agent 此時該怎麼應對。

🏗️ 專案級端到端 Worked Example:從手動 sync 到全自動同步的遷移

一個團隊從「每次改完手動 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 講行為,設定頁列環境變數速查
stalenessdebounce 窗口內的誠實標記MCP 工具auto-sync 講 banner 機制,MCP 工具講 agent 如何處理 banner
CI sync手動 sync + connect-time catch-upCLI 指令auto-sync 講 watcher 關閉時的 fallback,CLI 講 sync 指令用法

✅ 進階互動式檢核清單

- [ ] 能解釋三層機制各自防住什麼缺口(debounce 窗口、離線編輯、sandbox 環境) - [ ] 能觀察 codegraph status 的 pending → 清空過程,驗證 debounce 行為 - [ ] 能根據 repo 規模選擇適當的 CODEGRAPH_WATCH_DEBOUNCE_MS 值 - [ ] 能在 CI 環境(無 watcher)正確執行 pre-flight sync - [ ] 能解釋 connect-time catch-up 的 (size, mtime) + content-hash 對帳機制 - [ ] 能區分「自動同步」與「手動 sync」的適用時機