@powerhousedao/reactor 6.2.3-dev.9 → 6.2.3-staging.0

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.
Files changed (42) hide show
  1. package/dist/{build-worker-executor-BN30c2xw.js → build-worker-executor-BonPsKdx.js} +24 -8
  2. package/dist/build-worker-executor-BonPsKdx.js.map +1 -0
  3. package/dist/{document-indexer-CDX1D2RZ.js → document-indexer-I6i_YzsR.js} +866 -87
  4. package/dist/document-indexer-I6i_YzsR.js.map +1 -0
  5. package/dist/entry.d.ts +2 -1
  6. package/dist/entry.js +10 -6
  7. package/dist/entry.js.map +1 -1
  8. package/dist/{errors-D3S6Eysd.js → errors-BM_ULVWd.js} +9 -1
  9. package/dist/{errors-D3S6Eysd.js.map → errors-BM_ULVWd.js.map} +1 -1
  10. package/dist/{drive-container-types-aO-PASn7.js → forwarding-logger-BkwlvD9W.js} +1727 -306
  11. package/dist/forwarding-logger-BkwlvD9W.js.map +1 -0
  12. package/dist/index.d.ts +3946 -2817
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +2557 -712
  15. package/dist/index.js.map +1 -1
  16. package/dist/projection-entry.d.ts +2 -1
  17. package/dist/projection-entry.js +62 -20
  18. package/dist/projection-entry.js.map +1 -1
  19. package/dist/{projection-shard-manager-CPQc5XHL.js → projection-shard-manager-CJIdjsd8.js} +24 -4
  20. package/dist/projection-shard-manager-CJIdjsd8.js.map +1 -0
  21. package/dist/{projection-worker-wI4PwcV2.js → projection-worker-D8uNFdwX.js} +1 -1
  22. package/dist/{projection-worker-wI4PwcV2.js.map → projection-worker-D8uNFdwX.js.map} +1 -1
  23. package/dist/{transport-CuogVKN_.js → transport-B3G50KVd.js} +1 -1
  24. package/dist/{transport-CuogVKN_.js.map → transport-B3G50KVd.js.map} +1 -1
  25. package/dist/{transport-ByGviWdZ.js → transport-CZDHzfvy.js} +1 -1
  26. package/dist/{transport-ByGviWdZ.js.map → transport-CZDHzfvy.js.map} +1 -1
  27. package/dist/{worker-handle-CrERzl8s.js → worker-handle-DjbEQkq-.js} +22 -16
  28. package/dist/worker-handle-DjbEQkq-.js.map +1 -0
  29. package/package.json +20 -10
  30. package/dist/build-worker-executor-BN30c2xw.js.map +0 -1
  31. package/dist/document-indexer-CDX1D2RZ.js.map +0 -1
  32. package/dist/drive-container-types-aO-PASn7.js.map +0 -1
  33. package/dist/error-info-Cpu4OY3o.js +0 -62
  34. package/dist/error-info-Cpu4OY3o.js.map +0 -1
  35. package/dist/forwarding-logger-BBkMSxuJ.js +0 -85
  36. package/dist/forwarding-logger-BBkMSxuJ.js.map +0 -1
  37. package/dist/projection-shard-manager-CPQc5XHL.js.map +0 -1
  38. package/dist/types-DMKLa0Ok.js +0 -33
  39. package/dist/types-DMKLa0Ok.js.map +0 -1
  40. package/dist/worker-Bb0u-X9m.js +0 -22
  41. package/dist/worker-Bb0u-X9m.js.map +0 -1
  42. package/dist/worker-handle-CrERzl8s.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"document-indexer-I6i_YzsR.js","names":["uuidv4","uuidv4"],"sources":["../src/catch-up/scheduler.ts","../src/catch-up/types.ts","../src/catch-up/contiguous-cursor.ts","../src/read-models/base-read-model.ts","../src/read-models/coordinator.ts","../src/read-models/names.ts","../src/read-models/document-view.ts","../src/shared/consistency-tracker.ts","../src/shared/collect-all-pages.ts","../src/storage/kysely/document-indexer.ts"],"sourcesContent":["import type { ILogger } from \"document-model\";\nimport type { IOperationIndex } from \"../cache/operation-index-types.js\";\nimport type { WatermarkSession } from \"./settled-watermark.js\";\nimport type {\n CatchUpConfig,\n CatchUpConsumerStatus,\n CatchUpStatus,\n CatchUpThread,\n ICatchUp,\n ICatchUpConsumer,\n ISettledWatermark,\n SweepBlockedAt,\n SweepResult,\n} from \"./types.js\";\n\ntype ConsumerEntry = {\n consumer: ICatchUpConsumer;\n thread: CatchUpThread;\n blockedAt?: SweepBlockedAt;\n lastAdvanceUtcMs: number;\n warnedHeld: boolean;\n};\n\nexport type CatchUpSchedulerHooks = {\n /** Called for every sweep that moved, replayed or failed. */\n onSwept: (result: SweepResult, thread: CatchUpThread) => void;\n /** Names the sessions holding the watermark, for the stall warning. */\n describeSessions: (xids: readonly string[]) => Promise<WatermarkSession[]>;\n};\n\nconst noHooks: CatchUpSchedulerHooks = {\n onSwept: () => {},\n describeSessions: () => Promise.resolve([]),\n};\n\ntype PresentPage = { bound: number; present: number[] };\n\ntype ConsumerSource = {\n consumers: () => readonly ICatchUpConsumer[];\n thread: CatchUpThread;\n};\n\nexport function isCatchUpConsumer(value: unknown): value is ICatchUpConsumer {\n if (typeof value !== \"object\" || value === null) return false;\n const candidate = value as Partial<ICatchUpConsumer>;\n return (\n typeof candidate.sweep === \"function\" &&\n typeof candidate.consumerId === \"string\" &&\n typeof candidate.appliedThrough === \"number\"\n );\n}\n\n/** Periodically sweeps every consumer up to the settled watermark. */\nexport class CatchUpScheduler implements ICatchUp {\n private readonly fixed: ConsumerEntry[] = [];\n private readonly sources: ConsumerSource[] = [];\n private readonly sourced = new Map<ICatchUpConsumer, ConsumerEntry>();\n private timer: ReturnType<typeof setInterval> | undefined;\n private running: Promise<SweepResult[]> | undefined;\n private started = false;\n private stopped = false;\n private warnedStallSince: number | undefined;\n private readonly statusSources: Array<() => CatchUpConsumerStatus[]> = [];\n\n constructor(\n private readonly watermark: ISettledWatermark,\n private readonly operationIndex: IOperationIndex,\n private readonly config: CatchUpConfig,\n private readonly logger: ILogger,\n private readonly hooks: CatchUpSchedulerHooks = noHooks,\n ) {}\n\n get settledWatermark(): ISettledWatermark {\n return this.watermark;\n }\n\n addConsumer(consumer: ICatchUpConsumer, thread: CatchUpThread): void {\n if (this.fixed.some((entry) => entry.consumer === consumer)) return;\n this.fixed.push({\n consumer,\n thread,\n lastAdvanceUtcMs: Date.now(),\n warnedHeld: false,\n });\n this.ensureTimer();\n }\n\n /** Consumers read on every tick, so models registered later are swept too. */\n addSource(\n consumers: () => readonly ICatchUpConsumer[],\n thread: CatchUpThread,\n ): void {\n this.sources.push({ consumers, thread });\n this.ensureTimer();\n }\n\n /** Consumers another thread sweeps, reported in status(). */\n addStatusSource(source: () => CatchUpConsumerStatus[]): void {\n this.statusSources.push(source);\n }\n\n start(): void {\n this.started = true;\n this.ensureTimer();\n }\n\n async stop(): Promise<void> {\n this.stopped = true;\n if (this.timer !== undefined) {\n clearInterval(this.timer);\n this.timer = undefined;\n }\n if (this.running !== undefined) {\n await this.running.catch(() => []);\n }\n }\n\n async sweepNow(): Promise<SweepResult[]> {\n if (this.running !== undefined) {\n await this.running.catch(() => []);\n }\n return this.run();\n }\n\n status(): CatchUpStatus {\n return {\n watermark: this.watermark.status(),\n consumers: this.entries()\n .map((entry): CatchUpConsumerStatus => ({\n consumerId: entry.consumer.consumerId,\n thread: entry.thread,\n appliedThrough: entry.consumer.appliedThrough,\n trackedAbove: entry.consumer.trackedAbove,\n ...(entry.blockedAt !== undefined\n ? { blockedAt: entry.blockedAt }\n : {}),\n lastAdvanceUtcMs: entry.lastAdvanceUtcMs,\n }))\n .concat(this.statusSources.flatMap((source) => source())),\n };\n }\n\n private ensureTimer(): void {\n if (!this.started || this.stopped || this.timer !== undefined) return;\n if (this.fixed.length === 0 && this.sources.length === 0) return;\n\n const timer = setInterval(() => {\n if (this.running !== undefined) return;\n void this.run().catch((error: unknown) => {\n this.logger.error(\"catch-up tick failed: @error\", error);\n });\n }, this.config.intervalMs);\n if (typeof timer === \"object\" && \"unref\" in timer) timer.unref();\n this.timer = timer;\n }\n\n private run(): Promise<SweepResult[]> {\n const run = this.tick().finally(() => {\n if (this.running === run) this.running = undefined;\n });\n this.running = run;\n return run;\n }\n\n private async tick(): Promise<SweepResult[]> {\n const settled = await this.watermark.refresh();\n await this.warnIfWatermarkHeld();\n const entries = this.entries();\n if (entries.length === 0) return [];\n\n let lowest = Number.POSITIVE_INFINITY;\n for (const entry of entries) {\n lowest = Math.min(lowest, entry.consumer.appliedThrough);\n }\n const shared = await this.readPage(lowest, settled);\n\n const results: SweepResult[] = [];\n for (const entry of entries) {\n if (this.stopped) break;\n const result = await this.sweepOne(entry, shared, settled);\n if (result !== undefined) results.push(result);\n }\n return results;\n }\n\n private entries(): ConsumerEntry[] {\n const entries = [...this.fixed];\n const seen = new Set(entries.map((entry) => entry.consumer));\n for (const source of this.sources) {\n for (const consumer of source.consumers()) {\n if (seen.has(consumer)) continue;\n seen.add(consumer);\n let entry = this.sourced.get(consumer);\n if (entry === undefined) {\n entry = {\n consumer,\n thread: source.thread,\n lastAdvanceUtcMs: Date.now(),\n warnedHeld: false,\n };\n this.sourced.set(consumer, entry);\n }\n entries.push(entry);\n }\n }\n return entries;\n }\n\n private async sweepOne(\n entry: ConsumerEntry,\n shared: PresentPage,\n settled: number,\n ): Promise<SweepResult | undefined> {\n const applied = entry.consumer.appliedThrough;\n let page = shared;\n if (applied >= shared.bound && shared.bound < settled) {\n try {\n page = await this.readPage(applied, settled);\n } catch (error) {\n this.logger.error(\n \"@consumer catch-up could not read present ordinals: @error\",\n entry.consumer.consumerId,\n error,\n );\n return undefined;\n }\n }\n\n let result: SweepResult;\n try {\n result = await entry.consumer.sweep(page.bound, page.present);\n } catch (error) {\n this.logger.error(\n \"@consumer catch-up sweep failed: @error\",\n entry.consumer.consumerId,\n error,\n );\n return undefined;\n }\n\n if (result.to > result.from) {\n entry.lastAdvanceUtcMs = Date.now();\n entry.warnedHeld = false;\n }\n entry.blockedAt = result.blockedAt;\n this.warnIfConsumerHeld(entry, settled);\n if (\n result.to > result.from ||\n result.replayed > 0 ||\n result.blockedAt !== undefined\n ) {\n this.hooks.onSwept(result, entry.thread);\n }\n return result;\n }\n\n private async warnIfWatermarkHeld(): Promise<void> {\n const status = this.watermark.status();\n const since = status.stalledSinceUtcMs;\n if (since === undefined) {\n this.warnedStallSince = undefined;\n return;\n }\n const heldMs = Date.now() - since;\n if (heldMs < this.config.stuckWarnMs || this.warnedStallSince === since) {\n return;\n }\n this.warnedStallSince = since;\n const sessions: WatermarkSession[] = await this.hooks\n .describeSessions(status.waitingOn)\n .catch(() => []);\n this.logger.warn(\n \"settled watermark held at @settled (head @head) for @ms ms, waiting on xid @xids: @sessions\",\n status.settledThrough,\n status.head,\n heldMs,\n status.waitingOn.join(\", \"),\n sessions\n .map(\n (session) =>\n `pid ${session.pid} ${session.applicationName} ${session.state} since ${session.xactStart ?? \"?\"}`,\n )\n .join(\"; \"),\n );\n }\n\n private warnIfConsumerHeld(entry: ConsumerEntry, settled: number): void {\n const applied = entry.consumer.appliedThrough;\n if (applied >= settled || entry.warnedHeld) return;\n const heldMs = Date.now() - entry.lastAdvanceUtcMs;\n if (heldMs < this.config.stuckWarnMs) return;\n entry.warnedHeld = true;\n this.logger.warn(\n \"@consumer cursor held at @applied for @ms ms (settled @settled)\",\n entry.consumer.consumerId,\n applied,\n heldMs,\n settled,\n );\n }\n\n private async readPage(after: number, settled: number): Promise<PresentPage> {\n const limit = this.config.sweepPageSize;\n const present = await this.operationIndex.getOrdinalsInRange(\n after,\n settled,\n limit,\n );\n const bound =\n present.length >= limit ? present[present.length - 1]! : settled;\n return { bound, present };\n }\n}\n","import type { Unsubscribe } from \"../events/types.js\";\n\nexport type WatermarkStatus = {\n head: number;\n settledThrough: number;\n waitingOn: string[];\n stalledSinceUtcMs?: number;\n};\n\nexport interface ISettledWatermark {\n /** Every ordinal at or below this is visible or will never exist. */\n readonly settledThrough: number;\n /** Probes, coalesced with one in flight; resolves with settledThrough. */\n refresh(signal?: AbortSignal): Promise<number>;\n onAdvance(listener: (settledThrough: number) => void): Unsubscribe;\n status(): WatermarkStatus;\n}\n\nexport type SweepBlockedAt = {\n ordinal: number;\n documentId: string;\n scope: string;\n branch: string;\n type: string;\n error: string;\n};\n\nexport type SweepResult = {\n consumerId: string;\n from: number;\n to: number;\n durationMs: number;\n /** Late operations applied. */\n replayed: number;\n /** Later operations of their streams applied again. */\n reapplied: number;\n blockedAt?: SweepBlockedAt;\n};\n\nexport interface ICatchUpConsumer {\n readonly consumerId: string;\n readonly appliedThrough: number;\n /** Ordinals above the cursor this consumer holds in memory. */\n readonly trackedAbove: number;\n sweep(\n settledThrough: number,\n present: readonly number[],\n signal?: AbortSignal,\n ): Promise<SweepResult>;\n}\n\nexport type CatchUpThread = \"host\" | \"projection\";\n\nexport type CatchUpConsumerStatus = {\n consumerId: string;\n thread: CatchUpThread;\n appliedThrough: number;\n trackedAbove: number;\n blockedAt?: SweepBlockedAt;\n lastAdvanceUtcMs: number;\n};\n\nexport type CatchUpStatus = {\n watermark: WatermarkStatus;\n consumers: CatchUpConsumerStatus[];\n};\n\nexport type CatchUpConfig = {\n intervalMs: number;\n stuckWarnMs: number;\n maxTrackedAboveCursor: number;\n /** Present ordinals read per tick. */\n sweepPageSize: number;\n};\n\nexport const defaultCatchUpConfig: CatchUpConfig = {\n intervalMs: 2000,\n stuckWarnMs: 60_000,\n maxTrackedAboveCursor: 100_000,\n sweepPageSize: 10_000,\n};\n\n/** The catch-up surface a reactor module exposes. */\nexport interface ICatchUp {\n status(): CatchUpStatus;\n sweepNow(): Promise<SweepResult[]>;\n}\n","/** In memory, per consumer; tracks ordinals above appliedThrough only. */\nexport class ContiguousCursor {\n private claimed = new Set<number>();\n private applied = new Set<number>();\n\n constructor(\n private through: number,\n private limit: number,\n ) {}\n\n setLimit(limit: number): void {\n this.limit = limit;\n }\n\n /** Every present ordinal at or below this has been applied. */\n get appliedThrough(): number {\n return this.through;\n }\n\n get trackedAbove(): number {\n return this.claimed.size + this.applied.size;\n }\n\n /** Ordinals the caller must apply; the rest are already covered. */\n claim(ordinals: Iterable<number>): Set<number> {\n const mine = new Set<number>();\n for (const ordinal of ordinals) {\n if (ordinal <= this.through) continue;\n if (this.claimed.has(ordinal) || this.applied.has(ordinal)) continue;\n this.claimed.add(ordinal);\n mine.add(ordinal);\n }\n return mine;\n }\n\n /** Releases claims; committed ones join the applied set. */\n settle(ordinals: Iterable<number>, committed: boolean): void {\n for (const ordinal of ordinals) {\n if (!this.claimed.delete(ordinal)) continue;\n if (committed && ordinal > this.through) this.applied.add(ordinal);\n }\n }\n\n /** Past the limit, clears the applied set; the next sweep re-applies it. */\n enforceLimit(): void {\n if (this.claimed.size + this.applied.size > this.limit) {\n this.applied.clear();\n }\n }\n\n /** Present ordinals above the cursor that no path has claimed or applied. */\n missing(present: readonly number[]): number[] {\n return present.filter(\n (ordinal) =>\n ordinal > this.through &&\n !this.claimed.has(ordinal) &&\n !this.applied.has(ordinal),\n );\n }\n\n /** `settled`, held below the lowest claimed or unapplied ordinal. */\n target(settled: number, present: readonly number[]): number {\n let to = settled;\n for (const ordinal of this.claimed) {\n if (ordinal <= to) to = ordinal - 1;\n }\n for (const ordinal of present) {\n if (ordinal <= this.through || ordinal > to) continue;\n if (!this.applied.has(ordinal)) to = ordinal - 1;\n }\n return Math.max(to, this.through);\n }\n\n advance(to: number): void {\n if (to <= this.through) return;\n this.through = to;\n for (const ordinal of this.applied) {\n if (ordinal <= to) this.applied.delete(ordinal);\n }\n for (const ordinal of this.claimed) {\n if (ordinal <= to) this.claimed.delete(ordinal);\n }\n }\n}\n","import type { OperationWithContext } from \"@powerhousedao/shared/document-model\";\nimport { childLogger, type ILogger } from \"document-model\";\nimport type { Kysely } from \"kysely\";\nimport type { IOperationIndex } from \"../cache/operation-index-types.js\";\nimport type { IWriteCache } from \"../cache/write/interfaces.js\";\nimport { ContiguousCursor } from \"../catch-up/contiguous-cursor.js\";\nimport {\n createKyselyWatermarkProbe,\n SettledWatermark,\n} from \"../catch-up/settled-watermark.js\";\nimport {\n defaultCatchUpConfig,\n type ICatchUpConsumer,\n type ISettledWatermark,\n type SweepBlockedAt,\n type SweepResult,\n} from \"../catch-up/types.js\";\nimport type { Unsubscribe } from \"../events/types.js\";\nimport type { IConsistencyTracker } from \"../shared/consistency-tracker.js\";\nimport { DocumentNotFoundError } from \"../shared/errors.js\";\nimport type {\n ConsistencyCoordinate,\n ConsistencyToken,\n} from \"../shared/types.js\";\nimport { yieldToMain } from \"../shared/utils.js\";\nimport type { Database as StorageDatabase } from \"../storage/kysely/types.js\";\nimport type { IReadModel, IReadModelReservation } from \"./interfaces.js\";\nimport type { DocumentViewDatabase } from \"./types.js\";\n\n/** Bounds on an indexing pass: one transaction, and the stall between yields. */\nexport type ReadModelIndexingConfig = {\n /** Maximum operations committed in a single transaction. */\n commitChunkSize: number;\n /** Maximum elapsed milliseconds before yielding between chunks. */\n yieldDeadlineMs: number;\n};\n\n/** Small enough that a chunk's transaction rarely outlasts the yield deadline. */\nexport const DEFAULT_COMMIT_CHUNK_SIZE = 50;\n\n/** Matches the executor's own default, so both paths yield on the same cadence. */\nexport const DEFAULT_READ_MODEL_YIELD_DEADLINE_MS = 50;\n\nexport const defaultReadModelIndexingConfig: ReadModelIndexingConfig = {\n commitChunkSize: DEFAULT_COMMIT_CHUNK_SIZE,\n yieldDeadlineMs: DEFAULT_READ_MODEL_YIELD_DEADLINE_MS,\n};\n\n/** For read models whose callers can observe where a batch was split. */\nexport const unchunkedReadModelIndexingConfig: ReadModelIndexingConfig = {\n commitChunkSize: Number.MAX_SAFE_INTEGER,\n yieldDeadlineMs: DEFAULT_READ_MODEL_YIELD_DEADLINE_MS,\n};\n\n/**\n * Keeps the chunk size at one operation or more: a chunk of zero or less never\n * advances the indexing loop, so the pass would spin without ever resolving.\n */\nfunction normalizeIndexingConfig(\n config: ReadModelIndexingConfig,\n): ReadModelIndexingConfig {\n if (Number.isNaN(config.commitChunkSize)) {\n return { ...config, commitChunkSize: DEFAULT_COMMIT_CHUNK_SIZE };\n }\n\n return {\n ...config,\n commitChunkSize: Math.max(\n 1,\n Math.min(Math.floor(config.commitChunkSize), Number.MAX_SAFE_INTEGER),\n ),\n };\n}\n\nexport type BaseReadModelConfig = {\n readModelId: string;\n /** Rebuilds resultingState for boot replay and sweeps. */\n rebuildStateOnInit: boolean;\n /** Defaults to {@link defaultReadModelIndexingConfig}. */\n indexing?: ReadModelIndexingConfig;\n /** Where a first registration starts; defaults to \"beginning\". */\n startFrom?: \"beginning\" | \"head\";\n /** Re-applies the rest of a late operation's stream; defaults to true. */\n replayStreamSuffix?: boolean;\n};\n\ntype StreamGroup = {\n documentId: string;\n scope: string;\n branch: string;\n lowest: number;\n late: OperationWithContext[];\n};\n\nfunction ordinalOf(item: OperationWithContext): number {\n return item.context.ordinal;\n}\n\nfunction isTracked(ordinal: number): boolean {\n return Number.isFinite(ordinal) && ordinal > 0;\n}\n\nfunction streamKeyOf(stream: {\n documentId: string;\n scope: string;\n branch: string;\n}): string {\n return `${stream.documentId}\\u0000${stream.scope}\\u0000${stream.branch}`;\n}\n\nfunction groupByStream(items: OperationWithContext[]): StreamGroup[] {\n const groups = new Map<string, StreamGroup>();\n for (const item of items) {\n const { documentId, scope, branch } = item.context;\n const key = streamKeyOf(item.context);\n let group = groups.get(key);\n if (group === undefined) {\n group = { documentId, scope, branch, lowest: ordinalOf(item), late: [] };\n groups.set(key, group);\n }\n group.lowest = Math.min(group.lowest, ordinalOf(item));\n group.late.push(item);\n }\n return [...groups.values()].sort((a, b) => a.lowest - b.lowest);\n}\n\nfunction mergeByOrdinal(\n ...lists: OperationWithContext[][]\n): OperationWithContext[] {\n const byOrdinal = new Map<number, OperationWithContext>();\n for (const list of lists) {\n for (const item of list) byOrdinal.set(ordinalOf(item), item);\n }\n return [...byOrdinal.values()].sort((a, b) => ordinalOf(a) - ordinalOf(b));\n}\n\nfunction errorMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n\n/** A read model whose cursor only sweeps and boot replay advance. */\nexport class BaseReadModel implements IReadModel, ICatchUpConsumer {\n readonly name: string;\n\n private readonly indexing: ReadModelIndexingConfig;\n private readonly catchUpLogger: ILogger;\n private cursor: ContiguousCursor;\n private persisted = 0;\n private watermark: ISettledWatermark | undefined;\n private maxTrackedAboveCursor = defaultCatchUpConfig.maxTrackedAboveCursor;\n private loggedFailure: number | undefined;\n private failedItem: OperationWithContext | undefined;\n private initialized = false;\n private readonly liveInFlight = new Map<string, Set<number>>();\n private readonly sweptListeners = new Set<\n (coordinates: ConsistencyCoordinate[]) => void\n >();\n\n constructor(\n protected db: Kysely<DocumentViewDatabase>,\n protected operationIndex: IOperationIndex,\n protected writeCache: IWriteCache,\n protected consistencyTracker: IConsistencyTracker,\n protected config: BaseReadModelConfig,\n ) {\n this.name = config.readModelId;\n this.catchUpLogger = childLogger([\n \"reactor\",\n \"read-model\",\n config.readModelId,\n ]);\n this.indexing = normalizeIndexingConfig(\n config.indexing ?? defaultReadModelIndexingConfig,\n );\n this.cursor = new ContiguousCursor(0, this.maxTrackedAboveCursor);\n }\n\n get consumerId(): string {\n return this.config.readModelId;\n }\n\n get appliedThrough(): number {\n return this.cursor.appliedThrough;\n }\n\n get trackedAbove(): number {\n return this.cursor.trackedAbove;\n }\n\n /** Shares a thread's watermark; otherwise boot probes through this db. */\n attachCatchUp(\n watermark: ISettledWatermark,\n maxTrackedAboveCursor: number,\n ): void {\n this.watermark = watermark;\n this.maxTrackedAboveCursor = maxTrackedAboveCursor;\n this.cursor.setLimit(maxTrackedAboveCursor);\n }\n\n /** Notified with the coordinates each sweep applied. */\n onSwept(\n listener: (coordinates: ConsistencyCoordinate[]) => void,\n ): Unsubscribe {\n this.sweptListeners.add(listener);\n return () => {\n this.sweptListeners.delete(listener);\n };\n }\n\n /** A chunk that throws ends the replay; sweeps continue from below it. */\n async init(): Promise<void> {\n // A repeat init replays from where this process is, keeping what it applied.\n if (this.initialized) {\n await this.replayFromCursor();\n return;\n }\n let stored = await this.loadState();\n\n if (stored === undefined) {\n if (this.config.startFrom === \"head\") {\n const watermark = this.settledWatermark();\n const settled = await watermark.refresh();\n // The head, not settledThrough: any open write elsewhere holds that at 0.\n const head = Math.max(settled, watermark.status().head);\n await this.initializeState(head);\n this.resetCursor(head);\n this.initialized = true;\n return;\n }\n await this.initializeState(0);\n stored = 0;\n }\n\n this.resetCursor(stored);\n this.initialized = true;\n await this.replayFromCursor();\n }\n\n /** The live path: never moves the cursor. */\n indexOperations(items: OperationWithContext[]): Promise<void> {\n if (items.length === 0) return Promise.resolve();\n return this.reserveOperations(items).apply();\n }\n\n /** Holds a queued batch as live, so a sweep neither takes nor passes it. */\n reserveOperations(items: OperationWithContext[]): IReadModelReservation {\n const cursor = this.cursor;\n const owned = this.claimLive(items);\n const unmark = this.markLive(owned);\n let done = false;\n return {\n apply: () => {\n if (done) return Promise.resolve();\n done = true;\n // A cursor reset since the claim dropped it.\n if (cursor !== this.cursor) {\n unmark();\n return this.indexOperations(items);\n }\n if (owned.length === 0) {\n unmark();\n return Promise.resolve();\n }\n return this.applyChunked(owned, unmark);\n },\n release: () => {\n if (done) return;\n done = true;\n cursor.settle(owned.map(ordinalOf), false);\n unmark();\n },\n };\n }\n\n async sweep(\n settledThrough: number,\n present: readonly number[],\n signal?: AbortSignal,\n ): Promise<SweepResult> {\n const startedAt = performance.now();\n const from = this.cursor.appliedThrough;\n const range = present.filter(\n (ordinal) => ordinal > from && ordinal <= settledThrough,\n );\n const mine = this.cursor.claim(this.cursor.missing(range));\n\n let replayed = 0;\n let reapplied = 0;\n let blockedAt: SweepBlockedAt | undefined;\n\n if (mine.size > 0) {\n let late: OperationWithContext[];\n try {\n late = await this.operationIndex.getByOrdinals([...mine], signal);\n } catch (error) {\n this.cursor.settle(mine, false);\n throw error;\n }\n\n const found = new Set(late.map(ordinalOf));\n this.cursor.settle(\n [...mine].filter((ordinal) => !found.has(ordinal)),\n true,\n );\n\n for (const group of groupByStream(late)) {\n const outcome = await this.sweepStream(group, signal);\n replayed += outcome.replayed;\n reapplied += outcome.reapplied;\n blockedAt ??= outcome.blockedAt;\n }\n\n if (replayed > 0) {\n this.catchUpLogger.warn(\n \"@consumer applied @n operations its live path never received: ordinals @first..@last\",\n this.consumerId,\n replayed,\n Math.min(...found),\n Math.max(...found),\n );\n }\n }\n\n const to = this.cursor.target(settledThrough, range);\n if (to > from) {\n await this.moveCursor(to);\n }\n this.cursor.enforceLimit();\n\n return {\n consumerId: this.consumerId,\n from,\n to: this.cursor.appliedThrough,\n durationMs: performance.now() - startedAt,\n replayed,\n reapplied,\n ...(blockedAt !== undefined ? { blockedAt } : {}),\n };\n }\n\n /**\n * Waits for the read model to reach the specified consistency level.\n */\n async waitForConsistency(\n token: ConsistencyToken,\n timeoutMs?: number,\n signal?: AbortSignal,\n ): Promise<void> {\n if (token.coordinates.length === 0) {\n return;\n }\n await this.consistencyTracker.waitFor(token.coordinates, timeoutMs, signal);\n }\n\n /** The cursor, for subclasses: every present ordinal at or below it is applied. */\n protected get lastOrdinal(): number {\n return this.cursor.appliedThrough;\n }\n\n /** Called after the cursor advances. */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n protected onCursorAdvanced(appliedThrough: number): void {}\n\n // Subclass does domain-specific work here (snapshots, relationships, processor routing, etc.).\n protected async commitOperations(\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n items: OperationWithContext[],\n ): Promise<void> {}\n\n /** Rebuilds resultingState as the executor writes it: scopes plus header. */\n protected async rebuildStateForOperations(\n operations: OperationWithContext[],\n ): Promise<OperationWithContext[]> {\n const result: OperationWithContext[] = [];\n\n for (const op of operations) {\n const { documentId, scope, branch } = op.context;\n const targetRevision = op.operation.index;\n\n const document = await this.writeCache.getState(\n documentId,\n scope,\n branch,\n targetRevision,\n );\n\n result.push({\n operation: op.operation,\n context: {\n ...op.context,\n resultingState: JSON.stringify({\n ...document.state,\n header: document.header,\n }),\n },\n });\n }\n\n return result;\n }\n\n /**\n * Loads the last processed ordinal from the ViewState table.\n * Returns undefined if no state exists for this read model.\n */\n protected async loadState(): Promise<number | undefined> {\n const row = await this.db\n .selectFrom(\"ViewState\")\n .select(\"lastOrdinal\")\n .where(\"readModelId\", \"=\", this.config.readModelId)\n .executeTakeFirst();\n\n return row?.lastOrdinal;\n }\n\n /**\n * Initializes the ViewState row for this read model.\n */\n protected async initializeState(lastOrdinal = 0): Promise<void> {\n await this.db\n .insertInto(\"ViewState\")\n .values({\n readModelId: this.config.readModelId,\n lastOrdinal,\n })\n .onConflict((oc) => oc.column(\"readModelId\").doNothing())\n .execute();\n }\n\n /**\n * Updates the consistency tracker with the processed operations.\n */\n protected updateConsistencyTracker(items: OperationWithContext[]): void {\n this.consistencyTracker.update(this.coordinatesOf(items));\n }\n\n private coordinatesOf(\n items: OperationWithContext[],\n ): ConsistencyCoordinate[] {\n const coordinates: ConsistencyCoordinate[] = [];\n for (let i = 0; i < items.length; i++) {\n const item = items[i]!;\n coordinates.push({\n documentId: item.context.documentId,\n scope: item.context.scope,\n branch: item.context.branch,\n operationIndex: item.operation.index,\n });\n }\n return coordinates;\n }\n\n private settledWatermark(): ISettledWatermark {\n this.watermark ??= new SettledWatermark(\n createKyselyWatermarkProbe(this.db as unknown as Kysely<StorageDatabase>),\n this.catchUpLogger,\n );\n return this.watermark;\n }\n\n private resetCursor(appliedThrough: number): void {\n this.cursor = new ContiguousCursor(\n appliedThrough,\n this.maxTrackedAboveCursor,\n );\n this.persisted = appliedThrough;\n }\n\n /** Items this pass must apply; untracked ordinals always apply. */\n private claimLive(items: OperationWithContext[]): OperationWithContext[] {\n const mine = this.cursor.claim(items.map(ordinalOf));\n return items.filter((item) => {\n const ordinal = ordinalOf(item);\n return !isTracked(ordinal) || mine.has(ordinal);\n });\n }\n\n /** Marks items in flight on their streams; returns the release. */\n private markLive(items: OperationWithContext[]): () => void {\n const keys = new Set<string>();\n for (const item of items) {\n if (!isTracked(ordinalOf(item))) continue;\n const key = streamKeyOf(item.context);\n let ordinals = this.liveInFlight.get(key);\n if (ordinals === undefined) {\n ordinals = new Set();\n this.liveInFlight.set(key, ordinals);\n }\n ordinals.add(ordinalOf(item));\n keys.add(key);\n }\n return () => {\n for (const item of items) {\n this.liveInFlight\n .get(streamKeyOf(item.context))\n ?.delete(ordinalOf(item));\n }\n for (const key of keys) {\n if (this.liveInFlight.get(key)?.size === 0)\n this.liveInFlight.delete(key);\n }\n };\n }\n\n /** True while a live pass applies an earlier operation of the stream. */\n private liveBelow(group: StreamGroup): boolean {\n const ordinals = this.liveInFlight.get(streamKeyOf(group));\n if (ordinals === undefined) return false;\n for (const ordinal of ordinals) {\n if (ordinal < group.lowest) return true;\n }\n return false;\n }\n\n /** Commits in chunks, yielding with no transaction open. */\n private async applyChunked(\n items: OperationWithContext[],\n release = this.markLive(items),\n ): Promise<void> {\n try {\n const { commitChunkSize, yieldDeadlineMs } = this.indexing;\n let lastYield = performance.now();\n let committed = 0;\n\n for (let start = 0; start < items.length; start += commitChunkSize) {\n if (start > 0 && performance.now() - lastYield > yieldDeadlineMs) {\n await yieldToMain();\n lastYield = performance.now();\n }\n\n const chunk = items.slice(start, start + commitChunkSize);\n\n try {\n await this.commitOperations(chunk);\n } catch (error) {\n this.failedItem = chunk[0];\n const prefix = items.slice(0, committed);\n this.cursor.settle(prefix.map(ordinalOf), true);\n this.cursor.settle(items.slice(committed).map(ordinalOf), false);\n if (prefix.length > 0) this.updateConsistencyTracker(prefix);\n throw error;\n }\n\n committed += chunk.length;\n }\n\n this.cursor.settle(items.map(ordinalOf), true);\n this.cursor.enforceLimit();\n this.updateConsistencyTracker(items);\n } finally {\n release();\n }\n }\n\n private async replayFromCursor(): Promise<void> {\n const settledAtBoot = await this.settledAtBoot();\n let page = await this.operationIndex.getSinceOrdinal(\n this.cursor.appliedThrough,\n );\n\n while (page.results.length > 0) {\n const ordinals = page.results.map(ordinalOf);\n const failed = await this.replayPage(page.results);\n\n const pageMax = Math.max(...ordinals);\n const to = this.cursor.target(Math.min(settledAtBoot, pageMax), ordinals);\n if (to > this.cursor.appliedThrough) {\n await this.moveCursor(to);\n }\n\n if (failed || !page.next) break;\n page = await page.next();\n }\n }\n\n /** Without a probe, boot replay applies but leaves the cursor to sweeps. */\n private async settledAtBoot(): Promise<number> {\n try {\n return await this.settledWatermark().refresh();\n } catch (error) {\n this.catchUpLogger.warn(\n \"@consumer could not probe the settled watermark at boot; the cursor waits for a sweep: @error\",\n this.consumerId,\n error,\n );\n return this.cursor.appliedThrough;\n }\n }\n\n /** Returns true when a chunk failed, which ends the replay. */\n private async replayPage(results: OperationWithContext[]): Promise<boolean> {\n const owned = this.claimLive(results);\n if (owned.length === 0) return false;\n const release = this.markLive(owned);\n try {\n return await this.replayOwned(owned);\n } finally {\n release();\n }\n }\n\n private async replayOwned(owned: OperationWithContext[]): Promise<boolean> {\n let rebuilt: { items: OperationWithContext[]; absent: number[] };\n try {\n rebuilt = await this.rebuildIfConfigured(owned);\n } catch (error) {\n this.cursor.settle(owned.map(ordinalOf), false);\n this.block(owned[0]!, error);\n return true;\n }\n this.cursor.settle(rebuilt.absent, true);\n if (rebuilt.items.length === 0) return false;\n\n try {\n await this.applyChunked(rebuilt.items);\n } catch (error) {\n this.block(this.failedItem ?? rebuilt.items[0]!, error);\n return true;\n }\n return false;\n }\n\n private async sweepStream(\n group: StreamGroup,\n signal: AbortSignal | undefined,\n ): Promise<{\n replayed: number;\n reapplied: number;\n blockedAt?: SweepBlockedAt;\n }> {\n const owned = group.late.map(ordinalOf);\n if (this.liveBelow(group)) {\n this.cursor.settle(owned, false);\n return { replayed: 0, reapplied: 0 };\n }\n const ownedSet = new Set(owned);\n\n let items = group.late;\n if (this.config.replayStreamSuffix ?? true) {\n let suffix: OperationWithContext[];\n try {\n suffix = await this.operationIndex.getStreamAfter(\n group,\n group.lowest,\n signal,\n );\n } catch (error) {\n this.cursor.settle(owned, false);\n return {\n replayed: 0,\n reapplied: 0,\n blockedAt: this.block(group.late[0]!, error),\n };\n }\n items = mergeByOrdinal(group.late, suffix);\n }\n\n let rebuilt: { items: OperationWithContext[]; absent: number[] };\n try {\n rebuilt = await this.rebuildIfConfigured(items);\n } catch (error) {\n this.cursor.settle(owned, false);\n return {\n replayed: 0,\n reapplied: 0,\n blockedAt: this.block(items[0]!, error),\n };\n }\n\n try {\n await this.commitOperations(rebuilt.items);\n } catch (error) {\n this.cursor.settle(owned, false);\n return {\n replayed: 0,\n reapplied: 0,\n blockedAt: this.block(rebuilt.items[0] ?? items[0]!, error),\n };\n }\n\n this.cursor.settle(owned, true);\n this.updateConsistencyTracker(rebuilt.items);\n this.notifySwept(rebuilt.items);\n\n let replayed = 0;\n for (const item of rebuilt.items) {\n if (ownedSet.has(ordinalOf(item))) replayed++;\n }\n return { replayed, reapplied: rebuilt.items.length - replayed };\n }\n\n /** A document gone from the write cache is absent, not a failure. */\n private async rebuildIfConfigured(\n items: OperationWithContext[],\n ): Promise<{ items: OperationWithContext[]; absent: number[] }> {\n if (!this.config.rebuildStateOnInit) {\n return { items, absent: [] };\n }\n\n const rebuilt: OperationWithContext[] = [];\n const absent: number[] = [];\n for (const item of items) {\n let result: OperationWithContext[];\n try {\n result = await this.rebuildStateForOperations([item]);\n } catch (error) {\n if (!(error instanceof DocumentNotFoundError)) throw error;\n this.catchUpLogger.warn(\n \"@consumer dropped ordinal @ordinal: document @documentId is gone\",\n this.consumerId,\n ordinalOf(item),\n item.context.documentId,\n );\n absent.push(ordinalOf(item));\n continue;\n }\n rebuilt.push(...result);\n }\n return { items: rebuilt, absent };\n }\n\n private block(item: OperationWithContext, error: unknown): SweepBlockedAt {\n const blockedAt: SweepBlockedAt = {\n ordinal: ordinalOf(item),\n documentId: item.context.documentId,\n scope: item.context.scope,\n branch: item.context.branch,\n type: item.operation.action.type,\n error: errorMessage(error),\n };\n if (this.loggedFailure !== blockedAt.ordinal) {\n this.loggedFailure = blockedAt.ordinal;\n this.catchUpLogger.error(\n \"@consumer cursor held at @applied: ordinal @ordinal (@documentId/@scope/@branch, @type) failed: @error\",\n this.consumerId,\n this.cursor.appliedThrough,\n blockedAt.ordinal,\n blockedAt.documentId,\n blockedAt.scope,\n blockedAt.branch,\n blockedAt.type,\n blockedAt.error,\n );\n }\n return blockedAt;\n }\n\n private notifySwept(items: OperationWithContext[]): void {\n if (this.sweptListeners.size === 0 || items.length === 0) return;\n const coordinates = this.coordinatesOf(items);\n for (const listener of this.sweptListeners) {\n listener(coordinates);\n }\n }\n\n /** Compare-and-set against the value this process last wrote. */\n private async moveCursor(to: number): Promise<void> {\n if (to <= this.persisted) {\n this.advanceCursor(to);\n return;\n }\n\n const expected = this.persisted;\n const result = await this.db\n .updateTable(\"ViewState\")\n .set({ lastOrdinal: to, lastOperationTimestamp: new Date() })\n .where(\"readModelId\", \"=\", this.config.readModelId)\n .where(\"lastOrdinal\", \"=\", expected)\n .executeTakeFirst();\n\n if (Number(result.numUpdatedRows) > 0) {\n this.persisted = to;\n this.advanceCursor(to);\n return;\n }\n\n const stored = await this.loadState();\n if (stored === undefined) {\n await this.initializeState(0);\n this.lowered(0);\n return;\n }\n if (stored < this.cursor.appliedThrough) {\n this.lowered(stored);\n return;\n }\n this.persisted = stored;\n this.advanceCursor(Math.min(to, stored));\n }\n\n private advanceCursor(to: number): void {\n const before = this.cursor.appliedThrough;\n this.cursor.advance(to);\n if (this.cursor.appliedThrough > before) {\n this.onCursorAdvanced(this.cursor.appliedThrough);\n }\n }\n\n private lowered(to: number): void {\n this.catchUpLogger.info(\n \"@consumer cursor lowered externally from @old to @new; replaying\",\n this.consumerId,\n this.cursor.appliedThrough,\n to,\n );\n this.resetCursor(to);\n }\n}\n","import type { OperationWithContext } from \"@powerhousedao/shared/document-model\";\nimport { childLogger, type ILogger } from \"document-model\";\nimport type { IEventBus } from \"../events/interfaces.js\";\nimport {\n ReactorEventTypes,\n type JobReadReadyEvent,\n type JobWriteReadyEvent,\n type ReadModelBatchCompletedEvent,\n type ReadModelIndexedEvent,\n type ReadModelIndexingStage,\n type Unsubscribe,\n} from \"../events/types.js\";\nimport type {\n ILiveReadModelCoordinator,\n IReadModel,\n IReadModelReservation,\n ReadModelRegistrationStage,\n} from \"./interfaces.js\";\n\nexport type BatchReservations = Map<IReadModel, IReadModelReservation>;\n\n/** Claims a batch for every model that supports it, as the batch queues. */\nexport function reserveBatch(\n readModels: readonly IReadModel[],\n operations: OperationWithContext[],\n logger: ILogger,\n): BatchReservations {\n const reservations: BatchReservations = new Map();\n for (const readModel of readModels) {\n if (readModel.reserveOperations === undefined) continue;\n try {\n reservations.set(readModel, readModel.reserveOperations(operations));\n } catch (error) {\n logger.error(\n \"Read model @name could not reserve a batch: @Error\",\n readModel.name,\n error,\n );\n }\n }\n return reservations;\n}\n\n/** Applies the model's reservation, or indexes a model registered since. */\nexport function indexReserved(\n readModel: IReadModel,\n operations: OperationWithContext[],\n reservations: BatchReservations,\n): Promise<void> {\n const reservation = reservations.get(readModel);\n return reservation\n ? reservation.apply()\n : readModel.indexOperations(operations);\n}\n\n/** Frees every claim a run did not apply, so a sweep can take it. */\nexport function releaseBatch(reservations: BatchReservations): void {\n for (const reservation of reservations.values()) reservation.release();\n}\n\n/**\n * Coordinates read model synchronization by listening to operation write events\n * and updating all registered read models on per-`documentId:scope:branch`\n * serial chains. Cross-key projection runs in parallel; same-key projection is\n * serialized so the executor can return to dispatch without holding ordering\n * implicitly.\n */\nexport class ReadModelCoordinator implements ILiveReadModelCoordinator {\n private unsubscribe?: Unsubscribe;\n private isRunning = false;\n private readonly chains = new Map<string, Promise<void>>();\n private readonly logger: ILogger;\n\n readonly readModels: IReadModel[];\n\n constructor(\n private eventBus: IEventBus,\n public readonly preReady: IReadModel[],\n public readonly postReady: IReadModel[],\n ) {\n this.readModels = [...preReady, ...postReady];\n this.logger = childLogger([\"reactor\", \"read-model-coordinator\"]);\n }\n\n start(): void {\n if (this.isRunning) {\n return;\n }\n\n this.unsubscribe = this.eventBus.subscribe(\n ReactorEventTypes.JOB_WRITE_READY,\n (type, event: JobWriteReadyEvent) => {\n return this.handleWriteReady(event);\n },\n );\n\n this.isRunning = true;\n }\n\n stop(): void {\n if (!this.isRunning) {\n return;\n }\n\n if (this.unsubscribe) {\n this.unsubscribe();\n this.unsubscribe = undefined;\n }\n\n this.isRunning = false;\n }\n\n /**\n * Resolves when every per-queueKey projection chain has flushed. Intended\n * for test fixtures and explicit shutdown; production callers use\n * consistency tokens instead.\n */\n async drain(): Promise<void> {\n while (this.chains.size > 0) {\n const pending = Array.from(this.chains.values());\n await Promise.allSettled(pending);\n }\n }\n\n getChainDepth(): number {\n return this.chains.size;\n }\n\n indexedReadModels(): readonly IReadModel[] {\n return [...this.preReady, ...this.postReady];\n }\n\n addReadModel(readModel: IReadModel, stage: ReadModelRegistrationStage): void {\n if (this.readModels.some(({ name }) => name === readModel.name)) {\n throw new Error(`Read model \"${readModel.name}\" is already registered`);\n }\n\n if (stage === \"pre_ready\") {\n this.preReady.push(readModel);\n } else {\n this.postReady.push(readModel);\n }\n this.readModels.push(readModel);\n }\n\n private handleWriteReady(event: JobWriteReadyEvent): void {\n if (event.operations.length === 0) {\n void this.emitEmptyReadReady(event);\n return;\n }\n\n const enqueuedAt = performance.now();\n const key = this.queueKeyFor(event);\n const reservations = reserveBatch(\n this.readModels,\n event.operations,\n this.logger,\n );\n const run = () => this.runChain(event, enqueuedAt, reservations);\n const previous = this.chains.get(key) ?? Promise.resolve();\n const current = previous.then(run, run);\n\n this.chains.set(key, current);\n void current.finally(() => {\n if (this.chains.get(key) === current) {\n this.chains.delete(key);\n }\n });\n }\n\n private async emitEmptyReadReady(event: JobWriteReadyEvent): Promise<void> {\n const readyEvent: JobReadReadyEvent = {\n jobId: event.jobId,\n operations: event.operations,\n };\n try {\n await this.eventBus.emit(ReactorEventTypes.JOB_READ_READY, readyEvent);\n } catch (error) {\n this.logger.error(\n \"JOB_READ_READY emit failed for job @jobId: @Error\",\n { jobId: event.jobId },\n error,\n );\n }\n this.emitBatchCompleted({\n jobId: event.jobId,\n batchSize: 0,\n chainWaitDurationMs: 0,\n preReadyDurationMs: 0,\n emitDurationMs: 0,\n postReadyDurationMs: 0,\n });\n }\n\n private async runChain(\n event: JobWriteReadyEvent,\n enqueuedAt: number,\n reservations: BatchReservations,\n ): Promise<void> {\n try {\n await this.runBatch(event, enqueuedAt, reservations);\n } finally {\n releaseBatch(reservations);\n }\n }\n\n private async runBatch(\n event: JobWriteReadyEvent,\n enqueuedAt: number,\n reservations: BatchReservations,\n ): Promise<void> {\n const chainStartedAt = performance.now();\n const chainWaitDurationMs = chainStartedAt - enqueuedAt;\n\n const preReadyStart = performance.now();\n try {\n await Promise.all(\n this.preReady.map((readModel) =>\n this.indexWithTiming(readModel, \"pre_ready\", event, reservations),\n ),\n );\n } catch (error) {\n this.logger.error(\n \"Pre-ready read model indexing failed for job @jobId: @Error\",\n { jobId: event.jobId },\n error,\n );\n }\n const preReadyDurationMs = performance.now() - preReadyStart;\n\n const readyEvent: JobReadReadyEvent = {\n jobId: event.jobId,\n operations: event.operations,\n };\n const emitStart = performance.now();\n try {\n await this.eventBus.emit(ReactorEventTypes.JOB_READ_READY, readyEvent);\n } catch (error) {\n this.logger.error(\n \"JOB_READ_READY emit failed for job @jobId: @Error\",\n { jobId: event.jobId },\n error,\n );\n }\n const emitDurationMs = performance.now() - emitStart;\n\n const postReadyStart = performance.now();\n try {\n await Promise.all(\n this.postReady.map((readModel) =>\n this.indexWithTiming(readModel, \"post_ready\", event, reservations),\n ),\n );\n } catch (error) {\n this.logger.error(\n \"Post-ready read model indexing failed for job @jobId: @Error\",\n { jobId: event.jobId },\n error,\n );\n }\n const postReadyDurationMs = performance.now() - postReadyStart;\n\n this.emitBatchCompleted({\n jobId: event.jobId,\n batchSize: event.operations.length,\n chainWaitDurationMs,\n preReadyDurationMs,\n emitDurationMs,\n postReadyDurationMs,\n });\n }\n\n private async indexWithTiming(\n readModel: IReadModel,\n stage: ReadModelIndexingStage,\n event: JobWriteReadyEvent,\n reservations: BatchReservations,\n ): Promise<void> {\n const start = performance.now();\n let success = false;\n try {\n await indexReserved(readModel, event.operations, reservations);\n success = true;\n } finally {\n this.emitReadModelIndexed({\n jobId: event.jobId,\n readModelName: readModel.name,\n stage,\n durationMs: performance.now() - start,\n operationCount: event.operations.length,\n success,\n });\n }\n }\n\n private emitReadModelIndexed(payload: ReadModelIndexedEvent): void {\n void this.eventBus\n .emit(ReactorEventTypes.READMODEL_INDEXED, payload)\n .catch((err: unknown) =>\n this.logger.error(\n \"READMODEL_INDEXED emit failed for job @jobId: @Error\",\n { jobId: payload.jobId },\n err,\n ),\n );\n }\n\n private emitBatchCompleted(payload: ReadModelBatchCompletedEvent): void {\n void this.eventBus\n .emit(ReactorEventTypes.READMODEL_BATCH_COMPLETED, payload)\n .catch((err: unknown) =>\n this.logger.error(\n \"READMODEL_BATCH_COMPLETED emit failed for job @jobId: @Error\",\n { jobId: payload.jobId },\n err,\n ),\n );\n }\n\n private queueKeyFor(event: JobWriteReadyEvent): string {\n const ctx = event.operations[0]!.context;\n return `${ctx.documentId}:${ctx.scope}:${ctx.branch}`;\n }\n}\n","import type { IDocumentIndexer, IDocumentView } from \"../storage/interfaces.js\";\n\n/** `getReadModel` name of the document view; see {@link ReactorReadModels}. */\nexport const DOCUMENT_VIEW_READ_MODEL = \"document-view\";\n/** `getReadModel` name of the document indexer; see {@link ReactorReadModels}. */\nexport const DOCUMENT_INDEXER_READ_MODEL = \"document-indexer\";\n\n/**\n * Read models the reactor registers on every host, keyed by the name passed to\n * `IProcessorHostModule.getReadModel`. Each entry says what the model holds\n * and when a processor should reach for it instead of the reactor client.\n */\nexport interface ReactorReadModels {\n /**\n * Materialized document snapshots — header plus per-scope state — kept\n * current as operations are indexed, so reads never replay operations.\n *\n * Reach for it to read a document's current state from inside a\n * processor: `get` / `getMany` by id, `getByIdOrSlug`, `findByType`,\n * `exists`, and `resolveSlug` / `resolveIdOrSlug`. Every query takes an\n * optional `ConsistencyToken` (from a job result) for read-after-write.\n * Unlike `client.get`, results are the raw snapshot — scopes are not\n * filtered to what the caller may read.\n */\n [DOCUMENT_VIEW_READ_MODEL]: IDocumentView;\n\n /**\n * Directed graph of document relationships, built from ADD_RELATIONSHIP /\n * REMOVE_RELATIONSHIP operations. Edges carry a `relationshipType`;\n * parent/child links (drive → document, folder → file) use `\"child\"`.\n *\n * Reach for it to navigate structure rather than content: `getOutgoing` /\n * `getIncoming` neighbours, `hasRelationship`, `findPath`, and\n * `findAncestors` (e.g. which drive(s) contain a document). Snapshots of\n * the documents themselves come from the document view.\n */\n [DOCUMENT_INDEXER_READ_MODEL]: IDocumentIndexer;\n}\n","import type {\n OperationWithContext,\n PHAuthState,\n PHDocument,\n PHDocumentHeader,\n} from \"@powerhousedao/shared/document-model\";\nimport {\n createAuthState,\n isDenied,\n} from \"@powerhousedao/shared/document-model\";\nimport type { Kysely } from \"kysely\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport type { IOperationIndex } from \"../cache/operation-index-types.js\";\nimport type { IWriteCache } from \"../cache/write/interfaces.js\";\nimport type { IConsistencyTracker } from \"../shared/consistency-tracker.js\";\nimport { DOCUMENT_VIEW_READ_MODEL } from \"./names.js\";\nimport type {\n ConsistencyToken,\n PagedResults,\n PagingOptions,\n} from \"../shared/types.js\";\nimport {\n DocumentExistence,\n type DocumentRevisions,\n type IDocumentView,\n type IOperationStore,\n type ViewFilter,\n} from \"../storage/interfaces.js\";\nimport type { Database as StorageDatabase } from \"../storage/kysely/types.js\";\nimport {\n BaseReadModel,\n defaultReadModelIndexingConfig,\n type ReadModelIndexingConfig,\n} from \"./base-read-model.js\";\nimport type {\n DocumentViewDatabase,\n InsertableDocumentSnapshot,\n} from \"./types.js\";\n\ntype Database = StorageDatabase & DocumentViewDatabase;\n\n/**\n * What a single-document read of a deleted document returns. A listing omits a\n * deleted document under either value.\n */\nexport enum DeletedDocumentRead {\n /**\n * The document reads as missing: `get` throws and `resolveIdOrSlug` does not\n * match its id.\n */\n NotFound = \"NotFound\",\n /**\n * The document's state as of the deletion, with `state.document.isDeleted`\n * telling the caller what it holds. Only meaningful with `documentDecisions`,\n * which is what makes deletion positional.\n */\n StateAtDeletion = \"StateAtDeletion\",\n}\n\nexport class KyselyDocumentView extends BaseReadModel implements IDocumentView {\n private _db: Kysely<Database>;\n\n constructor(\n db: Kysely<Database>,\n private operationStore: IOperationStore,\n operationIndex: IOperationIndex,\n writeCache: IWriteCache,\n consistencyTracker: IConsistencyTracker,\n private readonly deletedDocumentRead: DeletedDocumentRead,\n indexing: ReadModelIndexingConfig = defaultReadModelIndexingConfig,\n ) {\n super(\n db as unknown as Kysely<DocumentViewDatabase>,\n operationIndex,\n writeCache,\n consistencyTracker,\n {\n readModelId: DOCUMENT_VIEW_READ_MODEL,\n rebuildStateOnInit: true,\n indexing,\n replayStreamSuffix: false,\n },\n );\n this._db = db;\n }\n\n /**\n * Indexes committed operations into DocumentSnapshot rows. CREATE_DOCUMENT\n * only seeds header/document/auth. UPGRADE_DOCUMENT reindexes every scope\n * present in resultingState when the operation vouches for them — a seed\n * carrying initialState or a migration stamped with the __migrated marker\n * — since the upgrade reducer may have reshaped any of them; upgrades\n * without either fall back to header/document/auth, because their sibling\n * echoes may be stale. All other action types index only header and their\n * own scope.\n *\n * Every row accepts a write only from an operation whose global ordinal is\n * at least the one the row already carries, so an older duplicate from a\n * sweep, boot replay or chunk interleave never rolls a scope back.\n */\n protected override async commitOperations(\n items: OperationWithContext[],\n ): Promise<void> {\n await this._db.transaction().execute(async (trx) => {\n for (const item of items) {\n const { operation, context } = item;\n const {\n documentId,\n scope,\n branch,\n documentType,\n resultingState,\n ordinal,\n } = context;\n const { index, hash } = operation;\n\n if (!resultingState) {\n throw new Error(\n `Missing resultingState in context for operation ${operation.id || \"unknown\"}. ` +\n `IDocumentView requires resultingState from upstream - it does not rebuild documents.`,\n );\n }\n\n let fullState: Record<string, unknown>;\n try {\n fullState = JSON.parse(resultingState) as Record<string, unknown>;\n } catch (error) {\n throw new Error(\n `Failed to parse resultingState for operation ${operation.id || \"unknown\"}: ${error instanceof Error ? error.message : String(error)}`,\n { cause: error },\n );\n }\n\n const operationType = operation.action.type;\n\n // A refused operation holds its index and changes nothing, so it must\n // not carry its action type's side effects. A refused DELETE_DOCUMENT\n // is the case that matters: taking the branch below would hide the\n // document from every listing and destroy its slug mapping, with\n // nothing in any path to put either back.\n const denied = isDenied(operation);\n\n // Non-header ops carry a denormalized header echo with possibly stale\n // `meta`; only header-scope ops and CREATE/UPGRADE/DELETE may write it.\n const preserveHeaderMeta =\n denied ||\n (operationType !== \"CREATE_DOCUMENT\" &&\n operationType !== \"UPGRADE_DOCUMENT\" &&\n operationType !== \"DELETE_DOCUMENT\" &&\n scope !== \"header\");\n\n if (operationType === \"DELETE_DOCUMENT\" && !denied) {\n const now = new Date();\n await trx\n .updateTable(\"DocumentSnapshot\")\n .set({\n isDeleted: true,\n deletedAt: now,\n lastOperationIndex: index,\n lastOperationHash: hash,\n lastOperationOrdinal: ordinal,\n lastUpdatedAt: now,\n })\n .where(\"documentId\", \"=\", documentId)\n .where(\"branch\", \"=\", branch)\n .where(\"lastOperationOrdinal\", \"<=\", ordinal)\n .execute();\n\n // The content has to say so too, or a caller served the boundary state\n // would get state claiming the document is live.\n const deletedDocumentScope = (\n fullState as Record<string, unknown> | undefined\n )?.document;\n if (deletedDocumentScope !== undefined) {\n await trx\n .updateTable(\"DocumentSnapshot\")\n .set({ content: deletedDocumentScope })\n .where(\"documentId\", \"=\", documentId)\n .where(\"branch\", \"=\", branch)\n .where(\"scope\", \"=\", \"document\")\n .where(\"lastOperationOrdinal\", \"<=\", ordinal)\n .execute();\n }\n\n await trx\n .deleteFrom(\"SlugMapping\")\n .where(\"documentId\", \"=\", documentId)\n .where(\"branch\", \"=\", branch)\n .execute();\n\n continue;\n }\n\n let scopesToIndex: Array<[string, unknown]>;\n\n if (operationType === \"CREATE_DOCUMENT\") {\n scopesToIndex = Object.entries(fullState).filter(\n ([key]) => key === \"header\" || key === \"document\" || key === \"auth\",\n );\n } else if (operationType === \"UPGRADE_DOCUMENT\") {\n // Sibling-scope echoes are only trustworthy when the operation\n // itself defines them: a seed carrying initialState, or a migration\n // whose executor vouched (via the __migrated marker) that it\n // fetched every scope fresh. Upgrades persisted by executors that\n // never fetched sibling scopes echo whatever stale state the write\n // cache held, and indexing those would clobber good snapshots.\n const upgradeInput = operation.action.input as {\n initialState?: unknown;\n };\n const scopesVouchedFor =\n fullState.__migrated === true ||\n upgradeInput.initialState !== undefined;\n delete fullState.__migrated;\n\n scopesToIndex = scopesVouchedFor\n ? Object.entries(fullState)\n : Object.entries(fullState).filter(\n ([key]) =>\n key === \"header\" || key === \"document\" || key === \"auth\",\n );\n } else {\n scopesToIndex = [];\n\n if (fullState.header !== undefined) {\n scopesToIndex.push([\"header\", fullState.header]);\n }\n\n if (fullState[scope] !== undefined) {\n scopesToIndex.push([scope, fullState[scope]]);\n } else {\n scopesToIndex.push([scope, {}]);\n }\n }\n\n for (const [scopeName, scopeState] of scopesToIndex) {\n // The previous `content` is the whole prior scope state - hundreds of\n // kilobytes of jsonb on a large drive - and only the header-meta\n // carry-over below reads it. Selecting it unconditionally made every\n // write pay to decompress and ship a copy of the state it was about\n // to overwrite.\n const needsExistingContent =\n scopeName === \"header\" && preserveHeaderMeta;\n\n const existingSnapshot = await trx\n .selectFrom(\"DocumentSnapshot\")\n .select([\n \"slug\",\n \"name\",\n \"isDeleted\",\n \"snapshotVersion\",\n \"lastOperationOrdinal\",\n ])\n .$if(needsExistingContent, (qb) => qb.select(\"content\"))\n .where(\"documentId\", \"=\", documentId)\n .where(\"scope\", \"=\", scopeName)\n .where(\"branch\", \"=\", branch)\n .executeTakeFirst();\n\n // An older operation carries stale state for the whole scope.\n if (\n existingSnapshot !== undefined &&\n existingSnapshot.lastOperationOrdinal > ordinal\n ) {\n continue;\n }\n\n const newState =\n typeof scopeState === \"object\" && scopeState !== null\n ? (scopeState as Record<string, unknown>)\n : {};\n\n let slug: string | null = existingSnapshot?.slug ?? null;\n let name: string | null = existingSnapshot?.name ?? null;\n\n if (scopeName === \"header\") {\n const headerSlug = newState.slug;\n const headerName = newState.name;\n\n if (typeof headerSlug === \"string\") {\n slug = headerSlug;\n }\n if (typeof headerName === \"string\") {\n name = headerName;\n }\n\n if (needsExistingContent && existingSnapshot) {\n const existingHeader = existingSnapshot.content as Record<\n string,\n unknown\n >;\n if (existingHeader && \"meta\" in existingHeader) {\n newState.meta = existingHeader.meta;\n }\n }\n\n // Never for a deleted document. Deleting removes its SlugMapping\n // rows, and resolveIdOrSlug relies on that to keep the slug branch\n // from reaching one: re-inserting here would make the document\n // resolvable by slug again, and the upsert would take the slug from\n // a live document that has since claimed it.\n if (slug && slug !== documentId && !existingSnapshot?.isDeleted) {\n await trx\n .insertInto(\"SlugMapping\")\n .values({\n slug,\n documentId,\n scope: scopeName,\n branch,\n })\n .onConflict((oc) =>\n oc.column(\"slug\").doUpdateSet({\n documentId,\n scope: scopeName,\n branch,\n }),\n )\n .execute();\n }\n }\n\n if (existingSnapshot) {\n await trx\n .updateTable(\"DocumentSnapshot\")\n .set({\n lastOperationIndex: index,\n lastOperationHash: hash,\n lastOperationOrdinal: ordinal,\n lastUpdatedAt: new Date(),\n snapshotVersion: existingSnapshot.snapshotVersion + 1,\n content: newState,\n slug,\n name,\n })\n .where(\"documentId\", \"=\", documentId)\n .where(\"scope\", \"=\", scopeName)\n .where(\"branch\", \"=\", branch)\n // Repeats the guard where the database can enforce it.\n .where(\"lastOperationOrdinal\", \"<=\", ordinal)\n .execute();\n } else {\n const snapshot: InsertableDocumentSnapshot = {\n id: uuidv4(),\n documentId,\n slug,\n name,\n scope: scopeName,\n branch,\n content: newState,\n documentType,\n lastOperationIndex: index,\n lastOperationHash: hash,\n lastOperationOrdinal: ordinal,\n identifiers: null,\n metadata: null,\n deletedAt: null,\n };\n\n await trx.insertInto(\"DocumentSnapshot\").values(snapshot).execute();\n }\n }\n }\n });\n }\n\n async exists(\n documentIds: string[],\n existence: DocumentExistence,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<boolean[]> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n if (documentIds.length === 0) {\n return [];\n }\n\n const existingIds =\n existence === DocumentExistence.IncludingDeleted\n ? await this.idsWithStreams(documentIds, signal)\n : await this.liveIds(documentIds);\n\n return documentIds.map((id) => existingIds.has(id));\n }\n\n async get<TDocument extends PHDocument>(\n documentId: string,\n view?: ViewFilter,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<TDocument> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const branch = view?.branch || \"main\";\n\n let scopesToQuery: string[];\n if (view?.scopes && view.scopes.length > 0) {\n scopesToQuery = [...new Set([\"header\", \"document\", ...view.scopes])];\n } else {\n scopesToQuery = [];\n }\n\n let query = this._db\n .selectFrom(\"DocumentSnapshot\")\n .selectAll()\n .where(\"documentId\", \"=\", documentId)\n .where(\"branch\", \"=\", branch);\n\n if (this.deletedDocumentRead === DeletedDocumentRead.NotFound) {\n query = query.where(\"isDeleted\", \"=\", false);\n }\n\n if (scopesToQuery.length > 0) {\n query = query.where(\"scope\", \"in\", scopesToQuery);\n }\n\n const snapshots = await query.execute();\n\n if (snapshots.length === 0) {\n throw new Error(`Document not found: ${documentId}`);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const headerSnapshot = snapshots.find((s) => s.scope === \"header\");\n if (!headerSnapshot) {\n throw new Error(`Document header not found: ${documentId}`);\n }\n\n const header = headerSnapshot.content as PHDocumentHeader;\n\n const revisions = await this.operationStore.getRevisions(\n documentId,\n branch,\n signal,\n );\n header.revision = revisions.revision;\n header.lastModifiedAtUtcIso = revisions.latestTimestamp;\n\n const state: Record<string, unknown> = {};\n for (const snapshot of snapshots) {\n if (snapshot.scope === \"header\") {\n continue;\n }\n\n // snapshot rows written before PHAuthState had a version carry auth: {}\n state[snapshot.scope] =\n snapshot.scope === \"auth\"\n ? createAuthState(snapshot.content as Partial<PHAuthState>)\n : snapshot.content;\n }\n\n const document: PHDocument = {\n header,\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n state: state as any,\n operations: {},\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n initialState: state as any,\n clipboard: [],\n };\n\n return document as TDocument;\n }\n\n async getMany<TDocument extends PHDocument>(\n documentIds: string[],\n view?: ViewFilter,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<TDocument[]> {\n if (documentIds.length === 0) {\n return [];\n }\n\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const branch = view?.branch || \"main\";\n\n let scopesToQuery: string[];\n if (view?.scopes && view.scopes.length > 0) {\n scopesToQuery = [...new Set([\"header\", \"document\", ...view.scopes])];\n } else {\n scopesToQuery = [];\n }\n\n let query = this._db\n .selectFrom(\"DocumentSnapshot\")\n .selectAll()\n .where(\"documentId\", \"in\", documentIds)\n .where(\"branch\", \"=\", branch)\n .where(\"isDeleted\", \"=\", false);\n\n if (scopesToQuery.length > 0) {\n query = query.where(\"scope\", \"in\", scopesToQuery);\n }\n\n const allSnapshots = await query.execute();\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const snapshotsByDocId = new Map<string, typeof allSnapshots>();\n for (const snapshot of allSnapshots) {\n const existing = snapshotsByDocId.get(snapshot.documentId) || [];\n existing.push(snapshot);\n snapshotsByDocId.set(snapshot.documentId, existing);\n }\n\n const foundDocumentIds = [...snapshotsByDocId.keys()];\n const revisionResults = await Promise.all(\n foundDocumentIds.map((docId) =>\n this.operationStore.getRevisions(docId, branch, signal),\n ),\n );\n\n const revisionsByDocId = new Map<string, DocumentRevisions>();\n for (let i = 0; i < foundDocumentIds.length; i++) {\n revisionsByDocId.set(foundDocumentIds[i], revisionResults[i]);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const documents: TDocument[] = [];\n for (const documentId of documentIds) {\n const snapshots = snapshotsByDocId.get(documentId);\n if (!snapshots || snapshots.length === 0) {\n continue;\n }\n\n const headerSnapshot = snapshots.find((s) => s.scope === \"header\");\n if (!headerSnapshot) {\n continue;\n }\n\n const header = headerSnapshot.content as PHDocumentHeader;\n const revisions = revisionsByDocId.get(documentId);\n if (revisions) {\n header.revision = revisions.revision;\n header.lastModifiedAtUtcIso = revisions.latestTimestamp;\n }\n\n const state: Record<string, unknown> = {};\n for (const snapshot of snapshots) {\n if (snapshot.scope === \"header\") {\n continue;\n }\n // snapshot rows written before PHAuthState had a version carry auth: {}\n state[snapshot.scope] =\n snapshot.scope === \"auth\"\n ? createAuthState(snapshot.content as Partial<PHAuthState>)\n : snapshot.content;\n }\n\n const document: PHDocument = {\n header,\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n state: state as any,\n operations: {},\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n initialState: state as any,\n clipboard: [],\n };\n\n documents.push(document as TDocument);\n }\n\n return documents;\n }\n\n async getByIdOrSlug<TDocument extends PHDocument>(\n identifier: string,\n view?: ViewFilter,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<TDocument> {\n const documentId = await this.resolveIdOrSlug(\n identifier,\n view,\n consistencyToken,\n signal,\n );\n return this.get<TDocument>(documentId, view, undefined, signal);\n }\n\n async findByType(\n type: string,\n view?: ViewFilter,\n paging?: PagingOptions,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<PagedResults<PHDocument>> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const branch = view?.branch || \"main\";\n\n const startIndex = paging?.cursor ? parseInt(paging.cursor) : 0;\n const limit = paging?.limit || 100;\n\n const rows = await this._db\n .selectFrom(\"DocumentSnapshot\")\n .select(\"documentId\")\n .select((eb) => eb.fn.max(\"lastUpdatedAt\").as(\"lastUpdatedAt\"))\n .where(\"documentType\", \"=\", type)\n .where(\"branch\", \"=\", branch)\n .where(\"isDeleted\", \"=\", false)\n .groupBy(\"documentId\")\n .orderBy(\"lastUpdatedAt\", \"desc\")\n .orderBy(\"documentId\", \"asc\")\n .offset(startIndex)\n .limit(limit + 1)\n .execute();\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const hasMore = rows.length > limit;\n const docsToFetch = rows.slice(0, limit).map((row) => row.documentId);\n\n const documents: PHDocument[] = [];\n for (const documentId of docsToFetch) {\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n try {\n const document = await this.get<PHDocument>(\n documentId,\n view,\n undefined,\n signal,\n );\n documents.push(document);\n } catch {\n continue;\n }\n }\n\n const nextCursor = hasMore ? String(startIndex + limit) : undefined;\n\n return {\n results: documents,\n options: paging || { cursor: \"0\", limit: 100 },\n nextCursor,\n next: hasMore\n ? () =>\n this.findByType(\n type,\n view,\n { cursor: nextCursor!, limit },\n consistencyToken,\n signal,\n )\n : undefined,\n };\n }\n\n async resolveSlug(\n slug: string,\n // TODO: this should only be branch\n view?: ViewFilter,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<string | undefined> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const branch = view?.branch || \"main\";\n\n const mapping = await this._db\n .selectFrom(\"SlugMapping\")\n .select(\"documentId\")\n .where(\"slug\", \"=\", slug)\n .where(\"branch\", \"=\", branch)\n .executeTakeFirst();\n\n if (!mapping) {\n return undefined;\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n if (view?.scopes && view.scopes.length > 0) {\n const scopeCheck = await this._db\n .selectFrom(\"DocumentSnapshot\")\n .select(\"scope\")\n .where(\"documentId\", \"=\", mapping.documentId)\n .where(\"branch\", \"=\", branch)\n .where(\"scope\", \"in\", view.scopes)\n .where(\"isDeleted\", \"=\", false)\n .executeTakeFirst();\n\n if (!scopeCheck) {\n return undefined;\n }\n }\n\n return mapping.documentId;\n }\n\n // TODO: fix\n async resolveSlugs(\n slugs: string[],\n // TODO: this should only be branch\n view?: ViewFilter,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<string[]> {\n const ids = await Promise.all(\n slugs.map((slug) =>\n this.resolveSlug(slug, view, consistencyToken, signal),\n ),\n );\n return ids.filter((id) => id !== undefined) as string[];\n }\n\n async resolveIdOrSlug(\n identifier: string,\n // TODO: this should only be branch\n view?: ViewFilter,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<string> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const branch = view?.branch || \"main\";\n\n // The id branch relaxes with `get`, or resolution throws before it. The slug\n // branch never does: deleting a document removes its SlugMapping rows.\n let idCheckQuery = this._db\n .selectFrom(\"DocumentSnapshot\")\n .select(\"documentId\")\n .where(\"documentId\", \"=\", identifier)\n .where(\"branch\", \"=\", branch);\n\n if (this.deletedDocumentRead === DeletedDocumentRead.NotFound) {\n idCheckQuery = idCheckQuery.where(\"isDeleted\", \"=\", false);\n }\n\n const idCheckPromise = idCheckQuery.executeTakeFirst();\n\n const slugCheckPromise = this._db\n .selectFrom(\"SlugMapping\")\n .select(\"documentId\")\n .where(\"slug\", \"=\", identifier)\n .where(\"branch\", \"=\", branch)\n .executeTakeFirst();\n\n const [idMatch, slugMatch] = await Promise.all([\n idCheckPromise,\n slugCheckPromise,\n ]);\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const idMatchDocId = idMatch?.documentId;\n const slugMatchDocId = slugMatch?.documentId;\n\n if (idMatchDocId && slugMatchDocId && idMatchDocId !== slugMatchDocId) {\n throw new Error(\n `Ambiguous identifier \"${identifier}\": matches both document ID \"${idMatchDocId}\" and slug for document ID \"${slugMatchDocId}\". ` +\n `Please use get() for ID or resolveSlug() + get() for slug to be explicit.`,\n );\n }\n\n const resolvedDocumentId = idMatchDocId || slugMatchDocId;\n\n if (!resolvedDocumentId) {\n throw new Error(`Document not found: ${identifier}`);\n }\n\n return resolvedDocumentId;\n }\n\n /**\n * Ids that have at least one stream, asked of the operation store rather than\n * of a table, so the answer holds for any store implementation. A deleted\n * document keeps its stream, so this is the id-is-taken answer, and it does\n * not drift when a snapshot row is missing.\n *\n * Asked on main because that is the branch a create writes: the store this\n * question exists to agree with validates the create against main's head.\n */\n private async idsWithStreams(\n documentIds: string[],\n signal?: AbortSignal,\n ): Promise<Set<string>> {\n const unique = [...new Set(documentIds)];\n const revisions = await Promise.all(\n unique.map((documentId) =>\n this.operationStore.getRevisions(documentId, \"main\", signal),\n ),\n );\n\n const taken = new Set<string>();\n for (let i = 0; i < unique.length; i++) {\n if (Object.keys(revisions[i].revision).length > 0) {\n taken.add(unique[i]);\n }\n }\n return taken;\n }\n\n private async liveIds(documentIds: string[]): Promise<Set<string>> {\n const rows = await this._db\n .selectFrom(\"DocumentSnapshot\")\n .select([\"documentId\"])\n .where(\"documentId\", \"in\", documentIds)\n .where(\"isDeleted\", \"=\", false)\n .distinct()\n .execute();\n\n return new Set(rows.map((row) => row.documentId));\n }\n}\n","import type { ConsistencyCoordinate, ConsistencyKey } from \"./types.js\";\n\nexport interface IConsistencyTracker {\n /**\n * Updates the tracker with new operation indexes.\n * When multiple coordinates have the same key, keeps the highest operationIndex.\n * Resolves any pending waiters whose coordinates are now satisfied.\n */\n update(coordinates: ConsistencyCoordinate[]): void;\n\n /**\n * Returns the latest operation index for a given key, or undefined if not tracked.\n */\n getLatest(key: ConsistencyKey): number | undefined;\n\n /**\n * Returns a promise that resolves when all coordinates are satisfied.\n * Rejects if the timeout is reached or the signal is aborted.\n */\n waitFor(\n coordinates: ConsistencyCoordinate[],\n timeoutMs?: number,\n signal?: AbortSignal,\n ): Promise<void>;\n\n /**\n * Returns a serializable snapshot of the current state.\n */\n serialize(): Array<[ConsistencyKey, number]>;\n\n /**\n * Restores state from a serialized snapshot.\n */\n hydrate(entries: Array<[ConsistencyKey, number]>): void;\n}\n\ntype Waiter = {\n coordinates: ConsistencyCoordinate[];\n resolve: () => void;\n reject: (reason: Error) => void;\n signal?: AbortSignal;\n timeoutId?: NodeJS.Timeout;\n};\n\n/**\n * Creates a consistency key from documentId, scope, and branch.\n */\nexport function makeConsistencyKey(\n documentId: string,\n scope: string,\n branch: string,\n): ConsistencyKey {\n return `${documentId}:${scope}:${branch}`;\n}\n\n/**\n * Tracks operation indexes for documents and provides read-after-write consistency guarantees.\n * Maintains an in-memory map of the latest operation index for each (documentId, scope, branch) tuple.\n */\nexport class ConsistencyTracker implements IConsistencyTracker {\n private state = new Map<ConsistencyKey, number>();\n private waiters: Waiter[] = [];\n\n update(coordinates: ConsistencyCoordinate[]): void {\n const deduplicated = this.deduplicateCoordinates(coordinates);\n\n for (let i = 0; i < deduplicated.length; i++) {\n const coord = deduplicated[i]!;\n const key = makeConsistencyKey(\n coord.documentId,\n coord.scope,\n coord.branch,\n );\n const current = this.state.get(key);\n if (current === undefined || coord.operationIndex > current) {\n this.state.set(key, coord.operationIndex);\n }\n }\n\n this.checkWaiters();\n }\n\n getLatest(key: ConsistencyKey): number | undefined {\n return this.state.get(key);\n }\n\n waitFor(\n coordinates: ConsistencyCoordinate[],\n timeoutMs?: number,\n signal?: AbortSignal,\n ): Promise<void> {\n if (signal?.aborted) {\n return Promise.reject(new Error(\"Operation aborted\"));\n }\n\n if (this.areCoordinatesSatisfied(coordinates)) {\n return Promise.resolve();\n }\n\n return new Promise<void>((resolve, reject) => {\n const waiter: Waiter = {\n coordinates,\n resolve,\n reject,\n signal,\n };\n\n if (timeoutMs !== undefined) {\n waiter.timeoutId = setTimeout(() => {\n this.removeWaiter(waiter);\n reject(new Error(`Consistency wait timed out after ${timeoutMs}ms`));\n }, timeoutMs);\n }\n\n if (signal) {\n const abortHandler = () => {\n this.removeWaiter(waiter);\n reject(new Error(\"Operation aborted\"));\n };\n signal.addEventListener(\"abort\", abortHandler, { once: true });\n }\n\n this.waiters.push(waiter);\n });\n }\n\n serialize(): Array<[ConsistencyKey, number]> {\n return Array.from(this.state.entries());\n }\n\n hydrate(entries: Array<[ConsistencyKey, number]>): void {\n this.state.clear();\n for (const [key, index] of entries) {\n this.state.set(key, index);\n }\n }\n\n private deduplicateCoordinates(\n coordinates: ConsistencyCoordinate[],\n ): ConsistencyCoordinate[] {\n const map = new Map<ConsistencyKey, ConsistencyCoordinate>();\n\n for (let i = 0; i < coordinates.length; i++) {\n const coord = coordinates[i]!;\n const key = makeConsistencyKey(\n coord.documentId,\n coord.scope,\n coord.branch,\n );\n const existing = map.get(key);\n\n if (!existing || coord.operationIndex > existing.operationIndex) {\n map.set(key, coord);\n }\n }\n\n return Array.from(map.values());\n }\n\n private areCoordinatesSatisfied(\n coordinates: ConsistencyCoordinate[],\n ): boolean {\n for (let i = 0; i < coordinates.length; i++) {\n const coord = coordinates[i]!;\n const key = makeConsistencyKey(\n coord.documentId,\n coord.scope,\n coord.branch,\n );\n const latest = this.state.get(key);\n if (latest === undefined || latest < coord.operationIndex) {\n return false;\n }\n }\n return true;\n }\n\n private checkWaiters(): void {\n const satisfiedWaiters: Waiter[] = [];\n const unsatisfiedWaiters: Waiter[] = [];\n\n for (const waiter of this.waiters) {\n if (waiter.signal?.aborted) {\n continue;\n }\n\n if (this.areCoordinatesSatisfied(waiter.coordinates)) {\n satisfiedWaiters.push(waiter);\n } else {\n unsatisfiedWaiters.push(waiter);\n }\n }\n\n this.waiters = unsatisfiedWaiters;\n\n for (const waiter of satisfiedWaiters) {\n if (waiter.timeoutId !== undefined) {\n clearTimeout(waiter.timeoutId);\n }\n waiter.resolve();\n }\n }\n\n private removeWaiter(waiter: Waiter): void {\n const index = this.waiters.indexOf(waiter);\n if (index !== -1) {\n this.waiters.splice(index, 1);\n }\n if (waiter.timeoutId !== undefined) {\n clearTimeout(waiter.timeoutId);\n }\n }\n}\n","import type { PagedResults } from \"./types.js\";\n\n/**\n * Collects all results from a paged result set by following the next() function\n * until all pages have been fetched.\n */\nexport async function collectAllPages<T>(\n firstPage: PagedResults<T>,\n signal?: AbortSignal,\n): Promise<T[]> {\n const allResults: T[] = [...firstPage.results];\n let currentPage = firstPage;\n\n while (currentPage.next) {\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n currentPage = await currentPage.next();\n allResults.push(...currentPage.results);\n }\n\n return allResults;\n}\n","import type {\n Operation,\n OperationWithContext,\n} from \"@powerhousedao/shared/document-model\";\nimport type { Kysely } from \"kysely\";\nimport { v4 as uuidv4 } from \"uuid\";\nimport type { IOperationIndex } from \"../../cache/operation-index-types.js\";\nimport type { IWriteCache } from \"../../cache/write/interfaces.js\";\nimport {\n BaseReadModel,\n defaultReadModelIndexingConfig,\n type ReadModelIndexingConfig,\n} from \"../../read-models/base-read-model.js\";\nimport { DOCUMENT_INDEXER_READ_MODEL } from \"../../read-models/names.js\";\nimport type { DocumentViewDatabase } from \"../../read-models/types.js\";\nimport { collectAllPages } from \"../../shared/collect-all-pages.js\";\nimport type { IConsistencyTracker } from \"../../shared/consistency-tracker.js\";\nimport type {\n ConsistencyToken,\n PagedResults,\n PagingOptions,\n} from \"../../shared/types.js\";\nimport type {\n DocumentGraphEdge,\n DocumentRelationship,\n IDocumentGraph,\n IDocumentIndexer,\n} from \"../interfaces.js\";\nimport type {\n DocumentIndexerDatabase,\n InsertableDocumentRelationship,\n Database as StorageDatabase,\n} from \"./types.js\";\n\nfunction isRelationshipAction(actionType: string): boolean {\n return (\n actionType === \"ADD_RELATIONSHIP\" ||\n actionType === \"REMOVE_RELATIONSHIP\" ||\n actionType === \"UPDATE_RELATIONSHIP\"\n );\n}\n\nexport type IndexerDatabase = StorageDatabase &\n DocumentIndexerDatabase &\n DocumentViewDatabase;\n\nexport class KyselyDocumentIndexer\n extends BaseReadModel\n implements IDocumentIndexer\n{\n private _db: Kysely<IndexerDatabase>;\n\n constructor(\n db: Kysely<IndexerDatabase>,\n operationIndex: IOperationIndex,\n writeCache: IWriteCache,\n consistencyTracker: IConsistencyTracker,\n indexing: ReadModelIndexingConfig = defaultReadModelIndexingConfig,\n ) {\n super(\n db as unknown as Kysely<DocumentViewDatabase>,\n operationIndex,\n writeCache,\n consistencyTracker,\n {\n readModelId: DOCUMENT_INDEXER_READ_MODEL,\n rebuildStateOnInit: false,\n indexing,\n },\n );\n this._db = db;\n }\n\n /** Opens no transaction for a batch carrying no relationship operation. */\n protected override async commitOperations(\n items: OperationWithContext[],\n ): Promise<void> {\n const relationshipOps = items.filter((item) =>\n isRelationshipAction(item.operation.action.type),\n );\n\n if (relationshipOps.length === 0) {\n return;\n }\n\n await this._db.transaction().execute(async (trx) => {\n for (const item of relationshipOps) {\n const { operation } = item;\n const actionType = operation.action.type;\n\n if (actionType === \"ADD_RELATIONSHIP\") {\n await this.handleAddRelationship(trx, operation);\n } else if (actionType === \"REMOVE_RELATIONSHIP\") {\n await this.handleRemoveRelationship(trx, operation);\n } else if (actionType === \"UPDATE_RELATIONSHIP\") {\n await this.handleUpdateRelationship(trx, operation);\n }\n }\n });\n }\n\n async getOutgoing(\n documentId: string,\n types?: string[],\n paging?: PagingOptions,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<PagedResults<DocumentRelationship>> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const startIndex = paging?.cursor ? parseInt(paging.cursor) : 0;\n const limit = paging?.limit || 100;\n\n let query = this._db\n .selectFrom(\"DocumentRelationship\")\n .selectAll()\n .where(\"sourceId\", \"=\", documentId);\n\n if (types && types.length > 0) {\n query = query.where(\"relationshipType\", \"in\", types);\n }\n\n const rows = await query\n .orderBy(\"createdAt\", \"asc\")\n .orderBy(\"id\", \"asc\")\n .offset(startIndex)\n .limit(limit + 1)\n .execute();\n\n const hasMore = rows.length > limit;\n const results = hasMore ? rows.slice(0, limit) : rows;\n const nextCursor = hasMore ? String(startIndex + limit) : undefined;\n\n return {\n results: results.map((row) => ({\n sourceId: row.sourceId,\n targetId: row.targetId,\n relationshipType: row.relationshipType,\n metadata: row.metadata\n ? (row.metadata as Record<string, unknown>)\n : undefined,\n createdAt: row.createdAt,\n updatedAt: row.updatedAt,\n })),\n options: paging || { cursor: \"0\", limit: 100 },\n nextCursor,\n next: hasMore\n ? () =>\n this.getOutgoing(\n documentId,\n types,\n { cursor: nextCursor!, limit },\n consistencyToken,\n signal,\n )\n : undefined,\n };\n }\n\n async getIncoming(\n documentId: string,\n types?: string[],\n paging?: PagingOptions,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<PagedResults<DocumentRelationship>> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const startIndex = paging?.cursor ? parseInt(paging.cursor) : 0;\n const limit = paging?.limit || 100;\n\n let query = this._db\n .selectFrom(\"DocumentRelationship\")\n .selectAll()\n .where(\"targetId\", \"=\", documentId);\n\n if (types && types.length > 0) {\n query = query.where(\"relationshipType\", \"in\", types);\n }\n\n const rows = await query\n .orderBy(\"createdAt\", \"asc\")\n .orderBy(\"id\", \"asc\")\n .offset(startIndex)\n .limit(limit + 1)\n .execute();\n\n const hasMore = rows.length > limit;\n const results = hasMore ? rows.slice(0, limit) : rows;\n const nextCursor = hasMore ? String(startIndex + limit) : undefined;\n\n return {\n results: results.map((row) => ({\n sourceId: row.sourceId,\n targetId: row.targetId,\n relationshipType: row.relationshipType,\n metadata: row.metadata\n ? (row.metadata as Record<string, unknown>)\n : undefined,\n createdAt: row.createdAt,\n updatedAt: row.updatedAt,\n })),\n options: paging || { cursor: \"0\", limit: 100 },\n nextCursor,\n next: hasMore\n ? () =>\n this.getIncoming(\n documentId,\n types,\n { cursor: nextCursor!, limit },\n consistencyToken,\n signal,\n )\n : undefined,\n };\n }\n\n async getOrphanedChildren(\n parentIds: string[],\n types?: string[],\n signal?: AbortSignal,\n ): Promise<string[]> {\n if (parentIds.length === 0) {\n return [];\n }\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n let query = this._db\n .selectFrom(\"DocumentRelationship as r\")\n .select(\"r.targetId\")\n .distinct()\n .where(\"r.sourceId\", \"in\", parentIds)\n .where((eb) =>\n eb.not(\n eb.exists(\n eb\n .selectFrom(\"DocumentRelationship as other\")\n .select(\"other.id\")\n .whereRef(\"other.targetId\", \"=\", \"r.targetId\")\n .where(\"other.sourceId\", \"not in\", parentIds),\n ),\n ),\n );\n\n if (types && types.length > 0) {\n query = query.where(\"r.relationshipType\", \"in\", types);\n }\n\n const rows = await query.execute();\n return rows.map((row) => row.targetId);\n }\n\n async hasRelationship(\n sourceId: string,\n targetId: string,\n types?: string[],\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<boolean> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n let query = this._db\n .selectFrom(\"DocumentRelationship\")\n .select(\"id\")\n .where(\"sourceId\", \"=\", sourceId)\n .where(\"targetId\", \"=\", targetId);\n\n if (types && types.length > 0) {\n query = query.where(\"relationshipType\", \"in\", types);\n }\n\n const result = await query.executeTakeFirst();\n\n return result !== undefined;\n }\n\n async getUndirectedRelationships(\n a: string,\n b: string,\n types?: string[],\n paging?: PagingOptions,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<PagedResults<DocumentRelationship>> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const startIndex = paging?.cursor ? parseInt(paging.cursor) : 0;\n const limit = paging?.limit || 100;\n\n let query = this._db\n .selectFrom(\"DocumentRelationship\")\n .selectAll()\n .where((eb) =>\n eb.or([\n eb.and([eb(\"sourceId\", \"=\", a), eb(\"targetId\", \"=\", b)]),\n eb.and([eb(\"sourceId\", \"=\", b), eb(\"targetId\", \"=\", a)]),\n ]),\n );\n\n if (types && types.length > 0) {\n query = query.where(\"relationshipType\", \"in\", types);\n }\n\n const rows = await query\n .orderBy(\"createdAt\", \"asc\")\n .orderBy(\"id\", \"asc\")\n .offset(startIndex)\n .limit(limit + 1)\n .execute();\n\n const hasMore = rows.length > limit;\n const results = hasMore ? rows.slice(0, limit) : rows;\n const nextCursor = hasMore ? String(startIndex + limit) : undefined;\n\n return {\n results: results.map((row) => ({\n sourceId: row.sourceId,\n targetId: row.targetId,\n relationshipType: row.relationshipType,\n metadata: row.metadata\n ? (row.metadata as Record<string, unknown>)\n : undefined,\n createdAt: row.createdAt,\n updatedAt: row.updatedAt,\n })),\n options: paging || { cursor: \"0\", limit: 100 },\n nextCursor,\n next: hasMore\n ? () =>\n this.getUndirectedRelationships(\n a,\n b,\n types,\n { cursor: nextCursor!, limit },\n consistencyToken,\n signal,\n )\n : undefined,\n };\n }\n\n async getDirectedRelationships(\n sourceId: string,\n targetId: string,\n types?: string[],\n paging?: PagingOptions,\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<PagedResults<DocumentRelationship>> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const startIndex = paging?.cursor ? parseInt(paging.cursor) : 0;\n const limit = paging?.limit || 100;\n\n let query = this._db\n .selectFrom(\"DocumentRelationship\")\n .selectAll()\n .where(\"sourceId\", \"=\", sourceId)\n .where(\"targetId\", \"=\", targetId);\n\n if (types && types.length > 0) {\n query = query.where(\"relationshipType\", \"in\", types);\n }\n\n const rows = await query\n .orderBy(\"createdAt\", \"asc\")\n .orderBy(\"id\", \"asc\")\n .offset(startIndex)\n .limit(limit + 1)\n .execute();\n\n const hasMore = rows.length > limit;\n const results = hasMore ? rows.slice(0, limit) : rows;\n const nextCursor = hasMore ? String(startIndex + limit) : undefined;\n\n return {\n results: results.map((row) => ({\n sourceId: row.sourceId,\n targetId: row.targetId,\n relationshipType: row.relationshipType,\n metadata: row.metadata\n ? (row.metadata as Record<string, unknown>)\n : undefined,\n createdAt: row.createdAt,\n updatedAt: row.updatedAt,\n })),\n options: paging || { cursor: \"0\", limit: 100 },\n nextCursor,\n next: hasMore\n ? () =>\n this.getDirectedRelationships(\n sourceId,\n targetId,\n types,\n { cursor: nextCursor!, limit },\n consistencyToken,\n signal,\n )\n : undefined,\n };\n }\n\n async findPath(\n sourceId: string,\n targetId: string,\n types?: string[],\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<string[] | null> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n if (sourceId === targetId) {\n return [sourceId];\n }\n\n const visited = new Set<string>();\n const queue: Array<{ id: string; path: string[] }> = [\n { id: sourceId, path: [sourceId] },\n ];\n\n while (queue.length > 0) {\n const current = queue.shift()!;\n\n if (current.id === targetId) {\n return current.path;\n }\n\n if (visited.has(current.id)) {\n continue;\n }\n\n visited.add(current.id);\n\n const outgoingPage = await this.getOutgoing(\n current.id,\n types,\n undefined,\n consistencyToken,\n signal,\n );\n const outgoingRelationships = await collectAllPages(outgoingPage, signal);\n\n for (const rel of outgoingRelationships) {\n if (!visited.has(rel.targetId)) {\n queue.push({\n id: rel.targetId,\n path: [...current.path, rel.targetId],\n });\n }\n }\n }\n\n return null;\n }\n\n async findAncestors(\n documentId: string,\n types?: string[],\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<IDocumentGraph> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const nodes = new Set<string>([documentId]);\n const edges: DocumentGraphEdge[] = [];\n const queue: string[] = [documentId];\n const visited = new Set<string>();\n\n while (queue.length > 0) {\n const currentId = queue.shift()!;\n\n if (visited.has(currentId)) {\n continue;\n }\n\n visited.add(currentId);\n\n const incomingPage = await this.getIncoming(\n currentId,\n types,\n undefined,\n consistencyToken,\n signal,\n );\n const incomingRelationships = await collectAllPages(incomingPage, signal);\n\n for (const rel of incomingRelationships) {\n nodes.add(rel.sourceId);\n edges.push({\n from: rel.sourceId,\n to: rel.targetId,\n type: rel.relationshipType,\n });\n\n if (!visited.has(rel.sourceId)) {\n queue.push(rel.sourceId);\n }\n }\n }\n\n return {\n nodes: Array.from(nodes),\n edges,\n };\n }\n\n async getRelationshipTypes(\n consistencyToken?: ConsistencyToken,\n signal?: AbortSignal,\n ): Promise<string[]> {\n if (consistencyToken) {\n await this.waitForConsistency(consistencyToken, undefined, signal);\n }\n\n if (signal?.aborted) {\n throw new Error(\"Operation aborted\");\n }\n\n const rows = await this._db\n .selectFrom(\"DocumentRelationship\")\n .select(\"relationshipType\")\n .distinct()\n .execute();\n\n return rows.map((row) => row.relationshipType);\n }\n\n private async handleAddRelationship(\n trx: Kysely<IndexerDatabase>,\n operation: Operation,\n ): Promise<void> {\n const input = operation.action.input as {\n sourceId: string;\n targetId: string;\n relationshipType: string;\n metadata?: Record<string, unknown>;\n };\n\n const existingDoc = await trx\n .selectFrom(\"Document\")\n .select(\"id\")\n .where(\"id\", \"=\", input.sourceId)\n .executeTakeFirst();\n\n if (!existingDoc) {\n await trx\n .insertInto(\"Document\")\n .values({\n id: input.sourceId,\n })\n .execute();\n }\n\n const existingTargetDoc = await trx\n .selectFrom(\"Document\")\n .select(\"id\")\n .where(\"id\", \"=\", input.targetId)\n .executeTakeFirst();\n\n if (!existingTargetDoc) {\n await trx\n .insertInto(\"Document\")\n .values({\n id: input.targetId,\n })\n .execute();\n }\n\n const existingRel = await trx\n .selectFrom(\"DocumentRelationship\")\n .select(\"id\")\n .where(\"sourceId\", \"=\", input.sourceId)\n .where(\"targetId\", \"=\", input.targetId)\n .where(\"relationshipType\", \"=\", input.relationshipType)\n .executeTakeFirst();\n\n if (!existingRel) {\n const relationship: InsertableDocumentRelationship = {\n id: uuidv4(),\n sourceId: input.sourceId,\n targetId: input.targetId,\n relationshipType: input.relationshipType,\n metadata: input.metadata || null,\n };\n\n await trx\n .insertInto(\"DocumentRelationship\")\n .values(relationship)\n .execute();\n }\n }\n\n private async handleRemoveRelationship(\n trx: Kysely<IndexerDatabase>,\n operation: Operation,\n ): Promise<void> {\n const input = operation.action.input as {\n sourceId: string;\n targetId: string;\n relationshipType: string;\n };\n\n await trx\n .deleteFrom(\"DocumentRelationship\")\n .where(\"sourceId\", \"=\", input.sourceId)\n .where(\"targetId\", \"=\", input.targetId)\n .where(\"relationshipType\", \"=\", input.relationshipType)\n .execute();\n }\n\n private async handleUpdateRelationship(\n trx: Kysely<IndexerDatabase>,\n operation: Operation,\n ): Promise<void> {\n const input = operation.action.input as {\n sourceId: string;\n targetId: string;\n relationshipType: string;\n metadata: Record<string, unknown> | null;\n };\n\n await trx\n .updateTable(\"DocumentRelationship\")\n .set({\n metadata: input.metadata,\n updatedAt: new Date(),\n })\n .where(\"sourceId\", \"=\", input.sourceId)\n .where(\"targetId\", \"=\", input.targetId)\n .where(\"relationshipType\", \"=\", input.relationshipType)\n .execute();\n }\n}\n"],"mappings":";;;;;AA8BA,MAAM,UAAiC;CACrC,eAAe,CAAC;CAChB,wBAAwB,QAAQ,QAAQ,CAAC,CAAC;AAC5C;AASA,SAAgB,kBAAkB,OAA2C;CAC3E,IAAI,OAAO,UAAU,YAAY,UAAU,MAAM,OAAO;CACxD,MAAM,YAAY;CAClB,OACE,OAAO,UAAU,UAAU,cAC3B,OAAO,UAAU,eAAe,YAChC,OAAO,UAAU,mBAAmB;AAExC;;AAGA,IAAa,mBAAb,MAAkD;CAY7B;CACA;CACA;CACA;CACA;CAfnB,QAA0C,CAAC;CAC3C,UAA6C,CAAC;CAC9C,0BAA2B,IAAI,IAAqC;CACpE;CACA;CACA,UAAkB;CAClB,UAAkB;CAClB;CACA,gBAAuE,CAAC;CAExE,YACE,WACA,gBACA,QACA,QACA,QAAgD,SAChD;EALiB,KAAA,YAAA;EACA,KAAA,iBAAA;EACA,KAAA,SAAA;EACA,KAAA,SAAA;EACA,KAAA,QAAA;CAChB;C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,2 @@
