@pikku/inspector 0.12.75 → 0.12.82

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 (223) hide show
  1. package/CHANGELOG.md +357 -0
  2. package/dist/add/add-analytics.d.ts +9 -0
  3. package/dist/add/add-analytics.js +178 -0
  4. package/dist/add/add-auth.js +0 -54
  5. package/dist/add/add-feature-flag.d.ts +2 -0
  6. package/dist/add/add-feature-flag.js +162 -0
  7. package/dist/add/add-middleware.js +66 -42
  8. package/dist/add/add-wire-addon.js +7 -4
  9. package/dist/add/add-wire-remote-addon.js +1 -0
  10. package/dist/error-codes.d.ts +1 -0
  11. package/dist/error-codes.js +1 -0
  12. package/dist/index.d.ts +3 -1
  13. package/dist/index.js +3 -1
  14. package/dist/inspector.js +5 -0
  15. package/dist/surface/collect-surface.d.ts +58 -0
  16. package/dist/surface/collect-surface.js +613 -0
  17. package/dist/types.d.ts +36 -12
  18. package/dist/utils/filter-inspector-state.js +14 -0
  19. package/dist/utils/load-addon-functions-meta.js +24 -11
  20. package/dist/utils/post-process.js +15 -0
  21. package/dist/utils/read-module-specifiers.d.ts +8 -0
  22. package/dist/utils/read-module-specifiers.js +23 -0
  23. package/dist/utils/read-tsconfig-out-dir.d.ts +5 -0
  24. package/dist/utils/read-tsconfig-out-dir.js +12 -0
  25. package/dist/utils/resolve-deploy-target.d.ts +7 -0
  26. package/dist/utils/resolve-deploy-target.js +15 -7
  27. package/dist/utils/serialize-inspector-state.d.ts +6 -0
  28. package/dist/utils/serialize-inspector-state.js +10 -0
  29. package/dist/visit.js +4 -0
  30. package/package.json +16 -6
  31. package/run-tests.sh +21 -6
  32. package/src/add/add-addon-bans.ts +0 -85
  33. package/src/add/add-agent-scorer.test.ts +0 -197
  34. package/src/add/add-agent-scorer.ts +0 -83
  35. package/src/add/add-agent.test.ts +0 -87
  36. package/src/add/add-agent.ts +0 -638
  37. package/src/add/add-approval-description.ts +0 -76
  38. package/src/add/add-auth.test.ts +0 -791
  39. package/src/add/add-auth.ts +0 -424
  40. package/src/add/add-channel.ts +0 -778
  41. package/src/add/add-cli-renderers.test.ts +0 -74
  42. package/src/add/add-cli.ts +0 -1006
  43. package/src/add/add-credential.ts +0 -186
  44. package/src/add/add-feature.test.ts +0 -306
  45. package/src/add/add-feature.ts +0 -253
  46. package/src/add/add-file-extends-core-type.ts +0 -66
  47. package/src/add/add-file-with-config.ts +0 -45
  48. package/src/add/add-file-with-factory.test.ts +0 -134
  49. package/src/add/add-file-with-factory.ts +0 -288
  50. package/src/add/add-functions.test.ts +0 -743
  51. package/src/add/add-functions.ts +0 -1548
  52. package/src/add/add-gateway.ts +0 -143
  53. package/src/add/add-http-route-ref.test.ts +0 -149
  54. package/src/add/add-http-route.ts +0 -530
  55. package/src/add/add-http-routes.ts +0 -471
  56. package/src/add/add-keyed-wiring.ts +0 -180
  57. package/src/add/add-mcp-prompt.ts +0 -157
  58. package/src/add/add-mcp-resource.ts +0 -175
  59. package/src/add/add-middleware.test.ts +0 -196
  60. package/src/add/add-middleware.ts +0 -879
  61. package/src/add/add-permission-auth.test.ts +0 -59
  62. package/src/add/add-permission.ts +0 -311
  63. package/src/add/add-personas.test.ts +0 -406
  64. package/src/add/add-personas.ts +0 -287
  65. package/src/add/add-queue-worker.ts +0 -117
  66. package/src/add/add-rpc-invocations.test.ts +0 -287
  67. package/src/add/add-rpc-invocations.ts +0 -221
  68. package/src/add/add-scenario-step.test.ts +0 -509
  69. package/src/add/add-schedule.ts +0 -120
  70. package/src/add/add-scope.test.ts +0 -419
  71. package/src/add/add-scope.ts +0 -221
  72. package/src/add/add-secret.ts +0 -10
  73. package/src/add/add-subject-scenario-steps.test.ts +0 -161
  74. package/src/add/add-system-role.test.ts +0 -297
  75. package/src/add/add-system-role.ts +0 -162
  76. package/src/add/add-trigger.ts +0 -165
  77. package/src/add/add-variable.ts +0 -10
  78. package/src/add/add-wire-addon.test.ts +0 -185
  79. package/src/add/add-wire-addon.ts +0 -146
  80. package/src/add/add-wire-remote-addon.ts +0 -80
  81. package/src/add/add-workflow-edge-cases.test.ts +0 -201
  82. package/src/add/add-workflow-fanout.test.ts +0 -166
  83. package/src/add/add-workflow-graph-foreach.test.ts +0 -174
  84. package/src/add/add-workflow-graph-input.test.ts +0 -94
  85. package/src/add/add-workflow-graph-notes.test.ts +0 -74
  86. package/src/add/add-workflow-graph.ts +0 -644
  87. package/src/add/add-workflow-on-error.test.ts +0 -118
  88. package/src/add/add-workflow-return-switch.test.ts +0 -121
  89. package/src/add/add-workflow-silent-drops.test.ts +0 -261
  90. package/src/add/add-workflow-sleep-expression.test.ts +0 -96
  91. package/src/add/add-workflow.test.ts +0 -155
  92. package/src/add/add-workflow.ts +0 -482
  93. package/src/add/addon-bans.test.ts +0 -121
  94. package/src/add/addon-contracts.test.ts +0 -221
  95. package/src/add/allow-escape-hatches.test.ts +0 -144
  96. package/src/add/audit-meta.test.ts +0 -75
  97. package/src/add/classified-sink-check.test.ts +0 -347
  98. package/src/add/config-value-metadata.test.ts +0 -263
  99. package/src/add/dynamic-import-check.test.ts +0 -119
  100. package/src/add/expect-eventually-scenario-only.test.ts +0 -108
  101. package/src/add/function-body-span.test.ts +0 -96
  102. package/src/add/inline-wiring-function.test.ts +0 -104
  103. package/src/add/pii-check.test.ts +0 -258
  104. package/src/add/rpc-type-cast.test.ts +0 -123
  105. package/src/add/scenario-instrumentation.test.ts +0 -89
  106. package/src/add/scenario-instrumentation.ts +0 -42
  107. package/src/add/scenario-marker.test.ts +0 -89
  108. package/src/add/scenario-not-extractable.test.ts +0 -159
  109. package/src/add/scenario-services.test.ts +0 -103
  110. package/src/add/scenario-skip.test.ts +0 -83
  111. package/src/add/validate-workflow-graph-addons.test.ts +0 -102
  112. package/src/add/wire-name-literal.test.ts +0 -117
  113. package/src/error-codes-emitted.test.ts +0 -76
  114. package/src/error-codes.ts +0 -145
  115. package/src/index.ts +0 -66
  116. package/src/inspector.ts +0 -575
  117. package/src/schema-generator.ts +0 -1
  118. package/src/source-file-cache.ts +0 -73
  119. package/src/tsconfig-paths.test.ts +0 -90
  120. package/src/types-map.ts +0 -141
  121. package/src/types.ts +0 -775
  122. package/src/utils/annotate-http-route-auth.test.ts +0 -161
  123. package/src/utils/annotate-http-route-auth.ts +0 -53
  124. package/src/utils/check-classified-sinks.ts +0 -215
  125. package/src/utils/check-pii-output.ts +0 -120
  126. package/src/utils/compute-diagnostics.test.ts +0 -106
  127. package/src/utils/compute-required-schemas.ts +0 -48
  128. package/src/utils/contract-hashes.test.ts +0 -553
  129. package/src/utils/contract-hashes.ts +0 -387
  130. package/src/utils/custom-types-generator.test.ts +0 -124
  131. package/src/utils/custom-types-generator.ts +0 -150
  132. package/src/utils/detect-schema-vendor.test.ts +0 -123
  133. package/src/utils/detect-schema-vendor.ts +0 -120
  134. package/src/utils/does-type-extend-core-type.ts +0 -53
  135. package/src/utils/ensure-function-metadata.ts +0 -385
  136. package/src/utils/extract-function-name.ts +0 -612
  137. package/src/utils/extract-function-node.ts +0 -70
  138. package/src/utils/extract-node-value.test.ts +0 -117
  139. package/src/utils/extract-node-value.ts +0 -172
  140. package/src/utils/extract-secret-usage.test.ts +0 -81
  141. package/src/utils/extract-secret-usage.ts +0 -73
  142. package/src/utils/extract-services.ts +0 -70
  143. package/src/utils/extract-surface-usage.test.ts +0 -147
  144. package/src/utils/extract-surface-usage.ts +0 -121
  145. package/src/utils/filter-inspector-state.test.ts +0 -2232
  146. package/src/utils/filter-inspector-state.ts +0 -1298
  147. package/src/utils/filter-utils.test.ts +0 -843
  148. package/src/utils/filter-utils.ts +0 -161
  149. package/src/utils/find-root-dir.ts +0 -68
  150. package/src/utils/find-zod-transform.test.ts +0 -70
  151. package/src/utils/find-zod-transform.ts +0 -103
  152. package/src/utils/get-exported-variable-name.ts +0 -48
  153. package/src/utils/get-files-and-methods.ts +0 -157
  154. package/src/utils/get-property-value.test.ts +0 -141
  155. package/src/utils/get-property-value.ts +0 -331
  156. package/src/utils/hash.ts +0 -26
  157. package/src/utils/literal-properties.ts +0 -97
  158. package/src/utils/load-addon-functions-meta.test.ts +0 -157
  159. package/src/utils/load-addon-functions-meta.ts +0 -508
  160. package/src/utils/load-addon-scopes.test.ts +0 -138
  161. package/src/utils/middleware.test.ts +0 -204
  162. package/src/utils/middleware.ts +0 -303
  163. package/src/utils/nested-project-filter.test.ts +0 -53
  164. package/src/utils/nested-project-filter.ts +0 -28
  165. package/src/utils/permissions-shorthand.test.ts +0 -79
  166. package/src/utils/permissions.test.ts +0 -125
  167. package/src/utils/permissions.ts +0 -129
  168. package/src/utils/post-process.test.ts +0 -1094
  169. package/src/utils/post-process.ts +0 -1604
  170. package/src/utils/resolve-addon-package.ts +0 -106
  171. package/src/utils/resolve-core-type.test.ts +0 -142
  172. package/src/utils/resolve-core-type.ts +0 -35
  173. package/src/utils/resolve-deploy-target.test.ts +0 -135
  174. package/src/utils/resolve-deploy-target.ts +0 -65
  175. package/src/utils/resolve-function-meta.ts +0 -25
  176. package/src/utils/resolve-function-types.ts +0 -42
  177. package/src/utils/resolve-identifier.ts +0 -46
  178. package/src/utils/resolve-ref-contract.ts +0 -71
  179. package/src/utils/resolve-versions.test.ts +0 -499
  180. package/src/utils/resolve-versions.ts +0 -228
  181. package/src/utils/schema-cache.test.ts +0 -124
  182. package/src/utils/schema-generator.test.ts +0 -272
  183. package/src/utils/schema-generator.ts +0 -780
  184. package/src/utils/secret-alias-services.test.ts +0 -146
  185. package/src/utils/secret-alias-services.ts +0 -58
  186. package/src/utils/serialize-inspector-state.ts +0 -742
  187. package/src/utils/serialize-mcp-json.ts +0 -150
  188. package/src/utils/serialize-middleware-groups-meta.ts +0 -36
  189. package/src/utils/serialize-openapi-json.ts +0 -277
  190. package/src/utils/serialize-permissions-groups-meta.ts +0 -7
  191. package/src/utils/single-declaration.ts +0 -57
  192. package/src/utils/type-utils.ts +0 -145
  193. package/src/utils/validate-auth-sessionless.ts +0 -30
  194. package/src/utils/validate-exposed-functions-gated.test.ts +0 -230
  195. package/src/utils/validate-exposed-functions-gated.ts +0 -103
  196. package/src/utils/validate-tags-resolve-to-middleware.test.ts +0 -108
  197. package/src/utils/validate-tags-resolve-to-middleware.ts +0 -48
  198. package/src/utils/workflow/derive-workflow-plan.test.ts +0 -178
  199. package/src/utils/workflow/derive-workflow-plan.ts +0 -117
  200. package/src/utils/workflow/dsl/deserialize-dsl-workflow.ts +0 -1465
  201. package/src/utils/workflow/dsl/deserialize-fanout-body.test.ts +0 -71
  202. package/src/utils/workflow/dsl/deserialize-fanout-fidelity.test.ts +0 -99
  203. package/src/utils/workflow/dsl/deserialize-graph-next.test.ts +0 -161
  204. package/src/utils/workflow/dsl/deserialize-roundtrip-bindings.test.ts +0 -121
  205. package/src/utils/workflow/dsl/deserialize-roundtrip-values.test.ts +0 -116
  206. package/src/utils/workflow/dsl/deserialize-roundtrip.test.ts +0 -145
  207. package/src/utils/workflow/dsl/extract-approval-policy.test.ts +0 -71
  208. package/src/utils/workflow/dsl/extract-dsl-workflow.ts +0 -2254
  209. package/src/utils/workflow/dsl/index.ts +0 -11
  210. package/src/utils/workflow/dsl/patterns.ts +0 -440
  211. package/src/utils/workflow/dsl/validation.ts +0 -203
  212. package/src/utils/workflow/graph/convert-dsl-to-graph-skip.test.ts +0 -35
  213. package/src/utils/workflow/graph/convert-dsl-to-graph.ts +0 -543
  214. package/src/utils/workflow/graph/fanout-node-identity.test.ts +0 -105
  215. package/src/utils/workflow/graph/finalize-workflow-wires.ts +0 -310
  216. package/src/utils/workflow/graph/finalize-workflows.test.ts +0 -50
  217. package/src/utils/workflow/graph/finalize-workflows.ts +0 -130
  218. package/src/utils/workflow/graph/index.ts +0 -28
  219. package/src/utils/workflow/graph/serialize-workflow-graph.ts +0 -240
  220. package/src/utils/workflow/graph/workflow-graph.types.ts +0 -281
  221. package/src/visit.ts +0 -181
  222. package/tsconfig.json +0 -19
  223. package/tsconfig.tsbuildinfo +0 -1
@@ -1,118 +0,0 @@
1
- import { strict as assert } from 'assert'
2
- import { describe, test } from 'node:test'
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
- import { tmpdir } from 'node:os'
5
- import { join } from 'node:path'
6
- import { inspect } from '../inspector.js'
7
- import type { InspectorLogger } from '../types.js'
8
- import { deserializeDslWorkflow } from '../utils/workflow/dsl/deserialize-dsl-workflow.js'
9
-
10
- const STEP_FILE = [
11
- "import { pikkuSessionlessFunc } from '@pikku/core'",
12
- 'export const chargeCard = pikkuSessionlessFunc({',
13
- " func: async () => ({ id: 'c' }),",
14
- '})',
15
- 'export const refundOrder = pikkuSessionlessFunc({',
16
- " func: async () => ({ id: 'r' }),",
17
- '})',
18
- ].join('\n')
19
-
20
- const WORKFLOW_BODY = [
21
- " await workflow.do('Charge', 'chargeCard', { id: data.id }, {",
22
- ' retries: 3,',
23
- " onError: 'refundOrder',",
24
- ' })',
25
- ' return { ok: true }',
26
- ].join('\n')
27
-
28
- async function inspectWorkflow(body: string): Promise<{
29
- nodes: Record<string, any>
30
- invoked: string[]
31
- code: string
32
- }> {
33
- const logger = {
34
- debug: () => {},
35
- info: () => {},
36
- warn: () => {},
37
- error: () => {},
38
- diagnostic: () => {},
39
- critical: () => {},
40
- hasCriticalErrors: () => false,
41
- } as unknown as InspectorLogger
42
-
43
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-oe-'))
44
- const stepFile = join(rootDir, 'w.steps.ts')
45
- const wfFile = join(rootDir, 'w.workflow.ts')
46
- await writeFile(stepFile, STEP_FILE)
47
- await writeFile(
48
- wfFile,
49
- [
50
- "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
51
- 'export const wf = pikkuWorkflowFunc(async (_, data: any, { workflow }) => {',
52
- body,
53
- '})',
54
- ].join('\n')
55
- )
56
- try {
57
- const state: any = await inspect(logger, [stepFile, wfFile], { rootDir })
58
- const gm = state.workflows.graphMeta
59
- const graph = (
60
- gm instanceof Map ? [...gm.values()] : Object.values(gm)
61
- )[0] as any
62
- assert.ok(graph, 'a graph should have been produced')
63
- return {
64
- nodes: graph.nodes,
65
- invoked: [...(state.rpc?.invokedFunctions ?? [])],
66
- code: deserializeDslWorkflow(graph),
67
- }
68
- } finally {
69
- await rm(rootDir, { recursive: true, force: true })
70
- }
71
- }
72
-
73
- describe('DSL onError — compensation is part of the graph', () => {
74
- test('the handler becomes a node the failing step points at', async () => {
75
- const { nodes } = await inspectWorkflow(WORKFLOW_BODY)
76
-
77
- const charge = Object.values(nodes).find(
78
- (n: any) => n.rpcName === 'chargeCard'
79
- ) as any
80
- assert.ok(charge, 'the charging step should exist')
81
- assert.ok(
82
- charge.onError,
83
- `the step must record its error route so the graph can render it, got: ${JSON.stringify(charge)}`
84
- )
85
-
86
- const handlerId = Array.isArray(charge.onError)
87
- ? charge.onError[0]
88
- : charge.onError
89
- const handler = nodes[handlerId]
90
- assert.ok(
91
- handler,
92
- `onError must point at a real node so an edge can be drawn, got id '${handlerId}' in ${JSON.stringify(Object.keys(nodes))}`
93
- )
94
- assert.equal(handler.rpcName, 'refundOrder')
95
- })
96
-
97
- test('the handler RPC is registered as invoked so it gets wired', async () => {
98
- const { invoked } = await inspectWorkflow(WORKFLOW_BODY)
99
-
100
- assert.ok(
101
- invoked.includes('refundOrder'),
102
- `the compensation RPC must be wired, got: ${JSON.stringify(invoked)}`
103
- )
104
- })
105
-
106
- test('onError survives regeneration back to code', async () => {
107
- const { code } = await inspectWorkflow(WORKFLOW_BODY)
108
-
109
- assert.ok(
110
- code.includes("onError: 'refundOrder'"),
111
- `onError must round-trip as an rpc name, got:\n${code}`
112
- )
113
- assert.ok(
114
- code.includes('retries: 3'),
115
- 'other options must still round-trip'
116
- )
117
- })
118
- })
@@ -1,121 +0,0 @@
1
- import { strict as assert } from 'assert'
2
- import { describe, test } from 'node:test'
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
- import { tmpdir } from 'node:os'
5
- import { join } from 'node:path'
6
- import { inspect } from '../inspector.js'
7
- import type { InspectorLogger } from '../types.js'
8
-
9
- const STEP_FILE = [
10
- "import { pikkuSessionlessFunc } from '@pikku/core'",
11
- 'export const ship = pikkuSessionlessFunc({',
12
- " func: async () => ({ id: 's', ok: true }),",
13
- '})',
14
- ].join('\n')
15
-
16
- async function inspectWorkflow(body: string): Promise<{
17
- nodes: Record<string, any>
18
- diags: string[]
19
- }> {
20
- const diags: string[] = []
21
- const logger = {
22
- debug: () => {},
23
- info: () => {},
24
- warn: () => {},
25
- error: () => {},
26
- diagnostic: (d: any) => diags.push(d?.message ?? String(d)),
27
- critical: (d: any) => diags.push(d?.message ?? String(d)),
28
- hasCriticalErrors: () => false,
29
- } as unknown as InspectorLogger
30
-
31
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-rs-'))
32
- const stepFile = join(rootDir, 'w.steps.ts')
33
- const wfFile = join(rootDir, 'w.workflow.ts')
34
- await writeFile(stepFile, STEP_FILE)
35
- await writeFile(
36
- wfFile,
37
- [
38
- "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
39
- 'export const wf = pikkuWorkflowFunc(async (_, data: any, { workflow }) => {',
40
- body,
41
- '})',
42
- ].join('\n')
43
- )
44
- try {
45
- const state: any = await inspect(logger, [stepFile, wfFile], { rootDir })
46
- const gm = state.workflows.graphMeta
47
- const graph = (
48
- gm instanceof Map ? [...gm.values()] : Object.values(gm)
49
- )[0] as any
50
- return { nodes: graph?.nodes ?? {}, diags }
51
- } finally {
52
- await rm(rootDir, { recursive: true, force: true })
53
- }
54
- }
55
-
56
- describe('switch extraction — a fall-through case is a real case', () => {
57
- test("case 'a': case 'b': records both values", async () => {
58
- const { nodes } = await inspectWorkflow(
59
- [
60
- ' switch (data.kind) {',
61
- " case 'a':",
62
- " case 'b':",
63
- " await workflow.do('Ship', 'ship', {})",
64
- ' break',
65
- ' }',
66
- ' return { ok: true }',
67
- ].join('\n')
68
- )
69
-
70
- const switchNode = Object.values(nodes).find(
71
- (n: any) => n.flow === 'switch'
72
- ) as any
73
- assert.ok(switchNode, 'a switch node should exist')
74
- const values = (switchNode.cases ?? []).map((c: any) => c.value)
75
- assert.deepEqual(
76
- values.sort(),
77
- ['a', 'b'],
78
- `a fall-through case must not be dropped, got: ${JSON.stringify(switchNode.cases)}`
79
- )
80
- const entries = new Set((switchNode.cases ?? []).map((c: any) => c.entry))
81
- assert.equal(entries.size, 1, 'both cases should share the same entry')
82
- })
83
- })
84
-
85
- describe('return extraction — a spread output is not silently dropped', () => {
86
- test('return { ...r, extra } keeps the spread', async () => {
87
- const { nodes } = await inspectWorkflow(
88
- [
89
- " const r = await workflow.do('Ship', 'ship', {})",
90
- ' return { ...r, extra: 1 }',
91
- ].join('\n')
92
- )
93
-
94
- const returnNode = Object.values(nodes).find(
95
- (n: any) => n.flow === 'return'
96
- ) as any
97
- assert.ok(returnNode, 'a return node should exist')
98
- assert.deepEqual(
99
- returnNode.spread,
100
- ['r'],
101
- `the spread source must be recorded, got: ${JSON.stringify(returnNode)}`
102
- )
103
- })
104
-
105
- test('return r produces a return node', async () => {
106
- const { nodes } = await inspectWorkflow(
107
- [" const r = await workflow.do('Ship', 'ship', {})", ' return r'].join(
108
- '\n'
109
- )
110
- )
111
-
112
- const returnNode = Object.values(nodes).find(
113
- (n: any) => n.flow === 'return'
114
- ) as any
115
- assert.ok(
116
- returnNode,
117
- `returning a variable must still produce a return node, got: ${JSON.stringify(nodes)}`
118
- )
119
- assert.deepEqual(returnNode.spread, ['r'])
120
- })
121
- })
@@ -1,261 +0,0 @@
1
- import { strict as assert } from 'assert'
2
- import { describe, test } from 'node:test'
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
- import { tmpdir } from 'node:os'
5
- import { join } from 'node:path'
6
- import { inspect } from '../inspector.js'
7
- import type { InspectorLogger } from '../types.js'
8
-
9
- function makeLogger(diags: string[]): InspectorLogger {
10
- return {
11
- debug: () => {},
12
- info: () => {},
13
- warn: () => {},
14
- error: () => {},
15
- diagnostic: ({ code, message }: any) => {
16
- diags.push(`${code}: ${message}`)
17
- },
18
- critical: (code: any, message: string) => {
19
- diags.push(`${code}: ${message}`)
20
- },
21
- hasCriticalErrors: () => false,
22
- }
23
- }
24
-
25
- const STEP_FILE = [
26
- "import { pikkuSessionlessFunc } from '@pikku/core'",
27
- 'export const listUsers = pikkuSessionlessFunc({',
28
- " func: async () => ({ users: [{ id: '1', email: 'a@b.c' }] }),",
29
- '})',
30
- 'export const rpcA = pikkuSessionlessFunc({',
31
- " func: async () => ({ id: 'a' }),",
32
- '})',
33
- 'export const rpcB = pikkuSessionlessFunc({',
34
- " func: async () => ({ id: 'b' }),",
35
- '})',
36
- 'export const rpcC = pikkuSessionlessFunc({',
37
- ' func: async () => ({ ok: true }),',
38
- '})',
39
- ].join('\n')
40
-
41
- async function inspectWorkflow(body: string) {
42
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-drops-'))
43
- const stepFile = join(rootDir, 'w.steps.ts')
44
- const wfFile = join(rootDir, 'w.workflow.ts')
45
- await writeFile(stepFile, STEP_FILE)
46
- await writeFile(
47
- wfFile,
48
- [
49
- "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
50
- 'export const wf = pikkuWorkflowFunc(async (_, data: any, { workflow }) => {',
51
- body,
52
- '})',
53
- ].join('\n')
54
- )
55
- const diags: string[] = []
56
- try {
57
- const state: any = await inspect(makeLogger(diags), [stepFile, wfFile], {
58
- rootDir,
59
- })
60
- const metaMap = state.workflows.meta
61
- const meta = (
62
- metaMap instanceof Map ? [...metaMap.values()] : Object.values(metaMap)
63
- )[0] as any
64
- return {
65
- invoked: [...state.rpc.invokedFunctions] as string[],
66
- steps: (meta?.steps ?? []) as any[],
67
- diags,
68
- }
69
- } finally {
70
- await rm(rootDir, { recursive: true, force: true })
71
- }
72
- }
73
-
74
- describe('DSL extraction — constructs that must not be silently dropped', () => {
75
- test('a property access on the loop item (u.id) survives as an item input', async () => {
76
- const { steps } = await inspectWorkflow(
77
- [
78
- " const audience = await workflow.do('collect', 'listUsers', {})",
79
- ' await Promise.all(',
80
- ' audience.users.map(async (u) => {',
81
- " await workflow.do('send', 'rpcC', { userId: u.id, tag: 'x' })",
82
- ' })',
83
- ' )',
84
- ' return { ok: true }',
85
- ].join('\n')
86
- )
87
-
88
- const fanout = steps.find((s) => s.type === 'fanout')
89
- assert.ok(fanout, 'fanout step should be extracted')
90
- const send = fanout.body?.[0]
91
- assert.ok(send, 'the fanout body step should be extracted')
92
- assert.deepEqual(
93
- send.inputs?.userId,
94
- { from: 'item', path: 'id' },
95
- `u.id must map to the loop item, got: ${JSON.stringify(send.inputs)}`
96
- )
97
- assert.deepEqual(
98
- send.inputs?.tag,
99
- { from: 'literal', value: 'x' },
100
- 'sibling literal inputs must still survive'
101
- )
102
- })
103
-
104
- test('array destructuring of a Promise.all group keeps both steps and binds each result', async () => {
105
- const { invoked, steps } = await inspectWorkflow(
106
- [
107
- ' const [org, user] = await Promise.all([',
108
- " workflow.do('create org', 'rpcA', {}),",
109
- " workflow.do('create user', 'rpcB', {}),",
110
- ' ])',
111
- ' return { orgId: org.id, userId: user.id }',
112
- ].join('\n')
113
- )
114
-
115
- assert.ok(invoked.includes('rpcA'), 'rpcA must not be dropped')
116
- assert.ok(invoked.includes('rpcB'), 'rpcB must not be dropped')
117
-
118
- const parallel = steps.find((s) => s.type === 'parallel')
119
- assert.ok(parallel, 'a parallel step should be extracted')
120
- assert.deepEqual(
121
- parallel.children?.map((c: any) => c.outputVar),
122
- ['org', 'user'],
123
- 'each destructured name should bind to its matching child step'
124
- )
125
-
126
- const ret = steps.find((s) => s.type === 'return')
127
- assert.deepEqual(
128
- ret?.outputs?.orgId,
129
- { from: 'outputVar', name: 'org', path: 'id' },
130
- 'org.id must resolve to the bound step output, not degrade to a trigger input'
131
- )
132
- })
133
-
134
- test('object destructuring of a step result reports a diagnostic instead of silently dropping', async () => {
135
- const { invoked, diags } = await inspectWorkflow(
136
- [
137
- " const { id } = await workflow.do('s', 'rpcA', {})",
138
- ' return { id }',
139
- ].join('\n')
140
- )
141
-
142
- const dropped = !invoked.includes('rpcA')
143
- assert.ok(
144
- !dropped || diags.length > 0,
145
- 'if the step is dropped the user must be told — silence is the bug'
146
- )
147
- })
148
-
149
- test('a counting for-of (Array(n).keys()) hard-errors instead of silently dropping its step', async () => {
150
- const { invoked, steps, diags } = await inspectWorkflow(
151
- [
152
- ' for (const i of [...Array(3).keys()]) {',
153
- " await workflow.do(`poll #${i}`, 'rpcA', {})",
154
- ' }',
155
- ' return { ok: true }',
156
- ].join('\n')
157
- )
158
-
159
- // The DSL extractor can't model a computed/counting iterable, so it must
160
- // FAIL LOUDLY (INVALID_DSL_WORKFLOW), not swallow the loop and every
161
- // workflow.do inside it — the silent drop is exactly what stranded
162
- // checkSandboxHostCapacity out of invokedFunctions and bricked prod.
163
- assert.ok(
164
- diags.length > 0,
165
- 'a counting for-of must raise a diagnostic, not vanish silently'
166
- )
167
- assert.ok(
168
- diags.some((d) => /for-of/i.test(d)),
169
- `the diagnostic must name the offending for-of, got: ${JSON.stringify(diags)}`
170
- )
171
- // And it must NOT have quietly produced a fanout with zero body steps.
172
- assert.ok(
173
- !invoked.includes('rpcA') || diags.length > 0,
174
- 'if rpcA was dropped the user must be told'
175
- )
176
- const fanout = steps.find((s) => s.type === 'fanout')
177
- assert.ok(
178
- !fanout,
179
- 'a counting loop must not serialize as a fanout — it is not a data-array fanout'
180
- )
181
- })
182
-
183
- /**
184
- * The diagnostic used to blame the iterable in every case, including this one
185
- * where the iterable is a perfectly good data array. That sent readers looking
186
- * at `data.tagsToRemove` when the real obstacle was the `if` in the body, so
187
- * the message has to distinguish the two causes.
188
- */
189
- test('a branch inside a fanout body hard-errors, and the diagnostic blames the branch not the iterable', async () => {
190
- const { steps, diags } = await inspectWorkflow(
191
- [
192
- ' for (const tag of data.tags) {',
193
- ' if (tag.length > 2) {',
194
- " await workflow.do('add', 'rpcA', { tag })",
195
- ' }',
196
- ' }',
197
- ' return { ok: true }',
198
- ].join('\n')
199
- )
200
-
201
- assert.ok(
202
- diags.length > 0,
203
- 'a branch inside a fanout body must raise a diagnostic, not vanish silently'
204
- )
205
- assert.ok(
206
- diags.some((d) => /if\/switch/.test(d)),
207
- `the diagnostic must name the branch as the cause, got: ${JSON.stringify(diags)}`
208
- )
209
- assert.ok(
210
- !diags.some((d) => /iterable must be a data array/.test(d)),
211
- `data.tags IS a data array — the diagnostic must not blame it, got: ${JSON.stringify(diags)}`
212
- )
213
- const fanout = steps.find((s) => s.type === 'fanout')
214
- assert.ok(!fanout, 'a fanout whose body was dropped must not serialize')
215
- })
216
-
217
- /**
218
- * The hazard the hard error exists for is a *dropped step*. A loop that only
219
- * massages locals has no step to drop, so failing the build on it is a false
220
- * positive — and it fired on real workflows that were doing nothing wrong.
221
- */
222
- test('a for-of that calls no workflow step is not an error — there is nothing to drop', async () => {
223
- const { steps, diags } = await inspectWorkflow(
224
- [
225
- " const first = await workflow.do('get', 'rpcA', {})",
226
- ' const merged: Record<string, string> = {}',
227
- ' for (const dup of data.duplicates) {',
228
- ' if (dup.phone && !first.phone) {',
229
- ' merged.phone = dup.phone',
230
- ' }',
231
- ' }',
232
- ' return { ok: true }',
233
- ].join('\n')
234
- )
235
-
236
- assert.deepEqual(
237
- diags,
238
- [],
239
- `a pure-computation loop must not fail the build, got: ${JSON.stringify(diags)}`
240
- )
241
- assert.ok(
242
- steps.some((s) => s.type === 'rpc'),
243
- 'the surrounding steps must still be extracted'
244
- )
245
- })
246
-
247
- test('a brace-less for-of body is still extracted as a sequential fanout', async () => {
248
- const { invoked, steps } = await inspectWorkflow(
249
- [
250
- ' for (const email of data.emails)',
251
- " await workflow.do('invite', 'rpcA', { email })",
252
- ' return { ok: true }',
253
- ].join('\n')
254
- )
255
-
256
- assert.ok(invoked.includes('rpcA'), 'rpcA must not be dropped')
257
- const fanout = steps.find((s) => s.type === 'fanout')
258
- assert.ok(fanout, 'a brace-less for-of should still produce a fanout step')
259
- assert.equal(fanout.mode, 'sequential')
260
- })
261
- })
@@ -1,96 +0,0 @@
1
- import { strict as assert } from 'assert'
2
- import { describe, test } from 'node:test'
3
- import { mkdtemp, rm, writeFile } from 'node:fs/promises'
4
- import { tmpdir } from 'node:os'
5
- import { join } from 'node:path'
6
- import { inspect } from '../inspector.js'
7
- import type { InspectorLogger } from '../types.js'
8
-
9
- const STEPS = [
10
- "import { pikkuSessionlessFunc } from '@pikku/core'",
11
- "export const notify = pikkuSessionlessFunc({ func: async () => ({ id: 'n' }) })",
12
- ].join('\n')
13
-
14
- async function run(body: string) {
15
- const diags: string[] = []
16
- const logger = {
17
- debug: () => {},
18
- info: () => {},
19
- warn: () => {},
20
- error: () => {},
21
- diagnostic: (...a: any[]) => diags.push(a.map(String).join(' ')),
22
- critical: (...a: any[]) => diags.push(a.map(String).join(' ')),
23
- hasCriticalErrors: () => false,
24
- } as unknown as InspectorLogger
25
-
26
- const rootDir = await mkdtemp(join(tmpdir(), 'pikku-sleep-'))
27
- const stepFile = join(rootDir, 'w.steps.ts')
28
- const wfFile = join(rootDir, 'w.workflow.ts')
29
- await writeFile(stepFile, STEPS)
30
- await writeFile(
31
- wfFile,
32
- [
33
- "import { pikkuWorkflowFunc } from '@pikku/core/workflow'",
34
- 'export const wf = pikkuWorkflowFunc(async (_, data: any, { workflow }) => {',
35
- body,
36
- '})',
37
- ].join('\n')
38
- )
39
- try {
40
- const state: any = await inspect(logger, [stepFile, wfFile], { rootDir })
41
- const gm = state.workflows.graphMeta
42
- const graph = (gm instanceof Map ? [...gm.values()] : Object.values(gm))[0]
43
- return { graph: graph as any, diags }
44
- } finally {
45
- await rm(rootDir, { recursive: true, force: true })
46
- }
47
- }
48
-
49
- describe('sleep with a duration that is not a literal', () => {
50
- test('a variable duration inside a loop is kept, not rejected', async () => {
51
- const { graph, diags } = await run(
52
- [
53
- ' for (const interval of data.intervals) {',
54
- ' await workflow.sleep(`Wait ${interval}`, interval)',
55
- " await workflow.do('Notify', 'notify', {})",
56
- ' }',
57
- ' return { sent: true }',
58
- ].join('\n')
59
- )
60
-
61
- assert.deepEqual(
62
- diags.filter((d) => d.includes('sleep')),
63
- [],
64
- 'a duration only known at runtime is legal — the closure evaluates it'
65
- )
66
-
67
- const sleepNode = Object.values(graph?.nodes ?? {}).find(
68
- (n: any) => n.flow === 'sleep'
69
- ) as any
70
- assert.ok(sleepNode, 'the sleep must still appear in the graph')
71
- assert.equal(
72
- sleepNode.expression,
73
- 'interval',
74
- 'its source text is recorded so the graph can show what it waits on'
75
- )
76
- })
77
-
78
- test('a literal duration still round-trips as a value', async () => {
79
- const { graph, diags } = await run(
80
- [" await workflow.sleep('Wait', '5s')", ' return { sent: true }'].join(
81
- '\n'
82
- )
83
- )
84
-
85
- assert.deepEqual(diags, [])
86
- const sleepNode = Object.values(graph?.nodes ?? {}).find(
87
- (n: any) => n.flow === 'sleep'
88
- ) as any
89
- assert.equal(sleepNode?.duration, '5s')
90
- assert.equal(
91
- sleepNode?.expression,
92
- undefined,
93
- 'a literal is a value, not an expression'
94
- )
95
- })
96
- })