@typeonce/effect-machine 0.12.0 → 0.14.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 (103) hide show
  1. package/README.md +130 -30
  2. package/dist/Machine.d.ts +722 -262
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +130 -62
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/atom.d.ts +1 -0
  7. package/dist/internal/machine/atom.d.ts.map +1 -1
  8. package/dist/internal/machine/atom.js +23 -0
  9. package/dist/internal/machine/atom.js.map +1 -1
  10. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  11. package/dist/internal/machine/commandRuntime.js +2 -5
  12. package/dist/internal/machine/commandRuntime.js.map +1 -1
  13. package/dist/internal/machine/configuration.d.ts +5 -0
  14. package/dist/internal/machine/configuration.d.ts.map +1 -1
  15. package/dist/internal/machine/configuration.js +6 -2
  16. package/dist/internal/machine/configuration.js.map +1 -1
  17. package/dist/internal/machine/executionPlan.d.ts +1 -0
  18. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  19. package/dist/internal/machine/executionPlan.js +57 -24
  20. package/dist/internal/machine/executionPlan.js.map +1 -1
  21. package/dist/internal/machine/initialization.d.ts +5 -0
  22. package/dist/internal/machine/initialization.d.ts.map +1 -0
  23. package/dist/internal/machine/initialization.js +61 -0
  24. package/dist/internal/machine/initialization.js.map +1 -0
  25. package/dist/internal/machine/inspectionRuntime.d.ts +25 -0
  26. package/dist/internal/machine/inspectionRuntime.d.ts.map +1 -0
  27. package/dist/internal/machine/inspectionRuntime.js +79 -0
  28. package/dist/internal/machine/inspectionRuntime.js.map +1 -0
  29. package/dist/internal/machine/invocation.d.ts +2 -1
  30. package/dist/internal/machine/invocation.d.ts.map +1 -1
  31. package/dist/internal/machine/invocation.js +17 -10
  32. package/dist/internal/machine/invocation.js.map +1 -1
  33. package/dist/internal/machine/machine.d.ts +4 -3
  34. package/dist/internal/machine/machine.d.ts.map +1 -1
  35. package/dist/internal/machine/machine.js +327 -32
  36. package/dist/internal/machine/machine.js.map +1 -1
  37. package/dist/internal/machine/planner.d.ts +28 -0
  38. package/dist/internal/machine/planner.d.ts.map +1 -1
  39. package/dist/internal/machine/planner.js +132 -51
  40. package/dist/internal/machine/planner.js.map +1 -1
  41. package/dist/internal/machine/process.d.ts.map +1 -1
  42. package/dist/internal/machine/process.js +4 -2
  43. package/dist/internal/machine/process.js.map +1 -1
  44. package/dist/internal/machine/runtime.d.ts +27 -3
  45. package/dist/internal/machine/runtime.d.ts.map +1 -1
  46. package/dist/internal/machine/runtime.js +440 -64
  47. package/dist/internal/machine/runtime.js.map +1 -1
  48. package/dist/internal/machine/symbols.d.ts +2 -0
  49. package/dist/internal/machine/symbols.d.ts.map +1 -1
  50. package/dist/internal/machine/symbols.js +2 -0
  51. package/dist/internal/machine/symbols.js.map +1 -1
  52. package/dist/internal/machine/topology.d.ts +25 -0
  53. package/dist/internal/machine/topology.d.ts.map +1 -1
  54. package/dist/internal/machine/topology.js +26 -10
  55. package/dist/internal/machine/topology.js.map +1 -1
  56. package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
  57. package/dist/internal/testing/machine/exploration.js +11 -2
  58. package/dist/internal/testing/machine/exploration.js.map +1 -1
  59. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  60. package/dist/internal/testing/machine/finiteModel.js +75 -69
  61. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  62. package/dist/internal/testing/machine/transitionCoverage.d.ts +20 -0
  63. package/dist/internal/testing/machine/transitionCoverage.d.ts.map +1 -0
  64. package/dist/internal/testing/machine/transitionCoverage.js +80 -0
  65. package/dist/internal/testing/machine/transitionCoverage.js.map +1 -0
  66. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  67. package/dist/internal/testing/machine/verification.js +171 -33
  68. package/dist/internal/testing/machine/verification.js.map +1 -1
  69. package/dist/testing/MachineTest.d.ts +97 -22
  70. package/dist/testing/MachineTest.d.ts.map +1 -1
  71. package/dist/testing/MachineTest.js +58 -16
  72. package/dist/testing/MachineTest.js.map +1 -1
  73. package/dist/unstable/cluster/ClusterMachine.d.ts +4 -1
  74. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  75. package/dist/unstable/cluster/ClusterMachine.js +4 -1
  76. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  77. package/dist/unstable/reactivity/AtomMachine.d.ts +21 -3
  78. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  79. package/dist/unstable/reactivity/AtomMachine.js +21 -3
  80. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  81. package/docs/agent-guide.md +240 -65
  82. package/package.json +5 -5
  83. package/src/Machine.ts +1240 -495
  84. package/src/internal/machine/atom.ts +26 -0
  85. package/src/internal/machine/commandRuntime.ts +2 -9
  86. package/src/internal/machine/configuration.ts +28 -3
  87. package/src/internal/machine/executionPlan.ts +72 -24
  88. package/src/internal/machine/initialization.ts +73 -0
  89. package/src/internal/machine/inspectionRuntime.ts +102 -0
  90. package/src/internal/machine/invocation.ts +37 -12
  91. package/src/internal/machine/machine.ts +438 -39
  92. package/src/internal/machine/planner.ts +180 -55
  93. package/src/internal/machine/process.ts +4 -2
  94. package/src/internal/machine/runtime.ts +661 -89
  95. package/src/internal/machine/symbols.ts +3 -0
  96. package/src/internal/machine/topology.ts +65 -12
  97. package/src/internal/testing/machine/exploration.ts +10 -2
  98. package/src/internal/testing/machine/finiteModel.ts +114 -80
  99. package/src/internal/testing/machine/transitionCoverage.ts +116 -0
  100. package/src/internal/testing/machine/verification.ts +216 -58
  101. package/src/testing/MachineTest.ts +118 -28
  102. package/src/unstable/cluster/ClusterMachine.ts +4 -1
  103. package/src/unstable/reactivity/AtomMachine.ts +24 -3
package/src/Machine.ts CHANGED
@@ -7,6 +7,7 @@
7
7
  import type * as Cause from "effect/Cause"
8
8
  import type * as Duration from "effect/Duration"
9
9
  import type * as Effect from "effect/Effect"
10
+ import type * as Exit from "effect/Exit"
10
11
  import type * as Option from "effect/Option"
11
12
  import type { Pipeable } from "effect/Pipeable"
12
13
  import { hasProperty } from "effect/Predicate"
@@ -52,6 +53,7 @@ declare const MachineTypeId: unique symbol
52
53
  declare const EventConstructionTypeId: unique symbol
53
54
  declare const EmittedEventConstructionTypeId: unique symbol
54
55
  declare const EventProtocolTypeId: unique symbol
56
+ declare const TransitionCaseTypeId: unique symbol
55
57
 
56
58
  const ChildMachineLogicTypeId: typeof internal.ChildMachineLogicTypeId = internal.ChildMachineLogicTypeId
57
59
 
@@ -257,6 +259,8 @@ export interface Machine<
257
259
 
258
260
  /** @internal */
259
261
  readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>
262
+ /** @internal */
263
+ readonly initialDefinition: Machine.InitialDefinition
260
264
  }
261
265
 
