@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.
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 +1139 -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
@@ -10,6 +10,7 @@ import type * as Graph from "effect/Graph"
10
10
  import type * as Schema from "effect/Schema"
11
11
  import type { EnsureExecutable } from "../internal/machine/readiness.js"
12
12
  import type { SchemaArbitraryReport } from "../internal/testing/machine/arbitrary.js"
13
+ import * as CheckImpl from "../internal/testing/machine/check.js"
13
14
  import * as ExplorationImpl from "../internal/testing/machine/exploration.js"
14
15
  import type { FiniteModel } from "../internal/testing/machine/finiteModel.js"
15
16
  import * as Format from "../internal/testing/machine/format.js"
@@ -27,6 +28,7 @@ export {
27
28
  advanceCommand,
28
29
  type CausalRuntimeAssertionContext,
29
30
  type CausalRuntimeCommandActual,
31
+ type CausalRuntimeCommandAttempt,
30
32
  CausalRuntimeCommandFailure,
31
33
  type CausalRuntimeCommandRecord,
32
34
  type CausalRuntimeCommandResult,
@@ -47,27 +49,18 @@ export {
47
49
  type EnqueuedRuntimeTranscript,
48
50
  formatCausalTranscript,
49
51
  formatEnqueuedTranscript,
50
- formatRuntimeTranscript,
51
52
  runCausalCommands,
52
53
  runEnqueuedCommands,
53
- runRuntimeCommands,
54
- type RuntimeAssertionContext,
55
54
  type RuntimeAwait,
56
55
  type RuntimeCommand,
57
- type RuntimeCommandActual,
58
56
  RuntimeCommandFailure,
59
- type RuntimeCommandRecord,
60
57
  type RuntimeCommandResult,
61
58
  type RuntimeCommands,
62
59
  runtimeCommands,
63
60
  type RuntimeCommandsDiagnostics,
64
61
  type RuntimeCommandsOptions,
65
- type RuntimeInspectionContext,
66
- type RuntimeModelOptions,
67
- type RuntimeModelStep,
68
62
  RuntimeObservationError,
69
63
  RuntimeSynchronization,
70
- type RuntimeTranscript,
71
64
  sendCommand,
72
65
  stopCommand,
73
66
  verifyCausalCommands
@@ -83,6 +76,7 @@ export {
83
76
  compileModel,
84
77
  type FiniteAtomicState,
85
78
  type FiniteAutomaticTransition,
79
+ type FiniteChoiceState,
86
80
  type FiniteCompoundState,
87
81
  type FiniteEventTransition,
88
82
  type FiniteFinalState,
@@ -229,33 +223,31 @@ export interface Scenarios<M extends AnyMachine> {
229
223
  * Unsupported schema derivations fail immediately through `Arbitrary.schema`.
230
224
  * Non-fatal derivation warnings are returned instead of being hidden.
231
225
  *
232
- * **Example**
226
+ * **Example** (Generating event scenarios)
233
227
  *
234
- * ```ts
228
+ * ```ts import.meta.vitest
235
229
  * import { Machine } from "@typeonce/effect-machine"
236
230
  * import { MachineTest } from "@typeonce/effect-machine/testing"
237
- * import { Schema } from "effect"
238
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
239
- * }
240
- * class Reset extends Schema.TaggedClass<Reset>("Reset")("Reset", {}) {
241
- * }
242
- * const States = Machine.state({ states: { Idle } })
231
+ * import { Effect } from "effect"
232
+ * import * as Arbitrary from "effect/Arbitrary"
233
+ *
234
+ * const Events = Machine.events({ Reset: {} })
243
235
  * const machine = Machine.make({
244
- * root: States,
245
- * events: Machine.eventsFromSchemas(Reset)
236
+ * root: Machine.state({ states: { Idle: {} } }),
237
+ * events: Events
246
238
  * }).handle({
247
- * initial: {
248
- * target: "Idle"
249
- * },
239
+ * initial: { target: "Idle" },
250
240
  * states: {
251
- * Idle: {
252
- * on: {
253
- * Reset: (to) => to.branch.Idle().resolve(({ target }) => target.from())
254
- * }
255
- * }
241
+ * Idle: { on: { Reset: { target: "Idle" } } }
256
242
  * }
257
243
  * })
244
+ *
258
245
  * const generated = MachineTest.scenarios(machine, { maxEvents: 5 })
246
+ * const samples = Arbitrary.sampleEffect(generated.arbitrary, { count: 1, seed: 1 })
247
+ *
248
+ * const count = Effect.map(samples, (scenarios) => scenarios.length)
249
+ *
250
+ * await Effect.runPromise(count) // => 1
259
251
  * ```
260
252
  *
261
253
  * @category constructors
@@ -491,30 +483,25 @@ export { ProbeUnavailableError } from "../internal/testing/machine/probe.js"
491
483
  * available exclusively through `MachineRef.send` and retain their
492
484
  * asynchronous enqueue-only semantics.
493
485
  *
494
- * **Example**
486
+ * **Example** (Acknowledging commands at runtime)
495
487
  *
496
- * ```ts
488
+ * ```ts import.meta.vitest
497
489
  * import { Machine } from "@typeonce/effect-machine"
498
490
  * import { MachineTest } from "@typeonce/effect-machine/testing"
499
- * import { Effect, Schema } from "effect"
500
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
501
- * }
502
- * const States = Machine.state({ states: { Idle } })
503
- * const machine = Machine.make({
504
- * root: States,
505
- * events: Machine.eventsFromSchemas()
506
- * }).handle({
507
- * initial: {
508
- * target: "Idle"
509
- * },
510
- * states: {
511
- * Idle: {}
512
- * }
491
+ * import { Effect } from "effect"
492
+ *
493
+ * const States = Machine.state({ states: { Idle: {} } })
494
+ * const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
495
+ * initial: { target: "Idle" }
513
496
  * })
514
- * const program = Effect.scoped(Effect.gen(function*() {
497
+ *
498
+ * const probed = Effect.scoped(Effect.gen(function*() {
515
499
  * const ref = yield* Machine.start(machine)
516
- * return yield* MachineTest.probe(machine, ref)
500
+ * const probe = yield* MachineTest.probe(machine, ref)
501
+ * return typeof probe
517
502
  * }))
503
+ *
504
+ * await Effect.runPromise(probed) // => "object"
518
505
  * ```
519
506
  *
520
507
  * @category runtime testing
@@ -1105,34 +1092,26 @@ export const Invariant: {
1105
1092
  * types. The machine is used only for inference; invariant evaluation remains
1106
1093
  * pure and reusable across traces from that machine.
1107
1094
  *
1108
- * **Example**
1095
+ * **Example** (Declaring a state invariant)
1109
1096
  *
1110
- * ```ts
1097
+ * ```ts import.meta.vitest
1111
1098
  * import { Machine } from "@typeonce/effect-machine"
1112
1099
  * import { MachineTest } from "@typeonce/effect-machine/testing"
1113
- * import { Schema } from "effect"
1114
- * class Count extends Schema.TaggedClass<Count>("Count")("Count", {
1115
- * value: Schema.Number
1116
- * }) {
1117
- * }
1118
- * const States = Machine.state({ states: { Count } })
1119
- * const machine = Machine.make({
1120
- * root: States,
1121
- * events: Machine.eventsFromSchemas()
1122
- * }).handle({
1123
- * initial: {
1124
- * target: "Count",
1125
- * decoded: true,
1126
- * data: new Count({ value: 0 })
1127
- * },
1128
- * states: {
1129
- * Count: {}
1130
- * }
1100
+ * import { Effect, Option, Schema } from "effect"
1101
+ *
1102
+ * const States = Machine.state({ states: { Count: { fields: { value: Schema.Number } } } })
1103
+ * const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
1104
+ * initial: { target: "Count", data: { value: 0 } }
1131
1105
  * })
1106
+ *
1132
1107
  * const nonNegative = MachineTest.invariants(machine).state(
1133
1108
  * "count is non-negative",
1134
- * ({ snapshot }) => snapshot.value.value >= 0
1109
+ * ({ snapshot }) => States.get(snapshot, "Count").pipe(Option.exists(({ value }) => value >= 0))
1135
1110
  * )
1111
+ *
1112
+ * const ok = Effect.map(MachineTest.check(machine, { invariants: [nonNegative] }), (report) => report.ok)
1113
+ *
1114
+ * await Effect.runPromise(ok) // => true
1136
1115
  * ```
1137
1116
  *
1138
1117
  * @category constructors
@@ -1407,7 +1386,7 @@ export interface Exploration<M extends AnyMachine, Key extends ExplorationKey =
1407
1386
  readonly completeness: ExplorationCompleteness<M, Key>
1408
1387
  }
1409
1388
 
1410
- interface ExploreOptionsBase<M extends AnyMachine, Key extends ExplorationKey> {
1389
+ type ExploreOptionsBase<M extends AnyMachine, Key extends ExplorationKey> = {
1411
1390
  readonly events: (context: ExplorationStateContext<M>) => ReadonlyArray<Machine.Machine.InputEvent<M>>
1412
1391
  readonly stateKey: (context: ExplorationStateContext<M>) => Key
1413
1392
  readonly limits?: ExplorationLimits
@@ -1443,41 +1422,36 @@ export type ExploreOptions<M extends AnyMachine, Key extends ExplorationKey = Ex
1443
1422
  * retained at a state-limit frontier. Depth- and transition-limit frontiers
1444
1423
  * have no plan and therefore contribute no transition hits.
1445
1424
  *
1446
- * **Example**
1425
+ * **Example** (Exploring a bounded counter)
1447
1426
  *
1448
- * ```ts
1427
+ * ```ts import.meta.vitest
1449
1428
  * import { Machine } from "@typeonce/effect-machine"
1450
1429
  * import { MachineTest } from "@typeonce/effect-machine/testing"
1451
- * import { Schema } from "effect"
1452
- * class Count extends Schema.TaggedClass<Count>("Count")("Count", {
1453
- * value: Schema.Number
1454
- * }) {
1455
- * }
1456
- * class Increment extends Schema.TaggedClass<Increment>("Increment")("Increment", {}) {
1457
- * }
1458
- * const States = Machine.state({ states: { Count } })
1459
- * const machine = Machine.make({
1460
- * root: States,
1461
- * events: Machine.eventsFromSchemas(Increment)
1462
- * }).handle({
1463
- * initial: {
1464
- * target: "Count",
1465
- * decoded: true,
1466
- * data: new Count({ value: 0 })
1467
- * },
1430
+ * import { Effect, Option, Schema } from "effect"
1431
+ *
1432
+ * const States = Machine.state({
1433
+ * states: { Count: { fields: { value: Schema.Number } } }
1434
+ * })
1435
+ * const Events = Machine.events({ Increment: {} })
1436
+ * const machine = Machine.make({ root: States, events: Events }).handle({
1437
+ * initial: { target: "Count", data: () => ({ value: 0 }) },
1468
1438
  * states: {
1469
1439
  * Count: {
1470
1440
  * on: {
1471
- * Increment: (to) =>
1472
- * to.branch.Count().resolve(({ state, target }) => target.decoded(new Count({ value: state.value + 1 })))
1441
+ * Increment: { update: { Count: ({ state }) => ({ value: state.value + 1 }) } }
1473
1442
  * }
1474
1443
  * }
1475
1444
  * }
1476
1445
  * })
1477
- * const explored = MachineTest.explore(machine, {
1478
- * events: ({ snapshot }) => snapshot.value.value < 2 ? [new Increment({})] : [],
1479
- * stateKey: ({ snapshot }) => snapshot.value.value
1480
- * })
1446
+ * const count = (snapshot: Machine.Snapshot<typeof States>) =>
1447
+ * States.get(snapshot, "Count").pipe(Option.map(({ value }) => value), Option.getOrElse(() => 0))
1448
+ *
1449
+ * const explored = await Effect.runPromise(MachineTest.explore(machine, {
1450
+ * events: ({ snapshot }) => count(snapshot) < 2 ? [Events.Increment()] : [],
1451
+ * stateKey: ({ snapshot }) => count(snapshot)
1452
+ * }))
1453
+ *
1454
+ * explored.nodes.length // => 3
1481
1455
  * ```
1482
1456
  *
1483
1457
  * @category exploration
@@ -1564,8 +1538,8 @@ export const assertUnreachable: <M extends AnyMachine, Key extends ExplorationKe
1564
1538
  * Checks a real planner trace against the independent finite statechart model
1565
1539
  * interpreter.
1566
1540
  *
1567
- * The oracle does not import the machine compiler, planner, snapshot helpers,
1568
- * or target builders. It compares semantic projections of the public trace
1541
+ * The oracle does not import the machine compiler, planner, or snapshot
1542
+ * helpers. It compares semantic projections of the public trace
1569
1543
  * and accumulates every disagreement in one structured error.
1570
1544
  *
1571
1545
  * @category verification
@@ -1640,27 +1614,21 @@ export type RunServices<M extends AnyMachine> = IsAny<
1640
1614
  * Typed planning errors retain the scenario and successfully completed prefix
1641
1615
  * in a `RunFailure`.
1642
1616
  *
1643
- * **Example**
1617
+ * **Example** (Running a pure trace)
1644
1618
  *
1645
- * ```ts
1619
+ * ```ts import.meta.vitest
1646
1620
  * import { Machine } from "@typeonce/effect-machine"
1647
1621
  * import { MachineTest } from "@typeonce/effect-machine/testing"
1648
- * import { Schema } from "effect"
1649
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
1650
- * }
1651
- * const States = Machine.state({ states: { Idle } })
1652
- * const machine = Machine.make({
1653
- * root: States,
1654
- * events: Machine.eventsFromSchemas()
1655
- * }).handle({
1656
- * initial: {
1657
- * target: "Idle"
1658
- * },
1659
- * states: {
1660
- * Idle: {}
1661
- * }
1622
+ * import { Effect } from "effect"
1623
+ *
1624
+ * const States = Machine.state({ states: { Idle: {} } })
1625
+ * const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
1626
+ * initial: { target: "Idle" }
1662
1627
  * })
1663
- * const trace = MachineTest.run(machine, { events: [] })
1628
+ *
1629
+ * const path = Effect.map(MachineTest.run(machine, { events: [] }), (trace) => trace.final.state.path)
1630
+ *
1631
+ * await Effect.runPromise(path) // => "Idle"
1664
1632
  * ```
1665
1633
  *
1666
1634
  * @category constructors
@@ -1911,27 +1879,22 @@ export interface Coverage<M extends AnyMachine> {
1911
1879
  * scenarios and logical configurations report observations only because their
1912
1880
  * complete spaces are generally infinite.
1913
1881
  *
1914
- * **Example**
1882
+ * **Example** (Reporting trace coverage)
1915
1883
  *
1916
- * ```ts
1884
+ * ```ts import.meta.vitest
1917
1885
  * import { Machine } from "@typeonce/effect-machine"
1918
1886
  * import { MachineTest } from "@typeonce/effect-machine/testing"
1919
- * import { Effect, Schema } from "effect"
1920
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
1921
- * }
1922
- * const States = Machine.state({ states: { Idle } })
1923
- * const machine = Machine.make({
1924
- * root: States,
1925
- * events: Machine.eventsFromSchemas()
1926
- * }).handle({
1927
- * initial: {
1928
- * target: "Idle"
1929
- * },
1930
- * states: {
1931
- * Idle: {}
1932
- * }
1887
+ * import { Effect } from "effect"
1888
+ *
1889
+ * const States = Machine.state({ states: { Idle: {} } })
1890
+ * const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
1891
+ * initial: { target: "Idle" }
1933
1892
  * })
1934
- * const report = Effect.map(MachineTest.run(machine, { events: [] }), (trace) => MachineTest.coverage(machine, trace))
1893
+ *
1894
+ * const missing = Effect.map(MachineTest.run(machine, { events: [] }), (trace) =>
1895
+ * MachineTest.coverage(machine, trace).transitions.definitions.missing)
1896
+ *
1897
+ * await Effect.runPromise(missing) // => 0
1935
1898
  * ```
1936
1899
  *
1937
1900
  * @category verification
@@ -2139,30 +2102,25 @@ export interface VerifyOptions {
2139
2102
  * Every selected law is evaluated and returned in one structured error so a
2140
2103
  * shrunk property-test counterexample retains all relevant evidence.
2141
2104
  *
2142
- * **Example**
2105
+ * **Example** (Verifying planner laws on a trace)
2143
2106
  *
2144
- * ```ts
2107
+ * ```ts import.meta.vitest
2145
2108
  * import { Machine } from "@typeonce/effect-machine"
2146
2109
  * import { MachineTest } from "@typeonce/effect-machine/testing"
2147
- * import { Effect, Schema } from "effect"
2148
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
2149
- * }
2150
- * const States = Machine.state({ states: { Idle } })
2151
- * const machine = Machine.make({
2152
- * root: States,
2153
- * events: Machine.eventsFromSchemas()
2154
- * }).handle({
2155
- * initial: {
2156
- * target: "Idle"
2157
- * },
2158
- * states: {
2159
- * Idle: {}
2160
- * }
2110
+ * import { Effect } from "effect"
2111
+ *
2112
+ * const States = Machine.state({ states: { Idle: {} } })
2113
+ * const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
2114
+ * initial: { target: "Idle" }
2161
2115
  * })
2162
- * const checked = Effect.gen(function*() {
2116
+ *
2117
+ * const verified = Effect.gen(function*() {
2163
2118
  * const trace = yield* MachineTest.run(machine, { events: [] })
2164
2119
  * yield* MachineTest.verify(machine, trace)
2120
+ * return true
2165
2121
  * })
2122
+ *
2123
+ * await Effect.runPromise(verified) // => true
2166
2124
  * ```
2167
2125
  *
2168
2126
  * @category verification
@@ -2174,6 +2132,269 @@ export const verify: <M extends AnyMachine>(
2174
2132
  options?: VerifyOptions
2175
2133
  ) => Effect.Effect<void, VerificationError> = internal.verify
2176
2134
 
2135
+ /**
2136
+ * A problem found by reading a machine's declarations, without running any of
2137
+ * its callbacks.
2138
+ *
2139
+ * @category models
2140
+ * @since 0.47.0
2141
+ */
2142
+ export type AnalysisFinding =
2143
+ | {
2144
+ readonly law: "topology.noPathFromInitial"
2145
+ readonly severity: "error"
2146
+ readonly path: string
2147
+ /** Descendants of `path` that are unreachable for the same reason. */
2148
+ readonly descendants: number
2149
+ readonly message: string
2150
+ }
2151
+ | {
2152
+ readonly law: "events.noHandler"
2153
+ readonly severity: "error"
2154
+ readonly event: string
2155
+ readonly message: string
2156
+ }
2157
+
2158
+ /**
2159
+ * Reads a machine's declared topology and reports states with no path from the
2160
+ * initial state and public events with no handler.
2161
+ *
2162
+ * **When to use**
2163
+ *
2164
+ * Use when a question has to be answered without running the machine. Use
2165
+ * {@link check} to also explore the machine and verify its planner laws.
2166
+ *
2167
+ * **Details**
2168
+ *
2169
+ * The analysis is conservative. A target can enter any descendant of the
2170
+ * state it names, and a history target any descendant of its owner, so that
2171
+ * whole subtree counts as reachable, and a state is only reported when no
2172
+ * declaration could enter it.
2173
+ *
2174
+ * **Example** (Finding a state with no incoming transition)
2175
+ *
2176
+ * ```ts import.meta.vitest
2177
+ * import { Machine } from "@typeonce/effect-machine"
2178
+ * import { MachineTest } from "@typeonce/effect-machine/testing"
2179
+ *
2180
+ * const machine = Machine.make({
2181
+ * root: Machine.state({ states: { Idle: {}, Orphan: {} } }),
2182
+ * events: Machine.events({ Start: {} })
2183
+ * }).handle({
2184
+ * initial: { target: "Idle" },
2185
+ * states: { Idle: { on: { Start: { target: "Idle" } } } }
2186
+ * })
2187
+ *
2188
+ * MachineTest.analyze(machine).map((finding) => finding.law) // => ["topology.noPathFromInitial"]
2189
+ * ```
2190
+ *
2191
+ * @category verification
2192
+ * @since 0.47.0
2193
+ */
2194
+ export const analyze: <M extends AnyMachine>(machine: M) => ReadonlyArray<AnalysisFinding> = CheckImpl.analyze
2195
+
2196
+ /**
2197
+ * The events and optional input that reproduce a finding from startup.
2198
+ *
2199
+ * @category models
2200
+ * @since 0.47.0
2201
+ */
2202
+ export interface CheckReplay {
2203
+ readonly input?: unknown
2204
+ readonly events: ReadonlyArray<unknown>
2205
+ }
2206
+
2207
+ /**
2208
+ * Why {@link check} could not prove it explored every behavior. An empty list
2209
+ * is what makes a report `"exhaustive"`.
2210
+ *
2211
+ * @category models
2212
+ * @since 0.47.0
2213
+ */
2214
+ export type CheckExplorationLimit =
2215
+ /** The depth, state, or transition limit stopped exploration early. */
2216
+ | "depth"
2217
+ | "states"
2218
+ | "transitions"
2219
+ /** Event payloads were sampled from their schemas. */
2220
+ | "sampledEvents"
2221
+ /** Machine input was sampled from its schema. */
2222
+ | "sampledInput"
2223
+ /** Exploration used caller-supplied events. */
2224
+ | "customEvents"
2225
+ /** A caller-supplied state key may merge distinct snapshots. */
2226
+ | "customStateKey"
2227
+ /** Invocations do not run during planning, so their outcomes were not explored. */
2228
+ | "invocations"
2229
+
2230
+ /**
2231
+ * A problem found by {@link check}. An `"error"` finding fails the report and a
2232
+ * `"warning"` does not.
2233
+ *
2234
+ * @category models
2235
+ * @since 0.47.0
2236
+ */
2237
+ export type CheckFinding =
2238
+ | AnalysisFinding
2239
+ | {
2240
+ readonly law: "schema.underivable"
2241
+ readonly severity: "error"
2242
+ readonly message: string
2243
+ }
2244
+ | {
2245
+ readonly law: "planning.failed"
2246
+ readonly severity: "error"
2247
+ readonly replay: CheckReplay
2248
+ readonly message: string
2249
+ }
2250
+ | {
2251
+ readonly law: "planning.deterministic"
2252
+ readonly severity: "error"
2253
+ /** `"initial"`, or the active state paths and event tag that were planned. */
2254
+ readonly source: string
2255
+ readonly replay: CheckReplay
2256
+ readonly message: string
2257
+ }
2258
+ | {
2259
+ readonly law: "planning.verification"
2260
+ readonly severity: "error"
2261
+ readonly verificationLaw: VerificationLaw
2262
+ readonly replay: CheckReplay
2263
+ readonly message: string
2264
+ }
2265
+ | {
2266
+ readonly law: "invariant.violated"
2267
+ readonly severity: "error"
2268
+ readonly invariant: string
2269
+ readonly replay: CheckReplay
2270
+ readonly message: string
2271
+ }
2272
+ | {
2273
+ readonly law: "transition.notCovered"
2274
+ readonly severity: "warning"
2275
+ readonly source: string
2276
+ readonly trigger: Machine.Machine.TransitionTrigger
2277
+ readonly target: string | undefined
2278
+ readonly branchKey?: string
2279
+ readonly message: string
2280
+ }
2281
+
2282
+ /**
2283
+ * The result of {@link check}.
2284
+ *
2285
+ * @category models
2286
+ * @since 0.47.0
2287
+ */
2288
+ export interface CheckReport {
2289
+ readonly machine: string
2290
+ /** Whether the report has no `"error"` finding. */
2291
+ readonly ok: boolean
2292
+ /**
2293
+ * `"exhaustive"` only when `limits` is empty. A `"bounded"` pass means nothing
2294
+ * was found within what was explored, not that the machine is correct.
2295
+ */
2296
+ readonly exploration: "exhaustive" | "bounded"
2297
+ readonly limits: ReadonlyArray<CheckExplorationLimit>
2298
+ readonly stats: {
2299
+ readonly states: number
2300
+ readonly plannedTransitions: number
2301
+ readonly transitions: { readonly total: number; readonly hit: number }
2302
+ readonly branches: { readonly total: number; readonly hit: number }
2303
+ }
2304
+ readonly findings: ReadonlyArray<CheckFinding>
2305
+ }
2306
+
2307
+ /**
2308
+ * Options for {@link check}. Every option has a default derived from the machine.
2309
+ *
2310
+ * @category models
2311
+ * @since 0.47.0
2312
+ */
2313
+ export interface CheckOptions<M extends AnyMachine> {
2314
+ /** Machine input. Defaults to a value sampled from the input schema. */
2315
+ readonly input?: InputValue<M>
2316
+ /** Events to try in every state. Defaults to samples derived from the event schemas. */
2317
+ readonly events?: ReadonlyArray<Machine.Machine.EventInput<Machine.Machine.InputEvent<M>>>
2318
+ /** Distinct values to sample for each event tag. Defaults to 3. */
2319
+ readonly eventSamples?: number
2320
+ /** Seed for sampled input and events. Defaults to 0, so reports are reproducible. */
2321
+ readonly seed?: number
2322
+ /** Identifies logical states. Defaults to the complete snapshot value. */
2323
+ readonly stateKey?: (context: ExplorationStateContext<M>) => ExplorationKey
2324
+ /** Defaults to `maxDepth: 16`, `maxStates: 256`, and `maxTransitions: 2048`. */
2325
+ readonly limits?: ExplorationLimits
2326
+ /** Application laws checked against every explored state and step. */
2327
+ readonly invariants?: ReadonlyArray<Invariant<M>>
2328
+ }
2329
+
2330
+ /**
2331
+ * Checks a machine with one call and reports every problem as data.
2332
+ *
2333
+ * **When to use**
2334
+ *
2335
+ * Use as the first verification step after writing or changing a machine. Reach
2336
+ * for {@link explore}, {@link verify}, and {@link coverage} directly when a test
2337
+ * needs their complete evidence, such as a specific scenario.
2338
+ *
2339
+ * **Details**
2340
+ *
2341
+ * Runs {@link analyze}, then {@link explore} with events derived from the event
2342
+ * schemas, then {@link verify} on every explored transition. It also plans each
2343
+ * transition a second time, so nondeterminism is found even when hidden behind a
2344
+ * helper. Declared branches that were never taken are reported as warnings.
2345
+ *
2346
+ * Findings are returned rather than failed, so the Effect only fails on
2347
+ * interruption. Each finding that came from running the machine includes the
2348
+ * events that reproduce it.
2349
+ *
2350
+ * **Gotchas**
2351
+ *
2352
+ * A passing `"bounded"` report means nothing was found in what was explored. It
2353
+ * is not a proof. Invocations do not run during planning, so test them with
2354
+ * {@link probe} on a started machine.
2355
+ *
2356
+ * **Example** (Finding nondeterministic planning)
2357
+ *
2358
+ * ```ts import.meta.vitest
2359
+ * import { Machine } from "@typeonce/effect-machine"
2360
+ * import { MachineTest } from "@typeonce/effect-machine/testing"
2361
+ * import { Effect, Schema } from "effect"
2362
+ *
2363
+ * const nextAmount = () => Math.random()
2364
+ * const machine = Machine.make({
2365
+ * root: Machine.state({
2366
+ * states: { Editing: {}, Charging: { fields: { amount: Schema.Number } } }
2367
+ * }),
2368
+ * events: Machine.events({ Pay: {} })
2369
+ * }).handle({
2370
+ * initial: { target: "Editing" },
2371
+ * states: {
2372
+ * Editing: { on: { Pay: { target: "Charging", data: () => ({ amount: nextAmount() }) } } }
2373
+ * }
2374
+ * })
2375
+ *
2376
+ * const report = await Effect.runPromise(MachineTest.check(machine))
2377
+ *
2378
+ * report.ok // => false
2379
+ * report.findings.map((finding) => finding.law) // => ["planning.deterministic"]
2380
+ * ```
2381
+ *
2382
+ * @category verification
2383
+ * @since 0.47.0
2384
+ */
2385
+ export const check: <M extends AnyMachine>(
2386
+ machine: RootReadyMachine<M>,
2387
+ options?: CheckOptions<M>
2388
+ ) => Effect.Effect<CheckReport, never, RunServices<M>> = CheckImpl.check
2389
+
2390
+ /**
2391
+ * Formats a report as line-oriented text for a terminal or an agent transcript.
2392
+ *
2393
+ * @category verification
2394
+ * @since 0.47.0
2395
+ */
2396
+ export const formatCheckReport: (report: CheckReport) => string = CheckImpl.formatReport
2397
+
2177
2398
  /**
2178
2399
  * Formats a trace as deterministic, line-oriented counterexample evidence.
2179
2400
  *