@mnmal-ai/substrate 1.0.0-alpha.1 → 1.0.0-alpha.11

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 (47) hide show
  1. package/README.md +48 -6
  2. package/dist/boot-store-C5vNZJ3i.js +4065 -0
  3. package/dist/{runtime-metrics-DwpDJ8vz.d.ts → bridge-COu0Tg_h.d.ts} +2 -87
  4. package/dist/cli-BrwW4tjO.js +3144 -0
  5. package/dist/client.d.ts +129 -8
  6. package/dist/client.js +6 -5
  7. package/dist/{agent-token-BIboWOU0.js → env-utils-CGNazUNt.js} +31 -2
  8. package/dist/{errors-D4vKuwz8.d.ts → errors-BOllGhVx.d.ts} +15 -2
  9. package/dist/{index-D7mSOFlM.d.ts → index-Cp74OcmS.d.ts} +1 -1
  10. package/dist/node-version-C5FSmwJX.js +26 -0
  11. package/dist/pack.d.ts +2 -2
  12. package/dist/pack.js +1 -1
  13. package/dist/pg-edges-store-BLsbcXkk.js +381 -0
  14. package/dist/{plugin-BuQQEzL-.js → plugin-DIYm2cZE.js} +171 -18
  15. package/dist/proposals-bootstrap.js +6 -4
  16. package/dist/proposals.config.schema.json +5 -0
  17. package/dist/proposals.d.ts +14 -4
  18. package/dist/proposals.default-config.json +2 -1
  19. package/dist/proposals.js +2 -2
  20. package/dist/recall-bootstrap.js +13 -4
  21. package/dist/recall.config.schema.json +12 -0
  22. package/dist/recall.d.ts +53 -9
  23. package/dist/recall.js +2 -2
  24. package/dist/{repl-C52wyfMs.js → repl-B8gy0YM7.js} +63 -30
  25. package/dist/{row-ByLFE2Wx.d.ts → row-DwaDU4b-.d.ts} +58 -9
  26. package/dist/run-B18qtGKc.js +2012 -0
  27. package/dist/{plugin-t2g2wX0b.d.ts → schema-CijHRfuk.d.ts} +957 -16
  28. package/dist/script-C6DSa_Na.js +134 -0
  29. package/dist/sdk.d.ts +104 -16
  30. package/dist/sdk.js +5 -110
  31. package/dist/{src-DuopV8u7.js → src-Br6aDUti.js} +153 -20
  32. package/dist/{src-5hVbeiAu.js → src-D041j6GS.js} +697 -57
  33. package/dist/{src-YXlv_RyL.js → src-DK7VEQT6.js} +4099 -1040
  34. package/dist/{src-C4DzaIQZ.js → src-Dbvuel6w.js} +392 -282
  35. package/dist/{row-DK7lPZVe.js → src-DpPdw2oL.js} +11 -11
  36. package/dist/{src-zcyUofMJ.js → src-DptNGNYQ.js} +39 -15
  37. package/dist/src-gz4I1F29.js +5097 -0
  38. package/dist/substrate-admin.js +17 -781
  39. package/dist/substrate.js +4 -5476
  40. package/dist/testing.d.ts +582 -9
  41. package/dist/testing.js +39 -3
  42. package/dist/{ws-C72iVS5T.d.ts → ws-DC8OhMaV.d.ts} +2 -3
  43. package/package.json +22 -20
  44. package/dist/dataset-DOXGRz50.js +0 -1535
  45. package/dist/env-utils-B7SGmoeJ.js +0 -21
  46. package/dist/schema-g4FQLr0V.d.ts +0 -433
  47. package/dist/src-CjEwiKXw.js +0 -2327
