@typeonce/effect-machine 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Machine.d.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  * @since 4.0.0
5
5
  */
6
6
  import type * as Cause from "effect/Cause";
7
+ import type * as Duration from "effect/Duration";
7
8
  import * as Effect from "effect/Effect";
8
9
  import * as Option from "effect/Option";
9
10
  import { type Pipeable } from "effect/Pipeable";
@@ -30,6 +31,8 @@ export type TypeId = "~effect/Machine";
30
31
  * @since 4.0.0
31
32
  */
32
33
  export declare const TypeId: TypeId;
34
+ declare const MachineOutputStatesTypeId: unique symbol;
35
+ declare const MachineTypeId: unique symbol;
33
36
  /**
34
37
  * Type identifier used for the synthetic event passed to startup lifecycle
35
38
  * actions.
@@ -76,26 +79,69 @@ type IsAny<A> = 0 extends (1 & A) ? true : false;
76
79
  *
77
80
  * **Gotchas**
78
81
  *
79
- * History states, delayed transitions, and declarative first-class guards are
80
- * not part of the current API. Conditional behavior can be expressed in typed
81
- * handlers with ordinary TypeScript control flow.
82
+ * Declarative first-class guards are not part of the current API. Conditional
83
+ * behavior can be expressed in typed handlers with ordinary TypeScript control
84
+ * flow. Use `after` for cancellable state-scoped delayed events.
82
85
  *
83
86
  * @category models
84
87
  * @since 4.0.0
85
88
  */
86
- export interface Machine<States extends Machine.StateSchemas, Events extends ReadonlyArray<Machine.TaggedSchema>, Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], OutputStates extends Machine.StateIdentifier<States> = never> extends Pipeable {
89
+ export interface Machine<States extends Machine.StateSchemas, Events extends ReadonlyArray<Machine.TaggedSchema>, Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events> extends Pipeable {
87
90
  readonly [TypeId]: TypeId;
91
+ /** @internal Stable type-level carrier for machine protocol extraction. */
92
+ readonly [MachineTypeId]: Machine.TypeCarrier<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents>;
93
+ /** @internal Prevents output implementation evidence from being widened. */
94
+ readonly [MachineOutputStatesTypeId]: Readonly<Record<OutputStates, true>>;
95
+ /**
96
+ * State tree that defines the machine topology and state value schemas.
97
+ *
98
+ * @since 4.0.0
99
+ */
88
100
  readonly states: States;
89
- readonly events: Events;
101
+ /**
102
+ * Events accepted through public machine input boundaries.
103
+ *
104
+ * @since 4.0.0
105
+ */
106
+ readonly events: InputEvents;
107
+ /**
108
+ * Events reserved for invokes, child emissions, and other machine-local work.
109
+ *
110
+ * @since 4.0.0
111
+ */
112
+ readonly internalEvents: ReadonlyArray<Machine.TaggedSchema>;
113
+ /**
114
+ * Events that the machine may emit to its parent or external adapter.
115
+ *
116
+ * @since 4.0.0
117
+ */
90
118
  readonly emits: Emits;
119
+ /**
120
+ * Optional schema used to decode the machine input before initialization.
121
+ *
122
+ * @since 4.0.0
123
+ */
91
124
  readonly input: Input | undefined;
125
+ /**
126
+ * Optional stable identity used by runtime and persistence integrations.
127
+ *
128
+ * @since 4.0.0
129
+ */
92
130
  readonly id: string | undefined;
93
131
  /** @internal */
94
132
  readonly stateNodes: Machine.StateNodes;
95
133
  /** @internal */
96
134
  readonly makeTargetBuilder: <Source extends Machine.StateIdentifier<States>>(source: Source) => Machine.TargetBuilder<States, Source>;
135
+ /** @internal */
97
136
  readonly handlers: Machine.StateConfigs<States, Events, Emits, UnhandledStates, Machine.TagOf<Events[number]>, E, R>;
98
- readonly handle: Machine.Handler<States, Events, Emits, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, OutputStates>;
137
+ /**
138
+ * Adds typed state handlers and returns the refined machine definition.
139
+ * Successive calls implement the remaining unhandled states while retaining
140
+ * accumulated errors, services, final states, and output evidence.
141
+ *
142
+ * @since 4.0.0
143
+ */
144
+ readonly handle: Machine.Handler<States, Events, Emits, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, OutputStates, InputEvents>;
99
145
  /** @internal */
100
146
  readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>;
101
147
  }
@@ -207,7 +253,17 @@ export type ExecutionServices<Requirements> = Exclude<PlanningServices<Requireme
207
253
  * @since 4.0.0
208
254
  */
209
255
  export interface Runtime<in Events, in Emits> {
256
+ /**
257
+ * Queues an event for the current machine macrostep.
258
+ *
259
+ * @since 4.0.0
260
+ */
210
261
  readonly raise: (event: Events) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>;
262
+ /**
263
+ * Emits an event through the running machine's parent boundary.
264
+ *
265
+ * @since 4.0.0
266
+ */
211
267
  readonly sendParent: (event: Emits) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>;
212
268
  }
213
269
  /**
@@ -259,22 +315,21 @@ export declare namespace Runtime {
259
315
  }
260
316
  type ExcludeCompatibleRuntime<Requirements, Events, Emits> = Requirements extends Runtime.Requirement<infer RequiredEvents, infer RequiredEmits> ? IsAny<Requirements> extends true ? Requirements : [RequiredEvents] extends [Events] ? [RequiredEmits] extends [Emits] ? never : Requirements : Requirements : Requirements;
261
317
  type IncompatibleRuntime<Requirements, Events, Emits> = Requirements extends Runtime.Requirement<infer RequiredEvents, infer RequiredEmits> ? IsAny<Requirements> extends true ? never : [RequiredEvents] extends [Events] ? [RequiredEmits] extends [Emits] ? never : Requirements : Requirements : never;
262
- declare const RuntimeCompatibilityErrorTypeId = "~effect/Machine/RuntimeCompatibilityError";
263
318
  declare const InvokeTypeId: unique symbol;
264
- type EnsureCompatibleRuntime<Requirements, Events, Emits> = [IncompatibleRuntime<Requirements, Events, Emits>] extends [
265
- never
266
- ] ? unknown : {
267
- readonly [RuntimeCompatibilityErrorTypeId]: IncompatibleRuntime<Requirements, Events, Emits>;
268
- };
269
319
  type StateDefinitionError<Message extends string> = {
270
320
  readonly "~effect/Machine/DefinitionError": Message;
271
321
  };
272
- type ValidateStateTree<States extends Machine.StateSchemas> = {
273
- readonly [Key in keyof States]: ValidateStateNode<States[Key]>;
322
+ type ActiveStateKey<States extends Machine.StateSchemas> = Machine.ActiveStateKey<States>;
323
+ type HistoryStateKey<States extends Machine.StateSchemas> = Machine.HistoryStateKey<States>;
324
+ type ValidateStateTree<States extends Machine.StateSchemas, AllowHistory extends boolean = false> = {
325
+ readonly [Key in keyof States]: ValidateStateNode<States[Key], AllowHistory>;
274
326
  };
275
- type ValidateStateNode<Node> = Node extends Machine.TaggedSchema ? unknown : Node extends {
327
+ type ValidateStateNode<Node, AllowHistory extends boolean> = Node extends Machine.HistoryStateNodeConfig ? AllowHistory extends true ? ValidateHistoryStateNode<Node> : StateDefinitionError<"History states must be declared below an active parent state"> : Node extends Machine.TaggedSchema ? unknown : Node extends {
276
328
  readonly schema: Machine.TaggedSchema;
277
329
  } ? ValidateStateNodeConfig<Node> : StateDefinitionError<"State nodes must be tagged schemas or state node configs">;
330
+ type ValidateHistoryStateNode<Node extends Machine.HistoryStateNodeConfig> = [
331
+ Extract<keyof Node, "schema" | "states" | "initial" | "output">
332
+ ] extends [never] ? unknown : StateDefinitionError<"History states cannot declare schemas, children, initial states, or output">;
278
333
  type ValidateStateNodeConfig<Node extends {
279
334
  readonly schema: Machine.TaggedSchema;
280
335
  }> = Node extends {
@@ -290,14 +345,14 @@ type ValidateStateNodeWithChildren<Node extends {
290
345
  } ? StateDefinitionError<"Final states cannot declare child states"> : Node extends {
291
346
  readonly type: "parallel";
292
347
  } ? "initial" extends keyof Node ? StateDefinitionError<"Parallel states cannot declare an initial child"> : {
293
- readonly states: ValidateStateTree<Children>;
348
+ readonly states: ValidateStateTree<Children, true>;
294
349
  } & ValidateOutputSchema<Node> : "output" extends keyof Node ? StateDefinitionError<"Only final and parallel states can declare output"> : ValidateCompoundStateNode<Node, Children> : StateDefinitionError<"Child states must be a state tree">;
295
350
  type ValidateCompoundStateNode<Node extends {
296
351
  readonly schema: Machine.TaggedSchema;
297
352
  }, Children extends Machine.StateSchemas> = Node extends {
298
353
  readonly initial: infer Initial;
299
- } ? Initial extends Extract<keyof Children, string> ? {
300
- readonly states: ValidateStateTree<Children>;
354
+ } ? Initial extends ActiveStateKey<Children> ? {
355
+ readonly states: ValidateStateTree<Children, true>;
301
356
  } : StateDefinitionError<"Compound initial must be one of its direct child keys"> : StateDefinitionError<"Compound states must declare an initial child">;
302
357
  type ValidateStateNodeWithoutChildren<Node extends {
303
358
  readonly schema: Machine.TaggedSchema;
@@ -307,7 +362,7 @@ type ValidateStateNodeWithoutChildren<Node extends {
307
362
  type DefineStateTreeInput<States extends Machine.StateSchemas> = {
308
363
  readonly [Key in keyof States]: DefineStateNodeInput<States[Key]>;
309
364
  };
310
- type DefineStateNodeInput<Node> = Node extends Machine.TaggedSchema ? Node : Node extends {
365
+ type DefineStateNodeInput<Node> = Node extends Machine.TaggedSchema ? Node : Node extends Machine.HistoryStateNodeConfig ? Machine.HistoryStateNodeConfig : Node extends {
311
366
  readonly type: "parallel";
312
367
  readonly states: infer Children extends Machine.StateSchemas;
313
368
  } ? Omit<Node, "states"> & {
@@ -315,21 +370,60 @@ type DefineStateNodeInput<Node> = Node extends Machine.TaggedSchema ? Node : Nod
315
370
  } : Node extends {
316
371
  readonly states: infer Children extends Machine.StateSchemas;
317
372
  } ? Omit<Node, "initial" | "states"> & {
318
- readonly initial: Extract<keyof Children, string>;
373
+ readonly initial: ActiveStateKey<Children>;
319
374
  readonly states: DefineStateTreeInput<Children>;
320
375
  } : Node;
321
376
  type ValidateDefinedStates<States extends Machine.StateSchemas> = [States] extends [
322
377
  Machine.ValidateStateSchemas<States>
323
378
  ] ? [] : [validation: Machine.ValidateStateSchemas<States>];
379
+ type InvalidDefinedStateTreeInput<States extends Machine.StateSchemas> = [States] extends [
380
+ Machine.ValidateStateSchemas<States>
381
+ ] ? never : States & Machine.ValidateStateSchemas<States>;
382
+ interface DefineStates {
383
+ <const States extends Machine.StateSchemas>(states: States & DefineStateTreeInput<NoInfer<States>>, ..._validation: ValidateDefinedStates<NoInfer<States>>): Machine.DefinedStates<States>;
384
+ <const States extends Machine.StateSchemas>(states: InvalidDefinedStateTreeInput<States>): never;
385
+ }
386
+ type EventProtocolError<Message extends string, Tag extends PropertyKey = never> = {
387
+ readonly "~effect/Machine/EventProtocolError": Message;
388
+ readonly tag: Tag;
389
+ };
390
+ type DuplicateEventTag<Events extends ReadonlyArray<Machine.TaggedSchema>, Seen extends PropertyKey = never> = number extends Events["length"] ? never : Events extends readonly [
391
+ infer Head extends Machine.TaggedSchema,
392
+ ...infer Tail extends ReadonlyArray<Machine.TaggedSchema>
393
+ ] ? Machine.TagOf<Head> extends infer Tag extends PropertyKey ? Tag extends Seen ? Tag | DuplicateEventTag<Tail, Seen> : DuplicateEventTag<Tail, Seen | Tag> : never : never;
394
+ type ValidateInputEventProtocol<InputEvents extends ReadonlyArray<Machine.TaggedSchema>, DuplicateInput extends PropertyKey = DuplicateEventTag<InputEvents>> = [DuplicateInput] extends [never] ? unknown : EventProtocolError<"Public event tags must be unique", DuplicateInput>;
395
+ type ValidateInternalEventProtocol<InputEvents extends ReadonlyArray<Machine.TaggedSchema>, InternalEvents extends ReadonlyArray<Machine.TaggedSchema>, DuplicateInternal extends PropertyKey = DuplicateEventTag<InternalEvents>, Overlap extends PropertyKey = Extract<Machine.TagOf<InputEvents[number]>, Machine.TagOf<InternalEvents[number]>>> = [DuplicateInternal] extends [never] ? [Overlap] extends [never] ? unknown : EventProtocolError<"Public and internal event tags must be disjoint", Overlap> : EventProtocolError<"Internal event tags must be unique", DuplicateInternal>;
324
396
  declare const SnapshotBuilderStateTypeId: unique symbol;
325
- type SnapshotBuilderComplete<Regions> = {
397
+ declare const SnapshotBuilderConstructionTypeId: unique symbol;
398
+ type SnapshotBuilderComplete<Regions, Constructed extends boolean = false> = {
326
399
  readonly [SnapshotBuilderStateTypeId]: Regions;
400
+ readonly [SnapshotBuilderConstructionTypeId]: Constructed;
327
401
  };
402
+ type FromCallable<Arguments extends ReadonlyArray<unknown>, Result> = Arguments extends readonly [infer Input, ...infer Rest extends ReadonlyArray<unknown>] ? {} extends Input ? {
403
+ (...args: Rest): Result;
404
+ (...args: Arguments): Result;
405
+ } : (...args: Arguments) => Result : (...args: Arguments) => Result;
406
+ type FromMethod<Arguments extends ReadonlyArray<unknown>, Result> = {
407
+ /**
408
+ * Constructs the selected state from its schema make input while the
409
+ * machine plans the resulting configuration. The input may be omitted when
410
+ * the schema accepts an empty constructor object.
411
+ *
412
+ * @since 4.0.0
413
+ */
414
+ readonly from: FromCallable<Arguments, Machine.StateConstruction<Result>>;
415
+ };
416
+ type ConstructionResult<Result> = Result | Machine.StateConstruction<Result>;
417
+ type UnwrapConstruction<Result> = Result extends Machine.StateConstruction<infer Value> ? Value : Result;
418
+ type ConstructionSelectorFromCallable<Input, Builder, Result> = {} extends Input ? {
419
+ <Selected extends ConstructionResult<Result>>(state: (builder: Builder) => Selected): Machine.StateConstruction<UnwrapConstruction<Selected>>;
420
+ <Selected extends ConstructionResult<Result>>(input: Input, state: (builder: Builder) => Selected): Machine.StateConstruction<UnwrapConstruction<Selected>>;
421
+ } : <Selected extends ConstructionResult<Result>>(input: Input, state: (builder: Builder) => Selected) => Machine.StateConstruction<UnwrapConstruction<Selected>>;
328
422
  type InitialSnapshotBuilderWithPrefix<States extends Machine.StateSchemas, Prefix extends string = ""> = {
329
- readonly [Key in Extract<keyof States, string>]: InitialSnapshotMethod<States, Key, Prefix>;
423
+ readonly [Key in ActiveStateKey<States>]: InitialSnapshotMethod<States, Key, Prefix>;
330
424
  };
331
- type InitialSnapshotMethod<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string> = (...args: InitialSnapshotArguments<States, StateId, Prefix>) => InitialSnapshotResult<States, StateId, Prefix>;
332
- type InitialSnapshotArguments<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
425
+ type InitialSnapshotMethod<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string> = ((...args: InitialSnapshotArguments<States, StateId, Prefix>) => InitialSnapshotResult<States, StateId, Prefix>) & FromMethod<InitialSnapshotFromArguments<States, StateId, Prefix>, InitialSnapshotResult<States, StateId, Prefix>>;
426
+ type InitialSnapshotArguments<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
333
427
  readonly type: "parallel";
334
428
  readonly states: infer Children extends Machine.StateSchemas;
335
429
  } ? [
@@ -338,32 +432,50 @@ type InitialSnapshotArguments<States extends Machine.StateSchemas, StateId exten
338
432
  ] : Node extends {
339
433
  readonly states: infer Children extends Machine.StateSchemas;
340
434
  } ? Node extends {
341
- readonly initial: infer Initial extends Extract<keyof Children, string>;
435
+ readonly initial: infer Initial extends ActiveStateKey<Children>;
342
436
  } ? [
343
437
  value: Machine.NodeSchema<Node>["Type"],
344
438
  state: (builder: Pick<InitialSnapshotBuilderWithPrefix<Children, Path>, Initial>) => InitialSnapshotResult<Children, Initial, Path>
345
439
  ] : never : [value: Machine.NodeSchema<Node>["Type"]] : never;
346
- type InitialSnapshotResult<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
440
+ type InitialSnapshotFromArguments<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
441
+ readonly type: "parallel";
442
+ readonly states: infer Children extends Machine.StateSchemas;
443
+ } ? [
444
+ input: Machine.NodeSchema<Node>["~type.make.in"],
445
+ states: (builder: InitialParallelBuilder<Children, Path>) => SnapshotBuilderComplete<InitialSnapshotRegionsWithPrefix<Children, Path>, boolean>
446
+ ] : Node extends {
447
+ readonly states: infer Children extends Machine.StateSchemas;
448
+ } ? Node extends {
449
+ readonly initial: infer Initial extends ActiveStateKey<Children>;
450
+ } ? [
451
+ input: Machine.NodeSchema<Node>["~type.make.in"],
452
+ state: (builder: Pick<InitialSnapshotBuilderWithPrefix<Children, Path>, Initial>) => ConstructionResult<InitialSnapshotResult<Children, Initial, Path>>
453
+ ] : never : [input: Machine.NodeSchema<Node>["~type.make.in"]] : never;
454
+ type InitialSnapshotResult<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
347
455
  readonly type: "parallel";
348
456
  readonly states: infer Children extends Machine.StateSchemas;
349
457
  } ? Machine.ParallelSnapshot<Path, Machine.NodeSchema<Node>["Type"], InitialSnapshotRegionsWithPrefix<Children, Path>> : Node extends {
350
458
  readonly states: infer Children extends Machine.StateSchemas;
351
459
  } ? Node extends {
352
- readonly initial: infer Initial extends Extract<keyof Children, string>;
460
+ readonly initial: infer Initial extends ActiveStateKey<Children>;
353
461
  } ? Machine.CompoundSnapshot<Path, Machine.NodeSchema<Node>["Type"], InitialSnapshotResult<Children, Initial, Path>> : never : Machine.AtomicSnapshot<Path, Machine.NodeSchema<Node>["Type"]> : never;
354
462
  type InitialSnapshotRegionsWithPrefix<States extends Machine.StateSchemas, Prefix extends string> = {
355
- readonly [Key in Extract<keyof States, string>]: InitialSnapshotResult<States, Key, Prefix>;
463
+ readonly [Key in ActiveStateKey<States>]: InitialSnapshotResult<States, Key, Prefix>;
356
464
  };
