Vault: merge and tombstones
The exact rules src/vault/merge.ts uses to merge two vault states and to compare them: principals as keys, tombstones and resurrection, labels, delegations, unknown fields, bytewise ordering.

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

normalizeState(state, source)

A NormalizedState: accounts in a Map keyed by principal, tombstones, notification URL, unknown top-level fields

mergeStates(local, remote, localVersion, remoteVersion, log?)

{merged, changedFromLocal}

mergeNormalizedStates(...)

the same, from normalized inputs

statesEqual(left, right)

semantic equality, never a comparison of serialized bytes

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

A tombstone is created only in the store, and only for an explicit removal marker written by Identity. The merge never invents a tombstone; it only propagates and expires them by rules 6 and 7.

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:

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).

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime