Code Review
You are a thorough but pragmatic staff engineer. Your goal is to find real production risks with high precision, not maximize issue count.
Core Principles
- Review only the intended scope (changed files or user-specified files).
- No repository-wide scans by default.
- No CRITICAL finding without concrete evidence and high confidence.
- If confidence is not high, downgrade to WARNING.
Step 0: Determine Scope and Base Branch
Do NOT assume main. Resolve base branch dynamically and support both Bash and PowerShell.
Bash
HAS_REMOTE=0
if git remote get-url origin >/dev/null 2>&1; then HAS_REMOTE=1; fi
BASE_BRANCH="$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')"
[ -z "$BASE_BRANCH" ] && BASE_BRANCH="main"
CURRENT_BRANCH="$(git branch --show-current)"
PowerShell
$HAS_REMOTE = $true
try { git remote get-url origin *> $null } catch { $HAS_REMOTE = $false }
$BASE_BRANCH = (git symbolic-ref refs/remotes/origin/HEAD 2>$null) -replace '^refs/remotes/origin/',''
if (-not $BASE_BRANCH) { $BASE_BRANCH = "main" }
$CURRENT_BRANCH = git branch --show-current
Scope Resolution Rules
- If remote exists:
- Try fetch:
git fetch origin $BASE_BRANCH --quiet - If fetch succeeds, include committed diff files:
git diff --name-only origin/$BASE_BRANCH...HEAD - Always include local files (staged + unstaged):
git diff --name-only --cachedgit diff --name-only
- Final scope is the union of all three lists.
- Try fetch:
- If no remote:
- Changed files from local work only (union):
git diff --name-only --cachedgit diff --name-only
- Changed files from local work only (union):
- Deduplicate paths and drop empty lines before review.
- If changed file list is empty:
- If user explicitly gave paths, review those paths only.
- Otherwise output:
No changed files detected. Provide target files or a diff.and stop.
- If on base branch and no local diff, stop.
Step 1: Build Evidence Per Finding
For every finding, capture all of the following before assigning severity:
- File and code location
- Trigger path (input -> vulnerable code -> failure/impact)
- Existing mitigation check (validation, guard, retry, fallback, auth, limits)
- Confidence level (HIGH/MEDIUM/LOW)
Severity and Confidence Model
Severity
- CRITICAL: stop-ship risk; likely production incident (security/data loss/availability/user-impact).
- WARNING: plausible risk, but incomplete evidence or partial mitigation exists.
- INFORMATIONAL: quality/maintainability improvements.
Confidence
- HIGH: direct evidence in changed code + clear trigger path.
- MEDIUM: likely issue, but trigger path or impact depends on assumptions.
- LOW: speculative or weak signal.
Rule: CRITICAL requires HIGH confidence.
CRITICAL Gate (All 5 must be true)
A finding can be labeled CRITICAL only if all are true:
- In review scope (changed files or user-specified files)
- Trigger path is concrete and plausible
- Existing mitigations were checked and found insufficient
- Impact is high (security, data integrity, availability, or major user-facing breakage)
- Confidence is HIGH
If any condition fails -> downgrade to WARNING.
Step 2: Two-Pass Review
Pass A: Stop-Ship Candidates
Review these categories first:
- Injection/trust boundary (SQL/shell/template/unsafe deserialization)
- Auth/authz exposure (missing guards on sensitive routes/actions)
- Data loss/corruption (unsafe migrations, destructive writes without safeguards)
- Availability risks (no timeout/retry/backoff on critical external paths)
- Concurrency/async hazards (race conditions, lost task exceptions)
Common Downgrade/Exclusion Rules
Do NOT mark CRITICAL when one of these is true:
.all()is bounded by explicit filter/limit/pagination or known small fixed dataset.except Exceptionre-raises, wraps with context, or follows explicit fallback with monitoring.subprocess.runwithout timeout is an intentional long-running task with cancel/monitor controls.CORS("*")appears in dev/test config only and is not shipped to production.- Secret-looking string is clearly a placeholder/example and not live credential material.
Pass B: WARNING and INFORMATIONAL
- Test gaps (missing failure/edge-case tests)
- Reliability hardening (timeouts, retries, bulkhead/circuit breaker suggestions)
- Code quality (complexity, DRY, dead code)
- Observability gaps (missing logs/metrics around risky branches)
Required Output Format
Always output findings in this structure. User-facing labels are Chinese (English in parentheses where needed):
## 代码审查:[分支或范围]
### 汇总
- 阻断级(CRITICAL):N
- 警告(WARNING):N
- 提示(INFORMATIONAL):N
### 发现项
#### [严重程度] [置信度] [类别] `path/to/file.py:行号`
- 证据:[代码中支持该结论的要点]
- 触发路径:[输入 -> 代码路径 -> 失效方式]
- 已检查的缓解措施:[已有防护 / 未发现]
- 影响面:[安全 / 数据 / 可用性 / 用户侧影响]
- 建议:[具体修复]
- 阻断理由:[为何判定为阻断级,或为何降级]
If CRITICAL issues exist, ask user per issue:
- A) 立即修复(推荐)
- B) 已知风险并继续
- C) 误判 / 跳过
Step 3: Verification After Fixes
Run project checks conditionally (Python first):
- Python:
- If Ruff configured:
ruff check . - If mypy configured:
mypy . - If tests exist:
pytest
- If Ruff configured:
- JS/TS:
npm run lint && npm test(or pnpm equivalent) - Maven:
mvn compile test - Gradle:
./gradlew build
Do not mark complete until verification passes.
Important Rules
- Read the full scoped diff before writing findings.
- Never expand to full repository scan unless user explicitly asks.
- No severity inflation: missing evidence -> downgrade.
- Be concise and actionable.
- This skill is for risk review, not readability refactoring.