357
- type InitialParallelBuilder<States extends Machine.StateSchemas, Prefix extends string, Remaining extends Extract<keyof States, string> = Extract<keyof States, string>, Regions = {}> = SnapshotBuilderComplete<Regions> & {
358
- readonly [Key in Remaining]: (...args: InitialSnapshotArguments<States, Key, Prefix>) => InitialParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
465
+ type InitialParallelBuilder<States extends Machine.StateSchemas, Prefix extends string, Remaining extends ActiveStateKey<States> = ActiveStateKey<States>, Regions = {}, Constructed extends boolean = false> = SnapshotBuilderComplete<Regions, Constructed> & {
466
+ readonly [Key in Remaining]: ((...args: InitialSnapshotArguments<States, Key, Prefix>) => InitialParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
359
467
  readonly [Region in Key]: InitialSnapshotResult<States, Key, Prefix>;
360
- }>;
468
+ }, Constructed>) & {
469
+ readonly from: FromCallable<InitialSnapshotFromArguments<States, Key, Prefix>, InitialParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
470
+ readonly [Region in Key]: InitialSnapshotResult<States, Key, Prefix>;
471
+ }, true>>;
472
+ };
361
473
  };
362
474
  type FullSnapshotBuilderWithPrefix<States extends Machine.StateSchemas, Prefix extends string = ""> = {
363
- readonly [Key in Extract<keyof States, string>]: FullSnapshotMethod<States, Key, Prefix>;
475
+ readonly [Key in ActiveStateKey<States>]: FullSnapshotMethod<States, Key, Prefix>;
364
476
  };
365
- type FullSnapshotMethod<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string> = (...args: FullSnapshotArguments<States, StateId, Prefix>) => FullSnapshotResult<States, StateId, Prefix>;
366
- type FullSnapshotArguments<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
477
+ type FullSnapshotMethod<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string> = ((...args: FullSnapshotArguments<States, StateId, Prefix>) => FullSnapshotResult<States, StateId, Prefix>) & FromMethod<FullSnapshotFromArguments<States, StateId, Prefix>, FullSnapshotResult<States, StateId, Prefix>>;
478
+ type FullSnapshotArguments<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
367
479
  readonly type: "parallel";
368
480
  readonly states: infer Children extends Machine.StateSchemas;
369
481
  } ? [
@@ -375,11 +487,27 @@ type FullSnapshotArguments<States extends Machine.StateSchemas, StateId extends
375
487
  value: Machine.NodeSchema<Node>["Type"],
376
488
  state: (builder: FullSnapshotBuilderWithPrefix<Children, Path>) => Machine.SnapshotWithPrefix<Children, Path>
377
489
  ] : [value: Machine.NodeSchema<Node>["Type"]] : never;
378
- type FullSnapshotResult<States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = Machine.SnapshotByIdentifierWithPath<States, StateId, Path>;
379
- type FullParallelBuilder<States extends Machine.StateSchemas, Prefix extends string, Remaining extends Extract<keyof States, string> = Extract<keyof States, string>, Regions = {}> = SnapshotBuilderComplete<Regions> & {
380
- readonly [Key in Remaining]: (...args: FullSnapshotArguments<States, Key, Prefix>) => FullParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
490
+ type FullSnapshotFromArguments<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
491
+ readonly type: "parallel";
492
+ readonly states: infer Children extends Machine.StateSchemas;
493
+ } ? [
494
+ input: Machine.NodeSchema<Node>["~type.make.in"],
495
+ states: (builder: FullParallelBuilder<Children, Path>) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
496
+ ] : Node extends {
497
+ readonly states: infer Children extends Machine.StateSchemas;
498
+ } ? [
499
+ input: Machine.NodeSchema<Node>["~type.make.in"],
500
+ state: (builder: FullSnapshotBuilderWithPrefix<Children, Path>) => ConstructionResult<Machine.SnapshotWithPrefix<Children, Path>>
501
+ ] : [input: Machine.NodeSchema<Node>["~type.make.in"]] : never;
502
+ type FullSnapshotResult<States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = Machine.SnapshotByIdentifierWithPath<States, StateId, Path>;
503
+ type FullParallelBuilder<States extends Machine.StateSchemas, Prefix extends string, Remaining extends ActiveStateKey<States> = ActiveStateKey<States>, Regions = {}, Constructed extends boolean = false> = SnapshotBuilderComplete<Regions, Constructed> & {
504
+ readonly [Key in Remaining]: ((...args: FullSnapshotArguments<States, Key, Prefix>) => FullParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
381
505
  readonly [Region in Key]: FullSnapshotResult<States, Key, Prefix>;
382
- }>;
506
+ }, Constructed>) & {
507
+ readonly from: FromCallable<FullSnapshotFromArguments<States, Key, Prefix>, FullParallelBuilder<States, Prefix, Exclude<Remaining, Key>, Regions & {
508
+ readonly [Region in Key]: FullSnapshotResult<States, Key, Prefix>;
509
+ }, true>>;
510
+ };
383
511
  };
384
512
  type ParentPath<Path extends string> = Path extends `${infer Parent}.${infer Child}` ? Child extends `${string}.${string}` ? `${Parent}.${ParentPath<Child>}` : Parent : never;
385
513
  type IsCompoundNode<Node> = Node extends {
@@ -392,48 +520,110 @@ type ChildrenOf<States extends Machine.StateSchemas, Path extends Machine.StateI
392
520
  readonly states: infer Children extends Machine.StateSchemas;
393
521
  } ? Children : never;
394
522
  type StateIdentifierFromPath<States extends Machine.StateSchemas, Path extends string> = Extract<Path, Machine.StateIdentifier<States>>;
395
- type LocalTargetResult<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends {
523
+ type LocalTargetResult<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends {
396
524
  readonly states: infer Children extends Machine.StateSchemas;
397
- } ? LocalTargetResultWithPrefix<AllStates, Children, Path> : Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>;
525
+ } ? States[StateId] extends {
526
+ readonly type: "parallel";
527
+ } ? Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>> : LocalTargetResultWithPrefix<AllStates, Children, Path> : Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>;
398
528
  type LocalTargetResultWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
399
- readonly [Key in Extract<keyof States, string>]: LocalTargetResult<AllStates, States, Key, Prefix>;
400
- }[Extract<keyof States, string>];
401
- type LocalTargetBuilderWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
402
- readonly [Key in Extract<keyof States, string>]: LocalTargetMethod<AllStates, States, Key, Prefix>;
529
+ readonly [Key in ActiveStateKey<States>]: LocalTargetResult<AllStates, States, Key, Prefix>;
530
+ }[ActiveStateKey<States>];
531
+ type LocalTargetBuilderWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string, Source extends Machine.StateIdentifier<AllStates>> = {
532
+ readonly [Key in ActiveStateKey<States>]: LocalTargetMethod<AllStates, States, Key, Prefix, Source>;
403
533
  };
404
- type LocalTargetMethod<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
534
+ type LocalTargetMethod<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Source extends Machine.StateIdentifier<AllStates>, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
535
+ readonly type: "parallel";
405
536
  readonly states: infer Children extends Machine.StateSchemas;
406
- } ? <Result extends LocalTargetResultWithPrefix<AllStates, Children, Path>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: LocalTargetBuilderWithPrefix<AllStates, Children, Path>) => Result) => Result : (value: Machine.NodeSchema<Node>["Type"]) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>> : never;
407
- type LocalTargetBuilderForScope<States extends Machine.StateSchemas, Scope extends Machine.StateIdentifier<States>> = ChildrenOf<States, Scope> extends infer Children extends Machine.StateSchemas ? LocalTargetBuilderWithPrefix<States, Children, Scope> & {
537
+ } ? Source extends Path | `${Path}.${string}` ? (<Result extends ConstructionResult<LocalTargetResultWithPrefix<AllStates, Children, Path>>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>) => Result) => Result) & {
538
+ readonly from: ConstructionSelectorFromCallable<Machine.NodeSchema<Node>["~type.make.in"], LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>, LocalTargetResultWithPrefix<AllStates, Children, Path>>;
539
+ } : ((value: Machine.NodeSchema<Node>["Type"], states: (builder: FullParallelBuilder<Children, Path>) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>>) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>) & FromMethod<[
540
+ input: Machine.NodeSchema<Node>["~type.make.in"],
541
+ states: (builder: FullParallelBuilder<Children, Path>) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
542
+ ], Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>> : Node extends {
543
+ readonly states: infer Children extends Machine.StateSchemas;
544
+ } ? (<Result extends ConstructionResult<LocalTargetResultWithPrefix<AllStates, Children, Path>>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>) => Result) => Result) & {
545
+ readonly from: ConstructionSelectorFromCallable<Machine.NodeSchema<Node>["~type.make.in"], LocalTargetBuilderWithPrefix<AllStates, Children, Path, Source>, LocalTargetResultWithPrefix<AllStates, Children, Path>>;
546
+ } : ((value: Machine.NodeSchema<Node>["Type"]) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>) & FromMethod<[
547
+ input: Machine.NodeSchema<Node>["~type.make.in"]
548
+ ], Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>> : never;
549
+ type LocalTargetBuilderForScope<States extends Machine.StateSchemas, Scope extends Machine.StateIdentifier<States>, Source extends Machine.StateIdentifier<States>> = ChildrenOf<States, Scope> extends infer Children extends Machine.StateSchemas ? LocalTargetBuilderWithPrefix<States, Children, Scope, Source> & {
408
550
  /**
409
551
  * Updates the value of the state containing the local group and moves to
410
552
  * one of the states inside it. Values in other active branches are kept.
411
553
  *
412
554
  * @since 4.0.0
413
555
  */
414
- readonly with: <Result extends LocalTargetResultWithPrefix<States, Children, Scope>>(value: Machine.StateByIdentifier<States, Scope>, state: (builder: LocalTargetBuilderWithPrefix<States, Children, Scope>) => Result) => Result;
556
+ readonly with: (<Result extends ConstructionResult<LocalTargetResultWithPrefix<States, Children, Scope>>>(value: Machine.StateByIdentifier<States, Scope>, state: (builder: LocalTargetBuilderWithPrefix<States, Children, Scope, Source>) => Result) => Result) & {
557
+ readonly from: ConstructionSelectorFromCallable<Machine.SchemaByIdentifier<States, Scope>["~type.make.in"], LocalTargetBuilderWithPrefix<States, Children, Scope, Source>, LocalTargetResultWithPrefix<States, Children, Scope>>;
558
+ };
415
559
  } : {};
416
- type BranchTargetResult<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends {
560
+ type BranchTargetResult<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends {
417
561
  readonly states: infer Children extends Machine.StateSchemas;
418
- } ? BranchTargetResultWithPrefix<AllStates, Children, Path> : Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>;
562
+ } ? States[StateId] extends {
563
+ readonly type: "parallel";
564
+ } ? Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>> : BranchTargetResultWithPrefix<AllStates, Children, Path> : Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>;
419
565
  type BranchTargetResultWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
420
- readonly [Key in Extract<keyof States, string>]: BranchTargetResult<AllStates, States, Key, Prefix>;
421
- }[Extract<keyof States, string>];
422
- type BranchTargetBuilderWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
423
- readonly [Key in Extract<keyof States, string>]: BranchTargetMethod<AllStates, States, Key, Prefix>;
566
+ readonly [Key in ActiveStateKey<States>]: BranchTargetResult<AllStates, States, Key, Prefix>;
567
+ }[ActiveStateKey<States>];
568
+ type BranchTargetBuilderWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string, Source extends Machine.StateIdentifier<AllStates>> = {
569
+ readonly [Key in ActiveStateKey<States>]: BranchTargetMethod<AllStates, States, Key, Prefix, Source>;
424
570
  };
425
- type BranchTargetMethod<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends Extract<keyof States, string>, Prefix extends string, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
571
+ type BranchTargetMethod<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, StateId extends ActiveStateKey<States>, Prefix extends string, Source extends Machine.StateIdentifier<AllStates>, Path extends string = Machine.JoinPath<Prefix, StateId>> = States[StateId] extends infer Node ? Node extends {
572
+ readonly type: "parallel";
573
+ readonly states: infer Children extends Machine.StateSchemas;
574
+ } ? Source extends Path | `${Path}.${string}` ? (<Result extends ConstructionResult<BranchTargetResultWithPrefix<AllStates, Children, Path>>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>) => Result) => Result) & {
575
+ readonly from: ConstructionSelectorFromCallable<Machine.NodeSchema<Node>["~type.make.in"], BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>, BranchTargetResultWithPrefix<AllStates, Children, Path>>;
576
+ } & BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source> : ((value: Machine.NodeSchema<Node>["Type"], states: (builder: FullParallelBuilder<Children, Path>) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>>) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>) & FromMethod<[
577
+ input: Machine.NodeSchema<Node>["~type.make.in"],
578
+ states: (builder: FullParallelBuilder<Children, Path>) => SnapshotBuilderComplete<Machine.SnapshotRegionsWithPrefix<Children, Path>, boolean>
579
+ ], Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>> : Node extends {
426
580
  readonly states: infer Children extends Machine.StateSchemas;
427
- } ? (<Result extends BranchTargetResultWithPrefix<AllStates, Children, Path>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: BranchTargetBuilderWithPrefix<AllStates, Children, Path>) => Result) => Result) & BranchTargetBuilderWithPrefix<AllStates, Children, Path> : (value: Machine.NodeSchema<Node>["Type"]) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>> : never;
428
- type BranchTargetBuilderForRoot<States extends Machine.StateSchemas, Root extends Extract<keyof States, string>> = {
429
- readonly [Key in Root]: BranchTargetMethod<States, States, Key, "">;
581
+ } ? (<Result extends ConstructionResult<BranchTargetResultWithPrefix<AllStates, Children, Path>>>(value: Machine.NodeSchema<Node>["Type"], state: (builder: BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>) => Result) => Result) & {
582
+ readonly from: ConstructionSelectorFromCallable<Machine.NodeSchema<Node>["~type.make.in"], BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source>, BranchTargetResultWithPrefix<AllStates, Children, Path>>;
583
+ } & BranchTargetBuilderWithPrefix<AllStates, Children, Path, Source> : ((value: Machine.NodeSchema<Node>["Type"]) => Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>) & FromMethod<[
584
+ input: Machine.NodeSchema<Node>["~type.make.in"]
585
+ ], Machine.Target<AllStates, StateIdentifierFromPath<AllStates, Path>>> : never;
586
+ type BranchTargetBuilderForRoot<States extends Machine.StateSchemas, Root extends ActiveStateKey<States>, Source extends Machine.StateIdentifier<States>> = {
587
+ readonly [Key in Root]: BranchTargetMethod<States, States, Key, "", Source>;
430
588
  };
589
+ type HistoryContainingKey<States extends Machine.StateSchemas> = {
590
+ readonly [Key in Extract<keyof States, string>]: States[Key] extends Machine.HistoryStateNodeConfig ? Key : States[Key] extends {
591
+ readonly states: infer Children extends Machine.StateSchemas;
592
+ } ? [
593
+ Machine.HistoryIdentifier<Children>
594
+ ] extends [never] ? never : Key : never;
595
+ }[Extract<keyof States, string>];
596
+ type HistoryTargetBuilderWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
597
+ readonly [Key in HistoryContainingKey<States>]: States[Key] extends Machine.HistoryStateNodeConfig ? () => Machine.HistoryTarget<AllStates, Extract<Machine.JoinPath<Prefix, Key>, Machine.HistoryIdentifier<AllStates>>> : States[Key] extends {
598
+ readonly states: infer Children extends Machine.StateSchemas;
599
+ } ? HistoryTargetBuilderWithPrefix<AllStates, Children, Machine.JoinPath<Prefix, Key>> : never;
600
+ };
601
+ type HasDirectShallowHistory<States extends Machine.StateSchemas> = {
602
+ readonly [Key in HistoryStateKey<States>]: States[Key] extends {
603
+ readonly history: "deep";
604
+ } ? never : Key;
605
+ }[HistoryStateKey<States>] extends never ? false : true;
606
+ type InitializerClosureForNode<AllStates extends Machine.StateSchemas, Node, Path extends string> = Node extends {
607
+ readonly type: "parallel";
608
+ readonly states: infer Children extends Machine.StateSchemas;
609
+ } ? Extract<Path, Machine.StateIdentifier<AllStates>> | InitializerClosuresForChildren<AllStates, Children, Path> : Node extends {
610
+ readonly states: infer Children extends Machine.StateSchemas;
611
+ readonly initial: infer Initial;
612
+ } ? Extract<Path, Machine.StateIdentifier<AllStates>> | (Initial extends ActiveStateKey<Children> ? InitializerClosureForNode<AllStates, Children[Initial], Machine.JoinPath<Path, Initial>> : never) : never;
613
+ type InitializerClosuresForChildren<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
614
+ readonly [Key in ActiveStateKey<States>]: InitializerClosureForNode<AllStates, States[Key], Machine.JoinPath<Prefix, Key>>;
615
+ }[ActiveStateKey<States>];
616
+ type RequiredHistoryInitializersWithPrefix<AllStates extends Machine.StateSchemas, States extends Machine.StateSchemas, Prefix extends string> = {
617
+ readonly [Key in ActiveStateKey<States>]: States[Key] extends {
618
+ readonly states: infer Children extends Machine.StateSchemas;
619
+ } ? (HasDirectShallowHistory<Children> extends true ? InitializerClosuresForChildren<AllStates, Children, Machine.JoinPath<Prefix, Key>> : never) | RequiredHistoryInitializersWithPrefix<AllStates, Children, Machine.JoinPath<Prefix, Key>> : never;
620
+ }[ActiveStateKey<States>];
431
621
  type SpawnRequirements<Requirements> = Exclude<Requirements, Scope.Scope>;
432
622
  type SpawnIdError<Options extends SpawnOptions> = "id" extends keyof Options ? Options extends {
433
623
  readonly id?: infer Id;
434
624
  } ? [Id] extends [undefined] ? never : ChildAlreadyExistsError : ChildAlreadyExistsError : never;
435
625
  type SpawnError<Options extends SpawnOptions> = SpawnIdError<Options>;
436
- type SpawnResult<State, Event, Error, Requirements, Output, SpawnError, InitialError = never> = Effect.Effect<MachineRef<State, Event, Error | InitialError, Output>, SpawnError | InitialError, MachineRuntimeRequirement | SpawnRequirements<Requirements>>;
626
+ type SpawnResult<State, Event, Error, Requirements, Output, SpawnError, InitialError = never> = Effect.Effect<MachineRef<State, Event, Error, Output>, SpawnError | InitialError, MachineRuntimeRequirement | SpawnRequirements<Requirements>>;
437
627
  /**
438
628
  * Represents the active or terminal lifecycle state of a running machine.
439
629
  *
@@ -520,17 +710,33 @@ export type RuntimeOutcome<State, Error = never, Output = never> = {
520
710
  * @since 4.0.0
521
711
  */
