@typeonce/effect-machine 0.46.0 → 0.47.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/AGENTS.md +1823 -0
  2. package/CLAUDE.md +1823 -0
  3. package/README.md +100 -77
  4. package/ai-docs/README.md +18 -0
  5. package/ai-docs/src/10_modeling/01_create-a-machine.ts +51 -0
  6. package/ai-docs/src/10_modeling/02_branch-on-an-event.ts +55 -0
  7. package/ai-docs/src/10_modeling/03_update-retained-state.ts +58 -0
  8. package/ai-docs/src/10_modeling/04_verify-after-changing.ts +29 -0
  9. package/{docs/agent-guide.md → ai-docs/src/10_modeling/index.md} +234 -155
  10. package/{docs/root-api.md → ai-docs/src/20_root-api/index.md} +29 -57
  11. package/{docs/effect-atom-react.md → ai-docs/src/30_react/index.md} +42 -49
  12. package/{docs/machine-review.md → ai-docs/src/40_review/index.md} +42 -40
  13. package/ai-docs/src/index.md +7 -0
  14. package/ai-docs/tsconfig.json +22 -0
  15. package/dist/Machine.d.ts +1195 -1474
  16. package/dist/Machine.d.ts.map +1 -1
  17. package/dist/Machine.js +366 -214
  18. package/dist/Machine.js.map +1 -1
  19. package/dist/internal/machine/atom.js.map +1 -1
  20. package/dist/internal/machine/cluster.js.map +1 -1
  21. package/dist/internal/machine/command.d.ts +8 -0
  22. package/dist/internal/machine/command.d.ts.map +1 -1
  23. package/dist/internal/machine/command.js +2 -2
  24. package/dist/internal/machine/command.js.map +1 -1
  25. package/dist/internal/machine/configuration.d.ts +21 -5
  26. package/dist/internal/machine/configuration.d.ts.map +1 -1
  27. package/dist/internal/machine/configuration.js +1 -0
  28. package/dist/internal/machine/configuration.js.map +1 -1
  29. package/dist/internal/machine/construction.js.map +1 -1
  30. package/dist/internal/machine/declaration.d.ts +2 -1
  31. package/dist/internal/machine/declaration.d.ts.map +1 -1
  32. package/dist/internal/machine/declaration.js +28 -46
  33. package/dist/internal/machine/declaration.js.map +1 -1
  34. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  35. package/dist/internal/machine/executionPlan.js +23 -16
  36. package/dist/internal/machine/executionPlan.js.map +1 -1
  37. package/dist/internal/machine/implementation.d.ts +2 -0
  38. package/dist/internal/machine/implementation.d.ts.map +1 -1
  39. package/dist/internal/machine/implementation.js.map +1 -1
  40. package/dist/internal/machine/initialDeclaration.d.ts +2 -0
  41. package/dist/internal/machine/initialDeclaration.d.ts.map +1 -1
  42. package/dist/internal/machine/initialDeclaration.js +15 -0
  43. package/dist/internal/machine/initialDeclaration.js.map +1 -1
  44. package/dist/internal/machine/invocation.d.ts.map +1 -1
  45. package/dist/internal/machine/invocation.js +25 -19
  46. package/dist/internal/machine/invocation.js.map +1 -1
  47. package/dist/internal/machine/invocationEvent.d.ts +34 -17
  48. package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
  49. package/dist/internal/machine/invocationEvent.js +16 -4
  50. package/dist/internal/machine/invocationEvent.js.map +1 -1
  51. package/dist/internal/machine/machine.d.ts +5 -22
  52. package/dist/internal/machine/machine.d.ts.map +1 -1
  53. package/dist/internal/machine/machine.js +157 -119
  54. package/dist/internal/machine/machine.js.map +1 -1
  55. package/dist/internal/machine/planner.d.ts +15 -4
  56. package/dist/internal/machine/planner.d.ts.map +1 -1
  57. package/dist/internal/machine/planner.js +18 -21
  58. package/dist/internal/machine/planner.js.map +1 -1
  59. package/dist/internal/machine/process.d.ts +2 -0
  60. package/dist/internal/machine/process.d.ts.map +1 -1
  61. package/dist/internal/machine/process.js +2 -0
  62. package/dist/internal/machine/process.js.map +1 -1
  63. package/dist/internal/machine/protocol.js.map +1 -1
  64. package/dist/internal/machine/runtime.d.ts.map +1 -1
  65. package/dist/internal/machine/runtime.js.map +1 -1
  66. package/dist/internal/machine/runtimeCompiled.js.map +1 -1
  67. package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -1
  68. package/dist/internal/machine/runtimeGeneric.js.map +1 -1
  69. package/dist/internal/machine/runtimeProtocol.d.ts +26 -0
  70. package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
  71. package/dist/internal/machine/runtimeProtocol.js +71 -0
  72. package/dist/internal/machine/runtimeProtocol.js.map +1 -1
  73. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  74. package/dist/internal/machine/stateDefinition.js +0 -14
  75. package/dist/internal/machine/stateDefinition.js.map +1 -1
  76. package/dist/internal/machine/symbols.d.ts +9 -0
  77. package/dist/internal/machine/symbols.d.ts.map +1 -1
  78. package/dist/internal/machine/symbols.js.map +1 -1
  79. package/dist/internal/machine/topology.d.ts +18 -9
  80. package/dist/internal/machine/topology.d.ts.map +1 -1
  81. package/dist/internal/machine/topology.js +16 -1
  82. package/dist/internal/machine/topology.js.map +1 -1
  83. package/dist/internal/testing/machine/arbitrary.d.ts.map +1 -1
  84. package/dist/internal/testing/machine/arbitrary.js +2 -0
  85. package/dist/internal/testing/machine/arbitrary.js.map +1 -1
  86. package/dist/internal/testing/machine/check.d.ts +17 -0
  87. package/dist/internal/testing/machine/check.d.ts.map +1 -0
  88. package/dist/internal/testing/machine/check.js +371 -0
  89. package/dist/internal/testing/machine/check.js.map +1 -0
  90. package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
  91. package/dist/internal/testing/machine/exploration.js.map +1 -1
  92. package/dist/internal/testing/machine/finiteModel.d.ts +14 -13
  93. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  94. package/dist/internal/testing/machine/finiteModel.js +21 -23
  95. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  96. package/dist/internal/testing/machine/runtime.d.ts +19 -88
  97. package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
  98. package/dist/internal/testing/machine/runtime.js +0 -19
  99. package/dist/internal/testing/machine/runtime.js.map +1 -1
  100. package/dist/internal/testing/machine/runtimeInvariant.js.map +1 -1
  101. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  102. package/dist/internal/testing/machine/verification.js.map +1 -1
  103. package/dist/testing/MachineTest.d.ts +349 -139
  104. package/dist/testing/MachineTest.d.ts.map +1 -1
  105. package/dist/testing/MachineTest.js +201 -136
  106. package/dist/testing/MachineTest.js.map +1 -1
  107. package/dist/unstable/cluster/ClusterMachine.d.ts +9 -14
  108. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  109. package/dist/unstable/cluster/ClusterMachine.js +9 -14
  110. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  111. package/dist/unstable/reactivity/AtomMachine.d.ts +169 -80
  112. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  113. package/dist/unstable/reactivity/AtomMachine.js +165 -76
  114. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  115. package/package.json +11 -9
  116. package/src/Machine.ts +1141 -2130
  117. package/src/internal/machine/atom.ts +3 -3
  118. package/src/internal/machine/cluster.ts +2 -2
  119. package/src/internal/machine/command.ts +10 -4
  120. package/src/internal/machine/configuration.ts +16 -17
  121. package/src/internal/machine/construction.ts +1 -1
  122. package/src/internal/machine/declaration.ts +25 -50
  123. package/src/internal/machine/executionPlan.ts +90 -56
  124. package/src/internal/machine/implementation.ts +3 -1
  125. package/src/internal/machine/initialDeclaration.ts +16 -0
  126. package/src/internal/machine/invocation.ts +38 -44
  127. package/src/internal/machine/invocationEvent.ts +46 -20
  128. package/src/internal/machine/machine.ts +217 -243
  129. package/src/internal/machine/planner.ts +72 -106
  130. package/src/internal/machine/process.ts +12 -0
  131. package/src/internal/machine/protocol.ts +6 -6
  132. package/src/internal/machine/runtime.ts +1 -5
  133. package/src/internal/machine/runtimeCompiled.ts +2 -2
  134. package/src/internal/machine/runtimeGeneric.ts +2 -4
  135. package/src/internal/machine/runtimeProtocol.ts +80 -4
  136. package/src/internal/machine/stateDefinition.ts +0 -39
  137. package/src/internal/machine/symbols.ts +11 -0
  138. package/src/internal/machine/topology.ts +40 -18
  139. package/src/internal/testing/machine/arbitrary.ts +2 -0
  140. package/src/internal/testing/machine/check.ts +460 -0
  141. package/src/internal/testing/machine/exploration.ts +4 -5
  142. package/src/internal/testing/machine/finiteModel.ts +40 -42
  143. package/src/internal/testing/machine/runtime.ts +31 -170
  144. package/src/internal/testing/machine/runtimeInvariant.ts +3 -3
  145. package/src/internal/testing/machine/verification.ts +14 -14
  146. package/src/testing/MachineTest.ts +366 -145
  147. package/src/unstable/cluster/ClusterMachine.ts +9 -14
  148. package/src/unstable/reactivity/AtomMachine.ts +167 -78
