@typeonce/effect-machine 0.41.0 → 0.42.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/README.md +33 -6
  2. package/dist/Machine.d.ts +159 -27
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +20 -6
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/atom.d.ts +2 -7
  7. package/dist/internal/machine/atom.d.ts.map +1 -1
  8. package/dist/internal/machine/atom.js +132 -87
  9. package/dist/internal/machine/atom.js.map +1 -1
  10. package/dist/internal/machine/childRegistry.d.ts +7 -0
  11. package/dist/internal/machine/childRegistry.d.ts.map +1 -1
  12. package/dist/internal/machine/childRegistry.js +7 -0
  13. package/dist/internal/machine/childRegistry.js.map +1 -1
  14. package/dist/internal/machine/invocation.d.ts.map +1 -1
  15. package/dist/internal/machine/invocation.js +13 -2
  16. package/dist/internal/machine/invocation.js.map +1 -1
  17. package/dist/internal/machine/invocationDefinition.d.ts +7 -0
  18. package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
  19. package/dist/internal/machine/invocationDefinition.js +16 -0
  20. package/dist/internal/machine/invocationDefinition.js.map +1 -1
  21. package/dist/internal/machine/machine.d.ts.map +1 -1
  22. package/dist/internal/machine/machine.js +69 -6
  23. package/dist/internal/machine/machine.js.map +1 -1
  24. package/dist/internal/machine/plannedSnapshot.d.ts +30 -0
  25. package/dist/internal/machine/plannedSnapshot.d.ts.map +1 -0
  26. package/dist/internal/machine/plannedSnapshot.js +103 -0
  27. package/dist/internal/machine/plannedSnapshot.js.map +1 -0
  28. package/dist/internal/machine/planner.d.ts +5 -0
  29. package/dist/internal/machine/planner.d.ts.map +1 -1
  30. package/dist/internal/machine/planner.js +1 -1
  31. package/dist/internal/machine/planner.js.map +1 -1
  32. package/dist/internal/machine/runtime.d.ts +5 -4
  33. package/dist/internal/machine/runtime.d.ts.map +1 -1
  34. package/dist/internal/machine/runtime.js +60 -15
  35. package/dist/internal/machine/runtime.js.map +1 -1
  36. package/dist/internal/machine/runtimeCompiled.js +2 -2
  37. package/dist/internal/machine/runtimeCompiled.js.map +1 -1
  38. package/dist/internal/machine/runtimeGeneric.js +1 -1
  39. package/dist/internal/machine/runtimeGeneric.js.map +1 -1
  40. package/dist/internal/machine/runtimeProtocol.d.ts +2 -1
  41. package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
  42. package/dist/internal/machine/runtimeProtocol.js +2 -2
  43. package/dist/internal/machine/runtimeProtocol.js.map +1 -1
  44. package/dist/testing/MachineTest.d.ts +2 -2
  45. package/dist/testing/MachineTest.js +2 -2
  46. package/dist/unstable/reactivity/AtomMachine.d.ts +82 -19
  47. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  48. package/dist/unstable/reactivity/AtomMachine.js +0 -7
  49. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  50. package/docs/agent-guide.md +28 -5
  51. package/docs/effect-atom-react.md +207 -11
  52. package/docs/machine-review.md +18 -4
  53. package/package.json +4 -4
  54. package/src/Machine.ts +229 -32
  55. package/src/internal/machine/atom.ts +263 -113
  56. package/src/internal/machine/childRegistry.ts +9 -0
  57. package/src/internal/machine/invocation.ts +17 -2
  58. package/src/internal/machine/invocationDefinition.ts +19 -0
  59. package/src/internal/machine/machine.ts +65 -2
  60. package/src/internal/machine/plannedSnapshot.ts +113 -0
  61. package/src/internal/machine/planner.ts +1 -1
  62. package/src/internal/machine/runtime.ts +101 -28
  63. package/src/internal/machine/runtimeCompiled.ts +2 -2
  64. package/src/internal/machine/runtimeGeneric.ts +1 -1
  65. package/src/internal/machine/runtimeProtocol.ts +4 -2
  66. package/src/testing/MachineTest.ts +2 -2
  67. package/src/unstable/reactivity/AtomMachine.ts +90 -20
package/src/Machine.ts CHANGED
@@ -674,6 +674,30 @@ type StateDefinitionError<
674
674
  readonly details: Details
