@typeonce/effect-machine 0.31.2 → 0.33.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 (151) hide show
  1. package/README.md +168 -162
  2. package/dist/Machine.d.ts +495 -284
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +91 -111
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/atom.d.ts +3 -4
  7. package/dist/internal/machine/atom.d.ts.map +1 -1
  8. package/dist/internal/machine/atom.js +0 -2
  9. package/dist/internal/machine/atom.js.map +1 -1
  10. package/dist/internal/machine/childRegistry.d.ts +52 -0
  11. package/dist/internal/machine/childRegistry.d.ts.map +1 -0
  12. package/dist/internal/machine/childRegistry.js +74 -0
  13. package/dist/internal/machine/childRegistry.js.map +1 -0
  14. package/dist/internal/machine/cluster.d.ts +2 -3
  15. package/dist/internal/machine/cluster.d.ts.map +1 -1
  16. package/dist/internal/machine/cluster.js.map +1 -1
  17. package/dist/internal/machine/configuration.d.ts.map +1 -1
  18. package/dist/internal/machine/configuration.js +14 -4
  19. package/dist/internal/machine/configuration.js.map +1 -1
  20. package/dist/internal/machine/executionPlan.d.ts +1 -0
  21. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  22. package/dist/internal/machine/executionPlan.js +97 -53
  23. package/dist/internal/machine/executionPlan.js.map +1 -1
  24. package/dist/internal/machine/invocation.d.ts.map +1 -1
  25. package/dist/internal/machine/invocation.js +4 -2
  26. package/dist/internal/machine/invocation.js.map +1 -1
  27. package/dist/internal/machine/invocationDefinition.d.ts +35 -0
  28. package/dist/internal/machine/invocationDefinition.d.ts.map +1 -0
  29. package/dist/internal/machine/invocationDefinition.js +21 -0
  30. package/dist/internal/machine/invocationDefinition.js.map +1 -0
  31. package/dist/internal/machine/invocationEvent.d.ts +0 -2
  32. package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
  33. package/dist/internal/machine/invocationEvent.js +0 -8
  34. package/dist/internal/machine/invocationEvent.js.map +1 -1
  35. package/dist/internal/machine/machine.d.ts +21 -43
  36. package/dist/internal/machine/machine.d.ts.map +1 -1
  37. package/dist/internal/machine/machine.js +134 -403
  38. package/dist/internal/machine/machine.js.map +1 -1
  39. package/dist/internal/machine/planner.d.ts +13 -19
  40. package/dist/internal/machine/planner.d.ts.map +1 -1
  41. package/dist/internal/machine/planner.js +63 -35
  42. package/dist/internal/machine/planner.js.map +1 -1
  43. package/dist/internal/machine/process.d.ts +6 -8
  44. package/dist/internal/machine/process.d.ts.map +1 -1
  45. package/dist/internal/machine/process.js +1 -0
  46. package/dist/internal/machine/process.js.map +1 -1
  47. package/dist/internal/machine/protocol.d.ts +2 -2
  48. package/dist/internal/machine/protocol.d.ts.map +1 -1
  49. package/dist/internal/machine/protocol.js +4 -18
  50. package/dist/internal/machine/protocol.js.map +1 -1
  51. package/dist/internal/machine/readiness.d.ts +1 -1
  52. package/dist/internal/machine/readiness.d.ts.map +1 -1
  53. package/dist/internal/machine/requirements.d.ts +4 -0
  54. package/dist/internal/machine/requirements.d.ts.map +1 -0
  55. package/dist/internal/machine/requirements.js +2 -0
  56. package/dist/internal/machine/requirements.js.map +1 -0
  57. package/dist/internal/machine/runtime.d.ts +4 -58
  58. package/dist/internal/machine/runtime.d.ts.map +1 -1
  59. package/dist/internal/machine/runtime.js +1 -69
  60. package/dist/internal/machine/runtime.js.map +1 -1
  61. package/dist/internal/machine/serialization.d.ts.map +1 -1
  62. package/dist/internal/machine/serialization.js +16 -0
  63. package/dist/internal/machine/serialization.js.map +1 -1
  64. package/dist/internal/machine/stateDefinition.d.ts +3 -1
  65. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  66. package/dist/internal/machine/stateDefinition.js +36 -2
  67. package/dist/internal/machine/stateDefinition.js.map +1 -1
  68. package/dist/internal/machine/targetBuilder.d.ts +16 -0
  69. package/dist/internal/machine/targetBuilder.d.ts.map +1 -0
  70. package/dist/internal/machine/targetBuilder.js +333 -0
  71. package/dist/internal/machine/targetBuilder.js.map +1 -0
  72. package/dist/internal/machine/topology.js +3 -3
  73. package/dist/internal/machine/topology.js.map +1 -1
  74. package/dist/internal/machine/transition.d.ts +16 -0
  75. package/dist/internal/machine/transition.d.ts.map +1 -0
  76. package/dist/internal/machine/transition.js +2 -0
  77. package/dist/internal/machine/transition.js.map +1 -0
  78. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  79. package/dist/internal/testing/machine/finiteModel.js +11 -13
  80. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  81. package/dist/internal/testing/machine/format.d.ts +7 -0
  82. package/dist/internal/testing/machine/format.d.ts.map +1 -0
  83. package/dist/internal/testing/machine/format.js +104 -0
  84. package/dist/internal/testing/machine/format.js.map +1 -0
  85. package/dist/internal/testing/machine/probe.d.ts +1 -1
  86. package/dist/internal/testing/machine/probe.d.ts.map +1 -1
  87. package/dist/internal/testing/machine/probe.js +1 -1
  88. package/dist/internal/testing/machine/probe.js.map +1 -1
  89. package/dist/internal/testing/machine/referenceModel.d.ts.map +1 -1
  90. package/dist/internal/testing/machine/referenceModel.js +15 -19
  91. package/dist/internal/testing/machine/referenceModel.js.map +1 -1
  92. package/dist/internal/testing/machine/trace.d.ts +1 -1
  93. package/dist/internal/testing/machine/trace.d.ts.map +1 -1
  94. package/dist/internal/testing/machine/trace.js +3 -3
  95. package/dist/internal/testing/machine/trace.js.map +1 -1
  96. package/dist/internal/testing/machine/value.d.ts +8 -0
  97. package/dist/internal/testing/machine/value.d.ts.map +1 -0
  98. package/dist/internal/testing/machine/value.js +82 -0
  99. package/dist/internal/testing/machine/value.js.map +1 -0
  100. package/dist/internal/testing/machine/verification.d.ts +2 -13
  101. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  102. package/dist/internal/testing/machine/verification.js +21 -113
  103. package/dist/internal/testing/machine/verification.js.map +1 -1
  104. package/dist/testing/MachineTest.d.ts +89 -104
  105. package/dist/testing/MachineTest.d.ts.map +1 -1
  106. package/dist/testing/MachineTest.js +70 -84
  107. package/dist/testing/MachineTest.js.map +1 -1
  108. package/dist/unstable/cluster/ClusterMachine.d.ts +10 -13
  109. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  110. package/dist/unstable/cluster/ClusterMachine.js +5 -8
  111. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  112. package/dist/unstable/reactivity/AtomMachine.d.ts +26 -61
  113. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  114. package/dist/unstable/reactivity/AtomMachine.js +17 -26
  115. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  116. package/docs/agent-guide.md +45 -45
  117. package/docs/effect-atom-react.md +35 -63
  118. package/docs/machine-review.md +7 -7
  119. package/docs/root-api.md +259 -0
  120. package/package.json +1 -1
  121. package/src/Machine.ts +1053 -538
  122. package/src/internal/machine/atom.ts +3 -13
  123. package/src/internal/machine/childRegistry.ts +141 -0
  124. package/src/internal/machine/cluster.ts +5 -14
  125. package/src/internal/machine/configuration.ts +20 -4
  126. package/src/internal/machine/executionPlan.ts +109 -52
  127. package/src/internal/machine/invocation.ts +10 -6
  128. package/src/internal/machine/invocationDefinition.ts +52 -0
  129. package/src/internal/machine/invocationEvent.ts +0 -8
  130. package/src/internal/machine/machine.ts +222 -716
  131. package/src/internal/machine/planner.ts +74 -62
  132. package/src/internal/machine/process.ts +14 -21
  133. package/src/internal/machine/protocol.ts +9 -32
  134. package/src/internal/machine/readiness.ts +1 -0
  135. package/src/internal/machine/requirements.ts +11 -0
  136. package/src/internal/machine/runtime.ts +17 -186
  137. package/src/internal/machine/serialization.ts +22 -0
  138. package/src/internal/machine/stateDefinition.ts +36 -3
  139. package/src/internal/machine/targetBuilder.ts +525 -0
  140. package/src/internal/machine/topology.ts +3 -3
  141. package/src/internal/machine/transition.ts +26 -0
  142. package/src/internal/testing/machine/finiteModel.ts +23 -24
  143. package/src/internal/testing/machine/format.ts +120 -0
  144. package/src/internal/testing/machine/probe.ts +4 -4
  145. package/src/internal/testing/machine/referenceModel.ts +25 -17
  146. package/src/internal/testing/machine/trace.ts +4 -4
  147. package/src/internal/testing/machine/value.ts +77 -0
  148. package/src/internal/testing/machine/verification.ts +904 -1109
  149. package/src/testing/MachineTest.ts +103 -114
  150. package/src/unstable/cluster/ClusterMachine.ts +10 -20
  151. package/src/unstable/reactivity/AtomMachine.ts +42 -79
