Six weeks on Automerge and Keyhive: what local-first taught me
In April 2026 I heard about local-first for the first time, in a livestream (in Ukrainian). The idea stayed with me: an app that keeps everything on your device and uses the network only to sync with other devices.
A lot of what we put up with because everything goes through a server simply goes away:
- you can work offline: changes are saved on the device and sent when the network is back, instead of "you are offline, try again later";
- your data stays on your own devices, and a server in the middle, if there is one, only passes changes along;
- changes made at the same time on different devices merge by themselves, and none get lost;
- every change is kept, so, as in Git, you can go back to an old version, see what changed between two of them, or branch off.
Two libraries do the hard part. Automerge syncs the data, Keyhive decides who may read and change it. zerno is my thin layer over the two, so an app can ask for "a document that only these people can read" without wiring both of them up by hand.
Step one: Automerge, or two devices without a referee
The first question is how two devices agree on anything without a server in charge. In short: Automerge is a library for data that several devices can change at once. It comes from Ink & Switch, a research lab working on local-first software. Every device keeps a full copy of a document. A change does not just overwrite a value: Automerge saves the change itself (what changed, on which device, after which earlier changes), and the document is what you get by applying all of them. That is what makes merging possible. When two devices meet again, each sends the changes the other is missing, both apply them, and they end up with the same document, with no server deciding who was right. They do not even need to see each other directly: a sync relay in the middle simply passes the changes along.
There are other libraries like it. I took Automerge, with one rule: if it does not pay off, I can always move to something else.
The smallest demo here does what zerno-repl does: two devices and one document with a title. Change it on each, take one or both offline, and bring them back.
The interactive demo needs JavaScript.
Step two: Keyhive, or access without a gatekeeper
Access came up with my next project, a board a bit like Miro. As soon as there was a board to share, the question was there: can I let one person view it and another edit it, without a central server that decides? In a client-server app that server is the authority: it checks every request against its own database. Here there is nobody in the middle to ask.
Automerge does exactly what it was built for: it merges changes. It knows which device made a change, but not who stands behind that device or whether they were allowed to make it. Anyone who knows a document's ID can read it and write into it. Ink & Switch call this security through obscurity.
That is how I found Keyhive, from the same lab. It adds access on top, and does it with cryptography instead of a server. Every device, person or group is a public key, and the owner of a document signs who may do what with it. Edit access is such a signed chain, and any device can check it by itself, so a change from someone without it is not applied anywhere. Read access is a key: documents are encrypted, and only those who may read get the keys. The sync relay only gets the right to pull the encrypted bytes, it cannot read them. Taking access away changes the key: the removed device keeps what it could already read, but nothing written after. Here A owns a document: share it with B or C, read or edit, take it away again, and watch who can read what.
The interactive demo needs JavaScript.
Step three: zerno, or eating my own dog food
Automerge and Keyhive are two separate packages that are meant to be used together, and wiring them up takes a lot of code in every app. So zerno started as a thin API over both: identities, documents, access. The API of the libraries underneath keeps changing, and zerno's changes with it.
To find the weak spots of a library, I write apps on it myself (dogfooding). So there are three examples, each bigger than the last:
-
zerno-repl, about 350 lines: a prompt with commands likedoc open,doc title setanddoc grant. You change one value in one terminal and watch it arrive in another. -
zerno-chat, about 1,300 lines: a chat in the terminal, built with Ink (React for the terminal). This is where I learned how to lay data out in Automerge: what should be one document and what should be many. -
zerno-web, about 6,300 lines: the same chat in the browser, with React and Vite, and a much better architecture. Try the demo.
zerno-web is also the hardest one to write. It has the most moving parts, and it runs on alpha software.
No tables, only links
Every app stands on the same layers. Each one only talks to the one below it:
-
zerno-repl,zerno-chat,zerno-webscreens, and services that know what a channel is -
zerno-reacthooks such asuseDocumentanduseMembers -
zerno-coreidentities, documents, access, groups, invites -
@automerge/
automerge-repo + @keyhive/ keyhivesyncing documents, encrypting them, deciding who may read them -
@automerge/
automerge-subductionpasses encrypted changes between devices, cannot read them
zerno-core is about 700 lines. zerno-react has the same hooks as @automerge/react, with the same names and arguments (useDocument, useDocuments, useDocHandle), so code written for plain Automerge moves over as it is. What they add is Keyhive: a document that arrived before its keys is retried until it opens, and useAccess and useMembers redraw when access changes.
The data model took me three tries. First, in zerno-chat, every message was its own document. It is the obvious way, and Keyhive choked on it: every new document means new keys and new signatures for every member, so each message set off a whole round of cryptography. Then each member got one message list per channel, created once: as many lists as members, and a message is just an entry in its author's list. After that the model barely changed: zerno-web added id and editedAt, and to make it faster the text of a message became an ImmutableString. A plain string in Automerge is collaborative text that tracks every character so that edits can merge, and a message nobody edits together does not need that.
There are no tables and no foreign keys here. A document simply holds the URLs of other documents: a workspace lists its channels, a channel points to its group, its phonebook and one message list per member, and the group lists the channel's documents back. Every member's workspace points to the same channel, so it is a graph rather than a tree, and every document in it has its own access. Here is one channel, seen from my workspace:
-
workspace my list of channels, only I can read it
-
channel its name and links to everything below
- group who the members are and what they get
- phonebook the members' contact cards, to give access to whoever joins later
-
messages one list per member
-
A's onlyAcan write here -
B's onlyBcan write here
-
-
channel its name and links to everything below
The list per member is the part I like most, and not only because it is fast. Nobody can change or delete someone else's message, because they cannot write into that document at all. That rule is not a check in the app that someone could skip: it is who holds the key to edit. The screen merges the lists by createdAt, a time each message stores, because Automerge itself does not know when a change happened.
Too good to be true?
Keyhive is an alpha, and it shows the moment you use it for something real. Some things I simply could not build. Others I could build only after patching the libraries underneath: four patches, about 800 lines, kept with pnpm patch instead of editing anything by hand. One example: a document can arrive before the keys to decrypt it. The library gave up on the first try, and a document that once failed to open stayed failed forever, even after the keys arrived. The patch makes it retry, and forget the failed attempt.
These patches are temporary. They mostly fill in what simply is not written yet.
Step four: groups, or where it all falls apart
A chat needs groups: a channel with members, where everyone sees everyone's messages.
At first I did it by hand: to send a message to a channel, I went through its members and decided whom to give access to. It sort of worked, but there had to be something more sensible. That is how I found Keyhive's own groups: give access to the group once, and everyone in it gets it, including whoever joins later.
They turned out not to be finished. My test ran three devices in one process against the real sync relay: A adds B, B adds C, and C tries to read A's document. The same run, from scratch, again and again, passed about half the time, at random. C got the permission, but sometimes never got a key to decrypt with. I patched the gap I found, and it was still a coin toss. Keyhive's authors say themselves that groups are the most complex part of the implementation, and that the algorithm is being reworked and simplified.
After that I tried workarounds on top to keep the group interface working. Looking back, I would rather drop groups until they are ready. And the problem stays anyway:
Acreates a channel and addsB. Both see everything.BaddsC.CseesB's messages, but notA's.
B can hand out the keys to their own messages, but not to A's: they were never B's to give. Underneath it is cryptography doing exactly its job. For the person in the chat it is a channel where some messages never appear.
I also started on invite links: the owner makes a link once, and whoever opens it joins with the owner's permission, even if the owner is offline. I am not sure yet how I want to build them, so for now I am experimenting on my own machine, and they are not in the demo. They run into the same transitive access, which is broken right now: for C to get in through a link, A has to sign a whole set of keys for read or for write, and somewhere in there it falls apart. And even working, they would get around the problem, not solve it.
This is the last and hardest demo: the whole chat on three devices, each showing the channel the way its owner sees it. Underneath, every member still has a list of their own, put in order by createdAt. Write, take devices offline, then add C through B and see what C cannot read. Then try the invite link from A, even with A offline. Here it works the way it is meant to; in zerno it does not yet.
The interactive demo needs JavaScript.
So, does it work?
The demo runs, but it is still more of an experiment than a chat you could rely on. The libraries underneath are young and change fast, and that is exactly what makes this an interesting area to explore. Local-first is still the idea I keep coming back to, and I am going to keep building on it.