@elaraai/east 1.0.79 → 1.0.80

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 (276) hide show
  1. package/README.md +1 -1
  2. package/dist/src/comparison.d.ts.map +1 -1
  3. package/dist/src/comparison.js +4 -5
  4. package/dist/src/comparison.js.map +1 -1
  5. package/dist/src/compile/builtins/array.d.ts +59 -0
  6. package/dist/src/compile/builtins/array.d.ts.map +1 -0
  7. package/dist/src/compile/builtins/array.js +656 -0
  8. package/dist/src/compile/builtins/array.js.map +1 -0
  9. package/dist/src/compile/builtins/blob.d.ts +18 -0
  10. package/dist/src/compile/builtins/blob.d.ts.map +1 -0
  11. package/dist/src/compile/builtins/blob.js +150 -0
  12. package/dist/src/compile/builtins/blob.js.map +1 -0
  13. package/dist/src/compile/builtins/compare.d.ts +18 -0
  14. package/dist/src/compile/builtins/compare.d.ts.map +1 -0
  15. package/dist/src/compile/builtins/compare.js +52 -0
  16. package/dist/src/compile/builtins/compare.js.map +1 -0
  17. package/dist/src/compile/builtins/datetime.d.ts +21 -0
  18. package/dist/src/compile/builtins/datetime.d.ts.map +1 -0
  19. package/dist/src/compile/builtins/datetime.js +46 -0
  20. package/dist/src/compile/builtins/datetime.js.map +1 -0
  21. package/dist/src/compile/builtins/dict.d.ts +46 -0
  22. package/dist/src/compile/builtins/dict.d.ts.map +1 -0
  23. package/dist/src/compile/builtins/dict.js +551 -0
  24. package/dist/src/compile/builtins/dict.js.map +1 -0
  25. package/dist/src/compile/builtins/index.d.ts +9 -0
  26. package/dist/src/compile/builtins/index.d.ts.map +1 -0
  27. package/dist/src/compile/builtins/index.js +28 -0
  28. package/dist/src/compile/builtins/index.js.map +1 -0
  29. package/dist/src/compile/builtins/matrix.d.ts +31 -0
  30. package/dist/src/compile/builtins/matrix.d.ts.map +1 -0
  31. package/dist/src/compile/builtins/matrix.js +310 -0
  32. package/dist/src/compile/builtins/matrix.js.map +1 -0
  33. package/dist/src/compile/builtins/primitives.d.ts +36 -0
  34. package/dist/src/compile/builtins/primitives.d.ts.map +1 -0
  35. package/dist/src/compile/builtins/primitives.js +67 -0
  36. package/dist/src/compile/builtins/primitives.js.map +1 -0
  37. package/dist/src/compile/builtins/ref.d.ts +11 -0
  38. package/dist/src/compile/builtins/ref.d.ts.map +1 -0
  39. package/dist/src/compile/builtins/ref.js +30 -0
  40. package/dist/src/compile/builtins/ref.js.map +1 -0
  41. package/dist/src/compile/builtins/set.d.ts +41 -0
  42. package/dist/src/compile/builtins/set.d.ts.map +1 -0
  43. package/dist/src/compile/builtins/set.js +385 -0
  44. package/dist/src/compile/builtins/set.js.map +1 -0
  45. package/dist/src/compile/builtins/sparse.d.ts +19 -0
  46. package/dist/src/compile/builtins/sparse.d.ts.map +1 -0
  47. package/dist/src/compile/builtins/sparse.js +143 -0
  48. package/dist/src/compile/builtins/sparse.js.map +1 -0
  49. package/dist/src/compile/builtins/string.d.ts +34 -0
  50. package/dist/src/compile/builtins/string.d.ts.map +1 -0
  51. package/dist/src/compile/builtins/string.js +238 -0
  52. package/dist/src/compile/builtins/string.js.map +1 -0
  53. package/dist/src/compile/builtins/vector.d.ts +44 -0
  54. package/dist/src/compile/builtins/vector.d.ts.map +1 -0
  55. package/dist/src/compile/builtins/vector.js +463 -0
  56. package/dist/src/compile/builtins/vector.js.map +1 -0
  57. package/dist/src/compile/constructors.d.ts +13 -0
  58. package/dist/src/compile/constructors.d.ts.map +1 -0
  59. package/dist/src/compile/constructors.js +179 -0
  60. package/dist/src/compile/constructors.js.map +1 -0
  61. package/dist/src/compile/control.d.ts +13 -0
  62. package/dist/src/compile/control.d.ts.map +1 -0
  63. package/dist/src/compile/control.js +513 -0
  64. package/dist/src/compile/control.js.map +1 -0
  65. package/dist/src/compile/functions.d.ts +13 -0
  66. package/dist/src/compile/functions.d.ts.map +1 -0
  67. package/dist/src/compile/functions.js +492 -0
  68. package/dist/src/compile/functions.js.map +1 -0
  69. package/dist/src/compile/ir.d.ts +19 -0
  70. package/dist/src/compile/ir.d.ts.map +1 -0
  71. package/dist/src/compile/ir.js +193 -0
  72. package/dist/src/compile/ir.js.map +1 -0
  73. package/dist/src/compile/runtime.d.ts +78 -0
  74. package/dist/src/compile/runtime.d.ts.map +1 -0
  75. package/dist/src/compile/runtime.js +111 -0
  76. package/dist/src/compile/runtime.js.map +1 -0
  77. package/dist/src/compile/type_parameters.d.ts +4 -0
  78. package/dist/src/compile/type_parameters.d.ts.map +1 -0
  79. package/dist/src/compile/type_parameters.js +55 -0
  80. package/dist/src/compile/type_parameters.js.map +1 -0
  81. package/dist/src/compile/typed_arrays.d.ts +28 -0
  82. package/dist/src/compile/typed_arrays.d.ts.map +1 -0
  83. package/dist/src/compile/typed_arrays.js +77 -0
  84. package/dist/src/compile/typed_arrays.js.map +1 -0
  85. package/dist/src/compile.d.ts +6 -41
  86. package/dist/src/compile.d.ts.map +1 -1
  87. package/dist/src/compile.js +8 -4415
  88. package/dist/src/compile.js.map +1 -1
  89. package/dist/src/compile.open_beast.spec.js +5 -4
  90. package/dist/src/compile.open_beast.spec.js.map +1 -1
  91. package/dist/src/containers/sortedmap.d.ts +2 -1
  92. package/dist/src/containers/sortedmap.d.ts.map +1 -1
  93. package/dist/src/containers/sortedmap.js +4 -2
  94. package/dist/src/containers/sortedmap.js.map +1 -1
  95. package/dist/src/containers/sortedset.d.ts +2 -1
  96. package/dist/src/containers/sortedset.d.ts.map +1 -1
  97. package/dist/src/containers/sortedset.js +4 -2
  98. package/dist/src/containers/sortedset.js.map +1 -1
  99. package/dist/src/error.d.ts +15 -0
  100. package/dist/src/error.d.ts.map +1 -1
  101. package/dist/src/error.js +16 -0
  102. package/dist/src/error.js.map +1 -1
  103. package/dist/src/index.d.ts +1 -0
  104. package/dist/src/index.d.ts.map +1 -1
  105. package/dist/src/index.js +1 -0
  106. package/dist/src/index.js.map +1 -1
  107. package/dist/src/patch/index.d.ts +1 -1
  108. package/dist/src/patch/index.d.ts.map +1 -1
  109. package/dist/src/patch/index.js +1 -1
  110. package/dist/src/patch/index.js.map +1 -1
  111. package/dist/src/patch/type_of_patch.d.ts +25 -1
  112. package/dist/src/patch/type_of_patch.d.ts.map +1 -1
  113. package/dist/src/patch/type_of_patch.js +33 -10
  114. package/dist/src/patch/type_of_patch.js.map +1 -1
  115. package/dist/src/platform.spec.js +19 -1
  116. package/dist/src/platform.spec.js.map +1 -1
  117. package/dist/src/runner_protocol.d.ts +316 -0
  118. package/dist/src/runner_protocol.d.ts.map +1 -0
  119. package/dist/src/runner_protocol.js +135 -0
  120. package/dist/src/runner_protocol.js.map +1 -0
  121. package/dist/src/serialization/beast2/index.d.ts +21 -5
  122. package/dist/src/serialization/beast2/index.d.ts.map +1 -1
  123. package/dist/src/serialization/beast2/index.js +12 -2
  124. package/dist/src/serialization/beast2/index.js.map +1 -1
  125. package/dist/src/serialization/beast2/index.spec.js +84 -3
  126. package/dist/src/serialization/beast2/index.spec.js.map +1 -1
  127. package/dist/src/serialization/beast2/shared.d.ts +32 -3
  128. package/dist/src/serialization/beast2/shared.d.ts.map +1 -1
  129. package/dist/src/serialization/beast2/shared.js +135 -10
  130. package/dist/src/serialization/beast2/shared.js.map +1 -1
  131. package/dist/src/serialization/beast2/v4/container.d.ts.map +1 -1
  132. package/dist/src/serialization/beast2/v4/container.js +5 -3
  133. package/dist/src/serialization/beast2/v4/container.js.map +1 -1
  134. package/dist/src/serialization/beast2/v5/aliasing.spec.d.ts +6 -0
  135. package/dist/src/serialization/beast2/v5/aliasing.spec.d.ts.map +1 -0
  136. package/dist/src/serialization/beast2/v5/aliasing.spec.js +75 -0
  137. package/dist/src/serialization/beast2/v5/aliasing.spec.js.map +1 -0
  138. package/dist/src/serialization/beast2/v5/boundary.d.ts +176 -0
  139. package/dist/src/serialization/beast2/v5/boundary.d.ts.map +1 -0
  140. package/dist/src/serialization/beast2/v5/boundary.js +297 -0
  141. package/dist/src/serialization/beast2/v5/boundary.js.map +1 -0
  142. package/dist/src/serialization/beast2/v5/boundary.spec.d.ts +6 -0
  143. package/dist/src/serialization/beast2/v5/boundary.spec.d.ts.map +1 -0
  144. package/dist/src/serialization/beast2/v5/boundary.spec.js +479 -0
  145. package/dist/src/serialization/beast2/v5/boundary.spec.js.map +1 -0
  146. package/dist/src/serialization/beast2/v5/codec.d.ts +6 -5
  147. package/dist/src/serialization/beast2/v5/codec.d.ts.map +1 -1
  148. package/dist/src/serialization/beast2/v5/codec.js +18 -3
  149. package/dist/src/serialization/beast2/v5/codec.js.map +1 -1
  150. package/dist/src/serialization/beast2/v5/deflate.d.ts +24 -2
  151. package/dist/src/serialization/beast2/v5/deflate.d.ts.map +1 -1
  152. package/dist/src/serialization/beast2/v5/deflate.js +47 -12
  153. package/dist/src/serialization/beast2/v5/deflate.js.map +1 -1
  154. package/dist/src/serialization/beast2/v5/deflate.spec.js +15 -1
  155. package/dist/src/serialization/beast2/v5/deflate.spec.js.map +1 -1
  156. package/dist/src/serialization/beast2/v5/elements.d.ts +64 -0
  157. package/dist/src/serialization/beast2/v5/elements.d.ts.map +1 -0
  158. package/dist/src/serialization/beast2/v5/elements.js +320 -0
  159. package/dist/src/serialization/beast2/v5/elements.js.map +1 -0
  160. package/dist/src/serialization/beast2/v5/elements.spec.d.ts +6 -0
  161. package/dist/src/serialization/beast2/v5/elements.spec.d.ts.map +1 -0
  162. package/dist/src/serialization/beast2/v5/elements.spec.js +161 -0
  163. package/dist/src/serialization/beast2/v5/elements.spec.js.map +1 -0
  164. package/dist/src/serialization/beast2/v5/frame-pool.d.ts +62 -21
  165. package/dist/src/serialization/beast2/v5/frame-pool.d.ts.map +1 -1
  166. package/dist/src/serialization/beast2/v5/frame-pool.js +138 -27
  167. package/dist/src/serialization/beast2/v5/frame-pool.js.map +1 -1
  168. package/dist/src/serialization/beast2/v5/frame-pool.spec.js +198 -71
  169. package/dist/src/serialization/beast2/v5/frame-pool.spec.js.map +1 -1
  170. package/dist/src/serialization/beast2/v5/frame-worker.js +25 -15
  171. package/dist/src/serialization/beast2/v5/frame-worker.js.map +1 -1
  172. package/dist/src/serialization/beast2/v5/frames.d.ts +22 -12
  173. package/dist/src/serialization/beast2/v5/frames.d.ts.map +1 -1
  174. package/dist/src/serialization/beast2/v5/frames.js +52 -27
  175. package/dist/src/serialization/beast2/v5/frames.js.map +1 -1
  176. package/dist/src/serialization/beast2/v5/geometry.d.ts +38 -1
  177. package/dist/src/serialization/beast2/v5/geometry.d.ts.map +1 -1
  178. package/dist/src/serialization/beast2/v5/geometry.js +73 -1
  179. package/dist/src/serialization/beast2/v5/geometry.js.map +1 -1
  180. package/dist/src/serialization/beast2/v5/geometry.spec.js +34 -23
  181. package/dist/src/serialization/beast2/v5/geometry.spec.js.map +1 -1
  182. package/dist/src/serialization/beast2/v5/index.spec.js +32 -28
  183. package/dist/src/serialization/beast2/v5/index.spec.js.map +1 -1
  184. package/dist/src/serialization/beast2/v5/lazy.d.ts +42 -1
  185. package/dist/src/serialization/beast2/v5/lazy.d.ts.map +1 -1
  186. package/dist/src/serialization/beast2/v5/lazy.js +143 -28
  187. package/dist/src/serialization/beast2/v5/lazy.js.map +1 -1
  188. package/dist/src/serialization/beast2/v5/lazy.spec.js +102 -24
  189. package/dist/src/serialization/beast2/v5/lazy.spec.js.map +1 -1
  190. package/dist/src/serialization/beast2/v5/manifest-writer.d.ts +110 -0
  191. package/dist/src/serialization/beast2/v5/manifest-writer.d.ts.map +1 -0
  192. package/dist/src/serialization/beast2/v5/manifest-writer.js +118 -0
  193. package/dist/src/serialization/beast2/v5/manifest-writer.js.map +1 -0
  194. package/dist/src/serialization/beast2/v5/manifest-writer.spec.d.ts +6 -0
  195. package/dist/src/serialization/beast2/v5/manifest-writer.spec.d.ts.map +1 -0
  196. package/dist/src/serialization/beast2/v5/manifest-writer.spec.js +207 -0
  197. package/dist/src/serialization/beast2/v5/manifest-writer.spec.js.map +1 -0
  198. package/dist/src/serialization/beast2/v5/manifest.d.ts +241 -0
  199. package/dist/src/serialization/beast2/v5/manifest.d.ts.map +1 -0
  200. package/dist/src/serialization/beast2/v5/manifest.js +233 -0
  201. package/dist/src/serialization/beast2/v5/manifest.js.map +1 -0
  202. package/dist/src/serialization/beast2/v5/merge.d.ts +93 -0
  203. package/dist/src/serialization/beast2/v5/merge.d.ts.map +1 -0
  204. package/dist/src/serialization/beast2/v5/merge.js +202 -0
  205. package/dist/src/serialization/beast2/v5/merge.js.map +1 -0
  206. package/dist/src/serialization/beast2/v5/merge.spec.d.ts +6 -0
  207. package/dist/src/serialization/beast2/v5/merge.spec.d.ts.map +1 -0
  208. package/dist/src/serialization/beast2/v5/merge.spec.js +118 -0
  209. package/dist/src/serialization/beast2/v5/merge.spec.js.map +1 -0
  210. package/dist/src/serialization/beast2/v5/recut.d.ts +143 -0
  211. package/dist/src/serialization/beast2/v5/recut.d.ts.map +1 -0
  212. package/dist/src/serialization/beast2/v5/recut.js +365 -0
  213. package/dist/src/serialization/beast2/v5/recut.js.map +1 -0
  214. package/dist/src/serialization/beast2/v5/recut.spec.d.ts +6 -0
  215. package/dist/src/serialization/beast2/v5/recut.spec.d.ts.map +1 -0
  216. package/dist/src/serialization/beast2/v5/recut.spec.js +215 -0
  217. package/dist/src/serialization/beast2/v5/recut.spec.js.map +1 -0
  218. package/dist/src/serialization/beast2/v5/runs.d.ts +148 -0
  219. package/dist/src/serialization/beast2/v5/runs.d.ts.map +1 -0
  220. package/dist/src/serialization/beast2/v5/runs.js +260 -0
  221. package/dist/src/serialization/beast2/v5/runs.js.map +1 -0
  222. package/dist/src/serialization/beast2/v5/runs.spec.d.ts +6 -0
  223. package/dist/src/serialization/beast2/v5/runs.spec.d.ts.map +1 -0
  224. package/dist/src/serialization/beast2/v5/runs.spec.js +182 -0
  225. package/dist/src/serialization/beast2/v5/runs.spec.js.map +1 -0
  226. package/dist/src/serialization/beast2/v5/sha256.d.ts +19 -0
  227. package/dist/src/serialization/beast2/v5/sha256.d.ts.map +1 -0
  228. package/dist/src/serialization/beast2/v5/sha256.js +103 -0
  229. package/dist/src/serialization/beast2/v5/sha256.js.map +1 -0
  230. package/dist/src/serialization/beast2/v5/sha256.spec.d.ts +6 -0
  231. package/dist/src/serialization/beast2/v5/sha256.spec.d.ts.map +1 -0
  232. package/dist/src/serialization/beast2/v5/sha256.spec.js +42 -0
  233. package/dist/src/serialization/beast2/v5/sha256.spec.js.map +1 -0
  234. package/dist/src/serialization/beast2/v5/stream.d.ts +273 -86
  235. package/dist/src/serialization/beast2/v5/stream.d.ts.map +1 -1
  236. package/dist/src/serialization/beast2/v5/stream.js +576 -244
  237. package/dist/src/serialization/beast2/v5/stream.js.map +1 -1
  238. package/dist/src/serialization/beast2/v5/sync-reader.spec.js +24 -10
  239. package/dist/src/serialization/beast2/v5/sync-reader.spec.js.map +1 -1
  240. package/dist/src/serialization/index.d.ts +1 -1
  241. package/dist/src/serialization/index.d.ts.map +1 -1
  242. package/dist/src/serialization/index.js +1 -1
  243. package/dist/src/serialization/index.js.map +1 -1
  244. package/dist/src/serialization/json.d.ts +28 -0
  245. package/dist/src/serialization/json.d.ts.map +1 -1
  246. package/dist/src/serialization/json.js +141 -30
  247. package/dist/src/serialization/json.js.map +1 -1
  248. package/dist/src/serialization/json.spec.js +47 -2
  249. package/dist/src/serialization/json.spec.js.map +1 -1
  250. package/dist/src/serialization/json_schema.d.ts +15 -11
  251. package/dist/src/serialization/json_schema.d.ts.map +1 -1
  252. package/dist/src/serialization/json_schema.js +21 -36
  253. package/dist/src/serialization/json_schema.js.map +1 -1
  254. package/dist/src/serialization/json_schema.spec.js +26 -32
  255. package/dist/src/serialization/json_schema.spec.js.map +1 -1
  256. package/dist/src/serialization/json_schema_to_type.d.ts +10 -5
  257. package/dist/src/serialization/json_schema_to_type.d.ts.map +1 -1
  258. package/dist/src/serialization/json_schema_to_type.js +13 -6
  259. package/dist/src/serialization/json_schema_to_type.js.map +1 -1
  260. package/dist/src/serialization/json_schema_to_type.spec.js +19 -0
  261. package/dist/src/serialization/json_schema_to_type.spec.js.map +1 -1
  262. package/dist/src/string_order.d.ts +36 -0
  263. package/dist/src/string_order.d.ts.map +1 -0
  264. package/dist/src/string_order.js +64 -0
  265. package/dist/src/string_order.js.map +1 -0
  266. package/dist/src/string_order.spec.d.ts +6 -0
  267. package/dist/src/string_order.spec.d.ts.map +1 -0
  268. package/dist/src/string_order.spec.js +79 -0
  269. package/dist/src/string_order.spec.js.map +1 -0
  270. package/dist/src/type_of_type.d.ts +17 -0
  271. package/dist/src/type_of_type.d.ts.map +1 -1
  272. package/dist/src/type_of_type.js +66 -0
  273. package/dist/src/type_of_type.js.map +1 -1
  274. package/dist/src/type_of_type.to_etv.spec.js +69 -1
  275. package/dist/src/type_of_type.to_etv.spec.js.map +1 -1
  276. package/package.json +1 -1
