sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / harness / skills / lean-verify / SKILL.md
| 1 | --- |
| 2 | name: lean-verify |
| 3 | description: Machine-checked proofs with Lean 4 + Mathlib through the `lean` MCP server on the Spark. Use to prove a safety invariant (core disjointness, disk floor, lease exclusion, queue bounds, protocol/state-machine properties) about an explicit model, and to check that a proof has no sorry and no extra axioms. |
| 4 | --- |
| 5 | |
| 6 | # Lean 4 verification (`lean` MCP) |
| 7 | |
| 8 | Lean is a verification oracle, the same class as the pinned compiler or `fsck.vfat`: it checks a claim, it does not run in the product path. |
| 9 | |
| 10 | ## Pins (checked 2026-11-06) |
| 11 | |
| 12 | | Component | Version | Where | |
| 13 | |---|---|---| |
| 14 | | elan | 4.2.4 (227caca13 2026-08-25) | Spark `~/.elan/bin` | |
| 15 | | Lean toolchain | `leanprover/lean4:v4.35.0` | `~/workspace/lean-verify/lean-toolchain` | |
| 16 | | Mathlib | `v4.35.0` (commit `56a005855ea17e0b0958f73ec801ae140b0a1679`), prebuilt oleans from `lake exe cache get` | `~/workspace/lean-verify/lake-manifest.json` | |
| 17 | | MCP server | `lean-lsp-mcp==0.33.0` (MIT, github.com/oOo0oOo/lean-lsp-mcp tag `v0.33.0` = `ed8aa4b9`), `leanclient==0.13.2`, `mcp==2.2.0` | venv `~/workspace/lean-verify/.mcp-venv`, full list in `.mcp-venv-freeze.txt` | |
| 18 | |
| 19 | Project: Spark `~/workspace/lean-verify` (lake, library `LeanVerify`, local git history). Worked examples: `LeanVerify/Check.lean`, `LeanVerify/CoreAssign.lean`, `LeanVerify/SkillExamples.lean`; sorry negative control: `Controls/SorryControl.lean` (deliberately not in the library). |
| 20 | |
| 21 | The server runs on the Spark over ssh stdio, pinned to cores 0–1 with `nice -n 10`. The four remote search tools (`lean_leansearch`, `lean_loogle`, `lean_leanfinder`, `lean_hammer_premise`) are disabled with `LEAN_MCP_DISABLED_TOOLS`, so `tools/list` returns 18 tools, all local. The full smoke test (below) passed inside `bwrap --unshare-net`, where `curl https://pypi.org` fails, so it needs no network. Search Mathlib with `lean_local_search` (ripgrep) instead. |
| 22 | |
| 23 | `mcp.json` entry (Mac `~/.omp/agent/mcp.json`). `<spark>` is your ssh alias for the Spark and `<home>` is the absolute home directory of the remote account. ssh does not expand `~` inside `env` arguments, so write the absolute path: |
| 24 | |
| 25 | ```json |
| 26 | "lean": { |
| 27 | "type": "stdio", |
| 28 | "command": "ssh", |
| 29 | "args": ["-o", "BatchMode=yes", "-o", "ConnectTimeout=8", "<spark>", "env", "PATH=<home>/.elan/bin:/usr/local/bin:/usr/bin:/bin", "LEAN_PROJECT_PATH=<home>/workspace/lean-verify", "LEAN_LOG_LEVEL=ERROR", "LEAN_MCP_DISABLED_TOOLS=lean_leansearch,lean_loogle,lean_leanfinder,lean_hammer_premise", "taskset", "-c", "0,1", "nice", "-n", "10", "<home>/workspace/lean-verify/.mcp-venv/bin/lean-lsp-mcp", "--transport", "stdio"], |
| 30 | "timeout": 300000 |
| 31 | } |
| 32 | ``` |
| 33 | |
| 34 | ## Lean or Wolfram? |
| 35 | |
| 36 | | Question | Tool | |
| 37 | |---|---| |
| 38 | | Compute a number, solve, simplify, integrate, convert units, check an identity numerically or symbolically | Wolfram (mandatory, `wolfram-math`) | |
| 39 | | "This property holds for **every** state / input / interleaving of the model" | Lean | |
| 40 | | "These finite sets never overlap", "this arithmetic bound always holds", "two holders can never both own the lease", "the queue never exceeds N" | Lean | |
| 41 | | A proof someone else must be able to re-check mechanically, with an explicit axiom list | Lean | |
| 42 | |
| 43 | Wolfram answers *what is the value*; Lean answers *is this statement true for all cases, with a kernel-checked proof*. Use Wolfram to get a constant, then Lean to prove the invariant that uses it. |
| 44 | |
| 45 | ## Tools |
| 46 | |
| 47 | Enabled on the `lean` server (names as `tools/list` returns them; in omp they follow the `mcp__<server>_<tool>` pattern of the other servers, e.g. `mcp__lean_lean_run_code`): |
| 48 | |
| 49 | - `lean_run_code` `{code}` — check a self-contained snippet (it needs its own `import`s). The usual entry point. Returns `{success, timed_out, diagnostics[]}`. |
| 50 | - `lean_verify` `{file_path, theorem_name}` — axiom check plus source scan for one theorem in a project file. `file_path` is absolute (`<home>/workspace/lean-verify/...`). Returns `{axioms, trust, non_standard_axioms, warnings}` with `trust` one of `standard`, `incomplete` (sorry), `native`, `custom`. |
| 51 | - `lean_diagnostic_messages` — errors/warnings/infos of a project file. |
| 52 | - `lean_goal`, `lean_term_goal` — proof state at a position. |
| 53 | - `lean_multi_attempt` — try several tactics at one position. |
| 54 | - `lean_hover_info`, `lean_completions`, `lean_declaration_file`, `lean_references`, `lean_file_outline`, `lean_local_search`, `lean_code_actions`, `lean_profile_proof`, `lean_minimal_hypotheses`, `lean_get_widgets`, `lean_get_widget_source`, `lean_build`. |
| 55 | |
| 56 | The first call starts `lake serve` and loads Mathlib oleans (about 3–8 s measured). Prefer `import Mathlib.<specific module>` over `import Mathlib` to keep it fast. |
| 57 | |
| 58 | Observed results (2026-11-06 smoke test): |
| 59 | |
| 60 | | Snippet | `lean_run_code` | Meaning | |
| 61 | |---|---|---| |
| 62 | | `theorem t : (2:Nat)+2=4 := rfl` + `#print axioms t` | `success: true`, info `'t' does not depend on any axioms` | proved | |
| 63 | | `theorem f : (2:Nat)+2=5 := rfl` | `success: false`, error `Not a definitional equality` | refuted | |
| 64 | | `theorem s : (2:Nat)+2=4 := sorry` | **`success: true`**, warning category `sorry`, `'s' depends on axioms: [sorryAx]` | NOT proved | |
| 65 | |
| 66 | `success: true` therefore does **not** mean proved. A theorem is proved only if `success` is true, no diagnostic has category `sorry`, and its axioms are within the standard three. `lean_verify` on a sorry theorem returned `trust: "incomplete"`, `axioms: ["sorryAx"]`. |
| 67 | |
| 68 | ## How to state a safety invariant |
| 69 | |
| 70 | 1. **Write the model explicitly.** Name every set, constant and transition. Finite, concrete data (`Finset Nat`, `Fin n`) can be decided by the kernel with `by decide`. |
| 71 | 2. **State the property as a theorem over the model**, never as a comment. |
| 72 | 3. **Write a negative control**: a deliberately broken model and a theorem proving the property *fails* for it. A property that also "holds" for the broken model is vacuous. |
| 73 | 4. **Print the axioms** of every theorem. |
| 74 | |
| 75 | ```lean |
| 76 | import Mathlib.Data.Finset.Basic |
| 77 | import Mathlib.Order.Disjoint |
| 78 | |
| 79 | -- Core-assignment disjointness (finite, decidable) |
| 80 | def lanes : Fin 3 → Finset Nat |
| 81 | | 0 => {4, 5} | 1 => {6} | 2 => {8, 9} |
| 82 | def reserved : Finset Nat := {3, 7, 15} |
| 83 | theorem lanes_disjoint : ∀ i j, i ≠ j → Disjoint (lanes i) (lanes j) := by decide |
| 84 | theorem no_reserved : ∀ i, Disjoint (lanes i) reserved := by decide |
| 85 | |
| 86 | -- Disk floor arithmetic: a write of w GiB is admitted only if it keeps ≥ floor free |
| 87 | def admit (free w floor : Nat) : Bool := decide (w + floor ≤ free) |
| 88 | theorem floor_kept (free w floor : Nat) (h : admit free w floor = true) : |
| 89 | floor ≤ free - w := by |
| 90 | simp [admit] at h; omega |
| 91 | |
| 92 | -- Lease mutual exclusion: once held, acquire by anyone leaves the holder unchanged |
| 93 | structure St where holder : Option Nat |
| 94 | def acquire (s : St) (p : Nat) : St := if s.holder = none then ⟨some p⟩ else s |
| 95 | def release (s : St) (p : Nat) : St := if s.holder = some p then ⟨none⟩ else s |
| 96 | theorem acquire_excl (s : St) (p q : Nat) (h : s.holder = some q) : |
| 97 | (acquire s p).holder = some q := by simp [acquire, h] |
| 98 | theorem release_other (s : St) (p q : Nat) (h : s.holder = some q) (hpq : p ≠ q) : |
| 99 | (release s p).holder = some q := by simp [release, h, Ne.symm hpq] |
| 100 | |
| 101 | -- Bounded queue capacity: push never exceeds cap |
| 102 | def push (cap : Nat) (q : List Nat) (x : Nat) : List Nat := |
| 103 | if q.length < cap then q ++ [x] else q |
| 104 | theorem push_bounded (cap : Nat) (q : List Nat) (x : Nat) (h : q.length ≤ cap) : |
| 105 | (push cap q x).length ≤ cap := by |
| 106 | unfold push |
| 107 | split |
| 108 | · simp; omega |
| 109 | · exact h |
| 110 | |
| 111 | #print axioms lanes_disjoint -- [propext, Classical.choice, Quot.sound] |
| 112 | #print axioms floor_kept -- [propext, Quot.sound] |
| 113 | #print axioms push_bounded -- [propext, Quot.sound] |
| 114 | ``` |
| 115 | |
| 116 | These examples compile as `LeanVerify/SkillExamples.lean` in the project (`lake build` passes). The full worked example is `LeanVerify/CoreAssign.lean`: eight lane sets, `no_reserved` (none uses 3, 7 or 15), `overlap_6_7` (`{0,1} ∩ {1,2} = {1}`), `disjoint_except_6_7` (every other pair is disjoint) and a negative control from a live `taskset -cp` readout. |
| 117 | |
| 118 | For a state machine, prove the invariant is preserved by every transition (`inv s → inv (step s e)`) and holds initially; that gives it for every reachable state by induction. |
| 119 | |
| 120 | ## A theorem proves the MODEL, not the running system |
| 121 | |
| 122 | Every Lean result must state its **model-to-system link**: which file, config, `taskset -cp` readout, unit file or source function the model was taken from, at which revision or time, and how that was checked to match (read back, generated, or hand-copied). Without that link the claim is about the model only. Label it that way. Example: "PROVED for the 8-set model copied from Drake's message 2026-11-06; live `taskset -cp` shows `aien-serve-http` on 2-4, which is NOT in the model and violates it." A proof never upgrades a status beyond what `omega-communication` allows. It is evidence about a design. It does not qualify hardware or a deployment. |
| 123 | |
| 124 | ## Forbidden: `sorry`, extra axioms, native evaluation |
| 125 | |
| 126 | - `sorry` (axiom `sorryAx`) means *not proved*. Lean reports `declaration uses 'sorry'` as a **warning** and `lean_run_code` still returns `success: true`, so "no errors" is not enough. Always check the axioms. |
| 127 | - An elaboration error also turns the declaration into `sorryAx` (`lake build` showed `depends on axioms: [sorryAx]` for a theorem whose proof failed), which is another reason to read the axiom list rather than trust a partial log. |
| 128 | - Run `#print axioms <thm>` (or `lean_verify`). The only acceptable set is a subset of `[propext, Classical.choice, Quot.sound]`. "does not depend on any axioms" is best. |
| 129 | - `native_decide` (axioms `Lean.ofReduceBool` / `Lean.trustCompiler` / `*._native.native_decide.ax*`) trusts the compiler rather than the kernel. Use `decide` or `decide +kernel`. |
| 130 | - A new `axiom` declaration, `unsafe`, `@[implemented_by]` or `@[extern]` in a proof file is forbidden. |
| 131 | - Report the verdict with the axiom list: `PROVED (axioms: [propext]) model=<source>` or `FAILED: <error>`. |
| 132 | |
| 133 | ## Running outside MCP |
| 134 | |
| 135 | ```bash |
| 136 | ssh <spark> 'cd ~/workspace/lean-verify && PATH=$HOME/.elan/bin:$PATH taskset -c 0,1 nice -n 10 lake env lean LeanVerify/CoreAssign.lean' |
| 137 | ``` |
| 138 | |
| 139 | Put new proofs under `LeanVerify/` and import them from `LeanVerify.lean` so `lake build` checks them all. |
| 140 | |
| 141 | ## Repair |
| 142 | |
| 143 | - Server does not start: `ssh <spark> ~/workspace/lean-verify/.mcp-venv/bin/lean-lsp-mcp --version` must print `lean-lsp-mcp 0.33.0`. Reinstall the pinned set with `python3 -m venv .mcp-venv && .mcp-venv/bin/pip install -r .mcp-venv-freeze.txt` (network needed once). |
| 144 | - `unknown module Mathlib...` or `environment does not contain`: wrong or missing import (for example `Finset.card` needs `Mathlib.Data.Finset.Card`), or oleans missing. For the latter run `lake exe cache get` (network, once) then `lake build` in the project, on cores 0–1. |
| 145 | - Upgrading Lean or Mathlib: change `lean-toolchain` and the mathlib `rev` together to the same `v4.x.y` tag, `lake update`, `lake exe cache get`, `lake build`, rerun the smoke test (initialize, `tools/list`, the three snippets above), and update the pins table. |