@@ -1,4 +1,290 @@
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
+ tombstone(id: string, at?: Date): Promise<void>;
113
+ update(patch: EdgePatch): Promise<void>;
114
+ readNextBatch(cursorId: string | null, size: number): Promise<{
115
+ edges: Edge[];
116
+ lastId: string | null;
117
+ }>;
118
+ findEdgesTo(ref: EdgeRef): Promise<Edge[]>;
119
+ /**
120
+ * Batched `findEdgesTo`: all live (non-tombstoned) inbound edges whose `to`
121
+ * matches ANY of `refs`, in a single round-trip. Empty `refs` yields an
122
+ * empty result. Callers group the flat result by `edge.to` themselves.
123
+ */
124
+ findEdgesToMany(refs: readonly EdgeRef[]): Promise<Edge[]>;
125
+ findEdgesFrom(ref: EdgeRef): Promise<Edge[]>;
126
+ canDelete(ref: EdgeRef): Promise<CanDeleteResult>;
127
+ findBlocked(): Promise<Edge[]>;
128
+ /**
129
+ * List live (non-tombstoned) edges where `from_ns === namespace` OR
130
+ * `to_ns === namespace`. Used by drop-namespace to detect dangling refs
131
+ * when `cascade === 'refuse'`. Bounded by `limit` — callers only need
132
+ * enough to decide + surface a sample.
133
+ */
134
+ findEdgesTouchingNamespace(namespace: string, limit: number): Promise<Edge[]>;
135
+ /**
136
+ * List live (non-tombstoned) edges where either endpoint matches
137
+ * `(namespace, type)`. Used by drop-type for the same cascade-refuse check.
138
+ */
139
+ findEdgesTouchingType(namespace: string, type: string, limit: number): Promise<Edge[]>;
140
+ /**
141
+ * Distinct namespaces other than `namespace` at the far end of any edge
142
+ * (tombstoned or live) touching `namespace`, or `(namespace, type)` when
143
+ * `type` is given: the namespaces `dropEdgesByNamespace` / `dropEdgesByType`
144
+ * would delete edges from.
145
+ */
146
+ namespacesLinkedTo(namespace: string, type?: string): Promise<string[]>;
147
+ /**
148
+ * Physically delete every edge (tombstoned or live) where `from_ns === namespace`
149
+ * OR `to_ns === namespace`. Used by drop-namespace when `cascade === 'delete'`.
150
+ * Returns the exact count physically removed.
151
+ *
152
+ * Same atomicity rules as `tombstone` — MUST run inside a caller-supplied
153
+ * `withTransaction` for multi-store atomicity.
154
+ */
155
+ dropEdgesByNamespace(namespace: string, within?: readonly string[]): Promise<number>;
156
+ /**
157
+ * Physically delete every edge (tombstoned or live) where either endpoint
158
+ * matches `(namespace, type)`. Used by drop-type when `cascade === 'delete'`.
159
+ * `within` restricts both drops to edges whose far end is `namespace` or one
160
+ * of the listed namespaces.
161
+ */
162
+ dropEdgesByType(namespace: string, type: string, within?: readonly string[]): Promise<number>;
163
+ /**
164
+ * Execute `fn` atomically. All ops performed on the `tx` argument run inside
165
+ * one substrate transaction. On throw, every mutation inside the callback
166
+ * rolls back; on resolve, the transaction commits and the value is returned.
167
+ *
168
+ * Nested calls flatten into the outer tx (savepoints out of scope).
169
+ * Cross-store atomicity with a sibling `Dataset.withTransaction` is the
170
+ * engine's concern; each store manages its own tx independently here.
171
+ * For enlisted cross-store atomicity, see `Dataset.withScopedTransaction`
172
+ * which yields a tx-bound `EdgesStore` alongside the tx-bound datasets.
173
+ *
174
+ * Mirrors `Dataset.withTransaction` (§4.5, G1 §5.3).
175
+ */
176
+ withTransaction<T>(fn: (tx: EdgesStore) => Promise<T>): Promise<T>;
177
+ }
178
+ //#endregion
179
+ //#region ../../packages/substrate-types/src/runtime-metrics.d.ts
180
+ /** Per-key resolver latency and error counts, as served by GET /substrate/v1/metrics. */
181
+ interface ResolverStat {
182
+ kind: 'query' | 'mutate';
183
+ calls: number;
184
+ errors: number;
185
+ p50Ms: number;
186
+ p99Ms: number;
187
+ maxMs: number;
188
+ lastAt: string;
189
+ }
190
+ interface ProcessStat {
191
+ rssMB: number;
192
+ heapUsedMB: number;
193
+ cpuUserMs: number;
194
+ cpuSystemMs: number;
195
+ eventLoopLagP99Ms: number;
196
+ inFlight: number;
197
+ }
198
+ interface SlowRequest {
199
+ key: string;
200
+ kind: 'query' | 'mutate';
201
+ durationMs: number;
202
+ at: string;
203
+ }
204
+ interface RuntimeErrorEntry {
205
+ at: string;
206
+ op: 'query' | 'mutate';
207
+ key: string;
208
+ code: string;
209
+ message: string;
210
+ /** The verified identity's id, or `'anonymous'`. */
211
+ caller?: string;
212
+ /** Present when consecutive identical `(unknown)` failures were merged into this entry. */
213
+ count?: number;
214
+ }
215
+ interface RuntimeErrors {
216
+ since: string;
217
+ errors: RuntimeErrorEntry[];
218
+ }
219
+ interface ResolveObservation {
220
+ key: string;
221
+ kind: 'query' | 'mutate';
222
+ durationMs: number;
223
+ ok: boolean;
224
+ code?: string;
225
+ message?: string;
226
+ /** The verified identity's id; absent for an anonymous caller. */
227
+ caller?: string;
228
+ }
229
+ /**
230
+ * One type's table and index statistics, for the index advisor. Counters are
231
+ * cumulative since the server's statistics were last reset. Row counts are
232
+ * estimates. A type ABSENT from a `typeStats()` result is unknown, not empty:
233
+ * never read its absence as 0 rows (a sampled store can miss a small type).
234
+ */
235
+ interface TypeStats {
236
+ namespace: string;
237
+ type: string;
238
+ liveRows: number;
239
+ insertsTotal: number;
240
+ updatesTotal: number;
241
+ deletesTotal: number;
242
+ indexes: Array<{
243
+ name: string;
244
+ scans: number;
245
+ /** The last scan (ISO), or null when none since the statistics were last reset. */
246
+ lastScanAt: string | null;
247
+ sizeBytes: number;
248
+ unique: boolean;
249
+ /** Built or adopted by the engine's index reconciler for this type. */
250
+ engineOwned: boolean;
251
+ }>;
252
+ }
253
+ /** What GET /substrate/v1/metrics returns. The new fields are optional so an older server still type-checks. */
254
+ interface RuntimeMetrics {
255
+ since: string;
256
+ requests: {
257
+ query: {
258
+ total: number;
259
+ ok: number;
260
+ error: number;
261
+ durationMsSum: number;
262
+ durationMsMax: number;
263
+ };
264
+ mutate: {
265
+ total: number;
266
+ ok: number;
267
+ error: number;
268
+ durationMsSum: number;
269
+ durationMsMax: number;
270
+ };
271
+ };
272
+ subscriptions: {
273
+ active: number;
274
+ maxQueueDepth?: number;
275
+ entries?: {
276
+ id: string;
277
+ ns: string;
278
+ type: string;
279
+ queueLength: number;
280
+ }[];
281
+ };
282
+ events: Record<string, number>;
283
+ resolvers?: Record<string, ResolverStat>;
284
+ process?: ProcessStat;
285
+ slowest?: SlowRequest[];
286
+ }
287
+ //#endregion
2
288
  //#region ../../packages/substrate-types/src/dataset.d.ts
3
289
  /**
4
290
  * Handles yielded by `Dataset.withScopedTransaction`. Contains the
@@ -21,12 +307,12 @@ interface TxScopeHandles {
21
307
  * substrate layer — the validator enforces `null` only on those two operators.
22
308
  */
23
309
  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;
310
+ eq?: number | string | boolean | Date | null | RefOperand;
311
+ neq?: number | string | boolean | Date | null | RefOperand;
312
+ gt?: number | string | Date | RefOperand;
313
+ gte?: number | string | Date | RefOperand;
314
+ lt?: number | string | Date | RefOperand;
315
+ lte?: number | string | Date | RefOperand;
30
316
  between?: [number | string | Date, number | string | Date];
31
317
  in?: Array<number | string | boolean | Date>;
32
318
  nin?: Array<number | string | boolean | Date>;
@@ -49,34 +335,129 @@ type WhereClause = {
49
335
  } | {
50
336
  NOT: WhereClause;
51
337
  } | {
338
+ _walk: WalkNode;
339
+ } | WalkAggPredicate | {
340
+ _walk?: never;
341
+ _agg?: never;
52
342
  [field: string]: FieldPredicate | WhereClause | unknown;
53
343
  };
