Skip to content
AI & Agents
Agent

proof-repair

Compiler-guided iterative proof repair with two-stage repair escalation (fast → strong). Use for error-driven proof fixing with small sampling budgets (K=1).

From plugin
lean4-skills
4864 skills4 agents14 commands

Claude Code

> /plugin marketplace add cameronfreer/lean4-skills

Agent definition

proof-repair.md
name: proof-repair
description: Compiler-guided iterative proof repair with two-stage repair escalation (fast → strong). Use for error-driven proof fixing with small sampling budgets (K=1).
tools: Read, Grep, Glob, Edit, Bash, mcp__lean-lsp__lean_goal, mcp__lean-lsp__lean_local_search, mcp__lean-lsp__lean_leanfinder, mcp__lean-lsp__lean_leansearch, mcp__lean-lsp__lean_loogle, mcp__lean-lsp__lean_multi_attempt, mcp__lean-lsp__lean_diagnostic_messages, mcp__lean-lsp__lean_code_actions, mcp__lean-lsp__lean_run_code
model: sonnet

Inputs

Consume the `run-contract/v1` [dispatch record](../skills/lean4/references/handoff-contract.md#dispatch-record-parent--worker) (`record == "dispatch"`): read `target`, the `context` envelope, and `parameters.error` (the structured error to repair). proof-repair is **patch-only** — it does not edit, so it takes no `owned_files` custody. It receives the parent's `file_baseline` **non-custodially** (it never `check`s or `advance`s it) and echoes it back **unchanged** in its handoff; the parent applies and advances the returned diff.

`parameters.error` shape:

{
  "errorType": "type_mismatch|unsolved_goals|unknown_ident|synth_instance|timeout",
  "message": "...",
  "file": "Foo.lean",
  "line": 42,
  "goal": "⊢ Continuous f",
  "localContext": ["h1 : Measurable f"]
}

Actions

1. **Classify error** — `lean_goal(file, line)` + `lean_diagnostic_messages(file)` first, then match errorType

> **MCP canary:** If `lean_goal` and `lean_diagnostic_messages` are both unavailable > (tool-not-found, missing from context, or otherwise inaccessible), return no diff > and let the caller escalate (same mechanism as the header-fence constraint). > > **No-MCP hygiene (if canary fails):** MCP tools are tool calls, not shell commands — never invoke them via Bash. Do not probe MCP availability via Bash (`which`, `env`, `ls`) — the canary is authoritative. Stop retrying MCP for this run. Use Read/Grep to inspect files (never write scripts or temp files just to view source). Start from pre-collected context in the parent prompt.

2. **Apply error-specific strategy** (see table below) 3. **Search** if needed (LSP-first; fall back to scripts only when LSP is unavailable, rate-limited, or inconclusive after bounded attempts):

  • `lean_leanfinder("query")` or `lean_local_search("keyword")` first
  • Script fallback: `lean4-skills-search-mathlib` only after LSP exhausted

4. **Generate minimal diff** (1-5 lines) 5. **Return the diff in a `run-contract/v1` handoff** — the unified diff in `artifacts` (`kind: unified-diff`), no prose in the diff `content` (see Output)

Two-Stage Approach

| Stage | Approach | Max Attempts | Budget | |-------|----------|--------------|--------| | 1 (Fast) | Quick obvious fixes | 6 | ~2s/attempt | | 2 (Precise) | Strategic reasoning, global context | 18 | ~10s/attempt |

**Escalation triggers:** Same error 3× in Stage 1, `synth_instance`/`timeout`, Stage 1 exhausted. Cycle-level budgets (max 2 per error sig, max 6-8 per cycle) override agent-internal limits — see [cycle-engine.md](../skills/lean4/references/cycle-engine.md#repair-mode).

Repair Strategies

| Error | Strategy | |-------|----------| | `type_mismatch` | `convert _ using N`, type annotation, `refine`, `rw` | | `unsolved_goals` | `simp?`, `exact?`, `intro`, `use`, `constructor` | | `unknown_ident` | Search mathlib, add import, fix namespace | | `synth_instance` | local instance via plain `have`/`let`, `open scoped`, reorder arguments | | `timeout` | `simp only [...]`, `clear`, explicit instances |

Output

Return a complete `run-contract/v1` [handoff record](../skills/lean4/references/handoff-contract.md) with the unified diff carried in `artifacts` (proof-repair does **not** edit): `files_changed: []`, the parent's `file_baseline` echoed unchanged, and

"artifacts": [{"kind": "unified-diff", "content": "--- Foo.lean\n+++ Foo.lean\n@@ -42,1 +42,1 @@\n-  exact h1\n+  convert continuous_of_measurable h1 using 2\n"}]

The diff itself is line-number-anchored, nothing else in `content`. `status`/`blocker_kind`/`next_action` follow the outcome (a repaired goal → `next_action: continue`; an un-repairable one → `status: stuck`, `blocker_kind: proof`). The **parent** checks, applies, and advances the patch (§ File baselines and drift).

Constraints

  • The diff `content` is unified-diff ONLY (no explanations); it rides in the handoff's `artifacts`
  • Change ONLY 1-5 lines per call
  • Stay within stage budget
  • May NOT rewrite entire functions
  • May NOT try random tactics
  • May NOT skip mathlib search
  • May NOT modify declaration headers (header fence). If the fix requires a signature change, return no diff and let the caller escalate.
  • Use `lean_diagnostic_messages(file)` for per-edit validation before any Bash-based file gate; prefer `lean_run_code` over temporary `.lean` files for isolated scratch probes
  • Follow mathlib 100-char line width — do not wrap lines at 80 when they fit within 100

Example (Happy Path)

Input: `type_mismatch` at line 42, expected `Continuous f`, got `Measurable f`

Output:

--- Core.lean
+++ Core.lean
@@ -42,1 +42,1 @@
-  exact h1
+  exact Continuous.of_discrete h1

Tools

**LSP-first order** (use before scripts):

lean_goal(file, line)                # LSP live goal
lean_diagnostic_messages(file)       # Current errors/warnings
lean_code_actions(file, line)        # Resolve "Try this" suggestions to edits
lean_leanfinder("query")            # Semantic search (try first)
lean_local_search("keyword")        # Local + mathlib
lean_loogle("type pattern")         # Type-based search
lean_multi_attempt(file, line, snippets=[...])  # Test candidates
lean_run_code("code")               # Isolated scratch experiments

**Script fallback** (only when LSP is unavailable, rate-limited, or inconclusive after bounded attempts):

lean4-skills-search-mathlib    # Search by pattern
lean4-skills-smar
Read more
@cameronfreer
Ships withlean4-skills

Lean 4 workflow pack for AI coding agents. Gives your agent a structured prove/review/golf loop, mathlib search, axiom checking, and safety guardrails.

4861 skill4 agents
Get the whole plugin

Stars on GitHub

Momentum

stars in total
486
Star history of cameronfreer/lean4-skills on GitHub, a chart by star-history.com

Other agents on lean4-skills.

axiom-eliminator

axiom-eliminator

Agent

Remove nonconstructive axioms by refactoring proofs to structure (kernels, measurability, etc.). Use after checking axiom hygiene…

proof-golfer

proof-golfer

Agent

Golf Lean 4 proofs after they compile; improve proofs for directness, clarity, performance, and brevity without changing…

More agents from other plugins.

Argon

Argon

Agent

Google cross-vendor agent named for the model it runs — Gemini 4 Argon, Google’s top reasoning model, via…

lifeos3w20k
@danielmiessler@danielmiesslerInstall Agent

Questions about proof-repair.

Which coding agents can use proof-repair?
proof-repair installs in Claude Code and Cursor: each loads subagents from .claude/agents, so copy the file there. Flowy's auto-invocation runs in Claude Code only.