基于 LLM 的可组合代码审查引擎,支持自动化 diff 分析与 PR 评审
DiffScope 是一款由 Rust 构建的高性能、可组合代码审查引擎,专为自动化 diff 分析设计。采用 Apache-2.0 许可证,支持跨平台(Linux、macOS、Windows)运行,同时支持 Docker、GitHub Actions 及 GitLab CI 部署。
核心能力#
模型无关设计:无缝对接 OpenAI(gpt-4o 等)、Anthropic Claude(claude-3.5-sonnet 等)、Ollama 及任何 OpenAI 兼容 API,支持 OpenAI Responses API,可完全本地/离线运行。
Git 深度集成:提供 diffscope git staged / uncommitted / branch 命令,直接针对未提交、暂存或分支变更进行审查。diffscope pr --post-comments 可自动在 GitHub PR 发表行内评论和汇总摘要,支持 @diffscope 交互式命令触发。
Smart Review 模式:输出包含置信度评分(0–100%)、修复工作量估算、自动标签分类、代码修复建议(diff 预览)、执行摘要及代码质量评分(0–10)。
符号索引:支持 Regex 和 LSP 两种索引模式,自动检测语言服务器,支持多跳依赖图展开(symbol_index_graph_hops)。
信号与上下文控制:提供 strictness 级别(1–3)、comment_types 过滤(logic/syntax/style/informational)、path-based custom_context 以及跨仓库 pattern_repositories 规则库。
辅助功能#
- Commit 消息建议:
diffscope git suggest生成遵循 Conventional Commits 规范的提交消息 - Changelog 生成:基于 git 历史自动生成 changelog / release notes
- Discuss 命令:
diffscope discuss支持对已生成评论进行线程式追问 - 多种输出格式:JSON、Markdown 报告、inline patch 注释
架构与扩展#
可组合架构与插件系统:模块化组件设计,内置 ESLint、Semgrep、duplicate_filter 插件,可扩展 pre-analyzer / post-processor。内置 Semgrep 插件支持 SQL 注入 / XSS / 认证授权问题检测,配合 rules_files 自定义安全规则。
自适应学习:根据 accepted/rejected 反馈历史自动抑制低价值重复评论。
评估框架:diffscope eval --fixtures 支持按 fixture 回归测试,输出 per-rule precision/recall/F1,可设定 CI 门控阈值防止模型输出退化。
可观测性:通过可选 otel feature 启用 OpenTelemetry 分布式追踪。
实现细节#
底层基于 Tokio 异步运行时,使用 similar 2.4 解析 diff,git2 0.20 处理 Git 操作,reqwest 0.12(rustls-tls)调用 LLM API。集成 axum 0.8 + tower-http 提供 Web UI 静态文件服务,sqlx 0.8 + PostgreSQL 实现数据持久化。CLI 基于 clap 4.4,配置使用 serde_yaml 解析 .diffscope.yml。Release profile 启用 LTO thin、codegen-units 1、strip true 以优化体积与性能。提供 Dockerfile、docker-compose.yml 及 Helm chart charts/diffscope 用于自托管部署。
快速上手#
# 预编译二进制(Linux/macOS)
curl -sSL https://raw.githubusercontent.com/evalops/diffscope/main/install.sh | sh
# Homebrew
brew tap evalops/diffscope && brew install diffscope
# Cargo
cargo install diffscope
# Docker
docker pull ghcr.io/evalops/diffscope:latest
# 最快验证
export OPENAI_API_KEY=your-key
git diff | diffscope review
关键配置 (.diffscope.yml)#
model: gpt-4o
temperature: 0.2
strictness: 2
comment_types: [logic, syntax, style, informational]
review_profile: balanced
symbol_index: true
feedback_path: ".diffscope.feedback.json"
custom_context:
- scope: "src/api/**"
notes: ["Auth flows must enforce tenant boundaries."]
files: ["docs/security/*.md"]
pattern_repositories:
- source: "../shared-review-patterns"
scope: "src/**"
include_patterns: ["rules/**/*.md"]
rules_files: [".diffscope-rules.yml", "rules/**/*.yml"]
max_active_rules: 30