@bjornpagen/bumbledb 1.3.0 → 2.0.0-rc.1

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 (290) hide show
  1. package/COOKBOOK.md +224 -95
  2. package/README.md +114 -45
  3. package/dist/alternatives.d.ts +5 -6
  4. package/dist/alternatives.d.ts.map +1 -1
  5. package/dist/alternatives.js +5 -6
  6. package/dist/alternatives.js.map +1 -1
  7. package/dist/bin.d.ts +3 -0
  8. package/dist/bin.d.ts.map +1 -0
  9. package/dist/bin.js +85 -0
  10. package/dist/bin.js.map +1 -0
  11. package/dist/capacity.d.ts +14 -17
  12. package/dist/capacity.d.ts.map +1 -1
  13. package/dist/capacity.js +7 -7
  14. package/dist/capacity.js.map +1 -1
  15. package/dist/changes.d.ts +32 -41
  16. package/dist/changes.d.ts.map +1 -1
  17. package/dist/changes.js +86 -110
  18. package/dist/changes.js.map +1 -1
  19. package/dist/closed.d.ts +2 -2
  20. package/dist/closed.d.ts.map +1 -1
  21. package/dist/closed.js +5 -5
  22. package/dist/closed.js.map +1 -1
  23. package/dist/codec.d.ts +20 -27
  24. package/dist/codec.d.ts.map +1 -1
  25. package/dist/codec.js +48 -22
  26. package/dist/codec.js.map +1 -1
  27. package/dist/compile.d.ts +26 -50
  28. package/dist/compile.d.ts.map +1 -1
  29. package/dist/compile.js +75 -64
  30. package/dist/compile.js.map +1 -1
  31. package/dist/database/database.d.ts +98 -0
  32. package/dist/database/database.d.ts.map +1 -0
  33. package/dist/database/database.js +249 -0
  34. package/dist/database/database.js.map +1 -0
  35. package/dist/database/driver.d.ts +40 -0
  36. package/dist/database/driver.d.ts.map +1 -0
  37. package/dist/database/driver.js +68 -0
  38. package/dist/database/driver.js.map +1 -0
  39. package/dist/database/fs.d.ts +12 -0
  40. package/dist/database/fs.d.ts.map +1 -0
  41. package/dist/database/fs.js +120 -0
  42. package/dist/database/fs.js.map +1 -0
  43. package/dist/database/generate.d.ts +26 -0
  44. package/dist/database/generate.d.ts.map +1 -0
  45. package/dist/database/generate.js +102 -0
  46. package/dist/database/generate.js.map +1 -0
  47. package/dist/database/hosted.d.ts +32 -0
  48. package/dist/database/hosted.d.ts.map +1 -0
  49. package/dist/database/hosted.js +44 -0
  50. package/dist/database/hosted.js.map +1 -0
  51. package/dist/database/io.d.ts +94 -0
  52. package/dist/database/io.d.ts.map +1 -0
  53. package/dist/database/io.js +35 -0
  54. package/dist/database/io.js.map +1 -0
  55. package/dist/database/mem.d.ts +26 -0
  56. package/dist/database/mem.d.ts.map +1 -0
  57. package/dist/database/mem.js +67 -0
  58. package/dist/database/mem.js.map +1 -0
  59. package/dist/database/migration.d.ts +55 -0
  60. package/dist/database/migration.d.ts.map +1 -0
  61. package/dist/database/migration.js +46 -0
  62. package/dist/database/migration.js.map +1 -0
  63. package/dist/database/s3.d.ts +36 -0
  64. package/dist/database/s3.d.ts.map +1 -0
  65. package/dist/database/s3.js +141 -0
  66. package/dist/database/s3.js.map +1 -0
  67. package/dist/db.d.ts +44 -128
  68. package/dist/db.d.ts.map +1 -1
  69. package/dist/db.js +163 -287
  70. package/dist/db.js.map +1 -1
  71. package/dist/engine.d.ts +7 -0
  72. package/dist/engine.d.ts.map +1 -0
  73. package/dist/engine.js +2 -0
  74. package/dist/engine.js.map +1 -0
  75. package/dist/errors.d.ts +152 -46
  76. package/dist/errors.d.ts.map +1 -1
  77. package/dist/errors.js +107 -19
  78. package/dist/errors.js.map +1 -1
  79. package/dist/face.d.ts +5 -5
  80. package/dist/face.d.ts.map +1 -1
  81. package/dist/face.js +6 -6
  82. package/dist/face.js.map +1 -1
  83. package/dist/fields.d.ts +4 -5
  84. package/dist/fields.d.ts.map +1 -1
  85. package/dist/fields.js +3 -3
  86. package/dist/fields.js.map +1 -1
  87. package/dist/index.d.ts +60 -58
  88. package/dist/index.d.ts.map +1 -1
  89. package/dist/index.js +27 -29
  90. package/dist/index.js.map +1 -1
  91. package/dist/judgment.d.ts +1 -1
  92. package/dist/judgment.js +1 -1
  93. package/dist/law.d.ts +16 -68
  94. package/dist/law.d.ts.map +1 -1
  95. package/dist/law.js +3 -3
  96. package/dist/law.js.map +1 -1
  97. package/dist/lower.d.ts +3 -3
  98. package/dist/lower.d.ts.map +1 -1
  99. package/dist/lower.js +35 -39
  100. package/dist/lower.js.map +1 -1
  101. package/dist/native/addon.d.ts +23 -0
  102. package/dist/native/addon.d.ts.map +1 -0
  103. package/dist/native/addon.js +15 -0
  104. package/dist/native/addon.js.map +1 -0
  105. package/dist/native/binding.d.ts +996 -0
  106. package/dist/native/json.d.ts +14 -0
  107. package/dist/native/json.d.ts.map +1 -0
  108. package/dist/native/json.js +73 -0
  109. package/dist/native/json.js.map +1 -0
  110. package/dist/native/load.d.ts +9 -0
  111. package/dist/native/load.d.ts.map +1 -0
  112. package/dist/native/load.js +30 -0
  113. package/dist/native/load.js.map +1 -0
  114. package/dist/native/op.d.ts +29 -0
  115. package/dist/native/op.d.ts.map +1 -0
  116. package/dist/native/op.js +50 -0
  117. package/dist/native/op.js.map +1 -0
  118. package/dist/query/atom.d.ts +13 -14
  119. package/dist/query/atom.d.ts.map +1 -1
  120. package/dist/query/atom.js +2 -2
  121. package/dist/query/atom.js.map +1 -1
  122. package/dist/query/compute.d.ts +7 -7
  123. package/dist/query/compute.d.ts.map +1 -1
  124. package/dist/query/compute.js +7 -16
  125. package/dist/query/compute.js.map +1 -1
  126. package/dist/query/find.d.ts +8 -8
  127. package/dist/query/find.d.ts.map +1 -1
  128. package/dist/query/find.js +1 -1
  129. package/dist/query/lower.d.ts +31 -17
  130. package/dist/query/lower.d.ts.map +1 -1
  131. package/dist/query/lower.js +260 -368
  132. package/dist/query/lower.js.map +1 -1
  133. package/dist/query/run.d.ts +5 -5
  134. package/dist/query/run.d.ts.map +1 -1
  135. package/dist/query/run.js +32 -15
  136. package/dist/query/run.js.map +1 -1
  137. package/dist/query/scope.d.ts +20 -28
  138. package/dist/query/scope.d.ts.map +1 -1
  139. package/dist/query/scope.js +13 -8
  140. package/dist/query/scope.js.map +1 -1
  141. package/dist/query/segments.d.ts +2 -2
  142. package/dist/query/segments.d.ts.map +1 -1
  143. package/dist/query/segments.js +3 -3
  144. package/dist/query/segments.js.map +1 -1
  145. package/dist/relation.d.ts +1 -1
  146. package/dist/relation.d.ts.map +1 -1
  147. package/dist/relation.js +4 -4
  148. package/dist/relation.js.map +1 -1
  149. package/dist/result.d.ts +15 -22
  150. package/dist/result.d.ts.map +1 -1
  151. package/dist/result.js +30 -39
  152. package/dist/result.js.map +1 -1
  153. package/dist/rows.d.ts +9 -33
  154. package/dist/rows.d.ts.map +1 -1
  155. package/dist/rows.js +8 -12
  156. package/dist/rows.js.map +1 -1
  157. package/dist/runtime.d.ts +17 -43
  158. package/dist/runtime.d.ts.map +1 -1
  159. package/dist/runtime.js +41 -241
  160. package/dist/runtime.js.map +1 -1
  161. package/dist/scalar.d.ts +36 -106
  162. package/dist/scalar.d.ts.map +1 -1
  163. package/dist/scalar.js +36 -185
  164. package/dist/scalar.js.map +1 -1
  165. package/dist/schema.d.ts +7 -18
  166. package/dist/schema.d.ts.map +1 -1
  167. package/dist/schema.js +18 -228
  168. package/dist/schema.js.map +1 -1
  169. package/dist/selection.d.ts +4 -4
  170. package/dist/selection.d.ts.map +1 -1
  171. package/dist/selection.js +5 -5
  172. package/dist/selection.js.map +1 -1
  173. package/dist/shape.d.ts +5 -5
  174. package/dist/shape.d.ts.map +1 -1
  175. package/dist/spec.d.ts +7 -79
  176. package/dist/spec.d.ts.map +1 -1
  177. package/dist/spec.js.map +1 -1
  178. package/dist/statements.d.ts +3 -3
  179. package/dist/statements.d.ts.map +1 -1
  180. package/dist/statements.js +10 -10
  181. package/dist/statements.js.map +1 -1
  182. package/dist/uuid.d.ts +1 -1
  183. package/dist/uuid.d.ts.map +1 -1
  184. package/dist/uuid.js +1 -1
  185. package/dist/uuid.js.map +1 -1
  186. package/dist/values.d.ts +2 -2
  187. package/dist/values.d.ts.map +1 -1
  188. package/dist/values.js +2 -2
  189. package/dist/values.js.map +1 -1
  190. package/package.json +18 -18
  191. package/src/alternatives.ts +7 -8
  192. package/src/bin.ts +82 -0
  193. package/src/capacity.ts +20 -23
  194. package/src/changes.ts +134 -197
  195. package/src/closed.ts +5 -5
  196. package/src/codec.ts +77 -51
  197. package/src/compile.ts +91 -113
  198. package/src/database/database.ts +413 -0
  199. package/src/database/driver.ts +134 -0
  200. package/src/database/fs.ts +119 -0
  201. package/src/database/generate.ts +116 -0
  202. package/src/database/hosted.ts +102 -0
  203. package/src/database/io.ts +134 -0
  204. package/src/database/mem.ts +113 -0
  205. package/src/database/migration.ts +107 -0
  206. package/src/database/s3.ts +231 -0
  207. package/src/db.ts +290 -547
  208. package/src/engine.ts +14 -0
  209. package/src/errors.ts +118 -39
  210. package/src/face.ts +9 -9
  211. package/src/fields.ts +6 -7
  212. package/src/index.ts +102 -109
  213. package/src/judgment.ts +1 -1
  214. package/src/law.ts +18 -70
  215. package/src/lower.ts +61 -72
  216. package/src/native/addon.ts +54 -0
  217. package/src/native/binding.d.ts +996 -0
  218. package/src/native/json.ts +100 -0
  219. package/src/native/load.ts +31 -0
  220. package/src/native/op.ts +73 -0
  221. package/src/query/atom.ts +15 -16
  222. package/src/query/compute.ts +13 -22
  223. package/src/query/find.ts +8 -8
  224. package/src/query/lower.ts +323 -414
  225. package/src/query/run.ts +42 -21
  226. package/src/query/scope.ts +37 -38
  227. package/src/query/segments.ts +4 -4
  228. package/src/relation.ts +4 -4
  229. package/src/result.ts +53 -80
  230. package/src/rows.ts +18 -46
  231. package/src/runtime.ts +50 -346
  232. package/src/scalar.ts +76 -336
  233. package/src/schema.ts +23 -284
  234. package/src/selection.ts +6 -6
  235. package/src/shape.ts +5 -5
  236. package/src/spec.ts +7 -85
  237. package/src/statements.ts +11 -11
  238. package/src/uuid.ts +1 -1
  239. package/src/values.ts +4 -4
  240. package/dist/close.d.ts +0 -24
  241. package/dist/close.d.ts.map +0 -1
  242. package/dist/close.js +0 -34
  243. package/dist/close.js.map +0 -1
  244. package/dist/db-native.d.ts +0 -196
  245. package/dist/db-native.d.ts.map +0 -1
  246. package/dist/db-native.js +0 -13
  247. package/dist/db-native.js.map +0 -1
  248. package/dist/internal/log.d.ts +0 -26
  249. package/dist/internal/log.d.ts.map +0 -1
  250. package/dist/internal/log.js +0 -12
  251. package/dist/internal/log.js.map +0 -1
  252. package/dist/migration.d.ts +0 -31
  253. package/dist/migration.d.ts.map +0 -1
  254. package/dist/migration.js +0 -47
  255. package/dist/migration.js.map +0 -1
  256. package/dist/native.d.ts +0 -562
  257. package/dist/native.d.ts.map +0 -1
  258. package/dist/native.js +0 -123
  259. package/dist/native.js.map +0 -1
  260. package/dist/query/description.d.ts +0 -41
  261. package/dist/query/description.d.ts.map +0 -1
  262. package/dist/query/description.js +0 -311
  263. package/dist/query/description.js.map +0 -1
  264. package/dist/query/parse-ir.d.ts +0 -10
  265. package/dist/query/parse-ir.d.ts.map +0 -1
  266. package/dist/query/parse-ir.js +0 -280
  267. package/dist/query/parse-ir.js.map +0 -1
  268. package/dist/runtime-codes.d.ts +0 -2
  269. package/dist/runtime-codes.d.ts.map +0 -1
  270. package/dist/runtime-codes.js +0 -18
  271. package/dist/runtime-codes.js.map +0 -1
  272. package/dist/runtime-errors.d.ts +0 -118
  273. package/dist/runtime-errors.d.ts.map +0 -1
  274. package/dist/runtime-errors.js +0 -98
  275. package/dist/runtime-errors.js.map +0 -1
  276. package/dist/runtime-native.d.ts +0 -141
  277. package/dist/runtime-native.d.ts.map +0 -1
  278. package/dist/runtime-native.js +0 -5
  279. package/dist/runtime-native.js.map +0 -1
  280. package/pack-provenance.json +0 -7
  281. package/src/close.ts +0 -53
  282. package/src/db-native.ts +0 -243
  283. package/src/internal/log.ts +0 -48
  284. package/src/migration.ts +0 -61
  285. package/src/native.ts +0 -736
  286. package/src/query/description.ts +0 -424
  287. package/src/query/parse-ir.ts +0 -291
  288. package/src/runtime-codes.ts +0 -17
  289. package/src/runtime-errors.ts +0 -109
  290. package/src/runtime-native.ts +0 -189
