Skip to content

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

  • lema and lema-vm-runner built and on your PATH, with ~/.lema/runtime.json initialized via lema toolchain install (Set up a machine for Lema).

1. Create a project (lema new)

Run lema new with your project name:

Shell
lema new counter
cd counter
lema status
Output
created 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 selected

lema new counter sets up the entire project in one step:

  1. Writes lema.json using schema lema.project/v1:
    lema.json
    {
      "schema": "lema.project/v1",
      "name": "counter",
      "namespace": "Counter",
      "salt": "counter-8b1e4d2a",
      "default_ref": "main",
      "modules": ["src/Main.lean"]
    }
  2. Initializes .lema/store and loads the pinned v0.2 foundational signature (Core, AppCmd, Frames, and Capability) so standard library and capability types resolve immediately.
  3. Registers a local development actor (dev) and writes its private authentication token to .lema/token (mode 0600), along with an initial lema.policy/v1 and the main project reference.
  4. 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 OS sockaddr_un.sun_path limit of 107 bytes on Linux or 103 bytes 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

Counter contract
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.

Lean syntax
def advance (n : Nat) : Nat := n + 1

def Advances (f : Nat → Nat) : Prop :=
  ∀ n : Nat, f n > n

-- Selected proposition: Advances advance

Explanation

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:

Shell
lema add src/Counter.lema
Output
added 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 add resolves symbolic references across the project’s lema.authoring/v2 records, orders modules topologically, updates modules in lema.json, and re-adds downstream modules whose symbolic imports moved.
  • Exact caret and --json diagnostics for agents: If elaboration or kernel replay fails, lema add maps Lean’s line and column spans back to the authored file (file:line:col) with a caret pointer, or emits errors: [{file, line, column, message}] when invoked with lema add --json.
  • Proof-optional contract selections: Selecting Advances(advance) creates an immutable lema.contract/v1 record in .lema/store with status: "open", pinning Counter.advance’s exact object ID in target.realizations. When your coding agent adds a .lean theorem that passes Store::check_witness against both the target statement and target.realizations, lema add writes a new immutable lema.contract/v1 record with status: "checked" and by: <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:

Shell
lema test
lema run
lema dev
  • lema test [ROOT] [--expect TEXT] evaluates pure ByteArray test roots directly on the Lema VM and runs Prog test roots via lema.exec/v2.
  • lema run [APP] [--input FILE] automatically infers the Prog command families from your entry’s accepted type in the store, synthesizes or updates its lema.exec/v2 record, compiles the closure to LemaIR (reusing .lema/store/vm-code-cache when inputs match), and runs it in lema-vm-runner. (Applications that touch secrets via Io::Secret, exchange, or serve require lema release and lema run --release; see Release an application.)
  • lema dev watches src/ 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:

Shell
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:

  1. 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).
  2. Computes the semantic change report (lema.change-report/v1) against the target reference’s current head revision.
  3. Discharges any contract obligations whose selections are now checked via Store::check_witness, carries any remaining open obligations in revision.obligations, attaches new accepted and guarantee receipts to revision.evidence, and creates the lema.revision/v1 record.
  4. Publishes the revision to main using 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):

Shell
lema stop
lema actor add --store .lema/store --id steward --operator
lema actor add --store .lema/store --id agent
lema start

Each 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:

policy-v2.json
{
  "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:

Shell
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.token

From 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 containsCause 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 suppliedCommands 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 useA 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 policyAnother 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