@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 CHANGED
@@ -11,7 +11,7 @@ It runs as a real install: the binary and its plugins resolve from one shared `n
11
11
  | `@mnmal-ai/substrate/sdk` | Plugin authoring: `defineNamespace`, the field DSL, autoCRUD, engine helpers, `defineBootstrap`, policy combinators, route helpers, and all shared types |
12
12
  | `@mnmal-ai/substrate/client` | `SubstrateClient`, `watchRows`, client errors, the admin API, and the token and grant helpers |
13
13
  | `@mnmal-ai/substrate/pack` | The MessagePack wire codec |
14
- | `@mnmal-ai/substrate/testing` | `compose`, `MemorySubstrateServer`, `MemoryDataset`, `MemoryEdgesStore`, `loadPlugins`, `FakeEmbedder`, and the Postgres test-database guards |
14
+ | `@mnmal-ai/substrate/testing` | `compose`, `MemorySubstrateServer`, `MemoryDataset`, `MemoryEdgesStore`, `loadPlugins`, `FakeEmbedder`, `PgDataset`, `PgEdgesStore`, the pg migration runners, and the Postgres test-database guards |
15
15
  | `@mnmal-ai/substrate/recall` | The recall plugin's primitives (`EmbeddingsStore`, `LexicalStore`, `embedAndStore`, …) |
16
16
  | `@mnmal-ai/substrate/proposals` | The proposals plugin's exports |
17
17
 
package/dist/client.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
- import { a as makeAuthorizationProvider, c as resolveDefaultKeyPath, i as isEncryptedKey, l as resolveDefaultKidPath, n as DEFAULT_CAPABILITIES, o as makeTokenProvider, r as adminClaimCapabilities, s as resolveAdminCredential, t as ADMIN_CAPABILITIES } from "./agent-token-BIboWOU0.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, g as checkUpdatable, h as checkRevocable, i as allowVerbs, j as flattenGrant, k as encodeGrant, l as requireNamespace, n as row, o as denyVerbs, r as allowAll, s as requireCapability, t as applyRowFilters, v as grantEntriesConfer, w as or, x as revokedAtIso, y as newlyGranted } from "./row-DK7lPZVe.js";
4
- import { g as SchemaDriftError } from "./src-C4DzaIQZ.js";
5
- import { S as reconcileIdentities, _ as currentStep, a as createDefaultDiagnosticsSink, b as DIAGNOSE_CHECKS, c as AuthError, d as AdminBackfillError, f as BACKFILL_CHUNK, g as STEP_MARKS, h as SKIPPABLE_STEPS, i as collectDiagnostics, l as NetworkError, m as ONBOARD_STEPS, n as tokenAudience, o as reduceSubscribeRows, p as CURRENT_MARK, r as SubstrateClient, s as watchRows, t as noRedirectFetch, u as SubscribeError, v as planOnboarding, x as diagnose, y as requiredOutstanding } from "./src-DuopV8u7.js";
2
+ import { a as adminClaimCapabilities, c as makeTokenProvider, d as resolveDefaultKidPath, i as DEFAULT_CAPABILITIES, l as resolveAdminCredential, o as isEncryptedKey, r as ADMIN_CAPABILITIES, s as makeAuthorizationProvider, u as resolveDefaultKeyPath } from "./env-utils-CGNazUNt.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, g as checkUpdatable, h as checkRevocable, i as allowVerbs, j as flattenGrant, k as encodeGrant, l as requireNamespace, n as row, o as denyVerbs, r as allowAll, s as requireCapability, t as applyRowFilters, v as grantEntriesConfer, w as or, x as revokedAtIso, y as newlyGranted } from "./row-DZ0Fnpc6.js";
4
+ import { g as SchemaDriftError } from "./src-BLgbLq2k.js";
5
+ import { S as reconcileIdentities, _ as currentStep, a as createDefaultDiagnosticsSink, b as DIAGNOSE_CHECKS, c as AuthError, d as AdminBackfillError, f as BACKFILL_CHUNK, g as STEP_MARKS, h as SKIPPABLE_STEPS, i as collectDiagnostics, l as NetworkError, m as ONBOARD_STEPS, n as tokenAudience, o as reduceSubscribeRows, p as CURRENT_MARK, r as SubstrateClient, s as watchRows, t as noRedirectFetch, u as SubscribeError, v as planOnboarding, x as diagnose, y as requiredOutstanding } from "./src-DMzc3HfW.js";
6
6
 
7
7
  export { ADMIN_CAPABILITIES, AdminBackfillError, AuthError, BACKFILL_CHUNK, CURRENT_MARK, DEFAULT_CAPABILITIES, DIAGNOSE_CHECKS, NetworkError, ONBOARD_STEPS, SKIPPABLE_STEPS, STEP_MARKS, SYSTEM_NAMESPACE, SchemaDriftError, SubscribeError, SubstrateClient, adminClaimCapabilities, allowAll, allowVerbs, and, applyRowFilters, capabilitiesFor, checkRevocable, checkUpdatable, collectDiagnostics, createDefaultDiagnosticsSink, currentStep, decodeList, denyAll, denyVerbs, describeGrant, diagnose, encodeGrant, encodeList, expandFlat, flattenGrant, grantCovers, grantEntriesConfer, grantEntryError, grantIsUniform, intersectGrants, isActiveAdminKeyRow, isEncryptedKey, makeAuthorizationProvider, makeTokenProvider, newlyGranted, newlyGrantedPairs, noRedirectFetch, not, or, parseGrantEntries, planOnboarding, reconcileIdentities, reduceSubscribeRows, requireCapability, requireIdentity, requireNamespace, requiredOutstanding, resolveAdminCredential, resolveDefaultKeyPath, resolveDefaultKidPath, revokedAtIso, row, tokenAudience, watchRows };
@@ -1,9 +1,9 @@
1
1
  import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
- import { d as buildAuditRow, f as AUDIT_EVENT_TYPE } from "./src-YXlv_RyL.js";
3
- import { a as makeAuthorizationProvider, c as resolveDefaultKeyPath, l as resolveDefaultKidPath, r as adminClaimCapabilities, s as resolveAdminCredential, t as ADMIN_CAPABILITIES } from "./agent-token-BIboWOU0.js";
4
- import { D as isActiveAdminKeyRow, E as describeGrant, L as encodeList, M as grantEntryError, T as SYSTEM_NAMESPACE, v as grantEntriesConfer } from "./row-DK7lPZVe.js";
5
- import { _ as currentStep, b as DIAGNOSE_CHECKS, g as STEP_MARKS, h as SKIPPABLE_STEPS, n as tokenAudience, p as CURRENT_MARK, v as planOnboarding, x as diagnose, y as requiredOutstanding } from "./src-DuopV8u7.js";
6
- import { i as pendingMigrations, o as runMigrations, t as PgDataset } from "./src-CjEwiKXw.js";
2
+ import { d as buildAuditRow, f as AUDIT_EVENT_TYPE } from "./src-CHp3QyqB.js";
3
+ import { a as adminClaimCapabilities, d as resolveDefaultKidPath, l as resolveAdminCredential, n as trimTrailingSlashes, r as ADMIN_CAPABILITIES, s as makeAuthorizationProvider, u as resolveDefaultKeyPath } from "./env-utils-CGNazUNt.js";
4
+ import { D as isActiveAdminKeyRow, E as describeGrant, L as encodeList, M as grantEntryError, T as SYSTEM_NAMESPACE, v as grantEntriesConfer } from "./row-DZ0Fnpc6.js";
5
+ import { _ as currentStep, b as DIAGNOSE_CHECKS, g as STEP_MARKS, h as SKIPPABLE_STEPS, n as tokenAudience, p as CURRENT_MARK, v as planOnboarding, x as diagnose, y as requiredOutstanding } from "./src-DMzc3HfW.js";
6
+ import { i as pendingMigrations, o as runMigrations, t as PgDataset } from "./src-CXKnqeTW.js";
7
7
  import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
8
8
  import { homedir } from "node:os";
9
9
  import { dirname, join } from "node:path";
@@ -11,7 +11,7 @@ import { createHash, createPublicKey, generateKeyPairSync, randomUUID } from "no
11
11
  import postgres from "postgres";
12
12
 
13
13
  //#region package.json
14
- var version = "1.0.0-alpha.1";
14
+ var version = "1.0.0-alpha.3";
15
15
 
16
16
  //#endregion
17
17
  //#region src/admin/errors.ts
@@ -535,7 +535,7 @@ function readEd25519PublicKey(path) {
535
535
  async function runKeyPatchViaRoute(body, baseUrl, env, exitCodes, fetchImpl = fetch) {
536
536
  const auth = resolveAdminAuth(env, baseUrl);
537
537
  if ("error" in auth) throw new AdminCliError(`${body.revoke === true ? "revoke" : "update"}: ${auth.error}`, 1);
538
- const url = `${baseUrl.replace(/\/+$/, "")}/substrate/v1/_system/keys`;
538
+ const url = `${trimTrailingSlashes(baseUrl)}/substrate/v1/_system/keys`;
539
539
  let res;
540
540
  try {
541
541
  res = await fetchImpl(url, {
@@ -598,7 +598,7 @@ const UPDATE_EXIT_CODES = {
598
598
  async function runKeyListViaRoute(baseUrl, env, fetchImpl = fetch) {
599
599
  const auth = resolveAdminAuth(env, baseUrl);
600
600
  if ("error" in auth) throw new AdminCliError(`list: ${auth.error}`, 1);
601
- const base = `${baseUrl.replace(/\/+$/, "")}/substrate/v1/_system/keys`;
601
+ const base = `${trimTrailingSlashes(baseUrl)}/substrate/v1/_system/keys`;
602
602
  const keys = [];
603
603
  let cursor;
604
604
  for (let page = 0; page < 50; page += 1) {
@@ -1185,7 +1185,7 @@ async function runRegisterViaRoute(args, baseUrl, env, fetchImpl = fetch) {
1185
1185
  ...Object.keys(directoryParams).length > 0 ? { params: directoryParams } : {}
1186
1186
  }
1187
1187
  };
1188
- const url = `${baseUrl.replace(/\/+$/, "")}/substrate/v1/_system/keys`;
1188
+ const url = `${trimTrailingSlashes(baseUrl)}/substrate/v1/_system/keys`;
1189
1189
  let res;
1190
1190
  try {
1191
1191
  res = await fetchImpl(url, {
@@ -111,7 +111,7 @@ function adminClaimCapabilities(env) {
111
111
  return custom === void 0 || custom.length === 0 || ADMIN_CAPABILITIES.includes(custom) ? ADMIN_CAPABILITIES : [...ADMIN_CAPABILITIES, custom];
112
112
  }
113
113
  function b64url(buf) {
114
- return buf.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
114
+ return buf.toString("base64url");
115
115
  }
116
116
  function sign$1(kid, key, ns, capabilities, aud) {
117
117
  const now = Math.floor(Date.now() / 1e3);
@@ -197,4 +197,33 @@ function makeAuthorizationProvider(cred, opts = {}) {
197
197
  }
198
198
 
199
199
  //#endregion
200
- export { makeAuthorizationProvider as a, resolveDefaultKeyPath as c, isEncryptedKey as i, resolveDefaultKidPath as l, DEFAULT_CAPABILITIES as n, makeTokenProvider as o, adminClaimCapabilities as r, resolveAdminCredential as s, ADMIN_CAPABILITIES as t };
200
+ //#region ../../packages/substrate-types/src/env-utils.ts
201
+ /**
202
+ * Pure env-string parsing helpers shared by plugins and deployment helpers
203
+ * (e.g. `substrate-sentinel-client`) without a substrate-sdk runtime dep.
204
+ */
205
+ /**
206
+ * Strict positive-integer env parsing. Returns `fallback` when `raw` is
207
+ * undefined (env var unset). Throws with `envVar` in the message when `raw`
208
+ * is set but not a positive integer — never falls back on invalid input, so
209
+ * a config typo fails at boot.
210
+ */
211
+ function parsePositiveInt(raw, envVar, fallback) {
212
+ if (raw === void 0) return fallback;
213
+ const n = Number(raw);
214
+ if (!Number.isFinite(n) || !Number.isInteger(n) || n <= 0) throw new Error(`${envVar} must be a positive integer (got '${raw}')`);
215
+ return n;
216
+ }
217
+ /**
218
+ * `url` with its trailing slashes removed, for joining a configured base URL
219
+ * to a path. A loop rather than `/\/+$/`, which backtracks quadratically on a
220
+ * long run of slashes that does not end the string.
221
+ */
222
+ function trimTrailingSlashes(url) {
223
+ let end = url.length;
224
+ while (end > 0 && url.charCodeAt(end - 1) === 47) end--;
225
+ return url.slice(0, end);
226
+ }
227
+
228
+ //#endregion
229
+ export { adminClaimCapabilities as a, makeTokenProvider as c, resolveDefaultKidPath as d, DEFAULT_CAPABILITIES as i, resolveAdminCredential as l, trimTrailingSlashes as n, isEncryptedKey as o, ADMIN_CAPABILITIES as r, makeAuthorizationProvider as s, parsePositiveInt as t, resolveDefaultKeyPath as u };
package/dist/pack.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
- import { _ as decodeEnvelope, a as buildDecodePlan, c as isPlainRequestData, d as PackEncodeError, f as encodeEnvelope, g as SchemaDriftError, h as PackDecodeError, i as applyDecodePlanInPlace, l as decodeStreamChunk, m as encodePositionalFromFieldDef, n as packFrame, o as decodeWsEnvelope, p as encodePositional, r as unpackFrame, s as encodeWsFrame, t as WS_SUBPROTOCOL_SUBSTRATE_PACK, u as encodeStreamChunk, v as decodePositional, y as decodePositionalFromFieldDef } from "./src-C4DzaIQZ.js";
2
+ import { _ as decodeEnvelope, a as buildDecodePlan, c as isPlainRequestData, d as PackEncodeError, f as encodeEnvelope, g as SchemaDriftError, h as PackDecodeError, i as applyDecodePlanInPlace, l as decodeStreamChunk, m as encodePositionalFromFieldDef, n as packFrame, o as decodeWsEnvelope, p as encodePositional, r as unpackFrame, s as encodeWsFrame, t as WS_SUBPROTOCOL_SUBSTRATE_PACK, u as encodeStreamChunk, v as decodePositional, y as decodePositionalFromFieldDef } from "./src-BLgbLq2k.js";
3
3
  import { n as acceptsPack, t as PACK_MIME } from "./content-type-C7T28xNb.js";
4
4
 
5
5
  export { PACK_MIME, PackDecodeError, PackEncodeError, SchemaDriftError, WS_SUBPROTOCOL_SUBSTRATE_PACK, acceptsPack, applyDecodePlanInPlace, buildDecodePlan, decodeEnvelope, decodePositional, decodePositionalFromFieldDef, decodeStreamChunk, decodeWsEnvelope, encodeEnvelope, encodePositional, encodePositionalFromFieldDef, encodeStreamChunk, encodeWsFrame, isPlainRequestData, packFrame, unpackFrame };
@@ -0,0 +1,381 @@
1
+ import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
+ import { n as txScopeIdFor } from "./src-CXKnqeTW.js";
3
+ import { randomUUID } from "node:crypto";
4
+
5
+ //#region ../../packages/substrate-dataset-pg/src/pg-edges-store.ts
6
+ /**
7
+ * PgEdgesStore — PostgreSQL-backed reference implementation of EdgesStore.
8
+ * Spec: §4.6, §6.2, §6.3.
9
+ *
10
+ * One table (`edges`) keyed by a text PK, no namespace scoping — edges
11
+ * are engine-scoped (§4.6), not per-ns. The caller owns the `postgres` Sql
12
+ * connection; this class never opens or closes pools.
13
+ *
14
+ * Edge ids are `crypto.randomUUID()`: unique, so cursor pagination works, but
15
+ * not time-ordered. The sweep walks the whole table, so it doesn't need order.
16
+ *
17
+ * Edge.from/to .id is `string | number`, stored and read back as text (unlike
18
+ * PgDataset, which preserves numeric ids).
19
+ *
20
+ * SQL safety: postgres.js tagged templates parameterise every value slot.
21
+ * Never string-concat caller input — the `update` dynamic column list uses a
22
+ * fixed allowlist of column names, not user-supplied keys.
23
+ */
24
+ const MUTABLE_PATCH_FIELDS = /* @__PURE__ */ new Set([
25
+ "id",
26
+ "lastSweptAt",
27
+ "blocked"
28
+ ]);
29
+ /**
30
+ * Batch size for chunked admin deletes (`dropEdgesByNamespace`,
31
+ * `dropEdgesByType`): bounds per-statement lock hold on large tables.
32
+ */
33
+ const DEFAULT_DROP_BATCH_SIZE = 5e3;
34
+ const VALID_POLICIES = /* @__PURE__ */ new Set([
35
+ "set_null",
36
+ "cascade",
37
+ "restrict",
38
+ "orphan"
39
+ ]);
40
+ const refToId = (id) => typeof id === "number" ? String(id) : id;
41
+ var PgEdgesStore = class PgEdgesStore {
42
+ #sql;
43
+ #now;
44
+ #inTx;
45
+ #dropBatchSize;
46
+ txScopeId;
47
+ get #edgeCols() {
48
+ return this.#sql`id, from_ns, from_type, from_id, from_bridged, to_ns, to_type, to_id, to_bridged, field_name, policy, created_at, deleted_at, last_swept_at, blocked`;
49
+ }
50
+ constructor(options) {
51
+ this.#sql = options.sql;
52
+ this.#now = options.now ?? (() => /* @__PURE__ */ new Date());
53
+ this.#inTx = "inTx" in options ? options.inTx : false;
54
+ const explicit = "txScopeId" in options ? options.txScopeId : void 0;
55
+ this.txScopeId = explicit ?? txScopeIdFor(options.sql);
56
+ const batch = options.dropBatchSize;
57
+ if (batch !== void 0 && (!Number.isFinite(batch) || batch <= 0)) throw new Error(`PgEdgesStore: dropBatchSize must be a positive finite number, got ${batch}`);
58
+ this.#dropBatchSize = batch ?? DEFAULT_DROP_BATCH_SIZE;
59
+ }
60
+ async write(input) {
61
+ const id = randomUUID();
62
+ const createdAt = this.#now();
63
+ const fromBridged = input.from.bridged === true;
64
+ const toBridged = input.to.bridged === true;
65
+ await this.#sql`
66
+ INSERT INTO edges (
67
+ id, from_ns, from_type, from_id, from_bridged,
68
+ to_ns, to_type, to_id, to_bridged,
69
+ field_name, policy, created_at, deleted_at, last_swept_at, blocked
70
+ )
71
+ VALUES (
72
+ ${id},
73
+ ${input.from.ns}, ${input.from.type}, ${refToId(input.from.id)}, ${fromBridged},
74
+ ${input.to.ns}, ${input.to.type}, ${refToId(input.to.id)}, ${toBridged},
75
+ ${input.fieldName}, ${input.policy}, ${createdAt},
76
+ NULL, NULL, false
77
+ )
78
+ `;
79
+ return {
80
+ id,
81
+ from: buildRef(input.from.ns, input.from.type, refToId(input.from.id), fromBridged),
82
+ to: buildRef(input.to.ns, input.to.type, refToId(input.to.id), toBridged),
83
+ fieldName: input.fieldName,
84
+ policy: input.policy,
85
+ createdAt
86
+ };
87
+ }
88
+ async tombstone(id, at) {
89
+ const stamp = at ?? this.#now();
90
+ await this.#sql`
91
+ UPDATE edges
92
+ SET deleted_at = ${stamp}
93
+ WHERE id = ${id}
94
+ AND deleted_at IS NULL
95
+ `;
96
+ }
97
+ /**
98
+ * Hard-deletes tombstoned edges whose `deleted_at` is older than
99
+ * `olderThan`. Called by the sweep loop. Returns the number deleted.
100
+ */
101
+ async purgeTombstonedEdges(opts) {
102
+ return { deleted: (await this.#sql`
103
+ DELETE FROM edges
104
+ WHERE deleted_at IS NOT NULL
105
+ AND deleted_at < ${opts.olderThan}
106
+ RETURNING id
107
+ `).length };
108
+ }
109
+ async update(patch) {
110
+ for (const key of Object.keys(patch)) if (!MUTABLE_PATCH_FIELDS.has(key)) throw new Error(`EdgesStore.update: field '${key}' is immutable`);
111
+ const hasLastSweptAt = Object.hasOwn(patch, "lastSweptAt");
112
+ const hasBlocked = Object.hasOwn(patch, "blocked");
113
+ if (!hasLastSweptAt && !hasBlocked) return;
114
+ if (hasLastSweptAt && hasBlocked) {
115
+ await this.#sql`
116
+ UPDATE edges
117
+ SET last_swept_at = ${patch.lastSweptAt ?? null},
118
+ blocked = ${patch.blocked ?? false}
119
+ WHERE id = ${patch.id}
120
+ `;
121
+ return;
122
+ }
123
+ if (hasLastSweptAt) {
124
+ await this.#sql`
125
+ UPDATE edges
126
+ SET last_swept_at = ${patch.lastSweptAt ?? null}
127
+ WHERE id = ${patch.id}
128
+ `;
129
+ return;
130
+ }
131
+ await this.#sql`
132
+ UPDATE edges
133
+ SET blocked = ${patch.blocked ?? false}
134
+ WHERE id = ${patch.id}
135
+ `;
136
+ }
137
+ async readNextBatch(cursorId, size) {
138
+ const edges = (cursorId === null ? await this.#sql`
139
+ SELECT ${this.#edgeCols}
140
+ FROM edges
141
+ ORDER BY id ASC
142
+ LIMIT ${size}
143
+ ` : await this.#sql`
144
+ SELECT ${this.#edgeCols}
145
+ FROM edges
146
+ WHERE id > ${cursorId}
147
+ ORDER BY id ASC
148
+ LIMIT ${size}
149
+ `).map(rowToEdge);
150
+ const last = edges[edges.length - 1];
151
+ return {
152
+ edges,
153
+ lastId: last ? last.id : null
154
+ };
155
+ }
156
+ async findEdgesTo(ref) {
157
+ return (await this.#sql`
158
+ SELECT ${this.#edgeCols}
159
+ FROM edges
160
+ WHERE to_ns = ${ref.ns}
161
+ AND to_type = ${ref.type}
162
+ AND to_id = ${refToId(ref.id)}
163
+ AND deleted_at IS NULL
164
+ `).map(rowToEdge);
165
+ }
166
+ async findEdgesToMany(refs) {
167
+ if (refs.length === 0) return [];
168
+ const sql = this.#sql;
169
+ const seenRef = /* @__PURE__ */ new Set();
170
+ const toNs = [];
171
+ const toType = [];
172
+ const toId = [];
173
+ for (const r of refs) {
174
+ const id = refToId(r.id);
175
+ const k = `${r.ns}\u0000${r.type}\u0000${id}`;
176
+ if (seenRef.has(k)) continue;
177
+ seenRef.add(k);
178
+ toNs.push(r.ns);
179
+ toType.push(r.type);
180
+ toId.push(id);
181
+ }
182
+ return (await sql`
183
+ SELECT ${this.#edgeCols}
184
+ FROM edges
185
+ JOIN unnest(${toNs}::text[], ${toType}::text[], ${toId}::text[])
186
+ AS seek(seek_ns, seek_type, seek_id)
187
+ ON to_ns = seek.seek_ns AND to_type = seek.seek_type AND to_id = seek.seek_id
188
+ WHERE deleted_at IS NULL
189
+ `).map(rowToEdge);
190
+ }
191
+ async findEdgesFrom(ref) {
192
+ return (await this.#sql`
193
+ SELECT ${this.#edgeCols}
194
+ FROM edges
195
+ WHERE from_ns = ${ref.ns}
196
+ AND from_type = ${ref.type}
197
+ AND from_id = ${refToId(ref.id)}
198
+ AND deleted_at IS NULL
199
+ `).map(rowToEdge);
200
+ }
201
+ async findEdgesTouchingNamespace(namespace, limit) {
202
+ return (await this.#sql`
203
+ SELECT ${this.#edgeCols}
204
+ FROM edges
205
+ WHERE deleted_at IS NULL
206
+ AND (from_ns = ${namespace} OR to_ns = ${namespace})
207
+ ORDER BY id ASC
208
+ LIMIT ${limit}
209
+ `).map(rowToEdge);
210
+ }
211
+ async findEdgesTouchingType(namespace, type, limit) {
212
+ return (await this.#sql`
213
+ SELECT ${this.#edgeCols}
214
+ FROM edges
215
+ WHERE deleted_at IS NULL
216
+ AND ((from_ns = ${namespace} AND from_type = ${type})
217
+ OR (to_ns = ${namespace} AND to_type = ${type}))
218
+ ORDER BY id ASC
219
+ LIMIT ${limit}
220
+ `).map(rowToEdge);
221
+ }
222
+ async namespacesLinkedTo(namespace, type) {
223
+ const fromMatch = type === void 0 ? this.#sql`from_ns = ${namespace}` : this.#sql`(from_ns = ${namespace} AND from_type = ${type})`;
224
+ const toMatch = type === void 0 ? this.#sql`to_ns = ${namespace}` : this.#sql`(to_ns = ${namespace} AND to_type = ${type})`;
225
+ return (await this.#sql`
226
+ SELECT ns FROM (
227
+ SELECT to_ns AS ns FROM edges WHERE ${fromMatch}
228
+ UNION
229
+ SELECT from_ns AS ns FROM edges WHERE ${toMatch}
230
+ ) linked
231
+ WHERE ns <> ${namespace}
232
+ ORDER BY ns
233
+ `).map((r) => r.ns);
234
+ }
235
+ /**
236
+ * Chunked physical delete of every edge touching `namespace`, at most
237
+ * `dropBatchSize` rows per statement, so a large delete does not hold every
238
+ * row lock at once. Returns the number deleted.
239
+ *
240
+ * Outside a tx each batch auto-commits. Inside a caller's tx the batches
241
+ * stay in it, so a throw still rolls back the whole drop.
242
+ */
243
+ async dropEdgesByNamespace(namespace, within) {
244
+ const batchSize = this.#dropBatchSize;
245
+ const farEnd = this.#farEndWithin(namespace, within, this.#sql`from_ns = ${namespace}`, this.#sql`to_ns = ${namespace}`);
246
+ let total = 0;
247
+ while (true) {
248
+ const rows = await this.#sql`
249
+ DELETE FROM edges
250
+ WHERE id IN (
251
+ SELECT id FROM edges
252
+ WHERE (from_ns = ${namespace} OR to_ns = ${namespace}) ${farEnd}
253
+ LIMIT ${batchSize}
254
+ )
255
+ RETURNING id
256
+ `;
257
+ total += rows.length;
258
+ if (rows.length < batchSize) return total;
259
+ await this.#yieldBetweenBatches();
260
+ }
261
+ }
262
+ /**
263
+ * Chunked physical delete of every edge touching `(namespace, type)`.
264
+ * See {@link dropEdgesByNamespace} for the lock-hold + tx semantics.
265
+ */
266
+ async dropEdgesByType(namespace, type, within) {
267
+ const batchSize = this.#dropBatchSize;
268
+ const fromMatch = this.#sql`(from_ns = ${namespace} AND from_type = ${type})`;
269
+ const toMatch = this.#sql`(to_ns = ${namespace} AND to_type = ${type})`;
270
+ const farEnd = this.#farEndWithin(namespace, within, fromMatch, toMatch);
271
+ let total = 0;
272
+ while (true) {
273
+ const rows = await this.#sql`
274
+ DELETE FROM edges
275
+ WHERE id IN (
276
+ SELECT id FROM edges
277
+ WHERE (${fromMatch} OR ${toMatch}) ${farEnd}
278
+ LIMIT ${batchSize}
279
+ )
280
+ RETURNING id
281
+ `;
282
+ total += rows.length;
283
+ if (rows.length < batchSize) return total;
284
+ await this.#yieldBetweenBatches();
285
+ }
286
+ }
287
+ /** `AND` clause keeping only edges whose far end is `namespace` or in `within`. */
288
+ #farEndWithin(namespace, within, fromMatch, toMatch) {
289
+ if (within === void 0) return this.#sql``;
290
+ const allowed = this.#sql([namespace, ...within]);
291
+ return this.#sql`AND ((${fromMatch} AND to_ns IN ${allowed}) OR (${toMatch} AND from_ns IN ${allowed}))`;
292
+ }
293
+ /**
294
+ * Yields to the event loop between drop batches outside a tx; no-op inside one.
295
+ */
296
+ async #yieldBetweenBatches() {
297
+ if (!this.#inTx) await new Promise((resolve) => setImmediate(resolve));
298
+ }
299
+ async canDelete(ref) {
300
+ const rows = await this.#sql`
301
+ SELECT ${this.#edgeCols}
302
+ FROM edges
303
+ WHERE to_ns = ${ref.ns}
304
+ AND to_type = ${ref.type}
305
+ AND to_id = ${refToId(ref.id)}
306
+ AND policy = 'restrict'
307
+ AND deleted_at IS NULL
308
+ `;
309
+ if (rows.length === 0) return { ok: true };
310
+ return {
311
+ ok: false,
312
+ blockedBy: rows.map(rowToEdge)
313
+ };
314
+ }
315
+ async findBlocked() {
316
+ return (await this.#sql`
317
+ SELECT ${this.#edgeCols}
318
+ FROM edges
319
+ WHERE blocked = true
320
+ AND deleted_at IS NULL
321
+ `).map(rowToEdge);
322
+ }
323
+ /**
324
+ * `sql.begin`-backed atomicity. Same flattening pattern as PgDataset —
325
+ * nested re-entry skips the inner begin and runs on the existing tx handle
326
+ * so an inner throw propagates to the outer `sql.begin` for rollback.
327
+ */
328
+ async withTransaction(fn) {
329
+ if (this.#inTx) return fn(this);
330
+ return this.#sql.begin(async (txSql) => {
331
+ return fn(new PgEdgesStore({
332
+ sql: txSql,
333
+ now: this.#now,
334
+ inTx: true,
335
+ txScopeId: this.txScopeId,
336
+ dropBatchSize: this.#dropBatchSize
337
+ }));
338
+ });
339
+ }
340
+ /**
341
+ * Engine-only. Returns a tx-bound clone on the supplied `TransactionSql`,
342
+ * for `PgDataset.withScopedTransaction`. The caller has verified
343
+ * the txScopeIds match.
344
+ */
345
+ _enlistInTx(txSql) {
346
+ return new PgEdgesStore({
347
+ sql: txSql,
348
+ now: this.#now,
349
+ inTx: true,
350
+ txScopeId: this.txScopeId,
351
+ dropBatchSize: this.#dropBatchSize
352
+ });
353
+ }
354
+ };
355
+ function buildRef(ns, type, id, bridged) {
356
+ const ref = {
357
+ ns,
358
+ type,
359
+ id
360
+ };
361
+ if (bridged) ref.bridged = true;
362
+ return ref;
363
+ }
364
+ function rowToEdge(row) {
365
+ if (!VALID_POLICIES.has(row.policy)) throw new Error(`PgEdgesStore: unknown policy '${row.policy}' on edge id=${row.id}`);
366
+ const edge = {
367
+ id: row.id,
368
+ from: buildRef(row.from_ns, row.from_type, row.from_id, row.from_bridged),
369
+ to: buildRef(row.to_ns, row.to_type, row.to_id, row.to_bridged),
370
+ fieldName: row.field_name,
371
+ policy: row.policy,
372
+ createdAt: row.created_at
373
+ };
374
+ if (row.deleted_at !== null) edge.deletedAt = row.deleted_at;
375
+ if (row.last_swept_at !== null) edge.lastSweptAt = row.last_swept_at;
376
+ if (row.blocked) edge.blocked = true;
377
+ return edge;
378
+ }
379
+
380
+ //#endregion
381
+ export { PgEdgesStore as t };
@@ -1,5 +1,5 @@
1
1
  import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
- import { L as applyObligations, Q as buildContext, et as jsonResponse, wt as ResolverFrameError } from "./src-YXlv_RyL.js";
2
+ import { L as applyObligations, Q as buildContext, et as jsonResponse, wt as ResolverFrameError } from "./src-CHp3QyqB.js";
3
3
  import { Worker } from "node:worker_threads";
4
4
 
5
5
  //#region ../../plugins/substrate-recall/src/candidate-selection.ts
@@ -951,6 +951,7 @@ async function handleRecallRequest(input, deps) {
951
951
  }
952
952
  const type = input.type;
953
953
  if (type === void 0 || type === "") throw Object.assign(/* @__PURE__ */ new Error("recall: pass `type`, the type to search. There is no default."), { code: "bad_request" });
954
+ if (typeof input.query !== "string" || input.query.trim() === "") throw Object.assign(/* @__PURE__ */ new Error("recall: pass `query`, the text to search for."), { code: "bad_request" });
954
955
  if (input.query.length > 8192) throw Object.assign(/* @__PURE__ */ new Error(`recall: query is longer than ${MAX_RECALL_QUERY_CHARS} characters`), { code: "bad_request" });
955
956
  const k = clamp(input.k ?? 10, 1, 100);
956
957
  const threshold = typeof input.threshold === "number" ? input.threshold : 0;
@@ -976,7 +977,8 @@ async function handleRecallRequest(input, deps) {
976
977
  type,
977
978
  model: embedder.modelId,
978
979
  vector,
979
- k
980
+ k,
981
+ salienceHalfLifeHours: salience.halfLifeHours
980
982
  };
981
983
  if (threshold > 0) knnInput.threshold = threshold;
982
984
  if (returnVector) knnInput.returnVector = true;
@@ -1502,14 +1504,35 @@ function rethrowCoded$1(err) {
1502
1504
  * answers both visibility and counts; `check` is cached per scope.
1503
1505
  */
1504
1506
  async function resolveClusterVisibility(record, deps, ctx) {
1505
- if (deps.checkReadPolicy === void 0) return {
1507
+ if (deps.checkReadPolicy === void 0) return storedVisibility(record);
1508
+ const members = await deps.clusters.listMembers(record.id, { includeDeparted: true });
1509
+ return visibilityOf(members, await readableMembers(members, deps, ctx));
1510
+ }
1511
+ /**
1512
+ * `resolveClusterVisibility` for a whole listing: one member read and one
1513
+ * readability check across every cluster, rather than two per cluster.
1514
+ */
1515
+ async function resolveListVisibility(records, deps, ctx) {
1516
+ if (deps.checkReadPolicy === void 0 || records.length === 0) return records.map(storedVisibility);
1517
+ const members = await deps.clusters.listMembers(records.map((r) => r.id), { includeDeparted: true });
1518
+ const readable = await readableMembers(members, deps, ctx);
1519
+ const byCluster = /* @__PURE__ */ new Map();
1520
+ for (const m of members) {
1521
+ const list = byCluster.get(m.clusterId);
1522
+ if (list === void 0) byCluster.set(m.clusterId, [m]);
1523
+ else list.push(m);
1524
+ }
1525
+ return records.map((r) => visibilityOf(byCluster.get(r.id) ?? [], readable));
1526
+ }
1527
+ function storedVisibility(record) {
1528
+ return {
1506
1529
  visible: true,
1507
1530
  memberCount: record.memberCount,
1508
1531
  departedCount: record.departedCount,
1509
1532
  labelReadable: true
1510
1533
  };
1511
- const members = await deps.clusters.listMembers(record.id, { includeDeparted: true });
1512
- const readable = await readableMembers(members, deps, ctx);
1534
+ }
1535
+ function visibilityOf(members, readable) {
1513
1536
  let liveTotal = 0;
1514
1537
  let memberCount = 0;
1515
1538
  let departedCount = 0;
@@ -1559,10 +1582,11 @@ function buildClusterSurface(deps) {
1559
1582
  return [project(clusterRow(one, vis), args.fields)];
1560
1583
  }
1561
1584
  const rows = await deps.clusters.listClusters(p.includeRetired === true ? { includeRetired: true } : {});
1585
+ const visibility = await resolveListVisibility(rows, deps, ctx);
1562
1586
  const visible = [];
1563
- for (const c of rows) {
1564
- const vis = await resolveClusterVisibility(c, deps, ctx);
1565
- if (vis.visible) visible.push(project(clusterRow(c, vis), args.fields));
1587
+ for (const [i, c] of rows.entries()) {
1588
+ const vis = visibility[i];
1589
+ if (vis?.visible === true) visible.push(project(clusterRow(c, vis), args.fields));
1566
1590
  }
1567
1591
  return visible;
1568
1592
  };
@@ -1739,7 +1763,7 @@ async function embedRowReporting(row, opts) {
1739
1763
  if (opts.serializeRow) source = await opts.serializeRow(row);
1740
1764
  else if (opts.fields && opts.fields.length > 0) source = opts.fields.map((f) => row[f]).filter((v) => typeof v === "string" && v.length > 0).join("\n");
1741
1765
  else source = "";
1742
- if (source === null || source === void 0) {
1766
+ if (source === null || source === void 0 || source.trim() === "") {
1743
1767
  await opts.store.delete({
1744
1768
  rowNamespace: opts.namespace,
1745
1769
  rowType: opts.type,
@@ -3594,8 +3618,9 @@ function makeSpreadResolver(registry, edges) {
3594
3618
  //#region ../../plugins/substrate-recall/src/plugin.ts
3595
3619
  const DEFAULT_CLUSTER_DIMENSION = 768;
3596
3620
  const DEFAULT_QUERY_EMBED_TIMEOUT_MS = 3e4;
3621
+ const DEFAULT_WRITE_EMBED_TIMEOUT_MS = 3e4;
3597
3622
  /** `embedder` with `timeoutMs` applied to every call that does not set its own. */
3598
- function withQueryTimeout(embedder, timeoutMs) {
3623
+ function withEmbedTimeout(embedder, timeoutMs) {
3599
3624
  return {
3600
3625
  modelId: embedder.modelId,
3601
3626
  dimension: embedder.dimension,
@@ -3634,7 +3659,7 @@ function makeEmbedSourceContext(resolveDataset) {
3634
3659
  }
3635
3660
  function createRecallPlugin(cfg) {
3636
3661
  const stats = cfg.stats ?? createEmbedStats();
3637
- const queryEmbedder = cfg.embedder === void 0 ? void 0 : withQueryTimeout(cfg.embedder, cfg.queryEmbedTimeoutMs ?? 3e4);
3662
+ const queryEmbedder = cfg.embedder === void 0 ? void 0 : withEmbedTimeout(cfg.embedder, cfg.queryEmbedTimeoutMs ?? 3e4);
3638
3663
  const spreadRegistry = new SpreadEdgeRegistry();
3639
3664
  const embedSourceRegistry = new EmbedSourceRegistry();
3640
3665
  const lexicalSourceRegistry = new LexicalSourceRegistry();
@@ -3826,7 +3851,10 @@ function createRecallPlugin(cfg) {
3826
3851
  stats,
3827
3852
  ...embedSourceContext !== void 0 ? { embedSourceContext } : {}
3828
3853
  };
3829
- const hook = makeEmbedPostWrite(binding);
3854
+ const hook = makeEmbedPostWrite({
3855
+ ...binding,
3856
+ embedder: withEmbedTimeout(binding.embedder, cfg.writeEmbedTimeoutMs ?? 3e4)
3857
+ });
3830
3858
  out[typeName] = composeHooks(out[typeName], hook);
3831
3859
  embedSourceRegistry.add({
3832
3860
  namespace: namespace.namespace,
@@ -1,6 +1,6 @@
1
1
  import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
- import { Pt as defineBootstrap } from "./src-YXlv_RyL.js";
3
- import { n as createNoopPubSub, t as createProposalsPlugin } from "./src-5hVbeiAu.js";
2
+ import { Pt as defineBootstrap } from "./src-CHp3QyqB.js";
3
+ import { n as createNoopPubSub, t as createProposalsPlugin } from "./src-CG7giuHr.js";
4
4
  import { z } from "zod";
5
5
 
6
6
  //#region ../../plugins/substrate-proposals/src/bootstrap.ts
package/dist/proposals.js CHANGED
@@ -1,4 +1,4 @@
1
1
  import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
- import { n as createNoopPubSub, r as ProposalError, t as createProposalsPlugin } from "./src-5hVbeiAu.js";
2
+ import { n as createNoopPubSub, r as ProposalError, t as createProposalsPlugin } from "./src-CG7giuHr.js";
3
3
 
4
4
  export { ProposalError, createNoopPubSub, createProposalsPlugin };