@typeonce/effect-machine 0.47.1 → 0.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (334) hide show
  1. package/AGENTS.md +374 -22
  2. package/CLAUDE.md +374 -22
  3. package/README.md +56 -7
  4. package/ai-docs/src/10_modeling/01_create-a-machine.ts +3 -3
  5. package/ai-docs/src/10_modeling/index.md +64 -17
  6. package/ai-docs/src/20_root-api/index.md +2 -2
  7. package/ai-docs/src/35_cluster/index.md +213 -0
  8. package/ai-docs/src/37_new-host/index.md +90 -0
  9. package/dist/LocalMachine.d.ts +417 -0
  10. package/dist/LocalMachine.d.ts.map +1 -0
  11. package/dist/LocalMachine.js +193 -0
  12. package/dist/LocalMachine.js.map +1 -0
  13. package/dist/Machine.d.ts +1120 -812
  14. package/dist/Machine.d.ts.map +1 -1
  15. package/dist/Machine.js +195 -186
  16. package/dist/Machine.js.map +1 -1
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +1 -0
  20. package/dist/index.js.map +1 -1
  21. package/dist/internal/machine/atom.d.ts +8 -10
  22. package/dist/internal/machine/atom.d.ts.map +1 -1
  23. package/dist/internal/machine/atom.js +7 -6
  24. package/dist/internal/machine/atom.js.map +1 -1
  25. package/dist/internal/machine/capability.d.ts +101 -0
  26. package/dist/internal/machine/capability.d.ts.map +1 -0
  27. package/dist/internal/machine/capability.js +70 -0
  28. package/dist/internal/machine/capability.js.map +1 -0
  29. package/dist/internal/machine/{invocationDefinition.d.ts → capturedInvocation.d.ts} +11 -11
  30. package/dist/internal/machine/capturedInvocation.d.ts.map +1 -0
  31. package/dist/internal/machine/{invocationDefinition.js → capturedInvocation.js} +2 -2
  32. package/dist/internal/machine/capturedInvocation.js.map +1 -0
  33. package/dist/internal/machine/child.d.ts +14 -0
  34. package/dist/internal/machine/child.d.ts.map +1 -0
  35. package/dist/internal/machine/child.js +19 -0
  36. package/dist/internal/machine/child.js.map +1 -0
  37. package/dist/internal/machine/childRegistry.d.ts +0 -2
  38. package/dist/internal/machine/childRegistry.d.ts.map +1 -1
  39. package/dist/internal/machine/childRegistry.js +2 -2
  40. package/dist/internal/machine/childRegistry.js.map +1 -1
  41. package/dist/internal/machine/cluster.d.ts +19 -4
  42. package/dist/internal/machine/cluster.d.ts.map +1 -1
  43. package/dist/internal/machine/cluster.js +263 -110
  44. package/dist/internal/machine/cluster.js.map +1 -1
  45. package/dist/internal/machine/clusterChild.d.ts +66 -0
  46. package/dist/internal/machine/clusterChild.d.ts.map +1 -0
  47. package/dist/internal/machine/clusterChild.js +105 -0
  48. package/dist/internal/machine/clusterChild.js.map +1 -0
  49. package/dist/internal/machine/clusterFailure.d.ts +14 -0
  50. package/dist/internal/machine/clusterFailure.d.ts.map +1 -0
  51. package/dist/internal/machine/clusterFailure.js +44 -0
  52. package/dist/internal/machine/clusterFailure.js.map +1 -0
  53. package/dist/internal/machine/clusterInstance.d.ts +194 -0
  54. package/dist/internal/machine/clusterInstance.d.ts.map +1 -0
  55. package/dist/internal/machine/clusterInstance.js +1076 -0
  56. package/dist/internal/machine/clusterInstance.js.map +1 -0
  57. package/dist/internal/machine/clusterKeepAlive.d.ts +47 -0
  58. package/dist/internal/machine/clusterKeepAlive.d.ts.map +1 -0
  59. package/dist/internal/machine/clusterKeepAlive.js +72 -0
  60. package/dist/internal/machine/clusterKeepAlive.js.map +1 -0
  61. package/dist/internal/machine/clusterProgress.d.ts +73 -0
  62. package/dist/internal/machine/clusterProgress.d.ts.map +1 -0
  63. package/dist/internal/machine/clusterProgress.js +116 -0
  64. package/dist/internal/machine/clusterProgress.js.map +1 -0
  65. package/dist/internal/machine/clusterProtocol.d.ts +37 -4
  66. package/dist/internal/machine/clusterProtocol.d.ts.map +1 -1
  67. package/dist/internal/machine/clusterProtocol.js +18 -4
  68. package/dist/internal/machine/clusterProtocol.js.map +1 -1
  69. package/dist/internal/machine/clusterProxy.d.ts +46 -0
  70. package/dist/internal/machine/clusterProxy.d.ts.map +1 -0
  71. package/dist/internal/machine/clusterProxy.js +150 -0
  72. package/dist/internal/machine/clusterProxy.js.map +1 -0
  73. package/dist/internal/machine/clusterRead.d.ts +61 -0
  74. package/dist/internal/machine/clusterRead.d.ts.map +1 -0
  75. package/dist/internal/machine/clusterRead.js +108 -0
  76. package/dist/internal/machine/clusterRead.js.map +1 -0
  77. package/dist/internal/machine/clusterSqlStorage.d.ts +16 -0
  78. package/dist/internal/machine/clusterSqlStorage.d.ts.map +1 -0
  79. package/dist/internal/machine/clusterSqlStorage.js +138 -0
  80. package/dist/internal/machine/clusterSqlStorage.js.map +1 -0
  81. package/dist/internal/machine/clusterStorageCheck.d.ts +19 -0
  82. package/dist/internal/machine/clusterStorageCheck.d.ts.map +1 -0
  83. package/dist/internal/machine/clusterStorageCheck.js +186 -0
  84. package/dist/internal/machine/clusterStorageCheck.js.map +1 -0
  85. package/dist/internal/machine/clusterTarget.d.ts +74 -0
  86. package/dist/internal/machine/clusterTarget.d.ts.map +1 -0
  87. package/dist/internal/machine/clusterTarget.js +81 -0
  88. package/dist/internal/machine/clusterTarget.js.map +1 -0
  89. package/dist/internal/machine/clusterTimer.d.ts +21 -0
  90. package/dist/internal/machine/clusterTimer.d.ts.map +1 -0
  91. package/dist/internal/machine/clusterTimer.js +50 -0
  92. package/dist/internal/machine/clusterTimer.js.map +1 -0
  93. package/dist/internal/machine/clusterWake.d.ts +61 -0
  94. package/dist/internal/machine/clusterWake.d.ts.map +1 -0
  95. package/dist/internal/machine/clusterWake.js +81 -0
  96. package/dist/internal/machine/clusterWake.js.map +1 -0
  97. package/dist/internal/machine/clusterWork.d.ts +130 -0
  98. package/dist/internal/machine/clusterWork.d.ts.map +1 -0
  99. package/dist/internal/machine/clusterWork.js +191 -0
  100. package/dist/internal/machine/clusterWork.js.map +1 -0
  101. package/dist/internal/machine/command.d.ts +4 -4
  102. package/dist/internal/machine/command.d.ts.map +1 -1
  103. package/dist/internal/machine/commandRuntime.d.ts +8 -6
  104. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  105. package/dist/internal/machine/commandRuntime.js +1 -5
  106. package/dist/internal/machine/commandRuntime.js.map +1 -1
  107. package/dist/internal/machine/configuration.d.ts +0 -13
  108. package/dist/internal/machine/configuration.d.ts.map +1 -1
  109. package/dist/internal/machine/configuration.js +0 -151
  110. package/dist/internal/machine/configuration.js.map +1 -1
  111. package/dist/internal/machine/errors.d.ts +11 -2
  112. package/dist/internal/machine/errors.d.ts.map +1 -1
  113. package/dist/internal/machine/errors.js +11 -2
  114. package/dist/internal/machine/errors.js.map +1 -1
  115. package/dist/internal/machine/executionPlan.d.ts +45 -3
  116. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  117. package/dist/internal/machine/executionPlan.js +328 -191
  118. package/dist/internal/machine/executionPlan.js.map +1 -1
  119. package/dist/internal/machine/implementation.d.ts +6 -6
  120. package/dist/internal/machine/implementation.d.ts.map +1 -1
  121. package/dist/internal/machine/inspectionRuntime.d.ts +1 -1
  122. package/dist/internal/machine/inspectionRuntime.d.ts.map +1 -1
  123. package/dist/internal/machine/invocationChanges.d.ts +60 -0
  124. package/dist/internal/machine/invocationChanges.d.ts.map +1 -0
  125. package/dist/internal/machine/invocationChanges.js +54 -0
  126. package/dist/internal/machine/invocationChanges.js.map +1 -0
  127. package/dist/internal/machine/{activities.d.ts → invocationDefinitions.d.ts} +8 -8
  128. package/dist/internal/machine/invocationDefinitions.d.ts.map +1 -0
  129. package/dist/internal/machine/{activities.js → invocationDefinitions.js} +5 -5
  130. package/dist/internal/machine/invocationDefinitions.js.map +1 -0
  131. package/dist/internal/machine/invocationEvent.d.ts +16 -7
  132. package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
  133. package/dist/internal/machine/invocationEvent.js +26 -12
  134. package/dist/internal/machine/invocationEvent.js.map +1 -1
  135. package/dist/internal/machine/invocationOutcome.d.ts +89 -0
  136. package/dist/internal/machine/invocationOutcome.d.ts.map +1 -0
  137. package/dist/internal/machine/invocationOutcome.js +76 -0
  138. package/dist/internal/machine/invocationOutcome.js.map +1 -0
  139. package/dist/internal/machine/invocationResolution.d.ts +137 -0
  140. package/dist/internal/machine/invocationResolution.d.ts.map +1 -0
  141. package/dist/internal/machine/invocationResolution.js +150 -0
  142. package/dist/internal/machine/invocationResolution.js.map +1 -0
  143. package/dist/internal/machine/logicScope.d.ts +24 -0
  144. package/dist/internal/machine/logicScope.d.ts.map +1 -0
  145. package/dist/internal/machine/logicScope.js +33 -0
  146. package/dist/internal/machine/logicScope.js.map +1 -0
  147. package/dist/internal/machine/machine.d.ts +17 -23
  148. package/dist/internal/machine/machine.d.ts.map +1 -1
  149. package/dist/internal/machine/machine.js +24 -40
  150. package/dist/internal/machine/machine.js.map +1 -1
  151. package/dist/internal/machine/observation.d.ts +3 -0
  152. package/dist/internal/machine/observation.d.ts.map +1 -1
  153. package/dist/internal/machine/observation.js +56 -2
  154. package/dist/internal/machine/observation.js.map +1 -1
  155. package/dist/internal/machine/plannedSnapshot.d.ts.map +1 -1
  156. package/dist/internal/machine/plannedSnapshot.js +11 -20
  157. package/dist/internal/machine/plannedSnapshot.js.map +1 -1
  158. package/dist/internal/machine/planner.d.ts +36 -23
  159. package/dist/internal/machine/planner.d.ts.map +1 -1
  160. package/dist/internal/machine/planner.js +15 -8
  161. package/dist/internal/machine/planner.js.map +1 -1
  162. package/dist/internal/machine/process.d.ts +17 -9
  163. package/dist/internal/machine/process.d.ts.map +1 -1
  164. package/dist/internal/machine/process.js +43 -503
  165. package/dist/internal/machine/process.js.map +1 -1
  166. package/dist/internal/machine/protocol.d.ts +1 -1
  167. package/dist/internal/machine/protocol.d.ts.map +1 -1
  168. package/dist/internal/machine/protocol.js +48 -10
  169. package/dist/internal/machine/protocol.js.map +1 -1
  170. package/dist/internal/machine/runtime.d.ts +20 -16
  171. package/dist/internal/machine/runtime.d.ts.map +1 -1
  172. package/dist/internal/machine/runtime.js +93 -88
  173. package/dist/internal/machine/runtime.js.map +1 -1
  174. package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -1
  175. package/dist/internal/machine/runtimeCompiled.js +359 -120
  176. package/dist/internal/machine/runtimeCompiled.js.map +1 -1
  177. package/dist/internal/machine/runtimeProtocol.d.ts +151 -82
  178. package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
  179. package/dist/internal/machine/runtimeProtocol.js +236 -219
  180. package/dist/internal/machine/runtimeProtocol.js.map +1 -1
  181. package/dist/internal/machine/stateDefinition.d.ts +0 -1
  182. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  183. package/dist/internal/machine/stateDefinition.js +0 -1
  184. package/dist/internal/machine/stateDefinition.js.map +1 -1
  185. package/dist/internal/machine/step.d.ts +486 -0
  186. package/dist/internal/machine/step.d.ts.map +1 -0
  187. package/dist/internal/machine/step.js +957 -0
  188. package/dist/internal/machine/step.js.map +1 -0
  189. package/dist/internal/machine/stepHost.d.ts +33 -0
  190. package/dist/internal/machine/stepHost.d.ts.map +1 -0
  191. package/dist/internal/machine/stepHost.js +501 -0
  192. package/dist/internal/machine/stepHost.js.map +1 -0
  193. package/dist/internal/machine/symbols.d.ts +2 -11
  194. package/dist/internal/machine/symbols.d.ts.map +1 -1
  195. package/dist/internal/machine/symbols.js +2 -2
  196. package/dist/internal/machine/symbols.js.map +1 -1
  197. package/dist/internal/machine/transition.d.ts +7 -7
  198. package/dist/internal/machine/transition.d.ts.map +1 -1
  199. package/dist/internal/testing/machine/check.d.ts.map +1 -1
  200. package/dist/internal/testing/machine/check.js +217 -25
  201. package/dist/internal/testing/machine/check.js.map +1 -1
  202. package/dist/internal/testing/machine/exploration.d.ts +19 -2
  203. package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
  204. package/dist/internal/testing/machine/exploration.js +14 -3
  205. package/dist/internal/testing/machine/exploration.js.map +1 -1
  206. package/dist/internal/testing/machine/format.js.map +1 -1
  207. package/dist/internal/testing/machine/probe.d.ts.map +1 -1
  208. package/dist/internal/testing/machine/probe.js +1 -1
  209. package/dist/internal/testing/machine/probe.js.map +1 -1
  210. package/dist/internal/testing/machine/runtime.d.ts +2 -2
  211. package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
  212. package/dist/internal/testing/machine/runtime.js +10 -3
  213. package/dist/internal/testing/machine/runtime.js.map +1 -1
  214. package/dist/internal/testing/machine/trace.d.ts +38 -3
  215. package/dist/internal/testing/machine/trace.d.ts.map +1 -1
  216. package/dist/internal/testing/machine/trace.js +180 -33
  217. package/dist/internal/testing/machine/trace.js.map +1 -1
  218. package/dist/internal/testing/machine/transitionCoverage.d.ts +1 -1
  219. package/dist/internal/testing/machine/transitionCoverage.d.ts.map +1 -1
  220. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  221. package/dist/internal/testing/machine/verification.js +9 -1
  222. package/dist/internal/testing/machine/verification.js.map +1 -1
  223. package/dist/internal/testing/machine/work.d.ts +47 -0
  224. package/dist/internal/testing/machine/work.d.ts.map +1 -0
  225. package/dist/internal/testing/machine/work.js +74 -0
  226. package/dist/internal/testing/machine/work.js.map +1 -0
  227. package/dist/testing/MachineTest.d.ts +253 -37
  228. package/dist/testing/MachineTest.d.ts.map +1 -1
  229. package/dist/testing/MachineTest.js +81 -9
  230. package/dist/testing/MachineTest.js.map +1 -1
  231. package/dist/unstable/cluster/ClusterMachine.d.ts +1071 -41
  232. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  233. package/dist/unstable/cluster/ClusterMachine.js +441 -12
  234. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  235. package/dist/unstable/cluster/ClusterMachineProxy.d.ts +257 -0
  236. package/dist/unstable/cluster/ClusterMachineProxy.d.ts.map +1 -0
  237. package/dist/unstable/cluster/ClusterMachineProxy.js +73 -0
  238. package/dist/unstable/cluster/ClusterMachineProxy.js.map +1 -0
  239. package/dist/unstable/cluster/SqlMachineStorage.d.ts +125 -0
  240. package/dist/unstable/cluster/SqlMachineStorage.d.ts.map +1 -0
  241. package/dist/unstable/cluster/SqlMachineStorage.js +62 -0
  242. package/dist/unstable/cluster/SqlMachineStorage.js.map +1 -0
  243. package/dist/unstable/cluster/index.d.ts +13 -0
  244. package/dist/unstable/cluster/index.d.ts.map +1 -1
  245. package/dist/unstable/cluster/index.js +13 -0
  246. package/dist/unstable/cluster/index.js.map +1 -1
  247. package/dist/unstable/reactivity/AtomMachine.d.ts +21 -26
  248. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  249. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  250. package/package.json +5 -3
  251. package/src/LocalMachine.ts +611 -0
  252. package/src/Machine.ts +1379 -1334
  253. package/src/index.ts +1 -0
  254. package/src/internal/machine/atom.ts +20 -49
  255. package/src/internal/machine/capability.ts +123 -0
  256. package/src/internal/machine/{invocationDefinition.ts → capturedInvocation.ts} +13 -13
  257. package/src/internal/machine/child.ts +25 -0
  258. package/src/internal/machine/childRegistry.ts +1 -4
  259. package/src/internal/machine/cluster.ts +420 -205
  260. package/src/internal/machine/clusterChild.ts +117 -0
  261. package/src/internal/machine/clusterFailure.ts +53 -0
  262. package/src/internal/machine/clusterInstance.ts +1779 -0
  263. package/src/internal/machine/clusterKeepAlive.ts +93 -0
  264. package/src/internal/machine/clusterProgress.ts +159 -0
  265. package/src/internal/machine/clusterProtocol.ts +46 -4
  266. package/src/internal/machine/clusterProxy.ts +233 -0
  267. package/src/internal/machine/clusterRead.ts +207 -0
  268. package/src/internal/machine/clusterSqlStorage.ts +181 -0
  269. package/src/internal/machine/clusterStorageCheck.ts +274 -0
  270. package/src/internal/machine/clusterTarget.ts +154 -0
  271. package/src/internal/machine/clusterTimer.ts +58 -0
  272. package/src/internal/machine/clusterWake.ts +100 -0
  273. package/src/internal/machine/clusterWork.ts +269 -0
  274. package/src/internal/machine/command.ts +4 -4
  275. package/src/internal/machine/commandRuntime.ts +12 -24
  276. package/src/internal/machine/configuration.ts +0 -181
  277. package/src/internal/machine/errors.ts +11 -2
  278. package/src/internal/machine/executionPlan.ts +493 -263
  279. package/src/internal/machine/implementation.ts +6 -6
  280. package/src/internal/machine/inspectionRuntime.ts +1 -1
  281. package/src/internal/machine/invocationChanges.ts +89 -0
  282. package/src/internal/machine/{activities.ts → invocationDefinitions.ts} +10 -10
  283. package/src/internal/machine/invocationEvent.ts +35 -12
  284. package/src/internal/machine/invocationOutcome.ts +145 -0
  285. package/src/internal/machine/invocationResolution.ts +307 -0
  286. package/src/internal/machine/logicScope.ts +47 -0
  287. package/src/internal/machine/machine.ts +35 -100
  288. package/src/internal/machine/observation.ts +67 -2
  289. package/src/internal/machine/plannedSnapshot.ts +17 -20
  290. package/src/internal/machine/planner.ts +75 -99
  291. package/src/internal/machine/process.ts +78 -722
  292. package/src/internal/machine/protocol.ts +70 -26
  293. package/src/internal/machine/runtime.ts +198 -195
  294. package/src/internal/machine/runtimeCompiled.ts +496 -171
  295. package/src/internal/machine/runtimeProtocol.ts +416 -305
  296. package/src/internal/machine/stateDefinition.ts +0 -5
  297. package/src/internal/machine/step.ts +1518 -0
  298. package/src/internal/machine/stepHost.ts +634 -0
  299. package/src/internal/machine/symbols.ts +3 -14
  300. package/src/internal/machine/transition.ts +11 -11
  301. package/src/internal/testing/machine/check.ts +240 -36
  302. package/src/internal/testing/machine/exploration.ts +42 -3
  303. package/src/internal/testing/machine/format.ts +1 -1
  304. package/src/internal/testing/machine/probe.ts +2 -1
  305. package/src/internal/testing/machine/runtime.ts +15 -5
  306. package/src/internal/testing/machine/trace.ts +261 -41
  307. package/src/internal/testing/machine/transitionCoverage.ts +1 -1
  308. package/src/internal/testing/machine/verification.ts +11 -5
  309. package/src/internal/testing/machine/work.ts +101 -0
  310. package/src/testing/MachineTest.ts +268 -44
  311. package/src/unstable/cluster/ClusterMachine.ts +1183 -59
  312. package/src/unstable/cluster/ClusterMachineProxy.ts +372 -0
  313. package/src/unstable/cluster/SqlMachineStorage.ts +136 -0
  314. package/src/unstable/cluster/index.ts +15 -0
  315. package/src/unstable/reactivity/AtomMachine.ts +27 -88
  316. package/dist/internal/machine/activities.d.ts.map +0 -1
  317. package/dist/internal/machine/activities.js.map +0 -1
  318. package/dist/internal/machine/invocation.d.ts +0 -33
  319. package/dist/internal/machine/invocation.d.ts.map +0 -1
  320. package/dist/internal/machine/invocation.js +0 -220
  321. package/dist/internal/machine/invocation.js.map +0 -1
  322. package/dist/internal/machine/invocationDefinition.d.ts.map +0 -1
  323. package/dist/internal/machine/invocationDefinition.js.map +0 -1
  324. package/dist/internal/machine/requirements.d.ts +0 -4
  325. package/dist/internal/machine/requirements.d.ts.map +0 -1
  326. package/dist/internal/machine/requirements.js +0 -2
  327. package/dist/internal/machine/requirements.js.map +0 -1
  328. package/dist/internal/machine/runtimeGeneric.d.ts +0 -3
  329. package/dist/internal/machine/runtimeGeneric.d.ts.map +0 -1
  330. package/dist/internal/machine/runtimeGeneric.js +0 -465
  331. package/dist/internal/machine/runtimeGeneric.js.map +0 -1
  332. package/src/internal/machine/invocation.ts +0 -376
  333. package/src/internal/machine/requirements.ts +0 -11
  334. package/src/internal/machine/runtimeGeneric.ts +0 -797
