Skip to content

Review and merge semantic changes

Inspect the seven semantic change categories and contract selection statuses with lema diff, commit revisions with lema commit, land a workspace's work, approve protected changes, and merge diverged lines with lema merge.

In a line-based version control system like Git, reviewing a pull request or merging two branches means reading a text diff and hoping that textually separate edits do not break each other’s invariants. When an AI coding agent such as Claude Code or Codex generates 800 lines of code and tactics across five files, line-by-line review quickly turns into rubber-stamping.

Lema’s Semantic Version Control (SVC) operates directly on content-addressed identities in .lema/store (crates/lema-store/src/changes.rs and merge.rs), completely independent of Git. Computing a semantic diff or a three-way merge never invokes the Lean 4.34.1 kernel; it is an instant, deterministic calculation over stored snapshots (lema.snapshot/v2), contract selections (lema.contract/v1), kernel receipts, and proof witnesses.

Before you start

  • A Lema project with its background server running (Work with a Lema project).
  • Any modified .lema or .lean files published into your store workspace with lema add (the project’s default workspace, or the one your directory is linked to; lema workspace show says which).

1. Inspect semantic changes with lema diff

Run lema diff to compare your workspace’s snapshot against the head of main (or pass one or two revision IDs, snapshot IDs or workspace:NAME to compare others):

Shell
lema diff                        # working tree vs. default ref (main)
lema diff --ref staging          # working tree vs. another ref
lema diff $REV_A $REV_B          # compare two specific revisions
lema diff --json                 # full canonical lema.change-report/v1 JSON

Every difference between the before-snapshot and after-snapshot is classified into one of seven semantic categories, each with its own evidence rule:

CategoryWhat changedEvidence requiredOpens an obligation when
renameAn export’s name changed while pointing to the exact same object ID and role.identityNever (pure rename).
exportsAn export or root was added or removed.receipt (when added), deprecation (when removed).Always when an export is removed (requires an explicit waiver).
rebindAn export moved to a theorem with the exact same statement ID (for example, a faster proof) or a definition with the exact same template ID.receiptOnly if the new declaration lacks a kernel receipt.
substituteAn export moved to a different implementation (template ID changed).replacement (replace witness or checked lema.contract/v1 selection).No replacement witness or checked contract covers the new implementation.
evolveAn export moved to a refined or corrected contract (refine / correct), or to a declaration with a new statement ID.refinementThe move is a contract correction or an unrelated statement change.
policyThe reference’s active lema.policy/v1 changed.decisionThe previous policy did not authorize the adoption.
materialAuthoring bundles (lema.authoring/v2), source files, notes, docs, configs, or assets changed.digest or mappingNever (non-semantic material).

Reading the contracts selection summary in lema diff

Alongside the seven export categories, lema diff summarizes every selected contract (lema.contract/v1, records::contract::summary):

  • checked (by: <theorem_id>): An accepted theorem passed Store::check_witness against the whole selection’s target (verifying both target.statement and target.realizations implementation bindings).
  • open (k/n laws proved): Either no witness theorem has been accepted yet, or an accepted theorem matched only an individual law target (laws[i].target, recorded in laws[i].by) of a multi-law contract structure. Individual law witnesses track partial progress (1/2 laws proved) without marking the whole selection checked or discharging its revision obligation.
  • waived (by: <actor>, reason: "..."): Closed by an explicit governance waiver (--waive), never counted as proof evidence.

For any rebind, substitute, evolve, or removed export, the JSON report (lema diff --json) also includes old_still_used, listing any remaining exports or roots in the snapshot that still depend on the old object ID. A report is marked conservative: true when every change only adds new checked exports/roots or updates non-executable material (doc, sources, or notes).

2. Commit a revision (lema commit) and handle protected reviews

Once you are satisfied with lema diff, record a semantic revision and advance the target reference:

Shell
lema commit -m "Optimize advance and prove Advances(advance)"

In a project, lema commit commits your workspace’s snapshot. It plans the commit (commit.plan), which computes the change report, discharges any {claim, selection: <contract_id>} obligation whose selection is now checked (discharged: [{obligation: claim, by: <theorem_id>}]) and carries the remaining open obligations in revision.obligations. The plan cites as evidence the current guarantee records rooted at the snapshot’s members; an accepted theorem is cited as the by of the obligation it discharges, and --evidence-file FILE adds anything else you want the revision to cite. Lema then stores the revision and advances your workspace in one step (workspace.commit), and lands it on the reference.

Pass --no-publish to stop after that local commit. The revision is your workspace’s new head and the reference has not moved; commit again as often as you like, and land the chain when it is ready (step 4).

