@mnmal-ai/substrate 1.0.0-alpha.17 → 1.0.0-alpha.19

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 (41) hide show
  1. package/README.md +4 -4
  2. package/dist/{boot-store-DNU0UfX7.js → boot-store-DL2Wkzun.js} +207 -258
  3. package/dist/{bridge-5VHZiH9o.d.ts → bridge-BhiJKwfF.d.ts} +1 -1
  4. package/dist/{cli-CwWUYUMv.js → cli-BYyPsfdY.js} +193 -31
  5. package/dist/client.browser.js +2478 -0
  6. package/dist/client.d.ts +181 -164
  7. package/dist/client.js +5 -5
  8. package/dist/{errors-DHgT7Fa8.d.ts → errors-CYOaWZ8l.d.ts} +1 -1
  9. package/dist/{index-DKJXQ031.d.ts → index-rW7fu2Af.d.ts} +3 -11
  10. package/dist/{node-version-CxqcqECU.js → node-version-BlYrbY2I.js} +8 -1
  11. package/dist/pack.d.ts +59 -11
  12. package/dist/pack.js +2 -2
  13. package/dist/{pg-edges-store-CSJ7sG2l.js → pg-edges-store-B0Krqoac.js} +1 -1
  14. package/dist/{plugin-Dk9RC3LH.js → plugin-Dk9h__NU.js} +2 -2
  15. package/dist/proposals-bootstrap.js +2 -2
  16. package/dist/proposals.d.ts +2 -2
  17. package/dist/proposals.js +1 -1
  18. package/dist/recall-bootstrap.js +2 -2
  19. package/dist/recall.d.ts +1 -1
  20. package/dist/recall.js +2 -2
  21. package/dist/{repl-BNnKFG3y.js → repl-D6_CBsue.js} +1 -1
  22. package/dist/{row-DHxnCnhA.d.ts → row-CfsHSEfB.d.ts} +4 -4
  23. package/dist/{run-NmwNmVUD.js → run-BJMgCcCC.js} +18 -9
  24. package/dist/{schema-B5WBO8yh.d.ts → schema-laGLMvMC.d.ts} +15 -40
  25. package/dist/{script-C6DSa_Na.js → script-DgXaJY1Y.js} +12 -2
  26. package/dist/script-runner.js +78 -0
  27. package/dist/sdk.d.ts +9 -8
  28. package/dist/sdk.js +3 -3
  29. package/dist/{src-CdbUzLhB.js → src-B6nsPVmD.js} +201 -25
  30. package/dist/{src-DV6PcXym.js → src-B_oTGi3X.js} +648 -845
  31. package/dist/{src-DWrFdHrY.js → src-BcigMQdR.js} +7 -4
  32. package/dist/{src-DR_hUj2p.js → src-D3ACIp1y.js} +1 -1
  33. package/dist/{src-DwynOVMk.js → src-Dlmuq32R.js} +3 -3
  34. package/dist/{src-7lp1y4HK.js → src-DreZjBk7.js} +3 -3
  35. package/dist/{src-BHh6UZ7L.js → src-oO0DtS6y.js} +431 -949
  36. package/dist/substrate-admin.js +2 -2
  37. package/dist/substrate.js +2 -2
  38. package/dist/testing.d.ts +13 -47
  39. package/dist/testing.js +6 -167
  40. package/dist/{ws-BL9nSELO.d.ts → ws-BT8kL1IF.d.ts} +4 -4
  41. package/package.json +21 -23
@@ -1,25 +1,24 @@
1
1
  import { createRequire as __binCreateRequire } from 'node:module'; const require = __binCreateRequire(import.meta.url);