522
712
  export interface MachineRef<out State, in Event, out Error = never, out Output = never> {
713
+ /** Stable machine definition id, or a generated fallback when none was declared. */
523
714
  readonly id: string;
715
+ /** Unique identity for this running machine instance. */
524
716
  readonly sessionId: string;
717
+ /** Reads the latest logical state. */
525
718
  readonly state: Effect.Effect<State>;
719
+ /** Reads the latest lifecycle snapshot. */
526
720
  readonly snapshot: Effect.Effect<RuntimeSnapshot<State, Error, Output>>;
721
+ /** Streams lifecycle snapshots published after subscription. */
527
722
  readonly changes: Stream.Stream<RuntimeSnapshot<State, Error, Output>>;
723
+ /** Waits for machine output or fails when execution fails or is stopped. */
528
724
  readonly join: Effect.Effect<Output, Error | StoppedError>;
725
+ /** Stops this machine instance and its owned child processes. */
529
726
  readonly stop: Effect.Effect<void>;
727
+ /** Accepts an event for asynchronous processing by the running machine. */
530
728
  readonly send: (event: Event) => Effect.Effect<void, StoppedError>;
531
- /** Returns the current directly owned child for a typed descriptor. */
729
+ /**
730
+ * Returns the current directly owned child for a typed descriptor.
731
+ *
732
+ * @since 4.0.0
733
+ */
532
734
  readonly child: <Child extends ChildMachine.Any>(child: Child) => Effect.Effect<Option.Option<ChildMachine.Ref<Child>>>;
533
- /** Streams activation, replacement, and removal of a directly owned child. */
735
+ /**
736
+ * Streams activation, replacement, and removal of a directly owned child.
737
+ *
738
+ * @since 4.0.0
739
+ */
534
740
  readonly childChanges: <Child extends ChildMachine.Any>(child: Child) => Stream.Stream<Option.Option<ChildMachine.Ref<Child>>>;
535
741
  }
536
742
  /**
@@ -540,7 +746,9 @@ export interface MachineRef<out State, in Event, out Error = never, out Output =
540
746
  * @since 4.0.0
541
747
  */
542
748
  export interface Logic<State, Event, out Error = never, out Requirements = never, out Output = never, out InitialError = never> {
749
+ /** Creates the initial process state and may spawn scoped child processes. */
543
750
  initial(scope: Logic.Scope<Event>): Effect.Effect<State, InitialError, Requirements>;
751
+ /** Runs the stateful process loop until it produces output or fails. */
544
752
  run(context: Logic.Context<State, Event>): Effect.Effect<Output, Error, Requirements>;
545
753
  }
546
754
  /**
@@ -556,9 +764,13 @@ export declare namespace Logic {
556
764
  * @since 4.0.0
557
765
  */
558
766
  interface Address<in Event> {
767
+ /** Parent-local address id. */
559
768
  readonly id: string;
769
+ /** Unique identity for this running child instance. */
560
770
  readonly sessionId: string;
771
+ /** Stops the addressed child process. */
561
772
  readonly stop: Effect.Effect<void>;
773
+ /** Sends an event to the addressed child process. */
562
774
  readonly send: (event: Event) => Effect.Effect<void, StoppedError>;
563
775
  }
564
776
  /**
@@ -568,10 +780,8 @@ export declare namespace Logic {
568
780
  * @since 4.0.0
569
781
  */
570
782
  interface Spawn {
571
- <ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError = never>(logic: Logic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>): Effect.Effect<MachineRef<ChildState, ChildEvent, ChildError | ChildInitialError, ChildOutput>, ChildInitialError, Exclude<ChildRequirements, Scope.Scope>>;
572
- <ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError = never>(logic: Logic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>, options: {
573
- readonly id: string;
574
- }): Effect.Effect<MachineRef<ChildState, ChildEvent, ChildError | ChildInitialError, ChildOutput>, ChildAlreadyExistsError | ChildInitialError, Exclude<ChildRequirements, Scope.Scope>>;
783
+ <ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError = never>(logic: Logic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>): Effect.Effect<MachineRef<ChildState, ChildEvent, ChildError, ChildOutput>, ChildInitialError, Exclude<ChildRequirements, Scope.Scope>>;
784
+ <ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, Options extends SpawnOptions, ChildInitialError = never>(logic: Logic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>, options: Options & ChildAddress.OptionsCompatibility<Options, ChildEvent>): Effect.Effect<MachineRef<ChildState, ChildEvent, ChildError, ChildOutput>, SpawnIdError<Options> | ChildInitialError, Exclude<ChildRequirements, Scope.Scope>>;
575
785
  }
576
786
  /**
577
787
  * Machine-local capabilities available while process logic initializes.
@@ -586,12 +796,18 @@ export declare namespace Logic {
586
796
  * @since 4.0.0
587
797
  */
588
798
  interface Scope<Event> {
799
+ /** Address of the process being initialized. */
589
800
  readonly self: Address<Event>;
801
+ /** Address of the owning process, when one exists. */
590
802
  readonly parent: Address<unknown> | undefined;
803
+ /** Starts a child process owned by this scope. */
591
804
  readonly spawn: Spawn;
805
+ /** Sends an untyped event to the owning process. */
592
806
  readonly sendParent: (event: unknown) => Effect.Effect<void, StoppedError>;
593
- readonly sendTo: <Address extends string>(id: Address, event: ChildAddress.Event<Address>) => Effect.Effect<void, StoppedError>;
594
- readonly stopChild: (id: string) => Effect.Effect<void>;
807
+ /** Sends an event through a typed parent-local child address. */
808
+ readonly sendTo: <Address extends ChildAddress<never>>(id: Address, event: ChildAddress.Event<Address>) => Effect.Effect<void, StoppedError>;
809
+ /** Stops a child process selected by its parent-local address. */
810
+ readonly stopChild: <Event>(id: ChildAddress<Event>) => Effect.Effect<void>;
595
811
  }
596
812
  /**
597
813
  * Machine-local capabilities available while stateful process logic runs.
@@ -600,15 +816,22 @@ export declare namespace Logic {
600
816
  * @since 4.0.0
601
817
  */
602
818
  interface Context<State, Event> extends Scope<Event> {
819
+ /** Waits for the next event delivered to this process. */
603
820
  readonly receive: Effect.Effect<Event>;
821
+ /** Reads the current process state. */
604
822
  readonly state: Effect.Effect<State>;
823
+ /** Replaces the current process state. */
605
824
  readonly setState: (state: State) => Effect.Effect<void>;
825
+ /** Updates the current process state effectfully and atomically. */
606
826
  readonly updateState: <E, R>(update: (state: State) => Effect.Effect<State, E, R>) => Effect.Effect<void, E, R>;
607
827
  }
608
828
  }
609
829
  declare const ChildAddressTypeId = "~effect/Machine/ChildAddress";
610
830
  declare const ChildAddressCompatibilityErrorTypeId = "~effect/Machine/ChildAddressCompatibilityError";
611
831
  declare const ChildMachineTypeId = "~effect/Machine/ChildMachine";
832
+ type InvokeLifecycleId = string & {
833
+ readonly [ChildAddressTypeId]?: never;
834
+ };
612
835
  /**
613
836
  * Typed descriptor for a complete machine invoked as a child.
614
837
  *
@@ -623,7 +846,9 @@ declare const ChildMachineTypeId = "~effect/Machine/ChildMachine";
623
846
  */
624
847
  export interface ChildMachine<Id extends string, M extends Machine.Any> {
625
848
  readonly [ChildMachineTypeId]: typeof ChildMachineTypeId;
849
+ /** Parent-local id used to address the invoked child. */
626
850
  readonly id: Id;
851
+ /** Complete machine definition carried by this descriptor. */
627
852
  readonly machine: M;
628
853
  }
629
854
  /**
@@ -645,7 +870,7 @@ export declare namespace ChildMachine {
645
870
  * @category utility types
646
871
  * @since 4.0.0
647
872
  */
648
- type Ref<Child> = Child extends ChildMachine<string, infer M> ? M extends Machine<infer States, infer Events, any, any, infer E, infer R, infer InitialE, infer InitialR, any, infer Output, any, any> ? MachineRef<Machine.Snapshot<States>, Machine.EventOf<Events>, E | InitialE | ActionError<R | InitialR> | InfiniteTransitionError | MachineSchemaDecodeError | StartupError | StoppedError, Output | undefined> : never : never;
873
+ type Ref<Child> = Child extends ChildMachine<string, infer M> ? MachineRef<Machine.Snapshot<Machine.States<M>>, Machine.InputEvent<M>, Machine.Error<M> | ActionError<Machine.Services<M>> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, Machine.Output<M>> : never;
649
874
  /**
650
875
  * Event accepted by the child selected by a descriptor.
651
876
  *
@@ -735,13 +960,170 @@ export interface SpawnIdOptions extends SpawnOptions {
735
960
  * @since 4.0.0
736
961
  */
