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

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-BVqfFxh1.js +4060 -0
  3. package/dist/{runtime-metrics-DwpDJ8vz.d.ts → bridge-Cp4PaIYO.d.ts} +2 -87
  4. package/dist/cli-hSCBywhE.js +3126 -0
  5. package/dist/client.d.ts +130 -8
  6. package/dist/client.js +6 -5
  7. package/dist/{plugin-t2g2wX0b.d.ts → dataset-CejF-Fr_.d.ts} +1640 -1320
  8. package/dist/{agent-token-BIboWOU0.js → env-utils-CGNazUNt.js} +31 -2
  9. package/dist/{errors-D4vKuwz8.d.ts → errors-7mycvNGq.d.ts} +1 -1
  10. package/dist/{index-D7mSOFlM.d.ts → index-DgSfwx7M.d.ts} +1 -1
  11. package/dist/node-version-C5FSmwJX.js +26 -0
  12. package/dist/pack.d.ts +2 -2
  13. package/dist/pack.js +1 -1
  14. package/dist/pg-edges-store-Lxsl5ZnK.js +381 -0
  15. package/dist/{plugin-BuQQEzL-.js → plugin-CtwOINCM.js} +171 -18
  16. package/dist/proposals-bootstrap.js +6 -4
  17. package/dist/proposals.config.schema.json +5 -0
  18. package/dist/proposals.d.ts +15 -4
  19. package/dist/proposals.default-config.json +2 -1
  20. package/dist/proposals.js +2 -2
  21. package/dist/recall-bootstrap.js +13 -4
  22. package/dist/recall.config.schema.json +12 -0
  23. package/dist/recall.d.ts +54 -9
  24. package/dist/recall.js +2 -2
  25. package/dist/{repl-C52wyfMs.js → repl-DqfHelK7.js} +62 -29
  26. package/dist/{row-ByLFE2Wx.d.ts → row-BKKCXIG6.d.ts} +5 -3
  27. package/dist/run-B5dTbZ3g.js +1997 -0
  28. package/dist/{schema-g4FQLr0V.d.ts → schema-DoSc-4ig.d.ts} +44 -7
  29. package/dist/script-C6DSa_Na.js +134 -0
  30. package/dist/sdk.d.ts +90 -12
  31. package/dist/sdk.js +5 -110
  32. package/dist/{src-YXlv_RyL.js → src-5SJ5GEIM.js} +1555 -326
  33. package/dist/{src-DuopV8u7.js → src-7CUSMK66.js} +148 -19
  34. package/dist/{src-zcyUofMJ.js → src-Bi849yhn.js} +39 -15
  35. package/dist/{src-5hVbeiAu.js → src-CqT-QWZ_.js} +697 -57
  36. package/dist/src-CyvV7dCK.js +4333 -0
  37. package/dist/{row-DK7lPZVe.js → src-Da0t6si5.js} +11 -11
  38. package/dist/{src-C4DzaIQZ.js → src-hZb8wfvg.js} +12 -1
  39. package/dist/substrate-admin.js +17 -781
  40. package/dist/substrate.js +4 -5476
  41. package/dist/testing.d.ts +557 -8
  42. package/dist/testing.js +39 -3
  43. package/dist/{ws-C72iVS5T.d.ts → ws-ByZyHk7m.d.ts} +3 -3
  44. package/package.json +21 -20
  45. package/dist/dataset-DOXGRz50.js +0 -1535
  46. package/dist/env-utils-B7SGmoeJ.js +0 -21
  47. package/dist/src-CjEwiKXw.js +0 -2327
