@mnmal-ai/substrate 1.0.0-alpha.9 → 1.0.0-beta.2

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 (52) hide show
  1. package/README.md +32 -14
  2. package/dist/boot-store-DpnnxRpR.js +4086 -0
  3. package/dist/{runtime-metrics-DwpDJ8vz.d.ts → bridge-DnToJedN.d.ts} +2 -87
  4. package/dist/cli-COS16L1Z.js +3449 -0
  5. package/dist/client.browser.js +2507 -0
  6. package/dist/client.d.ts +222 -171
  7. package/dist/client.js +6 -7
  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-CuCMwPpb.d.ts} +46 -4
  12. package/dist/{fake-DUVtKVSE.js → fake-DdXtb-m8.js} +0 -1
  13. package/dist/{index-D7mSOFlM.d.ts → index-BI4KqFiR.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/{plugin-DpncES1p.js → plugin-DWagHKxm.js} +78 -8
  19. package/dist/proposals-bootstrap.js +6 -5
  20. package/dist/proposals.config.schema.json +5 -0
  21. package/dist/proposals.d.ts +17 -5
  22. package/dist/proposals.default-config.json +2 -1
  23. package/dist/proposals.js +2 -3
  24. package/dist/recall-bootstrap.js +3 -4
  25. package/dist/recall.d.ts +11 -12
  26. package/dist/recall.js +2 -3
  27. package/dist/{repl-DeKzfvNV.js → repl-C8ZgDrQ7.js} +52 -26
  28. package/dist/{row-SRhura12.d.ts → row-D0If4qif.d.ts} +91 -13
  29. package/dist/run-D-4sXpTu.js +2443 -0
  30. package/dist/{plugin-t2g2wX0b.d.ts → schema-aIkY8sR3.d.ts} +1293 -63
  31. package/dist/{script-C6DSa_Na.js → script-ByQ9pzhC.js} +54 -26
  32. package/dist/script-runner.js +78 -0
  33. package/dist/sdk.d.ts +236 -38
  34. package/dist/sdk.js +5 -110
  35. package/dist/semver-D_zdq0hi.js +17 -0
  36. package/dist/{src-BLgbLq2k.js → src-B_kHkbsr.js} +780 -330
  37. package/dist/{src-DjIuRjzF.js → src-C0ylwQFC.js} +10243 -4238
  38. package/dist/{src-C12EA0w9.js → src-D1PgjgUg.js} +179 -96
  39. package/dist/{src-DCy7Iy2l.js → src-Db3XI_pn.js} +900 -77
  40. package/dist/{row-DZ0Fnpc6.js → src-DmiUWOLp.js} +2 -1
  41. package/dist/src-DxD3iktO.js +8307 -0
  42. package/dist/{src-DiNTSaFk.js → src-FXO5JPyk.js} +686 -856
  43. package/dist/substrate-admin.js +17 -782
  44. package/dist/substrate.js +4 -5302
  45. package/dist/testing.d.ts +473 -96
  46. package/dist/testing.js +19 -165
  47. package/dist/{ws-C72iVS5T.d.ts → ws-DW8ng02H.d.ts} +4 -5
  48. package/package.json +26 -26
  49. package/dist/dataset-CqrqTYPz.js +0 -1535
  50. package/dist/pg-edges-store-KH_FQCxx.js +0 -381
  51. package/dist/schema-g4FQLr0V.d.ts +0 -433
  52. package/dist/src-BD27zeGF.js +0 -2339
@@ -1,381 +0,0 @@
1
- import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
- import { n as txScopeIdFor } from "./src-BD27zeGF.js";
3
- import { randomUUID } from "node:crypto";
4
-
5
- //#region ../../packages/substrate-dataset-pg/src/pg-edges-store.ts
6
- /**
7
- * PgEdgesStore — PostgreSQL-backed reference implementation of EdgesStore.
8
- * Spec: §4.6, §6.2, §6.3.
9
- *
10
- * One table (`edges`) keyed by a text PK, no namespace scoping — edges
11
- * are engine-scoped (§4.6), not per-ns. The caller owns the `postgres` Sql
12
- * connection; this class never opens or closes pools.
13
- *
14
- * Edge ids are `crypto.randomUUID()`: unique, so cursor pagination works, but
15
- * not time-ordered. The sweep walks the whole table, so it doesn't need order.
16
- *
17
- * Edge.from/to .id is `string | number`, stored and read back as text (unlike
18
- * PgDataset, which preserves numeric ids).
19
- *
20
- * SQL safety: postgres.js tagged templates parameterise every value slot.
21
- * Never string-concat caller input — the `update` dynamic column list uses a
22
- * fixed allowlist of column names, not user-supplied keys.
23
- */
24
- const MUTABLE_PATCH_FIELDS = /* @__PURE__ */ new Set([
25
- "id",
26
- "lastSweptAt",
27
- "blocked"
28
- ]);
29
- /**
30
- * Batch size for chunked admin deletes (`dropEdgesByNamespace`,
31
- * `dropEdgesByType`): bounds per-statement lock hold on large tables.
32
- */
33
- const DEFAULT_DROP_BATCH_SIZE = 5e3;
34
- const VALID_POLICIES = /* @__PURE__ */ new Set([
35
- "set_null",
36
- "cascade",
37
- "restrict",
38
- "orphan"
39
- ]);
40
- const refToId = (id) => typeof id === "number" ? String(id) : id;
41
- var PgEdgesStore = class PgEdgesStore {
42
- #sql;
43
- #now;
44
- #inTx;
45
- #dropBatchSize;
46
- txScopeId;
47
- get #edgeCols() {
48
- return this.#sql`id, from_ns, from_type, from_id, from_bridged, to_ns, to_type, to_id, to_bridged, field_name, policy, created_at, deleted_at, last_swept_at, blocked`;
49
- }
50
- constructor(options) {
51
- this.#sql = options.sql;
52
- this.#now = options.now ?? (() => /* @__PURE__ */ new Date());
53
- this.#inTx = "inTx" in options ? options.inTx : false;
54
- const explicit = "txScopeId" in options ? options.txScopeId : void 0;
55
- this.txScopeId = explicit ?? txScopeIdFor(options.sql);
56
- const batch = options.dropBatchSize;
57
- if (batch !== void 0 && (!Number.isFinite(batch) || batch <= 0)) throw new Error(`PgEdgesStore: dropBatchSize must be a positive finite number, got ${batch}`);
58
- this.#dropBatchSize = batch ?? DEFAULT_DROP_BATCH_SIZE;
59
- }
60
- async write(input) {
61
- const id = randomUUID();
62
- const createdAt = this.#now();
63
- const fromBridged = input.from.bridged === true;
64
- const toBridged = input.to.bridged === true;
65
- await this.#sql`
66
- INSERT INTO edges (
67
- id, from_ns, from_type, from_id, from_bridged,
68
- to_ns, to_type, to_id, to_bridged,
69
- field_name, policy, created_at, deleted_at, last_swept_at, blocked
70
- )
71
- VALUES (
72
- ${id},
73
- ${input.from.ns}, ${input.from.type}, ${refToId(input.from.id)}, ${fromBridged},
74
- ${input.to.ns}, ${input.to.type}, ${refToId(input.to.id)}, ${toBridged},
75
- ${input.fieldName}, ${input.policy}, ${createdAt},
76
- NULL, NULL, false
77
- )
78
- `;
79
- return {
80
- id,
81
- from: buildRef(input.from.ns, input.from.type, refToId(input.from.id), fromBridged),
82
- to: buildRef(input.to.ns, input.to.type, refToId(input.to.id), toBridged),
83
- fieldName: input.fieldName,
84
- policy: input.policy,
85
- createdAt
86
- };
87
- }
88
- async tombstone(id, at) {
89
- const stamp = at ?? this.#now();
90
- await this.#sql`
91
- UPDATE edges
92
- SET deleted_at = ${stamp}
93
- WHERE id = ${id}
94
- AND deleted_at IS NULL
95
- `;
96
- }
97
- /**
98
- * Hard-deletes tombstoned edges whose `deleted_at` is older than
99
- * `olderThan`. Called by the sweep loop. Returns the number deleted.
100
- */
101
- async purgeTombstonedEdges(opts) {
102
- return { deleted: (await this.#sql`
103
- DELETE FROM edges
104
- WHERE deleted_at IS NOT NULL
105
- AND deleted_at < ${opts.olderThan}
106
- RETURNING id
107
- `).length };
108
- }
109
- async update(patch) {
110
- for (const key of Object.keys(patch)) if (!MUTABLE_PATCH_FIELDS.has(key)) throw new Error(`EdgesStore.update: field '${key}' is immutable`);
111
- const hasLastSweptAt = Object.hasOwn(patch, "lastSweptAt");
112
- const hasBlocked = Object.hasOwn(patch, "blocked");
113
- if (!hasLastSweptAt && !hasBlocked) return;
114
- if (hasLastSweptAt && hasBlocked) {
115
- await this.#sql`
116
- UPDATE edges
117
- SET last_swept_at = ${patch.lastSweptAt ?? null},
118
- blocked = ${patch.blocked ?? false}
119
- WHERE id = ${patch.id}
120
- `;
121
- return;
122
- }
123
- if (hasLastSweptAt) {
124
- await this.#sql`
125
- UPDATE edges
126
- SET last_swept_at = ${patch.lastSweptAt ?? null}
127
- WHERE id = ${patch.id}
128
- `;
129
- return;
130
- }
131
- await this.#sql`
132
- UPDATE edges
133
- SET blocked = ${patch.blocked ?? false}
134
- WHERE id = ${patch.id}
135
- `;
136
- }
137
- async readNextBatch(cursorId, size) {
138
- const edges = (cursorId === null ? await this.#sql`
139
- SELECT ${this.#edgeCols}
140
- FROM edges
141
- ORDER BY id ASC
142
- LIMIT ${size}
143
- ` : await this.#sql`
144
- SELECT ${this.#edgeCols}
145
- FROM edges
146
- WHERE id > ${cursorId}
147
- ORDER BY id ASC
148
- LIMIT ${size}
149
- `).map(rowToEdge);
150
- const last = edges[edges.length - 1];
151
- return {
152
- edges,
153
- lastId: last ? last.id : null
154
- };
155
- }
156
- async findEdgesTo(ref) {
157
- return (await this.#sql`
158
- SELECT ${this.#edgeCols}
159
- FROM edges
160
- WHERE to_ns = ${ref.ns}
161
- AND to_type = ${ref.type}
162
- AND to_id = ${refToId(ref.id)}
163
- AND deleted_at IS NULL
164
- `).map(rowToEdge);
165
- }
166
- async findEdgesToMany(refs) {
167
- if (refs.length === 0) return [];
168
- const sql = this.#sql;
169
- const seenRef = /* @__PURE__ */ new Set();
170
- const toNs = [];
171
- const toType = [];
172
- const toId = [];
173
- for (const r of refs) {
174
- const id = refToId(r.id);
175
- const k = `${r.ns}\u0000${r.type}\u0000${id}`;
176
- if (seenRef.has(k)) continue;
177
- seenRef.add(k);
178
- toNs.push(r.ns);
179
- toType.push(r.type);
180
- toId.push(id);
181
- }
182
- return (await sql`
183
- SELECT ${this.#edgeCols}
184
- FROM edges
185
- JOIN unnest(${toNs}::text[], ${toType}::text[], ${toId}::text[])
186
- AS seek(seek_ns, seek_type, seek_id)
187
- ON to_ns = seek.seek_ns AND to_type = seek.seek_type AND to_id = seek.seek_id
188
- WHERE deleted_at IS NULL
189
- `).map(rowToEdge);
190
- }
191
- async findEdgesFrom(ref) {
192
- return (await this.#sql`
193
- SELECT ${this.#edgeCols}
194
- FROM edges
195
- WHERE from_ns = ${ref.ns}
196
- AND from_type = ${ref.type}
197
- AND from_id = ${refToId(ref.id)}
198
- AND deleted_at IS NULL
199
- `).map(rowToEdge);
200
- }
201
- async findEdgesTouchingNamespace(namespace, limit) {
202
- return (await this.#sql`
203
- SELECT ${this.#edgeCols}
204
- FROM edges
205
- WHERE deleted_at IS NULL
206
- AND (from_ns = ${namespace} OR to_ns = ${namespace})
207
- ORDER BY id ASC
208
- LIMIT ${limit}
209
- `).map(rowToEdge);
210
- }
211
- async findEdgesTouchingType(namespace, type, limit) {
212
- return (await this.#sql`
213
- SELECT ${this.#edgeCols}
214
- FROM edges
215
- WHERE deleted_at IS NULL
216
- AND ((from_ns = ${namespace} AND from_type = ${type})
217
- OR (to_ns = ${namespace} AND to_type = ${type}))
218
- ORDER BY id ASC
219
- LIMIT ${limit}
220
- `).map(rowToEdge);
221
- }
222
- async namespacesLinkedTo(namespace, type) {
223
- const fromMatch = type === void 0 ? this.#sql`from_ns = ${namespace}` : this.#sql`(from_ns = ${namespace} AND from_type = ${type})`;
224
- const toMatch = type === void 0 ? this.#sql`to_ns = ${namespace}` : this.#sql`(to_ns = ${namespace} AND to_type = ${type})`;
225
- return (await this.#sql`
226
- SELECT ns FROM (
227
- SELECT to_ns AS ns FROM edges WHERE ${fromMatch}
228
- UNION
229
- SELECT from_ns AS ns FROM edges WHERE ${toMatch}
230
- ) linked
231
- WHERE ns <> ${namespace}
232
- ORDER BY ns
233
- `).map((r) => r.ns);
234
- }
235
- /**
236
- * Chunked physical delete of every edge touching `namespace`, at most
237
- * `dropBatchSize` rows per statement, so a large delete does not hold every
238
- * row lock at once. Returns the number deleted.
239
- *
240
- * Outside a tx each batch auto-commits. Inside a caller's tx the batches
241
- * stay in it, so a throw still rolls back the whole drop.
242
- */
243
- async dropEdgesByNamespace(namespace, within) {
244
- const batchSize = this.#dropBatchSize;
245
- const farEnd = this.#farEndWithin(namespace, within, this.#sql`from_ns = ${namespace}`, this.#sql`to_ns = ${namespace}`);
246
- let total = 0;
247
- while (true) {
248
- const rows = await this.#sql`
249
- DELETE FROM edges
250
- WHERE id IN (
251
- SELECT id FROM edges
252
- WHERE (from_ns = ${namespace} OR to_ns = ${namespace}) ${farEnd}
253
- LIMIT ${batchSize}
254
- )
255
- RETURNING id
256
- `;
257
- total += rows.length;
258
- if (rows.length < batchSize) return total;
259
- await this.#yieldBetweenBatches();
260
- }
261
- }
262
- /**
263
- * Chunked physical delete of every edge touching `(namespace, type)`.
264
- * See {@link dropEdgesByNamespace} for the lock-hold + tx semantics.
265
- */
266
- async dropEdgesByType(namespace, type, within) {
267
- const batchSize = this.#dropBatchSize;
268
- const fromMatch = this.#sql`(from_ns = ${namespace} AND from_type = ${type})`;
269
- const toMatch = this.#sql`(to_ns = ${namespace} AND to_type = ${type})`;
270
- const farEnd = this.#farEndWithin(namespace, within, fromMatch, toMatch);
271
- let total = 0;
272
- while (true) {
273
- const rows = await this.#sql`
274
- DELETE FROM edges
275
- WHERE id IN (
276
- SELECT id FROM edges
277
- WHERE (${fromMatch} OR ${toMatch}) ${farEnd}
278
- LIMIT ${batchSize}
279
- )
280
- RETURNING id
281
- `;
282
- total += rows.length;
283
- if (rows.length < batchSize) return total;
284
- await this.#yieldBetweenBatches();
285
- }
286
- }
287
- /** `AND` clause keeping only edges whose far end is `namespace` or in `within`. */
288
- #farEndWithin(namespace, within, fromMatch, toMatch) {
289
- if (within === void 0) return this.#sql``;
290
- const allowed = this.#sql([namespace, ...within]);
291
- return this.#sql`AND ((${fromMatch} AND to_ns IN ${allowed}) OR (${toMatch} AND from_ns IN ${allowed}))`;
292
- }
293
- /**
294
- * Yields to the event loop between drop batches outside a tx; no-op inside one.
295
- */
296
- async #yieldBetweenBatches() {
297
- if (!this.#inTx) await new Promise((resolve) => setImmediate(resolve));
298
- }
299
- async canDelete(ref) {
300
- const rows = await this.#sql`
301
- SELECT ${this.#edgeCols}
302
- FROM edges
303
- WHERE to_ns = ${ref.ns}
304
- AND to_type = ${ref.type}
305
- AND to_id = ${refToId(ref.id)}
306
- AND policy = 'restrict'
307
- AND deleted_at IS NULL
308
- `;
309
- if (rows.length === 0) return { ok: true };
310
- return {
311
- ok: false,
312
- blockedBy: rows.map(rowToEdge)
313
- };
314
- }
315
- async findBlocked() {
316
- return (await this.#sql`
317
- SELECT ${this.#edgeCols}
318
- FROM edges
319
- WHERE blocked = true
320
- AND deleted_at IS NULL
321
- `).map(rowToEdge);
322
- }
323
- /**
324
- * `sql.begin`-backed atomicity. Same flattening pattern as PgDataset —
325
- * nested re-entry skips the inner begin and runs on the existing tx handle
326
- * so an inner throw propagates to the outer `sql.begin` for rollback.
327
- */
328
- async withTransaction(fn) {
329
- if (this.#inTx) return fn(this);
330
- return this.#sql.begin(async (txSql) => {
331
- return fn(new PgEdgesStore({
332
- sql: txSql,
333
- now: this.#now,
334
- inTx: true,
335
- txScopeId: this.txScopeId,
336
- dropBatchSize: this.#dropBatchSize
337
- }));
338
- });
339
- }
340
- /**
341
- * Engine-only. Returns a tx-bound clone on the supplied `TransactionSql`,
342
- * for `PgDataset.withScopedTransaction`. The caller has verified
343
- * the txScopeIds match.
344
- */
345
- _enlistInTx(txSql) {
346
- return new PgEdgesStore({
347
- sql: txSql,
348
- now: this.#now,
349
- inTx: true,
350
- txScopeId: this.txScopeId,
351
- dropBatchSize: this.#dropBatchSize
352
- });
353
- }
354
- };
355
- function buildRef(ns, type, id, bridged) {
356
- const ref = {
357
- ns,
358
- type,
359
- id
360
- };
361
- if (bridged) ref.bridged = true;
362
- return ref;
363
- }
364
- function rowToEdge(row) {
365
- if (!VALID_POLICIES.has(row.policy)) throw new Error(`PgEdgesStore: unknown policy '${row.policy}' on edge id=${row.id}`);
366
- const edge = {
367
- id: row.id,
368
- from: buildRef(row.from_ns, row.from_type, row.from_id, row.from_bridged),
369
- to: buildRef(row.to_ns, row.to_type, row.to_id, row.to_bridged),
370
- fieldName: row.field_name,
371
- policy: row.policy,
372
- createdAt: row.created_at
373
- };
374
- if (row.deleted_at !== null) edge.deletedAt = row.deleted_at;
375
- if (row.last_swept_at !== null) edge.lastSweptAt = row.last_swept_at;
376
- if (row.blocked) edge.blocked = true;
377
- return edge;
378
- }
379
-
380
- //#endregion
381
- export { PgEdgesStore as t };