Glean 拾遗
日刊 /2026-07-23 / 本地优先的代码知识图谱,为AI编程工具提供精准上下文

本地优先的代码知识图谱,为AI编程工具提供精准上下文

原文 github.com 收录 2026-07-23 06:00 阅读 36 min
AI 解读

code-review-graph 是一个本地优先的代码智能图项目,基于 Tree-sitter 解析代码结构,通过 MCP 协议为 AI 编程助手(如 Codex、Claude Code、Cursor 等)提供精确的上下文。它解决 AI 在代码审查时重复读取大量 Token 的问题:通过 blast-radius 分析,只输出受变更影响的文件,帮助助手聚焦关键代码。支持 30+ MCP 工具、增量更新(2 秒内重建图)、GitHub Action 风险评分 PR 审查,以及自定义语言扩展。基准测试显示,中位数 Token 减少约 82 倍。适合使用 AI 编程助手的开发者和维护大型仓库的团队。

原文 36 分钟
原文 github.com ↗
§ 1

code-review-graph is a local-first code intelligence graph designed for AI coding tools. It builds a structural map of your codebase using Tree-sitter, tracks changes incrementally, and provides precise context to AI assistants via MCP (Model Context Protocol). The goal is to drastically reduce token consumption during code review by eliminating the need to re-read the entire codebase.

code-review-graph 是一个本地优先的代码智能图,专为 AI 编码工具设计。它利用 Tree-sitter 解析代码库构建结构化的依赖图,增量跟踪变更,并通过 MCP(模型上下文协议)为 AI 助手提供精确的上下文。其目标是大幅减少代码审查期间的 token 消耗,避免重复读取整个代码库。

§ 2

AI coding tools often re-read large parts of your codebase during review tasks, wasting tokens and time. code-review-graph fixes that by building a graph of functions, classes, imports, and their relationships. When a file changes, it computes the blast radius—all affected callers, dependents, and tests—so the AI only reads the minimal set of relevant files. Benchmarks show a median ~82x per-question token reduction (range 38x–528x) across six real repositories.

AI 编码工具在审查任务中常常需要重复读取代码库的大片内容,浪费 token 和时间。code-review-graph 通过构建包含函数、类、导入及其关系的知识图谱来解决问题。当文件变更时,它会计算“爆炸半径”——所有受影响的调用者、依赖者和测试——让 AI 只读取最必要的最小文件集。基准测试显示,在六个真实仓库中,单次问题的 token 消耗中位数减少了约 82 倍(范围 38x–528x)。

§ 3

The tool parses your codebase with Tree-sitter into an AST, stored as a graph of nodes (functions, classes, imports) and edges (calls, inheritance, test coverage) in SQLite. Updates are incremental: only changed files are re-parsed via SHA-256 hash checks, completing in under 2 seconds even for 2,900-file projects. Blast-radius analysis traces every caller and dependent, producing a minimal review set. The system supports 40+ languages and Jupyter notebooks, with easy custom language addition via a languages.toml file.

该工具使用 Tree-sitter 将代码库解析为 AST,并以图结构存储在 SQLite 中,节点包括函数、类、导入,边包括调用、继承、测试覆盖。更新是增量的:仅对变更文件重新解析(通过 SHA-256 哈希检查),即使对于 2900 个文件的项目也能在 2 秒内完成。爆炸半径分析追踪所有调用者和依赖者,生成最小审查集。系统支持 40 多种语言和 Jupyter notebook,并通过 languages.toml 文件轻松添加自定义语言。

§ 4

code-review-graph provides 30+ MCP tools for AI assistants, including blast radius analysis, semantic search, execution flow tracing, risk-scored PR reviews, and architecture overviews. It auto-detects AI coding platforms (Codex, Claude Code, Cursor, Windsurf, Zed, Continue, GitHub Copilot, etc.) and writes the correct MCP configuration via code-review-graph install. A GitHub Action provides risk-scored PR comments in CI, with optional fail-on-risk merge gates.

code-review-graph 为 AI 助手提供了 30 多个 MCP 工具,包括爆炸半径分析、语义搜索、执行流追踪、风险评分的 PR 审查和架构概览。它自动检测 AI 编码平台(Codex、Claude Code、Cursor、Windsurf、Zed、Continue、GitHub Copilot 等),并通过 code-review-graph install 写入正确的 MCP 配置。GitHub Action 可在 CI 中提供带风险评分的 PR 评论,并可选地作为合并门禁。

§ 5

Install via pip: pip install code-review-graph. Then run code-review-graph install to auto-detect and configure all supported platforms. Build the graph with code-review-graph build. After restarting your editor, ask your AI assistant to 'Build the code review graph for this project'. The initial build takes ~10 seconds for a 500-file project. For incremental updates, use code-review-graph watch or enable hooks. To view token savings, run code-review-graph detect-changes --brief.

通过 pip 安装:pip install code-review-graph。然后运行 code-review-graph install 自动检测并配置所有支持的平台。使用 code-review-graph build 构建图谱。重启编辑器后,让 AI 助手“为此项目构建代码审查图”。对于 500 个文件的项目,初始构建约需 10 秒。增量更新可使用 code-review-graph watch 或启用 hooks。要查看 token 节省情况,运行 code-review-graph detect-changes --brief

§ 6

Best suited for medium to large codebases where AI review frequently revisits many files. Excellent for monorepos (27,700+ files excluded from context). Not for trivial single-file diffs where overhead may exceed naive reads. Known weaknesses: recall is graph-derived (circular upper bound, ~1.0 by design), precision lower (0.58 average) due to conservative flagging. Search quality (MRR 0.35) needs improvement. Flow detection strongest for Python/PHP, weaker for JavaScript/Go.

最适合中大型代码库,AI 审查需要频繁重读大量文件的场景。对单体仓库(monorepo)尤其出色(可从上下文中排除 27,700+ 文件)。不适用于简单的单文件差异,因为开销可能超过直接读取。已知不足:召回率基于图谱自循环(设计上约 1.0),精确度较低(平均 0.58,因为保守标记)。搜索质量(MRR 0.35)有待提升。流检测对 Python/PHP 最强,JavaScript/Go 较弱。

打开原文 ↗