/lean4:reviewfrom lean4
Read-only code review of Lean proofs for quality, style, and optimization. Supports file, line, scope, and mathlib-review flags.
Ships in lean4. Installing the plugin adds it to Claude Code.
Read-only review of Lean proofs for quality, style, and optimization opportunities.
Non-destructive: Files are restored after analysis.
/lean4:review # Review changed files (default)
/lean4:review File.lean # Review specific file
/lean4:review File.lean --line=89 # Review single sorry
/lean4:review File.lean --line=89 --scope=deps # Review sorry + its dependencies
/lean4:review --scope=project # Review entire project (prompts)
/lean4:review --mathlib-review # Force the mathlib-review bar (Layer 2)
Interpret this command's inputs per the Command Invocation Contract.
Primary path (hook-validated): If a validated-invocation block for this
command appears in context, treat it as the authoritative interpretation of
parser-decidable inputs and do not re-parse the raw invocation text for
those inputs. Start by reading all parser-decided fields from the block. Emit
the final Resolved Inputs summary from the block values.
See Validated Invocation Block.
Fallback path (other hosts): If no validated-invocation block is present,
parse the raw invocation text against this command's input table before acting.
Startup requirements:
flag, repository_kind, contributing_upstream, the helper's
validated intent.source, or helper-failure; see
Mathlib Review Layer).--mathlib-review and --no-mathlib-review (--mathlib-review with
--no-mathlib-review → startup validation error).| Arg | Required | Description |
|---|---|---|
| target | No | File or directory to review |
| --scope | No | sorry, deps, file, changed, or project |
| --line | No | Line number for single-sorry scope |
| --codex | No | External review via Codex (interactive handoff) |
| --llm | No | Use llm CLI with model |
| --hook | No | Run custom analysis script |
| --json | No | Output structured JSON for external tools |
| --mode | No | batch (default) or stuck (triage) |
| --mathlib-review | No | Force the Layer-2 mathlib-review bar at full strictness, overriding project-context detection. Mutually exclusive with --no-mathlib-review. |
| --no-mathlib-review | No | Force Layer-2 findings to advisory, overriding project-context detection. Mutually exclusive with --mathlib-review. |
Scope levels:
| Scope | Description |
|---|---|
sorry | Single sorry at --line (requires target file + --line) |
deps | Sorry + same-file helpers and directly referenced lemmas (requires target file + --line) |
file | All sorries in target file |
changed | Files modified since last commit (git diff) |
project | Entire project (requires confirmation) |
Defaults:
--scope=changed--scope=file--line → --scope=sorrysorry or file)Note: Scope filtering is implemented by the reviewing agent, not the underlying scripts. The agent reads script output and filters results to match the requested scope.
Project-wide confirmation:
⚠️ This will review the entire project.
Proceed? (yes / no)
Output header always shows scope:
## Lean4 Review Report
**Scope:** Core.lean:89 (single sorry)
Batch mode (default):
Stuck mode:
Stuck mode output format:
## Stuck Review — Core.lean:89
**Primary blocker class:** missing library lemma
**Top 3 blockers:**
1. Missing lemma about tendsto_atTop → search Mathlib.Topology.Order
2. Typeclass instance missing for MeasurableSpace β → supply the intended structure (`have : MeasurableSpace β := borel β` only when `[TopologicalSpace β]` is available and Borel is intended; otherwise import the declaring module or report the missing prerequisite); `inferInstance` would re-run the failed search
3. Proof too long (38 lines) → extract helper lemma first
**Evidence:**
- searches — `lean_leansearch "tendsto atTop of monotone"`, `lean_loogle "Tendsto _ atTop"`
- returned lemmas — `tendsto_atTop_mono`, `Tendsto.comp`
- attempts — `exact tendsto_atTop_mono h` (type mismatch), `apply Tendsto.comp` (unification goal)
**Flag:** Statement may be false (optional — see below)
**Recommended next action:** Search for tendsto variants in Topology/Order
**Why first:** the top blocker is a missing lemma, so search dominates more tactic attempts
**next_action:** continue
The Primary blocker class value uses the Blocked-Goal Triage vocabulary from sorry-filling.md (definitional equality / missing intro-constructor-cases / missing rewrite / arithmetic / missing library lemma / typeclass-coercion-elaboration / needs helper lemma) and classifies the top blocker — the listed blockers may span classes. The Evidence block records the searches attempted, top candidate lemmas returned, and lean_multi_attempt outcomes required by the cycle-engine stuck-handoff contract. This block supplies the evidence and blocker vocabulary that the parent wraps into a run-contract/v1 handoff record — it is not itself a complete record (it carries no schema/record/status/blocker_signature/custody fields), and its human blocker phrases map to the record's kebab-case blocker_class enum. These fields are part of the human-readable stuck report only — they are not represented in the lean4-review-output/v2 interchange contract, which carries per-finding suggestions, not stuck-triage narrative.
next_action classification (stuck mode): continue (retryable), deep (needs escalation), repair (compiler blocker), redraft (statement-shape blocker), golf (sorry-free), stop (no path). Informational unless autoprove outer loop is active.
Falsification flag: Include when analysis suggests statement may be false:
decide or native_decideExample: **Flag:** Statement may be false (decidable goal failed decide)
Blocker priority (stuck mode):
decide, repeated proof failures)For strategy-level proof simplification (mathlib leverage, helper extraction, congr-lemma patterns), run /lean4:refactor or /lean4:refactor --dry-run.
The agent selects files based on scope, then runs these analyses (per file or directory):
lake build (project-wide); for scoped review (--scope=file), use lean_diagnostic_messages(file) + lake env lean <path/to/File.lean> (run from project root) firstlean4-skills-sorry-analyzer <target> --format=json --report-onlylean4-skills-check-axioms-inline <target> --report-onlymkAppM calls, short struct literals, single-expression tactic lines).lean4-skills-find-golfable <target> --filter-false-positivesStuck mode: Steps 5–6 are skipped; focus is on blockers (steps 1–4) for quick triage.
Layer 1 findings are always blockers-or-hygiene for the current project. The second-pass mathlib-review layer below runs on top of them.
A second pass surfaces what mathlib's own PR-review guide asks for, in four
first-class sections mapped onto the shipped
mathlib-review taxonomy
(#114) and the category values in
lean4-review-schema.json
(#115). This layer consumes those references; it does not restate their
vocabulary.
docstring, module-doc.file-placement, import-hygiene.api, generalization (settled rule rule_id: vacuous-api, severity: advisory).@[simp] justification, new-instance diamond risk, @[ext], @[reducible]. Categories: attribute, simp, instance.Layer 2 runs at full strictness (findings alongside Layer-1) only when the work is plausibly aimed at upstream mathlib; otherwise its findings are advisory. Resolve the mode in this order — conflict validation precedes precedence:
true participate; an explicit
--mathlib-review=false (or --no-mathlib-review=false) equals omission. If
both --mathlib-review and --no-mathlib-review resolve to true,
refuse to start (one startup validation error) — do not silently pick one.true flag wins — skip the helper. If --mathlib-review
resolves to true, use full strictness; if --no-mathlib-review resolves to
true, use advisory. Do not consult project-context in either case.lean4-skills-project-context --from "<target>" (the target file/dir if one
was given, else $PWD) and validate the record exactly as /lean4:checkpoint
does: schema = project-context/v1; facts.repository_kind a string in
mathlib | other-lean | not-lean | unknown; intent.contributing_upstream a
string in yes | no | unknown; intent.source a string in
env-override | invalid-env-override | remote-heuristic | default. A nonzero
exit, unparseable output, or any missing / non-string / out-of-domain field is
malformed helper output.| Condition (in order) | Layer-2 behavior | Source |
|---|---|---|
--no-mathlib-review true | advisory | flag |
--mathlib-review true | full strictness | flag |
facts.repository_kind == mathlib | full strictness | repository_kind |
intent.contributing_upstream == yes | full strictness | contributing_upstream |
any other resolved context (other-lean/not-lean/unknown, no/unknown intent) | advisory | intent.source |
| helper nonzero / unparseable / malformed record | advisory | helper-failure |
facts.repository_kind (what the repo is) and intent.contributing_upstream
(intent) are distinct signals; either independently activates full strictness,
and an explicit flag overrides both. The fail direction is toward advisory —
the opposite of the checkpoint gate's fail-closed — because a review consumer
must never impose mathlib blockers when the context is uncertain. Report the
resolved mode and its source in the Resolved Inputs block.
Advisory Layer-2 findings must be structurally separable from Layer-1
blockers. In human-readable output, render them under a distinct heading
(e.g. ### Advisory (mathlib-style)) or label them explicitly as advisory. In
lean4-review-output/v2, encode them with severity: advisory (the schema
carries severity, not a section heading). Intermixing advisory and blocking
findings in one undifferentiated list is insufficient.
Mode × layer:
batch × mathlib-targeted: all four Layer-2 sections emit as first-class findings alongside Layer-1.batch × non-mathlib: Layer-2 emits under a separate "Advisory (mathlib-style)" section.stuck × mathlib-targeted: bias toward blockers; allow one Layer-2 blocker when it is the main reason the patch won't survive mathlib review.stuck × non-mathlib: Layer-1 blockers only; Layer-2 stays advisory and may be omitted.## Lean4 Review Report
**Scope:** Core.lean:89 (single sorry)
### Build Status
✓ Project compiles
### Sorry Audit (N remaining)
| File | Line | Theorem | Suggestion |
|------|------|---------|------------|
| ... | ... | ... | ... |
### Axiom Status
✓ Standard axioms only
### Style Notes
- [file:line] - [suggestion]
### Golfing Opportunities
- [pattern] → [optimization]
### Recommendations
1. [action item]
Custom hooks receive structured JSON on stdin with file information, sorries, axioms, and build status. They return a complete lean4-review-output/v2 object containing a suggestions array.
Validate before merging. Every hook or Codex output/v2 object is validated
before its findings are incorporated — pipe it through
lean4-skills-validate-review-output (reads the object on stdin):
0 — valid; merge the findings.3 — validation failure (error_code schema-invalid or
semantic-invalid on stdout). Exclude these findings and report the
failure; never normalize or repair the output.2 — empty input or malformed JSON (treat as a hook that produced no
usable result); exit 4 — the shipped schema is unreadable (operational).The validator enforces the shipped schema plus the cross-field invariants JSON
Schema cannot express: total_suggestions == len(suggestions), the
by_severity histogram agrees with the suggestions, and a non-null error
implies empty suggestions.
See review-hook-schema.md for full input/output schemas, examples, and performance tips for rate-limited APIs.
When --codex is specified, display context for external review:
─────────────────────────────────────────────────────────
CODEX REVIEW — {scope description}
─────────────────────────────────────────────────────────
[Context based on scope:]
- sorry: ±50 lines around the target sorry
- deps: Target sorry + referenced helpers/lemmas
- file: Full file content
- changed: All modified files (git diff)
- project: Full project (requires confirmation)
If no sorries in scope:
- file: Include top-level definitions + relevant sections
- changed: Include diff + changed file list
To review in Codex CLI:
1. Run `codex` in project directory
2. Type `/review` → select "Review uncommitted changes"
3. Or paste the above context and ask for review
Return one complete `lean4-review-output/v2` object conforming to the shipped
[`lean4-review-schema.json`](../skills/lean4/references/lean4-review-schema.json):
every suggestion carries all eight fields (using `null` where unavailable), and
the root also includes `version`, `summary`, and `error`. See
[review-hook-schema.md](../skills/lean4/references/review-hook-schema.md) for a
worked example.
─────────────────────────────────────────────────────────
After review completes (internal or external), prompt:
## Review Complete
Would you like me to create an action plan from the review findings?
- [yes] — Enter plan mode with 3-6 step implementation plan
- [no] — End review, return to conversation
If "yes":
/lean4:prove/lean4:refactor/lean4:golfNote: When --mode=stuck is triggered by prove/autoprove, skip this prompt—the proving command handles the follow-up with its own "Apply this plan? [yes/no]" prompt.
--json report schema (lean4-review-report/v1)This legacy report format is distinct from the hook/Codex
lean4-review-output/v2 interchange contract above (lean4-review-schema.json):
it is the built-in command's own human-oriented --json dump, unchanged by
#115. When using --json, output follows this structure:
{
"version": "1.0",
"build_status": "passing" | "failing",
"sorries": [
{"file": "Core.lean", "line": 89, "theorem": "convergence_main", "goal": "..."}
],
"axioms": {
"standard": ["propext", "Classical.choice", "Quot.sound"],
"custom": []
},
"style_notes": [
{"file": "Core.lean", "line": 42, "message": "Consider using field syntax"}
],
"golfing_opportunities": [
{"file": "Core.lean", "line": 78, "pattern": "have chain", "suggestion": "Inline or extract"}
],
"summary": {
"total_sorries": 3,
"total_custom_axioms": 0,
"style_issues": 2,
"golf_opportunities": 5
}
}
Stuck mode only: The summary object includes "next_action": "continue" (or other value) when --mode=stuck. Absent in batch mode.
Note: Codex CLI's /review command is interactive-only. There's no codex review <sha> CLI command for automation. Two approaches are available:
codex in the project directory/review and select:
Tip: Use /diff after /review to see exact file changes.
codex exec)For CI or scripted reviews, use codex exec with a review prompt:
codex exec "Review this Lean 4 proof for correctness, focusing on:
1. Incomplete sorries and proof gaps
2. Type mismatches or missing instances
3. Non-standard axiom usage
$(cat Core.lean)
" --output-schema "$LEAN4_REFS/lean4-review-schema.json" -o review-output.json
The --output-schema argument points at the shipped, normative contract
lean4-review-schema.json
($LEAN4_REFS is the installed references directory). It is the single source
of truth for the category/severity enums — this command does not restate the
full schema. External Codex reviews emit fix: null and set rule_id only
for a specific rule (e.g. vacuous-api under api). A minimal conforming
suggestion:
{
"file": "Core.lean", "line": 89, "column": null,
"severity": "advisory", "category": "api", "rule_id": "vacuous-api",
"message": "Public API collapses to True; delete or replace.", "fix": null
}
See review-hook-schema.md for the field tables and the Codex SDK Cookbook for CI integration patterns.
Future autonomous external review: External review is currently manual-handoff only. Future versions may support autonomous external review via non-interactive CLI execution (e.g.,
codex exec) behind an explicit opt-in flag (--external-autonomous). Until then, unattended autoprove runs default to internal review.Requirements for autonomous external review:
- Stable JSON input/output contract
- Timeout + retry + cost budgets
- Safe fallback to internal review on external failure
- Explicit opt-in flag, not default behavior
#print axioms, then restoresTODO/HACK/FIXME), and may propose replacement wording in the report. It stays read-only: a docstring finding is a suggestion, never an in-place edit. See mathlib-style.md § Avoid Development History References for what to flag./lean4:prove - Guided cycle-by-cycle proving/lean4:autoprove - Autonomous multi-cycle proving/lean4:disprove - Guided counterexample search with certified refutation/lean4:golf - Apply golfing optimizationsWhat the command file sets; muted values are Claude Code's defaults
7-day visitors to each command page
npx claudepluginhub cameronfreer/lean4-skills --plugin lean412 commands in this plugin
/autoformalizeAutonomous end-to-end formalization from informal sources/autoproveAutonomous multi-cycle theorem proving with explicit stop budgets/checkpointSave progress with a safe commit checkpoint/diagnoseDiagnostics, cleanup, and migration help/disproveGuided counterexample search with certified refutation/draftDraft Lean declaration skeletons from informal claimsOther plugins' commands for the same job