@typeonce/effect-machine 0.47.1 → 0.48.0

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 (334) hide show
  1. package/AGENTS.md +374 -22
  2. package/CLAUDE.md +374 -22
  3. package/README.md +56 -7
  4. package/ai-docs/src/10_modeling/01_create-a-machine.ts +3 -3
  5. package/ai-docs/src/10_modeling/index.md +64 -17
  6. package/ai-docs/src/20_root-api/index.md +2 -2
  7. package/ai-docs/src/35_cluster/index.md +213 -0
  8. package/ai-docs/src/37_new-host/index.md +90 -0
  9. package/dist/LocalMachine.d.ts +417 -0
  10. package/dist/LocalMachine.d.ts.map +1 -0
  11. package/dist/LocalMachine.js +193 -0
  12. package/dist/LocalMachine.js.map +1 -0
  13. package/dist/Machine.d.ts +1120 -812
  14. package/dist/Machine.d.ts.map +1 -1
  15. package/dist/Machine.js +195 -186
  16. package/dist/Machine.js.map +1 -1
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +1 -0
  20. package/dist/index.js.map +1 -1
  21. package/dist/internal/machine/atom.d.ts +8 -10
  22. package/dist/internal/machine/atom.d.ts.map +1 -1
  23. package/dist/internal/machine/atom.js +7 -6
  24. package/dist/internal/machine/atom.js.map +1 -1
  25. package/dist/internal/machine/capability.d.ts +101 -0
  26. package/dist/internal/machine/capability.d.ts.map +1 -0
  27. package/dist/internal/machine/capability.js +70 -0
  28. package/dist/internal/machine/capability.js.map +1 -0
  29. package/dist/internal/machine/{invocationDefinition.d.ts → capturedInvocation.d.ts} +11 -11
  30. package/dist/internal/machine/capturedInvocation.d.ts.map +1 -0
  31. package/dist/internal/machine/{invocationDefinition.js → capturedInvocation.js} +2 -2
  32. package/dist/internal/machine/capturedInvocation.js.map +1 -0
  33. package/dist/internal/machine/child.d.ts +14 -0
  34. package/dist/internal/machine/child.d.ts.map +1 -0
  35. package/dist/internal/machine/child.js +19 -0
  36. package/dist/internal/machine/child.js.map +1 -0
  37. package/dist/internal/machine/childRegistry.d.ts +0 -2
  38. package/dist/internal/machine/childRegistry.d.ts.map +1 -1
  39. package/dist/internal/machine/childRegistry.js +2 -2
  40. package/dist/internal/machine/childRegistry.js.map +1 -1
  41. package/dist/internal/machine/cluster.d.ts +19 -4
  42. package/dist/internal/machine/cluster.d.ts.map +1 -1
  43. package/dist/internal/machine/cluster.js +263 -110
  44. package/dist/internal/machine/cluster.js.map +1 -1
  45. package/dist/internal/machine/clusterChild.d.ts +66 -0
  46. package/dist/internal/machine/clusterChild.d.ts.map +1 -0
  47. package/dist/internal/machine/clusterChild.js +105 -0
  48. package/dist/internal/machine/clusterChild.js.map +1 -0
  49. package/dist/internal/machine/clusterFailure.d.ts +14 -0
  50. package/dist/internal/machine/clusterFailure.d.ts.map +1 -0
  51. package/dist/internal/machine/clusterFailure.js +44 -0
  52. package/dist/internal/machine/clusterFailure.js.map +1 -0
  53. package/dist/internal/machine/clusterInstance.d.ts +194 -0
  54. package/dist/internal/machine/clusterInstance.d.ts.map +1 -0
  55. package/dist/internal/machine/clusterInstance.js +1076 -0
  56. package/dist/internal/machine/clusterInstance.js.map +1 -0
  57. package/dist/internal/machine/clusterKeepAlive.d.ts +47 -0
  58. package/dist/internal/machine/clusterKeepAlive.d.ts.map +1 -0
  59. package/dist/internal/machine/clusterKeepAlive.js +72 -0
  60. package/dist/internal/machine/clusterKeepAlive.js.map +1 -0
  61. package/dist/internal/machine/clusterProgress.d.ts +73 -0
  62. package/dist/internal/machine/clusterProgress.d.ts.map +1 -0
  63. package/dist/internal/machine/clusterProgress.js +116 -0
  64. package/dist/internal/machine/clusterProgress.js.map +1 -0
  65. package/dist/internal/machine/clusterProtocol.d.ts +37 -4
  66. package/dist/internal/machine/clusterProtocol.d.ts.map +1 -1
  67. package/dist/internal/machine/clusterProtocol.js +18 -4
  68. package/dist/internal/machine/clusterProtocol.js.map +1 -1
  69. package/dist/internal/machine/clusterProxy.d.ts +46 -0
  70. package/dist/internal/machine/clusterProxy.d.ts.map +1 -0
  71. package/dist/internal/machine/clusterProxy.js +150 -0
  72. package/dist/internal/machine/clusterProxy.js.map +1 -0
  73. package/dist/internal/machine/clusterRead.d.ts +61 -0
  74. package/dist/internal/machine/clusterRead.d.ts.map +1 -0
  75. package/dist/internal/machine/clusterRead.js +108 -0
  76. package/dist/internal/machine/clusterRead.js.map +1 -0
  77. package/dist/internal/machine/clusterSqlStorage.d.ts +16 -0
  78. package/dist/internal/machine/clusterSqlStorage.d.ts.map +1 -0
  79. package/dist/internal/machine/clusterSqlStorage.js +138 -0
  80. package/dist/internal/machine/clusterSqlStorage.js.map +1 -0
  81. package/dist/internal/machine/clusterStorageCheck.d.ts +19 -0
  82. package/dist/internal/machine/clusterStorageCheck.d.ts.map +1 -0
  83. package/dist/internal/machine/clusterStorageCheck.js +186 -0
  84. package/dist/internal/machine/clusterStorageCheck.js.map +1 -0
  85. package/dist/internal/machine/clusterTarget.d.ts +74 -0
  86. package/dist/internal/machine/clusterTarget.d.ts.map +1 -0
  87. package/dist/internal/machine/clusterTarget.js +81 -0
  88. package/dist/internal/machine/clusterTarget.js.map +1 -0
  89. package/dist/internal/machine/clusterTimer.d.ts +21 -0
  90. package/dist/internal/machine/clusterTimer.d.ts.map +1 -0
  91. package/dist/internal/machine/clusterTimer.js +50 -0
  92. package/dist/internal/machine/clusterTimer.js.map +1 -0
  93. package/dist/internal/machine/clusterWake.d.ts +61 -0
  94. package/dist/internal/machine/clusterWake.d.ts.map +1 -0
  95. package/dist/internal/machine/clusterWake.js +81 -0
  96. package/dist/internal/machine/clusterWake.js.map +1 -0
  97. package/dist/internal/machine/clusterWork.d.ts +130 -0
  98. package/dist/internal/machine/clusterWork.d.ts.map +1 -0
  99. package/dist/internal/machine/clusterWork.js +191 -0
  100. package/dist/internal/machine/clusterWork.js.map +1 -0
  101. package/dist/internal/machine/command.d.ts +4 -4
  102. package/dist/internal/machine/command.d.ts.map +1 -1
  103. package/dist/internal/machine/commandRuntime.d.ts +8 -6
  104. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  105. package/dist/internal/machine/commandRuntime.js +1 -5
  106. package/dist/internal/machine/commandRuntime.js.map +1 -1
  107. package/dist/internal/machine/configuration.d.ts +0 -13
  108. package/dist/internal/machine/configuration.d.ts.map +1 -1
  109. package/dist/internal/machine/configuration.js +0 -151
  110. package/dist/internal/machine/configuration.js.map +1 -1
  111. package/dist/internal/machine/errors.d.ts +11 -2
  112. package/dist/internal/machine/errors.d.ts.map +1 -1
  113. package/dist/internal/machine/errors.js +11 -2
  114. package/dist/internal/machine/errors.js.map +1 -1
  115. package/dist/internal/machine/executionPlan.d.ts +45 -3
  116. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  117. package/dist/internal/machine/executionPlan.js +328 -191
  118. package/dist/internal/machine/executionPlan.js.map +1 -1
  119. package/dist/internal/machine/implementation.d.ts +6 -6
  120. package/dist/internal/machine/implementation.d.ts.map +1 -1
  121. package/dist/internal/machine/inspectionRuntime.d.ts +1 -1
  122. package/dist/internal/machine/inspectionRuntime.d.ts.map +1 -1
  123. package/dist/internal/machine/invocationChanges.d.ts +60 -0
  124. package/dist/internal/machine/invocationChanges.d.ts.map +1 -0
  125. package/dist/internal/machine/invocationChanges.js +54 -0
  126. package/dist/internal/machine/invocationChanges.js.map +1 -0
  127. package/dist/internal/machine/{activities.d.ts → invocationDefinitions.d.ts} +8 -8
  128. package/dist/internal/machine/invocationDefinitions.d.ts.map +1 -0
  129. package/dist/internal/machine/{activities.js → invocationDefinitions.js} +5 -5
  130. package/dist/internal/machine/invocationDefinitions.js.map +1 -0
  131. package/dist/internal/machine/invocationEvent.d.ts +16 -7
  132. package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
  133. package/dist/internal/machine/invocationEvent.js +26 -12
  134. package/dist/internal/machine/invocationEvent.js.map +1 -1
  135. package/dist/internal/machine/invocationOutcome.d.ts +89 -0
  136. package/dist/internal/machine/invocationOutcome.d.ts.map +1 -0
  137. package/dist/internal/machine/invocationOutcome.js +76 -0
  138. package/dist/internal/machine/invocationOutcome.js.map +1 -0
  139. package/dist/internal/machine/invocationResolution.d.ts +137 -0
  140. package/dist/internal/machine/invocationResolution.d.ts.map +1 -0
  141. package/dist/internal/machine/invocationResolution.js +150 -0
  142. package/dist/internal/machine/invocationResolution.js.map +1 -0
  143. package/dist/internal/machine/logicScope.d.ts +24 -0
  144. package/dist/internal/machine/logicScope.d.ts.map +1 -0
  145. package/dist/internal/machine/logicScope.js +33 -0
  146. package/dist/internal/machine/logicScope.js.map +1 -0
  147. package/dist/internal/machine/machine.d.ts +17 -23
  148. package/dist/internal/machine/machine.d.ts.map +1 -1
  149. package/dist/internal/machine/machine.js +24 -40
  150. package/dist/internal/machine/machine.js.map +1 -1
  151. package/dist/internal/machine/observation.d.ts +3 -0
  152. package/dist/internal/machine/observation.d.ts.map +1 -1
  153. package/dist/internal/machine/observation.js +56 -2
  154. package/dist/internal/machine/observation.js.map +1 -1
  155. package/dist/internal/machine/plannedSnapshot.d.ts.map +1 -1
  156. package/dist/internal/machine/plannedSnapshot.js +11 -20
  157. package/dist/internal/machine/plannedSnapshot.js.map +1 -1
  158. package/dist/internal/machine/planner.d.ts +36 -23
  159. package/dist/internal/machine/planner.d.ts.map +1 -1
  160. package/dist/internal/machine/planner.js +15 -8
  161. package/dist/internal/machine/planner.js.map +1 -1
  162. package/dist/internal/machine/process.d.ts +17 -9
  163. package/dist/internal/machine/process.d.ts.map +1 -1
  164. package/dist/internal/machine/process.js +43 -503
  165. package/dist/internal/machine/process.js.map +1 -1
  166. package/dist/internal/machine/protocol.d.ts +1 -1
  167. package/dist/internal/machine/protocol.d.ts.map +1 -1
  168. package/dist/internal/machine/protocol.js +48 -10
  169. package/dist/internal/machine/protocol.js.map +1 -1
  170. package/dist/internal/machine/runtime.d.ts +20 -16
  171. package/dist/internal/machine/runtime.d.ts.map +1 -1
  172. package/dist/internal/machine/runtime.js +93 -88
  173. package/dist/internal/machine/runtime.js.map +1 -1
  174. package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -1
  175. package/dist/internal/machine/runtimeCompiled.js +359 -120
  176. package/dist/internal/machine/runtimeCompiled.js.map +1 -1
  177. package/dist/internal/machine/runtimeProtocol.d.ts +151 -82
  178. package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
  179. package/dist/internal/machine/runtimeProtocol.js +236 -219
  180. package/dist/internal/machine/runtimeProtocol.js.map +1 -1
  181. package/dist/internal/machine/stateDefinition.d.ts +0 -1
  182. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  183. package/dist/internal/machine/stateDefinition.js +0 -1
  184. package/dist/internal/machine/stateDefinition.js.map +1 -1
  185. package/dist/internal/machine/step.d.ts +486 -0
  186. package/dist/internal/machine/step.d.ts.map +1 -0
  187. package/dist/internal/machine/step.js +957 -0
  188. package/dist/internal/machine/step.js.map +1 -0
  189. package/dist/internal/machine/stepHost.d.ts +33 -0
  190. package/dist/internal/machine/stepHost.d.ts.map +1 -0
  191. package/dist/internal/machine/stepHost.js +501 -0
  192. package/dist/internal/machine/stepHost.js.map +1 -0
  193. package/dist/internal/machine/symbols.d.ts +2 -11
  194. package/dist/internal/machine/symbols.d.ts.map +1 -1
  195. package/dist/internal/machine/symbols.js +2 -2
  196. package/dist/internal/machine/symbols.js.map +1 -1
  197. package/dist/internal/machine/transition.d.ts +7 -7
  198. package/dist/internal/machine/transition.d.ts.map +1 -1
  199. package/dist/internal/testing/machine/check.d.ts.map +1 -1
  200. package/dist/internal/testing/machine/check.js +217 -25
  201. package/dist/internal/testing/machine/check.js.map +1 -1
  202. package/dist/internal/testing/machine/exploration.d.ts +19 -2
  203. package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
  204. package/dist/internal/testing/machine/exploration.js +14 -3
  205. package/dist/internal/testing/machine/exploration.js.map +1 -1
  206. package/dist/internal/testing/machine/format.js.map +1 -1
  207. package/dist/internal/testing/machine/probe.d.ts.map +1 -1
  208. package/dist/internal/testing/machine/probe.js +1 -1
  209. package/dist/internal/testing/machine/probe.js.map +1 -1
  210. package/dist/internal/testing/machine/runtime.d.ts +2 -2
  211. package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
  212. package/dist/internal/testing/machine/runtime.js +10 -3
  213. package/dist/internal/testing/machine/runtime.js.map +1 -1
  214. package/dist/internal/testing/machine/trace.d.ts +38 -3
  215. package/dist/internal/testing/machine/trace.d.ts.map +1 -1
  216. package/dist/internal/testing/machine/trace.js +180 -33
  217. package/dist/internal/testing/machine/trace.js.map +1 -1
  218. package/dist/internal/testing/machine/transitionCoverage.d.ts +1 -1
  219. package/dist/internal/testing/machine/transitionCoverage.d.ts.map +1 -1
  220. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  221. package/dist/internal/testing/machine/verification.js +9 -1
  222. package/dist/internal/testing/machine/verification.js.map +1 -1
  223. package/dist/internal/testing/machine/work.d.ts +47 -0
  224. package/dist/internal/testing/machine/work.d.ts.map +1 -0
  225. package/dist/internal/testing/machine/work.js +74 -0
  226. package/dist/internal/testing/machine/work.js.map +1 -0
  227. package/dist/testing/MachineTest.d.ts +253 -37
  228. package/dist/testing/MachineTest.d.ts.map +1 -1
  229. package/dist/testing/MachineTest.js +81 -9
  230. package/dist/testing/MachineTest.js.map +1 -1
  231. package/dist/unstable/cluster/ClusterMachine.d.ts +1071 -41
  232. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  233. package/dist/unstable/cluster/ClusterMachine.js +441 -12
  234. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  235. package/dist/unstable/cluster/ClusterMachineProxy.d.ts +257 -0
  236. package/dist/unstable/cluster/ClusterMachineProxy.d.ts.map +1 -0
  237. package/dist/unstable/cluster/ClusterMachineProxy.js +73 -0
  238. package/dist/unstable/cluster/ClusterMachineProxy.js.map +1 -0
  239. package/dist/unstable/cluster/SqlMachineStorage.d.ts +125 -0
  240. package/dist/unstable/cluster/SqlMachineStorage.d.ts.map +1 -0
  241. package/dist/unstable/cluster/SqlMachineStorage.js +62 -0
  242. package/dist/unstable/cluster/SqlMachineStorage.js.map +1 -0
  243. package/dist/unstable/cluster/index.d.ts +13 -0
  244. package/dist/unstable/cluster/index.d.ts.map +1 -1
  245. package/dist/unstable/cluster/index.js +13 -0
  246. package/dist/unstable/cluster/index.js.map +1 -1
  247. package/dist/unstable/reactivity/AtomMachine.d.ts +21 -26
  248. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  249. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  250. package/package.json +5 -3
  251. package/src/LocalMachine.ts +611 -0
  252. package/src/Machine.ts +1379 -1334
  253. package/src/index.ts +1 -0
  254. package/src/internal/machine/atom.ts +20 -49
  255. package/src/internal/machine/capability.ts +123 -0
  256. package/src/internal/machine/{invocationDefinition.ts → capturedInvocation.ts} +13 -13
  257. package/src/internal/machine/child.ts +25 -0
  258. package/src/internal/machine/childRegistry.ts +1 -4
  259. package/src/internal/machine/cluster.ts +420 -205
  260. package/src/internal/machine/clusterChild.ts +117 -0
  261. package/src/internal/machine/clusterFailure.ts +53 -0
  262. package/src/internal/machine/clusterInstance.ts +1779 -0
  263. package/src/internal/machine/clusterKeepAlive.ts +93 -0
  264. package/src/internal/machine/clusterProgress.ts +159 -0
  265. package/src/internal/machine/clusterProtocol.ts +46 -4
  266. package/src/internal/machine/clusterProxy.ts +233 -0
  267. package/src/internal/machine/clusterRead.ts +207 -0
  268. package/src/internal/machine/clusterSqlStorage.ts +181 -0
  269. package/src/internal/machine/clusterStorageCheck.ts +274 -0
  270. package/src/internal/machine/clusterTarget.ts +154 -0
  271. package/src/internal/machine/clusterTimer.ts +58 -0
  272. package/src/internal/machine/clusterWake.ts +100 -0
  273. package/src/internal/machine/clusterWork.ts +269 -0
  274. package/src/internal/machine/command.ts +4 -4
  275. package/src/internal/machine/commandRuntime.ts +12 -24
  276. package/src/internal/machine/configuration.ts +0 -181
  277. package/src/internal/machine/errors.ts +11 -2
  278. package/src/internal/machine/executionPlan.ts +493 -263
  279. package/src/internal/machine/implementation.ts +6 -6
  280. package/src/internal/machine/inspectionRuntime.ts +1 -1
  281. package/src/internal/machine/invocationChanges.ts +89 -0
  282. package/src/internal/machine/{activities.ts → invocationDefinitions.ts} +10 -10
  283. package/src/internal/machine/invocationEvent.ts +35 -12
  284. package/src/internal/machine/invocationOutcome.ts +145 -0
  285. package/src/internal/machine/invocationResolution.ts +307 -0
  286. package/src/internal/machine/logicScope.ts +47 -0
  287. package/src/internal/machine/machine.ts +35 -100
  288. package/src/internal/machine/observation.ts +67 -2
  289. package/src/internal/machine/plannedSnapshot.ts +17 -20
  290. package/src/internal/machine/planner.ts +75 -99
  291. package/src/internal/machine/process.ts +78 -722
  292. package/src/internal/machine/protocol.ts +70 -26
  293. package/src/internal/machine/runtime.ts +198 -195
  294. package/src/internal/machine/runtimeCompiled.ts +496 -171
  295. package/src/internal/machine/runtimeProtocol.ts +416 -305
  296. package/src/internal/machine/stateDefinition.ts +0 -5
  297. package/src/internal/machine/step.ts +1518 -0
  298. package/src/internal/machine/stepHost.ts +634 -0
  299. package/src/internal/machine/symbols.ts +3 -14
  300. package/src/internal/machine/transition.ts +11 -11
  301. package/src/internal/testing/machine/check.ts +240 -36
  302. package/src/internal/testing/machine/exploration.ts +42 -3
  303. package/src/internal/testing/machine/format.ts +1 -1
  304. package/src/internal/testing/machine/probe.ts +2 -1
  305. package/src/internal/testing/machine/runtime.ts +15 -5
  306. package/src/internal/testing/machine/trace.ts +261 -41
  307. package/src/internal/testing/machine/transitionCoverage.ts +1 -1
  308. package/src/internal/testing/machine/verification.ts +11 -5
  309. package/src/internal/testing/machine/work.ts +101 -0
  310. package/src/testing/MachineTest.ts +268 -44
  311. package/src/unstable/cluster/ClusterMachine.ts +1183 -59
  312. package/src/unstable/cluster/ClusterMachineProxy.ts +372 -0
  313. package/src/unstable/cluster/SqlMachineStorage.ts +136 -0
  314. package/src/unstable/cluster/index.ts +15 -0
  315. package/src/unstable/reactivity/AtomMachine.ts +27 -88
  316. package/dist/internal/machine/activities.d.ts.map +0 -1
  317. package/dist/internal/machine/activities.js.map +0 -1
  318. package/dist/internal/machine/invocation.d.ts +0 -33
  319. package/dist/internal/machine/invocation.d.ts.map +0 -1
  320. package/dist/internal/machine/invocation.js +0 -220
  321. package/dist/internal/machine/invocation.js.map +0 -1
  322. package/dist/internal/machine/invocationDefinition.d.ts.map +0 -1
  323. package/dist/internal/machine/invocationDefinition.js.map +0 -1
  324. package/dist/internal/machine/requirements.d.ts +0 -4
  325. package/dist/internal/machine/requirements.d.ts.map +0 -1
  326. package/dist/internal/machine/requirements.js +0 -2
  327. package/dist/internal/machine/requirements.js.map +0 -1
  328. package/dist/internal/machine/runtimeGeneric.d.ts +0 -3
  329. package/dist/internal/machine/runtimeGeneric.d.ts.map +0 -1
  330. package/dist/internal/machine/runtimeGeneric.js +0 -465
  331. package/dist/internal/machine/runtimeGeneric.js.map +0 -1
  332. package/src/internal/machine/invocation.ts +0 -376
  333. package/src/internal/machine/requirements.ts +0 -11
  334. package/src/internal/machine/runtimeGeneric.ts +0 -797
