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

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/dist/substrate.js CHANGED
@@ -4,9 +4,10 @@ import { B as saveSweepState, G as registerExtensionOperator, I as effectiveFilt
4
4
  import { i as DEFAULT_CAPABILITIES, n as trimTrailingSlashes, s as makeAuthorizationProvider, t as parsePositiveInt } from "./env-utils-CGNazUNt.js";
5
5
  import { L as encodeList, T as SYSTEM_NAMESPACE, f as coversNamespace, m as grantedIn, p as grantOf } from "./row-DZ0Fnpc6.js";
6
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 InMemoryLexicalStore, i as PgClusterStore, n as PgLexicalStore, o as InMemoryEmbeddingsStore, r as PgEmbeddingsStore, s as InMemoryClusterStore } from "./src-DWm0iA88.js";
8
- import { a as runDerivedMigrations, n as txScopeIdFor, o as runMigrations, r as deriveListIndexMigration, s as tryWithPgAdvisoryLock, t as PgDataset } from "./src-CXKnqeTW.js";
9
- import { B as version, R as openSql, t as openPgDataset, z as AdminCliError } from "./dataset-BQ8egGl5.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-DQ1U9Yhs.js";
10
+ import { B as version, R as openSql, t as openPgDataset, z as AdminCliError } from "./dataset-Bd18RP3O.js";
10
11
  import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
11
12
  import { homedir, hostname } from "node:os";
12
13
  import { basename, dirname, join, resolve } from "node:path";
@@ -292,382 +293,6 @@ var MemoryIdempotencyStore = class {
292
293
  }
293
294
  };
294
295
 
