cleanup

A command that checks a Lean source file against mathlib’s coding and proof-writing standards, then tidies and shortens its declarations. Lean is a programming language used to write computer-checked mathematical proofs.

In plain words
What is it for?
Use it to clean up a whole Lean file or one named declaration, including checking diagnostics, applying proof simplifications, and verifying that the result still compiles.
Why use it?
It finds style issues and opportunities to simplify proofs in a methodical pass, reducing the need to inspect every declaration manually.

Command

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/cbirkbeck/mathlib-quality/cleanup
Clone the repo
git clone --depth 1 https://github.com/CBirkbeck/mathlib-quality
Per session 21 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 24,292 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00021 $0.24292
Opus 5 $0.00010 $0.12146
Sonnet 5 $0.00004 $0.04858
Haiku 4.5 $0.00002 $0.02429

Measured 2d ago against content hash f5ae4484b812, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

cleanup scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

commands/cleanup.md · 1,848 lines

How it starts

The opening of the file, as written. The whole thing — 1,848 lines — stays where its author put it; the contents beside it link to each section on GitHub.

/cleanup — Style Audit + Cleanup + Golf

Run a full mathlib-quality pass on a Lean file. Absorbs what used to be /check-style: the audit phase first, then methodical fixes, then per-declaration deep golf.

This command is slow on purpose. Workers must take as long as they need. Nothing is skipped. Every rule is checked, every declaration is processed, every golfing technique is tried. "I'll come back to this" is forbidden — every audit item gets a concrete action or a documented "n/a: ".

Usage

/cleanup [file_path]                -- whole file (the standard mode)
/cleanup [file_path] [decl_name]    -- single declaration only

Mode A: Single Declaration

Skip the file-wide phases. Run Phase 4 (per-declaration deep cleanup) on just decl_name.

  1. Read the file, find the declaration.
  2. Run lean_diagnostic_messages and capture warnings on those lines.
  3. Dispatch the worker agent for that declaration (Phase 4 worker prompt below). The worker MUST read golfing-rules.md and proof-patterns.md in full as its first step.
  4. Verify compilation.
  5. Run the buzz pass on the declaration — Skill(skill="mathlib-quality:buzz", args="<file_path> <decl_name>") — the same hand-off as Phase 6.6 below, scoped to one decl. The declaration must come out under the elaboration budget or carry an explicit buzz deferral (→ /decompose-proof flag).
  6. Print before/after report (including the buzz-pass status block from 6.6b).

Mode B: Whole File (the standard mode)

Eleven phases. Do them in order. Do not skip phases.

PHASE 0   DOCTOR                  pre-flight: baseline must be green
PHASE 1   PREPARE                 collect context
PHASE 2   STYLE AUDIT             full punch-list, no fixes yet
PHASE 3   FILE-LEVEL FIXES        work the file-level items from the punch-list
PHASE 4   PER-DECLARATION GOLF    one worker per declaration, full audit + full golf + diff gates
PHASE 5a  NON-RENAME REFACTORING  mathlib replacements, junk-def inlining, big-change escalations
PHASE 5b  RENAME PASS             consume .mathlib-quality/renames.jsonl queue, apply sequentially
PHASE 6   FINAL VERIFICATION      file-level gates + cumulative checks
PHASE 6.5 SIMPLIFY                hand off to the built-in /simplify skill for a holistic review pass
PHASE 6.6 BUZZ                    hand off to /buzz for a performance pass — every decl under budget
PHASE 7   REPORT                  one consolidated report

Read the full file on GitHub · 1,848 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 1,848 lines · 21 tokens per session scan A f5ae4484b812

Subscribe to this mod's changes

cleanup is a command published in the GitHub repository CBirkbeck/mathlib-quality (32 stars, last pushed 14d ago), licensed MIT. It adds 21 tokens to every session and 24,292 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.