262
266
  export {
@@ -536,6 +540,7 @@ type IncompatibleRuntime<Requirements, Events, Emits> = Requirements extends Run
536
540
  : never
537
541
 
538
542
  const InvokeTypeId: typeof internal.InvokeTypeId = internal.InvokeTypeId
543
+ const TransitionTypeId: typeof internal.TransitionTypeId = internal.TransitionTypeId
539
544
 
540
545
  type StateDefinitionError<
541
546
  Message extends string,
@@ -840,6 +845,42 @@ type NodeMethod<
840
845
  > = Machine.NodeSchema<Node> extends never ? FromMethod<FromArguments, Result>
841
846
  : ((...args: Arguments) => Result) & FromMethod<FromArguments, Result>
842
847
 
848
+ type NodeMethodWithInitial<
849
+ Node,
850
+ Arguments extends ReadonlyArray<unknown>,
851
+ Result,
852
+ FromArguments extends ReadonlyArray<unknown>,
853
+ Path extends string
854
+ > = Machine.NodeSchema<Node> extends never ? {
855
+ readonly from: FromCallable<FromArguments, Machine.StateConstruction<Result>>
856
+ readonly initial: InitialTargetFactory<Node, Path>
857
+ }
858
+ :
859
+ & ((...args: Arguments) => Result)
860
+ & {
861
+ readonly from: FromCallable<FromArguments, Machine.StateConstruction<Result>>
862
+ readonly initial: InitialTargetFactory<Node, Path>
863
+ }
864
+
865
+ interface InitialTargetMethod<
866
+ Node,
867
+ Path extends string
868
+ > {
869
+ /** Enters this state through its declared initial configuration. */
870
+ readonly initial: InitialTargetFactory<Node, Path>
871
+ }
872
+
873
+ interface InitialTargetFactory<
874
+ Node,
875
+ Path extends string
876
+ > {
877
+ (...args: WithNodeValue<Node, readonly []>): Machine.InitialTarget<Path>
878
+ readonly from: FromCallable<
879
+ WithNodeInput<Node, readonly []>,
880
+ Machine.StateConstruction<Machine.InitialTarget<Path>>
881
+ >
882
+ }
883
+
843
884
  type NodeConstructionSelectorFromCallable<Node, Builder, Result> = Machine.NodeSchema<Node> extends never ? {
844
885
  <Selected extends ConstructionResult<Result>>(
845
886
  state: (builder: Builder) => Selected
@@ -847,15 +888,19 @@ type NodeConstructionSelectorFromCallable<Node, Builder, Result> = Machine.NodeS
847
888
  }
848
889
  : ConstructionSelectorFromCallable<NodeMakeInput<Node>, Builder, Result>
849
890
 
850
- type NestedTargetMethod<Node, Builder, Result> = Machine.NodeSchema<Node> extends never ? {
891
+ type NestedTargetMethod<Node, Builder, Result, Path extends string> = Machine.NodeSchema<Node> extends never ? {
851
892
  readonly from: NodeConstructionSelectorFromCallable<Node, Builder, Result>
893
+ readonly initial: InitialTargetFactory<Node, Path>
852
894
  }
853
895
  :
854
896
  & (<Selected extends ConstructionResult<Result>>(
855
897
  value: NodeValue<Node>,
856
898
  state: (builder: Builder) => Selected
857
899
  ) => Selected)
858
- & { readonly from: NodeConstructionSelectorFromCallable<Node, Builder, Result> }
900
+ & {
901
+ readonly from: NodeConstructionSelectorFromCallable<Node, Builder, Result>
902
+ readonly initial: InitialTargetFactory<Node, Path>
903
+ }
859
904
 
860
905
  type ConstructionSelectorFromCallable<Input, Builder, Result> = {} extends Input ? {
861
906
  <Selected extends ConstructionResult<Result>>(
@@ -1093,6 +1138,14 @@ type FullSnapshotResult<
1093
1138
  Path extends string = Machine.JoinPath<Prefix, StateId>
1094
1139
  > = Machine.SnapshotByIdentifierWithPath<States, StateId, Path>
1095
1140
 
1141
+ type InitialEntryStateKey<States extends Machine.StateSchemas> = {
1142
+ readonly [Key in ActiveStateKey<States>]: States[Key] extends { readonly states: Machine.StateSchemas } ? Key : never
1143
+ }[ActiveStateKey<States>]
1144
+
1145
+ type FullInitialTargetBuilder<States extends Machine.StateSchemas> = {
1146
+ readonly [Key in InitialEntryStateKey<States>]: InitialTargetMethod<States[Key], Key>
1147
+ }
1148
+
1096
1149
  type FullParallelBuilder<
1097
1150
  States extends Machine.StateSchemas,
1098
1151
  Prefix extends string,
@@ -1383,9 +1436,10 @@ type LocalTargetMethod<
1383
1436
  Source extends Path | `${Path}.${string}` ? NestedTargetMethod<
1384
1437
  Node,
1385
1438
  LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
1386
- LocalTargetResultWithPrefix<AllStates, Children, Path>
1439
+ LocalTargetResultWithPrefix<AllStates, Children, Path>,
1440
+ Path
1387
1441
  >
1388
- : NodeMethod<
1442
+ : NodeMethodWithInitial<
1389
1443
  Node,
1390
1444
  WithNodeValue<Node, [
1391
1445
  states: (
@@ -1397,12 +1451,14 @@ type LocalTargetMethod<
1397
1451
  states: (
1398
1452
  builder: FullParallelBuilder<Children, Path>
1399
1453
  ) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
1400
- ]>
1454
+ ]>,
1455
+ Path
1401
1456
  >
1402
1457
  : Node extends { readonly states: infer Children extends Machine.StateSchemas } ? NestedTargetMethod<
1403
1458
  Node,
1404
1459
  LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
1405
- LocalTargetResultWithPrefix<AllStates, Children, Path>
1460
+ LocalTargetResultWithPrefix<AllStates, Children, Path>,
1461
+ Path
1406
1462
  >
1407
1463
  : NodeMethod<
1408
1464
  Node,
@@ -1497,10 +1553,11 @@ type BranchTargetMethod<
1497
1553
  & NestedTargetMethod<
1498
1554
  Node,
1499
1555
  BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
1500
- BranchTargetResultWithPrefix<AllStates, Children, Path>
1556
+ BranchTargetResultWithPrefix<AllStates, Children, Path>,
1557
+ Path
1501
1558
  >
1502
1559
  & BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>
1503
- : NodeMethod<
1560
+ : NodeMethodWithInitial<
1504
1561
  Node,
1505
1562
  WithNodeValue<Node, [
1506
1563
  states: (
@@ -1512,13 +1569,15 @@ type BranchTargetMethod<
1512
1569
  states: (
1513
1570
  builder: FullParallelBuilder<Children, Path>
1514
1571
  ) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
1515
- ]>
1572
+ ]>,
1573
+ Path
1516
1574
  >
1517
1575
  : Node extends { readonly states: infer Children extends Machine.StateSchemas } ?
1518
1576
  & NestedTargetMethod<
1519
1577
  Node,
1520
1578
  BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>,
1521
- BranchTargetResultWithPrefix<AllStates, Children, Path>
1579
+ BranchTargetResultWithPrefix<AllStates, Children, Path>,
1580
+ Path
1522
1581
  >
1523
1582
  & BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>
1524
1583
  : NodeMethod<
@@ -1664,6 +1723,187 @@ export type RuntimeSnapshot<State, Error = never, Output = never> =
1664
1723
  readonly state: State
1665
1724
  }
1666
1725
 
1726
+ /**
1727
+ * Live, root-scoped records describing one prepared machine tree.
1728
+ *
1729
+ * Inspection records deliberately erase machine-specific values to `unknown`:
1730
+ * one stream may contain unrelated root, child-machine, and `Logic` protocols.
1731
+ * The record structure remains a closed discriminated union, while typed
1732
+ * application observation continues through `changes` and `emissions`.
1733
+ *
1734
+ * @category models
1735
+ * @since 0.13.0
1736
+ */
1737
+ export declare namespace Inspection {
1738
+ /** Read-only identity of a local machine endpoint. */
1739
+ export interface Endpoint {
1740
+ readonly id: string
1741
+ readonly sessionId: string
1742
+ }
1743
+
1744
+ /** Read-only identity of a runtime represented in the inspection tree. */
1745
+ export interface Subject extends Endpoint {
1746
+ readonly kind: "Machine" | "Logic"
1747
+ }
1748
+
1749
+ /** Causal origin of an inspected runtime. */
1750
+ export type Origin =
1751
+ | { readonly _tag: "Root" }
1752
+ | {
1753
+ readonly _tag: "Invoke"
1754
+ readonly ownerPath: string
1755
+ readonly invokeId: string
1756
+ }
1757
+ | {
1758
+ readonly _tag: "Spawn"
1759
+ readonly address: string | undefined
1760
+ }
1761
+
1762
+ /** Causal owner of a send or emission. */
1763
+ export type Causation =
1764
+ | { readonly _tag: "Initialization" }
1765
+ | { readonly _tag: "Macrostep"; readonly macrostepId: number }
1766
+ | { readonly _tag: "Activity"; readonly activitySessionId: string }
1767
+
1768
+ /** Closed command projection safe for observation. */
1769
+ export type Command =
1770
+ | {
1771
+ readonly _tag: "SendTo"
1772
+ readonly target: Endpoint | { readonly id: string }
1773
+ readonly event: unknown
1774
+ }
1775
+ | {
1776
+ readonly _tag: "Stop"
1777
+ readonly target: Endpoint | { readonly id: string }
1778
+ }
1779
+
1780
+ /** One statechart microstep in a committed macrostep. */
1781
+ export interface Microstep {
1782
+ readonly event: unknown
1783
+ readonly transitions: ReadonlyArray<Machine.RetainedTransition>
1784
+ readonly raisedEvents: ReadonlyArray<unknown>
1785
+ readonly emittedEvents: ReadonlyArray<unknown>
1786
+ readonly commands: ReadonlyArray<Command>
1787
+ readonly exitPaths: ReadonlyArray<string>
1788
+ readonly entryPaths: ReadonlyArray<string>
1789
+ readonly changed: boolean
1790
+ }
1791
+
1792
+ /** Common ordering and identity fields for every record. */
1793
+ export interface Base {
1794
+ /** Total publication order within this prepared root. */
1795
+ readonly sequence: number
1796
+ /** Session id of the prepared root that owns this inspection stream. */
1797
+ readonly rootSessionId: string
1798
+ /** Runtime instance described by this record. */
1799
+ readonly subject: Subject
1800
+ }
1801
+
1802
+ /** Announces allocation of a root or owned process identity. */
1803
+ export interface Created extends Base {
1804
+ readonly _tag: "Created"
1805
+ readonly parent: Subject | undefined
1806
+ readonly origin: Origin
1807
+ /** Compiled statechart definition; absent for generic `Logic`. */
1808
+ readonly definition: Machine.Any | undefined
1809
+ }
1810
+
1811
+ /** Announces successful initialization. */
1812
+ export interface Initialized extends Base {
1813
+ readonly _tag: "Initialized"
1814
+ readonly snapshot: RuntimeSnapshot<unknown, unknown, unknown>
1815
+ readonly initialEntryPaths: ReadonlyArray<string>
1816
+ readonly microsteps: ReadonlyArray<Microstep>
1817
+ }
1818
+
1819
+ /** Announces failure before an initial runtime snapshot exists. */
1820
+ export interface StartFailed extends Base {
1821
+ readonly _tag: "StartFailed"
1822
+ readonly cause: Cause.Cause<unknown>
1823
+ }
1824
+
1825
+ /** Announces acceptance of an event by a local mailbox. */
1826
+ export interface EventSent extends Base {
1827
+ readonly _tag: "EventSent"
1828
+ readonly deliveryId: number
1829
+ readonly source: Subject | undefined
1830
+ readonly target: Endpoint
1831
+ readonly event: unknown
1832
+ readonly causedBy: Causation | undefined
1833
+ }
1834
+
1835
+ /** Announces complete processing of one statechart mailbox event. */
1836
+ export interface EventProcessed extends Base {
1837
+ readonly _tag: "EventProcessed"
1838
+ readonly macrostepId: number
1839
+ readonly deliveryId: number
1840
+ readonly source: Subject | undefined
1841
+ readonly event: unknown
1842
+ readonly before: RuntimeSnapshot<unknown, unknown, unknown>
1843
+ readonly after: RuntimeSnapshot<unknown, unknown, unknown>
1844
+ readonly handled: boolean
1845
+ readonly configurationChanged: boolean
1846
+ readonly microsteps: ReadonlyArray<Microstep>
1847
+ }
1848
+
1849
+ /** Announces a direct state update made by generic `Logic`. */
1850
+ export interface StateChanged extends Base {
1851
+ readonly _tag: "StateChanged"
1852
+ readonly before: unknown
1853
+ readonly after: unknown
1854
+ readonly causedByDeliveryId: number | undefined
1855
+ }
1856
+
1857
+ /** Announces actual publication on a machine's domain emission stream. */
1858
+ export interface Emitted extends Base {
1859
+ readonly _tag: "Emitted"
1860
+ readonly emission: unknown
1861
+ readonly causedBy: Causation | undefined
1862
+ }
1863
+
1864
+ /** Identity of one Effect or timer invocation run. */
1865
+ export interface Activity {
1866
+ readonly id: string
1867
+ readonly sessionId: string
1868
+ readonly owner: Subject
1869
+ readonly ownerPath: string
1870
+ readonly kind: "Effect" | "Timer"
1871
+ }
1872
+
1873
+ /** Announces an Effect or timer invocation starting. */
1874
+ export interface ActivityStarted extends Base {
1875
+ readonly _tag: "ActivityStarted"
1876
+ readonly activity: Activity
1877
+ }
1878
+
1879
+ /** Announces an Effect or timer invocation outcome. */
1880
+ export interface ActivityStopped extends Base {
1881
+ readonly _tag: "ActivityStopped"
1882
+ readonly activity: Activity
1883
+ /** Success or failure from the invoke; interruption means its owner stopped it. */
1884
+ readonly exit: Exit.Exit<unknown, unknown>
1885
+ }
1886
+
1887
+ /** Announces a terminal local runtime snapshot. */
1888
+ export interface Terminated extends Base {
1889
+ readonly _tag: "Terminated"
1890
+ readonly snapshot: RuntimeSnapshot<unknown, unknown, unknown>
1891
+ }
1892
+
1893
+ /** Complete live inspection protocol. */
1894
+ export type Event =
1895
+ | Created
1896
+ | Initialized
1897
+ | StartFailed
1898
+ | EventSent
1899
+ | EventProcessed
1900
+ | StateChanged
1901
+ | Emitted
1902
+ | ActivityStarted
1903
+ | ActivityStopped
1904
+ | Terminated
1905
+ }
1906
+
1667
1907
  /**
1668
1908
  * Represents a classified terminal outcome derived from a runtime snapshot.
1669
1909
  *
@@ -1726,6 +1966,14 @@ export interface Prepared<out State, in Event, out Error, out Output, out Emitte
1726
1966
  /** Streams ephemeral notifications published after subscription. */
1727
1967
  readonly emissions: Stream.Stream<Emitted>
1728
1968
 
1969
+ /**
1970
+ * Streams ordered operational records for this root and every locally owned
1971
+ * descendant. The stream is hot, non-replayed, never fails, and completes
1972
+ * after the prepared root terminates. Subscribe before evaluating `start` to
1973
+ * observe initialization.
1974
+ */
1975
+ readonly inspection: Stream.Stream<Inspection.Event>
1976
+
1729
1977
  /** Initializes this machine once and returns its running reference. */
1730
1978
  readonly start: Effect.Effect<MachineRef<State, Event, Error, Output, Emitted>, StartError, StartRequirements>
1731
1979
  }
@@ -2167,6 +2415,8 @@ export declare namespace Machine {
2167
2415
  readonly invoke: any
2168
2416
  /** @internal */
2169
2417
  readonly initial: any
2418
+ /** @internal */
2419
+ readonly initialDefinition: InitialDefinition
2170
2420
  }
2171
2421
 
2172
2422
  /**
@@ -2647,15 +2897,15 @@ export declare namespace Machine {
2647
2897
  * @category utility types
2648
2898
  * @since 0.4.0
2649
2899
  */
2650
- export type InitialBuilder<States extends StateSchemas> = InitialSnapshotBuilderWithPrefix<States>
2900
+ type InitialBuilder<States extends StateSchemas> = InitialSnapshotBuilderWithPrefix<States>
2651
2901
 
2652
2902
  /**
2653
- * State definitions and snapshot builders returned by `defineStates`.
2903
+ * State definitions and snapshot access helpers returned by `defineStates`.
2654
2904
  *
2655
2905
  * **Details**
2656
2906
  *
2657
2907
  * The `states` property is the original state tree and can be passed directly
2658
- * to `make`. The `initial` property builds path-safe snapshots for the same
2908
+ * to `make`. The remaining helpers match and read snapshots for the same
2659
2909
  * state tree.
2660
2910
  *
2661
2911
  * @category models
@@ -2665,9 +2915,6 @@ export declare namespace Machine {
2665
2915
  /** Original state tree supplied to {@link defineStates}. */
2666
2916
  readonly states: States
2667
2917
 
2668
- /** Type-safe builders for constructing valid initial snapshots. */
2669
- readonly initial: InitialBuilder<States>
2670
-
2671
2918
  /**
2672
2919
  * Returns the decoded value for an active state path. The supplied
2673
2920
  * snapshot may be a complete root snapshot or a snapshot previously
@@ -2914,31 +3161,50 @@ export declare namespace Machine {
2914
3161
  readonly outcome: "done" | "failure" | "snapshot"
2915
3162
  }
2916
3163
 
2917
- /**
2918
- * Statically inspectable destination paths for a transition handler.
2919
- * A declared parent path covers every descendant below that state.
2920
- *
2921
- * @category models
2922
- * @since 0.4.0
2923
- */
2924
- export type TransitionTargets<Path extends string = string> =
3164
+ /** Static topology selected by a transition branch. */
3165
+ export interface TransitionTargetSelection<
3166
+ Path extends string | undefined = string | undefined,
3167
+ Kind extends Topology.TargetSelectionKind = Topology.TargetSelectionKind,
3168
+ Scope extends Topology.TargetSelectionScope | undefined = Topology.TargetSelectionScope | undefined
3169
+ > {
3170
+ readonly path: Path
3171
+ readonly kind: Kind
3172
+ readonly scope: Scope
3173
+ }
3174
+
3175
+ /** One statically captured branch of a transition definition. */
3176
+ export type TransitionBranch<Path extends string = string> =
2925
3177
  | {
2926
- readonly type: "dynamic"
3178
+ readonly type: "direct"
3179
+ readonly target: Path | undefined
3180
+ readonly selection: TransitionTargetSelection<Path | undefined>
2927
3181
  }
2928
3182
  | {
2929
- readonly type: "declared"
2930
- readonly paths: ReadonlyArray<Path>
3183
+ readonly type: "case"
3184
+ readonly title: string
3185
+ readonly target: Path | undefined
3186
+ readonly selection: TransitionTargetSelection<Path | undefined>
2931
3187
  }
3188
+ | {
3189
+ readonly type: "otherwise"
3190
+ readonly target: Path | undefined
3191
+ readonly selection: TransitionTargetSelection<Path | undefined>
3192
+ }
3193
+
3194
+ /** The statically selected root entry for machine startup. */
3195
+ export interface InitialDefinition<Path extends string = string> {
3196
+ readonly target: Path
3197
+ readonly selection: TransitionTargetSelection<Path, "state" | "initial", "initial">
3198
+ }
2932
3199
 
2933
3200
  /**
2934
3201
  * Inspectable registration for a transition handler.
2935
3202
  *
2936
3203
  * **Details**
2937
3204
  *
2938
- * Event, eventless, completion, and invocation lifecycle handlers may
2939
- * declare an upper bound of possible target paths. A handler without that
2940
- * declaration is explicitly reported as dynamic. The source, trigger, and
2941
- * reentry behavior are available without executing the handler.
3205
+ * Every branch exposes its selected target without executing its predicate
3206
+ * or resolver. A compound local or branch target covers its descendants;
3207
+ * `undefined` identifies an explicitly targetless branch.
2942
3208
  *
2943
3209
  * @category models
2944
3210
  * @since 0.4.0
@@ -2951,7 +3217,7 @@ export declare namespace Machine {
2951
3217
  readonly source: SourcePath
2952
3218
  readonly trigger: TransitionTrigger<EventTag>
2953
3219
  readonly reenter: boolean
2954
- readonly targets: TransitionTargets<TargetPath>
3220
+ readonly branches: ReadonlyArray<TransitionBranch<TargetPath>>
2955
3221
  }
2956
3222
 
2957
3223
  /**
@@ -2981,9 +3247,14 @@ export declare namespace Machine {
2981
3247
  readonly source: SourcePath
2982
3248
  readonly trigger: TransitionTrigger<EventTag>
2983
3249
  readonly reenter: boolean
2984
- /** Path returned by the handler, including a history pseudo-state. */
3250
+ /** Zero-based index of the selected static branch. */
3251
+ readonly branchIndex: number
3252
+ /** Path returned by the handler, including a choice or history pseudo-state. */
2985
3253
  readonly target: TargetPath | undefined
2986
- /** Concrete path used after resolving history, otherwise equal to `target`. */
3254
+ /**
3255
+ * Concrete path used after resolving choice, initial, or history routing.
3256
+ * Choice microsteps retain each intermediate pseudo-state edge separately.
3257
+ */
2987
3258
  readonly resolvedTarget: TargetPath | undefined
2988
3259
  }
2989
3260
 
@@ -3813,9 +4084,7 @@ export declare namespace Machine {
3813
4084
  * Opaque result returned by an explicitly targetless transition.
3814
4085
  *
3815
4086
  * The transition remains handled and retains its queued commands, raised
3816
- * events, and emitted events, but selects no concrete destination. Declared
3817
- * `targets` constrain only concrete destinations, so this result is always
3818
- * permitted from ordinary transition handlers.
4087
+ * events, and emitted events, but selects no concrete destination.
3819
4088
  *
3820
4089
  * @category models
3821
4090
  * @since 0.10.0
@@ -3843,6 +4112,23 @@ export declare namespace Machine {
3843
4112
  readonly parent: Extract<ParentPath<HistoryId>, StateIdentifier<States>>
3844
4113
  }
3845
4114
 
4115
+ /**
4116
+ * Transition instruction that enters a compound or parallel state through
4117
+ * its declared initial configuration.
4118
+ *
4119
+ * @category models
4120
+ * @since 0.13.0
4121
+ */
4122
+ export interface InitialTarget<StateId extends string> {
4123
+ readonly [Topology.TargetTypeId]: typeof Topology.TargetTypeId
4124
+ readonly [Topology.TargetSnapshotTypeId]?: never
4125
+ readonly [Topology.InitialTargetTypeId]: typeof Topology.InitialTargetTypeId
4126
+ readonly _tag: "InitialTarget"
4127
+ readonly path: StateId
4128
+ readonly value: never
4129
+ readonly values?: never
4130
+ }
4131
+
3846
4132
  /** Branded transient target instruction used while constructing initial states. */
3847
4133
  export interface ChoiceTargetInstruction<ChoiceId extends string = string> {
3848
4134
  readonly [Topology.ChoiceTargetTypeId]: typeof Topology.ChoiceTargetTypeId
@@ -3873,7 +4159,9 @@ export declare namespace Machine {
3873
4159
  * @category utility types
3874
4160
  * @since 0.4.0
3875
4161
  */
3876
- export type FullTargetBuilder<States extends StateSchemas> = FullSnapshotBuilderWithPrefix<States>
4162
+ export type FullTargetBuilder<States extends StateSchemas> = [InitialEntryStateKey<States>] extends [never] ?
4163
+ FullSnapshotBuilderWithPrefix<States>
4164
+ : FullSnapshotBuilderWithPrefix<States> & FullInitialTargetBuilder<States>
3877
4165
 
3878
4166
  /**
3879
4167
  * Builder for a complete fallback configuration containing a history owner.
@@ -3953,8 +4241,7 @@ export declare namespace Machine {
3953
4241
  *
3954
4242
  * The event or lifecycle outcome is handled and queued operations are
3955
4243
  * retained, while the current state configuration remains the transition
3956
- * result. This remains valid when the handler declares concrete `targets`,
3957
- * because those paths are an upper bound rather than an exhaustive result.
4244
+ * result.
3958
4245
  *
3959
4246
  * @since 0.10.0
3960
4247
  */
@@ -3995,6 +4282,159 @@ export declare namespace Machine {
3995
4282
  readonly history: HistoryTargetBuilder<States>
3996
4283
  }
3997
4284
 
4285
+ /**
4286
+ * Opaque exact destination selected while a machine definition is captured.
4287
+ * The selection contains topology only; state values are constructed later
4288
+ * by the selected branch's resolver.
4289
+ *
4290
+ * @category models
4291
+ * @since 0.14.0
4292
+ */
4293
+ export interface TargetSelection<
4294
+ out Result,
4295
+ out Path extends string | undefined = string | undefined,
4296
+ out Kind extends Topology.TargetSelectionKind = Topology.TargetSelectionKind
4297
+ > {
4298
+ readonly [Topology.TargetSelectionTypeId]: typeof Topology.TargetSelectionTypeId
4299
+ readonly kind: Kind
4300
+ readonly scope: Topology.TargetSelectionScope | undefined
4301
+ readonly path: Path
4302
+ readonly "~effect/Machine/TargetSelectionResult"?: Types.Covariant<Result>
4303
+ }
4304
+
4305
+ type SelectionMethod<Builder, Path extends string, Kind extends Topology.TargetSelectionKind = "state"> = () =>
4306
+ TargetSelection<Builder, Path, Kind>
4307
+
4308
+ type InitialSelectionMethod<Builder, Path extends string> = Builder extends { readonly initial: infer Initial } ? {
4309
+ readonly initial: SelectionMethod<Initial, Path, "initial">
4310
+ }
4311
+ : {}
4312
+
4313
+ type SelectionTreeWithPrefix<
4314
+ AllStates extends StateSchemas,
4315
+ States extends StateSchemas,
4316
+ Prefix extends string,
4317
+ Scope extends "local" | "branch",
4318
+ Builder
4319
+ > = {
4320
+ readonly [Key in Extract<ActiveStateKey<States> | ChoiceStateKey<States>, keyof Builder>]: SelectionNode<
4321
+ AllStates,
4322
+ States[Key],
4323
+ JoinPath<Prefix, Key>,
4324
+ Scope,
4325
+ Builder[Key]
4326
+ >
4327
+ }
4328
+
4329
+ type SelectionNode<
4330
+ AllStates extends StateSchemas,
4331
+ Node,
4332
+ Path extends string,
4333
+ Scope extends "local" | "branch",
4334
+ Builder
4335
+ > = Node extends ChoiceStateNodeConfig ? SelectionMethod<
4336
+ Builder,
4337
+ Path,
4338
+ "choice"
4339
+ >
4340
+ : Node extends { readonly states: infer Children extends StateSchemas } ?
4341
+ & SelectionMethod<Builder, Path>
4342
+ & InitialSelectionMethod<Builder, Path>
4343
+ & SelectionTreeWithPrefix<AllStates, Children, Path, Scope, Builder>
4344
+ : SelectionMethod<Builder, Path>
4345
+
4346
+ type FullSelectionNode<
4347
+ AllStates extends StateSchemas,
4348
+ Node,
4349
+ Path extends StateIdentifier<AllStates>,
4350
+ Builder
4351
+ > = Node extends { readonly states: StateSchemas } ?
4352
+ & SelectionMethod<Builder, Path>
4353
+ & InitialSelectionMethod<Builder, Path>
4354
+ : SelectionMethod<Builder, Path>
4355
+
4356
+ type FullTargetSelector<States extends StateSchemas> = {
4357
+ readonly [Key in Extract<ActiveStateKey<States>, keyof FullTargetBuilder<States>>]: FullSelectionNode<
4358
+ States,
4359
+ States[Key],
4360
+ Extract<Key, StateIdentifier<States>>,
4361
+ FullTargetBuilder<States>[Key]
4362
+ >
4363
+ }
4364
+
4365
+ type BranchTargetSelector<
4366
+ States extends StateSchemas,
4367
+ Source extends StateNodeIdentifier<States>,
4368
+ Root extends string = Source extends `${infer Head}.${string}` ? Head : Source
4369
+ > = Root extends ActiveStateKey<States> ? Root extends keyof BranchTargetBuilder<States, Source> ? {
4370
+ readonly [Key in Root]: SelectionNode<
4371
+ States,
4372
+ States[Key],
4373
+ Key,
4374
+ "branch",
4375
+ BranchTargetBuilder<States, Source>[Key]
4376
+ >
4377
+ }
4378
+ : {}
4379
+ : {}
4380
+
4381
+ type LocalTargetSelector<
4382
+ States extends StateSchemas,
4383
+ Source extends StateNodeIdentifier<States>
4384
+ > = NearestCompoundScope<States, Source> extends infer Scope extends StateIdentifier<States> ?
4385
+ ChildrenOf<States, Scope> extends infer Children extends StateSchemas ?
4386
+ LocalTargetBuilder<States, Source> extends infer Builder ?
4387
+ & SelectionTreeWithPrefix<States, Children, Scope, "local", Builder>
4388
+ & ("with" extends keyof Builder ? {
4389
+ readonly with: SelectionMethod<Builder["with"], Scope>
4390
+ }
4391
+ : {})
4392
+ : {}
4393
+ : {}
4394
+ : {}
4395
+
4396
+ type HistorySelectionTree<
4397
+ AllStates extends StateSchemas,
4398
+ States extends StateSchemas,
4399
+ Prefix extends string,
4400
+ Builder
4401
+ > = {
4402
+ readonly [Key in Extract<HistoryContainingKey<States>, keyof Builder>]: States[Key] extends HistoryStateNodeConfig ?
4403
+ SelectionMethod<
4404
+ Builder[Key],
4405
+ JoinPath<Prefix, Key>,
4406
+ "history"
4407
+ >
4408
+ : States[Key] extends { readonly states: infer Children extends StateSchemas } ? HistorySelectionTree<
4409
+ AllStates,
4410
+ Children,
4411
+ JoinPath<Prefix, Key>,
4412
+ Builder[Key]
4413
+ >
4414
+ : never
4415
+ }
4416
+
4417
+ /** Definition-time topology selector available to an ordinary transition. */
4418
+ export interface TargetSelector<
4419
+ States extends StateSchemas,
4420
+ Source extends StateNodeIdentifier<States>
4421
+ > {
4422
+ readonly none: SelectionMethod<TargetBuilder<States, Source>["none"], never, "none">
4423
+ readonly local: LocalTargetSelector<States, Source>
4424
+ readonly branch: BranchTargetSelector<States, Source>
4425
+ readonly full: FullTargetSelector<States>
4426
+ readonly history: HistorySelectionTree<States, States, "", HistoryTargetBuilder<States>>
4427
+ }
4428
+
4429
+ /** Definition-time selector that can choose only a valid top-level initial entry. */
4430
+ export type InitialSelector<States extends StateSchemas> = {
4431
+ readonly [Key in Extract<ActiveStateKey<States>, keyof InitialBuilder<States>>]: States[Key] extends
4432
+ { readonly states: StateSchemas } ? {
4433
+ readonly initial: SelectionMethod<InitialBuilder<States>[Key], Key, "initial">
4434
+ }
4435
+ : SelectionMethod<InitialBuilder<States>[Key], Key>
4436
+ }
4437
+
3998
4438
  /**
3999
4439
  * Context passed to a state/event handler.
4000
4440
  *
@@ -4292,7 +4732,8 @@ export declare namespace Machine {
4292
4732
  export type InitialResult<States extends StateSchemas, E, R> =
4293
4733
  | Snapshot<States>
4294
4734
  | InitialSnapshot<States>
4295
- | StateConstruction<Snapshot<States> | InitialSnapshot<States>>
4735
+ | InitialTarget<StateIdentifier<States>>
4736
+ | StateConstruction<Snapshot<States> | InitialSnapshot<States> | InitialTarget<StateIdentifier<States>>>
4296
4737
 
4297
4738
  /** Initial snapshots may transiently terminate in a declared choice node. */
4298
4739
  export type InitialSnapshot<States extends StateSchemas> = {
@@ -4376,8 +4817,8 @@ export declare namespace Machine {
4376
4817
  ? NonNullable<Config[Key]> extends (...args: any) => infer Ret ? Ret : never
4377
4818
  : never
4378
4819
  /** Extracts the return value from an implicit initial child implementation. */
4379
- export type StateInitialReturn<Config> = Config extends { readonly initial?: infer Initial }
4380
- ? NonNullable<Initial> extends (...args: any) => infer Ret ? Ret : never
4820
+ export type StateInitializeReturn<Config> = Config extends { readonly initialize?: infer Initialize }
4821
+ ? NonNullable<Initialize> extends (...args: any) => infer Ret ? Ret : never
4381
4822
  : never
4382
4823
  /** Extracts the return values from a state's history defaults. */
4383
4824
  export type HistoryDefaultReturn<Config> = Config extends { readonly history?: infer History } ? {
@@ -4388,19 +4829,22 @@ export declare namespace Machine {
4388
4829
  }[keyof NonNullable<History>]
4389
4830
  : never
4390
4831
  /** Extracts the return value from a choice resolver. */
4391
- export type ChoiceReturn<Config> = Config extends {
4392
- readonly choice: { readonly transition: (...args: any) => infer Ret }
4393
- } ? Ret :
4394
- never
4832
+ export type ChoiceReturn<Config> = Config extends { readonly choice: infer Choice } ? EventTransitionReturn<Choice>
4833
+ : never
4395
4834
  /**
4396
4835
  * Extracts the return value from an event transition config.
4397
4836
  *
4398
4837
  * @category utility types
4399
4838
  * @since 0.4.0
4400
4839
  */
4401
- export type EventTransitionReturn<Transition> = Transition extends (...args: any) => infer Ret ? Ret
4402
- : Transition extends { readonly transition: (...args: any) => infer Ret } ? Ret
4403
- : never
4840
+ export type EventTransitionReturn<Transition> =
4841
+ | (Transition extends { readonly resolve?: infer Resolve } ?
4842
+ NonNullable<Resolve> extends (...args: any) => infer Ret ? Ret : never
4843
+ : never)
4844
+ | (Transition extends { readonly cases: infer Cases extends ReadonlyArray<unknown> } ?
4845
+ EventTransitionReturn<Cases[number]>
4846
+ : never)
4847
+ | (Transition extends { readonly otherwise: infer Otherwise } ? EventTransitionReturn<Otherwise> : never)
4404
4848
  /**
4405
4849
  * Extracts the return value from a state's event handlers.
4406
4850
  *
@@ -4434,6 +4878,8 @@ export declare namespace Machine {
4434
4878
  */
4435
4879
  export type InvokeResolvedSource<Source> = Source extends (...args: any) => infer Resolved ? Resolved : Source
4436
4880
 
4881
+ type InvokeFactoryResult<Source> = Source extends (...args: any) => infer Resolved ? Resolved : never
4882
+
4437
4883
  type ChildMachineLogic<Child> = Child extends ChildMachine<string, infer M> ? Logic<
4438
4884
  Snapshot<States<M>>,
4439
4885
  EventInput<InputEvent<M>>,
@@ -4455,7 +4901,7 @@ export declare namespace Machine {
4455
4901
  : never
4456
4902
 
4457
4903
  export type InvokeLogic<Invoke> = Invoke extends { readonly effect: infer Source } ?
4458
- InvokeResolvedSource<Source> extends infer Fx extends Effect.Effect<any, any, any> ? Logic<
4904
+ InvokeFactoryResult<Source> extends infer Fx extends Effect.Effect<any, any, any> ? Logic<
4459
4905
  void,
4460
4906
  never,
4461
4907
  Effect.Error<Fx>,
@@ -4600,34 +5046,146 @@ export declare namespace Machine {
4600
5046
  | Effect.Services<DoneReturn<Config>>
4601
5047
  | Effect.Services<StateActionReturn<Config, "entry">>
4602
5048
  | Effect.Services<StateActionReturn<Config, "exit">>
4603
- | Effect.Services<StateInitialReturn<Config>>
4604
5049
  | Effect.Services<HistoryDefaultReturn<Config>>
4605
5050
  | Effect.Services<ChoiceReturn<Config>>
4606
5051
  | InvokeRequirements<Config>
4607
5052
 
4608
- export type InvokeTransition<
5053
+ type TransitionWhenContext<Context> = Omit<Context, "target">
5054
+
5055
+ /** Type evidence retained by {@link transition} without affecting runtime data. */
5056
+ export interface TransitionTyped<
4609
5057
  States extends StateSchemas,
4610
5058
  Events extends ReadonlyArray<TaggedSchema>,
4611
5059
  Emits extends ReadonlyArray<TaggedSchema>,
4612
- Context
5060
+ StateId extends StateNodeIdentifier<States>,
5061
+ Context,
5062
+ Reenter extends boolean
5063
+ > {
5064
+ readonly [TransitionTypeId]: {
5065
+ readonly owner: Types.Covariant<readonly [States, Events, Emits, StateId, Context]>
5066
+ }
5067
+ }
5068
+
5069
+ /** Type evidence for a targetless transition reusable in every owning context. */
5070
+ export interface TargetlessTransitionTyped {
5071
+ readonly [TransitionTypeId]: {
5072
+ readonly targetless: true
5073
+ }
5074
+ }
5075
+
5076
+ /** The only transition value accepted by machine handler APIs. */
5077
+ export type TransitionConfig<
5078
+ States extends StateSchemas,
5079
+ Events extends ReadonlyArray<TaggedSchema>,
5080
+ Emits extends ReadonlyArray<TaggedSchema>,
5081
+ StateId extends StateNodeIdentifier<States>,
5082
+ Context,
5083
+ Reenter extends boolean = false
4613
5084
  > =
4614
- | ((context: Context, enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>) => HandlerResult<States, any, any>)
4615
- | {
4616
- readonly reenter?: boolean
4617
- readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4618
- readonly transition: (
4619
- context: Context,
4620
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4621
- ) => HandlerResult<States, any, any>
5085
+ & (TransitionTyped<States, Events, Emits, StateId, Context, Reenter> | TargetlessTransitionTyped)
5086
+ & {
5087
+ readonly reenter?: [Reenter] extends [true] ? boolean : never
4622
5088
  }
4623
5089
 
4624
- export type InvokeSource<Value, Context> = Value | ((context: Context) => Value)
5090
+ export type SelectionBuilder<Selection> = Selection extends TargetSelection<infer Builder, any, any> ? Builder : never
5091
+ export type SelectionKind<Selection> = Selection extends TargetSelection<any, any, infer Kind> ? Kind : never
5092
+ export type SelectionPath<Selection> = Selection extends TargetSelection<any, infer Path, any> ? Path : never
5093
+ export type TargetBuilderResult<Builder> =
5094
+ | (Builder extends (...args: any) => infer Result ? Result : never)
5095
+ | (Builder extends { readonly from: (...args: any) => infer Result } ? Result : never)
5096
+ export type SelectedTargetResult<Selection> = SelectionBuilder<Selection> extends infer Builder ?
5097
+ TargetBuilderResult<Builder>
5098
+ : never
5099
+
5100
+ export type TransitionResolveContext<
5101
+ Context,
5102
+ Selection,
5103
+ Match = never
5104
+ > =
5105
+ & Omit<Context, "target">
5106
+ & ([Match] extends [never] ? {} : { readonly match: Match })
5107
+ & (SelectionKind<Selection> extends "none" ? {} : { readonly target: SelectionBuilder<Selection> })
5108
+
5109
+ export type TransitionResolver<
5110
+ Events extends ReadonlyArray<TaggedSchema>,
5111
+ Emits extends ReadonlyArray<TaggedSchema>,
5112
+ Context,
5113
+ Selection,
5114
+ Match = never
5115
+ > = (
5116
+ context: TransitionResolveContext<Context, Selection, Match>,
5117
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5118
+ ) => SelectionKind<Selection> extends "none" ? undefined : SelectedTargetResult<Selection> | undefined
5119
+
5120
+ export type TransitionDirectInput<
5121
+ States extends StateSchemas,
5122
+ Events extends ReadonlyArray<TaggedSchema>,
5123
+ Emits extends ReadonlyArray<TaggedSchema>,
5124
+ StateId extends StateNodeIdentifier<States>,
5125
+ Context,
5126
+ Reenter extends boolean,
5127
+ Selection extends TargetSelection<any, any, any>
5128
+ > = {
5129
+ readonly target: (to: TargetSelector<States, StateId>) => Selection
5130
+ readonly resolve?: TransitionResolver<Events, Emits, Context, Selection>
5131
+ readonly cases?: never
5132
+ readonly otherwise?: never
5133
+ readonly reenter?: Reenter
5134
+ }
5135
+
5136
+ export type TransitionCaseInput<
5137
+ States extends StateSchemas,
5138
+ Events extends ReadonlyArray<TaggedSchema>,
5139
+ Emits extends ReadonlyArray<TaggedSchema>,
5140
+ StateId extends StateNodeIdentifier<States>,
5141
+ Context,
5142
+ Selection extends TargetSelection<any, any, any>,
5143
+ Match
5144
+ > = {
5145
+ readonly title: string
5146
+ readonly when: (context: TransitionWhenContext<Context>) => Option.Option<Match>
5147
+ readonly target: (to: TargetSelector<States, StateId>) => Selection
5148
+ readonly resolve?: TransitionResolver<Events, Emits, Context, Selection, NoInfer<Match>>
5149
+ readonly cases?: never
5150
+ readonly otherwise?: never
5151
+ readonly reenter?: never
5152
+ }
4625
5153
 
4626
- interface AnyLogicSource {
4627
- initial(...args: ReadonlyArray<any>): Effect.Effect<any, any, any>
4628
- run(...args: ReadonlyArray<any>): Effect.Effect<any, any, any>
5154
+ export type TransitionConditionalInput<
5155
+ States extends StateSchemas,
5156
+ Events extends ReadonlyArray<TaggedSchema>,
5157
+ Emits extends ReadonlyArray<TaggedSchema>,
5158
+ StateId extends StateNodeIdentifier<States>,
5159
+ Context,
5160
+ Reenter extends boolean,
5161
+ Cases extends readonly [TransitionCaseTyped, ...ReadonlyArray<TransitionCaseTyped>],
5162
+ OtherwiseSelection extends TargetSelection<any, any, any>
5163
+ > = {
5164
+ readonly target?: never
5165
+ readonly resolve?: never
5166
+ readonly cases: (branch: TransitionCaseConstructor<States, Events, Emits, StateId, Context>) => Cases
5167
+ readonly otherwise: TransitionDirectInput<
5168
+ States,
5169
+ Events,
5170
+ Emits,
5171
+ StateId,
5172
+ Context,
5173
+ never,
5174
+ OtherwiseSelection
5175
+ >
5176
+ readonly reenter?: Reenter
4629
5177
  }
4630
5178
 
5179
+ export type InvokeTransition<
5180
+ States extends StateSchemas,
5181
+ Events extends ReadonlyArray<TaggedSchema>,
5182
+ Emits extends ReadonlyArray<TaggedSchema>,
5183
+ StateId extends StateNodeIdentifier<States>,
5184
+ Context
5185
+ > = TransitionConfig<States, Events, Emits, StateId, Context, true>
5186
+
5187
+ export type InvokeSource<Value, Context> = Value | ((context: Context) => Value)
5188
+
4631
5189
  /** Inline state-owned work that runs for the lifetime of its active state. */
4632
5190
  export interface InvokeOwned<
4633
5191
  States extends StateSchemas,
@@ -4662,10 +5220,9 @@ export declare namespace Machine {
4662
5220
  & (
4663
5221
  | {
4664
5222
  readonly id: string
4665
- readonly effect: InvokeSource<
4666
- Effect.Effect<any, any, any>,
4667
- InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
4668
- >
5223
+ readonly effect: (
5224
+ context: InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5225
+ ) => Effect.Effect<any, any, any>
4669
5226
  readonly after?: never
4670
5227
  readonly logic?: never
4671
5228
  readonly child?: never
@@ -4692,7 +5249,7 @@ export declare namespace Machine {
4692
5249
  readonly id: string
4693
5250
  readonly address: string
4694
5251
  readonly logic: InvokeSource<
4695
- AnyLogicSource,
5252
+ Logic<any, any, any, any, any, any>,
4696
5253
  InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
4697
5254
  >
4698
5255
  readonly effect?: never
@@ -4749,61 +5306,7 @@ export declare namespace Machine {
4749
5306
  : { readonly onFailure?: never }
4750
5307
  : never
4751
5308
 
4752
- export type EffectInvokeArgs<
4753
- States extends StateSchemas,
4754
- Events extends ReadonlyArray<TaggedSchema>,
4755
- Emits extends ReadonlyArray<TaggedSchema>,
4756
- StateId extends StateIdentifier<States>,
4757
- Fx extends Effect.Effect<any, any, any>,
4758
- Source = Fx,
4759
- InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4760
- ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4761
- > =
4762
- & {
4763
- readonly id: InvokeLifecycleId
4764
- readonly effect: Source
4765
- readonly after?: never
4766
- readonly logic?: never
4767
- readonly child?: never
4768
- readonly address?: never
4769
- readonly onSnapshot?: never
4770
- }
4771
- & InvokeDoneRequirement<
4772
- Effect.Success<NoInfer<Fx>>,
4773
- InvokeTransition<
4774
- States,
4775
- Events,
4776
- Emits,
4777
- InvokeDoneContext<
4778
- States,
4779
- Events,
4780
- Emits,
4781
- StateId,
4782
- Effect.Success<NoInfer<Fx>>,
4783
- InputEvents,
4784
- ParentEvents
4785
- >
4786
- >
4787
- >
4788
- & InvokeFailureRequirement<
4789
- Effect.Error<NoInfer<Fx>>,
4790
- InvokeTransition<
4791
- States,
4792
- Events,
4793
- Emits,
4794
- InvokeFailureContext<
4795
- States,
4796
- Events,
4797
- Emits,
4798
- StateId,
4799
- Effect.Error<NoInfer<Fx>>,
4800
- InputEvents,
4801
- ParentEvents
4802
- >
4803
- >
4804
- >
4805
-
4806
- export type TimerInvokeArgs<
5309
+ export type TimerInvokeArgs<
4807
5310
  States extends StateSchemas,
4808
5311
  Events extends ReadonlyArray<TaggedSchema>,
4809
5312
  Emits extends ReadonlyArray<TaggedSchema>,
@@ -4826,6 +5329,7 @@ export declare namespace Machine {
4826
5329
  States,
4827
5330
  Events,
4828
5331
  Emits,
5332
+ StateId,
4829
5333
  InvokeDoneContext<States, Events, Emits, StateId, void, InputEvents, ParentEvents>
4830
5334
  >
4831
5335
  }
@@ -4857,35 +5361,38 @@ export declare namespace Machine {
4857
5361
  States,
4858
5362
  Events,
4859
5363
  Emits,
5364
+ StateId,
4860
5365
  InvokeSnapshotContext<
4861
5366
  States,
4862
5367
  Events,
4863
5368
  Emits,
4864
5369
  StateId,
4865
- ChildState,
4866
- ChildError,
4867
- ChildOutput,
5370
+ NoInfer<ChildState>,
5371
+ NoInfer<ChildError>,
5372
+ NoInfer<ChildOutput>,
4868
5373
  InputEvents,
4869
5374
  ParentEvents
4870
5375
  >
4871
5376
  >
4872
5377
  }
4873
5378
  & InvokeDoneRequirement<
4874
- ChildOutput,
5379
+ NoInfer<ChildOutput>,
4875
5380
  InvokeTransition<
4876
5381
  States,
4877
5382
  Events,
4878
5383
  Emits,
4879
- InvokeDoneContext<States, Events, Emits, StateId, ChildOutput, InputEvents, ParentEvents>
5384
+ StateId,
5385
+ InvokeDoneContext<States, Events, Emits, StateId, NoInfer<ChildOutput>, InputEvents, ParentEvents>
4880
5386
  >
4881
5387
  >
4882
5388
  & InvokeFailureRequirement<
4883
- ChildError,
5389
+ NoInfer<ChildError>,
4884
5390
  InvokeTransition<
4885
5391
  States,
4886
5392
  Events,
4887
5393
  Emits,
4888
- InvokeFailureContext<States, Events, Emits, StateId, ChildError, InputEvents, ParentEvents>
5394
+ StateId,
5395
+ InvokeFailureContext<States, Events, Emits, StateId, NoInfer<ChildError>, InputEvents, ParentEvents>
4889
5396
  >
4890
5397
  >
4891
5398
 
@@ -4917,6 +5424,7 @@ export declare namespace Machine {
4917
5424
  States,
4918
5425
  Events,
4919
5426
  Emits,
5427
+ StateId,
4920
5428
  InvokeSnapshotContext<
4921
5429
  States,
4922
5430
  Events,
@@ -4942,6 +5450,7 @@ export declare namespace Machine {
4942
5450
  States,
4943
5451
  Events,
4944
5452
  Emits,
5453
+ StateId,
4945
5454
  InvokeDoneContext<
4946
5455
  States,
4947
5456
  Events,
@@ -4959,6 +5468,7 @@ export declare namespace Machine {
4959
5468
  States,
4960
5469
  Events,
4961
5470
  Emits,
5471
+ StateId,
4962
5472
  InvokeFailureContext<
4963
5473
  States,
4964
5474
  Events,
@@ -4971,12 +5481,24 @@ export declare namespace Machine {
4971
5481
  >
4972
5482
  >
4973
5483
 
4974
- export type LogicStateOf<Value> = Value extends Logic<infer State, any, any, any, any, any> ? State : never
4975
- export type LogicEventOf<Value> = Value extends Logic<any, infer Event, any, any, any, any> ? Event : never
4976
- export type LogicErrorOf<Value> = Value extends Logic<any, any, infer Error, any, any, any> ? Error : never
4977
- export type LogicServicesOf<Value> = Value extends Logic<any, any, any, infer Services, any, any> ? Services : never
4978
- export type LogicOutputOf<Value> = Value extends Logic<any, any, any, any, infer Output, any> ? Output : never
4979
- export type LogicInitialErrorOf<Value> = Value extends Logic<any, any, any, any, any, infer Error> ? Error : never
5484
+ type LogicInitialEffectOf<Value> = Value extends { readonly initial: infer Initial } ?
5485
+ Initial extends (...args: ReadonlyArray<any>) => infer Result ? Result : never
5486
+ : never
5487
+
5488
+ type LogicRunEffectOf<Value> = Value extends { readonly run: infer Run } ?
5489
+ Run extends (...args: ReadonlyArray<any>) => infer Result ? Result : never
5490
+ : never
5491
+
5492
+ export type LogicStateOf<Value> = Effect.Success<LogicInitialEffectOf<Value>>
5493
+ export type LogicEventOf<Value> = Value extends { readonly initial: infer Initial } ?
5494
+ Initial extends (scope: infer LogicScope, ...args: ReadonlyArray<any>) => any ?
5495
+ LogicScope extends Logic.Scope<infer Event> ? Event : never
5496
+ : never
5497
+ : never
5498
+ export type LogicErrorOf<Value> = Effect.Error<LogicRunEffectOf<Value>>
5499
+ export type LogicServicesOf<Value> = Effect.Services<LogicInitialEffectOf<Value> | LogicRunEffectOf<Value>>
5500
+ export type LogicOutputOf<Value> = Effect.Success<LogicRunEffectOf<Value>>
5501
+ export type LogicInitialErrorOf<Value> = Effect.Error<LogicInitialEffectOf<Value>>
4980
5502
 
4981
5503
  type ContextualInvokeConfig<
4982
5504
  States extends StateSchemas,
@@ -4988,13 +5510,14 @@ export declare namespace Machine {
4988
5510
  Raw
4989
5511
  > = Raw extends InvokeTyped<any, any, any, any, any> ? unknown
4990
5512
  : Raw extends { readonly effect: infer Source } ?
4991
- InvokeResolvedSource<Source> extends infer Fx extends Effect.Effect<any, any, any> ?
5513
+ InvokeFactoryResult<Source> extends infer Fx extends Effect.Effect<any, any, any> ?
4992
5514
  & InvokeDoneRequirement<
4993
5515
  Effect.Success<Fx>,
4994
5516
  InvokeTransition<
4995
5517
  States,
4996
5518
  Events,
4997
5519
  Emits,
5520
+ StateId,
4998
5521
  InvokeDoneContext<States, Events, Emits, StateId, Effect.Success<Fx>, InputEvents, ParentEvents>
4999
5522
  >
5000
5523
  >
@@ -5004,6 +5527,7 @@ export declare namespace Machine {
5004
5527
  States,
5005
5528
  Events,
5006
5529
  Emits,
5530
+ StateId,
5007
5531
  InvokeFailureContext<States, Events, Emits, StateId, Effect.Error<Fx>, InputEvents, ParentEvents>
5008
5532
  >
5009
5533
  >
@@ -5014,6 +5538,7 @@ export declare namespace Machine {
5014
5538
  States,
5015
5539
  Events,
5016
5540
  Emits,
5541
+ StateId,
5017
5542
  InvokeDoneContext<States, Events, Emits, StateId, void, InputEvents, ParentEvents>
5018
5543
  >
5019
5544
  readonly onFailure?: never
@@ -5027,6 +5552,7 @@ export declare namespace Machine {
5027
5552
  States,
5028
5553
  Events,
5029
5554
  Emits,
5555
+ StateId,
5030
5556
  InvokeDoneContext<States, Events, Emits, StateId, InvokeOutput<Raw>, InputEvents, ParentEvents>
5031
5557
  >
5032
5558
  >
@@ -5036,6 +5562,7 @@ export declare namespace Machine {
5036
5562
  States,
5037
5563
  Events,
5038
5564
  Emits,
5565
+ StateId,
5039
5566
  InvokeFailureContext<
5040
5567
  States,
5041
5568
  Events,
@@ -5058,6 +5585,7 @@ export declare namespace Machine {
5058
5585
  States,
5059
5586
  Events,
5060
5587
  Emits,
5588
+ StateId,
5061
5589
  InvokeSnapshotContext<
5062
5590
  States,
5063
5591
  Events,
@@ -5080,6 +5608,7 @@ export declare namespace Machine {
5080
5608
  States,
5081
5609
  Events,
5082
5610
  Emits,
5611
+ StateId,
5083
5612
  InvokeDoneContext<
5084
5613
  States,
5085
5614
  Events,
@@ -5097,6 +5626,7 @@ export declare namespace Machine {
5097
5626
  States,
5098
5627
  Events,
5099
5628
  Emits,
5629
+ StateId,
5100
5630
  InvokeFailureContext<
5101
5631
  States,
5102
5632
  Events,
@@ -5119,6 +5649,7 @@ export declare namespace Machine {
5119
5649
  States,
5120
5650
  Events,
5121
5651
  Emits,
5652
+ StateId,
5122
5653
  InvokeSnapshotContext<
5123
5654
  States,
5124
5655
  Events,
@@ -5209,57 +5740,40 @@ export declare namespace Machine {
5209
5740
  readonly invoke?:
5210
5741
  | InvokeDefinition<States, Events, Emits, StateId, InputEvents, ParentEvents>
5211
5742
  | TypedInvokeDefinition
5212
- readonly always?:
5213
- | ((
5214
- context: AlwaysContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
5215
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5216
- ) => HandlerResult<States, any, any>)
5217
- | {
5218
- /** Statically declared upper bound of possible target paths. */
5219
- readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
5220
- readonly transition: (
5221
- context: AlwaysContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
5222
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5223
- ) => HandlerResult<States, any, any>
5224
- }
5225
- readonly onDone?:
5226
- | ((
5227
- context: DoneContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
5228
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5229
- ) => HandlerResult<States, any, any>)
5230
- | {
5231
- /** Statically declared upper bound of possible target paths. */
5232
- readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
5233
- readonly transition: (
5234
- context: DoneContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
5235
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5236
- ) => HandlerResult<States, any, any>
5237
- }
5743
+ readonly always?: TransitionConfig<
5744
+ States,
5745
+ Events,
5746
+ Emits,
5747
+ StateId,
5748
+ AlwaysContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5749
+ >
5750
+ readonly onDone?: TransitionConfig<
5751
+ States,
5752
+ Events,
5753
+ Emits,
5754
+ StateId,
5755
+ DoneContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5756
+ >
5238
5757
  readonly on?: {
5239
- readonly [EventTag in TagOf<Events[number]>]?:
5240
- | ((
5241
- context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R, InputEvents, ParentEvents>,
5242
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5243
- ) => HandlerResult<States, any, any>)
5244
- | {
5245
- readonly reenter?: boolean
5246
- /**
5247
- * Upper bound of state or history paths this handler may target.
5248
- * Returning `target.none()` is always permitted. Declaring a parent state also
5249
- * permits concrete descendant targets below it.
5250
- */
5251
- readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
5252
- readonly transition: (
5253
- context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R, InputEvents, ParentEvents>,
5254
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5255
- ) => HandlerResult<States, any, any>
5256
- }
5758
+ readonly [EventTag in TagOf<Events[number]>]?: TransitionConfig<
5759
+ States,
5760
+ Events,
5761
+ Emits,
5762
+ StateId,
5763
+ HandlerContext<States, Events, Emits, StateId, EventTag, E, R, InputEvents, ParentEvents>,
5764
+ true
5765
+ >
5257
5766
  }
5258
- readonly initial?: StateInitialHandler<States, Events, Emits, StateId, InputEvents, ParentEvents>
5767
+ readonly initialize?: StateInitializeHandler<States, Events, Emits, StateId, InputEvents, ParentEvents>
5259
5768
  } & ActiveOutputHandlerConfig<States, Events, StateId>
5260
5769
 
5261
- /** Values supplied when a statechart implicitly enters a state's initial children. */
5262
- export type StateInitialValue<
5770
+ /**
5771
+ * Values constructed for a state's schema-valued direct initial children.
5772
+ *
5773
+ * @category utility types
5774
+ * @since 0.13.0
5775
+ */
5776
+ export type StateInitializeValue<
5263
5777
  States extends StateSchemas,
5264
5778
  StateId extends StateIdentifier<States>
5265
5779
  > = NodeByIdentifier<States, StateId> extends infer Node ?
@@ -5270,23 +5784,92 @@ export declare namespace Machine {
5270
5784
  }
5271
5785
  : Node extends { readonly states: infer Children extends StateSchemas; readonly initial: infer Initial } ?
5272
5786
  Initial extends ActiveStateKey<Children> ? NodeSchema<Children[Initial]> extends never ? never
5273
- : NodeValue<Children[Initial]>
5787
+ : { readonly [Key in Initial]: NodeValue<Children[Initial]> }
5274
5788
  : never
5275
5789
  : never
5276
5790
  : never
5277
5791
 
5278
- /** Context passed to an implicit child-state initializer. */
5279
- export type StateInitialContext<
5792
+ type StateInitializeCompoundBuilder<Node, Initial extends PropertyKey> = Node extends {
5793
+ readonly states: infer Children extends StateSchemas
5794
+ } ? Initial extends ActiveStateKey<Children> ? NodeSchema<Children[Initial]> extends never ? never
5795
+ : NodeBuilderMethod<
5796
+ Children[Initial],
5797
+ readonly [value: NodeValue<Children[Initial]>],
5798
+ SnapshotBuilderComplete<{ readonly [Key in Initial]: NodeValue<Children[Initial]> }>,
5799
+ readonly [input: NodeMakeInput<Children[Initial]>],
5800
+ SnapshotBuilderComplete<{ readonly [Key in Initial]: NodeValue<Children[Initial]> }, true>
5801
+ >
5802
+ : never
5803
+ : never
5804
+
5805
+ type StateInitializeParallelBuilder<
5806
+ Children extends StateSchemas,
5807
+ Remaining extends ActiveStateKey<Children> = {
5808
+ readonly [Key in ActiveStateKey<Children>]: NodeSchema<Children[Key]> extends never ? never : Key
5809
+ }[ActiveStateKey<Children>],
5810
+ Values = {},
5811
+ Constructed extends boolean = false
5812
+ > =
5813
+ & SnapshotBuilderComplete<Values, Constructed>
5814
+ & {
5815
+ readonly [Key in Remaining]: NodeBuilderMethod<
5816
+ Children[Key],
5817
+ readonly [value: NodeValue<Children[Key]>],
5818
+ StateInitializeParallelBuilder<
5819
+ Children,
5820
+ Exclude<Remaining, Key>,
5821
+ Values & { readonly [Region in Key]: NodeValue<Children[Key]> },
5822
+ Constructed
5823
+ >,
5824
+ readonly [input: NodeMakeInput<Children[Key]>],
5825
+ StateInitializeParallelBuilder<
5826
+ Children,
5827
+ Exclude<Remaining, Key>,
5828
+ Values & { readonly [Region in Key]: NodeValue<Children[Key]> },
5829
+ true
5830
+ >
5831
+ >
5832
+ }
5833
+
5834
+ /**
5835
+ * Builder bound to the direct initial child or regions owned by a state.
5836
+ *
5837
+ * @category utility types
5838
+ * @since 0.13.0
5839
+ */
5840
+ export type StateInitializeBuilder<
5841
+ States extends StateSchemas,
5842
+ StateId extends StateIdentifier<States>,
5843
+ Node = NodeByIdentifier<States, StateId>
5844
+ > = Node extends { readonly type: "parallel"; readonly states: infer Children extends StateSchemas } ?
5845
+ StateInitializeParallelBuilder<Children>
5846
+ : Node extends { readonly initial: infer Initial } ? StateInitializeCompoundBuilder<Node, Initial & PropertyKey>
5847
+ : never
5848
+
5849
+ /**
5850
+ * Context passed to an implicit child-state initializer.
5851
+ *
5852
+ * @category models
5853
+ * @since 0.13.0
5854
+ */
5855
+ export type StateInitializeContext<
5280
5856
  States extends StateSchemas,
5281
5857
  Events extends ReadonlyArray<TaggedSchema>,
5282
5858
  Emits extends ReadonlyArray<TaggedSchema>,
5283
5859
  StateId extends StateIdentifier<States>,
5284
5860
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5285
5861
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5286
- > = StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
5862
+ > = StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents> & {
5863
+ readonly builder: StateInitializeBuilder<States, StateId>
5864
+ }
5287
5865
 
5288
- /** Initial child value implementation for a compound or parallel state. */
5289
- export type StateInitialHandler<
5866
+ /**
5867
+ * Initial child value implementation for a compound or parallel state.
5868
+ *
5869
+ * @category models
5870
+ * @since 0.13.0
5871
+ */
5872
+ export type StateInitializeHandler<
5290
5873
  States extends StateSchemas,
5291
5874
  Events extends ReadonlyArray<TaggedSchema>,
5292
5875
  Emits extends ReadonlyArray<TaggedSchema>,
@@ -5294,9 +5877,9 @@ export declare namespace Machine {
5294
5877
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5295
5878
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5296
5879
  > = (
5297
- context: StateInitialContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
5880
+ context: StateInitializeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
5298
5881
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5299
- ) => StateInitialValue<States, StateId>
5882
+ ) => SnapshotBuilderComplete<StateInitializeValue<States, StateId>, boolean>
5300
5883
 
5301
5884
  /** Context used only when a history node has no previously captured record. */
5302
5885
  export interface HistoryDefaultContext<
@@ -5351,14 +5934,13 @@ export declare namespace Machine {
5351
5934
  InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5352
5935
  ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5353
5936
  > {
5354
- readonly choice: {
5355
- /** Statically inspectable upper bound of every possible target. */
5356
- readonly targets: readonly [StateNodeIdentifier<States>, ...ReadonlyArray<StateNodeIdentifier<States>>]
5357
- readonly transition: (
5358
- context: ChoiceContext<States, Events, Emits, ChoiceId, InputEvents, ParentEvents>,
5359
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
5360
- ) => ChoiceResult<States, any, any>
5361
- }
5937
+ readonly choice: TransitionConfig<
5938
+ States,
5939
+ Events,
5940
+ Emits,
5941
+ ChoiceId,
5942
+ ChoiceContext<States, Events, Emits, ChoiceId, InputEvents, ParentEvents>
5943
+ >
5362
5944
  readonly entry?: never
5363
5945
  readonly exit?: never
5364
5946
  readonly invoke?: never
@@ -5366,7 +5948,7 @@ export declare namespace Machine {
5366
5948
  readonly on?: never
5367
5949
  readonly onDone?: never
5368
5950
  readonly output?: never
5369
- readonly initial?: never
5951
+ readonly initialize?: never
5370
5952
  }
5371
5953
 
5372
5954
  /**
@@ -5589,7 +6171,7 @@ export declare namespace Machine {
5589
6171
  | "entry"
5590
6172
  | "exit"
5591
6173
  | "history"
5592
- | "initial"
6174
+ | "initialize"
5593
6175
  | "invoke"
5594
6176
  | "on"
5595
6177
  | "onDone"
@@ -5696,62 +6278,73 @@ export declare namespace Machine {
5696
6278
  }
5697
6279
  : unknown
5698
6280
 
5699
- type TransitionResultTargetPath<Result> = IsAny<Result> extends true ? never
5700
- : Result extends Effect.Effect<infer Success, any, any> ? TransitionResultTargetPath<Success>
5701
- : Result extends StateConstruction<infer Constructed> ? TransitionResultTargetPath<Constructed>
5702
- : Result extends { readonly path: infer Path extends string } ? Path
5703
- : never
5704
-
5705
- type DeclaredTransitionTarget<Transition> = Transition extends {
5706
- readonly targets: infer Targets extends ReadonlyArray<string>
5707
- } ? Targets[number]
6281
+ type TransitionResultInitialTargetPath<Result> = IsAny<Result> extends true ? never
6282
+ : Result extends Effect.Effect<infer Success, any, any> ? TransitionResultInitialTargetPath<Success>
6283
+ : Result extends StateConstruction<infer Constructed> ? TransitionResultInitialTargetPath<Constructed>
6284
+ : Result extends {
6285
+ readonly [Topology.InitialTargetTypeId]: typeof Topology.InitialTargetTypeId
6286
+ readonly _tag: "InitialTarget"
6287
+ } ? Result extends { readonly path: infer Path extends string } ? Path : never
5708
6288
  : never
5709
6289
 
5710
- type ExcludeDeclaredTransitionTarget<Returned, Declared extends string> = Returned extends string ?
5711
- Returned extends Declared | `${Declared}.${string}` ? never : Returned
5712
- : never
6290
+ type HandlerConfigInitialTargetPath<Config> = TransitionResultInitialTargetPath<
6291
+ | EventHandlerReturn<Config>
6292
+ | AlwaysReturn<Config>
6293
+ | DoneReturn<Config>
6294
+ | ChoiceReturn<Config>
6295
+ | InvokeOutcomeReturn<InvokeReturn<Config>>
6296
+ >
5713
6297
 
5714
- type UndeclaredTransitionTarget<Transition> = "targets" extends keyof Transition ? ExcludeDeclaredTransitionTarget<
5715
- TransitionResultTargetPath<EventTransitionReturn<Transition>>,
5716
- DeclaredTransitionTarget<Transition>
6298
+ type RequiredInitializersForTargetPath<
6299
+ AllStates extends StateSchemas,
6300
+ Path
6301
+ > = string extends Path ? never : Path extends StateIdentifier<AllStates> ? InitializerClosureForNode<
6302
+ AllStates,
6303
+ NodeByIdentifier<AllStates, Path>,
6304
+ Path
5717
6305
  >
6306
+ : never
6307
+
6308
+ type HandlerInitializeValidationAtPath<Config, Path extends string> = HandlerConfigAtPath<Config, Path> extends
6309
+ infer StateConfig ? [StateConfig] extends [never] ? HandlerValidationAtPath<Path, {
6310
+ readonly initialize: HandlerValidationError<
6311
+ "State requires initialize because a transition enters its declared initial configuration",
6312
+ Path
6313
+ >
6314
+ }>
6315
+ : "initialize" extends keyof StateConfig ? never
6316
+ : HandlerValidationAtPath<Path, {
6317
+ readonly initialize: HandlerValidationError<
6318
+ "State requires initialize because a transition enters its declared initial configuration",
6319
+ Path
6320
+ >
6321
+ }>
5718
6322
  : never
5719
6323
 
5720
- type HandlerOnTargetValidationErrors<StateId extends string, On> = {
5721
- readonly [
5722
- EventTag in keyof On as [UndeclaredTransitionTarget<On[EventTag]>] extends [never] ? never
5723
- : EventTag
5724
- ]: HandlerValidationError<
5725
- "Transition returns a target not listed in targets",
5726
- StateId,
5727
- readonly [event: EventTag, target: UndeclaredTransitionTarget<On[EventTag]>]
5728
- >
5729
- }
6324
+ type HandlerInitialTargetValidationForPaths<
6325
+ AllStates extends StateSchemas,
6326
+ Config,
6327
+ Required
6328
+ > = Types.UnionToIntersection<
6329
+ Required extends string ? HandlerInitializeValidationAtPath<Config, Required> : unknown
6330
+ >
5730
6331
 
5731
- type HandlerOnTargetValidation<StateId extends string, Config> = "on" extends keyof Config ?
5732
- Config extends { readonly on?: infer On } ? HandlerOnTargetValidationErrors<
5733
- StateId,
5734
- NonNullable<On>
5735
- > extends infer Errors ? keyof Errors extends never ? unknown
5736
- : { readonly on: Errors }
5737
- : never
5738
- : unknown
5739
- : unknown
6332
+ type HandlerTreeInitialTargetPath<Config> = string extends keyof Config ? never
6333
+ : Config extends object ? {
6334
+ readonly [Key in keyof Config]:
6335
+ | HandlerConfigInitialTargetPath<HandlerConfigPart<Config[Key]>>
6336
+ | (Config[Key] extends { readonly states: infer Children } ? HandlerTreeInitialTargetPath<Children> : never)
6337
+ }[keyof Config]
6338
+ : never
5740
6339
 
5741
- type HandlerDirectTargetValidation<
5742
- StateId extends string,
5743
- Config,
5744
- Trigger extends "always" | "onDone",
5745
- Transition = Config extends { readonly [Key in Trigger]?: infer Value } ? NonNullable<Value> : never,
5746
- Undeclared extends string = UndeclaredTransitionTarget<Transition>
5747
- > = Trigger extends keyof Config ? [Undeclared] extends [never] ? unknown
5748
- : {
5749
- readonly [Key in Trigger]: HandlerValidationError<
5750
- "Transition returns a target not listed in targets",
5751
- StateId,
5752
- readonly [trigger: Trigger, target: Undeclared]
5753
- >
5754
- }
6340
+ type HandlerInitialTargetValidation<
6341
+ AllStates extends StateSchemas,
6342
+ Config
6343
+ > = HandlerTreeInitialTargetPath<Config> extends infer TargetPath ? HandlerInitialTargetValidationForPaths<
6344
+ AllStates,
6345
+ Config,
6346
+ RequiredInitializersForTargetPath<AllStates, TargetPath>
6347
+ >
5755
6348
  : unknown
5756
6349
 
5757
6350
  type HandlerChildrenValidation<
@@ -5809,6 +6402,19 @@ export declare namespace Machine {
5809
6402
  : unknown
5810
6403
  : unknown)
5811
6404
 
6405
+ type HandlerRequiredHistoryInitializeValidation<
6406
+ AllStates extends StateSchemas,
6407
+ StateId extends StateIdentifier<AllStates>,
6408
+ Config
6409
+ > = [HistoryIdentifier<AllStates>] extends [never] ? unknown
6410
+ : StateId extends RequiredHistoryInitializers<AllStates> ? "initialize" extends keyof Config ? unknown : {
6411
+ readonly initialize: HandlerValidationError<
6412
+ "State requires initialize for shallow history restoration",
6413
+ StateId
6414
+ >
6415
+ }
6416
+ : unknown
6417
+
5812
6418
  type HandlerNodeValidation<
5813
6419
  AllStates extends StateSchemas,
5814
6420
  Node,
@@ -5820,17 +6426,14 @@ export declare namespace Machine {
5820
6426
  AvailableOutputStates extends StateIdentifier<AllStates>
5821
6427
  > = StateId extends ChoiceIdentifier<AllStates> ?
5822
6428
  & HandlerChoiceUnknownConfigKeyValidation<StateId, Config>
5823
- & HandlerChoiceTargetValidation<StateId, Config>
5824
6429
  & HandlerRuntimeValidation<Events, Emits, StateId, Config>
5825
6430
  : StateId extends StateIdentifier<AllStates> ?
5826
6431
  & HandlerUnknownConfigKeyValidation<StateId, Config>
5827
6432
  & HandlerOnKeyValidation<Events, StateId, Config>
5828
- & HandlerOnTargetValidation<StateId, Config>
5829
- & HandlerDirectTargetValidation<StateId, Config, "always">
5830
- & HandlerDirectTargetValidation<StateId, Config, "onDone">
5831
6433
  & HandlerInvokeParentEventsValidation<InputEvents, StateId, Config>
5832
6434
  & HandlerChildrenValidation<Node, StateId, Config>
5833
6435
  & HandlerOutputRequirementValidation<AllStates, StateId, AvailableOutputStates, Config>
6436
+ & HandlerRequiredHistoryInitializeValidation<AllStates, StateId, Config>
5834
6437
  & HandlerRuntimeValidation<Events, Emits, StateId, Config>
5835
6438
  : unknown
5836
6439
 
@@ -5846,17 +6449,6 @@ export declare namespace Machine {
5846
6449
  >
5847
6450
  }
5848
6451
 
5849
- type HandlerChoiceTargetValidation<StateId extends string, Config> = Config extends {
5850
- readonly choice: infer Choice
5851
- } ? [UndeclaredTransitionTarget<Choice>] extends [never] ? unknown : {
5852
- readonly choice: HandlerValidationError<
5853
- "Choice resolver returns a target not listed in targets",
5854
- StateId,
5855
- UndeclaredTransitionTarget<Choice>
5856
- >
5857
- }
5858
- : unknown
5859
-
5860
6452
  type HandlerInvokeParentEventsValidation<
5861
6453
  Events extends ReadonlyArray<TaggedSchema>,
5862
6454
  StateId extends string,
@@ -5944,7 +6536,7 @@ export declare namespace Machine {
5944
6536
  AllStates extends StateSchemas,
5945
6537
  StateId extends StateIdentifier<AllStates>,
5946
6538
  Config
5947
- > = StateId extends RequiredHistoryInitializers<AllStates> ? "initial" extends keyof Config ? true : false : true
6539
+ > = StateId extends RequiredHistoryInitializers<AllStates> ? "initialize" extends keyof Config ? true : false : true
5948
6540
 
5949
6541
  type HandlerHasRequiredHistoryDefaults<Node, Config> = Node extends {
5950
6542
  readonly states: infer Children extends StateSchemas
@@ -5984,7 +6576,6 @@ export declare namespace Machine {
5984
6576
  | Effect.Error<DoneReturn<Config>>
5985
6577
  | Effect.Error<StateActionReturn<Config, "entry">>
5986
6578
  | Effect.Error<StateActionReturn<Config, "exit">>
5987
- | Effect.Error<StateInitialReturn<Config>>
5988
6579
  | Effect.Error<HistoryDefaultReturn<Config>>
5989
6580
  | Effect.Error<ChoiceReturn<Config>>
5990
6581
  | InvokeError<Config>
@@ -6107,6 +6698,11 @@ export declare namespace Machine {
6107
6698
  StateIdentifier<States>
6108
6699
  >
6109
6700
  >
6701
+ & ([StateIdentifier<States>] extends [UnhandledStates] ? HandlerInitialTargetValidation<
6702
+ States,
6703
+ NoInfer<Config>
6704
+ >
6705
+ : unknown)
6110
6706
  ): HandleTreeResult<
6111
6707
  States,
6112
6708
  Events,
@@ -6153,19 +6749,14 @@ export declare namespace Machine {
6153
6749
  > = Readonly<
6154
6750
  Record<
6155
6751
  PropertyKey,
6156
- | ((
6157
- context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R>,
6158
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6159
- ) => HandlerResult<States, E, R>)
6160
- | {
6161
- readonly reenter?: boolean
6162
- /** Statically declared upper bound of possible target paths. */
6163
- readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
6164
- readonly transition: (
6165
- context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R>,
6166
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6167
- ) => HandlerResult<States, E, R>
6168
- }
6752
+ TransitionConfig<
6753
+ States,
6754
+ Events,
6755
+ Emits,
6756
+ StateId,
6757
+ HandlerContext<States, Events, Emits, StateId, EventTag, E, R>,
6758
+ true
6759
+ >
6169
6760
  >
6170
6761
  >
6171
6762
 
@@ -6193,30 +6784,20 @@ export declare namespace Machine {
6193
6784
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6194
6785
  ) => StateActionResult<E, R>
6195
6786
  readonly invoke?: InvokeDefinition<States, Events, Emits, StateId>
6196
- readonly always?:
6197
- | ((
6198
- context: AlwaysContext<States, Events, Emits, StateId>,
6199
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6200
- ) => HandlerResult<States, E, R>)
6201
- | {
6202
- readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
6203
- readonly transition: (
6204
- context: AlwaysContext<States, Events, Emits, StateId>,
6205
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6206
- ) => HandlerResult<States, E, R>
6207
- }
6208
- readonly onDone?:
6209
- | ((
6210
- context: DoneContext<States, Events, Emits, StateId>,
6211
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6212
- ) => HandlerResult<States, E, R>)
6213
- | {
6214
- readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
6215
- readonly transition: (
6216
- context: DoneContext<States, Events, Emits, StateId>,
6217
- enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
6218
- ) => HandlerResult<States, E, R>
6219
- }
6787
+ readonly always?: TransitionConfig<
6788
+ States,
6789
+ Events,
6790
+ Emits,
6791
+ StateId,
6792
+ AlwaysContext<States, Events, Emits, StateId>
6793
+ >
6794
+ readonly onDone?: TransitionConfig<
6795
+ States,
6796
+ Events,
6797
+ Emits,
6798
+ StateId,
6799
+ DoneContext<States, Events, Emits, StateId>
6800
+ >
6220
6801
  readonly output?:
6221
6802
  | ((context: FinalOutputContext<States, Events, StateId>) => unknown)
6222
6803
  | ((context: ParallelOutputContext<States, Events, StateId>) => unknown)
@@ -6301,15 +6882,15 @@ export const isFinal: <
6301
6882
  * **When to use**
6302
6883
  *
6303
6884
  * Use when you want to pass a state tree to `make` and also get typed
6304
- * snapshot builders for initial states and tests.
6885
+ * snapshot matching and access helpers.
6305
6886
  *
6306
6887
  * **Details**
6307
6888
  *
6308
6889
  * The returned `states` property is the same object passed to `defineStates`.
6309
- * The returned `initial` builder creates snapshots without user-authored path
6310
- * strings and enforces compound and parallel initial-state rules. Active nodes
6311
- * may omit `schema` when they own no value; those builders expose `.from(...)`
6312
- * and still participate fully in targeting, matching, and snapshots.
6890
+ * `Machine.make` derives its initial target selector from that tree and
6891
+ * enforces compound and parallel initial-state rules. Active nodes may omit
6892
+ * `schema` when they own no value and still participate fully in targeting,
6893
+ * matching, and snapshots.
6313
6894
  *
6314
6895
  * **Example** (Atomic initial snapshot)
6315
6896
  *
@@ -6324,7 +6905,10 @@ export const isFinal: <
6324
6905
  * Machine.make({
6325
6906
  * states: States.states,
6326
6907
  * events: Machine.events(),
6327
- * initial: () => States.initial.idle.from()
6908
+ * initial: {
6909
+ * target: (to) => to.idle(),
6910
+ * resolve: ({ target }) => target.from()
6911
+ * }
6328
6912
  * })
6329
6913
  * ```
6330
6914
  *
@@ -6333,6 +6917,20 @@ export const isFinal: <
6333
6917
  */
6334
6918
  export const defineStates: DefineStates = internal.defineStates
6335
6919
 
6920
+ type InitialDirectInput<
6921
+ States extends Machine.StateSchemas,
6922
+ Input,
6923
+ Selection extends Machine.TargetSelection<any, any, any>
6924
+ > = {
6925
+ readonly target: (to: Machine.InitialSelector<States>) => Selection
6926
+ readonly resolve?: (context: {
6927
+ readonly input: Input
6928
+ readonly target: Machine.SelectionBuilder<Selection>
6929
+ }) => Machine.SelectedTargetResult<Selection>
6930
+ readonly cases?: never
6931
+ readonly otherwise?: never
6932
+ }
6933
+
6336
6934
  type MakeConfig<
6337
6935
  States extends Machine.StateSchemas,
6338
6936
  InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
@@ -6357,7 +6955,7 @@ type MakeConfig<
6357
6955
  readonly emittedEvents?: Machine.EventProtocol<"emitted", Emits>
6358
6956
  readonly parentEvents?: Machine.EventProtocol<"public", ParentEvents>
6359
6957
  readonly input?: Input
6360
- readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>
6958
+ readonly initial: unknown
6361
6959
  }
6362
6960
 
6363
6961
  type MakeResult<
@@ -6395,9 +6993,15 @@ interface Make {
6395
6993
  InitialE = never,
6396
6994
  InitialR = never,
6397
6995
  const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
6398
- const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
6996
+ const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
6997
+ const InitialSelection extends Machine.TargetSelection<any, any, any> = Machine.TargetSelection<any, any, any>
6399
6998
  >(
6400
- config: MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>,
6999
+ config:
7000
+ & Omit<
7001
+ MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>,
7002
+ "initial"
7003
+ >
7004
+ & { readonly initial: InitialDirectInput<States, Input["Type"], InitialSelection> },
6401
7005
  ..._validation: ValidateDefinedStates<NoInfer<States>>
6402
7006
  ): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>
6403
7007
  <
@@ -6455,18 +7059,24 @@ interface Make {
6455
7059
  * const counter = Machine.make({
6456
7060
  * states: States.states,
6457
7061
  * events: Events,
6458
- * initial: () => States.initial.Count(new Count({ value: 0 }))
7062
+ * initial: {
7063
+ * target: (to) => to.Count(),
7064
+ * resolve: ({ target }) => target(new Count({ value: 0 }))
7065
+ * }
6459
7066
  * }).handle({
6460
7067
  * Count: {
6461
7068
  * on: {
6462
- * Increment: ({ event, state }) =>
6463
- * States.initial.Count(new Count({ value: state.value + event.by }))
7069
+ * Increment: Machine.transition({
7070
+ * target: (to) => to.full.Count(),
7071
+ * resolve: ({ event, state, target }) =>
7072
+ * target(new Count({ value: state.value + event.by }))
7073
+ * })
6464
7074
  * }
6465
7075
  * }
6466
7076
  * })
6467
7077
  * ```
6468
7078
  *
6469
- * @see {@link defineStates} for typed initial snapshot builders.
7079
+ * @see {@link defineStates} for typed state-tree helpers.
6470
7080
  * @category constructors
6471
7081
  * @since 0.4.0
6472
7082
  */
@@ -6593,7 +7203,10 @@ export const emittedEvents: {
6593
7203
  * const machine = Machine.make({
6594
7204
  * states: States.states,
6595
7205
  * events: Machine.events(),
6596
- * initial: () => States.initial.Idle.from()
7206
+ * initial: {
7207
+ * target: (to) => to.Idle(),
7208
+ * resolve: ({ target }) => target.from()
7209
+ * }
6597
7210
  * }).handle({ Idle: {} })
6598
7211
  *
6599
7212
  * const encoded = Effect.gen(function*() {
@@ -6675,7 +7288,10 @@ export const encodeSnapshot: <
6675
7288
  * const machine = Machine.make({
6676
7289
  * states: States.states,
6677
7290
  * events: Machine.events(),
6678
- * initial: () => States.initial.Idle.from()
7291
+ * initial: {
7292
+ * target: (to) => to.Idle(),
7293
+ * resolve: ({ target }) => target.from()
7294
+ * }
6679
7295
  * }).handle({ Idle: {} })
6680
7296
  *
6681
7297
  * const roundTrip = Effect.gen(function*() {
@@ -6728,7 +7344,7 @@ export const decodeSnapshot: <
6728
7344
  Machine.SnapshotDecodingServices<States>
6729
7345
  > = internal.decodeSnapshot as any
6730
7346
 
6731
- type DynamicEffectInvokeSource<
7347
+ type EffectInvokeSource<
6732
7348
  States extends Machine.StateSchemas,
6733
7349
  Events extends ReadonlyArray<Machine.TaggedSchema>,
6734
7350
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
@@ -6746,7 +7362,7 @@ type DynamicEffectInvokeSource<
6746
7362
  readonly onSnapshot?: never
6747
7363
  }
6748
7364
 
6749
- type DynamicEffectDoneHandler<
7365
+ type EffectDoneHandler<
6750
7366
  States extends Machine.StateSchemas,
6751
7367
  Events extends ReadonlyArray<Machine.TaggedSchema>,
6752
7368
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
@@ -6756,6 +7372,7 @@ type DynamicEffectDoneHandler<
6756
7372
  States,
6757
7373
  Events,
6758
7374
  Emits,
7375
+ StateId,
6759
7376
  Machine.InvokeDoneContext<
6760
7377
  States,
6761
7378
  Events,
@@ -6767,7 +7384,7 @@ type DynamicEffectDoneHandler<
6767
7384
  >
6768
7385
  >
6769
7386
 
6770
- type DynamicEffectFailureHandler<
7387
+ type EffectFailureHandler<
6771
7388
  States extends Machine.StateSchemas,
6772
7389
  Events extends ReadonlyArray<Machine.TaggedSchema>,
6773
7390
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
@@ -6777,6 +7394,7 @@ type DynamicEffectFailureHandler<
6777
7394
  States,
6778
7395
  Events,
6779
7396
  Emits,
7397
+ StateId,
6780
7398
  Machine.InvokeFailureContext<
6781
7399
  States,
6782
7400
  Events,
@@ -6788,7 +7406,7 @@ type DynamicEffectFailureHandler<
6788
7406
  >
6789
7407
  >
6790
7408
 
6791
- type DynamicEffectInvokeResult<
7409
+ type EffectInvokeResult<
6792
7410
  States extends Machine.StateSchemas,
6793
7411
  Events extends ReadonlyArray<Machine.TaggedSchema>,
6794
7412
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
@@ -6805,7 +7423,7 @@ type DynamicEffectInvokeResult<
6805
7423
 
6806
7424
  type InvokeChannelIsNever<Value> = IsAny<Value> extends true ? false : [Value] extends [never] ? true : false
6807
7425
 
6808
- type BoundDynamicEffectInvokeSource<
7426
+ type BoundEffectInvokeSource<
6809
7427
  States extends Machine.StateSchemas,
6810
7428
  Events extends ReadonlyArray<Machine.TaggedSchema>,
6811
7429
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
@@ -6825,7 +7443,7 @@ type BoundDynamicEffectInvokeSource<
6825
7443
  readonly onSnapshot?: never
6826
7444
  }
6827
7445
 
6828
- type BoundDynamicEffectDoneHandler<
7446
+ type BoundEffectDoneHandler<
6829
7447
  States extends Machine.StateSchemas,
6830
7448
  Events extends ReadonlyArray<Machine.TaggedSchema>,
6831
7449
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
@@ -6837,6 +7455,7 @@ type BoundDynamicEffectDoneHandler<
6837
7455
  States,
6838
7456
  Events,
6839
7457
  Emits,
7458
+ StateId,
6840
7459
  Machine.InvokeDoneContext<
6841
7460
  States,
6842
7461
  Events,
@@ -6848,7 +7467,7 @@ type BoundDynamicEffectDoneHandler<
6848
7467
  >
6849
7468
  >
6850
7469
 
6851
- type BoundDynamicEffectFailureHandler<
7470
+ type BoundEffectFailureHandler<
6852
7471
  States extends Machine.StateSchemas,
6853
7472
  Events extends ReadonlyArray<Machine.TaggedSchema>,
6854
7473
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
@@ -6860,6 +7479,7 @@ type BoundDynamicEffectFailureHandler<
6860
7479
  States,
6861
7480
  Events,
6862
7481
  Emits,
7482
+ StateId,
6863
7483
  Machine.InvokeFailureContext<
6864
7484
  States,
6865
7485
  Events,
@@ -6871,7 +7491,7 @@ type BoundDynamicEffectFailureHandler<
6871
7491
  >
6872
7492
  >
6873
7493
 
6874
- type BoundDynamicEffectInvokeResult<
7494
+ type BoundEffectInvokeResult<
6875
7495
  States extends Machine.StateSchemas,
6876
7496
  Events extends ReadonlyArray<Machine.TaggedSchema>,
6877
7497
  Emits extends ReadonlyArray<Machine.TaggedSchema>,
@@ -6888,6 +7508,162 @@ type BoundDynamicEffectInvokeResult<
6888
7508
  never
6889
7509
  >
6890
7510
 
7511
+ /** Type evidence retained for a case created by a conditional transition's local builder. */
7512
+ interface TransitionCaseTyped {
7513
+ readonly [TransitionCaseTypeId]: true
7514
+ }
7515
+
7516
+ /** Locally bound constructor that preserves each conditional transition case independently. */
7517
+ interface TransitionCaseConstructor<
7518
+ States extends Machine.StateSchemas,
7519
+ Events extends ReadonlyArray<Machine.TaggedSchema>,
7520
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
7521
+ StateId extends Machine.StateNodeIdentifier<States>,
7522
+ Context
7523
+ > {
7524
+ <
7525
+ const Selection extends Machine.TargetSelection<any, any, any>,
7526
+ const Match
7527
+ >(
7528
+ config: Machine.TransitionCaseInput<States, Events, Emits, StateId, Context, Selection, Match>
7529
+ ):
7530
+ & Machine.TransitionCaseInput<States, Events, Emits, StateId, Context, Selection, Match>
7531
+ & TransitionCaseTyped
7532
+ }
7533
+
7534
+ /**
7535
+ * Captures one exact transition topology while preserving handler inference.
7536
+ *
7537
+ * @category constructors
7538
+ * @since 0.14.0
7539
+ */
7540
+ type ConditionalTransitionResult<
7541
+ States extends Machine.StateSchemas,
7542
+ Events extends ReadonlyArray<Machine.TaggedSchema>,
7543
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
7544
+ StateId extends Machine.StateNodeIdentifier<States>,
7545
+ Context,
7546
+ Reenter extends boolean,
7547
+ Cases extends readonly [
7548
+ TransitionCaseTyped,
7549
+ ...ReadonlyArray<TransitionCaseTyped>
7550
+ ],
7551
+ OtherwiseSelection extends Machine.TargetSelection<any, any, any>
7552
+ > =
7553
+ & Omit<
7554
+ Machine.TransitionConditionalInput<
7555
+ States,
7556
+ Events,
7557
+ Emits,
7558
+ StateId,
7559
+ Context,
7560
+ Reenter,
7561
+ Cases,
7562
+ OtherwiseSelection
7563
+ >,
7564
+ "cases"
7565
+ >
7566
+ & { readonly cases: Cases }
7567
+ & Machine.TransitionTyped<States, Events, Emits, StateId, Context, Reenter>
7568
+
7569
+ /**
7570
+ * Contextually types direct and conditional transition definitions.
7571
+ *
7572
+ * @category constructors
7573
+ * @since 0.14.0
7574
+ */
7575
+ export interface TransitionConstructor {
7576
+ <
7577
+ const States extends Machine.StateSchemas,
7578
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
7579
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
7580
+ StateId extends Machine.StateNodeIdentifier<States>,
7581
+ Context,
7582
+ const Selection extends Machine.TargetSelection<any, any, any>,
7583
+ Reenter extends boolean = never
7584
+ >(
7585
+ config: Machine.TransitionDirectInput<States, Events, Emits, StateId, Context, Reenter, Selection>
7586
+ ):
7587
+ & Machine.TransitionDirectInput<States, Events, Emits, StateId, Context, Reenter, Selection>
7588
+ & (Machine.SelectionKind<Selection> extends "none" ? Machine.TargetlessTransitionTyped
7589
+ : Machine.TransitionTyped<States, Events, Emits, StateId, Context, Reenter>)
7590
+ <
7591
+ const States extends Machine.StateSchemas,
7592
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
7593
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
7594
+ StateId extends Machine.StateNodeIdentifier<States>,
7595
+ Context,
7596
+ const Cases extends readonly [
7597
+ TransitionCaseTyped,
7598
+ ...ReadonlyArray<TransitionCaseTyped>
7599
+ ],
7600
+ const OtherwiseSelection extends Machine.TargetSelection<any, any, any>,
7601
+ Reenter extends boolean = never
7602
+ >(
7603
+ config: Machine.TransitionConditionalInput<
7604
+ States,
7605
+ Events,
7606
+ Emits,
7607
+ StateId,
7608
+ Context,
7609
+ Reenter,
7610
+ Cases,
7611
+ OtherwiseSelection
7612
+ >
7613
+ ): ConditionalTransitionResult<
7614
+ States,
7615
+ Events,
7616
+ Emits,
7617
+ StateId,
7618
+ Context,
7619
+ Reenter,
7620
+ Cases,
7621
+ OtherwiseSelection
7622
+ >
7623
+ }
7624
+
7625
+ /**
7626
+ * Defines a statically inspectable transition.
7627
+ *
7628
+ * It is required for every event, lifecycle, choice, and invocation transition
7629
+ * so the destination selected by `target` can contextually type the
7630
+ * corresponding resolver. Conditional transitions use the locally bound
7631
+ * `branch` constructor so every case independently infers its matched value and
7632
+ * selected destination, with no limit on the number of cases.
7633
+ *
7634
+ * ```ts
7635
+ * Machine.transition({
7636
+ * target: (to) => to.full.Ready(),
7637
+ * resolve: ({ target }) => target.from()
7638
+ * })
7639
+ *
7640
+ * Machine.transition({
7641
+ * cases: (branch) => [
7642
+ * branch({
7643
+ * title: "has cached data",
7644
+ * when: ({ event }) => event.cached,
7645
+ * target: (to) => to.full.Ready(),
7646
+ * resolve: ({ match, target }) => target.from({ data: match })
7647
+ * })
7648
+ * ],
7649
+ * otherwise: {
7650
+ * target: (to) => to.full.Loading(),
7651
+ * resolve: ({ target }) => target.from()
7652
+ * }
7653
+ * })
7654
+ * ```
7655
+ *
7656
+ * @category constructors
7657
+ * @since 0.14.0
7658
+ */
7659
+ export const transition: TransitionConstructor = ((config: unknown) => {
7660
+ if (!hasProperty(config, "cases") || typeof config.cases !== "function") return config
7661
+ return {
7662
+ ...config,
7663
+ cases: config.cases(<Case extends object>(branch: Case): Case => branch)
7664
+ }
7665
+ }) as TransitionConstructor
7666
+
6891
7667
  /**
6892
7668
  * Machine-bound invocation constructor that preserves the owning machine's
6893
7669
  * public input and parent protocols in every invocation callback.
@@ -6909,9 +7685,9 @@ export interface Invoker<
6909
7685
  ) => Effect.Effect<unknown, unknown, unknown>
6910
7686
  >(
6911
7687
  config:
6912
- & BoundDynamicEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
7688
+ & BoundEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
6913
7689
  & {
6914
- readonly onDone: BoundDynamicEffectDoneHandler<
7690
+ readonly onDone: BoundEffectDoneHandler<
6915
7691
  States,
6916
7692
  Events,
6917
7693
  Emits,
@@ -6920,7 +7696,7 @@ export interface Invoker<
6920
7696
  StateId,
6921
7697
  Source
6922
7698
  >
6923
- readonly onFailure: BoundDynamicEffectFailureHandler<
7699
+ readonly onFailure: BoundEffectFailureHandler<
6924
7700
  States,
6925
7701
  Events,
6926
7702
  Emits,
@@ -6937,7 +7713,7 @@ export interface Invoker<
6937
7713
  "onFailure must be omitted when the Effect error is never"
6938
7714
  ]
6939
7715
  : []
6940
- ): BoundDynamicEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
7716
+ ): BoundEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
6941
7717
  <
6942
7718
  StateId extends Machine.StateIdentifier<States>,
6943
7719
  const Source extends (
@@ -6945,9 +7721,9 @@ export interface Invoker<
6945
7721
  ) => Effect.Effect<unknown, never, unknown>
6946
7722
  >(
6947
7723
  config:
6948
- & BoundDynamicEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
7724
+ & BoundEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
6949
7725
  & {
6950
- readonly onDone: BoundDynamicEffectDoneHandler<
7726
+ readonly onDone: BoundEffectDoneHandler<
6951
7727
  States,
6952
7728
  Events,
6953
7729
  Emits,
@@ -6962,7 +7738,7 @@ export interface Invoker<
6962
7738
  "onDone must be omitted when the Effect output is never"
6963
7739
  ]
6964
7740
  : []
6965
- ): BoundDynamicEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
7741
+ ): BoundEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
6966
7742
  <
6967
7743
  StateId extends Machine.StateIdentifier<States>,
6968
7744
  const Source extends (
@@ -6970,10 +7746,10 @@ export interface Invoker<
6970
7746
  ) => Effect.Effect<never, unknown, unknown>
6971
7747
  >(
6972
7748
  config:
6973
- & BoundDynamicEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
7749
+ & BoundEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
6974
7750
  & {
6975
7751
  readonly onDone?: never
6976
- readonly onFailure: BoundDynamicEffectFailureHandler<
7752
+ readonly onFailure: BoundEffectFailureHandler<
6977
7753
  States,
6978
7754
  Events,
6979
7755
  Emits,
@@ -6987,7 +7763,7 @@ export interface Invoker<
6987
7763
  "onFailure must be omitted when the Effect error is never"
6988
7764
  ]
6989
7765
  : []
6990
- ): BoundDynamicEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
7766
+ ): BoundEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
6991
7767
  <
6992
7768
  StateId extends Machine.StateIdentifier<States>,
6993
7769
  const Source extends (
@@ -6995,105 +7771,85 @@ export interface Invoker<
6995
7771
  ) => Effect.Effect<never, never, unknown>
6996
7772
  >(
6997
7773
  config:
6998
- & BoundDynamicEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
7774
+ & BoundEffectInvokeSource<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
6999
7775
  & {
7000
7776
  readonly onDone?: never
7001
7777
  readonly onFailure?: never
7002
7778
  }
7003
- ): BoundDynamicEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
7779
+ ): BoundEffectInvokeResult<States, Events, Emits, InputEvents, ParentEvents, StateId, Source>
7780
+ <StateId extends Machine.StateIdentifier<States>, const Config extends object>(
7781
+ config: Config & Machine.TimerInvokeArgs<States, Events, Emits, StateId, InputEvents, ParentEvents>
7782
+ ): Config & Machine.InvokeOwned<States, Events, Emits, StateId> & Machine.InvokeTyped<void, never, never, never>
7004
7783
  <
7005
7784
  StateId extends Machine.StateIdentifier<States>,
7006
- const Fx extends Effect.Effect<any, any, any>,
7007
- const Config extends object
7785
+ const Source extends (
7786
+ context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
7787
+ ) => unknown,
7788
+ Address extends ChildAddress<never>
7008
7789
  >(
7009
7790
  config:
7010
- & Config
7011
- & Machine.EffectInvokeArgs<
7791
+ & { readonly logic: Source }
7792
+ & Machine.LogicInvokeArgs<
7012
7793
  States,
7013
7794
  Events,
7014
7795
  Emits,
7015
7796
  StateId,
7016
- Fx,
7017
- Fx,
7797
+ Machine.LogicStateOf<ReturnType<Source>>,
7798
+ Machine.LogicEventOf<ReturnType<Source>>,
7799
+ Machine.LogicErrorOf<ReturnType<Source>>,
7800
+ Machine.LogicServicesOf<ReturnType<Source>>,
7801
+ Machine.LogicOutputOf<ReturnType<Source>>,
7802
+ Machine.LogicInitialErrorOf<ReturnType<Source>>,
7803
+ Address,
7804
+ Source,
7018
7805
  InputEvents,
7019
7806
  ParentEvents
7020
- >
7021
- ):
7022
- & Config
7023
- & Machine.InvokeOwned<States, Events, Emits, StateId>
7024
- & Machine.InvokeTyped<Effect.Success<Fx>, Effect.Error<Fx>, Effect.Services<Fx>, never>
7025
- <StateId extends Machine.StateIdentifier<States>, const Config extends object>(
7026
- config: Config & Machine.TimerInvokeArgs<States, Events, Emits, StateId, InputEvents, ParentEvents>
7027
- ): Config & Machine.InvokeOwned<States, Events, Emits, StateId> & Machine.InvokeTyped<void, never, never, never>
7028
- <
7029
- StateId extends Machine.StateIdentifier<States>,
7030
- ChildState,
7031
- ChildEvent,
7032
- ChildError,
7033
- ChildRequirements,
7034
- ChildOutput,
7035
- ChildInitialError,
7036
- Address extends ChildAddress<never>
7037
- >(
7038
- config: Machine.LogicInvokeArgs<
7039
- States,
7040
- Events,
7041
- Emits,
7042
- StateId,
7043
- ChildState,
7044
- ChildEvent,
7045
- ChildError,
7046
- ChildRequirements,
7047
- ChildOutput,
7048
- ChildInitialError,
7049
- Address,
7050
- (context: Machine.InvokeContext<States, Events, Emits, StateId, InputEvents, ParentEvents>) => Logic<
7051
- ChildState,
7052
- ChildEvent,
7053
- ChildError,
7054
- ChildRequirements,
7055
- ChildOutput,
7056
- ChildInitialError
7057
7807
  >,
7058
- InputEvents,
7059
- ParentEvents
7060
- >
7808
+ ..._validation: ReturnType<Source> extends { readonly initial: unknown; readonly run: unknown } ? [] : [
7809
+ "logic factory must return Machine.Logic"
7810
+ ]
7061
7811
  ):
7062
7812
  & Machine.InvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
7063
- & Machine.InvokeTyped<ChildOutput, ChildError, ChildRequirements, ChildInitialError>
7813
+ & Machine.InvokeTyped<
7814
+ Machine.LogicOutputOf<ReturnType<Source>>,
7815
+ Machine.LogicErrorOf<ReturnType<Source>>,
7816
+ Machine.LogicServicesOf<ReturnType<Source>>,
7817
+ Machine.LogicInitialErrorOf<ReturnType<Source>>
7818
+ >
7064
7819
  <
7065
7820
  StateId extends Machine.StateIdentifier<States>,
7066
- ChildState,
7067
- ChildEvent,
7068
- ChildError,
7069
- ChildRequirements,
7070
- ChildOutput,
7071
- ChildInitialError,
7072
- Address extends ChildAddress<never>,
7073
- const Config extends object
7821
+ const Source,
7822
+ Address extends ChildAddress<never>
7074
7823
  >(
7075
7824
  config:
7076
- & Config
7825
+ & { readonly logic: Source }
7077
7826
  & Machine.LogicInvokeArgs<
7078
7827
  States,
7079
7828
  Events,
7080
7829
  Emits,
7081
7830
  StateId,
7082
- ChildState,
7083
- ChildEvent,
7084
- ChildError,
7085
- ChildRequirements,
7086
- ChildOutput,
7087
- ChildInitialError,
7831
+ Machine.LogicStateOf<Source>,
7832
+ Machine.LogicEventOf<Source>,
7833
+ Machine.LogicErrorOf<Source>,
7834
+ Machine.LogicServicesOf<Source>,
7835
+ Machine.LogicOutputOf<Source>,
7836
+ Machine.LogicInitialErrorOf<Source>,
7088
7837
  Address,
7089
- Logic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>,
7838
+ Source,
7090
7839
  InputEvents,
7091
7840
  ParentEvents
7092
- >
7841
+ >,
7842
+ ..._validation: Source extends { readonly initial: unknown; readonly run: unknown } ? [] : [
7843
+ "logic must implement Machine.Logic"
7844
+ ]
7093
7845
  ):
7094
- & Config
7095
- & Machine.InvokeOwned<States, Events, Emits, StateId>
7096
- & Machine.InvokeTyped<ChildOutput, ChildError, ChildRequirements, ChildInitialError>
7846
+ & Machine.InvokeConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
7847
+ & Machine.InvokeTyped<
7848
+ Machine.LogicOutputOf<Source>,
7849
+ Machine.LogicErrorOf<Source>,
7850
+ Machine.LogicServicesOf<Source>,
7851
+ Machine.LogicInitialErrorOf<Source>
7852
+ >
7097
7853
  <
7098
7854
  StateId extends Machine.StateIdentifier<States>,
7099
7855
  const Child extends ChildMachine.Any,
@@ -7133,12 +7889,13 @@ export interface Invoker<
7133
7889
  * is required only when the source has a typed failure channel.
7134
7890
  *
7135
7891
  * This constructor is an identity at runtime, but preserves lifecycle callback
7136
- * inference through published declarations. Effects and durations may be
7137
- * supplied directly or derived from the owning state's entry context. Dynamic
7138
- * Effect sources infer the owner context, output, error, and service channels
7139
- * together without a return annotation. Logic invocations require both a
7140
- * lifecycle `id` and a typed communication `address`. Child descriptors already
7141
- * own their identity, so `id` and `address` must not be repeated.
7892
+ * inference through published declarations. Effect factories run when their
7893
+ * owning state is entered and infer the owner context, output, error, and
7894
+ * service channels together without a return annotation. Durations may be
7895
+ * supplied directly or derived from the owning state's entry context. Logic
7896
+ * invocations require both a lifecycle `id` and a typed communication
7897
+ * `address`. Child descriptors already own their identity, so `id` and
7898
+ * `address` must not be repeated.
7142
7899
  *
7143
7900
  * Owner references are intentionally non-sendable here because this standalone
7144
7901
  * constructor does not know the owning definition. When a callback sends to
@@ -7149,12 +7906,19 @@ export interface Invoker<
7149
7906
  * ```ts
7150
7907
  * invoke: Machine.invoke({
7151
7908
  * id: "load",
7152
- * effect: Effect.tryPromise({
7153
- * try: () => fetch("/api/data").then((response) => response.json()),
7154
- * catch: (cause) => new LoadError({ cause })
7909
+ * effect: () =>
7910
+ * Effect.tryPromise({
7911
+ * try: () => fetch("/api/data").then((response) => response.json()),
7912
+ * catch: (cause) => new LoadError({ cause })
7913
+ * }),
7914
+ * onDone: Machine.transition({
7915
+ * target: (to) => to.full.Ready(),
7916
+ * resolve: ({ output, target }) => target.from({ data: output })
7155
7917
  * }),
7156
- * onDone: ({ output, target }) => target.full.Ready({ data: output }),
7157
- * onFailure: ({ error, target }) => target.full.Failed({ error })
7918
+ * onFailure: Machine.transition({
7919
+ * target: (to) => to.full.Failed(),
7920
+ * resolve: ({ error, target }) => target.from({ error })
7921
+ * })
7158
7922
  * })
7159
7923
  * ```
7160
7924
  *
@@ -7172,10 +7936,10 @@ export const invoke: {
7172
7936
  ) => Effect.Effect<unknown, unknown, unknown>
7173
7937
  >(
7174
7938
  config:
7175
- & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
7939
+ & EffectInvokeSource<States, Events, Emits, StateId, Source>
7176
7940
  & {
7177
- readonly onDone: DynamicEffectDoneHandler<States, Events, Emits, StateId, Source>
7178
- readonly onFailure: DynamicEffectFailureHandler<States, Events, Emits, StateId, Source>
7941
+ readonly onDone: EffectDoneHandler<States, Events, Emits, StateId, Source>
7942
+ readonly onFailure: EffectFailureHandler<States, Events, Emits, StateId, Source>
7179
7943
  },
7180
7944
  ..._validation: InvokeChannelIsNever<Effect.Success<ReturnType<Source>>> extends true ? [
7181
7945
  "onDone must be omitted when the Effect output is never"
@@ -7184,7 +7948,7 @@ export const invoke: {
7184
7948
  "onFailure must be omitted when the Effect error is never"
7185
7949
  ]
7186
7950
  : []
7187
- ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
7951
+ ): EffectInvokeResult<States, Events, Emits, StateId, Source>
7188
7952
  <
7189
7953
  const States extends Machine.StateSchemas,
7190
7954
  const Events extends ReadonlyArray<Machine.TaggedSchema>,
@@ -7195,16 +7959,16 @@ export const invoke: {
7195
7959
  ) => Effect.Effect<unknown, never, unknown>
7196
7960
  >(
7197
7961
  config:
7198
- & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
7962
+ & EffectInvokeSource<States, Events, Emits, StateId, Source>
7199
7963
  & {
7200
- readonly onDone: DynamicEffectDoneHandler<States, Events, Emits, StateId, Source>
7964
+ readonly onDone: EffectDoneHandler<States, Events, Emits, StateId, Source>
7201
7965
  readonly onFailure?: never
7202
7966
  },
7203
7967
  ..._validation: InvokeChannelIsNever<Effect.Success<ReturnType<Source>>> extends true ? [
7204
7968
  "onDone must be omitted when the Effect output is never"
7205
7969
  ]
7206
7970
  : []
7207
- ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
7971
+ ): EffectInvokeResult<States, Events, Emits, StateId, Source>
7208
7972
  <
7209
7973
  const States extends Machine.StateSchemas,
7210
7974
  const Events extends ReadonlyArray<Machine.TaggedSchema>,
@@ -7215,16 +7979,16 @@ export const invoke: {
7215
7979
  ) => Effect.Effect<never, unknown, unknown>
7216
7980
  >(
7217
7981
  config:
7218
- & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
7982
+ & EffectInvokeSource<States, Events, Emits, StateId, Source>
7219
7983
  & {
7220
7984
  readonly onDone?: never
7221
- readonly onFailure: DynamicEffectFailureHandler<States, Events, Emits, StateId, Source>
7985
+ readonly onFailure: EffectFailureHandler<States, Events, Emits, StateId, Source>
7222
7986
  },
7223
7987
  ..._validation: InvokeChannelIsNever<Effect.Error<ReturnType<Source>>> extends true ? [
7224
7988
  "onFailure must be omitted when the Effect error is never"
7225
7989
  ]
7226
7990
  : []
7227
- ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
7991
+ ): EffectInvokeResult<States, Events, Emits, StateId, Source>
7228
7992
  <
7229
7993
  const States extends Machine.StateSchemas,
7230
7994
  const Events extends ReadonlyArray<Machine.TaggedSchema>,
@@ -7235,32 +7999,12 @@ export const invoke: {
7235
7999
  ) => Effect.Effect<never, never, unknown>
7236
8000
  >(
7237
8001
  config:
7238
- & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
8002
+ & EffectInvokeSource<States, Events, Emits, StateId, Source>
7239
8003
  & {
7240
8004
  readonly onDone?: never
7241
8005
  readonly onFailure?: never
7242
8006
  }
7243
- ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
7244
- <
7245
- const States extends Machine.StateSchemas,
7246
- const Events extends ReadonlyArray<Machine.TaggedSchema>,
7247
- const Emits extends ReadonlyArray<Machine.TaggedSchema>,
7248
- StateId extends Machine.StateIdentifier<States>,
7249
- const Fx extends Effect.Effect<any, any, any>,
7250
- const Config extends object
7251
- >(
7252
- config:
7253
- & Config
7254
- & Machine.EffectInvokeArgs<States, Events, Emits, StateId, Fx, Fx, readonly [], readonly []>
7255
- ):
7256
- & Config
7257
- & Machine.InvokeOwned<States, Events, Emits, StateId>
7258
- & Machine.InvokeTyped<
7259
- Effect.Success<Fx>,
7260
- Effect.Error<Fx>,
7261
- Effect.Services<Fx>,
7262
- never
7263
- >
8007
+ ): EffectInvokeResult<States, Events, Emits, StateId, Source>
7264
8008
  <
7265
8009
  const States extends Machine.StateSchemas,
7266
8010
  const Events extends ReadonlyArray<Machine.TaggedSchema>,
@@ -7407,7 +8151,10 @@ export const invoke: {
7407
8151
  * const machine = Machine.make({
7408
8152
  * states: States.states,
7409
8153
  * events: Machine.events(),
7410
- * initial: () => States.initial.Idle.from()
8154
+ * initial: {
8155
+ * target: (to) => to.Idle(),
8156
+ * resolve: ({ target }) => target.from()
8157
+ * }
7411
8158
  * }).handle({ Idle: {} })
7412
8159
  *
7413
8160
  * const initialState = Effect.map(Machine.planInitial(machine), (plan) => plan.state)
@@ -7514,6 +8261,20 @@ export const stateNodes: <M extends Machine.Any>(machine: M) => ReadonlyArray<
7514
8261
  >
7515
8262
  > = internal.stateNodes
7516
8263
 
8264
+ /**
8265
+ * Returns the statically selected root entry used during machine startup.
8266
+ *
8267
+ * This function does not execute the initial resolver or require machine
8268
+ * input. `kind: "initial"` means that the selected root enters its declared
8269
+ * initial configuration.
8270
+ *
8271
+ * @category getters
8272
+ * @since 0.14.0
8273
+ */
8274
+ export const initialDefinition: <M extends Machine.Any>(machine: M) => Machine.InitialDefinition<
8275
+ Machine.RootStateIdentifier<Machine.StateIdentifier<Machine.States<M>>>
8276
+ > = internal.initialDefinition
8277
+
7517
8278
  /**
7518
8279
  * Returns every registered transition handler in state definition order.
7519
8280
  *
@@ -7521,9 +8282,9 @@ export const stateNodes: <M extends Machine.Any>(machine: M) => ReadonlyArray<
7521
8282
  *
7522
8283
  * Event handlers retain their handler-key order within each source state and
7523
8284
  * are followed by eventless and completion handlers. This function does not
7524
- * execute handlers. Object-form event, eventless, and completion handlers with
7525
- * a `targets` declaration expose those possible paths; handlers without one
7526
- * remain dynamic.
8285
+ * execute predicates or resolvers. Every direct, conditional, fallback, and
8286
+ * targetless branch exposes the destination selected by its required static
8287
+ * `target` declaration.
7527
8288
  *
7528
8289
  * @category getters
7529
8290
  * @since 0.4.0
@@ -7646,8 +8407,21 @@ export const enabled: <
7646
8407
  * const machine = Machine.make({
7647
8408
  * states: States.states,
7648
8409
  * events: Machine.events(Toggle),
7649
- * initial: () => States.initial.Off.from()
7650
- * }).handle({ Off: { on: { Toggle: () => States.initial.On.from() } }, On: {} })
8410
+ * initial: {
8411
+ * target: (to) => to.Off(),
8412
+ * resolve: ({ target }) => target.from()
8413
+ * }
8414
+ * }).handle({
8415
+ * Off: {
8416
+ * on: {
8417
+ * Toggle: Machine.transition({
8418
+ * target: (to) => to.full.On(),
8419
+ * resolve: ({ target }) => target.from()
8420
+ * })
8421
+ * }
8422
+ * },
8423
+ * On: {}
8424
+ * })
7651
8425
  *
7652
8426
  * const nextState = Effect.gen(function*() {
7653
8427
  * const initial = yield* Machine.planInitial(machine)
@@ -7780,41 +8554,6 @@ export const logic: <
7780
8554
  ) => Effect.Effect<Output, Error, Requirements>
7781
8555
  }) => Logic<State, Event, Error, Requirements | InitialRequirements, Output, InitialError> = internal.logic
7782
8556
 
7783
- /**
7784
- * Creates child process logic from an initial state and a transition function.
7785
- *
7786
- * **When to use**
7787
- *
7788
- * Use when a child process only needs sequential event-driven state updates and
7789
- * does not need direct control over intermediate snapshots or child ownership.
7790
- *
7791
- * **Details**
7792
- *
7793
- * Each received event runs the transition Effect against the latest state. The
7794
- * resulting state is published before the next queued event is processed.
7795
- *
7796
- * **Example**
7797
- *
7798
- * ```ts
7799
- * import { Effect } from "effect"
7800
- * import { Machine } from "@typeonce/effect-machine"
7801
- *
7802
- * const counter = Machine.transition(
7803
- * 0,
7804
- * (count, event: { readonly by: number }) => Effect.succeed(count + event.by)
7805
- * )
7806
- * ```
7807
- *
7808
- * Use an inline `invoke` with an `effect` source for one-shot work.
7809
- * @see {@link logic} for direct process lifecycle control.
7810
- * @category constructors
7811
- * @since 0.4.0
7812
- */
7813
- export const transition: <State, Event, Error = never, Requirements = never>(
7814
- initial: State,
7815
- transition: (state: State, event: Event) => Effect.Effect<State, Error, Requirements>
7816
- ) => Logic<State, Event, Error, Requirements, never> = internal.transition
7817
-
7818
8557
  /**
7819
8558
  * Creates a typed descriptor for a complete child machine.
7820
8559
  *
@@ -8051,7 +8790,10 @@ export const prepare: <
8051
8790
  * const machine = Machine.make({
8052
8791
  * states: States.states,
8053
8792
  * events: Machine.events(),
8054
- * initial: () => States.initial.Idle.from()
8793
+ * initial: {
8794
+ * target: (to) => to.Idle(),
8795
+ * resolve: ({ target }) => target.from()
8796
+ * }
8055
8797
  * }).handle({ Idle: {} })
8056
8798
  *
8057
8799
  * const state = Effect.gen(function*() {
@@ -8161,7 +8903,10 @@ export const start: <
8161
8903
  * const machine = Machine.make({
8162
8904
  * states: States.states,
8163
8905
  * events: Machine.events(),
8164
- * initial: () => States.initial.Idle.from()
8906
+ * initial: {
8907
+ * target: (to) => to.Idle(),
8908
+ * resolve: ({ target }) => target.from()
8909
+ * }
8165
8910
  * }).handle({ Idle: {} })
8166
8911
  *
8167
8912
  * const resumed = Effect.gen(function*() {