295
- //#endregion
296
- //#region ../../packages/substrate-dataset-pg/src/pg-edges-store.ts
297
- /**
298
- * PgEdgesStore — PostgreSQL-backed reference implementation of EdgesStore.
299
- * Spec: §4.6, §6.2, §6.3.
300
- *
301
- * One table (`edges`) keyed by a text PK, no namespace scoping — edges
302
- * are engine-scoped (§4.6), not per-ns. The caller owns the `postgres` Sql
303
- * connection; this class never opens or closes pools.
304
- *
305
- * Edge ids are `crypto.randomUUID()`: unique, so cursor pagination works, but
306
- * not time-ordered. The sweep walks the whole table, so it doesn't need order.
307
- *
308
- * Edge.from/to .id is `string | number`, stored and read back as text (unlike
309
- * PgDataset, which preserves numeric ids).
310
- *
311
- * SQL safety: postgres.js tagged templates parameterise every value slot.
312
- * Never string-concat caller input — the `update` dynamic column list uses a
313
- * fixed allowlist of column names, not user-supplied keys.
314
- */
315
- const MUTABLE_PATCH_FIELDS = /* @__PURE__ */ new Set([
316
- "id",
317
- "lastSweptAt",
318
- "blocked"
319
- ]);
320
- /**
321
- * Batch size for chunked admin deletes (`dropEdgesByNamespace`,
322
- * `dropEdgesByType`): bounds per-statement lock hold on large tables.
323
- */
324
- const DEFAULT_DROP_BATCH_SIZE = 5e3;
325
- const VALID_POLICIES = /* @__PURE__ */ new Set([
326
- "set_null",
327
- "cascade",
328
- "restrict",
329
- "orphan"
330
- ]);
331
- const refToId = (id) => typeof id === "number" ? String(id) : id;
332
- var PgEdgesStore = class PgEdgesStore {
333
- #sql;
334
- #now;
335
- #inTx;
336
- #dropBatchSize;
337
- txScopeId;
338
- get #edgeCols() {
339
- 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`;
340
- }
341
- constructor(options) {
342
- this.#sql = options.sql;
343
- this.#now = options.now ?? (() => /* @__PURE__ */ new Date());
344
- this.#inTx = "inTx" in options ? options.inTx : false;
345
- const explicit = "txScopeId" in options ? options.txScopeId : void 0;
346
- this.txScopeId = explicit ?? txScopeIdFor(options.sql);
347
- const batch = options.dropBatchSize;
348
- if (batch !== void 0 && (!Number.isFinite(batch) || batch <= 0)) throw new Error(`PgEdgesStore: dropBatchSize must be a positive finite number, got ${batch}`);
349
- this.#dropBatchSize = batch ?? DEFAULT_DROP_BATCH_SIZE;
350
- }
351
- async write(input) {
352
- const id = randomUUID();
353
- const createdAt = this.#now();
354
- const fromBridged = input.from.bridged === true;
355
- const toBridged = input.to.bridged === true;
356
- await this.#sql`
357
- INSERT INTO edges (
358
- id, from_ns, from_type, from_id, from_bridged,
359
- to_ns, to_type, to_id, to_bridged,
360
- field_name, policy, created_at, deleted_at, last_swept_at, blocked
361
- )
362
- VALUES (
363
- ${id},
364
- ${input.from.ns}, ${input.from.type}, ${refToId(input.from.id)}, ${fromBridged},
365
- ${input.to.ns}, ${input.to.type}, ${refToId(input.to.id)}, ${toBridged},
366
- ${input.fieldName}, ${input.policy}, ${createdAt},
367
- NULL, NULL, false
368
- )
369
- `;
370
- return {
371
- id,
372
- from: buildRef(input.from.ns, input.from.type, refToId(input.from.id), fromBridged),
373
- to: buildRef(input.to.ns, input.to.type, refToId(input.to.id), toBridged),
374
- fieldName: input.fieldName,
375
- policy: input.policy,
376
- createdAt
377
- };
378
- }
379
- async tombstone(id, at) {
380
- const stamp = at ?? this.#now();
381
- await this.#sql`
382
- UPDATE edges
383
- SET deleted_at = ${stamp}
384
- WHERE id = ${id}
385
- AND deleted_at IS NULL
386
- `;
387
- }
388
- /**
389
- * Hard-deletes tombstoned edges whose `deleted_at` is older than
390
- * `olderThan`. Called by the sweep loop. Returns the number deleted.
391
- */
392
- async purgeTombstonedEdges(opts) {
393
- return { deleted: (await this.#sql`
394
- DELETE FROM edges
395
- WHERE deleted_at IS NOT NULL
396
- AND deleted_at < ${opts.olderThan}
397
- RETURNING id
398
- `).length };
399
- }
400
- async update(patch) {
401
- for (const key of Object.keys(patch)) if (!MUTABLE_PATCH_FIELDS.has(key)) throw new Error(`EdgesStore.update: field '${key}' is immutable`);
402
- const hasLastSweptAt = Object.hasOwn(patch, "lastSweptAt");
403
- const hasBlocked = Object.hasOwn(patch, "blocked");
404
- if (!hasLastSweptAt && !hasBlocked) return;
405
- if (hasLastSweptAt && hasBlocked) {
406
- await this.#sql`
407
- UPDATE edges
408
- SET last_swept_at = ${patch.lastSweptAt ?? null},
409
- blocked = ${patch.blocked ?? false}
410
- WHERE id = ${patch.id}
411
- `;
412
- return;
413
- }
414
- if (hasLastSweptAt) {
415
- await this.#sql`
416
- UPDATE edges
417
- SET last_swept_at = ${patch.lastSweptAt ?? null}
418
- WHERE id = ${patch.id}
419
- `;
420
- return;
421
- }
422
- await this.#sql`
423
- UPDATE edges
424
- SET blocked = ${patch.blocked ?? false}
425
- WHERE id = ${patch.id}
426
- `;
427
- }
428
- async readNextBatch(cursorId, size) {
429
- const edges = (cursorId === null ? await this.#sql`
430
- SELECT ${this.#edgeCols}
431
- FROM edges
432
- ORDER BY id ASC
433
- LIMIT ${size}
434
- ` : await this.#sql`
435
- SELECT ${this.#edgeCols}
436
- FROM edges
437
- WHERE id > ${cursorId}
438
- ORDER BY id ASC
439
- LIMIT ${size}
440
- `).map(rowToEdge);
441
- const last = edges[edges.length - 1];
442
- return {
443
- edges,
444
- lastId: last ? last.id : null
445
- };
446
- }
447
- async findEdgesTo(ref) {
448
- return (await this.#sql`
449
- SELECT ${this.#edgeCols}
450
- FROM edges
451
- WHERE to_ns = ${ref.ns}
452
- AND to_type = ${ref.type}
453
- AND to_id = ${refToId(ref.id)}
454
- AND deleted_at IS NULL
455
- `).map(rowToEdge);
456
- }
457
- async findEdgesToMany(refs) {
458
- if (refs.length === 0) return [];
459
- const sql = this.#sql;
460
- const seenRef = /* @__PURE__ */ new Set();
461
- const toNs = [];
462
- const toType = [];
463
- const toId = [];
464
- for (const r of refs) {
465
- const id = refToId(r.id);
466
- const k = `${r.ns}\u0000${r.type}\u0000${id}`;
467
- if (seenRef.has(k)) continue;
468
- seenRef.add(k);
469
- toNs.push(r.ns);
470
- toType.push(r.type);
471
- toId.push(id);
472
- }
473
- return (await sql`
474
- SELECT ${this.#edgeCols}
475
- FROM edges
476
- JOIN unnest(${toNs}::text[], ${toType}::text[], ${toId}::text[])
477
- AS seek(seek_ns, seek_type, seek_id)
478
- ON to_ns = seek.seek_ns AND to_type = seek.seek_type AND to_id = seek.seek_id
479
- WHERE deleted_at IS NULL
480
- `).map(rowToEdge);
481
- }
482
- async findEdgesFrom(ref) {
483
- return (await this.#sql`
484
- SELECT ${this.#edgeCols}
485
- FROM edges
486
- WHERE from_ns = ${ref.ns}
487
- AND from_type = ${ref.type}
488
- AND from_id = ${refToId(ref.id)}
489
- AND deleted_at IS NULL
490
- `).map(rowToEdge);
491
- }
492
- async findEdgesTouchingNamespace(namespace, limit) {
493
- return (await this.#sql`
494
- SELECT ${this.#edgeCols}
495
- FROM edges
496
- WHERE deleted_at IS NULL
497
- AND (from_ns = ${namespace} OR to_ns = ${namespace})
498
- ORDER BY id ASC
499
- LIMIT ${limit}
500
- `).map(rowToEdge);
501
- }
502
- async findEdgesTouchingType(namespace, type, limit) {
503
- return (await this.#sql`
504
- SELECT ${this.#edgeCols}
505
- FROM edges
506
- WHERE deleted_at IS NULL
507
- AND ((from_ns = ${namespace} AND from_type = ${type})
508
- OR (to_ns = ${namespace} AND to_type = ${type}))
509
- ORDER BY id ASC
510
- LIMIT ${limit}
511
- `).map(rowToEdge);
512
- }
513
- async namespacesLinkedTo(namespace, type) {
514
- const fromMatch = type === void 0 ? this.#sql`from_ns = ${namespace}` : this.#sql`(from_ns = ${namespace} AND from_type = ${type})`;
515
- const toMatch = type === void 0 ? this.#sql`to_ns = ${namespace}` : this.#sql`(to_ns = ${namespace} AND to_type = ${type})`;
516
- return (await this.#sql`
517
- SELECT ns FROM (
518
- SELECT to_ns AS ns FROM edges WHERE ${fromMatch}
519
- UNION
520
- SELECT from_ns AS ns FROM edges WHERE ${toMatch}
521
- ) linked
522
- WHERE ns <> ${namespace}
523
- ORDER BY ns
524
- `).map((r) => r.ns);
525
- }
526
- /**
527
- * Chunked physical delete of every edge touching `namespace`, at most
528
- * `dropBatchSize` rows per statement, so a large delete does not hold every
529
- * row lock at once. Returns the number deleted.
530
- *
531
- * Outside a tx each batch auto-commits. Inside a caller's tx the batches
532
- * stay in it, so a throw still rolls back the whole drop.
533
- */
534
- async dropEdgesByNamespace(namespace, within) {
535
- const batchSize = this.#dropBatchSize;
536
- const farEnd = this.#farEndWithin(namespace, within, this.#sql`from_ns = ${namespace}`, this.#sql`to_ns = ${namespace}`);
537
- let total = 0;
538
- while (true) {
539
- const rows = await this.#sql`
540
- DELETE FROM edges
541
- WHERE id IN (
542
- SELECT id FROM edges
543
- WHERE (from_ns = ${namespace} OR to_ns = ${namespace}) ${farEnd}
544
- LIMIT ${batchSize}
545
- )
546
- RETURNING id
547
- `;
548
- total += rows.length;
549
- if (rows.length < batchSize) return total;
550
- await this.#yieldBetweenBatches();
551
- }
552
- }
553
- /**
554
- * Chunked physical delete of every edge touching `(namespace, type)`.
555
- * See {@link dropEdgesByNamespace} for the lock-hold + tx semantics.
556
- */
557
- async dropEdgesByType(namespace, type, within) {
558
- const batchSize = this.#dropBatchSize;
559
- const fromMatch = this.#sql`(from_ns = ${namespace} AND from_type = ${type})`;
560
- const toMatch = this.#sql`(to_ns = ${namespace} AND to_type = ${type})`;
561
- const farEnd = this.#farEndWithin(namespace, within, fromMatch, toMatch);
562
- let total = 0;
563
- while (true) {
564
- const rows = await this.#sql`
565
- DELETE FROM edges
566
- WHERE id IN (
567
- SELECT id FROM edges
568
- WHERE (${fromMatch} OR ${toMatch}) ${farEnd}
569
- LIMIT ${batchSize}
570
- )
571
- RETURNING id
572
- `;
573
- total += rows.length;
574
- if (rows.length < batchSize) return total;
575
- await this.#yieldBetweenBatches();
576
- }
577
- }
578
- /** `AND` clause keeping only edges whose far end is `namespace` or in `within`. */
579
- #farEndWithin(namespace, within, fromMatch, toMatch) {
580
- if (within === void 0) return this.#sql``;
581
- const allowed = this.#sql([namespace, ...within]);
582
- return this.#sql`AND ((${fromMatch} AND to_ns IN ${allowed}) OR (${toMatch} AND from_ns IN ${allowed}))`;
583
- }
584
- /**
585
- * Yields to the event loop between drop batches outside a tx; no-op inside one.
586
- */
587
- async #yieldBetweenBatches() {
588
- if (!this.#inTx) await new Promise((resolve) => setImmediate(resolve));
589
- }
590
- async canDelete(ref) {
591
- const rows = await this.#sql`
592
- SELECT ${this.#edgeCols}
593
- FROM edges
594
- WHERE to_ns = ${ref.ns}
595
- AND to_type = ${ref.type}
596
- AND to_id = ${refToId(ref.id)}
597
- AND policy = 'restrict'
598
- AND deleted_at IS NULL
599
- `;
600
- if (rows.length === 0) return { ok: true };
601
- return {
602
- ok: false,
603
- blockedBy: rows.map(rowToEdge)
604
- };
605
- }
606
- async findBlocked() {
607
- return (await this.#sql`
608
- SELECT ${this.#edgeCols}
609
- FROM edges
610
- WHERE blocked = true
611
- AND deleted_at IS NULL
612
- `).map(rowToEdge);
613
- }
614
- /**
615
- * `sql.begin`-backed atomicity. Same flattening pattern as PgDataset —
616
- * nested re-entry skips the inner begin and runs on the existing tx handle
617
- * so an inner throw propagates to the outer `sql.begin` for rollback.
618
- */
619
- async withTransaction(fn) {
620
- if (this.#inTx) return fn(this);
621
- return this.#sql.begin(async (txSql) => {
622
- return fn(new PgEdgesStore({
623
- sql: txSql,
624
- now: this.#now,
625
- inTx: true,
626
- txScopeId: this.txScopeId,
627
- dropBatchSize: this.#dropBatchSize
628
- }));
629
- });
630
- }
631
- /**
632
- * Engine-only. Returns a tx-bound clone on the supplied `TransactionSql`,
633
- * for `PgDataset.withScopedTransaction`. The caller has verified
634
- * the txScopeIds match.
635
- */
636
- _enlistInTx(txSql) {
637
- return new PgEdgesStore({
638
- sql: txSql,
639
- now: this.#now,
640
- inTx: true,
641
- txScopeId: this.txScopeId,
642
- dropBatchSize: this.#dropBatchSize
643
- });
644
- }
645
- };
646
- function buildRef(ns, type, id, bridged) {
647
- const ref = {
648
- ns,
649
- type,
650
- id
651
- };
652
- if (bridged) ref.bridged = true;
653
- return ref;
654
- }
655
- function rowToEdge(row) {
656
- if (!VALID_POLICIES.has(row.policy)) throw new Error(`PgEdgesStore: unknown policy '${row.policy}' on edge id=${row.id}`);
657
- const edge = {
658
- id: row.id,
659
- from: buildRef(row.from_ns, row.from_type, row.from_id, row.from_bridged),
660
- to: buildRef(row.to_ns, row.to_type, row.to_id, row.to_bridged),
661
- fieldName: row.field_name,
662
- policy: row.policy,
663
- createdAt: row.created_at
664
- };
665
- if (row.deleted_at !== null) edge.deletedAt = row.deleted_at;
666
- if (row.last_swept_at !== null) edge.lastSweptAt = row.last_swept_at;
667
- if (row.blocked) edge.blocked = true;
668
- return edge;
669
- }
670
-
671
296
  //#endregion
672
297
  //#region ../../packages/substrate-dataset-pg/src/pg-idempotency-store.ts
673
298
  var PgIdempotencyStore = class {
@@ -5323,7 +4948,8 @@ function createMcpServer(endpoint, options = {}) {
5323
4948
  type: z.string().min(1).describe("Type to search, e.g. \"Note\". Required."),
5324
4949
  threshold: z.number().optional().describe("Minimum similarity (0..1). Default 0."),
5325
4950
  lexicalThreshold: z.number().optional().describe("Cosine floor for hits only the lexical (keyword) arm found. Default: same as threshold. 0 keeps them unfloored, so keyword matches survive a high threshold."),
5326
- where: z.record(z.string(), z.unknown()).optional().describe("Row-field filter applied inside every candidate source, so k fills with matching rows: field -> { eq: scalar | null } or { in: [scalar | null, ...] }, every field must match; eq null matches a missing or null field. Example: { \"project\": { \"in\": [\"substrate\", null] } }.")
4951
+ where: z.record(z.string(), z.unknown()).optional().describe("Row-field filter applied inside every candidate source, so k fills with matching rows: field -> { eq: scalar | null } or { in: [scalar | null, ...] }, every field must match; eq null matches a missing or null field. Example: { \"project\": { \"in\": [\"substrate\", null] } }."),
4952
+ rowFields: z.union([z.record(z.string(), z.literal(true)), z.literal(true)]).optional().describe("Row-body projection. Omitted, each hit's `row` carries `id` plus its handle fields (name, title, topic, synopsis, description), which is enough to decide on. Ask for more only when you need it: field -> true keeps just those fields plus `id`, e.g. { \"synopsis\": true, \"body\": true }; true returns the whole body.")
5327
4953
  }
5328
4954
  },
5329
4955
  handler: async (input) => {