@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
@@ -10,7 +10,7 @@ import { InfiniteTransitionError, StoppedError } from "./errors.js";
10
10
  import { toImpl } from "./implementation.js";
11
11
  import { isDataInitializer } from "./initialDeclaration.js";
12
12
  import * as InvocationEvent from "./invocationEvent.js";
13
- import { broadenTransitionBoundary, getEntryPaths, getExitPaths, getLeastCommonAncestor, getTargetNodePath, InitialEvent, MaxMacrostepIterations, normalizeTransition, planConfiguration, removeConflictingTransitions, resolveInitialConfiguration, resolveInitialTarget, sortEntryPaths, sortEvaluatedTransitions, sortExitPaths, validateDeclaredTransitionTarget } from "./planner.js";
13
+ import { broadenTransitionBoundary, getEntryPaths, getExitPaths, getLeastCommonAncestor, getTargetNodePath, InitialEvent, MaxMacrostepIterations, normalizeTransition, planConfiguration, planDecodedConfiguration, removeConflictingTransitions, resolveInitialConfiguration, resolveInitialTarget, sortEntryPaths, sortEvaluatedTransitions, sortExitPaths, validateDeclaredTransitionTarget } from "./planner.js";
14
14
  import { decodeEmitSync, decodeEventSync, decodeInputSync, decodeStateValueSync } from "./protocol.js";
15
15
  import { isCombinedTarget, isInitialTarget, isNoTarget, isSnapshot, isStateUpdate, isTarget, TargetSnapshotTypeId } from "./topology.js";
16
16
  const planningMachineReferences = new WeakMap();
@@ -139,7 +139,7 @@ const compileIndexedExecutionDescriptor = (machine) => {
139
139
  dispatchByLeaf
140
140
  };
141
141
  };
