@smoothbricks/lmao 0.1.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 (447) hide show
  1. package/README.md +269 -0
  2. package/dist/allocator.wasm +0 -0
  3. package/dist/bun/preload.d.ts +11 -0
  4. package/dist/bun/preload.d.ts.map +1 -0
  5. package/dist/bun/preload.js +10 -0
  6. package/dist/bun/trace-preload.d.ts +8 -0
  7. package/dist/bun/trace-preload.d.ts.map +1 -0
  8. package/dist/bun/trace-preload.js +12 -0
  9. package/dist/es.d.ts +8 -0
  10. package/dist/es.d.ts.map +1 -0
  11. package/dist/es.js +9 -0
  12. package/dist/index.d.ts +41 -0
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +84 -0
  15. package/dist/lib/JsBufferStrategy.d.ts +49 -0
  16. package/dist/lib/JsBufferStrategy.d.ts.map +1 -0
  17. package/dist/lib/JsBufferStrategy.js +60 -0
  18. package/dist/lib/archive/chunkCompaction.d.ts +15 -0
  19. package/dist/lib/archive/chunkCompaction.d.ts.map +1 -0
  20. package/dist/lib/archive/chunkCompaction.js +24 -0
  21. package/dist/lib/archive/chunkEnvelope.d.ts +21 -0
  22. package/dist/lib/archive/chunkEnvelope.d.ts.map +1 -0
  23. package/dist/lib/archive/chunkEnvelope.js +55 -0
  24. package/dist/lib/archive/chunkRouting.d.ts +10 -0
  25. package/dist/lib/archive/chunkRouting.d.ts.map +1 -0
  26. package/dist/lib/archive/chunkRouting.js +47 -0
  27. package/dist/lib/archive/chunkStats.d.ts +12 -0
  28. package/dist/lib/archive/chunkStats.d.ts.map +1 -0
  29. package/dist/lib/archive/chunkStats.js +10 -0
  30. package/dist/lib/archive/index.d.ts +5 -0
  31. package/dist/lib/archive/index.d.ts.map +1 -0
  32. package/dist/lib/archive/index.js +4 -0
  33. package/dist/lib/arrow/capacityStats.d.ts +9 -0
  34. package/dist/lib/arrow/capacityStats.d.ts.map +1 -0
  35. package/dist/lib/arrow/capacityStats.js +231 -0
  36. package/dist/lib/arrow/dictionaries.d.ts +27 -0
  37. package/dist/lib/arrow/dictionaries.d.ts.map +1 -0
  38. package/dist/lib/arrow/dictionaries.js +135 -0
  39. package/dist/lib/arrow/flechette.d.ts +40 -0
  40. package/dist/lib/arrow/flechette.d.ts.map +1 -0
  41. package/dist/lib/arrow/flechette.js +67 -0
  42. package/dist/lib/arrow/utils.d.ts +38 -0
  43. package/dist/lib/arrow/utils.d.ts.map +1 -0
  44. package/dist/lib/arrow/utils.js +112 -0
  45. package/dist/lib/buffer/Nanoseconds.d.ts +109 -0
  46. package/dist/lib/buffer/Nanoseconds.d.ts.map +1 -0
  47. package/dist/lib/buffer/Nanoseconds.js +86 -0
  48. package/dist/lib/bufferStrategy.d.ts +95 -0
  49. package/dist/lib/bufferStrategy.d.ts.map +1 -0
  50. package/dist/lib/bufferStrategy.js +1 -0
  51. package/dist/lib/capacityTuning.d.ts +50 -0
  52. package/dist/lib/capacityTuning.d.ts.map +1 -0
  53. package/dist/lib/capacityTuning.js +65 -0
  54. package/dist/lib/codegen/evaluatorGenerator.d.ts +35 -0
  55. package/dist/lib/codegen/evaluatorGenerator.d.ts.map +1 -0
  56. package/dist/lib/codegen/evaluatorGenerator.js +199 -0
  57. package/dist/lib/codegen/fixedPositionWriterGenerator.d.ts +139 -0
  58. package/dist/lib/codegen/fixedPositionWriterGenerator.d.ts.map +1 -0
  59. package/dist/lib/codegen/fixedPositionWriterGenerator.js +289 -0
  60. package/dist/lib/codegen/spanLoggerGenerator.d.ts +115 -0
  61. package/dist/lib/codegen/spanLoggerGenerator.d.ts.map +1 -0
  62. package/dist/lib/codegen/spanLoggerGenerator.js +575 -0
  63. package/dist/lib/convertToArrow.d.ts +40 -0
  64. package/dist/lib/convertToArrow.d.ts.map +1 -0
  65. package/dist/lib/convertToArrow.js +1467 -0
  66. package/dist/lib/defineOpContext.d.ts +96 -0
  67. package/dist/lib/defineOpContext.d.ts.map +1 -0
  68. package/dist/lib/defineOpContext.js +227 -0
  69. package/dist/lib/errors/Blocked.d.ts +113 -0
  70. package/dist/lib/errors/Blocked.d.ts.map +1 -0
  71. package/dist/lib/errors/Blocked.js +68 -0
  72. package/dist/lib/errors/CodeError.d.ts +101 -0
  73. package/dist/lib/errors/CodeError.d.ts.map +1 -0
  74. package/dist/lib/errors/CodeError.js +42 -0
  75. package/dist/lib/errors/RetriesExhausted.d.ts +67 -0
  76. package/dist/lib/errors/RetriesExhausted.d.ts.map +1 -0
  77. package/dist/lib/errors/RetriesExhausted.js +31 -0
  78. package/dist/lib/errors/Transient.d.ts +115 -0
  79. package/dist/lib/errors/Transient.d.ts.map +1 -0
  80. package/dist/lib/errors/Transient.js +54 -0
  81. package/dist/lib/errors/index.d.ts +14 -0
  82. package/dist/lib/errors/index.d.ts.map +1 -0
  83. package/dist/lib/errors/index.js +16 -0
  84. package/dist/lib/errors/retry-policy.d.ts +60 -0
  85. package/dist/lib/errors/retry-policy.d.ts.map +1 -0
  86. package/dist/lib/errors/retry-policy.js +54 -0
  87. package/dist/lib/flushScheduler.d.ts +162 -0
  88. package/dist/lib/flushScheduler.d.ts.map +1 -0
  89. package/dist/lib/flushScheduler.js +388 -0
  90. package/dist/lib/library.d.ts +149 -0
  91. package/dist/lib/library.d.ts.map +1 -0
  92. package/dist/lib/library.js +588 -0
  93. package/dist/lib/logApi.d.ts +61 -0
  94. package/dist/lib/logApi.d.ts.map +1 -0
  95. package/dist/lib/logApi.js +12 -0
  96. package/dist/lib/logBinding.d.ts +63 -0
  97. package/dist/lib/logBinding.d.ts.map +1 -0
  98. package/dist/lib/logBinding.js +1 -0
  99. package/dist/lib/op.d.ts +105 -0
  100. package/dist/lib/op.d.ts.map +1 -0
  101. package/dist/lib/op.js +59 -0
  102. package/dist/lib/opContext/contextTypes.d.ts +88 -0
  103. package/dist/lib/opContext/contextTypes.d.ts.map +1 -0
  104. package/dist/lib/opContext/contextTypes.js +34 -0
  105. package/dist/lib/opContext/createOpGroup.d.ts +57 -0
  106. package/dist/lib/opContext/createOpGroup.d.ts.map +1 -0
  107. package/dist/lib/opContext/createOpGroup.js +341 -0
  108. package/dist/lib/opContext/defineOp.d.ts +83 -0
  109. package/dist/lib/opContext/defineOp.d.ts.map +1 -0
  110. package/dist/lib/opContext/defineOp.js +197 -0
  111. package/dist/lib/opContext/featureFlagTypes.d.ts +58 -0
  112. package/dist/lib/opContext/featureFlagTypes.d.ts.map +1 -0
  113. package/dist/lib/opContext/featureFlagTypes.js +1 -0
  114. package/dist/lib/opContext/index.d.ts +13 -0
  115. package/dist/lib/opContext/index.d.ts.map +1 -0
  116. package/dist/lib/opContext/index.js +15 -0
  117. package/dist/lib/opContext/opGroupTypes.d.ts +250 -0
  118. package/dist/lib/opContext/opGroupTypes.d.ts.map +1 -0
  119. package/dist/lib/opContext/opGroupTypes.js +31 -0
  120. package/dist/lib/opContext/opTypes.d.ts +48 -0
  121. package/dist/lib/opContext/opTypes.d.ts.map +1 -0
  122. package/dist/lib/opContext/opTypes.js +7 -0
  123. package/dist/lib/opContext/spanContextTypes.d.ts +347 -0
  124. package/dist/lib/opContext/spanContextTypes.d.ts.map +1 -0
  125. package/dist/lib/opContext/spanContextTypes.js +1 -0
  126. package/dist/lib/opContext/types.d.ts +297 -0
  127. package/dist/lib/opContext/types.d.ts.map +1 -0
  128. package/dist/lib/opContext/types.js +16 -0
  129. package/dist/lib/result.d.ts +308 -0
  130. package/dist/lib/result.d.ts.map +1 -0
  131. package/dist/lib/result.js +359 -0
  132. package/dist/lib/schema/LogSchema.d.ts +62 -0
  133. package/dist/lib/schema/LogSchema.d.ts.map +1 -0
  134. package/dist/lib/schema/LogSchema.js +134 -0
  135. package/dist/lib/schema/builder.d.ts +12 -0
  136. package/dist/lib/schema/builder.d.ts.map +1 -0
  137. package/dist/lib/schema/builder.js +169 -0
  138. package/dist/lib/schema/defineFeatureFlags.d.ts +60 -0
  139. package/dist/lib/schema/defineFeatureFlags.d.ts.map +1 -0
  140. package/dist/lib/schema/defineFeatureFlags.js +29 -0
  141. package/dist/lib/schema/defineLogSchema.d.ts +87 -0
  142. package/dist/lib/schema/defineLogSchema.d.ts.map +1 -0
  143. package/dist/lib/schema/defineLogSchema.js +171 -0
  144. package/dist/lib/schema/evaluator.d.ts +267 -0
  145. package/dist/lib/schema/evaluator.d.ts.map +1 -0
  146. package/dist/lib/schema/evaluator.js +169 -0
  147. package/dist/lib/schema/extend.d.ts +14 -0
  148. package/dist/lib/schema/extend.d.ts.map +1 -0
  149. package/dist/lib/schema/extend.js +51 -0
  150. package/dist/lib/schema/systemSchema.d.ts +112 -0
  151. package/dist/lib/schema/systemSchema.d.ts.map +1 -0
  152. package/dist/lib/schema/systemSchema.js +260 -0
  153. package/dist/lib/schema/typeGuards.d.ts +52 -0
  154. package/dist/lib/schema/typeGuards.d.ts.map +1 -0
  155. package/dist/lib/schema/typeGuards.js +127 -0
  156. package/dist/lib/schema/types.d.ts +156 -0
  157. package/dist/lib/schema/types.d.ts.map +1 -0
  158. package/dist/lib/schema/types.js +4 -0
  159. package/dist/lib/spanBuffer.d.ts +133 -0
  160. package/dist/lib/spanBuffer.d.ts.map +1 -0
  161. package/dist/lib/spanBuffer.js +442 -0
  162. package/dist/lib/spanBufferHelpers.d.ts +53 -0
  163. package/dist/lib/spanBufferHelpers.d.ts.map +1 -0
  164. package/dist/lib/spanBufferHelpers.js +65 -0
  165. package/dist/lib/spanBufferStats.d.ts +38 -0
  166. package/dist/lib/spanBufferStats.d.ts.map +1 -0
  167. package/dist/lib/spanBufferStats.js +1 -0
  168. package/dist/lib/spanContext.d.ts +141 -0
  169. package/dist/lib/spanContext.d.ts.map +1 -0
  170. package/dist/lib/spanContext.js +763 -0
  171. package/dist/lib/sqlite/index.d.ts +14 -0
  172. package/dist/lib/sqlite/index.d.ts.map +1 -0
  173. package/dist/lib/sqlite/index.js +12 -0
  174. package/dist/lib/sqlite/sqlite-async-writer.d.ts +24 -0
  175. package/dist/lib/sqlite/sqlite-async-writer.d.ts.map +1 -0
  176. package/dist/lib/sqlite/sqlite-async-writer.js +65 -0
  177. package/dist/lib/sqlite/sqlite-common.d.ts +37 -0
  178. package/dist/lib/sqlite/sqlite-common.d.ts.map +1 -0
  179. package/dist/lib/sqlite/sqlite-common.js +207 -0
  180. package/dist/lib/sqlite/sqlite-d1.d.ts +25 -0
  181. package/dist/lib/sqlite/sqlite-d1.d.ts.map +1 -0
  182. package/dist/lib/sqlite/sqlite-d1.js +67 -0
  183. package/dist/lib/sqlite/sqlite-db.d.ts +29 -0
  184. package/dist/lib/sqlite/sqlite-db.d.ts.map +1 -0
  185. package/dist/lib/sqlite/sqlite-db.js +1 -0
  186. package/dist/lib/sqlite/sqlite-node.d.ts +16 -0
  187. package/dist/lib/sqlite/sqlite-node.d.ts.map +1 -0
  188. package/dist/lib/sqlite/sqlite-node.js +68 -0
  189. package/dist/lib/sqlite/sqlite-writer.d.ts +39 -0
  190. package/dist/lib/sqlite/sqlite-writer.d.ts.map +1 -0
  191. package/dist/lib/sqlite/sqlite-writer.js +78 -0
  192. package/dist/lib/testing/bun-harness.d.ts +150 -0
  193. package/dist/lib/testing/bun-harness.d.ts.map +1 -0
  194. package/dist/lib/testing/bun-harness.js +729 -0
  195. package/dist/lib/testing/extractFacts.d.ts +73 -0
  196. package/dist/lib/testing/extractFacts.d.ts.map +1 -0
  197. package/dist/lib/testing/extractFacts.js +237 -0
  198. package/dist/lib/testing/facts.d.ts +235 -0
  199. package/dist/lib/testing/facts.d.ts.map +1 -0
  200. package/dist/lib/testing/facts.js +209 -0
  201. package/dist/lib/testing/index.d.ts +58 -0
  202. package/dist/lib/testing/index.d.ts.map +1 -0
  203. package/dist/lib/testing/index.js +74 -0
  204. package/dist/lib/testing/queryable-span.d.ts +39 -0
  205. package/dist/lib/testing/queryable-span.d.ts.map +1 -0
  206. package/dist/lib/testing/queryable-span.js +58 -0
  207. package/dist/lib/testing/span-query.d.ts +21 -0
  208. package/dist/lib/testing/span-query.d.ts.map +1 -0
  209. package/dist/lib/testing/span-query.js +47 -0
  210. package/dist/lib/testing/stdio-replay.d.ts +4 -0
  211. package/dist/lib/testing/stdio-replay.d.ts.map +1 -0
  212. package/dist/lib/testing/stdio-replay.js +42 -0
  213. package/dist/lib/testing/trace-query.d.ts +59 -0
  214. package/dist/lib/testing/trace-query.d.ts.map +1 -0
  215. package/dist/lib/testing/trace-query.js +462 -0
  216. package/dist/lib/testing/vitest-harness.d.ts +135 -0
  217. package/dist/lib/testing/vitest-harness.d.ts.map +1 -0
  218. package/dist/lib/testing/vitest-harness.js +486 -0
  219. package/dist/lib/threadId.d.ts +58 -0
  220. package/dist/lib/threadId.d.ts.map +1 -0
  221. package/dist/lib/threadId.js +123 -0
  222. package/dist/lib/traceContext.d.ts +116 -0
  223. package/dist/lib/traceContext.d.ts.map +1 -0
  224. package/dist/lib/traceContext.js +37 -0
  225. package/dist/lib/traceId.d.ts +78 -0
  226. package/dist/lib/traceId.d.ts.map +1 -0
  227. package/dist/lib/traceId.js +87 -0
  228. package/dist/lib/traceRoot.d.ts +133 -0
  229. package/dist/lib/traceRoot.d.ts.map +1 -0
  230. package/dist/lib/traceRoot.es.d.ts +75 -0
  231. package/dist/lib/traceRoot.es.d.ts.map +1 -0
  232. package/dist/lib/traceRoot.es.js +129 -0
  233. package/dist/lib/traceRoot.js +19 -0
  234. package/dist/lib/traceRoot.node.d.ts +77 -0
  235. package/dist/lib/traceRoot.node.d.ts.map +1 -0
  236. package/dist/lib/traceRoot.node.js +133 -0
  237. package/dist/lib/traceRoot.universal.d.ts +3 -0
  238. package/dist/lib/traceRoot.universal.d.ts.map +1 -0
  239. package/dist/lib/traceRoot.universal.js +17 -0
  240. package/dist/lib/tracer.d.ts +296 -0
  241. package/dist/lib/tracer.d.ts.map +1 -0
  242. package/dist/lib/tracer.js +346 -0
  243. package/dist/lib/tracers/ArrayQueueTracer.d.ts +56 -0
  244. package/dist/lib/tracers/ArrayQueueTracer.d.ts.map +1 -0
  245. package/dist/lib/tracers/ArrayQueueTracer.js +67 -0
  246. package/dist/lib/tracers/CompositeTracer.d.ts +24 -0
  247. package/dist/lib/tracers/CompositeTracer.d.ts.map +1 -0
  248. package/dist/lib/tracers/CompositeTracer.js +57 -0
  249. package/dist/lib/tracers/NoOpTracer.d.ts +58 -0
  250. package/dist/lib/tracers/NoOpTracer.d.ts.map +1 -0
  251. package/dist/lib/tracers/NoOpTracer.js +41 -0
  252. package/dist/lib/tracers/SQLiteTracer.d.ts +42 -0
  253. package/dist/lib/tracers/SQLiteTracer.d.ts.map +1 -0
  254. package/dist/lib/tracers/SQLiteTracer.js +84 -0
  255. package/dist/lib/tracers/StdioTracer.d.ts +83 -0
  256. package/dist/lib/tracers/StdioTracer.d.ts.map +1 -0
  257. package/dist/lib/tracers/StdioTracer.js +270 -0
  258. package/dist/lib/tracers/TestTracer.d.ts +95 -0
  259. package/dist/lib/tracers/TestTracer.d.ts.map +1 -0
  260. package/dist/lib/tracers/TestTracer.js +87 -0
  261. package/dist/lib/tracers/index.d.ts +12 -0
  262. package/dist/lib/tracers/index.d.ts.map +1 -0
  263. package/dist/lib/tracers/index.js +11 -0
  264. package/dist/lib/types.d.ts +366 -0
  265. package/dist/lib/types.d.ts.map +1 -0
  266. package/dist/lib/types.js +1 -0
  267. package/dist/lib/utf8Cache.d.ts +111 -0
  268. package/dist/lib/utf8Cache.d.ts.map +1 -0
  269. package/dist/lib/utf8Cache.js +177 -0
  270. package/dist/lib/wasm/WasmBufferStrategy.d.ts +88 -0
  271. package/dist/lib/wasm/WasmBufferStrategy.d.ts.map +1 -0
  272. package/dist/lib/wasm/WasmBufferStrategy.js +138 -0
  273. package/dist/lib/wasm/wasmAllocator.d.ts +109 -0
  274. package/dist/lib/wasm/wasmAllocator.d.ts.map +1 -0
  275. package/dist/lib/wasm/wasmAllocator.js +227 -0
  276. package/dist/lib/wasm/wasmSpanBuffer.d.ts +123 -0
  277. package/dist/lib/wasm/wasmSpanBuffer.d.ts.map +1 -0
  278. package/dist/lib/wasm/wasmSpanBuffer.js +772 -0
  279. package/dist/lib/wasm/wasmTraceRoot.d.ts +167 -0
  280. package/dist/lib/wasm/wasmTraceRoot.d.ts.map +1 -0
  281. package/dist/lib/wasm/wasmTraceRoot.js +264 -0
  282. package/dist/node.d.ts +7 -0
  283. package/dist/node.d.ts.map +1 -0
  284. package/dist/node.js +8 -0
  285. package/dist/testing.d.ts +18 -0
  286. package/dist/testing.d.ts.map +1 -0
  287. package/dist/testing.js +17 -0
  288. package/package.json +193 -0
  289. package/src/bun/preload.ts +11 -0
  290. package/src/bun/trace-preload.ts +14 -0
  291. package/src/es.ts +11 -0
  292. package/src/index.ts +194 -0
  293. package/src/lib/JsBufferStrategy.ts +102 -0
  294. package/src/lib/__tests__/arrow-builder-integration/arrow-conversion.test.ts +485 -0
  295. package/src/lib/__tests__/arrow-builder-integration/binary-arrow-conversion.test.ts +390 -0
  296. package/src/lib/__tests__/arrow-builder-integration/binary-comparison.test.ts +647 -0
  297. package/src/lib/__tests__/arrow-builder-integration/binary-format-compliance.test.ts +292 -0
  298. package/src/lib/__tests__/arrow-builder-integration/buffer-chaining.test.ts +354 -0
  299. package/src/lib/__tests__/arrow-builder-integration/foundation.test.ts +127 -0
  300. package/src/lib/__tests__/arrow-builder-integration/integration.test.ts +392 -0
  301. package/src/lib/__tests__/arrow-builder-integration/lazy-columns.test.ts +389 -0
  302. package/src/lib/__tests__/arrow-conversion-algorithms.test.ts +54 -0
  303. package/src/lib/__tests__/arrow-test-helpers.ts +71 -0
  304. package/src/lib/__tests__/binary-e2e.test.ts +203 -0
  305. package/src/lib/__tests__/buffer-overflow-op-api.test.ts +608 -0
  306. package/src/lib/__tests__/buffer-overflow.test.ts +612 -0
  307. package/src/lib/__tests__/capacity-tuning.test.ts +544 -0
  308. package/src/lib/__tests__/effective-schema.test.ts +439 -0
  309. package/src/lib/__tests__/flechette.test.ts +42 -0
  310. package/src/lib/__tests__/flushScheduler.test.ts +537 -0
  311. package/src/lib/__tests__/integration.test.ts +600 -0
  312. package/src/lib/__tests__/library.test.ts +934 -0
  313. package/src/lib/__tests__/nested-library-tasks.test.ts +849 -0
  314. package/src/lib/__tests__/op-context-binding.test.ts +135 -0
  315. package/src/lib/__tests__/op-metadata.test.ts +198 -0
  316. package/src/lib/__tests__/op-type-safety.test.ts +71 -0
  317. package/src/lib/__tests__/package-exports.test.ts +57 -0
  318. package/src/lib/__tests__/remapped-buffer-view-integration.test.ts +319 -0
  319. package/src/lib/__tests__/remapped-buffer-view.test.ts +635 -0
  320. package/src/lib/__tests__/scope-attributes.test.ts +348 -0
  321. package/src/lib/__tests__/span-lifecycle.test.ts +680 -0
  322. package/src/lib/__tests__/sync-async-trace.test.ts +435 -0
  323. package/src/lib/__tests__/test-helpers.ts +318 -0
  324. package/src/lib/__tests__/timestamp.test.ts +259 -0
  325. package/src/lib/__tests__/trace-archive-primitives.test.ts +98 -0
  326. package/src/lib/__tests__/traceContext-type-enforcement.test.ts +271 -0
  327. package/src/lib/__tests__/tracer.test.ts +211 -0
  328. package/src/lib/__tests__/type-narrowing.test.ts +137 -0
  329. package/src/lib/archive/chunkCompaction.ts +44 -0
  330. package/src/lib/archive/chunkEnvelope.ts +79 -0
  331. package/src/lib/archive/chunkRouting.ts +70 -0
  332. package/src/lib/archive/chunkStats.ts +25 -0
  333. package/src/lib/archive/index.ts +4 -0
  334. package/src/lib/arrow/capacityStats.ts +340 -0
  335. package/src/lib/arrow/dictionaries.ts +159 -0
  336. package/src/lib/arrow/flechette.ts +114 -0
  337. package/src/lib/arrow/utils.ts +126 -0
  338. package/src/lib/buffer/Nanoseconds.ts +131 -0
  339. package/src/lib/bufferStrategy.ts +106 -0
  340. package/src/lib/capacityTuning.ts +102 -0
  341. package/src/lib/codegen/__tests__/__snapshots__/evaluatorGenerator.test.ts.snap +160 -0
  342. package/src/lib/codegen/__tests__/__snapshots__/scopeGenerator.test.ts.snap +196 -0
  343. package/src/lib/codegen/__tests__/__snapshots__/spanLoggerGenerator.test.ts.snap +1965 -0
  344. package/src/lib/codegen/__tests__/evaluatorGenerator.test.ts +516 -0
  345. package/src/lib/codegen/__tests__/nullBitmap.test.ts +207 -0
  346. package/src/lib/codegen/__tests__/spanLoggerGenerator.test.ts +237 -0
  347. package/src/lib/codegen/evaluatorGenerator.ts +309 -0
  348. package/src/lib/codegen/fixedPositionWriterGenerator.ts +444 -0
  349. package/src/lib/codegen/spanLoggerGenerator.ts +715 -0
  350. package/src/lib/convertToArrow.ts +1805 -0
  351. package/src/lib/defineOpContext.ts +398 -0
  352. package/src/lib/errors/Blocked.ts +165 -0
  353. package/src/lib/errors/CodeError.ts +132 -0
  354. package/src/lib/errors/RetriesExhausted.ts +78 -0
  355. package/src/lib/errors/Transient.ts +168 -0
  356. package/src/lib/errors/__tests__/Blocked-config.test.ts +152 -0
  357. package/src/lib/errors/__tests__/Transient.test.ts +129 -0
  358. package/src/lib/errors/__tests__/retry-policy.test.ts +85 -0
  359. package/src/lib/errors/index.ts +24 -0
  360. package/src/lib/errors/retry-policy.ts +87 -0
  361. package/src/lib/flushScheduler.ts +509 -0
  362. package/src/lib/library.ts +682 -0
  363. package/src/lib/logApi.ts +70 -0
  364. package/src/lib/logBinding.ts +67 -0
  365. package/src/lib/op.ts +103 -0
  366. package/src/lib/opContext/__tests__/retry.test.ts +764 -0
  367. package/src/lib/opContext/contextTypes.ts +130 -0
  368. package/src/lib/opContext/createOpGroup.ts +456 -0
  369. package/src/lib/opContext/defineOp.ts +281 -0
  370. package/src/lib/opContext/featureFlagTypes.ts +54 -0
  371. package/src/lib/opContext/index.ts +16 -0
  372. package/src/lib/opContext/opGroupTypes.ts +342 -0
  373. package/src/lib/opContext/opTypes.ts +80 -0
  374. package/src/lib/opContext/spanContextTypes.ts +398 -0
  375. package/src/lib/opContext/types.ts +403 -0
  376. package/src/lib/result.ts +550 -0
  377. package/src/lib/schema/LogSchema.ts +153 -0
  378. package/src/lib/schema/__tests__/defineLogSchema.test.ts +342 -0
  379. package/src/lib/schema/__tests__/featureFlags.test.ts +443 -0
  380. package/src/lib/schema/builder.ts +201 -0
  381. package/src/lib/schema/defineFeatureFlags.ts +91 -0
  382. package/src/lib/schema/defineLogSchema.ts +291 -0
  383. package/src/lib/schema/evaluator.ts +404 -0
  384. package/src/lib/schema/extend.ts +69 -0
  385. package/src/lib/schema/systemSchema.ts +339 -0
  386. package/src/lib/schema/typeGuards.ts +163 -0
  387. package/src/lib/schema/types.ts +265 -0
  388. package/src/lib/spanBuffer.ts +571 -0
  389. package/src/lib/spanBufferHelpers.ts +73 -0
  390. package/src/lib/spanBufferStats.ts +40 -0
  391. package/src/lib/spanContext.ts +1566 -0
  392. package/src/lib/sqlite/__tests__/sqlite-common.test.ts +89 -0
  393. package/src/lib/sqlite/index.ts +26 -0
  394. package/src/lib/sqlite/sqlite-async-writer.ts +101 -0
  395. package/src/lib/sqlite/sqlite-common.ts +309 -0
  396. package/src/lib/sqlite/sqlite-d1.ts +104 -0
  397. package/src/lib/sqlite/sqlite-db.ts +32 -0
  398. package/src/lib/sqlite/sqlite-node.ts +83 -0
  399. package/src/lib/sqlite/sqlite-writer.ts +128 -0
  400. package/src/lib/testing/TRACE-TESTING.md +100 -0
  401. package/src/lib/testing/__tests__/bun-harness.test.ts +108 -0
  402. package/src/lib/testing/__tests__/extractFacts.test.ts +279 -0
  403. package/src/lib/testing/__tests__/facts.test.ts +324 -0
  404. package/src/lib/testing/__tests__/vitest-harness.test.ts +32 -0
  405. package/src/lib/testing/bun-harness.ts +990 -0
  406. package/src/lib/testing/extractFacts.ts +362 -0
  407. package/src/lib/testing/facts.ts +470 -0
  408. package/src/lib/testing/index.ts +141 -0
  409. package/src/lib/testing/queryable-span.ts +108 -0
  410. package/src/lib/testing/span-query.ts +65 -0
  411. package/src/lib/testing/stdio-replay.ts +50 -0
  412. package/src/lib/testing/trace-query.ts +329 -0
  413. package/src/lib/testing/vitest-harness.ts +691 -0
  414. package/src/lib/threadId.ts +135 -0
  415. package/src/lib/traceContext.ts +182 -0
  416. package/src/lib/traceId.ts +142 -0
  417. package/src/lib/traceRoot.es.ts +157 -0
  418. package/src/lib/traceRoot.node.ts +170 -0
  419. package/src/lib/traceRoot.ts +154 -0
  420. package/src/lib/traceRoot.universal.ts +22 -0
  421. package/src/lib/tracer.ts +896 -0
  422. package/src/lib/tracers/ArrayQueueTracer.ts +76 -0
  423. package/src/lib/tracers/CompositeTracer.ts +76 -0
  424. package/src/lib/tracers/NoOpTracer.ts +73 -0
  425. package/src/lib/tracers/SQLiteTracer.ts +115 -0
  426. package/src/lib/tracers/StdioTracer.ts +380 -0
  427. package/src/lib/tracers/TestTracer.ts +146 -0
  428. package/src/lib/tracers/__tests__/ArrayQueueTracer.test.ts +184 -0
  429. package/src/lib/tracers/__tests__/NoOpTracer.test.ts +204 -0
  430. package/src/lib/tracers/__tests__/StdioTracer.test.ts +313 -0
  431. package/src/lib/tracers/__tests__/TestTracer.test.ts +200 -0
  432. package/src/lib/tracers/index.ts +12 -0
  433. package/src/lib/types.ts +498 -0
  434. package/src/lib/utf8Cache.ts +201 -0
  435. package/src/lib/wasm/WasmBufferStrategy.ts +219 -0
  436. package/src/lib/wasm/__tests__/WasmBufferStrategy.test.ts +256 -0
  437. package/src/lib/wasm/__tests__/wasm-integration.test.ts +476 -0
  438. package/src/lib/wasm/__tests__/wasmAllocator.test.ts +699 -0
  439. package/src/lib/wasm/__tests__/wasmSpanBuffer.test.ts +449 -0
  440. package/src/lib/wasm/__tests__/wasmTraceRoot.test.ts +390 -0
  441. package/src/lib/wasm/__tests__/wasmTracer.test.ts.disabled +444 -0
  442. package/src/lib/wasm/allocator.zig +945 -0
  443. package/src/lib/wasm/wasmAllocator.ts +469 -0
  444. package/src/lib/wasm/wasmSpanBuffer.ts +1014 -0
  445. package/src/lib/wasm/wasmTraceRoot.ts +315 -0
  446. package/src/node.ts +10 -0
  447. package/src/testing.ts +18 -0
