@typeonce/effect-machine 0.47.0 → 0.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (334) hide show
  1. package/AGENTS.md +374 -22
  2. package/CLAUDE.md +374 -22
  3. package/README.md +56 -7
  4. package/ai-docs/src/10_modeling/01_create-a-machine.ts +3 -3
  5. package/ai-docs/src/10_modeling/index.md +64 -17
  6. package/ai-docs/src/20_root-api/index.md +2 -2
  7. package/ai-docs/src/35_cluster/index.md +213 -0
  8. package/ai-docs/src/37_new-host/index.md +90 -0
  9. package/dist/LocalMachine.d.ts +417 -0
  10. package/dist/LocalMachine.d.ts.map +1 -0
  11. package/dist/LocalMachine.js +193 -0
  12. package/dist/LocalMachine.js.map +1 -0
  13. package/dist/Machine.d.ts +1122 -814
  14. package/dist/Machine.d.ts.map +1 -1
  15. package/dist/Machine.js +195 -186
  16. package/dist/Machine.js.map +1 -1
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +1 -0
  20. package/dist/index.js.map +1 -1
  21. package/dist/internal/machine/atom.d.ts +8 -10
  22. package/dist/internal/machine/atom.d.ts.map +1 -1
  23. package/dist/internal/machine/atom.js +7 -6
  24. package/dist/internal/machine/atom.js.map +1 -1
  25. package/dist/internal/machine/capability.d.ts +101 -0
  26. package/dist/internal/machine/capability.d.ts.map +1 -0
  27. package/dist/internal/machine/capability.js +70 -0
  28. package/dist/internal/machine/capability.js.map +1 -0
  29. package/dist/internal/machine/{invocationDefinition.d.ts → capturedInvocation.d.ts} +11 -11
  30. package/dist/internal/machine/capturedInvocation.d.ts.map +1 -0
  31. package/dist/internal/machine/{invocationDefinition.js → capturedInvocation.js} +2 -2
  32. package/dist/internal/machine/capturedInvocation.js.map +1 -0
  33. package/dist/internal/machine/child.d.ts +14 -0
  34. package/dist/internal/machine/child.d.ts.map +1 -0
  35. package/dist/internal/machine/child.js +19 -0
  36. package/dist/internal/machine/child.js.map +1 -0
  37. package/dist/internal/machine/childRegistry.d.ts +0 -2
  38. package/dist/internal/machine/childRegistry.d.ts.map +1 -1
  39. package/dist/internal/machine/childRegistry.js +2 -2
  40. package/dist/internal/machine/childRegistry.js.map +1 -1
  41. package/dist/internal/machine/cluster.d.ts +19 -4
  42. package/dist/internal/machine/cluster.d.ts.map +1 -1
  43. package/dist/internal/machine/cluster.js +263 -110
  44. package/dist/internal/machine/cluster.js.map +1 -1
  45. package/dist/internal/machine/clusterChild.d.ts +66 -0
  46. package/dist/internal/machine/clusterChild.d.ts.map +1 -0
  47. package/dist/internal/machine/clusterChild.js +105 -0
  48. package/dist/internal/machine/clusterChild.js.map +1 -0
  49. package/dist/internal/machine/clusterFailure.d.ts +14 -0
  50. package/dist/internal/machine/clusterFailure.d.ts.map +1 -0
  51. package/dist/internal/machine/clusterFailure.js +44 -0
  52. package/dist/internal/machine/clusterFailure.js.map +1 -0
  53. package/dist/internal/machine/clusterInstance.d.ts +194 -0
  54. package/dist/internal/machine/clusterInstance.d.ts.map +1 -0
  55. package/dist/internal/machine/clusterInstance.js +1076 -0
  56. package/dist/internal/machine/clusterInstance.js.map +1 -0
  57. package/dist/internal/machine/clusterKeepAlive.d.ts +47 -0
  58. package/dist/internal/machine/clusterKeepAlive.d.ts.map +1 -0
  59. package/dist/internal/machine/clusterKeepAlive.js +72 -0
  60. package/dist/internal/machine/clusterKeepAlive.js.map +1 -0
  61. package/dist/internal/machine/clusterProgress.d.ts +73 -0
  62. package/dist/internal/machine/clusterProgress.d.ts.map +1 -0
  63. package/dist/internal/machine/clusterProgress.js +116 -0
  64. package/dist/internal/machine/clusterProgress.js.map +1 -0
  65. package/dist/internal/machine/clusterProtocol.d.ts +37 -4
  66. package/dist/internal/machine/clusterProtocol.d.ts.map +1 -1
  67. package/dist/internal/machine/clusterProtocol.js +18 -4
  68. package/dist/internal/machine/clusterProtocol.js.map +1 -1
  69. package/dist/internal/machine/clusterProxy.d.ts +46 -0
  70. package/dist/internal/machine/clusterProxy.d.ts.map +1 -0
  71. package/dist/internal/machine/clusterProxy.js +150 -0
  72. package/dist/internal/machine/clusterProxy.js.map +1 -0
  73. package/dist/internal/machine/clusterRead.d.ts +61 -0
  74. package/dist/internal/machine/clusterRead.d.ts.map +1 -0
  75. package/dist/internal/machine/clusterRead.js +108 -0
  76. package/dist/internal/machine/clusterRead.js.map +1 -0
  77. package/dist/internal/machine/clusterSqlStorage.d.ts +16 -0
  78. package/dist/internal/machine/clusterSqlStorage.d.ts.map +1 -0
  79. package/dist/internal/machine/clusterSqlStorage.js +138 -0
  80. package/dist/internal/machine/clusterSqlStorage.js.map +1 -0
  81. package/dist/internal/machine/clusterStorageCheck.d.ts +19 -0
  82. package/dist/internal/machine/clusterStorageCheck.d.ts.map +1 -0
  83. package/dist/internal/machine/clusterStorageCheck.js +186 -0
  84. package/dist/internal/machine/clusterStorageCheck.js.map +1 -0
  85. package/dist/internal/machine/clusterTarget.d.ts +74 -0
  86. package/dist/internal/machine/clusterTarget.d.ts.map +1 -0
  87. package/dist/internal/machine/clusterTarget.js +81 -0
  88. package/dist/internal/machine/clusterTarget.js.map +1 -0
  89. package/dist/internal/machine/clusterTimer.d.ts +21 -0
  90. package/dist/internal/machine/clusterTimer.d.ts.map +1 -0
  91. package/dist/internal/machine/clusterTimer.js +50 -0
  92. package/dist/internal/machine/clusterTimer.js.map +1 -0
  93. package/dist/internal/machine/clusterWake.d.ts +61 -0
  94. package/dist/internal/machine/clusterWake.d.ts.map +1 -0
  95. package/dist/internal/machine/clusterWake.js +81 -0
  96. package/dist/internal/machine/clusterWake.js.map +1 -0
  97. package/dist/internal/machine/clusterWork.d.ts +130 -0
  98. package/dist/internal/machine/clusterWork.d.ts.map +1 -0
  99. package/dist/internal/machine/clusterWork.js +191 -0
  100. package/dist/internal/machine/clusterWork.js.map +1 -0
  101. package/dist/internal/machine/command.d.ts +4 -4
  102. package/dist/internal/machine/command.d.ts.map +1 -1
  103. package/dist/internal/machine/commandRuntime.d.ts +8 -6
  104. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  105. package/dist/internal/machine/commandRuntime.js +1 -5
  106. package/dist/internal/machine/commandRuntime.js.map +1 -1
  107. package/dist/internal/machine/configuration.d.ts +0 -13
  108. package/dist/internal/machine/configuration.d.ts.map +1 -1
  109. package/dist/internal/machine/configuration.js +0 -151
  110. package/dist/internal/machine/configuration.js.map +1 -1
  111. package/dist/internal/machine/errors.d.ts +11 -2
  112. package/dist/internal/machine/errors.d.ts.map +1 -1
  113. package/dist/internal/machine/errors.js +11 -2
  114. package/dist/internal/machine/errors.js.map +1 -1
  115. package/dist/internal/machine/executionPlan.d.ts +45 -3
  116. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  117. package/dist/internal/machine/executionPlan.js +328 -191
  118. package/dist/internal/machine/executionPlan.js.map +1 -1
  119. package/dist/internal/machine/implementation.d.ts +6 -6
  120. package/dist/internal/machine/implementation.d.ts.map +1 -1
  121. package/dist/internal/machine/inspectionRuntime.d.ts +1 -1
  122. package/dist/internal/machine/inspectionRuntime.d.ts.map +1 -1
  123. package/dist/internal/machine/invocationChanges.d.ts +60 -0
  124. package/dist/internal/machine/invocationChanges.d.ts.map +1 -0
  125. package/dist/internal/machine/invocationChanges.js +54 -0
  126. package/dist/internal/machine/invocationChanges.js.map +1 -0
  127. package/dist/internal/machine/{activities.d.ts → invocationDefinitions.d.ts} +8 -8
  128. package/dist/internal/machine/invocationDefinitions.d.ts.map +1 -0
  129. package/dist/internal/machine/{activities.js → invocationDefinitions.js} +5 -5
  130. package/dist/internal/machine/invocationDefinitions.js.map +1 -0
  131. package/dist/internal/machine/invocationEvent.d.ts +16 -7
  132. package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
  133. package/dist/internal/machine/invocationEvent.js +26 -12
  134. package/dist/internal/machine/invocationEvent.js.map +1 -1
  135. package/dist/internal/machine/invocationOutcome.d.ts +89 -0
  136. package/dist/internal/machine/invocationOutcome.d.ts.map +1 -0
  137. package/dist/internal/machine/invocationOutcome.js +76 -0
  138. package/dist/internal/machine/invocationOutcome.js.map +1 -0
  139. package/dist/internal/machine/invocationResolution.d.ts +137 -0
  140. package/dist/internal/machine/invocationResolution.d.ts.map +1 -0
  141. package/dist/internal/machine/invocationResolution.js +150 -0
  142. package/dist/internal/machine/invocationResolution.js.map +1 -0
  143. package/dist/internal/machine/logicScope.d.ts +24 -0
  144. package/dist/internal/machine/logicScope.d.ts.map +1 -0
  145. package/dist/internal/machine/logicScope.js +33 -0
  146. package/dist/internal/machine/logicScope.js.map +1 -0
  147. package/dist/internal/machine/machine.d.ts +17 -23
  148. package/dist/internal/machine/machine.d.ts.map +1 -1
  149. package/dist/internal/machine/machine.js +24 -40
  150. package/dist/internal/machine/machine.js.map +1 -1
  151. package/dist/internal/machine/observation.d.ts +3 -0
  152. package/dist/internal/machine/observation.d.ts.map +1 -1
  153. package/dist/internal/machine/observation.js +56 -2
  154. package/dist/internal/machine/observation.js.map +1 -1
  155. package/dist/internal/machine/plannedSnapshot.d.ts.map +1 -1
  156. package/dist/internal/machine/plannedSnapshot.js +11 -20
  157. package/dist/internal/machine/plannedSnapshot.js.map +1 -1
  158. package/dist/internal/machine/planner.d.ts +36 -23
  159. package/dist/internal/machine/planner.d.ts.map +1 -1
  160. package/dist/internal/machine/planner.js +15 -8
  161. package/dist/internal/machine/planner.js.map +1 -1
  162. package/dist/internal/machine/process.d.ts +17 -9
  163. package/dist/internal/machine/process.d.ts.map +1 -1
  164. package/dist/internal/machine/process.js +43 -503
  165. package/dist/internal/machine/process.js.map +1 -1
  166. package/dist/internal/machine/protocol.d.ts +1 -1
  167. package/dist/internal/machine/protocol.d.ts.map +1 -1
  168. package/dist/internal/machine/protocol.js +48 -10
  169. package/dist/internal/machine/protocol.js.map +1 -1
  170. package/dist/internal/machine/runtime.d.ts +20 -16
  171. package/dist/internal/machine/runtime.d.ts.map +1 -1
  172. package/dist/internal/machine/runtime.js +93 -88
  173. package/dist/internal/machine/runtime.js.map +1 -1
  174. package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -1
  175. package/dist/internal/machine/runtimeCompiled.js +359 -120
  176. package/dist/internal/machine/runtimeCompiled.js.map +1 -1
  177. package/dist/internal/machine/runtimeProtocol.d.ts +151 -82
  178. package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
  179. package/dist/internal/machine/runtimeProtocol.js +236 -219
  180. package/dist/internal/machine/runtimeProtocol.js.map +1 -1
  181. package/dist/internal/machine/stateDefinition.d.ts +0 -1
  182. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  183. package/dist/internal/machine/stateDefinition.js +0 -1
  184. package/dist/internal/machine/stateDefinition.js.map +1 -1
  185. package/dist/internal/machine/step.d.ts +486 -0
  186. package/dist/internal/machine/step.d.ts.map +1 -0
  187. package/dist/internal/machine/step.js +957 -0
  188. package/dist/internal/machine/step.js.map +1 -0
  189. package/dist/internal/machine/stepHost.d.ts +33 -0
  190. package/dist/internal/machine/stepHost.d.ts.map +1 -0
  191. package/dist/internal/machine/stepHost.js +501 -0
  192. package/dist/internal/machine/stepHost.js.map +1 -0
  193. package/dist/internal/machine/symbols.d.ts +2 -11
  194. package/dist/internal/machine/symbols.d.ts.map +1 -1
  195. package/dist/internal/machine/symbols.js +2 -2
  196. package/dist/internal/machine/symbols.js.map +1 -1
  197. package/dist/internal/machine/transition.d.ts +7 -7
  198. package/dist/internal/machine/transition.d.ts.map +1 -1
  199. package/dist/internal/testing/machine/check.d.ts.map +1 -1
  200. package/dist/internal/testing/machine/check.js +217 -25
  201. package/dist/internal/testing/machine/check.js.map +1 -1
  202. package/dist/internal/testing/machine/exploration.d.ts +19 -2
  203. package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
  204. package/dist/internal/testing/machine/exploration.js +14 -3
  205. package/dist/internal/testing/machine/exploration.js.map +1 -1
  206. package/dist/internal/testing/machine/format.js.map +1 -1
  207. package/dist/internal/testing/machine/probe.d.ts.map +1 -1
  208. package/dist/internal/testing/machine/probe.js +1 -1
  209. package/dist/internal/testing/machine/probe.js.map +1 -1
  210. package/dist/internal/testing/machine/runtime.d.ts +2 -2
  211. package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
  212. package/dist/internal/testing/machine/runtime.js +10 -3
  213. package/dist/internal/testing/machine/runtime.js.map +1 -1
  214. package/dist/internal/testing/machine/trace.d.ts +38 -3
  215. package/dist/internal/testing/machine/trace.d.ts.map +1 -1
  216. package/dist/internal/testing/machine/trace.js +180 -33
  217. package/dist/internal/testing/machine/trace.js.map +1 -1
  218. package/dist/internal/testing/machine/transitionCoverage.d.ts +1 -1
  219. package/dist/internal/testing/machine/transitionCoverage.d.ts.map +1 -1
  220. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  221. package/dist/internal/testing/machine/verification.js +9 -1
  222. package/dist/internal/testing/machine/verification.js.map +1 -1
  223. package/dist/internal/testing/machine/work.d.ts +47 -0
  224. package/dist/internal/testing/machine/work.d.ts.map +1 -0
  225. package/dist/internal/testing/machine/work.js +74 -0
  226. package/dist/internal/testing/machine/work.js.map +1 -0
  227. package/dist/testing/MachineTest.d.ts +253 -37
  228. package/dist/testing/MachineTest.d.ts.map +1 -1
  229. package/dist/testing/MachineTest.js +81 -9
  230. package/dist/testing/MachineTest.js.map +1 -1
  231. package/dist/unstable/cluster/ClusterMachine.d.ts +1071 -41
  232. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  233. package/dist/unstable/cluster/ClusterMachine.js +441 -12
  234. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  235. package/dist/unstable/cluster/ClusterMachineProxy.d.ts +257 -0
  236. package/dist/unstable/cluster/ClusterMachineProxy.d.ts.map +1 -0
  237. package/dist/unstable/cluster/ClusterMachineProxy.js +73 -0
  238. package/dist/unstable/cluster/ClusterMachineProxy.js.map +1 -0
  239. package/dist/unstable/cluster/SqlMachineStorage.d.ts +125 -0
  240. package/dist/unstable/cluster/SqlMachineStorage.d.ts.map +1 -0
  241. package/dist/unstable/cluster/SqlMachineStorage.js +62 -0
  242. package/dist/unstable/cluster/SqlMachineStorage.js.map +1 -0
  243. package/dist/unstable/cluster/index.d.ts +13 -0
  244. package/dist/unstable/cluster/index.d.ts.map +1 -1
  245. package/dist/unstable/cluster/index.js +13 -0
  246. package/dist/unstable/cluster/index.js.map +1 -1
  247. package/dist/unstable/reactivity/AtomMachine.d.ts +21 -26
  248. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  249. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  250. package/package.json +5 -3
  251. package/src/LocalMachine.ts +611 -0
  252. package/src/Machine.ts +1383 -1336
  253. package/src/index.ts +1 -0
  254. package/src/internal/machine/atom.ts +20 -49
  255. package/src/internal/machine/capability.ts +123 -0
  256. package/src/internal/machine/{invocationDefinition.ts → capturedInvocation.ts} +13 -13
  257. package/src/internal/machine/child.ts +25 -0
  258. package/src/internal/machine/childRegistry.ts +1 -4
  259. package/src/internal/machine/cluster.ts +420 -205
  260. package/src/internal/machine/clusterChild.ts +117 -0
  261. package/src/internal/machine/clusterFailure.ts +53 -0
  262. package/src/internal/machine/clusterInstance.ts +1779 -0
  263. package/src/internal/machine/clusterKeepAlive.ts +93 -0
  264. package/src/internal/machine/clusterProgress.ts +159 -0
  265. package/src/internal/machine/clusterProtocol.ts +46 -4
  266. package/src/internal/machine/clusterProxy.ts +233 -0
  267. package/src/internal/machine/clusterRead.ts +207 -0
  268. package/src/internal/machine/clusterSqlStorage.ts +181 -0
  269. package/src/internal/machine/clusterStorageCheck.ts +274 -0
  270. package/src/internal/machine/clusterTarget.ts +154 -0
  271. package/src/internal/machine/clusterTimer.ts +58 -0
  272. package/src/internal/machine/clusterWake.ts +100 -0
  273. package/src/internal/machine/clusterWork.ts +269 -0
  274. package/src/internal/machine/command.ts +4 -4
  275. package/src/internal/machine/commandRuntime.ts +12 -24
  276. package/src/internal/machine/configuration.ts +0 -181
  277. package/src/internal/machine/errors.ts +11 -2
  278. package/src/internal/machine/executionPlan.ts +493 -263
  279. package/src/internal/machine/implementation.ts +6 -6
  280. package/src/internal/machine/inspectionRuntime.ts +1 -1
  281. package/src/internal/machine/invocationChanges.ts +89 -0
  282. package/src/internal/machine/{activities.ts → invocationDefinitions.ts} +10 -10
  283. package/src/internal/machine/invocationEvent.ts +35 -12
  284. package/src/internal/machine/invocationOutcome.ts +145 -0
  285. package/src/internal/machine/invocationResolution.ts +307 -0
  286. package/src/internal/machine/logicScope.ts +47 -0
  287. package/src/internal/machine/machine.ts +35 -100
  288. package/src/internal/machine/observation.ts +67 -2
  289. package/src/internal/machine/plannedSnapshot.ts +17 -20
  290. package/src/internal/machine/planner.ts +75 -99
  291. package/src/internal/machine/process.ts +78 -722
  292. package/src/internal/machine/protocol.ts +70 -26
  293. package/src/internal/machine/runtime.ts +198 -195
  294. package/src/internal/machine/runtimeCompiled.ts +496 -171
  295. package/src/internal/machine/runtimeProtocol.ts +416 -305
  296. package/src/internal/machine/stateDefinition.ts +0 -5
  297. package/src/internal/machine/step.ts +1518 -0
  298. package/src/internal/machine/stepHost.ts +634 -0
  299. package/src/internal/machine/symbols.ts +3 -14
  300. package/src/internal/machine/transition.ts +11 -11
  301. package/src/internal/testing/machine/check.ts +240 -36
  302. package/src/internal/testing/machine/exploration.ts +42 -3
  303. package/src/internal/testing/machine/format.ts +1 -1
  304. package/src/internal/testing/machine/probe.ts +2 -1
  305. package/src/internal/testing/machine/runtime.ts +15 -5
  306. package/src/internal/testing/machine/trace.ts +261 -41
  307. package/src/internal/testing/machine/transitionCoverage.ts +1 -1
  308. package/src/internal/testing/machine/verification.ts +11 -5
  309. package/src/internal/testing/machine/work.ts +101 -0
  310. package/src/testing/MachineTest.ts +268 -44
  311. package/src/unstable/cluster/ClusterMachine.ts +1183 -59
  312. package/src/unstable/cluster/ClusterMachineProxy.ts +372 -0
  313. package/src/unstable/cluster/SqlMachineStorage.ts +136 -0
  314. package/src/unstable/cluster/index.ts +15 -0
  315. package/src/unstable/reactivity/AtomMachine.ts +27 -88
  316. package/dist/internal/machine/activities.d.ts.map +0 -1
  317. package/dist/internal/machine/activities.js.map +0 -1
  318. package/dist/internal/machine/invocation.d.ts +0 -33
  319. package/dist/internal/machine/invocation.d.ts.map +0 -1
  320. package/dist/internal/machine/invocation.js +0 -220
  321. package/dist/internal/machine/invocation.js.map +0 -1
  322. package/dist/internal/machine/invocationDefinition.d.ts.map +0 -1
  323. package/dist/internal/machine/invocationDefinition.js.map +0 -1
  324. package/dist/internal/machine/requirements.d.ts +0 -4
  325. package/dist/internal/machine/requirements.d.ts.map +0 -1
  326. package/dist/internal/machine/requirements.js +0 -2
  327. package/dist/internal/machine/requirements.js.map +0 -1
  328. package/dist/internal/machine/runtimeGeneric.d.ts +0 -3
  329. package/dist/internal/machine/runtimeGeneric.d.ts.map +0 -1
  330. package/dist/internal/machine/runtimeGeneric.js +0 -465
  331. package/dist/internal/machine/runtimeGeneric.js.map +0 -1
  332. package/src/internal/machine/invocation.ts +0 -376
  333. package/src/internal/machine/requirements.ts +0 -11
  334. package/src/internal/machine/runtimeGeneric.ts +0 -797
