alibaba/open-code-review · 上手攻略
- 仓库:alibaba/open-code-review
- 链接:https://github.com/alibaba/open-code-review
- 分类:agent · engineering · llm-infra
- 作者:Jay
- 更新:2026-07-07
它是什么
Open Code Review 是阿里巴巴开源的 AI 代码审查 CLI 工具,起源于阿里内部使用了两年多的官方 AI 代码审查助手,已服务数万名开发者、发现数百万个代码缺陷。核心设计理念是「确定性工程 + LLM Agent」混合架构:对必须精准的步骤用工程逻辑保证正确性(文件选择、规则匹配、位置定位),将 Agent 的动态判断能力集中在需要灵活性的环节。
解决什么问题
通用 AI 编程助手(如 Claude Code)做代码审查时有三个典型痛点:
- 覆盖不完整 — 大型变更集下,Agent 倾向于「走捷径」,只审查部分文件
- 位置漂移 — 报告的问题行号或文件引用与实际代码位置不符
- 质量不稳定 — 自然语言驱动的 Skill 难以调试,审查质量随提示词波动大
Open Code Review 通过混合架构解决这些问题:在精确性要求高的地方用确定性工程(规则匹配、文件分块、位置模块),在需要灵活判断的地方交给 LLM Agent。基准测试显示(50 个热门开源库、200 个真实 PR、10 种编程语言,80+ 资深工程师交叉验证),与 Claude Code 相比,F1 和精确率显著更高,Token 消耗仅约 1/9,速度更快。
快速安装
方式一:NPM(推荐)
npm install -g @alibaba-group/open-code-review
# 安装后全局可用 ocr 命令
方式二:二进制脚本(macOS / Linux 单行)
curl -fsSL https://raw.githubusercontent.com/alibaba/open-code-review/main/install.sh | sh
# 自动下载对应平台最新版本,验证 SHA-256,安装到 /usr/local/bin
指定版本或目录:
OCR_INSTALL_DIR="$HOME/.local/bin" OCR_VERSION=v1.3.13 \
sh -c "$(curl -fsSL https://raw.githubusercontent.com/alibaba/open-code-review/main/install.sh)"
方式三:手动下载二进制
从 GitHub Releases 下载对应平台:
# macOS Apple Silicon
curl -Lo ocr https://github.com/alibaba/open-code-review/releases/latest/download/opencodereview-darwin-arm64
chmod +x ocr && sudo mv ocr /usr/local/bin/ocr
# Linux x86_64
curl -Lo ocr https://github.com/alibaba/open-code-review/releases/latest/download/opencodereview-linux-amd64
chmod +x ocr && sudo mv ocr /usr/local/bin/ocr
# Windows
curl -Lo ocr.exe https://github.com/alibaba/open-code-review/releases/latest/download/opencodereview-windows-amd64.exe
方式四:源码编译
git clone https://github.com/alibaba/open-code-review.git
cd open-code-review
make build
sudo cp dist/opencodereview /usr/local/bin/ocr
依赖要求:Git >= 2.41
核心配置与用法
第一步:配置 LLM
交互式配置(推荐)
ocr config provider # 选择内置或自定义 Provider
ocr config model # 为当前 Provider 选择模型
ocr llm test # 测试连通性
内置 Provider 支持 OpenAI、Anthropic 等常见模型,已有的环境变量(如 ANTHROPIC_API_KEY、OPENAI_API_KEY)会自动读取。
CLI 非交互配置(适合 CI/CD)
使用内置 Provider:
ocr config set provider anthropic
ocr config set providers.anthropic.api_key your-api-key-here
ocr config set providers.anthropic.model claude-sonnet-4-6
使用自定义 Provider(如私有网关):
ocr config set provider my-gateway
ocr config set custom_providers.my-gateway.url https://my-llm-gateway.internal/v1
ocr config set custom_providers.my-gateway.protocol openai
ocr config set custom_providers.my-gateway.api_key your-api-key-here
ocr config set custom_providers.my-gateway.model gpt-4o
环境变量(优先级最高,适合 CI 环境)
export OCR_LLM_URL=https://api.anthropic.com/v1/messages
export OCR_LLM_TOKEN=your-api-key-here
export OCR_LLM_MODEL=claude-opus-4-6
⚠️ 注意:版本号以 GitHub Releases 页面为准,本文撰写时最新版本为 v1.3.13(建议运行
ocr --version确认)。
核心审查命令
# 工作区模式:审查所有已 staged、unstaged 和未跟踪的变更
ocr review
# 分支对比:审查两个 ref 之间的差异
ocr review --from main --to feature-branch
# 单 commit 审查
ocr review --commit abc123
# 全文件扫描(无需 git 历史,适合审查不熟悉的代码库)
ocr scan
ocr scan --path internal/agent # 扫描指定目录
ocr scan --path src/util.js # 扫描指定文件
典型适用场景
场景一:本地开发时快速审查
cd your-project
git checkout -b feature/new-login
# 写了一些代码
ocr review # 一键审查当前所有变更
场景二:集成 CI/CD 流水线
在 GitHub Actions 中使用(参考 .github/workflows/openwiki-update.yml 的模式),配置 API key 后:
- name: Open Code Review
env:
OCR_LLM_TOKEN: ${{ secrets.OCR_API_KEY }}
OCR_LLM_MODEL: claude-sonnet-4-6
run: ocr review --from main --to ${{ github.head_ref }}
场景三:作为 AI 编程 Agent 的插件使用
Claude Code 插件
在 Claude Code 中:
/plugin marketplace add alibaba/open-code-review
/plugin install open-code-review@open-code-review
注册 /open-code-review:review 斜杠命令,自动过滤并修复高置信度问题。
Codex(本地)插件
codex plugin marketplace add alibaba/open-code-review
@Open Code Review review my current changes
@Open Code Review review and fix high-confidence issues
Cursor 插件
cursor-plugin marketplace add alibaba/open-code-review
坑与注意
- 必须先配置 LLM:
ocr review若未配置 Provider 会报错,首次使用需先ocr config provider+ocr config model - Git 版本要求:依赖 Git >= 2.41(旧版 Git 的某些 diff 功能不兼容)
- Token 消耗注意:虽然是 1/9 的 Claude Code 消耗,大型 PR 仍可能产生较多 Token,建议设置
OCR_LLM_TOKEN预算提醒 - 精确率 vs 召回率的取舍:官方明确说明召回率低于通用 Agent——这是设计取舍,追求精确率、减少噪音,对应阿里内部实际需求
- 自定义 Provider 需要协议字段:
protocol必填,支持anthropic和openai两种 - Windows 路径:Windows 用户二进制名为
ocr.exe,使用 PowerShell 或 cmd 均可,路径需在 PATH 中
与同类对比
| 工具 | 架构 | Token 效率 | 定位精度 | CI 集成 | 许可 |
|---|---|---|---|---|---|
| Open Code Review | 混合(工程+Agent) | ⭐⭐⭐⭐⭐ 极低 | ⭐⭐⭐⭐⭐ 行级 | ⭐⭐⭐⭐⭐ 原生 | Apache-2.0 |
| Claude Code + Review Skill | 纯 Agent | ⭐⭐ 中等 | ⭐⭐⭐ 一般 | ⭐⭐⭐ 需配置 | 商业 |
| GitHub Copilot review | 纯 Agent | ⭐⭐⭐ 中等 | ⭐⭐⭐ 一般 | ⭐⭐⭐⭐ 内置 | 商业 |
| Sider / other CR tools | 规则+LLM | ⭐⭐⭐ 中等 | ⭐⭐⭐ 一般 | ⭐⭐⭐ 中等 | 多 |
Open Code Review 最大优势在于「经过阿里超大规模实战检验」+ 精确的行级定位 + 极低 Token 消耗。
一句话结论
追求高质量、低噪音代码审查,且希望 Token 成本可控的团队,Open Code Review 是目前开源领域最值得一试的选项——尤其在使用 Claude Code / Codex / Cursor 做 AI 编程的工作流中,将其作为专业审查插件集成进来效果最好。