@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
@@ -1,4 +1,8 @@
1
- /** Compiled statechart execution strategy. */
1
+ /**
2
+ * The local process: one driver for every logic that LocalMachine runs. A
3
+ * machine drains its mailbox through its compiled execution, and other logic
4
+ * runs as a resident program that receives from the same mailbox.
5
+ */
2
6
  import * as Cause from "effect/Cause"
3
7
  import * as Channel from "effect/Channel"
4
8
  import * as Context from "effect/Context"
@@ -9,58 +13,74 @@ import * as Fiber from "effect/Fiber"
9
13
  import * as Option from "effect/Option"
10
14
  import * as PubSub from "effect/PubSub"
11
15
  import type { SchedulerDispatcher } from "effect/Scheduler"
16
+ import * as Semaphore from "effect/Semaphore"
12
17
  import * as Stream from "effect/Stream"
13
18
  import type * as Take from "effect/Take"
14
- import type { Inspection } from "../../Machine.js"
19
+ import type { Inspection } from "../../LocalMachine.js"
20
+ import type { MachineTarget } from "../../Machine.js"
15
21
  import { type ChildSelector } from "./childRegistry.js"
16
- import { StoppedError } from "./errors.js"
22
+ import { MachineSchemaDecodeError, StoppedError } from "./errors.js"
17
23
  import * as InspectionRuntime from "./inspectionRuntime.js"
24
+ import { classifyOutcome } from "./observation.js"
18
25
  import {
19
26
  type AcknowledgedDelivery,
20
27
  AcknowledgedMessageTypeId,
21
28
  acknowledgedSend,
22
29
  childlessRuntime,
23
30
  type ChildRuntime,
24
- classifyOutcome,
25
31
  closeCompactMailbox,
32
+ closeProcessMailbox,
26
33
  type CompactProcessMailbox,
27
34
  type CompiledProcessContext,
28
35
  type CompiledProcessExecution,
29
36
  type CompiledProcessInitial,
37
+ decodeDelivered,
38
+ decodeSent,
39
+ type Deliverable,
40
+ deliverEvent,
41
+ deliverFromProcess,
42
+ deliverMachineTarget,
30
43
  type EmissionRuntime,
31
44
  EmissionsClosed,
45
+ type EventDecoder,
46
+ executionIsChildless,
32
47
  failAcknowledgedMessage,
33
48
  inspectionSubject,
34
49
  isAcknowledgedMessage,
35
50
  isMachineTarget,
36
51
  type LazyEmissions,
37
52
  type MachineRef,
38
- makeChildRuntime,
39
53
  makeChildRuntimeSync,
40
54
  makeInspectedMessage,
55
+ makeProcessMailbox,
41
56
  messageCausation,
42
57
  messageEvent,
43
58
  noInspectInitial,
44
- noParentSend,
45
59
  notifyActiveSnapshot,
46
60
  offerCompactMailbox,
61
+ offerProcessMailbox,
47
62
  type OwnedChildRuntime,
48
63
  pollCompactMailbox,
49
- type ProcessAddress,
50
- type ProcessContext,
64
+ type ProcessEndpoint,
65
+ type ProcessInitialContext,
51
66
  type ProcessLogic,
67
+ type ProcessMailbox,
52
68
  type ProcessMessage,
69
+ type ProcessRunContext,
53
70
  type ProcessRuntime,
54
- type ProcessScope,
55
71
  publishInspectedSent,
72
+ receiveProcessMailbox,
73
+ RefusedEvent,
56
74
  type RuntimeSnapshot,
57
75
  sendMachineTarget,
76
+ sentOf,
58
77
  snapshotObserver,
59
78
  type StartInternalOptions,
60
79
  type StartProcess,
61
80
  stopAcknowledgedMessage,
62
81
  succeedAcknowledgedMessage,
63
- type VersionedSnapshot
82
+ type VersionedSnapshot,
83
+ WorkMessage
64
84
  } from "./runtimeProtocol.js"
65
85
 
66
86
  const CompiledStoppedCompletion: unique symbol = Symbol("effect/Machine/CompiledStoppedCompletion")
@@ -73,29 +93,78 @@ type CompiledLifecycle = "Active" | "TerminationRequested" | "Completed"
73
93
  type CompiledRunState = "Initializing" | "Idle" | "Draining"
74
94
 
