@powerhousedao/switchboard 6.2.3-staging.0 → 6.3.0-dev.1

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 (271) hide show
  1. package/.tsbuild/test/ai-gateway.test.d.ts +2 -0
  2. package/.tsbuild/test/ai-gateway.test.d.ts.map +1 -0
  3. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts +2 -0
  4. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts.map +1 -0
  5. package/.tsbuild/test/boot-auth-config.test.d.ts +2 -0
  6. package/.tsbuild/test/boot-auth-config.test.d.ts.map +1 -0
  7. package/.tsbuild/test/boot-desktop-auth.test.d.ts +2 -0
  8. package/.tsbuild/test/boot-desktop-auth.test.d.ts.map +1 -0
  9. package/.tsbuild/test/boot-env-isolation.test.d.ts +2 -0
  10. package/.tsbuild/test/boot-env-isolation.test.d.ts.map +1 -0
  11. package/.tsbuild/test/boot-helpers.d.ts +5 -0
  12. package/.tsbuild/test/boot-helpers.d.ts.map +1 -0
  13. package/.tsbuild/test/boot-inspection-enabled.test.d.ts +2 -0
  14. package/.tsbuild/test/boot-inspection-enabled.test.d.ts.map +1 -0
  15. package/.tsbuild/test/boot-mcp-auth.test.d.ts +2 -0
  16. package/.tsbuild/test/boot-mcp-auth.test.d.ts.map +1 -0
  17. package/.tsbuild/test/boot-port-zero.test.d.ts +2 -0
  18. package/.tsbuild/test/boot-port-zero.test.d.ts.map +1 -0
  19. package/.tsbuild/test/boot-preconditions.test.d.ts +2 -0
  20. package/.tsbuild/test/boot-preconditions.test.d.ts.map +1 -0
  21. package/.tsbuild/test/boot-privacy.test.d.ts +2 -0
  22. package/.tsbuild/test/boot-privacy.test.d.ts.map +1 -0
  23. package/.tsbuild/test/boot-ready-drives.test.d.ts +2 -0
  24. package/.tsbuild/test/boot-ready-drives.test.d.ts.map +1 -0
  25. package/.tsbuild/test/boot-undo-v3-flag.test.d.ts +2 -0
  26. package/.tsbuild/test/boot-undo-v3-flag.test.d.ts.map +1 -0
  27. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts +2 -0
  28. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts.map +1 -0
  29. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts +2 -0
  30. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts.map +1 -0
  31. package/.tsbuild/test/config-from-env.test.d.ts +2 -0
  32. package/.tsbuild/test/config-from-env.test.d.ts.map +1 -0
  33. package/.tsbuild/test/config-merge.test.d.ts +2 -0
  34. package/.tsbuild/test/config-merge.test.d.ts.map +1 -0
  35. package/.tsbuild/test/config-schema.test.d.ts +2 -0
  36. package/.tsbuild/test/config-schema.test.d.ts.map +1 -0
  37. package/.tsbuild/test/default-composition.test.d.ts +2 -0
  38. package/.tsbuild/test/default-composition.test.d.ts.map +1 -0
  39. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts +2 -0
  40. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts.map +1 -0
  41. package/.tsbuild/test/default-drive-slug.test.d.ts +2 -0
  42. package/.tsbuild/test/default-drive-slug.test.d.ts.map +1 -0
  43. package/.tsbuild/test/delegated-publish.test.d.ts +2 -0
  44. package/.tsbuild/test/delegated-publish.test.d.ts.map +1 -0
  45. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts +2 -0
  46. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts.map +1 -0
  47. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/greetings/index.d.ts +13 -0
  48. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/greetings/index.d.ts.map +1 -0
  49. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/index.d.ts +3 -0
  50. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/index.d.ts.map +1 -0
  51. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/tally/index.d.ts +7 -0
  52. package/.tsbuild/test/fixtures/mixed-subgraphs/subgraphs/tally/index.d.ts.map +1 -0
  53. package/.tsbuild/test/inspection-throttle.test.d.ts +2 -0
  54. package/.tsbuild/test/inspection-throttle.test.d.ts.map +1 -0
  55. package/.tsbuild/test/keyframe-reclaim-store.test.d.ts +2 -0
  56. package/.tsbuild/test/keyframe-reclaim-store.test.d.ts.map +1 -0
  57. package/.tsbuild/test/observability-signals.test.d.ts +2 -0
  58. package/.tsbuild/test/observability-signals.test.d.ts.map +1 -0
  59. package/.tsbuild/test/permissions-backfill.test.d.ts +2 -0
  60. package/.tsbuild/test/permissions-backfill.test.d.ts.map +1 -0
  61. package/.tsbuild/test/pglite-preflight.test.d.ts +2 -0
  62. package/.tsbuild/test/pglite-preflight.test.d.ts.map +1 -0
  63. package/.tsbuild/test/pglite-storage-dirs.test.d.ts +2 -0
  64. package/.tsbuild/test/pglite-storage-dirs.test.d.ts.map +1 -0
  65. package/.tsbuild/test/pglite-store.test.d.ts +2 -0
  66. package/.tsbuild/test/pglite-store.test.d.ts.map +1 -0
  67. package/.tsbuild/test/plugin-config-schemas.test.d.ts +2 -0
  68. package/.tsbuild/test/plugin-config-schemas.test.d.ts.map +1 -0
  69. package/.tsbuild/test/plugin-presence.test.d.ts +2 -0
  70. package/.tsbuild/test/plugin-presence.test.d.ts.map +1 -0
  71. package/.tsbuild/test/port-probe.test.d.ts +2 -0
  72. package/.tsbuild/test/port-probe.test.d.ts.map +1 -0
  73. package/.tsbuild/test/processor-timing-log.test.d.ts +2 -0
  74. package/.tsbuild/test/processor-timing-log.test.d.ts.map +1 -0
  75. package/.tsbuild/test/renown-storage-path.test.d.ts +2 -0
  76. package/.tsbuild/test/renown-storage-path.test.d.ts.map +1 -0
  77. package/.tsbuild/test/served-schemas-mixed-package.test.d.ts +2 -0
  78. package/.tsbuild/test/served-schemas-mixed-package.test.d.ts.map +1 -0
  79. package/.tsbuild/test/served-schemas.test.d.ts +2 -0
  80. package/.tsbuild/test/served-schemas.test.d.ts.map +1 -0
  81. package/.tsbuild/test/shutdown.test.d.ts +2 -0
  82. package/.tsbuild/test/shutdown.test.d.ts.map +1 -0
  83. package/.tsbuild/test/snapshot-writer.d.ts +18 -0
  84. package/.tsbuild/test/snapshot-writer.d.ts.map +1 -0
  85. package/.tsbuild/test/store-lock.test.d.ts +2 -0
  86. package/.tsbuild/test/store-lock.test.d.ts.map +1 -0
  87. package/.tsbuild/test/two-switchboards.test.d.ts +2 -0
  88. package/.tsbuild/test/two-switchboards.test.d.ts.map +1 -0
  89. package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts +2 -0
  90. package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts.map +1 -0
  91. package/.tsbuild/test/workflow/runtime-host.test.d.ts +2 -0
  92. package/.tsbuild/test/workflow/runtime-host.test.d.ts.map +1 -0
  93. package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts +2 -0
  94. package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts.map +1 -0
  95. package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts +2 -0
  96. package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts.map +1 -0
  97. package/.tsbuild/test/workflow-reactor-access.test.d.ts +2 -0
  98. package/.tsbuild/test/workflow-reactor-access.test.d.ts.map +1 -0
  99. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts +2 -0
  100. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts.map +1 -0
  101. package/.tsbuild/test/workflows-plugin-config.test.d.ts +2 -0
  102. package/.tsbuild/test/workflows-plugin-config.test.d.ts.map +1 -0
  103. package/.tsbuild/tsconfig.tsbuildinfo +1 -1
  104. package/Auth.md +108 -2
  105. package/CHANGELOG.md +1065 -1
  106. package/README.md +69 -24
  107. package/dist/boot.d.mts +46 -0
  108. package/dist/boot.d.mts.map +1 -0
  109. package/dist/boot.mjs +67 -0
  110. package/dist/boot.mjs.map +1 -0
  111. package/dist/config/from-env.d.mts +15 -0
  112. package/dist/config/from-env.d.mts.map +1 -0
  113. package/dist/config/from-env.mjs +4 -0
  114. package/dist/config/schema.d.mts +3 -0
  115. package/dist/config/schema.mjs +4 -0
  116. package/dist/from-env-DKK_wPrO.mjs +497 -0
  117. package/dist/from-env-DKK_wPrO.mjs.map +1 -0
  118. package/dist/index.d.mts +1 -2
  119. package/dist/index.mjs +40 -219
  120. package/dist/index.mjs.map +1 -1
  121. package/dist/mcp-VW52B8KD.mjs +32 -0
  122. package/dist/mcp-VW52B8KD.mjs.map +1 -0
  123. package/dist/migrate.mjs +5 -4
  124. package/dist/migrate.mjs.map +1 -1
  125. package/dist/observability-CldagyT_.mjs +224 -0
  126. package/dist/observability-CldagyT_.mjs.map +1 -0
  127. package/dist/observability.d.mts +30 -0
  128. package/dist/observability.d.mts.map +1 -0
  129. package/dist/observability.mjs +4 -0
  130. package/dist/plugin-host-tTjDTNz0.d.mts +46 -0
  131. package/dist/plugin-host-tTjDTNz0.d.mts.map +1 -0
  132. package/dist/plugins/ai.d.mts +37 -0
  133. package/dist/plugins/ai.d.mts.map +1 -0
  134. package/dist/plugins/ai.mjs +400 -0
  135. package/dist/plugins/ai.mjs.map +1 -0
  136. package/dist/plugins/attachments.d.mts +33 -0
  137. package/dist/plugins/attachments.d.mts.map +1 -0
  138. package/dist/plugins/attachments.mjs +4 -0
  139. package/dist/plugins/mcp.d.mts +31 -0
  140. package/dist/plugins/mcp.d.mts.map +1 -0
  141. package/dist/plugins/mcp.mjs +4 -0
  142. package/dist/plugins/privacy.d.mts +22 -0
  143. package/dist/plugins/privacy.d.mts.map +1 -0
  144. package/dist/plugins/privacy.mjs +4 -0
  145. package/dist/plugins/workflows.d.mts +80 -0
  146. package/dist/plugins/workflows.d.mts.map +1 -0
  147. package/dist/plugins/workflows.mjs +4 -0
  148. package/dist/postgres-url-DTDhBjXX.mjs +11 -0
  149. package/dist/postgres-url-DTDhBjXX.mjs.map +1 -0
  150. package/dist/privacy-HQnhqf2H.mjs +91 -0
  151. package/dist/privacy-HQnhqf2H.mjs.map +1 -0
  152. package/dist/privacy-UodgY0i_.mjs +44 -0
  153. package/dist/privacy-UodgY0i_.mjs.map +1 -0
  154. package/dist/renown-layers-DmbWLt46.mjs +42 -0
  155. package/dist/renown-layers-DmbWLt46.mjs.map +1 -0
  156. package/dist/schema-BJ-OFAox.mjs +359 -0
  157. package/dist/schema-BJ-OFAox.mjs.map +1 -0
  158. package/dist/schema-w1lShimw.d.mts +258 -0
  159. package/dist/schema-w1lShimw.d.mts.map +1 -0
  160. package/dist/server-BS-PFzoK.mjs +2752 -0
  161. package/dist/server-BS-PFzoK.mjs.map +1 -0
  162. package/dist/server.d.mts +94 -228
  163. package/dist/server.d.mts.map +1 -1
  164. package/dist/server.mjs +5 -4
  165. package/dist/types-Bk4T4D9F.d.mts +318 -0
  166. package/dist/types-Bk4T4D9F.d.mts.map +1 -0
  167. package/dist/utils-Cm0TE6dP.mjs +148 -0
  168. package/dist/utils-Cm0TE6dP.mjs.map +1 -0
  169. package/dist/utils.d.mts +29 -3
  170. package/dist/utils.d.mts.map +1 -1
  171. package/dist/utils.mjs +5 -4
  172. package/dist/worker-pool-gyxJnCxA.mjs +304 -0
  173. package/dist/worker-pool-gyxJnCxA.mjs.map +1 -0
  174. package/dist/workflow-runtime-CF5pc8mt.d.mts +33 -0
  175. package/dist/workflow-runtime-CF5pc8mt.d.mts.map +1 -0
  176. package/dist/workflows-mdYUh3Us.mjs +1947 -0
  177. package/dist/workflows-mdYUh3Us.mjs.map +1 -0
  178. package/package.json +86 -24
  179. package/scripts/install-packages.sh +0 -0
  180. package/test/ai-gateway.test.ts +612 -0
  181. package/test/attachment-reference-read-model.test.ts +28 -20
  182. package/test/attachments/byte-grants.test.ts +4 -1
  183. package/test/attachments/download-target.test.ts +36 -20
  184. package/test/attachments/index.test.ts +13 -1
  185. package/test/attachments/reserve-metadata-parity.test.ts +96 -0
  186. package/test/attachments/routes-integration.test.ts +1 -0
  187. package/test/attachments/routes.test.ts +132 -12
  188. package/test/attachments/service-config.test.ts +28 -16
  189. package/test/boot-auth-config.test.ts +146 -0
  190. package/test/boot-create-signature-policy.test.ts +15 -13
  191. package/test/boot-desktop-auth.test.ts +278 -0
  192. package/test/boot-env-isolation.test.ts +26 -0
  193. package/test/boot-helpers.ts +19 -0
  194. package/test/boot-inspection-enabled.test.ts +140 -0
  195. package/test/boot-mcp-auth.test.ts +449 -0
  196. package/test/boot-mcp-enabled.test.ts +27 -27
  197. package/test/boot-port-zero.test.ts +101 -0
  198. package/test/boot-preconditions.test.ts +213 -0
  199. package/test/boot-privacy.test.ts +232 -0
  200. package/test/boot-ready-drives.test.ts +141 -0
  201. package/test/boot-undo-v3-flag.test.ts +120 -0
  202. package/test/boot-unsupported-stored-documents.test.ts +23 -19
  203. package/test/boot-without-renown.test.ts +11 -9
  204. package/test/boot-without-require-signatures.test.ts +12 -10
  205. package/test/boot-worker-trust-policy.test.ts +11 -10
  206. package/test/boot-workflow-singleton-conflict.test.ts +109 -0
  207. package/test/boot-workflow-singleton-retry.test.ts +122 -0
  208. package/test/config-from-env.test.ts +968 -0
  209. package/test/config-merge.test.ts +351 -0
  210. package/test/config-schema.test.ts +183 -0
  211. package/test/default-composition.test.ts +290 -0
  212. package/test/default-drive-protocol-versions.test.ts +79 -0
  213. package/test/default-drive-slug.test.ts +265 -0
  214. package/test/delegated-publish.test.ts +435 -0
  215. package/test/durable-node-fs-pg16.test.ts +80 -0
  216. package/test/fatal-shutdown.test.ts +104 -1
  217. package/test/fixtures/mixed-subgraphs/subgraphs/greetings/index.ts +16 -0
  218. package/test/fixtures/mixed-subgraphs/subgraphs/index.ts +3 -0
  219. package/test/fixtures/mixed-subgraphs/subgraphs/tally/index.ts +73 -0
  220. package/test/goldens/packages.graphql +797 -0
  221. package/test/goldens/privacy.graphql +853 -0
  222. package/test/goldens/reactor-drive-nodes.graphql +827 -0
  223. package/test/goldens/resolvers.json +327 -0
  224. package/test/goldens/supergraph.graphql +5918 -0
  225. package/test/goldens/workflow-runtime.graphql +1548 -0
  226. package/test/inspection-throttle.test.ts +23 -0
  227. package/test/keyframe-reclaim-store.test.ts +156 -0
  228. package/test/observability-signals.test.ts +131 -0
  229. package/test/permissions-backfill.test.ts +486 -0
  230. package/test/pglite-dialect.test.ts +237 -2
  231. package/test/pglite-preflight.test.ts +313 -0
  232. package/test/pglite-storage-dirs.test.ts +113 -0
  233. package/test/pglite-store.test.ts +441 -0
  234. package/test/pglite-version.test.ts +9 -1
  235. package/test/plugin-config-schemas.test.ts +83 -0
  236. package/test/plugin-presence.test.ts +262 -0
  237. package/test/port-probe.test.ts +45 -0
  238. package/test/processor-timing-log.test.ts +109 -0
  239. package/test/reactor-feature-flags.test.ts +20 -0
  240. package/test/redact-webhook-path.test.ts +9 -0
  241. package/test/renown-storage-path.test.ts +40 -0
  242. package/test/renown-trust-policy.test.ts +7 -9
  243. package/test/served-schemas-mixed-package.test.ts +251 -0
  244. package/test/served-schemas.test.ts +134 -0
  245. package/test/shutdown.test.ts +278 -0
  246. package/test/snapshot-writer.ts +94 -0
  247. package/test/store-lock.test.ts +65 -0
  248. package/test/two-switchboards.test.ts +146 -0
  249. package/test/worker-pool.test.ts +27 -1
  250. package/test/workflow/oauth-apps-config.test.ts +66 -0
  251. package/test/workflow/oauth.test.ts +82 -10
  252. package/test/workflow/resolvers.test.ts +230 -38
  253. package/test/workflow/runtime-host.test.ts +19 -0
  254. package/test/workflow/waitpoint-routes.test.ts +306 -0
  255. package/test/workflow/waitpoint-runtime.test.ts +204 -0
  256. package/test/workflow/webhook-trigger-test.test.ts +8 -0
  257. package/test/workflow-reactor-access.test.ts +384 -0
  258. package/test/workflow-reactor-enforcement.test.ts +808 -0
  259. package/test/workflow-read-gate.test.ts +128 -60
  260. package/test/workflow-runtime.test.ts +1005 -237
  261. package/test/workflows-plugin-config.test.ts +77 -0
  262. package/tsconfig.json +5 -2
  263. package/tsdown.config.ts +12 -0
  264. package/vitest.config.ts +66 -1
  265. package/.tsbuild/test/mcp-flag.test.d.ts +0 -2
  266. package/.tsbuild/test/mcp-flag.test.d.ts.map +0 -1
  267. package/dist/server-sQoWp21G.mjs +0 -2939
  268. package/dist/server-sQoWp21G.mjs.map +0 -1
  269. package/dist/utils-uaBlzyjS.mjs +0 -86
  270. package/dist/utils-uaBlzyjS.mjs.map +0 -1
  271. package/test/mcp-flag.test.ts +0 -56
@@ -0,0 +1,968 @@
1
+ import { parseCatchUpPriority } from "@powerhousedao/reactor";
2
+ import {
3
+ reactorApiConfigFromEnv,
4
+ resolveCredentialCacheTtlMs,
5
+ resolveRenownConfig,
6
+ } from "@powerhousedao/reactor-api";
7
+ import {
8
+ resolvePgliteFsync,
9
+ resolvePgliteMaintenance,
10
+ resolvePgliteWorkerMemoryMb,
11
+ } from "@powerhousedao/reactor-api/pglite-node";
12
+ import { parseAttachmentStorageConfig } from "@powerhousedao/reactor-attachments";
13
+ import { defaultStoreDirs } from "@powerhousedao/shared/clis/ph-dir";
14
+ import type { ILogger } from "document-model";
15
+ import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
16
+ import { resolveAttachmentUrlSigning } from "../src/attachments/url-signer.js";
17
+ import { reactorDbUrlOf } from "../src/boot-preconditions.mjs";
18
+ import { switchboardConfigFromEnv } from "../src/config/from-env.js";
19
+ import {
20
+ mergeSwitchboardConfig,
21
+ switchboardConfigSchema,
22
+ type SwitchboardConfig,
23
+ } from "../src/config/schema.js";
24
+ import { resolveCreateSignaturePolicy } from "../src/create-signature-policy.mjs";
25
+ import { EnvVarProvider } from "@openfeature/env-var-provider";
26
+ import { OpenFeature } from "@openfeature/server-sdk";
27
+ import { binSwitchboardConfig } from "../src/config/bin.js";
28
+ import { inspectionThrottleFromEnv } from "../src/inspection-throttle.mjs";
29
+ import { pluginHostFromEnv } from "../src/plugin-host.mjs";
30
+ import { resolvePrivacy } from "../src/privacy.mjs";
31
+ import { resolveProjectionWorkerOptions } from "../src/projection-worker.mjs";
32
+ import { resolveReactorFeatureFlags } from "../src/reactor-feature-flags.mjs";
33
+ import { resolveUnsupportedStoredDocuments } from "../src/unsupported-stored-documents.mjs";
34
+ import {
35
+ resolveHostPoolSize,
36
+ resolveWorkerPoolOptions,
37
+ } from "../src/worker-pool.mjs";
38
+
39
+ // The OpenFeature client the boot read these switches through before the config.
40
+ type BooleanFlagSource = {
41
+ getBooleanValue(flagKey: string, defaultValue: boolean): Promise<boolean>;
42
+ };
43
+
44
+ async function initFeatureFlags(): Promise<BooleanFlagSource> {
45
+ await OpenFeature.setProviderAndWait(new EnvVarProvider());
46
+ return OpenFeature.getClient();
47
+ }
48
+
49
+ // PH_WORKFLOWS_ENABLED's 1/0 forms, then the client, then the config file's (off).
50
+ function workflowsBefore(featureFlags: BooleanFlagSource, env: Env) {
51
+ const raw = env.PH_WORKFLOWS_ENABLED?.trim();
52
+ if (raw === "1") return Promise.resolve(true);
53
+ if (raw === "0") return Promise.resolve(false);
54
+ return featureFlags.getBooleanValue("PH_WORKFLOWS_ENABLED", false);
55
+ }
56
+
57
+ type Env = Record<string, string | undefined>;
58
+
59
+ // Stubbed per row: the real OpenFeature provider reads process.env.
60
+ const KEYS = [
61
+ "LOG_LEVEL",
62
+ "PORT",
63
+ "BASE_PATH",
64
+ "PUBLIC_URL",
65
+ "RENDER_EXTERNAL_URL",
66
+ "HEROKU_APP_DEFAULT_DOMAIN_NAME",
67
+ "GATEWAY_ADAPTER",
68
+ "PH_REACTOR_DATABASE_URL",
69
+ "PH_SWITCHBOARD_DATABASE_URL",
70
+ "DATABASE_URL",
71
+ "PH_PGLITE_IN_MEMORY",
72
+ "PH_USER_DATA_DIR",
73
+ "PH_PGLITE_FSYNC",
74
+ "PH_PGLITE_WORKERS",
75
+ "PH_PGLITE_WORKER_MEMORY_MB",
76
+ "PH_PGLITE_VACUUM_FULL",
77
+ "PH_PGLITE_VACUUM_FULL_ABOVE_MB",
78
+ "PH_PGLITE_VACUUM_FULL_QUIET_MS",
79
+ "PH_PGLITE_VACUUM_FULL_MAX_DEFER_MS",
80
+ "REACTOR_DOCUMENT_DECISIONS",
81
+ "REACTOR_AUTH_ENFORCEMENT",
82
+ "REACTOR_AUTH_GROUPS",
83
+ "REACTOR_AUTH_CONDITIONS",
84
+ "CREATE_SIGNATURE_POLICY",
85
+ "REACTOR_UNSUPPORTED_STORED_DOCUMENTS",
86
+ "REACTOR_LEGACY_PROCESSOR_IDS",
87
+ "MAX_SKIP_THRESHOLD",
88
+ "PH_CATCH_UP_PRIORITY",
89
+ "PH_TELEMETRY",
90
+ "PH_TELEMETRY_DIR",
91
+ "PH_PROCESSOR_TIMING",
92
+ "PH_PROCESSOR_TIMING_WINDOW_MS",
93
+ "PH_PROCESSOR_SLOW_BATCH_MS",
94
+ "PH_PROCESSOR_TIMING_LOG_MS",
95
+ "PH_MEMORY_TELEMETRY",
96
+ "PH_MEMORY_TELEMETRY_INTERVAL_MS",
97
+ "PH_MEMORY_TELEMETRY_OUTPUT",
98
+ "PH_MEMORY_TELEMETRY_FILE",
99
+ "PH_MEMORY_TELEMETRY_FILE_MAX_MB",
100
+ "PH_MEMORY_TELEMETRY_FILE_KEEP",
101
+ "PH_MEMORY_TELEMETRY_RING",
102
+ "PH_MEMORY_TELEMETRY_CONSOLE",
103
+ "PH_MEMORY_TELEMETRY_CONSOLE_INTERVAL_MS",
104
+ "PH_MEMORY_TELEMETRY_WORKERS",
105
+ "PH_MEMORY_TELEMETRY_WINDOW_MIN",
106
+ "PH_HEAP_SNAPSHOT_DIR",
107
+ "PH_KEYFRAMES",
108
+ "AUTH_ENABLED",
109
+ "RESOLVE_CALLER_IDENTITY",
110
+ "REQUIRE_AUTHENTICATED_CALLER",
111
+ "REQUIRE_AUTHENTICATED_CALLER_EXEMPT_PATHS",
112
+ "ATTACHMENT_READS_FOLLOW_DOCUMENT_POLICY",
113
+ "ADMINS",
114
+ "DEFAULT_PROTECTION",
115
+ "DOCUMENT_PERMISSIONS_ENABLED",
116
+ "DOCUMENT_PERMISSION_CACHE_TTL_MS",
117
+ "SKIP_CREDENTIAL_VERIFICATION",
118
+ "ALLOW_INSECURE_SKIP_CREDENTIAL_VERIFICATION",
119
+ "CREDENTIAL_VERIFICATION_CACHE_TTL_MS",
120
+ "WS_CREDENTIAL_RECHECK_INTERVAL_MS",
121
+ "RENOWN_SOURCE",
122
+ "RENOWN_URL",
123
+ "SWITCHBOARD_URL",
124
+ "INSPECTION_ENABLED",
125
+ "INSPECTION_ADMIN_ENABLED",
126
+ "PH_INSPECTION_CALLS_PER_MINUTE",
127
+ "REACTOR_WORKERS",
128
+ "REACTOR_DB_POOL_SIZE_WORKER",
129
+ "REACTOR_DB_ACQUIRE_TIMEOUT_MS",
130
+ "REACTOR_DB_POOL_SIZE_HOST",
131
+ "REACTOR_PROJECTION_WORKER",
132
+ "REACTOR_DB_POOL_SIZE_PROJECTION",
133
+ "PH_REGISTRY_URL",
134
+ "PH_REGISTRY_PACKAGES",
135
+ "DYNAMIC_MODEL_LOADING",
136
+ "MCP_ENABLED",
137
+ "PH_WORKFLOWS_ENABLED",
138
+ "DOCUMENT_MODEL_SUBGRAPHS_ENABLED",
139
+ "FEATURE_PLUGIN_HOST_ENABLED",
140
+ "PH_PRIVACY_ENABLED",
141
+ "PH_PRIVACY_DEPLOYMENT_SECRET",
142
+ "PH_PRIVACY_INTERVAL_MS",
143
+ "PH_PRIVACY_DEADLINE_DAYS",
144
+ "PH_PRIVACY_MARKER_GRACE_DAYS",
145
+ "PH_PRIVACY_PURGE_TIMEOUT_MINUTES",
146
+ "PH_ATTACHMENT_STORAGE",
147
+ "PH_ATTACHMENT_S3_ENDPOINT",
148
+ "PH_ATTACHMENT_S3_REGION",
149
+ "PH_ATTACHMENT_S3_BUCKET",
150
+ "PH_ATTACHMENT_S3_ACCESS_KEY_ID",
151
+ "PH_ATTACHMENT_S3_SECRET_ACCESS_KEY",
152
+ "PH_ATTACHMENT_URL_SIGNING_SECRET",
153
+ "ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS",
154
+ "PH_SWITCHBOARD_PUBLIC_URL",
155
+ "PH_SWITCHBOARD_PORT",
156
+ "PH_MIGRATE_PGLITE",
157
+ "PH_FORCE_PG_VERSION",
158
+ "PH_DEFAULT_DRIVE_TYPE",
159
+ "NODE_ENV",
160
+ ];
161
+
162
+ const SECRET = "s".repeat(32);
163
+ const CORES = 8;
164
+
165
+ const ROWS: Record<string, Env> = {
166
+ empty: {},
167
+ "a typical dev setup": {
168
+ LOG_LEVEL: "debug",
169
+ PORT: "4002",
170
+ BASE_PATH: "/sb",
171
+ PH_USER_DATA_DIR: "/home/dev/.local/share/connect",
172
+ PH_PGLITE_WORKERS: "false",
173
+ PH_PGLITE_FSYNC: "0",
174
+ MCP_ENABLED: "false",
175
+ PH_WORKFLOWS_ENABLED: "1",
176
+ DYNAMIC_MODEL_LOADING: "true",
177
+ PH_REGISTRY_URL: "http://localhost:8080",
178
+ PH_REGISTRY_PACKAGES: "@acme/a, @acme/b",
179
+ FEATURE_PLUGIN_HOST_ENABLED: "1",
180
+ PH_CATCH_UP_PRIORITY: "3",
181
+ },
182
+ "Postgres with workers": {
183
+ PH_REACTOR_DATABASE_URL: "postgres://u:p@db:5432/reactor",
184
+ DATABASE_URL: "postgres://u:p@db:5432/read",
185
+ REACTOR_WORKERS: "auto",
186
+ REACTOR_DB_POOL_SIZE_WORKER: "4",
187
+ REACTOR_DB_ACQUIRE_TIMEOUT_MS: "2000",
188
+ REACTOR_DB_POOL_SIZE_HOST: "32",
189
+ REACTOR_PROJECTION_WORKER: "Yes",
190
+ REACTOR_DB_POOL_SIZE_PROJECTION: "6",
191
+ RENOWN_SOURCE: "remote",
192
+ MAX_SKIP_THRESHOLD: "50",
193
+ REACTOR_LEGACY_PROCESSOR_IDS: "false",
194
+ REACTOR_UNSUPPORTED_STORED_DOCUMENTS: "read-only",
195
+ },
196
+ "auth on": {
197
+ AUTH_ENABLED: "true",
198
+ ADMINS: "0xABC,0xdef",
199
+ DEFAULT_PROTECTION: "TRUE",
200
+ DOCUMENT_PERMISSIONS_ENABLED: "true",
201
+ DOCUMENT_PERMISSION_CACHE_TTL_MS: "1000",
202
+ REQUIRE_AUTHENTICATED_CALLER: "true",
203
+ REQUIRE_AUTHENTICATED_CALLER_EXEMPT_PATHS: "/health, ,/ready ",
204
+ RENOWN_SOURCE: "self",
205
+ RENOWN_URL: " https://renown.example ",
206
+ CREDENTIAL_VERIFICATION_CACHE_TTL_MS: "500",
207
+ WS_CREDENTIAL_RECHECK_INTERVAL_MS: "0",
208
+ REACTOR_DOCUMENT_DECISIONS: "true",
209
+ REACTOR_AUTH_ENFORCEMENT: "true",
210
+ CREATE_SIGNATURE_POLICY: " legacy ",
211
+ PH_PRIVACY_ENABLED: "true",
212
+ PH_PRIVACY_DEPLOYMENT_SECRET: SECRET,
213
+ PH_PRIVACY_DEADLINE_DAYS: "2",
214
+ PH_PRIVACY_PURGE_TIMEOUT_MINUTES: "5",
215
+ },
216
+ "inspection admin": {
217
+ INSPECTION_ENABLED: "true",
218
+ INSPECTION_ADMIN_ENABLED: "true",
219
+ PH_INSPECTION_CALLS_PER_MINUTE: "120",
220
+ },
221
+ "inspection off, legacy storage chain": {
222
+ INSPECTION_ENABLED: "false",
223
+ PH_SWITCHBOARD_DATABASE_URL: "postgres://u:p@db/sb",
224
+ PH_PGLITE_IN_MEMORY: "1",
225
+ PH_PGLITE_WORKER_MEMORY_MB: "512",
226
+ PH_PGLITE_VACUUM_FULL: " Forced",
227
+ PH_PGLITE_VACUUM_FULL_ABOVE_MB: "64",
228
+ PH_PGLITE_VACUUM_FULL_QUIET_MS: "nope",
229
+ DOCUMENT_MODEL_SUBGRAPHS_ENABLED: "false",
230
+ PH_WORKFLOWS_ENABLED: "TRUE",
231
+ },
232
+ "attachments on S3": {
233
+ PH_ATTACHMENT_STORAGE: "s3",
234
+ PH_ATTACHMENT_S3_ENDPOINT: "https://s3.example.com/",
235
+ PH_ATTACHMENT_S3_REGION: "eu-west-1",
236
+ PH_ATTACHMENT_S3_BUCKET: "docs",
237
+ PH_ATTACHMENT_S3_ACCESS_KEY_ID: "id",
238
+ PH_ATTACHMENT_S3_SECRET_ACCESS_KEY: "key",
239
+ PH_ATTACHMENT_URL_SIGNING_SECRET: SECRET,
240
+ ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS: "60",
241
+ PH_SWITCHBOARD_PUBLIC_URL: "https://sb.example",
242
+ PUBLIC_URL: "",
243
+ HEROKU_APP_DEFAULT_DOMAIN_NAME: "sb.herokuapp.com",
244
+ NODE_ENV: "production",
245
+ },
246
+ "blank values": {
247
+ DATABASE_URL: "",
248
+ PH_SWITCHBOARD_DATABASE_URL: "postgres://u:p@db/sb",
249
+ PH_USER_DATA_DIR: "",
250
+ PORT: "",
251
+ LOG_LEVEL: "",
252
+ RENOWN_SOURCE: " ",
253
+ REACTOR_WORKERS: " ",
254
+ MCP_ENABLED: "",
255
+ PH_REGISTRY_URL: "",
256
+ },
257
+ };
258
+
259
+ const silent = { warn: () => undefined } as unknown as ILogger;
260
+
261
+ function stubRow(row: Env): void {
262
+ for (const key of KEYS) vi.stubEnv(key, row[key]);
263
+ }
264
+
265
+ function mapped(row: Env): SwitchboardConfig {
266
+ return switchboardConfigSchema.parse(
267
+ switchboardConfigFromEnv(row, { logger: silent }),
268
+ );
269
+ }
270
+
271
+ describe("switchboardConfigFromEnv matches today's env reads", () => {
272
+ let featureFlags: BooleanFlagSource;
273
+
274
+ beforeAll(async () => {
275
+ featureFlags = await initFeatureFlags();
276
+ });
277
+
278
+ afterEach(() => {
279
+ vi.unstubAllEnvs();
280
+ });
281
+
282
+ describe.each(Object.entries(ROWS))("%s", (_name, row) => {
283
+ it("server and storage", () => {
284
+ stubRow(row);
285
+ const config = mapped(row);
286
+ expect(config.logLevel).toBe(row.LOG_LEVEL || "info");
287
+ expect(config.server.port).toBe(row.PORT ? Number(row.PORT) : 4001);
288
+ expect(config.server.basePath).toBe(row.BASE_PATH || "/");
289
+ expect(config.storage.reactor.url).toBe(
290
+ reactorDbUrlOf(undefined, row) || undefined,
291
+ );
292
+ expect(config.storage.readModel.url).toBe(
293
+ (row.DATABASE_URL ?? row.PH_SWITCHBOARD_DATABASE_URL) || undefined,
294
+ );
295
+ expect(config.storage.inMemory).toBe(row.PH_PGLITE_IN_MEMORY === "1");
296
+ expect(
297
+ defaultStoreDirs({ PH_USER_DATA_DIR: config.storage.userDataDir }),
298
+ ).toEqual(defaultStoreDirs(row));
299
+ expect(config.storage.pglite.fsync).toBe(resolvePgliteFsync(row));
300
+ expect(
301
+ config.storage.pglite.workerMemoryMb ?? resolvePgliteWorkerMemoryMb({}),
302
+ ).toBe(resolvePgliteWorkerMemoryMb(row));
303
+ const vacuum = config.storage.pglite.vacuumFull;
304
+ expect(
305
+ resolvePgliteMaintenance({
306
+ PH_PGLITE_VACUUM_FULL: vacuum.mode,
307
+ PH_PGLITE_VACUUM_FULL_ABOVE_MB: vacuum.aboveMb?.toString(),
308
+ PH_PGLITE_VACUUM_FULL_QUIET_MS: vacuum.quietMs?.toString(),
309
+ PH_PGLITE_VACUUM_FULL_MAX_DEFER_MS: vacuum.maxDeferMs?.toString(),
310
+ }),
311
+ ).toEqual(resolvePgliteMaintenance(row));
312
+ });
313
+
314
+ it("feature switches, as the OpenFeature client reads them", async () => {
315
+ stubRow(row);
316
+ const config = mapped(row);
317
+ await expect(
318
+ featureFlags.getBooleanValue("PH_PGLITE_WORKERS", true),
319
+ ).resolves.toBe(config.storage.pglite.workers);
320
+ await expect(
321
+ featureFlags.getBooleanValue("INSPECTION_ENABLED", true),
322
+ ).resolves.toBe(config.inspection.enabled);
323
+ await expect(
324
+ featureFlags.getBooleanValue("MCP_ENABLED", true),
325
+ ).resolves.toBe(config.features.mcp ?? true);
326
+ await expect(workflowsBefore(featureFlags, row)).resolves.toBe(
327
+ config.features.workflows ?? false,
328
+ );
329
+ await expect(
330
+ featureFlags.getBooleanValue("DOCUMENT_MODEL_SUBGRAPHS_ENABLED", true),
331
+ ).resolves.toBe(config.features.documentModelSubgraphs);
332
+ // Unset stays unset: it selects the default composition, not the flag.
333
+ expect(config.features.pluginHost).toBe(
334
+ row.FEATURE_PLUGIN_HOST_ENABLED === undefined
335
+ ? undefined
336
+ : pluginHostFromEnv(row),
337
+ );
338
+ });
339
+
340
+ it("protocol", () => {
341
+ const { protocol } = mapped(row);
342
+ expect(protocol.featureFlags).toEqual(
343
+ resolveReactorFeatureFlags(row).flags,
344
+ );
345
+ for (const hasSigner of [true, false]) {
346
+ expect(
347
+ resolveCreateSignaturePolicy(
348
+ { CREATE_SIGNATURE_POLICY: protocol.createSignaturePolicy },
349
+ { hasSigner },
350
+ ),
351
+ ).toBe(resolveCreateSignaturePolicy(row, { hasSigner }));
352
+ }
353
+ expect(protocol.unsupportedStoredDocuments).toBe(
354
+ resolveUnsupportedStoredDocuments(undefined, row),
355
+ );
356
+ expect(protocol.legacyProcessorIds).toBe(
357
+ row.REACTOR_LEGACY_PROCESSOR_IDS !== "false",
358
+ );
359
+ const skip = parseInt(row.MAX_SKIP_THRESHOLD ?? "", 10);
360
+ expect(protocol.maxSkipThreshold).toBe(
361
+ !isNaN(skip) && skip > 0 ? skip : undefined,
362
+ );
363
+ const priority = parseCatchUpPriority(row.PH_CATCH_UP_PRIORITY) ?? {
364
+ mode: "dynamic",
365
+ };
366
+ expect(protocol.catchUpPriority).toBe(
367
+ priority.mode === "fixed" ? priority.level : "dynamic",
368
+ );
369
+ });
370
+
371
+ it("workers", () => {
372
+ const { workers } = mapped(row);
373
+ expect(
374
+ resolveWorkerPoolOptions(
375
+ {
376
+ numWorkers: workers.executor.count,
377
+ dbPoolSizePerWorker: workers.executor.poolSize,
378
+ acquireTimeoutMs: workers.executor.acquireTimeoutMs,
379
+ },
380
+ {},
381
+ CORES,
382
+ ),
383
+ ).toEqual(resolveWorkerPoolOptions(undefined, row, CORES));
384
+ expect(workers.hostPoolSize).toBe(resolveHostPoolSize(row));
385
+ expect(
386
+ resolveProjectionWorkerOptions(
387
+ {
388
+ enabled: workers.projection.enabled,
389
+ dbPoolSize: workers.projection.poolSize,
390
+ },
391
+ {},
392
+ ),
393
+ ).toEqual(resolveProjectionWorkerOptions(undefined, row));
394
+ });
395
+
396
+ it("inspection, packages, privacy and attachments", () => {
397
+ const config = mapped(row);
398
+ expect(config.inspection.admin).toBe(
399
+ row.INSPECTION_ADMIN_ENABLED === "true",
400
+ );
401
+ expect(config.inspection.callsPerMinute).toBe(
402
+ inspectionThrottleFromEnv(row).callsPerMinute,
403
+ );
404
+ expect(config.packages.registryUrl).toBe(row.PH_REGISTRY_URL);
405
+ expect(config.packages.registryPackages).toEqual(
406
+ row.PH_REGISTRY_PACKAGES?.split(",").map((p) => p.trim()) ?? [],
407
+ );
408
+ expect(config.packages.dynamic).toBe(
409
+ row.DYNAMIC_MODEL_LOADING === "true",
410
+ );
411
+ const privacy = config.features.privacy;
412
+ expect(
413
+ privacy.enabled
414
+ ? {
415
+ enabled: true,
416
+ deploymentSecret: privacy.deploymentSecret,
417
+ intervalMs: privacy.intervalMs,
418
+ deadlineMs: privacy.deadlineMs,
419
+ markerGraceMs: privacy.markerGraceMs,
420
+ purgeTimeoutMs: privacy.purgeTimeoutMs,
421
+ }
422
+ : { enabled: false },
423
+ ).toEqual(resolvePrivacy(undefined, row));
424
+ expect(config.attachments.storage).toEqual(
425
+ parseAttachmentStorageConfig(row),
426
+ );
427
+ expect(
428
+ resolveAttachmentUrlSigning({
429
+ PH_ATTACHMENT_URL_SIGNING_SECRET: config.attachments.urlSigningSecret,
430
+ NODE_ENV: row.NODE_ENV,
431
+ }).status,
432
+ ).toBe(resolveAttachmentUrlSigning(row).status);
433
+ expect(config.attachments.serviceUrl).toBe(row.PH_SWITCHBOARD_PUBLIC_URL);
434
+ expect(config.attachments.downloadTargetMaxTtlSeconds).toBe(
435
+ row.ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS === undefined
436
+ ? undefined
437
+ : Number(row.ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS),
438
+ );
439
+ });
440
+
441
+ it("renown", () => {
442
+ const { auth } = mapped(row);
443
+ const today = resolveRenownConfig(undefined, row, silent);
444
+ expect({
445
+ source: auth.renown.source ?? "remote",
446
+ url: auth.renown.url,
447
+ switchboardUrl: auth.renown.switchboardUrl,
448
+ }).toEqual(today);
449
+ expect(
450
+ auth.credentialCacheTtlMs ?? (today.source === "self" ? 0 : undefined),
451
+ ).toBe(
452
+ resolveCredentialCacheTtlMs(
453
+ row.CREDENTIAL_VERIFICATION_CACHE_TTL_MS,
454
+ today.source,
455
+ silent,
456
+ ),
457
+ );
458
+ });
459
+ });
460
+
461
+ it.each(Object.entries(ROWS))(
462
+ "takes the reactor-api sections from reactorApiConfigFromEnv: %s",
463
+ (_name, row) => {
464
+ const layer = switchboardConfigFromEnv(row, { logger: silent });
465
+ const api = reactorApiConfigFromEnv(row, silent);
466
+ const { renown: _renown, ...auth } = layer.auth ?? {};
467
+ expect(Object.keys(auth).length > 0 ? auth : undefined).toEqual(api.auth);
468
+ expect(layer.inspection?.admin).toBe(api.inspection?.admin);
469
+ expect(layer.attachments?.storage).toEqual(api.attachments?.storage);
470
+ const { port: _port, ...server } = layer.server ?? {};
471
+ expect(Object.keys(server).length > 0 ? server : undefined).toEqual(
472
+ api.server,
473
+ );
474
+ },
475
+ );
476
+
477
+ it("names only the keys the env sets", () => {
478
+ expect(switchboardConfigFromEnv({}, { logger: silent })).toEqual({});
479
+ expect(
480
+ switchboardConfigFromEnv(
481
+ { REACTOR_AUTH_GROUPS: "true", PH_PGLITE_VACUUM_FULL: "quiet" },
482
+ { logger: silent },
483
+ ),
484
+ ).toStrictEqual({
485
+ storage: { pglite: { vacuumFull: { mode: "quiet" } } },
486
+ protocol: { featureFlags: { authGroups: true } },
487
+ });
488
+ });
489
+ });
490
+
491
+ describe("switchboardConfigFromEnv errors", () => {
492
+ const messageOf = (fn: () => unknown): string => {
493
+ try {
494
+ fn();
495
+ } catch (error) {
496
+ return (error as Error).message;
497
+ }
498
+ throw new Error("expected today's read to throw");
499
+ };
500
+
501
+ const POSTGRES = "postgres://u:p@db/reactor";
502
+
503
+ it.each<[string, Env, () => unknown]>([
504
+ [
505
+ "REACTOR_WORKERS",
506
+ { REACTOR_WORKERS: "two" },
507
+ () => resolveWorkerPoolOptions(undefined, { REACTOR_WORKERS: "two" }),
508
+ ],
509
+ [
510
+ "REACTOR_DB_POOL_SIZE_WORKER with workers on",
511
+ { REACTOR_WORKERS: "2", REACTOR_DB_POOL_SIZE_WORKER: "-1" },
512
+ () =>
513
+ resolveWorkerPoolOptions(undefined, {
514
+ REACTOR_WORKERS: "2",
515
+ REACTOR_DB_POOL_SIZE_WORKER: "-1",
516
+ }),
517
+ ],
518
+ [
519
+ "REACTOR_DB_POOL_SIZE_HOST with a Postgres reactor",
520
+ { PH_REACTOR_DATABASE_URL: POSTGRES, REACTOR_DB_POOL_SIZE_HOST: "0" },
521
+ () => resolveHostPoolSize({ REACTOR_DB_POOL_SIZE_HOST: "0" }),
522
+ ],
523
+ [
524
+ "REACTOR_PROJECTION_WORKER",
525
+ { REACTOR_PROJECTION_WORKER: "maybe" },
526
+ () =>
527
+ resolveProjectionWorkerOptions(undefined, {
528
+ REACTOR_PROJECTION_WORKER: "maybe",
529
+ }),
530
+ ],
531
+ [
532
+ "REACTOR_DB_POOL_SIZE_PROJECTION with the projection worker on",
533
+ { REACTOR_PROJECTION_WORKER: "1", REACTOR_DB_POOL_SIZE_PROJECTION: "0" },
534
+ () =>
535
+ resolveProjectionWorkerOptions(undefined, {
536
+ REACTOR_PROJECTION_WORKER: "1",
537
+ REACTOR_DB_POOL_SIZE_PROJECTION: "0",
538
+ }),
539
+ ],
540
+ [
541
+ "PH_PRIVACY_INTERVAL_MS with privacy on",
542
+ {
543
+ PH_PRIVACY_ENABLED: "true",
544
+ PH_PRIVACY_DEPLOYMENT_SECRET: SECRET,
545
+ PH_PRIVACY_INTERVAL_MS: "-5",
546
+ },
547
+ () =>
548
+ resolvePrivacy(undefined, {
549
+ PH_PRIVACY_ENABLED: "true",
550
+ PH_PRIVACY_DEPLOYMENT_SECRET: SECRET,
551
+ PH_PRIVACY_INTERVAL_MS: "-5",
552
+ }),
553
+ ],
554
+ [
555
+ "PH_PGLITE_VACUUM_FULL",
556
+ { PH_PGLITE_VACUUM_FULL: "sometimes" },
557
+ () => resolvePgliteMaintenance({ PH_PGLITE_VACUUM_FULL: "sometimes" }),
558
+ ],
559
+ [
560
+ "PH_PGLITE_WORKER_MEMORY_MB",
561
+ { PH_PGLITE_WORKER_MEMORY_MB: "0" },
562
+ () => resolvePgliteWorkerMemoryMb({ PH_PGLITE_WORKER_MEMORY_MB: "0" }),
563
+ ],
564
+ [
565
+ "CREATE_SIGNATURE_POLICY",
566
+ { CREATE_SIGNATURE_POLICY: "v3" },
567
+ () =>
568
+ resolveCreateSignaturePolicy(
569
+ { CREATE_SIGNATURE_POLICY: "v3" },
570
+ { hasSigner: true },
571
+ ),
572
+ ],
573
+ [
574
+ "REACTOR_UNSUPPORTED_STORED_DOCUMENTS",
575
+ { REACTOR_UNSUPPORTED_STORED_DOCUMENTS: "ignore" },
576
+ () =>
577
+ resolveUnsupportedStoredDocuments(undefined, {
578
+ REACTOR_UNSUPPORTED_STORED_DOCUMENTS: "ignore",
579
+ }),
580
+ ],
581
+ [
582
+ "PH_INSPECTION_CALLS_PER_MINUTE",
583
+ { PH_INSPECTION_CALLS_PER_MINUTE: "0" },
584
+ () => inspectionThrottleFromEnv({ PH_INSPECTION_CALLS_PER_MINUTE: "0" }),
585
+ ],
586
+ [
587
+ "PH_ATTACHMENT_STORAGE",
588
+ { PH_ATTACHMENT_STORAGE: "gcs" },
589
+ () => parseAttachmentStorageConfig({ PH_ATTACHMENT_STORAGE: "gcs" }),
590
+ ],
591
+ [
592
+ "PH_ATTACHMENT_URL_SIGNING_SECRET",
593
+ { PH_ATTACHMENT_URL_SIGNING_SECRET: "short" },
594
+ () =>
595
+ resolveAttachmentUrlSigning({
596
+ PH_ATTACHMENT_URL_SIGNING_SECRET: "short",
597
+ }),
598
+ ],
599
+ ])("%s throws today's message", (_name, env, today) => {
600
+ expect(() =>
601
+ switchboardConfigFromEnv(env, { logger: silent }),
602
+ ).toThrowError(messageOf(today));
603
+ });
604
+
605
+ it.each<[string, Env, () => unknown]>([
606
+ [
607
+ "REACTOR_DB_POOL_SIZE_WORKER without workers",
608
+ { REACTOR_DB_POOL_SIZE_WORKER: "-1" },
609
+ () =>
610
+ resolveWorkerPoolOptions(undefined, {
611
+ REACTOR_DB_POOL_SIZE_WORKER: "-1",
612
+ }),
613
+ ],
614
+ [
615
+ "REACTOR_DB_POOL_SIZE_PROJECTION without the projection worker",
616
+ { REACTOR_DB_POOL_SIZE_PROJECTION: "0" },
617
+ () =>
618
+ resolveProjectionWorkerOptions(undefined, {
619
+ REACTOR_DB_POOL_SIZE_PROJECTION: "0",
620
+ }),
621
+ ],
622
+ [
623
+ "PH_PRIVACY_INTERVAL_MS without privacy",
624
+ { PH_PRIVACY_INTERVAL_MS: "-5" },
625
+ () => resolvePrivacy(undefined, { PH_PRIVACY_INTERVAL_MS: "-5" }),
626
+ ],
627
+ [
628
+ "REACTOR_DB_POOL_SIZE_HOST without a Postgres reactor",
629
+ { REACTOR_DB_POOL_SIZE_HOST: "0" },
630
+ () => undefined,
631
+ ],
632
+ [
633
+ "PH_PGLITE_WORKER_MEMORY_MB with PGlite workers off",
634
+ { PH_PGLITE_WORKERS: "false", PH_PGLITE_WORKER_MEMORY_MB: "0" },
635
+ () => undefined,
636
+ ],
637
+ ])("%s does not throw, as today", (_name, env, today) => {
638
+ expect(today).not.toThrow();
639
+ expect(() =>
640
+ switchboardConfigFromEnv(env, { logger: silent }),
641
+ ).not.toThrow();
642
+ });
643
+ });
644
+
645
+ describe("switchboardConfigFromEnv telemetry", () => {
646
+ const layer = (env: Env) => switchboardConfigFromEnv(env, { logger: silent });
647
+
648
+ it("maps the processor timing settings", () => {
649
+ expect(
650
+ layer({
651
+ PH_PROCESSOR_TIMING: "detailed",
652
+ PH_PROCESSOR_TIMING_WINDOW_MS: "120000",
653
+ PH_PROCESSOR_SLOW_BATCH_MS: "250",
654
+ PH_PROCESSOR_TIMING_LOG_MS: "60000",
655
+ }).protocol,
656
+ ).toEqual({
657
+ processorTiming: "detailed",
658
+ processorTimingWindowMs: 120_000,
659
+ processorSlowBatchMs: 250,
660
+ processorTimingLogMs: 60_000,
661
+ });
662
+ });
663
+
664
+ it("maps the memory telemetry settings", () => {
665
+ expect(
666
+ layer({
667
+ PH_MEMORY_TELEMETRY: "on",
668
+ PH_MEMORY_TELEMETRY_INTERVAL_MS: "5000",
669
+ PH_MEMORY_TELEMETRY_OUTPUT: "file,console",
670
+ PH_MEMORY_TELEMETRY_FILE_MAX_MB: "20",
671
+ PH_MEMORY_TELEMETRY_FILE_KEEP: "3",
672
+ PH_MEMORY_TELEMETRY_RING: "100",
673
+ PH_MEMORY_TELEMETRY_CONSOLE_INTERVAL_MS: "60000",
674
+ PH_MEMORY_TELEMETRY_WORKERS: "false",
675
+ PH_MEMORY_TELEMETRY_WINDOW_MIN: "10",
676
+ PH_HEAP_SNAPSHOT_DIR: "/snapshots",
677
+ }).telemetry,
678
+ ).toEqual({
679
+ memory: {
680
+ enabled: true,
681
+ intervalMs: 5000,
682
+ outputs: ["memory", "file", "console"],
683
+ file: { maxMb: 20, keep: 3 },
684
+ ringSize: 100,
685
+ consoleIntervalMs: 60_000,
686
+ workers: false,
687
+ windowMin: 10,
688
+ },
689
+ heapSnapshotDir: "/snapshots",
690
+ });
691
+ });
692
+
693
+ it("leaves the telemetry block out with nothing set", () => {
694
+ expect(layer({}).telemetry).toBeUndefined();
695
+ });
696
+
697
+ it("drops the console output when told to", () => {
698
+ expect(
699
+ layer({
700
+ PH_MEMORY_TELEMETRY_OUTPUT: "console,file",
701
+ PH_MEMORY_TELEMETRY_CONSOLE: "false",
702
+ }).telemetry?.memory?.outputs,
703
+ ).toEqual(["memory", "file"]);
704
+ });
705
+
706
+ it("adds the file output for an explicit path", () => {
707
+ expect(
708
+ layer({
709
+ PH_MEMORY_TELEMETRY_OUTPUT: "memory",
710
+ PH_MEMORY_TELEMETRY_FILE: "/x/memory.jsonl",
711
+ }).telemetry?.memory,
712
+ ).toEqual({
713
+ outputs: ["memory", "file"],
714
+ file: { path: "/x/memory.jsonl" },
715
+ });
716
+ });
717
+
718
+ it("lets PH_TELEMETRY=off win over both switches", () => {
719
+ const off = layer({
720
+ PH_TELEMETRY: "off",
721
+ PH_PROCESSOR_TIMING: "detailed",
722
+ PH_MEMORY_TELEMETRY: "on",
723
+ });
724
+ expect(off.protocol?.processorTiming).toBe("off");
725
+ expect(off.telemetry?.memory?.enabled).toBe(false);
726
+ });
727
+
728
+ it("carries the shared telemetry directory", () => {
729
+ expect(layer({ PH_TELEMETRY_DIR: "/t" }).telemetry?.dir).toBe("/t");
730
+ });
731
+ });
732
+
733
+ describe("switchboardConfigFromEnv warnings", () => {
734
+ it.each<
735
+ [
736
+ string,
737
+ Env,
738
+ (layer: ReturnType<typeof switchboardConfigFromEnv>) => unknown,
739
+ ]
740
+ >([
741
+ [
742
+ "PH_CATCH_UP_PRIORITY",
743
+ { PH_CATCH_UP_PRIORITY: "fast" },
744
+ (l) => l.protocol,
745
+ ],
746
+ ["PH_PROCESSOR_TIMING", { PH_PROCESSOR_TIMING: "full" }, (l) => l.protocol],
747
+ [
748
+ "PH_PROCESSOR_TIMING_WINDOW_MS",
749
+ { PH_PROCESSOR_TIMING_WINDOW_MS: "0" },
750
+ (l) => l.protocol,
751
+ ],
752
+ [
753
+ "PH_MEMORY_TELEMETRY",
754
+ { PH_MEMORY_TELEMETRY: "maybe" },
755
+ (l) => l.telemetry,
756
+ ],
757
+ [
758
+ "PH_MEMORY_TELEMETRY_OUTPUT",
759
+ { PH_MEMORY_TELEMETRY_OUTPUT: "memory,otel" },
760
+ (l) => l.telemetry,
761
+ ],
762
+ [
763
+ "PH_MEMORY_TELEMETRY_INTERVAL_MS",
764
+ { PH_MEMORY_TELEMETRY_INTERVAL_MS: "soon" },
765
+ (l) => l.telemetry,
766
+ ],
767
+ ["GATEWAY_ADAPTER", { GATEWAY_ADAPTER: "nginx" }, (l) => l.server],
768
+ ["LOG_LEVEL", { LOG_LEVEL: "INFO" }, (l) => l.logLevel],
769
+ ["PORT", { PORT: "http" }, (l) => l.server],
770
+ [
771
+ "CREDENTIAL_VERIFICATION_CACHE_TTL_MS",
772
+ { CREDENTIAL_VERIFICATION_CACHE_TTL_MS: "-1" },
773
+ (l) => l.auth,
774
+ ],
775
+ [
776
+ "ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS",
777
+ { ATTACHMENT_DOWNLOAD_TARGET_MAX_TTL_SECONDS: "0" },
778
+ (l) => l.attachments,
779
+ ],
780
+ ])("an invalid %s warns and is left unset", (name, env, section) => {
781
+ const warn = vi.fn();
782
+ const layer = switchboardConfigFromEnv(env, {
783
+ logger: { warn } as unknown as ILogger,
784
+ });
785
+ expect(section(layer)).toBeUndefined();
786
+ expect(warn).toHaveBeenCalledOnce();
787
+ expect(String(warn.mock.calls[0]?.[0])).toContain(name);
788
+ });
789
+
790
+ it("an invalid RENOWN_SOURCE warns and still overrides the config file, as remote", () => {
791
+ const warn = vi.fn();
792
+ const layer = switchboardConfigFromEnv(
793
+ { RENOWN_SOURCE: "both" },
794
+ { logger: { warn } as unknown as ILogger },
795
+ );
796
+ expect(layer.auth?.renown).toEqual({ source: "remote" });
797
+ expect(warn).toHaveBeenCalledOnce();
798
+ });
799
+ });
800
+
801
+ describe("switchboardConfigFromEnv for the bin", () => {
802
+ afterEach(() => {
803
+ vi.unstubAllEnvs();
804
+ vi.resetModules();
805
+ });
806
+
807
+ // What sb/config.ts gave for each row, before the bin moved to the config.
808
+ const binRows: [
809
+ Env,
810
+ { port: number; migrate: boolean; pg?: 16 | 17; drive?: string },
811
+ ][] = [
812
+ [{}, { port: 4001, migrate: false }],
813
+ [{ PORT: "5000" }, { port: 4001, migrate: false }],
814
+ [
815
+ { PH_SWITCHBOARD_PORT: "4100", PORT: "5000" },
816
+ { port: 4100, migrate: false },
817
+ ],
818
+ [{ PH_SWITCHBOARD_PORT: "abc" }, { port: 4001, migrate: false }],
819
+ [{ PH_SWITCHBOARD_PORT: " " }, { port: 0, migrate: false }],
820
+ [
821
+ {
822
+ PH_MIGRATE_PGLITE: "true",
823
+ PH_FORCE_PG_VERSION: "16",
824
+ PH_DEFAULT_DRIVE_TYPE: "powerhouse/reactor-drive",
825
+ },
826
+ { port: 4001, migrate: true, pg: 16, drive: "powerhouse/reactor-drive" },
827
+ ],
828
+ ];
829
+
830
+ it.each(binRows)("matches sb/config.ts for %o", (row, before) => {
831
+ const config = switchboardConfigSchema.parse(
832
+ switchboardConfigFromEnv(row, { bin: true, logger: silent }),
833
+ );
834
+ expect(config.server.port).toBe(before.port);
835
+ expect(config.storage.migratePglite).toBe(before.migrate);
836
+ expect(config.storage.forcePgVersion).toBe(before.pg);
837
+ expect(config.drive?.documentType).toBe(before.drive);
838
+ });
839
+
840
+ it("puts the config file's port under PH_SWITCHBOARD_PORT", () => {
841
+ const at = (env: Env) =>
842
+ switchboardConfigSchema.parse(
843
+ binSwitchboardConfig(env, {
844
+ filePort: 4200,
845
+ migratePglite: false,
846
+ logger: silent,
847
+ }),
848
+ ).server.port;
849
+ expect(at({})).toBe(4200);
850
+ expect(at({ PH_SWITCHBOARD_PORT: "abc" })).toBe(4200);
851
+ expect(at({ PH_SWITCHBOARD_PORT: "4100" })).toBe(4100);
852
+ });
853
+
854
+ it("keeps the bin's mcp, drive and --migrate-pglite over the env", () => {
855
+ const config = switchboardConfigSchema.parse(
856
+ binSwitchboardConfig(
857
+ {
858
+ MCP_ENABLED: "false",
859
+ PH_DEFAULT_DRIVE_TYPE: "powerhouse/reactor-drive",
860
+ },
861
+ { migratePglite: true, logger: silent },
862
+ ),
863
+ );
864
+ expect(config.features.mcp).toBe(false);
865
+ expect(config.storage.migratePglite).toBe(true);
866
+ expect(config.drive).toMatchObject({
867
+ id: "powerhouse",
868
+ slug: "powerhouse",
869
+ documentType: "powerhouse/reactor-drive",
870
+ global: { name: "Powerhouse" },
871
+ });
872
+ expect(
873
+ switchboardConfigSchema.parse(
874
+ binSwitchboardConfig({}, { migratePglite: false, logger: silent }),
875
+ ).features.mcp,
876
+ ).toBe(true);
877
+ });
878
+
879
+ it("throws the bin's errors only for the bin", () => {
880
+ const env = { PH_FORCE_PG_VERSION: "15", PH_DEFAULT_DRIVE_TYPE: "x" };
881
+ expect(() =>
882
+ switchboardConfigFromEnv(env, { bin: true, logger: silent }),
883
+ ).toThrowError("PH_FORCE_PG_VERSION must be one of: 16, 17 (got: 15)");
884
+ expect(() =>
885
+ switchboardConfigFromEnv(
886
+ { PH_DEFAULT_DRIVE_TYPE: "x" },
887
+ { bin: true, logger: silent },
888
+ ),
889
+ ).toThrowError("Invalid PH_DEFAULT_DRIVE_TYPE: x.");
890
+ expect(switchboardConfigFromEnv(env, { logger: silent })).toEqual({});
891
+ });
892
+ });
893
+
894
+ describe("PH_KEYFRAMES", () => {
895
+ it("leaves keyframes unset without it", () => {
896
+ expect(switchboardConfigFromEnv({}).storage?.keyframes).toBeUndefined();
897
+ expect(
898
+ switchboardConfigFromEnv({ PH_KEYFRAMES: " " }).storage?.keyframes,
899
+ ).toBeUndefined();
900
+ });
901
+
902
+ it("reads the rules", () => {
903
+ const rules = {
904
+ default: { interval: 10, keep: 4 },
905
+ types: { "a/b": { interval: 50, keep: 1 }, "a/c": { keep: null } },
906
+ };
907
+
908
+ const config = switchboardConfigFromEnv({
909
+ PH_KEYFRAMES: JSON.stringify(rules),
910
+ });
911
+
912
+ expect(config.storage?.keyframes).toEqual(rules);
913
+ expect(switchboardConfigSchema.parse(config).storage.keyframes).toEqual(
914
+ rules,
915
+ );
916
+ });
917
+
918
+ it("stops the boot on JSON it cannot read", () => {
919
+ expect(() =>
920
+ switchboardConfigFromEnv({ PH_KEYFRAMES: "{keep:1}" }),
921
+ ).toThrow("PH_KEYFRAMES is not valid JSON");
922
+ });
923
+
924
+ it("stops the boot on a rule it cannot use", () => {
925
+ expect(() =>
926
+ switchboardConfigFromEnv({
927
+ PH_KEYFRAMES: JSON.stringify({ default: { keep: 0 } }),
928
+ }),
929
+ ).toThrow("PH_KEYFRAMES.default.keep");
930
+ });
931
+
932
+ it("is replaced whole by a later layer", () => {
933
+ const merged = mergeSwitchboardConfig(
934
+ {
935
+ storage: {
936
+ keyframes: {
937
+ default: { keep: 4 },
938
+ types: { "a/b": { keep: 1 } },
939
+ },
940
+ },
941
+ },
942
+ { storage: { keyframes: { default: { keep: 2 } } } },
943
+ );
944
+
945
+ expect(merged.storage?.keyframes).toEqual({ default: { keep: 2 } });
946
+ });
947
+ });
948
+
949
+ describe("switchboardConfigFromEnv REACTOR_UNDO_V3", () => {
950
+ it.each(["true", "false", "1", ""])(
951
+ "maps %j as the env read does",
952
+ (value) => {
953
+ const row = { REACTOR_UNDO_V3: value };
954
+ expect(mapped(row).protocol.featureFlags).toEqual(
955
+ resolveReactorFeatureFlags(row).flags,
956
+ );
957
+ },
958
+ );
959
+
960
+ it("names undoV3 only when the env sets it", () => {
961
+ expect(
962
+ switchboardConfigFromEnv(
963
+ { REACTOR_UNDO_V3: "false" },
964
+ { logger: silent },
965
+ ),
966
+ ).toStrictEqual({ protocol: { featureFlags: { undoV3: false } } });
967
+ });
968
+ });