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

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 (53) hide show
  1. package/README.md +64 -14
  2. package/dist/boot-store-Dz6PGwNY.js +4079 -0
  3. package/dist/{runtime-metrics-DwpDJ8vz.d.ts → bridge-BeVye_Oj.d.ts} +2 -87
  4. package/dist/cli-CH7M6ZTQ.js +3383 -0
  5. package/dist/client.browser.js +2485 -0
  6. package/dist/client.d.ts +273 -135
  7. package/dist/client.js +6 -6
  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-CoHLKAX0.d.ts} +44 -4
  12. package/dist/{fake-DUVtKVSE.js → fake-DdXtb-m8.js} +0 -1
  13. package/dist/{index-D7mSOFlM.d.ts → index-D7XRAGC6.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/pg-edges-store-Dnaxesmy.js +443 -0
  19. package/dist/{plugin-QuJN9pG_.js → plugin-BIz4Pr5-.js} +132 -8
  20. package/dist/proposals-bootstrap.js +6 -5
  21. package/dist/proposals.config.schema.json +5 -0
  22. package/dist/proposals.d.ts +17 -5
  23. package/dist/proposals.default-config.json +2 -1
  24. package/dist/proposals.js +2 -3
  25. package/dist/recall-bootstrap.js +5 -4
  26. package/dist/recall.config.schema.json +6 -0
  27. package/dist/recall.d.ts +35 -6
  28. package/dist/recall.js +2 -3
  29. package/dist/{repl-C52wyfMs.js → repl-BUkd9DPZ.js} +65 -33
  30. package/dist/{row-ByLFE2Wx.d.ts → row-aEWVHEsf.d.ts} +92 -12
  31. package/dist/run-Cu9w5Y10.js +2435 -0
  32. package/dist/{plugin-t2g2wX0b.d.ts → schema-DwnXxdjk.d.ts} +1293 -63
  33. package/dist/script-A9dxMRZA.js +143 -0
  34. package/dist/script-runner.js +77 -0
  35. package/dist/sdk.d.ts +227 -38
  36. package/dist/sdk.js +5 -110
  37. package/dist/semver-D_zdq0hi.js +17 -0
  38. package/dist/{src-DWm0iA88.js → src--accZ5Ka.js} +188 -87
  39. package/dist/{src-DMzc3HfW.js → src-BNdhH_Hx.js} +745 -851
  40. package/dist/{src-CG7giuHr.js → src-BQnDrs_E.js} +793 -75
  41. package/dist/{src-BLgbLq2k.js → src-B_kHkbsr.js} +780 -330
  42. package/dist/src-BbCUeMsI.js +7723 -0
  43. package/dist/{src-CHp3QyqB.js → src-BmJD4ScT.js} +9215 -3376
  44. package/dist/{row-DZ0Fnpc6.js → src-DmiUWOLp.js} +2 -1
  45. package/dist/substrate-admin.js +17 -782
  46. package/dist/substrate.js +4 -5487
  47. package/dist/testing.d.ts +730 -53
  48. package/dist/testing.js +20 -163
  49. package/dist/{ws-C72iVS5T.d.ts → ws-DJaKruX-.d.ts} +4 -5
  50. package/package.json +26 -26
  51. package/dist/dataset-BQ8egGl5.js +0 -1535
  52. package/dist/schema-g4FQLr0V.d.ts +0 -433
  53. package/dist/src-CXKnqeTW.js +0 -2339
package/dist/sdk.js CHANGED
@@ -1,111 +1,6 @@
1
- import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
- import { $ as error400, A as resolveIdentity, At as CLIENT_SUPPLIED_SERVER_FIELD, C as purgeResolver, Ct as ReservedField, D as deleteRow, Dt as isAuthCause, E as deleteResolver, Et as UnsupportedFilterError, M as resolveEntityRow, Mt as canonicalize, Nt as BootstrapConfigError, O as createResolver, Ot as isResolverFrameError, Pt as defineBootstrap, S as updateResolver, St as NamespaceMismatch, T as getResolver, Tt as RestrictViolation, Z as JSON_HEADERS, _ as field, _t as createKeyedMutex, at as WS_REASON_SERVER_RESTART, b as installAutoCrud, bt as BridgeUnreachableError, ct as normaliseVocabValues, dt as vocabImplication, et as jsonResponse, ft as RegistryBase, g as RESERVED_AUTOCRUD_FIELD_NAMES, gt as normaliseMetadataWhere, h as RESERVED_AUTOCRUD_FIELDS, ht as normaliseMetadataOrderBy, it as WS_CODE_SUBSCRIPTION_UNSUPPORTED_FRAME, j as resolveEntityId, jt as ROW_METADATA_FIELDS, k as UnattributedWriteError, kt as isRestrictViolation, lt as normaliseVocabWhere, m as defineNamespace, mt as METADATA_FILTER_FIELDS, ot as WS_REASON_SUBSCRIPTION_OVERLOADED, pt as METADATA_AUTHOR_FIELDS, st as canonicalVocabValue, tt as parseJsonBody, ut as storedVocabSpellings, v as autoCrudOptionsOf, vt as assertHardDeleteAllowed, w as listResolver, wt as ResolverFrameError, x as applyUpdatePatch, xt as MissingReferentError, y as createResolverWithEdges, yt as validateHardDeleteEnv } from "./src-CHp3QyqB.js";
3
- import { a as adminClaimCapabilities, c as makeTokenProvider, d as resolveDefaultKidPath, i as DEFAULT_CAPABILITIES, l as resolveAdminCredential, n as trimTrailingSlashes, o as isEncryptedKey, r as ADMIN_CAPABILITIES, s as makeAuthorizationProvider, t as parsePositiveInt, u as resolveDefaultKeyPath } from "./env-utils-CGNazUNt.js";
4
- import { A as expandFlat, C as not, D as isActiveAdminKeyRow, E as describeGrant, F as parseGrantEntries, I as decodeList, L as encodeList, M as grantEntryError, N as grantIsUniform, O as capabilitiesFor, P as intersectGrants, S as and, T as SYSTEM_NAMESPACE, _ as grantCovers, a as denyAll, b as newlyGrantedPairs, c as requireIdentity, d as confersAnywhere, f as coversNamespace, g as checkUpdatable, h as checkRevocable, i as allowVerbs, j as flattenGrant, k as encodeGrant, l as requireNamespace, m as grantedIn, n as row, o as denyVerbs, p as grantOf, r as allowAll, s as requireCapability, t as applyRowFilters, u as allCapabilities, v as grantEntriesConfer, w as or, x as revokedAtIso, y as newlyGranted } from "./row-DZ0Fnpc6.js";
5
- import { S as assertNever, b as formatQualifiedKey, x as parseQualifiedKey } from "./src-BLgbLq2k.js";
1
+ import { $t as naturalOrderOf, A as isReservedAutoCrudField, An as WALK_BUDGET_EXCEEDED, B as createResolver, Bn as assertNoRepeatedIds, Bt as parseJsonBody, C as makeCtx, Cn as STORE_BUSY, D as RESERVED_AUTOCRUD_FIELDS, Dn as WALKS_IN_SUBSCRIPTION, E as selectAll, En as WALKS_IN_MUTATION, F as createResolverWithEdges, Fn as isResolverFrameError, G as listResolver, Gt as WS_REASON_SUBSCRIPTION_OVERLOADED, H as updateResolver, Hn as BootstrapConfigError, I as installAutoCrud, In as isRestrictViolation, It as JSON_HEADERS, J as resolveIdentity, Jt as normaliseVocabWhere, K as getResolver, Kt as canonicalVocabValue, L as deleteManyResolver, Ln as DEFAULT_FILTER_WRITE_LIMITS, M as field, Mn as filterTooBroad, N as AutoCrudNameCollisionError, Nn as isAuthCause, O as RESERVED_AUTOCRUD_FIELD_NAMES, On as WALKS_NEED_PARTITIONED, P as autoCrudOptionsOf, Pn as isBatchElementError, Qt as isColumnField, R as updateManyResolver, Rn as CLIENT_SUPPLIED_SERVER_FIELD, Rt as error400, S as EngineQueryError, Sn as RestrictViolation, T as queryOne, Tn as VALUE_OUT_OF_RANGE, U as applyUpdatePatch, Un as defineBootstrap, Ut as WS_CODE_SUBSCRIPTION_UNSUPPORTED_FRAME, V as deleteResolver, Vn as canonicalize, W as deleteRow, Wt as WS_REASON_SERVER_RESTART, X as resolveEntityRow, Xt as vocabImplication, Y as resolveEntityId, Yt as storedVocabSpellings, Zt as DEFAULT_ORDERED_BY, _n as QUERY_TIMEOUT, an as normaliseWalkNode, b as assertOrderedBy, bn as ReservedField, cn as isIsoTimestamp, dn as validateHardDeleteEnv, en as RegistryBase, fn as BatchElementError, gn as NamespaceMismatch, hn as MissingReferentError, in as normaliseMetadataWhere, j as reservedMetadataFieldMessage, jn as WALK_UNSUPPORTED_REF, k as RESERVED_METADATA_FIELD_NAMES, kn as WALKS_UNSUPPORTED_STORE, ln as normalizeIsoTimestamp, mn as FILTER_TOO_BROAD, nn as METADATA_FILTER_FIELDS, on as createKeyedMutex, pn as BridgeUnreachableError, q as UnattributedWriteError, qt as normaliseVocabValues, rn as normaliseMetadataOrderBy, sn as isImpossibleIsoTimestamp, tn as METADATA_AUTHOR_FIELDS, un as assertHardDeleteAllowed, vn as REGEX_BUDGET_EXCEEDED, w as queryAll, wn as UnsupportedFilterError, x as defineNamespace, xn as ResolverFrameError, y as assertIndexDecl, yn as ROW_POLICY_BLOCKS_WALK, z as purgeResolver, zn as ROW_METADATA_FIELDS, zt as jsonResponse } from "./src-BmJD4ScT.js";
2
+ import { a as adminClaimCapabilities, c as makeTokenProvider, d as resolveDefaultKidPath, i as DEFAULT_CAPABILITIES, l as resolveAdminCredential, n as trimTrailingSlashes, o as isEncryptedKey, r as ADMIN_CAPABILITIES, s as makeAuthorizationProvider, t as parsePositiveInt, u as resolveDefaultKeyPath } from "./env-utils-s25Z1YSA.js";
3
+ import { A as expandFlat, C as not, D as isActiveAdminKeyRow, E as describeGrant, F as parseGrantEntries, I as decodeList, L as encodeList, M as grantEntryError, N as grantIsUniform, O as capabilitiesFor, P as intersectGrants, S as and, T as SYSTEM_NAMESPACE, _ as grantCovers, a as denyAll, b as newlyGrantedPairs, c as requireIdentity, d as confersAnywhere, f as coversNamespace, g as checkUpdatable, h as checkRevocable, i as allowVerbs, j as flattenGrant, k as encodeGrant, l as requireNamespace, m as grantedIn, n as row, o as denyVerbs, p as grantOf, r as allowAll, s as requireCapability, t as applyRowFilters, u as allCapabilities, v as grantEntriesConfer, w as or, x as revokedAtIso, y as newlyGranted } from "./src-DmiUWOLp.js";
4
+ import { B as EXPR_AGGREGATES, F as isInt32, H as EXPR_LIMITS, I as parseInt64, L as stringifyJson, M as formatQualifiedKey, N as parseQualifiedKey, P as isInt16, R as stringifyJsonExact, U as assertNever, V as EXPR_FUNCTIONS, z as DATE_UNITS } from "./src-B_kHkbsr.js";
6
5
 
7
- //#region ../../packages/substrate-sdk/src/engine-helpers.ts
8
- /**
9
- * Engine-call helpers for coordinator/plugin authors.
10
- *
11
- * Run an internal engine query or build a request context from inside a
12
- * plugin route or scheduled task, so coordinators share one implementation.
13
- *
14
- * Depends only on `@mnmal-ai/substrate-types`; this package must not take a
15
- * runtime dependency on `substrate-core`.
16
- */
17
- /**
18
- * Build a minimal `Partial<RequestContext>` for a plugin's engine call.
19
- *
20
- * PASS THE CALLER'S IDENTITY WHENEVER THERE IS ONE. Omitting it yields the
21
- * SYSTEM ACTOR (`identity: null`), which the engine's per-frame namespace gate
22
- * waves through unconditionally — correct for genuinely system-initiated work
23
- * (a scheduled sweep, a liveness probe), and a privilege escalation for
24
- * anything a caller asked for. A plugin route that omits it lets a caller reach
25
- * namespaces their grant does not cover, because the route gate checks a
26
- * capability without a namespace and the engine gate then checks a namespace
27
- * without an identity.
28
- *
29
- * The distinction is WHO ASKED, not which plugin.
30
- */
31
- function makeCtx(namespace, operation, identity = null) {
32
- return {
33
- identity,
34
- namespace,
35
- operation,
36
- now: /* @__PURE__ */ new Date(),
37
- headers: {},
38
- meta: {}
39
- };
40
- }
41
- /**
42
- * A frame the engine REFUSED, surfaced as a throw rather than as an empty result.
43
- *
44
- * Substrate returns a partial-success envelope: a rejected frame comes back with its
45
- * key ABSENT from `data` and the reason in `errors`. Without this, `queryOne` /
46
- * `queryAll` would render a refusal as `null` / `[]`, indistinguishable from a
47
- * query that matched nothing.
48
- */
49
- var EngineQueryError = class extends Error {
50
- name = "EngineQueryError";
51
- code;
52
- constructor(code, message) {
53
- super(message);
54
- this.code = code;
55
- }
56
- };
57
- /** Throw when the envelope carries an error, so a refusal cannot read as absence. */
58
- function assertNoFrameError(env, key) {
59
- const err = env.errors?.[0];
60
- if (err !== void 0) throw new EngineQueryError(err.code ?? "query_failed", err.message ?? `${key} was refused`);
61
- }
62
- /** Expand a list of field names into the `{ field: true }` selection map. */
63
- function selectAll(fields) {
64
- const out = {};
65
- for (const f of fields) out[f] = true;
66
- return out;
67
- }
68
- /** Accept either a field-name list or an already-built selection map. */
69
- function toSelection(fields) {
70
- return Array.isArray(fields) ? selectAll(fields) : fields;
71
- }
72
- /**
73
- * Run an internal engine query and return the row list (empty when the frame
74
- * resolves to nothing). `fields` may be a name list or a prebuilt selection map.
75
- */
76
- async function queryAll(engine, namespace, typeName, params, fields, identity = null) {
77
- const key = formatQualifiedKey(namespace, typeName);
78
- const env = await engine.query({
79
- frame: { [key]: {
80
- params,
81
- fields: toSelection(fields)
82
- } },
83
- ctx: makeCtx(namespace, "query", identity)
84
- });
85
- assertNoFrameError(env, key);
86
- const rows = env.data[key];
87
- if (rows === void 0 || rows === null) return [];
88
- return rows;
89
- }
90
- /**
91
- * Run an internal engine query expecting at most one row; returns the first
92
- * row or null. `fields` may be a name list or a prebuilt selection map.
93
- */
94
- async function queryOne(engine, namespace, typeName, params, fields, identity = null) {
95
- const key = formatQualifiedKey(namespace, typeName);
96
- const env = await engine.query({
97
- frame: { [key]: {
98
- params,
99
- fields: toSelection(fields)
100
- } },
101
- ctx: makeCtx(namespace, "query", identity)
102
- });
103
- assertNoFrameError(env, key);
104
- const rows = env.data[key];
105
- if (rows === void 0 || rows === null) return null;
106
- if (Array.isArray(rows)) return rows[0] ?? null;
107
- return rows;
108
- }
109
-
110
- //#endregion
111
- export { ADMIN_CAPABILITIES, BootstrapConfigError, BridgeUnreachableError, CLIENT_SUPPLIED_SERVER_FIELD, DEFAULT_CAPABILITIES, EngineQueryError, JSON_HEADERS, METADATA_AUTHOR_FIELDS, METADATA_FILTER_FIELDS, MissingReferentError, NamespaceMismatch, RESERVED_AUTOCRUD_FIELDS, RESERVED_AUTOCRUD_FIELD_NAMES, ROW_METADATA_FIELDS, RegistryBase, ReservedField, ResolverFrameError, RestrictViolation, SYSTEM_NAMESPACE, UnattributedWriteError, UnsupportedFilterError, WS_CODE_SUBSCRIPTION_UNSUPPORTED_FRAME, WS_REASON_SERVER_RESTART, WS_REASON_SUBSCRIPTION_OVERLOADED, adminClaimCapabilities, allCapabilities, allowAll, allowVerbs, and, applyRowFilters, applyUpdatePatch, assertHardDeleteAllowed, assertNever, autoCrudOptionsOf, canonicalVocabValue, canonicalize, capabilitiesFor, checkRevocable, checkUpdatable, confersAnywhere, coversNamespace, createKeyedMutex, createResolver, createResolverWithEdges, decodeList, defineBootstrap, defineNamespace, deleteResolver, deleteRow, denyAll, denyVerbs, describeGrant, encodeGrant, encodeList, error400, expandFlat, field, flattenGrant, formatQualifiedKey, getResolver, grantCovers, grantEntriesConfer, grantEntryError, grantIsUniform, grantOf, grantedIn, installAutoCrud, intersectGrants, isActiveAdminKeyRow, isAuthCause, isEncryptedKey, isResolverFrameError, isRestrictViolation, jsonResponse, listResolver, makeAuthorizationProvider, makeCtx, makeTokenProvider, newlyGranted, newlyGrantedPairs, normaliseMetadataOrderBy, normaliseMetadataWhere, normaliseVocabValues, normaliseVocabWhere, not, or, parseGrantEntries, parseJsonBody, parsePositiveInt, parseQualifiedKey, purgeResolver, queryAll, queryOne, requireCapability, requireIdentity, requireNamespace, resolveAdminCredential, resolveDefaultKeyPath, resolveDefaultKidPath, resolveEntityId, resolveEntityRow, resolveIdentity, revokedAtIso, row, selectAll, storedVocabSpellings, trimTrailingSlashes, updateResolver, validateHardDeleteEnv, vocabImplication };
6
+ export { ADMIN_CAPABILITIES, AutoCrudNameCollisionError, BatchElementError, BootstrapConfigError, BridgeUnreachableError, CLIENT_SUPPLIED_SERVER_FIELD, DATE_UNITS, DEFAULT_CAPABILITIES, DEFAULT_FILTER_WRITE_LIMITS, DEFAULT_ORDERED_BY, EXPR_AGGREGATES, EXPR_FUNCTIONS, EXPR_LIMITS, EngineQueryError, FILTER_TOO_BROAD, JSON_HEADERS, METADATA_AUTHOR_FIELDS, METADATA_FILTER_FIELDS, MissingReferentError, NamespaceMismatch, QUERY_TIMEOUT, REGEX_BUDGET_EXCEEDED, RESERVED_AUTOCRUD_FIELDS, RESERVED_AUTOCRUD_FIELD_NAMES, RESERVED_METADATA_FIELD_NAMES, ROW_METADATA_FIELDS, ROW_POLICY_BLOCKS_WALK, RegistryBase, ReservedField, ResolverFrameError, RestrictViolation, STORE_BUSY, SYSTEM_NAMESPACE, UnattributedWriteError, UnsupportedFilterError, VALUE_OUT_OF_RANGE, WALKS_IN_MUTATION, WALKS_IN_SUBSCRIPTION, WALKS_NEED_PARTITIONED, WALKS_UNSUPPORTED_STORE, WALK_BUDGET_EXCEEDED, WALK_UNSUPPORTED_REF, WS_CODE_SUBSCRIPTION_UNSUPPORTED_FRAME, WS_REASON_SERVER_RESTART, WS_REASON_SUBSCRIPTION_OVERLOADED, adminClaimCapabilities, allCapabilities, allowAll, allowVerbs, and, applyRowFilters, applyUpdatePatch, assertHardDeleteAllowed, assertIndexDecl, assertNever, assertNoRepeatedIds, assertOrderedBy, autoCrudOptionsOf, canonicalVocabValue, canonicalize, capabilitiesFor, checkRevocable, checkUpdatable, confersAnywhere, coversNamespace, createKeyedMutex, createResolver, createResolverWithEdges, decodeList, defineBootstrap, defineNamespace, deleteManyResolver, deleteResolver, deleteRow, denyAll, denyVerbs, describeGrant, encodeGrant, encodeList, error400, expandFlat, field, filterTooBroad, flattenGrant, formatQualifiedKey, getResolver, grantCovers, grantEntriesConfer, grantEntryError, grantIsUniform, grantOf, grantedIn, installAutoCrud, intersectGrants, isActiveAdminKeyRow, isAuthCause, isBatchElementError, isColumnField, isEncryptedKey, isImpossibleIsoTimestamp, isInt16, isInt32, isIsoTimestamp, isReservedAutoCrudField, isResolverFrameError, isRestrictViolation, jsonResponse, listResolver, makeAuthorizationProvider, makeCtx, makeTokenProvider, naturalOrderOf, newlyGranted, newlyGrantedPairs, normaliseMetadataOrderBy, normaliseMetadataWhere, normaliseVocabValues, normaliseVocabWhere, normaliseWalkNode, normalizeIsoTimestamp, not, or, parseGrantEntries, parseInt64, parseJsonBody, parsePositiveInt, parseQualifiedKey, purgeResolver, queryAll, queryOne, requireCapability, requireIdentity, requireNamespace, reservedMetadataFieldMessage, resolveAdminCredential, resolveDefaultKeyPath, resolveDefaultKidPath, resolveEntityId, resolveEntityRow, resolveIdentity, revokedAtIso, row, selectAll, storedVocabSpellings, stringifyJson, stringifyJsonExact, trimTrailingSlashes, updateManyResolver, updateResolver, validateHardDeleteEnv, vocabImplication };
@@ -0,0 +1,17 @@
1
+ import semver from "semver";
2
+
3
+ //#region ../../packages/substrate-compose/src/semver.ts
4
+ /**
5
+ * The one module that imports semver. The published bundle gives it a chunk of
6
+ * its own, so a consumer that never checks a plugin's version range drops
7
+ * semver, which does not declare itself free of load-time side effects.
8
+ */
9
+ function validRange(range) {
10
+ return semver.validRange(range);
11
+ }
12
+ function satisfies(version, range) {
13
+ return semver.satisfies(version, range);
14
+ }
15
+
16
+ //#endregion
17
+ export { validRange as n, satisfies as t };
@@ -1,9 +1,8 @@
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-QuJN9pG_.js";
3
- import { n as txScopeIdFor } from "./src-CXKnqeTW.js";
1
+ import { c as typeTableRef, l as whileNoTypeTable, o as guardedRefRead, r as txScopeIdFor } from "./src-BbCUeMsI.js";
2
+ import { B as ROW_FILTER_FIELD, V as matchesRowFilter, W as inFlightError, Y as unresolvedRow, Z as cosineSimilarity, et as memberKey } from "./plugin-BIz4Pr5-.js";
4
3
 
5
4
  //#region ../../plugins/substrate-recall/package.json
6
- var version = "1.0.0-alpha.2";
5
+ var version = "1.0.0-alpha.21";
7
6
 
8
7
  //#endregion
9
8
  //#region ../../plugins/substrate-recall/src/in-memory-cluster-store.ts
@@ -184,7 +183,7 @@ var InMemoryEmbeddingsStore = class {
184
183
  }
185
184
  /**
186
185
  * Materialize a row body via `datasetFor`, mirroring pg's join against
187
- * `rows`. Returns UNRESOLVED when no dataset is wired, and `null`
186
+ * the type's table. Returns UNRESOLVED when no dataset is wired, and `null`
188
187
  * when the row is missing or deleted so the caller can exclude it.
189
188
  */
190
189
  async #readRow(namespace, type, id) {
@@ -481,7 +480,7 @@ async function recountClusters(tx, clusterIds, stamps) {
481
480
  WHERE c.id = counts.cluster_id
482
481
  `;
483
482
  }
484
- /** Distinct from the migrator's `substrate-migrations` key; hashed the same way. */
483
+ /** Distinct from the migrator's `substrate-migrations` key; scoped by schema the same way. */
485
484
  const FORMATION_LOCK_KEY = "substrate-recall-crystallise";
486
485
  var PgClusterStore = class {
487
486
  #sql;
@@ -492,7 +491,7 @@ var PgClusterStore = class {
492
491
  }
493
492
  /**
494
493
  * Runs `fn` holding a session advisory lock that excludes formation in every
495
- * process on this database; throws `crystallise_in_flight` if it is held.
494
+ * process on this schema; throws `crystallise_in_flight` if it is held.
496
495
  *
497
496
  * Lock and unlock share one reserved connection, since through the pool the
498
497
  * unlock can land on another session and fail to release.
@@ -501,13 +500,13 @@ var PgClusterStore = class {
501
500
  const conn = await this.#sql.reserve();
502
501
  try {
503
502
  const [row] = await conn`
504
- SELECT pg_try_advisory_lock(hashtext(${FORMATION_LOCK_KEY})) AS acquired
503
+ SELECT pg_try_advisory_lock(hashtext(${FORMATION_LOCK_KEY} || ':' || current_schema())) AS acquired
505
504
  `;
506
505
  if (row?.acquired !== true) throw inFlightError();
507
506
  try {
508
507
  return await fn();
509
508
  } finally {
510
- await conn`SELECT pg_advisory_unlock(hashtext(${FORMATION_LOCK_KEY}))`;
509
+ await conn`SELECT pg_advisory_unlock(hashtext(${FORMATION_LOCK_KEY} || ':' || current_schema()))`;
511
510
  }
512
511
  } finally {
513
512
  conn.release();
@@ -638,22 +637,56 @@ var PgClusterStore = class {
638
637
 
639
638
  //#endregion
640
639
  //#region ../../plugins/substrate-recall/src/pg-row-filter.ts
641
- /** The row body, as the dataset read path returns it. */
640
+ /**
641
+ * SQL over the `r` alias shared by the pg recall stores: the relation `r`
642
+ * reads, the row body as the dataset read path returns it, and the row filter.
643
+ *
644
+ * `r` is a relation built from the type tables (`rowsFrom`), with the old
645
+ * `rows` table's columns (namespace, type, id, deleted_at, updated_at, body),
646
+ * so every statement reads it alike, and its `body` is the row `dataset.read`
647
+ * returns.
648
+ */
649
+ /** The row body over `r`, as the dataset read path returns it: `rowsFrom` names it `body`. */
642
650
  const ROW_BODY_SQL = "r.body";
651
+ /** {@link ScopedRef} of each `{ namespace, type }`. */
652
+ function scopedRefs(sql, scopes) {
653
+ return Promise.all(scopes.map(async ({ namespace, type }) => ({
654
+ namespace,
655
+ type,
656
+ ref: await typeTableRef(sql, namespace, type)
657
+ })));
658
+ }
659
+ const EMPTY_ROWS = "(SELECT NULL::text AS namespace, NULL::text AS type, NULL::text AS id, NULL::timestamptz AS deleted_at, NULL::timestamptz AS updated_at, NULL::jsonb AS body WHERE false)";
660
+ /**
661
+ * The relation the stores join as `r`: the scopes' type tables as one
662
+ * relation (namespace, type, id, deleted_at, updated_at, body). `wide` (one scope only) adds the
663
+ * type table's own columns, which `rowFilterSql` reads through `ref.text`.
664
+ */
665
+ function rowsFrom(sql, scoped, wide = false) {
666
+ const present = scoped.flatMap((s) => s.ref === void 0 ? [] : [{
667
+ ...s,
668
+ ref: s.ref
669
+ }]);
670
+ if (present.length === 0) return sql.unsafe(EMPTY_ROWS);
671
+ const columns = wide ? "x.*" : "x.id, x.deleted_at, x.updated_at";
672
+ let union = sql``;
673
+ for (const [i, { namespace, type, ref }] of present.entries()) union = sql`${union}${sql.unsafe(i === 0 ? "" : " UNION ALL ")}SELECT ${namespace}::text AS namespace, ${type}::text AS type, ${sql.unsafe(`${columns}, ${ref.rowJson("x")} AS body FROM ${ref.from} x`)}`;
674
+ return sql`(${union})`;
675
+ }
643
676
  /**
644
677
  * The row filter as `AND …` clauses, for appending to a WHERE. Every
645
- * comparison is as text against the body, the same semantics as
646
- * `matchesRowFilter`. Field names are re-checked
647
- * against ROW_FILTER_FIELD and values are bound, so nothing caller-supplied is
648
- * spliced into SQL.
678
+ * comparison is as text against the row, the same semantics as
679
+ * `matchesRowFilter`: `ref.text`, over the columns a `wide` `rowsFrom` exposes. Field names
680
+ * are re-checked against ROW_FILTER_FIELD and values are bound, so nothing
681
+ * caller-supplied is spliced into SQL unquoted.
649
682
  */
650
- function rowFilterSql(tx, filter) {
683
+ function rowFilterSql(tx, ref, filter) {
651
684
  if (filter === void 0) return tx``;
652
685
  let out = tx``;
653
686
  for (const [field, pred] of Object.entries(filter)) {
654
687
  if (!ROW_FILTER_FIELD.test(field)) throw new Error(`row filter field '${field}' is not a plain identifier`);
655
- const text = tx`(r.body ->> ${field})`;
656
- const isNull = tx`(r.body ->> ${field}) IS NULL`;
688
+ const text = tx.unsafe(ref.text("r", field));
689
+ const isNull = tx.unsafe(`${ref.text("r", field)} IS NULL`);
657
690
  const values = pred.in ?? [pred.eq ?? null];
658
691
  const strings = values.filter((v) => v !== null).map((v) => String(v));
659
692
  const wantsNull = values.some((v) => v === null);
@@ -662,6 +695,37 @@ function rowFilterSql(tx, filter) {
662
695
  }
663
696
  return out;
664
697
  }
698
+ /**
699
+ * `purgeOrphans` on `tables`: once per distinct type in `table` (`embeddings`
700
+ * or `lexical`), deletes the entries whose row is missing or tombstoned in its
701
+ * type table. A type with no table deletes all of its entries.
702
+ *
703
+ * @returns the number of entries deleted.
704
+ */
705
+ async function purgeTypeTableOrphans(sql, table) {
706
+ const types = await sql`
707
+ SELECT DISTINCT row_namespace AS namespace, row_type AS type FROM ${sql(table)}`;
708
+ let deleted = 0;
709
+ for (const scope of await scopedRefs(sql, types)) {
710
+ if (scope.ref === void 0) {
711
+ const result = await whileNoTypeTable(sql, scope.namespace, scope.type, (tx) => tx`DELETE FROM ${tx(table)}
712
+ WHERE row_namespace = ${scope.namespace} AND row_type = ${scope.type}`);
713
+ deleted += result?.count ?? 0;
714
+ continue;
715
+ }
716
+ const result = await sql`
717
+ DELETE FROM ${sql(table)} o
718
+ WHERE o.row_namespace = ${scope.namespace}
719
+ AND o.row_type = ${scope.type}
720
+ AND NOT EXISTS (
721
+ SELECT 1 FROM ${rowsFrom(sql, [scope])} r
722
+ WHERE r.id = o.row_id AND r.deleted_at IS NULL
723
+ )
724
+ `;
725
+ deleted += result.count;
726
+ }
727
+ return deleted;
728
+ }
665
729
 
666
730
  //#endregion
667
731
  //#region ../../plugins/substrate-recall/src/pg-embeddings-store.ts
@@ -735,17 +799,22 @@ var PgEmbeddingsStore = class {
735
799
  const threshold = typeof input.threshold === "number" ? input.threshold : 0;
736
800
  const wantVector = input.returnVector === true;
737
801
  const halfLife = input.salienceHalfLifeHours;
738
- return (await sql.begin(async (tx) => {
802
+ return (await guardedRefRead(sql, input.namespace, input.type, (ref, guard) => sql.begin(async (tx) => {
803
+ const scoped = [{
804
+ namespace: input.namespace,
805
+ type: input.type,
806
+ ref
807
+ }];
739
808
  await tx`SET LOCAL hnsw.iterative_scan = 'strict_order'`;
740
809
  await tx.unsafe(`SET LOCAL hnsw.ef_search = ${DEFAULT_HNSW_EF_SEARCH}`);
741
- return tx`
810
+ return guard(tx, tx`
742
811
  SELECT
743
812
  ${tx.unsafe(ROW_BODY_SQL)} AS row,
744
813
  1 - (e.embedding <=> ${literal}::vector) AS score,
745
814
  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
746
815
  ${wantVector ? tx`, e.embedding::text AS embedding` : tx``}
747
816
  FROM embeddings e
748
- JOIN rows r
817
+ JOIN ${rowsFrom(tx, scoped, true)} r
749
818
  ON r.namespace = e.row_namespace
750
819
  AND r.type = e.row_type
751
820
  AND r.id = e.row_id
@@ -754,11 +823,11 @@ var PgEmbeddingsStore = class {
754
823
  AND e.model = ${input.model}
755
824
  AND r.deleted_at IS NULL
756
825
  AND 1 - (e.embedding <=> ${literal}::vector) >= ${threshold}
757
- ${rowFilterSql(tx, input.rowFilter)}
826
+ ${rowFilterSql(tx, ref, input.rowFilter)}
758
827
  ORDER BY e.embedding <=> ${literal}::vector
759
828
  LIMIT ${k}
760
- `;
761
- })).map((r) => ({
829
+ `);
830
+ }), "row")).map((r) => ({
762
831
  row: r.row,
763
832
  score: r.score,
764
833
  salience: r.salience,
@@ -795,21 +864,28 @@ var PgEmbeddingsStore = class {
795
864
  async fetchEmbeddings(ids, model) {
796
865
  if (ids.length === 0) return [];
797
866
  const sql = this.#sql;
798
- return (await sql`
867
+ const byType = /* @__PURE__ */ new Map();
868
+ for (const id of ids) {
869
+ const key = `${id.namespace}/${id.type}`;
870
+ byType.set(key, [...byType.get(key) ?? [], id]);
871
+ }
872
+ const scoped = await scopedRefs(sql, [...byType.values()].map(([first]) => first));
873
+ const fetch = (part, from) => sql`
799
874
  SELECT e.row_namespace, e.row_type, e.row_id, e.embedding::text AS embedding,
800
875
  ${sql.unsafe(ROW_BODY_SQL)} AS body, e.salience
801
- FROM (VALUES ${sql(ids.map((id) => [
876
+ FROM (VALUES ${sql(part.map((id) => [
802
877
  id.namespace,
803
878
  id.type,
804
879
  id.id
805
880
  ]))}) AS t(ns, ty, id)
806
881
  JOIN embeddings e
807
882
  ON e.row_namespace = t.ns AND e.row_type = t.ty AND e.row_id = t.id
808
- JOIN rows r
883
+ JOIN ${from} r
809
884
  ON r.namespace = e.row_namespace AND r.type = e.row_type AND r.id = e.row_id
810
885
  WHERE e.model = ${model}
811
886
  AND r.deleted_at IS NULL
812
- `).map((r) => ({
887
+ `;
888
+ return (await Promise.all(scoped.filter((s) => s.ref !== void 0).map((s) => fetch(byType.get(`${s.namespace}/${s.type}`) ?? [], rowsFrom(sql, [s]))))).flat().map((r) => ({
813
889
  namespace: r.row_namespace,
814
890
  type: r.row_type,
815
891
  id: r.row_id,
@@ -820,40 +896,39 @@ var PgEmbeddingsStore = class {
820
896
  }
821
897
  async selectCandidates(selector, model) {
822
898
  const sql = this.#sql;
823
- const namespaces = selector.scopes.map((s) => s.namespace);
824
- const types = selector.scopes.map((s) => s.type);
825
- const exclusionFields = selector.excludeTextPrefixes.map((e) => e.field);
826
- const exclusionPrefixes = selector.excludeTextPrefixes.map((e) => e.prefix);
899
+ const arms = (await scopedRefs(sql, selector.scopes)).flatMap((s) => s.ref === void 0 ? [] : [{
900
+ ...s,
901
+ ref: s.ref
902
+ }]);
903
+ if (arms.length === 0) return [];
904
+ let kept = sql``;
905
+ for (const [i, { namespace, type, ref }] of arms.entries()) {
906
+ let keep = sql`x.deleted_at IS NULL`;
907
+ for (const { field, prefix } of selector.excludeTextPrefixes) keep = sql`${keep} AND NOT COALESCE(
908
+ jsonb_typeof(${sql.unsafe(ref.json("x", field))}) IN ('string', 'number', 'boolean')
909
+ AND starts_with(${sql.unsafe(ref.text("x", field))}, ${prefix}::text), false)`;
910
+ kept = sql`${kept}${sql.unsafe(i === 0 ? "" : " UNION ALL ")}SELECT ${namespace}::text AS namespace, ${type}::text AS type, x.id FROM ${sql.unsafe(ref.from)} x WHERE ${keep}`;
911
+ }
827
912
  return (await sql`
828
- SELECT e.row_namespace, e.row_type, e.row_id, e.embedding::text AS embedding,
829
- ${sql.unsafe(ROW_BODY_SQL)} AS body
830
- FROM embeddings e
831
- JOIN rows r
832
- ON r.namespace = e.row_namespace
833
- AND r.type = e.row_type
834
- AND r.id = e.row_id
835
- JOIN unnest(${namespaces}::text[], ${types}::text[]) AS scope(ns, type)
836
- ON scope.ns = e.row_namespace AND scope.type = e.row_type
837
- WHERE e.model = ${model}
838
- AND r.deleted_at IS NULL
839
- AND NOT EXISTS (
840
- SELECT 1
841
- FROM unnest(${exclusionFields}::text[], ${exclusionPrefixes}::text[]) AS ex(field, prefix)
842
- -- starts_with(), not LIKE: a caller-supplied prefix must be matched
843
- -- literally. LIKE treats '_' and '%' in the prefix as wildcards, so
844
- -- a prefix like 'perf_seed' would also exclude 'perfXseed…' — the
845
- -- in-memory side (String(value).startsWith(...)) never had that
846
- -- hazard, so the two implementations silently disagreed. Restricted
847
- -- to scalar JSON values (jsonb_typeof) to match the in-memory side,
848
- -- which coerces only string/number/boolean via String(value) — a
849
- -- composite (object/array) has no agreed-on text rendering between
850
- -- pg's jsonb ->> and JS's String(), so both sides now treat a
851
- -- composite as never-excluded rather than risk disagreeing.
852
- WHERE jsonb_typeof(${sql.unsafe(ROW_BODY_SQL)} -> ex.field) IN ('string', 'number', 'boolean')
853
- AND starts_with(${sql.unsafe(ROW_BODY_SQL)} ->> ex.field, ex.prefix)
854
- )
855
- ORDER BY e.row_namespace COLLATE "C" ASC, e.row_type COLLATE "C" ASC, e.row_id COLLATE "C" ASC
856
- LIMIT ${selector.limit}
913
+ WITH picked AS (
914
+ SELECT e.row_namespace, e.row_type, e.row_id
915
+ FROM embeddings e
916
+ JOIN (${kept}) k
917
+ ON k.namespace = e.row_namespace AND k.type = e.row_type AND k.id = e.row_id
918
+ WHERE e.model = ${model}
919
+ ORDER BY e.row_namespace COLLATE "C" ASC, e.row_type COLLATE "C" ASC, e.row_id COLLATE "C" ASC
920
+ LIMIT ${selector.limit}
921
+ )
922
+ SELECT p.row_namespace, p.row_type, p.row_id, e.embedding::text AS embedding, b.body
923
+ FROM picked p
924
+ JOIN embeddings e
925
+ ON e.row_namespace = p.row_namespace AND e.row_type = p.row_type AND e.row_id = p.row_id
926
+ CROSS JOIN LATERAL (
927
+ SELECT ${sql.unsafe(ROW_BODY_SQL)} AS body FROM ${rowsFrom(sql, arms)} r
928
+ WHERE r.namespace = p.row_namespace AND r.type = p.row_type AND r.id = p.row_id
929
+ OFFSET 0
930
+ ) b
931
+ ORDER BY p.row_namespace COLLATE "C" ASC, p.row_type COLLATE "C" ASC, p.row_id COLLATE "C" ASC
857
932
  `).map((r) => ({
858
933
  namespace: r.row_namespace,
859
934
  type: r.row_type,
@@ -862,30 +937,54 @@ var PgEmbeddingsStore = class {
862
937
  row: r.body
863
938
  }));
864
939
  }
940
+ async staleRowIds(input) {
941
+ const sql = this.#sql;
942
+ const ref = await typeTableRef(sql, input.namespace, input.type);
943
+ if (ref === void 0) return [];
944
+ return (await sql`
945
+ SELECT r.id FROM ${rowsFrom(sql, [{
946
+ namespace: input.namespace,
947
+ type: input.type,
948
+ ref
949
+ }])} r
950
+ WHERE r.namespace = ${input.namespace}
951
+ AND r.type = ${input.type}
952
+ AND r.deleted_at IS NULL
953
+ AND r.id > ${input.afterId}
954
+ AND NOT EXISTS (
955
+ SELECT 1 FROM embeddings e
956
+ WHERE e.row_namespace = r.namespace
957
+ AND e.row_type = r.type
958
+ AND e.row_id = r.id
959
+ AND (r.updated_at IS NULL OR e.updated_at >= r.updated_at)
960
+ )
961
+ ORDER BY r.id
962
+ LIMIT ${input.limit}
963
+ `).map((r) => r.id);
964
+ }
865
965
  /**
866
966
  * Deletes every embedding whose backing row is missing or soft-deleted.
867
- * Needed because `embeddings` has no FK to `rows` and no
967
+ * Needed because `embeddings` has no FK to the type tables and no
868
968
  * dataset delete path touches it; tombstones are terminal, so soft-deleted
869
969
  * rows are safe to collect immediately. Run as a sweep stage by the binary.
870
970
  *
871
971
  * @returns number of orphaned embeddings deleted.
872
972
  */
873
973
  async purgeOrphans() {
874
- return (await this.#sql`
875
- DELETE FROM embeddings e
876
- WHERE NOT EXISTS (
877
- SELECT 1 FROM rows r
878
- WHERE r.namespace = e.row_namespace
879
- AND r.type = e.row_type
880
- AND r.id = e.row_id
881
- AND r.deleted_at IS NULL
882
- )
883
- `).count;
974
+ return purgeTypeTableOrphans(this.#sql, "embeddings");
884
975
  }
885
976
  };
886
977
 
887
978
  //#endregion
888
979
  //#region ../../plugins/substrate-recall/src/pg-lexical-store.ts
980
+ /**
981
+ * Postgres-backed `LexicalStore` — `lexical` + `ts_rank` over its GIN
982
+ * index. Counterpart to `PgEmbeddingsStore`.
983
+ *
984
+ * Search joins the type's rows the same way the KNN path does, with the same
985
+ * `deleted_at IS NULL` predicate, so a deleted row cannot surface through this
986
+ * arm after it has stopped surfacing through the dense one.
987
+ */
889
988
  /** Hard ceiling on `k`, mirroring the embeddings store's cap. */
890
989
  const MAX_K = 100;
891
990
  var PgLexicalStore = class {
@@ -915,27 +1014,24 @@ var PgLexicalStore = class {
915
1014
  /**
916
1015
  * Deletes every lexical row whose backing row is missing or soft-deleted,
917
1016
  * as `PgEmbeddingsStore.purgeOrphans` does for vectors: `lexical` has no FK
918
- * to `rows`, so a drop, purge or tombstone would otherwise leave the text.
1017
+ * to the type tables, so a drop, purge or tombstone would otherwise leave the text.
919
1018
  *
920
1019
  * @returns number of orphaned lexical rows deleted.
921
1020
  */
922
1021
  async purgeOrphans() {
923
- return (await this.#sql`
924
- DELETE FROM lexical l
925
- WHERE NOT EXISTS (
926
- SELECT 1 FROM rows r
927
- WHERE r.namespace = l.row_namespace
928
- AND r.type = l.row_type
929
- AND r.id = l.row_id
930
- AND r.deleted_at IS NULL
931
- )
932
- `).count;
1022
+ return purgeTypeTableOrphans(this.#sql, "lexical");
933
1023
  }
934
1024
  async countIndexed(namespace, type) {
1025
+ const ref = await typeTableRef(this.#sql, namespace, type);
1026
+ if (ref === void 0) return 0;
935
1027
  const rows = await this.#sql`
936
1028
  SELECT COUNT(*) AS n
937
1029
  FROM lexical l
938
- JOIN rows r
1030
+ JOIN ${rowsFrom(this.#sql, [{
1031
+ namespace,
1032
+ type,
1033
+ ref
1034
+ }])} r
939
1035
  ON r.namespace = l.row_namespace
940
1036
  AND r.type = l.row_type
941
1037
  AND r.id = l.row_id
@@ -961,6 +1057,7 @@ var PgLexicalStore = class {
961
1057
  if (strict.length > 0) return strict;
962
1058
  return this.#run(input, k, true);
963
1059
  }
1060
+ /** One pass, under the type table's read guard, so a promotion meanwhile cannot drop a field or a hit. */
964
1061
  async #run(input, k, degrade) {
965
1062
  const query = degrade ? this.#sql`(
966
1063
  SELECT CASE
@@ -969,7 +1066,7 @@ var PgLexicalStore = class {
969
1066
  END AS query
970
1067
  FROM (SELECT websearch_to_tsquery(${this.#tsConfig}, ${input.query})::text AS t) s
971
1068
  )` : this.#sql`websearch_to_tsquery(${this.#tsConfig}, ${input.query})`;
972
- return (await this.#sql`
1069
+ return (await guardedRefRead(this.#sql, input.namespace, input.type, (ref, guard) => guard(this.#sql, this.#sql`
973
1070
  SELECT
974
1071
  l.row_namespace,
975
1072
  l.row_type,
@@ -978,7 +1075,11 @@ var PgLexicalStore = class {
978
1075
  ts_rank(l.tsv, q.query) AS score
979
1076
  FROM lexical l
980
1077
  CROSS JOIN ${query} AS q(query)
981
- JOIN rows r
1078
+ JOIN ${rowsFrom(this.#sql, [{
1079
+ namespace: input.namespace,
1080
+ type: input.type,
1081
+ ref
1082
+ }], true)} r
982
1083
  ON r.namespace = l.row_namespace
983
1084
  AND r.type = l.row_type
984
1085
  AND r.id = l.row_id
@@ -987,10 +1088,10 @@ var PgLexicalStore = class {
987
1088
  AND r.deleted_at IS NULL
988
1089
  AND q.query IS NOT NULL
989
1090
  AND l.tsv @@ q.query
990
- ${rowFilterSql(this.#sql, input.rowFilter)}
1091
+ ${rowFilterSql(this.#sql, ref, input.rowFilter)}
991
1092
  ORDER BY score DESC, l.row_id ASC
992
1093
  LIMIT ${k}
993
- `).map((r) => ({
1094
+ `), "row_id")).map((r) => ({
994
1095
  rowNamespace: r.row_namespace,
995
1096
  rowType: r.row_type,
996
1097
  rowId: r.row_id,