@typeonce/effect-machine 0.35.0 → 0.37.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 (72) hide show
  1. package/README.md +45 -7
  2. package/dist/Machine.d.ts +143 -136
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +25 -0
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/command.d.ts +0 -1
  7. package/dist/internal/machine/command.d.ts.map +1 -1
  8. package/dist/internal/machine/command.js +0 -14
  9. package/dist/internal/machine/command.js.map +1 -1
  10. package/dist/internal/machine/configuration.d.ts.map +1 -1
  11. package/dist/internal/machine/configuration.js +15 -2
  12. package/dist/internal/machine/configuration.js.map +1 -1
  13. package/dist/internal/machine/construction.d.ts +9 -0
  14. package/dist/internal/machine/construction.d.ts.map +1 -0
  15. package/dist/internal/machine/construction.js +132 -0
  16. package/dist/internal/machine/construction.js.map +1 -0
  17. package/dist/internal/machine/declaration.d.ts +1 -0
  18. package/dist/internal/machine/declaration.d.ts.map +1 -1
  19. package/dist/internal/machine/declaration.js +4 -8
  20. package/dist/internal/machine/declaration.js.map +1 -1
  21. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  22. package/dist/internal/machine/executionPlan.js +13 -9
  23. package/dist/internal/machine/executionPlan.js.map +1 -1
  24. package/dist/internal/machine/implementation.d.ts +2 -3
  25. package/dist/internal/machine/implementation.d.ts.map +1 -1
  26. package/dist/internal/machine/implementation.js.map +1 -1
  27. package/dist/internal/machine/invocation.d.ts +1 -0
  28. package/dist/internal/machine/invocation.d.ts.map +1 -1
  29. package/dist/internal/machine/invocation.js +26 -3
  30. package/dist/internal/machine/invocation.js.map +1 -1
  31. package/dist/internal/machine/invocationDefinition.d.ts +3 -1
  32. package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
  33. package/dist/internal/machine/invocationDefinition.js.map +1 -1
  34. package/dist/internal/machine/machine.d.ts +6 -0
  35. package/dist/internal/machine/machine.d.ts.map +1 -1
  36. package/dist/internal/machine/machine.js +70 -168
  37. package/dist/internal/machine/machine.js.map +1 -1
  38. package/dist/internal/machine/observation.d.ts +3 -0
  39. package/dist/internal/machine/observation.d.ts.map +1 -0
  40. package/dist/internal/machine/observation.js +28 -0
  41. package/dist/internal/machine/observation.js.map +1 -0
  42. package/dist/internal/machine/planner.d.ts.map +1 -1
  43. package/dist/internal/machine/planner.js +63 -17
  44. package/dist/internal/machine/planner.js.map +1 -1
  45. package/dist/internal/machine/topology.d.ts +6 -0
  46. package/dist/internal/machine/topology.d.ts.map +1 -1
  47. package/dist/internal/machine/topology.js.map +1 -1
  48. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  49. package/dist/internal/testing/machine/finiteModel.js +36 -76
  50. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  51. package/docs/agent-guide.md +57 -34
  52. package/docs/root-api.md +143 -190
  53. package/package.json +1 -1
  54. package/src/Machine.ts +223 -144
  55. package/src/internal/machine/command.ts +0 -16
  56. package/src/internal/machine/configuration.ts +13 -10
  57. package/src/internal/machine/construction.ts +147 -0
  58. package/src/internal/machine/declaration.ts +5 -7
  59. package/src/internal/machine/executionPlan.ts +13 -9
  60. package/src/internal/machine/implementation.ts +2 -3
  61. package/src/internal/machine/invocation.ts +34 -3
  62. package/src/internal/machine/invocationDefinition.ts +1 -0
  63. package/src/internal/machine/machine.ts +89 -223
  64. package/src/internal/machine/observation.ts +39 -0
  65. package/src/internal/machine/planner.ts +69 -18
  66. package/src/internal/machine/topology.ts +4 -0
  67. package/src/internal/testing/machine/finiteModel.ts +44 -122
  68. package/dist/internal/machine/targetBuilder.d.ts +0 -16
  69. package/dist/internal/machine/targetBuilder.d.ts.map +0 -1
  70. package/dist/internal/machine/targetBuilder.js +0 -333
  71. package/dist/internal/machine/targetBuilder.js.map +0 -1
  72. package/src/internal/machine/targetBuilder.ts +0 -525
package/src/Machine.ts CHANGED
@@ -242,11 +242,6 @@ export interface Machine<
242
242
  /** @internal */
243
243
  readonly stateNodes: Machine.StateNodes
244
244
 
245
- /** @internal */
246
- readonly makeTargetBuilder: <Source extends Machine.StateNodeIdentifier<States>>(
247
- source: Source
248
- ) => Machine.TargetBuilder<States, Source>
249
-
250
245
  /** @internal */
251
246
  readonly handlers: Readonly<Record<string, unknown>>
252
247
 
@@ -300,7 +295,7 @@ export interface Definition<
300
295
  InputEvents,