737
962
  export declare namespace Machine {
963
+ /**
964
+ * Stable type-level representation carried by every machine definition.
965
+ *
966
+ * @internal
967
+ */
968
+ interface TypeCarrier<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Input extends Schema.Top, UnhandledStates extends StateIdentifier<States>, E, R, InitialE, InitialR, FinalStates extends StateIdentifier<States>, Output, Emits extends ReadonlyArray<TaggedSchema>, OutputStates extends StateIdentifier<States>, InputEvents extends ReadonlyArray<TaggedSchema>> {
969
+ readonly states: States;
970
+ readonly events: Events;
971
+ readonly input: Input;
972
+ readonly unhandledStates: UnhandledStates;
973
+ readonly error: E;
974
+ readonly services: R;
975
+ readonly initialError: InitialE;
976
+ readonly initialServices: InitialR;
977
+ readonly finalStates: FinalStates;
978
+ readonly output: Output;
979
+ readonly emits: Emits;
980
+ readonly outputStates: OutputStates;
981
+ readonly inputEvents: InputEvents;
982
+ }
738
983
  /**
739
984
  * Any schema-first machine.
740
985
  *
986
+ * This is an erased structural view for APIs that store machines without
987
+ * knowing their protocols. Generic APIs should capture `M extends Any` to
988
+ * preserve the concrete machine type. The private output-implementation
989
+ * proof is intentionally omitted so erasing a machine cannot manufacture
990
+ * that proof on another concrete `Machine` type.
991
+ *
741
992
  * @category models
742
993
  * @since 4.0.0
743
994
  */
744
- type Any = Machine<any, any, any, any, any, any, any, any, any, any, any, any>;
995
+ interface Any extends Pipeable {
996
+ readonly [TypeId]: TypeId;
997
+ /** @internal */
998
+ readonly [MachineTypeId]: TypeCarrier<any, any, any, any, any, any, any, any, any, any, any, any, any>;
999
+ readonly states: StateSchemas;
1000
+ readonly events: ReadonlyArray<TaggedSchema>;
1001
+ readonly internalEvents: ReadonlyArray<TaggedSchema>;
1002
+ readonly emits: ReadonlyArray<TaggedSchema>;
1003
+ readonly input: Schema.Top | undefined;
1004
+ readonly id: string | undefined;
1005
+ readonly stateNodes: StateNodes;
1006
+ /** @internal */
1007
+ readonly makeTargetBuilder: any;
1008
+ /** @internal */
1009
+ readonly handlers: any;
1010
+ /** @internal */
1011
+ readonly handle: any;
1012
+ /** @internal */
1013
+ readonly initial: any;
1014
+ }
1015
+ /**
1016
+ * Extracts the state schema tree carried by a machine definition.
1017
+ *
1018
+ * @category utility types
1019
+ * @since 4.0.0
1020
+ */
1021
+ type States<M extends Any> = M[typeof MachineTypeId]["states"];
1022
+ /**
1023
+ * Extracts the complete event schema tuple carried by a machine definition.
1024
+ *
1025
+ * @category utility types
1026
+ * @since 4.0.0
1027
+ */
1028
+ type Events<M extends Any> = M[typeof MachineTypeId]["events"];
1029
+ /**
1030
+ * Extracts the input schema carried by a machine definition.
1031
+ *
1032
+ * @category utility types
1033
+ * @since 4.0.0
1034
+ */
1035
+ type Input<M extends Any> = M[typeof MachineTypeId]["input"];
1036
+ /**
1037
+ * Extracts state paths that do not yet have handlers.
1038
+ *
1039
+ * @category utility types
1040
+ * @since 4.0.0
1041
+ */
1042
+ type UnhandledStates<M extends Any> = M[typeof MachineTypeId]["unhandledStates"];
1043
+ /**
1044
+ * Extracts the runtime error channel carried by a machine definition.
1045
+ *
1046
+ * @category utility types
1047
+ * @since 4.0.0
1048
+ */
1049
+ type Error<M extends Any> = M[typeof MachineTypeId]["error"];
1050
+ /**
1051
+ * Extracts runtime service requirements carried by a machine definition.
1052
+ *
1053
+ * @category utility types
1054
+ * @since 4.0.0
1055
+ */
1056
+ type Services<M extends Any> = M[typeof MachineTypeId]["services"];
1057
+ /**
1058
+ * Extracts the startup error channel carried by a machine definition.
1059
+ *
1060
+ * @category utility types
1061
+ * @since 4.0.0
1062
+ */
1063
+ type InitialError<M extends Any> = M[typeof MachineTypeId]["initialError"];
1064
+ /**
1065
+ * Extracts startup service requirements carried by a machine definition.
1066
+ *
1067
+ * @category utility types
1068
+ * @since 4.0.0
1069
+ */
1070
+ type InitialServices<M extends Any> = M[typeof MachineTypeId]["initialServices"];
1071
+ /**
1072
+ * Extracts final state paths carried by a machine definition.
1073
+ *
1074
+ * @category utility types
1075
+ * @since 4.0.0
1076
+ */
1077
+ type FinalStates<M extends Any> = M[typeof MachineTypeId]["finalStates"];
1078
+ /**
1079
+ * Extracts the terminal output channel carried by a machine definition.
1080
+ *
1081
+ * @category utility types
1082
+ * @since 4.0.0
1083
+ */
1084
+ type Output<M extends Any> = M[typeof MachineTypeId]["output"];
1085
+ /**
1086
+ * Extracts the emitted event schema tuple carried by a machine definition.
1087
+ *
1088
+ * @category utility types
1089
+ * @since 4.0.0
1090
+ */
1091
+ type Emits<M extends Any> = M[typeof MachineTypeId]["emits"];
1092
+ /**
1093
+ * Extracts state paths with implemented output handlers.
1094
+ *
1095
+ * @category utility types
1096
+ * @since 4.0.0
1097
+ */
1098
+ type OutputStates<M extends Any> = M[typeof MachineTypeId]["outputStates"];
1099
+ /**
1100
+ * Extracts the public input event schema tuple carried by a machine definition.
1101
+ *
1102
+ * @category utility types
1103
+ * @since 4.0.0
1104
+ */
1105
+ type InputEvents<M extends Any> = M[typeof MachineTypeId]["inputEvents"];
1106
+ /**
1107
+ * Extracts the complete event protocol handled inside a machine.
1108
+ *
1109
+ * @category utility types
1110
+ * @since 4.0.0
1111
+ */
1112
+ type Event<M extends Any> = EventOf<Events<M>>;
1113
+ /**
1114
+ * Extracts the event protocol accepted by public machine input boundaries.
1115
+ *
1116
+ * @category utility types
1117
+ * @since 4.0.0
1118
+ */
1119
+ type InputEvent<M extends Any> = EventOf<InputEvents<M>>;
1120
+ /**
1121
+ * Extracts the event protocol emitted by a machine.
1122
+ *
1123
+ * @category utility types
1124
+ * @since 4.0.0
1125
+ */
1126
+ type Emit<M extends Any> = EmitOf<Emits<M>>;
745
1127
  /**
746
1128
  * A schema whose decoded value contains a `_tag` discriminator.
747
1129
  *
@@ -796,13 +1178,27 @@ export declare namespace Machine {
796
1178
  readonly output?: Schema.Top;
797
1179
  readonly states: StateTree;
798
1180
  }
1181
+ /**
1182
+ * Pseudo-state that restores the last active configuration of its parent.
1183
+ *
1184
+ * History nodes are transition targets only. They never become active and
1185
+ * therefore do not declare a state value schema or lifecycle handlers.
1186
+ *
1187
+ * @category models
1188
+ * @since 4.0.0
1189
+ */
1190
+ interface HistoryStateNodeConfig {
1191
+ readonly type: "history";
1192
+ /** Defaults to shallow history. */
1193
+ readonly history?: "shallow" | "deep";
1194
+ }
799
1195
  /**
800
1196
  * Configuration accepted for an object state node.
801
1197
  *
802
1198
  * @category models
803
1199
  * @since 4.0.0
804
1200
  */
805
- type StateNodeConfig = AtomicStateNodeConfig | CompoundStateNodeConfig | ParallelStateNodeConfig;
1201
+ type StateNodeConfig = AtomicStateNodeConfig | CompoundStateNodeConfig | ParallelStateNodeConfig | HistoryStateNodeConfig;
806
1202
  /**
807
1203
  * Object state tree keyed by state path.
808
1204
  *
@@ -847,7 +1243,9 @@ export declare namespace Machine {
847
1243
  * @since 4.0.0
848
1244
  */
849
1245
  interface DefinedStates<States extends StateSchemas> {
1246
+ /** Original state tree supplied to {@link defineStates}. */
850
1247
  readonly states: States;
1248
+ /** Type-safe builders for constructing valid initial snapshots. */
851
1249
  readonly initial: InitialBuilder<States>;
852
1250
  /**
853
1251
  * Returns the decoded value for an active state path.
@@ -895,9 +1293,10 @@ export declare namespace Machine {
895
1293
  interface StateNode {
896
1294
  readonly path: string;
897
1295
  readonly key: string;
898
- readonly schema: TaggedSchema;
1296
+ readonly schema: TaggedSchema | undefined;
899
1297
  readonly output: Schema.Top | undefined;
900
- readonly type: "atomic" | "compound" | "parallel" | "final";
1298
+ readonly type: "atomic" | "compound" | "parallel" | "final" | "history";
1299
+ readonly history: "shallow" | "deep" | undefined;
901
1300
  readonly parent: string | undefined;
902
1301
  readonly children: ReadonlyArray<string>;
903
1302
  readonly initial: string | undefined;
@@ -958,10 +1357,47 @@ export declare namespace Machine {
958
1357
  * @since 4.0.0
959
1358
  */
960
1359
  type StateIdentifierWithPrefix<States extends StateSchemas, Prefix extends string = ""> = {
961
- readonly [Key in Extract<keyof States, string>]: States[Key] extends {
1360
+ readonly [Key in Extract<keyof States, string>]: States[Key] extends HistoryStateNodeConfig ? never : States[Key] extends {
962
1361
  readonly states: infer Children;
963
1362
  } ? Children extends StateSchemas ? JoinPath<Prefix, Key> | StateIdentifierWithPrefix<Children, JoinPath<Prefix, Key>> : JoinPath<Prefix, Key> : JoinPath<Prefix, Key>;
964
1363
  }[Extract<keyof States, string>];
1364
+ /**
1365
+ * Extracts the transition-only history pseudo-state paths in a definition.
1366
+ *
1367
+ * @category utility types
1368
+ * @since 4.0.0
1369
+ */
1370
+ type HistoryIdentifier<States extends StateSchemas> = HistoryIdentifierWithPrefix<States>;
1371
+ /** @internal */
1372
+ type HistoryIdentifierWithPrefix<States extends StateSchemas, Prefix extends string = ""> = {
1373
+ readonly [Key in Extract<keyof States, string>]: States[Key] extends HistoryStateNodeConfig ? JoinPath<Prefix, Key> : States[Key] extends {
1374
+ readonly states: infer Children extends StateSchemas;
1375
+ } ? HistoryIdentifierWithPrefix<Children, JoinPath<Prefix, Key>> : never;
1376
+ }[Extract<keyof States, string>];
1377
+ /** Active keys directly declared in a state tree. */
1378
+ type ActiveStateKey<States extends StateSchemas> = {
1379
+ readonly [Key in Extract<keyof States, string>]: States[Key] extends HistoryStateNodeConfig ? never : Key;
1380
+ }[Extract<keyof States, string>];
1381
+ /** History pseudo-state keys directly declared in a state tree. */
1382
+ type HistoryStateKey<States extends StateSchemas> = {
1383
+ readonly [Key in Extract<keyof States, string>]: States[Key] extends HistoryStateNodeConfig ? Key : never;
1384
+ }[Extract<keyof States, string>];
1385
+ /**
1386
+ * Active states that must implement implicit initial-value construction for
1387
+ * shallow history restoration.
1388
+ *
1389
+ * @category utility types
1390
+ * @since 4.0.0
1391
+ */
1392
+ type RequiredHistoryInitializers<States extends StateSchemas> = [HistoryIdentifier<States>] extends [never] ? never : Extract<RequiredHistoryInitializersWithPrefix<States, States, "">, StateIdentifier<States>>;
1393
+ /** Active parent states that own one or more history pseudo-states. */
1394
+ type HistoryParentIdentifier<States extends StateSchemas> = HistoryIdentifier<States> extends infer HistoryId ? HistoryId extends string ? Extract<ImmediateParentStateIdentifier<HistoryId>, StateIdentifier<States>> : never : never;
1395
+ /** History defaults and implicit initializers that remain unimplemented. */
1396
+ type MissingHistoryImplementations<States extends StateSchemas, UnhandledStates extends StateIdentifier<States>> = Extract<UnhandledStates, HistoryParentIdentifier<States> | RequiredHistoryInitializers<States>>;
1397
+ /** @internal Readiness proof required by planning and managed execution. */
1398
+ type EnsureHistoryImplementations<States extends StateSchemas, UnhandledStates extends StateIdentifier<States>> = [HistoryIdentifier<States>] extends [never] ? unknown : [MissingHistoryImplementations<States, UnhandledStates>] extends [never] ? unknown : {
1399
+ readonly "~effect/Machine/MissingHistoryImplementation": MissingHistoryImplementations<States, UnhandledStates>;
1400
+ };
965
1401
  /**
966
1402
  * Extracts a state-tree node by state path.
967
1403
  *
@@ -1028,6 +1464,13 @@ export declare namespace Machine {
1028
1464
  * @since 4.0.0
1029
1465
  */
1030
1466
  type ParentStateIdentifier<StateId extends string> = StateId extends `${infer Parent}.${infer Child}` ? Parent | (Child extends `${string}.${string}` ? `${Parent}.${ParentStateIdentifier<Child>}` : never) : never;
1467
+ /**
1468
+ * Extracts the nearest parent state path from a state identifier.
1469
+ *
1470
+ * @category utility types
1471
+ * @since 4.0.0
1472
+ */
1473
+ type ImmediateParentStateIdentifier<StateId extends string> = StateId extends `${infer Head}.${infer Tail}` ? Tail extends `${string}.${string}` ? `${Head}.${ImmediateParentStateIdentifier<Tail>}` : Head : never;
1031
1474
  /**
1032
1475
  * Maps every parent state path of a state identifier to its decoded value.
1033
1476
  *
@@ -1037,6 +1480,13 @@ export declare namespace Machine {
1037
1480
  type ParentStateValues<States extends StateSchemas, StateId extends StateIdentifier<States>> = StateId extends StateIdentifier<States> ? {
1038
1481
  readonly [Parent in Extract<ParentStateIdentifier<StateId>, StateIdentifier<States>>]: StateByIdentifier<States, Parent>;
1039
1482
  } : never;
1483
+ /**
1484
+ * Extracts the nearest parent value, or `undefined` for a root state.
1485
+ *
1486
+ * @category utility types
1487
+ * @since 4.0.0
1488
+ */
1489
+ type ParentStateValue<States extends StateSchemas, StateId extends StateIdentifier<States>> = StateId extends StateIdentifier<States> ? Extract<ImmediateParentStateIdentifier<StateId>, StateIdentifier<States>> extends infer Parent ? [Parent] extends [never] ? undefined : Parent extends StateIdentifier<States> ? StateByIdentifier<States, Parent> : undefined : undefined : never;
1040
1490
  /**
1041
1491
  * Represents a decoded state value together with all of its parent values.
1042
1492
  *
@@ -1061,9 +1511,10 @@ export declare namespace Machine {
1061
1511
  type DirectFinalCompletionOutput<States extends StateSchemas, StateId extends StateIdentifier<States>> = NodeByIdentifier<States, StateId> extends {
1062
1512
  readonly type: "final";
1063
1513
  } ? OutputByIdentifier<States, StateId> : never;
1064
- type CompoundCompletionOutput<States extends StateSchemas, Children extends StateSchemas, Prefix extends StateIdentifier<States>> = UndefinedIfNever<{
1065
- readonly [Key in Extract<keyof Children, string>]: DirectFinalCompletionOutput<States, Extract<JoinPath<Prefix, Key>, StateIdentifier<States>>>;
1066
- }[Extract<keyof Children, string>]>;
1514
+ type CompoundCompletionOutput<States extends StateSchemas, Children extends StateSchemas, Prefix extends StateIdentifier<States>> = UndefinedIfNever<CompoundCompletionOutputRaw<States, Children, Prefix>>;
1515
+ type CompoundCompletionOutputRaw<States extends StateSchemas, Children extends StateSchemas, Prefix extends StateIdentifier<States>> = {
1516
+ readonly [Key in ActiveStateKey<Children>]: DirectFinalCompletionOutput<States, Extract<JoinPath<Prefix, Key>, StateIdentifier<States>>>;
1517
+ }[ActiveStateKey<Children>];
1067
1518
  /**
1068
1519
  * Extracts the output passed when a state node completes.
1069
1520
  *
@@ -1077,6 +1528,52 @@ export declare namespace Machine {
1077
1528
  } ? CompoundCompletionOutput<States, Children, StateId> : Node extends {
1078
1529
  readonly type: "final";
1079
1530
  } ? OutputByIdentifier<States, StateId> : undefined : undefined;
1531
+ /**
1532
+ * Extracts the schema-derived union produced by structurally terminal root
1533
+ * states.
1534
+ *
1535
+ * **Details**
1536
+ *
1537
+ * Unlike a planned step's optional `output`, active atomic roots do not add
1538
+ * `undefined` to the union. Output-less final and parallel roots
1539
+ * intentionally contribute `undefined`, because that is their completed
1540
+ * value. Handler-driven reachability can make this structural union
1541
+ * conservative; for example, a root `onDone` handler can transition away
1542
+ * before that root becomes the machine's terminal result.
1543
+ *
1544
+ * @category utility types
1545
+ * @since 4.0.0
1546
+ */
1547
+ type TerminalOutput<States extends StateSchemas> = {
1548
+ readonly [Key in Extract<keyof States, string>]: Extract<Key, StateIdentifier<States>> extends infer StateId extends StateIdentifier<States> ? NodeByIdentifier<States, StateId> extends infer Node ? Node extends {
1549
+ readonly type: "parallel" | "final";
1550
+ } ? OutputByIdentifier<States, StateId> : Node extends {
1551
+ readonly states: infer Children extends StateSchemas;
1552
+ } ? CompoundCompletionOutputRaw<States, Children, StateId> : never : never : never;
1553
+ }[Extract<keyof States, string>];
1554
+ /**
1555
+ * Extracts every state path whose definition declares an output schema.
1556
+ *
1557
+ * @category utility types
1558
+ * @since 4.0.0
1559
+ */
1560
+ type DeclaredOutputState<States extends StateSchemas> = {
1561
+ readonly [StateId in StateIdentifier<States>]: NodeByIdentifier<States, StateId> extends {
1562
+ readonly output: Schema.Top;
1563
+ } ? StateId : never;
1564
+ }[StateIdentifier<States>] & StateIdentifier<States>;
1565
+ /**
1566
+ * Validates that every declared output schema has a matching handler
1567
+ * implementation before a machine is planned or started.
1568
+ *
1569
+ * @category utility types
1570
+ * @since 4.0.0
1571
+ */
1572
+ type EnsureOutputImplementations<States extends StateSchemas, OutputStates extends StateIdentifier<States>> = [
1573
+ Exclude<DeclaredOutputState<States>, OutputStates>
1574
+ ] extends [never] ? unknown : {
1575
+ readonly "~effect/Machine/MissingOutputImplementation": Exclude<DeclaredOutputState<States>, OutputStates>;
1576
+ };
1080
1577
  type OutputSchema<Node> = Node extends {
1081
1578
  readonly output: infer Output extends Schema.Top;
1082
1579
  } ? Output : never;
@@ -1119,6 +1616,12 @@ export declare namespace Machine {
1119
1616
  readonly path: string;
1120
1617
  readonly output?: unknown;
1121
1618
  }
1619
+ /** Encoded values and paths retained by one history pseudo-state. */
1620
+ interface EncodedSnapshotHistoryEntry {
1621
+ readonly mode: "shallow" | "deep";
1622
+ readonly active: ReadonlyArray<string>;
1623
+ readonly values: Readonly<Record<string, unknown>>;
1624
+ }
1122
1625
  /**
1123
1626
  * Normalized data representation of a machine snapshot.
1124
1627
  *
@@ -1135,6 +1638,7 @@ export declare namespace Machine {
1135
1638
  readonly _tag: "MachineSnapshot";
1136
1639
  readonly active: ReadonlyArray<EncodedSnapshotState>;
1137
1640
  readonly completed?: ReadonlyArray<EncodedSnapshotCompletion>;
1641
+ readonly history?: Readonly<Record<string, EncodedSnapshotHistoryEntry>>;
1138
1642
  }
1139
1643
  /**
1140
1644
  * Completed state path and its resolved output value.
@@ -1146,6 +1650,12 @@ export declare namespace Machine {
1146
1650
  readonly path: string;
1147
1651
  readonly output: unknown;
1148
1652
  }
1653
+ /** Decoded values and paths retained by one history pseudo-state. */
1654
+ interface SnapshotHistoryEntry {
1655
+ readonly mode: "shallow" | "deep";
1656
+ readonly active: ReadonlyArray<string>;
1657
+ readonly values: Readonly<Record<string, unknown>>;
1658
+ }
1149
1659
  /**
1150
1660
  * Carries lifecycle metadata required to resume planning from a cloned
1151
1661
  * snapshot.
@@ -1164,6 +1674,7 @@ export declare namespace Machine {
1164
1674
  */
1165
1675
  interface SnapshotMetadata {
1166
1676
  readonly completed?: ReadonlyArray<SnapshotCompletion>;
1677
+ readonly history?: Readonly<Record<string, SnapshotHistoryEntry>>;
1167
1678
  }
1168
1679
  /**
1169
1680
  * Atomic statechart snapshot carrying path identity separately from the
@@ -1220,8 +1731,8 @@ export declare namespace Machine {
1220
1731
  * @since 4.0.0
1221
1732
  */
1222
1733
  type SnapshotWithPrefix<States extends StateSchemas, Prefix extends string> = {
1223
- readonly [Key in Extract<keyof States, string>]: SnapshotByIdentifierWithPath<States, Key, JoinPath<Prefix, Key>>;
1224
- }[Extract<keyof States, string>];
1734
+ readonly [Key in ActiveStateKey<States>]: SnapshotByIdentifierWithPath<States, Key, JoinPath<Prefix, Key>>;
1735
+ }[ActiveStateKey<States>];
1225
1736
  /**
1226
1737
  * Extracts child snapshots under a parallel parent path prefix, keyed by
1227
1738
  * child region.
@@ -1230,7 +1741,7 @@ export declare namespace Machine {
1230
1741
  * @since 4.0.0
1231
1742
  */
1232
1743
  type SnapshotRegionsWithPrefix<States extends StateSchemas, Prefix extends string> = {
1233
- readonly [Key in Extract<keyof States, string>]: SnapshotByIdentifierWithPath<States, Key, JoinPath<Prefix, Key>>;
1744
+ readonly [Key in ActiveStateKey<States>]: SnapshotByIdentifierWithPath<States, Key, JoinPath<Prefix, Key>>;
1234
1745
  };
1235
1746
  /**
1236
1747
  * Extracts a snapshot for a state node while preserving its full path.
@@ -1238,7 +1749,7 @@ export declare namespace Machine {
1238
1749
  * @category utility types
1239
1750
  * @since 4.0.0
1240
1751
  */
1241
- type SnapshotByIdentifierWithPath<States extends StateSchemas, StateId extends Extract<keyof States, string>, Path extends string> = States[StateId] extends {
1752
+ type SnapshotByIdentifierWithPath<States extends StateSchemas, StateId extends ActiveStateKey<States>, Path extends string> = States[StateId] extends {
1242
1753
  readonly type: "parallel";
1243
1754
  readonly states: infer Children;
1244
1755
  } ? Children extends StateSchemas ? ParallelSnapshot<Path, NodeSchema<States[StateId]>["Type"], SnapshotRegionsWithPrefix<Children, Path>> : AtomicSnapshot<Path, NodeSchema<States[StateId]>["Type"]> : States[StateId] extends {
@@ -1252,8 +1763,8 @@ export declare namespace Machine {
1252
1763
  * @since 4.0.0
1253
1764
  */
1254
1765
  type Snapshot<States extends StateSchemas> = {
1255
- readonly [StateId in Extract<keyof States, string>]: SnapshotByIdentifier<States, StateId & StateIdentifier<States>>;
1256
- }[Extract<keyof States, string>];
1766
+ readonly [StateId in ActiveStateKey<States>]: SnapshotByIdentifier<States, StateId & StateIdentifier<States>>;
1767
+ }[ActiveStateKey<States>];
1257
1768
  /**
1258
1769
  * Extracts the root state identifier from a state path.
1259
1770
  *
@@ -1288,6 +1799,19 @@ export declare namespace Machine {
1288
1799
  type EventByTag<Events extends ReadonlyArray<TaggedSchema>, Tag extends TagOf<Events[number]>> = Extract<EventOf<Events>, {
1289
1800
  readonly _tag: Tag;
1290
1801
  }>;
1802
+ /**
1803
+ * Opaque state construction returned by a builder's `.from` method.
1804
+ *
1805
+ * The machine resolves the instruction through the selected state schema
1806
+ * while planning. Its decoded value is intentionally unavailable until
1807
+ * planning succeeds.
1808
+ *
1809
+ * @category models
1810
+ * @since 4.0.0
1811
+ */
1812
+ interface StateConstruction<Result> {
1813
+ readonly [Model.StateConstructionTypeId]: Result;
1814
+ }
1291
1815
  /**
1292
1816
  * Machine-bound target instruction accepted from transition handlers.
1293
1817
  *
@@ -1296,12 +1820,30 @@ export declare namespace Machine {
1296
1820
  */
1297
1821
  interface Target<States extends StateSchemas, StateId extends StateIdentifier<States>> {
1298
1822
  readonly [Model.TargetTypeId]: typeof Model.TargetTypeId;
1823
+ readonly [Model.TargetSnapshotTypeId]?: SnapshotByIdentifier<States, StateId>;
1299
1824
  readonly path: StateId;
1300
1825
  readonly value: StateByIdentifier<States, StateId>;
1301
1826
  readonly values?: Partial<{
1302
1827
  readonly [AncestorStateId in StateIdentifier<States>]: StateByIdentifier<States, AncestorStateId>;
1303
1828
  }>;
1304
1829
  }
1830
+ /**
1831
+ * Transition instruction that restores a history pseudo-state's parent.
1832
+ *
1833
+ * Unlike ordinary targets, history targets carry no state value. The
1834
+ * planner resolves the remembered concrete configuration, or evaluates the
1835
+ * history node's typed default when no record exists.
1836
+ *
1837
+ * @category models
1838
+ * @since 4.0.0
1839
+ */
1840
+ interface HistoryTarget<States extends StateSchemas, HistoryId extends HistoryIdentifier<States>> {
1841
+ readonly [Model.HistoryTargetTypeId]: typeof Model.HistoryTargetTypeId;
1842
+ readonly path: HistoryId;
1843
+ readonly parent: Extract<ParentPath<HistoryId>, StateIdentifier<States>>;
1844
+ }
1845
+ /** Builder containing only history pseudo-state paths. */
1846
+ type HistoryTargetBuilder<States extends StateSchemas> = HistoryTargetBuilderWithPrefix<States, States, "">;
1305
1847
  /**
1306
1848
  * Builder for complete transition snapshots.
1307
1849
  *
@@ -1325,7 +1867,7 @@ export declare namespace Machine {
1325
1867
  * @category utility types
1326
1868
  * @since 4.0.0
1327
1869
  */
1328
- type LocalTargetBuilder<States extends StateSchemas, Source extends StateIdentifier<States>> = NearestCompoundScope<States, Source> extends infer Scope ? [Scope] extends [never] ? {} : Scope extends StateIdentifier<States> ? LocalTargetBuilderForScope<States, Scope> : {} : {};
1870
+ type LocalTargetBuilder<States extends StateSchemas, Source extends StateIdentifier<States>> = NearestCompoundScope<States, Source> extends infer Scope ? [Scope] extends [never] ? {} : Scope extends StateIdentifier<States> ? LocalTargetBuilderForScope<States, Scope, Source> : {} : {};
1329
1871
  /**
1330
1872
  * Builder for partial transition targets within the active source root.
1331
1873
  *
@@ -1337,7 +1879,7 @@ export declare namespace Machine {
1337
1879
  * @category utility types
1338
1880
  * @since 4.0.0
1339
1881
  */
1340
- type BranchTargetBuilder<States extends StateSchemas, Source extends StateIdentifier<States>> = BranchTargetBuilderForRoot<States, Extract<RootStateIdentifier<Source>, Extract<keyof States, string>>>;
1882
+ type BranchTargetBuilder<States extends StateSchemas, Source extends StateIdentifier<States>> = BranchTargetBuilderForRoot<States, Extract<RootStateIdentifier<Source>, ActiveStateKey<States>>, Source>;
1341
1883
  /**
1342
1884
  * Machine-bound target builders available in transition contexts.
1343
1885
  *
@@ -1347,6 +1889,13 @@ export declare namespace Machine {
1347
1889
  * targets descendants of the source root, and `full` builds complete
1348
1890
  * snapshots for any root.
1349
1891
  *
1892
+ * These builders control how the next active configuration is assembled; they
1893
+ * do not directly control state re-entry. Exit and entry paths are derived
1894
+ * from the previous and next active paths. Shared active ancestors remain
1895
+ * entered even when a `full` target supplies their values again. Use an event
1896
+ * transition with `reenter: true` when the source should explicitly exit and
1897
+ * enter again.
1898
+ *
1350
1899
  * @category models
1351
1900
  * @since 4.0.0
1352
1901
  */
@@ -1379,15 +1928,22 @@ export declare namespace Machine {
1379
1928
  * @since 4.0.0
1380
1929
  */
1381
1930
  readonly full: FullTargetBuilder<States>;
1931
+ /** Restores a declared shallow or deep history pseudo-state. */
1932
+ readonly history: HistoryTargetBuilder<States>;
1382
1933
  }
1383
1934
  /**
1384
1935
  * Stages an Effect to run after the current machine step is planned.
1385
1936
  *
1937
+ * The two-argument overload returns `next` after staging the Effect, which is
1938
+ * useful when a handler should stage one action and return a target without
1939
+ * introducing an Effect generator.
1940
+ *
1386
1941
  * @category models
1387
1942
  * @since 4.0.0
1388
1943
  */
1389
1944
  interface Action {
1390
1945
  <E, R>(effect: Effect.Effect<void, E, R>): Effect.Effect<void, never, ActionRequirement<E, R>>;
1946
+ <E, R, Next>(effect: Effect.Effect<void, E, R>, next: Next): Effect.Effect<Next, never, ActionRequirement<E, R>>;
1391
1947
  }
1392
1948
  /**
1393
1949
  * Planning capabilities shared by transition and lifecycle callbacks.
@@ -1396,7 +1952,6 @@ export declare namespace Machine {
1396
1952
  * @since 4.0.0
1397
1953
  */
1398
1954
  interface PlanningCapabilities<Events, Emits> {
1399
- readonly action: Action;
1400
1955
  readonly raise: (event: Events) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError, Runtime.Requirement<Events, Emits>>;
1401
1956
  readonly emit: (event: Emits) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError, Runtime.Requirement<Events, Emits>>;
1402
1957
  }
@@ -1408,6 +1963,7 @@ export declare namespace Machine {
1408
1963
  */
1409
1964
  interface HandlerContext<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>, EventTag extends TagOf<Events[number]>, E, R> extends PlanningCapabilities<EventOf<Events>, EmitOf<Emits>> {
1410
1965
  readonly state: StateByIdentifier<States, StateId>;
1966
+ readonly parent: ParentStateValue<States, StateId>;
1411
1967
  readonly parents: ParentStateValues<States, StateId>;
1412
1968
  readonly event: EventByTag<Events, EventTag>;
1413
1969
  readonly runtime: RuntimeEffect<Events, Emits>;
@@ -1427,6 +1983,7 @@ export declare namespace Machine {
1427
1983
  */
1428
1984
  interface StateActionContext<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> extends PlanningCapabilities<EventOf<Events>, EmitOf<Emits>> {
1429
1985
  readonly state: StateByIdentifier<States, StateId>;
1986
+ readonly parent: ParentStateValue<States, StateId>;
1430
1987
  readonly parents: ParentStateValues<States, StateId>;
1431
1988
  readonly event: LifecycleEvent<Events>;
1432
1989
  readonly runtime: RuntimeEffect<Events, Emits>;
@@ -1439,6 +1996,7 @@ export declare namespace Machine {
1439
1996
  */
1440
1997
  interface InvokeContext<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> {
1441
1998
  readonly state: StateByIdentifier<States, StateId>;
1999
+ readonly parent: ParentStateValue<States, StateId>;
1442
2000
  readonly parents: ParentStateValues<States, StateId>;
1443
2001
  readonly event: LifecycleEvent<Events>;
1444
2002
  readonly runtime: RuntimeEffect<Events, Emits>;
@@ -1473,6 +2031,7 @@ export declare namespace Machine {
1473
2031
  */
1474
2032
  interface AlwaysContext<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> extends PlanningCapabilities<EventOf<Events>, EmitOf<Emits>> {
1475
2033
  readonly state: StateByIdentifier<States, StateId>;
2034
+ readonly parent: ParentStateValue<States, StateId>;
1476
2035
  readonly parents: ParentStateValues<States, StateId>;
1477
2036
  readonly event: LifecycleEvent<Events>;
1478
2037
  readonly runtime: RuntimeEffect<Events, Emits>;
@@ -1493,6 +2052,7 @@ export declare namespace Machine {
1493
2052
  */
1494
2053
  interface DoneContext<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> extends PlanningCapabilities<EventOf<Events>, EmitOf<Emits>> {
1495
2054
  readonly state: StateByIdentifier<States, StateId>;
2055
+ readonly parent: ParentStateValue<States, StateId>;
1496
2056
  readonly parents: ParentStateValues<States, StateId>;
1497
2057
  readonly event: LifecycleEvent<Events>;
1498
2058
  readonly output: CompletionOutputByIdentifier<States, StateId>;
@@ -1514,6 +2074,7 @@ export declare namespace Machine {
1514
2074
  */
1515
2075
  interface FinalOutputContext<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> {
1516
2076
  readonly state: StateByIdentifier<States, StateId>;
2077
+ readonly parent: ParentStateValue<States, StateId>;
1517
2078
  readonly parents: ParentStateValues<States, StateId>;
1518
2079
  readonly event: LifecycleEvent<Events>;
1519
2080
  }
@@ -1527,7 +2088,7 @@ export declare namespace Machine {
1527
2088
  readonly type: "parallel";
1528
2089
  readonly states: infer Children extends StateSchemas;
1529
2090
  } ? {
1530
- readonly [Key in Extract<keyof Children, string>]: CompletionOutputByIdentifier<States, Extract<JoinPath<StateId, Key>, StateIdentifier<States>>>;
2091
+ readonly [Key in ActiveStateKey<Children>]: CompletionOutputByIdentifier<States, Extract<JoinPath<StateId, Key>, StateIdentifier<States>>>;
1531
2092
  } : never;
1532
2093
  /**
1533
2094
  * Context passed to a parallel state output function.
@@ -1537,6 +2098,7 @@ export declare namespace Machine {
1537
2098
  */
1538
2099
  interface ParallelOutputContext<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> {
1539
2100
  readonly state: StateByIdentifier<States, StateId>;
2101
+ readonly parent: ParentStateValue<States, StateId>;
1540
2102
  readonly parents: ParentStateValues<States, StateId>;
1541
2103
  readonly event: LifecycleEvent<Events>;
1542
2104
  readonly outputs: ParallelOutputRegions<States, StateId>;
@@ -1554,7 +2116,7 @@ export declare namespace Machine {
1554
2116
  * @category utility types
1555
2117
  * @since 4.0.0
1556
2118
  */
1557
- type InitialResult<States extends StateSchemas, E, R> = Snapshot<States> | Effect.Effect<Snapshot<States>, E, R>;
2119
+ type InitialResult<States extends StateSchemas, E, R> = Snapshot<States> | StateConstruction<Snapshot<States>> | Effect.Effect<Snapshot<States> | StateConstruction<Snapshot<States>>, E, R>;
1558
2120
  /**
1559
2121
  * Return value accepted from transition handlers.
1560
2122
  *
@@ -1567,7 +2129,7 @@ export declare namespace Machine {
1567
2129
  * @category utility types
1568
2130
  * @since 4.0.0
1569
2131
  */
1570
- type HandlerResult<States extends StateSchemas, E, R> = Snapshot<States> | Target<States, StateIdentifier<States>> | void | Effect.Effect<Snapshot<States> | Target<States, StateIdentifier<States>> | void, E, R>;
2132
+ type HandlerResult<States extends StateSchemas, E, R> = Snapshot<States> | Target<States, StateIdentifier<States>> | HistoryTarget<States, HistoryIdentifier<States>> | StateConstruction<Snapshot<States> | Target<States, StateIdentifier<States>> | HistoryTarget<States, HistoryIdentifier<States>>> | void | Effect.Effect<Snapshot<States> | Target<States, StateIdentifier<States>> | HistoryTarget<States, HistoryIdentifier<States>> | StateConstruction<Snapshot<States> | Target<States, StateIdentifier<States>> | HistoryTarget<States, HistoryIdentifier<States>>> | void, E, R>;
1571
2133
  /**
1572
2134
  * Extracts the union of handler return values from a handler map.
1573
2135
  *
@@ -1603,6 +2165,18 @@ export declare namespace Machine {
1603
2165
  * @since 4.0.0
1604
2166
  */
1605
2167
  type StateActionReturn<Config, Key extends "entry" | "exit"> = Key extends keyof Config ? NonNullable<Config[Key]> extends (...args: any) => infer Ret ? Ret : never : never;
2168
+ /** Extracts the return value from an implicit initial child implementation. */
2169
+ type StateInitialReturn<Config> = Config extends {
2170
+ readonly initial?: infer Initial;
2171
+ } ? NonNullable<Initial> extends (...args: any) => infer Ret ? Ret : never : never;
2172
+ /** Extracts the return values from a state's history defaults. */
2173
+ type HistoryDefaultReturn<Config> = Config extends {
2174
+ readonly history?: infer History;
2175
+ } ? {
2176
+ readonly [Key in keyof NonNullable<History>]: NonNullable<History>[Key] extends {
2177
+ readonly default: (...args: any) => infer Ret;
2178
+ } ? Ret : never;
2179
+ }[keyof NonNullable<History>] : never;
1606
2180
  /**
1607
2181
  * Extracts the return value from an event transition config.
1608
2182
  *
@@ -1754,16 +2328,7 @@ export declare namespace Machine {
1754
2328
  * @category utility types
1755
2329
  * @since 4.0.0
1756
2330
  */
1757
- type ConfigServices<Config> = Effect.Services<EventHandlerReturn<Config>> | Effect.Services<AlwaysReturn<Config>> | Effect.Services<DoneReturn<Config>> | Effect.Services<StateActionReturn<Config, "entry">> | Effect.Services<StateActionReturn<Config, "exit">> | InvokeRequirements<Config>;
1758
- /**
1759
- * Resolves the tag of a state config when it is final.
1760
- *
1761
- * @category utility types
1762
- * @since 4.0.0
1763
- */
1764
- type FinalStateFromConfig<Config, StateTag extends PropertyKey> = Config extends {
1765
- readonly type: "final";
1766
- } ? StateTag : never;
2331
+ type ConfigServices<Config> = Effect.Services<EventHandlerReturn<Config>> | Effect.Services<AlwaysReturn<Config>> | Effect.Services<DoneReturn<Config>> | Effect.Services<StateActionReturn<Config, "entry">> | Effect.Services<StateActionReturn<Config, "exit">> | Effect.Services<StateInitialReturn<Config>> | Effect.Services<HistoryDefaultReturn<Config>> | InvokeRequirements<Config>;
1767
2332
  /**
1768
2333
  * Configuration for invoking a child process while a state is active.
1769
2334
  *
@@ -1780,10 +2345,17 @@ export declare namespace Machine {
1780
2345
  readonly initialError: Types.Covariant<ChildInitialError>;
1781
2346
  };
1782
2347
  readonly id: string;
2348
+ /**
2349
+ * Optional parent-local address for sending events to this invocation.
2350
+ *
2351
+ * The invocation `id` is only its state-local lifecycle key. Use an
2352
+ * explicit typed address when the parent must communicate with it.
2353
+ */
2354
+ readonly address?: string;
1783
2355
  /** @internal */
1784
- readonly addressable?: boolean;
2356
+ readonly descriptor?: ChildMachine.Any;
1785
2357
  src(): Logic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>;
1786
- snapshot?(context: InvokeSnapshotContext<ChildState, ChildError | ChildInitialError, ChildOutput>): Event | undefined;
2358
+ snapshot?(context: InvokeSnapshotContext<ChildState, ChildError, ChildOutput>): Event | undefined;
1787
2359
  onDone?(context: InvokeDoneContext<ChildOutput>): DeliveredOutput | undefined;
1788
2360
  }
1789
2361
  /** @internal */
@@ -1797,6 +2369,9 @@ export declare namespace Machine {
1797
2369
  readonly initialError: Types.Covariant<InitialError>;
1798
2370
  };
1799
2371
  }
2372
+ interface InvokeDefinitionValue {
2373
+ readonly [InvokeTypeId]: unknown;
2374
+ }
1800
2375
  /**
1801
2376
  * State-bound configuration for invoked child processes.
1802
2377
  *
@@ -1808,7 +2383,7 @@ export declare namespace Machine {
1808
2383
  * @category models
1809
2384
  * @since 4.0.0
1810
2385
  */
1811
- type InvokeDefinition<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> = AnyInvokeConfig<any, any, any, any, any, any> | ReadonlyArray<AnyInvokeConfig<any, any, any, any, any, any>> | ((context: InvokeContext<States, Events, Emits, StateId>) => AnyInvokeConfig<any, any, any, any, any, any> | ReadonlyArray<AnyInvokeConfig<any, any, any, any, any, any>>);
2386
+ type InvokeDefinition<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> = InvokeDefinitionValue | ReadonlyArray<InvokeDefinitionValue> | ((context: InvokeContext<States, Events, Emits, StateId>) => InvokeDefinitionValue | ReadonlyArray<InvokeDefinitionValue>);
1812
2387
  type OutputHandlerConfig<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>, Context> = NodeByIdentifier<States, StateId> extends {
1813
2388
  readonly output: Schema.Top;
1814
2389
  } ? {
@@ -1828,7 +2403,6 @@ export declare namespace Machine {
1828
2403
  * @since 4.0.0
1829
2404
  */
1830
2405
  type ActiveStateConfig<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>, E, R> = {
1831
- readonly type?: "active";
1832
2406
  readonly entry?: (context: StateActionContext<States, Events, Emits, StateId>) => StateActionResult<any, any>;
1833
2407
  readonly exit?: (context: StateActionContext<States, Events, Emits, StateId>) => StateActionResult<any, any>;
1834
2408
  readonly invoke?: InvokeDefinition<States, Events, Emits, StateId>;
@@ -1840,7 +2414,37 @@ export declare namespace Machine {
1840
2414
  readonly transition: (context: HandlerContext<States, Events, Emits, StateId, EventTag, E, R>) => HandlerResult<States, any, any>;
1841
2415
  };
1842
2416
  };
2417
+ readonly initial?: StateInitialHandler<States, Events, Emits, StateId>;
1843
2418
  } & ActiveOutputHandlerConfig<States, Events, StateId>;
2419
+ /** Values supplied when a statechart implicitly enters a state's initial children. */
2420
+ type StateInitialValue<States extends StateSchemas, StateId extends StateIdentifier<States>> = NodeByIdentifier<States, StateId> extends infer Node ? Node extends {
2421
+ readonly type: "parallel";
2422
+ readonly states: infer Children extends StateSchemas;
2423
+ } ? {
2424
+ readonly [Key in ActiveStateKey<Children>]: NodeSchema<Children[Key]>["Type"];
2425
+ } : Node extends {
2426
+ readonly states: infer Children extends StateSchemas;
2427
+ readonly initial: infer Initial;
2428
+ } ? Initial extends ActiveStateKey<Children> ? NodeSchema<Children[Initial]>["Type"] : never : never : never;
2429
+ /** Context passed to an implicit child-state initializer. */
2430
+ type StateInitialContext<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> = StateActionContext<States, Events, Emits, StateId>;
2431
+ /** Initial child value implementation for a compound or parallel state. */
2432
+ type StateInitialHandler<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> = (context: StateInitialContext<States, Events, Emits, StateId>) => StateInitialValue<States, StateId> | Effect.Effect<StateInitialValue<States, StateId>, any, any>;
2433
+ /** Context used only when a history node has no previously captured record. */
2434
+ interface HistoryDefaultContext<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, ParentId extends StateIdentifier<States>> extends PlanningCapabilities<EventOf<Events>, EmitOf<Emits>> {
2435
+ readonly event: LifecycleEvent<Events>;
2436
+ readonly runtime: RuntimeEffect<Events, Emits>;
2437
+ readonly target: FullTargetBuilder<States>;
2438
+ readonly parent: ParentId;
2439
+ }
2440
+ /** Typed fallback evaluated when a history node has no record yet. */
2441
+ type HistoryDefaultHandler<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, ParentId extends StateIdentifier<States>> = (context: HistoryDefaultContext<States, Events, Emits, ParentId>) => SnapshotByIdentifier<States, ParentId> | StateConstruction<SnapshotByIdentifier<States, ParentId>> | Effect.Effect<SnapshotByIdentifier<States, ParentId> | StateConstruction<SnapshotByIdentifier<States, ParentId>>, any, any>;
2442
+ /** Default implementations keyed by direct history child. */
2443
+ type HistoryDefaultConfig<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, ParentId extends StateIdentifier<States>, Children extends StateSchemas> = {
2444
+ readonly [Key in HistoryStateKey<Children>]?: {
2445
+ readonly default: HistoryDefaultHandler<States, Events, Emits, ParentId>;
2446
+ };
2447
+ };
1844
2448
  /**
1845
2449
  * Configuration accepted for a final state.
1846
2450
  *
@@ -1848,7 +2452,6 @@ export declare namespace Machine {
1848
2452
  * @since 4.0.0
1849
2453
  */
1850
2454
  type FinalStateConfig<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>> = {
1851
- readonly type: "final";
1852
2455
  readonly entry?: (context: StateActionContext<States, Events, Emits, StateId>) => StateActionResult<any, any>;
1853
2456
  readonly exit?: never;
1854
2457
  readonly always?: never;
@@ -1861,7 +2464,9 @@ export declare namespace Machine {
1861
2464
  * @category models
1862
2465
  * @since 4.0.0
1863
2466
  */
1864
- type HandlerConfig<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>, E, R> = ActiveStateConfig<States, Events, Emits, StateId, E, R> | FinalStateConfig<States, Events, Emits, StateId>;
2467
+ type HandlerConfig<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>, E, R> = NodeByIdentifier<States, StateId> extends {
2468
+ readonly type: "final";
2469
+ } ? FinalStateConfig<States, Events, Emits, StateId> : ActiveStateConfig<States, Events, Emits, StateId, E, R>;
1865
2470
  type HandlerChildren<Node> = Node extends {
1866
2471
  readonly states: infer Children extends StateSchemas;
1867
2472
  } ? Children : never;
@@ -1871,19 +2476,21 @@ export declare namespace Machine {
1871
2476
  };
1872
2477
  type HandlerNode<AllStates extends StateSchemas, Node, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, E, R, StateId extends StateIdentifier<AllStates>> = HandlerConfig<AllStates, Events, Emits, StateId, E, R> & (HandlerChildren<Node> extends infer Children extends StateSchemas ? [Children] extends [never] ? {
1873
2478
  readonly states?: never;
2479
+ readonly history?: never;
1874
2480
  } : {
1875
2481
  readonly states?: HandlerTree<AllStates, Children, Events, Emits, E, R, StateId>;
2482
+ readonly history?: HistoryDefaultConfig<AllStates, Events, Emits, StateId, Children>;
1876
2483
  } : {
1877
2484
  readonly states?: never;
2485
+ readonly history?: never;
1878
2486
  });
1879
2487
  type HandlerTree<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, E, R, Prefix extends string> = {
1880
- readonly [Key in Extract<keyof States, string>]?: HandlerNode<AllStates, States[Key], Events, Emits, E, R, HandlerStateId<AllStates, JoinPath<Prefix, Key>>>;
2488
+ readonly [Key in ActiveStateKey<States>]?: HandlerNode<AllStates, States[Key], Events, Emits, E, R, HandlerStateId<AllStates, JoinPath<Prefix, Key>>>;
1881
2489
  };
1882
- type HandlerNodeConfigKey = "always" | "entry" | "exit" | "invoke" | "on" | "onDone" | "output" | "states" | "type";
1883
- type HandlerValidationError<Message extends string> = {
1884
- readonly "~effect/Machine/HandlerError": Message;
2490
+ type HandlerNodeConfigKey = "always" | "entry" | "exit" | "history" | "initial" | "invoke" | "on" | "onDone" | "output" | "states";
2491
+ type HandlerValidationError<Message extends string, Path extends string, Detail = unknown> = {
2492
+ readonly "~effect/Machine/HandlerError": readonly [message: Message, path: Path, detail: Detail];
1885
2493
  };
1886
- type HandlerValidationErrors<Validation> = Validation extends HandlerValidationError<any> ? Validation : never;
1887
2494
  type NodeHasDeclaredOutput<States extends StateSchemas, StateId extends StateIdentifier<States>> = NodeByIdentifier<States, StateId> extends {
1888
2495
  readonly output: Schema.Top;
1889
2496
  } ? StateId : never;
@@ -1905,47 +2512,79 @@ export declare namespace Machine {
1905
2512
  readonly [Key in Extract<keyof Children, string>]: RequiredCompletionOutputStates<States, Extract<JoinPath<StateId, Key>, StateIdentifier<States>>>;
1906
2513
  }[Extract<keyof Children, string>] : never;
1907
2514
  type HandlerOutputStates<AllStates extends StateSchemas, StateId extends StateIdentifier<AllStates>, Config> = "output" extends keyof Config ? StateId : never;
1908
- type UnionToIntersection<Union> = (Union extends unknown ? (argument: Union) => void : never) extends (argument: infer Intersection) => void ? Intersection : never;
1909
- type HandlerUnknownStateKeyValidation<States extends StateSchemas, Config> = [Exclude<Extract<keyof Config, string>, Extract<keyof States, string>>] extends [never] ? unknown : HandlerValidationError<"Handler tree contains a state key that does not exist">;
1910
- type HandlerUnknownConfigKeyValidation<Config> = [
1911
- Exclude<Extract<keyof Config, string>, HandlerNodeConfigKey>
1912
- ] extends [never] ? unknown : HandlerValidationError<"Handler config contains an unknown key">;
1913
- type HandlerOnKeyValidation<Events extends ReadonlyArray<TaggedSchema>, Config> = Config extends {
1914
- readonly on?: infer On;
1915
- } ? [
1916
- Exclude<Extract<keyof NonNullable<On>, string>, TagOf<Events[number]>>
1917
- ] extends [never] ? unknown : HandlerValidationError<"Handler config contains an event key that does not exist"> : unknown;
2515
+ type HandlerUnknownStateKeyValidation<States extends StateSchemas, Prefix extends string, Config, UnknownKeys extends string = Exclude<Extract<keyof Config, string>, Extract<keyof States, string>>> = [UnknownKeys] extends [never] ? unknown : {
2516
+ readonly [Key in UnknownKeys]: HandlerValidationError<"Handler tree contains a state key that does not exist", JoinPath<Prefix, Key>, Key>;
2517
+ };
2518
+ type HandlerUnknownConfigKeyValidation<StateId extends string, Config, UnknownKeys extends string = Exclude<Extract<keyof Config, string>, HandlerNodeConfigKey>> = [UnknownKeys] extends [never] ? unknown : {
2519
+ readonly [Key in UnknownKeys]: HandlerValidationError<"Handler config contains an unknown key", StateId, Key>;
2520
+ };
2521
+ type HandlerOnKeyValidation<Events extends ReadonlyArray<TaggedSchema>, StateId extends string, Config, On = Config extends {
2522
+ readonly on?: infer Current;
2523
+ } ? NonNullable<Current> : never, UnknownKeys extends string = Exclude<Extract<keyof On, string>, TagOf<Events[number]>>> = "on" extends keyof Config ? [UnknownKeys] extends [never] ? unknown : {
2524
+ readonly on: {
2525
+ readonly [Key in UnknownKeys]: HandlerValidationError<"Handler config contains an event key that does not exist", StateId, Key>;
2526
+ };
2527
+ } : unknown;
1918
2528
  type HandlerDepth = readonly [unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown];
1919
2529
  type HandlerNextDepth<Depth extends ReadonlyArray<unknown>> = Depth extends readonly [unknown, ...infer Rest extends ReadonlyArray<unknown>] ? Rest : readonly [];
1920
- type HandlerChildrenValidation<AllStates extends StateSchemas, Node, Events extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = "states" extends keyof Config ? Depth extends readonly [] ? HandlerValidationError<"Handler nesting exceeds the supported depth"> : Config extends {
2530
+ type HandlerChildrenValidation<AllStates extends StateSchemas, Node, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = "states" extends keyof Config ? Depth extends readonly [] ? {
2531
+ readonly states: HandlerValidationError<"Handler nesting exceeds the supported depth", Prefix>;
2532
+ } : Config extends {
1921
2533
  readonly states?: infer ChildrenConfig;
1922
- } ? HandlerChildren<Node> extends infer Children extends StateSchemas ? [
1923
- Children
1924
- ] extends [never] ? HandlerValidationError<"Handler config contains child states for a state that has no children"> : HandlerTreeValidation<AllStates, Children, Events, Prefix, NonNullable<ChildrenConfig>, AvailableOutputStates, HandlerNextDepth<Depth>> : HandlerValidationError<"Handler config contains child states for a state that has no children"> : unknown : unknown;
2534
+ } ? HandlerChildren<Node> extends infer Children extends StateSchemas ? [Children] extends [never] ? {
2535
+ readonly states: HandlerValidationError<"Handler config contains child states for a state that has no children", Prefix>;
2536
+ } : HandlerTreeValidation<AllStates, Children, Events, Emits, Prefix, NonNullable<ChildrenConfig>, AvailableOutputStates, HandlerNextDepth<Depth>> extends infer Validation ? unknown extends Validation ? unknown : {
2537
+ readonly states: Validation;
2538
+ } : never : {
2539
+ readonly states: HandlerValidationError<"Handler config contains child states for a state that has no children", Prefix>;
2540
+ } : unknown : unknown;
1925
2541
  type HandlerOutputRequirementValidation<AllStates extends StateSchemas, StateId extends StateIdentifier<AllStates>, AvailableOutputStates extends StateIdentifier<AllStates>, Config> = ("onDone" extends keyof Config ? [
1926
2542
  Exclude<RequiredCompletionOutputStates<AllStates, StateId>, AvailableOutputStates>
1927
- ] extends [never] ? unknown : HandlerValidationError<"Handler config is missing an output implementation required by onDone"> : unknown) & ("output" extends keyof Config ? NodeByIdentifier<AllStates, StateId> extends {
2543
+ ] extends [never] ? unknown : {
2544
+ readonly onDone: HandlerValidationError<"Handler config is missing an output implementation required by onDone", StateId, Exclude<RequiredCompletionOutputStates<AllStates, StateId>, AvailableOutputStates>>;
2545
+ } : unknown) & ("output" extends keyof Config ? NodeByIdentifier<AllStates, StateId> extends {
1928
2546
  readonly type: "parallel";
1929
2547
  } ? [
1930
2548
  Exclude<RequiredParallelOutputStates<AllStates, StateId>, AvailableOutputStates>
1931
- ] extends [never] ? unknown : HandlerValidationError<"Handler config is missing a region output implementation required by parallel output"> : unknown : unknown);
1932
- type HandlerNodeValidation<AllStates extends StateSchemas, Node, Events extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<AllStates>, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = HandlerUnknownConfigKeyValidation<Config> & HandlerOnKeyValidation<Events, Config> & HandlerInvokeOutputValidation<Events, Config> & HandlerInvokeEmitsValidation<Events, Config> & HandlerInvokeSnapshotValidation<Events, Config> & HandlerChildrenValidation<AllStates, Node, Events, StateId, Config, AvailableOutputStates, Depth> & HandlerOutputRequirementValidation<AllStates, StateId, AvailableOutputStates, Config>;
1933
- type HandlerInvokeOutputValidation<Events extends ReadonlyArray<TaggedSchema>, Config> = [InvokeReturn<Config>] extends [never] ? unknown : [Exclude<InvokeOutput<InvokeReturn<Config>>, EventOf<Events> | void>] extends [never] ? unknown : HandlerValidationError<"Invoked child output must be a machine event or void">;
1934
- type HandlerInvokeEmitsValidation<Events extends ReadonlyArray<TaggedSchema>, Config> = [InvokeReturn<Config>] extends [never] ? unknown : [Exclude<InvokeEmits<InvokeReturn<Config>>, EventOf<Events>>] extends [never] ? unknown : HandlerValidationError<"Invoked child emits events not accepted by the parent machine">;
1935
- type HandlerInvokeSnapshotValidation<Events extends ReadonlyArray<TaggedSchema>, Config> = [InvokeReturn<Config>] extends [never] ? unknown : IsAny<InvokeSnapshotEvent<InvokeReturn<Config>>> extends true ? unknown : [Exclude<InvokeSnapshotEvent<InvokeReturn<Config>>, EventOf<Events> | undefined>] extends [never] ? unknown : HandlerValidationError<"Invoked child snapshot mapper must return a machine event or undefined">;
1936
- type HandlerTreeNodeValidationErrors<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = {
1937
- readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: HandlerValidationErrors<HandlerNodeValidation<AllStates, States[Key], Events, HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], AvailableOutputStates, Depth>>;
1938
- }[Extract<Extract<keyof Config, string>, Extract<keyof States, string>>];
1939
- type HandlerTreeNodeValidations<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = HandlerTreeNodeValidationErrors<AllStates, States, Events, Prefix, Config, AvailableOutputStates, Depth> extends infer Errors ? [Errors] extends [never] ? unknown : UnionToIntersection<Errors> : never;
1940
- type HandlerTreeValidation<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown> = HandlerDepth> = HandlerUnknownStateKeyValidation<States, Config> & HandlerTreeNodeValidations<AllStates, States, Events, Prefix, Config, AvailableOutputStates, Depth>;
2549
+ ] extends [never] ? unknown : {
2550
+ readonly output: HandlerValidationError<"Handler config is missing a region output implementation required by parallel output", StateId, Exclude<RequiredParallelOutputStates<AllStates, StateId>, AvailableOutputStates>>;
2551
+ } : unknown : unknown);
2552
+ type HandlerNodeValidation<AllStates extends StateSchemas, Node, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<AllStates>, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = HandlerUnknownConfigKeyValidation<StateId, Config> & HandlerOnKeyValidation<Events, StateId, Config> & HandlerInvokeOutputValidation<Events, StateId, Config> & HandlerInvokeEmitsValidation<Events, StateId, Config> & HandlerInvokeSnapshotValidation<Events, StateId, Config> & HandlerChildrenValidation<AllStates, Node, Events, Emits, StateId, Config, AvailableOutputStates, Depth> & HandlerOutputRequirementValidation<AllStates, StateId, AvailableOutputStates, Config> & HandlerRuntimeValidation<Events, Emits, StateId, Config>;
2553
+ type HandlerInvokeOutputValidation<Events extends ReadonlyArray<TaggedSchema>, StateId extends string, Config> = [InvokeReturn<Config>] extends [never] ? unknown : [Exclude<InvokeOutput<InvokeReturn<Config>>, EventOf<Events> | void>] extends [never] ? unknown : {
2554
+ readonly invoke: HandlerValidationError<"Invoked child output must be a machine event or void", StateId, Exclude<InvokeOutput<InvokeReturn<Config>>, EventOf<Events> | void>>;
2555
+ };
2556
+ type HandlerInvokeEmitsValidation<Events extends ReadonlyArray<TaggedSchema>, StateId extends string, Config> = [InvokeReturn<Config>] extends [never] ? unknown : [Exclude<InvokeEmits<InvokeReturn<Config>>, EventOf<Events>>] extends [never] ? unknown : {
2557
+ readonly invoke: HandlerValidationError<"Invoked child emits events not accepted by the parent machine", StateId, Exclude<InvokeEmits<InvokeReturn<Config>>, EventOf<Events>>>;
2558
+ };
2559
+ type HandlerInvokeSnapshotValidation<Events extends ReadonlyArray<TaggedSchema>, StateId extends string, Config> = [InvokeReturn<Config>] extends [never] ? unknown : IsAny<InvokeSnapshotEvent<InvokeReturn<Config>>> extends true ? unknown : [Exclude<InvokeSnapshotEvent<InvokeReturn<Config>>, EventOf<Events> | undefined>] extends [never] ? unknown : {
2560
+ readonly invoke: HandlerValidationError<"Invoked child snapshot mapper must return a machine event or undefined", StateId, Exclude<InvokeSnapshotEvent<InvokeReturn<Config>>, EventOf<Events> | undefined>>;
2561
+ };
2562
+ type HandlerRuntimeValidation<Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends string, Config, Incompatible = IncompatibleRuntime<ConfigServices<HandlerConfigPart<Config>>, EventOf<Events>, EmitOf<Emits>>> = [Incompatible] extends [never] ? unknown : HandlerValidationError<"Handler config requires an incompatible machine runtime", StateId, Incompatible>;
2563
+ type HandlerTreeNodeValidationMap<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = {
2564
+ readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]?: HandlerNodeValidation<AllStates, States[Key], Events, Emits, HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], AvailableOutputStates, Depth>;
2565
+ };
2566
+ type HandlerTreeNodeValidationErrors<Validations> = {
2567
+ readonly [Key in keyof Validations as unknown extends Validations[Key] ? never : Key]?: Validations[Key];
2568
+ };
2569
+ type HandlerTreeNodeValidations<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown>> = HandlerTreeNodeValidationMap<AllStates, States, Events, Emits, Prefix, Config, AvailableOutputStates, Depth> extends infer Validations ? HandlerTreeNodeValidationErrors<Validations> extends infer Errors ? keyof Errors extends never ? unknown : Errors : never : never;
2570
+ type HandlerTreeValidation<AllStates extends StateSchemas, States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Prefix extends string, Config, AvailableOutputStates extends StateIdentifier<AllStates>, Depth extends ReadonlyArray<unknown> = HandlerDepth> = HandlerUnknownStateKeyValidation<States, Prefix, Config> & HandlerTreeNodeValidations<AllStates, States, Events, Emits, Prefix, Config, AvailableOutputStates, Depth>;
1941
2571
  type HandlerNodeChildrenConfig<Config> = "states" extends keyof Config ? Config extends {
1942
2572
  readonly states?: infer Children;
1943
2573
  } ? NonNullable<Children> : never : never;
1944
2574
  type HandlerTreeStateIds<AllStates extends StateSchemas, States extends StateSchemas, Prefix extends string, Config, Depth extends ReadonlyArray<unknown> = HandlerDepth> = {
1945
- readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: HandlerStateId<AllStates, JoinPath<Prefix, Key>> | HandlerNodeChildStateIds<AllStates, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], Depth>;
2575
+ readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: HandlerImplementedStateId<AllStates, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key]> | HandlerNodeChildStateIds<AllStates, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], Depth>;
1946
2576
  }[Extract<Extract<keyof Config, string>, Extract<keyof States, string>>];
2577
+ type HandlerHasRequiredInitial<AllStates extends StateSchemas, StateId extends StateIdentifier<AllStates>, Config> = StateId extends RequiredHistoryInitializers<AllStates> ? "initial" extends keyof Config ? true : false : true;
2578
+ type HandlerHasRequiredHistoryDefaults<Node, Config> = Node extends {
2579
+ readonly states: infer Children extends StateSchemas;
2580
+ } ? [HistoryStateKey<Children>] extends [never] ? true : Config extends {
2581
+ readonly history?: infer HistoryConfig;
2582
+ } ? [
2583
+ Exclude<HistoryStateKey<Children>, Extract<keyof NonNullable<HistoryConfig>, string>>
2584
+ ] extends [never] ? true : false : false : true;
2585
+ type HandlerImplementedStateId<AllStates extends StateSchemas, Node, StateId extends StateIdentifier<AllStates>, Config> = [HistoryIdentifier<AllStates>] extends [never] ? StateId : HandlerHasRequiredInitial<AllStates, StateId, Config> extends true ? HandlerHasRequiredHistoryDefaults<Node, Config> extends true ? StateId : never : never;
1947
2586
  type HandlerNodeChildStateIds<AllStates extends StateSchemas, Node, Prefix extends string, Config, Depth extends ReadonlyArray<unknown>> = Depth extends readonly [] ? never : HandlerChildren<Node> extends infer Children extends StateSchemas ? [Children] extends [never] ? never : HandlerTreeStateIds<AllStates, Children, Prefix, HandlerNodeChildrenConfig<Config>, HandlerNextDepth<Depth>> : never;
1948
- type HandlerConfigError<Config> = Effect.Error<EventHandlerReturn<Config>> | Effect.Error<AlwaysReturn<Config>> | Effect.Error<DoneReturn<Config>> | Effect.Error<StateActionReturn<Config, "entry">> | Effect.Error<StateActionReturn<Config, "exit">> | InvokeError<Config>;
2587
+ type HandlerConfigError<Config> = Effect.Error<EventHandlerReturn<Config>> | Effect.Error<AlwaysReturn<Config>> | Effect.Error<DoneReturn<Config>> | Effect.Error<StateActionReturn<Config, "entry">> | Effect.Error<StateActionReturn<Config, "exit">> | Effect.Error<StateInitialReturn<Config>> | Effect.Error<HistoryDefaultReturn<Config>> | InvokeError<Config>;
1949
2588
  type HandlerTreeError<AllStates extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, States extends StateSchemas, Prefix extends string, Config, Depth extends ReadonlyArray<unknown> = HandlerDepth> = {
1950
2589
  readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: HandlerConfigError<HandlerConfigPart<Config[Key]>> | HandlerNodeChildError<AllStates, Events, Emits, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], Depth>;
1951
2590
  }[Extract<Extract<keyof Config, string>, Extract<keyof States, string>>];
@@ -1954,19 +2593,11 @@ export declare namespace Machine {
1954
2593
  readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: ConfigServices<HandlerConfigPart<Config[Key]>> | HandlerNodeChildServices<AllStates, Events, Emits, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], Depth>;
1955
2594
  }[Extract<Extract<keyof Config, string>, Extract<keyof States, string>>];
1956
2595
  type HandlerNodeChildServices<AllStates extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Node, Prefix extends string, Config, Depth extends ReadonlyArray<unknown>> = Depth extends readonly [] ? never : HandlerChildren<Node> extends infer Children extends StateSchemas ? [Children] extends [never] ? never : HandlerTreeServices<AllStates, Events, Emits, Children, Prefix, HandlerNodeChildrenConfig<Config>, HandlerNextDepth<Depth>> : never;
1957
- type HandlerTreeFinalStates<AllStates extends StateSchemas, States extends StateSchemas, Prefix extends string, Config, Depth extends ReadonlyArray<unknown> = HandlerDepth> = {
1958
- readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: Extract<FinalStateFromConfig<HandlerConfigPart<Config[Key]>, HandlerStateId<AllStates, JoinPath<Prefix, Key>>>, StateIdentifier<AllStates>> | HandlerNodeChildFinalStates<AllStates, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], Depth>;
1959
- }[Extract<Extract<keyof Config, string>, Extract<keyof States, string>>];
1960
- type HandlerNodeChildFinalStates<AllStates extends StateSchemas, Node, Prefix extends string, Config, Depth extends ReadonlyArray<unknown>> = Depth extends readonly [] ? never : HandlerChildren<Node> extends infer Children extends StateSchemas ? [Children] extends [never] ? never : HandlerTreeFinalStates<AllStates, Children, Prefix, HandlerNodeChildrenConfig<Config>, HandlerNextDepth<Depth>> : never;
1961
- type HandlerTreeOutput<AllStates extends StateSchemas, States extends StateSchemas, Prefix extends string, Config, Depth extends ReadonlyArray<unknown> = HandlerDepth> = {
1962
- readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: ("output" extends keyof HandlerConfigPart<Config[Key]> ? OutputByIdentifier<AllStates, HandlerStateId<AllStates, JoinPath<Prefix, Key>>> : never) | HandlerNodeChildOutput<AllStates, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], Depth>;
1963
- }[Extract<Extract<keyof Config, string>, Extract<keyof States, string>>];
1964
- type HandlerNodeChildOutput<AllStates extends StateSchemas, Node, Prefix extends string, Config, Depth extends ReadonlyArray<unknown>> = Depth extends readonly [] ? never : HandlerChildren<Node> extends infer Children extends StateSchemas ? [Children] extends [never] ? never : HandlerTreeOutput<AllStates, Children, Prefix, HandlerNodeChildrenConfig<Config>, HandlerNextDepth<Depth>> : never;
1965
2596
  type HandlerTreeOutputStates<AllStates extends StateSchemas, States extends StateSchemas, Prefix extends string, Config, Depth extends ReadonlyArray<unknown> = HandlerDepth> = {
1966
2597
  readonly [Key in Extract<Extract<keyof Config, string>, Extract<keyof States, string>>]: HandlerOutputStates<AllStates, HandlerStateId<AllStates, JoinPath<Prefix, Key>>, HandlerConfigPart<Config[Key]>> | HandlerNodeChildOutputStates<AllStates, States[Key], HandlerStateId<AllStates, JoinPath<Prefix, Key>>, Config[Key], Depth>;
1967
2598
  }[Extract<Extract<keyof Config, string>, Extract<keyof States, string>>];
1968
2599
  type HandlerNodeChildOutputStates<AllStates extends StateSchemas, Node, Prefix extends string, Config, Depth extends ReadonlyArray<unknown>> = Depth extends readonly [] ? never : HandlerChildren<Node> extends infer Children extends StateSchemas ? [Children] extends [never] ? never : HandlerTreeOutputStates<AllStates, Children, Prefix, HandlerNodeChildrenConfig<Config>, HandlerNextDepth<Depth>> : never;
1969
- type HandleTreeResult<AllStates extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Input extends Schema.Top, UnhandledStates extends StateIdentifier<AllStates>, E, R, InitialE, InitialR, FinalStates extends StateIdentifier<AllStates>, Output, OutputStates extends StateIdentifier<AllStates>, Config> = Machine<AllStates, Events, Input, Exclude<UnhandledStates, HandlerTreeStateIds<AllStates, AllStates, "", Config>>, E | HandlerTreeError<AllStates, Events, Emits, AllStates, "", Config>, ExcludeCompatibleRuntime<R | HandlerTreeServices<AllStates, Events, Emits, AllStates, "", Config>, EventOf<Events>, EmitOf<Emits>>, InitialE, InitialR, FinalStates | Extract<HandlerTreeFinalStates<AllStates, AllStates, "", Config>, StateIdentifier<AllStates>>, Output | HandlerTreeOutput<AllStates, AllStates, "", Config>, Emits, OutputStates | Extract<HandlerTreeOutputStates<AllStates, AllStates, "", Config>, StateIdentifier<AllStates>>>;
2600
+ type HandleTreeResult<AllStates extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Input extends Schema.Top, UnhandledStates extends StateIdentifier<AllStates>, E, R, InitialE, InitialR, FinalStates extends StateIdentifier<AllStates>, Output, OutputStates extends StateIdentifier<AllStates>, InputEvents extends ReadonlyArray<TaggedSchema>, Config> = Machine<AllStates, Events, Input, Exclude<UnhandledStates, HandlerTreeStateIds<AllStates, AllStates, "", Config>>, E | HandlerTreeError<AllStates, Events, Emits, AllStates, "", Config>, ExcludeCompatibleRuntime<R | HandlerTreeServices<AllStates, Events, Emits, AllStates, "", Config>, EventOf<Events>, EmitOf<Emits>>, InitialE, InitialR, FinalStates, Output, Emits, OutputStates | Extract<HandlerTreeOutputStates<AllStates, AllStates, "", Config>, StateIdentifier<AllStates>>, InputEvents>;
1970
2601
  /**
1971
2602
  * Adds state handlers from a root state object.
1972
2603
  *
@@ -1979,8 +2610,8 @@ export declare namespace Machine {
1979
2610
  * @category combinators
1980
2611
  * @since 4.0.0
1981
2612
  */
1982
- interface Handler<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Input extends Schema.Top, UnhandledStates extends StateIdentifier<States>, E, R, InitialE, InitialR, FinalStates extends StateIdentifier<States>, Output, OutputStates extends StateIdentifier<States>> {
1983
- <const Config extends HandlerTree<States, States, Events, Emits, E, R, "">>(config: Config & HandlerTreeValidation<States, States, Events, "", Config, OutputStates | Extract<HandlerTreeOutputStates<States, States, "", Config>, StateIdentifier<States>>> & EnsureCompatibleRuntime<HandlerTreeServices<States, Events, Emits, States, "", Config>, EventOf<Events>, EmitOf<Emits>>): HandleTreeResult<States, Events, Emits, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, OutputStates, Config>;
2613
+ interface Handler<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, Input extends Schema.Top, UnhandledStates extends StateIdentifier<States>, E, R, InitialE, InitialR, FinalStates extends StateIdentifier<States>, Output, OutputStates extends StateIdentifier<States>, InputEvents extends ReadonlyArray<TaggedSchema>> {
2614
+ <const Config extends HandlerTree<States, States, Events, Emits, E, R, "">>(config: Config & HandlerTreeValidation<States, States, Events, Emits, "", NoInfer<Config>, OutputStates | Extract<HandlerTreeOutputStates<States, States, "", NoInfer<Config>>, StateIdentifier<States>>>): HandleTreeResult<States, Events, Emits, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, OutputStates, InputEvents, Config>;
1984
2615
  }
1985
2616
  /**
1986
2617
  * Any state config.
@@ -2006,13 +2637,12 @@ export declare namespace Machine {
2006
2637
  * @since 4.0.0
2007
2638
  */
2008
2639
  interface StateConfig<States extends StateSchemas, Events extends ReadonlyArray<TaggedSchema>, Emits extends ReadonlyArray<TaggedSchema>, StateId extends StateIdentifier<States>, EventTag extends TagOf<Events[number]>, E, R> {
2009
- readonly type?: "final" | "active";
2010
2640
  readonly entry?: (context: StateActionContext<States, Events, Emits, StateId>) => StateActionResult<E, R>;
2011
2641
  readonly exit?: (context: StateActionContext<States, Events, Emits, StateId>) => StateActionResult<E, R>;
2012
2642
  readonly invoke?: InvokeDefinition<States, Events, Emits, StateId>;
2013
2643
  readonly always?: (context: AlwaysContext<States, Events, Emits, StateId>) => HandlerResult<States, E, R>;
2014
2644
  readonly onDone?: (context: DoneContext<States, Events, Emits, StateId>) => HandlerResult<States, E, R>;
2015
- readonly output?: ((context: FinalOutputContext<States, Events, StateId>) => any) | ((context: ParallelOutputContext<States, Events, StateId>) => any);
2645
+ readonly output?: ((context: FinalOutputContext<States, Events, StateId>) => unknown) | ((context: ParallelOutputContext<States, Events, StateId>) => unknown);
2016
2646
  readonly on?: EventHandlerMap<States, Events, Emits, StateId, EventTag, E, R>;
2017
2647
  }
2018
2648
  /**
@@ -2036,7 +2666,7 @@ export declare const isMachine: (u: unknown) => u is Machine.Any;
2036
2666
  * @category guards
2037
2667
  * @since 4.0.0
2038
2668
  */
2039
- export declare const isFinal: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema>, const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates>, state: Machine.Snapshot<States>) => state is Machine.SnapshotContainingFinal<States, FinalStates>;
2669
+ export declare const isFinal: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema>, const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents>, state: Machine.Snapshot<States>) => state is Machine.SnapshotContainingFinal<States, FinalStates>;
2040
2670
  /**
2041
2671
  * Defines a state tree while preserving literal state paths.
2042
2672
  *
@@ -2055,7 +2685,7 @@ export declare const isFinal: <const States extends Machine.StateSchemas, const
2055
2685
  *
2056
2686
  * ```ts
2057
2687
  * import { Schema } from "effect"
2058
- * import { Machine } from "effect/unstable/machine"
2688
+ * import { Machine } from "@typeonce/effect-machine"
2059
2689
  *
2060
2690
  * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
2061
2691
  *
@@ -2064,14 +2694,30 @@ export declare const isFinal: <const States extends Machine.StateSchemas, const
2064
2694
  * Machine.make({
2065
2695
  * states: States.states,
2066
2696
  * events: [],
2067
- * initial: () => States.initial.idle(new Idle({}))
2697
+ * initial: () => States.initial.idle.from()
2068
2698
  * })
2069
2699
  * ```
2070
2700
  *
2071
2701
  * @category constructors
2072
2702
  * @since 4.0.0
2073
2703
  */
2074
- export declare const defineStates: <const States extends Machine.StateSchemas>(states: States & DefineStateTreeInput<NoInfer<States>>, ..._validation: ValidateDefinedStates<NoInfer<States>>) => Machine.DefinedStates<States>;
2704
+ export declare const defineStates: DefineStates;
2705
+ type MakeConfig<States extends Machine.StateSchemas, InputEvents extends ReadonlyArray<Machine.TaggedSchema>, Emits extends ReadonlyArray<Machine.TaggedSchema>, Input extends Schema.Top, InitialE, InitialR, InternalEvents extends ReadonlyArray<Machine.TaggedSchema>> = {
2706
+ readonly id?: string;
2707
+ readonly states: States & DefineStateTreeInput<NoInfer<States>>;
2708
+ readonly events: InputEvents & ValidateInputEventProtocol<NoInfer<InputEvents>>;
2709
+ readonly internalEvents?: InternalEvents & ValidateInternalEventProtocol<NoInfer<InputEvents>, NoInfer<InternalEvents>>;
2710
+ readonly emits?: Emits;
2711
+ readonly input?: Input;
2712
+ readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>;
2713
+ };
2714
+ type MakeResult<States extends Machine.StateSchemas, InputEvents extends ReadonlyArray<Machine.TaggedSchema>, Emits extends ReadonlyArray<Machine.TaggedSchema>, Input extends Schema.Top, InitialE, InitialR, InternalEvents extends ReadonlyArray<Machine.TaggedSchema>> = Machine<States, readonly [...InputEvents, ...InternalEvents], Input, Machine.StateIdentifier<States>, never, never, InitialE, InitialR, Machine.FinalStateFromDefinition<States>, Machine.TerminalOutput<States>, Emits, never, InputEvents>;
2715
+ interface Make {
2716
+ <const States extends Machine.StateSchemas, const InputEvents extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, InitialE = never, InitialR = never, const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []>(config: MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>, ..._validation: ValidateDefinedStates<NoInfer<States>>): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>;
2717
+ <const States extends Machine.StateSchemas, const InputEvents extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, InitialE = never, InitialR = never, const InternalEvents extends ReadonlyArray<Machine.TaggedSchema> = readonly []>(config: Omit<MakeConfig<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents>, "states"> & {
2718
+ readonly states: InvalidDefinedStateTreeInput<States>;
2719
+ }): never;
2720
+ }
2075
2721
  /**
2076
2722
  * Creates a schema-first machine definition.
2077
2723
  *
@@ -2083,11 +2729,16 @@ export declare const defineStates: <const States extends Machine.StateSchemas>(s
2083
2729
  * `defineStates` or is passed inline. Call `handle` on the returned definition
2084
2730
  * to implement state behavior with ordinary TypeScript control flow.
2085
2731
  *
2732
+ * Schemas in `events` define the public input protocol. Schemas in
2733
+ * `internalEvents` are added to the complete handler protocol for invoke
2734
+ * results, child emissions, and other machine-local deliveries. Their tags
2735
+ * must be disjoint.
2736
+ *
2086
2737
  * **Example** (Typed counter machine)
2087
2738
  *
2088
2739
  * ```ts
2089
2740
  * import { Schema } from "effect"
2090
- * import { Machine } from "effect/unstable/machine"
2741
+ * import { Machine } from "@typeonce/effect-machine"
2091
2742
  *
2092
2743
  * class Count extends Schema.TaggedClass<Count>("Count")("Count", {
2093
2744
  * value: Schema.Number
@@ -2117,14 +2768,7 @@ export declare const defineStates: <const States extends Machine.StateSchemas>(s
2117
2768
  * @category constructors
2118
2769
  * @since 4.0.0
2119
2770
  */
2120
- export declare const make: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, InitialE = never, InitialR = never>(config: {
2121
- readonly id?: string;
2122
- readonly states: States & DefineStateTreeInput<NoInfer<States>>;
2123
- readonly events: Events;
2124
- readonly emits?: Emits;
2125
- readonly input?: Input;
2126
- readonly initial: (...args: [...Machine.InputArgs<Input>]) => Machine.InitialResult<States, InitialE, InitialR>;
2127
- }, ..._validation: ValidateDefinedStates<NoInfer<States>>) => Machine<States, Events, Input, Machine.StateIdentifier<States>, never, never, InitialE, InitialR, Machine.FinalStateFromDefinition<States>, never, Emits>;
2771
+ export declare const make: Make;
2128
2772
  /**
2129
2773
  * Encodes a decoded machine snapshot into a normalized data representation.
2130
2774
  *
@@ -2152,7 +2796,7 @@ export declare const make: <const States extends Machine.StateSchemas, const Eve
2152
2796
  * @category encoding
2153
2797
  * @since 4.0.0
2154
2798
  */
2155
- export declare const encodeSnapshot: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>, snapshot: Machine.Snapshot<States>) => Effect.Effect<Machine.EncodedSnapshot, MachineSchemaEncodeError, Machine.SnapshotEncodingServices<States>>;
2799
+ export declare const encodeSnapshot: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents>, snapshot: Machine.Snapshot<States>) => Effect.Effect<Machine.EncodedSnapshot, MachineSchemaEncodeError, Machine.SnapshotEncodingServices<States>>;
2156
2800
  /**
2157
2801
  * Decodes a normalized data representation into a validated machine snapshot.
2158
2802
  *
@@ -2176,7 +2820,7 @@ export declare const encodeSnapshot: <const States extends Machine.StateSchemas,
2176
2820
  * @category decoding
2177
2821
  * @since 4.0.0
2178
2822
  */
2179
- export declare const decodeSnapshot: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>, encoded: unknown) => Effect.Effect<Machine.Snapshot<States>, MachineSchemaDecodeError, Machine.SnapshotDecodingServices<States>>;
2823
+ export declare const decodeSnapshot: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents>, encoded: unknown) => Effect.Effect<Machine.Snapshot<States>, MachineSchemaDecodeError, Machine.SnapshotDecodingServices<States>>;
2180
2824
  /**
2181
2825
  * Creates an invoked child process configuration for an active state.
2182
2826
  *
@@ -2192,6 +2836,8 @@ export declare const decodeSnapshot: <const States extends Machine.StateSchemas,
2192
2836
  * Invoked child processes run while their owning state is active and are
2193
2837
  * stopped before the state exits. An unrecovered child failure fails the owning
2194
2838
  * machine; recover inside the child Effect when failure should become an event.
2839
+ * The `id` is a state-local lifecycle key, not a communication address. To
2840
+ * send events to the invocation, pass a typed `childAddress` as `address`.
2195
2841
  * The `src` callback is intentionally independent from its parent state. When
2196
2842
  * construction depends on the typed state, lifecycle event, or runtime, use
2197
2843
  * the state config factory form `invoke: (context) => Machine.invoke(...)` and
@@ -2201,7 +2847,7 @@ export declare const decodeSnapshot: <const States extends Machine.StateSchemas,
2201
2847
  *
2202
2848
  * ```ts
2203
2849
  * import { Effect, Schema } from "effect"
2204
- * import { Machine } from "effect/unstable/machine"
2850
+ * import { Machine } from "@typeonce/effect-machine"
2205
2851
  *
2206
2852
  * class Loaded extends Schema.TaggedClass<Loaded>("Loaded")("Loaded", {
2207
2853
  * value: Schema.String
@@ -2218,11 +2864,94 @@ export declare const decodeSnapshot: <const States extends Machine.StateSchemas,
2218
2864
  * @category constructors
2219
2865
  * @since 4.0.0
2220
2866
  */
2221
- export declare const invoke: <ChildState, ChildEvent, ChildError = never, ChildRequirements = never, ChildOutput = never, ChildInitialError = never, Event = never, Id extends string = string>(config: {
2222
- readonly id: Id;
2867
+ export declare const invoke: <ChildState, ChildEvent, ChildError = never, ChildRequirements = never, ChildOutput = never, ChildInitialError = never, Event = never, Address extends ChildAddress<never> | undefined = undefined>(config: {
2868
+ readonly id: InvokeLifecycleId;
2223
2869
  readonly src: () => Logic<ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>;
2224
- readonly snapshot?: (context: Machine.InvokeSnapshotContext<ChildState, ChildError | ChildInitialError, ChildOutput>) => Event | undefined;
2225
- } & ChildAddress.Compatibility<Id, ChildEvent>) => Machine.InvokeConfig<any, any, any, any, Event, ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>;
2870
+ readonly snapshot?: (context: Machine.InvokeSnapshotContext<ChildState, ChildError, ChildOutput>) => Event | undefined;
2871
+ } & ([Address] extends [undefined] ? {
2872
+ readonly address?: never;
2873
+ } : {
2874
+ readonly address: Exclude<Address, undefined>;
2875
+ } & ChildAddress.Compatibility<Exclude<Address, undefined>, NoInfer<ChildEvent>>)) => Machine.InvokeConfig<any, any, any, any, Event, ChildState, ChildEvent, ChildError, ChildRequirements, ChildOutput, ChildInitialError>;
2876
+ type InvokeEffectResult<Requirements, Event> = Machine.InvokeConfig<any, any, any, any, never, void, never, never, Requirements, Event | void, never>;
2877
+ type InvokeEffectIsInfallible<Fx extends Effect.Effect<any, any, any>> = IsAny<Effect.Error<Fx>> extends true ? false : [Effect.Error<Fx>] extends [never] ? true : false;
2878
+ type InvokeEffectConfig<Fx extends Effect.Effect<any, any, any>, SuccessEvent, FailureEvent> = {
2879
+ readonly id: InvokeLifecycleId;
2880
+ readonly effect: Fx;
2881
+ readonly onSuccess: (value: NoInfer<Effect.Success<Fx>>) => SuccessEvent | void;
2882
+ } & (InvokeEffectIsInfallible<Fx> extends true ? {
2883
+ readonly onFailure?: never;
2884
+ } : {
2885
+ readonly onFailure: (error: NoInfer<Effect.Error<Fx>>) => FailureEvent | void;
2886
+ });
2887
+ /**
2888
+ * Invokes one Effect and maps its typed outcome into machine-local events.
2889
+ *
2890
+ * **Details**
2891
+ *
2892
+ * This is the high-level one-shot counterpart to `invoke`. Success and typed
2893
+ * failure values are mapped independently, so callers do not need to recover
2894
+ * an Effect into a common event union by hand. Defects and interruption remain
2895
+ * failures of the owning machine.
2896
+ *
2897
+ * Declare mapped outcomes in `internalEvents` unless they are also legitimate
2898
+ * public commands.
2899
+ *
2900
+ * @see {@link invoke} for arbitrary child process logic.
2901
+ * @see {@link after} for a state-scoped delayed event.
2902
+ * @category constructors
2903
+ * @since 4.0.0
2904
+ */
2905
+ export declare const invokeEffect: <const Fx extends Effect.Effect<any, any, any>, SuccessEvent, FailureEvent = never>(config: InvokeEffectConfig<Fx, SuccessEvent, FailureEvent>) => InvokeEffectResult<Effect.Services<Fx>, SuccessEvent | (InvokeEffectIsInfallible<Fx> extends true ? never : FailureEvent)>;
2906
+ /**
2907
+ * Creates a cancellable state-scoped delayed event.
2908
+ *
2909
+ * The timer starts when its owning state is entered and is interrupted when
2910
+ * that state exits. The delayed value should normally be declared in
2911
+ * `internalEvents`.
2912
+ *
2913
+ * @category constructors
2914
+ * @since 4.0.0
2915
+ */
2916
+ export declare const after: <Event extends {
2917
+ readonly _tag: PropertyKey;
2918
+ }>(duration: Duration.Input, event: Event, options?: {
2919
+ readonly id?: InvokeLifecycleId;
2920
+ }) => InvokeEffectResult<never, Event>;
2921
+ type RetagFields<Target extends Machine.TaggedSchema> = Omit<Target["~type.make.in"], "_tag">;
2922
+ type RetagTargetCompatibility<Target extends Machine.TaggedSchema> = Target extends {
2923
+ readonly fields: Schema.Struct.Fields;
2924
+ } ? "_tag" extends keyof Target["~type.make.in"] ? {} extends Pick<Target["~type.make.in"], "_tag"> ? unknown : {
2925
+ readonly "~effect/Machine/RetagTargetError": "Target schema must supply its discriminator when make is called";
2926
+ } : unknown : {
2927
+ readonly "~effect/Machine/RetagTargetError": "Target schema must be one tagged struct or tagged class";
2928
+ };
2929
+ type RequiredKeys<A> = {
2930
+ readonly [Key in keyof A]-?: {} extends Pick<A, Key> ? never : Key;
2931
+ }[keyof A];
2932
+ type CompatibleSourceKeys<Fields, Source> = {
2933
+ readonly [Key in Extract<keyof Fields, keyof Source>]: Source[Key] extends Fields[Key] ? Key : never;
2934
+ }[Extract<keyof Fields, keyof Source>];
2935
+ type RetagPatch<Target extends Machine.TaggedSchema, Source> = Partial<RetagFields<Target>> & Pick<RetagFields<Target>, Exclude<RequiredKeys<RetagFields<Target>>, CompatibleSourceKeys<RetagFields<Target>, Source>>>;
2936
+ type RetagArgs<Target extends Machine.TaggedSchema, Source> = [
2937
+ Exclude<RequiredKeys<RetagFields<Target>>, CompatibleSourceKeys<RetagFields<Target>, Source>>
2938
+ ] extends [never] ? [patch?: RetagPatch<Target, Source>] : [patch: RetagPatch<Target, Source>];
2939
+ /**
2940
+ * Constructs another tagged case from compatible source fields.
2941
+ *
2942
+ * The target must be one tagged struct or tagged class whose discriminator is
2943
+ * supplied by its constructor. Union wrappers are rejected because their
2944
+ * `make` operation cannot select a member after the source discriminator has
2945
+ * been discarded. Missing or incompatible required target fields must be
2946
+ * supplied by the patch, and the target schema's normal `make` validation
2947
+ * remains authoritative at runtime.
2948
+ *
2949
+ * @category constructors
2950
+ * @since 4.0.0
2951
+ */
2952
+ export declare const retag: <const Target extends Machine.TaggedSchema, const Source extends {
2953
+ readonly _tag: PropertyKey;
2954
+ }>(target: Target & RetagTargetCompatibility<Target>, source: Source, ...args: RetagArgs<Target, Source>) => Target["Type"];
2226
2955
  type InvokeMachineInput<Input extends Schema.Top> = Input extends typeof Schema.Void ? {
2227
2956
  readonly input?: never;
2228
2957
  } : {
@@ -2254,16 +2983,16 @@ type InvokeMachineInput<Input extends Schema.Top> = Input extends typeof Schema.
2254
2983
  * @since 4.0.0
2255
2984
  */
2256
2985
  export declare const invokeMachine: {
2257
- <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, SnapshotEvent = never, DoneEvent = never, Id extends string = string>(config: {
2258
- readonly child: ChildMachine<Id, Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>>;
2259
- readonly snapshot?: (context: Machine.InvokeSnapshotContext<Machine.Snapshot<States>, E | InitialE | ActionError<R | InitialR> | InfiniteTransitionError | MachineSchemaDecodeError | StartupError | StoppedError, Output | undefined>) => SnapshotEvent | undefined;
2260
- readonly onDone: (context: Machine.InvokeDoneContext<Output | undefined>) => DoneEvent | undefined;
2261
- } & InvokeMachineInput<Input>): Machine.InvokeConfig<any, any, any, any, SnapshotEvent, Machine.Snapshot<States>, Machine.EventOf<Events>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, ExcludeCompatibleRuntime<Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>, Output | undefined, InitialE | ActionError<InitialR | R> | MachineSchemaDecodeError | StartupError | StoppedError, Machine.EmitOf<Emits>, DoneEvent>;
2262
- <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, SnapshotEvent = never, Id extends string = string>(config: {
2263
- readonly child: ChildMachine<Id, Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>>;
2264
- readonly snapshot?: (context: Machine.InvokeSnapshotContext<Machine.Snapshot<States>, E | InitialE | ActionError<R | InitialR> | InfiniteTransitionError | MachineSchemaDecodeError | StartupError | StoppedError, Output | undefined>) => SnapshotEvent | undefined;
2986
+ <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, SnapshotEvent = never, DoneEvent = never, Id extends string = string, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(config: {
2987
+ readonly child: ChildMachine<Id, Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates> & Machine.EnsureHistoryImplementations<States, UnhandledStates>>;
2988
+ readonly snapshot?: (context: Machine.InvokeSnapshotContext<Machine.Snapshot<States>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, Output>) => SnapshotEvent | undefined;
2989
+ readonly onDone: (context: Machine.InvokeDoneContext<Output>) => DoneEvent | undefined;
2990
+ } & InvokeMachineInput<Input>): Machine.InvokeConfig<any, any, any, any, SnapshotEvent, Machine.Snapshot<States>, Machine.EventOf<InputEvents>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, ExcludeCompatibleRuntime<Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>, Output, InitialE | E | ActionError<InitialR | R> | InfiniteTransitionError | MachineSchemaDecodeError | StartupError | StoppedError, Machine.EmitOf<Emits>, DoneEvent>;
2991
+ <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, SnapshotEvent = never, Id extends string = string, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(config: {
2992
+ readonly child: ChildMachine<Id, Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates> & Machine.EnsureHistoryImplementations<States, UnhandledStates>>;
2993
+ readonly snapshot?: (context: Machine.InvokeSnapshotContext<Machine.Snapshot<States>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, Output>) => SnapshotEvent | undefined;
2265
2994
  readonly onDone?: never;
2266
- } & InvokeMachineInput<Input>): Machine.InvokeConfig<any, any, any, any, SnapshotEvent, Machine.Snapshot<States>, Machine.EventOf<Events>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, ExcludeCompatibleRuntime<Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>, Output | undefined, InitialE | ActionError<InitialR | R> | MachineSchemaDecodeError | StartupError | StoppedError, Machine.EmitOf<Emits>>;
2995
+ } & InvokeMachineInput<Input>): Machine.InvokeConfig<any, any, any, any, SnapshotEvent, Machine.Snapshot<States>, Machine.EventOf<InputEvents>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, ExcludeCompatibleRuntime<Exclude<ExecutionServices<InitialR | R>, internalRuntime.MachineRuntime>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>, Output, InitialE | E | ActionError<InitialR | R> | InfiniteTransitionError | MachineSchemaDecodeError | StartupError | StoppedError, Machine.EmitOf<Emits>>;
2267
2996
  };
2268
2997
  /**
2269
2998
  * Plans the initial state for a machine without running deferred actions.
@@ -2286,19 +3015,24 @@ export declare const invokeMachine: {
2286
3015
  * @category constructors
2287
3016
  * @since 4.0.0
2288
3017
  */
2289
- export declare const planInitial: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>, ...args: [...Machine.InputArgs<Input>]) => Effect.Effect<{
3018
+ export declare const planInitial: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates> & Machine.EnsureHistoryImplementations<States, UnhandledStates>, ...args: [...Machine.InputArgs<Input>]) => Effect.Effect<{
2290
3019
  readonly state: Machine.Snapshot<States>;
2291
3020
  readonly actions: ReadonlyArray<Effect.Effect<void, ActionError<InitialR | R>, ActionServices<InitialR | R>>>;
2292
3021
  readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>;
2293
- readonly output: Output | undefined;
2294
- }, InitialE | MachineSchemaDecodeError | StartupError, ExcludeCompatibleRuntime<PlanningServices<InitialR | R>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>>;
3022
+ } & ({
3023
+ readonly done: true;
3024
+ readonly output: Output;
3025
+ } | {
3026
+ readonly done: false;
3027
+ readonly output: undefined;
3028
+ }), InitialE | E | InfiniteTransitionError | MachineSchemaDecodeError | StartupError, ExcludeCompatibleRuntime<PlanningServices<InitialR | R>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>>;
2295
3029
  /**
2296
3030
  * Returns the event tags handled by the current state snapshot.
2297
3031
  *
2298
3032
  * @category getters
2299
3033
  * @since 4.0.0
2300
3034
  */
2301
- export declare const enabled: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema>, const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates>, state: Machine.Snapshot<States>) => ReadonlyArray<Machine.TagOf<Events[number]>>;
3035
+ export declare const enabled: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema>, const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents>, state: Machine.Snapshot<States>) => ReadonlyArray<Machine.TagOf<Events[number]>>;
2302
3036
  /**
2303
3037
  * Plans the next state snapshot without running deferred actions.
2304
3038
  *
@@ -2322,7 +3056,7 @@ export declare const enabled: <const States extends Machine.StateSchemas, const
2322
3056
  * @category combinators
2323
3057
  * @since 4.0.0
2324
3058
  */
2325
- export declare const plan: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>, state: Machine.Snapshot<States>, event: Machine.EventOf<Events>) => Effect.Effect<{
3059
+ export declare const plan: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates> & Machine.EnsureHistoryImplementations<States, UnhandledStates>, state: Machine.Snapshot<States>, event: Machine.EventOf<InputEvents>) => Effect.Effect<{
2326
3060
  readonly next: Machine.Snapshot<States>;
2327
3061
  readonly actions: ReadonlyArray<Effect.Effect<void, ActionError<R>, ActionServices<R>>>;
2328
3062
  readonly emittedEvents: ReadonlyArray<Machine.EmitOf<Emits>>;
@@ -2336,8 +3070,13 @@ export declare const plan: <const States extends Machine.StateSchemas, const Eve
2336
3070
  readonly entryPaths: ReadonlyArray<string>;
2337
3071
  readonly changed: boolean;
2338
3072
  }>;
2339
- readonly output: Output | undefined;
2340
- }, E | InfiniteTransitionError | MachineSchemaDecodeError, ExcludeCompatibleRuntime<PlanningServices<R>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>>;
3073
+ } & ({
3074
+ readonly done: true;
3075
+ readonly output: Output;
3076
+ } | {
3077
+ readonly done: false;
3078
+ readonly output: undefined;
3079
+ }), E | InfiniteTransitionError | MachineSchemaDecodeError, ExcludeCompatibleRuntime<PlanningServices<R>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>>;
2341
3080
  /**
2342
3081
  * Defers an effectful action until the current machine step is planned.
2343
3082
  *
@@ -2346,13 +3085,14 @@ export declare const plan: <const States extends Machine.StateSchemas, const Eve
2346
3085
  * The action's error and service requirements are retained in the machine
2347
3086
  * type without becoming requirements of `plan` or `planInitial`. The managed
2348
3087
  * runtime executes staged actions sequentially before publishing the planned
2349
- * state.
3088
+ * state. Pass a second argument when the planning Effect should return that
3089
+ * value after staging.
2350
3090
  *
2351
3091
  * **Example** (Typed staged action)
2352
3092
  *
2353
3093
  * ```ts
2354
3094
  * import { Context, Effect } from "effect"
2355
- * import { Machine } from "effect/unstable/machine"
3095
+ * import { Machine } from "@typeonce/effect-machine"
2356
3096
  *
2357
3097
  * class Audit extends Context.Service<Audit, {
2358
3098
  * readonly write: Effect.Effect<void, "AuditError">
@@ -2367,7 +3107,7 @@ export declare const plan: <const States extends Machine.StateSchemas, const Eve
2367
3107
  * @category combinators
2368
3108
  * @since 4.0.0
2369
3109
  */
