Skip to content

Commit 7a25702

Browse files
igerberclaude
andcommitted
Enhance local AI review with full-file context, delta-diff re-review, and cost visibility
Add 9 improvements to /ai-review-local: full changed file contents (standard context), import-graph expansion (deep context), configurable --context levels, delta-diff re-review mode, sticky finding tracking via review-state.json, parameter propagation and semantic contract anti-patterns in pr_review.md, token cost visibility, --include-files for selective context, and --token-budget for controlling context size. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 04afed0 commit 7a25702

4 files changed

Lines changed: 1412 additions & 45 deletions

File tree

.claude/commands/ai-review-local.md

Lines changed: 111 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
description: Run AI code review locally using OpenAI API before opening a PR
3-
argument-hint: "[--full-registry] [--model <model>] [--dry-run]"
3+
argument-hint: "[--context minimal|standard|deep] [--include-files <files>] [--token-budget <n>] [--force-fresh] [--full-registry] [--model <model>] [--dry-run]"
44
---
55

66
# Local AI Code Review
@@ -12,6 +12,14 @@ pre-PR use. Designed for iterative review/revision cycles before submitting a PR
1212
## Arguments
1313

1414
`$ARGUMENTS` may contain optional flags:
15+
- `--context {minimal,standard,deep}`: Context depth (default: `standard`)
16+
- `minimal`: Diff only (original behavior)
17+
- `standard`: Diff + full contents of changed `diff_diff/` source files
18+
- `deep`: Standard + import-graph expansion (files imported by changed files)
19+
- `--include-files <file1,file2,...>`: Extra files to include as read-only context
20+
(filenames resolve under `diff_diff/`, or use paths relative to repo root)
21+
- `--token-budget <n>`: Max estimated input tokens before dropping context (default: 200000)
22+
- `--force-fresh`: Skip delta-diff mode, run a full fresh review even if previous state exists
1523
- `--full-registry`: Include the entire REGISTRY.md instead of selective sections
1624
- `--model <name>`: Override the OpenAI model (default: `gpt-5.4`)
1725
- `--dry-run`: Print the compiled prompt without calling the API
@@ -31,7 +39,8 @@ before any data is sent externally.
3139
### Step 1: Parse Arguments
3240

3341
Parse `$ARGUMENTS` for the optional flags listed above. All flags are optional —
34-
the default behavior (selective registry, gpt-5.4, live API call) requires no arguments.
42+
the default behavior (standard context, selective registry, gpt-5.4, live API call)
43+
requires no arguments.
3544

3645
### Step 2: Validate Prerequisites
3746

@@ -148,8 +157,38 @@ If user selects abort, clean up temp files and stop. If continue, proceed.
148157

149158
### Step 4: Handle Re-Review State
150159