675
675
  }
676
676
 
677
+ type InvocationDefinitionError<
678
+ Message extends string,
679
+ Source extends string,
680
+ Field extends PropertyKey
681
+ > = {
682
+ readonly "~effect/Machine/InvocationDefinitionError": Message
683
+ readonly source: Source
684
+ readonly field: Field
685
+ }
686
+
687
+ type TransitionDefinitionError<
688
+ Message extends string,
689
+ Choice extends string,
690
+ Target extends string
691
+ > = {
692
+ readonly "~effect/Machine/TransitionDefinitionError": Message
693
+ readonly choice: Choice
694
+ readonly target: Target
695
+ }
696
+
697
+ type RootCompatibilityError<Message extends string> = {
698
+ readonly "~effect/Machine/RootCompatibilityError": Message
699
+ }
700
+
677
701
  type ActiveStateKey<States extends Machine.StateSchemas> = Machine.ActiveStateKey<States>
678
702
 
679
703
  type HistoryStateKey<States extends Machine.StateSchemas> = Machine.HistoryStateKey<States>
@@ -1997,7 +2021,8 @@ export type RuntimeOutcome<State, Error = never, Output = never> =
1997
2021
  *
1998
2022
  * Subscribe to `emissions` before evaluating `start` when initial-entry
1999
2023
  * emissions must be observed. `start` is one-shot: concurrent and repeated
2000
- * evaluations share the same initialization and running reference.
2024
+ * evaluations share the same initialization and running reference, and the
2025
+ * `Scope` of the first evaluation owns the machine.
2001
2026
  *
2002
2027
  * @category models
2003
2028
  * @since 0.11.0
@@ -2023,7 +2048,7 @@ export interface Prepared<out State, in Event, out Error, out Output, out Emitte
2023
2048
  */
2024
2049
  readonly inspection: Stream.Stream<Inspection.Event>
2025
2050
 
2026
- /** Initializes this machine once and returns its running reference. */
2051
+ /** Initializes this machine once, owned by the first evaluating `Scope`, and returns its running reference. */
2027
2052
  readonly start: Effect.Effect<MachineRef<State, Event, Error, Output, Emitted>, StartError, StartRequirements>
2028
2053
  }
2029
2054
 