@@ -1,10 +1,17 @@
1
1
  /**
2
2
  * Planner trace construction shared by scenario runs and graph exploration.
3
3
  *
4
+ * A trace advances through the step of `Machine.toStep`, which alone decides
5
+ * which work runs, which result is still current, and what a result means to
6
+ * the machine. The trace keeps the state of that step for each trace it
7
+ * builds, and never evaluates the work it lists.
8
+ *
4
9
  * @since 0.4.0
5
10
  */
6
11
 
12
+ import * as Cause from "effect/Cause"
7
13
  import * as Effect from "effect/Effect"
14
+ import * as Exit from "effect/Exit"
8
15
  import * as Schema from "effect/Schema"
9
16
  import * as Machine from "../../../Machine.js"
10
17
  import type {
@@ -15,10 +22,18 @@ import type {
15
22
  RunFailure,
16
23
  RunServices,
17
24
  Scenario,
25
+ ScenarioEvent,
18
26
  Trace,
19
27
  TraceStep
20
28
  } from "../../../testing/MachineTest.js"
29
+ import type { ActiveConfiguration } from "../../machine/configuration.js"
30
+ import * as Planner from "../../machine/planner.js"
21
31
  import type { EnsureExecutable } from "../../machine/readiness.js"
32
+ import * as Step from "../../machine/step.js"
33
+ import * as Work from "./work.js"
34
+
35
+ // A scenario entry for a result of work is part of the trace model.
36
+ export { isWorkOutcome, WorkOutcomeTypeId } from "./work.js"
22
37
 
23
38
  type AnyMachine = Machine.Machine.Any
24
39
 
@@ -54,51 +69,246 @@ export const rawConfigurationPaths = <M extends AnyMachine>(
54
69
  .map((node) => node.path) as ReadonlyArray<StatePath<M>>
55
70
  }
56
71
 
72
+ /** A step whose result ends the machine with a failure instead of planning a state. */
73
+ export interface MachineFailure {
74
+ readonly _tag: "MachineFailure"
75
+ readonly cause: Cause.Cause<unknown>
76
+ }
77
+
78
+ /** One scenario entry planned after a trace, with the step state it reached. */
79
+ export interface Planned<M extends AnyMachine> {
80
+ readonly _tag: "Planned"
81
+ readonly plan: EventPlan<M>
82
+ readonly state: Step.State
83
+ }
84
+
85
+ const noConfiguration: ActiveConfiguration = {
86
+ active: new Set(),
87
+ values: new Map(),
88
+ outputs: new Map(),
89
+ history: new Map()
90
+ }
91
+
92
+ const steps = new WeakMap<AnyMachine, Step.MachineStep>()
93
+
94
+ const stepOf = (machine: AnyMachine): Step.MachineStep => {
95
+ let step = steps.get(machine)
96
+ if (step === undefined) {
97
+ // The machine refers to itself as it does in `Machine.plan`, so a command
98
+ // that targets it is the one that `Machine.plan` returns.
99
+ step = Step.make(machine, { self: Planner.resolveMachineReferences(machine, noConfiguration).self })
100
+ steps.set(machine, step)
101
+ }
102
+ return step
103
+ }
104
+
105
+ // The step state of every trace built here. A trace is a public value, so the
106
+ // state stays out of it, as the step's work stays out of `RunningWork`.
107
+ const states = new WeakMap<Trace<any>, Step.State>()
108
+
109
+ const stateOf = (trace: Trace<any>): Step.State => {
110
+ const state = states.get(trace)
111
+ if (state === undefined) throw new Error("MachineTest plans only after a trace that it built")
112
+ return state
113
+ }
114
+
115
+ const record = <M extends AnyMachine>(trace: Trace<M>, state: Step.State): Trace<M> => {
116
+ states.set(trace, state)
117
+ return trace
118
+ }
119
+
120
+ const noCommands: ReadonlyArray<never> = []
121
+
122
+ /** The public plan of a step, as `Machine.plan` returns it. */
123
+ const planOf = <M extends AnyMachine>(step: Step.Step, event: unknown): EventPlan<M> => {
124
+ const microsteps = step.microsteps
125
+ const plan = {
126
+ event: step.event ?? event,
127
+ next: step.snapshot,
128
+ // A transition plans its commands in its microstep, so the plan lists them in order.
129
+ commands: microsteps.length === 0
130
+ ? noCommands
131
+ : microsteps.length === 1
132
+ ? microsteps[0]!.commands
133
+ : microsteps.flatMap((microstep) => microstep.commands),
134
+ emittedEvents: step.emitted,
135
+ microsteps
136
+ }
137
+ const exit = step.exit
138
+ return (exit !== undefined && Exit.isSuccess(exit)
139
+ ? { ...plan, done: true, output: exit.value }
140
+ : { ...plan, done: false, output: undefined }) as EventPlan<M>
141
+ }
142
+
143
+ /**
144
+ * The step as a planned entry. A step that ends the machine with a failure
145
+ * failed to plan its event: a typed planning error, or a defect.
146
+ */
147
+ const fromStep = <M extends AnyMachine>(
148
+ step: Step.Step,
149
+ event: unknown
150
+ ): Exit.Exit<Planned<M>, unknown> => {
151
+ const exit = step.exit
152
+ return exit !== undefined && Exit.isFailure(exit)
153
+ ? Exit.failCause(exit.cause)
154
+ : Exit.succeed({ _tag: "Planned", plan: planOf<M>(step, event), state: step.state })
155
+ }
156
+
157
+ /**
158
+ * Plans one scenario entry after `trace` through its step. A result of work
159
+ * that fails the machine is a `MachineFailure`, because the machine plans no
160
+ * event for it.
161
+ */
162
+ export const planEntry = <M extends AnyMachine>(
163
+ machine: ReadyMachine<M>,
164
+ trace: Trace<M>,
165
+ entry: ScenarioEvent<M>
166
+ ): Effect.Effect<Planned<M> | MachineFailure, RunError<M>, RunServices<M>> => {
167
+ const step = stepOf(machine)
168
+ const state = stateOf(trace)
169
+ if (state.exit !== undefined) {
170
+ // A completed machine takes no further step, and `MachineStep.send` rejects
171
+ // events after completion with `StoppedError`. `MachineTest` preserves
172
+ // `Machine.plan`'s idempotent final-state contract (validating schemas with
173
+ // `RunServices<M>` and returning `{ done: true, output, next: trace.final }`),
174
+ // so post-completion entries delegate to `Machine.plan`.
175
+ return Effect.map(
176
+ (Machine.plan as any)(
177
+ machine,
178
+ trace.final,
179
+ Work.isWorkOutcome(entry) ? Work.stale(entry) : entry
180
+ ) as Effect.Effect<EventPlan<M>, RunError<M>, RunServices<M>>,
181
+ (plan): Planned<M> => ({ _tag: "Planned", plan, state })
182
+ )
183
+ }
184
+ if (!Work.isWorkOutcome(entry)) {
185
+ const sent = step.send(state, entry)
186
+ return (Exit.isFailure(sent) ? sent : fromStep<M>(sent.value, undefined)) as Exit.Exit<Planned<M>, RunError<M>>
187
+ }
188
+ const work = Work.find(trace.work, entry)
189
+ if (work === undefined) {
190
+ // Work that no longer runs reports a late result, which changes nothing.
191
+ return Exit.succeed({
192
+ _tag: "Planned",
193
+ plan: {
194
+ event: Work.staleEvent(entry),
195
+ next: trace.final,
196
+ commands: noCommands,
197
+ emittedEvents: noCommands,
198
+ microsteps: noCommands,
199
+ done: false,
200
+ output: undefined
201
+ },
202
+ state
203
+ })
204
+ }
205
+ if (!Work.accepts(work, entry)) {
206
+ return Exit.die(
207
+ new Error(
208
+ `MachineTest expected a ${entry.kind} outcome to name a running ${
209
+ entry.kind === "Elapsed" ? "timer" : "Effect"
210
+ }, but "${entry.path}" runs "${entry.id}" as ${work.kind}`
211
+ )
212
+ )
213
+ }
214
+ const settled = step.settle(state, Work.workOf(work), Work.exitOf(entry))
215
+ if (settled.event === undefined && settled.exit !== undefined && Exit.isFailure(settled.exit)) {
216
+ return Exit.succeed({ _tag: "MachineFailure", cause: settled.exit.cause })
217
+ }
218
+ return fromStep<M>(settled, Work.staleEvent(entry))
219
+ }
220
+
221
+ const failRun = <M extends AnyMachine>(
222
+ trace: Trace<M>,
223
+ event: ScenarioEvent<M>,
224
+ scenario: Scenario<M>,
225
+ cause: RunError<M>
226
+ ): RunFailure<RunError<M>, M> => ({
227
+ _tag: "MachineTestRunFailure",
228
+ scenario,
229
+ phase: "event",
230
+ eventIndex: trace.steps.length,
231
+ event,
232
+ initial: trace.initial,
233
+ steps: trace.steps.slice(),
234
+ cause
235
+ })
236
+
237
+ const extend = <M extends AnyMachine>(
238
+ machine: ReadyMachine<M>,
239
+ trace: Trace<M>,
240
+ event: ScenarioEvent<M>,
241
+ scenario: Scenario<M>,
242
+ planned: Planned<M>
243
+ ): Trace<M> => {
244
+ const { plan, state } = planned
245
+ const afterConfiguration = rawConfigurationPaths(machine, plan.next)
246
+ const step: TraceStep<M> = {
247
+ index: trace.steps.length,
248
+ before: trace.final,
249
+ beforeConfiguration: trace.finalConfiguration,
250
+ // A result of work keeps the outcome that reported it, so the scenario
251
+ // rebuilt from the steps reproduces the trace.
252
+ event: Work.isWorkOutcome(event) ? event : plan.event,
253
+ plan,
254
+ after: plan.next,
255
+ afterConfiguration
256
+ }
257
+ return record({
258
+ scenario: { ...scenario, events: [...trace.steps.map((step) => step.event), step.event] },
259
+ initial: trace.initial,
260
+ steps: [...trace.steps, step],
261
+ final: plan.next,
262
+ finalConfiguration: afterConfiguration,
263
+ work: state === stateOf(trace) ? trace.work : Work.running(state)
264
+ }, state)
265
+ }
266
+
267
+ /**
268
+ * Plans one scenario entry after `trace`. A result of work that fails the
269
+ * machine is returned as a `MachineFailure`, because a trace has no state for it.
270
+ */
271
+ export const stepTrace = <M extends AnyMachine>(
272
+ machine: ReadyMachine<M>,
273
+ trace: Trace<M>,
274
+ event: ScenarioEvent<M>,
275
+ scenario: Scenario<M>
276
+ ): Effect.Effect<Trace<M> | MachineFailure, RunFailure<RunError<M>, M>, RunServices<M>> =>
277
+ Effect.suspend(() => planEntry(machine, trace, event)).pipe(
278
+ Effect.mapError((cause) => failRun(trace, event, scenario, cause)),
279
+ Effect.map((planned) => planned._tag === "Planned" ? extend(machine, trace, event, scenario, planned) : planned)
280
+ )
281
+
57
282
  export const appendTrace = <M extends AnyMachine>(
58
283
  machine: ReadyMachine<M>,
59
284
  trace: Trace<M>,
60
- event: Machine.Machine.EventInput<Machine.Machine.InputEvent<M>>,
285
+ event: ScenarioEvent<M>,
61
286
  scenario: Scenario<M>
62
- ): Effect.Effect<Trace<M>, RunFailure<RunError<M>, M>, RunServices<M>> => {
63
- const index = trace.steps.length
64
- const before = trace.final
65
- const beforeConfiguration = rawConfigurationPaths(machine, before)
66
- return ((Machine.plan as any)(machine, before, event) as Effect.Effect<
67
- EventPlan<M>,
68
- RunError<M>,
69
- RunServices<M>
70
- >).pipe(
71
- Effect.mapError((cause): RunFailure<RunError<M>, M> => ({
72
- _tag: "MachineTestRunFailure",
73
- scenario,
74
- phase: "event",
75
- eventIndex: index,
76
- event,
77
- initial: trace.initial,
78
- steps: trace.steps.slice(),
79
- cause
80
- })),
81
- Effect.map((plan) => {
82
- const step: TraceStep<M> = {
83
- index,
84
- before,
85
- beforeConfiguration,
86
- event: plan.event,
87
- plan,
88
- after: plan.next,
89
- afterConfiguration: rawConfigurationPaths(machine, plan.next)
90
- }
91
- return {
92
- scenario: { ...scenario, events: [...trace.steps.map((step) => step.event), plan.event] },
93
- initial: trace.initial,
94
- steps: [...trace.steps, step],
95
- final: plan.next,
96
- finalConfiguration: step.afterConfiguration
97
- }
98
- })
287
+ ): Effect.Effect<Trace<M>, RunFailure<RunError<M>, M>, RunServices<M>> =>
288
+ Effect.flatMap(
289
+ stepTrace(machine, trace, event, scenario),
290
+ (next) => isMachineFailure(next) ? failMachine(trace, event, scenario, next) : Effect.succeed(next)
99
291
  )
292
+
293
+ /**
294
+ * The failure of planning a scenario whose entry failed the machine. A typed
295
+ * failure of the work is an error of the machine, and anything else a defect.
296
+ */
297
+ export const failMachine = <M extends AnyMachine>(
298
+ trace: Trace<M>,
299
+ event: ScenarioEvent<M>,
300
+ scenario: Scenario<M>,
301
+ failed: MachineFailure
302
+ ): Effect.Effect<never, RunFailure<RunError<M>, M>> => {
303
+ const failure = failed.cause.reasons.find(Cause.isFailReason)
304
+ return failure === undefined
305
+ ? Effect.failCause(failed.cause as Cause.Cause<never>)
306
+ : Effect.fail(failRun(trace, event, scenario, failure.error as RunError<M>))
100
307
  }
101
308
 
309
+ export const isMachineFailure = (value: unknown): value is MachineFailure =>
310
+ typeof value === "object" && value !== null && (value as { readonly _tag?: unknown })._tag === "MachineFailure"
311
+
102
312
  export const run: <M extends AnyMachine>(
103
313
  machine: ReadyMachine<M>,
104
314
  scenario: Scenario<M>
@@ -108,6 +318,12 @@ export const run: <M extends AnyMachine>(
108
318
  machine: ReadyMachine<M>,
109
319
  scenario: Scenario<M>
110
320
  ) {
321
+ // Startup uses `Machine.planInitial` instead of `step.start` because
322
+ // `InitialTrace` exposes `startingState` (the pre-settling snapshot, which
323
+ // `MachineStep.Step` does not retain), `Machine.planInitial` supports
324
+ // effectful schema services (`RunServices<M>`) whereas `step.start` is
325
+ // synchronous, and `MachineTest.run` reports raw `RunError<M>` rather than
326
+ // `step.start`'s `StartupError` wrapper.
111
327
  const initialEffect = (machine.input === undefined || machine.input === Schema.Void
112
328
  ? (Machine.planInitial as any)(machine)
113
329
  : (Machine.planInitial as any)(machine, (scenario as { readonly input: InputValue<M> }).input)) as Effect.Effect<
@@ -134,13 +350,17 @@ export const run: <M extends AnyMachine>(
134
350
  initialEntryPaths: initial.initialEntryPaths,
135
351
  configuration: rawConfigurationPaths(machine, initial.state)
136
352
  }
137
- let trace: Trace<M> = {
353
+ // The step resumes from the planned startup state: it lists the work of the
354
+ // active states as `start` does, without planning startup again.
355
+ const resumed = yield* Effect.orDie(stepOf(machine).resume(initial.state))
356
+ let trace: Trace<M> = record({
138
357
  scenario: { ...scenario, events: [] },
139
358
  initial: initialTrace,
140
359
  steps: [],
141
360
  final: initial.state,
142
- finalConfiguration: initialTrace.configuration
143
- }
361
+ finalConfiguration: initialTrace.configuration,
362
+ work: Work.running(resumed.state)
363
+ }, resumed.state)
144
364
 
145
365
  for (const event of scenario.events) {
146
366
  trace = yield* appendTrace(machine, trace, event, scenario)
@@ -52,7 +52,7 @@ export const sameTransitionTrigger = (
52
52
  }
53
53
 
54
54
  export interface TransitionCoverageCollector<M extends AnyMachine> {
55
- readonly observeMicrostep: (microstep: Microstep<M, unknown>) => void
55
+ readonly observeMicrostep: (microstep: Microstep<M>) => void
56
56
  readonly summary: () => MachineTransitionCoverage<M>
57
57
  }
58
58
 
@@ -38,6 +38,7 @@ import * as ReferenceModel from "./referenceModel.js"
38
38
  import { rawConfigurationPaths } from "./trace.js"
39
39
  import { makeTransitionCoverageCollector, sameTransitionTrigger } from "./transitionCoverage.js"
40
40
  import { sameValue } from "./value.js"
41
+ import { isWorkOutcome } from "./work.js"
41
42
 
42
43
  type AnyMachine = Machine.Machine.Any
43
44
 
@@ -427,6 +428,7 @@ export const coverage = <M extends AnyMachine>(
427
428
  let alwaysTriggered = 0
428
429
  let doneTriggered = 0
429
430
  let choiceTriggered = 0
431
+ let invokeTriggered = 0
430
432
  let donePlans = 0
431
433
  let completionRecordObservations = 0
432
434
  const completionPaths = new Set<string>()
@@ -464,7 +466,7 @@ export const coverage = <M extends AnyMachine>(
464
466
  }
465
467
  }
466
468
 
467
- const observeMicrostep = (microstep: Microstep<M, any>): void => {
469
+ const observeMicrostep = (microstep: Microstep<M>): void => {
468
470
  transitionCoverage.observeMicrostep(microstep)
469
471
  microsteps += 1
470
472
  if (microstep.changed) changedMicrosteps += 1
@@ -479,6 +481,7 @@ export const coverage = <M extends AnyMachine>(
479
481
  if (retained.trigger.type === "event") eventTriggered += 1
480
482
  else if (retained.trigger.type === "always") alwaysTriggered += 1
481
483
  else if (retained.trigger.type === "done") doneTriggered += 1
484
+ else if (retained.trigger.type === "invoke") invokeTriggered += 1
482
485
  else choiceTriggered += 1
483
486
  if (retained.target !== undefined && nodeByPath.get(retained.target)?.type === "history") {
484
487
  historyTargets += 1
@@ -491,6 +494,8 @@ export const coverage = <M extends AnyMachine>(
491
494
  scenarioEvents += trace.scenario.events.length
492
495
  if (trace.scenario.events.length === 0) emptyScenarios += 1
493
496
  for (const step of trace.steps) {
497
+ // Event coverage counts public events. A result of work is not one.
498
+ if (isWorkOutcome(step.event)) continue
494
499
  const tag = step.event._tag
495
500
  eventCounts.set(tag, (eventCounts.get(tag) ?? 0) + 1)
496
501
  }
@@ -569,7 +574,8 @@ export const coverage = <M extends AnyMachine>(
569
574
  eventTriggered,
570
575
  alwaysTriggered,
571
576
  doneTriggered,
572
- choiceTriggered
577
+ choiceTriggered,
578
+ invokeTriggered
573
579
  },
574
580
  completion: {
575
581
  donePlans,
@@ -597,7 +603,7 @@ interface SnapshotOccurrence<M extends AnyMachine> {
597
603
 
598
604
  interface GraphMicrostepDraft<M extends AnyMachine> {
599
605
  readonly nextOccurrence: number
600
- readonly microstep: Microstep<M, any>
606
+ readonly microstep: Microstep<M>
601
607
  }
602
608
 
603
609
  type GraphEdgeDraftData<M extends AnyMachine> = ObservedGraphEdge<M> extends infer Edge
@@ -637,7 +643,7 @@ export const observedGraph: <M extends AnyMachine>(
637
643
  occurrences.push({ snapshot, role })
638
644
  return index
639
645
  }
640
- const addMicrosteps = (microsteps: ReadonlyArray<Microstep<M, any>>): ReadonlyArray<GraphMicrostepDraft<M>> =>
646
+ const addMicrosteps = (microsteps: ReadonlyArray<Microstep<M>>): ReadonlyArray<GraphMicrostepDraft<M>> =>
641
647
  microsteps.map((microstep) => ({
642
648
  nextOccurrence: addOccurrence(microstep.next, "microstep"),
643
649
  microstep
@@ -1544,7 +1550,7 @@ export const verify = <M extends AnyMachine>(
1544
1550
  }
1545
1551
 
1546
1552
  const validateMicrostep = (
1547
- microstep: Microstep<M, any>,
1553
+ microstep: Microstep<M>,
1548
1554
  before: SnapshotInspection,
1549
1555
  after: SnapshotInspection,
1550
1556
  location: VerificationLocation
@@ -0,0 +1,101 @@
1
+ /**
2
+ * The public view of the work that a planner trace runs, and of the results
3
+ * that a scenario reports for it.
4
+ *
5
+ * Which work runs is decided by `Machine.toStep` alone: a trace reads it from
6
+ * the state of its step. This module only maps the step's work to
7
+ * `RunningWork`, and a `WorkOutcome` to the `Exit` that the step settles. It
8
+ * never evaluates a source or an input mapper.
9
+ *
10
+ * @since 0.48.0
11
+ */
12
+
13
+ import * as Exit from "effect/Exit"
14
+ import type { InvocationEvent as LifecycleEvent } from "../../../Machine.js"
15
+ import type { RunningWork, WorkOutcome } from "../../../testing/MachineTest.js"
16
+ import * as InvocationEvent from "../../machine/invocationEvent.js"
17
+ import type * as InvocationResolution from "../../machine/invocationResolution.js"
18
+ import * as Step from "../../machine/step.js"
19
+
20
+ /** @internal */
21
+ export const WorkOutcomeTypeId: unique symbol = Symbol.for("effect/MachineTest/WorkOutcome")
22
+
23
+ /** @internal */
24
+ export const isWorkOutcome = (value: unknown): value is WorkOutcome =>
25
+ typeof value === "object" && value !== null && (value as { readonly _tag?: unknown })._tag === WorkOutcomeTypeId
26
+
27
+ // The step's work stays out of the public value, so formatting, JSON, and
28
+ // structural comparison of a trace see only the public fields. Each piece of
29
+ // work keeps one public value for as long as it runs.
30
+ const views = new WeakMap<Step.Work, RunningWork>()
31
+ const works = new WeakMap<RunningWork, Step.Work>()
32
+
33
+ const viewOf = (work: Step.Work): RunningWork => {
34
+ let view = views.get(work)
35
+ if (view === undefined) {
36
+ view = Object.freeze({ path: work.path, id: work.id, source: work.source, kind: work._tag })
37
+ views.set(work, view)
38
+ works.set(view, work)
39
+ }
40
+ return view
41
+ }
42
+
43
+ const noWork: ReadonlyArray<never> = []
44
+
45
+ /** The public view of the work that a step state runs, in start order. */
46
+ export const running = (state: Step.State): ReadonlyArray<RunningWork> =>
47
+ state.running.length === 0 ? noWork : state.running.map(viewOf)
48
+
49
+ /** The step's work behind a public view. */
50
+ export const workOf = (work: RunningWork): Step.Work => works.get(work)!
51
+
52
+ /** The running work that `outcome` reports for, if it still runs. */
53
+ export const find = (running: ReadonlyArray<RunningWork>, outcome: WorkOutcome): RunningWork | undefined =>
54
+ running.find((work) => work.path === outcome.path && work.id === outcome.id)
55
+
56
+ /** The outcome channels that `work` declares a transition for. */
57
+ export const channels = (work: RunningWork): InvocationResolution.OutcomeChannels =>
58
+ workOf(work)[Step.WorkTypeId].channels
59
+
60
+ /** Whether a result of this kind can come from `work`. */
61
+ export const accepts = (work: RunningWork, outcome: WorkOutcome): boolean =>
62
+ outcome.kind === "Elapsed" ? work.kind === "Timer" : work.kind === "Effect"
63
+
64
+ /** How the work ended, as its host reports it to the step. */
65
+ export const exitOf = (outcome: WorkOutcome): Exit.Exit<unknown, unknown> => {
66
+ switch (outcome.kind) {
67
+ case "Done":
68
+ return Exit.succeed(outcome.output)
69
+ case "Elapsed":
70
+ // A timer completes with `undefined` once its duration has elapsed.
71
+ return Exit.void
72
+ case "Failure":
73
+ return Exit.fail(outcome.error)
74
+ }
75
+ }
76
+
77
+ /**
78
+ * The message of a result whose work no longer runs. The planner selects no
79
+ * transition for it, as every host ignores a late result.
80
+ */
81
+ export const stale = (outcome: WorkOutcome): InvocationEvent.InvocationEvent => {
82
+ const generation: InvocationEvent.Generation = { current: false }
83
+ return outcome.kind === "Failure"
84
+ ? InvocationEvent.failure(outcome.path, outcome.id, generation, outcome.error)
85
+ : InvocationEvent.done(outcome.path, outcome.id, generation, outcome.kind === "Done" ? outcome.output : undefined)
86
+ }
87
+
88
+ /** The event that transitions would receive for a late result. */
89
+ export const staleEvent = (outcome: WorkOutcome): LifecycleEvent => InvocationEvent.toLifecycleEvent(stale(outcome))
90
+
91
+ /** @internal */
92
+ export const done = (work: RunningWork, output: unknown): WorkOutcome =>
93
+ Object.freeze({ _tag: WorkOutcomeTypeId, kind: "Done", path: work.path, id: work.id, output })
94
+
95
+ /** @internal */
96
+ export const failure = (work: RunningWork, error: unknown): WorkOutcome =>
97
+ Object.freeze({ _tag: WorkOutcomeTypeId, kind: "Failure", path: work.path, id: work.id, error })
98
+
99
+ /** @internal */
100
+ export const elapsed = (work: RunningWork): WorkOutcome =>
101
+ Object.freeze({ _tag: WorkOutcomeTypeId, kind: "Elapsed", path: work.path, id: work.id })