151-
If `.claude/reviews/local-review-latest.md` exists, preserve it for re-review context:
160+
Check for existing review state and generate delta diff if applicable:
161+
152162
```bash
163+
# Check for review-state.json
164+
if [ -f .claude/reviews/review-state.json ]; then
165+
# Read last_reviewed_commit from the JSON file
166+
last_reviewed_commit=$(python3 -c "import json; print(json.load(open('.claude/reviews/review-state.json')).get('last_reviewed_commit', ''))")
167+
168+
if [ -n "$last_reviewed_commit" ] && git cat-file -t "$last_reviewed_commit" >/dev/null 2>&1; then
169+
# SHA is reachable
170+
if [ "--force-fresh" is NOT in the arguments ]; then
171+
# Generate delta diff (changes since last review)
172+
git diff --unified=5 "${last_reviewed_commit}...HEAD" > /tmp/ai-review-delta-diff.patch
173+
git diff --name-status "${last_reviewed_commit}...HEAD" > /tmp/ai-review-delta-files.txt
174+
175+
# Check if delta is empty
176+
if [ ! -s /tmp/ai-review-delta-diff.patch ]; then
177+
echo "No changes since last review (commit ${last_reviewed_commit:0:7}). Use --force-fresh to re-review."
178+
rm -f /tmp/ai-review-delta-diff.patch /tmp/ai-review-delta-files.txt
179+
# Clean up other temp files too
180+
rm -f /tmp/ai-review-diff.patch /tmp/ai-review-files.txt
181+
# Stop here
182+
fi
183+
fi
184+
else
185+
echo "Warning: Previous review state references unreachable commit (likely rebase). Running fresh review."
186+
# Delete stale state
187+
rm -f .claude/reviews/review-state.json
188+
fi
189+
fi
190+
191+
# Preserve previous review text (existing behavior, kept as fallback)
153192
if [ -f .claude/reviews/local-review-latest.md ]; then
154193
cp .claude/reviews/local-review-latest.md .claude/reviews/local-review-previous.md
155194
echo "Previous review preserved for re-review context."
@@ -158,8 +197,11 @@ fi
158197

159198
### Step 5: Run the Review Script
160199

161-
Build and run the command. Include `--previous-review` only if the previous review file
162-
exists from Step 4.
200+
Build and run the command. Include optional arguments only when their conditions are met:
201+
- `--previous-review`: only if `.claude/reviews/local-review-previous.md` exists
202+
- `--delta-diff` and `--delta-changed-files`: only if delta files were generated in Step 4
203+
- `--review-state` and `--commit-sha`: always include (enables finding tracking)
204+
- `--context`, `--include-files`, `--token-budget`: pass through from parsed arguments
163205

164206
```bash
165207
python3 .claude/scripts/openai_review.py \
@@ -169,14 +211,25 @@ python3 .claude/scripts/openai_review.py \
169211
--changed-files /tmp/ai-review-files.txt \
170212
--output .claude/reviews/local-review-latest.md \
171213
--branch-info "$branch_name" \
214+
--repo-root "$(pwd)" \
215+
--context "$context_level" \
216+
--review-state .claude/reviews/review-state.json \
217+
--commit-sha "$(git rev-parse HEAD)" \
172218
[--previous-review .claude/reviews/local-review-previous.md] \
219+
[--delta-diff /tmp/ai-review-delta-diff.patch] \
220+
[--delta-changed-files /tmp/ai-review-delta-files.txt] \
221+
[--include-files "$include_files"] \
222+
[--token-budget "$token_budget"] \
173223
[--full-registry] \
174224
[--model <model>] \
175225
[--dry-run]
176226
```
177227

178-
If `--dry-run`: display the prompt output and stop. Report the estimated token count
179-
and model that would be used.
228+
Note: `--force-fresh` is a skill-only flag — it controls whether delta diffs are
229+
generated in Step 4 and is NOT passed to the script.
230+
231+
If `--dry-run`: display the prompt output and stop. Report the estimated token count,
232+
cost estimate, and model that would be used.
180233

181234
If the script exits non-zero, display the error output and stop.
182235

@@ -191,6 +244,10 @@ Parse the review output to extract ALL findings. For each finding, capture:
191244
- Section (Methodology, Code Quality, etc.)
192245
- One-line summary of the issue
193246

247+
Note: The script handles writing `review-state.json` automatically (finding tracking
248+
across rounds). The skill does NOT need to write JSON — just pass `--review-state`
249+
and `--commit-sha` to the script.
250+
194251
Present a **findings summary** showing every finding, grouped by severity:
195252

196253
```
@@ -243,13 +300,21 @@ directly — for P0/P1 issues use `EnterPlanMode` for a structured approach; for
243300
issues, fix them directly since they are minor.
244301

245302
After fixes are committed, the user re-runs `/ai-review-local` for a follow-up review.
303+
On re-review, the script automatically activates delta-diff mode (comparing only
304+
changes since the last reviewed commit) and shows a structured findings table
305+
tracking which previous findings have been addressed.
246306

247307
### Step 8: Cleanup
248308

249309
```bash
250-
rm -f /tmp/ai-review-diff.patch /tmp/ai-review-files.txt
310+
rm -f /tmp/ai-review-diff.patch /tmp/ai-review-files.txt \
311+
/tmp/ai-review-delta-diff.patch /tmp/ai-review-delta-files.txt
251312
```
252313

314+
Note: `.claude/reviews/review-state.json` is preserved across review rounds (it
315+
tracks the last reviewed commit and findings). It is cleaned up when the user
316+
runs `--force-fresh` or when a rebase invalidates the tracked commit.
317+
253318
## Error Handling
254319