@@ -0,0 +1,297 @@
1
+ /**
2
+ * Copyright (c) 2025 Elara AI Pty Ltd
3
+ * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
4
+ */
5
+ /**
6
+ * Where a v5 collection blob's segments end.
7
+ *
8
+ * A store that addresses segments individually needs segmentation to be a
9
+ * **pure function of the value**: that is what makes equal values produce
10
+ * equal segment sets, a one-row edit re-cut the segments around it, and two
11
+ * states diff in O(changed segments). So every root's boundary is
12
+ * *content-defined*: a segment **starts at** an element whose hash falls under
13
+ * a threshold, within minimum and maximum bounds. Nothing about the writer
14
+ * enters the decision — not the codec, not the compressed size, not how
15
+ * elements were batched — so every runtime cuts the same value at the same
16
+ * elements.
17
+ *
18
+ * The rule is also **size-aware**. Each element has a *logical size*: the
19
+ * bytes of its canonical encoding, before compression, which per-element
20
+ * aliasing makes a function of the element alone. The threshold rises with the
21
+ * open segment's average element size, so a segment holds about
22
+ * {@link SEGMENT_TARGET_COUNT} narrow elements or about
23
+ * {@link SEGMENT_TARGET_BYTES} of wide ones — a collection of 1 MiB blobs is
24
+ * not one 300 MiB segment. And it is **normalized**: the threshold is lower
25
+ * until the open segment reaches that target and higher after, so segment
26
+ * sizes gather near the target rather than spreading geometrically, which
27
+ * bounds what one page read decodes.
28
+ *
29
+ * A Set or Dict hashes each element's key (its fence bytes, what a manifest
30
+ * stores as a segment's first key); an Array, which has no key, hashes the
31
+ * element's own canonical bytes. The cut falls *before* the deciding element,
32
+ * so a keyed segment's first key is the key that decided its boundary.
33
+ *
34
+ * The constants are load-bearing wire state: a manifest records the rule id it
35
+ * was cut under, so changing a constant means a new rule id, never a silent
36
+ * re-cut.
37
+ */
38
+ import { BufferWriter, BufferReader } from "../../binary-utils.js";
39
+ import {} from "../../../type_of_type.js";
40
+ import { asTypeValue } from "./type-section.js";
41
+ import { buildV5Encoder, buildV5Decoder, createV5EncodeContext, isSegmentedRoot, } from "./codec.js";
42
+ import { buildPlatformContext } from "../shared.js";
43
+ import { SourceMap } from "../../../location.js";
44
+ /** Fewest elements (pairs for a Dict) a segment holds before the hash is
45
+ * consulted — unless it already holds {@link SEGMENT_MIN_BYTES}. The
46
+ * collection's last segment is the exception: it holds whatever is left. */
47
+ export const SEGMENT_MIN_COUNT = 256;
48
+ /** A segment's expected size in narrow elements: the cut threshold is never
49
+ * below one element in this many. */
50
+ export const SEGMENT_TARGET_COUNT = 1024;
51
+ /** Most elements a segment holds: a segment this long forces the next element
52
+ * to start a new one, whatever its hash, which bounds a segment's decode cost
53
+ * whatever the keys are. */
54
+ export const SEGMENT_MAX_COUNT = 4096;
55
+ /** Fewest logical bytes that let a segment end by hash before it holds
56
+ * {@link SEGMENT_MIN_COUNT} elements — what lets a segment of wide rows end
57
+ * after a handful of them. */
58
+ export const SEGMENT_MIN_BYTES = 64 * 1024;
59
+ /** A segment's expected size in logical bytes for wide elements: the cut
60
+ * threshold rises with the open segment's average element size so that a
61
+ * segment holds about this many bytes. */
62
+ export const SEGMENT_TARGET_BYTES = 1024 * 1024;
63
+ /** Most logical bytes a segment holds before the next element is forced to
64
+ * start a new one. An element is never split, so one wider than this is a
65
+ * segment of its own. */
66
+ export const SEGMENT_MAX_BYTES = 8 * 1024 * 1024;
67
+ /**
68
+ * The rule id Set and Dict segments are cut under, stamped into every
69
+ * manifest: the hash, the bounds, and the version of this table. A writer that
70
+ * changes any constant must change this id.
71
+ */
72
+ export const SEGMENT_RULE_KEYED = "cdc/keyed/fnv1a-fmix32/256-1024-4096/64K-1M-8M/2";
73
+ /**
74
+ * The rule id Array segments are cut under: the same test as
75
+ * {@link SEGMENT_RULE_KEYED}, over each element's canonical bytes.
76
+ */
77
+ export const SEGMENT_RULE_ARRAY = "cdc/array/fnv1a-fmix32/256-1024-4096/64K-1M-8M/2";
78
+ /** The low 32-bit word of the FNV-1a 64-bit offset basis. */
79
+ const FNV_OFFSET_LOW = 0x84222325;
80
+ /** The low 32-bit word of the FNV-1a 64-bit prime (2^40 + 0x1b3). */
81
+ const FNV_PRIME_LOW = 0x1b3;
82
+ /** `2^32 / SEGMENT_TARGET_COUNT` — the threshold for narrow elements. */
83
+ const NARROW_THRESHOLD = 2 ** 32 / SEGMENT_TARGET_COUNT;
84
+ /** `2^32 / SEGMENT_TARGET_BYTES` — the threshold per byte of average element
85
+ * size. */
86
+ const THRESHOLD_PER_BYTE = 2 ** 32 / SEGMENT_TARGET_BYTES;
87
+ /**
88
+ * The boundary hash of an element: the low 32-bit word of its FNV-1a 64-bit
89
+ * hash (`fnv1a64`), mixed by murmur3's 32-bit finalizer.
90
+ *
91
+ * @remarks
92
+ * FNV-1a's low word evolves on its own — the prime's 2^40 term never reaches
93
+ * it — so it runs in 32-bit integer arithmetic, once per element cut. Its low
94
+ * bits depend only on the low bits of each byte, so the finalizer spreads every
95
+ * input bit over the whole word before the threshold compares it.
96
+ *
97
+ * @param bytes - a key's fence bytes (Set/Dict), or an element's canonical
98
+ * bytes (Array)
99
+ * @returns the hash, an unsigned 32-bit integer
100
+ */
101
+ export function segmentBoundaryHash(bytes) {
102
+ let h = FNV_OFFSET_LOW | 0;
103
+ for (let i = 0; i < bytes.length; i++)
104
+ h = Math.imul(h ^ bytes[i], FNV_PRIME_LOW);
105
+ h ^= h >>> 16;
106
+ h = Math.imul(h, 0x85ebca6b);
107
+ h ^= h >>> 13;
108
+ h = Math.imul(h, 0xc2b2ae35);
109
+ h ^= h >>> 16;
110
+ return h >>> 0;
111
+ }
112
+ /**
113
+ * Whether an element with boundary hash `hash` starts a new segment after an
114
+ * open segment of `count` elements and `bytes` logical bytes — the hash test
115
+ * alone, without the bounds.
116
+ *
117
+ * @remarks
118
+ * The base threshold is `2^32 × max(1 / 1024, (bytes / count) / 1 MiB)`: one
119
+ * narrow element in {@link SEGMENT_TARGET_COUNT}, rising with the segment's
120
+ * average element size so wide elements cut near {@link SEGMENT_TARGET_BYTES}.
121
+ * The test takes a quarter of it until the open segment holds
122
+ * {@link SEGMENT_TARGET_COUNT} elements or {@link SEGMENT_TARGET_BYTES}, and
123
+ * four times it after, so segments gather near the target. An average of
124
+ * 1 MiB or more makes every element a boundary.
125
+ *
126
+ * @param hash - the element's {@link segmentBoundaryHash}
127
+ * @param count - elements in the open segment; at least one
128
+ * @param bytes - logical bytes in the open segment
129
+ * @returns whether the element starts a segment
130
+ */
131
+ export function isSegmentBoundary(hash, count, bytes) {
132
+ const base = Math.max(NARROW_THRESHOLD, Math.floor((bytes * THRESHOLD_PER_BYTE) / count));
133
+ const threshold = count >= SEGMENT_TARGET_COUNT || bytes >= SEGMENT_TARGET_BYTES
134
+ ? Math.min(2 ** 32, base * 4)
135
+ : Math.floor(base / 4);
136
+ return hash < threshold;
137
+ }
138
+ /**
139
+ * The cut rule for every element but a collection's first: whether the
140
+ * element starts a new segment after an open segment of `count` elements and
141
+ * `bytes` logical bytes.
142
+ *
143
+ * @remarks
144
+ * A segment at a maximum always closes; one below both minimums never does;
145
+ * otherwise the element's boundary hash decides ({@link isSegmentBoundary}).
146
+ *
147
+ * @param count - elements in the open segment; at least one
148
+ * @param bytes - logical bytes in the open segment
149
+ * @param hashInput - the bytes the rule hashes: a Set/Dict element's key
150
+ * fence bytes, an Array element's canonical bytes
151
+ * @returns whether a new segment starts at the element
152
+ */
153
+ export function startsSegmentAfter(count, bytes, hashInput) {
154
+ if (count >= SEGMENT_MAX_COUNT || bytes >= SEGMENT_MAX_BYTES)
155
+ return true;
156
+ if (count < SEGMENT_MIN_COUNT && bytes < SEGMENT_MIN_BYTES)
157
+ return false;
158
+ return isSegmentBoundary(segmentBoundaryHash(hashInput), count, bytes);
159
+ }
160
+ /**
161
+ * The boundary rule id a root type's segments are cut under.
162
+ *
163
+ * @param type - the blob's root collection type
164
+ * @returns {@link SEGMENT_RULE_KEYED} for Set/Dict roots,
165
+ * {@link SEGMENT_RULE_ARRAY} for Array roots
166
+ * @throws {TypeError} When the type is not an Array, Set or Dict type.
167
+ */
168
+ export function segmentRuleFor(type) {
169
+ const typeValue = asTypeValue(type);
170
+ if (!isSegmentedRoot(typeValue)) {
171
+ throw new TypeError(`beast2 v5: segment rules address Array, Set or Dict roots, not ${typeValue.type}`);
172
+ }
173
+ return typeValue.type === "Array" ? SEGMENT_RULE_ARRAY : SEGMENT_RULE_KEYED;
174
+ }
175
+ /**
176
+ * The key (Dict) or element (Set) type a root collection's fences hold.
177
+ *
178
+ * @param type - the blob's root collection type
179
+ * @returns the order-key type, or `null` for an Array root (which has none)
180
+ * @throws {TypeError} When the type is not an Array, Set or Dict type.
181
+ */
182
+ export function segmentKeyTypeOf(type) {
183
+ const typeValue = asTypeValue(type);
184
+ if (!isSegmentedRoot(typeValue)) {
185
+ throw new TypeError(`beast2 v5: segment rules address Array, Set or Dict roots, not ${typeValue.type}`);
186
+ }
187
+ if (typeValue.type === "Array")
188
+ return null;
189
+ return typeValue.type === "Set"
190
+ ? typeValue.value
191
+ : (typeValue.value).key;
192
+ }
193
+ /**
194
+ * Builds a curried encoder for a *bare* value of `type` — the canonical v5
195
+ * value bytes with no container, header, or index around them.
196
+ *
197
+ * @remarks
198
+ * This is the form a segment fence is stored in and the form the keyed rule
199
+ * hashes, and it is position-independent: the encode runs against a fresh
200
+ * context, so no container REF can ever fire and a key's bytes depend on the
201
+ * key alone. The bytes are not self-describing — the reader supplies the
202
+ * type, which a manifest carries. Each call returns bytes of its own, sized to
203
+ * the key.
204
+ *
205
+ * @param type - the value's type
206
+ * @returns a function encoding one value to its canonical bytes
207
+ */
208
+ export function encodeBeast2FenceFor(type) {
209
+ const encode = buildV5Encoder(asTypeValue(type));
210
+ const writer = new BufferWriter(256);
211
+ return (value) => {
212
+ // An encode that threw left its bytes behind; the next key starts clean.
213
+ if (writer.size !== 0)
214
+ writer.pop();
215
+ encode(value, writer, createV5EncodeContext(null, true));
216
+ return writer.pop().slice();
217
+ };
218
+ }
219
+ /**
220
+ * Builds a curried decoder for the bare value bytes
221
+ * {@link encodeBeast2FenceFor} produces.
222
+ *
223
+ * @param type - the value's type
224
+ * @param options - decode options (platform functions for decoded functions)
225
+ * @returns a function decoding canonical bytes to one value
226
+ * @throws {Error} When the bytes are not exactly one value of `type`.
227
+ */
228
+ export function decodeBeast2FenceFor(type, options) {
229
+ const decode = buildV5Decoder(asTypeValue(type));
230
+ return (bytes) => {
231
+ const reader = new BufferReader(bytes, 0);
232
+ const value = decode(reader, {
233
+ containers: [],
234
+ sourceMap: new SourceMap(),
235
+ frozen: options?.frozen ?? false,
236
+ ...buildPlatformContext(options),
237
+ });
238
+ if (reader.offset !== bytes.length) {
239
+ throw new Error(`beast2 v5: ${bytes.length - reader.offset} bytes after the encoded key`);
240
+ }
241
+ return value;
242
+ };
243
+ }
244
+ /**
245
+ * The running decision of where a collection's segments begin.
246
+ *
247
+ * Fed each element in canonical order — its logical size and the bytes the
248
+ * rule hashes — it answers whether that element starts a new segment. The
249
+ * answer depends only on the elements since the last boundary, so a writer
250
+ * that hands elements over one at a time and one that replays the same
251
+ * elements reach the same cuts, whatever runtime they run in.
252
+ *
253
+ * @example
254
+ * ```ts
255
+ * const cutter = new SegmentCutter();
256
+ * for (const { bytes, key } of encodedElements) {
257
+ * if (cutter.startsSegment(bytes.length, key)) flush();
258
+ * segment.push(bytes);
259
+ * }
260
+ * flush();
261
+ * ```
262
+ */
263
+ export class SegmentCutter {
264
+ count = 0;
265
+ bytes = 0;
266
+ /**
267
+ * Accounts for one element and reports whether it starts a new segment.
268
+ *
269
+ * Never true for the collection's first element, which opens segment 0.
270
+ *
271
+ * @param elementBytes - the element's logical size: its canonical encoding's
272
+ * length (a Dict pair's key and value together)
273
+ * @param hashInput - the bytes the rule hashes: a Set/Dict element's key
274
+ * fence bytes, an Array element's canonical bytes; hashed only when the
275
+ * open segment has reached its minimum
276
+ * @returns whether a new segment starts at this element
277
+ */
278
+ startsSegment(elementBytes, hashInput) {
279
+ if (this.count !== 0 && startsSegmentAfter(this.count, this.bytes, hashInput)) {
280
+ this.count = 1;
281
+ this.bytes = elementBytes;
282
+ return true;
283
+ }
284
+ this.count++;
285
+ this.bytes += elementBytes;
286
+ return false;
287
+ }
288
+ /** Elements accounted for in the open segment. */
289
+ get openCount() {
290
+ return this.count;
291
+ }
292
+ /** Logical bytes accounted for in the open segment. */
293
+ get openBytes() {
294
+ return this.bytes;
295
+ }
296
+ }
297
+ //# sourceMappingURL=boundary.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"boundary.js","sourceRoot":"","sources":["../../../../../src/serialization/beast2/v5/boundary.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,EAAsB,MAAM,0BAA0B,CAAC;AAE9D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EACL,cAAc,EACd,cAAc,EACd,qBAAqB,EACrB,eAAe,GAChB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD;;6EAE6E;AAC7E,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAErC;sCACsC;AACtC,MAAM,CAAC,MAAM,oBAAoB,GA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uD;IACvD,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;CACF"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Copyright (c) 2025 Elara AI Pty Ltd
3
+ * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
4
+ */
5
+ export {};
6
+ //# sourceMappingURL=boundary.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"boundary.spec.d.ts","sourceRoot":"","sources":["../../../../../src/serialization/beast2/v5/boundary.spec.ts"],"names":[],"mappings":"AAAA;;;GAGG"}