@powerhousedao/powerhouse-vetra-packages 6.2.0-dev.1 → 6.2.0-dev.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/assets/{entry-Bzani6_n.js → entry-D6VwthNk.js} +2 -2
- package/dist/browser/assets/{projection-entry-Bpu-8SnI.js → projection-entry-CemvPRT8.js} +2 -2
- package/dist/browser/{connect-ZnLjvmRt.js → connect-BMrgEG7X.js} +4 -4
- package/dist/browser/{connect-ZnLjvmRt.js.map → connect-BMrgEG7X.js.map} +1 -1
- package/dist/browser/{dist-C1nRM9t2.js → dist-B6zWOl4d.js} +44 -30
- package/dist/browser/dist-B6zWOl4d.js.map +1 -0
- package/dist/browser/{dist-DLFI75Zd.js → dist-DPeHVSov.js} +3 -3
- package/dist/browser/dist-DPeHVSov.js.map +1 -0
- package/dist/browser/{document-drive-oqv68jDU.js → document-drive-sTKoi6L4.js} +314 -9
- package/dist/browser/document-drive-sTKoi6L4.js.map +1 -0
- package/dist/browser/document-models/index.js +1 -1
- package/dist/browser/{editor-CoTq6fqT.js → editor-Clp9-uJE.js} +6 -6
- package/dist/browser/{editor-CoTq6fqT.js.map → editor-Clp9-uJE.js.map} +1 -1
- package/dist/browser/{editor-DC3bigBu.js → editor-CzsQw1pE.js} +5 -5
- package/dist/browser/{editor-DC3bigBu.js.map → editor-CzsQw1pE.js.map} +1 -1
- package/dist/browser/editors/document-model-editor/module.js +1 -1
- package/dist/browser/editors/generic-drive-explorer/index.js +4 -4
- package/dist/browser/editors/generic-drive-explorer/module.js +1 -1
- package/dist/browser/{folder-view-DmYBf2pP.js → folder-view-D9jBl4kJ.js} +3 -3
- package/dist/browser/{folder-view-DmYBf2pP.js.map → folder-view-D9jBl4kJ.js.map} +1 -1
- package/dist/browser/{graphql-editor-CiGd_Li7.js → graphql-editor-8bWqxqUN.js} +3 -3
- package/dist/browser/{graphql-editor-CiGd_Li7.js.map → graphql-editor-8bWqxqUN.js.map} +1 -1
- package/dist/browser/index.js +2 -2
- package/dist/browser/{json-editor-sEqd5piI.js → json-editor-DlvyzQQO.js} +3 -3
- package/dist/browser/{json-editor-sEqd5piI.js.map → json-editor-DlvyzQQO.js.map} +1 -1
- package/dist/browser/{state-schemas-DJmzG_M8.js → state-schemas-CVQyYhpH.js} +3 -3
- package/dist/browser/{state-schemas-DJmzG_M8.js.map → state-schemas-CVQyYhpH.js.map} +1 -1
- package/dist/node/{connect-BLC4PXD9.mjs → connect-C8Ur6M5x.mjs} +4 -4
- package/dist/node/{connect-BLC4PXD9.mjs.map → connect-C8Ur6M5x.mjs.map} +1 -1
- package/dist/node/{dist-Bm05eoTg.mjs → dist-D3qXr1ES.mjs} +44 -30
- package/dist/node/dist-D3qXr1ES.mjs.map +1 -0
- package/dist/node/{dist-Cn3iExnV.mjs → dist-vA9scgbK.mjs} +3 -3
- package/dist/node/dist-vA9scgbK.mjs.map +1 -0
- package/dist/node/{document-drive-sM33juXr.mjs → document-drive-DjL9iikf.mjs} +314 -9
- package/dist/node/document-drive-DjL9iikf.mjs.map +1 -0
- package/dist/node/document-models/index.mjs +1 -1
- package/dist/node/{editor-BrS98lEH.mjs → editor-B6gn6QpO.mjs} +5 -5
- package/dist/node/{editor-BrS98lEH.mjs.map → editor-B6gn6QpO.mjs.map} +1 -1
- package/dist/node/{editor-BFBa9nsO.mjs → editor-kdobbTut.mjs} +6 -6
- package/dist/node/{editor-BFBa9nsO.mjs.map → editor-kdobbTut.mjs.map} +1 -1
- package/dist/node/editors/document-model-editor/module.mjs +1 -1
- package/dist/node/editors/generic-drive-explorer/index.mjs +4 -4
- package/dist/node/editors/generic-drive-explorer/module.mjs +1 -1
- package/dist/node/{folder-view-DHph3Yuu.mjs → folder-view-C9m-Kbjh.mjs} +3 -3
- package/dist/node/{folder-view-DHph3Yuu.mjs.map → folder-view-C9m-Kbjh.mjs.map} +1 -1
- package/dist/node/{graphql-editor-CPq7oIbf.mjs → graphql-editor-BiK6s8_B.mjs} +3 -3
- package/dist/node/{graphql-editor-CPq7oIbf.mjs.map → graphql-editor-BiK6s8_B.mjs.map} +1 -1
- package/dist/node/index.mjs +2 -2
- package/dist/node/{json-editor-L32CP7TS.mjs → json-editor-rMZOlQFM.mjs} +3 -3
- package/dist/node/{json-editor-L32CP7TS.mjs.map → json-editor-rMZOlQFM.mjs.map} +1 -1
- package/dist/node/{state-schemas-XTTjQZOe.mjs → state-schemas-Dt2-S4R8.mjs} +3 -3
- package/dist/node/{state-schemas-XTTjQZOe.mjs.map → state-schemas-Dt2-S4R8.mjs.map} +1 -1
- package/package.json +6 -6
- package/dist/browser/dist-C1nRM9t2.js.map +0 -1
- package/dist/browser/dist-DLFI75Zd.js.map +0 -1
- package/dist/browser/document-drive-oqv68jDU.js.map +0 -1
- package/dist/node/dist-Bm05eoTg.mjs.map +0 -1
- package/dist/node/dist-Cn3iExnV.mjs.map +0 -1
- package/dist/node/document-drive-sM33juXr.mjs.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dist-DPeHVSov.js","names":[],"sources":["../../../reactor/dist/drive-container-types-CAhznKRr.js","../../../reactor/dist/index.js","../../../../node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.mjs","../../../../node_modules/.pnpm/graphql-tag@2.12.6_graphql@16.12.0/node_modules/graphql-tag/lib/index.js","../../../reactor-drive/dist/index.js"],"sourcesContent":["import { n as ReactorEventTypes, t as EventBusAggregateError } from \"./types-CxSpmNGK.js\";\nimport { DowngradeNotSupportedError, applyDeleteDocumentAction, applyDeleteDocumentAction as applyDeleteDocumentAction$1, applyUpgradeDocumentAction, applyUpgradeDocumentAction as applyUpgradeDocumentAction$1, createPresignedHeader, defaultBaseState, deriveOperationId, isUndoRedo } from \"@powerhousedao/shared/document-model\";\nimport { v4 } from \"uuid\";\nimport { Migrator, sql } from \"kysely\";\n//#region \\0rolldown/runtime.js\nvar __defProp = Object.defineProperty;\nvar __exportAll = (all, no_symbols) => {\n\tlet target = {};\n\tfor (var name in all) __defProp(target, name, {\n\t\tget: all[name],\n\t\tenumerable: true\n\t});\n\tif (!no_symbols) __defProp(target, Symbol.toStringTag, { value: \"Module\" });\n\treturn target;\n};\n//#endregion\n//#region src/shared/utils.ts\nfunction matchesScope(view = {}, scope) {\n\tif (view.scopes) return view.scopes.includes(scope);\n\treturn true;\n}\nfunction yieldToMain() {\n\tconst s = globalThis.scheduler;\n\tif (s?.yield) return s.yield();\n\treturn new Promise((resolve) => setTimeout(resolve, 0));\n}\nconst defaultAbortError = () => /* @__PURE__ */ new Error(\"Operation aborted\");\nfunction throwIfAborted(signal, makeError = defaultAbortError) {\n\tif (signal?.aborted) throw makeError();\n}\n/**\n* Validates PagingOptions and returns a normalized offset and limit.\n* Throws if the cursor is not empty and not a non-negative integer, or if\n* limit is less than 1. When `paging` is undefined, returns offset 0 and\n* the caller-supplied `defaultLimit`.\n*/\nfunction parsePagingOptions(paging, defaultLimit) {\n\tif (paging === void 0) return {\n\t\toffset: 0,\n\t\tlimit: defaultLimit\n\t};\n\tif (!Number.isInteger(paging.limit) || paging.limit < 1) throw new Error(`Invalid paging limit: ${String(paging.limit)} (must be an integer >= 1)`);\n\tif (paging.cursor === \"\") return {\n\t\toffset: 0,\n\t\tlimit: paging.limit\n\t};\n\tconst parsed = Number(paging.cursor);\n\tif (!Number.isInteger(parsed) || parsed < 0) throw new Error(`Invalid paging cursor: ${JSON.stringify(paging.cursor)} (must be empty or a non-negative integer)`);\n\treturn {\n\t\toffset: parsed,\n\t\tlimit: paging.limit\n\t};\n}\n//#endregion\n//#region src/shared/errors.ts\n/**\n* Error thrown when attempting to access a deleted document.\n*/\nvar DocumentDeletedError = class DocumentDeletedError extends Error {\n\tdocumentId;\n\tdeletedAtUtcIso;\n\tconstructor(documentId, deletedAtUtcIso = null) {\n\t\tconst message = deletedAtUtcIso ? `Document ${documentId} was deleted at ${deletedAtUtcIso}` : `Document ${documentId} has been deleted`;\n\t\tsuper(message);\n\t\tthis.name = \"DocumentDeletedError\";\n\t\tthis.documentId = documentId;\n\t\tthis.deletedAtUtcIso = deletedAtUtcIso;\n\t\tError.captureStackTrace(this, DocumentDeletedError);\n\t}\n\tstatic isError(error) {\n\t\treturn Error.isError(error) && error.name === \"DocumentDeletedError\";\n\t}\n};\n/**\n* Error thrown when an operation has an invalid signature.\n*/\nvar InvalidSignatureError = class InvalidSignatureError extends Error {\n\tdocumentId;\n\treason;\n\tconstructor(documentId, reason) {\n\t\tsuper(`Invalid signature in document ${documentId}: ${reason}`);\n\t\tthis.name = \"InvalidSignatureError\";\n\t\tthis.documentId = documentId;\n\t\tthis.reason = reason;\n\t\tError.captureStackTrace(this, InvalidSignatureError);\n\t}\n};\n/**\n* Error thrown when a document is not found (no operations exist for the document ID).\n*/\nvar DocumentNotFoundError = class DocumentNotFoundError extends Error {\n\tdocumentId;\n\tconstructor(documentId) {\n\t\tsuper(`Document ${documentId} not found`);\n\t\tthis.name = \"DocumentNotFoundError\";\n\t\tthis.documentId = documentId;\n\t\tError.captureStackTrace(this, DocumentNotFoundError);\n\t}\n\tstatic isError(error) {\n\t\treturn Error.isError(error) && error.name === \"DocumentNotFoundError\";\n\t}\n};\n//#endregion\n//#region src/registry/errors.ts\n/**\n* Error thrown when a document model module is not found in the registry.\n*/\nvar ModuleNotFoundError = class extends Error {\n\tdocumentType;\n\trequestedVersion;\n\tconstructor(documentType, version) {\n\t\tconst versionSuffix = version !== void 0 ? ` version ${version}` : \"\";\n\t\tsuper(`Document model module not found for type: ${documentType}${versionSuffix}`);\n\t\tthis.name = \"ModuleNotFoundError\";\n\t\tthis.documentType = documentType;\n\t\tthis.requestedVersion = version;\n\t}\n\tstatic isError(error) {\n\t\treturn Error.isError(error) && error.name === \"ModuleNotFoundError\";\n\t}\n};\n/**\n* Error thrown when attempting to register a module that already exists.\n*/\nvar DuplicateModuleError = class extends Error {\n\tconstructor(documentType, version) {\n\t\tconst versionSuffix = version !== void 0 ? ` (version ${version})` : \"\";\n\t\tsuper(`Document model module already registered for type: ${documentType}${versionSuffix}`);\n\t\tthis.name = \"DuplicateModuleError\";\n\t}\n\tstatic isError(error) {\n\t\treturn Error.isError(error) && error.name === \"DuplicateModuleError\";\n\t}\n};\n/**\n* Error thrown when a module is invalid or malformed.\n*/\nvar InvalidModuleError = class extends Error {\n\tconstructor(message) {\n\t\tsuper(`Invalid document model module: ${message}`);\n\t\tthis.name = \"InvalidModuleError\";\n\t}\n};\n/**\n* Error thrown when attempting to register an upgrade manifest that already exists.\n*/\nvar DuplicateManifestError = class extends Error {\n\tconstructor(documentType) {\n\t\tsuper(`Upgrade manifest already registered for type: ${documentType}`);\n\t\tthis.name = \"DuplicateManifestError\";\n\t}\n\tstatic isError(error) {\n\t\treturn Error.isError(error) && error.name === \"DuplicateManifestError\";\n\t}\n};\n/**\n* Error thrown when an upgrade manifest is not found.\n*/\nvar ManifestNotFoundError = class extends Error {\n\tconstructor(documentType) {\n\t\tsuper(`Upgrade manifest not found for type: ${documentType}`);\n\t\tthis.name = \"ManifestNotFoundError\";\n\t}\n};\n/**\n* Error thrown when a required upgrade transition is missing from the manifest.\n*/\nvar MissingUpgradeTransitionError = class extends Error {\n\tconstructor(documentType, fromVersion, toVersion) {\n\t\tsuper(`Missing upgrade transition for ${documentType}: v${fromVersion} to v${toVersion}`);\n\t\tthis.name = \"MissingUpgradeTransitionError\";\n\t}\n};\n/**\n* Error thrown when getUpgradeReducer is called with a non-single-step version increment.\n*/\nvar InvalidUpgradeStepError = class extends Error {\n\tconstructor(documentType, fromVersion, toVersion) {\n\t\tsuper(`Invalid upgrade step for ${documentType}: must be single version increment, got v${fromVersion} to v${toVersion}`);\n\t\tthis.name = \"InvalidUpgradeStepError\";\n\t}\n};\n//#endregion\n//#region src/cache/collection-membership-cache.ts\nvar CollectionMembershipCache = class CollectionMembershipCache {\n\tcache = /* @__PURE__ */ new Map();\n\tconstructor(operationIndex) {\n\t\tthis.operationIndex = operationIndex;\n\t}\n\twithScopedIndex(operationIndex) {\n\t\tconst scoped = new CollectionMembershipCache(operationIndex);\n\t\tscoped.cache = this.cache;\n\t\treturn scoped;\n\t}\n\tasync getCollectionsForDocuments(documentIds) {\n\t\tconst result = {};\n\t\tconst missing = [];\n\t\tfor (const docId of documentIds) {\n\t\t\tconst cached = this.cache.get(docId);\n\t\t\tif (cached !== void 0) result[docId] = cached;\n\t\t\telse missing.push(docId);\n\t\t}\n\t\tif (missing.length > 0) {\n\t\t\tconst fromDb = await this.operationIndex.getCollectionsForDocuments(missing);\n\t\t\tfor (const docId of missing) {\n\t\t\t\tconst collections = fromDb[docId] ?? [];\n\t\t\t\tresult[docId] = collections;\n\t\t\t\tthis.cache.set(docId, collections);\n\t\t\t}\n\t\t}\n\t\treturn result;\n\t}\n\tinvalidate(documentId) {\n\t\tthis.cache.delete(documentId);\n\t}\n};\n//#endregion\n//#region src/executor/util.ts\n/**\n* Creates a PHDocument from a CREATE_DOCUMENT action input.\n* Reconstructs the document header and initializes the base state.\n*\n* @param action - The CREATE_DOCUMENT action containing the document parameters\n* @returns A newly constructed PHDocument with initialized header and base state\n*/\nfunction createDocumentFromAction(action) {\n\tconst input = action.input;\n\tconst header = createPresignedHeader();\n\theader.id = input.documentId;\n\theader.documentType = input.model;\n\tif (input.signing) {\n\t\theader.createdAtUtcIso = input.signing.createdAtUtcIso;\n\t\theader.lastModifiedAtUtcIso = input.signing.createdAtUtcIso;\n\t\theader.sig = {\n\t\t\tpublicKey: input.signing.publicKey,\n\t\t\tnonce: input.signing.nonce\n\t\t};\n\t}\n\tif (input.slug !== void 0) header.slug = input.slug;\n\tif (!header.slug) header.slug = input.documentId;\n\tif (input.name !== void 0) header.name = input.name;\n\tif (input.branch !== void 0) header.branch = input.branch;\n\tif (input.meta !== void 0) header.meta = input.meta;\n\tif (input.protocolVersions !== void 0) header.protocolVersions = input.protocolVersions;\n\tconst baseState = defaultBaseState();\n\treturn {\n\t\theader,\n\t\toperations: {},\n\t\tstate: baseState,\n\t\tinitialState: baseState,\n\t\tclipboard: []\n\t};\n}\n/**\n* Calculate the next operation index for a specific scope.\n* Each scope maintains its own independent index sequence.\n*\n* Per-scope indexing means:\n* - Each scope (document, global, local, etc.) has independent indexes\n* - Indexes start at 0 for each scope\n* - Different scopes can have operations with the same index value\n*\n* This function uses header.revision which is populated by the cache/storage layer\n* and contains the next available index for each scope. This design avoids requiring\n* the full operation history to be loaded, which is crucial for snapshot-based caching.\n*\n* @param document - The document whose header.revision to inspect\n* @param scope - The scope to calculate the next index for\n* @returns The next available index in the specified scope\n*/\nconst getNextIndexForScope = (document, scope) => {\n\treturn document.header.revision[scope] || 0;\n};\n/**\n* Creates an empty consistency token with no coordinates.\n* Used when a job is registered or fails without writing operations.\n*\n* @returns A consistency token with an empty coordinates array\n*/\nfunction createEmptyConsistencyToken() {\n\treturn {\n\t\tversion: 1,\n\t\tcreatedAtUtcIso: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tcoordinates: []\n\t};\n}\n/**\n* Creates a consistency token from operations written during job execution.\n* Maps each operation to a consistency coordinate tracking (documentId, scope, branch, operationIndex).\n* If no operations are provided, returns an empty token.\n*\n* @param operationsWithContext - Array of operations with their execution context\n* @returns A consistency token representing all operations written\n*/\nfunction createConsistencyToken(operationsWithContext) {\n\tif (operationsWithContext.length === 0) return createEmptyConsistencyToken();\n\tconst coordinates = [];\n\tfor (let i = 0; i < operationsWithContext.length; i++) {\n\t\tconst opWithContext = operationsWithContext[i];\n\t\tcoordinates.push({\n\t\t\tdocumentId: opWithContext.context.documentId,\n\t\t\tscope: opWithContext.context.scope,\n\t\t\tbranch: opWithContext.context.branch,\n\t\t\toperationIndex: opWithContext.operation.index\n\t\t});\n\t}\n\treturn {\n\t\tversion: 1,\n\t\tcreatedAtUtcIso: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tcoordinates\n\t};\n}\nfunction createOperation(action, index, skip, context) {\n\treturn {\n\t\tid: deriveOperationId(context.documentId, context.scope, context.branch, action.id),\n\t\tindex,\n\t\ttimestampUtcMs: action.timestampUtcMs || (/* @__PURE__ */ new Date()).toISOString(),\n\t\thash: \"\",\n\t\tskip,\n\t\taction\n\t};\n}\nfunction updateDocumentRevision(document, scope, operationIndex) {\n\tdocument.header.revision = {\n\t\t...document.header.revision,\n\t\t[scope]: operationIndex + 1\n\t};\n}\nfunction buildSuccessResult(job, operation, documentId, documentType, resultingState, startTime) {\n\treturn {\n\t\tjob,\n\t\tsuccess: true,\n\t\toperations: [operation],\n\t\toperationsWithContext: [{\n\t\t\toperation,\n\t\t\tcontext: {\n\t\t\t\tdocumentId,\n\t\t\t\tscope: job.scope,\n\t\t\t\tbranch: job.branch,\n\t\t\t\tdocumentType,\n\t\t\t\tresultingState,\n\t\t\t\tordinal: 0\n\t\t\t}\n\t\t}],\n\t\tduration: Date.now() - startTime\n\t};\n}\nfunction buildErrorResult(job, error, startTime) {\n\treturn {\n\t\tjob,\n\t\tsuccess: false,\n\t\terror,\n\t\tduration: Date.now() - startTime\n\t};\n}\n//#endregion\n//#region src/cache/lru/lru-tracker.ts\nvar LRUNode = class {\n\tkey;\n\tprev;\n\tnext;\n\tconstructor(key) {\n\t\tthis.key = key;\n\t\tthis.prev = void 0;\n\t\tthis.next = void 0;\n\t}\n};\nvar LRUTracker = class {\n\tmap;\n\thead;\n\ttail;\n\tconstructor() {\n\t\tthis.map = /* @__PURE__ */ new Map();\n\t\tthis.head = void 0;\n\t\tthis.tail = void 0;\n\t}\n\tget size() {\n\t\treturn this.map.size;\n\t}\n\ttouch(key) {\n\t\tconst node = this.map.get(key);\n\t\tif (node) this.moveToFront(node);\n\t\telse this.addToFront(key);\n\t}\n\tevict() {\n\t\tif (!this.tail) return;\n\t\tconst key = this.tail.key;\n\t\tthis.remove(key);\n\t\treturn key;\n\t}\n\tremove(key) {\n\t\tconst node = this.map.get(key);\n\t\tif (!node) return;\n\t\tthis.removeNode(node);\n\t\tthis.map.delete(key);\n\t}\n\tclear() {\n\t\tthis.map.clear();\n\t\tthis.head = void 0;\n\t\tthis.tail = void 0;\n\t}\n\taddToFront(key) {\n\t\tconst node = new LRUNode(key);\n\t\tthis.map.set(key, node);\n\t\tif (!this.head) {\n\t\t\tthis.head = node;\n\t\t\tthis.tail = node;\n\t\t} else {\n\t\t\tnode.next = this.head;\n\t\t\tthis.head.prev = node;\n\t\t\tthis.head = node;\n\t\t}\n\t}\n\tmoveToFront(node) {\n\t\tif (node === this.head) return;\n\t\tthis.removeNode(node);\n\t\tnode.prev = void 0;\n\t\tnode.next = this.head;\n\t\tif (this.head) this.head.prev = node;\n\t\tthis.head = node;\n\t\tif (!this.tail) this.tail = node;\n\t}\n\tremoveNode(node) {\n\t\tif (node.prev) node.prev.next = node.next;\n\t\telse this.head = node.next;\n\t\tif (node.next) node.next.prev = node.prev;\n\t\telse this.tail = node.prev;\n\t}\n};\n//#endregion\n//#region src/cache/document-meta-cache.ts\n/**\n* In-memory document metadata cache with LRU eviction.\n*\n* Caches PHDocumentState per (documentId, branch) key. On cache miss,\n* rebuilds from document scope operations. Provides an explicit cross-scope\n* contract for accessing document scope metadata.\n*\n* **Thread Safety:**\n* Not thread-safe. Designed for single-threaded job executor environment.\n*/\nvar DocumentMetaCache = class DocumentMetaCache {\n\tcache;\n\tlruTracker;\n\toperationStore;\n\tconfig;\n\tconstructor(operationStore, config) {\n\t\tthis.operationStore = operationStore;\n\t\tthis.config = { maxDocuments: config.maxDocuments };\n\t\tthis.cache = /* @__PURE__ */ new Map();\n\t\tthis.lruTracker = new LRUTracker();\n\t}\n\twithScopedStore(operationStore) {\n\t\tconst scoped = new DocumentMetaCache(operationStore, this.config);\n\t\tscoped.cache = this.cache;\n\t\tscoped.lruTracker = this.lruTracker;\n\t\treturn scoped;\n\t}\n\tasync startup() {\n\t\treturn Promise.resolve();\n\t}\n\tasync shutdown() {\n\t\treturn Promise.resolve();\n\t}\n\tasync getDocumentMeta(documentId, branch, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst key = this.makeKey(documentId, branch);\n\t\tconst cached = this.cache.get(key);\n\t\tif (cached) {\n\t\t\tthis.lruTracker.touch(key);\n\t\t\treturn cached;\n\t\t}\n\t\tconst meta = await this.rebuildLatest(documentId, branch, signal);\n\t\tthis.putDocumentMeta(documentId, branch, meta);\n\t\treturn meta;\n\t}\n\tasync rebuildAtRevision(documentId, branch, targetRevision, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\treturn this.rebuildFromOperations(documentId, branch, targetRevision, signal);\n\t}\n\tputDocumentMeta(documentId, branch, meta) {\n\t\tconst key = this.makeKey(documentId, branch);\n\t\tif (!this.cache.has(key) && this.cache.size >= this.config.maxDocuments) {\n\t\t\tconst evictKey = this.lruTracker.evict();\n\t\t\tif (evictKey) this.cache.delete(evictKey);\n\t\t}\n\t\tthis.cache.set(key, structuredClone(meta));\n\t\tthis.lruTracker.touch(key);\n\t}\n\tinvalidate(documentId, branch) {\n\t\tlet evicted = 0;\n\t\tif (branch === void 0) {\n\t\t\tfor (const key of this.cache.keys()) if (key.startsWith(`${documentId}:`)) {\n\t\t\t\tthis.cache.delete(key);\n\t\t\t\tthis.lruTracker.remove(key);\n\t\t\t\tevicted++;\n\t\t\t}\n\t\t} else {\n\t\t\tconst key = this.makeKey(documentId, branch);\n\t\t\tif (this.cache.has(key)) {\n\t\t\t\tthis.cache.delete(key);\n\t\t\t\tthis.lruTracker.remove(key);\n\t\t\t\tevicted = 1;\n\t\t\t}\n\t\t}\n\t\treturn evicted;\n\t}\n\tclear() {\n\t\tthis.cache.clear();\n\t\tthis.lruTracker.clear();\n\t}\n\tmakeKey(documentId, branch) {\n\t\treturn `${documentId}:${branch}`;\n\t}\n\tasync rebuildLatest(documentId, branch, signal) {\n\t\treturn this.rebuildFromOperations(documentId, branch, void 0, signal);\n\t}\n\tasync rebuildFromOperations(documentId, branch, targetRevision, signal) {\n\t\tconst docScopeOps = await this.operationStore.getSince(documentId, \"document\", branch, -1, void 0, void 0, signal);\n\t\tif (docScopeOps.results.length === 0) throw new DocumentNotFoundError(documentId);\n\t\tconst createOp = docScopeOps.results[0];\n\t\tif (createOp.action.type !== \"CREATE_DOCUMENT\") throw new Error(`Invalid document: first operation must be CREATE_DOCUMENT, found ${createOp.action.type}`);\n\t\tconst createAction = createOp.action;\n\t\tconst documentType = createAction.input.model;\n\t\tlet document = createDocumentFromAction(createAction);\n\t\tlet documentScopeRevision = 0;\n\t\tfor (const op of docScopeOps.results) {\n\t\t\tif (targetRevision !== void 0 && op.index > targetRevision) break;\n\t\t\tdocumentScopeRevision = op.index;\n\t\t\tif (op.action.type === \"UPGRADE_DOCUMENT\") {\n\t\t\t\tconst upgradeAction = op.action;\n\t\t\t\tdocument = applyUpgradeDocumentAction$1(document, upgradeAction);\n\t\t\t} else if (op.action.type === \"DELETE_DOCUMENT\") document = applyDeleteDocumentAction$1(document, op.action);\n\t\t}\n\t\treturn {\n\t\t\tstate: document.state.document,\n\t\t\tdocumentType,\n\t\t\tdocumentScopeRevision: documentScopeRevision + 1\n\t\t};\n\t}\n};\n//#endregion\n//#region src/cache/kysely-operation-index.ts\nvar KyselyOperationIndexTxn = class {\n\tcollections = [];\n\tcollectionMemberships = [];\n\tcollectionRemovals = [];\n\toperations = [];\n\tcreateCollection(collectionId) {\n\t\tthis.collections.push(collectionId);\n\t}\n\taddToCollection(collectionId, documentId) {\n\t\tconst lastOpIndex = this.operations.length - 1;\n\t\tif (lastOpIndex < 0) throw new Error(\"addToCollection must be called after write() - no operations in transaction\");\n\t\tthis.collectionMemberships.push({\n\t\t\tcollectionId,\n\t\t\tdocumentId,\n\t\t\toperationIndex: lastOpIndex\n\t\t});\n\t}\n\tremoveFromCollection(collectionId, documentId) {\n\t\tconst lastOpIndex = this.operations.length - 1;\n\t\tif (lastOpIndex < 0) throw new Error(\"removeFromCollection must be called after write() - no operations in transaction\");\n\t\tthis.collectionRemovals.push({\n\t\t\tcollectionId,\n\t\t\tdocumentId,\n\t\t\toperationIndex: lastOpIndex\n\t\t});\n\t}\n\twrite(operations) {\n\t\tthis.operations.push(...operations);\n\t}\n\tgetCollections() {\n\t\treturn this.collections;\n\t}\n\tgetCollectionMembershipRecords() {\n\t\treturn this.collectionMemberships;\n\t}\n\tgetCollectionRemovals() {\n\t\treturn this.collectionRemovals;\n\t}\n\tgetOperations() {\n\t\treturn this.operations;\n\t}\n};\nvar KyselyOperationIndex = class KyselyOperationIndex {\n\ttrx;\n\tconstructor(db) {\n\t\tthis.db = db;\n\t}\n\tget queryExecutor() {\n\t\treturn this.trx ?? this.db;\n\t}\n\twithTransaction(trx) {\n\t\tconst instance = new KyselyOperationIndex(this.db);\n\t\tinstance.trx = trx;\n\t\treturn instance;\n\t}\n\tstart() {\n\t\treturn new KyselyOperationIndexTxn();\n\t}\n\tasync commit(txn, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst kyselyTxn = txn;\n\t\tif (this.trx) return this.executeCommit(this.trx, kyselyTxn);\n\t\tlet resultOrdinals = [];\n\t\tawait this.db.transaction().execute(async (trx) => {\n\t\t\tresultOrdinals = await this.executeCommit(trx, kyselyTxn);\n\t\t});\n\t\treturn resultOrdinals;\n\t}\n\tasync executeCommit(trx, kyselyTxn) {\n\t\tconst collections = kyselyTxn.getCollections();\n\t\tconst memberships = kyselyTxn.getCollectionMembershipRecords();\n\t\tconst removals = kyselyTxn.getCollectionRemovals();\n\t\tconst operations = kyselyTxn.getOperations();\n\t\tif (collections.length > 0) {\n\t\t\tconst collectionRows = collections.map((collectionId) => ({\n\t\t\t\tdocumentId: collectionId,\n\t\t\t\tcollectionId,\n\t\t\t\tjoinedOrdinal: BigInt(0),\n\t\t\t\tleftOrdinal: null\n\t\t\t}));\n\t\t\tawait trx.insertInto(\"document_collections\").values(collectionRows).onConflict((oc) => oc.doNothing()).execute();\n\t\t}\n\t\tlet operationOrdinals = [];\n\t\tif (operations.length > 0) {\n\t\t\tconst operationRows = operations.map((op) => ({\n\t\t\t\topId: op.id || \"\",\n\t\t\t\tdocumentId: op.documentId,\n\t\t\t\tdocumentType: op.documentType,\n\t\t\t\tscope: op.scope,\n\t\t\t\tbranch: op.branch,\n\t\t\t\ttimestampUtcMs: op.timestampUtcMs,\n\t\t\t\tindex: op.index,\n\t\t\t\tskip: op.skip,\n\t\t\t\thash: op.hash,\n\t\t\t\taction: op.action,\n\t\t\t\tsourceRemote: op.sourceRemote\n\t\t\t}));\n\t\t\toperationOrdinals = (await trx.insertInto(\"operation_index_operations\").values(operationRows).returning(\"ordinal\").execute()).map((row) => row.ordinal);\n\t\t}\n\t\tif (memberships.length > 0) for (const m of memberships) {\n\t\t\tconst ordinal = operationOrdinals[m.operationIndex];\n\t\t\tawait trx.insertInto(\"document_collections\").values({\n\t\t\t\tdocumentId: m.documentId,\n\t\t\t\tcollectionId: m.collectionId,\n\t\t\t\tjoinedOrdinal: BigInt(ordinal),\n\t\t\t\tleftOrdinal: null\n\t\t\t}).onConflict((oc) => oc.columns([\"documentId\", \"collectionId\"]).doUpdateSet({\n\t\t\t\tjoinedOrdinal: BigInt(ordinal),\n\t\t\t\tleftOrdinal: null\n\t\t\t})).execute();\n\t\t}\n\t\tif (removals.length > 0) for (const r of removals) {\n\t\t\tconst ordinal = operationOrdinals[r.operationIndex];\n\t\t\tawait trx.updateTable(\"document_collections\").set({ leftOrdinal: BigInt(ordinal) }).where(\"collectionId\", \"=\", r.collectionId).where(\"documentId\", \"=\", r.documentId).where(\"leftOrdinal\", \"is\", null).execute();\n\t\t}\n\t\treturn operationOrdinals;\n\t}\n\tasync find(collectionId, cursor, view, paging, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst outerCursor = cursor ?? -1;\n\t\tconst pagingCursorOrdinal = paging?.cursor !== void 0 ? Number.parseInt(paging.cursor, 10) : -1;\n\t\tconst buildBranch = (kind) => {\n\t\t\tlet qb = this.queryExecutor.selectFrom(\"operation_index_operations as oi\").innerJoin(\"document_collections as dc\", \"oi.documentId\", \"dc.documentId\").selectAll(\"oi\").select([\"dc.documentId\", \"dc.collectionId\"]).where(\"dc.collectionId\", \"=\", collectionId).where(sql`(dc.\"leftOrdinal\" IS NULL OR oi.ordinal < dc.\"leftOrdinal\")`);\n\t\t\tif (kind === \"joiner\") qb = qb.where(\"dc.joinedOrdinal\", \">\", BigInt(outerCursor)).where(\"oi.ordinal\", \"<=\", outerCursor);\n\t\t\telse qb = qb.where(\"oi.ordinal\", \">\", outerCursor);\n\t\t\tqb = qb.where(\"oi.ordinal\", \">\", pagingCursorOrdinal);\n\t\t\tif (view?.branch) qb = qb.where(\"oi.branch\", \"=\", view.branch);\n\t\t\tif (view?.scopes && view.scopes.length > 0) qb = qb.where(\"oi.scope\", \"in\", view.scopes);\n\t\t\tif (view?.excludeSourceRemote) qb = qb.where(\"oi.sourceRemote\", \"!=\", view.excludeSourceRemote);\n\t\t\treturn qb;\n\t\t};\n\t\tlet unionQuery = buildBranch(\"joiner\").unionAll(buildBranch(\"newOps\")).orderBy(\"ordinal\", \"asc\");\n\t\tif (paging?.limit) unionQuery = unionQuery.limit(paging.limit + 1);\n\t\tconst rows = await unionQuery.execute();\n\t\tlet hasMore = false;\n\t\tlet items = rows;\n\t\tif (paging?.limit && rows.length > paging.limit) {\n\t\t\thasMore = true;\n\t\t\titems = rows.slice(0, paging.limit);\n\t\t}\n\t\tconst nextCursor = hasMore && items.length > 0 ? items[items.length - 1].ordinal.toString() : void 0;\n\t\tconst cursorValue = paging?.cursor || \"0\";\n\t\tconst limit = paging?.limit || 100;\n\t\treturn {\n\t\t\tresults: items.map((row) => this.rowToOperationIndexEntry(row)),\n\t\t\toptions: {\n\t\t\t\tcursor: cursorValue,\n\t\t\t\tlimit\n\t\t\t},\n\t\t\tnextCursor,\n\t\t\tnext: hasMore ? () => this.find(collectionId, cursor, view, {\n\t\t\t\tcursor: nextCursor,\n\t\t\t\tlimit\n\t\t\t}, signal) : void 0\n\t\t};\n\t}\n\tasync get(documentId, view, paging, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tlet query = this.queryExecutor.selectFrom(\"operation_index_operations\").selectAll().where(\"documentId\", \"=\", documentId).orderBy(\"ordinal\", \"asc\");\n\t\tif (view?.branch) query = query.where(\"branch\", \"=\", view.branch);\n\t\tif (view?.scopes && view.scopes.length > 0) query = query.where(\"scope\", \"in\", view.scopes);\n\t\tif (paging?.cursor) {\n\t\t\tconst cursorOrdinal = Number.parseInt(paging.cursor, 10);\n\t\t\tquery = query.where(\"ordinal\", \">\", cursorOrdinal);\n\t\t}\n\t\tif (paging?.limit) query = query.limit(paging.limit + 1);\n\t\tconst rows = await query.execute();\n\t\tlet hasMore = false;\n\t\tlet items = rows;\n\t\tif (paging?.limit && rows.length > paging.limit) {\n\t\t\thasMore = true;\n\t\t\titems = rows.slice(0, paging.limit);\n\t\t}\n\t\tconst nextCursor = hasMore && items.length > 0 ? items[items.length - 1].ordinal.toString() : void 0;\n\t\tconst cursorValue = paging?.cursor || \"0\";\n\t\tconst limit = paging?.limit || 100;\n\t\treturn {\n\t\t\tresults: items.map((row) => this.rowToOperationIndexEntry(row)),\n\t\t\toptions: {\n\t\t\t\tcursor: cursorValue,\n\t\t\t\tlimit\n\t\t\t},\n\t\t\tnextCursor,\n\t\t\tnext: hasMore ? () => this.get(documentId, view, {\n\t\t\t\tcursor: nextCursor,\n\t\t\t\tlimit\n\t\t\t}, signal) : void 0\n\t\t};\n\t}\n\tasync getSinceOrdinal(ordinal, paging, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tlet query = this.queryExecutor.selectFrom(\"operation_index_operations\").selectAll().where(\"ordinal\", \">\", ordinal).orderBy(\"ordinal\", \"asc\");\n\t\tif (paging?.cursor) {\n\t\t\tconst cursorOrdinal = Number.parseInt(paging.cursor, 10);\n\t\t\tquery = query.where(\"ordinal\", \">\", cursorOrdinal);\n\t\t}\n\t\tif (paging?.limit) query = query.limit(paging.limit + 1);\n\t\tconst rows = await query.execute();\n\t\tlet hasMore = false;\n\t\tlet items = rows;\n\t\tif (paging?.limit && rows.length > paging.limit) {\n\t\t\thasMore = true;\n\t\t\titems = rows.slice(0, paging.limit);\n\t\t}\n\t\tconst nextCursor = hasMore && items.length > 0 ? items[items.length - 1].ordinal.toString() : void 0;\n\t\tconst cursorValue = paging?.cursor || \"0\";\n\t\tconst limit = paging?.limit || 100;\n\t\treturn {\n\t\t\tresults: items.map((row) => this.rowToOperationWithContext(row)),\n\t\t\toptions: {\n\t\t\t\tcursor: cursorValue,\n\t\t\t\tlimit\n\t\t\t},\n\t\t\tnextCursor,\n\t\t\tnext: hasMore ? () => this.getSinceOrdinal(ordinal, {\n\t\t\t\tcursor: nextCursor,\n\t\t\t\tlimit\n\t\t\t}, signal) : void 0\n\t\t};\n\t}\n\trowToOperationWithContext(row) {\n\t\treturn {\n\t\t\toperation: {\n\t\t\t\tindex: row.index,\n\t\t\t\ttimestampUtcMs: row.timestampUtcMs,\n\t\t\t\thash: row.hash,\n\t\t\t\tskip: row.skip,\n\t\t\t\taction: row.action,\n\t\t\t\tid: row.opId\n\t\t\t},\n\t\t\tcontext: {\n\t\t\t\tdocumentId: row.documentId,\n\t\t\t\tdocumentType: row.documentType,\n\t\t\t\tscope: row.scope,\n\t\t\t\tbranch: row.branch,\n\t\t\t\tordinal: row.ordinal\n\t\t\t}\n\t\t};\n\t}\n\trowToOperationIndexEntry(row) {\n\t\treturn {\n\t\t\tordinal: row.ordinal,\n\t\t\tdocumentId: row.documentId,\n\t\t\tdocumentType: row.documentType,\n\t\t\tbranch: row.branch,\n\t\t\tscope: row.scope,\n\t\t\tindex: row.index,\n\t\t\ttimestampUtcMs: row.timestampUtcMs,\n\t\t\thash: row.hash,\n\t\t\tskip: row.skip,\n\t\t\taction: row.action,\n\t\t\tid: row.opId,\n\t\t\tsourceRemote: row.sourceRemote\n\t\t};\n\t}\n\tasync getLatestTimestampForCollection(collectionId, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\treturn (await this.queryExecutor.selectFrom(\"operation_index_operations as oi\").innerJoin(\"document_collections as dc\", \"oi.documentId\", \"dc.documentId\").select(\"oi.timestampUtcMs\").where(\"dc.collectionId\", \"=\", collectionId).where(sql`(dc.\"leftOrdinal\" IS NULL OR oi.ordinal < dc.\"leftOrdinal\")`).orderBy(\"oi.ordinal\", \"desc\").limit(1).executeTakeFirst())?.timestampUtcMs ?? null;\n\t}\n\tasync getCollectionsForDocuments(documentIds) {\n\t\tif (documentIds.length === 0) return {};\n\t\tconst rows = await this.queryExecutor.selectFrom(\"document_collections\").select([\"documentId\", \"collectionId\"]).where(\"documentId\", \"in\", documentIds).where(\"leftOrdinal\", \"is\", null).execute();\n\t\tconst result = {};\n\t\tfor (const row of rows) {\n\t\t\tif (!(row.documentId in result)) result[row.documentId] = [];\n\t\t\tresult[row.documentId].push(row.collectionId);\n\t\t}\n\t\treturn result;\n\t}\n};\n//#endregion\n//#region src/cache/buffer/ring-buffer.ts\n/**\n* RingBuffer is a generic circular buffer implementation that stores a fixed number\n* of items. When the buffer is full, new items overwrite the oldest items.\n*\n* This implementation maintains O(1) time complexity for push operations and provides\n* items in chronological order (oldest to newest) via getAll().\n*\n* @template T - The type of items stored in the buffer\n*/\nvar RingBuffer = class {\n\tbuffer;\n\thead = 0;\n\tsize = 0;\n\tcapacity;\n\tconstructor(capacity) {\n\t\tif (capacity <= 0) throw new Error(\"Ring buffer capacity must be greater than 0\");\n\t\tthis.capacity = capacity;\n\t\tthis.buffer = new Array(capacity);\n\t}\n\t/**\n\t* Adds an item to the buffer. If the buffer is full, overwrites the oldest item.\n\t*\n\t* @param item - The item to add\n\t*/\n\tpush(item) {\n\t\tconst index = (this.head + this.size) % this.capacity;\n\t\tif (this.size < this.capacity) {\n\t\t\tthis.buffer[index] = item;\n\t\t\tthis.size++;\n\t\t} else {\n\t\t\tthis.buffer[this.head] = item;\n\t\t\tthis.head = (this.head + 1) % this.capacity;\n\t\t}\n\t}\n\t/**\n\t* Returns all items in the buffer in chronological order (oldest to newest).\n\t*\n\t* @returns Array of items in insertion order\n\t*/\n\tgetAll() {\n\t\tif (this.size === 0) return [];\n\t\tconst result = [];\n\t\tfor (let i = 0; i < this.size; i++) {\n\t\t\tconst index = (this.head + i) % this.capacity;\n\t\t\tresult.push(this.buffer[index]);\n\t\t}\n\t\treturn result;\n\t}\n\t/**\n\t* Clears all items from the buffer.\n\t*/\n\tclear() {\n\t\tthis.buffer = new Array(this.capacity);\n\t\tthis.head = 0;\n\t\tthis.size = 0;\n\t}\n\t/**\n\t* Gets the current number of items in the buffer.\n\t*/\n\tget length() {\n\t\treturn this.size;\n\t}\n};\n//#endregion\n//#region src/cache/kysely-write-cache.ts\nfunction extractModuleVersion(doc) {\n\tconst v = doc.state.document.version;\n\treturn v === 0 ? void 0 : v;\n}\n/**\n* In-memory write cache with keyframe persistence for PHDocuments.\n*\n* Caches document snapshots in ring buffers with LRU eviction. On cache miss,\n* rebuilds documents from nearest keyframe or full operation history.\n*\n* **Performance Characteristics:**\n* - Cache hit: O(1) lookup in ring buffer\n* - Cold miss: O(n) where n is total operation count, or O(k) where k is operations since keyframe\n* - Warm miss: O(m) where m is operations since cached revision\n* - Eviction: O(1) for LRU tracking and removal\n*\n* **Thread Safety:**\n* Not thread-safe. Designed for single-threaded job executor environment.\n* External synchronization required for concurrent access across multiple executors.\n*\n* **Example:**\n* ```typescript\n* const cache = new KyselyWriteCache(\n* keyframeStore,\n* operationStore,\n* registry,\n* { maxDocuments: 1000, ringBufferSize: 10, keyframeInterval: 10 }\n* );\n*\n* await cache.startup();\n*\n* // Retrieve or rebuild document\n* const doc = await cache.getState(docId, docType, scope, branch, revision);\n*\n* // Cache result after job execution\n* cache.putState(docId, docType, scope, branch, newRevision, updatedDoc);\n*\n* await cache.shutdown();\n* ```\n*/\nvar KyselyWriteCache = class KyselyWriteCache {\n\tstreams;\n\tlruTracker;\n\tkeyframeStore;\n\toperationStore;\n\tregistry;\n\tconfig;\n\tconstructor(keyframeStore, operationStore, registry, config) {\n\t\tthis.keyframeStore = keyframeStore;\n\t\tthis.operationStore = operationStore;\n\t\tthis.registry = registry;\n\t\tthis.config = {\n\t\t\tmaxDocuments: config.maxDocuments,\n\t\t\tringBufferSize: config.ringBufferSize,\n\t\t\tkeyframeInterval: config.keyframeInterval\n\t\t};\n\t\tthis.streams = /* @__PURE__ */ new Map();\n\t\tthis.lruTracker = new LRUTracker();\n\t}\n\twithScopedStores(operationStore, keyframeStore) {\n\t\tconst scoped = new KyselyWriteCache(keyframeStore, operationStore, this.registry, this.config);\n\t\tscoped.streams = this.streams;\n\t\tscoped.lruTracker = this.lruTracker;\n\t\treturn scoped;\n\t}\n\t/**\n\t* Initializes the write cache.\n\t* Currently a no-op as keyframe store lifecycle is managed externally.\n\t*/\n\tasync startup() {\n\t\treturn Promise.resolve();\n\t}\n\t/**\n\t* Shuts down the write cache.\n\t* Currently a no-op as keyframe store lifecycle is managed externally.\n\t*/\n\tasync shutdown() {\n\t\treturn Promise.resolve();\n\t}\n\t/**\n\t* Retrieves document state at a specific revision from cache or rebuilds it.\n\t*\n\t* Cache hit path: Returns cached snapshot if available (O(1))\n\t* Warm miss path: Rebuilds from cached base revision + incremental ops\n\t* Cold miss path: Rebuilds from keyframe or from scratch using all operations\n\t*\n\t* @param documentId - The document identifier\n\t* @param scope - The operation scope\n\t* @param branch - The operation branch\n\t* @param targetRevision - The target revision, or undefined for newest\n\t* @param signal - Optional abort signal to cancel the operation\n\t* @returns The document at the target revision\n\t* @throws {Error} \"Operation aborted\" if signal is aborted\n\t* @throws {ModuleNotFoundError} If document type not registered in registry\n\t* @throws {Error} \"Failed to rebuild document\" if operation store fails\n\t* @throws {Error} If reducer throws during operation application\n\t* @throws {Error} If document serialization fails\n\t*/\n\tasync getState(documentId, scope, branch, targetRevision, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst streamKey = this.makeStreamKey(documentId, scope, branch);\n\t\tconst stream = this.streams.get(streamKey);\n\t\tif (stream) {\n\t\t\tconst snapshots = stream.ringBuffer.getAll();\n\t\t\tif (targetRevision === void 0) {\n\t\t\t\tif (snapshots.length > 0) {\n\t\t\t\t\tconst newest = snapshots[snapshots.length - 1];\n\t\t\t\t\tthis.lruTracker.touch(streamKey);\n\t\t\t\t\treturn newest.document;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tconst exactMatch = snapshots.find((s) => s.revision === targetRevision);\n\t\t\t\tif (exactMatch) {\n\t\t\t\t\tthis.lruTracker.touch(streamKey);\n\t\t\t\t\treturn exactMatch.document;\n\t\t\t\t}\n\t\t\t\tconst newestOlder = this.findNearestOlderSnapshot(snapshots, targetRevision);\n\t\t\t\tif (newestOlder) {\n\t\t\t\t\tconst document = await this.warmMissRebuild(newestOlder.document, newestOlder.revision, documentId, scope, branch, targetRevision, signal);\n\t\t\t\t\tthis.putState(documentId, scope, branch, targetRevision, document);\n\t\t\t\t\tthis.lruTracker.touch(streamKey);\n\t\t\t\t\treturn document;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst document = await this.coldMissRebuild(documentId, scope, branch, targetRevision, signal);\n\t\tlet revision = targetRevision;\n\t\tif (revision === void 0) revision = document.header.revision[scope] || 0;\n\t\tthis.putState(documentId, scope, branch, revision, document);\n\t\treturn document;\n\t}\n\t/**\n\t* Stores a document snapshot in the cache at a specific revision.\n\t*\n\t* The cached document is a shallow copy of the input with its operation history\n\t* truncated to the last operation per scope and its clipboard cleared. This keeps\n\t* memory use and copy costs constant regardless of operation count. Consumers of\n\t* getState() must not rely on the full operation history being present; the only\n\t* guaranteed invariant is that operations[scope].at(-1) reflects the latest\n\t* operation index for each scope.\n\t*\n\t* Updates LRU tracker and may evict least recently used stream if at capacity.\n\t* Asynchronously persists keyframes at configured intervals (fire-and-forget).\n\t*\n\t* @param documentId - The document identifier\n\t* @param scope - The operation scope\n\t* @param branch - The operation branch\n\t* @param revision - The revision number\n\t* @param document - The document to cache\n\t* @throws {Error} If document serialization fails\n\t*/\n\tputState(documentId, scope, branch, revision, document) {\n\t\tconst streamKey = this.makeStreamKey(documentId, scope, branch);\n\t\tconst stream = this.getOrCreateStream(streamKey);\n\t\tconst snapshot = {\n\t\t\trevision,\n\t\t\tdocument: {\n\t\t\t\t...document,\n\t\t\t\toperations: Object.fromEntries(Object.entries(document.operations).map(([k, ops]) => [k, ops.length ? [ops.at(-1)] : []])),\n\t\t\t\tclipboard: []\n\t\t\t}\n\t\t};\n\t\tstream.ringBuffer.push(snapshot);\n\t\tif (this.isKeyframeRevision(revision)) this.keyframeStore.putKeyframe(documentId, scope, branch, revision, {\n\t\t\t...document,\n\t\t\toperations: {},\n\t\t\tclipboard: []\n\t\t}).catch((err) => {\n\t\t\tconsole.error(`Failed to persist keyframe ${documentId}@${revision}:`, err);\n\t\t});\n\t}\n\t/**\n\t* Invalidates cached document streams.\n\t*\n\t* Supports three invalidation scopes:\n\t* - Document-level: invalidate(documentId) - removes all streams for document\n\t* - Scope-level: invalidate(documentId, scope) - removes all branches for scope\n\t* - Stream-level: invalidate(documentId, scope, branch) - removes specific stream\n\t*\n\t* @param documentId - The document identifier\n\t* @param scope - Optional scope to narrow invalidation\n\t* @param branch - Optional branch to narrow invalidation (requires scope)\n\t* @returns The number of streams evicted\n\t*/\n\tinvalidate(documentId, scope, branch) {\n\t\tlet evicted = 0;\n\t\tif (scope === void 0 && branch === void 0) {\n\t\t\tfor (const [key] of this.streams.entries()) if (key.startsWith(`${documentId}:`)) {\n\t\t\t\tthis.streams.delete(key);\n\t\t\t\tthis.lruTracker.remove(key);\n\t\t\t\tevicted++;\n\t\t\t}\n\t\t} else if (scope !== void 0 && branch === void 0) {\n\t\t\tfor (const [key] of this.streams.entries()) if (key.startsWith(`${documentId}:${scope}:`)) {\n\t\t\t\tthis.streams.delete(key);\n\t\t\t\tthis.lruTracker.remove(key);\n\t\t\t\tevicted++;\n\t\t\t}\n\t\t} else if (scope !== void 0 && branch !== void 0) {\n\t\t\tconst key = this.makeStreamKey(documentId, scope, branch);\n\t\t\tif (this.streams.has(key)) {\n\t\t\t\tthis.streams.delete(key);\n\t\t\t\tthis.lruTracker.remove(key);\n\t\t\t\tevicted = 1;\n\t\t\t}\n\t\t}\n\t\treturn evicted;\n\t}\n\t/**\n\t* Clears the entire cache, removing all cached document streams.\n\t* Resets LRU tracking state. This operation always succeeds.\n\t*/\n\tclear() {\n\t\tthis.streams.clear();\n\t\tthis.lruTracker.clear();\n\t}\n\t/**\n\t* Retrieves a specific stream for a document. Exposed on the implementation\n\t* for testing, but not on the interface.\n\t*\n\t* @internal\n\t*/\n\tgetStream(documentId, scope, branch) {\n\t\tconst key = this.makeStreamKey(documentId, scope, branch);\n\t\treturn this.streams.get(key);\n\t}\n\tasync findNearestKeyframe(documentId, scope, branch, targetRevision, signal) {\n\t\tif (targetRevision === Number.MAX_SAFE_INTEGER || targetRevision <= 0) return;\n\t\treturn this.keyframeStore.findNearestKeyframe(documentId, scope, branch, targetRevision, signal);\n\t}\n\tasync coldMissRebuild(documentId, scope, branch, targetRevision, signal) {\n\t\tconst effectiveTargetRevision = targetRevision || Number.MAX_SAFE_INTEGER;\n\t\tconst keyframe = await this.findNearestKeyframe(documentId, scope, branch, effectiveTargetRevision, signal);\n\t\tlet document;\n\t\tlet startRevision;\n\t\tlet documentType;\n\t\tconst validatedUpgrades = [];\n\t\tif (keyframe) {\n\t\t\tdocument = keyframe.document;\n\t\t\tstartRevision = keyframe.revision;\n\t\t\tdocumentType = keyframe.document.header.documentType;\n\t\t\tconst docScopeOpsAfterKeyframe = await this.operationStore.getSince(documentId, \"document\", branch, keyframe.revision, void 0, void 0, signal);\n\t\t\tfor (const operation of docScopeOpsAfterKeyframe.results) if (operation.action.type === \"UPGRADE_DOCUMENT\") {\n\t\t\t\tconst upgradeAction = operation.action;\n\t\t\t\tconst fromVersion = upgradeAction.input.fromVersion;\n\t\t\t\tconst toVersion = upgradeAction.input.toVersion;\n\t\t\t\tif (fromVersion > 0 && fromVersion < toVersion) {\n\t\t\t\t\tlet upgradePath;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tupgradePath = this.registry.computeUpgradePath(documentType, fromVersion, toVersion);\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tif (upgradeAction.input.initialState !== void 0) upgradePath = void 0;\n\t\t\t\t\t\telse throw new Error(`Failed to rebuild document ${documentId}: no upgrade manifest for ${documentType} v${fromVersion}→v${toVersion} and no initialState snapshot. ${err instanceof Error ? err.message : String(err)}`, { cause: err });\n\t\t\t\t\t}\n\t\t\t\t\tvalidatedUpgrades.push({\n\t\t\t\t\t\tfromVersion,\n\t\t\t\t\t\ttoVersion,\n\t\t\t\t\t\trevision: upgradeAction.input.revision,\n\t\t\t\t\t\ttimestampUtcMs: operation.timestampUtcMs\n\t\t\t\t\t});\n\t\t\t\t\tdocument = applyUpgradeDocumentAction(document, upgradeAction, upgradePath);\n\t\t\t\t}\n\t\t\t} else if (operation.action.type === \"DELETE_DOCUMENT\") applyDeleteDocumentAction(document, operation.action);\n\t\t} else {\n\t\t\tstartRevision = -1;\n\t\t\tconst createOpResult = await this.operationStore.getSince(documentId, \"document\", branch, -1, void 0, {\n\t\t\t\tcursor: \"0\",\n\t\t\t\tlimit: 1\n\t\t\t}, signal);\n\t\t\tif (createOpResult.results.length === 0) throw new Error(`Failed to rebuild document ${documentId}: no CREATE_DOCUMENT operation found in document scope`);\n\t\t\tconst createOp = createOpResult.results[0];\n\t\t\tif (createOp.action.type !== \"CREATE_DOCUMENT\") throw new Error(`Failed to rebuild document ${documentId}: first operation in document scope must be CREATE_DOCUMENT, found ${createOp.action.type}`);\n\t\t\tconst documentCreateAction = createOp.action;\n\t\t\tdocumentType = documentCreateAction.input.model;\n\t\t\tif (!documentType) throw new Error(`Failed to rebuild document ${documentId}: CREATE_DOCUMENT action missing model in input`);\n\t\t\tdocument = createDocumentFromAction(documentCreateAction);\n\t\t\tlet docModule = this.registry.getModule(documentType, extractModuleVersion(document));\n\t\t\tconst docScopeOps = await this.operationStore.getSince(documentId, \"document\", branch, 0, void 0, void 0, signal);\n\t\t\tfor (const operation of docScopeOps.results) {\n\t\t\t\tif (operation.index === 0) continue;\n\t\t\t\tif (operation.action.type === \"UPGRADE_DOCUMENT\") {\n\t\t\t\t\tconst upgradeAction = operation.action;\n\t\t\t\t\tconst fromVersion = upgradeAction.input.fromVersion;\n\t\t\t\t\tconst toVersion = upgradeAction.input.toVersion;\n\t\t\t\t\tlet upgradePath;\n\t\t\t\t\tif (fromVersion > 0 && fromVersion < toVersion) {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tupgradePath = this.registry.computeUpgradePath(documentType, fromVersion, toVersion);\n\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\tif (upgradeAction.input.initialState !== void 0) upgradePath = void 0;\n\t\t\t\t\t\t\telse throw new Error(`Failed to rebuild document ${documentId}: no upgrade manifest for ${documentType} v${fromVersion}→v${toVersion} and no initialState snapshot. ${err instanceof Error ? err.message : String(err)}`, { cause: err });\n\t\t\t\t\t\t}\n\t\t\t\t\t\tvalidatedUpgrades.push({\n\t\t\t\t\t\t\tfromVersion,\n\t\t\t\t\t\t\ttoVersion,\n\t\t\t\t\t\t\trevision: upgradeAction.input.revision,\n\t\t\t\t\t\t\ttimestampUtcMs: operation.timestampUtcMs\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tdocument = applyUpgradeDocumentAction(document, upgradeAction, upgradePath);\n\t\t\t\t\tdocModule = this.registry.getModule(documentType, extractModuleVersion(document));\n\t\t\t\t} else if (operation.action.type === \"DELETE_DOCUMENT\") applyDeleteDocumentAction(document, operation.action);\n\t\t\t\telse {\n\t\t\t\t\tconst protocolVersion = document.header.protocolVersions?.[\"base-reducer\"] ?? 1;\n\t\t\t\t\tdocument = docModule.reducer(document, operation.action, void 0, {\n\t\t\t\t\t\tskip: operation.skip,\n\t\t\t\t\t\tprotocolVersion\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tconst moduleCache = /* @__PURE__ */ new Map();\n\t\tconst getModuleCached = (version) => {\n\t\t\tconst key = version ?? 0;\n\t\t\tlet mod = moduleCache.get(key);\n\t\t\tif (!mod) {\n\t\t\t\tmod = this.registry.getModule(documentType, version);\n\t\t\t\tmoduleCache.set(key, mod);\n\t\t\t}\n\t\t\treturn mod;\n\t\t};\n\t\tlet cursor = void 0;\n\t\tconst pageSize = 100;\n\t\tlet hasMorePages;\n\t\tdo {\n\t\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\t\tconst paging = {\n\t\t\t\tcursor: cursor || \"0\",\n\t\t\t\tlimit: pageSize\n\t\t\t};\n\t\t\ttry {\n\t\t\t\tconst result = await this.operationStore.getSince(documentId, scope, branch, startRevision, void 0, paging, signal);\n\t\t\t\tfor (const operation of result.results) {\n\t\t\t\t\tif (targetRevision !== void 0 && operation.index > targetRevision) break;\n\t\t\t\t\tconst moduleVersion = this.resolveModuleVersionForOp(operation.index, operation.timestampUtcMs, scope, validatedUpgrades, extractModuleVersion(document));\n\t\t\t\t\tconst protocolVersion = document.header.protocolVersions?.[\"base-reducer\"] ?? 1;\n\t\t\t\t\tdocument = getModuleCached(moduleVersion).reducer(document, operation.action, void 0, {\n\t\t\t\t\t\tskip: operation.skip,\n\t\t\t\t\t\tprotocolVersion\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tconst reachedTarget = targetRevision !== void 0 && result.results.some((op) => op.index >= targetRevision);\n\t\t\t\thasMorePages = Boolean(result.nextCursor) && !reachedTarget;\n\t\t\t\tif (hasMorePages) cursor = result.nextCursor;\n\t\t\t} catch (err) {\n\t\t\t\tthrow new Error(`Failed to rebuild document ${documentId}: ${err instanceof Error ? err.message : String(err)}`, { cause: err });\n\t\t\t}\n\t\t} while (hasMorePages);\n\t\tconst revisions = await this.operationStore.getRevisions(documentId, branch, signal);\n\t\tdocument.header.revision = revisions.revision;\n\t\tdocument.header.lastModifiedAtUtcIso = revisions.latestTimestamp;\n\t\treturn document;\n\t}\n\t/**\n\t* Resolves which module version to use for a given operation in phase 2.\n\t*\n\t* Uses the validated-upgrade boundary rules from D7:\n\t* - If `input.revision` is present: op.index < revision[scope] → before the upgrade boundary\n\t* - Otherwise: timestamp fallback\n\t* - Falls back to final module version when neither is decidable\n\t*/\n\tresolveModuleVersionForOp(opIndex, opTimestamp, scope, validatedUpgrades, finalVersion) {\n\t\tif (validatedUpgrades.length === 0) return finalVersion;\n\t\tlet currentVersion = validatedUpgrades[0]?.fromVersion;\n\t\tfor (const upgrade of validatedUpgrades) {\n\t\t\tlet beforeUpgrade;\n\t\t\tif (upgrade.revision !== void 0) beforeUpgrade = opIndex < (upgrade.revision[scope] ?? 0);\n\t\t\telse beforeUpgrade = opTimestamp < upgrade.timestampUtcMs;\n\t\t\tif (beforeUpgrade) return currentVersion;\n\t\t\tcurrentVersion = upgrade.toVersion;\n\t\t}\n\t\treturn currentVersion;\n\t}\n\tasync warmMissRebuild(baseDocument, baseRevision, documentId, scope, branch, targetRevision, signal) {\n\t\tconst documentType = baseDocument.header.documentType;\n\t\tconst docScopeNextIndex = baseDocument.header.revision[\"document\"] ?? 0;\n\t\tif ((await this.operationStore.getSince(documentId, \"document\", branch, docScopeNextIndex - 1, void 0, void 0, signal)).results.some((op) => op.action.type === \"UPGRADE_DOCUMENT\")) return this.coldMissRebuild(documentId, scope, branch, targetRevision, signal);\n\t\tconst module = this.registry.getModule(documentType, extractModuleVersion(baseDocument));\n\t\tlet document = baseDocument;\n\t\ttry {\n\t\t\tconst pagedResults = await this.operationStore.getSince(documentId, scope, branch, baseRevision, void 0, void 0, signal);\n\t\t\tfor (const operation of pagedResults.results) {\n\t\t\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\t\t\tif (targetRevision !== void 0 && operation.index > targetRevision) break;\n\t\t\t\tconst protocolVersion = document.header.protocolVersions?.[\"base-reducer\"] ?? 1;\n\t\t\t\tdocument = module.reducer(document, operation.action, void 0, {\n\t\t\t\t\tskip: operation.skip,\n\t\t\t\t\tprotocolVersion\n\t\t\t\t});\n\t\t\t\tif (targetRevision !== void 0 && operation.index === targetRevision) break;\n\t\t\t}\n\t\t} catch (err) {\n\t\t\tthrow new Error(`Failed to rebuild document ${documentId}: ${err instanceof Error ? err.message : String(err)}`, { cause: err });\n\t\t}\n\t\tconst revisions = await this.operationStore.getRevisions(documentId, branch, signal);\n\t\tdocument.header.revision = revisions.revision;\n\t\tdocument.header.lastModifiedAtUtcIso = revisions.latestTimestamp;\n\t\treturn document;\n\t}\n\tfindNearestOlderSnapshot(snapshots, targetRevision) {\n\t\tlet nearest = void 0;\n\t\tfor (const snapshot of snapshots) if (snapshot.revision < targetRevision) {\n\t\t\tif (!nearest || snapshot.revision > nearest.revision) nearest = snapshot;\n\t\t}\n\t\treturn nearest;\n\t}\n\tmakeStreamKey(documentId, scope, branch) {\n\t\treturn `${documentId}:${scope}:${branch}`;\n\t}\n\tgetOrCreateStream(key) {\n\t\tlet stream = this.streams.get(key);\n\t\tif (!stream) {\n\t\t\tif (this.streams.size >= this.config.maxDocuments) {\n\t\t\t\tconst evictKey = this.lruTracker.evict();\n\t\t\t\tif (evictKey) this.streams.delete(evictKey);\n\t\t\t}\n\t\t\tstream = {\n\t\t\t\tkey,\n\t\t\t\tringBuffer: new RingBuffer(this.config.ringBufferSize)\n\t\t\t};\n\t\t\tthis.streams.set(key, stream);\n\t\t}\n\t\tthis.lruTracker.touch(key);\n\t\treturn stream;\n\t}\n\tisKeyframeRevision(revision) {\n\t\treturn revision > 0 && revision % this.config.keyframeInterval === 0;\n\t}\n};\n//#endregion\n//#region src/events/event-bus.ts\nvar EventBus = class {\n\teventTypeToSubscribers = /* @__PURE__ */ new Map();\n\tsubscribe(type, subscriber) {\n\t\tlet list = this.eventTypeToSubscribers.get(type);\n\t\tif (!list) {\n\t\t\tlist = [];\n\t\t\tthis.eventTypeToSubscribers.set(type, list);\n\t\t}\n\t\tlist.push(subscriber);\n\t\tlet done = false;\n\t\treturn () => {\n\t\t\tif (done) return;\n\t\t\tdone = true;\n\t\t\tconst arr = this.eventTypeToSubscribers.get(type);\n\t\t\tif (!arr) return;\n\t\t\tconst idx = arr.indexOf(subscriber);\n\t\t\tif (idx !== -1) arr.splice(idx, 1);\n\t\t\tif (arr.length === 0) this.eventTypeToSubscribers.delete(type);\n\t\t};\n\t}\n\tasync emit(type, data) {\n\t\tconst list = this.eventTypeToSubscribers.get(type);\n\t\tif (!list || list.length === 0) return;\n\t\tconst snapshot = list.slice();\n\t\tconst errors = [];\n\t\tfor (const fn of snapshot) try {\n\t\t\tawait Promise.resolve(fn(type, data));\n\t\t} catch (err) {\n\t\t\terrors.push(err);\n\t\t}\n\t\tif (errors.length > 0) throw new EventBusAggregateError(errors);\n\t}\n};\n//#endregion\n//#region src/executor/execution-scope.ts\nvar DefaultExecutionScope = class {\n\tconstructor(operationStore, operationIndex, writeCache, documentMetaCache, collectionMembershipCache) {\n\t\tthis.operationStore = operationStore;\n\t\tthis.operationIndex = operationIndex;\n\t\tthis.writeCache = writeCache;\n\t\tthis.documentMetaCache = documentMetaCache;\n\t\tthis.collectionMembershipCache = collectionMembershipCache;\n\t}\n\tasync run(fn, signal) {\n\t\tsignal?.throwIfAborted();\n\t\treturn fn({\n\t\t\toperationStore: this.operationStore,\n\t\t\toperationIndex: this.operationIndex,\n\t\t\twriteCache: this.writeCache,\n\t\t\tdocumentMetaCache: this.documentMetaCache,\n\t\t\tcollectionMembershipCache: this.collectionMembershipCache\n\t\t});\n\t}\n};\nvar KyselyExecutionScope = class {\n\tconstructor(db, operationStore, operationIndex, keyframeStore, writeCache, documentMetaCache, collectionMembershipCache) {\n\t\tthis.db = db;\n\t\tthis.operationStore = operationStore;\n\t\tthis.operationIndex = operationIndex;\n\t\tthis.keyframeStore = keyframeStore;\n\t\tthis.writeCache = writeCache;\n\t\tthis.documentMetaCache = documentMetaCache;\n\t\tthis.collectionMembershipCache = collectionMembershipCache;\n\t}\n\tasync run(fn, signal) {\n\t\tsignal?.throwIfAborted();\n\t\treturn this.db.transaction().execute(async (trx) => {\n\t\t\tconst scopedOperationStore = this.operationStore.withTransaction(trx);\n\t\t\tconst scopedOperationIndex = this.operationIndex.withTransaction(trx);\n\t\t\tconst scopedKeyframeStore = this.keyframeStore.withTransaction(trx);\n\t\t\treturn fn({\n\t\t\t\toperationStore: scopedOperationStore,\n\t\t\t\toperationIndex: scopedOperationIndex,\n\t\t\t\twriteCache: this.writeCache.withScopedStores(scopedOperationStore, scopedKeyframeStore),\n\t\t\t\tdocumentMetaCache: this.documentMetaCache.withScopedStore(scopedOperationStore),\n\t\t\t\tcollectionMembershipCache: this.collectionMembershipCache.withScopedIndex(scopedOperationIndex)\n\t\t\t});\n\t\t});\n\t}\n};\n//#endregion\n//#region src/utils/reshuffle.ts\nconst STRICT_ORDER_ACTION_TYPES = new Set([\n\t\"CREATE_DOCUMENT\",\n\t\"DELETE_DOCUMENT\",\n\t\"UPGRADE_DOCUMENT\",\n\t\"ADD_RELATIONSHIP\",\n\t\"REMOVE_RELATIONSHIP\",\n\t\"UPDATE_RELATIONSHIP\",\n\t\"ADD_FOLDER\",\n\t\"UPDATE_FOLDER\",\n\t\"REMOVE_FOLDER\"\n]);\n/**\n* Reshuffles operations by timestamp, then applies deterministic tie-breaking.\n* Used for merging concurrent operations from different branches.\n*\n* For strict document-structure actions (e.g., CREATE_DOCUMENT/UPGRADE_DOCUMENT),\n* logical index (index - skip) is prioritized to preserve causal replay order.\n*\n* For other actions, action ID is prioritized to ensure a canonical cross-reactor order\n* for concurrent operations that may have diverged local indices due to prior reshuffles.\n* Logical index and operation ID are then used as deterministic tie-breakers.\n*\n* Example:\n* [0:0, 1:0, 2:0, A3:0, A4:0, A5:0] + [0:0, 1:0, 2:0, B3:0, B4:2, B5:0]\n* GC => [0:0, 1:0, 2:0, A3:0, A4:0, A5:0] + [0:0, 1:0, B4:2, B5:0]\n* Split => [0:0, 1:0] + [2:0, A3:0, A4:0, A5:0] + [B4:2, B5:0]\n* Reshuffle(6:4) => [6:4, 7:0, 8:0, 9:0, 10:0, 11:0]\n* merge => [0:0, 1:0, 6:4, 7:0, 8:0, 9:0, 10:0, 11:0]\n*/\nfunction reshuffleByTimestamp(startIndex, opsA, opsB) {\n\treturn [...opsA, ...opsB].sort((a, b) => {\n\t\tconst timestampDiff = new Date(a.timestampUtcMs).getTime() - new Date(b.timestampUtcMs).getTime();\n\t\tif (timestampDiff !== 0) return timestampDiff;\n\t\tconst shouldPrioritizeLogicalIndex = STRICT_ORDER_ACTION_TYPES.has(a.action?.type ?? \"\") || STRICT_ORDER_ACTION_TYPES.has(b.action?.type ?? \"\");\n\t\tconst logicalIndexDiff = a.index - a.skip - (b.index - b.skip);\n\t\tif (shouldPrioritizeLogicalIndex) {\n\t\t\tif (logicalIndexDiff !== 0) return logicalIndexDiff;\n\t\t}\n\t\tconst actionIdDiff = (a.action?.id ?? \"\").localeCompare(b.action?.id ?? \"\");\n\t\tif (actionIdDiff !== 0) return actionIdDiff;\n\t\tif (!shouldPrioritizeLogicalIndex && logicalIndexDiff !== 0) return logicalIndexDiff;\n\t\treturn a.id.localeCompare(b.id);\n\t}).map((op, i) => ({\n\t\t...op,\n\t\tindex: startIndex.index + i,\n\t\tskip: i === 0 ? startIndex.skip : 0\n\t}));\n}\n//#endregion\n//#region src/cache/operation-index-types.ts\nfunction driveCollectionId(branch, driveId) {\n\treturn `drive.${branch}.${driveId}`;\n}\n//#endregion\n//#region src/executor/document-action-handler.ts\nvar DocumentActionHandler = class {\n\tconstructor(registry, logger, driveContainerTypes) {\n\t\tthis.registry = registry;\n\t\tthis.logger = logger;\n\t\tthis.driveContainerTypes = driveContainerTypes;\n\t}\n\tasync execute(job, action, startTime, indexTxn, stores, skip = 0, sourceRemote = \"\", signal) {\n\t\tswitch (action.type) {\n\t\t\tcase \"CREATE_DOCUMENT\": return this.executeCreate(job, action, startTime, indexTxn, stores, skip, sourceRemote, signal);\n\t\t\tcase \"DELETE_DOCUMENT\": return this.executeDelete(job, action, startTime, indexTxn, stores, sourceRemote, signal);\n\t\t\tcase \"UPGRADE_DOCUMENT\": return this.executeUpgrade(job, action, startTime, indexTxn, stores, skip, sourceRemote, signal);\n\t\t\tcase \"ADD_RELATIONSHIP\": return this.executeAddRelationship(job, action, startTime, indexTxn, stores, sourceRemote, signal);\n\t\t\tcase \"REMOVE_RELATIONSHIP\": return this.executeRemoveRelationship(job, action, startTime, indexTxn, stores, sourceRemote, signal);\n\t\t\tcase \"UPDATE_RELATIONSHIP\": return this.executeUpdateRelationship(job, action, startTime, indexTxn, stores, sourceRemote, signal);\n\t\t\tdefault: return buildErrorResult(job, /* @__PURE__ */ new Error(`Unknown document action type: ${action.type}`), startTime);\n\t\t}\n\t}\n\tasync executeCreate(job, action, startTime, indexTxn, stores, skip = 0, sourceRemote = \"\", signal) {\n\t\tif (job.scope !== \"document\") return {\n\t\t\tjob,\n\t\t\tsuccess: false,\n\t\t\terror: /* @__PURE__ */ new Error(`CREATE_DOCUMENT must be in \"document\" scope, got \"${job.scope}\"`),\n\t\t\tduration: Date.now() - startTime\n\t\t};\n\t\tconst document = createDocumentFromAction(action);\n\t\tlet operation = createOperation(action, 0, skip, {\n\t\t\tdocumentId: document.header.id,\n\t\t\tscope: job.scope,\n\t\t\tbranch: job.branch\n\t\t});\n\t\tconst resultingStateObj = {\n\t\t\theader: document.header,\n\t\t\t...document.state\n\t\t};\n\t\tconst resultingState = JSON.stringify(resultingStateObj);\n\t\tconst writeResult = await this.writeOperationToStore(document.header.id, document.header.documentType, job.scope, job.branch, operation, job, startTime, stores, signal);\n\t\tif (!Array.isArray(writeResult)) return writeResult;\n\t\toperation = writeResult[0];\n\t\tupdateDocumentRevision(document, job.scope, operation.index);\n\t\tdocument.operations = {\n\t\t\t...document.operations,\n\t\t\t[job.scope]: [...document.operations[job.scope] ?? [], operation]\n\t\t};\n\t\tstores.writeCache.putState(document.header.id, job.scope, job.branch, operation.index, document);\n\t\tindexTxn.write([{\n\t\t\t...operation,\n\t\t\tdocumentId: document.header.id,\n\t\t\tdocumentType: document.header.documentType,\n\t\t\tbranch: job.branch,\n\t\t\tscope: job.scope,\n\t\t\tsourceRemote\n\t\t}]);\n\t\tif (this.driveContainerTypes.has(document.header.documentType)) {\n\t\t\tconst collectionId = driveCollectionId(job.branch, document.header.id);\n\t\t\tindexTxn.createCollection(collectionId);\n\t\t\tindexTxn.addToCollection(collectionId, document.header.id);\n\t\t}\n\t\tstores.documentMetaCache.putDocumentMeta(document.header.id, job.branch, {\n\t\t\tstate: document.state.document,\n\t\t\tdocumentType: document.header.documentType,\n\t\t\tdocumentScopeRevision: 1\n\t\t});\n\t\treturn buildSuccessResult(job, operation, document.header.id, document.header.documentType, resultingState, startTime);\n\t}\n\tasync executeDelete(job, action, startTime, indexTxn, stores, sourceRemote = \"\", signal) {\n\t\tconst input = action.input;\n\t\tif (!input.documentId) return buildErrorResult(job, /* @__PURE__ */ new Error(\"DELETE_DOCUMENT action requires a documentId in input\"), startTime);\n\t\tconst documentId = input.documentId;\n\t\tlet document;\n\t\ttry {\n\t\t\tdocument = await stores.writeCache.getState(documentId, job.scope, job.branch, void 0, signal);\n\t\t} catch (error) {\n\t\t\treturn buildErrorResult(job, /* @__PURE__ */ new Error(`Failed to fetch document before deletion: ${error instanceof Error ? error.message : String(error)}`), startTime);\n\t\t}\n\t\tconst documentState = document.state.document;\n\t\tif (documentState.isDeleted) return buildErrorResult(job, new DocumentDeletedError(documentId, documentState.deletedAtUtcIso), startTime);\n\t\tlet operation = createOperation(action, getNextIndexForScope(document, job.scope), 0, {\n\t\t\tdocumentId,\n\t\t\tscope: job.scope,\n\t\t\tbranch: job.branch\n\t\t});\n\t\tapplyDeleteDocumentAction$1(document, action);\n\t\tconst resultingStateObj = {\n\t\t\theader: document.header,\n\t\t\tdocument: document.state.document\n\t\t};\n\t\tconst resultingState = JSON.stringify(resultingStateObj);\n\t\tconst writeResult = await this.writeOperationToStore(documentId, document.header.documentType, job.scope, job.branch, operation, job, startTime, stores, signal);\n\t\tif (!Array.isArray(writeResult)) return writeResult;\n\t\toperation = writeResult[0];\n\t\tupdateDocumentRevision(document, job.scope, operation.index);\n\t\tdocument.operations = {\n\t\t\t...document.operations,\n\t\t\t[job.scope]: [...document.operations[job.scope] ?? [], operation]\n\t\t};\n\t\tstores.writeCache.putState(documentId, job.scope, job.branch, operation.index, document);\n\t\tindexTxn.write([{\n\t\t\t...operation,\n\t\t\tdocumentId,\n\t\t\tdocumentType: document.header.documentType,\n\t\t\tbranch: job.branch,\n\t\t\tscope: job.scope,\n\t\t\tsourceRemote\n\t\t}]);\n\t\tstores.documentMetaCache.putDocumentMeta(documentId, job.branch, {\n\t\t\tstate: document.state.document,\n\t\t\tdocumentType: document.header.documentType,\n\t\t\tdocumentScopeRevision: operation.index + 1\n\t\t});\n\t\treturn buildSuccessResult(job, operation, documentId, document.header.documentType, resultingState, startTime);\n\t}\n\tasync executeUpgrade(job, action, startTime, indexTxn, stores, skip = 0, sourceRemote = \"\", signal) {\n\t\tconst input = action.input;\n\t\tif (!input.documentId) return buildErrorResult(job, /* @__PURE__ */ new Error(\"UPGRADE_DOCUMENT action requires a documentId in input\"), startTime);\n\t\tconst documentId = input.documentId;\n\t\tconst fromVersion = input.fromVersion;\n\t\tconst toVersion = input.toVersion;\n\t\tlet document;\n\t\ttry {\n\t\t\tdocument = await stores.writeCache.getState(documentId, job.scope, job.branch, void 0, signal);\n\t\t} catch (error) {\n\t\t\treturn buildErrorResult(job, /* @__PURE__ */ new Error(`Failed to fetch document for upgrade: ${error instanceof Error ? error.message : String(error)}`), startTime);\n\t\t}\n\t\tconst documentState = document.state.document;\n\t\tif (documentState.isDeleted) return buildErrorResult(job, new DocumentDeletedError(documentId, documentState.deletedAtUtcIso), startTime);\n\t\tconst nextIndex = getNextIndexForScope(document, job.scope);\n\t\tlet upgradePath;\n\t\tif (fromVersion > 0 && fromVersion < toVersion) try {\n\t\t\tupgradePath = this.registry.computeUpgradePath(document.header.documentType, fromVersion, toVersion);\n\t\t} catch (error) {\n\t\t\treturn buildErrorResult(job, error instanceof Error ? error : new Error(String(error)), startTime);\n\t\t}\n\t\tif (fromVersion === toVersion && fromVersion > 0) return {\n\t\t\tjob,\n\t\t\tsuccess: true,\n\t\t\toperations: [],\n\t\t\toperationsWithContext: [],\n\t\t\tduration: Date.now() - startTime\n\t\t};\n\t\ttry {\n\t\t\tdocument = applyUpgradeDocumentAction$1(document, action, upgradePath);\n\t\t} catch (error) {\n\t\t\treturn buildErrorResult(job, error instanceof Error ? error : new Error(String(error)), startTime);\n\t\t}\n\t\tlet operation = createOperation(action, nextIndex, skip, {\n\t\t\tdocumentId,\n\t\t\tscope: job.scope,\n\t\t\tbranch: job.branch\n\t\t});\n\t\tconst resultingStateObj = {\n\t\t\theader: document.header,\n\t\t\t...document.state\n\t\t};\n\t\tconst resultingState = JSON.stringify(resultingStateObj);\n\t\tconst writeResult = await this.writeOperationToStore(documentId, document.header.documentType, job.scope, job.branch, operation, job, startTime, stores, signal);\n\t\tif (!Array.isArray(writeResult)) return writeResult;\n\t\toperation = writeResult[0];\n\t\tupdateDocumentRevision(document, job.scope, operation.index);\n\t\tdocument.operations = {\n\t\t\t...document.operations,\n\t\t\t[job.scope]: [...document.operations[job.scope] ?? [], operation]\n\t\t};\n\t\tstores.writeCache.putState(documentId, job.scope, job.branch, operation.index, document);\n\t\tindexTxn.write([{\n\t\t\t...operation,\n\t\t\tdocumentId,\n\t\t\tdocumentType: document.header.documentType,\n\t\t\tbranch: job.branch,\n\t\t\tscope: job.scope,\n\t\t\tsourceRemote\n\t\t}]);\n\t\tstores.documentMetaCache.putDocumentMeta(documentId, job.branch, {\n\t\t\tstate: document.state.document,\n\t\t\tdocumentType: document.header.documentType,\n\t\t\tdocumentScopeRevision: operation.index + 1\n\t\t});\n\t\treturn buildSuccessResult(job, operation, documentId, document.header.documentType, resultingState, startTime);\n\t}\n\texecuteAddRelationship(job, action, startTime, indexTxn, stores, sourceRemote = \"\", signal) {\n\t\treturn this.withRelationshipAction(\"ADD_RELATIONSHIP\", job, action, startTime, indexTxn, stores, sourceRemote, signal, (input) => input.sourceId === input.targetId ? /* @__PURE__ */ new Error(\"ADD_RELATIONSHIP: sourceId and targetId cannot be the same (self-relationships not allowed)\") : null, ({ indexTxn: txn, stores: s, sourceDoc, input, job: j }) => {\n\t\t\tif (this.driveContainerTypes.has(sourceDoc.header.documentType)) {\n\t\t\t\tconst collectionId = driveCollectionId(j.branch, input.sourceId);\n\t\t\t\ttxn.addToCollection(collectionId, input.targetId);\n\t\t\t\ts.collectionMembershipCache.invalidate(input.targetId);\n\t\t\t}\n\t\t});\n\t}\n\texecuteRemoveRelationship(job, action, startTime, indexTxn, stores, sourceRemote = \"\", signal) {\n\t\treturn this.withRelationshipAction(\"REMOVE_RELATIONSHIP\", job, action, startTime, indexTxn, stores, sourceRemote, signal, null, ({ indexTxn: txn, stores: s, sourceDoc, input, job: j }) => {\n\t\t\tif (this.driveContainerTypes.has(sourceDoc.header.documentType)) {\n\t\t\t\tconst collectionId = driveCollectionId(j.branch, input.sourceId);\n\t\t\t\ttxn.removeFromCollection(collectionId, input.targetId);\n\t\t\t\ts.collectionMembershipCache.invalidate(input.targetId);\n\t\t\t}\n\t\t});\n\t}\n\texecuteUpdateRelationship(job, action, startTime, indexTxn, stores, sourceRemote = \"\", signal) {\n\t\treturn this.withRelationshipAction(\"UPDATE_RELATIONSHIP\", job, action, startTime, indexTxn, stores, sourceRemote, signal, null, null);\n\t}\n\tasync withRelationshipAction(actionTypeName, job, action, startTime, indexTxn, stores, sourceRemote, signal, preValidate, postWrite) {\n\t\tif (job.scope !== \"document\") return buildErrorResult(job, /* @__PURE__ */ new Error(`${actionTypeName} must be in \"document\" scope, got \"${job.scope}\"`), startTime);\n\t\tconst input = action.input;\n\t\tif (!input.sourceId || !input.targetId || !input.relationshipType) return buildErrorResult(job, /* @__PURE__ */ new Error(`${actionTypeName} action requires sourceId, targetId, and relationshipType in input`), startTime);\n\t\tif (preValidate !== null) {\n\t\t\tconst validationError = preValidate(input);\n\t\t\tif (validationError !== null) return buildErrorResult(job, validationError, startTime);\n\t\t}\n\t\tlet sourceDoc;\n\t\ttry {\n\t\t\tsourceDoc = await stores.writeCache.getState(input.sourceId, \"document\", job.branch, void 0, signal);\n\t\t} catch (error) {\n\t\t\treturn buildErrorResult(job, /* @__PURE__ */ new Error(`${actionTypeName}: source document ${input.sourceId} not found: ${error instanceof Error ? error.message : String(error)}`), startTime);\n\t\t}\n\t\tlet operation = createOperation(action, getNextIndexForScope(sourceDoc, job.scope), 0, {\n\t\t\tdocumentId: input.sourceId,\n\t\t\tscope: job.scope,\n\t\t\tbranch: job.branch\n\t\t});\n\t\tconst writeResult = await this.writeOperationToStore(input.sourceId, sourceDoc.header.documentType, job.scope, job.branch, operation, job, startTime, stores, signal);\n\t\tif (!Array.isArray(writeResult)) return writeResult;\n\t\toperation = writeResult[0];\n\t\tsourceDoc.header.lastModifiedAtUtcIso = operation.timestampUtcMs || (/* @__PURE__ */ new Date()).toISOString();\n\t\tupdateDocumentRevision(sourceDoc, job.scope, operation.index);\n\t\tsourceDoc.operations = {\n\t\t\t...sourceDoc.operations,\n\t\t\t[job.scope]: [...sourceDoc.operations[job.scope] ?? [], operation]\n\t\t};\n\t\tconst scopeState = sourceDoc.state[job.scope];\n\t\tconst resultingStateObj = {\n\t\t\theader: structuredClone(sourceDoc.header),\n\t\t\t[job.scope]: scopeState === void 0 ? {} : structuredClone(scopeState)\n\t\t};\n\t\tconst resultingState = JSON.stringify(resultingStateObj);\n\t\tstores.writeCache.putState(input.sourceId, job.scope, job.branch, operation.index, sourceDoc);\n\t\tindexTxn.write([{\n\t\t\t...operation,\n\t\t\tdocumentId: input.sourceId,\n\t\t\tdocumentType: sourceDoc.header.documentType,\n\t\t\tbranch: job.branch,\n\t\t\tscope: job.scope,\n\t\t\tsourceRemote\n\t\t}]);\n\t\tif (postWrite !== null) postWrite({\n\t\t\tindexTxn,\n\t\t\tstores,\n\t\t\tsourceDoc,\n\t\t\tinput,\n\t\t\tjob\n\t\t});\n\t\tstores.documentMetaCache.putDocumentMeta(input.sourceId, job.branch, {\n\t\t\tstate: sourceDoc.state.document,\n\t\t\tdocumentType: sourceDoc.header.documentType,\n\t\t\tdocumentScopeRevision: operation.index + 1\n\t\t});\n\t\treturn buildSuccessResult(job, operation, input.sourceId, sourceDoc.header.documentType, resultingState, startTime);\n\t}\n\tasync writeOperationToStore(documentId, documentType, scope, branch, operation, job, startTime, stores, signal) {\n\t\tlet storedOperations;\n\t\ttry {\n\t\t\tstoredOperations = await stores.operationStore.apply(documentId, documentType, scope, branch, operation.index, (txn) => {\n\t\t\t\ttxn.addOperations(operation);\n\t\t\t}, signal);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Error writing @Operation to IOperationStore: @Error\", operation, error);\n\t\t\tstores.writeCache.invalidate(documentId, scope, branch);\n\t\t\treturn {\n\t\t\t\tjob,\n\t\t\t\tsuccess: false,\n\t\t\t\terror: /* @__PURE__ */ new Error(`Failed to write operation to IOperationStore: ${error instanceof Error ? error.message : String(error)}`),\n\t\t\t\tduration: Date.now() - startTime\n\t\t\t};\n\t\t}\n\t\treturn storedOperations;\n\t}\n};\n//#endregion\n//#region src/executor/signature-verifier.ts\nvar SignatureVerifier = class {\n\tconstructor(verifier) {\n\t\tthis.verifier = verifier;\n\t}\n\tasync verifyActions(documentId, branch, actions) {\n\t\tif (!this.verifier) return;\n\t\tfor (const action of actions) {\n\t\t\tconst signer = action.context?.signer;\n\t\t\tif (!signer) continue;\n\t\t\tif (signer.signatures.length === 0) throw new InvalidSignatureError(documentId, `Action ${action.id} has signer but no signatures`);\n\t\t\tconst publicKey = signer.app.key;\n\t\t\tlet isValid;\n\t\t\ttry {\n\t\t\t\tconst tempOperation = {\n\t\t\t\t\tid: deriveOperationId(documentId, action.scope, branch, action.id),\n\t\t\t\t\tindex: 0,\n\t\t\t\t\ttimestampUtcMs: action.timestampUtcMs || (/* @__PURE__ */ new Date()).toISOString(),\n\t\t\t\t\thash: \"\",\n\t\t\t\t\tskip: 0,\n\t\t\t\t\taction\n\t\t\t\t};\n\t\t\t\tisValid = await this.verifier(tempOperation, publicKey);\n\t\t\t} catch (error) {\n\t\t\t\tconst errorMessage = error instanceof Error ? error.message : String(error);\n\t\t\t\tthrow new InvalidSignatureError(documentId, `Action ${action.id} verification failed: ${errorMessage}`);\n\t\t\t}\n\t\t\tif (!isValid) throw new InvalidSignatureError(documentId, `Action ${action.id} signature verification returned false`);\n\t\t}\n\t}\n\tasync verifyOperations(documentId, operations) {\n\t\tif (!this.verifier) return;\n\t\tfor (let i = 0; i < operations.length; i++) {\n\t\t\tconst operation = operations[i];\n\t\t\tconst signer = operation.action.context?.signer;\n\t\t\tif (!signer) continue;\n\t\t\tif (signer.signatures.length === 0) throw new InvalidSignatureError(documentId, `Operation ${operation.id} at index ${operation.index} has signer but no signatures`);\n\t\t\tconst publicKey = signer.app.key;\n\t\t\tlet isValid;\n\t\t\ttry {\n\t\t\t\tisValid = await this.verifier(operation, publicKey);\n\t\t\t} catch (error) {\n\t\t\t\tconst errorMessage = error instanceof Error ? error.message : String(error);\n\t\t\t\tthrow new InvalidSignatureError(documentId, `Operation ${operation.id} at index ${operation.index} verification failed: ${errorMessage}`);\n\t\t\t}\n\t\t\tif (!isValid) throw new InvalidSignatureError(documentId, `Operation ${operation.id} at index ${operation.index} signature verification returned false`);\n\t\t}\n\t}\n};\n//#endregion\n//#region src/executor/simple-job-executor.ts\nconst MAX_SKIP_THRESHOLD = 1e3;\nconst ISO_TIMESTAMP_REGEX = /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$/;\nfunction isValidISOTimestamp(value) {\n\tif (!ISO_TIMESTAMP_REGEX.test(value)) return false;\n\treturn !isNaN(new Date(value).getTime());\n}\nconst documentScopeActions = [\n\t\"CREATE_DOCUMENT\",\n\t\"DELETE_DOCUMENT\",\n\t\"UPGRADE_DOCUMENT\",\n\t\"ADD_RELATIONSHIP\",\n\t\"REMOVE_RELATIONSHIP\",\n\t\"UPDATE_RELATIONSHIP\"\n];\n/**\n* Simple job executor that processes a job by applying actions through document model reducers.\n*/\nvar SimpleJobExecutor = class {\n\tconfig;\n\tsignatureVerifierModule;\n\tdocumentActionHandler;\n\texecutionScope;\n\tconstructor(logger, registry, operationStore, eventBus, writeCache, operationIndex, documentMetaCache, collectionMembershipCache, driveContainerTypes, config, signatureVerifier, executionScope) {\n\t\tthis.logger = logger;\n\t\tthis.registry = registry;\n\t\tthis.operationStore = operationStore;\n\t\tthis.eventBus = eventBus;\n\t\tthis.writeCache = writeCache;\n\t\tthis.operationIndex = operationIndex;\n\t\tthis.documentMetaCache = documentMetaCache;\n\t\tthis.collectionMembershipCache = collectionMembershipCache;\n\t\tthis.driveContainerTypes = driveContainerTypes;\n\t\tthis.config = {\n\t\t\tmaxSkipThreshold: config.maxSkipThreshold ?? MAX_SKIP_THRESHOLD,\n\t\t\tmaxConcurrency: config.maxConcurrency ?? 1,\n\t\t\tjobTimeoutMs: config.jobTimeoutMs ?? 3e4,\n\t\t\tretryBaseDelayMs: config.retryBaseDelayMs ?? 100,\n\t\t\tretryMaxDelayMs: config.retryMaxDelayMs ?? 5e3,\n\t\t\tyieldDeadlineMs: config.yieldDeadlineMs ?? 50\n\t\t};\n\t\tthis.signatureVerifierModule = new SignatureVerifier(signatureVerifier);\n\t\tthis.documentActionHandler = new DocumentActionHandler(registry, logger, driveContainerTypes);\n\t\tthis.executionScope = executionScope ?? new DefaultExecutionScope(operationStore, operationIndex, writeCache, documentMetaCache, collectionMembershipCache);\n\t}\n\t/**\n\t* Execute a single job by applying all its actions through the appropriate reducers.\n\t* Actions are processed sequentially in order.\n\t*/\n\tasync executeJob(job, signal) {\n\t\tconst startTime = Date.now();\n\t\tconst touchedCacheEntries = [];\n\t\tlet pendingEvent;\n\t\tlet result;\n\t\ttry {\n\t\t\tresult = await this.executionScope.run(async (stores) => {\n\t\t\t\tconst indexTxn = stores.operationIndex.start();\n\t\t\t\tif (job.kind === \"load\") {\n\t\t\t\t\tconst loadResult = await this.executeLoadJob(job, startTime, indexTxn, stores, signal);\n\t\t\t\t\tif (loadResult.success && loadResult.operationsWithContext) {\n\t\t\t\t\t\tfor (const owc of loadResult.operationsWithContext) touchedCacheEntries.push({\n\t\t\t\t\t\t\tdocumentId: owc.context.documentId,\n\t\t\t\t\t\t\tscope: owc.context.scope,\n\t\t\t\t\t\t\tbranch: owc.context.branch\n\t\t\t\t\t\t});\n\t\t\t\t\t\tconst ordinals = await stores.operationIndex.commit(indexTxn, signal);\n\t\t\t\t\t\tfor (let i = 0; i < loadResult.operationsWithContext.length; i++) loadResult.operationsWithContext[i].context.ordinal = ordinals[i];\n\t\t\t\t\t\tconst collectionMemberships = loadResult.operationsWithContext.length > 0 ? await this.getCollectionMembershipsForOperations(loadResult.operationsWithContext, stores) : {};\n\t\t\t\t\t\tpendingEvent = {\n\t\t\t\t\t\t\tjobId: job.id,\n\t\t\t\t\t\t\toperations: loadResult.operationsWithContext,\n\t\t\t\t\t\t\tjobMeta: job.meta,\n\t\t\t\t\t\t\tcollectionMemberships\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t\treturn loadResult;\n\t\t\t\t}\n\t\t\t\tconst actionResult = await this.processActions(job, job.actions, startTime, indexTxn, stores, void 0, void 0, \"\", signal);\n\t\t\t\tif (!actionResult.success) return {\n\t\t\t\t\tjob,\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\terror: actionResult.error,\n\t\t\t\t\tduration: Date.now() - startTime\n\t\t\t\t};\n\t\t\t\tif (actionResult.operationsWithContext.length > 0) for (const owc of actionResult.operationsWithContext) touchedCacheEntries.push({\n\t\t\t\t\tdocumentId: owc.context.documentId,\n\t\t\t\t\tscope: owc.context.scope,\n\t\t\t\t\tbranch: owc.context.branch\n\t\t\t\t});\n\t\t\t\tconst ordinals = await stores.operationIndex.commit(indexTxn, signal);\n\t\t\t\tif (actionResult.operationsWithContext.length > 0) {\n\t\t\t\t\tfor (let i = 0; i < actionResult.operationsWithContext.length; i++) actionResult.operationsWithContext[i].context.ordinal = ordinals[i];\n\t\t\t\t\tconst collectionMemberships = await this.getCollectionMembershipsForOperations(actionResult.operationsWithContext, stores);\n\t\t\t\t\tpendingEvent = {\n\t\t\t\t\t\tjobId: job.id,\n\t\t\t\t\t\toperations: actionResult.operationsWithContext,\n\t\t\t\t\t\tjobMeta: job.meta,\n\t\t\t\t\t\tcollectionMemberships\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tjob,\n\t\t\t\t\tsuccess: true,\n\t\t\t\t\toperations: actionResult.generatedOperations,\n\t\t\t\t\toperationsWithContext: actionResult.operationsWithContext,\n\t\t\t\t\tduration: Date.now() - startTime\n\t\t\t\t};\n\t\t\t}, signal);\n\t\t} catch (error) {\n\t\t\tfor (const entry of touchedCacheEntries) {\n\t\t\t\tthis.writeCache.invalidate(entry.documentId, entry.scope, entry.branch);\n\t\t\t\tthis.documentMetaCache.invalidate(entry.documentId, entry.branch);\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t\tif (pendingEvent) this.eventBus.emit(ReactorEventTypes.JOB_WRITE_READY, pendingEvent).catch((error) => {\n\t\t\tthis.logger.error(\"Failed to emit JOB_WRITE_READY event: @Event : @Error\", pendingEvent, error);\n\t\t});\n\t\treturn result;\n\t}\n\tasync getCollectionMembershipsForOperations(operations, stores) {\n\t\tconst documentIds = [...new Set(operations.map((op) => op.context.documentId))];\n\t\treturn stores.collectionMembershipCache.getCollectionsForDocuments(documentIds);\n\t}\n\tasync processActions(job, actions, startTime, indexTxn, stores, skipValues, sourceOperations, sourceRemote = \"\", signal) {\n\t\tconst generatedOperations = [];\n\t\tconst operationsWithContext = [];\n\t\ttry {\n\t\t\tawait this.signatureVerifierModule.verifyActions(job.documentId, job.branch, actions);\n\t\t} catch (error) {\n\t\t\treturn {\n\t\t\t\tsuccess: false,\n\t\t\t\tgeneratedOperations,\n\t\t\t\toperationsWithContext,\n\t\t\t\terror: error instanceof Error ? error : new Error(String(error))\n\t\t\t};\n\t\t}\n\t\tfor (const action of actions) if (action.timestampUtcMs && !isValidISOTimestamp(action.timestampUtcMs)) return {\n\t\t\tsuccess: false,\n\t\t\tgeneratedOperations,\n\t\t\toperationsWithContext,\n\t\t\terror: /* @__PURE__ */ new Error(`Invalid timestamp \"${action.timestampUtcMs}\" on action ${action.type} (id: ${action.id})`)\n\t\t};\n\t\tlet lastYield = performance.now();\n\t\tfor (let actionIndex = 0; actionIndex < actions.length; actionIndex++) {\n\t\t\tconst action = actions[actionIndex];\n\t\t\tconst skip = skipValues?.[actionIndex] ?? 0;\n\t\t\tconst sourceOperation = sourceOperations?.[actionIndex];\n\t\t\tconst result = documentScopeActions.includes(action.type) ? await this.documentActionHandler.execute(job, action, startTime, indexTxn, stores, skip, sourceRemote, signal) : await this.executeRegularAction(job, action, startTime, indexTxn, stores, skip, sourceOperation, sourceRemote, signal);\n\t\t\tconst error = this.accumulateResultOrReturnError(result, generatedOperations, operationsWithContext);\n\t\t\tif (error !== null) return {\n\t\t\t\tsuccess: false,\n\t\t\t\tgeneratedOperations,\n\t\t\t\toperationsWithContext,\n\t\t\t\terror: error.error\n\t\t\t};\n\t\t\tif (performance.now() - lastYield > this.config.yieldDeadlineMs) {\n\t\t\t\tawait yieldToMain();\n\t\t\t\tlastYield = performance.now();\n\t\t\t\tif (signal?.aborted) return {\n\t\t\t\t\tsuccess: false,\n\t\t\t\t\tgeneratedOperations,\n\t\t\t\t\toperationsWithContext,\n\t\t\t\t\terror: /* @__PURE__ */ new Error(\"Aborted\")\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\t\treturn {\n\t\t\tsuccess: true,\n\t\t\tgeneratedOperations,\n\t\t\toperationsWithContext\n\t\t};\n\t}\n\tasync executeRegularAction(job, action, startTime, indexTxn, stores, skip = 0, sourceOperation, sourceRemote = \"\", signal) {\n\t\tlet docMeta;\n\t\ttry {\n\t\t\tdocMeta = await stores.documentMetaCache.getDocumentMeta(job.documentId, job.branch, signal);\n\t\t} catch (error) {\n\t\t\treturn buildErrorResult(job, error instanceof Error ? error : new Error(String(error)), startTime);\n\t\t}\n\t\tif (docMeta.state.isDeleted) return buildErrorResult(job, new DocumentDeletedError(job.documentId, docMeta.state.deletedAtUtcIso), startTime);\n\t\tif (isUndoRedo(action) || action.type === \"PRUNE\" || action.type === \"NOOP\" && skip > 0) stores.writeCache.invalidate(job.documentId, job.scope, job.branch);\n\t\tlet document;\n\t\ttry {\n\t\t\tdocument = await stores.writeCache.getState(job.documentId, job.scope, job.branch, void 0, signal);\n\t\t} catch (error) {\n\t\t\treturn buildErrorResult(job, error instanceof Error ? error : new Error(String(error)), startTime);\n\t\t}\n\t\tlet module;\n\t\ttry {\n\t\t\tconst moduleVersion = docMeta.state.version === 0 ? void 0 : docMeta.state.version;\n\t\t\tmodule = this.registry.getModule(document.header.documentType, moduleVersion);\n\t\t} catch (error) {\n\t\t\treturn buildErrorResult(job, error instanceof Error ? error : new Error(String(error)), startTime);\n\t\t}\n\t\tlet updatedDocument;\n\t\ttry {\n\t\t\tconst protocolVersion = document.header.protocolVersions?.[\"base-reducer\"] ?? 1;\n\t\t\tconst reducerOptions = sourceOperation ? {\n\t\t\t\tskip,\n\t\t\t\tbranch: job.branch,\n\t\t\t\treplayOptions: { operation: sourceOperation },\n\t\t\t\tprotocolVersion\n\t\t\t} : {\n\t\t\t\tskip,\n\t\t\t\tbranch: job.branch,\n\t\t\t\tprotocolVersion\n\t\t\t};\n\t\t\tupdatedDocument = module.reducer(document, action, void 0, reducerOptions);\n\t\t} catch (error) {\n\t\t\tconst contextMessage = `Failed to apply action to document:\\n Action type: ${action.type}\\n Document ID: ${job.documentId}\\n Document type: ${document.header.documentType}\\n Scope: ${job.scope}\\n Original error: ${error instanceof Error ? error.message : String(error)}`;\n\t\t\tconst enhancedError = new Error(contextMessage);\n\t\t\tif (error instanceof Error && error.stack) enhancedError.stack = `${contextMessage}\\n\\nOriginal stack trace:\\n${error.stack}`;\n\t\t\treturn buildErrorResult(job, enhancedError, startTime);\n\t\t}\n\t\tconst scope = job.scope;\n\t\tconst operations = updatedDocument.operations[scope];\n\t\tif (operations.length === 0) return buildErrorResult(job, /* @__PURE__ */ new Error(\"No operation generated from action\"), startTime);\n\t\tconst newOperation = operations[operations.length - 1];\n\t\tif (!isUndoRedo(action)) newOperation.skip = skip;\n\t\tconst resultingState = JSON.stringify({\n\t\t\t...updatedDocument.state,\n\t\t\theader: updatedDocument.header\n\t\t});\n\t\tlet storedOperations;\n\t\ttry {\n\t\t\tstoredOperations = await stores.operationStore.apply(job.documentId, document.header.documentType, scope, job.branch, newOperation.index, (txn) => {\n\t\t\t\ttxn.addOperations(newOperation);\n\t\t\t}, signal);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Error writing @Operation to IOperationStore: @Error\", newOperation, error);\n\t\t\tstores.writeCache.invalidate(job.documentId, scope, job.branch);\n\t\t\treturn {\n\t\t\t\tjob,\n\t\t\t\tsuccess: false,\n\t\t\t\terror: /* @__PURE__ */ new Error(`Failed to write operation to IOperationStore: ${error instanceof Error ? error.message : String(error)}`),\n\t\t\t\tduration: Date.now() - startTime\n\t\t\t};\n\t\t}\n\t\tconst storedOperation = storedOperations[0];\n\t\tupdatedDocument.header.revision = {\n\t\t\t...updatedDocument.header.revision,\n\t\t\t[scope]: storedOperation.index + 1\n\t\t};\n\t\tstores.writeCache.putState(job.documentId, scope, job.branch, storedOperation.index, updatedDocument);\n\t\tindexTxn.write([{\n\t\t\t...storedOperation,\n\t\t\tdocumentId: job.documentId,\n\t\t\tdocumentType: document.header.documentType,\n\t\t\tbranch: job.branch,\n\t\t\tscope,\n\t\t\tsourceRemote\n\t\t}]);\n\t\treturn {\n\t\t\tjob,\n\t\t\tsuccess: true,\n\t\t\toperations: [storedOperation],\n\t\t\toperationsWithContext: [{\n\t\t\t\toperation: storedOperation,\n\t\t\t\tcontext: {\n\t\t\t\t\tdocumentId: job.documentId,\n\t\t\t\t\tscope,\n\t\t\t\t\tbranch: job.branch,\n\t\t\t\t\tdocumentType: document.header.documentType,\n\t\t\t\t\tresultingState,\n\t\t\t\t\tordinal: 0\n\t\t\t\t}\n\t\t\t}],\n\t\t\tduration: Date.now() - startTime\n\t\t};\n\t}\n\tasync executeLoadJob(job, startTime, indexTxn, stores, signal) {\n\t\tif (job.operations.length === 0) return buildErrorResult(job, /* @__PURE__ */ new Error(\"Load job must include at least one operation\"), startTime);\n\t\tlet docMeta;\n\t\ttry {\n\t\t\tdocMeta = await stores.documentMetaCache.getDocumentMeta(job.documentId, job.branch, signal);\n\t\t} catch {}\n\t\tif (docMeta?.state.isDeleted) return buildErrorResult(job, new DocumentDeletedError(job.documentId, docMeta.state.deletedAtUtcIso), startTime);\n\t\tconst scope = job.scope;\n\t\tlet latestRevision;\n\t\ttry {\n\t\t\tlatestRevision = (await stores.operationStore.getRevisions(job.documentId, job.branch, signal)).revision[scope] ?? 0;\n\t\t} catch {\n\t\t\tlatestRevision = 0;\n\t\t}\n\t\tfor (const operation of job.operations) if (operation.timestampUtcMs && !isValidISOTimestamp(operation.timestampUtcMs)) return {\n\t\t\tjob,\n\t\t\tsuccess: false,\n\t\t\terror: /* @__PURE__ */ new Error(`Invalid timestamp \"${operation.timestampUtcMs}\" on operation (index: ${operation.index})`),\n\t\t\tduration: Date.now() - startTime\n\t\t};\n\t\tlet minIncomingIndex = Number.POSITIVE_INFINITY;\n\t\tlet minIncomingTimestamp = job.operations[0]?.timestampUtcMs || \"\";\n\t\tfor (const operation of job.operations) {\n\t\t\tminIncomingIndex = Math.min(minIncomingIndex, operation.index);\n\t\t\tconst ts = operation.timestampUtcMs || \"\";\n\t\t\tif (ts < minIncomingTimestamp) minIncomingTimestamp = ts;\n\t\t}\n\t\tlet conflictingOps;\n\t\ttry {\n\t\t\tconflictingOps = (await stores.operationStore.getConflicting(job.documentId, scope, job.branch, minIncomingTimestamp, void 0, signal)).results;\n\t\t} catch {\n\t\t\tconflictingOps = [];\n\t\t}\n\t\tlet allOpsFromMinConflictingIndex = conflictingOps;\n\t\tif (conflictingOps.length > 0) {\n\t\t\tconst minConflictingIndex = Math.min(...conflictingOps.map((op) => op.index));\n\t\t\ttry {\n\t\t\t\tallOpsFromMinConflictingIndex = (await stores.operationStore.getSince(job.documentId, scope, job.branch, minConflictingIndex - 1, void 0, void 0, signal)).results;\n\t\t\t} catch {\n\t\t\t\tallOpsFromMinConflictingIndex = conflictingOps;\n\t\t\t}\n\t\t}\n\t\tconst incomingActionIds = new Set(job.operations.map((op) => op.action.id));\n\t\tconst nonSupersededOps = conflictingOps.filter((op) => {\n\t\t\tif (op.index < minIncomingIndex && !incomingActionIds.has(op.action.id)) return false;\n\t\t\tfor (const laterOp of allOpsFromMinConflictingIndex) if (laterOp.index > op.index && laterOp.skip > 0) {\n\t\t\t\tif (laterOp.index - laterOp.skip <= op.index) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t\tconst existingOpsToReshuffle = nonSupersededOps;\n\t\tif (existingOpsToReshuffle.length > this.config.maxSkipThreshold) return {\n\t\t\tjob,\n\t\t\tsuccess: false,\n\t\t\terror: /* @__PURE__ */ new Error(`Excessive reshuffle detected: existing op count of ${existingOpsToReshuffle.length} exceeds threshold of ${this.config.maxSkipThreshold}. This indicates a significant divergence between local and incoming operations.`),\n\t\t\tduration: Date.now() - startTime\n\t\t};\n\t\tlet skipCount = existingOpsToReshuffle.length;\n\t\tif (existingOpsToReshuffle.length > 0) {\n\t\t\tlet minLogicalIndex = Number.POSITIVE_INFINITY;\n\t\t\tfor (const op of existingOpsToReshuffle) {\n\t\t\t\tconst logical = op.index - op.skip;\n\t\t\t\tif (logical < minLogicalIndex) minLogicalIndex = logical;\n\t\t\t}\n\t\t\tconst logicalSkip = latestRevision - minLogicalIndex;\n\t\t\tif (logicalSkip > skipCount) skipCount = logicalSkip;\n\t\t}\n\t\tconst existingActionIds = new Set(nonSupersededOps.map((op) => op.action.id));\n\t\tconst seenIncomingActionIds = /* @__PURE__ */ new Set();\n\t\tconst incomingOpsToApply = job.operations.filter((op) => {\n\t\t\tif (existingActionIds.has(op.action.id)) return false;\n\t\t\tif (seenIncomingActionIds.has(op.action.id)) return false;\n\t\t\tseenIncomingActionIds.add(op.action.id);\n\t\t\treturn true;\n\t\t});\n\t\tif (incomingOpsToApply.length === 0) return {\n\t\t\tjob,\n\t\t\tsuccess: true,\n\t\t\toperations: [],\n\t\t\toperationsWithContext: [],\n\t\t\tduration: Date.now() - startTime\n\t\t};\n\t\tconst reshuffledOperations = existingOpsToReshuffle.length === 0 && skipCount === 0 ? incomingOpsToApply.slice().sort((a, b) => a.index - b.index).map((operation, i) => ({\n\t\t\t...operation,\n\t\t\tindex: latestRevision + i\n\t\t})) : reshuffleByTimestamp({\n\t\t\tindex: latestRevision,\n\t\t\tskip: skipCount\n\t\t}, existingOpsToReshuffle, incomingOpsToApply.map((operation) => ({\n\t\t\t...operation,\n\t\t\tid: operation.id\n\t\t})));\n\t\tfor (const operation of reshuffledOperations) if (operation.action.type === \"NOOP\") operation.skip = 1;\n\t\tconst actions = reshuffledOperations.map((operation) => operation.action);\n\t\tconst skipValues = reshuffledOperations.map((operation) => operation.skip);\n\t\tconst effectiveSourceRemote = skipCount > 0 ? \"\" : job.meta.sourceRemote || \"\";\n\t\tconst result = await this.processActions(job, actions, startTime, indexTxn, stores, skipValues, reshuffledOperations, effectiveSourceRemote, signal);\n\t\tif (!result.success) return {\n\t\t\tjob,\n\t\t\tsuccess: false,\n\t\t\terror: result.error,\n\t\t\tduration: Date.now() - startTime\n\t\t};\n\t\tstores.writeCache.invalidate(job.documentId, scope, job.branch);\n\t\tif (scope === \"document\") stores.documentMetaCache.invalidate(job.documentId, job.branch);\n\t\treturn {\n\t\t\tjob,\n\t\t\tsuccess: true,\n\t\t\toperations: result.generatedOperations,\n\t\t\toperationsWithContext: result.operationsWithContext,\n\t\t\tduration: Date.now() - startTime\n\t\t};\n\t}\n\taccumulateResultOrReturnError(result, generatedOperations, operationsWithContext) {\n\t\tif (!result.success) return result;\n\t\tif (result.operations && result.operations.length > 0) generatedOperations.push(...result.operations);\n\t\tif (result.operationsWithContext) operationsWithContext.push(...result.operationsWithContext);\n\t\treturn null;\n\t}\n};\n//#endregion\n//#region src/registry/implementation.ts\n/**\n* In-memory implementation of the IDocumentModelRegistry interface.\n* Manages document model modules with version-aware storage and upgrade manifest support.\n*/\nvar DocumentModelRegistry = class {\n\tmodules = [];\n\tmanifests = [];\n\tregisterModules(...modules) {\n\t\treturn modules.map((module) => {\n\t\t\ttry {\n\t\t\t\tconst documentType = module.documentModel.global.id;\n\t\t\t\tconst version = module.version ?? 1;\n\t\t\t\tfor (let i = 0; i < this.modules.length; i++) {\n\t\t\t\t\tconst existing = this.modules[i];\n\t\t\t\t\tconst existingType = existing.documentModel.global.id;\n\t\t\t\t\tconst existingVersion = existing.version ?? 1;\n\t\t\t\t\tif (existingType === documentType && existingVersion === version) throw new DuplicateModuleError(documentType, version);\n\t\t\t\t}\n\t\t\t\tthis.modules.push(module);\n\t\t\t\treturn {\n\t\t\t\t\tstatus: \"success\",\n\t\t\t\t\titem: module\n\t\t\t\t};\n\t\t\t} catch (error) {\n\t\t\t\treturn {\n\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\titem: module,\n\t\t\t\t\terror: error instanceof Error ? error : new Error(String(error))\n\t\t\t\t};\n\t\t\t}\n\t\t});\n\t}\n\tunregisterModules(...documentTypes) {\n\t\tlet allFound = true;\n\t\tfor (const documentType of documentTypes) {\n\t\t\tif (!this.modules.some((m) => m.documentModel.global.id === documentType)) allFound = false;\n\t\t\tthis.modules = this.modules.filter((m) => m.documentModel.global.id !== documentType);\n\t\t}\n\t\treturn allFound;\n\t}\n\tgetModule(documentType, version) {\n\t\tlet latestModule;\n\t\tlet latestVersion = -1;\n\t\tfor (let i = 0; i < this.modules.length; i++) {\n\t\t\tconst module = this.modules[i];\n\t\t\tconst moduleType = module.documentModel.global.id;\n\t\t\tconst moduleVersion = module.version ?? 1;\n\t\t\tif (moduleType === documentType) {\n\t\t\t\tif (version !== void 0 && moduleVersion === version) return module;\n\t\t\t\tif (moduleVersion > latestVersion) {\n\t\t\t\t\tlatestModule = module;\n\t\t\t\t\tlatestVersion = moduleVersion;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (version === void 0 && latestModule !== void 0) return latestModule;\n\t\tthrow new ModuleNotFoundError(documentType, version);\n\t}\n\tgetAllModules() {\n\t\treturn [...this.modules];\n\t}\n\tclear() {\n\t\tthis.modules = [];\n\t\tthis.manifests = [];\n\t}\n\tgetSupportedVersions(documentType) {\n\t\tconst versions = [];\n\t\tfor (const module of this.modules) if (module.documentModel.global.id === documentType) versions.push(module.version ?? 1);\n\t\tif (versions.length === 0) throw new ModuleNotFoundError(documentType);\n\t\treturn versions.sort((a, b) => a - b);\n\t}\n\tgetLatestVersion(documentType) {\n\t\tlet latest = -1;\n\t\tlet found = false;\n\t\tfor (const module of this.modules) if (module.documentModel.global.id === documentType) {\n\t\t\tfound = true;\n\t\t\tconst version = module.version ?? 1;\n\t\t\tif (version > latest) latest = version;\n\t\t}\n\t\tif (!found) throw new ModuleNotFoundError(documentType);\n\t\treturn latest;\n\t}\n\tregisterUpgradeManifests(...manifestsToRegister) {\n\t\treturn manifestsToRegister.map((manifestToRegister) => {\n\t\t\ttry {\n\t\t\t\tif (!manifestToRegister.documentType) throw new Error(\"Upgrade manifest is missing a documentType\");\n\t\t\t\tfor (const registeredManifest of this.manifests) if (registeredManifest.documentType === manifestToRegister.documentType) throw new DuplicateManifestError(manifestToRegister.documentType);\n\t\t\t\tthis.manifests.push(manifestToRegister);\n\t\t\t\treturn {\n\t\t\t\t\tstatus: \"success\",\n\t\t\t\t\titem: manifestToRegister\n\t\t\t\t};\n\t\t\t} catch (error) {\n\t\t\t\treturn {\n\t\t\t\t\tstatus: \"error\",\n\t\t\t\t\titem: manifestToRegister,\n\t\t\t\t\terror: error instanceof Error ? error : new Error(String(error))\n\t\t\t\t};\n\t\t\t}\n\t\t});\n\t}\n\tunregisterUpgradeManifests(...documentTypes) {\n\t\tlet allFound = true;\n\t\tfor (const documentType of documentTypes) {\n\t\t\tif (!this.manifests.some((m) => m.documentType === documentType)) allFound = false;\n\t\t\tthis.manifests = this.manifests.filter((m) => m.documentType !== documentType);\n\t\t}\n\t\treturn allFound;\n\t}\n\tgetUpgradeManifest(documentType) {\n\t\tfor (let i = 0; i < this.manifests.length; i++) if (this.manifests[i].documentType === documentType) return this.manifests[i];\n\t\tthrow new ManifestNotFoundError(documentType);\n\t}\n\tcomputeUpgradePath(documentType, fromVersion, toVersion) {\n\t\tif (fromVersion === toVersion) return [];\n\t\tif (toVersion < fromVersion) throw new DowngradeNotSupportedError(documentType, fromVersion, toVersion);\n\t\tconst manifest = this.getUpgradeManifest(documentType);\n\t\tconst path = [];\n\t\tfor (let v = fromVersion + 1; v <= toVersion; v++) {\n\t\t\tconst key = `v${v}`;\n\t\t\tif (!(key in manifest.upgrades)) throw new MissingUpgradeTransitionError(documentType, v - 1, v);\n\t\t\tconst transition = manifest.upgrades[key];\n\t\t\tpath.push(transition);\n\t\t}\n\t\treturn path;\n\t}\n\tgetUpgradeReducer(documentType, fromVersion, toVersion) {\n\t\tif (toVersion !== fromVersion + 1) throw new InvalidUpgradeStepError(documentType, fromVersion, toVersion);\n\t\tconst manifest = this.getUpgradeManifest(documentType);\n\t\tconst key = `v${toVersion}`;\n\t\tif (!(key in manifest.upgrades)) throw new MissingUpgradeTransitionError(documentType, fromVersion, toVersion);\n\t\treturn manifest.upgrades[key].upgradeReducer;\n\t}\n};\n//#endregion\n//#region src/storage/kysely/keyframe-store.ts\nvar KyselyKeyframeStore = class KyselyKeyframeStore {\n\ttrx;\n\tconstructor(db) {\n\t\tthis.db = db;\n\t}\n\tget queryExecutor() {\n\t\treturn this.trx ?? this.db;\n\t}\n\twithTransaction(trx) {\n\t\tconst instance = new KyselyKeyframeStore(this.db);\n\t\tinstance.trx = trx;\n\t\treturn instance;\n\t}\n\tasync putKeyframe(documentId, scope, branch, revision, document, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tawait this.queryExecutor.insertInto(\"Keyframe\").values({\n\t\t\tdocumentId,\n\t\t\tdocumentType: document.header.documentType,\n\t\t\tscope,\n\t\t\tbranch,\n\t\t\trevision,\n\t\t\tdocument\n\t\t}).onConflict((oc) => oc.columns([\n\t\t\t\"documentId\",\n\t\t\t\"scope\",\n\t\t\t\"branch\",\n\t\t\t\"revision\"\n\t\t]).doUpdateSet({ document })).execute();\n\t}\n\tasync findNearestKeyframe(documentId, scope, branch, targetRevision, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst row = await this.queryExecutor.selectFrom(\"Keyframe\").selectAll().where(\"documentId\", \"=\", documentId).where(\"scope\", \"=\", scope).where(\"branch\", \"=\", branch).where(\"revision\", \"<=\", targetRevision).orderBy(\"revision\", \"desc\").limit(1).executeTakeFirst();\n\t\tif (!row) return;\n\t\treturn {\n\t\t\trevision: row.revision,\n\t\t\tdocument: row.document\n\t\t};\n\t}\n\tasync listKeyframes(documentId, scope, branch, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tlet query = this.queryExecutor.selectFrom(\"Keyframe\").selectAll().where(\"documentId\", \"=\", documentId).orderBy(\"revision\", \"asc\");\n\t\tif (scope !== void 0) query = query.where(\"scope\", \"=\", scope);\n\t\tif (branch !== void 0) query = query.where(\"branch\", \"=\", branch);\n\t\treturn (await query.execute()).map((row) => ({\n\t\t\tscope: row.scope,\n\t\t\tbranch: row.branch,\n\t\t\trevision: row.revision,\n\t\t\tdocument: row.document\n\t\t}));\n\t}\n\tasync deleteKeyframes(documentId, scope, branch, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tlet query = this.queryExecutor.deleteFrom(\"Keyframe\").where(\"documentId\", \"=\", documentId);\n\t\tif (scope !== void 0 && branch !== void 0) query = query.where(\"scope\", \"=\", scope).where(\"branch\", \"=\", branch);\n\t\telse if (scope !== void 0) query = query.where(\"scope\", \"=\", scope);\n\t\tconst result = await query.executeTakeFirst();\n\t\treturn Number(result.numDeletedRows || 0n);\n\t}\n};\n//#endregion\n//#region src/storage/kysely/pagination.ts\nconst DEFAULT_LIMIT = 100;\nfunction paginateRows(rows, paging, cursorOf, toItem, refetch) {\n\tlet hasMore = false;\n\tlet items = rows;\n\tif (paging?.limit && rows.length > paging.limit) {\n\t\thasMore = true;\n\t\titems = rows.slice(0, paging.limit);\n\t}\n\tconst nextCursor = hasMore && items.length > 0 ? cursorOf(items[items.length - 1]).toString() : void 0;\n\tconst cursor = paging?.cursor || \"0\";\n\tconst limit = paging?.limit || DEFAULT_LIMIT;\n\treturn {\n\t\tresults: items.map(toItem),\n\t\toptions: {\n\t\t\tcursor,\n\t\t\tlimit\n\t\t},\n\t\tnextCursor,\n\t\tnext: hasMore ? () => refetch(nextCursor, limit) : void 0\n\t};\n}\n//#endregion\n//#region src/storage/interfaces.ts\n/**\n* Thrown when an operation with the same identity already exists in the store.\n*/\nvar DuplicateOperationError = class extends Error {\n\tconstructor(description) {\n\t\tsuper(`Duplicate operation: ${description}`);\n\t\tthis.name = \"DuplicateOperationError\";\n\t}\n};\n/**\n* Thrown when a concurrent write conflict is detected during an atomic apply.\n*/\nvar OptimisticLockError = class extends Error {\n\tconstructor(message) {\n\t\tsuper(message);\n\t\tthis.name = \"OptimisticLockError\";\n\t}\n};\n/**\n* Thrown when the caller-provided revision does not match the current\n* stored revision, indicating a stale read.\n*/\nvar RevisionMismatchError = class extends Error {\n\tconstructor(expected, actual) {\n\t\tsuper(`Revision mismatch: expected ${expected}, got ${actual}`);\n\t\tthis.name = \"RevisionMismatchError\";\n\t}\n};\n//#endregion\n//#region src/storage/txn.ts\nvar AtomicTransaction = class {\n\toperations = [];\n\tconstructor(documentId, documentType, scope, branch, baseRevision) {\n\t\tthis.documentId = documentId;\n\t\tthis.documentType = documentType;\n\t\tthis.scope = scope;\n\t\tthis.branch = branch;\n\t\tthis.baseRevision = baseRevision;\n\t}\n\taddOperations(...operations) {\n\t\tfor (const op of operations) this.operations.push({\n\t\t\tjobId: v4(),\n\t\t\topId: op.id,\n\t\t\tprevOpId: \"\",\n\t\t\tdocumentId: this.documentId,\n\t\t\tdocumentType: this.documentType,\n\t\t\tscope: this.scope,\n\t\t\tbranch: this.branch,\n\t\t\ttimestampUtcMs: new Date(op.timestampUtcMs),\n\t\t\tindex: op.index,\n\t\t\taction: JSON.stringify(op.action),\n\t\t\tskip: op.skip,\n\t\t\terror: op.error || null,\n\t\t\thash: op.hash\n\t\t});\n\t}\n\tgetOperations() {\n\t\treturn this.operations;\n\t}\n};\n//#endregion\n//#region src/storage/kysely/store.ts\nvar _UniqueConstraintContext = class extends Error {\n\tconstructor(documentId, scope, branch, revision, stagedOps) {\n\t\tsuper(\"unique constraint\");\n\t\tthis.documentId = documentId;\n\t\tthis.scope = scope;\n\t\tthis.branch = branch;\n\t\tthis.revision = revision;\n\t\tthis.stagedOps = stagedOps;\n\t\tthis.name = \"UniqueConstraintContext\";\n\t}\n};\nvar KyselyOperationStore = class KyselyOperationStore {\n\ttrx;\n\tconstructor(db) {\n\t\tthis.db = db;\n\t}\n\tget queryExecutor() {\n\t\treturn this.trx ?? this.db;\n\t}\n\twithTransaction(trx) {\n\t\tconst instance = new KyselyOperationStore(this.db);\n\t\tinstance.trx = trx;\n\t\treturn instance;\n\t}\n\tasync apply(documentId, documentType, scope, branch, revision, fn, signal) {\n\t\tif (this.trx) {\n\t\t\tlet executeResult = null;\n\t\t\tlet uniqueCtx = null;\n\t\t\ttry {\n\t\t\t\texecuteResult = await this.executeApply(this.trx, documentId, documentType, scope, branch, revision, fn, signal);\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof _UniqueConstraintContext) uniqueCtx = error;\n\t\t\t\telse throw error;\n\t\t\t}\n\t\t\tif (uniqueCtx !== null) return this.resolveUniqueConstraint(uniqueCtx);\n\t\t\treturn executeResult;\n\t\t} else {\n\t\t\tlet transactionResult = null;\n\t\t\tlet uniqueCtx = null;\n\t\t\ttry {\n\t\t\t\ttransactionResult = await this.db.transaction().execute(async (trx) => {\n\t\t\t\t\treturn this.executeApply(trx, documentId, documentType, scope, branch, revision, fn, signal);\n\t\t\t\t});\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof _UniqueConstraintContext) uniqueCtx = error;\n\t\t\t\telse throw error;\n\t\t\t}\n\t\t\tif (uniqueCtx !== null) return this.resolveUniqueConstraint(uniqueCtx);\n\t\t\treturn transactionResult;\n\t\t}\n\t}\n\tasync resolveUniqueConstraint(ctx) {\n\t\tlet replayOps = null;\n\t\ttry {\n\t\t\treplayOps = await this.findIdempotentReplay(this.db, ctx.documentId, ctx.scope, ctx.branch, ctx.revision, ctx.stagedOps);\n\t\t} catch {}\n\t\tif (replayOps !== null) return replayOps;\n\t\tconst op = ctx.stagedOps[0];\n\t\tthrow new DuplicateOperationError(`${op.opId} at index ${op.index} with skip ${op.skip}`);\n\t}\n\tasync executeApply(trx, documentId, documentType, scope, branch, revision, fn, signal) {\n\t\tthrowIfAborted(signal);\n\t\tconst atomicTxn = new AtomicTransaction(documentId, documentType, scope, branch, revision);\n\t\tawait fn(atomicTxn);\n\t\tconst operations = atomicTxn.getOperations();\n\t\tif (operations.length === 0) return [];\n\t\tconst latestOp = await trx.selectFrom(\"Operation\").selectAll().where(\"documentId\", \"=\", documentId).where(\"scope\", \"=\", scope).where(\"branch\", \"=\", branch).orderBy(\"index\", \"desc\").limit(1).executeTakeFirst();\n\t\tconst currentRevision = latestOp ? latestOp.index : -1;\n\t\tif (currentRevision !== revision - 1) {\n\t\t\tlet replayOps = null;\n\t\t\ttry {\n\t\t\t\treplayOps = await this.findIdempotentReplay(trx, documentId, scope, branch, revision, operations);\n\t\t\t} catch {}\n\t\t\tif (replayOps !== null) return replayOps;\n\t\t\tthrow new RevisionMismatchError(currentRevision + 1, revision);\n\t\t}\n\t\tlet prevOpId = latestOp?.opId || \"\";\n\t\tfor (const op of operations) {\n\t\t\top.prevOpId = prevOpId;\n\t\t\tprevOpId = op.opId;\n\t\t}\n\t\ttry {\n\t\t\tawait trx.insertInto(\"Operation\").values(operations).execute();\n\t\t} catch (error) {\n\t\t\tif (error instanceof Error && error.message.includes(\"unique constraint\")) throw new _UniqueConstraintContext(documentId, scope, branch, revision, operations);\n\t\t\tthrow error;\n\t\t}\n\t\treturn operations.map((op) => ({\n\t\t\tindex: op.index,\n\t\t\ttimestampUtcMs: op.timestampUtcMs.toISOString(),\n\t\t\thash: op.hash,\n\t\t\tskip: op.skip,\n\t\t\terror: op.error || void 0,\n\t\t\tid: op.opId,\n\t\t\taction: JSON.parse(op.action)\n\t\t}));\n\t}\n\tasync findIdempotentReplay(executor, documentId, scope, branch, revision, stagedOps) {\n\t\tconst minIndex = revision;\n\t\tconst maxIndex = revision + stagedOps.length - 1;\n\t\tconst storedRows = await executor.selectFrom(\"Operation\").selectAll().where(\"documentId\", \"=\", documentId).where(\"scope\", \"=\", scope).where(\"branch\", \"=\", branch).where(\"index\", \">=\", minIndex).where(\"index\", \"<=\", maxIndex).orderBy(\"index\", \"asc\").execute();\n\t\tif (storedRows.length !== stagedOps.length) return null;\n\t\tfor (let i = 0; i < stagedOps.length; i++) {\n\t\t\tconst staged = stagedOps[i];\n\t\t\tconst stored = storedRows[i];\n\t\t\tif (stored.opId !== staged.opId || stored.index !== staged.index || stored.skip !== staged.skip) return null;\n\t\t}\n\t\treturn storedRows.map((row) => this.rowToOperation(row));\n\t}\n\tasync getSince(documentId, scope, branch, revision, filter, paging, signal) {\n\t\tthrowIfAborted(signal);\n\t\tlet query = this.queryExecutor.selectFrom(\"Operation\").selectAll().where(\"documentId\", \"=\", documentId).where(\"scope\", \"=\", scope).where(\"branch\", \"=\", branch).where(\"index\", \">\", revision).orderBy(\"index\", \"asc\");\n\t\tif (filter) {\n\t\t\tif (filter.actionTypes && filter.actionTypes.length > 0) {\n\t\t\t\tconst actionTypesArray = filter.actionTypes.map((t) => `'${t.replace(/'/g, \"''\")}'`).join(\",\");\n\t\t\t\tquery = query.where(sql`action->>'type' = ANY(ARRAY[${sql.raw(actionTypesArray)}]::text[])`);\n\t\t\t}\n\t\t\tif (filter.timestampFrom) query = query.where(\"timestampUtcMs\", \">=\", new Date(filter.timestampFrom));\n\t\t\tif (filter.timestampTo) query = query.where(\"timestampUtcMs\", \"<=\", new Date(filter.timestampTo));\n\t\t\tif (filter.sinceRevision !== void 0) query = query.where(\"index\", \">=\", filter.sinceRevision);\n\t\t}\n\t\tif (paging) {\n\t\t\tconst cursorValue = Number.parseInt(paging.cursor, 10);\n\t\t\tif (cursorValue > 0) query = query.where(\"index\", \">\", cursorValue);\n\t\t\tif (paging.limit) query = query.limit(paging.limit + 1);\n\t\t}\n\t\treturn paginateRows(await query.execute(), paging, (row) => row.index, (row) => this.rowToOperation(row), (cursor, limit) => this.getSince(documentId, scope, branch, revision, filter, {\n\t\t\tcursor,\n\t\t\tlimit\n\t\t}, signal));\n\t}\n\tasync getSinceId(id, paging, signal) {\n\t\tthrowIfAborted(signal);\n\t\tlet query = this.queryExecutor.selectFrom(\"Operation\").selectAll().where(\"id\", \">\", id).orderBy(\"id\", \"asc\");\n\t\tif (paging) {\n\t\t\tconst cursorValue = Number.parseInt(paging.cursor, 10);\n\t\t\tif (cursorValue > 0) query = query.where(\"id\", \">\", cursorValue);\n\t\t\tif (paging.limit) query = query.limit(paging.limit + 1);\n\t\t}\n\t\treturn paginateRows(await query.execute(), paging, (row) => row.id, (row) => this.rowToOperationWithContext(row), (cursor, limit) => this.getSinceId(id, {\n\t\t\tcursor,\n\t\t\tlimit\n\t\t}, signal));\n\t}\n\tasync getConflicting(documentId, scope, branch, minTimestamp, paging, signal) {\n\t\tthrowIfAborted(signal);\n\t\tlet query = this.queryExecutor.selectFrom(\"Operation\").selectAll().where(\"documentId\", \"=\", documentId).where(\"scope\", \"=\", scope).where(\"branch\", \"=\", branch).where(\"timestampUtcMs\", \">=\", new Date(minTimestamp)).orderBy(\"index\", \"asc\");\n\t\tif (paging) {\n\t\t\tconst cursorValue = Number.parseInt(paging.cursor, 10);\n\t\t\tif (cursorValue > 0) query = query.where(\"index\", \">\", cursorValue);\n\t\t\tif (paging.limit) query = query.limit(paging.limit + 1);\n\t\t}\n\t\treturn paginateRows(await query.execute(), paging, (row) => row.index, (row) => this.rowToOperation(row), (cursor, limit) => this.getConflicting(documentId, scope, branch, minTimestamp, {\n\t\t\tcursor,\n\t\t\tlimit\n\t\t}, signal));\n\t}\n\tasync getRevisions(documentId, branch, signal) {\n\t\tthrowIfAborted(signal);\n\t\tconst scopeRevisions = await this.queryExecutor.selectFrom(\"Operation as o1\").select([\n\t\t\t\"o1.scope\",\n\t\t\t\"o1.index\",\n\t\t\t\"o1.timestampUtcMs\"\n\t\t]).where(\"o1.documentId\", \"=\", documentId).where(\"o1.branch\", \"=\", branch).where((eb) => eb(\"o1.index\", \"=\", eb.selectFrom(\"Operation as o2\").select((eb2) => eb2.fn.max(\"o2.index\").as(\"maxIndex\")).where(\"o2.documentId\", \"=\", eb.ref(\"o1.documentId\")).where(\"o2.branch\", \"=\", eb.ref(\"o1.branch\")).where(\"o2.scope\", \"=\", eb.ref(\"o1.scope\")))).execute();\n\t\tconst revision = {};\n\t\tlet latestTimestamp = (/* @__PURE__ */ new Date(0)).toISOString();\n\t\tfor (const row of scopeRevisions) {\n\t\t\trevision[row.scope] = row.index + 1;\n\t\t\tconst timestamp = row.timestampUtcMs.toISOString();\n\t\t\tif (timestamp > latestTimestamp) latestTimestamp = timestamp;\n\t\t}\n\t\treturn {\n\t\t\trevision,\n\t\t\tlatestTimestamp\n\t\t};\n\t}\n\trowToOperation(row) {\n\t\treturn {\n\t\t\tindex: row.index,\n\t\t\ttimestampUtcMs: row.timestampUtcMs.toISOString(),\n\t\t\thash: row.hash,\n\t\t\tskip: row.skip,\n\t\t\terror: row.error || void 0,\n\t\t\tid: row.opId,\n\t\t\taction: row.action\n\t\t};\n\t}\n\trowToOperationWithContext(row) {\n\t\treturn {\n\t\t\toperation: this.rowToOperation(row),\n\t\t\tcontext: {\n\t\t\t\tdocumentId: row.documentId,\n\t\t\t\tdocumentType: row.documentType,\n\t\t\t\tscope: row.scope,\n\t\t\t\tbranch: row.branch,\n\t\t\t\tordinal: row.id\n\t\t\t}\n\t\t};\n\t}\n};\n//#endregion\n//#region src/storage/pool-instrumentation.ts\n/**\n* Wraps an existing pg.Pool with acquire-wait timing and an event\n* subscription surface. The pool is mutated in place: pool.connect()\n* is replaced with a timing wrapper so all callers (Kysely included)\n* pick up the instrumentation transparently.\n*/\nfunction instrumentPgPool(pool, name) {\n\tconst listeners = /* @__PURE__ */ new Set();\n\tconst originalConnect = pool.connect.bind(pool);\n\tconst wrappedConnect = async () => {\n\t\tconst start = performance.now();\n\t\tconst client = await originalConnect();\n\t\tconst durationMs = performance.now() - start;\n\t\tfor (const listener of listeners) try {\n\t\t\tlistener(durationMs);\n\t\t} catch {}\n\t\treturn client;\n\t};\n\tpool.connect = wrappedConnect;\n\treturn {\n\t\tname,\n\t\tgetStats() {\n\t\t\treturn {\n\t\t\t\tsize: pool.totalCount,\n\t\t\t\tidle: pool.idleCount,\n\t\t\t\twaiting: pool.waitingCount\n\t\t\t};\n\t\t},\n\t\tonAcquire(listener) {\n\t\t\tlisteners.add(listener);\n\t\t\treturn () => {\n\t\t\t\tlisteners.delete(listener);\n\t\t\t};\n\t\t}\n\t};\n}\nfunction createForwardingPoolInstrumentation(name) {\n\tconst listeners = /* @__PURE__ */ new Set();\n\tlet stats = {\n\t\tsize: 0,\n\t\tidle: 0,\n\t\twaiting: 0\n\t};\n\treturn {\n\t\tname,\n\t\tgetStats() {\n\t\t\treturn stats;\n\t\t},\n\t\tonAcquire(listener) {\n\t\t\tlisteners.add(listener);\n\t\t\treturn () => {\n\t\t\t\tlisteners.delete(listener);\n\t\t\t};\n\t\t},\n\t\tpushSamples(durations) {\n\t\t\tfor (const durationMs of durations) for (const listener of listeners) try {\n\t\t\t\tlistener(durationMs);\n\t\t\t} catch {}\n\t\t},\n\t\tupdateStats(next) {\n\t\t\tstats = next;\n\t\t}\n\t};\n}\n//#endregion\n//#region src/storage/migrations/001_create_operation_table.ts\nvar _001_create_operation_table_exports = /* @__PURE__ */ __exportAll({ up: () => up$13 });\nasync function up$13(db) {\n\tawait db.schema.createTable(\"Operation\").addColumn(\"id\", \"serial\", (col) => col.primaryKey()).addColumn(\"jobId\", \"text\", (col) => col.notNull()).addColumn(\"opId\", \"text\", (col) => col.notNull()).addColumn(\"prevOpId\", \"text\", (col) => col.notNull()).addColumn(\"writeTimestampUtcMs\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addColumn(\"documentId\", \"text\", (col) => col.notNull()).addColumn(\"documentType\", \"text\", (col) => col.notNull()).addColumn(\"scope\", \"text\", (col) => col.notNull()).addColumn(\"branch\", \"text\", (col) => col.notNull()).addColumn(\"timestampUtcMs\", \"timestamptz\", (col) => col.notNull()).addColumn(\"index\", \"integer\", (col) => col.notNull()).addColumn(\"action\", \"jsonb\", (col) => col.notNull()).addColumn(\"skip\", \"integer\", (col) => col.notNull()).addColumn(\"error\", \"text\").addColumn(\"hash\", \"text\", (col) => col.notNull()).addUniqueConstraint(\"unique_revision\", [\n\t\t\"documentId\",\n\t\t\"scope\",\n\t\t\"branch\",\n\t\t\"index\"\n\t]).addUniqueConstraint(\"unique_operation_instance\", [\n\t\t\"opId\",\n\t\t\"index\",\n\t\t\"skip\"\n\t]).execute();\n\tawait db.schema.createIndex(\"streamOperations\").on(\"Operation\").columns([\n\t\t\"documentId\",\n\t\t\"scope\",\n\t\t\"branch\",\n\t\t\"id\"\n\t]).execute();\n\tawait db.schema.createIndex(\"branchlessStreamOperations\").on(\"Operation\").columns([\n\t\t\"documentId\",\n\t\t\"scope\",\n\t\t\"id\"\n\t]).execute();\n}\n//#endregion\n//#region src/storage/migrations/002_create_keyframe_table.ts\nvar _002_create_keyframe_table_exports = /* @__PURE__ */ __exportAll({ up: () => up$12 });\nasync function up$12(db) {\n\tawait db.schema.createTable(\"Keyframe\").addColumn(\"id\", \"serial\", (col) => col.primaryKey()).addColumn(\"documentId\", \"text\", (col) => col.notNull()).addColumn(\"documentType\", \"text\", (col) => col.notNull()).addColumn(\"scope\", \"text\", (col) => col.notNull()).addColumn(\"branch\", \"text\", (col) => col.notNull()).addColumn(\"revision\", \"integer\", (col) => col.notNull()).addColumn(\"document\", \"jsonb\", (col) => col.notNull()).addColumn(\"createdAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addUniqueConstraint(\"unique_keyframe\", [\n\t\t\"documentId\",\n\t\t\"scope\",\n\t\t\"branch\",\n\t\t\"revision\"\n\t]).execute();\n\tawait db.schema.createIndex(\"keyframe_lookup\").on(\"Keyframe\").columns([\n\t\t\"documentId\",\n\t\t\"scope\",\n\t\t\"branch\",\n\t\t\"revision\"\n\t]).execute();\n}\n//#endregion\n//#region src/storage/migrations/003_create_document_table.ts\nvar _003_create_document_table_exports = /* @__PURE__ */ __exportAll({ up: () => up$11 });\nasync function up$11(db) {\n\tawait db.schema.createTable(\"Document\").addColumn(\"id\", \"text\", (col) => col.primaryKey()).addColumn(\"createdAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addColumn(\"updatedAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).execute();\n}\n//#endregion\n//#region src/storage/migrations/004_create_document_relationship_table.ts\nvar _004_create_document_relationship_table_exports = /* @__PURE__ */ __exportAll({ up: () => up$10 });\nasync function up$10(db) {\n\tawait db.schema.createTable(\"DocumentRelationship\").addColumn(\"id\", \"text\", (col) => col.primaryKey()).addColumn(\"sourceId\", \"text\", (col) => col.notNull().references(\"Document.id\").onDelete(\"cascade\")).addColumn(\"targetId\", \"text\", (col) => col.notNull().references(\"Document.id\").onDelete(\"cascade\")).addColumn(\"relationshipType\", \"text\", (col) => col.notNull()).addColumn(\"metadata\", \"jsonb\").addColumn(\"createdAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addColumn(\"updatedAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addUniqueConstraint(\"unique_source_target_type\", [\n\t\t\"sourceId\",\n\t\t\"targetId\",\n\t\t\"relationshipType\"\n\t]).execute();\n\tawait db.schema.createIndex(\"idx_relationship_source\").on(\"DocumentRelationship\").column(\"sourceId\").execute();\n\tawait db.schema.createIndex(\"idx_relationship_target\").on(\"DocumentRelationship\").column(\"targetId\").execute();\n\tawait db.schema.createIndex(\"idx_relationship_type\").on(\"DocumentRelationship\").column(\"relationshipType\").execute();\n}\n//#endregion\n//#region src/storage/migrations/005_create_indexer_state_table.ts\nvar _005_create_indexer_state_table_exports = /* @__PURE__ */ __exportAll({ up: () => up$9 });\nasync function up$9(db) {\n\tawait db.schema.createTable(\"IndexerState\").addColumn(\"id\", \"integer\", (col) => col.primaryKey().generatedAlwaysAsIdentity()).addColumn(\"lastOperationId\", \"integer\", (col) => col.notNull()).addColumn(\"lastOperationTimestamp\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).execute();\n}\n//#endregion\n//#region src/storage/migrations/006_create_document_snapshot_table.ts\nvar _006_create_document_snapshot_table_exports = /* @__PURE__ */ __exportAll({ up: () => up$8 });\nasync function up$8(db) {\n\tawait db.schema.createTable(\"DocumentSnapshot\").addColumn(\"id\", \"text\", (col) => col.primaryKey()).addColumn(\"documentId\", \"text\", (col) => col.notNull()).addColumn(\"slug\", \"text\").addColumn(\"name\", \"text\").addColumn(\"scope\", \"text\", (col) => col.notNull()).addColumn(\"branch\", \"text\", (col) => col.notNull()).addColumn(\"content\", \"jsonb\", (col) => col.notNull()).addColumn(\"documentType\", \"text\", (col) => col.notNull()).addColumn(\"lastOperationIndex\", \"integer\", (col) => col.notNull()).addColumn(\"lastOperationHash\", \"text\", (col) => col.notNull()).addColumn(\"lastUpdatedAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addColumn(\"snapshotVersion\", \"integer\", (col) => col.notNull().defaultTo(1)).addColumn(\"identifiers\", \"jsonb\").addColumn(\"metadata\", \"jsonb\").addColumn(\"isDeleted\", \"boolean\", (col) => col.notNull().defaultTo(false)).addColumn(\"deletedAt\", \"timestamptz\").addUniqueConstraint(\"unique_doc_scope_branch\", [\n\t\t\"documentId\",\n\t\t\"scope\",\n\t\t\"branch\"\n\t]).execute();\n\tawait db.schema.createIndex(\"idx_slug_scope_branch\").on(\"DocumentSnapshot\").columns([\n\t\t\"slug\",\n\t\t\"scope\",\n\t\t\"branch\"\n\t]).execute();\n\tawait db.schema.createIndex(\"idx_doctype_scope_branch\").on(\"DocumentSnapshot\").columns([\n\t\t\"documentType\",\n\t\t\"scope\",\n\t\t\"branch\"\n\t]).execute();\n\tawait db.schema.createIndex(\"idx_last_updated\").on(\"DocumentSnapshot\").column(\"lastUpdatedAt\").execute();\n\tawait db.schema.createIndex(\"idx_is_deleted\").on(\"DocumentSnapshot\").column(\"isDeleted\").execute();\n}\n//#endregion\n//#region src/storage/migrations/007_create_slug_mapping_table.ts\nvar _007_create_slug_mapping_table_exports = /* @__PURE__ */ __exportAll({ up: () => up$7 });\nasync function up$7(db) {\n\tawait db.schema.createTable(\"SlugMapping\").addColumn(\"slug\", \"text\", (col) => col.primaryKey()).addColumn(\"documentId\", \"text\", (col) => col.notNull()).addColumn(\"scope\", \"text\", (col) => col.notNull()).addColumn(\"branch\", \"text\", (col) => col.notNull()).addColumn(\"createdAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addColumn(\"updatedAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addUniqueConstraint(\"unique_docid_scope_branch\", [\n\t\t\"documentId\",\n\t\t\"scope\",\n\t\t\"branch\"\n\t]).execute();\n\tawait db.schema.createIndex(\"idx_slug_documentid\").on(\"SlugMapping\").column(\"documentId\").execute();\n}\n//#endregion\n//#region src/storage/migrations/008_create_view_state_table.ts\nvar _008_create_view_state_table_exports = /* @__PURE__ */ __exportAll({ up: () => up$6 });\nasync function up$6(db) {\n\tawait db.schema.createTable(\"ViewState\").addColumn(\"readModelId\", \"text\", (col) => col.primaryKey()).addColumn(\"lastOrdinal\", \"integer\", (col) => col.notNull().defaultTo(0)).addColumn(\"lastOperationTimestamp\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).execute();\n}\n//#endregion\n//#region src/storage/migrations/009_create_operation_index_tables.ts\nvar _009_create_operation_index_tables_exports = /* @__PURE__ */ __exportAll({ up: () => up$5 });\nasync function up$5(db) {\n\tawait db.schema.createTable(\"document_collections\").addColumn(\"documentId\", \"text\", (col) => col.notNull()).addColumn(\"collectionId\", \"text\", (col) => col.notNull()).addColumn(\"joinedOrdinal\", \"bigint\", (col) => col.notNull().defaultTo(0)).addColumn(\"leftOrdinal\", \"bigint\").addPrimaryKeyConstraint(\"document_collections_pkey\", [\"documentId\", \"collectionId\"]).execute();\n\tawait db.schema.createIndex(\"idx_document_collections_collectionId\").on(\"document_collections\").column(\"collectionId\").execute();\n\tawait db.schema.createIndex(\"idx_doc_collections_collection_range\").on(\"document_collections\").columns([\"collectionId\", \"joinedOrdinal\"]).execute();\n\tawait db.schema.createTable(\"operation_index_operations\").addColumn(\"ordinal\", \"serial\", (col) => col.primaryKey()).addColumn(\"opId\", \"text\", (col) => col.notNull()).addColumn(\"documentId\", \"text\", (col) => col.notNull()).addColumn(\"documentType\", \"text\", (col) => col.notNull()).addColumn(\"scope\", \"text\", (col) => col.notNull()).addColumn(\"branch\", \"text\", (col) => col.notNull()).addColumn(\"timestampUtcMs\", \"text\", (col) => col.notNull()).addColumn(\"writeTimestampUtcMs\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addColumn(\"index\", \"integer\", (col) => col.notNull()).addColumn(\"skip\", \"integer\", (col) => col.notNull()).addColumn(\"hash\", \"text\", (col) => col.notNull()).addColumn(\"action\", \"jsonb\", (col) => col.notNull()).execute();\n\tawait db.schema.createIndex(\"idx_operation_index_operations_document\").on(\"operation_index_operations\").columns([\n\t\t\"documentId\",\n\t\t\"branch\",\n\t\t\"scope\"\n\t]).execute();\n\tawait db.schema.createIndex(\"idx_operation_index_operations_ordinal\").on(\"operation_index_operations\").column(\"ordinal\").execute();\n}\n//#endregion\n//#region src/storage/migrations/010_create_sync_tables.ts\nvar _010_create_sync_tables_exports = /* @__PURE__ */ __exportAll({ up: () => up$4 });\nasync function up$4(db) {\n\tawait db.schema.createTable(\"sync_remotes\").addColumn(\"name\", \"text\", (col) => col.primaryKey()).addColumn(\"collection_id\", \"text\", (col) => col.notNull()).addColumn(\"channel_type\", \"text\", (col) => col.notNull()).addColumn(\"channel_id\", \"text\", (col) => col.notNull().defaultTo(\"\")).addColumn(\"remote_name\", \"text\", (col) => col.notNull().defaultTo(\"\")).addColumn(\"channel_parameters\", \"jsonb\", (col) => col.notNull().defaultTo(sql`'{}'::jsonb`)).addColumn(\"filter_document_ids\", \"jsonb\").addColumn(\"filter_scopes\", \"jsonb\").addColumn(\"filter_branch\", \"text\", (col) => col.notNull().defaultTo(\"main\")).addColumn(\"push_state\", \"text\", (col) => col.notNull().defaultTo(\"idle\")).addColumn(\"push_last_success_utc_ms\", \"text\").addColumn(\"push_last_failure_utc_ms\", \"text\").addColumn(\"push_failure_count\", \"integer\", (col) => col.notNull().defaultTo(0)).addColumn(\"pull_state\", \"text\", (col) => col.notNull().defaultTo(\"idle\")).addColumn(\"pull_last_success_utc_ms\", \"text\").addColumn(\"pull_last_failure_utc_ms\", \"text\").addColumn(\"pull_failure_count\", \"integer\", (col) => col.notNull().defaultTo(0)).addColumn(\"created_at\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addColumn(\"updated_at\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).execute();\n\tawait db.schema.createIndex(\"idx_sync_remotes_collection\").on(\"sync_remotes\").column(\"collection_id\").execute();\n\tawait db.schema.createTable(\"sync_cursors\").addColumn(\"remote_name\", \"text\", (col) => col.primaryKey().references(\"sync_remotes.name\").onDelete(\"cascade\")).addColumn(\"cursor_ordinal\", \"bigint\", (col) => col.notNull().defaultTo(0)).addColumn(\"last_synced_at_utc_ms\", \"text\").addColumn(\"updated_at\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).execute();\n\tawait db.schema.createIndex(\"idx_sync_cursors_ordinal\").on(\"sync_cursors\").column(\"cursor_ordinal\").execute();\n}\n//#endregion\n//#region src/storage/migrations/011_add_cursor_type_column.ts\nvar _011_add_cursor_type_column_exports = /* @__PURE__ */ __exportAll({ up: () => up$3 });\nasync function up$3(db) {\n\tawait db.deleteFrom(\"sync_cursors\").where(\"remote_name\", \"like\", \"outbox::%\").execute();\n\tawait db.deleteFrom(\"sync_remotes\").where(\"name\", \"like\", \"outbox::%\").execute();\n\tawait db.schema.dropTable(\"sync_cursors\").execute();\n\tawait db.schema.createTable(\"sync_cursors\").addColumn(\"remote_name\", \"text\", (col) => col.notNull()).addColumn(\"cursor_type\", \"text\", (col) => col.notNull().defaultTo(\"inbox\")).addColumn(\"cursor_ordinal\", \"bigint\", (col) => col.notNull().defaultTo(0)).addColumn(\"last_synced_at_utc_ms\", \"text\").addColumn(\"updated_at\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addPrimaryKeyConstraint(\"sync_cursors_pk\", [\"remote_name\", \"cursor_type\"]).execute();\n\tawait db.schema.createIndex(\"idx_sync_cursors_ordinal\").on(\"sync_cursors\").column(\"cursor_ordinal\").execute();\n}\n//#endregion\n//#region src/storage/migrations/012_add_source_remote_column.ts\nvar _012_add_source_remote_column_exports = /* @__PURE__ */ __exportAll({ up: () => up$2 });\nasync function up$2(db) {\n\tawait db.schema.alterTable(\"operation_index_operations\").addColumn(\"sourceRemote\", \"text\", (col) => col.notNull().defaultTo(\"\")).execute();\n}\n//#endregion\n//#region src/storage/migrations/013_create_sync_dead_letters_table.ts\nvar _013_create_sync_dead_letters_table_exports = /* @__PURE__ */ __exportAll({ up: () => up$1 });\nasync function up$1(db) {\n\tawait db.schema.createTable(\"sync_dead_letters\").addColumn(\"ordinal\", \"serial\", (col) => col.primaryKey()).addColumn(\"id\", \"text\", (col) => col.unique().notNull()).addColumn(\"job_id\", \"text\", (col) => col.notNull()).addColumn(\"job_dependencies\", \"jsonb\", (col) => col.notNull().defaultTo(sql`'[]'::jsonb`)).addColumn(\"remote_name\", \"text\", (col) => col.notNull().references(\"sync_remotes.name\").onDelete(\"cascade\")).addColumn(\"document_id\", \"text\", (col) => col.notNull()).addColumn(\"scopes\", \"jsonb\", (col) => col.notNull().defaultTo(sql`'[]'::jsonb`)).addColumn(\"branch\", \"text\", (col) => col.notNull()).addColumn(\"operations\", \"jsonb\", (col) => col.notNull().defaultTo(sql`'[]'::jsonb`)).addColumn(\"error_source\", \"text\", (col) => col.notNull()).addColumn(\"error_message\", \"text\", (col) => col.notNull()).addColumn(\"created_at\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).execute();\n\tawait db.schema.createIndex(\"idx_sync_dead_letters_remote\").on(\"sync_dead_letters\").column(\"remote_name\").execute();\n}\n//#endregion\n//#region src/storage/migrations/014_create_processor_cursor_table.ts\nvar _014_create_processor_cursor_table_exports = /* @__PURE__ */ __exportAll({ up: () => up });\nasync function up(db) {\n\tawait db.schema.createTable(\"ProcessorCursor\").addColumn(\"processorId\", \"text\", (col) => col.primaryKey()).addColumn(\"factoryId\", \"text\", (col) => col.notNull()).addColumn(\"driveId\", \"text\", (col) => col.notNull()).addColumn(\"processorIndex\", \"integer\", (col) => col.notNull()).addColumn(\"lastOrdinal\", \"integer\", (col) => col.notNull().defaultTo(sql`0`)).addColumn(\"status\", \"text\", (col) => col.notNull().defaultTo(sql`'active'`)).addColumn(\"lastError\", \"text\").addColumn(\"lastErrorTimestamp\", \"timestamptz\").addColumn(\"createdAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addColumn(\"updatedAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).execute();\n}\n//#endregion\n//#region src/storage/migrations/migrator.ts\nconst REACTOR_SCHEMA = \"reactor\";\nconst migrations = {\n\t\"001_create_operation_table\": _001_create_operation_table_exports,\n\t\"002_create_keyframe_table\": _002_create_keyframe_table_exports,\n\t\"003_create_document_table\": _003_create_document_table_exports,\n\t\"004_create_document_relationship_table\": _004_create_document_relationship_table_exports,\n\t\"005_create_indexer_state_table\": _005_create_indexer_state_table_exports,\n\t\"006_create_document_snapshot_table\": _006_create_document_snapshot_table_exports,\n\t\"007_create_slug_mapping_table\": _007_create_slug_mapping_table_exports,\n\t\"008_create_view_state_table\": _008_create_view_state_table_exports,\n\t\"009_create_operation_index_tables\": _009_create_operation_index_tables_exports,\n\t\"010_create_sync_tables\": _010_create_sync_tables_exports,\n\t\"011_add_cursor_type_column\": _011_add_cursor_type_column_exports,\n\t\"012_add_source_remote_column\": _012_add_source_remote_column_exports,\n\t\"013_create_sync_dead_letters_table\": _013_create_sync_dead_letters_table_exports,\n\t\"014_create_processor_cursor_table\": _014_create_processor_cursor_table_exports\n};\nvar ProgrammaticMigrationProvider = class {\n\tgetMigrations() {\n\t\treturn Promise.resolve(migrations);\n\t}\n};\nasync function runMigrations(db, schema = REACTOR_SCHEMA) {\n\ttry {\n\t\tawait sql`CREATE SCHEMA IF NOT EXISTS ${sql.id(schema)}`.execute(db);\n\t} catch (error) {\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\tmigrationsExecuted: [],\n\t\t\terror: error instanceof Error ? error : /* @__PURE__ */ new Error(\"Failed to create schema\")\n\t\t};\n\t}\n\tconst migrator = new Migrator({\n\t\tdb: db.withSchema(schema),\n\t\tprovider: new ProgrammaticMigrationProvider(),\n\t\tmigrationTableSchema: schema\n\t});\n\tlet error;\n\tlet results;\n\ttry {\n\t\tconst result = await migrator.migrateToLatest();\n\t\terror = result.error;\n\t\tresults = result.results;\n\t} catch (e) {\n\t\terror = e;\n\t\tresults = [];\n\t}\n\tconst migrationsExecuted = results?.map((result) => result.migrationName) ?? [];\n\tif (error) return {\n\t\tsuccess: false,\n\t\tmigrationsExecuted,\n\t\terror: error instanceof Error ? error : /* @__PURE__ */ new Error(\"Unknown migration error\")\n\t};\n\treturn {\n\t\tsuccess: true,\n\t\tmigrationsExecuted\n\t};\n}\nasync function getMigrationStatus(db, schema = REACTOR_SCHEMA) {\n\treturn await new Migrator({\n\t\tdb: db.withSchema(schema),\n\t\tprovider: new ProgrammaticMigrationProvider(),\n\t\tmigrationTableSchema: schema\n\t}).getMigrations();\n}\n//#endregion\n//#region src/core/drive-container-types.ts\nconst DEFAULT_DRIVE_CONTAINER_TYPES = new Set([\"powerhouse/document-drive\", \"powerhouse/reactor-drive\"]);\n//#endregion\nexport { parsePagingOptions as A, DuplicateManifestError as C, DocumentDeletedError as D, ModuleNotFoundError as E, __exportAll as M, DocumentNotFoundError as O, CollectionMembershipCache as S, InvalidModuleError as T, KyselyWriteCache as _, createForwardingPoolInstrumentation as a, createConsistencyToken as b, DuplicateOperationError as c, KyselyKeyframeStore as d, DocumentModelRegistry as f, EventBus as g, KyselyExecutionScope as h, runMigrations as i, throwIfAborted as j, matchesScope as k, OptimisticLockError as l, driveCollectionId as m, REACTOR_SCHEMA as n, instrumentPgPool as o, SimpleJobExecutor as p, getMigrationStatus as r, KyselyOperationStore as s, DEFAULT_DRIVE_CONTAINER_TYPES as t, RevisionMismatchError as u, KyselyOperationIndex as v, DuplicateModuleError as w, createEmptyConsistencyToken as x, DocumentMetaCache as y };\n\n//# sourceMappingURL=drive-container-types-CAhznKRr.js.map","import { A as parsePagingOptions, C as DuplicateManifestError, D as DocumentDeletedError, E as ModuleNotFoundError, O as DocumentNotFoundError, S as CollectionMembershipCache, T as InvalidModuleError, _ as KyselyWriteCache, a as createForwardingPoolInstrumentation, b as createConsistencyToken, c as DuplicateOperationError, d as KyselyKeyframeStore, f as DocumentModelRegistry, g as EventBus, h as KyselyExecutionScope, i as runMigrations, j as throwIfAborted, k as matchesScope, l as OptimisticLockError, m as driveCollectionId, n as REACTOR_SCHEMA, o as instrumentPgPool, p as SimpleJobExecutor, r as getMigrationStatus, s as KyselyOperationStore, t as DEFAULT_DRIVE_CONTAINER_TYPES, u as RevisionMismatchError, v as KyselyOperationIndex, w as DuplicateModuleError, x as createEmptyConsistencyToken, y as DocumentMetaCache } from \"./drive-container-types-CAhznKRr.js\";\nimport { n as ReactorEventTypes, t as EventBusAggregateError } from \"./types-CxSpmNGK.js\";\nimport { i as WorkerInitFailedError, r as WorkerExitedError, t as WorkerAbortTimeoutError } from \"./errors-D3S6Eysd.js\";\nimport { a as ReadModelCoordinator, i as KyselyDocumentView, n as ConsistencyTracker, o as BaseReadModel, r as makeConsistencyKey, t as KyselyDocumentIndexer } from \"./document-indexer-B2iLRB0o.js\";\nimport { n as errorToInfo, r as sanitizeArg, t as createForwardingLogger } from \"./forwarding-logger-BBkMSxuJ.js\";\nimport { t as workerEntryPath } from \"./worker-Dq6cUr6a.js\";\nimport { actions, actions as documentActions, createPresignedHeader, generateId, hashDocumentStateForScope, replayDocument } from \"@powerhousedao/shared/document-model\";\nimport { addFile, addFolder, copyNode, deleteNode, driveCreateDocument, generateNodesCopy, getDescendants, handleTargetNameCollisions, isFileNode, isFolderNode, moveNode, updateNode } from \"@powerhousedao/shared/document-drive\";\nimport { v4 } from \"uuid\";\nimport { ConsoleLogger } from \"document-model\";\nimport { sql } from \"kysely\";\nimport { RelationalDbProcessor, createRelationalDb } from \"@powerhousedao/shared/processors\";\n//#region src/actions/index.ts\n/**\n* Creates a CREATE_DOCUMENT action for document creation.\n*/\nfunction createDocumentAction(input) {\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"CREATE_DOCUMENT\",\n\t\tscope: \"document\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\n/**\n* Creates an UPGRADE_DOCUMENT action to set initial document state.\n*/\nfunction upgradeDocumentAction(input) {\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"UPGRADE_DOCUMENT\",\n\t\tscope: \"document\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\n/**\n* Creates a DELETE_DOCUMENT action for document deletion.\n*/\nfunction deleteDocumentAction(documentId) {\n\tconst input = { documentId };\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"DELETE_DOCUMENT\",\n\t\tscope: \"document\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\n/**\n* Creates an ADD_RELATIONSHIP action that records a directed edge from\n* `sourceId` to `targetId` with an arbitrary `relationshipType` and optional\n* `metadata`. The edge is opaque to the reactor — consumers (e.g. reactor-drive)\n* define their own type strings such as `\"drive/child\"` and attach\n* domain-specific metadata.\n*/\nfunction addRelationshipAction(sourceId, targetId, relationshipType, metadata) {\n\tconst input = {\n\t\tsourceId,\n\t\ttargetId,\n\t\trelationshipType,\n\t\t...metadata !== void 0 ? { metadata } : {}\n\t};\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"ADD_RELATIONSHIP\",\n\t\tscope: \"document\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\n/**\n* Creates an UPDATE_RELATIONSHIP action to replace a relationship's metadata\n* without losing its createdAt ordering.\n*/\nfunction updateRelationshipAction(sourceId, targetId, relationshipType, metadata) {\n\tconst input = {\n\t\tsourceId,\n\t\ttargetId,\n\t\trelationshipType,\n\t\tmetadata\n\t};\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"UPDATE_RELATIONSHIP\",\n\t\tscope: \"document\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\n/**\n* Creates a REMOVE_RELATIONSHIP action to remove a parent-child relationship.\n*/\nfunction removeRelationshipAction(sourceId, targetId, relationshipType = \"child\") {\n\tconst input = {\n\t\tsourceId,\n\t\ttargetId,\n\t\trelationshipType\n\t};\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"REMOVE_RELATIONSHIP\",\n\t\tscope: \"document\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\n//#endregion\n//#region src/core/utils.ts\n/**\n* Validates structural properties shared by all batch requests:\n* duplicate keys, missing dependencies, and dependency cycles.\n*/\nfunction validateBatchStructure(jobs) {\n\tconst keys = /* @__PURE__ */ new Set();\n\tfor (const job of jobs) {\n\t\tif (keys.has(job.key)) throw new Error(`Duplicate plan key: ${job.key}`);\n\t\tkeys.add(job.key);\n\t}\n\tfor (const job of jobs) for (const depKey of job.dependsOn) if (!keys.has(depKey)) throw new Error(`Job '${job.key}' depends on non-existent key: ${depKey}`);\n\tconst visited = /* @__PURE__ */ new Set();\n\tconst recStack = /* @__PURE__ */ new Set();\n\tconst detectCycle = (key) => {\n\t\tvisited.add(key);\n\t\trecStack.add(key);\n\t\tconst job = jobs.find((j) => j.key === key);\n\t\tif (job) {\n\t\t\tfor (const depKey of job.dependsOn) if (!visited.has(depKey)) {\n\t\t\t\tif (detectCycle(depKey)) return true;\n\t\t\t} else if (recStack.has(depKey)) return true;\n\t\t}\n\t\trecStack.delete(key);\n\t\treturn false;\n\t};\n\tfor (const job of jobs) if (!visited.has(job.key)) {\n\t\tif (detectCycle(job.key)) throw new Error(`Dependency cycle detected involving key: ${job.key}`);\n\t}\n}\n/**\n* Validates a batch mutation request for common errors\n*/\nfunction validateBatchRequest(jobs) {\n\tvalidateBatchStructure(jobs);\n\tfor (const job of jobs) if (job.actions.length === 0) throw new Error(`Job '${job.key}' has empty actions array`);\n}\n/**\n* Validates a batch load request for common errors\n*/\nfunction validateBatchLoadRequest(jobs) {\n\tvalidateBatchStructure(jobs);\n\tfor (const job of jobs) if (job.operations.length === 0) throw new Error(`Job '${job.key}' has empty operations array`);\n}\n/**\n* Validates that all actions in a job match the declared scope\n*/\nfunction validateActionScopes(job) {\n\tfor (const action of job.actions) {\n\t\tconst actionScope = action.scope || \"global\";\n\t\tif (actionScope !== job.scope) throw new Error(`Job '${job.key}' declares scope '${job.scope}' but action has scope '${actionScope}'`);\n\t}\n}\n/**\n* Validates that all operations in a job match the declared scope\n*/\nfunction validateOperationScopes(job) {\n\tfor (const operation of job.operations) {\n\t\tconst operationScope = operation.action.scope || \"global\";\n\t\tif (operationScope !== job.scope) throw new Error(`Job '${job.key}' declares scope '${job.scope}' but operation has scope '${operationScope}'`);\n\t}\n}\n/**\n* Performs topological sort on jobs based on dependencies\n*/\nfunction topologicalSort(jobs) {\n\tconst result = [];\n\tconst visited = /* @__PURE__ */ new Set();\n\tconst visit = (key) => {\n\t\tif (visited.has(key)) return;\n\t\tvisited.add(key);\n\t\tconst job = jobs.find((j) => j.key === key);\n\t\tif (job) for (const depKey of job.dependsOn) visit(depKey);\n\t\tresult.push(key);\n\t};\n\tfor (const job of jobs) visit(job.key);\n\treturn result;\n}\n/**\n* Converts an Error or string to ErrorInfo\n*/\nfunction toErrorInfo$1(error) {\n\tif (error instanceof Error) return {\n\t\tmessage: error.message,\n\t\tstack: error.stack || (/* @__PURE__ */ new Error()).stack || \"\"\n\t};\n\treturn {\n\t\tmessage: error,\n\t\tstack: (/* @__PURE__ */ new Error()).stack || \"\"\n\t};\n}\n/**\n* Filters paged results by document type\n*/\nfunction filterByType(results, type) {\n\treturn {\n\t\tresults: results.results.filter((document) => document.header.documentType === type),\n\t\toptions: results.options,\n\t\tnextCursor: results.nextCursor,\n\t\tnext: results.next ? async () => {\n\t\t\treturn filterByType(await results.next(), type);\n\t\t} : void 0\n\t};\n}\n/**\n* Validates that all operations share the same scope.\n* Throws an error if any operation has a different scope.\n*/\nfunction getSharedOperationScope(operations) {\n\tif (operations.length === 0) throw new Error(\"No operations provided\");\n\tconst baseScope = operations[0].action.scope;\n\tfor (const [index, operation] of operations.entries()) {\n\t\tconst scope = operation.action.scope;\n\t\tif (scope !== baseScope) throw new Error(`All operations in load must share the same scope. Expected '${baseScope}', received '${scope}' at position ${index}`);\n\t}\n\treturn baseScope;\n}\n/**\n* Validates that all actions share the same scope.\n* Throws an error if any action has a different scope.\n*/\nfunction getSharedActionScope(actions) {\n\tif (actions.length === 0) throw new Error(\"No actions provided\");\n\tconst baseScope = actions[0].scope;\n\tfor (const action of actions) if (action.scope !== baseScope) throw new Error(`All actions must share the same scope. Expected '${baseScope}', received '${action.scope}'`);\n\treturn baseScope;\n}\n/**\n* Signs an action with the provided signer.\n* If the action already has valid signatures, it is returned unchanged.\n*/\nconst signAction = async (action, signer, signal) => {\n\tconst existingSignatures = action.context?.signer?.signatures;\n\tif (existingSignatures && existingSignatures.length > 0) return action;\n\tconst signature = await signer.signAction(action, signal);\n\treturn {\n\t\t...action,\n\t\tcontext: {\n\t\t\t...action.context,\n\t\t\tsigner: {\n\t\t\t\tuser: {\n\t\t\t\t\taddress: signer.user?.address || \"\",\n\t\t\t\t\tnetworkId: signer.user?.networkId || \"\",\n\t\t\t\t\tchainId: signer.user?.chainId || 0\n\t\t\t\t},\n\t\t\t\tapp: {\n\t\t\t\t\tname: signer.app?.name || \"\",\n\t\t\t\t\tkey: signer.app?.key || \"\"\n\t\t\t\t},\n\t\t\t\tsignatures: [signature]\n\t\t\t}\n\t\t}\n\t};\n};\n/**\n* Signs multiple actions with the provided signer\n*/\nconst signActions = async (actions, signer, signal) => {\n\treturn Promise.all(actions.map((action) => signAction(action, signer, signal)));\n};\nfunction buildSingleJobMeta(jobId, callerMeta) {\n\treturn {\n\t\t...callerMeta,\n\t\tbatchId: v4(),\n\t\tbatchJobIds: [jobId]\n\t};\n}\n//#endregion\n//#region src/shared/types.ts\n/**\n* Enum that determines deletion propagation.\n*/\nlet PropagationMode = /* @__PURE__ */ function(PropagationMode) {\n\tPropagationMode[\"None\"] = \"none\";\n\tPropagationMode[\"Cascade\"] = \"cascade\";\n\treturn PropagationMode;\n}({});\n/**\n* Enum that describes the type of relationship change.\n*/\nlet RelationshipChangeType = /* @__PURE__ */ function(RelationshipChangeType) {\n\tRelationshipChangeType[\"Added\"] = \"added\";\n\tRelationshipChangeType[\"Removed\"] = \"removed\";\n\treturn RelationshipChangeType;\n}({});\n/**\n* Job execution statuses\n*/\nlet JobStatus = /* @__PURE__ */ function(JobStatus) {\n\t/** Job is queued but not yet started */\n\tJobStatus[\"PENDING\"] = \"PENDING\";\n\t/** Job is currently being executed */\n\tJobStatus[\"RUNNING\"] = \"RUNNING\";\n\t/** Operations have been written to the operation store (JOB_WRITE_READY event) */\n\tJobStatus[\"WRITE_READY\"] = \"WRITE_READY\";\n\t/** Read models have finished indexing operations (JOB_READ_READY event) */\n\tJobStatus[\"READ_READY\"] = \"READ_READY\";\n\t/** Job failed (may be retried) */\n\tJobStatus[\"FAILED\"] = \"FAILED\";\n\treturn JobStatus;\n}({});\n//#endregion\n//#region src/client/drive-client.ts\n/**\n* Implementation of {@link IDriveClient}.\n*\n* Holds a back-reference to its parent {@link IReactorClient} for read and\n* single-document write primitives, plus direct access to {@link IReactor}\n* for batch execution. The back-reference is captured but never invoked\n* during construction, so the partial-`this` hazard does not apply.\n*/\nvar DriveClient = class {\n\tconstructor(client, logger, reactor, signer) {\n\t\tthis.client = client;\n\t\tthis.logger = logger;\n\t\tthis.reactor = reactor;\n\t\tthis.signer = signer;\n\t}\n\tasync create(input, signal) {\n\t\tthis.logger.verbose(\"drives.create(@input)\", input);\n\t\tconst driveDoc = driveCreateDocument({ global: {\n\t\t\tname: input.global.name || \"\",\n\t\t\ticon: input.global.icon ?? null,\n\t\t\tnodes: []\n\t\t} });\n\t\tif (input.preferredEditor) driveDoc.header.meta = {\n\t\t\t...driveDoc.header.meta,\n\t\t\tpreferredEditor: input.preferredEditor\n\t\t};\n\t\treturn this.client.create(driveDoc, void 0, signal);\n\t}\n\tasync addFile(driveIdentifier, document, parentFolder, signal) {\n\t\tthis.logger.verbose(\"drives.addFile(@driveIdentifier, @document, @parentFolder)\", driveIdentifier, document.header.id, parentFolder);\n\t\tconst documentId = document.header.id;\n\t\tconst documentActions = await signActions([\n\t\t\tcreateDocumentAction({\n\t\t\t\tmodel: document.header.documentType,\n\t\t\t\tversion: 0,\n\t\t\t\tdocumentId: document.header.id,\n\t\t\t\tsigning: {\n\t\t\t\t\tsignature: document.header.id,\n\t\t\t\t\tpublicKey: document.header.sig.publicKey,\n\t\t\t\t\tnonce: document.header.sig.nonce,\n\t\t\t\t\tcreatedAtUtcIso: document.header.createdAtUtcIso,\n\t\t\t\t\tdocumentType: document.header.documentType\n\t\t\t\t},\n\t\t\t\tslug: document.header.slug,\n\t\t\t\tname: document.header.name,\n\t\t\t\tbranch: document.header.branch,\n\t\t\t\tmeta: document.header.meta,\n\t\t\t\tprotocolVersions: document.header.protocolVersions ?? { \"base-reducer\": 2 }\n\t\t\t}),\n\t\t\tupgradeDocumentAction({\n\t\t\t\tdocumentId: document.header.id,\n\t\t\t\tmodel: document.header.documentType,\n\t\t\t\tfromVersion: 0,\n\t\t\t\ttoVersion: 1,\n\t\t\t\tinitialState: document.state\n\t\t\t}),\n\t\t\taddRelationshipAction(driveIdentifier, documentId, \"child\")\n\t\t], this.signer, signal);\n\t\tconst driveActions = await signActions([addFile({\n\t\t\tid: documentId,\n\t\t\tname: document.header.name || documentId,\n\t\t\tdocumentType: document.header.documentType,\n\t\t\tparentFolder\n\t\t})], this.signer, signal);\n\t\tconst batchResult = await this.reactor.executeBatch({ jobs: [{\n\t\t\tkey: \"document\",\n\t\t\tdocumentId,\n\t\t\tscope: getSharedActionScope(documentActions),\n\t\t\tbranch: \"main\",\n\t\t\tactions: documentActions,\n\t\t\tdependsOn: []\n\t\t}, {\n\t\t\tkey: \"drive\",\n\t\t\tdocumentId: driveIdentifier,\n\t\t\tscope: getSharedActionScope(driveActions),\n\t\t\tbranch: \"main\",\n\t\t\tactions: driveActions,\n\t\t\tdependsOn: [\"document\"]\n\t\t}] }, signal);\n\t\tconst completedJobs = await Promise.all(Object.values(batchResult.jobs).map((job) => this.client.waitForJob(job, signal)));\n\t\tfor (const job of completedJobs) if (job.status === JobStatus.FAILED) throw new Error(job.error?.message);\n\t\treturn this.reactor.get(documentId);\n\t}\n\tasync addFolder(driveIdentifier, name, parentFolder, signal) {\n\t\tthis.logger.verbose(\"drives.addFolder(@driveIdentifier, @name, @parentFolder)\", driveIdentifier, name, parentFolder);\n\t\tconst folderId = generateId();\n\t\tconst node = (await this.client.execute(driveIdentifier, \"main\", [addFolder({\n\t\t\tid: folderId,\n\t\t\tname,\n\t\t\tparentFolder\n\t\t})], signal)).state.global.nodes.find((n) => n.id === folderId);\n\t\tif (!node || !isFolderNode(node)) throw new Error(\"Folder creation failed\");\n\t\treturn node;\n\t}\n\tasync removeNode(driveIdentifier, nodeId, signal) {\n\t\tthis.logger.verbose(\"drives.removeNode(@driveIdentifier, @nodeId)\", driveIdentifier, nodeId);\n\t\tconst drive = await this.client.get(driveIdentifier, void 0, signal);\n\t\tconst node = drive.state.global.nodes.find((n) => n.id === nodeId);\n\t\tif (!node) {\n\t\t\tif (!await this.documentExists(nodeId, signal)) throw new Error(`Node ${nodeId} not found in drive ${driveIdentifier}`);\n\t\t\tawait this.removeFileNode(driveIdentifier, nodeId, signal);\n\t\t\treturn;\n\t\t}\n\t\tif (isFolderNode(node)) {\n\t\t\tconst fileDescendants = getDescendants(node, drive.state.global.nodes).filter(isFileNode);\n\t\t\tfor (const file of fileDescendants) await this.removeFileNode(driveIdentifier, file.id, signal);\n\t\t\tawait this.client.execute(driveIdentifier, \"main\", [deleteNode({ id: nodeId })], signal);\n\t\t\treturn;\n\t\t}\n\t\tawait this.removeFileNode(driveIdentifier, nodeId, signal);\n\t}\n\tasync renameNode(driveIdentifier, nodeId, name, signal) {\n\t\tthis.logger.verbose(\"drives.renameNode(@driveIdentifier, @nodeId, @name)\", driveIdentifier, nodeId, name);\n\t\tif ((await this.client.execute(nodeId, \"main\", [actions.setName({ name })], signal)).header.name !== name) throw new Error(\"Document rename did not apply\");\n\t\tconst node = (await this.client.execute(driveIdentifier, \"main\", [updateNode({\n\t\t\tid: nodeId,\n\t\t\tname\n\t\t})], signal)).state.global.nodes.find((n) => n.id === nodeId);\n\t\tif (!node) throw new Error(\"Node missing from drive after rename\");\n\t\treturn node;\n\t}\n\tasync setPreferredEditorOnNode(nodeId, preferredEditor, signal) {\n\t\tthis.logger.verbose(\"drives.setPreferredEditorOnNode(@nodeId, @preferredEditor)\", nodeId, preferredEditor);\n\t\treturn this.client.setPreferredEditor(nodeId, preferredEditor, \"main\", signal);\n\t}\n\tasync moveNode(driveIdentifier, srcNodeId, targetParentFolderId, signal) {\n\t\tthis.logger.verbose(\"drives.moveNode(@driveIdentifier, @srcNodeId, @targetParentFolderId)\", driveIdentifier, srcNodeId, targetParentFolderId);\n\t\treturn this.client.execute(driveIdentifier, \"main\", [moveNode({\n\t\t\tsrcFolder: srcNodeId,\n\t\t\ttargetParentFolder: targetParentFolderId\n\t\t})], signal);\n\t}\n\tasync copyNode(driveIdentifier, srcNodeId, targetParentFolderId, signal) {\n\t\tthis.logger.verbose(\"drives.copyNode(@driveIdentifier, @srcNodeId, @targetParentFolderId)\", driveIdentifier, srcNodeId, targetParentFolderId);\n\t\tconst drive = await this.client.get(driveIdentifier, void 0, signal);\n\t\tconst srcNode = drive.state.global.nodes.find((n) => n.id === srcNodeId);\n\t\tif (!srcNode) throw new Error(`Node ${srcNodeId} not found in drive ${driveIdentifier}`);\n\t\tconst copyPlan = generateNodesCopy({\n\t\t\tsrcId: srcNodeId,\n\t\t\ttargetParentFolder: targetParentFolderId,\n\t\t\ttargetName: srcNode.name\n\t\t}, () => generateId(), drive.state.global.nodes);\n\t\tconst resolvedNamesByTargetId = /* @__PURE__ */ new Map();\n\t\tfor (const entry of copyPlan) {\n\t\t\tconst node = drive.state.global.nodes.find((n) => n.id === entry.srcId);\n\t\t\tif (!node) continue;\n\t\t\tconst resolved = handleTargetNameCollisions({\n\t\t\t\tnodes: drive.state.global.nodes,\n\t\t\t\tsrcName: entry.targetName || node.name,\n\t\t\t\tsrcKind: isFileNode(node) ? \"file\" : \"folder\",\n\t\t\t\ttargetParentFolder: entry.targetParentFolder ?? null\n\t\t\t});\n\t\t\tresolvedNamesByTargetId.set(entry.targetId, resolved);\n\t\t}\n\t\tfor (const entry of copyPlan) {\n\t\t\tconst node = drive.state.global.nodes.find((n) => n.id === entry.srcId);\n\t\t\tif (!node || !isFileNode(node)) continue;\n\t\t\tconst srcDoc = await this.client.get(entry.srcId, void 0, signal);\n\t\t\tconst module = await this.client.getDocumentModelModule(srcDoc.header.documentType);\n\t\t\tconst duplicated = replayDocument(srcDoc.initialState, srcDoc.operations, module.reducer, createPresignedHeader(entry.targetId, srcDoc.header.documentType));\n\t\t\tconst resolvedName = resolvedNamesByTargetId.get(entry.targetId);\n\t\t\tif (resolvedName) duplicated.header.name = resolvedName;\n\t\t\tawait this.addFile(driveIdentifier, duplicated, entry.targetParentFolder ?? void 0, signal);\n\t\t}\n\t\treturn this.client.execute(driveIdentifier, \"main\", copyPlan.map((entry) => copyNode(entry)), signal);\n\t}\n\tasync getNode(driveIdentifier, nodeId, signal) {\n\t\tthis.logger.verbose(\"drives.getNode(@driveIdentifier, @nodeId)\", driveIdentifier, nodeId);\n\t\tconst node = (await this.client.get(driveIdentifier, void 0, signal)).state.global.nodes.find((n) => n.id === nodeId);\n\t\tif (!node) throw new Error(`Node ${nodeId} not found in drive ${driveIdentifier}`);\n\t\treturn node;\n\t}\n\tasync listNodes(driveIdentifier, parentFolder, paging, signal) {\n\t\tthis.logger.verbose(\"drives.listNodes(@driveIdentifier, @parentFolder, @paging)\", driveIdentifier, parentFolder, paging);\n\t\tconst allNodes = (await this.client.get(driveIdentifier, void 0, signal)).state.global.nodes;\n\t\tconst filtered = parentFolder === void 0 ? [...allNodes] : allNodes.filter((n) => (n.parentFolder ?? null) === parentFolder);\n\t\tconst { offset: startIndex, limit } = parsePagingOptions(paging, filtered.length);\n\t\tconst effective = paging ?? {\n\t\t\tcursor: \"\",\n\t\t\tlimit\n\t\t};\n\t\tconst endIndex = startIndex + limit;\n\t\treturn {\n\t\t\tresults: filtered.slice(startIndex, endIndex),\n\t\t\toptions: effective,\n\t\t\t...endIndex < filtered.length ? { nextCursor: String(endIndex) } : {},\n\t\t\ttotalCount: filtered.length\n\t\t};\n\t}\n\tasync documentExists(documentId, signal) {\n\t\ttry {\n\t\t\tawait this.client.get(documentId, void 0, signal);\n\t\t\treturn true;\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t}\n\tasync removeFileNode(driveId, fileId, signal) {\n\t\tconst relationshipActions = await signActions([removeRelationshipAction(driveId, fileId, \"child\")], this.signer, signal);\n\t\tconst driveActions = await signActions([deleteNode({ id: fileId })], this.signer, signal);\n\t\tconst batchResult = await this.reactor.executeBatch({ jobs: [{\n\t\t\tkey: \"relationship\",\n\t\t\tdocumentId: driveId,\n\t\t\tscope: getSharedActionScope(relationshipActions),\n\t\t\tbranch: \"main\",\n\t\t\tactions: relationshipActions,\n\t\t\tdependsOn: []\n\t\t}, {\n\t\t\tkey: \"drive\",\n\t\t\tdocumentId: driveId,\n\t\t\tscope: getSharedActionScope(driveActions),\n\t\t\tbranch: \"main\",\n\t\t\tactions: driveActions,\n\t\t\tdependsOn: [\"relationship\"]\n\t\t}] }, signal);\n\t\tconst completedJobs = await Promise.all(Object.values(batchResult.jobs).map((job) => this.client.waitForJob(job, signal)));\n\t\tfor (const job of completedJobs) if (job.status === JobStatus.FAILED) throw new Error(job.error?.message);\n\t\tconst deleteJob = await this.reactor.deleteDocument(fileId, this.signer, signal);\n\t\tconst deleteCompleted = await this.client.waitForJob(deleteJob, signal);\n\t\tif (deleteCompleted.status === JobStatus.FAILED) throw new Error(deleteCompleted.error?.message);\n\t}\n};\n//#endregion\n//#region src/shared/awaiter.ts\n/**\n* Checks if a job status is terminal (job has finished).\n* WRITE_READY is not terminal - it's an intermediate state.\n* Only READ_READY and FAILED are truly terminal.\n*/\nfunction isTerminalStatus(status) {\n\treturn status === JobStatus.READ_READY || status === JobStatus.FAILED;\n}\n/**\n* Event-driven implementation of IJobAwaiter.\n* Subscribes to operation events to detect job completion without polling.\n*/\nvar JobAwaiter = class {\n\tpendingJobs = /* @__PURE__ */ new Map();\n\tunsubscribers = [];\n\tconstructor(eventBus, getJobStatus) {\n\t\tthis.eventBus = eventBus;\n\t\tthis.getJobStatus = getJobStatus;\n\t\tthis.subscribeToEvents();\n\t}\n\tsubscribeToEvents() {\n\t\tthis.unsubscribers.push(this.eventBus.subscribe(ReactorEventTypes.JOB_WRITE_READY, async (_type, event) => {\n\t\t\tawait this.handleWriteReady(event);\n\t\t}));\n\t\tthis.unsubscribers.push(this.eventBus.subscribe(ReactorEventTypes.JOB_READ_READY, async (_type, event) => {\n\t\t\tawait this.handleReadReady(event);\n\t\t}));\n\t\tthis.unsubscribers.push(this.eventBus.subscribe(ReactorEventTypes.JOB_FAILED, async (_type, event) => {\n\t\t\tawait this.handleJobFailed(event);\n\t\t}));\n\t}\n\tshutdown() {\n\t\tfor (const unsubscribe of this.unsubscribers) unsubscribe();\n\t\tthis.unsubscribers = [];\n\t\tfor (const [, waiters] of this.pendingJobs) for (const waiter of waiters) waiter.reject(/* @__PURE__ */ new Error(\"JobAwaiter destroyed\"));\n\t\tthis.pendingJobs.clear();\n\t}\n\tasync waitForJob(jobId, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst currentStatus = await this.getJobStatus(jobId, signal);\n\t\tif (isTerminalStatus(currentStatus.status)) return currentStatus;\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst waiter = {\n\t\t\t\tresolve,\n\t\t\t\treject,\n\t\t\t\tsignal\n\t\t\t};\n\t\t\tconst existingWaiters = this.pendingJobs.get(jobId) || [];\n\t\t\texistingWaiters.push(waiter);\n\t\t\tthis.pendingJobs.set(jobId, existingWaiters);\n\t\t\tif (signal) {\n\t\t\t\tconst abortHandler = () => {\n\t\t\t\t\tconst waiters = this.pendingJobs.get(jobId);\n\t\t\t\t\tif (waiters) {\n\t\t\t\t\t\tconst index = waiters.indexOf(waiter);\n\t\t\t\t\t\tif (index !== -1) {\n\t\t\t\t\t\t\twaiters.splice(index, 1);\n\t\t\t\t\t\t\tif (waiters.length === 0) this.pendingJobs.delete(jobId);\n\t\t\t\t\t\t\twaiter.reject(/* @__PURE__ */ new Error(\"Operation aborted\"));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t\tsignal.addEventListener(\"abort\", abortHandler, { once: true });\n\t\t\t}\n\t\t});\n\t}\n\tasync handleWriteReady(event) {\n\t\tconst jobId = event.jobId;\n\t\tawait this.checkAndResolveWaiters(jobId);\n\t}\n\tasync handleReadReady(event) {\n\t\tconst jobId = event.jobId;\n\t\tawait this.checkAndResolveWaiters(jobId);\n\t}\n\tasync handleJobFailed(event) {\n\t\tawait this.checkAndResolveWaiters(event.jobId);\n\t}\n\tasync checkAndResolveWaiters(jobId) {\n\t\tconst waiters = this.pendingJobs.get(jobId);\n\t\tif (!waiters || waiters.length === 0) return;\n\t\ttry {\n\t\t\tconst activeWaiters = waiters.filter((w) => !w.signal?.aborted);\n\t\t\tif (activeWaiters.length === 0) {\n\t\t\t\tthis.pendingJobs.delete(jobId);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst jobInfo = await this.getJobStatus(jobId, activeWaiters[0].signal);\n\t\t\tif (isTerminalStatus(jobInfo.status)) {\n\t\t\t\tthis.pendingJobs.delete(jobId);\n\t\t\t\tfor (const waiter of activeWaiters) waiter.resolve(jobInfo);\n\t\t\t\tfor (const waiter of waiters) if (waiter.signal?.aborted) waiter.reject(/* @__PURE__ */ new Error(\"Operation aborted\"));\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tthis.pendingJobs.delete(jobId);\n\t\t\tfor (const waiter of waiters) waiter.reject(error instanceof Error ? error : new Error(String(error)));\n\t\t}\n\t}\n};\n//#endregion\n//#region src/client/cursor.ts\nconst COMPOSITE_PREFIX = \"c:\";\n/**\n* Returns true if the cursor is a composite multi-scope cursor.\n*/\nfunction isCompositeCursor(cursor) {\n\treturn cursor.startsWith(COMPOSITE_PREFIX);\n}\n/**\n* Encodes per-scope cursors into a single composite cursor string.\n* Only scopes that still have more results should be included.\n*/\nfunction encodeCompositeCursor(scopeCursors) {\n\treturn COMPOSITE_PREFIX + JSON.stringify(scopeCursors);\n}\n/**\n* Decodes a composite cursor string into a map of scope to cursor.\n* Throws if the cursor is not a valid composite cursor.\n*/\nfunction decodeCompositeCursor(cursor) {\n\tif (!cursor.startsWith(COMPOSITE_PREFIX)) throw new Error(\"Invalid composite cursor format\");\n\tconst json = cursor.slice(2);\n\ttry {\n\t\tconst parsed = JSON.parse(json);\n\t\tif (typeof parsed !== \"object\" || parsed === null || Array.isArray(parsed)) throw new Error(\"Invalid composite cursor format\");\n\t\treturn parsed;\n\t} catch (error) {\n\t\tif (error instanceof SyntaxError) throw new Error(\"Invalid composite cursor format\", { cause: error });\n\t\tthrow error;\n\t}\n}\n//#endregion\n//#region src/client/types.ts\n/**\n* Describes the types of document changes that can occur.\n*/\nlet DocumentChangeType = /* @__PURE__ */ function(DocumentChangeType) {\n\tDocumentChangeType[\"Created\"] = \"created\";\n\tDocumentChangeType[\"Deleted\"] = \"deleted\";\n\tDocumentChangeType[\"Updated\"] = \"updated\";\n\tDocumentChangeType[\"ParentAdded\"] = \"parent_added\";\n\tDocumentChangeType[\"ParentRemoved\"] = \"parent_removed\";\n\tDocumentChangeType[\"ChildAdded\"] = \"child_added\";\n\tDocumentChangeType[\"ChildRemoved\"] = \"child_removed\";\n\treturn DocumentChangeType;\n}({});\n//#endregion\n//#region src/client/reactor-client.ts\n/**\n* ReactorClient implementation that wraps lower-level APIs to provide\n* a simpler interface for document operations.\n*\n* Features:\n* - Wraps Jobs with Promises for easier async handling\n* - Manages signing of submitted Action objects\n* - Provides quality-of-life functions for common tasks\n* - Wraps subscription interface with ViewFilters\n*/\nvar ReactorClient = class {\n\tlogger;\n\treactor;\n\tsigner;\n\tsubscriptionManager;\n\tjobAwaiter;\n\tdocumentIndexer;\n\tdocumentView;\n\tdrives;\n\tconstructor(logger, reactor, signer, subscriptionManager, jobAwaiter, documentIndexer, documentView) {\n\t\tthis.logger = logger;\n\t\tthis.reactor = reactor;\n\t\tthis.signer = signer;\n\t\tthis.subscriptionManager = subscriptionManager;\n\t\tthis.jobAwaiter = jobAwaiter;\n\t\tthis.documentIndexer = documentIndexer;\n\t\tthis.documentView = documentView;\n\t\tthis.drives = new DriveClient(this, logger, reactor, signer);\n\t\tthis.logger.verbose(\"ReactorClient initialized\");\n\t}\n\t/**\n\t* Retrieves a list of document model modules.\n\t*/\n\tasync getDocumentModelModules(namespace, paging, signal) {\n\t\tthis.logger.verbose(\"getDocumentModels(@namespace, @paging)\", namespace, paging);\n\t\treturn this.reactor.getDocumentModels(namespace, paging, signal);\n\t}\n\t/**\n\t* Retrieves a specific document model module by document type.\n\t*\n\t* @param documentType - The document type identifier\n\t* @returns The document model module\n\t*/\n\tasync getDocumentModelModule(documentType) {\n\t\tconst module = (await this.reactor.getDocumentModels()).results.find((m) => m.documentModel.global.id === documentType);\n\t\tif (!module) throw new Error(`Document model module not found for type: ${documentType}`);\n\t\treturn module;\n\t}\n\t/**\n\t* Retrieves a specific PHDocument\n\t*/\n\tasync get(identifier, view, signal) {\n\t\tthis.logger.verbose(\"get(@identifier, @view)\", identifier, view);\n\t\treturn await this.reactor.getByIdOrSlug(identifier, view, void 0, signal);\n\t}\n\t/**\n\t* Retrieves operations for a document\n\t*/\n\tasync getOperations(documentIdentifier, view, filter, paging, signal) {\n\t\tthis.logger.verbose(\"getOperations(@documentIdentifier, @view, @filter, @paging)\", documentIdentifier, view, filter, paging);\n\t\tconst documentId = await this.documentView.resolveIdOrSlug(documentIdentifier, view, void 0, signal);\n\t\tif (paging?.cursor && isCompositeCursor(paging.cursor)) return this.getOperationsWithCompositeCursor(documentId, view, filter, paging, signal);\n\t\tconst operationsByScope = await this.reactor.getOperations(documentId, view, filter, paging, void 0, signal);\n\t\tconst scopeEntries = Object.entries(operationsByScope);\n\t\tconst effectivePaging = paging || {\n\t\t\tcursor: \"0\",\n\t\t\tlimit: 100\n\t\t};\n\t\tif (scopeEntries.length <= 1) {\n\t\t\tconst allOperations = scopeEntries.length === 1 ? [...scopeEntries[0][1].results] : [];\n\t\t\tallOperations.sort((a, b) => a.index - b.index);\n\t\t\treturn {\n\t\t\t\tresults: allOperations,\n\t\t\t\toptions: effectivePaging,\n\t\t\t\tnextCursor: scopeEntries.length === 1 ? scopeEntries[0][1].nextCursor : void 0\n\t\t\t};\n\t\t}\n\t\tconst allOperations = [];\n\t\tconst activeCursors = {};\n\t\tfor (const [scopeName, scopeResults] of scopeEntries) {\n\t\t\tallOperations.push(...scopeResults.results);\n\t\t\tif (scopeResults.nextCursor) activeCursors[scopeName] = scopeResults.nextCursor;\n\t\t}\n\t\tallOperations.sort((a, b) => a.index - b.index);\n\t\treturn {\n\t\t\tresults: allOperations,\n\t\t\toptions: effectivePaging,\n\t\t\tnextCursor: Object.keys(activeCursors).length > 0 ? encodeCompositeCursor(activeCursors) : void 0\n\t\t};\n\t}\n\tasync getOperationsWithCompositeCursor(documentId, view, filter, paging, signal) {\n\t\tconst scopeCursors = decodeCompositeCursor(paging.cursor);\n\t\tconst allOperations = [];\n\t\tconst activeCursors = {};\n\t\tfor (const [scopeName, cursor] of Object.entries(scopeCursors)) {\n\t\t\tconst scopeView = {\n\t\t\t\t...view,\n\t\t\t\tscopes: [scopeName]\n\t\t\t};\n\t\t\tconst scopePaging = {\n\t\t\t\tcursor,\n\t\t\t\tlimit: paging.limit\n\t\t\t};\n\t\t\tconst scopeResult = (await this.reactor.getOperations(documentId, scopeView, filter, scopePaging, void 0, signal))[scopeName];\n\t\t\tallOperations.push(...scopeResult.results);\n\t\t\tif (scopeResult.nextCursor) activeCursors[scopeName] = scopeResult.nextCursor;\n\t\t}\n\t\tallOperations.sort((a, b) => a.index - b.index);\n\t\treturn {\n\t\t\tresults: allOperations,\n\t\t\toptions: paging,\n\t\t\tnextCursor: Object.keys(activeCursors).length > 0 ? encodeCompositeCursor(activeCursors) : void 0\n\t\t};\n\t}\n\t/**\n\t* Retrieves outgoing relationships of a given type from a source document.\n\t*/\n\tasync getOutgoingRelationships(sourceIdentifier, relationshipType, view, paging, signal) {\n\t\tthis.logger.verbose(\"getOutgoingRelationships(@sourceIdentifier, @relationshipType, @view, @paging)\", sourceIdentifier, relationshipType, view, paging);\n\t\tconst sourceId = await this.documentView.resolveIdOrSlug(sourceIdentifier, view, void 0, signal);\n\t\tconst targetIds = (await this.documentIndexer.getOutgoing(sourceId, [relationshipType], void 0, void 0, signal)).results.map((rel) => rel.targetId);\n\t\tif (targetIds.length === 0) return {\n\t\t\tresults: [],\n\t\t\toptions: paging || {\n\t\t\t\tcursor: \"0\",\n\t\t\t\tlimit: 0\n\t\t\t}\n\t\t};\n\t\treturn this.reactor.find({ ids: targetIds }, view, paging, void 0, signal);\n\t}\n\t/**\n\t* Retrieves incoming relationships of a given type to a target document.\n\t*/\n\tasync getIncomingRelationships(targetIdentifier, relationshipType, view, paging, signal) {\n\t\tthis.logger.verbose(\"getIncomingRelationships(@targetIdentifier, @relationshipType, @view, @paging)\", targetIdentifier, relationshipType, view, paging);\n\t\tconst targetId = await this.documentView.resolveIdOrSlug(targetIdentifier, view, void 0, signal);\n\t\tconst sourceIds = (await this.documentIndexer.getIncoming(targetId, [relationshipType], void 0, void 0, signal)).results.map((rel) => rel.sourceId);\n\t\tif (sourceIds.length === 0) return {\n\t\t\tresults: [],\n\t\t\toptions: paging || {\n\t\t\t\tcursor: \"0\",\n\t\t\t\tlimit: 0\n\t\t\t}\n\t\t};\n\t\treturn this.reactor.find({ ids: sourceIds }, view, paging, void 0, signal);\n\t}\n\t/**\n\t* Filters documents by criteria and returns a list of them\n\t*/\n\tasync find(search, view, paging, signal) {\n\t\tthis.logger.verbose(\"find(@search, @view, @paging)\", search, view, paging);\n\t\treturn this.reactor.find(search, view, paging, void 0, signal);\n\t}\n\t/**\n\t* Creates a document and waits for completion\n\t*/\n\tasync create(document, parentIdentifier, signal) {\n\t\tthis.logger.verbose(\"create(@id, @parentIdentifier)\", document.header.id, parentIdentifier);\n\t\tconst documentId = document.header.id;\n\t\tconst createActions = await signActions([createDocumentAction({\n\t\t\tmodel: document.header.documentType,\n\t\t\tversion: 0,\n\t\t\tdocumentId,\n\t\t\tsigning: {\n\t\t\t\tsignature: documentId,\n\t\t\t\tpublicKey: document.header.sig.publicKey,\n\t\t\t\tnonce: document.header.sig.nonce,\n\t\t\t\tcreatedAtUtcIso: document.header.createdAtUtcIso,\n\t\t\t\tdocumentType: document.header.documentType\n\t\t\t},\n\t\t\tslug: document.header.slug,\n\t\t\tname: document.header.name,\n\t\t\tbranch: document.header.branch,\n\t\t\tmeta: document.header.meta,\n\t\t\tprotocolVersions: document.header.protocolVersions ?? { \"base-reducer\": 2 }\n\t\t}), upgradeDocumentAction({\n\t\t\tdocumentId,\n\t\t\tmodel: document.header.documentType,\n\t\t\tfromVersion: 0,\n\t\t\ttoVersion: document.state.document.version,\n\t\t\tinitialState: document.state\n\t\t})], this.signer, signal);\n\t\tconst jobs = [{\n\t\t\tkey: \"create\",\n\t\t\tdocumentId,\n\t\t\tscope: getSharedActionScope(createActions),\n\t\t\tbranch: \"main\",\n\t\t\tactions: createActions,\n\t\t\tdependsOn: []\n\t\t}];\n\t\tif (parentIdentifier) {\n\t\t\tconst parentActions = await signActions([addRelationshipAction(parentIdentifier, documentId, \"child\")], this.signer, signal);\n\t\t\tjobs.push({\n\t\t\t\tkey: \"parent\",\n\t\t\t\tdocumentId: parentIdentifier,\n\t\t\t\tscope: getSharedActionScope(parentActions),\n\t\t\t\tbranch: \"main\",\n\t\t\t\tactions: parentActions,\n\t\t\t\tdependsOn: [\"create\"]\n\t\t\t});\n\t\t}\n\t\tconst batchResult = await this.reactor.executeBatch({ jobs }, signal);\n\t\tconst completedJobs = await Promise.all(Object.values(batchResult.jobs).map((job) => this.waitForJob(job, signal)));\n\t\tfor (const job of completedJobs) if (job.status === JobStatus.FAILED) throw new Error(job.error?.message);\n\t\treturn await this.reactor.get(documentId);\n\t}\n\t/**\n\t* Creates an empty document and waits for completion\n\t*/\n\tasync createEmpty(documentModelType, options, signal) {\n\t\tthis.logger.verbose(\"createEmpty(@documentModelType, @options)\", documentModelType, options);\n\t\tconst matchingModules = (await this.reactor.getDocumentModels(void 0, void 0, signal)).results.filter((m) => m.documentModel.global.id === documentModelType);\n\t\tlet module;\n\t\tif (options?.documentModelVersion !== void 0) {\n\t\t\tmodule = matchingModules.find((m) => m.version === options.documentModelVersion);\n\t\t\tif (!module) throw new Error(`Document model not found for type: ${documentModelType} with version: ${options.documentModelVersion}`);\n\t\t} else {\n\t\t\tmodule = matchingModules.reduce((latest, current) => {\n\t\t\t\tif (latest === void 0) return current;\n\t\t\t\treturn (current.version ?? 0) > (latest.version ?? 0) ? current : latest;\n\t\t\t}, void 0);\n\t\t\tif (!module) throw new Error(`Document model not found for type: ${documentModelType}`);\n\t\t}\n\t\tconst document = module.utils.createDocument();\n\t\tdocument.state.document.version = module.version ?? 1;\n\t\treturn this.create(document, options?.parentIdentifier, signal);\n\t}\n\t/**\n\t* Creates an empty document in a drive as a single batched operation.\n\t* Delegates to {@link IDriveClient.addFile}.\n\t*\n\t* @deprecated Use `client.drives.addFile` instead. This method will be\n\t* removed in a future release.\n\t*/\n\tasync createDocumentInDrive(driveId, document, parentFolder, signal) {\n\t\treturn this.drives.addFile(driveId, document, parentFolder, signal);\n\t}\n\t/**\n\t* Applies a list of actions to a document and waits for completion\n\t*/\n\tasync execute(documentIdentifier, branch, actions, signal) {\n\t\tthis.logger.verbose(\"execute(@documentIdentifier, @branch, @count actions)\", documentIdentifier, branch, actions.length);\n\t\tconst signedActions = await signActions(actions, this.signer, signal);\n\t\tconst jobInfo = await this.reactor.execute(documentIdentifier, branch, signedActions, signal);\n\t\tconst completedJob = await this.waitForJob(jobInfo, signal);\n\t\tif (completedJob.status === JobStatus.FAILED) throw new Error(completedJob.error?.message);\n\t\tconst view = { branch };\n\t\treturn await this.reactor.getByIdOrSlug(documentIdentifier, view, completedJob.consistencyToken, signal);\n\t}\n\t/**\n\t* Submits a list of actions to a document\n\t*/\n\tasync executeAsync(documentIdentifier, branch, actions, signal) {\n\t\tthis.logger.verbose(\"executeAsync(@documentIdentifier, @branch, @count actions)\", documentIdentifier, branch, actions.length);\n\t\tconst signedActions = await signActions(actions, this.signer, signal);\n\t\treturn this.reactor.execute(documentIdentifier, branch, signedActions, signal);\n\t}\n\tasync executeBatch(request, signal) {\n\t\tthis.logger.verbose(\"executeBatch(@count jobs)\", request.jobs.length);\n\t\tconst signedJobs = await Promise.all(request.jobs.map(async (job) => ({\n\t\t\t...job,\n\t\t\tactions: await signActions(job.actions, this.signer, signal)\n\t\t})));\n\t\tconst batchResult = await this.reactor.executeBatch({ jobs: signedJobs }, signal);\n\t\tconst completedJobs = await Promise.all(Object.values(batchResult.jobs).map((job) => this.waitForJob(job, signal)));\n\t\tfor (const job of completedJobs) if (job.status === JobStatus.FAILED) throw new Error(job.error?.message);\n\t\treturn batchResult;\n\t}\n\t/**\n\t* Renames a document and waits for completion\n\t*/\n\tasync rename(documentIdentifier, name, branch = \"main\", signal) {\n\t\tthis.logger.verbose(\"rename(@documentIdentifier, @name, @branch)\", documentIdentifier, name, branch);\n\t\treturn this.execute(documentIdentifier, branch, [actions.setName(name)], signal);\n\t}\n\t/**\n\t* Updates the preferred editor recorded in the document header meta.\n\t* Pass `null` to clear it.\n\t*/\n\tasync setPreferredEditor(documentIdentifier, preferredEditor, branch = \"main\", signal) {\n\t\tthis.logger.verbose(\"setPreferredEditor(@documentIdentifier, @preferredEditor, @branch)\", documentIdentifier, preferredEditor, branch);\n\t\treturn this.execute(documentIdentifier, branch, [actions.setPreferredEditor(preferredEditor)], signal);\n\t}\n\t/**\n\t* Adds multiple documents as children to another and waits for completion\n\t*/\n\tasync addRelationship(sourceIdentifier, targetIdentifier, relationshipType, branch = \"main\", signal) {\n\t\tthis.logger.verbose(\"addRelationship(@sourceIdentifier, @targetIdentifier, @relationshipType, @branch)\", sourceIdentifier, targetIdentifier, relationshipType, branch);\n\t\tconst jobInfo = await this.reactor.addRelationship(sourceIdentifier, targetIdentifier, relationshipType, branch, this.signer, signal);\n\t\tconst completedJob = await this.waitForJob(jobInfo, signal);\n\t\tif (completedJob.status === JobStatus.FAILED) throw new Error(completedJob.error?.message);\n\t\treturn await this.reactor.getByIdOrSlug(sourceIdentifier, { branch }, completedJob.consistencyToken, signal);\n\t}\n\t/**\n\t* Removes a relationship between two documents and waits for completion.\n\t*/\n\tasync removeRelationship(sourceIdentifier, targetIdentifier, relationshipType, branch = \"main\", signal) {\n\t\tthis.logger.verbose(\"removeRelationship(@sourceIdentifier, @targetIdentifier, @relationshipType, @branch)\", sourceIdentifier, targetIdentifier, relationshipType, branch);\n\t\tconst jobInfo = await this.reactor.removeRelationship(sourceIdentifier, targetIdentifier, relationshipType, branch, this.signer, signal);\n\t\tconst completedJob = await this.waitForJob(jobInfo, signal);\n\t\tif (completedJob.status === JobStatus.FAILED) throw new Error(completedJob.error?.message);\n\t\treturn await this.reactor.getByIdOrSlug(sourceIdentifier, { branch }, completedJob.consistencyToken, signal);\n\t}\n\t/**\n\t* Moves a relationship from one source document to another and waits for completion.\n\t*/\n\tasync moveRelationship(sourceParentIdentifier, targetParentIdentifier, targetIdentifier, relationshipType, branch = \"main\", signal) {\n\t\tthis.logger.verbose(\"moveRelationship(@sourceParentIdentifier, @targetParentIdentifier, @targetIdentifier, @relationshipType, @branch)\", sourceParentIdentifier, targetParentIdentifier, targetIdentifier, relationshipType, branch);\n\t\tconst removeJobInfo = await this.reactor.removeRelationship(sourceParentIdentifier, targetIdentifier, relationshipType, branch, this.signer, signal);\n\t\tconst removeCompletedJob = await this.waitForJob(removeJobInfo, signal);\n\t\tif (removeCompletedJob.status === JobStatus.FAILED) throw new Error(removeCompletedJob.error?.message);\n\t\tconst addJobInfo = await this.reactor.addRelationship(targetParentIdentifier, targetIdentifier, relationshipType, branch, this.signer, signal);\n\t\tconst addCompletedJob = await this.waitForJob(addJobInfo, signal);\n\t\tif (addCompletedJob.status === JobStatus.FAILED) throw new Error(addCompletedJob.error?.message);\n\t\treturn {\n\t\t\tsource: await this.reactor.getByIdOrSlug(sourceParentIdentifier, { branch }, removeCompletedJob.consistencyToken, signal),\n\t\t\ttarget: await this.reactor.getByIdOrSlug(targetParentIdentifier, { branch }, addCompletedJob.consistencyToken, signal)\n\t\t};\n\t}\n\tasync loadBatch(request, signal) {\n\t\tthis.logger.verbose(\"loadBatch(@count jobs)\", request.jobs.length);\n\t\tconst result = await this.reactor.loadBatch(request, signal);\n\t\tconst completedJobs = await Promise.all(Object.entries(result.jobs).map(async ([key, jobInfo]) => {\n\t\t\treturn [key, await this.waitForJob(jobInfo, signal)];\n\t\t}));\n\t\tfor (const [, completedJob] of completedJobs) if (completedJob.status === JobStatus.FAILED) throw new Error(completedJob.error?.message);\n\t\treturn { jobs: Object.fromEntries(completedJobs) };\n\t}\n\t/**\n\t* Deletes a document and waits for completion\n\t*/\n\tasync deleteDocument(identifier, propagate, signal) {\n\t\tthis.logger.verbose(\"deleteDocument(@identifier, @propagate)\", identifier, propagate);\n\t\tconst jobs = [];\n\t\tif (propagate === PropagationMode.Cascade) {\n\t\t\tconst toDelete = new Set([identifier]);\n\t\t\tlet changed = true;\n\t\t\twhile (changed) {\n\t\t\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\t\t\tchanged = false;\n\t\t\t\tconst orphans = await this.documentIndexer.getOrphanedChildren([...toDelete], [\"child\"], signal);\n\t\t\t\tfor (const id of orphans) if (!toDelete.has(id)) {\n\t\t\t\t\ttoDelete.add(id);\n\t\t\t\t\tchanged = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (const descendantId of toDelete) {\n\t\t\t\tif (descendantId === identifier) continue;\n\t\t\t\tconst removalJobs = await this.removeAllIncomingRelationships(descendantId, signal);\n\t\t\t\tjobs.push(...removalJobs);\n\t\t\t\tconst jobInfo = await this.reactor.deleteDocument(descendantId, this.signer, signal);\n\t\t\t\tjobs.push(jobInfo);\n\t\t\t}\n\t\t}\n\t\tconst removalJobs = await this.removeAllIncomingRelationships(identifier, signal);\n\t\tjobs.push(...removalJobs);\n\t\tconst jobInfo = await this.reactor.deleteDocument(identifier, this.signer, signal);\n\t\tjobs.push(jobInfo);\n\t\tconst completedJobs = await Promise.all(jobs.map((job) => this.waitForJob(job, signal)));\n\t\tfor (const completedJob of completedJobs) if (completedJob.status === JobStatus.FAILED) throw new Error(completedJob.error?.message);\n\t}\n\t/**\n\t* Deletes documents and waits for completion\n\t*/\n\tasync deleteDocuments(identifiers, propagate, signal) {\n\t\tthis.logger.verbose(\"deleteDocuments(@count identifiers, @propagate)\", identifiers.length, propagate);\n\t\tconst deletePromises = identifiers.map((identifier) => this.deleteDocument(identifier, propagate, signal));\n\t\tawait Promise.all(deletePromises);\n\t}\n\t/**\n\t* Retrieves the status of a job\n\t*/\n\tasync getJobStatus(jobId, signal) {\n\t\tthis.logger.verbose(\"getJobStatus(@jobId)\", jobId);\n\t\treturn this.reactor.getJobStatus(jobId, signal);\n\t}\n\t/**\n\t* Waits for a job to complete\n\t*/\n\tasync waitForJob(jobId, signal) {\n\t\tconst id = typeof jobId === \"string\" ? jobId : jobId.id;\n\t\tthis.logger.verbose(\"waitForJob(@id)\", id);\n\t\treturn this.jobAwaiter.waitForJob(id, signal);\n\t}\n\t/**\n\t* Subscribes to changes for documents matching specified filters\n\t*/\n\tsubscribe(search, callback, view) {\n\t\tthis.logger.verbose(\"subscribe(@search, @view)\", search, view);\n\t\tconst unsubscribeCreated = this.subscriptionManager.onDocumentCreated((result) => {\n\t\t\t(async () => {\n\t\t\t\ttry {\n\t\t\t\t\tconst documents = await Promise.all(result.results.map((id) => this.reactor.get(id, view, void 0, void 0)));\n\t\t\t\t\tcallback({\n\t\t\t\t\t\ttype: DocumentChangeType.Created,\n\t\t\t\t\t\tdocuments\n\t\t\t\t\t});\n\t\t\t\t} catch {}\n\t\t\t})();\n\t\t}, search);\n\t\tconst unsubscribeDeleted = this.subscriptionManager.onDocumentDeleted((documentIds) => {\n\t\t\tcallback({\n\t\t\t\ttype: DocumentChangeType.Deleted,\n\t\t\t\tdocuments: [],\n\t\t\t\tcontext: { childId: documentIds[0] }\n\t\t\t});\n\t\t}, search);\n\t\tconst unsubscribeUpdated = this.subscriptionManager.onDocumentStateUpdated((result) => {\n\t\t\tcallback({\n\t\t\t\ttype: DocumentChangeType.Updated,\n\t\t\t\tdocuments: result.results\n\t\t\t});\n\t\t}, search, view);\n\t\tconst unsubscribeRelationship = this.subscriptionManager.onRelationshipChanged((parentId, childId, changeType) => {\n\t\t\tcallback({\n\t\t\t\ttype: changeType === RelationshipChangeType.Added ? DocumentChangeType.ChildAdded : DocumentChangeType.ChildRemoved,\n\t\t\t\tdocuments: [],\n\t\t\t\tcontext: {\n\t\t\t\t\tparentId,\n\t\t\t\t\tchildId\n\t\t\t\t}\n\t\t\t});\n\t\t}, search);\n\t\treturn () => {\n\t\t\tunsubscribeCreated();\n\t\t\tunsubscribeDeleted();\n\t\t\tunsubscribeUpdated();\n\t\t\tunsubscribeRelationship();\n\t\t};\n\t}\n\tasync removeAllIncomingRelationships(documentId, signal) {\n\t\tconst incoming = await this.documentIndexer.getIncoming(documentId, void 0, void 0, void 0, signal);\n\t\tconst jobs = [];\n\t\tfor (const rel of incoming.results) {\n\t\t\tconst jobInfo = await this.reactor.removeRelationship(rel.sourceId, documentId, rel.relationshipType, \"main\", this.signer, signal);\n\t\t\tjobs.push(jobInfo);\n\t\t}\n\t\treturn jobs;\n\t}\n};\n//#endregion\n//#region src/queue/types.ts\n/**\n* State of a job in the queue\n*/\nlet JobQueueState = /* @__PURE__ */ function(JobQueueState) {\n\tJobQueueState[JobQueueState[\"UNKNOWN\"] = -1] = \"UNKNOWN\";\n\tJobQueueState[JobQueueState[\"PREPROCESSING\"] = 0] = \"PREPROCESSING\";\n\tJobQueueState[JobQueueState[\"PENDING\"] = 1] = \"PENDING\";\n\tJobQueueState[JobQueueState[\"READY\"] = 2] = \"READY\";\n\tJobQueueState[JobQueueState[\"RUNNING\"] = 3] = \"RUNNING\";\n\tJobQueueState[JobQueueState[\"RESOLVED\"] = 4] = \"RESOLVED\";\n\treturn JobQueueState;\n}({});\n/**\n* Event types for the queue system\n*/\nconst QueueEventTypes = { JOB_AVAILABLE: 1e4 };\n//#endregion\n//#region src/executor/job-result-handler.ts\nfunction toErrorInfo(error) {\n\tif (error instanceof Error) return {\n\t\tmessage: error.message,\n\t\tstack: error.stack || (/* @__PURE__ */ new Error()).stack || \"\"\n\t};\n\treturn {\n\t\tmessage: error,\n\t\tstack: (/* @__PURE__ */ new Error()).stack || \"\"\n\t};\n}\nvar JobResultHandler = class {\n\tconstructor(queue, jobTracker, eventBus, resolver, logger) {\n\t\tthis.queue = queue;\n\t\tthis.jobTracker = jobTracker;\n\t\tthis.eventBus = eventBus;\n\t\tthis.resolver = resolver;\n\t\tthis.logger = logger;\n\t}\n\tasync handleResult(handle, result, callbacks) {\n\t\tif (result.success) {\n\t\t\thandle.complete();\n\t\t\tif (this.hasCreateDocumentAction(handle.job)) await callbacks.flushDeferredFor(handle.job.documentId);\n\t\t\treturn;\n\t\t}\n\t\tif (result.error && ModuleNotFoundError.isError(result.error)) {\n\t\t\tlet modelLoaded = false;\n\t\t\ttry {\n\t\t\t\tawait this.resolver.ensureModelLoaded(result.error.documentType);\n\t\t\t\tmodelLoaded = true;\n\t\t\t} catch {}\n\t\t\tif (modelLoaded) {\n\t\t\t\tconst errorInfo = toErrorInfo(result.error);\n\t\t\t\ttry {\n\t\t\t\t\tawait this.queue.retryJob(handle.job.id, errorInfo);\n\t\t\t\t\treturn;\n\t\t\t\t} catch {}\n\t\t\t}\n\t\t}\n\t\tif (result.error && DocumentNotFoundError.isError(result.error)) {\n\t\t\thandle.defer();\n\t\t\tcallbacks.deferJob(handle.job.documentId, handle.job);\n\t\t\treturn;\n\t\t}\n\t\tif (result.error && DocumentDeletedError.isError(result.error)) {\n\t\t\tconst errorInfo = toErrorInfo(result.error);\n\t\t\tthis.jobTracker.markFailed(handle.job.id, errorInfo, handle.job);\n\t\t\tthis.eventBus.emit(ReactorEventTypes.JOB_FAILED, {\n\t\t\t\tjobId: handle.job.id,\n\t\t\t\terror: result.error,\n\t\t\t\tjob: handle.job\n\t\t\t}).catch(() => {});\n\t\t\thandle.fail(errorInfo);\n\t\t\treturn;\n\t\t}\n\t\tconst retryCount = handle.job.retryCount || 0;\n\t\tif (retryCount < (handle.job.maxRetries || 0)) {\n\t\t\tconst currentErrorInfo = result.error ? toErrorInfo(result.error) : toErrorInfo(\"Unknown error\");\n\t\t\ttry {\n\t\t\t\tawait this.queue.retryJob(handle.job.id, currentErrorInfo);\n\t\t\t} catch (error) {\n\t\t\t\tconst retryErrorInfo = toErrorInfo(error instanceof Error ? error : \"Failed to retry job\");\n\t\t\t\tthis.jobTracker.markFailed(handle.job.id, retryErrorInfo, handle.job);\n\t\t\t\tthis.eventBus.emit(ReactorEventTypes.JOB_FAILED, {\n\t\t\t\t\tjobId: handle.job.id,\n\t\t\t\t\terror: result.error ?? new Error(retryErrorInfo.message),\n\t\t\t\t\tjob: handle.job\n\t\t\t\t}).catch(() => {});\n\t\t\t\thandle.fail(retryErrorInfo);\n\t\t\t}\n\t\t} else {\n\t\t\tconst currentErrorInfo = result.error ? toErrorInfo(result.error) : toErrorInfo(\"Unknown error\");\n\t\t\tconst fullErrorInfo = this.formatErrorHistory(handle.job.errorHistory, currentErrorInfo, retryCount + 1);\n\t\t\tthis.jobTracker.markFailed(handle.job.id, fullErrorInfo, handle.job);\n\t\t\tthis.eventBus.emit(ReactorEventTypes.JOB_FAILED, {\n\t\t\t\tjobId: handle.job.id,\n\t\t\t\terror: result.error ?? new Error(fullErrorInfo.message),\n\t\t\t\tjob: handle.job\n\t\t\t}).catch(() => {});\n\t\t\thandle.fail(fullErrorInfo);\n\t\t}\n\t}\n\thasCreateDocumentAction(job) {\n\t\tfor (const action of job.actions) if (action.type === \"CREATE_DOCUMENT\") return true;\n\t\tfor (const operation of job.operations) if (operation.action.type === \"CREATE_DOCUMENT\") return true;\n\t\treturn false;\n\t}\n\tformatErrorHistory(errorHistory, currentError, totalAttempts) {\n\t\tconst allErrors = [...errorHistory, currentError];\n\t\tif (allErrors.length === 1) return currentError;\n\t\tconst messageLines = [`Job failed after ${totalAttempts} attempts:`];\n\t\tconst stackLines = [];\n\t\tallErrors.forEach((error, index) => {\n\t\t\tmessageLines.push(`[Attempt ${index + 1}] ${error.message}`);\n\t\t\tstackLines.push(`[Attempt ${index + 1}] Stack trace:\\n${error.stack}`);\n\t\t});\n\t\treturn {\n\t\t\tmessage: messageLines.join(\"\\n\"),\n\t\t\tstack: stackLines.join(\"\\n\\n\")\n\t\t};\n\t}\n};\n//#endregion\n//#region src/executor/types.ts\n/**\n* Event types for the job executor\n*/\nconst JobExecutorEventTypes = {\n\tJOB_STARTED: 2e4,\n\tJOB_COMPLETED: 20001,\n\tJOB_FAILED: 20002,\n\tEXECUTOR_STARTED: 20003,\n\tEXECUTOR_STOPPED: 20004\n};\n//#endregion\n//#region src/executor/worker-pool-router.ts\n/**\n* Sticky-by-document routing for the executor worker pool.\n*\n* Hashes `documentId` to a stable bucket so every job on a given document\n* lands on the same worker. The queue already enforces \"at most one job\n* executing per document at a time\", so a sticky worker always sees a fresh\n* post-commit snapshot before its next job on that document — which keeps\n* the per-worker `IWriteCache` and `IDocumentMetaCache` coherent for free.\n*\n* The hash is FNV-1a 32-bit: deterministic, dependency-free, and stable\n* across processes (so the same routing decision can be reproduced anywhere\n* the documentId is known).\n*\n* @see Executor Worker Pool Design wiki page\n* (Powerhouse board wiki id: d400d711-f07e-4389-a226-4e9fdd4fa8ba)\n*/\nconst FNV_OFFSET_BASIS = 2166136261;\nconst FNV_PRIME = 16777619;\nfunction hashDocumentId(documentId) {\n\tlet hash = FNV_OFFSET_BASIS;\n\tfor (let i = 0; i < documentId.length; i++) {\n\t\thash ^= documentId.charCodeAt(i);\n\t\thash = Math.imul(hash, FNV_PRIME);\n\t}\n\treturn hash >>> 0;\n}\nfunction bucketFor(documentId, numWorkers) {\n\tif (numWorkers < 1) throw new Error(`bucketFor: numWorkers must be >= 1 (got ${numWorkers})`);\n\treturn hashDocumentId(documentId) % numWorkers;\n}\n//#endregion\n//#region src/executor/worker-pool-job-executor-manager.ts\n/**\n* Action types whose application invalidates the parent's collection\n* membership cache. Mirrors the in-process invalidation pattern in\n* `document-action-handler.ts` (the worker pool relocates that work to\n* the parent because the cache lives there).\n*/\nconst MEMBERSHIP_INVALIDATING_ACTIONS = new Set([\n\t\"ADD_RELATIONSHIP\",\n\t\"REMOVE_RELATIONSHIP\",\n\t\"UPDATE_RELATIONSHIP\",\n\t\"DELETE_DOCUMENT\"\n]);\n/**\n* Manages a pool of executor workers and dispatches jobs across them with\n* sticky-by-documentId routing. Replaces `SimpleJobExecutorManager` when\n* the worker pool is enabled.\n*\n* Responsibilities that stay on the parent (not in the worker):\n* - Dequeueing from `IQueue` and routing to the matching worker bucket.\n* - Emitting `JOB_RUNNING` and `JOB_WRITE_READY` events; the worker's\n* local event bus is a no-op stub.\n* - Maintaining the deferred-jobs map for `DocumentNotFoundError`.\n* - Owning the authoritative `ICollectionMembershipCache` — workers do\n* not query it. Each result enriches the outgoing `JOB_WRITE_READY`\n* with `collectionMemberships` and invalidates targets named by\n* relationship/delete operations before the lookup.\n*\n* @see Executor Worker Pool Design wiki page\n* (Powerhouse board wiki id: d400d711-f07e-4389-a226-4e9fdd4fa8ba)\n*/\nvar WorkerPoolJobExecutorManager = class {\n\tworkers = [];\n\tisRunning = false;\n\tactiveJobs = 0;\n\ttotalJobsProcessed = 0;\n\tunsubscribe;\n\tdeferredJobs = /* @__PURE__ */ new Map();\n\tresultHandler;\n\tjobTimeoutMs;\n\tconstructor(workerFactory, eventBus, queue, jobTracker, logger, resolver, collectionMembershipCache, jobTimeoutMs = 3e4) {\n\t\tthis.workerFactory = workerFactory;\n\t\tthis.eventBus = eventBus;\n\t\tthis.queue = queue;\n\t\tthis.jobTracker = jobTracker;\n\t\tthis.logger = logger;\n\t\tthis.resolver = resolver;\n\t\tthis.collectionMembershipCache = collectionMembershipCache;\n\t\tthis.jobTimeoutMs = jobTimeoutMs;\n\t\tthis.resultHandler = new JobResultHandler(queue, jobTracker, eventBus, resolver, logger);\n\t}\n\tasync start(numWorkers) {\n\t\tif (this.isRunning) throw new Error(\"WorkerPoolJobExecutorManager is already running\");\n\t\tif (numWorkers < 1) throw new Error(\"Number of workers must be at least 1\");\n\t\tthis.workers = Array.from({ length: numWorkers }, (_, i) => this.workerFactory(i));\n\t\tawait Promise.all(this.workers.map((w) => w.start()));\n\t\tthis.unsubscribe = this.eventBus.subscribe(QueueEventTypes.JOB_AVAILABLE, async () => {\n\t\t\tawait this.tryDispatchAll();\n\t\t});\n\t\tthis.isRunning = true;\n\t\tawait this.tryDispatchAll();\n\t}\n\tasync stop(graceful = true) {\n\t\tif (!this.isRunning) return;\n\t\tif (this.unsubscribe) {\n\t\t\tthis.unsubscribe();\n\t\t\tthis.unsubscribe = void 0;\n\t\t}\n\t\tif (graceful) while (this.activeJobs > 0) await new Promise((resolve) => setTimeout(resolve, 10));\n\t\tfor (const [, jobs] of this.deferredJobs) for (const job of jobs) {\n\t\t\tconst errorInfo = toErrorInfo(new DocumentNotFoundError(job.documentId));\n\t\t\tthis.jobTracker.markFailed(job.id, errorInfo, job);\n\t\t\tthis.eventBus.emit(ReactorEventTypes.JOB_FAILED, {\n\t\t\t\tjobId: job.id,\n\t\t\t\terror: new DocumentNotFoundError(job.documentId),\n\t\t\t\tjob\n\t\t\t}).catch(() => {});\n\t\t}\n\t\tthis.deferredJobs.clear();\n\t\tawait Promise.all(this.workers.map((w) => w.shutdown(graceful).catch((err) => {\n\t\t\tthis.logger.warn(\"worker shutdown failed: @Error\", err);\n\t\t})));\n\t\tthis.workers = [];\n\t\tthis.isRunning = false;\n\t}\n\t/**\n\t* Worker-pool mode has no in-process `IJobExecutor` instances — the\n\t* executors live in worker threads behind `IExecutorWorker` handles.\n\t* Returns an empty array; callers that need pool-aware introspection\n\t* should use `getStatus()` instead.\n\t*/\n\tgetExecutors() {\n\t\treturn [];\n\t}\n\t/**\n\t* Broadcasts a `load-model` request to every running worker in parallel.\n\t* Rejects with the first worker's failure if any worker rejects (after\n\t* waiting for all in-flight broadcasts to settle). Workers that already\n\t* have the model registered respond with a `DuplicateModuleError`-rooted\n\t* failure; those are treated as success on the broadcast level so that\n\t* a model registered on some workers but not others still converges.\n\t*/\n\tasync loadModel(entry) {\n\t\tif (this.workers.length === 0) return;\n\t\tconst failures = (await Promise.allSettled(this.workers.map((w) => w.loadModel(entry)))).filter((r) => r.status === \"rejected\").filter((r) => !isDuplicateModuleFailure(r.reason));\n\t\tif (failures.length === 0) return;\n\t\tfor (const f of failures) this.logger.error(\"worker failed to load model @entry: @error\", entry, f.reason);\n\t\tthrow failures[0].reason instanceof Error ? failures[0].reason : new Error(String(failures[0].reason));\n\t}\n\tgetStatus() {\n\t\treturn {\n\t\t\tisRunning: this.isRunning,\n\t\t\tnumExecutors: this.workers.length,\n\t\t\tactiveJobs: this.activeJobs,\n\t\t\ttotalJobsProcessed: this.totalJobsProcessed\n\t\t};\n\t}\n\tasync tryDispatchAll() {\n\t\tif (!this.isRunning && this.workers.length === 0) return;\n\t\tawait Promise.all(this.workers.map((worker) => this.tryDispatchFor(worker)));\n\t}\n\tasync tryDispatchFor(worker) {\n\t\tif (!worker.isIdle()) return;\n\t\tconst index = worker.index;\n\t\tconst numWorkers = this.workers.length;\n\t\tconst predicate = (meta) => bucketFor(meta.documentId, numWorkers) === index;\n\t\tlet handle;\n\t\ttry {\n\t\t\thandle = await this.queue.dequeueNextMatching(predicate);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Error dequeueing next job: @Error\", error);\n\t\t\treturn;\n\t\t}\n\t\tif (!handle) return;\n\t\thandle.start();\n\t\tthis.activeJobs++;\n\t\tthis.jobTracker.markRunning(handle.job.id);\n\t\tconst runningEvent = {\n\t\t\tjobId: handle.job.id,\n\t\t\tjobMeta: handle.job.meta\n\t\t};\n\t\tthis.eventBus.emit(ReactorEventTypes.JOB_RUNNING, runningEvent).catch(() => {});\n\t\tconst workerId = worker.workerId;\n\t\tconst startedEvent = {\n\t\t\tjob: handle.job,\n\t\t\tstartedAt: (/* @__PURE__ */ new Date()).toISOString(),\n\t\t\tworkerId\n\t\t};\n\t\tthis.eventBus.emit(JobExecutorEventTypes.JOB_STARTED, startedEvent).catch(() => {});\n\t\tconst signal = AbortSignal.timeout(this.jobTimeoutMs);\n\t\tlet outcome;\n\t\ttry {\n\t\t\toutcome = await worker.execute(handle.job, signal);\n\t\t} catch (error) {\n\t\t\tconst errorInfo = toErrorInfo(error instanceof Error ? error : String(error));\n\t\t\tif (isWorkerTransportError(error)) {\n\t\t\t\tawait this.handleWorkerTransportFailure(worker, handle.job, errorInfo);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\thandle.fail(errorInfo);\n\t\t\tthis.activeJobs--;\n\t\t\tthis.jobTracker.markFailed(handle.job.id, errorInfo, handle.job);\n\t\t\tthis.eventBus.emit(ReactorEventTypes.JOB_FAILED, {\n\t\t\t\tjobId: handle.job.id,\n\t\t\t\terror: new Error(errorInfo.message),\n\t\t\t\tjob: handle.job\n\t\t\t}).catch(() => {});\n\t\t\tconst failedEvent = {\n\t\t\t\tjob: handle.job,\n\t\t\t\terror: errorInfo.message,\n\t\t\t\twillRetry: false,\n\t\t\t\tretryCount: 0,\n\t\t\t\tworkerId\n\t\t\t};\n\t\t\tthis.eventBus.emit(JobExecutorEventTypes.JOB_FAILED, failedEvent).catch(() => {});\n\t\t\tawait this.tryDispatchFor(worker);\n\t\t\treturn;\n\t\t}\n\t\tif (outcome.result.success) {\n\t\t\tthis.totalJobsProcessed++;\n\t\t\tconst completedEvent = {\n\t\t\t\tjob: handle.job,\n\t\t\t\tresult: outcome.result,\n\t\t\t\tworkerId\n\t\t\t};\n\t\t\tthis.eventBus.emit(JobExecutorEventTypes.JOB_COMPLETED, completedEvent).catch(() => {});\n\t\t} else {\n\t\t\tconst failedEvent = {\n\t\t\t\tjob: handle.job,\n\t\t\t\terror: outcome.result.error?.message ?? \"unknown\",\n\t\t\t\twillRetry: false,\n\t\t\t\tretryCount: 0,\n\t\t\t\tworkerId\n\t\t\t};\n\t\t\tthis.eventBus.emit(JobExecutorEventTypes.JOB_FAILED, failedEvent).catch(() => {});\n\t\t}\n\t\tif (outcome.result.success && outcome.writeReady) this.emitWriteReady(handle.job, outcome.writeReady).catch((error) => {\n\t\t\tthis.logger.error(\"emitWriteReady failed for job @jobId: @Error\", { jobId: handle.job.id }, error);\n\t\t});\n\t\tawait this.resultHandler.handleResult(handle, outcome.result, {\n\t\t\tdeferJob: (documentId, job) => {\n\t\t\t\tconst existing = this.deferredJobs.get(documentId) ?? [];\n\t\t\t\texisting.push(job);\n\t\t\t\tthis.deferredJobs.set(documentId, existing);\n\t\t\t},\n\t\t\tflushDeferredFor: (documentId) => this.flushDeferredJobs(documentId)\n\t\t});\n\t\tthis.activeJobs--;\n\t\tawait this.tryDispatchFor(worker);\n\t}\n\tasync emitWriteReady(job, payload) {\n\t\tthis.invalidateMembershipsFor(payload.operations);\n\t\tconst documentIds = [...new Set(payload.operations.map((op) => op.context.documentId))];\n\t\tlet collectionMemberships = {};\n\t\ttry {\n\t\t\tcollectionMemberships = await this.collectionMembershipCache.getCollectionsForDocuments(documentIds);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Failed to load collection memberships for JOB_WRITE_READY: @Error\", error);\n\t\t}\n\t\tconst event = {\n\t\t\tjobId: job.id,\n\t\t\toperations: payload.operations,\n\t\t\tjobMeta: payload.jobMeta,\n\t\t\tcollectionMemberships\n\t\t};\n\t\ttry {\n\t\t\tawait this.eventBus.emit(ReactorEventTypes.JOB_WRITE_READY, event);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Failed to emit JOB_WRITE_READY event: @Error\", error);\n\t\t}\n\t}\n\tinvalidateMembershipsFor(operations) {\n\t\tfor (const op of operations) {\n\t\t\tconst actionType = op.operation.action.type;\n\t\t\tif (!MEMBERSHIP_INVALIDATING_ACTIONS.has(actionType)) continue;\n\t\t\tconst target = extractMembershipTarget(op);\n\t\t\tif (target) this.collectionMembershipCache.invalidate(target);\n\t\t}\n\t}\n\t/**\n\t* Handle a worker-transport failure (worker exited / init failed / abort\n\t* timed out) detected while `worker.execute` was in flight. Re-enqueues\n\t* the in-flight job via `queue.retryJob` so it is retried on a healthy\n\t* worker, then replaces the dead worker with a fresh handle and resumes\n\t* dispatch on the same bucket. Does NOT emit JOB_FAILED — the job is\n\t* not failed, only the worker is.\n\t*/\n\tasync handleWorkerTransportFailure(dead, job, errorInfo) {\n\t\tthis.logger.warn(\"worker transport error during execute; retrying job @jobId on a replacement worker: @error\", {\n\t\t\tjobId: job.id,\n\t\t\tworkerId: dead.workerId\n\t\t}, errorInfo.message);\n\t\tthis.activeJobs--;\n\t\tawait this.replaceWorker(dead);\n\t\ttry {\n\t\t\tawait this.queue.retryJob(job.id, errorInfo);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"failed to re-enqueue job after worker transport error: @Error\", error);\n\t\t}\n\t}\n\t/**\n\t* Replace a dead worker at its existing pool index with a fresh handle\n\t* produced by `workerFactory`. Awaits `start()` on the replacement so it\n\t* is ready before dispatch resumes. On replacement failure the slot is\n\t* left empty (the index becomes a hole that subsequent retries will\n\t* route to no worker) and the error is logged — the manager keeps\n\t* running so other buckets continue to make progress.\n\t*/\n\tasync replaceWorker(dead) {\n\t\tconst deadIndex = dead.index;\n\t\tif (this.workers[deadIndex] !== dead) return;\n\t\tlet fresh;\n\t\ttry {\n\t\t\tfresh = this.workerFactory(deadIndex);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"workerFactory threw while replacing dead worker at index @index: @Error\", deadIndex, error);\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tawait fresh.start();\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"replacement worker at index @index failed to start: @Error\", deadIndex, error);\n\t\t\treturn;\n\t\t}\n\t\tthis.workers[deadIndex] = fresh;\n\t\tawait this.tryDispatchFor(fresh);\n\t}\n\tasync flushDeferredJobs(documentId) {\n\t\tconst jobs = this.deferredJobs.get(documentId);\n\t\tif (!jobs || jobs.length === 0) return;\n\t\tthis.deferredJobs.delete(documentId);\n\t\tfor (const job of jobs) try {\n\t\t\tawait this.queue.enqueue(job);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Error re-enqueuing deferred job: @Error\", error);\n\t\t}\n\t}\n};\nfunction isWorkerTransportError(error) {\n\treturn error instanceof WorkerExitedError || error instanceof WorkerInitFailedError || error instanceof WorkerAbortTimeoutError;\n}\nfunction isDuplicateModuleFailure(reason) {\n\tif (!(reason instanceof Error)) return false;\n\tif (reason.name === \"DuplicateModuleError\") return true;\n\tconst cause = reason.cause;\n\treturn cause instanceof Error && cause.name === \"DuplicateModuleError\";\n}\nfunction extractMembershipTarget(op) {\n\tconst actionType = op.operation.action.type;\n\tconst input = op.operation.action.input;\n\tif (actionType === \"ADD_RELATIONSHIP\" || actionType === \"REMOVE_RELATIONSHIP\" || actionType === \"UPDATE_RELATIONSHIP\") return input?.targetId;\n\tif (actionType === \"DELETE_DOCUMENT\") return input?.documentId ?? op.context.documentId;\n}\n//#endregion\n//#region src/executor/simple-job-executor-manager.ts\n/**\n* Manages multiple job executors and coordinates job distribution.\n* Listens for job available events and dispatches jobs to executors.\n*/\nvar SimpleJobExecutorManager = class {\n\texecutors = [];\n\tisRunning = false;\n\tactiveJobs = 0;\n\ttotalJobsProcessed = 0;\n\tunsubscribe;\n\tdeferredJobs = /* @__PURE__ */ new Map();\n\tresultHandler;\n\tjobTimeoutMs;\n\tconstructor(executorFactory, eventBus, queue, jobTracker, logger, resolver, jobTimeoutMs = 3e4) {\n\t\tthis.executorFactory = executorFactory;\n\t\tthis.eventBus = eventBus;\n\t\tthis.queue = queue;\n\t\tthis.jobTracker = jobTracker;\n\t\tthis.logger = logger;\n\t\tthis.resolver = resolver;\n\t\tthis.jobTimeoutMs = jobTimeoutMs;\n\t\tthis.resultHandler = new JobResultHandler(queue, jobTracker, eventBus, resolver, logger);\n\t}\n\tasync start(numExecutors) {\n\t\tif (this.isRunning) throw new Error(\"JobExecutorManager is already running\");\n\t\tif (numExecutors < 1) throw new Error(\"Number of executors must be at least 1\");\n\t\tthis.executors = [];\n\t\tfor (let i = 0; i < numExecutors; i++) this.executors.push(this.executorFactory());\n\t\tthis.unsubscribe = this.eventBus.subscribe(QueueEventTypes.JOB_AVAILABLE, async () => {\n\t\t\tif (this.activeJobs < this.executors.length) await this.processNextJob();\n\t\t});\n\t\tthis.isRunning = true;\n\t\tawait this.processExistingJobs();\n\t}\n\tasync stop(graceful = true) {\n\t\tif (!this.isRunning) return;\n\t\tif (this.unsubscribe) {\n\t\t\tthis.unsubscribe();\n\t\t\tthis.unsubscribe = void 0;\n\t\t}\n\t\tif (graceful) while (this.activeJobs > 0) await new Promise((resolve) => setTimeout(resolve, 100));\n\t\tfor (const [, jobs] of this.deferredJobs) for (const job of jobs) {\n\t\t\tconst errorInfo = toErrorInfo(new DocumentNotFoundError(job.documentId));\n\t\t\tthis.jobTracker.markFailed(job.id, errorInfo, job);\n\t\t\tthis.eventBus.emit(ReactorEventTypes.JOB_FAILED, {\n\t\t\t\tjobId: job.id,\n\t\t\t\terror: new DocumentNotFoundError(job.documentId),\n\t\t\t\tjob\n\t\t\t}).catch(() => {});\n\t\t}\n\t\tthis.deferredJobs.clear();\n\t\tthis.executors = [];\n\t\tthis.isRunning = false;\n\t}\n\tgetExecutors() {\n\t\treturn [...this.executors];\n\t}\n\tgetStatus() {\n\t\treturn {\n\t\t\tisRunning: this.isRunning,\n\t\t\tnumExecutors: this.executors.length,\n\t\t\tactiveJobs: this.activeJobs,\n\t\t\ttotalJobsProcessed: this.totalJobsProcessed\n\t\t};\n\t}\n\tasync processNextJob() {\n\t\tlet handle;\n\t\ttry {\n\t\t\thandle = await this.queue.dequeueNext();\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Error dequeueing next job: @Error\", error);\n\t\t\treturn;\n\t\t}\n\t\tif (!handle) return;\n\t\thandle.start();\n\t\tthis.activeJobs++;\n\t\tthis.jobTracker.markRunning(handle.job.id);\n\t\tconst runningEvent = {\n\t\t\tjobId: handle.job.id,\n\t\t\tjobMeta: handle.job.meta\n\t\t};\n\t\tthis.eventBus.emit(ReactorEventTypes.JOB_RUNNING, runningEvent).catch(() => {});\n\t\tconst executorIndex = this.totalJobsProcessed % this.executors.length;\n\t\tconst executor = this.executors[executorIndex];\n\t\tconst workerId = `in-process-${executorIndex}`;\n\t\tconst startedEvent = {\n\t\t\tjob: handle.job,\n\t\t\tstartedAt: (/* @__PURE__ */ new Date()).toISOString(),\n\t\t\tworkerId\n\t\t};\n\t\tthis.eventBus.emit(JobExecutorEventTypes.JOB_STARTED, startedEvent).catch(() => {});\n\t\tconst signal = AbortSignal.timeout(this.jobTimeoutMs);\n\t\tconst toError = (reason) => reason instanceof Error ? reason : new Error(String(reason));\n\t\tconst abortPromise = new Promise((_, reject) => {\n\t\t\tif (signal.aborted) {\n\t\t\t\treject(toError(signal.reason));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tsignal.addEventListener(\"abort\", () => reject(toError(signal.reason)), { once: true });\n\t\t});\n\t\tlet result;\n\t\ttry {\n\t\t\tresult = await Promise.race([executor.executeJob(handle.job, signal), abortPromise]);\n\t\t} catch (error) {\n\t\t\tconst errorInfo = toErrorInfo(error instanceof Error ? error : String(error));\n\t\t\thandle.fail(errorInfo);\n\t\t\tthis.activeJobs--;\n\t\t\tthis.jobTracker.markFailed(handle.job.id, errorInfo, handle.job);\n\t\t\tthis.eventBus.emit(ReactorEventTypes.JOB_FAILED, {\n\t\t\t\tjobId: handle.job.id,\n\t\t\t\terror: new Error(errorInfo.message),\n\t\t\t\tjob: handle.job\n\t\t\t}).catch(() => {});\n\t\t\tconst failedEvent = {\n\t\t\t\tjob: handle.job,\n\t\t\t\terror: errorInfo.message,\n\t\t\t\twillRetry: false,\n\t\t\t\tretryCount: 0,\n\t\t\t\tworkerId\n\t\t\t};\n\t\t\tthis.eventBus.emit(JobExecutorEventTypes.JOB_FAILED, failedEvent).catch(() => {});\n\t\t\tawait this.checkForMoreJobs();\n\t\t\treturn;\n\t\t}\n\t\tif (result.success) this.totalJobsProcessed++;\n\t\tif (result.success) {\n\t\t\tconst completedEvent = {\n\t\t\t\tjob: handle.job,\n\t\t\t\tresult,\n\t\t\t\tworkerId\n\t\t\t};\n\t\t\tthis.eventBus.emit(JobExecutorEventTypes.JOB_COMPLETED, completedEvent).catch(() => {});\n\t\t} else {\n\t\t\tconst failedEvent = {\n\t\t\t\tjob: handle.job,\n\t\t\t\terror: result.error?.message ?? \"unknown\",\n\t\t\t\twillRetry: false,\n\t\t\t\tretryCount: 0,\n\t\t\t\tworkerId\n\t\t\t};\n\t\t\tthis.eventBus.emit(JobExecutorEventTypes.JOB_FAILED, failedEvent).catch(() => {});\n\t\t}\n\t\tawait this.resultHandler.handleResult(handle, result, {\n\t\t\tdeferJob: (documentId, job) => {\n\t\t\t\tconst existing = this.deferredJobs.get(documentId) ?? [];\n\t\t\t\texisting.push(job);\n\t\t\t\tthis.deferredJobs.set(documentId, existing);\n\t\t\t},\n\t\t\tflushDeferredFor: (documentId) => this.flushDeferredJobs(documentId)\n\t\t});\n\t\tthis.activeJobs--;\n\t\tawait this.checkForMoreJobs();\n\t}\n\tasync checkForMoreJobs() {\n\t\tif (!this.isRunning) return;\n\t\tlet hasMore;\n\t\ttry {\n\t\t\thasMore = await this.queue.hasJobs();\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Error checking for more jobs: @Error\", error);\n\t\t\treturn;\n\t\t}\n\t\tif (hasMore) await this.processNextJob();\n\t}\n\tasync processExistingJobs() {\n\t\tlet hasJobs;\n\t\ttry {\n\t\t\thasJobs = await this.queue.hasJobs();\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Error checking for existing jobs: @Error\", error);\n\t\t\treturn;\n\t\t}\n\t\tif (hasJobs) {\n\t\t\tconst promises = [];\n\t\t\tfor (let i = 0; i < Math.min(this.executors.length, 5); i++) promises.push(this.processNextJob());\n\t\t\ttry {\n\t\t\t\tawait Promise.all(promises);\n\t\t\t} catch (error) {\n\t\t\t\tthis.logger.error(\"Error processing existing jobs: @Error\", error);\n\t\t\t}\n\t\t}\n\t}\n\tasync flushDeferredJobs(documentId) {\n\t\tconst jobs = this.deferredJobs.get(documentId);\n\t\tif (!jobs || jobs.length === 0) return;\n\t\tthis.deferredJobs.delete(documentId);\n\t\tfor (const job of jobs) try {\n\t\t\tawait this.queue.enqueue(job);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Error re-enqueuing deferred job: @Error\", error);\n\t\t}\n\t}\n};\n//#endregion\n//#region src/job-tracker/in-memory-job-tracker.ts\n/**\n* In-memory implementation of IJobTracker.\n* Maintains job status in a Map for synchronous access.\n* Subscribes to operation events to update job states.\n*/\nvar InMemoryJobTracker = class {\n\tjobs = /* @__PURE__ */ new Map();\n\tunsubscribers = [];\n\tconstructor(eventBus) {\n\t\tthis.eventBus = eventBus;\n\t\tthis.subscribeToEvents();\n\t}\n\tsubscribeToEvents() {\n\t\tthis.unsubscribers.push(this.eventBus.subscribe(ReactorEventTypes.JOB_WRITE_READY, (_type, event) => {\n\t\t\tthis.handleWriteReady(event);\n\t\t}));\n\t\tthis.unsubscribers.push(this.eventBus.subscribe(ReactorEventTypes.JOB_READ_READY, (_type, event) => {\n\t\t\tthis.handleReadReady(event);\n\t\t}));\n\t\tthis.unsubscribers.push(this.eventBus.subscribe(ReactorEventTypes.JOB_FAILED, (_type, event) => {\n\t\t\tthis.handleJobFailed(event);\n\t\t}));\n\t}\n\thandleWriteReady(event) {\n\t\tconst jobId = event.jobId;\n\t\tconst job = this.jobs.get(jobId);\n\t\tif (job && job.status === JobStatus.RUNNING) {\n\t\t\tconst consistencyToken = createConsistencyToken(event.operations);\n\t\t\tthis.jobs.set(jobId, {\n\t\t\t\t...job,\n\t\t\t\tstatus: JobStatus.WRITE_READY,\n\t\t\t\tconsistencyToken\n\t\t\t});\n\t\t}\n\t}\n\thandleReadReady(event) {\n\t\tconst jobId = event.jobId;\n\t\tconst job = this.jobs.get(jobId);\n\t\tif (job && job.status === JobStatus.WRITE_READY) this.jobs.set(jobId, {\n\t\t\t...job,\n\t\t\tstatus: JobStatus.READ_READY\n\t\t});\n\t}\n\thandleJobFailed(event) {\n\t\tthis.markFailed(event.jobId, {\n\t\t\tmessage: event.error.message,\n\t\t\tstack: event.error.stack || \"\"\n\t\t}, event.job);\n\t}\n\tshutdown() {\n\t\tfor (const unsubscribe of this.unsubscribers) unsubscribe();\n\t\tthis.unsubscribers = [];\n\t}\n\tregisterJob(jobInfo) {\n\t\tthis.jobs.set(jobInfo.id, { ...jobInfo });\n\t}\n\tmarkRunning(jobId) {\n\t\tconst job = this.jobs.get(jobId);\n\t\tif (!job) {\n\t\t\tthis.jobs.set(jobId, {\n\t\t\t\tid: jobId,\n\t\t\t\tstatus: JobStatus.RUNNING,\n\t\t\t\tcreatedAtUtcIso: (/* @__PURE__ */ new Date()).toISOString(),\n\t\t\t\tconsistencyToken: createEmptyConsistencyToken(),\n\t\t\t\tmeta: {\n\t\t\t\t\tbatchId: jobId,\n\t\t\t\t\tbatchJobIds: [jobId]\n\t\t\t\t}\n\t\t\t});\n\t\t\treturn;\n\t\t}\n\t\tthis.jobs.set(jobId, {\n\t\t\t...job,\n\t\t\tstatus: JobStatus.RUNNING\n\t\t});\n\t}\n\tmarkFailed(jobId, error, job) {\n\t\tconst existing = this.jobs.get(jobId);\n\t\tif (!existing) {\n\t\t\tthis.jobs.set(jobId, {\n\t\t\t\tid: jobId,\n\t\t\t\tstatus: JobStatus.FAILED,\n\t\t\t\tcreatedAtUtcIso: (/* @__PURE__ */ new Date()).toISOString(),\n\t\t\t\tcompletedAtUtcIso: (/* @__PURE__ */ new Date()).toISOString(),\n\t\t\t\terror,\n\t\t\t\tjob,\n\t\t\t\tconsistencyToken: createEmptyConsistencyToken(),\n\t\t\t\tmeta: {\n\t\t\t\t\tbatchId: jobId,\n\t\t\t\t\tbatchJobIds: [jobId]\n\t\t\t\t}\n\t\t\t});\n\t\t\treturn;\n\t\t}\n\t\tthis.jobs.set(jobId, {\n\t\t\t...existing,\n\t\t\tstatus: JobStatus.FAILED,\n\t\t\tcompletedAtUtcIso: (/* @__PURE__ */ new Date()).toISOString(),\n\t\t\terror,\n\t\t\tjob,\n\t\t\tconsistencyToken: createEmptyConsistencyToken()\n\t\t});\n\t}\n\tgetJobStatus(jobId) {\n\t\tconst job = this.jobs.get(jobId);\n\t\treturn job ? { ...job } : null;\n\t}\n};\n//#endregion\n//#region src/processors/utils.ts\nfunction isDriveDeletion(op) {\n\treturn op.operation.action.type === \"DELETE_DOCUMENT\";\n}\nfunction extractDriveHeader(op) {\n\tif (!op.context.resultingState) return void 0;\n\treturn JSON.parse(op.context.resultingState).header;\n}\nfunction extractDeletedDocumentId(op) {\n\treturn op.operation.action.input.documentId ?? op.context.documentId;\n}\nfunction createMinimalDriveHeader(driveId, documentType) {\n\treturn {\n\t\tid: driveId,\n\t\tdocumentType,\n\t\tsig: {\n\t\t\tpublicKey: {},\n\t\t\tnonce: \"\"\n\t\t},\n\t\tslug: \"\",\n\t\tname: \"\",\n\t\tbranch: \"main\",\n\t\trevision: {},\n\t\tcreatedAtUtcIso: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tlastModifiedAtUtcIso: (/* @__PURE__ */ new Date()).toISOString()\n\t};\n}\nfunction matchesFilter(op, filter) {\n\tif (filter.documentType && filter.documentType.length > 0) {\n\t\tif (!filter.documentType.includes(op.context.documentType)) return false;\n\t}\n\tif (filter.scope && filter.scope.length > 0) {\n\t\tif (!filter.scope.includes(op.context.scope)) return false;\n\t}\n\tif (filter.branch && filter.branch.length > 0) {\n\t\tif (!filter.branch.includes(op.context.branch)) return false;\n\t}\n\tif (filter.documentId && filter.documentId.length > 0) {\n\t\tif (!filter.documentId.includes(\"*\") && !filter.documentId.includes(op.context.documentId)) return false;\n\t}\n\treturn true;\n}\n//#endregion\n//#region src/processors/processor-manager.ts\n/**\n* Manages processor lifecycle based on operations.\n* Extends BaseReadModel to receive operations from ReadModelCoordinator.\n*\n* Responsibilities:\n* 1. Detect drive creation from CREATE_DOCUMENT operations\n* 2. Create processors for each drive using registered factories\n* 3. Route operations to matching processors based on filters\n* 4. Clean up processors when drives are deleted or factories are unregistered\n* 5. Track per-processor cursors for failure recovery and backfill\n*/\nvar ProcessorManager = class extends BaseReadModel {\n\tfactoryRegistry = /* @__PURE__ */ new Map();\n\tprocessorsByDrive = /* @__PURE__ */ new Map();\n\tfactoryToProcessors = /* @__PURE__ */ new Map();\n\tknownDrives = /* @__PURE__ */ new Map();\n\tcursorCache = /* @__PURE__ */ new Map();\n\tlogger;\n\tdriveContainerTypes;\n\tconstructor(db, operationIndex, writeCache, consistencyTracker, logger, driveContainerTypes) {\n\t\tsuper(db, operationIndex, writeCache, consistencyTracker, {\n\t\t\treadModelId: \"processor-manager\",\n\t\t\trebuildStateOnInit: true\n\t\t});\n\t\tthis.logger = logger;\n\t\tthis.driveContainerTypes = driveContainerTypes;\n\t}\n\tasync init() {\n\t\tawait super.init();\n\t\tawait this.loadAllCursors();\n\t\tawait this.discoverExistingDrives();\n\t}\n\tasync commitOperations(items) {\n\t\tawait this.detectAndRegisterNewDrives(items);\n\t\tawait this.detectAndCleanupDeletedDrives(items);\n\t\tawait this.routeOperationsToProcessors(items);\n\t}\n\tasync registerFactory(identifier, factory) {\n\t\tif (this.factoryRegistry.has(identifier)) await this.unregisterFactory(identifier);\n\t\tthis.factoryRegistry.set(identifier, factory);\n\t\tthis.factoryToProcessors.set(identifier, /* @__PURE__ */ new Map());\n\t\tfor (const [driveId, documentType] of this.knownDrives) {\n\t\t\tconst driveHeader = createMinimalDriveHeader(driveId, documentType);\n\t\t\tawait this.createProcessorsForDrive(driveId, identifier, factory, driveHeader);\n\t\t}\n\t}\n\tasync unregisterFactory(identifier) {\n\t\tconst factoryProcessors = this.factoryToProcessors.get(identifier);\n\t\tif (!factoryProcessors) return;\n\t\tfor (const [driveId, tracked] of factoryProcessors) {\n\t\t\tfor (const t of tracked) await this.safeDisconnect(t.record.processor);\n\t\t\tconst driveProcessors = this.processorsByDrive.get(driveId);\n\t\t\tif (driveProcessors) {\n\t\t\t\tconst remaining = driveProcessors.filter((p) => !tracked.includes(p));\n\t\t\t\tif (remaining.length > 0) this.processorsByDrive.set(driveId, remaining);\n\t\t\t\telse this.processorsByDrive.delete(driveId);\n\t\t\t}\n\t\t}\n\t\tawait this.deleteProcessorCursors({ factoryId: identifier });\n\t\tthis.factoryToProcessors.delete(identifier);\n\t\tthis.factoryRegistry.delete(identifier);\n\t}\n\tget(processorId) {\n\t\tfor (const tracked of this.allTrackedProcessors()) if (tracked.processorId === processorId) return tracked;\n\t}\n\tgetAll() {\n\t\treturn Array.from(this.allTrackedProcessors());\n\t}\n\t*allTrackedProcessors() {\n\t\tfor (const tracked of this.processorsByDrive.values()) yield* tracked;\n\t}\n\tasync detectAndRegisterNewDrives(operations) {\n\t\tfor (const op of operations) {\n\t\t\tif (!this.isDriveCreation(op)) continue;\n\t\t\tconst driveId = op.context.documentId;\n\t\t\tif (this.knownDrives.has(driveId)) continue;\n\t\t\tthis.knownDrives.set(driveId, op.context.documentType);\n\t\t\tconst driveHeader = extractDriveHeader(op);\n\t\t\tif (!driveHeader) continue;\n\t\t\tfor (const [identifier, factory] of this.factoryRegistry) await this.createProcessorsForDrive(driveId, identifier, factory, driveHeader);\n\t\t}\n\t}\n\tisDriveCreation(op) {\n\t\treturn op.operation.action.type === \"CREATE_DOCUMENT\" && this.driveContainerTypes.has(op.context.documentType);\n\t}\n\tasync detectAndCleanupDeletedDrives(operations) {\n\t\tfor (const op of operations) {\n\t\t\tif (!isDriveDeletion(op)) continue;\n\t\t\tconst driveId = extractDeletedDocumentId(op);\n\t\t\tif (!driveId || !this.knownDrives.has(driveId)) continue;\n\t\t\tif (!this.isDeletedDocumentADrive(driveId)) continue;\n\t\t\tawait this.cleanupDriveProcessors(driveId);\n\t\t\tthis.knownDrives.delete(driveId);\n\t\t}\n\t}\n\tasync discoverExistingDrives() {\n\t\tconst drives = await this.db.selectFrom(\"DocumentSnapshot\").select([\"documentId\", \"documentType\"]).where(\"documentType\", \"in\", [...this.driveContainerTypes]).where(\"isDeleted\", \"=\", false).execute();\n\t\tfor (const drive of drives) this.knownDrives.set(drive.documentId, drive.documentType);\n\t}\n\tisDeletedDocumentADrive(documentId) {\n\t\treturn this.knownDrives.has(documentId);\n\t}\n\tasync createProcessorsForDrive(driveId, identifier, factory, driveHeader) {\n\t\tlet records;\n\t\ttry {\n\t\t\trecords = await factory(driveHeader);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Factory '@FactoryId' failed for drive '@DriveId': @Error\", identifier, driveId, error);\n\t\t\treturn;\n\t\t}\n\t\tif (records.length === 0) return;\n\t\tconst trackedList = [];\n\t\tfor (let i = 0; i < records.length; i++) {\n\t\t\tconst record = records[i];\n\t\t\tconst processorId = `${identifier}:${driveId}:${i}`;\n\t\t\tconst cached = this.cursorCache.get(processorId);\n\t\t\tlet lastOrdinal;\n\t\t\tlet status;\n\t\t\tlet lastError;\n\t\t\tlet lastErrorTimestamp;\n\t\t\tif (cached) {\n\t\t\t\tlastOrdinal = cached.lastOrdinal;\n\t\t\t\tstatus = cached.status;\n\t\t\t\tlastError = cached.lastError ?? void 0;\n\t\t\t\tlastErrorTimestamp = cached.lastErrorTimestamp ?? void 0;\n\t\t\t} else {\n\t\t\t\tlastOrdinal = (record.startFrom ?? \"beginning\") === \"current\" ? this.lastOrdinal : 0;\n\t\t\t\tstatus = \"active\";\n\t\t\t\tlastError = void 0;\n\t\t\t\tlastErrorTimestamp = void 0;\n\t\t\t}\n\t\t\tconst tracked = {\n\t\t\t\tprocessorId,\n\t\t\t\tfactoryId: identifier,\n\t\t\t\tdriveId,\n\t\t\t\tprocessorIndex: i,\n\t\t\t\trecord,\n\t\t\t\tlastOrdinal,\n\t\t\t\tstatus,\n\t\t\t\tlastError,\n\t\t\t\tlastErrorTimestamp,\n\t\t\t\tretry: () => this.retryProcessor(tracked)\n\t\t\t};\n\t\t\ttrackedList.push(tracked);\n\t\t\tawait this.saveProcessorCursor(tracked);\n\t\t}\n\t\tawait this.db.deleteFrom(\"ProcessorCursor\").where(\"factoryId\", \"=\", identifier).where(\"driveId\", \"=\", driveId).where(\"processorIndex\", \">=\", records.length).execute();\n\t\tfor (const [id, row] of this.cursorCache) if (row.factoryId === identifier && row.driveId === driveId && row.processorIndex >= records.length) this.cursorCache.delete(id);\n\t\tconst factoryProcessors = this.factoryToProcessors.get(identifier);\n\t\tif (factoryProcessors) factoryProcessors.set(driveId, trackedList);\n\t\tconst existingDriveProcessors = this.processorsByDrive.get(driveId) ?? [];\n\t\tthis.processorsByDrive.set(driveId, [...existingDriveProcessors, ...trackedList]);\n\t\tfor (const tracked of trackedList) if (tracked.status === \"active\" && tracked.lastOrdinal < this.lastOrdinal) await this.backfillProcessor(tracked);\n\t}\n\tasync backfillProcessor(tracked) {\n\t\tlet page = await this.operationIndex.getSinceOrdinal(tracked.lastOrdinal);\n\t\twhile (page.results.length > 0) {\n\t\t\tconst matching = page.results.filter((op) => matchesFilter(op, tracked.record.filter));\n\t\t\tif (matching.length > 0) try {\n\t\t\t\tawait tracked.record.processor.onOperations(matching);\n\t\t\t} catch (error) {\n\t\t\t\ttracked.status = \"errored\";\n\t\t\t\ttracked.lastError = error instanceof Error ? error.message : String(error);\n\t\t\t\ttracked.lastErrorTimestamp = /* @__PURE__ */ new Date();\n\t\t\t\tawait this.safeSaveProcessorCursor(tracked);\n\t\t\t\tthis.logger.error(\"Processor '@ProcessorId' failed during backfill at ordinal @Ordinal: @Error\", tracked.processorId, tracked.lastOrdinal, error);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\ttracked.lastOrdinal = Math.max(...page.results.map((op) => op.context.ordinal));\n\t\t\tawait this.safeSaveProcessorCursor(tracked);\n\t\t\tif (!page.next) break;\n\t\t\tpage = await page.next();\n\t\t}\n\t}\n\tasync retryProcessor(tracked) {\n\t\tif (tracked.status !== \"errored\") return;\n\t\ttracked.status = \"active\";\n\t\ttracked.lastError = void 0;\n\t\ttracked.lastErrorTimestamp = void 0;\n\t\tawait this.saveProcessorCursor(tracked);\n\t\tawait this.backfillProcessor(tracked);\n\t}\n\tasync cleanupDriveProcessors(driveId) {\n\t\tconst processors = this.processorsByDrive.get(driveId);\n\t\tif (!processors) return;\n\t\tfor (const tracked of processors) await this.safeDisconnect(tracked.record.processor);\n\t\tthis.processorsByDrive.delete(driveId);\n\t\tfor (const factoryProcessors of this.factoryToProcessors.values()) factoryProcessors.delete(driveId);\n\t\tawait this.deleteProcessorCursors({ driveId });\n\t}\n\tasync safeDisconnect(processor) {\n\t\ttry {\n\t\t\tawait processor.onDisconnect();\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Error disconnecting processor: @Error\", error);\n\t\t}\n\t}\n\tasync routeOperationsToProcessors(operations) {\n\t\tconst maxOrdinal = Math.max(...operations.map((op) => op.context.ordinal));\n\t\tconst allTracked = Array.from(this.allTrackedProcessors());\n\t\tawait Promise.all(allTracked.map(async (tracked) => {\n\t\t\tif (tracked.status !== \"active\") return;\n\t\t\tconst matching = operations.filter((op) => op.context.ordinal > tracked.lastOrdinal).filter((op) => matchesFilter(op, tracked.record.filter));\n\t\t\tif (matching.length > 0) try {\n\t\t\t\tawait tracked.record.processor.onOperations(matching);\n\t\t\t} catch (error) {\n\t\t\t\ttracked.status = \"errored\";\n\t\t\t\ttracked.lastError = error instanceof Error ? error.message : String(error);\n\t\t\t\ttracked.lastErrorTimestamp = /* @__PURE__ */ new Date();\n\t\t\t\tawait this.safeSaveProcessorCursor(tracked);\n\t\t\t\tthis.logger.error(\"Processor '@ProcessorId' failed at ordinal @Ordinal: @Error\", tracked.processorId, tracked.lastOrdinal, error);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\ttracked.lastOrdinal = maxOrdinal;\n\t\t\tawait this.safeSaveProcessorCursor(tracked);\n\t\t}));\n\t}\n\tasync loadAllCursors() {\n\t\tconst rows = await this.db.selectFrom(\"ProcessorCursor\").selectAll().execute();\n\t\tfor (const row of rows) this.cursorCache.set(row.processorId, row);\n\t}\n\tasync safeSaveProcessorCursor(tracked) {\n\t\ttry {\n\t\t\tawait this.saveProcessorCursor(tracked);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Failed to persist cursor for '@ProcessorId': @Error\", tracked.processorId, error);\n\t\t}\n\t}\n\tasync saveProcessorCursor(tracked) {\n\t\tawait this.db.insertInto(\"ProcessorCursor\").values({\n\t\t\tprocessorId: tracked.processorId,\n\t\t\tfactoryId: tracked.factoryId,\n\t\t\tdriveId: tracked.driveId,\n\t\t\tprocessorIndex: tracked.processorIndex,\n\t\t\tlastOrdinal: tracked.lastOrdinal,\n\t\t\tstatus: tracked.status,\n\t\t\tlastError: tracked.lastError ?? null,\n\t\t\tlastErrorTimestamp: tracked.lastErrorTimestamp ?? null,\n\t\t\tupdatedAt: /* @__PURE__ */ new Date()\n\t\t}).onConflict((oc) => oc.column(\"processorId\").doUpdateSet({\n\t\t\tlastOrdinal: tracked.lastOrdinal,\n\t\t\tstatus: tracked.status,\n\t\t\tlastError: tracked.lastError ?? null,\n\t\t\tlastErrorTimestamp: tracked.lastErrorTimestamp ?? null,\n\t\t\tupdatedAt: /* @__PURE__ */ new Date()\n\t\t})).execute();\n\t\tthis.cursorCache.set(tracked.processorId, {\n\t\t\tprocessorId: tracked.processorId,\n\t\t\tfactoryId: tracked.factoryId,\n\t\t\tdriveId: tracked.driveId,\n\t\t\tprocessorIndex: tracked.processorIndex,\n\t\t\tlastOrdinal: tracked.lastOrdinal,\n\t\t\tstatus: tracked.status,\n\t\t\tlastError: tracked.lastError ?? null,\n\t\t\tlastErrorTimestamp: tracked.lastErrorTimestamp ?? null,\n\t\t\tcreatedAt: /* @__PURE__ */ new Date(),\n\t\t\tupdatedAt: /* @__PURE__ */ new Date()\n\t\t});\n\t}\n\tasync deleteProcessorCursors(filter) {\n\t\tif (\"factoryId\" in filter) {\n\t\t\tawait this.db.deleteFrom(\"ProcessorCursor\").where(\"factoryId\", \"=\", filter.factoryId).execute();\n\t\t\tfor (const [id, row] of this.cursorCache) if (row.factoryId === filter.factoryId) this.cursorCache.delete(id);\n\t\t} else {\n\t\t\tawait this.db.deleteFrom(\"ProcessorCursor\").where(\"driveId\", \"=\", filter.driveId).execute();\n\t\t\tfor (const [id, row] of this.cursorCache) if (row.driveId === filter.driveId) this.cursorCache.delete(id);\n\t\t}\n\t}\n};\n//#endregion\n//#region src/queue/job-execution-handle.ts\n/**\n* Implementation of the IJobExecutionHandle interface\n*/\nvar JobExecutionHandle = class {\n\t_state;\n\t_job;\n\tonStart;\n\tonComplete;\n\tonFail;\n\tonDefer;\n\tgetStateName(state) {\n\t\tswitch (state) {\n\t\t\tcase JobQueueState.PREPROCESSING: return \"PREPROCESSING\";\n\t\t\tcase JobQueueState.PENDING: return \"PENDING\";\n\t\t\tcase JobQueueState.READY: return \"READY\";\n\t\t\tcase JobQueueState.RUNNING: return \"RUNNING\";\n\t\t\tcase JobQueueState.RESOLVED: return \"RESOLVED\";\n\t\t\tdefault: return `UNKNOWN`;\n\t\t}\n\t}\n\tconstructor(job, initialState, callbacks) {\n\t\tthis._job = job;\n\t\tthis._state = initialState;\n\t\tthis.onStart = callbacks?.onStart;\n\t\tthis.onComplete = callbacks?.onComplete;\n\t\tthis.onFail = callbacks?.onFail;\n\t\tthis.onDefer = callbacks?.onDefer;\n\t}\n\tget job() {\n\t\treturn this._job;\n\t}\n\tget state() {\n\t\treturn this._state;\n\t}\n\tstart() {\n\t\tif (this._state !== JobQueueState.READY) throw new Error(`Cannot start job in state ${this.getStateName(this._state)}`);\n\t\tthis._state = JobQueueState.RUNNING;\n\t\tthis.onStart?.();\n\t}\n\tcomplete() {\n\t\tif (this._state !== JobQueueState.RUNNING) throw new Error(`Cannot complete job in state ${this.getStateName(this._state)}`);\n\t\tthis._state = JobQueueState.RESOLVED;\n\t\tthis.onComplete?.();\n\t}\n\tfail(error) {\n\t\tif (this._state !== JobQueueState.RUNNING) throw new Error(`Cannot fail job in state ${this.getStateName(this._state)}`);\n\t\tthis._state = JobQueueState.RESOLVED;\n\t\tthis.onFail?.(error);\n\t}\n\tdefer() {\n\t\tif (this._state !== JobQueueState.RUNNING) throw new Error(`Cannot defer job in state ${this.getStateName(this._state)}`);\n\t\tthis._state = JobQueueState.RESOLVED;\n\t\tthis.onDefer?.();\n\t}\n};\n//#endregion\n//#region src/queue/queue.ts\n/**\n* In-memory implementation of the IQueue interface.\n* Organizes jobs by documentId, scope, and branch to ensure proper ordering.\n* Ensures serial execution per document by tracking executing jobs.\n* Implements dependency management through queue hints.\n*/\nvar InMemoryQueue = class {\n\tqueues = /* @__PURE__ */ new Map();\n\tjobIdToQueueKey = /* @__PURE__ */ new Map();\n\tdocIdToJobId = /* @__PURE__ */ new Map();\n\tjobIdToDocId = /* @__PURE__ */ new Map();\n\tcompletedJobs = /* @__PURE__ */ new Set();\n\tjobIndex = /* @__PURE__ */ new Map();\n\tisBlocked = false;\n\tonDrainedCallback;\n\tisPausedFlag = false;\n\tconstructor(eventBus, resolver) {\n\t\tthis.eventBus = eventBus;\n\t\tthis.resolver = resolver;\n\t}\n\ttoErrorInfo(error) {\n\t\tif (error instanceof Error) return {\n\t\t\tmessage: error.message,\n\t\t\tstack: error.stack || (/* @__PURE__ */ new Error()).stack || \"\"\n\t\t};\n\t\treturn {\n\t\t\tmessage: error,\n\t\t\tstack: (/* @__PURE__ */ new Error()).stack || \"\"\n\t\t};\n\t}\n\t/**\n\t* Creates a unique key for a document/scope/branch combination\n\t*/\n\tcreateQueueKey(documentId, scope, branch) {\n\t\treturn `${documentId}:${scope}:${branch}`;\n\t}\n\t/**\n\t* Gets or creates a queue for the given key\n\t*/\n\tgetQueue(queueKey) {\n\t\tlet queue = this.queues.get(queueKey);\n\t\tif (!queue) {\n\t\t\tqueue = [];\n\t\t\tthis.queues.set(queueKey, queue);\n\t\t}\n\t\treturn queue;\n\t}\n\t/**\n\t* Check if a document has any jobs currently executing\n\t*/\n\tisDocumentExecuting(documentId) {\n\t\tconst executingSet = this.docIdToJobId.get(documentId);\n\t\treturn executingSet ? executingSet.size > 0 : false;\n\t}\n\t/**\n\t* Mark a job as executing for its document\n\t*/\n\tmarkJobExecuting(job) {\n\t\tlet executingSet = this.docIdToJobId.get(job.documentId);\n\t\tif (!executingSet) {\n\t\t\texecutingSet = /* @__PURE__ */ new Set();\n\t\t\tthis.docIdToJobId.set(job.documentId, executingSet);\n\t\t}\n\t\texecutingSet.add(job.id);\n\t\tthis.jobIdToDocId.set(job.id, job.documentId);\n\t}\n\t/**\n\t* Mark a job as no longer executing for its document\n\t*/\n\tmarkJobComplete(jobId, documentId) {\n\t\tconst executingSet = this.docIdToJobId.get(documentId);\n\t\tif (executingSet) {\n\t\t\texecutingSet.delete(jobId);\n\t\t\tif (executingSet.size === 0) this.docIdToJobId.delete(documentId);\n\t\t}\n\t\tthis.jobIdToDocId.delete(jobId);\n\t}\n\t/**\n\t* Check if all dependencies for a job have been completed\n\t*/\n\tareDependenciesMet(job) {\n\t\tif (job.queueHint.length === 0) return true;\n\t\treturn job.queueHint.every((depId) => this.completedJobs.has(depId));\n\t}\n\t/**\n\t* Returns the head of the sub-queue if its dependencies are met, or null.\n\t*\n\t* The dispatcher only ever considers the head — a dep-blocked head holds\n\t* the rest of its sub-queue. This preserves per-(documentId, scope, branch)\n\t* FIFO regardless of how dependencies are authored, and makes the queue's\n\t* documented \"serialized per document\" invariant hold even when callers\n\t* omit queueHint dependencies on jobs that share a sub-queue.\n\t*/\n\tgetNextJobWithMetDependencies(queue) {\n\t\tif (queue.length === 0) return null;\n\t\tconst head = queue[0];\n\t\treturn this.areDependenciesMet(head) ? head : null;\n\t}\n\tgetCreateDocumentType(job) {\n\t\tfor (const action of job.actions) if (action.type === \"CREATE_DOCUMENT\") return action.input.model;\n\t\tfor (const operation of job.operations) if (operation.action.type === \"CREATE_DOCUMENT\") return operation.action.input.model;\n\t}\n\tasync enqueue(job) {\n\t\tif (this.isBlocked) throw new Error(\"Queue is blocked\");\n\t\tconst queueKey = this.createQueueKey(job.documentId, job.scope, job.branch);\n\t\tthis.getQueue(queueKey).push(job);\n\t\tthis.jobIdToQueueKey.set(job.id, queueKey);\n\t\tthis.jobIndex.set(job.id, job);\n\t\tconst documentType = this.getCreateDocumentType(job);\n\t\tif (documentType) try {\n\t\t\tawait this.resolver.ensureModelLoaded(documentType);\n\t\t} catch {\n\t\t\tawait this.failJob(job.id, {\n\t\t\t\tmessage: `Failed to load document model for type: ${documentType}`,\n\t\t\t\tstack: (/* @__PURE__ */ new Error()).stack || \"\"\n\t\t\t});\n\t\t\treturn;\n\t\t}\n\t\tconst eventData = {\n\t\t\tdocumentId: job.documentId,\n\t\t\tscope: job.scope,\n\t\t\tbranch: job.branch,\n\t\t\tjobId: job.id\n\t\t};\n\t\tawait this.eventBus.emit(QueueEventTypes.JOB_AVAILABLE, eventData);\n\t}\n\tdequeue(documentId, scope, branch, signal) {\n\t\tconst queueKey = this.createQueueKey(documentId, scope, branch);\n\t\tconst queue = this.queues.get(queueKey);\n\t\tif (signal?.aborted) return Promise.reject(/* @__PURE__ */ new Error(\"Operation aborted\"));\n\t\tif (!queue || queue.length === 0) return Promise.resolve(null);\n\t\tconst job = this.getNextJobWithMetDependencies(queue);\n\t\tif (!job) return Promise.resolve(null);\n\t\tconst jobIndex = queue.indexOf(job);\n\t\tqueue.splice(jobIndex, 1);\n\t\tthis.jobIdToQueueKey.delete(job.id);\n\t\tthis.markJobExecuting(job);\n\t\tif (queue.length === 0) this.queues.delete(queueKey);\n\t\tconst handle = new JobExecutionHandle(job, JobQueueState.READY, {\n\t\t\tonStart: () => {},\n\t\t\tonComplete: () => {\n\t\t\t\tthis.completeJob(job.id);\n\t\t\t},\n\t\t\tonFail: (error) => {\n\t\t\t\tthis.failJob(job.id, error);\n\t\t\t},\n\t\t\tonDefer: () => {\n\t\t\t\tthis.deferJob(job.id);\n\t\t\t}\n\t\t});\n\t\treturn Promise.resolve(handle);\n\t}\n\tdequeueNext(signal) {\n\t\tif (signal?.aborted) return Promise.reject(/* @__PURE__ */ new Error(\"Operation aborted\"));\n\t\tif (this.isPausedFlag) return Promise.resolve(null);\n\t\tfor (const [queueKey, queue] of this.queues.entries()) if (queue.length > 0) {\n\t\t\tconst job = this.getNextJobWithMetDependencies(queue);\n\t\t\tif (!job) continue;\n\t\t\tif (!this.isDocumentExecuting(job.documentId)) {\n\t\t\t\tconst jobIdx = queue.indexOf(job);\n\t\t\t\tqueue.splice(jobIdx, 1);\n\t\t\t\tthis.jobIdToQueueKey.delete(job.id);\n\t\t\t\tthis.markJobExecuting(job);\n\t\t\t\tif (queue.length === 0) this.queues.delete(queueKey);\n\t\t\t\tconst handle = new JobExecutionHandle(job, JobQueueState.READY, {\n\t\t\t\t\tonStart: () => {},\n\t\t\t\t\tonComplete: () => {\n\t\t\t\t\t\tthis.completeJob(job.id);\n\t\t\t\t\t},\n\t\t\t\t\tonFail: (error) => {\n\t\t\t\t\t\tthis.failJob(job.id, error);\n\t\t\t\t\t},\n\t\t\t\t\tonDefer: () => {\n\t\t\t\t\t\tthis.deferJob(job.id);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\treturn Promise.resolve(handle);\n\t\t\t}\n\t\t}\n\t\treturn Promise.resolve(null);\n\t}\n\tdequeueNextMatching(predicate, signal) {\n\t\tif (signal?.aborted) return Promise.reject(/* @__PURE__ */ new Error(\"Operation aborted\"));\n\t\tif (this.isPausedFlag) return Promise.resolve(null);\n\t\tfor (const [queueKey, queue] of this.queues.entries()) if (queue.length > 0) {\n\t\t\tconst job = this.getNextJobWithMetDependencies(queue);\n\t\t\tif (!job) continue;\n\t\t\tif (this.isDocumentExecuting(job.documentId)) continue;\n\t\t\tif (!predicate({\n\t\t\t\tdocumentId: job.documentId,\n\t\t\t\tscope: job.scope,\n\t\t\t\tbranch: job.branch\n\t\t\t})) continue;\n\t\t\tconst jobIdx = queue.indexOf(job);\n\t\t\tqueue.splice(jobIdx, 1);\n\t\t\tthis.jobIdToQueueKey.delete(job.id);\n\t\t\tthis.markJobExecuting(job);\n\t\t\tif (queue.length === 0) this.queues.delete(queueKey);\n\t\t\tconst handle = new JobExecutionHandle(job, JobQueueState.READY, {\n\t\t\t\tonStart: () => {},\n\t\t\t\tonComplete: () => {\n\t\t\t\t\tthis.completeJob(job.id);\n\t\t\t\t},\n\t\t\t\tonFail: (error) => {\n\t\t\t\t\tthis.failJob(job.id, error);\n\t\t\t\t},\n\t\t\t\tonDefer: () => {\n\t\t\t\t\tthis.deferJob(job.id);\n\t\t\t\t}\n\t\t\t});\n\t\t\treturn Promise.resolve(handle);\n\t\t}\n\t\treturn Promise.resolve(null);\n\t}\n\tsize(documentId, scope, branch) {\n\t\tconst queueKey = this.createQueueKey(documentId, scope, branch);\n\t\tconst queue = this.queues.get(queueKey);\n\t\treturn Promise.resolve(queue ? queue.length : 0);\n\t}\n\ttotalSize() {\n\t\tlet total = 0;\n\t\tfor (const queue of this.queues.values()) total += queue.length;\n\t\treturn Promise.resolve(total);\n\t}\n\tremove(jobId) {\n\t\tconst queueKey = this.jobIdToQueueKey.get(jobId);\n\t\tif (!queueKey) return Promise.resolve(false);\n\t\tconst queue = this.queues.get(queueKey);\n\t\tif (!queue) {\n\t\t\tthis.jobIdToQueueKey.delete(jobId);\n\t\t\tthis.jobIndex.delete(jobId);\n\t\t\treturn Promise.resolve(false);\n\t\t}\n\t\tconst jobIdx = queue.findIndex((job) => job.id === jobId);\n\t\tif (jobIdx === -1) {\n\t\t\tthis.jobIdToQueueKey.delete(jobId);\n\t\t\tthis.jobIndex.delete(jobId);\n\t\t\treturn Promise.resolve(false);\n\t\t}\n\t\tqueue.splice(jobIdx, 1);\n\t\tthis.jobIdToQueueKey.delete(jobId);\n\t\tthis.jobIndex.delete(jobId);\n\t\tif (queue.length === 0) this.queues.delete(queueKey);\n\t\treturn Promise.resolve(true);\n\t}\n\tclear(documentId, scope, branch) {\n\t\tconst queueKey = this.createQueueKey(documentId, scope, branch);\n\t\tconst queue = this.queues.get(queueKey);\n\t\tif (queue) {\n\t\t\tfor (const job of queue) {\n\t\t\t\tthis.jobIdToQueueKey.delete(job.id);\n\t\t\t\tthis.jobIndex.delete(job.id);\n\t\t\t}\n\t\t\tthis.queues.delete(queueKey);\n\t\t}\n\t\treturn Promise.resolve();\n\t}\n\tclearAll() {\n\t\tthis.jobIdToQueueKey.clear();\n\t\tthis.jobIndex.clear();\n\t\tthis.completedJobs.clear();\n\t\tthis.queues.clear();\n\t\treturn Promise.resolve();\n\t}\n\thasJobs() {\n\t\treturn Promise.resolve(this.queues.size > 0 && Array.from(this.queues.values()).some((q) => q.length > 0));\n\t}\n\tasync completeJob(jobId) {\n\t\tconst documentId = this.jobIdToDocId.get(jobId);\n\t\tif (documentId) this.markJobComplete(jobId, documentId);\n\t\tthis.completedJobs.add(jobId);\n\t\tthis.jobIndex.delete(jobId);\n\t\tawait this.remove(jobId);\n\t\tthis.checkDrained();\n\t}\n\tasync failJob(jobId, error) {\n\t\tconst documentId = this.jobIdToDocId.get(jobId);\n\t\tif (documentId) this.markJobComplete(jobId, documentId);\n\t\tconst job = this.jobIndex.get(jobId);\n\t\tif (job) {\n\t\t\tjob.lastError = error;\n\t\t\tif (error) job.errorHistory.push(error);\n\t\t}\n\t\tthis.jobIndex.delete(jobId);\n\t\tthis.completedJobs.add(jobId);\n\t\tawait this.remove(jobId);\n\t\tthis.eventBus.emit(ReactorEventTypes.JOB_FAILED, {\n\t\t\tjobId,\n\t\t\terror: new Error(error?.message ?? \"Job failed\"),\n\t\t\tjob\n\t\t}).catch(() => {});\n\t\tthis.checkDrained();\n\t}\n\tdeferJob(jobId) {\n\t\tconst documentId = this.jobIdToDocId.get(jobId);\n\t\tif (documentId) this.markJobComplete(jobId, documentId);\n\t\tthis.jobIndex.delete(jobId);\n\t}\n\tasync retryJob(jobId, error) {\n\t\tconst job = this.jobIndex.get(jobId);\n\t\tif (!job) return;\n\t\tjob.lastError = error;\n\t\tconst documentId = this.jobIdToDocId.get(jobId);\n\t\tif (documentId) this.markJobComplete(jobId, documentId);\n\t\tthis.jobIndex.delete(jobId);\n\t\tthis.jobIdToQueueKey.delete(jobId);\n\t\tif (error) job.errorHistory.push(error);\n\t\tconst updatedJob = {\n\t\t\t...job,\n\t\t\tretryCount: (job.retryCount || 0) + 1,\n\t\t\tlastError: error\n\t\t};\n\t\tawait this.enqueue(updatedJob);\n\t}\n\t/**\n\t* Check if the queue is drained and call the callback if it is\n\t*/\n\tcheckDrained() {\n\t\tif (this.isDrained && this.onDrainedCallback) {\n\t\t\tconst callback = this.onDrainedCallback;\n\t\t\tthis.onDrainedCallback = void 0;\n\t\t\tcallback();\n\t\t}\n\t}\n\t/**\n\t* Returns true if and only if all jobs have been resolved.\n\t*/\n\tget isDrained() {\n\t\tconst hasPendingJobs = this.queues.size > 0 && Array.from(this.queues.values()).some((q) => q.length > 0);\n\t\tconst hasExecutingJobs = this.docIdToJobId.size > 0 && Array.from(this.docIdToJobId.values()).some((set) => set.size > 0);\n\t\treturn !hasPendingJobs && !hasExecutingJobs;\n\t}\n\t/**\n\t* Blocks the queue from accepting new jobs.\n\t* @param onDrained - Optional callback to call when the queue is drained\n\t*/\n\tblock(onDrained) {\n\t\tthis.isBlocked = true;\n\t\tthis.onDrainedCallback = onDrained;\n\t\tthis.checkDrained();\n\t}\n\t/**\n\t* Unblocks the queue from accepting new jobs.\n\t*/\n\tunblock() {\n\t\tthis.isBlocked = false;\n\t\tthis.onDrainedCallback = void 0;\n\t}\n\t/**\n\t* Pauses job dequeuing. Jobs can still be enqueued but dequeueNext() will return null.\n\t*/\n\tpause() {\n\t\tthis.isPausedFlag = true;\n\t}\n\t/**\n\t* Resumes job dequeuing and emits JOB_AVAILABLE events for pending jobs to wake up executors.\n\t*/\n\tasync resume() {\n\t\tthis.isPausedFlag = false;\n\t\tfor (const [, queue] of this.queues.entries()) if (queue.length > 0) {\n\t\t\tconst job = queue[0];\n\t\t\tawait this.eventBus.emit(QueueEventTypes.JOB_AVAILABLE, {\n\t\t\t\tdocumentId: job.documentId,\n\t\t\t\tscope: job.scope,\n\t\t\t\tbranch: job.branch,\n\t\t\t\tjobId: job.id\n\t\t\t});\n\t\t}\n\t}\n\t/**\n\t* Returns whether job dequeuing is paused.\n\t*/\n\tget paused() {\n\t\treturn this.isPausedFlag;\n\t}\n\t/**\n\t* Returns all pending jobs across all queues.\n\t*/\n\tgetPendingJobs() {\n\t\tconst jobs = [];\n\t\tfor (const queue of this.queues.values()) jobs.push(...queue);\n\t\treturn jobs;\n\t}\n\t/**\n\t* Returns a map of document IDs to sets of executing job IDs.\n\t*/\n\tgetExecutingJobIds() {\n\t\treturn new Map(Array.from(this.docIdToJobId.entries()).map(([k, v]) => [k, new Set(v)]));\n\t}\n\t/**\n\t* Returns a job by ID from the job index.\n\t*/\n\tgetJob(jobId) {\n\t\treturn this.jobIndex.get(jobId);\n\t}\n};\n//#endregion\n//#region src/registry/document-model-resolver.ts\n/**\n* Encapsulates the logic for resolving document model modules on demand.\n* Shared between the queue (CREATE_DOCUMENT gate) and the executor manager\n* (post-failure recovery) so that both paths use the same deduplication\n* and failure-caching state.\n*/\nvar DocumentModelResolver = class {\n\tloadingModels = /* @__PURE__ */ new Map();\n\tfailedModelTypes = /* @__PURE__ */ new Set();\n\tbroadcastHook = null;\n\tconstructor(registry, loader) {\n\t\tthis.registry = registry;\n\t\tthis.loader = loader;\n\t}\n\t/**\n\t* Install a post-success hook called after the resolver registers a\n\t* newly loaded model. ReactorBuilder uses this to wire the worker-pool\n\t* `load-model` broadcast without touching the resolver's constructor.\n\t*/\n\tsetBroadcastHook(hook) {\n\t\tthis.broadcastHook = hook;\n\t}\n\tasync ensureModelLoaded(documentType) {\n\t\ttry {\n\t\t\tthis.registry.getModule(documentType);\n\t\t\treturn;\n\t\t} catch (error) {\n\t\t\tif (!ModuleNotFoundError.isError(error)) throw error;\n\t\t}\n\t\tif (this.failedModelTypes.has(documentType)) throw new Error(`Document model type previously failed to load: ${documentType}`);\n\t\tconst existing = this.loadingModels.get(documentType);\n\t\tif (existing) return existing;\n\t\tconst loadPromise = (async () => {\n\t\t\ttry {\n\t\t\t\tconst module = await this.loader.load(documentType);\n\t\t\t\tconst [result] = this.registry.registerModules(module);\n\t\t\t\tif (result.status === \"error\" && !DuplicateModuleError.isError(result.error)) throw result.error;\n\t\t\t\tawait this.broadcastIfPossible(documentType);\n\t\t\t} catch (error) {\n\t\t\t\tthis.failedModelTypes.add(documentType);\n\t\t\t\tthrow error;\n\t\t\t} finally {\n\t\t\t\tthis.loadingModels.delete(documentType);\n\t\t\t}\n\t\t})();\n\t\tthis.loadingModels.set(documentType, loadPromise);\n\t\treturn loadPromise;\n\t}\n\tasync broadcastIfPossible(documentType) {\n\t\tif (!this.broadcastHook || !this.loader.resolveSpec) return;\n\t\tconst entry = await this.loader.resolveSpec(documentType);\n\t\tif (!entry) return;\n\t\tawait this.broadcastHook(entry);\n\t}\n};\n/**\n* No-op resolver used when no document model loader is configured.\n* Checks the registry for the model and returns if found; throws if not.\n* Since there is no loader, missing models cannot be recovered.\n*/\nvar NullDocumentModelResolver = class {\n\tconstructor(registry) {\n\t\tthis.registry = registry;\n\t}\n\tensureModelLoaded(documentType) {\n\t\tif (this.registry) try {\n\t\t\tthis.registry.getModule(documentType);\n\t\t\treturn Promise.resolve();\n\t\t} catch {}\n\t\treturn Promise.reject(new ModuleNotFoundError(documentType));\n\t}\n};\n//#endregion\n//#region src/subs/default-error-handler.ts\n/**\n* Default error handler that re-throws subscription errors.\n* This ensures that errors are not silently swallowed.\n*/\nvar DefaultSubscriptionErrorHandler = class {\n\thandleError(error, context) {\n\t\tconst errorMessage = `Subscription error in ${context.eventType} (${context.subscriptionId})`;\n\t\tif (error instanceof Error) {\n\t\t\tconst enhancedError = /* @__PURE__ */ new Error(`${errorMessage}: ${error.message}`);\n\t\t\tenhancedError.cause = error;\n\t\t\tenhancedError.stack = error.stack;\n\t\t\tthrow enhancedError;\n\t\t} else throw new Error(`${errorMessage}: ${String(error)}`);\n\t}\n};\n//#endregion\n//#region src/subs/react-subscription-manager.ts\nvar ReactorSubscriptionManager = class {\n\tcreatedSubscriptions = /* @__PURE__ */ new Map();\n\tdeletedSubscriptions = /* @__PURE__ */ new Map();\n\tupdatedSubscriptions = /* @__PURE__ */ new Map();\n\trelationshipSubscriptions = /* @__PURE__ */ new Map();\n\tsubscriptionCounter = 0;\n\terrorHandler;\n\tconstructor(errorHandler) {\n\t\tthis.errorHandler = errorHandler;\n\t}\n\tonDocumentCreated(callback, search) {\n\t\tconst id = `created-${++this.subscriptionCounter}`;\n\t\tthis.createdSubscriptions.set(id, {\n\t\t\tid,\n\t\t\tcallback,\n\t\t\tsearch\n\t\t});\n\t\treturn () => {\n\t\t\tthis.createdSubscriptions.delete(id);\n\t\t};\n\t}\n\tonDocumentDeleted(callback, search) {\n\t\tconst id = `deleted-${++this.subscriptionCounter}`;\n\t\tthis.deletedSubscriptions.set(id, {\n\t\t\tid,\n\t\t\tcallback,\n\t\t\tsearch\n\t\t});\n\t\treturn () => {\n\t\t\tthis.deletedSubscriptions.delete(id);\n\t\t};\n\t}\n\tonDocumentStateUpdated(callback, search, view) {\n\t\tconst id = `updated-${++this.subscriptionCounter}`;\n\t\tthis.updatedSubscriptions.set(id, {\n\t\t\tid,\n\t\t\tcallback,\n\t\t\tsearch,\n\t\t\tview\n\t\t});\n\t\treturn () => {\n\t\t\tthis.updatedSubscriptions.delete(id);\n\t\t};\n\t}\n\tonRelationshipChanged(callback, search) {\n\t\tconst id = `relationship-${++this.subscriptionCounter}`;\n\t\tthis.relationshipSubscriptions.set(id, {\n\t\t\tid,\n\t\t\tcallback,\n\t\t\tsearch\n\t\t});\n\t\treturn () => {\n\t\t\tthis.relationshipSubscriptions.delete(id);\n\t\t};\n\t}\n\t/**\n\t* Notify subscribers about created documents\n\t*/\n\tnotifyDocumentsCreated(documentIds, documentTypes, parentIds) {\n\t\tconst result = {\n\t\t\tresults: documentIds,\n\t\t\toptions: {\n\t\t\t\tcursor: \"\",\n\t\t\t\tlimit: documentIds.length\n\t\t\t}\n\t\t};\n\t\tfor (const subscription of this.createdSubscriptions.values()) {\n\t\t\tconst filteredIds = this.filterDocumentIds(documentIds, subscription.search, documentTypes, parentIds);\n\t\t\tif (filteredIds.length > 0) try {\n\t\t\t\tsubscription.callback({\n\t\t\t\t\t...result,\n\t\t\t\t\tresults: filteredIds\n\t\t\t\t});\n\t\t\t} catch (error) {\n\t\t\t\tthis.errorHandler.handleError(error, {\n\t\t\t\t\teventType: \"created\",\n\t\t\t\t\tsubscriptionId: subscription.id,\n\t\t\t\t\teventData: filteredIds\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\t/**\n\t* Notify subscribers about deleted documents\n\t*/\n\tnotifyDocumentsDeleted(documentIds, documentTypes, parentIds) {\n\t\tfor (const subscription of this.deletedSubscriptions.values()) {\n\t\t\tconst filteredIds = this.filterDocumentIds(documentIds, subscription.search, documentTypes, parentIds);\n\t\t\tif (filteredIds.length > 0) try {\n\t\t\t\tsubscription.callback(filteredIds);\n\t\t\t} catch (error) {\n\t\t\t\tthis.errorHandler.handleError(error, {\n\t\t\t\t\teventType: \"deleted\",\n\t\t\t\t\tsubscriptionId: subscription.id,\n\t\t\t\t\teventData: filteredIds\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\t/**\n\t* Notify subscribers about updated documents\n\t*/\n\tnotifyDocumentsUpdated(documents) {\n\t\tconst result = {\n\t\t\tresults: documents,\n\t\t\toptions: {\n\t\t\t\tcursor: \"\",\n\t\t\t\tlimit: documents.length\n\t\t\t}\n\t\t};\n\t\tfor (const subscription of this.updatedSubscriptions.values()) {\n\t\t\tconst filteredDocs = this.filterDocuments(documents, subscription.search);\n\t\t\tif (filteredDocs.length > 0) try {\n\t\t\t\tsubscription.callback({\n\t\t\t\t\t...result,\n\t\t\t\t\tresults: filteredDocs\n\t\t\t\t});\n\t\t\t} catch (error) {\n\t\t\t\tthis.errorHandler.handleError(error, {\n\t\t\t\t\teventType: \"updated\",\n\t\t\t\t\tsubscriptionId: subscription.id,\n\t\t\t\t\teventData: filteredDocs\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\t/**\n\t* Notify subscribers about relationship changes\n\t*/\n\tnotifyRelationshipChanged(parentId, childId, changeType, childType) {\n\t\tfor (const subscription of this.relationshipSubscriptions.values()) if (this.matchesRelationshipFilter(parentId, childId, childType, subscription.search)) try {\n\t\t\tsubscription.callback(parentId, childId, changeType);\n\t\t} catch (error) {\n\t\t\tthis.errorHandler.handleError(error, {\n\t\t\t\teventType: \"relationshipChanged\",\n\t\t\t\tsubscriptionId: subscription.id,\n\t\t\t\teventData: {\n\t\t\t\t\tparentId,\n\t\t\t\t\tchildId,\n\t\t\t\t\tchangeType\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t}\n\t/**\n\t* Clear all subscriptions\n\t*/\n\tclearAll() {\n\t\tthis.createdSubscriptions.clear();\n\t\tthis.deletedSubscriptions.clear();\n\t\tthis.updatedSubscriptions.clear();\n\t\tthis.relationshipSubscriptions.clear();\n\t}\n\tfilterDocumentIds(documentIds, search, documentTypes, parentIds) {\n\t\tif (!search) return documentIds;\n\t\treturn documentIds.filter((id) => {\n\t\t\tif (search.ids && !search.ids.includes(id)) return false;\n\t\t\tif (search.type && documentTypes) {\n\t\t\t\tif (documentTypes.get(id) !== search.type) return false;\n\t\t\t}\n\t\t\tif (search.parentId && parentIds) {\n\t\t\t\tif (parentIds.get(id) !== search.parentId) return false;\n\t\t\t}\n\t\t\treturn true;\n\t\t});\n\t}\n\tfilterDocuments(documents, search) {\n\t\tif (!search) return documents;\n\t\treturn documents.filter((doc) => {\n\t\t\tif (search.ids && !search.ids.includes(doc.header.id)) return false;\n\t\t\tif (search.type && doc.header.documentType !== search.type) return false;\n\t\t\tif (search.slugs && !search.slugs.includes(doc.header.slug)) return false;\n\t\t\treturn true;\n\t\t});\n\t}\n\tmatchesRelationshipFilter(parentId, childId, childType, search) {\n\t\tif (!search) return true;\n\t\tif (search.parentId && parentId !== search.parentId) return false;\n\t\tif (search.ids && !search.ids.includes(childId)) return false;\n\t\tif (search.type && childType && childType !== search.type) return false;\n\t\treturn true;\n\t}\n};\n//#endregion\n//#region src/subs/subscription-notification-read-model.ts\n/**\n* A read model that notifies the subscription manager when operations are processed.\n* This bridges the gap between operation processing and subscription callbacks.\n*\n* Must be processed AFTER other read models have completed and AFTER READ_READY\n* is emitted, so that reactor.get() returns fresh data when callbacks fire.\n*/\nvar SubscriptionNotificationReadModel = class {\n\tname = \"subscription-notification\";\n\tconstructor(subscriptionManager, documentView) {\n\t\tthis.subscriptionManager = subscriptionManager;\n\t\tthis.documentView = documentView;\n\t}\n\tasync indexOperations(operations) {\n\t\tif (operations.length === 0) return;\n\t\tconst created = [];\n\t\tconst deleted = [];\n\t\tconst updatedIds = /* @__PURE__ */ new Set();\n\t\tconst documentTypes = /* @__PURE__ */ new Map();\n\t\tconst parentIds = /* @__PURE__ */ new Map();\n\t\tfor (const item of operations) {\n\t\t\tconst { operation, context } = item;\n\t\t\tconst actionType = operation.action.type;\n\t\t\tdocumentTypes.set(context.documentId, context.documentType);\n\t\t\tif (actionType === \"CREATE_DOCUMENT\") created.push(context.documentId);\n\t\t\telse if (actionType === \"DELETE_DOCUMENT\") {\n\t\t\t\tconst deletedId = operation.action.input.documentId ?? context.documentId;\n\t\t\t\tdeleted.push(deletedId);\n\t\t\t} else if (actionType === \"ADD_RELATIONSHIP\") {\n\t\t\t\tconst input = operation.action.input;\n\t\t\t\tthis.subscriptionManager.notifyRelationshipChanged(input.sourceId, input.targetId, RelationshipChangeType.Added, input.childType);\n\t\t\t} else if (actionType === \"REMOVE_RELATIONSHIP\") {\n\t\t\t\tconst input = operation.action.input;\n\t\t\t\tthis.subscriptionManager.notifyRelationshipChanged(input.sourceId, input.targetId, RelationshipChangeType.Removed, input.childType);\n\t\t\t} else if (!created.includes(context.documentId)) updatedIds.add(context.documentId);\n\t\t}\n\t\tif (created.length > 0) this.subscriptionManager.notifyDocumentsCreated(created, documentTypes, parentIds);\n\t\tif (deleted.length > 0) this.subscriptionManager.notifyDocumentsDeleted(deleted, documentTypes, parentIds);\n\t\tif (updatedIds.size > 0 && this.documentView) {\n\t\t\tconst documents = await Promise.all(Array.from(updatedIds).map((id) => this.documentView.get(id)));\n\t\t\tthis.subscriptionManager.notifyDocumentsUpdated(documents);\n\t\t}\n\t}\n};\n//#endregion\n//#region src/sync/types.ts\nlet ChannelScheme = /* @__PURE__ */ function(ChannelScheme) {\n\tChannelScheme[\"CONNECT\"] = \"connect\";\n\tChannelScheme[\"SWITCHBOARD\"] = \"switchboard\";\n\treturn ChannelScheme;\n}({});\n/**\n* Controls how a remote drives its polling schedule.\n* - `Auto` (default): the channel runs the configured interval timer in the background.\n* - `Manual`: the channel registers and connects but never ticks on its own.\n* Callers must invoke ISyncManager.triggerPull(name) to fetch.\n*/\nlet PollBehavior = /* @__PURE__ */ function(PollBehavior) {\n\tPollBehavior[\"Auto\"] = \"auto\";\n\tPollBehavior[\"Manual\"] = \"manual\";\n\treturn PollBehavior;\n}({});\nlet SyncOperationStatus = /* @__PURE__ */ function(SyncOperationStatus) {\n\tSyncOperationStatus[SyncOperationStatus[\"Unknown\"] = -1] = \"Unknown\";\n\tSyncOperationStatus[SyncOperationStatus[\"TransportPending\"] = 0] = \"TransportPending\";\n\tSyncOperationStatus[SyncOperationStatus[\"ExecutionPending\"] = 1] = \"ExecutionPending\";\n\tSyncOperationStatus[SyncOperationStatus[\"Applied\"] = 2] = \"Applied\";\n\tSyncOperationStatus[SyncOperationStatus[\"Error\"] = 3] = \"Error\";\n\treturn SyncOperationStatus;\n}({});\nlet ChannelErrorSource = /* @__PURE__ */ function(ChannelErrorSource) {\n\tChannelErrorSource[\"None\"] = \"none\";\n\tChannelErrorSource[\"Channel\"] = \"channel\";\n\tChannelErrorSource[\"Inbox\"] = \"inbox\";\n\tChannelErrorSource[\"Outbox\"] = \"outbox\";\n\treturn ChannelErrorSource;\n}({});\n/**\n* Event types for sync lifecycle events.\n* These events track the sync progress of a job's operations to remotes.\n* Uses a separate namespace (20000 range) from ReactorEventTypes (10000 range).\n*/\nconst SyncEventTypes = {\n\tSYNC_PENDING: 20001,\n\tSYNC_SUCCEEDED: 20002,\n\tSYNC_FAILED: 20003,\n\tDEAD_LETTER_ADDED: 20004,\n\tCONNECTION_STATE_CHANGED: 20005\n};\n//#endregion\n//#region src/sync/mailbox.ts\nvar MailboxAggregateError = class extends Error {\n\terrors;\n\tconstructor(errors) {\n\t\tconst messages = errors.map((e) => e.message).join(\"; \");\n\t\tsuper(`Mailbox callback failed with ${errors.length} error(s): ${messages}`);\n\t\tthis.name = \"MailboxAggregateError\";\n\t\tthis.errors = errors;\n\t}\n};\nvar Mailbox = class {\n\titemsMap = /* @__PURE__ */ new Map();\n\taddedCallbacks = [];\n\tremovedCallbacks = [];\n\tpaused = false;\n\taddedBuffer = [];\n\tremovedBuffer = [];\n\t_ack = 0;\n\t_latestOrdinal = 0;\n\tinit(ackOrdinal) {\n\t\tthis._ack = this._latestOrdinal = ackOrdinal;\n\t}\n\tadvanceOrdinal(ordinal) {\n\t\tthis._latestOrdinal = Math.max(this._latestOrdinal, ordinal);\n\t}\n\tget items() {\n\t\treturn Array.from(this.itemsMap.values());\n\t}\n\tget ackOrdinal() {\n\t\treturn this._ack;\n\t}\n\tget latestOrdinal() {\n\t\treturn this._latestOrdinal;\n\t}\n\tget(id) {\n\t\treturn this.itemsMap.get(id);\n\t}\n\tadd(...items) {\n\t\tfor (const item of items) {\n\t\t\tthis.itemsMap.set(item.id, item);\n\t\t\tfor (const op of item.operations) this._latestOrdinal = Math.max(this._latestOrdinal, op.context.ordinal);\n\t\t\titem.on((syncOp, _, next) => {\n\t\t\t\tif (next === SyncOperationStatus.Applied) for (const op of syncOp.operations) this._ack = Math.max(this._ack, op.context.ordinal);\n\t\t\t});\n\t\t}\n\t\tif (this.paused) {\n\t\t\tthis.addedBuffer.push(...items);\n\t\t\treturn;\n\t\t}\n\t\tconst callbacks = [...this.addedCallbacks];\n\t\tconst errors = [];\n\t\tfor (const callback of callbacks) try {\n\t\t\tcallback(items);\n\t\t} catch (error) {\n\t\t\terrors.push(error instanceof Error ? error : new Error(String(error)));\n\t\t}\n\t\tif (errors.length > 0) throw new MailboxAggregateError(errors);\n\t}\n\tremove(...items) {\n\t\tfor (const item of items) this.itemsMap.delete(item.id);\n\t\tif (this.paused) {\n\t\t\tthis.removedBuffer.push(...items);\n\t\t\treturn;\n\t\t}\n\t\tconst callbacks = [...this.removedCallbacks];\n\t\tconst errors = [];\n\t\tfor (const callback of callbacks) try {\n\t\t\tcallback(items);\n\t\t} catch (error) {\n\t\t\terrors.push(error instanceof Error ? error : new Error(String(error)));\n\t\t}\n\t\tif (errors.length > 0) throw new MailboxAggregateError(errors);\n\t}\n\tonAdded(callback) {\n\t\tthis.addedCallbacks.push(callback);\n\t}\n\tonRemoved(callback) {\n\t\tthis.removedCallbacks.push(callback);\n\t}\n\tpause() {\n\t\tthis.paused = true;\n\t}\n\tresume() {\n\t\tthis.paused = false;\n\t\tthis.flush();\n\t}\n\tflush() {\n\t\tif (this.addedBuffer.length > 0) {\n\t\t\tconst items = this.addedBuffer.splice(0);\n\t\t\tconst callbacks = [...this.addedCallbacks];\n\t\t\tconst errors = [];\n\t\t\tfor (const callback of callbacks) try {\n\t\t\t\tcallback(items);\n\t\t\t} catch (error) {\n\t\t\t\terrors.push(error instanceof Error ? error : new Error(String(error)));\n\t\t\t}\n\t\t\tif (errors.length > 0) throw new MailboxAggregateError(errors);\n\t\t}\n\t\tif (this.removedBuffer.length > 0) {\n\t\t\tconst items = this.removedBuffer.splice(0);\n\t\t\tconst callbacks = [...this.removedCallbacks];\n\t\t\tconst errors = [];\n\t\t\tfor (const callback of callbacks) try {\n\t\t\t\tcallback(items);\n\t\t\t} catch (error) {\n\t\t\t\terrors.push(error instanceof Error ? error : new Error(String(error)));\n\t\t\t}\n\t\t\tif (errors.length > 0) throw new MailboxAggregateError(errors);\n\t\t}\n\t}\n\tisPaused() {\n\t\treturn this.paused;\n\t}\n};\n//#endregion\n//#region src/sync/buffered-mailbox.ts\nvar BufferedMailbox = class {\n\titemsMap = /* @__PURE__ */ new Map();\n\taddedCallbacks = [];\n\tremovedCallbacks = [];\n\taddedBuffer = [];\n\tremovedBuffer = [];\n\taddedTimer = null;\n\tremovedTimer = null;\n\tmilliseconds;\n\tmaxQueued;\n\tpaused = false;\n\t_ack = 0;\n\t_latestOrdinal = 0;\n\tconstructor(milliseconds, maxQueued) {\n\t\tthis.milliseconds = milliseconds;\n\t\tthis.maxQueued = maxQueued;\n\t}\n\tinit(ackOrdinal) {\n\t\tthis._ack = this._latestOrdinal = ackOrdinal;\n\t}\n\tadvanceOrdinal(ordinal) {\n\t\tthis._latestOrdinal = Math.max(this._latestOrdinal, ordinal);\n\t}\n\tget items() {\n\t\treturn Array.from(this.itemsMap.values());\n\t}\n\tget ackOrdinal() {\n\t\treturn this._ack;\n\t}\n\tget latestOrdinal() {\n\t\treturn this._latestOrdinal;\n\t}\n\tget(id) {\n\t\treturn this.itemsMap.get(id);\n\t}\n\tadd(...items) {\n\t\tfor (const item of items) {\n\t\t\tthis.itemsMap.set(item.id, item);\n\t\t\tfor (const op of item.operations) this._latestOrdinal = Math.max(this._latestOrdinal, op.context.ordinal);\n\t\t\titem.on((syncOp, _, next) => {\n\t\t\t\tif (next === SyncOperationStatus.Applied) for (const op of syncOp.operations) this._ack = Math.max(this._ack, op.context.ordinal);\n\t\t\t});\n\t\t}\n\t\tthis.addedBuffer.push(...items);\n\t\tif (this.paused) return;\n\t\tif (this.addedBuffer.length >= this.maxQueued) this.flushAdded();\n\t\telse this.scheduleAddedFlush();\n\t}\n\tremove(...items) {\n\t\tfor (const item of items) this.itemsMap.delete(item.id);\n\t\tthis.removedBuffer.push(...items);\n\t\tif (this.paused) return;\n\t\tif (this.removedBuffer.length >= this.maxQueued) this.flushRemoved();\n\t\telse this.scheduleRemovedFlush();\n\t}\n\tonAdded(callback) {\n\t\tthis.addedCallbacks.push(callback);\n\t}\n\tonRemoved(callback) {\n\t\tthis.removedCallbacks.push(callback);\n\t}\n\tpause() {\n\t\tthis.paused = true;\n\t\tif (this.addedTimer !== null) {\n\t\t\tclearTimeout(this.addedTimer);\n\t\t\tthis.addedTimer = null;\n\t\t}\n\t\tif (this.removedTimer !== null) {\n\t\t\tclearTimeout(this.removedTimer);\n\t\t\tthis.removedTimer = null;\n\t\t}\n\t}\n\tresume() {\n\t\tthis.paused = false;\n\t\tif (this.addedBuffer.length > 0) this.scheduleAddedFlush();\n\t\tif (this.removedBuffer.length > 0) this.scheduleRemovedFlush();\n\t}\n\tisPaused() {\n\t\treturn this.paused;\n\t}\n\tflush() {\n\t\tthis.flushAdded();\n\t\tthis.flushRemoved();\n\t}\n\tscheduleAddedFlush() {\n\t\tif (this.addedTimer !== null) clearTimeout(this.addedTimer);\n\t\tthis.addedTimer = setTimeout(() => {\n\t\t\tthis.flushAdded();\n\t\t}, this.milliseconds);\n\t}\n\tscheduleRemovedFlush() {\n\t\tif (this.removedTimer !== null) clearTimeout(this.removedTimer);\n\t\tthis.removedTimer = setTimeout(() => {\n\t\t\tthis.flushRemoved();\n\t\t}, this.milliseconds);\n\t}\n\tflushAdded() {\n\t\tif (this.addedTimer !== null) {\n\t\t\tclearTimeout(this.addedTimer);\n\t\t\tthis.addedTimer = null;\n\t\t}\n\t\tconst items = this.addedBuffer;\n\t\tthis.addedBuffer = [];\n\t\tif (items.length > 0) this.invokeCallbacks(this.addedCallbacks, items);\n\t}\n\tflushRemoved() {\n\t\tif (this.removedTimer !== null) {\n\t\t\tclearTimeout(this.removedTimer);\n\t\t\tthis.removedTimer = null;\n\t\t}\n\t\tconst items = this.removedBuffer;\n\t\tthis.removedBuffer = [];\n\t\tif (items.length > 0) this.invokeCallbacks(this.removedCallbacks, items);\n\t}\n\tinvokeCallbacks(callbacks, items) {\n\t\tconst callbacksCopy = [...callbacks];\n\t\tconst errors = [];\n\t\tfor (const callback of callbacksCopy) try {\n\t\t\tcallback(items);\n\t\t} catch (error) {\n\t\t\terrors.push(error instanceof Error ? error : new Error(String(error)));\n\t\t}\n\t\tif (errors.length > 0) throw new MailboxAggregateError(errors);\n\t}\n};\n//#endregion\n//#region src/sync/errors.ts\nvar GraphQLRequestError = class extends Error {\n\tstatusCode;\n\tcategory;\n\tconstructor(message, category, statusCode) {\n\t\tsuper(message);\n\t\tthis.name = \"GraphQLRequestError\";\n\t\tthis.category = category;\n\t\tthis.statusCode = statusCode;\n\t}\n};\nvar PollingChannelError = class extends Error {\n\tconstructor(message) {\n\t\tsuper(message);\n\t\tthis.name = \"PollingChannelError\";\n\t}\n};\nvar ChannelError = class extends Error {\n\tsource;\n\terror;\n\tconstructor(source, error) {\n\t\tsuper(`ChannelError[${source}]: ${error.message}`);\n\t\tthis.name = \"ChannelError\";\n\t\tthis.source = source;\n\t\tthis.error = error;\n\t}\n};\n//#endregion\n//#region src/sync/sync-operation.ts\nvar SyncOperationAggregateError = class extends Error {\n\terrors;\n\tconstructor(errors) {\n\t\tconst messages = errors.map((e) => e.message).join(\"; \");\n\t\tsuper(`SyncOperation callback failed with ${errors.length} error(s): ${messages}`);\n\t\tthis.name = \"SyncOperationAggregateError\";\n\t\tthis.errors = errors;\n\t}\n};\nvar SyncOperation = class {\n\tid;\n\tjobId;\n\tjobDependencies;\n\tremoteName;\n\tdocumentId;\n\tscopes;\n\tbranch;\n\toperations;\n\tstatus;\n\terror;\n\tdeliveredCount = 0;\n\temittedCount = 0;\n\tcallbacks = [];\n\tconstructor(id, jobId, jobDependencies, remoteName, documentId, scopes, branch, operations) {\n\t\tthis.id = id;\n\t\tthis.jobId = jobId;\n\t\tthis.jobDependencies = jobDependencies;\n\t\tthis.remoteName = remoteName;\n\t\tthis.documentId = documentId;\n\t\tthis.scopes = scopes;\n\t\tthis.branch = branch;\n\t\tthis.operations = operations;\n\t\tthis.status = SyncOperationStatus.Unknown;\n\t}\n\ton(callback) {\n\t\tthis.callbacks.push(callback);\n\t}\n\tstarted() {\n\t\tthis.transition(SyncOperationStatus.TransportPending);\n\t}\n\ttransported() {\n\t\tthis.transition(SyncOperationStatus.ExecutionPending);\n\t}\n\texecuted() {\n\t\tthis.transition(SyncOperationStatus.Applied);\n\t}\n\tfailed(error) {\n\t\tthis.error = error;\n\t\tthis.transition(SyncOperationStatus.Error);\n\t}\n\ttransition(next) {\n\t\tconst prev = this.status;\n\t\tif (next <= prev) return;\n\t\tthis.status = next;\n\t\tconst errors = [];\n\t\tfor (const callback of this.callbacks) try {\n\t\t\tcallback(this, prev, next);\n\t\t} catch (error) {\n\t\t\terrors.push(error instanceof Error ? error : new Error(String(error)));\n\t\t}\n\t\tif (errors.length > 0) throw new SyncOperationAggregateError(errors);\n\t}\n};\n//#endregion\n//#region src/sync/utils.ts\n/**\n* Trims a mailbox using the jobIds from a batch.\n*/\nfunction trimMailboxFromBatch(mailbox, batch) {\n\tconst toRemove = [];\n\tfor (const syncOp of batch.entries) for (const item of mailbox.items) if (syncOp.event.jobId === item.jobId) {\n\t\ttoRemove.push(item);\n\t\tbreak;\n\t}\n\tif (toRemove.length > 0) {\n\t\tfor (const syncOp of toRemove) syncOp.executed();\n\t\tmailbox.remove(...toRemove);\n\t}\n}\n/**\n* Trims a mailbox using the ack ordinal.\n*/\nfunction trimMailboxFromAckOrdinal(mailbox, ackOrdinal) {\n\tconst toRemove = [];\n\tfor (const syncOp of mailbox.items) {\n\t\tlet maxOrdinal = 0;\n\t\tfor (const op of syncOp.operations) maxOrdinal = Math.max(maxOrdinal, op.context.ordinal);\n\t\tif (maxOrdinal <= ackOrdinal) toRemove.push(syncOp);\n\t}\n\tif (toRemove.length > 0) {\n\t\tfor (const syncOp of toRemove) syncOp.executed();\n\t\tmailbox.remove(...toRemove);\n\t}\n}\n/**\n* Filters operations based on a remote's filter criteria.\n*\n* @param operations - The operations to filter\n* @param filter - The filter criteria to apply\n* @returns The filtered operations that match the criteria\n*/\nfunction filterOperations(operations, filter) {\n\treturn operations.filter((op) => {\n\t\tif (filter.branch && op.context.branch !== filter.branch) return false;\n\t\tif (filter.documentId.length > 0 && !filter.documentId.includes(op.context.documentId)) return false;\n\t\tif (filter.scope.length > 0 && !filter.scope.includes(op.context.scope)) return false;\n\t\treturn true;\n\t});\n}\n/**\n* Creates an idle channel health status.\n*\n* @returns A new idle channel health object\n*/\nfunction createIdleHealth() {\n\treturn {\n\t\tstate: \"idle\",\n\t\tfailureCount: 0\n\t};\n}\n/**\n* Batches consecutive operations by documentId and scope, preserving ordering.\n*\n* For operations [a1_doc, a1_global, a2_doc, b1_global], this returns:\n* - Batch 1: [a1_doc] for doc-a, document scope\n* - Batch 2: [a1_global] for doc-a, global scope\n* - Batch 3: [a2_doc] for doc-a, document scope\n* - Batch 4: [b1_global] for doc-b, global scope\n*\n* This ensures operations are grouped for efficient processing while maintaining\n* causality across documents and scopes.\n*/\nfunction batchOperationsByDocument(operations) {\n\tconst batches = [];\n\tlet currentDocId = null;\n\tlet currentScope = null;\n\tlet currentBatch = [];\n\tconst flushBatch = () => {\n\t\tif (currentBatch.length === 0 || currentDocId === null || currentScope === null) return;\n\t\tbatches.push({\n\t\t\tdocumentId: currentDocId,\n\t\t\tbranch: currentBatch[0].context.branch,\n\t\t\tscope: currentScope,\n\t\t\toperations: currentBatch\n\t\t});\n\t\tcurrentBatch = [];\n\t};\n\tfor (const op of operations) {\n\t\tconst docId = op.context.documentId;\n\t\tconst scope = op.context.scope;\n\t\tif (docId !== currentDocId || scope !== currentScope) {\n\t\t\tflushBatch();\n\t\t\tcurrentDocId = docId;\n\t\t\tcurrentScope = scope;\n\t\t}\n\t\tcurrentBatch.push(op);\n\t}\n\tflushBatch();\n\treturn batches;\n}\n/**\n* Splits a sorted page of operations into a safe-to-emit prefix and a\n* deferred tail containing the trailing run that shares the same\n* (documentId, branch, scope, timestampUtcMs) as the last operation.\n*\n* The page is assumed to be sorted by (documentId, scope, ordinal), so a\n* same-(docId, scope, ts) run is contiguous and lives at the end of any\n* page that contains its last member. Holding that tail back lets callers\n* prepend it to the next page so a single producer-side execute() call\n* never gets split across two outbound envelopes.\n*/\nfunction splitTrailingSameTimestampRun(operations) {\n\tif (operations.length === 0) return {\n\t\temit: [],\n\t\tcarry: []\n\t};\n\tconst last = operations[operations.length - 1];\n\tconst lastDocId = last.context.documentId;\n\tconst lastBranch = last.context.branch;\n\tconst lastScope = last.context.scope;\n\tconst lastTs = last.operation.timestampUtcMs;\n\tlet carryStart = operations.length;\n\tfor (let i = operations.length - 1; i >= 0; i--) {\n\t\tconst op = operations[i];\n\t\tif (op.context.documentId === lastDocId && op.context.branch === lastBranch && op.context.scope === lastScope && op.operation.timestampUtcMs === lastTs) carryStart = i;\n\t\telse break;\n\t}\n\treturn {\n\t\temit: operations.slice(0, carryStart),\n\t\tcarry: operations.slice(carryStart)\n\t};\n}\nfunction toOperationWithContext(entry) {\n\treturn {\n\t\toperation: {\n\t\t\tid: entry.id,\n\t\t\tindex: entry.index,\n\t\t\tskip: entry.skip,\n\t\t\thash: entry.hash,\n\t\t\ttimestampUtcMs: entry.timestampUtcMs,\n\t\t\taction: entry.action\n\t\t},\n\t\tcontext: {\n\t\t\tdocumentId: entry.documentId,\n\t\t\tdocumentType: entry.documentType,\n\t\t\tscope: entry.scope,\n\t\t\tbranch: entry.branch,\n\t\t\tordinal: entry.ordinal ?? 0\n\t\t}\n\t};\n}\n/**\n* Merges SyncOperations that share the same (documentId, scope, branch) into\n* a single SyncOperation per group. Within each group, operations are sorted\n* by context.ordinal. The merged SyncOperation keeps the first group member's\n* jobId; all other jobIds are remapped so external dependencies still resolve.\n*/\nfunction consolidateSyncOperations(syncOps) {\n\tif (syncOps.length <= 1) return syncOps;\n\tconst groups = /* @__PURE__ */ new Map();\n\tconst jobIdRemap = /* @__PURE__ */ new Map();\n\tconst insertionOrder = [];\n\tfor (const syncOp of syncOps) {\n\t\tconst key = `${syncOp.documentId}|${syncOp.scopes.slice().sort().join(\",\")}|${syncOp.branch}`;\n\t\tconst existing = groups.get(key);\n\t\tif (existing) {\n\t\t\texisting.ops.push(syncOp);\n\t\t\tif (syncOp.jobId && syncOp.jobId !== existing.canonicalJobId) jobIdRemap.set(syncOp.jobId, existing.canonicalJobId);\n\t\t} else {\n\t\t\tgroups.set(key, {\n\t\t\t\tops: [syncOp],\n\t\t\t\tcanonicalJobId: syncOp.jobId\n\t\t\t});\n\t\t\tinsertionOrder.push(key);\n\t\t}\n\t}\n\tconst result = [];\n\tfor (const key of insertionOrder) {\n\t\tconst group = groups.get(key);\n\t\tconst allOperations = group.ops.flatMap((op) => op.operations).sort((a, b) => a.context.ordinal - b.context.ordinal);\n\t\tconst allDeps = /* @__PURE__ */ new Set();\n\t\tfor (const op of group.ops) for (const dep of op.jobDependencies) allDeps.add(dep);\n\t\tallDeps.delete(group.canonicalJobId);\n\t\tfor (const op of group.ops) allDeps.delete(op.jobId);\n\t\tconst remappedDeps = [];\n\t\tfor (const dep of allDeps) {\n\t\t\tconst mapped = jobIdRemap.get(dep) ?? dep;\n\t\t\tif (!remappedDeps.includes(mapped) && mapped !== group.canonicalJobId) remappedDeps.push(mapped);\n\t\t}\n\t\tconst first = group.ops[0];\n\t\tconst merged = new SyncOperation(first.id, first.jobId, remappedDeps, first.remoteName, first.documentId, first.scopes, first.branch, allOperations);\n\t\tif (first.status > SyncOperationStatus.TransportPending) if (first.status >= SyncOperationStatus.Error) merged.executed();\n\t\telse if (first.status >= SyncOperationStatus.Applied) merged.transported();\n\t\telse merged.started();\n\t\tresult.push(merged);\n\t}\n\treturn result;\n}\n/**\n* Chunks sync operations into batches that respect dependency-connected\n* components. SyncOps linked by jobDependencies are kept in the same chunk.\n* If a connected component exceeds maxSize, it is split by topological order.\n*/\nfunction chunkSyncOperations(items, maxSize) {\n\tif (items.length === 0) return [];\n\tif (items.length <= maxSize) return [items];\n\tconst parent = items.map((_, i) => i);\n\tconst rank = new Array(items.length).fill(0);\n\tfunction find(x) {\n\t\twhile (parent[x] !== x) {\n\t\t\tparent[x] = parent[parent[x]];\n\t\t\tx = parent[x];\n\t\t}\n\t\treturn x;\n\t}\n\tfunction union(a, b) {\n\t\tconst ra = find(a);\n\t\tconst rb = find(b);\n\t\tif (ra === rb) return;\n\t\tif (rank[ra] < rank[rb]) parent[ra] = rb;\n\t\telse if (rank[ra] > rank[rb]) parent[rb] = ra;\n\t\telse {\n\t\t\tparent[rb] = ra;\n\t\t\trank[ra]++;\n\t\t}\n\t}\n\tconst jobIdToIndex = /* @__PURE__ */ new Map();\n\tfor (let i = 0; i < items.length; i++) jobIdToIndex.set(items[i].syncOp.jobId, i);\n\tfor (let i = 0; i < items.length; i++) for (const dep of items[i].syncOp.jobDependencies) {\n\t\tconst depIdx = jobIdToIndex.get(dep);\n\t\tif (depIdx !== void 0) union(i, depIdx);\n\t}\n\tconst componentMap = /* @__PURE__ */ new Map();\n\tfor (let i = 0; i < items.length; i++) {\n\t\tconst root = find(i);\n\t\tlet component = componentMap.get(root);\n\t\tif (!component) {\n\t\t\tcomponent = [];\n\t\t\tcomponentMap.set(root, component);\n\t\t}\n\t\tcomponent.push(items[i]);\n\t}\n\tconst components = [...componentMap.values()];\n\tconst chunks = [];\n\tlet currentChunk = [];\n\tfor (const component of components) {\n\t\tif (component.length > maxSize) {\n\t\t\tif (currentChunk.length > 0) {\n\t\t\t\tchunks.push(currentChunk);\n\t\t\t\tcurrentChunk = [];\n\t\t\t}\n\t\t\tfor (const subChunk of splitComponent(component, maxSize)) chunks.push(subChunk);\n\t\t\tcontinue;\n\t\t}\n\t\tif (currentChunk.length + component.length > maxSize) {\n\t\t\tif (currentChunk.length > 0) chunks.push(currentChunk);\n\t\t\tcurrentChunk = [...component];\n\t\t} else currentChunk.push(...component);\n\t}\n\tif (currentChunk.length > 0) chunks.push(currentChunk);\n\treturn chunks;\n}\n/**\n* Splits an oversized connected component into chunks by topological order.\n* Cross-chunk dependency references are handled by the caller's dep filter.\n*/\nfunction splitComponent(items, maxSize) {\n\tconst jobIdToItem = /* @__PURE__ */ new Map();\n\tconst jobIds = /* @__PURE__ */ new Set();\n\tfor (const item of items) {\n\t\tjobIdToItem.set(item.syncOp.jobId, item);\n\t\tjobIds.add(item.syncOp.jobId);\n\t}\n\tconst inDegree = /* @__PURE__ */ new Map();\n\tconst adjacency = /* @__PURE__ */ new Map();\n\tfor (const item of items) {\n\t\tconst key = item.syncOp.jobId;\n\t\tif (!inDegree.has(key)) inDegree.set(key, 0);\n\t\tif (!adjacency.has(key)) adjacency.set(key, []);\n\t\tfor (const dep of item.syncOp.jobDependencies) if (jobIds.has(dep)) {\n\t\t\tinDegree.set(key, (inDegree.get(key) ?? 0) + 1);\n\t\t\tif (!adjacency.has(dep)) adjacency.set(dep, []);\n\t\t\tadjacency.get(dep).push(key);\n\t\t}\n\t}\n\tconst queue = [];\n\tfor (const [key, degree] of inDegree) if (degree === 0) queue.push(key);\n\tconst sorted = [];\n\twhile (queue.length > 0) {\n\t\tconst key = queue.shift();\n\t\tsorted.push(jobIdToItem.get(key));\n\t\tfor (const neighbor of adjacency.get(key) ?? []) {\n\t\t\tconst newDegree = (inDegree.get(neighbor) ?? 1) - 1;\n\t\t\tinDegree.set(neighbor, newDegree);\n\t\t\tif (newDegree === 0) queue.push(neighbor);\n\t\t}\n\t}\n\tif (sorted.length < items.length) {\n\t\tconst sortedIds = new Set(sorted.map((item) => item.syncOp.jobId));\n\t\tfor (const item of items) if (!sortedIds.has(item.syncOp.jobId)) sorted.push(item);\n\t}\n\tconst chunks = [];\n\tfor (let i = 0; i < sorted.length; i += maxSize) chunks.push(sorted.slice(i, i + maxSize));\n\treturn chunks;\n}\n//#endregion\n//#region src/sync/channels/interval-poll-timer.ts\nconst DEFAULT_CONFIG = {\n\tintervalMs: 2e3,\n\tmaxQueueDepth: 100,\n\tbackpressureCheckIntervalMs: 500,\n\tretryBaseDelayMs: 1e3,\n\tretryMaxDelayMs: 3e5,\n\tstartPaused: false\n};\nfunction calculateBackoffDelay(consecutiveFailures, retryBaseDelayMs, retryMaxDelayMs, random) {\n\tconst backoff = Math.min(retryMaxDelayMs, retryBaseDelayMs * Math.pow(2, consecutiveFailures - 1));\n\treturn backoff / 2 + random * (backoff / 2);\n}\n/**\n* Default poll timer using setTimeout.\n* Waits for delegate completion before scheduling next tick.\n* Checks queue depth and defers polling when backpressure is detected.\n*/\nvar IntervalPollTimer = class {\n\tdelegate;\n\ttimer;\n\trunning;\n\tpaused;\n\tconsecutiveFailures;\n\tqueue;\n\tconfig;\n\tconstructor(queue, config = {}) {\n\t\tthis.queue = queue;\n\t\tthis.config = {\n\t\t\t...DEFAULT_CONFIG,\n\t\t\t...config\n\t\t};\n\t\tthis.running = false;\n\t\tthis.paused = this.config.startPaused;\n\t\tthis.consecutiveFailures = 0;\n\t}\n\tsetDelegate(delegate) {\n\t\tthis.delegate = delegate;\n\t}\n\tstart() {\n\t\tthis.running = true;\n\t\tthis.consecutiveFailures = 0;\n\t\tif (!this.paused) this.tick();\n\t}\n\tstop() {\n\t\tthis.running = false;\n\t\tif (this.timer) {\n\t\t\tclearTimeout(this.timer);\n\t\t\tthis.timer = void 0;\n\t\t}\n\t}\n\ttick() {\n\t\tif (!this.delegate || !this.running) return;\n\t\tconst delegate = this.delegate;\n\t\tthis.queue.totalSize().then((size) => {\n\t\t\tif (!this.running) return;\n\t\t\tif (size > this.config.maxQueueDepth) this.scheduleBackpressureRecheck();\n\t\t\telse delegate().then(() => {\n\t\t\t\tthis.consecutiveFailures = 0;\n\t\t\t\tthis.scheduleNext();\n\t\t\t}).catch(() => {\n\t\t\t\tthis.consecutiveFailures++;\n\t\t\t\tthis.scheduleRetry();\n\t\t\t});\n\t\t}).catch(() => {\n\t\t\tthis.scheduleNext();\n\t\t});\n\t}\n\tscheduleNext() {\n\t\tif (!this.running || this.paused) return;\n\t\tthis.timer = setTimeout(() => this.tick(), this.config.intervalMs);\n\t}\n\tscheduleRetry() {\n\t\tif (!this.running || this.paused) return;\n\t\tconst delay = calculateBackoffDelay(this.consecutiveFailures, this.config.retryBaseDelayMs, this.config.retryMaxDelayMs, Math.random());\n\t\tthis.timer = setTimeout(() => this.tick(), delay);\n\t}\n\tscheduleBackpressureRecheck() {\n\t\tif (!this.running || this.paused) return;\n\t\tthis.timer = setTimeout(() => this.tick(), this.config.backpressureCheckIntervalMs);\n\t}\n\tpause() {\n\t\tthis.paused = true;\n\t\tif (this.timer) {\n\t\t\tclearTimeout(this.timer);\n\t\t\tthis.timer = void 0;\n\t\t}\n\t}\n\tresume() {\n\t\tthis.paused = false;\n\t\tif (this.running) this.scheduleNext();\n\t}\n\ttriggerNow() {\n\t\tif (this.running && this.delegate) this.tick();\n\t}\n\tisPaused() {\n\t\treturn this.paused;\n\t}\n\tisRunning() {\n\t\treturn this.running;\n\t}\n\tgetIntervalMs() {\n\t\treturn this.config.intervalMs;\n\t}\n\tsetIntervalMs(ms) {\n\t\tthis.config.intervalMs = ms;\n\t}\n};\n//#endregion\n//#region src/sync/channels/utils.ts\nlet syncOpCounter = 0;\n/**\n* Serializes an action for GraphQL transport, converting signature tuples to strings.\n*\n* Only the fields declared by the GraphQL `ActionInput` type are forwarded. This\n* guards against stale runtime-only fields (e.g. a legacy `attachments` array on\n* operations persisted before the attachment-system removal) leaking into the\n* mutation variables, where the tightened schema would reject them.\n*/\nfunction serializeAction(action) {\n\tconst base = {\n\t\tid: action.id,\n\t\ttype: action.type,\n\t\ttimestampUtcMs: action.timestampUtcMs,\n\t\tinput: action.input,\n\t\tscope: action.scope\n\t};\n\tconst signer = action.context?.signer;\n\tif (!signer?.signatures) return action.context ? {\n\t\t...base,\n\t\tcontext: action.context\n\t} : base;\n\treturn {\n\t\t...base,\n\t\tcontext: {\n\t\t\t...action.context,\n\t\t\tsigner: {\n\t\t\t\t...signer,\n\t\t\t\tsignatures: signer.signatures.map((sig) => Array.isArray(sig) ? sig.join(\", \") : sig)\n\t\t\t}\n\t\t}\n\t};\n}\n/**\n* Serializes a SyncEnvelope for GraphQL transport.\n*\n* Signatures are serialized as comma-separated strings since GraphQL schema\n* defines them as [String!]!. The resultingState context field is stripped\n* since it is not defined in OperationContextInput.\n*/\nfunction serializeEnvelope(envelope) {\n\treturn {\n\t\ttype: envelope.type.toUpperCase(),\n\t\tchannelMeta: envelope.channelMeta,\n\t\toperations: envelope.operations?.map((opWithContext) => ({\n\t\t\toperation: {\n\t\t\t\tindex: opWithContext.operation.index,\n\t\t\t\ttimestampUtcMs: opWithContext.operation.timestampUtcMs,\n\t\t\t\thash: opWithContext.operation.hash,\n\t\t\t\tskip: opWithContext.operation.skip,\n\t\t\t\terror: opWithContext.operation.error,\n\t\t\t\tid: opWithContext.operation.id,\n\t\t\t\taction: serializeAction(opWithContext.operation.action)\n\t\t\t},\n\t\t\tcontext: {\n\t\t\t\tdocumentId: opWithContext.context.documentId,\n\t\t\t\tdocumentType: opWithContext.context.documentType,\n\t\t\t\tscope: opWithContext.context.scope,\n\t\t\t\tbranch: opWithContext.context.branch,\n\t\t\t\tordinal: opWithContext.context.ordinal\n\t\t\t}\n\t\t})),\n\t\tcursor: envelope.cursor,\n\t\tkey: envelope.key,\n\t\tdependsOn: envelope.dependsOn\n\t};\n}\n/**\n* Deserializes a signature from a comma-separated string back to a tuple.\n*\n* GraphQL serializes Signature tuples as comma-separated strings for transport.\n* This function converts them back to the expected [string, string, string, string, string] format.\n*/\nfunction deserializeSignature(sig) {\n\tif (Array.isArray(sig)) return sig;\n\treturn sig.split(\", \");\n}\n/**\n* Deserializes signatures in an operation's signer context from strings back to tuples.\n*\n* When operations are transported via GraphQL, signatures are serialized as comma-separated\n* strings. This function restores them to the Signature tuple format required for verification.\n*/\nfunction deserializeOperationSignatures(opWithContext) {\n\tconst signer = opWithContext.operation.action.context?.signer;\n\tif (!signer?.signatures || signer.signatures.length === 0) return opWithContext;\n\tconst deserializedSignatures = signer.signatures.map(deserializeSignature);\n\tconst deserializedOperation = {\n\t\t...opWithContext.operation,\n\t\taction: {\n\t\t\t...opWithContext.operation.action,\n\t\t\tcontext: {\n\t\t\t\t...opWithContext.operation.action.context,\n\t\t\t\tsigner: {\n\t\t\t\t\t...signer,\n\t\t\t\t\tsignatures: deserializedSignatures\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t};\n\treturn {\n\t\t...opWithContext,\n\t\toperation: deserializedOperation\n\t};\n}\n/**\n* Converts a SyncEnvelope containing operations into multiple SyncOperations.\n*\n* This function batches operations by documentId, preserving cross-document ordering.\n* For operations [a1, a2, a3, b1, b2, a4], it returns:\n* - SyncOperation 1: [a1, a2, a3] for doc-a\n* - SyncOperation 2: [b1, b2] for doc-b\n* - SyncOperation 3: [a4] for doc-a\n*\n* This ensures operations are grouped for efficient processing while maintaining\n* causality across documents.\n*/\nfunction envelopesToSyncOperations(envelope, remoteName) {\n\tif (!envelope.operations || envelope.operations.length === 0) return [];\n\treturn batchOperationsByDocument(envelope.operations.map(deserializeOperationSignatures)).map((batch) => {\n\t\treturn new SyncOperation(`syncop-${envelope.channelMeta.id}-${Date.now()}-${syncOpCounter++}`, envelope.key ?? \"\", (envelope.dependsOn ?? []).filter(Boolean), remoteName, batch.documentId, [batch.scope], batch.branch, batch.operations);\n\t});\n}\nconst getLatestAppliedOrdinal = (syncOps) => {\n\tlet maxOrdinal = 0;\n\tfor (const syncOp of syncOps) if (syncOp.status === SyncOperationStatus.Applied) for (const op of syncOp.operations) maxOrdinal = Math.max(maxOrdinal, op.context.ordinal);\n\treturn maxOrdinal;\n};\n//#endregion\n//#region src/sync/channels/gql-req-channel.ts\n/**\n* GraphQL-based synchronization channel for network communication between reactors.\n*/\nvar GqlRequestChannel = class {\n\tinbox;\n\toutbox;\n\tdeadLetter;\n\tconfig;\n\tbufferedOutbox;\n\tchannelId;\n\tremoteName;\n\tcursorStorage;\n\toperationIndex;\n\tpollTimer;\n\tabortController = new AbortController();\n\tisShutdown;\n\tfailureCount;\n\tlastSuccessUtcMs;\n\tlastFailureUtcMs;\n\tlastPersistedInboxOrdinal = 0;\n\tlastPersistedOutboxOrdinal = 0;\n\tpushFailureCount = 0;\n\tpushRetryTimer = null;\n\tpushBlocked = false;\n\tisPushing = false;\n\tpendingDrain = false;\n\treceivingPages = false;\n\tconnectionState = \"connecting\";\n\tconnectionStateCallbacks = /* @__PURE__ */ new Set();\n\tconstructor(logger, channelId, remoteName, cursorStorage, config, operationIndex, pollTimer) {\n\t\tthis.logger = logger;\n\t\tthis.channelId = channelId;\n\t\tthis.remoteName = remoteName;\n\t\tthis.cursorStorage = cursorStorage;\n\t\tthis.operationIndex = operationIndex;\n\t\tthis.pollTimer = pollTimer;\n\t\tthis.config = {\n\t\t\turl: config.url,\n\t\t\tjwtHandler: config.jwtHandler,\n\t\t\tfetchFn: config.fetchFn,\n\t\t\tcollectionId: config.collectionId,\n\t\t\tfilter: config.filter,\n\t\t\tretryBaseDelayMs: config.retryBaseDelayMs,\n\t\t\tretryMaxDelayMs: config.retryMaxDelayMs\n\t\t};\n\t\tthis.isShutdown = false;\n\t\tthis.failureCount = 0;\n\t\tthis.inbox = new Mailbox();\n\t\tthis.bufferedOutbox = new BufferedMailbox(500, 25);\n\t\tthis.outbox = this.bufferedOutbox;\n\t\tthis.deadLetter = new Mailbox();\n\t\tthis.deadLetter.onAdded((syncOps) => {\n\t\t\tfor (const syncOp of syncOps) this.logger.warn(\"Dead letter added for document @DocumentId on channel @ChannelId\", syncOp.documentId, this.channelId);\n\t\t});\n\t\tthis.outbox.onAdded((syncOps) => {\n\t\t\tif (this.isShutdown) return;\n\t\t\tif (this.isPushing) {\n\t\t\t\tthis.pendingDrain = true;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (this.pushBlocked) return;\n\t\t\tif (this.receivingPages) {\n\t\t\t\tthis.pendingDrain = true;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthis.attemptPush(syncOps);\n\t\t});\n\t\tthis.outbox.onRemoved((syncOps) => {\n\t\t\tconst maxOrdinal = getLatestAppliedOrdinal(syncOps);\n\t\t\tif (maxOrdinal > this.lastPersistedOutboxOrdinal) {\n\t\t\t\tthis.lastPersistedOutboxOrdinal = maxOrdinal;\n\t\t\t\tthis.cursorStorage.upsert({\n\t\t\t\t\tremoteName: this.remoteName,\n\t\t\t\t\tcursorType: \"outbox\",\n\t\t\t\t\tcursorOrdinal: maxOrdinal,\n\t\t\t\t\tlastSyncedAtUtcMs: Date.now()\n\t\t\t\t}).catch((error) => {\n\t\t\t\t\tthis.logger.error(\"Failed to update outbox cursor for @ChannelId! This means that future application runs may resend duplicate operations. This is recoverable (with deduplication protection), but not-optimal: @Error\", this.channelId, error);\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t\tthis.inbox.onRemoved((syncOps) => {\n\t\t\tconst maxOrdinal = getLatestAppliedOrdinal(syncOps);\n\t\t\tif (maxOrdinal > this.lastPersistedInboxOrdinal) {\n\t\t\t\tthis.lastPersistedInboxOrdinal = maxOrdinal;\n\t\t\t\tthis.cursorStorage.upsert({\n\t\t\t\t\tremoteName: this.remoteName,\n\t\t\t\t\tcursorType: \"inbox\",\n\t\t\t\t\tcursorOrdinal: maxOrdinal,\n\t\t\t\t\tlastSyncedAtUtcMs: Date.now()\n\t\t\t\t}).catch((error) => {\n\t\t\t\t\tthis.logger.error(\"Failed to update inbox cursor for @ChannelId! This is unlikely to cause a problem, but not-optimal: @Error\", this.channelId, error);\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t}\n\t/**\n\t* Shuts down the channel and prevents further operations.\n\t*/\n\tshutdown() {\n\t\tthis.abortController.abort();\n\t\tthis.bufferedOutbox.flush();\n\t\tthis.isShutdown = true;\n\t\tthis.pollTimer.stop();\n\t\tif (this.pushRetryTimer) {\n\t\t\tclearTimeout(this.pushRetryTimer);\n\t\t\tthis.pushRetryTimer = null;\n\t\t}\n\t\tthis.transitionConnectionState(\"disconnected\");\n\t\treturn Promise.resolve();\n\t}\n\tgetConnectionState() {\n\t\treturn {\n\t\t\tstate: this.connectionState,\n\t\t\tfailureCount: this.failureCount,\n\t\t\tlastSuccessUtcMs: this.lastSuccessUtcMs ?? 0,\n\t\t\tlastFailureUtcMs: this.lastFailureUtcMs ?? 0,\n\t\t\tpushBlocked: this.pushBlocked,\n\t\t\tpushFailureCount: this.pushFailureCount,\n\t\t\treceivingPages: this.receivingPages\n\t\t};\n\t}\n\tonConnectionStateChange(callback) {\n\t\tthis.connectionStateCallbacks.add(callback);\n\t\treturn () => {\n\t\t\tthis.connectionStateCallbacks.delete(callback);\n\t\t};\n\t}\n\ttriggerPull() {\n\t\tif (this.isShutdown) return;\n\t\tthis.pollTimer.triggerNow();\n\t}\n\t/**\n\t* Initializes the channel by registering it on the remote server and starting polling.\n\t*/\n\tasync init() {\n\t\tconst { ackOrdinal } = await this.touchRemoteChannel();\n\t\tconst cursors = await this.cursorStorage.list(this.remoteName);\n\t\tconst inboxOrdinal = cursors.find((c) => c.cursorType === \"inbox\")?.cursorOrdinal ?? 0;\n\t\tconst outboxOrdinal = cursors.find((c) => c.cursorType === \"outbox\")?.cursorOrdinal ?? 0;\n\t\tthis.inbox.init(inboxOrdinal);\n\t\tthis.outbox.init(outboxOrdinal);\n\t\tthis.lastPersistedInboxOrdinal = inboxOrdinal;\n\t\tthis.lastPersistedOutboxOrdinal = outboxOrdinal;\n\t\tif (ackOrdinal > 0) trimMailboxFromAckOrdinal(this.outbox, ackOrdinal);\n\t\tthis.pollTimer.setDelegate(() => this.poll());\n\t\tthis.pollTimer.start();\n\t\tthis.transitionConnectionState(\"connected\");\n\t}\n\ttransitionConnectionState(next) {\n\t\tif (this.connectionState === next) return;\n\t\tthis.connectionState = next;\n\t\tconst snapshot = this.getConnectionState();\n\t\tfor (const callback of this.connectionStateCallbacks) try {\n\t\t\tcallback(snapshot);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Connection state change callback error: @Error\", error);\n\t\t}\n\t}\n\t/**\n\t* Polls the remote for new sync envelopes.\n\t*/\n\tasync poll() {\n\t\tif (this.isShutdown) return;\n\t\tlet response;\n\t\ttry {\n\t\t\tresponse = await this.pollSyncEnvelopes(this.inbox.ackOrdinal, this.inbox.latestOrdinal);\n\t\t} catch (error) {\n\t\t\tif (!this.handlePollError(error)) throw error;\n\t\t\treturn;\n\t\t}\n\t\tconst { envelopes, ackOrdinal, deadLetters, hasMore } = response;\n\t\tif (ackOrdinal > 0) trimMailboxFromAckOrdinal(this.outbox, ackOrdinal);\n\t\tconst allSyncOps = [];\n\t\tfor (const envelope of envelopes) if (envelope.type.toLowerCase() === \"operations\" && envelope.operations) {\n\t\t\tconst syncOps = envelopesToSyncOperations(envelope, this.remoteName);\n\t\t\tfor (const syncOp of syncOps) syncOp.transported();\n\t\t\tallSyncOps.push(...syncOps);\n\t\t}\n\t\tconst consolidated = allSyncOps.length > 1 ? consolidateSyncOperations(allSyncOps) : allSyncOps;\n\t\tif (consolidated.length > 0) this.inbox.add(...consolidated);\n\t\tif (deadLetters.length > 0) this.handleRemoteDeadLetters(deadLetters);\n\t\tif (hasMore) this.receivingPages = true;\n\t\telse if (this.receivingPages) {\n\t\t\tthis.receivingPages = false;\n\t\t\tthis.drainOutbox();\n\t\t}\n\t\tthis.lastSuccessUtcMs = Date.now();\n\t\tthis.failureCount = 0;\n\t\tthis.transitionConnectionState(\"connected\");\n\t}\n\t/**\n\t* Handles dead letters reported by the remote server.\n\t* Creates local dead letter SyncOperations so the channel quiesces.\n\t*/\n\thandleRemoteDeadLetters(deadLetters) {\n\t\tfor (const dl of deadLetters) this.logger.error(\"Remote dead letter on @ChannelId: document @DocumentId failed with: @Error\", this.channelId, dl.documentId, dl.error);\n\t\tconst syncOps = [];\n\t\tfor (const dl of deadLetters) {\n\t\t\tconst syncOp = new SyncOperation(crypto.randomUUID(), dl.jobId, [], this.remoteName, dl.documentId, dl.scopes, dl.branch, []);\n\t\t\tsyncOp.failed(new ChannelError(ChannelErrorSource.Outbox, new Error(dl.error)));\n\t\t\tsyncOps.push(syncOp);\n\t\t}\n\t\tthis.deadLetter.add(...syncOps);\n\t}\n\t/**\n\t* Handles polling errors with error classification.\n\t* Returns true if the error was handled (caller should not rethrow).\n\t*/\n\thandlePollError(error) {\n\t\tif (this.isShutdown) return true;\n\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\tif (err.message.includes(\"Channel not found\")) {\n\t\t\tthis.transitionConnectionState(\"reconnecting\");\n\t\t\tthis.recoverFromChannelNotFound();\n\t\t\treturn true;\n\t\t}\n\t\tconst classification = this.classifyError(err);\n\t\tthis.failureCount++;\n\t\tthis.lastFailureUtcMs = Date.now();\n\t\tconst channelError = new ChannelError(ChannelErrorSource.Inbox, err);\n\t\tthis.logger.error(\"GqlChannel poll error (@FailureCount, @Classification): @Error\", this.failureCount, classification, channelError);\n\t\tif (classification === \"unrecoverable\") {\n\t\t\tthis.pollTimer.stop();\n\t\t\tthis.transitionConnectionState(\"error\");\n\t\t\treturn true;\n\t\t}\n\t\tthis.transitionConnectionState(\"error\");\n\t\treturn false;\n\t}\n\t/**\n\t* Recovers from a \"Channel not found\" error by re-registering and restarting polling.\n\t* Self-retries with backoff instead of restarting the poll timer on failure.\n\t*/\n\trecoverFromChannelNotFound() {\n\t\tthis.logger.info(\"GqlChannel @ChannelId not found on remote, re-registering...\", this.channelId);\n\t\tthis.pollTimer.stop();\n\t\tconst attemptRecovery = (attempt) => {\n\t\t\tif (this.isShutdown) return;\n\t\t\tthis.touchRemoteChannel().then(({ ackOrdinal }) => {\n\t\t\t\tthis.logger.info(\"GqlChannel @ChannelId re-registered successfully\", this.channelId);\n\t\t\t\tthis.failureCount = 0;\n\t\t\t\tif (ackOrdinal > 0) trimMailboxFromAckOrdinal(this.outbox, ackOrdinal);\n\t\t\t\tthis.pollTimer.start();\n\t\t\t\tthis.transitionConnectionState(\"connected\");\n\t\t\t}).catch((recoveryError) => {\n\t\t\t\tconst err = recoveryError instanceof Error ? recoveryError : new Error(String(recoveryError));\n\t\t\t\tconst classification = this.classifyError(err);\n\t\t\t\tthis.logger.error(\"GqlChannel @ChannelId recovery attempt @Attempt failed (@Classification): @Error\", this.channelId, attempt, classification, recoveryError);\n\t\t\t\tthis.failureCount++;\n\t\t\t\tthis.lastFailureUtcMs = Date.now();\n\t\t\t\tif (classification === \"unrecoverable\") {\n\t\t\t\t\tthis.transitionConnectionState(\"error\");\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tthis.transitionConnectionState(\"reconnecting\");\n\t\t\t\tconst delay = calculateBackoffDelay(attempt, this.config.retryBaseDelayMs, this.config.retryMaxDelayMs, Math.random());\n\t\t\t\tsetTimeout(() => attemptRecovery(attempt + 1), delay);\n\t\t\t});\n\t\t};\n\t\tattemptRecovery(1);\n\t}\n\t/**\n\t* Queries the remote GraphQL endpoint for sync envelopes.\n\t*/\n\tasync pollSyncEnvelopes(ackOrdinal, latestOrdinal) {\n\t\tconst query = `\n query PollSyncEnvelopes($channelId: String!, $outboxAck: Int!, $outboxLatest: Int!) {\n pollSyncEnvelopes(channelId: $channelId, outboxAck: $outboxAck, outboxLatest: $outboxLatest) {\n envelopes {\n type\n channelMeta {\n id\n }\n operations {\n operation {\n index\n timestampUtcMs\n hash\n skip\n error\n id\n action {\n id\n type\n timestampUtcMs\n input\n scope\n context {\n signer {\n user {\n address\n networkId\n chainId\n }\n app {\n name\n key\n }\n signatures\n }\n }\n }\n }\n context {\n documentId\n documentType\n scope\n branch\n ordinal\n }\n }\n cursor {\n remoteName\n cursorOrdinal\n lastSyncedAtUtcMs\n }\n key\n dependsOn\n }\n ackOrdinal\n deadLetters {\n documentId\n error\n jobId\n branch\n scopes\n operationCount\n }\n hasMore\n }\n }\n `;\n\t\tconst variables = {\n\t\t\tchannelId: this.channelId,\n\t\t\toutboxAck: ackOrdinal,\n\t\t\toutboxLatest: latestOrdinal\n\t\t};\n\t\tconst response = await this.executeGraphQL(query, variables);\n\t\treturn {\n\t\t\tenvelopes: response.pollSyncEnvelopes.envelopes,\n\t\t\tackOrdinal: response.pollSyncEnvelopes.ackOrdinal,\n\t\t\tdeadLetters: response.pollSyncEnvelopes.deadLetters ?? [],\n\t\t\thasMore: response.pollSyncEnvelopes.hasMore\n\t\t};\n\t}\n\t/**\n\t* Registers or updates this channel on the remote server via GraphQL mutation.\n\t* Returns the remote's ack ordinal so the client can trim its outbox.\n\t*/\n\tasync touchRemoteChannel() {\n\t\tlet sinceTimestampUtcMs = \"0\";\n\t\ttry {\n\t\t\tconst result = await this.operationIndex.getLatestTimestampForCollection(this.config.collectionId);\n\t\t\tif (result) sinceTimestampUtcMs = result;\n\t\t} catch {}\n\t\tconst mutation = `\n mutation TouchChannel($input: TouchChannelInput!) {\n touchChannel(input: $input) {\n success\n ackOrdinal\n }\n }\n `;\n\t\tconst variables = { input: {\n\t\t\tid: this.channelId,\n\t\t\tname: this.channelId,\n\t\t\tcollectionId: this.config.collectionId,\n\t\t\tfilter: {\n\t\t\t\tdocumentId: this.config.filter.documentId,\n\t\t\t\tscope: this.config.filter.scope,\n\t\t\t\tbranch: this.config.filter.branch\n\t\t\t},\n\t\t\tsinceTimestampUtcMs\n\t\t} };\n\t\tconst data = await this.executeGraphQL(mutation, variables);\n\t\tif (!data.touchChannel.success) throw new GraphQLRequestError(\"touchChannel returned success=false\", \"graphql\");\n\t\treturn { ackOrdinal: data.touchChannel.ackOrdinal };\n\t}\n\t/**\n\t* Fire-and-forget push with retry on recoverable errors.\n\t* On success, clears push blocked state. On recoverable error, blocks\n\t* further pushes and schedules a retry. On unrecoverable error, moves\n\t* ops to deadLetter.\n\t*/\n\tattemptPush(syncOps) {\n\t\tthis.isPushing = true;\n\t\tthis.pushSyncOperations(syncOps).then(() => {\n\t\t\tthis.isPushing = false;\n\t\t\tthis.pushBlocked = false;\n\t\t\tthis.pushFailureCount = 0;\n\t\t\tif (this.connectionState === \"reconnecting\" || this.connectionState === \"error\") this.transitionConnectionState(\"connected\");\n\t\t\tthis.drainOutbox();\n\t\t}).catch((error) => {\n\t\t\tthis.isPushing = false;\n\t\t\tthis.pendingDrain = false;\n\t\t\tif (this.isShutdown) return;\n\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\tif (this.classifyError(err) === \"recoverable\") {\n\t\t\t\tthis.pushFailureCount++;\n\t\t\t\tthis.pushBlocked = true;\n\t\t\t\tthis.logger.error(\"GqlChannel push failed (attempt @FailureCount), will retry: @Error\", this.pushFailureCount, err);\n\t\t\t\tthis.transitionConnectionState(\"reconnecting\");\n\t\t\t\tthis.schedulePushRetry();\n\t\t\t} else {\n\t\t\t\tconst channelError = new ChannelError(ChannelErrorSource.Outbox, err);\n\t\t\t\tfor (const syncOp of syncOps) syncOp.failed(channelError);\n\t\t\t\tthis.deadLetter.add(...syncOps);\n\t\t\t\tthis.outbox.remove(...syncOps);\n\t\t\t\tthis.transitionConnectionState(\"error\");\n\t\t\t}\n\t\t});\n\t}\n\t/**\n\t* Schedules a retry of all current outbox items using exponential backoff.\n\t*/\n\tschedulePushRetry() {\n\t\tif (this.pushRetryTimer) return;\n\t\tconst delay = calculateBackoffDelay(this.pushFailureCount, this.config.retryBaseDelayMs, this.config.retryMaxDelayMs, Math.random());\n\t\tthis.pushRetryTimer = setTimeout(() => {\n\t\t\tthis.pushRetryTimer = null;\n\t\t\tif (this.isShutdown) return;\n\t\t\tconst allItems = this.outbox.items;\n\t\t\tif (allItems.length === 0) {\n\t\t\t\tthis.pushBlocked = false;\n\t\t\t\tthis.pushFailureCount = 0;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthis.attemptPush([...allItems]);\n\t\t}, delay);\n\t}\n\t/**\n\t* Drains pending outbox items that arrived while a push was in-flight.\n\t* Server-side action.id dedup handles any overlap with the previous push.\n\t*/\n\tdrainOutbox() {\n\t\tif (!this.pendingDrain) return;\n\t\tthis.pendingDrain = false;\n\t\tif (this.isShutdown) return;\n\t\tconst items = this.outbox.items;\n\t\tif (items.length === 0) return;\n\t\tthis.attemptPush([...items]);\n\t}\n\t/**\n\t* Classifies an error as recoverable or unrecoverable based on its type.\n\t* Recoverable errors are transient and worth retrying (network, 5xx, parse).\n\t* Unrecoverable errors will not self-heal (auth, client errors, GraphQL rejections).\n\t*/\n\tclassifyError(error) {\n\t\tif (!(error instanceof GraphQLRequestError)) return \"recoverable\";\n\t\tswitch (error.category) {\n\t\t\tcase \"network\": return \"recoverable\";\n\t\t\tcase \"http\":\n\t\t\t\tif (error.statusCode !== void 0 && error.statusCode >= 500) return \"recoverable\";\n\t\t\t\treturn \"unrecoverable\";\n\t\t\tcase \"parse\": return \"recoverable\";\n\t\t\tcase \"graphql\": return \"unrecoverable\";\n\t\t\tcase \"missing-data\": return \"unrecoverable\";\n\t\t}\n\t}\n\t/**\n\t* Pushes multiple sync operations to the remote via a single GraphQL mutation.\n\t* Creates one SyncEnvelope per SyncOperation with key/dependsOn for batch ordering.\n\t*/\n\tasync pushSyncOperations(syncOps) {\n\t\tfor (const syncOp of syncOps) syncOp.started();\n\t\tconst jobIdToKeys = /* @__PURE__ */ new Map();\n\t\tconst envelopes = [];\n\t\tfor (let i = 0; i < syncOps.length; i++) {\n\t\t\tconst syncOp = syncOps[i];\n\t\t\tconst key = String(i);\n\t\t\tif (syncOp.jobId) {\n\t\t\t\tif (!jobIdToKeys.has(syncOp.jobId)) jobIdToKeys.set(syncOp.jobId, []);\n\t\t\t\tjobIdToKeys.get(syncOp.jobId).push(key);\n\t\t\t}\n\t\t\tconst dependsOn = [];\n\t\t\tfor (const dep of syncOp.jobDependencies) {\n\t\t\t\tconst depKeys = jobIdToKeys.get(dep);\n\t\t\t\tif (depKeys) dependsOn.push(...depKeys);\n\t\t\t}\n\t\t\tthis.logger.debug(\"[PUSH]: @Operations\", syncOp.operations.map((op) => `(${op.context.documentId}, ${op.context.branch}, ${op.context.scope}, ${op.operation.index})`));\n\t\t\tenvelopes.push({\n\t\t\t\ttype: \"operations\",\n\t\t\t\tchannelMeta: { id: this.channelId },\n\t\t\t\toperations: syncOp.operations,\n\t\t\t\tkey,\n\t\t\t\tdependsOn\n\t\t\t});\n\t\t}\n\t\tconst mutation = `\n mutation PushSyncEnvelopes($envelopes: [SyncEnvelopeInput!]!) {\n pushSyncEnvelopes(envelopes: $envelopes)\n }\n `;\n\t\tconst variables = { envelopes: envelopes.map((e) => serializeEnvelope(e)) };\n\t\tawait this.executeGraphQL(mutation, variables);\n\t}\n\t/**\n\t* Gets the authorization header value using jwtHandler.\n\t*/\n\tasync getAuthorizationHeader() {\n\t\tif (!this.config.jwtHandler) return;\n\t\ttry {\n\t\t\tconst token = await this.config.jwtHandler(this.config.url);\n\t\t\tif (token) return `Bearer ${token}`;\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"JWT handler failed: @Error\", error);\n\t\t}\n\t}\n\t/**\n\t* Executes a GraphQL query or mutation against the remote endpoint.\n\t*/\n\tasync executeGraphQL(query, variables) {\n\t\tconst headers = { \"Content-Type\": \"application/json\" };\n\t\tconst authHeader = await this.getAuthorizationHeader();\n\t\tif (authHeader) headers[\"Authorization\"] = authHeader;\n\t\tconst operationName = query.match(/(?:query|mutation)\\s+(\\w+)/)?.[1] ?? \"unknown\";\n\t\tthis.logger.verbose(\"GQL request @channelId @operation @url vars=@variables\", this.channelId, operationName, this.config.url, JSON.stringify(variables));\n\t\tconst fetchFn = this.config.fetchFn ?? fetch;\n\t\tlet response;\n\t\ttry {\n\t\t\tresponse = await fetchFn(this.config.url, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders,\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tquery,\n\t\t\t\t\tvariables\n\t\t\t\t}),\n\t\t\t\tsignal: this.abortController.signal\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tthrow new GraphQLRequestError(`GraphQL request failed: ${error instanceof Error ? error.message : String(error)}`, \"network\");\n\t\t}\n\t\tif (!response.ok) throw new GraphQLRequestError(`GraphQL request failed: ${response.status} ${response.statusText}`, \"http\", response.status);\n\t\tlet result;\n\t\ttry {\n\t\t\tresult = await response.json();\n\t\t} catch (error) {\n\t\t\tthrow new GraphQLRequestError(`Failed to parse GraphQL response: ${error instanceof Error ? error.message : String(error)}`, \"parse\");\n\t\t}\n\t\tthis.logger.verbose(\"GQL response @channelId @operation status=@status data=@data errors=@errors\", this.channelId, operationName, response.status, JSON.stringify(result.data), result.errors ? JSON.stringify(result.errors) : \"none\");\n\t\tif (result.errors) throw new GraphQLRequestError(`GraphQL errors: ${JSON.stringify(result.errors, null, 2)}`, \"graphql\");\n\t\tif (!result.data) throw new GraphQLRequestError(\"GraphQL response missing data field\", \"missing-data\");\n\t\treturn result.data;\n\t}\n\tget poller() {\n\t\treturn this.pollTimer;\n\t}\n};\n//#endregion\n//#region src/sync/channels/gql-request-channel-factory.ts\n/**\n* Factory for creating GqlRequestChannel instances.\n*\n* Extracts GraphQL-specific configuration from ChannelConfig.parameters and\n* instantiates GqlRequestChannel instances for network-based synchronization.\n*\n* The optional jwtHandler enables dynamic JWT token generation per-request,\n* which is useful for short-lived tokens with audience-specific claims.\n*/\nvar GqlRequestChannelFactory = class {\n\tlogger;\n\tjwtHandler;\n\tqueue;\n\tconstructor(logger, jwtHandler, queue) {\n\t\tthis.logger = logger;\n\t\tthis.jwtHandler = jwtHandler;\n\t\tthis.queue = queue;\n\t}\n\t/**\n\t* Creates a new GqlRequestChannel instance with the given configuration.\n\t* See GqlChannelConfig for the expected parameters.\n\t*\n\t* @param config - Channel configuration including type and parameters\n\t* @param cursorStorage - Storage for persisting synchronization cursors\n\t* @param operationIndex - Operation index for querying timestamps\n\t* @returns A new GqlRequestChannel instance\n\t*/\n\tinstance(remoteId, remoteName, config, cursorStorage, collectionId, filter, operationIndex, options) {\n\t\tconst url = config.parameters.url;\n\t\tif (typeof url !== \"string\" || !url) throw new Error(\"GqlRequestChannelFactory requires \\\"url\\\" parameter in config.parameters\");\n\t\tconst gqlConfig = {\n\t\t\turl,\n\t\t\tcollectionId,\n\t\t\tfilter,\n\t\t\tjwtHandler: this.jwtHandler,\n\t\t\tretryBaseDelayMs: 1e3,\n\t\t\tretryMaxDelayMs: 3e5\n\t\t};\n\t\tlet pollIntervalMs = 2e3;\n\t\tif (config.parameters.pollIntervalMs !== void 0) {\n\t\t\tif (typeof config.parameters.pollIntervalMs !== \"number\") throw new Error(\"\\\"pollIntervalMs\\\" parameter must be a number\");\n\t\t\tpollIntervalMs = config.parameters.pollIntervalMs;\n\t\t}\n\t\tlet retryBaseDelayMs;\n\t\tif (config.parameters.retryBaseDelayMs !== void 0) {\n\t\t\tif (typeof config.parameters.retryBaseDelayMs !== \"number\") throw new Error(\"\\\"retryBaseDelayMs\\\" parameter must be a number\");\n\t\t\tretryBaseDelayMs = config.parameters.retryBaseDelayMs;\n\t\t}\n\t\tlet retryMaxDelayMs;\n\t\tif (config.parameters.retryMaxDelayMs !== void 0) {\n\t\t\tif (typeof config.parameters.retryMaxDelayMs !== \"number\") throw new Error(\"\\\"retryMaxDelayMs\\\" parameter must be a number\");\n\t\t\tretryMaxDelayMs = config.parameters.retryMaxDelayMs;\n\t\t}\n\t\tif (config.parameters.fetchFn !== void 0) {\n\t\t\tif (typeof config.parameters.fetchFn !== \"function\") throw new Error(\"\\\"fetchFn\\\" parameter must be a function\");\n\t\t\tgqlConfig.fetchFn = config.parameters.fetchFn;\n\t\t}\n\t\tif (retryBaseDelayMs !== void 0) gqlConfig.retryBaseDelayMs = retryBaseDelayMs;\n\t\tif (retryMaxDelayMs !== void 0) gqlConfig.retryMaxDelayMs = retryMaxDelayMs;\n\t\tlet maxQueueDepth;\n\t\tif (config.parameters.maxQueueDepth !== void 0) {\n\t\t\tif (typeof config.parameters.maxQueueDepth !== \"number\") throw new Error(\"\\\"maxQueueDepth\\\" parameter must be a number\");\n\t\t\tmaxQueueDepth = config.parameters.maxQueueDepth;\n\t\t}\n\t\tlet backpressureCheckIntervalMs;\n\t\tif (config.parameters.backpressureCheckIntervalMs !== void 0) {\n\t\t\tif (typeof config.parameters.backpressureCheckIntervalMs !== \"number\") throw new Error(\"\\\"backpressureCheckIntervalMs\\\" parameter must be a number\");\n\t\t\tbackpressureCheckIntervalMs = config.parameters.backpressureCheckIntervalMs;\n\t\t}\n\t\tconst pollTimer = new IntervalPollTimer(this.queue, {\n\t\t\tintervalMs: pollIntervalMs,\n\t\t\t...retryBaseDelayMs !== void 0 && { retryBaseDelayMs },\n\t\t\t...retryMaxDelayMs !== void 0 && { retryMaxDelayMs },\n\t\t\t...maxQueueDepth !== void 0 && { maxQueueDepth },\n\t\t\t...backpressureCheckIntervalMs !== void 0 && { backpressureCheckIntervalMs },\n\t\t\tstartPaused: options?.pollBehavior === PollBehavior.Manual\n\t\t});\n\t\treturn new GqlRequestChannel(this.logger, remoteId, remoteName, cursorStorage, gqlConfig, operationIndex, pollTimer);\n\t}\n};\n//#endregion\n//#region src/sync/channels/gql-res-channel.ts\n/**\n* This class is used server-side to accumulate inbox + outbox operations.\n*\n* In general, the resolvers are responsible for updating mailboxes.\n*/\nvar GqlResponseChannel = class {\n\tinbox;\n\toutbox;\n\tdeadLetter;\n\tchannelId;\n\tremoteName;\n\tcursorStorage;\n\tisShutdown;\n\tlastPersistedInboxOrdinal = 0;\n\tlastPersistedOutboxOrdinal = 0;\n\tconnectionState = \"connecting\";\n\tconnectionStateCallbacks = /* @__PURE__ */ new Set();\n\tconstructor(logger, channelId, remoteName, cursorStorage) {\n\t\tthis.logger = logger;\n\t\tthis.channelId = channelId;\n\t\tthis.remoteName = remoteName;\n\t\tthis.cursorStorage = cursorStorage;\n\t\tthis.isShutdown = false;\n\t\tthis.inbox = new Mailbox();\n\t\tthis.outbox = new Mailbox();\n\t\tthis.deadLetter = new Mailbox();\n\t\tthis.outbox.onRemoved((syncOps) => {\n\t\t\tconst maxOrdinal = getLatestAppliedOrdinal(syncOps);\n\t\t\tif (maxOrdinal > this.lastPersistedOutboxOrdinal) {\n\t\t\t\tthis.lastPersistedOutboxOrdinal = maxOrdinal;\n\t\t\t\tthis.cursorStorage.upsert({\n\t\t\t\t\tremoteName: this.remoteName,\n\t\t\t\t\tcursorType: \"outbox\",\n\t\t\t\t\tcursorOrdinal: maxOrdinal,\n\t\t\t\t\tlastSyncedAtUtcMs: Date.now()\n\t\t\t\t}).catch((error) => {\n\t\t\t\t\tthis.logger.error(\"Failed to update outbox cursor for @ChannelId! This means that future application runs may resend duplicate operations. This is recoverable (with deduplication protection), but not-optimal: @Error\", this.channelId, error);\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t\tthis.inbox.onRemoved((syncOps) => {\n\t\t\tconst maxOrdinal = getLatestAppliedOrdinal(syncOps);\n\t\t\tif (maxOrdinal > this.lastPersistedInboxOrdinal) {\n\t\t\t\tthis.lastPersistedInboxOrdinal = maxOrdinal;\n\t\t\t\tthis.cursorStorage.upsert({\n\t\t\t\t\tremoteName: this.remoteName,\n\t\t\t\t\tcursorType: \"inbox\",\n\t\t\t\t\tcursorOrdinal: maxOrdinal,\n\t\t\t\t\tlastSyncedAtUtcMs: Date.now()\n\t\t\t\t}).catch((error) => {\n\t\t\t\t\tthis.logger.error(\"Failed to update inbox cursor for @ChannelId! This is unlikely to cause a problem, but not-optimal: @Error\", this.channelId, error);\n\t\t\t\t});\n\t\t\t}\n\t\t});\n\t}\n\tshutdown() {\n\t\tthis.isShutdown = true;\n\t\tthis.transitionConnectionState(\"disconnected\");\n\t\treturn Promise.resolve();\n\t}\n\tgetConnectionState() {\n\t\treturn {\n\t\t\tstate: this.connectionState,\n\t\t\tfailureCount: 0,\n\t\t\tlastSuccessUtcMs: 0,\n\t\t\tlastFailureUtcMs: 0,\n\t\t\tpushBlocked: false,\n\t\t\tpushFailureCount: 0,\n\t\t\treceivingPages: false\n\t\t};\n\t}\n\tonConnectionStateChange(callback) {\n\t\tthis.connectionStateCallbacks.add(callback);\n\t\treturn () => {\n\t\t\tthis.connectionStateCallbacks.delete(callback);\n\t\t};\n\t}\n\t/** Response channels are push-driven; resolvers populate mailboxes directly. */\n\ttriggerPull() {}\n\ttransitionConnectionState(next) {\n\t\tif (this.connectionState === next) return;\n\t\tthis.connectionState = next;\n\t\tconst snapshot = this.getConnectionState();\n\t\tfor (const callback of this.connectionStateCallbacks) try {\n\t\t\tcallback(snapshot);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Connection state change callback error: @Error\", error);\n\t\t}\n\t}\n\tasync init() {\n\t\tconst cursors = await this.cursorStorage.list(this.remoteName);\n\t\tconst inboxOrdinal = cursors.find((c) => c.cursorType === \"inbox\")?.cursorOrdinal ?? 0;\n\t\tconst outboxOrdinal = cursors.find((c) => c.cursorType === \"outbox\")?.cursorOrdinal ?? 0;\n\t\tthis.inbox.init(inboxOrdinal);\n\t\tthis.outbox.init(outboxOrdinal);\n\t\tthis.lastPersistedInboxOrdinal = inboxOrdinal;\n\t\tthis.lastPersistedOutboxOrdinal = outboxOrdinal;\n\t\tthis.transitionConnectionState(\"connected\");\n\t}\n};\n//#endregion\n//#region src/sync/channels/gql-response-channel-factory.ts\n/**\n* Factory for creating GqlResponseChannel instances.\n*/\nvar GqlResponseChannelFactory = class {\n\tlogger;\n\tconstructor(logger) {\n\t\tthis.logger = logger;\n\t}\n\tinstance(remoteId, remoteName, config, cursorStorage) {\n\t\treturn new GqlResponseChannel(this.logger, remoteId, remoteName, cursorStorage);\n\t}\n};\n//#endregion\n//#region src/storage/kysely/sync-cursor-storage.ts\nfunction rowToRemoteCursor(row) {\n\treturn {\n\t\tremoteName: row.remote_name,\n\t\tcursorType: row.cursor_type,\n\t\tcursorOrdinal: Number(row.cursor_ordinal),\n\t\tlastSyncedAtUtcMs: row.last_synced_at_utc_ms ? new Date(row.last_synced_at_utc_ms).getTime() : void 0\n\t};\n}\nfunction remoteCursorToRow(cursor) {\n\treturn {\n\t\tremote_name: cursor.remoteName,\n\t\tcursor_type: cursor.cursorType,\n\t\tcursor_ordinal: BigInt(cursor.cursorOrdinal),\n\t\tlast_synced_at_utc_ms: cursor.lastSyncedAtUtcMs ? new Date(cursor.lastSyncedAtUtcMs).toISOString() : null\n\t};\n}\nvar KyselySyncCursorStorage = class {\n\tconstructor(db) {\n\t\tthis.db = db;\n\t}\n\tasync list(remoteName, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst rows = await this.db.selectFrom(\"sync_cursors\").selectAll().where(\"remote_name\", \"=\", remoteName).execute();\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\treturn rows.map(rowToRemoteCursor);\n\t}\n\tasync get(remoteName, cursorType, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst row = await this.db.selectFrom(\"sync_cursors\").selectAll().where(\"remote_name\", \"=\", remoteName).where(\"cursor_type\", \"=\", cursorType).executeTakeFirst();\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tif (!row) return {\n\t\t\tremoteName,\n\t\t\tcursorType,\n\t\t\tcursorOrdinal: 0\n\t\t};\n\t\treturn rowToRemoteCursor(row);\n\t}\n\tasync upsert(cursor, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tawait this.db.transaction().execute(async (trx) => {\n\t\t\tconst insertable = remoteCursorToRow(cursor);\n\t\t\tawait trx.insertInto(\"sync_cursors\").values(insertable).onConflict((oc) => oc.columns([\"remote_name\", \"cursor_type\"]).doUpdateSet({\n\t\t\t\t...insertable,\n\t\t\t\tupdated_at: sql`NOW()`\n\t\t\t})).execute();\n\t\t});\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t}\n\tasync remove(remoteName, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tawait this.db.transaction().execute(async (trx) => {\n\t\t\tawait trx.deleteFrom(\"sync_cursors\").where(\"remote_name\", \"=\", remoteName).execute();\n\t\t});\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t}\n};\n//#endregion\n//#region src/storage/kysely/sync-dead-letter-storage.ts\nfunction rowToDeadLetterRecord(row) {\n\treturn {\n\t\tid: row.id,\n\t\tjobId: row.job_id,\n\t\tjobDependencies: row.job_dependencies,\n\t\tremoteName: row.remote_name,\n\t\tdocumentId: row.document_id,\n\t\tscopes: row.scopes,\n\t\tbranch: row.branch,\n\t\toperations: row.operations,\n\t\terrorSource: row.error_source,\n\t\terrorMessage: row.error_message\n\t};\n}\nfunction deadLetterRecordToRow(record) {\n\treturn {\n\t\tid: record.id,\n\t\tjob_id: record.jobId,\n\t\tjob_dependencies: JSON.stringify(record.jobDependencies),\n\t\tremote_name: record.remoteName,\n\t\tdocument_id: record.documentId,\n\t\tscopes: JSON.stringify(record.scopes),\n\t\tbranch: record.branch,\n\t\toperations: JSON.stringify(record.operations),\n\t\terror_source: record.errorSource,\n\t\terror_message: record.errorMessage\n\t};\n}\n/**\n* PGlite/Kysely-backed implementation of {@link ISyncDeadLetterStorage}.\n*/\nvar KyselySyncDeadLetterStorage = class {\n\tconstructor(db) {\n\t\tthis.db = db;\n\t}\n\tasync list(remoteName, paging, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst startIndex = paging?.cursor ? parseInt(paging.cursor) : 0;\n\t\tconst limit = paging?.limit || 100;\n\t\tconst rows = await this.db.selectFrom(\"sync_dead_letters\").selectAll().where(\"remote_name\", \"=\", remoteName).orderBy(\"ordinal\", \"desc\").offset(startIndex).limit(limit + 1).execute();\n\t\tlet hasMore = false;\n\t\tlet items = rows;\n\t\tif (paging?.limit && rows.length > limit) {\n\t\t\thasMore = true;\n\t\t\titems = rows.slice(0, limit);\n\t\t}\n\t\tconst nextCursor = hasMore ? String(startIndex + limit) : void 0;\n\t\tconst cursor = paging?.cursor || \"0\";\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\treturn {\n\t\t\tresults: items.map(rowToDeadLetterRecord),\n\t\t\toptions: {\n\t\t\t\tcursor,\n\t\t\t\tlimit\n\t\t\t},\n\t\t\tnextCursor\n\t\t};\n\t}\n\tasync add(deadLetter, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tawait this.db.transaction().execute(async (trx) => {\n\t\t\tconst insertable = deadLetterRecordToRow(deadLetter);\n\t\t\tawait trx.insertInto(\"sync_dead_letters\").values(insertable).onConflict((oc) => oc.column(\"id\").doNothing()).execute();\n\t\t});\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t}\n\tasync remove(id, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tawait this.db.transaction().execute(async (trx) => {\n\t\t\tawait trx.deleteFrom(\"sync_dead_letters\").where(\"id\", \"=\", id).execute();\n\t\t});\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t}\n\tasync removeByRemote(remoteName, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tawait this.db.transaction().execute(async (trx) => {\n\t\t\tawait trx.deleteFrom(\"sync_dead_letters\").where(\"remote_name\", \"=\", remoteName).execute();\n\t\t});\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t}\n\tasync listQuarantinedDocumentIds(signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst rows = await this.db.selectFrom(\"sync_dead_letters\").select(\"document_id\").distinct().execute();\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\treturn rows.map((row) => row.document_id);\n\t}\n};\n//#endregion\n//#region src/storage/kysely/sync-remote-storage.ts\nfunction rowToRemoteRecord(row) {\n\treturn {\n\t\tid: row.channel_id,\n\t\tname: row.name,\n\t\tcollectionId: row.collection_id,\n\t\tchannelConfig: {\n\t\t\ttype: row.channel_type,\n\t\t\tparameters: row.channel_parameters ?? {}\n\t\t},\n\t\tfilter: {\n\t\t\tdocumentId: row.filter_document_ids ?? [],\n\t\t\tscope: row.filter_scopes ?? [],\n\t\t\tbranch: row.filter_branch\n\t\t},\n\t\toptions: { sinceTimestampUtcMs: \"0\" },\n\t\tstatus: {\n\t\t\tpush: {\n\t\t\t\tstate: row.push_state,\n\t\t\t\tlastSuccessUtcMs: row.push_last_success_utc_ms ? new Date(row.push_last_success_utc_ms).getTime() : void 0,\n\t\t\t\tlastFailureUtcMs: row.push_last_failure_utc_ms ? new Date(row.push_last_failure_utc_ms).getTime() : void 0,\n\t\t\t\tfailureCount: row.push_failure_count\n\t\t\t},\n\t\t\tpull: {\n\t\t\t\tstate: row.pull_state,\n\t\t\t\tlastSuccessUtcMs: row.pull_last_success_utc_ms ? new Date(row.pull_last_success_utc_ms).getTime() : void 0,\n\t\t\t\tlastFailureUtcMs: row.pull_last_failure_utc_ms ? new Date(row.pull_last_failure_utc_ms).getTime() : void 0,\n\t\t\t\tfailureCount: row.pull_failure_count\n\t\t\t}\n\t\t}\n\t};\n}\nfunction remoteRecordToRow(remote) {\n\treturn {\n\t\tname: remote.name,\n\t\tcollection_id: remote.collectionId,\n\t\tchannel_type: remote.channelConfig.type,\n\t\tchannel_id: remote.id,\n\t\tremote_name: remote.name,\n\t\tchannel_parameters: remote.channelConfig.parameters,\n\t\tfilter_document_ids: remote.filter.documentId.length > 0 ? remote.filter.documentId : null,\n\t\tfilter_scopes: remote.filter.scope.length > 0 ? remote.filter.scope : null,\n\t\tfilter_branch: remote.filter.branch,\n\t\tpush_state: remote.status.push.state,\n\t\tpush_last_success_utc_ms: remote.status.push.lastSuccessUtcMs ? new Date(remote.status.push.lastSuccessUtcMs).toISOString() : null,\n\t\tpush_last_failure_utc_ms: remote.status.push.lastFailureUtcMs ? new Date(remote.status.push.lastFailureUtcMs).toISOString() : null,\n\t\tpush_failure_count: remote.status.push.failureCount,\n\t\tpull_state: remote.status.pull.state,\n\t\tpull_last_success_utc_ms: remote.status.pull.lastSuccessUtcMs ? new Date(remote.status.pull.lastSuccessUtcMs).toISOString() : null,\n\t\tpull_last_failure_utc_ms: remote.status.pull.lastFailureUtcMs ? new Date(remote.status.pull.lastFailureUtcMs).toISOString() : null,\n\t\tpull_failure_count: remote.status.pull.failureCount\n\t};\n}\nvar KyselySyncRemoteStorage = class {\n\tconstructor(db) {\n\t\tthis.db = db;\n\t}\n\tasync list(signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst rows = await this.db.selectFrom(\"sync_remotes\").selectAll().execute();\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\treturn rows.map(rowToRemoteRecord);\n\t}\n\tasync get(name, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tconst row = await this.db.selectFrom(\"sync_remotes\").selectAll().where(\"name\", \"=\", name).executeTakeFirst();\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tif (!row) throw new Error(`Remote not found: ${name}`);\n\t\treturn rowToRemoteRecord(row);\n\t}\n\tasync upsert(remote, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tawait this.db.transaction().execute(async (trx) => {\n\t\t\tconst insertable = remoteRecordToRow(remote);\n\t\t\tawait trx.insertInto(\"sync_remotes\").values(insertable).onConflict((oc) => oc.column(\"name\").doUpdateSet({\n\t\t\t\t...insertable,\n\t\t\t\tupdated_at: sql`NOW()`\n\t\t\t})).execute();\n\t\t});\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t}\n\tasync remove(name, signal) {\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t\tawait this.db.transaction().execute(async (trx) => {\n\t\t\tawait trx.deleteFrom(\"sync_remotes\").where(\"name\", \"=\", name).execute();\n\t\t});\n\t\tif (signal?.aborted) throw new Error(\"Operation aborted\");\n\t}\n};\n//#endregion\n//#region src/sync/batch-aggregator.ts\nvar BatchAggregator = class {\n\tlogger;\n\tdriveContainerTypes;\n\tonBatchReady;\n\tqueue = [];\n\tprocessing = false;\n\tpendingBatches = /* @__PURE__ */ new Map();\n\tconstructor(logger, driveContainerTypes, onBatchReady) {\n\t\tthis.logger = logger;\n\t\tthis.driveContainerTypes = driveContainerTypes;\n\t\tthis.onBatchReady = onBatchReady;\n\t}\n\tasync enqueueWriteReady(event) {\n\t\tthis.queue.push(event);\n\t\tawait this.processQueue();\n\t}\n\tasync handleJobFailed(event) {\n\t\tconst batchId = event.job?.meta.batchId;\n\t\tif (!batchId) return;\n\t\tconst pending = this.pendingBatches.get(batchId);\n\t\tif (!pending) return;\n\t\tthis.pendingBatches.delete(batchId);\n\t\tif (pending.events.length > 0) await this.onBatchReady(this.prepareBatch(pending.events));\n\t}\n\tclear() {\n\t\tthis.queue = [];\n\t\tthis.pendingBatches.clear();\n\t}\n\tasync processQueue() {\n\t\tif (this.processing) return;\n\t\tthis.processing = true;\n\t\ttry {\n\t\t\twhile (this.queue.length > 0) {\n\t\t\t\tconst event = this.queue.shift();\n\t\t\t\ttry {\n\t\t\t\t\tawait this.handleWriteReady(event);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\t\t\tthis.logger.error(\"Failed to process write-ready event (@jobId, @error)\", event.jobId, err.message);\n\t\t\t\t}\n\t\t\t}\n\t\t} finally {\n\t\t\tthis.processing = false;\n\t\t}\n\t}\n\tasync handleWriteReady(event) {\n\t\tconst { batchId, batchJobIds } = event.jobMeta;\n\t\tif (batchJobIds.length <= 1) {\n\t\t\tawait this.onBatchReady(this.prepareBatch([event]));\n\t\t\treturn;\n\t\t}\n\t\tlet pending = this.pendingBatches.get(batchId);\n\t\tif (!pending) {\n\t\t\tpending = {\n\t\t\t\texpectedJobIds: new Set(batchJobIds),\n\t\t\t\tarrivedJobIds: /* @__PURE__ */ new Set(),\n\t\t\t\tevents: []\n\t\t\t};\n\t\t\tthis.pendingBatches.set(batchId, pending);\n\t\t}\n\t\tpending.arrivedJobIds.add(event.jobId);\n\t\tpending.events.push(event);\n\t\tif (pending.arrivedJobIds.size >= pending.expectedJobIds.size) {\n\t\t\tthis.pendingBatches.delete(batchId);\n\t\t\tawait this.onBatchReady(this.prepareBatch(pending.events));\n\t\t}\n\t}\n\tprepareBatch(events) {\n\t\tconst collectionMemberships = this.mergeCollectionMemberships(events);\n\t\tconst isBatch = events.length > 1;\n\t\tconst priorJobIds = [];\n\t\tconst entries = [];\n\t\tfor (const event of events) {\n\t\t\tentries.push({\n\t\t\t\tevent,\n\t\t\t\tjobDependencies: isBatch ? [...priorJobIds] : []\n\t\t\t});\n\t\t\tif (isBatch && event.jobId) priorJobIds.push(event.jobId);\n\t\t}\n\t\treturn {\n\t\t\tcollectionMemberships,\n\t\t\tentries\n\t\t};\n\t}\n\tmergeCollectionMemberships(events) {\n\t\tconst mergedMemberships = {};\n\t\tfor (const event of events) {\n\t\t\tif (event.collectionMemberships) for (const [docId, collections] of Object.entries(event.collectionMemberships)) {\n\t\t\t\tif (!(docId in mergedMemberships)) mergedMemberships[docId] = [];\n\t\t\t\tfor (const c of collections) if (!mergedMemberships[docId].includes(c)) mergedMemberships[docId].push(c);\n\t\t\t}\n\t\t\tfor (const op of event.operations) {\n\t\t\t\tconst action = op.operation.action;\n\t\t\t\tif (action.type !== \"ADD_RELATIONSHIP\") continue;\n\t\t\t\tif (!this.driveContainerTypes.has(op.context.documentType)) continue;\n\t\t\t\tconst input = action.input;\n\t\t\t\tif (!input?.sourceId || !input.targetId) continue;\n\t\t\t\tconst collectionId = driveCollectionId(op.context.branch, input.sourceId);\n\t\t\t\tif (!(input.targetId in mergedMemberships)) mergedMemberships[input.targetId] = [];\n\t\t\t\tif (!mergedMemberships[input.targetId].includes(collectionId)) mergedMemberships[input.targetId].push(collectionId);\n\t\t\t}\n\t\t}\n\t\treturn mergedMemberships;\n\t}\n};\n//#endregion\n//#region src/sync/sync-awaiter.ts\n/**\n* Provides a promise-based interface for waiting on sync completion.\n* Subscribes to sync events at construction and tracks completed sync results\n* to provide a fast path for jobs that have already synced.\n*/\nvar SyncAwaiter = class {\n\tcompletedResults = /* @__PURE__ */ new Map();\n\tpendingWaiters = /* @__PURE__ */ new Map();\n\tunsubscribers = [];\n\tisShutdown = false;\n\tconstructor(eventBus) {\n\t\tthis.eventBus = eventBus;\n\t\tthis.subscribeToEvents();\n\t}\n\t/**\n\t* Waits for sync operations for a job to complete.\n\t* Resolves when SYNC_SUCCEEDED is emitted.\n\t* Rejects when SYNC_FAILED is emitted.\n\t*\n\t* @param jobId - The job id to wait for\n\t* @param signal - Optional abort signal\n\t* @returns The sync result\n\t*/\n\twaitForSync(jobId, signal) {\n\t\tif (signal?.aborted) return Promise.reject(/* @__PURE__ */ new Error(\"Operation aborted\"));\n\t\tif (this.isShutdown) return Promise.reject(/* @__PURE__ */ new Error(\"SyncAwaiter is shutdown\"));\n\t\tconst completedResult = this.completedResults.get(jobId);\n\t\tif (completedResult) return Promise.resolve(completedResult);\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst waiter = {\n\t\t\t\tresolve,\n\t\t\t\treject,\n\t\t\t\tsignal\n\t\t\t};\n\t\t\tconst existingWaiters = this.pendingWaiters.get(jobId) || [];\n\t\t\texistingWaiters.push(waiter);\n\t\t\tthis.pendingWaiters.set(jobId, existingWaiters);\n\t\t\tif (signal) {\n\t\t\t\tconst abortHandler = () => {\n\t\t\t\t\tconst waiters = this.pendingWaiters.get(jobId);\n\t\t\t\t\tif (waiters) {\n\t\t\t\t\t\tconst index = waiters.indexOf(waiter);\n\t\t\t\t\t\tif (index !== -1) {\n\t\t\t\t\t\t\twaiters.splice(index, 1);\n\t\t\t\t\t\t\tif (waiters.length === 0) this.pendingWaiters.delete(jobId);\n\t\t\t\t\t\t\twaiter.reject(/* @__PURE__ */ new Error(\"Operation aborted\"));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t};\n\t\t\t\tsignal.addEventListener(\"abort\", abortHandler, { once: true });\n\t\t\t}\n\t\t});\n\t}\n\t/**\n\t* Shuts down the sync awaiter. This will synchronously reject all pending waiters.\n\t*/\n\tshutdown() {\n\t\tthis.isShutdown = true;\n\t\tfor (const unsubscribe of this.unsubscribers) unsubscribe();\n\t\tthis.unsubscribers.length = 0;\n\t\tfor (const [, waiters] of this.pendingWaiters) for (const waiter of waiters) waiter.reject(/* @__PURE__ */ new Error(\"SyncAwaiter shutdown\"));\n\t\tthis.pendingWaiters.clear();\n\t}\n\tsubscribeToEvents() {\n\t\tthis.unsubscribers.push(this.eventBus.subscribe(SyncEventTypes.SYNC_SUCCEEDED, (_type, event) => {\n\t\t\tthis.handleSyncSucceeded(event);\n\t\t}));\n\t\tthis.unsubscribers.push(this.eventBus.subscribe(SyncEventTypes.SYNC_FAILED, (_type, event) => {\n\t\t\tthis.handleSyncFailed(event);\n\t\t}));\n\t}\n\thandleSyncSucceeded(event) {\n\t\tconst result = {\n\t\t\tjobId: event.jobId,\n\t\t\tstatus: \"succeeded\",\n\t\t\tsyncOperationCount: event.syncOperationCount,\n\t\t\tsuccessCount: event.syncOperationCount,\n\t\t\tfailureCount: 0,\n\t\t\terrors: []\n\t\t};\n\t\tthis.completedResults.set(event.jobId, result);\n\t\tthis.resolveWaiters(event.jobId, result);\n\t}\n\thandleSyncFailed(event) {\n\t\tconst result = {\n\t\t\tjobId: event.jobId,\n\t\t\tstatus: \"failed\",\n\t\t\tsyncOperationCount: event.successCount + event.failureCount,\n\t\t\tsuccessCount: event.successCount,\n\t\t\tfailureCount: event.failureCount,\n\t\t\terrors: event.errors\n\t\t};\n\t\tthis.completedResults.set(event.jobId, result);\n\t\tthis.resolveWaiters(event.jobId, result);\n\t}\n\tresolveWaiters(jobId, result) {\n\t\tconst waiters = this.pendingWaiters.get(jobId);\n\t\tif (!waiters || waiters.length === 0) return;\n\t\tthis.pendingWaiters.delete(jobId);\n\t\tfor (const waiter of waiters) if (waiter.signal?.aborted) waiter.reject(/* @__PURE__ */ new Error(\"Operation aborted\"));\n\t\telse waiter.resolve(result);\n\t}\n};\n//#endregion\n//#region src/sync/sync-status-tracker.ts\nlet SyncStatus = /* @__PURE__ */ function(SyncStatus) {\n\tSyncStatus[\"Synced\"] = \"SYNCED\";\n\tSyncStatus[\"Outgoing\"] = \"OUTGOING\";\n\tSyncStatus[\"Incoming\"] = \"INCOMING\";\n\tSyncStatus[\"OutgoingAndIncoming\"] = \"OUTGOING_AND_INCOMING\";\n\tSyncStatus[\"Error\"] = \"ERROR\";\n\treturn SyncStatus;\n}({});\nvar SyncStatusTracker = class {\n\tremotes = /* @__PURE__ */ new Map();\n\tseen = /* @__PURE__ */ new Set();\n\tcallbacks = /* @__PURE__ */ new Set();\n\tgetStatus(documentId) {\n\t\tif (!this.seen.has(documentId)) return;\n\t\tlet totalInbox = 0;\n\t\tlet totalOutbox = 0;\n\t\tlet totalErrors = 0;\n\t\tfor (const documents of this.remotes.values()) {\n\t\t\tconst counts = documents.get(documentId);\n\t\t\tif (counts) {\n\t\t\t\ttotalInbox += counts.inboxCount;\n\t\t\t\ttotalOutbox += counts.outboxCount;\n\t\t\t\ttotalErrors += counts.errorCount;\n\t\t\t}\n\t\t}\n\t\treturn deriveStatus(totalInbox, totalOutbox, totalErrors);\n\t}\n\tonChange(callback) {\n\t\tthis.callbacks.add(callback);\n\t\treturn () => {\n\t\t\tthis.callbacks.delete(callback);\n\t\t};\n\t}\n\ttrackRemote(remoteName, channel) {\n\t\tthis.remotes.set(remoteName, /* @__PURE__ */ new Map());\n\t\tchannel.inbox.onAdded((syncOps) => this.handleAdded(remoteName, \"inbox\", syncOps));\n\t\tchannel.inbox.onRemoved((syncOps) => this.handleRemoved(remoteName, \"inbox\", syncOps));\n\t\tchannel.outbox.onAdded((syncOps) => this.handleAdded(remoteName, \"outbox\", syncOps));\n\t\tchannel.outbox.onRemoved((syncOps) => this.handleRemoved(remoteName, \"outbox\", syncOps));\n\t\tchannel.deadLetter.onAdded((syncOps) => this.handleAdded(remoteName, \"deadLetter\", syncOps));\n\t}\n\tuntrackRemote(remoteName) {\n\t\tconst documents = this.remotes.get(remoteName);\n\t\tif (!documents) return;\n\t\tconst affectedDocumentIds = [...documents.keys()];\n\t\tthis.remotes.delete(remoteName);\n\t\tfor (const documentId of affectedDocumentIds) this.notifyChange(documentId);\n\t}\n\tclear() {\n\t\tthis.remotes.clear();\n\t\tthis.seen.clear();\n\t\tthis.callbacks.clear();\n\t}\n\thandleAdded(remoteName, mailboxType, syncOps) {\n\t\tconst changedDocuments = /* @__PURE__ */ new Set();\n\t\tfor (const syncOp of syncOps) {\n\t\t\tif (mailboxType === \"inbox\" && !syncOp.remoteName) continue;\n\t\t\tconst counts = this.getOrCreateCounts(remoteName, syncOp.documentId);\n\t\t\tthis.seen.add(syncOp.documentId);\n\t\t\tif (mailboxType === \"inbox\") counts.inboxCount++;\n\t\t\telse if (mailboxType === \"outbox\") counts.outboxCount++;\n\t\t\telse counts.errorCount++;\n\t\t\tchangedDocuments.add(syncOp.documentId);\n\t\t}\n\t\tfor (const documentId of changedDocuments) this.notifyChange(documentId);\n\t}\n\thandleRemoved(remoteName, mailboxType, syncOps) {\n\t\tconst changedDocuments = /* @__PURE__ */ new Set();\n\t\tfor (const syncOp of syncOps) {\n\t\t\tconst counts = this.getOrCreateCounts(remoteName, syncOp.documentId);\n\t\t\tif (mailboxType === \"inbox\") counts.inboxCount = Math.max(0, counts.inboxCount - 1);\n\t\t\telse if (mailboxType === \"outbox\") counts.outboxCount = Math.max(0, counts.outboxCount - 1);\n\t\t\tchangedDocuments.add(syncOp.documentId);\n\t\t}\n\t\tfor (const documentId of changedDocuments) this.notifyChange(documentId);\n\t}\n\tgetOrCreateCounts(remoteName, documentId) {\n\t\tlet documents = this.remotes.get(remoteName);\n\t\tif (!documents) {\n\t\t\tdocuments = /* @__PURE__ */ new Map();\n\t\t\tthis.remotes.set(remoteName, documents);\n\t\t}\n\t\tlet counts = documents.get(documentId);\n\t\tif (!counts) {\n\t\t\tcounts = {\n\t\t\t\tinboxCount: 0,\n\t\t\t\toutboxCount: 0,\n\t\t\t\terrorCount: 0\n\t\t\t};\n\t\t\tdocuments.set(documentId, counts);\n\t\t}\n\t\treturn counts;\n\t}\n\tnotifyChange(documentId) {\n\t\tconst status = this.getStatus(documentId);\n\t\tif (status === void 0) return;\n\t\tfor (const callback of [...this.callbacks]) callback(documentId, status);\n\t}\n};\nfunction deriveStatus(inboxCount, outboxCount, errorCount) {\n\tif (errorCount > 0) return SyncStatus.Error;\n\tif (inboxCount > 0 && outboxCount > 0) return SyncStatus.OutgoingAndIncoming;\n\tif (inboxCount > 0) return SyncStatus.Incoming;\n\tif (outboxCount > 0) return SyncStatus.Outgoing;\n\treturn SyncStatus.Synced;\n}\n//#endregion\n//#region src/sync/sync-manager.ts\nvar OutboxMode = /* @__PURE__ */ function(OutboxMode) {\n\tOutboxMode[\"Backfill\"] = \"backfill\";\n\tOutboxMode[\"BatchTriggered\"] = \"batch-triggered\";\n\treturn OutboxMode;\n}(OutboxMode || {});\nconst defaultSyncManagerConfig = {\n\tmaxDeadLettersPerRemote: 100,\n\tmaxInboxBatchSize: 32\n};\nconst PLAN_KEY_TO_JOB_UUID_CAP = 1e4;\nvar SyncManager = class {\n\tlogger;\n\tremoteStorage;\n\tcursorStorage;\n\tdeadLetterStorage;\n\tchannelFactory;\n\toperationIndex;\n\treactor;\n\teventBus;\n\tremotes;\n\tawaiter;\n\tsyncAwaiter;\n\tabortController = new AbortController();\n\tisShutdown;\n\teventUnsubscribe;\n\tfailedEventUnsubscribe;\n\tbatchAggregator;\n\tsyncStatusTracker;\n\tconfig;\n\tconnectionStateUnsubscribes = /* @__PURE__ */ new Map();\n\tquarantinedDocumentIds = /* @__PURE__ */ new Set();\n\tbackfillAbortControllers = /* @__PURE__ */ new Map();\n\tplanKeyToJobUuid = /* @__PURE__ */ new Map();\n\tlastEnqueuedJobIdByKey = /* @__PURE__ */ new Map();\n\tinboxChunkChain = Promise.resolve();\n\tconstructor(logger, remoteStorage, cursorStorage, deadLetterStorage, channelFactory, operationIndex, reactor, eventBus, driveContainerTypes, config = {}) {\n\t\tthis.logger = logger;\n\t\tthis.remoteStorage = remoteStorage;\n\t\tthis.cursorStorage = cursorStorage;\n\t\tthis.deadLetterStorage = deadLetterStorage;\n\t\tthis.channelFactory = channelFactory;\n\t\tthis.operationIndex = operationIndex;\n\t\tthis.reactor = reactor;\n\t\tthis.eventBus = eventBus;\n\t\tthis.config = {\n\t\t\t...defaultSyncManagerConfig,\n\t\t\t...config\n\t\t};\n\t\tthis.remotes = /* @__PURE__ */ new Map();\n\t\tthis.awaiter = new JobAwaiter(eventBus, (jobId, signal) => reactor.getJobStatus(jobId, signal));\n\t\tthis.syncAwaiter = new SyncAwaiter(eventBus);\n\t\tthis.isShutdown = false;\n\t\tthis.batchAggregator = new BatchAggregator(logger, driveContainerTypes, (batch) => this.processCompleteBatch(batch));\n\t\tthis.syncStatusTracker = new SyncStatusTracker();\n\t}\n\tasync startup() {\n\t\tif (this.isShutdown) throw new Error(\"SyncManager is already shutdown and cannot be started\");\n\t\ttry {\n\t\t\tconst quarantinedIds = await this.deadLetterStorage.listQuarantinedDocumentIds();\n\t\t\tfor (const id of quarantinedIds) this.quarantinedDocumentIds.add(id);\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Failed to load quarantined document IDs (@error)\", error instanceof Error ? error.message : String(error));\n\t\t}\n\t\tconst remoteRecords = await this.remoteStorage.list();\n\t\tfor (const record of remoteRecords) {\n\t\t\tconst channel = this.channelFactory.instance(record.id, record.name, record.channelConfig, this.cursorStorage, record.collectionId, record.filter, this.operationIndex, record.options);\n\t\t\tconst remote = {\n\t\t\t\tid: record.id,\n\t\t\t\tname: record.name,\n\t\t\t\tcollectionId: record.collectionId,\n\t\t\t\tfilter: record.filter,\n\t\t\t\toptions: record.options,\n\t\t\t\tchannel\n\t\t\t};\n\t\t\tthis.remotes.set(record.name, remote);\n\t\t\tawait this.loadDeadLetters(remote);\n\t\t\tthis.wireChannelCallbacks(remote);\n\t\t\ttry {\n\t\t\t\tawait channel.init();\n\t\t\t} catch (error) {\n\t\t\t\tthis.logger.error(\"Error initializing channel for remote (@name, @error)\", record.name, error instanceof Error ? error.message : String(error));\n\t\t\t\tthis.remotes.delete(record.name);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst outboxAckOrdinal = remote.channel.outbox.ackOrdinal;\n\t\t\tif (outboxAckOrdinal > 0) {\n\t\t\t\tconst backfillController = new AbortController();\n\t\t\t\tthis.backfillAbortControllers.set(record.name, backfillController);\n\t\t\t\tthis.updateOutbox(remote, outboxAckOrdinal, OutboxMode.Backfill, backfillController.signal).catch((error) => {\n\t\t\t\t\tif (backfillController.signal.aborted) return;\n\t\t\t\t\tthis.logger.error(\"Backfill failed for remote @RemoteName: @Error\", remote.name, error instanceof Error ? error : new Error(String(error)));\n\t\t\t\t}).finally(() => {\n\t\t\t\t\tthis.backfillAbortControllers.delete(record.name);\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\tthis.eventUnsubscribe = this.eventBus.subscribe(ReactorEventTypes.JOB_WRITE_READY, async (_type, event) => this.batchAggregator.enqueueWriteReady(event));\n\t\tthis.failedEventUnsubscribe = this.eventBus.subscribe(ReactorEventTypes.JOB_FAILED, async (_type, event) => this.batchAggregator.handleJobFailed(event));\n\t}\n\tshutdown() {\n\t\tthis.isShutdown = true;\n\t\tthis.abortController.abort();\n\t\tfor (const controller of this.backfillAbortControllers.values()) controller.abort();\n\t\tthis.backfillAbortControllers.clear();\n\t\tthis.planKeyToJobUuid.clear();\n\t\tthis.lastEnqueuedJobIdByKey.clear();\n\t\tthis.batchAggregator.clear();\n\t\tif (this.eventUnsubscribe) {\n\t\t\tthis.eventUnsubscribe();\n\t\t\tthis.eventUnsubscribe = void 0;\n\t\t}\n\t\tif (this.failedEventUnsubscribe) {\n\t\t\tthis.failedEventUnsubscribe();\n\t\t\tthis.failedEventUnsubscribe = void 0;\n\t\t}\n\t\tthis.awaiter.shutdown();\n\t\tthis.syncAwaiter.shutdown();\n\t\tthis.syncStatusTracker.clear();\n\t\tfor (const unsub of this.connectionStateUnsubscribes.values()) unsub();\n\t\tthis.connectionStateUnsubscribes.clear();\n\t\tconst promises = [];\n\t\tfor (const remote of this.remotes.values()) promises.push(remote.channel.shutdown());\n\t\tthis.remotes.clear();\n\t\treturn {\n\t\t\tisShutdown: true,\n\t\t\tcompleted: Promise.all(promises).then(() => void 0)\n\t\t};\n\t}\n\tgetByName(name) {\n\t\tconst remote = this.remotes.get(name);\n\t\tif (!remote) throw new Error(`Remote with name '${name}' does not exist`);\n\t\treturn remote;\n\t}\n\tgetById(id) {\n\t\tfor (const remote of this.remotes.values()) if (remote.id === id) return remote;\n\t\tthrow new Error(`Remote with id '${id}' does not exist`);\n\t}\n\tasync add(name, collectionId, channelConfig, filter = {\n\t\tdocumentId: [],\n\t\tscope: [],\n\t\tbranch: \"\"\n\t}, options = { sinceTimestampUtcMs: \"0\" }, id) {\n\t\tif (this.isShutdown) throw new Error(\"SyncManager is shutdown and cannot add remotes\");\n\t\tif (this.remotes.has(name)) throw new Error(`Remote with name '${name}' already exists`);\n\t\tthis.logger.debug(\"Adding remote (@name, @collectionId, @channelConfig, @filter, @options, @id)\", name, collectionId, channelConfig, filter, options, id);\n\t\tconst remoteId = id ?? crypto.randomUUID();\n\t\tconst remoteRecord = {\n\t\t\tid: remoteId,\n\t\t\tname,\n\t\t\tcollectionId,\n\t\t\tchannelConfig,\n\t\t\tfilter,\n\t\t\toptions,\n\t\t\tstatus: {\n\t\t\t\tpush: createIdleHealth(),\n\t\t\t\tpull: createIdleHealth()\n\t\t\t}\n\t\t};\n\t\tawait this.remoteStorage.upsert(remoteRecord);\n\t\tconst channel = this.channelFactory.instance(remoteId, name, channelConfig, this.cursorStorage, collectionId, filter, this.operationIndex, options);\n\t\tconst remote = {\n\t\t\tid: remoteId,\n\t\t\tname,\n\t\t\tcollectionId,\n\t\t\tfilter,\n\t\t\toptions,\n\t\t\tchannel\n\t\t};\n\t\tthis.remotes.set(name, remote);\n\t\tawait this.loadDeadLetters(remote);\n\t\tthis.wireChannelCallbacks(remote);\n\t\ttry {\n\t\t\tawait channel.init();\n\t\t} catch (error) {\n\t\t\tthis.remotes.delete(name);\n\t\t\tawait this.remoteStorage.remove(name);\n\t\t\tthrow error;\n\t\t}\n\t\tconst backfillController = new AbortController();\n\t\tthis.backfillAbortControllers.set(name, backfillController);\n\t\tthis.updateOutbox(remote, 0, OutboxMode.Backfill, backfillController.signal).catch((error) => {\n\t\t\tif (backfillController.signal.aborted) return;\n\t\t\tthis.logger.error(\"Backfill failed for remote @RemoteName: @Error\", remote.name, error instanceof Error ? error : new Error(String(error)));\n\t\t}).finally(() => {\n\t\t\tthis.backfillAbortControllers.delete(name);\n\t\t});\n\t\treturn remote;\n\t}\n\ttriggerPull(name) {\n\t\tconst remote = this.remotes.get(name);\n\t\tif (!remote) throw new Error(`Remote with name '${name}' does not exist`);\n\t\tremote.channel.triggerPull();\n\t}\n\tasync remove(name) {\n\t\tconst remote = this.remotes.get(name);\n\t\tif (!remote) throw new Error(`Remote with name '${name}' does not exist`);\n\t\tconst backfillController = this.backfillAbortControllers.get(name);\n\t\tif (backfillController) {\n\t\t\tbackfillController.abort();\n\t\t\tthis.backfillAbortControllers.delete(name);\n\t\t}\n\t\tawait remote.channel.shutdown();\n\t\tawait this.remoteStorage.remove(name);\n\t\tawait this.cursorStorage.remove(name);\n\t\tthis.syncStatusTracker.untrackRemote(name);\n\t\tconst unsub = this.connectionStateUnsubscribes.get(name);\n\t\tif (unsub) {\n\t\t\tunsub();\n\t\t\tthis.connectionStateUnsubscribes.delete(name);\n\t\t}\n\t\tthis.remotes.delete(name);\n\t}\n\tlist() {\n\t\treturn Array.from(this.remotes.values());\n\t}\n\twaitForSync(jobId, signal) {\n\t\treturn this.syncAwaiter.waitForSync(jobId, signal);\n\t}\n\tgetSyncStatus(documentId) {\n\t\treturn this.syncStatusTracker.getStatus(documentId);\n\t}\n\tonSyncStatusChange(callback) {\n\t\treturn this.syncStatusTracker.onChange(callback);\n\t}\n\trecordPlanKeyMapping(planKey, jobId) {\n\t\tif (!this.planKeyToJobUuid.has(planKey) && this.planKeyToJobUuid.size >= PLAN_KEY_TO_JOB_UUID_CAP) {\n\t\t\tconst oldest = this.planKeyToJobUuid.keys().next().value;\n\t\t\tif (oldest !== void 0) this.planKeyToJobUuid.delete(oldest);\n\t\t}\n\t\tthis.planKeyToJobUuid.set(planKey, jobId);\n\t}\n\twireChannelCallbacks(remote) {\n\t\tremote.channel.inbox.onAdded((syncOps) => this.handleInboxAdded(remote, syncOps));\n\t\tthis.syncStatusTracker.trackRemote(remote.name, remote.channel);\n\t\tconst unsubscribe = remote.channel.onConnectionStateChange((snapshot) => {\n\t\t\tthis.eventBus.emit(SyncEventTypes.CONNECTION_STATE_CHANGED, {\n\t\t\t\tremoteName: remote.name,\n\t\t\t\tremoteId: remote.id,\n\t\t\t\tprevious: snapshot.state,\n\t\t\t\tcurrent: snapshot.state,\n\t\t\t\tsnapshot\n\t\t\t}).catch(() => {});\n\t\t});\n\t\tthis.connectionStateUnsubscribes.set(remote.name, unsubscribe);\n\t\tremote.channel.deadLetter.onAdded((syncOps) => {\n\t\t\tfor (const syncOp of syncOps) {\n\t\t\t\tthis.logger.error(\"Dead letter (@remote, @documentId, @jobId, @error, @dependencies)\", remote.name, syncOp.documentId, syncOp.jobId, syncOp.error?.message ?? \"unknown\", syncOp.jobDependencies);\n\t\t\t\tthis.quarantinedDocumentIds.add(syncOp.documentId);\n\t\t\t\tconst record = {\n\t\t\t\t\tid: syncOp.id,\n\t\t\t\t\tjobId: syncOp.jobId,\n\t\t\t\t\tjobDependencies: syncOp.jobDependencies,\n\t\t\t\t\tremoteName: syncOp.remoteName,\n\t\t\t\t\tdocumentId: syncOp.documentId,\n\t\t\t\t\tscopes: syncOp.scopes,\n\t\t\t\t\tbranch: syncOp.branch,\n\t\t\t\t\toperations: syncOp.operations,\n\t\t\t\t\terrorSource: syncOp.error?.source ?? ChannelErrorSource.None,\n\t\t\t\t\terrorMessage: syncOp.error?.error.message ?? \"unknown\"\n\t\t\t\t};\n\t\t\t\tthis.deadLetterStorage.add(record).catch((err) => {\n\t\t\t\t\tthis.logger.error(\"Failed to persist dead letter (@id, @error)\", record.id, err instanceof Error ? err.message : String(err));\n\t\t\t\t});\n\t\t\t\tthis.eventBus.emit(SyncEventTypes.DEAD_LETTER_ADDED, {\n\t\t\t\t\tid: record.id,\n\t\t\t\t\tjobId: record.jobId,\n\t\t\t\t\tremoteName: record.remoteName,\n\t\t\t\t\tdocumentId: record.documentId,\n\t\t\t\t\terrorSource: record.errorSource\n\t\t\t\t}).catch(() => {});\n\t\t\t}\n\t\t\tconst items = remote.channel.deadLetter.items;\n\t\t\tif (items.length > this.config.maxDeadLettersPerRemote) {\n\t\t\t\tconst excessCount = items.length - this.config.maxDeadLettersPerRemote;\n\t\t\t\tconst toEvict = items.slice(0, excessCount);\n\t\t\t\tremote.channel.deadLetter.remove(...toEvict);\n\t\t\t}\n\t\t});\n\t}\n\tasync loadDeadLetters(remote) {\n\t\tlet records;\n\t\ttry {\n\t\t\trecords = (await this.deadLetterStorage.list(remote.name, {\n\t\t\t\tcursor: \"0\",\n\t\t\t\tlimit: this.config.maxDeadLettersPerRemote\n\t\t\t})).results;\n\t\t} catch (error) {\n\t\t\tthis.logger.error(\"Failed to load dead letters for remote (@name, @error)\", remote.name, error instanceof Error ? error.message : String(error));\n\t\t\treturn;\n\t\t}\n\t\tif (records.length === 0) return;\n\t\trecords.reverse();\n\t\tconst syncOps = [];\n\t\tfor (const record of records) {\n\t\t\tconst syncOp = new SyncOperation(record.id, record.jobId, record.jobDependencies, record.remoteName, record.documentId, record.scopes, record.branch, record.operations);\n\t\t\tsyncOp.failed(new ChannelError(record.errorSource, new Error(record.errorMessage)));\n\t\t\tsyncOps.push(syncOp);\n\t\t}\n\t\tremote.channel.deadLetter.add(...syncOps);\n\t\tthis.logger.debug(\"Loaded @count persisted dead letters for remote @name\", records.length, remote.name);\n\t}\n\tgetRemotesForCollection(collectionId) {\n\t\treturn Array.from(this.remotes.values()).filter((remote) => remote.collectionId === collectionId);\n\t}\n\tasync processCompleteBatch(batch) {\n\t\tif (this.isShutdown) return;\n\t\tconst collectionIds = [...new Set(Object.values(batch.collectionMemberships).flatMap((collections) => collections))];\n\t\tconst affectedRemotes = [];\n\t\tfor (const collectionId of collectionIds) {\n\t\t\tconst remotes = this.getRemotesForCollection(collectionId);\n\t\t\tfor (const remote of remotes) if (!affectedRemotes.includes(remote)) affectedRemotes.push(remote);\n\t\t}\n\t\tfor (const remote of affectedRemotes) trimMailboxFromBatch(remote.channel.inbox, batch);\n\t\tfor (const remote of affectedRemotes) await this.updateOutbox(remote, remote.channel.outbox.latestOrdinal, OutboxMode.BatchTriggered);\n\t}\n\thandleInboxAdded(remote, syncOps) {\n\t\tif (this.isShutdown) return;\n\t\tconst eligible = syncOps.filter((op) => !this.quarantinedDocumentIds.has(op.documentId));\n\t\tif (eligible.length === 0) return;\n\t\tconst keyed = [];\n\t\tconst nonKeyed = [];\n\t\tfor (const syncOp of eligible) if (syncOp.jobId) keyed.push(syncOp);\n\t\telse nonKeyed.push(syncOp);\n\t\tfor (const syncOp of nonKeyed) this.applyInboxJob(remote, syncOp);\n\t\tif (keyed.length > 0) {\n\t\t\tconst chunks = chunkSyncOperations(keyed.map((syncOp) => ({\n\t\t\t\tremote,\n\t\t\t\tsyncOp\n\t\t\t})), this.config.maxInboxBatchSize);\n\t\t\tthis.processInboxChunks(chunks);\n\t\t}\n\t}\n\tprocessInboxChunks(chunks) {\n\t\tconst next = this.inboxChunkChain.then(async () => {\n\t\t\tfor (const chunk of chunks) {\n\t\t\t\tif (this.isShutdown) return;\n\t\t\t\tawait this.applyInboxBatch(chunk);\n\t\t\t}\n\t\t});\n\t\tthis.inboxChunkChain = next.catch((err) => {\n\t\t\tthis.logger.error(\"Inbox chunk processing failed (@error)\", err instanceof Error ? err.message : String(err));\n\t\t});\n\t\treturn next;\n\t}\n\tasync applyInboxJob(remote, syncOp) {\n\t\tconst operations = syncOp.operations.map((op) => op.operation);\n\t\tlet jobInfo;\n\t\ttry {\n\t\t\tjobInfo = await this.reactor.load(syncOp.documentId, syncOp.branch, operations, this.abortController.signal, { sourceRemote: remote.name });\n\t\t} catch (error) {\n\t\t\tif (this.isShutdown) return;\n\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\tthis.logger.error(\"Failed to load operations from inbox (@remote, @documentId, @error)\", remote.name, syncOp.documentId, err.message);\n\t\t\tconst channelError = new ChannelError(ChannelErrorSource.Inbox, err);\n\t\t\tsyncOp.failed(channelError);\n\t\t\tremote.channel.deadLetter.add(syncOp);\n\t\t\tremote.channel.inbox.remove(syncOp);\n\t\t\treturn;\n\t\t}\n\t\tlet completedJobInfo;\n\t\ttry {\n\t\t\tcompletedJobInfo = await this.awaiter.waitForJob(jobInfo.id, this.abortController.signal);\n\t\t} catch (error) {\n\t\t\tif (this.isShutdown) return;\n\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\tthis.logger.error(\"Failed to wait for job completion (@remote, @documentId, @jobId, @error)\", remote.name, syncOp.documentId, jobInfo.id, err.message);\n\t\t\tconst channelError = new ChannelError(ChannelErrorSource.Inbox, err);\n\t\t\tsyncOp.failed(channelError);\n\t\t\tremote.channel.deadLetter.add(syncOp);\n\t\t\tremote.channel.inbox.remove(syncOp);\n\t\t\treturn;\n\t\t}\n\t\tif (this.isShutdown) return;\n\t\tif (completedJobInfo.status === JobStatus.FAILED) {\n\t\t\tconst errorMessage = completedJobInfo.error?.message || \"Unknown error\";\n\t\t\tthis.logger.error(\"Failed to apply operations from inbox (@remote, @documentId, @jobId, @error)\", remote.name, syncOp.documentId, completedJobInfo.id, errorMessage);\n\t\t\tconst error = new ChannelError(ChannelErrorSource.Inbox, /* @__PURE__ */ new Error(`Failed to apply operations: ${errorMessage}`));\n\t\t\tsyncOp.failed(error);\n\t\t\tremote.channel.deadLetter.add(syncOp);\n\t\t} else syncOp.executed();\n\t\tremote.channel.inbox.remove(syncOp);\n\t}\n\tasync applyInboxBatch(items) {\n\t\tconst sourceRemote = items[0].remote.name;\n\t\tconst chunkKeys = new Set(items.map(({ syncOp }) => syncOp.jobId));\n\t\tconst jobs = items.map(({ syncOp }) => {\n\t\t\tconst dependsOn = [];\n\t\t\tconst externalDeps = [];\n\t\t\tfor (const dep of syncOp.jobDependencies) {\n\t\t\t\tif (!dep) continue;\n\t\t\t\tif (chunkKeys.has(dep)) dependsOn.push(dep);\n\t\t\t\telse {\n\t\t\t\t\tconst uuid = this.planKeyToJobUuid.get(dep);\n\t\t\t\t\tif (uuid !== void 0) externalDeps.push(uuid);\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst fifoKey = `${syncOp.documentId}:${syncOp.scopes[0]}:${syncOp.branch}`;\n\t\t\tconst prevUuid = this.lastEnqueuedJobIdByKey.get(fifoKey);\n\t\t\tif (prevUuid !== void 0) externalDeps.push(prevUuid);\n\t\t\treturn {\n\t\t\t\tkey: syncOp.jobId,\n\t\t\t\tdocumentId: syncOp.documentId,\n\t\t\t\tscope: syncOp.scopes[0],\n\t\t\t\tbranch: syncOp.branch,\n\t\t\t\toperations: syncOp.operations.map((op) => op.operation),\n\t\t\t\tdependsOn,\n\t\t\t\texternalDeps\n\t\t\t};\n\t\t});\n\t\tconst request = { jobs };\n\t\tlet result;\n\t\ttry {\n\t\t\tresult = await this.reactor.loadBatch(request, this.abortController.signal, { sourceRemote });\n\t\t} catch (error) {\n\t\t\tif (this.isShutdown) return;\n\t\t\tfor (const { remote, syncOp } of items) {\n\t\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\t\tsyncOp.failed(new ChannelError(ChannelErrorSource.Inbox, err));\n\t\t\t\tremote.channel.deadLetter.add(syncOp);\n\t\t\t\tremote.channel.inbox.remove(syncOp);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tif (this.isShutdown) return;\n\t\tfor (const plan of jobs) {\n\t\t\tif (!(plan.key in result.jobs)) continue;\n\t\t\tconst info = result.jobs[plan.key];\n\t\t\tthis.recordPlanKeyMapping(plan.key, info.id);\n\t\t\tconst fifoKey = `${plan.documentId}:${plan.scope}:${plan.branch}`;\n\t\t\tthis.lastEnqueuedJobIdByKey.set(fifoKey, info.id);\n\t\t}\n\t\tfor (const { remote, syncOp } of items) {\n\t\t\tif (!(syncOp.jobId in result.jobs)) {\n\t\t\t\tthis.logger.error(\"Job key missing from batch load result (@remote, @documentId, @jobId)\", remote.name, syncOp.documentId, syncOp.jobId);\n\t\t\t\tconst error = new ChannelError(ChannelErrorSource.Inbox, /* @__PURE__ */ new Error(`Job key '${syncOp.jobId}' missing from batch load result`));\n\t\t\t\tsyncOp.failed(error);\n\t\t\t\tremote.channel.deadLetter.add(syncOp);\n\t\t\t\tremote.channel.inbox.remove(syncOp);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst jobInfo = result.jobs[syncOp.jobId];\n\t\t\tlet completedJobInfo;\n\t\t\ttry {\n\t\t\t\tcompletedJobInfo = await this.awaiter.waitForJob(jobInfo.id, this.abortController.signal);\n\t\t\t} catch (error) {\n\t\t\t\tif (this.isShutdown) continue;\n\t\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\t\tsyncOp.failed(new ChannelError(ChannelErrorSource.Inbox, err));\n\t\t\t\tremote.channel.deadLetter.add(syncOp);\n\t\t\t\tremote.channel.inbox.remove(syncOp);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (this.isShutdown) return;\n\t\t\tif (completedJobInfo.status === JobStatus.FAILED) {\n\t\t\t\tconst errorMessage = completedJobInfo.error?.message || \"Unknown error\";\n\t\t\t\tconst channelError = new ChannelError(ChannelErrorSource.Inbox, /* @__PURE__ */ new Error(`Failed to apply operations: ${errorMessage}`));\n\t\t\t\tsyncOp.failed(channelError);\n\t\t\t\tremote.channel.deadLetter.add(syncOp);\n\t\t\t} else syncOp.executed();\n\t\t\tremote.channel.inbox.remove(syncOp);\n\t\t}\n\t}\n\tasync updateOutbox(remote, ackOrdinal, mode = OutboxMode.Backfill, signal) {\n\t\tconst composedSignal = signal ? AbortSignal.any([signal, this.abortController.signal]) : this.abortController.signal;\n\t\tlet maxOrdinal = ackOrdinal;\n\t\tconst lastJobByDoc = /* @__PURE__ */ new Map();\n\t\tlet prevChainJobId;\n\t\tconst sinceTimestamp = remote.options.sinceTimestampUtcMs;\n\t\tconst emitBatches = (operations) => {\n\t\t\tif (operations.length === 0) return;\n\t\t\tconst batches = batchOperationsByDocument(operations);\n\t\t\tconst syncOps = [];\n\t\t\tfor (const batch of batches) {\n\t\t\t\tconst jobId = crypto.randomUUID();\n\t\t\t\tconst prevJobId = lastJobByDoc.get(batch.documentId);\n\t\t\t\tconst deps = [];\n\t\t\t\tif (prevJobId) deps.push(prevJobId);\n\t\t\t\tif (mode === OutboxMode.BatchTriggered && prevChainJobId && prevChainJobId !== prevJobId) deps.push(prevChainJobId);\n\t\t\t\tconst syncOp = new SyncOperation(crypto.randomUUID(), jobId, deps, remote.name, batch.documentId, [batch.scope], batch.branch, batch.operations);\n\t\t\t\tsyncOps.push(syncOp);\n\t\t\t\tlastJobByDoc.set(batch.documentId, jobId);\n\t\t\t\tif (mode === OutboxMode.BatchTriggered) prevChainJobId = jobId;\n\t\t\t}\n\t\t\tremote.channel.outbox.add(...syncOps);\n\t\t};\n\t\tlet page = await this.operationIndex.find(remote.collectionId, ackOrdinal, { excludeSourceRemote: remote.name }, void 0, composedSignal);\n\t\tlet carry = [];\n\t\tlet hasMore;\n\t\tdo {\n\t\t\tif (composedSignal.aborted) return;\n\t\t\tfor (const entry of page.results) maxOrdinal = Math.max(maxOrdinal, entry.ordinal ?? 0);\n\t\t\tlet operations = page.results.map((entry) => toOperationWithContext(entry));\n\t\t\tif (carry.length > 0) {\n\t\t\t\toperations = [...carry, ...operations];\n\t\t\t\tcarry = [];\n\t\t\t}\n\t\t\tif (sinceTimestamp && sinceTimestamp !== \"0\") operations = operations.filter((op) => op.operation.timestampUtcMs >= sinceTimestamp);\n\t\t\toperations = filterOperations(operations, remote.filter);\n\t\t\toperations = operations.filter((op) => !this.quarantinedDocumentIds.has(op.context.documentId));\n\t\t\thasMore = !!page.next;\n\t\t\tif (operations.length > 0) {\n\t\t\t\toperations.sort((a, b) => {\n\t\t\t\t\tif (a.context.documentId !== b.context.documentId) return a.context.documentId < b.context.documentId ? -1 : 1;\n\t\t\t\t\tif (a.context.scope !== b.context.scope) return a.context.scope < b.context.scope ? -1 : 1;\n\t\t\t\t\treturn a.context.ordinal - b.context.ordinal;\n\t\t\t\t});\n\t\t\t\tif (hasMore) {\n\t\t\t\t\tconst split = splitTrailingSameTimestampRun(operations);\n\t\t\t\t\tcarry = split.carry;\n\t\t\t\t\temitBatches(split.emit);\n\t\t\t\t} else emitBatches(operations);\n\t\t\t}\n\t\t\tif (hasMore) page = await page.next();\n\t\t} while (hasMore);\n\t\tif (carry.length > 0) emitBatches(carry);\n\t\tremote.channel.outbox.advanceOrdinal(maxOrdinal);\n\t}\n};\n//#endregion\n//#region src/sync/sync-builder.ts\nvar SyncBuilder = class {\n\tchannelFactory;\n\tremoteStorage;\n\tcursorStorage;\n\tdeadLetterStorage;\n\tconfig = {};\n\twithChannelFactory(factory) {\n\t\tthis.channelFactory = factory;\n\t\treturn this;\n\t}\n\twithRemoteStorage(storage) {\n\t\tthis.remoteStorage = storage;\n\t\treturn this;\n\t}\n\twithCursorStorage(storage) {\n\t\tthis.cursorStorage = storage;\n\t\treturn this;\n\t}\n\twithDeadLetterStorage(storage) {\n\t\tthis.deadLetterStorage = storage;\n\t\treturn this;\n\t}\n\twithMaxDeadLettersPerRemote(limit) {\n\t\tthis.config.maxDeadLettersPerRemote = limit;\n\t\treturn this;\n\t}\n\twithMaxInboxBatchSize(limit) {\n\t\tthis.config.maxInboxBatchSize = limit;\n\t\treturn this;\n\t}\n\tbuild(reactor, logger, operationIndex, eventBus, db, driveContainerTypes) {\n\t\treturn this.buildModule(reactor, logger, operationIndex, eventBus, db, driveContainerTypes).syncManager;\n\t}\n\tbuildModule(reactor, logger, operationIndex, eventBus, db, driveContainerTypes) {\n\t\tif (!this.channelFactory) throw new Error(\"Channel factory is required\");\n\t\tconst remoteStorage = this.remoteStorage ?? new KyselySyncRemoteStorage(db);\n\t\tconst cursorStorage = this.cursorStorage ?? new KyselySyncCursorStorage(db);\n\t\tconst deadLetterStorage = this.deadLetterStorage ?? new KyselySyncDeadLetterStorage(db);\n\t\tconst syncManager = new SyncManager(logger, remoteStorage, cursorStorage, deadLetterStorage, this.channelFactory, operationIndex, reactor, eventBus, driveContainerTypes, this.config);\n\t\treturn {\n\t\t\tremoteStorage,\n\t\t\tcursorStorage,\n\t\t\tdeadLetterStorage,\n\t\t\tchannelFactory: this.channelFactory,\n\t\t\tsyncManager\n\t\t};\n\t}\n};\n//#endregion\n//#region src/core/create-default-database.ts\nasync function createDefaultDatabase() {\n\tconst { Kysely } = await import(\"kysely\");\n\tconst { PGlite } = await import(\"@electric-sql/pglite\");\n\tconst { PGliteDialect } = await import(\"kysely-pglite-dialect\");\n\treturn new Kysely({ dialect: new PGliteDialect(new PGlite()) });\n}\n//#endregion\n//#region src/shared/factories.ts\n/**\n* Factory method to create a ShutdownStatus that can be updated\n*\n* @param initialState - Initial shutdown state (default: false)\n* @returns A tuple of [ShutdownStatus, setShutdown function, setCompleted function]\n*/\nfunction createMutableShutdownStatus(initialState = false) {\n\tlet shutdownState = initialState;\n\tlet completedPromise = Promise.resolve();\n\tconst status = {\n\t\tget isShutdown() {\n\t\t\treturn shutdownState;\n\t\t},\n\t\tget completed() {\n\t\t\treturn completedPromise;\n\t\t}\n\t};\n\tconst setShutdown = (value) => {\n\t\tshutdownState = value;\n\t};\n\tconst setCompleted = (promise) => {\n\t\tcompletedPromise = promise;\n\t};\n\treturn [\n\t\tstatus,\n\t\tsetShutdown,\n\t\tsetCompleted\n\t];\n}\n//#endregion\n//#region src/core/types.ts\nvar AbortError = class extends Error {\n\tconstructor(message) {\n\t\tsuper(message || \"Aborted\");\n\t\tthis.name = \"AbortError\";\n\t}\n};\n//#endregion\n//#region src/core/reactor.ts\n/**\n* This class implements the IReactor interface and serves as the main entry point\n* for the new Reactor architecture.\n*/\nvar Reactor = class {\n\tlogger;\n\tdocumentModelRegistry;\n\tshutdownStatus;\n\tsetShutdown;\n\tsetCompleted;\n\tqueue;\n\tjobTracker;\n\treadModelCoordinator;\n\tfeatures;\n\tdocumentView;\n\tdocumentIndexer;\n\toperationStore;\n\teventBus;\n\texecutorManager;\n\tconstructor(logger, documentModelRegistry, queue, jobTracker, readModelCoordinator, features, documentView, documentIndexer, operationStore, eventBus, executorManager) {\n\t\tthis.logger = logger;\n\t\tthis.documentModelRegistry = documentModelRegistry;\n\t\tthis.queue = queue;\n\t\tthis.jobTracker = jobTracker;\n\t\tthis.readModelCoordinator = readModelCoordinator;\n\t\tthis.features = features;\n\t\tthis.documentView = documentView;\n\t\tthis.documentIndexer = documentIndexer;\n\t\tthis.operationStore = operationStore;\n\t\tthis.eventBus = eventBus;\n\t\tthis.executorManager = executorManager;\n\t\tconst [status, setShutdown, setCompleted] = createMutableShutdownStatus(false);\n\t\tthis.shutdownStatus = status;\n\t\tthis.setShutdown = setShutdown;\n\t\tthis.setCompleted = setCompleted;\n\t\tthis.eventBus.subscribe(ReactorEventTypes.JOB_FAILED, (_type, event) => {\n\t\t\tthis.logger.error(\"Job @JobId failed with @Message: @Job\", event.jobId, event.error.message, event.job);\n\t\t});\n\t\tthis.readModelCoordinator.start();\n\t}\n\tkill() {\n\t\tthis.logger.verbose(\"kill()\");\n\t\tif (this.shutdownStatus.isShutdown) return this.shutdownStatus;\n\t\tthis.setShutdown(true);\n\t\tconst shutdownAsync = async () => {\n\t\t\tawait this.executorManager.stop(true);\n\t\t\tthis.readModelCoordinator.stop();\n\t\t\tthis.jobTracker.shutdown();\n\t\t};\n\t\tthis.setCompleted(shutdownAsync());\n\t\treturn this.shutdownStatus;\n\t}\n\tgetDocumentModels(namespace, paging, signal) {\n\t\tthis.logger.verbose(\"getDocumentModels(@namespace, @paging)\", namespace, paging);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\tconst filteredModels = this.documentModelRegistry.getAllModules().filter((module) => !namespace || module.documentModel.global.id.startsWith(namespace));\n\t\tconst startIndex = paging ? parseInt(paging.cursor) || 0 : 0;\n\t\tconst limit = paging?.limit || filteredModels.length;\n\t\tconst pagedModels = filteredModels.slice(startIndex, startIndex + limit);\n\t\tconst hasMore = startIndex + limit < filteredModels.length;\n\t\tconst nextCursor = hasMore ? String(startIndex + limit) : void 0;\n\t\treturn Promise.resolve({\n\t\t\tresults: pagedModels,\n\t\t\toptions: paging || {\n\t\t\t\tcursor: \"0\",\n\t\t\t\tlimit: filteredModels.length\n\t\t\t},\n\t\t\tnextCursor,\n\t\t\tnext: hasMore ? () => this.getDocumentModels(namespace, {\n\t\t\t\tcursor: nextCursor,\n\t\t\t\tlimit\n\t\t\t}, signal) : void 0\n\t\t});\n\t}\n\tasync get(id, view, consistencyToken, signal) {\n\t\tthis.logger.verbose(\"get(@id, @view)\", id, view);\n\t\treturn await this.documentView.get(id, view, consistencyToken, signal);\n\t}\n\tasync getBySlug(slug, view, consistencyToken, signal) {\n\t\tthis.logger.verbose(\"getBySlug(@slug, @view)\", slug, view);\n\t\tconst documentId = await this.documentView.resolveSlug(slug, view, consistencyToken, signal);\n\t\tif (!documentId) throw new Error(`Document not found with slug: ${slug}`);\n\t\treturn await this.get(documentId, view, consistencyToken, signal);\n\t}\n\tasync getByIdOrSlug(identifier, view, consistencyToken, signal) {\n\t\tthis.logger.verbose(\"getByIdOrSlug(@identifier, @view)\", identifier, view);\n\t\treturn await this.documentView.getByIdOrSlug(identifier, view, consistencyToken, signal);\n\t}\n\tasync getOutgoingRelationships(sourceId, relationshipType, consistencyToken, signal) {\n\t\tconst relationships = await this.documentIndexer.getOutgoing(sourceId, [relationshipType], void 0, consistencyToken, signal);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\treturn relationships.results.map((rel) => rel.targetId);\n\t}\n\tasync getIncomingRelationships(targetId, relationshipType, consistencyToken, signal) {\n\t\tconst relationships = await this.documentIndexer.getIncoming(targetId, [relationshipType], void 0, consistencyToken, signal);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\treturn relationships.results.map((rel) => rel.sourceId);\n\t}\n\tasync getOperations(documentId, view, filter, paging, consistencyToken, signal) {\n\t\tthis.logger.verbose(\"getOperations(@documentId, @view, @filter, @paging)\", documentId, view, filter, paging);\n\t\tconst branch = view?.branch || \"main\";\n\t\tconst revisions = await this.operationStore.getRevisions(documentId, branch, signal);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\tconst allScopes = Object.keys(revisions.revision);\n\t\tconst result = {};\n\t\tfor (const scope of allScopes) {\n\t\t\tif (!matchesScope(view, scope)) continue;\n\t\t\tthrowIfAborted(signal, () => new AbortError());\n\t\t\tconst scopeResult = await this.operationStore.getSince(documentId, scope, branch, -1, filter, paging, signal);\n\t\t\tresult[scope] = {\n\t\t\t\tresults: scopeResult.results,\n\t\t\t\toptions: scopeResult.options,\n\t\t\t\tnextCursor: scopeResult.nextCursor,\n\t\t\t\tnext: scopeResult.next ? async () => {\n\t\t\t\t\treturn (await this.getOperations(documentId, view, filter, {\n\t\t\t\t\t\tcursor: scopeResult.nextCursor,\n\t\t\t\t\t\tlimit: scopeResult.options.limit\n\t\t\t\t\t}, consistencyToken, signal))[scope];\n\t\t\t\t} : void 0\n\t\t\t};\n\t\t}\n\t\treturn result;\n\t}\n\tasync find(search, view, paging, consistencyToken, signal) {\n\t\tthis.logger.verbose(\"find(@search, @view, @paging)\", search, view, paging);\n\t\tlet results;\n\t\tif (search.ids) {\n\t\t\tif (search.slugs && search.slugs.length > 0) throw new Error(\"Cannot use both ids and slugs in the same search\");\n\t\t\tresults = await this.findByIds(search.ids, view, paging, consistencyToken, signal);\n\t\t\tif (search.type) results = filterByType(results, search.type);\n\t\t} else if (search.slugs) {\n\t\t\tresults = await this.findBySlugs(search.slugs, view, paging, consistencyToken, signal);\n\t\t\tif (search.type) results = filterByType(results, search.type);\n\t\t} else if (search.parentId) {\n\t\t\tresults = await this.findByParentId(search.parentId, view, paging, consistencyToken, signal);\n\t\t\tif (search.type) results = filterByType(results, search.type);\n\t\t} else if (search.type) results = await this.findByType(search.type, view, paging, consistencyToken, signal);\n\t\telse throw new Error(\"No search criteria provided\");\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\treturn results;\n\t}\n\tasync create(document, signer, signal, meta) {\n\t\tthis.logger.verbose(\"create(@id, @type, @slug)\", document.header.id, document.header.documentType, document.header.slug);\n\t\tconst createdAtUtcIso = (/* @__PURE__ */ new Date()).toISOString();\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\tlet actions = [createDocumentAction({\n\t\t\tmodel: document.header.documentType,\n\t\t\tversion: 0,\n\t\t\tdocumentId: document.header.id,\n\t\t\tsigning: {\n\t\t\t\tsignature: document.header.id,\n\t\t\t\tpublicKey: document.header.sig.publicKey,\n\t\t\t\tnonce: document.header.sig.nonce,\n\t\t\t\tcreatedAtUtcIso: document.header.createdAtUtcIso,\n\t\t\t\tdocumentType: document.header.documentType\n\t\t\t},\n\t\t\tslug: document.header.slug,\n\t\t\tname: document.header.name,\n\t\t\tbranch: document.header.branch,\n\t\t\tmeta: document.header.meta,\n\t\t\tprotocolVersions: document.header.protocolVersions ?? { \"base-reducer\": 2 }\n\t\t}), upgradeDocumentAction({\n\t\t\tdocumentId: document.header.id,\n\t\t\tmodel: document.header.documentType,\n\t\t\tfromVersion: 0,\n\t\t\ttoVersion: document.state.document.version,\n\t\t\tinitialState: document.state\n\t\t})];\n\t\tif (signer) actions = await signActions(actions, signer, signal);\n\t\tconst jobId = v4();\n\t\tconst jobMeta = buildSingleJobMeta(jobId, meta);\n\t\tconst job = {\n\t\t\tid: jobId,\n\t\t\tkind: \"mutation\",\n\t\t\tdocumentId: document.header.id,\n\t\t\tscope: \"document\",\n\t\t\tbranch: \"main\",\n\t\t\tactions,\n\t\t\toperations: [],\n\t\t\tcreatedAt: (/* @__PURE__ */ new Date()).toISOString(),\n\t\t\tqueueHint: [],\n\t\t\tmaxRetries: 3,\n\t\t\terrorHistory: [],\n\t\t\tmeta: jobMeta\n\t\t};\n\t\tconst jobInfo = {\n\t\t\tid: jobId,\n\t\t\tstatus: JobStatus.PENDING,\n\t\t\tcreatedAtUtcIso,\n\t\t\tconsistencyToken: {\n\t\t\t\tversion: 1,\n\t\t\t\tcreatedAtUtcIso,\n\t\t\t\tcoordinates: []\n\t\t\t},\n\t\t\tmeta: jobMeta\n\t\t};\n\t\tthis.jobTracker.registerJob(jobInfo);\n\t\tthis.emitJobPending(jobInfo.id, jobMeta);\n\t\tawait this.queue.enqueue(job);\n\t\treturn jobInfo;\n\t}\n\tasync deleteDocument(id, signer, signal, meta) {\n\t\tthis.logger.verbose(\"deleteDocument(@id)\", id);\n\t\tconst createdAtUtcIso = (/* @__PURE__ */ new Date()).toISOString();\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\tlet action = deleteDocumentAction(id);\n\t\tif (signer) action = await signAction(action, signer, signal);\n\t\tconst jobId = v4();\n\t\tconst jobMeta = buildSingleJobMeta(jobId, meta);\n\t\tconst job = {\n\t\t\tid: jobId,\n\t\t\tkind: \"mutation\",\n\t\t\tdocumentId: id,\n\t\t\tscope: \"document\",\n\t\t\tbranch: \"main\",\n\t\t\tactions: [action],\n\t\t\toperations: [],\n\t\t\tcreatedAt: (/* @__PURE__ */ new Date()).toISOString(),\n\t\t\tqueueHint: [],\n\t\t\tmaxRetries: 3,\n\t\t\terrorHistory: [],\n\t\t\tmeta: jobMeta\n\t\t};\n\t\tconst jobInfo = {\n\t\t\tid: jobId,\n\t\t\tstatus: JobStatus.PENDING,\n\t\t\tcreatedAtUtcIso,\n\t\t\tconsistencyToken: {\n\t\t\t\tversion: 1,\n\t\t\t\tcreatedAtUtcIso,\n\t\t\t\tcoordinates: []\n\t\t\t},\n\t\t\tmeta: jobMeta\n\t\t};\n\t\tthis.jobTracker.registerJob(jobInfo);\n\t\tthis.emitJobPending(jobInfo.id, jobMeta);\n\t\tawait this.queue.enqueue(job);\n\t\treturn jobInfo;\n\t}\n\tasync execute(docId, branch, actions, signal, meta) {\n\t\tthis.logger.verbose(\"execute(@docId, @branch, @actions)\", docId, branch, actions);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\tconst createdAtUtcIso = (/* @__PURE__ */ new Date()).toISOString();\n\t\tconst scope = getSharedActionScope(actions);\n\t\tconst jobId = v4();\n\t\tconst jobMeta = buildSingleJobMeta(jobId, meta);\n\t\tconst job = {\n\t\t\tid: jobId,\n\t\t\tkind: \"mutation\",\n\t\t\tdocumentId: docId,\n\t\t\tscope,\n\t\t\tbranch,\n\t\t\tactions,\n\t\t\toperations: [],\n\t\t\tcreatedAt: (/* @__PURE__ */ new Date()).toISOString(),\n\t\t\tqueueHint: [],\n\t\t\tmaxRetries: 3,\n\t\t\terrorHistory: [],\n\t\t\tmeta: jobMeta\n\t\t};\n\t\tconst jobInfo = {\n\t\t\tid: jobId,\n\t\t\tstatus: JobStatus.PENDING,\n\t\t\tcreatedAtUtcIso,\n\t\t\tconsistencyToken: {\n\t\t\t\tversion: 1,\n\t\t\t\tcreatedAtUtcIso,\n\t\t\t\tcoordinates: []\n\t\t\t},\n\t\t\tmeta: jobMeta\n\t\t};\n\t\tthis.jobTracker.registerJob(jobInfo);\n\t\tthis.emitJobPending(jobInfo.id, jobMeta);\n\t\tawait this.queue.enqueue(job);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\treturn jobInfo;\n\t}\n\tasync load(docId, branch, operations, signal, meta) {\n\t\tthis.logger.verbose(\"load(@docId, @branch, @count, @operations)\", docId, branch, operations.length, operations);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\tif (operations.length === 0) throw new Error(\"load requires at least one operation\");\n\t\tconst scope = getSharedOperationScope(operations);\n\t\tconst createdAtUtcIso = (/* @__PURE__ */ new Date()).toISOString();\n\t\tconst jobId = v4();\n\t\tconst jobMeta = buildSingleJobMeta(jobId, meta);\n\t\tconst job = {\n\t\t\tid: jobId,\n\t\t\tkind: \"load\",\n\t\t\tdocumentId: docId,\n\t\t\tscope,\n\t\t\tbranch,\n\t\t\tactions: [],\n\t\t\toperations,\n\t\t\tcreatedAt: createdAtUtcIso,\n\t\t\tqueueHint: [],\n\t\t\tmaxRetries: 3,\n\t\t\terrorHistory: [],\n\t\t\tmeta: jobMeta\n\t\t};\n\t\tconst jobInfo = {\n\t\t\tid: jobId,\n\t\t\tstatus: JobStatus.PENDING,\n\t\t\tcreatedAtUtcIso,\n\t\t\tconsistencyToken: {\n\t\t\t\tversion: 1,\n\t\t\t\tcreatedAtUtcIso,\n\t\t\t\tcoordinates: []\n\t\t\t},\n\t\t\tmeta: jobMeta\n\t\t};\n\t\tthis.jobTracker.registerJob(jobInfo);\n\t\tthis.emitJobPending(jobInfo.id, jobMeta);\n\t\tawait this.queue.enqueue(job);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\treturn jobInfo;\n\t}\n\tasync executeBatch(request, signal, meta) {\n\t\tthis.logger.verbose(\"executeBatch(@count jobs)\", request.jobs.length);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\tvalidateBatchRequest(request.jobs);\n\t\tfor (const jobPlan of request.jobs) validateActionScopes(jobPlan);\n\t\tconst createdAtUtcIso = (/* @__PURE__ */ new Date()).toISOString();\n\t\tconst planKeyToJobId = /* @__PURE__ */ new Map();\n\t\tfor (const jobPlan of request.jobs) planKeyToJobId.set(jobPlan.key, v4());\n\t\tconst batchId = v4();\n\t\tconst batchJobIds = [...planKeyToJobId.values()];\n\t\tconst batchMeta = {\n\t\t\t...meta,\n\t\t\tbatchId,\n\t\t\tbatchJobIds\n\t\t};\n\t\tconst jobInfos = /* @__PURE__ */ new Map();\n\t\tfor (const jobPlan of request.jobs) {\n\t\t\tconst jobInfo = {\n\t\t\t\tid: planKeyToJobId.get(jobPlan.key),\n\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\tcreatedAtUtcIso,\n\t\t\t\tconsistencyToken: {\n\t\t\t\t\tversion: 1,\n\t\t\t\t\tcreatedAtUtcIso,\n\t\t\t\t\tcoordinates: []\n\t\t\t\t},\n\t\t\t\tmeta: batchMeta\n\t\t\t};\n\t\t\tthis.jobTracker.registerJob(jobInfo);\n\t\t\tthis.emitJobPending(jobInfo.id, batchMeta);\n\t\t\tjobInfos.set(jobPlan.key, jobInfo);\n\t\t}\n\t\tconst sortedKeys = topologicalSort(request.jobs);\n\t\tconst enqueuedKeys = [];\n\t\ttry {\n\t\t\tfor (const key of sortedKeys) {\n\t\t\t\tthrowIfAborted(signal, () => new AbortError());\n\t\t\t\tconst jobPlan = request.jobs.find((j) => j.key === key);\n\t\t\t\tconst jobId = planKeyToJobId.get(key);\n\t\t\t\tconst queueHint = jobPlan.dependsOn.map((depKey) => planKeyToJobId.get(depKey));\n\t\t\t\tconst job = {\n\t\t\t\t\tid: jobId,\n\t\t\t\t\tkind: \"mutation\",\n\t\t\t\t\tdocumentId: jobPlan.documentId,\n\t\t\t\t\tscope: jobPlan.scope,\n\t\t\t\t\tbranch: jobPlan.branch,\n\t\t\t\t\tactions: jobPlan.actions,\n\t\t\t\t\toperations: [],\n\t\t\t\t\tcreatedAt: createdAtUtcIso,\n\t\t\t\t\tqueueHint,\n\t\t\t\t\tmaxRetries: 3,\n\t\t\t\t\terrorHistory: [],\n\t\t\t\t\tmeta: batchMeta\n\t\t\t\t};\n\t\t\t\tawait this.queue.enqueue(job);\n\t\t\t\tenqueuedKeys.push(key);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tfor (const key of enqueuedKeys) {\n\t\t\t\tconst jobId = planKeyToJobId.get(key);\n\t\t\t\ttry {\n\t\t\t\t\tawait this.queue.remove(jobId);\n\t\t\t\t} catch {}\n\t\t\t}\n\t\t\tfor (const jobInfo of jobInfos.values()) this.jobTracker.markFailed(jobInfo.id, toErrorInfo$1(\"Batch enqueue failed\"));\n\t\t\tthrow error;\n\t\t}\n\t\treturn { jobs: Object.fromEntries(jobInfos) };\n\t}\n\tasync loadBatch(request, signal, meta) {\n\t\tthis.logger.verbose(\"loadBatch(@count jobs)\", request.jobs.length);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\tvalidateBatchLoadRequest(request.jobs);\n\t\tfor (const jobPlan of request.jobs) validateOperationScopes(jobPlan);\n\t\tconst createdAtUtcIso = (/* @__PURE__ */ new Date()).toISOString();\n\t\tconst planKeyToJobId = /* @__PURE__ */ new Map();\n\t\tfor (const jobPlan of request.jobs) planKeyToJobId.set(jobPlan.key, v4());\n\t\tconst batchId = v4();\n\t\tconst batchJobIds = [...planKeyToJobId.values()];\n\t\tconst batchMeta = {\n\t\t\t...meta,\n\t\t\tbatchId,\n\t\t\tbatchJobIds\n\t\t};\n\t\tconst jobInfos = /* @__PURE__ */ new Map();\n\t\tfor (const jobPlan of request.jobs) {\n\t\t\tconst jobInfo = {\n\t\t\t\tid: planKeyToJobId.get(jobPlan.key),\n\t\t\t\tstatus: JobStatus.PENDING,\n\t\t\t\tcreatedAtUtcIso,\n\t\t\t\tconsistencyToken: {\n\t\t\t\t\tversion: 1,\n\t\t\t\t\tcreatedAtUtcIso,\n\t\t\t\t\tcoordinates: []\n\t\t\t\t},\n\t\t\t\tmeta: batchMeta\n\t\t\t};\n\t\t\tthis.jobTracker.registerJob(jobInfo);\n\t\t\tthis.emitJobPending(jobInfo.id, batchMeta);\n\t\t\tjobInfos.set(jobPlan.key, jobInfo);\n\t\t}\n\t\tconst sortedKeys = topologicalSort(request.jobs);\n\t\tconst enqueuedKeys = [];\n\t\ttry {\n\t\t\tfor (const key of sortedKeys) {\n\t\t\t\tthrowIfAborted(signal, () => new AbortError());\n\t\t\t\tconst jobPlan = request.jobs.find((j) => j.key === key);\n\t\t\t\tconst jobId = planKeyToJobId.get(key);\n\t\t\t\tconst queueHint = [...jobPlan.dependsOn.map((depKey) => planKeyToJobId.get(depKey)), ...jobPlan.externalDeps];\n\t\t\t\tconst job = {\n\t\t\t\t\tid: jobId,\n\t\t\t\t\tkind: \"load\",\n\t\t\t\t\tdocumentId: jobPlan.documentId,\n\t\t\t\t\tscope: jobPlan.scope,\n\t\t\t\t\tbranch: jobPlan.branch,\n\t\t\t\t\tactions: [],\n\t\t\t\t\toperations: jobPlan.operations,\n\t\t\t\t\tcreatedAt: createdAtUtcIso,\n\t\t\t\t\tqueueHint,\n\t\t\t\t\tmaxRetries: 3,\n\t\t\t\t\terrorHistory: [],\n\t\t\t\t\tmeta: batchMeta\n\t\t\t\t};\n\t\t\t\tawait this.queue.enqueue(job);\n\t\t\t\tenqueuedKeys.push(key);\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tfor (const key of enqueuedKeys) {\n\t\t\t\tconst jobId = planKeyToJobId.get(key);\n\t\t\t\ttry {\n\t\t\t\t\tawait this.queue.remove(jobId);\n\t\t\t\t} catch {}\n\t\t\t}\n\t\t\tfor (const jobInfo of jobInfos.values()) this.jobTracker.markFailed(jobInfo.id, toErrorInfo$1(\"Batch enqueue failed\"));\n\t\t\tthrow error;\n\t\t}\n\t\treturn { jobs: Object.fromEntries(jobInfos) };\n\t}\n\tasync addRelationship(sourceId, targetId, relationshipType, branch = \"main\", signer, signal) {\n\t\tthis.logger.verbose(\"addRelationship(@sourceId, @targetId, @relationshipType, @branch)\", sourceId, targetId, relationshipType, branch);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\tlet actions = [addRelationshipAction(sourceId, targetId, relationshipType)];\n\t\tif (signer) actions = await signActions(actions, signer, signal);\n\t\treturn await this.execute(sourceId, branch, actions, signal);\n\t}\n\tasync removeRelationship(sourceId, targetId, relationshipType, branch = \"main\", signer, signal) {\n\t\tthis.logger.verbose(\"removeRelationship(@sourceId, @targetId, @relationshipType, @branch)\", sourceId, targetId, relationshipType, branch);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\tlet actions = [removeRelationshipAction(sourceId, targetId, relationshipType)];\n\t\tif (signer) actions = await signActions(actions, signer, signal);\n\t\treturn await this.execute(sourceId, branch, actions, signal);\n\t}\n\tgetJobStatus(jobId, signal) {\n\t\tthis.logger.verbose(\"getJobStatus(@jobId)\", jobId);\n\t\tthrowIfAborted(signal, () => new AbortError());\n\t\tconst jobInfo = this.jobTracker.getJobStatus(jobId);\n\t\tif (!jobInfo) {\n\t\t\tconst now = (/* @__PURE__ */ new Date()).toISOString();\n\t\t\treturn Promise.resolve({\n\t\t\t\tid: jobId,\n\t\t\t\tstatus: JobStatus.FAILED,\n\t\t\t\tcreatedAtUtcIso: now,\n\t\t\t\tcompletedAtUtcIso: now,\n\t\t\t\terror: toErrorInfo$1(\"Job not found\"),\n\t\t\t\tconsistencyToken: {\n\t\t\t\t\tversion: 1,\n\t\t\t\t\tcreatedAtUtcIso: now,\n\t\t\t\t\tcoordinates: []\n\t\t\t\t},\n\t\t\t\tmeta: {\n\t\t\t\t\tbatchId: jobId,\n\t\t\t\t\tbatchJobIds: [jobId]\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t\treturn Promise.resolve(jobInfo);\n\t}\n\tasync findByIds(ids, view, paging, consistencyToken, signal) {\n\t\tthis.logger.verbose(\"findByIds(@count ids)\", ids.length);\n\t\tconst startIndex = paging?.cursor ? parseInt(paging.cursor) || 0 : 0;\n\t\tconst limit = paging?.limit || ids.length;\n\t\tconst pagedIds = ids.slice(startIndex, startIndex + limit);\n\t\tconst results = await this.documentView.getMany(pagedIds, view, consistencyToken, signal);\n\t\tconst nextCursor = startIndex + limit < ids.length ? String(startIndex + limit) : void 0;\n\t\treturn {\n\t\t\tresults,\n\t\t\toptions: paging || {\n\t\t\t\tcursor: \"0\",\n\t\t\t\tlimit: ids.length\n\t\t\t},\n\t\t\tnextCursor\n\t\t};\n\t}\n\tasync findBySlugs(slugs, view, paging, consistencyToken, signal) {\n\t\tthis.logger.verbose(\"findBySlugs(@count slugs)\", slugs.length);\n\t\tconst ids = await this.documentView.resolveSlugs(slugs, view, consistencyToken, signal);\n\t\treturn await this.findByIds(ids, view, paging, consistencyToken, signal);\n\t}\n\tasync findByParentId(parentId, view, paging, consistencyToken, signal) {\n\t\tthis.logger.verbose(\"findByParentId(@parentId)\", parentId);\n\t\tconst ids = (await this.documentIndexer.getOutgoing(parentId, [\"child\"], paging, consistencyToken, signal)).results.map((rel) => rel.targetId);\n\t\treturn await this.findByIds(ids, view, paging, void 0, signal);\n\t}\n\tasync findByType(type, view, paging, consistencyToken, signal) {\n\t\tthis.logger.verbose(\"findByType(@type)\", type);\n\t\treturn await this.documentView.findByType(type, view, paging, consistencyToken, signal);\n\t}\n\temitJobPending(jobId, meta) {\n\t\tconst event = {\n\t\t\tjobId,\n\t\t\tjobMeta: meta\n\t\t};\n\t\tthis.eventBus.emit(ReactorEventTypes.JOB_PENDING, event).catch(() => {});\n\t}\n};\n//#endregion\n//#region src/core/reactor-builder.ts\nvar ReactorBuilder = class {\n\tlogger;\n\tdocumentModels = [];\n\tupgradeManifests = [];\n\tfeatures = { legacyStorageEnabled: false };\n\treadModels = [];\n\treadModelFactories = [];\n\texecutorManager;\n\texecutorConfig = {};\n\twriteCacheConfig;\n\tmigrationStrategy = \"auto\";\n\tsyncBuilder;\n\teventBus;\n\treadModelCoordinator;\n\tsignatureVerifier;\n\tkyselyInstance;\n\tsignalHandlersEnabled = false;\n\tqueueInstance;\n\tchannelScheme;\n\tjwtHandler;\n\tdocumentModelLoader;\n\tshutdownHooks = [];\n\tdriveContainerTypes = DEFAULT_DRIVE_CONTAINER_TYPES;\n\tdocumentModelSpecs = [];\n\tworkerPoolConfig;\n\tresolvedModelManifest;\n\tworkerDbConfig;\n\tworkerSignatureVerifierSpec;\n\tworkerFactory;\n\tprojectionShardConfig;\n\tprojectionWorkerFactory;\n\tinstrumentedPools = [];\n\twithLogger(logger) {\n\t\tthis.logger = logger;\n\t\treturn this;\n\t}\n\twithDocumentModels(models) {\n\t\tthis.documentModels = models;\n\t\treturn this;\n\t}\n\twithUpgradeManifests(manifests) {\n\t\tthis.upgradeManifests = manifests;\n\t\treturn this;\n\t}\n\twithFeatures(features) {\n\t\tthis.features = {\n\t\t\t...this.features,\n\t\t\t...features\n\t\t};\n\t\treturn this;\n\t}\n\twithReadModel(readModel) {\n\t\tthis.readModels.push(readModel);\n\t\treturn this;\n\t}\n\t/**\n\t* Register a factory that builds a pre-ready read model after the reactor's\n\t* internal `operationIndex`, `writeCache`, and processor-manager consistency\n\t* tracker are constructed. Use this for read models (e.g. `BaseReadModel`\n\t* subclasses) that need those dependencies and therefore cannot be built\n\t* before calling `buildModule()`.\n\t*/\n\twithReadModelFactory(factory) {\n\t\tthis.readModelFactories.push(factory);\n\t\treturn this;\n\t}\n\twithReadModelCoordinator(readModelCoordinator) {\n\t\tthis.readModelCoordinator = readModelCoordinator;\n\t\treturn this;\n\t}\n\twithExecutor(executor) {\n\t\tthis.executorManager = executor;\n\t\treturn this;\n\t}\n\twithExecutorConfig(config) {\n\t\tthis.executorConfig = {\n\t\t\t...this.executorConfig,\n\t\t\t...config\n\t\t};\n\t\treturn this;\n\t}\n\twithWriteCacheConfig(config) {\n\t\tthis.writeCacheConfig = config;\n\t\treturn this;\n\t}\n\twithDriveContainerTypes(types) {\n\t\tthis.driveContainerTypes = new Set(types);\n\t\treturn this;\n\t}\n\twithMigrationStrategy(strategy) {\n\t\tthis.migrationStrategy = strategy;\n\t\treturn this;\n\t}\n\twithSync(syncBuilder) {\n\t\tthis.syncBuilder = syncBuilder;\n\t\treturn this;\n\t}\n\twithEventBus(eventBus) {\n\t\tthis.eventBus = eventBus;\n\t\treturn this;\n\t}\n\twithSignatureVerifier(verifier) {\n\t\tthis.signatureVerifier = verifier;\n\t\treturn this;\n\t}\n\twithKysely(kysely) {\n\t\tthis.kyselyInstance = kysely;\n\t\treturn this;\n\t}\n\t/**\n\t* Register an externally-constructed pg.Pool's {@link PoolInstrumentation}\n\t* so it surfaces through {@link ReactorModule.pools}. Use this when the\n\t* caller built the pool itself (e.g. the in-process bench host wiring) so\n\t* pool acquire-wait and pool-stat metrics still emit. The builder also\n\t* registers any pool it constructs internally via {@link createPostgresDatabase}.\n\t*/\n\twithInstrumentedPool(instrumentation) {\n\t\tthis.instrumentedPools.push(instrumentation);\n\t\treturn this;\n\t}\n\twithQueue(queue) {\n\t\tthis.queueInstance = queue;\n\t\treturn this;\n\t}\n\twithChannelScheme(scheme) {\n\t\tthis.channelScheme = scheme;\n\t\treturn this;\n\t}\n\twithJwtHandler(handler) {\n\t\tthis.jwtHandler = handler;\n\t\treturn this;\n\t}\n\twithDocumentModelLoader(loader) {\n\t\tthis.documentModelLoader = loader;\n\t\treturn this;\n\t}\n\twithSignalHandlers() {\n\t\tthis.signalHandlersEnabled = true;\n\t\treturn this;\n\t}\n\t/**\n\t* Register an async cleanup hook to run during graceful shutdown. Hooks fire\n\t* after `reactor.kill()` resolves and before `database.destroy()`, so callers\n\t* that depend on the reactor (e.g. an HTTP API layered on top) can drain\n\t* cleanly before the underlying kysely instance is torn down. Hook errors are\n\t* logged and otherwise ignored — one bad hook cannot strand the rest of the\n\t* shutdown chain.\n\t*/\n\twithShutdownHook(hook) {\n\t\tthis.shutdownHooks.push(hook);\n\t\treturn this;\n\t}\n\twithDocumentModelSpecs(specs) {\n\t\tthis.documentModelSpecs = specs;\n\t\treturn this;\n\t}\n\t/**\n\t* Stores the worker-pool configuration. When `config.enabled === true` the\n\t* builder constructs a {@link WorkerPoolJobExecutorManager} in place of the\n\t* in-process {@link SimpleJobExecutorManager}.\n\t*/\n\twithWorkerPool(config) {\n\t\tthis.workerPoolConfig = config;\n\t\treturn this;\n\t}\n\t/**\n\t* Postgres connection info forwarded to each worker so it can open its own\n\t* pool. Required when `workerPool.enabled === true` unless a custom\n\t* `withWorkerFactory` or `withExecutor` is provided.\n\t*/\n\twithWorkerDbConfig(db) {\n\t\tthis.workerDbConfig = db;\n\t\treturn this;\n\t}\n\t/**\n\t* Factory spec the worker imports to instantiate its signature verifier.\n\t* Required when `workerPool.enabled === true` unless a custom\n\t* `withWorkerFactory` or `withExecutor` is provided.\n\t*/\n\twithWorkerSignatureVerifierSpec(spec) {\n\t\tthis.workerSignatureVerifierSpec = spec;\n\t\treturn this;\n\t}\n\t/**\n\t* Inject a custom {@link WorkerFactory}. When set, the builder skips\n\t* default thread-transport wiring and hands the factory directly to the\n\t* pool manager. Use this in tests or to plug in a different transport\n\t* (e.g. a child-process adapter).\n\t*/\n\twithWorkerFactory(factory) {\n\t\tthis.workerFactory = factory;\n\t\treturn this;\n\t}\n\t/**\n\t* Configure N sharded projection workers. When set, the builder replaces\n\t* the default in-process {@link ReadModelCoordinator} with a\n\t* {@link ProjectionShardManager}. The same `DbConfig` registered via\n\t* {@link withWorkerDbConfig} is reused for the projection workers'\n\t* connection info; only the `poolSize` is overridden by\n\t* {@link ProjectionShardBuilderConfig.poolSize}.\n\t*\n\t* Requires {@link withWorkerDbConfig} (the projection workers need\n\t* connection info to open their own pools). The same model manifest\n\t* resolved from {@link withDocumentModelSpecs} is forwarded.\n\t*/\n\twithProjectionShards(config) {\n\t\tthis.projectionShardConfig = config;\n\t\treturn this;\n\t}\n\t/**\n\t* Inject a custom {@link ProjectionWorkerFactory}. When set, the builder\n\t* skips default thread-transport wiring for the projection shards and\n\t* hands the factory directly to {@link ProjectionShardManager}.\n\t*/\n\twithProjectionWorkerFactory(factory) {\n\t\tthis.projectionWorkerFactory = factory;\n\t\treturn this;\n\t}\n\tgetResolvedModelManifest() {\n\t\treturn this.resolvedModelManifest;\n\t}\n\tasync build() {\n\t\treturn (await this.buildModule()).reactor;\n\t}\n\tasync buildModule() {\n\t\tif (!this.logger) this.logger = new ConsoleLogger([\"reactor\"]);\n\t\tif (this.workerPoolConfig?.enabled) {\n\t\t\tif (this.documentModels.length > 0) throw new Error(\"workerPool.enabled requires withDocumentModelSpecs; remove withDocumentModels() in worker-pool mode.\");\n\t\t\tif (this.documentModelSpecs.length === 0) throw new Error(\"workerPool.enabled requires at least one spec registered via withDocumentModelSpecs.\");\n\t\t\tconst needsDefaultFactory = !this.executorManager && !this.workerFactory;\n\t\t\tif (needsDefaultFactory && !this.workerDbConfig) throw new Error(\"workerPool.enabled requires withWorkerDbConfig (or a custom withWorkerFactory / withExecutor).\");\n\t\t\tif (needsDefaultFactory && !this.workerSignatureVerifierSpec) throw new Error(\"workerPool.enabled requires withWorkerSignatureVerifierSpec (or a custom withWorkerFactory / withExecutor).\");\n\t\t}\n\t\tif (this.documentModelSpecs.length > 0) this.resolvedModelManifest = this.documentModelSpecs.map((input) => {\n\t\t\tif (\"filePath\" in input) return {\n\t\t\t\tdocumentType: \"<unresolved>\",\n\t\t\t\tversion: \"<unresolved>\",\n\t\t\t\tspec: { module: {\n\t\t\t\t\tfilePath: input.filePath,\n\t\t\t\t\texportName: \"documentModel\"\n\t\t\t\t} }\n\t\t\t};\n\t\t\treturn {\n\t\t\t\tdocumentType: \"<unresolved>\",\n\t\t\t\tversion: input.version,\n\t\t\t\tspec: { module: {\n\t\t\t\t\tpackageName: input.packageName,\n\t\t\t\t\texportName: \"documentModel\"\n\t\t\t\t} }\n\t\t\t};\n\t\t});\n\t\tconst documentModelRegistry = new DocumentModelRegistry();\n\t\tif (this.upgradeManifests.length > 0) {\n\t\t\tconst results = documentModelRegistry.registerUpgradeManifests(...this.upgradeManifests);\n\t\t\tfor (const result of results) if (result.status === \"error\") this.logger.error(\"Failed to register upgrade manifest: @error\", result.error.message);\n\t\t}\n\t\tif (this.documentModels.length > 0) {\n\t\t\tconst results = documentModelRegistry.registerModules(...this.documentModels);\n\t\t\tfor (const result of results) if (result.status === \"error\") this.logger.error(\"Failed to register document model: @error\", result.error.message);\n\t\t}\n\t\tconst baseDatabase = this.kyselyInstance ?? (this.workerPoolConfig?.enabled && this.workerDbConfig ? await this.createPostgresDatabase(this.workerDbConfig) : await createDefaultDatabase());\n\t\tif (this.migrationStrategy === \"auto\") {\n\t\t\tconst result = await runMigrations(baseDatabase, REACTOR_SCHEMA);\n\t\t\tif (!result.success && result.error) throw new Error(`Database migration failed: ${result.error.message}`);\n\t\t}\n\t\tconst database = baseDatabase.withSchema(REACTOR_SCHEMA);\n\t\tconst operationStore = new KyselyOperationStore(database);\n\t\tconst keyframeStore = new KyselyKeyframeStore(database);\n\t\tconst eventBus = this.eventBus || new EventBus();\n\t\tconst resolver = this.documentModelLoader ? new DocumentModelResolver(documentModelRegistry, this.documentModelLoader) : new NullDocumentModelResolver(documentModelRegistry);\n\t\tconst queue = this.queueInstance ?? new InMemoryQueue(eventBus, resolver);\n\t\tconst jobTracker = new InMemoryJobTracker(eventBus);\n\t\tconst writeCache = new KyselyWriteCache(keyframeStore, operationStore, documentModelRegistry, {\n\t\t\tmaxDocuments: this.writeCacheConfig?.maxDocuments ?? 100,\n\t\t\tringBufferSize: this.writeCacheConfig?.ringBufferSize ?? 10,\n\t\t\tkeyframeInterval: this.writeCacheConfig?.keyframeInterval ?? 10\n\t\t});\n\t\tawait writeCache.startup();\n\t\tconst operationIndex = new KyselyOperationIndex(database);\n\t\tconst documentMetaCache = new DocumentMetaCache(operationStore, { maxDocuments: 1e3 });\n\t\tawait documentMetaCache.startup();\n\t\tconst collectionMembershipCache = new CollectionMembershipCache(operationIndex);\n\t\tconst executionScope = new KyselyExecutionScope(database, operationStore, operationIndex, keyframeStore, writeCache, documentMetaCache, collectionMembershipCache);\n\t\tlet executorManager = this.executorManager;\n\t\tlet executorStartCount = this.executorConfig.maxConcurrency ?? 1;\n\t\tif (!executorManager) if (this.workerPoolConfig?.enabled) {\n\t\t\tconst poolManager = new WorkerPoolJobExecutorManager(this.workerFactory ?? await this.createDefaultWorkerFactory(this.workerPoolConfig), eventBus, queue, jobTracker, this.logger, resolver, collectionMembershipCache, this.executorConfig.jobTimeoutMs);\n\t\t\texecutorManager = poolManager;\n\t\t\texecutorStartCount = this.workerPoolConfig.numWorkers;\n\t\t\tif (resolver instanceof DocumentModelResolver) resolver.setBroadcastHook((entry) => poolManager.loadModel(entry));\n\t\t} else executorManager = new SimpleJobExecutorManager(() => new SimpleJobExecutor(this.logger, documentModelRegistry, operationStore, eventBus, writeCache, operationIndex, documentMetaCache, collectionMembershipCache, this.driveContainerTypes, this.executorConfig, this.signatureVerifier, executionScope), eventBus, queue, jobTracker, this.logger, resolver, this.executorConfig.jobTimeoutMs);\n\t\tawait executorManager.start(executorStartCount);\n\t\tconst readModelInstances = Array.from(new Set([...this.readModels]));\n\t\tconst documentViewConsistencyTracker = new ConsistencyTracker();\n\t\tconst documentView = new KyselyDocumentView(database, operationStore, operationIndex, writeCache, documentViewConsistencyTracker);\n\t\ttry {\n\t\t\tawait documentView.init();\n\t\t} catch (error) {\n\t\t\tconsole.error(\"Error initializing document view\", error);\n\t\t}\n\t\treadModelInstances.push(documentView);\n\t\tconst documentIndexerConsistencyTracker = new ConsistencyTracker();\n\t\tconst documentIndexer = new KyselyDocumentIndexer(database, operationIndex, writeCache, documentIndexerConsistencyTracker);\n\t\ttry {\n\t\t\tawait documentIndexer.init();\n\t\t} catch (error) {\n\t\t\tconsole.error(\"Error initializing document indexer\", error);\n\t\t}\n\t\treadModelInstances.push(documentIndexer);\n\t\tconst subscriptionManager = new ReactorSubscriptionManager(new DefaultSubscriptionErrorHandler());\n\t\tconst subscriptionNotificationReadModel = new SubscriptionNotificationReadModel(subscriptionManager, documentView);\n\t\tconst processorManagerConsistencyTracker = new ConsistencyTracker();\n\t\tconst processorManager = new ProcessorManager(database, operationIndex, writeCache, processorManagerConsistencyTracker, this.logger, this.driveContainerTypes);\n\t\ttry {\n\t\t\tawait processorManager.init();\n\t\t} catch (error) {\n\t\t\tconsole.error(\"Error initializing processor manager\", error);\n\t\t}\n\t\tfor (const factory of this.readModelFactories) {\n\t\t\tconst readModel = await factory({\n\t\t\t\toperationIndex,\n\t\t\t\twriteCache,\n\t\t\t\tprocessorManagerConsistencyTracker\n\t\t\t});\n\t\t\treadModelInstances.push(readModel);\n\t\t}\n\t\tconst readModelCoordinator = this.readModelCoordinator ? this.readModelCoordinator : this.projectionShardConfig ? await this.createProjectionShardManager(this.projectionShardConfig, eventBus) : new ReadModelCoordinator(eventBus, readModelInstances, [subscriptionNotificationReadModel, processorManager]);\n\t\tconst reactor = new Reactor(this.logger, documentModelRegistry, queue, jobTracker, readModelCoordinator, this.features, documentView, documentIndexer, operationStore, eventBus, executorManager);\n\t\tlet syncModule = void 0;\n\t\tif (this.channelScheme) {\n\t\t\tconst factory = this.channelScheme === ChannelScheme.CONNECT ? new GqlRequestChannelFactory(this.logger, this.jwtHandler, queue) : new GqlResponseChannelFactory(this.logger);\n\t\t\tsyncModule = new SyncBuilder().withChannelFactory(factory).buildModule(reactor, this.logger, operationIndex, eventBus, database, this.driveContainerTypes);\n\t\t\tawait syncModule.syncManager.startup();\n\t\t} else if (this.syncBuilder) {\n\t\t\tsyncModule = this.syncBuilder.buildModule(reactor, this.logger, operationIndex, eventBus, database, this.driveContainerTypes);\n\t\t\tawait syncModule.syncManager.startup();\n\t\t}\n\t\tconst module = {\n\t\t\teventBus,\n\t\t\tdocumentModelRegistry,\n\t\t\tqueue,\n\t\t\tjobTracker,\n\t\t\texecutorManager,\n\t\t\tdatabase,\n\t\t\toperationStore,\n\t\t\tkeyframeStore,\n\t\t\twriteCache,\n\t\t\toperationIndex,\n\t\t\tdocumentView,\n\t\t\tdocumentViewConsistencyTracker,\n\t\t\tdocumentIndexer,\n\t\t\tdocumentIndexerConsistencyTracker,\n\t\t\treadModelCoordinator,\n\t\t\tsubscriptionManager,\n\t\t\tprocessorManager,\n\t\t\tprocessorManagerConsistencyTracker,\n\t\t\tsyncModule,\n\t\t\treactor,\n\t\t\tpools: this.instrumentedPools\n\t\t};\n\t\tif (this.signalHandlersEnabled) this.attachSignalHandlers(module);\n\t\treturn module;\n\t}\n\t/**\n\t* Constructs a {@link ProjectionShardManager} bound to the host event\n\t* bus. Builds the default thread-transport factory unless one was\n\t* injected via {@link withProjectionWorkerFactory}. Calls\n\t* `manager.startup()` so all N workers reach READY before the reactor\n\t* is returned to the caller.\n\t*/\n\tasync createProjectionShardManager(config, eventBus) {\n\t\tif (!this.workerDbConfig) throw new Error(\"withProjectionShards requires withWorkerDbConfig; projection workers need connection info to open their own pools.\");\n\t\tconst models = this.resolvedModelManifest ?? [];\n\t\tconst db = {\n\t\t\t...this.workerDbConfig,\n\t\t\tpoolSize: config.poolSize ?? this.workerDbConfig.poolSize,\n\t\t\tapplicationName: \"reactor-projection-shard\"\n\t\t};\n\t\tconst factory = this.projectionWorkerFactory ?? await this.createDefaultProjectionWorkerFactory();\n\t\tconst poolInstrumentations = [];\n\t\tfor (let i = 0; i < config.shardCount; i++) {\n\t\t\tconst forwarder = createForwardingPoolInstrumentation(`projection-shard-${i}`);\n\t\t\tpoolInstrumentations.push(forwarder);\n\t\t\tthis.instrumentedPools.push(forwarder);\n\t\t}\n\t\tconst { ProjectionShardManager } = await import(\"./projection-shard-manager-_c7orNo5.js\");\n\t\tconst manager = new ProjectionShardManager({\n\t\t\tshardCount: config.shardCount,\n\t\t\tdb,\n\t\t\tmodels,\n\t\t\tpreReadyKinds: config.preReadyKinds,\n\t\t\tpostReadyKinds: config.postReadyKinds,\n\t\t\tfactory,\n\t\t\tlogger: this.logger,\n\t\t\thostBus: eventBus,\n\t\t\tinitTimeoutMs: config.initTimeoutMs,\n\t\t\tshutdownGraceMs: config.shutdownGraceMs,\n\t\t\tdrainTimeoutMs: config.drainTimeoutMs,\n\t\t\tchainDepthReportIntervalMs: config.chainDepthReportIntervalMs,\n\t\t\tpoolInstrumentations\n\t\t});\n\t\tawait manager.startup();\n\t\tthis.shutdownHooks.push(() => manager.shutdown());\n\t\treturn manager;\n\t}\n\tasync createDefaultProjectionWorkerFactory() {\n\t\tconst [{ createProjectionThreadTransport }, { projectionWorkerEntryPath }] = await Promise.all([import(\"./transport-CuogVKN_.js\"), import(\"./projection-worker-wI4PwcV2.js\")]);\n\t\treturn () => createProjectionThreadTransport(projectionWorkerEntryPath);\n\t}\n\t/**\n\t* Default {@link WorkerFactory} used when `workerPool.enabled` is set and\n\t* the caller did not inject `withWorkerFactory`. Each worker spawns a real\n\t* `node:worker_threads` Worker pointing at the compiled `worker/entry.js`.\n\t*/\n\tasync createDefaultWorkerFactory(poolConfig) {\n\t\tconst [{ WorkerHandle }, { createThreadTransport }, { workerEntryPath }] = await Promise.all([\n\t\t\timport(\"./worker-handle-B1w03nRA.js\"),\n\t\t\timport(\"./transport-ByGviWdZ.js\"),\n\t\t\timport(\"./worker-Dq6cUr6a.js\").then((n) => n.n)\n\t\t]);\n\t\tconst db = this.workerDbConfig;\n\t\tconst signatureVerifier = this.workerSignatureVerifierSpec;\n\t\tconst models = this.resolvedModelManifest ?? [];\n\t\tconst logger = this.logger;\n\t\treturn (index) => {\n\t\t\tconst workerId = `reactor-worker-${index}`;\n\t\t\tconst poolInstrumentation = createForwardingPoolInstrumentation(workerId);\n\t\t\tthis.instrumentedPools.push(poolInstrumentation);\n\t\t\treturn new WorkerHandle({\n\t\t\t\tworkerId,\n\t\t\t\tindex,\n\t\t\t\ttransport: createThreadTransport(workerEntryPath),\n\t\t\t\tinitPayload: {\n\t\t\t\t\tpoolConfig,\n\t\t\t\t\tdb,\n\t\t\t\t\tsignatureVerifier,\n\t\t\t\t\tmodels\n\t\t\t\t},\n\t\t\t\tlogger,\n\t\t\t\tpoolInstrumentation\n\t\t\t});\n\t\t};\n\t}\n\t/**\n\t* Builds the parent Kysely instance against a real Postgres server using\n\t* the same {@link DbConfig} the workers receive at init. Used in the\n\t* worker-pool path so the parent reactor and each worker thread share\n\t* storage; PGlite cannot be shared across threads. The constructed pool\n\t* is wrapped with {@link instrumentPgPool} and the resulting\n\t* {@link PoolInstrumentation} is pushed onto {@link instrumentedPools} so\n\t* the reactor module exposes acquire-wait and pool-stat surfaces.\n\t*/\n\tasync createPostgresDatabase(config) {\n\t\tconst { Kysely, PostgresDialect } = await import(\"kysely\");\n\t\tconst Pool = (await import(\"pg\")).default.Pool;\n\t\tconst pool = new Pool({\n\t\t\thost: config.host,\n\t\t\tport: config.port,\n\t\t\tdatabase: config.database,\n\t\t\tuser: config.user,\n\t\t\tpassword: config.password,\n\t\t\tssl: config.ssl ? { rejectUnauthorized: false } : void 0,\n\t\t\tapplication_name: config.applicationName,\n\t\t\tmax: config.poolSize,\n\t\t\tconnectionTimeoutMillis: config.connectionTimeoutMillis,\n\t\t\tidleTimeoutMillis: config.idleTimeoutMillis\n\t\t});\n\t\tthis.instrumentedPools.push(instrumentPgPool(pool, config.applicationName ?? \"reactor-host\"));\n\t\treturn new Kysely({ dialect: new PostgresDialect({ pool }) });\n\t}\n\tattachSignalHandlers(module) {\n\t\tif (typeof globalThis === \"undefined\" || !(\"process\" in globalThis) || typeof globalThis.process.on !== \"function\") return;\n\t\tconst nodeProcess = globalThis.process;\n\t\tconst realExit = nodeProcess.exit.bind(nodeProcess);\n\t\tlet shutdownInProgress = false;\n\t\tlet pendingExitCode;\n\t\tconst handler = async (signal) => {\n\t\t\tif (shutdownInProgress) {\n\t\t\t\tthis.logger.warn(`Received ${signal} again, continuing graceful shutdown...`);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tshutdownInProgress = true;\n\t\t\tnodeProcess.exit = ((code) => {\n\t\t\t\tpendingExitCode ??= code ?? 0;\n\t\t\t});\n\t\t\tthis.logger.info(`Received ${signal}, starting graceful shutdown...`);\n\t\t\tconst status = module.reactor.kill();\n\t\t\ttry {\n\t\t\t\tawait status.completed;\n\t\t\t} catch (error) {\n\t\t\t\tthis.logger.error(\"Shutdown failed waiting for reactor:\", error);\n\t\t\t\tnodeProcess.exit = realExit;\n\t\t\t\trealExit(1);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tfor (const hook of this.shutdownHooks) try {\n\t\t\t\tawait hook();\n\t\t\t} catch (error) {\n\t\t\t\tthis.logger.error(\"Shutdown hook failed:\", error);\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait module.database.destroy();\n\t\t\t} catch (error) {\n\t\t\t\tthis.logger.error(\"Shutdown failed destroying database:\", error);\n\t\t\t\tnodeProcess.exit = realExit;\n\t\t\t\trealExit(1);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthis.logger.info(\"Shutdown complete\");\n\t\t\tnodeProcess.exit = realExit;\n\t\t\trealExit(pendingExitCode ?? 0);\n\t\t};\n\t\tnodeProcess.prependListener(\"SIGINT\", () => void handler(\"SIGINT\"));\n\t\tnodeProcess.prependListener(\"SIGTERM\", () => void handler(\"SIGTERM\"));\n\t}\n};\n//#endregion\n//#region src/signer/passthrough-signer.ts\n/**\n* A no-op signer that returns empty values for all methods.\n* Used when signing is not required.\n*/\nvar PassthroughSigner = class {\n\tpublicKey = {};\n\tsign() {\n\t\treturn Promise.resolve(new Uint8Array(0));\n\t}\n\tverify() {\n\t\treturn Promise.resolve();\n\t}\n\tsignAction() {\n\t\treturn Promise.resolve([\n\t\t\t\"\",\n\t\t\t\"\",\n\t\t\t\"\",\n\t\t\t\"\",\n\t\t\t\"\"\n\t\t]);\n\t}\n};\n//#endregion\n//#region src/core/reactor-client-builder.ts\n/**\n* Builder class for constructing ReactorClient instances with proper configuration\n*/\nvar ReactorClientBuilder = class {\n\tlogger;\n\treactorBuilder;\n\treactor;\n\teventBus;\n\tdocumentIndexer;\n\tdocumentView;\n\tsigner;\n\tsignatureVerifier;\n\tsubscriptionManager;\n\tjobAwaiter;\n\tdocumentModelLoader;\n\t/**\n\t* Sets the logger for the ReactorClient.\n\t* @param logger - The logger to use.\n\t* @returns The ReactorClientBuilder instance.\n\t*/\n\twithLogger(logger) {\n\t\tthis.logger = logger;\n\t\treturn this;\n\t}\n\t/**\n\t* Either this or withReactor must be set.\n\t*/\n\twithReactorBuilder(reactorBuilder) {\n\t\tif (this.reactor) throw new Error(\"Reactor is already set\");\n\t\tthis.reactorBuilder = reactorBuilder;\n\t\treturn this;\n\t}\n\t/**\n\t* Either this or withReactorBuilder must be set.\n\t*/\n\twithReactor(reactor, eventBus, documentIndexer, documentView) {\n\t\tif (this.reactorBuilder) throw new Error(\"ReactorBuilder is already set\");\n\t\tthis.reactor = reactor;\n\t\tthis.eventBus = eventBus;\n\t\tthis.documentIndexer = documentIndexer;\n\t\tthis.documentView = documentView;\n\t\treturn this;\n\t}\n\t/**\n\t* Sets the signer configuration for signing and verifying actions.\n\t*\n\t* @param config - Either an ISigner for signing only, or a SignerConfig for both signing and verification\n\t*/\n\twithSigner(config) {\n\t\tif (\"signer\" in config) {\n\t\t\tthis.signer = config.signer;\n\t\t\tthis.signatureVerifier = config.verifier;\n\t\t} else this.signer = config;\n\t\treturn this;\n\t}\n\twithSubscriptionManager(subscriptionManager) {\n\t\tthis.subscriptionManager = subscriptionManager;\n\t\treturn this;\n\t}\n\twithJobAwaiter(jobAwaiter) {\n\t\tthis.jobAwaiter = jobAwaiter;\n\t\treturn this;\n\t}\n\twithDocumentModelLoader(loader) {\n\t\tthis.documentModelLoader = loader;\n\t\treturn this;\n\t}\n\tasync build() {\n\t\treturn (await this.buildModule()).client;\n\t}\n\tasync buildModule() {\n\t\tif (!this.logger) this.logger = new ConsoleLogger([\"reactor-client\"]);\n\t\tlet reactor;\n\t\tlet eventBus;\n\t\tlet documentIndexer;\n\t\tlet documentView;\n\t\tlet reactorModule;\n\t\tif (this.reactorBuilder) {\n\t\t\tif (this.signatureVerifier) this.reactorBuilder.withSignatureVerifier(this.signatureVerifier);\n\t\t\tif (this.documentModelLoader) this.reactorBuilder.withDocumentModelLoader(this.documentModelLoader);\n\t\t\treactorModule = await this.reactorBuilder.buildModule();\n\t\t\treactor = reactorModule.reactor;\n\t\t\teventBus = reactorModule.eventBus;\n\t\t\tdocumentIndexer = reactorModule.documentIndexer;\n\t\t\tdocumentView = reactorModule.documentView;\n\t\t} else if (this.reactor && this.eventBus && this.documentIndexer && this.documentView) {\n\t\t\treactor = this.reactor;\n\t\t\teventBus = this.eventBus;\n\t\t\tdocumentIndexer = this.documentIndexer;\n\t\t\tdocumentView = this.documentView;\n\t\t\treactorModule = void 0;\n\t\t} else throw new Error(\"Either ReactorBuilder or (Reactor + EventBus + DocumentIndexer + DocumentView) is required\");\n\t\tconst signer = this.signer ?? new PassthroughSigner();\n\t\tconst subscriptionManager = this.subscriptionManager ?? reactorModule?.subscriptionManager ?? new ReactorSubscriptionManager(new DefaultSubscriptionErrorHandler());\n\t\tconst jobAwaiter = this.jobAwaiter ?? new JobAwaiter(eventBus, (jobId, signal) => reactor.getJobStatus(jobId, signal));\n\t\treturn {\n\t\t\tclient: new ReactorClient(this.logger, reactor, signer, subscriptionManager, jobAwaiter, documentIndexer, documentView),\n\t\t\treactor,\n\t\t\teventBus,\n\t\t\tdocumentIndexer,\n\t\t\tdocumentView,\n\t\t\tsigner,\n\t\t\tsubscriptionManager,\n\t\t\tjobAwaiter,\n\t\t\treactorModule\n\t\t};\n\t}\n};\n//#endregion\n//#region src/shared/drive-url.ts\n/**\n* Parse a drive URL to extract drive ID and construct GraphQL endpoint.\n* Preserves any subpath prefix so the result is correct when the reactor is\n* served behind a proxy at a non-root path.\n* e.g., \"http://localhost:4001/d/abc123\" -> { driveId: \"abc123\", graphqlEndpoint: \"http://localhost:4001/graphql/r\" }\n* e.g., \"https://example.com/api/reactor/d/abc123\" -> { ..., graphqlEndpoint: \"https://example.com/api/reactor/graphql/r\" }\n*/\nfunction parseDriveUrl(url) {\n\tconst parsedUrl = new URL(url);\n\tconst driveId = url.split(\"/\").pop() ?? \"\";\n\tconst basePath = parsedUrl.pathname.replace(/\\/d\\/[^/]+\\/?$/, \"\");\n\treturn {\n\t\turl,\n\t\tdriveId,\n\t\tgraphqlEndpoint: `${parsedUrl.protocol}//${parsedUrl.host}${basePath}/graphql/r`\n\t};\n}\n/**\n* Extract drive ID from a drive URL.\n*/\nfunction driveIdFromUrl(url) {\n\treturn url.split(\"/\").pop() ?? \"\";\n}\n//#endregion\n//#region src/admin/passthrough-keyframe-store.ts\nconst passthroughKeyframeStore = {\n\tputKeyframe: () => Promise.resolve(),\n\tfindNearestKeyframe: () => Promise.resolve(void 0),\n\tlistKeyframes: () => Promise.resolve([]),\n\tdeleteKeyframes: () => Promise.resolve(0)\n};\n//#endregion\n//#region src/admin/document-integrity-service.ts\nvar DocumentIntegrityService = class {\n\tkeyframeStore;\n\toperationStore;\n\twriteCache;\n\tdocumentView;\n\tdocumentModelRegistry;\n\tconstructor(keyframeStore, operationStore, writeCache, documentView, documentModelRegistry) {\n\t\tthis.keyframeStore = keyframeStore;\n\t\tthis.operationStore = operationStore;\n\t\tthis.writeCache = writeCache;\n\t\tthis.documentView = documentView;\n\t\tthis.documentModelRegistry = documentModelRegistry;\n\t}\n\tasync validateDocument(documentId, branch = \"main\", signal) {\n\t\tconst keyframeIssues = [];\n\t\tconst snapshotIssues = [];\n\t\tconst replayCache = new KyselyWriteCache(passthroughKeyframeStore, this.operationStore, this.documentModelRegistry, {\n\t\t\tmaxDocuments: 1,\n\t\t\tringBufferSize: 1,\n\t\t\tkeyframeInterval: Number.MAX_SAFE_INTEGER\n\t\t});\n\t\tconst keyframes = await this.keyframeStore.listKeyframes(documentId, void 0, branch, signal);\n\t\tfor (const keyframe of keyframes) {\n\t\t\tthrowIfAborted(signal);\n\t\t\treplayCache.invalidate(documentId, keyframe.scope, branch);\n\t\t\tconst replayedDoc = await replayCache.getState(documentId, keyframe.scope, branch, keyframe.revision, signal);\n\t\t\tconst kfHash = hashDocumentStateForScope(keyframe.document, keyframe.scope);\n\t\t\tconst replayHash = hashDocumentStateForScope(replayedDoc, keyframe.scope);\n\t\t\tif (kfHash !== replayHash) keyframeIssues.push({\n\t\t\t\tscope: keyframe.scope,\n\t\t\t\tbranch,\n\t\t\t\trevision: keyframe.revision,\n\t\t\t\tkeyframeHash: kfHash,\n\t\t\t\treplayedHash: replayHash\n\t\t\t});\n\t\t}\n\t\tlet currentDoc;\n\t\ttry {\n\t\t\tcurrentDoc = await this.documentView.get(documentId);\n\t\t} catch {\n\t\t\treturn {\n\t\t\t\tdocumentId,\n\t\t\t\tisConsistent: keyframeIssues.length === 0,\n\t\t\t\tkeyframeIssues,\n\t\t\t\tsnapshotIssues\n\t\t\t};\n\t\t}\n\t\tconst revisions = await this.operationStore.getRevisions(documentId, branch, signal);\n\t\tconst allScopes = Object.keys(revisions.revision);\n\t\tfor (const scope of allScopes) {\n\t\t\tif (scope === \"document\") continue;\n\t\t\treplayCache.invalidate(documentId, scope, branch);\n\t\t\tlet replayedDoc;\n\t\t\ttry {\n\t\t\t\treplayedDoc = await replayCache.getState(documentId, scope, branch, void 0, signal);\n\t\t\t} catch {\n\t\t\t\tthrowIfAborted(signal);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst snapshotHash = hashDocumentStateForScope(currentDoc, scope);\n\t\t\tconst replayHash = hashDocumentStateForScope(replayedDoc, scope);\n\t\t\tif (snapshotHash !== replayHash) snapshotIssues.push({\n\t\t\t\tscope,\n\t\t\t\tbranch,\n\t\t\t\tsnapshotHash,\n\t\t\t\treplayedHash: replayHash\n\t\t\t});\n\t\t}\n\t\treturn {\n\t\t\tdocumentId,\n\t\t\tisConsistent: keyframeIssues.length === 0 && snapshotIssues.length === 0,\n\t\t\tkeyframeIssues,\n\t\t\tsnapshotIssues\n\t\t};\n\t}\n\tasync rebuildKeyframes(documentId, branch = \"main\", signal) {\n\t\treturn {\n\t\t\tdocumentId,\n\t\t\tkeyframesDeleted: await this.keyframeStore.deleteKeyframes(documentId, void 0, branch, signal),\n\t\t\tscopesInvalidated: 0\n\t\t};\n\t}\n\tasync rebuildSnapshots(documentId, branch = \"main\", signal) {\n\t\tconst scopes = await this.discoverScopes(documentId, branch, signal);\n\t\tfor (const scope of scopes) {\n\t\t\tthrowIfAborted(signal);\n\t\t\tthis.writeCache.invalidate(documentId, scope, branch);\n\t\t}\n\t\treturn {\n\t\t\tdocumentId,\n\t\t\tkeyframesDeleted: 0,\n\t\t\tscopesInvalidated: scopes.length\n\t\t};\n\t}\n\tasync discoverScopes(documentId, branch, signal) {\n\t\tconst revisions = await this.operationStore.getRevisions(documentId, branch, signal);\n\t\treturn Object.keys(revisions.revision);\n\t}\n};\n//#endregion\nexport { BaseReadModel, ChannelError, ChannelErrorSource, ChannelScheme, ConsistencyTracker, DEFAULT_DRIVE_CONTAINER_TYPES, DefaultSubscriptionErrorHandler, DocumentChangeType, DocumentIntegrityService, DocumentModelRegistry, DocumentModelResolver, DriveClient, DuplicateManifestError, DuplicateModuleError, DuplicateOperationError, EventBus, EventBusAggregateError, GqlRequestChannel, GqlRequestChannelFactory, GqlResponseChannel, GqlResponseChannelFactory, SimpleJobExecutor as InMemoryJobExecutor, SimpleJobExecutor, InMemoryJobTracker, InMemoryQueue, IntervalPollTimer, InvalidModuleError, JobAwaiter, JobExecutorEventTypes, JobStatus, KyselyDocumentIndexer, KyselyDocumentView, KyselyKeyframeStore, KyselyOperationStore, KyselySyncCursorStorage, KyselySyncRemoteStorage, KyselyWriteCache, Mailbox, ModuleNotFoundError, NullDocumentModelResolver, OptimisticLockError, PollBehavior, PollingChannelError, ProcessorManager, PropagationMode, QueueEventTypes, REACTOR_SCHEMA, Reactor, ReactorBuilder, ReactorClient, ReactorClientBuilder, ReactorEventTypes, ReactorSubscriptionManager, ReadModelCoordinator, RelationalDbProcessor, RelationshipChangeType, RevisionMismatchError, SimpleJobExecutorManager, SyncBuilder, SyncEventTypes, SyncOperation, SyncOperationAggregateError, SyncOperationStatus, SyncStatus, SyncStatusTracker, addRelationshipAction, batchOperationsByDocument, consolidateSyncOperations, createDocumentAction, createForwardingLogger, createMutableShutdownStatus, createRelationalDb, deleteDocumentAction, documentActions, driveCollectionId, driveIdFromUrl, envelopesToSyncOperations, errorToInfo, getMigrationStatus, instrumentPgPool, makeConsistencyKey, parseDriveUrl, parsePagingOptions, removeRelationshipAction, runMigrations, sanitizeArg, trimMailboxFromAckOrdinal, updateRelationshipAction, upgradeDocumentAction, workerEntryPath };\n\n//# sourceMappingURL=index.js.map","/******************************************************************************\nCopyright (c) Microsoft Corporation.\n\nPermission to use, copy, modify, and/or distribute this software for any\npurpose with or without fee is hereby granted.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH\nREGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY\nAND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,\nINDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM\nLOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR\nOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR\nPERFORMANCE OF THIS SOFTWARE.\n***************************************************************************** */\n/* global Reflect, Promise, SuppressedError, Symbol, Iterator */\n\nvar extendStatics = function(d, b) {\n extendStatics = Object.setPrototypeOf ||\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\n function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };\n return extendStatics(d, b);\n};\n\nexport function __extends(d, b) {\n if (typeof b !== \"function\" && b !== null)\n throw new TypeError(\"Class extends value \" + String(b) + \" is not a constructor or null\");\n extendStatics(d, b);\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n}\n\nexport var __assign = function() {\n __assign = Object.assign || function __assign(t) {\n for (var s, i = 1, n = arguments.length; i < n; i++) {\n s = arguments[i];\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\n }\n return t;\n }\n return __assign.apply(this, arguments);\n}\n\nexport function __rest(s, e) {\n var t = {};\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\n t[p] = s[p];\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {\n if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))\n t[p[i]] = s[p[i]];\n }\n return t;\n}\n\nexport function __decorate(decorators, target, key, desc) {\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n return c > 3 && r && Object.defineProperty(target, key, r), r;\n}\n\nexport function __param(paramIndex, decorator) {\n return function (target, key) { decorator(target, key, paramIndex); }\n}\n\nexport function __esDecorate(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {\n function accept(f) { if (f !== void 0 && typeof f !== \"function\") throw new TypeError(\"Function expected\"); return f; }\n var kind = contextIn.kind, key = kind === \"getter\" ? \"get\" : kind === \"setter\" ? \"set\" : \"value\";\n var target = !descriptorIn && ctor ? contextIn[\"static\"] ? ctor : ctor.prototype : null;\n var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});\n var _, done = false;\n for (var i = decorators.length - 1; i >= 0; i--) {\n var context = {};\n for (var p in contextIn) context[p] = p === \"access\" ? {} : contextIn[p];\n for (var p in contextIn.access) context.access[p] = contextIn.access[p];\n context.addInitializer = function (f) { if (done) throw new TypeError(\"Cannot add initializers after decoration has completed\"); extraInitializers.push(accept(f || null)); };\n var result = (0, decorators[i])(kind === \"accessor\" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);\n if (kind === \"accessor\") {\n if (result === void 0) continue;\n if (result === null || typeof result !== \"object\") throw new TypeError(\"Object expected\");\n if (_ = accept(result.get)) descriptor.get = _;\n if (_ = accept(result.set)) descriptor.set = _;\n if (_ = accept(result.init)) initializers.unshift(_);\n }\n else if (_ = accept(result)) {\n if (kind === \"field\") initializers.unshift(_);\n else descriptor[key] = _;\n }\n }\n if (target) Object.defineProperty(target, contextIn.name, descriptor);\n done = true;\n};\n\nexport function __runInitializers(thisArg, initializers, value) {\n var useValue = arguments.length > 2;\n for (var i = 0; i < initializers.length; i++) {\n value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);\n }\n return useValue ? value : void 0;\n};\n\nexport function __propKey(x) {\n return typeof x === \"symbol\" ? x : \"\".concat(x);\n};\n\nexport function __setFunctionName(f, name, prefix) {\n if (typeof name === \"symbol\") name = name.description ? \"[\".concat(name.description, \"]\") : \"\";\n return Object.defineProperty(f, \"name\", { configurable: true, value: prefix ? \"\".concat(prefix, \" \", name) : name });\n};\n\nexport function __metadata(metadataKey, metadataValue) {\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\n}\n\nexport function __awaiter(thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n}\n\nexport function __generator(thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === \"function\" ? Iterator : Object).prototype);\n return g.next = verb(0), g[\"throw\"] = verb(1), g[\"return\"] = verb(2), typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [op[0] & 2, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n}\n\nexport var __createBinding = Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n var desc = Object.getOwnPropertyDescriptor(m, k);\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\n desc = { enumerable: true, get: function() { return m[k]; } };\n }\n Object.defineProperty(o, k2, desc);\n}) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n});\n\nexport function __exportStar(m, o) {\n for (var p in m) if (p !== \"default\" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);\n}\n\nexport function __values(o) {\n var s = typeof Symbol === \"function\" && Symbol.iterator, m = s && o[s], i = 0;\n if (m) return m.call(o);\n if (o && typeof o.length === \"number\") return {\n next: function () {\n if (o && i >= o.length) o = void 0;\n return { value: o && o[i++], done: !o };\n }\n };\n throw new TypeError(s ? \"Object is not iterable.\" : \"Symbol.iterator is not defined.\");\n}\n\nexport function __read(o, n) {\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\n if (!m) return o;\n var i = m.call(o), r, ar = [], e;\n try {\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\n }\n catch (error) { e = { error: error }; }\n finally {\n try {\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\n }\n finally { if (e) throw e.error; }\n }\n return ar;\n}\n\n/** @deprecated */\nexport function __spread() {\n for (var ar = [], i = 0; i < arguments.length; i++)\n ar = ar.concat(__read(arguments[i]));\n return ar;\n}\n\n/** @deprecated */\nexport function __spreadArrays() {\n for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;\n for (var r = Array(s), k = 0, i = 0; i < il; i++)\n for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)\n r[k] = a[j];\n return r;\n}\n\nexport function __spreadArray(to, from, pack) {\n if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {\n if (ar || !(i in from)) {\n if (!ar) ar = Array.prototype.slice.call(from, 0, i);\n ar[i] = from[i];\n }\n }\n return to.concat(ar || Array.prototype.slice.call(from));\n}\n\nexport function __await(v) {\n return this instanceof __await ? (this.v = v, this) : new __await(v);\n}\n\nexport function __asyncGenerator(thisArg, _arguments, generator) {\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\n return i = Object.create((typeof AsyncIterator === \"function\" ? AsyncIterator : Object).prototype), verb(\"next\"), verb(\"throw\"), verb(\"return\", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;\n function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }\n function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\n function fulfill(value) { resume(\"next\", value); }\n function reject(value) { resume(\"throw\", value); }\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\n}\n\nexport function __asyncDelegator(o) {\n var i, p;\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\n function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }\n}\n\nexport function __asyncValues(o) {\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\n var m = o[Symbol.asyncIterator], i;\n return m ? m.call(o) : (o = typeof __values === \"function\" ? __values(o) : o[Symbol.iterator](), i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i);\n function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }\n function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }\n}\n\nexport function __makeTemplateObject(cooked, raw) {\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\n return cooked;\n};\n\nvar __setModuleDefault = Object.create ? (function(o, v) {\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\n}) : function(o, v) {\n o[\"default\"] = v;\n};\n\nvar ownKeys = function(o) {\n ownKeys = Object.getOwnPropertyNames || function (o) {\n var ar = [];\n for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;\n return ar;\n };\n return ownKeys(o);\n};\n\nexport function __importStar(mod) {\n if (mod && mod.__esModule) return mod;\n var result = {};\n if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== \"default\") __createBinding(result, mod, k[i]);\n __setModuleDefault(result, mod);\n return result;\n}\n\nexport function __importDefault(mod) {\n return (mod && mod.__esModule) ? mod : { default: mod };\n}\n\nexport function __classPrivateFieldGet(receiver, state, kind, f) {\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a getter\");\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot read private member from an object whose class did not declare it\");\n return kind === \"m\" ? f : kind === \"a\" ? f.call(receiver) : f ? f.value : state.get(receiver);\n}\n\nexport function __classPrivateFieldSet(receiver, state, value, kind, f) {\n if (kind === \"m\") throw new TypeError(\"Private method is not writable\");\n if (kind === \"a\" && !f) throw new TypeError(\"Private accessor was defined without a setter\");\n if (typeof state === \"function\" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError(\"Cannot write private member to an object whose class did not declare it\");\n return (kind === \"a\" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;\n}\n\nexport function __classPrivateFieldIn(state, receiver) {\n if (receiver === null || (typeof receiver !== \"object\" && typeof receiver !== \"function\")) throw new TypeError(\"Cannot use 'in' operator on non-object\");\n return typeof state === \"function\" ? receiver === state : state.has(receiver);\n}\n\nexport function __addDisposableResource(env, value, async) {\n if (value !== null && value !== void 0) {\n if (typeof value !== \"object\" && typeof value !== \"function\") throw new TypeError(\"Object expected.\");\n var dispose, inner;\n if (async) {\n if (!Symbol.asyncDispose) throw new TypeError(\"Symbol.asyncDispose is not defined.\");\n dispose = value[Symbol.asyncDispose];\n }\n if (dispose === void 0) {\n if (!Symbol.dispose) throw new TypeError(\"Symbol.dispose is not defined.\");\n dispose = value[Symbol.dispose];\n if (async) inner = dispose;\n }\n if (typeof dispose !== \"function\") throw new TypeError(\"Object not disposable.\");\n if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };\n env.stack.push({ value: value, dispose: dispose, async: async });\n }\n else if (async) {\n env.stack.push({ async: true });\n }\n return value;\n}\n\nvar _SuppressedError = typeof SuppressedError === \"function\" ? SuppressedError : function (error, suppressed, message) {\n var e = new Error(message);\n return e.name = \"SuppressedError\", e.error = error, e.suppressed = suppressed, e;\n};\n\nexport function __disposeResources(env) {\n function fail(e) {\n env.error = env.hasError ? new _SuppressedError(e, env.error, \"An error was suppressed during disposal.\") : e;\n env.hasError = true;\n }\n var r, s = 0;\n function next() {\n while (r = env.stack.pop()) {\n try {\n if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);\n if (r.dispose) {\n var result = r.dispose.call(r.value);\n if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });\n }\n else s |= 1;\n }\n catch (e) {\n fail(e);\n }\n }\n if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();\n if (env.hasError) throw env.error;\n }\n return next();\n}\n\nexport function __rewriteRelativeImportExtension(path, preserveJsx) {\n if (typeof path === \"string\" && /^\\.\\.?\\//.test(path)) {\n return path.replace(/\\.(tsx)$|((?:\\.d)?)((?:\\.[^./]+?)?)\\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {\n return tsx ? preserveJsx ? \".jsx\" : \".js\" : d && (!ext || !cm) ? m : (d + ext + \".\" + cm.toLowerCase() + \"js\");\n });\n }\n return path;\n}\n\nexport default {\n __extends,\n __assign,\n __rest,\n __decorate,\n __param,\n __esDecorate,\n __runInitializers,\n __propKey,\n __setFunctionName,\n __metadata,\n __awaiter,\n __generator,\n __createBinding,\n __exportStar,\n __values,\n __read,\n __spread,\n __spreadArrays,\n __spreadArray,\n __await,\n __asyncGenerator,\n __asyncDelegator,\n __asyncValues,\n __makeTemplateObject,\n __importStar,\n __importDefault,\n __classPrivateFieldGet,\n __classPrivateFieldSet,\n __classPrivateFieldIn,\n __addDisposableResource,\n __disposeResources,\n __rewriteRelativeImportExtension,\n};\n","import { __assign } from \"tslib\";\nimport { parse } from 'graphql';\nvar docCache = new Map();\nvar fragmentSourceMap = new Map();\nvar printFragmentWarnings = true;\nvar experimentalFragmentVariables = false;\nfunction normalize(string) {\n return string.replace(/[\\s,]+/g, ' ').trim();\n}\nfunction cacheKeyFromLoc(loc) {\n return normalize(loc.source.body.substring(loc.start, loc.end));\n}\nfunction processFragments(ast) {\n var seenKeys = new Set();\n var definitions = [];\n ast.definitions.forEach(function (fragmentDefinition) {\n if (fragmentDefinition.kind === 'FragmentDefinition') {\n var fragmentName = fragmentDefinition.name.value;\n var sourceKey = cacheKeyFromLoc(fragmentDefinition.loc);\n var sourceKeySet = fragmentSourceMap.get(fragmentName);\n if (sourceKeySet && !sourceKeySet.has(sourceKey)) {\n if (printFragmentWarnings) {\n console.warn(\"Warning: fragment with name \" + fragmentName + \" already exists.\\n\"\n + \"graphql-tag enforces all fragment names across your application to be unique; read more about\\n\"\n + \"this in the docs: http://dev.apollodata.com/core/fragments.html#unique-names\");\n }\n }\n else if (!sourceKeySet) {\n fragmentSourceMap.set(fragmentName, sourceKeySet = new Set);\n }\n sourceKeySet.add(sourceKey);\n if (!seenKeys.has(sourceKey)) {\n seenKeys.add(sourceKey);\n definitions.push(fragmentDefinition);\n }\n }\n else {\n definitions.push(fragmentDefinition);\n }\n });\n return __assign(__assign({}, ast), { definitions: definitions });\n}\nfunction stripLoc(doc) {\n var workSet = new Set(doc.definitions);\n workSet.forEach(function (node) {\n if (node.loc)\n delete node.loc;\n Object.keys(node).forEach(function (key) {\n var value = node[key];\n if (value && typeof value === 'object') {\n workSet.add(value);\n }\n });\n });\n var loc = doc.loc;\n if (loc) {\n delete loc.startToken;\n delete loc.endToken;\n }\n return doc;\n}\nfunction parseDocument(source) {\n var cacheKey = normalize(source);\n if (!docCache.has(cacheKey)) {\n var parsed = parse(source, {\n experimentalFragmentVariables: experimentalFragmentVariables,\n allowLegacyFragmentVariables: experimentalFragmentVariables\n });\n if (!parsed || parsed.kind !== 'Document') {\n throw new Error('Not a valid GraphQL document.');\n }\n docCache.set(cacheKey, stripLoc(processFragments(parsed)));\n }\n return docCache.get(cacheKey);\n}\nexport function gql(literals) {\n var args = [];\n for (var _i = 1; _i < arguments.length; _i++) {\n args[_i - 1] = arguments[_i];\n }\n if (typeof literals === 'string') {\n literals = [literals];\n }\n var result = literals[0];\n args.forEach(function (arg, i) {\n if (arg && arg.kind === 'Document') {\n result += arg.loc.source.body;\n }\n else {\n result += arg;\n }\n result += literals[i + 1];\n });\n return parseDocument(result);\n}\nexport function resetCaches() {\n docCache.clear();\n fragmentSourceMap.clear();\n}\nexport function disableFragmentWarnings() {\n printFragmentWarnings = false;\n}\nexport function enableExperimentalFragmentVariables() {\n experimentalFragmentVariables = true;\n}\nexport function disableExperimentalFragmentVariables() {\n experimentalFragmentVariables = false;\n}\nvar extras = {\n gql: gql,\n resetCaches: resetCaches,\n disableFragmentWarnings: disableFragmentWarnings,\n enableExperimentalFragmentVariables: enableExperimentalFragmentVariables,\n disableExperimentalFragmentVariables: disableExperimentalFragmentVariables\n};\n(function (gql_1) {\n gql_1.gql = extras.gql, gql_1.resetCaches = extras.resetCaches, gql_1.disableFragmentWarnings = extras.disableFragmentWarnings, gql_1.enableExperimentalFragmentVariables = extras.enableExperimentalFragmentVariables, gql_1.disableExperimentalFragmentVariables = extras.disableExperimentalFragmentVariables;\n})(gql || (gql = {}));\ngql[\"default\"] = gql;\nexport default gql;\n//# sourceMappingURL=index.js.map","import { baseCreateDocument, baseLoadFromInput, baseSaveToFileHandle, createPresignedHeader, createReducer, createState, defaultBaseState, generateId, isDocumentAction, replayDocumentVersioned } from \"@powerhousedao/shared/document-model\";\nimport { Migrator, sql } from \"kysely\";\nimport { BaseReadModel, addRelationshipAction, createDocumentAction, removeRelationshipAction, upgradeDocumentAction } from \"@powerhousedao/reactor\";\nimport { gql } from \"graphql-tag\";\n//#region \\0rolldown/runtime.js\nvar __defProp = Object.defineProperty;\nvar __exportAll = (all, no_symbols) => {\n\tlet target = {};\n\tfor (var name in all) __defProp(target, name, {\n\t\tget: all[name],\n\t\tenumerable: true\n\t});\n\tif (!no_symbols) __defProp(target, Symbol.toStringTag, { value: \"Module\" });\n\treturn target;\n};\n//#endregion\n//#region src/constants.ts\nconst REACTOR_DRIVE_DOCUMENT_TYPE = \"powerhouse/reactor-drive\";\nconst DRIVE_CHILD_RELATIONSHIP_TYPE = \"drive/child\";\nconst REACTOR_DRIVE_FILE_EXTENSION = \"phrd\";\n//#endregion\n//#region src/actions.ts\nfunction setDriveNameAction(input) {\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"SET_DRIVE_NAME\",\n\t\tscope: \"global\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\nfunction setDriveIconAction(input) {\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"SET_DRIVE_ICON\",\n\t\tscope: \"global\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\nfunction setSharingTypeAction(input) {\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"SET_SHARING_TYPE\",\n\t\tscope: \"local\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\nfunction setAvailableOfflineAction(input) {\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"SET_AVAILABLE_OFFLINE\",\n\t\tscope: \"local\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\nfunction addFolderAction(input) {\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"ADD_FOLDER\",\n\t\tscope: \"document\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\nfunction updateFolderAction(input) {\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"UPDATE_FOLDER\",\n\t\tscope: \"document\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\nfunction removeFolderAction(input) {\n\treturn {\n\t\tid: generateId(),\n\t\ttype: \"REMOVE_FOLDER\",\n\t\tscope: \"document\",\n\t\ttimestampUtcMs: (/* @__PURE__ */ new Date()).toISOString(),\n\t\tinput\n\t};\n}\nconst reactorDriveActions = {\n\tsetDriveName: setDriveNameAction,\n\tsetDriveIcon: setDriveIconAction,\n\tsetSharingType: setSharingTypeAction,\n\tsetAvailableOffline: setAvailableOfflineAction,\n\taddFolder: addFolderAction,\n\tupdateFolder: updateFolderAction,\n\tremoveFolder: removeFolderAction\n};\n//#endregion\n//#region src/reducer/drive.ts\nconst reactorDriveStateReducer = (state, action) => {\n\tif (isDocumentAction(action)) return state;\n\tconst typedAction = action;\n\tswitch (typedAction.type) {\n\t\tcase \"SET_DRIVE_NAME\": {\n\t\t\tconst input = typedAction.input;\n\t\t\tstate.global.name = input.name;\n\t\t\treturn state;\n\t\t}\n\t\tcase \"SET_DRIVE_ICON\": {\n\t\t\tconst input = typedAction.input;\n\t\t\tstate.global.icon = input.icon;\n\t\t\treturn state;\n\t\t}\n\t\tcase \"SET_SHARING_TYPE\": {\n\t\t\tconst input = typedAction.input;\n\t\t\tstate.local.sharingType = input.sharingType;\n\t\t\treturn state;\n\t\t}\n\t\tcase \"SET_AVAILABLE_OFFLINE\": {\n\t\t\tconst input = typedAction.input;\n\t\t\tstate.local.availableOffline = input.availableOffline;\n\t\t\treturn state;\n\t\t}\n\t\tcase \"ADD_FOLDER\":\n\t\tcase \"UPDATE_FOLDER\":\n\t\tcase \"REMOVE_FOLDER\": return state;\n\t\tdefault: return state;\n\t}\n};\n//#endregion\n//#region src/module.ts\nconst initialGlobalState = {\n\tname: \"\",\n\ticon: null\n};\nconst initialLocalState = {\n\tsharingType: \"private\",\n\tavailableOffline: false\n};\nconst reactorDriveCreateState = (state) => {\n\treturn {\n\t\t...defaultBaseState(),\n\t\tglobal: {\n\t\t\t...initialGlobalState,\n\t\t\t...state?.global\n\t\t},\n\t\tlocal: {\n\t\t\t...initialLocalState,\n\t\t\t...state?.local\n\t\t}\n\t};\n};\nconst reactorDriveCreateDocument = (state) => {\n\treturn baseCreateDocument(reactorDriveCreateState, state, REACTOR_DRIVE_DOCUMENT_TYPE);\n};\nconst reactorDriveSaveToFileHandle = (document, input) => {\n\treturn baseSaveToFileHandle(document, input);\n};\nconst reactorDriveLoadFromInput = (input) => {\n\treturn baseLoadFromInput(input, reactorDriveDocumentReducer);\n};\nconst isReactorDriveState = (state) => {\n\treturn typeof state === \"object\" && state !== null && \"global\" in state && \"local\" in state;\n};\nconst assertIsReactorDriveState = (state) => {\n\tif (!isReactorDriveState(state)) throw new Error(\"Not a reactor-drive state\");\n};\nconst isReactorDriveDocument = (document) => {\n\treturn typeof document === \"object\" && document !== null && \"header\" in document && document.header.documentType === \"powerhouse/reactor-drive\";\n};\nconst assertIsReactorDriveDocument = (document) => {\n\tif (!isReactorDriveDocument(document)) throw new Error(\"Not a reactor-drive document\");\n};\nconst reactorDriveDocumentReducer = createReducer(reactorDriveStateReducer);\nconst reactorDriveDocumentGlobalState = {\n\tid: REACTOR_DRIVE_DOCUMENT_TYPE,\n\tname: \"ReactorDrive\",\n\textension: REACTOR_DRIVE_FILE_EXTENSION,\n\tdescription: \"\",\n\tauthor: {\n\t\tname: \"Powerhouse Inc\",\n\t\twebsite: \"https://www.powerhouse.inc/\"\n\t},\n\tspecifications: [{\n\t\tversion: 1,\n\t\tchangeLog: [],\n\t\tstate: {\n\t\t\tglobal: {\n\t\t\t\tschema: \"type ReactorDriveState {\\n name: String!\\n icon: String\\n}\",\n\t\t\t\tinitialValue: JSON.stringify(JSON.stringify(initialGlobalState)),\n\t\t\t\texamples: []\n\t\t\t},\n\t\t\tlocal: {\n\t\t\t\tschema: \"type ReactorDriveLocalState {\\n sharingType: String!\\n availableOffline: Boolean!\\n}\",\n\t\t\t\tinitialValue: JSON.stringify(JSON.stringify(initialLocalState)),\n\t\t\t\texamples: []\n\t\t\t}\n\t\t},\n\t\tmodules: [{\n\t\t\tid: \"reactor-drive/base-operations\",\n\t\t\tname: \"base_operations\",\n\t\t\tdescription: \"\",\n\t\t\toperations: [\n\t\t\t\t{\n\t\t\t\t\tid: \"SET_DRIVE_NAME\",\n\t\t\t\t\tname: \"SET_DRIVE_NAME\",\n\t\t\t\t\tdescription: \"\",\n\t\t\t\t\tschema: \"input SetDriveNameInput { name: String! }\",\n\t\t\t\t\ttemplate: \"\",\n\t\t\t\t\treducer: \"\",\n\t\t\t\t\terrors: [],\n\t\t\t\t\texamples: [],\n\t\t\t\t\tscope: \"global\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tid: \"SET_DRIVE_ICON\",\n\t\t\t\t\tname: \"SET_DRIVE_ICON\",\n\t\t\t\t\tdescription: \"\",\n\t\t\t\t\tschema: \"input SetDriveIconInput { icon: String }\",\n\t\t\t\t\ttemplate: \"\",\n\t\t\t\t\treducer: \"\",\n\t\t\t\t\terrors: [],\n\t\t\t\t\texamples: [],\n\t\t\t\t\tscope: \"global\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tid: \"SET_SHARING_TYPE\",\n\t\t\t\t\tname: \"SET_SHARING_TYPE\",\n\t\t\t\t\tdescription: \"\",\n\t\t\t\t\tschema: \"input SetSharingTypeInput { sharingType: String! }\",\n\t\t\t\t\ttemplate: \"\",\n\t\t\t\t\treducer: \"\",\n\t\t\t\t\terrors: [],\n\t\t\t\t\texamples: [],\n\t\t\t\t\tscope: \"local\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tid: \"SET_AVAILABLE_OFFLINE\",\n\t\t\t\t\tname: \"SET_AVAILABLE_OFFLINE\",\n\t\t\t\t\tdescription: \"\",\n\t\t\t\t\tschema: \"input SetAvailableOfflineInput { availableOffline: Boolean! }\",\n\t\t\t\t\ttemplate: \"\",\n\t\t\t\t\treducer: \"\",\n\t\t\t\t\terrors: [],\n\t\t\t\t\texamples: [],\n\t\t\t\t\tscope: \"local\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tid: \"ADD_FOLDER\",\n\t\t\t\t\tname: \"ADD_FOLDER\",\n\t\t\t\t\tdescription: \"\",\n\t\t\t\t\tschema: \"input AddFolderInput { folderId: String! parentFolderId: String name: String! }\",\n\t\t\t\t\ttemplate: \"\",\n\t\t\t\t\treducer: \"\",\n\t\t\t\t\terrors: [],\n\t\t\t\t\texamples: [],\n\t\t\t\t\tscope: \"document\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tid: \"UPDATE_FOLDER\",\n\t\t\t\t\tname: \"UPDATE_FOLDER\",\n\t\t\t\t\tdescription: \"\",\n\t\t\t\t\tschema: \"input UpdateFolderInput { folderId: String! name: String parentFolderId: String }\",\n\t\t\t\t\ttemplate: \"\",\n\t\t\t\t\treducer: \"\",\n\t\t\t\t\terrors: [],\n\t\t\t\t\texamples: [],\n\t\t\t\t\tscope: \"document\"\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tid: \"REMOVE_FOLDER\",\n\t\t\t\t\tname: \"REMOVE_FOLDER\",\n\t\t\t\t\tdescription: \"\",\n\t\t\t\t\tschema: \"input RemoveFolderInput { folderId: String! }\",\n\t\t\t\t\ttemplate: \"\",\n\t\t\t\t\treducer: \"\",\n\t\t\t\t\terrors: [],\n\t\t\t\t\texamples: [],\n\t\t\t\t\tscope: \"document\"\n\t\t\t\t}\n\t\t\t]\n\t\t}]\n\t}]\n};\nconst reactorDriveDocumentModelModule = {\n\tactions: reactorDriveActions,\n\treducer: reactorDriveDocumentReducer,\n\tdocumentModel: createState(defaultBaseState(), reactorDriveDocumentGlobalState),\n\tutils: {\n\t\tfileExtension: REACTOR_DRIVE_FILE_EXTENSION,\n\t\tcreateState: reactorDriveCreateState,\n\t\tcreateDocument: reactorDriveCreateDocument,\n\t\tloadFromInput: reactorDriveLoadFromInput,\n\t\tsaveToFileHandle: reactorDriveSaveToFileHandle,\n\t\tisStateOfType: isReactorDriveState,\n\t\tassertIsStateOfType: assertIsReactorDriveState,\n\t\tisDocumentOfType: isReactorDriveDocument,\n\t\tassertIsDocumentOfType: assertIsReactorDriveDocument\n\t}\n};\n//#endregion\n//#region src/schema/migrations/0001_drive_node.ts\nvar _0001_drive_node_exports = /* @__PURE__ */ __exportAll({\n\tdown: () => down$1,\n\tup: () => up$1\n});\nasync function up$1(db) {\n\tawait db.schema.createTable(\"DriveNode\").ifNotExists().addColumn(\"driveId\", \"text\", (col) => col.notNull()).addColumn(\"id\", \"text\", (col) => col.notNull()).addColumn(\"kind\", \"text\", (col) => col.notNull()).addColumn(\"name\", \"text\", (col) => col.notNull()).addColumn(\"requestedName\", \"text\", (col) => col.notNull()).addColumn(\"parentFolder\", \"text\").addColumn(\"documentType\", \"text\").addColumn(\"createdAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addColumn(\"updatedAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).addPrimaryKeyConstraint(\"pk_drive_node\", [\"driveId\", \"id\"]).addCheckConstraint(\"chk_drive_node_document_type\", sql`(kind = 'file' AND \"documentType\" IS NOT NULL) OR (kind = 'folder' AND \"documentType\" IS NULL)`).execute();\n\tawait db.schema.createIndex(\"idx_drive_node_parent_name\").ifNotExists().on(\"DriveNode\").columns([\n\t\t\"driveId\",\n\t\t\"parentFolder\",\n\t\t\"name\"\n\t]).execute();\n\tawait db.schema.createIndex(\"idx_drive_node_parent_kind_id\").ifNotExists().on(\"DriveNode\").columns([\n\t\t\"driveId\",\n\t\t\"parentFolder\",\n\t\t\"kind\",\n\t\t\"id\"\n\t]).execute();\n}\nasync function down$1(db) {\n\tawait db.schema.dropTable(\"DriveNode\").execute();\n}\n//#endregion\n//#region src/schema/migrations/0002_document_name.ts\nvar _0002_document_name_exports = /* @__PURE__ */ __exportAll({\n\tdown: () => down,\n\tup: () => up\n});\nasync function up(db) {\n\tawait db.schema.createTable(\"DocumentName\").ifNotExists().addColumn(\"docId\", \"text\", (col) => col.primaryKey()).addColumn(\"name\", \"text\", (col) => col.notNull()).addColumn(\"updatedAt\", \"timestamptz\", (col) => col.notNull().defaultTo(sql`NOW()`)).execute();\n}\nasync function down(db) {\n\tawait db.schema.dropTable(\"DocumentName\").execute();\n}\n//#endregion\n//#region src/schema/migrations/migrator.ts\nconst migrations = {\n\t\"0001_drive_node\": _0001_drive_node_exports,\n\t\"0002_document_name\": _0002_document_name_exports\n};\nconst REACTOR_DRIVE_MIGRATION_TABLE = \"kysely_migration_reactor_drive\";\nconst REACTOR_DRIVE_MIGRATION_LOCK_TABLE = \"kysely_migration_reactor_drive_lock\";\nvar ProgrammaticMigrationProvider = class {\n\tgetMigrations() {\n\t\treturn Promise.resolve(migrations);\n\t}\n};\nasync function runReactorDriveMigrations(db, schema) {\n\ttry {\n\t\tawait sql`CREATE SCHEMA IF NOT EXISTS ${sql.id(schema)}`.execute(db);\n\t} catch (error) {\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\tmigrationsExecuted: [],\n\t\t\terror: error instanceof Error ? error : /* @__PURE__ */ new Error(\"Failed to create schema\")\n\t\t};\n\t}\n\tconst migrator = new Migrator({\n\t\tdb: db.withSchema(schema),\n\t\tprovider: new ProgrammaticMigrationProvider(),\n\t\tmigrationTableSchema: schema,\n\t\tmigrationTableName: REACTOR_DRIVE_MIGRATION_TABLE,\n\t\tmigrationLockTableName: REACTOR_DRIVE_MIGRATION_LOCK_TABLE\n\t});\n\tlet error;\n\tlet results;\n\ttry {\n\t\tconst result = await migrator.migrateToLatest();\n\t\terror = result.error;\n\t\tresults = result.results;\n\t} catch (e) {\n\t\terror = e;\n\t\tresults = [];\n\t}\n\tconst migrationsExecuted = results?.map((result) => result.migrationName) ?? [];\n\tif (error) return {\n\t\tsuccess: false,\n\t\tmigrationsExecuted,\n\t\terror: error instanceof Error ? error : /* @__PURE__ */ new Error(\"Unknown migration error\")\n\t};\n\treturn {\n\t\tsuccess: true,\n\t\tmigrationsExecuted\n\t};\n}\nasync function getReactorDriveMigrationStatus(db, schema) {\n\treturn await new Migrator({\n\t\tdb: db.withSchema(schema),\n\t\tprovider: new ProgrammaticMigrationProvider(),\n\t\tmigrationTableSchema: schema,\n\t\tmigrationTableName: REACTOR_DRIVE_MIGRATION_TABLE,\n\t\tmigrationLockTableName: REACTOR_DRIVE_MIGRATION_LOCK_TABLE\n\t}).getMigrations();\n}\n//#endregion\n//#region src/processors/utils/collisions.ts\n/**\n* Deterministic per-folder name collision rule. Given a desired name and the\n* set of names already taken by siblings, returns the next available name as\n* `requested`, `requested (2)`, `requested (3)`, etc.\n*\n* The rule matches the legacy document-drive module so that migration from\n* the legacy state produces stable suffixes.\n*/\nfunction resolveCollision(requested, takenNames) {\n\tconst taken = /* @__PURE__ */ new Set();\n\tfor (const name of takenNames) taken.add(name);\n\tif (!taken.has(requested)) return requested;\n\tlet suffix = 2;\n\twhile (taken.has(`${requested} (${suffix})`)) suffix += 1;\n\treturn `${requested} (${suffix})`;\n}\n//#endregion\n//#region src/processors/node-processor.ts\nconst NAME_ACTION_TYPES = new Set([\n\t\"CREATE_DOCUMENT\",\n\t\"UPGRADE_DOCUMENT\",\n\t\"SET_NAME\"\n]);\nconst STRUCTURE_ACTION_TYPES = new Set([\n\t\"ADD_RELATIONSHIP\",\n\t\"REMOVE_RELATIONSHIP\",\n\t\"ADD_FOLDER\",\n\t\"UPDATE_FOLDER\",\n\t\"REMOVE_FOLDER\"\n]);\nvar NodeProcessor = class extends BaseReadModel {\n\tdriveDb;\n\tbaseDb;\n\tschema;\n\tconstructor(baseDb, schema, operationIndex, writeCache, consistencyTracker) {\n\t\tconst scopedDb = baseDb.withSchema(schema);\n\t\tsuper(scopedDb, operationIndex, writeCache, consistencyTracker, {\n\t\t\treadModelId: \"reactor-drive-node-processor\",\n\t\t\trebuildStateOnInit: false\n\t\t});\n\t\tthis.driveDb = scopedDb;\n\t\tthis.baseDb = baseDb;\n\t\tthis.schema = schema;\n\t}\n\tasync init() {\n\t\tconst result = await runReactorDriveMigrations(this.baseDb, this.schema);\n\t\tif (!result.success && result.error) throw new Error(`Reactor drive migrations failed: ${result.error.message}`);\n\t\tawait super.init();\n\t}\n\tasync commitOperations(items) {\n\t\tawait this.driveDb.transaction().execute(async (trx) => {\n\t\t\tfor (const item of items) {\n\t\t\t\tconst actionType = item.operation.action.type;\n\t\t\t\tif (NAME_ACTION_TYPES.has(actionType)) {\n\t\t\t\t\tawait this.applyNameOperation(trx, item);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (STRUCTURE_ACTION_TYPES.has(actionType)) {\n\t\t\t\t\tawait this.applyStructureOperation(trx, item);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (actionType === \"DELETE_DOCUMENT\") await this.applyDeleteDocument(trx, item);\n\t\t\t}\n\t\t});\n\t}\n\tasync applyNameOperation(trx, item) {\n\t\tconst action = item.operation.action;\n\t\tconst docId = item.context.documentId;\n\t\tlet name;\n\t\tif (action.type === \"CREATE_DOCUMENT\") name = action.input.name;\n\t\telse if (action.type === \"UPGRADE_DOCUMENT\") name = (action.input.initialState ?? {}).header?.name;\n\t\telse if (action.type === \"SET_NAME\") name = action.input.name;\n\t\tif (name === void 0) return;\n\t\tawait this.upsertDocumentName(trx, docId, name);\n\t\tconst linkedRows = await trx.selectFrom(\"DriveNode\").selectAll().where(\"id\", \"=\", docId).where(\"kind\", \"=\", \"file\").execute();\n\t\tfor (const row of linkedRows) {\n\t\t\tconst resolved = await this.resolveSiblingName(trx, row.driveId, row.parentFolder, name, docId);\n\t\t\tawait trx.updateTable(\"DriveNode\").set({\n\t\t\t\tname: resolved,\n\t\t\t\trequestedName: name,\n\t\t\t\tupdatedAt: /* @__PURE__ */ new Date()\n\t\t\t}).where(\"driveId\", \"=\", row.driveId).where(\"id\", \"=\", docId).execute();\n\t\t}\n\t}\n\tasync applyStructureOperation(trx, item) {\n\t\tif (item.context.documentType !== \"powerhouse/reactor-drive\") return;\n\t\tconst action = item.operation.action;\n\t\tconst driveId = item.context.documentId;\n\t\tif (action.type === \"ADD_RELATIONSHIP\") {\n\t\t\tconst input = action.input;\n\t\t\tif (input.relationshipType !== \"drive/child\") return;\n\t\t\tawait this.handleAddFileRelationship(trx, driveId, input);\n\t\t\treturn;\n\t\t}\n\t\tif (action.type === \"REMOVE_RELATIONSHIP\") {\n\t\t\tconst input = action.input;\n\t\t\tif (input.relationshipType !== \"drive/child\") return;\n\t\t\tawait trx.deleteFrom(\"DriveNode\").where(\"driveId\", \"=\", driveId).where(\"id\", \"=\", input.targetId).execute();\n\t\t\treturn;\n\t\t}\n\t\tif (action.type === \"ADD_FOLDER\") {\n\t\t\tawait this.handleAddFolder(trx, driveId, action.input);\n\t\t\treturn;\n\t\t}\n\t\tif (action.type === \"UPDATE_FOLDER\") {\n\t\t\tawait this.handleUpdateFolder(trx, driveId, action.input);\n\t\t\treturn;\n\t\t}\n\t\tif (action.type === \"REMOVE_FOLDER\") {\n\t\t\tconst input = action.input;\n\t\t\tawait trx.deleteFrom(\"DriveNode\").where(\"driveId\", \"=\", driveId).where(\"id\", \"=\", input.folderId).execute();\n\t\t}\n\t}\n\tasync applyDeleteDocument(trx, item) {\n\t\tconst docId = item.operation.action.input.documentId || item.context.documentId;\n\t\tawait trx.deleteFrom(\"DriveNode\").where(\"id\", \"=\", docId).execute();\n\t\tawait trx.deleteFrom(\"DocumentName\").where(\"docId\", \"=\", docId).execute();\n\t}\n\tasync handleAddFileRelationship(trx, driveId, input) {\n\t\tconst metadata = this.parseFileMetadata(input);\n\t\tconst parentFolder = metadata.parentFolderId ?? null;\n\t\tconst documentType = metadata.documentType;\n\t\tconst requestedName = await this.lookupDocumentName(trx, input.targetId) ?? \"\";\n\t\tconst resolved = await this.resolveSiblingName(trx, driveId, parentFolder, requestedName, input.targetId);\n\t\tawait trx.insertInto(\"DriveNode\").values({\n\t\t\tdriveId,\n\t\t\tid: input.targetId,\n\t\t\tkind: \"file\",\n\t\t\tname: resolved,\n\t\t\trequestedName,\n\t\t\tparentFolder,\n\t\t\tdocumentType\n\t\t}).onConflict((oc) => oc.columns([\"driveId\", \"id\"]).doUpdateSet({\n\t\t\tparentFolder,\n\t\t\tname: resolved,\n\t\t\trequestedName,\n\t\t\tkind: \"file\",\n\t\t\tdocumentType,\n\t\t\tupdatedAt: /* @__PURE__ */ new Date()\n\t\t})).execute();\n\t}\n\tasync handleAddFolder(trx, driveId, input) {\n\t\tconst parentFolder = input.parentFolderId ?? null;\n\t\tconst resolved = await this.resolveSiblingName(trx, driveId, parentFolder, input.name, input.folderId);\n\t\tawait trx.insertInto(\"DriveNode\").values({\n\t\t\tdriveId,\n\t\t\tid: input.folderId,\n\t\t\tkind: \"folder\",\n\t\t\tname: resolved,\n\t\t\trequestedName: input.name,\n\t\t\tparentFolder,\n\t\t\tdocumentType: null\n\t\t}).onConflict((oc) => oc.columns([\"driveId\", \"id\"]).doUpdateSet({\n\t\t\tparentFolder,\n\t\t\tname: resolved,\n\t\t\trequestedName: input.name,\n\t\t\tkind: \"folder\",\n\t\t\tdocumentType: null,\n\t\t\tupdatedAt: /* @__PURE__ */ new Date()\n\t\t})).execute();\n\t}\n\tasync handleUpdateFolder(trx, driveId, input) {\n\t\tconst row = await trx.selectFrom(\"DriveNode\").selectAll().where(\"driveId\", \"=\", driveId).where(\"id\", \"=\", input.folderId).executeTakeFirst();\n\t\tif (!row) return;\n\t\tconst nextParentFolder = input.parentFolderId === void 0 ? row.parentFolder : input.parentFolderId ?? null;\n\t\tconst nextRequestedName = typeof input.name === \"string\" ? input.name : row.requestedName;\n\t\tconst resolved = await this.resolveSiblingName(trx, driveId, nextParentFolder, nextRequestedName, input.folderId);\n\t\tawait trx.updateTable(\"DriveNode\").set({\n\t\t\tparentFolder: nextParentFolder,\n\t\t\tname: resolved,\n\t\t\trequestedName: nextRequestedName,\n\t\t\tupdatedAt: /* @__PURE__ */ new Date()\n\t\t}).where(\"driveId\", \"=\", driveId).where(\"id\", \"=\", input.folderId).execute();\n\t}\n\tasync resolveSiblingName(trx, driveId, parentFolder, requested, excludeId) {\n\t\tlet query = trx.selectFrom(\"DriveNode\").select(\"name\").where(\"driveId\", \"=\", driveId).where(\"id\", \"!=\", excludeId);\n\t\tquery = parentFolder === null ? query.where(\"parentFolder\", \"is\", null) : query.where(\"parentFolder\", \"=\", parentFolder);\n\t\treturn resolveCollision(requested, (await query.execute()).map((r) => r.name));\n\t}\n\tasync lookupDocumentName(trx, docId) {\n\t\treturn (await trx.selectFrom(\"DocumentName\").select(\"name\").where(\"docId\", \"=\", docId).executeTakeFirst())?.name;\n\t}\n\tparseFileMetadata(input) {\n\t\tconst metadata = input.metadata;\n\t\tif (!metadata || typeof metadata !== \"object\" || metadata.kind !== \"file\" || typeof metadata.documentType !== \"string\") throw new Error(`ADD_RELATIONSHIP for target ${input.targetId}: missing or invalid drive/child file metadata (expected { kind: \"file\", parentFolderId, documentType })`);\n\t\tconst typed = metadata;\n\t\treturn {\n\t\t\tkind: \"file\",\n\t\t\tparentFolderId: typed.parentFolderId ?? null,\n\t\t\tdocumentType: typed.documentType\n\t\t};\n\t}\n\tasync upsertDocumentName(trx, docId, name) {\n\t\tawait trx.insertInto(\"DocumentName\").values({\n\t\t\tdocId,\n\t\t\tname\n\t\t}).onConflict((oc) => oc.column(\"docId\").doUpdateSet({\n\t\t\tname,\n\t\t\tupdatedAt: /* @__PURE__ */ new Date()\n\t\t})).execute();\n\t}\n};\n//#endregion\n//#region src/read-model/drive-node-view.ts\nconst DEFAULT_LIMIT$1 = 100;\nfunction parseListChildrenPaging(paging) {\n\tif (paging === void 0) return {\n\t\tlimit: DEFAULT_LIMIT$1,\n\t\tcursor: null\n\t};\n\tif (!Number.isInteger(paging.limit) || paging.limit < 1) throw new Error(`Invalid paging limit: ${String(paging.limit)} (must be an integer >= 1)`);\n\tif (paging.cursor === \"\") return {\n\t\tlimit: paging.limit,\n\t\tcursor: null\n\t};\n\treturn {\n\t\tlimit: paging.limit,\n\t\tcursor: decodeKeysetCursor(paging.cursor)\n\t};\n}\nfunction encodeKeysetCursor(createdAt, id) {\n\treturn globalThis.btoa(JSON.stringify({\n\t\tcreatedAt: createdAt.toISOString(),\n\t\tid\n\t}));\n}\nfunction decodeKeysetCursor(cursor) {\n\tlet decoded;\n\ttry {\n\t\tdecoded = globalThis.atob(cursor);\n\t} catch {\n\t\tthrow new Error(`Invalid paging cursor: ${JSON.stringify(cursor)} (must be a keyset cursor returned by a prior page)`);\n\t}\n\tlet parsed;\n\ttry {\n\t\tparsed = JSON.parse(decoded);\n\t} catch {\n\t\tthrow new Error(`Invalid paging cursor: ${JSON.stringify(cursor)} (must be a keyset cursor returned by a prior page)`);\n\t}\n\tif (parsed === null || typeof parsed !== \"object\" || typeof parsed.createdAt !== \"string\" || typeof parsed.id !== \"string\") throw new Error(`Invalid paging cursor: ${JSON.stringify(cursor)} (must be a keyset cursor returned by a prior page)`);\n\tconst createdAt = new Date(parsed.createdAt);\n\tif (Number.isNaN(createdAt.getTime())) throw new Error(`Invalid paging cursor: ${JSON.stringify(cursor)} (createdAt is not a valid timestamp)`);\n\treturn {\n\t\tcreatedAt,\n\t\tid: parsed.id\n\t};\n}\nvar DriveNodeView = class {\n\tconstructor(db) {\n\t\tthis.db = db;\n\t}\n\tasync getNode(driveId, nodeId) {\n\t\tconst row = await this.db.selectFrom(\"DriveNode\").selectAll().where(\"driveId\", \"=\", driveId).where(\"id\", \"=\", nodeId).executeTakeFirst();\n\t\tif (!row) return void 0;\n\t\treturn rowToNode(row);\n\t}\n\tasync listChildren(driveId, parentFolder, paging) {\n\t\tconst { limit, cursor } = parseListChildrenPaging(paging);\n\t\tlet query = this.db.selectFrom(\"DriveNode\").selectAll().where(\"driveId\", \"=\", driveId);\n\t\tif (parentFolder === null) query = query.where(\"parentFolder\", \"is\", null);\n\t\telse if (parentFolder !== void 0) query = query.where(\"parentFolder\", \"=\", parentFolder);\n\t\tif (cursor !== null) query = query.where((eb) => eb(eb.refTuple(\"createdAt\", \"id\"), \">\", eb.tuple(cursor.createdAt, cursor.id)));\n\t\tconst rows = await query.orderBy(\"createdAt\", \"asc\").orderBy(\"id\", \"asc\").limit(limit + 1).execute();\n\t\tconst hasMore = rows.length > limit;\n\t\tconst sliced = hasMore ? rows.slice(0, limit) : rows;\n\t\tconst last = sliced[sliced.length - 1];\n\t\treturn {\n\t\t\tresults: sliced.map(rowToNode),\n\t\t\toptions: {\n\t\t\t\tcursor: paging?.cursor ?? \"\",\n\t\t\t\tlimit\n\t\t\t},\n\t\t\tnextCursor: hasMore ? encodeKeysetCursor(last.createdAt, last.id) : void 0\n\t\t};\n\t}\n\tasync listAll(driveId) {\n\t\treturn (await this.db.selectFrom(\"DriveNode\").selectAll().where(\"driveId\", \"=\", driveId).orderBy(\"createdAt\", \"asc\").orderBy(\"id\", \"asc\").execute()).map(rowToNode);\n\t}\n\tasync getDescendants(driveId, root) {\n\t\treturn (await this.db.withRecursive(\"descendants\", (qb) => qb.selectFrom(\"DriveNode\").select([\n\t\t\t\"driveId\",\n\t\t\t\"id\",\n\t\t\t\"kind\",\n\t\t\t\"name\",\n\t\t\t\"requestedName\",\n\t\t\t\"parentFolder\",\n\t\t\t\"documentType\",\n\t\t\t\"createdAt\"\n\t\t]).where(\"driveId\", \"=\", driveId).where(\"id\", \"=\", root).unionAll(qb.selectFrom(\"DriveNode\").innerJoin(\"descendants\", \"DriveNode.parentFolder\", \"descendants.id\").where(\"DriveNode.driveId\", \"=\", driveId).select([\n\t\t\t\"DriveNode.driveId\",\n\t\t\t\"DriveNode.id\",\n\t\t\t\"DriveNode.kind\",\n\t\t\t\"DriveNode.name\",\n\t\t\t\"DriveNode.requestedName\",\n\t\t\t\"DriveNode.parentFolder\",\n\t\t\t\"DriveNode.documentType\",\n\t\t\t\"DriveNode.createdAt\"\n\t\t]))).selectFrom(\"descendants\").select([\n\t\t\t\"driveId\",\n\t\t\t\"id\",\n\t\t\t\"kind\",\n\t\t\t\"name\",\n\t\t\t\"requestedName\",\n\t\t\t\"parentFolder\",\n\t\t\t\"documentType\"\n\t\t]).orderBy(\"createdAt\", \"asc\").orderBy(\"id\", \"asc\").execute()).map(rowToNode);\n\t}\n};\nfunction rowToNode(row) {\n\tif (row.kind === \"file\") {\n\t\tif (row.documentType === null) throw new Error(`DriveNode ${row.driveId}/${row.id}: file row has null documentType, which violates the schema CHECK constraint`);\n\t\treturn {\n\t\t\tkind: \"file\",\n\t\t\tid: row.id,\n\t\t\tdriveId: row.driveId,\n\t\t\tparentFolder: row.parentFolder,\n\t\t\tname: row.name,\n\t\t\tdocumentType: row.documentType\n\t\t};\n\t}\n\treturn {\n\t\tkind: \"folder\",\n\t\tid: row.id,\n\t\tdriveId: row.driveId,\n\t\tparentFolder: row.parentFolder,\n\t\tname: row.name\n\t};\n}\n//#endregion\n//#region src/client/reactor-drive-client.ts\n/**\n* Implementation of {@link IDriveClient} backed by the reactor's relationship\n* primitives and the drive-scoped folder actions. Folder structure lives in\n* the operation log (ADD_FOLDER/UPDATE_FOLDER/REMOVE_FOLDER + ADD_RELATIONSHIP\n* for files) and is materialised by `NodeProcessor` into the `DriveNode`\n* table consumed via {@link IDriveReadModel}.\n*/\nvar ReactorDriveClient = class {\n\treactor;\n\treadModel;\n\tconstructor(args) {\n\t\tthis.reactor = args.reactor;\n\t\tthis.readModel = args.readModel;\n\t}\n\tasync create(input, signal) {\n\t\tconst driveDoc = reactorDriveCreateDocument({ global: {\n\t\t\tname: input.global.name,\n\t\t\ticon: input.global.icon ?? null\n\t\t} });\n\t\tif (input.local) {\n\t\t\tif (typeof input.local.sharingType === \"string\") driveDoc.state.local.sharingType = input.local.sharingType;\n\t\t\tif (typeof input.local.availableOffline === \"boolean\") driveDoc.state.local.availableOffline = input.local.availableOffline;\n\t\t}\n\t\tif (input.preferredEditor) driveDoc.header.meta = {\n\t\t\t...driveDoc.header.meta,\n\t\t\tpreferredEditor: input.preferredEditor\n\t\t};\n\t\tconst created = await this.reactor.create(driveDoc, void 0, signal);\n\t\treturn this.toLegacyDriveDocument(created, created.header.id);\n\t}\n\tasync addFile(driveIdentifier, document, parentFolder, signal) {\n\t\tconst documentId = document.header.id;\n\t\tconst createInput = {\n\t\t\tmodel: document.header.documentType,\n\t\t\tversion: 0,\n\t\t\tdocumentId,\n\t\t\tsigning: {\n\t\t\t\tsignature: documentId,\n\t\t\t\tpublicKey: document.header.sig.publicKey,\n\t\t\t\tnonce: document.header.sig.nonce,\n\t\t\t\tcreatedAtUtcIso: document.header.createdAtUtcIso,\n\t\t\t\tdocumentType: document.header.documentType\n\t\t\t},\n\t\t\tslug: document.header.slug,\n\t\t\tname: document.header.name,\n\t\t\tbranch: document.header.branch,\n\t\t\tmeta: document.header.meta,\n\t\t\tprotocolVersions: document.header.protocolVersions ?? { \"base-reducer\": 2 }\n\t\t};\n\t\tconst metadata = {\n\t\t\tkind: \"file\",\n\t\t\tparentFolderId: parentFolder ?? null,\n\t\t\tdocumentType: document.header.documentType\n\t\t};\n\t\tconst request = { jobs: [{\n\t\t\tkey: \"create\",\n\t\t\tdocumentId,\n\t\t\tscope: \"document\",\n\t\t\tbranch: \"main\",\n\t\t\tactions: [createDocumentAction(createInput), upgradeDocumentAction({\n\t\t\t\tdocumentId,\n\t\t\t\tmodel: document.header.documentType,\n\t\t\t\tfromVersion: 0,\n\t\t\t\ttoVersion: document.state.document.version,\n\t\t\t\tinitialState: document.state\n\t\t\t})],\n\t\t\tdependsOn: []\n\t\t}, {\n\t\t\tkey: \"link\",\n\t\t\tdocumentId: driveIdentifier,\n\t\t\tscope: \"document\",\n\t\t\tbranch: \"main\",\n\t\t\tactions: [addRelationshipAction(driveIdentifier, documentId, DRIVE_CHILD_RELATIONSHIP_TYPE, metadata)],\n\t\t\tdependsOn: [\"create\"]\n\t\t}] };\n\t\tawait this.reactor.executeBatch(request, signal);\n\t\treturn this.reactor.get(documentId, void 0, signal);\n\t}\n\tasync addFolder(driveIdentifier, name, parentFolder, signal) {\n\t\tconst folderId = generateId();\n\t\tawait this.reactor.execute(driveIdentifier, \"main\", [addFolderAction({\n\t\t\tfolderId,\n\t\t\tparentFolderId: parentFolder ?? null,\n\t\t\tname\n\t\t})], signal);\n\t\treturn {\n\t\t\tid: folderId,\n\t\t\tkind: \"folder\",\n\t\t\tname,\n\t\t\tparentFolder: parentFolder ?? null\n\t\t};\n\t}\n\tasync removeNode(driveIdentifier, nodeId, signal) {\n\t\tconst node = await this.readModel.getNode(driveIdentifier, nodeId, signal);\n\t\tif (!node) throw new Error(`Node ${nodeId} not found in drive ${driveIdentifier}`);\n\t\tif (node.kind === \"folder\") {\n\t\t\tconst subtree = await this.readModel.getDescendants(driveIdentifier, nodeId, signal);\n\t\t\tconst fileDescendants = subtree.filter((n) => n.kind === \"file\");\n\t\t\tconst subFolders = subtree.filter((n) => n.kind === \"folder\").filter((f) => f.id !== nodeId);\n\t\t\tconst deepestFirstFolders = this.orderDeepestFirst(subFolders, nodeId);\n\t\t\tconst batch = [\n\t\t\t\t...fileDescendants.map((f) => removeRelationshipAction(driveIdentifier, f.id, DRIVE_CHILD_RELATIONSHIP_TYPE)),\n\t\t\t\t...deepestFirstFolders.map((f) => removeFolderAction({ folderId: f.id })),\n\t\t\t\tremoveFolderAction({ folderId: nodeId })\n\t\t\t];\n\t\t\tawait this.reactor.execute(driveIdentifier, \"main\", batch, signal);\n\t\t\tfor (const file of fileDescendants) await this.reactor.deleteDocument(file.id, \"cascade\", signal);\n\t\t\treturn;\n\t\t}\n\t\tawait this.reactor.execute(driveIdentifier, \"main\", [removeRelationshipAction(driveIdentifier, nodeId, DRIVE_CHILD_RELATIONSHIP_TYPE)], signal);\n\t\tawait this.reactor.deleteDocument(nodeId, void 0, signal);\n\t}\n\tasync renameNode(driveIdentifier, nodeId, name, signal) {\n\t\tconst node = await this.readModel.getNode(driveIdentifier, nodeId, signal);\n\t\tif (!node) throw new Error(`Node ${nodeId} not found in drive ${driveIdentifier}`);\n\t\tif (node.kind === \"file\") await this.reactor.rename(nodeId, name, \"main\", signal);\n\t\telse await this.reactor.execute(driveIdentifier, \"main\", [updateFolderAction({\n\t\t\tfolderId: nodeId,\n\t\t\tname\n\t\t})], signal);\n\t\tconst updated = await this.readModel.getNode(driveIdentifier, nodeId, signal);\n\t\tif (!updated) throw new Error(\"Node missing from drive after rename\");\n\t\treturn this.toLegacyNode(updated);\n\t}\n\tasync setPreferredEditorOnNode(nodeId, preferredEditor, signal) {\n\t\treturn this.reactor.setPreferredEditor(nodeId, preferredEditor, \"main\", signal);\n\t}\n\tasync moveNode(driveIdentifier, srcNodeId, targetParentFolderId, signal) {\n\t\tconst node = await this.readModel.getNode(driveIdentifier, srcNodeId, signal);\n\t\tif (!node) throw new Error(`Node ${srcNodeId} not found in drive ${driveIdentifier}`);\n\t\tif (node.kind === \"folder\") await this.reactor.execute(driveIdentifier, \"main\", [updateFolderAction({\n\t\t\tfolderId: srcNodeId,\n\t\t\tparentFolderId: targetParentFolderId ?? null\n\t\t})], signal);\n\t\telse {\n\t\t\tconst metadata = {\n\t\t\t\tkind: \"file\",\n\t\t\t\tparentFolderId: targetParentFolderId ?? null,\n\t\t\t\tdocumentType: node.documentType\n\t\t\t};\n\t\t\tawait this.reactor.execute(driveIdentifier, \"main\", [removeRelationshipAction(driveIdentifier, srcNodeId, DRIVE_CHILD_RELATIONSHIP_TYPE), addRelationshipAction(driveIdentifier, srcNodeId, DRIVE_CHILD_RELATIONSHIP_TYPE, metadata)], signal);\n\t\t}\n\t\tconst drive = await this.reactor.get(driveIdentifier, void 0, signal);\n\t\treturn this.toLegacyDriveDocument(drive, driveIdentifier);\n\t}\n\tasync copyNode(driveIdentifier, srcNodeId, targetParentFolderId, signal) {\n\t\tif (!await this.readModel.getNode(driveIdentifier, srcNodeId, signal)) throw new Error(`Node ${srcNodeId} not found in drive ${driveIdentifier}`);\n\t\tconst subtree = await this.readModel.getDescendants(driveIdentifier, srcNodeId, signal);\n\t\tif (targetParentFolderId !== void 0 && subtree.some((n) => n.id === targetParentFolderId)) throw new Error(`Cannot copy node ${srcNodeId} into itself or one of its descendants (target: ${targetParentFolderId})`);\n\t\tconst idMap = /* @__PURE__ */ new Map();\n\t\tfor (const node of subtree) idMap.set(node.id, generateId());\n\t\tconst jobs = [];\n\t\tconst driveActions = [];\n\t\tconst fileCreateKeys = [];\n\t\tfor (const node of subtree) {\n\t\t\tconst newId = idMap.get(node.id);\n\t\t\tlet newParent;\n\t\t\tif (node.id === srcNodeId) newParent = targetParentFolderId ?? null;\n\t\t\telse {\n\t\t\t\tif (node.parentFolder == null) throw new Error(`copyNode: descendant ${node.id} has no parentFolder`);\n\t\t\t\tconst mapped = idMap.get(node.parentFolder);\n\t\t\t\tif (mapped === void 0) throw new Error(`copyNode: descendant ${node.id} parent ${node.parentFolder} missing from idMap`);\n\t\t\t\tnewParent = mapped;\n\t\t\t}\n\t\t\tif (node.kind === \"folder\") {\n\t\t\t\tdriveActions.push(addFolderAction({\n\t\t\t\t\tfolderId: newId,\n\t\t\t\t\tparentFolderId: newParent,\n\t\t\t\t\tname: node.name\n\t\t\t\t}));\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst srcDoc = await this.reactor.get(node.id, void 0, signal);\n\t\t\tconst documentType = srcDoc.header.documentType;\n\t\t\tconst { results: allModules } = await this.reactor.getDocumentModelModules();\n\t\t\tconst modulesForType = allModules.filter((m) => m.documentModel.global.id === documentType);\n\t\t\tif (modulesForType.length === 0) throw new Error(`Document model module not found for type: ${documentType}`);\n\t\t\tconst reducers = {};\n\t\t\tfor (const m of modulesForType) reducers[m.version ?? 1] = m.reducer;\n\t\t\tconst config = { reducers };\n\t\t\tconst duplicated = replayDocumentVersioned(srcDoc.initialState, srcDoc.operations, config, createPresignedHeader(newId, documentType));\n\t\t\tduplicated.header.name = node.name;\n\t\t\tconst createInput = {\n\t\t\t\tmodel: duplicated.header.documentType,\n\t\t\t\tversion: 0,\n\t\t\t\tdocumentId: newId,\n\t\t\t\tsigning: {\n\t\t\t\t\tsignature: newId,\n\t\t\t\t\tpublicKey: duplicated.header.sig.publicKey,\n\t\t\t\t\tnonce: duplicated.header.sig.nonce,\n\t\t\t\t\tcreatedAtUtcIso: duplicated.header.createdAtUtcIso,\n\t\t\t\t\tdocumentType: duplicated.header.documentType\n\t\t\t\t},\n\t\t\t\tslug: duplicated.header.slug,\n\t\t\t\tname: duplicated.header.name,\n\t\t\t\tbranch: duplicated.header.branch,\n\t\t\t\tmeta: duplicated.header.meta,\n\t\t\t\tprotocolVersions: duplicated.header.protocolVersions ?? { \"base-reducer\": 2 }\n\t\t\t};\n\t\t\tconst createKey = `create:${newId}`;\n\t\t\tjobs.push({\n\t\t\t\tkey: createKey,\n\t\t\t\tdocumentId: newId,\n\t\t\t\tscope: \"document\",\n\t\t\t\tbranch: \"main\",\n\t\t\t\tactions: [createDocumentAction(createInput), upgradeDocumentAction({\n\t\t\t\t\tdocumentId: newId,\n\t\t\t\t\tmodel: duplicated.header.documentType,\n\t\t\t\t\tfromVersion: 0,\n\t\t\t\t\ttoVersion: duplicated.state.document.version,\n\t\t\t\t\tinitialState: duplicated.state\n\t\t\t\t})],\n\t\t\t\tdependsOn: []\n\t\t\t});\n\t\t\tfileCreateKeys.push(createKey);\n\t\t\tconst metadata = {\n\t\t\t\tkind: \"file\",\n\t\t\t\tparentFolderId: newParent,\n\t\t\t\tdocumentType: duplicated.header.documentType\n\t\t\t};\n\t\t\tdriveActions.push(addRelationshipAction(driveIdentifier, newId, DRIVE_CHILD_RELATIONSHIP_TYPE, metadata));\n\t\t}\n\t\tif (driveActions.length > 0) jobs.push({\n\t\t\tkey: \"drive\",\n\t\t\tdocumentId: driveIdentifier,\n\t\t\tscope: \"document\",\n\t\t\tbranch: \"main\",\n\t\t\tactions: driveActions,\n\t\t\tdependsOn: fileCreateKeys\n\t\t});\n\t\tif (jobs.length > 0) {\n\t\t\tconst request = { jobs };\n\t\t\tawait this.reactor.executeBatch(request, signal);\n\t\t}\n\t\tconst drive = await this.reactor.get(driveIdentifier, void 0, signal);\n\t\treturn this.toLegacyDriveDocument(drive, driveIdentifier);\n\t}\n\tasync getNode(driveIdentifier, nodeId, signal) {\n\t\tconst node = await this.readModel.getNode(driveIdentifier, nodeId, signal);\n\t\tif (!node) throw new Error(`Node ${nodeId} not found in drive ${driveIdentifier}`);\n\t\treturn this.toLegacyNode(node);\n\t}\n\tasync listNodes(driveIdentifier, parentFolder, paging, signal) {\n\t\tconst page = await this.readModel.listChildren(driveIdentifier, parentFolder, paging, signal);\n\t\treturn {\n\t\t\tresults: page.results.map((node) => this.toLegacyNode(node)),\n\t\t\toptions: page.options,\n\t\t\t...page.nextCursor !== void 0 ? { nextCursor: page.nextCursor } : {},\n\t\t\t...page.totalCount !== void 0 ? { totalCount: page.totalCount } : {}\n\t\t};\n\t}\n\ttoLegacyNode(node) {\n\t\tif (node.kind === \"file\") {\n\t\t\tconst file = node;\n\t\t\treturn {\n\t\t\t\tid: file.id,\n\t\t\t\tkind: \"file\",\n\t\t\t\tname: file.name,\n\t\t\t\tparentFolder: file.parentFolder,\n\t\t\t\tdocumentType: file.documentType\n\t\t\t};\n\t\t}\n\t\tconst folder = node;\n\t\treturn {\n\t\t\tid: folder.id,\n\t\t\tkind: \"folder\",\n\t\t\tname: folder.name,\n\t\t\tparentFolder: folder.parentFolder\n\t\t};\n\t}\n\tasync toLegacyDriveDocument(doc, driveId) {\n\t\tif (doc.header.documentType !== \"powerhouse/reactor-drive\") throw new Error(`Document ${doc.header.id} is not a reactor-drive document`);\n\t\tconst allNodes = await this.readModel.listAll(driveId);\n\t\tconst legacy = structuredClone(doc);\n\t\tconst existingGlobal = legacy.state.global;\n\t\tlegacy.state.global = {\n\t\t\tname: existingGlobal.name ?? \"\",\n\t\t\ticon: existingGlobal.icon ?? null,\n\t\t\tnodes: allNodes.map((n) => this.toLegacyNode(n))\n\t\t};\n\t\treturn legacy;\n\t}\n\torderDeepestFirst(folders, rootId) {\n\t\tconst depthById = /* @__PURE__ */ new Map();\n\t\tconst compute = (id) => {\n\t\t\tif (id === rootId) return 0;\n\t\t\tconst cached = depthById.get(id);\n\t\t\tif (cached !== void 0) return cached;\n\t\t\tconst folder = folders.find((f) => f.id === id);\n\t\t\tif (!folder) return 0;\n\t\t\tconst depth = compute(folder.parentFolder ?? rootId) + 1;\n\t\t\tdepthById.set(id, depth);\n\t\t\treturn depth;\n\t\t};\n\t\treturn folders.slice().sort((a, b) => compute(b.id) - compute(a.id));\n\t}\n};\n//#endregion\n//#region src/subgraph/schema.ts\nconst typeDefs = gql`\n enum ReactorDriveNodeKind {\n FILE\n FOLDER\n }\n\n input ReactorDrivePagingInput {\n cursor: String\n limit: Int\n }\n\n type ReactorDriveFileNode {\n id: ID!\n driveId: ID!\n name: String!\n parentFolder: ID\n documentType: String!\n }\n\n type ReactorDriveFolderNode {\n id: ID!\n driveId: ID!\n name: String!\n parentFolder: ID\n children(\n paging: ReactorDrivePagingInput\n kind: ReactorDriveNodeKind\n ): ReactorDriveNodePage!\n }\n\n union ReactorDriveNode = ReactorDriveFileNode | ReactorDriveFolderNode\n\n type ReactorDriveNodePage {\n results: [ReactorDriveNode!]!\n nextCursor: String\n hasMore: Boolean!\n totalCount: Int\n }\n\n type ReactorDrive {\n id: ID!\n name: String!\n icon: String\n sharingType: String!\n availableOffline: Boolean!\n rootNodes(\n paging: ReactorDrivePagingInput\n kind: ReactorDriveNodeKind\n ): ReactorDriveNodePage!\n }\n\n type Query {\n reactorDrive(id: ID!): ReactorDrive\n reactorDriveNode(driveId: ID!, id: ID!): ReactorDriveNode\n reactorDriveDescendants(driveId: ID!, root: ID!): [ReactorDriveNode!]!\n }\n`;\n//#endregion\n//#region src/subgraph/resolvers.ts\nconst DEFAULT_LIMIT = 100;\nfunction toPaging(input) {\n\tif (!input) return void 0;\n\treturn {\n\t\tcursor: input.cursor ?? \"\",\n\t\tlimit: input.limit ?? DEFAULT_LIMIT\n\t};\n}\nfunction filterByKind(page, kind) {\n\tif (!kind) return page;\n\tconst wanted = kind === \"FILE\" ? \"file\" : \"folder\";\n\treturn {\n\t\t...page,\n\t\tresults: page.results.filter((node) => node.kind === wanted)\n\t};\n}\nfunction shapePage(page) {\n\treturn {\n\t\tresults: page.results,\n\t\tnextCursor: page.nextCursor,\n\t\thasMore: page.nextCursor !== void 0,\n\t\ttotalCount: page.totalCount\n\t};\n}\n/**\n* Builds GraphQL resolvers backed by the drive read model. The resolvers are\n* pure — every external dependency is read off the GraphQL context. Wiring\n* the resolvers into a subgraph (e.g. `reactor-api`'s `ISubgraph`) is the\n* caller's responsibility.\n*/\nfunction createReactorDriveResolvers() {\n\treturn {\n\t\tQuery: {\n\t\t\tasync reactorDrive(_root, args, ctx) {\n\t\t\t\tconst document = await ctx.reactorClient.get(args.id);\n\t\t\t\tif (document.header.documentType !== \"powerhouse/reactor-drive\") return null;\n\t\t\t\treturn {\n\t\t\t\t\tid: document.header.id,\n\t\t\t\t\tname: document.state.global.name,\n\t\t\t\t\ticon: document.state.global.icon,\n\t\t\t\t\tsharingType: document.state.local.sharingType,\n\t\t\t\t\tavailableOffline: document.state.local.availableOffline\n\t\t\t\t};\n\t\t\t},\n\t\t\tasync reactorDriveNode(_root, args, ctx) {\n\t\t\t\treturn ctx.readModel.getNode(args.driveId, args.id);\n\t\t\t},\n\t\t\tasync reactorDriveDescendants(_root, args, ctx) {\n\t\t\t\treturn ctx.readModel.getDescendants(args.driveId, args.root);\n\t\t\t}\n\t\t},\n\t\tReactorDrive: { async rootNodes(parent, args, ctx) {\n\t\t\treturn shapePage(filterByKind(await ctx.readModel.listChildren(parent.id, null, toPaging(args.paging)), args.kind));\n\t\t} },\n\t\tReactorDriveFolderNode: { async children(parent, args, ctx) {\n\t\t\treturn shapePage(filterByKind(await ctx.readModel.listChildren(parent.driveId, parent.id, toPaging(args.paging)), args.kind));\n\t\t} },\n\t\tReactorDriveNode: { __resolveType(node) {\n\t\t\treturn node.kind === \"file\" ? \"ReactorDriveFileNode\" : \"ReactorDriveFolderNode\";\n\t\t} }\n\t};\n}\n//#endregion\n//#region src/migration/migrate-legacy-state.ts\n/**\n* Translates a legacy `document-drive` `state.global.nodes` array into the\n* action vocabulary used by the new reactor-drive module.\n*\n* Folder nodes are emitted as `ADD_FOLDER` actions targeting the drive\n* document. File nodes are emitted as `ADD_RELATIONSHIP` actions on the drive\n* with `drive/child` metadata carrying the parent folder id. File nodes\n* assume the underlying PHDocument still exists under the same id — the\n* migration only re-links it into the new drive, it does not recreate\n* documents.\n*\n* Re-running the migration is safe: existing `DriveNode` rows are skipped\n* so already-migrated nodes are left untouched.\n*/\nasync function migrateLegacyDriveState(args) {\n\tconst { reactor, readModel, driveId, nodes, signal } = args;\n\tconst branch = args.branch ?? \"main\";\n\tconst ordered = orderLegacyNodes(nodes);\n\tconst actions = [];\n\tlet skippedExisting = 0;\n\tfor (const node of ordered) {\n\t\tif (await readModel.getNode(driveId, node.id, signal)) {\n\t\t\tskippedExisting += 1;\n\t\t\tcontinue;\n\t\t}\n\t\tactions.push(toAction(driveId, node));\n\t}\n\tif (actions.length === 0) return {\n\t\temittedActions: 0,\n\t\tskippedExisting\n\t};\n\tawait reactor.execute(driveId, branch, actions, signal);\n\treturn {\n\t\temittedActions: actions.length,\n\t\tskippedExisting\n\t};\n}\nfunction toAction(driveId, node) {\n\tif (node.kind === \"folder\") return addFolderAction({\n\t\tfolderId: node.id,\n\t\tparentFolderId: node.parentFolder ?? null,\n\t\tname: node.name\n\t});\n\tconst fileNode = node;\n\tconst metadata = {\n\t\tkind: \"file\",\n\t\tparentFolderId: fileNode.parentFolder ?? null,\n\t\tdocumentType: fileNode.documentType\n\t};\n\treturn addRelationshipAction(driveId, fileNode.id, DRIVE_CHILD_RELATIONSHIP_TYPE, metadata);\n}\n/**\n* Sorts legacy nodes so that any folder appears before its children. The\n* legacy state stores nodes in insertion order, which usually already\n* satisfies that property, but defensive sorting keeps replay deterministic\n* if the input was constructed out of order.\n*/\nfunction orderLegacyNodes(nodes) {\n\tconst byId = /* @__PURE__ */ new Map();\n\tfor (const node of nodes) byId.set(node.id, node);\n\tconst visited = /* @__PURE__ */ new Set();\n\tconst ordered = [];\n\tfor (const start of nodes) {\n\t\tif (visited.has(start.id)) continue;\n\t\tconst chain = [];\n\t\tconst seenInWalk = /* @__PURE__ */ new Set();\n\t\tlet current = start;\n\t\twhile (current && !visited.has(current.id) && !seenInWalk.has(current.id)) {\n\t\t\tseenInWalk.add(current.id);\n\t\t\tchain.push(current);\n\t\t\tconst parentId = current.parentFolder;\n\t\t\tcurrent = parentId ? byId.get(parentId) : void 0;\n\t\t}\n\t\twhile (chain.length > 0) {\n\t\t\tconst node = chain.pop();\n\t\t\tif (visited.has(node.id)) continue;\n\t\t\tvisited.add(node.id);\n\t\t\tordered.push(node);\n\t\t}\n\t}\n\treturn ordered;\n}\n//#endregion\nexport { DRIVE_CHILD_RELATIONSHIP_TYPE, DriveNodeView, NodeProcessor, REACTOR_DRIVE_DOCUMENT_TYPE, REACTOR_DRIVE_FILE_EXTENSION, ReactorDriveClient, addFolderAction, createReactorDriveResolvers, getReactorDriveMigrationStatus, migrateLegacyDriveState, reactorDriveActions, reactorDriveCreateDocument, reactorDriveCreateState, reactorDriveDocumentModelModule, reactorDriveDocumentReducer, reactorDriveStateReducer, typeDefs as reactorDriveSubgraphTypeDefs, removeFolderAction, resolveCollision, runReactorDriveMigrations, setAvailableOfflineAction, setDriveIconAction, setDriveNameAction, setSharingTypeAction, updateFolderAction };\n\n//# sourceMappingURL=index.js.map"],"x_google_ignoreList":[2,3],"mappings":";;;;;;AA2GA,IAAI,sBAAsB,cAAc,MAAM;CAC7C;CACA;CACA,YAAY,cAAc,SAAS;EAClC,MAAM,gBAAgB,YAAY,KAAK,IAAI,YAAY,YAAY;AACnE,QAAM,6CAA6C,eAAe,gBAAgB;AAClF,OAAK,OAAO;AACZ,OAAK,eAAe;AACpB,OAAK,mBAAmB;;CAEzB,OAAO,QAAQ,OAAO;AACrB,SAAO,MAAM,QAAQ,MAAM,IAAI,MAAM,SAAS;;;AA6yChD,SAAS,kBAAkB,QAAQ,SAAS;AAC3C,QAAO,SAAS,OAAO,GAAG;;;;AC2lH3B,IAAI,aAA6B,yBAAS,YAAY;AACrD,YAAW,YAAY;AACvB,YAAW,cAAc;AACzB,YAAW,cAAc;AACzB,YAAW,yBAAyB;AACpC,YAAW,WAAW;AACtB,QAAO;EACN,EAAE,CAAC;;;ACv+JL,IAAW,WAAW,WAAW;AAC/B,YAAW,OAAO,UAAU,SAAS,SAAS,GAAG;AAC7C,OAAK,IAAI,GAAG,IAAI,GAAG,IAAI,UAAU,QAAQ,IAAI,GAAG,KAAK;AACjD,OAAI,UAAU;AACd,QAAK,IAAI,KAAK,EAAG,KAAI,OAAO,UAAU,eAAe,KAAK,GAAG,EAAE,CAAE,GAAE,KAAK,EAAE;;AAE9E,SAAO;;AAEX,QAAO,SAAS,MAAM,MAAM,UAAU;;AAGxC,SAAgB,OAAO,GAAG,GAAG;CAC3B,IAAI,IAAI,EAAE;AACV,MAAK,IAAI,KAAK,EAAG,KAAI,OAAO,UAAU,eAAe,KAAK,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAC9E,GAAE,KAAK,EAAE;AACb,KAAI,KAAK,QAAQ,OAAO,OAAO,0BAA0B;OAChD,IAAI,IAAI,GAAG,IAAI,OAAO,sBAAsB,EAAE,EAAE,IAAI,EAAE,QAAQ,IAC/D,KAAI,EAAE,QAAQ,EAAE,GAAG,GAAG,KAAK,OAAO,UAAU,qBAAqB,KAAK,GAAG,EAAE,GAAG,CAC1E,GAAE,EAAE,MAAM,EAAE,EAAE;;AAE1B,QAAO;;AAkKT,SAAgB,cAAc,IAAI,MAAM,MAAM;AAC5C,KAAI,QAAQ,UAAU,WAAW;OAAQ,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,IAAI,IAAI,GAAG,IAC5E,KAAI,MAAM,EAAE,KAAK,OAAO;AACpB,OAAI,CAAC,GAAI,MAAK,MAAM,UAAU,MAAM,KAAK,MAAM,GAAG,EAAE;AACpD,MAAG,KAAK,KAAK;;;AAGrB,QAAO,GAAG,OAAO,MAAM,MAAM,UAAU,MAAM,KAAK,KAAK,CAAC;;;;AC1N1D,IAAI,2BAAW,IAAI,KAAK;AACxB,IAAI,oCAAoB,IAAI,KAAK;AACjC,IAAI,wBAAwB;AAC5B,IAAI,gCAAgC;AACpC,SAAS,UAAU,QAAQ;AACvB,QAAO,OAAO,QAAQ,WAAW,IAAI,CAAC,MAAM;;AAEhD,SAAS,gBAAgB,KAAK;AAC1B,QAAO,UAAU,IAAI,OAAO,KAAK,UAAU,IAAI,OAAO,IAAI,IAAI,CAAC;;AAEnE,SAAS,iBAAiB,KAAK;CAC3B,IAAI,2BAAW,IAAI,KAAK;CACxB,IAAI,cAAc,EAAE;AACpB,KAAI,YAAY,QAAQ,SAAU,oBAAoB;AAClD,MAAI,mBAAmB,SAAS,sBAAsB;GAClD,IAAI,eAAe,mBAAmB,KAAK;GAC3C,IAAI,YAAY,gBAAgB,mBAAmB,IAAI;GACvD,IAAI,eAAe,kBAAkB,IAAI,aAAa;AACtD,OAAI,gBAAgB,CAAC,aAAa,IAAI,UAAU;QACxC,sBACA,SAAQ,KAAK,iCAAiC,eAAe,gMAEwB;cAGpF,CAAC,aACN,mBAAkB,IAAI,cAAc,+BAAe,IAAI,KAAG,CAAC;AAE/D,gBAAa,IAAI,UAAU;AAC3B,OAAI,CAAC,SAAS,IAAI,UAAU,EAAE;AAC1B,aAAS,IAAI,UAAU;AACvB,gBAAY,KAAK,mBAAmB;;QAIxC,aAAY,KAAK,mBAAmB;GAE1C;AACF,QAAO,SAAS,SAAS,EAAE,EAAE,IAAI,EAAE,EAAe,aAAa,CAAC;;AAEpE,SAAS,SAAS,KAAK;CACnB,IAAI,UAAU,IAAI,IAAI,IAAI,YAAY;AACtC,SAAQ,QAAQ,SAAU,MAAM;AAC5B,MAAI,KAAK,IACL,QAAO,KAAK;AAChB,SAAO,KAAK,KAAK,CAAC,QAAQ,SAAU,KAAK;GACrC,IAAI,QAAQ,KAAK;AACjB,OAAI,SAAS,OAAO,UAAU,SAC1B,SAAQ,IAAI,MAAM;IAExB;GACJ;CACF,IAAI,MAAM,IAAI;AACd,KAAI,KAAK;AACL,SAAO,IAAI;AACX,SAAO,IAAI;;AAEf,QAAO;;AAEX,SAAS,cAAc,QAAQ;CAC3B,IAAI,WAAW,UAAU,OAAO;AAChC,KAAI,CAAC,SAAS,IAAI,SAAS,EAAE;EACzB,IAAI,SAAS,MAAM,QAAQ;GACQ;GAC/B,8BAA8B;GACjC,CAAC;AACF,MAAI,CAAC,UAAU,OAAO,SAAS,WAC3B,OAAM,IAAI,MAAM,gCAAgC;AAEpD,WAAS,IAAI,UAAU,SAAS,iBAAiB,OAAO,CAAC,CAAC;;AAE9D,QAAO,SAAS,IAAI,SAAS;;AAEjC,SAAgB,IAAI,UAAU;CAC1B,IAAI,OAAO,EAAE;AACb,MAAK,IAAI,KAAK,GAAG,KAAK,UAAU,QAAQ,KACpC,MAAK,KAAK,KAAK,UAAU;AAE7B,KAAI,OAAO,aAAa,SACpB,YAAW,CAAC,SAAS;CAEzB,IAAI,SAAS,SAAS;AACtB,MAAK,QAAQ,SAAU,KAAK,GAAG;AAC3B,MAAI,OAAO,IAAI,SAAS,WACpB,WAAU,IAAI,IAAI,OAAO;MAGzB,WAAU;AAEd,YAAU,SAAS,IAAI;GACzB;AACF,QAAO,cAAc,OAAO;;AAEhC,SAAgB,cAAc;AAC1B,UAAS,OAAO;AAChB,mBAAkB,OAAO;;AAE7B,SAAgB,0BAA0B;AACtC,yBAAwB;;AAE5B,SAAgB,sCAAsC;AAClD,iCAAgC;;AAEpC,SAAgB,uCAAuC;AACnD,iCAAgC;;AAEpC,IAAI,SAAS;CACJ;CACQ;CACY;CACY;CACC;CACzC;CACA,SAAU,OAAO;AACd,OAAM,MAAM,OAAO,KAAK,MAAM,cAAc,OAAO,aAAa,MAAM,0BAA0B,OAAO,yBAAyB,MAAM,sCAAsC,OAAO,qCAAqC,MAAM,uCAAuC,OAAO;GAC7Q,QAAQ,MAAM,EAAE,EAAE;AACrB,IAAI,aAAa;;;ACrGjB,MAAM,8BAA8B;AAEpC,MAAM,+BAA+B;AAGrC,SAAS,mBAAmB,OAAO;AAClC,QAAO;EACN,IAAI,YAAY;EAChB,MAAM;EACN,OAAO;EACP,iCAAiC,IAAI,MAAM,EAAE,aAAa;EAC1D;EACA;;AAEF,SAAS,mBAAmB,OAAO;AAClC,QAAO;EACN,IAAI,YAAY;EAChB,MAAM;EACN,OAAO;EACP,iCAAiC,IAAI,MAAM,EAAE,aAAa;EAC1D;EACA;;AAEF,SAAS,qBAAqB,OAAO;AACpC,QAAO;EACN,IAAI,YAAY;EAChB,MAAM;EACN,OAAO;EACP,iCAAiC,IAAI,MAAM,EAAE,aAAa;EAC1D;EACA;;AAEF,SAAS,0BAA0B,OAAO;AACzC,QAAO;EACN,IAAI,YAAY;EAChB,MAAM;EACN,OAAO;EACP,iCAAiC,IAAI,MAAM,EAAE,aAAa;EAC1D;EACA;;AAEF,SAAS,gBAAgB,OAAO;AAC/B,QAAO;EACN,IAAI,YAAY;EAChB,MAAM;EACN,OAAO;EACP,iCAAiC,IAAI,MAAM,EAAE,aAAa;EAC1D;EACA;;AAEF,SAAS,mBAAmB,OAAO;AAClC,QAAO;EACN,IAAI,YAAY;EAChB,MAAM;EACN,OAAO;EACP,iCAAiC,IAAI,MAAM,EAAE,aAAa;EAC1D;EACA;;AAEF,SAAS,mBAAmB,OAAO;AAClC,QAAO;EACN,IAAI,YAAY;EAChB,MAAM;EACN,OAAO;EACP,iCAAiC,IAAI,MAAM,EAAE,aAAa;EAC1D;EACA;;AAEF,MAAM,sBAAsB;CAC3B,cAAc;CACd,cAAc;CACd,gBAAgB;CAChB,qBAAqB;CACrB,WAAW;CACX,cAAc;CACd,cAAc;CACd;AAGD,MAAM,4BAA4B,OAAO,WAAW;AACnD,KAAI,iBAAiB,OAAO,CAAE,QAAO;CACrC,MAAM,cAAc;AACpB,SAAQ,YAAY,MAApB;EACC,KAAK,kBAAkB;GACtB,MAAM,QAAQ,YAAY;AAC1B,SAAM,OAAO,OAAO,MAAM;AAC1B,UAAO;;EAER,KAAK,kBAAkB;GACtB,MAAM,QAAQ,YAAY;AAC1B,SAAM,OAAO,OAAO,MAAM;AAC1B,UAAO;;EAER,KAAK,oBAAoB;GACxB,MAAM,QAAQ,YAAY;AAC1B,SAAM,MAAM,cAAc,MAAM;AAChC,UAAO;;EAER,KAAK,yBAAyB;GAC7B,MAAM,QAAQ,YAAY;AAC1B,SAAM,MAAM,mBAAmB,MAAM;AACrC,UAAO;;EAER,KAAK;EACL,KAAK;EACL,KAAK,gBAAiB,QAAO;EAC7B,QAAS,QAAO;;;AAKlB,MAAM,qBAAqB;CAC1B,MAAM;CACN,MAAM;CACN;AACD,MAAM,oBAAoB;CACzB,aAAa;CACb,kBAAkB;CAClB;AACD,MAAM,2BAA2B,UAAU;AAC1C,QAAO;EACN,GAAG,kBAAkB;EACrB,QAAQ;GACP,GAAG;GACH,GAAG,OAAO;GACV;EACD,OAAO;GACN,GAAG;GACH,GAAG,OAAO;GACV;EACD;;AAEF,MAAM,8BAA8B,UAAU;AAC7C,QAAO,mBAAmB,yBAAyB,OAAO,4BAA4B;;AAEvF,MAAM,gCAAgC,UAAU,UAAU;AACzD,QAAO,qBAAqB,UAAU,MAAM;;AAE7C,MAAM,6BAA6B,UAAU;AAC5C,QAAO,kBAAkB,OAAO,4BAA4B;;AAE7D,MAAM,uBAAuB,UAAU;AACtC,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,YAAY,SAAS,WAAW;;AAEvF,MAAM,6BAA6B,UAAU;AAC5C,KAAI,CAAC,oBAAoB,MAAM,CAAE,OAAM,IAAI,MAAM,4BAA4B;;AAE9E,MAAM,0BAA0B,aAAa;AAC5C,QAAO,OAAO,aAAa,YAAY,aAAa,QAAQ,YAAY,YAAY,SAAS,OAAO,iBAAiB;;AAEtH,MAAM,gCAAgC,aAAa;AAClD,KAAI,CAAC,uBAAuB,SAAS,CAAE,OAAM,IAAI,MAAM,+BAA+B;;AAEvF,MAAM,8BAA8B,cAAc,yBAAyB;AAC3E,MAAM,kCAAkC;CACvC,IAAI;CACJ,MAAM;CACN,WAAW;CACX,aAAa;CACb,QAAQ;EACP,MAAM;EACN,SAAS;EACT;CACD,gBAAgB,CAAC;EAChB,SAAS;EACT,WAAW,EAAE;EACb,OAAO;GACN,QAAQ;IACP,QAAQ;IACR,cAAc,KAAK,UAAU,KAAK,UAAU,mBAAmB,CAAC;IAChE,UAAU,EAAE;IACZ;GACD,OAAO;IACN,QAAQ;IACR,cAAc,KAAK,UAAU,KAAK,UAAU,kBAAkB,CAAC;IAC/D,UAAU,EAAE;IACZ;GACD;EACD,SAAS,CAAC;GACT,IAAI;GACJ,MAAM;GACN,aAAa;GACb,YAAY;IACX;KACC,IAAI;KACJ,MAAM;KACN,aAAa;KACb,QAAQ;KACR,UAAU;KACV,SAAS;KACT,QAAQ,EAAE;KACV,UAAU,EAAE;KACZ,OAAO;KACP;IACD;KACC,IAAI;KACJ,MAAM;KACN,aAAa;KACb,QAAQ;KACR,UAAU;KACV,SAAS;KACT,QAAQ,EAAE;KACV,UAAU,EAAE;KACZ,OAAO;KACP;IACD;KACC,IAAI;KACJ,MAAM;KACN,aAAa;KACb,QAAQ;KACR,UAAU;KACV,SAAS;KACT,QAAQ,EAAE;KACV,UAAU,EAAE;KACZ,OAAO;KACP;IACD;KACC,IAAI;KACJ,MAAM;KACN,aAAa;KACb,QAAQ;KACR,UAAU;KACV,SAAS;KACT,QAAQ,EAAE;KACV,UAAU,EAAE;KACZ,OAAO;KACP;IACD;KACC,IAAI;KACJ,MAAM;KACN,aAAa;KACb,QAAQ;KACR,UAAU;KACV,SAAS;KACT,QAAQ,EAAE;KACV,UAAU,EAAE;KACZ,OAAO;KACP;IACD;KACC,IAAI;KACJ,MAAM;KACN,aAAa;KACb,QAAQ;KACR,UAAU;KACV,SAAS;KACT,QAAQ,EAAE;KACV,UAAU,EAAE;KACZ,OAAO;KACP;IACD;KACC,IAAI;KACJ,MAAM;KACN,aAAa;KACb,QAAQ;KACR,UAAU;KACV,SAAS;KACT,QAAQ,EAAE;KACV,UAAU,EAAE;KACZ,OAAO;KACP;IACD;GACD,CAAC;EACF,CAAC;CACF;AACD,MAAM,kCAAkC;CACvC,SAAS;CACT,SAAS;CACT,eAAe,YAAY,kBAAkB,EAAE,gCAAgC;CAC/E,OAAO;EACN,eAAe;EACf,aAAa;EACb,gBAAgB;EAChB,eAAe;EACf,kBAAkB;EAClB,eAAe;EACf,qBAAqB;EACrB,kBAAkB;EAClB,wBAAwB;EACxB;CACD;AAutBgB,GAAG"}
|