344
+ type WalkPick = {
345
+ latest: string;
346
+ } | {
347
+ max: string;
348
+ } | {
349
+ min: string;
350
+ };
351
+ interface WalkNode {
352
+ as?: string;
353
+ /** `'<ns>/<Type>.<field>'`: inbound edge. */
354
+ in?: string;
355
+ /** `'<field>'`: outbound edge. */
356
+ out?: string;
357
+ where?: WhereClause;
358
+ quantifier?: 'any' | 'none' | 'all';
359
+ pick?: WalkPick;
360
+ }
361
+ /** `'$.f'` | `'<as>.f'` | `'_agg.<name>'` */
362
+ interface RefOperand {
363
+ $ref: string;
364
+ times?: number;
365
+ plus?: number;
366
+ }
367
+ type AggFn = {
368
+ count: true;
369
+ } | {
370
+ avg: string;
371
+ } | {
372
+ sum: string;
373
+ } | {
374
+ min: string;
375
+ } | {
376
+ max: string;
377
+ } | {
378
+ latest: {
379
+ by: string;
380
+ value: string;
381
+ };
382
+ };
383
+ type WalkAgg = {
384
+ in?: string;
385
+ out?: string;
386
+ where?: WhereClause;
387
+ } & AggFn;
388
+ type FrameAgg = Exclude<AggFn, {
389
+ latest: unknown;
390
+ }>;
391
+ interface BucketSpec {
392
+ field: string;
393
+ by: 'day' | 'hour';
394
+ }
395
+ type DeriveSpec = {
396
+ diff: [string, string];
397
+ } | {
398
+ ratio: [string, string];
399
+ };
400
+ interface NestedSelection {
401
+ _walk: {
402
+ in?: string;
403
+ out?: string;
404
+ where?: WhereClause;
405
+ };
406
+ fields: Record<string, unknown>;
407
+ where?: WhereClause;
408
+ orderBy?: OrderByClause[];
409
+ limit?: number;
410
+ }
411
+ /** An `_agg` comparison operand: a number, a date or string (by the aggregate's kind), or a `$ref`. */
412
+ type WalkAggOperand = number | string | RefOperand;
413
+ /** `{ _agg: <walk aggregate>, <cmp>: <operand> }` */
414
+ type WalkAggPredicate = {
415
+ _agg: WalkAgg;
416
+ } & ({
417
+ eq: WalkAggOperand;
418
+ } | {
419
+ neq: WalkAggOperand;
420
+ } | {
421
+ gt: WalkAggOperand;
422
+ } | {
423
+ gte: WalkAggOperand;
424
+ } | {
425
+ lt: WalkAggOperand;
426
+ } | {
427
+ lte: WalkAggOperand;
428
+ });
54
429
  /**
55
430
  * OrderBy clause. One entry per field, direction per field. Engine appends an
56
431
  * implicit `{ id: 'asc' }` when callers omit `id` as the final tie-breaker — a
57
432
  * deterministic tie-break is what makes cursor pagination stable across pages.
58
433
  */
59
434
  interface OrderByClause {
435
+ /** A `$`-prefixed key orders by a `params.derive` entry. */
60
436
  [field: string]: 'asc' | 'desc';
61
437
  }
62
438
  interface ReadParams {
439
+ /** Aborts the read. Substrates that can (PgDataset) cancel the in-flight statement; others MAY ignore it. */
440
+ signal?: AbortSignal;
63
441
  id?: Id;
64
442
  ids?: Id[];
65
443
  includeDeleted?: boolean;
66
444
  where?: WhereClause;
67
445
  orderBy?: OrderByClause[];
68
446
  limit?: number;
447
+ /** Return a match count (`ReadResult.count`). */
448
+ count?: boolean;
449
+ aggregate?: Record<string, FrameAgg>;
450
+ bucket?: BucketSpec;
451
+ derive?: Record<string, DeriveSpec>;
452
+ /** Per-row walk aggregates, lifted out of `fields` by the list resolver. */
453
+ agg?: Record<string, WalkAgg>;
454
+ /** Per-row nested selections, lifted out of `fields` by the list resolver. */
455
+ nested?: Record<string, NestedSelection>;
69
456
  /**
70
457
  * Opaque cursor produced by a prior `read()` call. Base64url-encoded JSON
71
458
  * (server-built, server-consumed). Callers should treat it as a black box.
72
459
  */
73
460
  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
461
  /**
81
462
  * Optional caller-supplied projection hint. Substrates MAY use this to
82
463
  * narrow the body keys they materialise from storage — e.g. PgDataset
@@ -109,9 +490,16 @@ interface ReadResult {
109
490
  rows: Row[];
110
491
  nextCursor?: string;
111
492
  hasMore: boolean;
493
+ count?: number;
494
+ groups?: Array<Record<string, unknown>>;
495
+ truncated?: boolean;
112
496
  }
113
497
  interface WriteResult {
114
498
  written: number;
499
+ /**
500
+ * One per written row, in input order: a supplied id verbatim, otherwise
501
+ * the id the dataset assigned. Under `insertOnly`, skipped rows are absent.
502
+ */
115
503
  ids: Id[];
116
504
  /**
117
505
  * Merged rows after upsert — substrates populate this with the post-merge
@@ -165,6 +553,11 @@ interface FilterCapabilities {
165
553
  orderBy: boolean;
166
554
  /** Does the dataset honour cursor-based pagination? */
167
555
  cursor: boolean;
556
+ /**
557
+ * Does the dataset answer the typed-edge-walk grammar? The engine refuses a
558
+ * walk frame on a dataset without it (`walks_unsupported_store`).
559
+ */
560
+ walks?: boolean;
168
561
  }
