Skip to content

Move an alias safely

Point a human-readable alias at a new accepted object using compare-and-swap expectations (expect, generation, and idempotency_key) so concurrent agents never overwrite each other.

In a Lema store, accepted declarations, records, and inductive families are immutable and identified by their 64-character SHA-256 hashes. Human-readable names live outside object identity as mutable pointers called aliases.

During ordinary work inside a lema.json project, lema add (and lema publish --expect current) resolves and updates your module’s alias expectations automatically. Use the lower-level alias commands on this page when you are driving lema serve directly from an external tool or multi-agent pipeline and want to move a specific alias to an already-accepted object with compare-and-swap protection.

Why every alias move is a compare-and-swap operation

Suppose two automated coding agents (or an agent and a human developer) are connected to the same lema serve socket and both try to update app.main at the same time. If moving an alias were a blind overwrite, the second caller would silently clobber the first caller’s update.

Instead, every alias move in Lema requires you to state what target (and optionally what generation counter) you expect the alias to hold right now (expect). If another caller moved the alias after you read it, the server refuses your request immediately without touching the alias or starting any Lean call.

Before you start

  • A running server (either started automatically in a project via lema start, or listening on --socket $SOCK).
  • The 64-character SHA-256 ID of the new target object, which must already be accepted in this store (along with its role if it is a multi-role object such as a record or family).

1. Read the current alias binding and generation (find)

Query find with the alias name (or a prefix such as "app.") to read its current 64-character object ID and generation counter:

Shell
lema call find --params '{"prefix": "app.main"}'
Output
{
  "app.main": {
    "generation": 2,
    "id": "04527d69167f…"
  }
}

Always copy the full 64-character id from find or lema add --json rather than guessing from a 12-character terminal abbreviation.

2. Submit the compare-and-swap move (alias)

Send an alias request naming the alias, the new target, the expect object copied from Step 1, and an optional idempotency_key:

alias-move.json
{
  "name": "app.main",
  "target": {
    "id": "ef20c6e6b2e2…"
  },
  "expect": {
    "id": "04527d69167f…",
    "generation": 2
  },
  "idempotency_key": "agent-1/app.main/gen-2-to-3"
}
Shell
lema call alias --params-file alias-move.json
Output
{
  "name": "app.main",
  "id": "ef20c6e6b2e2…",
  "generation": 3,
  "changed": true
}

Moving an alias appends one atomic entry to the store’s history.jsonl and makes zero native Lean calls.

Choosing the right expect mode

expect value in JSONCLI flag on lema publishWhen the move succeeds
null--expect noneOnly when the alias name does not exist yet (creates the alias at generation: 1).
{"id": "...", "role"?: "..."}--expect current (resolves current ID automatically)When the alias currently points to that exact object ID (and role), regardless of its generation counter.
{"id": "...", "generation": N}--expect FILEOnly when the alias points to that exact object ID at generation N. Catches ABA updates where an alias moved away to another object and then moved back to the original ID at a higher generation (generation: N + 2).
(omit expect, pass "overwrite": true)(explicit overwrite in JSON)Forces the alias to target regardless of its current state and records the move as an explicit overwrite in history.jsonl.

Using idempotency_key for safe retries

If a client or coding agent loses its socket connection before reading the response to alias, publish, submit, import, or replace, retrying without an idempotency key would fail with stale expectation because the alias has already moved to generation 3.

When you include "idempotency_key": "agent-1/app.main/gen-2-to-3" (1 to 256 ASCII characters without control characters, prefixed by your actor or service name), re-sending the identical request payload returns the original success response without applying the move a second time.

3. Moving aliases during single-file lema publish

When publishing a standalone .lema or .lean file outside lema add, lema publish lets you bind aliases atomically as part of the publication:

Shell
# Create new aliases (fails if any exported alias already exists):
lema publish src/Module.lean --prefix Lema --alias app. --expect none

# Update existing aliases by automatically reading their current targets first:
lema publish src/Module.lean --prefix Lema --alias app. --expect current

The server checks all alias expectations before starting the sandboxed Lean elaboration call, so a stale expectation fails in milliseconds at zero kernel cost.

Common refusals

Refusal message containsCause and fix
stale expectation: alias app.main was expected to be new, but it names ...You passed expect: null (--expect none) for an alias that already exists. Use --expect current or pass its current {id, generation}.
stale expectation: alias app.main was expected to name ..., but it names ...Another caller moved app.main since you read it. Run lema call find to inspect the new target before deciding whether to move it.
stale expectation: alias app.main names ... at generation 4, not at the expected generation 2 (it has moved since, and back)The alias moved to another object and back since generation 2. Inspect history.jsonl or find and update generation.
alias target ... is not acceptedThe target.id has not been accepted into this store yet.
idempotency key "..." was used for another requestYou reused an existing idempotency_key with a different request payload. Generate a fresh key for each distinct operation.

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