@typeonce/effect-machine 0.33.0 → 0.35.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 (137) hide show
  1. package/README.md +272 -628
  2. package/dist/Machine.d.ts +1076 -896
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +141 -90
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/childRegistry.d.ts +1 -1
  7. package/dist/internal/machine/childRegistry.d.ts.map +1 -1
  8. package/dist/internal/machine/cluster.d.ts +2 -50
  9. package/dist/internal/machine/cluster.d.ts.map +1 -1
  10. package/dist/internal/machine/cluster.js +4 -27
  11. package/dist/internal/machine/cluster.js.map +1 -1
  12. package/dist/internal/machine/clusterProtocol.d.ts +35 -0
  13. package/dist/internal/machine/clusterProtocol.d.ts.map +1 -0
  14. package/dist/internal/machine/clusterProtocol.js +28 -0
  15. package/dist/internal/machine/clusterProtocol.js.map +1 -0
  16. package/dist/internal/machine/commandRuntime.d.ts +1 -1
  17. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  18. package/dist/internal/machine/configuration.d.ts +2 -2
  19. package/dist/internal/machine/configuration.d.ts.map +1 -1
  20. package/dist/internal/machine/configuration.js +38 -64
  21. package/dist/internal/machine/configuration.js.map +1 -1
  22. package/dist/internal/machine/declaration.d.ts +17 -0
  23. package/dist/internal/machine/declaration.d.ts.map +1 -0
  24. package/dist/internal/machine/declaration.js +120 -0
  25. package/dist/internal/machine/declaration.js.map +1 -0
  26. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  27. package/dist/internal/machine/executionPlan.js +15 -22
  28. package/dist/internal/machine/executionPlan.js.map +1 -1
  29. package/dist/internal/machine/implementation.d.ts +34 -0
  30. package/dist/internal/machine/implementation.d.ts.map +1 -0
  31. package/dist/internal/machine/implementation.js +3 -0
  32. package/dist/internal/machine/implementation.js.map +1 -0
  33. package/dist/internal/machine/initialDeclaration.d.ts +14 -0
  34. package/dist/internal/machine/initialDeclaration.d.ts.map +1 -0
  35. package/dist/internal/machine/initialDeclaration.js +137 -0
  36. package/dist/internal/machine/initialDeclaration.js.map +1 -0
  37. package/dist/internal/machine/invocation.d.ts +1 -1
  38. package/dist/internal/machine/invocation.d.ts.map +1 -1
  39. package/dist/internal/machine/invocation.js +1 -1
  40. package/dist/internal/machine/invocation.js.map +1 -1
  41. package/dist/internal/machine/invocationDefinition.d.ts +1 -1
  42. package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
  43. package/dist/internal/machine/machine.d.ts +13 -4
  44. package/dist/internal/machine/machine.d.ts.map +1 -1
  45. package/dist/internal/machine/machine.js +169 -388
  46. package/dist/internal/machine/machine.js.map +1 -1
  47. package/dist/internal/machine/planner.d.ts +8 -0
  48. package/dist/internal/machine/planner.d.ts.map +1 -1
  49. package/dist/internal/machine/planner.js +37 -78
  50. package/dist/internal/machine/planner.js.map +1 -1
  51. package/dist/internal/machine/process.d.ts +8 -7
  52. package/dist/internal/machine/process.d.ts.map +1 -1
  53. package/dist/internal/machine/process.js +16 -12
  54. package/dist/internal/machine/process.js.map +1 -1
  55. package/dist/internal/machine/protocol.d.ts +1 -1
  56. package/dist/internal/machine/protocol.d.ts.map +1 -1
  57. package/dist/internal/machine/protocol.js.map +1 -1
  58. package/dist/internal/machine/runtime.d.ts +2 -230
  59. package/dist/internal/machine/runtime.d.ts.map +1 -1
  60. package/dist/internal/machine/runtime.js +8 -1857
  61. package/dist/internal/machine/runtime.js.map +1 -1
  62. package/dist/internal/machine/runtimeCompiled.d.ts +5 -0
  63. package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -0
  64. package/dist/internal/machine/runtimeCompiled.js +867 -0
  65. package/dist/internal/machine/runtimeCompiled.js.map +1 -0
  66. package/dist/internal/machine/runtimeGeneric.d.ts +3 -0
  67. package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -0
  68. package/dist/internal/machine/runtimeGeneric.js +471 -0
  69. package/dist/internal/machine/runtimeGeneric.js.map +1 -0
  70. package/dist/internal/machine/runtimeProtocol.d.ts +312 -0
  71. package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -0
  72. package/dist/internal/machine/runtimeProtocol.js +561 -0
  73. package/dist/internal/machine/runtimeProtocol.js.map +1 -0
  74. package/dist/internal/machine/stateDefinition.d.ts +1 -1
  75. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  76. package/dist/internal/machine/stateDefinition.js +1 -13
  77. package/dist/internal/machine/stateDefinition.js.map +1 -1
  78. package/dist/internal/machine/targetReference.d.ts +15 -0
  79. package/dist/internal/machine/targetReference.d.ts.map +1 -0
  80. package/dist/internal/machine/targetReference.js +41 -0
  81. package/dist/internal/machine/targetReference.js.map +1 -0
  82. package/dist/internal/machine/topology.d.ts.map +1 -1
  83. package/dist/internal/machine/topology.js +5 -3
  84. package/dist/internal/machine/topology.js.map +1 -1
  85. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  86. package/dist/internal/testing/machine/finiteModel.js +53 -46
  87. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  88. package/dist/internal/testing/machine/probe.js +1 -1
  89. package/dist/internal/testing/machine/probe.js.map +1 -1
  90. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  91. package/dist/internal/testing/machine/verification.js +7 -1
  92. package/dist/internal/testing/machine/verification.js.map +1 -1
  93. package/dist/testing/MachineTest.d.ts +105 -73
  94. package/dist/testing/MachineTest.d.ts.map +1 -1
  95. package/dist/testing/MachineTest.js +105 -73
  96. package/dist/testing/MachineTest.js.map +1 -1
  97. package/dist/unstable/cluster/ClusterMachine.d.ts +18 -13
  98. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  99. package/dist/unstable/cluster/ClusterMachine.js +18 -25
  100. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  101. package/dist/unstable/reactivity/AtomMachine.d.ts +41 -24
  102. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  103. package/dist/unstable/reactivity/AtomMachine.js +41 -24
  104. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  105. package/docs/agent-guide.md +66 -91
  106. package/docs/machine-review.md +19 -49
  107. package/docs/root-api.md +62 -62
  108. package/package.json +1 -1
  109. package/src/Machine.ts +1836 -2907
  110. package/src/internal/machine/childRegistry.ts +1 -1
  111. package/src/internal/machine/cluster.ts +7 -73
  112. package/src/internal/machine/clusterProtocol.ts +56 -0
  113. package/src/internal/machine/commandRuntime.ts +1 -1
  114. package/src/internal/machine/configuration.ts +53 -65
  115. package/src/internal/machine/declaration.ts +140 -0
  116. package/src/internal/machine/executionPlan.ts +16 -22
  117. package/src/internal/machine/implementation.ts +41 -0
  118. package/src/internal/machine/initialDeclaration.ts +157 -0
  119. package/src/internal/machine/invocation.ts +3 -3
  120. package/src/internal/machine/invocationDefinition.ts +1 -1
  121. package/src/internal/machine/machine.ts +225 -576
  122. package/src/internal/machine/planner.ts +84 -118
  123. package/src/internal/machine/process.ts +40 -33
  124. package/src/internal/machine/protocol.ts +3 -3
  125. package/src/internal/machine/runtime.ts +20 -3037
  126. package/src/internal/machine/runtimeCompiled.ts +1166 -0
  127. package/src/internal/machine/runtimeGeneric.ts +799 -0
  128. package/src/internal/machine/runtimeProtocol.ts +1213 -0
  129. package/src/internal/machine/stateDefinition.ts +1 -15
  130. package/src/internal/machine/targetReference.ts +55 -0
  131. package/src/internal/machine/topology.ts +5 -3
  132. package/src/internal/testing/machine/finiteModel.ts +58 -53
  133. package/src/internal/testing/machine/probe.ts +1 -1
  134. package/src/internal/testing/machine/verification.ts +7 -1
  135. package/src/testing/MachineTest.ts +105 -73
  136. package/src/unstable/cluster/ClusterMachine.ts +22 -25
  137. package/src/unstable/reactivity/AtomMachine.ts +41 -24
@@ -35,10 +35,11 @@ import {
35
35
  normalizeTargetConfigurationSync,
36
36
  pathDepth,
37
37
  snapshotFromConfiguration,
38
- snapshotFromConfigurationAtPath,
39
- validateInitialConfiguration
38
+ snapshotFromConfigurationAtPath
40
39
  } from "./configuration.js"
41
40
  import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError, StoppedError } from "./errors.js"
41
+ import { type CapturedStateConfig, toImpl } from "./implementation.js"
42
+ import { isDataInitializer } from "./initialDeclaration.js"
42
43
  import { getStateInitializeValues, makeStateInitializeBuilder } from "./initialization.js"
43
44
  import * as InvocationDefinition from "./invocationDefinition.js"
44
45
  import * as InvocationEvent from "./invocationEvent.js"
@@ -299,11 +300,13 @@ const completeHistoryConfiguration = (
299
300
  )
300
301
  let next = current
301
302
  for (const routed of [choice.target, ...choice.additionalTargets]) {
302
- if (routed === undefined || isHistoryTarget(routed)) {
303
+ if (routed === undefined) {
303
304
  throw new Error(`Machine initial choice "${child.path}" must resolve to an active state target`)
304
305
  }
305
306
  const resolved = isInitialTarget(routed)
306
307
  ? resolveInitialTarget(machine, next, routed, event)
308
+ : isHistoryTarget(routed)
309
+ ? resolveHistoryTarget(machine, next, routed, event)
307
310
  : undefined
308
311
  next = normalizeTargetConfigurationSync(
309
312
  machine,
@@ -330,7 +333,7 @@ const completeHistoryConfiguration = (
330
333
  }
331
334
  active.add(child.path)
332
335
  if (child.schema !== undefined) {
333
- const initializer = machine.handlers[path]?.initialize
336
+ const initializer = toImpl(machine).handlers[path]?.initialize
334
337
  if (initializer === undefined) {
335
338
  values.set(child.path, decodeStateValueSync(machine, child, makeStateInput({})))
336
339
  changed = true
@@ -342,7 +345,7 @@ const completeHistoryConfiguration = (
342
345
  containingState: getParentValue(machine, current, path),
343
346
  ancestors: getParentValues(machine, current, path),
344
347
  event,
345
- builder: makeStateInitializeBuilder(machine, path)
348
+ ...(isDataInitializer(initializer) ? {} : { builder: makeStateInitializeBuilder(machine, path) })
346
349
  })
347
350
  const initializedValues = getStateInitializeValues(path, initialized.value)
348
351
  values.set(child.path, decodeStateValueSync(machine, child, initializedValues[child.key]))
@@ -363,14 +366,14 @@ const completeHistoryConfiguration = (
363
366
  history: configuration.history,
364
367
  machineReferences: configuration.machineReferences
365
368
  } as ActiveConfiguration
366
- const initializer = valuedMissing.length === 0 ? undefined : machine.handlers[path]?.initialize
369
+ const initializer = valuedMissing.length === 0 ? undefined : toImpl(machine).handlers[path]?.initialize
367
370
  const initialized = initializer === undefined ? undefined : collectStateInitializer(machine, initializer, {
368
371
  ...resolveMachineReferences(machine, current),
369
372
  state: current.values.get(path),
370
373
  containingState: getParentValue(machine, current, path),
371
374
  ancestors: getParentValues(machine, current, path),
372
375
  event,
373
- builder: makeStateInitializeBuilder(machine, path)
376
+ ...(isDataInitializer(initializer) ? {} : { builder: makeStateInitializeBuilder(machine, path) })
374
377
  })
375
378
  const initializedValues = initialized === undefined
376
379
  ? undefined
@@ -386,6 +389,10 @@ const completeHistoryConfiguration = (
386
389
  if (
387
390
  initializedValues === undefined || !Object.prototype.hasOwnProperty.call(initializedValues, child.key)
388
391
  ) {
392
+ if (initializer !== undefined && isDataInitializer(initializer)) {
393
+ values.set(child.path, decodeStateValueSync(machine, child, makeStateInput({})))
394
+ continue
395
+ }
389
396
  throw new Error(`Machine parallel state initializer for "${path}" must return region "${child.key}"`)
390
397
  }
391
398
  values.set(
@@ -418,14 +425,24 @@ const completeHistoryConfiguration = (
418
425
  }
419
426
  }
420
427
 
421
- export function resolveInitialTarget(
428
+ /** Resolves initial data into owned configuration without a snapshot round trip. */
429
+ export function resolveInitialConfiguration(
422
430
  machine: Machine.Any,
423
431
  configuration: ActiveConfiguration,
424
432
  target: InitialTargetInstruction,
425
433
  event: unknown
426
434
  ) {
427
435
  const partial = configurationFromInitialTargetSync(machine, configuration, target)
428
- const completed = completeHistoryConfiguration(machine, partial, event)
436
+ return completeHistoryConfiguration(machine, partial, event)
437
+ }
438
+
439
+ export function resolveInitialTarget(
440
+ machine: Machine.Any,
441
+ configuration: ActiveConfiguration,
442
+ target: InitialTargetInstruction,
443
+ event: unknown
444
+ ) {
445
+ const completed = resolveInitialConfiguration(machine, configuration, target, event)
429
446
  const snapshot = snapshotFromConfigurationAtPath(machine, completed.configuration, target.path)
430
447
  return {
431
448
  target: makeTarget(target.path as any, snapshot.value as any, {
@@ -472,7 +489,7 @@ function resolveHistoryTarget(
472
489
  }
473
490
 
474
491
  const key = node.key
475
- const fallback = machine.handlers[target.parent]?.history?.[key]?.default
492
+ const fallback = toImpl(machine).handlers[target.parent]?.history?.[key]?.default
476
493
  if (fallback === undefined) {
477
494
  throw new Error(`Machine history state "${target.path}" requires a default implementation`)
478
495
  }
@@ -709,7 +726,7 @@ const makeTransitionContext = <
709
726
  path: string,
710
727
  event: Machine.EventByTag<Events, EventTag>,
711
728
  snapshot: Machine.Snapshot<States>
712
- ): Machine.HandlerContext<States, Events, Emits, StateId, EventTag, any, any> => ({
729
+ ): ConstructionContext<Machine.HandlerContext<States, Events, Emits, StateId, EventTag, any, any>> => ({
713
730
  ...resolveMachineReferences(machine, configuration),
714
731
  state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
715
732
  containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
@@ -731,7 +748,7 @@ const makeDoneContext = <
731
748
  event: Machine.LifecycleEvent<Events>,
732
749
  output: unknown,
733
750
  snapshot: Machine.Snapshot<States>
734
- ): Machine.DoneContext<States, Events, Emits, StateId> => ({
751
+ ): ConstructionContext<Machine.DoneContext<States, Events, Emits, StateId>> => ({
735
752
  ...resolveMachineReferences(machine, configuration),
736
753
  state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
737
754
  containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
@@ -766,7 +783,7 @@ const collectStateActions = <
766
783
  R
767
784
  >(
768
785
  machine,
769
- machine.handlers[path]?.[key],
786
+ toImpl(machine).handlers[path]?.[key],
770
787
  makeStateActionContext<States, Events, Emits, Machine.StateIdentifier<States>>(
771
788
  machine,
772
789
  configuration,
@@ -796,7 +813,7 @@ const selectAlwaysTransitions = <
796
813
  States,
797
814
  E,
798
815
  R,
799
- Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>
816
+ ConstructionContext<Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>>
800
817
  >
801
818
  > => {
802
819
  const selected: Array<
@@ -804,7 +821,7 @@ const selectAlwaysTransitions = <
804
821
  States,
805
822
  E,
806
823
  R,
807
- Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>
824
+ ConstructionContext<Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>>
808
825
  >
809
826
  > = []
810
827
  const selectedSources = new Set<string>()
@@ -813,7 +830,7 @@ const selectAlwaysTransitions = <
813
830
  const capturedSnapshot = () => snapshot ??= snapshotFromConfiguration<States>(machine, configuration)
814
831
  for (const leaf of getActiveLeafPaths(machine, configuration)) {
815
832
  for (const path of getLeafCandidatePaths(machine, leaf)) {
816
- const always = normalizeTransition(machine.handlers[path]?.always)
833
+ const always = normalizeTransition(toImpl(machine).handlers[path]?.always)
817
834
  if (always !== undefined) {
818
835
  let candidate = evaluatedSources.get(path)
819
836
  if (!evaluatedSources.has(path)) {
@@ -825,7 +842,7 @@ const selectAlwaysTransitions = <
825
842
  States,
826
843
  E,
827
844
  R,
828
- Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>
845
+ ConstructionContext<Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>>
829
846
  >,
830
847
  context: {
831
848
  ...resolveMachineReferences(machine, configuration),
@@ -876,7 +893,7 @@ const selectDoneTransitions = <
876
893
  States,
877
894
  E,
878
895
  R,
879
- Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>
896
+ ConstructionContext<Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>>
880
897
  >
881
898
  > => {
882
899
  const selected: Array<
@@ -884,14 +901,14 @@ const selectDoneTransitions = <
884
901
  States,
885
902
  E,
886
903
  R,
887
- Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>
904
+ ConstructionContext<Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>>
888
905
  >
889
906
  > = []
890
907
  const selectedSources = new Set<string>()
891
908
  let snapshot: Machine.Snapshot<States> | undefined
892
909
  const capturedSnapshot = () => snapshot ??= snapshotFromConfiguration<States>(machine, configuration)
893
910
  for (const completion of completions) {
894
- const onDone = normalizeTransition(machine.handlers[completion.path]?.onDone)
911
+ const onDone = normalizeTransition(toImpl(machine).handlers[completion.path]?.onDone)
895
912
  if (onDone !== undefined && !selectedSources.has(completion.path)) {
896
913
  selectedSources.add(completion.path)
897
914
  const candidate = resolveDeclinableCandidate(machine, {
@@ -902,7 +919,7 @@ const selectDoneTransitions = <
902
919
  States,
903
920
  E,
904
921
  R,
905
- Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>
922
+ ConstructionContext<Machine.DoneContext<States, Events, Emits, Machine.StateIdentifier<States>>>
906
923
  >,
907
924
  context: makeDoneContext<States, Events, Emits, Machine.StateIdentifier<States>>(
908
925
  machine,
@@ -934,14 +951,7 @@ const selectEventTransitions = <
934
951
  States,
935
952
  E,
936
953
  R,
937
- Machine.HandlerContext<States, Events, Emits, Machine.StateIdentifier<States>, Machine.TagOf<Events[number]>, E, R>
938
- >
939
- > => {
940
- const selected: Array<
941
- SelectedTransition<
942
- States,
943
- E,
944
- R,
954
+ ConstructionContext<
945
955
  Machine.HandlerContext<
946
956
  States,
947
957
  Events,
@@ -952,6 +962,25 @@ const selectEventTransitions = <
952
962
  R
953
963
  >
954
964
  >
965
+ >
966
+ > => {
967
+ const selected: Array<
968
+ SelectedTransition<
969
+ States,
970
+ E,
971
+ R,
972
+ ConstructionContext<
973
+ Machine.HandlerContext<
974
+ States,
975
+ Events,
976
+ Emits,
977
+ Machine.StateIdentifier<States>,
978
+ Machine.TagOf<Events[number]>,
979
+ E,
980
+ R
981
+ >
982
+ >
983
+ >
955
984
  > = []
956
985
  const selectedSources = new Set<string>()
957
986
  const evaluatedSources = new Map<string, SelectedTransition<States, E, R, any> | undefined>()
@@ -959,7 +988,7 @@ const selectEventTransitions = <
959
988
  const capturedSnapshot = () => snapshot ??= snapshotFromConfiguration<States>(machine, configuration)
960
989
  for (const leaf of getActiveLeafPaths(machine, configuration)) {
961
990
  for (const path of getLeafCandidatePaths(machine, leaf)) {
962
- const transition = normalizeTransition(machine.handlers[path]?.on?.[event._tag])
991
+ const transition = normalizeTransition(toImpl(machine).handlers[path]?.on?.[event._tag])
963
992
  if (transition !== undefined) {
964
993
  let candidate = evaluatedSources.get(path)
965
994
  if (!evaluatedSources.has(path)) {
@@ -971,14 +1000,16 @@ const selectEventTransitions = <
971
1000
  States,
972
1001
  E,
973
1002
  R,
974
- Machine.HandlerContext<
975
- States,
976
- Events,
977
- Emits,
978
- Machine.StateIdentifier<States>,
979
- Machine.TagOf<Events[number]>,
980
- E,
981
- R
1003
+ ConstructionContext<
1004
+ Machine.HandlerContext<
1005
+ States,
1006
+ Events,
1007
+ Emits,
1008
+ Machine.StateIdentifier<States>,
1009
+ Machine.TagOf<Events[number]>,
1010
+ E,
1011
+ R
1012
+ >
982
1013
  >
983
1014
  >,
984
1015
  context: makeTransitionContext<
@@ -1015,7 +1046,7 @@ const selectInvocationTransition = <
1015
1046
  event: InvocationEvent.InvocationEvent
1016
1047
  ): ReadonlyArray<SelectedTransition<States, E, R, any>> => {
1017
1048
  if (!configuration.active.has(event.path)) return []
1018
- const config = machine.handlers[event.path] as Machine.AnyStateConfig | undefined
1049
+ const config = toImpl(machine).handlers[event.path] as CapturedStateConfig | undefined
1019
1050
  const invoke = InvocationDefinition.definitions(config?.invoke).find((definition) => {
1020
1051
  const id = "child" in definition ? definition.child?.id : definition.id
1021
1052
  return String(id) === event.id
@@ -1265,7 +1296,7 @@ function resolveChoiceTarget(
1265
1296
  if (node.type !== "choice" || node.parent !== extracted.target.parent) {
1266
1297
  throw new Error(`Machine expected choice target "${extracted.target.path}" to resolve to its declared parent`)
1267
1298
  }
1268
- const choice = machine.handlers[node.path]?.choice
1299
+ const choice = toImpl(machine).handlers[node.path]?.choice
1269
1300
  if (choice === undefined || typeof choice.transition !== "function") {
1270
1301
  throw new Error(`Machine choice state "${node.path}" requires an implementation`)
1271
1302
  }
@@ -1705,77 +1736,13 @@ export const planInitialSync = <
1705
1736
  outputs: new Map(),
1706
1737
  history: new Map()
1707
1738
  }
1708
- const rootResolution = isInitialTarget(initial)
1709
- ? resolveInitialTarget(machine, emptyConfiguration, initial, InitialEvent)
1710
- : undefined
1711
- const state = rootResolution?.target ?? initial
1712
- const initialChoice = choiceFromTarget(state)
1713
- const choiceResolution = initialChoice === undefined
1714
- ? undefined
1715
- : resolveChoiceTarget(
1716
- machine,
1717
- emptyConfiguration,
1718
- state,
1719
- InitialEvent
1720
- )
1721
- const initialHistoryActions: Array<RuntimeCommand> = []
1722
- const initialHistoryRaisedEvents: Array<unknown> = []
1723
- const initialHistoryEmittedEvents: Array<unknown> = []
1724
- const initialHistoryChoiceTransitions: Array<ResolvedChoiceTransition> = []
1725
- const resolvedInitialTargets: Array<unknown> = []
1726
- for (
1727
- const target of choiceResolution !== undefined
1728
- ? [choiceResolution.target, ...choiceResolution.additionalTargets]
1729
- : rootResolution === undefined
1730
- ? []
1731
- : [rootResolution.target]
1732
- ) {
1733
- if (!isHistoryTarget(target)) {
1734
- resolvedInitialTargets.push(target)
1735
- continue
1736
- }
1737
- const history = resolveHistoryTarget(machine, emptyConfiguration, target, InitialEvent)
1738
- resolvedInitialTargets.push(history.target)
1739
- initialHistoryActions.push(...history.commands)
1740
- initialHistoryRaisedEvents.push(...history.raisedEvents)
1741
- initialHistoryEmittedEvents.push(...history.emittedEvents)
1742
- initialHistoryChoiceTransitions.push(...history.transitions)
1743
- }
1744
- let resolvedConfiguration = resolvedInitialTargets.length === 0
1745
- ? normalizeConfigurationSync<States>(machine, state as Machine.Snapshot<States>)
1746
- : normalizeTargetConfigurationSync<States>(
1747
- machine,
1748
- emptyConfiguration,
1749
- resolvedInitialTargets[0] as
1750
- | Machine.Snapshot<States>
1751
- | Machine.Target<States, Machine.StateIdentifier<States>>
1752
- )
1753
- for (const additionalTarget of resolvedInitialTargets.slice(1)) {
1754
- resolvedConfiguration = normalizeTargetConfigurationSync<States>(
1755
- machine,
1756
- resolvedConfiguration,
1757
- additionalTarget as Machine.Snapshot<States> | Machine.Target<States, Machine.StateIdentifier<States>>
1758
- )
1759
- }
1760
- const configuration: ActiveConfiguration = resolvedConfiguration
1761
- if (machine.initialDefinition.selection.kind === "initial") validateInitialConfiguration(machine, configuration)
1739
+ const rootResolution = resolveInitialTarget(machine, emptyConfiguration, initial, InitialEvent)
1740
+ const configuration = normalizeTargetConfigurationSync(machine, emptyConfiguration, rootResolution.target)
1762
1741
  const startingState = snapshotFromConfiguration<States>(machine, configuration)
1763
1742
  const initialEntryPaths = getInitialEntryPaths(machine, configuration)
1764
- const commands = [
1765
- ...(rootResolution?.commands ?? []),
1766
- ...(choiceResolution?.commands ?? []),
1767
- ...initialHistoryActions
1768
- ]
1769
- const raisedEvents = [
1770
- ...(rootResolution?.raisedEvents ?? []),
1771
- ...(choiceResolution?.raisedEvents ?? []),
1772
- ...initialHistoryRaisedEvents
1773
- ]
1774
- const emittedEvents = [
1775
- ...(rootResolution?.emittedEvents ?? []),
1776
- ...(choiceResolution?.emittedEvents ?? []),
1777
- ...initialHistoryEmittedEvents
1778
- ]
1743
+ const commands = rootResolution.commands
1744
+ const raisedEvents = rootResolution.raisedEvents
1745
+ const emittedEvents = rootResolution.emittedEvents
1779
1746
  const entry = collectStateActions<States, Events, Emits, E, R>(
1780
1747
  machine,
1781
1748
  configuration,
@@ -1790,17 +1757,12 @@ export const planInitialSync = <
1790
1757
  [...entry.commands],
1791
1758
  [...raisedEvents, ...entry.raisedEvents] as Array<Machine.EventOf<Events>>,
1792
1759
  [...emittedEvents, ...entry.emittedEvents],
1793
- choiceResolution === undefined && (rootResolution?.transitions.length ?? 0) === 0 &&
1794
- initialHistoryChoiceTransitions.length === 0 ?
1760
+ rootResolution.transitions.length === 0 ?
1795
1761
  [] :
1796
1762
  [{
1797
1763
  next: configuration,
1798
1764
  event: InitialEvent,
1799
- transitions: [
1800
- ...(rootResolution?.transitions ?? []),
1801
- ...(choiceResolution?.transitions ?? []),
1802
- ...initialHistoryChoiceTransitions
1803
- ],
1765
+ transitions: rootResolution.transitions,
1804
1766
  commands,
1805
1767
  raisedEvents: [
1806
1768
  ...raisedEvents
@@ -1876,7 +1838,7 @@ export const enabled = <
1876
1838
  const tags: Array<Machine.TagOf<Events[number]>> = []
1877
1839
  const seen = new Set<PropertyKey>()
1878
1840
  for (const path of getCandidatePaths(machine, configuration)) {
1879
- for (const tag of Reflect.ownKeys(machine.handlers[path]?.on ?? {})) {
1841
+ for (const tag of Reflect.ownKeys(toImpl(machine).handlers[path]?.on ?? {})) {
1880
1842
  if (!seen.has(tag)) {
1881
1843
  seen.add(tag)
1882
1844
  tags.push(tag as Machine.TagOf<Events[number]>)
@@ -2090,7 +2052,7 @@ const settle = <
2090
2052
  const completed = completeConfigurationSync(machine, currentState, currentEvent)
2091
2053
  currentState = completed.configuration
2092
2054
  pendingCompletions.push(
2093
- ...completed.completions.filter((completion) => machine.handlers[completion.path]?.onDone !== undefined)
2055
+ ...completed.completions.filter((completion) => toImpl(machine).handlers[completion.path]?.onDone !== undefined)
2094
2056
  )
2095
2057
  while (pendingCompletions.length > 0 && !currentState.active.has(pendingCompletions[0]!.path)) {
2096
2058
  pendingCompletions.shift()
@@ -2356,3 +2318,7 @@ export const planInitial = (
2356
2318
  : new StartupError({ cause: Cause.die(error) })
2357
2319
  }
2358
2320
  })
2321
+
2322
+ // Captured constructors are private planner inputs. Public callbacks receive root data
2323
+ // and the constructors bound to their declared branches during normalization.
2324
+ type ConstructionContext<C> = Omit<C, "root"> & { readonly target: unknown }