169
562
  interface UpdateFieldRef {
170
563
  ns?: string;
@@ -207,7 +600,7 @@ interface Dataset {
207
600
  */
208
601
  readonly txScopeId: string;
209
602
  read(type: string, params: ReadParams): Promise<ReadResult>;
210
- write(type: string, rows: Row[], options?: WriteOptions): Promise<WriteResult>;
603
+ write(type: string, rows: WriteRow[], options?: WriteOptions): Promise<WriteResult>;
211
604
  tombstone(type: string, ids: Id[]): Promise<DeleteResult>;
212
605
  purge(type: string, ids: Id[]): Promise<DeleteResult>;
213
606
  /**
@@ -275,7 +668,7 @@ interface Dataset {
275
668
  * Run `fn` while holding a mutual-exclusion lock named `key`, waiting until
276
669
  * it is free. Excludes every other holder of the same key on this substrate:
277
670
  * pg holds a session advisory lock, so the exclusion spans processes sharing
278
- * the database; memory excludes holders on this dataset instance only.
671
+ * the store's database schema; memory excludes holders on this dataset instance only.
279
672
  * Released when `fn` settles, whether it resolves or throws, except on a
280
673
  * pg tx-bound dataset, where it is held until the transaction ends so the
281
674
  * next holder sees what this one committed. Not re-entrant:
@@ -287,6 +680,104 @@ interface Dataset {
287
680
  * @throws whatever `fn` throws, after the lock is released
288
681
  */
289
682
  withAdvisoryLock?<T>(key: string, fn: () => Promise<T>): Promise<T>;
683
+ /**
684
+ * Prepare dedicated storage for `type` in this namespace, moving any rows
685
+ * already written for it there. Idempotent, and safe beside concurrent
686
+ * reads and writes: no row is lost, duplicated or seen twice. That holds for
687
+ * READ COMMITTED readers; a REPEATABLE READ snapshot taken before the move
688
+ * can miss the moved rows. Also builds the type's indexes, including its
689
+ * declared `indexes`, and throws when a unique one cannot be built.
690
+ *
691
+ * `indexes` picks which: `unique` builds only the unique ones and leaves the
692
+ * rest `pending`, for a later `nonUnique` call to build. `signal` cancels an
693
+ * in-flight non-unique build, which the next call retries. Default `all`.
694
+ *
695
+ * OPTIONAL. Backends without per-type storage omit it.
696
+ */
697
+ ensureTypeStorage?(type: string, opts?: {
698
+ indexes?: 'all' | 'unique' | 'nonUnique';
699
+ signal?: AbortSignal;
700
+ }): Promise<void>;
701
+ /**
702
+ * Where `type`'s rows live and how its declared indexes stand, read from the
703
+ * catalog without building anything.
704
+ *
705
+ * OPTIONAL. Backends without per-type storage omit it.
706
+ */
707
+ describeStorage?(type: string): Promise<TypeStorageReport>;
708
+ /**
709
+ * The database-wide storage layout and what an operator should look at:
710
+ * rows parked in DEFAULT partitions, indexes and CHECK constraints on the
711
+ * `rows` parent, indexes this process has yet to build or failed to build,
712
+ * and a partition count over `partitionBudget`; and, as `info`, engine
713
+ * indexes no longer declared and open index recommendations. Never changes
714
+ * anything.
715
+ *
716
+ * OPTIONAL. Backends without per-type storage omit it.
717
+ */
718
+ storageReport?(opts: {
719
+ partitionBudget: number;
720
+ }): Promise<StorageReport>;
721
+ /**
722
+ * Table and index statistics for every type the store holds, every
723
+ * namespace, read from the database's own statistics. Estimates, never a
724
+ * count. Never changes anything.
725
+ *
726
+ * OPTIONAL. Backends without such statistics omit it.
727
+ */
728
+ typeStats?(): Promise<TypeStats[]>;
729
+ /**
730
+ * The indexes the engine wants for `namespace`/`type` (any namespace, not
731
+ * only this one), with each one's leading keys, and the type's size hint.
732
+ * Undefined when the type is unknown. Never changes anything.
733
+ *
734
+ * OPTIONAL. Backends that build no indexes omit it.
735
+ */
736
+ indexPlan?(type: string, namespace: string): Promise<IndexPlan | undefined>;
737
+ /**
738
+ * Drop index `name` of this namespace's `type`, a signed `propose_index`
739
+ * remove. Only an engine-owned, non-unique index the engine no longer wants
740
+ * (a leftover or a retracted declaration) is dropped; anything else is
741
+ * refused with an error whose `code` is `unknown_index`, `not_engine_owned`,
742
+ * `unique_index` or `still_desired`, and nothing is dropped. A drop that
743
+ * waits longer than `lockTimeoutMs` on another transaction is abandoned with
744
+ * `index_drop_timeout`; the index may then be left invalid, and a retry drops it.
745
+ *
746
+ * OPTIONAL. Backends that build no indexes omit it (the memory dataset):
747
+ * there, `propose_index remove` has no index to find, so it is refused
748
+ * `unknown_index`, and a remove recommendation it names is superseded.
749
+ */
750
+ dropEngineIndex?(type: string, name: string, opts?: {
751
+ /** Runs once every check has passed, just before the drop; a throw drops nothing. */
752
+ beforeDrop?: () => Promise<void>;
753
+ /** The drop's bound on each lock wait, in ms. Defaults to the backend's own. */
754
+ lockTimeoutMs?: number;
755
+ }): Promise<void>;
756
+ /**
757
+ * Drop the engine-owned indexes of this namespace, after a namespace drop
758
+ * has committed: its types leave the engine, so nothing would remove them.
759
+ * Non-unique ones only, unless `unique`, which the caller passes only when
760
+ * the namespace is declared nowhere (in no code and no live proposal): a
761
+ * declared namespace's types come back, and until the reconciler rebuilds a
762
+ * unique index, writes would not be held to it. Returns the names dropped.
763
+ * Never inside a transaction; a tx-bound instance throws. A lock wait that
764
+ * times out throws `index_drop_timeout`, leaving the rest for
765
+ * `storageReport` to list as orphaned.
766
+ *
767
+ * OPTIONAL. Backends that build no indexes omit it.
768
+ */
769
+ dropNamespaceIndexes?(opts?: {
770
+ unique?: boolean;
771
+ }): Promise<string[]>;
772
+ /**
773
+ * The entry of this namespace's `type`'s declared `indexes` whose index is
774
+ * named `name`, or undefined when no declaration builds it (a derived or
775
+ * engine index, or none). `propose_index remove` retracts it when it is a
776
+ * proposed type's. Never changes anything.
777
+ *
778
+ * OPTIONAL. Backends that build no indexes omit it.
779
+ */
780
+ indexDeclarationOf?(type: string, name: string): Promise<IndexDecl | undefined>;
290
781
  /**
291
782
  * Execute `fn` atomically. All ops performed on the `tx` argument run inside
292
783
  * one substrate transaction. If `fn` throws or returns a rejected promise,
@@ -416,6 +907,47 @@ interface DatasetRegistry {
416
907
  */
417
908
  dispose?(ns: string): void;
418
909
  }
910
+ /**
911
+ * One declared index of a type and its state: as the catalog holds it, or,
912
+ * while this process defers or runs its build, `pending`, `building` or
913
+ * `failed`. Filters on an index that is not `valid` scan.
914
+ */
915
+ interface TypeIndexState {
916
+ name: string;
917
+ fields: string[];
918
+ unique: boolean;
919
+ state: 'valid' | 'building' | 'pending' | 'invalid' | 'failed';
920
+ outcome: 'created' | 'adopted' | 'swapped' | 'unchanged';
921
+ /** Why a `failed` index failed: an error code, then a short reason. */
922
+ error?: string;
923
+ /** Why a `pending` index was not built: the free-disk check refused it. */
924
+ reason?: 'insufficient_disk';
925
+ }
926
+ /** Where a type's rows live: `partition` is set on the partitioned layout once the type has one. */
927
+ interface TypeStorageReport {
928
+ layout: 'legacy' | 'partitioned';
929
+ partition?: string;
930
+ indexes: TypeIndexState[];
931
+ }
932
+ interface StorageReport {
933
+ /** `unknown` when the report itself failed. */
934
+ layout: 'legacy' | 'partitioned' | 'unknown';
935
+ warnings: string[];
936
+ /** Worth showing, not a fault: the doctor prints these without failing. */
937
+ info?: string[];
938
+ }
939
+ /** See `Dataset.indexPlan`. */
940
+ interface IndexPlan {
941
+ /** Each desired index, its keys in order: a derived index ends with the type's natural order. */
942
+ indexes: Array<{
943
+ name: string;
944
+ keys: Array<[string, 'asc' | 'desc']>;
945
+ unique: boolean;
946
+ /** A partial index's predicate beyond `live`: it serves only reads that `eq` each field to its value. */
947
+ only?: Record<string, boolean | null>;
948
+ }>;
949
+ expectedSize?: 'small' | 'large';
950
+ }
419
951
  //#endregion
420
952
  //#region ../../packages/substrate-types/src/policy.d.ts
421
953
  interface Identity {
@@ -545,6 +1077,12 @@ interface ResolverArgs {
545
1077
  * the tx-scoped `dataset` handle instead. Absent on the query/subscribe path.
546
1078
  */
547
1079
  resolveDataset?: (ns: string) => Dataset | undefined;
1080
+ /**
1081
+ * Engine-injected on mutate: how many frame entries share this call's
1082
+ * transaction, itself included. A resolver whose effect waits on other
1083
+ * transactions (a concurrent index drop) refuses to share one.
1084
+ */
1085
+ txEntries?: number;
548
1086
  }
549
1087
  /**
550
1088
  * Post-commit write announcement consumed by the engine's subscription bus
@@ -784,6 +1322,12 @@ interface RegistryEntryInput {
784
1322
  kind?: RegistryEntryKind;
785
1323
  params?: Record<string, FieldDef>;
786
1324
  returns?: FieldDef;
1325
+ /** Natural order of a `kind: 'type'` entry; published on its `TypeSchema`. */
1326
+ orderedBy?: OrderedBy;
1327
+ /** Declared indexes of a `kind: 'type'` entry installed at runtime (a proposed type). */
1328
+ indexes?: IndexDecl[];
1329
+ /** A proposed type's size hint; `large` lowers the index advisor's row minimum. */
1330
+ expectedSize?: 'small' | 'large';
787
1331
  /**
788
1332
  * Semantic verb for autoCRUD-registered mutations. The engine
789
1333
  * threads this into `RequestContext.verb` at dispatch so type-/plugin-level
@@ -807,6 +1351,12 @@ interface RegistryEntry {
807
1351
  params: Record<string, FieldDef>;
808
1352
  /** Present only for `kind: 'mutation'` entries that declared a return shape. */
809
1353
  returns?: FieldDef;
1354
+ /** Mirrors `RegistryEntryInput.orderedBy`. */
1355
+ orderedBy?: OrderedBy;
1356
+ /** Mirrors `RegistryEntryInput.indexes`. */
1357
+ indexes?: IndexDecl[];
1358
+ /** Mirrors `RegistryEntryInput.expectedSize`. */
1359
+ expectedSize?: 'small' | 'large';
810
1360
  /** Mirrors `RegistryEntryInput.verb` — present when the registrant stamped one. */
811
1361
  verb?: AutoCrudVerb;
812
1362
  }
@@ -943,6 +1493,29 @@ interface EmbedSourceContext {
943
1493
  id: string;
944
1494
  }) => Promise<Row | null>;
