@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
@@ -0,0 +1,634 @@
1
+ /**
2
+ * Runs a machine on its owned step, as a compiled process of the local runtime.
3
+ *
4
+ * The step decides everything a machine does: its next state, its commands and
5
+ * emissions, the work it starts and stops (before and after commit), and what
6
+ * the result of that work means. This module only carries a step out, in the
7
+ * order of the step: `commands`, `stop`, the new state, `emitted`, then `start`
8
+ * and `stopAfter`. Work runs as an owned process of the machine, so it starts
9
+ * in the turn of the event that entered its state, and its result reaches the
10
+ * step through the mailbox of the machine, in a later turn.
11
+ *
12
+ * @since 0.48.0
13
+ */
14
+
15
+ import * as Cause from "effect/Cause"
16
+ import * as Effect from "effect/Effect"
17
+ import * as Exit from "effect/Exit"
18
+ import * as Option from "effect/Option"
19
+ import * as Stream from "effect/Stream"
20
+ import type { Inspection } from "../../LocalMachine.js"
21
+ import type { ChildMachine, Machine } from "../../Machine.js"
22
+ import * as ChildRegistry from "./childRegistry.js"
23
+ import * as CommandRuntime from "./commandRuntime.js"
24
+ import { ChildAlreadyExistsError, failPlanning } from "./errors.js"
25
+ import type * as ExecutionPlan from "./executionPlan.js"
26
+ import type * as InvocationResolution from "./invocationResolution.js"
27
+ import * as Runtime from "./runtimeProtocol.js"
28
+ import * as Step from "./step.js"
29
+
30
+ type OwnedStep = Step.Step<"Owned">
31
+ type Kernel = Step.Kernel<"Owned">
32
+ type Context = Runtime.CompiledProcessContext<Machine.Snapshot<any>, unknown>
33
+ type Scope = Runtime.ProcessInitialContext<unknown>
34
+ type AnyProcessLogic = Runtime.ProcessLogic<any, any, any, any, any, any>
35
+ type Drain = Effect.Effect<Option.Option<unknown>, unknown, unknown>
36
+
37
+ /** How a process of the machine begins: started with input, or resumed from a snapshot. */
38
+ export type Entry =
39
+ | { readonly _tag: "Initial"; readonly args: ReadonlyArray<unknown> }
40
+ | { readonly _tag: "Resume"; readonly snapshot: Machine.Snapshot<any> }
41
+
42
+ /**
43
+ * The host of a step in a process, which is also the execution state of the
44
+ * process: the latest state of its step, and the step whose work is still to
45
+ * start, which only its first step has. Work runs as a process of the
46
+ * machine, which finds the services of the machine in its own: every process
47
+ * of the runtime provides them. The children of the machine are the children
48
+ * its process owns.
49
+ */
50
+ class Host extends Step.PlanningBase implements Step.Children {
51
+ readonly self: Scope["self"]
52
+ state!: Step.State<"Owned">
53
+ pending: OwnedStep | undefined = undefined
54
+ liveRuntime: CommandRuntime.LiveRuntime | undefined = undefined
55
+
56
+ constructor(machine: Machine.Any, kernel: Kernel, private readonly scope: Scope) {
57
+ super(machine, kernel.plan)
58
+ this.self = scope.self
59
+ }
60
+
61
+ get parent() {
62
+ return this.scope.parent
63
+ }
64
+ get children(): Step.Children {
65
+ return this
66
+ }
67
+ get ambient(): boolean {
68
+ return true
69
+ }
70
+ get services(): never {
71
+ throw new Error("Local host runs work inside ambient machine services")
72
+ }
73
+
74
+ spawn(child: ChildMachine.Any, options: { readonly input: unknown } | undefined) {
75
+ const spawn = this.scope.spawn as (child: unknown, ...options: ReadonlyArray<unknown>) => Effect.Effect<unknown>
76
+ return options === undefined ? spawn(child) : spawn(child, options)
77
+ }
78
+ spawnLogic(
79
+ logic: Parameters<Step.Children["spawnLogic"]>[0],
80
+ options: { readonly id?: string | undefined } | undefined
81
+ ) {
82
+ const spawn = this.scope.spawn as (logic: unknown, options?: unknown) => Effect.Effect<unknown>
83
+ return options === undefined ? spawn(logic) : spawn(logic, options)
84
+ }
85
+ sendTo(child: string | ChildMachine.Any, event: unknown) {
86
+ return this.scope.sendTo(child, event)
87
+ }
88
+ stop(child: string | ChildMachine.Any) {
89
+ return this.scope.stopChild(child)
90
+ }
91
+ }
92
+
93
+ /** Whether the process reports what it does, so its steps keep their microsteps. */
94
+ const isInspected = (scope: Scope): boolean => scope.self.inspectionSubject !== undefined
95
+
96
+ const runSequentialDiscard = (effects: ReadonlyArray<Effect.Effect<void, any, any>>): Effect.Effect<void, any, any> =>
97
+ effects.length === 1 ? effects[0]! : Effect.all(effects, { discard: true })
98
+
99
+ /** Carries out the commands of a step, in order. */
100
+ const runCommands = (commands: ReadonlyArray<Step.Command>, scope: Scope): Effect.Effect<void, any, any> =>
101
+ Effect.forEach(commands, (command) => {
102
+ switch (command._tag) {
103
+ case "Send":
104
+ return Runtime.deliverFromScope(scope, command.target, command.event)
105
+ case "SendToChild":
106
+ return Runtime.deliverFromScope(scope, command.child, command.event)
107
+ case "StopChild":
108
+ return scope.stopChild(command.child)
109
+ }
110
+ }, { discard: true })
111
+
112
+ /** @internal */
113
+ export const makeKey = (path: string, id: string): string => `${path.length}:${path}${id}`
114
+
115
+ /** Stops running work, all at once, as the local runtime stops the children of exited states. */
116
+ const stopWork = (context: Context, work: ReadonlyArray<Step.Work>): Effect.Effect<void> | undefined =>
117
+ work.length === 0
118
+ ? undefined
119
+ : work.length === 1
120
+ ? context.ownedChildren.stopKey(makeKey(work[0]!.path, work[0]!.id))
121
+ : Effect.all(work.map((one) => context.ownedChildren.stopKey(makeKey(one.path, one.id))), {
122
+ concurrency: "unbounded",
123
+ discard: true
124
+ })
125
+
126
+ // Invocations the runtime hosts as an inspected and traced program.
127
+ type ProgramInvocation =
128
+ | InvocationResolution.EffectInvocation
129
+ | InvocationResolution.TimerInvocation
130
+ | InvocationResolution.StreamInvocation
131
+
132
+ const isProgram = (invocation: InvocationResolution.ResolvedInvocation): invocation is ProgramInvocation =>
133
+ invocation._tag === "Effect" || invocation._tag === "Timer" || invocation._tag === "Stream"
134
+
135
+ // Logic addresses and child descriptor ids are caller-chosen; every other id is
136
+ // owned by the runtime.
137
+ const childId = (invocation: InvocationResolution.ResolvedInvocation): string => {
138
+ switch (invocation._tag) {
139
+ case "Effect":
140
+ case "Timer":
141
+ case "Stream":
142
+ return `${ChildRegistry.invocationChildIdPrefix}${makeKey(invocation.path, invocation.id)}`
143
+ case "Logic":
144
+ return invocation.address
145
+ case "Child":
146
+ return invocation.child.id
147
+ }
148
+ }
149
+
150
+ /**
151
+ * The value of an accessor of work. Its failure is thrown inside the startup
152
+ * boundary of the process that would run the work, which fails the machine.
153
+ */
154
+ const valueOf = <A>(exit: Exit.Exit<A>): A => {
155
+ if (Exit.isSuccess(exit)) return exit.value
156
+ throw Cause.squash(exit.cause)
157
+ }
158
+
159
+ /** The exit of work as its step settles it. */
160
+ const exitOf = (
161
+ outcome: Exclude<Runtime.RuntimeOutcome<any, any, any>, { readonly _tag: "Stopped" }>
162
+ ): Exit.Exit<unknown, unknown> => outcome._tag === "Done" ? Exit.succeed(outcome.output) : Exit.failCause(outcome.cause)
163
+
164
+ const traced = (scope: Scope, invocation: ProgramInvocation, logic: AnyProcessLogic): AnyProcessLogic => ({
165
+ ...logic,
166
+ run: (context: Runtime.ProcessRunContext<any, any>) =>
167
+ Effect.withSpan(
168
+ Effect.suspend(() => logic.run(context)),
169
+ "Machine.invoke",
170
+ {
171
+ attributes: {
172
+ "machine.id": scope.self.id,
173
+ "machine.sessionId": scope.self.sessionId,
174
+ "machine.state.path": invocation.path,
175
+ "machine.invoke.id": invocation.id,
176
+ "machine.invoke.source": invocation.sourceName ?? invocation.id,
177
+ "machine.invoke.kind": invocation._tag,
178
+ "machine.invoke.sessionId": context.self.sessionId
179
+ },
180
+ captureStackTrace: false
181
+ }
182
+ )
183
+ })
184
+
185
+ /** The process that runs a program, built when its process starts. */
186
+ const programLogic = (
187
+ scope: Scope,
188
+ kernel: Kernel,
189
+ work: Step.Work,
190
+ invocation: ProgramInvocation
191
+ ): AnyProcessLogic => {
192
+ switch (work._tag) {
193
+ case "Effect": {
194
+ const effect = valueOf(work.effect())
195
+ return traced(scope, invocation, { initial: () => Effect.void, run: () => effect })
196
+ }
197
+ case "Timer": {
198
+ const duration = valueOf(work.duration())
199
+ return traced(scope, invocation, { initial: () => Effect.void, run: () => Effect.sleep(duration) })
200
+ }
201
+ case "Stream": {
202
+ const stream = valueOf(work.stream())
203
+ const inspected = isInspected(scope)
204
+ return traced(scope, invocation, {
205
+ initial: () => Effect.void,
206
+ run: ({ parent }: Runtime.ProcessRunContext<any, any>) => {
207
+ const send = parent?.[Runtime.acknowledgedSend]
208
+ if (send === undefined) {
209
+ return Effect.die(new Error("Stream invocation requires an acknowledged parent machine"))
210
+ }
211
+ // Each element waits for the machine to take it, which paces the stream.
212
+ return Effect.scoped(Stream.runForEach(
213
+ stream,
214
+ (element) =>
215
+ send(
216
+ new Runtime.WorkMessage(
217
+ "Element",
218
+ work,
219
+ element,
220
+ inspected ? kernel.elementEvent(work, element) : undefined
221
+ )
222
+ ).pipe(
223
+ Effect.asVoid,
224
+ Effect.catchCause(() => Effect.interrupt)
225
+ )
226
+ ))
227
+ }
228
+ })
229
+ }
230
+ default:
231
+ throw new Error(`Work of kind ${work._tag} is not a program`)
232
+ }
233
+ }
234
+
235
+ /** Reports the result of work to its machine, which plans it in a later turn. */
236
+ const report = (
237
+ scope: Scope,
238
+ message: Runtime.WorkMessage,
239
+ invocationSessionId: string | undefined
240
+ ): Effect.Effect<void> =>
241
+ Runtime.deliverToEndpoint(
242
+ scope.self,
243
+ message,
244
+ invocationSessionId === undefined ? undefined : scope.self.inspectionSubject,
245
+ invocationSessionId === undefined
246
+ ? undefined
247
+ : ({ _tag: "ProgramInvocation", invocationSessionId } satisfies Inspection.Causation)
248
+ ).pipe(Effect.catchTag("StoppedError", () => Effect.void))
249
+
250
+ /** Starts one piece of work that a step listed, as an owned process of the machine. */
251
+ const startWork = (context: Context, kernel: Kernel, work: Step.Work): Effect.Effect<void, any, any> => {
252
+ const scope = context.scope
253
+ const invocation = work[Step.WorkTypeId]
254
+ const program = isProgram(invocation)
255
+ const id = childId(invocation)
256
+ // Keep caller-chosen ids out of the namespace reserved for runtime-owned
257
+ // invocation ids; runtime-owned ids skip this check.
258
+ if (!program && ChildRegistry.isReservedChildId(id)) {
259
+ return Effect.fail(new ChildAlreadyExistsError({ id: invocation.id }))
260
+ }
261
+ return context.ownedChildren.spawn(
262
+ program
263
+ ? () => programLogic(scope, kernel, work, invocation)
264
+ : work._tag === "Logic"
265
+ ? () => valueOf(work.logic()) as AnyProcessLogic
266
+ // The runtime builds the child's logic from this description, so a
267
+ // running parent retains no closure for it.
268
+ : invocation as Extract<InvocationResolution.ResolvedInvocation, { readonly _tag: "Child" }>,
269
+ {
270
+ key: makeKey(work.path, work.id),
271
+ path: work.path,
272
+ id,
273
+ duplicateId: work.id,
274
+ ...(program ? { programKind: invocation._tag } : undefined),
275
+ // The work starts while the event that entered its state is processed,
276
+ // and its outcome is handled in a later turn, even when the work
277
+ // completes without suspending. The entered state is therefore always
278
+ // observed before the outcome leaves it, and the outcome of a state that
279
+ // was exited in between is discarded. The step decides what the exit
280
+ // means when the mailbox delivers it.
281
+ onOutcome: (isCurrent, outcome, invocationSessionId) =>
282
+ Effect.andThen(
283
+ Effect.yieldNow,
284
+ Effect.suspend(() => {
285
+ if (!isCurrent() || outcome._tag === "Stopped") return Effect.void
286
+ const exit = exitOf(outcome)
287
+ return report(
288
+ scope,
289
+ new Runtime.WorkMessage(
290
+ "Settle",
291
+ work,
292
+ exit,
293
+ isInspected(scope) ? kernel.settleEvent(work, exit) : undefined
294
+ ),
295
+ program ? invocationSessionId : undefined
296
+ )
297
+ })
298
+ ),
299
+ ...(kernel.observesSnapshots(work)
300
+ ? {
301
+ onSnapshot: (isCurrent: () => boolean, snapshot: { readonly state: unknown }) => {
302
+ if (!isCurrent()) return Effect.void
303
+ return report(
304
+ scope,
305
+ new Runtime.WorkMessage(
306
+ "Report",
307
+ work,
308
+ snapshot.state,
309
+ isInspected(scope) ? kernel.snapshotEvent(work, snapshot.state) : undefined
310
+ ),
311
+ undefined
312
+ )
313
+ }
314
+ }
315
+ : undefined)
316
+ }
317
+ )
318
+ }
319
+
320
+ const startAll = (
321
+ context: Context,
322
+ kernel: Kernel,
323
+ work: ReadonlyArray<Step.Work>
324
+ ): Effect.Effect<void, any, any> | undefined =>
325
+ work.length === 0
326
+ ? undefined
327
+ : work.length === 1
328
+ ? startWork(context, kernel, work[0]!)
329
+ : Effect.all(work.map((one) => startWork(context, kernel, one)), { discard: true })
330
+
331
+ /** The plan of a step, as an acknowledged delivery reports it. */
332
+ const planOf = (step: OwnedStep): unknown => {
333
+ const microsteps = step.microsteps
334
+ const exit = step.exit
335
+ return {
336
+ event: step.event,
337
+ next: step.snapshot,
338
+ commands: microsteps.length === 0 ? microsteps : microsteps.flatMap((microstep) => microstep.commands),
339
+ emittedEvents: step.emitted,
340
+ microsteps,
341
+ done: exit !== undefined && Exit.isSuccess(exit),
342
+ output: exit !== undefined && Exit.isSuccess(exit) ? exit.value : undefined
343
+ }
344
+ }
345
+
346
+ const noQueuedMessages: Drain = Effect.succeed(Option.none())
347
+
348
+ /**
349
+ * The drain of a machine on `kernel`. It plans each message with the step,
350
+ * commits steps that need no effectful work in the caller, and returns the
351
+ * drain continuing from the first step that does.
352
+ */
353
+ const makeDrain = (machine: Machine.Any, kernel: Kernel, childless: boolean) => {
354
+ // A machine that invokes no work keeps only its snapshot until it takes a
355
+ // message, and its state from then on.
356
+ const hostOf = (context: Context): Host => {
357
+ let host = context.executionState as Host | undefined
358
+ if (host === undefined) {
359
+ host = new Host(machine, kernel, context.scope)
360
+ host.state = kernel.fromSnapshot(host, context.state())
361
+ context.executionState = host
362
+ }
363
+ return host
364
+ }
365
+
366
+ const takeWork = (state: Step.State<"Owned">, message: Runtime.WorkMessage, retain: boolean): OwnedStep => {
367
+ const work = message.work as Step.Work
368
+ switch (message._tag) {
369
+ case "Settle":
370
+ return kernel.settle(state, work, message.value as Exit.Exit<unknown, unknown>, retain)
371
+ case "Element":
372
+ return kernel.element(state, work, message.value, retain)
373
+ case "Report":
374
+ return kernel.report(state, work, message.value, retain)
375
+ }
376
+ }
377
+
378
+ const loop = (context: Context): Drain => Effect.suspend(() => drainSync(context) ?? noQueuedMessages)
379
+
380
+ /** Starts the work of the first step of the process, then drains. */
381
+ const startPending = (context: Context, host: Host, step: OwnedStep): Drain => {
382
+ host.pending = undefined
383
+ const starting = startAll(context, kernel, step.start)
384
+ return starting === undefined ? loop(context) : starting.pipe(Effect.andThen(loop(context)))
385
+ }
386
+
387
+ /** Carries out a step that needs effectful work, then continues the drain. */
388
+ const continueEffectful = (
389
+ context: Context,
390
+ host: Host,
391
+ step: OwnedStep,
392
+ acknowledged: boolean,
393
+ before: Machine.Snapshot<any>,
394
+ settled: Step.Work | undefined
395
+ ): Drain => {
396
+ const scope = context.scope
397
+ const exit = step.exit
398
+ // Work that ended in a state the step exits is awaited with `step.stop`, so
399
+ // its process has stopped before the state is entered again. Remaining work
400
+ // of non-exited states on a final step is in `step.stopAfter`.
401
+ const stopBefore: ReadonlyArray<Step.Work> = settled === undefined ? step.stop : [settled, ...step.stop]
402
+ const beforeCommit: Array<Effect.Effect<void, any, any>> = []
403
+ if (step.commands.length > 0) beforeCommit.push(runCommands(step.commands, scope))
404
+ const stopping = stopWork(context, stopBefore)
405
+ if (stopping !== undefined) beforeCommit.push(stopping)
406
+ const afterCommit: Array<Effect.Effect<void, any, any>> = []
407
+ if (step.emitted.length > 0) {
408
+ afterCommit.push(
409
+ CommandRuntime.runEmittedEvents(
410
+ step.emitted,
411
+ host.liveRuntime ??= CommandRuntime.makeLiveRuntime(machine, scope)
412
+ )
413
+ )
414
+ }
415
+ if (step.start.length > 0) {
416
+ const starting = startAll(context, kernel, step.start)
417
+ if (starting !== undefined) afterCommit.push(starting)
418
+ }
419
+ if (step.stopAfter.length > 0) {
420
+ const stopped = stopWork(context, step.stopAfter)
421
+ if (stopped !== undefined) afterCommit.push(stopped)
422
+ }
423
+ const planned = step.planned
424
+ const after = planned ? step.snapshot : before
425
+ const continueAfterCommit = (): Drain => {
426
+ const continued = Effect.suspend((): Drain => {
427
+ if (acknowledged) context.completeMessage({ before, plan: planOf(step), after })
428
+ return exit !== undefined && Exit.isSuccess(exit) ? Effect.succeed(Option.some(exit.value)) : loop(context)
429
+ })
430
+ return afterCommit.length === 0 ? continued : runSequentialDiscard(afterCommit).pipe(Effect.andThen(continued))
431
+ }
432
+ const commitAndContinue = (): Drain => {
433
+ const notification = planned ? context.commit(after) : undefined
434
+ const continued = continueAfterCommit()
435
+ const effect = notification === undefined ? continued : notification.pipe(Effect.andThen(continued))
436
+ return notification === undefined && afterCommit.length === 0 ? effect : context.runAfterChanges(effect)
437
+ }
438
+ return beforeCommit.length === 0
439
+ ? commitAndContinue()
440
+ : context.runAfterChanges(
441
+ runSequentialDiscard(beforeCommit).pipe(Effect.andThen(Effect.suspend(commitAndContinue)))
442
+ )
443
+ }
444
+
445
+ // Commits queued messages whose steps need no effectful work. Returns the
446
+ // drain continuing from the first message that does, or `undefined` once the
447
+ // mailbox is empty.
448
+ const drainSync = (context: Context, first?: Runtime.ProcessMessage<unknown>): Drain | undefined => {
449
+ const host = hostOf(context)
450
+ if (!childless && host.pending !== undefined) {
451
+ if (first !== undefined) context.beginMessage(first)
452
+ return startPending(context, host, host.pending)
453
+ }
454
+ let delivered = first
455
+ while (true) {
456
+ let message: Runtime.ProcessMessage<unknown>
457
+ let acknowledged: boolean
458
+ if (delivered !== undefined) {
459
+ message = delivered
460
+ delivered = undefined
461
+ acknowledged = context.beginMessage(message)
462
+ } else {
463
+ const pending = context.pollMessage()
464
+ if (Option.isNone(pending)) {
465
+ // A machine that runs work keeps, while it is idle, only what the
466
+ // work needs: its execution state is built again for its next message.
467
+ if (!childless) host.state = kernel.rest(host.state)
468
+ return undefined
469
+ }
470
+ message = pending.value
471
+ acknowledged = Runtime.isAcknowledgedMessage(message)
472
+ }
473
+ const event: unknown = acknowledged ? (message as Runtime.AcknowledgedMessage<unknown>).event : message
474
+ if (event instanceof Runtime.RefusedEvent) return failPlanning(event.error)
475
+ const work = !childless && event instanceof Runtime.WorkMessage ? event : undefined
476
+ let step: OwnedStep
477
+ if (work !== undefined) {
478
+ step = takeWork(host.state, work, acknowledged)
479
+ } else if (acknowledged) {
480
+ // Only an acknowledged delivery reads the microsteps of its step.
481
+ step = kernel.sendDecoded(host.state, event as Machine.EventOf<any>, true)
482
+ } else {
483
+ // An event that only moves the machine is committed without a step or new state wrapper.
484
+ const moved = kernel.move(host.state, event as Machine.EventOf<any>)
485
+ if (moved === undefined) continue
486
+ if (moved instanceof Step.Step) {
487
+ step = moved
488
+ } else {
489
+ const notification = context.commit(moved)
490
+ if (notification !== undefined) {
491
+ return context.runAfterChanges(notification.pipe(Effect.andThen(loop(context))))
492
+ }
493
+ continue
494
+ }
495
+ }
496
+ const before = acknowledged || !step.planned ? context.state() : undefined
497
+ const state = step.state
498
+ host.state = state
499
+ const exit = state.exit
500
+ if (exit === undefined && step.commands.length === 0 && step.emitted.length === 0) {
501
+ if (!step.planned) {
502
+ if (step.stop.length === 0 && step.start.length === 0) {
503
+ if (acknowledged) context.completeMessage({ before: before!, plan: planOf(step), after: before! })
504
+ continue
505
+ }
506
+ } else if (step.stop.length === 0 && step.start.length === 0 && !exitsWork(step, work)) {
507
+ const next = state.snapshot
508
+ const notification = context.commit(next)
509
+ if (notification !== undefined) {
510
+ return context.runAfterChanges(notification.pipe(Effect.andThen(Effect.suspend(() => {
511
+ if (acknowledged) context.completeMessage({ before: before!, plan: planOf(step), after: next })
512
+ return loop(context)
513
+ }))))
514
+ }
515
+ if (acknowledged) context.completeMessage({ before: before!, plan: planOf(step), after: next })
516
+ continue
517
+ }
518
+ }
519
+ if (exit !== undefined && Exit.isFailure(exit)) return Effect.failCause(exit.cause)
520
+ const settled = exitsWork(step, work) ? work!.work as Step.Work : undefined
521
+ return continueEffectful(context, host, step, acknowledged, before ?? context.state(), settled)
522
+ }
523
+ }
524
+
525
+ return {
526
+ run: (context: Context): Drain =>
527
+ Runtime.provideMachineRuntime(Effect.suspend(() => drainSync(context) ?? noQueuedMessages), context.scope),
528
+ runSync: (context: Context, message: Runtime.ProcessMessage<unknown>): Drain | undefined => {
529
+ const remaining = drainSync(context, message)
530
+ return remaining === undefined ? undefined : Runtime.provideMachineRuntime(remaining, context.scope)
531
+ }
532
+ }
533
+ }
534
+
535
+ /** Whether a step settled work whose state it exited, so the work stops before the state is entered again. */
536
+ const exitsWork = (step: OwnedStep, message: Runtime.WorkMessage | undefined): boolean =>
537
+ message !== undefined && message._tag === "Settle" && step.planned &&
538
+ step.exited.includes((message.work as Step.Work).path)
539
+
540
+ /** The result of the first step of a process, as the compiled process takes it. */
541
+ const initialOf = (
542
+ step: OwnedStep,
543
+ host: Host | undefined
544
+ ): Runtime.CompiledProcessInitial<Machine.Snapshot<any>, unknown> => {
545
+ const exit = step.exit
546
+ if (exit !== undefined && Exit.isSuccess(exit)) {
547
+ return host === undefined
548
+ ? { state: step.snapshot, done: true, output: exit.value }
549
+ : { state: step.snapshot, done: true, output: exit.value, executionState: host }
550
+ }
551
+ return host === undefined
552
+ ? { state: step.snapshot, done: false, output: undefined }
553
+ : { state: step.snapshot, done: false, output: undefined, executionState: host }
554
+ }
555
+
556
+ /**
557
+ * The execution of a machine on its owned step, which plans with `plan`.
558
+ *
559
+ * @internal
560
+ */
561
+ export const makeExecution = (
562
+ machine: Machine.Any,
563
+ entry: Entry,
564
+ plan: ExecutionPlan.CompiledExecutionPlan,
565
+ childless: boolean,
566
+ decodeEvent: Runtime.EventDecoder
567
+ ): Runtime.CompiledProcessExecution<Machine.Snapshot<any>, unknown, any, any, any, any> => {
568
+ const kernel = Step.kernel(machine, "Owned", plan)
569
+ const drain = makeDrain(machine, kernel, childless)
570
+
571
+ const first = (scope: Scope): Exit.Exit<OwnedStep, unknown> =>
572
+ entry._tag === "Initial"
573
+ ? kernel.start(new Host(machine, kernel, scope), entry.args, isInspected(scope))
574
+ : kernel.resume(new Host(machine, kernel, scope), entry.snapshot)
575
+
576
+ // An idle machine that invokes no work holds only its snapshot.
577
+ const host = (step: OwnedStep): Host | undefined => {
578
+ if (childless) return undefined
579
+ const host = step.state.planning as Host
580
+ host.state = step.state
581
+ if (step.start.length > 0) host.pending = step
582
+ return host
583
+ }
584
+
585
+ // A machine that its execution plan starts runs no command and emits nothing
586
+ // when it starts, so it starts without an Effect.
587
+ const initialSync = entry._tag === "Initial" && kernel.plan.initial !== undefined
588
+ ? (scope: Scope): Runtime.CompiledProcessInitial<Machine.Snapshot<any>, unknown> => {
589
+ if (kernel.startSnapshot !== undefined && !isInspected(scope)) {
590
+ return kernel.startSnapshot(scope, entry.args)
591
+ }
592
+ const started = first(scope)
593
+ if (Exit.isFailure(started)) throw Cause.squash(started.cause)
594
+ const step = started.value
595
+ if (step.commands.length > 0 || step.emitted.length > 0) {
596
+ throw new Error("Machine started by its execution plan planned startup commands or emissions")
597
+ }
598
+ if (isInspected(scope)) scope.inspectInitial(step.entered, step.microsteps)
599
+ return initialOf(step, host(step))
600
+ }
601
+ : undefined
602
+
603
+ const initial = (
604
+ scope: Scope
605
+ ): Effect.Effect<Runtime.CompiledProcessInitial<Machine.Snapshot<any>, unknown>, unknown, unknown> =>
606
+ Runtime.provideMachineRuntime(
607
+ Effect.suspend(() => {
608
+ const started = first(scope)
609
+ if (Exit.isFailure(started)) return Effect.failCause(started.cause)
610
+ const step = started.value
611
+ if (entry._tag === "Initial" && isInspected(scope)) scope.inspectInitial(step.entered, step.microsteps)
612
+ const result = Effect.succeed(initialOf(step, host(step)))
613
+ const commands = step.commands.length === 0 ? undefined : runCommands(step.commands, scope)
614
+ const emitted = step.emitted.length === 0
615
+ ? undefined
616
+ : CommandRuntime.runEmittedEvents(step.emitted, CommandRuntime.makeLiveRuntime(machine, scope))
617
+ return commands === undefined
618
+ ? emitted === undefined ? result : emitted.pipe(Effect.andThen(result))
619
+ : emitted === undefined
620
+ ? commands.pipe(Effect.andThen(result))
621
+ : commands.pipe(Effect.andThen(emitted), Effect.andThen(result))
622
+ }),
623
+ scope
624
+ )
625
+
626
+ return {
627
+ _tag: "Compiled",
628
+ childless,
629
+ decodeEvent,
630
+ initial,
631
+ ...(initialSync === undefined ? undefined : { initialSync }),
632
+ drain: { _tag: "Owned", run: drain.run, runSync: drain.runSync }
633
+ }
634
+ }
@@ -1,19 +1,8 @@
1
- import type { Logic } from "../../Machine.js"
2
-
3
1
  /** @internal */
4
2
  export const InitialEventTypeId: unique symbol = Symbol("effect/Machine/InitialEvent")
5
3
 
6
4
  /** @internal */
7
- export const SnapshotBuilderStateTypeId: unique symbol = Symbol("effect/Machine/SnapshotBuilderState")
8
-
9
- /** @internal Returns process logic for a child descriptor and optional input. */
10
- export const ChildMachineLogicTypeId: unique symbol = Symbol("effect/Machine/ChildMachineLogic")
5
+ export const InvocationEventTypeId: unique symbol = Symbol("effect/Machine/InvocationEvent")
11
6
 
12
- /**
13
- * @internal Runtime slot of a child machine descriptor. The public
14
- * `ChildMachine` declares this key as a type-level brand only, so descriptors
15
- * cross that erased boundary where they are built and where their logic is read.
16
- */
17
- export interface ChildMachineLogicSlot {
18
- readonly [ChildMachineLogicTypeId]: (input?: unknown) => Logic<any, any, any, any, any, any>
19
- }
7
+ /** @internal */
8
+ export const SnapshotBuilderStateTypeId: unique symbol = Symbol("effect/Machine/SnapshotBuilderState")