@powerhousedao/switchboard 6.2.3 → 6.3.0-dev.2

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 +1066 -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
@@ -0,0 +1,204 @@
1
+ // The resume routes wired to a real runtime over a real socket: a POST, a
2
+ // /sync POST and the confirm page each resume a waiting run.
3
+ import {
4
+ createHttpAdapter,
5
+ getDbClient,
6
+ HttpRouteService,
7
+ } from "@powerhousedao/reactor-api";
8
+ import {
9
+ CORE_PIECE_NAME,
10
+ CORE_PIECE_VERSION,
11
+ createWorkflowRuntime,
12
+ type WorkflowRuntimeService,
13
+ } from "@powerhousedao/reactor-workflow";
14
+ import {
15
+ createRelationalDb,
16
+ type IRelationalDb,
17
+ } from "@powerhousedao/shared/processors";
18
+ import {
19
+ actions,
20
+ reducer,
21
+ utils,
22
+ type WorkflowDocument,
23
+ } from "@powerhousedao/workflow/document-models/workflow";
24
+ import type { Action } from "document-model";
25
+ import type { Kysely } from "kysely";
26
+ import { afterAll, beforeAll, describe, expect, it } from "vitest";
27
+ import {
28
+ registerWaitpointRoutes,
29
+ waitpointBaseUrlOf,
30
+ } from "../../src/workflow/waitpoint-routes.js";
31
+
32
+ const CTX = { headers: {}, db: {}, user: { address: "0xabc" } } as never;
33
+ const POLL = { timeout: 15_000, interval: 25 };
34
+
35
+ const documents = new Map<string, WorkflowDocument>();
36
+ let service: WorkflowRuntimeService;
37
+ let close: () => Promise<void>;
38
+
39
+ // Manual trigger → Respond and wait for webhook → a Stop run answering `text`.
40
+ function workflow(id: string, text: string) {
41
+ let document = utils.createDocument();
42
+ document.header.id = id;
43
+ const step = (key: string, actionName: string, config: object) =>
44
+ actions.addStep({
45
+ id: key,
46
+ key,
47
+ name: key,
48
+ pieceName: CORE_PIECE_NAME,
49
+ pieceVersion: CORE_PIECE_VERSION,
50
+ actionName,
51
+ config,
52
+ });
53
+ const list: Action[] = [
54
+ actions.setTrigger({
55
+ id: "t1",
56
+ pieceName: CORE_PIECE_NAME,
57
+ pieceVersion: CORE_PIECE_VERSION,
58
+ triggerName: "manual",
59
+ config: {},
60
+ }),
61
+ step("wait", "waitForWebhook", {}),
62
+ step("stop", "stop", {
63
+ responseType: "raw",
64
+ text,
65
+ }),
66
+ actions.addEdge({ id: "e0", from: "t1", to: "wait", port: "next" }),
67
+ actions.addEdge({ id: "e1", from: "wait", to: "stop", port: "next" }),
68
+ actions.publishWorkflow({ publishedAt: "2026-01-01T00:00:00.000Z" }),
69
+ actions.setWorkflowStatus({ status: "ENABLED" }),
70
+ ];
71
+ for (const action of list) document = reducer(document, action as never);
72
+ documents.set(id, document);
73
+ }
74
+
75
+ // Fires the workflow to WAITING; returns the run and its resume URL.
76
+ async function waiting(id: string, text = "done") {
77
+ workflow(id, text);
78
+ const fired = await service.fire(id, undefined, "manual", undefined, CTX);
79
+ expect(fired.status).toBe("WAITING");
80
+ const runId = fired.runId!;
81
+ const wait = (await (await service.store())!.getSteps(runId)).find(
82
+ (row) => row.step_key === "wait",
83
+ )!;
84
+ const { resumeUrl } = JSON.parse(wait.output!) as { resumeUrl: string };
85
+ return { runId, resumeUrl };
86
+ }
87
+
88
+ async function settles(runId: string) {
89
+ await expect
90
+ .poll(
91
+ async () => (await (await service.store())!.getRun(runId))?.status,
92
+ POLL,
93
+ )
94
+ .toBe("SUCCEEDED");
95
+ const rows = await (await service.store())!.getSteps(runId);
96
+ return Object.fromEntries(rows.map((row) => [row.step_key, row]));
97
+ }
98
+
99
+ beforeAll(async () => {
100
+ const { adapter } = await createHttpAdapter("express");
101
+ adapter.setupMiddleware({});
102
+ const server = await adapter.listen(0, undefined, "127.0.0.1");
103
+ const { port } = server.address() as { port: number };
104
+ const routes = new HttpRouteService({
105
+ httpAdapter: adapter,
106
+ publicUrl: `http://127.0.0.1:${port}`,
107
+ });
108
+ const scope = routes.scopeFor("@powerhousedao/workflow");
109
+ const { db } = getDbClient();
110
+ service = createWorkflowRuntime({
111
+ relationalDb: createRelationalDb(
112
+ db as unknown as Kysely<unknown>,
113
+ ) as IRelationalDb,
114
+ reactorClient: {
115
+ find: () => Promise.resolve({ results: [] }),
116
+ get: (id: string) => {
117
+ const document = documents.get(id);
118
+ return document
119
+ ? Promise.resolve(structuredClone(document))
120
+ : Promise.reject(new Error(`No document ${id}`));
121
+ },
122
+ },
123
+ assertCanRead: () => Promise.resolve(undefined),
124
+ assertCanWrite: () => Promise.resolve(undefined),
125
+ waitpointBaseUrl: waitpointBaseUrlOf(scope),
126
+ } as never);
127
+ const handles = registerWaitpointRoutes(scope, service);
128
+ close = async () => {
129
+ for (const handle of handles) handle.dispose();
130
+ service.shutdown();
131
+ await new Promise((done) => server.close(() => done(undefined)));
132
+ };
133
+ });
134
+
135
+ afterAll(() => close());
136
+
137
+ describe("a resume URL served by the switchboard", () => {
138
+ it("resumes the run on a POST, with its body and query", async () => {
139
+ const { runId, resumeUrl } = await waiting("wf-route-post");
140
+ expect(resumeUrl).toMatch(/\/api\/@powerhousedao\/workflow\/waitpoints\//);
141
+
142
+ const response = await fetch(`${resumeUrl}?action=go`, {
143
+ method: "POST",
144
+ headers: { "content-type": "application/json" },
145
+ body: JSON.stringify({ n: 1 }),
146
+ });
147
+ expect(response.status).toBe(200);
148
+ expect(await response.json()).toMatchObject({ status: "resumed" });
149
+
150
+ const steps = await settles(runId);
151
+ expect(JSON.parse(steps.wait!.output!)).toMatchObject({
152
+ body: { n: 1 },
153
+ queryParams: { action: "go" },
154
+ });
155
+ // Used once.
156
+ const again = await fetch(resumeUrl, { method: "POST" });
157
+ expect(await again.json()).toMatchObject({ status: "already" });
158
+ });
159
+
160
+ it("answers a /sync POST with the resumed run's reply", async () => {
161
+ const { runId, resumeUrl } = await waiting(
162
+ "wf-route-sync",
163
+ "got {{steps.wait.output.body.n}}",
164
+ );
165
+
166
+ const response = await fetch(`${resumeUrl}/sync`, {
167
+ method: "POST",
168
+ headers: { "content-type": "application/json" },
169
+ body: JSON.stringify({ n: 7 }),
170
+ });
171
+ expect(response.status).toBe(200);
172
+ expect(await response.text()).toBe("got 7");
173
+ await settles(runId);
174
+ });
175
+
176
+ it("shows the confirm page, and resumes on its form", async () => {
177
+ const { runId, resumeUrl } = await waiting("wf-route-confirm");
178
+
179
+ const page = await fetch(`${resumeUrl}/confirm`);
180
+ expect(page.status).toBe(200);
181
+ expect(await page.text()).toContain("<form");
182
+ expect((await (await service.store())!.getRun(runId))?.status).toBe(
183
+ "WAITING",
184
+ );
185
+
186
+ const form = await fetch(`${resumeUrl}/confirm?action=approve`, {
187
+ method: "POST",
188
+ headers: {
189
+ "content-type": "application/x-www-form-urlencoded",
190
+ accept: "text/html",
191
+ },
192
+ body: new URLSearchParams({ reason: "ok" }).toString(),
193
+ });
194
+ expect(form.status).toBe(200);
195
+ expect(await form.text()).toContain("You approved");
196
+
197
+ const steps = await settles(runId);
198
+ expect(JSON.parse(steps.wait!.output!)).toMatchObject({
199
+ body: { reason: "ok" },
200
+ queryParams: { action: "approve" },
201
+ });
202
+ expect((await fetch(`${resumeUrl}/confirm`)).status).toBe(200);
203
+ });
204
+ });
@@ -43,6 +43,14 @@ beforeAll(async () => {
43
43
  apply(...list);
44
44
  return Promise.resolve(document);
45
45
  },
46
+ write: {
47
+ execute: (_id: string, _branch: string, list: Action[]) => {
48
+ apply(...list);
49
+ return Promise.resolve({
50
+ document: () => Promise.resolve(document),
51
+ });
52
+ },
53
+ },
46
54
  },
47
55
  });