301
296
  ParentEvents
302
297
  >,
303
- TypeId | "stateNodes" | "makeTargetBuilder" | "handlers" | "initial" | "initialDefinition"
298
+ TypeId | "stateNodes" | "handlers" | "initial" | "initialDefinition"
304
299
  >
305
300
  {
306
301
  /**
@@ -355,10 +350,7 @@ export declare namespace Definition {
355
350
  * @category models
356
351
  * @since 0.15.0
357
352
  */
358
- export interface Any
359
- extends
360
- Omit<Machine.Any, TypeId | "stateNodes" | "makeTargetBuilder" | "handlers" | "initial" | "initialDefinition">
361
- {
353
+ export interface Any extends Omit<Machine.Any, TypeId | "stateNodes" | "handlers" | "initial" | "initialDefinition"> {
362
354
  readonly handle: any
363
355
  }
364
356
  }
@@ -2621,7 +2613,6 @@ export declare namespace Machine {
2621
2613
  /** @internal */
2622
2614
  readonly stateNodes: StateNodes
2623
2615
  /** @internal */
2624
- readonly makeTargetBuilder: any
2625
2616
  /** @internal */
2626
2617
  readonly handlers: any
2627
2618
  /** @internal */
@@ -4534,12 +4525,9 @@ export declare namespace Machine {
4534
4525
  export type HistoryDefaultTargetBuilder<
4535
4526
  States extends StateSchemas,
4536
4527
  Owner extends StateIdentifier<States>
4537
- > = HistorySnapshotMethod<
4538
- { readonly "": Extract<Omit<States[""], "~effect/Machine/ExplicitInitial">, StateNodeConfig> },
4539
- "" & ActiveStateKey<{ readonly "": Extract<Omit<States[""], "~effect/Machine/ExplicitInitial">, StateNodeConfig> }>,
4540
- "",
4541
- Owner
4542
- >
4528
+ > = (
4529
+ construction: HistoryConstruction<States[""], Owner>
4530
+ ) => StateConstruction<CompleteSnapshotContaining<States, Owner>>
4543
4531
 
4544
4532
  /**
4545
4533
  * Builder for source-local transition targets.
@@ -5708,6 +5696,32 @@ export declare namespace Machine {
5708
5696
  readonly output?: never
5709
5697
  }
5710
5698
 
5699
+ type HistoryConstruction<Node, Owner extends string> =
5700
+ & ConstructionData<Node>
5701
+ & (Node extends {
5702
+ readonly states: infer Children extends StateSchemas
5703
+ } ? {
5704
+ readonly states: Node extends { readonly type: "parallel" } ? {
5705
+ readonly [K in (ActiveStateKey<Children> | ChoiceStateKey<Children>)]: HistoryConstruction<
5706
+ Children[K],
5707
+ Owner extends `${K}.${infer Rest}` ? Rest : ""
5708
+ >
5709
+ }
5710
+ : {
5711
+ readonly [K in (ActiveStateKey<Children> | ChoiceStateKey<Children>)]: Owner extends
5712
+ "" | K | `${K}.${string}` ?
5713
+ & {
5714
+ readonly [P in K]: HistoryConstruction<
5715
+ Children[P],
5716
+ Owner extends `${K}.${infer Rest}` ? Rest : ""
5717
+ >
5718
+ }
5719
+ & { readonly [P in Exclude<(ActiveStateKey<Children> | ChoiceStateKey<Children>), K>]?: never } :
5720
+ never
5721
+ }[(ActiveStateKey<Children> | ChoiceStateKey<Children>)]
5722
+ }
5723
+ : { readonly states?: never })
5724
+
5711
5725
  /** Context used only when a history node has no previously captured record. */
5712
5726
  export interface HistoryDefaultContext<
5713
5727
  in out States extends StateSchemas,
@@ -6393,7 +6407,7 @@ export declare namespace Machine {
6393
6407
  & (
6394
6408
  | {
6395
6409
  /** Selects a declared descendant; its transition or bound branch constructor supplies required values. */
6396
- readonly target: ReferenceUnion<S, DestinationPath<S>>
6410
+ readonly target: ReferenceUnion<S, DestinationPath<S>> | TargetReference<Root, "", "state">
6397
6411
  /** Replaces the complete value of a retained source or ancestor without replacing its active children. */
6398
6412
  readonly update?: ReferenceUnion<S, ValuedStateIdentifier<S>>
6399
6413
  /** Enters a compound or parallel subtree through its declared initialization. */
@@ -6415,18 +6429,6 @@ export declare namespace Machine {
6415
6429
  /** Accepts the event without selecting a new destination. */
6416
6430
  readonly none?: never
6417
6431
  }
6418
- | {
6419
- /** Enters a compound or parallel subtree through its declared initialization. */
6420
- readonly initial: ReferenceUnion<S, Exclude<ObjectCompoundPath<S>, "">>
6421
- /** Selects a declared descendant; its transition or bound branch constructor supplies required values. */
6422
- readonly target?: never
6423
- /** Replaces the complete value of a retained source or ancestor without replacing its active children. */
6424
- readonly update?: never
6425
- /** Restores the declared history reference, using its fallback on first entry. */
6426
- readonly history?: never
6427
- /** Accepts the event without selecting a new destination. */
6428
- readonly none?: never
6429
- }
6430
6432
  | {
6431
6433
  /** Restores the declared history reference, using its fallback on first entry. */
6432
6434
  readonly history: ReferenceUnion<S, HistoryIdentifier<S>>
@@ -6470,56 +6472,111 @@ export declare namespace Machine {
6470
6472
  /** Named groups of inspectable destinations used by transition resolvers. */
6471
6473
  readonly branches?: Readonly<Record<string, Readonly<Record<string, unknown>>>>
6472
6474
  }
6475
+ type RegisteredInput<R> = R extends { readonly "~input": infer I extends Schema.Top } ? I : typeof Schema.Void
6473
6476
  type Registered<R, Kind extends PropertyKey> = Kind extends keyof R ? NonNullable<R[Kind]> : {}
6474
- type AtBuilder<T, P extends string> = P extends "" ? T
6475
- : P extends `${infer H}.${infer Rest}` ? H extends keyof T ? AtBuilder<T[H], Rest> : never
6476
- : P extends keyof T ? T[P]
6477
- : never
6478
- type BranchBuilderAt<S extends StateSchemas, Src extends StateNodeIdentifier<S>, P extends string> = AtBuilder<
6479
- RootBuilder<BranchTargetBuilder<S, Src>>,
6480
- P
6481
- >
6482
6477
  type RefPath<R> = R extends TargetReference<any, infer P, any> ? P : never
6483
- type SelectionOf<S extends StateSchemas, Src extends StateNodeIdentifier<S>, D> = D extends {
6484
- /** Selects a declared descendant; its transition or bound branch constructor supplies required values. */
6478
+ type ConstructionData<Node> = [NodeSchema<Node>] extends [never] ? { readonly data?: never; readonly decoded?: never }
6479
+ :
6480
+ | (
6481
+ & { readonly decoded?: false }
6482
+ & ({} extends NodeMakeInput<Node> ? { readonly data?: NodeMakeInput<Node> } :
6483
+ { readonly data: NodeMakeInput<Node> })
6484
+ )
6485
+ | { readonly decoded: true; readonly data: NodeValue<Node> }
6486
+ type ConstructionChildren<Children extends StateSchemas, Parallel extends boolean, Source extends string = never> =
6487
+ Parallel extends true ? {
6488
+ readonly [K in ActiveStateKey<Children>]: ConstructionObject<
6489
+ Children[K],
6490
+ Source extends `${K}.${infer Rest}` ? Rest : Source extends K ? "" : never
6491
+ >
6492
+ }
6493
+ : {
6494
+ readonly [K in ActiveStateKey<Children> | ChoiceStateKey<Children>]:
6495
+ & {
6496
+ readonly [P in K]: ConstructionObject<
6497
+ Children[P],
6498
+ Source extends `${P}.${infer Rest}` ? Rest : Source extends P ? "" : never
6499
+ >
6500
+ }
6501
+ & { readonly [P in Exclude<ActiveStateKey<Children> | ChoiceStateKey<Children>, K>]?: never }
6502
+ }[ActiveStateKey<Children> | ChoiceStateKey<Children>]
6503
+ /** Values for one state and, optionally, its explicitly selected descendants. */
6504
+ export type ConstructionObject<Node, Source extends string = never> =
6505
+ & { readonly input?: never }
6506
+ & ConstructionData<Node>
6507
+ & (Node extends {
6508
+ readonly states: infer Children extends StateSchemas
6509
+ } ? {
6510
+ readonly states?: ConstructionChildren<
6511
+ Children,
6512
+ Node extends { readonly type: "parallel" } ? [Source] extends [never] ? true : false
6513
+ : false,
6514
+ Source
6515
+ >
6516
+ }
6517
+ : { readonly states?: never })
6518
+ type ConstructionCall<A, Result> = {} extends A ? (construction?: A) => Result : (construction: A) => Result
6519
+ type RootInput<R> =
6520
+ & { readonly data?: never; readonly decoded?: never; readonly states?: never; readonly update?: never }
6521
+ & (RegisteredInput<R> extends typeof Schema.Void ? { readonly input?: never }
6522
+ : {
6523
+ /** Fresh machine input used by root construction and root initial callbacks. */ readonly input:
6524
+ RegisteredInput<R>["Type"]
6525
+ })
6526
+ type ObjectSelection<
6527
+ S extends StateSchemas,
6528
+ Src extends StateNodeIdentifier<S>,
6529
+ P extends StateNodeIdentifier<S>,
6530
+ R
6531
+ > = P extends "" ? ConstructionCall<RootInput<R>, InitialTarget<P>>
6532
+ : P extends ChoiceIdentifier<S> ? () => ChoiceTarget<S, P>
6533
+ : P extends StateIdentifier<S> ? ConstructionCall<
6534
+ ConstructionObject<
6535
+ NodeByIdentifier<S, P>,
6536
+ Src extends `${P}.${infer Rest}` ? Rest : Src extends P ? "" : never
6537
+ > & { readonly update?: never },
6538
+ Target<S, P>
6539
+ >
6540
+ : never
6541
+ type SelectionOf<S extends StateSchemas, Src extends StateNodeIdentifier<S>, D, R = {}> = D extends {
6485
6542
  readonly target: infer Ref
6486
- } ? RefPath<Ref> extends infer P extends StateNodeIdentifier<S> ? D extends {
6487
- /** Replaces the complete value of a retained source or ancestor without replacing its active children. */
6488
- readonly update: infer Update
6489
- } ?
6490
- RefPath<Update> extends
6543
+ } ?
6544
+ RefPath<Ref> extends infer P extends StateNodeIdentifier<S> ?
6545
+ D extends { readonly update: infer Update }
6546
+ ? RefPath<Update> extends
6491
6547
  infer Owner extends Extract<ParentStateIdentifier<Src>, ParentStateIdentifier<P> & ValuedStateIdentifier<S>>
6492
- ? TargetSelection<UpdatingTargetBuilder<BranchBuilderAt<S, Src, P>, S, Owner>, P, "state", "branch"> :
6493
- never
6494
- : TargetSelection<BranchBuilderAt<S, Src, P>, P, P extends ChoiceIdentifier<S> ? "choice" : "state", "branch">
6548
+ ? P extends StateIdentifier<S> ? TargetSelection<
6549
+ ConstructionCall<
6550
+ ConstructionObject<
6551
+ NodeByIdentifier<S, P>,
6552
+ Src extends `${P}.${infer Rest}` ? Rest : Src extends P ? "" : never
6553
+ > & {
6554
+ readonly update: ConstructionData<NodeByIdentifier<S, Owner>>
6555
+ },
6556
+ CombinedTarget<Target<S, P>, S, Owner>
6557
+ >,
6558
+ P,
6559
+ "state",
6560
+ "branch"
6561
+ > :
6562
+ never
6563
+ : never
6564
+ : TargetSelection<ObjectSelection<S, Src, P, R>, P, P extends ChoiceIdentifier<S> ? "choice" : "state", "branch">
6495
6565
  : never
6496
- : D extends {
6497
- /** Enters a compound or parallel subtree through its declared initialization. */
6498
- readonly initial: infer Ref
6499
- } ? RefPath<Ref> extends infer P extends string ? BranchBuilderAt<S, Src, P> extends {
6500
- /** Enters a compound or parallel subtree through its declared initialization. */
6501
- readonly initial: infer B
6502
- } ? TargetSelection<B, P, "initial", "branch">
6503
- : never :
6504
- never
6505
- : D extends {
6506
- /** Restores the declared history reference, using its fallback on first entry. */
6507
- readonly history: infer Ref
6508
- } ?
6509
- RefPath<Ref> extends infer P extends string
6510
- ? TargetSelection<AtBuilder<RootBuilder<HistoryTargetBuilder<S>>, P>, P, "history", "full"> :
6566
+ : D extends { readonly history: infer Ref }
6567
+ ? RefPath<Ref> extends infer P extends HistoryIdentifier<S>
6568
+ ? TargetSelection<() => HistoryTarget<S, P>, P, "history", "full"> :
6511
6569
  never
6512
- : D extends {
6513
- /** Replaces the complete value of a retained source or ancestor without replacing its active children. */
6514
- readonly update: infer Ref
6515
- } ?
6516
- RefPath<Ref> extends infer P extends Extract<Src | ParentStateIdentifier<Src>, ValuedStateIdentifier<S>>
6517
- ? TargetSelection<StateUpdateBuilder<S, P>, P, "update", "branch"> :
6570
+ : D extends { readonly update: infer Ref }
6571
+ ? RefPath<Ref> extends infer P extends Extract<Src | ParentStateIdentifier<Src>, ValuedStateIdentifier<S>>
6572
+ ? TargetSelection<
6573
+ ConstructionCall<ConstructionData<NodeByIdentifier<S, P>>, StateUpdate<S, P>>,
6574
+ P,
6575
+ "update",
6576
+ "branch"
6577
+ > :
6518
6578
  never
6519
- : D extends {
6520
- /** Accepts the event without selecting a new destination. */
6521
- readonly none: true
6522
- } ? TargetSelection<() => NoTarget, never, "none", "local">
6579
+ : D extends { readonly none: true } ? TargetSelection<() => NoTarget, never, "none", "local">
6523
6580
  : never
6524
6581
  type BuilderArgs<B, K extends PropertyKey> = K extends keyof B
6525
6582
  ? B[K] extends (...args: infer A) => unknown ? A : never
@@ -6530,15 +6587,6 @@ export declare namespace Machine {
6530
6587
  & { readonly [K in Exclude<"from" | "decoded", M>]?: never }
6531
6588
  : never
6532
6589
  }[Extract<keyof B, "from" | "decoded">]
6533
- type ObjectDefault<B> = B extends {
6534
- /** Constructs schema make input from the typed source context. */
6535
- readonly from: () => unknown
6536
- } | (() => unknown) ? {
6537
- /** Constructs schema make input from the typed source context. */
6538
- readonly from?: never /** Supplies an already decoded schema value from the typed source context. */
6539
- readonly decoded?: never
6540
- }
6541
- : never
6542
6590
  type ObjectPolicy<C, Reenter extends boolean, Acceptance extends TransitionAcceptance> = {
6543
6591
  /** Set to true to exit and reenter the handler source. */
6544
6592
  readonly reenter?: Reenter extends true ? boolean : never
@@ -6560,13 +6608,24 @@ export declare namespace Machine {
6560
6608
  P extends DestinationPath<S>,
6561
6609
  C
6562
6610
  > = P extends ChoiceIdentifier<S> ? { readonly from?: never; readonly decoded?: never }
6563
- : P extends StateIdentifier<S> ?
6564
- NodeByIdentifier<S, P> extends { readonly states: StateSchemas }
6565
- ? [NodeSchema<NodeByIdentifier<S, P>>] extends [never]
6566
- ? ObjectConstruction<C, BranchBuilderAt<S, Src, P>> | ObjectDefault<BranchBuilderAt<S, Src, P>>
6567
- : never
6568
- : InlineNodeConstruction<C, NodeByIdentifier<S, P>>
6611
+ : P extends StateIdentifier<S> ? InlineNodeConstruction<C, NodeByIdentifier<S, P>>
6569
6612
  : never
6613
+ type InlineRoot<S extends StateSchemas, C, R> =
6614
+ & {
6615
+ readonly target: ReferenceAt<S, Extract<"", StateNodeIdentifier<S>>>
6616
+ readonly update?: never
6617
+ readonly branches?: never
6618
+ readonly resolve?: never
6619
+ readonly history?: never
6620
+ readonly none?: never
6621
+ }
6622
+ & (RegisteredInput<R> extends typeof Schema.Void ? { readonly input?: never }
6623
+ : {
6624
+ /** Fresh machine input used to reconstruct root and its initial children. */ readonly input: DataValue<
6625
+ C,
6626
+ RegisteredInput<R>["Type"]
6627
+ >
6628
+ })
6570
6629
  type InlineDestination<S extends StateSchemas, Src extends StateNodeIdentifier<S>, C> = {
6571
6630
  readonly [P in DestinationPath<S>]:
6572
6631
  & {
@@ -6595,24 +6654,7 @@ export declare namespace Machine {
6595
6654
  P extends string,
6596
6655
  Owner extends ValuedStateIdentifier<S>,
6597
6656
  C
6598
- > = P extends StateIdentifier<S> ? NodeByIdentifier<S, P> extends { readonly states: StateSchemas } ? {
6599
- readonly [
6600
- M in Extract<keyof BranchBuilderAt<S, Src, P> & keyof StateUpdateBuilder<S, Owner>, "from" | "decoded">
6601
- ]: BuilderArgs<BranchBuilderAt<S, Src, P>, M> extends readonly [unknown?] ?
6602
- & {
6603
- readonly [K in M]: (
6604
- context: C
6605
- ) => {
6606
- /** Selects a declared descendant; its transition or bound branch constructor supplies required values. */
6607
- readonly target: BuilderArgs<BranchBuilderAt<S, Src, P>, M>[0]
6608
- /** Replaces the complete value of a retained source or ancestor without replacing its active children. */
6609
- readonly update: BuilderArgs<StateUpdateBuilder<S, Owner>, M>[0]
6610
- }
6611
- }
6612
- & { readonly [K in Exclude<"from" | "decoded", M>]?: never }
6613
- : never
6614
- }[Extract<keyof BranchBuilderAt<S, Src, P> & keyof StateUpdateBuilder<S, Owner>, "from" | "decoded">]
6615
- :
6657
+ > = P extends StateIdentifier<S> ?
6616
6658
  | {
6617
6659
  readonly from: (
6618
6660
  context: C
@@ -6684,35 +6726,10 @@ export declare namespace Machine {
6684
6726
  }
6685
6727
  & ObjectConstruction<C, StateUpdateBuilder<S, P>>
6686
6728
  }[Extract<Src | ParentStateIdentifier<Src>, ValuedStateIdentifier<S>>]
6687
- type InlineInitial<S extends StateSchemas, Src extends StateNodeIdentifier<S>, C> =
6688
- [Exclude<ObjectCompoundPath<S>, "">] extends [never] ? never : {
6689
- readonly [P in Exclude<ObjectCompoundPath<S>, "">]: InitialTargetFactory<NodeByIdentifier<S, P>, P> extends
6690
- infer B ?
6691
- & {
6692
- /** Enters a compound or parallel subtree through its declared initialization. */
6693
- readonly initial: ReferenceAt<S, P>
6694
- /** Selects a declared descendant; its transition or bound branch constructor supplies required values. */
6695
- readonly target?: never
6696
- /** Replaces the complete value of a retained source or ancestor without replacing its active children. */
6697
- readonly update?: never
6698
- /** Restores the declared history reference, using its fallback on first entry. */
6699
- readonly history?: never
6700
- /** Accepts the event without selecting a new destination. */
6701
- readonly none?: never
6702
- /** Named groups of inspectable destinations used by transition resolvers. */
6703
- readonly branches?: never
6704
- /** Selects one declared branch and may enqueue synchronous commands. */
6705
- readonly resolve?: never
6706
- /** Set to true when the resolver can explicitly return decline(). */
6707
- readonly declinable?: never
6708
- }
6709
- & (ObjectConstruction<C, B> | ObjectDefault<B>) :
6710
- never
6711
- }[Exclude<ObjectCompoundPath<S>, "">]
6712
- type BranchSelections<S extends StateSchemas, Src extends StateNodeIdentifier<S>, Group> = {
6729
+ type BranchSelections<S extends StateSchemas, Src extends StateNodeIdentifier<S>, Group, R> = {
6713
6730
  readonly [K in Extract<keyof Group, string>]: {
6714
6731
  /** Selects a declared descendant; its transition or bound branch constructor supplies required values. */
6715
- readonly target: SelectionOf<S, Src, Group[K]>
6732
+ readonly target: SelectionOf<S, Src, Group[K], R>
6716
6733
  }
6717
6734
  }
6718
6735
  type ObjectBranchResolver<
@@ -6725,7 +6742,7 @@ export declare namespace Machine {
6725
6742
  context: C & { readonly select: BranchSelectors<B> } & DeclineCapability,
6726
6743
  enqueue: Enqueue<EventOf<Ev>, EmittedEventOf<Em>>
6727
6744
  ) => BranchSelectionResult<B> | (Declinable extends true ? Declined : never)
6728
- type InvalidBranchSelection<S extends StateSchemas, Src extends StateNodeIdentifier<S>, Group> = {
6745
+ type InvalidBranchSelection<S extends StateSchemas, Src extends StateNodeIdentifier<S>, Group, R> = {
6729
6746
  readonly [K in keyof Group]: Src extends ChoiceIdentifier<S> ? Group[K] extends {
6730
6747
  /** Accepts the event without selecting a new destination. */
6731
6748
  readonly none: true
@@ -6733,9 +6750,9 @@ export declare namespace Machine {
6733
6750
  /** Replaces the complete value of a retained source or ancestor without replacing its active children. */
6734
6751
  readonly update: unknown
6735
6752
  } ? K
6736
- : [SelectionBuilder<SelectionOf<S, Src, Group[K]>>] extends [never] ? K
6753
+ : [SelectionBuilder<SelectionOf<S, Src, Group[K], R>>] extends [never] ? K
6737
6754
  : never
6738
- : [SelectionBuilder<SelectionOf<S, Src, Group[K]>>] extends [never] ? K
6755
+ : [SelectionBuilder<SelectionOf<S, Src, Group[K], R>>] extends [never] ? K
6739
6756
  : never
6740
6757
  }[keyof Group]
6741
6758
  type ObjectBranches<
@@ -6748,7 +6765,7 @@ export declare namespace Machine {
6748
6765
  Acceptance extends TransitionAcceptance
6749
6766
  > = {
6750
6767
  readonly [K in Extract<keyof Registered<R, "branches">, string>]:
6751
- & ([InvalidBranchSelection<S, Src, Registered<R, "branches">[K]>] extends [never] ? unknown : never)
6768
+ & ([InvalidBranchSelection<S, Src, Registered<R, "branches">[K], R>] extends [never] ? unknown : never)
6752
6769
  & {
6753
6770
  /** Named groups of inspectable destinations used by transition resolvers. */
6754
6771
  readonly branches: K
@@ -6771,7 +6788,13 @@ export declare namespace Machine {
6771
6788
  /** Set to true when the resolver can explicitly return decline(). */
6772
6789
  readonly declinable?: "declinable" extends Acceptance ? boolean : false
6773
6790
  /** Selects one declared branch and may enqueue synchronous commands. */
6774
- readonly resolve: ObjectBranchResolver<Ev, Em, C, BranchSelections<S, Src, Registered<R, "branches">[K]>, true>
6791
+ readonly resolve: ObjectBranchResolver<
6792
+ Ev,
6793
+ Em,
6794
+ C,
6795
+ BranchSelections<S, Src, Registered<R, "branches">[K], R>,
6796
+ true
6797
+ >
6775
6798
  }
6776
6799
  }[Extract<keyof Registered<R, "branches">, string>]
6777
6800
  type DataValue<C, A> = A | ((context: C) => A)
@@ -6803,7 +6826,7 @@ export declare namespace Machine {
6803
6826
  | InlineDestination<S, Src, C>
6804
6827
  | InlineUpdate<S, Src, C>
6805
6828
  | InlineCombined<S, Src, C>
6806
- | InlineInitial<S, Src, C>
6829
+ | InlineRoot<S, C, R>
6807
6830
  | {
6808
6831
  /** Restores the declared history reference, using its fallback on first entry. */
6809
6832
  readonly history: ReferenceUnion<S, HistoryIdentifier<S>>
@@ -7135,13 +7158,13 @@ export declare namespace Machine {
7135
7158
  type RootConstruction<Node, Input> = [NodeSchema<Node>] extends [never] ?
7136
7159
  { /** Root-owned data is available only when the root declares a schema. */ readonly root?: never }
7137
7160
  : {} extends NodeMakeInput<Node> ? {
7138
- /** Constructs root-owned data once from startup input. */ readonly root?: ShortConstruction<
7161
+ /** Constructs root-owned data from startup or fresh root-target input. */ readonly root?: ShortConstruction<
7139
7162
  { readonly input: Input },
7140
7163
  Node
7141
7164
  >
7142
7165
  }
7143
7166
  : {
7144
- /** Constructs root-owned data once from startup input. */ readonly root: ShortConstruction<
7167
+ /** Constructs root-owned data from startup or fresh root-target input. */ readonly root: ShortConstruction<
7145
7168
  { readonly input: Input },
7146
7169
  Node
7147
7170
  >
@@ -7215,10 +7238,16 @@ export declare namespace Machine {
7215
7238
  S,
7216
7239
  Node,
7217
7240
  Src,
7218
- StateActionContext<S, Ev, Em, Extract<Src, StateIdentifier<S>>, In, Pa> & {
7241
+ & StateActionContext<S, Ev, Em, Extract<Src, StateIdentifier<S>>, In, Pa>
7242
+ & {
7219
7243
  /** Root-owned data constructed before initial descendant values. */
7220
7244
  readonly root: StateByIdentifier<S, Extract<"", StateIdentifier<S>>>
7221
7245
  }
7246
+ & (Src extends "" ? {
7247
+ /** Fresh machine input used by root construction and root initial callbacks. */ readonly input:
7248
+ RegisteredInput<R>["Type"]
7249
+ }
7250
+ : {})
7222
7251
  >
7223
7252
  & (Src extends ChoiceIdentifier<S> ? {
7224
7253
  /** Required total transition for a transient choice state. */
@@ -7314,13 +7343,16 @@ export declare namespace Machine {
7314
7343
  }) :
7315
7344
  never)
7316
7345
  type ValidateObjectTransition<T> =
7346
+ & ("input" extends keyof T
7347
+ ? T extends { readonly target: TargetReference<any, "", "state"> } ? unknown : { readonly input: never }
7348
+ : unknown)
7317
7349
  & {
7318
7350
  readonly [
7319
7351
  K in Exclude<
7320
7352
  keyof T,
7321
7353
  | "target"
7322
7354
  | "update"
7323
- | "initial"
7355
+ | "input"
7324
7356
  | "history"
7325
7357
  | "none"
7326
7358
  | "branches"
@@ -7436,7 +7468,7 @@ export declare namespace Machine {
7436
7468
  & (C extends (...args: never[]) => unknown ? never : unknown)
7437
7469
  & HandlerShape<
7438
7470
  C,
7439
- & StateHandler<S, S[""], Ev, Em, Extract<"", StateNodeIdentifier<S>>, In, Pa, R>
7471
+ & StateHandler<S, S[""], Ev, Em, Extract<"", StateNodeIdentifier<S>>, In, Pa, R & { readonly "~input": I }>
7440
7472
  & RootConstruction<S[""], I["Type"]>
7441
7473
  >
7442
7474
  & RootConstruction<S[""], I["Type"]>
@@ -9154,6 +9186,53 @@ export const watch: <State, Event, Error = never, Output = never>(
9154
9186
  ref: MachineRef<State, Event, Error, Output>
9155
9187
  ) => Stream.Stream<RuntimeOutcome<State, Error, Output>> = internal.watch
9156
9188
 
9189
+ /**
9190
+ * Waits for the first current or subsequent published snapshot matching a predicate.
9191
+ *
9192
+ * Use for external Effect coordination and tests. Prefer atom selectors for UI
9193
+ * reads, invocation outcomes for workflow behavior, and `ref.join` for output.
9194
+ * This observes state; it does not acknowledge or correlate a sent event.
9195
+ *
9196
+ * The predicate runs before terminal classification, so explicitly matching a
9197
+ * done, error, or stopped snapshot succeeds. Otherwise an error preserves its
9198
+ * Cause, stopping fails with `StoppedError`, and completion without a match
9199
+ * fails with `Cause.NoSuchElementError`. Predicate exceptions are defects.
9200
+ *
9201
+ * Evaluation subscribes lazily. Interruption releases only the subscription,
9202
+ * never the machine. There is no default timeout; compose `Effect.timeout`.
9203
+ * Type predicates narrow the returned snapshot. Intermediate microsteps and
9204
+ * historical snapshots are not observed.
9205
+ *
9206
+ * @example
9207
+ * ```ts
9208
+ * const snapshot = yield* Machine.waitFor(ref, isReady).pipe(Effect.timeout("5 seconds"))
9209
+ * ```
9210
+ * @category combinators
9211
+ * @since 0.37.0
9212
+ */
9213
+ export const waitFor: {
9214
+ <State, Error, Output, Narrowed extends RuntimeSnapshot<State, Error, Output>>(
9215
+ predicate: (snapshot: RuntimeSnapshot<State, Error, Output>) => snapshot is Narrowed
9216
+ ): <Event, Emitted>(ref: MachineRef<State, Event, Error, Output, Emitted>) => Effect.Effect<
9217
+ Narrowed,
9218
+ Error | StoppedError | Cause.NoSuchElementError
9219
+ >
9220
+ <State, Error, Output>(
9221
+ predicate: (snapshot: RuntimeSnapshot<State, Error, Output>) => boolean
9222
+ ): <Event, Emitted>(ref: MachineRef<State, Event, Error, Output, Emitted>) => Effect.Effect<
9223
+ RuntimeSnapshot<State, Error, Output>,
9224
+ Error | StoppedError | Cause.NoSuchElementError
9225
+ >
9226
+ <State, Event, Error, Output, Emitted, Narrowed extends RuntimeSnapshot<State, Error, Output>>(
9227
+ ref: MachineRef<State, Event, Error, Output, Emitted>,
9228
+ predicate: (snapshot: RuntimeSnapshot<State, Error, Output>) => snapshot is Narrowed
9229
+ ): Effect.Effect<Narrowed, Error | StoppedError | Cause.NoSuchElementError>
9230
+ <State, Event, Error, Output, Emitted>(
9231
+ ref: MachineRef<State, Event, Error, Output, Emitted>,
9232
+ predicate: (snapshot: RuntimeSnapshot<State, Error, Output>) => boolean
9233
+ ): Effect.Effect<RuntimeSnapshot<State, Error, Output>, Error | StoppedError | Cause.NoSuchElementError>
9234
+ } = internal.waitFor
9235
+
9157
9236
  /**
9158
9237
  * Prepares a fresh machine without initializing it.
9159
9238
  *
@@ -16,22 +16,6 @@ export interface Collected<Event> {
16
16
  readonly emittedEvents: Array<unknown>
17
17
  }
18
18
 
19
- const targetBuilderCache = new WeakMap<object, Map<string, unknown>>()
20
-
21
- export const getTargetBuilder = (machine: Machine.Any, path: string): any => {
22
- let byPath = targetBuilderCache.get(machine)
23
- if (byPath === undefined) {
24
- byPath = new Map()
25
- targetBuilderCache.set(machine, byPath)
26
- }
27
- if (byPath.has(path)) {
28
- return byPath.get(path)
29
- }
30
- const builder = machine.makeTargetBuilder(path as any)
31
- byPath.set(path, builder)
32
- return builder
33
- }
34
-
35
19
  export const makeCollector = <Event>(machine: Machine.Any): Collected<Event> => {
36
20
  const commands: Array<RuntimeCommand> = []
37
21
  const raisedEvents: Array<Event> = []
@@ -991,7 +991,7 @@ const configurationFromTargetPathSync = (
991
991
 
992
992
  for (const ancestor of paths) {
993
993
  const ancestorNode = getNode(machine, ancestor)
994
- if (ancestorNode.type !== "parallel") continue
994
+ if (ancestorNode.type !== "parallel" || ancestor === node.path) continue
995
995
  for (const child of ancestorNode.children) {
996
996
  if (pathSet.has(child) || !current.active.has(child)) continue
997
997
  for (const activePath of current.active) {
@@ -1016,15 +1016,18 @@ export const configurationFromInitialTargetSync = (
1016
1016
  machine: Machine.Any,
1017
1017
  current: ActiveConfiguration,
1018
1018
  target: InitialTargetInstruction
1019
- ): ActiveConfiguration =>
1020
- configurationFromTargetPathSync(
1021
- machine,
1022
- current,
1023
- target.path,
1024
- target.value,
1025
- target.values as Readonly<Record<string, unknown>> | undefined,
1026
- true
1027
- )
1019
+ ): ActiveConfiguration => {
1020
+ const partial = configurationFromTargetPathSync(machine, current, target.path, target.value, target.values, true)
1021
+ if (target.children === undefined) return partial
1022
+ const active = new Set(partial.active)
1023
+ const values = new Map(partial.values)
1024
+ for (const [path, value] of target.children) {
1025
+ const node = getNode(machine, path)
1026
+ active.add(path)
1027
+ if (node.schema !== undefined) values.set(path, decodeStateValueSync(machine, node, value))
1028
+ }
1029
+ return { ...partial, active, values }
1030
+ }
1028
1031
 
1029
1032
  const configurationFromTargetSnapshotSync = (
1030
1033
  machine: Machine.Any,