@bjornpagen/bumbledb 0.7.0 → 0.9.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 (68) hide show
  1. package/COOKBOOK.md +105 -3
  2. package/README.md +3 -3
  3. package/dist/capacity.d.ts +310 -0
  4. package/dist/capacity.d.ts.map +1 -0
  5. package/dist/capacity.js +138 -0
  6. package/dist/capacity.js.map +1 -0
  7. package/dist/db.d.ts +5 -3
  8. package/dist/db.d.ts.map +1 -1
  9. package/dist/db.js +8 -8
  10. package/dist/db.js.map +1 -1
  11. package/dist/face.d.ts +1 -1
  12. package/dist/face.d.ts.map +1 -1
  13. package/dist/face.js.map +1 -1
  14. package/dist/index.d.ts +8 -8
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +3 -3
  17. package/dist/index.js.map +1 -1
  18. package/dist/law.d.ts +3 -2
  19. package/dist/law.d.ts.map +1 -1
  20. package/dist/law.js +1 -1
  21. package/dist/law.js.map +1 -1
  22. package/dist/lower.d.ts.map +1 -1
  23. package/dist/lower.js +3 -2
  24. package/dist/lower.js.map +1 -1
  25. package/dist/native.d.ts +6 -3
  26. package/dist/native.d.ts.map +1 -1
  27. package/dist/native.js.map +1 -1
  28. package/dist/order.d.ts +53 -2
  29. package/dist/order.d.ts.map +1 -1
  30. package/dist/order.js +30 -12
  31. package/dist/order.js.map +1 -1
  32. package/dist/query/atom.d.ts +33 -13
  33. package/dist/query/atom.d.ts.map +1 -1
  34. package/dist/query/atom.js +1 -1
  35. package/dist/query/atom.js.map +1 -1
  36. package/dist/query/find.d.ts +19 -8
  37. package/dist/query/find.d.ts.map +1 -1
  38. package/dist/query/find.js +6 -5
  39. package/dist/query/find.js.map +1 -1
  40. package/dist/schema.js +1 -1
  41. package/dist/schema.js.map +1 -1
  42. package/dist/spec.d.ts +76 -23
  43. package/dist/spec.d.ts.map +1 -1
  44. package/dist/spec.js +39 -7
  45. package/dist/spec.js.map +1 -1
  46. package/dist/statements.d.ts +42 -25
  47. package/dist/statements.d.ts.map +1 -1
  48. package/dist/statements.js +81 -19
  49. package/dist/statements.js.map +1 -1
  50. package/package.json +4 -4
  51. package/src/capacity.ts +454 -0
  52. package/src/db.ts +13 -11
  53. package/src/face.ts +1 -0
  54. package/src/index.ts +20 -9
  55. package/src/law.ts +3 -2
  56. package/src/lower.ts +3 -2
  57. package/src/native.ts +6 -3
  58. package/src/order.ts +83 -5
  59. package/src/query/atom.ts +37 -13
  60. package/src/query/find.ts +34 -16
  61. package/src/schema.ts +1 -1
  62. package/src/spec.ts +91 -25
  63. package/src/statements.ts +160 -34
  64. package/dist/count.d.ts +0 -102
  65. package/dist/count.d.ts.map +0 -1
  66. package/dist/count.js +0 -115
  67. package/dist/count.js.map +0 -1
  68. package/src/count.ts +0 -211
