@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.
- package/AGENTS.md +374 -22
- package/CLAUDE.md +374 -22
- package/README.md +56 -7
- package/ai-docs/src/10_modeling/01_create-a-machine.ts +3 -3
- package/ai-docs/src/10_modeling/index.md +64 -17
- package/ai-docs/src/20_root-api/index.md +2 -2
- package/ai-docs/src/35_cluster/index.md +213 -0
- package/ai-docs/src/37_new-host/index.md +90 -0
- package/dist/LocalMachine.d.ts +417 -0
- package/dist/LocalMachine.d.ts.map +1 -0
- package/dist/LocalMachine.js +193 -0
- package/dist/LocalMachine.js.map +1 -0
- package/dist/Machine.d.ts +1122 -814
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +195 -186
- package/dist/Machine.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/machine/atom.d.ts +8 -10
- package/dist/internal/machine/atom.d.ts.map +1 -1
- package/dist/internal/machine/atom.js +7 -6
- package/dist/internal/machine/atom.js.map +1 -1
- package/dist/internal/machine/capability.d.ts +101 -0
- package/dist/internal/machine/capability.d.ts.map +1 -0
- package/dist/internal/machine/capability.js +70 -0
- package/dist/internal/machine/capability.js.map +1 -0
- package/dist/internal/machine/{invocationDefinition.d.ts → capturedInvocation.d.ts} +11 -11
- package/dist/internal/machine/capturedInvocation.d.ts.map +1 -0
- package/dist/internal/machine/{invocationDefinition.js → capturedInvocation.js} +2 -2
- package/dist/internal/machine/capturedInvocation.js.map +1 -0
- package/dist/internal/machine/child.d.ts +14 -0
- package/dist/internal/machine/child.d.ts.map +1 -0
- package/dist/internal/machine/child.js +19 -0
- package/dist/internal/machine/child.js.map +1 -0
- package/dist/internal/machine/childRegistry.d.ts +0 -2
- package/dist/internal/machine/childRegistry.d.ts.map +1 -1
- package/dist/internal/machine/childRegistry.js +2 -2
- package/dist/internal/machine/childRegistry.js.map +1 -1
- package/dist/internal/machine/cluster.d.ts +19 -4
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +263 -110
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/clusterChild.d.ts +66 -0
- package/dist/internal/machine/clusterChild.d.ts.map +1 -0
- package/dist/internal/machine/clusterChild.js +105 -0
- package/dist/internal/machine/clusterChild.js.map +1 -0
- package/dist/internal/machine/clusterFailure.d.ts +14 -0
- package/dist/internal/machine/clusterFailure.d.ts.map +1 -0
- package/dist/internal/machine/clusterFailure.js +44 -0
- package/dist/internal/machine/clusterFailure.js.map +1 -0
- package/dist/internal/machine/clusterInstance.d.ts +194 -0
- package/dist/internal/machine/clusterInstance.d.ts.map +1 -0
- package/dist/internal/machine/clusterInstance.js +1076 -0
- package/dist/internal/machine/clusterInstance.js.map +1 -0
- package/dist/internal/machine/clusterKeepAlive.d.ts +47 -0
- package/dist/internal/machine/clusterKeepAlive.d.ts.map +1 -0
- package/dist/internal/machine/clusterKeepAlive.js +72 -0
- package/dist/internal/machine/clusterKeepAlive.js.map +1 -0
- package/dist/internal/machine/clusterProgress.d.ts +73 -0
- package/dist/internal/machine/clusterProgress.d.ts.map +1 -0
- package/dist/internal/machine/clusterProgress.js +116 -0
- package/dist/internal/machine/clusterProgress.js.map +1 -0
- package/dist/internal/machine/clusterProtocol.d.ts +37 -4
- package/dist/internal/machine/clusterProtocol.d.ts.map +1 -1
- package/dist/internal/machine/clusterProtocol.js +18 -4
- package/dist/internal/machine/clusterProtocol.js.map +1 -1
- package/dist/internal/machine/clusterProxy.d.ts +46 -0
- package/dist/internal/machine/clusterProxy.d.ts.map +1 -0
- package/dist/internal/machine/clusterProxy.js +150 -0
- package/dist/internal/machine/clusterProxy.js.map +1 -0
- package/dist/internal/machine/clusterRead.d.ts +61 -0
- package/dist/internal/machine/clusterRead.d.ts.map +1 -0
- package/dist/internal/machine/clusterRead.js +108 -0
- package/dist/internal/machine/clusterRead.js.map +1 -0
- package/dist/internal/machine/clusterSqlStorage.d.ts +16 -0
- package/dist/internal/machine/clusterSqlStorage.d.ts.map +1 -0
- package/dist/internal/machine/clusterSqlStorage.js +138 -0
- package/dist/internal/machine/clusterSqlStorage.js.map +1 -0
- package/dist/internal/machine/clusterStorageCheck.d.ts +19 -0
- package/dist/internal/machine/clusterStorageCheck.d.ts.map +1 -0
- package/dist/internal/machine/clusterStorageCheck.js +186 -0
- package/dist/internal/machine/clusterStorageCheck.js.map +1 -0
- package/dist/internal/machine/clusterTarget.d.ts +74 -0
- package/dist/internal/machine/clusterTarget.d.ts.map +1 -0
- package/dist/internal/machine/clusterTarget.js +81 -0
- package/dist/internal/machine/clusterTarget.js.map +1 -0
- package/dist/internal/machine/clusterTimer.d.ts +21 -0
- package/dist/internal/machine/clusterTimer.d.ts.map +1 -0
- package/dist/internal/machine/clusterTimer.js +50 -0
- package/dist/internal/machine/clusterTimer.js.map +1 -0
- package/dist/internal/machine/clusterWake.d.ts +61 -0
- package/dist/internal/machine/clusterWake.d.ts.map +1 -0
- package/dist/internal/machine/clusterWake.js +81 -0
- package/dist/internal/machine/clusterWake.js.map +1 -0
- package/dist/internal/machine/clusterWork.d.ts +130 -0
- package/dist/internal/machine/clusterWork.d.ts.map +1 -0
- package/dist/internal/machine/clusterWork.js +191 -0
- package/dist/internal/machine/clusterWork.js.map +1 -0
- package/dist/internal/machine/command.d.ts +4 -4
- package/dist/internal/machine/command.d.ts.map +1 -1
- package/dist/internal/machine/commandRuntime.d.ts +8 -6
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/commandRuntime.js +1 -5
- package/dist/internal/machine/commandRuntime.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts +0 -13
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +0 -151
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/errors.d.ts +11 -2
- package/dist/internal/machine/errors.d.ts.map +1 -1
- package/dist/internal/machine/errors.js +11 -2
- package/dist/internal/machine/errors.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts +45 -3
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +328 -191
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +6 -6
- package/dist/internal/machine/implementation.d.ts.map +1 -1
- package/dist/internal/machine/inspectionRuntime.d.ts +1 -1
- package/dist/internal/machine/inspectionRuntime.d.ts.map +1 -1
- package/dist/internal/machine/invocationChanges.d.ts +60 -0
- package/dist/internal/machine/invocationChanges.d.ts.map +1 -0
- package/dist/internal/machine/invocationChanges.js +54 -0
- package/dist/internal/machine/invocationChanges.js.map +1 -0
- package/dist/internal/machine/{activities.d.ts → invocationDefinitions.d.ts} +8 -8
- package/dist/internal/machine/invocationDefinitions.d.ts.map +1 -0
- package/dist/internal/machine/{activities.js → invocationDefinitions.js} +5 -5
- package/dist/internal/machine/invocationDefinitions.js.map +1 -0
- package/dist/internal/machine/invocationEvent.d.ts +16 -7
- package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
- package/dist/internal/machine/invocationEvent.js +26 -12
- package/dist/internal/machine/invocationEvent.js.map +1 -1
- package/dist/internal/machine/invocationOutcome.d.ts +89 -0
- package/dist/internal/machine/invocationOutcome.d.ts.map +1 -0
- package/dist/internal/machine/invocationOutcome.js +76 -0
- package/dist/internal/machine/invocationOutcome.js.map +1 -0
- package/dist/internal/machine/invocationResolution.d.ts +137 -0
- package/dist/internal/machine/invocationResolution.d.ts.map +1 -0
- package/dist/internal/machine/invocationResolution.js +150 -0
- package/dist/internal/machine/invocationResolution.js.map +1 -0
- package/dist/internal/machine/logicScope.d.ts +24 -0
- package/dist/internal/machine/logicScope.d.ts.map +1 -0
- package/dist/internal/machine/logicScope.js +33 -0
- package/dist/internal/machine/logicScope.js.map +1 -0
- package/dist/internal/machine/machine.d.ts +17 -23
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +24 -40
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/observation.d.ts +3 -0
- package/dist/internal/machine/observation.d.ts.map +1 -1
- package/dist/internal/machine/observation.js +56 -2
- package/dist/internal/machine/observation.js.map +1 -1
- package/dist/internal/machine/plannedSnapshot.d.ts.map +1 -1
- package/dist/internal/machine/plannedSnapshot.js +11 -20
- package/dist/internal/machine/plannedSnapshot.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +36 -23
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +15 -8
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +17 -9
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +43 -503
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/protocol.d.ts +1 -1
- package/dist/internal/machine/protocol.d.ts.map +1 -1
- package/dist/internal/machine/protocol.js +48 -10
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +20 -16
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +93 -88
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -1
- package/dist/internal/machine/runtimeCompiled.js +359 -120
- package/dist/internal/machine/runtimeCompiled.js.map +1 -1
- package/dist/internal/machine/runtimeProtocol.d.ts +151 -82
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
- package/dist/internal/machine/runtimeProtocol.js +236 -219
- package/dist/internal/machine/runtimeProtocol.js.map +1 -1
- package/dist/internal/machine/stateDefinition.d.ts +0 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +0 -1
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- package/dist/internal/machine/step.d.ts +486 -0
- package/dist/internal/machine/step.d.ts.map +1 -0
- package/dist/internal/machine/step.js +957 -0
- package/dist/internal/machine/step.js.map +1 -0
- package/dist/internal/machine/stepHost.d.ts +33 -0
- package/dist/internal/machine/stepHost.d.ts.map +1 -0
- package/dist/internal/machine/stepHost.js +501 -0
- package/dist/internal/machine/stepHost.js.map +1 -0
- package/dist/internal/machine/symbols.d.ts +2 -11
- package/dist/internal/machine/symbols.d.ts.map +1 -1
- package/dist/internal/machine/symbols.js +2 -2
- package/dist/internal/machine/symbols.js.map +1 -1
- package/dist/internal/machine/transition.d.ts +7 -7
- package/dist/internal/machine/transition.d.ts.map +1 -1
- package/dist/internal/testing/machine/check.d.ts.map +1 -1
- package/dist/internal/testing/machine/check.js +217 -25
- package/dist/internal/testing/machine/check.js.map +1 -1
- package/dist/internal/testing/machine/exploration.d.ts +19 -2
- package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
- package/dist/internal/testing/machine/exploration.js +14 -3
- package/dist/internal/testing/machine/exploration.js.map +1 -1
- package/dist/internal/testing/machine/format.js.map +1 -1
- package/dist/internal/testing/machine/probe.d.ts.map +1 -1
- package/dist/internal/testing/machine/probe.js +1 -1
- package/dist/internal/testing/machine/probe.js.map +1 -1
- package/dist/internal/testing/machine/runtime.d.ts +2 -2
- package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
- package/dist/internal/testing/machine/runtime.js +10 -3
- package/dist/internal/testing/machine/runtime.js.map +1 -1
- package/dist/internal/testing/machine/trace.d.ts +38 -3
- package/dist/internal/testing/machine/trace.d.ts.map +1 -1
- package/dist/internal/testing/machine/trace.js +180 -33
- package/dist/internal/testing/machine/trace.js.map +1 -1
- package/dist/internal/testing/machine/transitionCoverage.d.ts +1 -1
- package/dist/internal/testing/machine/transitionCoverage.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +9 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/internal/testing/machine/work.d.ts +47 -0
- package/dist/internal/testing/machine/work.d.ts.map +1 -0
- package/dist/internal/testing/machine/work.js +74 -0
- package/dist/internal/testing/machine/work.js.map +1 -0
- package/dist/testing/MachineTest.d.ts +253 -37
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +81 -9
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +1071 -41
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +441 -12
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachineProxy.d.ts +257 -0
- package/dist/unstable/cluster/ClusterMachineProxy.d.ts.map +1 -0
- package/dist/unstable/cluster/ClusterMachineProxy.js +73 -0
- package/dist/unstable/cluster/ClusterMachineProxy.js.map +1 -0
- package/dist/unstable/cluster/SqlMachineStorage.d.ts +125 -0
- package/dist/unstable/cluster/SqlMachineStorage.d.ts.map +1 -0
- package/dist/unstable/cluster/SqlMachineStorage.js +62 -0
- package/dist/unstable/cluster/SqlMachineStorage.js.map +1 -0
- package/dist/unstable/cluster/index.d.ts +13 -0
- package/dist/unstable/cluster/index.d.ts.map +1 -1
- package/dist/unstable/cluster/index.js +13 -0
- package/dist/unstable/cluster/index.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +21 -26
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/package.json +5 -3
- package/src/LocalMachine.ts +611 -0
- package/src/Machine.ts +1383 -1336
- package/src/index.ts +1 -0
- package/src/internal/machine/atom.ts +20 -49
- package/src/internal/machine/capability.ts +123 -0
- package/src/internal/machine/{invocationDefinition.ts → capturedInvocation.ts} +13 -13
- package/src/internal/machine/child.ts +25 -0
- package/src/internal/machine/childRegistry.ts +1 -4
- package/src/internal/machine/cluster.ts +420 -205
- package/src/internal/machine/clusterChild.ts +117 -0
- package/src/internal/machine/clusterFailure.ts +53 -0
- package/src/internal/machine/clusterInstance.ts +1779 -0
- package/src/internal/machine/clusterKeepAlive.ts +93 -0
- package/src/internal/machine/clusterProgress.ts +159 -0
- package/src/internal/machine/clusterProtocol.ts +46 -4
- package/src/internal/machine/clusterProxy.ts +233 -0
- package/src/internal/machine/clusterRead.ts +207 -0
- package/src/internal/machine/clusterSqlStorage.ts +181 -0
- package/src/internal/machine/clusterStorageCheck.ts +274 -0
- package/src/internal/machine/clusterTarget.ts +154 -0
- package/src/internal/machine/clusterTimer.ts +58 -0
- package/src/internal/machine/clusterWake.ts +100 -0
- package/src/internal/machine/clusterWork.ts +269 -0
- package/src/internal/machine/command.ts +4 -4
- package/src/internal/machine/commandRuntime.ts +12 -24
- package/src/internal/machine/configuration.ts +0 -181
- package/src/internal/machine/errors.ts +11 -2
- package/src/internal/machine/executionPlan.ts +493 -263
- package/src/internal/machine/implementation.ts +6 -6
- package/src/internal/machine/inspectionRuntime.ts +1 -1
- package/src/internal/machine/invocationChanges.ts +89 -0
- package/src/internal/machine/{activities.ts → invocationDefinitions.ts} +10 -10
- package/src/internal/machine/invocationEvent.ts +35 -12
- package/src/internal/machine/invocationOutcome.ts +145 -0
- package/src/internal/machine/invocationResolution.ts +307 -0
- package/src/internal/machine/logicScope.ts +47 -0
- package/src/internal/machine/machine.ts +35 -100
- package/src/internal/machine/observation.ts +67 -2
- package/src/internal/machine/plannedSnapshot.ts +17 -20
- package/src/internal/machine/planner.ts +75 -99
- package/src/internal/machine/process.ts +78 -722
- package/src/internal/machine/protocol.ts +70 -26
- package/src/internal/machine/runtime.ts +198 -195
- package/src/internal/machine/runtimeCompiled.ts +496 -171
- package/src/internal/machine/runtimeProtocol.ts +416 -305
- package/src/internal/machine/stateDefinition.ts +0 -5
- package/src/internal/machine/step.ts +1518 -0
- package/src/internal/machine/stepHost.ts +634 -0
- package/src/internal/machine/symbols.ts +3 -14
- package/src/internal/machine/transition.ts +11 -11
- package/src/internal/testing/machine/check.ts +240 -36
- package/src/internal/testing/machine/exploration.ts +42 -3
- package/src/internal/testing/machine/format.ts +1 -1
- package/src/internal/testing/machine/probe.ts +2 -1
- package/src/internal/testing/machine/runtime.ts +15 -5
- package/src/internal/testing/machine/trace.ts +261 -41
- package/src/internal/testing/machine/transitionCoverage.ts +1 -1
- package/src/internal/testing/machine/verification.ts +11 -5
- package/src/internal/testing/machine/work.ts +101 -0
- package/src/testing/MachineTest.ts +268 -44
- package/src/unstable/cluster/ClusterMachine.ts +1183 -59
- package/src/unstable/cluster/ClusterMachineProxy.ts +372 -0
- package/src/unstable/cluster/SqlMachineStorage.ts +136 -0
- package/src/unstable/cluster/index.ts +15 -0
- package/src/unstable/reactivity/AtomMachine.ts +27 -88
- package/dist/internal/machine/activities.d.ts.map +0 -1
- package/dist/internal/machine/activities.js.map +0 -1
- package/dist/internal/machine/invocation.d.ts +0 -33
- package/dist/internal/machine/invocation.d.ts.map +0 -1
- package/dist/internal/machine/invocation.js +0 -220
- package/dist/internal/machine/invocation.js.map +0 -1
- package/dist/internal/machine/invocationDefinition.d.ts.map +0 -1
- package/dist/internal/machine/invocationDefinition.js.map +0 -1
- package/dist/internal/machine/requirements.d.ts +0 -4
- package/dist/internal/machine/requirements.d.ts.map +0 -1
- package/dist/internal/machine/requirements.js +0 -2
- package/dist/internal/machine/requirements.js.map +0 -1
- package/dist/internal/machine/runtimeGeneric.d.ts +0 -3
- package/dist/internal/machine/runtimeGeneric.d.ts.map +0 -1
- package/dist/internal/machine/runtimeGeneric.js +0 -465
- package/dist/internal/machine/runtimeGeneric.js.map +0 -1
- package/src/internal/machine/invocation.ts +0 -376
- package/src/internal/machine/requirements.ts +0 -11
- package/src/internal/machine/runtimeGeneric.ts +0 -797
package/CLAUDE.md
CHANGED
|
@@ -91,18 +91,20 @@ Start the implemented machine at the application boundary and send events
|
|
|
91
91
|
through the exported descriptor:
|
|
92
92
|
|
|
93
93
|
```ts
|
|
94
|
+
import { LocalMachine } from "@typeonce/effect-machine"
|
|
94
95
|
import { Effect } from "effect"
|
|
95
96
|
|
|
96
97
|
const program = Effect.scoped(Effect.gen(function*() {
|
|
97
|
-
const counter = yield*
|
|
98
|
+
const counter = yield* LocalMachine.start(CounterMachine)
|
|
98
99
|
|
|
99
100
|
yield* counter.send(CounterEvents.Start())
|
|
100
101
|
yield* counter.send(CounterEvents.Increment())
|
|
101
102
|
}))
|
|
102
103
|
```
|
|
103
104
|
|
|
104
|
-
`
|
|
105
|
-
it owns; use `ref.stop` to stop
|
|
105
|
+
`LocalMachine` runs a machine in memory. `LocalMachine.start` requires a `Scope`.
|
|
106
|
+
Closing it stops the machine and everything it owns; use `ref.stop` to stop
|
|
107
|
+
earlier.
|
|
106
108
|
|
|
107
109
|
### Make impossible states unrepresentable
|
|
108
110
|
|
|
@@ -352,6 +354,20 @@ const LoadMachine = Machine.make({
|
|
|
352
354
|
implemented machine type, so the runtime must provide them when it starts the
|
|
353
355
|
machine.
|
|
354
356
|
|
|
357
|
+
The same requirements say what a machine needs of the host that runs it.
|
|
358
|
+
`Machine.InvocationSupport<"Stream">`, `<"Logic">`, and `<"Child">` mark a machine
|
|
359
|
+
whose states invoke that kind. `Machine.MachineRuntime` marks one whose work uses
|
|
360
|
+
`Machine.spawn`, or `Machine.sendTo` or `Machine.stopChild` with a child address,
|
|
361
|
+
and `Machine.ChildMachineRuntime` one whose work uses `Machine.sendTo` or
|
|
362
|
+
`Machine.stopChild` with the descriptor of a child machine. `LocalMachine` and
|
|
363
|
+
`AtomMachine` run all of them. `ClusterMachine.make` runs Effects, timers, and
|
|
364
|
+
child machines, and provides `Machine.ChildMachineRuntime`. It does not compile
|
|
365
|
+
for a machine that invokes a Stream or a `Logic`, or needs
|
|
366
|
+
`Machine.MachineRuntime`, and its error lists what to replace. Cluster stores
|
|
367
|
+
each step and runs work again after a crash, so it runs only work that ends with
|
|
368
|
+
one outcome: the progress of a Stream and the state of a `Logic` live in memory
|
|
369
|
+
and would be lost. Model such work as a child machine.
|
|
370
|
+
|
|
355
371
|
Model a per-instance capability, such as a router or a callback owned by a UI
|
|
356
372
|
component, as such a service. Do not put functions in state data or startup
|
|
357
373
|
input: snapshots must remain encodable. A React owner provides the capability
|
|
@@ -361,11 +377,11 @@ through its own runtime, as described in
|
|
|
361
377
|
Do not start a promise inside a transition callback. A transition has no
|
|
362
378
|
lifetime in which to own that work. A state does.
|
|
363
379
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
in a later turn, even when the
|
|
367
|
-
|
|
368
|
-
browser call that requires user activation, such as `window.open` or
|
|
380
|
+
A local machine, including one hosted by `AtomMachine`, starts invoked work while
|
|
381
|
+
the event that entered its state is processed. The work runs in the sender's turn
|
|
382
|
+
until it first suspends, and its outcome is handled in a later turn, even when the
|
|
383
|
+
work completes without suspending. A state that invokes work is therefore always
|
|
384
|
+
observed before its outcome leaves it. Put a browser call that requires user activation, such as `window.open` or
|
|
369
385
|
`navigator.clipboard.writeText`, in the first step of the invoked Effect:
|
|
370
386
|
|
|
371
387
|
```ts
|
|
@@ -462,6 +478,14 @@ Do not read the clock, generate randomness, call a service, or await work while
|
|
|
462
478
|
choosing a transition. Receive such values in an event or produce them through
|
|
463
479
|
state-owned work first.
|
|
464
480
|
|
|
481
|
+
A transition that throws follows one rule on every host: the error goes to the
|
|
482
|
+
sender when one waits for the event to be processed, and otherwise the machine
|
|
483
|
+
fails with it. `send` of `LocalMachine` returns before the event is processed,
|
|
484
|
+
so the machine fails; a `ClusterMachine` client waits, so it is rejected and the
|
|
485
|
+
machine goes on. An event that its schema refuses is different: every host
|
|
486
|
+
rejects it at `send`, `LocalMachine` with `MachineSchemaDecodeError` and
|
|
487
|
+
`ClusterMachine` as `InvalidEvent`, and the machine keeps running.
|
|
488
|
+
|
|
465
489
|
`resolve` is checked against `branches` in both directions, and each `select`
|
|
466
490
|
call takes the values of the path it is given. An agent can edit this one
|
|
467
491
|
transition without reading anything else: a missing entry, an entry that is never
|
|
@@ -534,9 +558,26 @@ const verifyCounter = Effect.gen(function*() {
|
|
|
534
558
|
From a shell, `effect-machine check src/counter.ts --json` from
|
|
535
559
|
`@typeonce/effect-machine-devtools` does the same for every machine a file exports.
|
|
536
560
|
|
|
537
|
-
To fix a finding, keep the failing evidence first.
|
|
538
|
-
it, make the smallest behavior change, then run
|
|
539
|
-
finding by weakening a type or a test.
|
|
561
|
+
To fix a finding, keep the failing evidence first. Pass its `replay` to
|
|
562
|
+
`MachineTest.run` to reproduce it, make the smallest behavior change, then run
|
|
563
|
+
the check again. Do not silence a finding by weakening a type or a test.
|
|
564
|
+
|
|
565
|
+
The check takes the results of invoked work as further branches while that work
|
|
566
|
+
runs. A timer elapses without any option. An Effect's results are only a type,
|
|
567
|
+
so supply the values to try for each registered source in `outcomes`; each value
|
|
568
|
+
is one branch, and the compiler rejects an unknown source or a value of the
|
|
569
|
+
wrong type:
|
|
570
|
+
|
|
571
|
+
```ts
|
|
572
|
+
const report = yield* MachineTest.check(LoadMachine, {
|
|
573
|
+
outcomes: {
|
|
574
|
+
loadDocument: { done: ["# Title"], failure: [new LoadError()] }
|
|
575
|
+
}
|
|
576
|
+
})
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
A replay lists the steps in order: events, and the results of work such as
|
|
580
|
+
`done Loading#loadDocument "# Title"` or `elapsed Waiting#timeout`.
|
|
540
581
|
|
|
541
582
|
| Law | Meaning | Fix |
|
|
542
583
|
| ---------------------------- | --------------------------------------------------------------- | ------------------------------------------------------------------- |
|
|
@@ -546,13 +587,18 @@ finding by weakening a type or a test.
|
|
|
546
587
|
| `planning.failed` | A planning callback threw or a value failed its schema. | Replay the events, then fix the callback or the value. |
|
|
547
588
|
| `planning.verification` | A planner law failed. This usually means an Effect Machine bug. | Report it with the replay. |
|
|
548
589
|
| `invariant.violated` | An invariant you supplied failed. | Replay the events, then fix the behavior or the invariant. |
|
|
549
|
-
| `
|
|
590
|
+
| `outcome.failsMachine` | A supplied result fails the machine, whose types were erased. | Add the missing `onFailure` or `onDone`. |
|
|
591
|
+
| `outcomes.invalid` | `outcomes` names no Effect source that a state invokes. | Fix the source name, or remove the entry. |
|
|
592
|
+
| `transition.notCovered` | Exploration never took a declared branch. This is a warning. | Cover it with a scenario or another outcome, or remove it. |
|
|
550
593
|
| `schema.underivable` | Input or events could not be sampled from their schemas. | Pass `input` or `events` explicitly. |
|
|
551
594
|
|
|
552
595
|
A report is `"exhaustive"` only when `limits` is empty. Otherwise a pass means
|
|
553
596
|
nothing was found in what was explored. Keep sampled checks, bounded exploration,
|
|
554
|
-
and proof distinct when reporting a result.
|
|
555
|
-
|
|
597
|
+
and proof distinct when reporting a result. Supplied outcomes add the
|
|
598
|
+
`customOutcomes` limit. An Effect without outcomes adds `effectsWithoutOutcomes`,
|
|
599
|
+
and Streams, logic, and child machines add `streams`, `logic`, and `children`:
|
|
600
|
+
their results are not explored, so test them on a started machine with
|
|
601
|
+
`MachineTest.probe`.
|
|
556
602
|
|
|
557
603
|
### Test paths and invariants
|
|
558
604
|
|
|
@@ -592,9 +638,10 @@ const testProgram = Effect.gen(function*() {
|
|
|
592
638
|
})
|
|
593
639
|
```
|
|
594
640
|
|
|
595
|
-
Use pure planner traces for state and transition rules
|
|
596
|
-
|
|
597
|
-
|
|
641
|
+
Use pure planner traces for state and transition rules, including the results
|
|
642
|
+
of invoked work, which a scenario lists beside its events. Start a live machine
|
|
643
|
+
and use `MachineTest.probe` when a test depends on how the work actually runs,
|
|
644
|
+
on raised events, or on runtime scheduling.
|
|
598
645
|
|
|
599
646
|
### Choose observation deliberately
|
|
600
647
|
|
|
@@ -624,7 +671,7 @@ Declare the topology, the protocol, and the behavior, then start the result
|
|
|
624
671
|
inside a Scope.
|
|
625
672
|
|
|
626
673
|
```ts
|
|
627
|
-
import { Machine } from "@typeonce/effect-machine"
|
|
674
|
+
import { LocalMachine, Machine } from "@typeonce/effect-machine"
|
|
628
675
|
import { Effect, Schema } from "effect"
|
|
629
676
|
|
|
630
677
|
// The root declares the topology and the data each state owns.
|
|
@@ -661,10 +708,10 @@ export const CounterMachine = Machine.make({
|
|
|
661
708
|
}
|
|
662
709
|
})
|
|
663
710
|
|
|
664
|
-
// `
|
|
711
|
+
// `LocalMachine.start` requires a Scope. Closing it stops the machine and everything
|
|
665
712
|
// the machine owns.
|
|
666
713
|
export const program = Effect.scoped(Effect.gen(function*() {
|
|
667
|
-
const counter = yield*
|
|
714
|
+
const counter = yield* LocalMachine.start(CounterMachine)
|
|
668
715
|
|
|
669
716
|
yield* counter.send(CounterEvents.Start())
|
|
670
717
|
yield* counter.send(CounterEvents.Increment())
|
|
@@ -864,7 +911,7 @@ const Document = Machine.make({
|
|
|
864
911
|
})
|
|
865
912
|
```
|
|
866
913
|
|
|
867
|
-
Start with `
|
|
914
|
+
Start with `LocalMachine.start(Document, { locale: "en", documentId: "intro" })`.
|
|
868
915
|
Root construction runs before its initial child construction. Both receive the
|
|
869
916
|
same validated input. `documentId` belongs only to Loading; it need not be stored
|
|
870
917
|
in root data. Input is not retained as a reset value on the machine or snapshot.
|
|
@@ -1004,7 +1051,7 @@ history: {
|
|
|
1004
1051
|
|
|
1005
1052
|
A fallback must explicitly construct a complete tree containing its owner,
|
|
1006
1053
|
including required ancestors and parallel siblings. Its root data is state data,
|
|
1007
|
-
not startup input. `
|
|
1054
|
+
not startup input. `LocalMachine.resume` restores a validated snapshot without rerunning
|
|
1008
1055
|
startup constructors.
|
|
1009
1056
|
|
|
1010
1057
|
`guard` declines before construction. A resolver may return `select.decline()` only
|
|
@@ -1610,6 +1657,311 @@ Do not wrap hydrated forms in a Suspense boundary that a startup read can
|
|
|
1610
1657
|
trigger. A suspended boundary replaces its server-rendered content with the
|
|
1611
1658
|
fallback and later mounts new DOM nodes, discarding typed text and focus.
|
|
1612
1659
|
|
|
1660
|
+
## Run a machine on Effect Cluster
|
|
1661
|
+
|
|
1662
|
+
`ClusterMachine` from `@typeonce/effect-machine/cluster` runs a machine as a
|
|
1663
|
+
persisted Cluster entity. It takes the same machine that `LocalMachine` runs.
|
|
1664
|
+
Nothing about how the machine is written changes.
|
|
1665
|
+
|
|
1666
|
+
### Choose a host
|
|
1667
|
+
|
|
1668
|
+
| Host | Where the machine lives | What survives a crash |
|
|
1669
|
+
| ---------------- | --------------------------------------------- | ---------------------------------------- |
|
|
1670
|
+
| `LocalMachine` | in memory, owned by a `Scope` | nothing, unless you encode and resume it |
|
|
1671
|
+
| `AtomMachine` | in memory, owned by an `AtomRegistry` | nothing |
|
|
1672
|
+
| `ClusterMachine` | one entity per id, on any runner of a Cluster | every committed step, from `Storage` |
|
|
1673
|
+
|
|
1674
|
+
Use `ClusterMachine` when a machine must outlive the process that started it,
|
|
1675
|
+
be addressed by id from anywhere, or wait on timers longer than a process
|
|
1676
|
+
lives. Effect Cluster owns identity, placement, routing, and delivery.
|
|
1677
|
+
`ClusterMachine` owns only the step: it applies one input at a time and commits
|
|
1678
|
+
the result to `Storage`.
|
|
1679
|
+
|
|
1680
|
+
`ClusterMachine` needs `effect` 4.1.5 or later. Earlier versions answer the
|
|
1681
|
+
keep-alive request of an entity after about ten minutes, so a machine whose
|
|
1682
|
+
work runs or whose timer waits longer would not come up again after a crash.
|
|
1683
|
+
|
|
1684
|
+
### Make an adapter
|
|
1685
|
+
|
|
1686
|
+
```ts
|
|
1687
|
+
import { Machine } from "@typeonce/effect-machine"
|
|
1688
|
+
import { ClusterMachine, SqlMachineStorage } from "@typeonce/effect-machine/cluster"
|
|
1689
|
+
import { Layer } from "effect"
|
|
1690
|
+
import { SqlMessageStorage } from "effect/cluster"
|
|
1691
|
+
|
|
1692
|
+
const Invoice = ClusterMachine.make("Invoice", invoiceMachine, { version: "1" })
|
|
1693
|
+
|
|
1694
|
+
// Provide with a Cluster runner layer and an `SqlClient`.
|
|
1695
|
+
const InvoiceLive = Invoice.toLayer().pipe(
|
|
1696
|
+
Layer.provide(Layer.mergeAll(SqlMessageStorage.layer, SqlMachineStorage.layer))
|
|
1697
|
+
)
|
|
1698
|
+
```
|
|
1699
|
+
|
|
1700
|
+
`version` is stored with every checkpoint. A checkpoint written by another
|
|
1701
|
+
definition id or version is refused, so change it together with a migration of
|
|
1702
|
+
the stored checkpoints. Pass `shardGroup` to `ClusterMachine.make` to route
|
|
1703
|
+
entities (and their invoked child entities) to named Cluster shard groups.
|
|
1704
|
+
|
|
1705
|
+
`adapter.toLayer(options)` forwards standard Effect Cluster `Entity.toLayer`
|
|
1706
|
+
options (`maxIdleTime`, `concurrency`, `mailboxCapacity`, `mailboxMetrics`,
|
|
1707
|
+
`disableFatalDefects`, `defectRetryPolicy`, `spanAttributes`) to the entity and
|
|
1708
|
+
its children. It also accepts transactional hooks that run inside the same
|
|
1709
|
+
`MessageStorage` transaction as the checkpoint write:
|
|
1710
|
+
|
|
1711
|
+
- `enqueue(event)` durably writes emitted events to an outbox table.
|
|
1712
|
+
- `onCommit({ entityId, checkpoint, snapshot })` runs after every committed
|
|
1713
|
+
checkpoint (active, done, failed), so SQL read-model projections or audit
|
|
1714
|
+
tables commit or roll back atomically with the machine state.
|
|
1715
|
+
|
|
1716
|
+
State values, completion outputs, startup input, and public input events must
|
|
1717
|
+
have JSON encoded forms. A schema that does not is a compile error that names
|
|
1718
|
+
the offending states, events, or input.
|
|
1719
|
+
|
|
1720
|
+
### What runs, and how often
|
|
1721
|
+
|
|
1722
|
+
Each live entity keeps one machine in memory and applies requests, the ends
|
|
1723
|
+
of invoked effects, and timers one at a time. `Storage` is the truth and
|
|
1724
|
+
memory is a cache: an entity that comes up anywhere continues from its last
|
|
1725
|
+
checkpoint.
|
|
1726
|
+
|
|
1727
|
+
Invoked effects run at least once. Work starts once the step that entered its
|
|
1728
|
+
state is stored, and runs again after a crash, a move to another runner, or a
|
|
1729
|
+
restart, with `Machine.InitialEvent` as the event of its input mapper. Two
|
|
1730
|
+
copies can briefly run at the same time while a shard moves. Exactly one result
|
|
1731
|
+
is applied, and results are not stored. Make every invoked effect safe to
|
|
1732
|
+
repeat, for example with an idempotency key from the state.
|
|
1733
|
+
|
|
1734
|
+
Work that ends in a way the model does not handle fails the machine, as it
|
|
1735
|
+
does locally. The failure is stored as text with the last state, the
|
|
1736
|
+
snapshot reads `failed`, and every later request is rejected as
|
|
1737
|
+
`MachineFailed`. There is no hidden retry: a model that recovers declares
|
|
1738
|
+
`onFailure`. A failure of storage or of Cluster itself never fails the
|
|
1739
|
+
machine; it is retried.
|
|
1740
|
+
|
|
1741
|
+
### Timers
|
|
1742
|
+
|
|
1743
|
+
A timer is stored as the time at which it is due, in the checkpoint of the step
|
|
1744
|
+
that enters its state. An entity that comes up again waits only for the time
|
|
1745
|
+
that remains, and completes a timer that is already due at once. A timer up to
|
|
1746
|
+
`DurableClock.InMemoryThreshold` (one minute by default) keeps its entity in
|
|
1747
|
+
memory. A longer one is saved as a delayed Cluster message, in the same
|
|
1748
|
+
transaction, so the entity may leave memory and is brought back when the timer
|
|
1749
|
+
is due, up to `entityMessagePollInterval` late.
|
|
1750
|
+
|
|
1751
|
+
This differs from `LocalMachine.resume`, which starts a timer's full duration
|
|
1752
|
+
again.
|
|
1753
|
+
|
|
1754
|
+
### Children and messages between machines
|
|
1755
|
+
|
|
1756
|
+
A child machine invoked by a state runs as an entity of its own, of type
|
|
1757
|
+
`Type/childId` and id `entityId/childId/revision`, and starts and stops with
|
|
1758
|
+
the stored state. `onDone`, `onFailure`, and `onSnapshot` work as they do
|
|
1759
|
+
locally. A failure of a child fails its parent, even with `onFailure`, because
|
|
1760
|
+
the failure is stored as text.
|
|
1761
|
+
|
|
1762
|
+
`adapter.target(id)` names another Cluster machine. `enqueue.sendTo` with a
|
|
1763
|
+
target or with the descriptor of an invoked child saves the event with the
|
|
1764
|
+
checkpoint of the step, so it is sent if and only if the step is stored, and
|
|
1765
|
+
applied once. `enqueue.stop` with the descriptor of an invoked child stops the
|
|
1766
|
+
running child in the same transaction and settles its progress so it does not
|
|
1767
|
+
restart while its state stays active. Commands run before the work of the states
|
|
1768
|
+
a step enters starts, as they do locally, so both drop a child that the same step
|
|
1769
|
+
enters. Work sends with `Machine.sendTo` and stops a child with
|
|
1770
|
+
`Machine.stopChild`, given the child's descriptor, and an input mapper does the
|
|
1771
|
+
same with `children.sendTo` and `children.stop`. `children.spawn` fails the
|
|
1772
|
+
machine.
|
|
1773
|
+
|
|
1774
|
+
### What is refused, and why
|
|
1775
|
+
|
|
1776
|
+
These do not compile with `ClusterMachine.make`. The error names the property
|
|
1777
|
+
`~effect/cluster/ClusterMachine/UnsupportedProcessLocal` and lists what to replace:
|
|
1778
|
+
|
|
1779
|
+
- a state that invokes a Stream or a `Logic`;
|
|
1780
|
+
- work that requires `Machine.MachineRuntime`, such as `Machine.spawn`, or
|
|
1781
|
+
`Machine.sendTo` and `Machine.stopChild` with a child address.
|
|
1782
|
+
|
|
1783
|
+
A checkpoint holds the state of a machine, and after a crash its work runs
|
|
1784
|
+
again from the start. An Effect or a timer ends with one outcome, so running it
|
|
1785
|
+
again changes nothing that was applied. A Stream ends after many elements, and
|
|
1786
|
+
how far it got lives only in memory; a `Logic` has a state and a mailbox of its
|
|
1787
|
+
own. Both would be lost. Model a source of many events as a child machine,
|
|
1788
|
+
whose every step is stored, or as an Effect that ends with one result and a
|
|
1789
|
+
state that is entered again for the next one.
|
|
1790
|
+
|
|
1791
|
+
### Talk to a machine from outside
|
|
1792
|
+
|
|
1793
|
+
```ts
|
|
1794
|
+
const program = Effect.gen(function*() {
|
|
1795
|
+
const client = (yield* Invoice.client)("invoice-1")
|
|
1796
|
+
yield* client.send(Events.Send({ invoiceId: "inv-1" }))
|
|
1797
|
+
const latest = yield* client.snapshot // Option of the last committed checkpoint
|
|
1798
|
+
const receipt = yield* client.join // waits for the output, or fails with MachineFailedError
|
|
1799
|
+
})
|
|
1800
|
+
```
|
|
1801
|
+
|
|
1802
|
+
`start` initializes an entity with its per-entity startup input (or commits the
|
|
1803
|
+
initial state of a machine without input without needing an event). It is an
|
|
1804
|
+
idempotent no-op when the entity is already active, and rejects as
|
|
1805
|
+
`MachineFailed` if the entity failed or was stopped. A machine that declares
|
|
1806
|
+
`input` can omit a static default input in `ClusterMachine.make` so each entity
|
|
1807
|
+
is started with `client.start(input)` (sending an event before start rejects as
|
|
1808
|
+
`InvalidEvent`), or pass a default input so `send` can auto-start while
|
|
1809
|
+
`client.start(customInput)` still starts with per-entity input.
|
|
1810
|
+
|
|
1811
|
+
`send` decodes the event as a local reference does and waits until the step is
|
|
1812
|
+
committed. An event the machine refuses fails with `Rejected` and its reason.
|
|
1813
|
+
Failures of Cluster itself are `ClientError`s; a request that ended in one may
|
|
1814
|
+
still have been delivered, and is applied at most once when sent again.
|
|
1815
|
+
|
|
1816
|
+
`snapshot`, `changes`, and `join` read `Storage`, never the entity, so they
|
|
1817
|
+
work from any process that reaches the store and never wake the entity. A
|
|
1818
|
+
client is a remote view, narrower than a `MachineRef`: it sees committed
|
|
1819
|
+
checkpoints only, `changes` polls every `readInterval` (one second by default)
|
|
1820
|
+
and may skip checkpoints in between, a failure is text rather than a `Cause`,
|
|
1821
|
+
and nothing is read before the first event is committed.
|
|
1822
|
+
|
|
1823
|
+
### Storage
|
|
1824
|
+
|
|
1825
|
+
`ClusterMachine.Storage` holds one checkpoint per entity and the ids of the
|
|
1826
|
+
requests it committed. `SqlMachineStorage` implements it for PostgreSQL and
|
|
1827
|
+
SQLite. Use it over the same `SqlClient` as `SqlMessageStorage`, so a
|
|
1828
|
+
checkpoint, the events it sends, and the reply commit in one transaction. Its
|
|
1829
|
+
`layer` runs migrations first; use `layerMigrations` in a deploy step and
|
|
1830
|
+
`layerStorage` at runtime to run them with another connection.
|
|
1831
|
+
`ClusterMachine.layerMemory` is for tests and is not durable.
|
|
1832
|
+
|
|
1833
|
+
To write a store of your own, run `ClusterMachine.checkStorage` against it in
|
|
1834
|
+
your tests, with the `withTransaction` of its `MessageStorage`. It fails with
|
|
1835
|
+
the first part of the contract the store breaks.
|
|
1836
|
+
|
|
1837
|
+
### Run in a Web Worker or over RPC
|
|
1838
|
+
|
|
1839
|
+
`ClusterMachine` and `SqlMachineStorage` have no Node.js dependencies, so a
|
|
1840
|
+
single-node Cluster can run inside a Web Worker or `SharedWorker` with
|
|
1841
|
+
`SingleRunner.layer` and a browser SQL client such as `@effect/sql-sqlite-wasm`
|
|
1842
|
+
or `@effect/sql-pglite`.
|
|
1843
|
+
|
|
1844
|
+
When the runner and its database live in another thread or process, callers do
|
|
1845
|
+
not have direct access to `Sharding` or `ClusterMachine.Storage`.
|
|
1846
|
+
`ClusterMachineProxy` from `@typeonce/effect-machine/cluster` exposes `start`,
|
|
1847
|
+
`send`, `snapshot`, `changes`, and `join` over any `effect/rpc` transport (Web
|
|
1848
|
+
Workers, `SharedWorker`, Node worker threads, WebSockets, or HTTP):
|
|
1849
|
+
|
|
1850
|
+
```ts
|
|
1851
|
+
// Inside the Web Worker: serve one machine over RpcServer.Protocol
|
|
1852
|
+
const WorkerLive = ClusterMachineProxy.toLayer(Invoice).pipe(
|
|
1853
|
+
Layer.provide(Invoice.toLayer()),
|
|
1854
|
+
Layer.provide(ClusterLive),
|
|
1855
|
+
Layer.provide(RpcServer.layerProtocolWorkerRunner)
|
|
1856
|
+
)
|
|
1857
|
+
|
|
1858
|
+
// On the main thread: zero Sharding, zero Storage, zero SQL
|
|
1859
|
+
const program = Effect.gen(function*() {
|
|
1860
|
+
const client = (yield* ClusterMachineProxy.client(Invoice))("invoice-1")
|
|
1861
|
+
yield* client.send(Events.Send({ invoiceId: "inv-1" }))
|
|
1862
|
+
const latest = yield* client.snapshot
|
|
1863
|
+
const receipt = yield* client.join
|
|
1864
|
+
})
|
|
1865
|
+
```
|
|
1866
|
+
|
|
1867
|
+
To host several machines on one worker or server, merge their groups with
|
|
1868
|
+
`ClusterMachineProxy.toRpcGroup(A).merge(ClusterMachineProxy.toRpcGroup(B))` and
|
|
1869
|
+
provide each with `ClusterMachineProxy.layerHandlers`. When using
|
|
1870
|
+
`RpcClient.layerProtocolWorker` with `size: 1`, set `concurrency` above `1` (for
|
|
1871
|
+
example `{ size: 1, concurrency: 100 }`) so streaming `changes` or `join` calls
|
|
1872
|
+
do not block concurrent `send` calls.
|
|
1873
|
+
|
|
1874
|
+
## Write a host for a new runtime
|
|
1875
|
+
|
|
1876
|
+
Applications run machines with `LocalMachine`, `AtomMachine`, or
|
|
1877
|
+
`ClusterMachine`. Use `Machine.toStep` only to run a machine somewhere these
|
|
1878
|
+
hosts do not reach, such as a UI store that keeps the state itself.
|
|
1879
|
+
|
|
1880
|
+
`Machine.toStep(machine, options)` returns pure functions. They run nothing and
|
|
1881
|
+
call no user code: each one returns the next state as a value, with the
|
|
1882
|
+
commands to carry out and the work to stop and to start.
|
|
1883
|
+
|
|
1884
|
+
```ts
|
|
1885
|
+
const step = Machine.toStep(machine, { self, parent, children })
|
|
1886
|
+
|
|
1887
|
+
step.start(input) // Exit<Step, StartError>: the machine starts
|
|
1888
|
+
step.send(state, event) // Exit<Step, Rejection>: a public event arrives
|
|
1889
|
+
step.settle(state, work, exit) // Step: work ended, or could not start
|
|
1890
|
+
step.element(state, work, value) // Step: a running Stream produced a value
|
|
1891
|
+
step.report(state, work, snapshot) // Step: a running Logic or child machine reached a state
|
|
1892
|
+
step.stop(state) // Step: the owner stops the machine
|
|
1893
|
+
step.resume(snapshot, progress) // Exit<Step, MachineSchemaDecodeError>: from a decoded snapshot
|
|
1894
|
+
```
|
|
1895
|
+
|
|
1896
|
+
The options are what the machine needs of its host:
|
|
1897
|
+
|
|
1898
|
+
- `self` is the host's own target. Transitions and input mappers receive it,
|
|
1899
|
+
so its `send` must plan the event in a later step.
|
|
1900
|
+
- `parent` is the target of the owner, for a machine that declares a parent.
|
|
1901
|
+
It is required when the parent is.
|
|
1902
|
+
- `children` are the operations on the children of the machine, which the host
|
|
1903
|
+
owns: `spawn`, `spawnLogic`, `sendTo`, and `stop`. They are required when work
|
|
1904
|
+
requires `Machine.MachineRuntime` or `Machine.ChildMachineRuntime`. Without
|
|
1905
|
+
them, every operation on children fails the machine with `ProcessLocalError`.
|
|
1906
|
+
|
|
1907
|
+
A host carries out each `Step` in this order: carry out `commands`, interrupt
|
|
1908
|
+
`stop`, publish `snapshot`, deliver `emitted`, start `start`, then interrupt
|
|
1909
|
+
`stopAfter`. Pass `step.state` to the next call.
|
|
1910
|
+
|
|
1911
|
+
- A failed `Exit` from `send`, `start`, or `resume` means nothing happened. An
|
|
1912
|
+
event is rejected because its schema refused it or because the machine has
|
|
1913
|
+
ended, and the state you passed still holds.
|
|
1914
|
+
- A machine that fails or ends still returns a `Step`. Its `exit` is set: when
|
|
1915
|
+
it reaches a final state, `stop` lists the work of exited states (interrupted
|
|
1916
|
+
before `snapshot` and `emitted`) and `stopAfter` lists the remaining work of
|
|
1917
|
+
states that stayed active (interrupted after `emitted`, latest first); when it
|
|
1918
|
+
fails or is stopped by its owner, `stop` lists every piece of work that still
|
|
1919
|
+
runs and `stopAfter` is empty.
|
|
1920
|
+
- `commands` are `Send` (call `target.send(event)`), `SendToChild`, and
|
|
1921
|
+
`StopChild`. A command to a child names the running `Logic` or child machine
|
|
1922
|
+
it selects as `work`. A stopped one no longer runs, and reports nothing. With
|
|
1923
|
+
no `work`, the command is for a child that work spawned.
|
|
1924
|
+
- `snapshot` and `microsteps` are built when first read. `event` is the event
|
|
1925
|
+
the step planned, for inspection.
|
|
1926
|
+
|
|
1927
|
+
Work is data. The host evaluates each piece as it starts it, in order, with the
|
|
1928
|
+
accessor of its kind. Each accessor calls the source and the input mapper at
|
|
1929
|
+
most once and returns an `Exit`:
|
|
1930
|
+
|
|
1931
|
+
- `Effect`: `work.effect()`, then run it and settle its `Exit`.
|
|
1932
|
+
- `Timer`: `work.duration()` holds how long to wait, then settle with
|
|
1933
|
+
`Exit.void`. It fails, like every accessor, when the timer cannot start,
|
|
1934
|
+
also when its input names no duration. `work.effect()` is the same timer as
|
|
1935
|
+
an Effect, for a host whose timers wait on fibers.
|
|
1936
|
+
- `Stream`: `work.stream()`. Report each element with `element`, pull the next
|
|
1937
|
+
element after that step, and settle with the end of the stream.
|
|
1938
|
+
- `Logic`: `work.logic()`. Run `initial` and `run` with contexts of the host,
|
|
1939
|
+
`report` each state it sets, and settle with the end of `run`.
|
|
1940
|
+
- `Child`: `work.input()` holds the arguments of its `start`. Host
|
|
1941
|
+
`work.child.machine` with `Machine.toStep` again, with this machine as its
|
|
1942
|
+
`parent`, `report` its snapshots, and settle with its `exit`.
|
|
1943
|
+
|
|
1944
|
+
A failed accessor means the work could not start: settle the work with that
|
|
1945
|
+
`Exit` at once. The machine fails and runs no more work, so do not start the rest
|
|
1946
|
+
of the step.
|
|
1947
|
+
|
|
1948
|
+
The state value records which entry of a state each piece of work belongs to.
|
|
1949
|
+
`settle`, `element`, and `report` ignore work whose state was exited, so the host
|
|
1950
|
+
never compares anything. Pass back the work object that a step listed: work is
|
|
1951
|
+
matched by identity, so a result from before a `resume` is ignored too.
|
|
1952
|
+
|
|
1953
|
+
A durable host passes `progress` to `resume`: `{ path, id, status }` for each
|
|
1954
|
+
invocation of an active state that `"Ended"` or is still `"Running"`. Ended work
|
|
1955
|
+
is not listed again. Running work is not in `start`; find it in `step.running`
|
|
1956
|
+
and settle it when it ends. To keep that progress, read `step.exited` and
|
|
1957
|
+
`step.entered`, the paths of the states a step left and entered, without
|
|
1958
|
+
building a snapshot: forget what you stored for the work of an exited state.
|
|
1959
|
+
A result that is ignored returns a step whose `state` is the state you passed.
|
|
1960
|
+
`ClusterMachine` is such a host.
|
|
1961
|
+
|
|
1962
|
+
A host that runs only some kinds of work refuses the others at compile time:
|
|
1963
|
+
`machine: M & Machine.MachineStep.Supported<M, "Stream">`.
|
|
1964
|
+
|
|
1613
1965
|
## Effect Machine implementation review
|
|
1614
1966
|
|
|
1615
1967
|
Use this review after a machine and its UI adapter have been implemented. Look
|
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ The package is pre-1.0: a clearer or safer long-term API takes priority over
|
|
|
28
28
|
backward compatibility. Breaking changes use minor releases, compatible fixes
|
|
29
29
|
use patch releases, and compatibility aliases are not added by default.
|
|
30
30
|
|
|
31
|
-
The core machine model
|
|
31
|
+
The core machine model does not depend on the host that runs it. Distributed identity, placement,
|
|
32
32
|
transport, routing, delivery, and remote lifecycle semantics belong to Effect
|
|
33
33
|
Cluster and are exposed only through explicit integration boundaries.
|
|
34
34
|
|
|
@@ -39,7 +39,8 @@ pnpm add @typeonce/effect-machine effect
|
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
`effect` is a peer dependency requiring stable `^4.0.0`. Keep any installed
|
|
42
|
-
`@effect/*` packages on the same version as `effect`.
|
|
42
|
+
`@effect/*` packages on the same version as `effect`. `ClusterMachine` needs
|
|
43
|
+
`effect` 4.1.5 or later.
|
|
43
44
|
|
|
44
45
|
Development and benchmarks use exact versions. CI checks both the pinned
|
|
45
46
|
minimum and the latest stable Effect 4 ecosystem with runtime, type, and
|
|
@@ -50,7 +51,7 @@ packed-consumer tests.
|
|
|
50
51
|
Declare the root once, then add behavior with event maps:
|
|
51
52
|
|
|
52
53
|
```ts
|
|
53
|
-
import { Machine } from "@typeonce/effect-machine"
|
|
54
|
+
import { LocalMachine, Machine } from "@typeonce/effect-machine"
|
|
54
55
|
import { Effect, Schema } from "effect"
|
|
55
56
|
const Root = Machine.state({ fields: { count: Schema.Number } })
|
|
56
57
|
const Events = Machine.events({ Increment: { by: Schema.Number } })
|
|
@@ -66,7 +67,7 @@ const Counter = Machine.make({
|
|
|
66
67
|
}
|
|
67
68
|
})
|
|
68
69
|
const program = Effect.scoped(Effect.gen(function*() {
|
|
69
|
-
const ref = yield*
|
|
70
|
+
const ref = yield* LocalMachine.start(Counter)
|
|
70
71
|
yield* ref.send(Events.Increment({ by: 1 }))
|
|
71
72
|
}))
|
|
72
73
|
```
|
|
@@ -75,6 +76,10 @@ Root data is shared state. `update: { root: ... }` replaces that value while
|
|
|
75
76
|
retaining the active child configuration and its running work. Ordinary
|
|
76
77
|
transitions target a descendant; the root itself is not a destination.
|
|
77
78
|
|
|
79
|
+
`Machine` declares the model. `LocalMachine` runs it in memory, and other hosts
|
|
80
|
+
run the same model elsewhere: `AtomMachine` in an Atom registry, and
|
|
81
|
+
`ClusterMachine` as a persisted Cluster entity.
|
|
82
|
+
|
|
78
83
|
A started machine is owned by the enclosing `Scope`. Closing it is equivalent
|
|
79
84
|
to `ref.stop`: invocations are interrupted, owned children stop, and later
|
|
80
85
|
sends fail with `StoppedError`. Own a `Scope` explicitly when a machine must
|
|
@@ -318,7 +323,7 @@ Handlers receive typed `self` and, when declared, `parent` references. Resolvers
|
|
|
318
323
|
use their second `enqueue` parameter to raise, emit, send, or stop work.
|
|
319
324
|
Planning stays synchronous: perform asynchronous work in an invocation.
|
|
320
325
|
|
|
321
|
-
`
|
|
326
|
+
`LocalMachine.prepare(machine)` exposes an `inspection` stream for the live local
|
|
322
327
|
machine tree; subscribe before running `prepared.start` to observe startup. Distributed
|
|
323
328
|
placement, transport, routing, and identity belong to the explicit Cluster
|
|
324
329
|
adapter.
|
|
@@ -592,7 +597,7 @@ Snapshots can be validated for storage or transport:
|
|
|
592
597
|
const resumed = Effect.gen(function*() {
|
|
593
598
|
const encoded = yield* Machine.encodeSnapshot(machine, snapshot)
|
|
594
599
|
const decoded = yield* Machine.decodeSnapshot(machine, encoded)
|
|
595
|
-
return yield*
|
|
600
|
+
return yield* LocalMachine.resume(machine, decoded) // owned by the current Scope
|
|
596
601
|
})
|
|
597
602
|
```
|
|
598
603
|
|
|
@@ -610,6 +615,50 @@ full duration, and prior fibers, subscriptions, queues, and child runtimes are
|
|
|
610
615
|
not restored. Store machine identity and migration/version metadata beside the
|
|
611
616
|
encoded snapshot.
|
|
612
617
|
|
|
618
|
+
## Run on Effect Cluster
|
|
619
|
+
|
|
620
|
+
`ClusterMachine` runs the same machine as a persisted Cluster entity, one per
|
|
621
|
+
entity id. Effect Cluster owns identity, placement, routing, and delivery;
|
|
622
|
+
`ClusterMachine` applies one input at a time and commits each step to
|
|
623
|
+
`Storage`, which is the truth. An entity that comes up on any runner
|
|
624
|
+
continues from its last checkpoint.
|
|
625
|
+
|
|
626
|
+
```ts
|
|
627
|
+
import { ClusterMachine, SqlMachineStorage } from "@typeonce/effect-machine/cluster"
|
|
628
|
+
import { Layer } from "effect"
|
|
629
|
+
import { SqlMessageStorage } from "effect/cluster"
|
|
630
|
+
|
|
631
|
+
const Invoice = ClusterMachine.make("Invoice", invoiceMachine, { version: "1" })
|
|
632
|
+
const InvoiceLive = Invoice.toLayer().pipe(
|
|
633
|
+
Layer.provide(Layer.mergeAll(SqlMessageStorage.layer, SqlMachineStorage.layer))
|
|
634
|
+
)
|
|
635
|
+
|
|
636
|
+
const client = (yield* Invoice.client)("invoice-1")
|
|
637
|
+
yield* client.send(Events.Send({ invoiceId: "inv-1" }))
|
|
638
|
+
const receipt = yield* client.join
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
What to know before you deploy:
|
|
642
|
+
|
|
643
|
+
- invoked effects run **at least once**, and again after a crash, a move, or a
|
|
644
|
+
restart, so make them safe to repeat;
|
|
645
|
+
- work that ends in a way the model does not handle stores the machine as
|
|
646
|
+
`failed`, with no hidden retry; storage and Cluster failures are retried;
|
|
647
|
+
- timers store their due time and wait only for what remains after a restart;
|
|
648
|
+
long ones are delayed Cluster messages, so the entity may leave memory;
|
|
649
|
+
- child machines run as entities of their own, and machines send to each other
|
|
650
|
+
through `adapter.target(id)`, applied once;
|
|
651
|
+
- Streams, `Logic`, and work that needs `Machine.MachineRuntime` do not compile,
|
|
652
|
+
because their progress cannot be stored;
|
|
653
|
+
- a client reads committed checkpoints from `Storage`, and is a remote view
|
|
654
|
+
without the synchronous guarantees of a local reference;
|
|
655
|
+
- `SqlMachineStorage` stores checkpoints in PostgreSQL or SQLite, in the
|
|
656
|
+
transaction of `SqlMessageStorage`; check a store of your own with
|
|
657
|
+
`ClusterMachine.checkStorage`.
|
|
658
|
+
|
|
659
|
+
`ClusterMachine` needs `effect` 4.1.5 or later. The agent documentation covers
|
|
660
|
+
each point in full.
|
|
661
|
+
|
|
613
662
|
## Testing
|
|
614
663
|
|
|
615
664
|
Start with `MachineTest.check`. It reads the declared topology, explores the
|
|
@@ -681,7 +730,7 @@ paths and seeds do not preserve the same generated cases after migration.
|
|
|
681
730
|
## Entrypoints
|
|
682
731
|
|
|
683
732
|
```ts
|
|
684
|
-
import { Machine } from "@typeonce/effect-machine"
|
|
733
|
+
import { LocalMachine, Machine } from "@typeonce/effect-machine"
|
|
685
734
|
import { ClusterMachine } from "@typeonce/effect-machine/cluster"
|
|
686
735
|
import { AtomMachine } from "@typeonce/effect-machine/reactivity"
|
|
687
736
|
import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Declare the topology, the protocol, and the behavior, then start the result
|
|
5
5
|
* inside a Scope.
|
|
6
6
|
*/
|
|
7
|
-
import { Machine } from "@typeonce/effect-machine"
|
|
7
|
+
import { LocalMachine, Machine } from "@typeonce/effect-machine"
|
|
8
8
|
import { Effect, Schema } from "effect"
|
|
9
9
|
|
|
10
10
|
// The root declares the topology and the data each state owns.
|
|
@@ -41,10 +41,10 @@ export const CounterMachine = Machine.make({
|
|
|
41
41
|
}
|
|
42
42
|
})
|
|
43
43
|
|
|
44
|
-
// `
|
|
44
|
+
// `LocalMachine.start` requires a Scope. Closing it stops the machine and everything
|
|
45
45
|
// the machine owns.
|
|
46
46
|
export const program = Effect.scoped(Effect.gen(function*() {
|
|
47
|
-
const counter = yield*
|
|
47
|
+
const counter = yield* LocalMachine.start(CounterMachine)
|
|
48
48
|
|
|
49
49
|
yield* counter.send(CounterEvents.Start())
|
|
50
50
|
yield* counter.send(CounterEvents.Increment())
|