Embedded Code Review Expert
Severity Levels
| Level |
Name |
Description |
Action |
| P0 |
Critical |
Memory corruption, interrupt safety violation, security vulnerability, brick risk |
Must block merge |
| P1 |
High |
Race condition, resource leak, undefined behavior, RTOS misuse |
Should fix before merge |
| P2 |
Medium |
Code smell, portability issue, missing error handling, suboptimal pattern |
Fix or create follow-up |
| P3 |
Low |
Style, naming, documentation, minor suggestion |
Optional improvement |
Workflow
Mode Selection
Single-model mode (default for small diffs ≤100 lines):
- One review pass using the current session's model
- Faster, lower cost
- Suitable for trivial changes, config tweaks, documentation
Dual-model cross-review (default for diffs >100 lines, or when explicitly requested):
- Claude Code + Codex review independently via ACP
- Cross-compare findings
- Higher quality, catches heterogeneous blind spots
- Use for: new features, architecture changes, critical paths (ISR, crypto, NFC, DMA)
User can override: "用双模型 review" or "quick review 就行"
Phase 0: Preflight — Scope & Context
Run scripts/prepare-diff.sh <repo_path> [diff_range] — outputs:
- Repository info (branch, last commit)
- Target identification (MCU, RTOS, compiler from build files)
- Diff stat, line count, and size assessment (SMALL/MEDIUM/LARGE)
- Critical path detection (ISR/DMA, crypto, NFC, boot/OTA)
- Full diff content
Use script output to decide review mode:
- No changes: Inform user; offer to review staged changes or a commit range.
- SMALL (≤100 lines): Default to single-model review.
- LARGE (>500 lines): Summarize by file/module first, then review in batches by subsystem.
- Critical path detected: Always recommend dual-model.
Build review context package:
REVIEW_CONTEXT = {
repo_info: (branch, MCU, RTOS, compiler),
diff: (full git diff text),
references: (relevant checklist sections from references/),
focus_areas: (user-specified or auto-detected critical paths)
}
Phase 1: Single-Model Review
For small diffs or when dual-model is not requested:
1) Memory safety scan
- Load
references/memory-safety.md for detailed checklist.
- Stack overflow, buffer overrun, alignment, DMA cache coherence, heap fragmentation
- Flag
sprintf, strcpy, gets, strcat — suggest bounded alternatives
2) Interrupt & concurrency correctness
- Load
references/interrupt-safety.md for detailed checklist.
- Shared variable access, critical sections, ISR best practices, RTOS pitfalls
- Priority inversion, reentrancy, nested interrupt handling
3) Hardware interface review
- Load
references/hardware-interface.md for detailed checklist.
- Peripheral init ordering, register access, timing violations, pin conflicts
- Communication protocols: I2C/SPI/UART/NFC buffer management, timeout handling
4) C/C++ language pitfalls
- Load
references/c-pitfalls.md for detailed checklist.
- Undefined behavior, integer issues, compiler assumptions, linker issues
- Preprocessor hazards, portability, type safety
5) Architecture & maintainability
- HAL/BSP layering, abstraction, coupling, testability
- Dead code, magic numbers, configuration management
6) Security scan (embedded-specific)
- Secret storage, debug interfaces, firmware update integrity
- Side channels, fault injection, input validation, stack canaries
→ Skip to Phase 3: Output for single-model results.
Phase 2: Dual-Model Cross-Review (ACP)
When dual-model review is triggered:
Step 1: Prepare review payloads
Build two independent review tasks from the same REVIEW_CONTEXT:
Claude Code task:
You are a senior embedded systems engineer reviewing firmware code changes.
[REVIEW_CONTEXT: repo info, diff, focus areas]
Review checklist (apply all that are relevant):
- Memory safety (references/memory-safety.md)
- Interrupt & concurrency (references/interrupt-safety.md)
- Hardware interfaces (references/hardware-interface.md)
- C/C++ pitfalls (references/c-pitfalls.md)
- Architecture & security
Output format: For each finding, provide:
[P0/P1/P2/P3] [file:line] Title
- Description
- Risk
- Suggested fix
Be thorough. Flag everything you find, even if uncertain — mark uncertain items with [?].
Codex task:
You are an independent code reviewer for embedded/firmware projects.
Your job is to find bugs, security issues, and correctness problems.
[REVIEW_CONTEXT: repo info, diff, focus areas]
Focus on:
1. Memory corruption risks (buffer overflow, use-after-free, stack overflow)
2. Concurrency bugs (race conditions, missing volatile, ISR safety)
3. Hardware interface errors (timing, register access, peripheral init)
4. Logic errors and edge cases
5. Security vulnerabilities
Output: List every issue found as:
[SEVERITY: critical/high/medium/low] [file:line] Issue title
- What's wrong
- What could happen
- How to fix
Do NOT skip low-severity items. Report everything.
Step 2: Spawn parallel ACP sessions
sessions_spawn(runtime="acp", agentId="claude-code", task=claude_task)
sessions_spawn(runtime="acp", agentId="codex", task=codex_task)
Both run simultaneously. Wait for both to complete.
Step 3: Cross-compare findings
After both complete, analyze results:
- Consensus findings (both flagged same issue): HIGH CONFIDENCE — these are real bugs
- Claude-only findings: Review for validity — may be false positive or genuine catch
- Codex-only findings: Review for validity — heterogeneous perspective may catch Claude's blind spots
- Contradictions: Flag for human judgment — one says it's fine, other says it's a bug
Map to unified severity levels (P0-P3).
Phase 3: Output Format
## Embedded Code Review Summary
**Target**: [MCU/Board] | [RTOS/Bare-metal] | [Compiler]
**Branch**: [branch name]
**Files reviewed**: X files, Y lines changed
**Review mode**: [Single-model / Dual-model (Claude Code + Codex)]
**Overall assessment**: [APPROVE / REQUEST_CHANGES / COMMENT]
---
## Findings
### 🔴 P0 - Critical (must block)
(none or list)
### 🟠 P1 - High (fix before merge)
1. **[file:line]** Brief title [🤝 consensus / 🔵 Claude-only / 🟢 Codex-only]
- Description of issue
- Risk: what can go wrong
- Suggested fix
### 🟡 P2 - Medium (fix or follow-up)
...
### ⚪ P3 - Low (optional)
...
---
## Cross-Review Analysis (dual-model only)
| Metric | Count |
|--------|-------|
| 🤝 Consensus (both found) | X |
| 🔵 Claude-only | Y |
| 🟢 Codex-only | Z |
| ⚠️ Contradictions | W |
### Notable disagreements
(list any contradictions with both perspectives)
---
## Hardware/Timing Concerns
(register access, peripheral init, timing-sensitive code)
## Architecture Notes
(layering, testability, portability observations)
Phase 4: Next Steps
---
## Next Steps
Found X issues (P0: _, P1: _, P2: _, P3: _).
**How would you like to proceed?**
1. **Fix all** — implement all suggested fixes
2. **Fix P0/P1 only** — address critical and high priority
3. **Fix specific items** — tell me which issues to fix
4. **Re-review with dual-model** — run cross-review (if single-model was used)
5. **No changes** — review complete
Important: Do NOT implement changes until user explicitly confirms.
Resources
references/
| File |
Purpose |
memory-safety.md |
Buffer, stack, heap, DMA, alignment checklist |
interrupt-safety.md |
ISR, concurrency, RTOS, atomic operations checklist |
hardware-interface.md |
Peripherals, registers, timing, protocol checklist |
c-pitfalls.md |
UB, integer, compiler, preprocessor, portability checklist |
scripts/
| File |
Purpose |
prepare-diff.sh |
Extract git diff and build review context |
1---2name: embedded-review3description: Expert code review for embedded/firmware projects with dual-model cross-review (Claude + Codex via ACP). Detects memory safety, interrupt hazards, RTOS pitfalls, hardware interface bugs, and C/C++ anti-patterns. Trigger when user asks to review embedded/firmware/MCU code changes, diffs, or PRs. Examples: 'review firmware-pro2 的改动', 'review the NFC changes', '/embedded-review ~/path/to/repo', '/embedded-review ~/path/to/repo HEAD~5..HEAD', '/embedded-review <github-pr-url>'. Target environments: bare-metal MCU (STM32, nRF, ESP32, RP2040), RTOS (FreeRTOS/Zephyr/ThreadX), Linux embedded, mixed C/C++ firmware.4---56# Embedded Code Review Expert78## Severity Levels910| Level | Name | Description | Action |11|-------|------|-------------|--------|12| **P0** | Critical | Memory corruption, interrupt safety violation, security vulnerability, brick risk | Must block merge |13| **P1** | High | Race condition, resource leak, undefined behavior, RTOS misuse | Should fix before merge |14| **P2** | Medium | Code smell, portability issue, missing error handling, suboptimal pattern | Fix or create follow-up |15| **P3** | Low | Style, naming, documentation, minor suggestion | Optional improvement |1617---1819## Workflow2021### Mode Selection2223**Single-model mode** (default for small diffs ≤100 lines):24- One review pass using the current session's model25- Faster, lower cost26- Suitable for trivial changes, config tweaks, documentation2728**Dual-model cross-review** (default for diffs >100 lines, or when explicitly requested):29- Claude Code + Codex review independently via ACP30- Cross-compare findings31- Higher quality, catches heterogeneous blind spots32- Use for: new features, architecture changes, critical paths (ISR, crypto, NFC, DMA)3334User can override: "用双模型 review" or "quick review 就行"3536---3738### Phase 0: Preflight — Scope & Context39401. Run `scripts/prepare-diff.sh <repo_path> [diff_range]` — outputs:41 - Repository info (branch, last commit)42 - Target identification (MCU, RTOS, compiler from build files)43 - Diff stat, line count, and size assessment (SMALL/MEDIUM/LARGE)44 - Critical path detection (ISR/DMA, crypto, NFC, boot/OTA)45 - Full diff content46472. Use script output to decide review mode:48 - **No changes**: Inform user; offer to review staged changes or a commit range.49 - **SMALL (≤100 lines)**: Default to single-model review.50 - **LARGE (>500 lines)**: Summarize by file/module first, then review in batches by subsystem.51 - **Critical path detected**: Always recommend dual-model.52533. Build review context package:54 ```55 REVIEW_CONTEXT = {56 repo_info: (branch, MCU, RTOS, compiler),57 diff: (full git diff text),58 references: (relevant checklist sections from references/),59 focus_areas: (user-specified or auto-detected critical paths)60 }61 ```6263---6465### Phase 1: Single-Model Review6667For small diffs or when dual-model is not requested:6869#### 1) Memory safety scan70- Load `references/memory-safety.md` for detailed checklist.71- Stack overflow, buffer overrun, alignment, DMA cache coherence, heap fragmentation72- Flag `sprintf`, `strcpy`, `gets`, `strcat` — suggest bounded alternatives7374#### 2) Interrupt & concurrency correctness75- Load `references/interrupt-safety.md` for detailed checklist.76- Shared variable access, critical sections, ISR best practices, RTOS pitfalls77- Priority inversion, reentrancy, nested interrupt handling7879#### 3) Hardware interface review80- Load `references/hardware-interface.md` for detailed checklist.81- Peripheral init ordering, register access, timing violations, pin conflicts82- Communication protocols: I2C/SPI/UART/NFC buffer management, timeout handling8384#### 4) C/C++ language pitfalls85- Load `references/c-pitfalls.md` for detailed checklist.86- Undefined behavior, integer issues, compiler assumptions, linker issues87- Preprocessor hazards, portability, type safety8889#### 5) Architecture & maintainability90- HAL/BSP layering, abstraction, coupling, testability91- Dead code, magic numbers, configuration management9293#### 6) Security scan (embedded-specific)94- Secret storage, debug interfaces, firmware update integrity95- Side channels, fault injection, input validation, stack canaries9697→ Skip to **Phase 3: Output** for single-model results.9899---100101### Phase 2: Dual-Model Cross-Review (ACP)102103When dual-model review is triggered:104105#### Step 1: Prepare review payloads106107Build two independent review tasks from the same REVIEW_CONTEXT:108109**Claude Code task:**110```111You are a senior embedded systems engineer reviewing firmware code changes.112113[REVIEW_CONTEXT: repo info, diff, focus areas]114115Review checklist (apply all that are relevant):116- Memory safety (references/memory-safety.md)117- Interrupt & concurrency (references/interrupt-safety.md)118- Hardware interfaces (references/hardware-interface.md)119- C/C++ pitfalls (references/c-pitfalls.md)120- Architecture & security121122Output format: For each finding, provide:123[P0/P1/P2/P3] [file:line] Title124- Description125- Risk126- Suggested fix127128Be thorough. Flag everything you find, even if uncertain — mark uncertain items with [?].129```130131**Codex task:**132```133You are an independent code reviewer for embedded/firmware projects.134Your job is to find bugs, security issues, and correctness problems.135136[REVIEW_CONTEXT: repo info, diff, focus areas]137138Focus on:1391. Memory corruption risks (buffer overflow, use-after-free, stack overflow)1402. Concurrency bugs (race conditions, missing volatile, ISR safety)1413. Hardware interface errors (timing, register access, peripheral init)1424. Logic errors and edge cases1435. Security vulnerabilities144145Output: List every issue found as:146[SEVERITY: critical/high/medium/low] [file:line] Issue title147- What's wrong148- What could happen149- How to fix150151Do NOT skip low-severity items. Report everything.152```153154#### Step 2: Spawn parallel ACP sessions155156```157sessions_spawn(runtime="acp", agentId="claude-code", task=claude_task)158sessions_spawn(runtime="acp", agentId="codex", task=codex_task)159```160161Both run simultaneously. Wait for both to complete.162163#### Step 3: Cross-compare findings164165After both complete, analyze results:1661671. **Consensus findings** (both flagged same issue): HIGH CONFIDENCE — these are real bugs1682. **Claude-only findings**: Review for validity — may be false positive or genuine catch1693. **Codex-only findings**: Review for validity — heterogeneous perspective may catch Claude's blind spots1704. **Contradictions**: Flag for human judgment — one says it's fine, other says it's a bug171172Map to unified severity levels (P0-P3).173174---175176### Phase 3: Output Format177178```markdown179## Embedded Code Review Summary180181**Target**: [MCU/Board] | [RTOS/Bare-metal] | [Compiler]182**Branch**: [branch name]183**Files reviewed**: X files, Y lines changed184**Review mode**: [Single-model / Dual-model (Claude Code + Codex)]185**Overall assessment**: [APPROVE / REQUEST_CHANGES / COMMENT]186187---188189## Findings190191### 🔴 P0 - Critical (must block)192(none or list)193194### 🟠 P1 - High (fix before merge)1951. **[file:line]** Brief title [🤝 consensus / 🔵 Claude-only / 🟢 Codex-only]196 - Description of issue197 - Risk: what can go wrong198 - Suggested fix199200### 🟡 P2 - Medium (fix or follow-up)201...202203### ⚪ P3 - Low (optional)204...205206---207208## Cross-Review Analysis (dual-model only)209210| Metric | Count |211|--------|-------|212| 🤝 Consensus (both found) | X |213| 🔵 Claude-only | Y |214| 🟢 Codex-only | Z |215| ⚠️ Contradictions | W |216217### Notable disagreements218(list any contradictions with both perspectives)219220---221222## Hardware/Timing Concerns223(register access, peripheral init, timing-sensitive code)224225## Architecture Notes226(layering, testability, portability observations)227```228229### Phase 4: Next Steps230231```markdown232---233## Next Steps234235Found X issues (P0: _, P1: _, P2: _, P3: _).236237**How would you like to proceed?**2381. **Fix all** — implement all suggested fixes2392. **Fix P0/P1 only** — address critical and high priority2403. **Fix specific items** — tell me which issues to fix2414. **Re-review with dual-model** — run cross-review (if single-model was used)2425. **No changes** — review complete243```244245**Important**: Do NOT implement changes until user explicitly confirms.246247---248249## Resources250251### references/252253| File | Purpose |254|------|---------|255| `memory-safety.md` | Buffer, stack, heap, DMA, alignment checklist |256| `interrupt-safety.md` | ISR, concurrency, RTOS, atomic operations checklist |257| `hardware-interface.md` | Peripherals, registers, timing, protocol checklist |258| `c-pitfalls.md` | UB, integer, compiler, preprocessor, portability checklist |259260### scripts/261262| File | Purpose |263|------|---------|264| `prepare-diff.sh` | Extract git diff and build review context |