@typeonce/effect-machine 0.8.0 → 0.10.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 (108) hide show
  1. package/README.md +180 -40
  2. package/dist/Machine.d.ts +659 -481
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +86 -222
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/activities.d.ts +5 -12
  7. package/dist/internal/machine/activities.d.ts.map +1 -1
  8. package/dist/internal/machine/activities.js +47 -17
  9. package/dist/internal/machine/activities.js.map +1 -1
  10. package/dist/internal/machine/atom.d.ts +6 -2
  11. package/dist/internal/machine/atom.d.ts.map +1 -1
  12. package/dist/internal/machine/atom.js +5 -0
  13. package/dist/internal/machine/atom.js.map +1 -1
  14. package/dist/internal/machine/cluster.d.ts.map +1 -1
  15. package/dist/internal/machine/cluster.js +2 -1
  16. package/dist/internal/machine/cluster.js.map +1 -1
  17. package/dist/internal/machine/command.js +2 -2
  18. package/dist/internal/machine/command.js.map +1 -1
  19. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  20. package/dist/internal/machine/commandRuntime.js +6 -3
  21. package/dist/internal/machine/commandRuntime.js.map +1 -1
  22. package/dist/internal/machine/configuration.d.ts +8 -1
  23. package/dist/internal/machine/configuration.d.ts.map +1 -1
  24. package/dist/internal/machine/configuration.js +9 -4
  25. package/dist/internal/machine/configuration.js.map +1 -1
  26. package/dist/internal/machine/errors.d.ts +1 -1
  27. package/dist/internal/machine/errors.d.ts.map +1 -1
  28. package/dist/internal/machine/executionPlan.d.ts +3 -3
  29. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  30. package/dist/internal/machine/executionPlan.js +74 -24
  31. package/dist/internal/machine/executionPlan.js.map +1 -1
  32. package/dist/internal/machine/invocation.d.ts +10 -2
  33. package/dist/internal/machine/invocation.d.ts.map +1 -1
  34. package/dist/internal/machine/invocation.js +94 -34
  35. package/dist/internal/machine/invocation.js.map +1 -1
  36. package/dist/internal/machine/invocationEvent.d.ts +39 -0
  37. package/dist/internal/machine/invocationEvent.d.ts.map +1 -0
  38. package/dist/internal/machine/invocationEvent.js +43 -0
  39. package/dist/internal/machine/invocationEvent.js.map +1 -0
  40. package/dist/internal/machine/machine.d.ts +16 -65
  41. package/dist/internal/machine/machine.d.ts.map +1 -1
  42. package/dist/internal/machine/machine.js +18 -73
  43. package/dist/internal/machine/machine.js.map +1 -1
  44. package/dist/internal/machine/planner.d.ts +4 -2
  45. package/dist/internal/machine/planner.d.ts.map +1 -1
  46. package/dist/internal/machine/planner.js +104 -24
  47. package/dist/internal/machine/planner.js.map +1 -1
  48. package/dist/internal/machine/process.d.ts.map +1 -1
  49. package/dist/internal/machine/process.js +8 -8
  50. package/dist/internal/machine/process.js.map +1 -1
  51. package/dist/internal/machine/protocol.d.ts +8 -5
  52. package/dist/internal/machine/protocol.d.ts.map +1 -1
  53. package/dist/internal/machine/protocol.js +207 -15
  54. package/dist/internal/machine/protocol.js.map +1 -1
  55. package/dist/internal/machine/runtime.d.ts +8 -2
  56. package/dist/internal/machine/runtime.d.ts.map +1 -1
  57. package/dist/internal/machine/runtime.js +86 -8
  58. package/dist/internal/machine/runtime.js.map +1 -1
  59. package/dist/internal/machine/symbols.d.ts +2 -0
  60. package/dist/internal/machine/symbols.d.ts.map +1 -1
  61. package/dist/internal/machine/symbols.js +2 -0
  62. package/dist/internal/machine/symbols.js.map +1 -1
  63. package/dist/internal/machine/topology.d.ts +7 -0
  64. package/dist/internal/machine/topology.d.ts.map +1 -1
  65. package/dist/internal/machine/topology.js +29 -0
  66. package/dist/internal/machine/topology.js.map +1 -1
  67. package/dist/internal/testing/machine/finiteModel.js +1 -1
  68. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  69. package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
  70. package/dist/internal/testing/machine/runtime.js +2 -1
  71. package/dist/internal/testing/machine/runtime.js.map +1 -1
  72. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  73. package/dist/internal/testing/machine/verification.js +5 -3
  74. package/dist/internal/testing/machine/verification.js.map +1 -1
  75. package/dist/testing/MachineTest.d.ts +7 -7
  76. package/dist/testing/MachineTest.js +7 -7
  77. package/dist/unstable/cluster/ClusterMachine.d.ts +1 -1
  78. package/dist/unstable/cluster/ClusterMachine.js +1 -1
  79. package/dist/unstable/reactivity/AtomMachine.d.ts +25 -8
  80. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  81. package/dist/unstable/reactivity/AtomMachine.js +18 -3
  82. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  83. package/docs/agent-guide.md +242 -122
  84. package/package.json +2 -2
  85. package/src/Machine.ts +1453 -812
  86. package/src/internal/machine/activities.ts +48 -33
  87. package/src/internal/machine/atom.ts +38 -13
  88. package/src/internal/machine/cluster.ts +2 -1
  89. package/src/internal/machine/command.ts +2 -2
  90. package/src/internal/machine/commandRuntime.ts +9 -4
  91. package/src/internal/machine/configuration.ts +23 -5
  92. package/src/internal/machine/errors.ts +1 -1
  93. package/src/internal/machine/executionPlan.ts +102 -28
  94. package/src/internal/machine/invocation.ts +182 -50
  95. package/src/internal/machine/invocationEvent.ts +72 -0
  96. package/src/internal/machine/machine.ts +80 -387
  97. package/src/internal/machine/planner.ts +127 -32
  98. package/src/internal/machine/process.ts +18 -8
  99. package/src/internal/machine/protocol.ts +313 -25
  100. package/src/internal/machine/runtime.ts +135 -9
  101. package/src/internal/machine/symbols.ts +3 -0
  102. package/src/internal/machine/topology.ts +38 -0
  103. package/src/internal/testing/machine/finiteModel.ts +1 -1
  104. package/src/internal/testing/machine/runtime.ts +2 -1
  105. package/src/internal/testing/machine/verification.ts +5 -3
  106. package/src/testing/MachineTest.ts +7 -7
  107. package/src/unstable/cluster/ClusterMachine.ts +1 -1
  108. package/src/unstable/reactivity/AtomMachine.ts +45 -13
package/src/Machine.ts CHANGED
@@ -49,6 +49,11 @@ export const TypeId: TypeId = "~effect/Machine"
49
49
 
50
50
  declare const MachineOutputStatesTypeId: unique symbol
51
51
  declare const MachineTypeId: unique symbol
52
+ declare const EventConstructionTypeId: unique symbol
53
+ declare const EmittedEventConstructionTypeId: unique symbol
54
+ declare const EventProtocolTypeId: unique symbol
55
+
56
+ const ChildMachineLogicTypeId: typeof internal.ChildMachineLogicTypeId = internal.ChildMachineLogicTypeId
52
57
 
53
58
  /**
54
59
  * Type identifier used for the synthetic event passed to startup lifecycle
@@ -121,7 +126,8 @@ export interface Machine<
121
126
  Output = never,
122
127
  Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
123
128
  OutputStates extends Machine.StateIdentifier<States> = never,
124
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
129
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
130
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
125
131
  > extends Pipeable {
126
132
  readonly [TypeId]: TypeId
127
133
  /** @internal Stable type-level carrier for machine protocol extraction. */
@@ -138,7 +144,8 @@ export interface Machine<
138
144
  Output,
139
145
  Emits,
140
146
  OutputStates,
141
- InputEvents
147
+ InputEvents,
148
+ ParentEvents
142
149
  >
143
150
  /** @internal Prevents output implementation evidence from being widened. */
144
151
  readonly [MachineOutputStatesTypeId]: Readonly<Record<OutputStates, true>>
@@ -155,21 +162,37 @@ export interface Machine<
155
162
  *
156
163
  * @since 0.4.0
157
164
  */
158
- readonly events: InputEvents
165
+ readonly events: Machine.EventProtocol<"public", InputEvents>
159
166
 
160
167
  /**
161
- * Events reserved for invokes, child emissions, and other machine-local work.
168
+ * Events reserved for raised events and other machine-local work.
162
169
  *
163
170
  * @since 0.4.0
164
171
  */
165
- readonly internalEvents: ReadonlyArray<Machine.TaggedSchema>
172
+ readonly internalEvents: Machine.EventProtocol<
173
+ "internal",
174
+ Machine.InternalEventSchemas<Events, InputEvents>
175
+ >
166
176
 
167
177
  /**
168
- * Events that the machine may emit to its parent or external adapter.
178
+ * Ephemeral outward notifications published to this actor's observers.
179
+ * Emissions are never delivered implicitly to an owning actor.
169
180
  *
170
181
  * @since 0.4.0
171
182
  */
172
- readonly emits: Emits
183
+ readonly emittedEvents: Machine.EventProtocol<"emitted", Emits>
184
+
185
+ /**
186
+ * Public input events accepted by an owning actor when this machine is
187
+ * running as a child.
188
+ *
189
+ * The protocol types the optional `parent` actor reference exposed to
190
+ * handlers and is checked against a concrete parent's public events at
191
+ * composition boundaries.
192
+ *
193
+ * @since 0.10.0
194
+ */
195
+ readonly parentEvents: Machine.EventProtocol<"public", ParentEvents>
173
196
 
174
197
  /**
175
198
  * Optional schema used to decode the machine input before initialization.
@@ -219,7 +242,8 @@ export interface Machine<
219
242
  FinalStates,
220
243
  Output,
221
244
  OutputStates,
222
- InputEvents
245
+ InputEvents,
246
+ ParentEvents
223
247
  >
224
248
 
225
249
  /** @internal */
@@ -339,7 +363,8 @@ export type ExecutionServices<Requirements> =
339
363
  | Exclude<ActionServices<Requirements>, MachineRuntimeRequirement>
340
364
 
341
365
  /**
342
- * Managed runtime capability used to deliver raised and emitted events.
366
+ * Managed runtime capability used to deliver raised events and publish
367
+ * emitted notifications.
343
368
  *
344
369
  * @category models
345
370
  * @since 0.4.0
@@ -350,14 +375,45 @@ export interface Runtime<in Events, in Emits> {
350
375
  *
351
376
  * @since 0.4.0
352
377
  */
353
- readonly raise: (event: Events) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>
378
+ readonly raise: (event: Machine.EventInput<Events>) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>
354
379
 
355
380
  /**
356
- * Emits an event through the running machine's parent boundary.
381
+ * Publishes an ephemeral notification to the running actor's observers.
357
382
  *
358
383
  * @since 0.4.0
359
384
  */
360
- readonly sendParent: (event: Emits) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>
385
+ readonly emit: (
386
+ event: Machine.EmittedEventInput<Emits>
387
+ ) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>
388
+ }
389
+
390
+ /**
391
+ * Minimal typed reference accepted by targeted actor commands.
392
+ *
393
+ * @category models
394
+ * @since 0.10.0
395
+ */
396
+ export interface ActorRef<in Event> {
397
+ readonly id: string
398
+ readonly sessionId: string
399
+ readonly send: (event: Event) => Effect.Effect<void, StoppedError>
400
+ }
401
+
402
+ /**
403
+ * Actor references available while evaluating machine behavior.
404
+ *
405
+ * @category models
406
+ * @since 0.10.0
407
+ */
408
+ export interface ActorContext<
409
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
410
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema>
411
+ > {
412
+ /** Reference to the current actor. Sending queues a later mailbox event. */
413
+ readonly self: ActorRef<Machine.EventInputOf<InputEvents>>
414
+
415
+ /** Reference to the owning actor, or `undefined` when running as a root. */
416
+ readonly parent: ActorRef<Machine.EventInputOf<ParentEvents>> | undefined
361
417
  }
362
418
 
363
419
  /**
@@ -370,13 +426,14 @@ export interface Runtime<in Events, in Emits> {
370
426
  */
