@mnmal-ai/substrate 1.0.0-alpha.2 → 1.0.0-alpha.20

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 (53) hide show
  1. package/README.md +64 -14
  2. package/dist/boot-store-BaDqbBS-.js +4062 -0
  3. package/dist/{runtime-metrics-DwpDJ8vz.d.ts → bridge-BhiJKwfF.d.ts} +2 -87
  4. package/dist/cli-RRFphMtQ.js +3366 -0
  5. package/dist/client.browser.js +2485 -0
  6. package/dist/client.d.ts +273 -135
  7. package/dist/client.js +6 -6
  8. package/dist/cluster-worker.js +0 -1
  9. package/dist/{content-type-C7T28xNb.js → content-type-wLljot5M.js} +0 -1
  10. package/dist/{env-utils-CGNazUNt.js → env-utils-s25Z1YSA.js} +0 -1
  11. package/dist/{errors-D4vKuwz8.d.ts → errors-CYOaWZ8l.d.ts} +44 -4
  12. package/dist/{fake-DUVtKVSE.js → fake-DdXtb-m8.js} +0 -1
  13. package/dist/{index-D7mSOFlM.d.ts → index-ZKeYFHDD.d.ts} +7 -11
  14. package/dist/msgpackr-Dq18g6Ds.js +16 -0
  15. package/dist/node-version-BtK_Nq6z.js +32 -0
  16. package/dist/pack.d.ts +75 -11
  17. package/dist/pack.js +4 -4
  18. package/dist/pg-edges-store-C-IRv-O9.js +443 -0
  19. package/dist/{plugin-QuJN9pG_.js → plugin-MSbSZ6Al.js} +132 -8
  20. package/dist/proposals-bootstrap.js +6 -5
  21. package/dist/proposals.config.schema.json +5 -0
  22. package/dist/proposals.d.ts +17 -5
  23. package/dist/proposals.default-config.json +2 -1
  24. package/dist/proposals.js +2 -3
  25. package/dist/recall-bootstrap.js +5 -4
  26. package/dist/recall.config.schema.json +6 -0
  27. package/dist/recall.d.ts +35 -6
  28. package/dist/recall.js +2 -3
  29. package/dist/{repl-C52wyfMs.js → repl-Ob5OyQqj.js} +65 -33
  30. package/dist/{row-ByLFE2Wx.d.ts → row-CfsHSEfB.d.ts} +92 -12
  31. package/dist/run-B438GGJK.js +2435 -0
  32. package/dist/{plugin-t2g2wX0b.d.ts → schema-laGLMvMC.d.ts} +1287 -63
  33. package/dist/script-A9dxMRZA.js +143 -0
  34. package/dist/script-runner.js +77 -0
  35. package/dist/sdk.d.ts +227 -38
  36. package/dist/sdk.js +5 -110
  37. package/dist/semver-D_zdq0hi.js +17 -0
  38. package/dist/{src-DMzc3HfW.js → src-BNdhH_Hx.js} +745 -851
  39. package/dist/{src-CG7giuHr.js → src-BSL2yyVy.js} +779 -75
  40. package/dist/{src-BLgbLq2k.js → src-B_kHkbsr.js} +780 -330
  41. package/dist/{src-DWm0iA88.js → src-De9j33jv.js} +188 -87
  42. package/dist/src-DkxrpQOz.js +7709 -0
  43. package/dist/{row-DZ0Fnpc6.js → src-DmiUWOLp.js} +2 -1
  44. package/dist/{src-CHp3QyqB.js → src-S84ef4e3.js} +9177 -3377
  45. package/dist/substrate-admin.js +17 -782
  46. package/dist/substrate.js +4 -5487
  47. package/dist/testing.d.ts +730 -53
  48. package/dist/testing.js +20 -163
  49. package/dist/{ws-C72iVS5T.d.ts → ws-BT8kL1IF.d.ts} +4 -5
  50. package/package.json +26 -26
  51. package/dist/dataset-BQ8egGl5.js +0 -1535
  52. package/dist/schema-g4FQLr0V.d.ts +0 -433
  53. package/dist/src-CXKnqeTW.js +0 -2339