255320
| Scenario | Response |
@@ -260,28 +325,57 @@ rm -f /tmp/ai-review-diff.patch /tmp/ai-review-files.txt
260325
| Script exits non-zero | Display stderr output from script |
261326
| Previous review file missing on re-run | Script warns and continues as fresh review |
262327
| User aborts due to uncommitted changes | Clean exit |
328+
| No changes since last review (empty delta) | Report and stop, suggest `--force-fresh` |
329+
| Rebase invalidates last reviewed commit | Warn, delete stale state, run fresh review |
330+
| `review-state.json` schema mismatch | Script warns and starts fresh |
263331

264332
## Examples
265333

266334
```bash
267-
# Standard review of current branch vs main
335+
# Standard review of current branch vs main (default: full source file context)
268336
/ai-review-local
269337

270-
# Review with full methodology registry
271-
/ai-review-local --full-registry
338+
# Review with minimal context (diff only, original behavior)
339+
/ai-review-local --context minimal
340+
341+
# Review with deep context (changed files + imported files)
342+
/ai-review-local --context deep
343+
344+
# Include specific files as extra context
345+
/ai-review-local --include-files linalg.py,utils.py
272346

273347
# Preview the compiled prompt without calling the API
274348
/ai-review-local --dry-run
275349

276-
# Use a different model
277-
/ai-review-local --model gpt-4.1
350+
# Force a fresh review (ignore previous review state)
351+
/ai-review-local --force-fresh
352+
353+
# Use a different model with full registry
354+
/ai-review-local --model gpt-4.1 --full-registry
355+
356+
# Limit token budget for faster/cheaper reviews
357+
/ai-review-local --token-budget 100000
278358
```
279359

280360
## Notes
281361

282362
- This skill does NOT modify source files — it only generates temp files and
283363
review artifacts in `.claude/reviews/` (which is gitignored). It may also
284364
create a commit if there are uncommitted changes (Step 3).
365+
- **Context levels**: By default (`standard`), the full contents of changed
366+
`diff_diff/` source files are sent alongside the diff. This catches "sins of
367+
omission" — code that should have changed but wasn't (e.g., a wrapper missing
368+
a new parameter). Use `--context deep` to also include files imported by
369+
changed files as read-only reference.
370+
- **Delta-diff re-review**: When `review-state.json` exists from a previous run,
371+
the script automatically generates a delta diff (changes since the last reviewed
372+
commit) and focuses the reviewer on those changes. The full branch diff is
373+
included as reference context. Use `--force-fresh` to bypass this.
374+
- **Finding tracking**: The script writes structured findings to `review-state.json`
375+
after each review. On re-review, previous findings are shown in a table with
376+
their status (open/addressed), enabling the reviewer to focus on what changed.
377+
- **Cost visibility**: The script shows estimated cost before the API call and
378+
actual cost (from the API response) after completion.
285379
- Re-review mode activates automatically when a previous review exists in
286380
`.claude/reviews/local-review-latest.md`
287381
- The review criteria are adapted from `.github/codex/prompts/pr_review.md` (same
@@ -290,8 +384,9 @@ rm -f /tmp/ai-review-diff.patch /tmp/ai-review-files.txt
290384
- The CI review (Codex action with full repo access) remains the authoritative final
291385
check — local review is a fast first pass to catch most issues early
292386
- **Data transmission**: In non-dry-run mode, this skill transmits the unified diff,
293-
changed-file metadata, selected methodology registry text, and prior review context
294-
(if present) to OpenAI via the Chat Completions API. Use `--dry-run` to preview
295-
exactly what would be sent.
387+
changed-file metadata, full source file contents (in standard/deep mode),
388+
import-context files (in deep mode), selected methodology registry text, and
389+
prior review context (if present) to OpenAI via the Chat Completions API.
390+
Use `--dry-run` to preview exactly what would be sent.
296391
- This skill pairs naturally with the iterative workflow:
297392
`/ai-review-local` -> address findings -> `/ai-review-local` -> `/submit-pr`

0 commit comments

Comments
 (0)