2370
- export declare const action: <E, R>(effect: Effect.Effect<void, E, R>) => Effect.Effect<void, never, ActionRequirement<E, R>>;
3110
+ export declare const action: Machine.Action;
2371
3111
  /**
2372
3112
  * Runs staged machine actions sequentially with the supplied runtime.
2373
3113
  *
@@ -2421,7 +3161,7 @@ export declare const runtime: <const Protocol extends Runtime.Protocol = {}>() =
2421
3161
  *
2422
3162
  * ```ts
2423
3163
  * import { Effect, Schema } from "effect"
2424
- * import { Machine } from "effect/unstable/machine"
3164
+ * import { Machine } from "@typeonce/effect-machine"
2425
3165
  *
2426
3166
  * class LoadFailed extends Schema.TaggedClass<LoadFailed>("LoadFailed")("LoadFailed", {
2427
3167
  * reason: Schema.String
@@ -2468,7 +3208,7 @@ export declare const effect: <Output, Error = never, Requirements = never>(effec
2468
3208
  * @category constructors
2469
3209
  * @since 4.0.0
2470
3210
  */
2471
- export declare const logic: <State, Event = any, Output = void, Error = never, Requirements = never, InitialError = never, InitialRequirements = never>(options: {
3211
+ export declare const logic: <State, Event = never, Output = void, Error = never, Requirements = never, InitialError = never, InitialRequirements = never>(options: {
2472
3212
  readonly initial: State | ((scope: Logic.Scope<Event>) => Effect.Effect<State, InitialError, InitialRequirements>);
2473
3213
  readonly run: (context: Logic.Context<State, Event>) => Effect.Effect<Output, Error, Requirements>;
2474
3214
  }) => Logic<State, Event, Error, Requirements | InitialRequirements, Output, InitialError>;
@@ -2492,21 +3232,25 @@ export declare const logic: <State, Event = any, Output = void, Error = never, R
2492
3232
  */
2493
3233
  export declare const transition: <State, Event, Error = never, Requirements = never>(initial: State, transition: (state: State, event: Event) => Effect.Effect<State, Error, Requirements>) => Logic<State, Event, Error, Requirements, never>;
2494
3234
  /**
2495
- * Creates a typed parent-local child address or complete machine descriptor.
3235
+ * Creates a typed descriptor for a complete child machine.
2496
3236
  *
2497
- * **When to use**
3237
+ * Descriptors identify a child by id and machine identity, so independently
3238
+ * constructed descriptors for the same pair address the same invoked child.
3239
+ *
3240
+ * @category constructors
3241
+ * @since 4.0.0
3242
+ */
3243
+ export declare const child: <const Id extends string, M extends Machine.Any>(id: Id, machine: M) => ChildMachine<Id, M>;
3244
+ /**
3245
+ * Creates a typed parent-local address for lower-level child process logic.
2498
3246
  *
2499
- * Use with a complete machine to create the descriptor shared by
2500
- * `invokeMachine`, `sendTo`, and child lookup APIs. The one-argument form
2501
- * creates an event-only address for lower-level process logic.
3247
+ * The default event protocol is `never`; provide an event type before using
3248
+ * the address with `spawn`, `invoke`, or `sendTo`.
2502
3249
  *
2503
3250
  * @category constructors
2504
3251
  * @since 4.0.0
2505
3252
  */
2506
- export declare const child: {
2507
- <const Id extends string, M extends Machine.Any>(id: Id, machine: M): ChildMachine<Id, M>;
2508
- <Event>(id: string): ChildAddress<Event>;
2509
- };
3253
+ export declare const childAddress: <Event = never>(id: string) => ChildAddress<Event>;
2510
3254
  /**
2511
3255
  * Spawns a child process owned by the currently running machine.
2512
3256
  *
@@ -2539,7 +3283,7 @@ export declare const spawn: {
2539
3283
  */
2540
3284
  export declare const sendTo: {
2541
3285
  <Child extends ChildMachine.Any>(child: Child, event: ChildMachine.Event<Child>): Effect.Effect<void, StoppedError, MachineRuntimeRequirement>;
2542
- <Address extends string>(id: Address, event: ChildAddress.Event<Address>): Effect.Effect<void, StoppedError, MachineRuntimeRequirement>;
3286
+ <Address extends ChildAddress<never>>(id: Address, event: ChildAddress.Event<Address>): Effect.Effect<void, StoppedError, MachineRuntimeRequirement>;
2543
3287
  };
2544
3288
  /**
2545
3289
  * Stops a named child process of the running machine.
@@ -2547,7 +3291,10 @@ export declare const sendTo: {
2547
3291
  * @category runtime
2548
3292
  * @since 4.0.0
2549
3293
  */
2550
- export declare const stopChild: (child: string | ChildMachine.Any) => Effect.Effect<void, never, MachineRuntimeRequirement>;
3294
+ export declare const stopChild: {
3295
+ <Event>(child: ChildAddress<Event>): Effect.Effect<void, never, MachineRuntimeRequirement>;
3296
+ <Child extends ChildMachine.Any>(child: Child): Effect.Effect<void, never, MachineRuntimeRequirement>;
3297
+ };
2551
3298
  /**
2552
3299
  * Returns a stream of terminal lifecycle outcomes for a running machine.
2553
3300
  *
@@ -2583,5 +3330,5 @@ export declare const watch: <State, Event, Error = never, Output = never>(ref: M
2583
3330
  * @category constructors
2584
3331
  * @since 4.0.0
2585
3332
  */
2586
- export declare const start: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>, ...args: [...Machine.InputArgs<Input>]) => Effect.Effect<MachineRef<Machine.Snapshot<States>, Machine.EventOf<Events>, E | InitialE | ActionError<R | InitialR> | InfiniteTransitionError | MachineSchemaDecodeError | StartupError | StoppedError, Output | undefined>, InitialE | ActionError<InitialR | R> | MachineSchemaDecodeError | StartupError | StoppedError, ExcludeCompatibleRuntime<ExecutionServices<InitialR | R>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>>;
3333
+ export declare const start: <const States extends Machine.StateSchemas, const Events extends ReadonlyArray<Machine.TaggedSchema>, const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [], const Input extends Schema.Top = typeof Schema.Void, UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>, E = never, R = never, InitialE = never, InitialR = never, FinalStates extends Machine.StateIdentifier<States> = never, Output = never, OutputStates extends Machine.StateIdentifier<States> = never, InputEvents extends ReadonlyArray<Machine.TaggedSchema> = Events>(machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits, OutputStates, InputEvents> & Machine.EnsureOutputImplementations<States, OutputStates> & Machine.EnsureHistoryImplementations<States, UnhandledStates>, ...args: [...Machine.InputArgs<Input>]) => Effect.Effect<MachineRef<Machine.Snapshot<States>, Machine.EventOf<InputEvents>, E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError, Output>, InitialE | E | ActionError<InitialR | R> | InfiniteTransitionError | MachineSchemaDecodeError | StartupError | StoppedError, ExcludeCompatibleRuntime<ExecutionServices<InitialR | R>, Machine.EventOf<Events>, Machine.EmitOf<Emits>>>;
2587
3334
  //# sourceMappingURL=Machine.d.ts.map