package/dist/testing.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { C as Id, E as Row, _ as EdgesStore, b as ActorIdentity, f as CanDeleteResult, g as EdgeWriteInput, h as EdgeRef, l as PackEnvelope, m as EdgePatch, p as Edge, u as Schema } from "./schema-g4FQLr0V.js";
2
- import { a as RuntimeErrors, n as ResolveObservation, r as ResolverStat, s as SlowRequest, t as ProcessStat } from "./runtime-metrics-DwpDJ8vz.js";
3
- import { $ as PolicyProbeContext, A as ScheduledTask, B as StartOpts, C as QualifiedFrame, D as RegistryEntryInput, E as RegistryEntry, F as HttpMethod, G as WsConnection, H as SubstrateResponse, I as PluginRouter, K as WsHandler, P as HttpHandler, Q as PolicyDecision, R as RequestObserver, St as WriteResult, T as RegistryBase, U as SubstrateServer, V as SubstrateRequest, Y as Identity, _t as ReadResult, a as EngineHandle, ct as Dataset, dt as DropResult, et as RequestContext, g as NamespaceConfig, gt as ReadParams, lt as DatasetRegistry, mt as FilterCapabilities, n as DropParticipant, o as EngineInput, p as KernelPlugin, st as ConditionalUpdateResult, t as CoordinatorPlugin, ut as DeleteResult, w as Registry$1, xt as WriteOptions, yt as UpdateFieldRef, z as RuntimeOp } from "./plugin-t2g2wX0b.js";
4
- import { a as WsResponseFrame, f as IdempotencyStore, l as SweepDeps, s as BridgeRegistry, u as SweepState } from "./ws-C72iVS5T.js";
1
+ import { C as ActorIdentity, E as Id, M as WriteRow, _ as EdgePatch, b as EdgesStore, d as PackEnvelope, f as Schema, g as Edge, h as CanDeleteResult, k as Row, r as FieldDef, u as OrderedBy, v as EdgeRef, y as EdgeWriteInput } from "./schema-DoSc-4ig.js";
2
+ import { $ as Registry$1, A as DropParticipant, C as ResolverStat, D as SlowRequest, Dt as PolicyDecision, G as NamespaceConfig, H as KernelPlugin, L as IndexDecl, N as EngineHandle, O as TypeStats, Ot as PolicyProbeContext, P as EngineInput, Q as QualifiedFrame, S as ResolveObservation, T as RuntimeErrors, _ as UpdateFieldRef, _t as SubstrateResponse, a as DropResult, at as ScheduledTask, b as WriteResult, bt as WsConnection, c as FilterCapabilities, d as ReadParams, dt as PluginRouter, et as RegistryBase, f as ReadResult, g as TypeStorageReport, gt as SubstrateRequest, ht as StartOpts, i as DeleteResult, k as CoordinatorPlugin, kt as RequestContext, l as IndexPlan, lt as HttpHandler, m as TxScopeHandles, mt as RuntimeOp, n as Dataset, nt as RegistryEntryInput, p as StorageReport, pt as RequestObserver, r as DatasetRegistry, t as ConditionalUpdateResult, tt as RegistryEntry, ut as HttpMethod, vt as SubstrateServer, wt as Identity, x as ProcessStat, xt as WsHandler, y as WriteOptions } from "./dataset-CejF-Fr_.js";
3
+ import { a as WsResponseFrame, f as IdempotencyStore, l as SweepDeps, s as BridgeRegistry, u as SweepState } from "./ws-ByZyHk7m.js";
5
4
  import { t as Embedder } from "./types-8c0gtnd2.js";
5
+ import { Sql, TransactionSql } from "postgres";
6
6
  //#region ../../packages/substrate-compose/src/auth.d.ts
7
7
  type JwtAlg = 'RS256' | 'ES256' | 'HS256' | 'EdDSA';
