Work with a Lema project
Create a project with lema new, author Pythonic .lema contracts and .lean implementations with an AI coding agent, run the dev loop, and configure actor-governed reference policies.
A Lema project combines a lightweight source workspace (lema.json and src/) with a local content-addressed store (.lema/store) and a background server (.lema/lema.sock). Instead of tracking line diffs across text files, the project layer tracks accepted mathematical declarations, contract selections (lema.contract/v1), and semantic revisions (lema.revision/v1) on a governed reference such as main.
This structure is designed for both solo developers pairing with an AI coding agent (such as Claude Code or Codex) and multi-actor engineering teams where human maintainers gate protected contracts while agents supply implementations and proofs.
Before you start
lemaandlema-vm-runnerbuilt and on yourPATH, with~/.lema/runtime.jsoninitialized vialema toolchain install(Set up a machine for Lema).
1. Create a project (lema new)
Run lema new with your project name:
lema new counter
cd counter
lema statuscreated project counter (/home/you/counter)
server: running (pid 52104, socket /home/you/counter/.lema/lema.sock)
store: /home/you/counter/.lema/store (accepted: 184, aliases: 42)
ref: main (revision: none, generation: 1, policy_version: 1)
contracts: 0 selectedlema new counter sets up the entire project in one step:
- Writes
lema.jsonusing schemalema.project/v1:lema.json { "schema": "lema.project/v1", "name": "counter", "namespace": "Counter", "salt": "counter-8b1e4d2a", "default_ref": "main", "modules": ["src/Main.lean"] } - Initializes
.lema/storeand loads the pinnedv0.2foundational signature (Core,AppCmd,Frames, andCapability) so standard library and capability types resolve immediately. - Registers a local development actor (
dev) and writes its private authentication token to.lema/token(mode 0600), along with an initiallema.policy/v1and themainproject reference. - Starts the background server detached on
.lema/lema.sock(automatically falling back to a short socket path under$XDG_RUNTIME_DIR/lema/or~/.lema/run/if your project directory path exceeds the OSsockaddr_un.sun_pathlimit of107bytes on Linux or103bytes on macOS).
Because every lema command discovers lema.json by walking upward from your current working directory, you can run commands from the project root or any subdirectory without passing --store, --socket, or --runtime.
2. Author .lema contracts and .lean implementations (lema add)
When pairing with an AI coding agent such as Claude Code or Codex, keep your contract specifications and select statements in .lema files under src/, and let your agent write implementations and theorem proofs in .lean files.
Create src/Counter.lema:
Pythonic
def advance(n: Nat) -> Nat:
n + 1
def Advances(f: Nat -> Nat) -> Prop:
forall n: Nat :: f(n) > n
select Advances(advance)Lean
These declarations express the same implementation and promise. Selection is a Lema codebase operation; the final comment names its target, without supplying a proof.
def advance (n : Nat) : Nat := n + 1
def Advances (f : Nat → Nat) : Prop :=
∀ n : Nat, f n > n
-- Selected proposition: Advances advanceExplanation
For every nonnegative integer n, the selected function must return a number greater than n. The implementation advance adds one.
Advances describes the promise independently of the implementation. Selecting Advances(advance) records an open obligation about that exact function. It becomes checked only when a matching proof is accepted.
The promise requires an increase, but does not require an increase of exactly one. A function that adds two would satisfy it too.
Publish src/Counter.lema into the project:
lema add src/Counter.lemaadded src/Counter.lema
Counter.advance #2c91e04b7a12 (def, checked, 0 axioms)
Counter.Advances #7d18f53c90e4 (def, checked, 0 axioms)
select Counter.Advances(Counter.advance) #b409a812e3f1 (contract, open)Notice what happens when you add .lema and .lean files with lema add:
- Cross-module dependency ordering: When you pass multiple files (
lema add src/Counter.lema src/CounterProof.lean) or edit an upstream file,lema addresolves symbolic references across the project’slema.authoring/v2records, orders modules topologically, updatesmodulesinlema.json, and re-adds downstream modules whose symbolic imports moved. - Exact caret and
--jsondiagnostics for agents: If elaboration or kernel replay fails,lema addmaps Lean’s line and column spans back to the authored file (file:line:col) with a caret pointer, or emitserrors: [{file, line, column, message}]when invoked withlema add --json. - Proof-optional contract selections: Selecting
Advances(advance)creates an immutablelema.contract/v1record in.lema/storewithstatus: "open", pinningCounter.advance’s exact object ID intarget.realizations. When your coding agent adds a.leantheorem that passesStore::check_witnessagainst both the target statement andtarget.realizations,lema addwrites a new immutablelema.contract/v1record withstatus: "checked"andby: <theorem_id>.
3. Run the development loop (lema test, lema run, lema dev)
You can test and execute your code on the Lema VM at any time, even while contract selections are still open:
lema test
lema run
lema devlema test [ROOT] [--expect TEXT]evaluates pureByteArraytest roots directly on the Lema VM and runsProgtest roots vialema.exec/v2.lema run [APP] [--input FILE]automatically infers theProgcommand families from your entry’s accepted type in the store, synthesizes or updates itslema.exec/v2record, compiles the closure toLemaIR(reusing.lema/store/vm-code-cachewhen inputs match), and runs it inlema-vm-runner. (Applications that touch secrets viaIo::Secret,exchange, orserverequirelema releaseandlema run --release; see Release an application.)lema devwatchessrc/continuously, re-adding changed and downstream modules and refreshing contract status on every save.
4. Inspect changes and commit a semantic revision (lema diff and lema commit)
Before recording a revision on main, inspect the semantic change report:
lema diff
lema commit -m "Add Counter contract and advance implementation"lema commit -m INTENT [--ref NAME] [--waive OBLIGATION=REASON..] performs the full semantic version control cycle in one command:
- Snapshots your current project bindings (
Store::create_binding_map->lema.binding-map/v1) and authoring bundles into an immutable snapshot (Store::create_snapshot->lema.snapshot/v2). - Computes the semantic change report (
lema.change-report/v1) against the target reference’s current head revision. - Discharges any contract obligations whose selections are now
checkedviaStore::check_witness, carries any remainingopenobligations inrevision.obligations, attaches newacceptedandguaranteereceipts torevision.evidence, and creates thelema.revision/v1record. - Publishes the revision to
mainusing atomic compare-and-swap expectations ({revision, generation, policy_version}).
5. Advanced: multi-actor governance and custom reference policies
In a team or production repository, you often want to separate the coding agent actor (allowed to publish implementations and proofs) from the human steward actor (whose approval is required before a protected contract can change or an obligation can be waived).
Registering additional actors
Stop the background server briefly (lema stop), register your actors against .lema/store, and start the server again (lema start):
lema stop
lema actor add --store .lema/store --id steward --operator
lema actor add --store .lema/store --id agent
lema startEach lema actor add call prints a random token once and stores only its SHA-256 hash in .lema/store/actors.json. Save the printed tokens in owner-only (0600) files such as steward.token and agent.token. You can list registered actors at any time (with the server stopped) using lema actor list --store .lema/store.
Upgrading the reference policy (lema.policy/v1)
Write a policy JSON file that allows agent to publish revisions on main, requires zero open obligations (require.no_obligations: true), protects the Counter.Advances contract so changing it requires an explicit approval review from steward, and restricts waivers to steward:
{
"version": 2,
"previous": "<current_policy_id_from_lema_status>",
"publish": ["agent", "steward"],
"adopt": ["steward"],
"waive": ["steward"],
"require": {
"no_obligations": true,
"axioms": ["propext", "Quot.sound", "Classical.choice"]
},
"protect": [
{
"exports": ["Counter.Advances"],
"reviewers": ["steward"],
"needed": 1
}
]
}Store the new policy and adopt it on main:
lema policy create --file policy-v2.json
lema adopt-policy --ref main --policy $NEW_POLICY_ID \
--expect-revision $CURRENT_REV --expect-generation $GEN --expect-policy-version 1 \
--actor steward --token-file steward.tokenFrom that point onward, if agent runs lema commit while a contract selection is still open, the store saves the revision record in .lema/store and refuses to advance main until agent proves the contract (or steward records a deliberate waiver or review approval; see Review and merge semantic changes and Build static inspection pages).
Common refusals
| Refusal message contains | Cause and fix |
|---|---|
socket ... answers for store ..., not ... | Another lema serve process is bound to that socket path for a different store. Run lema stop --socket PATH or remove the stale socket. |
both --store and --socket were supplied | Commands that operate either directly on a store directory or over a server socket refuse when given both flags (LEMA-232). Pass one or rely on project auto-discovery. |
the store at ... is in use | A store-direct command (lema actor, lema site build --store) was run while the background server held the store lock. Run lema stop first, then lema start afterwards. |
theorem ... binds ... to ..., and the selection binds it to ... | Store::check_witness caught a theorem proving the right surface statement about a different implementation ID. Ensure the proof module imports the project’s current implementation. |
stale expectation or stale policy | Another actor advanced the reference or upgraded its policy concurrently. Run lema status to inspect the latest generation and policy version. |
Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility