@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
package/src/Machine.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  /**
2
- * Schema-first machine definitions.
2
+ * Schema-first machine models.
3
+ *
4
+ * This module declares machines, plans their steps, encodes their snapshots, and
5
+ * defines the contracts their types mention. It runs nothing: a host executes a
6
+ * machine, such as `LocalMachine` in memory or `ClusterMachine` as a Cluster
7
+ * entity.
3
8
  *
4
9
  * @since 0.4.0
5
10
  */
@@ -15,7 +20,8 @@ import type * as Schema from "effect/Schema"
15
20
  import type * as Scope from "effect/Scope"
16
21
  import type * as Stream from "effect/Stream"
17
22
  import type * as Types from "effect/Types"
18
- import type * as Activities from "./internal/machine/activities.js"
23
+ import type { ChildMachineRuntime, MachineRuntime } from "./internal/machine/capability.js"
24
+ import type * as InvocationDefinitions from "./internal/machine/invocationDefinitions.js"
19
25
  import type {
20
26
  ChildAlreadyExistsError,
21
27
  InfiniteTransitionError,
@@ -25,10 +31,8 @@ import type {
25
31
  StoppedError
26
32
  } from "./internal/machine/machine.js"
27
33
  import * as internal from "./internal/machine/machine.js"
28
- import { InitialEventTypeId } from "./internal/machine/machine.js"
34
+ import { InitialEventTypeId, InvocationEventTypeId } from "./internal/machine/machine.js"
29
35
  import type { EnsureExecutable } from "./internal/machine/readiness.js"
30
- import type { ExcludeCompatibleRuntime } from "./internal/machine/requirements.js"
31
- import type { MachineRuntime } from "./internal/machine/runtimeProtocol.js"
32
36
  import type * as StateDefinition from "./internal/machine/stateDefinition.js"
33
37
  import type * as Topology from "./internal/machine/topology.js"
34
38
 
@@ -56,12 +60,14 @@ export const TypeId: TypeId = "~effect/Machine"
56
60
  // interface carries the string key as a brand only.
57
61
  declare const MachineOutputStatesTypeId: "~effect/Machine/OutputStates"
58
62
  declare const MachineTypeId: "~effect/Machine/TypeCarrier"
63
+ declare const MachineEffectResultsTypeId: "~effect/Machine/EffectResults"
59
64
  declare const EventConstructionTypeId: "~effect/Machine/EventConstruction"
60
65
  declare const EmittedEventConstructionTypeId: "~effect/Machine/EmittedEventConstruction"
61
66
  declare const EventProtocolTypeId: "~effect/Machine/EventProtocol"
62
67
  declare const ParentModeTypeId: "~effect/Machine/ParentMode"
63
- declare const ChildMachineLogicTypeId: "~effect/Machine/ChildMachineLogic"
64
68
  declare const InvokeTypeId: "~effect/Machine/Invoke"
69
+ declare const InvocationSupportTypeId: "~effect/Machine/InvocationSupport"
70
+ declare const MachineStepStateTypeId: "~effect/Machine/MachineStep/State"
65
71
  declare const StateConstructionTypeId: "~effect/Machine/StateConstruction"
66
72
  declare const TargetSnapshotTypeId: "~effect/Machine/TargetSnapshot"
67
73
  declare const HistoryTargetTypeId: "~effect/Machine/HistoryTarget"
@@ -111,6 +117,56 @@ export const InitialEvent: InitialEvent = { _tag: InitialEventTypeId }
111
117
  */
112
118
  export const isInitialEvent = (u: unknown): u is InitialEvent => hasProperty(u, "_tag") && u._tag === InitialEventTypeId
113
119
 
120
+ /**
121
+ * Type identifier used for the synthetic event of a step caused by invoked work.
122
+ *
123
+ * @category type IDs
124
+ * @since 0.48.0
125
+ */
126
+ export { InvocationEventTypeId }
127
+
128
+ /**
129
+ * Synthetic event passed to lifecycle callbacks of a step that invoked work
130
+ * caused: its output, typed failure, stream element, or child snapshot.
131
+ *
132
+ * **Details**
133
+ *
134
+ * An outcome is not an event of the machine's protocol, so the states that its
135
+ * transition enters and exits, and the work they start, receive this marker as
136
+ * their `event`, as they receive `InitialEvent` while the initial state
137
+ * settles. `path` and `id` name the state that invoked the work and the
138
+ * invocation, and `kind` names what the work reported. The reported value
139
+ * itself is available to the transition that handles it, as `output`,
140
+ * `error`, `element`, or `snapshot`.
141
+ *
142
+ * **Gotchas**
143
+ *
144
+ * A host that restarts work after a recovery, such as `ClusterMachine`, passes
145
+ * `InitialEvent` to the input mapper of the restarted work, not this event.
146
+ *
147
+ * @category models
148
+ * @since 0.48.0
149
+ */
150
+ export interface InvocationEvent {
151
+ readonly _tag: typeof InvocationEventTypeId
152
+ /** Path of the state that invoked the work. */
153
+ readonly path: string
154
+ /** Id of the invocation within that state. */
155
+ readonly id: string
156
+ /** What the work reported. */
157
+ readonly kind: "Done" | "Failure" | "Element" | "Snapshot"
158
+ }
159
+
160
+ /**
161
+ * Returns `true` if a value is the synthetic event of a step caused by invoked
162
+ * work.
163
+ *
164
+ * @category guards
165
+ * @since 0.48.0
166
+ */
167
+ export const isInvocationEvent = (u: unknown): u is InvocationEvent =>
168
+ hasProperty(u, "_tag") && u._tag === InvocationEventTypeId
169
+
114
170
  type IsAny<A> = 0 extends (1 & A) ? true : false
115
171
 
116
172
  /**
@@ -171,8 +227,6 @@ export interface Machine<
171
227
  UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
172
228
  E = never,
173
229
  R = never,
174
- InitialE = never,
175
- InitialR = never,
176
230
  FinalStates extends Machine.StateIdentifier<States> = never,
177
231
  Output = never,
178
232
  Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
@@ -189,8 +243,6 @@ export interface Machine<
189
243
  UnhandledStates,
190
244
  E,
191
245
  R,
192
- InitialE,
193
- InitialR,
194
246
  FinalStates,
195
247
  Output,
196
248
  Emits,
@@ -269,7 +321,7 @@ export interface Machine<
269
321
  readonly handlers: Readonly<Record<string, unknown>>
270
322
 
271
323
  /** @internal */
272
- readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>
324
+ readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States>
273
325
  /** @internal */
274
326
  readonly initialDefinition: Machine.InitialDefinition
275
327
  }
@@ -292,8 +344,6 @@ export interface Definition<
292
344
  States extends Machine.StateSchemas,
293
345
  Events extends ReadonlyArray<Machine.TaggedSchema>,
294
346
  Input extends Schema.Top = typeof Schema.Void,
295
- InitialE = never,
296
- InitialR = never,
297
347
  FinalStates extends Machine.StateIdentifier<States> = never,
298
348
  Output = never,
299
349
  Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
@@ -309,8 +359,6 @@ export interface Definition<
309
359
  Machine.StateIdentifier<States>,
310
360
  never,
311
361
  never,
312
- InitialE,
313
- InitialR,
314
362
  FinalStates,
315
363
  Output,
316
364
  Emits,
@@ -357,8 +405,6 @@ export interface Definition<
357
405
  Events,
358
406
  Emits,
359
407
  Input,
360
- InitialE,
361
- InitialR,
362
408
  FinalStates,
363
409
  Output,
364
410
  InputEvents,
@@ -419,8 +465,17 @@ export {
419
465
  */
420
466
  MachineSchemaEncodeError,
421
467
  /**
422
- * Error returned when standalone action execution attempts an operation that
423
- * requires a managed machine process.
468
+ * Error raised when invoked work uses an operation that requires a local
469
+ * machine process, on a host that does not provide one.
470
+ *
471
+ * **Details**
472
+ *
473
+ * `ClusterMachine` raises it when work reads `self.sessionId`, calls
474
+ * `self.send`, or calls an operation of `children`. The step functions of
475
+ * {@link toStep} raise it when work calls an operation on children and the
476
+ * host passed no `children`. It is a defect rather than a typed failure,
477
+ * because the same work runs correctly on a local machine.
478
+ * `operation` names the refused operation.
424
479
  *
425
480
  * @category errors
426
481
  * @since 0.4.0
@@ -446,8 +501,38 @@ export {
446
501
 
447
502
  export type {
448
503
  /**
449
- * Service required by operations that act on the machine executing them,
450
- * such as `sendTo`, `stopChild`, and `spawn`.
504
+ * Capability to act on the child machines that the states of the running
505
+ * machine invoke, required by `sendTo` and `stopChild` with the descriptor of
506
+ * a child machine.
507
+ *
508
+ * **When to use**
509
+ *
510
+ * Name this requirement when annotating a reusable helper built from those
511
+ * operations, or when exporting a value whose inferred type includes it.
512
+ *
513
+ * **Details**
514
+ *
515
+ * The model declares this capability, and a host provides it to the work it
516
+ * runs. `LocalMachine` and `ClusterMachine` provide it, also when `AtomMachine`
517
+ * hosts the machine. Unlike {@link MachineRuntime}, it starts no process, so a
518
+ * host that runs child machines provides it.
519
+ *
520
+ * `Machine.Machine.Services` contains it when the work that a state invokes
521
+ * requires it, or when the machine of an invoked child does.
522
+ * `LocalMachine.RequiredServices` removes it.
523
+ *
524
+ * **Gotchas**
525
+ *
526
+ * Only the type is public: hosts of this library provide the service, and it
527
+ * cannot be constructed elsewhere.
528
+ *
529
+ * @category services
530
+ * @since 0.48.0
531
+ */
532
+ ChildMachineRuntime,
533
+ /**
534
+ * Capability to act on the processes of the running machine, required by
535
+ * `spawn`, and by `sendTo` and `stopChild` with a child address.
451
536
  *
452
537
  * **When to use**
453
538
  *
@@ -456,108 +541,103 @@ export type {
456
541
  *
457
542
  * **Details**
458
543
  *
459
- * A running machine provides this service to the effects it executes, so it
460
- * never appears in the requirements of the machine itself. Only the type is
461
- * public: the service cannot be constructed or provided outside a running
462
- * machine.
544
+ * The model declares this capability, and a host provides it to the work it
545
+ * runs. `LocalMachine` provides it, also when `AtomMachine` hosts the machine.
546
+ *
547
+ * `Machine.Machine.Services` contains it when the Effect, Stream, or `Logic` that
548
+ * a state invokes requires it, or when the machine of an invoked child does. A
549
+ * host that provides it runs every process that the work spawns, whatever that
550
+ * process invokes. `LocalMachine.RequiredServices` removes it.
551
+ *
552
+ * **Gotchas**
553
+ *
554
+ * `ClusterMachine` does not provide it, so `ClusterMachine.make` does not compile
555
+ * for a machine whose services contain it.
556
+ *
557
+ * Only the type is public: hosts of this library provide the service, and it
558
+ * cannot be constructed elsewhere.
463
559
  *
464
560
  * @category services
465
561
  * @since 0.47.0
466
562
  */
467
563
  MachineRuntime
468
- } from "./internal/machine/runtimeProtocol.js"
469
-
470
- const RuntimeRequirementTypeId = "~effect/Machine/RuntimeRequirement"
471
- const ActionRequirementTypeId = "~effect/Machine/ActionRequirement"
564
+ } from "./internal/machine/capability.js"
472
565
 
473
566
  /**
474
- * Opaque marker used to keep staged action errors and services separate from
475
- * the Effect that plans a machine step.
567
+ * Requirement that a machine runs on a host which supports one kind of
568
+ * invocation.
476
569
  *
477
- * @category services
478
- * @since 0.4.0
479
- */
480
- export interface ActionRequirement<Error, Requirements> {
481
- readonly [ActionRequirementTypeId]: {
482
- readonly error: Types.Covariant<Error>
483
- readonly requirements: Types.Covariant<Requirements>
484
- }
485
- }
486
-
487
- /**
488
- * Extracts the typed error channel of staged machine actions.
570
+ * **When to use**
489
571
  *
490
- * @category utility types
491
- * @since 0.4.0
492
- */
493
- export type ActionError<Requirements> = Requirements extends ActionRequirement<infer Error, any> ? Error : never
494
-
495
- /**
496
- * Extracts the service requirements of staged machine actions.
572
+ * Name this requirement when annotating a machine that invokes a Stream, a
573
+ * `Logic`, or a child machine, or when a host states in its types which
574
+ * invocations it runs.
497
575
  *
498
- * @category utility types
499
- * @since 0.4.0
500
- */
501
- export type ActionServices<Requirements> = Requirements extends ActionRequirement<any, infer Services> ? Services
502
- : never
503
-
504
- /**
505
- * Removes staged action requirements from machine planning services.
576
+ * **Details**
506
577
  *
507
- * @category utility types
508
- * @since 0.4.0
509
- */
510
- export type PlanningServices<Requirements> = Exclude<Requirements, ActionRequirement<any, any>>
511
-
512
- /**
513
- * Resolves all services needed to execute a machine at runtime.
578
+ * `Machine.Machine.Services` contains one for each kind that the states of a
579
+ * machine invoke. Registering a source without invoking it adds nothing. A
580
+ * machine that invokes a child machine also carries the requirements of that
581
+ * child.
582
+ *
583
+ * Every host runs Effects and timers, so no requirement stands for them.
584
+ * `LocalMachine` supports every kind, also when `AtomMachine` hosts the machine,
585
+ * and `LocalMachine.RequiredServices` removes them all. `ClusterMachine` supports
586
+ * child machines, which it runs as entities of their own, and
587
+ * `ClusterMachine.make` does not compile for a machine that invokes a Stream or
588
+ * a `Logic`, whose progress it cannot store.
589
+ *
590
+ * **Gotchas**
591
+ *
592
+ * This exists in types only. Nothing constructs it, and it is not provided as a
593
+ * service.
514
594
  *
515
595
  * @category utility types
516
- * @since 0.4.0
596
+ * @since 0.48.0
517
597
  */
518
- export type ExecutionServices<Requirements> =
519
- | Exclude<PlanningServices<Requirements>, MachineRuntime>
520
- | Exclude<ActionServices<Requirements>, MachineRuntime>
598
+ export interface InvocationSupport<out Kind extends InvocationSupport.Kind> {
599
+ readonly [InvocationSupportTypeId]: Kind
600
+ }
521
601
 
522
602
  /**
523
- * Managed runtime capability used to deliver raised events and publish
524
- * emitted notifications.
603
+ * Namespace containing type-level members associated with
604
+ * {@link InvocationSupport}.
525
605
  *
526
- * @category models
527
- * @since 0.4.0
606
+ * @category utility types
607
+ * @since 0.48.0
528
608
  */