142
- const copyOwnedIndexedState = (state) => ({
142
+ const copyIndexedState = (state) => ({
143
143
  active: state.active.slice(),
144
144
  activeLeaves: state.activeLeaves.slice(),
145
145
  values: state.values.slice(),
@@ -147,11 +147,12 @@ const copyOwnedIndexedState = (state) => ({
147
147
  outputs: state.outputs.slice(),
148
148
  completedOrder: state.completedOrder.slice()
149
149
  });
150
- const retainIndexedMicrostep = (step, retain) => retain ? { ...step, next: copyOwnedIndexedState(step.next) } : step;
151
- const updateOwnedIndexedValue = (state, index, value) => {
152
- state.values[index] = value;
153
- };
154
- const ownedIndexedStateFromActive = (descriptor, configuration) => {
150
+ /** A state equal to `state` whose values the kernel may write, as only `values` is ever written. */
151
+ const withWritableIndexedValues = (state) => ({
152
+ ...state,
153
+ values: state.values.slice()
154
+ });
155
+ const indexedStateFromActive = (descriptor, configuration) => {
155
156
  const active = new Uint8Array(descriptor.nodes.length);
156
157
  const values = new Array(descriptor.nodes.length);
157
158
  const completed = new Uint8Array(descriptor.nodes.length);
@@ -257,6 +258,8 @@ const makeIndexedTransitionContext = (machine, descriptor, configuration, source
257
258
  };
258
259
  };
259
260
  const emptyExecutionValues = Object.freeze([]);
261
+ /** Marks that a flat microstep writes no value. A decoded value may be `undefined`. */
262
+ const noIndexedValue = Symbol("effect/Machine/ExecutionPlan/NoValue");
260
263
  const collectIndexedTransition = (machine, transition, context, evaluate) => {
261
264
  let commands;
262
265
  let raisedEvents;
@@ -332,7 +335,7 @@ const normalizeIndexedTargetStateSync = (machine, descriptor, current, target, a
332
335
  values[activeLeafIndex] = decodeStateValueSync(machine, descriptor.nodes[activeLeafIndex], target.value);
333
336
  return { ...current, values };
334
337
  }
335
- return ownedIndexedStateFromActive(descriptor, normalizeTargetConfigurationSync(machine, activeConfigurationFromIndexedState(descriptor, current), target));
338
+ return indexedStateFromActive(descriptor, normalizeTargetConfigurationSync(machine, activeConfigurationFromIndexedState(descriptor, current), target));
336
339
  };
337
340
  const collectIndexedEvaluatedTransition = (machine, descriptor, state, selection) => {
338
341
  const transitionResult = collectIndexedTransition(machine, selection.transition.transition, selection.context, selection.transition.evaluate);
@@ -375,7 +378,7 @@ const collectIndexedEvaluatedTransition = (machine, descriptor, state, selection
375
378
  const initialRaisedEvents = (initialResolution?.raisedEvents ?? emptyExecutionValues);
376
379
  let next = target === undefined
377
380
  ? update === undefined ? state : (() => {
378
- const next = copyOwnedIndexedState(state);
381
+ const next = copyIndexedState(state);
379
382
  next.values[descriptor.indexByPath.get(update.path)] = update.value;
380
383
  return next;
381
384
  })()
@@ -436,30 +439,119 @@ const collectIndexedEvaluatedTransition = (machine, descriptor, state, selection
436
439
  choiceTransitions: initialResolution?.transitions ?? []
437
440
  };
438
441
  };
442
+ const retainIndexedTransition = (transition) => ({
443
+ source: transition.selection.sourcePath,
444
+ trigger: transition.selection.trigger,
445
+ reenter: transition.selection.transition.reenter,
446
+ branchIndex: transition.branchIndex,
447
+ branchKey: transition.branchKey,
448
+ target: transition.unresolvedTarget === undefined ? undefined : getTargetNodePath(transition.unresolvedTarget),
449
+ resolvedTarget: transition.target === undefined ? undefined : getTargetNodePath(transition.target),
450
+ updates: transition.update === undefined ? [] : [transition.update.path]
451
+ });
452
+ /**
453
+ * A retained microstep of the hierarchical kernel. It builds its retained
454
+ * transitions on first read, from evaluated transitions that hold only values
455
+ * no later plan writes.
456
+ */
457
+ class RetainedHierarchicalMicrostep {
458
+ next;
459
+ event;
460
+ commands;
461
+ raisedEvents;
462
+ emittedEvents;
463
+ exitPaths;
464
+ entryPaths;
465
+ changed;
466
+ #evaluated;
467
+ #transitions;
468
+ constructor(next, event, evaluated, commands, raisedEvents, emittedEvents, exitPaths, entryPaths, changed) {
469
+ this.next = next;
470
+ this.event = event;
471
+ this.commands = commands;
472
+ this.raisedEvents = raisedEvents;
473
+ this.emittedEvents = emittedEvents;
474
+ this.exitPaths = exitPaths;
475
+ this.entryPaths = entryPaths;
476
+ this.changed = changed;
477
+ this.#evaluated = evaluated;
478
+ }
479
+ get transitions() {
480
+ if (this.#transitions === undefined) {
481
+ this.#transitions = this.#evaluated.map(retainIndexedTransition);
482
+ this.#evaluated = undefined;
483
+ }
484
+ return this.#transitions;
485
+ }
486
+ }
487
+ /**
488
+ * A retained microstep of the flat kernel, which builds its one retained
489
+ * transition on first read.
490
+ */
491
+ class RetainedFlatMicrostep {
492
+ next;
493
+ event;
494
+ commands;
495
+ raisedEvents;
496
+ emittedEvents;
497
+ exitPaths;
498
+ entryPaths;
499
+ changed;
500
+ #source;
501
+ #reenter;
502
+ #branchIndex;
503
+ #branchKey;
504
+ #target;
505
+ #update;
506
+ #transitions;
507
+ constructor(next, event, commands, raisedEvents, emittedEvents, exitPaths, entryPaths, changed, source, reenter, branchIndex, branchKey, target, update) {
508
+ this.next = next;
509
+ this.event = event;
510
+ this.commands = commands;
511
+ this.raisedEvents = raisedEvents;
512
+ this.emittedEvents = emittedEvents;
513
+ this.exitPaths = exitPaths;
514
+ this.entryPaths = entryPaths;
515
+ this.changed = changed;
516
+ this.#source = source;
517
+ this.#reenter = reenter;
518
+ this.#branchIndex = branchIndex;
519
+ this.#branchKey = branchKey;
520
+ this.#target = target;
521
+ this.#update = update;
522
+ }
523
+ get transitions() {
524
+ if (this.#transitions === undefined) {
525
+ const target = this.#target === undefined ? undefined : getTargetNodePath(this.#target);
526
+ this.#transitions = [{
527
+ source: this.#source,
528
+ trigger: { type: "event", event: this.event._tag },
529
+ reenter: this.#reenter,
530
+ branchIndex: this.#branchIndex,
531
+ branchKey: this.#branchKey,
532
+ target,
533
+ resolvedTarget: target,
534
+ updates: this.#update === undefined ? [] : [this.#update.path]
535
+ }];
536
+ }
537
+ return this.#transitions;
538
+ }
539
+ }
439
540
  const indexedMicrostep = (machine, descriptor, state, event, selections, retainTransitions) => {
440
- const retain = (transition) => ({
441
- source: transition.selection.sourcePath,
442
- trigger: transition.selection.trigger,
443
- reenter: transition.selection.transition.reenter,
444
- branchIndex: transition.branchIndex,
445
- branchKey: transition.branchKey,
446
- target: transition.unresolvedTarget === undefined ? undefined : getTargetNodePath(transition.unresolvedTarget),
447
- resolvedTarget: transition.target === undefined ? undefined : getTargetNodePath(transition.target),
448
- updates: transition.update === undefined ? [] : [transition.update.path]
449
- });
450
541
  if (selections.length === 1) {
451
542
  const transition = collectIndexedEvaluatedTransition(machine, descriptor, state, selections[0]);
452
- return {
453
- next: transition.next,
454
- event,
455
- ...(retainTransitions ? { transitions: [retain(transition)] } : undefined),
456
- commands: transition.commands,
457
- raisedEvents: transition.raisedEvents,
458
- emittedEvents: transition.emittedEvents,
459
- exitPaths: transition.exitPaths,
460
- entryPaths: transition.entryPaths,
461
- changed: transition.changed
462
- };
543
+ return retainTransitions
544
+ ? new RetainedHierarchicalMicrostep(transition.next, event, [transition], transition.commands, transition.raisedEvents, transition.emittedEvents, transition.exitPaths, transition.entryPaths, transition.changed)
545
+ : {
546
+ next: transition.next,
547
+ event,
548
+ commands: transition.commands,
549
+ raisedEvents: transition.raisedEvents,
550
+ emittedEvents: transition.emittedEvents,
551
+ exitPaths: transition.exitPaths,
552
+ entryPaths: transition.entryPaths,
553
+ changed: transition.changed
554
+ };
463
555
  }
464
556
  const activeSelections = selections.filter((selection) => !selections.some((other) => other.sourceIndex !== selection.sourceIndex &&
465
557
  isDescendantOf(other.sourcePath, selection.sourcePath)));
@@ -491,19 +583,13 @@ const indexedMicrostep = (machine, descriptor, state, event, selections, retainT
491
583
  const raisedEvents = transitions.flatMap((transition) => transition.raisedEvents);
492
584
  const emittedEvents = transitions.flatMap((transition) => transition.emittedEvents);
493
585
  const changed = transitions.some((transition) => transition.changed);
494
- return {
495
- next,
496
- event,
497
- ...(retainTransitions ? { transitions: transitions.map(retain) } : undefined),
498
- commands,
499
- raisedEvents,
500
- emittedEvents,
501
- exitPaths: changed ? sortExitPaths(machine, transitions.flatMap((transition) => transition.exitPaths)) : [],
502
- entryPaths: changed ? sortEntryPaths(machine, transitions.flatMap((transition) => transition.entryPaths)) : [],
503
- changed
504
- };
586
+ const exitPaths = changed ? sortExitPaths(machine, transitions.flatMap((transition) => transition.exitPaths)) : [];
587
+ const entryPaths = changed ? sortEntryPaths(machine, transitions.flatMap((transition) => transition.entryPaths)) : [];
588
+ return retainTransitions
589
+ ? new RetainedHierarchicalMicrostep(next, event, transitions, commands, raisedEvents, emittedEvents, exitPaths, entryPaths, changed)
590
+ : { next, event, commands, raisedEvents, emittedEvents, exitPaths, entryPaths, changed };
505
591
  };
506
- const planIndexedFlatState = (machine, descriptor, configuration, decoded, retainMicrosteps, machineReferences) => {
592
+ const planIndexedFlatState = (machine, descriptor, configuration, decoded, retainMicrosteps, ownership, machineReferences) => {
507
593
  let current = configuration;
508
594
  let event = decoded;
509
595
  let commands;
@@ -512,137 +598,164 @@ const planIndexedFlatState = (machine, descriptor, configuration, decoded, retai
512
598
  let microsteps;
513
599
  let raisedIndex = 0;
514
600
  let iterations = 0;
515
- while (true) {
516
- iterations += 1;
517
- if (iterations > MaxMacrostepIterations) {
518
- throw new InfiniteTransitionError({
519
- machineId: machine.id,
520
- state: descriptor.nodes[current.activeLeaves[0]].path,
521
- maxIterations: MaxMacrostepIterations
522
- });
523
- }
524
- const sourceIndex = current.activeLeaves[0];
525
- if (sourceIndex === undefined) {
526
- throw new Error("Machine expected an active indexed root state");
527
- }
528
- if (descriptor.nodes[sourceIndex].type === "final") {
529
- const completed = completeConfigurationSync(machine, activeConfigurationFromIndexedState(descriptor, current), event).configuration;
530
- const root = getRootPath(machine, completed);
531
- if (!completed.outputs.has(root)) {
532
- throw new Error("Machine reached a terminal indexed configuration without a completed root output");
601
+ // Whether `current` may be written in place, because nothing but this plan
602
+ // references it. A persistent caller's state, and the state of a retained
603
+ // microstep, are copied before they would be written.
604
+ let writable = ownership === "Owned";
605
+ // The value that this plan overwrote in an owned caller's state, so that a
606
+ // plan that throws leaves that state as it was. Only the active leaf is
607
+ // written, and the leaf of the caller's state stays active while `current`
608
+ // is that state, so one value is all a plan overwrites in it.
609
+ let restoreIndex = -1;
610
+ let restoreValue;
611
+ try {
612
+ while (true) {
613
+ iterations += 1;
614
+ if (iterations > MaxMacrostepIterations) {
615
+ throw new InfiniteTransitionError({
616
+ machineId: machine.id,
617
+ state: descriptor.nodes[current.activeLeaves[0]].path,
618
+ maxIterations: MaxMacrostepIterations
619
+ });
533
620
  }
534
- return {
535
- event: decoded,
536
- next: ownedIndexedStateFromActive(descriptor, completed),
537
- commands: commands ?? emptyExecutionValues,
538
- emittedEvents: emittedEvents ?? emptyExecutionValues,
539
- microsteps: microsteps ?? emptyExecutionValues,
540
- done: true,
541
- output: completed.outputs.get(root)
542
- };
543
- }
544
- const sourcePath = descriptor.nodes[sourceIndex].path;
545
- const transition = normalizeTransition(toImpl(machine).handlers[sourcePath]?.on?.[event._tag]);
546
- if (transition !== undefined) {
547
- const transitionResult = collectIndexedTransition(machine, transition.transition, {
548
- ...resolveMachineReferences(machine, machineReferences),
549
- state: current.values[sourceIndex],
550
- containingState: undefined,
551
- ancestors: {},
552
- event,
553
- snapshot: snapshotFromIndexedState(descriptor, current)
554
- }, transition.evaluate);
555
- const update = isStateUpdate(transitionResult.state) ? transitionResult.state : undefined;
556
- const target = update === undefined ? transitionResult.state : undefined;
557
- if (update !== undefined) {
558
- const node = descriptor.nodes[sourceIndex];
559
- if (update.path !== sourcePath || node.schema === undefined) {
560
- throw new Error(`Machine state update owner "${update.path}" must be the active valued state`);
561
- }
562
- updateOwnedIndexedValue(current, sourceIndex, decodeStateValueSync(machine, node, update.value));
621
+ const sourceIndex = current.activeLeaves[0];
622
+ if (sourceIndex === undefined) {
623
+ throw new Error("Machine expected an active indexed root state");
563
624
  }
564
- validateDeclaredTransitionTarget(sourcePath, { type: "event", event: event._tag }, transition.targets, target);
565
- if (target !== undefined && !isTarget(target) && !isSnapshot(target)) {
566
- throw new Error("Machine expected indexed transition target to be a snapshot or target builder result");
625
+ if (descriptor.nodes[sourceIndex].type === "final") {
626
+ const completed = completeConfigurationSync(machine, activeConfigurationFromIndexedState(descriptor, current), event).configuration;
627
+ const root = getRootPath(machine, completed);
628
+ if (!completed.outputs.has(root)) {
629
+ throw new Error("Machine reached a terminal indexed configuration without a completed root output");
630
+ }
631
+ return {
632
+ event: decoded,
633
+ next: indexedStateFromActive(descriptor, completed),
634
+ commands: commands ?? emptyExecutionValues,
635
+ emittedEvents: emittedEvents ?? emptyExecutionValues,
636
+ microsteps: microsteps ?? emptyExecutionValues,
637
+ done: true,
638
+ output: completed.outputs.get(root)
639
+ };
567
640
  }
568
- let next = current;
569
- if (target !== undefined) {
570
- const targetIndex = descriptor.indexByPath.get(String(target.path));
571
- const isSimpleTarget = isTarget(target)
572
- ? target[TargetSnapshotTypeId] === undefined && target.values === undefined
573
- : !("state" in target) && !("states" in target) && !("completed" in target) && !("history" in target);
574
- if (targetIndex === sourceIndex && isSimpleTarget && current.completedOrder.length === 0 &&
575
- descriptor.valued[sourceIndex]) {
576
- updateOwnedIndexedValue(current, sourceIndex, decodeStateValueSync(machine, descriptor.nodes[sourceIndex], target.value));
641
+ const sourcePath = descriptor.nodes[sourceIndex].path;
642
+ const transition = normalizeTransition(toImpl(machine).handlers[sourcePath]?.on?.[event._tag]);
643
+ if (transition !== undefined) {
644
+ const transitionResult = collectIndexedTransition(machine, transition.transition, {
645
+ ...resolveMachineReferences(machine, machineReferences),
646
+ state: current.values[sourceIndex],
647
+ containingState: undefined,
648
+ ancestors: {},
649
+ event,
650
+ snapshot: snapshotFromIndexedState(descriptor, current)
651
+ }, transition.evaluate);
652
+ const update = isStateUpdate(transitionResult.state) ? transitionResult.state : undefined;
653
+ const target = update === undefined ? transitionResult.state : undefined;
654
+ // The new value of the source state, when this microstep keeps it active.
655
+ let value = noIndexedValue;
656
+ if (update !== undefined) {
657
+ const node = descriptor.nodes[sourceIndex];
658
+ if (update.path !== sourcePath || node.schema === undefined) {
659
+ throw new Error(`Machine state update owner "${update.path}" must be the active valued state`);
660
+ }
661
+ value = decodeStateValueSync(machine, node, update.value);
577
662
  }
578
- else {
579
- next = normalizeIndexedTargetStateSync(machine, descriptor, current, target, sourceIndex);
663
+ validateDeclaredTransitionTarget(sourcePath, { type: "event", event: event._tag }, transition.targets, target);
664
+ if (target !== undefined && !isTarget(target) && !isSnapshot(target)) {
665
+ throw new Error("Machine expected indexed transition target to be a snapshot or target builder result");
580
666
  }
581
- }
582
- const changed = transition.reenter || !hasSameIndexedActive(current, next);
583
- const nextIndex = next.activeLeaves[0];
584
- if (nextIndex === undefined) {
585
- throw new Error("Machine expected an active indexed transition target");
586
- }
587
- const step = {
588
- next,
589
- event,
590
- ...(retainMicrosteps
591
- ? {
592
- transitions: [{
593
- source: sourcePath,
594
- trigger: { type: "event", event: event._tag },
595
- reenter: transition.reenter,
596
- branchIndex: transitionResult.branchIndex,
597
- branchKey: transitionResult.branchKey,
598
- target: target === undefined ? undefined : getTargetNodePath(target),
599
- resolvedTarget: target === undefined ? undefined : getTargetNodePath(target),
600
- updates: update === undefined ? [] : [update.path]
601
- }]
667
+ let next = current;
668
+ if (target !== undefined) {
669
+ const targetIndex = descriptor.indexByPath.get(String(target.path));
670
+ const isSimpleTarget = isTarget(target)
671
+ ? target[TargetSnapshotTypeId] === undefined && target.values === undefined
672
+ : !("state" in target) && !("states" in target) && !("completed" in target) && !("history" in target);
673
+ if (targetIndex === sourceIndex && isSimpleTarget && current.completedOrder.length === 0 &&
674
+ descriptor.valued[sourceIndex]) {
675
+ value = decodeStateValueSync(machine, descriptor.nodes[sourceIndex], target.value);
602
676
  }
603
- : undefined),
604
- commands: transitionResult.commands,
605
- raisedEvents: transitionResult.raisedEvents,
606
- emittedEvents: transitionResult.emittedEvents,
607
- exitPaths: changed ? [sourcePath] : emptyExecutionValues,
608
- entryPaths: changed ? [descriptor.nodes[nextIndex].path] : emptyExecutionValues,
609
- changed
610
- };
611
- current = next;
612
- (microsteps ??= []).push(retainIndexedMicrostep(step, retainMicrosteps));
613
- if (transitionResult.commands.length > 0) {
614
- ;
615
- (commands ??= []).push(...transitionResult.commands);
677
+ else {
678
+ next = normalizeIndexedTargetStateSync(machine, descriptor, current, target, sourceIndex);
679
+ }
680
+ }
681
+ if (value !== noIndexedValue) {
682
+ if (!writable) {
683
+ current = withWritableIndexedValues(current);
684
+ writable = true;
685
+ }
686
+ else if (current === configuration && restoreIndex === -1) {
687
+ restoreIndex = sourceIndex;
688
+ restoreValue = configuration.values[sourceIndex];
689
+ }
690
+ current.values[sourceIndex] = value;
691
+ next = current;
692
+ }
693
+ const changed = transition.reenter || !hasSameIndexedActive(current, next);
694
+ const nextIndex = next.activeLeaves[0];
695
+ if (nextIndex === undefined) {
696
+ throw new Error("Machine expected an active indexed transition target");
697
+ }
698
+ const exitPaths = changed ? [sourcePath] : emptyExecutionValues;
699
+ const entryPaths = changed ? [descriptor.nodes[nextIndex].path] : emptyExecutionValues;
700
+ const step = retainMicrosteps
701
+ ? new RetainedFlatMicrostep(next, event, transitionResult.commands, transitionResult.raisedEvents, transitionResult.emittedEvents, exitPaths, entryPaths, changed, sourcePath, transition.reenter, transitionResult.branchIndex, transitionResult.branchKey, target, update)
702
+ : {
703
+ next,
704
+ event,
705
+ commands: transitionResult.commands,
706
+ raisedEvents: transitionResult.raisedEvents,
707
+ emittedEvents: transitionResult.emittedEvents,
708
+ exitPaths,
709
+ entryPaths,
710
+ changed
711
+ };
712
+ // A state that this microstep created is referenced by nothing else,
713
+ // unless the microstep is retained.
714
+ if (next !== current)
715
+ writable = true;
716
+ current = next;
717
+ if (retainMicrosteps)
718
+ writable = false;
719
+ (microsteps ??= []).push(step);
720
+ if (transitionResult.commands.length > 0) {
721
+ ;
722
+ (commands ??= []).push(...transitionResult.commands);
723
+ }
724
+ if (transitionResult.raisedEvents.length > 0) {
725
+ ;
726
+ (raisedEvents ??= []).push(...transitionResult.raisedEvents);
727
+ }
728
+ if (transitionResult.emittedEvents.length > 0) {
729
+ ;
730
+ (emittedEvents ??= []).push(...transitionResult.emittedEvents);
731
+ }
616
732
  }
617
- if (transitionResult.raisedEvents.length > 0) {
618
- ;
619
- (raisedEvents ??= []).push(...transitionResult.raisedEvents);
733
+ if (descriptor.nodes[current.activeLeaves[0]].type === "final") {
734
+ continue;
620
735
  }
621
- if (transitionResult.emittedEvents.length > 0) {
622
- ;
623
- (emittedEvents ??= []).push(...transitionResult.emittedEvents);
736
+ const raised = raisedEvents?.[raisedIndex];
737
+ if (raised === undefined) {
738
+ return {
739
+ event: decoded,
740
+ next: current,
741
+ commands: commands ?? emptyExecutionValues,
742
+ emittedEvents: emittedEvents ?? emptyExecutionValues,
743
+ microsteps: microsteps ?? emptyExecutionValues,
744
+ done: false,
745
+ output: undefined
746
+ };
624
747
  }
748
+ raisedIndex += 1;
749
+ event = raised;
625
750
  }
626
- if (descriptor.nodes[current.activeLeaves[0]].type === "final") {
627
- continue;
628
- }
629
- const raised = raisedEvents?.[raisedIndex];
630
- if (raised === undefined) {
631
- return {
632
- event: decoded,
633
- next: current,
634
- commands: commands ?? emptyExecutionValues,
635
- emittedEvents: emittedEvents ?? emptyExecutionValues,
636
- microsteps: microsteps ?? emptyExecutionValues,
637
- done: false,
638
- output: undefined
639
- };
640
- }
641
- raisedIndex += 1;
642
- event = raised;
751
+ }
752
+ catch (error) {
753
+ if (restoreIndex !== -1)
754
+ configuration.values[restoreIndex] = restoreValue;
755
+ throw error;
643
756
  }
644
757
  };
645
- const planIndexedState = (machine, descriptor, configuration, input, retainMicrosteps, machineReferences) => {
758
+ const planIndexedState = (machine, descriptor, configuration, input, decode, retainMicrosteps, ownership, machineReferences) => {
646
759
  if (InvocationEvent.isInvocationEvent(input)) {
647
760
  // Invocation lifecycle transitions retain the generic planner as their
648
761
  // semantic reference. Ordinary machine events and initialization stay on
@@ -653,11 +766,11 @@ const planIndexedState = (machine, descriptor, configuration, input, retainMicro
653
766
  : withMachineReferences(activeConfigurationFromIndexedState(descriptor, configuration), machineReferences), input);
654
767
  return {
655
768
  event: planned.event,
656
- next: ownedIndexedStateFromActive(descriptor, planned.next),
769
+ next: indexedStateFromActive(descriptor, planned.next),
657
770
  commands: planned.commands,
658
771
  emittedEvents: planned.emittedEvents,
659
772
  microsteps: planned.microsteps.map((step) => ({
660
- next: ownedIndexedStateFromActive(descriptor, step.next),
773
+ next: indexedStateFromActive(descriptor, step.next),
661
774
  event: step.event,
662
775
  transitions: step.transitions,
663
776
  commands: step.commands,
@@ -671,10 +784,12 @@ const planIndexedState = (machine, descriptor, configuration, input, retainMicro
671
784
  output: planned.output
672
785
  };
673
786
  }
674
- const decoded = decodeEventSync(machine, input);
787
+ const decoded = decode ? decodeEventSync(machine, input) : input;
675
788
  if (descriptor.flat) {
676
- return planIndexedFlatState(machine, descriptor, configuration, decoded, retainMicrosteps, machineReferences);
789
+ return planIndexedFlatState(machine, descriptor, configuration, decoded, retainMicrosteps, ownership, machineReferences);
677
790
  }
791
+ // The hierarchical kernel never writes a state in place, so it plans every
792
+ // ownership alike.
678
793
  if (descriptor.finalIndices.some((index) => configuration.active[index] === 1)) {
679
794
  const active = activeConfigurationFromIndexedState(descriptor, configuration);
680
795
  if (isActiveFinalConfiguration(machine, active)) {
@@ -685,7 +800,7 @@ const planIndexedState = (machine, descriptor, configuration, input, retainMicro
685
800
  }
686
801
  return {
687
802
  event: decoded,
688
- next: ownedIndexedStateFromActive(descriptor, completed),
803
+ next: indexedStateFromActive(descriptor, completed),
689
804
  commands: [],
690
805
  emittedEvents: [],
691
806
  microsteps: [],
@@ -712,7 +827,7 @@ const planIndexedState = (machine, descriptor, configuration, input, retainMicro
712
827
  const commands = [...first.commands];
713
828
  const raisedEvents = [...first.raisedEvents];
714
829
  const emittedEvents = [...first.emittedEvents];
715
- const microsteps = [retainIndexedMicrostep(first, retainMicrosteps)];
830
+ const microsteps = [first];
716
831
  let raisedIndex = 0;
717
832
  let iterations = 0;
718
833
  while (true) {
@@ -726,7 +841,7 @@ const planIndexedState = (machine, descriptor, configuration, input, retainMicro
726
841
  }
727
842
  if (descriptor.finalIndices.some((index) => current.active[index] === 1)) {
728
843
  const completed = completeConfigurationSync(machine, activeConfigurationFromIndexedState(descriptor, current), currentEvent).configuration;
729
- current = ownedIndexedStateFromActive(descriptor, completed);
844
+ current = indexedStateFromActive(descriptor, completed);
730
845
  if (isActiveFinalConfiguration(machine, completed)) {
731
846
  const root = getRootPath(machine, completed);
732
847
  if (!completed.outputs.has(root)) {
@@ -765,23 +880,29 @@ const planIndexedState = (machine, descriptor, configuration, input, retainMicro
765
880
  commands.push(...step.commands);
766
881
  raisedEvents.push(...step.raisedEvents);
767
882
  emittedEvents.push(...step.emittedEvents);
768
- microsteps.push(retainIndexedMicrostep(step, retainMicrosteps));
883
+ microsteps.push(step);
769
884
  }
770
885
  };
771
886
  const executionPlanCache = new WeakMap();
887
+ const referenceExecutionPlanCache = new WeakMap();
772
888
  const makeActiveExecutionPlan = (machine) => ({
773
889
  fromConfiguration: (configuration) => configuration,
774
890
  toConfiguration: (state) => state,
775
891
  snapshot: (state) => snapshotFromConfiguration(machine, state),
776
- plan: (state, event, _retainMicrosteps, machineReferences) => planConfiguration(machine, machineReferences === undefined
892
+ // The reference planner never writes a configuration in place.
893
+ plan: (state, event, _ownership, _retainMicrosteps, machineReferences) => planConfiguration(machine, machineReferences === undefined
894
+ ? state
895
+ : withMachineReferences(state, machineReferences), event),
896
+ planDecoded: (state, event, _ownership, _retainMicrosteps, machineReferences) => planDecodedConfiguration(machine, machineReferences === undefined
777
897
  ? state
778
898
  : withMachineReferences(state, machineReferences), event)
779
899
  });
780
900
  const makeIndexedExecutionPlan = (machine, indexed) => ({
781
- fromConfiguration: (configuration) => ownedIndexedStateFromActive(indexed, configuration),
901
+ fromConfiguration: (configuration) => indexedStateFromActive(indexed, configuration),
782
902
  toConfiguration: (state) => activeConfigurationFromIndexedState(indexed, state),
783
903
  snapshot: (state) => snapshotFromIndexedState(indexed, state),
784
- plan: (state, event, retainMicrosteps = false, machineReferences) => planIndexedState(machine, indexed, state, event, retainMicrosteps, machineReferences),
904
+ plan: (state, event, ownership, retainMicrosteps, machineReferences) => planIndexedState(machine, indexed, state, event, true, retainMicrosteps, ownership, machineReferences),
905
+ planDecoded: (state, event, ownership, retainMicrosteps, machineReferences) => planIndexedState(machine, indexed, state, event, false, retainMicrosteps, ownership, machineReferences),
785
906
  // Only captured data constructors can use indexed startup. Unknown initializer
786
907
  // protocols retain generic startup so commands and emissions cannot be dropped.
787
908
  ...(Object.values(toImpl(machine).handlers).some((config) => config.initialize !== undefined && !isDataInitializer(config.initialize)) ?
@@ -807,7 +928,7 @@ const makeIndexedExecutionPlan = (machine, indexed) => ({
807
928
  ? normalized
808
929
  : withMachineReferences(normalized, machineReferences);
809
930
  const completed = completeConfigurationSync(machine, active, InitialEvent).configuration;
810
- const configuration = ownedIndexedStateFromActive(indexed, completed);
931
+ const configuration = indexedStateFromActive(indexed, completed);
811
932
  const state = snapshotFromIndexedState(indexed, configuration);
812
933
  const done = isActiveFinalConfiguration(machine, completed);
813
934
  if (!done) {
@@ -835,25 +956,23 @@ const makeIndexedExecutionPlan = (machine, indexed) => ({
835
956
  }
836
957
  })
837
958
  });
838
- const selectExecutionPlan = (machine, strategy) => {
839
- if (strategy === "generic") {
840
- return { strategy, plan: makeActiveExecutionPlan(machine) };
841
- }
842
- const indexed = compileIndexedExecutionDescriptor(machine);
843
- if (indexed === undefined) {
844
- if (strategy === "auto") {
845
- return { strategy: "generic", plan: makeActiveExecutionPlan(machine) };
846
- }
847
- throw new Error(`Machine cannot compile the requested ${strategy} execution plan`);
848
- }
849
- const selected = indexed.flat ? "indexed-flat" : "indexed-hierarchical";
850
- if (strategy !== "auto" && strategy !== selected) {
851
- throw new Error(`Machine compiled ${selected}, not the requested ${strategy} execution plan`);
959
+ const executionPlanStrategyCache = new WeakMap();
960
+ /**
961
+ * The reference execution plan of `machine`, which always plans on the generic
962
+ * planner without indexed compilation. Cached per machine.
963
+ *
964
+ * @internal
965
+ */
966
+ export const referenceExecutionPlan = (machine) => {
967
+ const cached = referenceExecutionPlanCache.get(machine);
968
+ if (cached !== undefined) {
969
+ return cached;
852
970
  }
853
- return { strategy: selected, plan: makeIndexedExecutionPlan(machine, indexed) };
971
+ const reference = makeActiveExecutionPlan(machine);
972
+ referenceExecutionPlanCache.set(machine, reference);
973
+ executionPlanStrategyCache.set(reference, "generic");
974
+ return reference;
854
975
  };
855
- /** @internal Test-only uncached strategy selection. */
856
- export const selectExecutionPlanForTesting = (machine, strategy) => selectExecutionPlan(machine, strategy);
857
976
  export const compileExecutionPlan = (machine) => {
858
977
  const cached = executionPlanCache.get(machine);
859
978
  if (cached !== undefined) {
@@ -861,9 +980,27 @@ export const compileExecutionPlan = (machine) => {
861
980
  }
862
981
  const indexed = compileIndexedExecutionDescriptor(machine);
863
982
  const compiled = indexed === undefined
864
- ? makeActiveExecutionPlan(machine)
983
+ ? referenceExecutionPlan(machine)
865
984
  : makeIndexedExecutionPlan(machine, indexed);
985
+ if (indexed !== undefined) {
986
+ executionPlanStrategyCache.set(compiled, indexed.flat ? "indexed-flat" : "indexed-hierarchical");
987
+ }
866
988
  executionPlanCache.set(machine, compiled);
867
989
  return compiled;
868
990
  };
991
+ /** @internal Test-only planner selection, delegating to the cached reference and compiled plans. */
992
+ export const selectExecutionPlanForTesting = (machine, strategy) => {
993
+ if (strategy === "generic") {
994
+ return { strategy, plan: referenceExecutionPlan(machine) };
995
+ }
996
+ const plan = compileExecutionPlan(machine);
997
+ const selected = executionPlanStrategyCache.get(plan) ?? "generic";
998
+ if (strategy !== "auto" && strategy !== selected) {
999
+ if (selected === "generic") {
1000
+ throw new Error(`Machine cannot compile the requested ${strategy} execution plan`);
1001
+ }
1002
+ throw new Error(`Machine compiled ${selected}, not the requested ${strategy} execution plan`);
1003
+ }
1004
+ return { strategy: selected, plan };
1005
+ };
869
1006
  //# sourceMappingURL=executionPlan.js.map