371
427
  export interface Enqueue<in Events, in Emits> {
372
428
  /** Raises an event inside the current macrostep. */
373
- readonly raise: (event: Events) => void
429
+ readonly raise: (event: Machine.EventInput<Events>) => void
374
430
 
375
- /** Emits an event through the machine's parent boundary. */
376
- readonly emit: (event: Emits) => void
431
+ /** Publishes an ephemeral notification to this actor's observers. */
432
+ readonly emit: (event: Machine.EmittedEventInput<Emits>) => void
377
433
 
378
434
  /** Sends an event to an invoked child after the transition is selected. */
379
435
  readonly sendTo: {
436
+ <Event>(target: ActorRef<Event>, event: Event): void
380
437
  <Child extends ChildMachine.Any>(child: Child, event: ChildMachine.Event<Child>): void
381
438
  <Address extends ChildAddress<never>>(child: Address, event: ChildAddress.Event<Address>): void
382
439
  }
@@ -397,7 +454,7 @@ export interface Enqueue<in Events, in Emits> {
397
454
  export type Command =
398
455
  | {
399
456
  readonly _tag: "SendTo"
400
- readonly child: ChildMachine.Any | ChildAddress<never>
457
+ readonly target: ActorRef<unknown> | ChildMachine.Any | ChildAddress<never>
401
458
  readonly event: unknown
402
459
  }
403
460
  | {
@@ -672,8 +729,9 @@ type DuplicateEventTag<
672
729
  : Events extends readonly [
673
730
  infer Head extends Machine.TaggedSchema,
674
731
  ...infer Tail extends ReadonlyArray<Machine.TaggedSchema>
675
- ] ? Machine.TagOf<Head> extends infer Tag extends PropertyKey ? Tag extends Seen ? Tag | DuplicateEventTag<Tail, Seen>
676
- : DuplicateEventTag<Tail, Seen | Tag>
732
+ ] ? Machine.TagOf<Head> extends infer Tag extends PropertyKey ?
733
+ | Extract<Tag, Seen>
734
+ | DuplicateEventTag<Tail, Seen | Tag>
677
735
  : never
678
736
  : never
679
737
 
@@ -683,6 +741,12 @@ type ValidateInputEventProtocol<
683
741
  > = [DuplicateInput] extends [never] ? unknown
684
742
  : EventProtocolError<"Public event tags must be unique", DuplicateInput>
685
743
 
744
+ type ValidateEmittedEventProtocol<
745
+ EmittedEvents extends ReadonlyArray<Machine.TaggedSchema>,
746
+ DuplicateEmitted extends PropertyKey = DuplicateEventTag<EmittedEvents>
747
+ > = [DuplicateEmitted] extends [never] ? unknown
748
+ : EventProtocolError<"Emitted event tags must be unique", DuplicateEmitted>
749
+
686
750
  type ValidateInternalEventProtocol<
687
751
  InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
688
752
  InternalEvents extends ReadonlyArray<Machine.TaggedSchema>,
@@ -695,6 +759,14 @@ type ValidateInternalEventProtocol<
695
759
  : EventProtocolError<"Public and internal event tags must be disjoint", Overlap>
696
760
  : EventProtocolError<"Internal event tags must be unique", DuplicateInternal>
697
761
 
762
+ type ValidateEventProtocolBuilder<
763
+ Kind extends Machine.EventProtocolKind,
764
+ Inputs extends ReadonlyArray<Machine.EventProtocolInput<Kind>>,
765
+ Schemas extends ReadonlyArray<Machine.TaggedSchema> = Machine.EventProtocolInputSchemasOf<Kind, Inputs>,
766
+ Duplicate extends PropertyKey = DuplicateEventTag<Schemas>
767
+ > = [Duplicate] extends [never] ? unknown
768
+ : EventProtocolError<"Event protocol tags must be unique", Duplicate>
769
+
698
770
  const SnapshotBuilderStateTypeId: typeof internal.SnapshotBuilderStateTypeId = internal.SnapshotBuilderStateTypeId
699
771
  const SnapshotBuilderConstructionTypeId: unique symbol = Symbol("effect/Machine/SnapshotBuilderConstruction")
700
772
 
@@ -1636,7 +1708,9 @@ export type RuntimeOutcome<State, Error = never, Output = never> =
1636
1708
  * @category models
1637
1709
  * @since 0.4.0
1638
1710
  */
1639
- export interface MachineRef<out State, in Event, out Error = never, out Output = never> {
1711
+ export interface MachineRef<out State, in Event, out Error = never, out Output = never, out Emitted = never>
1712
+ extends ActorRef<Event>
1713
+ {
1640
1714
  /** Stable machine definition id, or a generated fallback when none was declared. */
1641
1715
  readonly id: string
1642
1716
 
@@ -1649,9 +1723,15 @@ export interface MachineRef<out State, in Event, out Error = never, out Output =
1649
1723
  /** Reads the latest lifecycle snapshot. */
1650
1724
  readonly snapshot: Effect.Effect<RuntimeSnapshot<State, Error, Output>>
1651
1725
 
1652
- /** Streams lifecycle snapshots published after subscription. */
1726
+ /** Streams the current lifecycle snapshot followed by later changes. */
1653
1727
  readonly changes: Stream.Stream<RuntimeSnapshot<State, Error, Output>>
1654
1728
 
1729
+ /**
1730
+ * Streams ephemeral notifications published after subscription. Emissions
1731
+ * are not retained or replayed; the stream completes when the actor stops.
1732
+ */
1733
+ readonly emissions: Stream.Stream<Emitted>
1734
+
1655
1735
  /** Waits for machine output or fails when execution fails or is stopped. */
1656
1736
  readonly join: Effect.Effect<Output, Error | StoppedError>
1657
1737
 
@@ -1763,12 +1843,6 @@ export declare namespace Logic {
1763
1843
  /**
1764
1844
  * Machine-local capabilities available while process logic initializes.
1765
1845
  *
1766
- * **Gotchas**
1767
- *
1768
- * `sendParent` accepts `unknown` because process logic is independent from
1769
- * the parent that eventually owns it. Prefer typed process output or an
1770
- * invoke snapshot mapper when either can represent the communication.
1771
- *
1772
1846
  * @category models
1773
1847
  * @since 0.4.0
1774
1848
  */
@@ -1782,14 +1856,14 @@ export declare namespace Logic {
1782
1856
  /** Starts a child process owned by this scope. */
1783
1857
  readonly spawn: Spawn
1784
1858
 
1785
- /** Sends an untyped event to the owning process. */
1786
- readonly sendParent: (event: unknown) => Effect.Effect<void, StoppedError>
1787
-
1788
- /** Sends an event through a typed parent-local child address. */
1789
- readonly sendTo: <Address extends ChildAddress<never>>(
1790
- id: Address,
1791
- event: ChildAddress.Event<Address>
1792
- ) => Effect.Effect<void, StoppedError>
1859
+ /** Sends an event to an actor reference or typed parent-local child address. */
1860
+ readonly sendTo: {
1861
+ <TargetEvent>(target: ActorRef<TargetEvent>, event: TargetEvent): Effect.Effect<void, StoppedError>
1862
+ <Address extends ChildAddress<never>>(
1863
+ id: Address,
1864
+ event: ChildAddress.Event<Address>
1865
+ ): Effect.Effect<void, StoppedError>
1866
+ }
1793
1867
 
1794
1868
  /** Stops a child process selected by its parent-local address. */
1795
1869
  readonly stopChild: <Event>(id: ChildAddress<Event>) => Effect.Effect<void>
@@ -1829,8 +1903,9 @@ type InvokeLifecycleId = string & { readonly [ChildAddressTypeId]?: never }
1829
1903
  * **Details**
1830
1904
  *
1831
1905
  * The descriptor carries the child's address and complete machine type. Pass
1832
- * the same value to `invokeMachine`, `sendTo`, and child lookup APIs so state,
1833
- * event, error, and output types are inferred without separate annotations.
1906
+ * a descriptor for the same id and machine to inline `invoke`, `sendTo`, and
1907
+ * child lookup APIs so state, event, error, and output types are inferred
1908
+ * without separate annotations.
1834
1909
  *
1835
1910
  * @category models
1836
1911
  * @since 0.4.0
@@ -1843,6 +1918,9 @@ export interface ChildMachine<Id extends string, M extends Machine.Any> {
1843
1918
 
1844
1919
  /** Complete machine definition carried by this descriptor. */
1845
1920
  readonly machine: M
1921
+
1922
+ /** @internal */
1923
+ readonly [ChildMachineLogicTypeId]: (input?: unknown) => Logic<any, any, any, any, any, any>
1846
1924
  }
1847
1925
 
1848
1926
  /**
@@ -1868,13 +1946,14 @@ export declare namespace ChildMachine {
1868
1946
  */
1869
1947
  export type Ref<Child> = Child extends ChildMachine<string, infer M> ? MachineRef<
1870
1948
  Machine.Snapshot<Machine.States<M>>,
1871
- Machine.InputEvent<M>,
1949
+ Machine.EventInput<Machine.InputEvent<M>>,
1872
1950
  | Machine.Error<M>
1873
1951
  | ActionError<Machine.Services<M>>
1874
1952
  | InfiniteTransitionError
1875
1953
  | MachineSchemaDecodeError
1876
1954
  | StoppedError,
1877
- Machine.Output<M>
1955
+ Machine.Output<M>,
1956
+ Machine.EmittedEvent<M>
1878
1957
  >
1879
1958
  : never
1880
1959
 
@@ -1884,7 +1963,8 @@ export declare namespace ChildMachine {
1884
1963
  * @category utility types
1885
1964
  * @since 0.4.0
1886
1965
  */
1887
- export type Event<Child> = Ref<Child> extends MachineRef<any, infer Event, any, any> ? Event : never
1966
+ export type Event<Child> = Child extends ChildMachine<string, infer M> ? Machine.EventInput<Machine.InputEvent<M>>
1967
+ : never
1888
1968
  }
1889
1969
 
1890
1970
  /**
@@ -1997,7 +2077,8 @@ export declare namespace Machine {
1997
2077
  Output,
1998
2078
  Emits extends ReadonlyArray<TaggedSchema>,
1999
2079
  OutputStates extends StateIdentifier<States>,
2000
- InputEvents extends ReadonlyArray<TaggedSchema>
2080
+ InputEvents extends ReadonlyArray<TaggedSchema>,
2081
+ ParentEvents extends ReadonlyArray<TaggedSchema>
2001
2082
  > {
2002
2083
  readonly states: States
2003
2084
  readonly events: Events
@@ -2009,9 +2090,10 @@ export declare namespace Machine {
2009
2090
  readonly initialServices: InitialR
2010
2091
  readonly finalStates: FinalStates
2011
2092
  readonly output: Output
2012
- readonly emits: Emits
2093
+ readonly emittedEvents: Emits
2013
2094
  readonly outputStates: OutputStates
2014
2095
  readonly inputEvents: InputEvents
2096
+ readonly parentEvents: ParentEvents
2015
2097
  }
2016
2098
 
2017
2099
  /**
@@ -2029,11 +2111,12 @@ export declare namespace Machine {
2029
2111
  export interface Any extends Pipeable {
2030
2112
  readonly [TypeId]: TypeId
2031
2113
  /** @internal */
2032
- readonly [MachineTypeId]: TypeCarrier<any, any, any, any, any, any, any, any, any, any, any, any, any>
2114
+ readonly [MachineTypeId]: TypeCarrier<any, any, any, any, any, any, any, any, any, any, any, any, any, any>
2033
2115
  readonly states: StateSchemas
2034
- readonly events: ReadonlyArray<TaggedSchema>
2035
- readonly internalEvents: ReadonlyArray<TaggedSchema>
2036
- readonly emits: ReadonlyArray<TaggedSchema>
2116
+ readonly events: EventProtocol.Any<"public">
2117
+ readonly internalEvents: EventProtocol.Any<"internal">
2118
+ readonly emittedEvents: EventProtocol.Any<"emitted">
2119
+ readonly parentEvents: EventProtocol.Any<"public">
2037
2120
  readonly input: Schema.Top | undefined
2038
2121
  readonly id: string | undefined
2039
2122
  /** @internal */
@@ -2134,7 +2217,10 @@ export declare namespace Machine {
2134
2217
  * @category utility types
2135
2218
  * @since 0.4.0
2136
2219
  */
2137
- export type Emits<M extends Any> = M[typeof MachineTypeId]["emits"]
2220
+ export type EmittedEvents<M extends Any> = M[typeof MachineTypeId]["emittedEvents"]
2221
+
2222
+ /** @deprecated Use {@link EmittedEvents}. */
2223
+ export type Emits<M extends Any> = EmittedEvents<M>
2138
2224
 
2139
2225
  /**
2140
2226
  * Extracts state paths with implemented output handlers.
@@ -2152,6 +2238,22 @@ export declare namespace Machine {
2152
2238
  */
2153
2239
  export type InputEvents<M extends Any> = M[typeof MachineTypeId]["inputEvents"]
2154
2240
 
2241
+ /** Extracts the public input protocol required from an owning actor. */
2242
+ export type ParentEvents<M extends Any> = M[typeof MachineTypeId]["parentEvents"]
2243
+
2244
+ /** Extracts an internal event schema tuple from the complete and public protocols. */
2245
+ export type InternalEventSchemas<
2246
+ Events extends ReadonlyArray<TaggedSchema>,
2247
+ InputEvents extends ReadonlyArray<TaggedSchema>
2248
+ > = Events extends readonly [
2249
+ ...InputEvents,
2250
+ ...infer Internal extends ReadonlyArray<TaggedSchema>
2251
+ ] ? readonly [...Internal]
2252
+ : readonly []
2253
+
2254
+ /** Extracts the internal event schema tuple carried by a machine definition. */
2255
+ export type InternalEvents<M extends Any> = InternalEventSchemas<Events<M>, InputEvents<M>>
2256
+
2155
2257
  /**
2156
2258
  * Extracts the complete event protocol handled inside a machine.
2157
2259
  *
@@ -2168,13 +2270,145 @@ export declare namespace Machine {
2168
2270
  */
2169
2271
  export type InputEvent<M extends Any> = EventOf<InputEvents<M>>
2170
2272
 
2273
+ /**
2274
+ * Opaque event construction returned by {@link events} and
2275
+ * {@link internalEvents}.
2276
+ *
2277
+ * The machine resolves the instruction through its event schema when it is
2278
+ * delivered. Only the discriminator is available before decoding succeeds.
2279
+ */
2280
+ export interface EventConstruction<out Event extends { readonly _tag: PropertyKey }> {
2281
+ readonly [EventConstructionTypeId]: Event
2282
+ readonly _tag: Event["_tag"]
2283
+ }
2284
+
2285
+ /** Opaque construction returned by {@link emittedEvents}. */
2286
+ export interface EmittedEventConstruction<out Event extends { readonly _tag: PropertyKey }> {
2287
+ readonly [EmittedEventConstructionTypeId]: Event
2288
+ readonly _tag: Event["_tag"]
2289
+ }
2290
+
2291
+ /** A decoded event or a deferred machine-bound construction of that event. */
2292
+ export type EventInput<Event> =
2293
+ | Event
2294
+ | (Event extends { readonly _tag: PropertyKey } ? EventConstruction<Event> : never)
2295
+
2296
+ /** A decoded emitted event or a deferred emitted-event construction. */
2297
+ export type EmittedEventInput<Event> =
2298
+ | Event
2299
+ | (Event extends { readonly _tag: PropertyKey } ? EmittedEventConstruction<Event> : never)
2300
+
2301
+ /** Event inputs accepted for a schema tuple at machine delivery boundaries. */
2302
+ export type EventInputOf<Events extends ReadonlyArray<TaggedSchema>> = EventInput<EventOf<Events>>
2303
+
2304
+ /** Identifies whether an event protocol is accepted publicly or only inside a machine. */
2305
+ export type EventProtocolKind = "public" | "internal" | "emitted"
2306
+
2307
+ type EventConstructorInput<EventSchema extends TaggedSchema> = Omit<EventSchema["~type.make.in"], "_tag">
2308
+
2309
+ type EventConstructor<
2310
+ EventSchema extends TaggedSchema,
2311
+ Tag extends EventSchema["Type"]["_tag"],
2312
+ Kind extends EventProtocolKind
2313
+ > = {} extends EventConstructorInput<EventSchema> ? (
2314
+ input?: EventConstructorInput<EventSchema>
2315
+ ) => Kind extends "emitted" ? EmittedEventConstruction<EventByTag<readonly [EventSchema], Tag>>
2316
+ : EventConstruction<EventByTag<readonly [EventSchema], Tag>>
2317
+ : (
2318
+ input: EventConstructorInput<EventSchema>
2319
+ ) => Kind extends "emitted" ? EmittedEventConstruction<EventByTag<readonly [EventSchema], Tag>>
2320
+ : EventConstruction<EventByTag<readonly [EventSchema], Tag>>
2321
+
2322
+ type FiniteEventTag<Tag extends PropertyKey> = string extends Tag ? never
2323
+ : number extends Tag ? never
2324
+ : symbol extends Tag ? never
2325
+ : Tag
2326
+
2327
+ type EventConstructorsForSchema<
2328
+ EventSchema extends TaggedSchema,
2329
+ Kind extends EventProtocolKind
2330
+ > = EventSchema extends {
2331
+ readonly cases: infer Cases extends Readonly<Record<PropertyKey, TaggedSchema>>
2332
+ } ? {
2333
+ readonly [Tag in keyof Cases]: Tag extends Cases[Tag]["Type"]["_tag"] ? EventConstructor<Cases[Tag], Tag, Kind>
2334
+ : never
2335
+ }
2336
+ : EventSchema extends { readonly members: infer Members extends ReadonlyArray<TaggedSchema> } ?
2337
+ Types.UnionToIntersection<EventConstructorsForSchema<Members[number], Kind>>
2338
+ : {
2339
+ readonly [Tag in FiniteEventTag<EventSchema["Type"]["_tag"]>]: EventConstructor<EventSchema, Tag, Kind>
2340
+ }
2341
+
2342
+ /** Protocol-bound constructors keyed by each configured event tag. */
2343
+ export type EventConstructors<
2344
+ Events extends ReadonlyArray<TaggedSchema>,
2345
+ Kind extends EventProtocolKind = "public"
2346
+ > = {
2347
+ readonly [Tag in keyof Types.UnionToIntersection<EventConstructorsForSchema<Events[number], Kind>>]:
2348
+ Types.UnionToIntersection<EventConstructorsForSchema<Events[number], Kind>>[Tag]
2349
+ }
2350
+
2351
+ /**
2352
+ * A schema-backed event protocol exposing only deferred event constructors.
2353
+ *
2354
+ * The schema tuple is retained opaquely for machine runtime validation and
2355
+ * type inference. It is not exposed as a runtime property.
2356
+ *
2357
+ * @since 0.10.0
2358
+ */
2359
+ export type EventProtocol<
2360
+ Kind extends EventProtocolKind,
2361
+ Schemas extends ReadonlyArray<TaggedSchema>
2362
+ > = EventConstructors<Schemas, Kind> & {
2363
+ readonly [EventProtocolTypeId]: {
2364
+ readonly kind: Kind
2365
+ readonly schemas: Schemas
2366
+ }
2367
+ }
2368
+
2369
+ export namespace EventProtocol {
2370
+ /** An erased event protocol descriptor retaining its kind and schema carrier. */
2371
+ export interface Any<Kind extends EventProtocolKind = EventProtocolKind> {
2372
+ readonly [EventProtocolTypeId]: {
2373
+ readonly kind: Kind
2374
+ readonly schemas: ReadonlyArray<TaggedSchema>
2375
+ }
2376
+ }
2377
+ }
2378
+
2379
+ /** A schema or an existing protocol accepted by an event builder. */
2380
+ export type EventProtocolInput<Kind extends EventProtocolKind> = TaggedSchema | EventProtocol.Any<Kind>
2381
+
2382
+ type EventProtocolInputSchemas<
2383
+ Kind extends EventProtocolKind,
2384
+ Input extends EventProtocolInput<Kind>
2385
+ > = Input extends EventProtocol<Kind, infer Schemas> ? Schemas
2386
+ : Input extends TaggedSchema ? readonly [Input]
2387
+ : readonly []
2388
+
2389
+ /** Flattens schema and protocol inputs into one owned schema tuple. */
2390
+ export type EventProtocolInputSchemasOf<
2391
+ Kind extends EventProtocolKind,
2392
+ Inputs extends ReadonlyArray<EventProtocolInput<Kind>>
2393
+ > = Inputs extends readonly [
2394
+ infer Head extends EventProtocolInput<Kind>,
2395
+ ...infer Tail extends ReadonlyArray<EventProtocolInput<Kind>>
2396
+ ] ? readonly [...EventProtocolInputSchemas<Kind, Head>, ...EventProtocolInputSchemasOf<Kind, Tail>]
2397
+ : readonly []
2398
+
2399
+ /** @internal Extracts the schema tuple carried opaquely by an event protocol. */
2400
+ export type EventProtocolSchemas<Protocol extends EventProtocol.Any> = Protocol[typeof EventProtocolTypeId]["schemas"]
2401
+
2171
2402
  /**
2172
2403
  * Extracts the event protocol emitted by a machine.
2173
2404
  *
2174
2405
  * @category utility types
2175
2406
  * @since 0.4.0
2176
2407
  */
2177
- export type Emit<M extends Any> = EmitOf<Emits<M>>
2408
+ export type EmittedEvent<M extends Any> = EmittedEventOf<Emits<M>>
2409
+
2410
+ /** @deprecated Use {@link EmittedEvent}. */
2411
+ export type Emit<M extends Any> = EmittedEvent<M>
2178
2412
 
2179
2413
  /**
2180
2414
  * A schema whose decoded value contains a `_tag` discriminator.
@@ -2636,6 +2870,11 @@ export declare namespace Machine {
2636
2870
  | {
2637
2871
  readonly type: "choice"
2638
2872
  }
2873
+ | {
2874
+ readonly type: "invoke"
2875
+ readonly id: string
2876
+ readonly outcome: "done" | "failure" | "snapshot"
2877
+ }
2639
2878
 
2640
2879
  /**
2641
2880
  * Statically inspectable destination paths for a transition handler.
@@ -2658,10 +2897,10 @@ export declare namespace Machine {
2658
2897
  *
2659
2898
  * **Details**
2660
2899
  *
2661
- * Event, eventless, and completion handlers may declare an upper bound of
2662
- * possible target paths. A handler without that declaration is explicitly
2663
- * reported as dynamic. The source, trigger, and reentry behavior are
2664
- * available without executing the handler.
2900
+ * Event, eventless, completion, and invocation lifecycle handlers may
2901
+ * declare an upper bound of possible target paths. A handler without that
2902
+ * declaration is explicitly reported as dynamic. The source, trigger, and
2903
+ * reentry behavior are available without executing the handler.
2665
2904
  *
2666
2905
  * @category models
2667
2906
  * @since 0.4.0
@@ -2680,9 +2919,9 @@ export declare namespace Machine {
2680
2919
  /**
2681
2920
  * Serializable description of state-owned work.
2682
2921
  *
2683
- * Static invoke descriptors expose their lifecycle id and kind without
2684
- * retaining Effects, closures, services, or child runtimes. A function-valued
2685
- * invoke factory is reported as dynamic and is never evaluated by inspection.
2922
+ * Static invoke definitions expose their lifecycle id and kind without
2923
+ * retaining Effects, closures, services, or child runtimes. Function-valued
2924
+ * sources are reported as dynamic and are never evaluated by inspection.
2686
2925
  *
2687
2926
  * @category models
2688
2927
  * @since 0.4.0
@@ -2957,7 +3196,10 @@ export declare namespace Machine {
2957
3196
  * @category utility types
2958
3197
  * @since 0.4.0
2959
3198
  */
2960
- export type EmitOf<Emits extends ReadonlyArray<TaggedSchema>> = Emits[number]["Type"]
3199
+ export type EmittedEventOf<Emits extends ReadonlyArray<TaggedSchema>> = Emits[number]["Type"]
3200
+
3201
+ /** @deprecated Use {@link EmittedEventOf}. */
3202
+ export type EmitOf<Emits extends ReadonlyArray<TaggedSchema>> = EmittedEventOf<Emits>
2961
3203
 
2962
3204
  /**
2963
3205
  * Event values received by lifecycle callbacks.
@@ -3529,6 +3771,21 @@ export declare namespace Machine {
3529
3771
  >
3530
3772
  }
3531
3773
 
3774
+ /**
3775
+ * Opaque result returned by an explicitly targetless transition.
3776
+ *
3777
+ * The transition remains handled and retains its queued commands, raised
3778
+ * events, and emitted events, but selects no concrete destination. Declared
3779
+ * `targets` constrain only concrete destinations, so this result is always
3780
+ * permitted from ordinary transition handlers.
3781
+ *
3782
+ * @category models
3783
+ * @since 0.10.0
3784
+ */
3785
+ export interface NoTarget {
3786
+ readonly [Topology.NoTargetTypeId]: typeof Topology.NoTargetTypeId
3787
+ }
3788
+
3532
3789
  /**
3533
3790
  * Transition instruction that restores a history pseudo-state's parent.
3534
3791
  *
@@ -3635,9 +3892,9 @@ export declare namespace Machine {
3635
3892
  *
3636
3893
  * **Details**
3637
3894
  *
3638
- * `local` targets the nearest compound scope for the source state, `branch`
3639
- * targets descendants of the source root, and `full` builds complete
3640
- * snapshots for any root.
3895
+ * `none()` handles without selecting a destination, `local` targets the
3896
+ * nearest compound scope for the source state, `branch` targets descendants
3897
+ * of the source root, and `full` builds complete snapshots for any root.
3641
3898
  *
3642
3899
  * These builders control how the next active configuration is assembled; they
3643
3900
  * do not directly control state re-entry. Exit and entry paths are derived
@@ -3653,6 +3910,18 @@ export declare namespace Machine {
3653
3910
  States extends StateSchemas,
3654
3911
  Source extends StateNodeIdentifier<States>
3655
3912
  > {
3913
+ /**
3914
+ * Selects an explicitly targetless transition.
3915
+ *
3916
+ * The event or lifecycle outcome is handled and queued operations are
3917
+ * retained, while the current state configuration remains the transition
3918
+ * result. This remains valid when the handler declares concrete `targets`,
3919
+ * because those paths are an upper bound rather than an exhaustive result.
3920
+ *
3921
+ * @since 0.10.0
3922
+ */
3923
+ readonly none: () => NoTarget
3924
+
3656
3925
  /**
3657
3926
  * Moves to another state in the same local group. The value of the state
3658
3927
  * containing that group, and values in other active branches, are kept.
@@ -3701,11 +3970,13 @@ export declare namespace Machine {
3701
3970
  StateId extends StateIdentifier<States>,
3702
3971
  EventTag extends TagOf<Events[number]>,
3703
3972
  E,
3704
- R
3705
- > {
3973
+ R,
3974
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
3975
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
3976
+ > extends ActorContext<InputEvents, ParentEvents> {
3706
3977
  readonly state: StateByIdentifier<States, StateId>
3707
- readonly parent: ParentStateValue<States, StateId>
3708
- readonly parents: ParentStateValues<States, StateId>
3978
+ readonly containingState: ParentStateValue<States, StateId>
3979
+ readonly ancestors: ParentStateValues<States, StateId>
3709
3980
  /** Complete logical configuration captured at the start of this microstep. */
3710
3981
  readonly snapshot: Snapshot<States>
3711
3982
  readonly event: EventByTag<Events, EventTag>
@@ -3729,16 +4000,18 @@ export declare namespace Machine {
3729
4000
  States extends StateSchemas,
3730
4001
  Events extends ReadonlyArray<TaggedSchema>,
3731
4002
  Emits extends ReadonlyArray<TaggedSchema>,
3732
- StateId extends StateIdentifier<States>
3733
- > {
4003
+ StateId extends StateIdentifier<States>,
4004
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4005
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4006
+ > extends ActorContext<InputEvents, ParentEvents> {
3734
4007
  readonly state: StateByIdentifier<States, StateId>
3735
- readonly parent: ParentStateValue<States, StateId>
3736
- readonly parents: ParentStateValues<States, StateId>
4008
+ readonly containingState: ParentStateValue<States, StateId>
4009
+ readonly ancestors: ParentStateValues<States, StateId>
3737
4010
  readonly event: LifecycleEvent<Events>
3738
4011
  }
3739
4012
 
3740
4013
  /**
3741
- * Context passed to an invoked child process source.
4014
+ * Context passed to a function-valued invocation source.
3742
4015
  *
3743
4016
  * @category models
3744
4017
  * @since 0.4.0
@@ -3747,36 +4020,84 @@ export declare namespace Machine {
3747
4020
  States extends StateSchemas,
3748
4021
  Events extends ReadonlyArray<TaggedSchema>,
3749
4022
  Emits extends ReadonlyArray<TaggedSchema>,
3750
- StateId extends StateIdentifier<States>
3751
- > {
4023
+ StateId extends StateIdentifier<States>,
4024
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4025
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4026
+ > extends ActorContext<InputEvents, ParentEvents> {
3752
4027
  readonly state: StateByIdentifier<States, StateId>
3753
- readonly parent: ParentStateValue<States, StateId>
3754
- readonly parents: ParentStateValues<States, StateId>
4028
+ readonly containingState: ParentStateValue<States, StateId>
4029
+ readonly ancestors: ParentStateValues<States, StateId>
3755
4030
  readonly event: LifecycleEvent<Events>
3756
4031
  }
3757
4032
 
3758
4033
  /**
3759
- * Context passed to an invoked child process active snapshot mapper.
4034
+ * Context passed to an invocation's active-snapshot transition.
3760
4035
  *
3761
4036
  * @category models
3762
4037
  * @since 0.4.0
3763
4038
  */
3764
- export interface InvokeSnapshotContext<State, Error, Output> {
4039
+ export interface InvokeSnapshotContext<
4040
+ States extends StateSchemas,
4041
+ Events extends ReadonlyArray<TaggedSchema>,
4042
+ Emits extends ReadonlyArray<TaggedSchema>,
4043
+ StateId extends StateIdentifier<States>,
4044
+ State,
4045
+ Error,
4046
+ Output,
4047
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4048
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4049
+ > extends ActorContext<InputEvents, ParentEvents> {
3765
4050
  readonly id: string
4051
+ readonly state: StateByIdentifier<States, StateId>
4052
+ readonly containingState: ParentStateValue<States, StateId>
4053
+ readonly ancestors: ParentStateValues<States, StateId>
4054
+ readonly target: TargetBuilder<States, StateId>
3766
4055
  readonly snapshot: Extract<RuntimeSnapshot<State, Error, Output>, { readonly status: "active" }>
3767
4056
  }
3768
4057
 
3769
4058
  /**
3770
- * Context passed to an invoked machine terminal output mapper.
4059
+ * Context passed to an invocation's successful completion transition.
3771
4060
  *
3772
4061
  * @category models
3773
4062
  * @since 0.4.0
3774
4063
  */
3775
- export interface InvokeDoneContext<Output> {
4064
+ export interface InvokeDoneContext<
4065
+ States extends StateSchemas,
4066
+ Events extends ReadonlyArray<TaggedSchema>,
4067
+ Emits extends ReadonlyArray<TaggedSchema>,
4068
+ StateId extends StateIdentifier<States>,
4069
+ Output,
4070
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4071
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4072
+ > extends ActorContext<InputEvents, ParentEvents> {
3776
4073
  readonly id: string
4074
+ readonly state: StateByIdentifier<States, StateId>
4075
+ readonly containingState: ParentStateValue<States, StateId>
4076
+ readonly ancestors: ParentStateValues<States, StateId>
4077
+ readonly snapshot: Snapshot<States>
4078
+ readonly target: TargetBuilder<States, StateId>
3777
4079
  readonly output: Output
3778
4080
  }
3779
4081
 
4082
+ /** Context passed to an invocation typed-failure transition. */
4083
+ export interface InvokeFailureContext<
4084
+ States extends StateSchemas,
4085
+ Events extends ReadonlyArray<TaggedSchema>,
4086
+ Emits extends ReadonlyArray<TaggedSchema>,
4087
+ StateId extends StateIdentifier<States>,
4088
+ Error,
4089
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4090
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4091
+ > extends ActorContext<InputEvents, ParentEvents> {
4092
+ readonly id: string
4093
+ readonly state: StateByIdentifier<States, StateId>
4094
+ readonly containingState: ParentStateValue<States, StateId>
4095
+ readonly ancestors: ParentStateValues<States, StateId>
4096
+ readonly snapshot: Snapshot<States>
4097
+ readonly target: TargetBuilder<States, StateId>
4098
+ readonly error: Error
4099
+ }
4100
+
3780
4101
  /**
3781
4102
  * Context passed to an eventless transition handler.
3782
4103
  *
@@ -3787,11 +4108,13 @@ export declare namespace Machine {
3787
4108
  States extends StateSchemas,
3788
4109
  Events extends ReadonlyArray<TaggedSchema>,
3789
4110
  Emits extends ReadonlyArray<TaggedSchema>,
3790
- StateId extends StateIdentifier<States>
3791
- > {
4111
+ StateId extends StateIdentifier<States>,
4112
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4113
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4114
+ > extends ActorContext<InputEvents, ParentEvents> {
3792
4115
  readonly state: StateByIdentifier<States, StateId>
3793
- readonly parent: ParentStateValue<States, StateId>
3794
- readonly parents: ParentStateValues<States, StateId>
4116
+ readonly containingState: ParentStateValue<States, StateId>
4117
+ readonly ancestors: ParentStateValues<States, StateId>
3795
4118
  /** Complete logical configuration captured at the start of this microstep. */
3796
4119
  readonly snapshot: Snapshot<States>
3797
4120
  readonly event: LifecycleEvent<Events>
@@ -3816,11 +4139,13 @@ export declare namespace Machine {
3816
4139
  States extends StateSchemas,
3817
4140
  Events extends ReadonlyArray<TaggedSchema>,
3818
4141
  Emits extends ReadonlyArray<TaggedSchema>,
3819
- StateId extends StateIdentifier<States>
3820
- > {
4142
+ StateId extends StateIdentifier<States>,
4143
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4144
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4145
+ > extends ActorContext<InputEvents, ParentEvents> {
3821
4146
  readonly state: StateByIdentifier<States, StateId>
3822
- readonly parent: ParentStateValue<States, StateId>
3823
- readonly parents: ParentStateValues<States, StateId>
4147
+ readonly containingState: ParentStateValue<States, StateId>
4148
+ readonly ancestors: ParentStateValues<States, StateId>
3824
4149
  /** Complete logical configuration captured at the start of this microstep. */
3825
4150
  readonly snapshot: Snapshot<States>
3826
4151
  readonly event: LifecycleEvent<Events>
@@ -3841,13 +4166,15 @@ export declare namespace Machine {
3841
4166
  States extends StateSchemas,
3842
4167
  Events extends ReadonlyArray<TaggedSchema>,
3843
4168
  Emits extends ReadonlyArray<TaggedSchema>,
3844
- ChoiceId extends ChoiceIdentifier<States>
3845
- > {
3846
- readonly parent: StateByIdentifier<
4169
+ ChoiceId extends ChoiceIdentifier<States>,
4170
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
4171
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4172
+ > extends ActorContext<InputEvents, ParentEvents> {
4173
+ readonly containingState: StateByIdentifier<
3847
4174
  States,
3848
4175
  Extract<ImmediateParentStateIdentifier<ChoiceId>, StateIdentifier<States>>
3849
4176
  >
3850
- readonly parents: {
4177
+ readonly ancestors: {
3851
4178
  readonly [Parent in Extract<ParentStateIdentifier<ChoiceId>, ValuedStateIdentifier<States>>]: StateByIdentifier<
3852
4179
  States,
3853
4180
  Parent
@@ -3869,8 +4196,8 @@ export declare namespace Machine {
3869
4196
  StateId extends StateIdentifier<States>
3870
4197
  > {
3871
4198
  readonly state: StateByIdentifier<States, StateId>
3872
- readonly parent: ParentStateValue<States, StateId>
3873
- readonly parents: ParentStateValues<States, StateId>
4199
+ readonly containingState: ParentStateValue<States, StateId>
4200
+ readonly ancestors: ParentStateValues<States, StateId>
3874
4201
  readonly event: LifecycleEvent<Events>
3875
4202
  }
3876
4203
 
@@ -3904,8 +4231,8 @@ export declare namespace Machine {
3904
4231
  StateId extends StateIdentifier<States>
3905
4232
  > {
3906
4233
  readonly state: StateByIdentifier<States, StateId>
3907
- readonly parent: ParentStateValue<States, StateId>
3908
- readonly parents: ParentStateValues<States, StateId>
4234
+ readonly containingState: ParentStateValue<States, StateId>
4235
+ readonly ancestors: ParentStateValues<States, StateId>
3909
4236
  readonly event: LifecycleEvent<Events>
3910
4237
  readonly outputs: ParallelOutputRegions<States, StateId>
3911
4238
  }
@@ -3939,9 +4266,10 @@ export declare namespace Machine {
3939
4266
  *
3940
4267
  * **Details**
3941
4268
  *
3942
- * Handlers return snapshots for complete state replacement or target builder
3943
- * results for path-safe partial transitions. Raw decoded state values are not
3944
- * accepted at transition boundaries.
4269
+ * Handlers return snapshots for complete state replacement, target builder
4270
+ * results for path-safe partial transitions, or `target.none()` for an
4271
+ * explicitly targetless transition. Raw decoded state values and `void` are
4272
+ * not accepted at transition boundaries.
3945
4273
  *
3946
4274
  * @category utility types
3947
4275
  * @since 0.4.0
@@ -3957,7 +4285,7 @@ export declare namespace Machine {
3957
4285
  | HistoryTarget<States, HistoryIdentifier<States>>
3958
4286
  | ChoiceTarget<States, ChoiceIdentifier<States>>
3959
4287
  >
3960
- | void
4288
+ | NoTarget
3961
4289
 
3962
4290
  /** A choice resolver must always select a typed target synchronously. */
3963
4291
  export type ChoiceResult<States extends StateSchemas, E, R> =
@@ -4066,7 +4394,41 @@ export declare namespace Machine {
4066
4394
  * @category utility types
4067
4395
  * @since 0.4.0
4068
4396
  */
4069
- export type InvokeLogic<Invoke> = Invoke extends { readonly src: (...args: any) => infer Logic } ? Logic : never
4397
+ export type InvokeResolvedSource<Source> = Source extends (...args: any) => infer Resolved ? Resolved : Source
4398
+
4399
+ type ChildMachineLogic<Child> = Child extends ChildMachine<string, infer M> ? Logic<
4400
+ Snapshot<States<M>>,
4401
+ EventInput<InputEvent<M>>,
4402
+ Error<M> | ActionError<Services<M>> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
4403
+ ExcludeCompatibleRuntime<
4404
+ Exclude<ExecutionServices<InitialServices<M> | Services<M>>, internalRuntime.MachineRuntime>,
4405
+ Event<M>,
4406
+ Emit<M>
4407
+ >,
4408
+ Output<M>,
4409
+ | InitialError<M>
4410
+ | Error<M>
4411
+ | ActionError<InitialServices<M> | Services<M>>
4412
+ | InfiniteTransitionError
4413
+ | MachineSchemaDecodeError
4414
+ | StartupError
4415
+ | StoppedError
4416
+ >
4417
+ : never
4418
+
4419
+ export type InvokeLogic<Invoke> = Invoke extends { readonly effect: infer Source } ?
4420
+ InvokeResolvedSource<Source> extends infer Fx extends Effect.Effect<any, any, any> ? Logic<
4421
+ void,
4422
+ never,
4423
+ Effect.Error<Fx>,
4424
+ Effect.Services<Fx>,
4425
+ Effect.Success<Fx>
4426
+ >
4427
+ : never
4428
+ : Invoke extends { readonly after: unknown } ? Logic<void, never, never, never, void>
4429
+ : Invoke extends { readonly logic: infer Source } ? InvokeResolvedSource<Source>
4430
+ : Invoke extends { readonly child: infer Child } ? ChildMachineLogic<Child>
4431
+ : never
4070
4432
  /**
4071
4433
  * Extracts the startup error from an invoke source child process logic.
4072
4434
  *
@@ -4118,19 +4480,24 @@ export declare namespace Machine {
4118
4480
  * @since 0.4.0
4119
4481
  */
4120
4482
  export type InvokeEmits<Invoke> = Invoke extends {
4121
- readonly [InvokeTypeId]: { readonly emits: Types.Covariant<infer Emits> }
4122
- } ? Emits :
4123
- never
4124
- /**
4125
- * Extracts events returned by an invoked child snapshot mapper.
4126
- *
4127
- * @category utility types
4128
- * @since 0.4.0
4129
- */
4130
- export type InvokeSnapshotEvent<Invoke> = Invoke extends {
4131
- readonly [InvokeTypeId]: { readonly snapshotEvent: Types.Covariant<infer Event> }
4132
- } ? Event :
4133
- never
4483
+ readonly [InvokeTypeId]: { readonly emits: Types.Covariant<infer Emitted> }
4484
+ } ? Emitted
4485
+ : Invoke extends { readonly child: ChildMachine<string, infer M> } ? Emit<M>
4486
+ : never
4487
+
4488
+ /** Public parent inputs required by an invoked child machine. */
4489
+ export type InvokeParentEvents<Invoke> = Invoke extends {
4490
+ readonly [InvokeTypeId]: { readonly parentEvents: Types.Covariant<infer ParentEvent> }
4491
+ } ? ParentEvent
4492
+ : IsAny<Invoke> extends true ? never
4493
+ : Invoke extends { readonly child: ChildMachine<string, infer M> } ? EventOf<ParentEvents<M>>
4494
+ : never
4495
+ /** Extracts transition results returned by invocation lifecycle handlers. */
4496
+ export type InvokeOutcomeReturn<Invoke> = Invoke extends unknown ?
4497
+ | (Invoke extends { readonly onDone?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
4498
+ | (Invoke extends { readonly onFailure?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
4499
+ | (Invoke extends { readonly onSnapshot?: infer Handler } ? EventTransitionReturn<NonNullable<Handler>> : never)
4500
+ : never
4134
4501
  /**
4135
4502
  * Extracts the parent transition error contribution from invoked children.
4136
4503
  *
@@ -4138,7 +4505,10 @@ export declare namespace Machine {
4138
4505
  * @since 0.4.0
4139
4506
  */
4140
4507
  export type InvokeError<Config> = [InvokeReturn<Config>] extends [never] ? never
4141
- : ChildAlreadyExistsError | InvokeInitialError<InvokeReturn<Config>> | InvokeRuntimeError<InvokeReturn<Config>>
4508
+ :
4509
+ | ChildAlreadyExistsError
4510
+ | InvokeInitialError<InvokeReturn<Config>>
4511
+ | Effect.Error<InvokeOutcomeReturn<InvokeReturn<Config>>>
4142
4512
  /**
4143
4513
  * Extracts the parent service requirement contribution from invoked children.
4144
4514
  *
@@ -4146,7 +4516,12 @@ export declare namespace Machine {
4146
4516
  * @since 0.4.0
4147
4517
  */
4148
4518
  export type InvokeRequirements<Config> = [InvokeReturn<Config>] extends [never] ? never
4149
- : MachineRuntimeRequirement | InvokeServices<InvokeReturn<Config>>
4519
+ :
4520
+ | MachineRuntimeRequirement
4521
+ | InvokeServices<InvokeReturn<Config>>
4522
+ | Effect.Services<
4523
+ InvokeOutcomeReturn<InvokeReturn<Config>>
4524
+ >
4150
4525
  /**
4151
4526
  * Extracts the return value from an eventless transition.
4152
4527
  *
@@ -4192,128 +4567,459 @@ export declare namespace Machine {
4192
4567
  | Effect.Services<ChoiceReturn<Config>>
4193
4568
  | InvokeRequirements<Config>
4194
4569
 
4195
- /**
4196
- * Configuration for invoking a child process while a state is active.
4197
- *
4198
- * @category models
4199
- * @since 0.4.0
4200
- */
4201
- export interface InvokeConfig<
4570
+ export type InvokeTransition<
4202
4571
  States extends StateSchemas,
4203
4572
  Events extends ReadonlyArray<TaggedSchema>,
4204
4573
  Emits extends ReadonlyArray<TaggedSchema>,
4205
- StateId extends StateIdentifier<States>,
4206
- Event,
4207
- ChildState,
4208
- ChildEvent,
4209
- ChildError,
4210
- ChildRequirements,
4211
- ChildOutput,
4212
- ChildInitialError,
4213
- ChildEmits = never,
4214
- DeliveredOutput = ChildOutput
4215
- > {
4216
- readonly [InvokeTypeId]: {
4217
- readonly output: Types.Covariant<DeliveredOutput>
4218
- readonly emits: Types.Covariant<ChildEmits>
4219
- readonly snapshotEvent: Types.Covariant<Event>
4220
- readonly error: Types.Covariant<ChildError>
4221
- readonly requirements: Types.Covariant<ChildRequirements>
4222
- readonly initialError: Types.Covariant<ChildInitialError>
4574
+ Context
4575
+ > =
4576
+ | ((context: Context, enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>) => HandlerResult<States, any, any>)
4577
+ | {
4578
+ readonly reenter?: boolean
4579
+ readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4580
+ readonly transition: (
4581
+ context: Context,
4582
+ enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4583
+ ) => HandlerResult<States, any, any>
4223
4584
  }
4224
- /** @internal Serializable descriptor metadata used by inspection. */
4225
- readonly [Activities.ActivityMetadataTypeId]?: Activities.StaticActivityMetadata
4226
- readonly id: string
4227
- /**
4228
- * Optional parent-local address for sending events to this invocation.
4229
- *
4230
- * The invocation `id` is only its state-local lifecycle key. Use an
4231
- * explicit typed address when the parent must communicate with it.
4232
- */
4233
- readonly address?: string
4234
- /** @internal */
4235
- readonly descriptor?: ChildMachine.Any
4236
- src(): Logic<
4237
- ChildState,
4238
- ChildEvent,
4239
- ChildError,
4240
- ChildRequirements,
4241
- ChildOutput,
4242
- ChildInitialError
4243
- >
4244
- snapshot?(
4245
- context: InvokeSnapshotContext<ChildState, ChildError, ChildOutput>
4246
- ): Event | undefined
4247
- onDone?(context: InvokeDoneContext<ChildOutput>): DeliveredOutput | undefined
4585
+
4586
+ export type InvokeSource<Value, Context> = Value | ((context: Context) => Value)
4587
+
4588
+ interface AnyLogicSource {
4589
+ initial(...args: ReadonlyArray<any>): Effect.Effect<any, any, any>
4590
+ run(...args: ReadonlyArray<any>): Effect.Effect<any, any, any>
4248
4591
  }
4249
4592
 
4250
- /** @internal */
4251
- export interface AnyInvokeConfig<
4252
- Output = unknown,
4253
- Error = unknown,
4254
- Requirements = unknown,
4255
- InitialError = unknown,
4256
- Emits = never,
4257
- SnapshotEvent = never
4593
+ /** Inline state-owned work that runs for the lifetime of its active state. */
4594
+ export interface InvokeOwned<
4595
+ States extends StateSchemas,
4596
+ Events extends ReadonlyArray<TaggedSchema>,
4597
+ Emits extends ReadonlyArray<TaggedSchema>,
4598
+ StateId extends StateIdentifier<States>
4258
4599
  > {
4600
+ readonly "~effect/Machine/InvokeOwner"?: Types.Covariant<readonly [States, Events, Emits, StateId]>
4601
+ }
4602
+
4603
+ /** Type evidence retained by {@link invoke} without affecting runtime data. */
4604
+ export interface InvokeTyped<Output, Error, Requirements, InitialError, Emits = never, ParentEvent = never> {
4259
4605
  readonly [InvokeTypeId]: {
4260
4606
  readonly output: Types.Covariant<Output>
4261
- readonly emits: Types.Covariant<Emits>
4262
- readonly snapshotEvent: Types.Covariant<SnapshotEvent>
4263
4607
  readonly error: Types.Covariant<Error>
4264
4608
  readonly requirements: Types.Covariant<Requirements>
4265
4609
  readonly initialError: Types.Covariant<InitialError>
4610
+ readonly emits: Types.Covariant<Emits>
4611
+ readonly parentEvents: Types.Covariant<ParentEvent>
4266
4612
  }
4267
4613
  }
4268
4614
 
4269
- interface InvokeDefinitionValue {
4270
- readonly [InvokeTypeId]: unknown
4271
- }
4615
+ export type InvokeConfig<
4616
+ States extends StateSchemas,
4617
+ Events extends ReadonlyArray<TaggedSchema>,
4618
+ Emits extends ReadonlyArray<TaggedSchema>,
4619
+ StateId extends StateIdentifier<States>
4620
+ > =
4621
+ & InvokeOwned<States, Events, Emits, StateId>
4622
+ & (
4623
+ | {
4624
+ readonly id: string
4625
+ readonly effect: InvokeSource<Effect.Effect<any, any, any>, InvokeContext<States, Events, Emits, StateId>>
4626
+ readonly after?: never
4627
+ readonly logic?: never
4628
+ readonly child?: never
4629
+ readonly address?: never
4630
+ readonly onDone?: unknown
4631
+ readonly onFailure?: unknown
4632
+ readonly onSnapshot?: never
4633
+ }
4634
+ | {
4635
+ readonly id: string
4636
+ readonly after: InvokeSource<Duration.Input, InvokeContext<States, Events, Emits, StateId>>
4637
+ readonly effect?: never
4638
+ readonly logic?: never
4639
+ readonly child?: never
4640
+ readonly address?: never
4641
+ readonly onDone: unknown
4642
+ readonly onFailure?: never
4643
+ readonly onSnapshot?: never
4644
+ }
4645
+ | {
4646
+ readonly id: string
4647
+ readonly address: string
4648
+ readonly logic: InvokeSource<AnyLogicSource, InvokeContext<States, Events, Emits, StateId>>
4649
+ readonly effect?: never
4650
+ readonly after?: never
4651
+ readonly child?: never
4652
+ readonly onDone?: unknown
4653
+ readonly onFailure?: unknown
4654
+ readonly onSnapshot?: unknown
4655
+ }
4656
+ | {
4657
+ readonly child: ChildMachine.Any
4658
+ readonly input?: {} | null | ((context: InvokeContext<States, Events, Emits, StateId>) => unknown)
4659
+ readonly id?: never
4660
+ readonly address?: never
4661
+ readonly effect?: never
4662
+ readonly after?: never
4663
+ readonly logic?: never
4664
+ readonly onDone?: unknown
4665
+ readonly onFailure?: unknown
4666
+ readonly onSnapshot?: unknown
4667
+ }
4668
+ )
4272
4669
 
4273
- /**
4274
- * State-bound configuration for invoked child processes.
4275
- *
4276
- * **Details**
4277
- *
4278
- * A function form receives the owning state's typed value and lifecycle
4279
- * event before constructing one or more invoke configurations.
4280
- *
4281
- * @category models
4282
- * @since 0.4.0
4283
- */
4670
+ /** State-bound inline invocation configuration. */
4284
4671
  export type InvokeDefinition<
4285
4672
  States extends StateSchemas,
4286
4673
  Events extends ReadonlyArray<TaggedSchema>,
4287
4674
  Emits extends ReadonlyArray<TaggedSchema>,
4288
4675
  StateId extends StateIdentifier<States>
4289
4676
  > =
4290
- | InvokeDefinitionValue
4291
- | ReadonlyArray<InvokeDefinitionValue>
4292
- | ((context: InvokeContext<States, Events, Emits, StateId>) =>
4293
- | InvokeDefinitionValue
4294
- | ReadonlyArray<InvokeDefinitionValue>)
4677
+ | InvokeConfig<States, Events, Emits, StateId>
4678
+ | ReadonlyArray<InvokeConfig<States, Events, Emits, StateId>>
4295
4679
 
4296
- type OutputHandlerConfig<
4680
+ type InvokeHandlerRequirement<Value, Handler> = IsAny<Value> extends true ? { readonly handler: Handler }
4681
+ : [Value] extends [never] ? { readonly handler?: never }
4682
+ : { readonly handler: Handler }
4683
+
4684
+ export type InvokeDoneRequirement<Value, Handler> = InvokeHandlerRequirement<Value, Handler> extends
4685
+ infer Requirement ? Requirement extends { readonly handler: infer Required } ? { readonly onDone: Required }
4686
+ : { readonly onDone?: never }
4687
+ : never
4688
+
4689
+ export type InvokeFailureRequirement<Value, Handler> = InvokeHandlerRequirement<Value, Handler> extends
4690
+ infer Requirement ? Requirement extends { readonly handler: infer Required } ? { readonly onFailure: Required }
4691
+ : { readonly onFailure?: never }
4692
+ : never
4693
+
4694
+ export type EffectInvokeArgs<
4297
4695
  States extends StateSchemas,
4298
4696
  Events extends ReadonlyArray<TaggedSchema>,
4697
+ Emits extends ReadonlyArray<TaggedSchema>,
4299
4698
  StateId extends StateIdentifier<States>,
4300
- Context
4301
- > = NodeByIdentifier<States, StateId> extends { readonly output: Schema.Top } ? {
4302
- readonly output: (context: Context) => OutputByIdentifier<States, StateId>
4303
- }
4304
- : {
4305
- readonly output?: never
4699
+ Fx extends Effect.Effect<any, any, any>,
4700
+ Source = Fx
4701
+ > =
4702
+ & {
4703
+ readonly id: InvokeLifecycleId
4704
+ readonly effect: Source
4705
+ readonly after?: never
4706
+ readonly logic?: never
4707
+ readonly child?: never
4708
+ readonly address?: never
4709
+ readonly onSnapshot?: never
4306
4710
  }
4711
+ & InvokeDoneRequirement<
4712
+ Effect.Success<NoInfer<Fx>>,
4713
+ InvokeTransition<
4714
+ States,
4715
+ Events,
4716
+ Emits,
4717
+ InvokeDoneContext<States, Events, Emits, StateId, Effect.Success<NoInfer<Fx>>>
4718
+ >
4719
+ >
4720
+ & InvokeFailureRequirement<
4721
+ Effect.Error<NoInfer<Fx>>,
4722
+ InvokeTransition<
4723
+ States,
4724
+ Events,
4725
+ Emits,
4726
+ InvokeFailureContext<States, Events, Emits, StateId, Effect.Error<NoInfer<Fx>>>
4727
+ >
4728
+ >
4307
4729
 
4308
- type ActiveOutputHandlerConfig<
4730
+ export type TimerInvokeArgs<
4309
4731
  States extends StateSchemas,
4310
4732
  Events extends ReadonlyArray<TaggedSchema>,
4733
+ Emits extends ReadonlyArray<TaggedSchema>,
4311
4734
  StateId extends StateIdentifier<States>
4312
- > = NodeByIdentifier<States, StateId> extends { readonly type: "parallel" } ? OutputHandlerConfig<
4735
+ > = {
4736
+ readonly id: InvokeLifecycleId
4737
+ readonly after: InvokeSource<Duration.Input, InvokeContext<States, Events, Emits, StateId>>
4738
+ readonly effect?: never
4739
+ readonly logic?: never
4740
+ readonly child?: never
4741
+ readonly address?: never
4742
+ readonly onFailure?: never
4743
+ readonly onSnapshot?: never
4744
+ readonly onDone: InvokeTransition<
4313
4745
  States,
4314
4746
  Events,
4315
- StateId,
4316
- ParallelOutputContext<States, Events, StateId>
4747
+ Emits,
4748
+ InvokeDoneContext<States, Events, Emits, StateId, void>
4749
+ >
4750
+ }
4751
+
4752
+ export type LogicInvokeArgs<
4753
+ States extends StateSchemas,
4754
+ Events extends ReadonlyArray<TaggedSchema>,
4755
+ Emits extends ReadonlyArray<TaggedSchema>,
4756
+ StateId extends StateIdentifier<States>,
4757
+ ChildState,
4758
+ ChildEvent,
4759
+ ChildError,
4760
+ ChildRequirements,
4761
+ ChildOutput,
4762
+ ChildInitialError,
4763
+ Address extends ChildAddress<never>,
4764
+ Source = Logic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>
4765
+ > =
4766
+ & {
4767
+ readonly id: InvokeLifecycleId
4768
+ readonly address: Address & ChildAddress.Compatibility<Address, ChildEvent>
4769
+ readonly logic: Source
4770
+ readonly effect?: never
4771
+ readonly after?: never
4772
+ readonly child?: never
4773
+ readonly onSnapshot?: InvokeTransition<
4774
+ States,
4775
+ Events,
4776
+ Emits,
4777
+ InvokeSnapshotContext<States, Events, Emits, StateId, ChildState, ChildError, ChildOutput>
4778
+ >
4779
+ }
4780
+ & InvokeDoneRequirement<
4781
+ ChildOutput,
4782
+ InvokeTransition<States, Events, Emits, InvokeDoneContext<States, Events, Emits, StateId, ChildOutput>>
4783
+ >
4784
+ & InvokeFailureRequirement<
4785
+ ChildError,
4786
+ InvokeTransition<States, Events, Emits, InvokeFailureContext<States, Events, Emits, StateId, ChildError>>
4787
+ >
4788
+
4789
+ export type ChildInvokeArgs<
4790
+ States extends StateSchemas,
4791
+ Events extends ReadonlyArray<TaggedSchema>,
4792
+ Emits extends ReadonlyArray<TaggedSchema>,
4793
+ StateId extends StateIdentifier<States>,
4794
+ ChildDefinition extends Machine.Any,
4795
+ Child extends ChildMachine<string, ChildDefinition>
4796
+ > =
4797
+ & {
4798
+ readonly child:
4799
+ & Child
4800
+ & (ChildDefinition extends EnsureExecutable<
4801
+ Machine.States<ChildDefinition>,
4802
+ Machine.UnhandledStates<ChildDefinition>,
4803
+ Machine.OutputStates<ChildDefinition>
4804
+ > ? unknown :
4805
+ never)
4806
+ readonly id?: never
4807
+ readonly address?: never
4808
+ readonly effect?: never
4809
+ readonly after?: never
4810
+ readonly logic?: never
4811
+ readonly onSnapshot?: InvokeTransition<
4812
+ States,
4813
+ Events,
4814
+ Emits,
4815
+ InvokeSnapshotContext<
4816
+ States,
4817
+ Events,
4818
+ Emits,
4819
+ StateId,
4820
+ Snapshot<Machine.States<ChildDefinition>>,
4821
+ Error<ChildDefinition>,
4822
+ Output<ChildDefinition>
4823
+ >
4824
+ >
4825
+ }
4826
+ & (Input<ChildDefinition> extends typeof Schema.Void ? { readonly input?: never } : {
4827
+ readonly input: InvokeSource<Input<ChildDefinition>["Type"], InvokeContext<States, Events, Emits, StateId>>
4828
+ })
4829
+ & InvokeDoneRequirement<
4830
+ Output<ChildDefinition>,
4831
+ InvokeTransition<
4832
+ States,
4833
+ Events,
4834
+ Emits,
4835
+ InvokeDoneContext<States, Events, Emits, StateId, Output<ChildDefinition>>
4836
+ >
4837
+ >
4838
+ & InvokeFailureRequirement<
4839
+ Error<ChildDefinition> | ActionError<Services<ChildDefinition>>,
4840
+ InvokeTransition<
4841
+ States,
4842
+ Events,
4843
+ Emits,
4844
+ InvokeFailureContext<
4845
+ States,
4846
+ Events,
4847
+ Emits,
4848
+ StateId,
4849
+ Error<ChildDefinition> | ActionError<Services<ChildDefinition>>
4850
+ >
4851
+ >
4852
+ >
4853
+
4854
+ export type LogicStateOf<Value> = Value extends Logic<infer State, any, any, any, any, any> ? State : never
4855
+ export type LogicEventOf<Value> = Value extends Logic<any, infer Event, any, any, any, any> ? Event : never
4856
+ export type LogicErrorOf<Value> = Value extends Logic<any, any, infer Error, any, any, any> ? Error : never
4857
+ export type LogicServicesOf<Value> = Value extends Logic<any, any, any, infer Services, any, any> ? Services : never
4858
+ export type LogicOutputOf<Value> = Value extends Logic<any, any, any, any, infer Output, any> ? Output : never
4859
+ export type LogicInitialErrorOf<Value> = Value extends Logic<any, any, any, any, any, infer Error> ? Error : never
4860
+
4861
+ type ContextualInvokeConfig<
4862
+ States extends StateSchemas,
4863
+ Events extends ReadonlyArray<TaggedSchema>,
4864
+ Emits extends ReadonlyArray<TaggedSchema>,
4865
+ StateId extends StateIdentifier<States>,
4866
+ Raw
4867
+ > = Raw extends InvokeTyped<any, any, any, any, any> ? unknown
4868
+ : Raw extends { readonly effect: infer Source } ?
4869
+ InvokeResolvedSource<Source> extends infer Fx extends Effect.Effect<any, any, any> ?
4870
+ & InvokeDoneRequirement<
4871
+ Effect.Success<Fx>,
4872
+ InvokeTransition<
4873
+ States,
4874
+ Events,
4875
+ Emits,
4876
+ InvokeDoneContext<States, Events, Emits, StateId, Effect.Success<Fx>>
4877
+ >
4878
+ >
4879
+ & InvokeFailureRequirement<
4880
+ Effect.Error<Fx>,
4881
+ InvokeTransition<
4882
+ States,
4883
+ Events,
4884
+ Emits,
4885
+ InvokeFailureContext<States, Events, Emits, StateId, Effect.Error<Fx>>
4886
+ >
4887
+ >
4888
+ & { readonly onSnapshot?: never }
4889
+ : never
4890
+ : Raw extends { readonly after: unknown } ? {
4891
+ readonly onDone: InvokeTransition<
4892
+ States,
4893
+ Events,
4894
+ Emits,
4895
+ InvokeDoneContext<States, Events, Emits, StateId, void>
4896
+ >
4897
+ readonly onFailure?: never
4898
+ readonly onSnapshot?: never
4899
+ }
4900
+ : Raw extends { readonly logic: infer Source; readonly address: infer Address } ?
4901
+ InvokeResolvedSource<Source> extends infer ChildLogic extends Logic<any, any, any, any, any, any> ?
4902
+ & InvokeDoneRequirement<
4903
+ InvokeOutput<Raw>,
4904
+ InvokeTransition<
4905
+ States,
4906
+ Events,
4907
+ Emits,
4908
+ InvokeDoneContext<States, Events, Emits, StateId, InvokeOutput<Raw>>
4909
+ >
4910
+ >
4911
+ & InvokeFailureRequirement<
4912
+ InvokeRuntimeError<Raw>,
4913
+ InvokeTransition<
4914
+ States,
4915
+ Events,
4916
+ Emits,
4917
+ InvokeFailureContext<States, Events, Emits, StateId, InvokeRuntimeError<Raw>>
4918
+ >
4919
+ >
4920
+ & {
4921
+ readonly address:
4922
+ & ChildAddress<never>
4923
+ & ChildAddress.Compatibility<
4924
+ Address,
4925
+ ChildLogic extends Logic<any, infer ChildEvent, any, any, any, any> ? ChildEvent : never
4926
+ >
4927
+ readonly onSnapshot?: InvokeTransition<
4928
+ States,
4929
+ Events,
4930
+ Emits,
4931
+ InvokeSnapshotContext<
4932
+ States,
4933
+ Events,
4934
+ Emits,
4935
+ StateId,
4936
+ ChildLogic extends Logic<infer ChildState, any, any, any, any, any> ? ChildState : never,
4937
+ InvokeRuntimeError<Raw>,
4938
+ InvokeOutput<Raw>
4939
+ >
4940
+ >
4941
+ }
4942
+ : never
4943
+ : Raw extends { readonly child: infer Child extends ChildMachine<string, infer ChildDefinition> } ?
4944
+ ChildMachineLogic<Child> extends infer ChildLogic extends Logic<any, any, any, any, any, any> ?
4945
+ & InvokeDoneRequirement<
4946
+ Output<ChildDefinition>,
4947
+ InvokeTransition<
4948
+ States,
4949
+ Events,
4950
+ Emits,
4951
+ InvokeDoneContext<States, Events, Emits, StateId, Output<ChildDefinition>>
4952
+ >
4953
+ >
4954
+ & InvokeFailureRequirement<
4955
+ Error<ChildDefinition> | ActionError<Services<ChildDefinition>>,
4956
+ InvokeTransition<
4957
+ States,
4958
+ Events,
4959
+ Emits,
4960
+ InvokeFailureContext<
4961
+ States,
4962
+ Events,
4963
+ Emits,
4964
+ StateId,
4965
+ Error<ChildDefinition> | ActionError<Services<ChildDefinition>>
4966
+ >
4967
+ >
4968
+ >
4969
+ & (Input<ChildDefinition> extends typeof Schema.Void ? { readonly input?: never } : {
4970
+ readonly input: InvokeSource<Input<ChildDefinition>["Type"], InvokeContext<States, Events, Emits, StateId>>
4971
+ })
4972
+ & {
4973
+ readonly onSnapshot?: InvokeTransition<
4974
+ States,
4975
+ Events,
4976
+ Emits,
4977
+ InvokeSnapshotContext<
4978
+ States,
4979
+ Events,
4980
+ Emits,
4981
+ StateId,
4982
+ Snapshot<Machine.States<ChildDefinition>>,
4983
+ Error<ChildDefinition>,
4984
+ Output<ChildDefinition>
4985
+ >
4986
+ >
4987
+ }
4988
+ : never
4989
+ : never
4990
+
4991
+ type ContextualInvokeDefinition<
4992
+ States extends StateSchemas,
4993
+ Events extends ReadonlyArray<TaggedSchema>,
4994
+ Emits extends ReadonlyArray<TaggedSchema>,
4995
+ StateId extends StateIdentifier<States>,
4996
+ Raw
4997
+ > = Raw extends ReadonlyArray<any> ? {
4998
+ readonly [Index in keyof Raw]: ContextualInvokeConfig<States, Events, Emits, StateId, Raw[Index]>
4999
+ }
5000
+ : ContextualInvokeConfig<States, Events, Emits, StateId, Raw>
5001
+
5002
+ type OutputHandlerConfig<
5003
+ States extends StateSchemas,
5004
+ Events extends ReadonlyArray<TaggedSchema>,
5005
+ StateId extends StateIdentifier<States>,
5006
+ Context
5007
+ > = NodeByIdentifier<States, StateId> extends { readonly output: Schema.Top } ? {
5008
+ readonly output: (context: Context) => OutputByIdentifier<States, StateId>
5009
+ }
5010
+ : {
5011
+ readonly output?: never
5012
+ }
5013
+
5014
+ type ActiveOutputHandlerConfig<
5015
+ States extends StateSchemas,
5016
+ Events extends ReadonlyArray<TaggedSchema>,
5017
+ StateId extends StateIdentifier<States>
5018
+ > = NodeByIdentifier<States, StateId> extends { readonly type: "parallel" } ? OutputHandlerConfig<
5019
+ States,
5020
+ Events,
5021
+ StateId,
5022
+ ParallelOutputContext<States, Events, StateId>
4317
5023
  >
4318
5024
  : {
4319
5025
  readonly output?: never
@@ -4331,64 +5037,66 @@ export declare namespace Machine {
4331
5037
  Emits extends ReadonlyArray<TaggedSchema>,
4332
5038
  StateId extends StateIdentifier<States>,
4333
5039
  E,
4334
- R
5040
+ R,
5041
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5042
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4335
5043
  > = {
4336
5044
  readonly entry?: (
4337
- context: StateActionContext<States, Events, Emits, StateId>,
5045
+ context: StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
4338
5046
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4339
5047
  ) => StateActionResult<any, any>
4340
5048
  readonly exit?: (
4341
- context: StateActionContext<States, Events, Emits, StateId>,
5049
+ context: StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
4342
5050
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4343
5051
  ) => StateActionResult<any, any>
4344
5052
  readonly invoke?: InvokeDefinition<States, Events, Emits, StateId>
4345
5053
  readonly always?:
4346
5054
  | ((
4347
- context: AlwaysContext<States, Events, Emits, StateId>,
5055
+ context: AlwaysContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
4348
5056
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4349
5057
  ) => HandlerResult<States, any, any>)
4350
5058
  | {
4351
5059
  /** Statically declared upper bound of possible target paths. */
4352
5060
  readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4353
5061
  readonly transition: (
4354
- context: AlwaysContext<States, Events, Emits, StateId>,
5062
+ context: AlwaysContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
4355
5063
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4356
5064
  ) => HandlerResult<States, any, any>
4357
5065
  }
4358
5066
  readonly onDone?:
4359
5067
  | ((
4360
- context: DoneContext<States, Events, Emits, StateId>,
5068
+ context: DoneContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
4361
5069
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4362
5070
  ) => HandlerResult<States, any, any>)
4363
5071
  | {
4364
5072
  /** Statically declared upper bound of possible target paths. */
4365
5073
  readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4366
5074
  readonly transition: (
4367
- context: DoneContext<States, Events, Emits, StateId>,
5075
+ context: DoneContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
4368
5076
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4369
5077
  ) => HandlerResult<States, any, any>
4370
5078
  }
4371
5079
  readonly on?: {
4372
5080
  readonly [EventTag in TagOf<Events[number]>]?:
4373
5081
  | ((
4374
- context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R>,
5082
+ context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R, InputEvents, ParentEvents>,
4375
5083
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4376
5084
  ) => HandlerResult<States, any, any>)
4377
5085
  | {
4378
5086
  readonly reenter?: boolean
4379
5087
  /**
4380
5088
  * Upper bound of state or history paths this handler may target.
4381
- * Returning `void` is always permitted. Declaring a parent state also
5089
+ * Returning `target.none()` is always permitted. Declaring a parent state also
4382
5090
  * permits concrete descendant targets below it.
4383
5091
  */
4384
5092
  readonly targets?: ReadonlyArray<StateNodeIdentifier<States>>
4385
5093
  readonly transition: (
4386
- context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R>,
5094
+ context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R, InputEvents, ParentEvents>,
4387
5095
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4388
5096
  ) => HandlerResult<States, any, any>
4389
5097
  }
4390
5098
  }
4391
- readonly initial?: StateInitialHandler<States, Events, Emits, StateId>
5099
+ readonly initial?: StateInitialHandler<States, Events, Emits, StateId, InputEvents, ParentEvents>
4392
5100
  } & ActiveOutputHandlerConfig<States, Events, StateId>
4393
5101
 
4394
5102
  /** Values supplied when a statechart implicitly enters a state's initial children. */
@@ -4413,17 +5121,21 @@ export declare namespace Machine {
4413
5121
  States extends StateSchemas,
4414
5122
  Events extends ReadonlyArray<TaggedSchema>,
4415
5123
  Emits extends ReadonlyArray<TaggedSchema>,
4416
- StateId extends StateIdentifier<States>
4417
- > = StateActionContext<States, Events, Emits, StateId>
5124
+ StateId extends StateIdentifier<States>,
5125
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5126
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
5127
+ > = StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>
4418
5128
 
4419
5129
  /** Initial child value implementation for a compound or parallel state. */
4420
5130
  export type StateInitialHandler<
4421
5131
  States extends StateSchemas,
4422
5132
  Events extends ReadonlyArray<TaggedSchema>,
4423
5133
  Emits extends ReadonlyArray<TaggedSchema>,
4424
- StateId extends StateIdentifier<States>
5134
+ StateId extends StateIdentifier<States>,
5135
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5136
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4425
5137
  > = (
4426
- context: StateInitialContext<States, Events, Emits, StateId>,
5138
+ context: StateInitialContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
4427
5139
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4428
5140
  ) => StateInitialValue<States, StateId>
4429
5141
 
@@ -4436,7 +5148,7 @@ export declare namespace Machine {
4436
5148
  > {
4437
5149
  readonly event: LifecycleEvent<Events>
4438
5150
  readonly target: HistoryDefaultTargetBuilder<States, ParentId>
4439
- readonly parent: ParentId
5151
+ readonly owner: ParentId
4440
5152
  }
4441
5153
 
4442
5154
  /**
@@ -4476,13 +5188,15 @@ export declare namespace Machine {
4476
5188
  States extends StateSchemas,
4477
5189
  Events extends ReadonlyArray<TaggedSchema>,
4478
5190
  Emits extends ReadonlyArray<TaggedSchema>,
4479
- ChoiceId extends ChoiceIdentifier<States>
5191
+ ChoiceId extends ChoiceIdentifier<States>,
5192
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5193
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4480
5194
  > {
4481
5195
  readonly choice: {
4482
5196
  /** Statically inspectable upper bound of every possible target. */
4483
5197
  readonly targets: readonly [StateNodeIdentifier<States>, ...ReadonlyArray<StateNodeIdentifier<States>>]
4484
5198
  readonly transition: (
4485
- context: ChoiceContext<States, Events, Emits, ChoiceId>,
5199
+ context: ChoiceContext<States, Events, Emits, ChoiceId, InputEvents, ParentEvents>,
4486
5200
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4487
5201
  ) => ChoiceResult<States, any, any>
4488
5202
  }
@@ -4506,10 +5220,12 @@ export declare namespace Machine {
4506
5220
  States extends StateSchemas,
4507
5221
  Events extends ReadonlyArray<TaggedSchema>,
4508
5222
  Emits extends ReadonlyArray<TaggedSchema>,
4509
- StateId extends StateIdentifier<States>
5223
+ StateId extends StateIdentifier<States>,
5224
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5225
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4510
5226
  > = {
4511
5227
  readonly entry?: (
4512
- context: StateActionContext<States, Events, Emits, StateId>,
5228
+ context: StateActionContext<States, Events, Emits, StateId, InputEvents, ParentEvents>,
4513
5229
  enqueue: Enqueue<EventOf<Events>, EmitOf<Emits>>
4514
5230
  ) => StateActionResult<any, any>
4515
5231
  readonly exit?: never
@@ -4530,10 +5246,12 @@ export declare namespace Machine {
4530
5246
  Emits extends ReadonlyArray<TaggedSchema>,
4531
5247
  StateId extends StateIdentifier<States>,
4532
5248
  E,
4533
- R
5249
+ R,
5250
+ InputEvents extends ReadonlyArray<TaggedSchema> = Events,
5251
+ ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
4534
5252
  > = NodeByIdentifier<States, StateId> extends { readonly type: "final" } ?
4535
- FinalStateConfig<States, Events, Emits, StateId>
4536
- : ActiveStateConfig<States, Events, Emits, StateId, E, R>
5253
+ FinalStateConfig<States, Events, Emits, StateId, InputEvents, ParentEvents>
5254
+ : ActiveStateConfig<States, Events, Emits, StateId, E, R, InputEvents, ParentEvents>
4537
5255
 
4538
5256
  type HandlerNodeConfig<
4539
5257
  States extends StateSchemas,
@@ -4541,9 +5259,11 @@ export declare namespace Machine {
4541
5259
  Emits extends ReadonlyArray<TaggedSchema>,
4542
5260
  Path extends StateNodeIdentifier<States>,
4543
5261
  E,
4544
- R
4545
- > = Path extends ChoiceIdentifier<States> ? ChoiceStateConfig<States, Events, Emits, Path>
4546
- : Path extends StateIdentifier<States> ? HandlerConfig<States, Events, Emits, Path, E, R>
5262
+ R,
5263
+ InputEvents extends ReadonlyArray<TaggedSchema>,
5264
+ ParentEvents extends ReadonlyArray<TaggedSchema>
5265
+ > = Path extends ChoiceIdentifier<States> ? ChoiceStateConfig<States, Events, Emits, Path, InputEvents, ParentEvents>
5266
+ : Path extends StateIdentifier<States> ? HandlerConfig<States, Events, Emits, Path, E, R, InputEvents, ParentEvents>
4547
5267
  : never
4548
5268
 
4549
5269
  type HandlerChildren<Node> = Node extends { readonly states: infer Children extends StateSchemas } ? Children : never
@@ -4577,6 +5297,39 @@ export declare namespace Machine {
4577
5297
  : Path extends keyof Config ? Config[Path]
4578
5298
  : never
4579
5299
 
5300
+ type HandlerInvokeContextAtPath<
5301
+ AllStates extends StateSchemas,
5302
+ Events extends ReadonlyArray<TaggedSchema>,
5303
+ Emits extends ReadonlyArray<TaggedSchema>,
5304
+ Config,
5305
+ StateId extends StateNodeIdentifier<AllStates>,
5306
+ NodeConfig = HandlerConfigAtPath<Config, StateId>
5307
+ > = StateId extends StateIdentifier<AllStates> ?
5308
+ NodeConfig extends { readonly invoke: infer Invoke } ? HandlerValidationAtPath<
5309
+ StateId,
5310
+ { readonly invoke: ContextualInvokeDefinition<AllStates, Events, Emits, StateId, Invoke> }
5311
+ >
5312
+ : unknown
5313
+ : unknown
5314
+
5315
+ type HandlerInvokeContexts<
5316
+ AllStates extends StateSchemas,
5317
+ Events extends ReadonlyArray<TaggedSchema>,
5318
+ Emits extends ReadonlyArray<TaggedSchema>,
5319
+ Config
5320
+ > = Types.UnionToIntersection<
5321
+ StateNodeIdentifier<AllStates> extends infer StateId extends StateNodeIdentifier<AllStates> ?
5322
+ StateId extends StateNodeIdentifier<AllStates> ? HandlerInvokeContextAtPath<
5323
+ AllStates,
5324
+ Events,
5325
+ Emits,
5326
+ Config,
5327
+ StateId
5328
+ >
5329
+ : never
5330
+ : never
5331
+ >
5332
+
4580
5333
  // Rebuild the public nested handler shape so branded validation errors stay
4581
5334
  // attached to the exact property that introduced them.
4582
5335
  type HandlerValidationAtPath<Path extends string, Validation> = Path extends `${infer Head}.${infer Rest}` ? {
@@ -4593,9 +5346,11 @@ export declare namespace Machine {
4593
5346
  Emits extends ReadonlyArray<TaggedSchema>,
4594
5347
  E,
4595
5348
  R,
5349
+ InputEvents extends ReadonlyArray<TaggedSchema>,
5350
+ ParentEvents extends ReadonlyArray<TaggedSchema>,
4596
5351
  StateId extends StateNodeIdentifier<AllStates>
4597
5352
  > =
4598
- & HandlerNodeConfig<AllStates, Events, Emits, StateId, E, R>
5353
+ & HandlerNodeConfig<AllStates, Events, Emits, StateId, E, R, InputEvents, ParentEvents>
4599
5354
  & (StateId extends ChoiceIdentifier<AllStates> ? {
4600
5355
  readonly states?: never
4601
5356
  readonly history?: never
@@ -4612,6 +5367,8 @@ export declare namespace Machine {
4612
5367
  Emits,
4613
5368
  E,
4614
5369
  R,
5370
+ InputEvents,
5371
+ ParentEvents,
4615
5372
  Extract<StateId, StateIdentifier<AllStates>>
4616
5373
  >
4617
5374
  readonly history?: HistoryDefaultConfig<
@@ -4634,6 +5391,8 @@ export declare namespace Machine {
4634
5391
  Emits extends ReadonlyArray<TaggedSchema>,
4635
5392
  E,
4636
5393
  R,
5394
+ InputEvents extends ReadonlyArray<TaggedSchema>,
5395
+ ParentEvents extends ReadonlyArray<TaggedSchema>,
4637
5396
  Prefix extends string
4638
5397
  > = {
4639
5398
  readonly [Key in ActiveStateKey<States> | ChoiceStateKey<States>]?: HandlerNode<
@@ -4643,6 +5402,8 @@ export declare namespace Machine {
4643
5402
  Emits,
4644
5403
  E,
4645
5404
  R,
5405
+ InputEvents,
5406
+ ParentEvents,
4646
5407
  HandlerNodeId<AllStates, JoinPath<Prefix, Key>>
4647
5408
  >
4648
5409
  }
@@ -4877,6 +5638,7 @@ export declare namespace Machine {
4877
5638
  AllStates extends StateSchemas,
4878
5639
  Node,
4879
5640
  Events extends ReadonlyArray<TaggedSchema>,
5641
+ InputEvents extends ReadonlyArray<TaggedSchema>,
4880
5642
  Emits extends ReadonlyArray<TaggedSchema>,
4881
5643
  StateId extends StateNodeIdentifier<AllStates>,
4882
5644
  Config,
@@ -4891,9 +5653,7 @@ export declare namespace Machine {
4891
5653
  & HandlerOnTargetValidation<StateId, Config>
4892
5654
  & HandlerDirectTargetValidation<StateId, Config, "always">
4893
5655
  & HandlerDirectTargetValidation<StateId, Config, "onDone">
4894
- & HandlerInvokeOutputValidation<Events, StateId, Config>
4895
- & HandlerInvokeEmitsValidation<Events, StateId, Config>
4896
- & HandlerInvokeSnapshotValidation<Events, StateId, Config>
5656
+ & HandlerInvokeParentEventsValidation<InputEvents, StateId, Config>
4897
5657
  & HandlerChildrenValidation<Node, StateId, Config>
4898
5658
  & HandlerOutputRequirementValidation<AllStates, StateId, AvailableOutputStates, Config>
4899
5659
  & HandlerRuntimeValidation<Events, Emits, StateId, Config>
@@ -4922,46 +5682,17 @@ export declare namespace Machine {
4922
5682
  }
4923
5683
  : unknown
4924
5684
 
4925
- type HandlerInvokeOutputValidation<
4926
- Events extends ReadonlyArray<TaggedSchema>,
4927
- StateId extends string,
4928
- Config
4929
- > = [InvokeReturn<Config>] extends [never] ? unknown
4930
- : [Exclude<InvokeOutput<InvokeReturn<Config>>, EventOf<Events> | void>] extends [never] ? unknown
4931
- : {
4932
- readonly invoke: HandlerValidationError<
4933
- "Invoked child output must be a machine event or void",
4934
- StateId,
4935
- Exclude<InvokeOutput<InvokeReturn<Config>>, EventOf<Events> | void>
4936
- >
4937
- }
4938
-
4939
- type HandlerInvokeEmitsValidation<
4940
- Events extends ReadonlyArray<TaggedSchema>,
4941
- StateId extends string,
4942
- Config
4943
- > = [InvokeReturn<Config>] extends [never] ? unknown
4944
- : [Exclude<InvokeEmits<InvokeReturn<Config>>, EventOf<Events>>] extends [never] ? unknown
4945
- : {
4946
- readonly invoke: HandlerValidationError<
4947
- "Invoked child emits events not accepted by the parent machine",
4948
- StateId,
4949
- Exclude<InvokeEmits<InvokeReturn<Config>>, EventOf<Events>>
4950
- >
4951
- }
4952
-
4953
- type HandlerInvokeSnapshotValidation<
5685
+ type HandlerInvokeParentEventsValidation<
4954
5686
  Events extends ReadonlyArray<TaggedSchema>,
4955
5687
  StateId extends string,
4956
5688
  Config
4957
5689
  > = [InvokeReturn<Config>] extends [never] ? unknown
4958
- : IsAny<InvokeSnapshotEvent<InvokeReturn<Config>>> extends true ? unknown
4959
- : [Exclude<InvokeSnapshotEvent<InvokeReturn<Config>>, EventOf<Events> | undefined>] extends [never] ? unknown
5690
+ : [Exclude<InvokeParentEvents<InvokeReturn<Config>>, EventOf<Events>>] extends [never] ? unknown
4960
5691
  : {
4961
5692
  readonly invoke: HandlerValidationError<
4962
- "Invoked child snapshot mapper must return a machine event or undefined",
5693
+ "Invoked child expects parent events not accepted by this machine",
4963
5694
  StateId,
4964
- Exclude<InvokeSnapshotEvent<InvokeReturn<Config>>, EventOf<Events> | undefined>
5695
+ Exclude<InvokeParentEvents<InvokeReturn<Config>>, EventOf<Events>>
4965
5696
  >
4966
5697
  }
4967
5698
 
@@ -4981,6 +5712,7 @@ export declare namespace Machine {
4981
5712
  type HandlerNodeValidationAtPath<
4982
5713
  AllStates extends StateSchemas,
4983
5714
  Events extends ReadonlyArray<TaggedSchema>,
5715
+ InputEvents extends ReadonlyArray<TaggedSchema>,
4984
5716
  Emits extends ReadonlyArray<TaggedSchema>,
4985
5717
  Config,
4986
5718
  AvailableOutputStates extends StateIdentifier<AllStates>,
@@ -4991,6 +5723,7 @@ export declare namespace Machine {
4991
5723
  AllStates,
4992
5724
  HandlerNodeByPath<AllStates, StateId>,
4993
5725
  Events,
5726
+ InputEvents,
4994
5727
  Emits,
4995
5728
  StateId,
4996
5729
  NodeConfig,
@@ -5002,6 +5735,7 @@ export declare namespace Machine {
5002
5735
  type HandlerTreeNodeValidations<
5003
5736
  AllStates extends StateSchemas,
5004
5737
  Events extends ReadonlyArray<TaggedSchema>,
5738
+ InputEvents extends ReadonlyArray<TaggedSchema>,
5005
5739
  Emits extends ReadonlyArray<TaggedSchema>,
5006
5740
  Config,
5007
5741
  AvailableOutputStates extends StateIdentifier<AllStates>
@@ -5010,6 +5744,7 @@ export declare namespace Machine {
5010
5744
  StateId extends StateNodeIdentifier<AllStates> ? HandlerNodeValidationAtPath<
5011
5745
  AllStates,
5012
5746
  Events,
5747
+ InputEvents,
5013
5748
  Emits,
5014
5749
  Config,
5015
5750
  AvailableOutputStates,
@@ -5022,12 +5757,13 @@ export declare namespace Machine {
5022
5757
  type HandlerTreeValidation<
5023
5758
  AllStates extends StateSchemas,
5024
5759
  Events extends ReadonlyArray<TaggedSchema>,
5760
+ InputEvents extends ReadonlyArray<TaggedSchema>,
5025
5761
  Emits extends ReadonlyArray<TaggedSchema>,
5026
5762
  Config,
5027
5763
  AvailableOutputStates extends StateIdentifier<AllStates>
5028
5764
  > =
5029
5765
  & HandlerUnknownStateKeyValidation<AllStates, "", Config>
5030
- & HandlerTreeNodeValidations<AllStates, Events, Emits, Config, AvailableOutputStates>
5766
+ & HandlerTreeNodeValidations<AllStates, Events, InputEvents, Emits, Config, AvailableOutputStates>
5031
5767
 
5032
5768
  type HandlerHasRequiredInitial<
5033
5769
  AllStates extends StateSchemas,
@@ -5123,6 +5859,7 @@ export declare namespace Machine {
5123
5859
  Output,
5124
5860
  OutputStates extends StateIdentifier<AllStates>,
5125
5861
  InputEvents extends ReadonlyArray<TaggedSchema>,
5862
+ ParentEvents extends ReadonlyArray<TaggedSchema>,
5126
5863
  Config
5127
5864
  > = Machine<
5128
5865
  AllStates,
@@ -5141,7 +5878,8 @@ export declare namespace Machine {
5141
5878
  Output,
5142
5879
  Emits,
5143
5880
  OutputStates | Extract<HandlerTreeEvidence<AllStates, Config>["outputState"], StateIdentifier<AllStates>>,
5144
- InputEvents
5881
+ InputEvents,
5882
+ ParentEvents
5145
5883
  >
5146
5884
 
5147
5885
  /**
@@ -5163,14 +5901,29 @@ export declare namespace Machine {
5163
5901
  FinalStates extends StateIdentifier<States>,
5164
5902
  Output,
5165
5903
  OutputStates extends StateIdentifier<States>,
5166
- InputEvents extends ReadonlyArray<TaggedSchema>
5904
+ InputEvents extends ReadonlyArray<TaggedSchema>,
5905
+ ParentEvents extends ReadonlyArray<TaggedSchema>
5167
5906
  > {
5168
- <const Config extends HandlerTree<States, States, Events, Emits, E, R, "">>(
5907
+ <
5908
+ const Config extends HandlerTree<
5909
+ States,
5910
+ States,
5911
+ Events,
5912
+ Emits,
5913
+ E,
5914
+ R,
5915
+ InputEvents,
5916
+ ParentEvents,
5917
+ ""
5918
+ >
5919
+ >(
5169
5920
  config:
5170
5921
  & Config
5922
+ & HandlerInvokeContexts<States, Events, Emits, NoInfer<Config>>
5171
5923
  & HandlerTreeValidation<
5172
5924
  States,
5173
5925
  Events,
5926
+ InputEvents,
5174
5927
  Emits,
5175
5928
  NoInfer<Config>,
5176
5929
  | OutputStates
@@ -5193,6 +5946,7 @@ export declare namespace Machine {
5193
5946
  Output,
5194
5947
  OutputStates,
5195
5948
  InputEvents,
5949
+ ParentEvents,
5196
5950
  Config
5197
5951
  >
5198
5952
  }
@@ -5344,7 +6098,8 @@ export const isFinal: <
5344
6098
  FinalStates extends Machine.StateIdentifier<States> = never,
5345
6099
  Output = never,
5346
6100
  OutputStates extends Machine.StateIdentifier<States> = never,
5347
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
6101
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
6102
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
5348
6103
  >(
5349
6104
  machine: Machine<
5350
6105
  States,
@@ -5359,10 +6114,11 @@ export const isFinal: <
5359
6114
  Output,
5360
6115
  Emits,
5361
6116
  OutputStates,
5362
- InputEvents
6117
+ InputEvents,
6118
+ ParentEvents
5363
6119
  >,
5364
6120
  state: Machine.Snapshot<States>
5365
- ) => state is Machine.SnapshotContainingFinal<States, FinalStates> = internal.isFinal
6121
+ ) => state is Machine.SnapshotContainingFinal<States, FinalStates> = internal.isFinal as any
5366
6122
 
5367
6123
  /**
5368
6124
  * Defines a state tree while preserving literal state paths.
@@ -5392,7 +6148,7 @@ export const isFinal: <
5392
6148
  *
5393
6149
  * Machine.make({
5394
6150
  * states: States.states,
5395
- * events: [],
6151
+ * events: Machine.events(),
5396
6152
  * initial: () => States.initial.idle.from()
5397
6153
  * })
5398
6154
  * ```
@@ -5409,18 +6165,22 @@ type MakeConfig<
5409
6165
  Input extends Schema.Top,
5410
6166
  InitialE,
5411
6167
  InitialR,
5412
- InternalEvents extends ReadonlyArray<Machine.TaggedSchema>
6168
+ InternalEvents extends ReadonlyArray<Machine.TaggedSchema>,
6169
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema>
5413
6170
  > = {
5414
6171
  readonly id?: string
5415
6172
  readonly states: States & DefineStateTreeInput<NoInfer<States>>
5416
- readonly events: InputEvents & ValidateInputEventProtocol<NoInfer<InputEvents>>
6173
+ readonly events:
6174
+ & Machine.EventProtocol<"public", InputEvents>
6175
+ & ValidateInputEventProtocol<NoInfer<InputEvents>>
5417
6176
  readonly internalEvents?:
5418
- & InternalEvents
6177
+ & Machine.EventProtocol<"internal", InternalEvents>
5419
6178
  & ValidateInternalEventProtocol<
5420
6179
  NoInfer<InputEvents>,
5421
6180
  NoInfer<InternalEvents>
5422
6181
  >
5423
- readonly emits?: Emits
6182
+ readonly emittedEvents?: Machine.EventProtocol<"emitted", Emits>
6183
+ readonly parentEvents?: Machine.EventProtocol<"public", ParentEvents>
5424
6184
  readonly input?: Input
5425
6185
  readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>
5426
6186
  }
@@ -5432,7 +6192,8 @@ type MakeResult<
5432
6192
  Input extends Schema.Top,
5433
6193
  InitialE,
5434
6194
  InitialR,
5435
- InternalEvents extends ReadonlyArray<Machine.TaggedSchema>
6195
+ InternalEvents extends ReadonlyArray<Machine.TaggedSchema>,
6196
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema>
5436
6197
  > = Machine<
5437
6198
  States,
5438
6199
  readonly [...InputEvents, ...InternalEvents],
@@ -5446,7 +6207,8 @@ type MakeResult<
5446
6207
  Machine.TerminalOutput<States>,
5447
6208
  Emits,
5448
6209
  never,
5449
- InputEvents
6210
+ InputEvents,
6211
+ ParentEvents
5450
6212
  >
5451
6213
 
5452
6214
  interface Make {
@@ -5457,11 +6219,12 @@ interface Make {
5457
6219
  const Input extends Schema.Top = typeof Schema.Void,
5458
6220
  InitialE = never,
5459
6221
  InitialR = never,
5460
- const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
6222
+ const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
6223
+ const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
5461
6224
  >(
5462
- config: MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>,
6225
+ config: MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>,
5463
6226
  ..._validation: ValidateDefinedStates<NoInfer<States>>
5464
- ): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>
6227
+ ): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>
5465
6228
  <
5466
6229
  const States extends Machine.StateSchemas,
5467
6230
  const InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
@@ -5469,10 +6232,11 @@ interface Make {
5469
6232
  const Input extends Schema.Top = typeof Schema.Void,
5470
6233
  InitialE = never,
5471
6234
  InitialR = never,
5472
- const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
6235
+ const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
6236
+ const ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
5473
6237
  >(
5474
6238
  config:
5475
- & Omit<MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>, "states">
6239
+ & Omit<MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentEvents>, "states">
5476
6240
  & { readonly states: InvalidDefinedStateTreeInput<States> }
5477
6241
  ): never
5478
6242
  }
@@ -5488,10 +6252,13 @@ interface Make {
5488
6252
  * `defineStates` or is passed inline. Call `handle` on the returned definition
5489
6253
  * to implement state behavior with ordinary TypeScript control flow.
5490
6254
  *
5491
- * Schemas in `events` define the public input protocol. Schemas in
5492
- * `internalEvents` are added to the complete handler protocol for invoke
5493
- * results, child emissions, and other machine-local deliveries. Their tags
5494
- * must be disjoint.
6255
+ * `Machine.events` defines the public input protocol. `Machine.internalEvents`
6256
+ * adds raised events and other machine-local deliveries.
6257
+ * `Machine.emittedEvents` defines outward ephemeral notifications, while
6258
+ * `parentEvents` declares the inputs a child may explicitly send to its owning
6259
+ * actor. All descriptors expose deferred constructors while retaining their
6260
+ * schemas opaquely for runtime validation. Public and internal tags must be
6261
+ * disjoint.
5495
6262
  *
5496
6263
  * **Example** (Typed counter machine)
5497
6264
  *
@@ -5508,10 +6275,11 @@ interface Make {
5508
6275
  * }) {}
5509
6276
  *
5510
6277
  * const States = Machine.defineStates({ Count })
6278
+ * const Events = Machine.events(Increment)
5511
6279
  *
5512
6280
  * const counter = Machine.make({
5513
6281
  * states: States.states,
5514
- * events: [Increment],
6282
+ * events: Events,
5515
6283
  * initial: () => States.initial.Count(new Count({ value: 0 }))
5516
6284
  * }).handle({
5517
6285
  * Count: {
@@ -5529,54 +6297,92 @@ interface Make {
5529
6297
  */
5530
6298
  export const make: Make = internal.make
5531
6299
 
5532
- type EventConstructorArgs<EventSchema extends Machine.TaggedSchema> = {} extends EventSchema["~type.make.in"] ?
5533
- [input?: EventSchema["~type.make.in"]]
5534
- : [input: EventSchema["~type.make.in"]]
5535
-
5536
6300
  /**
5537
- * Constructs an event from a schema owned by a machine's protocol.
5538
- *
5539
- * **Details**
6301
+ * Extracts the decoded event union carried by an event protocol descriptor.
5540
6302
  *
5541
- * The schema constructor runs exactly once. The resulting decoded event is
5542
- * trusted by this machine and definitions derived from it with `handle`, so
5543
- * repeated delivery does not decode the same already-constructed value again.
5544
- * Events supplied through ordinary `send` and `plan` calls remain untrusted
5545
- * and continue through full runtime schema validation.
5546
- * Treat the returned event as immutable after construction.
6303
+ * @category utility types
6304
+ * @since 0.10.0
6305
+ */
6306
+ export type EventOf<Protocol extends Machine.EventProtocol.Any> = Machine.EventOf<
6307
+ Machine.EventProtocolSchemas<Protocol>
6308
+ >
6309
+
6310
+ /**
6311
+ * Defines a public event protocol and returns deferred constructors for every
6312
+ * statically finite configured event tag.
5547
6313
  *
5548
- * The schema must be one of the machine's configured public or internal event
5549
- * schemas, or a case schema belonging to a configured `Schema.TaggedUnion`.
6314
+ * Constructor inputs retain their schema-derived required fields, defaults,
6315
+ * and transformations. Construction is deferred until delivery so failures
6316
+ * enter the machine's `MachineSchemaDecodeError` channel rather than throwing
6317
+ * at the call site.
5550
6318
  *
5551
6319
  * **Example**
5552
6320
  *
5553
6321
  * ```ts
5554
- * import { Schema } from "effect"
5555
- * import { Machine } from "@typeonce/effect-machine"
6322
+ * const Events = Machine.events(Increment, Reset)
6323
+ * const machine = Machine.make({ events: Events, ... })
6324
+ * yield* ref.send(Events.Increment({ by: 1 }))
6325
+ * ```
5556
6326
  *
5557
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
5558
- * class Increment extends Schema.TaggedClass<Increment>("Increment")("Increment", {
5559
- * by: Schema.Number
5560
- * }) {}
6327
+ * @category constructors
6328
+ * @since 0.10.0
6329
+ */
6330
+ export const events: {
6331
+ <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
6332
+ ...schemas: Schemas & ValidateInputEventProtocol<NoInfer<Schemas>>
6333
+ ): Machine.EventProtocol<"public", readonly [...Schemas]>
6334
+ <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"public">>>(
6335
+ ...inputs: Inputs & ValidateEventProtocolBuilder<"public", Inputs>
6336
+ ): Machine.EventProtocol<"public", Machine.EventProtocolInputSchemasOf<"public", Inputs>>
6337
+ } = internal.events as any
6338
+
6339
+ /**
6340
+ * Defines an internal event protocol and returns deferred constructors for
6341
+ * every statically finite configured event tag.
5561
6342
  *
5562
- * const States = Machine.defineStates({ Idle })
5563
- * const counter = Machine.make({
5564
- * states: States.states,
5565
- * events: [Increment],
5566
- * initial: () => States.initial.Idle.from()
5567
- * }).handle({ Idle: { on: { Increment: () => States.initial.Idle.from() } } })
6343
+ * Use these constructors for raised events and other machine-local deliveries.
6344
+ * Construction failures are reported through the
6345
+ * owning machine's `MachineSchemaDecodeError` channel.
6346
+ *
6347
+ * **Example**
6348
+ *
6349
+ * ```ts
6350
+ * const InternalEvents = Machine.internalEvents(Loaded, Failed)
6351
+ * const machine = Machine.make({ internalEvents: InternalEvents, ... })
5568
6352
  *
5569
- * const increment = Machine.event(counter, Increment, { by: 1 })
6353
+ * // Inside a transition callback:
6354
+ * enqueue.raise(InternalEvents.Loaded({ value }))
5570
6355
  * ```
5571
6356
  *
5572
6357
  * @category constructors
5573
- * @since 0.4.0
6358
+ * @since 0.10.0
5574
6359
  */
5575
- export const event: <const M extends Machine.Any, const EventSchema extends Machine.TaggedSchema>(
5576
- machine: M,
5577
- schema: EventSchema & ([EventSchema["Type"]] extends [Machine.Event<M>] ? unknown : never),
5578
- ...args: EventConstructorArgs<EventSchema>
5579
- ) => EventSchema["Type"] = internal.event
6360
+ export const internalEvents: {
6361
+ <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
6362
+ ...schemas: Schemas & ValidateInternalEventProtocol<readonly [], NoInfer<Schemas>>
6363
+ ): Machine.EventProtocol<"internal", readonly [...Schemas]>
6364
+ <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"internal">>>(
6365
+ ...inputs: Inputs & ValidateEventProtocolBuilder<"internal", Inputs>
6366
+ ): Machine.EventProtocol<"internal", Machine.EventProtocolInputSchemasOf<"internal", Inputs>>
6367
+ } = internal.internalEvents as any
6368
+
6369
+ /**
6370
+ * Defines the ephemeral notifications a machine may publish to external
6371
+ * observers. Emitted events are separate from actor input and are never sent
6372
+ * implicitly to a parent actor. Observe them through `MachineRef.emissions` or
6373
+ * the AtomMachine emission stream adapters.
6374
+ *
6375
+ * @category constructors
6376
+ * @since 0.10.0
6377
+ */
6378
+ export const emittedEvents: {
6379
+ <const Schemas extends ReadonlyArray<Machine.TaggedSchema>>(
6380
+ ...schemas: Schemas & ValidateEmittedEventProtocol<NoInfer<Schemas>>
6381
+ ): Machine.EventProtocol<"emitted", readonly [...Schemas]>
6382
+ <const Inputs extends ReadonlyArray<Machine.EventProtocolInput<"emitted">>>(
6383
+ ...inputs: Inputs & ValidateEventProtocolBuilder<"emitted", Inputs>
6384
+ ): Machine.EventProtocol<"emitted", Machine.EventProtocolInputSchemasOf<"emitted", Inputs>>
6385
+ } = internal.emittedEvents as any
5580
6386
 
5581
6387
  /**
5582
6388
  * Encodes a decoded machine snapshot into a normalized data representation.
@@ -5611,7 +6417,7 @@ export const event: <const M extends Machine.Any, const EventSchema extends Mach
5611
6417
  * const States = Machine.defineStates({ Idle })
5612
6418
  * const machine = Machine.make({
5613
6419
  * states: States.states,
5614
- * events: [],
6420
+ * events: Machine.events(),
5615
6421
  * initial: () => States.initial.Idle.from()
5616
6422
  * }).handle({ Idle: {} })
5617
6423
  *
@@ -5638,7 +6444,8 @@ export const encodeSnapshot: <
5638
6444
  FinalStates extends Machine.StateIdentifier<States> = never,
5639
6445
  Output = never,
5640
6446
  OutputStates extends Machine.StateIdentifier<States> = never,
5641
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
6447
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
6448
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
5642
6449
  >(
5643
6450
  machine: Machine<
5644
6451
  States,
@@ -5653,14 +6460,15 @@ export const encodeSnapshot: <
5653
6460
  Output,
5654
6461
  Emits,
5655
6462
  OutputStates,
5656
- InputEvents
6463
+ InputEvents,
6464
+ ParentEvents
5657
6465
  >,
5658
6466
  snapshot: Machine.Snapshot<States>
5659
6467
  ) => Effect.Effect<
5660
6468
  Machine.EncodedSnapshot,
5661
6469
  MachineSchemaEncodeError,
5662
6470
  Machine.SnapshotEncodingServices<States>
5663
- > = internal.encodeSnapshot
6471
+ > = internal.encodeSnapshot as any
5664
6472
 
5665
6473
  /**
5666
6474
  * Decodes a normalized data representation into a validated machine snapshot.
@@ -5691,7 +6499,7 @@ export const encodeSnapshot: <
5691
6499
  * const States = Machine.defineStates({ Idle })
5692
6500
  * const machine = Machine.make({
5693
6501
  * states: States.states,
5694
- * events: [],
6502
+ * events: Machine.events(),
5695
6503
  * initial: () => States.initial.Idle.from()
5696
6504
  * }).handle({ Idle: {} })
5697
6505
  *
@@ -5719,7 +6527,8 @@ export const decodeSnapshot: <
5719
6527
  FinalStates extends Machine.StateIdentifier<States> = never,
5720
6528
  Output = never,
5721
6529
  OutputStates extends Machine.StateIdentifier<States> = never,
5722
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
6530
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
6531
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
5723
6532
  >(
5724
6533
  machine: Machine<
5725
6534
  States,
@@ -5734,449 +6543,316 @@ export const decodeSnapshot: <
5734
6543
  Output,
5735
6544
  Emits,
5736
6545
  OutputStates,
5737
- InputEvents
6546
+ InputEvents,
6547
+ ParentEvents
5738
6548
  >,
5739
6549
  encoded: unknown
5740
6550
  ) => Effect.Effect<
5741
6551
  Machine.Snapshot<States>,
5742
6552
  MachineSchemaDecodeError,
5743
6553
  Machine.SnapshotDecodingServices<States>
5744
- > = internal.decodeSnapshot
6554
+ > = internal.decodeSnapshot as any
5745
6555
 
5746
- /**
5747
- * Creates an invoked child process configuration for an active state.
5748
- *
5749
- * **When to use**
5750
- *
5751
- * Use to run a child process while a machine remains in a state. Successful
5752
- * outputs are sent directly to the parent machine as events; `void` sends
5753
- * nothing. Unrecovered child failures fail the owning machine. Active
5754
- * snapshots can optionally be mapped to progress events.
5755
- *
5756
- * **Gotchas**
5757
- *
5758
- * Invoked child processes run while their owning state is active and are
5759
- * stopped before the state exits. An unrecovered child failure fails the owning
5760
- * machine; recover inside the child Effect when failure should become an event.
5761
- * The `id` is a state-local lifecycle key, not a communication address. To
5762
- * send events to the invocation, pass a typed `childAddress` as `address`.
5763
- * The `src` callback is intentionally independent from its parent state. When
5764
- * construction depends on the typed state, lifecycle event, or runtime, use
5765
- * the state config factory form `invoke: (context) => Machine.invoke(...)` and
5766
- * close over that context from `src`.
5767
- *
5768
- * **Example** (Effect output as a parent event)
5769
- *
5770
- * ```ts
5771
- * import { Effect, Schema } from "effect"
5772
- * import { Machine } from "@typeonce/effect-machine"
5773
- *
5774
- * class Loaded extends Schema.TaggedClass<Loaded>("Loaded")("Loaded", {
5775
- * value: Schema.String
5776
- * }) {}
5777
- *
5778
- * const load = Machine.invoke({
5779
- * id: "load",
5780
- * src: () => Machine.effect(Effect.succeed(new Loaded({ value: "ready" })))
5781
- * })
5782
- * ```
5783
- *
5784
- * @see {@link effect} for one-shot child effects.
5785
- * @see {@link spawn} for children whose lifetime is controlled by actions.
5786
- * @category constructors
5787
- * @since 0.4.0
5788
- */
5789
- export const invoke: <
5790
- ChildState,
5791
- ChildEvent,
5792
- ChildError = never,
5793
- ChildRequirements = never,
5794
- ChildOutput = never,
5795
- ChildInitialError = never,
5796
- Event = never,
5797
- Address extends ChildAddress<never> | undefined = undefined
5798
- >(
5799
- config:
5800
- & {
5801
- readonly id: InvokeLifecycleId
5802
- readonly src: () => Logic<
5803
- ChildState,
5804
- ChildEvent,
5805
- ChildError,
5806
- ChildRequirements,
5807
- ChildOutput,
5808
- ChildInitialError
5809
- >
5810
- readonly snapshot?: (
5811
- context: Machine.InvokeSnapshotContext<ChildState, ChildError, ChildOutput>
5812
- ) => Event | undefined
5813
- }
5814
- & ([Address] extends [undefined] ? {
5815
- readonly address?: never
5816
- }
5817
- : {
5818
- readonly address: Exclude<Address, undefined>
5819
- } & ChildAddress.Compatibility<Exclude<Address, undefined>, NoInfer<ChildEvent>>)
5820
- ) => Machine.InvokeConfig<
5821
- any,
5822
- any,
5823
- any,
5824
- any,
5825
- Event,
5826
- ChildState,
5827
- ChildEvent,
5828
- ChildError,
5829
- ChildRequirements,
5830
- ChildOutput,
5831
- ChildInitialError
5832
- > = internal.invoke
5833
-
5834
- type InvokeEffectResult<Requirements, Event> = Machine.InvokeConfig<
5835
- any,
5836
- any,
5837
- any,
5838
- any,
5839
- never,
5840
- void,
5841
- never,
5842
- never,
5843
- Requirements,
5844
- Event | void,
5845
- never
6556
+ type DynamicEffectInvokeSource<
6557
+ States extends Machine.StateSchemas,
6558
+ Events extends ReadonlyArray<Machine.TaggedSchema>,
6559
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
6560
+ StateId extends Machine.StateIdentifier<States>,
6561
+ Source extends (
6562
+ context: Machine.InvokeContext<States, Events, Emits, StateId>
6563
+ ) => Effect.Effect<unknown, unknown, unknown>
6564
+ > = {
6565
+ readonly id: InvokeLifecycleId
6566
+ readonly effect: Source
6567
+ readonly after?: never
6568
+ readonly logic?: never
6569
+ readonly child?: never
6570
+ readonly address?: never
6571
+ readonly onSnapshot?: never
6572
+ }
6573
+
6574
+ type DynamicEffectDoneHandler<
6575
+ States extends Machine.StateSchemas,
6576
+ Events extends ReadonlyArray<Machine.TaggedSchema>,
6577
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
6578
+ StateId extends Machine.StateIdentifier<States>,
6579
+ Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
6580
+ > = Machine.InvokeTransition<
6581
+ States,
6582
+ Events,
6583
+ Emits,
6584
+ Machine.InvokeDoneContext<States, Events, Emits, StateId, Effect.Success<ReturnType<NoInfer<Source>>>>
5846
6585
  >
5847
6586
 
5848
- type InvokeEffectIsInfallible<Fx extends Effect.Effect<any, any, any>> = IsAny<Effect.Error<Fx>> extends true ? false
5849
- : [Effect.Error<Fx>] extends [never] ? true
5850
- : false
6587
+ type DynamicEffectFailureHandler<
6588
+ States extends Machine.StateSchemas,
6589
+ Events extends ReadonlyArray<Machine.TaggedSchema>,
6590
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
6591
+ StateId extends Machine.StateIdentifier<States>,
6592
+ Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
6593
+ > = Machine.InvokeTransition<
6594
+ States,
6595
+ Events,
6596
+ Emits,
6597
+ Machine.InvokeFailureContext<States, Events, Emits, StateId, Effect.Error<ReturnType<NoInfer<Source>>>>
6598
+ >
5851
6599
 
5852
- type InvokeEffectConfig<
5853
- Fx extends Effect.Effect<any, any, any>,
5854
- SuccessEvent,
5855
- FailureEvent
6600
+ type DynamicEffectInvokeResult<
6601
+ States extends Machine.StateSchemas,
6602
+ Events extends ReadonlyArray<Machine.TaggedSchema>,
6603
+ Emits extends ReadonlyArray<Machine.TaggedSchema>,
6604
+ StateId extends Machine.StateIdentifier<States>,
6605
+ Source extends (...args: ReadonlyArray<never>) => Effect.Effect<unknown, unknown, unknown>
5856
6606
  > =
5857
- & {
5858
- readonly id: InvokeLifecycleId
5859
- readonly effect: Fx
5860
- readonly onSuccess: (value: NoInfer<Effect.Success<Fx>>) => SuccessEvent | void
5861
- }
5862
- & (
5863
- InvokeEffectIsInfallible<Fx> extends true ? {
5864
- readonly onFailure?: never
5865
- }
5866
- : {
5867
- readonly onFailure: (error: NoInfer<Effect.Error<Fx>>) => FailureEvent | void
5868
- }
5869
- )
6607
+ & Machine.InvokeConfig<States, Events, Emits, StateId>
6608
+ & Machine.InvokeTyped<
6609
+ Effect.Success<ReturnType<Source>>,
6610
+ Effect.Error<ReturnType<Source>>,
6611
+ Effect.Services<ReturnType<Source>>,
6612
+ never
6613
+ >
6614
+
6615
+ type InvokeChannelIsNever<Value> = IsAny<Value> extends true ? false : [Value] extends [never] ? true : false
5870
6616
 
5871
6617
  /**
5872
- * Invokes one Effect and maps its typed outcome into machine-local events.
6618
+ * Preserves inference for a state-owned invocation configuration.
5873
6619
  *
5874
- * **Details**
5875
- *
5876
- * This is the high-level one-shot counterpart to `invoke`. Success and typed
5877
- * failure values are mapped independently, so callers do not need to recover
5878
- * an Effect into a common event union by hand. Defects and interruption remain
5879
- * failures of the owning machine.
6620
+ * Use `effect` for one-shot work, `after` for a cancellable timer, `logic` for
6621
+ * reusable process logic, or `child` for a complete child machine. `onDone` is
6622
+ * required whenever the source can complete with an output, while `onFailure`
6623
+ * is required only when the source has a typed failure channel.
5880
6624
  *
5881
- * Declare mapped outcomes in `internalEvents` unless they are also legitimate
5882
- * public commands.
5883
- *
5884
- * **Example**
6625
+ * This constructor is an identity at runtime, but preserves lifecycle callback
6626
+ * inference through published declarations. Effects and durations may be
6627
+ * supplied directly or derived from the owning state's entry context. Dynamic
6628
+ * Effect sources infer the owner context, output, error, and service channels
6629
+ * together without a return annotation. Logic invocations require both a
6630
+ * lifecycle `id` and a typed communication `address`. Child descriptors already
6631
+ * own their identity, so `id` and `address` must not be repeated.
5885
6632
  *
5886
6633
  * ```ts
5887
- * import { Effect, Schema } from "effect"
5888
- * import { Machine } from "@typeonce/effect-machine"
5889
- *
5890
- * class Loaded extends Schema.TaggedClass<Loaded>("Loaded")("Loaded", {
5891
- * value: Schema.String
5892
- * }) {}
5893
- *
5894
- * const load = Machine.invokeEffect({
6634
+ * invoke: Machine.invoke({
5895
6635
  * id: "load",
5896
- * effect: Effect.succeed("ready"),
5897
- * onSuccess: (value) => new Loaded({ value })
6636
+ * effect: Effect.tryPromise({
6637
+ * try: () => fetch("/api/data").then((response) => response.json()),
6638
+ * catch: (cause) => new LoadError({ cause })
6639
+ * }),
6640
+ * onDone: ({ output, target }) => target.full.Ready({ data: output }),
6641
+ * onFailure: ({ error, target }) => target.full.Failed({ error })
5898
6642
  * })
5899
6643
  * ```
5900
6644
  *
5901
- * @see {@link invoke} for arbitrary child process logic.
5902
- * @see {@link after} for a state-scoped delayed event.
5903
- * @category constructors
5904
- * @since 0.4.0
5905
- */
5906
- export const invokeEffect: <const Fx extends Effect.Effect<any, any, any>, SuccessEvent, FailureEvent = never>(
5907
- config: InvokeEffectConfig<Fx, SuccessEvent, FailureEvent>
5908
- ) => InvokeEffectResult<
5909
- Effect.Services<Fx>,
5910
- SuccessEvent | (InvokeEffectIsInfallible<Fx> extends true ? never : FailureEvent)
5911
- > = internal.invokeEffect
5912
-
5913
- /**
5914
- * Creates a cancellable state-scoped delayed event.
5915
- *
5916
- * The timer starts when its owning state is entered and is interrupted when
5917
- * that state exits. The delayed value should normally be declared in
5918
- * `internalEvents`.
5919
- *
5920
- * **Example**
5921
- *
5922
- * ```ts
5923
- * import { Schema } from "effect"
5924
- * import { Machine } from "@typeonce/effect-machine"
5925
- *
5926
- * class TimedOut extends Schema.TaggedClass<TimedOut>("TimedOut")("TimedOut", {}) {}
5927
- *
5928
- * const timeout = Machine.after("5 seconds", new TimedOut({}))
5929
- * ```
5930
- *
5931
- * @category constructors
5932
- * @since 0.4.0
5933
- */
5934
- export const after: <Event extends { readonly _tag: PropertyKey }>(
5935
- duration: Duration.Input,
5936
- event: Event,
5937
- options?: { readonly id?: InvokeLifecycleId }
5938
- ) => InvokeEffectResult<never, Event> = internal.after
5939
-
5940
- type RetagFields<Target extends Machine.TaggedSchema> = Omit<Target["~type.make.in"], "_tag">
5941
-
5942
- type RetagTargetCompatibility<Target extends Machine.TaggedSchema> = Target extends {
5943
- readonly fields: Schema.Struct.Fields
5944
- } ? "_tag" extends keyof Target["~type.make.in"] ? {} extends Pick<Target["~type.make.in"], "_tag"> ? unknown : {
5945
- readonly "~effect/Machine/RetagTargetError": "Target schema must supply its discriminator when make is called"
5946
- }
5947
- : unknown
5948
- : {
5949
- readonly "~effect/Machine/RetagTargetError": "Target schema must be one tagged struct or tagged class"
5950
- }
5951
-
5952
- type RequiredKeys<A> = {
5953
- readonly [Key in keyof A]-?: {} extends Pick<A, Key> ? never : Key
5954
- }[keyof A]
5955
-
5956
- type CompatibleSourceKeys<Fields, Source> = {
5957
- readonly [Key in Extract<keyof Fields, keyof Source>]: Source[Key] extends Fields[Key] ? Key : never
5958
- }[Extract<keyof Fields, keyof Source>]
5959
-
5960
- type RetagPatch<Target extends Machine.TaggedSchema, Source> =
5961
- & Partial<RetagFields<Target>>
5962
- & Pick<
5963
- RetagFields<Target>,
5964
- Exclude<RequiredKeys<RetagFields<Target>>, CompatibleSourceKeys<RetagFields<Target>, Source>>
5965
- >
5966
-
5967
- type RetagArgs<Target extends Machine.TaggedSchema, Source> = [
5968
- Exclude<RequiredKeys<RetagFields<Target>>, CompatibleSourceKeys<RetagFields<Target>, Source>>
5969
- ] extends [never] ? [patch?: RetagPatch<Target, Source>]
5970
- : [patch: RetagPatch<Target, Source>]
5971
-
5972
- /**
5973
- * Constructs another tagged case from compatible source fields.
5974
- *
5975
- * The target must be one tagged struct or tagged class whose discriminator is
5976
- * supplied by its constructor. Union wrappers are rejected because their
5977
- * `make` operation cannot select a member after the source discriminator has
5978
- * been discarded. Missing or incompatible required target fields must be
5979
- * supplied by the patch, and the target schema's normal `make` validation
5980
- * remains authoritative at runtime.
5981
- *
5982
- * @category constructors
5983
- * @since 0.4.0
5984
- */
5985
- export const retag: <const Target extends Machine.TaggedSchema, const Source extends { readonly _tag: PropertyKey }>(
5986
- target: Target & RetagTargetCompatibility<Target>,
5987
- source: Source,
5988
- ...args: RetagArgs<Target, Source>
5989
- ) => Target["Type"] = internal.retag
5990
-
5991
- type InvokeMachineInput<Input extends Schema.Top> = Input extends typeof Schema.Void ? {
5992
- readonly input?: never
5993
- }
5994
- : {
5995
- readonly input: Input["Type"]
5996
- }
5997
-
5998
- /**
5999
- * Creates an invoked child process from a complete statechart machine.
6000
- *
6001
- * **When to use**
6002
- *
6003
- * Use when a state should own another statechart machine and communicate with
6004
- * it through typed child events, emissions, snapshots, or terminal output.
6005
- *
6006
- * **Details**
6007
- *
6008
- * Child emissions are delivered directly to the parent as events. Active
6009
- * snapshots and terminal output can be mapped to parent events. The owning
6010
- * state controls the child lifetime.
6011
- *
6012
- * **Gotchas**
6013
- *
6014
- * Active invoked machines must have unique child addresses. A machine starts
6015
- * after its owning state's entry actions, so those actions cannot send events
6016
- * to a newly entered child. Unrecovered child failures fail the parent.
6017
- *
6018
- * @see {@link invoke} for invoking lower-level process logic.
6019
- * @see {@link sendTo} for sending events to the invoked machine.
6020
6645
  * @category constructors
6021
- * @since 0.4.0
6646
+ * @since 0.9.0
6022
6647
  */
6023
- export const invokeMachine: {
6648
+ export const invoke: {
6024
6649
  <
6025
6650
  const States extends Machine.StateSchemas,
6026
6651
  const Events extends ReadonlyArray<Machine.TaggedSchema>,
6027
- const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
6028
- const Input extends Schema.Top = typeof Schema.Void,
6029
- UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
6030
- E = never,
6031
- R = never,
6032
- InitialE = never,
6033
- InitialR = never,
6034
- FinalStates extends Machine.StateIdentifier<States> = never,
6035
- Output = never,
6036
- SnapshotEvent = never,
6037
- DoneEvent = never,
6038
- Id extends string = string,
6039
- OutputStates extends Machine.StateIdentifier<States> = never,
6040
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
6652
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6653
+ StateId extends Machine.StateIdentifier<States>,
6654
+ const Source extends (
6655
+ context: Machine.InvokeContext<States, Events, Emits, StateId>
6656
+ ) => Effect.Effect<unknown, unknown, unknown>
6041
6657
  >(
6042
6658
  config:
6659
+ & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
6043
6660
  & {
6044
- readonly child: ChildMachine<
6045
- Id,
6046
- & Machine<
6047
- States,
6048
- Events,
6049
- Input,
6050
- UnhandledStates,
6051
- E,
6052
- R,
6053
- InitialE,
6054
- InitialR,
6055
- FinalStates,
6056
- Output,
6057
- Emits,
6058
- OutputStates,
6059
- InputEvents
6060
- >
6061
- & EnsureExecutable<States, UnhandledStates, OutputStates>
6062
- >
6063
- readonly snapshot?: (
6064
- context: Machine.InvokeSnapshotContext<
6065
- Machine.Snapshot<States>,
6066
- | E
6067
- | ActionError<R>
6068
- | InfiniteTransitionError
6069
- | MachineSchemaDecodeError
6070
- | StoppedError,
6071
- Output
6072
- >
6073
- ) => SnapshotEvent | undefined
6074
- readonly onDone: (context: Machine.InvokeDoneContext<Output>) => DoneEvent | undefined
6075
- }
6076
- & InvokeMachineInput<Input>
6077
- ): Machine.InvokeConfig<
6078
- any,
6079
- any,
6080
- any,
6081
- any,
6082
- SnapshotEvent,
6083
- Machine.Snapshot<States>,
6084
- Machine.EventOf<InputEvents>,
6085
- E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
6086
- ExcludeCompatibleRuntime<
6087
- Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>,
6088
- Machine.EventOf<Events>,
6089
- Machine.EmitOf<Emits>
6090
- >,
6091
- Output,
6092
- | InitialE
6093
- | E
6094
- | ActionError<InitialR | R>
6095
- | InfiniteTransitionError
6096
- | MachineSchemaDecodeError
6097
- | StartupError
6098
- | StoppedError,
6099
- Machine.EmitOf<Emits>,
6100
- DoneEvent
6101
- >
6661
+ readonly onDone: DynamicEffectDoneHandler<States, Events, Emits, StateId, Source>
6662
+ readonly onFailure: DynamicEffectFailureHandler<States, Events, Emits, StateId, Source>
6663
+ },
6664
+ ..._validation: InvokeChannelIsNever<Effect.Success<ReturnType<Source>>> extends true ? [
6665
+ "onDone must be omitted when the Effect output is never"
6666
+ ]
6667
+ : InvokeChannelIsNever<Effect.Error<ReturnType<Source>>> extends true ? [
6668
+ "onFailure must be omitted when the Effect error is never"
6669
+ ]
6670
+ : []
6671
+ ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
6102
6672
  <
6103
6673
  const States extends Machine.StateSchemas,
6104
6674
  const Events extends ReadonlyArray<Machine.TaggedSchema>,
6105
- const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
6106
- const Input extends Schema.Top = typeof Schema.Void,
6107
- UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
6108
- E = never,
6109
- R = never,
6110
- InitialE = never,
6111
- InitialR = never,
6112
- FinalStates extends Machine.StateIdentifier<States> = never,
6113
- Output = never,
6114
- SnapshotEvent = never,
6115
- Id extends string = string,
6116
- OutputStates extends Machine.StateIdentifier<States> = never,
6117
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
6675
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6676
+ StateId extends Machine.StateIdentifier<States>,
6677
+ const Source extends (
6678
+ context: Machine.InvokeContext<States, Events, Emits, StateId>
6679
+ ) => Effect.Effect<unknown, never, unknown>
6118
6680
  >(
6119
6681
  config:
6682
+ & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
6683
+ & {
6684
+ readonly onDone: DynamicEffectDoneHandler<States, Events, Emits, StateId, Source>
6685
+ readonly onFailure?: never
6686
+ },
6687
+ ..._validation: InvokeChannelIsNever<Effect.Success<ReturnType<Source>>> extends true ? [
6688
+ "onDone must be omitted when the Effect output is never"
6689
+ ]
6690
+ : []
6691
+ ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
6692
+ <
6693
+ const States extends Machine.StateSchemas,
6694
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6695
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6696
+ StateId extends Machine.StateIdentifier<States>,
6697
+ const Source extends (
6698
+ context: Machine.InvokeContext<States, Events, Emits, StateId>
6699
+ ) => Effect.Effect<never, unknown, unknown>
6700
+ >(
6701
+ config:
6702
+ & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
6703
+ & {
6704
+ readonly onDone?: never
6705
+ readonly onFailure: DynamicEffectFailureHandler<States, Events, Emits, StateId, Source>
6706
+ },
6707
+ ..._validation: InvokeChannelIsNever<Effect.Error<ReturnType<Source>>> extends true ? [
6708
+ "onFailure must be omitted when the Effect error is never"
6709
+ ]
6710
+ : []
6711
+ ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
6712
+ <
6713
+ const States extends Machine.StateSchemas,
6714
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6715
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6716
+ StateId extends Machine.StateIdentifier<States>,
6717
+ const Source extends (
6718
+ context: Machine.InvokeContext<States, Events, Emits, StateId>
6719
+ ) => Effect.Effect<never, never, unknown>
6720
+ >(
6721
+ config:
6722
+ & DynamicEffectInvokeSource<States, Events, Emits, StateId, Source>
6120
6723
  & {
6121
- readonly child: ChildMachine<
6122
- Id,
6123
- & Machine<
6124
- States,
6125
- Events,
6126
- Input,
6127
- UnhandledStates,
6128
- E,
6129
- R,
6130
- InitialE,
6131
- InitialR,
6132
- FinalStates,
6133
- Output,
6134
- Emits,
6135
- OutputStates,
6136
- InputEvents
6137
- >
6138
- & EnsureExecutable<States, UnhandledStates, OutputStates>
6139
- >
6140
- readonly snapshot?: (
6141
- context: Machine.InvokeSnapshotContext<
6142
- Machine.Snapshot<States>,
6143
- | E
6144
- | ActionError<R>
6145
- | InfiniteTransitionError
6146
- | MachineSchemaDecodeError
6147
- | StoppedError,
6148
- Output
6149
- >
6150
- ) => SnapshotEvent | undefined
6151
6724
  readonly onDone?: never
6725
+ readonly onFailure?: never
6152
6726
  }
6153
- & InvokeMachineInput<Input>
6154
- ): Machine.InvokeConfig<
6155
- any,
6156
- any,
6157
- any,
6158
- any,
6159
- SnapshotEvent,
6160
- Machine.Snapshot<States>,
6161
- Machine.EventOf<InputEvents>,
6162
- E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
6163
- ExcludeCompatibleRuntime<
6164
- Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>,
6165
- Machine.EventOf<Events>,
6166
- Machine.EmitOf<Emits>
6167
- >,
6168
- Output,
6169
- | InitialE
6170
- | E
6171
- | ActionError<InitialR | R>
6172
- | InfiniteTransitionError
6173
- | MachineSchemaDecodeError
6174
- | StartupError
6175
- | StoppedError,
6176
- Machine.EmitOf<Emits>
6177
- >
6178
- } = internal.invokeMachine
6179
-
6727
+ ): DynamicEffectInvokeResult<States, Events, Emits, StateId, Source>
6728
+ <
6729
+ const States extends Machine.StateSchemas,
6730
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6731
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6732
+ StateId extends Machine.StateIdentifier<States>,
6733
+ const Fx extends Effect.Effect<any, any, any>,
6734
+ const Config extends object
6735
+ >(
6736
+ config: Config & Machine.EffectInvokeArgs<States, Events, Emits, StateId, Fx>
6737
+ ):
6738
+ & Config
6739
+ & Machine.InvokeOwned<States, Events, Emits, StateId>
6740
+ & Machine.InvokeTyped<
6741
+ Effect.Success<Fx>,
6742
+ Effect.Error<Fx>,
6743
+ Effect.Services<Fx>,
6744
+ never
6745
+ >
6746
+ <
6747
+ const States extends Machine.StateSchemas,
6748
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6749
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6750
+ StateId extends Machine.StateIdentifier<States>,
6751
+ const Config extends object
6752
+ >(
6753
+ config: Config & Machine.TimerInvokeArgs<States, Events, Emits, StateId>
6754
+ ): Config & Machine.InvokeOwned<States, Events, Emits, StateId> & Machine.InvokeTyped<void, never, never, never>
6755
+ <
6756
+ const States extends Machine.StateSchemas,
6757
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6758
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6759
+ StateId extends Machine.StateIdentifier<States>,
6760
+ ChildState,
6761
+ ChildEvent,
6762
+ ChildError,
6763
+ ChildRequirements,
6764
+ ChildOutput,
6765
+ ChildInitialError,
6766
+ Address extends ChildAddress<never>
6767
+ >(
6768
+ config: Machine.LogicInvokeArgs<
6769
+ States,
6770
+ Events,
6771
+ Emits,
6772
+ StateId,
6773
+ ChildState,
6774
+ ChildEvent,
6775
+ ChildError,
6776
+ ChildRequirements,
6777
+ ChildOutput,
6778
+ ChildInitialError,
6779
+ Address,
6780
+ (context: Machine.InvokeContext<States, Events, Emits, StateId>) => Logic<
6781
+ ChildState,
6782
+ ChildEvent,
6783
+ ChildError,
6784
+ ChildRequirements,
6785
+ ChildOutput,
6786
+ ChildInitialError
6787
+ >
6788
+ >
6789
+ ):
6790
+ & Machine.InvokeConfig<States, Events, Emits, StateId>
6791
+ & Machine.InvokeTyped<
6792
+ ChildOutput,
6793
+ ChildError,
6794
+ ChildRequirements,
6795
+ ChildInitialError
6796
+ >
6797
+ <
6798
+ const States extends Machine.StateSchemas,
6799
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6800
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6801
+ StateId extends Machine.StateIdentifier<States>,
6802
+ ChildState,
6803
+ ChildEvent,
6804
+ ChildError,
6805
+ ChildRequirements,
6806
+ ChildOutput,
6807
+ ChildInitialError,
6808
+ Address extends ChildAddress<never>,
6809
+ const Config extends object
6810
+ >(
6811
+ config:
6812
+ & Config
6813
+ & Machine.LogicInvokeArgs<
6814
+ States,
6815
+ Events,
6816
+ Emits,
6817
+ StateId,
6818
+ ChildState,
6819
+ ChildEvent,
6820
+ ChildError,
6821
+ ChildRequirements,
6822
+ ChildOutput,
6823
+ ChildInitialError,
6824
+ Address
6825
+ >
6826
+ ):
6827
+ & Config
6828
+ & Machine.InvokeOwned<States, Events, Emits, StateId>
6829
+ & Machine.InvokeTyped<
6830
+ ChildOutput,
6831
+ ChildError,
6832
+ ChildRequirements,
6833
+ ChildInitialError
6834
+ >
6835
+ <
6836
+ const States extends Machine.StateSchemas,
6837
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
6838
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
6839
+ StateId extends Machine.StateIdentifier<States>,
6840
+ const Child extends ChildMachine.Any,
6841
+ const Config extends object
6842
+ >(
6843
+ config: Config & Machine.ChildInvokeArgs<States, Events, Emits, StateId, Child["machine"], Child>
6844
+ ):
6845
+ & Config
6846
+ & Machine.InvokeOwned<States, Events, Emits, StateId>
6847
+ & Machine.InvokeTyped<
6848
+ Machine.Output<Child["machine"]>,
6849
+ Machine.Error<Child["machine"]> | ActionError<Machine.Services<Child["machine"]>>,
6850
+ Machine.Services<Child["machine"]>,
6851
+ Machine.InitialError<Child["machine"]>,
6852
+ Machine.Emit<Child["machine"]>,
6853
+ Machine.EventOf<Machine.ParentEvents<Child["machine"]>>
6854
+ >
6855
+ } = ((config: unknown) => config) as any
6180
6856
  /**
6181
6857
  * Plans the initial state for a machine without executing actor commands.
6182
6858
  *
@@ -6205,7 +6881,7 @@ export const invokeMachine: {
6205
6881
  * const States = Machine.defineStates({ Idle })
6206
6882
  * const machine = Machine.make({
6207
6883
  * states: States.states,
6208
- * events: [],
6884
+ * events: Machine.events(),
6209
6885
  * initial: () => States.initial.Idle.from()
6210
6886
  * }).handle({ Idle: {} })
6211
6887
  *
@@ -6230,7 +6906,8 @@ export const planInitial: <
6230
6906
  FinalStates extends Machine.StateIdentifier<States> = never,
6231
6907
  Output = never,
6232
6908
  OutputStates extends Machine.StateIdentifier<States> = never,
6233
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
6909
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
6910
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
6234
6911
  >(
6235
6912
  machine:
6236
6913
  & Machine<
@@ -6246,7 +6923,8 @@ export const planInitial: <
6246
6923
  Output,
6247
6924
  Emits,
6248
6925
  OutputStates,
6249
- InputEvents
6926
+ InputEvents,
6927
+ ParentEvents
6250
6928
  >
6251
6929
  & EnsureExecutable<States, UnhandledStates, OutputStates>,
6252
6930
  ...args: [...Machine.InputArgs<Input>]
@@ -6287,7 +6965,7 @@ export const planInitial: <
6287
6965
  ),
6288
6966
  InitialE | E | InfiniteTransitionError | MachineSchemaDecodeError | StartupError,
6289
6967
  never
6290
- > = internal.planInitial
6968
+ > = internal.planInitial as any
6291
6969
 
6292
6970
  /**
6293
6971
  * Returns every compiled state node in definition order.
@@ -6338,10 +7016,9 @@ export const transitionDefinitions: <M extends Machine.Any>(machine: M) => Reado
6338
7016
  *
6339
7017
  * **Details**
6340
7018
  *
6341
- * Static `invoke`, `invokeEffect`, `after`, and `invokeMachine` descriptors
6342
- * expose stable ownership and lifecycle metadata without serializing runtime
6343
- * values. Function-valued invoke factories are represented as dynamic and are
6344
- * never evaluated during inspection.
7019
+ * Static inline invocation definitions expose stable ownership and lifecycle
7020
+ * metadata without serializing runtime values. Function-valued sources are
7021
+ * represented as dynamic and are never evaluated during inspection.
6345
7022
  *
6346
7023
  * @category getters
6347
7024
  * @since 0.4.0
@@ -6392,7 +7069,8 @@ export const enabled: <
6392
7069
  FinalStates extends Machine.StateIdentifier<States> = never,
6393
7070
  Output = never,
6394
7071
  OutputStates extends Machine.StateIdentifier<States> = never,
6395
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
7072
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
7073
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
6396
7074
  >(
6397
7075
  machine: Machine<
6398
7076
  States,
@@ -6407,10 +7085,11 @@ export const enabled: <
6407
7085
  Output,
6408
7086
  Emits,
6409
7087
  OutputStates,
6410
- InputEvents
7088
+ InputEvents,
7089
+ ParentEvents
6411
7090
  >,
6412
7091
  state: Machine.Snapshot<States>
6413
- ) => ReadonlyArray<Machine.TagOf<Events[number]>> = internal.enabled
7092
+ ) => ReadonlyArray<Machine.TagOf<Events[number]>> = internal.enabled as any
6414
7093
 
6415
7094
  /**
6416
7095
  * Plans the next state snapshot synchronously.
@@ -6441,7 +7120,7 @@ export const enabled: <
6441
7120
  * const States = Machine.defineStates({ Off, On })
6442
7121
  * const machine = Machine.make({
6443
7122
  * states: States.states,
6444
- * events: [Toggle],
7123
+ * events: Machine.events(Toggle),
6445
7124
  * initial: () => States.initial.Off.from()
6446
7125
  * }).handle({ Off: { on: { Toggle: () => States.initial.On.from() } }, On: {} })
6447
7126
  *
@@ -6469,7 +7148,8 @@ export const plan: <
6469
7148
  FinalStates extends Machine.StateIdentifier<States> = never,
6470
7149
  Output = never,
6471
7150
  OutputStates extends Machine.StateIdentifier<States> = never,
6472
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
7151
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
7152
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
6473
7153
  >(
6474
7154
  machine:
6475
7155
  & Machine<
@@ -6485,11 +7165,12 @@ export const plan: <
6485
7165
  Output,
6486
7166
  Emits,
6487
7167
  OutputStates,
6488
- InputEvents
7168
+ InputEvents,
7169
+ ParentEvents
6489
7170
  >
6490
7171
  & EnsureExecutable<States, UnhandledStates, OutputStates>,
6491
7172
  state: Machine.Snapshot<States>,
6492
- event: Machine.EventOf<InputEvents>
7173
+ event: Machine.EventInputOf<InputEvents>
6493
7174
  ) => Effect.Effect<
6494
7175
  & {
6495
7176
  readonly next: Machine.Snapshot<States>
@@ -6525,53 +7206,7 @@ export const plan: <
6525
7206
  ),
6526
7207
  E | InfiniteTransitionError | MachineSchemaDecodeError,
6527
7208
  never
6528
- > = internal.plan
6529
-
6530
- /**
6531
- * Creates a one-shot child process from an Effect.
6532
- *
6533
- * **When to use**
6534
- *
6535
- * Use when you need side effects that produce one typed output or error.
6536
- *
6537
- * **Details**
6538
- *
6539
- * The Effect may run arbitrary side effects. Its success value is the process
6540
- * output, its typed error is preserved, and its services are inferred. When
6541
- * invoked, the output is sent to the owning machine as an event unless it is
6542
- * `void`.
6543
- *
6544
- * **Gotchas**
6545
- *
6546
- * This process has no incoming event protocol. Its Effect runs once. Use
6547
- * `transition` for a process that receives events over time and `logic` for
6548
- * direct machine-local communication or intermediate snapshots.
6549
- *
6550
- * **Example** (Recover a child failure as output)
6551
- *
6552
- * ```ts
6553
- * import { Effect, Schema } from "effect"
6554
- * import { Machine } from "@typeonce/effect-machine"
6555
- *
6556
- * class LoadFailed extends Schema.TaggedClass<LoadFailed>("LoadFailed")("LoadFailed", {
6557
- * reason: Schema.String
6558
- * }) {}
6559
- *
6560
- * const load = Machine.effect(
6561
- * Effect.fail("unavailable").pipe(
6562
- * Effect.catch((reason) => Effect.succeed(new LoadFailed({ reason })))
6563
- * )
6564
- * )
6565
- * ```
6566
- *
6567
- * @see {@link transition} for event-driven state.
6568
- * @see {@link logic} for direct control over intermediate snapshots.
6569
- * @category constructors
6570
- * @since 0.4.0
6571
- */
6572
- export const effect: <Output, Error = never, Requirements = never>(
6573
- effect: Effect.Effect<Output, Error, Requirements>
6574
- ) => Logic<void, never, Error, Requirements, Output> = internal.effect
7209
+ > = internal.plan as any
6575
7210
 
6576
7211
  /**
6577
7212
  * Creates advanced stateful process logic from explicit initialization and
@@ -6594,9 +7229,9 @@ export const effect: <Output, Error = never, Requirements = never>(
6594
7229
  * This is the low-level process constructor. Parent messages sent directly
6595
7230
  * through its scope are intentionally `unknown` because the logic does not know
6596
7231
  * which machine will eventually own it. Prefer typed output, typed child
6597
- * addresses, or invoke snapshot mapping when possible.
7232
+ * addresses, or invocation lifecycle transitions when possible.
6598
7233
  *
6599
- * @see {@link effect} for one-shot work.
7234
+ * Use an inline `invoke` with an `effect` source for one-shot work.
6600
7235
  * @see {@link transition} for event-driven state.
6601
7236
  * @category constructors
6602
7237
  * @since 0.4.0
@@ -6645,7 +7280,7 @@ export const logic: <
6645
7280
  * )
6646
7281
  * ```
6647
7282
  *
6648
- * @see {@link effect} for one-shot work.
7283
+ * Use an inline `invoke` with an `effect` source for one-shot work.
6649
7284
  * @see {@link logic} for direct process lifecycle control.
6650
7285
  * @category constructors
6651
7286
  * @since 0.4.0
@@ -6807,7 +7442,7 @@ export const watch: <State, Event, Error = never, Output = never>(
6807
7442
  * const States = Machine.defineStates({ Idle })
6808
7443
  * const machine = Machine.make({
6809
7444
  * states: States.states,
6810
- * events: [],
7445
+ * events: Machine.events(),
6811
7446
  * initial: () => States.initial.Idle.from()
6812
7447
  * }).handle({ Idle: {} })
6813
7448
  *
@@ -6835,7 +7470,8 @@ export const start: <
6835
7470
  FinalStates extends Machine.StateIdentifier<States> = never,
6836
7471
  Output = never,
6837
7472
  OutputStates extends Machine.StateIdentifier<States> = never,
6838
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
7473
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
7474
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
6839
7475
  >(
6840
7476
  machine:
6841
7477
  & Machine<
@@ -6851,20 +7487,22 @@ export const start: <
6851
7487
  Output,
6852
7488
  Emits,
6853
7489
  OutputStates,
6854
- InputEvents
7490
+ InputEvents,
7491
+ ParentEvents
6855
7492
  >
6856
7493
  & EnsureExecutable<States, UnhandledStates, OutputStates>,
6857
7494
  ...args: [...Machine.InputArgs<Input>]
6858
7495
  ) => Effect.Effect<
6859
7496
  MachineRef<
6860
7497
  Machine.Snapshot<States>,
6861
- Machine.EventOf<InputEvents>,
7498
+ Machine.EventInputOf<InputEvents>,
6862
7499
  | E
6863
7500
  | ActionError<R>
6864
7501
  | InfiniteTransitionError
6865
7502
  | MachineSchemaDecodeError
6866
7503
  | StoppedError,
6867
- Output
7504
+ Output,
7505
+ Machine.EmittedEventOf<Emits>
6868
7506
  >,
6869
7507
  | InitialE
6870
7508
  | E
@@ -6878,7 +7516,7 @@ export const start: <
6878
7516
  Machine.EventOf<Events>,
6879
7517
  Machine.EmitOf<Emits>
6880
7518
  >
6881
- > = internal.start
7519
+ > = internal.start as any
6882
7520
 
6883
7521
  /**
6884
7522
  * Starts a fresh managed runtime from a decoded logical snapshot.
@@ -6890,8 +7528,8 @@ export const start: <
6890
7528
  * entry or transition actions, re-deliver raised or emitted events, or
6891
7529
  * re-evaluate historical completion and eventless transitions. Active-state
6892
7530
  * invokes start once in ancestor and document order with {@link InitialEvent};
6893
- * delayed invokes restart their complete duration and invoked machines start
6894
- * from their own initial state.
7531
+ * timer invocations restart their complete duration and child-machine
7532
+ * invocations start from their own initial state.
6895
7533
  *
6896
7534
  * Only logical state, completion, and history metadata are resumed. Queues,
6897
7535
  * scopes, subscriptions, fibers, spawned children, invoke progress, and prior
@@ -6914,7 +7552,7 @@ export const start: <
6914
7552
  * const States = Machine.defineStates({ Idle })
6915
7553
  * const machine = Machine.make({
6916
7554
  * states: States.states,
6917
- * events: [],
7555
+ * events: Machine.events(),
6918
7556
  * initial: () => States.initial.Idle.from()
6919
7557
  * }).handle({ Idle: {} })
6920
7558
  *
@@ -6944,7 +7582,8 @@ export const resume: <
6944
7582
  FinalStates extends Machine.StateIdentifier<States> = never,
6945
7583
  Output = never,
6946
7584
  OutputStates extends Machine.StateIdentifier<States> = never,
6947
- InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events
7585
+ InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events,
7586
+ ParentEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []
6948
7587
  >(
6949
7588
  machine:
6950
7589
  & Machine<
@@ -6960,20 +7599,22 @@ export const resume: <
6960
7599
  Output,
6961
7600
  Emits,
6962
7601
  OutputStates,
6963
- InputEvents
7602
+ InputEvents,
7603
+ ParentEvents
6964
7604
  >
6965
7605
  & EnsureExecutable<States, UnhandledStates, OutputStates>,
6966
7606
  snapshot: Machine.Snapshot<States>
6967
7607
  ) => Effect.Effect<
6968
7608
  MachineRef<
6969
7609
  Machine.Snapshot<States>,
6970
- Machine.EventOf<InputEvents>,
7610
+ Machine.EventInputOf<InputEvents>,
6971
7611
  | E
6972
7612
  | ActionError<R>
6973
7613
  | InfiniteTransitionError
6974
7614
  | MachineSchemaDecodeError
6975
7615
  | StoppedError,
6976
- Output
7616
+ Output,
7617
+ Machine.EmittedEventOf<Emits>
6977
7618
  >,
6978
7619
  MachineSchemaDecodeError,
6979
7620
  ExcludeCompatibleRuntime<
@@ -6981,4 +7622,4 @@ export const resume: <
6981
7622
  Machine.EventOf<Events>,
6982
7623
  Machine.EmitOf<Emits>
6983
7624
  >
6984
- > = internal.resume
7625
+ > = internal.resume as any