1
- export { };
1
+ export {}
2
+ export {};
package/dist/entry.js CHANGED
@@ -1,6 +1,5 @@
1
- import { o as instrumentPgPool } from "./drive-container-types-aO-PASn7.js";
2
- import { n as errorToInfo, t as createForwardingLogger } from "./forwarding-logger-BBkMSxuJ.js";
3
- import { n as defaultLoadFactory, t as buildWorkerExecutor } from "./build-worker-executor-BN30c2xw.js";
1
+ import { n as errorToInfo, t as createForwardingLogger, u as instrumentPgPool } from "./forwarding-logger-BkwlvD9W.js";
2
+ import { n as defaultLoadFactory, t as buildWorkerExecutor } from "./build-worker-executor-BonPsKdx.js";
4
3
  import { ConsoleLogger } from "document-model";
5
4
  import { isMainThread, parentPort } from "node:worker_threads";
6
5
  //#region src/executor/worker/run-worker.ts
@@ -38,7 +37,7 @@ async function defaultCreateDatabase(config, workerId) {
38
37
  /**
39
38
  * Drives the worker's message loop. Owns lifecycle of the database handle
40
39
  * and executor stack. The default factories build a real Postgres pool and
41
- * use dynamic `import()` for model/verifier specs; tests inject overrides
40
+ * use dynamic `import()` for model/signer specs; tests inject overrides
42
41
  * for an in-process PGlite path.
43
42
  */
44
43
  function runWorker(parentPort, overrides = {}) {
@@ -146,11 +145,16 @@ function runWorker(parentPort, overrides = {}) {
146
145
  return;
147
146
  }
148
147
  try {
148
+ const result = await executorStack.executor.executeJob(job);
149
+ const writeReady = executorStack.takeLastWriteReady();
150
+ const signatureRefusals = executorStack.takeSignatureRefusals();
149
151
  post({
150
152
  type: "result",
151
153
  correlationId,
152
- result: await executorStack.executor.executeJob(job),
153
- writeReady: executorStack.takeLastWriteReady() ?? void 0
154
+ result,
155
+ writeReady: writeReady ?? void 0,
156
+ signatureRefusals: signatureRefusals.length > 0 ? signatureRefusals : void 0,
157
+ error: !result.success && result.error ? errorToInfo(result.error) : void 0
154
158
  });
155
159
  } catch (error) {
156
160
  post({
package/dist/entry.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"entry.js","names":[],"sources":["../src/executor/worker/run-worker.ts","../src/executor/worker/entry.ts"],"sourcesContent":["import type { DocumentModelModule } from \"@powerhousedao/shared/document-model\";\nimport { ConsoleLogger } from \"document-model\";\nimport type { Kysely } from \"kysely\";\nimport type { MessagePort } from \"node:worker_threads\";\nimport type { Database } from \"../../core/types.js\";\nimport type { Job } from \"../../queue/types.js\";\nimport {\n instrumentPgPool,\n type PoolInstrumentation,\n} from \"../../storage/pool-instrumentation.js\";\nimport {\n buildWorkerExecutor,\n defaultLoadFactory,\n type BuildWorkerExecutorOptions,\n type WorkerExecutorStack,\n} from \"./build-worker-executor.js\";\nimport { createForwardingLogger } from \"./forwarding-logger.js\";\nimport type {\n DbConfig,\n FactorySpec,\n InitMessage,\n LoadModelMessage,\n ParentMessage,\n WorkerMessage,\n} from \"./protocol.js\";\nimport { errorToInfo } from \"./sanitize.js\";\n\nconst POOL_SAMPLE_INTERVAL_MS = 1_000;\n\n/**\n * Closeable handle around the parent's Postgres pool the worker owns.\n * Decoupled so tests can substitute a PGlite-backed Kysely.\n *\n * When the worker owns a real pg.Pool, the handle exposes a\n * {@link PoolInstrumentation} so the run-loop can subscribe to acquire-wait\n * samples and forward them to the host. Tests that swap in PGlite leave\n * this undefined; pool metrics simply do not emit in that path.\n */\nexport type WorkerDatabaseHandle = {\n kysely: Kysely<Database>;\n poolInstrumentation?: PoolInstrumentation;\n shutdown(): Promise<void>;\n};\n\nexport type RunWorkerOverrides = {\n /**\n * Builds the worker's Kysely instance from the init's DbConfig. Defaults\n * to a real Postgres pool. Tests typically swap this for PGlite.\n */\n createDatabase?: (\n config: DbConfig,\n workerId: string,\n ) => Promise<WorkerDatabaseHandle>;\n /**\n * Overrides the dynamic-import factory loader used for the signature\n * verifier and document model manifest. Tests usually inject this so\n * they don't need real packages.\n */\n loadFactory?: BuildWorkerExecutorOptions[\"loadFactory\"];\n /**\n * Called after the database is created but before the executor stack\n * is built. Lets tests run reactor migrations against PGlite, since\n * the production parent runs them once against shared Postgres.\n */\n beforeBuildExecutor?: (db: Kysely<Database>) => Promise<void>;\n};\n\nasync function defaultCreateDatabase(\n config: DbConfig,\n workerId: string,\n): Promise<WorkerDatabaseHandle> {\n const { Kysely, PostgresDialect } = await import(\"kysely\");\n const pgModule = await import(\"pg\");\n const Pool = pgModule.default.Pool;\n const pool = new Pool({\n host: config.host,\n port: config.port,\n database: config.database,\n user: config.user,\n password: config.password,\n ssl: config.ssl ? { rejectUnauthorized: false } : undefined,\n application_name: config.applicationName ?? workerId,\n max: config.poolSize,\n connectionTimeoutMillis: config.connectionTimeoutMillis,\n idleTimeoutMillis: config.idleTimeoutMillis,\n });\n const poolInstrumentation = instrumentPgPool(pool, workerId);\n const kysely = new Kysely<Database>({\n dialect: new PostgresDialect({ pool }),\n });\n return {\n kysely,\n poolInstrumentation,\n async shutdown(): Promise<void> {\n try {\n await kysely.destroy();\n } catch {\n // best-effort\n }\n try {\n await pool.end();\n } catch {\n // best-effort\n }\n },\n };\n}\n\n/**\n * Drives the worker's message loop. Owns lifecycle of the database handle\n * and executor stack. The default factories build a real Postgres pool and\n * use dynamic `import()` for model/verifier specs; tests inject overrides\n * for an in-process PGlite path.\n */\nexport function runWorker(\n parentPort: MessagePort,\n overrides: RunWorkerOverrides = {},\n): void {\n let workerId = \"\";\n let initCompleted = false;\n let initConfig: InitMessage | null = null;\n let executorStack: WorkerExecutorStack | null = null;\n let database: WorkerDatabaseHandle | null = null;\n let poolSampleTimer: NodeJS.Timeout | null = null;\n let pendingPoolSamples: number[] = [];\n let detachPoolListener: (() => void) | null = null;\n const activeLoadFactory: NonNullable<\n BuildWorkerExecutorOptions[\"loadFactory\"]\n > = overrides.loadFactory ?? defaultLoadFactory;\n\n function post(msg: WorkerMessage): void {\n parentPort.postMessage(msg);\n }\n\n const logger = createForwardingLogger(post);\n\n function startPoolReporter(instrumentation: PoolInstrumentation): void {\n detachPoolListener = instrumentation.onAcquire((durationMs) => {\n pendingPoolSamples.push(durationMs);\n });\n poolSampleTimer = setInterval(() => {\n if (pendingPoolSamples.length === 0) {\n return;\n }\n const durations = pendingPoolSamples;\n pendingPoolSamples = [];\n const stats = instrumentation.getStats();\n post({\n type: \"pool-acquire-samples\",\n workerId,\n poolName: instrumentation.name,\n timestamp: Date.now(),\n durations,\n size: stats.size,\n idle: stats.idle,\n waiting: stats.waiting,\n });\n }, POOL_SAMPLE_INTERVAL_MS);\n poolSampleTimer.unref();\n }\n\n function stopPoolReporter(): void {\n if (detachPoolListener) {\n detachPoolListener();\n detachPoolListener = null;\n }\n if (poolSampleTimer) {\n clearInterval(poolSampleTimer);\n poolSampleTimer = null;\n }\n pendingPoolSamples = [];\n }\n\n process.on(\"uncaughtException\", (err: unknown) => {\n try {\n post({\n type: \"log\",\n level: \"error\",\n message: \"worker uncaughtException\",\n args: [errorToInfo(err)],\n timestamp: Date.now(),\n });\n } catch {\n // nothing left to do\n }\n throw err;\n });\n\n process.on(\"unhandledRejection\", (reason: unknown) => {\n try {\n post({\n type: \"log\",\n level: \"error\",\n message: \"worker unhandledRejection\",\n args: [errorToInfo(reason)],\n timestamp: Date.now(),\n });\n } catch {\n // nothing left to do\n }\n });\n\n async function handleInit(msg: InitMessage): Promise<void> {\n workerId = msg.workerId;\n initConfig = msg;\n const createDb = overrides.createDatabase ?? defaultCreateDatabase;\n database = await createDb(msg.db, msg.workerId);\n if (overrides.beforeBuildExecutor) {\n await overrides.beforeBuildExecutor(database.kysely);\n }\n executorStack = await buildWorkerExecutor({\n init: msg,\n database: database.kysely,\n logger: new ConsoleLogger([`reactor-worker:${msg.workerId}`]),\n executorConfig: msg.executorConfig,\n loadFactory: activeLoadFactory,\n });\n if (database.poolInstrumentation) {\n startPoolReporter(database.poolInstrumentation);\n }\n initCompleted = true;\n logger.info(\"worker initialized: @workerId\", msg.workerId);\n post({\n type: \"ready\",\n correlationId: msg.correlationId,\n workerId: msg.workerId,\n });\n }\n\n async function handleExecute(correlationId: string, job: Job): Promise<void> {\n if (!executorStack) {\n post({\n type: \"result\",\n correlationId,\n result: { job, success: false },\n error: errorToInfo(new Error(\"execute received before init\")),\n });\n return;\n }\n try {\n const result = await executorStack.executor.executeJob(job);\n const writeReady = executorStack.takeLastWriteReady();\n post({\n type: \"result\",\n correlationId,\n result,\n writeReady: writeReady ?? undefined,\n });\n } catch (error) {\n post({\n type: \"result\",\n correlationId,\n result: { job, success: false },\n error: errorToInfo(error),\n });\n }\n }\n\n async function handleLoadModel(msg: LoadModelMessage): Promise<void> {\n const stack = executorStack;\n if (!stack) {\n post({\n type: \"model-load-failed\",\n correlationId: msg.correlationId,\n documentType: msg.model.documentType,\n version: msg.model.version,\n error: errorToInfo(new Error(\"load-model received before init\")),\n });\n return;\n }\n let module: DocumentModelModule;\n try {\n module = (await activeLoadFactory(msg.model.spec)) as DocumentModelModule;\n } catch (error) {\n post({\n type: \"model-load-failed\",\n correlationId: msg.correlationId,\n documentType: msg.model.documentType,\n version: msg.model.version,\n error: errorToInfo(error),\n });\n return;\n }\n const [result] = stack.registry.registerModules(module);\n if (result.status === \"error\") {\n post({\n type: \"model-load-failed\",\n correlationId: msg.correlationId,\n documentType: msg.model.documentType,\n version: msg.model.version,\n error: errorToInfo(result.error),\n });\n return;\n }\n post({\n type: \"model-loaded\",\n correlationId: msg.correlationId,\n documentType: msg.model.documentType,\n version: msg.model.version,\n });\n }\n\n async function shutdownDatabase(): Promise<void> {\n stopPoolReporter();\n if (database) {\n await database.shutdown();\n }\n }\n\n function handleParentMessage(msg: ParentMessage): void {\n switch (msg.type) {\n case \"init\": {\n handleInit(msg).catch((err: unknown) => {\n post({\n type: \"log\",\n level: \"error\",\n message: \"worker init failed\",\n args: [errorToInfo(err)],\n timestamp: Date.now(),\n });\n });\n break;\n }\n\n case \"execute\": {\n if (!initCompleted) {\n logger.warn(\"received execute before init\");\n post({\n type: \"result\",\n correlationId: msg.correlationId,\n result: { job: msg.job, success: false },\n error: errorToInfo(new Error(\"execute received before init\")),\n });\n break;\n }\n handleExecute(msg.correlationId, msg.job).catch((err: unknown) => {\n post({\n type: \"result\",\n correlationId: msg.correlationId,\n result: { job: msg.job, success: false },\n error: errorToInfo(err),\n });\n });\n break;\n }\n\n case \"shutdown\": {\n logger.info(\"worker shutting down: @workerId\", workerId);\n void shutdownDatabase().finally(() => {\n post({\n type: \"log\",\n level: \"info\",\n message: \"worker shutdown\",\n args: [],\n timestamp: Date.now(),\n });\n process.exit(0);\n });\n break;\n }\n\n case \"abort\": {\n logger.warn(\n \"abort received (no-op stub): @correlationId\",\n msg.correlationId,\n );\n break;\n }\n\n case \"load-model\": {\n handleLoadModel(msg).catch((err: unknown) => {\n post({\n type: \"model-load-failed\",\n correlationId: msg.correlationId,\n documentType: msg.model.documentType,\n version: msg.model.version,\n error: errorToInfo(err),\n });\n });\n break;\n }\n\n default: {\n const raw = msg as Record<string, unknown>;\n if (raw[\"type\"] === \"__test_throw\") {\n const rawReason = raw[\"reason\"];\n const reason =\n typeof rawReason === \"string\"\n ? rawReason\n : \"synthetic uncaughtException\";\n setTimeout(() => {\n throw new Error(reason);\n }, 0);\n return;\n }\n const _exhaustive: never = msg;\n void _exhaustive;\n break;\n }\n }\n }\n\n parentPort.on(\"message\", handleParentMessage);\n\n // Test-only exports surfaced on a side-channel for unit tests that drive\n // the message loop directly without a real worker_threads parent.\n const harness = {\n handleParentMessage,\n get initCompleted(): boolean {\n return initCompleted;\n },\n get initConfig(): InitMessage | null {\n return initConfig;\n },\n get workerId(): string {\n return workerId;\n },\n };\n (\n parentPort as unknown as { __reactorWorkerHarness?: unknown }\n ).__reactorWorkerHarness = harness;\n}\n\nexport type FactorySpecForTesting = FactorySpec;\n","import { isMainThread, parentPort } from \"node:worker_threads\";\nimport { runWorker } from \"./run-worker.js\";\n\nif (isMainThread || parentPort === null) {\n throw new Error(\"entry.ts must be run as a worker thread\");\n}\n\nrunWorker(parentPort);\n"],"mappings":";;;;;;AA2BA,MAAM,0BAA0B;AAwChC,eAAe,sBACb,QACA,UAC+B;CAC/B,MAAM,EAAE,QAAQ,oBAAoB,MAAM,OAAO;CAEjD,MA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C3B,UAAO;;EAET,IAAI,aAAiC;AACnC,UAAO;;EAET,IAAI,WAAmB;AACrB,UAAO;;EAEV;;;;AC9ZH,IAAI,gBAAgB,eAAe,KACjC,OAAM,IAAI,MAAM,0CAA0C;AAG5D,UAAU,WAAW"}
1
+ {"version":3,"file":"entry.js","names":[],"sources":["../src/executor/worker/run-worker.ts","../src/executor/worker/entry.ts"],"sourcesContent":["import type { DocumentModelModule } from \"@powerhousedao/shared/document-model\";\nimport { ConsoleLogger } from \"document-model\";\nimport type { Kysely } from \"kysely\";\nimport type { MessagePort } from \"node:worker_threads\";\nimport type { Database } from \"../../core/types.js\";\nimport type { Job } from \"../../queue/types.js\";\nimport {\n instrumentPgPool,\n type PoolInstrumentation,\n} from \"../../storage/pool-instrumentation.js\";\nimport {\n buildWorkerExecutor,\n defaultLoadFactory,\n type BuildWorkerExecutorOptions,\n type WorkerExecutorStack,\n} from \"./build-worker-executor.js\";\nimport { createForwardingLogger } from \"./forwarding-logger.js\";\nimport type {\n DbConfig,\n FactorySpec,\n InitMessage,\n LoadModelMessage,\n ParentMessage,\n WorkerMessage,\n} from \"./protocol.js\";\nimport { errorToInfo } from \"./sanitize.js\";\n\nconst POOL_SAMPLE_INTERVAL_MS = 1_000;\n\n/**\n * Closeable handle around the parent's Postgres pool the worker owns.\n * Decoupled so tests can substitute a PGlite-backed Kysely.\n *\n * When the worker owns a real pg.Pool, the handle exposes a\n * {@link PoolInstrumentation} so the run-loop can subscribe to acquire-wait\n * samples and forward them to the host. Tests that swap in PGlite leave\n * this undefined; pool metrics simply do not emit in that path.\n */\nexport type WorkerDatabaseHandle = {\n kysely: Kysely<Database>;\n poolInstrumentation?: PoolInstrumentation;\n shutdown(): Promise<void>;\n};\n\nexport type RunWorkerOverrides = {\n /**\n * Builds the worker's Kysely instance from the init's DbConfig. Defaults\n * to a real Postgres pool. Tests typically swap this for PGlite.\n */\n createDatabase?: (\n config: DbConfig,\n workerId: string,\n ) => Promise<WorkerDatabaseHandle>;\n /**\n * Overrides the dynamic-import factory loader used for the signer and\n * document model manifest. Tests usually inject this so\n * they don't need real packages.\n */\n loadFactory?: BuildWorkerExecutorOptions[\"loadFactory\"];\n /**\n * Called after the database is created but before the executor stack\n * is built. Lets tests run reactor migrations against PGlite, since\n * the production parent runs them once against shared Postgres.\n */\n beforeBuildExecutor?: (db: Kysely<Database>) => Promise<void>;\n};\n\nasync function defaultCreateDatabase(\n config: DbConfig,\n workerId: string,\n): Promise<WorkerDatabaseHandle> {\n const { Kysely, PostgresDialect } = await import(\"kysely\");\n const pgModule = await import(\"pg\");\n const Pool = pgModule.default.Pool;\n const pool = new Pool({\n host: config.host,\n port: config.port,\n database: config.database,\n user: config.user,\n password: config.password,\n ssl: config.ssl ? { rejectUnauthorized: false } : undefined,\n application_name: config.applicationName ?? workerId,\n max: config.poolSize,\n connectionTimeoutMillis: config.connectionTimeoutMillis,\n idleTimeoutMillis: config.idleTimeoutMillis,\n });\n const poolInstrumentation = instrumentPgPool(pool, workerId);\n const kysely = new Kysely<Database>({\n dialect: new PostgresDialect({ pool }),\n });\n return {\n kysely,\n poolInstrumentation,\n async shutdown(): Promise<void> {\n try {\n await kysely.destroy();\n } catch {\n // best-effort\n }\n try {\n await pool.end();\n } catch {\n // best-effort\n }\n },\n };\n}\n\n/**\n * Drives the worker's message loop. Owns lifecycle of the database handle\n * and executor stack. The default factories build a real Postgres pool and\n * use dynamic `import()` for model/signer specs; tests inject overrides\n * for an in-process PGlite path.\n */\nexport function runWorker(\n parentPort: MessagePort,\n overrides: RunWorkerOverrides = {},\n): void {\n let workerId = \"\";\n let initCompleted = false;\n let initConfig: InitMessage | null = null;\n let executorStack: WorkerExecutorStack | null = null;\n let database: WorkerDatabaseHandle | null = null;\n let poolSampleTimer: NodeJS.Timeout | null = null;\n let pendingPoolSamples: number[] = [];\n let detachPoolListener: (() => void) | null = null;\n const activeLoadFactory: NonNullable<\n BuildWorkerExecutorOptions[\"loadFactory\"]\n > = overrides.loadFactory ?? defaultLoadFactory;\n\n function post(msg: WorkerMessage): void {\n parentPort.postMessage(msg);\n }\n\n const logger = createForwardingLogger(post);\n\n function startPoolReporter(instrumentation: PoolInstrumentation): void {\n detachPoolListener = instrumentation.onAcquire((durationMs) => {\n pendingPoolSamples.push(durationMs);\n });\n poolSampleTimer = setInterval(() => {\n if (pendingPoolSamples.length === 0) {\n return;\n }\n const durations = pendingPoolSamples;\n pendingPoolSamples = [];\n const stats = instrumentation.getStats();\n post({\n type: \"pool-acquire-samples\",\n workerId,\n poolName: instrumentation.name,\n timestamp: Date.now(),\n durations,\n size: stats.size,\n idle: stats.idle,\n waiting: stats.waiting,\n });\n }, POOL_SAMPLE_INTERVAL_MS);\n poolSampleTimer.unref();\n }\n\n function stopPoolReporter(): void {\n if (detachPoolListener) {\n detachPoolListener();\n detachPoolListener = null;\n }\n if (poolSampleTimer) {\n clearInterval(poolSampleTimer);\n poolSampleTimer = null;\n }\n pendingPoolSamples = [];\n }\n\n process.on(\"uncaughtException\", (err: unknown) => {\n try {\n post({\n type: \"log\",\n level: \"error\",\n message: \"worker uncaughtException\",\n args: [errorToInfo(err)],\n timestamp: Date.now(),\n });\n } catch {\n // nothing left to do\n }\n throw err;\n });\n\n process.on(\"unhandledRejection\", (reason: unknown) => {\n try {\n post({\n type: \"log\",\n level: \"error\",\n message: \"worker unhandledRejection\",\n args: [errorToInfo(reason)],\n timestamp: Date.now(),\n });\n } catch {\n // nothing left to do\n }\n });\n\n async function handleInit(msg: InitMessage): Promise<void> {\n workerId = msg.workerId;\n initConfig = msg;\n const createDb = overrides.createDatabase ?? defaultCreateDatabase;\n database = await createDb(msg.db, msg.workerId);\n if (overrides.beforeBuildExecutor) {\n await overrides.beforeBuildExecutor(database.kysely);\n }\n executorStack = await buildWorkerExecutor({\n init: msg,\n database: database.kysely,\n logger: new ConsoleLogger([`reactor-worker:${msg.workerId}`]),\n executorConfig: msg.executorConfig,\n loadFactory: activeLoadFactory,\n });\n if (database.poolInstrumentation) {\n startPoolReporter(database.poolInstrumentation);\n }\n initCompleted = true;\n logger.info(\"worker initialized: @workerId\", msg.workerId);\n post({\n type: \"ready\",\n correlationId: msg.correlationId,\n workerId: msg.workerId,\n });\n }\n\n async function handleExecute(correlationId: string, job: Job): Promise<void> {\n if (!executorStack) {\n post({\n type: \"result\",\n correlationId,\n result: { job, success: false },\n error: errorToInfo(new Error(\"execute received before init\")),\n });\n return;\n }\n try {\n const result = await executorStack.executor.executeJob(job);\n const writeReady = executorStack.takeLastWriteReady();\n const signatureRefusals = executorStack.takeSignatureRefusals();\n post({\n type: \"result\",\n correlationId,\n result,\n writeReady: writeReady ?? undefined,\n signatureRefusals:\n signatureRefusals.length > 0 ? signatureRefusals : undefined,\n // Structured clone drops a custom error name; the ErrorInfo keeps it.\n error:\n !result.success && result.error\n ? errorToInfo(result.error)\n : undefined,\n });\n } catch (error) {\n post({\n type: \"result\",\n correlationId,\n result: { job, success: false },\n error: errorToInfo(error),\n });\n }\n }\n\n async function handleLoadModel(msg: LoadModelMessage): Promise<void> {\n const stack = executorStack;\n if (!stack) {\n post({\n type: \"model-load-failed\",\n correlationId: msg.correlationId,\n documentType: msg.model.documentType,\n version: msg.model.version,\n error: errorToInfo(new Error(\"load-model received before init\")),\n });\n return;\n }\n let module: DocumentModelModule;\n try {\n module = (await activeLoadFactory(msg.model.spec)) as DocumentModelModule;\n } catch (error) {\n post({\n type: \"model-load-failed\",\n correlationId: msg.correlationId,\n documentType: msg.model.documentType,\n version: msg.model.version,\n error: errorToInfo(error),\n });\n return;\n }\n const [result] = stack.registry.registerModules(module);\n if (result.status === \"error\") {\n post({\n type: \"model-load-failed\",\n correlationId: msg.correlationId,\n documentType: msg.model.documentType,\n version: msg.model.version,\n error: errorToInfo(result.error),\n });\n return;\n }\n post({\n type: \"model-loaded\",\n correlationId: msg.correlationId,\n documentType: msg.model.documentType,\n version: msg.model.version,\n });\n }\n\n async function shutdownDatabase(): Promise<void> {\n stopPoolReporter();\n if (database) {\n await database.shutdown();\n }\n }\n\n function handleParentMessage(msg: ParentMessage): void {\n switch (msg.type) {\n case \"init\": {\n handleInit(msg).catch((err: unknown) => {\n post({\n type: \"log\",\n level: \"error\",\n message: \"worker init failed\",\n args: [errorToInfo(err)],\n timestamp: Date.now(),\n });\n });\n break;\n }\n\n case \"execute\": {\n if (!initCompleted) {\n logger.warn(\"received execute before init\");\n post({\n type: \"result\",\n correlationId: msg.correlationId,\n result: { job: msg.job, success: false },\n error: errorToInfo(new Error(\"execute received before init\")),\n });\n break;\n }\n handleExecute(msg.correlationId, msg.job).catch((err: unknown) => {\n post({\n type: \"result\",\n correlationId: msg.correlationId,\n result: { job: msg.job, success: false },\n error: errorToInfo(err),\n });\n });\n break;\n }\n\n case \"shutdown\": {\n logger.info(\"worker shutting down: @workerId\", workerId);\n void shutdownDatabase().finally(() => {\n post({\n type: \"log\",\n level: \"info\",\n message: \"worker shutdown\",\n args: [],\n timestamp: Date.now(),\n });\n process.exit(0);\n });\n break;\n }\n\n case \"abort\": {\n logger.warn(\n \"abort received (no-op stub): @correlationId\",\n msg.correlationId,\n );\n break;\n }\n\n case \"load-model\": {\n handleLoadModel(msg).catch((err: unknown) => {\n post({\n type: \"model-load-failed\",\n correlationId: msg.correlationId,\n documentType: msg.model.documentType,\n version: msg.model.version,\n error: errorToInfo(err),\n });\n });\n break;\n }\n\n default: {\n const raw = msg as Record<string, unknown>;\n if (raw[\"type\"] === \"__test_throw\") {\n const rawReason = raw[\"reason\"];\n const reason =\n typeof rawReason === \"string\"\n ? rawReason\n : \"synthetic uncaughtException\";\n setTimeout(() => {\n throw new Error(reason);\n }, 0);\n return;\n }\n const _exhaustive: never = msg;\n void _exhaustive;\n break;\n }\n }\n }\n\n parentPort.on(\"message\", handleParentMessage);\n\n // Test-only exports surfaced on a side-channel for unit tests that drive\n // the message loop directly without a real worker_threads parent.\n const harness = {\n handleParentMessage,\n get initCompleted(): boolean {\n return initCompleted;\n },\n get initConfig(): InitMessage | null {\n return initConfig;\n },\n get workerId(): string {\n return workerId;\n },\n };\n (\n parentPort as unknown as { __reactorWorkerHarness?: unknown }\n ).__reactorWorkerHarness = harness;\n}\n\nexport type FactorySpecForTesting = FactorySpec;\n","import { isMainThread, parentPort } from \"node:worker_threads\";\nimport { runWorker } from \"./run-worker.js\";\n\nif (isMainThread || parentPort === null) {\n throw new Error(\"entry.ts must be run as a worker thread\");\n}\n\nrunWorker(parentPort);\n"],"mappings":";;;;;AA2BA,MAAM,0BAA0B;AAwChC,eAAe,sBACb,QACA,UAC+B;CAC/B,MAAM,EAAE,QAAQ,oBAAoB,MAAM,OAAO;CAEjD,MAAM,QAAO,MADU,OAAO,MAAA,CACR,QAAQ;CAC9B,MAAM,OAAO,IAAI,KAAK;EACpB,MAAM,OAAO;EACb,MAAM,OAAO;EACb,UAAU,OAAO;EACjB,MAAM,OAAO;EACb,UAAU,OAAO;EACjB,KAAK,OAAO,MAAM,EAAE,oBAAoB,MAAM,IAAI,KAAA;EAClD,kBAAkB,OAAO,mBAAmB;EAC5C,KAAK,OAAO;EACZ,yBAAyB,OAAO;EAChC,mBAAmB,OAAO;CAC5B,CAAC;CACD,MAAM,sBAAsB,iBAAiB,MAAM,QAAQ;CAC3D,MAAM,SAAS,IAAI,OAAiB,EAClC,SAAS,IAAI,gBAAgB,EAAE,KAAK,CAAC,EACvC,CAAC;CACD,OAAO;EACL;EACA;EACA,MAAM,WAA0B;GAC9B,IAAI;IACF,MAAM,OAAO,QAAQ;GACvB,QAAQ,CAER;GACA,IAAI;IACF,MAAM,KAAK,IAAI;GACjB,QAAQ,CAER;EACF;CACF;AACF;;;;;;;AAQA,SAAgB,UACd,YACA,YAAgC,CAAC,GAC3B;CACN,IAAI,WAAW;CACf,IAAI,gBAAgB;CACpB,IAAI,aAAiC;CACrC,IAAI,gBAA4C;CAChD,IAAI,WAAwC;CAC5C,IAAI,kBAAyC;CAC7C,IAAI,qBAA+B,CAAC;CACpC,IAAI,qBAA0C;CAC9C,MAAM,oBAEF,UAAU,eAAe;CAE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,CAAC;IACD;GAGF,KAAK;IACH,OAAO,KAAK,mCAAmC,QAAQ;IACvD,iBAAsB,CAAC,CAAC,cAAc;KACpC,KAAK;MACH,MAAM;MACN,OAAO;MACP,SAAS;MACT,MAAM,CAAC;MACP,WAAW,KAAK,IAAI;KACtB,CAAC;KACD,QAAQ,KAAK,CAAC;IAChB,CAAC;IACD;GAGF,KAAK;IACH,OAAO,KACL,+CACA,IAAI,aACN;IACA;GAGF,KAAK;IACH,gBAAgB,GAAG,CAAC,CAAC,OAAO,QAAiB;KAC3C,KAAK;MACH,MAAM;MACN,eAAe,IAAI;MACnB,cAAc,IAAI,MAAM;MACxB,SAAS,IAAI,MAAM;MACnB,OAAO,YAAY,GAAG;KACxB,CAAC;IACH,CAAC;IACD;GAGF,SAAS;IACP,MAAM,MAAM;IACZ,IAAI,IAAI,YAAY,gBAAgB;KAClC,MAAM,YAAY,IAAI;KACtB,MAAM,SACJ,OAAO,cAAc,WACjB,YACA;KACN,iBAAiB;MACf,MAAM,IAAI,MAAM,MAAM;KACxB,GAAG,CAAC;KACJ;IACF;IAGA;GACF;EACF;CACF;CAEA,WAAW,GAAG,WAAW,mBAAmB;CAgB5C,WAEE,yBAAyB;EAbzB;EACA,IAAI,gBAAyB;GAC3B,OAAO;EACT;EACA,IAAI,aAAiC;GACnC,OAAO;EACT;EACA,IAAI,WAAmB;GACrB,OAAO;EACT;CAI+B;AACnC;;;AC1aA,IAAI,gBAAgB,eAAe,MACjC,MAAM,IAAI,MAAM,yCAAyC;AAG3D,UAAU,UAAU"}
@@ -13,6 +13,8 @@ var WorkerBusyError = class extends Error {
13
13
  }
14
14
  };
15
15
  var WorkerExitedError = class extends Error {
16
+ exitCode;
17
+ lastCorrelationId;
16
18
  name = "WorkerExitedError";
17
19
  constructor(workerId, exitCode, lastCorrelationId) {
18
20
  super(`worker ${workerId} exited with code ${exitCode}` + (lastCorrelationId ? ` while handling correlationId ${lastCorrelationId}` : ""));
@@ -21,6 +23,8 @@ var WorkerExitedError = class extends Error {
21
23
  }
22
24
  };
23
25
  var WorkerAbortTimeoutError = class extends Error {
26
+ correlationId;
27
+ graceMs;
24
28
  name = "WorkerAbortTimeoutError";
25
29
  constructor(workerId, correlationId, graceMs) {
26
30
  super(`worker ${workerId} did not acknowledge abort for ${correlationId} within ${graceMs}ms; terminating`);
@@ -29,6 +33,7 @@ var WorkerAbortTimeoutError = class extends Error {
29
33
  }
30
34
  };
31
35
  var WorkerInitFailedError = class extends Error {
36
+ reason;
32
37
  name = "WorkerInitFailedError";
33
38
  constructor(workerId, reason, options) {
34
39
  super(`worker ${workerId} failed to initialize: ${reason}`, options);
@@ -36,6 +41,8 @@ var WorkerInitFailedError = class extends Error {
36
41
  }
37
42
  };
38
43
  var WorkerLoadModelFailedError = class extends Error {
44
+ documentType;
45
+ version;
39
46
  name = "WorkerLoadModelFailedError";
40
47
  constructor(workerId, documentType, version, reason, options) {
41
48
  super(`worker ${workerId} failed to load model ${documentType}@${version}: ${reason}`, options);
@@ -44,6 +51,7 @@ var WorkerLoadModelFailedError = class extends Error {
44
51
  }
45
52
  };
46
53
  var WorkerShutdownTimeoutError = class extends Error {
54
+ graceMs;
47
55
  name = "WorkerShutdownTimeoutError";
48
56
  constructor(workerId, graceMs) {
49
57
  super(`worker ${workerId} did not drain within ${graceMs}ms; force-terminating`);
@@ -53,4 +61,4 @@ var WorkerShutdownTimeoutError = class extends Error {
53
61
  //#endregion
54
62
  export { WorkerLoadModelFailedError as a, WorkerInitFailedError as i, WorkerBusyError as n, WorkerShutdownTimeoutError as o, WorkerExitedError as r, WorkerAbortTimeoutError as t };
55
63
 
56
- //# sourceMappingURL=errors-D3S6Eysd.js.map
64
+ //# sourceMappingURL=errors-BM_ULVWd.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors-D3S6Eysd.js","names":[],"sources":["../src/executor/worker/errors.ts"],"sourcesContent":["/**\n * Errors emitted by {@link WorkerHandle} at the IPC / transport boundary.\n *\n * These are distinct from per-job failures (which surface as\n * `JobResult.success === false`). The manager branches on `name` to decide\n * between retrying, replacing the worker, or surfacing the failure.\n */\n\nexport class WorkerBusyError extends Error {\n readonly name = \"WorkerBusyError\";\n constructor(workerId: string, jobId: string) {\n super(\n `worker ${workerId} is busy; cannot dispatch job ${jobId} while another job is in flight`,\n );\n }\n}\n\nexport class WorkerExitedError extends Error {\n readonly name = \"WorkerExitedError\";\n constructor(\n workerId: string,\n readonly exitCode: number,\n readonly lastCorrelationId: string | null,\n ) {\n super(\n `worker ${workerId} exited with code ${exitCode}` +\n (lastCorrelationId\n ? ` while handling correlationId ${lastCorrelationId}`\n : \"\"),\n );\n }\n}\n\nexport class WorkerAbortTimeoutError extends Error {\n readonly name = \"WorkerAbortTimeoutError\";\n constructor(\n workerId: string,\n readonly correlationId: string,\n readonly graceMs: number,\n ) {\n super(\n `worker ${workerId} did not acknowledge abort for ${correlationId} within ${graceMs}ms; terminating`,\n );\n }\n}\n\nexport class WorkerInitFailedError extends Error {\n readonly name = \"WorkerInitFailedError\";\n constructor(\n workerId: string,\n readonly reason: string,\n options?: { cause?: unknown },\n ) {\n super(`worker ${workerId} failed to initialize: ${reason}`, options);\n }\n}\n\nexport class WorkerLoadModelFailedError extends Error {\n readonly name = \"WorkerLoadModelFailedError\";\n constructor(\n workerId: string,\n readonly documentType: string,\n readonly version: string,\n reason: string,\n options?: { cause?: unknown },\n ) {\n super(\n `worker ${workerId} failed to load model ${documentType}@${version}: ${reason}`,\n options,\n );\n }\n}\n\nexport class WorkerShutdownTimeoutError extends Error {\n readonly name = \"WorkerShutdownTimeoutError\";\n constructor(\n workerId: string,\n readonly graceMs: number,\n ) {\n super(\n `worker ${workerId} did not drain within ${graceMs}ms; force-terminating`,\n );\n }\n}\n"],"mappings":";;;;;;;;AAQA,IAAa,kBAAb,cAAqC,MAAM;CACzC,OAAgB;CAChB,YAAY,UAAkB,OAAe;AAC3C,QACE,UAAU,SAAS,gCAAgC,MAAM,iCAC1D;;;AAIL,IAAa,oBAAb,cAAuC,MAAM;CAC3C,OAAgB;CAChB,YACE,UACA,UACA,mBACA;AACA,QACE,UAAU,SAAS,oBAAoB,cACpC,oBACG,iCAAiC,sBACjC,IACP;AARQ,OAAA,WAAA;AACA,OAAA,oBAAA;;;AAWb,IAAa,0BAAb,cAA6C,MAAM;CACjD,OAAgB;CAChB,YACE,UACA,eACA,SACA;AACA,QACE,UAAU,SAAS,iCAAiC,cAAc,UAAU,QAAQ,iBACrF;AALQ,OAAA,gBAAA;AACA,OAAA,UAAA;;;AAQb,IAAa,wBAAb,cAA2C,MAAM;CAC/C,OAAgB;CAChB,YACE,UACA,QACA,SACA;AACA,QAAM,UAAU,SAAS,yBAAyB,UAAU,QAAQ;AAH3D,OAAA,SAAA;;;AAOb,IAAa,6BAAb,cAAgD,MAAM;CACpD,OAAgB;CAChB,YACE,UACA,cACA,SACA,QACA,SACA;AACA,QACE,UAAU,SAAS,wBAAwB,aAAa,GAAG,QAAQ,IAAI,UACvE,QACD;AARQ,OAAA,eAAA;AACA,OAAA,UAAA;;;AAWb,IAAa,6BAAb,cAAgD,MAAM;CACpD,OAAgB;CAChB,YACE,UACA,SACA;AACA,QACE,UAAU,SAAS,wBAAwB,QAAQ,uBACpD;AAJQ,OAAA,UAAA"}
1
+ {"version":3,"file":"errors-BM_ULVWd.js","names":[],"sources":["../src/executor/worker/errors.ts"],"sourcesContent":["/**\n * Errors emitted by {@link WorkerHandle} at the IPC / transport boundary.\n *\n * These are distinct from per-job failures (which surface as\n * `JobResult.success === false`). The manager branches on `name` to decide\n * between retrying, replacing the worker, or surfacing the failure.\n */\n\nexport class WorkerBusyError extends Error {\n readonly name = \"WorkerBusyError\";\n constructor(workerId: string, jobId: string) {\n super(\n `worker ${workerId} is busy; cannot dispatch job ${jobId} while another job is in flight`,\n );\n }\n}\n\nexport class WorkerExitedError extends Error {\n readonly name = \"WorkerExitedError\";\n constructor(\n workerId: string,\n readonly exitCode: number,\n readonly lastCorrelationId: string | null,\n ) {\n super(\n `worker ${workerId} exited with code ${exitCode}` +\n (lastCorrelationId\n ? ` while handling correlationId ${lastCorrelationId}`\n : \"\"),\n );\n }\n}\n\nexport class WorkerAbortTimeoutError extends Error {\n readonly name = \"WorkerAbortTimeoutError\";\n constructor(\n workerId: string,\n readonly correlationId: string,\n readonly graceMs: number,\n ) {\n super(\n `worker ${workerId} did not acknowledge abort for ${correlationId} within ${graceMs}ms; terminating`,\n );\n }\n}\n\nexport class WorkerInitFailedError extends Error {\n readonly name = \"WorkerInitFailedError\";\n constructor(\n workerId: string,\n readonly reason: string,\n options?: { cause?: unknown },\n ) {\n super(`worker ${workerId} failed to initialize: ${reason}`, options);\n }\n}\n\nexport class WorkerLoadModelFailedError extends Error {\n readonly name = \"WorkerLoadModelFailedError\";\n constructor(\n workerId: string,\n readonly documentType: string,\n readonly version: string,\n reason: string,\n options?: { cause?: unknown },\n ) {\n super(\n `worker ${workerId} failed to load model ${documentType}@${version}: ${reason}`,\n options,\n );\n }\n}\n\nexport class WorkerShutdownTimeoutError extends Error {\n readonly name = \"WorkerShutdownTimeoutError\";\n constructor(\n workerId: string,\n readonly graceMs: number,\n ) {\n super(\n `worker ${workerId} did not drain within ${graceMs}ms; force-terminating`,\n );\n }\n}\n"],"mappings":";;;;;;;;AAQA,IAAa,kBAAb,cAAqC,MAAM;CACzC,OAAgB;CAChB,YAAY,UAAkB,OAAe;EAC3C,MACE,UAAU,SAAS,gCAAgC,MAAM,gCAC3D;CACF;AACF;AAEA,IAAa,oBAAb,cAAuC,MAAM;CAIhC;CACA;CAJX,OAAgB;CAChB,YACE,UACA,UACA,mBACA;EACA,MACE,UAAU,SAAS,oBAAoB,cACpC,oBACG,iCAAiC,sBACjC,GACR;EARS,KAAA,WAAA;EACA,KAAA,oBAAA;CAQX;AACF;AAEA,IAAa,0BAAb,cAA6C,MAAM;CAItC;CACA;CAJX,OAAgB;CAChB,YACE,UACA,eACA,SACA;EACA,MACE,UAAU,SAAS,iCAAiC,cAAc,UAAU,QAAQ,gBACtF;EALS,KAAA,gBAAA;EACA,KAAA,UAAA;CAKX;AACF;AAEA,IAAa,wBAAb,cAA2C,MAAM;CAIpC;CAHX,OAAgB;CAChB,YACE,UACA,QACA,SACA;EACA,MAAM,UAAU,SAAS,yBAAyB,UAAU,OAAO;EAH1D,KAAA,SAAA;CAIX;AACF;AAEA,IAAa,6BAAb,cAAgD,MAAM;CAIzC;CACA;CAJX,OAAgB;CAChB,YACE,UACA,cACA,SACA,QACA,SACA;EACA,MACE,UAAU,SAAS,wBAAwB,aAAa,GAAG,QAAQ,IAAI,UACvE,OACF;EARS,KAAA,eAAA;EACA,KAAA,UAAA;CAQX;AACF;AAEA,IAAa,6BAAb,cAAgD,MAAM;CAIzC;CAHX,OAAgB;CAChB,YACE,UACA,SACA;EACA,MACE,UAAU,SAAS,wBAAwB,QAAQ,sBACrD;EAJS,KAAA,UAAA;CAKX;AACF"}