@powerhousedao/switchboard 6.2.3-dev.10 → 6.2.3-dev.100

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 (310) 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/byte-grants.test.d.ts +2 -0
  4. package/.tsbuild/test/attachments/byte-grants.test.d.ts.map +1 -0
  5. package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts +2 -0
  6. package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts.map +1 -0
  7. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts +2 -0
  8. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts.map +1 -0
  9. package/.tsbuild/test/attachments/url-signer.test.d.ts +2 -0
  10. package/.tsbuild/test/attachments/url-signer.test.d.ts.map +1 -0
  11. package/.tsbuild/test/boot-auth-config.test.d.ts +2 -0
  12. package/.tsbuild/test/boot-auth-config.test.d.ts.map +1 -0
  13. package/.tsbuild/test/boot-create-signature-policy.test.d.ts +2 -0
  14. package/.tsbuild/test/boot-create-signature-policy.test.d.ts.map +1 -0
  15. package/.tsbuild/test/boot-desktop-auth.test.d.ts +2 -0
  16. package/.tsbuild/test/boot-desktop-auth.test.d.ts.map +1 -0
  17. package/.tsbuild/test/boot-env-isolation.test.d.ts +2 -0
  18. package/.tsbuild/test/boot-env-isolation.test.d.ts.map +1 -0
  19. package/.tsbuild/test/boot-helpers.d.ts +5 -0
  20. package/.tsbuild/test/boot-helpers.d.ts.map +1 -0
  21. package/.tsbuild/test/boot-inspection-enabled.test.d.ts +2 -0
  22. package/.tsbuild/test/boot-inspection-enabled.test.d.ts.map +1 -0
  23. package/.tsbuild/test/boot-mcp-auth.test.d.ts +2 -0
  24. package/.tsbuild/test/boot-mcp-auth.test.d.ts.map +1 -0
  25. package/.tsbuild/test/boot-mcp-enabled.test.d.ts +2 -0
  26. package/.tsbuild/test/boot-mcp-enabled.test.d.ts.map +1 -0
  27. package/.tsbuild/test/boot-port-zero.test.d.ts +2 -0
  28. package/.tsbuild/test/boot-port-zero.test.d.ts.map +1 -0
  29. package/.tsbuild/test/boot-preconditions.test.d.ts +2 -0
  30. package/.tsbuild/test/boot-preconditions.test.d.ts.map +1 -0
  31. package/.tsbuild/test/boot-privacy.test.d.ts +2 -0
  32. package/.tsbuild/test/boot-privacy.test.d.ts.map +1 -0
  33. package/.tsbuild/test/boot-ready-drives.test.d.ts +2 -0
  34. package/.tsbuild/test/boot-ready-drives.test.d.ts.map +1 -0
  35. package/.tsbuild/test/boot-undo-v3-flag.test.d.ts +2 -0
  36. package/.tsbuild/test/boot-undo-v3-flag.test.d.ts.map +1 -0
  37. package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts +2 -0
  38. package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts.map +1 -0
  39. package/.tsbuild/test/boot-without-renown.test.d.ts +2 -0
  40. package/.tsbuild/test/boot-without-renown.test.d.ts.map +1 -0
  41. package/.tsbuild/test/boot-without-require-signatures.test.d.ts +2 -0
  42. package/.tsbuild/test/boot-without-require-signatures.test.d.ts.map +1 -0
  43. package/.tsbuild/test/boot-worker-trust-policy.test.d.ts +2 -0
  44. package/.tsbuild/test/boot-worker-trust-policy.test.d.ts.map +1 -0
  45. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts +2 -0
  46. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts.map +1 -0
  47. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts +2 -0
  48. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts.map +1 -0
  49. package/.tsbuild/test/builder-defaults.test.d.ts +2 -0
  50. package/.tsbuild/test/builder-defaults.test.d.ts.map +1 -0
  51. package/.tsbuild/test/config-from-env.test.d.ts +2 -0
  52. package/.tsbuild/test/config-from-env.test.d.ts.map +1 -0
  53. package/.tsbuild/test/config-merge.test.d.ts +2 -0
  54. package/.tsbuild/test/config-merge.test.d.ts.map +1 -0
  55. package/.tsbuild/test/config-schema.test.d.ts +2 -0
  56. package/.tsbuild/test/config-schema.test.d.ts.map +1 -0
  57. package/.tsbuild/test/create-signature-policy.test.d.ts +2 -0
  58. package/.tsbuild/test/create-signature-policy.test.d.ts.map +1 -0
  59. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts +2 -0
  60. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts.map +1 -0
  61. package/.tsbuild/test/default-drive-slug.test.d.ts +2 -0
  62. package/.tsbuild/test/default-drive-slug.test.d.ts.map +1 -0
  63. package/.tsbuild/test/delegated-publish.test.d.ts +2 -0
  64. package/.tsbuild/test/delegated-publish.test.d.ts.map +1 -0
  65. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts +2 -0
  66. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts.map +1 -0
  67. package/.tsbuild/test/fatal-shutdown.test.d.ts +2 -0
  68. package/.tsbuild/test/fatal-shutdown.test.d.ts.map +1 -0
  69. package/.tsbuild/test/inspection-throttle.test.d.ts +2 -0
  70. package/.tsbuild/test/inspection-throttle.test.d.ts.map +1 -0
  71. package/.tsbuild/test/keyframe-reclaim-store.test.d.ts +2 -0
  72. package/.tsbuild/test/keyframe-reclaim-store.test.d.ts.map +1 -0
  73. package/.tsbuild/test/observability-signals.test.d.ts +2 -0
  74. package/.tsbuild/test/observability-signals.test.d.ts.map +1 -0
  75. package/.tsbuild/test/permissions-backfill.test.d.ts +2 -0
  76. package/.tsbuild/test/permissions-backfill.test.d.ts.map +1 -0
  77. package/.tsbuild/test/pglite-preflight.test.d.ts +2 -0
  78. package/.tsbuild/test/pglite-preflight.test.d.ts.map +1 -0
  79. package/.tsbuild/test/pglite-storage-dirs.test.d.ts +2 -0
  80. package/.tsbuild/test/pglite-storage-dirs.test.d.ts.map +1 -0
  81. package/.tsbuild/test/pglite-store.test.d.ts +2 -0
  82. package/.tsbuild/test/pglite-store.test.d.ts.map +1 -0
  83. package/.tsbuild/test/plugin-config-schemas.test.d.ts +2 -0
  84. package/.tsbuild/test/plugin-config-schemas.test.d.ts.map +1 -0
  85. package/.tsbuild/test/plugin-presence.test.d.ts +2 -0
  86. package/.tsbuild/test/plugin-presence.test.d.ts.map +1 -0
  87. package/.tsbuild/test/port-probe.test.d.ts +2 -0
  88. package/.tsbuild/test/port-probe.test.d.ts.map +1 -0
  89. package/.tsbuild/test/processor-timing-log.test.d.ts +2 -0
  90. package/.tsbuild/test/processor-timing-log.test.d.ts.map +1 -0
  91. package/.tsbuild/test/renown-storage-path.test.d.ts +2 -0
  92. package/.tsbuild/test/renown-storage-path.test.d.ts.map +1 -0
  93. package/.tsbuild/test/renown-trust-policy.test.d.ts +2 -0
  94. package/.tsbuild/test/renown-trust-policy.test.d.ts.map +1 -0
  95. package/.tsbuild/test/served-schemas.test.d.ts +2 -0
  96. package/.tsbuild/test/served-schemas.test.d.ts.map +1 -0
  97. package/.tsbuild/test/shutdown.test.d.ts +2 -0
  98. package/.tsbuild/test/shutdown.test.d.ts.map +1 -0
  99. package/.tsbuild/test/snapshot-writer.d.ts +18 -0
  100. package/.tsbuild/test/snapshot-writer.d.ts.map +1 -0
  101. package/.tsbuild/test/store-lock.test.d.ts +2 -0
  102. package/.tsbuild/test/store-lock.test.d.ts.map +1 -0
  103. package/.tsbuild/test/two-switchboards.test.d.ts +2 -0
  104. package/.tsbuild/test/two-switchboards.test.d.ts.map +1 -0
  105. package/.tsbuild/test/unsupported-stored-documents.test.d.ts +2 -0
  106. package/.tsbuild/test/unsupported-stored-documents.test.d.ts.map +1 -0
  107. package/.tsbuild/test/workflow/harness.d.ts +56 -0
  108. package/.tsbuild/test/workflow/harness.d.ts.map +1 -0
  109. package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts +2 -0
  110. package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts.map +1 -0
  111. package/.tsbuild/test/workflow/oauth.test.d.ts +2 -0
  112. package/.tsbuild/test/workflow/oauth.test.d.ts.map +1 -0
  113. package/.tsbuild/test/workflow/resolvers.test.d.ts +2 -0
  114. package/.tsbuild/test/workflow/resolvers.test.d.ts.map +1 -0
  115. package/.tsbuild/test/workflow/runtime-host.test.d.ts +2 -0
  116. package/.tsbuild/test/workflow/runtime-host.test.d.ts.map +1 -0
  117. package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts +2 -0
  118. package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts.map +1 -0
  119. package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts +2 -0
  120. package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts.map +1 -0
  121. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts +2 -0
  122. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts.map +1 -0
  123. package/.tsbuild/test/workflow/webhook-signed.test.d.ts +2 -0
  124. package/.tsbuild/test/workflow/webhook-signed.test.d.ts.map +1 -0
  125. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts +2 -0
  126. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts.map +1 -0
  127. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts +2 -0
  128. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts.map +1 -0
  129. package/.tsbuild/test/workflow-reactor-access.test.d.ts +2 -0
  130. package/.tsbuild/test/workflow-reactor-access.test.d.ts.map +1 -0
  131. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts +2 -0
  132. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts.map +1 -0
  133. package/.tsbuild/test/workflow-read-gate.test.d.ts +2 -0
  134. package/.tsbuild/test/workflow-read-gate.test.d.ts.map +1 -0
  135. package/.tsbuild/test/workflow-runtime.test.d.ts +2 -0
  136. package/.tsbuild/test/workflow-runtime.test.d.ts.map +1 -0
  137. package/.tsbuild/test/workflows-plugin-config.test.d.ts +2 -0
  138. package/.tsbuild/test/workflows-plugin-config.test.d.ts.map +1 -0
  139. package/.tsbuild/tsconfig.tsbuildinfo +1 -1
  140. package/.tsbuild/tsdown.config.d.ts.map +1 -1
  141. package/.tsbuild/vitest.config.d.ts.map +1 -1
  142. package/Auth.md +144 -2
  143. package/CHANGELOG.md +1284 -0
  144. package/README.md +102 -22
  145. package/dist/boot.d.mts +46 -0
  146. package/dist/boot.d.mts.map +1 -0
  147. package/dist/boot.mjs +67 -0
  148. package/dist/boot.mjs.map +1 -0
  149. package/dist/config/from-env.d.mts +15 -0
  150. package/dist/config/from-env.d.mts.map +1 -0
  151. package/dist/config/from-env.mjs +4 -0
  152. package/dist/config/schema.d.mts +3 -0
  153. package/dist/config/schema.mjs +4 -0
  154. package/dist/from-env-FH4fPeIE.mjs +481 -0
  155. package/dist/from-env-FH4fPeIE.mjs.map +1 -0
  156. package/dist/index.d.mts +2 -1
  157. package/dist/index.mjs +84 -84
  158. package/dist/index.mjs.map +1 -1
  159. package/dist/install-packages.d.mts +2 -1
  160. package/dist/install-packages.mjs +2 -2
  161. package/dist/install-packages.mjs.map +1 -1
  162. package/dist/mcp-SsBK14Jm.mjs +32 -0
  163. package/dist/mcp-SsBK14Jm.mjs.map +1 -0
  164. package/dist/migrate.d.mts +2 -1
  165. package/dist/migrate.mjs +7 -5
  166. package/dist/migrate.mjs.map +1 -1
  167. package/dist/plugin-host-CK9_tVak.d.mts +46 -0
  168. package/dist/plugin-host-CK9_tVak.d.mts.map +1 -0
  169. package/dist/plugins/ai.d.mts +37 -0
  170. package/dist/plugins/ai.d.mts.map +1 -0
  171. package/dist/plugins/ai.mjs +400 -0
  172. package/dist/plugins/ai.mjs.map +1 -0
  173. package/dist/plugins/attachments.d.mts +33 -0
  174. package/dist/plugins/attachments.d.mts.map +1 -0
  175. package/dist/plugins/attachments.mjs +4 -0
  176. package/dist/plugins/mcp.d.mts +31 -0
  177. package/dist/plugins/mcp.d.mts.map +1 -0
  178. package/dist/plugins/mcp.mjs +4 -0
  179. package/dist/plugins/privacy.d.mts +22 -0
  180. package/dist/plugins/privacy.d.mts.map +1 -0
  181. package/dist/plugins/privacy.mjs +4 -0
  182. package/dist/plugins/workflows.d.mts +79 -0
  183. package/dist/plugins/workflows.d.mts.map +1 -0
  184. package/dist/plugins/workflows.mjs +4 -0
  185. package/dist/postgres-url-DTDhBjXX.mjs +11 -0
  186. package/dist/postgres-url-DTDhBjXX.mjs.map +1 -0
  187. package/dist/privacy-BO8Q25xI.mjs +44 -0
  188. package/dist/privacy-BO8Q25xI.mjs.map +1 -0
  189. package/dist/privacy-DxIJGH4P.mjs +91 -0
  190. package/dist/privacy-DxIJGH4P.mjs.map +1 -0
  191. package/dist/renown-layers-DmbWLt46.mjs +42 -0
  192. package/dist/renown-layers-DmbWLt46.mjs.map +1 -0
  193. package/dist/schema-CsY8bjsu.mjs +329 -0
  194. package/dist/schema-CsY8bjsu.mjs.map +1 -0
  195. package/dist/schema-MAUTnmj1.d.mts +251 -0
  196. package/dist/schema-MAUTnmj1.d.mts.map +1 -0
  197. package/dist/server-CirQ9hyu.mjs +2732 -0
  198. package/dist/server-CirQ9hyu.mjs.map +1 -0
  199. package/dist/server.d.mts +129 -189
  200. package/dist/server.d.mts.map +1 -1
  201. package/dist/server.mjs +5 -5
  202. package/dist/types-CidZdkhU.d.mts +314 -0
  203. package/dist/types-CidZdkhU.d.mts.map +1 -0
  204. package/dist/utils-Cm0TE6dP.mjs +148 -0
  205. package/dist/utils-Cm0TE6dP.mjs.map +1 -0
  206. package/dist/utils.d.mts +30 -5
  207. package/dist/utils.d.mts.map +1 -1
  208. package/dist/utils.mjs +5 -4
  209. package/dist/worker-pool-gyxJnCxA.mjs +304 -0
  210. package/dist/worker-pool-gyxJnCxA.mjs.map +1 -0
  211. package/dist/workflow-runtime-CF5pc8mt.d.mts +33 -0
  212. package/dist/workflow-runtime-CF5pc8mt.d.mts.map +1 -0
  213. package/dist/workflows-C9BQlyT0.mjs +1943 -0
  214. package/dist/workflows-C9BQlyT0.mjs.map +1 -0
  215. package/package.json +85 -24
  216. package/scripts/install-packages.sh +0 -0
  217. package/test/ai-gateway.test.ts +612 -0
  218. package/test/attachment-reference-read-model.test.ts +28 -20
  219. package/test/attachments/auth.test.ts +85 -0
  220. package/test/attachments/byte-grants.test.ts +290 -0
  221. package/test/attachments/byte-routes-read-gate.integration.test.ts +314 -0
  222. package/test/attachments/download-target.test.ts +211 -32
  223. package/test/attachments/index.test.ts +47 -4
  224. package/test/attachments/reserve-metadata-parity.test.ts +96 -0
  225. package/test/attachments/routes-integration.test.ts +60 -4
  226. package/test/attachments/routes.test.ts +297 -35
  227. package/test/attachments/service-config.test.ts +41 -19
  228. package/test/attachments/url-signer.test.ts +105 -0
  229. package/test/boot-auth-config.test.ts +109 -0
  230. package/test/boot-create-signature-policy.test.ts +110 -0
  231. package/test/boot-desktop-auth.test.ts +278 -0
  232. package/test/boot-env-isolation.test.ts +26 -0
  233. package/test/boot-helpers.ts +19 -0
  234. package/test/boot-inspection-enabled.test.ts +140 -0
  235. package/test/boot-mcp-auth.test.ts +449 -0
  236. package/test/boot-mcp-enabled.test.ts +119 -0
  237. package/test/boot-port-zero.test.ts +101 -0
  238. package/test/boot-preconditions.test.ts +213 -0
  239. package/test/boot-privacy.test.ts +232 -0
  240. package/test/boot-ready-drives.test.ts +141 -0
  241. package/test/boot-undo-v3-flag.test.ts +120 -0
  242. package/test/boot-unsupported-stored-documents.test.ts +214 -0
  243. package/test/boot-without-renown.test.ts +83 -0
  244. package/test/boot-without-require-signatures.test.ts +76 -0
  245. package/test/boot-worker-trust-policy.test.ts +91 -0
  246. package/test/boot-workflow-singleton-conflict.test.ts +109 -0
  247. package/test/boot-workflow-singleton-retry.test.ts +122 -0
  248. package/test/builder-defaults.test.ts +80 -0
  249. package/test/config-from-env.test.ts +963 -0
  250. package/test/config-merge.test.ts +248 -0
  251. package/test/config-schema.test.ts +167 -0
  252. package/test/create-signature-policy.test.ts +69 -0
  253. package/test/default-drive-protocol-versions.test.ts +79 -0
  254. package/test/default-drive-slug.test.ts +265 -0
  255. package/test/delegated-publish.test.ts +435 -0
  256. package/test/durable-node-fs-pg16.test.ts +80 -0
  257. package/test/fatal-shutdown.test.ts +205 -0
  258. package/test/goldens/packages.graphql +797 -0
  259. package/test/goldens/privacy.graphql +853 -0
  260. package/test/goldens/reactor-drive-nodes.graphql +827 -0
  261. package/test/goldens/resolvers.json +327 -0
  262. package/test/goldens/supergraph.graphql +5918 -0
  263. package/test/goldens/workflow-runtime.graphql +1548 -0
  264. package/test/inspection-throttle.test.ts +23 -0
  265. package/test/keyframe-reclaim-store.test.ts +156 -0
  266. package/test/observability-signals.test.ts +131 -0
  267. package/test/permissions-backfill.test.ts +486 -0
  268. package/test/pglite-dialect.test.ts +237 -2
  269. package/test/pglite-preflight.test.ts +313 -0
  270. package/test/pglite-storage-dirs.test.ts +113 -0
  271. package/test/pglite-store.test.ts +441 -0
  272. package/test/pglite-version.test.ts +9 -1
  273. package/test/plugin-config-schemas.test.ts +83 -0
  274. package/test/plugin-presence.test.ts +262 -0
  275. package/test/port-probe.test.ts +45 -0
  276. package/test/processor-timing-log.test.ts +109 -0
  277. package/test/reactor-feature-flags.test.ts +20 -0
  278. package/test/redact-webhook-path.test.ts +9 -0
  279. package/test/renown-storage-path.test.ts +40 -0
  280. package/test/renown-trust-policy.test.ts +313 -0
  281. package/test/served-schemas.test.ts +134 -0
  282. package/test/shutdown.test.ts +278 -0
  283. package/test/snapshot-writer.ts +94 -0
  284. package/test/store-lock.test.ts +65 -0
  285. package/test/two-switchboards.test.ts +146 -0
  286. package/test/unsupported-stored-documents.test.ts +72 -0
  287. package/test/worker-pool.test.ts +27 -1
  288. package/test/workflow/harness.ts +289 -0
  289. package/test/workflow/oauth-apps-config.test.ts +66 -0
  290. package/test/workflow/oauth.test.ts +286 -0
  291. package/test/workflow/resolvers.test.ts +488 -0
  292. package/test/workflow/runtime-host.test.ts +19 -0
  293. package/test/workflow/waitpoint-routes.test.ts +306 -0
  294. package/test/workflow/waitpoint-runtime.test.ts +204 -0
  295. package/test/workflow/webhook-dedupe.test.ts +295 -0
  296. package/test/workflow/webhook-signed.test.ts +509 -0
  297. package/test/workflow/webhook-trigger-test.test.ts +82 -0
  298. package/test/workflow/webhook-unsigned.test.ts +345 -0
  299. package/test/workflow-reactor-access.test.ts +384 -0
  300. package/test/workflow-reactor-enforcement.test.ts +808 -0
  301. package/test/workflow-read-gate.test.ts +213 -0
  302. package/test/workflow-runtime.test.ts +1254 -0
  303. package/test/workflows-plugin-config.test.ts +77 -0
  304. package/tsconfig.json +11 -2
  305. package/tsdown.config.ts +13 -1
  306. package/vitest.config.ts +65 -0
  307. package/dist/server-CiKkVE6g.mjs +0 -1555
  308. package/dist/server-CiKkVE6g.mjs.map +0 -1
  309. package/dist/utils-Baw7rThP.mjs +0 -80
  310. package/dist/utils-Baw7rThP.mjs.map +0 -1
@@ -0,0 +1,214 @@
1
+ import { PGlite } from "@electric-sql/pglite";
2
+ import { NodeFS } from "@electric-sql/pglite/nodefs";
3
+ import {
4
+ JobStatus,
5
+ ReactorBuilder,
6
+ type Database,
7
+ } from "@powerhousedao/reactor";
8
+ import { driveDocumentModelModule } from "@powerhousedao/shared/document-drive";
9
+ import {
10
+ withSignaturePolicy,
11
+ type PeerCapability,
12
+ } from "@powerhousedao/shared/document-model";
13
+ import type { ILogger } from "document-model";
14
+ import { Kysely } from "kysely";
15
+ import { cp, mkdtemp, rm } from "node:fs/promises";
16
+ import { tmpdir } from "node:os";
17
+ import { join } from "node:path";
18
+ import {
19
+ afterAll,
20
+ afterEach,
21
+ beforeAll,
22
+ beforeEach,
23
+ describe,
24
+ expect,
25
+ it,
26
+ vi,
27
+ } from "vitest";
28
+ import { ClosablePGliteDialect } from "../src/pglite-dialect.js";
29
+ import { bootSwitchboard } from "./boot-helpers.js";
30
+ import { StoredDocumentsRefusedError } from "../src/unsupported-stored-documents.mjs";
31
+
32
+ const ENV_KEYS = [
33
+ "PH_REACTOR_DATABASE_URL",
34
+ "DATABASE_URL",
35
+ "REACTOR_UNSUPPORTED_STORED_DOCUMENTS",
36
+ ] as const;
37
+
38
+ const BASE_REDUCER_7: PeerCapability = {
39
+ kind: "protocol",
40
+ name: "base-reducer",
41
+ baseline: [1, 2],
42
+ supported: () => [1, 2, 7],
43
+ preferred: () => 2,
44
+ optional: false,
45
+ };
46
+
47
+ type StubLogger = ILogger & {
48
+ warn: ReturnType<typeof vi.fn>;
49
+ error: ReturnType<typeof vi.fn>;
50
+ };
51
+
52
+ function stubLogger(): StubLogger {
53
+ const logger = {
54
+ verbose: vi.fn(),
55
+ debug: vi.fn(),
56
+ info: vi.fn(),
57
+ warn: vi.fn(),
58
+ error: vi.fn(),
59
+ child: vi.fn(),
60
+ };
61
+ logger.child.mockReturnValue(logger);
62
+ return logger as unknown as StubLogger;
63
+ }
64
+
65
+ /** A reactor store holding one drive created at base-reducer 7. */
66
+ async function seedStore(dir: string): Promise<void> {
67
+ const db = new Kysely<Database>({
68
+ dialect: new ClosablePGliteDialect(new PGlite({ fs: new NodeFS(dir) })),
69
+ });
70
+ const module = await new ReactorBuilder()
71
+ .withKysely(db)
72
+ .withDocumentModelSources([driveDocumentModelModule as never])
73
+ .withPeerCapabilities([BASE_REDUCER_7])
74
+ .buildModule();
75
+ const created = await module.reactor.create(
76
+ withSignaturePolicy(
77
+ driveDocumentModelModule.utils.createDocument(),
78
+ "legacy",
79
+ {
80
+ id: "br7-stored",
81
+ protocolVersions: { "base-reducer": 7 },
82
+ },
83
+ ),
84
+ );
85
+ await vi.waitUntil(
86
+ async () => {
87
+ const { status } = await module.reactor.getJobStatus(created.id);
88
+ return status === JobStatus.READ_READY || status === JobStatus.FAILED;
89
+ },
90
+ // PGlite is several times slower on the Windows runner.
91
+ { timeout: BOOT_TIMEOUT, interval: 5 },
92
+ );
93
+ expect((await module.reactor.getJobStatus(created.id)).status).toBe(
94
+ JobStatus.READ_READY,
95
+ );
96
+ await module.reactor.kill().completed;
97
+ await db.destroy();
98
+ // PGlite leaves its lockfile behind after a clean close.
99
+ await rm(join(dir, "postmaster.pid"), { force: true });
100
+ }
101
+
102
+ // PGlite boots are several times slower on Windows runners.
103
+ const BOOT_TIMEOUT = 120_000;
104
+ // Windows can briefly keep just-closed store files locked (EBUSY/EPERM).
105
+ const RM_OPTIONS = { recursive: true, force: true, maxRetries: 10 };
106
+
107
+ describe("booting over documents this build does not run", () => {
108
+ let seeded: string;
109
+ let tempRoot: string;
110
+ let previous: Partial<Record<(typeof ENV_KEYS)[number], string>>;
111
+
112
+ beforeAll(async () => {
113
+ seeded = await mkdtemp(join(tmpdir(), "switchboard-stored-protocol-seed-"));
114
+ await seedStore(join(seeded, "reactor-storage"));
115
+ }, BOOT_TIMEOUT);
116
+
117
+ afterAll(async () => {
118
+ await rm(seeded, RM_OPTIONS);
119
+ }, BOOT_TIMEOUT);
120
+
121
+ beforeEach(async () => {
122
+ tempRoot = await mkdtemp(join(tmpdir(), "switchboard-stored-protocol-"));
123
+ previous = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
124
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
125
+ process.env.DATABASE_URL = join(tempRoot, "read-model");
126
+ delete process.env.REACTOR_UNSUPPORTED_STORED_DOCUMENTS;
127
+ await cp(
128
+ join(seeded, "reactor-storage"),
129
+ process.env.PH_REACTOR_DATABASE_URL,
130
+ { recursive: true },
131
+ );
132
+ }, BOOT_TIMEOUT);
133
+
134
+ afterEach(async () => {
135
+ for (const key of ENV_KEYS) {
136
+ if (previous[key] === undefined) {
137
+ delete process.env[key];
138
+ } else {
139
+ process.env[key] = previous[key];
140
+ }
141
+ }
142
+ await rm(tempRoot, RM_OPTIONS);
143
+ }, BOOT_TIMEOUT);
144
+
145
+ function boot(logger: ILogger) {
146
+ return bootSwitchboard(
147
+ {
148
+ server: { port: 0 },
149
+ features: { workflows: false, mcp: false },
150
+ packages: { disableLocal: true },
151
+ identity: { keypairPath: join(tempRoot, "identity.json") },
152
+ },
153
+ { logger },
154
+ );
155
+ }
156
+
157
+ it(
158
+ "refuses by default and tells the operator both ways forward",
159
+ async () => {
160
+ const logger = stubLogger();
161
+
162
+ const booted = boot(logger);
163
+
164
+ await expect(booted).rejects.toSatisfy(
165
+ (error) =>
166
+ StoredDocumentsRefusedError.isError(error) &&
167
+ error.documents === 1 &&
168
+ error.versions.some(
169
+ ({ protocol, version }) =>
170
+ protocol === "base-reducer" && version === 7,
171
+ ),
172
+ );
173
+ const message = (await booted.catch((error: Error) => error)) as Error;
174
+ expect(message.message).toMatch(
175
+ /1 stored document\(s\) require base-reducer 7/,
176
+ );
177
+ expect(message.message).toMatch(
178
+ /switchboard build that runs base-reducer 7/,
179
+ );
180
+ expect(message.message).toMatch(
181
+ /REACTOR_UNSUPPORTED_STORED_DOCUMENTS=read-only/,
182
+ );
183
+ expect(logger.error).toHaveBeenCalledWith(message.message);
184
+ expect(logger.error).not.toHaveBeenCalledWith(
185
+ "App crashed: @error",
186
+ expect.anything(),
187
+ );
188
+ },
189
+ BOOT_TIMEOUT,
190
+ );
191
+
192
+ it(
193
+ "starts read-only when REACTOR_UNSUPPORTED_STORED_DOCUMENTS says so",
194
+ async () => {
195
+ process.env.REACTOR_UNSUPPORTED_STORED_DOCUMENTS = "read-only";
196
+ const logger = stubLogger();
197
+
198
+ const switchboard = await boot(logger);
199
+ try {
200
+ expect(logger.warn).toHaveBeenCalledWith(
201
+ expect.stringContaining("read-only"),
202
+ expect.stringContaining(
203
+ "1 stored document(s) require base-reducer 7",
204
+ ),
205
+ );
206
+ const stored = await switchboard.reactor.get("br7-stored");
207
+ expect(stored.header.id).toBe("br7-stored");
208
+ } finally {
209
+ await switchboard.shutdown();
210
+ }
211
+ },
212
+ BOOT_TIMEOUT,
213
+ );
214
+ });
@@ -0,0 +1,83 @@
1
+ import {
2
+ isDerivedDocumentId,
3
+ signaturePolicyOf,
4
+ } from "@powerhousedao/shared/document-model";
5
+ import { mkdtemp, rm } from "node:fs/promises";
6
+ import { tmpdir } from "node:os";
7
+ import { join } from "node:path";
8
+ import type { ILogger } from "document-model";
9
+ import { describe, expect, it, vi } from "vitest";
10
+ import type * as RenownModule from "../src/renown.js";
11
+ import type { startSwitchboard } from "../src/server.mjs";
12
+ import { bootSwitchboard } from "./boot-helpers.js";
13
+
14
+ vi.mock("../src/renown.js", async (importOriginal) => ({
15
+ ...(await importOriginal<typeof RenownModule>()),
16
+ initRenown: vi.fn().mockRejectedValue(new Error("Renown is unreachable")),
17
+ }));
18
+
19
+ function stubLogger(): ILogger & { warn: ReturnType<typeof vi.fn> } {
20
+ const logger = {
21
+ verbose: vi.fn(),
22
+ debug: vi.fn(),
23
+ info: vi.fn(),
24
+ warn: vi.fn(),
25
+ error: vi.fn(),
26
+ child: vi.fn(),
27
+ };
28
+ logger.child.mockReturnValue(logger);
29
+ return logger as unknown as ILogger & { warn: ReturnType<typeof vi.fn> };
30
+ }
31
+
32
+ describe("booting Switchboard without Renown", () => {
33
+ it("warns and creates legacy documents, which it can write unsigned", async () => {
34
+ const tempRoot = await mkdtemp(join(tmpdir(), "switchboard-no-renown-"));
35
+ const previous = process.env.PH_REACTOR_DATABASE_URL;
36
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
37
+ const logger = stubLogger();
38
+ let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
39
+
40
+ try {
41
+ switchboard = await bootSwitchboard(
42
+ {
43
+ server: { port: 0 },
44
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
45
+ features: { workflows: false, mcp: false },
46
+ packages: { disableLocal: true },
47
+ },
48
+ { logger },
49
+ );
50
+
51
+ expect(
52
+ logger.warn.mock.calls.some(
53
+ ([message]) =>
54
+ typeof message === "string" &&
55
+ message.includes("new documents are created legacy"),
56
+ ),
57
+ ).toBe(true);
58
+
59
+ const document = await switchboard.reactor.createEmpty(
60
+ "powerhouse/document-model",
61
+ );
62
+ expect(signaturePolicyOf(document.header)).toBe("legacy");
63
+ expect(isDerivedDocumentId(document.header.id)).toBe(false);
64
+
65
+ const renamed = await switchboard.reactor.rename(
66
+ document.header.id,
67
+ "Renamed",
68
+ );
69
+ expect(renamed.header.name).toBe("Renamed");
70
+
71
+ await switchboard.shutdown();
72
+ switchboard = undefined;
73
+ } finally {
74
+ await switchboard?.shutdown();
75
+ if (previous === undefined) {
76
+ delete process.env.PH_REACTOR_DATABASE_URL;
77
+ } else {
78
+ process.env.PH_REACTOR_DATABASE_URL = previous;
79
+ }
80
+ await rm(tempRoot, { recursive: true, force: true });
81
+ }
82
+ }, 60_000);
83
+ });
@@ -0,0 +1,76 @@
1
+ import { mkdtemp, rm } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { join } from "node:path";
4
+ import type { ILogger } from "document-model";
5
+ import { describe, expect, it, vi } from "vitest";
6
+ import type { startSwitchboard } from "../src/server.mjs";
7
+ import { bootSwitchboard } from "./boot-helpers.js";
8
+
9
+ function stubLogger(): ILogger & { info: ReturnType<typeof vi.fn> } {
10
+ const logger = {
11
+ verbose: vi.fn(),
12
+ debug: vi.fn(),
13
+ info: vi.fn(),
14
+ warn: vi.fn(),
15
+ error: vi.fn(),
16
+ child: vi.fn(),
17
+ };
18
+ logger.child.mockReturnValue(logger);
19
+ return logger as unknown as ILogger & { info: ReturnType<typeof vi.fn> };
20
+ }
21
+
22
+ describe("booting Switchboard", () => {
23
+ it("has no REQUIRE_SIGNATURES flag, and ignores the variable", async () => {
24
+ const tempRoot = await mkdtemp(join(tmpdir(), "switchboard-signatures-"));
25
+ const previous = {
26
+ reactorDb: process.env.PH_REACTOR_DATABASE_URL,
27
+ requireSignatures: process.env.REQUIRE_SIGNATURES,
28
+ };
29
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
30
+ process.env.REQUIRE_SIGNATURES = "true";
31
+ const logger = stubLogger();
32
+ let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
33
+
34
+ try {
35
+ switchboard = await bootSwitchboard(
36
+ {
37
+ server: { port: 0 },
38
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
39
+ features: { workflows: false, mcp: false },
40
+ packages: { disableLocal: true },
41
+ identity: { keypairPath: join(tempRoot, "identity.json") },
42
+ },
43
+ { logger },
44
+ );
45
+
46
+ const flagLog = logger.info.mock.calls.find(
47
+ ([message]) => message === "Feature flags: @flags",
48
+ );
49
+ expect(flagLog).toBeDefined();
50
+ expect(JSON.parse(flagLog![1] as string)).not.toHaveProperty(
51
+ "REQUIRE_SIGNATURES",
52
+ );
53
+
54
+ const created = await switchboard.reactor.createEmpty(
55
+ "powerhouse/document-model",
56
+ );
57
+ expect(created.header.id).toBeTruthy();
58
+
59
+ await switchboard.shutdown();
60
+ switchboard = undefined;
61
+ } finally {
62
+ await switchboard?.shutdown();
63
+ for (const [name, value] of [
64
+ ["PH_REACTOR_DATABASE_URL", previous.reactorDb],
65
+ ["REQUIRE_SIGNATURES", previous.requireSignatures],
66
+ ] as const) {
67
+ if (value === undefined) {
68
+ delete process.env[name];
69
+ } else {
70
+ process.env[name] = value;
71
+ }
72
+ }
73
+ await rm(tempRoot, { recursive: true, force: true });
74
+ }
75
+ }, 60_000);
76
+ });
@@ -0,0 +1,91 @@
1
+ import type { ILogger } from "document-model";
2
+ import { mkdtemp, rm } from "node:fs/promises";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6
+ import type * as RenownModule from "../src/renown.js";
7
+ import { bootSwitchboard } from "./boot-helpers.js";
8
+
9
+ vi.mock("../src/renown.js", async (importOriginal) => ({
10
+ ...(await importOriginal<typeof RenownModule>()),
11
+ initRenown: vi.fn().mockRejectedValue(new Error("Renown is unreachable")),
12
+ }));
13
+
14
+ const TRUST_FAILURE =
15
+ /REACTOR_WORKERS[\s\S]*REACTOR_AUTH_ENFORCEMENT[\s\S]*RENOWN_SOURCE/;
16
+ const ENV_KEYS = [
17
+ "PH_REACTOR_DATABASE_URL",
18
+ "RENOWN_SOURCE",
19
+ "REACTOR_DOCUMENT_DECISIONS",
20
+ "REACTOR_AUTH_ENFORCEMENT",
21
+ ] as const;
22
+
23
+ function stubLogger(): ILogger {
24
+ const logger = {
25
+ verbose: vi.fn(),
26
+ debug: vi.fn(),
27
+ info: vi.fn(),
28
+ warn: vi.fn(),
29
+ error: vi.fn(),
30
+ child: vi.fn(),
31
+ };
32
+ logger.child.mockReturnValue(logger);
33
+ return logger as unknown as ILogger;
34
+ }
35
+
36
+ describe("booting with pooled workers under authEnforcement", () => {
37
+ let tempRoot: string;
38
+ let previous: Partial<Record<(typeof ENV_KEYS)[number], string>>;
39
+
40
+ beforeEach(async () => {
41
+ tempRoot = await mkdtemp(join(tmpdir(), "switchboard-worker-trust-"));
42
+ previous = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
43
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
44
+ process.env.REACTOR_DOCUMENT_DECISIONS = "true";
45
+ process.env.REACTOR_AUTH_ENFORCEMENT = "true";
46
+ });
47
+
48
+ afterEach(async () => {
49
+ for (const key of ENV_KEYS) {
50
+ if (previous[key] === undefined) {
51
+ delete process.env[key];
52
+ } else {
53
+ process.env[key] = previous[key];
54
+ }
55
+ }
56
+ await rm(tempRoot, { recursive: true, force: true });
57
+ });
58
+
59
+ function boot(numWorkers: number) {
60
+ return bootSwitchboard(
61
+ {
62
+ server: { port: 0 },
63
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
64
+ features: { workflows: false, mcp: false },
65
+ packages: { disableLocal: true },
66
+ workers: { executor: { count: numWorkers } },
67
+ },
68
+ { logger: stubLogger() },
69
+ );
70
+ }
71
+
72
+ it("fails with a self Renown source", async () => {
73
+ process.env.RENOWN_SOURCE = "self";
74
+
75
+ await expect(boot(2)).rejects.toThrow(TRUST_FAILURE);
76
+ });
77
+
78
+ // Postgres is the next requirement a pooled boot has; reaching it passes the check.
79
+ it("passes the check with a remote Renown source", async () => {
80
+ process.env.RENOWN_SOURCE = "remote";
81
+
82
+ await expect(boot(2)).rejects.toThrow(/requires a Postgres/);
83
+ });
84
+
85
+ // No loaded package serves the renown read model here; reaching that check passes this one.
86
+ it("passes the check without workers", async () => {
87
+ process.env.RENOWN_SOURCE = "self";
88
+
89
+ await expect(boot(0)).rejects.toThrow(/no loaded package serves/);
90
+ }, 60_000);
91
+ });
@@ -0,0 +1,109 @@
1
+ // Losing the workflow singleton claim must not take the whole API down.
2
+ //
3
+ // It used to: WorkflowSingletonConflictError propagated out of startSwitchboard
4
+ // and aborted the boot. With the old random per-process owner name, an unclean
5
+ // kill left a lease nobody could re-claim, so every restart inside the 60s TTL
6
+ // crash-looped the entire Switchboard — inspection, GraphQL, sync and all —
7
+ // over a component this host is merely not allowed to run.
8
+ import { mkdtemp, rm } from "node:fs/promises";
9
+ import { tmpdir } from "node:os";
10
+ import { join } from "node:path";
11
+ import type * as WorkflowEngine from "@powerhousedao/reactor-workflow";
12
+ import type { ILogger } from "document-model";
13
+ import { describe, expect, it, vi } from "vitest";
14
+
15
+ const HOLDER = "another-live-replica";
16
+ const HELD_UNTIL = "2030-01-01T00:00:00.000Z";
17
+
18
+ // The lease itself is the engine's to test (reactor-workflow,
19
+ // singleton-lease.test.ts). What is under test here is what THIS host does
20
+ // when the claim is refused, so only the claim is faked.
21
+ vi.mock("@powerhousedao/reactor-workflow", async (importOriginal) => {
22
+ const actual = await importOriginal<typeof WorkflowEngine>();
23
+ return {
24
+ ...actual,
25
+ acquireWorkflowSingletonLease: () => {
26
+ const error = new Error(
27
+ `Workflow execution is a singleton and "${HOLDER}" holds it until ${HELD_UNTIL}`,
28
+ );
29
+ error.name = "WorkflowSingletonConflictError";
30
+ return Promise.reject(
31
+ Object.assign(error, {
32
+ owner: HOLDER,
33
+ expiresAt: HELD_UNTIL,
34
+ wouldBe: "this-host",
35
+ }),
36
+ );
37
+ },
38
+ };
39
+ });
40
+
41
+ const { startSwitchboard } = await import("../src/server.mjs");
42
+ const { bootSwitchboard } = await import("./boot-helpers.js");
43
+
44
+ function stubLogger(): ILogger & { warn: ReturnType<typeof vi.fn> } {
45
+ const logger = {
46
+ verbose: vi.fn(),
47
+ debug: vi.fn(),
48
+ info: vi.fn(),
49
+ warn: vi.fn(),
50
+ error: vi.fn(),
51
+ child: vi.fn(),
52
+ };
53
+ logger.child.mockReturnValue(logger);
54
+ return logger as unknown as ILogger & { warn: ReturnType<typeof vi.fn> };
55
+ }
56
+
57
+ describe("booting Switchboard when another process holds the singleton", () => {
58
+ it("boots the API without the workflow runtime, naming the owner", async () => {
59
+ const tempRoot = await mkdtemp(join(tmpdir(), "switchboard-wf-conflict-"));
60
+ const previousReactorDb = process.env.PH_REACTOR_DATABASE_URL;
61
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
62
+ const logger = stubLogger();
63
+ let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
64
+
65
+ try {
66
+ // The boot RESOLVES. That is the whole finding.
67
+ switchboard = await bootSwitchboard(
68
+ {
69
+ server: { port: 0 },
70
+ storage: { readModel: { url: join(tempRoot, "read-model") } },
71
+ features: { workflows: true, mcp: false },
72
+ packages: { disableLocal: true },
73
+ identity: { keypairPath: join(tempRoot, "identity.json") },
74
+ },
75
+ { logger },
76
+ );
77
+
78
+ // The API is up and serving: the reactor answers, the document models
79
+ // are registered, everything but workflows works.
80
+ const { results } = await switchboard.reactor.getDocumentModelModules();
81
+ expect(results.length).toBeGreaterThan(0);
82
+
83
+ // And the runtime is absent, said so rather than half-present.
84
+ expect(switchboard.workflowTriggers).toBeUndefined();
85
+
86
+ // Warned, not errored into silence, and the warning names who holds it
87
+ // and how to get workflows back.
88
+ const warning = logger.warn.mock.calls
89
+ .map((call) => String(call[0]))
90
+ .find((line) => line.includes("workflow singleton"));
91
+ expect(warning).toBeDefined();
92
+ expect(warning).toContain(HOLDER);
93
+ expect(warning).toContain(HELD_UNTIL);
94
+ expect(warning).toContain("WITHOUT the workflow runtime");
95
+ expect(warning).toContain("PH_WORKFLOWS_SINGLETON_OWNER");
96
+
97
+ await switchboard.shutdown();
98
+ switchboard = undefined;
99
+ } finally {
100
+ await switchboard?.shutdown();
101
+ if (previousReactorDb === undefined) {
102
+ delete process.env.PH_REACTOR_DATABASE_URL;
103
+ } else {
104
+ process.env.PH_REACTOR_DATABASE_URL = previousReactorDb;
105
+ }
106
+ await rm(tempRoot, { recursive: true, force: true });
107
+ }
108
+ }, 60_000);
109
+ });
@@ -0,0 +1,122 @@
1
+ // A Switchboard refused the workflow singleton at boot retries the claim and
2
+ // starts workflows once the holder lets go. The runtime it composes then has
3
+ // to be reachable: its GraphQL face mounted, not only registered.
4
+ import { mkdtemp, rm } from "node:fs/promises";
5
+ import { tmpdir } from "node:os";
6
+ import { join } from "node:path";
7
+ import {
8
+ acquireWorkflowSingletonLease,
9
+ type WorkflowSingletonLease,
10
+ } from "@powerhousedao/reactor-workflow";
11
+ import {
12
+ createRelationalDb,
13
+ type IRelationalDb,
14
+ } from "@powerhousedao/shared/processors";
15
+ import type { ILogger } from "document-model";
16
+ import { Kysely, PostgresDialect } from "kysely";
17
+ import pg from "pg";
18
+ import { describe, expect, it, vi } from "vitest";
19
+ import type { startSwitchboard } from "../src/server.mjs";
20
+ import { bootSwitchboard } from "./boot-helpers.js";
21
+
22
+ const PG_URL = process.env.REACTOR_TEST_PG_URL;
23
+ const DATABASE = "switchboard_workflow_retry";
24
+
25
+ function stubLogger(): ILogger {
26
+ const logger = {
27
+ verbose: vi.fn(),
28
+ debug: vi.fn(),
29
+ info: vi.fn(),
30
+ warn: vi.fn(),
31
+ error: vi.fn(),
32
+ child: vi.fn(),
33
+ };
34
+ logger.child.mockReturnValue(logger);
35
+ return logger as unknown as ILogger;
36
+ }
37
+
38
+ async function queryHealth(port: number): Promise<string | undefined> {
39
+ const response = await fetch(`http://127.0.0.1:${port}/graphql`, {
40
+ method: "POST",
41
+ headers: { "content-type": "application/json" },
42
+ body: JSON.stringify({ query: "{ workflowRuntime { health } }" }),
43
+ });
44
+ const body = (await response.json()) as {
45
+ data?: { workflowRuntime?: { health?: string } };
46
+ };
47
+ return body.data?.workflowRuntime?.health;
48
+ }
49
+
50
+ describe.skipIf(!PG_URL)(
51
+ "a Switchboard that claims the workflow singleton after boot [Postgres]",
52
+ () => {
53
+ it("serves the workflow GraphQL API once it has the claim", async () => {
54
+ const admin = new pg.Pool({ connectionString: PG_URL });
55
+ await admin.query(`DROP DATABASE IF EXISTS "${DATABASE}" WITH (FORCE)`);
56
+ await admin.query(`CREATE DATABASE "${DATABASE}"`);
57
+ const url = new URL(PG_URL!);
58
+ url.pathname = `/${DATABASE}`;
59
+ const readModelUrl = url.toString();
60
+ const holderDb = new Kysely<unknown>({
61
+ dialect: new PostgresDialect({
62
+ pool: new pg.Pool({ connectionString: readModelUrl, max: 2 }),
63
+ }),
64
+ });
65
+ const tempRoot = await mkdtemp(join(tmpdir(), "switchboard-wf-retry-"));
66
+ const previousReactorDb = process.env.PH_REACTOR_DATABASE_URL;
67
+ process.env.PH_REACTOR_DATABASE_URL = join(tempRoot, "reactor-storage");
68
+ let holder: WorkflowSingletonLease | undefined;
69
+ let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
70
+ try {
71
+ // Another live host holds the singleton over the same journal.
72
+ holder = await acquireWorkflowSingletonLease({
73
+ relationalDb: createRelationalDb(holderDb) as IRelationalDb,
74
+ logger: stubLogger(),
75
+ owner: "the-other-host",
76
+ });
77
+
78
+ switchboard = await bootSwitchboard(
79
+ {
80
+ server: { port: 0 },
81
+ storage: { readModel: { url: readModelUrl } },
82
+ features: { workflows: true, mcp: false },
83
+ packages: { disableLocal: true },
84
+ identity: { keypairPath: join(tempRoot, "identity.json") },
85
+ },
86
+ { logger: stubLogger() },
87
+ );
88
+ expect(switchboard.workflowTriggers).toBeUndefined();
89
+ expect(await queryHealth(switchboard.port)).toBeUndefined();
90
+
91
+ await holder.release();
92
+ holder = undefined;
93
+
94
+ const port = switchboard.port;
95
+ await vi.waitFor(
96
+ async () => expect(await queryHealth(port)).toBe("ok"),
97
+ { timeout: 60_000, interval: 500 },
98
+ );
99
+ expect(switchboard.workflowTriggers).toEqual({ status: "available" });
100
+
101
+ await switchboard.shutdown();
102
+ switchboard = undefined;
103
+ } finally {
104
+ await holder?.release();
105
+ await switchboard?.shutdown();
106
+ await holderDb.destroy();
107
+ if (previousReactorDb === undefined) {
108
+ delete process.env.PH_REACTOR_DATABASE_URL;
109
+ } else {
110
+ process.env.PH_REACTOR_DATABASE_URL = previousReactorDb;
111
+ }
112
+ // Not forced: terminating a pool's idle connections under it raises
113
+ // an unhandled error. A database left behind is dropped next run.
114
+ await admin
115
+ .query(`DROP DATABASE IF EXISTS "${DATABASE}"`)
116
+ .catch(() => undefined);
117
+ await admin.end();
118
+ await rm(tempRoot, { recursive: true, force: true });
119
+ }
120
+ }, 120_000);
121
+ },
122
+ );