@@ -1,4 +1,359 @@
1
- import { C as Id, E as Row, _ as EdgesStore, b as ActorIdentity, l as PackEnvelope, n as FieldDef, u as Schema } from "./schema-g4FQLr0V.js";
1
+ //#region ../../packages/substrate-types/src/core.d.ts
2
+ /**
3
+ * Core substrate-neutral types. Spec: §4.5 Dataset, §4.6 Edge.
4
+ */
5
+ type Id = string | number;
6
+ interface Row {
7
+ id: Id;
8
+ _deletedAt?: Date | null;
9
+ /**
10
+ * Server-managed row-lifecycle metadata, set by autoCRUD on create/update
11
+ * and projected on every read alongside `id`. Each dataset stores it in
12
+ * its own way.
13
+ */
14
+ _metadata?: RowMetadata;
15
+ /** Per-row walk aggregates (`params.agg`). */
16
+ _agg?: Record<string, unknown>;
17
+ /** Nested selections whose cap was hit, by name. */
18
+ _truncated?: Record<string, true>;
19
+ [field: string]: unknown;
20
+ }
21
+ /**
22
+ * A row as passed to `Dataset.write`. `id` may be omitted: the dataset then
23
+ * assigns a version-7 UUID and returns it in `WriteResult.ids`.
24
+ */
25
+ interface WriteRow {
26
+ id?: Id;
27
+ _deletedAt?: Date | null;
28
+ _metadata?: RowMetadata;
29
+ [field: string]: unknown;
30
+ }
31
+ /**
32
+ * Refuse an upsert batch that names one id twice: the datasets would
33
+ * otherwise disagree (pg cannot update a row twice in one statement).
34
+ */
35
+ declare function assertNoRepeatedIds(type: string, rows: readonly WriteRow[]): void;
36
+ type IdentityKind = 'agent' | 'user' | 'system';
37
+ /**
38
+ * Row-attribution actor — records who created or last updated a row.
39
+ * Distinct from the auth `Identity` in `policy.ts` (JWT principal).
40
+ */
41
+ interface ActorIdentity {
42
+ readonly kind: IdentityKind;
43
+ readonly id: string;
44
+ }
45
+ interface RowMetadata {
46
+ readonly createdAt: string | null;
47
+ readonly updatedAt: string | null;
48
+ readonly createdBy: ActorIdentity | null;
49
+ readonly updatedBy: ActorIdentity | null;
50
+ /**
51
+ * The row's place in its type's commit order, on a type declared
52
+ * `ordering: 'visible'`; absent on every other type. Assigned on insert only.
53
+ */
54
+ readonly seq?: number;
55
+ }
56
+ declare const CLIENT_SUPPLIED_SERVER_FIELD: 'client_supplied_server_field';
57
+ type ClientSuppliedServerFieldError = typeof CLIENT_SUPPLIED_SERVER_FIELD;
58
+ declare const ROW_METADATA_FIELDS: readonly ['createdAt', 'updatedAt', 'createdBy', 'updatedBy'];
59
+ type RowMetadataField = (typeof ROW_METADATA_FIELDS)[number];
60
+ //#endregion
61
+ //#region ../../packages/substrate-types/src/edge.d.ts
62
+ type OnDelete = 'set_null' | 'cascade' | 'restrict' | 'orphan';
63
+ type OnMissing = 'reject' | 'allow';
64
+ interface EdgeRef {
65
+ ns: string;
66
+ type: string;
67
+ id: Id;
68
+ bridged?: boolean;
69
+ }
70
+ interface Edge {
71
+ id: string;
72
+ from: EdgeRef;
73
+ to: EdgeRef;
74
+ fieldName: string;
75
+ policy: OnDelete;
76
+ createdAt: Date;
77
+ deletedAt?: Date;
78
+ lastSweptAt?: Date;
79
+ blocked?: boolean;
80
+ }
81
+ /**
82
+ * Narrow patch type for `EdgesStore.update`. Immutable fields (from, to,
83
+ * policy, fieldName, createdAt, id-as-identity) cannot be changed post-write.
84
+ * `deletedAt` is managed exclusively by `tombstone(id)`.
85
+ */
86
+ type EdgePatch = Pick<Edge, 'id'> & Partial<Pick<Edge, 'lastSweptAt' | 'blocked'>>;
87
+ type EdgeWriteInput = Omit<Edge, 'id' | 'createdAt' | 'lastSweptAt' | 'deletedAt' | 'blocked'>;
88
+ type CanDeleteResult = {
89
+ ok: true;
90
+ } | {
91
+ ok: false;
92
+ blockedBy: Edge[];
93
+ };
94
+ interface EdgesStore {
95
+ /**
96
+ * Stable identifier for the storage substrate this edges store wraps.
97
+ * Parallels `Dataset.txScopeId` (spec §5.3). When an `EdgesStore`
98
+ * and a `Dataset` share a `txScopeId`, the engine enlists edge
99
+ * mutations into the dataset's `withScopedTransaction`, making
100
+ * row-tombstone + edge-tombstone within one frame atomic.
101
+ *
102
+ * Convention:
103
+ * - pg: derived from the `postgres.Sql` pool reference identity. A
104
+ * `PgEdgesStore` constructed against the same `Sql` as a
105
+ * `PgDataset` advertises the same `txScopeId`.
106
+ * - memory: unique per `MemoryEdgesStore` instance. A memory edges
107
+ * store can never coalesce into a dataset's substrate tx; the engine
108
+ * falls back to nested independent transactions for that case.
109
+ */
110
+ readonly txScopeId: string;
111
+ write(input: EdgeWriteInput): Promise<Edge>;
112
+ /**
113
+ * `write` for many inputs, returned in input order (pg: one multi-row
114
+ * INSERT whatever the count). Empty `inputs` returns `[]` and sends nothing.
115
+ */
116
+ writeMany(inputs: readonly EdgeWriteInput[]): Promise<Edge[]>;
117
+ tombstone(id: string, at?: Date): Promise<void>;
118
+ /**
119
+ * `tombstone` for many ids with one stamp (pg: one statement). Unknown and
120
+ * already-tombstoned ids are skipped, the latter keeping their first stamp.
121
+ * Empty `ids` does nothing and sends nothing.
122
+ */
123
+ tombstoneMany(ids: readonly string[], at?: Date): Promise<void>;
124
+ update(patch: EdgePatch): Promise<void>;
125
+ readNextBatch(cursorId: string | null, size: number): Promise<{
126
+ edges: Edge[];
127
+ lastId: string | null;
128
+ }>;
129
+ findEdgesTo(ref: EdgeRef): Promise<Edge[]>;
130
+ /**
131
+ * Batched `findEdgesTo`: all live (non-tombstoned) inbound edges whose `to`
132
+ * matches ANY of `refs`, in a single round-trip. Empty `refs` yields an
133
+ * empty result. Callers group the flat result by `edge.to` themselves.
134
+ */
135
+ findEdgesToMany(refs: readonly EdgeRef[]): Promise<Edge[]>;
136
+ findEdgesFrom(ref: EdgeRef): Promise<Edge[]>;
137
+ /**
138
+ * Batched `findEdgesFrom`: all live outbound edges whose `from` matches ANY
139
+ * of `refs`, in a single round-trip, as `findEdgesToMany` does for `to`.
140
+ */
141
+ findEdgesFromMany(refs: readonly EdgeRef[]): Promise<Edge[]>;
142
+ canDelete(ref: EdgeRef): Promise<CanDeleteResult>;
143
+ findBlocked(): Promise<Edge[]>;
144
+ /**
145
+ * List live (non-tombstoned) edges where `from_ns === namespace` OR
146
+ * `to_ns === namespace`. Used by drop-namespace to detect dangling refs
147
+ * when `cascade === 'refuse'`. Bounded by `limit` — callers only need
148
+ * enough to decide + surface a sample.
149
+ */
150
+ findEdgesTouchingNamespace(namespace: string, limit: number): Promise<Edge[]>;
151
+ /**
152
+ * List live (non-tombstoned) edges where either endpoint matches
153
+ * `(namespace, type)`. Used by drop-type for the same cascade-refuse check.
154
+ */
155
+ findEdgesTouchingType(namespace: string, type: string, limit: number): Promise<Edge[]>;
156
+ /**
157
+ * Distinct namespaces other than `namespace` at the far end of any edge
158
+ * (tombstoned or live) touching `namespace`, or `(namespace, type)` when
159
+ * `type` is given: the namespaces `dropEdgesByNamespace` / `dropEdgesByType`
160
+ * would delete edges from.
161
+ */
162
+ namespacesLinkedTo(namespace: string, type?: string): Promise<string[]>;
163
+ /**
164
+ * Physically delete every edge (tombstoned or live) where `from_ns === namespace`
165
+ * OR `to_ns === namespace`. Used by drop-namespace when `cascade === 'delete'`.
166
+ * Returns the exact count physically removed.
167
+ *
168
+ * Same atomicity rules as `tombstone` — MUST run inside a caller-supplied
169
+ * `withTransaction` for multi-store atomicity.
170
+ */
171
+ dropEdgesByNamespace(namespace: string, within?: readonly string[]): Promise<number>;
172
+ /**
173
+ * Physically delete every edge (tombstoned or live) where either endpoint
174
+ * matches `(namespace, type)`. Used by drop-type when `cascade === 'delete'`.
175
+ * `within` restricts both drops to edges whose far end is `namespace` or one
176
+ * of the listed namespaces.
177
+ */
178
+ dropEdgesByType(namespace: string, type: string, within?: readonly string[]): Promise<number>;
179
+ /**
180
+ * Execute `fn` atomically. All ops performed on the `tx` argument run inside
181
+ * one substrate transaction. On throw, every mutation inside the callback
182
+ * rolls back; on resolve, the transaction commits and the value is returned.
183
+ *
184
+ * Nested calls flatten into the outer tx (savepoints out of scope).
185
+ * Cross-store atomicity with a sibling `Dataset.withTransaction` is the
186
+ * engine's concern; each store manages its own tx independently here.
187
+ * For enlisted cross-store atomicity, see `Dataset.withScopedTransaction`
188
+ * which yields a tx-bound `EdgesStore` alongside the tx-bound datasets.
189
+ *
190
+ * Mirrors `Dataset.withTransaction` (§4.5, G1 §5.3).
191
+ */
192
+ withTransaction<T>(fn: (tx: EdgesStore) => Promise<T>): Promise<T>;
193
+ }
194
+ //#endregion
195
+ //#region ../../packages/substrate-types/src/expression.d.ts
196
+ /**
197
+ * Typed expressions for the read grammar: the JSON form, the type lattice, the
198
+ * curated function catalogue and the size limits.
199
+ *
200
+ * An expression is a bare string (a field, or `_agg.<name>`), a number, boolean
201
+ * or `null` literal, `{ lit: "text" }` (string literal), `{ int64: "<decimal>" }`
202
+ * (exact int64 constant) or `{ <fn>: [args...] }` (a call, one key).
203
+ */
204
+ type Expr = string | number | boolean | null | {
205
+ lit: string;
206
+ } | {
207
+ int64: string;
208
+ } | {
209
+ [fn: string]: Expr[];
210
+ };
211
+ type ExprType = 'number' | 'int64' | 'string' | 'boolean' | 'date';
212
+ /**
213
+ * An argument slot. `'any'` slots unify to one type (int64 and number compare
214
+ * as number); `'unit'` is a `{ lit }` date unit, `'pattern'` a `{ lit }`
215
+ * regex and `'likePattern'` a `{ lit }` LIKE pattern, all literal-only.
216
+ */
217
+ type ArgType = ExprType | 'any' | 'unit' | 'pattern' | 'likePattern';
218
+ interface FunctionSig {
219
+ name: string;
220
+ /** Fixed argument slots, or `'variadic'` when the checker applies the function's own rules. */
221
+ args: ArgType[] | 'variadic';
222
+ /** `'any'` is the unified type of the `'any'` (and, for arithmetic, `'number'`) arguments. */
223
+ returns: ExprType | 'any';
224
+ description: string;
225
+ }
226
+ declare const EXPR_FUNCTIONS: readonly FunctionSig[];
227
+ declare const EXPR_LIMITS: {
228
+ maxDepth: number;
229
+ maxNodesPerExpr: number;
230
+ maxNodesPerFrame: number;
231
+ };
232
+ /** The aggregate names `_agg`, `having` and grouped reads accept. */
233
+ declare const EXPR_AGGREGATES: readonly ['count', 'countDistinct', 'sum', 'avg', 'min', 'max'];
234
+ declare const DATE_UNITS: readonly ['second', 'minute', 'hour', 'day', 'week', 'month', 'year'];
235
+ //#endregion
236
+ //#region ../../packages/substrate-types/src/runtime-metrics.d.ts
237
+ /** Per-key resolver latency and error counts, as served by GET /substrate/v1/metrics. */
238
+ interface ResolverStat {
239
+ kind: 'query' | 'mutate';
240
+ calls: number;
241
+ errors: number;
242
+ p50Ms: number;
243
+ p99Ms: number;
244
+ maxMs: number;
245
+ lastAt: string;
246
+ }
247
+ interface ProcessStat {
248
+ rssMB: number;
249
+ heapUsedMB: number;
250
+ cpuUserMs: number;
251
+ cpuSystemMs: number;
252
+ eventLoopLagP99Ms: number;
253
+ inFlight: number;
254
+ }
255
+ interface SlowRequest {
256
+ key: string;
257
+ kind: 'query' | 'mutate';
258
+ durationMs: number;
259
+ at: string;
260
+ }
261
+ interface RuntimeErrorEntry {
262
+ at: string;
263
+ op: 'query' | 'mutate';
264
+ key: string;
265
+ code: string;
266
+ message: string;
267
+ /** The verified identity's id, or `'anonymous'`. */
268
+ caller?: string;
269
+ /** Present when consecutive identical `(unknown)` failures were merged into this entry. */
270
+ count?: number;
271
+ }
272
+ interface RuntimeErrors {
273
+ since: string;
274
+ errors: RuntimeErrorEntry[];
275
+ }
276
+ interface ResolveObservation {
277
+ key: string;
278
+ kind: 'query' | 'mutate';
279
+ durationMs: number;
280
+ ok: boolean;
281
+ code?: string;
282
+ message?: string;
283
+ /** The verified identity's id; absent for an anonymous caller. */
284
+ caller?: string;
285
+ /**
286
+ * Set when the element ran in a batch resolver call: the number of elements
287
+ * in that call. `durationMs` is then the whole call's time, shared by all of
288
+ * them. Absent for a per-element resolve.
289
+ */
290
+ batchSize?: number;
291
+ /**
292
+ * The element's position (0..batchSize-1) in its batch call; set with
293
+ * `batchSize`. Latency aggregates take a batch once, from index 0, so one
294
+ * call's time is not counted batchSize times. Call counts take every element.
295
+ */
296
+ batchIndex?: number;
297
+ }
298
+ /**
299
+ * One type's table and index statistics, for the index advisor. Counters are
300
+ * cumulative since the server's statistics were last reset. Row counts are
301
+ * estimates. A type ABSENT from a `typeStats()` result is unknown, not empty:
302
+ * never read its absence as 0 rows (a sampled store can miss a small type).
303
+ */
304
+ interface TypeStats {
305
+ namespace: string;
306
+ type: string;
307
+ liveRows: number;
308
+ insertsTotal: number;
309
+ updatesTotal: number;
310
+ deletesTotal: number;
311
+ indexes: Array<{
312
+ name: string;
313
+ scans: number;
314
+ /** The last scan (ISO), or null when none since the statistics were last reset. */
315
+ lastScanAt: string | null;
316
+ sizeBytes: number;
317
+ unique: boolean;
318
+ /** Built or adopted by the engine's index reconciler for this type. */
319
+ engineOwned: boolean;
320
+ }>;
321
+ }
322
+ /** What GET /substrate/v1/metrics returns. The new fields are optional so an older server still type-checks. */
323
+ interface RuntimeMetrics {
324
+ since: string;
325
+ requests: {
326
+ query: {
327
+ total: number;
328
+ ok: number;
329
+ error: number;
330
+ durationMsSum: number;
331
+ durationMsMax: number;
332
+ };
333
+ mutate: {
334
+ total: number;
335
+ ok: number;
336
+ error: number;
337
+ durationMsSum: number;
338
+ durationMsMax: number;
339
+ };
340
+ };
341
+ subscriptions: {
342
+ active: number;
343
+ maxQueueDepth?: number;
344
+ entries?: {
345
+ id: string;
346
+ ns: string;
347
+ type: string;
348
+ queueLength: number;
349
+ }[];
350
+ };
351
+ events: Record<string, number>;
352
+ resolvers?: Record<string, ResolverStat>;
353
+ process?: ProcessStat;
354
+ slowest?: SlowRequest[];
355
+ }
356
+ //#endregion
2
357
  //#region ../../packages/substrate-types/src/dataset.d.ts
3
358
  /**
4
359
  * Handles yielded by `Dataset.withScopedTransaction`. Contains the
@@ -21,12 +376,12 @@ interface TxScopeHandles {
21
376
  * substrate layer — the validator enforces `null` only on those two operators.
22
377
  */
