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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +48 -6
  2. package/dist/boot-store-C5vNZJ3i.js +4065 -0
  3. package/dist/{runtime-metrics-DwpDJ8vz.d.ts → bridge-COu0Tg_h.d.ts} +2 -87
  4. package/dist/cli-BrwW4tjO.js +3144 -0
  5. package/dist/client.d.ts +129 -8
  6. package/dist/client.js +6 -5
  7. package/dist/{agent-token-BIboWOU0.js → env-utils-CGNazUNt.js} +31 -2
  8. package/dist/{errors-D4vKuwz8.d.ts → errors-BOllGhVx.d.ts} +15 -2
  9. package/dist/{index-D7mSOFlM.d.ts → index-Cp74OcmS.d.ts} +1 -1
  10. package/dist/node-version-C5FSmwJX.js +26 -0
  11. package/dist/pack.d.ts +2 -2
  12. package/dist/pack.js +1 -1
  13. package/dist/pg-edges-store-BLsbcXkk.js +381 -0
  14. package/dist/{plugin-BuQQEzL-.js → plugin-DIYm2cZE.js} +171 -18
  15. package/dist/proposals-bootstrap.js +6 -4
  16. package/dist/proposals.config.schema.json +5 -0
  17. package/dist/proposals.d.ts +14 -4
  18. package/dist/proposals.default-config.json +2 -1
  19. package/dist/proposals.js +2 -2
  20. package/dist/recall-bootstrap.js +13 -4
  21. package/dist/recall.config.schema.json +12 -0
  22. package/dist/recall.d.ts +53 -9
  23. package/dist/recall.js +2 -2
  24. package/dist/{repl-C52wyfMs.js → repl-B8gy0YM7.js} +63 -30
  25. package/dist/{row-ByLFE2Wx.d.ts → row-DwaDU4b-.d.ts} +58 -9
  26. package/dist/run-B18qtGKc.js +2012 -0
  27. package/dist/{plugin-t2g2wX0b.d.ts → schema-CijHRfuk.d.ts} +957 -16
  28. package/dist/script-C6DSa_Na.js +134 -0
  29. package/dist/sdk.d.ts +104 -16
  30. package/dist/sdk.js +5 -110
  31. package/dist/{src-DuopV8u7.js → src-Br6aDUti.js} +153 -20
  32. package/dist/{src-5hVbeiAu.js → src-D041j6GS.js} +697 -57
  33. package/dist/{src-YXlv_RyL.js → src-DK7VEQT6.js} +4099 -1040
  34. package/dist/{src-C4DzaIQZ.js → src-Dbvuel6w.js} +392 -282
  35. package/dist/{row-DK7lPZVe.js → src-DpPdw2oL.js} +11 -11
  36. package/dist/{src-zcyUofMJ.js → src-DptNGNYQ.js} +39 -15
  37. package/dist/src-gz4I1F29.js +5097 -0
  38. package/dist/substrate-admin.js +17 -781
  39. package/dist/substrate.js +4 -5476
  40. package/dist/testing.d.ts +582 -9
  41. package/dist/testing.js +39 -3
  42. package/dist/{ws-C72iVS5T.d.ts → ws-DC8OhMaV.d.ts} +2 -3
  43. package/package.json +22 -20
  44. package/dist/dataset-DOXGRz50.js +0 -1535
  45. package/dist/env-utils-B7SGmoeJ.js +0 -21
  46. package/dist/schema-g4FQLr0V.d.ts +0 -433
  47. package/dist/src-CjEwiKXw.js +0 -2327
@@ -1,4 +1,6 @@
1
1
  import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
+ import "./src-Dbvuel6w.js";
3
+
2
4
  //#region ../../packages/substrate-policy/src/encoding.ts
3
5
  /**
4
6
  * Comma-separated string-list encoding. Spec §5.12.
@@ -388,18 +390,16 @@ function newlyGranted(prev, next) {
388
390
  * facing refusal message.
389
391
  */