@@ -2579,9 +2604,23 @@ export declare namespace Machine {
2579
2604
  infer Mode ? Mode extends ParentMode ? Parent<Mode, Events> : undefined
2580
2605
  : never
2581
2606
 
2582
- /** Constraint applied to APIs that create an independent root runtime. */
2607
+ /**
2608
+ * Constraint applied to APIs that create an independent root runtime.
2609
+ *
2610
+ * **Details**
2611
+ *
2612
+ * A machine without a parent protocol, or with an optional one, is
2613
+ * accepted. A machine that declares a required parent can only run as an
2614
+ * owned child, so the constraint resolves to a diagnostic that names the
2615
+ * reason instead of an unexplained `never`.
2616
+ *
2617
+ * @category utility types
2618
+ * @since 0.27.0
2619
+ */
2583
2620
  export type RootCompatible<Events extends ReadonlyArray<TaggedSchema>> = ParentModeOf<Events> extends "required"
2584
- ? never
2621
+ ? RootCompatibilityError<
2622
+ "Machine declares a required parent and cannot run as a root; invoke or spawn it from an owning machine"
2623
+ >
2585
2624
  : unknown
2586
2625
 
2587
2626
  /**
@@ -5055,7 +5094,7 @@ export declare namespace Machine {
5055
5094
  | ChoiceTarget<States, ChoiceIdentifier<States>>
5056
5095
  | StateUpdate<States, ValuedStateIdentifier<States>>
5057
5096
  | CombinedTarget<
5058
- Target<States, StateIdentifier<States>> | Snapshot<States>,
5097
+ Target<States, StateIdentifier<States>> | Snapshot<States> | ChoiceTarget<States, ChoiceIdentifier<States>>,
5059
5098
  States,
5060
5099
  ValuedStateIdentifier<States>
5061
5100
  >
@@ -5291,15 +5330,30 @@ export declare namespace Machine {
5291
5330
  : Effect.Error<InvokeOutcomeReturn<Invoke>>
5292
5331
  type InvokeOutcomeServices<Invoke> = IsAny<InvokeOutcomeReturn<Invoke>> extends true ? never
5293
5332
  : Effect.Services<InvokeOutcomeReturn<Invoke>>
5333
+ // Effect, Stream, and timer invocations run under a runtime-reserved child id
5334
+ // derived from their state path and distinct lifecycle id. Only caller-chosen
5335
+ // child ids (logic addresses and child descriptor ids) can collide.
5336
+ type InvokeCollisionError<Invoke> = Invoke extends { readonly logic: unknown } | { readonly child: unknown }
5337
+ ? ChildAlreadyExistsError
5338
+ : never
5294
5339
  /**
5295
5340
  * Extracts the parent transition error contribution from invoked children.
5296
5341
  *
5342
+ * **Details**
5343
+ *
5344
+ * The contribution contains the startup and lifecycle-handler failures of
5345
+ * every invocation. `ChildAlreadyExistsError` is included only for
5346
+ * invocations whose child id is chosen by the caller, namely logic
5347
+ * invocations with an `address` and child machine invocations, which fail to
5348
+ * start while another child with that id is active. Effect, Stream, and timer
5349
+ * invocations run under runtime-reserved ids and cannot collide.
5350
+ *
5297
5351
  * @category utility types
5298
5352
  * @since 0.4.0
5299
5353
  */
5300
5354
  export type InvokeError<Config> = [InvokeReturn<Config>] extends [never] ? never
5301
5355
  :
5302
- | ChildAlreadyExistsError
5356
+ | InvokeCollisionError<InvokeReturn<Config>>
5303
5357
  | InvokeInitialError<InvokeReturn<Config>>
5304
5358
  | InvokeOutcomeError<InvokeReturn<Config>>
5305
5359
  /**
@@ -6401,7 +6455,10 @@ export declare namespace Machine {
6401
6455
  | {
6402
6456
  /** Selects a declared descendant; its transition or bound branch constructor supplies required values. */
6403
6457
  readonly target: ReferenceUnion<S, DestinationPath<S>>
6404
- /** Replaces the complete value of a retained source or ancestor without replacing its active children. */
6458
+ /**
6459
+ * Replaces the complete value of a retained ancestor of the source and target without replacing its active
6460
+ * children. A choice target resolves after the update, and every branch it can reach must stay below the owner.
6461
+ */
6405
6462
  readonly update?: ReferenceUnion<S, ValuedStateIdentifier<S>>
6406
6463
  /** Enters a compound or parallel subtree through its declared initialization. */
6407
6464
  readonly initial?: never
@@ -6576,6 +6633,14 @@ export declare namespace Machine {
6576
6633
  P,
6577
6634
  "state",
6578
6635
  "branch"
6636
+ >
6637
+ : P extends ChoiceIdentifier<S> ? TargetSelection<
6638
+ (construction: {
6639
+ readonly update: ConstructionData<NodeByIdentifier<S, Owner>>
6640
+ }) => CombinedTarget<ChoiceTarget<S, P>, S, Owner>,
6641
+ P,
6642
+ "choice",
6643
+ "branch"
6579
6644
  > :
6580
6645
  never
6581
6646
  : never
@@ -6693,6 +6758,7 @@ export declare namespace Machine {
6693
6758
  ) => { readonly target: NodeValue<NodeByIdentifier<S, P>>; readonly update: StateByIdentifier<S, Owner> }
6694
6759
  readonly from?: never
6695
6760
  })
6761
+ : P extends ChoiceIdentifier<S> ? ObjectConstruction<C, StateUpdateBuilder<S, Owner>>
6696
6762
  : never
6697
6763
  type InlineCombined<S extends StateSchemas, Src extends StateNodeIdentifier<S>, C> = Src extends ChoiceIdentifier<S> ?
6698
6764
  never :
