# Code Review

> Pragmatic pre-merge code review for Python-first projects. Reviews only changed files, uses three severities (CRITICAL/WARNING/INFORMATIONAL), and requires confidence + evidence + mitigation checks before marking stop-ship issues. Use when user asks to review code, review a PR, check production risks, or says: code review, 代码审查, PR review, 帮我审查代码, 提交前检查. Not for readability-only refactors; use code-simplifier for that.

- Skill: `romeosy/code-review` (Agent Skill)
- Install (CLI): `npx skillmds@latest add romeosy/code-review`
- Raw SKILL.md: https://api.skillmd.com/api/skills/romeosy/code-review/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: RomeoSY (https://skillmd.com/u/romeosy)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/romeosy/code-review

---


# 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

1. Review only the intended scope (changed files or user-specified files).
2. No repository-wide scans by default.
3. No CRITICAL finding without concrete evidence and high confidence.
4. 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

```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

```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

1. 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 --cached`
     - `git diff --name-only`
   - Final scope is the union of all three lists.
2. If no remote:
   - Changed files from local work only (union):
     - `git diff --name-only --cached`
     - `git diff --name-only`
3. Deduplicate paths and drop empty lines before review.
4. 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.
5. 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:

1. In review scope (changed files or user-specified files)
2. Trigger path is concrete and plausible
3. Existing mitigations were checked and found insufficient
4. Impact is high (security, data integrity, availability, or major user-facing breakage)
5. 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 Exception` re-raises, wraps with context, or follows explicit fallback with monitoring.
- `subprocess.run` without 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):

```markdown
## 代码审查：[分支或范围]

### 汇总
- 阻断级（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`
- 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.