2
- import { A as isReservedAutoCrudField, Ft as HttpBridgeClient, Lt as buildContext, Vt as parseAuthConfig, Wt as WS_REASON_SERVER_RESTART, Z as MemoryEdgesStore, _ as AUDIT_EVENT_TYPE, _t as getExtensionOperator, a as composeFromManifests, bt as unregisterExtensionOperator, c as MemoryDataset, et as effectiveFilterOperators, g as buildAuditRow, h as SYSTEM_TYPE_DEFS, i as PluginLoadError, n as kernelMigrationTracking, o as DEFAULT_NAMESPACE, r as mountKernelRoute, s as compose, t as composeKernelPlugins, u as isStoreProfileName, vt as listExtensionOperators, yt as registerExtensionOperator } from "./src-BHh6UZ7L.js";
2
+ import { A as isReservedAutoCrudField, Ft as HttpBridgeClient, Lt as buildContext, Vt as parseAuthConfig, Wt as WS_REASON_SERVER_RESTART, Z as MemoryEdgesStore, _ as AUDIT_EVENT_TYPE, _t as getExtensionOperator, a as composeFromManifests, bt as unregisterExtensionOperator, c as MemoryDataset, et as effectiveFilterOperators, g as buildAuditRow, h as SYSTEM_TYPE_DEFS, i as PluginLoadError, n as kernelMigrationTracking, o as DEFAULT_NAMESPACE, r as mountKernelRoute, s as compose, t as composeKernelPlugins, u as isStoreProfileName, vt as listExtensionOperators, yt as registerExtensionOperator } from "./src-oO0DtS6y.js";
3
3
  import { a as adminClaimCapabilities, d as resolveDefaultKidPath, l as resolveAdminCredential, n as trimTrailingSlashes, r as ADMIN_CAPABILITIES, s as makeAuthorizationProvider, t as parsePositiveInt, 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 "./src-DR_hUj2p.js";
5
- import { L as DEFAULT_FILTER_WRITE_LIMITS } from "./src-CdbUzLhB.js";
6
- import { C as diagnose, S as DIAGNOSE_CHECKS, _ as SKIPPABLE_STEPS, b as planOnboarding, h as CURRENT_MARK, i as tokenAudience, v as STEP_MARKS, x as requiredOutstanding, y as currentStep } from "./src-DV6PcXym.js";
7
- import { _ as pendingMigrations, f as assertNotMigrating, h as deriveListIndexMigration, m as assertTablesLayout, n as PgDataset, t as PgIdempotencyStore, v as runDerivedMigrations, y as runMigrations } from "./src-DWrFdHrY.js";
8
- import { t as PgEdgesStore } from "./pg-edges-store-CSJ7sG2l.js";
9
- import { a as InMemoryLexicalStore, i as PgClusterStore, n as PgLexicalStore, o as InMemoryEmbeddingsStore, r as PgEmbeddingsStore, s as InMemoryClusterStore } from "./src-7lp1y4HK.js";
10
- import { et as memberKey } from "./plugin-Dk9RC3LH.js";
4
+ import { D as isActiveAdminKeyRow, E as describeGrant, L as encodeList, M as grantEntryError, T as SYSTEM_NAMESPACE, v as grantEntriesConfer } from "./src-D3ACIp1y.js";
5
+ import { J as DEFAULT_FILTER_WRITE_LIMITS, a as FRAMES_JSON_MIME, f as withIdleTimeout, l as decodeStreamFrames } from "./src-B6nsPVmD.js";
6
+ import { C as diagnose, E as substrateFetch, S as DIAGNOSE_CHECKS, _ as SKIPPABLE_STEPS, b as planOnboarding, h as CURRENT_MARK, i as tokenAudience, v as STEP_MARKS, x as requiredOutstanding, y as currentStep } from "./src-B_oTGi3X.js";
7
+ import { _ as pendingMigrations, f as assertNotMigrating, h as deriveListIndexMigration, m as assertTablesLayout, n as PgDataset, t as PgIdempotencyStore, v as runDerivedMigrations, y as runMigrations } from "./src-BcigMQdR.js";
8
+ import { t as PgEdgesStore } from "./pg-edges-store-B0Krqoac.js";
9
+ import { a as InMemoryLexicalStore, i as PgClusterStore, n as PgLexicalStore, o as InMemoryEmbeddingsStore, r as PgEmbeddingsStore, s as InMemoryClusterStore } from "./src-DreZjBk7.js";
10
+ import { et as memberKey } from "./plugin-Dk9h__NU.js";
11
11
  import { existsSync, linkSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
12
12
  import { homedir } from "node:os";
13
13
  import { basename, dirname, join, resolve } from "node:path";
14
14
  import { createHash, createPublicKey, generateKeyPairSync, randomUUID } from "node:crypto";
15
15
  import { BlockList, isIP } from "node:net";
16
- import WebSocket$1 from "ws";
16
+ import { constants } from "node:http2";
17
17
  import { readFile, readdir, stat, statfs } from "node:fs/promises";
18
18
  import postgres from "postgres";
19
19
  import { pathToFileURL } from "node:url";
20
20
  import compressPlugin from "@fastify/compress";
21
21
  import corsPlugin from "@fastify/cors";
22
- import websocketPlugin from "@fastify/websocket";
23
22
  import Fastify from "fastify";
24
23
  import { Readable } from "node:stream";
25
24
  import { setTimeout as setTimeout$1 } from "node:timers/promises";
@@ -710,6 +709,14 @@ function readEd25519PublicKey(path) {
710
709
  //#endregion
711
710
  //#region src/admin/route.ts
712
711
  /**
712
+ * Driving the admin-gated `PATCH /substrate/v1/_system/keys` route, and the refusal ->
713
+ * exit-code maps that go with it.
714
+ *
715
+ * The route is the DEFAULT for every mutating subcommand and `--direct` is what
716
+ * an operator has to type, because a direct write leaves the running server's
717
+ * registry stale — for `revoke` that means the credential keeps authenticating.
718
+ */
719
+ /**
713
720
  * Drive `PATCH /substrate/v1/_system/keys`. A write through the engine reaches the
714
721
  * running server's key registry; a direct write does not.
715
722
  *
@@ -717,7 +724,7 @@ function readEd25519PublicKey(path) {
717
724
  * because both sides ask `key-guards`. Statuses are mapped back to the
718
725
  * same exit codes so scripts do not care which transport ran.
719
726
  */
720
- async function runKeyPatchViaRoute(body, baseUrl, env, exitCodes, fetchImpl = fetch) {
727
+ async function runKeyPatchViaRoute(body, baseUrl, env, exitCodes, fetchImpl = substrateFetch) {
721
728
  const auth = resolveAdminAuth(env, baseUrl);
722
729
  if ("error" in auth) throw new AdminCliError(`${body.revoke === true ? "revoke" : "update"}: ${auth.error}`, 1);
723
730
  const url = `${trimTrailingSlashes(baseUrl)}/substrate/v1/_system/keys`;
@@ -780,7 +787,7 @@ const UPDATE_EXIT_CODES = {
780
787
  * Pages to exhaustion rather than returning the first page: a silently
781
788
  * truncated answer to "which identities exist" is worse than an error.
782
789
  */
783
- async function runKeyListViaRoute(baseUrl, env, fetchImpl = fetch) {
790
+ async function runKeyListViaRoute(baseUrl, env, fetchImpl = substrateFetch) {
784
791
  const auth = resolveAdminAuth(env, baseUrl);
785
792
  if ("error" in auth) throw new AdminCliError(`list: ${auth.error}`, 1);
786
793
  const base = `${trimTrailingSlashes(baseUrl)}/substrate/v1/_system/keys`;
@@ -1016,7 +1023,7 @@ const DIRECT_TARGET_REFUSAL = {
1016
1023
  * Runs every check against `target`, in dependency order. A failure
1017
1024
  * short-circuits its dependants into explicit skips.
1018
1025
  */
1019
- async function runDoctor(env, target, fetchImpl = fetch) {
1026
+ async function runDoctor(env, target, fetchImpl = substrateFetch) {
1020
1027
  if (target.kind !== "route") return {
1021
1028
  checks: [DIRECT_TARGET_REFUSAL, ...DIAGNOSE_CHECKS.map((name) => ({
1022
1029
  name,
@@ -1036,7 +1043,7 @@ async function runDoctor(env, target, fetchImpl = fetch) {
1036
1043
  };
1037
1044
  }
1038
1045
  /** Diagnose `baseUrl` with the admin credential `env` resolves — shared by doctor and onboard. */
1039
- async function diagnoseTarget(env, baseUrl, fetchImpl = fetch) {
1046
+ async function diagnoseTarget(env, baseUrl, fetchImpl = substrateFetch) {
1040
1047
  const auth = resolveAdminAuth(env, baseUrl);
1041
1048
  return diagnose({
1042
1049
  baseUrl,
@@ -1079,7 +1086,7 @@ function formatDoctorReport(report) {
1079
1086
  return `${lines.join("\n")}\n`;
1080
1087
  }
1081
1088
  /** Resolve the target and run — the shape `main` dispatches to. */
1082
- async function doctorCommand(argv, env, fetchImpl = fetch) {
1089
+ async function doctorCommand(argv, env, fetchImpl = substrateFetch) {
1083
1090
  validateArgv("doctor", argv);
1084
1091
  let url;
1085
1092
  let direct = false;
@@ -1368,7 +1375,7 @@ async function runRegister(args, env, openDataset = openPgDataset) {
1368
1375
  * writes the directory row in the same transaction. Throws `AdminCliError`
1369
1376
  * on a non-2xx response.
1370
1377
  */
1371
- async function runRegisterViaRoute(args, baseUrl, env, fetchImpl = fetch) {
1378
+ async function runRegisterViaRoute(args, baseUrl, env, fetchImpl = substrateFetch) {
1372
1379
  if (grantEntriesConfer(args.namespaces, args.capabilities, "admin") && !args.allowAdmin) throw new AdminCliError("register: 'admin' capability requires --allow-admin (bootstrap is the deliberate admin path)", 1);
1373
1380
  const publicKeyPem = readEd25519PublicKey(args.pubkeyPath);
1374
1381
  const fingerprint = `sha256:${base64Url(createHash("sha256").update(publicKeyPem).digest()).slice(0, 8)}`;
@@ -1640,7 +1647,7 @@ function registerArgs(kid, pubkeyPath, over) {
1640
1647
  };
1641
1648
  }
1642
1649
  /** The production action runners: the same functions the standalone subcommands call. */
1643
- function realOnboardActions(env, baseUrl, fetchImpl = fetch) {
1650
+ function realOnboardActions(env, baseUrl, fetchImpl = substrateFetch) {
1644
1651
  const cap = env.SUBSTRATE_ADMIN_CAPABILITY !== void 0 && env.SUBSTRATE_ADMIN_CAPABILITY.length > 0 ? env.SUBSTRATE_ADMIN_CAPABILITY : DEFAULT_ADMIN_CAPABILITY;
1645
1652
  return {
1646
1653
  bootstrap: async () => formatBootstrapOutput(await runBootstrap(env)),
@@ -1662,7 +1669,7 @@ function realOnboardActions(env, baseUrl, fetchImpl = fetch) {
1662
1669
  };
1663
1670
  }
1664
1671
  /** Parse argv, resolve the target, and run — the shape `main` dispatches to. */
1665
- async function onboardCommand(argv, env, io, interactive, fetchImpl = fetch) {
1672
+ async function onboardCommand(argv, env, io, interactive, fetchImpl = substrateFetch) {
1666
1673
  validateArgv("onboard", argv);
1667
1674
  let url;
1668
1675
  let direct = false;
@@ -1747,7 +1754,7 @@ async function closeQuietly(sql) {
1747
1754
 
1748
1755
  //#endregion
1749
1756
  //#region package.json
1750
- var version = "1.0.0-alpha.17";
1757
+ var version = "1.0.0-alpha.19";
1751
1758
 
1752
1759
  //#endregion
1753
1760
  //#region src/plugin-manifest.ts
@@ -2135,18 +2142,21 @@ async function applySubstrateResponse(reply, res) {
2135
2142
  * createPluginRouter design call: plugin routes are mounted under
2136
2143
  * `/substrate/v1/plugins/<name>` so they can never shadow `/substrate/v1/*` and two plugins
2137
2144
  * declaring the same subpath (e.g. `/ping`) stay isolated. The facade
2138
- * prefixes every path before handing it to `registerRoute` / `registerWsRoute`
2145
+ * prefixes every path before handing it to `registerRoute`
2139
2146
  * — the main Fastify instance still sees one flat routing table.
2140
2147
  *
2141
2148
  * start() twice: Fastify's listen() rejects on a second call. We surface that
2142
2149
  * error as-is rather than caching and returning the prior address — the
2143
2150
  * caller's intent is ambiguous, fail loudly.
2144
2151
  */
2152
+ /** @fastify/compress's default filter. It already leaves `text/event-stream` alone. */
2153
+ const COMPRESSIBLE = /^text\/(?!event-stream)|(?:\+|\/)json(?:;|$)|(?:\+|\/)text(?:;|$)|(?:\+|\/)xml(?:;|$)|octet-stream(?:;|$)/u;
2154
+ /** Frame streams are `+json`, but a gzip stream buffers, which would stop them streaming. */
2155
+ const FRAME_STREAM = /^application\/vnd\.substrate\.frames\+/u;
2145
2156
  const DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
2146
- const DEFAULT_WS_MAX_PAYLOAD = 1 << 20;
2147
- const DEFAULT_WS_MAX_BUFFERED = 16 << 20;
2148
- /** How long a buffer-full socket gets to finish its close handshake before it is dropped. */
2149
- const WS_CLOSE_GRACE_MS = 1e3;
2157
+ const DEFAULT_PING_INTERVAL_MS = 3e4;
2158
+ /** Streams one HTTP/2 connection may have open at once. Node's default is unbounded. */
2159
+ const H2_MAX_CONCURRENT_STREAMS = 128;
2150
2160
  const HANDLED_ERRORS = /* @__PURE__ */ new WeakSet();
2151
2161
  var FastifySubstrateServer = class {
2152
2162
  fastify;
@@ -2155,32 +2165,40 @@ var FastifySubstrateServer = class {
2155
2165
  #observer;
2156
2166
  #inFlightObserver;
2157
2167
  inFlight = /* @__PURE__ */ new Set();
2158
- inFlightWs = /* @__PURE__ */ new Map();
2159
2168
  #shutdownHooks = /* @__PURE__ */ new Set();
2160
- #streamedResponses = /* @__PURE__ */ new Set();
2161
- #wsMaxBuffered;
2162
- #onWarn;
2169
+ #sockets = /* @__PURE__ */ new Set();
2170
+ #refused = /* @__PURE__ */ new Set();
2163
2171
  constructor(options = {}) {
2164
- const { logger = false, bodyLimit, tls, cors, compression = true, requestTimeout = DEFAULT_REQUEST_TIMEOUT_MS, wsMaxPayload = DEFAULT_WS_MAX_PAYLOAD, wsMaxBuffered = DEFAULT_WS_MAX_BUFFERED, onWarn } = options;
2165
- this.#wsMaxBuffered = wsMaxBuffered;
2166
- this.#onWarn = onWarn;
2172
+ const { logger = false, bodyLimit, tls, cors, compression = true, requestTimeout = DEFAULT_REQUEST_TIMEOUT_MS, pingIntervalMs = DEFAULT_PING_INTERVAL_MS } = options;
2167
2173
  this.hasTls = tls !== void 0;
2168
2174
  const common = {
2169
2175
  logger,
2170
2176
  requestTimeout,
2171
2177
  ...bodyLimit !== void 0 ? { bodyLimit } : {}
2172
2178
  };
2173
- if (tls) {
2174
- const httpsOptions = {
2179
+ const settings = { maxConcurrentStreams: H2_MAX_CONCURRENT_STREAMS };
2180
+ this.fastify = Fastify({
2181
+ ...common,
2182
+ http2: true,
2183
+ ...tls ? { https: {
2184
+ allowHTTP1: false,
2175
2185
  cert: tls.cert,
2176
2186
  key: tls.key,
2177
- ...tls.ca !== void 0 ? { ca: tls.ca } : {}
2178
- };
2179
- this.fastify = Fastify({
2180
- ...common,
2181
- https: httpsOptions
2182
- });
2183
- } else this.fastify = Fastify(common);
2187
+ ...tls.ca !== void 0 ? { ca: tls.ca } : {},
2188
+ settings
2189
+ } } : { http: { settings } }
2190
+ });
2191
+ if (requestTimeout > 0) boundH2Requests(this.fastify, requestTimeout);
2192
+ if (pingIntervalMs > 0) pingSessions(this.fastify, pingIntervalMs);
2193
+ this.fastify.server.on("connection", (socket) => {
2194
+ this.#sockets.add(socket);
2195
+ socket.once("close", () => this.#sockets.delete(socket));
2196
+ });
2197
+ this.fastify.server.on("secureConnection", (socket) => {
2198
+ if (socket.alpnProtocol === "h2") return;
2199
+ this.#refused.add(socket);
2200
+ socket.once("close", () => this.#refused.delete(socket));
2201
+ });
2184
2202
  this.fastify.addContentTypeParser("application/octet-stream", { parseAs: "buffer" }, (_req, body, done) => done(null, body));
2185
2203
  this.fastify.addContentTypeParser("application/vnd.substrate.pack", { parseAs: "buffer" }, (_req, body, done) => done(null, body));
2186
2204
  this.fastify.setErrorHandler((err, req, reply) => {
@@ -2203,7 +2221,10 @@ var FastifySubstrateServer = class {
2203
2221
  this.fastify.addHook("onError", async (req, _reply, err) => {
2204
2222
  this.invokeErrorHook(err, req);
2205
2223
  });
2206
- if (compression) this.fastify.register(compressPlugin, { threshold: 1024 });
2224
+ if (compression) this.fastify.register(compressPlugin, {
2225
+ threshold: 1024,
2226
+ customTypes: (type) => COMPRESSIBLE.test(type) && !FRAME_STREAM.test(type)
2227
+ });
2207
2228
  if (cors !== void 0) this.fastify.register(corsPlugin, {
2208
2229
  origin: cors.origins === "*" ? "*" : cors.origins,
2209
2230
  credentials: cors.credentials ?? cors.origins !== "*",
@@ -2221,7 +2242,6 @@ var FastifySubstrateServer = class {
2221
2242
  "Accept"
2222
2243
  ]
2223
2244
  });
2224
- this.fastify.register(websocketPlugin, { options: { maxPayload: wsMaxPayload } });
2225
2245
  }
2226
2246
  invokeErrorHook(err, req) {
2227
2247
  if (!this.errorHook) return;
@@ -2254,37 +2274,21 @@ var FastifySubstrateServer = class {
2254
2274
  };
2255
2275
  }
2256
2276
  async stop(opts) {
2257
- this.notifyWsShutdown(WS_REASON_SERVER_RESTART);
2258
2277
  this.#notifyShutdown(WS_REASON_SERVER_RESTART);
2259
2278
  if (opts?.abortInFlight === true) this.abortAllRequests("server_shutdown");
2279
+ for (const socket of this.#refused) socket.destroy();
2280
+ const closed = this.fastify.close();
2260
2281
  const grace = opts?.grace;
2261
- if (grace === void 0) {
2262
- await this.#drainThenClose();
2263
- return;
2282
+ if (grace !== void 0) {
2283
+ let timer;
2284
+ const late = await Promise.race([closed.then(() => false, () => false), new Promise((resolve) => {
2285
+ timer = setTimeout(() => resolve(true), grace);
2286
+ timer.unref();
2287
+ })]);
2288
+ clearTimeout(timer);
2289
+ if (late) for (const socket of this.#sockets) socket.destroy();
2264
2290
  }
2265
- await Promise.race([this.#drainThenClose(), new Promise((resolve) => {
2266
- setTimeout(resolve, grace).unref();
2267
- })]);
2268
- }
2269
- /**
2270
- * Let streamed responses finish before handing off to `fastify.close()`.
2271
- *
2272
- * `close()` takes exactly ONE pass at reaping idle keep-alive connections. A
2273
- * socket still flushing its final bytes at that instant survives the pass,
2274
- * and nothing revisits it — so `close()` waits on a connection that will
2275
- * never be reaped, and a bare `stop()` never returns.
2276
- *
2277
- * Unbounded on purpose: `fastify.close()` already waits on in-flight
2278
- * requests, so a route that ignores the shutdown notice blocks `stop()`
2279
- * anyway. Callers who need a ceiling pass `grace`, which races this whole
2280
- * sequence.
2281
- */
2282
- async #drainThenClose() {
2283
- await Promise.all([...this.#streamedResponses]);
2284
- await new Promise((resolve) => {
2285
- setImmediate(resolve);
2286
- });
2287
- await this.fastify.close();
2291
+ await closed;
2288
2292
  }
2289
2293
  abortAllRequests(reason) {
2290
2294
  const controllers = [...this.inFlight];
@@ -2303,24 +2307,8 @@ var FastifySubstrateServer = class {
2303
2307
  } catch {}
2304
2308
  }
2305
2309
  /**
2306
- * Walk every live WS connection and invoke its registered shutdown emitter,
2307
- * then close the socket with code 1012 (Service Restart). Synchronous so
2308
- * frames flush before the @fastify/websocket plugin's close pass runs.
2309
- */
2310
- notifyWsShutdown(reason) {
2311
- const entries = [...this.inFlightWs.entries()];
2312
- for (const [conn, emit] of entries) {
2313
- try {
2314
- emit?.(reason);
2315
- } catch {}
2316
- try {
2317
- conn.close(1012, reason);
2318
- } catch {}
2319
- }
2320
- }
2321
- /**
2322
2310
  * Wrap an HTTP-method route in a child plugin so it lands in the plugin
2323
- * queue *after* compress / cors / websocket have finished loading.
2311
+ * queue *after* compress / cors have finished loading.
2324
2312
  * @fastify/compress installs an `onRoute` hook that decorates each route
2325
2313
  * with compression metadata; routes added directly to the parent scope
2326
2314
  * before ready() runs miss that hook and are never compressed.
@@ -2330,14 +2318,7 @@ var FastifySubstrateServer = class {
2330
2318
  */
2331
2319
  #registerWrapped(method, path, handler, inFlight = this.inFlight) {
2332
2320
  this.fastify.register((scope, _opts, done) => {
2333
- registerRoute(scope, method, path, handler, inFlight, this.#streamedResponses);
2334
- done();
2335
- });
2336
- }
2337
- /** Same ordering guarantee as #registerWrapped but for WebSocket routes. */
2338
- #registerWsWrapped(path, handler) {
2339
- this.fastify.register((scope, _opts, done) => {
2340
- registerWsRoute(scope, path, handler, this.inFlightWs, (err) => this.invokeErrorHook(err), this.#wsMaxBuffered, this.#onWarn ?? ((message) => scope.log.warn(message)));
2321
+ registerRoute(scope, method, path, handler, inFlight);
2341
2322
  done();
2342
2323
  });
2343
2324
  }
@@ -2379,9 +2360,6 @@ var FastifySubstrateServer = class {
2379
2360
  observeRequests(observer) {
2380
2361
  this.#observer = observer;
2381
2362
  }
2382
- websocket(path, handler) {
2383
- this.#registerWsWrapped(path, handler);
2384
- }
2385
2363
  onError(hook) {
2386
2364
  this.errorHook = hook;
2387
2365
  }
@@ -2395,10 +2373,7 @@ var FastifySubstrateServer = class {
2395
2373
  "DELETE",
2396
2374
  "PATCH"
2397
2375
  ].map((method) => [method.toLowerCase(), (path, handler) => this.#registerWrapped(method, prefixed(path), handler, this.inFlight)]);
2398
- return {
2399
- ...Object.fromEntries(verbEntries),
2400
- websocket: (path, handler) => this.#registerWsWrapped(prefixed(path), handler)
2401
- };
2376
+ return { ...Object.fromEntries(verbEntries) };
2402
2377
  }
2403
2378
  async inject(req) {
2404
2379
  await this.fastify.ready();
@@ -2436,24 +2411,7 @@ function buildInjectUrl(path, query) {
2436
2411
  }
2437
2412
  return `${path}${path.includes("?") ? "&" : "?"}${qs.toString()}`;
2438
2413
  }
2439
- /**
2440
- * Register a response that streams its body, resolving when the underlying
2441
- * socket finishes (`close` covers both a completed response and a torn-down
2442
- * connection). `stop()` awaits these so it never calls `fastify.close()` while
2443
- * a socket is mid-flush.
2444
- */
2445
- function trackStreamedResponse(reply, streamed) {
2446
- let settle;
2447
- const finished = new Promise((resolve) => {
2448
- settle = resolve;
2449
- });
2450
- streamed.add(finished);
2451
- reply.raw.once("close", () => {
2452
- streamed.delete(finished);
2453
- settle();
2454
- });
2455
- }
2456
- function registerRoute(fastify, method, path, handler, inFlight, streamed) {
2414
+ function registerRoute(fastify, method, path, handler, inFlight) {
2457
2415
  fastify.route({
2458
2416
  method,
2459
2417
  url: path,
@@ -2461,12 +2419,11 @@ function registerRoute(fastify, method, path, handler, inFlight, streamed) {
2461
2419
  const controller = new AbortController();
2462
2420
  inFlight.add(controller);
2463
2421
  const onResponseClose = () => {
2464
- if (!reply.raw.writableFinished && !controller.signal.aborted) controller.abort("client_disconnect");
2422
+ if (!reply.raw.writableEnded && !controller.signal.aborted) controller.abort("client_disconnect");
2465
2423
  };
2466
2424
  reply.raw.once("close", onResponseClose);
2467
2425
  try {
2468
2426
  const hRes = await handler(toSubstrateRequest(req, controller.signal));
2469
- if (hRes.body instanceof ReadableStream) trackStreamedResponse(reply, streamed);
2470
2427
  await applySubstrateResponse(reply, hRes);
2471
2428
  } finally {
2472
2429
  reply.raw.off("close", onResponseClose);
@@ -2475,151 +2432,147 @@ function registerRoute(fastify, method, path, handler, inFlight, streamed) {
2475
2432
  }
2476
2433
  });
2477
2434
  }
2478
- function registerWsRoute(fastify, path, handler, inFlightWs, onHandlerError, maxBuffered, warn) {
2479
- const wsHandler = (socket, req) => {
2480
- const substrateReq = {
2481
- ...toSubstrateRequest(req),
2482
- body: null
2483
- };
2484
- let shutdownEmitter = null;
2485
- let bufferFull = false;
2486
- const conn = {
2487
- id: randomUUID(),
2488
- send(frame) {
2489
- if (bufferFull) return;
2490
- if (socket.bufferedAmount > maxBuffered) {
2491
- bufferFull = true;
2492
- warn(`websocket ${path} conn ${conn.id}: send buffer full (${socket.bufferedAmount} > ${maxBuffered} bytes); closing 1013`);
2493
- socket.close(1013, "send_buffer_full");
2494
- setTimeout(() => socket.terminate(), WS_CLOSE_GRACE_MS).unref?.();
2495
- return;
2496
- }
2497
- if (typeof frame === "string") socket.send(frame);
2498
- else if (Buffer.isBuffer(frame)) socket.send(frame);
2499
- else socket.send(Buffer.from(frame));
2500
- },
2501
- drain() {
2502
- const HIGH_WATER = 1 << 20;
2503
- if (socket.bufferedAmount <= HIGH_WATER || socket.readyState !== 1) return Promise.resolve();
2504
- return new Promise((resolve) => {
2505
- const check = () => {
2506
- if (socket.readyState !== 1 || socket.bufferedAmount <= HIGH_WATER) {
2507
- resolve();
2508
- return;
2509
- }
2510
- setTimeout(check, 10).unref?.();
2511
- };
2512
- setTimeout(check, 10).unref?.();
2513
- });
2514
- },
2515
- onMessage(cb) {
2516
- socket.on("message", (buf) => {
2517
- cb(Buffer.isBuffer(buf) ? buf : Buffer.from(buf));
2518
- });
2519
- },
2520
- onClose(cb) {
2521
- socket.on("close", (code) => {
2522
- cb(code);
2523
- });
2524
- },
2525
- close(code, reason) {
2526
- socket.close(code, reason);
2527
- },
2528
- onShutdown(cb) {
2529
- shutdownEmitter = cb;
2530
- inFlightWs.set(conn, shutdownEmitter);
2435
+ /**
2436
+ * HTTP/2 has no `requestTimeout`: give each h2 request `ms` to arrive in full
2437
+ * (headers and body), then reset its stream. The clock stops once the handler
2438
+ * runs, so a streamed response is not bounded by it.
2439
+ */
2440
+ function boundH2Requests(fastify, ms) {
2441
+ const timers = /* @__PURE__ */ new WeakMap();
2442
+ fastify.addHook("onRequest", async (req) => {
2443
+ const stream = req.raw.stream;
2444
+ if (stream === void 0) return;
2445
+ const timer = setTimeout(() => stream.close(constants.NGHTTP2_CANCEL), ms);
2446
+ timers.set(req.raw, timer);
2447
+ stream.once("close", () => clearTimeout(timer));
2448
+ });
2449
+ fastify.addHook("preHandler", async (req, reply) => {
2450
+ clearTimeout(timers.get(req.raw));
2451
+ if (req.raw.stream?.closed === true) return reply.code(408).send();
2452
+ });
2453
+ }
2454
+ /**
2455
+ * Ping each HTTP/2 session every `ms`; a session with two pings in a row
2456
+ * unanswered is destroyed. That is how a peer that vanished without closing
2457
+ * its socket is noticed, and its streams' close paths run. Two, not one: a
2458
+ * ping can queue behind a full write buffer on a slow link, or its ack behind
2459
+ * a blocked event loop.
2460
+ */
2461
+ function pingSessions(fastify, ms) {
2462
+ fastify.server.on("session", (session) => {
2463
+ let unanswered = 0;
2464
+ const timer = setInterval(() => {
2465
+ if (unanswered >= 2) {
2466
+ session.destroy();
2467
+ return;
2531
2468
  }
2532
- };
2533
- inFlightWs.set(conn, null);
2534
- socket.on("close", () => {
2535
- inFlightWs.delete(conn);
2536
- });
2537
- const fail = (err) => {
2538
- onHandlerError(err instanceof Error ? err : new Error(String(err)));
2539
- socket.close(1011, "internal error");
2540
- };
2541
- try {
2542
- Promise.resolve(handler(substrateReq, conn)).catch(fail);
2543
- } catch (err) {
2544
- fail(err);
2545
- }
2546
- };
2547
- fastify.route({
2548
- method: "GET",
2549
- url: path,
2550
- handler: (_req, reply) => {
2551
- reply.status(426).send({ error: "Upgrade Required" });
2552
- },
2553
- wsHandler
2469
+ unanswered++;
2470
+ try {
2471
+ session.ping((err) => {
2472
+ if (err === null) unanswered = 0;
2473
+ });
2474
+ } catch {}
2475
+ }, ms);
2476
+ timer.unref();
2477
+ session.once("close", () => clearInterval(timer));
2554
2478
  });
2555
2479
  }
2556
2480
 
2557
2481
  //#endregion
2558
2482
  //#region ../../packages/substrate-sentinel-client/src/client.ts
2559
2483
  /**
2560
- * substrate-sentinel-client — watches a sentinel instance's WS topology stream and
2484
+ * substrate-sentinel-client — watches a sentinel instance's topology feed and
2561
2485
  * mutates a caller-owned BridgeRegistry on online/offline events.
2562
2486
  *
2563
- * - connect to sentinel WS, send {type:'watch'}
2487
+ * - GET <sentinel>/substrate/v1/plugins/substrate-sentinel/watch, a stream of
2488
+ * length-prefixed JSON frames
2564
2489
  * - on {event:'online'}: create HttpBridgeClient, register in bridges,
2565
2490
  * call engine.refreshBridgeSchemas()
2566
2491
  * - on {event:'offline'}: unregister bridge + dispose client
2567
- * - on error/close: reconnect with exponential backoff
2568
- * - stop(): closes WS + disposes all active bridges
2492
+ * - on a refusal or a dropped stream: reconnect with exponential backoff,
2493
+ * reset only after a stream that lasted `reconnectMaxMs`
2494
+ * - stop(): ends the stream + disposes all active bridges
2569
2495
  */
2496
+ const WATCH_PATH = "/substrate/v1/plugins/substrate-sentinel/watch";
2497
+ /** Three of the sentinel's heartbeats at its 30 s maximum `pingIntervalMs`, plus a margin. */
2498
+ const SENTINEL_IDLE_MS = 95e3;
2570
2499
  const NO_OP_METRIC = () => {};
2571
2500
  function startSentinelClient(opts) {
2572
2501
  const metric = opts.metric ?? NO_OP_METRIC;
2573
- const upgradeAuth = opts.auth;
2574
- const wsFactory = opts.wsFactory ?? ((url) => upgradeAuth === void 0 ? new WebSocket$1(url) : new WebSocket$1(url, { headers: { authorization: upgradeAuth() } }));
2502
+ const watchAuth = opts.auth;
2503
+ const fetchImpl = opts.fetch ?? substrateFetch;
2504
+ const watchUrl = `${trimTrailingSlashes(opts.sentinelUrl)}${WATCH_PATH}`;
2575
2505
  const baseMs = opts.reconnectBaseMs ?? 500;
2576
2506
  const maxMs = opts.reconnectMaxMs ?? 3e4;
2507
+ const idleMs = opts.idleMs ?? SENTINEL_IDLE_MS;
2577
2508
  const ownedBridges = /* @__PURE__ */ new Map();
2578
- let ws = null;
2509
+ const ctrl = new AbortController();
2579
2510
  let stopped = false;
2580
2511
  let connected = false;
2581
2512
  let currentBackoff = baseMs;
2582
- let reconnectTimer = null;
2583
- function scheduleReconnect() {
2584
- if (stopped) return;
2585
- const delay = currentBackoff;
2586
- metric("sentinel_client_reconnecting", { delayMs: delay });
2587
- const timer = setTimeout(() => {
2588
- reconnectTimer = null;
2589
- if (stopped) return;
2590
- currentBackoff = Math.min(currentBackoff * 2, maxMs);
2591
- connect();
2592
- }, delay);
2593
- timer.unref();
2594
- reconnectTimer = timer;
2513
+ /** Resolves after `ms`, or at once when stopped. The timer never holds the process open. */
2514
+ function pause(ms) {
2515
+ return new Promise((resolve) => {
2516
+ const timer = setTimeout(done, ms);
2517
+ timer.unref();
2518
+ function done() {
2519
+ clearTimeout(timer);
2520
+ ctrl.signal.removeEventListener("abort", done);
2521
+ resolve();
2522
+ }
2523
+ ctrl.signal.addEventListener("abort", done, { once: true });
2524
+ });
2595
2525
  }
2596
- function connect() {
2597
- if (stopped) return;
2598
- const socket = wsFactory(opts.sentinelUrl);
2599
- ws = socket;
2600
- socket.once("open", () => {
2601
- connected = true;
2602
- currentBackoff = baseMs;
2603
- metric("sentinel_client_connected");
2604
- socket.send(JSON.stringify({ type: "watch" }));
2526
+ /** One watch request, read to its end. */
2527
+ async function watchOnce() {
2528
+ const headers = { accept: FRAMES_JSON_MIME };
2529
+ if (watchAuth !== void 0) headers.authorization = watchAuth();
2530
+ const res = await fetchImpl(watchUrl, {
2531
+ method: "GET",
2532
+ headers,
2533
+ signal: ctrl.signal
2605
2534
  });
2606
- socket.on("message", (buf) => {
2607
- let frame;
2608
- try {
2609
- frame = JSON.parse(buf.toString("utf8"));
2610
- } catch {
2611
- return;
2535
+ const type = res.headers.get("content-type") ?? "";
2536
+ if (!res.ok || res.body === null || !type.startsWith("application/vnd.substrate.frames+json")) {
2537
+ await res.body?.cancel();
2538
+ metric("sentinel_client_error", {
2539
+ status: res.status,
2540
+ error: `watch answered ${res.status} ${type}`
2541
+ });
2542
+ return;
2543
+ }
2544
+ const openedAt = Date.now();
2545
+ connected = true;
2546
+ metric("sentinel_client_connected");
2547
+ try {
2548
+ const utf8 = new TextDecoder();
2549
+ for await (const payload of decodeStreamFrames(idleMs > 0 ? withIdleTimeout(res.body, idleMs) : res.body)) {
2550
+ let frame;
2551
+ try {
2552
+ frame = JSON.parse(utf8.decode(payload));
2553
+ } catch {
2554
+ continue;
2555
+ }
2556
+ if (frame.event === "online") handleOnline(frame);
2557
+ else if (frame.event === "offline") handleOffline(frame);
2612
2558
  }
2613
- if (frame.event === "online") handleOnline(frame);
2614
- else if (frame.event === "offline") handleOffline(frame);
2615
- else if (typeof frame.error === "string") metric("sentinel_client_error", { error: frame.error });
2616
- });
2617
- socket.once("close", () => {
2559
+ } finally {
2618
2560
  connected = false;
2561
+ if (Date.now() - openedAt >= maxMs) currentBackoff = baseMs;
2562
+ }
2563
+ }
2564
+ async function run() {
2565
+ while (!stopped) {
2566
+ try {
2567
+ await watchOnce();
2568
+ } catch {}
2569
+ if (stopped) return;
2619
2570
  metric("sentinel_client_disconnected");
2620
- scheduleReconnect();
2621
- });
2622
- socket.once("error", () => {});
2571
+ const delay = currentBackoff;
2572
+ metric("sentinel_client_reconnecting", { delayMs: delay });
2573
+ currentBackoff = Math.min(currentBackoff * 2, maxMs);
2574
+ await pause(delay);
2575
+ }
2623
2576
  }
2624
2577
  async function handleOnline(frame) {
2625
2578
  const name = typeof frame.name === "string" ? frame.name : void 0;
@@ -2672,7 +2625,8 @@ function startSentinelClient(opts) {
2672
2625
  const client = new HttpBridgeClient({
2673
2626
  name,
2674
2627
  baseUrl: url,
2675
- auth
2628
+ auth,
2629
+ fetch: substrateFetch
2676
2630
  });
2677
2631
  opts.bridges.register(ns, client);
2678
2632
  ownedBridges.set(ns, client);
@@ -2710,16 +2664,13 @@ function startSentinelClient(opts) {
2710
2664
  metric("sentinel_client_offline", { ns });
2711
2665
  }
2712
2666
  }
2713
- connect();
2667
+ const running = run();
2714
2668
  return {
2715
2669
  isConnected: () => connected,
2716
2670
  stop: async () => {
2717
2671
  if (stopped) return;
2718
2672
  stopped = true;
2719
- if (reconnectTimer !== null) {
2720
- clearTimeout(reconnectTimer);
2721
- reconnectTimer = null;
2722
- }
2673
+ ctrl.abort();
2723
2674
  for (const [ns, client] of ownedBridges) {
2724
2675
  try {
2725
2676
  opts.bridges.unregister(ns);
@@ -2727,10 +2678,7 @@ function startSentinelClient(opts) {
2727
2678
  client.dispose();
2728
2679
  }
2729
2680
  ownedBridges.clear();
2730
- if (ws !== null && ws.readyState !== WebSocket$1.CLOSED) await new Promise((resolve) => {
2731
- ws?.once("close", () => resolve());
2732
- ws?.close();
2733
- });
2681
+ await running;
2734
2682
  }
2735
2683
  };
2736
2684
  }
@@ -2759,7 +2707,7 @@ const DEFAULT_RECONNECT_MAX_MS = 3e4;
2759
2707
  function loadConfig(env = process.env) {
2760
2708
  const rawUrl = env.SUBSTRATE_SENTINEL_URL;
2761
2709
  if (rawUrl === void 0 || rawUrl.length === 0) return null;
2762
- if (!rawUrl.startsWith("ws://") && !rawUrl.startsWith("wss://")) throw new Error("SUBSTRATE_SENTINEL_URL must be a ws:// or wss:// URL");
2710
+ if (!rawUrl.startsWith("http://") && !rawUrl.startsWith("https://")) throw new Error("SUBSTRATE_SENTINEL_URL must be the sentinel's http:// or https:// base URL (e.g. https://sentinel.example:3100); the ws:// endpoint is retired");
2763
2711
  return {
2764
2712
  url: rawUrl,
2765
2713
  cred: parseCred(env.SUBSTRATE_SENTINEL_CREDENTIAL),
@@ -2887,7 +2835,8 @@ function buildBridgeRegistry(specs, creds) {
2887
2835
  const client = new HttpBridgeClient({
2888
2836
  name: spec.name,
2889
2837
  baseUrl: spec.url,
2890
- auth
2838
+ auth,
2839
+ fetch: substrateFetch
2891
2840
  });
2892
2841
  clients.push(client);
2893
2842
  registry.register(spec.ns, client);
@@ -3287,9 +3236,10 @@ function parseEnv(env) {
3287
3236
  const snapshotLimit = parseSnapshotLimit(env, knobs, pageLimits.maxLimit);
3288
3237
  const filterWriteLimits = parseFilterWriteLimits(env, knobs);
3289
3238
  const keyReprimeIntervalMs = parseKeyReprimeIntervalMs(env, knobs);
3290
- knobs.recordSecret("streamTicketSecret", "SUBSTRATE_STREAM_TICKET_SECRET", env.SUBSTRATE_STREAM_TICKET_SECRET);
3239
+ const cursorEnv = nonEmpty(env.SUBSTRATE_CURSOR_SECRET?.trim()) === void 0 && nonEmpty(env.SUBSTRATE_STREAM_TICKET_SECRET?.trim()) !== void 0 ? "SUBSTRATE_STREAM_TICKET_SECRET" : "SUBSTRATE_CURSOR_SECRET";
3240
+ knobs.recordSecret("cursorSealSecret", cursorEnv, env[cursorEnv]);
3291
3241
  knobs.record("systemActor", "SUBSTRATE_SYSTEM_ACTOR", env.SUBSTRATE_SYSTEM_ACTOR, nonEmpty(env.SUBSTRATE_SYSTEM_ACTOR?.trim()) ?? null);
3292
- const streamTicketSecret = nonEmpty(env.SUBSTRATE_STREAM_TICKET_SECRET?.trim()) ?? null;
3242
+ const cursorSealSecret = nonEmpty(env[cursorEnv]?.trim()) ?? null;
3293
3243
  return {
3294
3244
  storeSpec,
3295
3245
  memoryByDefault,
@@ -3321,7 +3271,7 @@ function parseEnv(env) {
3321
3271
  filterWriteLimits,
3322
3272
  keyReprimeIntervalMs,
3323
3273
  knobs: knobs.report(),
3324
- streamTicketSecret,
3274
+ cursorSealSecret,
3325
3275
  systemActor: nonEmpty(env.SUBSTRATE_SYSTEM_ACTOR?.trim()) ?? null
3326
3276
  };
3327
3277
  }
@@ -3676,8 +3626,8 @@ function parseBridgeCreds(raw) {
3676
3626
  /**
3677
3627
  * serve/compose-opts.ts — `compose()` options assembly.
3678
3628
  *
3679
- * Pure assembly over already-built pieces: datasets, bridges, auth, ticket
3680
- * secret, idempotency. No I/O except the sentinel-client env read; the
3629
+ * Pure assembly over already-built pieces: datasets, bridges, auth, the
3630
+ * cursor seal secret, idempotency. No I/O except the sentinel-client env read; the
3681
3631
  * substrate-migrations table ensure runs here too so it lands before any
3682
3632
  * substrate plugin migration.
3683
3633
  */
@@ -3737,7 +3687,7 @@ async function buildComposeOpts(args) {
3737
3687
  if (config.auth !== null) composeOpts.auth = config.auth;
3738
3688
  if (config.keyReprimeIntervalMs !== null) composeOpts.keyReprimeIntervalMs = config.keyReprimeIntervalMs;
3739
3689
  if (config.systemActor !== null) composeOpts.systemActor = config.systemActor;
3740
- if (config.streamTicketSecret !== null) composeOpts.streamTicketSecret = config.streamTicketSecret;
3690
+ if (config.cursorSealSecret !== null) composeOpts.cursorSealSecret = config.cursorSealSecret;
3741
3691
  let idempotencyStore = null;
3742
3692
  if (!config.idempotency.disabled) {
3743
3693
  idempotencyStore = storeSql !== null ? new PgIdempotencyStore({ sql: storeSql }) : new MemoryIdempotencyStore();
@@ -4019,7 +3969,6 @@ async function bootStore(opts) {
4019
3969
  opened.push(stopKernel);
4020
3970
  const server = new FastifySubstrateServer({
4021
3971
  logger: false,
4022
- onWarn: (message) => process.stderr.write(`${message}\n`),
4023
3972
  ...config.tls !== null ? { tls: config.tls } : {},
4024
3973
  ...config.cors !== null ? { cors: config.cors } : {},
4025
3974
  ...config.bodyLimit !== null ? { bodyLimit: config.bodyLimit } : {}