Skip to content

Run and release a program on the Lema VM

Run pure ByteArray tests with lema test, execute interactive Prog applications on the Lema VM with automatic family inference and vm-code-cache reuse, watch files with lema dev, and cut a release with lema release and lema run --release.

In Lessons 3 through 5, you authored Pythonic .lema contracts and .lean implementations, checked proofs in the pinned Lean 4.34.1 kernel, and tracked semantic revisions with lema diff and lema commit. Now you will see how Lema compiles accepted store objects to LemaIR and executes them on the Lema VM (lema-vm and lema-vm-runner).

Along the way, you will use all four parts of Lema’s execution workflow:

  1. lema test for pure ByteArray test roots and Prog test vectors.
  2. lema run for interactive Prog applications, with automatic command-family inference into lema.exec/v2 and <store>/vm-code-cache compilation reuse.
  3. lema dev for continuous file watching across src/.
  4. lema release and lema run --release for pinning a release artifact and deployment configuration (and why applications that touch secrets must run through a release).

1. Run a pure ByteArray test (lema test)

The simplest runnable unit in Lema is a pure definition of type ByteArray. Because a ByteArray value makes no network or storage calls, lema test evaluates its accepted closure directly in the Lema VM (crates/lema-cli/src/vm_test.rs) and compares the resulting bytes against an explicit test oracle. Use --expect for expected UTF-8 text, --expect-sha256 for its digest, or --report lines for a test-report root.

In your hello project, add a test root Reuse.report to src/Reuse.lean that checks keep and addZero on sample inputs and formats a summary line:

src/Reuse.lean (adding a pure ByteArray test root)
def report : ByteArray :=
  if keep (keep 7) == 7 && addZero (addZero 7) == 7 then
    "reuse vectors ok\n".toUTF8
  else
    "reuse vectors fail\n".toUTF8

Publish the update and run lema test:

Shell
lema add src/Reuse.lean
lema test Reuse.report --expect "reuse vectors ok"
Output
test Reuse.report: passed (output matched "reuse vectors ok")

Notice that lema test did not invoke lean or a C compiler (leanc was retired in Decision 0052). Instead, Lema read the accepted canonical objects for Reuse.report, Reuse.keep, and Reuse.addZero directly from .lema/store, compiled them in pure Rust to LemaIR (lema.ir/v1), and executed the register bytecode on the Lema VM.

2. Run a Prog application on the Lema VM (lema run)

While pure functions return a ByteArray, real applications interact with timers, storage, network sockets, or concurrent tasks by returning a program value (Prog Cmd Reply A from Core). A Prog value is a pure decision tree: at each step, it either returns a final answer (Prog.ret bytes) or yields a typed command (Prog.call cmd next) and waits for the host runner (lema-vm-runner) to supply the reply.

Inspect src/Main.lean and update it so Hello.app is a Prog application entry point (Nat → ByteArray → Prog AppCmd AppReply ByteArray, where task 0 is the main task and input holds the request bytes passed to the run):

src/Main.lean
set_option autoImplicit false
namespace Lema
namespace Hello

def app (task : Nat) (input : ByteArray) :
    Received.Prog Received.AppCmd Received.AppReply ByteArray :=
  Received.ret ("hello, ".toUTF8 ++ input)

end Hello
end Lema

Publish src/Main.lean and run Hello.app on the Lema VM with lema run:

Shell
echo "world" > input.txt
lema add src/Main.lean
lema run Hello.app --input input.txt
Output
hello, world

What lema run just did automatically

Behind that one-line command, lema run handled three steps that used to require manual JSON manifests:

  1. Automatic Prog command-family inference (LEMA-377): lema run inspected the accepted type of Hello.app in .lema/store, inferred its command families automatically (or used --families if you passed an explicit list), and synthesized the store’s lema.exec/v2 execution record (crates/lema-cli/src/exec_records.rs).
  2. Store-native LemaIR compilation and <store>/vm-code-cache reuse (LEMA-352): Lema compiled the accepted closure of Hello.app to LemaIR and stored the verified compile memo inside .lema/store/vm-code-cache. On subsequent runs, if the accepted root IDs, compiler version, and target (vm) have not changed, lema run reuses the cached LemaIR artifact immediately without recompiling.
  3. Sandboxed VM execution: lema-vm-runner executed task 0 of Hello.app inside the sandbox with deterministic step, allocation, bigint-bit, and call-depth budgets, and printed the returned bytes to standard output.

3. Watch your project continuously with lema dev

When you are actively editing .lema contracts and .lean implementations (or pairing live with an AI coding agent in your editor), run lema dev:

Shell
lema dev
Output
watching src/ in /home/you/projects/hello (ctrl-c to stop)
  [14:02:11] src/Double.lema changed -> re-added src/Double.lema, src/DoubleProof.lean (2/2 contracts checked)

Whenever a file under src/ is saved, lema dev automatically:

  • parses and elaborates the changed .lema or .lean file;
  • topologically re-adds any downstream project modules in lema.json whose symbolic imports (lema.authoring/v2) moved;
  • prints exact file:line:col caret diagnostics if a proof or type check fails; and
  • updates your contract selection summary (open vs. checked) in real time.

Press Ctrl-C to stop lema dev when you are ready to cut a release.

4. Cut a release and run through the release gate (lema release and lema run --release)

During development, lema run can directly execute pure tests and applications that do not touch secrets. However, Lema enforces a strict security boundary (mandatory secret admission, LEMA-384 / LEMA-421) whenever an application uses credentials or cryptographic keys:

  • Any application that names Io::Secret or uses the opaque exchange (client SigV4 / BearerHeader) or serve (serveVerified TLS 1.3 + SigV4 server) families must pass two static LemaIR analyzers at build time: ConstantTime (no secret branches, secret memory indices, or variable-time operations) and SecretFlow.<role> (secrets flow only into approved pinned exchange or server sinks).
  • In addition, secret-using applications refuse unverified lema run and can only be executed through an approved release record (lema.release/v1) via lema run --release.

Even for an application without secrets like Hello.app, cutting a release binds the accepted store closure, the compiled LemaIR artifact, the lema-vm-runner worker binary hash, and the deployment configuration (lema-cap.json) into a single immutable record:

Shell
lema release Hello.app
lema run --release --input input.txt
Output
created release #91c4e820a7b3 for Hello.app (target: vm, saved to .lema/last-release)
hello, world

Notice that you did not have to copy and paste the 64-character release ID into lema run --release:

  • lema release wrote the newly created release ID (#91c4e820a7b3...) to .lema/last-release.
  • When you run lema run --release without an explicit ID inside a project, it reads .lema/last-release and runs the full release verification gate: checking every kernel receipt in the closure, the cached LemaIR code correspondence, any required ConstantTime and SecretFlow guarantee receipts, the lema-vm-runner binary digest, and the deployment configuration before starting the VM worker.

Where to go next

You have completed all six tutorials: reading Pythonic .lema and Lean contracts, setting up the toolchain and background server, creating a project, proving a selected contract with Store::check_witness, reusing a general theorem across implementations, and running and releasing code on the Lema VM.

The last lesson, Work alongside an agent in store workspaces, shows how a coding agent and you can work on one project at once, each in a workspace of your own, and bring the agent’s work into main.

From here:

Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility