Running federation
Federation is sync between two Mnemosyne servers: one serves its facts, the other pulls them and resolves conflicts. The federation concept page explains the model. This page covers what an operator can run today.
Beta in docs/STATUS.md. A pull between two servers is tested on real
Postgres over plain HTTP, and a signed pull is tested through a real nginx
(1.27.5) that terminates TLS 1.3 with client certificates.
POST /memory/federation/resolve is tested between two servers as well.
What is implemented
- The routes under
/memory/federation/*, listed below. - The internal
@mnemo-ai/federationpackage: the sync engine, conflict resolution and the signed, sealed envelope. It is not on npm. - Boot wiring in
packages/server/src/boot/federation.ts. There is no feature flag: the routes are mounted whenever the package loads, and the log saysfederation.ready. Without a federation identity the log saysfederation.identity=none: the server can serve peers but cannot pull. - Pulls between two stock servers. Each pull carries the key the serving server issued and a service JWT signed with the puller’s own key.
- A peer registry in Postgres.
POST /memory/federation/peersadds a peer, and themnemo_federation_peertable (migration 0061) keeps it. A peer belongs to the installation, not to a workspace: row-level security hides the table from every other query, workspace transactions included. Without the Postgres adapter the server keeps peers in memory and logsfederation.peer_store=in_memory. - Peer secrets sealed at rest.
apiKeyandsymmetricKeyare stored encrypted with theMNEMO_MASTER_KEYkey ring (AES-256-GCM), bound to the peer and the column. Without a master key, which only a development server allows, they are stored as given and the boot log says so. No route, audit entry or log line returns them. - Sync jobs in Postgres. The
mnemo_federation_sync_jobtable (migration 0063) keeps each job, so a restart does not lose it. While a process runs a job it updates a heartbeat every 30 seconds; apendingorrunningjob with no heartbeat for 2 minutes reads asfailed, with the reason.GET /memory/federation/sync/{jobId}only finds jobs of the caller’s workspace. Without the Postgres adapter, jobs are kept in memory. - Incremental sync. A sync reads the peer page by page from where the previous sync from that peer ended. Edits arrive as new versions, and forgotten and deleted facts as tombstones (section What a sync reads).
- Pulled items in recall.
POST /recallreturns what a sync brought in, marked with the peer it came from (section Pulled items in recall). - A tested TLS proxy setup. nginx terminates TLS 1.3 with client certificates, and the server accepts the certificate it forwards (section Behind nginx).
What is missing
- Discovery. There is none.
GET /memory/federation/peers/pendingalways returns an empty list withdiscovery.enabled: false. - A peer config file. There is no such file: peers are added through the route.
- Push. A peer never writes into this server. The gRPC
FederationSyncRPC answersUnimplementedto every key; a pull is started withPOST /memory/federation/sync. - A review queue for contested items. They are counted, emitted and listed in the job, and an operator settles them by hand.
Two servers, end to end
In this example Larkspur Robotics (larkspur.example) pulls from Acme
(acme.example). For pulls in both directions, run the steps the other way
round as well.
1. Give each server an identity. MNEMO_FED_PEER_ID is the id the other
server registers it under. MNEMO_FEDERATION_PRIVATE_KEY is its Ed25519
private key, PEM, which signs every pull; the other server pins the matching
public key.
# On each server: its federation id and its Ed25519 identity key
openssl genpkey -algorithm ed25519 -out fed_priv.pem
export MNEMO_FED_PEER_ID="acme.example"
export MNEMO_FEDERATION_PRIVATE_KEY="$(cat fed_priv.pem)"
# The public key the other server pins (SPKI DER, base64)
openssl pkey -in fed_priv.pem -pubout -outform DER | base642. On Acme, issue Larkspur a federation key. POST /admin/keys with
"federation": true returns an mk_fed_ key. Its scopes must be exactly
["memory:federation"], and it must be a workspace key, not a test key. Acme
serves the facts of the workspace the key belongs to. Hand the key to
Larkspur.
curl -X POST https://mnemo.acme.example/admin/keys \
-H "Authorization: Bearer mns_live_..." \
-H "Content-Type: application/json" \
-d '{"label": "larkspur pull", "scopes": ["memory:federation"], "federation": true}'3. On Acme, register Larkspur with its id and its public key:
curl -X POST https://mnemo.acme.example/memory/federation/peers \
-H "Authorization: Bearer mns_live_..." \
-H "Content-Type: application/json" \
-d '{"peerId": "larkspur.example", "pinnedCertDer": "<Larkspur public key>", "trustWeight": 0.8}'4. On Larkspur, register Acme with its id, its public key, its url and
the mk_fed_ key as apiKey. signingPublicKey and symmetricKey are only
needed for the sealed envelope (see below):
curl -X POST https://mnemo.larkspur.example/memory/federation/peers \
-H "Authorization: Bearer mns_live_..." \
-H "Content-Type: application/json" \
-d '{
"peerId": "acme.example",
"url": "https://mnemo.acme.example",
"pinnedCertDer": "<Acme public key>",
"trustWeight": 0.8,
"apiKey": "mk_fed_...",
"signingPublicKey": "-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----\n",
"symmetricKey": "<base64 of 32 bytes>"
}'5. On Larkspur, start the pull with a local key that holds
memory:federation, and poll the job:
curl -X POST https://mnemo.larkspur.example/memory/federation/sync \
-H "Authorization: Bearer mns_live_..." \
-H "Content-Type: application/json" \
-d '{"peerId": "acme.example"}'
curl https://mnemo.larkspur.example/memory/federation/sync/JOB_ID \
-H "Authorization: Bearer mns_live_..."POST /memory/federation/sync is a local action. A peer’s mk_fed_ key is
refused with 403, so a peer cannot make a server pull. The items land in
the workspace of the key that started the sync. The job ends failed, with
the reason, when the pull fails, when the server has no identity, or when the
peer record has no url or apiKey. The job is kept in Postgres, so a
restart does not lose it; one a restart cut short reads as failed.
What a sync reads
A sync asks the peer for pages of up to 200 facts, passing each page’s
nextCursor as the next cursor, until the peer answers nextCursor: null.
- It goes on from the last sync. The job records its cursor in
outcome.cursor, also when it fails midway, and the next sync from the same peer into the same workspace starts there. The first sync starts from the peer’s first fact. - It is bounded. One job reads at most 50 pages and runs at most 5
minutes. Past that it stops with
outcome.more: true, and the next sync goes on from its cursor. A page with an item that failed to apply stops the sync before that page, so the next sync retries it. - The outcome stays small.
outcomekeepsitemsProcessed, a count per result inoutcomesand the first 100 per-itemresults. - Edits, forgets and deletes travel. The peer serves every fact of the
workspace. An active fact carries
version, its revision count, which every edit raises. A forgotten or merged fact is a tombstone: the sameremoteIdandversion,deletedAtwith the day only, and an emptypayload. A fact deleted outright (DELETE /facts/{id}/hard,DELETE /facts/purge) leaves no row, so its tombstone, withversion0, comes from the deletion log (migration 0068). It still archives the copy. A fact deleted before that migration reaches no peer. - Tombstones only go to a peer that may hold the fact. The peer serves
one only for a fact created before the previous sync ended, so a first
sync gets none. It does not take the caller’s word for where that was: per
workspace and per authenticated peer, it keeps the newest creation time
among the facts it served that peer (migration 0070), and bounds
knownCursorwith it. AknownCursorin the future reveals nothing more, and a peer it never served gets no tombstones. - A malformed cursor starts over. A
cursorthat is not in the format the server writes reads from the beginning, instead of failing. - Each request is capped. A request times out after 30 seconds, and a response larger than 32 MiB is refused.
GET /memory/federation/items answers like this:
{
"items": [
{
"remoteId": "<fact id>",
"version": 2,
"peerDomain": "<host name of the request URL>",
"payload": {
"statement": "Acme ships on Thursdays",
"kind": "objective_fact",
"attribution": "federation_received",
"sensitivity": "internal",
"validFrom": "2026-11-02T10:00:00.000Z",
"validTo": null
}
}
],
"nextCursor": "<opaque cursor>"
}The puller ignores peerDomain: it stores each item under the peerId it
registered the peer with, and only that exact id counts as the item’s origin.
Migration 0066 renamed, once, the copies older servers had stored under the
peer’s host name, where exactly one registered peer had that host in its
url. The rest count as another origin, so no peer can update or tombstone
them.
Adding a peer
A peer exists only once an operator adds it with a federation:admin key, as
in steps 3 and 4.
| Field | Required | Notes |
|---|---|---|
peerId | yes | The peer’s MNEMO_FED_PEER_ID: a domain or a UUID, letters, digits, ., _, : and -, up to 253 characters. |
pinnedCertDer | yes | The peer’s public key, SPKI DER in base64, up to 16 KiB. |
trustWeight | yes | The peer’s local voting weight, 0 to 1. |
enabled | no | Default true. |
url | no | The peer’s base URL, https (plain http only with MNEMO_FED_INSECURE_TRANSPORT). Needed to pull from it. |
apiKey | no | The mk_fed_ key the peer issued to this server. Needed to pull from it. |
signingPublicKey | no | The peer’s Ed25519 public key, PEM, to verify its envelope. Sent together with symmetricKey. |
symmetricKey | no | The shared key, base64 of 32 bytes. Sent together with signingPublicKey. |
The body is strict: an unknown field answers 400. approvedAt is set to
the server’s time, and the answer is 201 with created, peerId and
approvedAt. A peerId that already exists answers 409. The creation is
audited as create on federation_peer, with the key’s fingerprint. The
TypeScript client calls it createFederationPeer.
GET /memory/federation/peers shows each peer without its secrets: it says
whether a pull key and a symmetric key are set.
{
"peers": [
{
"peerId": "acme.example",
"certFingerprint": "<SHA-256 of the pinned key, hex>",
"trustWeight": 0.8,
"approvedAt": "2026-11-19T10:00:00.000Z",
"enabled": true,
"url": "https://mnemo.acme.example",
"signingPublicKey": "-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----\n",
"hasApiKey": true,
"hasSymmetricKey": true
}
]
}Routes
| Method and path | Scope | Purpose |
|---|---|---|
GET /memory/federation/peers | federation:admin | List known peers, without their secrets. |
POST /memory/federation/peers | federation:admin | Add a peer. The only way a peer is created. |
GET /memory/federation/peers/pending | federation:admin | Always empty today. |
POST /memory/federation/peers/{id}/approve | federation:admin | Replace an existing peer’s pinned certificate. |
POST /memory/federation/peers/{id}/approve-key | federation:admin | Approve a key rotation for an existing peer. |
DELETE /memory/federation/peers/{id} | federation:admin | Remove a peer. |
GET /memory/federation/items | memory:federation | Serve the facts of the key’s workspace to a peer, tombstones included. Peer gate. |
POST /memory/federation/sync | memory:federation | Pull from a peer into the caller’s workspace. Answers 202 with a jobId. Local key only. |
GET /memory/federation/sync/{jobId} | memory:federation | Poll a sync job of the caller’s workspace: pending, running, done or failed. |
POST /memory/federation/resolve | memory:federation | Resolve a conflict between versions. Peer gate. |
The two approve routes act only on a peer that already exists; for any
other id they answer 422 with unknown_peer.
Both approve routes take { "newCertDer": "<base64 DER>" }. POST /memory/federation/sync takes { "peerId": "...", "conflictResolution": "..." }, where conflictResolution is optional and one of theirs_wins,
ours_wins, manual or merge. GET /memory/federation/items takes
since (an ISO timestamp), limit (default 100, at most 200), cursor (the
nextCursor of the previous page) and knownCursor (where the caller’s
previous sync ended, which lets the peer send tombstones, bounded by what it
served the caller).
What a pull sends
| Header | Value |
|---|---|
Authorization | Bearer and the mk_fed_ key the serving server issued. |
x-mnemo-peer-id | The puller’s MNEMO_FED_PEER_ID. |
x-mnemo-client-cert | The puller’s public key, SPKI DER in base64url. |
x-mnemo-service-jwt | An EdDSA JWT signed with the puller’s key: iss is its id, aud the serving server’s id, a fresh jti, valid for 60 seconds. |
The peer gate
The routes marked “peer gate” check more than the scope. No header is trusted on its own. A request passes only when all of these hold:
- The key in
Authorizationis a federation key (prefixmk_fed_). - The transport is TLS 1.3 (see below).
x-mnemo-peer-idnames a known, enabled peer.- The presented key equals the one pinned for that peer. A forwarded X.509
certificate is compared by its public key. A mismatch is written to the
audit log as
federation_cert_pin_mismatch. x-mnemo-service-jwtverifies against the pinned key, which proves the caller holds the private key. Itsissis the peer, itsaudis this server (MNEMO_FED_PEER_ID, orMNEMO_FED_AUDIENCEwhen set), it has not expired, and itsjtihas not been seen before.- If the JWT names workspaces, the key’s workspace is one of them.
Replay protection for the jti uses Redis when MNEMO_REDIS_URL is set, so
it holds across replicas. Without Redis it is per process.
Transport
The serving server requires TLS 1.3 and never takes the client’s word for it.
| Variable | Description |
|---|---|
| (none) | The server terminates TLS itself and reads the version from its own socket. |
MNEMO_FED_TLS_PROXY | true behind a TLS-terminating proxy: the version is read from x-mnemo-tls-protocol, which the proxy must overwrite on every request (nginx: $ssl_protocol) and drop when a client sends it. |
MNEMO_TRUSTED_PROXY_IP | The address the proxy connects from, as the server’s socket sees it. When set, x-mnemo-tls-protocol and x-mnemo-client-cert count only on connections from that address; X-Forwarded-For is never read. The puller sends its public key in x-mnemo-client-cert, so the proxy passes it on or replaces it with the verified client certificate (nginx: $ssl_client_escaped_cert, URL-encoded PEM; base64url DER is accepted too). With MNEMO_FED_TLS_PROXY=true and no address, the headers count from any connection and the boot log warns. |
MNEMO_FED_INSECURE_TRANSPORT | true accepts peers over plain HTTP, and http peer URLs. For development only: the server refuses to start with it in production. Every other check still runs. |
With MNEMO_FED_TLS_PROXY=true, set MNEMO_TRUSTED_PROXY_IP and keep the
server reachable only through the proxy. Without the address, a client that
reaches the server directly can forge x-mnemo-tls-protocol and
x-mnemo-client-cert.
Behind nginx
This configuration is tested against nginx:1.27.5. nginx accepts only TLS
1.3, verifies the client certificate against the CA that issues your peers’
certificates, and overwrites both headers on every request:
server {
listen 443 ssl;
ssl_protocols TLSv1.3;
ssl_certificate /etc/ssl/server.crt;
ssl_certificate_key /etc/ssl/server.key;
# The CA that issues your peers' client certificates.
ssl_client_certificate /etc/ssl/peers-ca.crt;
ssl_verify_client on;
location / {
proxy_pass http://10.0.0.3:3000;
# Both replace whatever the client sent.
proxy_set_header X-Mnemo-Tls-Protocol $ssl_protocol;
proxy_set_header X-Mnemo-Client-Cert $ssl_client_escaped_cert;
}
}On the Mnemosyne server behind it, set MNEMO_FED_TLS_PROXY=true and
MNEMO_TRUSTED_PROXY_IP=10.0.0.2, the address nginx connects from. The server
compares the public key of the forwarded certificate with the peer’s pinned
key and still requires the signed service JWT. So the peer’s client
certificate is issued for its federation key: with the key from step 1 in
fed_priv.pem, the CA signs a request for it:
openssl req -new -key fed_priv.pem -subj /CN=larkspur.example -out fed.csr
openssl x509 -req -in fed.csr -CA peers-ca.crt -CAkey peers-ca.key -days 365 -out fed.crtA TLS 1.2 handshake, a request without a client certificate, a certificate for an unpinned key and a direct connection that writes the headers itself are all refused.
The puller’s side. For its own pulls (POST /memory/federation/sync)
to get through a peer’s nginx, Larkspur presents that certificate. Point the
server at the files; MNEMO_FED_CA_FILE is needed only when the peer’s
server certificate is not from a public CA:
MNEMO_FED_CLIENT_CERT_FILE=/etc/mnemo/fed.crt
MNEMO_FED_CLIENT_KEY_FILE=/etc/mnemo/fed_priv.pem
MNEMO_FED_CA_FILE=/etc/mnemo/peer-server-ca.crtSet both certificate variables or neither. A file that cannot be read, or a certificate and key that do not match, stop the server at startup.
Signing and sealing payloads
MNEMO_FEDERATION_PRIVATE_KEY, the identity key from step 1, also signs what
the server serves once a shared key is set. With both variables, the serving
side signs and encrypts its items:
# Ed25519 private key (PEM): signs the envelope
openssl genpkey -algorithm ed25519 -out fed_priv.pem
export MNEMO_FEDERATION_PRIVATE_KEY="$(cat fed_priv.pem)"
# Shared 32-byte key (base64): encrypts each item
export MNEMO_FEDERATION_SYMMETRIC_KEY="$(openssl rand -base64 32)"
# Public key, for the servers that pull from this one
openssl pkey -in fed_priv.pem -pubout -out fed_pub.pemWith both set, GET /memory/federation/items returns an envelope with alg: "ed25519+aes-256-gcm": an Ed25519 signature and items sealed with
AES-256-GCM. If a key is invalid, the server logs
federation.serve_crypto_init_failed and serves plaintext.
On the pulling side, a peer record that holds the peer’s signingPublicKey
and the shared symmetricKey makes verification and decryption mandatory: a
plaintext or tampered response is rejected. MNEMO_FED_ALLOW_PLAINTEXT=true
accepts plaintext during a migration.
How incoming items are treated
These rules are enforced inside the sync engine, whatever the sender claims:
- Every incoming item is stored with the attribution
federation_received. - Trust weights come from the local peer record, never from the peer.
- An item is kept apart from the workspace’s own memory, in
mnemo_federated_item. Recall reads it as one more source. - A copy that came from the same peer is that peer’s own item: a higher
versionreplaces it, the same or a lower one changes nothing. No conflict is resolved. - A tombstone archives the local copy only when the sender is the copy’s
origin. Another peer’s tombstone is rejected
(
rejected_foreign_tombstone), and one for an item this server does not hold is ignored, so a peer cannot block an id it does not own. An archived id is never brought back. - An item more than 100 versions ahead of a copy from another peer is rejected.
- A copy from another peer goes to the strategy of the sync, or, with none,
to the trust-anchor rule. Only a verdict
with confidence above 0.7 decides. Anything else is contested: the local
copy stays,
mnemo_federation_conflicts_totalgoes up, afederation.conflictevent goes out on the WebSocket channelfederation.syncand as a webhook, and the job lists the item.
POST /memory/federation/resolve accepts either { "itemA": ..., "itemB": ... }, resolved by last write wins, or { "items": [...] }, resolved by a
weighted supermajority vote that can end in no_consensus with a
recommendation of human review. In the vote, each claim weighs what the
local record of its peerId says, never a weight sent in the body. With last
write wins, the trustScore of a federation_received winner is capped at
20. Both paths are tested between two servers. See
trust-anchor consensus.
Pulled items in recall
POST /recall, and the MCP tool memory_recall through it, also returns what
a sync pulled into the caller’s workspace. Each such hit carries a mark:
{
"id": "<local id>",
"kind": "fact",
"content": "Acme ships on Thursdays",
"federation": { "peer": "acme.example", "trustScore": 20 }
}peeris the id this server registered the peer under.trustScoreis that offederation_received, the lowest.- Pulled items have no embedding, so they match by words only. The strict
semanticmode never returns them. - They come after the local hits, unless nothing local was relevant. The
page limit applies to the whole page, and they are left out of later pages
and of filters they cannot meet (
scope,scopeRef,actorId, a personal recall,kindswithoutfact). - The tier is the
sensitivitythe peer sent, and an unknown value reads asrestricted. The caller’s ceiling applies as to any hit, and so doasOfand the validity window. - The
idis local andGET /facts/{id}does not find it, so the hit is never truncated. Recalling it changes nothing: no recall log, worth, hot tier or cache. - Only
POST /recallreturns them, because only its hit can carry the mark. gRPCRecall,/recall/unified,/recall/explainand anticipation do not.
Monitoring
Three Prometheus series cover federation, labelled by peer where it applies.
The conflicts counter goes up for each contested item of a sync and for the
last-write-wins form of /resolve:
mnemo_federation_lag_seconds{peer}
mnemo_federation_sync_duration_seconds{peer}
mnemo_federation_conflicts_totalThe federation.json Grafana dashboard in docker/scripts/dashboards/
charts them. See Monitoring.