75
95
  /**
76
- * Compact runtime for compiled statecharts.
96
+ * The endpoint of a local process: what its work, its children, and its parent
97
+ * see as `self` or `parent`. Caller-awaited `send` and `sendInspected` reject
98
+ * events refused by the machine's schema and keep the machine running, while
99
+ * `[deliverEvent]` delivers transition commands and internal work reports.
100
+ * Prototype getters and methods keep an endpoint to one reference slot with no
101
+ * eager `stop` Effect or closure.
102
+ */
103
+ class CompiledAddress implements ProcessEndpoint<unknown>, Deliverable {
104
+ constructor(private readonly process: LocalProcess) {}
105
+ get id(): string {
106
+ return this.process.id
107
+ }
108
+ get sessionId(): string {
109
+ return this.process.sessionId
110
+ }
111
+ get inspectionSubject(): Inspection.Subject | undefined {
112
+ return this.process.inspectionSubject
113
+ }
114
+ get stop(): Effect.Effect<void> {
115
+ return Effect.suspend(() => this.process.stopFromProcess())
116
+ }
117
+ send(event: unknown): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> {
118
+ return this.process.send(event)
119
+ }
120
+ sendInspected(
121
+ event: unknown,
122
+ source: Inspection.Subject | undefined,
123
+ causedBy: Inspection.Causation | undefined
124
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> {
125
+ return this.process.sendPublic(event, source, causedBy)
126
+ }
127
+ [deliverEvent](
128
+ event: unknown,
129
+ source: Inspection.Subject | undefined,
130
+ causedBy: Inspection.Causation | undefined
131
+ ): Effect.Effect<void, StoppedError> {
132
+ return this.process.deliver(event, source, causedBy)
133
+ }
134
+ [deliverFromProcess](
135
+ target: MachineTarget<unknown> | ChildSelector,
136
+ event: unknown
137
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> {
138
+ return this.process.deliverTo(target, event)
139
+ }
140
+ }
141
+
142
+ /**
143
+ * A local process.
77
144
  *
78
145
  * All long-lived state is stored directly on this object. Operations are
79
146
  * implemented by shared prototype methods, and public Effect / Stream values
80
- * are materialized only when accessed. Arbitrary process logic continues to
81
- * use the general runtime above.
147
+ * are materialized only when accessed. `MachineProcess` drains a compiled
148
+ * execution, and `LogicProcess` runs other logic as a resident program.
82
149
  */
83
- class CompiledProcess implements MachineRef<any, any, any, any> {
150
+ abstract class LocalProcess implements MachineRef<any, any, any, any> {
84
151
  readonly id: string
85
152
  readonly sessionId: string
86
153
  readonly inspectionSubject?: Inspection.Subject
87
- readonly send: (event: unknown) => Effect.Effect<void, StoppedError>
154
+ readonly send: (event: unknown) => Effect.Effect<void, StoppedError | MachineSchemaDecodeError>
88
155
  sendInspected(
89
156
  event: unknown,
90
157
  source: Inspection.Subject | undefined,
91
158
  causedBy: Inspection.Causation | undefined
92
- ): Effect.Effect<void, StoppedError> {
93
- return this.offerEvent(event, source, causedBy)
159
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> {
160
+ return this.sendPublic(event, source, causedBy)
94
161
  }
95
- [acknowledgedSend](
96
- event: unknown
97
- ): Effect.Effect<AcknowledgedDelivery<unknown>, unknown | StoppedError> {
98
- return this.sendAcknowledgedEffect(event)
162
+ [deliverEvent](
163
+ event: unknown,
164
+ source: Inspection.Subject | undefined,
165
+ causedBy: Inspection.Causation | undefined
166
+ ): Effect.Effect<void, StoppedError> {
167
+ return this.deliver(event, source, causedBy)
99
168
  }
100
169
  [snapshotObserver](observer: () => void): () => void {
101
170
  const observers = this.snapshotObservers ??= new Set()
@@ -105,45 +174,45 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
105
174
  }
106
175
  }
107
176
 
108
- private readonly mailbox: CompactProcessMailbox<unknown> = {
177
+ protected readonly mailbox: CompactProcessMailbox<unknown> = {
109
178
  items: undefined,
110
179
  index: 0,
111
180
  closed: false
112
181
  }
113
- private readonly address: ProcessAddress<unknown>
182
+ protected readonly address: CompiledAddress
114
183
  private childRuntime: ChildRuntime = childlessRuntime
115
- private processScope!: ProcessScope<unknown>
116
- private processContext: ProcessContext<unknown, unknown> | undefined
184
+ protected processScope!: ProcessInitialContext<unknown>
185
+ protected processContext: ProcessRunContext<unknown, unknown> | undefined
117
186
  private compiledContext: CompiledProcessContext<unknown, unknown> | undefined
118
- private current!: VersionedSnapshot<unknown, unknown, unknown>
187
+ protected current!: VersionedSnapshot<unknown, unknown, unknown>
119
188
  /**
120
189
  * `Active` has no terminal payload, `TerminationRequested` owns
121
190
  * `termination` and its optional reserved snapshot, and `Completed` owns
122
191
  * `completion`. `runState` independently tracks worker activity.
123
192
  */
124
- private lifecycle: CompiledLifecycle = "Active"
125
- private runState: CompiledRunState = "Initializing"
193
+ protected lifecycle: CompiledLifecycle = "Active"
194
+ protected runState: CompiledRunState = "Initializing"
126
195
  private completion: CompiledCompletion | undefined
127
196
  private waiter: Deferred.Deferred<unknown, unknown> | undefined
128
197
  private termination: CompiledTermination | undefined
129
198
  private terminationSnapshot: RuntimeSnapshot<unknown, unknown, unknown> | undefined
130
- private worker: Fiber.Fiber<any, never> | undefined
199
+ protected worker: Fiber.Fiber<any, never> | undefined
131
200
  private interruptRequested = false
132
201
  private offerRevision = 0
133
202
  private snapshotObservers: Set<() => void> | undefined
134
203
  private changesFlushScheduled = false
135
- private inFlightMessage: ProcessMessage<unknown> | undefined
204
+ protected inFlightMessage: ProcessMessage<unknown> | undefined
136
205
  private readonly externalEmissions: EmissionRuntime | undefined
137
206
  private emissionsPubSub: LazyEmissions
138
- private readonly activity?: Inspection.Activity
207
+ private readonly programInvocation?: Inspection.ProgramInvocation
139
208
  private initialEntryPaths?: ReadonlyArray<string>
140
209
  private initialMicrosteps?: ReadonlyArray<unknown>
141
210
  private initializing?: boolean
142
211
 
143
212
  constructor(
144
- private readonly logic: ProcessLogic<any, any, any, any, any, any>,
145
- private readonly options: StartInternalOptions,
146
- private readonly services: Context.Context<any>,
213
+ protected readonly logic: ProcessLogic<any, any, any, any, any, any>,
214
+ protected readonly options: StartInternalOptions,
215
+ protected readonly services: Context.Context<any>,
147
216
  sessionId: string
148
217
  ) {
149
218
  this.sessionId = sessionId
@@ -152,31 +221,16 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
152
221
  if (inspector !== undefined) {
153
222
  this.inspectionSubject = inspectionSubject(logic, this.id, sessionId)
154
223
  this.initializing = true
155
- if (options.activity !== undefined) this.activity = { ...options.activity, sessionId }
224
+ if (options.programInvocation !== undefined) this.programInvocation = { ...options.programInvocation, sessionId }
156
225
  }
157
226
  this.externalEmissions = options.emissions
158
227
  this.send = inspector === undefined
159
- ? (event) => this.offerMessage(event)
160
- : (event) => this.offerEvent(event, undefined, undefined)
161
- this.address = {
162
- id: this.id,
163
- sessionId,
164
- stop: Effect.suspend(() => this.stopFromProcess()),
165
- send: this.send,
166
- ...(inspector === undefined
167
- ? undefined
168
- : {
169
- inspectionSubject: this.inspectionSubject!,
170
- sendInspected: (
171
- event: unknown,
172
- source: Inspection.Subject | undefined,
173
- causedBy: Inspection.Causation | undefined
174
- ) => this.offerEvent(event, source, causedBy)
175
- })
176
- }
228
+ ? (event) => this.offerMessage(event, "Sent")
229
+ : (event) => this.sendPublic(event, undefined, undefined)
230
+ this.address = new CompiledAddress(this)
177
231
  }
178
232
 
179
- private get inspector(): InspectionRuntime.Runtime | undefined {
233
+ protected get inspector(): InspectionRuntime.Runtime | undefined {
180
234
  return this.options.runtime.inspection
181
235
  }
182
236
 
@@ -189,7 +243,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
189
243
  const subject = this.inspectionSubject
190
244
  if (inspector === undefined || subject === undefined) return
191
245
  inspector.publishUnsafe(
192
- this.activity === undefined
246
+ this.programInvocation === undefined
193
247
  ? {
194
248
  _tag: "Created",
195
249
  subject,
@@ -197,13 +251,17 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
197
251
  origin: this.options.origin ?? { _tag: "Root" },
198
252
  definition: this.logic.inspection?.definition
199
253
  }
200
- : { _tag: "ActivityStarted", subject: this.activity.owner, activity: this.activity }
254
+ : {
255
+ _tag: "ProgramInvocationStarted",
256
+ subject: this.programInvocation.owner,
257
+ invocation: this.programInvocation
258
+ }
201
259
  )
202
260
  }
203
261
 
204
262
  private publishInitialized(state: unknown): void {
205
263
  this.initializing = false
206
- if (this.activity !== undefined) return
264
+ if (this.programInvocation !== undefined) return
207
265
  const inspector = this.inspector
208
266
  const subject = this.inspectionSubject
209
267
  if (inspector === undefined || subject === undefined) return
@@ -222,54 +280,80 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
222
280
  const subject = this.inspectionSubject
223
281
  if (inspector === undefined || subject === undefined) return
224
282
  inspector.publishUnsafe(
225
- this.activity === undefined
283
+ this.programInvocation === undefined
226
284
  ? { _tag: "StartFailed", subject, cause }
227
285
  : {
228
- _tag: "ActivityStopped",
229
- subject: this.activity.owner,
230
- activity: this.activity,
286
+ _tag: "ProgramInvocationStopped",
287
+ subject: this.programInvocation.owner,
288
+ invocation: this.programInvocation,
231
289
  exit: Exit.failCause(cause)
232
290
  }
233
291
  )
234
292
  }
235
293
 
236
- private get execution(): CompiledProcessExecution<any, any, any, any, any, any> {
294
+ protected get execution(): CompiledProcessExecution<any, any, any, any, any, any> {
237
295
  return this.logic.execution as CompiledProcessExecution<any, any, any, any, any, any>
238
296
  }
239
297
 
298
+ /** Decodes a public event as it enters the mailbox; only a machine decodes. */
299
+ protected get decodeEvent(): EventDecoder | undefined {
300
+ const execution = this.logic.execution
301
+ return execution?._tag === "Compiled" ? execution.decodeEvent : undefined
302
+ }
303
+
304
+ protected get childless(): boolean {
305
+ return executionIsChildless(this.logic.execution)
306
+ }
307
+
308
+ /** The children of this process, as its logic spawns them. */
309
+ protected abstract makeChildRuntime(): ChildRuntime
310
+
311
+ /** Enqueues a message that the mailbox accepts. */
312
+ protected enqueue(message: ProcessMessage<unknown>): void {
313
+ offerCompactMailbox(this.mailbox, message)
314
+ }
315
+
316
+ protected closeMailbox(): void {
317
+ closeCompactMailbox(this.mailbox)
318
+ }
319
+
320
+ protected prepareCompiledContext(initialized: object): void {
321
+ this.compiledContext = new CompiledProcessContextImpl(this.processScope, this.childRuntime.owned, this)
322
+ if ("executionState" in initialized) {
323
+ this.compiledContext.executionState = (initialized as { readonly executionState: unknown }).executionState
324
+ }
325
+ }
326
+
327
+ /** Prepares how the process runs once its initial state is known. */
328
+ protected abstract prepareRun(initialized: object): void
329
+
330
+ /** Runs the process once it is initialized and its startup hooks ran. */
331
+ protected abstract run(): Effect.Effect<void, never, any>
332
+
333
+ /** The run state of an initialized process that does not run yet. */
334
+ protected abstract get initializedRunState(): CompiledRunState
335
+
240
336
  initializeCompiledSync(): Effect.Effect<MachineRef<any, any, any, any>, unknown> {
241
337
  this.publishCreated()
242
- if (!this.execution.childless) {
243
- this.childRuntime = makeChildRuntimeSync(
244
- this.address,
245
- this.options.runtime,
246
- this.services,
247
- (event) => this.sendAcknowledgedEffect(event)
248
- )
249
- }
338
+ const childless = this.childless
339
+ if (!childless) this.childRuntime = this.makeChildRuntime()
250
340
  const parent = this.options.parent
251
- const sendParent = this.options.sendParent ?? (parent === undefined
252
- ? noParentSend
253
- : this.inspector === undefined
254
- ? parent.send
255
- : (event: unknown) => sendMachineTarget(parent, event, this.inspectionSubject, this.causation()))
256
341
  this.processScope = {
257
342
  self: this.address,
258
343
  parent,
259
344
  spawn: this.childRuntime.spawn,
260
- sendParent,
261
345
  emit: (event) => this.emitEvent(event),
262
- sendTo: ((target: unknown, event: unknown) =>
346
+ sendTo: (target: unknown, event: unknown) =>
263
347
  isMachineTarget(target)
264
348
  ? this.inspector === undefined
265
- ? target.send(event)
349
+ ? sendMachineTarget(target, event, undefined, undefined)
266
350
  : sendMachineTarget(target, event, this.inspectionSubject, this.causation())
267
351
  : this.childRuntime.sendTo(
268
352
  target as ChildSelector,
269
353
  event,
270
354
  this.inspectionSubject,
271
355
  this.causation()
272
- )),
356
+ ),
273
357
  stopChild: this.childRuntime.stop,
274
358
  failCause: (cause: Cause.Cause<unknown>) => this.failCause(cause),
275
359
  inspectInitial: this.inspector === undefined
@@ -296,10 +380,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
296
380
  snapshot: { status: "active", state: initialized.state }
297
381
  }
298
382
  this.publishInitialized(initialized.state)
299
- this.compiledContext = new CompiledProcessContextImpl(this.processScope, this.childRuntime.owned, this)
300
- if ("executionState" in initialized) {
301
- this.compiledContext.executionState = initialized.executionState
302
- }
383
+ this.prepareCompiledContext(initialized)
303
384
  if (this.options.onReadySync !== undefined && !this.options.onReadySync(this)) {
304
385
  this.requestTerminationSync({ _tag: "Stopped" })
305
386
  }
@@ -307,7 +388,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
307
388
  this.requestTerminationSync({ _tag: "Done", output: initialized.output })
308
389
  }
309
390
  if (
310
- initialized.done === false && this.execution.childless &&
391
+ initialized.done === false && childless &&
311
392
  this.lifecycle === "Active" &&
312
393
  this.mailbox.items === undefined
313
394
  ) {
@@ -321,37 +402,24 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
321
402
  const self = this
322
403
  return Effect.gen(function*() {
323
404
  self.publishCreated()
324
- if (!self.execution.childless) {
325
- self.childRuntime = yield* makeChildRuntime(
326
- self.address,
327
- self.options.runtime,
328
- self.services,
329
- (event) => self.sendAcknowledgedEffect(event)
330
- )
331
- }
405
+ if (!self.childless) self.childRuntime = self.makeChildRuntime()
332
406
  const parent = self.options.parent
333
- const sendParent = self.options.sendParent ?? (parent === undefined
334
- ? noParentSend
335
- : self.inspector === undefined
336
- ? parent.send
337
- : (event: unknown) => sendMachineTarget(parent, event, self.inspectionSubject, self.causation()))
338
407
  self.processScope = {
339
408
  self: self.address,
340
409
  parent,
341
410
  spawn: self.childRuntime.spawn,
342
- sendParent,
343
411
  emit: (event) => self.emitEvent(event),
344
- sendTo: ((target: unknown, event: unknown) =>
412
+ sendTo: (target: unknown, event: unknown) =>
345
413
  isMachineTarget(target)
346
414
  ? self.inspector === undefined
347
- ? target.send(event)
415
+ ? sendMachineTarget(target, event, undefined, undefined)
348
416
  : sendMachineTarget(target, event, self.inspectionSubject, self.causation())
349
417
  : self.childRuntime.sendTo(
350
418
  target as ChildSelector,
351
419
  event,
352
420
  self.inspectionSubject,
353
421
  self.causation()
354
- )),
422
+ ),
355
423
  stopChild: self.childRuntime.stop,
356
424
  failCause: (cause: Cause.Cause<unknown>) => self.failCause(cause),
357
425
  inspectInitial: self.inspector === undefined
@@ -364,7 +432,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
364
432
 
365
433
  const cleanupStartupFailure = <A, E>(exit: Exit.Exit<A, E>): Effect.Effect<void> =>
366
434
  Exit.isFailure(exit) ? self.childRuntime.close(exit) : Effect.void
367
- const compiledInitial = self.execution.initial
435
+ const compiledInitial = self.logic.execution?._tag === "Compiled" ? self.execution.initial : undefined
368
436
  const initializeEffect: Effect.Effect<
369
437
  {
370
438
  readonly state: unknown
@@ -384,7 +452,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
384
452
  return cleanupStartupFailure(exit)
385
453
  }),
386
454
  Effect.ensuring(Effect.sync(() => {
387
- self.runState = "Idle"
455
+ self.runState = self.initializedRunState
388
456
  }))
389
457
  )
390
458
  const initial = initialized.state
@@ -395,24 +463,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
395
463
  snapshot: { status: "active", state: initial }
396
464
  }
397
465
  self.publishInitialized(initial)
398
- if (self.execution.drain._tag === "Process") {
399
- self.processContext = {
400
- ...self.processScope,
401
- receive: Effect.never,
402
- poll: Effect.sync(() => Option.map(pollCompactMailbox(self.mailbox), messageEvent)),
403
- receiveMessage: Effect.never,
404
- pollMessage: Effect.sync(() => self.pollCompiledMessage()),
405
- completeMessage: (delivery) => self.completeCompiledMessage(delivery),
406
- state: Effect.sync(() => self.current.snapshot.state),
407
- setState: (state: unknown) => self.setActiveState(state),
408
- updateState: (f) => self.updateState(f)
409
- }
410
- } else {
411
- self.compiledContext = new CompiledProcessContextImpl(self.processScope, self.childRuntime.owned, self)
412
- if ("executionState" in initialized) {
413
- self.compiledContext.executionState = initialized.executionState
414
- }
415
- }
466
+ self.prepareRun(initialized)
416
467
 
417
468
  if (self.options.onReadySync !== undefined && !self.options.onReadySync(self)) {
418
469
  yield* self.requestTermination({ _tag: "Stopped" })
@@ -432,14 +483,13 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
432
483
  // first drain to perform. Future sends observe an idle run state and
433
484
  // start the ordinary compiled worker.
434
485
  if (
435
- initialized.done === false && self.execution.childless &&
486
+ initialized.done === false && self.childless &&
436
487
  self.lifecycle === "Active" && self.mailbox.items === undefined
437
488
  ) {
438
489
  return self
439
490
  }
440
491
 
441
- self.runState = "Draining"
442
- yield* self.drainRuntime()
492
+ yield* self.run()
443
493
  return self
444
494
  })
445
495
  }
@@ -482,7 +532,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
482
532
  return this.childRuntime.changes(child)
483
533
  }
484
534
 
485
- private requestTermination(requested: CompiledTermination): Effect.Effect<boolean> {
535
+ protected requestTermination(requested: CompiledTermination): Effect.Effect<boolean> {
486
536
  return Effect.sync(() => this.requestTerminationSync(requested))
487
537
  }
488
538
 
@@ -490,7 +540,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
490
540
  return this.lifecycle === "TerminationRequested"
491
541
  }
492
542
 
493
- private requestTerminationSync(requested: CompiledTermination): boolean {
543
+ protected requestTerminationSync(requested: CompiledTermination): boolean {
494
544
  if (this.lifecycle !== "Active") {
495
545
  return false
496
546
  }
@@ -511,12 +561,12 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
511
561
  ? { status: "stopped", state: latest.snapshot.state }
512
562
  : requested._tag === "Done"
513
563
  ? { status: "done", state: latest.snapshot.state, output: requested.output }
514
- : { status: "error", state: latest.snapshot.state, cause: requested.cause }
564
+ : { status: "failed", state: latest.snapshot.state, cause: requested.cause }
515
565
  this.current = { ...latest, terminalizing: true }
516
566
  return snapshot
517
567
  }
518
568
 
519
- private stopFromProcess(): Effect.Effect<void> {
569
+ stopFromProcess(): Effect.Effect<void> {
520
570
  const request = this.requestTermination({ _tag: "Stopped" }).pipe(Effect.asVoid)
521
571
  return this.runState === "Initializing" ? request : request.pipe(Effect.andThen(Effect.interrupt))
522
572
  }
@@ -532,13 +582,64 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
532
582
  )
533
583
  }
534
584
 
535
- private sendAcknowledgedEffect(
585
+ /**
586
+ * A caller-awaited `send`: an event that the schema refuses is rejected, and
587
+ * the machine keeps running. A machine that has ended rejects first.
588
+ */
589
+ sendPublic(
590
+ event: unknown,
591
+ source: Inspection.Subject | undefined,
592
+ causedBy: Inspection.Causation | undefined
593
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> {
594
+ if (this.inspector === undefined) return this.offerMessage(event, "Sent")
595
+ return Effect.suspend((): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> => {
596
+ if (this.mailbox.closed || this.lifecycle !== "Active") return Effect.fail(new StoppedError())
597
+ const decoded = decodeSent(this.decodeEvent, event)
598
+ return decoded._tag === "Rejected"
599
+ ? Effect.fail(decoded.error)
600
+ : this.offerEvent(decoded.event, source, causedBy, undefined, event)
601
+ })
602
+ }
603
+
604
+ /** Delivers a transition command from this process to a target or child. */
605
+ deliverTo(
606
+ target: MachineTarget<unknown> | ChildSelector,
536
607
  event: unknown
608
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> {
609
+ return isMachineTarget(target)
610
+ ? this.inspector === undefined
611
+ ? deliverMachineTarget(target, event, undefined, undefined)
612
+ : deliverMachineTarget(target, event, this.inspectionSubject, this.causation())
613
+ : this.childRuntime.deliverTo(
614
+ target,
615
+ event,
616
+ this.inspectionSubject,
617
+ this.causation()
618
+ )
619
+ }
620
+
621
+ /** A transition-command or internal-work delivery. */
622
+ deliver(
623
+ event: unknown,
624
+ source: Inspection.Subject | undefined,
625
+ causedBy: Inspection.Causation | undefined
626
+ ): Effect.Effect<void, StoppedError> {
627
+ if (this.inspector === undefined) {
628
+ return this.offerMessage(event, "Delivered") as Effect.Effect<void, StoppedError>
629
+ }
630
+ return Effect.suspend(() =>
631
+ this.offerEvent(decodeDelivered(this.decodeEvent, event), source, causedBy, undefined, sentOf(event))
632
+ )
633
+ }
634
+
635
+ protected sendAcknowledgedEffect(
636
+ event: unknown,
637
+ sent: unknown = event
537
638
  ): Effect.Effect<AcknowledgedDelivery<unknown>, unknown | StoppedError> {
538
639
  return Effect.uninterruptibleMask((restore) =>
539
640
  Deferred.make<AcknowledgedDelivery<unknown>, unknown>().pipe(
540
641
  Effect.flatMap((deferred) =>
541
- this.offerEvent(event, undefined, undefined, deferred).pipe(
642
+ this.offerEvent(event, undefined, undefined, deferred, sent).pipe(
542
643
  Effect.andThen(restore(Deferred.await(deferred)))
543
644
  )
544
645
  )
@@ -550,50 +651,76 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
550
651
  event: unknown,
551
652
  source: Inspection.Subject | undefined,
552
653
  causedBy: Inspection.Causation | undefined,
553
- deferred?: Deferred.Deferred<AcknowledgedDelivery<unknown>, unknown>
654
+ deferred: Deferred.Deferred<AcknowledgedDelivery<unknown>, unknown> | undefined,
655
+ sent: unknown
554
656
  ): Effect.Effect<void, StoppedError> {
555
657
  return Effect.suspend(() => {
556
658
  const inspector = this.inspector
557
659
  const subject = this.inspectionSubject
558
- const message: ProcessMessage<unknown> = inspector?.isActive() === true && subject !== undefined
660
+ const message: ProcessMessage<unknown> = inspector?.isActive() === true && subject !== undefined &&
661
+ !(event instanceof WorkMessage && sent === undefined)
559
662
  ? makeInspectedMessage(
560
663
  inspector,
561
664
  subject,
562
665
  event,
563
666
  source,
564
667
  causedBy,
565
- deferred
668
+ deferred,
669
+ sent
566
670
  )
567
671
  : deferred === undefined
568
672
  ? event
569
673
  : { [AcknowledgedMessageTypeId]: true as const, event, deferred }
570
- return this.offerMessage(message)
674
+ return this.offerMessage(message) as Effect.Effect<void, StoppedError>
571
675
  })
572
676
  }
573
677
 
574
- private offerMessage(message: ProcessMessage<unknown>): Effect.Effect<void, StoppedError> {
678
+ /**
679
+ * Offers a message. Without an inspector, `send` and a delivery offer the
680
+ * event as it was sent, and it is decoded here, once the machine is known to
681
+ * run: `"Sent"` rejects a refused event, and `"Delivered"` lets the drain
682
+ * fail the machine with it.
683
+ */
684
+ private offerMessage(
685
+ offered: ProcessMessage<unknown>,
686
+ decoding: "Decoded" | "Sent" | "Delivered" = "Decoded"
687
+ ): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> {
575
688
  return Effect.uninterruptible(
576
- Effect.withFiber((fiber) => {
689
+ Effect.withFiber((fiber): Effect.Effect<void, StoppedError | MachineSchemaDecodeError> => {
577
690
  if (this.mailbox.closed || this.lifecycle !== "Active") {
578
691
  return Effect.fail(new StoppedError())
579
692
  }
580
- const inspector = this.inspector
693
+ const execution = this.logic.execution
694
+ const compiled = execution?._tag === "Compiled" ? execution : undefined
695
+ let message = offered
696
+ if (decoding !== "Decoded") {
697
+ const decode = compiled?.decodeEvent
698
+ if (decode !== undefined && (decoding === "Sent" || !(offered instanceof WorkMessage))) {
699
+ // Decoded inline, so a send allocates nothing more than before.
700
+ try {
701
+ message = decode(offered)
702
+ } catch (error) {
703
+ if (decoding === "Sent" && error instanceof MachineSchemaDecodeError) return Effect.fail(error)
704
+ message = new RefusedEvent(error)
705
+ }
706
+ }
707
+ }
581
708
  const subject = this.inspectionSubject
582
- if (inspector !== undefined && subject !== undefined) publishInspectedSent(inspector, subject, message)
709
+ if (subject !== undefined) {
710
+ const inspector = this.inspector
711
+ if (inspector !== undefined) publishInspectedSent(inspector, subject, message)
712
+ }
583
713
  this.offerRevision += 1
584
714
  // Initialization drains whatever it queued once it finishes, and an
585
715
  // idle machine has an empty mailbox, so it receives the message directly.
586
- const drain = this.execution.drain
587
- const context = this.compiledContext
588
- const runSync = this.runState === "Idle" && context !== undefined && drain._tag === "Owned"
589
- ? drain.runSync
590
- : undefined
591
- if (runSync === undefined) {
592
- offerCompactMailbox(this.mailbox, message)
593
- }
594
716
  if (this.runState !== "Idle") {
717
+ this.enqueue(message)
595
718
  return Effect.void
596
719
  }
720
+ const context = this.compiledContext
721
+ const runSync = context !== undefined && compiled !== undefined && compiled.drain._tag === "Owned"
722
+ ? compiled.drain.runSync
723
+ : undefined
597
724
  // An idle machine drains in the sender's turn, so a transition that
598
725
  // completes synchronously is committed before `send` returns. Sends
599
726
  // that arrive while draining only enqueue.
@@ -613,6 +740,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
613
740
  this.scheduleChangesFlush(fiber.currentDispatcher)
614
741
  return Effect.void
615
742
  }
743
+ this.enqueue(message)
616
744
  return this.startWorker(undefined)
617
745
  })
618
746
  )
@@ -676,7 +804,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
676
804
 
677
805
  private finishIdleChildlessStop(): boolean {
678
806
  if (
679
- !this.execution.childless || this.runState !== "Idle" || this.worker !== undefined ||
807
+ !this.childless || this.runState !== "Idle" || this.worker !== undefined ||
680
808
  this.lifecycle !== "Active" ||
681
809
  (this.options.onOutcome !== undefined && this.options.skipStoppedOutcome !== true) ||
682
810
  this.options.onStop !== undefined ||
@@ -694,7 +822,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
694
822
  this.lifecycle = "TerminationRequested"
695
823
  this.termination = { _tag: "Stopped" }
696
824
  this.terminationSnapshot = snapshot
697
- closeCompactMailbox(this.mailbox)
825
+ this.closeMailbox()
698
826
  this.current = {
699
827
  revision: this.current.revision + 1,
700
828
  terminalizing: true,
@@ -718,7 +846,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
718
846
  return true
719
847
  }
720
848
 
721
- private settleRequestedTermination(): Effect.Effect<void> {
849
+ protected settleRequestedTermination(): Effect.Effect<void> {
722
850
  return Effect.suspend(() => {
723
851
  if (this.runState !== "Draining") {
724
852
  return this.finishRequestedTermination()
@@ -760,7 +888,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
760
888
  return stopped
761
889
  }
762
890
 
763
- private drainRuntime(
891
+ protected drainRuntime(
764
892
  first?: Effect.Effect<Option.Option<unknown>, unknown, unknown>
765
893
  ): Effect.Effect<void, never, any> {
766
894
  const self = this
@@ -813,19 +941,21 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
813
941
  )
814
942
  }
815
943
 
816
- private finishRequestedTermination(): Effect.Effect<void> {
944
+ protected finishRequestedTermination(): Effect.Effect<void> {
817
945
  return Effect.suspend(() => {
818
946
  if (this.lifecycle !== "TerminationRequested") {
819
947
  return Effect.void
820
948
  }
821
949
  const requested = this.termination
822
950
  if (requested === undefined) {
823
- return Effect.void
951
+ return this.awaitCompletion()
824
952
  }
825
953
  const snapshot = this.terminationSnapshot ?? this.reserveTermination(requested)
826
954
  if (snapshot === undefined) {
827
955
  return this.awaitCompletion()
828
956
  }
957
+ // One caller finishes the process; any other awaits its completion.
958
+ this.termination = undefined
829
959
  const exit = requested._tag === "Stopped"
830
960
  ? Exit.void
831
961
  : requested._tag === "Done"
@@ -850,12 +980,12 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
850
980
  : this.closeEmissions().pipe(
851
981
  Effect.andThen(Effect.sync(() =>
852
982
  inspector.publishUnsafe(
853
- this.activity === undefined
983
+ this.programInvocation === undefined
854
984
  ? { _tag: "Terminated", subject, snapshot }
855
985
  : {
856
- _tag: "ActivityStopped",
857
- subject: this.activity.owner,
858
- activity: this.activity,
986
+ _tag: "ProgramInvocationStopped",
987
+ subject: this.programInvocation.owner,
988
+ invocation: this.programInvocation,
859
989
  exit: snapshot.status === "stopped" ? Exit.interrupt() : exit
860
990
  }
861
991
  )
@@ -863,7 +993,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
863
993
  )
864
994
  return Effect.uninterruptible(
865
995
  Effect.sync(() => {
866
- closeCompactMailbox(this.mailbox)
996
+ this.closeMailbox()
867
997
  }).pipe(
868
998
  Effect.andThen(this.childRuntime.close(exit)),
869
999
  Effect.andThen(this.setAndPublishSnapshot(snapshot)),
@@ -1004,7 +1134,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
1004
1134
  })
1005
1135
  }
1006
1136
 
1007
- private setActiveState(state: unknown): Effect.Effect<void> {
1137
+ protected setActiveState(state: unknown): Effect.Effect<void> {
1008
1138
  return Effect.suspend(() => this.commitActiveState(state) ?? Effect.void)
1009
1139
  }
1010
1140
 
@@ -1016,8 +1146,13 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
1016
1146
  return message
1017
1147
  }
1018
1148
 
1019
- beginCompiledMessage(message: ProcessMessage<unknown>): void {
1020
- this.inFlightMessage = isAcknowledgedMessage(message) ? message : undefined
1149
+ beginCompiledMessage(message: ProcessMessage<unknown>): boolean {
1150
+ if (isAcknowledgedMessage(message)) {
1151
+ this.inFlightMessage = message
1152
+ return true
1153
+ }
1154
+ this.inFlightMessage = undefined
1155
+ return false
1021
1156
  }
1022
1157
 
1023
1158
  completeCompiledMessage(delivery: AcknowledgedDelivery<unknown>): void {
@@ -1033,7 +1168,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
1033
1168
  return this.commitActiveState(state, true)
1034
1169
  }
1035
1170
 
1036
- private commitActiveState(state: unknown, batchChanges = false): Effect.Effect<void> | undefined {
1171
+ protected commitActiveState(state: unknown, batchChanges = false): Effect.Effect<void> | undefined {
1037
1172
  const latest = this.current
1038
1173
  if (latest.terminalizing || latest.snapshot.status !== "active") {
1039
1174
  return undefined
@@ -1106,7 +1241,7 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
1106
1241
  PubSub.publishUnsafe(current.changes, pendingChanges)
1107
1242
  }
1108
1243
 
1109
- private updateState<E, R>(
1244
+ protected updateState<E, R>(
1110
1245
  f: (state: unknown) => Effect.Effect<unknown, E, R>
1111
1246
  ): Effect.Effect<void, E, R> {
1112
1247
  return Effect.suspend(() => {
@@ -1184,13 +1319,201 @@ class CompiledProcess implements MachineRef<any, any, any, any> {
1184
1319
  }
1185
1320
  }
1186
1321
 
1322
+ /** A machine: its compiled execution drains its mailbox. */
1323
+ class MachineProcess extends LocalProcess {
1324
+ constructor(
1325
+ logic: ProcessLogic<any, any, any, any, any, any>,
1326
+ options: StartInternalOptions,
1327
+ services: Context.Context<any>,
1328
+ sessionId: string
1329
+ ) {
1330
+ super(logic, options, services, sessionId)
1331
+ }
1332
+
1333
+ protected override get childless(): boolean {
1334
+ return this.execution.childless
1335
+ }
1336
+
1337
+ [acknowledgedSend](
1338
+ event: unknown
1339
+ ): Effect.Effect<AcknowledgedDelivery<unknown>, unknown | StoppedError> {
1340
+ return Effect.suspend(() => {
1341
+ if (this.mailbox.closed || this.lifecycle !== "Active") return Effect.fail(new StoppedError())
1342
+ const decoded = decodeSent(this.decodeEvent, event)
1343
+ return decoded._tag === "Rejected"
1344
+ ? Effect.fail(decoded.error)
1345
+ : this.sendAcknowledgedEffect(decoded.event, event)
1346
+ })
1347
+ }
1348
+
1349
+ protected get initializedRunState(): CompiledRunState {
1350
+ return "Idle"
1351
+ }
1352
+
1353
+ protected makeChildRuntime(): ChildRuntime {
1354
+ return makeChildRuntimeSync(
1355
+ this.address,
1356
+ this.options.runtime,
1357
+ this.services,
1358
+ (event) => this.sendAcknowledgedEffect(decodeDelivered(this.decodeEvent, event), sentOf(event))
1359
+ )
1360
+ }
1361
+
1362
+ protected prepareRun(initialized: object): void {
1363
+ if (this.execution.drain._tag === "Process") {
1364
+ this.processContext = {
1365
+ ...this.processScope,
1366
+ receive: Effect.never,
1367
+ poll: Effect.sync(() => Option.map(pollCompactMailbox(this.mailbox), messageEvent)),
1368
+ receiveMessage: Effect.never,
1369
+ pollMessage: Effect.sync(() => this.pollCompiledMessage()),
1370
+ completeMessage: (delivery) => this.completeCompiledMessage(delivery),
1371
+ state: Effect.sync(() => this.current.snapshot.state),
1372
+ setState: (state: unknown) => this.setActiveState(state),
1373
+ updateState: (f) => this.updateState(f)
1374
+ }
1375
+ } else {
1376
+ this.prepareCompiledContext(initialized)
1377
+ }
1378
+ }
1379
+
1380
+ protected run(): Effect.Effect<void, never, any> {
1381
+ this.runState = "Draining"
1382
+ return this.drainRuntime()
1383
+ }
1384
+ }
1385
+
1386
+ /**
1387
+ * Logic other than a machine, run as a resident program: it starts in the
1388
+ * turn of its starter, receives from the mailbox, and its exit ends the
1389
+ * process. Its state is updated one change at a time.
1390
+ */
1391
+ class LogicProcess extends LocalProcess {
1392
+ // A resident program runs from the end of its startup until it exits, so
1393
+ // the process never rests idle and every send only enqueues.
1394
+ protected get initializedRunState(): CompiledRunState {
1395
+ return "Initializing"
1396
+ }
1397
+ protected override readonly mailbox: ProcessMailbox<unknown> = makeProcessMailbox()
1398
+ private updates: Semaphore.Semaphore | undefined
1399
+
1400
+ protected makeChildRuntime(): ChildRuntime {
1401
+ return makeChildRuntimeSync(this.address, this.options.runtime)
1402
+ }
1403
+
1404
+ protected override enqueue(message: ProcessMessage<unknown>): void {
1405
+ offerProcessMailbox(this.mailbox, message)
1406
+ }
1407
+
1408
+ protected override closeMailbox(): void {
1409
+ closeProcessMailbox(this.mailbox)
1410
+ }
1411
+
1412
+ /**
1413
+ * Initialization finishes constructing a state before a stopped snapshot can
1414
+ * be published, so a stop requested there is recorded and returns. Once
1415
+ * running, the process that stops itself never continues: the program is
1416
+ * interrupted, and the caller waits until it is.
1417
+ */
1418
+ override stopFromProcess(): Effect.Effect<void> {
1419
+ const request = this.requestTermination({ _tag: "Stopped" }).pipe(Effect.asVoid)
1420
+ return this.runState === "Initializing"
1421
+ ? request
1422
+ : request.pipe(
1423
+ Effect.andThen(Effect.forkDetach(this.settleRequestedTermination())),
1424
+ Effect.andThen(Effect.never)
1425
+ )
1426
+ }
1427
+
1428
+ protected prepareRun(): void {
1429
+ const inspected = this.inspector !== undefined
1430
+ const receive = receiveProcessMailbox(this.mailbox)
1431
+ const poll = Effect.sync(() => pollCompactMailbox(this.mailbox))
1432
+ this.processContext = {
1433
+ ...this.processScope,
1434
+ receive: inspected
1435
+ ? Effect.map(receive, (message) => messageEvent(this.inFlightMessage = message))
1436
+ : Effect.map(receive, messageEvent),
1437
+ poll: inspected
1438
+ ? Effect.map(poll, (message) => {
1439
+ if (Option.isSome(message)) this.inFlightMessage = message.value
1440
+ return Option.map(message, messageEvent)
1441
+ })
1442
+ : Effect.map(poll, Option.map(messageEvent)),
1443
+ state: Effect.sync(() => this.current.snapshot.state),
1444
+ setState: (state: unknown) => this.serialized(Effect.suspend(() => this.changeState(state))),
1445
+ updateState: (f) =>
1446
+ this.serialized(Effect.suspend(() => {
1447
+ const observed = this.current
1448
+ if (observed.terminalizing || observed.snapshot.status !== "active") return Effect.void
1449
+ return Effect.flatMap(f(observed.snapshot.state), (state) => this.changeState(state))
1450
+ }))
1451
+ }
1452
+ }
1453
+
1454
+ /** Changes of state wait for an update in progress, as one change at a time. */
1455
+ private serialized<A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> {
1456
+ return (this.updates ??= Semaphore.makeUnsafe(1)).withPermits(1)(effect)
1457
+ }
1458
+
1459
+ /** Commits a state, reported as a change of state of logic that is not a machine. */
1460
+ private changeState(state: unknown): Effect.Effect<void> {
1461
+ const before = this.current
1462
+ const published = this.commitActiveState(state)
1463
+ const inspector = this.inspector
1464
+ if (
1465
+ inspector !== undefined && this.current !== before && this.logic.inspection?.kind !== "Machine"
1466
+ ) {
1467
+ const message = this.inFlightMessage
1468
+ inspector.publishUnsafe({
1469
+ _tag: "StateChanged",
1470
+ subject: this.inspectionSubject!,
1471
+ before: before.snapshot.state,
1472
+ after: state,
1473
+ causedByDeliveryId: message !== undefined && isAcknowledgedMessage(message)
1474
+ ? message.inspection?.deliveryId
1475
+ : undefined
1476
+ })
1477
+ }
1478
+ return published ?? Effect.void
1479
+ }
1480
+
1481
+ protected run(): Effect.Effect<void, never, any> {
1482
+ return Effect.suspend(() => {
1483
+ if (this.lifecycle !== "Active") return this.finishRequestedTermination()
1484
+ this.runState = "Draining"
1485
+ const program = Effect.uninterruptibleMask((restore) =>
1486
+ restore(Effect.suspend(() => this.logic.run(this.processContext!))).pipe(
1487
+ Effect.exit,
1488
+ Effect.flatMap((exit) => {
1489
+ this.requestTerminationSync(
1490
+ Exit.isFailure(exit) ? { _tag: "Failure", cause: exit.cause } : { _tag: "Done", output: exit.value }
1491
+ )
1492
+ return this.finishRequestedTermination()
1493
+ })
1494
+ )
1495
+ )
1496
+ // The program starts in the caller's turn and runs until it first
1497
+ // suspends, so a process started while an event is processed begins its
1498
+ // work before that event's sender continues.
1499
+ const fork = this.options.detached === true
1500
+ ? Effect.forkDetach(program, { startImmediately: true })
1501
+ : Effect.forkChild(program, { startImmediately: true })
1502
+ return Effect.flatMap(fork, (fiber) =>
1503
+ Effect.sync(() => {
1504
+ if (fiber.pollUnsafe() === undefined) this.worker = fiber
1505
+ }))
1506
+ })
1507
+ }
1508
+ }
1509
+
1187
1510
  class CompiledProcessContextImpl implements CompiledProcessContext<unknown, unknown> {
1188
1511
  executionState: unknown
1189
1512
 
1190
1513
  constructor(
1191
- readonly scope: ProcessScope<unknown>,
1514
+ readonly scope: ProcessInitialContext<unknown>,
1192
1515
  readonly ownedChildren: OwnedChildRuntime,
1193
- private readonly process: CompiledProcess
1516
+ private readonly process: LocalProcess
1194
1517
  ) {}
1195
1518
 
1196
1519
  poll(): Option.Option<unknown> {
@@ -1201,8 +1524,8 @@ class CompiledProcessContextImpl implements CompiledProcessContext<unknown, unkn
1201
1524
  return this.process.pollCompiledMessage()
1202
1525
  }
1203
1526
 
1204
- beginMessage(message: ProcessMessage<unknown>): void {
1205
- this.process.beginCompiledMessage(message)
1527
+ beginMessage(message: ProcessMessage<unknown>): boolean {
1528
+ return this.process.beginCompiledMessage(message)
1206
1529
  }
1207
1530
 
1208
1531
  state(): unknown {
@@ -1227,14 +1550,16 @@ export const startCompactCompiledInternal: StartProcess = Effect.fnUntraced(func
1227
1550
  logic: ProcessLogic<any, any, any, any, any, any>,
1228
1551
  options: StartInternalOptions
1229
1552
  ) {
1230
- const sessionId = options.sessionId ?? (yield* options.runtime.nextSessionId)
1553
+ const sessionId = options.sessionId ?? options.runtime.nextSessionIdSync()
1231
1554
  const services = yield* Effect.context<any>()
1232
- const execution = logic.execution as CompiledProcessExecution<any, any, any, any, any, any>
1233
- const process = new CompiledProcess(logic, options, services, sessionId)
1555
+ const execution = logic.execution?._tag === "Compiled" ? logic.execution : undefined
1556
+ const process = logic.execution?._tag === "Compiled"
1557
+ ? new MachineProcess(logic, options, services, sessionId)
1558
+ : new LogicProcess(logic, options, services, sessionId)
1234
1559
  // A compiled initializer is synchronous by construction. Only startup
1235
1560
  // callbacks that themselves return Effects need the generic initialization
1236
1561
  // program; the compiled drain is still provided the complete service context.
1237
- const initialize = execution.initialSync !== undefined &&
1562
+ const initialize = execution?.initialSync !== undefined &&
1238
1563
  options.onReady === undefined && options.onSnapshot === undefined
1239
1564
  ? process.initializeCompiledSync()
1240
1565
  : process.initialize()
@@ -1255,7 +1580,7 @@ export const startCompactCompiledInternal: StartProcess = Effect.fnUntraced(func
1255
1580
 
1256
1581
  /** Preserves synchronous startup for owned compiled children. */
1257
1582
  export const startCompiledSync: ProcessRuntime["startCompiledSync"] = (logic, options, services, sessionId) =>
1258
- new CompiledProcess(logic, options, services, sessionId).initializeCompiledSync()
1583
+ new MachineProcess(logic, options, services, sessionId).initializeCompiledSync()
1259
1584
 
1260
1585
  type CompiledTermination =
1261
1586
  | { readonly _tag: "Stopped" }