23
378
  interface FieldPredicate {
24
- eq?: number | string | boolean | Date | null;
25
- neq?: number | string | boolean | Date | null;
26
- gt?: number | string | Date;
27
- gte?: number | string | Date;
28
- lt?: number | string | Date;
29
- lte?: number | string | Date;
379
+ eq?: number | string | boolean | Date | null | RefOperand;
380
+ neq?: number | string | boolean | Date | null | RefOperand;
381
+ gt?: number | string | Date | RefOperand;
382
+ gte?: number | string | Date | RefOperand;
383
+ lt?: number | string | Date | RefOperand;
384
+ lte?: number | string | Date | RefOperand;
30
385
  between?: [number | string | Date, number | string | Date];
31
386
  in?: Array<number | string | boolean | Date>;
32
387
  nin?: Array<number | string | boolean | Date>;
@@ -49,8 +404,99 @@ type WhereClause = {
49
404
  } | {
50
405
  NOT: WhereClause;
51
406
  } | {
407
+ _walk: WalkNode;
408
+ } | WalkAggPredicate | {
409
+ _walk?: never;
410
+ _agg?: never;
52
411
  [field: string]: FieldPredicate | WhereClause | unknown;
53
412
  };
413
+ type WalkPick = {
414
+ latest: string;
415
+ } | {
416
+ max: string;
417
+ } | {
418
+ min: string;
419
+ };
420
+ interface WalkNode {
421
+ as?: string;
422
+ /** `'<ns>/<Type>.<field>'`: inbound edge. */
423
+ in?: string;
424
+ /** `'<field>'`: outbound edge. */
425
+ out?: string;
426
+ where?: WhereClause;
427
+ quantifier?: 'any' | 'none' | 'all';
428
+ pick?: WalkPick;
429
+ }
430
+ /** `'$.f'` | `'<as>.f'` | `'_agg.<name>'` */
431
+ interface RefOperand {
432
+ $ref: string;
433
+ times?: number;
434
+ plus?: number;
435
+ }
436
+ type AggFn = {
437
+ count: true;
438
+ } | {
439
+ avg: string;
440
+ } | {
441
+ sum: string;
442
+ } | {
443
+ min: string;
444
+ } | {
445
+ max: string;
446
+ } | {
447
+ latest: {
448
+ by: string;
449
+ value: string;
450
+ };
451
+ };
452
+ type WalkAgg = {
453
+ in?: string;
454
+ out?: string;
455
+ where?: WhereClause;
456
+ } & AggFn;
457
+ /** A frame aggregate: `count: true` counts rows; every other argument is an expression. */
458
+ type FrameAgg = {
459
+ count: Expr;
460
+ } | {
461
+ countDistinct: Expr;
462
+ } | {
463
+ avg: Expr;
464
+ } | {
465
+ sum: Expr;
466
+ } | {
467
+ min: Expr;
468
+ } | {
469
+ max: Expr;
470
+ };
471
+ interface NestedSelection {
472
+ _walk: {
473
+ in?: string;
474
+ out?: string;
475
+ where?: WhereClause;
476
+ };
477
+ fields: Record<string, unknown>;
478
+ where?: WhereClause;
479
+ orderBy?: OrderByClause[];
480
+ limit?: number;
481
+ }
482
+ /** An `_agg` comparison operand: a number, a date or string (by the aggregate's kind), or a `$ref`. */
483
+ type WalkAggOperand = number | string | RefOperand;
484
+ /** `{ _agg: <walk aggregate>, <cmp>: <operand> }` */
485
+ type WalkAggPredicate = {
486
+ _agg: WalkAgg;
487
+ } & ({
488
+ eq: WalkAggOperand;
489
+ } | {
490
+ neq: WalkAggOperand;
491
+ } | {
492
+ gt: WalkAggOperand;
493
+ } | {
494
+ gte: WalkAggOperand;
495
+ } | {
496
+ lt: WalkAggOperand;
497
+ } | {
498
+ lte: WalkAggOperand;
499
+ });
54
500
  /**
55
501
  * OrderBy clause. One entry per field, direction per field. Engine appends an
56
502
  * implicit `{ id: 'asc' }` when callers omit `id` as the final tie-breaker — a
@@ -60,23 +506,34 @@ interface OrderByClause {
60
506
  [field: string]: 'asc' | 'desc';
61
507
  }
62
508
  interface ReadParams {
509
+ /** Aborts the read. Substrates that can (PgDataset) cancel the in-flight statement; others MAY ignore it. */
510
+ signal?: AbortSignal;
63
511
  id?: Id;
64
512
  ids?: Id[];
65
513
  includeDeleted?: boolean;
66
514
  where?: WhereClause;
67
515
  orderBy?: OrderByClause[];
68
516
  limit?: number;
517
+ /** Return a match count (`ReadResult.count`). */
518
+ count?: boolean;
519
+ aggregate?: Record<string, FrameAgg>;
520
+ /** Group keys by output name; with `aggregate`, the read returns one group per distinct key tuple. */
521
+ groupBy?: Record<string, Expr>;
522
+ /** A boolean expression over group keys and aggregate names, applied before order and limit. */
523
+ having?: Expr;
524
+ /** Groups to skip, on a grouped read. */
525
+ offset?: number;
526
+ /** Computed per-row values by output name: each row gains one key per entry, which `orderBy` may use. */
527
+ select?: Record<string, Expr>;
528
+ /** Per-row walk aggregates, lifted out of `fields` by the list resolver. */
529
+ agg?: Record<string, WalkAgg>;
530
+ /** Per-row nested selections, lifted out of `fields` by the list resolver. */
531
+ nested?: Record<string, NestedSelection>;
69
532
  /**
70
533
  * Opaque cursor produced by a prior `read()` call. Base64url-encoded JSON
71
534
  * (server-built, server-consumed). Callers should treat it as a black box.
72
535
  */
73
536
  cursor?: string;
74
- /**
75
- * Admin-gated escape hatch for filtering on unindexed fields. Absent → the
76
- * validator rejects filters on any `indexed: false` field. Policies can pin
77
- * this to admin identities.
78
- */
79
- _allowTableScan?: boolean;
80
537
  /**
81
538
  * Optional caller-supplied projection hint. Substrates MAY use this to
82
539
  * narrow the body keys they materialise from storage — e.g. PgDataset
@@ -109,9 +566,16 @@ interface ReadResult {
109
566
  rows: Row[];
110
567
  nextCursor?: string;
111
568
  hasMore: boolean;
569
+ count?: number;
570
+ groups?: Array<Record<string, unknown>>;
571
+ truncated?: boolean;
112
572
  }
113
573
  interface WriteResult {
114
574
  written: number;
575
+ /**
576
+ * One per written row, in input order: a supplied id verbatim, otherwise
577
+ * the id the dataset assigned. Under `insertOnly`, skipped rows are absent.
578
+ */
115
579
  ids: Id[];
116
580
  /**
117
581
  * Merged rows after upsert — substrates populate this with the post-merge
@@ -147,6 +611,33 @@ interface DeleteResult {
147
611
  deleted: number;
148
612
  ids: Id[];
149
613
  }
614
+ /** Options of `Dataset.updateWhere` / `deleteWhere`. */
615
+ interface FilterWriteOptions {
616
+ /** Most rows the filter may match; one more refuses the write with `filter_too_broad`. */
617
+ maxMatch: number;
618
+ /** Stamped as `updatedBy` (update only); the anonymous system actor when absent. */
619
+ actor?: ActorIdentity;
620
+ /** Stamped as `updatedAt` (update only); the clock when absent. */
621
+ now?: Date;
622
+ }
623
+ /**
624
+ * What a by-filter write changed: every changed id, and the changed rows
625
+ * (post-images for an update, pre-images for a delete). Both are bounded by
626
+ * `maxMatch`.
627
+ */
628
+ interface FilterWriteResult {
629
+ count: number;
630
+ ids: Id[];
631
+ rows: Row[];
632
+ }
633
+ /** The caps on `updateMany<T>` / `deleteMany<T>`. */
634
+ interface FilterWriteLimits {
635
+ /** Most rows one filter may match (`SUBSTRATE_FILTER_WRITE_MAX_MATCH`). */
636
+ maxMatch: number;
637
+ /** Most ids one result returns (`SUBSTRATE_FILTER_WRITE_IDS_CAP`). */
638
+ idsCap: number;
639
+ }
640
+ declare const DEFAULT_FILTER_WRITE_LIMITS: Readonly<FilterWriteLimits>;
150
641
  interface DropResult {
151
642
  /** Number of rows physically removed. */
152
643
  rowsDeleted: number;
@@ -158,13 +649,18 @@ interface DropResult {
158
649
  * validates every where-clause leaf against `operators[fieldKind]` and rejects
159
650
  * ops the dataset can't execute rather than returning a silently wrong result.
160
651
  */
161
- type FieldKind = 'scalar.number' | 'scalar.string' | 'scalar.boolean' | 'scalar.date' | 'scalar.json' | 'crossRef' | 'list';
652
+ type FieldKind = 'scalar.number' | 'scalar.int16' | 'scalar.int32' | 'scalar.int64' | 'scalar.string' | 'scalar.boolean' | 'scalar.date' | 'scalar.json' | 'crossRef' | 'list';
162
653
  interface FilterCapabilities {
163
654
  operators: Record<FieldKind, string[]>;
164
655
  /** Does the dataset honour `orderBy`? */
165
656
  orderBy: boolean;
166
657
  /** Does the dataset honour cursor-based pagination? */
167
658
  cursor: boolean;
659
+ /**
660
+ * Does the dataset answer the typed-edge-walk grammar? The engine refuses a
661
+ * walk frame on a dataset without it (`walks_unsupported_store`).
662
+ */
663
+ walks?: boolean;
168
664
  }
169
665
  interface UpdateFieldRef {
170
666
  ns?: string;
@@ -207,7 +703,7 @@ interface Dataset {
207
703
  */
208
704
  readonly txScopeId: string;
209
705
  read(type: string, params: ReadParams): Promise<ReadResult>;
210
- write(type: string, rows: Row[], options?: WriteOptions): Promise<WriteResult>;
706
+ write(type: string, rows: WriteRow[], options?: WriteOptions): Promise<WriteResult>;
211
707
  tombstone(type: string, ids: Id[]): Promise<DeleteResult>;
212
708
  purge(type: string, ids: Id[]): Promise<DeleteResult>;
213
709
  /**
@@ -232,7 +728,7 @@ interface Dataset {
232
728
  * Physically delete every row AND short-id for `this.namespace`, across
233
729
  * every type. Equivalent to calling `dropType` for each distinct type
234
730
  * seen in storage, but substrate impls SHOULD use a single bulk DELETE
235
- * where available (pg: `DELETE FROM rows WHERE namespace = $1`).
731
+ * where available (pg: one DELETE per type table).
236
732
  *
237
733
  * Same atomicity rules as `dropType`. Same "ignores archived state".
238
734
  */
@@ -246,8 +742,9 @@ interface Dataset {
246
742
  *
247
743
  * Semantics:
248
744
  * - Equality is strict (`===`) for scalars; `Date` compares by
249
- * `.getTime()`; `null` matches only a stored `null` (including
250
- * intentionally-null fields set via `updateField`), never an absent field.
745
+ * `.getTime()`; `null` matches a stored `null` or an absent field, on both
746
+ * datasets. A declared scalar or crossRef set to `null` reads back absent
747
+ * (`isColumnField`), so it matches `null` all the same.
251
748
  * - A missing row fails the match even when `match` is empty — the
252
749
  * operation is "update this existing row if predicate holds".
253
750
  * - `updates` is merged onto the stored row the same way `write()` upserts
@@ -275,7 +772,7 @@ interface Dataset {
275
772
  * Run `fn` while holding a mutual-exclusion lock named `key`, waiting until
276
773
  * it is free. Excludes every other holder of the same key on this substrate:
277
774
  * pg holds a session advisory lock, so the exclusion spans processes sharing
278
- * the database; memory excludes holders on this dataset instance only.
775
+ * the store's database schema; memory excludes holders on this dataset instance only.
279
776
  * Released when `fn` settles, whether it resolves or throws, except on a
280
777
  * pg tx-bound dataset, where it is held until the transaction ends so the
281
778
  * next holder sees what this one committed. Not re-entrant:
@@ -287,6 +784,152 @@ interface Dataset {
287
784
  * @throws whatever `fn` throws, after the lock is released
288
785
  */
289
786
  withAdvisoryLock?<T>(key: string, fn: () => Promise<T>): Promise<T>;
787
+ /**
788
+ * Prepare dedicated storage for `type` in this namespace, moving any rows
789
+ * already written for it there. Idempotent, and safe beside concurrent
790
+ * reads and writes: no row is lost, duplicated or seen twice. That holds for
791
+ * READ COMMITTED readers; a REPEATABLE READ snapshot taken before the move
792
+ * can miss the moved rows. Also builds the type's indexes, including its
793
+ * declared `indexes`, and throws when a unique one cannot be built.
794
+ *
795
+ * `indexes` picks which: `unique` builds only the unique ones and leaves the
796
+ * rest `pending`, for a later `nonUnique` call to build. `signal` cancels an
797
+ * in-flight non-unique build, which the next call retries. Default `all`.
798
+ *
799
+ * On a store keeping each type in a table of its own, also brings that
800
+ * table in line with the type's declared fields: a newly declared field gets
801
+ * its column, or, with `newFields: 'pending'`, is recorded `pending` for a
802
+ * background sweep to add. Resolves with the fields that went `pending` in
803
+ * this call; a store without such tables resolves `{ pending: [] }`.
804
+ *
805
+ * OPTIONAL. Backends without per-type storage omit it.
806
+ */
807
+ ensureTypeStorage?(type: string, opts?: {
808
+ indexes?: 'all' | 'unique' | 'nonUnique';
809
+ signal?: AbortSignal;
810
+ newFields?: 'column' | 'pending';
811
+ }): Promise<{
812
+ pending: string[];
813
+ }>;
814
+ /**
815
+ * Where `type`'s rows live and how its declared indexes stand, read from the
816
+ * catalog without building anything.
817
+ *
818
+ * OPTIONAL. Backends without per-type storage omit it.
819
+ */
820
+ describeStorage?(type: string): Promise<TypeStorageReport>;
821
+ /**
822
+ * Gives each `pending` field of every namespace its column and moves its
823
+ * values out of jsonb, and finishes each `promoting` one, a field at a time.
824
+ * DDL waits at most `lockTimeoutMs` for its table's lock, then that field is
825
+ * `busy` and retried by the next call; `batchRows` rows move per statement.
826
+ * `signal` stops the sweep between steps. Resolves with one event per field
827
+ * it touched. A field that failed on a unique conflict waits for its type's
828
+ * catalog to change, unless `retryConflicts` is set.
829
+ *
830
+ * OPTIONAL. Backends with no per-type tables omit it.
831
+ */
832
+ promotePendingFields?(opts: {
833
+ lockTimeoutMs: number;
834
+ batchRows: number;
835
+ signal?: AbortSignal;
836
+ /** Retries fields a unique conflict holds back until their type's catalog changes: an operator's run after fixing the data. */
837
+ retryConflicts?: boolean;
838
+ /**
839
+ * The store has no other writers (migrate-store's target): a field's move
840
+ * to its column waits for its table lock, so a blocking autovacuum is
841
+ * cancelled, rather than retrying without queueing in front of writers.
842
+ */
843
+ noWriters?: boolean;
844
+ }): Promise<PromotionEvent[]>;
845
+ /**
846
+ * Store-wide counts of the fields an operator should know about: held in
847
+ * jsonb until promoted, whose promotion failed, and no longer declared with
848
+ * their column kept. Counts only, with no namespace, type, field or error
849
+ * text, since `/health` shows it unauthenticated; `storageReport` names them.
850
+ *
851
+ * OPTIONAL. Backends with no per-type tables omit it.
852
+ */
853
+ fieldWarnings?(): Promise<string[]>;
854
+ /**
855
+ * The connection pool right now: connections open, opening, idle and leased,
856
+ * and requests waiting for one. Counts only; `/health` shows it unauthenticated.
857
+ *
858
+ * OPTIONAL. Backends with no connection pool omit it.
859
+ */
860
+ poolStats?(): PoolStats;
861
+ /**
862
+ * The database-wide storage layout and what an operator should look at:
863
+ * indexes this process has yet to build or failed to build, fields whose
864
+ * promotion failed, and a type-table count over `partitionBudget`; and, as
865
+ * `info`, engine indexes no longer declared, pending and orphaned fields,
866
+ * and open index recommendations. Never changes anything.
867
+ *
868
+ * OPTIONAL. Backends without per-type storage omit it.
869
+ */
870
+ storageReport?(opts: {
871
+ partitionBudget: number;
872
+ }): Promise<StorageReport>;
873
+ /**
874
+ * Table and index statistics for every type the store holds, every
875
+ * namespace, read from the database's own statistics. Estimates, never a
876
+ * count. Never changes anything.
877
+ *
878
+ * OPTIONAL. Backends without such statistics omit it.
879
+ */
880
+ typeStats?(): Promise<TypeStats[]>;
881
+ /**
882
+ * The indexes the engine wants for `namespace`/`type` (any namespace, not
883
+ * only this one), with each one's leading keys, and the type's size hint.
884
+ * Undefined when the type is unknown. Never changes anything.
885
+ *
886
+ * OPTIONAL. Backends that build no indexes omit it.
887
+ */
888
+ indexPlan?(type: string, namespace: string): Promise<IndexPlan | undefined>;
889
+ /**
890
+ * Drop index `name` of this namespace's `type`, a signed `propose_index`
891
+ * remove. Only an engine-owned, non-unique index the engine no longer wants
892
+ * (a leftover or a retracted declaration) is dropped; anything else is
893
+ * refused with an error whose `code` is `unknown_index`, `not_engine_owned`,
894
+ * `unique_index` or `still_desired`, and nothing is dropped. A drop that
895
+ * waits longer than `lockTimeoutMs` on another transaction is abandoned with
896
+ * `index_drop_timeout`; the index may then be left invalid, and a retry drops it.
897
+ *
898
+ * OPTIONAL. Backends that build no indexes omit it (the memory dataset):
899
+ * there, `propose_index remove` has no index to find, so it is refused
900
+ * `unknown_index`, and a remove recommendation it names is superseded.
901
+ */
902
+ dropEngineIndex?(type: string, name: string, opts?: {
903
+ /** Runs once every check has passed, just before the drop; a throw drops nothing. */
904
+ beforeDrop?: () => Promise<void>;
905
+ /** The drop's bound on each lock wait, in ms. Defaults to the backend's own. */
906
+ lockTimeoutMs?: number;
907
+ }): Promise<void>;
908
+ /**
909
+ * Drop the engine-owned indexes of this namespace, after a namespace drop
910
+ * has committed: its types leave the engine, so nothing would remove them.
911
+ * Non-unique ones only, unless `unique`, which the caller passes only when
912
+ * the namespace is declared nowhere (in no code and no live proposal): a
913
+ * declared namespace's types come back, and until the reconciler rebuilds a
914
+ * unique index, writes would not be held to it. Returns the names dropped.
915
+ * Never inside a transaction; a tx-bound instance throws. A lock wait that
916
+ * times out throws `index_drop_timeout`, leaving the rest for
917
+ * `storageReport` to list as orphaned.
918
+ *
919
+ * OPTIONAL. Backends that build no indexes omit it.
920
+ */
921
+ dropNamespaceIndexes?(opts?: {
922
+ unique?: boolean;
923
+ }): Promise<string[]>;
924
+ /**
925
+ * The entry of this namespace's `type`'s declared `indexes` whose index is
926
+ * named `name`, or undefined when no declaration builds it (a derived or
927
+ * engine index, or none). `propose_index remove` retracts it when it is a
928
+ * proposed type's. Never changes anything.
929
+ *
930
+ * OPTIONAL. Backends that build no indexes omit it.
931
+ */
932
+ indexDeclarationOf?(type: string, name: string): Promise<IndexDecl | undefined>;
290
933
  /**
291
934
  * Execute `fn` atomically. All ops performed on the `tx` argument run inside
292
935
  * one substrate transaction. If `fn` throws or returns a rejected promise,
@@ -364,6 +1007,62 @@ interface Dataset {
364
1007
  * callers chain their own `read()` after resolution.
365
1008
  */
366
1009
  resolveShortId(type: string, shortId: number): Promise<string | null>;
1010
+ /**
1011
+ * `count` fresh short ids for `type`, ascending, in one call (pg: one
1012
+ * statement whatever `count`). Like `allocateShortId`, binds nothing.
1013
+ * `count` of 0 returns `[]` and sends nothing.
1014
+ */
1015
+ allocateShortIds(type: string, count: number): Promise<number[]>;
1016
+ /**
1017
+ * `bindShortId` for many pairs at once (pg: one statement). Idempotent per
1018
+ * pair, as `bindShortId` is. An empty `pairs` does nothing and sends nothing.
1019
+ */
1020
+ bindShortIds(type: string, pairs: ReadonlyArray<{
1021
+ shortId: number;
1022
+ rowId: string;
1023
+ }>): Promise<void>;
1024
+ /**
1025
+ * Set-wise `resolveShortId` for the LIVE rows among `shortIds` (pg: one
1026
+ * statement). A short id with no binding, or whose row is tombstoned or
1027
+ * purged, is absent from the map: the answer `resolveShortId` then a live
1028
+ * `read` gives, which is how callers resolve today. Repeats in `shortIds`
1029
+ * are deduped. An empty `shortIds` returns an empty map and sends nothing.
1030
+ */
1031
+ resolveShortIds(type: string, shortIds: readonly number[]): Promise<ReadonlyMap<number, string>>;
1032
+ /**
1033
+ * Multi-row `update`: merges each patch into its LIVE row with today's
1034
+ * semantics (a key absent from the patch keeps its value, `null` clears it;
1035
+ * `id` and `_deletedAt` keys are ignored), stamps `updatedAt` = `now` and
1036
+ * `updatedBy` = `actor` (the anonymous system actor when `null`), as
1037
+ * `conditionalUpdate` does. pg runs one statement whatever the batch size.
1038
+ *
1039
+ * The result has one entry per patch, in order: the merged row, or `null`
1040
+ * when `id` is not live. It never revives a tombstoned row or re-creates a
1041
+ * purged one.
1042
+ *
1043
+ * A repeated `id` is allowed: its patches apply in input order (later keys
1044
+ * win, as sequential patches would) and every slot for that id holds the
1045
+ * final merged row. An empty `patches` returns `[]` and sends nothing.
1046
+ */
1047
+ patchRows(type: string, patches: ReadonlyArray<{
1048
+ id: Id;
1049
+ patch: Record<string, unknown>;
1050
+ }>, actor: ActorIdentity | null, now: Date): Promise<Array<Row | null>>;
1051
+ /**
1052
+ * Update every live row of `type` matching `where`: merges `set` into each,
1053
+ * as `patchRows` does, and stamps `updatedAt` / `updatedBy`. When more than
1054
+ * `opts.maxMatch` rows match it throws `ResolverFrameError('filter_too_broad')`
1055
+ * and changes nothing; zero matches is `{ count: 0 }`. pg runs one
1056
+ * statement, which locks the matched rows and re-tests `where` on each, so
1057
+ * a row a concurrent writer moved out of the filter is not updated.
1058
+ */
1059
+ updateWhere?(type: string, where: WhereClause, set: Record<string, unknown>, opts: FilterWriteOptions): Promise<FilterWriteResult>;
1060
+ /**
1061
+ * Tombstone every live row of `type` matching `where`, under the same cap
1062
+ * and lock as `updateWhere`. `rows` are the pre-images. Checks no edges:
1063
+ * the caller gates `restrict` and rolls back.
1064
+ */
1065
+ deleteWhere?(type: string, where: WhereClause, opts: FilterWriteOptions): Promise<FilterWriteResult>;
367
1066
  /**
368
1067
  * Inverse of `resolveShortId` — look up the short id assigned to a
369
1068
  * given row id. Returns null if no sidecar entry exists (e.g. rows written
@@ -416,6 +1115,75 @@ interface DatasetRegistry {
416
1115
  */
417
1116
  dispose?(ns: string): void;
418
1117
  }
1118
+ /**
1119
+ * One declared index of a type and its state: as the catalog holds it, or,
1120
+ * while this process defers or runs its build, `pending`, `building` or
1121
+ * `failed`. Filters on an index that is not `valid` scan.
1122
+ */
1123
+ interface TypeIndexState {
1124
+ name: string;
1125
+ fields: string[];
1126
+ unique: boolean;
1127
+ state: 'valid' | 'building' | 'pending' | 'invalid' | 'failed';
1128
+ outcome: 'created' | 'adopted' | 'swapped' | 'unchanged';
1129
+ /** Why a `failed` index failed: an error code, then a short reason. */
1130
+ error?: string;
1131
+ /** Why a `pending` index was not built: the free-disk check refused it. */
1132
+ reason?: 'insufficient_disk';
1133
+ }
1134
+ /** Where a type's rows live: `table` is set once the type has its table. */
1135
+ interface TypeStorageReport {
1136
+ layout: 'tables';
1137
+ table?: string;
1138
+ indexes: TypeIndexState[];
1139
+ /** Declared fields whose values live in `extra` until they get a column. */
1140
+ pending?: string[];
1141
+ /** Fields no longer declared whose columns are kept. */
1142
+ orphaned?: string[];
1143
+ }
1144
+ /**
1145
+ * One field's outcome in a promotion sweep (`Dataset.promotePendingFields`):
1146
+ * `promoted` (it has its column and every value moved), `busy` (its table's
1147
+ * lock was held past the timeout; retried next sweep) or `failed` (an error,
1148
+ * or the third busy attempt in a row). `attempts` counts this one. A field
1149
+ * another process is sweeping is `busy` with the error `held by another
1150
+ * process`, and its `attempts` is the recorded count, unchanged.
1151
+ */
1152
+ interface PromotionEvent {
1153
+ kind: 'promoted' | 'busy' | 'failed';
1154
+ namespace: string;
1155
+ type: string;
1156
+ field: string;
1157
+ attempts: number;
1158
+ error?: string;
1159
+ }
1160
+ interface StorageReport {
1161
+ /** `unknown` when the report itself failed. */
1162
+ layout: 'tables' | 'unknown';
1163
+ warnings: string[];
1164
+ /** Worth showing, not a fault: the doctor prints these without failing. */
1165
+ info?: string[];
1166
+ }
1167
+ /** See `Dataset.indexPlan`. */
1168
+ interface IndexPlan {
1169
+ /** Each desired index, its keys in order: a derived index ends with the type's natural order. */
1170
+ indexes: Array<{
1171
+ name: string;
1172
+ keys: Array<[string, 'asc' | 'desc']>;
1173
+ unique: boolean;
1174
+ /** A partial index's predicate beyond `live`: it serves only reads that `eq` each field to its value. */
1175
+ only?: Record<string, boolean | null>;
1176
+ }>;
1177
+ expectedSize?: 'small' | 'large';
1178
+ }
1179
+ /** A connection pool's counts, as `Dataset.poolStats` reports them. */
1180
+ interface PoolStats {
1181
+ connections: number;
1182
+ opening: number;
1183
+ idle: number;
1184
+ leased: number;
1185
+ waiting: number;
1186
+ }
419
1187
  //#endregion
420
1188
  //#region ../../packages/substrate-types/src/policy.d.ts
421
1189
  interface Identity {
@@ -427,8 +1195,8 @@ interface Identity {
427
1195
  * tasks: ['read','mutate'] }`; `'*'` is a legal key. Read it through the
428
1196
  * `identity-grant.ts` helpers.
429
1197
  *
430
- * A plain object rather than a Map: `Identity` is JSON-serialised into the
431
- * SSE stream ticket, and a Map would arrive as `{}` on the far side.
1198
+ * A plain object rather than a Map: `Identity` is JSON-serialised, and a
1199
+ * Map would arrive as `{}` on the far side.
432
1200
  *
433
1201
  * A bare `{ id }` with no grant (direct-call tests, system actors) is the
434
1202
  * no-claim case: `grantOf` resolves it to `null`, and the engine's grant
@@ -456,7 +1224,7 @@ interface RequestContext {
456
1224
  * `peerIp` + the operator-configured trusted-proxy allowlist. When the peer
457
1225
  * is in the allowlist, this is the leftmost `x-forwarded-for` entry; when
458
1226
  * untrusted, this is the peer IP itself. Undefined when the transport doesn't
459
- * expose peerIp (direct engine unit tests, pre-upgrade WS paths).
1227
+ * expose peerIp (direct engine unit tests, paths without a peer).
460
1228
  */
461
1229
  clientIp?: string;
462
1230
  /**
@@ -545,6 +1313,14 @@ interface ResolverArgs {
545
1313
  * the tx-scoped `dataset` handle instead. Absent on the query/subscribe path.
546
1314
  */
547
1315
  resolveDataset?: (ns: string) => Dataset | undefined;
1316
+ /**
1317
+ * Engine-injected on mutate: how many frame entries share this call's
1318
+ * transaction, itself included. A resolver whose effect waits on other
1319
+ * transactions (a concurrent index drop) refuses to share one.
1320
+ */
1321
+ txEntries?: number;
1322
+ /** Engine-injected on `updateMany<T>` / `deleteMany<T>`: the deployment's caps. */
1323
+ filterWriteLimits?: FilterWriteLimits;
548
1324
  }
549
1325
  /**
550
1326
  * Post-commit write announcement consumed by the engine's subscription bus
@@ -664,33 +1440,6 @@ interface RequestObservation {
664
1440
  readonly durationMs: number;
665
1441
  }
666
1442
  type RequestObserver = (o: RequestObservation) => void;
667
- interface WsConnection {
668
- readonly id: string;
669
- send(frame: Buffer | Uint8Array | string): void;
670
- /**
671
- * Transport-level back-pressure seam. Resolves once the
672
- * socket's write buffer is below its high-water mark — immediately when it
673
- * already is. The stream route awaits this between chunks so a fast producer
674
- * paces to a slow consumer instead of growing the send buffer unbounded.
675
- * Optional: transports without buffer introspection (memory tests) omit it,
676
- * and the route treats a missing hook as "always drained".
677
- */
678
- drain?(): Promise<void>;
679
- onMessage(cb: (frame: Buffer) => void): void;
680
- onClose(cb: (code?: number) => void): void;
681
- close(code?: number, reason?: string): void;
682
- /**
683
- * Register a callback invoked synchronously during graceful server shutdown,
684
- * BEFORE the underlying socket is closed. Higher layers use this to flush
685
- * structured frames (e.g. `{ kind: 'end', reason: 'server_restart' }` per
686
- * active subscription) so clients can short-circuit reconnect backoff
687
- * instead of seeing an abrupt 1006 drop. Optional — transports without a
688
- * pre-close hook (memory tests) may omit it; the route layer should
689
- * tolerate undefined.
690
- */
691
- onShutdown?(cb: (reason: string) => void): void;
692
- }
693
- type WsHandler = (req: SubstrateRequest, conn: WsConnection) => void | Promise<void>;
694
1443
  interface TlsConfig {
695
1444
  cert: string | Buffer;
696
1445
  key: string | Buffer;
@@ -707,7 +1456,6 @@ interface PluginRouter {
707
1456
  put(path: string, handler: HttpHandler): void;
708
1457
  delete(path: string, handler: HttpHandler): void;
709
1458
  patch(path: string, handler: HttpHandler): void;
710
- websocket(path: string, handler: WsHandler): void;
711
1459
  }
712
1460
  interface SubstrateServer {
713
1461
  start(opts?: StartOpts): Promise<{
@@ -719,7 +1467,6 @@ interface SubstrateServer {
719
1467
  abortInFlight?: boolean;
720
1468
  }): Promise<void>;
721
1469
  route(method: HttpMethod, path: string, handler: HttpHandler): void;
722
- websocket(path: string, handler: WsHandler): void;
723
1470
  onError?(hook: (err: Error, req?: SubstrateRequest) => void): void;
724
1471
  createPluginRouter(plugin: {
725
1472
  name: string;
@@ -727,17 +1474,16 @@ interface SubstrateServer {
727
1474
  /**
728
1475
  * Abort every in-flight HTTP request's `req.signal` so resolvers cooperatively
729
1476
  * bail. Used during graceful shutdown to give the engine a chance to roll
730
- * back mutations before fastify's drain timeout fires. WS connections are
731
- * not affected by this call; their teardown rides on socket close.
1477
+ * back mutations before fastify's drain timeout fires.
732
1478
  */
733
1479
  abortAllRequests(reason?: string): void;
734
1480
  /**
735
1481
  * Register a callback invoked synchronously at the START of `stop()` —
736
1482
  * before in-flight requests are aborted and before the transport drains.
737
1483
  *
738
- * The HTTP analogue of `WsConnection.onShutdown`, for routes that hold
739
- * a response open indefinitely (SSE): handlers flush a terminal frame and end
740
- * their response, or the drain would wait forever.
1484
+ * For routes that hold a response open indefinitely (streams): handlers
1485
+ * flush a terminal frame and end their response, or the drain would wait
1486
+ * forever.
741
1487
  *
742
1488
  * Additive, not last-one-wins (unlike `onError`): a dropped registration is
743
1489
  * a leaked stream. Optional — transports with no graceful phase may omit it,
@@ -776,14 +1522,28 @@ type PostWriteHook = (row: Row, ctx: RequestContext) => Promise<void>;
776
1522
  * want the canonical alias should import `Resolver` from this package.
777
1523
  */
778
1524
  type RegistryEntryResolver = (ctx: RequestContext, args: ResolverArgs) => Promise<Row | Row[] | ReadResult | AsyncIterable<Row> | null> | AsyncIterable<Row>;
1525
+ /**
1526
+ * Set-wise resolver for a mutate array: one call for every uncached element,
1527
+ * in order, returning one entry per element. Throw `BatchElementError(i, cause)`
1528
+ * to refuse element `i`; any throw rolls back the whole array.
1529
+ */
1530
+ type BatchResolver = (ctx: RequestContext, args: readonly ResolverArgs[]) => Promise<Array<Row | null>>;
779
1531
  /** Input shape for `Registry.register` / `Registry.replace`. */
780
1532
  interface RegistryEntryInput {
781
1533
  resolver: RegistryEntryResolver;
1534
+ /** When present, a mutate array calls this once instead of `resolver` per element. */
1535
+ batch?: BatchResolver;
782
1536
  policy?: Policy;
783
1537
  fields?: Record<string, FieldDef>;
784
1538
  kind?: RegistryEntryKind;
785
1539
  params?: Record<string, FieldDef>;
786
1540
  returns?: FieldDef;
1541
+ /** Natural order of a `kind: 'type'` entry; published on its `TypeSchema`. */
1542
+ orderedBy?: OrderedBy;
1543
+ /** Declared indexes of a `kind: 'type'` entry installed at runtime (a proposed type). */
1544
+ indexes?: IndexDecl[];
1545
+ /** A proposed type's size hint; `large` lowers the index advisor's row minimum. */
1546
+ expectedSize?: 'small' | 'large';
787
1547
  /**
788
1548
  * Semantic verb for autoCRUD-registered mutations. The engine
789
1549
  * threads this into `RequestContext.verb` at dispatch so type-/plugin-level
@@ -791,6 +1551,11 @@ interface RegistryEntryInput {
791
1551
  * string matching. Omit for type entries and custom mutations.
792
1552
  */
793
1553
  verb?: AutoCrudVerb;
1554
+ /**
1555
+ * The type a by-filter mutation (`updateMany<T>` / `deleteMany<T>`) writes.
1556
+ * The engine validates `params.where` against it as a query's `where`.
1557
+ */
1558
+ filterTarget?: string;
794
1559
  }
795
1560
  /**
796
1561
  * Stored shape returned by `Registry.get` and `Registry.all`. Mirrors the
@@ -800,6 +1565,8 @@ interface RegistryEntryInput {
800
1565
  */
801
1566
  interface RegistryEntry {
802
1567
  resolver: RegistryEntryResolver;
1568
+ /** Mirrors `RegistryEntryInput.batch`. */
1569
+ batch?: BatchResolver;
803
1570
  policy: Policy;
804
1571
  fields: Record<string, FieldDef>;
805
1572
  kind: RegistryEntryKind;
@@ -807,8 +1574,16 @@ interface RegistryEntry {
807
1574
  params: Record<string, FieldDef>;
808
1575
  /** Present only for `kind: 'mutation'` entries that declared a return shape. */
809
1576
  returns?: FieldDef;
1577
+ /** Mirrors `RegistryEntryInput.orderedBy`. */
1578
+ orderedBy?: OrderedBy;
1579
+ /** Mirrors `RegistryEntryInput.indexes`. */
1580
+ indexes?: IndexDecl[];
1581
+ /** Mirrors `RegistryEntryInput.expectedSize`. */
1582
+ expectedSize?: 'small' | 'large';
810
1583
  /** Mirrors `RegistryEntryInput.verb` — present when the registrant stamped one. */
811
1584
  verb?: AutoCrudVerb;
1585
+ /** Mirrors `RegistryEntryInput.filterTarget`. */
1586
+ filterTarget?: string;
812
1587
  }
813
1588
  /**
814
1589
  * Canonical per-namespace registry surface, implemented by the concrete
@@ -943,6 +1718,29 @@ interface EmbedSourceContext {
943
1718
  id: string;
944
1719
  }) => Promise<Row | null>;
945
1720
  }
1721
+ /**
1722
+ * A declared index over a type's scalar or crossRef fields, built by the
1723
+ * engine with the same expressions the filter compiler emits.
1724
+ *
1725
+ * - `fields`: key columns in order; `dir` defaults to `asc`. Both directions
1726
+ * sort NULLs last, as `orderBy` does.
1727
+ * - `unique`: refused at build time while duplicate live values exist.
1728
+ * - `where`: always ANDed with "not deleted". `'live'` adds nothing more; a
1729
+ * record adds equality to a boolean (or IS NULL for `null`) on the type's
1730
+ * own fields. Only those operands compile to literals, which a generic plan
1731
+ * needs to match the index predicate.
1732
+ * - `name`: the pg index name to own or adopt; generated when omitted. It may
1733
+ * not end in `__swap`, the reconciler's temporary suffix.
1734
+ */
1735
+ interface IndexDecl {
1736
+ fields: Array<string | {
1737
+ field: string;
1738
+ dir: 'asc' | 'desc';
1739
+ }>;
1740
+ unique?: boolean;
1741
+ where?: 'live' | Record<string, boolean | null>;
1742
+ name?: string;
1743
+ }
946
1744
  /**
947
1745
  * Structural shape of a type definition, mirroring `TypeInput` from
948
1746
  * @mnmal-ai/substrate-sdk (redeclared to avoid a substrate-types → substrate-sdk import
@@ -991,6 +1789,14 @@ interface NamespaceTypeDef {
991
1789
  lexical?: {
992
1790
  source: (row: Row) => string | null | undefined | Promise<string | null | undefined>;
993
1791
  };
1792
+ /** Composite and unique indexes the engine builds for this type. See {@link IndexDecl}. */
1793
+ indexes?: IndexDecl[];
1794
+ /** The type's natural order, the default read order. See {@link OrderedBy}. */
1795
+ orderedBy?: OrderedBy;
1796
+ /** See the SDK's `TypeInput.ordering`. */
1797
+ ordering?: 'visible';
1798
+ /** Size hint: `large` lowers the index advisor's row minimum for this type. */
1799
+ expectedSize?: 'small' | 'large';
994
1800
  [key: string]: unknown;
995
1801
  }
996
1802
  interface NamespaceDefinition {
@@ -1010,10 +1816,10 @@ interface NamespaceDefinition {
1010
1816
  */
1011
1817
  annotations?: Readonly<Record<string, unknown>>;
1012
1818
  /**
1013
- * Body keys a SQL dataset mirrors into dedicated `rows` columns, so filters
1014
- * and sorts on them hit an index. The declaring plugin creates the columns in
1015
- * its kernel migrations; the engine creates none. `column` is a bare
1016
- * identifier.
1819
+ * @deprecated Ignored. Every declared scalar is a column of its type's
1820
+ * table, so there is nothing left to promote. It was the old layouts' body
1821
+ * keys mirrored into dedicated `rows` columns; it still type-checks, so a
1822
+ * plugin that sets it boots unchanged.
1017
1823
  */
1018
1824
  promotedColumns?: Readonly<Record<string, {
1019
1825
  column: string;
@@ -1119,6 +1925,25 @@ interface NamespaceConfig {
1119
1925
  /** From `NamespaceDefinition.annotations`; surfaced on the schema, never hashed. */
1120
1926
  annotations?: Readonly<Record<string, unknown>>;
1121
1927
  }
1928
+ /**
1929
+ * What a coordinator's `onCommittedWrites` hook is told: one `(namespace, type,
1930
+ * operation)` group of one committed transaction. `ids` keep the order the
1931
+ * writes were emitted in. `actor` is who the request was attributed to, or
1932
+ * null for a request with no recognisable identity.
1933
+ */
1934
+ interface CommittedWriteBatch {
1935
+ namespace: string;
1936
+ type: string;
1937
+ operation: 'create' | 'update' | 'delete';
1938
+ actor: ActorIdentity | null;
1939
+ ids: readonly Id[];
1940
+ }
1941
+ /**
1942
+ * Never awaited by the mutate, so keep its synchronous part cheap. A throw or a
1943
+ * rejection is recorded as a `/health` warning (plugin name and time, not the
1944
+ * message) and does not touch the write.
1945
+ */
1946
+ type CommittedWritesHook = (batch: CommittedWriteBatch) => void | Promise<void>;
1122
1947
  /**
1123
1948
  * Narrow engine handle threaded into scheduled-task / plugin-route contexts.
1124
1949
  * The concrete `Engine` class in `@mnmal-ai/substrate-core` satisfies this
@@ -1128,6 +1953,14 @@ interface NamespaceConfig {
1128
1953
  interface EngineHandle {
1129
1954
  query(input: EngineInput): Promise<PackEnvelope>;
1130
1955
  mutate(input: EngineInput): Promise<PackEnvelope>;
1956
+ /**
1957
+ * Announce writes that were committed outside `mutate`: they reach
1958
+ * subscribers, then every coordinator's `onCommittedWrites`, as a commit
1959
+ * through `mutate` would. This is not a transaction and checks nothing: the
1960
+ * caller guarantees `events` are already committed, since an announced write
1961
+ * that rolls back cannot be taken back.
1962
+ */
1963
+ announceWrites(events: readonly WriteEvent[], actor: ActorIdentity | null): Promise<void>;
1131
1964
  /**
1132
1965
  * Evaluate the read policy for a type entry without dispatching it.
1133
1966
  * Out-of-band read surfaces (kernel routes returning row data
@@ -1291,6 +2124,12 @@ interface CoordinatorPlugin {
1291
2124
  * `NamespaceInstallContext.pluginPolicy`.
1292
2125
  */
1293
2126
  policies?: Policy[];
2127
+ /**
2128
+ * Told what each committed transaction wrote, once per `(namespace, type,
2129
+ * operation)` rather than once per row. Never awaited by the mutate, run in
2130
+ * commit order per plugin; a slow hook delays only this plugin's later calls.
2131
+ */
2132
+ onCommittedWrites?: CommittedWritesHook;
1294
2133
  /**
1295
2134
  * Post-register lifecycle hook. Fires after every plugin's `install()`
1296
2135
  * resolves — use this for resource priming that needs the full namespace
@@ -1309,7 +2148,7 @@ interface CoordinatorPlugin {
1309
2148
  * - subprocess children that should receive SIGTERM cleanly
1310
2149
  * - long-lived `setInterval` timers (`.unref()` keeps them from
1311
2150
  * blocking exit, but explicit teardown is still cleaner for tests)
1312
- * - WebSocket subscriptions / outbound connections that should close
2151
+ * - open subscriptions / outbound connections that should close
1313
2152
  * cooperatively rather than dropping mid-request
1314
2153
  *
1315
2154
  * Errors are caught + logged via the lifecycle metric sink under
@@ -1329,7 +2168,7 @@ interface CoordinatorPlugin {
1329
2168
  */
1330
2169
  schedule?: ScheduledTask[];
1331
2170
  /**
1332
- * Mount plugin-scoped HTTP/WS routes. Called once at compose-time with a
2171
+ * Mount plugin-scoped HTTP routes. Called once at compose-time with a
1333
2172
  * `PluginRouter` pre-prefixed `/substrate/v1/plugins/<name>` by the server impl. The
1334
2173
  * router surface matches `PluginRouter` in `./server.ts`.
1335
2174
  *
@@ -1376,6 +2215,12 @@ interface KernelPlugin {
1376
2215
  * to populate this slot).
1377
2216
  */
1378
2217
  migrations?: KernelMigration[];
2218
+ /**
2219
+ * Names of the tables (in the store's schema) this plugin keeps data in.
2220
+ * `substrate-admin migrate-store` copies them alongside the engine's own
2221
+ * tables and refuses a source table that nothing declares.
2222
+ */
2223
+ storeTables?: readonly string[];
1379
2224
  /**
1380
2225
  * Filter operators contributed to the where-clause grammar. Each entry
1381
2226
  * registers a custom operator (e.g. `embeddingSimilar`) the engine's
@@ -1406,6 +2251,11 @@ interface KernelPlugin {
1406
2251
  * coordinators arrive.
1407
2252
  */
1408
2253
  onKernelReady?(ctx: KernelLifecycleContext): void | Promise<void>;
2254
+ /**
2255
+ * Stops whatever `onKernelReady` started (timers, workers). The host calls
2256
+ * it before it closes the store, and may call it more than once.
2257
+ */
2258
+ onKernelStop?(): void | Promise<void>;
1409
2259
  /**
1410
2260
  * Contribute postWrite hooks for a coordinator namespace at install time.
1411
2261
  * Called by the engine ONCE per registered coordinator namespace, AFTER
@@ -1437,6 +2287,13 @@ interface KernelMigrationContext {
1437
2287
  * binary is running against the in-memory dataset.
1438
2288
  */
1439
2289
  sql: unknown | null;
2290
+ /**
2291
+ * How rows are stored: `'tables'`, one table per type and no `rows` table,
2292
+ * the only layout a store boots. `null` on the in-memory dataset. A
2293
+ * migration that still runs DDL on `rows` fails, and the host names its
2294
+ * plugin (`KernelMigrationLayoutError`).
2295
+ */
2296
+ layout: 'tables' | null;
1440
2297
  metric(name: string, payload?: unknown): void;
1441
2298
  }
1442
2299
  /**
@@ -1562,7 +2419,374 @@ interface KernelLifecycleContext {
1562
2419
  plugin: string;
1563
2420
  namespace: string;
1564
2421
  }[];
2422
+ /**
2423
+ * Whether this boot may start background work (timers, backfills). False
2424
+ * when the host boots the store only to prepare or copy into it, as
2425
+ * `substrate-admin migrate-store` does: do convergent reconciliation, start
2426
+ * nothing that writes later.
2427
+ */
2428
+ background: boolean;
1565
2429
  metric(name: string, payload?: unknown): void;
1566
2430
  }
1567
2431
  //#endregion
1568
- export { PolicyProbeContext as $, ScheduledTask as A, StartOpts as B, QualifiedFrame as C, RegistryEntryInput as D, RegistryEntry as E, HttpMethod as F, WsConnection as G, SubstrateResponse as H, PluginRouter as I, AutoCrudVerb as J, WsHandler as K, RequestObservation as L, VirtualMutationDef as M, VirtualTypeDef as N, RegistryEntryKind as O, HttpHandler as P, PolicyDecision as Q, RequestObserver as R, PostWriteHook as S, WriteResult as St, RegistryBase as T, SubstrateServer as U, SubstrateRequest as V, TlsConfig as W, Obligation as X, Identity as Y, Policy as Z, NamespaceDefinition as _, ReadResult as _t, EngineHandle as a, TypedResolver as at, PluginLifecycleContext as b, WhereClause as bt, FrameEntry as c, Dataset as ct, KernelMigrationContext as d, DropResult as dt, RequestContext as et, KernelNamespaceContribution as f, FieldKind as ft, NamespaceConfig as g, ReadParams as gt, McpToolRegistration as h, OrderByClause as ht, EncodeContext as i, RowFilterObligation as it, ScheduledTaskContext as j, RegistryEntryResolver as k, KernelLifecycleContext as l, DatasetRegistry as lt, KernelRouteClaim as m, FilterCapabilities as mt, DropParticipant as n, ResolverArgs as nt, EngineInput as o, WriteEvent as ot, KernelPlugin as p, FieldPredicate as pt, AfterTxWork as q, EmbedSourceContext as r, ResolverReturn as rt, FilterOperatorDef as s, ConditionalUpdateResult as st, CoordinatorPlugin as t, Resolver as tt, KernelMigration as u, DeleteResult as ut, NamespaceInstallContext as v, TxScopeHandles as vt, Registry as w, PluginRouteContext as x, WriteOptions as xt, NamespaceTypeDef as y, UpdateFieldRef as yt, RuntimeOp as z };
2432
+ //#region ../../packages/substrate-types/src/schema.d.ts
2433
+ /**
2434
+ * One clause of a walk-grammar read, marked index-served or not (typed edge
2435
+ * walks, goal 7). `path` is inside the frame entry, e.g. `['where', '_walk']`.
2436
+ * An unserved clause names the index that would serve it in `recommend`, when
2437
+ * one can be declared.
2438
+ */
2439
+ interface PlanClause {
2440
+ path: string[];
2441
+ kind: 'hop' | 'filter' | 'order' | 'ref' | 'select' | 'group' | 'agg';
2442
+ served: boolean;
2443
+ recommend?: IndexDecl;
2444
+ /**
2445
+ * On the hop of a pick or a frame `_agg`: how the store evaluates it.
2446
+ * `per-seed` probes once per seed row; `per-hub` computes a pick once per
2447
+ * hub many seeds share; `group-by` aggregates a window in one pass.
2448
+ */
2449
+ form?: 'per-seed' | 'per-hub' | 'group-by';
2450
+ }
2451
+ /** Words of a natural-language question the parse did not use, and why. */
2452
+ interface Dropped {
2453
+ span: string;
2454
+ reason: string;
2455
+ /**
2456
+ * What the loss changes. `filter`: which rows match (a filter, comparison,
2457
+ * walk, time window or connective), so a count, aggregate, `$ref` or pick
2458
+ * reading that lost it is `unknown`. `order`: an ordering, limit or
2459
+ * projection; the matched rows are the same and the answer stands.
2460
+ */
2461
+ kind: 'filter' | 'order';
2462
+ }
2463
+ interface FieldDefScalar {
2464
+ kind: 'scalar';
2465
+ /**
2466
+ * `'json'` is an opaque body: msgpack-friendly nested object/array, value
2467
+ * round-trips unchanged, but no per-leaf filtering or ordering. Use it for
2468
+ * freeform payloads (audit details, debug bags). When you need filterable
2469
+ * inner keys, use `field.struct` instead.
2470
+ */
2471
+ type: 'string' | 'number' | 'int16' | 'int32' | 'int64' | 'boolean' | 'date' | 'json';
2472
+ /**
2473
+ * Tri-state. `true` builds an index (a btree; a GIN for json). Unset means
2474
+ * no index. `false` means no index AND not a search handle: long-form
2475
+ * prose that NL and similar consumers leave out. The field filters either
2476
+ * way. Physical consumers test `=== true`, so never normalise unset to
2477
+ * `false`.
2478
+ */
2479
+ indexed?: boolean;
2480
+ /**
2481
+ * Metadata: when true, the value may be unset. A string, number, boolean or
2482
+ * date reads back as an absent key then, never `null` (`isColumnField`); a
2483
+ * `json` field keeps an explicit `null`.
2484
+ */
2485
+ nullable?: boolean;
2486
+ /**
2487
+ * Metadata: when true, this field is populated by the engine (not the caller)
2488
+ * and write attempts that include it MUST be rejected. Used for the
2489
+ * auto-injected `shortId` field. Consumers (autoCRUD create, schema
2490
+ * publisher, client codegen) rely on this flag as a declarative contract —
2491
+ * do not imperatively guard readonly fields without also flipping this.
2492
+ */
2493
+ readonly?: boolean;
2494
+ /**
2495
+ * Server-managed date flag. Clients may not supply the field in the
2496
+ * indicated phase(s) — autoCRUD rejects it with `client_supplied_server_field`:
2497
+ * - 'create' — on create only
2498
+ * - 'update' — on update only
2499
+ * - 'both' — on create and update
2500
+ * Nothing stamps it automatically: server-side code (a custom resolver or a
2501
+ * system writer such as `applyUpdatePatch`) sets it. A non-nullable,
2502
+ * non-defaulted field gated on create is refused by autoCRUD at install,
2503
+ * since nothing could fill it. Only honoured on `type: 'date'`.
2504
+ */
2505
+ serverSet?: 'create' | 'update' | 'both';
2506
+ /**
2507
+ * Controlled vocabulary. When present, the value MUST be one of these
2508
+ * strings. Consumed by autoCRUD write-validation (covers memory + pg
2509
+ * uniformly) and surfaced verbatim in the published schema. `type` stays
2510
+ * 'string' — an enum is a constrained string, so pack / client decoder /
2511
+ * where-validation treat it identically with no new branch.
2512
+ */
2513
+ enum?: readonly string[];
2514
+ /**
2515
+ * Governed vocabulary: alias -> canonical value (each canonical value is in
2516
+ * `enum`). autoCRUD stores the canonical value when an alias is written, and
2517
+ * `where` filters on the field match it, so synonyms converge instead of
2518
+ * sprawling. Matching is case-insensitive. Published in the schema so every
2519
+ * client reads the same table.
2520
+ */
2521
+ aliases?: Readonly<Record<string, string>>;
2522
+ /**
2523
+ * `false` admits values outside `enum` (aliases are still normalised): a
2524
+ * transition mode for adopting a vocabulary over existing free-string data.
2525
+ * Absent means strict.
2526
+ */
2527
+ strict?: boolean;
2528
+ /**
2529
+ * Alias (or canonical value) -> sibling fields it implies, e.g.
2530
+ * `done -> { resolution: 'completed' }`. On write the siblings are filled
2531
+ * when the caller omits them and a contradicting value is refused; an
2532
+ * `eq` filter on the alias also matches the implied siblings. Keys match
2533
+ * case-insensitively.
2534
+ */
2535
+ implies?: Readonly<Record<string, Readonly<Record<string, unknown>>>>;
2536
+ /**
2537
+ * Default value applied by autoCRUD `create` when the caller omits this
2538
+ * field. Two effects: (1) the row is written with the default instead of
2539
+ * being absent; (2) it satisfies the non-nullable presence check, so a
2540
+ * non-nullable scalar WITHOUT a default that is omitted on create is
2541
+ * rejected (`missing_required_field`) — that is how "required" is expressed.
2542
+ * Only applied on create (update is a partial patch). The value should match
2543
+ * `type` (booleans for boolean, ISO string for date, etc.).
2544
+ */
2545
+ default?: string | number | boolean;
2546
+ }
2547
+ interface FieldDefCrossRef {
2548
+ kind: 'crossRef';
2549
+ ns: string;
2550
+ type: string;
2551
+ onDelete: OnDelete;
2552
+ /**
2553
+ * Write-time existence policy. 'reject' refuses a create/update
2554
+ * whose crossRef value points at a non-existent or tombstoned target;
2555
+ * 'allow' is the default when absent. Consulted only at write time, never
2556
+ * stored on the edge. Enforced only for locally-resolvable
2557
+ * target namespaces (compose rejects 'reject' on bridged/unknown targets).
2558
+ */
2559
+ onMissing?: OnMissing;
2560
+ /**
2561
+ * Substrate-level indexing hint for the edge-source column, mirroring
2562
+ * `FieldDefScalar.indexed`, so the dataset need not infer it from the
2563
+ * presence of the crossRef itself.
2564
+ */
2565
+ indexed?: boolean;
2566
+ /**
2567
+ * Metadata: when true, the value may be unset. An unset field reads back as an
2568
+ * absent key, never `null` (`isColumnField`).
2569
+ */
2570
+ nullable?: boolean;
2571
+ /**
2572
+ * Activation-spreading opt-in. When `spread: true`,
2573
+ * recall hits on a row touching this crossRef propagate a damped salience
2574
+ * boost to the linked row (forward direction, X → row X references) AND to
2575
+ * rows whose same field points at the boosted row (backward direction,
2576
+ * X ← rows that reference X). Default off — operators opt in per-edge to
2577
+ * avoid polluting attention with low-signal edges (e.g. Decision.sessionId).
2578
+ *
2579
+ * Damping (typically 0.3) + depth bound (1) are configured on the recall
2580
+ * substrate, not per-field.
2581
+ */
2582
+ spread?: boolean;
2583
+ /**
2584
+ * Cross-TYPE opt-in for the recall expansion path.
2585
+ * Only meaningful when `spread: true` (the SDK `field.crossRef` builder
2586
+ * rejects `expandCrossType` without `spread` at registration). When both
2587
+ * are set, BACKWARD walks via this edge are allowed to surface neighbours
2588
+ * whose type differs from the request's `scope.type` (e.g. a Concept-typed
2589
+ * query surfaces Decisions via inbound `Decision.establishesConcept`).
2590
+ *
2591
+ * Cross-NAMESPACE expansion is always rejected regardless of this flag.
2592
+ * Forward walks always honour scope strictly: allowing them lets
2593
+ * neighbour-type rows displace the rows that establish them in the ranking.
2594
+ * Default false.
2595
+ */
2596
+ expandCrossType?: boolean;
2597
+ }
2598
+ interface FieldDefStruct {
2599
+ kind: 'struct';
2600
+ fields: Record<string, FieldDef>;
2601
+ /** Metadata: when true, the value may be `null` or absent. */
2602
+ nullable?: boolean;
2603
+ }
2604
+ interface FieldDefList {
2605
+ kind: 'list';
2606
+ of: FieldDef;
2607
+ /**
2608
+ * When true, the `has` (element membership) operator is backed by a GIN
2609
+ * index on `(body -> '<field>')`. List fields are stored as native JSONB
2610
+ * arrays. Default false — `has` on an unindexed list still works, by
2611
+ * scanning the type's rows. The substrate binary derives the matching GIN
2612
+ * as an idempotent migration at boot from this flag; no hand-authored
2613
+ * migration is needed per field.
2614
+ */
2615
+ indexed?: boolean;
2616
+ /** Metadata: when true, the value may be `null` or absent. */
2617
+ nullable?: boolean;
2618
+ }
2619
+ type FieldDef = FieldDefScalar | FieldDefCrossRef | FieldDefStruct | FieldDefList;
2620
+ /**
2621
+ * True when the field's value is a typed column: a scalar other than `json` that
2622
+ * the server does not own, or a crossRef. An explicit `null` on such a field
2623
+ * reads back as absent. A json, struct or list field keeps its `null`, and a
2624
+ * readonly scalar such as `shortId` is the server's.
2625
+ */
2626
+ declare function isColumnField(def: FieldDef): boolean;
2627
+ /**
2628
+ * Per-type schema entry. Fields may be declared at registration time; MVP emits
2629
+ * `fields: {}` for types registered without a field map (e.g. mutations whose
2630
+ * shape is inferred from the resolver's return).
2631
+ */
2632
+ interface TypeSchema {
2633
+ fields: Record<string, FieldDef>;
2634
+ /** The type's natural order; absent means {@link DEFAULT_ORDERED_BY}. */
2635
+ orderedBy?: OrderedBy;
2636
+ /**
2637
+ * Same-namespace crossRefs that point at this type: `field` on `type`.
2638
+ * Where a walk can come from. Absent when none.
2639
+ */
2640
+ inboundRefs?: Array<{
2641
+ type: string;
2642
+ field: string;
2643
+ }>;
2644
+ }
2645
+ /**
2646
+ * A type's natural order: exactly one `{ field: direction }` key, the order a
2647
+ * read with no `orderBy` returns. The datasets still append `id` as the tie-break.
2648
+ */
2649
+ type OrderedBy = Record<string, 'asc' | 'desc'>;
2650
+ /** Natural order of a type that declares none: newest first. */
2651
+ declare const DEFAULT_ORDERED_BY: OrderedBy;
2652
+ /** The declared natural order, or {@link DEFAULT_ORDERED_BY}. */
2653
+ declare function naturalOrderOf(schema: Pick<TypeSchema, 'orderedBy'>): OrderedBy;
2654
+ /**
2655
+ * Per-mutation schema entry. `params` describes the input shape a client
2656
+ * sends; `returns` describes the shape a client decodes on success. Both are
2657
+ * required — an empty param bag surfaces as `{}`, never as an absent key.
2658
+ *
2659
+ * `returns` is typically a `FieldDefStruct` describing the row shape the
2660
+ * mutation produces (e.g. `create<T>` returns the inserted row; `delete<T>`
2661
+ * returns `{ id, deleted }`). `get<T>` and `update<T>` mark the struct
2662
+ * `nullable: true` because an id-miss legitimately produces `null`.
2663
+ *
2664
+ * Mutations registered without explicit `returns` metadata receive a sentinel
2665
+ * fallback (`{ kind: 'scalar', type: 'string', nullable: true }`) so substrate-pack
2666
+ * can still round-trip the response — clients treating it as "unknown shape"
2667
+ * should fall back to JSON passthrough.
2668
+ */
2669
+ interface MutationSchema {
2670
+ params: Record<string, FieldDef>;
2671
+ returns: FieldDef;
2672
+ /**
2673
+ * Present when the mutation writes rows of its namespace's type (an autoCRUD
2674
+ * create or update): with no `fields`, its result is `{ id, shortId }`.
2675
+ */
2676
+ writesRows?: true;
2677
+ }
2678
+ /**
2679
+ * The published schema shape surfaced at `GET /substrate/v1/schema`. Spec: §4.2, §6.1,
2680
+ * §7.1. `schemaVersion` is the 12-char SHA-256 prefix over the canonicalized
2681
+ * schema JSON and travels on every response envelope (§7.0) so the client can
2682
+ * detect drift.
2683
+ */
2684
+ interface Schema {
2685
+ schemaVersion: string;
2686
+ types: Record<string, TypeSchema>;
2687
+ mutations: Record<string, MutationSchema>;
2688
+ /** Present (true) on substrate-contributed virtual namespaces: no autoCRUD, no storage, query/mutate one-shot only. */
2689
+ virtual?: true;
2690
+ /**
2691
+ * The namespace's opaque per-consumer metadata (`defineNamespace` option).
2692
+ * Not part of `schemaVersion`: an annotations-only change does not move it.
2693
+ */
2694
+ annotations?: Readonly<Record<string, unknown>>;
2695
+ }
2696
+ /**
2697
+ * Per-key shape. A single frame resolves to `'single' | 'list'`, or for a
2698
+ * walk read to `'count'` (`data[key]` is `{ count }`) or `'groups'`
2699
+ * (`data[key]` is `{ groups, truncated? }`). A batched mutate op-key (array
2700
+ * of frames) resolves to `Array<'single' | 'list'>` — one shape per frame, in
2701
+ * frame-array order. The decoder keys off the discriminator: string → single
2702
+ * frame; array → batched.
2703
+ */
2704
+ type EnvelopeShape = 'single' | 'list' | 'count' | 'groups' | Array<'single' | 'list'>;
2705
+ /**
2706
+ * The response envelope. Spec: §7.0.
2707
+ *
2708
+ * Query data values are positionally encoded using the client-sent `FieldInput`
2709
+ * selection; mutation data values are positionally encoded using the declared
2710
+ * `MutationSchema.returns` (§4.2) — mutations don't carry a FieldInput
2711
+ * since there's no field selection on a return row.
2712
+ */
2713
+ interface PackEnvelope {
2714
+ schemaVersion: string;
2715
+ data: Record<string, unknown>;
2716
+ shapes: Record<string, EnvelopeShape>;
2717
+ _meta?: Record<string, unknown>;
2718
+ _pagination?: Record<string, {
2719
+ nextCursor?: string;
2720
+ hasMore: boolean;
2721
+ }>;
2722
+ /**
2723
+ * Per-top-level-key completeness. ABSENT MEANS COMPLETE — callers that
2724
+ * predate this field stay correct without change.
2725
+ *
2726
+ * Set when a bridged key could not be fully resolved (§6.5 "reads never
2727
+ * fail" means such a read still returns 200 with orphan markers). The
2728
+ * per-row `__orphan` marker says a row is missing; this says the RESULT SET
2729
+ * is partial, which a consumer cannot otherwise distinguish from an
2730
+ * genuinely empty result.
2731
+ */
2732
+ _completeness?: Record<string, {
2733
+ complete: boolean;
2734
+ unreachable?: string[];
2735
+ }>;
2736
+ /**
2737
+ * Non-fatal read diagnostics, keyed by top-level frame key. ABSENT MEANS
2738
+ * NOTHING TO REPORT — callers that predate this field stay correct without
2739
+ * change, same contract as `_completeness`.
2740
+ *
2741
+ * Deliberately NOT `errors`: a consumer doing `if (env.errors) throw` would
2742
+ * start failing on reads that succeeded.
2743
+ *
2744
+ * A client that maps rows into declared interfaces MUST surface this (log,
2745
+ * attach to the result, or throw in development): an unknown projection key
2746
+ * arrives as `null`, satisfies the interface, and the read looks correct.
2747
+ */
2748
+ _diagnostics?: Record<string, {
2749
+ /**
2750
+ * Projection keys the type does not declare. The read still returned
2751
+ * every row; each unknown key materialises as `null` on all of them,
2752
+ * indistinguishable from a declared field that is null everywhere.
2753
+ */
2754
+ unknownProjectionFields?: string[];
2755
+ /**
2756
+ * The where-clause reached the substrate without being schema-validated,
2757
+ * because the type is registered but absent from its namespace's
2758
+ * published schema (e.g. runtime-created types). The result is still
2759
+ * correct — substrates refuse a predicate they cannot honour — but the
2760
+ * engine's operator and operand check did not run.
2761
+ */
2762
+ filterValidationSkipped?: string;
2763
+ /**
2764
+ * A walk-grammar read's clauses, each marked index-served or not
2765
+ * (typed edge walks, goal 7). Annotation only: nothing is refused on
2766
+ * it. Absent on ordinary reads, and when the store has no index plan.
2767
+ */
2768
+ plan?: PlanClause[];
2769
+ }>;
2770
+ errors?: Array<{
2771
+ message: string;
2772
+ path?: string[];
2773
+ code?: string;
2774
+ /**
2775
+ * Optional structured payload — populated when the underlying error class
2776
+ * carries one (e.g. `ResolverFrameError({ field: '...' })`).
2777
+ */
2778
+ details?: Record<string, unknown>;
2779
+ }>;
2780
+ }
2781
+ /**
2782
+ * Client-sent field selection tree. Defined here rather than in
2783
+ * `@mnmal-ai/substrate-pack` so subscription and streaming machinery (§5.4, §5.5)
2784
+ * can reference the same shape without adding substrate-pack to their dependency
2785
+ * set — substrate-pack already imports from types, so moving the type upward
2786
+ * avoids the would-be cycle.
2787
+ */
2788
+ type FieldInput = {
2789
+ [field: string]: true | FieldInput;
2790
+ };
2791
+ //#endregion
2792
+ export { VirtualMutationDef as $, WalkAgg as $t, KernelLifecycleContext as A, OnMissing as An, DatasetRegistry as At, NamespaceTypeDef as B, WriteRow as Bn, IndexPlan as Bt, EmbedSourceContext as C, CanDeleteResult as Cn, RowFilterObligation as Ct, FilterOperatorDef as D, EdgeWriteInput as Dn, ConditionalUpdateResult as Dt, EngineInput as E, EdgeRef as En, AggFn as Et, KernelRouteClaim as F, IdentityKind as Fn, FilterCapabilities as Ft, Registry as G, ReadParams as Gt, PluginRouteContext as H, OrderByClause as Ht, McpToolRegistration as I, ROW_METADATA_FIELDS as In, FilterWriteLimits as It, RegistryEntryInput as J, StorageReport as Jt, RegistryBase as K, ReadResult as Kt, NamespaceConfig as L, Row as Ln, FilterWriteOptions as Lt, KernelMigrationContext as M, CLIENT_SUPPLIED_SERVER_FIELD as Mn, DropResult as Mt, KernelNamespaceContribution as N, ClientSuppliedServerFieldError as Nn, FieldKind as Nt, FrameEntry as O, EdgesStore as On, DEFAULT_FILTER_WRITE_LIMITS as Ot, KernelPlugin as P, Id as Pn, FieldPredicate as Pt, ScheduledTaskContext as Q, UpdateFieldRef as Qt, NamespaceDefinition as R, RowMetadata as Rn, FilterWriteResult as Rt, DropParticipant as S, FunctionSig as Sn, ResolverReturn as St, EngineHandle as T, EdgePatch as Tn, WriteEvent as Tt, PostWriteHook as U, PoolStats as Ut, PluginLifecycleContext as V, assertNoRepeatedIds as Vn, NestedSelection as Vt, QualifiedFrame as W, PromotionEvent as Wt, RegistryEntryResolver as X, TypeIndexState as Xt, RegistryEntryKind as Y, TxScopeHandles as Yt, ScheduledTask as Z, TypeStorageReport as Zt, naturalOrderOf as _, EXPR_AGGREGATES as _n, PolicyDecision as _t, FieldDefCrossRef as a, WriteOptions as an, RequestObserver as at, CommittedWritesHook as b, Expr as bn, Resolver as bt, FieldDefStruct as c, ResolveObservation as cn, SubstrateRequest as ct, OrderedBy as d, RuntimeErrors as dn, TlsConfig as dt, WalkAggOperand as en, VirtualTypeDef as et, PackEnvelope as f, RuntimeMetrics as fn, AfterTxWork as ft, isColumnField as g, DATE_UNITS as gn, Policy as gt, TypeSchema as h, ArgType as hn, Obligation as ht, FieldDef as i, WhereClause as in, RequestObservation as it, KernelMigration as j, ActorIdentity as jn, DeleteResult as jt, IndexDecl as k, OnDelete as kn, Dataset as kt, FieldInput as l, ResolverStat as ln, SubstrateResponse as lt, Schema as m, TypeStats as mn, Identity as mt, Dropped as n, WalkNode as nn, HttpMethod as nt, FieldDefList as o, WriteResult as on, RuntimeOp as ot, PlanClause as p, SlowRequest as pn, AutoCrudVerb as pt, RegistryEntry as q, RefOperand as qt, EnvelopeShape as r, WalkPick as rn, PluginRouter as rt, FieldDefScalar as s, ProcessStat as sn, StartOpts as st, DEFAULT_ORDERED_BY as t, WalkAggPredicate as tn, HttpHandler as tt, MutationSchema as u, RuntimeErrorEntry as un, SubstrateServer as ut, BatchResolver as v, EXPR_FUNCTIONS as vn, PolicyProbeContext as vt, EncodeContext as w, Edge as wn, TypedResolver as wt, CoordinatorPlugin as x, ExprType as xn, ResolverArgs as xt, CommittedWriteBatch as y, EXPR_LIMITS as yn, RequestContext as yt, NamespaceInstallContext as z, RowMetadataField as zn, FrameAgg as zt };