src/vault/merge.ts decides what a vault looks like after two devices changed it. It is "a line-for-line port of the Go implementation" in backend/storage/vault/vault.go, and its header explains the stakes: the merge "is what keeps every device's vault convergent, and any divergence makes devices oscillate or silently lose identities". Each helper maps to one Go function.
Entry points
Function | Returns |
|---|---|
A | |
|
|
the same, from normalized inputs | |
semantic equality, never a comparison of serialized bytes |
Callers are the connect ceremony and the sync pass.
Normalization rules
A seed that is not 32 bytes throws, naming the side (local or remote) and the index.
An account's key is its principal, the account id derived from the seed. Never the display name: "Two accounts may share a display name."
The name is trimmed and kept only if it matches ^[a-zA-Z0-9_-]+$; otherwise it becomes the principal.
Delegations are deduplicated by capability CID (rule 13) and sorted by that key.
Two entries with one principal inside a single state are resolved: with equal seeds their delegations are unioned and the tiebreak winner (rule 9) keeps the record; with different seeds the tiebreak winner replaces the other.
Merge rules
Tombstones union. For each account id the merged tombstone is the larger of the local and remote times.
Deletion or resurrection. Let latestLiveCreateTime be the largest createTime among the local and remote copies. If the winning tombstone is greater than 0 and greater than or equal to latestLiveCreateTime, the account is deleted and the tombstone kept. Otherwise the tombstone is removed and the account lives. A tombstone therefore only kills a copy created at or before it; a later re-creation survives.
Same account on both sides, same seed. Merge field by field (mergeMatchingAccounts): createTime is the maximum, delegations are unioned, unknown fields are the remote's overlaid by the local's, and the name follows rule 10.
Same principal, different seed. One whole account wins the tiebreak (accountWinsByTiebreak): later createTime, then larger principal, then larger name.
Labels are per device. The local label stays. The remote label is taken only when this device has none (its name is the principal). Before adoption, labelFreeFor checks that no other local account holds the label; if one does, it tries label-<last 8 characters of the principal>, then label-<principal>.
Only on one side. The account is copied as is.
Notification server URL. The side with the higher version wins. On a tie, local wins if it is set or if remote is empty.
Delegations. Keyed by the unpadded base64url of the DAG-CBOR encoding of the capability CID. A duplicate is resolved by later createTime, then larger clientId, then larger deviceType. A delegation without a capability CID throws.
Unknown fields. Top-level extras merge as remote overlaid by local. They are preserved through the state codec.
Ordering. Account ids are processed in bytewise order; merged accounts are sorted by name, then by account id. deletedAccounts is written only when non-empty.
Result flag. changedFromLocal is true when the merged state, normalized again, differs from the local one. The caller uses it to decide whether to rewrite the local vault.
Equality rules
statesEqual normalizes both sides and compares notification URL, tombstones, unknown fields, account count and, per principal, seed bytes plus the canonical CBOR of the account (known fields, extras, delegations).
One adjustment runs first, alignDifferingLabels: two different labels for the same account compare equal. The comment explains the bug this prevents: counting them as a difference "made two devices rewrite the remote vault on every sync, each with its own label". A label against no label still differs, "so a label reaches the remote and the devices that have none".
Value comparisons go through cborValuesEqual, which encodes both sides with encodeBlob and compares bytes, returning false if encoding fails.
Bytewise comparison
Every string comparison uses compareStringsBytewise, a UTF-8 byte comparison matching Go's < and >. The comment is emphatic: "NEVER localeCompare: base58 principals are mixed-case and ICU collation disagrees with Go's byte order."
How deletion reaches here
Logging
VAULT_MERGE_LOG_PREFIX is ๐ VAULT-MERGE, the marker the Go daemon uses, and a defaultLog that prints with it is defined. In this snapshot the log parameter defaults to a no-op and no caller passes a logger, so merges are silent in the app (verified by reading the callers; the header comment says the default is console.log).
Turn budget
The merge is synchronous and derives account ids repeatedly while normalizing, sorting and comparing. Callers run warmAccountIds first so each derivation is a cache hit; see Crypto: vault envelope and state codec and The 100 ms turn budget.
Tests
In vault.test.ts:
unknown fields, notifications and deletion or resurrection ordering: a tombstone at 20 deletes an account created at 20 and spares one created at 21,
a key name is a per-device label: two labels never rewrite the vault, a label fills in where there is none,
a label taken from the remote never duplicates a local name: the suffix rule.
The real vault suite checks convergence against the repository's real vault server and web vault.
User stories
The code described here exists to deliver these stories. Each story lists this page in its Trace (traceability).
US-ID-04 See and share my account ID, and name my key on this device: Key names as per-device labels when a vault is connected
US-ID-06 Remove an identity from this device: How a deletion spreads to other devices and when a re-created identity survives
US-VAULT-01 Connect my vault to bring my identities to this phone: How local identities merge into the remote vault on connect
US-VAULT-02 Keep my identities in sync across devices: The merge rules: deletions, re-creation, per-device key names, unknown fields
US-VAULT-04 Trust that my keys stay safe: A deletion is only ever propagated, never invented; re-creation survives
US-TEAM-02 Trust that a change is safe: The removal marker rule the regression tests protect
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime