@typeonce/effect-machine 0.47.0 → 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 +1122 -814
  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 +1383 -1336
  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
@@ -5,70 +5,13 @@
5
5
  */
6
6
 
7
7
  import * as Effect from "effect/Effect"
8
- import * as Option from "effect/Option"
9
- import type { Machine, Runtime } from "../../Machine.js"
10
- import * as CommandRuntime from "./commandRuntime.js"
11
- import * as Configuration from "./configuration.js"
12
- import { failPlanning, toStartupFailure } from "./errors.js"
8
+ import type { Machine } from "../../Machine.js"
13
9
  import * as ExecutionPlan from "./executionPlan.js"
14
10
  import { type CapturedStateConfig, toImpl } from "./implementation.js"
15
- import * as Invocation from "./invocation.js"
16
- import * as internalPlanner from "./planner.js"
17
- import * as internalRuntime from "./runtime.js"
11
+ import { makeEventDecoderSync } from "./protocol.js"
12
+ import * as ProcessRuntime from "./runtime.js"
18
13
  import * as internalRuntimeProtocol from "./runtimeProtocol.js"
19
- import * as Serialization from "./serialization.js"
20
-
21
- type ProcessEntry =
22
- | {
23
- readonly _tag: "Initial"
24
- readonly args: ReadonlyArray<unknown>
25
- }
26
- | {
27
- readonly _tag: "Resume"
28
- readonly snapshot: Machine.Snapshot<any>
29
- }
30
-
31
- const runSequentialDiscard = <E, R>(
32
- effects: ReadonlyArray<Effect.Effect<void, E, R>>
33
- ): Effect.Effect<void, E, R> =>
34
- effects.length === 0
35
- ? Effect.void
36
- : effects.length === 1
37
- ? effects[0]!
38
- : Effect.all(effects, { discard: true })
39
-
40
- const acknowledgedPlan = (
41
- planned: {
42
- readonly event: unknown
43
- readonly next: unknown
44
- readonly commands: ReadonlyArray<unknown>
45
- readonly emittedEvents: ReadonlyArray<unknown>
46
- readonly microsteps: ReadonlyArray<{
47
- readonly next: unknown
48
- readonly event: unknown
49
- readonly transitions?: ReadonlyArray<unknown>
50
- readonly commands: ReadonlyArray<unknown>
51
- readonly raisedEvents: ReadonlyArray<unknown>
52
- readonly emittedEvents: ReadonlyArray<unknown>
53
- readonly exitPaths: ReadonlyArray<string>
54
- readonly entryPaths: ReadonlyArray<string>
55
- readonly changed: boolean
56
- }>
57
- readonly done: boolean
58
- readonly output: unknown
59
- },
60
- snapshot: (state: unknown) => unknown
61
- ): unknown => ({
62
- event: planned.event,
63
- next: snapshot(planned.next),
64
- commands: planned.commands,
65
- emittedEvents: planned.emittedEvents,
66
- microsteps: planned.microsteps.map((microstep) => {
67
- return { ...microstep, transitions: microstep.transitions ?? [], next: snapshot(microstep.next) }
68
- }),
69
- done: planned.done,
70
- output: planned.output
71
- })
14
+ import * as StepHost from "./stepHost.js"
72
15
 
73
16
  const invokeCapabilityCache = new WeakMap<Machine.Any, boolean>()
74
17
 
@@ -84,649 +27,40 @@ const hasInvokeCapability = (machine: Machine.Any): boolean => {
84
27
  return hasInvokes
85
28
  }
86
29
 
87
- const noQueuedMessages = Effect.succeed(Option.none())
88
-
89
- const makeChildlessCompiledDrain = (
90
- machine: Machine.Any,
91
- checkInitialFinal: boolean
92
- ): Pick<internalRuntimeProtocol.OwnedCompiledProcessDrain<any, any, any, any, any>, "run" | "runSync"> => {
93
- type Context = internalRuntimeProtocol.CompiledProcessContext<any, any>
94
- const executionPlan = ExecutionPlan.compileExecutionPlan(machine)
95
- const liveRuntimes = new WeakMap<object, Runtime<unknown, unknown>>()
96
- const liveRuntime = (context: Context): Runtime<unknown, unknown> => {
97
- let runtime = liveRuntimes.get(context.scope)
98
- if (runtime === undefined) {
99
- runtime = CommandRuntime.makeLiveRuntime(machine, context.scope)
100
- liveRuntimes.set(context.scope, runtime)
101
- }
102
- return runtime
103
- }
104
- const initialOutput = (context: Context): Effect.Effect<Option.Option<any>, any, any> | undefined => {
105
- if (!checkInitialFinal) return undefined
106
- const current = context.state()
107
- return internalPlanner.isFinalState(machine, current)
108
- ? internalPlanner.getFinalOutputEffect(machine, current, internalPlanner.InitialEvent).pipe(
109
- Effect.map(Option.some)
110
- )
111
- : undefined
112
- }
113
- const loop = (context: Context): Effect.Effect<Option.Option<any>, any, any> =>
114
- Effect.suspend(() => drainSync(context) ?? noQueuedMessages)
115
-
116
- // Runs the effectful work of one planned message, then continues the drain.
117
- const continueEffectful = (
118
- context: Context,
119
- acknowledged: boolean,
120
- before: unknown,
121
- planned: ReturnType<typeof executionPlan.plan>,
122
- next: unknown
123
- ): Effect.Effect<Option.Option<any>, any, any> => {
124
- const beforeCommit = planned.commands.length === 0
125
- ? undefined
126
- : CommandRuntime.runCommands(planned.commands, context.scope)
127
- const afterCommit = planned.emittedEvents.length === 0
128
- ? undefined
129
- : CommandRuntime.runEmittedEvents(planned.emittedEvents, liveRuntime(context))
130
- const continueAfterCommit = (): Effect.Effect<Option.Option<any>, any, any> => {
131
- const continued = Effect.suspend(() => {
132
- if (acknowledged) {
133
- context.completeMessage({ before, plan: acknowledgedPlan(planned, executionPlan.snapshot), after: next })
134
- }
135
- return planned.done ? Effect.succeed(Option.some(planned.output)) : loop(context)
136
- })
137
- return afterCommit === undefined ? continued : afterCommit.pipe(Effect.andThen(continued))
138
- }
139
- const commitAndContinue = (): Effect.Effect<Option.Option<any>, any, any> => {
140
- const notification = context.commit(next)
141
- const continued = continueAfterCommit()
142
- const effect = notification === undefined ? continued : notification.pipe(Effect.andThen(continued))
143
- return notification === undefined && afterCommit === undefined
144
- ? effect
145
- : context.runAfterChanges(effect)
146
- }
147
- return beforeCommit === undefined
148
- ? commitAndContinue()
149
- : context.runAfterChanges(beforeCommit.pipe(Effect.andThen(Effect.suspend(commitAndContinue))))
150
- }
151
-
152
- // Commits queued messages whose transitions need no effectful work. Returns
153
- // the drain continuing from the first message that does, or `undefined`
154
- // once the mailbox is empty. Only this drain commits a childless machine's
155
- // state, so the compact execution state stays valid in the process context
156
- // while the machine is idle and the next send does not re-derive it.
157
- const drainSync = (
158
- context: Context,
159
- first?: internalRuntimeProtocol.ProcessMessage<any>
160
- ): Effect.Effect<Option.Option<any>, any, any> | undefined => {
161
- let delivered = first
162
- while (true) {
163
- let message: internalRuntimeProtocol.ProcessMessage<any>
164
- if (delivered !== undefined) {
165
- message = delivered
166
- delivered = undefined
167
- context.beginMessage(message)
168
- } else {
169
- const pending = context.pollMessage()
170
- if (Option.isNone(pending)) {
171
- return undefined
172
- }
173
- message = pending.value
174
- }
175
-
176
- const acknowledged = internalRuntimeProtocol.isAcknowledgedMessage(message)
177
- const event = acknowledged ? message.event : message
178
- const before = context.state()
179
-
180
- let planned
181
- try {
182
- planned = executionPlan.plan(
183
- context.executionState ??
184
- executionPlan.fromConfiguration(Configuration.normalizeConfigurationSync(machine, before)),
185
- event,
186
- acknowledged,
187
- context.scope
188
- )
189
- } catch (error) {
190
- return failPlanning(error)
191
- }
192
- context.executionState = planned.next
193
- if (planned.microsteps.length === 0) {
194
- if (acknowledged) {
195
- context.completeMessage({ before, plan: acknowledgedPlan(planned, executionPlan.snapshot), after: before })
196
- }
197
- continue
198
- }
199
-
200
- const next = executionPlan.snapshot(planned.next)
201
- if (planned.commands.length > 0 || planned.emittedEvents.length > 0 || planned.done) {
202
- return continueEffectful(context, acknowledged, before, planned, next)
203
- }
204
- const notification = context.commit(next)
205
- if (notification !== undefined) {
206
- return context.runAfterChanges(notification.pipe(Effect.andThen(Effect.suspend(() => {
207
- if (acknowledged) {
208
- context.completeMessage({ before, plan: acknowledgedPlan(planned, executionPlan.snapshot), after: next })
209
- }
210
- return loop(context)
211
- }))))
212
- }
213
- if (acknowledged) {
214
- context.completeMessage({ before, plan: acknowledgedPlan(planned, executionPlan.snapshot), after: next })
215
- }
216
- }
217
- }
218
-
219
- return {
220
- run: (context) =>
221
- internalRuntimeProtocol.provideMachineRuntime(initialOutput(context) ?? loop(context), context.scope),
222
- runSync: (context, message) => {
223
- // A machine resumed in a final state reports its output instead; the
224
- // message stays in flight, so termination settles it as stopped.
225
- const output = initialOutput(context)
226
- if (output !== undefined) {
227
- context.beginMessage(message)
228
- }
229
- const remaining = output ?? drainSync(context, message)
230
- return remaining === undefined
231
- ? undefined
232
- : internalRuntimeProtocol.provideMachineRuntime(remaining, context.scope)
233
- }
234
- }
235
- }
236
-
237
- class InvokeExecutionState {
238
- initialized = false
239
- initial:
240
- | {
241
- readonly configuration: unknown
242
- readonly activeConfiguration: Configuration.ActiveConfiguration
243
- readonly entryPaths: ReadonlyArray<string>
244
- }
245
- | undefined
246
-
247
- constructor(initial?: {
248
- readonly configuration: unknown
249
- readonly activeConfiguration: Configuration.ActiveConfiguration
250
- readonly entryPaths: ReadonlyArray<string>
251
- }) {
252
- this.initial = initial
253
- }
254
- }
255
-
256
- const makeInvokingCompiledDrain = (
257
- machine: Machine.Any,
258
- checkInitialFinal: boolean
259
- ): (
260
- context: internalRuntimeProtocol.CompiledProcessContext<any, any>
261
- ) => Effect.Effect<Option.Option<any>, any, any> => {
262
- const executionPlan = ExecutionPlan.compileExecutionPlan(machine)
263
- return (context) => {
264
- let current = context.state()
265
- if (checkInitialFinal && internalPlanner.isFinalState(machine, current)) {
266
- return internalPlanner.getFinalOutputEffect(
267
- machine,
268
- current,
269
- internalPlanner.InitialEvent
270
- ).pipe(Effect.map(Option.some))
271
- }
272
-
273
- const scope = context.scope
274
- const stored = context.executionState
275
- const execution = stored instanceof InvokeExecutionState
276
- ? stored
277
- : new InvokeExecutionState()
278
- context.executionState = execution
279
- let liveRuntime: Runtime<any, any> | undefined
280
- let configuration: unknown
281
-
282
- let loop: Effect.Effect<Option.Option<any>, any, any>
283
- loop = Effect.suspend(() => {
284
- const pending = context.pollMessage()
285
- if (Option.isNone(pending)) {
286
- configuration = undefined
287
- return Effect.succeed(Option.none())
288
- }
289
-
290
- const message = pending.value
291
- const acknowledged = internalRuntimeProtocol.isAcknowledgedMessage(message)
292
- const event = acknowledged ? message.event : message
293
- const before = current
294
-
295
- let planned
296
- try {
297
- planned = executionPlan.plan(
298
- configuration ??
299
- executionPlan.fromConfiguration(Configuration.normalizeConfigurationSync(machine, current)),
300
- event,
301
- acknowledged,
302
- scope
303
- )
304
- } catch (error) {
305
- return failPlanning(error)
306
- }
307
- configuration = planned.next
308
- if (planned.microsteps.length === 0) {
309
- if (acknowledged) {
310
- context.completeMessage({ before, plan: acknowledgedPlan(planned, executionPlan.snapshot), after: current })
311
- }
312
- return loop
313
- }
314
-
315
- const changed = planned.microsteps.some((step) => step.changed)
316
- const exitPaths = changed ? planned.microsteps.flatMap((step) => step.exitPaths) : []
317
- const entryEvents = new Map<string, Machine.LifecycleEvent<any>>()
318
- if (changed) {
319
- for (const step of planned.microsteps) {
320
- if (step.changed) {
321
- for (const path of step.entryPaths) {
322
- entryEvents.set(path, step.event)
323
- }
324
- }
325
- }
326
- }
327
-
328
- const activeConfiguration = executionPlan.toConfiguration(planned.next)
329
- const next = executionPlan.snapshot(planned.next)
330
- const beforeCommit: Array<Effect.Effect<void, any, any>> = []
331
- if (planned.commands.length > 0) {
332
- beforeCommit.push(CommandRuntime.runCommands(planned.commands, scope))
333
- }
334
- if (changed) {
335
- const stopping = context.ownedChildren.stopPaths(exitPaths)
336
- if (stopping !== undefined) beforeCommit.push(stopping)
337
- }
338
- const afterCommit: Array<Effect.Effect<void, any, any>> = []
339
- if (planned.emittedEvents.length > 0) {
340
- afterCommit.push(
341
- CommandRuntime.runEmittedEvents(
342
- planned.emittedEvents,
343
- liveRuntime ??= CommandRuntime.makeLiveRuntime(machine, scope)
344
- )
345
- )
346
- }
347
- if (planned.done) {
348
- afterCommit.push(context.ownedChildren.stopAll())
349
- } else if (changed) {
350
- for (const [path, entryEvent] of entryEvents) {
351
- const starting = Invocation.startAll(
352
- machine,
353
- scope,
354
- context.ownedChildren,
355
- activeConfiguration,
356
- [path],
357
- entryEvent
358
- )
359
- if (starting !== undefined) afterCommit.push(starting)
360
- }
361
- }
362
-
363
- const commit = (): Effect.Effect<void> | undefined => {
364
- const notification = context.commit(next)
365
- current = next
366
- return notification
367
- }
368
- const continueAfterCommit = (): Effect.Effect<Option.Option<any>, any, any> => {
369
- const continued = Effect.suspend(() => {
370
- if (acknowledged) {
371
- context.completeMessage({ before, plan: acknowledgedPlan(planned, executionPlan.snapshot), after: next })
372
- }
373
- return planned.done ? Effect.succeed(Option.some(planned.output)) : loop
374
- })
375
- return afterCommit.length === 0
376
- ? continued
377
- : runSequentialDiscard(afterCommit).pipe(Effect.andThen(continued))
378
- }
379
- const commitAndContinue = (): Effect.Effect<Option.Option<any>, any, any> => {
380
- const notification = commit()
381
- const continued = continueAfterCommit()
382
- const effect = notification === undefined ? continued : notification.pipe(Effect.andThen(continued))
383
- return notification === undefined && afterCommit.length === 0
384
- ? effect
385
- : context.runAfterChanges(effect)
386
- }
387
- if (beforeCommit.length === 0) {
388
- return commitAndContinue()
389
- }
390
- return context.runAfterChanges(
391
- runSequentialDiscard(beforeCommit).pipe(Effect.andThen(Effect.suspend(commitAndContinue)))
392
- )
393
- })
30
+ const eventDecoders = new WeakMap<Machine.Any, internalRuntimeProtocol.EventDecoder>()
394
31
 
395
- const initialize = (): Effect.Effect<Option.Option<any>, any, any> => {
396
- if (execution.initialized) {
397
- return loop
398
- }
399
- const seeded = execution.initial
400
- const initialConfiguration = seeded?.activeConfiguration ??
401
- Configuration.normalizeConfigurationSync(machine, current)
402
- configuration = seeded?.configuration ?? executionPlan.fromConfiguration(initialConfiguration)
403
- const starting = Invocation.startAll(
404
- machine,
405
- scope,
406
- context.ownedChildren,
407
- initialConfiguration,
408
- seeded?.entryPaths ?? Configuration.getInitialEntryPaths(machine, initialConfiguration),
409
- internalPlanner.InitialEvent
410
- )
411
- execution.initial = undefined
412
- execution.initialized = true
413
- return starting === undefined ? loop : starting.pipe(Effect.andThen(loop))
414
- }
415
- return internalRuntimeProtocol.provideMachineRuntime(Effect.suspend(initialize), scope)
32
+ // One decoder per machine, so a started process holds no closure of its own
33
+ // for it.
34
+ const eventDecoderOf = (machine: Machine.Any): internalRuntimeProtocol.EventDecoder => {
35
+ let decoder = eventDecoders.get(machine)
36
+ if (decoder === undefined) {
37
+ decoder = makeEventDecoderSync(machine)
38
+ eventDecoders.set(machine, decoder)
416
39
  }
40
+ return decoder
417
41
  }
418
42
 
419
- // Planned initial states only carry an output once the machine is done.
420
- const initialResult = (
421
- planned: { readonly state: Machine.Snapshot<any>; readonly done: boolean; readonly output: unknown }
422
- ): internalRuntimeProtocol.CompiledProcessInitial<Machine.Snapshot<any>, unknown> =>
423
- planned.done
424
- ? { state: planned.state, done: true, output: planned.output }
425
- : { state: planned.state, done: false, output: undefined }
426
-
427
- // Process logic executes erased machine definitions. The public `Machine`
43
+ // Process logic executes erased machine definitions. The public `LocalMachine`
428
44
  // module owns the typed contract that specializes this boundary.
429
45
  const makeProcessLogic = (
430
46
  machine: Machine.Any,
431
- entry: ProcessEntry
47
+ entry: StepHost.Entry,
48
+ plan: ExecutionPlan.CompiledExecutionPlan
432
49
  ): internalRuntimeProtocol.ProcessLogic<any, any, any, any, any, any> => {
433
- const hasInvokes = hasInvokeCapability(machine)
434
- const executionPlan = ExecutionPlan.compileExecutionPlan(machine)
435
- const initialArgs = entry._tag === "Initial" ? entry.args : []
436
- const compiledInitial = entry._tag === "Initial" ? executionPlan.initial : undefined
437
- const makeCompiledInitial = compiledInitial === undefined ? undefined : (
438
- scope: internalRuntimeProtocol.ProcessScope<Machine.EventOf<any>>
439
- ) => {
440
- try {
441
- const planned = compiledInitial(initialArgs, scope)
442
- scope.inspectInitial(planned.initialEntryPaths)
443
- const result = initialResult(planned)
444
- return hasInvokes
445
- ? {
446
- ...result,
447
- executionState: new InvokeExecutionState({
448
- configuration: planned.configuration,
449
- activeConfiguration: planned.activeConfiguration,
450
- entryPaths: planned.initialEntryPaths
451
- })
452
- }
453
- : result
454
- } catch (error) {
455
- throw toStartupFailure(error)
456
- }
457
- }
458
- const makeInitial = (
459
- scope: internalRuntimeProtocol.ProcessScope<Machine.EventOf<any>>
460
- ) =>
461
- compiledInitial === undefined
462
- ? internalRuntimeProtocol.provideMachineRuntime(
463
- internalPlanner.planInitial(internalPlanner.withMachineReferences(machine, scope), ...initialArgs).pipe(
464
- Effect.flatMap((planned) => {
465
- scope.inspectInitial(planned.initialEntryPaths, planned.microsteps)
466
- const commands = planned.commands.length === 0
467
- ? undefined
468
- : CommandRuntime.runCommands(planned.commands, scope)
469
- const emitted = planned.emittedEvents.length === 0
470
- ? undefined
471
- : CommandRuntime.runEmittedEvents(
472
- planned.emittedEvents,
473
- CommandRuntime.makeLiveRuntime(machine, scope)
474
- )
475
- const result = Effect.succeed(initialResult(planned))
476
- return commands === undefined
477
- ? emitted === undefined ? result : emitted.pipe(Effect.andThen(result))
478
- : emitted === undefined
479
- ? commands.pipe(Effect.andThen(result))
480
- : commands.pipe(Effect.andThen(emitted), Effect.andThen(result))
481
- })
482
- ),
483
- scope
484
- )
485
- : Effect.try({ try: () => makeCompiledInitial!(scope), catch: toStartupFailure })
486
- return ({
50
+ const execution = StepHost.makeExecution(
51
+ machine,
52
+ entry,
53
+ plan,
54
+ !hasInvokeCapability(machine),
55
+ eventDecoderOf(machine)
56
+ )
57
+ return {
487
58
  inspection: { kind: "Machine", definition: machine },
488
- execution: {
489
- _tag: "Compiled",
490
- childless: !hasInvokes,
491
- ...(entry._tag === "Initial" ? { initial: makeInitial } : {}),
492
- ...(makeCompiledInitial === undefined ? {} : { initialSync: makeCompiledInitial }),
493
- drain: hasInvokes
494
- ? { _tag: "Owned", run: makeInvokingCompiledDrain(machine, entry._tag === "Resume") }
495
- : { _tag: "Owned", ...makeChildlessCompiledDrain(machine, entry._tag === "Resume") }
496
- },
497
- initial: (scope) =>
498
- entry._tag === "Resume"
499
- ? internalRuntimeProtocol.provideMachineRuntime(
500
- Serialization.normalizeSnapshotEffect(machine, entry.snapshot),
501
- scope
502
- )
503
- : makeInitial(scope).pipe(Effect.map((initialized) => initialized.state)),
504
- run: (context) =>
505
- internalRuntimeProtocol.provideMachineRuntime(
506
- Effect.gen(function*() {
507
- const { completeMessage, pollMessage, receiveMessage, state, setState } = context
508
- if (completeMessage === undefined || pollMessage === undefined || receiveMessage === undefined) {
509
- return yield* Effect.die(new Error("Machine statechart started without acknowledged mailbox access"))
510
- }
511
- let terminal: { readonly output: any } | undefined
512
-
513
- let current = yield* state
514
- if (internalPlanner.isFinalState(machine, current)) {
515
- return yield* internalPlanner.getFinalOutputEffect(
516
- machine,
517
- current,
518
- internalPlanner.InitialEvent
519
- )
520
- }
521
-
522
- if (!hasInvokes) {
523
- // A queued batch is produced entirely by this worker, so its
524
- // configuration is already validated. Drop both caches before
525
- // blocking again so idle machines retain only the public snapshot.
526
- // Keeping the loop in this generator avoids a suspended generator
527
- // per iteration; every iteration still crosses Effect boundaries,
528
- // so the Effect scheduler remains responsible for cooperative yield.
529
- let configuration: Configuration.ActiveConfiguration | undefined
530
- let pendingMessage: Option.Option<internalRuntimeProtocol.ProcessMessage<Machine.EventOf<any>>> = Option
531
- .none()
532
- let liveRuntime: Runtime<Machine.EventOf<any>, Machine.EmittedEventOf<any>> | undefined
533
- while (terminal === undefined) {
534
- const message = Option.isSome(pendingMessage) ? pendingMessage.value : yield* receiveMessage
535
- pendingMessage = Option.none()
536
- const acknowledged = internalRuntimeProtocol.isAcknowledgedMessage(message)
537
- const event = acknowledged ? message.event : message
538
- const before = current
539
- let planned
540
- try {
541
- planned = internalPlanner.planConfiguration(
542
- machine,
543
- Configuration.withMachineReferences(
544
- configuration ?? Configuration.normalizeConfigurationSync(machine, current),
545
- context
546
- ),
547
- event
548
- )
549
- } catch (error) {
550
- return yield* failPlanning(error)
551
- }
552
- configuration = planned.next
553
-
554
- if (planned.microsteps.length > 0) {
555
- const next = Configuration.snapshotFromConfiguration(machine, planned.next)
556
- yield* CommandRuntime.runCommands(planned.commands, context)
557
- yield* setState(next)
558
- current = next
559
- if (planned.emittedEvents.length > 0) {
560
- yield* CommandRuntime.runEmittedEvents(
561
- planned.emittedEvents,
562
- liveRuntime ??= CommandRuntime.makeLiveRuntime(machine, context)
563
- )
564
- }
565
-
566
- if (planned.done) {
567
- terminal = { output: planned.output }
568
- }
569
- }
570
-
571
- if (acknowledged) {
572
- completeMessage({
573
- before,
574
- plan: acknowledgedPlan(
575
- planned,
576
- (state) =>
577
- Configuration.snapshotFromConfiguration(machine, state as Configuration.ActiveConfiguration)
578
- ),
579
- after: current
580
- })
581
- }
582
-
583
- if (terminal === undefined) {
584
- pendingMessage = yield* pollMessage
585
- if (Option.isNone(pendingMessage)) {
586
- configuration = undefined
587
- }
588
- }
589
- }
590
-
591
- if (terminal === undefined) {
592
- return yield* Effect.die(
593
- new Error("Machine process stopped receiving events before reaching a terminal configuration")
594
- )
595
- }
596
- return terminal.output
597
- }
598
-
599
- // The execution descriptor requests this owner-local capability only
600
- // when an invoking statechart is deliberately run by the generic
601
- // reference strategy.
602
- const ownedChildren = context.ownedChildren
603
- if (ownedChildren === undefined) {
604
- return yield* Effect.die(new Error("Invoking statechart started without an owned child runtime"))
605
- }
606
- const startInvokes: (
607
- configuration: Configuration.ActiveConfiguration,
608
- paths: ReadonlyArray<string>,
609
- event: Machine.LifecycleEvent<any>
610
- ) => Effect.Effect<void, any, any> = (configuration, paths, event) => (Invocation.startAll(
611
- machine,
612
- context,
613
- ownedChildren,
614
- configuration,
615
- paths,
616
- event
617
- ) ?? Effect.void)
618
- const stopInvokes = (paths: ReadonlyArray<string>): Effect.Effect<void> =>
619
- ownedChildren.stopPaths(paths) ?? Effect.void
620
-
621
- return yield* Effect.gen(function*() {
622
- let configuration: Configuration.ActiveConfiguration | undefined = yield* Configuration
623
- .normalizeConfigurationEffect(
624
- machine,
625
- current
626
- )
627
- yield* startInvokes(
628
- configuration,
629
- Configuration.getInitialEntryPaths(machine, configuration),
630
- internalPlanner.InitialEvent
631
- )
632
- // As above, keep the normalized configuration only while this
633
- // worker can continue draining an already queued batch.
634
- configuration = undefined
635
- let pendingMessage: Option.Option<internalRuntimeProtocol.ProcessMessage<Machine.EventOf<any>>> = Option
636
- .none()
637
- let liveRuntime: Runtime<Machine.EventOf<any>, Machine.EmittedEventOf<any>> | undefined
638
-
639
- // Match the compact non-invoke loop while retaining state-scoped
640
- // child lifecycle work at the same ordered Effect boundaries.
641
- while (terminal === undefined) {
642
- const message = Option.isSome(pendingMessage) ? pendingMessage.value : yield* receiveMessage
643
- pendingMessage = Option.none()
644
- const acknowledged = internalRuntimeProtocol.isAcknowledgedMessage(message)
645
- const event = acknowledged ? message.event : message
646
- const before = current
647
- let planned
648
- try {
649
- planned = internalPlanner.planConfiguration(
650
- machine,
651
- Configuration.withMachineReferences(
652
- configuration ?? Configuration.normalizeConfigurationSync(machine, current),
653
- context
654
- ),
655
- event
656
- )
657
- } catch (error) {
658
- return yield* failPlanning(error)
659
- }
660
- configuration = planned.next
661
- if (planned.microsteps.length > 0) {
662
- const changed = planned.microsteps.some((step) => step.changed)
663
- const exitPaths = planned.microsteps.flatMap((step) => step.exitPaths)
664
- const entryEvents = new Map<string, Machine.LifecycleEvent<any>>()
665
- for (const step of planned.microsteps) {
666
- if (step.changed) {
667
- for (const path of step.entryPaths) {
668
- entryEvents.set(path, step.event)
669
- }
670
- }
671
- }
672
-
673
- const next = Configuration.snapshotFromConfiguration(machine, planned.next)
674
- yield* CommandRuntime.runCommands(planned.commands, context)
675
- if (changed) {
676
- yield* stopInvokes(exitPaths)
677
- }
678
- yield* setState(next)
679
- current = next
680
- if (planned.emittedEvents.length > 0) {
681
- yield* CommandRuntime.runEmittedEvents(
682
- planned.emittedEvents,
683
- liveRuntime ??= CommandRuntime.makeLiveRuntime(machine, context)
684
- )
685
- }
686
-
687
- if (planned.done) {
688
- terminal = { output: planned.output }
689
- yield* ownedChildren.stopAll()
690
- } else if (changed) {
691
- for (const [path, entryEvent] of entryEvents) {
692
- yield* startInvokes(planned.next, [path], entryEvent)
693
- }
694
- }
695
- }
696
-
697
- if (acknowledged) {
698
- completeMessage({
699
- before,
700
- plan: acknowledgedPlan(
701
- planned,
702
- (state) =>
703
- Configuration.snapshotFromConfiguration(machine, state as Configuration.ActiveConfiguration)
704
- ),
705
- after: current
706
- })
707
- }
708
-
709
- if (terminal === undefined) {
710
- pendingMessage = yield* pollMessage
711
- if (Option.isNone(pendingMessage)) {
712
- configuration = undefined
713
- }
714
- }
715
- }
716
-
717
- if (terminal === undefined) {
718
- return yield* Effect.die(
719
- new Error("Machine process stopped receiving events before reaching a terminal configuration")
720
- )
721
- }
722
- return terminal.output
723
- }).pipe(
724
- Effect.onExit(() => ownedChildren.stopAll())
725
- )
726
- }),
727
- context
728
- )
729
- })
59
+ execution,
60
+ initial: (scope) => execution.initial!(scope).pipe(Effect.map((initialized) => initialized.state)),
61
+ // A machine runs only on its compiled execution.
62
+ run: () => Effect.die(new Error("A machine runs on its compiled execution"))
63
+ }
730
64
  }
731
65
 
732
66
  const initialProcessLogicCache = new WeakMap<
@@ -739,7 +73,7 @@ export const toProcessLogic = (
739
73
  ...args: ReadonlyArray<unknown>
740
74
  ): internalRuntimeProtocol.ProcessLogic<any, any, any, any, any, any> => {
741
75
  if (args.length > 0) {
742
- return makeProcessLogic(machine, { _tag: "Initial", args })
76
+ return makeProcessLogic(machine, { _tag: "Initial", args }, ExecutionPlan.compileExecutionPlan(machine))
743
77
  }
744
78
  // The execution descriptor stores process-local invoke sessions by each
745
79
  // runtime address and evaluates initialization/services on every start. A
@@ -750,7 +84,7 @@ export const toProcessLogic = (
750
84
  if (cached !== undefined) {
751
85
  return cached as any
752
86
  }
753
- const logic = makeProcessLogic(machine, { _tag: "Initial", args })
87
+ const logic = makeProcessLogic(machine, { _tag: "Initial", args }, ExecutionPlan.compileExecutionPlan(machine))
754
88
  initialProcessLogicCache.set(machine, logic as any)
755
89
  return logic
756
90
  }
@@ -759,63 +93,85 @@ const toResumedProcessLogic = (
759
93
  machine: Machine.Any,
760
94
  snapshot: Machine.Snapshot<any>
761
95
  ): internalRuntimeProtocol.ProcessLogic<any, any, any, any, any, any> =>
762
- makeProcessLogic(machine, { _tag: "Resume", snapshot })
96
+ makeProcessLogic(machine, { _tag: "Resume", snapshot }, ExecutionPlan.compileExecutionPlan(machine))
97
+
98
+ const toChildProcessLogic: internalRuntimeProtocol.MakeChildLogic = (machine, input) =>
99
+ machine.input === undefined ? toProcessLogic(machine) : toProcessLogic(machine, input)
100
+
101
+ const runtime = ProcessRuntime.make(toChildProcessLogic)
102
+
103
+ /**
104
+ * Which execution plan a local machine test runs with, on the one local
105
+ * process driver: `"reference"` forces the reference planner, and `"indexed"`
106
+ * uses the default compiled plan (indexed kernel when supported, otherwise
107
+ * reference).
108
+ *
109
+ * @internal
110
+ */
111
+ export type LocalPlannerStrategy = "reference" | "indexed"
112
+
113
+ const strategyLogic = (
114
+ machine: Machine.Any,
115
+ strategy: LocalPlannerStrategy,
116
+ entry: StepHost.Entry
117
+ ): internalRuntimeProtocol.ProcessLogic<any, any, any, any, any, any> =>
118
+ strategy === "reference"
119
+ ? makeProcessLogic(machine, entry, ExecutionPlan.referenceExecutionPlan(machine))
120
+ : entry._tag === "Initial"
121
+ ? toProcessLogic(machine, ...entry.args)
122
+ : toResumedProcessLogic(machine, entry.snapshot)
763
123
 
764
- /** @internal Test-only runtime strategy selection for a fresh machine. */
124
+ /** @internal Test-only planner selection for a fresh local machine. */
765
125
  export const startWithRuntimeStrategyForTesting = (
766
126
  machine: Machine.Any,
767
- strategy: internalRuntime.ProcessRuntimeStrategy,
127
+ strategy: LocalPlannerStrategy,
768
128
  ...args: ReadonlyArray<unknown>
769
129
  ): Effect.Effect<internalRuntimeProtocol.MachineRef<any, any, any, any>, any, any> =>
770
- internalRuntime.startProcessWithStrategyForTesting(
771
- toProcessLogic(machine, ...args),
772
- strategy,
130
+ runtime.startProcess(
131
+ strategyLogic(machine, strategy, { _tag: "Initial", args }),
773
132
  machine.id === undefined ? undefined : { id: machine.id }
774
133
  )
775
134
 
776
- /** @internal Test-only prepared startup strategy selection for a fresh machine. */
135
+ /** @internal Test-only prepared startup planner selection for a fresh local machine. */
777
136
  export const prepareWithRuntimeStrategyForTesting = (
778
137
  machine: Machine.Any,
779
- strategy: internalRuntime.ProcessRuntimeStrategy,
138
+ strategy: LocalPlannerStrategy,
780
139
  ...args: ReadonlyArray<unknown>
781
140
  ): Effect.Effect<internalRuntimeProtocol.PreparedProcess<any, any, any, any, any, any, any>, any, any> =>
782
- internalRuntime.prepareProcessWithStrategyForTesting(
783
- toProcessLogic(machine, ...args),
784
- strategy,
141
+ runtime.prepareProcess(
142
+ strategyLogic(machine, strategy, { _tag: "Initial", args }),
785
143
  machine.id === undefined ? undefined : { id: machine.id }
786
144
  )
787
145
 
788
- /** @internal Test-only runtime strategy selection for a resumed machine. */
146
+ /** @internal Test-only planner selection for a resumed local machine. */
789
147
  export const resumeWithRuntimeStrategyForTesting = (
790
148
  machine: Machine.Any,
791
149
  snapshot: Machine.Snapshot<any>,
792
- strategy: internalRuntime.ProcessRuntimeStrategy
150
+ strategy: LocalPlannerStrategy
793
151
  ): Effect.Effect<internalRuntimeProtocol.MachineRef<any, any, any, any>, any, any> =>
794
- internalRuntime.startProcessWithStrategyForTesting(
795
- toResumedProcessLogic(machine, snapshot),
796
- strategy,
152
+ runtime.startProcess(
153
+ strategyLogic(machine, strategy, { _tag: "Resume", snapshot }),
797
154
  machine.id === undefined ? undefined : { id: machine.id }
798
155
  )
799
156
 
800
- /** @internal Test-only runtime strategy selection for a prepared resumed machine. */
157
+ /** @internal Test-only planner selection for a prepared resumed local machine. */
801
158
  export const prepareResumedWithRuntimeStrategyForTesting = (
802
159
  machine: Machine.Any,
803
160
  snapshot: Machine.Snapshot<any>,
804
- strategy: internalRuntime.ProcessRuntimeStrategy
161
+ strategy: LocalPlannerStrategy
805
162
  ): Effect.Effect<internalRuntimeProtocol.PreparedProcess<any, any, any, any, any, any, any>, any, any> =>
806
- internalRuntime.prepareProcessWithStrategyForTesting(
807
- toResumedProcessLogic(machine, snapshot),
808
- strategy,
163
+ runtime.prepareProcess(
164
+ strategyLogic(machine, strategy, { _tag: "Resume", snapshot }),
809
165
  machine.id === undefined ? undefined : { id: machine.id }
810
166
  )
811
167
 
812
- // The public `Machine` module owns the typed start, prepare, and resume
168
+ // The public `LocalMachine` module owns the typed start, prepare, and resume
813
169
  // signatures. These implementations are the erased boundary they specialize.
814
170
  export const start = (
815
171
  machine: Machine.Any,
816
172
  ...args: ReadonlyArray<unknown>
817
173
  ): Effect.Effect<internalRuntimeProtocol.MachineRef<any, any, any, any, any>, any, any> =>
818
- internalRuntime.startProcess(
174
+ runtime.startProcess(
819
175
  toProcessLogic(machine, ...args),
820
176
  machine.id === undefined ? undefined : { id: machine.id }
821
177
  )
@@ -824,7 +180,7 @@ export const prepare = (
824
180
  machine: Machine.Any,
825
181
  ...args: ReadonlyArray<unknown>
826
182
  ): Effect.Effect<internalRuntimeProtocol.PreparedProcess<any, any, any, any, any, any, any>> =>
827
- internalRuntime.prepareProcess(
183
+ runtime.prepareProcess(
828
184
  toProcessLogic(machine, ...args),
829
185
  machine.id === undefined ? undefined : { id: machine.id }
830
186
  )
@@ -833,7 +189,7 @@ export const resume = (
833
189
  machine: Machine.Any,
834
190
  snapshot: Machine.Snapshot<any>
835
191
  ): Effect.Effect<internalRuntimeProtocol.MachineRef<any, any, any, any, any>, any, any> =>
836
- internalRuntime.startProcess(
192
+ runtime.startProcess(
837
193
  toResumedProcessLogic(machine, snapshot),
838
194
  machine.id === undefined ? undefined : { id: machine.id }
839
195
  )