Giới thiệu
Open Code Review (OCR) là CLI tool review code dựa trên AI, xuất phát từ tool nội bộ chính thức của Alibaba. Trong hơn 2 năm vận hành nội bộ, nó phục vụ tens of thousands dev và bắt millions of defect — sau khi đủ battle-test, Alibaba spin out thành open source project.
Cơ chế cốt lõi: đọc git diff, gửi file thay đổi qua LLM (cấu hình endpoint tùy ý), và sinh ra structured review comment với line-level precision. Agent có thể đọc toàn file, search codebase, inspect các file thay đổi khác để có context — sinh review deep chứ không chỉ surface-level diff feedback.
Vấn đề của general-purpose agent
Nếu từng dùng Claude Code với Skill cho review, bạn sẽ quen ba pain point:
- Incomplete coverage: trên changeset lớn, agent “cut corner” — chỉ review một số file, bỏ qua file khác.
- Position drift: issue report ra không khớp vị trí code thực, line number hoặc file reference lệch.
- Unstable quality: skill driven bởi natural language khó debug, chất lượng review dao động mạnh theo prompt nhỏ.
Root cause: kiến trúc thuần language-driven không có hard constraint nào.
Kiến trúc hybrid: deterministic engineering × LLM agent
OCR design theo nguyên tắc: phần nào không được sai → deterministic engineering, phần nào cần dynamic → LLM.
Deterministic engineering (hard constraint):
- Precise file selection: xác định chính xác file nào cần review, file nào filter — không bỏ sót thay đổi quan trọng.
- Smart file bundling: group file liên quan thành một review unit (ví dụ:
message_en.propertiesvàmessage_zh.propertiesbundle chung). Mỗi bundle chạy như sub-agent với context isolate — divide-and-conquer giữ ổn định trên changeset rất lớn, native concurrent. - Fine-grained rule matching: match rule với đặc điểm từng file qua template engine → ổn định và predictable hơn rule guidance bằng prompt.
- External positioning + reflection module: module độc lập cho comment positioning và comment reflection → cải thiện cả độ chính xác vị trí lẫn nội dung.
Agent (dynamic decision):
- Scenario-tuned prompt: template optimize sâu cho code review, vừa tăng hiệu quả vừa giảm token consumption.
- Scenario-tuned toolset: distill từ phân tích tool-call trace trên production data — bao gồm call frequency distribution, per-tool repetition rate, impact của tool mới lên chain call. Kết quả: purpose-built toolset ổn định hơn generic agent toolkit.
Cách sử dụng
Cài đặt
# Khuyến nghị: NPM
npm install -g @alibaba-group/open-code-review
# Hoặc tải binary trực tiếp từ GitHub Release
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
Sau cài, lệnh ocr available global.
Cấu hình LLM
# Anthropic (Claude)
ocr config set llm.url https://api.anthropic.com/v1/messages
ocr config set llm.auth_token your-api-key-here
ocr config set llm.model claude-opus-4-6
ocr config set llm.use_anthropic true
# Hoặc env var (priority cao hơn)
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
Config lưu ở ~/.opencodereview/config.json. Tool cũng tương thích Claude Code env var (ANTHROPIC_BASE_URL, ANTHROPIC_AUTH_TOKEN, ANTHROPIC_MODEL) và đọc luôn từ ~/.zshrc / ~/.bashrc.
Chạy review
# Workspace mode — review staged + unstaged + untracked
ocr review
# Branch range — so 2 ref
ocr review --from main --to feature-branch
# Single commit
ocr review --commit abc123
Tích hợp với coding agent
Ba cách plug OCR vào Claude Code:
Cách 1 — Skill:
npx skills add alibaba/open-code-review --skill open-code-review
Dạy agent của bạn cách invoke ocr, classify issue theo priority, optionally apply fix.
Cách 2 — Claude Code plugin:
/plugin marketplace add alibaba/open-code-review
/plugin install open-code-review@open-code-review
Đăng ký slash command /open-code-review:review chạy OCR rồi tự filter và fix issue.
Cách 3 — Copy command file: download file .md vào .claude/commands/ (project-level, share qua git) hoặc ~/.claude/commands/ (user-level).
CI/CD
ocr review --from "origin/main" --to "origin/feature-branch" --format json
Flag --format json cho output machine-readable cho CI script. Repo có sẵn example cho GitHub Actions và GitLab CI.
Built-in ruleset
Khác với tool review chỉ dựa vào prompt template, OCR ship ruleset đã được fine-tune cho:
- NPE (NullPointerException) detection
- Thread-safety issue
- XSS vulnerability
- SQL injection
Khi review file, OCR match rule theo file type/path và inject vào context của LLM — sharper attention, less noise.