@@ -1,14 +1,13 @@
1
- # Effect Machine agent guide
1
+ ## Effect Machine agent guide
2
2
 
3
3
  Use this guide to model a statechart with `@typeonce/effect-machine`. It covers
4
4
  the decisions that shape the machine. Use the API reference for method
5
5
  signatures, history states, and choice states.
6
6
 
7
- Read [Effect Atom and React patterns](./effect-atom-react.md) when React needs
8
- to consume a machine. Keep React ownership and atom lookup out of the machine
9
- model.
7
+ Read [Effect Atom and React](#effect-atom-and-react) when React needs to consume
8
+ a machine. Keep React ownership and atom lookup out of the machine model.
10
9
 
11
- ## Create a machine
10
+ ### Create a machine
12
11
 
13
12
  Define schemas first, then states, events, the machine definition, and its
14
13
  handlers. Export the state descriptor, public event descriptor, and implemented
@@ -22,10 +21,12 @@ const CounterState = Schema.TaggedUnion({
22
21
  Running: { count: Schema.Number }
23
22
  })
24
23
 
25
- export const CounterStates = Machine.state({ states: {
26
- Idle: {},
27
- Running: CounterState.cases.Running
28
- } })
24
+ export const CounterStates = Machine.state({
25
+ states: {
26
+ Idle: {},
27
+ Running: CounterState.cases.Running
28
+ }
29
+ })
29
30
 
30
31
  export const CounterEvents = Machine.eventsFromSchemas(
31
32
  Schema.TaggedUnion({
@@ -38,20 +39,23 @@ export const CounterEvents = Machine.eventsFromSchemas(
38
39
  export const CounterMachine = Machine.make({
39
40
  id: "Counter",
40
41
  root: CounterStates,
41
- events: CounterEvents,
42
- }).handle({ initial: { target: "Idle" }, states: {
43
- Idle: {
44
- on: {
45
- Start: { target: "Running", data: () => ({ count: 0 }) }
46
- }
47
- },
48
- Running: {
49
- on: {
50
- Increment: { update: "Running", data: ({ state }) => ({ count: state.count + 1 }) },
51
- Stop: { target: "Idle" }
42
+ events: CounterEvents
43
+ }).handle({
44
+ initial: { target: "Idle" },
45
+ states: {
46
+ Idle: {
47
+ on: {
48
+ Start: { target: "Running", data: () => ({ count: 0 }) }
49
+ }
50
+ },
51
+ Running: {
52
+ on: {
53
+ Increment: { update: { Running: ({ state }) => ({ count: state.count + 1 }) } },
54
+ Stop: { target: "Idle" }
55
+ }
52
56
  }
53
57
  }
54
- } })
58
+ })
55
59
  ```
56
60
 
57
61
  Each step has one job:
@@ -68,8 +72,8 @@ when the module exports one machine implementation.
68
72
 
69
73
  Use `data` for schema make input. Constructor defaults and validation run while
70
74
  planning. Use `{ decoded: true, data }` for an existing schema Type; validation
71
- still applies. Named selectors take the same object format, with nested children
72
- under `states`. Structural states omit data. See [Root API](./root-api.md) for
75
+ still applies. `select.target` takes the same object format, with nested
76
+ children under `states`. Structural states omit data. See [Root machine API](#root-machine-api) for
73
77
  startup input, root initialization, subtree construction, and history fallbacks.
74
78
 
75
79
  The examples below show one modeling decision at a time. They omit unchanged
@@ -92,7 +96,7 @@ const program = Effect.scoped(Effect.gen(function*() {
92
96
  `Machine.start` requires a `Scope`. Closing it stops the machine and everything
93
97
  it owns; use `ref.stop` to stop earlier.
94
98
 
95
- ## Make impossible states unrepresentable
99
+ ### Make impossible states unrepresentable
96
100
 
97
101
  A finite state describes how the machine behaves now. State data holds values
98
102
  needed while that mode is active.
@@ -107,12 +111,14 @@ const RequestState = Schema.TaggedUnion({
107
111
  Failed: { message: Schema.String }
108
112
  })
109
113
 
110
- const RequestStates = Machine.state({ states: {
111
- Idle: {},
112
- Loading: {},
113
- Ready: RequestState.cases.Ready,
114
- Failed: RequestState.cases.Failed
115
- } })
114
+ const RequestStates = Machine.state({
115
+ states: {
116
+ Idle: {},
117
+ Loading: {},
118
+ Ready: RequestState.cases.Ready,
119
+ Failed: RequestState.cases.Failed
120
+ }
121
+ })
116
122
  ```
117
123
 
118
124
  The machine can now be `Loading`, `Ready`, or `Failed`. It cannot construct a
@@ -123,7 +129,7 @@ which events the machine should handle, which work runs, or how the machine
123
129
  behaves, model it as a state. Otherwise, keep it as data on the state that owns
124
130
  it.
125
131
 
126
- ## Put data on the lowest state that owns it
132
+ ### Put data on the lowest state that owns it
127
133
 
128
134
  State data should exist only while it is valid. Put it on the lowest node whose
129
135
  active subtree needs it. If several sibling states need the same data, their
@@ -140,26 +146,28 @@ const DocumentState = Schema.TaggedUnion({
140
146
  }
141
147
  })
142
148
 
143
- const DocumentStates = Machine.state({ states: {
144
- Closed: {},
145
- Open: {
146
- // Editing, Saving, and SaveFailed all need the document and draft.
147
- schema: DocumentState.cases.Open,
148
- states: {
149
- Editing: {},
150
- Saving: {},
151
- // Only this state owns an error message.
152
- SaveFailed: DocumentState.cases.SaveFailed
149
+ const DocumentStates = Machine.state({
150
+ states: {
151
+ Closed: {},
152
+ Open: {
153
+ // Editing, Saving, and SaveFailed all need the document and draft.
154
+ schema: DocumentState.cases.Open,
155
+ states: {
156
+ Editing: {},
157
+ Saving: {},
158
+ // Only this state owns an error message.
159
+ SaveFailed: DocumentState.cases.SaveFailed
160
+ }
153
161
  }
154
162
  }
155
- } })
163
+ })
156
164
  ```
157
165
 
158
166
  Do not copy `documentId` and `draft` into every child. Copies can disagree after
159
167
  a transition. Do not move `message` to `Open` either. That would allow an error
160
168
  message while `Editing` or `Saving` is active.
161
169
 
162
- ## Put shared behavior on the lowest common ancestor
170
+ ### Put shared behavior on the lowest common ancestor
163
171
 
164
172
  Hierarchy owns behavior as well as data. Define a transition on the lowest
165
173
  compound state whose children share it.
@@ -174,28 +182,31 @@ const DocumentEvents = Machine.eventsFromSchemas(
174
182
  const DocumentMachine = Machine.make({
175
183
  root: DocumentStates,
176
184
  events: DocumentEvents
177
- }).handle({ initial: { target: "Closed" }, states: {
178
- Closed: {},
179
- Open: {
180
- initial: { target: "Open.Editing" },
181
- on: {
182
- // All Open children close the document in the same way.
183
- Close: { target: "Closed" }
184
- },
185
- states: {
186
- Editing: {},
187
- Saving: {},
188
- SaveFailed: {}
185
+ }).handle({
186
+ initial: { target: "Closed" },
187
+ states: {
188
+ Closed: {},
189
+ Open: {
190
+ initial: { target: "Open.Editing" },
191
+ on: {
192
+ // All Open children close the document in the same way.
193
+ Close: { target: "Closed" }
194
+ },
195
+ states: {
196
+ Editing: {},
197
+ Saving: {},
198
+ SaveFailed: {}
199
+ }
189
200
  }
190
201
  }
191
- } })
202
+ })
192
203
  ```
193
204
 
194
205
  The machine checks the deepest active state first, then its ancestors. Put a
195
206
  handler on a child when that state needs different behavior. Keep the shared
196
207
  case on the parent instead of repeating it in every child.
197
208
 
198
- ## Treat events as the domain protocol
209
+ ### Treat events as the domain protocol
199
210
 
200
211
  An event tells the machine what was requested or what happened. Name events
201
212
  after domain actions and outcomes. Do not expose state setters such as
@@ -212,20 +223,22 @@ export const CheckoutEvents = Machine.eventsFromSchemas(
212
223
  const CheckoutMachine = Machine.make({
213
224
  root: CheckoutStates,
214
225
  events: CheckoutEvents
215
- }).handle({ states: {
216
- Editing: {
217
- on: {
218
- Submit: { target: "Submitting" }
219
- }
220
- },
221
- Submitting: {
222
- on: {
223
- // Cancel has meaning while work is in progress.
224
- Cancel: { target: "Editing" }
225
- }
226
- },
227
- Complete: {}
228
- } })
226
+ }).handle({
227
+ states: {
228
+ Editing: {
229
+ on: {
230
+ Submit: { target: "Submitting" }
231
+ }
232
+ },
233
+ Submitting: {
234
+ on: {
235
+ // Cancel has meaning while work is in progress.
236
+ Cancel: { target: "Editing" }
237
+ }
238
+ },
239
+ Complete: {}
240
+ }
241
+ })
229
242
  ```
230
243
 
231
244
  The sender requests `Submit`. The machine decides whether `Submit` has a
@@ -235,40 +248,45 @@ Carry facts that the machine cannot read from its current snapshot in the event
235
248
  payload. Do not copy current state into an event to help a handler reconstruct
236
249
  what the machine already knows.
237
250
 
238
- ## Use parallel states only for independent modes
251
+ ### Use parallel states only for independent modes
239
252
 
240
253
  A compound state activates one direct child. A parallel state activates one
241
254
  child in every region. A parallel model therefore accepts the full product of
242
255
  those regions.
243
256
 
244
257
  ```ts
245
- const ScreenStates = Machine.state({ states: {
246
- Screen: {
247
- type: "parallel",
248
- states: {
249
- connection: {
250
- states: {
251
- Online: {},
252
- Offline: {}
253
- }
254
- },
255
- panel: {
256
- states: {
257
- Closed: {},
258
- Open: {}
258
+ const ScreenStates = Machine.state({
259
+ states: {
260
+ Screen: {
261
+ type: "parallel",
262
+ states: {
263
+ connection: {
264
+ states: {
265
+ Online: {},
266
+ Offline: {}
267
+ }
268
+ },
269
+ panel: {
270
+ states: {
271
+ Closed: {},
272
+ Open: {}
273
+ }
259
274
  }
260
275
  }
261
276
  }
262
277
  }
263
- } })
278
+ })
264
279
  const ScreenMachine = Machine.make({ root: ScreenStates, events: Machine.events({}) }).handle({
265
280
  initial: { target: "Screen" },
266
- states: { Screen: { states: {
267
- connection: { initial: { target: "Screen.connection.Online" } },
268
- panel: { initial: { target: "Screen.panel.Closed" } }
269
- } } }
281
+ states: {
282
+ Screen: {
283
+ states: {
284
+ connection: { initial: { target: "Screen.connection.Online" } },
285
+ panel: { initial: { target: "Screen.panel.Closed" } }
286
+ }
287
+ }
288
+ }
270
289
  })
271
-
272
290
  ```
273
291
 
274
292
  This model permits all four combinations: online with a closed panel, online
@@ -278,7 +296,7 @@ If one combination would break a domain rule, do not repair it with a UI check
278
296
  or repeated cross-region conditions. Change the topology. A compound hierarchy
279
297
  can place a mode only under the parent where it is valid.
280
298
 
281
- ## Let states own running work
299
+ ### Let states own running work
282
300
 
283
301
  Put asynchronous work on the state whose meaning requires that work. The
284
302
  machine starts the work when it enters the state and interrupts it when it exits.
@@ -291,30 +309,35 @@ const LoadState = Schema.TaggedUnion({
291
309
  Failed: { message: Schema.String }
292
310
  })
293
311
 
294
- const LoadStates = Machine.state({ states: {
295
- Idle: {},
296
- Loading: LoadState.cases.Loading,
297
- Ready: LoadState.cases.Ready,
298
- Failed: LoadState.cases.Failed
299
- } })
312
+ const LoadStates = Machine.state({
313
+ states: {
314
+ Idle: {},
315
+ Loading: LoadState.cases.Loading,
316
+ Ready: LoadState.cases.Ready,
317
+ Failed: LoadState.cases.Failed
318
+ }
319
+ })
300
320
 
301
321
  const LoadMachine = Machine.make({
302
322
  root: LoadStates,
303
323
  effects: { loadDocument },
304
- events: Machine.eventsFromSchemas(),
305
- }).handle({ initial: { target: "Idle" }, states: {
306
- Idle: {},
307
- Loading: {
308
- invoke: {
309
- src: "loadDocument",
310
- input: ({ state }) => state.documentId,
311
- onDone: { target: "Ready", data: ({ output }) => ({ content: output }) },
312
- onFailure: { target: "Failed", data: ({ error }) => ({ message: String(error) }) }
313
- }
314
- },
315
- Ready: {},
316
- Failed: {}
317
- } })
324
+ events: Machine.eventsFromSchemas()
325
+ }).handle({
326
+ initial: { target: "Idle" },
327
+ states: {
328
+ Idle: {},
329
+ Loading: {
330
+ invoke: {
331
+ src: "loadDocument",
332
+ input: ({ state }) => state.documentId,
333
+ onDone: { target: "Ready", data: ({ output }) => ({ content: output }) },
334
+ onFailure: { target: "Failed", data: ({ error }) => ({ message: String(error) }) }
335
+ }
336
+ },
337
+ Ready: {},
338
+ Failed: {}
339
+ }
340
+ })
318
341
  ```
319
342
 
320
343
  `loadDocument` may require Effect services. Those requirements remain on the
@@ -325,7 +348,7 @@ Model a per-instance capability, such as a router or a callback owned by a UI
325
348
  component, as such a service. Do not put functions in state data or startup
326
349
  input: snapshots must remain encodable. A React owner provides the capability
327
350
  through its own runtime, as described in
328
- [Per-owner capabilities](./effect-atom-react.md#per-owner-capabilities).
351
+ [Per-owner capabilities](#per-owner-capabilities).
329
352
 
330
353
  Do not start a promise inside a transition callback. A transition has no
331
354
  lifetime in which to own that work. A state does.
@@ -375,15 +398,14 @@ the sequence with separate states and transition from the first outcome. Do
375
398
  not depend on one child completing quickly enough for another declaration to
376
399
  reuse its identity.
377
400
 
378
- ### Choose state-owned or process-owned children
401
+ #### Choose state-owned or process-owned children
379
402
 
380
403
  Register a descriptor in `make({ children: { worker: Worker } })` and use
381
404
  `invoke: { src: "worker", ...outcomes }` when a child belongs to one state and
382
405
  must stop when that state exits. Use a child family and `children.spawn(...)`
383
406
  inside a registered Effect when runtime events determine ids or cardinality
384
- and the children must survive owner state changes. See the
385
- [dynamic children example](../README.md#dynamic-children) for the complete
386
- input mapping and service requirements.
407
+ and the children must survive owner state changes. The Effect receives the
408
+ `children` capability through its invocation `input` mapper.
387
409
 
388
410
  The Effect owns the startup attempt. The machine process owns every child that
389
411
  starts successfully. Leaving `Commissioning` does not stop those children.
@@ -392,7 +414,7 @@ Stop one with `children.stop(Worker(id))` inside an Effect or
392
414
  instead of replacing the existing child, and a partially successful group is
393
415
  not rolled back automatically.
394
416
 
395
- ## Keep transition decisions synchronous
417
+ ### Keep transition decisions synchronous
396
418
 
397
419
  A transition should choose the next state from the current snapshot and event.
398
420
  Use ordinary TypeScript conditions when one event has several valid outcomes.
@@ -406,27 +428,25 @@ const ReviewEvents = Machine.eventsFromSchemas(
406
428
 
407
429
  const ReviewMachine = Machine.make({
408
430
  root: ReviewStates,
409
- events: ReviewEvents,
410
- branches: {
411
- evaluate: {
412
- accepted: { target: "Accepted" },
413
- rejected: { target: "Rejected" }
414
- }
415
- }
416
- }).handle({ initial: { target: "Pending" }, states: {
417
- Pending: {
418
- on: {
419
- Evaluate: {
420
- branches: "evaluate",
421
- resolve: ({ event, select }) => event.score >= 80
422
- ? select.accepted()
423
- : select.rejected()
431
+ events: ReviewEvents
432
+ }).handle({
433
+ initial: { target: "Pending" },
434
+ states: {
435
+ Pending: {
436
+ on: {
437
+ Evaluate: {
438
+ branches: [{ target: "Accepted" }, { target: "Rejected" }],
439
+ resolve: ({ event, select }) =>
440
+ event.score >= 80
441
+ ? select.target("Accepted")
442
+ : select.target("Rejected")
443
+ }
424
444
  }
425
- }
426
- },
427
- Accepted: {},
428
- Rejected: {}
429
- } })
445
+ },
446
+ Accepted: {},
447
+ Rejected: {}
448
+ }
449
+ })
430
450
  ```
431
451
 
432
452
  Given the same snapshot and event, the handler should choose the same result.
@@ -434,31 +454,53 @@ Do not read the clock, generate randomness, call a service, or await work while
434
454
  choosing a transition. Receive such values in an event or produce them through
435
455
  state-owned work first.
436
456
 
437
- A `{ none: true }` branch accepts its event without changing state, so
438
- `Machine.can` reports `true` when a resolver selects it. When the event must be
439
- refused, for example to disable a control, use `guard`, or declare
440
- `declinable: true` and return `decline()` from the resolver.
457
+ `resolve` is checked against `branches` in both directions, and each `select`
458
+ call takes the values of the path it is given. An agent can edit this one
459
+ transition without reading anything else: a missing entry, an entry that is never
460
+ selected, and a path that returns nothing are all compile errors that name the fix.
461
+
462
+ A `{ stay: true }` accepts its event without changing state, so `Machine.can`
463
+ reports `true` when a resolver selects it. When the event must be refused, for
464
+ example to disable a control, use `guard`, or list `{ decline: true }` and return
465
+ `select.decline()` from the resolver. `guard` and `decline` exist only on `on`
466
+ handlers and `always`: an invocation outcome, a completion, and a choice have no
467
+ other candidate, so they use `{ stay: true }` or select a destination. A declined
468
+ invocation outcome would strand the machine in its state.
469
+
470
+ A `{ stay: true }` in one parallel region accepts the event, so it blocks a handler
471
+ for that event on a parallel ancestor. `{ decline: true }` lets that handler run.
472
+
473
+ Enqueue work with `commands` on any plain transition, and with `enqueue` in
474
+ `resolve`:
475
+
476
+ ```ts
477
+ Cancelled: {
478
+ target: "Archived",
479
+ commands: (_, enqueue) => enqueue.emit(Emitted.Cancelled())
480
+ }
481
+ ```
441
482
 
442
483
  When only an active state's value changes, use `update` to preserve its active
443
484
  descendants and running work:
444
485
 
445
486
  ```ts
446
487
  Changed: {
447
- update: "Document",
448
- data: ({ ancestors }) => ({
449
- ...ancestors.Document,
450
- revision: ancestors.Document.revision + 1
451
- })
488
+ update: {
489
+ Document: ({ ancestors }) => ({
490
+ ...ancestors.Document,
491
+ revision: ancestors.Document.revision + 1
492
+ })
493
+ }
452
494
  }
453
495
  ```
454
496
 
455
497
  Choose a valued source or retained ancestor explicitly. To change a parallel
456
498
  sibling, send an event handled by that sibling. Combine a destination and one
457
- retained owner update with `{ target, update, data }`; the callback returns
458
- `{ target: destinationInput, update: completeOwnerInput }`. Both values are
459
- validated atomically, and destination entry sees the new owner value. A named
460
- branch can declare the same pair when a resolver needs commands or nested
461
- construction. Its selector requires `update: { data: ownerValue }` in the construction object.
499
+ retained owner update with `{ target, data, update: { Owner: ... } }`, where `data`
500
+ only ever means the target's value. Both values are validated atomically, and
501
+ destination entry sees the new owner value. A `branches` entry names the same pair
502
+ as `{ target, update: "Owner" }`, and its call passes the value:
503
+ `select.target(target, { data, update: { Owner: value } })`.
462
504
 
463
505
  The destination of `{ target, update, data }` can be a choice. The owner is
464
506
  updated before the choice resolves, so its resolver reads the new value through
@@ -467,7 +509,44 @@ owner input. Every branch the choice can reach, including through nested
467
509
  choices, must stay below the owner; otherwise the definition does not compile
468
510
  and `handle` throws while naming the choice, branch, and escaping target.
469
511
 
470
- ## Test paths and invariants
512
+ ### Verify a machine after changing it
513
+
514
+ Run this after writing or changing a machine, before writing a specific test:
515
+
516
+ ```ts
517
+ import { MachineTest } from "@typeonce/effect-machine/testing"
518
+ import { Effect } from "effect"
519
+
520
+ const verifyCounter = Effect.gen(function*() {
521
+ const report = yield* MachineTest.check(CounterMachine)
522
+ if (!report.ok) yield* Effect.die(MachineTest.formatCheckReport(report))
523
+ })
524
+ ```
525
+
526
+ From a shell, `effect-machine check src/counter.ts --json` from
527
+ `@typeonce/effect-machine-devtools` does the same for every machine a file exports.
528
+
529
+ To fix a finding, keep the failing evidence first. Replay its events to reproduce
530
+ it, make the smallest behavior change, then run the check again. Do not silence a
531
+ finding by weakening a type or a test.
532
+
533
+ | Law | Meaning | Fix |
534
+ | ---------------------------- | --------------------------------------------------------------- | ------------------------------------------------------------------- |
535
+ | `topology.noPathFromInitial` | No declared transition can enter the state. | Add the missing transition, or remove the state. |
536
+ | `events.noHandler` | No state handles a public event. | Handle it, or remove it from the protocol. |
537
+ | `planning.deterministic` | The same snapshot and event planned two different results. | Move the clock, randomness, or external read into an event or work. |
538
+ | `planning.failed` | A planning callback threw or a value failed its schema. | Replay the events, then fix the callback or the value. |
539
+ | `planning.verification` | A planner law failed. This usually means an Effect Machine bug. | Report it with the replay. |
540
+ | `invariant.violated` | An invariant you supplied failed. | Replay the events, then fix the behavior or the invariant. |
541
+ | `transition.notCovered` | Exploration never took a declared branch. This is a warning. | Cover it with a scenario, or remove it if it cannot happen. |
542
+ | `schema.underivable` | Input or events could not be sampled from their schemas. | Pass `input` or `events` explicitly. |
543
+
544
+ A report is `"exhaustive"` only when `limits` is empty. Otherwise a pass means
545
+ nothing was found in what was explored. Keep sampled checks, bounded exploration,
546
+ and proof distinct when reporting a result. Invocations do not run during planning,
547
+ so test them on a started machine with `MachineTest.probe`.
548
+
549
+ ### Test paths and invariants
471
550
 
472
551
  Test the statechart as a graph. Send domain events, inspect reached states, and
473
552
  state the rules that every trace must preserve. Do not duplicate the handler's
@@ -509,7 +588,7 @@ Use pure planner traces for state and transition rules. Start a live machine
509
588
  and use `MachineTest.probe` when a test depends on timers, invoked work, raised
510
589
  events, or runtime scheduling.
511
590
 
512
- ## Choose observation deliberately
591
+ ### Choose observation deliberately
513
592
 
514
593
  Use atom selectors to render state, invocation outcomes to express workflow
515
594
  behavior, `ref.join` to await final output, and `ref.changes` for ongoing