@typeonce/effect-machine 0.46.0 → 0.47.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 +1823 -0
- package/CLAUDE.md +1823 -0
- package/README.md +100 -77
- package/ai-docs/README.md +18 -0
- package/ai-docs/src/10_modeling/01_create-a-machine.ts +51 -0
- package/ai-docs/src/10_modeling/02_branch-on-an-event.ts +55 -0
- package/ai-docs/src/10_modeling/03_update-retained-state.ts +58 -0
- package/ai-docs/src/10_modeling/04_verify-after-changing.ts +29 -0
- package/{docs/agent-guide.md → ai-docs/src/10_modeling/index.md} +234 -155
- package/{docs/root-api.md → ai-docs/src/20_root-api/index.md} +29 -57
- package/{docs/effect-atom-react.md → ai-docs/src/30_react/index.md} +42 -49
- package/{docs/machine-review.md → ai-docs/src/40_review/index.md} +42 -40
- package/ai-docs/src/index.md +7 -0
- package/ai-docs/tsconfig.json +22 -0
- package/dist/Machine.d.ts +1195 -1474
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +366 -214
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/atom.js.map +1 -1
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/command.d.ts +8 -0
- package/dist/internal/machine/command.d.ts.map +1 -1
- package/dist/internal/machine/command.js +2 -2
- package/dist/internal/machine/command.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts +21 -5
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +1 -0
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/construction.js.map +1 -1
- package/dist/internal/machine/declaration.d.ts +2 -1
- package/dist/internal/machine/declaration.d.ts.map +1 -1
- package/dist/internal/machine/declaration.js +28 -46
- package/dist/internal/machine/declaration.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +23 -16
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +2 -0
- package/dist/internal/machine/implementation.d.ts.map +1 -1
- package/dist/internal/machine/implementation.js.map +1 -1
- package/dist/internal/machine/initialDeclaration.d.ts +2 -0
- package/dist/internal/machine/initialDeclaration.d.ts.map +1 -1
- package/dist/internal/machine/initialDeclaration.js +15 -0
- package/dist/internal/machine/initialDeclaration.js.map +1 -1
- package/dist/internal/machine/invocation.d.ts.map +1 -1
- package/dist/internal/machine/invocation.js +25 -19
- package/dist/internal/machine/invocation.js.map +1 -1
- package/dist/internal/machine/invocationEvent.d.ts +34 -17
- package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
- package/dist/internal/machine/invocationEvent.js +16 -4
- package/dist/internal/machine/invocationEvent.js.map +1 -1
- package/dist/internal/machine/machine.d.ts +5 -22
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +157 -119
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +15 -4
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +18 -21
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +2 -0
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +2 -0
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.js.map +1 -1
- package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -1
- package/dist/internal/machine/runtimeGeneric.js.map +1 -1
- package/dist/internal/machine/runtimeProtocol.d.ts +26 -0
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
- package/dist/internal/machine/runtimeProtocol.js +71 -0
- package/dist/internal/machine/runtimeProtocol.js.map +1 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +0 -14
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- package/dist/internal/machine/symbols.d.ts +9 -0
- package/dist/internal/machine/symbols.d.ts.map +1 -1
- package/dist/internal/machine/symbols.js.map +1 -1
- package/dist/internal/machine/topology.d.ts +18 -9
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js +16 -1
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/arbitrary.d.ts.map +1 -1
- package/dist/internal/testing/machine/arbitrary.js +2 -0
- package/dist/internal/testing/machine/arbitrary.js.map +1 -1
- package/dist/internal/testing/machine/check.d.ts +17 -0
- package/dist/internal/testing/machine/check.d.ts.map +1 -0
- package/dist/internal/testing/machine/check.js +371 -0
- package/dist/internal/testing/machine/check.js.map +1 -0
- package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
- package/dist/internal/testing/machine/exploration.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts +14 -13
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +21 -23
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/runtime.d.ts +19 -88
- package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
- package/dist/internal/testing/machine/runtime.js +0 -19
- package/dist/internal/testing/machine/runtime.js.map +1 -1
- package/dist/internal/testing/machine/runtimeInvariant.js.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +349 -139
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +201 -136
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +9 -14
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +9 -14
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +169 -80
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +165 -76
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/package.json +11 -9
- package/src/Machine.ts +1139 -2130
- package/src/internal/machine/atom.ts +3 -3
- package/src/internal/machine/cluster.ts +2 -2
- package/src/internal/machine/command.ts +10 -4
- package/src/internal/machine/configuration.ts +16 -17
- package/src/internal/machine/construction.ts +1 -1
- package/src/internal/machine/declaration.ts +25 -50
- package/src/internal/machine/executionPlan.ts +90 -56
- package/src/internal/machine/implementation.ts +3 -1
- package/src/internal/machine/initialDeclaration.ts +16 -0
- package/src/internal/machine/invocation.ts +38 -44
- package/src/internal/machine/invocationEvent.ts +46 -20
- package/src/internal/machine/machine.ts +217 -243
- package/src/internal/machine/planner.ts +72 -106
- package/src/internal/machine/process.ts +12 -0
- package/src/internal/machine/protocol.ts +6 -6
- package/src/internal/machine/runtime.ts +1 -5
- package/src/internal/machine/runtimeCompiled.ts +2 -2
- package/src/internal/machine/runtimeGeneric.ts +2 -4
- package/src/internal/machine/runtimeProtocol.ts +80 -4
- package/src/internal/machine/stateDefinition.ts +0 -39
- package/src/internal/machine/symbols.ts +11 -0
- package/src/internal/machine/topology.ts +40 -18
- package/src/internal/testing/machine/arbitrary.ts +2 -0
- package/src/internal/testing/machine/check.ts +460 -0
- package/src/internal/testing/machine/exploration.ts +4 -5
- package/src/internal/testing/machine/finiteModel.ts +40 -42
- package/src/internal/testing/machine/runtime.ts +31 -170
- package/src/internal/testing/machine/runtimeInvariant.ts +3 -3
- package/src/internal/testing/machine/verification.ts +14 -14
- package/src/testing/MachineTest.ts +366 -145
- package/src/unstable/cluster/ClusterMachine.ts +9 -14
- package/src/unstable/reactivity/AtomMachine.ts +167 -78
package/README.md
CHANGED
|
@@ -7,8 +7,7 @@ Effect Schema. The same definition can be planned synchronously, run as a
|
|
|
7
7
|
managed machine, mounted as an Atom, tested as a model, or hosted by the
|
|
8
8
|
cluster adapter.
|
|
9
9
|
|
|
10
|
-
> This is early-release software. Its API may change
|
|
11
|
-
> one exact Effect beta.
|
|
10
|
+
> This is early-release software. Its API may change.
|
|
12
11
|
|
|
13
12
|
## Design principles
|
|
14
13
|
|
|
@@ -18,8 +17,10 @@ cluster adapter.
|
|
|
18
17
|
- **Explicit and opinionated:** give different semantics different names and
|
|
19
18
|
contracts. Builders and inference remove ceremony without making behavior
|
|
20
19
|
depend on ambiguous omissions.
|
|
21
|
-
- **
|
|
22
|
-
|
|
20
|
+
- **Local, unambiguous models:** keep each decision next to the state that owns
|
|
21
|
+
it, and give every key one meaning, so a model can be edited and verified one
|
|
22
|
+
transition at a time. Prefer an explicit declaration over a shorter ambiguous
|
|
23
|
+
one.
|
|
23
24
|
- **Effect-native:** design toward eventual inclusion in Effect core and follow
|
|
24
25
|
its API shape, module boundaries, ownership, and failure conventions.
|
|
25
26
|
|
|
@@ -34,7 +35,7 @@ Cluster and are exposed only through explicit integration boundaries.
|
|
|
34
35
|
## Install
|
|
35
36
|
|
|
36
37
|
```sh
|
|
37
|
-
pnpm add @typeonce/effect-machine effect
|
|
38
|
+
pnpm add @typeonce/effect-machine effect
|
|
38
39
|
```
|
|
39
40
|
|
|
40
41
|
`effect` is a peer dependency requiring stable `^4.0.0`. Keep any installed
|
|
@@ -60,8 +61,7 @@ const Counter = Machine.make({
|
|
|
60
61
|
root: () => ({ count: 0 }),
|
|
61
62
|
on: {
|
|
62
63
|
Increment: {
|
|
63
|
-
update:
|
|
64
|
-
data: ({ root, event }) => ({ count: root.count + event.by })
|
|
64
|
+
update: { root: ({ root, event }) => ({ count: root.count + event.by }) }
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
})
|
|
@@ -71,7 +71,7 @@ const program = Effect.scoped(Effect.gen(function*() {
|
|
|
71
71
|
}))
|
|
72
72
|
```
|
|
73
73
|
|
|
74
|
-
Root data is shared state. `update:
|
|
74
|
+
Root data is shared state. `update: { root: ... }` replaces that value while
|
|
75
75
|
retaining the active child configuration and its running work. Ordinary
|
|
76
76
|
transitions target a descendant; the root itself is not a destination.
|
|
77
77
|
|
|
@@ -88,9 +88,10 @@ boolean flags that allow impossible combinations. Put shared behavior on the
|
|
|
88
88
|
lowest common ancestor, and reserve parallel states for independent modes.
|
|
89
89
|
|
|
90
90
|
`Machine.state` declares topology. `Machine.make` captures protocols, startup
|
|
91
|
-
input,
|
|
92
|
-
|
|
93
|
-
independent implementations by calling
|
|
91
|
+
input, and sources. `.handle` implements behavior in one nested object, with each
|
|
92
|
+
transition declared next to the state that owns it, and returns an executable
|
|
93
|
+
machine. One definition can create independent implementations by calling
|
|
94
|
+
`.handle` more than once.
|
|
94
95
|
|
|
95
96
|
### Declare startup data and initial edges
|
|
96
97
|
|
|
@@ -191,31 +192,25 @@ unrestricted destination builder in the callback.
|
|
|
191
192
|
|
|
192
193
|
### Declare advanced and conditional transitions
|
|
193
194
|
|
|
194
|
-
A
|
|
195
|
-
|
|
196
|
-
before any resolver executes:
|
|
195
|
+
A transition that chooses between outcomes or constructs nested children lists
|
|
196
|
+
every possible outcome in `branches`, next to the `resolve` callback that picks
|
|
197
|
+
one. The list is captured before any resolver executes:
|
|
197
198
|
|
|
198
199
|
```ts
|
|
199
200
|
const machine = Machine.make({
|
|
200
201
|
root: Root,
|
|
201
|
-
events: Events
|
|
202
|
-
branches: {
|
|
203
|
-
search: {
|
|
204
|
-
loading: { target: "Loading", title: "Query supplied" },
|
|
205
|
-
idle: { target: "Idle", title: "Empty query" }
|
|
206
|
-
}
|
|
207
|
-
}
|
|
202
|
+
events: Events
|
|
208
203
|
}).handle({
|
|
209
204
|
initial: { target: "Idle" },
|
|
210
205
|
states: {
|
|
211
206
|
Idle: {
|
|
212
207
|
on: {
|
|
213
208
|
Search: {
|
|
214
|
-
branches: "
|
|
209
|
+
branches: [{ target: "Loading" }, { target: "Idle" }],
|
|
215
210
|
resolve: ({ event, select }) =>
|
|
216
211
|
event.query.length > 0
|
|
217
|
-
? select.
|
|
218
|
-
: select.
|
|
212
|
+
? select.target("Loading", { data: { query: event.query } })
|
|
213
|
+
: select.target("Idle")
|
|
219
214
|
}
|
|
220
215
|
}
|
|
221
216
|
}
|
|
@@ -223,19 +218,24 @@ const machine = Machine.make({
|
|
|
223
218
|
})
|
|
224
219
|
```
|
|
225
220
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
`select.
|
|
221
|
+
The compiler checks `resolve` against `branches` in both directions: selecting an
|
|
222
|
+
undeclared entry and declaring an entry that is never selected are both errors, and
|
|
223
|
+
each one names the entry to add or remove. A resolver must return a `select` call on
|
|
224
|
+
every path, and `select.target` takes the values of the path it is given. For a
|
|
225
|
+
compound target,
|
|
226
|
+
`select.target("Checkout", { data: parentValues, states: { Review: { data: childValues } } })`
|
|
230
227
|
constructs the explicitly selected subtree. Parallel constructors require every
|
|
231
228
|
entered region; source-local construction can retain active sibling regions. Every compound has an initial edge, including inactive branches. Final outputs,
|
|
232
229
|
history defaults, and choices remain part of machine readiness checking.
|
|
233
230
|
|
|
234
|
-
Use `guard: context => boolean` to decline before
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
231
|
+
Use `guard: context => boolean` to decline before commands or construction, and
|
|
232
|
+
`commands: (context, enqueue) => ...` to enqueue work on any plain transition. A
|
|
233
|
+
resolver that can decline lists `{ decline: true }` and returns `select.decline()`.
|
|
234
|
+
`guard` and `decline` exist only on `on` handlers and `always`. An invocation
|
|
235
|
+
outcome, a completion, and a choice have no other candidate to fall through to, so
|
|
236
|
+
they use `{ stay: true }` or select a destination. `reenter: true` restarts the
|
|
237
|
+
handler's source where that operation supports reentry. A retained source-value
|
|
238
|
+
update does not restart its lifecycle.
|
|
239
239
|
|
|
240
240
|
### Select destinations and retain owners
|
|
241
241
|
|
|
@@ -244,15 +244,26 @@ spelled the same way as snapshot paths. The root node is `"root"`, so a
|
|
|
244
244
|
top-level state cannot use that name. Paths are checked at compile time and
|
|
245
245
|
suggested by the editor:
|
|
246
246
|
|
|
247
|
-
|
|
|
248
|
-
| ------------------------------------------ | ------------------------------------------------------------------------- |
|
|
249
|
-
| `{ target: "Checkout.Review", data: ... }` | Enter a declared destination with its value. |
|
|
250
|
-
| `{ target: "Checkout", data: ... }` | Enter the declared initial configuration of a compound or parallel state. |
|
|
251
|
-
| `{
|
|
252
|
-
| `{
|
|
253
|
-
| `{ update:
|
|
254
|
-
| `{
|
|
255
|
-
| `{
|
|
247
|
+
| Plain transition | Entry in `branches` | Call in `resolve` | Meaning |
|
|
248
|
+
| ------------------------------------------ | -------------------------------- | -------------------------------------------------------- | ------------------------------------------------------------------------- |
|
|
249
|
+
| `{ target: "Checkout.Review", data: ... }` | `{ target: "Checkout.Review" }` | `select.target("Checkout.Review", { data })` | Enter a declared destination with its value. |
|
|
250
|
+
| `{ target: "Checkout", data: ... }` | `{ target: "Checkout" }` | `select.target("Checkout", { data })` | Enter the declared initial configuration of a compound or parallel state. |
|
|
251
|
+
| `{ target, update: { Checkout: ... } }` | `{ target, update: "Checkout" }` | `select.target(target, { update: { Checkout: value } })` | Enter a destination and replace one retained owner's value. |
|
|
252
|
+
| `{ history: "Checkout.recent" }` | `{ history: "Checkout.recent" }` | `select.history("Checkout.recent")` | Restore a declared history state. |
|
|
253
|
+
| `{ update: { Checkout: ... } }` | `{ update: "Checkout" }` | `select.update({ Checkout: value })` | Replace a retained active owner's value. |
|
|
254
|
+
| `{ update: { root: ... } }` | `{ update: "root" }` | `select.update({ root: value })` | Replace root data and retain active descendants. |
|
|
255
|
+
| `{ initialize: ... }` | `{ initialize: true }` | `select.initialize({ input })` | Reconstruct root and its initial children from fresh machine input. |
|
|
256
|
+
| `{ stay: true }` | `{ stay: true }` | `select.stay()` | Accept an event without changing the configuration. |
|
|
257
|
+
| `{ reenter: true }` | | | Exit and reenter the source. |
|
|
258
|
+
| | `{ decline: true }` | `select.decline()` | Refuse the event, and let the next candidate handle it. |
|
|
259
|
+
|
|
260
|
+
In a plain transition `update` is a record holding the new value, with exactly one
|
|
261
|
+
owner. A `branches` entry has no values yet, so it names the owner as a string; the
|
|
262
|
+
value is passed to `select`. `data` only ever means the target's value.
|
|
263
|
+
|
|
264
|
+
A `{ stay: true }` in one parallel region accepts the event, so it blocks a handler
|
|
265
|
+
for that event on a parallel ancestor, while `{ decline: true }` lets that handler
|
|
266
|
+
run.
|
|
256
267
|
|
|
257
268
|
Declarations written inline are checked in place. A declaration stored in a
|
|
258
269
|
variable before it reaches `make` or `.handle` needs `as const` so its paths
|
|
@@ -263,41 +274,34 @@ An atomic transition can enter a destination and update one retained owner:
|
|
|
263
274
|
```ts
|
|
264
275
|
Save: {
|
|
265
276
|
target: "Checkout.Saving",
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
target: { request: event.request },
|
|
269
|
-
update: { ...ancestors.Checkout, revision: event.revision }
|
|
270
|
-
})
|
|
277
|
+
data: ({ event }) => ({ request: event.request }),
|
|
278
|
+
update: { Checkout: ({ event, ancestors }) => ({ ...ancestors.Checkout, revision: event.revision }) }
|
|
271
279
|
}
|
|
272
280
|
```
|
|
273
281
|
|
|
274
282
|
The complete replacement values are validated before either change is applied.
|
|
275
|
-
Advanced construction declares both paths in a branch and uses
|
|
276
|
-
`select.saved({ data: destinationValues, update: { data: ownerValues } })`.
|
|
277
283
|
A retained owner must be active for that source and remain active through the
|
|
278
284
|
transition. A sibling region's value cannot be updated through this operation.
|
|
279
285
|
|
|
280
|
-
The destination can be a choice
|
|
281
|
-
|
|
286
|
+
The destination can be a choice, which has no `data` of its own. The owner is
|
|
287
|
+
updated first, so the choice resolver reads the new value:
|
|
282
288
|
|
|
283
289
|
```ts
|
|
284
290
|
Bump: {
|
|
285
291
|
target: "Route",
|
|
286
|
-
update:
|
|
287
|
-
data: ({ root }) => ({ count: root.count + 1 })
|
|
292
|
+
update: { root: ({ root }) => ({ count: root.count + 1 }) }
|
|
288
293
|
}
|
|
289
294
|
```
|
|
290
295
|
|
|
291
296
|
Every branch the choice can reach, including through nested choices, must stay
|
|
292
|
-
below the owner.
|
|
293
|
-
`select.route({ update: { data: ownerValues } })`.
|
|
297
|
+
below the owner.
|
|
294
298
|
|
|
295
299
|
The runtime transition API does not replace arbitrary complete root
|
|
296
300
|
configurations. The root is not a destination. `initialize` restarts it: it
|
|
297
301
|
accepts fresh machine input, or `true` for a machine without input, and follows
|
|
298
302
|
the root constructor and initial declarations in `.handle`. Add `reenter: true`
|
|
299
303
|
to also exit and re-enter the root lifecycle. A branch declares
|
|
300
|
-
`{ initialize: true }` and its resolver supplies `select.
|
|
304
|
+
`{ initialize: true }` and its resolver supplies `select.initialize({ input })`.
|
|
301
305
|
History defaults retain complete subtree construction for restoration.
|
|
302
306
|
|
|
303
307
|
### Protocols and ownership
|
|
@@ -322,7 +326,7 @@ adapter.
|
|
|
322
326
|
## Statechart capabilities
|
|
323
327
|
|
|
324
328
|
The same root supports compound and parallel states, eventless `always`
|
|
325
|
-
transitions,
|
|
329
|
+
transitions, choice states, final states, `onDone` completion,
|
|
326
330
|
initial declarations, shallow and deep history, and retained snapshots. The topology
|
|
327
331
|
is captured directly from declarations; visualization and branch coverage do
|
|
328
332
|
not execute user resolvers or depend on TypeScript source extraction.
|
|
@@ -391,17 +395,17 @@ const invocations = [
|
|
|
391
395
|
{
|
|
392
396
|
src: "updates",
|
|
393
397
|
onElement: {
|
|
394
|
-
|
|
395
|
-
|
|
398
|
+
stay: true,
|
|
399
|
+
commands: ({ element }, enqueue) => {
|
|
396
400
|
enqueue.raise(Events.Changed({ value: element }))
|
|
397
401
|
}
|
|
398
402
|
},
|
|
399
|
-
onDone: {
|
|
400
|
-
onFailure: {
|
|
403
|
+
onDone: { stay: true },
|
|
404
|
+
onFailure: { stay: true }
|
|
401
405
|
},
|
|
402
406
|
{ src: "timeout", onDone: { target: "Expired" } },
|
|
403
|
-
{ src: "worker", id: "worker", address: WorkerAddress, onSnapshot: {
|
|
404
|
-
{ src: "validation", input: ({ state }) => ({ query: state.query }), onDone: {
|
|
407
|
+
{ src: "worker", id: "worker", address: WorkerAddress, onSnapshot: { stay: true } },
|
|
408
|
+
{ src: "validation", input: ({ state }) => ({ query: state.query }), onDone: { stay: true } }
|
|
405
409
|
]
|
|
406
410
|
```
|
|
407
411
|
|
|
@@ -444,7 +448,7 @@ Prefer `ref.join` for final output, invocation `onDone` / `onFailure` for workfl
|
|
|
444
448
|
behavior, atom selectors for UI reads, and `ref.changes` for continuous observation.
|
|
445
449
|
|
|
446
450
|
```ts
|
|
447
|
-
const ready =
|
|
451
|
+
const ready = Machine.waitFor(ref, isReady).pipe(Effect.timeout("5 seconds"))
|
|
448
452
|
```
|
|
449
453
|
|
|
450
454
|
The predicate runs first, including for terminal snapshots. An unmatched failure
|
|
@@ -500,8 +504,8 @@ type ProcessMachineAtom = ReturnType<typeof makeProcessMachine>
|
|
|
500
504
|
Each call creates a fresh lazy bridge. `factory` does not cache by input;
|
|
501
505
|
`AtomMachine.family` remains the keyed shared-identity interface.
|
|
502
506
|
|
|
503
|
-
The bridge exposes `ref`, `snapshot`,
|
|
504
|
-
`
|
|
507
|
+
The bridge exposes `ref`, `snapshot`, fail-aware `result`, writable `send` and
|
|
508
|
+
`stop` atoms, and `child(descriptor)`. Use `AtomMachine.select`,
|
|
505
509
|
`AtomMachine.selectSnapshot`, and `AtomMachine.matches` for typed,
|
|
506
510
|
equality-aware derivations. Repeating one of these calls with the same bridge
|
|
507
511
|
and state path returns the same atom.
|
|
@@ -585,9 +589,11 @@ encoded snapshots are rejected; migrate persisted data explicitly before decodin
|
|
|
585
589
|
Snapshots can be validated for storage or transport:
|
|
586
590
|
|
|
587
591
|
```ts
|
|
588
|
-
const
|
|
589
|
-
const
|
|
590
|
-
const
|
|
592
|
+
const resumed = Effect.gen(function*() {
|
|
593
|
+
const encoded = yield* Machine.encodeSnapshot(machine, snapshot)
|
|
594
|
+
const decoded = yield* Machine.decodeSnapshot(machine, encoded)
|
|
595
|
+
return yield* Machine.resume(machine, decoded) // owned by the current Scope
|
|
596
|
+
})
|
|
591
597
|
```
|
|
592
598
|
|
|
593
599
|
Decoded snapshots are local runtime values and may contain class instances or
|
|
@@ -606,7 +612,25 @@ encoded snapshot.
|
|
|
606
612
|
|
|
607
613
|
## Testing
|
|
608
614
|
|
|
609
|
-
|
|
615
|
+
Start with `MachineTest.check`. It reads the declared topology, explores the
|
|
616
|
+
machine with events derived from its schemas, verifies every explored transition,
|
|
617
|
+
plans each one twice to find nondeterminism, and returns the problems as data:
|
|
618
|
+
|
|
619
|
+
```ts
|
|
620
|
+
import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
621
|
+
|
|
622
|
+
const report = yield* MachineTest.check(Counter)
|
|
623
|
+
report.ok // false when any finding is an error
|
|
624
|
+
report.exploration // "exhaustive", or "bounded" with the reasons in report.limits
|
|
625
|
+
report.findings // each one names its law, and replay events where they apply
|
|
626
|
+
```
|
|
627
|
+
|
|
628
|
+
A `"bounded"` pass means nothing was found in what was explored; it is not a
|
|
629
|
+
proof. `effect-machine check --json` from `@typeonce/effect-machine-devtools` runs
|
|
630
|
+
the same check across a project. `MachineTest.analyze` runs only the static part,
|
|
631
|
+
without calling any machine callback.
|
|
632
|
+
|
|
633
|
+
The testing entrypoint also provides complementary layers for specific tests:
|
|
610
634
|
|
|
611
635
|
- `MachineTest.run` and `verify` inspect pure planner traces;
|
|
612
636
|
- `coverage` reports exact transition-definition and conditional-branch hits;
|
|
@@ -668,20 +692,19 @@ Each ESM entrypoint is independent and tree-shakeable.
|
|
|
668
692
|
## Reference and development
|
|
669
693
|
|
|
670
694
|
- [API reference](https://effect-machine.typeonce.dev)
|
|
671
|
-
- [Agent
|
|
672
|
-
- [Contributing guide](
|
|
695
|
+
- [Agent documentation](https://github.com/typeonce-dev/effect-machine/blob/main/ai-docs/src/index.md), shipped in this package as `AGENTS.md`, `CLAUDE.md`, and `ai-docs/`
|
|
696
|
+
- [Contributing guide](https://github.com/typeonce-dev/effect-machine/blob/main/CONTRIBUTING.md)
|
|
673
697
|
|
|
674
|
-
|
|
698
|
+
Develop the repository with pnpm 10 and Node.js 22.22 or newer:
|
|
675
699
|
|
|
676
700
|
```sh
|
|
677
701
|
pnpm install --frozen-lockfile
|
|
678
702
|
pnpm check
|
|
679
703
|
```
|
|
680
704
|
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
a changeset and the performance checks described in `AGENTS.md`.
|
|
705
|
+
Pull requests that change `src/` or `package.json` need a changeset and the
|
|
706
|
+
performance checks described in the repository's
|
|
707
|
+
[`AGENTS.md`](https://github.com/typeonce-dev/effect-machine/blob/main/AGENTS.md).
|
|
685
708
|
|
|
686
709
|
When equivalent Machine modules ship in Effect, this package is intended to
|
|
687
710
|
become a compatibility re-export before eventual retirement.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# AI docs
|
|
2
|
+
|
|
3
|
+
`LLMS.md` is generated from `ai-docs/src`. Packing the core package copies it in as `AGENTS.md` and `CLAUDE.md`, next to a copy of `ai-docs`, so an installed package carries documentation matching its version. The copies are removed after packing.
|
|
4
|
+
|
|
5
|
+
## Add content
|
|
6
|
+
|
|
7
|
+
1. Add or update markdown in `ai-docs/src/**/index.md` for section text. Top-level sections start at `##`.
|
|
8
|
+
2. Add complete examples as `.ts` files in the same folder. `pnpm typecheck` compiles them.
|
|
9
|
+
3. Run `pnpm ai-docgen` to regenerate `LLMS.md`. `pnpm ai-docgen:check` fails when it is stale.
|
|
10
|
+
|
|
11
|
+
## Source file conventions
|
|
12
|
+
|
|
13
|
+
- Numeric filename prefixes control ordering. A `.ts` file starting with `0` is inlined into `LLMS.md`; any other is linked.
|
|
14
|
+
- A top JSDoc block with `@title` and an optional description sets the rendered title and description.
|
|
15
|
+
- `fixtures` directories are ignored.
|
|
16
|
+
- Link between sections with anchors, such as `[Root machine API](#root-machine-api)`, because every section ends up in one document.
|
|
17
|
+
|
|
18
|
+
Examples must represent real usage and explain the how and why in comments.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @title Creating and running a machine
|
|
3
|
+
*
|
|
4
|
+
* Declare the topology, the protocol, and the behavior, then start the result
|
|
5
|
+
* inside a Scope.
|
|
6
|
+
*/
|
|
7
|
+
import { Machine } from "@typeonce/effect-machine"
|
|
8
|
+
import { Effect, Schema } from "effect"
|
|
9
|
+
|
|
10
|
+
// The root declares the topology and the data each state owns.
|
|
11
|
+
export const CounterStates = Machine.state({
|
|
12
|
+
states: {
|
|
13
|
+
Idle: {},
|
|
14
|
+
Running: { fields: { count: Schema.Number } }
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
// Events are the public protocol. Each one also becomes a typed constructor.
|
|
19
|
+
export const CounterEvents = Machine.events({ Start: {}, Increment: {}, Stop: {} })
|
|
20
|
+
|
|
21
|
+
// `make` joins the topology and protocol. `handle` implements every state and
|
|
22
|
+
// returns the machine to export.
|
|
23
|
+
export const CounterMachine = Machine.make({
|
|
24
|
+
id: "Counter",
|
|
25
|
+
root: CounterStates,
|
|
26
|
+
events: CounterEvents
|
|
27
|
+
}).handle({
|
|
28
|
+
initial: { target: "Idle" },
|
|
29
|
+
states: {
|
|
30
|
+
Idle: {
|
|
31
|
+
on: {
|
|
32
|
+
Start: { target: "Running", data: () => ({ count: 0 }) }
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
Running: {
|
|
36
|
+
on: {
|
|
37
|
+
Increment: { update: { Running: ({ state }) => ({ count: state.count + 1 }) } },
|
|
38
|
+
Stop: { target: "Idle" }
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// `Machine.start` requires a Scope. Closing it stops the machine and everything
|
|
45
|
+
// the machine owns.
|
|
46
|
+
export const program = Effect.scoped(Effect.gen(function*() {
|
|
47
|
+
const counter = yield* Machine.start(CounterMachine)
|
|
48
|
+
|
|
49
|
+
yield* counter.send(CounterEvents.Start())
|
|
50
|
+
yield* counter.send(CounterEvents.Increment())
|
|
51
|
+
}))
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @title Choosing a destination from an event
|
|
3
|
+
*
|
|
4
|
+
* List every destination in `branches`, next to the `resolve` that selects one.
|
|
5
|
+
* The compiler checks one against the other, so this transition can be edited
|
|
6
|
+
* without reading anything else.
|
|
7
|
+
*/
|
|
8
|
+
import { Machine } from "@typeonce/effect-machine"
|
|
9
|
+
import { Schema } from "effect"
|
|
10
|
+
|
|
11
|
+
export const CheckoutStates = Machine.state({
|
|
12
|
+
fields: { attempts: Schema.Number },
|
|
13
|
+
states: {
|
|
14
|
+
Editing: {},
|
|
15
|
+
Charging: { fields: { amount: Schema.Number } },
|
|
16
|
+
Paid: {}
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
export const CheckoutEvents = Machine.events({
|
|
21
|
+
Pay: { amount: Schema.Number },
|
|
22
|
+
Paid: {}
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
export const CheckoutMachine = Machine.make({
|
|
26
|
+
root: CheckoutStates,
|
|
27
|
+
events: CheckoutEvents
|
|
28
|
+
}).handle({
|
|
29
|
+
root: { attempts: 0 },
|
|
30
|
+
initial: { target: "Editing" },
|
|
31
|
+
states: {
|
|
32
|
+
Editing: {
|
|
33
|
+
on: {
|
|
34
|
+
Pay: {
|
|
35
|
+
// `stay` accepts the event, so `Machine.can` is true. `decline` refuses it, so `Machine.can` is false and a
|
|
36
|
+
// parent handler for the same event runs.
|
|
37
|
+
branches: [{ target: "Charging" }, { target: "Paid" }, { update: "root" }, { stay: true }, { decline: true }],
|
|
38
|
+
resolve: ({ event, root, select }) => {
|
|
39
|
+
if (event.amount < 0) return select.decline()
|
|
40
|
+
if (event.amount === 0) return select.target("Paid")
|
|
41
|
+
if (root.attempts >= 3) return select.stay()
|
|
42
|
+
if (event.amount > 1_000) return select.update({ root: { attempts: root.attempts + 1 } })
|
|
43
|
+
// Each call takes the values of the path it is given.
|
|
44
|
+
return select.target("Charging", { data: { amount: event.amount } })
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
Charging: {
|
|
50
|
+
on: {
|
|
51
|
+
Paid: { target: "Paid" }
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @title Updating retained state and enqueueing commands
|
|
3
|
+
*
|
|
4
|
+
* `data` only ever means the target's value. `update` is a record naming the one
|
|
5
|
+
* retained state it replaces. `commands` enqueues work on any plain transition.
|
|
6
|
+
*/
|
|
7
|
+
import { Machine } from "@typeonce/effect-machine"
|
|
8
|
+
import { Schema } from "effect"
|
|
9
|
+
|
|
10
|
+
export const DocumentEmitted = Machine.emittedEvents({ Saved: { revision: Schema.Number } })
|
|
11
|
+
|
|
12
|
+
export const DocumentMachine = Machine.make({
|
|
13
|
+
root: Machine.state({
|
|
14
|
+
states: {
|
|
15
|
+
Document: {
|
|
16
|
+
fields: { revision: Schema.Number },
|
|
17
|
+
states: {
|
|
18
|
+
Editing: {},
|
|
19
|
+
Saving: { fields: { draft: Schema.String } }
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}),
|
|
24
|
+
events: Machine.events({ Change: {}, Save: { draft: Schema.String }, Saved: {} }),
|
|
25
|
+
emittedEvents: DocumentEmitted
|
|
26
|
+
}).handle({
|
|
27
|
+
initial: { target: "Document", data: { revision: 0 } },
|
|
28
|
+
states: {
|
|
29
|
+
Document: {
|
|
30
|
+
initial: { target: "Document.Editing" },
|
|
31
|
+
on: {
|
|
32
|
+
// Replaces the value of Document and keeps its active child and running work.
|
|
33
|
+
Change: { update: { Document: ({ state }) => ({ revision: state.revision + 1 }) } }
|
|
34
|
+
},
|
|
35
|
+
states: {
|
|
36
|
+
Editing: {
|
|
37
|
+
on: {
|
|
38
|
+
// Enters a state and replaces a retained ancestor in one validated step.
|
|
39
|
+
Save: {
|
|
40
|
+
target: "Document.Saving",
|
|
41
|
+
data: ({ event }) => ({ draft: event.draft }),
|
|
42
|
+
update: { Document: ({ ancestors }) => ({ revision: ancestors.Document.revision + 1 }) }
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
Saving: {
|
|
47
|
+
on: {
|
|
48
|
+
Saved: {
|
|
49
|
+
target: "Document.Editing",
|
|
50
|
+
commands: ({ ancestors }, enqueue) =>
|
|
51
|
+
enqueue.emit(DocumentEmitted.Saved({ revision: ancestors.Document.revision }))
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @title Verifying a machine after changing it
|
|
3
|
+
*
|
|
4
|
+
* Run `MachineTest.check` after every change. It needs no test setup, and a
|
|
5
|
+
* finding names the law it broke and the events that reproduce it.
|
|
6
|
+
*/
|
|
7
|
+
import { Machine } from "@typeonce/effect-machine"
|
|
8
|
+
import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
9
|
+
import { Effect } from "effect"
|
|
10
|
+
|
|
11
|
+
export const SwitchMachine = Machine.make({
|
|
12
|
+
root: Machine.state({ states: { Off: {}, On: {} } }),
|
|
13
|
+
events: Machine.events({ Toggle: {} })
|
|
14
|
+
}).handle({
|
|
15
|
+
initial: { target: "Off" },
|
|
16
|
+
states: {
|
|
17
|
+
Off: { on: { Toggle: { target: "On" } } },
|
|
18
|
+
On: { on: { Toggle: { target: "Off" } } }
|
|
19
|
+
}
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
export const verified = Effect.gen(function*() {
|
|
23
|
+
const report = yield* MachineTest.check(SwitchMachine)
|
|
24
|
+
if (!report.ok) {
|
|
25
|
+
return yield* Effect.die(MachineTest.formatCheckReport(report))
|
|
26
|
+
}
|
|
27
|
+
// "exhaustive" means nothing limited exploration. "bounded" lists why in `limits`, and is not a proof.
|
|
28
|
+
return report.exploration
|
|
29
|
+
})
|