package/src/Machine.ts CHANGED
@@ -27,6 +27,7 @@ import type {
27
27
  import * as internal from "./internal/machine/machine.js"
28
28
  import { InitialEventTypeId } from "./internal/machine/machine.js"
29
29
  import type { EnsureExecutable } from "./internal/machine/readiness.js"
30
+ import type { ExcludeCompatibleRuntime } from "./internal/machine/requirements.js"
30
31
  import type * as internalRuntime from "./internal/machine/runtime.js"
31
32
  import type * as StateDefinition from "./internal/machine/stateDefinition.js"
32
33
  import type * as Topology from "./internal/machine/topology.js"
@@ -161,6 +162,9 @@ export interface Machine<
161
162
  */
162
163
  readonly states: States
163
164
 
165
+ /** Root descriptor for topology and snapshot access. */
166
+ readonly root: State<Extract<Omit<States[""], "~effect/Machine/ExplicitInitial">, Machine.StateNodeConfig>>
167
+
164
168
  /**
165
169
  * Events accepted through public machine input boundaries.
166
170
  *
@@ -281,15 +285,15 @@ export interface Definition<
281
285
  * **Example**
282
286
  *
283
287
  * ```ts
284
- * const counter = definition.handle({
288
+ * const counter = definition.handle({ states: {
285
289
  * Count: {
286
290
  * on: {
287
291
  * Increment: (to) =>
288
- * to.full.Count().resolve(({ event, state, target }) =>
292
+ * to.branch.Count().resolve(({ event, state, target }) =>
289
293
  * target.decoded(new Count({ value: state.value + event.by })))
290
294
  * }
291
295
  * }
292
- * })
296
+ * } })
293
297
  * ```
294
298
  *
295
299
  * @since 0.4.0
@@ -631,14 +635,6 @@ export declare namespace Runtime {
631
635
  }
632
636
  }
633
637
 
634
- type ExcludeCompatibleRuntime<Requirements, Events, Emits> = Requirements extends Runtime.Requirement<
635
- infer RequiredEvents,
636
- infer RequiredEmits
637
- > ? IsAny<Requirements> extends true ? Requirements
638
- : [RequiredEvents] extends [Events] ? [RequiredEmits] extends [Emits] ? never : Requirements
639
- : Requirements
640
- : Requirements
641
-
642
638
  type IncompatibleRuntime<Requirements, Events, Emits> = Requirements extends Runtime.Requirement<
643
639
  infer RequiredEvents,
644
640
  infer RequiredEmits
@@ -842,29 +838,6 @@ type ValidateStateNodeWithoutChildren<Node extends Machine.StateNodeConfig> = "i
842
838
  : "output" extends keyof Node ? StateDefinitionError<"Only final and parallel states can declare output">
843
839
  : unknown
844
840
 
845
- type DefineStateTreeInput<States extends Machine.StateSchemas> = {
846
- readonly [Key in keyof States]: DefineStateNodeInput<States[Key]>
847
- }
848
-
849
- type DefineStateNodeInput<Node> = Node extends Machine.TaggedSchema ? Node
850
- : Node extends Machine.HistoryStateNodeConfig ? Machine.HistoryStateNodeConfig
851
- : Node extends Machine.ChoiceStateNodeConfig ? Machine.ChoiceStateNodeConfig
852
- : Node extends { readonly type: "parallel"; readonly states: infer Children extends Machine.StateSchemas } ?
853
- Omit<Node, "states"> & { readonly states: DefineStateTreeInput<Children> }
854
- : Node extends { readonly states: infer Children extends Machine.StateSchemas } ? Omit<Node, "initial" | "states"> & {
855
- readonly initial: ActiveStateKey<Children> | ChoiceStateKey<Children>
856
- readonly states: DefineStateTreeInput<Children>
857
- }
858
- : Node
859
-
860
- type ValidateDefinedStates<States extends Machine.StateSchemas> = [States] extends
861
- [Machine.ValidateStateSchemas<States>] ? []
862
- : [validation: Machine.ValidateStateSchemas<States>]
863
-
864
- type InvalidDefinedStateTreeInput<States extends Machine.StateSchemas> = [States] extends
865
- [Machine.ValidateStateSchemas<States>] ? never
866
- : States & Machine.ValidateStateSchemas<States>
867
-
868
841
  type ReusableStateNodeConfig =
869
842
  | Machine.AtomicStateNodeConfig
870
843
  | Machine.CompoundStateNodeConfig
@@ -876,23 +849,73 @@ type ReusableStateValidation<Node> = Node extends ReusableStateNodeConfig ? Vali
876
849
  type ValidateDefinedState<Node> = [Node] extends [ReusableStateValidation<Node>] ? []
877
850
  : [validation: ReusableStateValidation<Node>]
878
851
 
879
- type InvalidDefinedStateInput<Node> = [Node] extends [ReusableStateValidation<Node>] ? never
880
- : Node & ReusableStateValidation<Node>
852
+ const StateTypeId = "~effect/Machine/State"
881
853
 
882
- interface StateConstructor {
883
- <const Node>(
884
- node: Node & DefineStateNodeInput<NoInfer<Node>>,
885
- ..._validation: ValidateDefinedState<NoInfer<Node>>
886
- ): Node
887
- <const Node>(node: InvalidDefinedStateInput<Node>): never
854
+ /** An immutable state descriptor, usable as a root or a nested state.
855
+ * @category models
856
+ * @since 0.32.0
857
+ */
858
+ export interface State<Node extends Machine.StateNodeConfig> extends Machine.StateAccessors<{ readonly "": Node }> {
859
+ readonly [StateTypeId]: typeof StateTypeId
860
+ readonly node: Node
861
+ }
862
+
863
+ type NormalizeState<Node, Tag extends string = ""> = Node extends
864
+ { readonly [StateTypeId]: typeof StateTypeId; readonly node: infer Config } ? Config
865
+ : Node extends Machine.TaggedSchema ? Node
866
+ : "fields" extends keyof Node ? NormalizeStateFields<Node, Tag>
867
+ : Node extends { readonly states: infer Children } ?
868
+ Children extends Readonly<Record<string, Machine.TaggedSchema>> ? Node : NormalizeStateFields<Node, Tag>
869
+ : Node
870
+
871
+ type NormalizeStateFields<Node, Tag extends string> = {
872
+ readonly [Key in keyof Node as Key extends "fields" ? "schema" : Key]: Key extends "fields" ?
873
+ Node[Key] extends Schema.Struct.Fields ? Schema.TaggedStruct<Tag, Node[Key]> : never
874
+ : Key extends "states" ?
875
+ { readonly [Child in keyof Node[Key]]: NormalizeState<Node[Key][Child], Extract<Child, string>> }
876
+ : Node[Key]
877
+ }
878
+
879
+ type NormalizedState<Node> = Extract<NormalizeState<Node>, Machine.StateNodeConfig>
880
+ type StateFieldsValidation<Node> = Node extends { readonly [StateTypeId]: typeof StateTypeId } | Machine.TaggedSchema ?
881
+ unknown :
882
+ & (Node extends { readonly fields: infer Fields } ?
883
+ "schema" extends keyof Node ? StateDefinitionError<"Declare fields or schema, not both", "">
884
+ : Fields extends Schema.Struct.Fields ?
885
+ "_tag" extends keyof Fields ? StateDefinitionError<"State fields cannot declare _tag", ""> : unknown
886
+ : StateDefinitionError<"State fields must be schema fields", "">
887
+ : unknown)
888
+ & (Node extends { readonly states: infer Children } ? {
889
+ readonly states: {
890
+ readonly [Key in keyof Children]: StateFieldsValidation<Children[Key]>
891
+ }
892
+ } :
893
+ unknown)
894
+
895
+ interface StateInput {
896
+ readonly schema?: Machine.TaggedSchema
897
+ readonly fields?: Schema.Struct.Fields
898
+ readonly type?: "active" | "parallel" | "final" | "history" | "choice"
899
+ readonly initial?: string
900
+ readonly states?: Readonly<
901
+ Record<
902
+ string,
903
+ Machine.TaggedSchema | StateInput | {
904
+ readonly [StateTypeId]: typeof StateTypeId
905
+ readonly node: Machine.StateNodeConfig
906
+ }
907
+ >
908
+ >
909
+ readonly history?: "shallow" | "deep"
910
+ readonly output?: Schema.Top
911
+ readonly annotations?: Machine.SchemaLessStateAnnotations
888
912
  }
889
913
 
890
- interface StatesConstructor {
891
- <const States extends Machine.StateSchemas>(
892
- states: States & DefineStateTreeInput<NoInfer<States>>,
893
- ..._validation: ValidateDefinedStates<NoInfer<States>>
894
- ): Machine.DefinedStates<States>
895
- <const States extends Machine.StateSchemas>(states: InvalidDefinedStateTreeInput<States>): never
914
+ interface StateConstructor {
915
+ <const Node extends StateInput>(
916
+ node: Node & StateFieldsValidation<NoInfer<Node>>,
917
+ ..._validation: ValidateDefinedState<NormalizeState<NoInfer<Node>>>
918
+ ): State<NormalizedState<Node>>
896
919
  }
897
920
 
898
921
  type EventProtocolError<Message extends string, Tag extends PropertyKey = never> = {
@@ -919,12 +942,6 @@ type ValidateInputEventProtocol<
919
942
  > = [DuplicateInput] extends [never] ? unknown
920
943
  : EventProtocolError<"Public event tags must be unique", DuplicateInput>
921
944
 
922
- type ValidateEmittedEventProtocol<
923
- EmittedEvents extends ReadonlyArray<Machine.TaggedSchema>,
924
- DuplicateEmitted extends PropertyKey = DuplicateEventTag<EmittedEvents>
925
- > = [DuplicateEmitted] extends [never] ? unknown
926
- : EventProtocolError<"Emitted event tags must be unique", DuplicateEmitted>
927
-
928
945
  type ValidateInternalEventProtocol<
929
946
  InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
930
947
  InternalEvents extends ReadonlyArray<Machine.TaggedSchema>,
@@ -961,7 +978,7 @@ type FromCallable<Arguments extends ReadonlyArray<unknown>, Result> = Arguments
961
978
  : (...args: Arguments) => Result
962
979
  : (...args: Arguments) => Result
963
980
 
964
- type FromMethod<Arguments extends ReadonlyArray<unknown>, Result> = {
981
+ type FromMethod<in out Arguments extends ReadonlyArray<unknown>, out Result> = {
965
982
  /**
966
983
  * Constructs the selected state from its schema make input while the
967
984
  * machine plans the resulting configuration. The input may be omitted when
@@ -1231,33 +1248,28 @@ type InitialParallelBuilder<
1231
1248
  }
1232
1249
 
1233
1250
  type FullSnapshotBuilderWithPrefix<
1234
- States extends Machine.StateSchemas,
1235
- Prefix extends string = ""
1236
- > =
1237
- & {
1238
- readonly [Key in ActiveStateKey<States>]: FullSnapshotMethod<States, Key, Prefix>
1239
- }
1240
- & {
1241
- readonly [Key in ChoiceStateKey<States>]: () => Machine.ChoiceTargetInstruction<Machine.JoinPath<Prefix, Key>>
1242
- }
1251
+ in out States extends Machine.StateSchemas,
1252
+ in out Prefix extends string = ""
1253
+ > = {
1254
+ readonly [Key in ActiveStateKey<States> | ChoiceStateKey<States>]: Key extends ActiveStateKey<States>
1255
+ ? FullSnapshotMethod<States, Key, Prefix>
1256
+ : () => Machine.ChoiceTargetInstruction<Machine.JoinPath<Prefix, Key>>
1257
+ }
1243
1258
 
1244
1259
  type FullSnapshotMethod<
1245
- States extends Machine.StateSchemas,
1246
- StateId extends ActiveStateKey<States>,
1247
- Prefix extends string
1248
- > = Machine.NodeSchema<States[StateId]> extends never ? FromMethod<
1249
- FullSnapshotFromArguments<States, StateId, Prefix>,
1250
- FullSnapshotResult<States, StateId, Prefix>
1251
- >
1252
- :
1253
- & DecodedMethod<
1254
- FullSnapshotArguments<States, StateId, Prefix>,
1255
- FullSnapshotResult<States, StateId, Prefix>
1256
- >
1257
- & FromMethod<
1260
+ in out States extends Machine.StateSchemas,
1261
+ in out StateId extends ActiveStateKey<States>,
1262
+ in out Prefix extends string
1263
+ > = {
1264
+ readonly [
1265
+ /** Constructs this node using schema make input or an already decoded value. */
1266
+ Method in Machine.NodeSchema<States[StateId]> extends never ? "from" : "from" | "decoded"
1267
+ ]: Method extends "from" ? FromCallable<
1258
1268
  FullSnapshotFromArguments<States, StateId, Prefix>,
1259
- FullSnapshotResult<States, StateId, Prefix>
1269
+ Machine.StateConstruction<FullSnapshotResult<States, StateId, Prefix>>
1260
1270
  >
1271
+ : (...args: FullSnapshotArguments<States, StateId, Prefix>) => FullSnapshotResult<States, StateId, Prefix>
1272
+ }
1261
1273
 
1262
1274
  type FullSnapshotArguments<
1263
1275
  States extends Machine.StateSchemas,
@@ -1416,23 +1428,20 @@ type HistorySnapshotResult<
1416
1428
  : never
1417
1429
 
1418
1430
  type HistorySnapshotMethod<
1419
- States extends Machine.StateSchemas,
1420
- StateId extends ActiveStateKey<States>,
1421
- Prefix extends string,
1422
- Owner extends string
1423
- > = Machine.NodeSchema<States[StateId]> extends never ? FromMethod<
1424
- HistorySnapshotFromArguments<States, StateId, Prefix, Owner>,
1425
- HistorySnapshotResult<States, StateId, Prefix, Owner>
1426
- >
1427
- :
1428
- & DecodedMethod<
1429
- HistorySnapshotArguments<States, StateId, Prefix, Owner>,
1430
- HistorySnapshotResult<States, StateId, Prefix, Owner>
1431
- >
1432
- & FromMethod<
1431
+ in out States extends Machine.StateSchemas,
1432
+ in out StateId extends ActiveStateKey<States>,
1433
+ in out Prefix extends string,
1434
+ in out Owner extends string
1435
+ > = {
1436
+ readonly [Key in Machine.NodeSchema<States[StateId]> extends never ? "from" : "from" | "decoded"]: Key extends
1437
+ "from" ? FromCallable<
1433
1438
  HistorySnapshotFromArguments<States, StateId, Prefix, Owner>,
1434
- HistorySnapshotResult<States, StateId, Prefix, Owner>
1435
- >
1439
+ Machine.StateConstruction<HistorySnapshotResult<States, StateId, Prefix, Owner>>
1440
+ > :
1441
+ (
1442
+ ...args: HistorySnapshotArguments<States, StateId, Prefix, Owner>
1443
+ ) => HistorySnapshotResult<States, StateId, Prefix, Owner>
1444
+ }
1436
1445
 
1437
1446
  type HistorySnapshotWithPrefix<
1438
1447
  States extends Machine.StateSchemas,
@@ -1441,7 +1450,8 @@ type HistorySnapshotWithPrefix<
1441
1450
  > = {
1442
1451
  readonly [Key in ActiveStateKey<States>]: Owner extends
1443
1452
  | Machine.JoinPath<Prefix, Key>
1444
- | `${Machine.JoinPath<Prefix, Key>}.${string}` ? HistorySnapshotResult<States, Key, Prefix, Owner>
1453
+ | (Machine.JoinPath<Prefix, Key> extends "" ? string : `${Machine.JoinPath<Prefix, Key>}.${string}`) ?
1454
+ HistorySnapshotResult<States, Key, Prefix, Owner>
1445
1455
  : never
1446
1456
  }[ActiveStateKey<States>]
1447
1457
 
@@ -1453,7 +1463,7 @@ type HistorySnapshotBuilderWithPrefix<
1453
1463
  readonly [
1454
1464
  Key in ActiveStateKey<States> as Owner extends
1455
1465
  | Machine.JoinPath<Prefix, Key>
1456
- | `${Machine.JoinPath<Prefix, Key>}.${string}` ? Key
1466
+ | (Machine.JoinPath<Prefix, Key> extends "" ? string : `${Machine.JoinPath<Prefix, Key>}.${string}`) ? Key
1457
1467
  : never
1458
1468
  ]: HistorySnapshotMethod<States, Key, Prefix, Owner>
1459
1469
  }
@@ -1465,7 +1475,8 @@ type HistorySnapshotRegions<
1465
1475
  > = {
1466
1476
  readonly [Key in ActiveStateKey<States>]: Owner extends
1467
1477
  | Machine.JoinPath<Prefix, Key>
1468
- | `${Machine.JoinPath<Prefix, Key>}.${string}` ? HistorySnapshotResult<States, Key, Prefix, Owner>
1478
+ | (Machine.JoinPath<Prefix, Key> extends "" ? string : `${Machine.JoinPath<Prefix, Key>}.${string}`) ?
1479
+ HistorySnapshotResult<States, Key, Prefix, Owner>
1469
1480
  : FullSnapshotResult<States, Key, Prefix>
1470
1481
  }
1471
1482
 
@@ -1481,7 +1492,8 @@ type HistoryParallelBuilder<
1481
1492
  & {
1482
1493
  readonly [Key in Remaining]: Owner extends
1483
1494
  | Machine.JoinPath<Prefix, Key>
1484
- | `${Machine.JoinPath<Prefix, Key>}.${string}` ? NodeBuilderMethod<
1495
+ | (Machine.JoinPath<Prefix, Key> extends "" ? string : `${Machine.JoinPath<Prefix, Key>}.${string}`) ?
1496
+ NodeBuilderMethod<
1485
1497
  States[Key],
1486
1498
  HistorySnapshotArguments<States, Key, Prefix, Owner>,
1487
1499
  HistoryParallelBuilder<
@@ -1525,9 +1537,7 @@ type HistoryParallelBuilder<
1525
1537
  >
1526
1538
  }
1527
1539
 
1528
- type ParentPath<Path extends string> = Path extends `${infer Parent}.${infer Child}`
1529
- ? Child extends `${string}.${string}` ? `${Parent}.${ParentPath<Child>}` : Parent
1530
- : never
1540
+ type ParentPath<Path extends string> = Machine.ImmediateParentStateIdentifier<Path>
1531
1541
 
1532
1542
  type IsCompoundNode<Node> = Node extends { readonly type: "parallel" } ? false
1533
1543
  : Node extends { readonly states: Machine.StateSchemas } ? true
@@ -1607,7 +1617,7 @@ type LocalTargetMethod<
1607
1617
  Path extends string = Machine.JoinPath<Prefix, StateId>
1608
1618
  > = States[StateId] extends infer Node ?
1609
1619
  Node extends { readonly type: "parallel"; readonly states: infer Children extends Machine.StateSchemas } ?
1610
- Source extends Path | `${Path}.${string}` ? NestedTargetMethod<
1620
+ Source extends Path | (Path extends "" ? string : `${Path}.${string}`) ? NestedTargetMethod<
1611
1621
  Node,
1612
1622
  LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
1613
1623
  LocalTargetResultWithPrefix<AllStates, Children, Path>,
@@ -1722,7 +1732,7 @@ type BranchTargetMethod<
1722
1732
  Path extends string = Machine.JoinPath<Prefix, StateId>
1723
1733
  > = States[StateId] extends infer Node ?
1724
1734
  Node extends { readonly type: "parallel"; readonly states: infer Children extends Machine.StateSchemas } ?
1725
- Source extends Path | `${Path}.${string}` ?
1735
+ Source extends Path | (Path extends "" ? string : `${Path}.${string}`) ?
1726
1736
  & NestedTargetMethod<
1727
1737
  Node,
1728
1738
  BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
@@ -1795,20 +1805,26 @@ type HasDirectShallowHistory<States extends Machine.StateSchemas> = {
1795
1805
  readonly [Key in HistoryStateKey<States>]: States[Key] extends { readonly history: "deep" } ? never : Key
1796
1806
  }[HistoryStateKey<States>] extends never ? false : true
1797
1807
 
1798
- type HasValuedActiveChild<States extends Machine.StateSchemas> = {
1799
- readonly [Key in ActiveStateKey<States>]: Machine.NodeSchema<States[Key]> extends never ? never : Key
1808
+ type RequiresInitialValue<Node> = Machine.NodeSchema<Node> extends never ? false
1809
+ : {} extends NodeMakeInput<Node> ? false
1810
+ : true
1811
+
1812
+ type HasRequiredActiveChild<States extends Machine.StateSchemas> = {
1813
+ readonly [Key in ActiveStateKey<States>]: RequiresInitialValue<States[Key]> extends true ? Key : never
1800
1814
  }[ActiveStateKey<States>] extends never ? false : true
1801
1815
 
1802
1816
  type InitializerClosureForNode<
1803
1817
  AllStates extends Machine.StateSchemas,
1804
1818
  Node,
1805
1819
  Path extends string
1806
- > = Node extends { readonly type: "parallel"; readonly states: infer Children extends Machine.StateSchemas } ?
1807
- | (HasValuedActiveChild<Children> extends true ? Extract<Path, Machine.StateIdentifier<AllStates>> : never)
1808
- | InitializerClosuresForChildren<AllStates, Children, Path>
1820
+ > = Node extends { readonly states: infer Declared extends Machine.StateSchemas } ?
1821
+ string extends keyof Declared ? never :
1822
+ Node extends { readonly type: "parallel"; readonly states: infer Children extends Machine.StateSchemas } ?
1823
+ | (HasRequiredActiveChild<Children> extends true ? Extract<Path, Machine.StateIdentifier<AllStates>> : never)
1824
+ | InitializerClosuresForChildren<AllStates, Children, Path>
1809
1825
  : Node extends { readonly states: infer Children extends Machine.StateSchemas; readonly initial: infer Initial } ?
1810
- | (Initial extends ActiveStateKey<Children> ? Machine.NodeSchema<Children[Initial]> extends never ? never
1811
- : Extract<Path, Machine.StateIdentifier<AllStates>>
1826
+ | (Initial extends ActiveStateKey<Children> ?
1827
+ RequiresInitialValue<Children[Initial]> extends true ? Extract<Path, Machine.StateIdentifier<AllStates>> : never
1812
1828
  : never)
1813
1829
  | (Initial extends ActiveStateKey<Children> ? InitializerClosureForNode<
1814
1830
  AllStates,
@@ -1817,6 +1833,7 @@ type InitializerClosureForNode<
1817
1833
  >
1818
1834
  : never)
1819
1835
  : never
1836
+ : never
1820
1837
 
1821
1838
  type InitializerClosuresForChildren<
1822
1839
  AllStates extends Machine.StateSchemas,
@@ -2490,7 +2507,7 @@ export declare namespace ChildMachine {
2490
2507
  ExcludeCompatibleRuntime<
2491
2508
  Exclude<ExecutionServices<Machine.InitialServices<M> | Machine.Services<M>>, MachineRuntimeRequirement>,
2492
2509
  Machine.Event<M>,
2493
- Machine.Emit<M>
2510
+ Machine.EmittedEvent<M>
2494
2511
  >,
2495
2512
  Scope.Scope
2496
2513
  >
@@ -2728,6 +2745,7 @@ export declare namespace Machine {
2728
2745
  /** @internal */
2729
2746
  readonly [MachineTypeId]: TypeCarrier<any, any, any, any, any, any, any, any, any, any, any, any, any, any>
2730
2747
  readonly states: StateSchemas
2748
+ readonly root: Pick<State<Machine.StateNodeConfig>, typeof StateTypeId | "node">
2731
2749
  readonly events: EventProtocol.Any<"public">
2732
2750
  readonly internalEvents: EventProtocol.Any<"internal">
2733
2751
  readonly emittedEvents: EventProtocol.Any<"emitted">
@@ -2848,9 +2866,6 @@ export declare namespace Machine {
2848
2866
  */
2849
2867
  export type EmittedEvents<M extends Any> = M[typeof MachineTypeId]["emittedEvents"]
2850
2868
 
2851
- /** @deprecated Use {@link EmittedEvents}. */
2852
- export type Emits<M extends Any> = EmittedEvents<M>
2853
-
2854
2869
  /**
2855
2870
  * Extracts state paths with implemented output handlers.
2856
2871
  *
@@ -3037,10 +3052,7 @@ export declare namespace Machine {
3037
3052
  * @category utility types
3038
3053
  * @since 0.4.0
3039
3054
  */
3040
- export type EmittedEvent<M extends Any> = EmittedEventOf<Emits<M>>
3041
-
3042
- /** @deprecated Use {@link EmittedEvent}. */
3043
- export type Emit<M extends Any> = EmittedEvent<M>
3055
+ export type EmittedEvent<M extends Any> = EmittedEventOf<EmittedEvents<M>>
3044
3056
 
3045
3057
  /**
3046
3058
  * A schema whose decoded value contains a `_tag` discriminator.
@@ -3080,9 +3092,6 @@ export declare namespace Machine {
3080
3092
  "title" | "description" | "documentation"
3081
3093
  >
3082
3094
 
3083
- /** @deprecated Use {@link SchemaLessStateAnnotations}. */
3084
- export type PseudoStateAnnotations = SchemaLessStateAnnotations
3085
-
3086
3095
  /**
3087
3096
  * Configuration accepted for an atomic object state node.
3088
3097
  *
@@ -3278,21 +3287,16 @@ export declare namespace Machine {
3278
3287
  type InitialBuilder<States extends StateSchemas> = InitialSnapshotBuilderWithPrefix<States>
3279
3288
 
3280
3289
  /**
3281
- * State definitions and snapshot access helpers returned by `states`.
3290
+ * Typed snapshot access shared by state descriptors.
3282
3291
  *
3283
3292
  * **Details**
3284
3293
  *
3285
- * The `states` property is the original state tree and can be passed directly
3286
- * to `make`. The remaining helpers match and read snapshots for the same
3287
- * state tree.
3294
+ * The helpers match and read snapshots for the same captured topology.
3288
3295
  *
3289
3296
  * @category models
3290
3297
  * @since 0.4.0
3291
3298
  */
3292
- export interface DefinedStates<States extends StateSchemas> {
3293
- /** Captured state tree supplied to {@link states}. */
3294
- readonly states: States
3295
-
3299
+ export interface StateAccessors<States extends StateSchemas> {
3296
3300
  /**
3297
3301
  * Checks and preserves one active state path from this definition.
3298
3302
  *
@@ -3321,7 +3325,10 @@ export declare namespace Machine {
3321
3325
  const Path extends ValuedStateIdentifier<States>
3322
3326
  >(
3323
3327
  snapshot: SnapshotByIdentifier<States, From>,
3324
- path: Path & (Path extends NoInfer<From> | `${NoInfer<From>}.${string}` ? unknown : never)
3328
+ path:
3329
+ & Path
3330
+ & (Path extends NoInfer<From> | (NoInfer<From> extends "" ? string : `${NoInfer<From>}.${string}`) ? unknown
3331
+ : never)
3325
3332
  ): Option.Option<StateByIdentifier<States, Path>>
3326
3333
  }
3327
3334
 
@@ -3358,7 +3365,10 @@ export declare namespace Machine {
3358
3365
  const Path extends StateIdentifier<States>
3359
3366
  >(
3360
3367
  snapshot: SnapshotByIdentifier<States, From>,
3361
- path: Path & (Path extends NoInfer<From> | `${NoInfer<From>}.${string}` ? unknown : never)
3368
+ path:
3369
+ & Path
3370
+ & (Path extends NoInfer<From> | (NoInfer<From> extends "" ? string : `${NoInfer<From>}.${string}`) ? unknown
3371
+ : never)
3362
3372
  ): Option.Option<SnapshotByIdentifier<States, Path>>
3363
3373
  }
3364
3374
 
@@ -3380,7 +3390,10 @@ export declare namespace Machine {
3380
3390
  const Path extends StateIdentifier<States>
3381
3391
  >(
3382
3392
  snapshot: SnapshotByIdentifier<States, From>,
3383
- path: Path & (Path extends NoInfer<From> | `${NoInfer<From>}.${string}` ? unknown : never)
3393
+ path:
3394
+ & Path
3395
+ & (Path extends NoInfer<From> | (NoInfer<From> extends "" ? string : `${NoInfer<From>}.${string}`) ? unknown
3396
+ : never)
3384
3397
  ): boolean
3385
3398
  }
3386
3399
  }
@@ -3824,17 +3837,17 @@ export declare namespace Machine {
3824
3837
  export type EnsureChoiceImplementations<
3825
3838
  States extends StateSchemas,
3826
3839
  UnhandledStates extends StateIdentifier<States>
3827
- > = [ChoiceIdentifier<States>] extends [never] ? unknown
3828
- : [MissingChoiceImplementations<States, UnhandledStates>] extends [never] ? unknown
3829
- : {
3830
- readonly "~effect/Machine/MissingChoiceImplementation": MissingChoiceImplementations<States, UnhandledStates>
3831
- }
3840
+ > = IsAny<States> extends true ? unknown : [ChoiceIdentifier<States>] extends [never] ? unknown
3841
+ : [MissingChoiceImplementations<States, UnhandledStates>] extends [never] ? unknown
3842
+ : {
3843
+ readonly "~effect/Machine/MissingChoiceImplementation": MissingChoiceImplementations<States, UnhandledStates>
3844
+ }
3832
3845
 
3833
3846
  /** @internal Readiness proof required by planning and managed execution. */
3834
3847
  export type EnsureHistoryImplementations<
3835
3848
  States extends StateSchemas,
3836
3849
  UnhandledStates extends StateIdentifier<States>
3837
- > =
3850
+ > = IsAny<States> extends true ? unknown :
3838
3851
  & ([HistoryIdentifier<States>] extends [never] ? unknown
3839
3852
  : [MissingHistoryImplementations<States, UnhandledStates>] extends [never] ? unknown :
3840
3853
  {
@@ -3842,6 +3855,20 @@ export declare namespace Machine {
3842
3855
  })
3843
3856
  & EnsureChoiceImplementations<States, UnhandledStates>
3844
3857
 
3858
+ /** Owners whose declared default descendants require supplied values. */
3859
+ export type RequiredInitializers<States extends StateSchemas> = string extends keyof States ? never :
3860
+ "" extends keyof States ? States[""] extends { readonly "~effect/Machine/ExplicitInitial": true } ? never
3861
+ : InitializerClosureForNode<States, States[""], ""> :
3862
+ never
3863
+
3864
+ /** Proof that initialization can construct every required default value. */
3865
+ export type EnsureInitialImplementations<
3866
+ States extends StateSchemas,
3867
+ UnhandledStates extends StateIdentifier<States>
3868
+ > = [Extract<RequiredInitializers<States>, UnhandledStates>] extends [never] ? unknown : {
3869
+ readonly "~effect/Machine/MissingInitialImplementation": Extract<RequiredInitializers<States>, UnhandledStates>
3870
+ }
3871
+
3845
3872
  /**
3846
3873
  * Extracts a state-tree node by state path.
3847
3874
  *
@@ -3851,7 +3878,15 @@ export declare namespace Machine {
3851
3878
  export type NodeByIdentifier<
3852
3879
  States extends StateSchemas,
3853
3880
  StateId extends StateIdentifier<States>
3854
- > = StateId extends `${infer Head}.${infer Rest}`
3881
+ > = string extends keyof States ? NodeByNamedIdentifier<States, StateId>
3882
+ : "" extends keyof States ? StateId extends "" ? States[""]
3883
+ : States[""] extends { readonly states: infer Children extends StateSchemas }
3884
+ ? NodeByIdentifier<Children, Extract<StateId, StateIdentifier<Children>>> :
3885
+ never
3886
+ : NodeByNamedIdentifier<States, StateId>
3887
+
3888
+ type NodeByNamedIdentifier<States extends StateSchemas, StateId extends string> = StateId extends
3889
+ `${infer Head}.${infer Rest}`
3855
3890
  ? Head extends keyof States
3856
3891
  ? States[Head] extends { readonly states: infer Children extends StateSchemas }
3857
3892
  ? Rest extends StateIdentifier<Children> ? NodeByIdentifier<Children, Rest> : never
@@ -3899,9 +3934,6 @@ export declare namespace Machine {
3899
3934
  */
3900
3935
  export type EmittedEventOf<Emits extends ReadonlyArray<TaggedSchema>> = Emits[number]["Type"]
3901
3936
 
3902
- /** @deprecated Use {@link EmittedEventOf}. */
3903
- export type EmitOf<Emits extends ReadonlyArray<TaggedSchema>> = EmittedEventOf<Emits>
3904
-
3905
3937
  /**
3906
3938
  * Event values received by lifecycle callbacks.
3907
3939
  *
@@ -3927,8 +3959,11 @@ export declare namespace Machine {
3927
3959
  * @category utility types
3928
3960
  * @since 0.4.0
3929
3961
  */
3930
- export type ParentStateIdentifier<StateId extends string> = StateId extends `${infer Parent}.${infer Child}`
3931
- ? Parent | (Child extends `${string}.${string}` ? `${Parent}.${ParentStateIdentifier<Child>}` : never)
3962
+ export type ParentStateIdentifier<StateId extends string> = StateId extends "" ? never
3963
+ : "" | NamedParentStateIdentifier<StateId>
3964
+
3965
+ type NamedParentStateIdentifier<StateId extends string> = StateId extends `${infer Parent}.${infer Child}`
3966
+ ? Parent | (Child extends `${string}.${string}` ? `${Parent}.${NamedParentStateIdentifier<Child>}` : never)
3932
3967
  : never
3933
3968
 
3934
3969
  /**
@@ -3940,7 +3975,8 @@ export declare namespace Machine {
3940
3975
  export type ImmediateParentStateIdentifier<StateId extends string> = StateId extends `${infer Head}.${infer Tail}` ?
3941
3976
  Tail extends `${string}.${string}` ? `${Head}.${ImmediateParentStateIdentifier<Tail>}`
3942
3977
  : Head
3943
- : never
3978
+ : StateId extends "" ? never
3979
+ : ""
3944
3980
 
3945
3981
  /**
3946
3982
  * Maps every parent state path of a state identifier to its decoded value.
@@ -4023,7 +4059,7 @@ export declare namespace Machine {
4023
4059
  States extends StateSchemas,
4024
4060
  Children extends StateSchemas,
4025
4061
  Prefix extends StateIdentifier<States>
4026
- > = {
4062
+ > = Prefix extends "" ? TerminalOutput<Children> : {
4027
4063
  readonly [Key in ActiveStateKey<Children>]: DirectFinalCompletionOutput<
4028
4064
  States,
4029
4065
  Extract<JoinPath<Prefix, Key>, StateIdentifier<States>>
@@ -4194,6 +4230,7 @@ export declare namespace Machine {
4194
4230
  * @since 0.4.0
4195
4231
  */
4196
4232
  export interface EncodedSnapshot {
4233
+ readonly version: 2
4197
4234
  readonly _tag: "MachineSnapshot"
4198
4235
  readonly active: ReadonlyArray<EncodedSnapshotState>
4199
4236
  readonly completed?: ReadonlyArray<EncodedSnapshotCompletion>
@@ -4388,7 +4425,7 @@ export declare namespace Machine {
4388
4425
  * @category utility types
4389
4426
  * @since 0.4.0
4390
4427
  */
4391
- export type RootStateIdentifier<StateId extends string> = StateId extends `${infer Root}.${string}` ? Root : StateId
4428
+ export type RootStateIdentifier<StateId extends string> = StateId extends string ? "" : never
4392
4429
 
4393
4430
  /**
4394
4431
  * Extracts the public snapshot shape that contains a final state path.
@@ -4450,7 +4487,7 @@ export declare namespace Machine {
4450
4487
  * @category models
4451
4488
  * @since 0.4.0
4452
4489
  */
4453
- export interface StateConstruction<Result> {
4490
+ export interface StateConstruction<out Result> {
4454
4491
  readonly [Topology.StateConstructionTypeId]: Result
4455
4492
  }
4456
4493
 
@@ -4633,7 +4670,12 @@ export declare namespace Machine {
4633
4670
  export type HistoryDefaultTargetBuilder<
4634
4671
  States extends StateSchemas,
4635
4672
  Owner extends StateIdentifier<States>
4636
- > = HistorySnapshotBuilderWithPrefix<States, Owner>
4673
+ > = HistorySnapshotMethod<
4674
+ { readonly "": Extract<Omit<States[""], "~effect/Machine/ExplicitInitial">, StateNodeConfig> },
4675
+ "" & ActiveStateKey<{ readonly "": Extract<Omit<States[""], "~effect/Machine/ExplicitInitial">, StateNodeConfig> }>,
4676
+ "",
4677
+ Owner
4678
+ >
4637
4679
 
4638
4680
  /**
4639
4681
  * Builder for source-local transition targets.
@@ -4670,7 +4712,7 @@ export declare namespace Machine {
4670
4712
  Source extends StateNodeIdentifier<States>
4671
4713
  > = BranchTargetBuilderForRoot<
4672
4714
  States,
4673
- Extract<RootStateIdentifier<Source>, ActiveStateKey<States>>,
4715
+ Extract<"" extends keyof States ? "" : RootStateIdentifier<Source>, ActiveStateKey<States>>,
4674
4716
  Source
4675
4717
  >
4676
4718
 
@@ -4687,15 +4729,15 @@ export declare namespace Machine {
4687
4729
  * do not directly control state re-entry. Exit and entry paths are derived
4688
4730
  * from the previous and next active paths. Shared active ancestors remain
4689
4731
  * entered even when a `full` target supplies their values again. Use an event
4690
- * transition with `reenter: true` when the source should explicitly exit and
4732
+ * transition with `.reenter()` when the source should explicitly exit and
4691
4733
  * enter again.
4692
4734
  *
4693
4735
  * @category models
4694
4736
  * @since 0.4.0
4695
4737
  */
4696
4738
  export interface TargetBuilder<
4697
- States extends StateSchemas,
4698
- Source extends StateNodeIdentifier<States>
4739
+ in out States extends StateSchemas,
4740
+ in out Source extends StateNodeIdentifier<States>
4699
4741
  > {
4700
4742
  /**
4701
4743
  * Selects an explicitly targetless transition.
@@ -4827,16 +4869,15 @@ export declare namespace Machine {
4827
4869
  Node,
4828
4870
  Path extends string,
4829
4871
  Scope extends "local" | "branch"
4830
- > = Node extends { readonly states: StateSchemas } ? NodeSchema<Node> extends never ? {}
4831
- : {
4832
- readonly update: SelectionValue<
4833
- StateUpdateBuilder<AllStates, Extract<Path, ValuedStateIdentifier<AllStates>>>,
4834
- Path,
4835
- "update",
4836
- Scope
4837
- >
4838
- }
4839
- : {}
4872
+ > = Node extends { readonly type: "final" } ? {} : NodeSchema<Node> extends never ? {}
4873
+ : {
4874
+ readonly update: SelectionValue<
4875
+ StateUpdateBuilder<AllStates, Extract<Path, ValuedStateIdentifier<AllStates>>>,
4876
+ Path,
4877
+ "update",
4878
+ Scope
4879
+ >
4880
+ }
4840
4881
 
4841
4882
  /** @internal */
4842
4883
  type BranchUpdateSelectionPath<
@@ -4879,34 +4920,18 @@ export declare namespace Machine {
4879
4920
  & InitialSelectionMethod<Builder, Path, "full">
4880
4921
  : SelectionMethod<Builder, Path, "state", "full">
4881
4922
 
4882
- type FullTargetSelector<States extends StateSchemas> = {
4883
- readonly [Key in Extract<ActiveStateKey<States>, keyof FullTargetBuilder<States>>]: FullSelectionNode<
4884
- States,
4885
- States[Key],
4886
- Extract<Key, StateIdentifier<States>>,
4887
- FullTargetBuilder<States>[Key]
4888
- >
4889
- }
4923
+ type RootBuilder<Builder> = Builder extends { readonly "": infer Root } ? Root : never
4890
4924
 
4891
- type BranchTargetSelector<
4892
- States extends StateSchemas,
4893
- Source extends StateNodeIdentifier<States>,
4894
- Root extends string = Source extends `${infer Head}.${string}` ? Head : Source
4895
- > = Root extends ActiveStateKey<States> ? Root extends keyof BranchTargetBuilder<States, Source> ? {
4896
- readonly [Key in Root]:
4897
- & SelectionNode<
4898
- States,
4899
- States[Key],
4900
- Key,
4901
- "branch",
4902
- BranchTargetBuilder<States, Source>[Key]
4903
- >
4904
- & (Source extends ChoiceIdentifier<States> ? {}
4905
- : Source extends `${Key}.${infer Rest}` ? BranchUpdateSelectionPath<States, States[Key], Key, Rest>
4906
- : StateUpdateSelectionForNode<States, States[Key], Key, "branch">)
4907
- }
4908
- : {}
4909
- : {}
4925
+ type FullTargetSelector<States extends StateSchemas> =
4926
+ & SelectionValue<RootBuilder<FullTargetBuilder<States>>, "", "state", "full">
4927
+ & InitialSelectionMethod<RootBuilder<FullTargetBuilder<States>>, "", "full">
4928
+
4929
+ type BranchTargetSelector<States extends StateSchemas, Source extends StateNodeIdentifier<States>> =
4930
+ States[""] extends { readonly states: infer Children extends StateSchemas } ?
4931
+ & SelectionTreeWithPrefix<States, Children, "", "branch", RootBuilder<BranchTargetBuilder<States, Source>>>
4932
+ & (Source extends ChoiceIdentifier<States> ? {}
4933
+ : Omit<BranchUpdateSelectionPath<States, States[""], "", Source>, "update">)
4934
+ : {}
4910
4935
 
4911
4936
  type LocalTargetSelector<
4912
4937
  States extends StateSchemas,
@@ -4965,6 +4990,22 @@ export declare namespace Machine {
4965
4990
  States extends StateSchemas,
4966
4991
  Source extends StateNodeIdentifier<States>
4967
4992
  > {
4993
+ /** Replaces this handler owner's value without reentry. */
4994
+ /** Selects the current handler owner for a value update without reentry. */
4995
+ readonly self: Source extends ValuedStateIdentifier<States>
4996
+ ? StateUpdateSelectionForNode<States, NodeByIdentifier<States, Source>, Source, "branch">
4997
+ : {}
4998
+ /** Addresses the root and its retained value from any active descendant. */
4999
+ readonly root: "" extends ActiveStateKey<States> ?
5000
+ & SelectionNode<
5001
+ States,
5002
+ States[""],
5003
+ "",
5004
+ "branch",
5005
+ (BranchTargetBuilder<States, Source> extends { readonly "": infer Builder } ? Builder : never)
5006
+ >
5007
+ & StateUpdateSelectionForNode<States, States[""], "", "branch">
5008
+ : never
4968
5009
  /** Handles the trigger without selecting a destination. */
4969
5010
  readonly none: SelectionValue<TargetBuilder<States, Source>["none"], never, "none", "local">
4970
5011
  /** Selects a destination or updates the nearest active compound scope. */
@@ -4974,7 +5015,9 @@ export declare namespace Machine {
4974
5015
  /** Selects a complete destination under any top-level state. */
4975
5016
  readonly full: FullTargetSelector<States>
4976
5017
  /** Restores a shallow or deep history pseudo-state. */
4977
- readonly history: HistorySelectionTree<States, States, "", HistoryTargetBuilder<States>>
5018
+ readonly history: States[""] extends { readonly states: infer Children extends StateSchemas }
5019
+ ? HistorySelectionTree<States, Children, "", RootBuilder<HistoryTargetBuilder<States>>>
5020
+ : {}
4978
5021
  }
4979
5022
 
4980
5023
  /** Definition-time selector that can choose only a valid top-level initial entry. */
@@ -5380,8 +5423,9 @@ export declare namespace Machine {
5380
5423
  *
5381
5424
  * Handlers return snapshots for complete state replacement, target builder
5382
5425
  * results for path-safe partial transitions, state-value updates, or
5383
- * `target.none()` for an explicitly targetless transition. Raw decoded state
5384
- * values and `void` are not accepted at transition boundaries.
5426
+ * an explicit targetless result. In a machine definition, select `to.none`;
5427
+ * its optional resolver returns `undefined`. Raw decoded state values are
5428
+ * not accepted as transition targets.
5385
5429
  *
5386
5430
  * @category utility types
5387
5431
  * @since 0.4.0
@@ -5524,7 +5568,7 @@ export declare namespace Machine {
5524
5568
  ExcludeCompatibleRuntime<
5525
5569
  Exclude<ExecutionServices<InitialServices<M> | Services<M>>, internalRuntime.MachineRuntime>,
5526
5570
  Event<M>,
5527
- Emit<M>
5571
+ EmittedEvent<M>
5528
5572
  >,
5529
5573
  Output<M>,
5530
5574
  | InitialError<M>
@@ -5612,7 +5656,7 @@ export declare namespace Machine {
5612
5656
  export type InvokeEmits<Invoke> = Invoke extends {
5613
5657
  readonly [InvokeTypeId]: { readonly emits: Types.Covariant<infer Emitted> }
5614
5658
  } ? Emitted
5615
- : Invoke extends { readonly child: ChildMachine<string, infer M> } ? Emit<M>
5659
+ : Invoke extends { readonly child: ChildMachine<string, infer M> } ? EmittedEvent<M>
5616
5660
  : never
5617
5661
 
5618
5662
  /** Public parent inputs required by an invoked child machine. */
@@ -5846,7 +5890,7 @@ export declare namespace Machine {
5846
5890
  Selection
5847
5891
  > = (
5848
5892
  context: TransitionResolveContext<Context, Selection>,
5849
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5893
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
5850
5894
  ) => SelectionKind<Selection> extends "none" ? undefined : SelectedTargetResult<Selection> | undefined
5851
5895
 
5852
5896
  export type DeclinableTransitionResolver<
@@ -5856,7 +5900,7 @@ export declare namespace Machine {
5856
5900
  Selection
5857
5901
  > = (
5858
5902
  context: TransitionResolveContext<Context, Selection> & DeclineCapability,
5859
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5903
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
5860
5904
  ) =>
5861
5905
  | (SelectionKind<Selection> extends "none" ? undefined : SelectedTargetResult<Selection> | undefined)
5862
5906
  | Declined
@@ -5870,7 +5914,7 @@ export declare namespace Machine {
5870
5914
  Owner extends ValuedStateIdentifier<States>
5871
5915
  > = (
5872
5916
  context: StateUpdateResolveContext<States, Context, Owner>,
5873
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5917
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
5874
5918
  ) => StateUpdate<States, Owner>
5875
5919
 
5876
5920
  /** @internal */
@@ -5882,7 +5926,7 @@ export declare namespace Machine {
5882
5926
  Owner extends ValuedStateIdentifier<States>
5883
5927
  > = (
5884
5928
  context: StateUpdateResolveContext<States, Context, Owner> & DeclineCapability,
5885
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5929
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
5886
5930
  ) => StateUpdate<States, Owner> | Declined
5887
5931
 
5888
5932
  /** One named destination declared by a branching transition. */
@@ -5950,7 +5994,7 @@ export declare namespace Machine {
5950
5994
  Branches extends Readonly<Record<string, TransitionBranchInput>>
5951
5995
  > = (
5952
5996
  context: TransitionBranchesResolveContext<Context, Branches>,
5953
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5997
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
5954
5998
  ) => BranchSelectionResult<Branches>
5955
5999
 
5956
6000
  export type DeclinableTransitionBranchesResolver<
@@ -5960,7 +6004,7 @@ export declare namespace Machine {
5960
6004
  Branches extends Readonly<Record<string, TransitionBranchInput>>
5961
6005
  > = (
5962
6006
  context: TransitionBranchesResolveContext<Context, Branches> & DeclineCapability,
5963
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6007
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
5964
6008
  ) => BranchSelectionResult<Branches> | Declined
5965
6009
 
5966
6010
  /** @internal Type evidence retained by a transition authored through its bound selector. */
@@ -5971,25 +6015,17 @@ export declare namespace Machine {
5971
6015
  }
5972
6016
  }
5973
6017
 
5974
- type TransitionReenterOption<Reenter extends boolean> = [Reenter] extends [true] ? {
5975
- /** Forces the source state to exit and enter even when active paths remain unchanged. */
5976
- readonly reenter?: boolean
5977
- }
5978
- : { readonly reenter?: never }
5979
-
5980
- type TransitionRequiredOptions<Reenter extends boolean> =
5981
- & TransitionReenterOption<Reenter>
5982
- & {
5983
- /** Keeps the transition required. The resolver cannot return `decline()`. */
5984
- readonly declinable?: false
5985
- }
6018
+ type TransitionRequiredOptions = {
6019
+ /** Keeps the transition required. The resolver cannot return `decline()`. */
6020
+ readonly declinable?: false
6021
+ readonly reenter?: never
6022
+ }
5986
6023
 
5987
- type TransitionDeclinableOptions<Reenter extends boolean> =
5988
- & TransitionReenterOption<Reenter>
5989
- & {
5990
- /** Adds `decline()` to the resolver context and permits declining this candidate. */
5991
- readonly declinable: true
5992
- }
6024
+ type TransitionDeclinableOptions = {
6025
+ /** Adds `decline()` to the resolver context and permits declining this candidate. */
6026
+ readonly declinable: true
6027
+ readonly reenter?: never
6028
+ }
5993
6029
 
5994
6030
  type BuiltTransition<
5995
6031
  States extends StateSchemas,
@@ -6015,7 +6051,7 @@ export declare namespace Machine {
6015
6051
  > {
6016
6052
  (
6017
6053
  resolve: TransitionResolver<Events, Emits, Context, Selection>,
6018
- options?: TransitionRequiredOptions<Reenter>
6054
+ options?: TransitionRequiredOptions
6019
6055
  ): BuiltTransition<
6020
6056
  States,
6021
6057
  Events,
@@ -6039,7 +6075,7 @@ export declare namespace Machine {
6039
6075
  > {
6040
6076
  (
6041
6077
  resolve: DeclinableTransitionResolver<Events, Emits, Context, Selection>,
6042
- options: TransitionDeclinableOptions<Reenter>
6078
+ options: TransitionDeclinableOptions
6043
6079
  ): BuiltTransition<
6044
6080
  States,
6045
6081
  Events,
@@ -6053,22 +6089,79 @@ export declare namespace Machine {
6053
6089
  >
6054
6090
  }
6055
6091
 
6092
+ type ConstructionCallbacks<Context, Builder, Result> = {
6093
+ readonly [
6094
+ Method in Extract<keyof Builder, "from" | "decoded"> as Builder[Method] extends (...args: infer Args) => unknown
6095
+ ? Args extends readonly [unknown?] ? Method : never :
6096
+ never
6097
+ ]: Builder[Method] extends (...args: infer Args) => unknown ? (
6098
+ value: (context: Context) => Args[0]
6099
+ ) => Result :
6100
+ never
6101
+ }
6102
+
6103
+ type GuardedTransition<
6104
+ States extends StateSchemas,
6105
+ Events extends ReadonlyArray<TaggedSchema>,
6106
+ Emits extends ReadonlyArray<TaggedSchema>,
6107
+ StateId extends StateNodeIdentifier<States>,
6108
+ Context,
6109
+ Reenter extends boolean,
6110
+ Selection extends TargetSelection<any, any, any>
6111
+ > =
6112
+ & ConstructionCallbacks<
6113
+ Omit<Context, "target">,
6114
+ SelectionBuilder<Selection>,
6115
+ BuiltTransition<
6116
+ States,
6117
+ Events,
6118
+ Emits,
6119
+ StateId,
6120
+ Context,
6121
+ Reenter,
6122
+ SelectedTargetResult<Selection> | Declined,
6123
+ "declinable"
6124
+ >
6125
+ >
6126
+ & (SelectionSupportsDefaultConstruction<Selection> extends true ? BuiltTransition<
6127
+ States,
6128
+ Events,
6129
+ Emits,
6130
+ StateId,
6131
+ Context,
6132
+ Reenter,
6133
+ SelectedTargetResult<Selection> | Declined,
6134
+ "declinable"
6135
+ >
6136
+ : {})
6137
+ & {
6138
+ readonly resolve: (
6139
+ resolve: TransitionResolver<Events, Emits, Context, Selection>,
6140
+ options?: TransitionRequiredOptions
6141
+ ) => BuiltTransition<
6142
+ States,
6143
+ Events,
6144
+ Emits,
6145
+ StateId,
6146
+ Context,
6147
+ Reenter,
6148
+ SelectedTargetResult<Selection> | Declined,
6149
+ "declinable"
6150
+ >
6151
+ }
6152
+
6056
6153
  /**
6057
6154
  * A selected transition target with target-specific resolver operations.
6058
6155
  *
6059
- * **Example** (Updating state while reentering)
6156
+ * **Example** (Constructing state while reentering)
6060
6157
  *
6061
6158
  * ```ts
6062
6159
  * Reset: (to) =>
6063
- * to.full.Ready().resolve(
6064
- * ({ target }) => target.from(),
6065
- * { reenter: true }
6066
- * )
6160
+ * to.branch.Ready().reenter().from(({ event }) => ({ count: event.count }))
6067
6161
  * ```
6068
6162
  *
6069
6163
  * @inlineType TransitionRequiredOptions
6070
6164
  * @inlineType TransitionDeclinableOptions
6071
- * @inlineType TransitionReenterOption
6072
6165
  */
6073
6166
  export type TransitionTarget<
6074
6167
  States extends StateSchemas,
@@ -6081,6 +6174,27 @@ export declare namespace Machine {
6081
6174
  Selection extends TargetSelection<any, any, any>
6082
6175
  > =
6083
6176
  & Selection
6177
+ & ("declinable" extends Acceptance ? {
6178
+ /** Declines this candidate before construction when the predicate is false. */
6179
+ readonly guard: (
6180
+ predicate: (context: Omit<Context, "target">) => boolean
6181
+ ) => GuardedTransition<States, Events, Emits, StateId, Context, Reenter, Selection>
6182
+ } :
6183
+ {})
6184
+ & ConstructionCallbacks<
6185
+ Omit<Context, "target">,
6186
+ SelectionBuilder<Selection>,
6187
+ BuiltTransition<
6188
+ States,
6189
+ Events,
6190
+ Emits,
6191
+ StateId,
6192
+ Context,
6193
+ Reenter,
6194
+ SelectedTargetResult<Selection>,
6195
+ "required"
6196
+ >
6197
+ >
6084
6198
  & (SelectionSupportsDefaultConstruction<Selection> extends true ? BuiltTransition<
6085
6199
  States,
6086
6200
  Events,
@@ -6107,21 +6221,14 @@ export declare namespace Machine {
6107
6221
  >
6108
6222
  : {})
6109
6223
  }
6110
- & ([Reenter] extends [true] ? SelectionSupportsDefaultConstruction<Selection> extends true ? {
6111
- /** Reenters the source using the selected target's default construction. */
6112
- readonly reenter: () => BuiltTransition<
6113
- States,
6114
- Events,
6115
- Emits,
6116
- StateId,
6117
- Context,
6118
- Reenter,
6119
- SelectionKind<Selection> extends "none" ? undefined : SelectedTargetResult<Selection> | undefined,
6120
- "required"
6121
- >
6122
- }
6123
- : {}
6124
- : {})
6224
+ & ([Reenter] extends [true] ? {
6225
+ /** Forces source exit and entry, preserving subsequent construction and guards. */
6226
+ readonly reenter: () => Omit<
6227
+ TransitionTarget<States, Events, Emits, StateId, Context, Reenter, Acceptance, Selection>,
6228
+ typeof Topology.TargetSelectionTypeId
6229
+ >
6230
+ } :
6231
+ {})
6125
6232
  & (SelectionKind<Selection> extends "state" ?
6126
6233
  SelectionScope<Selection> extends "local" | "branch" ?
6127
6234
  RetainedUpdateOwner<States, StateId, Selection> extends infer Owner extends ValuedStateIdentifier<States> ?
@@ -6146,92 +6253,195 @@ export declare namespace Machine {
6146
6253
  : {}
6147
6254
  : {})
6148
6255
 
6149
- /** A topology selection that requires one retained owner replacement. */
6150
- export type UpdatingTransitionTarget<
6256
+ type UpdatingConstructionCallbacks<Context, Builder, OwnerBuilder, Result> = {
6257
+ readonly [
6258
+ Method in Extract<keyof Builder & keyof OwnerBuilder, "from" | "decoded"> as Builder[Method] extends
6259
+ (...args: infer Args) => unknown ? Args extends readonly [unknown?] ? Method : never : never
6260
+ ]: Builder[Method] extends (...args: infer Args) => unknown ?
6261
+ OwnerBuilder[Method] extends (value: infer Update) => unknown ?
6262
+ (construct: (context: Context) => { readonly target: Args[0]; readonly update: Update }) => Result
6263
+ : never :
6264
+ never
6265
+ }
6266
+
6267
+ type UpdatingCallbacks<
6151
6268
  States extends StateSchemas,
6152
6269
  Events extends ReadonlyArray<TaggedSchema>,
6153
6270
  Emits extends ReadonlyArray<TaggedSchema>,
6154
6271
  StateId extends StateNodeIdentifier<States>,
6155
6272
  Context,
6156
6273
  Reenter extends boolean,
6157
- Acceptance extends TransitionAcceptance,
6158
6274
  Selection extends TargetSelection<any, any, "state">,
6159
- Owner extends ValuedStateIdentifier<States>
6160
- > = Selection & {
6161
- /** @internal */
6162
- readonly "~effect/Machine/UpdatingTransitionTarget": Owner
6163
- readonly resolve:
6164
- & ((
6165
- resolve: (
6166
- context: UpdatingTransitionResolveContext<States, Context, Selection, Owner>,
6167
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6168
- ) => CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>,
6169
- options?: TransitionRequiredOptions<Reenter>
6170
- ) => BuiltTransition<
6171
- States,
6172
- Events,
6173
- Emits,
6174
- StateId,
6175
- Context,
6176
- Reenter,
6177
- CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>,
6178
- "required"
6179
- >)
6180
- & ("declinable" extends Acceptance ? (
6181
- resolve: (
6182
- context: UpdatingTransitionResolveContext<States, Context, Selection, Owner> & DeclineCapability,
6183
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6184
- ) => CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner> | Declined,
6185
- options: TransitionDeclinableOptions<Reenter>
6186
- ) => BuiltTransition<
6187
- States,
6188
- Events,
6189
- Emits,
6190
- StateId,
6191
- Context,
6192
- Reenter,
6193
- CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner> | Declined,
6194
- "declinable"
6195
- >
6196
- : {})
6197
- }
6275
+ Owner extends ValuedStateIdentifier<States>,
6276
+ Acceptance extends TransitionAcceptance
6277
+ > = UpdatingConstructionCallbacks<
6278
+ Omit<UpdatingTransitionResolveContext<States, Context, Selection, Owner>, "owner" | "target">,
6279
+ SelectionBuilder<Selection>,
6280
+ StateUpdateBuilder<States, Owner>,
6281
+ BuiltTransition<
6282
+ States,
6283
+ Events,
6284
+ Emits,
6285
+ StateId,
6286
+ Context,
6287
+ Reenter,
6288
+ | CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>
6289
+ | (Acceptance extends "declinable" ? Declined : never),
6290
+ Acceptance
6291
+ >
6292
+ >
6198
6293
 
6199
- /** @internal */
6200
- interface StateUpdateTransitionRequired<
6294
+ type GuardedUpdatingTransition<
6201
6295
  States extends StateSchemas,
6202
6296
  Events extends ReadonlyArray<TaggedSchema>,
6203
6297
  Emits extends ReadonlyArray<TaggedSchema>,
6204
6298
  StateId extends StateNodeIdentifier<States>,
6205
6299
  Context,
6206
6300
  Reenter extends boolean,
6207
- Selection extends TargetSelection<any, any, "update">
6208
- > {
6209
- (
6210
- resolve: StateUpdateResolver<
6211
- States,
6212
- Events,
6213
- Emits,
6214
- Context,
6215
- Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>
6216
- >,
6217
- options?: TransitionRequiredOptions<Reenter>
6218
- ): BuiltTransition<
6301
+ Selection extends TargetSelection<any, any, "state">,
6302
+ Owner extends ValuedStateIdentifier<States>
6303
+ > = UpdatingCallbacks<States, Events, Emits, StateId, Context, Reenter, Selection, Owner, "declinable"> & {
6304
+ readonly resolve: (
6305
+ resolve: (
6306
+ context: UpdatingTransitionResolveContext<States, Context, Selection, Owner>,
6307
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
6308
+ ) => CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>,
6309
+ options?: TransitionRequiredOptions
6310
+ ) => BuiltTransition<
6219
6311
  States,
6220
6312
  Events,
6221
6313
  Emits,
6222
6314
  StateId,
6223
6315
  Context,
6224
6316
  Reenter,
6225
- SelectedTargetResult<Selection>,
6226
- "required"
6317
+ CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner> | Declined,
6318
+ "declinable"
6227
6319
  >
6228
6320
  }
6229
6321
 
6230
- /** @internal */
6231
- interface StateUpdateTransitionDeclinable<
6232
- States extends StateSchemas,
6233
- Events extends ReadonlyArray<TaggedSchema>,
6234
- Emits extends ReadonlyArray<TaggedSchema>,
6322
+ /**
6323
+ * A topology selection that requires one retained owner replacement.
6324
+ *
6325
+ * `.from` constructs both values from schema inputs; `.decoded` accepts both
6326
+ * decoded values. Both callbacks return `{ target, update }` and read the same
6327
+ * pre-transition snapshot. Use `.resolve` for explicit child construction,
6328
+ * mixed construction methods, or queued commands.
6329
+ *
6330
+ * **Example** (Guarding an atomic destination and owner update)
6331
+ *
6332
+ * ```ts
6333
+ * Save: (to) => to.local.Saving().updating(to.root)
6334
+ * .guard(({ current }) => current.draft.length > 0)
6335
+ * .from(({ current }) => ({
6336
+ * target: { text: current.draft },
6337
+ * update: { ...current, attempts: current.attempts + 1 }
6338
+ * }))
6339
+ * ```
6340
+ */
6341
+ export type UpdatingTransitionTarget<
6342
+ States extends StateSchemas,
6343
+ Events extends ReadonlyArray<TaggedSchema>,
6344
+ Emits extends ReadonlyArray<TaggedSchema>,
6345
+ StateId extends StateNodeIdentifier<States>,
6346
+ Context,
6347
+ Reenter extends boolean,
6348
+ Acceptance extends TransitionAcceptance,
6349
+ Selection extends TargetSelection<any, any, "state">,
6350
+ Owner extends ValuedStateIdentifier<States>
6351
+ > =
6352
+ & Selection
6353
+ & UpdatingCallbacks<States, Events, Emits, StateId, Context, Reenter, Selection, Owner, "required">
6354
+ & ([Reenter] extends [true] ? {
6355
+ readonly reenter: () => Omit<
6356
+ UpdatingTransitionTarget<States, Events, Emits, StateId, Context, Reenter, Acceptance, Selection, Owner>,
6357
+ typeof Topology.TargetSelectionTypeId
6358
+ >
6359
+ } :
6360
+ {})
6361
+ & ("declinable" extends Acceptance ? {
6362
+ /** Declines before either value is constructed or any commands are enqueued. */
6363
+ readonly guard: (
6364
+ predicate: (
6365
+ context: Omit<UpdatingTransitionResolveContext<States, Context, Selection, Owner>, "owner" | "target">
6366
+ ) => boolean
6367
+ ) => GuardedUpdatingTransition<States, Events, Emits, StateId, Context, Reenter, Selection, Owner>
6368
+ } :
6369
+ {})
6370
+ & {
6371
+ /** @internal */
6372
+ readonly "~effect/Machine/UpdatingTransitionTarget": Owner
6373
+ readonly resolve:
6374
+ & ((
6375
+ resolve: (
6376
+ context: UpdatingTransitionResolveContext<States, Context, Selection, Owner>,
6377
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
6378
+ ) => CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>,
6379
+ options?: TransitionRequiredOptions
6380
+ ) => BuiltTransition<
6381
+ States,
6382
+ Events,
6383
+ Emits,
6384
+ StateId,
6385
+ Context,
6386
+ Reenter,
6387
+ CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner>,
6388
+ "required"
6389
+ >)
6390
+ & ("declinable" extends Acceptance ? (
6391
+ resolve: (
6392
+ context: UpdatingTransitionResolveContext<States, Context, Selection, Owner> & DeclineCapability,
6393
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
6394
+ ) => CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner> | Declined,
6395
+ options: TransitionDeclinableOptions
6396
+ ) => BuiltTransition<
6397
+ States,
6398
+ Events,
6399
+ Emits,
6400
+ StateId,
6401
+ Context,
6402
+ Reenter,
6403
+ CombinedTarget<UnwrapConstruction<SelectedTargetResult<Selection>>, States, Owner> | Declined,
6404
+ "declinable"
6405
+ >
6406
+ : {})
6407
+ }
6408
+
6409
+ /** @internal */
6410
+ interface StateUpdateTransitionRequired<
6411
+ States extends StateSchemas,
6412
+ Events extends ReadonlyArray<TaggedSchema>,
6413
+ Emits extends ReadonlyArray<TaggedSchema>,
6414
+ StateId extends StateNodeIdentifier<States>,
6415
+ Context,
6416
+ Reenter extends boolean,
6417
+ Selection extends TargetSelection<any, any, "update">
6418
+ > {
6419
+ (
6420
+ resolve: StateUpdateResolver<
6421
+ States,
6422
+ Events,
6423
+ Emits,
6424
+ Context,
6425
+ Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>
6426
+ >,
6427
+ options?: TransitionRequiredOptions
6428
+ ): BuiltTransition<
6429
+ States,
6430
+ Events,
6431
+ Emits,
6432
+ StateId,
6433
+ Context,
6434
+ Reenter,
6435
+ SelectedTargetResult<Selection>,
6436
+ "required"
6437
+ >
6438
+ }
6439
+
6440
+ /** @internal */
6441
+ interface StateUpdateTransitionDeclinable<
6442
+ States extends StateSchemas,
6443
+ Events extends ReadonlyArray<TaggedSchema>,
6444
+ Emits extends ReadonlyArray<TaggedSchema>,
6235
6445
  StateId extends StateNodeIdentifier<States>,
6236
6446
  Context,
6237
6447
  Reenter extends boolean,
@@ -6245,7 +6455,7 @@ export declare namespace Machine {
6245
6455
  Context,
6246
6456
  Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>
6247
6457
  >,
6248
- options: TransitionDeclinableOptions<Reenter>
6458
+ options: TransitionDeclinableOptions
6249
6459
  ): BuiltTransition<
6250
6460
  States,
6251
6461
  Events,
@@ -6258,38 +6468,128 @@ export declare namespace Machine {
6258
6468
  >
6259
6469
  }
6260
6470
 
6261
- /** @internal */
6262
- type StateUpdateTransition<
6471
+ type GuardedStateUpdateTransition<
6263
6472
  States extends StateSchemas,
6264
6473
  Events extends ReadonlyArray<TaggedSchema>,
6265
6474
  Emits extends ReadonlyArray<TaggedSchema>,
6266
6475
  StateId extends StateNodeIdentifier<States>,
6267
6476
  Context,
6268
6477
  Reenter extends boolean,
6269
- Acceptance extends TransitionAcceptance,
6270
6478
  Selection extends TargetSelection<any, any, "update">
6271
6479
  > =
6272
- & Selection
6273
- & StateUpdateTransitionRequired<States, Events, Emits, StateId, Context, Reenter, Selection>
6274
- & ("declinable" extends Acceptance ? StateUpdateTransitionDeclinable<
6480
+ & ConstructionCallbacks<
6481
+ Omit<
6482
+ StateUpdateResolveContext<States, Context, Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>>,
6483
+ "owner"
6484
+ >,
6485
+ SelectionBuilder<Selection>,
6486
+ BuiltTransition<
6275
6487
  States,
6276
6488
  Events,
6277
6489
  Emits,
6278
6490
  StateId,
6279
6491
  Context,
6280
6492
  Reenter,
6281
- Selection
6493
+ SelectedTargetResult<Selection> | Declined,
6494
+ "declinable"
6282
6495
  >
6283
- : {})
6496
+ >
6497
+ & {
6498
+ readonly resolve: (
6499
+ resolve: StateUpdateResolver<
6500
+ States,
6501
+ Events,
6502
+ Emits,
6503
+ Context,
6504
+ Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>
6505
+ >,
6506
+ options?: TransitionRequiredOptions
6507
+ ) => BuiltTransition<
6508
+ States,
6509
+ Events,
6510
+ Emits,
6511
+ StateId,
6512
+ Context,
6513
+ Reenter,
6514
+ SelectedTargetResult<Selection> | Declined,
6515
+ "declinable"
6516
+ >
6517
+ }
6518
+
6519
+ /** @internal */
6520
+ type StateUpdateTransition<
6521
+ States extends StateSchemas,
6522
+ Events extends ReadonlyArray<TaggedSchema>,
6523
+ Emits extends ReadonlyArray<TaggedSchema>,
6524
+ StateId extends StateNodeIdentifier<States>,
6525
+ Context,
6526
+ Reenter extends boolean,
6527
+ Acceptance extends TransitionAcceptance,
6528
+ Selection extends TargetSelection<any, any, "update">
6529
+ > =
6530
+ & Selection
6531
+ & ([Reenter] extends [true] ? {
6532
+ /** Reenters the source while updating its retained valued ancestor. */
6533
+ readonly reenter: () => Omit<
6534
+ StateUpdateTransition<States, Events, Emits, StateId, Context, Reenter, Acceptance, Selection>,
6535
+ typeof Topology.TargetSelectionTypeId
6536
+ >
6537
+ } :
6538
+ {})
6539
+ & ("declinable" extends Acceptance ? {
6540
+ /** Declines before replacing the selected owner value. */
6541
+ readonly guard: (
6542
+ predicate: (
6543
+ context: Omit<
6544
+ StateUpdateResolveContext<
6545
+ States,
6546
+ Context,
6547
+ Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>
6548
+ >,
6549
+ "owner"
6550
+ >
6551
+ ) => boolean
6552
+ ) => GuardedStateUpdateTransition<States, Events, Emits, StateId, Context, Reenter, Selection>
6553
+ } :
6554
+ {})
6555
+ & ConstructionCallbacks<
6556
+ Omit<
6557
+ StateUpdateResolveContext<States, Context, Extract<SelectionPath<Selection>, ValuedStateIdentifier<States>>>,
6558
+ "owner"
6559
+ >,
6560
+ SelectionBuilder<Selection>,
6561
+ BuiltTransition<States, Events, Emits, StateId, Context, Reenter, SelectedTargetResult<Selection>, "required">
6562
+ >
6563
+ & {
6564
+ readonly resolve:
6565
+ & StateUpdateTransitionRequired<States, Events, Emits, StateId, Context, Reenter, Selection>
6566
+ & ("declinable" extends Acceptance ? StateUpdateTransitionDeclinable<
6567
+ States,
6568
+ Events,
6569
+ Emits,
6570
+ StateId,
6571
+ Context,
6572
+ Reenter,
6573
+ Selection
6574
+ >
6575
+ : {})
6576
+ }
6284
6577
 
6285
6578
  /** @internal Type evidence retained by a machine initial-entry declaration. */
6286
6579
  export interface InitialBuilderEvidence<out Selection> {
6287
6580
  readonly [InitialBuilderTypeId]: Types.Covariant<Selection>
6288
6581
  }
6289
6582
 
6583
+ /** Decoded input shared by initial construction callbacks. */
6584
+ export interface InitialContext<Input> {
6585
+ /** Decoded value supplied when the machine is started. */
6586
+ readonly input: Input
6587
+ }
6588
+
6290
6589
  /** A selected machine initial entry with its exact resolver target. */
6291
6590
  export type InitialTransitionTarget<Input, Selection extends TargetSelection<any, any, any>> =
6292
6591
  & Selection
6592
+ & ConstructionCallbacks<InitialContext<Input>, SelectionBuilder<Selection>, InitialBuilderEvidence<Selection>>
6293
6593
  & (SelectionSupportsDefaultConstruction<Selection> extends true ? InitialBuilderEvidence<Selection> : {})
6294
6594
  & {
6295
6595
  /** Lazily constructs the selected initial state from decoded machine input. */
@@ -6326,6 +6626,42 @@ export declare namespace Machine {
6326
6626
  to: InitialSelector<States, Input>
6327
6627
  ) => InitialBuilderEvidence<TargetSelection<any, any, any>>
6328
6628
 
6629
+ /** Initializes the root and its declared default descendants. */
6630
+ export type RootInitialBuilderInput<Root extends StateNodeConfig, Input> = (
6631
+ root: InitialTransitionTarget<Input, SelectionValue<InitialTargetFactory<Root, "">, "", "initial", "initial">>
6632
+ ) => InitialBuilderEvidence<TargetSelection<any, any, any>>
6633
+
6634
+ interface RootConfigurationSelection<in out Root extends StateNodeConfig> extends
6635
+ TargetSelection<
6636
+ FullSnapshotMethod<{ readonly "": Root }, "" & ActiveStateKey<{ readonly "": Root }>, "">,
6637
+ "",
6638
+ "state",
6639
+ "initial"
6640
+ >
6641
+ {}
6642
+
6643
+ /** Explicitly constructs a complete starting configuration. */
6644
+ export type RootConfigurationBuilderInput<Root extends StateNodeConfig, Input> = (
6645
+ root:
6646
+ & ConstructionCallbacks<
6647
+ InitialContext<Input>,
6648
+ SelectionBuilder<RootConfigurationSelection<Root>>,
6649
+ InitialBuilderEvidence<RootConfigurationSelection<Root>>
6650
+ >
6651
+ & {
6652
+ readonly resolve: (
6653
+ resolve: (
6654
+ context: {
6655
+ /** Decoded startup input. */
6656
+ readonly input: Input
6657
+ /** Builder for the complete root configuration. */
6658
+ readonly target: SelectionBuilder<RootConfigurationSelection<Root>>
6659
+ }
6660
+ ) => ConstructionResult<Snapshot<{ readonly "": Root }>>
6661
+ ) => InitialBuilderEvidence<RootConfigurationSelection<Root>>
6662
+ }
6663
+ ) => InitialBuilderEvidence<TargetSelection<any, any, any>>
6664
+
6329
6665
  type TransitionSelectorNode<
6330
6666
  States extends StateSchemas,
6331
6667
  Events extends ReadonlyArray<TaggedSchema>,
@@ -6403,7 +6739,7 @@ export declare namespace Machine {
6403
6739
  > {
6404
6740
  (
6405
6741
  resolve: TransitionBranchesResolver<Events, Emits, Context, Branches>,
6406
- options?: TransitionRequiredOptions<Reenter>
6742
+ options?: TransitionRequiredOptions
6407
6743
  ): BuiltTransition<
6408
6744
  States,
6409
6745
  Events,
@@ -6427,7 +6763,7 @@ export declare namespace Machine {
6427
6763
  > {
6428
6764
  (
6429
6765
  resolve: DeclinableTransitionBranchesResolver<Events, Emits, Context, Branches>,
6430
- options: TransitionDeclinableOptions<Reenter>
6766
+ options: TransitionDeclinableOptions
6431
6767
  ): BuiltTransition<
6432
6768
  States,
6433
6769
  Events,
@@ -6440,46 +6776,74 @@ export declare namespace Machine {
6440
6776
  >
6441
6777
  }
6442
6778
 
6443
- /** Selector supplied to inline transition declarations. */
6444
- export type TransitionSelector<
6445
- States extends StateSchemas,
6446
- Events extends ReadonlyArray<TaggedSchema>,
6447
- Emits extends ReadonlyArray<TaggedSchema>,
6448
- StateId extends StateNodeIdentifier<States>,
6449
- Context,
6450
- Reenter extends boolean,
6451
- Acceptance extends TransitionAcceptance
6452
- > =
6453
- & TransitionSelectorNode<
6779
+ type TransitionSelectorTargets<
6780
+ in out States extends StateSchemas,
6781
+ in out Events extends ReadonlyArray<TaggedSchema>,
6782
+ in out Emits extends ReadonlyArray<TaggedSchema>,
6783
+ in out StateId extends StateNodeIdentifier<States>,
6784
+ in out Context,
6785
+ in out Reenter extends boolean,
6786
+ in out Acceptance extends TransitionAcceptance
6787
+ > = {
6788
+ readonly [Key in keyof TargetSelector<States, StateId>]: TransitionSelectorNode<
6454
6789
  States,
6455
6790
  Events,
6456
6791
  Emits,
6457
6792
  StateId,
6458
6793
  Context,
6459
- Reenter,
6794
+ Key extends "self" ? false : Reenter,
6460
6795
  Acceptance,
6461
- TargetSelector<States, StateId>
6796
+ TargetSelector<States, StateId>[Key]
6462
6797
  >
6798
+ }
6799
+
6800
+ type TransitionBranchesTarget<
6801
+ States extends StateSchemas,
6802
+ Events extends ReadonlyArray<TaggedSchema>,
6803
+ Emits extends ReadonlyArray<TaggedSchema>,
6804
+ StateId extends StateNodeIdentifier<States>,
6805
+ Context,
6806
+ Reenter extends boolean,
6807
+ Acceptance extends TransitionAcceptance,
6808
+ Branches extends Readonly<Record<string, TransitionBranchInput>>
6809
+ > =
6463
6810
  & {
6464
- /** Declares a closed set of named destinations for one resolver. */
6465
- readonly branches: <const Branches extends Readonly<Record<string, TransitionBranchInput>>>(
6466
- branches: Branches & ValidateTransitionBranchRecord<NoInfer<Branches>>
6467
- ) => {
6468
- /** Resolves exactly one declared branch after this transition is selected. */
6469
- readonly resolve:
6470
- & TransitionBranchesResolveRequired<States, Events, Emits, StateId, Context, Reenter, Branches>
6471
- & ("declinable" extends Acceptance ? TransitionBranchesResolveDeclinable<
6472
- States,
6473
- Events,
6474
- Emits,
6475
- StateId,
6476
- Context,
6477
- Reenter,
6478
- Branches
6479
- >
6480
- : {})
6481
- }
6811
+ /** Resolves exactly one declared branch after this transition is selected. */
6812
+ readonly resolve:
6813
+ & TransitionBranchesResolveRequired<States, Events, Emits, StateId, Context, Reenter, Branches>
6814
+ & ("declinable" extends Acceptance
6815
+ ? TransitionBranchesResolveDeclinable<States, Events, Emits, StateId, Context, Reenter, Branches>
6816
+ : {})
6482
6817
  }
6818
+ & ([Reenter] extends [true] ? {
6819
+ readonly reenter: () => TransitionBranchesTarget<
6820
+ States,
6821
+ Events,
6822
+ Emits,
6823
+ StateId,
6824
+ Context,
6825
+ Reenter,
6826
+ Acceptance,
6827
+ Branches
6828
+ >
6829
+ } :
6830
+ {})
6831
+
6832
+ /** Selector supplied to inline transition declarations. */
6833
+ export interface TransitionSelector<
6834
+ in out States extends StateSchemas,
6835
+ in out Events extends ReadonlyArray<TaggedSchema>,
6836
+ in out Emits extends ReadonlyArray<TaggedSchema>,
6837
+ in out StateId extends StateNodeIdentifier<States>,
6838
+ in out Context,
6839
+ in out Reenter extends boolean,
6840
+ in out Acceptance extends TransitionAcceptance
6841
+ > extends TransitionSelectorTargets<States, Events, Emits, StateId, Context, Reenter, Acceptance> {
6842
+ /** Declares a closed set of named destinations for one resolver. */
6843
+ readonly branches: <const Branches extends Readonly<Record<string, TransitionBranchInput>>>(
6844
+ branches: Branches & ValidateTransitionBranchRecord<NoInfer<Branches>>
6845
+ ) => TransitionBranchesTarget<States, Events, Emits, StateId, Context, Reenter, Acceptance, Branches>
6846
+ }
6483
6847
 
6484
6848
  /** Inline transition declaration accepted by state and invocation handlers. */
6485
6849
  export type TransitionBuilderInput<
@@ -6980,7 +7344,7 @@ export declare namespace Machine {
6980
7344
  Error<ChildDefinition> | ActionError<Services<ChildDefinition>>,
6981
7345
  Services<ChildDefinition>,
6982
7346
  InitialError<ChildDefinition>,
6983
- Emit<ChildDefinition>,
7347
+ EmittedEvent<ChildDefinition>,
6984
7348
  EventOf<Machine.ParentEvents<ChildDefinition>>,
6985
7349
  never,
6986
7350
  | RequiredInvokeChannel<Output<ChildDefinition>, "done">
@@ -7016,9 +7380,9 @@ export declare namespace Machine {
7016
7380
  * ```ts
7017
7381
  * invoke: (from) =>
7018
7382
  * from.effect("load-user", () => loadUser).onDone((to) =>
7019
- * to.full.Ready().resolve(({ output, target }) => target.from({ user: output }))
7383
+ * to.branch.Ready().resolve(({ output, target }) => target.from({ user: output }))
7020
7384
  * ).onFailure((to) =>
7021
- * to.full.Failed().resolve(({ error, target }) => target.from({ error }))
7385
+ * to.branch.Failed().resolve(({ error, target }) => target.from({ error }))
7022
7386
  * )
7023
7387
  * ```
7024
7388
  *
@@ -7244,14 +7608,14 @@ export declare namespace Machine {
7244
7608
  * **Example** (State actions, events, and invocation)
7245
7609
  *
7246
7610
  * ```ts
7247
- * machine.handle({
7611
+ * machine.handle({ states: {
7248
7612
  * Loading: {
7249
7613
  * entry: (_, enqueue) => enqueue.emit({ _tag: "Started" }),
7250
7614
  * invoke: (from) =>
7251
- * from.effect("load", () => load).onDone((to) => to.full.Ready()),
7252
- * on: { Cancel: (to) => to.full.Idle() }
7615
+ * from.effect("load", () => load).onDone((to) => to.branch.Ready()),
7616
+ * on: { Cancel: (to) => to.branch.Idle() }
7253
7617
  * }
7254
- * })
7618
+ * } })
7255
7619
  * ```
7256
7620
  *
7257
7621
  * @inlineType ActiveOutputHandlerConfig
@@ -7273,12 +7637,12 @@ export declare namespace Machine {
7273
7637
  /** Runs synchronously when the state is entered and may enqueue commands. */
7274
7638
  readonly entry?: (
7275
7639
  context: StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
7276
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
7640
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
7277
7641
  ) => StateActionResult<any, any>
7278
7642
  /** Runs synchronously before the state is exited and may enqueue commands. */
7279
7643
  readonly exit?: (
7280
7644
  context: StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
7281
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
7645
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
7282
7646
  ) => StateActionResult<any, any>
7283
7647
  /** Starts state-owned Effect, Stream, timer, logic, or child lifecycles. */
7284
7648
  readonly invoke?: InvokeBuilderInput<States, Events, Emits, StateId, InputEvents, ParentEvents>
@@ -7429,15 +7793,15 @@ export declare namespace Machine {
7429
7793
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
7430
7794
  > = (
7431
7795
  context: StateInitializeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
7432
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
7796
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
7433
7797
  ) => SnapshotBuilderComplete<StateInitializeValue<States, StateId>, boolean>
7434
7798
 
7435
7799
  /** Context used only when a history node has no previously captured record. */
7436
7800
  export interface HistoryDefaultContext<
7437
- States extends StateSchemas,
7801
+ in out States extends StateSchemas,
7438
7802
  Events extends ReadonlyArray<TaggedSchema>,
7439
7803
  Emits extends ReadonlyArray<TaggedSchema>,
7440
- ParentId extends StateIdentifier<States>
7804
+ in out ParentId extends StateIdentifier<States>
7441
7805
  > {
7442
7806
  /** Lifecycle event that attempted to restore this history node. */
7443
7807
  readonly event: LifecycleEvent<Events>
@@ -7461,7 +7825,7 @@ export declare namespace Machine {
7461
7825
  ParentId extends StateIdentifier<States>
7462
7826
  > = (
7463
7827
  context: HistoryDefaultContext<States, Events, Emits, ParentId>,
7464
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
7828
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
7465
7829
  ) =>
7466
7830
  | CompleteSnapshotContaining<States, ParentId>
7467
7831
  | StateConstruction<CompleteSnapshotContaining<States, ParentId>>
@@ -7542,7 +7906,7 @@ export declare namespace Machine {
7542
7906
  /** Runs synchronously when the final state is entered and may enqueue commands. */
7543
7907
  readonly entry?: (
7544
7908
  context: StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
7545
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
7909
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
7546
7910
  ) => StateActionResult<any, any>
7547
7911
  readonly exit?: never
7548
7912
  readonly always?: never
@@ -7595,21 +7959,27 @@ export declare namespace Machine {
7595
7959
  : never
7596
7960
  : never
7597
7961
 
7598
- type HandlerNodeByPath<States extends StateSchemas, Path extends string> = Path extends
7599
- `${infer Head}.${infer Rest}` ? Head extends keyof States ? States[Head] extends {
7600
- readonly states: infer Children extends StateSchemas
7601
- } ? HandlerNodeByPath<Children, Rest>
7602
- : never
7962
+ type HandlerNodeByPath<States extends StateSchemas, Path extends string> = "" extends keyof States ?
7963
+ Path extends "" ? States[""]
7964
+ : States[""] extends { readonly states: infer Children extends StateSchemas } ? HandlerNodeByPath<Children, Path>
7603
7965
  : never
7966
+ : Path extends `${infer Head}.${infer Rest}` ? Head extends keyof States ? States[Head] extends {
7967
+ readonly states: infer Children extends StateSchemas
7968
+ } ? HandlerNodeByPath<Children, Rest>
7969
+ : never
7970
+ : never
7604
7971
  : Path extends keyof States ? States[Path]
7605
7972
  : never
7606
7973
 
7607
7974
  // Resolve only the supplied branch for a flattened state-node path. Keeping
7608
7975
  // this recursion on the finite path avoids recursively expanding an open
7609
7976
  // generic handler config.
7610
- type HandlerConfigAtPath<Config, Path extends string> = Path extends `${infer Head}.${infer Rest}` ?
7611
- Head extends keyof Config ? HandlerConfigAtPath<HandlerNodeChildrenConfig<Config[Head]>, Rest>
7612
- : never
7977
+ type HandlerConfigAtPath<Config, Path extends string> = [Config] extends [never] ? never :
7978
+ "" extends keyof Config ? Path extends "" ? Config[""]
7979
+ : HandlerConfigAtPath<HandlerNodeChildrenConfig<Config[""]>, Path>
7980
+ : Path extends `${infer Head}.${infer Rest}` ?
7981
+ Head extends keyof Config ? HandlerConfigAtPath<HandlerNodeChildrenConfig<Config[Head]>, Rest>
7982
+ : never
7613
7983
  : Path extends keyof Config ? Config[Path]
7614
7984
  : never
7615
7985
 
@@ -7768,7 +8138,7 @@ export declare namespace Machine {
7768
8138
  States extends StateSchemas,
7769
8139
  Prefix extends string,
7770
8140
  Config,
7771
- UnknownKeys extends string = Exclude<Extract<keyof Config, string>, Extract<keyof States, string>>
8141
+ UnknownKeys extends string = Exclude<Extract<keyof Config, string>, ActiveStateKey<States> | ChoiceStateKey<States>>
7772
8142
  > = [UnknownKeys] extends [never] ? unknown : {
7773
8143
  readonly [Key in UnknownKeys]: HandlerValidationError<
7774
8144
  "Handler tree contains a state key that does not exist",
@@ -7937,7 +8307,8 @@ export declare namespace Machine {
7937
8307
  StateId extends StateIdentifier<AllStates>,
7938
8308
  Config
7939
8309
  > = [HistoryIdentifier<AllStates>] extends [never] ? unknown
7940
- : StateId extends RequiredHistoryInitializers<AllStates> ? "initialize" extends keyof Config ? unknown : {
8310
+ : StateId extends RequiredHistoryInitializers<AllStates> | RequiredInitializers<AllStates> ?
8311
+ "initialize" extends keyof Config ? unknown : {
7941
8312
  readonly initialize: HandlerValidationError<
7942
8313
  "State requires initialize for shallow history restoration",
7943
8314
  StateId
@@ -7998,10 +8369,10 @@ export declare namespace Machine {
7998
8369
  Emits extends ReadonlyArray<TaggedSchema>,
7999
8370
  StateId extends string,
8000
8371
  Config,
8001
- Incompatible = IncompatibleRuntime<
8372
+ Incompatible = keyof Config extends never ? never : IncompatibleRuntime<
8002
8373
  ConfigServices<HandlerConfigPart<Config>>,
8003
8374
  EventOf<Events>,
8004
- EmitOf<Emits>
8375
+ EmittedEventOf<Emits>
8005
8376
  >
8006
8377
  > = [Incompatible] extends [never] ? unknown
8007
8378
  : HandlerValidationError<"Handler config requires an incompatible machine runtime", StateId, Incompatible>
@@ -8026,6 +8397,8 @@ export declare namespace Machine {
8026
8397
  NodeConfig,
8027
8398
  AvailableOutputStates
8028
8399
  > extends infer Validation ? unknown extends Validation ? never
8400
+ : "" extends keyof AllStates ? StateId extends "" ? { readonly ""?: Validation }
8401
+ : { readonly ""?: { readonly states: HandlerValidationAtPath<StateId, Validation> } }
8029
8402
  : HandlerValidationAtPath<StateId, Validation>
8030
8403
  : never
8031
8404
 
@@ -8062,11 +8435,82 @@ export declare namespace Machine {
8062
8435
  & HandlerUnknownStateKeyValidation<AllStates, "", Config>
8063
8436
  & HandlerTreeNodeValidations<AllStates, Events, InputEvents, Emits, Config, AvailableOutputStates>
8064
8437
 
8438
+ type RootInitialTargetValidation<States extends StateSchemas, Config> =
8439
+ RequiredInitializersForTargetPath<States, HandlerTreeInitialTargetPath<{ readonly "": Config }>> extends
8440
+ infer Required ? Types.UnionToIntersection<
8441
+ Required extends string ?
8442
+ HandlerConfigAtPath<{ readonly "": Config }, Required> extends infer NodeConfig ?
8443
+ [NodeConfig] extends [never] ? RootInitializeError<Required>
8444
+ : "initialize" extends keyof NodeConfig ? never
8445
+ : RootInitializeError<Required>
8446
+ : never :
8447
+ unknown
8448
+ > :
8449
+ unknown
8450
+
8451
+ type RootInitializeError<Path extends string> = Path extends "" ? {
8452
+ readonly initialize: HandlerValidationError<
8453
+ "State requires initialize because a transition enters its declared initial configuration",
8454
+ Path
8455
+ >
8456
+ } :
8457
+ {
8458
+ readonly states: HandlerValidationAtPath<Path, {
8459
+ readonly initialize: HandlerValidationError<
8460
+ "State requires initialize because a transition enters its declared initial configuration",
8461
+ Path
8462
+ >
8463
+ }>
8464
+ }
8465
+
8466
+ type RootHandlerValidation<
8467
+ States extends StateSchemas,
8468
+ Events extends ReadonlyArray<TaggedSchema>,
8469
+ InputEvents extends ReadonlyArray<TaggedSchema>,
8470
+ Emits extends ReadonlyArray<TaggedSchema>,
8471
+ Config,
8472
+ OutputStates extends StateIdentifier<States>
8473
+ > =
8474
+ & HandlerNodeValidation<
8475
+ States,
8476
+ States[""],
8477
+ Events,
8478
+ InputEvents,
8479
+ Emits,
8480
+ Extract<"", StateNodeIdentifier<States>>,
8481
+ Config,
8482
+ OutputStates
8483
+ >
8484
+ & RootInitialTargetValidation<States, Config>
8485
+ & Types.UnionToIntersection<
8486
+ Exclude<StateNodeIdentifier<States>, ""> extends infer Path ?
8487
+ Path extends StateNodeIdentifier<States> ?
8488
+ HandlerConfigAtPath<{ readonly "": Config }, Path> extends infer NodeConfig ?
8489
+ [NodeConfig] extends [never] ? never :
8490
+ HandlerNodeValidation<
8491
+ States,
8492
+ HandlerNodeByPath<States, Path>,
8493
+ Events,
8494
+ InputEvents,
8495
+ Emits,
8496
+ Path,
8497
+ NodeConfig,
8498
+ OutputStates
8499
+ > extends infer Validation ? unknown extends Validation ? never
8500
+ : { readonly states: HandlerValidationAtPath<Path, Validation> }
8501
+ : never :
8502
+ never :
8503
+ never :
8504
+ never
8505
+ >
8506
+
8065
8507
  type HandlerHasRequiredInitial<
8066
8508
  AllStates extends StateSchemas,
8067
8509
  StateId extends StateIdentifier<AllStates>,
8068
8510
  Config
8069
- > = StateId extends RequiredHistoryInitializers<AllStates> ? "initialize" extends keyof Config ? true : false : true
8511
+ > = StateId extends RequiredHistoryInitializers<AllStates> | RequiredInitializers<AllStates>
8512
+ ? "initialize" extends keyof Config ? true : false
8513
+ : true
8070
8514
 
8071
8515
  type HandlerHasRequiredHistoryDefaults<Node, Config> = Node extends {
8072
8516
  readonly states: infer Children extends StateSchemas
@@ -8093,7 +8537,8 @@ export declare namespace Machine {
8093
8537
  Node,
8094
8538
  StateId extends StateIdentifier<AllStates>,
8095
8539
  Config
8096
- > = [HistoryIdentifier<AllStates> | ChoiceIdentifier<AllStates>] extends [never] ? StateId
8540
+ > = [HistoryIdentifier<AllStates> | ChoiceIdentifier<AllStates> | RequiredInitializers<AllStates>] extends [never] ?
8541
+ StateId
8097
8542
  : HandlerHasRequiredInitial<AllStates, StateId, Config> extends true ?
8098
8543
  HandlerHasRequiredHistoryDefaults<Node, Config> extends true ?
8099
8544
  HandlerHasRequiredChoices<Node, Config> extends true ? StateId : never
@@ -8166,7 +8611,7 @@ export declare namespace Machine {
8166
8611
  ExcludeCompatibleRuntime<
8167
8612
  R | HandlerTreeEvidence<AllStates, Config>["services"],
8168
8613
  EventOf<Events>,
8169
- EmitOf<Emits>
8614
+ EmittedEventOf<Emits>
8170
8615
  >,
8171
8616
  InitialE,
8172
8617
  InitialR,
@@ -8201,21 +8646,23 @@ export declare namespace Machine {
8201
8646
  ParentEvents extends ReadonlyArray<TaggedSchema>
8202
8647
  > {
8203
8648
  <
8204
- const Config extends HandlerTree<
8205
- States,
8206
- States,
8207
- Events,
8208
- Emits,
8209
- E,
8210
- R,
8211
- InputEvents,
8212
- ParentEvents,
8213
- ""
8214
- >
8649
+ const Config
8215
8650
  >(
8216
8651
  config:
8217
8652
  & Config
8218
- & HandlerTreeValidation<
8653
+ & (Config extends (...args: never[]) => unknown ? never : unknown)
8654
+ & HandlerNode<
8655
+ States,
8656
+ States[""],
8657
+ Events,
8658
+ Emits,
8659
+ E,
8660
+ R,
8661
+ InputEvents,
8662
+ ParentEvents,
8663
+ Extract<"", StateNodeIdentifier<States>>
8664
+ >
8665
+ & RootHandlerValidation<
8219
8666
  States,
8220
8667
  Events,
8221
8668
  InputEvents,
@@ -8223,15 +8670,10 @@ export declare namespace Machine {
8223
8670
  NoInfer<Config>,
8224
8671
  | OutputStates
8225
8672
  | Extract<
8226
- HandlerTreeEvidence<States, NoInfer<Config>>["outputState"],
8673
+ HandlerTreeEvidence<States, { readonly "": NoInfer<Config> }>["outputState"],
8227
8674
  StateIdentifier<States>
8228
8675
  >
8229
8676
  >
8230
- & ([StateIdentifier<States>] extends [UnhandledStates] ? HandlerInitialTargetValidation<
8231
- States,
8232
- NoInfer<Config>
8233
- >
8234
- : unknown)
8235
8677
  ): HandleTreeResult<
8236
8678
  States,
8237
8679
  Events,
@@ -8247,7 +8689,7 @@ export declare namespace Machine {
8247
8689
  OutputStates,
8248
8690
  InputEvents,
8249
8691
  ParentEvents,
8250
- Config
8692
+ { readonly "": Config }
8251
8693
  >
8252
8694
  }
8253
8695
 
@@ -8307,11 +8749,11 @@ export declare namespace Machine {
8307
8749
  > {
8308
8750
  readonly entry?: (
8309
8751
  context: StateActionContext<States, Events, Emits, StateId>,
8310
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
8752
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
8311
8753
  ) => StateActionResult<E, R>
8312
8754
  readonly exit?: (
8313
8755
  context: StateActionContext<States, Events, Emits, StateId>,
8314
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
8756
+ enqueue: Enqueue<EventOf<Events>, EmittedEventOf<Emits>>
8315
8757
  ) => StateActionResult<E, R>
8316
8758
  readonly invoke?: StoredInvokeDefinition<States, Events, Emits, StateId>
8317
8759
  readonly always?: TransitionConfig<
@@ -8361,9 +8803,9 @@ export declare namespace Machine {
8361
8803
  >
8362
8804
  }
8363
8805
 
8364
- type StateSource = Machine.DefinedStates<any> | Machine.Any
8806
+ type StateSource = State<any> | Machine.Any
8365
8807
 
8366
- type StateSchemasOf<Source extends StateSource> = Source extends Machine.DefinedStates<infer States> ? States
8808
+ type StateSchemasOf<Source extends StateSource> = Source extends State<infer Node> ? { readonly "": Node }
8367
8809
  : Source extends Machine.Any ? Machine.States<Source>
8368
8810
  : never
8369
8811
 
@@ -8376,6 +8818,29 @@ type StateSchemasOf<Source extends StateSource> = Source extends Machine.Defined
8376
8818
  */
8377
8819
  export type Snapshot<Source extends StateSource> = Machine.Snapshot<StateSchemasOf<Source>>
8378
8820
 
8821
+ /** Type utilities for logical snapshots, including snapshots carried by atom bridges. */
8822
+ /** Typed traversal of root and child logical snapshots.
8823
+ * @category utility types
8824
+ * @since 0.32.0
8825
+ */
8826
+ export declare namespace Snapshot {
8827
+ /** Every active node represented by a logical snapshot type. */
8828
+ export type Node<State> = State extends { readonly path: string; readonly value: unknown } ?
8829
+ | State
8830
+ | (State extends { readonly state: infer Child } ? Node<Child>
8831
+ : State extends { readonly states: infer Regions } ? Node<Regions[keyof Regions]>
8832
+ : never) :
8833
+ never
8834
+ /** Every valid absolute path in a logical snapshot type. */
8835
+ export type Path<State> = Node<State> extends infer Current
8836
+ ? Current extends { readonly path: infer Path extends string } ? Path : never
8837
+ : never
8838
+ /** The node at one absolute path. Runtime access can still be inactive. */
8839
+ export type At<State, Path extends Snapshot.Path<State>> = Node<State> extends infer Current
8840
+ ? Current extends { readonly path: Path } ? Current : never
8841
+ : never
8842
+ }
8843
+
8379
8844
  /**
8380
8845
  * Extracts the decoded value owned by a schema-backed state path.
8381
8846
  *
@@ -8395,7 +8860,7 @@ export type Value<
8395
8860
  *
8396
8861
  * The source may be the object returned by {@link states} or a machine
8397
8862
  * definition. This is the type-level counterpart of
8398
- * `DefinedStates.getSnapshot`.
8863
+ * `State.getSnapshot`.
8399
8864
  *
8400
8865
  * @category utility types
8401
8866
  * @since 0.18.0
@@ -8480,10 +8945,10 @@ export const isFinal: <
8480
8945
  * }
8481
8946
  * })
8482
8947
  *
8483
- * const States = Machine.states({
8948
+ * const States = Machine.state({ initial: "slot1", states: {
8484
8949
  * slot1: TradingSlot,
8485
8950
  * slot2: TradingSlot
8486
- * })
8951
+ * } })
8487
8952
  * ```
8488
8953
  *
8489
8954
  * @category constructors
@@ -8491,47 +8956,8 @@ export const isFinal: <
8491
8956
  */
8492
8957
  export const state: StateConstructor = internal.state as StateConstructor
8493
8958
 
8494
- /**
8495
- * Defines the complete state tree while preserving literal state paths.
8496
- *
8497
- * **When to use**
8498
- *
8499
- * Use when you want to pass a state tree to `make` and also get typed
8500
- * snapshot matching and access helpers.
8501
- *
8502
- * **Details**
8503
- *
8504
- * The returned `states` property is an immutable structural capture of the
8505
- * supplied tree.
8506
- * `Machine.make` derives its initial target selector from that tree and
8507
- * enforces compound and parallel initial-state rules. Active nodes may omit
8508
- * `schema` when they own no value and still participate fully in targeting,
8509
- * matching, and snapshots.
8510
- *
8511
- * **Example** (Atomic initial snapshot)
8512
- *
8513
- * ```ts
8514
- * import { Schema } from "effect"
8515
- * import { Machine } from "@typeonce/effect-machine"
8516
- *
8517
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
8518
- *
8519
- * const States = Machine.states({ idle: Idle })
8520
- *
8521
- * Machine.make({
8522
- * states: States.states,
8523
- * events: Machine.events(),
8524
- * initial: (to) => to.idle().resolve(({ target }) => target.from())
8525
- * })
8526
- * ```
8527
- *
8528
- * @category constructors
8529
- * @since 0.4.0
8530
- */
8531
- export const states: StatesConstructor = internal.states
8532
-
8533
8959
  type MakeConfig<
8534
- States extends Machine.StateSchemas,
8960
+ Root extends Machine.StateNodeConfig,
8535
8961
  InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
8536
8962
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
8537
8963
  Input extends Schema.Top,
@@ -8542,8 +8968,8 @@ type MakeConfig<
8542
8968
  > = {
8543
8969
  /** Stable definition identifier used by inspection and visualization. */
8544
8970
  readonly id?: string
8545
- /** State topology and value schemas, normally supplied by `Machine.states`. */
8546
- readonly states: States & DefineStateTreeInput<NoInfer<States>>
8971
+ /** State topology and value schemas, captured by a `Machine.state` descriptor. */
8972
+ readonly root: { readonly [StateTypeId]: typeof StateTypeId; readonly node: Root }
8547
8973
  /** Public events accepted by independently running machine references. */
8548
8974
  readonly events:
8549
8975
  & Machine.EventProtocol<"public", InputEvents>
@@ -8561,12 +8987,10 @@ type MakeConfig<
8561
8987
  readonly parent?: ParentDeclaration
8562
8988
  /** Schema used to decode input before initial-state construction. */
8563
8989
  readonly input?: Input
8564
- /** Target-first declaration that constructs the initial active configuration. */
8565
- readonly initial: unknown
8566
8990
  }
8567
8991
 
8568
8992
  type MakeResult<
8569
- States extends Machine.StateSchemas,
8993
+ Root extends Machine.StateNodeConfig,
8570
8994
  InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
8571
8995
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
8572
8996
  Input extends Schema.Top,
@@ -8575,23 +8999,29 @@ type MakeResult<
8575
8999
  InternalEvents extends ReadonlyArray<Machine.TaggedSchema>,
8576
9000
  ParentDeclaration extends Parent.Any | undefined
8577
9001
  > = Definition<
8578
- States,
9002
+ { readonly "": Root },
8579
9003
  readonly [...InputEvents, ...InternalEvents],
8580
9004
  Input,
8581
9005
  InitialE,
8582
9006
  InitialR,
8583
- Machine.FinalStateFromDefinition<States>,
8584
- Machine.TerminalOutput<States>,
9007
+ Machine.FinalStateFromDefinition<{ readonly "": Root }>,
9008
+ Machine.TerminalOutput<{ readonly "": Root }>,
8585
9009
  Emits,
8586
9010
  InputEvents,
8587
9011
  Machine.ParentEventsOf<ParentDeclaration>
8588
9012
  >
8589
9013
 
9014
+ type RootInitialization<Root extends Machine.StateNodeConfig, Input> =
9015
+ & { readonly initialConfiguration?: never }
9016
+ & (Machine.NodeSchema<Root> extends never ? { readonly initial?: Machine.RootInitialBuilderInput<Root, Input> }
9017
+ : {} extends NodeMakeInput<Root> ? { readonly initial?: Machine.RootInitialBuilderInput<Root, Input> }
9018
+ : { readonly initial: Machine.RootInitialBuilderInput<Root, Input> })
9019
+
8590
9020
  /** @inline */
8591
9021
  interface Make {
8592
- /** @param config Complete schema-first machine definition. */
9022
+ /** @param config Complete starting configuration, including root and descendant values. */
8593
9023
  <
8594
- const States extends Machine.StateSchemas,
9024
+ const Root extends Machine.StateNodeConfig,
8595
9025
  const InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
8596
9026
  const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8597
9027
  const Input extends Schema.Top = typeof Schema.Void,
@@ -8601,16 +9031,29 @@ interface Make {
8601
9031
  const ParentDeclaration extends Parent.Any | undefined = undefined
8602
9032
  >(
8603
9033
  config:
8604
- & Omit<
8605
- MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentDeclaration>,
8606
- "initial"
8607
- >
8608
- & { readonly initial: Machine.InitialBuilderInput<States, Input["Type"]> },
8609
- ..._validation: ValidateDefinedStates<NoInfer<States>>
8610
- ): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentDeclaration>
8611
- /** @param config Invalid state tree retained only to report its validation error at the call site. */
9034
+ & MakeConfig<Root, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentDeclaration>
9035
+ & {
9036
+ /** Mutually exclusive with a complete initial configuration. */
9037
+ readonly initial?: never
9038
+ /** Constructs the complete initial root configuration, overriding child defaults. */
9039
+ readonly initialConfiguration: Machine.RootConfigurationBuilderInput<
9040
+ NoInfer<Root>,
9041
+ Input["Type"]
9042
+ >
9043
+ }
9044
+ ): MakeResult<
9045
+ Root & { readonly "~effect/Machine/ExplicitInitial": true },
9046
+ InputEvents,
9047
+ Emits,
9048
+ Input,
9049
+ InitialE,
9050
+ InitialR,
9051
+ InternalEvents,
9052
+ ParentDeclaration
9053
+ >
9054
+ /** @param config Complete schema-first machine definition. */
8612
9055
  <
8613
- const States extends Machine.StateSchemas,
9056
+ const Root extends Machine.StateNodeConfig,
8614
9057
  const InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
8615
9058
  const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
8616
9059
  const Input extends Schema.Top = typeof Schema.Void,
@@ -8620,12 +9063,21 @@ interface Make {
8620
9063
  const ParentDeclaration extends Parent.Any | undefined = undefined
8621
9064
  >(
8622
9065
  config:
8623
- & Omit<
8624
- MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentDeclaration>,
8625
- "states"
9066
+ & MakeConfig<Root, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentDeclaration>
9067
+ & Exclude<
9068
+ RootInitialization<NoInfer<Root>, Input["Type"]>,
9069
+ { readonly initialConfiguration: unknown }
8626
9070
  >
8627
- & { readonly states: InvalidDefinedStateTreeInput<States> }
8628
- ): never
9071
+ ): MakeResult<
9072
+ Root,
9073
+ InputEvents,
9074
+ Emits,
9075
+ Input,
9076
+ InitialE,
9077
+ InitialR,
9078
+ InternalEvents,
9079
+ ParentDeclaration
9080
+ >
8629
9081
  }
8630
9082
 
8631
9083
  /**
@@ -8667,30 +9119,30 @@ interface Make {
8667
9119
  * by: Schema.Number
8668
9120
  * }) {}
8669
9121
  *
8670
- * const States = Machine.states({ Count })
8671
- * const Events = Machine.events(Increment)
9122
+ * const States = Machine.state({ initial: "Count", states: { Count } })
9123
+ * const Events = Machine.eventsFromSchemas(Increment)
8672
9124
  *
8673
9125
  * const counter = Machine.make({
8674
- * states: States.states,
9126
+ * root: States,
8675
9127
  * events: Events,
8676
- * initial: (to) => to.Count().resolve(({ target }) => target.decoded(new Count({ value: 0 })))
8677
- * }).handle({
9128
+ * initialConfiguration: root => root.resolve(({ target }) => target.from(to => to.Count.decoded(new Count({ value: 0 }))))
9129
+ * }).handle({ states: {
8678
9130
  * Count: {
8679
9131
  * on: {
8680
9132
  * Increment: (to) =>
8681
- * to.full.Count().resolve(({ event, state, target }) =>
9133
+ * to.branch.Count().resolve(({ event, state, target }) =>
8682
9134
  * target.decoded(new Count({ value: state.value + event.by })))
8683
9135
  * }
8684
9136
  * }
8685
- * })
9137
+ * } })
8686
9138
  * ```
8687
9139
  *
8688
- * @see {@link states} for typed state-tree helpers.
9140
+ * @see {@link state} for typed state-tree helpers.
8689
9141
  * @inlineType MakeConfig
8690
9142
  * @category constructors
8691
9143
  * @since 0.4.0
8692
9144
  */
8693
- export const make: Make = internal.make
9145
+ export const make: Make = internal.make as unknown as Make
8694
9146
 
8695
9147
  /**
8696
9148
  * Extracts the decoded event union carried by an event protocol descriptor.
@@ -8714,7 +9166,7 @@ export type EventOf<Protocol extends Machine.EventProtocol.Any> = Machine.EventO
8714
9166
  * **Example**
8715
9167
  *
8716
9168
  * ```ts
8717
- * export const Event = Machine.events(
9169
+ * export const Event = Machine.eventsFromSchemas(
8718
9170
  * Schema.TaggedUnion({
8719
9171
  * Increment: { by: Schema.Number },
8720
9172
  * Reset: {}
@@ -8727,10 +9179,41 @@ export type EventOf<Protocol extends Machine.EventProtocol.Any> = Machine.EventO
8727
9179
  * @category constructors
8728
9180
  * @since 0.10.0
8729
9181
  */
8730
- export const events: {
8731
- <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
8732
- ...schemas: Schemas & ValidateInputEventProtocol<NoInfer<Schemas>>
8733
- ): Machine.EventProtocol<"public", readonly [...Schemas]>
9182
+ type EventFieldsSchemas<Cases extends Readonly<Record<string, Schema.Struct.Fields>>> = keyof Cases extends never
9183
+ ? readonly []
9184
+ : readonly [
9185
+ Extract<
9186
+ Schema.TaggedUnion<{ readonly [Tag in keyof Cases & string]: Schema.TaggedStruct<Tag, Cases[Tag]> }>,
9187
+ Machine.TaggedSchema
9188
+ >
9189
+ ]
9190
+ type ValidateEventFields<Cases> = {
9191
+ readonly [Tag in keyof Cases]: "_tag" extends keyof Cases[Tag]
9192
+ ? EventProtocolError<"Event fields cannot declare _tag", Extract<Tag, PropertyKey>>
9193
+ : unknown
9194
+ }
9195
+
9196
+ /** Defines a public event protocol from tagged field records.
9197
+ *
9198
+ * ```ts
9199
+ * const Events = Machine.events({ Increment: { by: Schema.Number }, Reset: {} })
9200
+ * const increment = Events.Increment({ by: 1 })
9201
+ * ```
9202
+ *
9203
+ * Constructors defer schema validation until delivery. Use eventsFromSchemas
9204
+ * when importing existing tagged schemas or protocol descriptors.
9205
+ * @category constructors
9206
+ * @since 0.32.0
9207
+ */
9208
+ export const events: <const Cases extends Readonly<Record<string, Schema.Struct.Fields>>>(
9209
+ cases: Cases & ValidateEventFields<NoInfer<Cases>> & ValidateEventProtocolBuilder<"public", EventFieldsSchemas<Cases>>
9210
+ ) => Machine.EventProtocol<"public", EventFieldsSchemas<Cases>> = internal.eventsFromFields as any
9211
+
9212
+ /** Imports existing schemas and protocols without rebuilding their contracts.
9213
+ * @category constructors
9214
+ * @since 0.32.0
9215
+ */
9216
+ export const eventsFromSchemas: {
8734
9217
  <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"public">>>(
8735
9218
  ...inputs: Inputs & ValidateEventProtocolBuilder<"public", Inputs>
8736
9219
  ): Machine.EventProtocol<"public", Machine.EventProtocolInputSchemasOf<"public", Inputs>>
@@ -8774,7 +9257,7 @@ export const optionalParent: <const Events extends ReadonlyArray<Machine.TaggedS
8774
9257
  * **Example**
8775
9258
  *
8776
9259
  * ```ts
8777
- * const Internal = Machine.internalEvents(
9260
+ * const Internal = Machine.internalEventsFromSchemas(
8778
9261
  * Schema.TaggedUnion({
8779
9262
  * Loaded: { value: Schema.String },
8780
9263
  * Failed: { message: Schema.String }
@@ -8789,10 +9272,18 @@ export const optionalParent: <const Events extends ReadonlyArray<Machine.TaggedS
8789
9272
  * @category constructors
8790
9273
  * @since 0.10.0
8791
9274
  */
8792
- export const internalEvents: {
8793
- <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
8794
- ...schemas: Schemas & ValidateInternalEventProtocol<readonly [], NoInfer<Schemas>>
8795
- ): Machine.EventProtocol<"internal", readonly [...Schemas]>
9275
+ export const internalEvents: <const Cases extends Readonly<Record<string, Schema.Struct.Fields>>>(
9276
+ cases:
9277
+ & Cases
9278
+ & ValidateEventFields<NoInfer<Cases>>
9279
+ & ValidateEventProtocolBuilder<"internal", EventFieldsSchemas<Cases>>
9280
+ ) => Machine.EventProtocol<"internal", EventFieldsSchemas<Cases>> = internal.internalEventsFromFields as any
9281
+
9282
+ /** Imports existing schemas and protocols without rebuilding their contracts.
9283
+ * @category constructors
9284
+ * @since 0.32.0
9285
+ */
9286
+ export const internalEventsFromSchemas: {
8796
9287
  <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"internal">>>(
8797
9288
  ...inputs: Inputs & ValidateEventProtocolBuilder<"internal", Inputs>
8798
9289
  ): Machine.EventProtocol<"internal", Machine.EventProtocolInputSchemasOf<"internal", Inputs>>
@@ -8807,7 +9298,7 @@ export const internalEvents: {
8807
9298
  * **Example**
8808
9299
  *
8809
9300
  * ```ts
8810
- * const Emitted = Machine.emittedEvents(
9301
+ * const Emitted = Machine.emittedEventsFromSchemas(
8811
9302
  * Schema.TaggedUnion({
8812
9303
  * Saved: { id: Schema.String }
8813
9304
  * })
@@ -8818,10 +9309,18 @@ export const internalEvents: {
8818
9309
  * @category constructors
8819
9310
  * @since 0.10.0
8820
9311
  */
8821
- export const emittedEvents: {
8822
- <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
8823
- ...schemas: Schemas & ValidateEmittedEventProtocol<NoInfer<Schemas>>
8824
- ): Machine.EventProtocol<"emitted", readonly [...Schemas]>
9312
+ export const emittedEvents: <const Cases extends Readonly<Record<string, Schema.Struct.Fields>>>(
9313
+ cases:
9314
+ & Cases
9315
+ & ValidateEventFields<NoInfer<Cases>>
9316
+ & ValidateEventProtocolBuilder<"emitted", EventFieldsSchemas<Cases>>
9317
+ ) => Machine.EventProtocol<"emitted", EventFieldsSchemas<Cases>> = internal.emittedEventsFromFields as any
9318
+
9319
+ /** Imports existing schemas and protocols without rebuilding their contracts.
9320
+ * @category constructors
9321
+ * @since 0.32.0
9322
+ */
9323
+ export const emittedEventsFromSchemas: {
8825
9324
  <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"emitted">>>(
8826
9325
  ...inputs: Inputs & ValidateEventProtocolBuilder<"emitted", Inputs>
8827
9326
  ): Machine.EventProtocol<"emitted", Machine.EventProtocolInputSchemasOf<"emitted", Inputs>>
@@ -8860,12 +9359,12 @@ export const emittedEvents: {
8860
9359
  * import { Machine } from "@typeonce/effect-machine"
8861
9360
  *
8862
9361
  * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
8863
- * const States = Machine.states({ Idle })
9362
+ * const States = Machine.state({ initial: "Idle", states: { Idle } })
8864
9363
  * const machine = Machine.make({
8865
- * states: States.states,
8866
- * events: Machine.events(),
8867
- * initial: (to) => to.Idle().resolve(({ target }) => target.from())
8868
- * }).handle({ Idle: {} })
9364
+ * root: States,
9365
+ * events: Machine.eventsFromSchemas(),
9366
+ * initialConfiguration: root => root.resolve(({ target }) => target.from(to => to.Idle.from()))
9367
+ * }).handle({ states: { Idle: {} } })
8869
9368
  *
8870
9369
  * const encoded = Effect.gen(function*() {
8871
9370
  * const initial = yield* Machine.planInitial(machine)
@@ -8942,12 +9441,12 @@ export const encodeSnapshot: <
8942
9441
  * import { Machine } from "@typeonce/effect-machine"
8943
9442
  *
8944
9443
  * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
8945
- * const States = Machine.states({ Idle })
9444
+ * const States = Machine.state({ initial: "Idle", states: { Idle } })
8946
9445
  * const machine = Machine.make({
8947
- * states: States.states,
8948
- * events: Machine.events(),
8949
- * initial: (to) => to.Idle().resolve(({ target }) => target.from())
8950
- * }).handle({ Idle: {} })
9446
+ * root: States,
9447
+ * events: Machine.eventsFromSchemas(),
9448
+ * initialConfiguration: root => root.resolve(({ target }) => target.from(to => to.Idle.from()))
9449
+ * }).handle({ states: { Idle: {} } })
8951
9450
  *
8952
9451
  * const roundTrip = Effect.gen(function*() {
8953
9452
  * const initial = yield* Machine.planInitial(machine)
@@ -9051,12 +9550,12 @@ type ValidateTransitionBranchRecord<Branches> = [keyof Branches] extends [never]
9051
9550
  * import { Machine } from "@typeonce/effect-machine"
9052
9551
  *
9053
9552
  * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
9054
- * const States = Machine.states({ Idle })
9553
+ * const States = Machine.state({ initial: "Idle", states: { Idle } })
9055
9554
  * const machine = Machine.make({
9056
- * states: States.states,
9057
- * events: Machine.events(),
9058
- * initial: (to) => to.Idle().resolve(({ target }) => target.from())
9059
- * }).handle({ Idle: {} })
9555
+ * root: States,
9556
+ * events: Machine.eventsFromSchemas(),
9557
+ * initialConfiguration: root => root.resolve(({ target }) => target.from(to => to.Idle.from()))
9558
+ * }).handle({ states: { Idle: {} } })
9060
9559
  *
9061
9560
  * const initialState = Effect.map(Machine.planInitial(machine), (plan) => plan.state)
9062
9561
  * ```
@@ -9108,7 +9607,7 @@ export const planInitial: <
9108
9607
  readonly initialEntryPaths: ReadonlyArray<Machine.StateIdentifier<States>>
9109
9608
  readonly state: Machine.Snapshot<States>
9110
9609
  readonly commands: ReadonlyArray<Command>
9111
- readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>
9610
+ readonly emittedEvents: ReadonlyArray<Machine.EmittedEventOf<Emits>>
9112
9611
  readonly microsteps: ReadonlyArray<{
9113
9612
  readonly next: Machine.Snapshot<States>
9114
9613
  readonly event: Machine.EventOf<Events> | InitialEvent
@@ -9121,7 +9620,7 @@ export const planInitial: <
9121
9620
  >
9122
9621
  readonly commands: ReadonlyArray<Command>
9123
9622
  readonly raisedEvents: ReadonlyArray<Machine.EventOf<Events>>
9124
- readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>
9623
+ readonly emittedEvents: ReadonlyArray<Machine.EmittedEventOf<Emits>>
9125
9624
  readonly exitPaths: ReadonlyArray<string>
9126
9625
  readonly entryPaths: ReadonlyArray<string>
9127
9626
  readonly changed: boolean
@@ -9312,23 +9811,38 @@ export const enabled: <
9312
9811
  * they accept the event. Any commands, emissions, or raised events collected
9313
9812
  * during that check are discarded.
9314
9813
  *
9315
- * Event input is decoded through the machine's public event protocol. Invalid
9316
- * input fails with `MachineSchemaDecodeError`. Final snapshots and valid events
9814
+ * Event input is decoded through the machine's public and internal event protocols.
9815
+ * Invalid input fails with `MachineSchemaDecodeError`. Final snapshots and valid events
9317
9816
  * with no accepting handler return `false`.
9318
9817
  *
9319
9818
  * **Gotchas**
9320
9819
  *
9321
9820
  * This query does not execute transitions or stabilize the resulting machine.
9322
9821
  * It does not run entry, exit, always, completion, child lifecycle, or command
9323
- * effects. A `true` result therefore describes event acceptance only.
9822
+ * effects. A `true` result therefore describes event acceptance only, for this
9823
+ * snapshot. It does not guarantee acceptance after the machine advances.
9824
+ *
9825
+ * This Effect can also be used by machine effects to query internal events.
9826
+ * Synchronous transition resolvers cannot execute it. Querying an internal event
9827
+ * does not make that event available to `MachineRef.send`.
9324
9828
  *
9325
9829
  * **Example**
9326
9830
  *
9327
9831
  * ```ts
9328
- * const canCheckout = Machine.can(checkoutMachine)
9832
+ * import { Machine } from "@typeonce/effect-machine"
9833
+ * import { Effect, Schema } from "effect"
9834
+ *
9835
+ * const internalEvents = Machine.internalEventsFromSchemas(Schema.TaggedStruct("Loaded", {}))
9836
+ * const machine = Machine.make({
9837
+ * root: Machine.state({ initial: "Idle", states: { Idle: {} } }),
9838
+ * events: Machine.eventsFromSchemas(),
9839
+ * internalEvents,
9840
+ * initialConfiguration: (root) => root.resolve(({ target }) => target.from((to) => to.Idle.from()))
9841
+ * }).handle({ states: { Idle: { on: { Loaded: (to) => to.none } } } })
9329
9842
  *
9330
- * const canSubmit = yield* canCheckout(snapshot, {
9331
- * _tag: "SubmitOrder"
9843
+ * export const canLoad = Effect.gen(function*() {
9844
+ * const initial = yield* Machine.planInitial(machine)
9845
+ * return yield* Machine.can(machine, initial.state, internalEvents.Loaded())
9332
9846
  * })
9333
9847
  * ```
9334
9848
  *
@@ -9373,7 +9887,7 @@ export const can: {
9373
9887
  & Machine.RootCompatible<ParentEvents>
9374
9888
  ): (
9375
9889
  state: Machine.Snapshot<States>,
9376
- event: Machine.EventInputOf<InputEvents>
9890
+ event: Machine.EventInputOf<Events>
9377
9891
  ) => Effect.Effect<boolean, MachineSchemaDecodeError>
9378
9892
  <
9379
9893
  const States extends Machine.StateSchemas,
@@ -9411,12 +9925,12 @@ export const can: {
9411
9925
  & EnsureExecutable<States, UnhandledStates, OutputStates>
9412
9926
  & Machine.RootCompatible<ParentEvents>,
9413
9927
  state: Machine.Snapshot<States>,
9414
- event: Machine.EventInputOf<InputEvents>
9928
+ event: Machine.EventInputOf<Events>
9415
9929
  ): Effect.Effect<boolean, MachineSchemaDecodeError>
9416
9930
  } = internal.can as any
9417
9931
 
9418
9932
  /**
9419
- * Plans the next state snapshot synchronously.
9933
+ * Returns an Effect that plans the next state snapshot without running command effects.
9420
9934
  *
9421
9935
  * **Details**
9422
9936
  *
@@ -9441,20 +9955,20 @@ export const can: {
9441
9955
  * class Off extends Schema.TaggedClass<Off>("Off")("Off", {}) {}
9442
9956
  * class On extends Schema.TaggedClass<On>("On")("On", {}) {}
9443
9957
  * class Toggle extends Schema.TaggedClass<Toggle>("Toggle")("Toggle", {}) {}
9444
- * const States = Machine.states({ Off, On })
9958
+ * const States = Machine.state({ initial: "Off", states: { Off, On } })
9445
9959
  * const machine = Machine.make({
9446
- * states: States.states,
9447
- * events: Machine.events(Toggle),
9448
- * initial: (to) => to.Off().resolve(({ target }) => target.from())
9449
- * }).handle({
9960
+ * root: States,
9961
+ * events: Machine.eventsFromSchemas(Toggle),
9962
+ * initialConfiguration: root => root.resolve(({ target }) => target.from(to => to.Off.from()))
9963
+ * }).handle({ states: {
9450
9964
  * Off: {
9451
9965
  * on: {
9452
9966
  * Toggle: (to) =>
9453
- * to.full.On().resolve(({ target }) => target.from())
9967
+ * to.branch.On().resolve(({ target }) => target.from())
9454
9968
  * }
9455
9969
  * },
9456
9970
  * On: {}
9457
- * })
9971
+ * } })
9458
9972
  *
9459
9973
  * const nextState = Effect.gen(function*() {
9460
9974
  * const initial = yield* Machine.planInitial(machine)
@@ -9506,9 +10020,10 @@ export const plan: <
9506
10020
  event: Machine.EventInputOf<InputEvents>
9507
10021
  ) => Effect.Effect<
9508
10022
  & {
10023
+ readonly event: Machine.EventOf<InputEvents>
9509
10024
  readonly next: Machine.Snapshot<States>
9510
10025
  readonly commands: ReadonlyArray<Command>
9511
- readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>
10026
+ readonly emittedEvents: ReadonlyArray<Machine.EmittedEventOf<Emits>>
9512
10027
  readonly microsteps: ReadonlyArray<{
9513
10028
  readonly next: Machine.Snapshot<States>
9514
10029
  readonly event: Machine.EventOf<Events> | InitialEvent
@@ -9521,7 +10036,7 @@ export const plan: <
9521
10036
  >
9522
10037
  readonly commands: ReadonlyArray<Command>
9523
10038
  readonly raisedEvents: ReadonlyArray<Machine.EventOf<Events>>
9524
- readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>
10039
+ readonly emittedEvents: ReadonlyArray<Machine.EmittedEventOf<Emits>>
9525
10040
  readonly exitPaths: ReadonlyArray<string>
9526
10041
  readonly entryPaths: ReadonlyArray<string>
9527
10042
  readonly changed: boolean
@@ -9799,7 +10314,7 @@ export const prepare: <
9799
10314
  ExcludeCompatibleRuntime<
9800
10315
  ExecutionServices<InitialR | R>,
9801
10316
  Machine.EventOf<Events>,
9802
- Machine.EmitOf<Emits>
10317
+ Machine.EmittedEventOf<Emits>
9803
10318
  >
9804
10319
  >
9805
10320
  > = internal.prepare as any
@@ -9833,12 +10348,12 @@ export const prepare: <
9833
10348
  * import { Machine } from "@typeonce/effect-machine"
9834
10349
  *
9835
10350
  * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
9836
- * const States = Machine.states({ Idle })
10351
+ * const States = Machine.state({ initial: "Idle", states: { Idle } })
9837
10352
  * const machine = Machine.make({
9838
- * states: States.states,
9839
- * events: Machine.events(),
9840
- * initial: (to) => to.Idle().resolve(({ target }) => target.from())
9841
- * }).handle({ Idle: {} })
10353
+ * root: States,
10354
+ * events: Machine.eventsFromSchemas(),
10355
+ * initialConfiguration: root => root.resolve(({ target }) => target.from(to => to.Idle.from()))
10356
+ * }).handle({ states: { Idle: {} } })
9842
10357
  *
9843
10358
  * const state = Effect.gen(function*() {
9844
10359
  * const ref = yield* Machine.start(machine)
@@ -9909,7 +10424,7 @@ export const start: <
9909
10424
  ExcludeCompatibleRuntime<
9910
10425
  ExecutionServices<InitialR | R>,
9911
10426
  Machine.EventOf<Events>,
9912
- Machine.EmitOf<Emits>
10427
+ Machine.EmittedEventOf<Emits>
9913
10428
  >
9914
10429
  > = internal.start as any
9915
10430
 
@@ -9944,12 +10459,12 @@ export const start: <
9944
10459
  * import { Machine } from "@typeonce/effect-machine"
9945
10460
  *
9946
10461
  * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
9947
- * const States = Machine.states({ Idle })
10462
+ * const States = Machine.state({ initial: "Idle", states: { Idle } })
9948
10463
  * const machine = Machine.make({
9949
- * states: States.states,
9950
- * events: Machine.events(),
9951
- * initial: (to) => to.Idle().resolve(({ target }) => target.from())
9952
- * }).handle({ Idle: {} })
10464
+ * root: States,
10465
+ * events: Machine.eventsFromSchemas(),
10466
+ * initialConfiguration: root => root.resolve(({ target }) => target.from(to => to.Idle.from()))
10467
+ * }).handle({ states: { Idle: {} } })
9953
10468
  *
9954
10469
  * const resumed = Effect.gen(function*() {
9955
10470
  * const initial = yield* Machine.planInitial(machine)
@@ -10016,6 +10531,6 @@ export const resume: <
10016
10531
  ExcludeCompatibleRuntime<
10017
10532
  ExecutionServices<R>,
10018
10533
  Machine.EventOf<Events>,
10019
- Machine.EmitOf<Emits>
10534
+ Machine.EmittedEventOf<Emits>
10020
10535
  >
10021
10536
  > = internal.resume as any