Files
nexus/wiki/concepts/IncrementalGraphUpdate.md
2026-05-03 05:42:12 +08:00

53 lines
2.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "IncrementalGraphUpdate"
type: concept
tags: ["LSP", "code-intelligence", "incremental-update", "event-driven", "real-time"]
sources: ["lsp-index-engineer.md"]
last_updated: 2026-04-29
---
## Definition
增量图谱更新Incremental Graph Update是一种通过文件监听器和 Git hooks 实时监听代码变化,仅对受影响的部分图谱进行增量更新而非全量重建的策略,确保图谱始终与代码库保持同步,同时保持 < 100ms 端到端响应时间。
## Core Mechanism
### 触发来源
1. **文件监听器**File Watcher监听文件系统的 `create/modify/delete` 事件
2. **Git Hooks**`pre-commit` / `post-commit` / `post-merge` 钩子检测代码变更
### 增量更新流程
1. 检测文件变更(新增/修改/删除)
2. 定位受影响的符号节点和边
3. 对该文件运行 LSP `textDocument/didChange` 通知
4. 接收 LSP 的增量语义更新
5. 更新相关图谱节点和边
6. 通过 WebSocket 推送图谱差异Graph Diff到客户端
### Graph Diff 推送
```typescript
interface GraphDiff {
added: GraphNode[];
removed: GraphNode[];
modified: GraphNode[];
addedEdges: GraphEdge[];
removedEdges: GraphEdge[];
}
```
仅推送差异,不推送全量图谱,实现最小化网络开销。
### 一致性保证
- **原子性更新**:图谱更新是事务性的,要么全部成功要么全部回滚
- **乐观锁**:并发更新时使用版本号检测冲突
- **不变性约束验证**:更新前后验证图谱一致性不变量(符号唯一定义、边引用有效性等)
## Performance Characteristics
- 文件变更到 WebSocket 推送:< 50ms
- 单文件增量更新:< 20ms
- 100k+ 符号规模:仍保持增量更新性能
## Connections
- [[IncrementalGraphUpdate]] ← is_update_strategy_of ← [[GraphDaemon]]
- [[IncrementalGraphUpdate]] ← triggered_by ← [[FileWatcher]]
- [[IncrementalGraphUpdate]] ← streams_via ← [[WebSocket]]
- [[IncrementalGraphUpdate]] ← replaces ← [[LSIF]]LSIF 用于预计算Incremental 用于实时)