@@ -0,0 +1,454 @@
1
+ /**
2
+ * Capacity-law mints — the window, weight, and dependent-bound vocabulary
3
+ * the one `capacity()` statement constructor consumes
4
+ * (`docs/architecture/70-api.md` § the canonical-utterance law, restated
5
+ * around the aggregate form). `within()` is the ONE window spelling —
6
+ * `within(n)` exact (`within(0n)` IS the exclusion's one spelling),
7
+ * `within(lo, hi)` range (`within(0n, hi)` the canonical ceiling),
8
+ * `within(lo, "*")` floor — `weigh()` names the measure on the SOURCE row
9
+ * (`weigh("watts")` a u64 field, `weigh(duration("booked"))` an interval's
10
+ * measure), and `ref()`/`duration()` read a dependent bound from the
11
+ * TARGET row (hi slot only — ruled 2026-07-24, C6). The ban table is
12
+ * enforced REPRESENTATIONALLY in two tiers, split per-aggregate where
13
+ * weight-sensitive (design § 6: a ban is canonical-utterance policing when
14
+ * it is weight-independent, semantic deduplication when it is not):
15
+ *
16
+ * - **The type tier**: a banned spelling written as a LITERAL does not
17
+ * compile — every negative bound, `within(n, n)`, `within(0n, 0n)`, and
18
+ * `within(0n, "*")` are type errors naming the canonical form. The
19
+ * weight-SENSITIVE row rides the `capacity()` overloads themselves:
20
+ * `within(1n, "*")` is banned on the unit overload only (`{1..*}` on the
21
+ * count instance is the bare containment respelled —
22
+ * `window_floor_containment`), and LEGAL on the weighted one ("positive
23
+ * total" is not an existence claim over rows).
24
+ * - **The construction tier**: a bound the type level cannot judge — a
25
+ * COMPUTED `bigint`, or an inverted `within(lo, hi)` order — is judged at
26
+ * construction with the same canonical-naming errors; and past both
27
+ * tiers the engine's own spec validation remains the law for a hostile
28
+ * FFI caller (the standing two-tier ban enforcement).
29
+ *
30
+ * The weight vocabulary is closed at the row (ruled 2026-07-24, ruling 6):
31
+ * a path weight (`weigh("model.watts")`) is a typed refusal at BOTH tiers
32
+ * whose diagnostic names the pinned-column idiom — the two-column
33
+ * containment IS the join, stated as a law. Bounds are `bigint` (u64
34
+ * crosses as bigint always, PRD-04's law); the witnessed measure comes
35
+ * back as `bigint` too (u128-wide engine accumulator, C3).
36
+ */
37
+
38
+ import * as errors from "@superbuilders/errors"
39
+ import type { AnyFace, FaceFields, FaceSource, ProjectedShape } from "#face.ts"
40
+ import type { CapacityBoundSpec, CapacityWindowSpec, WeightSpec } from "#spec.ts"
41
+
42
+ /**
43
+ * The admission brand — a module-private symbol, deliberately unexported
44
+ * (the standing statement-mint pattern): `CapacityWindowSpec` and
45
+ * `WeightSpec` are public wire types, so without this brand every banned
46
+ * spelling in the ban table would be writable as a plain object literal.
47
+ * The symbol makes `within()` and `weigh()` the ONLY producers of the
48
+ * values `capacity()` accepts, which is what "the ban table is unwritable"
49
+ * means.
50
+ */
51
+ const admitted: unique symbol = Symbol("bumbledb.capacity.admitted")
52
+
53
+ /**
54
+ * An admitted capacity window — opaque and inert: a fact about the theory,
55
+ * not a builder. Only `within()` produces one; the spec is carried at its
56
+ * EXACT type so the weight-sensitive `{1..*}` ban and the dependent-bound
57
+ * target wall are judged at the `capacity()` call.
58
+ */
59
+ interface CapacityWindow<S extends CapacityWindowSpec = CapacityWindowSpec> {
60
+ readonly window: S
61
+ readonly [admitted]: true
62
+ }
63
+
64
+ /**
65
+ * An admitted measure — `weigh()`'s product, the `[w]` bracket of the
66
+ * operator. The spec is carried at its exact type so the u64/interval
67
+ * source wall ({@link WeightOnSource}) reads the weighed field's name off
68
+ * the value.
69
+ */
70
+ interface CapacityWeight<S extends WeightSpec = WeightSpec> {
71
+ readonly weight: S
72
+ readonly [admitted]: true
73
+ }
74
+
75
+ /**
76
+ * A dependent bound naming a u64 field of the TARGET row — `ref()`'s
77
+ * product, legal in `within()`'s hi slot only (C6: a dependent floor has
78
+ * no use case; inversion with idents is unrepresentable). Carries no
79
+ * value: bounds resolve per target row at judge time.
80
+ */
81
+ interface FieldRef<F extends string = string> {
82
+ readonly kind: "field"
83
+ readonly field: F
84
+ }
85
+
86
+ /**
87
+ * A dependent interval-measure bound (`Duration(field)` of the TARGET
88
+ * row), or — handed to `weigh()` — the interval-measure weight of the
89
+ * SOURCE row. One mint, both slots: the interval enters through the
90
+ * measure argument, never the group key.
91
+ */
92
+ interface DurationRef<F extends string = string> {
93
+ readonly kind: "durationField"
94
+ readonly field: F
95
+ }
96
+
97
+ /** Narrows any value to an admitted capacity window through the module-private brand. */
98
+ function isCapacityWindow(value: unknown): value is CapacityWindow {
99
+ return typeof value === "object" && value !== null && admitted in value && "window" in value
100
+ }
101
+
102
+ /** Narrows any value to an admitted capacity weight through the module-private brand. */
103
+ function isCapacityWeight(value: unknown): value is CapacityWeight {
104
+ return typeof value === "object" && value !== null && admitted in value && "weight" in value
105
+ }
106
+
107
+ /**
108
+ * The legible banned-spelling verdict: intersected into a mint's parameter
109
+ * when the LITERAL argument spells a banned window, naming the canonical
110
+ * form — the compile-time face of the ban table.
111
+ */
112
+ interface BannedWindow<Canonical extends string> {
113
+ readonly "banned window spelling — the canonical-utterance law names the one legal form": Canonical
114
+ }
115
+
116
+ /**
117
+ * The path-weight refusal (ruling 6 — a boundary, not a deferral): the
118
+ * verdict names the pinned-column idiom, the same diagnostic the macro
119
+ * expansion and the spec resolver carry.
120
+ */
121
+ interface RefusedPath<Idiom extends string> {
122
+ readonly "path spelling refused — the vocabulary is closed at the row": Idiom
123
+ }
124
+
125
+ /** `true` exactly when the literal bigint `N` is negative (out of the u64 bound domain). */
126
+ type IsNegative<N extends bigint> = `${N}` extends `-${string}` ? true : false
127
+
128
+ /**
129
+ * The dotted-name ban, both mints: a `a.b` spelling is a typed refusal
130
+ * whose verdict names the composition idiom — pin the column with a
131
+ * two-column containment (`Device(model, watts) <= Model(id, watts)`) and
132
+ * name the local field.
133
+ */
134
+ type PathBan<F extends string> = string extends F
135
+ ? unknown
136
+ : F extends `${string}.${string}`
137
+ ? RefusedPath<"pin the column — a two-column containment (Source(ref, f) <= Catalog(id, f)) proves the local copy, then name the local field">
138
+ : unknown
139
+
140
+ /** The ban verdict on a single bound: negatives are out of the u64 domain. */
141
+ type NegativeBan<N extends bigint> = bigint extends N
142
+ ? unknown
143
+ : IsNegative<N> extends true
144
+ ? BannedWindow<"capacity bounds are u64 — a negative bound is out of domain">
145
+ : unknown
146
+
147
+ /**
148
+ * The ban verdict on `within(lo, "*")`: `{0..*}` is vacuous —
149
+ * weight-independent (sums are ≥ 0). `{1..*}` is NOT judged here: the ban
150
+ * is weight-sensitive (Count-instance only) and rides the `capacity()`
151
+ * unit overload ({@link UnitWindowBan}).
152
+ */
153
+ type FloorBan<N extends bigint> = bigint extends N
154
+ ? unknown
155
+ : IsNegative<N> extends true
156
+ ? BannedWindow<"capacity bounds are u64 — a negative bound is out of domain">
157
+ : N extends 0n
158
+ ? BannedWindow<"`{0..*}` is vacuous — it provably says nothing; delete the statement">
159
+ : unknown
160
+
161
+ /**
162
+ * The ban verdict on `within(lo, hi)` with both literals known: `{n..n}`
163
+ * is the exact measure respelled (`within(n)`, or `within(0n)` at 0) —
164
+ * weight-independent canonical policing. Bound ORDER (`{hi..lo}` inverted)
165
+ * is not type-expressible — bigint literals have no type-level comparison
166
+ * — so inversion stays a construction error.
167
+ */
168
+ type RangeBan<Lo extends bigint, Hi extends bigint> = bigint extends Lo
169
+ ? unknown
170
+ : bigint extends Hi
171
+ ? unknown
172
+ : IsNegative<Lo> extends true
173
+ ? BannedWindow<"capacity bounds are u64 — a negative bound is out of domain">
174
+ : IsNegative<Hi> extends true
175
+ ? BannedWindow<"capacity bounds are u64 — a negative bound is out of domain">
176
+ : Lo extends Hi
177
+ ? Hi extends Lo
178
+ ? Lo extends 0n
179
+ ? BannedWindow<"`{0..0}` — the point window is written `{0}`: use within(0n)">
180
+ : BannedWindow<"`{n..n}` — an exact measure is written `{n}`: use within(n)">
181
+ : unknown
182
+ : unknown
183
+
184
+ /**
185
+ * The weight-SENSITIVE ban row, judged at the `capacity()` call where the
186
+ * aggregate instance is known (design § 6): on the UNIT overload a
187
+ * `{1..*}` floor says only what the bare containment says
188
+ * (`window_floor_containment`) and is banned naming `contained`; on the
189
+ * weighted overload the same window is LEGAL — "positive total" admits
190
+ * zero-weight rows and is a different, weaker law.
191
+ */
192
+ type UnitWindowBan<W extends CapacityWindow> = W["window"] extends {
193
+ readonly kind: "floor"
194
+ readonly lo: { readonly kind: "lit"; readonly value: 1n }
195
+ }
196
+ ? BannedWindow<"`{1..*}` on the unit instance says only what the bare containment says — write contained(source, target)">
197
+ : unknown
198
+
199
+ /** The projected kind of one field of a face's source, read off the schema type. */
200
+ type KindAt<S extends FaceSource, K extends string> =
201
+ ProjectedShape<S, K> extends readonly [infer Kind, ...unknown[]] ? Kind : undefined
202
+
203
+ /**
204
+ * The legible off-roster verdict for a dependent bound: bound names
205
+ * resolve against the TARGET's full field roster (C1 — the written
206
+ * projection tuple stays the pure grouping key), and the verdict names
207
+ * that roster.
208
+ */
209
+ interface BoundOffTargetRoster<K, Roster> {
210
+ readonly "dependent bound must name a field of the TARGET's own row — bound names resolve against the target's full roster": readonly [
211
+ K,
212
+ Roster
213
+ ]
214
+ }
215
+
216
+ /** The legible kind-mismatch verdict for a dependent bound: `ref()` needs u64, `duration()` an interval. */
217
+ interface BoundKindMismatch<K, Want> {
218
+ readonly "dependent bound kind mismatch — ref() reads a u64 field, duration() an interval field, of the TARGET row": readonly [
219
+ K,
220
+ Want
221
+ ]
222
+ }
223
+
224
+ /**
225
+ * One dependent-bound slot judged against the target face's roster and
226
+ * kinds. A WIDE field name (the untyped-caller path — literal identity
227
+ * already lost) passes the type tier and is judged at construction.
228
+ */
229
+ type BoundOnTarget<K extends string, Want extends "u64" | "interval", B extends AnyFace> = string extends K
230
+ ? unknown
231
+ : K extends FaceFields<B["source"]>
232
+ ? KindAt<B["source"], K> extends Want
233
+ ? unknown
234
+ : BoundKindMismatch<K, Want>
235
+ : BoundOffTargetRoster<K, FaceFields<B["source"]>>
236
+
237
+ /**
238
+ * The dependent-bound target wall (type tier): for a `ref(K)` bound in the
239
+ * window's hi slot, `K` must name a u64 field of the TARGET face's own
240
+ * row; for a `duration(K)` bound, an interval field. Literal-bound windows
241
+ * pass untouched. The runtime twin for untyped callers lives at the
242
+ * `capacity()` construction (`statements.ts`).
243
+ */
244
+ type BoundsOnTarget<W extends CapacityWindow, B extends AnyFace> = W["window"] extends {
245
+ readonly hi: FieldRef<infer K>
246
+ }
247
+ ? BoundOnTarget<K, "u64", B>
248
+ : W["window"] extends { readonly hi: DurationRef<infer K> }
249
+ ? BoundOnTarget<K, "interval", B>
250
+ : unknown
251
+
252
+ /** The legible off-roster verdict for a weight: the vocabulary is closed at the SOURCE row. */
253
+ interface WeightOffSourceRoster<K, Roster> {
254
+ readonly "weight must name a field of the SOURCE's own row — the weight vocabulary is closed at the row": readonly [
255
+ K,
256
+ Roster
257
+ ]
258
+ }
259
+
260
+ /**
261
+ * The legible kind-mismatch verdict for a weight: a signed weight would
262
+ * break the polarity scheduler (an insert could lower a sum), so the
263
+ * illegal weight is unrepresentable, not checked.
264
+ */
265
+ interface WeightKindMismatch<K, Want> {
266
+ readonly "weight kind mismatch — weigh(field) reads a u64 field, weigh(duration(field)) an interval field, of the SOURCE row": readonly [
267
+ K,
268
+ Want
269
+ ]
270
+ }
271
+
272
+ /**
273
+ * The weight source wall (type tier): `weigh(K)`'s field must be a
274
+ * u64-encoded position of the SOURCE face's own row (signed encodings are
275
+ * the typed polarity refusal), `weigh(duration(K))`'s an interval
276
+ * position. Checked at the `capacity()` call where the source face is
277
+ * inferred — the proven constrain-after-inference pattern.
278
+ */
279
+ type WeightOnSource<M extends CapacityWeight, A extends AnyFace> = M["weight"] extends {
280
+ readonly kind: "field"
281
+ readonly field: infer K extends string
282
+ }
283
+ ? string extends K
284
+ ? unknown
285
+ : K extends FaceFields<A["source"]>
286
+ ? KindAt<A["source"], K> extends "u64"
287
+ ? unknown
288
+ : WeightKindMismatch<K, "u64">
289
+ : WeightOffSourceRoster<K, FaceFields<A["source"]>>
290
+ : M["weight"] extends { readonly kind: "durationField"; readonly field: infer K extends string }
291
+ ? string extends K
292
+ ? unknown
293
+ : K extends FaceFields<A["source"]>
294
+ ? KindAt<A["source"], K> extends "interval"
295
+ ? unknown
296
+ : WeightKindMismatch<K, "interval">
297
+ : WeightOffSourceRoster<K, FaceFields<A["source"]>>
298
+ : unknown
299
+
300
+ /** The unit weight — a case, not an absence (C4): the count instance's one wire spelling. */
301
+ const unitWeight: WeightSpec = Object.freeze({ kind: "unit" })
302
+
303
+ /** The construction-tier twin of {@link PathBan}: a dotted name refuses naming the idiom. */
304
+ function assertRowLocal(field: string, role: string): string {
305
+ if (field.includes(".")) {
306
+ throw errors.new(
307
+ `${role} \`${field}\` walks a reference — the vocabulary is closed at the row (ruling 6): pin the column with a two-column containment (Source(ref, f) <= Catalog(id, f)) and name the local field`
308
+ )
309
+ }
310
+ return field
311
+ }
312
+
313
+ /** Stamps one admitted window spec as a frozen branded value. */
314
+ function admitWindow<S extends CapacityWindowSpec>(window: S): CapacityWindow<S> {
315
+ const value: CapacityWindow<S> = { window, [admitted]: true }
316
+ return Object.freeze(value)
317
+ }
318
+
319
+ /** One literal bound. */
320
+ function lit(value: bigint): CapacityBoundSpec {
321
+ return Object.freeze({ kind: "lit", value })
322
+ }
323
+
324
+ /**
325
+ * `{n}` — THE exact-measure spelling; `within(0n)` IS the exclusion's one
326
+ * spelling on the unit instance, and the weaker "total is zero" law on a
327
+ * weighted one (zero-weight rows may exist — design § 6, stated loudly).
328
+ */
329
+ function within<const N extends bigint>(
330
+ n: N & NegativeBan<N>
331
+ ): CapacityWindow<{ readonly kind: "exact"; readonly n: { readonly kind: "lit"; readonly value: N } }>
332
+ /**
333
+ * `{lo..*}` — a floor with no ceiling. `within(0n, "*")` is vacuous
334
+ * (unwritable as a literal); `within(1n, "*")` constructs — the `{1..*}`
335
+ * ban is weight-sensitive and judged at the `capacity()` call (unit
336
+ * instance only).
337
+ */
338
+ function within<const Lo extends bigint>(
339
+ lo: Lo & FloorBan<Lo>,
340
+ hi: "*"
341
+ ): CapacityWindow<{ readonly kind: "floor"; readonly lo: { readonly kind: "lit"; readonly value: Lo } }>
342
+ /**
343
+ * `{lo..field}` / `{lo..Duration(field)}` — the dependent ceiling, read
344
+ * from the TARGET row per group (C6: hi slot only). The ref carries no
345
+ * value, so no inversion judgment exists at construction — a per-row
346
+ * inverted window is the judge's typed refusal.
347
+ */
348
+ function within<const Lo extends bigint, const R extends FieldRef | DurationRef>(
349
+ lo: Lo & NegativeBan<Lo>,
350
+ hi: R
351
+ ): CapacityWindow<{
352
+ readonly kind: "range"
353
+ readonly lo: { readonly kind: "lit"; readonly value: Lo }
354
+ readonly hi: R
355
+ }>
356
+ /**
357
+ * `{lo..hi}` — both bounds explicit, lo < hi; `within(0n, hi)` is the
358
+ * canonical ceiling. `lo === hi` is the exact measure respelled
359
+ * (unwritable as literals — {@link RangeBan} names `within(n)`), rejected
360
+ * at construction when computed; an inverted window is unsatisfiable and
361
+ * rejected at construction (bigint literals carry no type-level order).
362
+ */
363
+ function within<const Lo extends bigint, const Hi extends bigint>(
364
+ lo: Lo,
365
+ hi: Hi & RangeBan<Lo, Hi>
366
+ ): CapacityWindow<{
367
+ readonly kind: "range"
368
+ readonly lo: { readonly kind: "lit"; readonly value: Lo }
369
+ readonly hi: { readonly kind: "lit"; readonly value: Hi }
370
+ }>
371
+ function within(lo: bigint, hi?: bigint | "*" | FieldRef | DurationRef): CapacityWindow {
372
+ if (lo < 0n) {
373
+ throw errors.new(`capacity bounds are u64: within(${lo}${hi === undefined ? "" : ", …"}) is out of domain`)
374
+ }
375
+ if (hi === undefined) {
376
+ return admitWindow({ kind: "exact", n: lit(lo) })
377
+ }
378
+ if (hi === "*") {
379
+ if (lo === 0n) {
380
+ throw errors.new("the `{0..*}` window is vacuous — it provably says nothing; delete the statement")
381
+ }
382
+ return admitWindow({ kind: "floor", lo: lit(lo) })
383
+ }
384
+ if (typeof hi === "bigint") {
385
+ if (hi < 0n) {
386
+ throw errors.new(`capacity bounds are u64: within(${lo}, ${hi}) is out of domain`)
387
+ }
388
+ if (hi < lo) {
389
+ throw errors.new(
390
+ `the window \`{${lo}..${hi}}\` is inverted — no measure satisfies it; bounds are \`{lo..hi}\` with lo < hi (an exact measure is \`{n}\`: within(n))`
391
+ )
392
+ }
393
+ if (lo === hi) {
394
+ if (lo === 0n) {
395
+ throw errors.new("`{0..0}` — the point window is written `{0}`: use within(0n)")
396
+ }
397
+ throw errors.new(`\`{${lo}..${lo}}\` — an exact measure is written \`{${lo}}\`: use within(${lo}n)`)
398
+ }
399
+ return admitWindow({ kind: "range", lo: lit(lo), hi: lit(hi) })
400
+ }
401
+ const field = assertRowLocal(hi.field, "dependent bound")
402
+ if (hi.kind === "durationField") {
403
+ return admitWindow({ kind: "range", lo: lit(lo), hi: Object.freeze({ kind: "durationField", field }) })
404
+ }
405
+ return admitWindow({ kind: "range", lo: lit(lo), hi: Object.freeze({ kind: "field", field }) })
406
+ }
407
+
408
+ /** Stamps one admitted weight spec as a frozen branded value. */
409
+ function admitWeight<S extends WeightSpec>(weight: S): CapacityWeight<S> {
410
+ const value: CapacityWeight<S> = { weight, [admitted]: true }
411
+ return Object.freeze(value)
412
+ }
413
+
414
+ /**
415
+ * `[field]` — the measure: a u64-encoded field of the SOURCE row summed
416
+ * per target group. A dotted path is the typed refusal naming the
417
+ * pinned-column idiom (ruling 6); `weigh(duration(field))` is the
418
+ * interval-measure weight — calendar capacity as one statement.
419
+ */
420
+ function weigh<const F extends string>(
421
+ field: F & PathBan<F>
422
+ ): CapacityWeight<{ readonly kind: "field"; readonly field: F }>
423
+ function weigh<const F extends string>(
424
+ measure: DurationRef<F>
425
+ ): CapacityWeight<{ readonly kind: "durationField"; readonly field: F }>
426
+ function weigh(measure: string | DurationRef): CapacityWeight {
427
+ if (typeof measure === "string") {
428
+ return admitWeight({ kind: "field", field: assertRowLocal(measure, "weight") })
429
+ }
430
+ return admitWeight({ kind: "durationField", field: assertRowLocal(measure.field, "weight") })
431
+ }
432
+
433
+ /**
434
+ * A dependent bound by name — `within(0n, ref("supply"))` reads each
435
+ * group's ceiling from the TARGET row's u64 field (bound names resolve
436
+ * against the target's FULL roster, C1 — the projection tuple stays the
437
+ * pure grouping key). Carries no value.
438
+ */
439
+ function ref<const F extends string>(field: F & PathBan<F>): FieldRef<F> {
440
+ return Object.freeze({ kind: "field", field: assertRowLocal(field, "dependent bound") }) as FieldRef<F>
441
+ }
442
+
443
+ /**
444
+ * `Duration(field)` — the interval-measure spelling, one mint for both
445
+ * slots: handed to `weigh()` it is the SOURCE row's interval measure;
446
+ * in `within()`'s hi slot it is the TARGET row's interval-measure bound
447
+ * (Duration weights pair with Duration-capable bounds — C18).
448
+ */
449
+ function duration<const F extends string>(field: F & PathBan<F>): DurationRef<F> {
450
+ return Object.freeze({ kind: "durationField", field: assertRowLocal(field, "Duration measure") }) as DurationRef<F>
451
+ }
452
+
453
+ export type { BoundsOnTarget, CapacityWeight, CapacityWindow, DurationRef, FieldRef, UnitWindowBan, WeightOnSource }
454
+ export { duration, isCapacityWeight, isCapacityWindow, ref, unitWeight, weigh, within }
package/src/db.ts CHANGED
@@ -117,8 +117,10 @@ interface OffendingFact<Rels extends SchemaRelations> {
117
117
  * orientation (identical strings; the engine's `render.rs` renders each
118
118
  * partner of a mirrored pair as the `==` spelling, never a bare `<=`
119
119
  * direction). `direction` (`sourceUnsatisfied` | `targetRequired`) and
120
- * `count` are the containment/window form payloads, passed through from
121
- * the engine VERBATIM `direction` is relative to the violated SLOT's
120
+ * `measure` are the containment/capacity form payloads, passed through
121
+ * from the engine VERBATIM (`measure` the capacity form's witnessed group
122
+ * total — u128-wide, crossing whole as bigint, C3) — `direction` is
123
+ * relative to the violated SLOT's
122
124
  * own orientation, so for a `mirrors` statement it alone cannot say which
123
125
  * side of the `==` was violated: the slot identity is carried by
124
126
  * `orientation`, present exactly for `mirrors` slots — `written` is the
@@ -131,7 +133,7 @@ interface Violation<Rels extends SchemaRelations> {
131
133
  readonly canonical: string
132
134
  readonly direction?: "sourceUnsatisfied" | "targetRequired"
133
135
  readonly orientation?: "written" | "mirrored"
134
- readonly count?: bigint
136
+ readonly measure?: bigint
135
137
  readonly facts: readonly OffendingFact<Rels>[]
136
138
  }
137
139
 
@@ -536,10 +538,10 @@ function impliedKeyEntries(theory: AnySchema): StatementEntry[] {
536
538
  }
537
539
 
538
540
  /**
539
- * One declared statement's materialized slots: a key or window occupies
540
- * one, a `mirrors` occupies two adjacent slots (the engine lowers `==` to
541
- * two containments, `source <= target` first), both owned by the one SDK
542
- * value.
541
+ * One declared statement's materialized slots: a key or capacity statement
542
+ * occupies one, a `mirrors` occupies two adjacent slots (the engine lowers
543
+ * `==` to two containments, `source <= target` first), both owned by the
544
+ * one SDK value.
543
545
  */
544
546
  function declaredEntries(statement: Statement): StatementEntry[] {
545
547
  const data = statement.data
@@ -562,8 +564,8 @@ function declaredEntries(statement: Statement): StatementEntry[] {
562
564
  }
563
565
  return [{ kind: "containment", statement, key: undefined }]
564
566
  }
565
- case "window": {
566
- return [{ kind: "cardinality", statement, key: undefined }]
567
+ case "capacity": {
568
+ return [{ kind: "capacity", statement, key: undefined }]
567
569
  }
568
570
  }
569
571
  }