390
392
  function newlyGrantedPairs(prev, next) {
391
- const covered = (ns, cap) => {
392
- const direct = prev[ns];
393
- if (Array.isArray(direct) && direct.includes(cap)) return true;
394
- const wild = prev["*"];
395
- return Array.isArray(wild) && wild.includes(cap);
396
- };
393
+ const effective = (grant, ns) => grant[ns] ?? grant["*"] ?? [];
397
394
  const byNs = /* @__PURE__ */ new Map();
398
- for (const [ns, caps] of Object.entries(next)) for (const cap of caps) {
399
- if (covered(ns, cap)) continue;
400
- const bucket = byNs.get(ns);
401
- if (bucket === void 0) byNs.set(ns, [cap]);
402
- else bucket.push(cap);
395
+ for (const ns of /* @__PURE__ */ new Set([...Object.keys(prev), ...Object.keys(next)])) {
396
+ const had = effective(prev, ns);
397
+ for (const cap of effective(next, ns)) {
398
+ if (had.includes(cap)) continue;
399
+ const bucket = byNs.get(ns);
400
+ if (bucket === void 0) byNs.set(ns, [cap]);
401
+ else bucket.push(cap);
402
+ }
403
403
  }
404
404
  return [...byNs.entries()].map(([ns, caps]) => `${ns}:${[...caps].sort().join("+")}`).sort();
405
405
  }
@@ -1,9 +1,9 @@
1
1
  import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
- import { B as ROW_FILTER_FIELD, V as matchesRowFilter, W as inFlightError, Y as unresolvedRow, Z as cosineSimilarity, et as memberKey } from "./plugin-BuQQEzL-.js";
3
- import { n as txScopeIdFor } from "./src-CjEwiKXw.js";
2
+ import { n as txScopeIdFor } from "./src-gz4I1F29.js";
3
+ import { B as ROW_FILTER_FIELD, V as matchesRowFilter, W as inFlightError, Y as unresolvedRow, Z as cosineSimilarity, et as memberKey } from "./plugin-DIYm2cZE.js";
4
4
 
5
5
  //#region ../../plugins/substrate-recall/package.json
6
- var version = "1.0.0-alpha.1";
6
+ var version = "1.0.0-alpha.11";
7
7
 
8
8
  //#endregion
9
9
  //#region ../../plugins/substrate-recall/src/in-memory-cluster-store.ts
@@ -68,7 +68,8 @@ var InMemoryClusterStore = class {
68
68
  return found === void 0 ? null : this.#copyCluster(found);
69
69
  }
70
70
  async listMembers(clusterId, opts = {}) {
71
- return this.#members.filter((m) => m.clusterId === clusterId).filter((m) => opts.includeDeparted === true || m.departedAt === null).map((m) => ({ ...m }));
71
+ const ids = new Set(typeof clusterId === "string" ? [clusterId] : clusterId);
72
+ return this.#members.filter((m) => ids.has(m.clusterId)).filter((m) => opts.includeDeparted === true || m.departedAt === null).map((m) => ({ ...m }));
72
73
  }
73
74
  async listAllMembers() {
74
75
  return this.#members.filter((m) => m.departedAt === null).map((m) => ({ ...m }));
@@ -195,6 +196,8 @@ var InMemoryEmbeddingsStore = class {
195
196
  async knnSearch(input) {
196
197
  const threshold = typeof input.threshold === "number" ? input.threshold : 0;
197
198
  const candidates = [];
199
+ const now = Date.now();
200
+ const halfLife = input.salienceHalfLifeHours;
198
201
  for (const entry of this.#entries.values()) {
199
202
  if (entry.rowNamespace !== input.namespace) continue;
200
203
  if (entry.rowType !== input.type) continue;
@@ -218,7 +221,7 @@ var InMemoryEmbeddingsStore = class {
218
221
  results.push({
219
222
  row,
220
223
  score,
221
- salience: entry.salience,
224
+ salience: halfLife !== void 0 && halfLife > 0 ? decayed(entry.salience, now - entry.salienceUpdatedAt.getTime(), halfLife) : entry.salience,
222
225
  ...input.returnVector === true ? { vector: Array.from(entry.vector) } : {}
223
226
  });
224
227
  }
@@ -227,14 +230,12 @@ var InMemoryEmbeddingsStore = class {
227
230
  async boostSalience(targets, halfLifeHours, bump = 1) {
228
231
  if (targets.length === 0) return [];
229
232
  const now = /* @__PURE__ */ new Date();
230
- const halfLifeMs = halfLifeHours * 3600 * 1e3;
231
233
  const applied = [];
232
234
  for (const t of targets) {
233
235
  const entry = this.#entries.get(key(t.namespace, t.type, t.id));
234
236
  if (entry === void 0) continue;
235
- const elapsedMs = Math.max(0, now.getTime() - entry.salienceUpdatedAt.getTime());
236
- const decay = Math.exp(-Math.LN2 * elapsedMs / halfLifeMs);
237
- entry.salience = entry.salience * decay + bump;
237
+ const elapsedMs = now.getTime() - entry.salienceUpdatedAt.getTime();
238
+ entry.salience = decayed(entry.salience, elapsedMs, halfLifeHours) + bump;
238
239
  entry.salienceUpdatedAt = now;
239
240
  applied.push({
240
241
  namespace: t.namespace,
@@ -297,6 +298,10 @@ var InMemoryEmbeddingsStore = class {
297
298
  return out;
298
299
  }
299
300
  };
301
+ /** `salience` after `elapsedMs` of exponential decay at `halfLifeHours`. */
302
+ function decayed(salience, elapsedMs, halfLifeHours) {
303
+ return salience * Math.exp(-Math.LN2 * Math.max(0, elapsedMs) / (halfLifeHours * 3600 * 1e3));
304
+ }
300
305
 
301
306
  //#endregion
302
307
  //#region ../../plugins/substrate-recall/src/in-memory-lexical-store.ts
@@ -476,7 +481,7 @@ async function recountClusters(tx, clusterIds, stamps) {
476
481
  WHERE c.id = counts.cluster_id
477
482
  `;
478
483
  }
479
- /** Distinct from the migrator's `substrate-migrations` key; hashed the same way. */
484
+ /** Distinct from the migrator's `substrate-migrations` key; scoped by schema the same way. */
480
485
  const FORMATION_LOCK_KEY = "substrate-recall-crystallise";
481
486
  var PgClusterStore = class {
482
487
  #sql;
@@ -487,7 +492,7 @@ var PgClusterStore = class {
487
492
  }
488
493
  /**
489
494
  * Runs `fn` holding a session advisory lock that excludes formation in every
490
- * process on this database; throws `crystallise_in_flight` if it is held.
495
+ * process on this schema; throws `crystallise_in_flight` if it is held.
491
496
  *
492
497
  * Lock and unlock share one reserved connection, since through the pool the
493
498
  * unlock can land on another session and fail to release.
@@ -496,13 +501,13 @@ var PgClusterStore = class {
496
501
  const conn = await this.#sql.reserve();
497
502
  try {
498
503
  const [row] = await conn`
499
- SELECT pg_try_advisory_lock(hashtext(${FORMATION_LOCK_KEY})) AS acquired
504
+ SELECT pg_try_advisory_lock(hashtext(${FORMATION_LOCK_KEY} || ':' || current_schema())) AS acquired
500
505
  `;
501
506
  if (row?.acquired !== true) throw inFlightError();
502
507
  try {
503
508
  return await fn();
504
509
  } finally {
505
- await conn`SELECT pg_advisory_unlock(hashtext(${FORMATION_LOCK_KEY}))`;
510
+ await conn`SELECT pg_advisory_unlock(hashtext(${FORMATION_LOCK_KEY} || ':' || current_schema()))`;
506
511
  }
507
512
  } finally {
508
513
  conn.release();
@@ -556,7 +561,7 @@ var PgClusterStore = class {
556
561
  const sql = this.#sql;
557
562
  return (await sql`
558
563
  SELECT * FROM recall_cluster_members
559
- WHERE cluster_id = ${clusterId}
564
+ WHERE cluster_id = ANY(${typeof clusterId === "string" ? [clusterId] : [...clusterId]}::text[])
560
565
  ${opts.includeDeparted === true ? sql`` : sql`AND departed_at IS NULL`}
561
566
  ORDER BY short_id ASC
562
567
  `).map(toMember);
@@ -729,6 +734,7 @@ var PgEmbeddingsStore = class {
729
734
  const k = clampK(input.k);
730
735
  const threshold = typeof input.threshold === "number" ? input.threshold : 0;
731
736
  const wantVector = input.returnVector === true;
737
+ const halfLife = input.salienceHalfLifeHours;
732
738
  return (await sql.begin(async (tx) => {
733
739
  await tx`SET LOCAL hnsw.iterative_scan = 'strict_order'`;
734
740
  await tx.unsafe(`SET LOCAL hnsw.ef_search = ${DEFAULT_HNSW_EF_SEARCH}`);
@@ -736,7 +742,7 @@ var PgEmbeddingsStore = class {
736
742
  SELECT
737
743
  ${tx.unsafe(ROW_BODY_SQL)} AS row,
738
744
  1 - (e.embedding <=> ${literal}::vector) AS score,
739
- e.salience AS salience
745
+ e.salience ${halfLife !== void 0 && halfLife > 0 ? tx`* exp(-ln(2) * GREATEST(0, EXTRACT(EPOCH FROM (now() - e.salience_updated_at))) / ${halfLife * 3600})` : tx``} AS salience
740
746
  ${wantVector ? tx`, e.embedding::text AS embedding` : tx``}
741
747
  FROM embeddings e
742
748
  JOIN rows r
@@ -864,6 +870,24 @@ var PgEmbeddingsStore = class {
864
870
  *
865
871
  * @returns number of orphaned embeddings deleted.
866
872
  */
873
+ async staleRowIds(input) {
874
+ return (await this.#sql`
875
+ SELECT r.id FROM rows r
876
+ WHERE r.namespace = ${input.namespace}
877
+ AND r.type = ${input.type}
878
+ AND r.deleted_at IS NULL
879
+ AND r.id > ${input.afterId}
880
+ AND NOT EXISTS (
881
+ SELECT 1 FROM embeddings e
882
+ WHERE e.row_namespace = r.namespace
883
+ AND e.row_type = r.type
884
+ AND e.row_id = r.id
885
+ AND (r.updated_at IS NULL OR e.updated_at >= r.updated_at)
886
+ )
887
+ ORDER BY r.id
888
+ LIMIT ${input.limit}
889
+ `).map((r) => r.id);
890
+ }
867
891
  async purgeOrphans() {
868
892
  return (await this.#sql`
869
893
  DELETE FROM embeddings e