@@ -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";
@@ -8,21 +12,61 @@ import * as Exit from "effect/Exit";
8
12
  import * as Fiber from "effect/Fiber";
9
13
  import * as Option from "effect/Option";
10
14
  import * as PubSub from "effect/PubSub";
15
+ import * as Semaphore from "effect/Semaphore";
11
16
  import * as Stream from "effect/Stream";
12
17
  import {} from "./childRegistry.js";
13
- import { StoppedError } from "./errors.js";
18
+ import { MachineSchemaDecodeError, StoppedError } from "./errors.js";
14
19
  import * as InspectionRuntime from "./inspectionRuntime.js";
15
- import { AcknowledgedMessageTypeId, acknowledgedSend, childlessRuntime, classifyOutcome, closeCompactMailbox, EmissionsClosed, failAcknowledgedMessage, inspectionSubject, isAcknowledgedMessage, isMachineTarget, makeChildRuntime, makeChildRuntimeSync, makeInspectedMessage, messageCausation, messageEvent, noInspectInitial, noParentSend, notifyActiveSnapshot, offerCompactMailbox, pollCompactMailbox, publishInspectedSent, sendMachineTarget, snapshotObserver, stopAcknowledgedMessage, succeedAcknowledgedMessage } from "./runtimeProtocol.js";
20
+ import { classifyOutcome } from "./observation.js";
21
+ import { AcknowledgedMessageTypeId, acknowledgedSend, childlessRuntime, closeCompactMailbox, closeProcessMailbox, decodeDelivered, decodeSent, deliverEvent, deliverFromProcess, deliverMachineTarget, EmissionsClosed, executionIsChildless, failAcknowledgedMessage, inspectionSubject, isAcknowledgedMessage, isMachineTarget, makeChildRuntimeSync, makeInspectedMessage, makeProcessMailbox, messageCausation, messageEvent, noInspectInitial, notifyActiveSnapshot, offerCompactMailbox, offerProcessMailbox, pollCompactMailbox, publishInspectedSent, receiveProcessMailbox, RefusedEvent, sendMachineTarget, sentOf, snapshotObserver, stopAcknowledgedMessage, succeedAcknowledgedMessage, WorkMessage } from "./runtimeProtocol.js";
16
22
  const CompiledStoppedCompletion = Symbol("effect/Machine/CompiledStoppedCompletion");
