@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
|
@@ -18,9 +18,9 @@ import type * as RuntimeTest from "../internal/testing/machine/runtime.js";
|
|
|
18
18
|
import * as RuntimeInvariantTest from "../internal/testing/machine/runtimeInvariant.js";
|
|
19
19
|
import type { VerificationError } from "../internal/testing/machine/verification.js";
|
|
20
20
|
import type * as Machine from "../Machine.js";
|
|
21
|
-
export { advanceCommand, type CausalRuntimeAssertionContext, type CausalRuntimeCommandActual, CausalRuntimeCommandFailure, type CausalRuntimeCommandRecord, type CausalRuntimeCommandResult, type CausalRuntimeInspectionContext, type CausalRuntimeModelOptions, type CausalRuntimeModelStep, type CausalRuntimeTranscript, type CausalVerificationAwaitContext, type CausalVerificationOptions, type CausalVerificationTranscript, checkpointCommand, type EnqueuedRuntimeAssertionContext, type EnqueuedRuntimeCommandActual, type EnqueuedRuntimeCommandRecord, type EnqueuedRuntimeInspectionContext, type EnqueuedRuntimeModelOptions, type EnqueuedRuntimeModelStep, type EnqueuedRuntimeTranscript, formatCausalTranscript, formatEnqueuedTranscript,
|
|
21
|
+
export { advanceCommand, type CausalRuntimeAssertionContext, type CausalRuntimeCommandActual, type CausalRuntimeCommandAttempt, CausalRuntimeCommandFailure, type CausalRuntimeCommandRecord, type CausalRuntimeCommandResult, type CausalRuntimeInspectionContext, type CausalRuntimeModelOptions, type CausalRuntimeModelStep, type CausalRuntimeTranscript, type CausalVerificationAwaitContext, type CausalVerificationOptions, type CausalVerificationTranscript, checkpointCommand, type EnqueuedRuntimeAssertionContext, type EnqueuedRuntimeCommandActual, type EnqueuedRuntimeCommandRecord, type EnqueuedRuntimeInspectionContext, type EnqueuedRuntimeModelOptions, type EnqueuedRuntimeModelStep, type EnqueuedRuntimeTranscript, formatCausalTranscript, formatEnqueuedTranscript, runCausalCommands, runEnqueuedCommands, type RuntimeAwait, type RuntimeCommand, RuntimeCommandFailure, type RuntimeCommandResult, type RuntimeCommands, runtimeCommands, type RuntimeCommandsDiagnostics, type RuntimeCommandsOptions, RuntimeObservationError, RuntimeSynchronization, sendCommand, stopCommand, verifyCausalCommands } from "../internal/testing/machine/runtime.js";
|
|
22
22
|
export type { SchemaArbitraryOpaqueFilterWarning, SchemaArbitraryReport, SchemaArbitraryWarning } from "../internal/testing/machine/arbitrary.js";
|
|
23
|
-
export { compileModel, type FiniteAtomicState, type FiniteAutomaticTransition, type FiniteCompoundState, type FiniteEventTransition, type FiniteFinalState, type FiniteHistoryMutation, type FiniteHistoryScenario, type FiniteHistoryState, type FiniteHistoryTransfer, type FiniteModel, type FiniteModelDiagnostics, type FiniteModelOptions, type FiniteModels, finiteModels, type FiniteParallelState, type FiniteState, type FiniteTransition, type FiniteTransitionTrigger } from "../internal/testing/machine/finiteModel.js";
|
|
23
|
+
export { compileModel, type FiniteAtomicState, type FiniteAutomaticTransition, type FiniteChoiceState, type FiniteCompoundState, type FiniteEventTransition, type FiniteFinalState, type FiniteHistoryMutation, type FiniteHistoryScenario, type FiniteHistoryState, type FiniteHistoryTransfer, type FiniteModel, type FiniteModelDiagnostics, type FiniteModelOptions, type FiniteModels, finiteModels, type FiniteParallelState, type FiniteState, type FiniteTransition, type FiniteTransitionTrigger } from "../internal/testing/machine/finiteModel.js";
|
|
24
24
|
export { ModelVerificationError, type ModelVerificationField, type ModelVerificationLocation, type ModelVerificationMismatch, type ReferenceCompletion, type ReferenceHistoryRecord, type ReferenceInitialStep, type ReferenceMicrostep, type ReferenceState, type ReferenceStateValue, type ReferenceStep, type ReferenceTrace, type ReferenceTransition } from "../internal/testing/machine/referenceModel.js";
|
|
25
25
|
/**
|
|
26
26
|
* Purely interprets a finite hierarchical model without compiling a
|
|
@@ -108,33 +108,31 @@ export interface Scenarios<M extends AnyMachine> {
|
|
|
108
108
|
* Unsupported schema derivations fail immediately through `Arbitrary.schema`.
|
|
109
109
|
* Non-fatal derivation warnings are returned instead of being hidden.
|
|
110
110
|
*
|
|
111
|
-
* **Example**
|
|
111
|
+
* **Example** (Generating event scenarios)
|
|
112
112
|
*
|
|
113
|
-
* ```ts
|
|
113
|
+
* ```ts import.meta.vitest
|
|
114
114
|
* import { Machine } from "@typeonce/effect-machine"
|
|
115
115
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
116
|
-
* import {
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
* }
|
|
121
|
-
* const States = Machine.state({ states: { Idle } })
|
|
116
|
+
* import { Effect } from "effect"
|
|
117
|
+
* import * as Arbitrary from "effect/Arbitrary"
|
|
118
|
+
*
|
|
119
|
+
* const Events = Machine.events({ Reset: {} })
|
|
122
120
|
* const machine = Machine.make({
|
|
123
|
-
* root:
|
|
124
|
-
* events:
|
|
121
|
+
* root: Machine.state({ states: { Idle: {} } }),
|
|
122
|
+
* events: Events
|
|
125
123
|
* }).handle({
|
|
126
|
-
* initial: {
|
|
127
|
-
* target: "Idle"
|
|
128
|
-
* },
|
|
124
|
+
* initial: { target: "Idle" },
|
|
129
125
|
* states: {
|
|
130
|
-
* Idle: {
|
|
131
|
-
* on: {
|
|
132
|
-
* Reset: (to) => to.branch.Idle().resolve(({ target }) => target.from())
|
|
133
|
-
* }
|
|
134
|
-
* }
|
|
126
|
+
* Idle: { on: { Reset: { target: "Idle" } } }
|
|
135
127
|
* }
|
|
136
128
|
* })
|
|
129
|
+
*
|
|
137
130
|
* const generated = MachineTest.scenarios(machine, { maxEvents: 5 })
|
|
131
|
+
* const samples = Arbitrary.sampleEffect(generated.arbitrary, { count: 1, seed: 1 })
|
|
132
|
+
*
|
|
133
|
+
* const count = Effect.map(samples, (scenarios) => scenarios.length)
|
|
134
|
+
*
|
|
135
|
+
* await Effect.runPromise(count) // => 1
|
|
138
136
|
* ```
|
|
139
137
|
*
|
|
140
138
|
* @category constructors
|
|
@@ -318,30 +316,25 @@ export { ProbeUnavailableError } from "../internal/testing/machine/probe.js";
|
|
|
318
316
|
* available exclusively through `MachineRef.send` and retain their
|
|
319
317
|
* asynchronous enqueue-only semantics.
|
|
320
318
|
*
|
|
321
|
-
* **Example**
|
|
319
|
+
* **Example** (Acknowledging commands at runtime)
|
|
322
320
|
*
|
|
323
|
-
* ```ts
|
|
321
|
+
* ```ts import.meta.vitest
|
|
324
322
|
* import { Machine } from "@typeonce/effect-machine"
|
|
325
323
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
326
|
-
* import { Effect
|
|
327
|
-
*
|
|
328
|
-
* }
|
|
329
|
-
* const
|
|
330
|
-
*
|
|
331
|
-
* root: States,
|
|
332
|
-
* events: Machine.eventsFromSchemas()
|
|
333
|
-
* }).handle({
|
|
334
|
-
* initial: {
|
|
335
|
-
* target: "Idle"
|
|
336
|
-
* },
|
|
337
|
-
* states: {
|
|
338
|
-
* Idle: {}
|
|
339
|
-
* }
|
|
324
|
+
* import { Effect } from "effect"
|
|
325
|
+
*
|
|
326
|
+
* const States = Machine.state({ states: { Idle: {} } })
|
|
327
|
+
* const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
|
|
328
|
+
* initial: { target: "Idle" }
|
|
340
329
|
* })
|
|
341
|
-
*
|
|
330
|
+
*
|
|
331
|
+
* const probed = Effect.scoped(Effect.gen(function*() {
|
|
342
332
|
* const ref = yield* Machine.start(machine)
|
|
343
|
-
*
|
|
333
|
+
* const probe = yield* MachineTest.probe(machine, ref)
|
|
334
|
+
* return typeof probe
|
|
344
335
|
* }))
|
|
336
|
+
*
|
|
337
|
+
* await Effect.runPromise(probed) // => "object"
|
|
345
338
|
* ```
|
|
346
339
|
*
|
|
347
340
|
* @category runtime testing
|
|
@@ -789,34 +782,26 @@ export declare const Invariant: {
|
|
|
789
782
|
* types. The machine is used only for inference; invariant evaluation remains
|
|
790
783
|
* pure and reusable across traces from that machine.
|
|
791
784
|
*
|
|
792
|
-
* **Example**
|
|
785
|
+
* **Example** (Declaring a state invariant)
|
|
793
786
|
*
|
|
794
|
-
* ```ts
|
|
787
|
+
* ```ts import.meta.vitest
|
|
795
788
|
* import { Machine } from "@typeonce/effect-machine"
|
|
796
789
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
797
|
-
* import { Schema } from "effect"
|
|
798
|
-
*
|
|
799
|
-
*
|
|
800
|
-
* }) {
|
|
801
|
-
* }
|
|
802
|
-
* const States = Machine.state({ states: { Count } })
|
|
803
|
-
* const machine = Machine.make({
|
|
804
|
-
* root: States,
|
|
805
|
-
* events: Machine.eventsFromSchemas()
|
|
806
|
-
* }).handle({
|
|
807
|
-
* initial: {
|
|
808
|
-
* target: "Count",
|
|
809
|
-
* decoded: true,
|
|
810
|
-
* data: new Count({ value: 0 })
|
|
811
|
-
* },
|
|
812
|
-
* states: {
|
|
813
|
-
* Count: {}
|
|
814
|
-
* }
|
|
790
|
+
* import { Effect, Option, Schema } from "effect"
|
|
791
|
+
*
|
|
792
|
+
* const States = Machine.state({ states: { Count: { fields: { value: Schema.Number } } } })
|
|
793
|
+
* const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
|
|
794
|
+
* initial: { target: "Count", data: { value: 0 } }
|
|
815
795
|
* })
|
|
796
|
+
*
|
|
816
797
|
* const nonNegative = MachineTest.invariants(machine).state(
|
|
817
798
|
* "count is non-negative",
|
|
818
|
-
* ({ snapshot }) => snapshot.
|
|
799
|
+
* ({ snapshot }) => States.get(snapshot, "Count").pipe(Option.exists(({ value }) => value >= 0))
|
|
819
800
|
* )
|
|
801
|
+
*
|
|
802
|
+
* const ok = Effect.map(MachineTest.check(machine, { invariants: [nonNegative] }), (report) => report.ok)
|
|
803
|
+
*
|
|
804
|
+
* await Effect.runPromise(ok) // => true
|
|
820
805
|
* ```
|
|
821
806
|
*
|
|
822
807
|
* @category constructors
|
|
@@ -1053,12 +1038,12 @@ export interface Exploration<M extends AnyMachine, Key extends ExplorationKey =
|
|
|
1053
1038
|
readonly transitionCoverage: TransitionCoverage<StateNodePath<M>, Machine.Machine.TagOf<Machine.Machine.Events<M>[number]>, StateNodePath<M>>;
|
|
1054
1039
|
readonly completeness: ExplorationCompleteness<M, Key>;
|
|
1055
1040
|
}
|
|
1056
|
-
|
|
1041
|
+
type ExploreOptionsBase<M extends AnyMachine, Key extends ExplorationKey> = {
|
|
1057
1042
|
readonly events: (context: ExplorationStateContext<M>) => ReadonlyArray<Machine.Machine.InputEvent<M>>;
|
|
1058
1043
|
readonly stateKey: (context: ExplorationStateContext<M>) => Key;
|
|
1059
1044
|
readonly limits?: ExplorationLimits;
|
|
1060
1045
|
readonly invariants?: ReadonlyArray<Invariant<M>>;
|
|
1061
|
-
}
|
|
1046
|
+
};
|
|
1062
1047
|
/**
|
|
1063
1048
|
* Configuration for bounded planner exploration.
|
|
1064
1049
|
*
|
|
@@ -1084,41 +1069,36 @@ export type ExploreOptions<M extends AnyMachine, Key extends ExplorationKey = Ex
|
|
|
1084
1069
|
* retained at a state-limit frontier. Depth- and transition-limit frontiers
|
|
1085
1070
|
* have no plan and therefore contribute no transition hits.
|
|
1086
1071
|
*
|
|
1087
|
-
* **Example**
|
|
1072
|
+
* **Example** (Exploring a bounded counter)
|
|
1088
1073
|
*
|
|
1089
|
-
* ```ts
|
|
1074
|
+
* ```ts import.meta.vitest
|
|
1090
1075
|
* import { Machine } from "@typeonce/effect-machine"
|
|
1091
1076
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
1092
|
-
* import { Schema } from "effect"
|
|
1093
|
-
*
|
|
1094
|
-
*
|
|
1095
|
-
* }
|
|
1096
|
-
* }
|
|
1097
|
-
*
|
|
1098
|
-
* }
|
|
1099
|
-
*
|
|
1100
|
-
* const machine = Machine.make({
|
|
1101
|
-
* root: States,
|
|
1102
|
-
* events: Machine.eventsFromSchemas(Increment)
|
|
1103
|
-
* }).handle({
|
|
1104
|
-
* initial: {
|
|
1105
|
-
* target: "Count",
|
|
1106
|
-
* decoded: true,
|
|
1107
|
-
* data: new Count({ value: 0 })
|
|
1108
|
-
* },
|
|
1077
|
+
* import { Effect, Option, Schema } from "effect"
|
|
1078
|
+
*
|
|
1079
|
+
* const States = Machine.state({
|
|
1080
|
+
* states: { Count: { fields: { value: Schema.Number } } }
|
|
1081
|
+
* })
|
|
1082
|
+
* const Events = Machine.events({ Increment: {} })
|
|
1083
|
+
* const machine = Machine.make({ root: States, events: Events }).handle({
|
|
1084
|
+
* initial: { target: "Count", data: () => ({ value: 0 }) },
|
|
1109
1085
|
* states: {
|
|
1110
1086
|
* Count: {
|
|
1111
1087
|
* on: {
|
|
1112
|
-
* Increment: (
|
|
1113
|
-
* to.branch.Count().resolve(({ state, target }) => target.decoded(new Count({ value: state.value + 1 })))
|
|
1088
|
+
* Increment: { update: { Count: ({ state }) => ({ value: state.value + 1 }) } }
|
|
1114
1089
|
* }
|
|
1115
1090
|
* }
|
|
1116
1091
|
* }
|
|
1117
1092
|
* })
|
|
1118
|
-
* const
|
|
1119
|
-
*
|
|
1120
|
-
*
|
|
1121
|
-
*
|
|
1093
|
+
* const count = (snapshot: Machine.Snapshot<typeof States>) =>
|
|
1094
|
+
* States.get(snapshot, "Count").pipe(Option.map(({ value }) => value), Option.getOrElse(() => 0))
|
|
1095
|
+
*
|
|
1096
|
+
* const explored = await Effect.runPromise(MachineTest.explore(machine, {
|
|
1097
|
+
* events: ({ snapshot }) => count(snapshot) < 2 ? [Events.Increment()] : [],
|
|
1098
|
+
* stateKey: ({ snapshot }) => count(snapshot)
|
|
1099
|
+
* }))
|
|
1100
|
+
*
|
|
1101
|
+
* explored.nodes.length // => 3
|
|
1122
1102
|
* ```
|
|
1123
1103
|
*
|
|
1124
1104
|
* @category exploration
|
|
@@ -1178,8 +1158,8 @@ export declare const assertUnreachable: <M extends AnyMachine, Key extends Explo
|
|
|
1178
1158
|
* Checks a real planner trace against the independent finite statechart model
|
|
1179
1159
|
* interpreter.
|
|
1180
1160
|
*
|
|
1181
|
-
* The oracle does not import the machine compiler, planner, snapshot
|
|
1182
|
-
*
|
|
1161
|
+
* The oracle does not import the machine compiler, planner, or snapshot
|
|
1162
|
+
* helpers. It compares semantic projections of the public trace
|
|
1183
1163
|
* and accumulates every disagreement in one structured error.
|
|
1184
1164
|
*
|
|
1185
1165
|
* @category verification
|
|
@@ -1238,27 +1218,21 @@ export type RunServices<M extends AnyMachine> = IsAny<Machine.PlanningServices<M
|
|
|
1238
1218
|
* Typed planning errors retain the scenario and successfully completed prefix
|
|
1239
1219
|
* in a `RunFailure`.
|
|
1240
1220
|
*
|
|
1241
|
-
* **Example**
|
|
1221
|
+
* **Example** (Running a pure trace)
|
|
1242
1222
|
*
|
|
1243
|
-
* ```ts
|
|
1223
|
+
* ```ts import.meta.vitest
|
|
1244
1224
|
* import { Machine } from "@typeonce/effect-machine"
|
|
1245
1225
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
1246
|
-
* import {
|
|
1247
|
-
*
|
|
1248
|
-
* }
|
|
1249
|
-
* const
|
|
1250
|
-
*
|
|
1251
|
-
* root: States,
|
|
1252
|
-
* events: Machine.eventsFromSchemas()
|
|
1253
|
-
* }).handle({
|
|
1254
|
-
* initial: {
|
|
1255
|
-
* target: "Idle"
|
|
1256
|
-
* },
|
|
1257
|
-
* states: {
|
|
1258
|
-
* Idle: {}
|
|
1259
|
-
* }
|
|
1226
|
+
* import { Effect } from "effect"
|
|
1227
|
+
*
|
|
1228
|
+
* const States = Machine.state({ states: { Idle: {} } })
|
|
1229
|
+
* const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
|
|
1230
|
+
* initial: { target: "Idle" }
|
|
1260
1231
|
* })
|
|
1261
|
-
*
|
|
1232
|
+
*
|
|
1233
|
+
* const path = Effect.map(MachineTest.run(machine, { events: [] }), (trace) => trace.final.state.path)
|
|
1234
|
+
*
|
|
1235
|
+
* await Effect.runPromise(path) // => "Idle"
|
|
1262
1236
|
* ```
|
|
1263
1237
|
*
|
|
1264
1238
|
* @category constructors
|
|
@@ -1473,27 +1447,22 @@ export interface Coverage<M extends AnyMachine> {
|
|
|
1473
1447
|
* scenarios and logical configurations report observations only because their
|
|
1474
1448
|
* complete spaces are generally infinite.
|
|
1475
1449
|
*
|
|
1476
|
-
* **Example**
|
|
1450
|
+
* **Example** (Reporting trace coverage)
|
|
1477
1451
|
*
|
|
1478
|
-
* ```ts
|
|
1452
|
+
* ```ts import.meta.vitest
|
|
1479
1453
|
* import { Machine } from "@typeonce/effect-machine"
|
|
1480
1454
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
1481
|
-
* import { Effect
|
|
1482
|
-
*
|
|
1483
|
-
* }
|
|
1484
|
-
* const
|
|
1485
|
-
*
|
|
1486
|
-
* root: States,
|
|
1487
|
-
* events: Machine.eventsFromSchemas()
|
|
1488
|
-
* }).handle({
|
|
1489
|
-
* initial: {
|
|
1490
|
-
* target: "Idle"
|
|
1491
|
-
* },
|
|
1492
|
-
* states: {
|
|
1493
|
-
* Idle: {}
|
|
1494
|
-
* }
|
|
1455
|
+
* import { Effect } from "effect"
|
|
1456
|
+
*
|
|
1457
|
+
* const States = Machine.state({ states: { Idle: {} } })
|
|
1458
|
+
* const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
|
|
1459
|
+
* initial: { target: "Idle" }
|
|
1495
1460
|
* })
|
|
1496
|
-
*
|
|
1461
|
+
*
|
|
1462
|
+
* const missing = Effect.map(MachineTest.run(machine, { events: [] }), (trace) =>
|
|
1463
|
+
* MachineTest.coverage(machine, trace).transitions.definitions.missing)
|
|
1464
|
+
*
|
|
1465
|
+
* await Effect.runPromise(missing) // => 0
|
|
1497
1466
|
* ```
|
|
1498
1467
|
*
|
|
1499
1468
|
* @category verification
|
|
@@ -1642,36 +1611,277 @@ export interface VerifyOptions {
|
|
|
1642
1611
|
* Every selected law is evaluated and returned in one structured error so a
|
|
1643
1612
|
* shrunk property-test counterexample retains all relevant evidence.
|
|
1644
1613
|
*
|
|
1645
|
-
* **Example**
|
|
1614
|
+
* **Example** (Verifying planner laws on a trace)
|
|
1646
1615
|
*
|
|
1647
|
-
* ```ts
|
|
1616
|
+
* ```ts import.meta.vitest
|
|
1648
1617
|
* import { Machine } from "@typeonce/effect-machine"
|
|
1649
1618
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
1650
|
-
* import { Effect
|
|
1651
|
-
*
|
|
1652
|
-
* }
|
|
1653
|
-
* const
|
|
1654
|
-
*
|
|
1655
|
-
* root: States,
|
|
1656
|
-
* events: Machine.eventsFromSchemas()
|
|
1657
|
-
* }).handle({
|
|
1658
|
-
* initial: {
|
|
1659
|
-
* target: "Idle"
|
|
1660
|
-
* },
|
|
1661
|
-
* states: {
|
|
1662
|
-
* Idle: {}
|
|
1663
|
-
* }
|
|
1619
|
+
* import { Effect } from "effect"
|
|
1620
|
+
*
|
|
1621
|
+
* const States = Machine.state({ states: { Idle: {} } })
|
|
1622
|
+
* const machine = Machine.make({ root: States, events: Machine.events({}) }).handle({
|
|
1623
|
+
* initial: { target: "Idle" }
|
|
1664
1624
|
* })
|
|
1665
|
-
*
|
|
1625
|
+
*
|
|
1626
|
+
* const verified = Effect.gen(function*() {
|
|
1666
1627
|
* const trace = yield* MachineTest.run(machine, { events: [] })
|
|
1667
1628
|
* yield* MachineTest.verify(machine, trace)
|
|
1629
|
+
* return true
|
|
1668
1630
|
* })
|
|
1631
|
+
*
|
|
1632
|
+
* await Effect.runPromise(verified) // => true
|
|
1669
1633
|
* ```
|
|
1670
1634
|
*
|
|
1671
1635
|
* @category verification
|
|
1672
1636
|
* @since 0.4.0
|
|
1673
1637
|
*/
|
|
1674
1638
|
export declare const verify: <M extends AnyMachine>(machine: M, trace: Trace<M>, options?: VerifyOptions) => Effect.Effect<void, VerificationError>;
|
|
1639
|
+
/**
|
|
1640
|
+
* A problem found by reading a machine's declarations, without running any of
|
|
1641
|
+
* its callbacks.
|
|
1642
|
+
*
|
|
1643
|
+
* @category models
|
|
1644
|
+
* @since 0.47.0
|
|
1645
|
+
*/
|
|
1646
|
+
export type AnalysisFinding = {
|
|
1647
|
+
readonly law: "topology.noPathFromInitial";
|
|
1648
|
+
readonly severity: "error";
|
|
1649
|
+
readonly path: string;
|
|
1650
|
+
/** Descendants of `path` that are unreachable for the same reason. */
|
|
1651
|
+
readonly descendants: number;
|
|
1652
|
+
readonly message: string;
|
|
1653
|
+
} | {
|
|
1654
|
+
readonly law: "events.noHandler";
|
|
1655
|
+
readonly severity: "error";
|
|
1656
|
+
readonly event: string;
|
|
1657
|
+
readonly message: string;
|
|
1658
|
+
};
|
|
1659
|
+
/**
|
|
1660
|
+
* Reads a machine's declared topology and reports states with no path from the
|
|
1661
|
+
* initial state and public events with no handler.
|
|
1662
|
+
*
|
|
1663
|
+
* **When to use**
|
|
1664
|
+
*
|
|
1665
|
+
* Use when a question has to be answered without running the machine. Use
|
|
1666
|
+
* {@link check} to also explore the machine and verify its planner laws.
|
|
1667
|
+
*
|
|
1668
|
+
* **Details**
|
|
1669
|
+
*
|
|
1670
|
+
* The analysis is conservative. A target can enter any descendant of the
|
|
1671
|
+
* state it names, and a history target any descendant of its owner, so that
|
|
1672
|
+
* whole subtree counts as reachable, and a state is only reported when no
|
|
1673
|
+
* declaration could enter it.
|
|
1674
|
+
*
|
|
1675
|
+
* **Example** (Finding a state with no incoming transition)
|
|
1676
|
+
*
|
|
1677
|
+
* ```ts import.meta.vitest
|
|
1678
|
+
* import { Machine } from "@typeonce/effect-machine"
|
|
1679
|
+
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
1680
|
+
*
|
|
1681
|
+
* const machine = Machine.make({
|
|
1682
|
+
* root: Machine.state({ states: { Idle: {}, Orphan: {} } }),
|
|
1683
|
+
* events: Machine.events({ Start: {} })
|
|
1684
|
+
* }).handle({
|
|
1685
|
+
* initial: { target: "Idle" },
|
|
1686
|
+
* states: { Idle: { on: { Start: { target: "Idle" } } } }
|
|
1687
|
+
* })
|
|
1688
|
+
*
|
|
1689
|
+
* MachineTest.analyze(machine).map((finding) => finding.law) // => ["topology.noPathFromInitial"]
|
|
1690
|
+
* ```
|
|
1691
|
+
*
|
|
1692
|
+
* @category verification
|
|
1693
|
+
* @since 0.47.0
|
|
1694
|
+
*/
|
|
1695
|
+
export declare const analyze: <M extends AnyMachine>(machine: M) => ReadonlyArray<AnalysisFinding>;
|
|
1696
|
+
/**
|
|
1697
|
+
* The events and optional input that reproduce a finding from startup.
|
|
1698
|
+
*
|
|
1699
|
+
* @category models
|
|
1700
|
+
* @since 0.47.0
|
|
1701
|
+
*/
|
|
1702
|
+
export interface CheckReplay {
|
|
1703
|
+
readonly input?: unknown;
|
|
1704
|
+
readonly events: ReadonlyArray<unknown>;
|
|
1705
|
+
}
|
|
1706
|
+
/**
|
|
1707
|
+
* Why {@link check} could not prove it explored every behavior. An empty list
|
|
1708
|
+
* is what makes a report `"exhaustive"`.
|
|
1709
|
+
*
|
|
1710
|
+
* @category models
|
|
1711
|
+
* @since 0.47.0
|
|
1712
|
+
*/
|
|
1713
|
+
export type CheckExplorationLimit =
|
|
1714
|
+
/** The depth, state, or transition limit stopped exploration early. */
|
|
1715
|
+
"depth" | "states" | "transitions"
|
|
1716
|
+
/** Event payloads were sampled from their schemas. */
|
|
1717
|
+
| "sampledEvents"
|
|
1718
|
+
/** Machine input was sampled from its schema. */
|
|
1719
|
+
| "sampledInput"
|
|
1720
|
+
/** Exploration used caller-supplied events. */
|
|
1721
|
+
| "customEvents"
|
|
1722
|
+
/** A caller-supplied state key may merge distinct snapshots. */
|
|
1723
|
+
| "customStateKey"
|
|
1724
|
+
/** Invocations do not run during planning, so their outcomes were not explored. */
|
|
1725
|
+
| "invocations";
|
|
1726
|
+
/**
|
|
1727
|
+
* A problem found by {@link check}. An `"error"` finding fails the report and a
|
|
1728
|
+
* `"warning"` does not.
|
|
1729
|
+
*
|
|
1730
|
+
* @category models
|
|
1731
|
+
* @since 0.47.0
|
|
1732
|
+
*/
|
|
1733
|
+
export type CheckFinding = AnalysisFinding | {
|
|
1734
|
+
readonly law: "schema.underivable";
|
|
1735
|
+
readonly severity: "error";
|
|
1736
|
+
readonly message: string;
|
|
1737
|
+
} | {
|
|
1738
|
+
readonly law: "planning.failed";
|
|
1739
|
+
readonly severity: "error";
|
|
1740
|
+
readonly replay: CheckReplay;
|
|
1741
|
+
readonly message: string;
|
|
1742
|
+
} | {
|
|
1743
|
+
readonly law: "planning.deterministic";
|
|
1744
|
+
readonly severity: "error";
|
|
1745
|
+
/** `"initial"`, or the active state paths and event tag that were planned. */
|
|
1746
|
+
readonly source: string;
|
|
1747
|
+
readonly replay: CheckReplay;
|
|
1748
|
+
readonly message: string;
|
|
1749
|
+
} | {
|
|
1750
|
+
readonly law: "planning.verification";
|
|
1751
|
+
readonly severity: "error";
|
|
1752
|
+
readonly verificationLaw: VerificationLaw;
|
|
1753
|
+
readonly replay: CheckReplay;
|
|
1754
|
+
readonly message: string;
|
|
1755
|
+
} | {
|
|
1756
|
+
readonly law: "invariant.violated";
|
|
1757
|
+
readonly severity: "error";
|
|
1758
|
+
readonly invariant: string;
|
|
1759
|
+
readonly replay: CheckReplay;
|
|
1760
|
+
readonly message: string;
|
|
1761
|
+
} | {
|
|
1762
|
+
readonly law: "transition.notCovered";
|
|
1763
|
+
readonly severity: "warning";
|
|
1764
|
+
readonly source: string;
|
|
1765
|
+
readonly trigger: Machine.Machine.TransitionTrigger;
|
|
1766
|
+
readonly target: string | undefined;
|
|
1767
|
+
readonly branchKey?: string;
|
|
1768
|
+
readonly message: string;
|
|
1769
|
+
};
|
|
1770
|
+
/**
|
|
1771
|
+
* The result of {@link check}.
|
|
1772
|
+
*
|
|
1773
|
+
* @category models
|
|
1774
|
+
* @since 0.47.0
|
|
1775
|
+
*/
|
|
1776
|
+
export interface CheckReport {
|
|
1777
|
+
readonly machine: string;
|
|
1778
|
+
/** Whether the report has no `"error"` finding. */
|
|
1779
|
+
readonly ok: boolean;
|
|
1780
|
+
/**
|
|
1781
|
+
* `"exhaustive"` only when `limits` is empty. A `"bounded"` pass means nothing
|
|
1782
|
+
* was found within what was explored, not that the machine is correct.
|
|
1783
|
+
*/
|
|
1784
|
+
readonly exploration: "exhaustive" | "bounded";
|
|
1785
|
+
readonly limits: ReadonlyArray<CheckExplorationLimit>;
|
|
1786
|
+
readonly stats: {
|
|
1787
|
+
readonly states: number;
|
|
1788
|
+
readonly plannedTransitions: number;
|
|
1789
|
+
readonly transitions: {
|
|
1790
|
+
readonly total: number;
|
|
1791
|
+
readonly hit: number;
|
|
1792
|
+
};
|
|
1793
|
+
readonly branches: {
|
|
1794
|
+
readonly total: number;
|
|
1795
|
+
readonly hit: number;
|
|
1796
|
+
};
|
|
1797
|
+
};
|
|
1798
|
+
readonly findings: ReadonlyArray<CheckFinding>;
|
|
1799
|
+
}
|
|
1800
|
+
/**
|
|
1801
|
+
* Options for {@link check}. Every option has a default derived from the machine.
|
|
1802
|
+
*
|
|
1803
|
+
* @category models
|
|
1804
|
+
* @since 0.47.0
|
|
1805
|
+
*/
|
|
1806
|
+
export interface CheckOptions<M extends AnyMachine> {
|
|
1807
|
+
/** Machine input. Defaults to a value sampled from the input schema. */
|
|
1808
|
+
readonly input?: InputValue<M>;
|
|
1809
|
+
/** Events to try in every state. Defaults to samples derived from the event schemas. */
|
|
1810
|
+
readonly events?: ReadonlyArray<Machine.Machine.EventInput<Machine.Machine.InputEvent<M>>>;
|
|
1811
|
+
/** Distinct values to sample for each event tag. Defaults to 3. */
|
|
1812
|
+
readonly eventSamples?: number;
|
|
1813
|
+
/** Seed for sampled input and events. Defaults to 0, so reports are reproducible. */
|
|
1814
|
+
readonly seed?: number;
|
|
1815
|
+
/** Identifies logical states. Defaults to the complete snapshot value. */
|
|
1816
|
+
readonly stateKey?: (context: ExplorationStateContext<M>) => ExplorationKey;
|
|
1817
|
+
/** Defaults to `maxDepth: 16`, `maxStates: 256`, and `maxTransitions: 2048`. */
|
|
1818
|
+
readonly limits?: ExplorationLimits;
|
|
1819
|
+
/** Application laws checked against every explored state and step. */
|
|
1820
|
+
readonly invariants?: ReadonlyArray<Invariant<M>>;
|
|
1821
|
+
}
|
|
1822
|
+
/**
|
|
1823
|
+
* Checks a machine with one call and reports every problem as data.
|
|
1824
|
+
*
|
|
1825
|
+
* **When to use**
|
|
1826
|
+
*
|
|
1827
|
+
* Use as the first verification step after writing or changing a machine. Reach
|
|
1828
|
+
* for {@link explore}, {@link verify}, and {@link coverage} directly when a test
|
|
1829
|
+
* needs their complete evidence, such as a specific scenario.
|
|
1830
|
+
*
|
|
1831
|
+
* **Details**
|
|
1832
|
+
*
|
|
1833
|
+
* Runs {@link analyze}, then {@link explore} with events derived from the event
|
|
1834
|
+
* schemas, then {@link verify} on every explored transition. It also plans each
|
|
1835
|
+
* transition a second time, so nondeterminism is found even when hidden behind a
|
|
1836
|
+
* helper. Declared branches that were never taken are reported as warnings.
|
|
1837
|
+
*
|
|
1838
|
+
* Findings are returned rather than failed, so the Effect only fails on
|
|
1839
|
+
* interruption. Each finding that came from running the machine includes the
|
|
1840
|
+
* events that reproduce it.
|
|
1841
|
+
*
|
|
1842
|
+
* **Gotchas**
|
|
1843
|
+
*
|
|
1844
|
+
* A passing `"bounded"` report means nothing was found in what was explored. It
|
|
1845
|
+
* is not a proof. Invocations do not run during planning, so test them with
|
|
1846
|
+
* {@link probe} on a started machine.
|
|
1847
|
+
*
|
|
1848
|
+
* **Example** (Finding nondeterministic planning)
|
|
1849
|
+
*
|
|
1850
|
+
* ```ts import.meta.vitest
|
|
1851
|
+
* import { Machine } from "@typeonce/effect-machine"
|
|
1852
|
+
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
1853
|
+
* import { Effect, Schema } from "effect"
|
|
1854
|
+
*
|
|
1855
|
+
* const nextAmount = () => Math.random()
|
|
1856
|
+
* const machine = Machine.make({
|
|
1857
|
+
* root: Machine.state({
|
|
1858
|
+
* states: { Editing: {}, Charging: { fields: { amount: Schema.Number } } }
|
|
1859
|
+
* }),
|
|
1860
|
+
* events: Machine.events({ Pay: {} })
|
|
1861
|
+
* }).handle({
|
|
1862
|
+
* initial: { target: "Editing" },
|
|
1863
|
+
* states: {
|
|
1864
|
+
* Editing: { on: { Pay: { target: "Charging", data: () => ({ amount: nextAmount() }) } } }
|
|
1865
|
+
* }
|
|
1866
|
+
* })
|
|
1867
|
+
*
|
|
1868
|
+
* const report = await Effect.runPromise(MachineTest.check(machine))
|
|
1869
|
+
*
|
|
1870
|
+
* report.ok // => false
|
|
1871
|
+
* report.findings.map((finding) => finding.law) // => ["planning.deterministic"]
|
|
1872
|
+
* ```
|
|
1873
|
+
*
|
|
1874
|
+
* @category verification
|
|
1875
|
+
* @since 0.47.0
|
|
1876
|
+
*/
|
|
1877
|
+
export declare const check: <M extends AnyMachine>(machine: RootReadyMachine<M>, options?: CheckOptions<M>) => Effect.Effect<CheckReport, never, RunServices<M>>;
|
|
1878
|
+
/**
|
|
1879
|
+
* Formats a report as line-oriented text for a terminal or an agent transcript.
|
|
1880
|
+
*
|
|
1881
|
+
* @category verification
|
|
1882
|
+
* @since 0.47.0
|
|
1883
|
+
*/
|
|
1884
|
+
export declare const formatCheckReport: (report: CheckReport) => string;
|
|
1675
1885
|
/**
|
|
1676
1886
|
* Formats a trace as deterministic, line-oriented counterexample evidence.
|
|
1677
1887
|
*
|