How the store accepts code
How Lema separates untrusted elaboration from source-free Lean kernel replay, enforces a fixed axiom floor, and keeps daily checking fast with incremental delta modules.
When an AI coding agent writes a thousand lines of implementation code and proof tactics, how does Lema make sure the agent did not slip in an unproved assumption (sorry), exploit a parser or macro quirk, trust native code during checking (native_decide), or hang your machine?
Lema enforces a strict architectural firewall between elaboration (turning human- or agent-authored text into an untrusted candidate draft) and acceptance (replaying pure canonical objects from scratch, with zero source text, through a pinned Lean 4.34.1 kernel inside a locked-down sandbox).
Elaborator & tactic search
Parses Pythonic or Lean text, runs macros and tactics, and emits candidate canonical JSON.
Rust check + Lean 4.34.1 kernel
lema-coreCanonical JSON & boundsaddDeclCoreSource-free replaycollectAxiomsRefuses sorryAx & _native
Objects + immutable receipt
Stores verified SHA-256 objects, records exact axiom footprint, and caches delta modules.
Source text, macros, and proof tactics stop at the sandbox boundary; only canonical objects passing addDeclCore and the axiom floor enter the store.
Step 1: Untrusted elaboration (lema add and publish)
When you or an agent run lema add, Lema resolves your project’s pinned dependency scopes (Decision 0048 and Decision 0050):
- Declarations written in Pythonic Lema syntax are parsed with line/column spans, paired with deterministic proof synthesis where applicable (Where the ideas come from), and lowered alongside Lean declarations inside an isolated worker process.
- Lean’s elaborator resolves types and runs proof tactics, then exports the resulting constants into canonical objects (
statement,template,proof,satisfaction,declaration,record,family).
Nothing in that draft is trusted or stored yet. Parsers, macro expanders, and tactic engines are large programs, so Lema treats their output purely as an unverified proposal. If elaboration fails, Lema maps errors back to caret diagnostics without touching the store.
Step 2 & 3: Rust validation and source-free kernel replay
Before invoking the kernel checker, the Rust store validates the candidate payload in memory: canonical key ordering, deduplicated expression DAGs (Decision 0030), inductive family rules (Decision 0039), complete helper units (Decision 0045), and atomic workspace/alias expectations (Decision 0044).
Once structural validation passes, Lema streams the ordered canonical objects into the replay decoder inside the pinned Lean 4.34.1 runtime:
- Zero source text crosses the boundary. Original Pythonic Lema or Lean text, macros, and tactic scripts are discarded completely.
- Direct kernel checking (
addDeclCoreat--trust=0): The decoder checks that the toolchain version and Git hash match the pinnednamespace, verifies externalInitconstants, plugsproofobjects into the numbered holes of theirtemplateobjects, computes deterministic unfolding heights, and submits each declaration directly to Lean’s small kernel checker. - Proof-optional selections never admit
sorryAx: When you record anopencontract selection (select LawfulEscrowStorage(MemoryStorage)under Decision 0051), the store never inserts a fake axiom into the object graph. Instead, it asks the kernel to check the tautologyT → T(fun h => h), confirmingTis a well-typed closed proposition over your pinned implementations before recording the selection asopen.
Step 4: The two-level axiom audit and receipts
A theorem is only as trustworthy as the axioms behind it. In interactive Lean, writing sorry silently introduces sorryAx, and native_decide introduces an axiom trusting native code execution during checking.
After kernel replay succeeds, Lema inspects every constant’s transitive axiom footprint (collectAxioms) against two strict rules:
- The fixed floor (unconditionally refused in every store): Lema rejects
sorryAx,Lean.ofReduceBool,Lean.ofReduceNat,Lean.trustCompiler, any_nativeaxiom fromnative_decide, and anyunsafeconstant. No flag or configuration can turn this check off. - The store’s axiom policy: Above that floor, every declaration’s axioms must stay within the store’s configured axiom policy. By default, a store permits Lean’s three standard mathematical axioms (
propext,Quot.sound, andClassical.choice), and you can initialize a strictly constructive store withlema init --axioms propext,Quot.sound.
Only when both checks pass does the store commit the objects and write an immutable accepted receipt recording the checker digests, host profile, and exact audited axiom set.
Fast incremental checking and full lema reverify
A mature store holds tens of thousands of accepted objects across Core, cryptography, TLS 1.3, HTTP/1.1, S3, and BlockStore. Replaying all 30,000+ objects from raw JSON on every ten-line edit would slow daily work to a crawl.
- Incremental delta modules for daily speed: Accepted batches are compiled into hash-verified delta modules so
lema addonly replays your new declarations against already-checked dependencies. - Full-store replay on demand (
lema reverify): Because loading a cached module trusts local disk state from an earlier check,lema reverifyignores all cached modules, streams the entire accepted store from canonical JSON throughaddDeclCorefrom scratch, and confirms every receipt. - Sandboxed checker workers: Every Lean elaboration or replay call verifies the pinned Lean 4.34.1 distribution digests and runs inside an isolated OS sandbox with no network access and bounded memory/CPU quotas (Linux
bubblewrapunder Decision 0034, or macOS Seatbelt).
Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility