@powerhousedao/switchboard 6.2.3 → 6.3.0-dev.10

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 +1122 -2
  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
@@ -1,5 +1,5 @@
1
1
  import { PGlite } from "@electric-sql/pglite";
2
- import { AtomicNodeFs } from "@powerhousedao/pglite-fs";
2
+ import { NodeFS } from "@electric-sql/pglite/nodefs";
3
3
  import {
4
4
  JobStatus,
5
5
  ReactorBuilder,
@@ -26,7 +26,7 @@ import {
26
26
  vi,
27
27
  } from "vitest";
28
28
  import { ClosablePGliteDialect } from "../src/pglite-dialect.js";
29
- import { startSwitchboard } from "../src/server.mjs";
29
+ import { bootSwitchboard } from "./boot-helpers.js";
30
30
  import { StoredDocumentsRefusedError } from "../src/unsupported-stored-documents.mjs";
31
31
 
32
32
  const ENV_KEYS = [
@@ -65,9 +65,7 @@ function stubLogger(): StubLogger {
65
65
  /** A reactor store holding one drive created at base-reducer 7. */
66
66
  async function seedStore(dir: string): Promise<void> {
67
67
  const db = new Kysely<Database>({
68
- dialect: new ClosablePGliteDialect(
69
- new PGlite({ fs: new AtomicNodeFs(dir) }),
70
- ),
68
+ dialect: new ClosablePGliteDialect(new PGlite({ fs: new NodeFS(dir) })),
71
69
  });
72
70
  const module = await new ReactorBuilder()
73
71
  .withKysely(db)
@@ -89,17 +87,22 @@ async function seedStore(dir: string): Promise<void> {
89
87
  const { status } = await module.reactor.getJobStatus(created.id);
90
88
  return status === JobStatus.READ_READY || status === JobStatus.FAILED;
91
89
  },
92
- { timeout: 10_000, interval: 5 },
90
+ // PGlite is several times slower on the Windows runner.
91
+ { timeout: BOOT_TIMEOUT, interval: 5 },
93
92
  );
94
93
  expect((await module.reactor.getJobStatus(created.id)).status).toBe(
95
94
  JobStatus.READ_READY,
96
95
  );
97
96
  await module.reactor.kill().completed;
98
97
  await db.destroy();
98
+ // PGlite leaves its lockfile behind after a clean close.
99
+ await rm(join(dir, "postmaster.pid"), { force: true });
99
100
  }
100
101
 
101
102
  // PGlite boots are several times slower on Windows runners.
102
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 };
103
106
 
104
107
  describe("booting over documents this build does not run", () => {
105
108
  let seeded: string;
@@ -112,8 +115,8 @@ describe("booting over documents this build does not run", () => {
112
115
  }, BOOT_TIMEOUT);
113
116
 
114
117
  afterAll(async () => {
115
- await rm(seeded, { recursive: true, force: true });
116
- });
118
+ await rm(seeded, RM_OPTIONS);
119
+ }, BOOT_TIMEOUT);
117
120
 
118
121
  beforeEach(async () => {
119
122
  tempRoot = await mkdtemp(join(tmpdir(), "switchboard-stored-protocol-"));
@@ -126,7 +129,7 @@ describe("booting over documents this build does not run", () => {
126
129
  process.env.PH_REACTOR_DATABASE_URL,
127
130
  { recursive: true },
128
131
  );
129
- });
132
+ }, BOOT_TIMEOUT);
130
133
 
131
134
  afterEach(async () => {
132
135
  for (const key of ENV_KEYS) {
@@ -136,18 +139,19 @@ describe("booting over documents this build does not run", () => {
136
139
  process.env[key] = previous[key];
137
140
  }
138
141
  }
139
- await rm(tempRoot, { recursive: true, force: true });
140
- });
142
+ await rm(tempRoot, RM_OPTIONS);
143
+ }, BOOT_TIMEOUT);
141
144
 
142
145
  function boot(logger: ILogger) {
143
- return startSwitchboard({
144
- workflows: { enabled: false },
145
- port: 0,
146
- mcp: false,
147
- disableLocalPackages: true,
148
- identity: { keypairPath: join(tempRoot, "identity.json") },
149
- logger,
150
- });
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
+ );
151
155
  }
152
156
 
153
157
  it(
@@ -8,7 +8,8 @@ import { join } from "node:path";
8
8
  import type { ILogger } from "document-model";
9
9
  import { describe, expect, it, vi } from "vitest";
10
10
  import type * as RenownModule from "../src/renown.js";
11
- import { startSwitchboard } from "../src/server.mjs";
11
+ import type { startSwitchboard } from "../src/server.mjs";
12
+ import { bootSwitchboard } from "./boot-helpers.js";
12
13
 
13
14
  vi.mock("../src/renown.js", async (importOriginal) => ({
14
15
  ...(await importOriginal<typeof RenownModule>()),
@@ -37,14 +38,15 @@ describe("booting Switchboard without Renown", () => {
37
38
  let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
38
39
 
39
40
  try {
40
- switchboard = await startSwitchboard({
41
- workflows: { enabled: false },
42
- dbPath: join(tempRoot, "read-model"),
43
- port: 0,
44
- mcp: false,
45
- disableLocalPackages: true,
46
- logger,
47
- });
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
+ );
48
50
 
49
51
  expect(
50
52
  logger.warn.mock.calls.some(
@@ -3,7 +3,8 @@ import { tmpdir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import type { ILogger } from "document-model";
5
5
  import { describe, expect, it, vi } from "vitest";
6
- import { startSwitchboard } from "../src/server.mjs";
6
+ import type { startSwitchboard } from "../src/server.mjs";
7
+ import { bootSwitchboard } from "./boot-helpers.js";
7
8
 
8
9
  function stubLogger(): ILogger & { info: ReturnType<typeof vi.fn> } {
9
10
  const logger = {
@@ -31,15 +32,16 @@ describe("booting Switchboard", () => {
31
32
  let switchboard: Awaited<ReturnType<typeof startSwitchboard>> | undefined;
32
33
 
33
34
  try {
34
- switchboard = await startSwitchboard({
35
- workflows: { enabled: false },
36
- dbPath: join(tempRoot, "read-model"),
37
- port: 0,
38
- mcp: false,
39
- disableLocalPackages: true,
40
- identity: { keypairPath: join(tempRoot, "identity.json") },
41
- logger,
42
- });
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
+ );
43
45
 
44
46
  const flagLog = logger.info.mock.calls.find(
45
47
  ([message]) => message === "Feature flags: @flags",
@@ -4,7 +4,7 @@ import { tmpdir } from "node:os";
4
4
  import { join } from "node:path";
5
5
  import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6
6
  import type * as RenownModule from "../src/renown.js";
7
- import { startSwitchboard } from "../src/server.mjs";
7
+ import { bootSwitchboard } from "./boot-helpers.js";
8
8
 
9
9
  vi.mock("../src/renown.js", async (importOriginal) => ({
10
10
  ...(await importOriginal<typeof RenownModule>()),
@@ -57,15 +57,16 @@ describe("booting with pooled workers under authEnforcement", () => {
57
57
  });
58
58
 
59
59
  function boot(numWorkers: number) {
60
- return startSwitchboard({
61
- workflows: { enabled: false },
62
- dbPath: join(tempRoot, "read-model"),
63
- port: 0,
64
- mcp: false,
65
- disableLocalPackages: true,
66
- logger: stubLogger(),
67
- workerPool: { numWorkers },
68
- });
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
+ );
69
70
  }
70
71
 
71
72
  it("fails with a self Renown source", async () => {
@@ -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
+ );