@@ -1,291 +0,0 @@
1
- import { AuthoringError } from "#errors.ts"
2
- import type {
3
- AtomIr,
4
- CmpOpIr,
5
- ConditionTreeIr,
6
- FindTermIr,
7
- HeadTermIr,
8
- ParsedQuery,
9
- QueryIr,
10
- RuleIr,
11
- ScalarExprIr,
12
- TermIr
13
- } from "#native.ts"
14
- import { roundingMode } from "#scalar.ts"
15
- import { arrayValue as array, recordValue, valueDescriptor } from "#values.ts"
16
-
17
- function fail(context: string, expected: string): never {
18
- throw new AuthoringError({
19
- message: `${context}: ${expected}`,
20
- diagnostic: { code: "InvalidQuery", context, expected }
21
- })
22
- }
23
-
24
- function tagged(context: string, input: unknown): Readonly<Record<string, unknown>> {
25
- if (typeof input !== "object" || input === null) return fail(context, "expected a descriptor record")
26
- return recordValue(context, input, Object.keys(input))
27
- }
28
-
29
- function ordinal(context: string, input: unknown, maximum = 0xffff): number {
30
- if (typeof input !== "number" || !Number.isInteger(input) || input < 0 || input > maximum)
31
- return fail(context, `expected an ordinal from 0 through ${maximum}`)
32
- return input
33
- }
34
-
35
- function term(context: string, input: unknown): TermIr {
36
- const raw = tagged(context, input)
37
- switch (raw.kind) {
38
- case "var":
39
- recordValue(context, raw, ["kind", "var"])
40
- return Object.freeze({ kind: raw.kind, var: ordinal(context, raw.var) })
41
- case "param":
42
- case "paramSet":
43
- recordValue(context, raw, ["kind", "param"])
44
- return Object.freeze({ kind: raw.kind, param: ordinal(context, raw.param) })
45
- case "literal":
46
- recordValue(context, raw, ["kind", "value"])
47
- return Object.freeze({ kind: raw.kind, value: valueDescriptor(`${context}.value`, raw.value) })
48
- default:
49
- return fail(context, "unknown term kind")
50
- }
51
- }
52
-
53
- function scalar(context: string, input: unknown, depth = 1): ScalarExprIr {
54
- if (depth > 128) return fail(context, "scalar expression deeper than 128")
55
- const raw = tagged(context, input)
56
- switch (raw.kind) {
57
- case "var":
58
- recordValue(context, raw, ["kind", "var"])
59
- return Object.freeze({ kind: raw.kind, var: ordinal(context, raw.var) })
60
- case "literal":
61
- recordValue(context, raw, ["kind", "value"])
62
- return Object.freeze({ kind: raw.kind, value: valueDescriptor(`${context}.value`, raw.value) })
63
- case "measure":
64
- case "negate":
65
- case "isNaN":
66
- case "isFinite":
67
- recordValue(context, raw, ["kind", "expr"])
68
- return Object.freeze({ kind: raw.kind, expr: scalar(`${context}.expr`, raw.expr, depth + 1) })
69
- case "add":
70
- case "subtract":
71
- case "multiply":
72
- case "divide":
73
- recordValue(context, raw, ["kind", "left", "right"])
74
- return Object.freeze({
75
- kind: raw.kind,
76
- left: scalar(`${context}.left`, raw.left, depth + 1),
77
- right: scalar(`${context}.right`, raw.right, depth + 1)
78
- })
79
- case "mulDiv":
80
- recordValue(context, raw, ["kind", "a", "b", "divisor", "rounding"])
81
- return Object.freeze({
82
- kind: raw.kind,
83
- a: scalar(`${context}.a`, raw.a, depth + 1),
84
- b: scalar(`${context}.b`, raw.b, depth + 1),
85
- divisor: scalar(`${context}.divisor`, raw.divisor, depth + 1),
86
- rounding: roundingMode(raw.rounding)
87
- })
88
- case "cast": {
89
- recordValue(context, raw, ["kind", "cast", "expr"])
90
- const cast = raw.cast
91
- if (cast !== "toF64" && cast !== "toF64Exact" && cast !== "toI64Exact" && cast !== "toU64Exact")
92
- return fail(context, "unknown numeric cast")
93
- return Object.freeze({ kind: raw.kind, cast, expr: scalar(`${context}.expr`, raw.expr, depth + 1) })
94
- }
95
- default:
96
- return fail(context, "unknown scalar expression kind")
97
- }
98
- }
99
-
100
- function headTerm(context: string, input: unknown): HeadTermIr {
101
- const raw = tagged(context, input)
102
- if (raw.kind === "var" || raw.kind === "compute") {
103
- recordValue(context, raw, ["kind"])
104
- return Object.freeze({ kind: raw.kind })
105
- }
106
- recordValue(context, raw, ["kind", "op"])
107
- if (raw.kind !== "aggregate") return fail(context, "unknown head kind")
108
- const op = raw.op
109
- if (op !== "sum" && op !== "mean" && op !== "min" && op !== "max" && op !== "count" && op !== "pack")
110
- return fail(context, "unknown head aggregate")
111
- return Object.freeze({ kind: raw.kind, op })
112
- }
113
-
114
- function find(context: string, input: unknown): FindTermIr {
115
- const raw = tagged(context, input)
116
- if (raw.kind === "segments") {
117
- recordValue(context, raw, ["kind", "op", "left", "right"])
118
- if (raw.op !== "intersection" && raw.op !== "difference") return fail(context, "unknown segment operator")
119
- return Object.freeze({
120
- kind: raw.kind,
121
- op: raw.op,
122
- left: ordinal(context, raw.left),
123
- right: ordinal(context, raw.right)
124
- })
125
- }
126
- switch (raw.kind) {
127
- case "var":
128
- recordValue(context, raw, ["kind", "var"])
129
- return Object.freeze({ kind: raw.kind, var: ordinal(context, raw.var) })
130
- case "count":
131
- if (Object.hasOwn(raw, "over")) return fail(context, "Count carries no over")
132
- recordValue(context, raw, ["kind"])
133
- return Object.freeze({ kind: raw.kind })
134
- case "compute":
135
- if (!Object.hasOwn(raw, "expr")) return fail(context, "compute requires expr")
136
- recordValue(context, raw, ["kind", "expr"])
137
- return Object.freeze({ kind: raw.kind, expr: scalar(`${context}.expr`, raw.expr) })
138
- case "pack":
139
- recordValue(context, raw, ["kind", "over"])
140
- return Object.freeze({ kind: raw.kind, over: ordinal(context, raw.over) })
141
- case "aggregate": {
142
- recordValue(context, raw, ["kind", "op", "over"])
143
- const op = recordValue(`${context}.op`, raw.op, ["kind"]).kind
144
- if (op !== "sum" && op !== "mean" && op !== "min" && op !== "max") return fail(context, "unknown fold operator")
145
- return Object.freeze({ kind: raw.kind, op: Object.freeze({ kind: op }), over: ordinal(context, raw.over) })
146
- }
147
- default:
148
- return fail(context, "unknown find kind")
149
- }
150
- }
151
-
152
- function atom(context: string, input: unknown): AtomIr {
153
- const raw = recordValue(context, input, ["source", "bindings"])
154
- const source = tagged(`${context}.source`, raw.source)
155
- let owned: AtomIr["source"]
156
- if (source.kind === "edb") {
157
- recordValue(context, source, ["kind", "relation"])
158
- owned = { kind: source.kind, relation: ordinal(`${context}.source.relation`, source.relation, 0xffffffff) }
159
- } else if (source.kind === "interior") {
160
- recordValue(context, source, ["kind", "interior"])
161
- owned = { kind: source.kind, interior: ordinal(`${context}.source.interior`, source.interior, 0xffffffff) }
162
- } else return fail(context, "unknown atom source")
163
- const seen = new Set<number>()
164
- const bindings = array(`${context}.bindings`, raw.bindings, (path, pair): readonly [number, TermIr] => {
165
- const cells = array(path, pair, (_, value) => value)
166
- if (cells.length !== 2) return fail(path, "a binding has exactly two elements")
167
- const field = ordinal(`${path}[0]`, cells[0])
168
- if (seen.has(field)) return fail(path, "duplicate field binding")
169
- seen.add(field)
170
- return Object.freeze([field, term(`${path}[1]`, cells[1])])
171
- })
172
- return Object.freeze({ source: Object.freeze(owned), bindings })
173
- }
174
-
175
- function comparisonOp(context: string, input: unknown): CmpOpIr {
176
- const raw = tagged(context, input)
177
- if (raw.kind === "allen") {
178
- recordValue(context, raw, ["kind", "mask"])
179
- return Object.freeze({ kind: raw.kind, mask: ordinal(`${context}.mask`, raw.mask, 0x1fff) })
180
- }
181
- recordValue(context, raw, ["kind"])
182
- const kind = raw.kind
183
- if (
184
- kind !== "eq" &&
185
- kind !== "ne" &&
186
- kind !== "lt" &&
187
- kind !== "le" &&
188
- kind !== "gt" &&
189
- kind !== "ge" &&
190
- kind !== "pointIn"
191
- )
192
- return fail(context, "unknown comparison operator")
193
- return Object.freeze({ kind })
194
- }
195
-
196
- function condition(context: string, input: unknown, depth = 1): ConditionTreeIr {
197
- if (depth > 64) return fail(context, "condition tree deeper than 64")
198
- const raw = tagged(context, input)
199
- if (raw.kind === "leaf") {
200
- recordValue(context, raw, ["kind", "cmp"])
201
- const cmp = recordValue(`${context}.cmp`, raw.cmp, ["op", "lhs", "rhs"])
202
- return Object.freeze({
203
- kind: raw.kind,
204
- cmp: Object.freeze({
205
- op: comparisonOp(`${context}.cmp.op`, cmp.op),
206
- lhs: term(`${context}.cmp.lhs`, cmp.lhs),
207
- rhs: term(`${context}.cmp.rhs`, cmp.rhs)
208
- })
209
- })
210
- }
211
- if (raw.kind !== "and" && raw.kind !== "or") return fail(context, "unknown condition kind")
212
- recordValue(context, raw, ["kind", "children"])
213
- return Object.freeze({
214
- kind: raw.kind,
215
- children: array(`${context}.children`, raw.children, (path, value) => condition(path, value, depth + 1))
216
- })
217
- }
218
-
219
- function rule(context: string, input: unknown): RuleIr {
220
- const raw = recordValue(context, input, ["finds", "atoms", "negated", "conditions"])
221
- return Object.freeze({
222
- finds: array(`${context}.finds`, raw.finds, find),
223
- atoms: array(`${context}.atoms`, raw.atoms, atom),
224
- negated: array(`${context}.negated`, raw.negated, atom),
225
- conditions: array(`${context}.conditions`, raw.conditions, condition)
226
- })
227
- }
228
-
229
- function align(context: string, head: readonly HeadTermIr[], rules: readonly RuleIr[]): void {
230
- for (const [index, rule] of rules.entries()) {
231
- if (rule.finds.length !== head.length) fail(`${context}.rules[${index}]`, "finds width does not match head width")
232
- for (const [position, find] of rule.finds.entries()) {
233
- const term = head[position]
234
- const plainFamily = find.kind === "var" || find.kind === "compute" ? find.kind : "aggregate"
235
- const family = find.kind === "segments" ? "compute" : plainFamily
236
- if (term?.kind !== family)
237
- fail(`${context}.rules[${index}].finds[${position}]`, "find family does not match head")
238
- if (term.kind === "aggregate") {
239
- const op = find.kind === "aggregate" ? find.op.kind : find.kind
240
- if (op !== term.op)
241
- fail(`${context}.rules[${index}].finds[${position}]`, "aggregate operator does not match head")
242
- }
243
- }
244
- }
245
- }
246
-
247
- /**
248
- * Own and validate the complete wire grammar. The native compiler then
249
- * checks schema membership, binding types, safety and recursion semantics;
250
- * parsing alone does not assert that a query is admitted by a schema.
251
- */
252
- function parseQueryIr(input: unknown): ParsedQuery {
253
- const raw = tagged("query", input)
254
- if (raw.kind !== "cq" && raw.kind !== "reach") return fail("query", "unknown query kind")
255
- recordValue(
256
- "query",
257
- raw,
258
- raw.kind === "cq" ? ["kind", "interiors", "head", "rules"] : ["kind", "interiors", "rec", "head", "rules"]
259
- )
260
- const rules = array("query.rules", raw.rules, rule)
261
- if (rules.length === 0) return fail("parseQueryIr", "main rules are empty")
262
- const head = array("query.head", raw.head, headTerm)
263
- align("query", head, rules)
264
- const interiors = array("query.interiors", raw.interiors, (path, input) => {
265
- const raw = recordValue(path, input, ["head", "rules"])
266
- const head = array(`${path}.head`, raw.head, headTerm)
267
- const rules = array(`${path}.rules`, raw.rules, rule)
268
- if (rules.length === 0) return fail(path, "interior rules are empty")
269
- align(path, head, rules)
270
- return Object.freeze({ head, rules })
271
- })
272
- let owned: QueryIr
273
- if (raw.kind === "cq") owned = { kind: raw.kind, interiors, head, rules }
274
- else {
275
- const rawRec = recordValue("query.rec", raw.rec, ["head", "base", "rec"])
276
- const headRec = array("query.rec.head", rawRec.head, headTerm)
277
- const base = array("query.rec.base", rawRec.base, rule)
278
- if (base.length === 0) return fail("parseQueryIr", "rec base is empty")
279
- const rec = array("query.rec.rec", rawRec.rec, rule)
280
- if (rec.length === 0) return fail("parseQueryIr", "rec step is empty")
281
- align("rec base", headRec, base)
282
- align("rec step", headRec, rec)
283
- if (headRec.some((term) => term.kind !== "var") || [...base, ...rec].some((rule) => rule.negated.length !== 0))
284
- return fail("query.rec", "recursion requires projection-only heads and no negation")
285
- owned = { kind: raw.kind, interiors, head, rules, rec: Object.freeze({ head: headRec, base, rec }) }
286
- }
287
- return Object.freeze(owned) as ParsedQuery
288
- }
289
-
290
- export type { ParsedQuery }
291
- export { parseQueryIr }
@@ -1,17 +0,0 @@
1
- // Generated from the exact native runtimeErrorCodes export; do not edit.
2
- export const runtimeErrorCodes = [
3
- "RuntimeAlreadyLive",
4
- "ForeignRuntime",
5
- "ClosedHandle",
6
- "SpentHandle",
7
- "QueueFull",
8
- "InvalidArgument",
9
- "Internal",
10
- "DirectoryBusy",
11
- "WriterBusy",
12
- "InvalidPath",
13
- "Io",
14
- "ResourceLimit",
15
- "Engine",
16
- "Cancelled"
17
- ] as const
@@ -1,109 +0,0 @@
1
- import { Schema } from "effect"
2
- import { AuthoringDiagnostic, AuthoringError } from "#errors.ts"
3
- import { runtimeErrorCodes } from "#runtime-codes.ts"
4
-
5
- export { runtimeErrorCodes } from "#runtime-codes.ts"
6
-
7
- const ResourceLimit = Schema.Struct({
8
- _tag: Schema.Literal("ResourceLimit"),
9
- dimension: Schema.String,
10
- used: Schema.BigInt,
11
- requested: Schema.BigInt,
12
- limit: Schema.BigInt
13
- })
14
- const PlainReason = Schema.Struct({
15
- _tag: Schema.Literals(
16
- runtimeErrorCodes.filter(
17
- (code) => code !== "ResourceLimit" && code !== "Io" && code !== "Engine" && code !== "InvalidArgument"
18
- )
19
- )
20
- })
21
- const InvalidArgument = Schema.Struct({
22
- _tag: Schema.Literal("InvalidArgument"),
23
- detail: Schema.optional(
24
- Schema.Struct({
25
- message: Schema.String,
26
- diagnostic: Schema.optional(AuthoringDiagnostic)
27
- })
28
- )
29
- })
30
- const Io = Schema.Struct({
31
- _tag: Schema.Literal("Io"),
32
- kind: Schema.String,
33
- osCode: Schema.optional(Schema.Number)
34
- })
35
- /** A typed engine refusal crossing the executor: core family tag + message. */
36
- const Engine = Schema.Struct({
37
- _tag: Schema.Literal("Engine"),
38
- kind: Schema.String,
39
- message: Schema.String
40
- })
41
- export const DbReason = Schema.Union([ResourceLimit, Io, Engine, InvalidArgument, PlainReason])
42
- export class DbError extends Schema.TaggedError<DbError>()("DbError", {
43
- operation: Schema.String,
44
- reason: DbReason
45
- }) {
46
- get code() {
47
- return this.reason._tag
48
- }
49
- /** Safe default display; callers explicitly inspect structured detail. */
50
- override get message(): string {
51
- return `${this.operation}: ${this.code}`
52
- }
53
- }
54
-
55
- const decodeReason = Schema.decodeUnknownOption(DbReason)
56
- export function dbError(operation: string, cause: unknown): DbError {
57
- if (cause instanceof DbError) return cause
58
- if (cause instanceof AuthoringError) return argumentError(operation, cause)
59
- const decoded = decodeReason(cause)
60
- if (decoded._tag === "Some") return new DbError({ operation, reason: decoded.value })
61
- // Synchronous native schema/marshalling refusals precede the executor's
62
- // tagged error wire. Preserve their engine family instead of losing it.
63
- if (
64
- typeof cause === "object" &&
65
- cause !== null &&
66
- "kind" in cause &&
67
- typeof cause.kind === "string" &&
68
- "message" in cause &&
69
- typeof cause.message === "string"
70
- ) {
71
- return new DbError({ operation, reason: { _tag: "Engine", kind: cause.kind, message: cause.message } })
72
- }
73
- return new DbError({ operation, reason: { _tag: "Internal" } })
74
- }
75
-
76
- /** Host input failures retain authoring details without displaying values. */
77
- export function argumentError(operation: string, cause: unknown): DbError {
78
- if (cause instanceof DbError) return cause
79
- if (cause instanceof AuthoringError) {
80
- const detail =
81
- cause.diagnostic === undefined
82
- ? { message: cause.message }
83
- : { message: cause.message, diagnostic: cause.diagnostic }
84
- return new DbError({ operation, reason: { _tag: "InvalidArgument", detail } })
85
- }
86
- return new DbError({ operation, reason: { _tag: "InvalidArgument" } })
87
- }
88
-
89
- const Outstanding = Schema.Struct({
90
- phase: Schema.Literals(["open", "closing", "closed"]),
91
- queued: Schema.BigInt,
92
- active: Schema.BigInt,
93
- retained: Schema.BigInt,
94
- owners: Schema.BigInt,
95
- databases: Schema.BigInt,
96
- natives: Schema.BigInt
97
- })
98
- export type OutstandingWork = typeof Outstanding.Type
99
- const Close = Schema.Union([
100
- Schema.Struct({ kind: Schema.Literal("closed") }),
101
- Schema.Struct({ kind: Schema.Literal("incomplete"), outstanding: Outstanding }),
102
- Schema.Struct({ kind: Schema.Literal("failed"), error: DbError })
103
- ])
104
- export type CloseReport = typeof Close.Type
105
-
106
- export class CloseFailure extends Schema.TaggedError<CloseFailure>()("CloseFailure", {
107
- operation: Schema.String,
108
- report: Close
109
- }) {}
@@ -1,189 +0,0 @@
1
- /** Exact-version internal bridge, shared by core and log. Not a public API. */
2
-
3
- import type {
4
- DbHandle,
5
- LogChainKind,
6
- LogCommandKind,
7
- LogCommandMetadata,
8
- LogCommandRef,
9
- LogCondition,
10
- LogDecisionStamp,
11
- LogIdentity,
12
- LogLimits,
13
- LogOutcomeWire,
14
- LogSchemaHandle,
15
- LogStateStamp,
16
- Violation
17
- } from "#native.ts"
18
- import { native } from "#native.ts"
19
- import type { SchemaSpec } from "#spec.ts"
20
-
21
- export interface RuntimeHandle {
22
- readonly __runtime: unique symbol
23
- }
24
- export interface OperationHandle {
25
- readonly __operation: unique symbol
26
- }
27
- export interface DirectoryHandle {
28
- readonly __directory: unique symbol
29
- }
30
-
31
- /** Stamped `NativeKind::RepositoryLock` mint — not a directory-owner twin. */
32
- export interface RepositoryLockHandle {
33
- readonly __repositoryLock: unique symbol
34
- }
35
-
36
- /** Resource kinds installed in native worker tables. */
37
- export type NativeKind = "snapshot" | "prepared" | "result" | "cursor" | "draft" | "changes" | "repository-lock"
38
-
39
- /**
40
- * Checked capability into the native registry. JS tokens validate
41
- * these five fields; native re-judges kind/generation/owner on every verb.
42
- */
43
- export interface Capability {
44
- readonly runtime: bigint
45
- readonly worker: number
46
- readonly kind: NativeKind
47
- readonly id: bigint
48
- readonly generation: bigint
49
- }
50
-
51
- export type ManagedDbOutcome =
52
- | { readonly tag: "accepted"; readonly db: DbHandle }
53
- | { readonly tag: "rejected"; readonly violations: readonly Violation[] }
54
- | {
55
- readonly tag: "refused"
56
- readonly kind: "schemaError" | "newtypeMismatch" | "fingerprintMismatch" | "destinationExists"
57
- readonly message: string
58
- }
59
-
60
- export type LogTakeWire =
61
- | { readonly ok: false; readonly kind: LogCommandKind | LogChainKind; readonly message: string }
62
- | {
63
- /** Command sealed: envelope bytes + reference. */
64
- readonly ok: true
65
- readonly bytes: Uint8Array
66
- readonly ref: LogCommandRef
67
- }
68
- | {
69
- /** Command parsed. */
70
- readonly ok: true
71
- readonly identity: LogIdentity
72
- readonly receiptEpoch: bigint
73
- readonly requestId: string
74
- readonly condition: LogCondition
75
- readonly changes: Uint8Array
76
- readonly result: Uint8Array
77
- readonly ref: LogCommandRef
78
- }
79
- | {
80
- /** Decision framed. */
81
- readonly ok: true
82
- readonly bytes: Uint8Array
83
- readonly digest: Uint8Array
84
- }
85
- | {
86
- /** Decision decoded (and chain-verified when a parent was given). */
87
- readonly ok: true
88
- readonly identity: LogIdentity
89
- readonly seq: bigint
90
- readonly parent: LogDecisionStamp
91
- readonly beforeState: LogStateStamp
92
- readonly afterState: LogStateStamp
93
- readonly commandBytes: Uint8Array
94
- readonly command: LogCommandRef
95
- readonly outcome: LogOutcomeWire
96
- readonly digest: Uint8Array
97
- }
98
-
99
- export interface OptionsWire {
100
- readonly workers?: number | undefined
101
- readonly queueCapacity?: number | undefined
102
- readonly cleanupCapacity?: number | undefined
103
- readonly ownerCapacity?: number | undefined
104
- readonly nativeHandleCapacity?: number | undefined
105
- readonly cleanupTimeoutMs?: number | undefined
106
- }
107
-
108
- export interface InspectionWire {
109
- readonly phase: "open" | "closing" | "closed"
110
- readonly queued: bigint
111
- readonly active: bigint
112
- readonly retained: bigint
113
- readonly owners: bigint
114
- readonly databases: bigint
115
- readonly natives: bigint
116
- }
117
-
118
- export type CloseWire =
119
- | { readonly kind: "closed" }
120
- | { readonly kind: "incomplete"; readonly outstanding: InspectionWire }
121
- | { readonly kind: "failed" }
122
-
123
- interface RuntimeNative {
124
- runtimeErrorCodes(): readonly string[]
125
- runtimeOpen(options: OptionsWire): RuntimeHandle
126
- runtimeReady(runtime: RuntimeHandle, callback: () => void): OperationHandle
127
- runtimeHash(runtime: RuntimeHandle, bytes: Uint8Array, callback: () => void): OperationHandle
128
- runtimeTake(operation: OperationHandle): Uint8Array | null
129
- runtimeCancel(operation: OperationHandle, callback: (report: CloseWire) => void): void
130
- runtimeClose(runtime: RuntimeHandle, callback: (report: CloseWire) => void): void
131
- runtimeInspect(runtime: RuntimeHandle): InspectionWire
132
- /**
133
- * L12 request (D12/D25): one-shot arm. Next `dispatch_payload_message`
134
- * must cancel after `work()` returns and the post-work `checkpoint()`
135
- * (`runtime.rs` ~860–862), before `complete_operation` writes
136
- * `operation.output`. A local `QueuedOutput` is not registration.
137
- * Predelivery: no JS take; cursor stays on row1 so retry does not skip.
138
- */
139
- runtimeArmPublicationCancel(runtime: RuntimeHandle): void
140
- runtimeDirectoryAcquire(runtime: RuntimeHandle, path: string, callback: () => void): OperationHandle
141
- runtimeDirectoryTake(operation: OperationHandle): DirectoryHandle
142
- runtimeDirectoryBegin(owner: DirectoryHandle): OperationHandle
143
- runtimeDirectoryCheck(operation: OperationHandle): void
144
- runtimeDirectoryEnd(operation: OperationHandle): void
145
- runtimeDirectoryClose(owner: DirectoryHandle, remove: boolean, callback: (report: CloseWire) => void): void
146
- runtimeDirectoryDbOpen(
147
- owner: DirectoryHandle,
148
- childName: string,
149
- spec: SchemaSpec,
150
- create: boolean,
151
- callback: () => void
152
- ): OperationHandle
153
- runtimeDbTake(operation: OperationHandle): ManagedDbOutcome
154
- runtimeManagedDbClose(db: DbHandle, callback: (report: CloseWire) => void): void
155
-
156
- // --- successor log grammar on the executor (hashing over
157
- // whole canonical change payloads; C06 through C09) ---
158
- runtimeLogCommandSeal(
159
- runtime: RuntimeHandle,
160
- schema: LogSchemaHandle,
161
- metadata: LogCommandMetadata,
162
- changes: Uint8Array,
163
- result: Uint8Array | null,
164
- limits: LogLimits,
165
- callback: () => void
166
- ): OperationHandle
167
- runtimeLogCommandParse(
168
- runtime: RuntimeHandle,
169
- schema: LogSchemaHandle,
170
- bytes: Uint8Array,
171
- limits: LogLimits,
172
- callback: () => void
173
- ): OperationHandle
174
- runtimeLogDecisionDecode(
175
- runtime: RuntimeHandle,
176
- bytes: Uint8Array,
177
- parent: LogDecisionStamp | null,
178
- limits: LogLimits,
179
- callback: () => void
180
- ): OperationHandle
181
- runtimeLogTake(operation: OperationHandle): LogTakeWire
182
- /** L14 mint: `Runtime::mint_repository_lock` stamps `NativeKind::RepositoryLock` at take. */
183
- logRepositoryLockAcquire(runtime: RuntimeHandle, directory: string, callback: () => void): OperationHandle
184
- logRepositoryLockTake(operation: OperationHandle): RepositoryLockHandle
185
- logRepositoryLockRelease(owner: RepositoryLockHandle, callback: (report: CloseWire) => void): void
186
- }
187
-
188
- // The checked source/fresh-addon roster test pins this private declaration.
189
- export const runtimeNative = native as typeof native & RuntimeNative