17
23
  /**
18
- * Compact runtime for compiled statecharts.
24
+ * The endpoint of a local process: what its work, its children, and its parent
25
+ * see as `self` or `parent`. Caller-awaited `send` and `sendInspected` reject
26
+ * events refused by the machine's schema and keep the machine running, while
27
+ * `[deliverEvent]` delivers transition commands and internal work reports.
28
+ * Prototype getters and methods keep an endpoint to one reference slot with no
29
+ * eager `stop` Effect or closure.
30
+ */
31
+ class CompiledAddress {
32
+ process;
33
+ constructor(process) {
34
+ this.process = process;
35
+ }
36
+ get id() {
37
+ return this.process.id;
38
+ }
39
+ get sessionId() {
40
+ return this.process.sessionId;
41
+ }
42
+ get inspectionSubject() {
43
+ return this.process.inspectionSubject;
44
+ }
45
+ get stop() {
46
+ return Effect.suspend(() => this.process.stopFromProcess());
47
+ }
48
+ send(event) {
49
+ return this.process.send(event);
50
+ }
51
+ sendInspected(event, source, causedBy) {
52
+ return this.process.sendPublic(event, source, causedBy);
53
+ }
54
+ [deliverEvent](event, source, causedBy) {
55
+ return this.process.deliver(event, source, causedBy);
56
+ }
57
+ [deliverFromProcess](target, event) {
58
+ return this.process.deliverTo(target, event);
59
+ }
60
+ }
61
+ /**
62
+ * A local process.
19
63
  *
20
64
  * All long-lived state is stored directly on this object. Operations are
21
65
  * implemented by shared prototype methods, and public Effect / Stream values
22
- * are materialized only when accessed. Arbitrary process logic continues to
23
- * use the general runtime above.
66
+ * are materialized only when accessed. `MachineProcess` drains a compiled
67
+ * execution, and `LogicProcess` runs other logic as a resident program.
24
68
  */