@@ -889,7 +891,7 @@ function openDb<Rels extends SchemaRelations>(handle: DbHandle, theory: Schema<R
889
891
  canonical: wire.canonical,
890
892
  direction: wire.direction,
891
893
  orientation: orientationOf(entry.reversed),
892
- count: wire.count,
894
+ measure: wire.measure,
893
895
  facts: Object.freeze(wire.facts.map(offendingFactOf))
894
896
  })
895
897
  }
@@ -912,7 +914,7 @@ function openDb<Rels extends SchemaRelations>(handle: DbHandle, theory: Schema<R
912
914
  )
913
915
  }
914
916
  if (entry.kind !== "functionality" || entry.key === undefined) {
915
- throw errors.new("keyed get takes a key() statement — containments and windows key nothing")
917
+ throw errors.new("keyed get takes a key() statement — containments and capacity statements key nothing")
916
918
  }
917
919
  if (entry.key.owner !== relation.name) {
918
920
  throw errors.new(
package/src/face.ts CHANGED
@@ -322,6 +322,7 @@ export type {
322
322
  FaceShapes,
323
323
  FaceSource,
324
324
  OwnerOf,
325
+ ProjectedShape,
325
326
  SameArity,
326
327
  SameShapes
327
328
  }
package/src/index.ts CHANGED
@@ -25,6 +25,16 @@
25
25
  * `.slice`). The raw native bridge is not exported.
26
26
  */
27
27
 
28
+ export type {
29
+ BoundsOnTarget,
30
+ CapacityWeight,
31
+ CapacityWindow,
32
+ DurationRef,
33
+ FieldRef,
34
+ UnitWindowBan,
35
+ WeightOnSource
36
+ } from "#capacity.ts"
37
+ export { duration, ref, weigh, within } from "#capacity.ts"
28
38
  export type {
29
39
  AnyClosed,
30
40
  AnySelectedClosed,
@@ -40,8 +50,6 @@ export type {
40
50
  SelectedClosed
41
51
  } from "#closed.ts"
42
52
  export { closed } from "#closed.ts"
43
- export type { Count } from "#count.ts"
44
- export { atLeast, atMost, between, exactly, none } from "#count.ts"
45
53
  export type {
46
54
  Abandon,
47
55
  AbandonedArm,
@@ -81,6 +89,7 @@ export type {
81
89
  FaceShapes,
82
90
  FaceSource,
83
91
  OwnerOf,
92
+ ProjectedShape,
84
93
  SameArity,
85
94
  SameShapes
86
95
  } from "#face.ts"
@@ -110,7 +119,7 @@ export type {
110
119
  Staleness,
111
120
  StatementKindTag
112
121
  } from "#native.ts"
113
- export type { Desc, SortKey } from "#order.ts"
122
+ export type { Desc, EngineOrderable, SortKey } from "#order.ts"
114
123
  export { by, desc } from "#order.ts"
115
124
 
116
125
  export type {
@@ -181,6 +190,8 @@ export { relation } from "#relation.ts"
181
190
  export type { AnySchema, Schema, SchemaRelation, SchemaRelations } from "#schema.ts"
182
191
  export { schema } from "#schema.ts"
183
192
  export type {
193
+ CapacityBoundSpec,
194
+ CapacityWindowSpec,
184
195
  FieldSpec,
185
196
  LiteralSetSpec,
186
197
  LiteralSpec,
@@ -191,17 +202,17 @@ export type {
191
202
  StatementSpec,
192
203
  ValueSpec,
193
204
  ValueTypeSpec,
194
- WindowSpec
205
+ WeightSpec
195
206
  } from "#spec.ts"
196
- export { renderLiteral, renderLiteralSet, renderWindow } from "#spec.ts"
207
+ export { renderCapacityBound, renderCapacityWindow, renderLiteral, renderLiteralSet, renderWeight } from "#spec.ts"
197
208
  export type {
209
+ CapacityData,
210
+ CapacityStatement,
198
211
  ContainedStatement,
199
212
  ContainmentData,
200
213
  KeyData,
201
214
  KeyStatement,
202
215
  Statement,
203
- StatementData,
204
- WindowData,
205
- WindowStatement
216
+ StatementData
206
217
  } from "#statements.ts"
207
- export { contained, key, mirrors, renderStatement, window } from "#statements.ts"
218
+ export { capacity, contained, key, mirrors, renderStatement } from "#statements.ts"
package/src/law.ts CHANGED
@@ -33,7 +33,7 @@
33
33
  *
34
34
  * Every paired face of the statement tuple unions its positionwise field
35
35
  * slots: containment (ψ-selected targets included — a selection changes
36
- * pairing not at all), the `==` bijection, and window source/target pairs.
36
+ * pairing not at all), the `==` bijection, and capacity source/target pairs.
37
37
  * `key()` statements pair nothing (an FD constrains one relation's own
38
38
  * rows; it identifies no carriers).
39
39
  *
@@ -141,7 +141,8 @@ type ZipCoords<
141
141
 
142
142
  /**
143
143
  * One statement's slot pairs: containments (bidirectional included — pair
144
- * unions are symmetric) and windows pair their two faces positionwise;
144
+ * unions are symmetric) and capacity statements pair their two faces
145
+ * positionwise;
145
146
  * `key()` pairs nothing. A widened face (owner name or projection no
146
147
  * longer literal) contributes nothing — the runtime map stays complete.
147
148
  */
package/src/lower.ts CHANGED
@@ -98,10 +98,11 @@ function lowerStatement(statement: Statement): StatementSpec {
98
98
  target: lowerFace(data.target),
99
99
  bidirectional: data.bidirectional
100
100
  }
101
- case "window":
101
+ case "capacity":
102
102
  return {
103
- kind: "cardinality",
103
+ kind: "capacity",
104
104
  target: lowerFace(data.target),
105
+ weight: data.weight,
105
106
  window: data.window,
106
107
  source: lowerFace(data.source)
107
108
  }
package/src/native.ts CHANGED
@@ -175,7 +175,7 @@ type ConditionTreeIr =
175
175
  | { readonly kind: "or"; readonly children: readonly ConditionTreeIr[] }
176
176
 
177
177
  /** A statement's form tag. */
178
- type StatementKindTag = "functionality" | "containment" | "cardinality"
178
+ type StatementKindTag = "functionality" | "containment" | "capacity"
179
179
 
180
180
  /** One field's name, dense id, and structural type. */
181
181
  interface ManifestField {
@@ -231,14 +231,17 @@ interface ViolationFact {
231
231
  * One violated statement of a rejected commit, rendered to plain data: the
232
232
  * statement id (materialized order), form tag, CANONICAL spelling (the
233
233
  * engine's one renderer — a bijection on legal statements, paste-back-able),
234
- * the form's direction/count payloads, and the decoded offending facts.
234
+ * the form's direction/measure payloads, and the decoded offending facts.
235
+ * `measure` is the capacity form's witnessed group total — the engine
236
+ * accumulates in u128 and the value crosses WHOLE as bigint (C3:
237
+ * truncation is unrepresentable).
235
238
  */
236
239
  interface Violation {
237
240
  readonly statementId: number
238
241
  readonly kind: StatementKindTag
239
242
  readonly canonical: string
240
243
  readonly direction?: "sourceUnsatisfied" | "targetRequired"
241
- readonly count?: bigint
244
+ readonly measure?: bigint
242
245
  readonly facts: readonly ViolationFact[]
243
246
  }
244
247