48
56
  });
@@ -0,0 +1,384 @@
1
+ // The Switchboard's workflow runtime and its subgraph, composed over a real
2
+ // reactor with grants, with auth enforcement on and off (ADR 0005 §5-§9).
3
+ import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+ import {
7
+ ReactorBuilder,
8
+ ReactorClientBuilder,
9
+ type IReactorClient,
10
+ type InProcessReactorClientModule,
11
+ } from "@powerhousedao/reactor";
12
+ import {
13
+ AuthorizationPolicy,
14
+ getDbClient,
15
+ type Context,
16
+ type IAuthorizationService,
17
+ } from "@powerhousedao/reactor-api";
18
+ import {
19
+ initializeAuth,
20
+ withSignaturePolicy,
21
+ type DocumentModelModule,
22
+ type Grant,
23
+ type ISigner,
24
+ } from "@powerhousedao/shared/document-model";
25
+ import {
26
+ createRelationalDb,
27
+ type IRelationalDb,
28
+ } from "@powerhousedao/shared/processors";
29
+ import {
30
+ Connection,
31
+ REACTOR_CONNECTOR_ID,
32
+ actions as connectionActions,
33
+ type ConnectionDocument,
34
+ } from "@powerhousedao/workflow/document-models/connection";
35
+ import {
36
+ Workflow,
37
+ actions as workflowActions,
38
+ } from "@powerhousedao/workflow/document-models/workflow";
39
+ import {
40
+ MemoryKeyStorage,
41
+ RenownCryptoBuilder,
42
+ RenownCryptoSigner,
43
+ } from "@renown/sdk/node";
44
+ import type { ILogger } from "document-model";
45
+ import { documentModelDocumentModelModule } from "document-model";
46
+ import type { Kysely } from "kysely";
47
+ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
48
+ import {
49
+ composeWorkflowRuntime,
50
+ type ComposedWorkflowRuntime,
51
+ } from "../src/workflow-runtime.mjs";
52
+
53
+ const PIECE = "@powerhousedao/piece-switchboard-docs";
54
+ const PIECE_VERSION = "1.0.0";
55
+ const PUBLISHER = "0xpublisher";
56
+ const OTHER = "0xother";
57
+ const HOST = "0xhost";
58
+
59
+ const SOURCE = `
60
+ const action = (name, requireReactor) => ({
61
+ name,
62
+ displayName: name,
63
+ description: name,
64
+ requireAuth: false,
65
+ requireReactor,
66
+ props: {},
67
+ run: async () => undefined,
68
+ });
69
+ module.exports = {
70
+ app: {
71
+ displayName: "Docs",
72
+ description: "Works with documents",
73
+ logoUrl: "https://example.com/docs.png",
74
+ categories: [],
75
+ actions: {
76
+ create_doc: {
77
+ ...action("create_doc", "write"),
78
+ run: async (ctx) => {
79
+ const doc = await ctx.reactor.createEmpty("powerhouse/document-model");
80
+ return { id: doc.header.id };
81
+ },
82
+ },
83
+ pick_doc: {
84
+ ...action("pick_doc", "read"),
85
+ props: {
86
+ document: {
87
+ displayName: "Document",
88
+ type: "DROPDOWN",
89
+ required: false,
90
+ refreshers: [],
91
+ options: async (_values, ctx) => {
92
+ const page = await ctx.reactor.find({
93
+ type: "powerhouse/document-model",
94
+ });
95
+ return {
96
+ options: page.results.map((d) => ({
97
+ label: d.header.id,
98
+ value: d.header.id,
99
+ })),
100
+ };
101
+ },
102
+ },
103
+ },
104
+ },
105
+ },
106
+ triggers: {},
107
+ },
108
+ };
109
+ `;
110
+
111
+ const logger = {
112
+ verbose: vi.fn(),
113
+ debug: vi.fn(),
114
+ info: vi.fn(),
115
+ warn: vi.fn(),
116
+ error: vi.fn(),
117
+ child: () => logger,
118
+ } as unknown as ILogger;
119
+
120
+ const ctxOf = (address?: string) =>
121
+ ({
122
+ headers: {},
123
+ db: {},
124
+ user: address ? { address, appKey: undefined } : undefined,
125
+ }) as unknown as Context;
126
+
127
+ const grant = (
128
+ id: string,
129
+ principal: Grant["principal"],
130
+ capability: Grant["capability"],
131
+ ): Grant => ({ id, description: id, effect: "allow", principal, capability });
132
+
133
+ async function signerFor(address: string): Promise<ISigner> {
134
+ const crypto = await new RenownCryptoBuilder()
135
+ .withKeyPairStorage(new MemoryKeyStorage())
136
+ .build();
137
+ return new RenownCryptoSigner(crypto, "test", {
138
+ address,
139
+ networkId: "eip155",
140
+ chainId: 1,
141
+ });
142
+ }
143
+
144
+ const OPEN: IAuthorizationService = {
145
+ config: {
146
+ admins: [],
147
+ defaultProtection: false,
148
+ policy: AuthorizationPolicy.OPEN,
149
+ },
150
+ isSupremeAdmin: () => true,
151
+ canCreate: () => true,
152
+ canRead: () => Promise.resolve(true),
153
+ canWrite: () => Promise.resolve(true),
154
+ canManage: () => Promise.resolve(true),
155
+ canMutate: () => Promise.resolve(true),
156
+ };
157
+
158
+ type Resolver = (parent: unknown, args: unknown, ctx: Context) => unknown;
159
+
160
+ describe.each([
161
+ { enforcement: false, label: "auth enforcement off" },
162
+ { enforcement: true, label: "auth enforcement on" },
163
+ ])("the Switchboard workflow runtime, $label", ({ enforcement }) => {
164
+ let root = "";
165
+ let module: InProcessReactorClientModule;
166
+ let hostSigner: ISigner;
167
+ let publisher: IReactorClient;
168
+ let composed: ComposedWorkflowRuntime;
169
+ let queries: Record<string, Resolver>;
170
+ let mutations: Record<string, Resolver>;
171
+
172
+ async function createDocument(
173
+ client: IReactorClient,
174
+ model: DocumentModelModule,
175
+ id: string,
176
+ grants?: Grant[],
177
+ ) {
178
+ await client.create(
179
+ withSignaturePolicy(model.utils.createDocument(), "legacy", { id }),
180
+ );
181
+ if (grants) {
182
+ await client.execute(id, "main", [
183
+ initializeAuth({ version: 1, grants }),
184
+ ]);
185
+ }
186
+ }
187
+
188
+ beforeAll(async () => {
189
+ root = await mkdtemp(join(tmpdir(), "switchboard-reactor-access-"));
190
+ const bundle = join(root, "dist", "node", "pieces", "docs");
191
+ await mkdir(bundle, { recursive: true });
192
+ await writeFile(
193
+ join(bundle, "package.json"),
194
+ JSON.stringify({ name: PIECE, version: PIECE_VERSION, main: "index.js" }),
195
+ );
196
+ await writeFile(join(bundle, "index.js"), SOURCE);
197
+
198
+ hostSigner = await signerFor(HOST);
199
+ module = await new ReactorClientBuilder()
200
+ .withSigner(hostSigner)
201
+ .withCreateSignaturePolicy("legacy")
202
+ .withReactorBuilder(
203
+ new ReactorBuilder()
204
+ .withDocumentModelSources([
205
+ Workflow as unknown as DocumentModelModule,
206
+ Connection as unknown as DocumentModelModule,
207
+ documentModelDocumentModelModule as unknown as DocumentModelModule,
208
+ ])
209
+ .withExecutorConfig({
210
+ featureFlags: enforcement
211
+ ? { documentDecisions: true, authEnforcement: true }
212
+ : {},
213
+ })
214
+ // Test keys hold no Renown credential.
215
+ .withTrustPolicy({ authorizeSigner: () => Promise.resolve(true) }),
216
+ )
217
+ .buildModule();
218
+ publisher = await new ReactorClientBuilder()
219
+ .withReactor(
220
+ module.reactor,
221
+ module.eventBus,
222
+ module.documentIndexer,
223
+ module.documentView,
224
+ )
225
+ .withSigner(await signerFor(PUBLISHER))
226
+ .build();
227
+
228
+ // Only the publisher reads or writes the first; the host may write both.
229
+ const publisherOnly = grant(
230
+ "publisher",
231
+ { address: PUBLISHER },
232
+ { can: "execute" },
233
+ );
234
+ await createDocument(
235
+ publisher,
236
+ documentModelDocumentModelModule as never,
237
+ "doc-publisher",
238
+ [publisherOnly],
239
+ );
240
+ await createDocument(
241
+ publisher,
242
+ documentModelDocumentModelModule as never,
243
+ "doc-shared",
244
+ [publisherOnly, grant("host", { address: HOST }, { can: "execute" })],
245
+ );
246
+ await createDocument(publisher, Connection as never, "conn-open");
247
+ await publisher.execute("conn-open", "main", [
248
+ connectionActions.setConnector({
249
+ connectorId: REACTOR_CONNECTOR_ID,
250
+ authType: "REACTOR",
251
+ }),
252
+ connectionActions.setConfig({ config: { endpoint: "local" } }),
253
+ ]);
254
+ await createDocument(publisher, Workflow as never, "wf-create");
255
+ await publisher.execute("wf-create", "main", [
256
+ workflowActions.addStep({
257
+ id: "create",
258
+ key: "create",
259
+ name: "create",
260
+ pieceName: PIECE,
261
+ pieceVersion: PIECE_VERSION,
262
+ actionName: "create_doc",
263
+ config: {},
264
+ reactorConnectionId: "conn-open",
265
+ }),
266
+ ]);
267
+ const authorizationService = OPEN;
268
+ const { db } = getDbClient();
269
+ const relationalDb = createRelationalDb(
270
+ db as unknown as Kysely<unknown>,
271
+ ) as IRelationalDb;
272
+ const pieces = new Map([
273
+ [root, [{ name: PIECE, version: PIECE_VERSION, bundleDir: bundle }]],
274
+ ]);
275
+ composed = await composeWorkflowRuntime({
276
+ reactorClient: module.client,
277
+ clientModule: module,
278
+ relationalDb,
279
+ attachments: {} as never,
280
+ authorizationService,
281
+ pieces: { getPieces: () => pieces, onPiecesChange: () => undefined },
282
+ logger,
283
+ });
284
+ const subgraph = new composed.subgraph({
285
+ reactorClient: module.client,
286
+ authorizationService,
287
+ relationalDb,
288
+ } as never);
289
+ const resolvers = subgraph.resolvers as Record<
290
+ string,
291
+ Record<string, Resolver>
292
+ >;
293
+ queries = resolvers.WorkflowRuntimeQueries;
294
+ mutations = resolvers.WorkflowRuntimeMutations;
295
+ });
296
+
297
+ afterAll(async () => {
298
+ await composed.stop();
299
+ module.reactor.kill();
300
+ await rm(root, { recursive: true, force: true });
301
+ });
302
+
303
+ it("tells any caller whether auth is enforced", () => {
304
+ expect(queries.authEnforcement({}, {}, ctxOf())).toBe(enforcement);
305
+ expect(queries.authEnforcement({}, {}, ctxOf(OTHER))).toBe(enforcement);
306
+ });
307
+
308
+ it("names the host's identity and the grant form to signed-in callers only", () => {
309
+ expect(queries.reactorIdentity({}, {}, ctxOf())).toBeNull();
310
+ expect(queries.authConditions({}, {}, ctxOf())).toBeNull();
311
+
312
+ expect(queries.reactorIdentity({}, {}, ctxOf(OTHER))).toEqual({
313
+ address: HOST,
314
+ key: hostSigner.app?.key,
315
+ });
316
+ expect(queries.authConditions({}, {}, ctxOf(OTHER))).toBe(false);
317
+ });
318
+
319
+ it("resolves options reading as the caller", async () => {
320
+ const options = async (caller: string, reactorConnectionId?: string) =>
321
+ (
322
+ (await queries.blockOptions(
323
+ {},
324
+ {
325
+ block: {
326
+ pieceName: PIECE,
327
+ pieceVersion: PIECE_VERSION,
328
+ kind: "action",
329
+ name: "pick_doc",
330
+ },
331
+ propName: "document",
332
+ ...(reactorConnectionId ? { reactorConnectionId } : {}),
333
+ },
334
+ ctxOf(caller),
335
+ )) as { options: { value: string }[] }
336
+ ).options.map((option) => option.value);
337
+
338
+ expect(await options(PUBLISHER, "conn-open")).toEqual(
339
+ expect.arrayContaining(["doc-publisher", "doc-shared"]),
340
+ );
341
+ // Both grant only named principals; the read gate serves OTHER neither.
342
+ const asOther = await options(OTHER, "conn-open");
343
+ expect(asOther).not.toContain("doc-publisher");
344
+ expect(asOther).not.toContain("doc-shared");
345
+ });
346
+
347
+ it("grants the host on a document a run creates, so it can write it later", async () => {
348
+ const result = (await mutations.testStep(
349
+ {},
350
+ { workflowId: "wf-create", stepId: "create" },
351
+ ctxOf(PUBLISHER),
352
+ )) as { status: string; error?: string | null; output: { id: string } };
353
+ expect(result.error ?? undefined).toBeUndefined();
354
+ expect(result.status).toBe("SUCCEEDED");
355
+ const { id } = result.output;
356
+
357
+ const created = await module.client.get(id);
358
+ expect(created.state.auth.grants.map((g) => g.principal)).toEqual(
359
+ expect.arrayContaining([{ address: PUBLISHER }, { address: HOST }]),
360
+ );
361
+ // The host signs this write; under enforcement it lands only with its grant.
362
+ const renamed = await module.client.execute(id, "main", [
363
+ documentModelDocumentModelModule.actions.setModelName({ name: "Host" }),
364
+ ]);
365
+ expect(
366
+ (renamed.state as unknown as { global: { name: string } }).global.name,
367
+ ).toBe("Host");
368
+ });
369
+
370
+ it("checks a REACTOR connection's config instead of calling it unconfigured", async () => {
371
+ const result = (await mutations.checkConnection(
372
+ {},
373
+ { connectionId: "conn-open" },
374
+ ctxOf(PUBLISHER),
375
+ )) as { ok: boolean; detail: string | null };
376
+
377
+ expect(result).toMatchObject({
378
+ ok: true,
379
+ detail: "Local reactor, write access",
380
+ });
381
+ const connection = await module.client.get<ConnectionDocument>("conn-open");
382
+ expect(connection.state.global.status).toBe("OK");
383
+ });
384
+ });