529
- export interface Runtime<in Events, in Emits> {
530
- /**
531
- * Queues an event for the current machine macrostep.
532
- *
533
- * @since 0.4.0
534
- */
535
- readonly raise: (event: Machine.EventInput<Events>) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>
536
-
609
+ export declare namespace InvocationSupport {
537
610
  /**
538
- * Publishes an ephemeral notification to the running machine's observers.
611
+ * Kinds of invocation that a host may not support.
539
612
  *
540
- * @since 0.4.0
613
+ * @category models
614
+ * @since 0.48.0
541
615
  */
542
- readonly emit: (
543
- event: Machine.EmittedEventInput<Emits>
544
- ) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>
616
+ export type Kind = "Stream" | "Logic" | "Child"
545
617
  }
546
618
 
547
619
  /**
548
620
  * Minimal typed target accepted by inter-machine send commands.
549
621
  *
550
- * `send` reports whether the event was accepted for delivery. It promises
551
- * nothing about when the target processes it; a local {@link MachineRef}
552
- * strengthens that guarantee.
622
+ * `send` reports whether the event was accepted for delivery. When the target
623
+ * processes it is up to the host that runs the target.
553
624
  *
554
625
  * @category models
555
626
  * @since 0.12.0
556
627
  */
557
628
  export interface MachineTarget<in Event> {
629
+ /** Id of the target, such as its machine definition id or child address. */
558
630
  readonly id: string
631
+
632
+ /** Unique identity of the running target. */
559
633
  readonly sessionId: string
560
- readonly send: (event: Event) => Effect.Effect<void, StoppedError>
634
+
635
+ /**
636
+ * Accepts an event for delivery. Fails with `StoppedError` once the target has
637
+ * terminated, and with `MachineSchemaDecodeError` when the target rejects an
638
+ * event that its schema refuses.
639
+ */
640
+ readonly send: (event: Event) => Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
561
641
  }
562
642
 
563
643
  /**
@@ -626,7 +706,7 @@ export type MachineReferences<
626
706
  * @category models
627
707
  * @since 0.4.0
628
708
  */
629
- export interface Enqueue<in Events, in Emits> {
709
+ export interface Commands<in Events, in Emits> {
630
710
  /** Raises an event inside the current macrostep. */
631
711
  readonly raise: (event: Machine.EventInput<Events>) => void
632
712
 
@@ -650,6 +730,8 @@ export interface Enqueue<in Events, in Emits> {
650
730
  /**
651
731
  * A closed machine command recorded by a synchronous transition.
652
732
  *
733
+ * Planning only records commands. The host that runs the machine executes them.
734
+ *
653
735
  * @category models
654
736
  * @since 0.4.0
655
737
  */
@@ -664,35 +746,6 @@ export type Command =
664
746
  readonly child: ChildMachine.Any | ChildAddress<never>
665
747
  }
666
748
 
667
- /**
668
- * Namespace containing type-level members associated with `Runtime`.
669
- *
670
- * @category models
671
- * @since 0.4.0
672
- */
673
- export declare namespace Runtime {
674
- /**
675
- * Opaque service requirement for a machine runtime capability.
676
- *
677
- * @category services
678
- * @since 0.4.0
679
- */
680
- export interface Requirement<Events, Emits> {
681
- readonly [RuntimeRequirementTypeId]: {
682
- readonly events: Events
683
- readonly emits: Emits
684
- }
685
- }
686
- }
687
-
688
- type IncompatibleRuntime<Requirements, Events, Emits> = Requirements extends Runtime.Requirement<
689
- infer RequiredEvents,
690
- infer RequiredEmits
691
- > ? IsAny<Requirements> extends true ? never
692
- : [RequiredEvents] extends [Events] ? [RequiredEmits] extends [Emits] ? never : Requirements
693
- : Requirements
694
- : never
695
-
696
749
  type StateDefinitionError<
697
750
  Message extends string,
698
751
  Path extends PropertyKey = never,
@@ -1148,7 +1201,8 @@ type SpawnResult<State, Event, Error, Requirements, Output, SpawnError, InitialE
1148
1201
  >
1149
1202
 
1150
1203
  /**
1151
- * Represents the active or terminal lifecycle state of a running machine.
1204
+ * Represents the active or terminal lifecycle state of a running machine, as its
1205
+ * host publishes it.
1152
1206
  *
1153
1207
  * **Details**
1154
1208
  *
@@ -1170,7 +1224,7 @@ export type RuntimeSnapshot<State, Error = never, Output = never> =
1170
1224
  readonly output: Output
1171
1225
  }
1172
1226
  | {
1173
- readonly status: "error"
1227
+ readonly status: "failed"
1174
1228
  readonly state: State
1175
1229
  readonly cause: Cause.Cause<Error>
1176
1230
  }
@@ -1179,190 +1233,6 @@ export type RuntimeSnapshot<State, Error = never, Output = never> =
1179
1233
  readonly state: State
1180
1234
  }
1181
1235
 
1182
- /**
1183
- * Live, root-scoped records describing one prepared machine tree.
1184
- *
1185
- * Inspection records deliberately erase machine-specific values to `unknown`:
1186
- * one stream may contain unrelated root, child-machine, and `Logic` protocols.
1187
- * The record structure remains a closed discriminated union, while typed
1188
- * application observation continues through `changes` and `emissions`.
1189
- * Records retain decoded local events and snapshots and are not themselves a
1190
- * stable JSON export format. Telemetry exporters must project process-local
1191
- * values into an explicit portable representation.
1192
- *
1193
- * @category models
1194
- * @since 0.13.0
1195
- */
1196
- export declare namespace Inspection {
1197
- /** Read-only identity of a local machine endpoint. */
1198
- export interface Endpoint {
1199
- readonly id: string
1200
- readonly sessionId: string
1201
- }
1202
-
1203
- /** Read-only identity of a runtime represented in the inspection tree. */
1204
- export interface Subject extends Endpoint {
1205
- readonly kind: "Machine" | "Logic"
1206
- }
1207
-
1208
- /** Causal origin of an inspected runtime. */
1209
- export type Origin =
1210
- | { readonly _tag: "Root" }
1211
- | {
1212
- readonly _tag: "Invoke"
1213
- readonly ownerPath: string
1214
- readonly invokeId: string
1215
- }
1216
- | {
1217
- readonly _tag: "Spawn"
1218
- readonly address: string | undefined
1219
- }
1220
-
1221
- /** Causal owner of a send or emission. */
1222
- export type Causation =
1223
- | { readonly _tag: "Initialization" }
1224
- | { readonly _tag: "Macrostep"; readonly macrostepId: number }
1225
- | { readonly _tag: "Activity"; readonly activitySessionId: string }
1226
-
1227
- /** Closed command projection safe for observation. */
1228
- export type Command =
1229
- | {
1230
- readonly _tag: "SendTo"
1231
- readonly target: Endpoint | { readonly id: string }
1232
- readonly event: unknown
1233
- }
1234
- | {
1235
- readonly _tag: "Stop"
1236
- readonly target: Endpoint | { readonly id: string }
1237
- }
1238
-
1239
- /** One statechart microstep in a committed macrostep. */
1240
- export interface Microstep {
1241
- readonly event: unknown
1242
- readonly transitions: ReadonlyArray<Machine.RetainedTransition>
1243
- readonly raisedEvents: ReadonlyArray<unknown>
1244
- readonly emittedEvents: ReadonlyArray<unknown>
1245
- readonly commands: ReadonlyArray<Command>
1246
- readonly exitPaths: ReadonlyArray<string>
1247
- readonly entryPaths: ReadonlyArray<string>
1248
- readonly changed: boolean
1249
- }
1250
-
1251
- /** Common ordering and identity fields for every record. */
1252
- export interface Base {
1253
- /** Total publication order within this prepared root. */
1254
- readonly sequence: number
1255
- /** Session id of the prepared root that owns this inspection stream. */
1256
- readonly rootSessionId: string
1257
- /** Runtime instance described by this record. */
1258
- readonly subject: Subject
1259
- }
1260
-
1261
- /** Announces allocation of a root or owned process identity. */
1262
- export interface Created extends Base {
1263
- readonly _tag: "Created"
1264
- readonly parent: Subject | undefined
1265
- readonly origin: Origin
1266
- /** Compiled statechart definition; absent for generic `Logic`. */
1267
- readonly definition: Machine.Any | undefined
1268
- }
1269
-
1270
- /** Announces successful initialization. */
1271
- export interface Initialized extends Base {
1272
- readonly _tag: "Initialized"
1273
- readonly snapshot: RuntimeSnapshot<unknown, unknown, unknown>
1274
- readonly initialEntryPaths: ReadonlyArray<string>
1275
- readonly microsteps: ReadonlyArray<Microstep>
1276
- }
1277
-
1278
- /** Announces failure before an initial runtime snapshot exists. */
1279
- export interface StartFailed extends Base {
1280
- readonly _tag: "StartFailed"
1281
- readonly cause: Cause.Cause<unknown>
1282
- }
1283
-
1284
- /** Announces acceptance of an event by a local mailbox. */
1285
- export interface EventSent extends Base {
1286
- readonly _tag: "EventSent"
1287
- readonly deliveryId: number
1288
- readonly source: Subject | undefined
1289
- readonly target: Endpoint
1290
- readonly event: unknown
1291
- readonly causedBy: Causation | undefined
1292
- }
1293
-
1294
- /** Announces complete processing of one statechart mailbox event. */
1295
- export interface EventProcessed extends Base {
1296
- readonly _tag: "EventProcessed"
1297
- readonly macrostepId: number
1298
- readonly deliveryId: number
1299
- readonly source: Subject | undefined
1300
- readonly event: unknown
1301
- readonly before: RuntimeSnapshot<unknown, unknown, unknown>
1302
- readonly after: RuntimeSnapshot<unknown, unknown, unknown>
1303
- readonly handled: boolean
1304
- readonly configurationChanged: boolean
1305
- readonly microsteps: ReadonlyArray<Microstep>
1306
- }
1307
-
1308
- /** Announces a direct state update made by generic `Logic`. */
1309
- export interface StateChanged extends Base {
1310
- readonly _tag: "StateChanged"
1311
- readonly before: unknown
1312
- readonly after: unknown
1313
- readonly causedByDeliveryId: number | undefined
1314
- }
1315
-
1316
- /** Announces actual publication on a machine's domain emission stream. */
1317
- export interface Emitted extends Base {
1318
- readonly _tag: "Emitted"
1319
- readonly emission: unknown
1320
- readonly causedBy: Causation | undefined
1321
- }
1322
-
1323
- /** Identity of one Effect or timer invocation run. */
1324
- export interface Activity {
1325
- readonly id: string
1326
- readonly sessionId: string
1327
- readonly owner: Subject
1328
- readonly ownerPath: string
1329
- readonly kind: "Effect" | "Stream" | "Timer"
1330
- }
1331
-
1332
- /** Announces an Effect, Stream, or timer invocation starting. */
1333
- export interface ActivityStarted extends Base {
1334
- readonly _tag: "ActivityStarted"
1335
- readonly activity: Activity
1336
- }
1337
-
1338
- /** Announces an Effect or timer invocation outcome. */
1339
- export interface ActivityStopped extends Base {
1340
- readonly _tag: "ActivityStopped"
1341
- readonly activity: Activity
1342
- /** Success or failure from the invoke; interruption means its owner stopped it. */
1343
- readonly exit: Exit.Exit<unknown, unknown>
1344
- }
1345
-
1346
- /** Announces a terminal local runtime snapshot. */
1347
- export interface Terminated extends Base {
1348
- readonly _tag: "Terminated"
1349
- readonly snapshot: RuntimeSnapshot<unknown, unknown, unknown>
1350
- }
1351
-
1352
- /** Complete live inspection protocol. */
1353
- export type Event =
1354
- | Created
1355
- | Initialized
1356
- | StartFailed
1357
- | EventSent
1358
- | EventProcessed
1359
- | StateChanged
1360
- | Emitted
1361
- | ActivityStarted
1362
- | ActivityStopped
1363
- | Terminated
1364
- }
1365
-
1366
1236
  /**
1367
1237
  * Represents a classified terminal outcome derived from a runtime snapshot.
1368
1238
  *
@@ -1379,87 +1249,46 @@ export type RuntimeOutcome<State, Error = never, Output = never> =
1379
1249
  readonly _tag: "Failure"
1380
1250
  readonly error: Error
1381
1251
  readonly cause: Cause.Cause<Error>
1382
- readonly snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "error" }>
1252
+ readonly snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "failed" }>
1383
1253
  }
1384
1254
  | {
1385
1255
  readonly _tag: "Defect"
1386
1256
  readonly defect: unknown
1387
1257
  readonly cause: Cause.Cause<Error>
1388
- readonly snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "error" }>
1258
+ readonly snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "failed" }>
1389
1259
  }
1390
1260
  | {
1391
1261
  readonly _tag: "Interrupted"
1392
1262
  readonly cause: Cause.Cause<Error>
1393
- readonly snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "error" }>
1263
+ readonly snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "failed" }>
1394
1264
  }
1395
1265
  | {
1396
1266
  readonly _tag: "Cause"
1397
1267
  readonly cause: Cause.Cause<Error>
1398
- readonly snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "error" }>
1268
+ readonly snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "failed" }>
1399
1269
  }
1400
1270
  | {
1401
1271
  readonly _tag: "Stopped"
1402
1272
  readonly snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "stopped" }>
1403
1273
  }
1404
1274
 
1405
- /**
1406
- * A fresh machine whose observable streams exist before initialization.
1407
- *
1408
- * Subscribe to `emissions` before evaluating `start` when initial-entry
1409
- * emissions must be observed. `start` is one-shot: concurrent and repeated
1410
- * evaluations share the same initialization and running reference, and the
1411
- * `Scope` of the first evaluation owns the machine.
1412
- *
1413
- * @category models
1414
- * @since 0.11.0
1415
- */
1416
- export interface Prepared<out State, in Event, out Error, out Output, out Emitted, out StartError, StartRequirements> {
1417
- /** Stable machine definition id, or a generated fallback when none was declared. */
1418
- readonly id: string
1419
-
1420
- /** Unique identity reserved for this prepared machine instance. */
1421
- readonly sessionId: string
1422
-
1423
- /** Waits for startup, then streams the initial lifecycle snapshot and later changes. */
1424
- readonly changes: Stream.Stream<RuntimeSnapshot<State, Error, Output>, StartError>
1425
-
1426
- /** Streams ephemeral notifications published after subscription. */
1427
- readonly emissions: Stream.Stream<Emitted>
1428
-
1429
- /**
1430
- * Streams ordered operational records for this root and every locally owned
1431
- * descendant. The stream is hot, non-replayed, never fails, and completes
1432
- * after the prepared root terminates. Subscribe before evaluating `start` to
1433
- * observe initialization.
1434
- */
1435
- readonly inspection: Stream.Stream<Inspection.Event>
1436
-
1437
- /** Initializes this machine once, owned by the first evaluating `Scope`, and returns its running reference. */
1438
- readonly start: Effect.Effect<MachineRef<State, Event, Error, Output, Emitted>, StartError, StartRequirements>
1439
- }
1440
-
1441
1275
  /**
1442
1276
  * Provides access to a running machine's state, lifecycle, event input, and
1443
1277
  * termination operations.
1444
1278
  *
1279
+ * **Details**
1280
+ *
1281
+ * This is the contract every host's reference satisfies. `send` reports whether
1282
+ * an event was accepted for delivery. Errors that occur while processing an
1283
+ * accepted event are observed through `snapshot`, `changes`, or `join`. Sending
1284
+ * after termination fails with `StoppedError`.
1285
+ *
1445
1286
  * **Gotchas**
1446
1287
  *
1447
- * `send` reports whether an event was accepted for delivery. A running
1448
- * machine reference is local, so an idle machine processes an accepted event
1449
- * in the sender's turn: work that completes without suspending is reflected
1450
- * by `snapshot` when `send` returns. Work invoked by an entered state also
1451
- * starts in the sender's turn and runs until it first suspends, so browser
1452
- * APIs that require user activation, such as `window.open`, can run in the
1453
- * first step of an invoked Effect. Work that suspends continues in the
1454
- * background, and its outcome is handled in a later turn, even when the work
1455
- * completes without suspending, so the entered state is always observed
1456
- * before the outcome leaves it. An event sent while the machine is processing
1457
- * is queued behind the current work. `changes` subscribers receive every committed
1458
- * snapshot in order once the sender yields, with consecutive sends delivered
1459
- * together. This guarantee belongs to `MachineRef`; a `MachineTarget` only
1460
- * promises delivery. Errors that occur while processing an accepted event are
1461
- * observed through `snapshot`, `changes`, or `join`. Sending after
1462
- * termination fails with `StoppedError`.
1288
+ * When an accepted event is processed is up to the host. Code written against
1289
+ * `MachineRef` cannot assume that `snapshot` reflects an event once `send`
1290
+ * returns. `LocalMachine.start` documents the delivery order that a reference to
1291
+ * a local machine adds to this contract.
1463
1292
  *
1464
1293
  * @category models
1465
1294
  * @since 0.4.0
@@ -1491,16 +1320,22 @@ export interface MachineRef<out State, in Event, out Error = never, out Output =
1491
1320
  /** Waits for machine output or fails when execution fails or is stopped. */
1492
1321
  readonly join: Effect.Effect<Output, Error | StoppedError>
1493
1322
 
1494
- /** Stops this machine instance and its owned child processes. */
1323
+ /** Stops this machine instance and the children it owns. */
1495
1324
  readonly stop: Effect.Effect<void>
1496
1325
 
1497
1326
  /**
1498
- * Accepts an event for processing by this local machine. An idle machine
1499
- * processes it in the sender's turn until the work first suspends, so
1500
- * `snapshot` reflects it when `send` returns. Work invoked by an entered
1501
- * state starts in the same turn; its outcome is handled in a later turn.
1327
+ * Accepts an event for delivery. When the machine processes it is up to its
1328
+ * host. An event that the schema of the machine refuses is rejected with
1329
+ * `MachineSchemaDecodeError`, and the machine keeps running; once the machine
1330
+ * has ended, `send` fails with `StoppedError`.
1331
+ *
1332
+ * A transition that throws follows one rule on every host: the error goes to
1333
+ * the sender when one waits for the event to be processed, and otherwise the
1334
+ * machine fails with it. A `send` of `LocalMachine` returns before its event
1335
+ * is processed, so the machine fails; a `ClusterMachine` client waits, so it
1336
+ * is rejected and the machine goes on.
1502
1337
  */
1503
- readonly send: (event: Event) => Effect.Effect<void, StoppedError>
1338
+ readonly send: (event: Event) => Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
1504
1339
 
1505
1340
  /**
1506
1341
  * Returns the current directly owned child for a typed descriptor.
@@ -1522,7 +1357,12 @@ export interface MachineRef<out State, in Event, out Error = never, out Output =
1522
1357
  }
1523
1358
 
1524
1359
  /**
1525
- * Machine-specific process logic used by `spawn` and state-owned invocations.
1360
+ * Stateful process logic used by `spawn` and state-owned invocations.
1361
+ *
1362
+ * Logic describes a process by what it needs from a host: the capabilities of
1363
+ * `Logic.InitialContext` and `Logic.RunContext`. `LocalMachine` runs it as a process owned by
1364
+ * the machine. `ClusterMachine.make` does not compile for a machine that invokes
1365
+ * it.
1526
1366
  *
1527
1367
  * @category models
1528
1368
  * @since 0.4.0
@@ -1536,26 +1376,26 @@ export interface Logic<
1536
1376
  out InitialError = never
1537
1377
  > {
1538
1378
  /** Creates the initial process state and may spawn scoped child processes. */
1539
- initial(scope: Logic.Scope<Event>): Effect.Effect<State, InitialError, Requirements>
1379
+ initial(context: Logic.InitialContext<Event>): Effect.Effect<State, InitialError, Requirements>
1540
1380
 
1541
1381
  /** Runs the stateful process loop until it produces output or fails. */
1542
- run(context: Logic.Context<State, Event>): Effect.Effect<Output, Error, Requirements>
1382
+ run(context: Logic.RunContext<State, Event>): Effect.Effect<Output, Error, Requirements>
1543
1383
  }
1544
1384
 
1545
1385
  /**
1546
- * Public types used by advanced machine process logic.
1386
+ * Capabilities a host gives to process logic.
1547
1387
  *
1548
1388
  * @category models
1549
1389
  * @since 0.4.0
1550
1390
  */
1551
1391
  export declare namespace Logic {
1552
1392
  /**
1553
- * Machine-local endpoint that can receive events and be stopped.
1393
+ * Endpoint of a process that can receive events and be stopped.
1554
1394
  *
1555
1395
  * @category models
1556
1396
  * @since 0.4.0
1557
1397
  */
1558
- export interface Address<in Event> {
1398
+ export interface Endpoint<in Event> {
1559
1399
  /** Parent-local address id. */
1560
1400
  readonly id: string
1561
1401
 
@@ -1565,8 +1405,12 @@ export declare namespace Logic {
1565
1405
  /** Stops the addressed child process. */
1566
1406
  readonly stop: Effect.Effect<void>
1567
1407
 
1568
- /** Sends an event to the addressed child process. */
1569
- readonly send: (event: Event) => Effect.Effect<void, StoppedError>
1408
+ /**
1409
+ * Sends an event to the addressed process. When the endpoint belongs to a
1410
+ * machine whose event schema refuses the event, fails with
1411
+ * {@link MachineSchemaDecodeError} and leaves the machine running.
1412
+ */
1413
+ readonly send: (event: Event) => Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
1570
1414
  }
1571
1415
 
1572
1416
  /**
@@ -1610,28 +1454,40 @@ export declare namespace Logic {
1610
1454
  }
1611
1455
 
1612
1456
  /**
1613
- * Machine-local capabilities available while process logic initializes.
1457
+ * Capabilities available while process logic initializes.
1614
1458
  *
1615
1459
  * @category models
1616
1460
  * @since 0.4.0
1617
1461
  */
1618
- export interface Scope<Event> {
1619
- /** Address of the process being initialized. */
1620
- readonly self: Address<Event>
1462
+ export interface InitialContext<Event> {
1463
+ /** Endpoint of the process being initialized. */
1464
+ readonly self: Endpoint<Event>
1621
1465
 
1622
- /** Address of the owning process, when one exists. */
1623
- readonly parent: Address<unknown> | undefined
1466
+ /** Endpoint of the owning process, when one exists. */
1467
+ readonly parent: Endpoint<unknown> | undefined
1624
1468
 
1625
- /** Starts a child process owned by this scope. */
1469
+ /** Starts a child process owned by this process. */
1626
1470
  readonly spawn: Spawn<Event>
1627
1471
 
1628
- /** Sends an event to a machine target or typed parent-local child address. */
1472
+ /**
1473
+ * Sends an event to a process endpoint, machine target, or typed
1474
+ * parent-local child address. When the addressed receiver is a machine
1475
+ * whose event schema refuses the event, fails with
1476
+ * {@link MachineSchemaDecodeError} and leaves the receiver running.
1477
+ */
1629
1478
  readonly sendTo: {
1630
- <TargetEvent>(target: MachineTarget<TargetEvent>, event: TargetEvent): Effect.Effect<void, StoppedError>
1479
+ <TargetEvent>(
1480
+ target: Endpoint<TargetEvent>,
1481
+ event: TargetEvent
1482
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
1483
+ <TargetEvent>(
1484
+ target: MachineTarget<TargetEvent>,
1485
+ event: TargetEvent
1486
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
1631
1487
  <Address extends ChildAddress<never>>(
1632
1488
  id: Address,
1633
1489
  event: ChildAddress.Event<Address>
1634
- ): Effect.Effect<void, StoppedError>
1490
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
1635
1491
  }
1636
1492
 
1637
1493
  /** Stops a child process selected by its parent-local address. */
@@ -1639,12 +1495,12 @@ export declare namespace Logic {
1639
1495
  }
1640
1496
 
1641
1497
  /**
1642
- * Machine-local capabilities available while stateful process logic runs.
1498
+ * Capabilities available while stateful process logic runs.
1643
1499
  *
1644
1500
  * @category models
1645
1501
  * @since 0.4.0
1646
1502
  */
1647
- export interface Context<State, Event> extends Scope<Event> {
1503
+ export interface RunContext<State, Event> extends InitialContext<Event> {
1648
1504
  /** Waits for the next event delivered to this process. */
1649
1505
  readonly receive: Effect.Effect<Event>
1650
1506
 
@@ -1672,8 +1528,8 @@ type InvokeLifecycleId = string & { readonly [ChildAddressTypeId]?: never }
1672
1528
  *
1673
1529
  * **Details**
1674
1530
  *
1675
- * The descriptor carries the child's address and complete machine type.
1676
- * Register it in `make({ children })` to invoke it by `src`, and pass a
1531
+ * The descriptor holds the child's id and machine definition, and nothing a host
1532
+ * needs to run the child: the host that starts it decides how. Register it in `make({ children })` to invoke it by `src`, and pass a
1677
1533
  * descriptor for the same id and machine to `sendTo` and child lookup APIs, so
1678
1534
  * state, event, error, and output types are inferred without separate
1679
1535
  * annotations.
@@ -1689,9 +1545,6 @@ export interface ChildMachine<Id extends string, M extends Machine.Any> {
1689
1545
 
1690
1546
  /** Complete machine definition carried by this descriptor. */
1691
1547
  readonly machine: M
1692
-
1693
- /** @internal Brand of descriptors built by `child` and `childFamily`. */
1694
- readonly [ChildMachineLogicTypeId]: typeof ChildMachineLogicTypeId
1695
1548
  }
1696
1549
 
1697
1550
  /**
@@ -1773,9 +1626,7 @@ export declare namespace ChildMachine {
1773
1626
  * @since 0.20.0
1774
1627
  */
1775
1628
  export type StartError<Child extends Any> = Child extends ChildMachine<string, infer M> ?
1776
- | Machine.InitialError<M>
1777
1629
  | Machine.Error<M>
1778
- | ActionError<Machine.InitialServices<M> | Machine.Services<M>>
1779
1630
  | InfiniteTransitionError
1780
1631
  | MachineSchemaDecodeError
1781
1632
  | StartupError
@@ -1783,18 +1634,20 @@ export declare namespace ChildMachine {
1783
1634
  : never
1784
1635
 
1785
1636
  /**
1786
- * Services needed to initialize a spawned child machine.
1637
+ * Services needed to initialize a spawned child machine, without
1638
+ * {@link MachineRuntime} and {@link InvocationSupport}.
1639
+ *
1640
+ * **Details**
1641
+ *
1642
+ * The process that spawns the child already runs on a host that spawns, and such
1643
+ * a host runs a child whatever that invokes.
1787
1644
  *
1788
1645
  * @category utility types
1789
1646
  * @since 0.20.0
1790
1647
  */
1791
1648
  export type StartRequirements<Child extends Any> = Child extends ChildMachine<string, infer M> ? Exclude<
1792
- ExcludeCompatibleRuntime<
1793
- Exclude<ExecutionServices<Machine.InitialServices<M> | Machine.Services<M>>, MachineRuntime>,
1794
- Machine.Event<M>,
1795
- Machine.EmittedEvent<M>
1796
- >,
1797
- Scope.Scope
1649
+ Machine.Services<M>,
1650
+ MachineRuntime | ChildMachineRuntime | InvocationSupport<InvocationSupport.Kind> | Scope.Scope
1798
1651
  >
1799
1652
  : never
1800
1653
 
@@ -1808,7 +1661,6 @@ export declare namespace ChildMachine {
1808
1661
  Machine.Snapshot<Machine.States<M>>,
1809
1662
  Machine.EventInput<Machine.InputEvent<M>>,
1810
1663
  | Machine.Error<M>
1811
- | ActionError<Machine.Services<M>>
1812
1664
  | InfiniteTransitionError
1813
1665
  | MachineSchemaDecodeError
1814
1666
  | StoppedError,
@@ -1828,8 +1680,12 @@ export declare namespace ChildMachine {
1828
1680
  }
1829
1681
 
1830
1682
  /**
1831
- * Effectful operations for child machines owned directly by the current
1832
- * machine process.
1683
+ * Effectful operations for child machines owned directly by the running
1684
+ * machine.
1685
+ *
1686
+ * Input mappers receive it from the host. `LocalMachine` runs each child as an
1687
+ * owned local machine. The owner that `ClusterMachine` supplies refuses every
1688
+ * operation with {@link ProcessLocalError}.
1833
1689
  *
1834
1690
  * @category models
1835
1691
  * @since 0.20.0
@@ -1845,18 +1701,22 @@ export interface ChildOwner<OwnerEvent> {
1845
1701
  ChildMachine.StartRequirements<Child>
1846
1702
  >
1847
1703
 
1848
- /** Sends an event to one active child. Missing children are ignored. */
1704
+ /**
1705
+ * Sends an event to one active child. Missing children are ignored. An event
1706
+ * that the child's event schema refuses fails with
1707
+ * {@link MachineSchemaDecodeError} and leaves the child running.
1708
+ */
1849
1709
  readonly sendTo: <Child extends ChildMachine.Any>(
1850
1710
  child: Child,
1851
1711
  event: ChildMachine.Event<Child>
1852
- ) => Effect.Effect<void, StoppedError>
1712
+ ) => Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
1853
1713
 
1854
1714
  /** Stops one active child. Missing children are ignored. */
1855
1715
  readonly stop: <Child extends ChildMachine.Any>(child: Child) => Effect.Effect<void>
1856
1716
  }
1857
1717
 
1858
1718
  /**
1859
- * Parent-local address for a child process that can receive events.
1719
+ * Typed address of a child, unique within the machine that owns it.
1860
1720
  *
1861
1721
  * @category models
1862
1722
  * @since 0.4.0
@@ -1927,6 +1787,7 @@ export declare namespace ChildAddress {
1927
1787
  * @since 0.4.0
1928
1788
  */
1929
1789
  export interface SpawnOptions {
1790
+ /** Address of the child, unique among the active children of its owner. */
1930
1791
  readonly id?: string
1931
1792
  }
1932
1793
 
@@ -1949,8 +1810,6 @@ export declare namespace Machine {
1949
1810
  UnhandledStates extends StateIdentifier<States>,
1950
1811
  E,
1951
1812
  R,
1952
- InitialE,
1953
- InitialR,
1954
1813
  FinalStates extends StateIdentifier<States>,
1955
1814
  Output,
1956
1815
  Emits extends ReadonlyArray<TaggedSchema>,
@@ -1964,8 +1823,6 @@ export declare namespace Machine {
1964
1823
  readonly unhandledStates: UnhandledStates
1965
1824
  readonly error: E
1966
1825
  readonly services: R
1967
- readonly initialError: InitialE
1968
- readonly initialServices: InitialR
1969
1826
  readonly finalStates: FinalStates
1970
1827
  readonly output: Output
1971
1828
  readonly emittedEvents: Emits
@@ -2032,7 +1889,7 @@ export declare namespace Machine {
2032
1889
  export interface Any extends Pipeable {
2033
1890
  readonly [TypeId]: TypeId
2034
1891
  /** @internal */
2035
- readonly [MachineTypeId]: TypeCarrier<any, any, any, any, any, any, any, any, any, any, any, any, any, any>
1892
+ readonly [MachineTypeId]: TypeCarrier<any, any, any, any, any, any, any, any, any, any, any, any>
2036
1893
  readonly states: StateSchemas
2037
1894
  readonly root: Pick<State<Machine.StateNodeConfig>, typeof StateTypeId | "node">
2038
1895
  readonly events: EventProtocol.Any<"public">
@@ -2106,28 +1963,18 @@ export declare namespace Machine {
2106
1963
  export type Error<M extends Any> = M[typeof MachineTypeId]["error"]
2107
1964
 
2108
1965
  /**
2109
- * Extracts runtime service requirements carried by a machine definition.
1966
+ * Extracts what a machine definition requires of the host that runs it.
2110
1967
  *
2111
- * @category utility types
2112
- * @since 0.4.0
2113
- */
2114
- export type Services<M extends Any> = M[typeof MachineTypeId]["services"]
2115
-
2116
- /**
2117
- * Extracts the startup error channel carried by a machine definition.
1968
+ * **Details**
2118
1969
  *
2119
- * @category utility types
2120
- * @since 0.4.0
2121
- */
2122
- export type InitialError<M extends Any> = M[typeof MachineTypeId]["initialError"]
2123
-
2124
- /**
2125
- * Extracts startup service requirements carried by a machine definition.
1970
+ * The requirements are the services of the work its states invoke,
1971
+ * {@link MachineRuntime} when that work uses it, and one
1972
+ * {@link InvocationSupport} for each invoked kind that not every host runs.
2126
1973
  *
2127
1974
  * @category utility types
2128
1975
  * @since 0.4.0
2129
1976
  */
2130
- export type InitialServices<M extends Any> = M[typeof MachineTypeId]["initialServices"]
1977
+ export type Services<M extends Any> = M[typeof MachineTypeId]["services"]
2131
1978
 
2132
1979
  /**
2133
1980
  * Extracts final state paths carried by a machine definition.
@@ -2172,6 +2019,61 @@ export declare namespace Machine {
2172
2019
  /** Extracts the public input protocol required from an owning machine. */
2173
2020
  export type ParentEvents<M extends Any> = M[typeof MachineTypeId]["parentEvents"]
2174
2021
 
2022
+ /**
2023
+ * The value an Effect source succeeds with and the typed error it fails with.
2024
+ *
2025
+ * @category utility types
2026
+ * @since 0.48.0
2027
+ */
2028
+ export interface EffectResult<Done, Failure> {
2029
+ readonly done: Done
2030
+ readonly failure: Failure
2031
+ }
2032
+
2033
+ /**
2034
+ * Extracts the result types of every Effect source a machine registers,
2035
+ * keyed by source name.
2036
+ *
2037
+ * **Details**
2038
+ *
2039
+ * The machine returned by `handle` carries these types, and only as types:
2040
+ * nothing is added to the machine value. A source that takes input
2041
+ * contributes the Effect its function returns.
2042
+ *
2043
+ * **Gotchas**
2044
+ *
2045
+ * A machine whose type was widened, for example by annotating it as a
2046
+ * `Machine.Machine<...>` or as `Machine.Machine.Any`, no longer carries them,
2047
+ * and this type is `never`.
2048
+ *
2049
+ * @category utility types
2050
+ * @since 0.48.0
2051
+ */
2052
+ export type EffectResults<M extends Any> = M extends {
2053
+ readonly [MachineEffectResultsTypeId]?: infer Results extends Readonly<Record<string, EffectResult<any, any>>>
2054
+ } ? Results
2055
+ : never
2056
+
2057
+ /**
2058
+ * Type-only evidence of the result types of a machine's Effect sources, read
2059
+ * through {@link EffectResults}.
2060
+ *
2061
+ * @category models
2062
+ * @since 0.48.0
2063
+ */
2064
+ export interface EffectResultsEvidence<Results> {
2065
+ /**
2066
+ * @internal Optional, so a machine type rebuilt from its parameters stays
2067
+ * assignable to and from the type that `handle` returned.
2068
+ */
2069
+ readonly [MachineEffectResultsTypeId]?: Results
2070
+ }
2071
+
2072
+ /** The result types of one registered Effect source. */
2073
+ export type EffectResultOf<Source> = InvokeResolvedSource<Source> extends
2074
+ Effect.Effect<infer Done, infer Failure, infer _R> ? EffectResult<Done, Failure>
2075
+ : never
2076
+
2175
2077
  /** Extracts whether a machine requires or optionally accepts an owner. */
2176
2078
  export type ParentAvailability<M extends Any> = ParentModeOf<M[typeof MachineTypeId]["parentEvents"]>
2177
2079
 
@@ -2923,7 +2825,9 @@ export declare namespace Machine {
2923
2825
  * @category models
2924
2826
  * @since 0.4.0
2925
2827
  */
2926
- export type ActivityDefinition<SourcePath extends string = string> = Activities.ActivityDefinition<SourcePath>
2828
+ export type InvocationDefinition<SourcePath extends string = string> = InvocationDefinitions.InvocationDefinition<
2829
+ SourcePath
2830
+ >
2927
2831
 
2928
2832
  /**
2929
2833
  * Transition retained after hierarchy precedence and conflict resolution for
@@ -3229,12 +3133,17 @@ export declare namespace Machine {
3229
3133
  export type EmittedEventOf<Emits extends ReadonlyArray<TaggedSchema>> = Emits[number]["Type"]
3230
3134
 
3231
3135
  /**
3232
- * Event values received by lifecycle callbacks.
3136
+ * Event values received by lifecycle callbacks: a declared event,
3137
+ * `InitialEvent` while the initial state settles, or `InvocationEvent` for a
3138
+ * step caused by invoked work.
3233
3139
  *
3234
3140
  * @category utility types
3235
3141
  * @since 0.4.0
3236
3142
  */
3237
- export type LifecycleEvent<Events extends ReadonlyArray<TaggedSchema>> = EventOf<Events> | InitialEvent
3143
+ export type LifecycleEvent<Events extends ReadonlyArray<TaggedSchema>> =
3144
+ | EventOf<Events>
3145
+ | InitialEvent
3146
+ | InvocationEvent
3238
3147
 
3239
3148
  /**
3240
3149
  * Extracts a state value from a state definition by identifier.
@@ -3811,7 +3720,7 @@ export declare namespace Machine {
3811
3720
  * value without changing its active descendants.
3812
3721
  *
3813
3722
  * @category models
3814
- * @since 0.21.0
3723
+ * @since 0.48.0
3815
3724
  */
3816
3725
  export interface StateUpdate<
3817
3726
  States extends StateSchemas,
@@ -3955,8 +3864,6 @@ export declare namespace Machine {
3955
3864
  Emits extends ReadonlyArray<TaggedSchema>,
3956
3865
  StateId extends StateIdentifier<States>,
3957
3866
  EventTag extends TagOf<Events[number]>,
3958
- E,
3959
- R,
3960
3867
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
3961
3868
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
3962
3869
  > = MachineReferences<InputEvents, ParentEvents> & {
@@ -4296,7 +4203,7 @@ export declare namespace Machine {
4296
4203
  * @category utility types
4297
4204
  * @since 0.4.0
4298
4205
  */
4299
- export type StateActionResult<E, R> = undefined
4206
+ export type StateActionResult = undefined
4300
4207
 
4301
4208
  /**
4302
4209
  * Initial target that a machine resolves at startup.
@@ -4304,7 +4211,7 @@ export declare namespace Machine {
4304
4211
  * @category utility types
4305
4212
  * @since 0.4.0
4306
4213
  */
4307
- export type InitialResult<States extends StateSchemas, E, R> = InitialTarget<StateIdentifier<States>>
4214
+ export type InitialResult<States extends StateSchemas> = InitialTarget<StateIdentifier<States>>
4308
4215
 
4309
4216
  /**
4310
4217
  * Result that a transition declaration resolves to.
@@ -4319,7 +4226,7 @@ export declare namespace Machine {
4319
4226
  * @category utility types
4320
4227
  * @since 0.4.0
4321
4228
  */
4322
- export type HandlerResult<States extends StateSchemas, E, R> =
4229
+ export type HandlerResult<States extends StateSchemas> =
4323
4230
  | Snapshot<States>
4324
4231
  | Target<States, StateIdentifier<States>>
4325
4232
  | HistoryTarget<States, HistoryIdentifier<States>>
@@ -4356,45 +4263,19 @@ export declare namespace Machine {
4356
4263
  never
4357
4264
  }[keyof NonNullable<History>]
4358
4265
  : never
4359
- /** Extracts the return value from a choice resolver. */
4360
- export type ChoiceReturn<Config> = Config extends { readonly choice: infer Choice } ? EventTransitionReturn<Choice>
4361
- : never
4362
4266
  /**
4363
- * Extracts the return value from an event transition config.
4267
+ * Extracts the invoke config or configs from a state config.
4364
4268
  *
4365
4269
  * @category utility types
4366
4270
  * @since 0.4.0
4367
4271
  */
4368
- export type EventTransitionReturn<Transition> = Transition extends { readonly resolve?: infer Resolve } ?
4369
- NonNullable<Resolve> extends (...args: any) => infer Ret ? Ret : never
4272
+ export type InvokeReturn<Config> = "invoke" extends keyof Config
4273
+ ? Config extends { readonly invoke?: infer Invoke }
4274
+ ? NonNullable<Invoke> extends ReadonlyArray<infer One> ? One : NonNullable<Invoke>
4275
+ : never
4370
4276
  : never
4371
4277
  /**
4372
- * Extracts the return value from a state's event handlers.
4373
- *
4374
- * @category utility types
4375
- * @since 0.4.0
4376
- */
4377
- export type EventHandlerReturn<Config> = Config extends { readonly on?: infer On }
4378
- ? { readonly [EventTag in keyof On]: EventTransitionReturn<NonNullable<On[EventTag]>> }[
4379
- keyof On
4380
- ]
4381
- : never
4382
- /**
4383
- * Extracts the invoke config or configs from a state config.
4384
- *
4385
- * @category utility types
4386
- * @since 0.4.0
4387
- */
4388
- export type InvokeReturn<Config> = "invoke" extends keyof Config
4389
- ? Config extends { readonly invoke?: infer Invoke }
4390
- ? NonNullable<Invoke> extends (...args: any) => infer Resolved
4391
- ? NonNullable<Resolved> extends ReadonlyArray<infer One> ? One : NonNullable<Resolved>
4392
- : NonNullable<Invoke> extends ReadonlyArray<infer One> ? One
4393
- : NonNullable<Invoke>
4394
- : never
4395
- : never
4396
- /**
4397
- * Extracts the child process logic returned by an invoke source.
4278
+ * Extracts the child process logic returned by an invoke source.
4398
4279
  *
4399
4280
  * @category utility types
4400
4281
  * @since 0.4.0
@@ -4408,16 +4289,10 @@ export declare namespace Machine {
4408
4289
  type ChildMachineLogic<Child> = Child extends ChildMachine<string, infer M> ? Logic<
4409
4290
  Snapshot<States<M>>,
4410
4291
  EventInput<InputEvent<M>>,
4411
- Error<M> | ActionError<Services<M>> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
4412
- ExcludeCompatibleRuntime<
4413
- Exclude<ExecutionServices<InitialServices<M> | Services<M>>, MachineRuntime>,
4414
- Event<M>,
4415
- EmittedEvent<M>
4416
- >,
4292
+ Error<M> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
4293
+ Services<M>,
4417
4294
  Output<M>,
4418
- | InitialError<M>
4419
4295
  | Error<M>
4420
- | ActionError<InitialServices<M> | Services<M>>
4421
4296
  | InfiniteTransitionError
4422
4297
  | MachineSchemaDecodeError
4423
4298
  | StartupError
@@ -4450,12 +4325,15 @@ export declare namespace Machine {
4450
4325
  /**
4451
4326
  * Extracts the startup error from an invoke source child process logic.
4452
4327
  *
4328
+ * **Details**
4329
+ *
4330
+ * Invoked logic contributes its startup error. A child machine declares none.
4331
+ *
4453
4332
  * @category utility types
4454
4333
  * @since 0.4.0
4455
4334
  */
4456
- export type InvokeInitialError<Invoke> = Invoke extends {
4457
- readonly [InvokeTypeId]: { readonly initialError: Types.Covariant<infer InitialError> }
4458
- } ? InitialError
4335
+ export type InvokeInitialError<Invoke> = IsAny<Invoke> extends true ? unknown
4336
+ : Invoke extends { readonly child: unknown } ? never
4459
4337
  : LogicInitialErrorOf<InvokeLogic<Invoke>>
4460
4338
  /**
4461
4339
  * Extracts the runtime error from an invoked child process.
@@ -4473,10 +4351,7 @@ export declare namespace Machine {
4473
4351
  * @category utility types
4474
4352
  * @since 0.4.0
4475
4353
  */
4476
- export type InvokeOutput<Invoke> = Invoke extends {
4477
- readonly [InvokeTypeId]: { readonly output: Types.Covariant<infer Output> }
4478
- } ? Output
4479
- : LogicOutputOf<InvokeLogic<Invoke>>
4354
+ export type InvokeOutput<Invoke> = LogicOutputOf<InvokeLogic<Invoke>>
4480
4355
  /**
4481
4356
  * Extracts the service requirements from an invoke source child process logic.
4482
4357
  *
@@ -4489,27 +4364,9 @@ export declare namespace Machine {
4489
4364
  : LogicServicesOf<InvokeLogic<Invoke>>
4490
4365
 
4491
4366
  /** Public parent inputs required by an invoked child machine. */
4492
- export type InvokeParentEvents<Invoke> = Invoke extends {
4493
- readonly [InvokeTypeId]: { readonly parentEvents: Types.Covariant<infer ParentEvent> }
4494
- } ? ParentEvent
4495
- : IsAny<Invoke> extends true ? never
4367
+ export type InvokeParentEvents<Invoke> = IsAny<Invoke> extends true ? never
4496
4368
  : Invoke extends { readonly child: ChildMachine<string, infer M> } ? EventOf<ParentEvents<M>>
4497
4369
  : never
4498
- /** Extracts transition results returned by invocation lifecycle handlers. */
4499
- export type InvokeOutcomeReturn<Invoke> = Invoke extends unknown ?
4500
- | (Invoke extends {
4501
- readonly [InvokeTypeId]: { readonly outcomes: Types.Covariant<infer Outcomes> }
4502
- } ? EventTransitionReturn<Outcomes> :
4503
- never)
4504
- | (Invoke extends { readonly onDone?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
4505
- | (Invoke extends { readonly onFailure?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
4506
- | (Invoke extends { readonly onElement?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
4507
- | (Invoke extends { readonly onSnapshot?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
4508
- : never
4509
- type InvokeOutcomeError<Invoke> = IsAny<InvokeOutcomeReturn<Invoke>> extends true ? never
4510
- : Effect.Error<InvokeOutcomeReturn<Invoke>>
4511
- type InvokeOutcomeServices<Invoke> = IsAny<InvokeOutcomeReturn<Invoke>> extends true ? never
4512
- : Effect.Services<InvokeOutcomeReturn<Invoke>>
4513
4370
  // Effect, Stream, and timer invocations run under a runtime-reserved child id
4514
4371
  // derived from their state path and distinct lifecycle id. Only caller-chosen
4515
4372
  // child ids (logic addresses and child descriptor ids) can collide.
@@ -4535,65 +4392,47 @@ export declare namespace Machine {
4535
4392
  :
4536
4393
  | InvokeCollisionError<InvokeReturn<Config>>
4537
4394
  | InvokeInitialError<InvokeReturn<Config>>
4538
- | InvokeOutcomeError<InvokeReturn<Config>>
4395
+ type InvokeSupport<Invoke> = IsAny<Invoke> extends true ? never
4396
+ : Invoke extends { readonly stream: unknown } ? InvocationSupport<"Stream">
4397
+ : Invoke extends { readonly logic: unknown } ? InvocationSupport<"Logic">
4398
+ : Invoke extends { readonly child: unknown } ? InvocationSupport<"Child">
4399
+ : never
4539
4400
  /**
4540
- * Extracts the parent service requirement contribution from invoked children.
4401
+ * Extracts the requirements that the invocations of a state handler config add
4402
+ * to its machine.
4403
+ *
4404
+ * **Details**
4405
+ *
4406
+ * The contribution contains the services of every invoked source, and one
4407
+ * {@link InvocationSupport} for each invoked kind that not every host runs. An
4408
+ * invoked child machine contributes all the requirements of its machine.
4541
4409
  *
4542
4410
  * @category utility types
4543
4411
  * @since 0.4.0
4544
4412
  */
4545
4413
  export type InvokeRequirements<Config> = [InvokeReturn<Config>] extends [never] ? never
4546
4414
  :
4547
- | MachineRuntime
4415
+ | InvokeSupport<InvokeReturn<Config>>
4548
4416
  | InvokeServices<InvokeReturn<Config>>
4549
- | InvokeOutcomeServices<InvokeReturn<Config>>
4550
4417
  /**
4551
- * Extracts the return value from an eventless transition.
4418
+ * Extracts all service requirements contributed by a state handler config.
4552
4419
  *
4553
- * @category utility types
4554
- * @since 0.4.0
4555
- */
4556
- export type AlwaysReturn<Config> = Config extends { readonly always?: infer Always }
4557
- ? EventTransitionReturn<NonNullable<Always>>
4558
- : never
4559
- /**
4560
- * Extracts the return value from a state completion transition.
4420
+ * **Details**
4561
4421
  *
4562
- * @category utility types
4563
- * @since 0.4.0
4564
- */
4565
- export type DoneReturn<Config> = Config extends { readonly onDone?: infer OnDone }
4566
- ? EventTransitionReturn<NonNullable<OnDone>>
4567
- : never
4568
-
4569
- /**
4570
- * Extracts all service requirements contributed by a state handler config.
4422
+ * Transitions and actions are synchronous, so only invocations contribute
4423
+ * services. An action or history default whose return type is `any` contributes
4424
+ * `unknown`.
4571
4425
  *
4572
4426
  * @category utility types
4573
4427
  * @since 0.4.0
4574
4428
  */
4575
4429
  export type ConfigServices<Config> = Config extends unknown ? [keyof Config] extends [never] ? never :
4576
- | Effect.Services<EventHandlerReturn<Config>>
4577
- | Effect.Services<AlwaysReturn<Config>>
4578
- | Effect.Services<DoneReturn<Config>>
4579
4430
  | Effect.Services<StateActionReturn<Config, "entry">>
4580
4431
  | Effect.Services<StateActionReturn<Config, "exit">>
4581
4432
  | Effect.Services<HistoryDefaultReturn<Config>>
4582
- | Effect.Services<ChoiceReturn<Config>>
4583
4433
  | InvokeRequirements<Config>
4584
4434
  : never
4585
4435
 
4586
- /** The only transition value accepted by machine handler APIs. */
4587
- type TransitionConfig<
4588
- States extends StateSchemas,
4589
- Events extends ReadonlyArray<TaggedSchema>,
4590
- Emits extends ReadonlyArray<TaggedSchema>,
4591
- StateId extends StateNodeIdentifier<States>,
4592
- Context,
4593
- Reenter extends boolean = false,
4594
- Acceptance extends TransitionAcceptance = "required"
4595
- > = Transition<States, StateId, Context, Events, Emits, {}, Reenter, Acceptance>
4596
-
4597
4436
  /** Opaque evidence that `resolve` selected one declared branch. */
4598
4437
  export interface SelectedBranch<out Key extends string, out Result> {
4599
4438
  readonly [SelectedBranchTypeId]: {
@@ -4602,136 +4441,6 @@ export declare namespace Machine {
4602
4441
  }
4603
4442
  }
4604
4443
 
4605
- export type InvokeSource<Value, Context> = Value | ((context: Context) => Value)
4606
-
4607
- /**
4608
- * State-owned work that runs for the lifetime of its active state.
4609
- *
4610
- * **Details**
4611
- *
4612
- * Every `invoke` entry carries this type-level marker, which records the
4613
- * state that owns the entry together with the machine's state schemas and
4614
- * event protocols. The marker property is optional and is never set at
4615
- * runtime.
4616
- *
4617
- * @category models
4618
- * @since 0.23.0
4619
- */
4620
- export interface InvokeOwned<
4621
- States extends StateSchemas,
4622
- Events extends ReadonlyArray<TaggedSchema>,
4623
- Emits extends ReadonlyArray<TaggedSchema>,
4624
- StateId extends StateIdentifier<States>,
4625
- InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4626
- ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4627
- > {
4628
- readonly "~effect/Machine/InvokeOwner"?: Types.Covariant<
4629
- readonly [States, Events, Emits, StateId, InputEvents, ParentEvents]
4630
- >
4631
- }
4632
-
4633
- type StoredInvokeConfig<
4634
- States extends StateSchemas,
4635
- Events extends ReadonlyArray<TaggedSchema>,
4636
- Emits extends ReadonlyArray<TaggedSchema>,
4637
- StateId extends StateIdentifier<States>,
4638
- InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4639
- ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4640
- > =
4641
- & InvokeOwned<States, Events, Emits, StateId, InputEvents, ParentEvents>
4642
- & (
4643
- | {
4644
- readonly id: string
4645
- readonly effect: (
4646
- context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
4647
- ) => Effect.Effect<any, any, any>
4648
- readonly stream?: never
4649
- readonly after?: never
4650
- readonly logic?: never
4651
- readonly child?: never
4652
- readonly address?: never
4653
- readonly onDone?: unknown
4654
- readonly onFailure?: unknown
4655
- readonly onElement?: never
4656
- readonly onSnapshot?: never
4657
- }
4658
- | {
4659
- readonly id: string
4660
- readonly stream: (
4661
- context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
4662
- ) => Stream.Stream<any, any, any>
4663
- readonly effect?: never
4664
- readonly after?: never
4665
- readonly logic?: never
4666
- readonly child?: never
4667
- readonly address?: never
4668
- readonly onElement?: unknown
4669
- readonly onDone: unknown
4670
- readonly onFailure?: unknown
4671
- readonly onSnapshot?: never
4672
- }
4673
- | {
4674
- readonly id: string
4675
- readonly after: InvokeSource<
4676
- Duration.Input,
4677
- InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
4678
- >
4679
- readonly effect?: never
4680
- readonly stream?: never
4681
- readonly logic?: never
4682
- readonly child?: never
4683
- readonly address?: never
4684
- readonly onDone: unknown
4685
- readonly onFailure?: never
4686
- readonly onElement?: never
4687
- readonly onSnapshot?: never
4688
- }
4689
- | {
4690
- readonly id: string
4691
- readonly address: string
4692
- readonly logic: InvokeSource<
4693
- Logic<any, any, any, any, any, any>,
4694
- InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
4695
- >
4696
- readonly effect?: never
4697
- readonly stream?: never
4698
- readonly after?: never
4699
- readonly child?: never
4700
- readonly onDone?: unknown
4701
- readonly onFailure?: unknown
4702
- readonly onElement?: never
4703
- readonly onSnapshot?: unknown
4704
- }
4705
- | {
4706
- readonly child: ChildMachine.Any
4707
- readonly input?:
4708
- | {}
4709
- | null
4710
- | ((context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>) => unknown)
4711
- readonly id?: never
4712
- readonly address?: never
4713
- readonly effect?: never
4714
- readonly stream?: never
4715
- readonly after?: never
4716
- readonly logic?: never
4717
- readonly onDone?: unknown
4718
- readonly onFailure?: unknown
4719
- readonly onElement?: never
4720
- readonly onSnapshot?: unknown
4721
- }
4722
- )
4723
-
4724
- type StoredInvokeDefinition<
4725
- States extends StateSchemas,
4726
- Events extends ReadonlyArray<TaggedSchema>,
4727
- Emits extends ReadonlyArray<TaggedSchema>,
4728
- StateId extends StateIdentifier<States>,
4729
- InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4730
- ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4731
- > =
4732
- | StoredInvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
4733
- | ReadonlyArray<StoredInvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>>
4734
-
4735
4444
  type LogicInitialEffectOf<Value> = Value extends { readonly initial: infer Initial } ?
4736
4445
  Initial extends (...args: ReadonlyArray<any>) => infer Result ? Result : never
4737
4446
  : never
@@ -4742,8 +4451,8 @@ export declare namespace Machine {
4742
4451
 
4743
4452
  export type LogicStateOf<Value> = Effect.Success<LogicInitialEffectOf<Value>>
4744
4453
  export type LogicEventOf<Value> = Value extends { readonly initial: infer Initial } ?
4745
- Initial extends (scope: infer LogicScope, ...args: ReadonlyArray<any>) => any ?
4746
- LogicScope extends Logic.Scope<infer Event> ? Event : never
4454
+ Initial extends (context: infer LogicInitialContext, ...args: ReadonlyArray<any>) => any ?
4455
+ LogicInitialContext extends Logic.InitialContext<infer Event> ? Event : never
4747
4456
  : never
4748
4457
  : never
4749
4458
  export type LogicErrorOf<Value> = Effect.Error<LogicRunEffectOf<Value>>
@@ -4754,7 +4463,6 @@ export declare namespace Machine {
4754
4463
  /** Output construction available to final and output-producing parallel states. */
4755
4464
  type OutputHandlerConfig<
4756
4465
  States extends StateSchemas,
4757
- Events extends ReadonlyArray<TaggedSchema>,
4758
4466
  StateId extends StateIdentifier<States>,
4759
4467
  Context
4760
4468
  > = NodeByIdentifier<States, StateId> extends { readonly output: Schema.Top } ? {
@@ -4771,7 +4479,6 @@ export declare namespace Machine {
4771
4479
  StateId extends StateIdentifier<States>
4772
4480
  > = NodeByIdentifier<States, StateId> extends { readonly type: "parallel" } ? OutputHandlerConfig<
4773
4481
  States,
4774
- Events,
4775
4482
  StateId,
4776
4483
  ParallelOutputContext<States, Events, StateId>
4777
4484
  >
@@ -4834,7 +4541,7 @@ export declare namespace Machine {
4834
4541
  ParentId extends StateIdentifier<States>
4835
4542
  > = (
4836
4543
  context: HistoryDefaultContext<States, Events, Emits, ParentId>,
4837
- enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
4544
+ enqueue: Commands<EventOf<Events>, EmittedEventOf<Emits>>
4838
4545
  ) =>
4839
4546
  | CompleteSnapshotContaining<States, ParentId>
4840
4547
  | StateConstruction<CompleteSnapshotContaining<States, ParentId>>
@@ -4876,42 +4583,6 @@ export declare namespace Machine {
4876
4583
  readonly [Key in HistoryStateKey<Children>]?: HistoryDefaultEntry<States, Events, Emits, ParentId>
4877
4584
  }
4878
4585
 
4879
- /**
4880
- * Required implementation for a choice pseudo-state.
4881
- *
4882
- * **Details**
4883
- *
4884
- * A choice state declares only `choice`, the transition that resolves it to
4885
- * a concrete destination. Every other handler key is rejected.
4886
- *
4887
- * @category models
4888
- * @since 0.23.0
4889
- */
4890
- export interface ChoiceStateConfig<
4891
- States extends StateSchemas,
4892
- Events extends ReadonlyArray<TaggedSchema>,
4893
- Emits extends ReadonlyArray<TaggedSchema>,
4894
- ChoiceId extends ChoiceIdentifier<States>,
4895
- InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4896
- ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4897
- > {
4898
- /** Required transition that resolves this transient choice to a concrete destination. */
4899
- readonly choice: TransitionConfig<
4900
- States,
4901
- Events,
4902
- Emits,
4903
- ChoiceId,
4904
- ChoiceContext<States, Events, Emits, ChoiceId, InputEvents, ParentEvents>
4905
- >
4906
- readonly entry?: never
4907
- readonly exit?: never
4908
- readonly invoke?: never
4909
- readonly always?: never
4910
- readonly on?: never
4911
- readonly onDone?: never
4912
- readonly output?: never
4913
- }
4914
-
4915
4586
  type HandlerChildren<Node> = Node extends { readonly states: infer Children extends StateSchemas } ? Children : never
4916
4587
 
4917
4588
  type HandlerConfigPart<Config> = {
@@ -5106,13 +4777,10 @@ export declare namespace Machine {
5106
4777
  Node,
5107
4778
  Events extends ReadonlyArray<TaggedSchema>,
5108
4779
  InputEvents extends ReadonlyArray<TaggedSchema>,
5109
- Emits extends ReadonlyArray<TaggedSchema>,
5110
4780
  StateId extends StateNodeIdentifier<AllStates>,
5111
4781
  Config,
5112
4782
  AvailableOutputStates extends StateIdentifier<AllStates>
5113
- > = StateId extends ChoiceIdentifier<AllStates> ?
5114
- & HandlerChoiceUnknownConfigKeyValidation<StateId, Config>
5115
- & HandlerRuntimeValidation<Events, Emits, StateId, Config>
4783
+ > = StateId extends ChoiceIdentifier<AllStates> ? HandlerChoiceUnknownConfigKeyValidation<StateId, Config>
5116
4784
  : StateId extends StateIdentifier<AllStates> ?
5117
4785
  & HandlerUnknownConfigKeyValidation<StateId, Config>
5118
4786
  & HandlerOnKeyValidation<Events, StateId, Config>
@@ -5121,7 +4789,6 @@ export declare namespace Machine {
5121
4789
  & HandlerOutputRequirementValidation<AllStates, StateId, AvailableOutputStates, Config>
5122
4790
  & HandlerRequiredHistoryInitializeValidation<AllStates, StateId, Config>
5123
4791
  & ValidateConstructionDeclarations<Node, Config>
5124
- & HandlerRuntimeValidation<Events, Emits, StateId, Config>
5125
4792
  : unknown
5126
4793
 
5127
4794
  type HandlerChoiceUnknownConfigKeyValidation<
@@ -5150,25 +4817,11 @@ export declare namespace Machine {
5150
4817
  >
5151
4818
  }
5152
4819
 
5153
- type HandlerRuntimeValidation<
5154
- Events extends ReadonlyArray<TaggedSchema>,
5155
- Emits extends ReadonlyArray<TaggedSchema>,
5156
- StateId extends string,
5157
- Config,
5158
- Incompatible = keyof Config extends never ? never : IncompatibleRuntime<
5159
- ConfigServices<HandlerConfigPart<Config>>,
5160
- EventOf<Events>,
5161
- EmittedEventOf<Emits>
5162
- >
5163
- > = [Incompatible] extends [never] ? unknown
5164
- : HandlerValidationError<"Handler config requires an incompatible machine runtime", StateId, Incompatible>
5165
-
5166
4820
  type HandlerTreeValidation<
5167
4821
  States extends StateSchemas,
5168
4822
  Node,
5169
4823
  Events extends ReadonlyArray<TaggedSchema>,
5170
4824
  InputEvents extends ReadonlyArray<TaggedSchema>,
5171
- Emits extends ReadonlyArray<TaggedSchema>,
5172
4825
  Path extends StateNodeIdentifier<States>,
5173
4826
  Config,
5174
4827
  OutputStates extends StateIdentifier<States>
@@ -5178,7 +4831,6 @@ export declare namespace Machine {
5178
4831
  Node,
5179
4832
  Events,
5180
4833
  InputEvents,
5181
- Emits,
5182
4834
  Path,
5183
4835
  Config,
5184
4836
  OutputStates
@@ -5191,7 +4843,6 @@ export declare namespace Machine {
5191
4843
  Children[K],
5192
4844
  Events,
5193
4845
  InputEvents,
5194
- Emits,
5195
4846
  Extract<JoinPath<Path, Extract<K, string>>, StateNodeIdentifier<States>>,
5196
4847
  Handlers[K],
5197
4848
  OutputStates
@@ -5206,7 +4857,6 @@ export declare namespace Machine {
5206
4857
  States extends StateSchemas,
5207
4858
  Events extends ReadonlyArray<TaggedSchema>,
5208
4859
  InputEvents extends ReadonlyArray<TaggedSchema>,
5209
- Emits extends ReadonlyArray<TaggedSchema>,
5210
4860
  Config,
5211
4861
  OutputStates extends StateIdentifier<States>
5212
4862
  > = HandlerTreeValidation<
@@ -5214,7 +4864,6 @@ export declare namespace Machine {
5214
4864
  States[""],
5215
4865
  Events,
5216
4866
  InputEvents,
5217
- Emits,
5218
4867
  Extract<"", StateNodeIdentifier<States>>,
5219
4868
  Config,
5220
4869
  OutputStates
@@ -5262,13 +4911,9 @@ export declare namespace Machine {
5262
4911
  : never
5263
4912
 
5264
4913
  type HandlerConfigError<Config> = Config extends unknown ? [keyof Config] extends [never] ? never :
5265
- | Effect.Error<EventHandlerReturn<Config>>
5266
- | Effect.Error<AlwaysReturn<Config>>
5267
- | Effect.Error<DoneReturn<Config>>
5268
4914
  | Effect.Error<StateActionReturn<Config, "entry">>
5269
4915
  | Effect.Error<StateActionReturn<Config, "exit">>
5270
4916
  | Effect.Error<HistoryDefaultReturn<Config>>
5271
- | Effect.Error<ChoiceReturn<Config>>
5272
4917
  | InvokeError<Config>
5273
4918
  : never
5274
4919
 
@@ -5496,7 +5141,6 @@ export declare namespace Machine {
5496
5141
  | ({} extends NodeMakeInput<Node> ? { readonly from?: never; readonly decoded?: never } : never)
5497
5142
  type InlineDestinationConstruction<
5498
5143
  S extends StateSchemas,
5499
- Src extends StateNodeIdentifier<S>,
5500
5144
  P extends DestinationPath<S>,
5501
5145
  C
5502
5146
  > = P extends ChoiceIdentifier<S> ? { readonly from?: never; readonly decoded?: never }
@@ -5552,7 +5196,7 @@ export declare namespace Machine {
5552
5196
  /** Selects one declared branch and may enqueue synchronous commands. */
5553
5197
  readonly resolve?: never
5554
5198
  }
5555
- & InlineDestinationConstruction<S, Src, P, C>
5199
+ & InlineDestinationConstruction<S, P, C>
5556
5200
  }[DestinationPath<S>]
5557
5201
  // A misspelled target matches this member, so validation can name the declared paths instead of `never`.
5558
5202
  type InlineUnknownTarget<S extends StateSchemas> = {
@@ -5642,7 +5286,7 @@ export declare namespace Machine {
5642
5286
  /** Selects one of the declared `branches`. */
5643
5287
  readonly select: BranchSelect<S, Src, R>
5644
5288
  },
5645
- enqueue: Enqueue<EventOf<Ev>, EmittedEventOf<Em>>
5289
+ enqueue: Commands<EventOf<Ev>, EmittedEventOf<Em>>
5646
5290
  ) => unknown
5647
5291
  /** Set to true to exit and reenter the handler source. */
5648
5292
  readonly reenter?: Reenter extends true ? boolean : never
@@ -5692,7 +5336,7 @@ export declare namespace Machine {
5692
5336
  & ObjectPolicy<C, Reenter, Acceptance>
5693
5337
  & {
5694
5338
  /** Enqueues synchronous commands when this transition is selected. */
5695
- readonly commands?: (context: C, enqueue: Enqueue<EventOf<Ev>, EmittedEventOf<Em>>) => void
5339
+ readonly commands?: (context: C, enqueue: Commands<EventOf<Ev>, EmittedEventOf<Em>>) => void
5696
5340
  }
5697
5341
  & DeclarationData<
5698
5342
  | InlineDestination<S, Src, C>
@@ -5776,7 +5420,10 @@ export declare namespace Machine {
5776
5420
  /** Maps the owning state context to the registered source input. */
5777
5421
  readonly input?: never
5778
5422
  }
5779
- type ResolvedInvoke<R, K extends PropertyKey> = SourceKind<R, K> extends "effects" ?
5423
+ // Distributes over the source names, so a state that picks its work by a
5424
+ // condition keeps the requirements and outcomes of every name it can pick.
5425
+ type ResolvedInvoke<R, K extends PropertyKey> = K extends PropertyKey ? ResolvedSource<R, K> : never
5426
+ type ResolvedSource<R, K extends PropertyKey> = SourceKind<R, K> extends "effects" ?
5780
5427
  { readonly effect: () => InvokeResolvedSource<SourceValue<R, K>> }
5781
5428
  : SourceKind<R, K> extends "streams" ? { readonly stream: () => InvokeResolvedSource<SourceValue<R, K>> }
5782
5429
  : SourceKind<R, K> extends "timers" ? { readonly after: Duration.Input }
@@ -5788,8 +5435,7 @@ export declare namespace Machine {
5788
5435
  & { readonly child: SourceValue<R, K> }
5789
5436
  & (SourceValue<R, K> extends ChildMachine<string, infer M> ? {
5790
5437
  readonly [InvokeTypeId]: {
5791
- readonly error: Types.Covariant<Error<M> | ActionError<Services<M>>>
5792
- readonly initialError: Types.Covariant<InitialError<M>>
5438
+ readonly error: Types.Covariant<Error<M>>
5793
5439
  readonly requirements: Types.Covariant<Services<M>>
5794
5440
  }
5795
5441
  } :
@@ -5869,7 +5515,7 @@ export declare namespace Machine {
5869
5515
  >
5870
5516
  }
5871
5517
  type RegisteredInvokeError<R, K extends PropertyKey> = SourceKind<R, K> extends "children"
5872
- ? SourceValue<R, K> extends ChildMachine<string, infer M> ? Error<M> | ActionError<Services<M>> : never
5518
+ ? SourceValue<R, K> extends ChildMachine<string, infer M> ? Error<M> : never
5873
5519
  : InvokeRuntimeError<ResolvedInvoke<R, K>>
5874
5520
  /**
5875
5521
  * A registered source invocation with its required input and reachable outcomes.
@@ -6067,21 +5713,21 @@ export declare namespace Machine {
6067
5713
  /** Runs synchronously on entry and may enqueue commands. */
6068
5714
  readonly entry?: (
6069
5715
  context: StateActionContext<S, Ev, Em, Src, In, Pa>,
6070
- enqueue: Enqueue<EventOf<Ev>, EmittedEventOf<Em>>
6071
- ) => StateActionResult<any, any>
5716
+ enqueue: Commands<EventOf<Ev>, EmittedEventOf<Em>>
5717
+ ) => StateActionResult
6072
5718
  }
6073
5719
  & (Node extends { readonly type: "final" } ?
6074
5720
  & {
6075
5721
  /** Runs synchronously on exit and may enqueue commands. */
6076
5722
  readonly exit?: never
6077
5723
  }
6078
- & OutputHandlerConfig<S, Ev, Src, FinalOutputContext<S, Ev, Src>>
5724
+ & OutputHandlerConfig<S, Src, FinalOutputContext<S, Ev, Src>>
6079
5725
  : {
6080
5726
  /** Runs synchronously on exit and may enqueue commands. */
6081
5727
  readonly exit?: (
6082
5728
  context: StateActionContext<S, Ev, Em, Src, In, Pa>,
6083
- enqueue: Enqueue<EventOf<Ev>, EmittedEventOf<Em>>
6084
- ) => StateActionResult<any, any>
5729
+ enqueue: Commands<EventOf<Ev>, EmittedEventOf<Em>>
5730
+ ) => StateActionResult
6085
5731
  } & ActiveOutputHandlerConfig<S, Ev, Src>)
6086
5732
  : {
6087
5733
  /** Runs synchronously on entry and may enqueue commands. */
@@ -6162,7 +5808,7 @@ export declare namespace Machine {
6162
5808
  readonly [Tag in TagOf<Ev[number]>]?: Transition<
6163
5809
  S,
6164
5810
  Src,
6165
- HandlerContext<S, Ev, Em, Src, Tag, never, never, In, Pa>,
5811
+ HandlerContext<S, Ev, Em, Src, Tag, In, Pa>,
6166
5812
  Ev,
6167
5813
  Em,
6168
5814
  R,
@@ -6605,8 +6251,6 @@ export declare namespace Machine {
6605
6251
  Ev extends ReadonlyArray<TaggedSchema>,
6606
6252
  Em extends ReadonlyArray<TaggedSchema>,
6607
6253
  I extends Schema.Top,
6608
- IE,
6609
- IR,
6610
6254
  Final extends StateIdentifier<S>,
6611
6255
  Out,
6612
6256
  In extends ReadonlyArray<TaggedSchema>,
@@ -6632,127 +6276,32 @@ export declare namespace Machine {
6632
6276
  S,
6633
6277
  Ev,
6634
6278
  In,
6635
- Em,
6636
6279
  NormalizeObjectConfig<NoInfer<C>, R>,
6637
6280
  Extract<
6638
6281
  HandlerTreeEvidence<S, { readonly "": NormalizeObjectConfig<NoInfer<C>, R> }>["outputState"],
6639
6282
  StateIdentifier<S>
6640
6283
  >
6641
6284
  >
6642
- ): Machine<
6643
- S,
6644
- Ev,
6645
- I,
6646
- Exclude<StateIdentifier<S>, HandleEvidence<S, C, R>["stateId"]>,
6647
- HandleEvidence<S, C, R>["error"],
6648
- ExcludeCompatibleRuntime<
6285
+ ):
6286
+ & Machine<
6287
+ S,
6288
+ Ev,
6289
+ I,
6290
+ Exclude<StateIdentifier<S>, HandleEvidence<S, C, R>["stateId"]>,
6291
+ HandleEvidence<S, C, R>["error"],
6649
6292
  HandleEvidence<S, C, R>["services"],
6650
- EventOf<Ev>,
6651
- EmittedEventOf<Em>
6652
- >,
6653
- IE,
6654
- IR,
6655
- Final,
6656
- Out,
6657
- Em,
6658
- Extract<HandleEvidence<S, C, R>["outputState"], StateIdentifier<S>>,
6659
- In,
6660
- Pa
6661
- >
6662
- }
6663
-
6664
- /**
6665
- * Any state config.
6666
- *
6667
- * @category utility types
6668
- * @since 0.4.0
6669
- */
6670
- export type AnyStateConfig =
6671
- | StateConfig<any, any, any, any, any, any, any>
6672
- | ChoiceStateConfig<any, any, any, any>
6673
-
6674
- /**
6675
- * Runtime event-handler map stored for a single state tag.
6676
- *
6677
- * @category models
6678
- * @since 0.4.0
6679
- */
6680
- export type EventHandlerMap<
6681
- States extends StateSchemas,
6682
- Events extends ReadonlyArray<TaggedSchema>,
6683
- Emits extends ReadonlyArray<TaggedSchema>,
6684
- StateId extends StateIdentifier<States>,
6685
- EventTag extends TagOf<Events[number]>,
6686
- E,
6687
- R
6688
- > = Readonly<
6689
- Record<
6690
- PropertyKey,
6691
- TransitionConfig<
6692
- States,
6693
- Events,
6694
- Emits,
6695
- StateId,
6696
- HandlerContext<States, Events, Emits, StateId, EventTag, E, R>,
6697
- true,
6698
- TransitionAcceptance
6293
+ Final,
6294
+ Out,
6295
+ Em,
6296
+ Extract<HandleEvidence<S, C, R>["outputState"], StateIdentifier<S>>,
6297
+ In,
6298
+ Pa
6299
+ >
6300
+ // Type-only: what `MachineTest.check` reads to type supplied results.
6301
+ // Mapped in place, so a declaration prints the results and not the sources.
6302
+ & EffectResultsEvidence<
6303
+ { readonly [K in keyof Registered<R, "effects"> & string]: EffectResultOf<Registered<R, "effects">[K]> }
6699
6304
  >
6700
- >
6701
- >
6702
-
6703
- /**
6704
- * Runtime state config stored for a single state tag.
6705
- *
6706
- * @category models
6707
- * @since 0.4.0
6708
- */
6709
- export interface StateConfig<
6710
- States extends StateSchemas,
6711
- Events extends ReadonlyArray<TaggedSchema>,
6712
- Emits extends ReadonlyArray<TaggedSchema>,
6713
- StateId extends StateIdentifier<States>,
6714
- EventTag extends TagOf<Events[number]>,
6715
- E,
6716
- R
6717
- > {
6718
- /** Runs synchronously on entry and may enqueue commands. */
6719
- readonly entry?: (
6720
- context: StateActionContext<States, Events, Emits, StateId>,
6721
- enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
6722
- ) => StateActionResult<E, R>
6723
- /** Runs synchronously on exit and may enqueue commands. */
6724
- readonly exit?: (
6725
- context: StateActionContext<States, Events, Emits, StateId>,
6726
- enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
6727
- ) => StateActionResult<E, R>
6728
- /** One registered invocation or an array with distinct lifecycle identities. */
6729
- readonly invoke?: StoredInvokeDefinition<States, Events, Emits, StateId>
6730
- /** Eventless transition evaluated during stabilization. */
6731
- readonly always?: TransitionConfig<
6732
- States,
6733
- Events,
6734
- Emits,
6735
- StateId,
6736
- AlwaysContext<States, Events, Emits, StateId>,
6737
- false,
6738
- TransitionAcceptance
6739
- >
6740
- /** Handles successful invocation or state completion with its exact output type. */
6741
- readonly onDone?: TransitionConfig<
6742
- States,
6743
- Events,
6744
- Emits,
6745
- StateId,
6746
- DoneContext<States, Events, Emits, StateId>,
6747
- false,
6748
- TransitionAcceptance
6749
- >
6750
- /** Constructs the decoded output declared by the state schema. */
6751
- readonly output?:
6752
- | ((context: FinalOutputContext<States, Events, StateId>) => unknown)
6753
- | ((context: ParallelOutputContext<States, Events, StateId>) => unknown)
6754
- /** Event handlers keyed by the public or internal event tag. */
6755
- readonly on?: EventHandlerMap<States, Events, Emits, StateId, EventTag, E, R>
6756
6305
  }
6757
6306
 
6758
6307
  // An explicit export makes this ambient namespace export only the members marked `export`; the rest stay private.
@@ -6851,8 +6400,6 @@ export const isFinal: <
6851
6400
  UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
6852
6401
  E = never,
6853
6402
  R = never,
6854
- InitialE = never,
6855
- InitialR = never,
6856
6403
  FinalStates extends Machine.StateIdentifier<States> = never,
6857
6404
  Output = never,
6858
6405
  OutputStates extends Machine.StateIdentifier<States> = never,
@@ -6866,8 +6413,6 @@ export const isFinal: <
6866
6413
  UnhandledStates,
6867
6414
  E,
6868
6415
  R,
6869
- InitialE,
6870
- InitialR,
6871
6416
  FinalStates,
6872
6417
  Output,
6873
6418
  Emits,
@@ -6937,8 +6482,6 @@ type MakeConfig<
6937
6482
  InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
6938
6483
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
6939
6484
  Input extends Schema.Top,
6940
- InitialE,
6941
- InitialR,
6942
6485
  InternalEvents extends ReadonlyArray<Machine.TaggedSchema>,
6943
6486
  ParentDeclaration extends Parent.Any | undefined,
6944
6487
  Effects extends Machine.EffectSources,
@@ -6987,8 +6530,6 @@ type MakeResult<
6987
6530
  InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
6988
6531
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
6989
6532
  Input extends Schema.Top,
6990
- InitialE,
6991
- InitialR,
6992
6533
  InternalEvents extends ReadonlyArray<Machine.TaggedSchema>,
6993
6534
  ParentDeclaration extends Parent.Any | undefined,
6994
6535
  Effects extends Machine.EffectSources,
@@ -7000,8 +6541,6 @@ type MakeResult<
7000
6541
  { readonly "": Root },
7001
6542
  readonly [...InputEvents, ...InternalEvents],
7002
6543
  Input,
7003
- InitialE,
7004
- InitialR,
7005
6544
  Machine.FinalStateFromDefinition<{ readonly "": Root }>,
7006
6545
  Machine.TerminalOutput<{ readonly "": Root }>,
7007
6546
  Emits,
@@ -7029,8 +6568,6 @@ type Make = {
7029
6568
  const InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
7030
6569
  const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
7031
6570
  const Input extends Schema.Top = typeof Schema.Void,
7032
- InitialE = never,
7033
- InitialR = never,
7034
6571
  const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
7035
6572
  const ParentDeclaration extends Parent.Any | undefined = undefined,
7036
6573
  const Effects extends Machine.EffectSources = {},
@@ -7045,8 +6582,6 @@ type Make = {
7045
6582
  InputEvents,
7046
6583
  Emits,
7047
6584
  Input,
7048
- InitialE,
7049
- InitialR,
7050
6585
  InternalEvents,
7051
6586
  ParentDeclaration,
7052
6587
  Effects,
@@ -7061,8 +6596,6 @@ type Make = {
7061
6596
  InputEvents,
7062
6597
  Emits,
7063
6598
  Input,
7064
- InitialE,
7065
- InitialR,
7066
6599
  InternalEvents,
7067
6600
  ParentDeclaration,
7068
6601
  Effects,
@@ -7089,6 +6622,13 @@ type Make = {
7089
6622
  * registered source by name with `invoke: { src }`, and only invoked sources
7090
6623
  * add to the machine's requirements.
7091
6624
  *
6625
+ * Each run of invoked work owns a `Scope`, which every host closes when the run
6626
+ * ends or is stopped. An Effect and a Stream get a new one for each run, and
6627
+ * logic gets one from its `initial` through the end of its `run`. Finalizers
6628
+ * added with `Effect.addFinalizer` or `Effect.acquireRelease` therefore run with
6629
+ * the work, not when the machine stops. A timer acquires nothing, and a child
6630
+ * machine has no scope of its own: its invoked work follows the same rule.
6631
+ *
7092
6632
  * `Machine.events` defines the public input protocol. `Machine.internalEvents`
7093
6633
  * adds raised events and other machine-local deliveries.
7094
6634
  * `Machine.emittedEvents` defines outward ephemeral notifications. The
@@ -7106,7 +6646,7 @@ type Make = {
7106
6646
  * **Example** (Invoking a registered Effect)
7107
6647
  *
7108
6648
  * ```ts import.meta.vitest
7109
- * import { Machine } from "@typeonce/effect-machine"
6649
+ * import { LocalMachine, Machine } from "@typeonce/effect-machine"
7110
6650
  * import { Effect, Option, Schema } from "effect"
7111
6651
  *
7112
6652
  * const Lookup = Schema.TaggedUnion({ Loading: { id: Schema.String }, Ready: { name: Schema.String } })
@@ -7132,7 +6672,7 @@ type Make = {
7132
6672
  * })
7133
6673
  *
7134
6674
  * const name = Effect.scoped(Effect.gen(function*() {
7135
- * const ref = yield* Machine.start(machine)
6675
+ * const ref = yield* LocalMachine.start(machine)
7136
6676
  * yield* ref.send(Events.Load({ id: "1" }))
7137
6677
  * const ready = yield* Machine.waitFor(ref, ({ state }) => state.state.path === "Ready").pipe(Effect.timeout("5 seconds"))
7138
6678
  * return States.get(ready.state, "Ready").pipe(Option.map((state) => state.name))
@@ -7246,7 +6786,7 @@ export const events: <const Cases extends Readonly<Record<string, Schema.Struct.
7246
6786
  * **Example** (Reusing a tagged union as the event protocol)
7247
6787
  *
7248
6788
  * ```ts import.meta.vitest
7249
- * import { Machine } from "@typeonce/effect-machine"
6789
+ * import { LocalMachine, Machine } from "@typeonce/effect-machine"
7250
6790
  * import { Effect, Schema } from "effect"
7251
6791
  *
7252
6792
  * const Event = Machine.eventsFromSchemas(
@@ -7264,7 +6804,7 @@ export const events: <const Cases extends Readonly<Record<string, Schema.Struct.
7264
6804
  * })
7265
6805
  *
7266
6806
  * const count = Effect.scoped(Effect.gen(function*() {
7267
- * const ref = yield* Machine.start(machine)
6807
+ * const ref = yield* LocalMachine.start(machine)
7268
6808
  * yield* ref.send(Event.Increment({ by: 1 }))
7269
6809
  * return (yield* ref.state).value.count
7270
6810
  * }))
@@ -7437,7 +6977,7 @@ export const emittedEventsFromSchemas: {
7437
6977
  * **When to use**
7438
6978
  *
7439
6979
  * Use when you need to store or transport a statechart snapshot independently
7440
- * of its local machine runtime.
6980
+ * of the host that runs the machine.
7441
6981
  *
7442
6982
  * **Details**
7443
6983
  *
@@ -7489,8 +7029,6 @@ export const encodeSnapshot: <
7489
7029
  UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
7490
7030
  E = never,
7491
7031
  R = never,
7492
- InitialE = never,
7493
- InitialR = never,
7494
7032
  FinalStates extends Machine.StateIdentifier<States> = never,
7495
7033
  Output = never,
7496
7034
  OutputStates extends Machine.StateIdentifier<States> = never,
@@ -7504,8 +7042,6 @@ export const encodeSnapshot: <
7504
7042
  UnhandledStates,
7505
7043
  E,
7506
7044
  R,
7507
- InitialE,
7508
- InitialR,
7509
7045
  FinalStates,
7510
7046
  Output,
7511
7047
  Emits,
@@ -7572,8 +7108,6 @@ export const decodeSnapshot: <
7572
7108
  UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
7573
7109
  E = never,
7574
7110
  R = never,
7575
- InitialE = never,
7576
- InitialR = never,
7577
7111
  FinalStates extends Machine.StateIdentifier<States> = never,
7578
7112
  Output = never,
7579
7113
  OutputStates extends Machine.StateIdentifier<States> = never,
@@ -7587,8 +7121,6 @@ export const decodeSnapshot: <
7587
7121
  UnhandledStates,
7588
7122
  E,
7589
7123
  R,
7590
- InitialE,
7591
- InitialR,
7592
7124
  FinalStates,
7593
7125
  Output,
7594
7126
  Emits,
@@ -7617,9 +7149,9 @@ export const decodeSnapshot: <
7617
7149
  *
7618
7150
  * **Gotchas**
7619
7151
  *
7620
- * `start` executes the closed command list as part of the managed machine commit
7621
- * protocol. Manual planners may inspect commands but need running machine targets
7622
- * to execute child-addressed operations.
7152
+ * Planning does not execute the closed command list. A host such as `LocalMachine`
7153
+ * executes it as part of its commit protocol. Manual planners may inspect commands
7154
+ * but need running machine targets to execute child-addressed operations.
7623
7155
  *
7624
7156
  * **Example** (Planning startup without running the machine)
7625
7157
  *
@@ -7638,7 +7170,6 @@ export const decodeSnapshot: <
7638
7170
  * ```
7639
7171
  *
7640
7172
  * @see {@link plan} for planning a received event.
7641
- * @see {@link start} for the managed runtime protocol.
7642
7173
  * @category constructors
7643
7174
  * @since 0.4.0
7644
7175
  */
@@ -7650,8 +7181,6 @@ export const planInitial: <
7650
7181
  UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
7651
7182
  E = never,
7652
7183
  R = never,
7653
- InitialE = never,
7654
- InitialR = never,
7655
7184
  FinalStates extends Machine.StateIdentifier<States> = never,
7656
7185
  Output = never,
7657
7186
  OutputStates extends Machine.StateIdentifier<States> = never,
@@ -7666,8 +7195,6 @@ export const planInitial: <
7666
7195
  UnhandledStates,
7667
7196
  E,
7668
7197
  R,
7669
- InitialE,
7670
- InitialR,
7671
7198
  FinalStates,
7672
7199
  Output,
7673
7200
  Emits,
@@ -7715,7 +7242,7 @@ export const planInitial: <
7715
7242
  readonly output: undefined
7716
7243
  }
7717
7244
  ),
7718
- InitialE | E | InfiniteTransitionError | MachineSchemaDecodeError | StartupError,
7245
+ E | InfiniteTransitionError | MachineSchemaDecodeError | StartupError,
7719
7246
  never
7720
7247
  > = internal.planInitial
7721
7248
 
@@ -7796,7 +7323,7 @@ export const transitionDefinitions: <M extends Machine.Any>(machine: M) => Reado
7796
7323
  > = internal.transitionDefinitions
7797
7324
 
7798
7325
  /**
7799
- * Returns serializable descriptions of every state-owned activity.
7326
+ * Returns serializable descriptions of every state-owned invocation.
7800
7327
  *
7801
7328
  * **Details**
7802
7329
  *
@@ -7807,10 +7334,10 @@ export const transitionDefinitions: <M extends Machine.Any>(machine: M) => Reado
7807
7334
  * @category getters
7808
7335
  * @since 0.4.0
7809
7336
  */
7810
- export const activityDefinitions: <M extends Machine.Any>(
7337
+ export const invocationDefinitions: <M extends Machine.Any>(
7811
7338
  machine: M
7812
- ) => ReadonlyArray<Machine.ActivityDefinition<Machine.StateIdentifier<Machine.States<M>>>> =
7813
- internal.activityDefinitions
7339
+ ) => ReadonlyArray<Machine.InvocationDefinition<Machine.StateIdentifier<Machine.States<M>>>> =
7340
+ internal.invocationDefinitions
7814
7341
 
7815
7342
  /**
7816
7343
  * Returns every state node active in a decoded snapshot, in definition order.
@@ -7853,8 +7380,6 @@ export const enabled: <
7853
7380
  UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
7854
7381
  E = never,
7855
7382
  R = never,
7856
- InitialE = never,
7857
- InitialR = never,
7858
7383
  FinalStates extends Machine.StateIdentifier<States> = never,
7859
7384
  Output = never,
7860
7385
  OutputStates extends Machine.StateIdentifier<States> = never,
@@ -7868,8 +7393,6 @@ export const enabled: <
7868
7393
  UnhandledStates,
7869
7394
  E,
7870
7395
  R,
7871
- InitialE,
7872
- InitialR,
7873
7396
  FinalStates,
7874
7397
  Output,
7875
7398
  Emits,
@@ -7944,8 +7467,6 @@ export const can: {
7944
7467
  UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
7945
7468
  E = never,
7946
7469
  R = never,
7947
- InitialE = never,
7948
- InitialR = never,
7949
7470
  FinalStates extends Machine.StateIdentifier<States> = never,
7950
7471
  Output = never,
7951
7472
  OutputStates extends Machine.StateIdentifier<States> = never,
@@ -7960,8 +7481,6 @@ export const can: {
7960
7481
  UnhandledStates,
7961
7482
  E,
7962
7483
  R,
7963
- InitialE,
7964
- InitialR,
7965
7484
  FinalStates,
7966
7485
  Output,
7967
7486
  Emits,
@@ -7983,8 +7502,6 @@ export const can: {
7983
7502
  UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
7984
7503
  E = never,
7985
7504
  R = never,
7986
- InitialE = never,
7987
- InitialR = never,
7988
7505
  FinalStates extends Machine.StateIdentifier<States> = never,
7989
7506
  Output = never,
7990
7507
  OutputStates extends Machine.StateIdentifier<States> = never,
@@ -7999,8 +7516,6 @@ export const can: {
7999
7516
  UnhandledStates,
8000
7517
  E,
8001
7518
  R,
8002
- InitialE,
8003
- InitialR,
8004
7519
  FinalStates,
8005
7520
  Output,
8006
7521
  Emits,
@@ -8027,8 +7542,8 @@ export const can: {
8027
7542
  *
8028
7543
  * **Gotchas**
8029
7544
  *
8030
- * `plan` returns data; it does not implement the managed machine commit protocol.
8031
- * `start` executes child commands, publishes `next`, and then delivers
7545
+ * `plan` returns data; it does not implement a commit protocol. A host such as
7546
+ * `LocalMachine` executes child commands, publishes `next`, and then delivers
8032
7547
  * `emittedEvents`. Events with no enabled transition are ignored and produce
8033
7548
  * an unchanged plan.
8034
7549
  *
@@ -8059,7 +7574,6 @@ export const can: {
8059
7574
  * ```
8060
7575
  *
8061
7576
  * @see {@link planInitial} for planning machine startup.
8062
- * @see {@link start} for managed execution and lifecycle observation.
8063
7577
  * @category combinators
8064
7578
  * @since 0.4.0
8065
7579
  */
@@ -8071,8 +7585,6 @@ export const plan: <
8071
7585
  UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
8072
7586
  E = never,
8073
7587
  R = never,
8074
- InitialE = never,
8075
- InitialR = never,
8076
7588
  FinalStates extends Machine.StateIdentifier<States> = never,
8077
7589
  Output = never,
8078
7590
  OutputStates extends Machine.StateIdentifier<States> = never,
@@ -8087,8 +7599,6 @@ export const plan: <
8087
7599
  UnhandledStates,
8088
7600
  E,
8089
7601
  R,
8090
- InitialE,
8091
- InitialR,
8092
7602
  FinalStates,
8093
7603
  Output,
8094
7604
  Emits,
@@ -8141,73 +7651,930 @@ export const plan: <
8141
7651
  > = internal.plan
8142
7652
 
8143
7653
  /**
8144
- * Creates advanced stateful process logic from explicit initialization and
8145
- * execution methods.
8146
- *
8147
- * **When to use**
8148
- *
8149
- * Use when you need a machine-scoped process to publish intermediate snapshots
8150
- * directly.
7654
+ * The pure form of a machine, returned by {@link toStep}: functions that say
7655
+ * what the next state is, and which work to start and stop, without running
7656
+ * anything.
8151
7657
  *
8152
7658
  * **Details**
8153
7659
  *
8154
- * Initialization produces the first state before `run` starts. The running
8155
- * context receives events, reads or updates state, manages child processes,
8156
- * and can communicate with its owning machine. Errors and service requirements
8157
- * from both phases remain in the returned `Logic` type.
7660
+ * Every function is synchronous and returns a value. Equal arguments give equal
7661
+ * results, and no call changes a value a caller holds, so a caller may keep an
7662
+ * earlier {@link MachineStep.State} and step from it again.
7663
+ *
7664
+ * - `start` starts the machine from its input, as `LocalMachine.start` does.
7665
+ * - `send` takes one public event. A failed `Exit` means the step did not
7666
+ * happen and the state passed in still holds: the event does not match its
7667
+ * schema (`MachineSchemaDecodeError`), or the machine has ended
7668
+ * (`StoppedError`).
7669
+ * - `settle` reports the end of work that a step started, with its `Exit`, and
7670
+ * that work could not start, with the failure of its evaluation.
7671
+ * - `element` reports one element of a running Stream.
7672
+ * - `report` reports the state of a running `Logic` or child machine.
7673
+ * - `stop` stops the machine as its owner, as `MachineRef.stop` does.
7674
+ * - `resume` starts the machine from a decoded snapshot, as `LocalMachine.resume`
7675
+ * does. A durable host passes the {@link MachineStep.Progress} of the work of
7676
+ * the active states, so that work which ended does not run again and work
7677
+ * which still runs is not started twice.
7678
+ *
7679
+ * A machine that fails or ends still takes a step. The step carries the last
7680
+ * state, the work to stop, and its {@link MachineStep.Step} `exit`.
8158
7681
  *
8159
7682
  * **Gotchas**
8160
7683
  *
8161
- * This is the low-level process constructor. Parent messages sent directly
8162
- * through its scope are intentionally `unknown` because the logic does not know
8163
- * which machine will eventually own it. Prefer typed output, typed child
8164
- * addresses, or invocation lifecycle transitions when possible.
7684
+ * The result of `settle`, `element`, or `report` for work whose state was
7685
+ * exited is a step that changes nothing, also when the same state was entered
7686
+ * again since. So is the result for work that already ended, and for any work
7687
+ * of a machine that has ended.
8165
7688
  *
8166
- * For one-shot work, register an Effect in `make({ effects })` and invoke it
8167
- * with `invoke: { src }` instead. Register logic in `make({ logic })` to run it
8168
- * for the lifetime of one state.
7689
+ * These functions take the work object that a step listed, and compare it by
7690
+ * identity. A copy of it changes nothing. Neither does work that was listed
7691
+ * before a `resume`, or by another step taken from the same state, even though
7692
+ * its `path`, `id`, and `run` are equal.
8169
7693
  *
8170
- * @see {@link make} for registering sources that states invoke by name.
8171
- * @category constructors
8172
- * @since 0.4.0
7694
+ * @category models
7695
+ * @since 0.48.0
8173
7696
  */
8174
- export const logic: <
8175
- State,
8176
- Event = never,
8177
- Output = void,
8178
- Error = never,
8179
- Requirements = never,
8180
- InitialError = never,
8181
- InitialRequirements = never
8182
- >(options: {
8183
- /** The first state, or an Effect that produces it from the process scope before `run` starts. */
8184
- readonly initial:
8185
- | State
8186
- | ((
8187
- scope: Logic.Scope<Event>
8188
- ) => Effect.Effect<State, InitialError, InitialRequirements>)
8189
- readonly run: (
8190
- context: Logic.Context<State, Event>
8191
- ) => Effect.Effect<Output, Error, Requirements>
8192
- }) => Logic<State, Event, Error, Requirements | InitialRequirements, Output, InitialError> = internal.logic
7697
+ export interface MachineStep<in out M extends Machine.Any> {
7698
+ /** Starts the machine from its decoded input. */
7699
+ readonly start: (
7700
+ ...input: [...Machine.InputArgs<Machine.InputSchema<M>>]
7701
+ ) => Exit.Exit<MachineStep.Step<M>, MachineStep.StartError>
8193
7702
 
8194
- /**
8195
- * Creates a typed descriptor for a complete child machine.
8196
- *
8197
- * Descriptors identify a child by id and machine identity, so independently
8198
- * constructed descriptors for the same pair address the same invoked child.
8199
- *
8200
- * @category constructors
8201
- * @since 0.4.0
8202
- */
8203
- export const child: <const Id extends string, M extends Machine.Any>(id: Id, machine: M) => ChildMachine<Id, M> =
8204
- internal.child
7703
+ /**
7704
+ * Takes one public event. A failure means that the event was rejected and
7705
+ * `state` still holds.
7706
+ */
7707
+ readonly send: (
7708
+ state: MachineStep.State<M>,
7709
+ event: Machine.EventInputOf<Machine.InputEvents<M>>
7710
+ ) => Exit.Exit<MachineStep.Step<M>, MachineStep.Rejection>
7711
+
7712
+ /**
7713
+ * Reports the end of work that a step started, or the failure of its
7714
+ * evaluation when it could not start.
7715
+ */
7716
+ readonly settle: (
7717
+ state: MachineStep.State<M>,
7718
+ work: MachineStep.Work<M>,
7719
+ exit: Exit.Exit<unknown, unknown>
7720
+ ) => MachineStep.Step<M>
7721
+
7722
+ /**
7723
+ * Reports one element of a running Stream. The element is taken before the
7724
+ * next one is pulled.
7725
+ */
7726
+ readonly element: (
7727
+ state: MachineStep.State<M>,
7728
+ work: MachineStep.Work.Stream<M>,
7729
+ element: unknown
7730
+ ) => MachineStep.Step<M>
7731
+
7732
+ /**
7733
+ * Reports the state of a running `Logic` or child machine while it is
7734
+ * active: the state that the `Logic` sets, or the snapshot that a step of the
7735
+ * child publishes. Its owner reads it as the `state` of the snapshot that
7736
+ * `onSnapshot` receives.
7737
+ */
7738
+ readonly report: (
7739
+ state: MachineStep.State<M>,
7740
+ work: MachineStep.Work.Logic<M> | MachineStep.Work.Child<M>,
7741
+ snapshot: unknown
7742
+ ) => MachineStep.Step<M>
7743
+
7744
+ /** Stops the machine as its owner. Its `exit` fails with `StoppedError`. */
7745
+ readonly stop: (state: MachineStep.State<M>) => MachineStep.Step<M>
7746
+
7747
+ /**
7748
+ * Starts the machine from a decoded snapshot. `progress` names the work of
7749
+ * the active states that ended or still runs; any other work starts.
7750
+ */
7751
+ readonly resume: (
7752
+ snapshot: Machine.Snapshot<Machine.States<M>>,
7753
+ progress?: ReadonlyArray<MachineStep.Progress<M>>
7754
+ ) => Exit.Exit<MachineStep.Step<M>, MachineSchemaDecodeError>
7755
+ }
8205
7756
 
8206
7757
  /**
8207
- * Binds one machine definition to an open family of runtime child ids.
7758
+ * Types of the values that a {@link MachineStep} takes and returns.
8208
7759
  *
8209
- * Descriptors created by the returned function are interchangeable with
8210
- * {@link child} descriptors for the same id and machine definition.
7760
+ * @category models
7761
+ * @since 0.48.0
7762
+ */
7763
+ export declare namespace MachineStep {
7764
+ /**
7765
+ * The state of a machine, as a host passes it from one step to the next.
7766
+ *
7767
+ * **Details**
7768
+ *
7769
+ * It is an immutable value. It holds the logical state, the work that runs,
7770
+ * and which entry of each invoking state the work belongs to, which is how a
7771
+ * step tells current work from work of an earlier entry.
7772
+ *
7773
+ * **Gotchas**
7774
+ *
7775
+ * Only the steps of the same machine construct it. Encode
7776
+ * {@link Step} `snapshot` with `Machine.encodeSnapshot` to store it, and
7777
+ * `resume` from the decoded snapshot.
7778
+ *
7779
+ * @category models
7780
+ * @since 0.48.0
7781
+ */
7782
+ export interface State<in out M extends Machine.Any> {
7783
+ readonly [MachineStepStateTypeId]: Types.Invariant<M>
7784
+ }
7785
+
7786
+ /**
7787
+ * What one step did, and what its host does next.
7788
+ *
7789
+ * **Details**
7790
+ *
7791
+ * A host carries a step out in this order, which is the order of
7792
+ * `LocalMachine`: it carries out `commands`, interrupts `stop`, publishes
7793
+ * `snapshot`, delivers `emitted`, starts `start`, and interrupts `stopAfter`.
7794
+ *
7795
+ * `snapshot` and `microsteps` are built when they are first read, so a host
7796
+ * that publishes neither pays for neither.
7797
+ *
7798
+ * @category models
7799
+ * @since 0.48.0
7800
+ */
7801
+ export interface Step<in out M extends Machine.Any> {
7802
+ /** The state to pass to the next step. */
7803
+ readonly state: State<M>
7804
+
7805
+ /** The logical state, as a view reads it and `Machine.encodeSnapshot` stores it. */
7806
+ readonly snapshot: Machine.Snapshot<Machine.States<M>>
7807
+
7808
+ /**
7809
+ * The event this step planned, as transitions received it: a decoded
7810
+ * public event, the lifecycle event of work, or the initial event of `start`
7811
+ * and `resume`. It is set also when planning it failed the machine.
7812
+ * `undefined` when the step planned no event, as for `stop`, a result that
7813
+ * is ignored, or a result that fails the machine without a transition.
7814
+ */
7815
+ readonly event: Machine.LifecycleEvent<Machine.Events<M>> | undefined
7816
+
7817
+ /** The microsteps of the plan, in order, as `Machine.plan` returns them. Empty when nothing changed. */
7818
+ readonly microsteps: ReadonlyArray<Microstep<M>>
7819
+
7820
+ /** What transitions asked of the host with `enqueue.sendTo` and `enqueue.stop`, in order. */
7821
+ readonly commands: ReadonlyArray<Command<M>>
7822
+
7823
+ /**
7824
+ * Running work to interrupt before publishing `snapshot`, in order: work of
7825
+ * exited states, or all remaining work when the machine fails or is
7826
+ * stopped. Its `Exit` is not reported.
7827
+ */
7828
+ readonly stop: ReadonlyArray<Work<M>>
7829
+
7830
+ /** Events emitted to the observers of the machine, in order. */
7831
+ readonly emitted: ReadonlyArray<Machine.EmittedEvent<M>>
7832
+
7833
+ /** Work to start, in order. Report its end with `settle`. */
7834
+ readonly start: ReadonlyArray<Work<M>>
7835
+
7836
+ /**
7837
+ * Running work of states that were not exited, to interrupt after
7838
+ * publishing `snapshot` and delivering `emitted` when this step reaches a
7839
+ * final state, latest first. Empty on every non-final, failed, or owner-stopped
7840
+ * step. Its `Exit` is not reported.
7841
+ */
7842
+ readonly stopAfter: ReadonlyArray<Work<M>>
7843
+
7844
+ /**
7845
+ * Every piece of work that runs once this step is carried out, in start
7846
+ * order: the work that runs on, and then `start`. After `resume`, it also
7847
+ * holds the work that {@link Progress} names as running.
7848
+ */
7849
+ readonly running: ReadonlyArray<Work<M>>
7850
+
7851
+ /**
7852
+ * Paths of the states that this step exited, in exit order, each once. A
7853
+ * durable host forgets what it stored about the work of these states. A
7854
+ * state that the step exited and entered again is in `exited` and in
7855
+ * `entered`. `start`, `resume`, `stop`, and a result that is ignored exit
7856
+ * nothing.
7857
+ */
7858
+ readonly exited: ReadonlyArray<Machine.StateIdentifier<Machine.States<M>>>
7859
+
7860
+ /**
7861
+ * Paths of the states that this step entered, in entry order, each once.
7862
+ * `start` enters the states of the initial state. `resume` restores its
7863
+ * states and enters none.
7864
+ */
7865
+ readonly entered: ReadonlyArray<Machine.StateIdentifier<Machine.States<M>>>
7866
+
7867
+ /**
7868
+ * How the machine ended, once it has: its output, or why it failed or
7869
+ * stopped. A machine that has ended runs no work and takes no event.
7870
+ */
7871
+ readonly exit: Exit.Exit<Machine.Output<M>, Error<M>> | undefined
7872
+ }
7873
+
7874
+ /**
7875
+ * One microstep of a {@link Step}.
7876
+ *
7877
+ * @category models
7878
+ * @since 0.48.0
7879
+ */
7880
+ export interface Microstep<M extends Machine.Any> {
7881
+ readonly next: Machine.Snapshot<Machine.States<M>>
7882
+ readonly event: Machine.LifecycleEvent<Machine.Events<M>>
7883
+ readonly transitions: ReadonlyArray<
7884
+ Machine.RetainedTransition<
7885
+ Machine.StateNodeIdentifier<Machine.States<M>>,
7886
+ Machine.TagOf<Machine.Events<M>[number]>,
7887
+ Machine.StateNodeIdentifier<Machine.States<M>>
7888
+ >
7889
+ >
7890
+ readonly commands: ReadonlyArray<Command>
7891
+ readonly raisedEvents: ReadonlyArray<Machine.EventOf<Machine.Events<M>>>
7892
+ readonly emittedEvents: ReadonlyArray<Machine.EmittedEvent<M>>
7893
+ readonly exitPaths: ReadonlyArray<string>
7894
+ readonly entryPaths: ReadonlyArray<string>
7895
+ readonly changed: boolean
7896
+ }
7897
+
7898
+ /**
7899
+ * What a transition asked of its host with `enqueue.sendTo` or
7900
+ * `enqueue.stop`.
7901
+ *
7902
+ * **Details**
7903
+ *
7904
+ * - `Send` sends `event` to a target with `target.send(event)`. An event sent
7905
+ * to `self` reaches the `self` target of the host, which plans it in a
7906
+ * later step.
7907
+ * - `SendToChild` sends `event` to a child of the machine. `work` is the
7908
+ * running `Logic` or child machine that `child` selects, when one runs.
7909
+ * Otherwise the host sends it to a child that work spawned with that
7910
+ * address, and ignores it when there is none.
7911
+ * - `StopChild` stops a child of the machine. `work` is the running `Logic`
7912
+ * or child machine that `child` selects, which no longer runs after this
7913
+ * step: the host stops it, and reports nothing of it. Otherwise the host
7914
+ * stops a child that work spawned with that address, if there is one.
7915
+ *
7916
+ * @category models
7917
+ * @since 0.48.0
7918
+ */
7919
+ export type Command<M extends Machine.Any = Machine.Any> =
7920
+ | Command.Send
7921
+ | Command.SendToChild<M>
7922
+ | Command.StopChild<M>
7923
+
7924
+ /**
7925
+ * The kinds of {@link Command}.
7926
+ *
7927
+ * @category models
7928
+ * @since 0.48.0
7929
+ */
7930
+ export namespace Command {
7931
+ /**
7932
+ * Sends an event to a target outside the machine.
7933
+ *
7934
+ * @category models
7935
+ * @since 0.48.0
7936
+ */
7937
+ export interface Send {
7938
+ readonly _tag: "Send"
7939
+ readonly target: MachineTarget<unknown>
7940
+ readonly event: unknown
7941
+ }
7942
+
7943
+ /**
7944
+ * Sends an event to a child of the machine.
7945
+ *
7946
+ * @category models
7947
+ * @since 0.48.0
7948
+ */
7949
+ export interface SendToChild<M extends Machine.Any> {
7950
+ readonly _tag: "SendToChild"
7951
+ readonly child: ChildMachine.Any | ChildAddress<never>
7952
+ readonly work: Work.Logic<M> | Work.Child<M> | undefined
7953
+ readonly event: unknown
7954
+ }
7955
+
7956
+ /**
7957
+ * Stops a child of the machine.
7958
+ *
7959
+ * @category models
7960
+ * @since 0.48.0
7961
+ */
7962
+ export interface StopChild<M extends Machine.Any> {
7963
+ readonly _tag: "StopChild"
7964
+ readonly child: ChildMachine.Any | ChildAddress<never>
7965
+ readonly work: Work.Logic<M> | Work.Child<M> | undefined
7966
+ }
7967
+ }
7968
+
7969
+ /**
7970
+ * One piece of work that an entry of a state starts.
7971
+ *
7972
+ * **Details**
7973
+ *
7974
+ * Work is described, not evaluated. A host evaluates it when it starts it,
7975
+ * with the accessor of its kind: `effect`, `duration`, `stream`, `logic`, or
7976
+ * `input`. Each accessor calls the source and the input mapper of the work at
7977
+ * most once, however often it is called, and returns an `Exit` instead of
7978
+ * throwing.
7979
+ *
7980
+ * A host starts the work of a step in order. A failed `Exit` of an accessor
7981
+ * means that the work could not start: the host settles the work with that
7982
+ * `Exit` at once. That fails the machine, which then runs no work, so the
7983
+ * rest of the step does not start. This is what `LocalMachine` does when a
7984
+ * source throws.
7985
+ *
7986
+ * @category models
7987
+ * @since 0.48.0
7988
+ */
7989
+ export type Work<M extends Machine.Any> =
7990
+ | Work.Effect<M>
7991
+ | Work.Timer<M>
7992
+ | Work.Stream<M>
7993
+ | Work.Logic<M>
7994
+ | Work.Child<M>
7995
+
7996
+ /**
7997
+ * The kinds of {@link Work}.
7998
+ *
7999
+ * @category models
8000
+ * @since 0.48.0
8001
+ */
8002
+ export namespace Work {
8003
+ /**
8004
+ * What every kind of work names.
8005
+ *
8006
+ * @category models
8007
+ * @since 0.48.0
8008
+ */
8009
+ export interface Proto<M extends Machine.Any> {
8010
+ /** Path of the state that invokes the work. */
8011
+ readonly path: Machine.StateIdentifier<Machine.States<M>>
8012
+
8013
+ /** Id of the invocation within its state. */
8014
+ readonly id: string
8015
+
8016
+ /** Name of the registered source, when the work has one. */
8017
+ readonly source: string | undefined
8018
+
8019
+ /** The entry of its state that started the work, counted from `0`. */
8020
+ readonly run: number
8021
+ }
8022
+
8023
+ /**
8024
+ * An Effect to run. It owns a `Scope`, closed with its `Exit`.
8025
+ *
8026
+ * @category models
8027
+ * @since 0.48.0
8028
+ */
8029
+ export interface Effect<M extends Machine.Any> extends Proto<M> {
8030
+ readonly _tag: "Effect"
8031
+
8032
+ /** Evaluates the Effect to run, at most once. */
8033
+ readonly effect: () => Exit.Exit<StepEffect<unknown, Requirements<M>>>
8034
+ }
8035
+
8036
+ /**
8037
+ * A timer. It waits for its duration, and its host then settles it with a
8038
+ * successful `Exit`.
8039
+ *
8040
+ * **Details**
8041
+ *
8042
+ * `duration` evaluates how long the timer waits, at most once. It follows
8043
+ * the rule of every kind of work: a failure means that the timer could not
8044
+ * start, whether its input mapper or source threw, or its input names no
8045
+ * duration. A host that keeps timers of its own, or stores a due time
8046
+ * before it commits a step, reads `duration` in the step that enters the
8047
+ * state, and settles the timer with that failure where it would have
8048
+ * started.
8049
+ *
8050
+ * `effect` is the timer as an Effect that sleeps for the duration, for a
8051
+ * host whose timers wait on fibers. It fails as `duration` fails.
8052
+ *
8053
+ * @category models
8054
+ * @since 0.48.0
8055
+ */
8056
+ export interface Timer<M extends Machine.Any> extends Proto<M> {
8057
+ readonly _tag: "Timer"
8058
+
8059
+ /** Evaluates how long the timer waits, at most once. */
8060
+ readonly duration: () => Exit.Exit<Duration.Duration>
8061
+
8062
+ /** Evaluates the timer as an Effect that waits for its duration. */
8063
+ readonly effect: () => Exit.Exit<StepEffect<void, never>>
8064
+ }
8065
+
8066
+ /**
8067
+ * A Stream to run. It owns a `Scope`, closed with its end. The host reports
8068
+ * each element with `element`, pulls the next element once that step is
8069
+ * carried out, and settles the Stream with the `Exit` of its run.
8070
+ *
8071
+ * @category models
8072
+ * @since 0.48.0
8073
+ */
8074
+ export interface Stream<M extends Machine.Any> extends Proto<M> {
8075
+ readonly _tag: "Stream"
8076
+
8077
+ /** Evaluates the Stream to run, at most once. */
8078
+ readonly stream: () => Exit.Exit<StepStream<Requirements<M>>>
8079
+ }
8080
+
8081
+ /**
8082
+ * Process logic to run as a child of the machine, at its `address`.
8083
+ *
8084
+ * **Details**
8085
+ *
8086
+ * The host runs `initial` and then `run`, with contexts of its own: the
8087
+ * `parent` endpoint is the machine. It reports each state that the logic
8088
+ * sets with `report`, and settles the logic with the `Exit` of `run`, or
8089
+ * with the failure of `initial`. Both run in one `Scope`, forked from the
8090
+ * `Scope` that `initial` runs in and closed when `run` ends.
8091
+ *
8092
+ * @category models
8093
+ * @since 0.48.0
8094
+ */
8095
+ export interface Logic<M extends Machine.Any> extends Proto<M> {
8096
+ readonly _tag: "Logic"
8097
+
8098
+ /** Address of the logic among the children of the machine. */
8099
+ readonly address: ChildAddress<never>
8100
+
8101
+ /** Evaluates the logic to run, at most once. */
8102
+ readonly logic: () => Exit.Exit<
8103
+ StepLogic<Requirements<M> | Scope.Scope>
8104
+ >
8105
+ }
8106
+
8107
+ /**
8108
+ * A child machine to run, with the machine as its parent.
8109
+ *
8110
+ * **Details**
8111
+ *
8112
+ * A host runs it as a machine of its own, such as one more `Machine.toStep`
8113
+ * of `child.machine` with `parent` set to a target of this machine. It
8114
+ * reports each snapshot of the child with `report`, and settles it with the
8115
+ * output of the child or with why it failed.
8116
+ *
8117
+ * @category models
8118
+ * @since 0.48.0
8119
+ */
8120
+ export interface Child<M extends Machine.Any> extends Proto<M> {
8121
+ readonly _tag: "Child"
8122
+
8123
+ /** The descriptor of the child, which commands select it by. */
8124
+ readonly child: ChildMachine.Any
8125
+
8126
+ /**
8127
+ * Evaluates the arguments of the child's `start`, at most once: empty when
8128
+ * the invocation declares no input, and the input it maps otherwise.
8129
+ */
8130
+ readonly input: () => Exit.Exit<ReadonlyArray<unknown>>
8131
+ }
8132
+ }
8133
+
8134
+ /**
8135
+ * How far a piece of work of an active state got, as a durable host stored
8136
+ * it, for `resume`.
8137
+ *
8138
+ * **Details**
8139
+ *
8140
+ * `Ended` work is not listed again: its end was applied, and running it again
8141
+ * would apply it twice. `Running` work is not listed in {@link Step} `start`,
8142
+ * because the host still runs it. It is in {@link Step} `running`, as the work
8143
+ * object that the host settles once it ends. Any work of an active state that
8144
+ * no progress names starts.
8145
+ *
8146
+ * @category models
8147
+ * @since 0.48.0
8148
+ */
8149
+ export interface Progress<M extends Machine.Any> {
8150
+ /** Path of the state that invokes the work. */
8151
+ readonly path: Machine.StateIdentifier<Machine.States<M>>
8152
+
8153
+ /** Id of the invocation within its state. */
8154
+ readonly id: string
8155
+
8156
+ readonly status: "Ended" | "Running"
8157
+ }
8158
+
8159
+ /**
8160
+ * The children of a running machine, which its host owns.
8161
+ *
8162
+ * **Details**
8163
+ *
8164
+ * Work acts on the children of its machine through these operations: the
8165
+ * `children` of an input mapper, and `Machine.spawn`, `Machine.sendTo`, and
8166
+ * `Machine.stopChild`. A child is selected by its descriptor, or by the
8167
+ * address it was spawned or invoked at.
8168
+ *
8169
+ * A host that runs the `Logic` and child machines of the states also routes
8170
+ * `sendTo` and `stop` to them, and refuses to `spawn` a child at an address
8171
+ * that one of them holds, with `ChildAlreadyExistsError`.
8172
+ *
8173
+ * @category models
8174
+ * @since 0.48.0
8175
+ */
8176
+ export interface Children {
8177
+ /** Starts a child machine, as `ChildOwner.spawn` does. */
8178
+ readonly spawn: (
8179
+ child: ChildMachine.Any,
8180
+ options: { readonly input: unknown } | undefined
8181
+ ) => StepEffect<MachineRef<unknown, never, unknown, unknown, unknown>, unknown>
8182
+
8183
+ /** Starts process logic, as `Machine.spawn` does. */
8184
+ readonly spawnLogic: (
8185
+ logic: StepLogic<unknown>,
8186
+ options: SpawnOptions | undefined
8187
+ ) => StepEffect<MachineRef<unknown, never, unknown, unknown, unknown>, unknown>
8188
+
8189
+ /**
8190
+ * Sends an event to a child. A child that does not run is ignored. When the
8191
+ * child is a machine whose event schema refuses the event, fails with
8192
+ * {@link MachineSchemaDecodeError} and leaves the child running.
8193
+ */
8194
+ readonly sendTo: (
8195
+ child: ChildMachine.Any | ChildAddress<never>,
8196
+ event: unknown
8197
+ ) => Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
8198
+
8199
+ /** Stops a child. A child that does not run is ignored. */
8200
+ readonly stop: (child: ChildMachine.Any | ChildAddress<never>) => Effect.Effect<void>
8201
+ }
8202
+
8203
+ /**
8204
+ * What {@link toStep} asks of a host for a machine.
8205
+ *
8206
+ * **Details**
8207
+ *
8208
+ * - `self` is the target of the running machine, through which the host takes
8209
+ * the events that the machine and its work send to it.
8210
+ * - `parent` is the target of the owner of a machine that declares a parent,
8211
+ * and is required when that parent is. Transitions and input mappers receive
8212
+ * it as `parent`.
8213
+ * - `children` are the children of the machine. They are required when work
8214
+ * of the machine requires `Machine.MachineRuntime` or
8215
+ * `Machine.ChildMachineRuntime`. Without them, every operation on children
8216
+ * fails the machine with `ProcessLocalError`.
8217
+ *
8218
+ * @category models
8219
+ * @since 0.48.0
8220
+ */
8221
+ export type Options<M extends Machine.Any> =
8222
+ & { readonly self: MachineTarget<Machine.EventInputOf<Machine.InputEvents<M>>> }
8223
+ & ParentOption<Machine.ParentEvents<M>>
8224
+ & ChildrenOption<Machine.Services<M>>
8225
+
8226
+ /**
8227
+ * Refuses, at compile time, a machine that a host cannot run.
8228
+ *
8229
+ * **When to use**
8230
+ *
8231
+ * Intersect it with the machine parameter of a host that runs only some
8232
+ * kinds of work: `machine: M & MachineStep.Supported<M, "Stream", never>`.
8233
+ *
8234
+ * **Details**
8235
+ *
8236
+ * `Kinds` are the kinds of {@link InvocationSupport} that the host runs, and
8237
+ * `Capabilities` are the services of children that it supplies as
8238
+ * {@link Children}. Effects and timers need no kind. A machine that needs
8239
+ * more resolves to an object whose property
8240
+ * `~effect/Machine/MachineStep/Unsupported` names what is missing.
8241
+ *
8242
+ * @category utility types
8243
+ * @since 0.48.0
8244
+ */
8245
+ export type Supported<
8246
+ M extends Machine.Any,
8247
+ Kinds extends InvocationSupport.Kind,
8248
+ Capabilities extends MachineRuntime | ChildMachineRuntime = never
8249
+ > = IsAny<Machine.Services<M>> extends true ? unknown
8250
+ : [
8251
+ | UnsupportedInvocation<Machine.Services<M>, Kinds>
8252
+ | UnsupportedCapability<Machine.Services<M>, Capabilities>
8253
+ ] extends [never] ? unknown
8254
+ : {
8255
+ readonly "~effect/Machine/MachineStep/Unsupported": {
8256
+ readonly invocations: UnsupportedInvocation<Machine.Services<M>, Kinds>
8257
+ readonly capabilities: UnsupportedCapability<Machine.Services<M>, Capabilities>
8258
+ }
8259
+ }
8260
+
8261
+ /**
8262
+ * Services that the work of a machine requires of its host. The services of
8263
+ * children come from {@link Children}.
8264
+ *
8265
+ * @category utility types
8266
+ * @since 0.48.0
8267
+ */
8268
+ export type Requirements<M extends Machine.Any> = Exclude<
8269
+ Machine.Services<M>,
8270
+ InvocationSupport<InvocationSupport.Kind> | MachineRuntime | ChildMachineRuntime | Scope.Scope
8271
+ >
8272
+
8273
+ /**
8274
+ * Why a machine failed, or that its owner stopped it, as `MachineRef.join`
8275
+ * of `LocalMachine` reports it.
8276
+ *
8277
+ * @category errors
8278
+ * @since 0.48.0
8279
+ */
8280
+ export type Error<M extends Machine.Any> =
8281
+ | Machine.Error<M>
8282
+ | InfiniteTransitionError
8283
+ | MachineSchemaDecodeError
8284
+ | StoppedError
8285
+
8286
+ /**
8287
+ * Why a machine could not start, as `LocalMachine.start` reports it.
8288
+ *
8289
+ * @category errors
8290
+ * @since 0.48.0
8291
+ */
8292
+ export type StartError = InfiniteTransitionError | MachineSchemaDecodeError | StartupError
8293
+
8294
+ /**
8295
+ * Why `send` did not take an event. The state passed to it still holds.
8296
+ *
8297
+ * @category errors
8298
+ * @since 0.48.0
8299
+ */
8300
+ export type Rejection = MachineSchemaDecodeError | StoppedError
8301
+ }
8302
+
8303
+ // The kinds of work are named `Effect`, `Stream`, and `Logic` in their
8304
+ // namespace, so they refer to the modules through these aliases.
8305
+ type StepEffect<A, R> = Effect.Effect<A, unknown, R>
8306
+
8307
+ type StepStream<R> = Stream.Stream<unknown, unknown, R>
8308
+
8309
+ type StepLogic<R> = Logic<unknown, unknown, unknown, R, unknown, unknown>
8310
+
8311
+ type ParentOption<ParentEvents extends ReadonlyArray<Machine.TaggedSchema>> = Machine.ParentModeOf<ParentEvents> extends
8312
+ "required" ? { readonly parent: MachineTarget<Machine.EventInputOf<ParentEvents>> }
8313
+ : Machine.ParentModeOf<ParentEvents> extends "optional"
8314
+ ? { readonly parent?: MachineTarget<Machine.EventInputOf<ParentEvents>> | undefined }
8315
+ : { readonly parent?: undefined }
8316
+
8317
+ type ChildrenOption<R> = IsAny<R> extends true ? { readonly children?: MachineStep.Children | undefined }
8318
+ : [Extract<R, MachineRuntime | ChildMachineRuntime>] extends [never]
8319
+ ? { readonly children?: MachineStep.Children | undefined }
8320
+ : { readonly children: MachineStep.Children }
8321
+
8322
+ type UnsupportedInvocation<R, Kinds> = R extends InvocationSupport<infer Kind> ? Exclude<Kind, Kinds> : never
8323
+
8324
+ type UnsupportedCapability<R, Capabilities> = R extends MachineRuntime
8325
+ ? [MachineRuntime] extends [Capabilities] ? never : "MachineRuntime"
8326
+ : R extends ChildMachineRuntime ? [ChildMachineRuntime] extends [Capabilities] ? never : "ChildMachineRuntime"
8327
+ : never
8328
+
8329
+ /**
8330
+ * Extracts the step function of a machine: its pure form, for code that runs
8331
+ * a machine on a new host.
8332
+ *
8333
+ * **When to use**
8334
+ *
8335
+ * Applications run machines with `LocalMachine`, `AtomMachine`, or
8336
+ * `ClusterMachine`. Use `toStep` to write a host for a new runtime, such as a
8337
+ * UI framework or a store, that keeps the state itself, runs the work of the
8338
+ * machine, and reports back how it ended.
8339
+ *
8340
+ * **Details**
8341
+ *
8342
+ * The returned {@link MachineStep} runs nothing, and calls no user code for the
8343
+ * work it lists. Each of its functions returns the next state, as a value,
8344
+ * together with the commands to carry out, the work to stop and to start, in
8345
+ * the order the host carries them out. Work is an Effect, a timer, a Stream, a
8346
+ * `Logic`, or a child machine, described as data that the host evaluates when
8347
+ * it starts it. The current entry of a state is part of the state value, so a
8348
+ * result of work whose state was exited is ignored without the host comparing
8349
+ * anything.
8350
+ *
8351
+ * `toStep` accepts every machine that `LocalMachine.start` accepts, and also a
8352
+ * machine that declares a parent. A host states in its own types which kinds of
8353
+ * work it runs, with {@link MachineStep.Supported}. The options are the
8354
+ * {@link MachineStep.Options} of the machine: `self`, the `parent` of a machine
8355
+ * that has one, and the `children` that its work acts on.
8356
+ *
8357
+ * `send` rejects an event that does not match its schema, and keeps the
8358
+ * machine, as `send` of every host does.
8359
+ *
8360
+ * **Gotchas**
8361
+ *
8362
+ * The compiler reads only the requirements of a machine, and an input mapper
8363
+ * receives `children` whatever its machine requires. A host that passes no
8364
+ * `children` refuses every operation on them at run time with
8365
+ * `ProcessLocalError`, as a defect that fails the machine.
8366
+ *
8367
+ * **Example** (Stepping a machine that loads a document)
8368
+ *
8369
+ * ```ts import.meta.vitest
8370
+ * import { Machine } from "@typeonce/effect-machine"
8371
+ * import { Effect, Exit, Schema } from "effect"
8372
+ *
8373
+ * const Events = Machine.events({ Load: {} })
8374
+ * const machine = Machine.make({
8375
+ * root: Machine.state({
8376
+ * states: { Idle: {}, Loading: {}, Loaded: { fields: { text: Schema.String } } }
8377
+ * }),
8378
+ * events: Events,
8379
+ * effects: { load: Effect.succeed("hello") }
8380
+ * }).handle({
8381
+ * initial: { target: "Idle" },
8382
+ * states: {
8383
+ * Idle: { on: { Load: { target: "Loading" } } },
8384
+ * Loading: { invoke: { src: "load", onDone: { target: "Loaded", data: ({ output }) => ({ text: output }) } } }
8385
+ * }
8386
+ * })
8387
+ *
8388
+ * const self = { id: "loader", sessionId: "loader-1", send: () => Effect.void }
8389
+ * const step = Machine.toStep(machine, { self })
8390
+ *
8391
+ * const program = Effect.gen(function*() {
8392
+ * const started = yield* step.start()
8393
+ * const loading = yield* step.send(started.state, Events.Load())
8394
+ * const [work] = loading.start
8395
+ * if (work?._tag !== "Effect") return "no work"
8396
+ * const effect = work.effect()
8397
+ * if (Exit.isFailure(effect)) return "cannot start"
8398
+ * const loaded = step.settle(loading.state, work, yield* Effect.exit(effect.value))
8399
+ * return loaded.snapshot.state.path
8400
+ * })
8401
+ *
8402
+ * await Effect.runPromise(program) // => "Loaded"
8403
+ * ```
8404
+ *
8405
+ * **Example** (Starting work whose input mapper throws)
8406
+ *
8407
+ * ```ts import.meta.vitest
8408
+ * import { Machine } from "@typeonce/effect-machine"
8409
+ * import { Effect, Exit } from "effect"
8410
+ *
8411
+ * const machine = Machine.make({
8412
+ * root: Machine.state({ states: { Loading: {} } }),
8413
+ * events: Machine.events({}),
8414
+ * effects: { load: (_: string) => Effect.void }
8415
+ * }).handle({
8416
+ * initial: { target: "Loading" },
8417
+ * states: {
8418
+ * Loading: {
8419
+ * invoke: {
8420
+ * src: "load",
8421
+ * input: (): string => {
8422
+ * throw new Error("no input")
8423
+ * },
8424
+ * onDone: { stay: true }
8425
+ * }
8426
+ * }
8427
+ * }
8428
+ * })
8429
+ *
8430
+ * const self = { id: "loader", sessionId: "loader-1", send: () => Effect.void }
8431
+ * const step = Machine.toStep(machine, { self })
8432
+ *
8433
+ * // An `Exit` is an Effect, so the step that starts the machine runs as one.
8434
+ * const started = Effect.runSync(step.start())
8435
+ * const [work] = started.start
8436
+ * // The step called no user code: the host evaluates work as it starts it.
8437
+ * const effect = work?._tag === "Effect" ? work.effect() : undefined
8438
+ * const failed = effect !== undefined && Exit.isFailure(effect) ? step.settle(started.state, work!, effect) : started
8439
+ *
8440
+ * failed.exit !== undefined && Exit.isFailure(failed.exit) // => true
8441
+ * ```
8442
+ *
8443
+ * @see {@link plan} for planning one event without work.
8444
+ * @category destructors
8445
+ * @since 0.48.0
8446
+ */
8447
+ export const toStep: <
8448
+ const States extends Machine.StateSchemas,
8449
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
8450
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8451
+ const Input extends Schema.Top = typeof Schema.Void,
8452
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
8453
+ E = never,
8454
+ R = never,
8455
+ FinalStates extends Machine.StateIdentifier<States> = never,
8456
+ Output = never,
8457
+ OutputStates extends Machine.StateIdentifier<States> = never,
8458
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
8459
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8460
+ >(
8461
+ machine:
8462
+ & Machine<
8463
+ States,
8464
+ Events,
8465
+ Input,
8466
+ UnhandledStates,
8467
+ E,
8468
+ R,
8469
+ FinalStates,
8470
+ Output,
8471
+ Emits,
8472
+ OutputStates,
8473
+ InputEvents,
8474
+ ParentEvents
8475
+ >
8476
+ & EnsureExecutable<States, UnhandledStates, OutputStates>,
8477
+ options: MachineStep.Options<
8478
+ Machine<
8479
+ States,
8480
+ Events,
8481
+ Input,
8482
+ UnhandledStates,
8483
+ E,
8484
+ R,
8485
+ FinalStates,
8486
+ Output,
8487
+ Emits,
8488
+ OutputStates,
8489
+ InputEvents,
8490
+ ParentEvents
8491
+ >
8492
+ >
8493
+ ) => MachineStep<
8494
+ Machine<
8495
+ States,
8496
+ Events,
8497
+ Input,
8498
+ UnhandledStates,
8499
+ E,
8500
+ R,
8501
+ FinalStates,
8502
+ Output,
8503
+ Emits,
8504
+ OutputStates,
8505
+ InputEvents,
8506
+ ParentEvents
8507
+ >
8508
+ > = internal.toStep as never
8509
+
8510
+ /**
8511
+ * Creates advanced stateful process logic from explicit initialization and
8512
+ * execution methods.
8513
+ *
8514
+ * **When to use**
8515
+ *
8516
+ * Use when you need a machine-scoped process to publish intermediate snapshots
8517
+ * directly.
8518
+ *
8519
+ * **Details**
8520
+ *
8521
+ * Initialization produces the first state before `run` starts. The running
8522
+ * context receives events, reads or updates state, manages child processes,
8523
+ * and can communicate with its owning machine. Errors and service requirements
8524
+ * from both phases remain in the returned `Logic` type.
8525
+ *
8526
+ * **Gotchas**
8527
+ *
8528
+ * This is the low-level process constructor. Parent messages sent directly
8529
+ * through its scope are intentionally `unknown` because the logic does not know
8530
+ * which machine will eventually own it. Prefer typed output, typed child
8531
+ * addresses, or invocation lifecycle transitions when possible.
8532
+ *
8533
+ * For one-shot work, register an Effect in `make({ effects })` and invoke it
8534
+ * with `invoke: { src }` instead. Register logic in `make({ logic })` to run it
8535
+ * for the lifetime of one state.
8536
+ *
8537
+ * @see {@link make} for registering sources that states invoke by name.
8538
+ * @category constructors
8539
+ * @since 0.4.0
8540
+ */
8541
+ export const logic: <
8542
+ State,
8543
+ Event = never,
8544
+ Output = void,
8545
+ Error = never,
8546
+ Requirements = never,
8547
+ InitialError = never,
8548
+ InitialRequirements = never
8549
+ >(options: {
8550
+ /** The first state, or an Effect that produces it from the initial context before `run` starts. */
8551
+ readonly initial:
8552
+ | State
8553
+ | ((
8554
+ context: Logic.InitialContext<Event>
8555
+ ) => Effect.Effect<State, InitialError, InitialRequirements>)
8556
+ readonly run: (
8557
+ context: Logic.RunContext<State, Event>
8558
+ ) => Effect.Effect<Output, Error, Requirements>
8559
+ }) => Logic<State, Event, Error, Requirements | InitialRequirements, Output, InitialError> = internal.logic
8560
+
8561
+ /**
8562
+ * Creates a typed descriptor for a complete child machine.
8563
+ *
8564
+ * Descriptors identify a child by id and machine identity, so independently
8565
+ * constructed descriptors for the same pair address the same invoked child.
8566
+ *
8567
+ * @category constructors
8568
+ * @since 0.4.0
8569
+ */
8570
+ export const child: <const Id extends string, M extends Machine.Any>(id: Id, machine: M) => ChildMachine<Id, M> =
8571
+ internal.child
8572
+
8573
+ /**
8574
+ * Binds one machine definition to an open family of runtime child ids.
8575
+ *
8576
+ * Descriptors created by the returned function are interchangeable with
8577
+ * {@link child} descriptors for the same id and machine definition.
8211
8578
  *
8212
8579
  * @category constructors
8213
8580
  * @since 0.20.0
@@ -8236,8 +8603,8 @@ export const childAddress: <Event = never>(id: string) => ChildAddress<Event> =
8236
8603
  *
8237
8604
  * **Gotchas**
8238
8605
  *
8239
- * This Effect requires a managed process runtime. A named child id must be
8240
- * unique for the current parent until that child stops.
8606
+ * This Effect requires {@link MachineRuntime}, which only some hosts provide. A
8607
+ * named child id must be unique for the current parent until that child stops.
8241
8608
  *
8242
8609
  * Register logic in `make({ logic })` and use `invoke: { src, id, address, ...outcomes }` for children
8243
8610
  * that start and stop with that state.
@@ -8288,6 +8655,14 @@ export const spawn: {
8288
8655
  /**
8289
8656
  * Sends an event to a named child process of the running machine.
8290
8657
  *
8658
+ * **Details**
8659
+ *
8660
+ * With the descriptor of an invoked child machine, it requires only
8661
+ * {@link ChildMachineRuntime}, which `ClusterMachine` provides too. With a
8662
+ * child address, it requires {@link MachineRuntime}. When the addressed child
8663
+ * is a machine whose event schema refuses the event, fails with
8664
+ * {@link MachineSchemaDecodeError} and leaves the child running.
8665
+ *
8291
8666
  * @category runtime
8292
8667
  * @since 0.4.0
8293
8668
  */
@@ -8295,27 +8670,36 @@ export const sendTo: {
8295
8670
  <Child extends ChildMachine.Any>(
8296
8671
  child: Child,
8297
8672
  event: ChildMachine.Event<Child>
8298
- ): Effect.Effect<void, StoppedError, MachineRuntime>
8673
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError, ChildMachineRuntime>
8299
8674
  <Address extends ChildAddress<never>>(
8300
8675
  id: Address,
8301
8676
  event: ChildAddress.Event<Address>
8302
- ): Effect.Effect<void, StoppedError, MachineRuntime>
8303
- } = internal.sendTo
8677
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError, MachineRuntime>
8678
+ } = internal.sendTo as never
8304
8679
 
8305
8680
  /**
8306
8681
  * Stops a named child process of the running machine.
8307
8682
  *
8683
+ * **Details**
8684
+ *
8685
+ * With the descriptor of an invoked child machine, it requires only
8686
+ * {@link ChildMachineRuntime}, which `ClusterMachine` provides too. With a
8687
+ * child address, it requires {@link MachineRuntime}.
8688
+ *
8308
8689
  * @category runtime
8309
8690
  * @since 0.4.0
8310
8691
  */
8311
8692
  export const stopChild: {
8312
8693
  <Event>(child: ChildAddress<Event>): Effect.Effect<void, never, MachineRuntime>
8313
- <Child extends ChildMachine.Any>(child: Child): Effect.Effect<void, never, MachineRuntime>
8314
- } = internal.stopChild
8694
+ <Child extends ChildMachine.Any>(child: Child): Effect.Effect<void, never, ChildMachineRuntime>
8695
+ } = internal.stopChild as never
8315
8696
 
8316
8697
  /**
8317
8698
  * Returns a stream of terminal lifecycle outcomes for a running machine.
8318
8699
  *
8700
+ * Reads only the `changes` of the reference, so it works with a reference from
8701
+ * any host.
8702
+ *
8319
8703
  * @category combinators
8320
8704
  * @since 0.4.0
8321
8705
  */
@@ -8326,6 +8710,9 @@ export const watch: <State, Event, Error = never, Output = never>(
8326
8710
  /**
8327
8711
  * Waits for the first current or subsequent published snapshot matching a predicate.
8328
8712
  *
8713
+ * Reads only the `changes` of the reference, so it works with a reference from
8714
+ * any host.
8715
+ *
8329
8716
  * Use for external Effect coordination and tests. Prefer atom selectors for UI
8330
8717
  * reads, invocation outcomes for workflow behavior, and `ref.join` for output.
8331
8718
  * This observes state; it does not acknowledge or correlate a sent event.
@@ -8343,7 +8730,7 @@ export const watch: <State, Event, Error = never, Output = never>(
8343
8730
  * **Example** (Waiting for a state with a timeout)
8344
8731
  *
8345
8732
  * ```ts import.meta.vitest
8346
- * import { Machine } from "@typeonce/effect-machine"
8733
+ * import { LocalMachine, Machine } from "@typeonce/effect-machine"
8347
8734
  * import { Effect } from "effect"
8348
8735
  *
8349
8736
  * const Events = Machine.events({ Finish: {} })
@@ -8358,7 +8745,7 @@ export const watch: <State, Event, Error = never, Output = never>(
8358
8745
  * })
8359
8746
  *
8360
8747
  * const path = Effect.scoped(Effect.gen(function*() {
8361
- * const ref = yield* Machine.start(machine)
8748
+ * const ref = yield* LocalMachine.start(machine)
8362
8749
  * yield* ref.send(Events.Finish())
8363
8750
  * const ready = yield* Machine.waitFor(ref, ({ state }) => state.state.path === "Ready").pipe(Effect.timeout("5 seconds"))
8364
8751
  * return ready.state.state.path
@@ -8391,345 +8778,3 @@ export const waitFor: {
8391
8778
  predicate: (snapshot: RuntimeSnapshot<State, Error, Output>) => boolean
8392
8779
  ): Effect.Effect<RuntimeSnapshot<State, Error, Output>, Error | StoppedError | Cause.NoSuchElementError>
8393
8780
  } = internal.waitFor
8394
-
8395
- /**
8396
- * Prepares a fresh machine without initializing it.
8397
- *
8398
- * Use this constructor when observation must be composed before initial-entry
8399
- * actions run. Subscribe to `prepared.emissions`, then evaluate
8400
- * `prepared.start`. Ordinary callers can continue to use {@link start}, which
8401
- * starts directly and does not allocate the prepared lifecycle boundary.
8402
- *
8403
- * `prepared.start` is one-shot and scoped: the `Scope` that first evaluates it
8404
- * owns the machine, and closing that `Scope` is equivalent to `ref.stop`.
8405
- *
8406
- * **Example** (Observing emissions before startup)
8407
- *
8408
- * ```ts import.meta.vitest
8409
- * import { Machine } from "@typeonce/effect-machine"
8410
- * import { Effect, Stream } from "effect"
8411
- *
8412
- * const Emitted = Machine.emittedEvents({ Started: {} })
8413
- * const machine = Machine.make({
8414
- * root: Machine.state({ states: { Idle: {} } }),
8415
- * events: Machine.events({}),
8416
- * emittedEvents: Emitted
8417
- * }).handle({
8418
- * initial: { target: "Idle" },
8419
- * states: {
8420
- * Idle: {
8421
- * entry: (_, enqueue) => {
8422
- * enqueue.emit(Emitted.Started())
8423
- * }
8424
- * }
8425
- * }
8426
- * })
8427
- *
8428
- * const emitted = Effect.scoped(Effect.gen(function*() {
8429
- * const prepared = yield* Machine.prepare(machine)
8430
- * const seen: Array<string> = []
8431
- * yield* prepared.emissions.pipe(
8432
- * Stream.runForEach((event) => Effect.sync(() => seen.push(event._tag))),
8433
- * Effect.forkScoped({ startImmediately: true })
8434
- * )
8435
- * yield* prepared.start
8436
- * yield* Effect.yieldNow
8437
- * return seen
8438
- * }))
8439
- *
8440
- * await Effect.runPromise(emitted) // => ["Started"]
8441
- * ```
8442
- *
8443
- * @category constructors
8444
- * @since 0.11.0
8445
- */
8446
- export const prepare: <
8447
- const States extends Machine.StateSchemas,
8448
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8449
- const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8450
- const Input extends Schema.Top = typeof Schema.Void,
8451
- UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
8452
- E = never,
8453
- R = never,
8454
- InitialE = never,
8455
- InitialR = never,
8456
- FinalStates extends Machine.StateIdentifier<States> = never,
8457
- Output = never,
8458
- OutputStates extends Machine.StateIdentifier<States> = never,
8459
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
8460
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8461
- >(
8462
- machine:
8463
- & Machine<
8464
- States,
8465
- Events,
8466
- Input,
8467
- UnhandledStates,
8468
- E,
8469
- R,
8470
- InitialE,
8471
- InitialR,
8472
- FinalStates,
8473
- Output,
8474
- Emits,
8475
- OutputStates,
8476
- InputEvents,
8477
- ParentEvents
8478
- >
8479
- & EnsureExecutable<States, UnhandledStates, OutputStates>
8480
- & Machine.RootCompatible<ParentEvents>,
8481
- ...args: [...Machine.InputArgs<Input>]
8482
- ) => Effect.Effect<
8483
- Prepared<
8484
- Machine.Snapshot<States>,
8485
- Machine.EventInputOf<InputEvents>,
8486
- | E
8487
- | ActionError<R>
8488
- | InfiniteTransitionError
8489
- | MachineSchemaDecodeError
8490
- | StoppedError,
8491
- Output,
8492
- Machine.EmittedEventOf<Emits>,
8493
- | InitialE
8494
- | E
8495
- | ActionError<InitialR | R>
8496
- | InfiniteTransitionError
8497
- | MachineSchemaDecodeError
8498
- | StartupError
8499
- | StoppedError,
8500
- | Scope.Scope
8501
- | ExcludeCompatibleRuntime<
8502
- ExecutionServices<InitialR | R>,
8503
- Machine.EventOf<Events>,
8504
- Machine.EmittedEventOf<Emits>
8505
- >
8506
- >
8507
- > = internal.prepare
8508
-
8509
- /**
8510
- * Starts a machine owned by the current `Scope`.
8511
- *
8512
- * **When to use**
8513
- *
8514
- * Use when you want asynchronous event delivery, lifecycle snapshots, `join`,
8515
- * and machine-owned spawned or invoked children.
8516
- *
8517
- * **Details**
8518
- *
8519
- * For each accepted event the runtime plans the complete synchronous
8520
- * macrostep, executes closed machine commands, stops invokes for exited states,
8521
- * publishes the new state, delivers emitted events, and then starts invokes
8522
- * for entered states.
8523
- *
8524
- * The machine is a scoped resource, like `Effect.forkScoped`. Closing the
8525
- * `Scope` is equivalent to `ref.stop`: active invocations are interrupted,
8526
- * owned children stop, the snapshot becomes `stopped`, and later sends fail
8527
- * with `StoppedError`. An earlier `ref.stop` or terminal completion leaves the
8528
- * finalizer with nothing to do. Own a `Scope` explicitly, for example with
8529
- * `Scope.make` and `Scope.provide`, when the machine must outlive the caller.
8530
- *
8531
- * **Gotchas**
8532
- *
8533
- * The returned handle's `send` operation only enqueues events. Transition
8534
- * failures are reported through the runtime snapshot, `changes`, and `join`
8535
- * rather than being returned by `send`. Sending after the machine reaches any
8536
- * terminal state fails immediately with `StoppedError`.
8537
- *
8538
- * **Example** (Starting a machine in a scope)
8539
- *
8540
- * ```ts import.meta.vitest
8541
- * import { Machine } from "@typeonce/effect-machine"
8542
- * import { Effect } from "effect"
8543
- *
8544
- * const States = Machine.state({ states: { Idle: {} } })
8545
- * const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
8546
- * initial: { target: "Idle" }
8547
- * })
8548
- *
8549
- * const path = Effect.scoped(Effect.gen(function*() {
8550
- * const ref = yield* Machine.start(machine)
8551
- * return (yield* ref.state).state.path
8552
- * }))
8553
- *
8554
- * await Effect.runPromise(path) // => "Idle"
8555
- * ```
8556
- *
8557
- * @see {@link plan} for inspecting the same transition plan without executing it.
8558
- * @see {@link watch} for classified terminal outcomes.
8559
- * @category constructors
8560
- * @since 0.4.0
8561
- */
8562
- export const start: <
8563
- const States extends Machine.StateSchemas,
8564
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8565
- const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8566
- const Input extends Schema.Top = typeof Schema.Void,
8567
- UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
8568
- E = never,
8569
- R = never,
8570
- InitialE = never,
8571
- InitialR = never,
8572
- FinalStates extends Machine.StateIdentifier<States> = never,
8573
- Output = never,
8574
- OutputStates extends Machine.StateIdentifier<States> = never,
8575
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
8576
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8577
- >(
8578
- machine:
8579
- & Machine<
8580
- States,
8581
- Events,
8582
- Input,
8583
- UnhandledStates,
8584
- E,
8585
- R,
8586
- InitialE,
8587
- InitialR,
8588
- FinalStates,
8589
- Output,
8590
- Emits,
8591
- OutputStates,
8592
- InputEvents,
8593
- ParentEvents
8594
- >
8595
- & EnsureExecutable<States, UnhandledStates, OutputStates>
8596
- & Machine.RootCompatible<ParentEvents>,
8597
- ...args: [...Machine.InputArgs<Input>]
8598
- ) => Effect.Effect<
8599
- MachineRef<
8600
- Machine.Snapshot<States>,
8601
- Machine.EventInputOf<InputEvents>,
8602
- | E
8603
- | ActionError<R>
8604
- | InfiniteTransitionError
8605
- | MachineSchemaDecodeError
8606
- | StoppedError,
8607
- Output,
8608
- Machine.EmittedEventOf<Emits>
8609
- >,
8610
- | InitialE
8611
- | E
8612
- | ActionError<InitialR | R>
8613
- | InfiniteTransitionError
8614
- | MachineSchemaDecodeError
8615
- | StartupError
8616
- | StoppedError,
8617
- | Scope.Scope
8618
- | ExcludeCompatibleRuntime<
8619
- ExecutionServices<InitialR | R>,
8620
- Machine.EventOf<Events>,
8621
- Machine.EmittedEventOf<Emits>
8622
- >
8623
- > = internal.start
8624
-
8625
- /**
8626
- * Starts a fresh managed runtime from a decoded logical snapshot, owned by the
8627
- * current `Scope`.
8628
- *
8629
- * **Details**
8630
- *
8631
- * The resumed machine has the same scoped lifetime as {@link start}: closing
8632
- * the `Scope` is equivalent to `ref.stop`.
8633
- *
8634
- * `resume` validates and normalizes the supplied snapshot before publishing it
8635
- * as the first state. It does not construct the declared initial state, replay
8636
- * entry or transition actions, re-deliver raised or emitted events, or
8637
- * re-evaluate historical completion and eventless transitions. Active-state
8638
- * invokes start once in ancestor and document order with {@link InitialEvent};
8639
- * timer invocations restart their complete duration and child-machine
8640
- * invocations start from their own initial state.
8641
- *
8642
- * Only logical state, completion, and history metadata are resumed. Queues,
8643
- * scopes, subscriptions, fibers, spawned children, invoke progress, and prior
8644
- * runtime status are process-local and are not restored. A final snapshot
8645
- * immediately produces a completed ref with its current-machine output.
8646
- *
8647
- * Decode encoded data explicitly with {@link decodeSnapshot} before calling
8648
- * this function. Stable snapshots are hosted as supplied; newly enabled
8649
- * eventless or completion transitions in a changed machine definition are not
8650
- * evaluated merely because the runtime was resumed; only ordinary subsequent
8651
- * transition planning can enter and stabilize states.
8652
- *
8653
- * **Example** (Resuming from a decoded snapshot)
8654
- *
8655
- * ```ts import.meta.vitest
8656
- * import { Machine } from "@typeonce/effect-machine"
8657
- * import { Effect } from "effect"
8658
- *
8659
- * const States = Machine.state({ states: { Idle: {} } })
8660
- * const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
8661
- * initial: { target: "Idle" }
8662
- * })
8663
- *
8664
- * const path = Effect.scoped(Effect.gen(function*() {
8665
- * const initial = yield* Machine.planInitial(machine)
8666
- * const encoded = yield* Machine.encodeSnapshot(machine, initial.state)
8667
- * const snapshot = yield* Machine.decodeSnapshot(machine, encoded)
8668
- * const ref = yield* Machine.resume(machine, snapshot)
8669
- * return (yield* ref.state).state.path
8670
- * }))
8671
- *
8672
- * await Effect.runPromise(path) // => "Idle"
8673
- * ```
8674
- *
8675
- * @see {@link decodeSnapshot} for the schema and transport boundary.
8676
- * @see {@link start} for ordinary initial startup.
8677
- * @category constructors
8678
- * @since 0.4.0
8679
- */
8680
- export const resume: <
8681
- const States extends Machine.StateSchemas,
8682
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
8683
- const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8684
- const Input extends Schema.Top = typeof Schema.Void,
8685
- UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
8686
- E = never,
8687
- R = never,
8688
- InitialE = never,
8689
- InitialR = never,
8690
- FinalStates extends Machine.StateIdentifier<States> = never,
8691
- Output = never,
8692
- OutputStates extends Machine.StateIdentifier<States> = never,
8693
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
8694
- ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
8695
- >(
8696
- machine:
8697
- & Machine<
8698
- States,
8699
- Events,
8700
- Input,
8701
- UnhandledStates,
8702
- E,
8703
- R,
8704
- InitialE,
8705
- InitialR,
8706
- FinalStates,
8707
- Output,
8708
- Emits,
8709
- OutputStates,
8710
- InputEvents,
8711
- ParentEvents
8712
- >
8713
- & EnsureExecutable<States, UnhandledStates, OutputStates>
8714
- & Machine.RootCompatible<ParentEvents>,
8715
- snapshot: Machine.Snapshot<States>
8716
- ) => Effect.Effect<
8717
- MachineRef<
8718
- Machine.Snapshot<States>,
8719
- Machine.EventInputOf<InputEvents>,
8720
- | E
8721
- | ActionError<R>
8722
- | InfiniteTransitionError
8723
- | MachineSchemaDecodeError
8724
- | StoppedError,
8725
- Output,
8726
- Machine.EmittedEventOf<Emits>
8727
- >,
8728
- MachineSchemaDecodeError,
8729
- | Scope.Scope
8730
- | ExcludeCompatibleRuntime<
8731
- ExecutionServices<R>,
8732
- Machine.EventOf<Events>,
8733
- Machine.EmittedEventOf<Emits>
8734
- >
8735
- > = internal.resume