8
8
  interface AuthKey {
@@ -55,8 +55,8 @@ interface AuthConfig {
55
55
  maxTokenLifetimeSeconds?: number;
56
56
  /**
57
57
  * The origins this server answers as (`SUBSTRATE_PUBLIC_ORIGIN`). When set, a
58
- * token carrying `aud` must name one of them; a token with no `aud` is still
59
- * accepted. Absent → `aud` is not checked.
58
+ * token must carry `aud` naming one of them, or it is refused. Absent → `aud`
59
+ * is not checked.
60
60
  */
61
61
  audiences?: ReadonlyArray<string>;
62
62
  /**
@@ -75,6 +75,13 @@ export declare function signHs256(payload: Record<string, unknown>, secret: stri
75
75
  kid?: string;
76
76
  }): string;
77
77
  //#endregion
78
+ //#region ../../packages/substrate-compose/src/advisor/profiles.d.ts
79
+ /**
80
+ * Sizing profiles: how sensitive the index advisor is. A profile only tunes
81
+ * when a recommendation is written; nothing is ever built without approval.
82
+ */
83
+ type StoreProfileName = 'personal' | 'growth' | 'scale';
84
+ //#endregion
78
85
  //#region ../../packages/substrate-compose/src/_system/types.d.ts
79
86
  /**
80
87
  * Where the operator's agent DIRECTORY lives, so `POST /substrate/v1/_system/keys` can
@@ -187,6 +194,38 @@ interface RateLimiter {
187
194
  dispose(): void;
188
195
  }
189
196
  //#endregion
197
+ //#region ../../packages/substrate-core/src/advisor/shape-recorder.d.ts
198
+ interface ReadShape {
199
+ namespace: string;
200
+ type: string;
201
+ filters: Array<{
202
+ field: string;
203
+ op: string;
204
+ }>;
205
+ orderBy: Array<[string, 'asc' | 'desc']>;
206
+ /** An OR or a NOT somewhere in the filter: no composite index serves it as a conjunction. */
207
+ disjunctive?: true;
208
+ /**
209
+ * The boolean or null operand of each top-level (or AND-ed) `eq`, the only
210
+ * operand values kept: they decide whether a partial index's predicate
211
+ * holds, and have three possible values.
212
+ */
213
+ literals?: Record<string, boolean | null>;
214
+ }
215
+ declare class ShapeRecorder {
216
+ #private;
217
+ /** When this recorder was created or last drained: the start of what `drain` returns. */
218
+ since: number;
219
+ constructor(now?: () => number);
220
+ observe(shape: ReadShape, durationMs: number): void;
221
+ /** Every shape seen since the last drain, with its count and p95; then forgets them. */
222
+ drain(): Array<{
223
+ shape: ReadShape;
224
+ count: number;
225
+ p95Ms: number;
226
+ }>;
227
+ }
228
+ //#endregion
190
229
  //#region ../../packages/substrate-core/src/registry.d.ts
191
230
  type TypeKey = string | {
192
231
  readonly name: string;
@@ -232,6 +271,12 @@ interface EngineOptions {
232
271
  * does no timing at all.
233
272
  */
234
273
  onResolve?: (o: ResolveObservation) => void;
274
+ /**
275
+ * Observes each successful local read of a type: its shape (filtered fields
276
+ * and operators, the effective order including the natural one) and how long
277
+ * the resolver took. For the index advisor; absent means no recording.
278
+ */
279
+ shapeRecorder?: Pick<ShapeRecorder, 'observe'>;
235
280
  /**
236
281
  * Idempotency dedup (§5.13). Absent → no dedup; `FrameEntry.idempotencyKey`
237
282
  * is ignored on mutate (queries ignore it unconditionally). Present → the
@@ -728,11 +773,26 @@ interface MemoryDatasetOptions {
728
773
  * tests without spinning up pg.
729
774
  */
730
775
  txScopeId?: string;
776
+ /**
777
+ * The namespace's field declarations. Lets `date` fields filter and sort as
778
+ * instants, as the pg dataset's lookup does; without it they compare as text.
779
+ */
780
+ fieldDefLookup?: (type: string, field: string) => FieldDef | undefined;
781
+ /**
782
+ * The namespace's type definitions; a type declared `ordering: 'visible'`
783
+ * gets `_metadata.seq` on insert, as the pg dataset's lookup does.
784
+ */
785
+ typeDefLookup?: (type: string, namespace: string) => {
786
+ ordering?: 'visible';
787
+ } | undefined;
731
788
  }
732
789
  export declare class MemoryDataset implements Dataset {
733
790
  #private;
734
791
  readonly namespace: string;
735
792
  readonly txScopeId: string;
793
+ private readonly typeDefLookup;
794
+ /** Last seq assigned per visible-order type; never decreases, as pg's `visible_seqs`. */
795
+ private seqs;
736
796
  readonly capabilities: FilterCapabilities;
737
797
  /** See `Dataset.withAdvisoryLock`. Excludes holders on this instance. */
738
798
  withAdvisoryLock<T>(key: string, fn: () => Promise<T>): Promise<T>;
@@ -741,7 +801,7 @@ export declare class MemoryDataset implements Dataset {
741
801
  constructor(options: MemoryDatasetOptions);
742
802
  private bucket;
743
803
  read(type: string, params: ReadParams): Promise<ReadResult>;
744
- write(type: string, rows: Row[], options?: WriteOptions): Promise<WriteResult>;
804
+ write(type: string, rows: WriteRow[], options?: WriteOptions): Promise<WriteResult>;
745
805
  tombstone(type: string, ids: Id[]): Promise<DeleteResult>;
746
806
  purge(type: string, ids: Id[]): Promise<DeleteResult>;
747
807
  dropType(type: string): Promise<DropResult>;
@@ -979,6 +1039,20 @@ interface ComposeOpts {
979
1039
  * second reading of the environment. Omitted by programmatic embedders.
980
1040
  */
981
1041
  configReport?: ConfigReport;
1042
+ /** Reported beside `configReport`; ignored without it. */
1043
+ storageReport?: () => Promise<StorageReport>;
1044
+ /**
1045
+ * The index advisor. Every `intervalMs` (`SUBSTRATE_ADVISOR_INTERVAL_MS`)
1046
+ * it turns the reads the engine recorded and the store's statistics into
1047
+ * `_system/IndexRecommendation` rows. Omitted or `intervalMs: 0`: off, and
1048
+ * no read is recorded. `storeProfile` seeds `_system/StoreProfile` when it
1049
+ * has no row yet.
1050
+ */
1051
+ advisor?: {
1052
+ intervalMs: number;
1053
+ storeProfile?: StoreProfileName;
1054
+ writeHeavy?: boolean;
1055
+ };
982
1056
  }
983
1057
  interface ComposeResult {
984
1058
  engine: Engine;
@@ -1001,6 +1075,10 @@ interface ComposeResult {
1001
1075
  * loop in `main.ts`.
1002
1076
  */
1003
1077
  runSweepNow: () => Promise<SweepState>;
1078
+ /** Run one index-advisor pass now, for tests; null while the advisor is off. */
1079
+ runAdvisorNow: (() => Promise<{
1080
+ upserted: number;
1081
+ }>) | null;
1004
1082
  /**
1005
1083
  * Non-null when any coordinator plugin contributed a `schedule`. The
1006
1084
  * composition root constructs the scheduler, registers every plugin's
@@ -1017,6 +1095,460 @@ interface ComposeResult {
1017
1095
  }
1018
1096
  export declare function compose(opts: ComposeOpts): Promise<ComposeResult>;
1019
1097
  //#endregion
1098
+ //#region ../../packages/substrate-dataset-pg/src/disk-guard.d.ts
1099
+ /**
1100
+ * Refuses a background index build that would leave less than a margin of
1101
+ * free space on the database volume. Postgres cannot report filesystem space,
1102
+ * so the volume is a local path the operator names; with none, every build is
1103
+ * allowed and `path` is undefined so the storage report can say checks are off.
1104
+ */
1105
+ interface DiskGuard {
1106
+ readonly path: string | undefined;
1107
+ check(estimateBytes: number): Promise<{
1108
+ ok: true;
1109
+ } | {
1110
+ ok: false;
1111
+ freeBytes: number;
1112
+ needBytes: number;
1113
+ }>;
1114
+ }
1115
+ //#endregion
1116
+ //#region ../../packages/substrate-dataset-pg/src/layout.d.ts
1117
+ type StorageLayout = 'legacy' | 'partitioned';
1118
+ //#endregion
1119
+ //#region ../../packages/substrate-dataset-pg/src/pg-sql/primitives.d.ts
1120
+ /**
1121
+ * A body key mirrored into a dedicated column on `rows`, so filters and
1122
+ * sorts on it hit an index instead of extracting from JSONB.
1123
+ *
1124
+ * `column` is a BARE column name, not a qualified `r.<col>` fragment: taking a
1125
+ * fragment from a caller would put caller text into `sql.unsafe`.
1126
+ */
1127
+ interface PromotedBodyColumn {
1128
+ column: string;
1129
+ kind: 'boolean' | 'text';
1130
+ }
1131
+ type PromotedBodyColumnMap = Readonly<Record<string, PromotedBodyColumn>>;
1132
+ //#endregion
1133
+ //#region ../../packages/substrate-dataset-pg/src/migrate.d.ts
1134
+ /**
1135
+ * Public migration shape accepted by the runner. Either supply both
1136
+ * `txStatements` + (optional) `concurrentStatements`, OR a legacy
1137
+ * `statements` bucket — the runner treats `statements` as `txStatements`
1138
+ * with no concurrent statements. Mixing `statements` and `txStatements`
1139
+ * is a configuration error.
1140
+ */
1141
+ type MigrationInput = {
1142
+ readonly version: string;
1143
+ readonly txStatements: ReadonlyArray<string>;
1144
+ readonly concurrentStatements?: ReadonlyArray<string>;
1145
+ readonly statements?: never;
1146
+ } | {
1147
+ readonly version: string;
1148
+ readonly statements: ReadonlyArray<string>;
1149
+ readonly txStatements?: never;
1150
+ readonly concurrentStatements?: never;
1151
+ };
1152
+ interface RunMigrationsOptions {
1153
+ /**
1154
+ * When true, drop the `migrations` tracking table before running.
1155
+ * Forces every migration to re-apply on this call. Intended for tests
1156
+ * that DROP schema objects between runs and need the framework to
1157
+ * recreate them; production paths should never set this.
1158
+ */
1159
+ force?: boolean;
1160
+ /**
1161
+ * Override the `SUBSTRATE_ENFORCE_UUID_CHECK=true` env-var gate for the gated
1162
+ * UUID-format CHECK constraint migration (0002). Defaults to whatever the
1163
+ * env says. Tests that need to exercise the constrained path explicitly
1164
+ * pass `true`; tests that need to opt out of an env-set gate pass `false`.
1165
+ * Production bootstraps shouldn't pass this — let the env decide.
1166
+ */
1167
+ enforceUuidCheck?: boolean;
1168
+ /**
1169
+ * The layout for a database with no `rows` yet. Ignored once `rows` exists,
1170
+ * since a database keeps its layout. Defaults to `SUBSTRATE_STORAGE_LAYOUT`,
1171
+ * then `partitioned`.
1172
+ */
1173
+ layout?: StorageLayout;
1174
+ }
1175
+ export declare function runMigrations(sql: Sql, opts?: RunMigrationsOptions): Promise<void>;
1176
+ /**
1177
+ * One `field.list({ indexed: true })` or `field.json({ indexed: true })` field
1178
+ * that should be backed by a GIN over `(body -> field)`.
1179
+ */
1180
+ interface IndexedListField {
1181
+ readonly namespace: string;
1182
+ readonly type: string;
1183
+ readonly field: string;
1184
+ }
1185
+ /**
1186
+ * Derives the tracked, idempotent CONCURRENTLY GIN migration for one indexed
1187
+ * field. The version key uses the raw ns/type/field.
1188
+ */
1189
+ export declare function deriveListIndexMigration(f: IndexedListField): MigrationInput;
1190
+ /**
1191
+ * Applies migrations derived from field definitions through the same tracked,
1192
+ * advisory-locked machinery as the static list. Called after plugins load,
1193
+ * since `runMigrations` has no field-def knowledge. Idempotent: applied
1194
+ * versions are skipped.
1195
+ *
1196
+ * On the partitioned layout the list/json GIN migrations are skipped: those
1197
+ * indexes live on each type's partition, built by `reconcileTypeIndexes`, and
1198
+ * Postgres cannot build one CONCURRENTLY on the partitioned parent.
1199
+ */
1200
+ export declare function runDerivedMigrations(sql: Sql, all: ReadonlyArray<MigrationInput>): Promise<void>;
1201
+ //#endregion
1202
+ //#region ../../packages/substrate-dataset-pg/src/tx-scope.d.ts
1203
+ type PgHandle = Sql | TransactionSql;
1204
+ //#endregion
1205
+ //#region ../../packages/substrate-dataset-pg/src/pg-edges-store.d.ts
1206
+ interface PgEdgesStoreOptions {
1207
+ sql: Sql;
1208
+ /** Injected clock for tests. Defaults to `() => new Date()`. */
1209
+ now?: () => Date;
1210
+ /**
1211
+ * Per-statement batch size for chunked admin deletes. Defaults to
1212
+ * {@link DEFAULT_DROP_BATCH_SIZE}.
1213
+ */
1214
+ dropBatchSize?: number;
1215
+ }
1216
+ interface InternalPgEdgesStoreOptions {
1217
+ sql: PgHandle;
1218
+ now: () => Date;
1219
+ inTx: boolean;
1220
+ /**
1221
+ * Tx-bound instances inherit their parent's txScopeId. The public
1222
+ * constructor derives it from the Sql reference.
1223
+ */
1224
+ txScopeId?: string;
1225
+ dropBatchSize?: number;
1226
+ }
1227
+ export declare class PgEdgesStore implements EdgesStore {
1228
+ #private;
1229
+ readonly txScopeId: string;
1230
+ constructor(options: PgEdgesStoreOptions | InternalPgEdgesStoreOptions);
1231
+ write(input: EdgeWriteInput): Promise<Edge>;
1232
+ tombstone(id: string, at?: Date): Promise<void>;
1233
+ /**
1234
+ * Hard-deletes tombstoned edges whose `deleted_at` is older than
1235
+ * `olderThan`. Called by the sweep loop. Returns the number deleted.
1236
+ */
1237
+ purgeTombstonedEdges(opts: {
1238
+ olderThan: Date;
1239
+ }): Promise<{
1240
+ deleted: number;
1241
+ }>;
1242
+ update(patch: EdgePatch): Promise<void>;
1243
+ readNextBatch(cursorId: string | null, size: number): Promise<{
1244
+ edges: Edge[];
1245
+ lastId: string | null;
1246
+ }>;
1247
+ findEdgesTo(ref: EdgeRef): Promise<Edge[]>;
1248
+ findEdgesToMany(refs: readonly EdgeRef[]): Promise<Edge[]>;
1249
+ findEdgesFrom(ref: EdgeRef): Promise<Edge[]>;
1250
+ findEdgesTouchingNamespace(namespace: string, limit: number): Promise<Edge[]>;
1251
+ findEdgesTouchingType(namespace: string, type: string, limit: number): Promise<Edge[]>;
1252
+ namespacesLinkedTo(namespace: string, type?: string): Promise<string[]>;
1253
+ /**
1254
+ * Chunked physical delete of every edge touching `namespace`, at most
1255
+ * `dropBatchSize` rows per statement, so a large delete does not hold every
1256
+ * row lock at once. Returns the number deleted.
1257
+ *
1258
+ * Outside a tx each batch auto-commits. Inside a caller's tx the batches
1259
+ * stay in it, so a throw still rolls back the whole drop.
1260
+ */
1261
+ dropEdgesByNamespace(namespace: string, within?: readonly string[]): Promise<number>;
1262
+ /**
1263
+ * Chunked physical delete of every edge touching `(namespace, type)`.
1264
+ * See {@link dropEdgesByNamespace} for the lock-hold + tx semantics.
1265
+ */
1266
+ dropEdgesByType(namespace: string, type: string, within?: readonly string[]): Promise<number>;
1267
+ canDelete(ref: EdgeRef): Promise<CanDeleteResult>;
1268
+ findBlocked(): Promise<Edge[]>;
1269
+ /**
1270
+ * `sql.begin`-backed atomicity. Same flattening pattern as PgDataset —
1271
+ * nested re-entry skips the inner begin and runs on the existing tx handle
1272
+ * so an inner throw propagates to the outer `sql.begin` for rollback.
1273
+ */
1274
+ withTransaction<T>(fn: (tx: EdgesStore) => Promise<T>): Promise<T>;
1275
+ /**
1276
+ * Engine-only. Returns a tx-bound clone on the supplied `TransactionSql`,
1277
+ * for `PgDataset.withScopedTransaction`. The caller has verified
1278
+ * the txScopeIds match.
1279
+ */
1280
+ _enlistInTx(txSql: PgHandle): PgEdgesStore;
1281
+ }
1282
+ //#endregion
1283
+ //#region ../../packages/substrate-dataset-pg/src/pg-dataset.d.ts
1284
+ /**
1285
+ * Returns the declared FieldDef for a type's field, or undefined when unknown
1286
+ * (body-key sorts then fall back to text compare). Lets the SQL builders cast
1287
+ * body values by declared kind, so `count` sorts 2 before 10 — see
1288
+ * `resolveBodyOrderByCast`.
1289
+ */
1290
+ type FieldDefLookup = (type: string, field: string) => FieldDef | undefined;
1291
+ /**
1292
+ * A type's declared fields and indexes, or undefined when the type is unknown.
1293
+ * Called with the asking dataset's namespace, so a `forNamespace` clone that
1294
+ * inherits the lookup resolves its own types.
1295
+ */
1296
+ type TypeDefLookup = (type: string, namespace: string) => {
1297
+ fields: Record<string, FieldDef>;
1298
+ indexes?: IndexDecl[];
1299
+ orderedBy?: OrderedBy;
1300
+ ordering?: 'visible';
1301
+ expectedSize?: 'small' | 'large';
1302
+ } | undefined;
1303
+ interface PgDatasetOptions {
1304
+ namespace: string;
1305
+ sql: Sql;
1306
+ /**
1307
+ * Optional sibling `PgEdgesStore` to enlist into this dataset's substrate
1308
+ * transactions. When supplied, MUST share the same `Sql` pool (verified
1309
+ * via `txScopeId === txScopeIdFor(sql)`); the dataset throws on
1310
+ * construction otherwise.
1311
+ *
1312
+ * When supplied, `withScopedTransaction` yields a tx-bound edges store
1313
+ * on the same `TransactionSql` as the tx-bound datasets. When omitted, it
1314
+ * yields `edges: null` and the engine runs edge ops in a separate tx.
1315
+ */
1316
+ edgesStore?: PgEdgesStore;
1317
+ /**
1318
+ * Optional field-def lookup so orderBy + cursor compares on body fields can
1319
+ * cast to the declared scalar pg type (numeric / timestamptz / boolean)
1320
+ * instead of falling back to lexicographic text compare. When omitted, every
1321
+ * body-key orderBy compares as text.
1322
+ *
1323
+ * A cast sort cannot use an index on the uncast `(body ->> 'field')`
1324
+ * expression; it needs a matching cast expression index.
1325
+ */
1326
+ fieldDefLookup?: FieldDefLookup;
1327
+ /**
1328
+ * Body keys mirrored into dedicated columns on `rows`, so filters and
1329
+ * sorts on them use an index instead of extracting from JSONB.
1330
+ *
1331
+ * None by default. A namespace declares its own (`NamespaceDefinition`'s
1332
+ * `promotedColumns`), and the plugin that declares them adds the columns in
1333
+ * its kernel migrations: the engine creates none. Three things that bite:
1334
+ *
1335
+ * - Filters and sorts route to the column and ONLY the column, so a column
1336
+ * added over existing rows must be backfilled from the body first, or
1337
+ * those rows miss every filter. Reads return the body.
1338
+ * - `column` is a BARE name (`'severity'`, not `'r.severity'`) and is
1339
+ * validated as an identifier at construction, because it is interpolated
1340
+ * into SQL as an identifier.
1341
+ * - Text columns compare `COLLATE "C"` in every operator, eq included,
1342
+ * so an index meant to serve them must key `(column COLLATE "C")`, as
1343
+ * declared `indexes` do.
1344
+ */
1345
+ promotedColumns?: PromotedBodyColumnMap;
1346
+ /**
1347
+ * The namespace's type definitions, which `ensureTypeStorage` builds the
1348
+ * type's indexes from. Without it only the engine's own indexes are built.
1349
+ */
1350
+ typeDefLookup?: TypeDefLookup;
1351
+ /**
1352
+ * Checked before each non-unique index build `ensureTypeStorage` runs; a
1353
+ * refused build stays `pending` (`insufficient_disk`) until a later pass.
1354
+ * Without one nothing is checked. With one that has no path, nothing is
1355
+ * checked either, and `storageReport` says so.
1356
+ */
1357
+ diskGuard?: DiskGuard;
1358
+ }
1359
+ interface InternalPgSubstrateDatasetOptions {
1360
+ namespace: string;
1361
+ sql: PgHandle;
1362
+ inTx: boolean;
1363
+ /**
1364
+ * Tx-bound instances reuse the root's txScopeId so one substrate never
1365
+ * advertises two ids. The public constructor derives it from the Sql.
1366
+ */
1367
+ txScopeId?: string;
1368
+ edgesStore?: PgEdgesStore;
1369
+ fieldDefLookup?: FieldDefLookup;
1370
+ /** See `PgDatasetOptions.promotedColumns`. */
1371
+ promotedColumns?: PromotedBodyColumnMap;
1372
+ /** Shared with the instance this one was derived from. */
1373
+ layoutCache?: LayoutCache;
1374
+ typeDefLookup?: TypeDefLookup;
1375
+ diskGuard?: DiskGuard;
1376
+ }
1377
+ /** The detected layout, shared by an instance and every clone of it. */
1378
+ interface LayoutCache {
1379
+ layout?: StorageLayout;
1380
+ }
1381
+ export declare class PgDataset implements Dataset {
1382
+ #private;
1383
+ readonly namespace: string;
1384
+ readonly capabilities: FilterCapabilities;
1385
+ readonly txScopeId: string;
1386
+ constructor(options: PgDatasetOptions | InternalPgSubstrateDatasetOptions);
1387
+ read(type: string, params: ReadParams): Promise<ReadResult>;
1388
+ /**
1389
+ * Upsert `rows`. A row without an `id` gets one from the database's
1390
+ * `uuidv7()`, returned in `ids` in input order. Limits of a v7 id: it is
1391
+ * never a happened-before signal; ids sort in creation order only within one
1392
+ * connection (across connections within a millisecond, and for
1393
+ * client-supplied ids, order is arbitrary); it reveals its creation time;
1394
+ * and deciding who won (last-writer-wins, `conditionalUpdate`) uses the
1395
+ * database's transaction time or a per-row version, never the id.
1396
+ *
1397
+ * A supplied id repeated in one batch: under `insertOnly` the first
1398
+ * occurrence lands and the rest are skipped; otherwise the batch is refused.
1399
+ *
1400
+ * On an `ordering: 'visible'` type each inserted row gets the next `seq`,
1401
+ * consecutive in input order, under a per-type transaction lock held until
1402
+ * commit, so seq order is commit order. A row that already exists keeps its
1403
+ * seq, and one `insertOnly` skips gets none.
1404
+ */
1405
+ write(type: string, rows: WriteRow[], options?: WriteOptions): Promise<WriteResult>;
1406
+ tombstone(type: string, ids: Id[]): Promise<DeleteResult>;
1407
+ purge(type: string, ids: Id[]): Promise<DeleteResult>;
1408
+ /**
1409
+ * Hard-deletes tombstoned rows whose `deleted_at` is older than `olderThan`,
1410
+ * across ALL namespaces (the table is substrate-shared). Called by the sweep
1411
+ * loop. Returns the number of rows deleted.
1412
+ *
1413
+ * Short-id mappings are left in place, as in `purge()`: a row id can
1414
+ * reappear after a tombstone + rewrite.
1415
+ */
1416
+ purgeTombstonedRows(opts: {
1417
+ olderThan: Date;
1418
+ }): Promise<{
1419
+ deleted: number;
1420
+ }>;
1421
+ /**
1422
+ * Hard-delete every row for `type` in this namespace, including tombstoned
1423
+ * rows (`deleted_at IS NOT NULL`). Short-id mappings for that type are
1424
+ * removed in the same logical operation. Both deletes run through `this.#sql`
1425
+ * so they participate in any surrounding `withTransaction` boundary.
1426
+ */
1427
+ dropType(type: string): Promise<DropResult>;
1428
+ /**
1429
+ * Hard-delete every row across all types in this namespace, including
1430
+ * tombstoned rows. Short-id mappings for all types are removed too.
1431
+ */
1432
+ dropNamespace(): Promise<DropResult>;
1433
+ /**
1434
+ * Compare-and-update (spec §5.12): merges `updates` into the live row only
1435
+ * if every `match` field equals its expected value. Returns `matched` and
1436
+ * the updated row.
1437
+ *
1438
+ * One `UPDATE ... WHERE <pk> AND <match>`, so the row lock makes exactly one
1439
+ * of N concurrent callers match. `_deletedAt` and `id` are dropped from
1440
+ * `updates`.
1441
+ */
1442
+ conditionalUpdate(type: string, id: Id, match: Record<string, unknown>, updates: Record<string, unknown>, actor?: ActorIdentity, now?: Date): Promise<ConditionalUpdateResult>;
1443
+ updateField(ref: UpdateFieldRef, fieldName: string, value: unknown): Promise<void>;
1444
+ /**
1445
+ * `sql.begin`-backed atomicity. Spec §5.3 / G1.
1446
+ *
1447
+ * Nested calls flatten: postgres.js's `begin` inside a `begin` would create
1448
+ * a savepoint (inner rolls back independently of outer), which is stricter
1449
+ * than the G1 contract promises. To match the memory impl — where an inner
1450
+ * throw tears down outer writes too — we detect the re-entry via `#inTx`
1451
+ * and skip the nested begin entirely. The inner throw then propagates to
1452
+ * the outer `sql.begin`, which rolls back the whole thing.
1453
+ */
1454
+ withTransaction<T>(fn: (tx: Dataset) => Promise<T>): Promise<T>;
1455
+ /**
1456
+ * Runs `fn` in one pg transaction with a tx-bound dataset per requested
1457
+ * namespace (and the edges store, when wired), all on one `TransactionSql`.
1458
+ * A throw rolls back every namespace. Spec §5.3.
1459
+ *
1460
+ * Nested calls flatten onto the current transaction rather than opening a
1461
+ * savepoint.
1462
+ */
1463
+ withScopedTransaction<T>(namespaces: ReadonlyArray<string>, fn: (handles: TxScopeHandles) => Promise<T>): Promise<T>;
1464
+ /**
1465
+ * See `Dataset.withAdvisoryLock`. Excludes every session on the
1466
+ * same schema, across processes. On a tx-bound instance the lock is
1467
+ * `pg_advisory_xact_lock` on the transaction's own connection, held until
1468
+ * it commits or rolls back: a session lock would need a second connection
1469
+ * beside the open transaction.
1470
+ */
1471
+ withAdvisoryLock<T>(key: string, fn: () => Promise<T>): Promise<T>;
1472
+ allocateShortId(type: string): Promise<number>;
1473
+ bindShortId(type: string, shortId: number, rowId: string): Promise<void>;
1474
+ resolveShortId(type: string, shortId: number): Promise<string | null>;
1475
+ getShortIdForRow(type: string, rowId: string): Promise<number | null>;
1476
+ forNamespace(namespace: string): Dataset;
1477
+ /**
1478
+ * See `Dataset.ensureTypeStorage`. On the partitioned layout, creates this
1479
+ * namespace's partition and the type's, then reconciles the type's indexes
1480
+ * on it; on legacy, reconciles only its declared `indexes`. Index builds
1481
+ * run CONCURRENTLY, outside any transaction, so a tx-bound instance refuses.
1482
+ * Logs each index it created, adopted or swapped, and records each one
1483
+ * left pending, building or failed for `describeStorage` and `storageReport`.
1484
+ */
1485
+ ensureTypeStorage(type: string, opts?: {
1486
+ indexes?: 'all' | 'unique' | 'nonUnique';
1487
+ signal?: AbortSignal;
1488
+ }): Promise<void>;
1489
+ /**
1490
+ * The type's non-unique indexes the next build pass would build or swap,
1491
+ * each with the disk guard's estimate. Empty when the store or the type's
1492
+ * storage does not exist yet. Changes nothing.
1493
+ */
1494
+ pendingIndexBuilds(type: string): Promise<Array<{
1495
+ name: string;
1496
+ estimateBytes: number;
1497
+ }>>;
1498
+ /** See `Dataset.describeStorage`. Reads the catalog only, in a fixed number of queries. */
1499
+ describeStorage(type: string): Promise<TypeStorageReport>;
1500
+ /**
1501
+ * See `Dataset.storageReport`. Spans every namespace of the database. Reads
1502
+ * the partition tree and every index on it in one query each, so its cost
1503
+ * does not grow with the number of partitions.
1504
+ */
1505
+ storageReport(opts: {
1506
+ partitionBudget: number;
1507
+ }): Promise<StorageReport>;
1508
+ /** See `Dataset.indexPlan`. */
1509
+ indexPlan(type: string, namespace: string): Promise<IndexPlan | undefined>;
1510
+ /**
1511
+ * See `Dataset.dropEngineIndex`. Checks and drops under the table's index
1512
+ * build lock, so a reconcile cannot rebuild or swap the index in between.
1513
+ * Ownership is the reconciler's marker and generated name on an index of
1514
+ * this type (`typeIndexesOf`); the wanted set is the reconciler's own
1515
+ * (`desiredIndexKeys`), so only a leftover or a retracted declaration goes.
1516
+ * Refuses with `IndexDropRefusedError`.
1517
+ *
1518
+ * The drop waits for every transaction holding a lock on the table, and pg
1519
+ * cannot see a cycle through a caller that waits on this call, so each wait
1520
+ * is bounded by `lockTimeoutMs` (default `DROP_LOCK_TIMEOUT_MS`).
1521
+ */
1522
+ dropEngineIndex(type: string, name: string, opts?: {
1523
+ beforeDrop?: () => Promise<void>;
1524
+ lockTimeoutMs?: number;
1525
+ }): Promise<void>;
1526
+ /**
1527
+ * See `Dataset.dropNamespaceIndexes`. On legacy, the engine-owned partial
1528
+ * indexes on `rows` scoped to this namespace; on partitioned, the
1529
+ * engine-owned indexes on its type partitions, which a namespace drop
1530
+ * empties but keeps. Unique ones only when `opts.unique`. Each table's under
1531
+ * its index-build lock, CONCURRENTLY; the wait for that lock and each lock
1532
+ * wait of a drop are bounded by `lockTimeoutMs` (default
1533
+ * `DROP_LOCK_TIMEOUT_MS`), past which it throws `index_drop_timeout`.
1534
+ */
1535
+ dropNamespaceIndexes(opts?: {
1536
+ unique?: boolean;
1537
+ lockTimeoutMs?: number;
1538
+ }): Promise<string[]>;
1539
+ /** See `Dataset.indexDeclarationOf`. */
1540
+ indexDeclarationOf(type: string, name: string): Promise<IndexDecl | undefined>;
1541
+ /**
1542
+ * See `Dataset.typeStats`. Spans every namespace of the database, in a fixed
1543
+ * number of queries. Partitioned: each type partition's own statistics;
1544
+ * `liveRows` is `reltuples` (or `n_live_tup` before the first ANALYZE) and
1545
+ * counts tombstoned rows too. Legacy: see the shortcut below.
1546
+ */
1547
+ typeStats(): Promise<TypeStats[]>;
1548
+ private materialize;
1549
+ private extractOriginalId;
1550
+ }
1551
+ //#endregion
1020
1552
  //#region ../../packages/substrate-dataset-pg/src/testing.d.ts
1021
1553
  /**
1022
1554
  * Safety rails for pg integration tests. Shared by every package whose suites
@@ -1139,6 +1671,23 @@ export declare function publicLeftoverWarning(names: readonly string[], packageN
1139
1671
  * Call from `globalSetup`, so forked workers inherit the rewritten URL.
1140
1672
  */
1141
1673
  export declare function preparePgTestSchema(url: string | undefined, opts: SchemaIsolationOptions): Promise<string | undefined>;
1674
+ /**
1675
+ * The storage layout this run's pg suites build, from `SUBSTRATE_TEST_LAYOUT`
1676
+ * (default `legacy`). Copies it into `SUBSTRATE_STORAGE_LAYOUT`, which
1677
+ * `runMigrations` reads on a fresh database, and returns the package name to
1678
+ * isolate under, so each layout gets a schema of its own.
1679
+ *
1680
+ * Call from `globalSetup`, before `preparePgTestSchema`.
1681
+ */
1682
+ export declare function selectPgTestLayout(packageName: string): {
1683
+ layout: 'legacy' | 'partitioned';
1684
+ packageName: string;
1685
+ };
1686
+ /**
1687
+ * A `createSchema` for `preparePgTestSchema` that drops and recreates the
1688
+ * schema, so every run builds its layout from an empty database.
1689
+ */
1690
+ export declare function recreatePgTestSchema(url: string, schema: string): Promise<void>;
1142
1691
  //#endregion
1143
1692
  //#region ../../packages/substrate-embedder/src/fake.d.ts
1144
1693
  interface FakeEmbedderOptions {
@@ -1237,4 +1786,4 @@ export declare class MemorySubstrateServer implements SubstrateServer {
1237
1786
  */
1238
1787
  export declare function matchRoute(registered: string, actual: string): Record<string, string> | null;
1239
1788
  //#endregion
1240
- export type { AuthConfig, ComposeResult, MemoryWsClient };
1789
+ export type { AuthConfig, ComposeResult, FieldDefLookup, IndexedListField, MemoryWsClient, PgDatasetOptions, PgEdgesStoreOptions };