@@ -6703,7 +6769,11 @@ export declare namespace Machine {
6703
6769
  & {
6704
6770
  /** Selects a declared descendant; its transition or bound branch constructor supplies required values. */
6705
6771
  readonly target: ReferenceAt<S, P>
6706
- /** Replaces the complete value of a retained source or ancestor without replacing its active children. */
6772
+ /**
6773
+ * Replaces the complete value of a retained ancestor of the source and target without replacing its active
6774
+ * children. A choice target resolves after the update, and every branch it can reach must stay below the
6775
+ * owner; `data` then constructs only the owner value.
6776
+ */
6707
6777
  readonly update: ReferenceAt<S, Owner>
6708
6778
  /** Enters a compound or parallel subtree through its declared initialization. */
6709
6779
  readonly initial?: never
@@ -7413,27 +7483,137 @@ export declare namespace Machine {
7413
7483
  : "onElement"
7414
7484
  : never)
7415
7485
  | (SourceKind<R, K> extends "logic" | "children" ? "onSnapshot" : never)
7416
- type ValidateObjectInvoke<I, R> = I extends ReadonlyArray<unknown> ?
7417
- { readonly [K in keyof I]: ValidateObjectInvoke<I[K], R> } :
7418
- I extends {
7419
- /** Name of the source registered in make. */
7420
- readonly src: infer Src extends PropertyKey
7421
- } ? {
7422
- readonly [K in keyof I]: K extends AllowedInvokeChannels<R, Src> ? ValidateObjectTransition<I[K]>
7423
- : K extends "src" | "id" | "address" | "input" ? unknown
7424
- : never
7425
- } :
7486
+ type UnreachableInvokeChannelMessage<Channel extends string, Src extends string> = Channel extends "onDone"
7487
+ ? `Invocation '${Src}' never completes, so onDone is unreachable`
7488
+ : Channel extends "onFailure" ? `Invocation '${Src}' cannot fail, so onFailure is unreachable`
7489
+ : Channel extends "onElement" ? `Invocation '${Src}' emits no Stream elements, so onElement is unreachable`
7490
+ : `Invocation '${Src}' has no snapshots; onSnapshot applies only to logic and child invocations`
7491
+ type InvokeFieldError<K, Src extends string> = K extends "onDone" | "onFailure" | "onElement" | "onSnapshot"
7492
+ ? InvocationDefinitionError<UnreachableInvokeChannelMessage<K, Src>, Src, K>
7493
+ : K extends string ? InvocationDefinitionError<`Invocation '${Src}' does not accept the field '${K}'`, Src, K>
7494
+ : never
7495
+ // Explicit ids or the registered source name for programs; descriptor ids for child machines.
7496
+ type InvokeLifecycleIdOf<I, R> = I extends { readonly id: infer Id extends string } ? Id
7497
+ : I extends { readonly src: infer Src extends PropertyKey } ?
7498
+ SourceValue<R, Src> extends ChildMachine<infer Id, Any> ? Id : Extract<Src, string>
7499
+ : never
7500
+ type LiteralInvokeIdAt<I extends ReadonlyArray<unknown>, K, R> = K extends keyof I
7501
+ ? InvokeLifecycleIdOf<I[K], R> extends infer Id extends string ? string extends Id ? never : Id : never
7502
+ : never
7503
+ type DuplicateInvokeId<I extends ReadonlyArray<unknown>, K, R> = LiteralInvokeIdAt<I, K, R> extends infer Id ?
7504
+ [Id] extends [never] ? never
7505
+ : Id extends LiteralInvokeIdAt<I, Exclude<Extract<keyof I, `${number}`>, K>, R> ? Id
7506
+ : never
7507
+ : never
7508
+ type ValidateInvokeEntry<I, R, Duplicate extends string, Root> = I extends {
7509
+ /** Name of the source registered in make. */
7510
+ readonly src: infer Src extends PropertyKey
7511
+ } ? {
7512
+ readonly [K in keyof I]: [Duplicate] extends [never] ? ValidateInvokeField<I, K, R, Extract<Src, string>, Root>
7513
+ : K extends ("id" extends keyof I ? "id" : "src") ? InvocationDefinitionError<
7514
+ `Invocation id '${Duplicate}' is declared more than once in this state`,
7515
+ Extract<Src, string>,
7516
+ K
7517
+ >
7518
+ : ValidateInvokeField<I, K, R, Extract<Src, string>, Root>
7519
+ } :
7426
7520
  never
7521
+ type ValidateInvokeField<I, K extends keyof I, R, Src extends string, Root> = K extends
7522
+ AllowedInvokeChannels<R, Src> ? ValidateObjectTransition<I[K]> & ValidateChoiceUpdate<I[K], R, Root>
7523
+ : K extends "src" | "id" | "address" | "input" ? unknown
7524
+ : InvokeFieldError<K, Src>
7525
+ type ValidateObjectInvoke<I, R, Root> = I extends ReadonlyArray<unknown> ?
7526
+ number extends I["length"] ? { readonly [K in keyof I]: ValidateInvokeEntry<I[K], R, never, Root> }
7527
+ : { readonly [K in keyof I]: ValidateInvokeEntry<I[K], R, DuplicateInvokeId<I, K, R>, Root> } :
7528
+ ValidateInvokeEntry<I, R, never, Root>
7529
+ // Handler config of a dotted state path, read from the root handler config.
7530
+ type HandlerConfigAt<Config, Path extends string> = Config extends { readonly states: infer Children } ?
7531
+ Path extends `${infer Head}.${infer Rest}` ? Head extends keyof Children ? HandlerConfigAt<Children[Head], Rest>
7532
+ : never
7533
+ : Path extends keyof Children ? Children[Path]
7534
+ : never
7535
+ : never
7536
+ type ChoiceConfigAt<Root, Path extends string> = HandlerConfigAt<Root, Path> extends {
7537
+ /** Implements this choice pseudo-state with one required synchronous transition. */
7538
+ readonly choice: infer Choice
7539
+ } ? Choice
7540
+ : never
7541
+ type ChoiceBranchDeclaration<Choice, R> = Choice extends {
7542
+ /** Named groups of inspectable destinations used by transition resolvers. */
7543
+ readonly branches: infer G extends keyof Registered<R, "branches">
7544
+ } ? {
7545
+ readonly [K in Extract<keyof Registered<R, "branches">[G], string>]: {
7546
+ readonly label: `branch '${K}'`
7547
+ readonly declaration: Registered<R, "branches">[G][K]
7548
+ }
7549
+ }[Extract<keyof Registered<R, "branches">[G], string>]
7550
+ : { readonly label: "transition"; readonly declaration: Choice }
7551
+ type IsBelowOwner<Path extends string, Owner extends string> = Owner extends "" ? Path extends "" ? false : true
7552
+ : Path extends `${Owner}.${string}` ? true
7553
+ : false
7554
+ type ChoiceBranchEscape<Path extends string, Owner extends string, Choice extends string, Label extends string> =
7555
+ IsBelowOwner<Path, Owner> extends true ? never : TransitionDefinitionError<
7556
+ `Choice '${Choice}' ${Label} targets '${PathName<Path>}' outside the updated owner '${PathName<Owner>}'`,
7557
+ Choice,
7558
+ PathName<Path>
7559
+ >
7560
+ // Every branch reachable from a choice entered with an owner update must stay below that owner.
7561
+ type ChoiceUpdateEscape<Root, R, Choice extends string, Owner extends string, Visited extends string = never> =
7562
+ Choice extends Visited ? never
7563
+ : ChoiceBranchDeclaration<ChoiceConfigAt<Root, Choice>, R> extends infer Branch ?
7564
+ Branch extends { readonly label: infer Label extends string; readonly declaration: infer D } ? D extends {
7565
+ /** Selects a declared descendant; its transition or bound branch constructor supplies required values. */
7566
+ readonly target: infer Ref extends string
7567
+ } ?
7568
+ [ChoiceConfigAt<Root, PathOf<Ref>>] extends [never] ? ChoiceBranchEscape<PathOf<Ref>, Owner, Choice, Label>
7569
+ : ChoiceUpdateEscape<Root, R, PathOf<Ref>, Owner, Visited | Choice>
7570
+ : D extends {
7571
+ /** Restores the declared history reference, using its fallback on first entry. */
7572
+ readonly history: infer Ref extends string
7573
+ } ? ChoiceBranchEscape<PathOf<Ref>, Owner, Choice, Label>
7574
+ : D extends {
7575
+ /** Reconstructs root and its initial children from fresh machine input. */
7576
+ readonly initialize: unknown
7577
+ } ? ChoiceBranchEscape<"", Owner, Choice, Label>
7578
+ : never
7579
+ : never
7580
+ : never
7581
+ type ChoiceUpdateEscapeOf<D, R, Root> = D extends {
7582
+ /** Selects a declared descendant; its transition or bound branch constructor supplies required values. */
7583
+ readonly target: infer Ref extends string
7584
+ /** Replaces the complete value of a retained source or ancestor without replacing its active children. */
7585
+ readonly update: infer Owner extends string
7586
+ } ? [ChoiceConfigAt<Root, PathOf<Ref>>] extends [never] ? never
7587
+ : ChoiceUpdateEscape<Root, R, PathOf<Ref>, PathOf<Owner>>
7588
+ : never
7589
+ type ValidateChoiceUpdate<T, R, Root> = T extends {
7590
+ /** Named groups of inspectable destinations used by transition resolvers. */
7591
+ readonly branches: infer G extends keyof Registered<R, "branches">
7592
+ } ? {
7593
+ readonly [K in keyof Registered<R, "branches">[G]]: ChoiceUpdateEscapeOf<Registered<R, "branches">[G][K], R, Root>
7594
+ }[keyof Registered<R, "branches">[G]] extends infer Escape ? [Escape] extends [never] ? unknown : {
7595
+ /** Named groups of inspectable destinations used by transition resolvers. */
7596
+ readonly branches: Escape
7597
+ }
7598
+ : never
7599
+ : ChoiceUpdateEscapeOf<T, R, Root> extends infer Escape ? [Escape] extends [never] ? unknown : {
7600
+ /** Replaces the complete value of a retained source or ancestor without replacing its active children. */
7601
+ readonly update: Escape
7602
+ }
7603
+ : never
7427
7604
  type ObjectDeclarations<C> =
7428
7605
  | Extract<keyof C, "on" | "always" | "onDone" | "choice" | "invoke">
7429
7606
  | (C extends { readonly states: infer Children }
7430
7607
  ? { readonly [K in keyof Children]: ObjectDeclarations<Children[K]> }[keyof Children]
7431
7608
  : never)
7432
- type ValidateObjectConfig<C, R> = [ObjectDeclarations<C>] extends [never] ? unknown : {
7433
- readonly [K in keyof C]: K extends "states" ? { readonly [P in keyof C[K]]: ValidateObjectConfig<C[K][P], R> }
7434
- : K extends "on" ? { readonly [P in keyof C[K]]: ValidateObjectTransition<C[K][P]> }
7435
- : K extends "always" | "onDone" | "choice" ? ValidateObjectTransition<C[K]>
7436
- : K extends "invoke" ? ValidateObjectInvoke<C[K], R>
7609
+ type ValidateObjectConfig<C, R, Root = C> = [ObjectDeclarations<C>] extends [never] ? unknown : {
7610
+ readonly [K in keyof C]: K extends "states" ? { readonly [P in keyof C[K]]: ValidateObjectConfig<C[K][P], R, Root> }
7611
+ : K extends "on" ? {
7612
+ readonly [P in keyof C[K]]: ValidateObjectTransition<C[K][P]> & ValidateChoiceUpdate<C[K][P], R, Root>
7613
+ }
7614
+ : K extends "always" | "onDone" ? ValidateObjectTransition<C[K]> & ValidateChoiceUpdate<C[K], R, Root>
7615
+ : K extends "choice" ? ValidateObjectTransition<C[K]>
7616
+ : K extends "invoke" ? ValidateObjectInvoke<C[K], R, Root>
7437
7617
  : unknown
7438
7618
  }
7439
7619
  type NormalizeObjectInvoke<I, R> = I extends ReadonlyArray<unknown> ?
@@ -9203,6 +9383,9 @@ export const waitFor: {
9203
9383
  * `prepared.start`. Ordinary callers can continue to use {@link start}, which
9204
9384
  * starts directly and does not allocate the prepared lifecycle boundary.
9205
9385
  *
9386
+ * `prepared.start` is one-shot and scoped: the `Scope` that first evaluates it
9387
+ * owns the machine, and closing that `Scope` is equivalent to `ref.stop`.
9388
+ *
9206
9389
  * ```ts
9207
9390
  * const prepared = yield* Machine.prepare(machine)
9208
9391
  *
@@ -9271,7 +9454,8 @@ export const prepare: <
9271
9454
  | MachineSchemaDecodeError
9272
9455
  | StartupError
9273
9456
  | StoppedError,
9274
- ExcludeCompatibleRuntime<
9457
+ | Scope.Scope
9458
+ | ExcludeCompatibleRuntime<
9275
9459
  ExecutionServices<InitialR | R>,
9276
9460
  Machine.EventOf<Events>,
9277
9461
  Machine.EmittedEventOf<Emits>
@@ -9280,7 +9464,7 @@ export const prepare: <
9280
9464
  > = internal.prepare
9281
9465
 
9282
9466
  /**
9283
- * Starts a machine.
9467
+ * Starts a machine owned by the current `Scope`.
9284
9468
  *
9285
9469
  * **When to use**
9286
9470
  *
@@ -9294,6 +9478,13 @@ export const prepare: <
9294
9478
  * publishes the new state, delivers emitted events, and then starts invokes
9295
9479
  * for entered states.
9296
9480
  *
9481
+ * The machine is a scoped resource, like `Effect.forkScoped`. Closing the
9482
+ * `Scope` is equivalent to `ref.stop`: active invocations are interrupted,
9483
+ * owned children stop, the snapshot becomes `stopped`, and later sends fail
9484
+ * with `StoppedError`. An earlier `ref.stop` or terminal completion leaves the
9485
+ * finalizer with nothing to do. Own a `Scope` explicitly, for example with
9486
+ * `Scope.make` and `Scope.provide`, when the machine must outlive the caller.
9487
+ *
9297
9488
  * **Gotchas**
9298
9489
  *
9299
9490
  * The returned handle's `send` operation only enqueues events. Transition
@@ -9320,10 +9511,10 @@ export const prepare: <
9320
9511
  * Idle: {}
9321
9512
  * }
9322
9513
  * })
9323
- * const state = Effect.gen(function*() {
9514
+ * const state = Effect.scoped(Effect.gen(function*() {
9324
9515
  * const ref = yield* Machine.start(machine)
9325
9516
  * return yield* ref.state
9326
- * })
9517
+ * }))
9327
9518
  * ```
9328
9519
  *
9329
9520
  * @see {@link plan} for inspecting the same transition plan without executing it.
@@ -9386,7 +9577,8 @@ export const start: <
9386
9577
  | MachineSchemaDecodeError
9387
9578
  | StartupError
9388
9579
  | StoppedError,
9389
- ExcludeCompatibleRuntime<
9580
+ | Scope.Scope
9581
+ | ExcludeCompatibleRuntime<
9390
9582
  ExecutionServices<InitialR | R>,
9391
9583
  Machine.EventOf<Events>,
9392
9584
  Machine.EmittedEventOf<Emits>
@@ -9394,10 +9586,14 @@ export const start: <
9394
9586
  > = internal.start
9395
9587
 
9396
9588
  /**
9397
- * Starts a fresh managed runtime from a decoded logical snapshot.
9589
+ * Starts a fresh managed runtime from a decoded logical snapshot, owned by the
9590
+ * current `Scope`.
9398
9591
  *
9399
9592
  * **Details**
9400
9593
  *
9594
+ * The resumed machine has the same scoped lifetime as {@link start}: closing
9595
+ * the `Scope` is equivalent to `ref.stop`.
9596
+ *
9401
9597
  * `resume` validates and normalizes the supplied snapshot before publishing it
9402
9598
  * as the first state. It does not call the machine's initial function, replay
9403
9599
  * entry or transition actions, re-deliver raised or emitted events, or
@@ -9436,12 +9632,12 @@ export const start: <
9436
9632
  * Idle: {}
9437
9633
  * }
9438
9634
  * })
9439
- * const resumed = Effect.gen(function*() {
9635
+ * const resumed = Effect.scoped(Effect.gen(function*() {
9440
9636
  * const initial = yield* Machine.planInitial(machine)
9441
9637
  * const encoded = yield* Machine.encodeSnapshot(machine, initial.state)
9442
9638
  * const snapshot = yield* Machine.decodeSnapshot(machine, encoded)
9443
9639
  * return yield* Machine.resume(machine, snapshot)
9444
- * })
9640
+ * }))
9445
9641
  * ```
9446
9642
  *
9447
9643
  * @see {@link decodeSnapshot} for the schema and transport boundary.
@@ -9498,7 +9694,8 @@ export const resume: <
9498
9694
  Machine.EmittedEventOf<Emits>
9499
9695
  >,
9500
9696
  MachineSchemaDecodeError,
9501
- ExcludeCompatibleRuntime<
9697
+ | Scope.Scope
9698
+ | ExcludeCompatibleRuntime<
9502
9699
  ExecutionServices<R>,
9503
9700
  Machine.EventOf<Events>,
9504
9701
  Machine.EmittedEventOf<Emits>