945
1495
  }
1496
+ /**
1497
+ * A declared index over a type's scalar or crossRef fields, built by the
1498
+ * engine with the same expressions the filter compiler emits.
1499
+ *
1500
+ * - `fields`: key columns in order; `dir` defaults to `asc`. Both directions
1501
+ * sort NULLs last, as `orderBy` does.
1502
+ * - `unique`: refused at build time while duplicate live values exist.
1503
+ * - `where`: always ANDed with "not deleted". `'live'` adds nothing more; a
1504
+ * record adds equality to a boolean (or IS NULL for `null`) on the type's
1505
+ * own fields. Only those operands compile to literals, which a generic plan
1506
+ * needs to match the index predicate.
1507
+ * - `name`: the pg index name to own or adopt; generated when omitted. It may
1508
+ * not end in `__swap`, the reconciler's temporary suffix.
1509
+ */
1510
+ interface IndexDecl {
1511
+ fields: Array<string | {
1512
+ field: string;
1513
+ dir: 'asc' | 'desc';
1514
+ }>;
1515
+ unique?: boolean;
1516
+ where?: 'live' | Record<string, boolean | null>;
1517
+ name?: string;
1518
+ }
946
1519
  /**
947
1520
  * Structural shape of a type definition, mirroring `TypeInput` from
948
1521
  * @mnmal-ai/substrate-sdk (redeclared to avoid a substrate-types → substrate-sdk import
@@ -991,6 +1564,14 @@ interface NamespaceTypeDef {
991
1564
  lexical?: {
992
1565
  source: (row: Row) => string | null | undefined | Promise<string | null | undefined>;
993
1566
  };
1567
+ /** Composite and unique indexes the engine builds for this type. See {@link IndexDecl}. */
1568
+ indexes?: IndexDecl[];
1569
+ /** The type's natural order, the default read order. See {@link OrderedBy}. */
1570
+ orderedBy?: OrderedBy;
1571
+ /** See the SDK's `TypeInput.ordering`. */
1572
+ ordering?: 'visible';
1573
+ /** Size hint: `large` lowers the index advisor's row minimum for this type. */
1574
+ expectedSize?: 'small' | 'large';
994
1575
  [key: string]: unknown;
995
1576
  }
996
1577
  interface NamespaceDefinition {
@@ -1376,6 +1957,12 @@ interface KernelPlugin {
1376
1957
  * to populate this slot).
1377
1958
  */
1378
1959
  migrations?: KernelMigration[];
1960
+ /**
1961
+ * Names of the tables (in the store's schema) this plugin keeps data in.
1962
+ * `substrate-admin migrate-store` copies them alongside the engine's own
1963
+ * tables and refuses a source table that nothing declares.
1964
+ */
1965
+ storeTables?: readonly string[];
1379
1966
  /**
1380
1967
  * Filter operators contributed to the where-clause grammar. Each entry
1381
1968
  * registers a custom operator (e.g. `embeddingSimilar`) the engine's
@@ -1406,6 +1993,11 @@ interface KernelPlugin {
1406
1993
  * coordinators arrive.
1407
1994
  */
1408
1995
  onKernelReady?(ctx: KernelLifecycleContext): void | Promise<void>;
1996
+ /**
1997
+ * Stops whatever `onKernelReady` started (timers, workers). The host calls
1998
+ * it before it closes the store, and may call it more than once.
1999
+ */
2000
+ onKernelStop?(): void | Promise<void>;
1409
2001
  /**
1410
2002
  * Contribute postWrite hooks for a coordinator namespace at install time.
1411
2003
  * Called by the engine ONCE per registered coordinator namespace, AFTER
@@ -1437,6 +2029,13 @@ interface KernelMigrationContext {
1437
2029
  * binary is running against the in-memory dataset.
1438
2030
  */
1439
2031
  sql: unknown | null;
2032
+ /**
2033
+ * How `rows` is stored: `'partitioned'` (LIST(namespace) then LIST(type)) or
2034
+ * `'legacy'` (one plain table). `null` on the in-memory dataset. PG refuses
2035
+ * a unique index on a partitioned parent unless it includes every partition
2036
+ * key column (`namespace`, `type`), so such DDL on `rows` must branch on this.
2037
+ */
2038
+ layout: 'legacy' | 'partitioned' | null;
1440
2039
  metric(name: string, payload?: unknown): void;
1441
2040
  }
1442
2041
  /**
@@ -1562,7 +2161,349 @@ interface KernelLifecycleContext {
1562
2161
  plugin: string;
1563
2162
  namespace: string;
1564
2163
  }[];
2164
+ /**
2165
+ * Whether this boot may start background work (timers, backfills). False
2166
+ * when the host boots the store only to prepare or copy into it, as
2167
+ * `substrate-admin migrate-store` does: do convergent reconciliation, start
2168
+ * nothing that writes later.
2169
+ */
2170
+ background: boolean;
1565
2171
  metric(name: string, payload?: unknown): void;
1566
2172
  }
1567
2173
  //#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 };
