@mnmal-ai/substrate 1.0.0-alpha.1 → 1.0.0-alpha.3
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.
- package/README.md +1 -1
- package/dist/client.js +4 -4
- package/dist/{dataset-DOXGRz50.js → dataset-BBZuhrsL.js} +9 -9
- package/dist/{agent-token-BIboWOU0.js → env-utils-CGNazUNt.js} +31 -2
- package/dist/pack.js +1 -1
- package/dist/pg-edges-store-DKPYyd8l.js +381 -0
- package/dist/{plugin-BuQQEzL-.js → plugin-QuJN9pG_.js} +40 -12
- package/dist/proposals-bootstrap.js +2 -2
- package/dist/proposals.js +1 -1
- package/dist/recall-bootstrap.js +11 -4
- package/dist/recall.config.schema.json +6 -0
- package/dist/recall.d.ts +21 -6
- package/dist/recall.js +2 -2
- package/dist/{row-DK7lPZVe.js → row-DZ0Fnpc6.js} +9 -11
- package/dist/sdk.d.ts +6 -0
- package/dist/sdk.js +5 -6
- package/dist/{src-zcyUofMJ.js → src-B-wkCHYL.js} +17 -11
- package/dist/{src-C4DzaIQZ.js → src-BLgbLq2k.js} +2 -0
- package/dist/{src-5hVbeiAu.js → src-CG7giuHr.js} +2 -2
- package/dist/{src-YXlv_RyL.js → src-CHp3QyqB.js} +232 -161
- package/dist/{src-CjEwiKXw.js → src-CXKnqeTW.js} +100 -88
- package/dist/{src-DuopV8u7.js → src-DMzc3HfW.js} +53 -11
- package/dist/substrate-admin.js +5 -5
- package/dist/substrate.js +29 -394
- package/dist/testing.d.ts +318 -3
- package/dist/testing.js +5 -3
- package/package.json +19 -19
- package/dist/env-utils-B7SGmoeJ.js +0 -21
package/dist/substrate.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
|
|
3
|
-
import { B as saveSweepState, G as registerExtensionOperator, I as effectiveFilterOperators, N as MemoryEdgesStore, Q as buildContext, R as loadSweepState, W as getExtensionOperator, a as composeFromManifests, at as WS_REASON_SERVER_RESTART, bt as BridgeUnreachableError, c as MemoryDataset, d as buildAuditRow, f as AUDIT_EVENT_TYPE, i as PluginLoadError, n as kernelMigrationTracking, nt as parseAuthConfig, o as DEFAULT_NAMESPACE, p as INVITE_TYPE, r as mountKernelRoute, s as compose, t as composeKernelPlugins, u as runInviteSweep, yt as validateHardDeleteEnv, z as runSweep } from "./src-
|
|
4
|
-
import {
|
|
5
|
-
import { L as encodeList, T as SYSTEM_NAMESPACE, f as coversNamespace, m as grantedIn, p as grantOf } from "./row-
|
|
6
|
-
import { t as
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { a as
|
|
10
|
-
import { B as version, R as openSql, t as openPgDataset, z as AdminCliError } from "./dataset-
|
|
3
|
+
import { B as saveSweepState, G as registerExtensionOperator, I as effectiveFilterOperators, N as MemoryEdgesStore, Q as buildContext, R as loadSweepState, W as getExtensionOperator, a as composeFromManifests, at as WS_REASON_SERVER_RESTART, bt as BridgeUnreachableError, c as MemoryDataset, d as buildAuditRow, f as AUDIT_EVENT_TYPE, i as PluginLoadError, n as kernelMigrationTracking, nt as parseAuthConfig, o as DEFAULT_NAMESPACE, p as INVITE_TYPE, r as mountKernelRoute, s as compose, t as composeKernelPlugins, u as runInviteSweep, yt as validateHardDeleteEnv, z as runSweep } from "./src-CHp3QyqB.js";
|
|
4
|
+
import { i as DEFAULT_CAPABILITIES, n as trimTrailingSlashes, s as makeAuthorizationProvider, t as parsePositiveInt } from "./env-utils-CGNazUNt.js";
|
|
5
|
+
import { L as encodeList, T as SYSTEM_NAMESPACE, f as coversNamespace, m as grantedIn, p as grantOf } from "./row-DZ0Fnpc6.js";
|
|
6
|
+
import { c as AuthError, d as AdminBackfillError, l as NetworkError, n as tokenAudience, r as SubstrateClient, t as noRedirectFetch } from "./src-DMzc3HfW.js";
|
|
7
|
+
import { a as runDerivedMigrations, o as runMigrations, r as deriveListIndexMigration, s as tryWithPgAdvisoryLock, t as PgDataset } from "./src-CXKnqeTW.js";
|
|
8
|
+
import { t as PgEdgesStore } from "./pg-edges-store-DKPYyd8l.js";
|
|
9
|
+
import { a as InMemoryLexicalStore, i as PgClusterStore, n as PgLexicalStore, o as InMemoryEmbeddingsStore, r as PgEmbeddingsStore, s as InMemoryClusterStore } from "./src-B-wkCHYL.js";
|
|
10
|
+
import { B as version, R as openSql, t as openPgDataset, z as AdminCliError } from "./dataset-BBZuhrsL.js";
|
|
11
11
|
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
12
12
|
import { homedir, hostname } from "node:os";
|
|
13
13
|
import { basename, dirname, join, resolve } from "node:path";
|
|
@@ -75,8 +75,13 @@ var HttpBridgeClient = class {
|
|
|
75
75
|
_deletedAt: true
|
|
76
76
|
}
|
|
77
77
|
} };
|
|
78
|
-
const
|
|
79
|
-
|
|
78
|
+
const envelope = await this.query(frame);
|
|
79
|
+
const data = envelope.data?.[key];
|
|
80
|
+
if (data === void 0) {
|
|
81
|
+
const code = envelope.errors?.[0]?.code ?? "no_data";
|
|
82
|
+
throw new BridgeUnreachableError(this.name, this.baseUrl, `refused (${code})`);
|
|
83
|
+
}
|
|
84
|
+
if (data === null) return null;
|
|
80
85
|
if (Array.isArray(data)) {
|
|
81
86
|
const first = data[0];
|
|
82
87
|
return first === void 0 || first === null ? null : first;
|
|
@@ -288,382 +293,6 @@ var MemoryIdempotencyStore = class {
|
|
|
288
293
|
}
|
|
289
294
|
};
|
|
290
295
|
|
|
291
|
-
//#endregion
|
|
292
|
-
//#region ../../packages/substrate-dataset-pg/src/pg-edges-store.ts
|
|
293
|
-
/**
|
|
294
|
-
* PgEdgesStore — PostgreSQL-backed reference implementation of EdgesStore.
|
|
295
|
-
* Spec: §4.6, §6.2, §6.3.
|
|
296
|
-
*
|
|
297
|
-
* One table (`edges`) keyed by a text PK, no namespace scoping — edges
|
|
298
|
-
* are engine-scoped (§4.6), not per-ns. The caller owns the `postgres` Sql
|
|
299
|
-
* connection; this class never opens or closes pools.
|
|
300
|
-
*
|
|
301
|
-
* Edge ids are `crypto.randomUUID()`: unique, so cursor pagination works, but
|
|
302
|
-
* not time-ordered. The sweep walks the whole table, so it doesn't need order.
|
|
303
|
-
*
|
|
304
|
-
* Edge.from/to .id is `string | number`, stored and read back as text (unlike
|
|
305
|
-
* PgDataset, which preserves numeric ids).
|
|
306
|
-
*
|
|
307
|
-
* SQL safety: postgres.js tagged templates parameterise every value slot.
|
|
308
|
-
* Never string-concat caller input — the `update` dynamic column list uses a
|
|
309
|
-
* fixed allowlist of column names, not user-supplied keys.
|
|
310
|
-
*/
|
|
311
|
-
const MUTABLE_PATCH_FIELDS = /* @__PURE__ */ new Set([
|
|
312
|
-
"id",
|
|
313
|
-
"lastSweptAt",
|
|
314
|
-
"blocked"
|
|
315
|
-
]);
|
|
316
|
-
/**
|
|
317
|
-
* Batch size for chunked admin deletes (`dropEdgesByNamespace`,
|
|
318
|
-
* `dropEdgesByType`): bounds per-statement lock hold on large tables.
|
|
319
|
-
*/
|
|
320
|
-
const DEFAULT_DROP_BATCH_SIZE = 5e3;
|
|
321
|
-
const VALID_POLICIES = /* @__PURE__ */ new Set([
|
|
322
|
-
"set_null",
|
|
323
|
-
"cascade",
|
|
324
|
-
"restrict",
|
|
325
|
-
"orphan"
|
|
326
|
-
]);
|
|
327
|
-
const refToId = (id) => typeof id === "number" ? String(id) : id;
|
|
328
|
-
var PgEdgesStore = class PgEdgesStore {
|
|
329
|
-
#sql;
|
|
330
|
-
#now;
|
|
331
|
-
#inTx;
|
|
332
|
-
#dropBatchSize;
|
|
333
|
-
txScopeId;
|
|
334
|
-
get #edgeCols() {
|
|
335
|
-
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`;
|
|
336
|
-
}
|
|
337
|
-
constructor(options) {
|
|
338
|
-
this.#sql = options.sql;
|
|
339
|
-
this.#now = options.now ?? (() => /* @__PURE__ */ new Date());
|
|
340
|
-
this.#inTx = "inTx" in options ? options.inTx : false;
|
|
341
|
-
const explicit = "txScopeId" in options ? options.txScopeId : void 0;
|
|
342
|
-
this.txScopeId = explicit ?? txScopeIdFor(options.sql);
|
|
343
|
-
const batch = options.dropBatchSize;
|
|
344
|
-
if (batch !== void 0 && (!Number.isFinite(batch) || batch <= 0)) throw new Error(`PgEdgesStore: dropBatchSize must be a positive finite number, got ${batch}`);
|
|
345
|
-
this.#dropBatchSize = batch ?? DEFAULT_DROP_BATCH_SIZE;
|
|
346
|
-
}
|
|
347
|
-
async write(input) {
|
|
348
|
-
const id = randomUUID();
|
|
349
|
-
const createdAt = this.#now();
|
|
350
|
-
const fromBridged = input.from.bridged === true;
|
|
351
|
-
const toBridged = input.to.bridged === true;
|
|
352
|
-
await this.#sql`
|
|
353
|
-
INSERT INTO edges (
|
|
354
|
-
id, from_ns, from_type, from_id, from_bridged,
|
|
355
|
-
to_ns, to_type, to_id, to_bridged,
|
|
356
|
-
field_name, policy, created_at, deleted_at, last_swept_at, blocked
|
|
357
|
-
)
|
|
358
|
-
VALUES (
|
|
359
|
-
${id},
|
|
360
|
-
${input.from.ns}, ${input.from.type}, ${refToId(input.from.id)}, ${fromBridged},
|
|
361
|
-
${input.to.ns}, ${input.to.type}, ${refToId(input.to.id)}, ${toBridged},
|
|
362
|
-
${input.fieldName}, ${input.policy}, ${createdAt},
|
|
363
|
-
NULL, NULL, false
|
|
364
|
-
)
|
|
365
|
-
`;
|
|
366
|
-
return {
|
|
367
|
-
id,
|
|
368
|
-
from: buildRef(input.from.ns, input.from.type, refToId(input.from.id), fromBridged),
|
|
369
|
-
to: buildRef(input.to.ns, input.to.type, refToId(input.to.id), toBridged),
|
|
370
|
-
fieldName: input.fieldName,
|
|
371
|
-
policy: input.policy,
|
|
372
|
-
createdAt
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
async tombstone(id, at) {
|
|
376
|
-
const stamp = at ?? this.#now();
|
|
377
|
-
await this.#sql`
|
|
378
|
-
UPDATE edges
|
|
379
|
-
SET deleted_at = ${stamp}
|
|
380
|
-
WHERE id = ${id}
|
|
381
|
-
AND deleted_at IS NULL
|
|
382
|
-
`;
|
|
383
|
-
}
|
|
384
|
-
/**
|
|
385
|
-
* Hard-deletes tombstoned edges whose `deleted_at` is older than
|
|
386
|
-
* `olderThan`. Called by the sweep loop. Returns the number deleted.
|
|
387
|
-
*/
|
|
388
|
-
async purgeTombstonedEdges(opts) {
|
|
389
|
-
return { deleted: (await this.#sql`
|
|
390
|
-
DELETE FROM edges
|
|
391
|
-
WHERE deleted_at IS NOT NULL
|
|
392
|
-
AND deleted_at < ${opts.olderThan}
|
|
393
|
-
RETURNING id
|
|
394
|
-
`).length };
|
|
395
|
-
}
|
|
396
|
-
async update(patch) {
|
|
397
|
-
for (const key of Object.keys(patch)) if (!MUTABLE_PATCH_FIELDS.has(key)) throw new Error(`EdgesStore.update: field '${key}' is immutable`);
|
|
398
|
-
const hasLastSweptAt = Object.hasOwn(patch, "lastSweptAt");
|
|
399
|
-
const hasBlocked = Object.hasOwn(patch, "blocked");
|
|
400
|
-
if (!hasLastSweptAt && !hasBlocked) return;
|
|
401
|
-
if (hasLastSweptAt && hasBlocked) {
|
|
402
|
-
await this.#sql`
|
|
403
|
-
UPDATE edges
|
|
404
|
-
SET last_swept_at = ${patch.lastSweptAt ?? null},
|
|
405
|
-
blocked = ${patch.blocked ?? false}
|
|
406
|
-
WHERE id = ${patch.id}
|
|
407
|
-
`;
|
|
408
|
-
return;
|
|
409
|
-
}
|
|
410
|
-
if (hasLastSweptAt) {
|
|
411
|
-
await this.#sql`
|
|
412
|
-
UPDATE edges
|
|
413
|
-
SET last_swept_at = ${patch.lastSweptAt ?? null}
|
|
414
|
-
WHERE id = ${patch.id}
|
|
415
|
-
`;
|
|
416
|
-
return;
|
|
417
|
-
}
|
|
418
|
-
await this.#sql`
|
|
419
|
-
UPDATE edges
|
|
420
|
-
SET blocked = ${patch.blocked ?? false}
|
|
421
|
-
WHERE id = ${patch.id}
|
|
422
|
-
`;
|
|
423
|
-
}
|
|
424
|
-
async readNextBatch(cursorId, size) {
|
|
425
|
-
const edges = (cursorId === null ? await this.#sql`
|
|
426
|
-
SELECT ${this.#edgeCols}
|
|
427
|
-
FROM edges
|
|
428
|
-
ORDER BY id ASC
|
|
429
|
-
LIMIT ${size}
|
|
430
|
-
` : await this.#sql`
|
|
431
|
-
SELECT ${this.#edgeCols}
|
|
432
|
-
FROM edges
|
|
433
|
-
WHERE id > ${cursorId}
|
|
434
|
-
ORDER BY id ASC
|
|
435
|
-
LIMIT ${size}
|
|
436
|
-
`).map(rowToEdge);
|
|
437
|
-
const last = edges[edges.length - 1];
|
|
438
|
-
return {
|
|
439
|
-
edges,
|
|
440
|
-
lastId: last ? last.id : null
|
|
441
|
-
};
|
|
442
|
-
}
|
|
443
|
-
async findEdgesTo(ref) {
|
|
444
|
-
return (await this.#sql`
|
|
445
|
-
SELECT ${this.#edgeCols}
|
|
446
|
-
FROM edges
|
|
447
|
-
WHERE to_ns = ${ref.ns}
|
|
448
|
-
AND to_type = ${ref.type}
|
|
449
|
-
AND to_id = ${refToId(ref.id)}
|
|
450
|
-
AND deleted_at IS NULL
|
|
451
|
-
`).map(rowToEdge);
|
|
452
|
-
}
|
|
453
|
-
async findEdgesToMany(refs) {
|
|
454
|
-
if (refs.length === 0) return [];
|
|
455
|
-
const sql = this.#sql;
|
|
456
|
-
const seenRef = /* @__PURE__ */ new Set();
|
|
457
|
-
const toNs = [];
|
|
458
|
-
const toType = [];
|
|
459
|
-
const toId = [];
|
|
460
|
-
for (const r of refs) {
|
|
461
|
-
const id = refToId(r.id);
|
|
462
|
-
const k = `${r.ns}\u0000${r.type}\u0000${id}`;
|
|
463
|
-
if (seenRef.has(k)) continue;
|
|
464
|
-
seenRef.add(k);
|
|
465
|
-
toNs.push(r.ns);
|
|
466
|
-
toType.push(r.type);
|
|
467
|
-
toId.push(id);
|
|
468
|
-
}
|
|
469
|
-
return (await sql`
|
|
470
|
-
SELECT ${this.#edgeCols}
|
|
471
|
-
FROM edges
|
|
472
|
-
JOIN unnest(${toNs}::text[], ${toType}::text[], ${toId}::text[])
|
|
473
|
-
AS seek(seek_ns, seek_type, seek_id)
|
|
474
|
-
ON to_ns = seek.seek_ns AND to_type = seek.seek_type AND to_id = seek.seek_id
|
|
475
|
-
WHERE deleted_at IS NULL
|
|
476
|
-
`).map(rowToEdge);
|
|
477
|
-
}
|
|
478
|
-
async findEdgesFrom(ref) {
|
|
479
|
-
return (await this.#sql`
|
|
480
|
-
SELECT ${this.#edgeCols}
|
|
481
|
-
FROM edges
|
|
482
|
-
WHERE from_ns = ${ref.ns}
|
|
483
|
-
AND from_type = ${ref.type}
|
|
484
|
-
AND from_id = ${refToId(ref.id)}
|
|
485
|
-
AND deleted_at IS NULL
|
|
486
|
-
`).map(rowToEdge);
|
|
487
|
-
}
|
|
488
|
-
async findEdgesTouchingNamespace(namespace, limit) {
|
|
489
|
-
return (await this.#sql`
|
|
490
|
-
SELECT ${this.#edgeCols}
|
|
491
|
-
FROM edges
|
|
492
|
-
WHERE deleted_at IS NULL
|
|
493
|
-
AND (from_ns = ${namespace} OR to_ns = ${namespace})
|
|
494
|
-
ORDER BY id ASC
|
|
495
|
-
LIMIT ${limit}
|
|
496
|
-
`).map(rowToEdge);
|
|
497
|
-
}
|
|
498
|
-
async findEdgesTouchingType(namespace, type, limit) {
|
|
499
|
-
return (await this.#sql`
|
|
500
|
-
SELECT ${this.#edgeCols}
|
|
501
|
-
FROM edges
|
|
502
|
-
WHERE deleted_at IS NULL
|
|
503
|
-
AND ((from_ns = ${namespace} AND from_type = ${type})
|
|
504
|
-
OR (to_ns = ${namespace} AND to_type = ${type}))
|
|
505
|
-
ORDER BY id ASC
|
|
506
|
-
LIMIT ${limit}
|
|
507
|
-
`).map(rowToEdge);
|
|
508
|
-
}
|
|
509
|
-
async namespacesLinkedTo(namespace, type) {
|
|
510
|
-
const fromMatch = type === void 0 ? this.#sql`from_ns = ${namespace}` : this.#sql`(from_ns = ${namespace} AND from_type = ${type})`;
|
|
511
|
-
const toMatch = type === void 0 ? this.#sql`to_ns = ${namespace}` : this.#sql`(to_ns = ${namespace} AND to_type = ${type})`;
|
|
512
|
-
return (await this.#sql`
|
|
513
|
-
SELECT ns FROM (
|
|
514
|
-
SELECT to_ns AS ns FROM edges WHERE ${fromMatch}
|
|
515
|
-
UNION
|
|
516
|
-
SELECT from_ns AS ns FROM edges WHERE ${toMatch}
|
|
517
|
-
) linked
|
|
518
|
-
WHERE ns <> ${namespace}
|
|
519
|
-
ORDER BY ns
|
|
520
|
-
`).map((r) => r.ns);
|
|
521
|
-
}
|
|
522
|
-
/**
|
|
523
|
-
* Chunked physical delete of every edge touching `namespace`, at most
|
|
524
|
-
* `dropBatchSize` rows per statement, so a large delete does not hold every
|
|
525
|
-
* row lock at once. Returns the number deleted.
|
|
526
|
-
*
|
|
527
|
-
* Outside a tx each batch auto-commits. Inside a caller's tx the batches
|
|
528
|
-
* stay in it, so a throw still rolls back the whole drop.
|
|
529
|
-
*/
|
|
530
|
-
async dropEdgesByNamespace(namespace, within) {
|
|
531
|
-
const batchSize = this.#dropBatchSize;
|
|
532
|
-
const farEnd = this.#farEndWithin(namespace, within, this.#sql`from_ns = ${namespace}`, this.#sql`to_ns = ${namespace}`);
|
|
533
|
-
let total = 0;
|
|
534
|
-
while (true) {
|
|
535
|
-
const rows = await this.#sql`
|
|
536
|
-
DELETE FROM edges
|
|
537
|
-
WHERE id IN (
|
|
538
|
-
SELECT id FROM edges
|
|
539
|
-
WHERE (from_ns = ${namespace} OR to_ns = ${namespace}) ${farEnd}
|
|
540
|
-
LIMIT ${batchSize}
|
|
541
|
-
)
|
|
542
|
-
RETURNING id
|
|
543
|
-
`;
|
|
544
|
-
total += rows.length;
|
|
545
|
-
if (rows.length < batchSize) return total;
|
|
546
|
-
await this.#yieldBetweenBatches();
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
/**
|
|
550
|
-
* Chunked physical delete of every edge touching `(namespace, type)`.
|
|
551
|
-
* See {@link dropEdgesByNamespace} for the lock-hold + tx semantics.
|
|
552
|
-
*/
|
|
553
|
-
async dropEdgesByType(namespace, type, within) {
|
|
554
|
-
const batchSize = this.#dropBatchSize;
|
|
555
|
-
const fromMatch = this.#sql`(from_ns = ${namespace} AND from_type = ${type})`;
|
|
556
|
-
const toMatch = this.#sql`(to_ns = ${namespace} AND to_type = ${type})`;
|
|
557
|
-
const farEnd = this.#farEndWithin(namespace, within, fromMatch, toMatch);
|
|
558
|
-
let total = 0;
|
|
559
|
-
while (true) {
|
|
560
|
-
const rows = await this.#sql`
|
|
561
|
-
DELETE FROM edges
|
|
562
|
-
WHERE id IN (
|
|
563
|
-
SELECT id FROM edges
|
|
564
|
-
WHERE (${fromMatch} OR ${toMatch}) ${farEnd}
|
|
565
|
-
LIMIT ${batchSize}
|
|
566
|
-
)
|
|
567
|
-
RETURNING id
|
|
568
|
-
`;
|
|
569
|
-
total += rows.length;
|
|
570
|
-
if (rows.length < batchSize) return total;
|
|
571
|
-
await this.#yieldBetweenBatches();
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
/** `AND` clause keeping only edges whose far end is `namespace` or in `within`. */
|
|
575
|
-
#farEndWithin(namespace, within, fromMatch, toMatch) {
|
|
576
|
-
if (within === void 0) return this.#sql``;
|
|
577
|
-
const allowed = this.#sql([namespace, ...within]);
|
|
578
|
-
return this.#sql`AND ((${fromMatch} AND to_ns IN ${allowed}) OR (${toMatch} AND from_ns IN ${allowed}))`;
|
|
579
|
-
}
|
|
580
|
-
/**
|
|
581
|
-
* Yields to the event loop between drop batches outside a tx; no-op inside one.
|
|
582
|
-
*/
|
|
583
|
-
async #yieldBetweenBatches() {
|
|
584
|
-
if (!this.#inTx) await new Promise((resolve) => setImmediate(resolve));
|
|
585
|
-
}
|
|
586
|
-
async canDelete(ref) {
|
|
587
|
-
const rows = await this.#sql`
|
|
588
|
-
SELECT ${this.#edgeCols}
|
|
589
|
-
FROM edges
|
|
590
|
-
WHERE to_ns = ${ref.ns}
|
|
591
|
-
AND to_type = ${ref.type}
|
|
592
|
-
AND to_id = ${refToId(ref.id)}
|
|
593
|
-
AND policy = 'restrict'
|
|
594
|
-
AND deleted_at IS NULL
|
|
595
|
-
`;
|
|
596
|
-
if (rows.length === 0) return { ok: true };
|
|
597
|
-
return {
|
|
598
|
-
ok: false,
|
|
599
|
-
blockedBy: rows.map(rowToEdge)
|
|
600
|
-
};
|
|
601
|
-
}
|
|
602
|
-
async findBlocked() {
|
|
603
|
-
return (await this.#sql`
|
|
604
|
-
SELECT ${this.#edgeCols}
|
|
605
|
-
FROM edges
|
|
606
|
-
WHERE blocked = true
|
|
607
|
-
AND deleted_at IS NULL
|
|
608
|
-
`).map(rowToEdge);
|
|
609
|
-
}
|
|
610
|
-
/**
|
|
611
|
-
* `sql.begin`-backed atomicity. Same flattening pattern as PgDataset —
|
|
612
|
-
* nested re-entry skips the inner begin and runs on the existing tx handle
|
|
613
|
-
* so an inner throw propagates to the outer `sql.begin` for rollback.
|
|
614
|
-
*/
|
|
615
|
-
async withTransaction(fn) {
|
|
616
|
-
if (this.#inTx) return fn(this);
|
|
617
|
-
return this.#sql.begin(async (txSql) => {
|
|
618
|
-
return fn(new PgEdgesStore({
|
|
619
|
-
sql: txSql,
|
|
620
|
-
now: this.#now,
|
|
621
|
-
inTx: true,
|
|
622
|
-
txScopeId: this.txScopeId,
|
|
623
|
-
dropBatchSize: this.#dropBatchSize
|
|
624
|
-
}));
|
|
625
|
-
});
|
|
626
|
-
}
|
|
627
|
-
/**
|
|
628
|
-
* Engine-only. Returns a tx-bound clone on the supplied `TransactionSql`,
|
|
629
|
-
* for `PgDataset.withScopedTransaction`. The caller has verified
|
|
630
|
-
* the txScopeIds match.
|
|
631
|
-
*/
|
|
632
|
-
_enlistInTx(txSql) {
|
|
633
|
-
return new PgEdgesStore({
|
|
634
|
-
sql: txSql,
|
|
635
|
-
now: this.#now,
|
|
636
|
-
inTx: true,
|
|
637
|
-
txScopeId: this.txScopeId,
|
|
638
|
-
dropBatchSize: this.#dropBatchSize
|
|
639
|
-
});
|
|
640
|
-
}
|
|
641
|
-
};
|
|
642
|
-
function buildRef(ns, type, id, bridged) {
|
|
643
|
-
const ref = {
|
|
644
|
-
ns,
|
|
645
|
-
type,
|
|
646
|
-
id
|
|
647
|
-
};
|
|
648
|
-
if (bridged) ref.bridged = true;
|
|
649
|
-
return ref;
|
|
650
|
-
}
|
|
651
|
-
function rowToEdge(row) {
|
|
652
|
-
if (!VALID_POLICIES.has(row.policy)) throw new Error(`PgEdgesStore: unknown policy '${row.policy}' on edge id=${row.id}`);
|
|
653
|
-
const edge = {
|
|
654
|
-
id: row.id,
|
|
655
|
-
from: buildRef(row.from_ns, row.from_type, row.from_id, row.from_bridged),
|
|
656
|
-
to: buildRef(row.to_ns, row.to_type, row.to_id, row.to_bridged),
|
|
657
|
-
fieldName: row.field_name,
|
|
658
|
-
policy: row.policy,
|
|
659
|
-
createdAt: row.created_at
|
|
660
|
-
};
|
|
661
|
-
if (row.deleted_at !== null) edge.deletedAt = row.deleted_at;
|
|
662
|
-
if (row.last_swept_at !== null) edge.lastSweptAt = row.last_swept_at;
|
|
663
|
-
if (row.blocked) edge.blocked = true;
|
|
664
|
-
return edge;
|
|
665
|
-
}
|
|
666
|
-
|
|
667
296
|
//#endregion
|
|
668
297
|
//#region ../../packages/substrate-dataset-pg/src/pg-idempotency-store.ts
|
|
669
298
|
var PgIdempotencyStore = class {
|
|
@@ -796,7 +425,7 @@ async function runAsk(argv, options = {}) {
|
|
|
796
425
|
else project = v;
|
|
797
426
|
} else words.push(a);
|
|
798
427
|
}
|
|
799
|
-
const endpoint = (env.SUBSTRATE_URL ?? "")
|
|
428
|
+
const endpoint = trimTrailingSlashes(env.SUBSTRATE_URL ?? "");
|
|
800
429
|
if (endpoint === "") {
|
|
801
430
|
write("SUBSTRATE_URL is required (e.g. https://substrate.example.com:3100)\n");
|
|
802
431
|
return 2;
|
|
@@ -896,7 +525,7 @@ async function runBackfillField(argv, options = {}) {
|
|
|
896
525
|
write("usage: substrate backfill-field <namespace> <Type> <field> --values <file.json> [--apply] [--overwrite] [--json]\n");
|
|
897
526
|
return 2;
|
|
898
527
|
}
|
|
899
|
-
const baseUrl = (options.baseUrl ?? process.env.SUBSTRATE_URL ?? "")
|
|
528
|
+
const baseUrl = trimTrailingSlashes(options.baseUrl ?? process.env.SUBSTRATE_URL ?? "");
|
|
900
529
|
if (baseUrl === "") {
|
|
901
530
|
write("SUBSTRATE_URL is required (e.g. https://substrate.example.com:3100)\n");
|
|
902
531
|
return 2;
|
|
@@ -1073,7 +702,7 @@ async function scanField(field, opts) {
|
|
|
1073
702
|
async function runCheckEnums(argv = [], options = {}) {
|
|
1074
703
|
const write = options.write ?? ((text) => process.stdout.write(text));
|
|
1075
704
|
const asJson = argv.includes("--json");
|
|
1076
|
-
const baseUrl = (options.baseUrl ?? process.env.SUBSTRATE_URL ?? "")
|
|
705
|
+
const baseUrl = trimTrailingSlashes(options.baseUrl ?? process.env.SUBSTRATE_URL ?? "");
|
|
1077
706
|
if (baseUrl === "") {
|
|
1078
707
|
write("SUBSTRATE_URL is required (e.g. https://substrate.example.com:3100)\n");
|
|
1079
708
|
return 2;
|
|
@@ -2091,7 +1720,10 @@ const filterRegistry = { register(name, op) {
|
|
|
2091
1720
|
const pendingMcpTools = [];
|
|
2092
1721
|
async function wireStore(storeSpec, fieldDefLookupFor) {
|
|
2093
1722
|
if (storeSpec === "memory") return {
|
|
2094
|
-
dataset: new MemoryDataset({
|
|
1723
|
+
dataset: new MemoryDataset({
|
|
1724
|
+
namespace: DEFAULT_NAMESPACE,
|
|
1725
|
+
fieldDefLookup: fieldDefLookupFor(DEFAULT_NAMESPACE)
|
|
1726
|
+
}),
|
|
2095
1727
|
edges: new MemoryEdgesStore({ now: () => /* @__PURE__ */ new Date() }),
|
|
2096
1728
|
substrateNamespaceDataset: null,
|
|
2097
1729
|
sql: null,
|
|
@@ -2226,7 +1858,10 @@ function buildPluginDatasetMap(plugins, defaultDataset, sharedSql, sharedEdges,
|
|
|
2226
1858
|
fieldDefLookup: fieldDefLookupFor(ns),
|
|
2227
1859
|
...nsDef.promotedColumns !== void 0 ? { promotedColumns: nsDef.promotedColumns } : {},
|
|
2228
1860
|
...sharedEdges !== null ? { edgesStore: sharedEdges } : {}
|
|
2229
|
-
}) : new MemoryDataset({
|
|
1861
|
+
}) : new MemoryDataset({
|
|
1862
|
+
namespace: ns,
|
|
1863
|
+
fieldDefLookup: fieldDefLookupFor(ns)
|
|
1864
|
+
});
|
|
2230
1865
|
}
|
|
2231
1866
|
return datasets;
|
|
2232
1867
|
}
|
|
@@ -4859,7 +4494,7 @@ async function runNormaliseVocab(argv, options = {}) {
|
|
|
4859
4494
|
write("usage: substrate normalise-vocab <namespace> <Type> <field> [--apply] [--reject a,b] [--json]\n");
|
|
4860
4495
|
return 2;
|
|
4861
4496
|
}
|
|
4862
|
-
const baseUrl = (options.baseUrl ?? process.env.SUBSTRATE_URL ?? "")
|
|
4497
|
+
const baseUrl = trimTrailingSlashes(options.baseUrl ?? process.env.SUBSTRATE_URL ?? "");
|
|
4863
4498
|
if (baseUrl === "") {
|
|
4864
4499
|
write("SUBSTRATE_URL is required (e.g. https://substrate.example.com:3100)\n");
|
|
4865
4500
|
return 2;
|
|
@@ -5013,7 +4648,7 @@ function toMessage(err) {
|
|
|
5013
4648
|
* Used by /substrate/v1/nl + /substrate/v1/_system/drop-{namespace,type}.
|
|
5014
4649
|
*/
|
|
5015
4650
|
async function postSubstrate(endpoint, path, body, errorMode = "textPreview") {
|
|
5016
|
-
const url = `${endpoint
|
|
4651
|
+
const url = `${trimTrailingSlashes(endpoint)}${path}`;
|
|
5017
4652
|
let res;
|
|
5018
4653
|
try {
|
|
5019
4654
|
res = await fetch(url, {
|
|
@@ -5051,7 +4686,7 @@ async function postSubstrate(endpoint, path, body, errorMode = "textPreview") {
|
|
|
5051
4686
|
* a transport failure wraps the cause with the failing URL.
|
|
5052
4687
|
*/
|
|
5053
4688
|
async function getSubstrate(endpoint, path) {
|
|
5054
|
-
const url = `${endpoint
|
|
4689
|
+
const url = `${trimTrailingSlashes(endpoint)}${path}`;
|
|
5055
4690
|
let res;
|
|
5056
4691
|
try {
|
|
5057
4692
|
res = await fetch(url, {
|