@@ -0,0 +1,498 @@
1
+ /**
2
+ * LMAO-specific types for trace logging
3
+ *
4
+ * ## Type System
5
+ *
6
+ * - `AnySpanBuffer`: NO index signatures, accepts any schema (for generic processing)
7
+ * - `SpanBuffer<T>`: WITH index signatures, typed for specific schema T (T is REQUIRED)
8
+ *
9
+ * This mirrors arrow-builder's AnyColumnBuffer/ColumnBuffer<T> pattern.
10
+ */
11
+
12
+ import type {
13
+ AnyColumnBuffer,
14
+ ColumnBuffer,
15
+ ColumnValueType,
16
+ EagerBinarySchema,
17
+ LazyBinarySchema,
18
+ Nanoseconds,
19
+ TypedArray,
20
+ } from '@smoothbricks/arrow-builder';
21
+ import type { OpMetadata } from './opContext/opTypes.js';
22
+ import type { LogSchema } from './schema/LogSchema.js';
23
+ import type { InferSchema } from './schema/types.js';
24
+ import type { SpanBufferStats } from './spanBufferStats.js';
25
+ import type { TraceId } from './traceId.js';
26
+ import type { ITraceRoot } from './traceRoot.js';
27
+
28
+ // Re-export infrastructure types
29
+ export type { LogBinding, RemappedViewConstructor } from './logBinding.js';
30
+ export type { OpMetadata } from './opContext/opTypes.js';
31
+ export type { extractSpanIdentity, SpanIdentity } from './traceId.js';
32
+ export type { TracerLifecycleHooks } from './traceRoot.js';
33
+
34
+ // Re-export arrow-builder types for convenience
35
+ export type { AnyColumnBuffer, ColumnBuffer, ColumnValueType, TypedArray };
36
+
37
+ // ============================================================================
38
+ // AnySpanBuffer - Base type WITHOUT index signatures
39
+ // ============================================================================
40
+
41
+ /**
42
+ * AnySpanBuffer - Core buffer API for Arrow conversion and generic processing.
43
+ *
44
+ * Per specs/lmao/01b5_spanbuffer_memory_layout.md:
45
+ * SpanBuffer is the per-span columnar storage that holds all trace data for a single span.
46
+ * It uses cache-aligned TypedArrays for V8 optimization and zero-copy Arrow conversion.
47
+ *
48
+ * This interface has NO index signatures, making it compatible with any SpanBuffer<T>
49
+ * regardless of schema. Use this type when you need to accept any buffer:
50
+ * - Arrow conversion (convertToArrowTable)
51
+ * - Tree walking (traverseSpanTree)
52
+ * - Generic buffer processing utilities
53
+ *
54
+ * ## Memory Layout (per specs/lmao/01b5)
55
+ *
56
+ * ```
57
+ * _system ArrayBuffer (64-byte aligned):
58
+ * ├─ timestamp: BigInt64Array (8 bytes × capacity) - nanosecond precision
59
+ * └─ entry_type: Uint8Array (1 byte × capacity) - enum from ENTRY_TYPE_*
60
+ *
61
+ * _identity Uint8Array (48 bytes, immutable after creation):
62
+ * ├─ trace_id: bytes 0-15 (128-bit UUID)
63
+ * ├─ thread_id: bytes 16-23 (64-bit BigInt)
64
+ * ├─ span_id: bytes 24-27 (32-bit counter)
65
+ * ├─ parent_thread_id: bytes 28-35 (64-bit BigInt, 0 if root)
66
+ * └─ parent_span_id: bytes 36-39 (32-bit counter, 0 if root)
67
+ * ```
68
+ *
69
+ * ## Fixed Row Layout (per specs/lmao/01h)
70
+ *
71
+ * - Row 0: span-start (written by writeSpanStart)
72
+ * - Row 1: span-end (span-ok, span-err, or span-exception)
73
+ * - Rows 2+: log entries (info, debug, warn, error, tag, ff-access)
74
+ *
75
+ * @see SpanBuffer<T> for schema-typed version with index signatures
76
+ */
77
+ export interface AnySpanBuffer extends AnyColumnBuffer {
78
+ // ===========================================================================
79
+ // System Memory (cache-aligned, per specs/lmao/01b1)
80
+ // ===========================================================================
81
+
82
+ /**
83
+ * System ArrayBuffer containing timestamp and entry_type columns.
84
+ * 64-byte aligned for CPU cache optimization and SIMD operations.
85
+ */
86
+ readonly _system: ArrayBuffer;
87
+
88
+ /**
89
+ * Identity bytes (48 bytes) containing trace_id, thread_id, span_id, parent info.
90
+ * Immutable after buffer creation - identity never changes.
91
+ */
92
+ readonly _identity: Uint8Array;
93
+
94
+ // ===========================================================================
95
+ // System Columns (always eager, never lazy - per specs/lmao/01b6)
96
+ // ===========================================================================
97
+
98
+ /**
99
+ * Nanosecond timestamps for each entry (BigInt64Array).
100
+ *
101
+ * Per specs/lmao/01b3_high_precision_timestamps.md:
102
+ * - Uses performance.now() anchored to epoch for nanosecond precision
103
+ * - Row 0: span-start timestamp
104
+ * - Row 1: span-end timestamp (ok/err/exception)
105
+ * - Rows 2+: log entry timestamps
106
+ */
107
+ readonly timestamp: BigInt64Array;
108
+
109
+ /**
110
+ * Entry type enum for each row (Uint8Array, 1 byte per entry).
111
+ *
112
+ * Per specs/lmao/01h_entry_types_and_logging_primitives.md:
113
+ * Values are ENTRY_TYPE_* constants (span-start=1, span-ok=2, etc.)
114
+ */
115
+ readonly entry_type: Uint8Array;
116
+
117
+ /**
118
+ * Timestamp of span start (row 0).
119
+ *
120
+ * Internal helper getter (underscore-prefixed) to avoid conflicts with schema columns.
121
+ */
122
+ readonly _spanStartTime: Nanoseconds;
123
+
124
+ /**
125
+ * Most recent non-zero timestamp in this span's written rows, including overflow buffers.
126
+ * Returns null if no written row has a timestamp yet.
127
+ *
128
+ * Internal helper getter (underscore-prefixed) to avoid conflicts with schema columns.
129
+ */
130
+ readonly _lastLoggedTime: Nanoseconds | null;
131
+
132
+ // ===========================================================================
133
+ // Buffer State
134
+ // ===========================================================================
135
+
136
+ /**
137
+ * Current write position (next row to write).
138
+ * Starts at 2 after span-start (row 0) and span-end placeholder (row 1).
139
+ */
140
+ _writeIndex: number;
141
+
142
+ /**
143
+ * Maximum number of rows this buffer can hold.
144
+ * Per specs/lmao/01b2_buffer_self_tuning.md: Capacity adapts based on workload.
145
+ */
146
+ readonly _capacity: number;
147
+
148
+ /**
149
+ * Next buffer in overflow chain (created when _writeIndex >= _capacity).
150
+ * Per specs/lmao/01b2: Buffers chain rather than resize for predictable allocation.
151
+ */
152
+ _overflow?: AnySpanBuffer;
153
+
154
+ // ===========================================================================
155
+ // Identity Getters (read from _identity bytes)
156
+ // ===========================================================================
157
+
158
+ /**
159
+ * Span ID - 32-bit counter unique within this trace.
160
+ * Per specs/lmao/01b4_span_identity.md: Monotonic counter, not globally unique.
161
+ */
162
+ readonly span_id: number;
163
+
164
+ /**
165
+ * Thread ID - 64-bit identifier for this execution thread/context.
166
+ * Per specs/lmao/01b4: Combines worker ID + sequence for uniqueness.
167
+ */
168
+ readonly thread_id: bigint;
169
+
170
+ /**
171
+ * Trace ID - 128-bit UUID identifying the entire trace.
172
+ * Per specs/lmao/01b4: Propagated to all child spans and buffers.
173
+ */
174
+ readonly trace_id: TraceId;
175
+
176
+ /**
177
+ * Whether this span has a parent (false for root spans).
178
+ */
179
+ readonly _hasParent: boolean;
180
+
181
+ /**
182
+ * Parent's span ID (0 if root span).
183
+ */
184
+ readonly parent_span_id: number;
185
+
186
+ /**
187
+ * Parent's thread ID (0n if root span).
188
+ */
189
+ readonly parent_thread_id: bigint;
190
+
191
+ // ===========================================================================
192
+ // Schema & Stats Access (convenience getters for static properties)
193
+ // ===========================================================================
194
+
195
+ /**
196
+ * Schema for this buffer (accessed via constructor.schema).
197
+ * All buffers from same defineOpContext share the same schema.
198
+ * Underscore prefix since this is an internal property.
199
+ */
200
+ readonly _logSchema: LogSchema;
201
+
202
+ /**
203
+ * Column entries for Arrow conversion iteration.
204
+ * For SpanBuffer: delegates to _logSchema._columns (unprefixed names)
205
+ * For RemappedBufferView: built in constructor with prefixed names from mapping
206
+ *
207
+ * This enables Arrow conversion to iterate column names correctly regardless of
208
+ * whether the buffer is wrapped in a RemappedBufferView.
209
+ */
210
+ readonly _columns: ReadonlyArray<[string, unknown]>;
211
+
212
+ /**
213
+ * Shared stats for all buffers from same defineOpContext (accessed via constructor.stats).
214
+ * Enables self-tuning capacity learning across all ops in the same context.
215
+ * Underscore prefix since this is an internal property.
216
+ */
217
+ readonly _stats: SpanBufferStats;
218
+
219
+ // ===========================================================================
220
+ // Tree Structure (for span hierarchy)
221
+ // ===========================================================================
222
+
223
+ /**
224
+ * Child spans created via ctx.span().
225
+ * Per specs/lmao/01c: Parent-child relationship for trace tree.
226
+ */
227
+ _children: AnySpanBuffer[];
228
+
229
+ /**
230
+ * Parent span buffer (undefined for root).
231
+ * Set during createChildSpanBuffer().
232
+ */
233
+ _parent?: AnySpanBuffer;
234
+
235
+ // ===========================================================================
236
+ // Context (metadata and scope)
237
+ // ===========================================================================
238
+
239
+ /**
240
+ * Root trace context - shared by all buffers in a trace.
241
+ * Contains anchorEpochNanos/anchorPerfNow for timestamp calculation.
242
+ */
243
+ _traceRoot: ITraceRoot;
244
+
245
+ /**
246
+ * Op metadata for this span - identifies WHICH OP IS EXECUTING.
247
+ * Per specs/lmao/01j and opgroup-refactor.md:
248
+ * - Used for rows 1+ (log entries, tags, span-ok/err) attribution
249
+ * - For Op calls: set to op.metadata
250
+ * - For plain functions: inherited from parent's _opMetadata
251
+ * - Different from _callsiteMetadata which identifies WHO CALLED span()
252
+ */
253
+ _opMetadata: OpMetadata;
254
+
255
+ /**
256
+ * Callsite metadata - where ctx.span() was called from.
257
+ * Different from _opMetadata when calling into another op.
258
+ * Per specs/lmao/01j: Row 0 gets callsite, rows 1+ get op metadata.
259
+ */
260
+ _callsiteMetadata?: OpMetadata;
261
+
262
+ /**
263
+ * Scoped attribute values - inherited by child spans.
264
+ *
265
+ * Per specs/lmao/01i_span_scope_attributes.md:
266
+ * - Frozen object (never mutated, replaced on setScope)
267
+ * - Child spans inherit by reference (safe because frozen)
268
+ * - Applied to all rows at Arrow conversion via TypedArray.fill()
269
+ */
270
+ _scopeValues: Readonly<Record<string, unknown>>;
271
+
272
+ // ===========================================================================
273
+ // System Column Arrays (per specs/lmao/01h - known system schema fields)
274
+ // These are explicitly typed here since they're always present from systemSchema.
275
+ // ===========================================================================
276
+
277
+ /**
278
+ * Message column values - category strings (eager, always allocated).
279
+ * Contains span names, log messages, exception messages, etc.
280
+ */
281
+ readonly message_values: string[];
282
+ readonly message_nulls: Uint8Array;
283
+
284
+ /**
285
+ * Line number column values - Float64Array (lazy but always present).
286
+ * Contains source code line numbers injected by transformer.
287
+ */
288
+ readonly line_values: Float64Array;
289
+ readonly line_nulls: Uint8Array;
290
+
291
+ /**
292
+ * Error code column values - category strings (lazy but always present).
293
+ */
294
+ readonly error_code_values: string[];
295
+ readonly error_code_nulls: Uint8Array;
296
+
297
+ /**
298
+ * Retry attempt column values - number (lazy but always present).
299
+ */
300
+ readonly retry_attempt_values: Float64Array;
301
+ readonly retry_attempt_nulls: Uint8Array;
302
+
303
+ /**
304
+ * Retry delay column values in milliseconds - number (lazy but always present).
305
+ */
306
+ readonly retry_delay_ms_values: Float64Array;
307
+ readonly retry_delay_ms_nulls: Uint8Array;
308
+
309
+ /**
310
+ * Exception stack trace column values - text strings (lazy but always present).
311
+ */
312
+ readonly exception_stack_values: string[];
313
+ readonly exception_stack_nulls: Uint8Array;
314
+
315
+ /**
316
+ * Feature flag value column - category strings (lazy but always present).
317
+ */
318
+ readonly ff_value_values: string[];
319
+ readonly ff_value_nulls: Uint8Array;
320
+
321
+ /**
322
+ * Uint64 value column - BigUint64Array (lazy but always present).
323
+ */
324
+ readonly uint64_value_values: BigUint64Array;
325
+ readonly uint64_value_nulls: Uint8Array;
326
+
327
+ // ===========================================================================
328
+ // System Column Setters (per specs/lmao/01h - always eager)
329
+ // ===========================================================================
330
+
331
+ /**
332
+ * Set message for a row (span name for row 0, log message for rows 2+).
333
+ * @returns this buffer for chaining (returns AnySpanBuffer for variance)
334
+ */
335
+ message(pos: number, val: string): AnySpanBuffer;
336
+
337
+ /**
338
+ * Set source line number for a row.
339
+ * Per specs/lmao/01o: Injected by transformer at compile time.
340
+ */
341
+ line(pos: number, val: number): AnySpanBuffer;
342
+
343
+ /**
344
+ * Set error code for a row (used by span-err and error log entries).
345
+ */
346
+ error_code(pos: number, val: string): AnySpanBuffer;
347
+
348
+ /**
349
+ * Set retry attempt for a row (used by span-retry entries).
350
+ */
351
+ retry_attempt(pos: number, val: number): AnySpanBuffer;
352
+
353
+ /**
354
+ * Set retry delay in ms for a row (used by span-retry entries).
355
+ */
356
+ retry_delay_ms(pos: number, val: number): AnySpanBuffer;
357
+
358
+ /**
359
+ * Set exception stack trace for a row (used by span-exception).
360
+ */
361
+ exception_stack(pos: number, val: string): AnySpanBuffer;
362
+
363
+ /**
364
+ * Set feature flag value for a row (used by ff-access entries).
365
+ */
366
+ ff_value(pos: number, val: string): AnySpanBuffer;
367
+
368
+ /**
369
+ * Set uint64 value for a row (general-purpose bigint storage).
370
+ */
371
+ uint64_value(pos: number, val: bigint): AnySpanBuffer;
372
+
373
+ // ===========================================================================
374
+ // Methods
375
+ // ===========================================================================
376
+
377
+ /**
378
+ * Get or create an overflow buffer for this span.
379
+ *
380
+ * This method handles the full overflow lifecycle:
381
+ * 1. Returns existing _overflow if already created
382
+ * 2. Notifies tracer.onStatsWillResetFor() before stats may reset
383
+ * 3. Checks capacity tuning based on utilization
384
+ * 4. Creates new overflow buffer via bufferStrategy
385
+ * 5. Links this._overflow to the new buffer
386
+ *
387
+ * SpanLogger calls this when buffer is full, then prefills scope on the new buffer.
388
+ *
389
+ * @returns The overflow buffer (existing or newly created)
390
+ */
391
+ getOrCreateOverflow(): AnySpanBuffer;
392
+
393
+ /**
394
+ * Check if this span is a parent of another span.
395
+ * Used for tree traversal and validation.
396
+ */
397
+ isParentOf(other: AnySpanBuffer): boolean;
398
+
399
+ /**
400
+ * Check if this span is a child of another span.
401
+ * Used for tree traversal and validation.
402
+ */
403
+ isChildOf(other: AnySpanBuffer): boolean;
404
+
405
+ /**
406
+ * Copy this span's thread_id bytes to a destination array.
407
+ * Used for Arrow column building (zero-copy when possible).
408
+ */
409
+ copyThreadIdTo(dest: Uint8Array, offset: number): void;
410
+
411
+ /**
412
+ * Copy this span's parent_thread_id bytes to a destination array.
413
+ * Used for Arrow column building (zero-copy when possible).
414
+ */
415
+ copyParentThreadIdTo(dest: Uint8Array, offset: number): void;
416
+ }
417
+
418
+ // ============================================================================
419
+ // Schema Type Helpers
420
+ // ============================================================================
421
+
422
+ type SetterValueType<S> = S extends { __schema_type: 'enum' }
423
+ ? number
424
+ : S extends { __schema_type: 'category' | 'text' }
425
+ ? string
426
+ : S extends { __schema_type: 'number' }
427
+ ? number
428
+ : S extends { __schema_type: 'bigUint64' }
429
+ ? bigint
430
+ : S extends { __schema_type: 'boolean' }
431
+ ? boolean
432
+ : S extends LazyBinarySchema<infer T> | EagerBinarySchema<infer T>
433
+ ? T
434
+ : unknown;
435
+
436
+ type ValuesArrayType<S> = S extends { __schema_type: 'enum' }
437
+ ? Uint8Array
438
+ : S extends { __schema_type: 'category' | 'text' }
439
+ ? string[]
440
+ : S extends { __schema_type: 'number' }
441
+ ? Float64Array
442
+ : S extends { __schema_type: 'bigUint64' }
443
+ ? BigUint64Array
444
+ : S extends { __schema_type: 'boolean' }
445
+ ? Uint8Array
446
+ : S extends LazyBinarySchema | EagerBinarySchema
447
+ ? unknown[]
448
+ : TypedArray | string[];
449
+
450
+ type FilterSchemaFields<T> = {
451
+ [K in keyof T as T[K] extends (...args: unknown[]) => unknown ? never : K]: T[K];
452
+ };
453
+
454
+ // ============================================================================
455
+ // SpanBuffer<T> - Typed buffer WITH index signatures
456
+ // ============================================================================
457
+
458
+ /**
459
+ * SpanBuffer<T> - Fully typed buffer for a specific schema.
460
+ *
461
+ * Extends AnySpanBuffer with:
462
+ * - Schema-specific _values/_nulls properties
463
+ * - Schema-specific setter methods
464
+ * - Index signatures for dynamic access
465
+ *
466
+ * T is REQUIRED - there is no default. Use AnySpanBuffer for generic processing.
467
+ */
468
+ export type SpanBuffer<T extends LogSchema> = AnySpanBuffer & {
469
+ // Override tree structure with typed versions
470
+ _children: SpanBuffer<T>[];
471
+ _parent?: SpanBuffer<T>;
472
+ _overflow?: SpanBuffer<T>;
473
+
474
+ // Override schema access with the concrete schema for this buffer family
475
+ readonly _logSchema: T;
476
+ _scopeValues: Readonly<Partial<InferSchema<T>>>;
477
+
478
+ // Override methods with typed versions
479
+ isParentOf(other: SpanBuffer<T>): boolean;
480
+ isChildOf(other: SpanBuffer<T>): boolean;
481
+
482
+ // Index signatures for dynamic access (includes unknown[] for binary columns)
483
+ [key: `${string}_values`]: TypedArray | string[] | unknown[] | undefined;
484
+ [key: `${string}_nulls`]: Uint8Array | undefined;
485
+ } & {
486
+ // Schema-specific column data
487
+ readonly [K in keyof FilterSchemaFields<T['fields']> as `${K & string}_values`]: ValuesArrayType<
488
+ FilterSchemaFields<T['fields']>[K]
489
+ >;
490
+ } & {
491
+ readonly [K in keyof FilterSchemaFields<T['fields']> as `${K & string}_nulls`]: Uint8Array;
492
+ } & {
493
+ // Schema-specific setter methods (return this for method chaining)
494
+ [K in keyof FilterSchemaFields<T['fields']>]: (
495
+ pos: number,
496
+ val: SetterValueType<FilterSchemaFields<T['fields']>[K]>,
497
+ ) => SpanBuffer<T>;
498
+ };
@@ -0,0 +1,201 @@
1
+ /**
2
+ * SIEVE-based cache for UTF-8 string encoding
3
+ *
4
+ * Per string storage design specs:
5
+ * - Category/text columns store raw JS strings in hot path (zero cost)
6
+ * - UTF-8 encoding happens in cold path (Arrow conversion)
7
+ * - Frequent strings benefit from caching to avoid re-encoding
8
+ *
9
+ * Uses SIEVE algorithm (NSDI'24) which is simpler AND better than LRU
10
+ * for web workloads. SIEVE achieves ~9% lower miss ratio than LRU-K,
11
+ * ARC, and 2Q while being much simpler to implement.
12
+ *
13
+ * @see https://junchengyang.com/publication/nsdi24-SIEVE.pdf
14
+ */
15
+
16
+ import { SieveCache } from '@neophi/sieve-cache';
17
+ import type { Utf8Encoder } from '@smoothbricks/arrow-builder';
18
+
19
+ /**
20
+ * Default cache size for UTF-8 encoding
21
+ *
22
+ * 4096 entries is a reasonable default that:
23
+ * - Handles typical category cardinality (user IDs, session IDs, etc.)
24
+ * - Bounds memory usage (~4KB keys + encoded values)
25
+ * - Provides good hit rate for repetitive strings
26
+ */
27
+ const DEFAULT_UTF8_CACHE_SIZE = 4096;
28
+
29
+ type EncodedUtf8Batch = { data: Uint8Array; offsets: Int32Array };
30
+ type Utf8CacheStats = { hits: number; misses: number; hitRate: number; size: number };
31
+
32
+ /**
33
+ * UTF-8 encoding cache using SIEVE eviction algorithm
34
+ *
35
+ * This cache is used during Arrow conversion to avoid re-encoding
36
+ * strings that appear multiple times across buffers or conversions.
37
+ *
38
+ * Thread-safety: JavaScript is single-threaded, so no locks needed.
39
+ * The cache is safe to use in async contexts since JS awaits don't
40
+ * allow concurrent access to the same cache instance.
41
+ */
42
+ export class Utf8Cache implements Utf8Encoder {
43
+ private readonly cache: SieveCache<string, Uint8Array>;
44
+ private readonly encoder = new TextEncoder();
45
+
46
+ // Stats for debugging/monitoring
47
+ private hits = 0;
48
+ private misses = 0;
49
+
50
+ constructor(maxSize = DEFAULT_UTF8_CACHE_SIZE) {
51
+ this.cache = new SieveCache<string, Uint8Array>(maxSize);
52
+ }
53
+
54
+ /**
55
+ * Get UTF-8 encoded bytes for a string, using cache if available
56
+ *
57
+ * @param str - String to encode
58
+ * @returns UTF-8 encoded bytes
59
+ */
60
+ encode(str: string): Uint8Array {
61
+ const cached = this.cache.get(str);
62
+ if (cached !== undefined) {
63
+ this.hits++;
64
+ return cached;
65
+ }
66
+
67
+ this.misses++;
68
+ const encoded = this.encoder.encode(str);
69
+ this.cache.set(str, encoded);
70
+ return encoded;
71
+ }
72
+
73
+ /**
74
+ * Calculate UTF-8 byte length of a string
75
+ *
76
+ * Uses cache if available to avoid re-encoding.
77
+ *
78
+ * @param str - String to measure
79
+ * @returns UTF-8 byte length
80
+ */
81
+ byteLength(str: string): number {
82
+ // Check cache first - if encoded, we know the length
83
+ const cached = this.cache.get(str);
84
+ if (cached !== undefined) {
85
+ this.hits++;
86
+ return cached.length;
87
+ }
88
+
89
+ // Not cached - encode and cache it, then return length
90
+ this.misses++;
91
+ const encoded = this.encoder.encode(str);
92
+ this.cache.set(str, encoded);
93
+ return encoded.length;
94
+ }
95
+
96
+ /**
97
+ * Encode string directly into buffer at offset
98
+ *
99
+ * @param str - String to encode
100
+ * @param buffer - Target buffer
101
+ * @param offset - Offset in buffer to write at
102
+ * @returns Number of bytes written
103
+ */
104
+ encodeInto(str: string, buffer: Uint8Array, offset: number): number {
105
+ // Use encode() which leverages the cache
106
+ const encoded = this.encode(str);
107
+ buffer.set(encoded, offset);
108
+ return encoded.length;
109
+ }
110
+
111
+ /**
112
+ * Encode multiple strings, returning concatenated bytes and offsets
113
+ *
114
+ * This is optimized for Arrow dictionary construction where we need
115
+ * both the concatenated bytes and the offset array.
116
+ *
117
+ * @param strings - Array of strings to encode
118
+ * @returns Concatenated UTF-8 bytes and Arrow-format offsets
119
+ */
120
+ encodeMany(strings: readonly string[]): EncodedUtf8Batch {
121
+ // First pass: encode all strings and calculate total size
122
+ const encoded: Uint8Array[] = new Array(strings.length);
123
+ let totalSize = 0;
124
+
125
+ for (let i = 0; i < strings.length; i++) {
126
+ encoded[i] = this.encode(strings[i]);
127
+ totalSize += encoded[i].length;
128
+ }
129
+
130
+ // Build offsets array (Arrow format: length = strings.length + 1)
131
+ const offsets = new Int32Array(strings.length + 1);
132
+ offsets[0] = 0;
133
+ for (let i = 0; i < encoded.length; i++) {
134
+ offsets[i + 1] = offsets[i] + encoded[i].length;
135
+ }
136
+
137
+ // Concatenate all encoded bytes
138
+ const data = new Uint8Array(totalSize);
139
+ let offset = 0;
140
+ for (const bytes of encoded) {
141
+ data.set(bytes, offset);
142
+ offset += bytes.length;
143
+ }
144
+
145
+ return { data, offsets };
146
+ }
147
+
148
+ /**
149
+ * Get cache statistics
150
+ */
151
+ get stats(): Utf8CacheStats {
152
+ const total = this.hits + this.misses;
153
+ return {
154
+ hits: this.hits,
155
+ misses: this.misses,
156
+ hitRate: total > 0 ? this.hits / total : 0,
157
+ size: this.cache.size,
158
+ };
159
+ }
160
+
161
+ /**
162
+ * Reset statistics (useful for per-conversion tracking)
163
+ */
164
+ resetStats(): void {
165
+ this.hits = 0;
166
+ this.misses = 0;
167
+ }
168
+
169
+ /**
170
+ * Clear the cache entirely
171
+ */
172
+ clear(): void {
173
+ this.cache.clear();
174
+ this.hits = 0;
175
+ this.misses = 0;
176
+ }
177
+ }
178
+
179
+ /**
180
+ * Global UTF-8 cache instance
181
+ *
182
+ * Using a global cache provides cross-conversion caching benefits:
183
+ * - Strings that appear in multiple spans/buffers are cached
184
+ * - Warm cache improves performance over time
185
+ *
186
+ * The SIEVE eviction ensures bounded memory even with unbounded inputs.
187
+ */
188
+ export const globalUtf8Cache = new Utf8Cache();
189
+
190
+ /**
191
+ * Create a new UTF-8 cache with custom size
192
+ *
193
+ * Use this when you want a dedicated cache instance (e.g., for testing
194
+ * or for isolation between different conversion contexts).
195
+ *
196
+ * @param maxSize - Maximum number of entries to cache
197
+ * @returns New Utf8Cache instance
198
+ */
199
+ export function createUtf8Cache(maxSize?: number): Utf8Cache {
200
+ return new Utf8Cache(maxSize);
201
+ }