25
- class CompiledProcess {
69
+ class LocalProcess {
26
70
  logic;
27
71
  options;
28
72
  services;
@@ -31,10 +75,10 @@ class CompiledProcess {
31
75
  inspectionSubject;
32
76
  send;
33
77
  sendInspected(event, source, causedBy) {
34
- return this.offerEvent(event, source, causedBy);
78
+ return this.sendPublic(event, source, causedBy);
35
79
  }
36
- [acknowledgedSend](event) {
37
- return this.sendAcknowledgedEffect(event);
80
+ [deliverEvent](event, source, causedBy) {
81
+ return this.deliver(event, source, causedBy);
38
82
  }
39
83
  [snapshotObserver](observer) {
40
84
  const observers = this.snapshotObservers ??= new Set();
@@ -73,7 +117,7 @@ class CompiledProcess {
73
117
  inFlightMessage;
74
118
  externalEmissions;
75
119
  emissionsPubSub;
76
- activity;
120
+ programInvocation;
77
121
  initialEntryPaths;
78
122
  initialMicrosteps;
79
123
  initializing;
@@ -87,25 +131,14 @@ class CompiledProcess {
87
131
  if (inspector !== undefined) {
88
132
  this.inspectionSubject = inspectionSubject(logic, this.id, sessionId);
89
133
  this.initializing = true;
90
- if (options.activity !== undefined)
91
- this.activity = { ...options.activity, sessionId };
134
+ if (options.programInvocation !== undefined)
135
+ this.programInvocation = { ...options.programInvocation, sessionId };
92
136
  }
93
137
  this.externalEmissions = options.emissions;
94
138
  this.send = inspector === undefined
95
- ? (event) => this.offerMessage(event)
96
- : (event) => this.offerEvent(event, undefined, undefined);
97
- this.address = {
98
- id: this.id,
99
- sessionId,
100
- stop: Effect.suspend(() => this.stopFromProcess()),
101
- send: this.send,
102
- ...(inspector === undefined
103
- ? undefined
104
- : {
105
- inspectionSubject: this.inspectionSubject,
106
- sendInspected: (event, source, causedBy) => this.offerEvent(event, source, causedBy)
107
- })
108
- };
139
+ ? (event) => this.offerMessage(event, "Sent")
140
+ : (event) => this.sendPublic(event, undefined, undefined);
141
+ this.address = new CompiledAddress(this);
109
142
  }
110
143
  get inspector() {
111
144
  return this.options.runtime.inspection;
@@ -118,7 +151,7 @@ class CompiledProcess {
118
151
  const subject = this.inspectionSubject;
119
152
  if (inspector === undefined || subject === undefined)
120
153
  return;
121
- inspector.publishUnsafe(this.activity === undefined
154
+ inspector.publishUnsafe(this.programInvocation === undefined
122
155
  ? {
123
156
  _tag: "Created",
124
157
  subject,
@@ -126,11 +159,15 @@ class CompiledProcess {
126
159
  origin: this.options.origin ?? { _tag: "Root" },
127
160
  definition: this.logic.inspection?.definition
128
161
  }
129
- : { _tag: "ActivityStarted", subject: this.activity.owner, activity: this.activity });
162
+ : {
163
+ _tag: "ProgramInvocationStarted",
164
+ subject: this.programInvocation.owner,
165
+ invocation: this.programInvocation
166
+ });
130
167
  }
131
168
  publishInitialized(state) {
132
169
  this.initializing = false;
133
- if (this.activity !== undefined)
170
+ if (this.programInvocation !== undefined)
134
171
  return;
135
172
  const inspector = this.inspector;
136
173
  const subject = this.inspectionSubject;
@@ -150,40 +187,55 @@ class CompiledProcess {
150
187
  const subject = this.inspectionSubject;
151
188
  if (inspector === undefined || subject === undefined)
152
189
  return;
153
- inspector.publishUnsafe(this.activity === undefined
190
+ inspector.publishUnsafe(this.programInvocation === undefined
154
191
  ? { _tag: "StartFailed", subject, cause }
155
192
  : {
156
- _tag: "ActivityStopped",
157
- subject: this.activity.owner,
158
- activity: this.activity,
193
+ _tag: "ProgramInvocationStopped",
194
+ subject: this.programInvocation.owner,
195
+ invocation: this.programInvocation,
159
196
  exit: Exit.failCause(cause)
160
197
  });
161
198
  }
162
199
  get execution() {
163
200
  return this.logic.execution;
164
201
  }
202
+ /** Decodes a public event as it enters the mailbox; only a machine decodes. */
203
+ get decodeEvent() {
204
+ const execution = this.logic.execution;
205
+ return execution?._tag === "Compiled" ? execution.decodeEvent : undefined;
206
+ }
207
+ get childless() {
208
+ return executionIsChildless(this.logic.execution);
209
+ }
210
+ /** Enqueues a message that the mailbox accepts. */
211
+ enqueue(message) {
212
+ offerCompactMailbox(this.mailbox, message);
213
+ }
214
+ closeMailbox() {
215
+ closeCompactMailbox(this.mailbox);
216
+ }
217
+ prepareCompiledContext(initialized) {
218
+ this.compiledContext = new CompiledProcessContextImpl(this.processScope, this.childRuntime.owned, this);
219
+ if ("executionState" in initialized) {
220
+ this.compiledContext.executionState = initialized.executionState;
221
+ }
222
+ }
165
223
  initializeCompiledSync() {
166
224
  this.publishCreated();
167
- if (!this.execution.childless) {
168
- this.childRuntime = makeChildRuntimeSync(this.address, this.options.runtime, this.services, (event) => this.sendAcknowledgedEffect(event));
169
- }
225
+ const childless = this.childless;
226
+ if (!childless)
227
+ this.childRuntime = this.makeChildRuntime();
170
228
  const parent = this.options.parent;
171
- const sendParent = this.options.sendParent ?? (parent === undefined
172
- ? noParentSend
173
- : this.inspector === undefined
174
- ? parent.send
175
- : (event) => sendMachineTarget(parent, event, this.inspectionSubject, this.causation()));
176
229
  this.processScope = {
177
230
  self: this.address,
178
231
  parent,
179
232
  spawn: this.childRuntime.spawn,
180
- sendParent,
181
233
  emit: (event) => this.emitEvent(event),
182
- sendTo: ((target, event) => isMachineTarget(target)
234
+ sendTo: (target, event) => isMachineTarget(target)
183
235
  ? this.inspector === undefined
184
- ? target.send(event)
236
+ ? sendMachineTarget(target, event, undefined, undefined)
185
237
  : sendMachineTarget(target, event, this.inspectionSubject, this.causation())
186
- : this.childRuntime.sendTo(target, event, this.inspectionSubject, this.causation())),
238
+ : this.childRuntime.sendTo(target, event, this.inspectionSubject, this.causation()),
187
239
  stopChild: this.childRuntime.stop,
188
240
  failCause: (cause) => this.failCause(cause),
189
241
  inspectInitial: this.inspector === undefined
@@ -211,17 +263,14 @@ class CompiledProcess {
211
263
  snapshot: { status: "active", state: initialized.state }
212
264
  };
213
265
  this.publishInitialized(initialized.state);
214
- this.compiledContext = new CompiledProcessContextImpl(this.processScope, this.childRuntime.owned, this);
215
- if ("executionState" in initialized) {
216
- this.compiledContext.executionState = initialized.executionState;
217
- }
266
+ this.prepareCompiledContext(initialized);
218
267
  if (this.options.onReadySync !== undefined && !this.options.onReadySync(this)) {
219
268
  this.requestTerminationSync({ _tag: "Stopped" });
220
269
  }
221
270
  if (initialized.done === true && this.lifecycle === "Active") {
222
271
  this.requestTerminationSync({ _tag: "Done", output: initialized.output });
223
272
  }
224
- if (initialized.done === false && this.execution.childless &&
273
+ if (initialized.done === false && childless &&
225
274
  this.lifecycle === "Active" &&
226
275
  this.mailbox.items === undefined) {
227
276
  return Effect.succeed(this);
@@ -233,26 +282,19 @@ class CompiledProcess {
233
282
  const self = this;
234
283
  return Effect.gen(function* () {
235
284
  self.publishCreated();
236
- if (!self.execution.childless) {
237
- self.childRuntime = yield* makeChildRuntime(self.address, self.options.runtime, self.services, (event) => self.sendAcknowledgedEffect(event));
238
- }
285
+ if (!self.childless)
286
+ self.childRuntime = self.makeChildRuntime();
239
287
  const parent = self.options.parent;
240
- const sendParent = self.options.sendParent ?? (parent === undefined
241
- ? noParentSend
242
- : self.inspector === undefined
243
- ? parent.send
244
- : (event) => sendMachineTarget(parent, event, self.inspectionSubject, self.causation()));
245
288
  self.processScope = {
246
289
  self: self.address,
247
290
  parent,
248
291
  spawn: self.childRuntime.spawn,
249
- sendParent,
250
292
  emit: (event) => self.emitEvent(event),
251
- sendTo: ((target, event) => isMachineTarget(target)
293
+ sendTo: (target, event) => isMachineTarget(target)
252
294
  ? self.inspector === undefined
253
- ? target.send(event)
295
+ ? sendMachineTarget(target, event, undefined, undefined)
254
296
  : sendMachineTarget(target, event, self.inspectionSubject, self.causation())
255
- : self.childRuntime.sendTo(target, event, self.inspectionSubject, self.causation())),
297
+ : self.childRuntime.sendTo(target, event, self.inspectionSubject, self.causation()),
256
298
  stopChild: self.childRuntime.stop,
257
299
  failCause: (cause) => self.failCause(cause),
258
300
  inspectInitial: self.inspector === undefined
@@ -263,7 +305,7 @@ class CompiledProcess {
263
305
  }
264
306
  };
265
307
  const cleanupStartupFailure = (exit) => Exit.isFailure(exit) ? self.childRuntime.close(exit) : Effect.void;
266
- const compiledInitial = self.execution.initial;
308
+ const compiledInitial = self.logic.execution?._tag === "Compiled" ? self.execution.initial : undefined;
267
309
  const initializeEffect = compiledInitial === undefined
268
310
  ? self.logic.initial(self.processScope).pipe(Effect.map((state) => ({ state, done: undefined, output: undefined })))
269
311
  : compiledInitial(self.processScope);
@@ -272,7 +314,7 @@ class CompiledProcess {
272
314
  self.publishStartFailed(exit.cause);
273
315
  return cleanupStartupFailure(exit);
274
316
  }), Effect.ensuring(Effect.sync(() => {
275
- self.runState = "Idle";
317
+ self.runState = self.initializedRunState;
276
318
  })));
277
319
  const initial = initialized.state;
278
320
  self.current = {
@@ -282,25 +324,7 @@ class CompiledProcess {
282
324
  snapshot: { status: "active", state: initial }
283
325
  };
284
326
  self.publishInitialized(initial);
285
- if (self.execution.drain._tag === "Process") {
286
- self.processContext = {
287
- ...self.processScope,
288
- receive: Effect.never,
289
- poll: Effect.sync(() => Option.map(pollCompactMailbox(self.mailbox), messageEvent)),
290
- receiveMessage: Effect.never,
291
- pollMessage: Effect.sync(() => self.pollCompiledMessage()),
292
- completeMessage: (delivery) => self.completeCompiledMessage(delivery),
293
- state: Effect.sync(() => self.current.snapshot.state),
294
- setState: (state) => self.setActiveState(state),
295
- updateState: (f) => self.updateState(f)
296
- };
297
- }
298
- else {
299
- self.compiledContext = new CompiledProcessContextImpl(self.processScope, self.childRuntime.owned, self);
300
- if ("executionState" in initialized) {
301
- self.compiledContext.executionState = initialized.executionState;
302
- }
303
- }
327
+ self.prepareRun(initialized);
304
328
  if (self.options.onReadySync !== undefined && !self.options.onReadySync(self)) {
305
329
  yield* self.requestTermination({ _tag: "Stopped" });
306
330
  }
@@ -318,12 +342,11 @@ class CompiledProcess {
318
342
  // neither startup hooks nor emitted work queued an event, there is no
319
343
  // first drain to perform. Future sends observe an idle run state and
320
344
  // start the ordinary compiled worker.
321
- if (initialized.done === false && self.execution.childless &&
345
+ if (initialized.done === false && self.childless &&
322
346
  self.lifecycle === "Active" && self.mailbox.items === undefined) {
323
347
  return self;
324
348
  }
325
- self.runState = "Draining";
326
- yield* self.drainRuntime();
349
+ yield* self.run();
327
350
  return self;
328
351
  });
329
352
  }
@@ -381,7 +404,7 @@ class CompiledProcess {
381
404
  ? { status: "stopped", state: latest.snapshot.state }
382
405
  : requested._tag === "Done"
383
406
  ? { status: "done", state: latest.snapshot.state, output: requested.output }
384
- : { status: "error", state: latest.snapshot.state, cause: requested.cause };
407
+ : { status: "failed", state: latest.snapshot.state, cause: requested.cause };
385
408
  this.current = { ...latest, terminalizing: true };
386
409
  return snapshot;
387
410
  }
@@ -395,44 +418,98 @@ class CompiledProcess {
395
418
  ? Effect.forkDetach(this.settleRequestedTermination()).pipe(Effect.asVoid)
396
419
  : Effect.void));
397
420
  }
398
- sendAcknowledgedEffect(event) {
399
- return Effect.uninterruptibleMask((restore) => Deferred.make().pipe(Effect.flatMap((deferred) => this.offerEvent(event, undefined, undefined, deferred).pipe(Effect.andThen(restore(Deferred.await(deferred)))))));
421
+ /**
422
+ * A caller-awaited `send`: an event that the schema refuses is rejected, and
423
+ * the machine keeps running. A machine that has ended rejects first.
424
+ */
425
+ sendPublic(event, source, causedBy) {
426
+ if (this.inspector === undefined)
427
+ return this.offerMessage(event, "Sent");
428
+ return Effect.suspend(() => {
429
+ if (this.mailbox.closed || this.lifecycle !== "Active")
430
+ return Effect.fail(new StoppedError());
431
+ const decoded = decodeSent(this.decodeEvent, event);
432
+ return decoded._tag === "Rejected"
433
+ ? Effect.fail(decoded.error)
434
+ : this.offerEvent(decoded.event, source, causedBy, undefined, event);
435
+ });
436
+ }
437
+ /** Delivers a transition command from this process to a target or child. */
438
+ deliverTo(target, event) {
439
+ return isMachineTarget(target)
440
+ ? this.inspector === undefined
441
+ ? deliverMachineTarget(target, event, undefined, undefined)
442
+ : deliverMachineTarget(target, event, this.inspectionSubject, this.causation())
443
+ : this.childRuntime.deliverTo(target, event, this.inspectionSubject, this.causation());
444
+ }
445
+ /** A transition-command or internal-work delivery. */
446
+ deliver(event, source, causedBy) {
447
+ if (this.inspector === undefined) {
448
+ return this.offerMessage(event, "Delivered");
449
+ }
450
+ return Effect.suspend(() => this.offerEvent(decodeDelivered(this.decodeEvent, event), source, causedBy, undefined, sentOf(event)));
451
+ }
452
+ sendAcknowledgedEffect(event, sent = event) {
453
+ return Effect.uninterruptibleMask((restore) => Deferred.make().pipe(Effect.flatMap((deferred) => this.offerEvent(event, undefined, undefined, deferred, sent).pipe(Effect.andThen(restore(Deferred.await(deferred)))))));
400
454
  }
401
- offerEvent(event, source, causedBy, deferred) {
455
+ offerEvent(event, source, causedBy, deferred, sent) {
402
456
  return Effect.suspend(() => {
403
457
  const inspector = this.inspector;
404
458
  const subject = this.inspectionSubject;
405
- const message = inspector?.isActive() === true && subject !== undefined
406
- ? makeInspectedMessage(inspector, subject, event, source, causedBy, deferred)
459
+ const message = inspector?.isActive() === true && subject !== undefined &&
460
+ !(event instanceof WorkMessage && sent === undefined)
461
+ ? makeInspectedMessage(inspector, subject, event, source, causedBy, deferred, sent)
407
462
  : deferred === undefined
408
463
  ? event
409
464
  : { [AcknowledgedMessageTypeId]: true, event, deferred };
410
465
  return this.offerMessage(message);
411
466
  });
412
467
  }
413
- offerMessage(message) {
468
+ /**
469
+ * Offers a message. Without an inspector, `send` and a delivery offer the
470
+ * event as it was sent, and it is decoded here, once the machine is known to
471
+ * run: `"Sent"` rejects a refused event, and `"Delivered"` lets the drain
472
+ * fail the machine with it.
473
+ */
474
+ offerMessage(offered, decoding = "Decoded") {
414
475
  return Effect.uninterruptible(Effect.withFiber((fiber) => {
415
476
  if (this.mailbox.closed || this.lifecycle !== "Active") {
416
477
  return Effect.fail(new StoppedError());
417
478
  }
418
- const inspector = this.inspector;
479
+ const execution = this.logic.execution;
480
+ const compiled = execution?._tag === "Compiled" ? execution : undefined;
481
+ let message = offered;
482
+ if (decoding !== "Decoded") {
483
+ const decode = compiled?.decodeEvent;
484
+ if (decode !== undefined && (decoding === "Sent" || !(offered instanceof WorkMessage))) {
485
+ // Decoded inline, so a send allocates nothing more than before.
486
+ try {
487
+ message = decode(offered);
488
+ }
489
+ catch (error) {
490
+ if (decoding === "Sent" && error instanceof MachineSchemaDecodeError)
491
+ return Effect.fail(error);
492
+ message = new RefusedEvent(error);
493
+ }
494
+ }
495
+ }
419
496
  const subject = this.inspectionSubject;
420
- if (inspector !== undefined && subject !== undefined)
421
- publishInspectedSent(inspector, subject, message);
497
+ if (subject !== undefined) {
498
+ const inspector = this.inspector;
499
+ if (inspector !== undefined)
500
+ publishInspectedSent(inspector, subject, message);
501
+ }
422
502
  this.offerRevision += 1;
423
503
  // Initialization drains whatever it queued once it finishes, and an
424
504
  // idle machine has an empty mailbox, so it receives the message directly.
425
- const drain = this.execution.drain;
426
- const context = this.compiledContext;
427
- const runSync = this.runState === "Idle" && context !== undefined && drain._tag === "Owned"
428
- ? drain.runSync
429
- : undefined;
430
- if (runSync === undefined) {
431
- offerCompactMailbox(this.mailbox, message);
432
- }
433
505
  if (this.runState !== "Idle") {
506
+ this.enqueue(message);
434
507
  return Effect.void;
435
508
  }
509
+ const context = this.compiledContext;
510
+ const runSync = context !== undefined && compiled !== undefined && compiled.drain._tag === "Owned"
511
+ ? compiled.drain.runSync
512
+ : undefined;
436
513
  // An idle machine drains in the sender's turn, so a transition that
437
514
  // completes synchronously is committed before `send` returns. Sends
438
515
  // that arrive while draining only enqueue.
@@ -453,6 +530,7 @@ class CompiledProcess {
453
530
  this.scheduleChangesFlush(fiber.currentDispatcher);
454
531
  return Effect.void;
455
532
  }
533
+ this.enqueue(message);
456
534
  return this.startWorker(undefined);
457
535
  }));
458
536
  }
@@ -499,7 +577,7 @@ class CompiledProcess {
499
577
  });
500
578
  }
501
579
  finishIdleChildlessStop() {
502
- if (!this.execution.childless || this.runState !== "Idle" || this.worker !== undefined ||
580
+ if (!this.childless || this.runState !== "Idle" || this.worker !== undefined ||
503
581
  this.lifecycle !== "Active" ||
504
582
  (this.options.onOutcome !== undefined && this.options.skipStoppedOutcome !== true) ||
505
583
  this.options.onStop !== undefined ||
@@ -516,7 +594,7 @@ class CompiledProcess {
516
594
  this.lifecycle = "TerminationRequested";
517
595
  this.termination = { _tag: "Stopped" };
518
596
  this.terminationSnapshot = snapshot;
519
- closeCompactMailbox(this.mailbox);
597
+ this.closeMailbox();
520
598
  this.current = {
521
599
  revision: this.current.revision + 1,
522
600
  terminalizing: true,
@@ -620,12 +698,14 @@ class CompiledProcess {
620
698
  }
621
699
  const requested = this.termination;
622
700
  if (requested === undefined) {
623
- return Effect.void;
701
+ return this.awaitCompletion();
624
702
  }
625
703
  const snapshot = this.terminationSnapshot ?? this.reserveTermination(requested);
626
704
  if (snapshot === undefined) {
627
705
  return this.awaitCompletion();
628
706
  }
707
+ // One caller finishes the process; any other awaits its completion.
708
+ this.termination = undefined;
629
709
  const exit = requested._tag === "Stopped"
630
710
  ? Exit.void
631
711
  : requested._tag === "Done"
@@ -644,16 +724,16 @@ class CompiledProcess {
644
724
  const subject = this.inspectionSubject;
645
725
  const closeEmissionsAndInspect = inspector === undefined || subject === undefined
646
726
  ? this.closeEmissions()
647
- : this.closeEmissions().pipe(Effect.andThen(Effect.sync(() => inspector.publishUnsafe(this.activity === undefined
727
+ : this.closeEmissions().pipe(Effect.andThen(Effect.sync(() => inspector.publishUnsafe(this.programInvocation === undefined
648
728
  ? { _tag: "Terminated", subject, snapshot }
649
729
  : {
650
- _tag: "ActivityStopped",
651
- subject: this.activity.owner,
652
- activity: this.activity,
730
+ _tag: "ProgramInvocationStopped",
731
+ subject: this.programInvocation.owner,
732
+ invocation: this.programInvocation,
653
733
  exit: snapshot.status === "stopped" ? Exit.interrupt() : exit
654
734
  }))));
655
735
  return Effect.uninterruptible(Effect.sync(() => {
656
- closeCompactMailbox(this.mailbox);
736
+ this.closeMailbox();
657
737
  }).pipe(Effect.andThen(this.childRuntime.close(exit)), Effect.andThen(this.setAndPublishSnapshot(snapshot)), Effect.andThen(closeEmissionsAndInspect), Effect.andThen(Effect.sync(() => {
658
738
  if (requested._tag === "Failure") {
659
739
  failAcknowledgedMessage(this.inFlightMessage, requested.cause);
@@ -768,7 +848,12 @@ class CompiledProcess {
768
848
  return message;
769
849
  }
770
850
  beginCompiledMessage(message) {
771
- this.inFlightMessage = isAcknowledgedMessage(message) ? message : undefined;
851
+ if (isAcknowledgedMessage(message)) {
852
+ this.inFlightMessage = message;
853
+ return true;
854
+ }
855
+ this.inFlightMessage = undefined;
856
+ return false;
772
857
  }
773
858
  completeCompiledMessage(delivery) {
774
859
  succeedAcknowledgedMessage(this.inFlightMessage, delivery);
@@ -904,6 +989,158 @@ class CompiledProcess {
904
989
  }));
905
990
  }
906
991
  }
992
+ /** A machine: its compiled execution drains its mailbox. */
993
+ class MachineProcess extends LocalProcess {
994
+ constructor(logic, options, services, sessionId) {
995
+ super(logic, options, services, sessionId);
996
+ }
997
+ get childless() {
998
+ return this.execution.childless;
999
+ }
1000
+ [acknowledgedSend](event) {
1001
+ return Effect.suspend(() => {
1002
+ if (this.mailbox.closed || this.lifecycle !== "Active")
1003
+ return Effect.fail(new StoppedError());
1004
+ const decoded = decodeSent(this.decodeEvent, event);
1005
+ return decoded._tag === "Rejected"
1006
+ ? Effect.fail(decoded.error)
1007
+ : this.sendAcknowledgedEffect(decoded.event, event);
1008
+ });
1009
+ }
1010
+ get initializedRunState() {
1011
+ return "Idle";
1012
+ }
1013
+ makeChildRuntime() {
1014
+ return makeChildRuntimeSync(this.address, this.options.runtime, this.services, (event) => this.sendAcknowledgedEffect(decodeDelivered(this.decodeEvent, event), sentOf(event)));
1015
+ }
1016
+ prepareRun(initialized) {
1017
+ if (this.execution.drain._tag === "Process") {
1018
+ this.processContext = {
1019
+ ...this.processScope,
1020
+ receive: Effect.never,
1021
+ poll: Effect.sync(() => Option.map(pollCompactMailbox(this.mailbox), messageEvent)),
1022
+ receiveMessage: Effect.never,
1023
+ pollMessage: Effect.sync(() => this.pollCompiledMessage()),
1024
+ completeMessage: (delivery) => this.completeCompiledMessage(delivery),
1025
+ state: Effect.sync(() => this.current.snapshot.state),
1026
+ setState: (state) => this.setActiveState(state),
1027
+ updateState: (f) => this.updateState(f)
1028
+ };
1029
+ }
1030
+ else {
1031
+ this.prepareCompiledContext(initialized);
1032
+ }
1033
+ }
1034
+ run() {
1035
+ this.runState = "Draining";
1036
+ return this.drainRuntime();
1037
+ }
1038
+ }
1039
+ /**
1040
+ * Logic other than a machine, run as a resident program: it starts in the
1041
+ * turn of its starter, receives from the mailbox, and its exit ends the
1042
+ * process. Its state is updated one change at a time.
1043
+ */
1044
+ class LogicProcess extends LocalProcess {
1045
+ // A resident program runs from the end of its startup until it exits, so
1046
+ // the process never rests idle and every send only enqueues.
1047
+ get initializedRunState() {
1048
+ return "Initializing";
1049
+ }
1050
+ mailbox = makeProcessMailbox();
1051
+ updates;
1052
+ makeChildRuntime() {
1053
+ return makeChildRuntimeSync(this.address, this.options.runtime);
1054
+ }
1055
+ enqueue(message) {
1056
+ offerProcessMailbox(this.mailbox, message);
1057
+ }
1058
+ closeMailbox() {
1059
+ closeProcessMailbox(this.mailbox);
1060
+ }
1061
+ /**
1062
+ * Initialization finishes constructing a state before a stopped snapshot can
1063
+ * be published, so a stop requested there is recorded and returns. Once
1064
+ * running, the process that stops itself never continues: the program is
1065
+ * interrupted, and the caller waits until it is.
1066
+ */
1067
+ stopFromProcess() {
1068
+ const request = this.requestTermination({ _tag: "Stopped" }).pipe(Effect.asVoid);
1069
+ return this.runState === "Initializing"
1070
+ ? request
1071
+ : request.pipe(Effect.andThen(Effect.forkDetach(this.settleRequestedTermination())), Effect.andThen(Effect.never));
1072
+ }
1073
+ prepareRun() {
1074
+ const inspected = this.inspector !== undefined;
1075
+ const receive = receiveProcessMailbox(this.mailbox);
1076
+ const poll = Effect.sync(() => pollCompactMailbox(this.mailbox));
1077
+ this.processContext = {
1078
+ ...this.processScope,
1079
+ receive: inspected
1080
+ ? Effect.map(receive, (message) => messageEvent(this.inFlightMessage = message))
1081
+ : Effect.map(receive, messageEvent),
1082
+ poll: inspected
1083
+ ? Effect.map(poll, (message) => {
1084
+ if (Option.isSome(message))
1085
+ this.inFlightMessage = message.value;
1086
+ return Option.map(message, messageEvent);
1087
+ })
1088
+ : Effect.map(poll, Option.map(messageEvent)),
1089
+ state: Effect.sync(() => this.current.snapshot.state),
1090
+ setState: (state) => this.serialized(Effect.suspend(() => this.changeState(state))),
1091
+ updateState: (f) => this.serialized(Effect.suspend(() => {
1092
+ const observed = this.current;
1093
+ if (observed.terminalizing || observed.snapshot.status !== "active")
1094
+ return Effect.void;
1095
+ return Effect.flatMap(f(observed.snapshot.state), (state) => this.changeState(state));
1096
+ }))
1097
+ };
1098
+ }
1099
+ /** Changes of state wait for an update in progress, as one change at a time. */
1100
+ serialized(effect) {
1101
+ return (this.updates ??= Semaphore.makeUnsafe(1)).withPermits(1)(effect);
1102
+ }
1103
+ /** Commits a state, reported as a change of state of logic that is not a machine. */
1104
+ changeState(state) {
1105
+ const before = this.current;
1106
+ const published = this.commitActiveState(state);
1107
+ const inspector = this.inspector;
1108
+ if (inspector !== undefined && this.current !== before && this.logic.inspection?.kind !== "Machine") {
1109
+ const message = this.inFlightMessage;
1110
+ inspector.publishUnsafe({
1111
+ _tag: "StateChanged",
1112
+ subject: this.inspectionSubject,
1113
+ before: before.snapshot.state,
1114
+ after: state,
1115
+ causedByDeliveryId: message !== undefined && isAcknowledgedMessage(message)
1116
+ ? message.inspection?.deliveryId
1117
+ : undefined
1118
+ });
1119
+ }
1120
+ return published ?? Effect.void;
1121
+ }
1122
+ run() {
1123
+ return Effect.suspend(() => {
1124
+ if (this.lifecycle !== "Active")
1125
+ return this.finishRequestedTermination();
1126
+ this.runState = "Draining";
1127
+ const program = Effect.uninterruptibleMask((restore) => restore(Effect.suspend(() => this.logic.run(this.processContext))).pipe(Effect.exit, Effect.flatMap((exit) => {
1128
+ this.requestTerminationSync(Exit.isFailure(exit) ? { _tag: "Failure", cause: exit.cause } : { _tag: "Done", output: exit.value });
1129
+ return this.finishRequestedTermination();
1130
+ })));
1131
+ // The program starts in the caller's turn and runs until it first
1132
+ // suspends, so a process started while an event is processed begins its
1133
+ // work before that event's sender continues.
1134
+ const fork = this.options.detached === true
1135
+ ? Effect.forkDetach(program, { startImmediately: true })
1136
+ : Effect.forkChild(program, { startImmediately: true });
1137
+ return Effect.flatMap(fork, (fiber) => Effect.sync(() => {
1138
+ if (fiber.pollUnsafe() === undefined)
1139
+ this.worker = fiber;
1140
+ }));
1141
+ });
1142
+ }
1143
+ }
907
1144
  class CompiledProcessContextImpl {
908
1145
  scope;
909
1146
  ownedChildren;
@@ -921,7 +1158,7 @@ class CompiledProcessContextImpl {
921
1158
  return this.process.pollCompiledMessage();
922
1159
  }
923
1160
  beginMessage(message) {
924
- this.process.beginCompiledMessage(message);
1161
+ return this.process.beginCompiledMessage(message);
925
1162
  }
926
1163
  state() {
927
1164
  return this.process.compiledState();
@@ -938,14 +1175,16 @@ class CompiledProcessContextImpl {
938
1175
  }
939
1176
  }
940
1177
  export const startCompactCompiledInternal = Effect.fnUntraced(function* (logic, options) {
941
- const sessionId = options.sessionId ?? (yield* options.runtime.nextSessionId);
1178
+ const sessionId = options.sessionId ?? options.runtime.nextSessionIdSync();
942
1179
  const services = yield* Effect.context();
943
- const execution = logic.execution;
944
- const process = new CompiledProcess(logic, options, services, sessionId);
1180
+ const execution = logic.execution?._tag === "Compiled" ? logic.execution : undefined;
1181
+ const process = logic.execution?._tag === "Compiled"
1182
+ ? new MachineProcess(logic, options, services, sessionId)
1183
+ : new LogicProcess(logic, options, services, sessionId);
945
1184
  // A compiled initializer is synchronous by construction. Only startup
946
1185
  // callbacks that themselves return Effects need the generic initialization
947
1186
  // program; the compiled drain is still provided the complete service context.
948
- const initialize = execution.initialSync !== undefined &&
1187
+ const initialize = execution?.initialSync !== undefined &&
949
1188
  options.onReady === undefined && options.onSnapshot === undefined
950
1189
  ? process.initializeCompiledSync()
951
1190
  : process.initialize();
@@ -956,7 +1195,7 @@ export const startCompactCompiledInternal = Effect.fnUntraced(function* (logic,
956
1195
  : Effect.void));
957
1196
  });
958
1197
  /** Preserves synchronous startup for owned compiled children. */
959
- export const startCompiledSync = (logic, options, services, sessionId) => new CompiledProcess(logic, options, services, sessionId).initializeCompiledSync();
1198
+ export const startCompiledSync = (logic, options, services, sessionId) => new MachineProcess(logic, options, services, sessionId).initializeCompiledSync();
960
1199
  // Stopping is commonly used only for resource cleanup. Keep that path free of
961
1200
  // Error stack capture and materialize the typed join failure only if observed.
962
1201
  //# sourceMappingURL=runtimeCompiled.js.map