import { next as A } from '@automerge/automerge'; import { type DocumentId } from '@automerge/automerge-repo'; import { type Context } from '@dxos/context'; import { type DatabaseDirectory } from '@dxos/echo-protocol'; import { type EntityMeta, type Referrer } from '@dxos/index-core'; import { type EntityId, type SpaceId } from '@dxos/keys'; /** * The document surface a merge needs — structurally satisfied by the host's `DocumentLease` * (whose disposal the merge honors) and by a bare `DocHandle` in tests. */ export type MergeDocumentRef = { readonly documentId: DocumentId; doc(): A.Doc; change(callback: A.ChangeFn): void; [Symbol.dispose]?: () => void; }; export type ConvergenceKeyMergerDeps = { loadDoc: (ctx: Context, documentId: DocumentId, opts?: { timeout?: number; fetchFromNetwork?: boolean; }) => Promise; /** * Persist a document's pending changes durably. Winner and loser live in different documents * with no cross-document write ordering, so the merge flushes the winner's folded data before * writing any tombstone whose watermark claims that fold happened. */ flushDoc: (ctx: Context, documentId: DocumentId) => Promise; /** * Detection point lookup (`IndexEngine.queryByConvergenceKeys`), already bound to the host * runtime. Promise-typed so tests can supply rows without a database. */ queryByConvergenceKeys: (spaceId: SpaceId, convergenceKeys: readonly string[]) => Promise; /** * Referrers of one entity (`IndexEngine.queryReferrers`) — the point lookup behind reference * rewriting. Index rows are derived state; every hit is re-verified against the document before * anything is written. */ queryReferrers: (spaceId: SpaceId, targetId: EntityId) => Promise; }; export type ConvergenceKeyMergeResult = { /** Duplicate groups that required writes (a merge or a late-edit fold). */ mergedGroups: number; /** Keys serviced to completion, per space — safe to clear from the durable intent log. */ serviced: Map>; }; /** * Merges convergence-key duplicates surfaced by the indexing intent log — the worker-side trigger. * * The merge operates on the raw document structures via the storage-independent core in * `@dxos/echo/internal`; the writes replicate to clients like any other change, and the * `documentsSaved` event re-indexes the tombstones, which is what removes the losers from query * results everywhere. */ export declare class ConvergenceKeyMerger { #private; constructor(deps: ConvergenceKeyMergerDeps); /** * Merge every duplicate group among the given pending keys. * * Runs after the index engine processes changed documents, which is the earliest a duplicate * can exist on this device: duplicates are born from replication, and a replicated write is * exactly what lands here. Detection is a point lookup on only the pending keys, so the cost * is proportional to writes that carry one — nil for everything else — and no client, query, * or full scan is involved. * * Already-redirected entities that re-index — a straggler peer's late edits replicating onto a * tombstone, or a loser resurrected by `db.add` — are serviced too: their post-merge edits are * folded into the winner and the tombstone is re-asserted, which is what makes `mergedInto` * sticky and the convergence argument hold without any client-side pass. * * Failure containment: a key whose group throws is reported un-serviced (it stays in the intent * log and the next pass retries it) without blocking the rest of the batch. A group member whose * document cannot be loaded is merged around — the merge over the loadable subset is safe, and * the missing document's eventual arrival is itself an indexed write that re-presents the key. */ mergeDuplicates(ctx: Context, convergenceKeys: ReadonlyMap>): Promise; /** * Merge one convergence-key group: the live candidates fold into the minimum-id winner, and * already-redirected members get their late edits folded and their tombstones re-asserted. * * Public as the unit under test — `mergeDuplicates` adds detection and failure containment * around it. */ mergeGroup(ctx: Context, spaceId: SpaceId, convergenceKey: string, group: readonly { objectId: EntityId; documentId: string; }[]): Promise; } //# sourceMappingURL=convergence-key-merge.d.ts.map