Work alongside an agent in store workspaces
Give a coding agent a store workspace and a directory of its own, author .lema and .lean modules on both sides at once, watch the store's live activity, land the agent's work with a three-way merge, and recover from a policy refusal and a conflict.
When a coding agent and a person work on the same project, the hard part is rarely the code: it is keeping two streams of edits from trampling each other. If both publish into one shared set of names, whoever runs lema add last wins, and neither can try something without the other seeing it half-done.
Lema solves this inside the store with workspaces. Each workspace is a working state of its own (the modules added to it, the snapshot they make, and the revision it last committed), and all of them live in one store behind one lema serve. You will give an agent called agent-1 a workspace and a directory, let it make two commits while you move main on, watch what it does as it happens, and then bring its work back into main: first through a policy refusal, then through a real conflict.
Before you start
- A
lemathat has store workspaces and the activity stream. The public 0.3.2 release predates them; buildlemafrom source (Build Lema from source) and runlema setup. - Linux on
x86_64, as for the other hands-on tutorials (Set up a machine for Lema). If you are reading on another system, every command’s output is shown below. - About two minutes: each
lema addchecks its module with Lean.
The IDs in the outputs are from our run; yours differ, because each project has its own salt.
1. Create a project and land a first revision
Start with an empty project and one Lean module. lema new creates the store, the project’s default workspace, a local operator actor dev (its token in .lema/token), and the reference main under a first policy that lets dev publish and adopt policies. It also starts the project’s server, which every command below shares.
lema new greeter --empty
cd greetercreated /tmp/lema-tutorial-workspaces/greeter (namespace Greeter, salt greeter-6c2aa8a1, ref main)
store /tmp/lema-tutorial-workspaces/greeter/.lema/store
actor dev (operator), token /tmp/lema-tutorial-workspaces/greeter/.lema/token (mode 0600)
policy 3adc0a772317… (version 1: dev publishes and adopts)
started (pid 31173, log /tmp/lema-tutorial-workspaces/greeter/.lema/serve.log)Write a module with one definition and add it. Adding changes your workspace: Lema checks the module with Lean, makes a new snapshot that includes it, and points default at that snapshot.
namespace Greeter.Text
def greet (n : Nat) : Nat := n + 1
end Greeter.Textlema add Text.lean
lema commit -m 'Add greet.'order: Text.lean
Text.lean: added (da508e230ec2)
added Greeter.Text.greet
workspace default (generation 2)
lock /tmp/lema-tutorial-workspaces/greeter/.lema/lock.json
landed 875dba8350cdlema commit wrote a revision for the workspace’s snapshot and landed it on main. Call this first revision R0: both lines of work below start from it.
2. Register the agent as a store actor
Moving a reference is a governed act, so the store wants to know who does it. A store actor is an identity whose token the store checks. Register agent-1 while the server runs; dev’s token (from .lema/token) authorizes it, and the new token goes straight to a file only you and the agent can read:
lema actor add --id agent-1 --write-token ../agent1.token{
"already_applied": false,
"id": "agent-1",
"operator": false,
"token_file": "/tmp/lema-tutorial-workspaces/greeter/../agent1.token"
}The store keeps only the token’s SHA-256, so a lost token cannot be shown again. Rotating issues a fresh one, and the old one stops working:
lema actor rotate --id agent-1 --write-token ../agent1.token
lema actor list[
{
"id": "agent-1",
"operator": false
},
{
"id": "dev",
"operator": true
}
]3. Give the agent a workspace and a directory
Create agent-1’s workspace from main, attributed to the agent, with a directory of its own. The directory gets the project’s files and a link (.lema/workspace.json), so every lema command run there acts in agent-1 through the same server:
lema workspace create agent-1 --from main --actor agent-1 --dir /tmp/lema-tutorial-workspaces/agent1{
"created": true,
"dir": "/tmp/lema-tutorial-workspaces/agent1",
"generation": 1,
"name": "agent-1",
"workspace": {
"actor": "agent-1",
"head": "875dba8350cd…",
"modules": ["Text.lean"],
"ref": "main",
...
}
}The workspace starts where main is: its head is R0 and it has the one module.
4. Work in both workspaces at once
Now play the agent. In its directory, add a Pythonic .lema module and commit it without publishing, then add a .lean module and commit again. --no-publish stores each revision and advances the workspace’s head, while main stays where it is:
def foo(x: Nat) -> Nat:
xnamespace Greeter.Baz
def baz : Nat := 2
end Greeter.Bazcd ../agent1
lema add Foo.lema
lema commit --no-publish -m 'Add foo.'
lema add Baz.lean
lema commit --no-publish -m 'Add baz.'order: Text.lean, Foo.lema
Foo.lema: added (02a9420885fd)
added Greeter.Foo.foo
module Foo.lema (pythonic): recorded d5a9fe42f3a3
def Greeter.Foo.foo → 04527d69167f (line 1)
workspace agent-1 (generation 2)
committed 50845159648a locally (not published)
order: Text.lean, Foo.lema, Baz.lean
Baz.lean: added (520d2375b235)
added Greeter.Baz.baz
workspace agent-1 (generation 4)
committed 4bfca7151028 locally (not published)Meanwhile, you keep working in the project directory. Add a different module and land it, which moves main past R0:
namespace Greeter.Bar
def bar : Nat := 3
end Greeter.Barcd ../greeter
lema add Bar.lean
lema commit -m 'Add bar.'order: Text.lean, Bar.lean
Bar.lean: added (4da555d5ec23)
added Greeter.Bar.bar
workspace default (generation 6)
landed 9d4178b0adf2Notice the two order: lines: each workspace has its own module list. Neither side’s lema add touched the other’s names.
5. See what each workspace sees
In a project, lema search reads the active workspace’s snapshot, so a name only another workspace has is not there. From the project directory, default has no foo; ask for agent-1’s snapshot and it does:
lema search foo
lema search foo --workspace agent-1{
"matched": 0,
"results": [],
"scope": {"kind": "workspace", "workspace": "default", "generation": 9, ...}
}
{
"matched": 1,
"results": [
{"name": "Greeter.Foo.foo", "kind": "declaration", "score": 100, ...}
],
"scope": {"kind": "workspace", "workspace": "agent-1", "generation": 5, ...}
}lema diff compares any two states, and workspace:NAME names a workspace’s current one. Compared straight against today’s main, the agent’s snapshot adds foo and baz and seems to remove bar:
lema diff main workspace:agent-1from main → revision 9d4178b0adf2 (snapshot 0a1f26789641)
to workspace agent-1 → snapshot 3747301df296
rename 0 · exports 3 · rebind 0 · substitute 0 · evolve 0 · policy 0 · material 9 · obligations 1 · uncarried 1
exports - Greeter.Bar.bar 4441a61a1b1a [requires deprecation]
exports + Greeter.Baz.baz cffc36d49a36 [receipt]
exports + Greeter.Foo.foo 04527d69167f [receipt]The agent never removed bar: it never had it, because bar arrived on main after the agent started. That is why landing does not use this two-way comparison. It merges three ways, from the revision both lines share (R0), so each side’s additions are kept.
6. Watch the store as it works
The project’s server records what it serves: every read, every write, and each job’s progress, with the workspace and actor each was for. Ask for the agent’s jobs (each lema add ran a publish job), and for the writes whose actor’s token the server checked:
lema activity --workspace agent-1 --kind job
lema activity --kind write --actor dev --authenticated-onlystream 00a19eda4d9e4e53: events 1..127
63 job publish #7 queued @agent-1
64 job publish #7 running @agent-1
65 job publish #7 committed @agent-1
86 job publish #8 queued @agent-1
87 job publish #8 running @agent-1
88 job publish #8 committed @agent-1
stream 00a19eda4d9e4e53: events 1..127
2 write ref.create dev (authenticated)
40 write ref.publish @default dev (authenticated) revision 875dba8350cd
44 write actor.add dev (authenticated)
45 write actor.rotate dev (authenticated)
116 write ref.publish @default dev (authenticated) revision 9d4178b0adf2An actor that a request merely names shows as (claimed); only a method that checked a token makes it (authenticated). To watch live, follow the stream. Here, while the agent searched for baz and looked at it:
lema activity --follow --workspace agent-1 278 read search @agent-1 search "baz"
279 read context @agent-1 object cffc36d49a36
280 read neighborhood @agent-1 neighborhood cffc36d49a36 in depth 1Lema Desktop shows the same stream in Agents mode, with the store’s workspaces. Choose Follow on agent-1, and each new search, object or neighborhood it reads opens on your canvas, read in the agent’s own snapshot. Any navigation of your own stops following. Desktop marks its own reads as an observer’s, so watching the agent never adds to the stream you are watching.
7. Land the agent’s work
Landing brings a workspace’s local commits onto its reference. Run it as the agent, with the agent’s token:
cd ../agent1
lema workspace land --actor agent-1 --token-file ../agent1.token -m 'Land agent-1.'lema: main refused the merge revision efd197c268dc…: actor agent-1 may not publish reference main: its active policy (version 1, 3adc0a772317…) lists ["dev"] under publish
the workspace keeps it as its landing (lema workspace show); once what the refusal asks for is done (a review: lema review --revision efd197c268dc…; or a policy that allows it: lema adopt-policy), run lema workspace land againBecause main had moved, Lema merged the agent’s head with main first and then asked main to take the merge. The policy refused: only dev may publish. Nothing is lost. The workspace records where the landing stopped:
lema workspace show"pending_land": {
"attempt_id": "4fe34c82887760f4",
"kind": "merge_candidate",
"candidate_revision": "efd197c268dc…",
"local_revision": "4bfca7151028…",
"expected_ref_revision": "9d4178b0adf2…",
"target_ref": "main"
}The refusal names its remedy. As the operator, adopt a second policy version that also lets agent-1 publish. adopt-policy names the reference state it expects (from lema ref list), so it cannot overwrite a concurrent change:
{"version": 2, "previous": "3adc0a772317…", "publish": ["dev", "agent-1"], "adopt": ["dev"]}cd ../greeter
lema policy create --file ../policy-v2.json
lema adopt-policy --ref main --policy f57fc1def847… \
--expect-revision 9d4178b0adf2… \
--expect-generation 3 --expect-policy-version 1 --actor dev --token-file .lema/token{
"changed": true,
"policy_version": 2,
"previous_policy": {"id": "3adc0a77…", "version": 1},
"ref": "main",
...
}Run the same landing again. It resumes from the recorded step and publishes the merge it had already made:
cd ../agent1
lema workspace land --actor agent-1 --token-file ../agent1.token -m 'Land agent-1.'{
"files": {"updated": true, "written": ["Bar.lean", "lema.json"], ...},
"local_revision": "4bfca7151028…",
"merge": "efd197c268dc…",
"published": true,
"workspace_advanced": true
}The merge’s base is R0, the revision both lines started from (lema call revision.show lists it as base). The project manifests of the two sides merged module by module: bar from main, foo and baz from the agent, with no conflict, and the agent’s directory received Bar.lean. Bring your own directory up to main:
cd ../greeter
lema checkout mainchecked out main (snapshot 7375efbc0301, revision efd197c268dc) into workspace default (generation 10)
wrote Baz.lean, Foo.lema, lema.jsonFoo.lema is written back byte for byte as the agent authored it, from the record the snapshot keeps.
8. Resolve a conflict
Now let both sides change the same thing. The agent makes greet add two and commits locally; you make it add ten and land that:
cd ../agent1
# Text.lean: def greet (n : Nat) : Nat := n + 2
lema add Text.lean
lema commit --no-publish -m 'greet adds two.'
cd ../greeter
# Text.lean: def greet (n : Nat) : Nat := n + 10
lema add Text.lean
lema commit -m 'greet adds ten.'
cd ../agent1
lema workspace land --actor agent-1 --token-file ../agent1.token -m 'Land greet.'lema: merging 7dedd0b47fc5 into main (base efd197c268dc) has 3 unresolved conflict(s): asset .lema/manifest.json (update-update); export Greeter.Text.greet (update-update); source Text.lean (update-update)
the merge is recorded (66a6a039e66e…, lema workspace show); resolve them in a file (merge.create's resolve) and run lema workspace land --resolve FILELema will not guess which greet is right. Its three conflicts all describe the one disagreement from different angles: the export Greeter.Text.greet, the source file Text.lean, and the project manifest that records the module. The recorded merge lists each conflict with both sides’ values, tagged by the revision they come from:
lema call record.get --params '{"kind":"merge","id":"66a6a039e66e…"}'To keep the agent’s version, write a resolution that picks the agent’s side (the side whose parent is the workspace’s head, 7dedd0b47fc5…): its greet export, its manifest asset, and, for the source, the authoring records of the agent’s snapshot (the authoring list of the snapshot record that lema workspace show names):
{
"exports": {
"Greeter.Text.greet": {"id": "9320bd32595e…"}
},
"assets": {
".lema/manifest.json": {"id": "a951a9992056…"}
},
"authoring": [
"02a9420885fd…",
"1d5365dc4fd4…",
...
]
}A resolution may name only keys that are in conflict, so it cannot slip an unrelated edit into the merge. Land again with it:
lema workspace land --actor agent-1 --token-file ../agent1.token --resolve ../resolve.json -m 'Land greet, keeping agent-1.'
cd ../greeter
lema checkout main{"merge": "170231e72063…", "published": true, "workspace_advanced": true, ...}
checked out main (snapshot 814ce221c0b7, revision 170231e72063) into workspace default (generation 15)
wrote Text.leanmain now has the agent’s greet, and your Text.lean says n + 2. Your own change is still in main’s history, as the revision the merge’s other parent points at.
Where to go next
- Semantic version control explains the six terms, why landing merges three ways, and what a scoped read sees.
- Review and merge semantic changes covers waivers, protected references and reviews.
- CLI and Server API list every flag and method used here.
Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility