@earendil-works/pi-durable 1.1.0 → 1.3.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 (216) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +164 -11
  3. package/dist/codemode/codemode.d.ts +173 -0
  4. package/dist/codemode/codemode.d.ts.map +1 -0
  5. package/dist/codemode/codemode.js +886 -0
  6. package/dist/codemode/codemode.js.map +1 -0
  7. package/dist/codemode/describe.d.ts +38 -0
  8. package/dist/codemode/describe.d.ts.map +1 -0
  9. package/dist/codemode/describe.js +119 -0
  10. package/dist/codemode/describe.js.map +1 -0
  11. package/dist/codemode/index.d.ts +3 -0
  12. package/dist/codemode/index.d.ts.map +1 -0
  13. package/dist/codemode/index.js +3 -0
  14. package/dist/codemode/index.js.map +1 -0
  15. package/dist/codemode/models.d.ts +28 -0
  16. package/dist/codemode/models.d.ts.map +1 -0
  17. package/dist/codemode/models.js +231 -0
  18. package/dist/codemode/models.js.map +1 -0
  19. package/dist/codemode/output.d.ts +41 -0
  20. package/dist/codemode/output.d.ts.map +1 -0
  21. package/dist/codemode/output.js +167 -0
  22. package/dist/codemode/output.js.map +1 -0
  23. package/dist/env/index.d.ts +43 -0
  24. package/dist/env/index.d.ts.map +1 -1
  25. package/dist/env/index.js.map +1 -1
  26. package/dist/errors.d.ts +20 -3
  27. package/dist/errors.d.ts.map +1 -1
  28. package/dist/errors.js +34 -5
  29. package/dist/errors.js.map +1 -1
  30. package/dist/harness/agent.d.ts +5 -2
  31. package/dist/harness/agent.d.ts.map +1 -1
  32. package/dist/harness/agent.js +69 -25
  33. package/dist/harness/agent.js.map +1 -1
  34. package/dist/harness/compaction.d.ts +6 -1
  35. package/dist/harness/compaction.d.ts.map +1 -1
  36. package/dist/harness/compaction.js +10 -7
  37. package/dist/harness/compaction.js.map +1 -1
  38. package/dist/harness/context.d.ts +5 -0
  39. package/dist/harness/context.d.ts.map +1 -1
  40. package/dist/harness/context.js +46 -12
  41. package/dist/harness/context.js.map +1 -1
  42. package/dist/harness/events.d.ts +35 -15
  43. package/dist/harness/events.d.ts.map +1 -1
  44. package/dist/harness/events.js +77 -23
  45. package/dist/harness/events.js.map +1 -1
  46. package/dist/harness/generation.d.ts +2 -0
  47. package/dist/harness/generation.d.ts.map +1 -1
  48. package/dist/harness/generation.js +31 -8
  49. package/dist/harness/generation.js.map +1 -1
  50. package/dist/harness/harness.d.ts +7 -0
  51. package/dist/harness/harness.d.ts.map +1 -1
  52. package/dist/harness/harness.js +53 -26
  53. package/dist/harness/harness.js.map +1 -1
  54. package/dist/harness/inbox.d.ts +16 -4
  55. package/dist/harness/inbox.d.ts.map +1 -1
  56. package/dist/harness/inbox.js +26 -3
  57. package/dist/harness/inbox.js.map +1 -1
  58. package/dist/harness/live.d.ts +74 -17
  59. package/dist/harness/live.d.ts.map +1 -1
  60. package/dist/harness/live.js +66 -14
  61. package/dist/harness/live.js.map +1 -1
  62. package/dist/harness/scheduler.d.ts +18 -5
  63. package/dist/harness/scheduler.d.ts.map +1 -1
  64. package/dist/harness/scheduler.js +696 -163
  65. package/dist/harness/scheduler.js.map +1 -1
  66. package/dist/harness/submissions.d.ts +7 -4
  67. package/dist/harness/submissions.d.ts.map +1 -1
  68. package/dist/harness/submissions.js +32 -13
  69. package/dist/harness/submissions.js.map +1 -1
  70. package/dist/harness/task-graph.d.ts +1 -1
  71. package/dist/harness/task-graph.d.ts.map +1 -1
  72. package/dist/harness/task-graph.js +15 -10
  73. package/dist/harness/task-graph.js.map +1 -1
  74. package/dist/harness/tool.d.ts +45 -5
  75. package/dist/harness/tool.d.ts.map +1 -1
  76. package/dist/harness/tool.js +378 -63
  77. package/dist/harness/tool.js.map +1 -1
  78. package/dist/harness/types.d.ts +153 -13
  79. package/dist/harness/types.d.ts.map +1 -1
  80. package/dist/harness/types.js.map +1 -1
  81. package/dist/harness/util.d.ts +6 -1
  82. package/dist/harness/util.d.ts.map +1 -1
  83. package/dist/harness/util.js +5 -2
  84. package/dist/harness/util.js.map +1 -1
  85. package/dist/harness/view.d.ts +6 -2
  86. package/dist/harness/view.d.ts.map +1 -1
  87. package/dist/harness/view.js +22 -12
  88. package/dist/harness/view.js.map +1 -1
  89. package/dist/images/cloudflare.d.ts +4 -0
  90. package/dist/images/cloudflare.d.ts.map +1 -0
  91. package/dist/images/cloudflare.js +9 -0
  92. package/dist/images/cloudflare.js.map +1 -0
  93. package/dist/images/exif.d.ts +3 -0
  94. package/dist/images/exif.d.ts.map +1 -0
  95. package/dist/images/exif.js +71 -0
  96. package/dist/images/exif.js.map +1 -0
  97. package/dist/images/index.d.ts +18 -0
  98. package/dist/images/index.d.ts.map +1 -0
  99. package/dist/images/index.js +125 -0
  100. package/dist/images/index.js.map +1 -0
  101. package/dist/images/node-worker.d.ts +17 -0
  102. package/dist/images/node-worker.d.ts.map +1 -0
  103. package/dist/images/node-worker.js +16 -0
  104. package/dist/images/node-worker.js.map +1 -0
  105. package/dist/images/node.d.ts +21 -0
  106. package/dist/images/node.d.ts.map +1 -0
  107. package/dist/images/node.js +121 -0
  108. package/dist/images/node.js.map +1 -0
  109. package/dist/index.d.ts +6 -6
  110. package/dist/index.d.ts.map +1 -1
  111. package/dist/index.js +3 -3
  112. package/dist/index.js.map +1 -1
  113. package/dist/search.d.ts +13 -0
  114. package/dist/search.d.ts.map +1 -0
  115. package/dist/search.js +85 -0
  116. package/dist/search.js.map +1 -0
  117. package/dist/session/observation.d.ts +10 -3
  118. package/dist/session/observation.d.ts.map +1 -1
  119. package/dist/session/observation.js +24 -34
  120. package/dist/session/observation.js.map +1 -1
  121. package/dist/session/session.d.ts +68 -5
  122. package/dist/session/session.d.ts.map +1 -1
  123. package/dist/session/session.js +461 -49
  124. package/dist/session/session.js.map +1 -1
  125. package/dist/session/transaction.d.ts.map +1 -1
  126. package/dist/session/transaction.js +1 -0
  127. package/dist/session/transaction.js.map +1 -1
  128. package/dist/storage/jsonl/storage.d.ts +4 -0
  129. package/dist/storage/jsonl/storage.d.ts.map +1 -1
  130. package/dist/storage/jsonl/storage.js +2 -1
  131. package/dist/storage/jsonl/storage.js.map +1 -1
  132. package/dist/storage/memory.d.ts.map +1 -1
  133. package/dist/storage/memory.js +8 -10
  134. package/dist/storage/memory.js.map +1 -1
  135. package/dist/storage/scan.d.ts.map +1 -1
  136. package/dist/storage/scan.js +5 -4
  137. package/dist/storage/scan.js.map +1 -1
  138. package/dist/storage/sqlite/cloudflare.d.ts +17 -5
  139. package/dist/storage/sqlite/cloudflare.d.ts.map +1 -1
  140. package/dist/storage/sqlite/cloudflare.js +57 -21
  141. package/dist/storage/sqlite/cloudflare.js.map +1 -1
  142. package/dist/storage/sqlite/index.d.ts +1 -1
  143. package/dist/storage/sqlite/index.d.ts.map +1 -1
  144. package/dist/storage/sqlite/index.js.map +1 -1
  145. package/dist/storage/sqlite/migrations.d.ts +5 -1
  146. package/dist/storage/sqlite/migrations.d.ts.map +1 -1
  147. package/dist/storage/sqlite/migrations.js +94 -84
  148. package/dist/storage/sqlite/migrations.js.map +1 -1
  149. package/dist/storage/sqlite/node.d.ts +6 -4
  150. package/dist/storage/sqlite/node.d.ts.map +1 -1
  151. package/dist/storage/sqlite/node.js +6 -2
  152. package/dist/storage/sqlite/node.js.map +1 -1
  153. package/dist/storage/sqlite/schema.d.ts +47 -0
  154. package/dist/storage/sqlite/schema.d.ts.map +1 -0
  155. package/dist/storage/sqlite/schema.js +57 -0
  156. package/dist/storage/sqlite/schema.js.map +1 -0
  157. package/dist/storage/sqlite/storage.d.ts +9 -1
  158. package/dist/storage/sqlite/storage.d.ts.map +1 -1
  159. package/dist/storage/sqlite/storage.js +56 -55
  160. package/dist/storage/sqlite/storage.js.map +1 -1
  161. package/dist/testing/env-conformance.d.ts.map +1 -1
  162. package/dist/testing/env-conformance.js +100 -0
  163. package/dist/testing/env-conformance.js.map +1 -1
  164. package/dist/testing/runner.d.ts +1 -1
  165. package/dist/testing/runner.d.ts.map +1 -1
  166. package/dist/testing/runner.js.map +1 -1
  167. package/dist/testing/storage-conformance.d.ts.map +1 -1
  168. package/dist/testing/storage-conformance.js +3 -2
  169. package/dist/testing/storage-conformance.js.map +1 -1
  170. package/dist/testing/types.d.ts +2 -0
  171. package/dist/testing/types.d.ts.map +1 -1
  172. package/dist/testing/types.js.map +1 -1
  173. package/dist/tool-search/index.d.ts +2 -0
  174. package/dist/tool-search/index.d.ts.map +1 -0
  175. package/dist/tool-search/index.js +2 -0
  176. package/dist/tool-search/index.js.map +1 -0
  177. package/dist/tool-search/tool-search.d.ts +23 -0
  178. package/dist/tool-search/tool-search.d.ts.map +1 -0
  179. package/dist/tool-search/tool-search.js +84 -0
  180. package/dist/tool-search/tool-search.js.map +1 -0
  181. package/dist/tools/bash.d.ts +10 -2
  182. package/dist/tools/bash.d.ts.map +1 -1
  183. package/dist/tools/bash.js +27 -9
  184. package/dist/tools/bash.js.map +1 -1
  185. package/dist/tools/edit.js +1 -1
  186. package/dist/tools/edit.js.map +1 -1
  187. package/dist/tools/image-processor.d.ts +50 -0
  188. package/dist/tools/image-processor.d.ts.map +1 -0
  189. package/dist/tools/image-processor.js +20 -0
  190. package/dist/tools/image-processor.js.map +1 -0
  191. package/dist/tools/image.d.ts +8 -0
  192. package/dist/tools/image.d.ts.map +1 -1
  193. package/dist/tools/image.js +47 -0
  194. package/dist/tools/image.js.map +1 -1
  195. package/dist/tools/index.d.ts +25 -2
  196. package/dist/tools/index.d.ts.map +1 -1
  197. package/dist/tools/index.js +13 -5
  198. package/dist/tools/index.js.map +1 -1
  199. package/dist/tools/read.d.ts +10 -2
  200. package/dist/tools/read.d.ts.map +1 -1
  201. package/dist/tools/read.js +88 -29
  202. package/dist/tools/read.js.map +1 -1
  203. package/dist/tools/write.js +1 -1
  204. package/dist/tools/write.js.map +1 -1
  205. package/dist/truncate.d.ts +3 -2
  206. package/dist/truncate.d.ts.map +1 -1
  207. package/dist/truncate.js +4 -34
  208. package/dist/truncate.js.map +1 -1
  209. package/dist/types.d.ts +59 -5
  210. package/dist/types.d.ts.map +1 -1
  211. package/dist/types.js.map +1 -1
  212. package/package.json +43 -4
  213. package/dist/harness/output.d.ts +0 -69
  214. package/dist/harness/output.d.ts.map +0 -1
  215. package/dist/harness/output.js +0 -318
  216. package/dist/harness/output.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"memory.js","sourceRoot":"","sources":["../../src/storage/memory.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAW,MAAM,6BAA6B,CAAC;AAC7E,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AA+BxD,OAAO,EAAE,UAAU,EAAkB,SAAS,EAAE,MAAM,WAAW,CAAC;AAiBlE,QAAQ,CAAC,CAAC,oBAAoB,CAC7B,EAAc,EACd,OAAe,EACf,SAAsC,EACtC,KAAa;IAEb,KAAK,IAAI,KAAK,GAAG,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC3D,MAAM,QAAQ,GAAG,SAAS,CAAC,KAAK,CAAE,CAAC;QACnC,IAAI,QAAQ,CAAC,IAAI,KAAK,OAAO,IAAI,QAAQ,CAAC,OAAO,KAAK,OAAO,EAAE,CAAC;YAC/D,MAAM,IAAI,KAAK,CAAC,YAAY,EAAE,mDAAmD,CAAC,CAAC;QACpF,CAAC;QACD,MAAM,QAAQ,CAAC,GAAG,CAAC;IACpB,CAAC;AACF,CAAC;AA6BD,MAAM,KAAK,GAAG,CAAI,KAAQ,EAAK,EAAE;IAChC,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC9D,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CA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,EAAE,CAAC;gBAClD,MAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,GAAG,CAAC,gBAAgB,CAAC,MAAO,CAAC,MAAM,CAAC,CAAE,CAAC;gBACpF,IAAI,MAAM,CAAC,MAAM,IAAI,OAAO,CAAC,SAAS,KAAK,EAAE;oBAAE,OAAO,OAAO,CAAC,SAAS,CAAC;gBACxE,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,MAAM;oBAAE,OAAO,CAAC,SAAS,GAAG,EAAE,CAAC;YAC3E,CAAC;QACF,CAAC;IACF,CAAC;IAEO,UAAU;QACjB,IAAI,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7D,CAAC;CACD","sourcesContent":["import type { Context, JsonValue } from \"@earendil-works/chord\";\nimport { applyImmutableBatches, type Op } from \"@earendil-works/chord/delta\";\nimport { StorageRejected } from \"../errors.ts\";\nimport { idFromNumber, seqFromNumber } from \"../ids.ts\";\nimport type {\n\tConversationId,\n\tConversationQuery,\n\tConversationRecord,\n\tCursor,\n\tDocumentAddress,\n\tDocumentContent,\n\tDocumentCreate,\n\tDocumentId,\n\tDocumentPoint,\n\tDocumentQuery,\n\tDocumentRecord,\n\tEntryId,\n\tEntryQuery,\n\tEntryRecord,\n\tId,\n\tJsonObject,\n\tPage,\n\tScanOrder,\n\tSeq,\n\tStorage,\n\tStorageWrite,\n\tStoredDocument,\n\tSubmissionId,\n\tSubmissionQuery,\n\tSubmissionRecord,\n\tTaskId,\n\tTaskQuery,\n\tTaskRecord,\n} from \"../types.ts\";\nimport { nextCursor, type ScanStart, scanStart } from \"./scan.ts\";\n\ntype StoredTask = TaskRecord<JsonValue, JsonValue, JsonValue>;\ntype TaskStatus = StoredTask[\"state\"][\"status\"];\ntype SubmissionStatus = SubmissionRecord[\"status\"];\ntype TableName = \"conversation\" | \"entry\" | \"task\" | \"submission\" | \"document\";\ntype DocumentRevision = DocumentContent & { readonly seq: Seq };\ntype StoredDocumentState = {\n\trecord: DocumentRecord;\n\trevisions: DocumentRevision[];\n};\ntype DocumentAction = {\n\tcreate?: DocumentCreate;\n\tcontent?: DocumentContent;\n\tretire: boolean;\n};\n\nfunction* documentDeltaBatches(\n\tid: DocumentId,\n\tversion: number,\n\trevisions: readonly DocumentRevision[],\n\tstart: number,\n): Generator<readonly Op[]> {\n\tfor (let index = start; index < revisions.length; index++) {\n\t\tconst revision = revisions[index]!;\n\t\tif (revision.kind !== \"delta\" || revision.version !== version) {\n\t\t\tthrow new Error(`Document ${id} crosses a stored version boundary without a base`);\n\t\t}\n\t\tyield revision.ops;\n\t}\n}\n\ntype DocumentAddressIndex = {\n\tids: DocumentId[];\n\tcurrentId?: DocumentId;\n};\n\ntype State = {\n\trecordTypes: Map<Id<string>, TableName>;\n\tconversations: Map<ConversationId, ConversationRecord>;\n\tconversationIds: ConversationId[];\n\tconversationIdsByOwnerConversation: Map<ConversationId, ConversationId[]>;\n\tconversationIdsByOwnerTask: Map<TaskId, ConversationId[]>;\n\tentries: Map<EntryId, EntryRecord>;\n\tentryIds: Map<ConversationId, EntryId[]>;\n\theadEntryIds: Map<ConversationId, EntryId[]>;\n\tentryCommitSeqs: Map<EntryId, Seq>;\n\ttasks: Map<TaskId, StoredTask>;\n\ttaskIds: TaskId[];\n\ttaskIdsByStatus: Record<TaskStatus, TaskId[]>;\n\tsubmissions: Map<SubmissionId, SubmissionRecord>;\n\tsubmissionIds: SubmissionId[];\n\tsubmissionIdsByStatus: Record<SubmissionStatus, SubmissionId[]>;\n\tsubmissionIdsByRequest: Map<ConversationId, Map<string, SubmissionId>>;\n\tdocuments: Map<DocumentId, StoredDocumentState>;\n\tdocumentAddresses: Map<string, DocumentAddressIndex>;\n\tdocumentIdsByScope: Map<string, DocumentId[]>;\n};\n\nconst clone = <T>(value: T): T => {\n\tif (value === null || typeof value !== \"object\") return value;\n\tif (Array.isArray(value)) return value.map((item) => clone(item)) as T;\n\tconst source = value as Record<string, unknown>;\n\tconst nullPrototype = Object.getPrototypeOf(value) === null;\n\tconst result = (nullPrototype ? Object.create(null) : {}) as Record<string, unknown>;\n\tfor (const key of Object.keys(source)) {\n\t\tconst copied = clone(source[key]);\n\t\tif (!nullPrototype && key in result) {\n\t\t\tObject.defineProperty(result, key, {\n\t\t\t\tvalue: copied,\n\t\t\t\twritable: true,\n\t\t\t\tenumerable: true,\n\t\t\t\tconfigurable: true,\n\t\t\t});\n\t\t} else {\n\t\t\tresult[key] = copied;\n\t\t}\n\t}\n\treturn result as T;\n};\n\nconst freeze = <T>(value: T): T => {\n\tif (value === null || typeof value !== \"object\" || Object.isFrozen(value)) return value;\n\tfor (const child of Object.values(value)) freeze(child);\n\treturn Object.freeze(value);\n};\n\nconst cursorId = <I extends Id<string>>(cursor: Readonly<Record<string, JsonValue>> | undefined): I | undefined => {\n\tconst after = cursor?.after;\n\tif (after === undefined) return undefined;\n\tif (typeof after !== \"number\" || !Number.isSafeInteger(after)) throw new TypeError(\"Invalid storage cursor\");\n\treturn idFromNumber<I>(after);\n};\n\nconst lowerBound = <I extends Id<string>>(ids: readonly I[], target: number): number => {\n\tlet low = 0;\n\tlet high = ids.length;\n\twhile (low < high) {\n\t\tconst middle = (low + high) >>> 1;\n\t\tif (ids[middle] < target) low = middle + 1;\n\t\telse high = middle;\n\t}\n\treturn low;\n};\n\nconst upperBound = <I extends Id<string>>(ids: readonly I[], target: number): number => {\n\tlet low = 0;\n\tlet high = ids.length;\n\twhile (low < high) {\n\t\tconst middle = (low + high) >>> 1;\n\t\tif (ids[middle] <= target) low = middle + 1;\n\t\telse high = middle;\n\t}\n\treturn low;\n};\n\nconst insertSorted = <I extends Id<string>>(ids: I[], id: I): void => {\n\tif (ids.length === 0 || ids[ids.length - 1] < id) ids.push(id);\n\telse ids.splice(lowerBound(ids, id), 0, id);\n};\n\nconst removeSorted = <I extends Id<string>>(ids: I[], id: I): void => {\n\tconst index = lowerBound(ids, id);\n\tif (ids[index] === id) ids.splice(index, 1);\n};\n\nconst insertMapId = <K, I extends Id<string>>(index: Map<K, I[]>, key: K, id: I): void => {\n\tlet ids = index.get(key);\n\tif (ids === undefined) {\n\t\tids = [];\n\t\tindex.set(key, ids);\n\t}\n\tinsertSorted(ids, id);\n};\n\nconst scopeKey = (scope: DocumentRecord[\"scope\"]): string => {\n\tswitch (scope.kind) {\n\t\tcase \"session\":\n\t\t\treturn JSON.stringify([\"session\"]);\n\t\tcase \"conversation\":\n\t\t\treturn JSON.stringify([\"conversation\", scope.conversationId]);\n\t\tcase \"task\":\n\t\t\treturn JSON.stringify([\"task\", scope.taskId]);\n\t}\n};\n\nconst addressKey = (address: DocumentAddress): string =>\n\tJSON.stringify([\n\t\taddress.kind,\n\t\tscopeKey(address.scope),\n\t\taddress.key === undefined ? [\"singleton\"] : [\"family\", address.key],\n\t]);\n\nconst recordAddressKey = (record: DocumentRecord | DocumentCreate): string =>\n\taddressKey({ kind: record.kind, scope: record.scope, key: record.key });\n\nconst isAliveAt = (record: DocumentRecord, at: DocumentPoint): boolean => {\n\tif (at === \"current\") return record.retiredAt === undefined;\n\treturn record.createdAt <= at && (record.retiredAt === undefined || at < record.retiredAt);\n};\n\nconst isCurrentOnly = (record: DocumentRecord): boolean =>\n\trecord.scope.kind !== \"conversation\" || record.history === \"latest\";\n\nconst tableContaining = (state: State, id: Id<string>): TableName | undefined => state.recordTypes.get(id);\n\nconst page = <T extends { readonly id: Id<string> }>(\n\tvalues: readonly T[],\n\tlimit: number,\n\torder: ScanOrder = \"ascending\",\n): Page<T, Cursor> => {\n\tconst items = values.slice(0, limit);\n\tif (values.length <= limit) return { items: clone(items) };\n\treturn { items: clone(items), next: nextCursor(items.at(-1)!.id, order) };\n};\n\n/** Indexes of sorted `ids` in scan order, after the cursor's ID when there is one. */\nfunction* scanIndexes(ids: readonly Id<string>[], start: ScanStart): Generator<number> {\n\tif (start.order === \"ascending\") {\n\t\tconst first = start.after === undefined ? 0 : upperBound(ids, start.after);\n\t\tfor (let index = first; index < ids.length; index++) yield index;\n\t} else {\n\t\tconst end = start.after === undefined ? ids.length : lowerBound(ids, start.after);\n\t\tfor (let index = end - 1; index >= 0; index--) yield index;\n\t}\n}\n\n/** A fully validated, detached state mutation whose application performs no fallible preparation. */\nexport interface PreparedMemoryCommit {\n\treadonly seq: Seq;\n\t/** Deeply frozen detached writes for persistence. */\n\treadonly writes: readonly StorageWrite[];\n\tapply(): Seq;\n}\n\n/**\n * Detached in-memory reference implementation of `Storage`.\n *\n * Reads and retained writes are cloned intentionally to match the ownership boundary\n * of serialization-backed stores. This is backend conformance, not validation.\n */\nexport class MemoryStorage implements Storage {\n\tprivate readonly state: State = {\n\t\trecordTypes: new Map(),\n\t\tconversations: new Map(),\n\t\tconversationIds: [],\n\t\tconversationIdsByOwnerConversation: new Map(),\n\t\tconversationIdsByOwnerTask: new Map(),\n\t\tentries: new Map(),\n\t\tentryIds: new Map(),\n\t\theadEntryIds: new Map(),\n\t\tentryCommitSeqs: new Map(),\n\t\ttasks: new Map(),\n\t\ttaskIds: [],\n\t\ttaskIdsByStatus: { pending: [], running: [], waiting: [], completing: [], terminal: [] },\n\t\tsubmissions: new Map(),\n\t\tsubmissionIds: [],\n\t\tsubmissionIdsByStatus: { queued: [], placed: [], done: [], unanswered: [] },\n\t\tsubmissionIdsByRequest: new Map(),\n\t\tdocuments: new Map(),\n\t\tdocumentAddresses: new Map(),\n\t\tdocumentIdsByScope: new Map(),\n\t};\n\tprivate nextId = 2;\n\tprivate nextSeq = 1;\n\tprivate closed = false;\n\n\tasync commit(writes: readonly StorageWrite[], _context: Context): Promise<Seq> {\n\t\treturn this.prepareCommit(writes).apply();\n\t}\n\n\t/** Validate and detach one commit without changing observable state. */\n\tprepareCommit(writes: readonly StorageWrite[], seq: Seq = seqFromNumber(this.nextSeq)): PreparedMemoryCommit {\n\t\tthis.assertOpen();\n\t\tif (!Number.isSafeInteger(seq) || seq < this.nextSeq) {\n\t\t\tthrow new Error(`Commit sequence ${seq} does not strictly increase`);\n\t\t}\n\t\tconst detachedWrites = freeze(this.resolveDocumentCopies(writes.map((write) => clone(write))));\n\t\tthis.checkGlobalIds(detachedWrites);\n\t\tconst documentActions = this.prepareDocumentActions(detachedWrites);\n\t\tthis.checkDocumentActions(documentActions);\n\t\tlet applied = false;\n\t\treturn {\n\t\t\tseq,\n\t\t\twrites: detachedWrites,\n\t\t\tapply: () => {\n\t\t\t\tif (!applied) {\n\t\t\t\t\tapplied = true;\n\t\t\t\t\tthis.applyPreparedCommit(detachedWrites, documentActions, seq);\n\t\t\t\t}\n\t\t\t\treturn seq;\n\t\t\t},\n\t\t};\n\t}\n\n\tprivate resolveDocumentCopies(writes: StorageWrite[]): StorageWrite[] {\n\t\tif (!writes.some((write) => write.type === \"document.copy\")) return writes;\n\t\tconst changedDocumentIds = new Set<DocumentId>();\n\t\tfor (const write of writes) {\n\t\t\tif (write.type === \"document.create\" || write.type === \"document.copy\") {\n\t\t\t\tchangedDocumentIds.add(write.record.id);\n\t\t\t} else if (write.type === \"document.change\" || write.type === \"document.retire\") {\n\t\t\t\tchangedDocumentIds.add(write.id);\n\t\t\t}\n\t\t}\n\t\treturn writes.map((write) => {\n\t\t\tif (write.type !== \"document.copy\") return write;\n\t\t\ttry {\n\t\t\t\tif (changedDocumentIds.has(write.source.id)) {\n\t\t\t\t\tthrow new Error(`Fork source document ${write.source.id} is changed in the copy batch`);\n\t\t\t\t}\n\t\t\t\tconst stored = this.materializeDocument(write.source.id, write.source.at);\n\t\t\t\tif (stored === undefined) throw new Error(`Fork source document ${write.source.id} cannot be read`);\n\t\t\t\tif (\n\t\t\t\t\tstored.record.scope.kind !== \"conversation\" ||\n\t\t\t\t\twrite.record.scope.kind !== \"conversation\" ||\n\t\t\t\t\tstored.record.kind !== write.record.kind ||\n\t\t\t\t\tstored.record.key !== write.record.key ||\n\t\t\t\t\tstored.record.history !== write.record.history ||\n\t\t\t\t\tstored.record.fork !== write.record.fork\n\t\t\t\t) {\n\t\t\t\t\tthrow new Error(`Fork source document ${write.source.id} does not match the copied record`);\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"document.create\",\n\t\t\t\t\trecord: write.record,\n\t\t\t\t\tcontent: { kind: \"base\", version: stored.version, value: stored.value },\n\t\t\t\t};\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof StorageRejected) throw error;\n\t\t\t\tthrow new StorageRejected(`Document copy ${write.record.id} was rejected`, { cause: error });\n\t\t\t}\n\t\t});\n\t}\n\n\tprivate applyPreparedCommit(\n\t\tprepared: readonly StorageWrite[],\n\t\tdocumentActions: ReadonlyMap<DocumentId, DocumentAction>,\n\t\tseq: Seq,\n\t): Seq {\n\t\tfor (const write of prepared) {\n\t\t\tswitch (write.type) {\n\t\t\t\tcase \"conversation\": {\n\t\t\t\t\tthis.state.recordTypes.set(write.value.id, \"conversation\");\n\t\t\t\t\tthis.state.conversations.set(write.value.id, write.value);\n\t\t\t\t\tinsertSorted(this.state.conversationIds, write.value.id);\n\t\t\t\t\tconst owner = write.value.owner;\n\t\t\t\t\tif (owner !== undefined) {\n\t\t\t\t\t\tinsertMapId(this.state.conversationIdsByOwnerConversation, owner.conversationId, write.value.id);\n\t\t\t\t\t\tinsertMapId(this.state.conversationIdsByOwnerTask, owner.taskId, write.value.id);\n\t\t\t\t\t}\n\t\t\t\t\tthis.nextId = Math.max(this.nextId, write.value.id + 1);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"entry\": {\n\t\t\t\t\tthis.state.recordTypes.set(write.value.id, \"entry\");\n\t\t\t\t\tthis.state.entries.set(write.value.id, write.value);\n\t\t\t\t\tthis.state.entryCommitSeqs.set(write.value.id, seq);\n\t\t\t\t\tlet ids = this.state.entryIds.get(write.value.conversationId);\n\t\t\t\t\tif (ids === undefined) {\n\t\t\t\t\t\tids = [];\n\t\t\t\t\t\tthis.state.entryIds.set(write.value.conversationId, ids);\n\t\t\t\t\t}\n\t\t\t\t\tinsertSorted(ids, write.value.id);\n\t\t\t\t\tif (write.value.head !== undefined) {\n\t\t\t\t\t\tlet headIds = this.state.headEntryIds.get(write.value.conversationId);\n\t\t\t\t\t\tif (headIds === undefined) {\n\t\t\t\t\t\t\theadIds = [];\n\t\t\t\t\t\t\tthis.state.headEntryIds.set(write.value.conversationId, headIds);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tinsertSorted(headIds, write.value.id);\n\t\t\t\t\t}\n\t\t\t\t\tthis.nextId = Math.max(this.nextId, write.value.id + 1);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"task\": {\n\t\t\t\t\tthis.state.recordTypes.set(write.value.id, \"task\");\n\t\t\t\t\tconst previous = this.state.tasks.get(write.value.id);\n\t\t\t\t\tif (previous === undefined) {\n\t\t\t\t\t\tinsertSorted(this.state.taskIds, write.value.id);\n\t\t\t\t\t\tinsertSorted(this.state.taskIdsByStatus[write.value.state.status], write.value.id);\n\t\t\t\t\t} else if (previous.state.status !== write.value.state.status) {\n\t\t\t\t\t\tremoveSorted(this.state.taskIdsByStatus[previous.state.status], write.value.id);\n\t\t\t\t\t\tinsertSorted(this.state.taskIdsByStatus[write.value.state.status], write.value.id);\n\t\t\t\t\t}\n\t\t\t\t\tthis.state.tasks.set(write.value.id, write.value);\n\t\t\t\t\tthis.nextId = Math.max(this.nextId, write.value.id + 1);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"submission\": {\n\t\t\t\t\tthis.state.recordTypes.set(write.value.id, \"submission\");\n\t\t\t\t\tconst previous = this.state.submissions.get(write.value.id);\n\t\t\t\t\tif (previous === undefined) {\n\t\t\t\t\t\tinsertSorted(this.state.submissionIds, write.value.id);\n\t\t\t\t\t\tinsertSorted(this.state.submissionIdsByStatus[write.value.status], write.value.id);\n\t\t\t\t\t} else if (previous.status !== write.value.status) {\n\t\t\t\t\t\tremoveSorted(this.state.submissionIdsByStatus[previous.status], write.value.id);\n\t\t\t\t\t\tinsertSorted(this.state.submissionIdsByStatus[write.value.status], write.value.id);\n\t\t\t\t\t}\n\t\t\t\t\tif (previous?.requestId !== undefined) {\n\t\t\t\t\t\tconst previousRequests = this.state.submissionIdsByRequest.get(previous.conversationId);\n\t\t\t\t\t\tif (previousRequests?.get(previous.requestId) === write.value.id) {\n\t\t\t\t\t\t\tpreviousRequests.delete(previous.requestId);\n\t\t\t\t\t\t\tif (previousRequests.size === 0) this.state.submissionIdsByRequest.delete(previous.conversationId);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tthis.state.submissions.set(write.value.id, write.value);\n\t\t\t\t\tif (write.value.requestId !== undefined) {\n\t\t\t\t\t\tlet requests = this.state.submissionIdsByRequest.get(write.value.conversationId);\n\t\t\t\t\t\tif (requests === undefined) {\n\t\t\t\t\t\t\trequests = new Map();\n\t\t\t\t\t\t\tthis.state.submissionIdsByRequest.set(write.value.conversationId, requests);\n\t\t\t\t\t\t}\n\t\t\t\t\t\trequests.set(write.value.requestId, write.value.id);\n\t\t\t\t\t}\n\t\t\t\t\tthis.nextId = Math.max(this.nextId, write.value.id + 1);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"document.copy\":\n\t\t\t\t\tthrow new Error(\"Prepared document copy was not resolved\");\n\t\t\t\tcase \"document.create\":\n\t\t\t\tcase \"document.change\":\n\t\t\t\tcase \"document.retire\":\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tthis.applyDocumentActions(documentActions, seq);\n\t\tthis.nextSeq = seq + 1;\n\t\treturn seq;\n\t}\n\n\tasync mintId<I extends Id<string>>(): Promise<I> {\n\t\tthis.assertOpen();\n\t\tif (!Number.isSafeInteger(this.nextId)) throw new Error(\"ID space is exhausted\");\n\t\treturn idFromNumber<I>(this.nextId++);\n\t}\n\n\tasync conversation(id: ConversationId, _context: Context): Promise<ConversationRecord | undefined> {\n\t\tthis.assertOpen();\n\t\tconst value = this.state.conversations.get(id);\n\t\treturn value === undefined ? undefined : clone(value);\n\t}\n\n\tasync scanConversations(\n\t\tquery: ConversationQuery,\n\t\tlimit: number,\n\t\tcursor: Cursor | undefined,\n\t\t_context: Context,\n\t): Promise<Page<ConversationRecord, Cursor>> {\n\t\tthis.assertOpen();\n\t\tconst ids =\n\t\t\tquery.ownerTaskId !== undefined\n\t\t\t\t? (this.state.conversationIdsByOwnerTask.get(query.ownerTaskId) ?? [])\n\t\t\t\t: query.ownerConversationId !== undefined\n\t\t\t\t\t? (this.state.conversationIdsByOwnerConversation.get(query.ownerConversationId) ?? [])\n\t\t\t\t\t: this.state.conversationIds;\n\t\tconst start = scanStart(query.order, cursor, \"ascending\");\n\t\tconst values: ConversationRecord[] = [];\n\t\tfor (const index of scanIndexes(ids, start)) {\n\t\t\tif (values.length > limit) break;\n\t\t\tconst value = this.state.conversations.get(ids[index]!)!;\n\t\t\tif (query.ownerConversationId !== undefined && value.owner?.conversationId !== query.ownerConversationId) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalues.push(value);\n\t\t}\n\t\treturn page(values, limit, start.order);\n\t}\n\n\tentry(id: EntryId, context: Context): Promise<{ readonly entry: EntryRecord; readonly commitSeq: Seq } | undefined>;\n\tentry(\n\t\tconversationId: ConversationId,\n\t\tid: EntryId,\n\t\tcontext: Context,\n\t): Promise<{ readonly entry: EntryRecord; readonly commitSeq: Seq } | undefined>;\n\tasync entry(\n\t\tidOrConversationId: EntryId | ConversationId,\n\t\tidOrContext: EntryId | Context,\n\t\tcontext?: Context,\n\t): Promise<{ readonly entry: EntryRecord; readonly commitSeq: Seq } | undefined> {\n\t\tthis.assertOpen();\n\t\tif (context === undefined) {\n\t\t\tconst id = idFromNumber<EntryId>(idOrConversationId);\n\t\t\tconst entry = this.state.entries.get(id);\n\t\t\tif (entry === undefined) return undefined;\n\t\t\treturn { entry: clone(entry), commitSeq: this.state.entryCommitSeqs.get(id)! };\n\t\t}\n\t\tif (typeof idOrContext !== \"number\") throw new TypeError(\"Storage.entry() requires an entry ID\");\n\t\tconst conversationId = idFromNumber<ConversationId>(idOrConversationId);\n\t\tconst id = idFromNumber<EntryId>(idOrContext);\n\t\tconst entry = this.visibleEntries(conversationId, id, id).next().value;\n\t\tif (entry === undefined) return undefined;\n\t\treturn { entry: clone(entry), commitSeq: this.state.entryCommitSeqs.get(id)! };\n\t}\n\n\tasync findLatestHeadMarker(\n\t\tconversationId: ConversationId,\n\t\tatOrBeforeEntryId: EntryId | undefined,\n\t\t_context: Context,\n\t): Promise<(EntryRecord & { readonly head: EntryId }) | undefined> {\n\t\tthis.assertOpen();\n\t\tif (!this.state.conversations.has(conversationId)) {\n\t\t\tthrow new Error(`Unknown conversation: ${conversationId}`);\n\t\t}\n\t\tlet currentId = conversationId;\n\t\tlet upperEntryId = atOrBeforeEntryId ?? Number.POSITIVE_INFINITY;\n\t\twhile (true) {\n\t\t\tconst ids = this.state.headEntryIds.get(currentId) ?? [];\n\t\t\tconst index = upperBound(ids, upperEntryId) - 1;\n\t\t\tif (index >= 0) {\n\t\t\t\tconst entry = this.state.entries.get(ids[index])!;\n\t\t\t\treturn clone({ ...entry, head: entry.head! });\n\t\t\t}\n\t\t\tconst conversation = this.state.conversations.get(currentId)!;\n\t\t\tif (conversation.parent === undefined) return undefined;\n\t\t\tupperEntryId = Math.min(upperEntryId, conversation.parent.at);\n\t\t\tcurrentId = conversation.parent.conversationId;\n\t\t}\n\t}\n\n\tasync scanEntries(\n\t\tquery: EntryQuery,\n\t\tlimit: number,\n\t\tcursor: Cursor | undefined,\n\t\t_context: Context,\n\t): Promise<Page<EntryRecord, Cursor>> {\n\t\tthis.assertOpen();\n\t\tconst { order, after } = scanStart(query.order, cursor, \"descending\");\n\t\t// The cursor narrows the bound on the side the scan moves away from.\n\t\tlet minEntryId: number | undefined = query.minEntryId;\n\t\tlet maxEntryId: number | undefined = query.maxEntryId;\n\t\tif (after !== undefined && order === \"descending\") maxEntryId = Math.min(maxEntryId ?? after, after - 1);\n\t\tif (after !== undefined && order === \"ascending\") minEntryId = Math.max(minEntryId ?? after, after + 1);\n\t\tconst visible: EntryRecord[] = [];\n\t\tconst entries =\n\t\t\torder === \"descending\"\n\t\t\t\t? this.visibleEntries(query.conversationId, minEntryId, maxEntryId)\n\t\t\t\t: this.visibleEntriesAscending(query.conversationId, minEntryId, maxEntryId);\n\t\tfor (const entry of entries) {\n\t\t\tvisible.push(entry);\n\t\t\tif (visible.length > limit) break;\n\t\t}\n\t\treturn page(visible, limit, order);\n\t}\n\n\tasync task(id: TaskId, _context: Context): Promise<StoredTask | undefined> {\n\t\tthis.assertOpen();\n\t\tconst value = this.state.tasks.get(id);\n\t\treturn value === undefined ? undefined : clone(value);\n\t}\n\n\tasync scanTasks(\n\t\tquery: TaskQuery,\n\t\tlimit: number,\n\t\tcursor: Cursor | undefined,\n\t\t_context: Context,\n\t): Promise<Page<StoredTask, Cursor>> {\n\t\tthis.assertOpen();\n\t\tconst start = scanStart(query.order, cursor, \"ascending\");\n\t\tconst ids = query.status === undefined ? this.state.taskIds : this.state.taskIdsByStatus[query.status];\n\t\tconst values: StoredTask[] = [];\n\t\tfor (const index of scanIndexes(ids, start)) {\n\t\t\tif (values.length > limit) break;\n\t\t\tconst value = this.state.tasks.get(ids[index]!)!;\n\t\t\tif (query.conversationId !== undefined && value.conversationId !== query.conversationId) continue;\n\t\t\tif (query.kind !== undefined && value.kind !== query.kind) continue;\n\t\t\tif (query.abortRequested !== undefined && value.abortRequested !== query.abortRequested) continue;\n\t\t\tif (query.background !== undefined && value.background !== query.background) continue;\n\t\t\tvalues.push(value);\n\t\t}\n\t\treturn page(values, limit, start.order);\n\t}\n\n\tasync submission(id: SubmissionId, _context: Context): Promise<SubmissionRecord | undefined> {\n\t\tthis.assertOpen();\n\t\tconst value = this.state.submissions.get(id);\n\t\treturn value === undefined ? undefined : clone(value);\n\t}\n\n\tasync scanSubmissions(\n\t\tquery: SubmissionQuery,\n\t\tlimit: number,\n\t\tcursor: Cursor | undefined,\n\t\t_context: Context,\n\t): Promise<Page<SubmissionRecord, Cursor>> {\n\t\tthis.assertOpen();\n\t\tconst start = scanStart(query.order, cursor, \"ascending\");\n\t\tconst ids =\n\t\t\tquery.status === undefined ? this.state.submissionIds : this.state.submissionIdsByStatus[query.status];\n\t\tconst values: SubmissionRecord[] = [];\n\t\tfor (const index of scanIndexes(ids, start)) {\n\t\t\tif (values.length > limit) break;\n\t\t\tconst value = this.state.submissions.get(ids[index]!)!;\n\t\t\tif (query.conversationId !== undefined && value.conversationId !== query.conversationId) continue;\n\t\t\tvalues.push(value);\n\t\t}\n\t\treturn page(values, limit, start.order);\n\t}\n\n\tasync submissionByRequest(\n\t\tconversationId: ConversationId,\n\t\trequestId: string,\n\t\t_context: Context,\n\t): Promise<SubmissionRecord | undefined> {\n\t\tthis.assertOpen();\n\t\tconst id = this.state.submissionIdsByRequest.get(conversationId)?.get(requestId);\n\t\tif (id === undefined) return undefined;\n\t\treturn clone(this.state.submissions.get(id)!);\n\t}\n\n\tasync findDocument(\n\t\taddress: DocumentAddress,\n\t\tat: DocumentPoint,\n\t\t_context: Context,\n\t): Promise<DocumentRecord | undefined> {\n\t\tthis.assertOpen();\n\t\tconst index = this.state.documentAddresses.get(addressKey(address));\n\t\tif (at === \"current\") {\n\t\t\treturn index?.currentId === undefined ? undefined : clone(this.state.documents.get(index.currentId)!.record);\n\t\t}\n\t\tfor (const id of index?.ids ?? []) {\n\t\t\tconst record = this.state.documents.get(id)!.record;\n\t\t\tif (isAliveAt(record, at)) return clone(record);\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tasync document(id: DocumentId, at: DocumentPoint, _context: Context): Promise<StoredDocument | undefined> {\n\t\tthis.assertOpen();\n\t\tconst stored = this.materializeDocument(id, at);\n\t\treturn stored === undefined\n\t\t\t? undefined\n\t\t\t: {\n\t\t\t\t\trecord: clone(stored.record),\n\t\t\t\t\tversion: stored.version,\n\t\t\t\t\tvalue: clone(stored.value),\n\t\t\t\t\tdeltasSinceBase: stored.deltasSinceBase,\n\t\t\t\t};\n\t}\n\n\tasync scanDocuments(\n\t\tquery: DocumentQuery,\n\t\tlimit: number,\n\t\tcursor: Cursor | undefined,\n\t\t_context: Context,\n\t): Promise<Page<DocumentRecord, Cursor>> {\n\t\tthis.assertOpen();\n\t\tconst ids = this.state.documentIdsByScope.get(scopeKey(query.scope)) ?? [];\n\t\tconst after = cursorId(cursor);\n\t\tconst start = after === undefined ? 0 : upperBound(ids, after);\n\t\tconst values: DocumentRecord[] = [];\n\t\tfor (let index = start; index < ids.length && values.length <= limit; index++) {\n\t\t\tconst record = this.state.documents.get(ids[index]!)!.record;\n\t\t\tif (query.kind !== undefined && record.kind !== query.kind) continue;\n\t\t\tif (isAliveAt(record, query.at)) values.push(record);\n\t\t}\n\t\treturn page(values, limit);\n\t}\n\n\tasync close(_context: Context): Promise<void> {\n\t\tthis.closed = true;\n\t}\n\n\tprivate materializeDocument(id: DocumentId, at: DocumentPoint): StoredDocument | undefined {\n\t\tconst stored = this.state.documents.get(id);\n\t\tif (stored === undefined) return undefined;\n\t\tif (at !== \"current\" && isCurrentOnly(stored.record)) {\n\t\t\tthrow new Error(`Document ${id} does not retain historical content`);\n\t\t}\n\t\tif (!isAliveAt(stored.record, at)) return undefined;\n\t\tconst revisions = at === \"current\" ? stored.revisions : stored.revisions.filter((revision) => revision.seq <= at);\n\t\tlet baseIndex = revisions.length - 1;\n\t\twhile (baseIndex >= 0 && revisions[baseIndex]!.kind !== \"base\") baseIndex--;\n\t\tconst base = revisions[baseIndex];\n\t\tif (base?.kind !== \"base\") throw new Error(`Document ${id} is missing a required base`);\n\t\tconst value = applyImmutableBatches(\n\t\t\tbase.value,\n\t\t\tdocumentDeltaBatches(id, base.version, revisions, baseIndex + 1),\n\t\t) as JsonObject;\n\t\treturn { record: stored.record, version: base.version, value, deltasSinceBase: revisions.length - baseIndex - 1 };\n\t}\n\n\tprivate *visibleEntries(\n\t\tconversationId: ConversationId,\n\t\tminEntryId: number = Number.NEGATIVE_INFINITY,\n\t\tmaxEntryId: number = Number.POSITIVE_INFINITY,\n\t): Generator<EntryRecord> {\n\t\tif (!this.state.conversations.has(conversationId)) {\n\t\t\tthrow new Error(`Unknown conversation: ${conversationId}`);\n\t\t}\n\t\tlet currentId = conversationId;\n\t\tlet upperEntryId = maxEntryId;\n\t\twhile (true) {\n\t\t\tconst ids = this.state.entryIds.get(currentId) ?? [];\n\t\t\tfor (let index = upperBound(ids, upperEntryId) - 1; index >= 0; index--) {\n\t\t\t\tconst id = ids[index];\n\t\t\t\tif (id < minEntryId) break;\n\t\t\t\tyield this.state.entries.get(id)!;\n\t\t\t}\n\t\t\tconst conversation = this.state.conversations.get(currentId)!;\n\t\t\tif (conversation.parent === undefined) break;\n\t\t\tupperEntryId = Math.min(upperEntryId, conversation.parent.at);\n\t\t\tif (upperEntryId < minEntryId) break;\n\t\t\tcurrentId = conversation.parent.conversationId;\n\t\t}\n\t}\n\n\t/** Visible entries oldest first: the fork chain's segments from the root conversation forward. */\n\tprivate *visibleEntriesAscending(\n\t\tconversationId: ConversationId,\n\t\tminEntryId: number = Number.NEGATIVE_INFINITY,\n\t\tmaxEntryId: number = Number.POSITIVE_INFINITY,\n\t): Generator<EntryRecord> {\n\t\tif (!this.state.conversations.has(conversationId)) {\n\t\t\tthrow new Error(`Unknown conversation: ${conversationId}`);\n\t\t}\n\t\tconst segments: { readonly conversationId: ConversationId; readonly upper: number }[] = [];\n\t\tlet currentId = conversationId;\n\t\tlet upperEntryId = maxEntryId;\n\t\twhile (true) {\n\t\t\tsegments.push({ conversationId: currentId, upper: upperEntryId });\n\t\t\tconst conversation = this.state.conversations.get(currentId)!;\n\t\t\tif (conversation.parent === undefined) break;\n\t\t\tupperEntryId = Math.min(upperEntryId, conversation.parent.at);\n\t\t\tif (upperEntryId < minEntryId) break;\n\t\t\tcurrentId = conversation.parent.conversationId;\n\t\t}\n\t\tfor (const segment of segments.reverse()) {\n\t\t\tconst ids = this.state.entryIds.get(segment.conversationId) ?? [];\n\t\t\tfor (let index = lowerBound(ids, minEntryId); index < ids.length; index++) {\n\t\t\t\tconst id = ids[index]!;\n\t\t\t\tif (id > segment.upper) break;\n\t\t\t\tyield this.state.entries.get(id)!;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate checkGlobalIds(writes: readonly StorageWrite[]): void {\n\t\tconst claimed = new Map<Id<string>, TableName>();\n\t\tfor (const write of writes) {\n\t\t\tif (write.type === \"document.change\" || write.type === \"document.retire\") continue;\n\t\t\tconst document = write.type === \"document.create\" || write.type === \"document.copy\";\n\t\t\tconst table: TableName = document ? \"document\" : write.type;\n\t\t\tconst id = document ? write.record.id : write.value.id;\n\t\t\tconst existing = tableContaining(this.state, id);\n\t\t\tconst earlier = claimed.get(id);\n\t\t\tif (table === \"conversation\" || table === \"entry\" || table === \"document\") {\n\t\t\t\tif (existing !== undefined) throw new Error(`ID ${id} already belongs to ${existing}`);\n\t\t\t\tif (earlier !== undefined) throw new Error(`ID ${id} is written more than once`);\n\t\t\t} else {\n\t\t\t\tif (existing !== undefined && existing !== table) {\n\t\t\t\t\tthrow new Error(`ID ${id} already belongs to ${existing}`);\n\t\t\t\t}\n\t\t\t\tif (earlier !== undefined && earlier !== table) throw new Error(`ID ${id} is written as two record types`);\n\t\t\t}\n\t\t\tclaimed.set(id, table);\n\t\t}\n\t}\n\n\tprivate prepareDocumentActions(writes: readonly StorageWrite[]): Map<DocumentId, DocumentAction> {\n\t\tconst actions = new Map<DocumentId, DocumentAction>();\n\t\tfor (const write of writes) {\n\t\t\tif (write.type !== \"document.create\" && write.type !== \"document.change\" && write.type !== \"document.retire\") {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst id = write.type === \"document.create\" ? write.record.id : write.id;\n\t\t\tlet action = actions.get(id);\n\t\t\tif (action === undefined) {\n\t\t\t\taction = { retire: false };\n\t\t\t\tactions.set(id, action);\n\t\t\t}\n\t\t\tswitch (write.type) {\n\t\t\t\tcase \"document.create\":\n\t\t\t\t\tif (action.create !== undefined || action.content !== undefined) {\n\t\t\t\t\t\tthrow new Error(`Document ${id} has more than one content command`);\n\t\t\t\t\t}\n\t\t\t\t\taction.create = write.record;\n\t\t\t\t\taction.content = write.content;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"document.change\":\n\t\t\t\t\tif (action.content !== undefined) throw new Error(`Document ${id} has more than one content command`);\n\t\t\t\t\taction.content = write.content;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"document.retire\":\n\t\t\t\t\tif (action.retire) throw new Error(`Document ${id} is retired more than once`);\n\t\t\t\t\taction.retire = true;\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn actions;\n\t}\n\n\tprivate checkDocumentActions(actions: ReadonlyMap<DocumentId, DocumentAction>): void {\n\t\tconst liveCounts = new Map<string, number>();\n\t\tfor (const [id, action] of actions) {\n\t\t\tconst existing = this.state.documents.get(id);\n\t\t\tif (action.create === undefined && existing === undefined) throw new Error(`Unknown document: ${id}`);\n\t\t\tif (action.create !== undefined && existing !== undefined) throw new Error(`Document ${id} already exists`);\n\t\t\tif (existing?.record.retiredAt !== undefined) throw new Error(`Document ${id} is retired`);\n\t\t\tconst previous = existing?.revisions.at(-1);\n\t\t\tif (action.content?.kind === \"delta\") {\n\t\t\t\tif (previous === undefined) throw new Error(`Document ${id} delta has no base`);\n\t\t\t\tif (previous.version !== action.content.version) {\n\t\t\t\t\tthrow new Error(`Document ${id} version transition requires a base`);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst key = action.create === undefined ? recordAddressKey(existing!.record) : recordAddressKey(action.create);\n\t\t\tconst currentId = this.state.documentAddresses.get(key)?.currentId;\n\t\t\tlet live = liveCounts.get(key);\n\t\t\tif (live === undefined) live = currentId === undefined ? 0 : 1;\n\t\t\tif (action.retire && currentId === id) live--;\n\t\t\tif (action.create !== undefined && !action.retire) live++;\n\t\t\tliveCounts.set(key, live);\n\t\t}\n\n\t\tfor (const live of liveCounts.values()) {\n\t\t\tif (live > 1) throw new Error(`Document address already has a current incarnation`);\n\t\t}\n\t}\n\n\tprivate applyDocumentActions(actions: ReadonlyMap<DocumentId, DocumentAction>, seq: Seq): void {\n\t\tfor (const [id, action] of actions) {\n\t\t\tlet stored = this.state.documents.get(id);\n\t\t\tif (action.create !== undefined) {\n\t\t\t\tconst record: DocumentRecord = {\n\t\t\t\t\t...action.create,\n\t\t\t\t\tcreatedAt: seq,\n\t\t\t\t\t...(action.retire ? { retiredAt: seq } : {}),\n\t\t\t\t};\n\t\t\t\tstored = { record, revisions: [{ ...action.content!, seq }] };\n\t\t\t\tthis.state.recordTypes.set(id, \"document\");\n\t\t\t\tthis.state.documents.set(id, stored);\n\n\t\t\t\tconst key = recordAddressKey(record);\n\t\t\t\tlet address = this.state.documentAddresses.get(key);\n\t\t\t\tif (address === undefined) {\n\t\t\t\t\taddress = { ids: [] };\n\t\t\t\t\tthis.state.documentAddresses.set(key, address);\n\t\t\t\t}\n\t\t\t\tinsertSorted(address.ids, id);\n\n\t\t\t\tlet scopeIds = this.state.documentIdsByScope.get(scopeKey(record.scope));\n\t\t\t\tif (scopeIds === undefined) {\n\t\t\t\t\tscopeIds = [];\n\t\t\t\t\tthis.state.documentIdsByScope.set(scopeKey(record.scope), scopeIds);\n\t\t\t\t}\n\t\t\t\tinsertSorted(scopeIds, id);\n\t\t\t\tthis.nextId = Math.max(this.nextId, id + 1);\n\t\t\t} else if (action.content !== undefined) {\n\t\t\t\tconst revision = { ...action.content, seq } as DocumentRevision;\n\t\t\t\tif (revision.kind === \"base\" && isCurrentOnly(stored!.record)) stored!.revisions = [revision];\n\t\t\t\telse stored!.revisions.push(revision);\n\t\t\t}\n\n\t\t\tif (action.retire && action.create === undefined) stored!.record = { ...stored!.record, retiredAt: seq };\n\t\t\tif (action.retire && isCurrentOnly(stored!.record)) stored!.revisions = [];\n\t\t\tif (action.create !== undefined || action.retire) {\n\t\t\t\tconst address = this.state.documentAddresses.get(recordAddressKey(stored!.record))!;\n\t\t\t\tif (action.retire && address.currentId === id) delete address.currentId;\n\t\t\t\tif (action.create !== undefined && !action.retire) address.currentId = id;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate assertOpen(): void {\n\t\tif (this.closed) throw new Error(\"MemoryStorage is closed\");\n\t}\n}\n"]}
1
+ {"version":3,"file":"memory.js","sourceRoot":"","sources":["../../src/storage/memory.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAW,MAAM,6BAA6B,CAAC;AAC7E,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AA+BxD,OAAO,EAAE,UAAU,EAAkB,SAAS,EAAE,MAAM,WAAW,CAAC;AAiBlE,QAAQ,CAAC,CAAC,oBAAoB,CAC7B,EAAc,EACd,OAAe,EACf,SAAsC,EACtC,KAAa;IAEb,KAAK,IAAI,KAAK,GAAG,KAAK,EAAE,KAAK,GAAG,SAAS,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC3D,MAAM,QAAQ,GAAG,SAAS,CAAC,KAAK,CAAE,CAAC;QACnC,IAAI,QAAQ,CAAC,IAAI,KAAK,OAAO,IAAI,QAAQ,CAAC,OAAO,KAAK,OAAO,EAAE,CAAC;YAC/D,MAAM,IAAI,KAAK,CAAC,YAAY,EAAE,mDAAmD,CAAC,CAAC;QACpF,CAAC;QACD,MAAM,QAAQ,CAAC,GAAG,CAAC;IACpB,CAAC;AACF,CAAC;AA6BD,MAAM,KAAK,GAAG,CAAI,KAAQ,EAAK,EAAE;IAChC,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC9D,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAM,CAAC;IACvE,MAAM,MAAM,GAAG,KA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AAG,CAAC,gBAAgB,CAAC,MAAO,CAAC,MAAM,CAAC,CAAE,CAAC;gBACpF,IAAI,MAAM,CAAC,MAAM,IAAI,OAAO,CAAC,SAAS,KAAK,EAAE;oBAAE,OAAO,OAAO,CAAC,SAAS,CAAC;gBACxE,IAAI,MAAM,CAAC,MAAM,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,MAAM;oBAAE,OAAO,CAAC,SAAS,GAAG,EAAE,CAAC;YAC3E,CAAC;QACF,CAAC;IACF,CAAC;IAEO,UAAU;QACjB,IAAI,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7D,CAAC;CACD","sourcesContent":["import type { Context, JsonValue } from \"@earendil-works/chord\";\nimport { applyImmutableBatches, type Op } from \"@earendil-works/chord/delta\";\nimport { StorageRequestError } from \"../errors.ts\";\nimport { idFromNumber, seqFromNumber } from \"../ids.ts\";\nimport type {\n\tConversationId,\n\tConversationQuery,\n\tConversationRecord,\n\tCursor,\n\tDocumentAddress,\n\tDocumentContent,\n\tDocumentCreate,\n\tDocumentId,\n\tDocumentPoint,\n\tDocumentQuery,\n\tDocumentRecord,\n\tEntryId,\n\tEntryQuery,\n\tEntryRecord,\n\tId,\n\tJsonObject,\n\tPage,\n\tScanOrder,\n\tSeq,\n\tStorage,\n\tStorageWrite,\n\tStoredDocument,\n\tSubmissionId,\n\tSubmissionQuery,\n\tSubmissionRecord,\n\tTaskId,\n\tTaskQuery,\n\tTaskRecord,\n} from \"../types.ts\";\nimport { nextCursor, type ScanStart, scanStart } from \"./scan.ts\";\n\ntype StoredTask = TaskRecord<JsonValue, JsonValue, JsonValue>;\ntype TaskStatus = StoredTask[\"state\"][\"status\"];\ntype SubmissionStatus = SubmissionRecord[\"status\"];\ntype TableName = \"conversation\" | \"entry\" | \"task\" | \"submission\" | \"document\";\ntype DocumentRevision = DocumentContent & { readonly seq: Seq };\ntype StoredDocumentState = {\n\trecord: DocumentRecord;\n\trevisions: DocumentRevision[];\n};\ntype DocumentAction = {\n\tcreate?: DocumentCreate;\n\tcontent?: DocumentContent;\n\tretire: boolean;\n};\n\nfunction* documentDeltaBatches(\n\tid: DocumentId,\n\tversion: number,\n\trevisions: readonly DocumentRevision[],\n\tstart: number,\n): Generator<readonly Op[]> {\n\tfor (let index = start; index < revisions.length; index++) {\n\t\tconst revision = revisions[index]!;\n\t\tif (revision.kind !== \"delta\" || revision.version !== version) {\n\t\t\tthrow new Error(`Document ${id} crosses a stored version boundary without a base`);\n\t\t}\n\t\tyield revision.ops;\n\t}\n}\n\ntype DocumentAddressIndex = {\n\tids: DocumentId[];\n\tcurrentId?: DocumentId;\n};\n\ntype State = {\n\trecordTypes: Map<Id<string>, TableName>;\n\tconversations: Map<ConversationId, ConversationRecord>;\n\tconversationIds: ConversationId[];\n\tconversationIdsByOwnerConversation: Map<ConversationId, ConversationId[]>;\n\tconversationIdsByOwnerTask: Map<TaskId, ConversationId[]>;\n\tentries: Map<EntryId, EntryRecord>;\n\tentryIds: Map<ConversationId, EntryId[]>;\n\theadEntryIds: Map<ConversationId, EntryId[]>;\n\tentryCommitSeqs: Map<EntryId, Seq>;\n\ttasks: Map<TaskId, StoredTask>;\n\ttaskIds: TaskId[];\n\ttaskIdsByStatus: Record<TaskStatus, TaskId[]>;\n\tsubmissions: Map<SubmissionId, SubmissionRecord>;\n\tsubmissionIds: SubmissionId[];\n\tsubmissionIdsByStatus: Record<SubmissionStatus, SubmissionId[]>;\n\tsubmissionIdsByRequest: Map<ConversationId, Map<string, SubmissionId>>;\n\tdocuments: Map<DocumentId, StoredDocumentState>;\n\tdocumentAddresses: Map<string, DocumentAddressIndex>;\n\tdocumentIdsByScope: Map<string, DocumentId[]>;\n};\n\nconst clone = <T>(value: T): T => {\n\tif (value === null || typeof value !== \"object\") return value;\n\tif (Array.isArray(value)) return value.map((item) => clone(item)) as T;\n\tconst source = value as Record<string, unknown>;\n\tconst nullPrototype = Object.getPrototypeOf(value) === null;\n\tconst result = (nullPrototype ? Object.create(null) : {}) as Record<string, unknown>;\n\tfor (const key of Object.keys(source)) {\n\t\tconst copied = clone(source[key]);\n\t\tif (!nullPrototype && key in result) {\n\t\t\tObject.defineProperty(result, key, {\n\t\t\t\tvalue: copied,\n\t\t\t\twritable: true,\n\t\t\t\tenumerable: true,\n\t\t\t\tconfigurable: true,\n\t\t\t});\n\t\t} else {\n\t\t\tresult[key] = copied;\n\t\t}\n\t}\n\treturn result as T;\n};\n\nconst freeze = <T>(value: T): T => {\n\tif (value === null || typeof value !== \"object\" || Object.isFrozen(value)) return value;\n\tfor (const child of Object.values(value)) freeze(child);\n\treturn Object.freeze(value);\n};\n\nconst cursorId = <I extends Id<string>>(cursor: Readonly<Record<string, JsonValue>> | undefined): I | undefined => {\n\tconst after = cursor?.after;\n\tif (after === undefined) return undefined;\n\tif (typeof after !== \"number\" || !Number.isSafeInteger(after))\n\t\tthrow new StorageRequestError(\"Invalid storage cursor\");\n\treturn idFromNumber<I>(after);\n};\n\nconst lowerBound = <I extends Id<string>>(ids: readonly I[], target: number): number => {\n\tlet low = 0;\n\tlet high = ids.length;\n\twhile (low < high) {\n\t\tconst middle = (low + high) >>> 1;\n\t\tif (ids[middle] < target) low = middle + 1;\n\t\telse high = middle;\n\t}\n\treturn low;\n};\n\nconst upperBound = <I extends Id<string>>(ids: readonly I[], target: number): number => {\n\tlet low = 0;\n\tlet high = ids.length;\n\twhile (low < high) {\n\t\tconst middle = (low + high) >>> 1;\n\t\tif (ids[middle] <= target) low = middle + 1;\n\t\telse high = middle;\n\t}\n\treturn low;\n};\n\nconst insertSorted = <I extends Id<string>>(ids: I[], id: I): void => {\n\tif (ids.length === 0 || ids[ids.length - 1] < id) ids.push(id);\n\telse ids.splice(lowerBound(ids, id), 0, id);\n};\n\nconst removeSorted = <I extends Id<string>>(ids: I[], id: I): void => {\n\tconst index = lowerBound(ids, id);\n\tif (ids[index] === id) ids.splice(index, 1);\n};\n\nconst insertMapId = <K, I extends Id<string>>(index: Map<K, I[]>, key: K, id: I): void => {\n\tlet ids = index.get(key);\n\tif (ids === undefined) {\n\t\tids = [];\n\t\tindex.set(key, ids);\n\t}\n\tinsertSorted(ids, id);\n};\n\nconst scopeKey = (scope: DocumentRecord[\"scope\"]): string => {\n\tswitch (scope.kind) {\n\t\tcase \"session\":\n\t\t\treturn JSON.stringify([\"session\"]);\n\t\tcase \"conversation\":\n\t\t\treturn JSON.stringify([\"conversation\", scope.conversationId]);\n\t\tcase \"task\":\n\t\t\treturn JSON.stringify([\"task\", scope.taskId]);\n\t}\n};\n\nconst addressKey = (address: DocumentAddress): string =>\n\tJSON.stringify([\n\t\taddress.kind,\n\t\tscopeKey(address.scope),\n\t\taddress.key === undefined ? [\"singleton\"] : [\"family\", address.key],\n\t]);\n\nconst recordAddressKey = (record: DocumentRecord | DocumentCreate): string =>\n\taddressKey({ kind: record.kind, scope: record.scope, key: record.key });\n\nconst isAliveAt = (record: DocumentRecord, at: DocumentPoint): boolean => {\n\tif (at === \"current\") return record.retiredAt === undefined;\n\treturn record.createdAt <= at && (record.retiredAt === undefined || at < record.retiredAt);\n};\n\nconst isCurrentOnly = (record: DocumentRecord): boolean =>\n\trecord.scope.kind !== \"conversation\" || record.history === \"latest\";\n\nconst tableContaining = (state: State, id: Id<string>): TableName | undefined => state.recordTypes.get(id);\n\nconst page = <T extends { readonly id: Id<string> }>(\n\tvalues: readonly T[],\n\tlimit: number,\n\torder: ScanOrder = \"ascending\",\n): Page<T, Cursor> => {\n\tconst items = values.slice(0, limit);\n\tif (values.length <= limit) return { items: clone(items) };\n\treturn { items: clone(items), next: nextCursor(items.at(-1)!.id, order) };\n};\n\n/** Indexes of sorted `ids` in scan order, after the cursor's ID when there is one. */\nfunction* scanIndexes(ids: readonly Id<string>[], start: ScanStart): Generator<number> {\n\tif (start.order === \"ascending\") {\n\t\tconst first = start.after === undefined ? 0 : upperBound(ids, start.after);\n\t\tfor (let index = first; index < ids.length; index++) yield index;\n\t} else {\n\t\tconst end = start.after === undefined ? ids.length : lowerBound(ids, start.after);\n\t\tfor (let index = end - 1; index >= 0; index--) yield index;\n\t}\n}\n\n/** A fully validated, detached state mutation whose application performs no fallible preparation. */\nexport interface PreparedMemoryCommit {\n\treadonly seq: Seq;\n\t/** Deeply frozen detached writes for persistence. */\n\treadonly writes: readonly StorageWrite[];\n\tapply(): Seq;\n}\n\n/**\n * Detached in-memory reference implementation of `Storage`.\n *\n * Reads and retained writes are cloned intentionally to match the ownership boundary\n * of serialization-backed stores. This is backend conformance, not validation.\n */\nexport class MemoryStorage implements Storage {\n\tprivate readonly state: State = {\n\t\trecordTypes: new Map(),\n\t\tconversations: new Map(),\n\t\tconversationIds: [],\n\t\tconversationIdsByOwnerConversation: new Map(),\n\t\tconversationIdsByOwnerTask: new Map(),\n\t\tentries: new Map(),\n\t\tentryIds: new Map(),\n\t\theadEntryIds: new Map(),\n\t\tentryCommitSeqs: new Map(),\n\t\ttasks: new Map(),\n\t\ttaskIds: [],\n\t\ttaskIdsByStatus: { pending: [], running: [], waiting: [], completing: [], terminal: [] },\n\t\tsubmissions: new Map(),\n\t\tsubmissionIds: [],\n\t\tsubmissionIdsByStatus: { queued: [], placed: [], done: [], unanswered: [] },\n\t\tsubmissionIdsByRequest: new Map(),\n\t\tdocuments: new Map(),\n\t\tdocumentAddresses: new Map(),\n\t\tdocumentIdsByScope: new Map(),\n\t};\n\tprivate nextId = 2;\n\tprivate nextSeq = 1;\n\tprivate closed = false;\n\n\tasync commit(writes: readonly StorageWrite[], _context: Context): Promise<Seq> {\n\t\treturn this.prepareCommit(writes).apply();\n\t}\n\n\t/** Validate and detach one commit without changing observable state. */\n\tprepareCommit(writes: readonly StorageWrite[], seq: Seq = seqFromNumber(this.nextSeq)): PreparedMemoryCommit {\n\t\tthis.assertOpen();\n\t\tif (!Number.isSafeInteger(seq) || seq < this.nextSeq) {\n\t\t\tthrow new Error(`Commit sequence ${seq} does not strictly increase`);\n\t\t}\n\t\tconst detachedWrites = freeze(this.resolveDocumentCopies(writes.map((write) => clone(write))));\n\t\tthis.checkGlobalIds(detachedWrites);\n\t\tconst documentActions = this.prepareDocumentActions(detachedWrites);\n\t\tthis.checkDocumentActions(documentActions);\n\t\tlet applied = false;\n\t\treturn {\n\t\t\tseq,\n\t\t\twrites: detachedWrites,\n\t\t\tapply: () => {\n\t\t\t\tif (!applied) {\n\t\t\t\t\tapplied = true;\n\t\t\t\t\tthis.applyPreparedCommit(detachedWrites, documentActions, seq);\n\t\t\t\t}\n\t\t\t\treturn seq;\n\t\t\t},\n\t\t};\n\t}\n\n\tprivate resolveDocumentCopies(writes: StorageWrite[]): StorageWrite[] {\n\t\tif (!writes.some((write) => write.type === \"document.copy\")) return writes;\n\t\tconst changedDocumentIds = new Set<DocumentId>();\n\t\tfor (const write of writes) {\n\t\t\tif (write.type === \"document.create\" || write.type === \"document.copy\") {\n\t\t\t\tchangedDocumentIds.add(write.record.id);\n\t\t\t} else if (write.type === \"document.change\" || write.type === \"document.retire\") {\n\t\t\t\tchangedDocumentIds.add(write.id);\n\t\t\t}\n\t\t}\n\t\treturn writes.map((write) => {\n\t\t\tif (write.type !== \"document.copy\") return write;\n\t\t\ttry {\n\t\t\t\tif (changedDocumentIds.has(write.source.id)) {\n\t\t\t\t\tthrow new Error(`Fork source document ${write.source.id} is changed in the copy batch`);\n\t\t\t\t}\n\t\t\t\tconst stored = this.materializeDocument(write.source.id, write.source.at);\n\t\t\t\tif (stored === undefined) throw new Error(`Fork source document ${write.source.id} cannot be read`);\n\t\t\t\tif (\n\t\t\t\t\tstored.record.scope.kind !== \"conversation\" ||\n\t\t\t\t\twrite.record.scope.kind !== \"conversation\" ||\n\t\t\t\t\tstored.record.kind !== write.record.kind ||\n\t\t\t\t\tstored.record.key !== write.record.key ||\n\t\t\t\t\tstored.record.history !== write.record.history ||\n\t\t\t\t\tstored.record.fork !== write.record.fork\n\t\t\t\t) {\n\t\t\t\t\tthrow new Error(`Fork source document ${write.source.id} does not match the copied record`);\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"document.create\",\n\t\t\t\t\trecord: write.record,\n\t\t\t\t\tcontent: { kind: \"base\", version: stored.version, value: stored.value },\n\t\t\t\t};\n\t\t\t} catch (error) {\n\t\t\t\tthrow new Error(`Document copy ${write.record.id} was rejected`, { cause: error });\n\t\t\t}\n\t\t});\n\t}\n\n\tprivate applyPreparedCommit(\n\t\tprepared: readonly StorageWrite[],\n\t\tdocumentActions: ReadonlyMap<DocumentId, DocumentAction>,\n\t\tseq: Seq,\n\t): Seq {\n\t\tfor (const write of prepared) {\n\t\t\tswitch (write.type) {\n\t\t\t\tcase \"conversation\": {\n\t\t\t\t\tthis.state.recordTypes.set(write.value.id, \"conversation\");\n\t\t\t\t\tthis.state.conversations.set(write.value.id, write.value);\n\t\t\t\t\tinsertSorted(this.state.conversationIds, write.value.id);\n\t\t\t\t\tconst owner = write.value.owner;\n\t\t\t\t\tif (owner !== undefined) {\n\t\t\t\t\t\tinsertMapId(this.state.conversationIdsByOwnerConversation, owner.conversationId, write.value.id);\n\t\t\t\t\t\tinsertMapId(this.state.conversationIdsByOwnerTask, owner.taskId, write.value.id);\n\t\t\t\t\t}\n\t\t\t\t\tthis.nextId = Math.max(this.nextId, write.value.id + 1);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"entry\": {\n\t\t\t\t\tthis.state.recordTypes.set(write.value.id, \"entry\");\n\t\t\t\t\tthis.state.entries.set(write.value.id, write.value);\n\t\t\t\t\tthis.state.entryCommitSeqs.set(write.value.id, seq);\n\t\t\t\t\tlet ids = this.state.entryIds.get(write.value.conversationId);\n\t\t\t\t\tif (ids === undefined) {\n\t\t\t\t\t\tids = [];\n\t\t\t\t\t\tthis.state.entryIds.set(write.value.conversationId, ids);\n\t\t\t\t\t}\n\t\t\t\t\tinsertSorted(ids, write.value.id);\n\t\t\t\t\tif (write.value.head !== undefined) {\n\t\t\t\t\t\tlet headIds = this.state.headEntryIds.get(write.value.conversationId);\n\t\t\t\t\t\tif (headIds === undefined) {\n\t\t\t\t\t\t\theadIds = [];\n\t\t\t\t\t\t\tthis.state.headEntryIds.set(write.value.conversationId, headIds);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tinsertSorted(headIds, write.value.id);\n\t\t\t\t\t}\n\t\t\t\t\tthis.nextId = Math.max(this.nextId, write.value.id + 1);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"task\": {\n\t\t\t\t\tthis.state.recordTypes.set(write.value.id, \"task\");\n\t\t\t\t\tconst previous = this.state.tasks.get(write.value.id);\n\t\t\t\t\tif (previous === undefined) {\n\t\t\t\t\t\tinsertSorted(this.state.taskIds, write.value.id);\n\t\t\t\t\t\tinsertSorted(this.state.taskIdsByStatus[write.value.state.status], write.value.id);\n\t\t\t\t\t} else if (previous.state.status !== write.value.state.status) {\n\t\t\t\t\t\tremoveSorted(this.state.taskIdsByStatus[previous.state.status], write.value.id);\n\t\t\t\t\t\tinsertSorted(this.state.taskIdsByStatus[write.value.state.status], write.value.id);\n\t\t\t\t\t}\n\t\t\t\t\tthis.state.tasks.set(write.value.id, write.value);\n\t\t\t\t\tthis.nextId = Math.max(this.nextId, write.value.id + 1);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"submission\": {\n\t\t\t\t\tthis.state.recordTypes.set(write.value.id, \"submission\");\n\t\t\t\t\tconst previous = this.state.submissions.get(write.value.id);\n\t\t\t\t\tif (previous === undefined) {\n\t\t\t\t\t\tinsertSorted(this.state.submissionIds, write.value.id);\n\t\t\t\t\t\tinsertSorted(this.state.submissionIdsByStatus[write.value.status], write.value.id);\n\t\t\t\t\t} else if (previous.status !== write.value.status) {\n\t\t\t\t\t\tremoveSorted(this.state.submissionIdsByStatus[previous.status], write.value.id);\n\t\t\t\t\t\tinsertSorted(this.state.submissionIdsByStatus[write.value.status], write.value.id);\n\t\t\t\t\t}\n\t\t\t\t\tif (previous?.requestId !== undefined) {\n\t\t\t\t\t\tconst previousRequests = this.state.submissionIdsByRequest.get(previous.conversationId);\n\t\t\t\t\t\tif (previousRequests?.get(previous.requestId) === write.value.id) {\n\t\t\t\t\t\t\tpreviousRequests.delete(previous.requestId);\n\t\t\t\t\t\t\tif (previousRequests.size === 0) this.state.submissionIdsByRequest.delete(previous.conversationId);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tthis.state.submissions.set(write.value.id, write.value);\n\t\t\t\t\tif (write.value.requestId !== undefined) {\n\t\t\t\t\t\tlet requests = this.state.submissionIdsByRequest.get(write.value.conversationId);\n\t\t\t\t\t\tif (requests === undefined) {\n\t\t\t\t\t\t\trequests = new Map();\n\t\t\t\t\t\t\tthis.state.submissionIdsByRequest.set(write.value.conversationId, requests);\n\t\t\t\t\t\t}\n\t\t\t\t\t\trequests.set(write.value.requestId, write.value.id);\n\t\t\t\t\t}\n\t\t\t\t\tthis.nextId = Math.max(this.nextId, write.value.id + 1);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"document.copy\":\n\t\t\t\t\tthrow new Error(\"Prepared document copy was not resolved\");\n\t\t\t\tcase \"document.create\":\n\t\t\t\tcase \"document.change\":\n\t\t\t\tcase \"document.retire\":\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tthis.applyDocumentActions(documentActions, seq);\n\t\tthis.nextSeq = seq + 1;\n\t\treturn seq;\n\t}\n\n\tasync mintId<I extends Id<string>>(): Promise<I> {\n\t\tthis.assertOpen();\n\t\tif (!Number.isSafeInteger(this.nextId)) throw new Error(\"ID space is exhausted\");\n\t\treturn idFromNumber<I>(this.nextId++);\n\t}\n\n\tasync conversation(id: ConversationId, _context: Context): Promise<ConversationRecord | undefined> {\n\t\tthis.assertOpen();\n\t\tconst value = this.state.conversations.get(id);\n\t\treturn value === undefined ? undefined : clone(value);\n\t}\n\n\tasync scanConversations(\n\t\tquery: ConversationQuery,\n\t\tlimit: number,\n\t\tcursor: Cursor | undefined,\n\t\t_context: Context,\n\t): Promise<Page<ConversationRecord, Cursor>> {\n\t\tthis.assertOpen();\n\t\tconst ids =\n\t\t\tquery.ownerTaskId !== undefined\n\t\t\t\t? (this.state.conversationIdsByOwnerTask.get(query.ownerTaskId) ?? [])\n\t\t\t\t: query.ownerConversationId !== undefined\n\t\t\t\t\t? (this.state.conversationIdsByOwnerConversation.get(query.ownerConversationId) ?? [])\n\t\t\t\t\t: this.state.conversationIds;\n\t\tconst start = scanStart(query.order, cursor, \"ascending\");\n\t\tconst values: ConversationRecord[] = [];\n\t\tfor (const index of scanIndexes(ids, start)) {\n\t\t\tif (values.length > limit) break;\n\t\t\tconst value = this.state.conversations.get(ids[index]!)!;\n\t\t\tif (query.ownerConversationId !== undefined && value.owner?.conversationId !== query.ownerConversationId) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tvalues.push(value);\n\t\t}\n\t\treturn page(values, limit, start.order);\n\t}\n\n\tentry(id: EntryId, context: Context): Promise<{ readonly entry: EntryRecord; readonly commitSeq: Seq } | undefined>;\n\tentry(\n\t\tconversationId: ConversationId,\n\t\tid: EntryId,\n\t\tcontext: Context,\n\t): Promise<{ readonly entry: EntryRecord; readonly commitSeq: Seq } | undefined>;\n\tasync entry(\n\t\tidOrConversationId: EntryId | ConversationId,\n\t\tidOrContext: EntryId | Context,\n\t\tcontext?: Context,\n\t): Promise<{ readonly entry: EntryRecord; readonly commitSeq: Seq } | undefined> {\n\t\tthis.assertOpen();\n\t\tif (context === undefined) {\n\t\t\tconst id = idFromNumber<EntryId>(idOrConversationId);\n\t\t\tconst entry = this.state.entries.get(id);\n\t\t\tif (entry === undefined) return undefined;\n\t\t\treturn { entry: clone(entry), commitSeq: this.state.entryCommitSeqs.get(id)! };\n\t\t}\n\t\tif (typeof idOrContext !== \"number\") throw new StorageRequestError(\"Storage.entry() requires an entry ID\");\n\t\tconst conversationId = idFromNumber<ConversationId>(idOrConversationId);\n\t\tconst id = idFromNumber<EntryId>(idOrContext);\n\t\tconst entry = this.visibleEntries(conversationId, id, id).next().value;\n\t\tif (entry === undefined) return undefined;\n\t\treturn { entry: clone(entry), commitSeq: this.state.entryCommitSeqs.get(id)! };\n\t}\n\n\tasync findLatestHeadMarker(\n\t\tconversationId: ConversationId,\n\t\tatOrBeforeEntryId: EntryId | undefined,\n\t\t_context: Context,\n\t): Promise<(EntryRecord & { readonly head: EntryId }) | undefined> {\n\t\tthis.assertOpen();\n\t\tif (!this.state.conversations.has(conversationId)) {\n\t\t\tthrow new StorageRequestError(`Unknown conversation: ${conversationId}`);\n\t\t}\n\t\tlet currentId = conversationId;\n\t\tlet upperEntryId = atOrBeforeEntryId ?? Number.POSITIVE_INFINITY;\n\t\twhile (true) {\n\t\t\tconst ids = this.state.headEntryIds.get(currentId) ?? [];\n\t\t\tconst index = upperBound(ids, upperEntryId) - 1;\n\t\t\tif (index >= 0) {\n\t\t\t\tconst entry = this.state.entries.get(ids[index])!;\n\t\t\t\treturn clone({ ...entry, head: entry.head! });\n\t\t\t}\n\t\t\tconst conversation = this.state.conversations.get(currentId)!;\n\t\t\tif (conversation.parent === undefined) return undefined;\n\t\t\tupperEntryId = Math.min(upperEntryId, conversation.parent.at);\n\t\t\tcurrentId = conversation.parent.conversationId;\n\t\t}\n\t}\n\n\tasync scanEntries(\n\t\tquery: EntryQuery,\n\t\tlimit: number,\n\t\tcursor: Cursor | undefined,\n\t\t_context: Context,\n\t): Promise<Page<EntryRecord, Cursor>> {\n\t\tthis.assertOpen();\n\t\tconst { order, after } = scanStart(query.order, cursor, \"descending\");\n\t\t// The cursor narrows the bound on the side the scan moves away from.\n\t\tlet minEntryId: number | undefined = query.minEntryId;\n\t\tlet maxEntryId: number | undefined = query.maxEntryId;\n\t\tif (after !== undefined && order === \"descending\") maxEntryId = Math.min(maxEntryId ?? after, after - 1);\n\t\tif (after !== undefined && order === \"ascending\") minEntryId = Math.max(minEntryId ?? after, after + 1);\n\t\tconst visible: EntryRecord[] = [];\n\t\tconst entries =\n\t\t\torder === \"descending\"\n\t\t\t\t? this.visibleEntries(query.conversationId, minEntryId, maxEntryId)\n\t\t\t\t: this.visibleEntriesAscending(query.conversationId, minEntryId, maxEntryId);\n\t\tfor (const entry of entries) {\n\t\t\tvisible.push(entry);\n\t\t\tif (visible.length > limit) break;\n\t\t}\n\t\treturn page(visible, limit, order);\n\t}\n\n\tasync task(id: TaskId, _context: Context): Promise<StoredTask | undefined> {\n\t\tthis.assertOpen();\n\t\tconst value = this.state.tasks.get(id);\n\t\treturn value === undefined ? undefined : clone(value);\n\t}\n\n\tasync scanTasks(\n\t\tquery: TaskQuery,\n\t\tlimit: number,\n\t\tcursor: Cursor | undefined,\n\t\t_context: Context,\n\t): Promise<Page<StoredTask, Cursor>> {\n\t\tthis.assertOpen();\n\t\tconst start = scanStart(query.order, cursor, \"ascending\");\n\t\tconst ids = query.status === undefined ? this.state.taskIds : this.state.taskIdsByStatus[query.status];\n\t\tconst values: StoredTask[] = [];\n\t\tfor (const index of scanIndexes(ids, start)) {\n\t\t\tif (values.length > limit) break;\n\t\t\tconst value = this.state.tasks.get(ids[index]!)!;\n\t\t\tif (query.conversationId !== undefined && value.conversationId !== query.conversationId) continue;\n\t\t\tif (query.kind !== undefined && value.kind !== query.kind) continue;\n\t\t\tif (query.abortRequested !== undefined && value.abortRequested !== query.abortRequested) continue;\n\t\t\tif (query.background !== undefined && value.background !== query.background) continue;\n\t\t\tvalues.push(value);\n\t\t}\n\t\treturn page(values, limit, start.order);\n\t}\n\n\tasync submission(id: SubmissionId, _context: Context): Promise<SubmissionRecord | undefined> {\n\t\tthis.assertOpen();\n\t\tconst value = this.state.submissions.get(id);\n\t\treturn value === undefined ? undefined : clone(value);\n\t}\n\n\tasync scanSubmissions(\n\t\tquery: SubmissionQuery,\n\t\tlimit: number,\n\t\tcursor: Cursor | undefined,\n\t\t_context: Context,\n\t): Promise<Page<SubmissionRecord, Cursor>> {\n\t\tthis.assertOpen();\n\t\tconst start = scanStart(query.order, cursor, \"ascending\");\n\t\tconst ids =\n\t\t\tquery.status === undefined ? this.state.submissionIds : this.state.submissionIdsByStatus[query.status];\n\t\tconst values: SubmissionRecord[] = [];\n\t\tfor (const index of scanIndexes(ids, start)) {\n\t\t\tif (values.length > limit) break;\n\t\t\tconst value = this.state.submissions.get(ids[index]!)!;\n\t\t\tif (query.conversationId !== undefined && value.conversationId !== query.conversationId) continue;\n\t\t\tvalues.push(value);\n\t\t}\n\t\treturn page(values, limit, start.order);\n\t}\n\n\tasync submissionByRequest(\n\t\tconversationId: ConversationId,\n\t\trequestId: string,\n\t\t_context: Context,\n\t): Promise<SubmissionRecord | undefined> {\n\t\tthis.assertOpen();\n\t\tconst id = this.state.submissionIdsByRequest.get(conversationId)?.get(requestId);\n\t\tif (id === undefined) return undefined;\n\t\treturn clone(this.state.submissions.get(id)!);\n\t}\n\n\tasync findDocument(\n\t\taddress: DocumentAddress,\n\t\tat: DocumentPoint,\n\t\t_context: Context,\n\t): Promise<DocumentRecord | undefined> {\n\t\tthis.assertOpen();\n\t\tconst index = this.state.documentAddresses.get(addressKey(address));\n\t\tif (at === \"current\") {\n\t\t\treturn index?.currentId === undefined ? undefined : clone(this.state.documents.get(index.currentId)!.record);\n\t\t}\n\t\tfor (const id of index?.ids ?? []) {\n\t\t\tconst record = this.state.documents.get(id)!.record;\n\t\t\tif (isAliveAt(record, at)) return clone(record);\n\t\t}\n\t\treturn undefined;\n\t}\n\n\tasync document(id: DocumentId, at: DocumentPoint, _context: Context): Promise<StoredDocument | undefined> {\n\t\tthis.assertOpen();\n\t\tconst stored = this.materializeDocument(id, at);\n\t\treturn stored === undefined\n\t\t\t? undefined\n\t\t\t: {\n\t\t\t\t\trecord: clone(stored.record),\n\t\t\t\t\tversion: stored.version,\n\t\t\t\t\tvalue: clone(stored.value),\n\t\t\t\t\tdeltasSinceBase: stored.deltasSinceBase,\n\t\t\t\t};\n\t}\n\n\tasync scanDocuments(\n\t\tquery: DocumentQuery,\n\t\tlimit: number,\n\t\tcursor: Cursor | undefined,\n\t\t_context: Context,\n\t): Promise<Page<DocumentRecord, Cursor>> {\n\t\tthis.assertOpen();\n\t\tconst ids = this.state.documentIdsByScope.get(scopeKey(query.scope)) ?? [];\n\t\tconst after = cursorId(cursor);\n\t\tconst start = after === undefined ? 0 : upperBound(ids, after);\n\t\tconst values: DocumentRecord[] = [];\n\t\tfor (let index = start; index < ids.length && values.length <= limit; index++) {\n\t\t\tconst record = this.state.documents.get(ids[index]!)!.record;\n\t\t\tif (query.kind !== undefined && record.kind !== query.kind) continue;\n\t\t\tif (isAliveAt(record, query.at)) values.push(record);\n\t\t}\n\t\treturn page(values, limit);\n\t}\n\n\tasync close(_context: Context): Promise<void> {\n\t\tthis.closed = true;\n\t}\n\n\tprivate materializeDocument(id: DocumentId, at: DocumentPoint): StoredDocument | undefined {\n\t\tconst stored = this.state.documents.get(id);\n\t\tif (stored === undefined) return undefined;\n\t\tif (at !== \"current\" && isCurrentOnly(stored.record)) {\n\t\t\tthrow new StorageRequestError(`Document ${id} does not retain historical content`);\n\t\t}\n\t\tif (!isAliveAt(stored.record, at)) return undefined;\n\t\tconst revisions = at === \"current\" ? stored.revisions : stored.revisions.filter((revision) => revision.seq <= at);\n\t\tlet baseIndex = revisions.length - 1;\n\t\twhile (baseIndex >= 0 && revisions[baseIndex]!.kind !== \"base\") baseIndex--;\n\t\tconst base = revisions[baseIndex];\n\t\tif (base?.kind !== \"base\") throw new Error(`Document ${id} is missing a required base`);\n\t\tconst value = applyImmutableBatches(\n\t\t\tbase.value,\n\t\t\tdocumentDeltaBatches(id, base.version, revisions, baseIndex + 1),\n\t\t) as JsonObject;\n\t\treturn { record: stored.record, version: base.version, value, deltasSinceBase: revisions.length - baseIndex - 1 };\n\t}\n\n\tprivate *visibleEntries(\n\t\tconversationId: ConversationId,\n\t\tminEntryId: number = Number.NEGATIVE_INFINITY,\n\t\tmaxEntryId: number = Number.POSITIVE_INFINITY,\n\t): Generator<EntryRecord> {\n\t\tif (!this.state.conversations.has(conversationId)) {\n\t\t\tthrow new StorageRequestError(`Unknown conversation: ${conversationId}`);\n\t\t}\n\t\tlet currentId = conversationId;\n\t\tlet upperEntryId = maxEntryId;\n\t\twhile (true) {\n\t\t\tconst ids = this.state.entryIds.get(currentId) ?? [];\n\t\t\tfor (let index = upperBound(ids, upperEntryId) - 1; index >= 0; index--) {\n\t\t\t\tconst id = ids[index];\n\t\t\t\tif (id < minEntryId) break;\n\t\t\t\tyield this.state.entries.get(id)!;\n\t\t\t}\n\t\t\tconst conversation = this.state.conversations.get(currentId)!;\n\t\t\tif (conversation.parent === undefined) break;\n\t\t\tupperEntryId = Math.min(upperEntryId, conversation.parent.at);\n\t\t\tif (upperEntryId < minEntryId) break;\n\t\t\tcurrentId = conversation.parent.conversationId;\n\t\t}\n\t}\n\n\t/** Visible entries oldest first: the fork chain's segments from the root conversation forward. */\n\tprivate *visibleEntriesAscending(\n\t\tconversationId: ConversationId,\n\t\tminEntryId: number = Number.NEGATIVE_INFINITY,\n\t\tmaxEntryId: number = Number.POSITIVE_INFINITY,\n\t): Generator<EntryRecord> {\n\t\tif (!this.state.conversations.has(conversationId)) {\n\t\t\tthrow new StorageRequestError(`Unknown conversation: ${conversationId}`);\n\t\t}\n\t\tconst segments: { readonly conversationId: ConversationId; readonly upper: number }[] = [];\n\t\tlet currentId = conversationId;\n\t\tlet upperEntryId = maxEntryId;\n\t\twhile (true) {\n\t\t\tsegments.push({ conversationId: currentId, upper: upperEntryId });\n\t\t\tconst conversation = this.state.conversations.get(currentId)!;\n\t\t\tif (conversation.parent === undefined) break;\n\t\t\tupperEntryId = Math.min(upperEntryId, conversation.parent.at);\n\t\t\tif (upperEntryId < minEntryId) break;\n\t\t\tcurrentId = conversation.parent.conversationId;\n\t\t}\n\t\tfor (const segment of segments.reverse()) {\n\t\t\tconst ids = this.state.entryIds.get(segment.conversationId) ?? [];\n\t\t\tfor (let index = lowerBound(ids, minEntryId); index < ids.length; index++) {\n\t\t\t\tconst id = ids[index]!;\n\t\t\t\tif (id > segment.upper) break;\n\t\t\t\tyield this.state.entries.get(id)!;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate checkGlobalIds(writes: readonly StorageWrite[]): void {\n\t\tconst claimed = new Map<Id<string>, TableName>();\n\t\tfor (const write of writes) {\n\t\t\tif (write.type === \"document.change\" || write.type === \"document.retire\") continue;\n\t\t\tconst document = write.type === \"document.create\" || write.type === \"document.copy\";\n\t\t\tconst table: TableName = document ? \"document\" : write.type;\n\t\t\tconst id = document ? write.record.id : write.value.id;\n\t\t\tconst existing = tableContaining(this.state, id);\n\t\t\tconst earlier = claimed.get(id);\n\t\t\tif (table === \"conversation\" || table === \"entry\" || table === \"document\") {\n\t\t\t\tif (existing !== undefined) throw new Error(`ID ${id} already belongs to ${existing}`);\n\t\t\t\tif (earlier !== undefined) throw new Error(`ID ${id} is written more than once`);\n\t\t\t} else {\n\t\t\t\tif (existing !== undefined && existing !== table) {\n\t\t\t\t\tthrow new Error(`ID ${id} already belongs to ${existing}`);\n\t\t\t\t}\n\t\t\t\tif (earlier !== undefined && earlier !== table) throw new Error(`ID ${id} is written as two record types`);\n\t\t\t}\n\t\t\tclaimed.set(id, table);\n\t\t}\n\t}\n\n\tprivate prepareDocumentActions(writes: readonly StorageWrite[]): Map<DocumentId, DocumentAction> {\n\t\tconst actions = new Map<DocumentId, DocumentAction>();\n\t\tfor (const write of writes) {\n\t\t\tif (write.type !== \"document.create\" && write.type !== \"document.change\" && write.type !== \"document.retire\") {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst id = write.type === \"document.create\" ? write.record.id : write.id;\n\t\t\tlet action = actions.get(id);\n\t\t\tif (action === undefined) {\n\t\t\t\taction = { retire: false };\n\t\t\t\tactions.set(id, action);\n\t\t\t}\n\t\t\tswitch (write.type) {\n\t\t\t\tcase \"document.create\":\n\t\t\t\t\tif (action.create !== undefined || action.content !== undefined) {\n\t\t\t\t\t\tthrow new Error(`Document ${id} has more than one content command`);\n\t\t\t\t\t}\n\t\t\t\t\taction.create = write.record;\n\t\t\t\t\taction.content = write.content;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"document.change\":\n\t\t\t\t\tif (action.content !== undefined) throw new Error(`Document ${id} has more than one content command`);\n\t\t\t\t\taction.content = write.content;\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"document.retire\":\n\t\t\t\t\tif (action.retire) throw new Error(`Document ${id} is retired more than once`);\n\t\t\t\t\taction.retire = true;\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\treturn actions;\n\t}\n\n\tprivate checkDocumentActions(actions: ReadonlyMap<DocumentId, DocumentAction>): void {\n\t\tconst liveCounts = new Map<string, number>();\n\t\tfor (const [id, action] of actions) {\n\t\t\tconst existing = this.state.documents.get(id);\n\t\t\tif (action.create === undefined && existing === undefined) throw new Error(`Unknown document: ${id}`);\n\t\t\tif (action.create !== undefined && existing !== undefined) throw new Error(`Document ${id} already exists`);\n\t\t\tif (existing?.record.retiredAt !== undefined) throw new Error(`Document ${id} is retired`);\n\t\t\tconst previous = existing?.revisions.at(-1);\n\t\t\tif (action.content?.kind === \"delta\") {\n\t\t\t\tif (previous === undefined) throw new Error(`Document ${id} delta has no base`);\n\t\t\t\tif (previous.version !== action.content.version) {\n\t\t\t\t\tthrow new Error(`Document ${id} version transition requires a base`);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tconst key = action.create === undefined ? recordAddressKey(existing!.record) : recordAddressKey(action.create);\n\t\t\tconst currentId = this.state.documentAddresses.get(key)?.currentId;\n\t\t\tlet live = liveCounts.get(key);\n\t\t\tif (live === undefined) live = currentId === undefined ? 0 : 1;\n\t\t\tif (action.retire && currentId === id) live--;\n\t\t\tif (action.create !== undefined && !action.retire) live++;\n\t\t\tliveCounts.set(key, live);\n\t\t}\n\n\t\tfor (const live of liveCounts.values()) {\n\t\t\tif (live > 1) throw new Error(`Document address already has a current incarnation`);\n\t\t}\n\t}\n\n\tprivate applyDocumentActions(actions: ReadonlyMap<DocumentId, DocumentAction>, seq: Seq): void {\n\t\tfor (const [id, action] of actions) {\n\t\t\tlet stored = this.state.documents.get(id);\n\t\t\tif (action.create !== undefined) {\n\t\t\t\tconst record: DocumentRecord = {\n\t\t\t\t\t...action.create,\n\t\t\t\t\tcreatedAt: seq,\n\t\t\t\t\t...(action.retire ? { retiredAt: seq } : {}),\n\t\t\t\t};\n\t\t\t\tstored = { record, revisions: [{ ...action.content!, seq }] };\n\t\t\t\tthis.state.recordTypes.set(id, \"document\");\n\t\t\t\tthis.state.documents.set(id, stored);\n\n\t\t\t\tconst key = recordAddressKey(record);\n\t\t\t\tlet address = this.state.documentAddresses.get(key);\n\t\t\t\tif (address === undefined) {\n\t\t\t\t\taddress = { ids: [] };\n\t\t\t\t\tthis.state.documentAddresses.set(key, address);\n\t\t\t\t}\n\t\t\t\tinsertSorted(address.ids, id);\n\n\t\t\t\tlet scopeIds = this.state.documentIdsByScope.get(scopeKey(record.scope));\n\t\t\t\tif (scopeIds === undefined) {\n\t\t\t\t\tscopeIds = [];\n\t\t\t\t\tthis.state.documentIdsByScope.set(scopeKey(record.scope), scopeIds);\n\t\t\t\t}\n\t\t\t\tinsertSorted(scopeIds, id);\n\t\t\t\tthis.nextId = Math.max(this.nextId, id + 1);\n\t\t\t} else if (action.content !== undefined) {\n\t\t\t\tconst revision = { ...action.content, seq } as DocumentRevision;\n\t\t\t\tif (revision.kind === \"base\" && isCurrentOnly(stored!.record)) stored!.revisions = [revision];\n\t\t\t\telse stored!.revisions.push(revision);\n\t\t\t}\n\n\t\t\tif (action.retire && action.create === undefined) stored!.record = { ...stored!.record, retiredAt: seq };\n\t\t\tif (action.retire && isCurrentOnly(stored!.record)) stored!.revisions = [];\n\t\t\tif (action.create !== undefined || action.retire) {\n\t\t\t\tconst address = this.state.documentAddresses.get(recordAddressKey(stored!.record))!;\n\t\t\t\tif (action.retire && address.currentId === id) delete address.currentId;\n\t\t\t\tif (action.create !== undefined && !action.retire) address.currentId = id;\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate assertOpen(): void {\n\t\tif (this.closed) throw new Error(\"MemoryStorage is closed\");\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"scan.d.ts","sourceRoot":"","sources":["../../src/storage/scan.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEjD,gGAAgG;AAChG,MAAM,MAAM,SAAS,GAAG;IACvB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;CACnC,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,SAAS,CACxB,SAAS,EAAE,SAAS,GAAG,SAAS,EAChC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,SAAS,EACrD,QAAQ,EAAE,SAAS,GACjB,SAAS,CAeX;AAED,oEAAoE;AACpE,wBAAgB,UAAU,CAAC,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,SAAS,GAAG;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAA;CAAE,CAElH"}
1
+ {"version":3,"file":"scan.d.ts","sourceRoot":"","sources":["../../src/storage/scan.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEjD,gGAAgG;AAChG,MAAM,MAAM,SAAS,GAAG;IACvB,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;CACnC,CAAC;AAEF;;;;GAIG;AACH,wBAAgB,SAAS,CACxB,SAAS,EAAE,SAAS,GAAG,SAAS,EAChC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAAG,SAAS,EACrD,QAAQ,EAAE,SAAS,GACjB,SAAS,CAgBX;AAED,oEAAoE;AACpE,wBAAgB,UAAU,CAAC,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,SAAS,GAAG;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAA;CAAE,CAElH"}
@@ -1,3 +1,4 @@
1
+ import { StorageRequestError } from "../errors.js";
1
2
  /**
2
3
  * Resolve a scan's order and position. A cursor continues in the order it was created with, whether the query repeats
3
4
  * that order or omits it; a different `order` throws. A cursor without an order, written before scans had one,
@@ -5,19 +6,19 @@
5
6
  */
6
7
  export function scanStart(requested, cursor, fallback) {
7
8
  if (requested !== undefined && requested !== "ascending" && requested !== "descending") {
8
- throw new TypeError(`Invalid scan order: ${String(requested)}`);
9
+ throw new StorageRequestError(`Invalid scan order: ${String(requested)}`);
9
10
  }
10
11
  if (cursor === undefined)
11
12
  return { order: requested ?? fallback, after: undefined };
12
13
  const { after, order: stored } = cursor;
13
14
  if (typeof after !== "number" || !Number.isSafeInteger(after))
14
- throw new TypeError("Invalid storage cursor");
15
+ throw new StorageRequestError("Invalid storage cursor");
15
16
  if (stored !== undefined && stored !== "ascending" && stored !== "descending") {
16
- throw new TypeError("Invalid storage cursor");
17
+ throw new StorageRequestError("Invalid storage cursor");
17
18
  }
18
19
  const order = stored ?? fallback;
19
20
  if (requested !== undefined && requested !== order) {
20
- throw new TypeError(`The cursor continues a ${order} scan; the query asks for ${requested}`);
21
+ throw new StorageRequestError(`The cursor continues a ${order} scan; the query asks for ${requested}`);
21
22
  }
22
23
  return { order, after };
23
24
  }
@@ -1 +1 @@
1
- {"version":3,"file":"scan.js","sourceRoot":"","sources":["../../src/storage/scan.ts"],"names":[],"mappings":"AAQA;;;;GAIG;AACH,MAAM,UAAU,SAAS,CACxB,SAAgC,EAChC,MAAqD,EACrD,QAAmB;IAEnB,IAAI,SAAS,KAAK,SAAS,IAAI,SAAS,KAAK,WAAW,IAAI,SAAS,KAAK,YAAY,EAAE,CAAC;QACxF,MAAM,IAAI,SAAS,CAAC,uBAAuB,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;IACjE,CAAC;IACD,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,EAAE,KAAK,EAAE,SAAS,IAAI,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;IACpF,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,wBAAwB,CAAC,CAAC;IAC7G,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,KAAK,WAAW,IAAI,MAAM,KAAK,YAAY,EAAE,CAAC;QAC/E,MAAM,IAAI,SAAS,CAAC,wBAAwB,CAAC,CAAC;IAC/C,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,IAAI,QAAQ,CAAC;IACjC,IAAI,SAAS,KAAK,SAAS,IAAI,SAAS,KAAK,KAAK,EAAE,CAAC;QACpD,MAAM,IAAI,SAAS,CAAC,0BAA0B,KAAK,6BAA6B,SAAS,EAAE,CAAC,CAAC;IAC9F,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AACzB,CAAC;AAED,oEAAoE;AACpE,MAAM,UAAU,UAAU,CAAC,EAAc,EAAE,KAAgB;IAC1D,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC;AAC7B,CAAC","sourcesContent":["import type { Id, ScanOrder } from \"../types.ts\";\n\n/** Where a built-in storage scan starts: its order and the last ID a previous page returned. */\nexport type ScanStart = {\n\treadonly order: ScanOrder;\n\treadonly after: number | undefined;\n};\n\n/**\n * Resolve a scan's order and position. A cursor continues in the order it was created with, whether the query repeats\n * that order or omits it; a different `order` throws. A cursor without an order, written before scans had one,\n * continues in the scan's default order.\n */\nexport function scanStart(\n\trequested: ScanOrder | undefined,\n\tcursor: Readonly<Record<string, unknown>> | undefined,\n\tfallback: ScanOrder,\n): ScanStart {\n\tif (requested !== undefined && requested !== \"ascending\" && requested !== \"descending\") {\n\t\tthrow new TypeError(`Invalid scan order: ${String(requested)}`);\n\t}\n\tif (cursor === undefined) return { order: requested ?? fallback, after: undefined };\n\tconst { after, order: stored } = cursor;\n\tif (typeof after !== \"number\" || !Number.isSafeInteger(after)) throw new TypeError(\"Invalid storage cursor\");\n\tif (stored !== undefined && stored !== \"ascending\" && stored !== \"descending\") {\n\t\tthrow new TypeError(\"Invalid storage cursor\");\n\t}\n\tconst order = stored ?? fallback;\n\tif (requested !== undefined && requested !== order) {\n\t\tthrow new TypeError(`The cursor continues a ${order} scan; the query asks for ${requested}`);\n\t}\n\treturn { order, after };\n}\n\n/** The continuation of a scan whose last returned item has `id`. */\nexport function nextCursor(id: Id<string>, order: ScanOrder): { readonly after: number; readonly order: ScanOrder } {\n\treturn { after: id, order };\n}\n"]}
1
+ {"version":3,"file":"scan.js","sourceRoot":"","sources":["../../src/storage/scan.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AASnD;;;;GAIG;AACH,MAAM,UAAU,SAAS,CACxB,SAAgC,EAChC,MAAqD,EACrD,QAAmB;IAEnB,IAAI,SAAS,KAAK,SAAS,IAAI,SAAS,KAAK,WAAW,IAAI,SAAS,KAAK,YAAY,EAAE,CAAC;QACxF,MAAM,IAAI,mBAAmB,CAAC,uBAAuB,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;IAC3E,CAAC;IACD,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,EAAE,KAAK,EAAE,SAAS,IAAI,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;IACpF,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC;QAC5D,MAAM,IAAI,mBAAmB,CAAC,wBAAwB,CAAC,CAAC;IACzD,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,KAAK,WAAW,IAAI,MAAM,KAAK,YAAY,EAAE,CAAC;QAC/E,MAAM,IAAI,mBAAmB,CAAC,wBAAwB,CAAC,CAAC;IACzD,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,IAAI,QAAQ,CAAC;IACjC,IAAI,SAAS,KAAK,SAAS,IAAI,SAAS,KAAK,KAAK,EAAE,CAAC;QACpD,MAAM,IAAI,mBAAmB,CAAC,0BAA0B,KAAK,6BAA6B,SAAS,EAAE,CAAC,CAAC;IACxG,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AACzB,CAAC;AAED,oEAAoE;AACpE,MAAM,UAAU,UAAU,CAAC,EAAc,EAAE,KAAgB;IAC1D,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC;AAC7B,CAAC","sourcesContent":["import { StorageRequestError } from \"../errors.ts\";\nimport type { Id, ScanOrder } from \"../types.ts\";\n\n/** Where a built-in storage scan starts: its order and the last ID a previous page returned. */\nexport type ScanStart = {\n\treadonly order: ScanOrder;\n\treadonly after: number | undefined;\n};\n\n/**\n * Resolve a scan's order and position. A cursor continues in the order it was created with, whether the query repeats\n * that order or omits it; a different `order` throws. A cursor without an order, written before scans had one,\n * continues in the scan's default order.\n */\nexport function scanStart(\n\trequested: ScanOrder | undefined,\n\tcursor: Readonly<Record<string, unknown>> | undefined,\n\tfallback: ScanOrder,\n): ScanStart {\n\tif (requested !== undefined && requested !== \"ascending\" && requested !== \"descending\") {\n\t\tthrow new StorageRequestError(`Invalid scan order: ${String(requested)}`);\n\t}\n\tif (cursor === undefined) return { order: requested ?? fallback, after: undefined };\n\tconst { after, order: stored } = cursor;\n\tif (typeof after !== \"number\" || !Number.isSafeInteger(after))\n\t\tthrow new StorageRequestError(\"Invalid storage cursor\");\n\tif (stored !== undefined && stored !== \"ascending\" && stored !== \"descending\") {\n\t\tthrow new StorageRequestError(\"Invalid storage cursor\");\n\t}\n\tconst order = stored ?? fallback;\n\tif (requested !== undefined && requested !== order) {\n\t\tthrow new StorageRequestError(`The cursor continues a ${order} scan; the query asks for ${requested}`);\n\t}\n\treturn { order, after };\n}\n\n/** The continuation of a scan whose last returned item has `id`. */\nexport function nextCursor(id: Id<string>, order: ScanOrder): { readonly after: number; readonly order: ScanOrder } {\n\treturn { after: id, order };\n}\n"]}
@@ -1,5 +1,5 @@
1
1
  import type { SqliteDatabase, SqliteExecutor, SqliteValue } from "./database.ts";
2
- import { SqliteStorage } from "./storage.ts";
2
+ import { SqliteStorage, type SqliteStorageOptions } from "./storage.ts";
3
3
  /** Values a Durable Object SQL binding accepts and returns. */
4
4
  type DurableObjectSqlValue = ArrayBuffer | string | number | null;
5
5
  /**
@@ -9,6 +9,13 @@ type DurableObjectSqlValue = ArrayBuffer | string | number | null;
9
9
  export type DurableObjectSqliteStorage = {
10
10
  readonly sql: {
11
11
  exec(query: string, ...bindings: DurableObjectSqlValue[]): {
12
+ next(): {
13
+ readonly done?: false;
14
+ readonly value: Record<string, DurableObjectSqlValue>;
15
+ } | {
16
+ readonly done: true;
17
+ readonly value?: undefined;
18
+ };
12
19
  toArray(): Record<string, DurableObjectSqlValue>[];
13
20
  };
14
21
  };
@@ -26,11 +33,13 @@ declare class DurableObjectSqliteExecutor implements SqliteExecutor {
26
33
  protected check(): void;
27
34
  }
28
35
  /**
29
- * `SqliteDatabase` adapter for the SQLite storage of a Cloudflare Durable Object. Integers are JavaScript numbers;
30
- * binding a `bigint` outside the safe integer range throws.
36
+ * `SqliteDatabase` adapter for the SQLite storage of a Cloudflare Durable Object. Adapters given the same storage
37
+ * object share a queue; direct calls to the underlying storage bypass it. Integers are JavaScript numbers; binding a
38
+ * `bigint` outside the safe integer range throws.
31
39
  */
32
40
  export declare class DurableObjectSqliteDatabase extends DurableObjectSqliteExecutor implements SqliteDatabase {
33
41
  private readonly queue;
42
+ constructor(storage: DurableObjectSqliteStorage);
34
43
  exec(sql: string): Promise<void>;
35
44
  run(sql: string, ...params: SqliteValue[]): Promise<void>;
36
45
  get<T extends object>(sql: string, ...params: SqliteValue[]): Promise<T | undefined>;
@@ -40,7 +49,10 @@ export declare class DurableObjectSqliteDatabase extends DurableObjectSqliteExec
40
49
  /** Waits for queued work. The Durable Object owns its storage, so nothing else closes. */
41
50
  close(): Promise<void>;
42
51
  }
43
- /** Open durable storage on a SQLite-backed Durable Object's `ctx.storage`. */
44
- export declare function openDurableObjectSqliteStorage(storage: DurableObjectSqliteStorage): Promise<SqliteStorage>;
52
+ /**
53
+ * Open durable storage on a SQLite-backed Durable Object, optionally under a table prefix. Calls given the same
54
+ * storage object share an operation queue.
55
+ */
56
+ export declare function openDurableObjectSqliteStorage(storage: DurableObjectSqliteStorage, options?: SqliteStorageOptions): Promise<SqliteStorage>;
45
57
  export {};
46
58
  //# sourceMappingURL=cloudflare.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cloudflare.d.ts","sourceRoot":"","sources":["../../../src/storage/sqlite/cloudflare.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACjF,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,+DAA+D;AAC/D,KAAK,qBAAqB,GAAG,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;AAElE;;;GAGG;AACH,MAAM,MAAM,0BAA0B,GAAG;IACxC,QAAQ,CAAC,GAAG,EAAE;QACb,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,QAAQ,EAAE,qBAAqB,EAAE,GAAG;YAAE,OAAO,IAAI,MAAM,CAA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AAC,IAAI,CAAC,CAEjE;IAEQ,GAAG,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAE5F;IAEQ,GAAG,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAElF;IAED,qGAAqG;IACrG,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,WAAW,EAAE,cAAc,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAWhF;IAED,0FAA0F;IAC1F,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAErB;CACD;AAED;;;GAGG;AACH,wBAAgB,8BAA8B,CAC7C,OAAO,EAAE,0BAA0B,EACnC,OAAO,GAAE,oBAAyB,GAChC,OAAO,CAAC,aAAa,CAAC,CAExB"}
@@ -1,13 +1,39 @@
1
1
  import { SqliteStorage } from "./storage.js";
2
- /** Runs operations one at a time in call order, so a transaction excludes every other operation. */
2
+ /** Runs synchronous operations immediately when idle and holds queued calls behind a transaction. */
3
3
  class SerialQueue {
4
4
  tail = Promise.resolve();
5
+ pending = 0;
5
6
  run(operation) {
6
- const result = this.tail.then(operation);
7
- this.tail = result.catch(() => { });
7
+ if (this.pending > 0)
8
+ return this.enqueue(operation);
9
+ try {
10
+ return Promise.resolve(operation());
11
+ }
12
+ catch (error) {
13
+ return Promise.reject(error);
14
+ }
15
+ }
16
+ hold(operation) {
17
+ return this.enqueue(operation);
18
+ }
19
+ enqueue(operation) {
20
+ this.pending++;
21
+ const result = this.tail.then(operation).finally(() => {
22
+ this.pending--;
23
+ });
24
+ this.tail = result.then(() => { }, () => { });
8
25
  return result;
9
26
  }
10
27
  }
28
+ const storageQueues = new WeakMap();
29
+ function queueFor(storage) {
30
+ let queue = storageQueues.get(storage);
31
+ if (queue === undefined) {
32
+ queue = new SerialQueue();
33
+ storageQueues.set(storage, queue);
34
+ }
35
+ return queue;
36
+ }
11
37
  /** Durable Object SQL binds numbers as doubles: a `bigint` outside the safe integer range would lose precision. */
12
38
  function bindValue(value) {
13
39
  if (value instanceof Uint8Array)
@@ -20,16 +46,14 @@ function bindValue(value) {
20
46
  return Number(value);
21
47
  }
22
48
  const bind = (params) => params.map(bindValue);
23
- /** Rows as returned, with `BLOB` columns converted to `Uint8Array` in place; no other copy is made. */
24
- function rows(rows) {
25
- for (const row of rows) {
26
- for (const key in row) {
27
- const value = row[key];
28
- if (value instanceof ArrayBuffer)
29
- row[key] = new Uint8Array(value);
30
- }
49
+ /** A row as returned, with `BLOB` columns converted to `Uint8Array` in place; no other copy is made. */
50
+ function row(row) {
51
+ for (const key in row) {
52
+ const value = row[key];
53
+ if (value instanceof ArrayBuffer)
54
+ row[key] = new Uint8Array(value);
31
55
  }
32
- return rows;
56
+ return row;
33
57
  }
34
58
  /** Executes SQL through `ctx.storage.sql`, which has no separate prepare step. */
35
59
  class DurableObjectSqliteExecutor {
@@ -47,11 +71,15 @@ class DurableObjectSqliteExecutor {
47
71
  }
48
72
  async get(sql, ...params) {
49
73
  this.check();
50
- return rows(this.storage.sql.exec(sql, ...bind(params)).toArray())[0];
74
+ const result = this.storage.sql.exec(sql, ...bind(params)).next();
75
+ return result.done ? undefined : row(result.value);
51
76
  }
52
77
  async all(sql, ...params) {
53
78
  this.check();
54
- return rows(this.storage.sql.exec(sql, ...bind(params)).toArray());
79
+ return this.storage.sql
80
+ .exec(sql, ...bind(params))
81
+ .toArray()
82
+ .map((value) => row(value));
55
83
  }
56
84
  /** Throws when this handle may no longer run SQL. */
57
85
  check() { }
@@ -65,11 +93,16 @@ class DurableObjectSqliteTransaction extends DurableObjectSqliteExecutor {
65
93
  }
66
94
  }
67
95
  /**
68
- * `SqliteDatabase` adapter for the SQLite storage of a Cloudflare Durable Object. Integers are JavaScript numbers;
69
- * binding a `bigint` outside the safe integer range throws.
96
+ * `SqliteDatabase` adapter for the SQLite storage of a Cloudflare Durable Object. Adapters given the same storage
97
+ * object share a queue; direct calls to the underlying storage bypass it. Integers are JavaScript numbers; binding a
98
+ * `bigint` outside the safe integer range throws.
70
99
  */
71
100
  export class DurableObjectSqliteDatabase extends DurableObjectSqliteExecutor {
72
- queue = new SerialQueue();
101
+ queue;
102
+ constructor(storage) {
103
+ super(storage);
104
+ this.queue = queueFor(storage);
105
+ }
73
106
  exec(sql) {
74
107
  return this.queue.run(() => super.exec(sql));
75
108
  }
@@ -84,7 +117,7 @@ export class DurableObjectSqliteDatabase extends DurableObjectSqliteExecutor {
84
117
  }
85
118
  /** `ctx.storage.transaction()` commits when the callback resolves and rolls back when it rejects. */
86
119
  transaction(callback) {
87
- return this.queue.run(() => this.storage.transaction(async () => {
120
+ return this.queue.hold(() => this.storage.transaction(async () => {
88
121
  const transaction = new DurableObjectSqliteTransaction(this.storage);
89
122
  try {
90
123
  return await callback(transaction);
@@ -99,8 +132,11 @@ export class DurableObjectSqliteDatabase extends DurableObjectSqliteExecutor {
99
132
  return this.queue.run(() => { });
100
133
  }
101
134
  }
102
- /** Open durable storage on a SQLite-backed Durable Object's `ctx.storage`. */
103
- export function openDurableObjectSqliteStorage(storage) {
104
- return SqliteStorage.open(new DurableObjectSqliteDatabase(storage));
135
+ /**
136
+ * Open durable storage on a SQLite-backed Durable Object, optionally under a table prefix. Calls given the same
137
+ * storage object share an operation queue.
138
+ */
139
+ export function openDurableObjectSqliteStorage(storage, options = {}) {
140
+ return SqliteStorage.open(new DurableObjectSqliteDatabase(storage), options);
105
141
  }
106
142
  //# sourceMappingURL=cloudflare.js.map