What happens when a policy gate requires a human review

If the active policy on main protects a modified contract or export (protect in lema.policy/v1), lema commit stores the new revision ($REV) in .lema/store and prints the approval view digest (lema.review-display/v1, domain LEMA-REVIEW-DISPLAY-v1\0):

Output when a protected export requires review
stored revision #7c18a902f4b1 (waiting for review on main)
  protected exports: Counter.Advances (needs 1 review from [steward])
  approval display digest: 3e91b84c02d7…

That display digest binds the transition on main, the policy version, the rendered Pythonic .lema text of every protected declaration (Outcome::Faithful), and lema_core::view::renderer_sha256(). A human reviewer inspects the rendered contract view (via lema view or Build static inspection pages) and records their approval:

Shell
lema review --ref main --revision #7c18a902f4b1 --decision approve \
  --display 3e91b84c02d7… \
  --actor steward --token-file steward.token

Once the required review is recorded, re-running lema commit (or lema publish-revision) advances main immediately.

3. Record a deliberate waiver (--waive)

When you intentionally want to publish a revision with an unproved contract selection or a deprecated export removal (and the reference policy authorizes your actor under waive), pass --waive:

Shell
lema commit -m "Remove legacy helper and defer experimental law" \
  --waive Counter.experimental_law="Deferred until v2 storage migration"

Lema records each waiver in the revision’s discharged array as {obligation, waiver: "<reason>"}. Moving the reference to a revision that brings waived obligations into its history is refused unless the publishing actor is listed under waive in the reference’s active policy. This is separate from a contract selection whose own status is waived (lema.contract/v1), which records a waiver on the selection itself. Both lema diff and generated inspection sites show waivers distinctly from proofs; a waiver never turns an unproved claim into a theorem.

4. Land a workspace’s work (lema workspace land)

When an agent works in a workspace of its own (lema workspace create agent-1 --from main --dir ../agent1), its lema commit --no-publish revisions stay on that workspace until you land them:

Shell
cd ../agent1
lema workspace land --token-file ../escrow/.lema/token -m "Land agent-1's storage work"

If main has not moved since the workspace’s first local commit, landing fast-forwards main to the workspace’s head. If it has moved, Lema merges the workspace’s head with main’s first, exactly as lema merge does (pass --resolve FILE when the merge reports a conflict), publishes the merge, and moves the workspace to it. The project manifests of both sides merge module by module, so two workspaces that each added a module merge cleanly. Each step is recorded in the workspace, so an interrupted landing resumes where it stopped when you run the command again.

5. Merge another line of work (lema merge)

Suppose two developers (or two coding agents working on separate branches/revisions) made concurrent changes starting from a common ancestor, and you want to merge $OTHER_REV into main:

Shell
lema merge $OTHER_REV --into main -m "Merge concurrent storage and parser updates"

How does lema merge (crates/lema-store/src/merge.rs, lema.merge/v1) work without line diffs?

  1. Finds the nearest common ancestor (base): It walks the revision graph to locate the nearest common ancestor snapshot $BASE of main’s head ($OURS) and $OTHER_REV ($THEIRS).
  2. Merges key by key by identity: It compares each parent’s after-snapshot against $BASE across export names, contract selections, assets, notes, and authoring bundles. If only one parent changed a key, or both parents changed it to the exact same SHA-256 object ID, the merge takes that change cleanly and preserves merge.reuse evidence coverage from both parents.
  3. Reports typed semantic conflicts when both parents touched the same key incompatibly:
Conflict kindWhat happened
collisionBoth parents added the same new export or asset key pointing to different object IDs.
delete-updateOne parent removed a key while the other modified it.
update-updateBoth parents modified the same existing key to different object IDs.
rename-renameBoth parents renamed the same base export to different new names.
bundleA source file or note merged cleanly by key, while the merged authoring bundle set does not contain that value.

Resolving a semantic merge conflict (--resolve)

If lema merge reports a conflict, write a small JSON resolution file naming only the conflicting keys and the chosen object IDs (or null to delete):

resolve.json
{
  "exports": {
    "Counter.advance": {
      "id": "2c91e04b7a12…"
    }
  }
}

Then re-run lema merge with --resolve resolve.json:

Shell
lema merge $OTHER_REV --into main --resolve resolve.json -m "Merge and select verified Counter.advance"

Note that lema merge refuses any resolution file that mentions a non-conflicting key or sneaks in an unrelated edit; put new edits in a follow-up commit so the merge record remains a faithful combination of its parents.

The merge revision carries every obligation either parent left open (inherited), and it discharges none of them. Discharge an inherited obligation in a later lema commit, once the theorem that proves it is in your workspace.

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