2174
+ //#region ../../packages/substrate-types/src/schema.d.ts
2175
+ /**
2176
+ * One clause of a walk-grammar read, marked index-served or not (typed edge
2177
+ * walks, goal 7). `path` is inside the frame entry, e.g. `['where', '_walk']`.
2178
+ * An unserved clause names the index that would serve it in `recommend`, when
2179
+ * one can be declared.
2180
+ */
2181
+ interface PlanClause {
2182
+ path: string[];
2183
+ kind: 'hop' | 'filter' | 'order' | 'ref' | 'derive' | 'bucket' | 'agg';
2184
+ served: boolean;
2185
+ recommend?: IndexDecl;
2186
+ }
2187
+ /** Words of a natural-language question the parse did not use, and why. */
2188
+ interface Dropped {
2189
+ span: string;
2190
+ reason: string;
2191
+ /**
2192
+ * What the loss changes. `filter`: which rows match (a filter, comparison,
2193
+ * walk, time window or connective), so a count, aggregate, `$ref` or pick
2194
+ * reading that lost it is `unknown`. `order`: an ordering, limit or
2195
+ * projection; the matched rows are the same and the answer stands.
2196
+ */
2197
+ kind: 'filter' | 'order';
2198
+ }
2199
+ interface FieldDefScalar {
2200
+ kind: 'scalar';
2201
+ /**
2202
+ * `'json'` is an opaque body: msgpack-friendly nested object/array, value
2203
+ * round-trips unchanged, but no per-leaf filtering or ordering. Use it for
2204
+ * freeform payloads (audit details, debug bags). When you need filterable
2205
+ * inner keys, use `field.struct` instead.
2206
+ */
2207
+ type: 'string' | 'number' | 'boolean' | 'date' | 'json';
2208
+ /**
2209
+ * Tri-state. `true` builds an index (a btree; a GIN for json). Unset means
2210
+ * no index. `false` means no index AND not a search handle: long-form
2211
+ * prose that NL and similar consumers leave out. The field filters either
2212
+ * way. Physical consumers test `=== true`, so never normalise unset to
2213
+ * `false`.
2214
+ */
2215
+ indexed?: boolean;
2216
+ /** Metadata: when true, consumers SHOULD expect `null` as a legitimate value. */
2217
+ nullable?: boolean;
2218
+ /**
2219
+ * Metadata: when true, this field is populated by the engine (not the caller)
2220
+ * and write attempts that include it MUST be rejected. Used for the
2221
+ * auto-injected `shortId` field. Consumers (autoCRUD create, schema
2222
+ * publisher, client codegen) rely on this flag as a declarative contract —
2223
+ * do not imperatively guard readonly fields without also flipping this.
2224
+ */
2225
+ readonly?: boolean;
2226
+ /**
2227
+ * Server-managed date flag. Clients may not supply the field in the
2228
+ * indicated phase(s) — autoCRUD rejects it with `client_supplied_server_field`:
2229
+ * - 'create' — on create only
2230
+ * - 'update' — on update only
2231
+ * - 'both' — on create and update
2232
+ * Nothing stamps it automatically: server-side code (a custom resolver or a
2233
+ * system writer such as `applyUpdatePatch`) sets it. A non-nullable,
2234
+ * non-defaulted field gated on create is refused by autoCRUD at install,
2235
+ * since nothing could fill it. Only honoured on `type: 'date'`.
2236
+ */
2237
+ serverSet?: 'create' | 'update' | 'both';
2238
+ /**
2239
+ * Controlled vocabulary. When present, the value MUST be one of these
2240
+ * strings. Consumed by autoCRUD write-validation (covers memory + pg
2241
+ * uniformly) and surfaced verbatim in the published schema. `type` stays
2242
+ * 'string' — an enum is a constrained string, so pack / client decoder /
2243
+ * where-validation treat it identically with no new branch.
2244
+ */
2245
+ enum?: readonly string[];
2246
+ /**
2247
+ * Governed vocabulary: alias -> canonical value (each canonical value is in
2248
+ * `enum`). autoCRUD stores the canonical value when an alias is written, and
2249
+ * `where` filters on the field match it, so synonyms converge instead of
2250
+ * sprawling. Matching is case-insensitive. Published in the schema so every
2251
+ * client reads the same table.
2252
+ */
2253
+ aliases?: Readonly<Record<string, string>>;
2254
+ /**
2255
+ * `false` admits values outside `enum` (aliases are still normalised): a
2256
+ * transition mode for adopting a vocabulary over existing free-string data.
2257
+ * Absent means strict.
2258
+ */
2259
+ strict?: boolean;
2260
+ /**
2261
+ * Alias (or canonical value) -> sibling fields it implies, e.g.
2262
+ * `done -> { resolution: 'completed' }`. On write the siblings are filled
2263
+ * when the caller omits them and a contradicting value is refused; an
2264
+ * `eq` filter on the alias also matches the implied siblings. Keys match
2265
+ * case-insensitively.
2266
+ */
2267
+ implies?: Readonly<Record<string, Readonly<Record<string, unknown>>>>;
2268
+ /**
2269
+ * Default value applied by autoCRUD `create` when the caller omits this
2270
+ * field. Two effects: (1) the row is written with the default instead of
2271
+ * being absent; (2) it satisfies the non-nullable presence check, so a
2272
+ * non-nullable scalar WITHOUT a default that is omitted on create is
2273
+ * rejected (`missing_required_field`) — that is how "required" is expressed.
2274
+ * Only applied on create (update is a partial patch). The value should match
2275
+ * `type` (booleans for boolean, ISO string for date, etc.).
2276
+ */
2277
+ default?: string | number | boolean;
2278
+ }
2279
+ interface FieldDefCrossRef {
2280
+ kind: 'crossRef';
2281
+ ns: string;
2282
+ type: string;
2283
+ onDelete: OnDelete;
2284
+ /**
2285
+ * Write-time existence policy. 'reject' refuses a create/update
2286
+ * whose crossRef value points at a non-existent or tombstoned target;
2287
+ * 'allow' is the default when absent. Consulted only at write time, never
2288
+ * stored on the edge. Enforced only for locally-resolvable
2289
+ * target namespaces (compose rejects 'reject' on bridged/unknown targets).
2290
+ */
2291
+ onMissing?: OnMissing;
2292
+ /**
2293
+ * Substrate-level indexing hint for the edge-source column, mirroring
2294
+ * `FieldDefScalar.indexed`, so the dataset need not infer it from the
2295
+ * presence of the crossRef itself.
2296
+ */
2297
+ indexed?: boolean;
2298
+ /** Metadata: when true, consumers SHOULD expect `null` as a legitimate value. */
2299
+ nullable?: boolean;
2300
+ /**
2301
+ * Activation-spreading opt-in. When `spread: true`,
2302
+ * recall hits on a row touching this crossRef propagate a damped salience
2303
+ * boost to the linked row (forward direction, X → row X references) AND to
2304
+ * rows whose same field points at the boosted row (backward direction,
2305
+ * X ← rows that reference X). Default off — operators opt in per-edge to
2306
+ * avoid polluting attention with low-signal edges (e.g. Decision.sessionId).
2307
+ *
2308
+ * Damping (typically 0.3) + depth bound (1) are configured on the recall
2309
+ * substrate, not per-field.
2310
+ */
2311
+ spread?: boolean;
2312
+ /**
2313
+ * Cross-TYPE opt-in for the recall expansion path.
2314
+ * Only meaningful when `spread: true` (the SDK `field.crossRef` builder
2315
+ * rejects `expandCrossType` without `spread` at registration). When both
2316
+ * are set, BACKWARD walks via this edge are allowed to surface neighbours
2317
+ * whose type differs from the request's `scope.type` (e.g. a Concept-typed
2318
+ * query surfaces Decisions via inbound `Decision.establishesConcept`).
2319
+ *
2320
+ * Cross-NAMESPACE expansion is always rejected regardless of this flag.
2321
+ * Forward walks always honour scope strictly: allowing them lets
2322
+ * neighbour-type rows displace the rows that establish them in the ranking.
2323
+ * Default false.
2324
+ */
2325
+ expandCrossType?: boolean;
2326
+ }
2327
+ interface FieldDefStruct {
2328
+ kind: 'struct';
2329
+ fields: Record<string, FieldDef>;
2330
+ /** Metadata: when true, consumers SHOULD expect `null` as a legitimate value. */
2331
+ nullable?: boolean;
2332
+ }
2333
+ interface FieldDefList {
2334
+ kind: 'list';
2335
+ of: FieldDef;
2336
+ /**
2337
+ * When true, the `has` (element membership) operator is backed by a GIN
2338
+ * index on `(body -> '<field>')`. List fields are stored as native JSONB
2339
+ * arrays. Default false — `has` on an unindexed list still works, by
2340
+ * scanning the type's rows. The substrate binary derives the matching GIN
2341
+ * as an idempotent migration at boot from this flag; no hand-authored
2342
+ * migration is needed per field.
2343
+ */
2344
+ indexed?: boolean;
2345
+ /** Metadata: when true, consumers SHOULD expect `null` as a legitimate value. */
2346
+ nullable?: boolean;
2347
+ }
2348
+ type FieldDef = FieldDefScalar | FieldDefCrossRef | FieldDefStruct | FieldDefList;
2349
+ /**
2350
+ * Per-type schema entry. Fields may be declared at registration time; MVP emits
2351
+ * `fields: {}` for types registered without a field map (e.g. mutations whose
2352
+ * shape is inferred from the resolver's return).
2353
+ */
2354
+ interface TypeSchema {
2355
+ fields: Record<string, FieldDef>;
2356
+ /** The type's natural order; absent means {@link DEFAULT_ORDERED_BY}. */
2357
+ orderedBy?: OrderedBy;
2358
+ /**
2359
+ * Same-namespace crossRefs that point at this type: `field` on `type`.
2360
+ * Where a walk can come from. Absent when none.
2361
+ */
2362
+ inboundRefs?: Array<{
2363
+ type: string;
2364
+ field: string;
2365
+ }>;
2366
+ }
2367
+ /**
2368
+ * A type's natural order: exactly one `{ field: direction }` key, the order a
2369
+ * read with no `orderBy` returns. The datasets still append `id` as the tie-break.
2370
+ */
2371
+ type OrderedBy = Record<string, 'asc' | 'desc'>;
2372
+ /** Natural order of a type that declares none: newest first. */
2373
+ declare const DEFAULT_ORDERED_BY: OrderedBy;
2374
+ /** The declared natural order, or {@link DEFAULT_ORDERED_BY}. */
2375
+ declare function naturalOrderOf(schema: Pick<TypeSchema, 'orderedBy'>): OrderedBy;
2376
+ /**
2377
+ * Per-mutation schema entry. `params` describes the input shape a client
2378
+ * sends; `returns` describes the shape a client decodes on success. Both are
2379
+ * required — an empty param bag surfaces as `{}`, never as an absent key.
2380
+ *
2381
+ * `returns` is typically a `FieldDefStruct` describing the row shape the
2382
+ * mutation produces (e.g. `create<T>` returns the inserted row; `delete<T>`
2383
+ * returns `{ id, deleted }`). `get<T>` and `update<T>` mark the struct
2384
+ * `nullable: true` because an id-miss legitimately produces `null`.
2385
+ *
2386
+ * Mutations registered without explicit `returns` metadata receive a sentinel
2387
+ * fallback (`{ kind: 'scalar', type: 'string', nullable: true }`) so substrate-pack
2388
+ * can still round-trip the response — clients treating it as "unknown shape"
2389
+ * should fall back to JSON passthrough.
2390
+ */
2391
+ interface MutationSchema {
2392
+ params: Record<string, FieldDef>;
2393
+ returns: FieldDef;
2394
+ }
2395
+ /**
2396
+ * The published schema shape surfaced at `GET /substrate/v1/schema`. Spec: §4.2, §6.1,
2397
+ * §7.1. `schemaVersion` is the 12-char SHA-256 prefix over the canonicalized
2398
+ * schema JSON and travels on every response envelope (§7.0) so the client can
2399
+ * detect drift.
2400
+ */
2401
+ interface Schema {
2402
+ schemaVersion: string;
2403
+ types: Record<string, TypeSchema>;
2404
+ mutations: Record<string, MutationSchema>;
2405
+ /** Present (true) on substrate-contributed virtual namespaces: no autoCRUD, no storage, query/mutate one-shot only. */
2406
+ virtual?: true;
2407
+ /**
2408
+ * The namespace's opaque per-consumer metadata (`defineNamespace` option).
2409
+ * Not part of `schemaVersion`: an annotations-only change does not move it.
2410
+ */
2411
+ annotations?: Readonly<Record<string, unknown>>;
2412
+ }
2413
+ /**
2414
+ * Per-key shape. A single frame resolves to `'single' | 'list'`, or for a
2415
+ * walk read to `'count'` (`data[key]` is `{ count }`) or `'groups'`
2416
+ * (`data[key]` is `{ groups, truncated? }`). A batched mutate op-key (array
2417
+ * of frames) resolves to `Array<'single' | 'list'>` — one shape per frame, in
2418
+ * frame-array order. The decoder keys off the discriminator: string → single
2419
+ * frame; array → batched.
2420
+ */
2421
+ type EnvelopeShape = 'single' | 'list' | 'count' | 'groups' | Array<'single' | 'list'>;
2422
+ /**
2423
+ * The response envelope. Spec: §7.0.
2424
+ *
2425
+ * Query data values are positionally encoded using the client-sent `FieldInput`
2426
+ * selection; mutation data values are positionally encoded using the declared
2427
+ * `MutationSchema.returns` (§4.2) — mutations don't carry a FieldInput
2428
+ * since there's no field selection on a return row.
2429
+ */
2430
+ interface PackEnvelope {
2431
+ schemaVersion: string;
2432
+ data: Record<string, unknown>;
2433
+ shapes: Record<string, EnvelopeShape>;
2434
+ _meta?: Record<string, unknown>;
2435
+ _pagination?: Record<string, {
2436
+ nextCursor?: string;
2437
+ hasMore: boolean;
2438
+ }>;
2439
+ /**
2440
+ * Per-top-level-key completeness. ABSENT MEANS COMPLETE — callers that
2441
+ * predate this field stay correct without change.
2442
+ *
2443
+ * Set when a bridged key could not be fully resolved (§6.5 "reads never
2444
+ * fail" means such a read still returns 200 with orphan markers). The
2445
+ * per-row `__orphan` marker says a row is missing; this says the RESULT SET
2446
+ * is partial, which a consumer cannot otherwise distinguish from an
2447
+ * genuinely empty result.
2448
+ */
2449
+ _completeness?: Record<string, {
2450
+ complete: boolean;
2451
+ unreachable?: string[];
2452
+ }>;
2453
+ /**
2454
+ * Non-fatal read diagnostics, keyed by top-level frame key. ABSENT MEANS
2455
+ * NOTHING TO REPORT — callers that predate this field stay correct without
2456
+ * change, same contract as `_completeness`.
2457
+ *
2458
+ * Deliberately NOT `errors`: a consumer doing `if (env.errors) throw` would
2459
+ * start failing on reads that succeeded.
2460
+ *
2461
+ * A client that maps rows into declared interfaces MUST surface this (log,
2462
+ * attach to the result, or throw in development): an unknown projection key
2463
+ * arrives as `null`, satisfies the interface, and the read looks correct.
2464
+ */
2465
+ _diagnostics?: Record<string, {
2466
+ /**
2467
+ * Projection keys the type does not declare. The read still returned
2468
+ * every row; each unknown key materialises as `null` on all of them,
2469
+ * indistinguishable from a declared field that is null everywhere.
2470
+ */
2471
+ unknownProjectionFields?: string[];
2472
+ /**
2473
+ * The where-clause reached the substrate without being schema-validated,
2474
+ * because the type is registered but absent from its namespace's
2475
+ * published schema (e.g. runtime-created types). The result is still
2476
+ * correct — substrates refuse a predicate they cannot honour — but the
2477
+ * engine's operator and operand check did not run.
2478
+ */
2479
+ filterValidationSkipped?: string;
2480
+ /**
2481
+ * A walk-grammar read's clauses, each marked index-served or not
2482
+ * (typed edge walks, goal 7). Annotation only: nothing is refused on
2483
+ * it. Absent on ordinary reads, and when the store has no index plan.
2484
+ */
2485
+ plan?: PlanClause[];
2486
+ }>;
2487
+ errors?: Array<{
2488
+ message: string;
2489
+ path?: string[];
2490
+ code?: string;
2491
+ /**
2492
+ * Optional structured payload — populated when the underlying error class
2493
+ * carries one (e.g. `ResolverFrameError({ field: '...' })`).
2494
+ */
2495
+ details?: Record<string, unknown>;
2496
+ }>;
2497
+ }
2498
+ /**
2499
+ * Client-sent field selection tree. Defined here rather than in
2500
+ * `@mnmal-ai/substrate-pack` so subscription and streaming machinery (§5.4, §5.5)
2501
+ * can reference the same shape without adding substrate-pack to their dependency
2502
+ * set — substrate-pack already imports from types, so moving the type upward
2503
+ * avoids the would-be cycle.
2504
+ */
2505
+ type FieldInput = {
2506
+ [field: string]: true | FieldInput;
2507
+ };
2508
+ //#endregion
2509
+ export { PluginRouter as $, WriteOptions as $t, KernelPlugin as A, DeriveSpec as At, QualifiedFrame as B, ReadResult as Bt, FilterOperatorDef as C, Row as Cn, WriteEvent as Ct, KernelMigration as D, assertNoRepeatedIds as Dn, Dataset as Dt, KernelLifecycleContext as E, WriteRow as En, ConditionalUpdateResult as Et, NamespaceInstallContext as F, FrameAgg as Ft, RegistryEntryKind as G, TypeStorageReport as Gt, RegistryBase as H, StorageReport as Ht, NamespaceTypeDef as I, IndexPlan as It, ScheduledTaskContext as J, WalkAggOperand as Jt, RegistryEntryResolver as K, UpdateFieldRef as Kt, PluginLifecycleContext as L, NestedSelection as Lt, McpToolRegistration as M, FieldKind as Mt, NamespaceConfig as N, FieldPredicate as Nt, KernelMigrationContext as O, DatasetRegistry as Ot, NamespaceDefinition as P, FilterCapabilities as Pt, HttpMethod as Q, WhereClause as Qt, PluginRouteContext as R, OrderByClause as Rt, EngineInput as S, ROW_METADATA_FIELDS as Sn, TypedResolver as St, IndexDecl as T, RowMetadataField as Tn, BucketSpec as Tt, RegistryEntry as U, TxScopeHandles as Ut, Registry as V, RefOperand as Vt, RegistryEntryInput as W, TypeIndexState as Wt, VirtualTypeDef as X, WalkNode as Xt, VirtualMutationDef as Y, WalkAggPredicate as Yt, HttpHandler as Z, WalkPick as Zt, CoordinatorPlugin as _, ActorIdentity as _n, RequestContext as _t, FieldDefCrossRef as a, RuntimeErrors as an, SubstrateResponse as at, EncodeContext as b, Id as bn, ResolverReturn as bt, FieldDefStruct as c, TypeStats as cn, WsConnection as ct, OrderedBy as d, EdgePatch as dn, AutoCrudVerb as dt, WriteResult as en, RequestObservation as et, PackEnvelope as f, EdgeRef as fn, Identity as ft, naturalOrderOf as g, OnMissing as gn, PolicyProbeContext as gt, TypeSchema as h, OnDelete as hn, PolicyDecision as ht, FieldDef as i, RuntimeErrorEntry as in, SubstrateRequest as it, KernelRouteClaim as j, DropResult as jt, KernelNamespaceContribution as k, DeleteResult as kt, FieldInput as l, CanDeleteResult as ln, WsHandler as lt, Schema as m, EdgesStore as mn, Policy as mt, Dropped as n, ResolveObservation as nn, RuntimeOp as nt, FieldDefList as o, RuntimeMetrics as on, SubstrateServer as ot, PlanClause as p, EdgeWriteInput as pn, Obligation as pt, ScheduledTask as q, WalkAgg as qt, EnvelopeShape as r, ResolverStat as rn, StartOpts as rt, FieldDefScalar as s, SlowRequest as sn, TlsConfig as st, DEFAULT_ORDERED_BY as t, ProcessStat as tn, RequestObserver as tt, MutationSchema as u, Edge as un, AfterTxWork as ut, DropParticipant as v, CLIENT_SUPPLIED_SERVER_FIELD as vn, Resolver as vt, FrameEntry as w, RowMetadata as wn, AggFn as wt, EngineHandle as x, IdentityKind as xn, RowFilterObligation as xt, EmbedSourceContext as y, ClientSuppliedServerFieldError as yn, ResolverArgs as yt, PostWriteHook as z, ReadParams as zt };