@@ -0,0 +1,611 @@
1
+ /**
2
+ * Runs machines in memory, as scoped resources of the current process.
3
+ *
4
+ * `Machine` declares a model and plans its steps. `LocalMachine` is the host
5
+ * that executes them without storage or distribution: {@link start} and
6
+ * {@link resume} return a live reference, and {@link prepare} exposes the
7
+ * machine's streams before it initializes.
8
+ *
9
+ * @since 0.48.0
10
+ */
11
+
12
+ import type * as Cause from "effect/Cause"
13
+ import type * as Effect from "effect/Effect"
14
+ import type * as Exit from "effect/Exit"
15
+ import type * as Schema from "effect/Schema"
16
+ import type * as Scope from "effect/Scope"
17
+ import type * as Stream from "effect/Stream"
18
+ import * as internal from "./internal/machine/process.js"
19
+ import type { EnsureExecutable } from "./internal/machine/readiness.js"
20
+ import type {
21
+ ChildMachineRuntime,
22
+ InfiniteTransitionError,
23
+ InvocationSupport,
24
+ Machine,
25
+ MachineRef,
26
+ MachineRuntime,
27
+ MachineSchemaDecodeError,
28
+ RuntimeSnapshot,
29
+ StartupError,
30
+ StoppedError
31
+ } from "./Machine.js"
32
+
33
+ /**
34
+ * Resolves the services a local machine requires from its environment.
35
+ *
36
+ * **When to use**
37
+ *
38
+ * Use when writing a generic API that starts local machines, to name the
39
+ * services its caller must provide.
40
+ *
41
+ * **Details**
42
+ *
43
+ * Removes `Machine.MachineRuntime` and `Machine.ChildMachineRuntime` from the
44
+ * machine's requirements, because the running machine provides them to the work
45
+ * it runs, and every
46
+ * `Machine.InvocationSupport`, because a local machine runs every kind of
47
+ * invocation.
48
+ *
49
+ * @category utility types
50
+ * @since 0.48.0
51
+ */
52
+ export type RequiredServices<Requirements> = Exclude<
53
+ Requirements,
54
+ MachineRuntime | ChildMachineRuntime | InvocationSupport<InvocationSupport.Kind>
55
+ >
56
+
57
+ /**
58
+ * Live, root-scoped records describing one prepared machine tree.
59
+ *
60
+ * Inspection records deliberately erase machine-specific values to `unknown`:
61
+ * one stream may contain unrelated root, child-machine, and `Logic` protocols.
62
+ * The record structure remains a closed discriminated union, while typed
63
+ * application observation continues through `changes` and `emissions`.
64
+ * Records retain decoded local events and snapshots and are not themselves a
65
+ * stable JSON export format. Telemetry exporters must project process-local
66
+ * values into an explicit portable representation.
67
+ *
68
+ * @category models
69
+ * @since 0.48.0
70
+ */
71
+ export declare namespace Inspection {
72
+ /** Read-only identity of a local machine endpoint. */
73
+ export interface Endpoint {
74
+ readonly id: string
75
+ readonly sessionId: string
76
+ }
77
+
78
+ /** Read-only identity of a runtime represented in the inspection tree. */
79
+ export interface Subject extends Endpoint {
80
+ readonly kind: "Machine" | "Logic"
81
+ }
82
+
83
+ /** Causal origin of an inspected runtime. */
84
+ export type Origin =
85
+ | { readonly _tag: "Root" }
86
+ | {
87
+ readonly _tag: "Invoke"
88
+ readonly ownerPath: string
89
+ readonly invokeId: string
90
+ }
91
+ | {
92
+ readonly _tag: "Spawn"
93
+ readonly address: string | undefined
94
+ }
95
+
96
+ /** Causal owner of a send or emission. */
97
+ export type Causation =
98
+ | { readonly _tag: "Initialization" }
99
+ | { readonly _tag: "Macrostep"; readonly macrostepId: number }
100
+ | { readonly _tag: "ProgramInvocation"; readonly invocationSessionId: string }
101
+
102
+ /** Closed command projection safe for observation. */
103
+ export type Command =
104
+ | {
105
+ readonly _tag: "SendTo"
106
+ readonly target: Endpoint | { readonly id: string }
107
+ readonly event: unknown
108
+ }
109
+ | {
110
+ readonly _tag: "Stop"
111
+ readonly target: Endpoint | { readonly id: string }
112
+ }
113
+
114
+ /** One statechart microstep in a committed macrostep. */
115
+ export interface Microstep {
116
+ readonly event: unknown
117
+ readonly transitions: ReadonlyArray<Machine.RetainedTransition>
118
+ readonly raisedEvents: ReadonlyArray<unknown>
119
+ readonly emittedEvents: ReadonlyArray<unknown>
120
+ readonly commands: ReadonlyArray<Command>
121
+ readonly exitPaths: ReadonlyArray<string>
122
+ readonly entryPaths: ReadonlyArray<string>
123
+ readonly changed: boolean
124
+ }
125
+
126
+ /** Common ordering and identity fields for every record. */
127
+ export interface Base {
128
+ /** Total publication order within this prepared root. */
129
+ readonly sequence: number
130
+ /** Session id of the prepared root that owns this inspection stream. */
131
+ readonly rootSessionId: string
132
+ /** Runtime instance described by this record. */
133
+ readonly subject: Subject
134
+ }
135
+
136
+ /** Announces allocation of a root or owned process identity. */
137
+ export interface Created extends Base {
138
+ readonly _tag: "Created"
139
+ readonly parent: Subject | undefined
140
+ readonly origin: Origin
141
+ /** Compiled statechart definition; absent for generic `Logic`. */
142
+ readonly definition: Machine.Any | undefined
143
+ }
144
+
145
+ /** Announces successful initialization. */
146
+ export interface Initialized extends Base {
147
+ readonly _tag: "Initialized"
148
+ readonly snapshot: RuntimeSnapshot<unknown, unknown, unknown>
149
+ readonly initialEntryPaths: ReadonlyArray<string>
150
+ readonly microsteps: ReadonlyArray<Microstep>
151
+ }
152
+
153
+ /** Announces failure before an initial runtime snapshot exists. */
154
+ export interface StartFailed extends Base {
155
+ readonly _tag: "StartFailed"
156
+ readonly cause: Cause.Cause<unknown>
157
+ }
158
+
159
+ /** Announces acceptance of an event by a local mailbox. */
160
+ export interface EventSent extends Base {
161
+ readonly _tag: "EventSent"
162
+ readonly deliveryId: number
163
+ readonly source: Subject | undefined
164
+ readonly target: Endpoint
165
+ readonly event: unknown
166
+ readonly causedBy: Causation | undefined
167
+ }
168
+
169
+ /** Announces complete processing of one statechart mailbox event. */
170
+ export interface EventProcessed extends Base {
171
+ readonly _tag: "EventProcessed"
172
+ readonly macrostepId: number
173
+ readonly deliveryId: number
174
+ readonly source: Subject | undefined
175
+ readonly event: unknown
176
+ readonly before: RuntimeSnapshot<unknown, unknown, unknown>
177
+ readonly after: RuntimeSnapshot<unknown, unknown, unknown>
178
+ readonly handled: boolean
179
+ readonly configurationChanged: boolean
180
+ readonly microsteps: ReadonlyArray<Microstep>
181
+ }
182
+
183
+ /** Announces a direct state update made by generic `Logic`. */
184
+ export interface StateChanged extends Base {
185
+ readonly _tag: "StateChanged"
186
+ readonly before: unknown
187
+ readonly after: unknown
188
+ readonly causedByDeliveryId: number | undefined
189
+ }
190
+
191
+ /** Announces actual publication on a machine's domain emission stream. */
192
+ export interface Emitted extends Base {
193
+ readonly _tag: "Emitted"
194
+ readonly emission: unknown
195
+ readonly causedBy: Causation | undefined
196
+ }
197
+
198
+ /**
199
+ * Identity of one run of an invoked program: an Effect, a Stream, or a timer.
200
+ *
201
+ * A program has no state or mailbox of its own, so its records name the machine
202
+ * that owns it as their subject. An invoked child machine or `Logic` process is
203
+ * a subject itself, and its `Created` record has an `Invoke` origin instead.
204
+ */
205
+ export interface ProgramInvocation {
206
+ readonly id: string
207
+ readonly sessionId: string
208
+ readonly owner: Subject
209
+ readonly ownerPath: string
210
+ readonly kind: "Effect" | "Stream" | "Timer"
211
+ }
212
+
213
+ /** Announces an invoked Effect, Stream, or timer starting. */
214
+ export interface ProgramInvocationStarted extends Base {
215
+ readonly _tag: "ProgramInvocationStarted"
216
+ readonly invocation: ProgramInvocation
217
+ }
218
+
219
+ /** Announces the outcome of an invoked Effect, Stream, or timer. */
220
+ export interface ProgramInvocationStopped extends Base {
221
+ readonly _tag: "ProgramInvocationStopped"
222
+ readonly invocation: ProgramInvocation
223
+ /** Success or failure from the invoke; interruption means its owner stopped it. */
224
+ readonly exit: Exit.Exit<unknown, unknown>
225
+ }
226
+
227
+ /** Announces a terminal local runtime snapshot. */
228
+ export interface Terminated extends Base {
229
+ readonly _tag: "Terminated"
230
+ readonly snapshot: RuntimeSnapshot<unknown, unknown, unknown>
231
+ }
232
+
233
+ /** Complete live inspection protocol. */
234
+ export type Event =
235
+ | Created
236
+ | Initialized
237
+ | StartFailed
238
+ | EventSent
239
+ | EventProcessed
240
+ | StateChanged
241
+ | Emitted
242
+ | ProgramInvocationStarted
243
+ | ProgramInvocationStopped
244
+ | Terminated
245
+ }
246
+
247
+ /**
248
+ * A fresh local machine whose observable streams exist before initialization.
249
+ *
250
+ * Subscribe to `emissions` before evaluating `start` when initial-entry
251
+ * emissions must be observed. `start` is one-shot: concurrent and repeated
252
+ * evaluations share the same initialization and running reference, and the
253
+ * `Scope` of the first evaluation owns the machine.
254
+ *
255
+ * @category models
256
+ * @since 0.48.0
257
+ */
258
+ export interface Prepared<out State, in Event, out Error, out Output, out Emitted, out StartError, StartRequirements> {
259
+ /** Stable machine definition id, or a generated fallback when none was declared. */
260
+ readonly id: string
261
+
262
+ /** Unique identity reserved for this prepared machine instance. */
263
+ readonly sessionId: string
264
+
265
+ /** Waits for startup, then streams the initial lifecycle snapshot and later changes. */
266
+ readonly changes: Stream.Stream<RuntimeSnapshot<State, Error, Output>, StartError>
267
+
268
+ /** Streams ephemeral notifications published after subscription. */
269
+ readonly emissions: Stream.Stream<Emitted>
270
+
271
+ /**
272
+ * Streams ordered operational records for this root and every locally owned
273
+ * descendant. The stream is hot, non-replayed, never fails, and completes
274
+ * after the prepared root terminates. Subscribe before evaluating `start` to
275
+ * observe initialization.
276
+ */
277
+ readonly inspection: Stream.Stream<Inspection.Event>
278
+
279
+ /** Initializes this machine once, owned by the first evaluating `Scope`, and returns its running reference. */
280
+ readonly start: Effect.Effect<MachineRef<State, Event, Error, Output, Emitted>, StartError, StartRequirements>
281
+ }
282
+
283
+ /**
284
+ * Prepares a fresh local machine without initializing it.
285
+ *
286
+ * Use this constructor when observation must be composed before initial-entry
287
+ * actions run. Subscribe to `prepared.emissions`, then evaluate
288
+ * `prepared.start`. Ordinary callers can continue to use {@link start}, which
289
+ * starts directly and does not allocate the prepared lifecycle boundary.
290
+ *
291
+ * `prepared.start` is one-shot and scoped: the `Scope` that first evaluates it
292
+ * owns the machine, and closing that `Scope` is equivalent to `ref.stop`.
293
+ *
294
+ * **Example** (Observing emissions before startup)
295
+ *
296
+ * ```ts import.meta.vitest
297
+ * import { LocalMachine, Machine } from "@typeonce/effect-machine"
298
+ * import { Effect, Stream } from "effect"
299
+ *
300
+ * const Emitted = Machine.emittedEvents({ Started: {} })
301
+ * const machine = Machine.make({
302
+ * root: Machine.state({ states: { Idle: {} } }),
303
+ * events: Machine.events({}),
304
+ * emittedEvents: Emitted
305
+ * }).handle({
306
+ * initial: { target: "Idle" },
307
+ * states: {
308
+ * Idle: {
309
+ * entry: (_, enqueue) => {
310
+ * enqueue.emit(Emitted.Started())
311
+ * }
312
+ * }
313
+ * }
314
+ * })
315
+ *
316
+ * const emitted = Effect.scoped(Effect.gen(function*() {
317
+ * const prepared = yield* LocalMachine.prepare(machine)
318
+ * const seen: Array<string> = []
319
+ * yield* prepared.emissions.pipe(
320
+ * Stream.runForEach((event) => Effect.sync(() => seen.push(event._tag))),
321
+ * Effect.forkScoped({ startImmediately: true })
322
+ * )
323
+ * yield* prepared.start
324
+ * yield* Effect.yieldNow
325
+ * return seen
326
+ * }))
327
+ *
328
+ * await Effect.runPromise(emitted) // => ["Started"]
329
+ * ```
330
+ *
331
+ * @category constructors
332
+ * @since 0.48.0
333
+ */
334
+ export const prepare: <
335
+ const States extends Machine.StateSchemas,
336
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
337
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
338
+ const Input extends Schema.Top = typeof Schema.Void,
339
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
340
+ E = never,
341
+ R = never,
342
+ FinalStates extends Machine.StateIdentifier<States> = never,
343
+ Output = never,
344
+ OutputStates extends Machine.StateIdentifier<States> = never,
345
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
346
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
347
+ >(
348
+ machine:
349
+ & Machine<
350
+ States,
351
+ Events,
352
+ Input,
353
+ UnhandledStates,
354
+ E,
355
+ R,
356
+ FinalStates,
357
+ Output,
358
+ Emits,
359
+ OutputStates,
360
+ InputEvents,
361
+ ParentEvents
362
+ >
363
+ & EnsureExecutable<States, UnhandledStates, OutputStates>
364
+ & Machine.RootCompatible<ParentEvents>,
365
+ ...args: [...Machine.InputArgs<Input>]
366
+ ) => Effect.Effect<
367
+ Prepared<
368
+ Machine.Snapshot<States>,
369
+ Machine.EventInputOf<InputEvents>,
370
+ | E
371
+ | InfiniteTransitionError
372
+ | MachineSchemaDecodeError
373
+ | StoppedError,
374
+ Output,
375
+ Machine.EmittedEventOf<Emits>,
376
+ | E
377
+ | InfiniteTransitionError
378
+ | MachineSchemaDecodeError
379
+ | StartupError
380
+ | StoppedError,
381
+ | Scope.Scope
382
+ | RequiredServices<R>
383
+ >
384
+ > = internal.prepare
385
+
386
+ /**
387
+ * Starts a machine in memory, owned by the current `Scope`.
388
+ *
389
+ * **When to use**
390
+ *
391
+ * Use when you want asynchronous event delivery, lifecycle snapshots, `join`,
392
+ * and machine-owned spawned or invoked children.
393
+ *
394
+ * **Details**
395
+ *
396
+ * For each accepted event the runtime plans the complete synchronous
397
+ * macrostep, executes closed machine commands, stops invokes for exited states,
398
+ * publishes the new state, delivers emitted events, and then starts invokes
399
+ * for entered states.
400
+ *
401
+ * An event sent to an idle machine is processed in the sender's turn: work that
402
+ * completes without suspending is reflected by `snapshot` when `send` returns.
403
+ * Work invoked by an entered state also starts in the sender's turn and runs
404
+ * until it first suspends, so browser APIs that require user activation, such as
405
+ * `window.open`, can run in the first step of an invoked Effect. Work that
406
+ * suspends continues in the background. Its outcome is handled in a later turn,
407
+ * even when the work completes without suspending, so the entered state is always
408
+ * observed before the outcome leaves it. An event sent while the machine is
409
+ * processing is queued behind the current work. `changes` subscribers receive
410
+ * every committed snapshot in order once the sender yields, with consecutive
411
+ * sends delivered together.
412
+ *
413
+ * The machine is a scoped resource, like `Effect.forkScoped`. Closing the
414
+ * `Scope` is equivalent to `ref.stop`: active invocations are interrupted,
415
+ * owned children stop, the snapshot becomes `stopped`, and later sends fail
416
+ * with `StoppedError`. An earlier `ref.stop` or terminal completion leaves the
417
+ * finalizer with nothing to do. Own a `Scope` explicitly, for example with
418
+ * `Scope.make` and `Scope.provide`, when the machine must outlive the caller.
419
+ *
420
+ * **Gotchas**
421
+ *
422
+ * The delivery order above belongs to references of local machines. The
423
+ * `Machine.MachineRef` contract promises only delivery.
424
+ *
425
+ * `send` rejects an event that the schema of the machine refuses with
426
+ * `MachineSchemaDecodeError`, and the machine keeps running. Sending after the
427
+ * machine reaches any terminal state fails immediately with `StoppedError`.
428
+ *
429
+ * A transition that throws fails the machine: no sender waits for its event,
430
+ * so the failure is reported through the runtime snapshot, `changes`, and
431
+ * `join`, by the rule that `Machine.MachineRef` states for `send`. An event that another machine, a child or
432
+ * work delivers is not checked at `send`: one that the schema refuses fails the
433
+ * machine when it is planned.
434
+ *
435
+ * **Example** (Starting a machine in a scope)
436
+ *
437
+ * ```ts import.meta.vitest
438
+ * import { LocalMachine, Machine } from "@typeonce/effect-machine"
439
+ * import { Effect } from "effect"
440
+ *
441
+ * const States = Machine.state({ states: { Idle: {} } })
442
+ * const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
443
+ * initial: { target: "Idle" }
444
+ * })
445
+ *
446
+ * const path = Effect.scoped(Effect.gen(function*() {
447
+ * const ref = yield* LocalMachine.start(machine)
448
+ * return (yield* ref.state).state.path
449
+ * }))
450
+ *
451
+ * await Effect.runPromise(path) // => "Idle"
452
+ * ```
453
+ *
454
+ * @see {@link prepare} for observing a machine before it initializes.
455
+ * @see {@link resume} for starting from a decoded snapshot.
456
+ * @category constructors
457
+ * @since 0.48.0
458
+ */
459
+ export const start: <
460
+ const States extends Machine.StateSchemas,
461
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
462
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
463
+ const Input extends Schema.Top = typeof Schema.Void,
464
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
465
+ E = never,
466
+ R = never,
467
+ FinalStates extends Machine.StateIdentifier<States> = never,
468
+ Output = never,
469
+ OutputStates extends Machine.StateIdentifier<States> = never,
470
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
471
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
472
+ >(
473
+ machine:
474
+ & Machine<
475
+ States,
476
+ Events,
477
+ Input,
478
+ UnhandledStates,
479
+ E,
480
+ R,
481
+ FinalStates,
482
+ Output,
483
+ Emits,
484
+ OutputStates,
485
+ InputEvents,
486
+ ParentEvents
487
+ >
488
+ & EnsureExecutable<States, UnhandledStates, OutputStates>
489
+ & Machine.RootCompatible<ParentEvents>,
490
+ ...args: [...Machine.InputArgs<Input>]
491
+ ) => Effect.Effect<
492
+ MachineRef<
493
+ Machine.Snapshot<States>,
494
+ Machine.EventInputOf<InputEvents>,
495
+ | E
496
+ | InfiniteTransitionError
497
+ | MachineSchemaDecodeError
498
+ | StoppedError,
499
+ Output,
500
+ Machine.EmittedEventOf<Emits>
501
+ >,
502
+ | E
503
+ | InfiniteTransitionError
504
+ | MachineSchemaDecodeError
505
+ | StartupError
506
+ | StoppedError,
507
+ | Scope.Scope
508
+ | RequiredServices<R>
509
+ > = internal.start
510
+
511
+ /**
512
+ * Starts a fresh local machine from a decoded logical snapshot, owned by the
513
+ * current `Scope`.
514
+ *
515
+ * **Details**
516
+ *
517
+ * The resumed machine has the same scoped lifetime as {@link start}: closing
518
+ * the `Scope` is equivalent to `ref.stop`.
519
+ *
520
+ * `resume` validates and normalizes the supplied snapshot before publishing it
521
+ * as the first state. It does not construct the declared initial state, replay
522
+ * entry or transition actions, re-deliver raised or emitted events, or
523
+ * re-evaluate historical completion and eventless transitions. Active-state
524
+ * invokes start once in ancestor and document order with `Machine.InitialEvent`;
525
+ * timer invocations restart their complete duration and child-machine
526
+ * invocations start from their own initial state.
527
+ *
528
+ * Only logical state, completion, and history metadata are resumed. Queues,
529
+ * scopes, subscriptions, fibers, spawned children, invoke progress, and prior
530
+ * runtime status are process-local and are not restored. A final snapshot
531
+ * immediately produces a completed ref with its current-machine output.
532
+ *
533
+ * Decode encoded data explicitly with `Machine.decodeSnapshot` before calling
534
+ * this function. Stable snapshots are hosted as supplied; newly enabled
535
+ * eventless or completion transitions in a changed machine definition are not
536
+ * evaluated merely because the runtime was resumed; only ordinary subsequent
537
+ * transition planning can enter and stabilize states.
538
+ *
539
+ * **Example** (Resuming from a decoded snapshot)
540
+ *
541
+ * ```ts import.meta.vitest
542
+ * import { LocalMachine, Machine } from "@typeonce/effect-machine"
543
+ * import { Effect } from "effect"
544
+ *
545
+ * const States = Machine.state({ states: { Idle: {} } })
546
+ * const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
547
+ * initial: { target: "Idle" }
548
+ * })
549
+ *
550
+ * const path = Effect.scoped(Effect.gen(function*() {
551
+ * const initial = yield* Machine.planInitial(machine)
552
+ * const encoded = yield* Machine.encodeSnapshot(machine, initial.state)
553
+ * const snapshot = yield* Machine.decodeSnapshot(machine, encoded)
554
+ * const ref = yield* LocalMachine.resume(machine, snapshot)
555
+ * return (yield* ref.state).state.path
556
+ * }))
557
+ *
558
+ * await Effect.runPromise(path) // => "Idle"
559
+ * ```
560
+ *
561
+ * @see {@link start} for ordinary initial startup.
562
+ * @category constructors
563
+ * @since 0.48.0
564
+ */
565
+ export const resume: <
566
+ const States extends Machine.StateSchemas,
567
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
568
+ const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
569
+ const Input extends Schema.Top = typeof Schema.Void,
570
+ UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
571
+ E = never,
572
+ R = never,
573
+ FinalStates extends Machine.StateIdentifier<States> = never,
574
+ Output = never,
575
+ OutputStates extends Machine.StateIdentifier<States> = never,
576
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
577
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
578
+ >(
579
+ machine:
580
+ & Machine<
581
+ States,
582
+ Events,
583
+ Input,
584
+ UnhandledStates,
585
+ E,
586
+ R,
587
+ FinalStates,
588
+ Output,
589
+ Emits,
590
+ OutputStates,
591
+ InputEvents,
592
+ ParentEvents
593
+ >
594
+ & EnsureExecutable<States, UnhandledStates, OutputStates>
595
+ & Machine.RootCompatible<ParentEvents>,
596
+ snapshot: Machine.Snapshot<States>
597
+ ) => Effect.Effect<
598
+ MachineRef<
599
+ Machine.Snapshot<States>,
600
+ Machine.EventInputOf<InputEvents>,
601
+ | E
602
+ | InfiniteTransitionError
603
+ | MachineSchemaDecodeError
604
+ | StoppedError,
605
+ Output,
606
+ Machine.EmittedEventOf<Emits>
607
+ >,
608
+ MachineSchemaDecodeError,
609
+ | Scope.Scope
610
+ | RequiredServices<R>
611
+ > = internal.resume