@typeonce/effect-machine 0.45.1 → 0.47.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 (148) hide show
  1. package/AGENTS.md +1823 -0
  2. package/CLAUDE.md +1823 -0
  3. package/README.md +100 -77
  4. package/ai-docs/README.md +18 -0
  5. package/ai-docs/src/10_modeling/01_create-a-machine.ts +51 -0
  6. package/ai-docs/src/10_modeling/02_branch-on-an-event.ts +55 -0
  7. package/ai-docs/src/10_modeling/03_update-retained-state.ts +58 -0
  8. package/ai-docs/src/10_modeling/04_verify-after-changing.ts +29 -0
  9. package/{docs/agent-guide.md → ai-docs/src/10_modeling/index.md} +254 -155
  10. package/{docs/root-api.md → ai-docs/src/20_root-api/index.md} +29 -57
  11. package/{docs/effect-atom-react.md → ai-docs/src/30_react/index.md} +49 -49
  12. package/{docs/machine-review.md → ai-docs/src/40_review/index.md} +42 -40
  13. package/ai-docs/src/index.md +7 -0
  14. package/ai-docs/tsconfig.json +22 -0
  15. package/dist/Machine.d.ts +1205 -1478
  16. package/dist/Machine.d.ts.map +1 -1
  17. package/dist/Machine.js +366 -214
  18. package/dist/Machine.js.map +1 -1
  19. package/dist/internal/machine/atom.js.map +1 -1
  20. package/dist/internal/machine/cluster.js.map +1 -1
  21. package/dist/internal/machine/command.d.ts +8 -0
  22. package/dist/internal/machine/command.d.ts.map +1 -1
  23. package/dist/internal/machine/command.js +2 -2
  24. package/dist/internal/machine/command.js.map +1 -1
  25. package/dist/internal/machine/configuration.d.ts +21 -5
  26. package/dist/internal/machine/configuration.d.ts.map +1 -1
  27. package/dist/internal/machine/configuration.js +1 -0
  28. package/dist/internal/machine/configuration.js.map +1 -1
  29. package/dist/internal/machine/construction.js.map +1 -1
  30. package/dist/internal/machine/declaration.d.ts +2 -1
  31. package/dist/internal/machine/declaration.d.ts.map +1 -1
  32. package/dist/internal/machine/declaration.js +28 -46
  33. package/dist/internal/machine/declaration.js.map +1 -1
  34. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  35. package/dist/internal/machine/executionPlan.js +23 -16
  36. package/dist/internal/machine/executionPlan.js.map +1 -1
  37. package/dist/internal/machine/implementation.d.ts +2 -0
  38. package/dist/internal/machine/implementation.d.ts.map +1 -1
  39. package/dist/internal/machine/implementation.js.map +1 -1
  40. package/dist/internal/machine/initialDeclaration.d.ts +2 -0
  41. package/dist/internal/machine/initialDeclaration.d.ts.map +1 -1
  42. package/dist/internal/machine/initialDeclaration.js +15 -0
  43. package/dist/internal/machine/initialDeclaration.js.map +1 -1
  44. package/dist/internal/machine/invocation.d.ts.map +1 -1
  45. package/dist/internal/machine/invocation.js +32 -21
  46. package/dist/internal/machine/invocation.js.map +1 -1
  47. package/dist/internal/machine/invocationEvent.d.ts +34 -17
  48. package/dist/internal/machine/invocationEvent.d.ts.map +1 -1
  49. package/dist/internal/machine/invocationEvent.js +16 -4
  50. package/dist/internal/machine/invocationEvent.js.map +1 -1
  51. package/dist/internal/machine/machine.d.ts +5 -22
  52. package/dist/internal/machine/machine.d.ts.map +1 -1
  53. package/dist/internal/machine/machine.js +157 -119
  54. package/dist/internal/machine/machine.js.map +1 -1
  55. package/dist/internal/machine/planner.d.ts +15 -4
  56. package/dist/internal/machine/planner.d.ts.map +1 -1
  57. package/dist/internal/machine/planner.js +18 -21
  58. package/dist/internal/machine/planner.js.map +1 -1
  59. package/dist/internal/machine/process.d.ts +2 -0
  60. package/dist/internal/machine/process.d.ts.map +1 -1
  61. package/dist/internal/machine/process.js +2 -0
  62. package/dist/internal/machine/process.js.map +1 -1
  63. package/dist/internal/machine/protocol.js.map +1 -1
  64. package/dist/internal/machine/runtime.d.ts.map +1 -1
  65. package/dist/internal/machine/runtime.js.map +1 -1
  66. package/dist/internal/machine/runtimeCompiled.js.map +1 -1
  67. package/dist/internal/machine/runtimeGeneric.js +5 -3
  68. package/dist/internal/machine/runtimeGeneric.js.map +1 -1
  69. package/dist/internal/machine/runtimeProtocol.d.ts +26 -0
  70. package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -1
  71. package/dist/internal/machine/runtimeProtocol.js +71 -0
  72. package/dist/internal/machine/runtimeProtocol.js.map +1 -1
  73. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  74. package/dist/internal/machine/stateDefinition.js +0 -14
  75. package/dist/internal/machine/stateDefinition.js.map +1 -1
  76. package/dist/internal/machine/symbols.d.ts +9 -0
  77. package/dist/internal/machine/symbols.d.ts.map +1 -1
  78. package/dist/internal/machine/symbols.js.map +1 -1
  79. package/dist/internal/machine/topology.d.ts +18 -9
  80. package/dist/internal/machine/topology.d.ts.map +1 -1
  81. package/dist/internal/machine/topology.js +16 -1
  82. package/dist/internal/machine/topology.js.map +1 -1
  83. package/dist/internal/testing/machine/arbitrary.d.ts.map +1 -1
  84. package/dist/internal/testing/machine/arbitrary.js +2 -0
  85. package/dist/internal/testing/machine/arbitrary.js.map +1 -1
  86. package/dist/internal/testing/machine/check.d.ts +17 -0
  87. package/dist/internal/testing/machine/check.d.ts.map +1 -0
  88. package/dist/internal/testing/machine/check.js +371 -0
  89. package/dist/internal/testing/machine/check.js.map +1 -0
  90. package/dist/internal/testing/machine/exploration.d.ts.map +1 -1
  91. package/dist/internal/testing/machine/exploration.js.map +1 -1
  92. package/dist/internal/testing/machine/finiteModel.d.ts +14 -13
  93. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  94. package/dist/internal/testing/machine/finiteModel.js +21 -23
  95. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  96. package/dist/internal/testing/machine/runtime.d.ts +19 -88
  97. package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
  98. package/dist/internal/testing/machine/runtime.js +0 -19
  99. package/dist/internal/testing/machine/runtime.js.map +1 -1
  100. package/dist/internal/testing/machine/runtimeInvariant.js.map +1 -1
  101. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  102. package/dist/internal/testing/machine/verification.js.map +1 -1
  103. package/dist/testing/MachineTest.d.ts +349 -139
  104. package/dist/testing/MachineTest.d.ts.map +1 -1
  105. package/dist/testing/MachineTest.js +201 -136
  106. package/dist/testing/MachineTest.js.map +1 -1
  107. package/dist/unstable/cluster/ClusterMachine.d.ts +9 -14
  108. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  109. package/dist/unstable/cluster/ClusterMachine.js +9 -14
  110. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  111. package/dist/unstable/reactivity/AtomMachine.d.ts +169 -80
  112. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  113. package/dist/unstable/reactivity/AtomMachine.js +165 -76
  114. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  115. package/package.json +11 -9
  116. package/src/Machine.ts +1149 -2134
  117. package/src/internal/machine/atom.ts +3 -3
  118. package/src/internal/machine/cluster.ts +2 -2
  119. package/src/internal/machine/command.ts +10 -4
  120. package/src/internal/machine/configuration.ts +16 -17
  121. package/src/internal/machine/construction.ts +1 -1
  122. package/src/internal/machine/declaration.ts +25 -50
  123. package/src/internal/machine/executionPlan.ts +90 -56
  124. package/src/internal/machine/implementation.ts +3 -1
  125. package/src/internal/machine/initialDeclaration.ts +16 -0
  126. package/src/internal/machine/invocation.ts +73 -70
  127. package/src/internal/machine/invocationEvent.ts +46 -20
  128. package/src/internal/machine/machine.ts +217 -243
  129. package/src/internal/machine/planner.ts +72 -106
  130. package/src/internal/machine/process.ts +12 -0
  131. package/src/internal/machine/protocol.ts +6 -6
  132. package/src/internal/machine/runtime.ts +1 -5
  133. package/src/internal/machine/runtimeCompiled.ts +2 -2
  134. package/src/internal/machine/runtimeGeneric.ts +7 -7
  135. package/src/internal/machine/runtimeProtocol.ts +80 -4
  136. package/src/internal/machine/stateDefinition.ts +0 -39
  137. package/src/internal/machine/symbols.ts +11 -0
  138. package/src/internal/machine/topology.ts +40 -18
  139. package/src/internal/testing/machine/arbitrary.ts +2 -0
  140. package/src/internal/testing/machine/check.ts +460 -0
  141. package/src/internal/testing/machine/exploration.ts +4 -5
  142. package/src/internal/testing/machine/finiteModel.ts +40 -42
  143. package/src/internal/testing/machine/runtime.ts +31 -170
  144. package/src/internal/testing/machine/runtimeInvariant.ts +3 -3
  145. package/src/internal/testing/machine/verification.ts +14 -14
  146. package/src/testing/MachineTest.ts +366 -145
  147. package/src/unstable/cluster/ClusterMachine.ts +9 -14
  148. package/src/unstable/reactivity/AtomMachine.ts +167 -78
@@ -6,7 +6,7 @@
6
6
 
7
7
  import * as Effect from "effect/Effect"
8
8
  import type { Machine, MachineTarget } from "../../Machine.js"
9
- import { type RuntimeCommand } from "./command.js"
9
+ import { type RuntimeCommand, type SendToTarget, type StopTarget } from "./command.js"
10
10
  import {
11
11
  type ActiveConfiguration,
12
12
  compareDocumentOrder,
@@ -56,6 +56,7 @@ import {
56
56
  isSnapshot,
57
57
  isStateUpdate,
58
58
  isTarget,
59
+ type TargetSnapshotSlot,
59
60
  TargetSnapshotTypeId
60
61
  } from "./topology.js"
61
62
 
@@ -151,7 +152,7 @@ const compileIndexedExecutionDescriptor = (
151
152
  finalPaths.push(node.path)
152
153
  }
153
154
 
154
- const config = toImpl(machine).handlers[node.path] as CapturedStateConfig | undefined
155
+ const config = toImpl(machine).handlers[node.path]
155
156
  if (!supportsIndexedStateConfig(config)) {
156
157
  return undefined
157
158
  }
@@ -328,13 +329,22 @@ const activeConfigurationFromIndexedState = (
328
329
  return { active, values, outputs, history: new Map() }
329
330
  }
330
331
 
332
+ /** A snapshot node under construction; it is detached from the indexed tables. */
333
+ interface IndexedSnapshotNode {
334
+ readonly path: string
335
+ readonly value: unknown
336
+ state?: IndexedSnapshotNode
337
+ states?: Record<string, IndexedSnapshotNode>
338
+ completed?: ReadonlyArray<Machine.SnapshotCompletion>
339
+ }
340
+
331
341
  const snapshotFromIndexedStateStatePath = (
332
342
  descriptor: IndexedExecutionDescriptor,
333
343
  configuration: OwnedIndexedState,
334
344
  index: number
335
- ): Machine.AtomicSnapshot<string, unknown> => {
345
+ ): IndexedSnapshotNode => {
336
346
  const node = descriptor.nodes[index]!
337
- const snapshot: Record<string, unknown> = {
347
+ const snapshot: IndexedSnapshotNode = {
338
348
  path: node.path,
339
349
  value: descriptor.valued[index] ? configuration.values[index] : undefined
340
350
  }
@@ -345,7 +355,7 @@ const snapshotFromIndexedStateStatePath = (
345
355
  }
346
356
  snapshot.state = snapshotFromIndexedStateStatePath(descriptor, configuration, childIndex)
347
357
  } else if (node.type === "parallel") {
348
- const states: Record<string, unknown> = {}
358
+ const states: Record<string, IndexedSnapshotNode> = {}
349
359
  for (const childIndex of descriptor.childIndices[index]!) {
350
360
  if (configuration.active[childIndex] !== 1) {
351
361
  throw new Error(
@@ -362,7 +372,7 @@ const snapshotFromIndexedStateStatePath = (
362
372
  }
363
373
  snapshot.states = states
364
374
  }
365
- return snapshot as unknown as Machine.AtomicSnapshot<string, unknown>
375
+ return snapshot
366
376
  }
367
377
 
368
378
  const snapshotFromIndexedState = (
@@ -371,11 +381,9 @@ const snapshotFromIndexedState = (
371
381
  ): Machine.Snapshot<any> => {
372
382
  const rootIndex = descriptor.rootIndices.find((index) => configuration.active[index] === 1)
373
383
  if (rootIndex === undefined) throw new Error("Machine expected an active indexed root state")
374
- const snapshot = snapshotFromIndexedStateStatePath(descriptor, configuration, rootIndex) as Machine.Snapshot<any>
384
+ const snapshot = snapshotFromIndexedStateStatePath(descriptor, configuration, rootIndex)
375
385
  if (configuration.completedOrder.length > 0) {
376
- ;(snapshot as Machine.AtomicSnapshot<string, unknown> & {
377
- completed: ReadonlyArray<Machine.SnapshotCompletion>
378
- }).completed = configuration.completedOrder.map((index) => ({
386
+ snapshot.completed = configuration.completedOrder.map((index) => ({
379
387
  path: descriptor.nodes[index]!.path,
380
388
  output: configuration.outputs[index]
381
389
  }))
@@ -383,14 +391,30 @@ const snapshotFromIndexedState = (
383
391
  return snapshot
384
392
  }
385
393
 
394
+ /** A decoded machine event; indexed dispatch reads only its tag. */
395
+ interface IndexedEvent {
396
+ readonly _tag: PropertyKey
397
+ }
398
+
399
+ /** Handler context built by indexed dispatch, matching the generic planner's transition context. */
400
+ interface IndexedTransitionContext {
401
+ readonly self: MachineTarget<any>
402
+ readonly parent?: MachineTarget<any> | undefined
403
+ readonly state: unknown
404
+ readonly containingState: unknown
405
+ readonly ancestors: Readonly<Record<string, unknown>>
406
+ readonly event: IndexedEvent
407
+ readonly snapshot: Machine.Snapshot<any>
408
+ }
409
+
386
410
  const makeIndexedTransitionContext = (
387
411
  machine: Machine.Any,
388
412
  descriptor: IndexedExecutionDescriptor,
389
413
  configuration: OwnedIndexedState,
390
414
  sourceIndex: number,
391
- event: any,
415
+ event: IndexedEvent,
392
416
  machineReferences?: PlanningMachineReferences
393
- ): any => {
417
+ ): IndexedTransitionContext => {
394
418
  const parentIndex = descriptor.parentIndices[sourceIndex]!
395
419
  const ancestors: Record<string, unknown> = {}
396
420
  for (const ancestorIndex of descriptor.ancestorIndices[sourceIndex]!) {
@@ -408,14 +432,14 @@ const makeIndexedTransitionContext = (
408
432
  }
409
433
  }
410
434
 
411
- type IndexedSelectedTransition = SelectedTransition<any, any, any, any> & {
435
+ type IndexedSelectedTransition = SelectedTransition<any, any, any, IndexedTransitionContext> & {
412
436
  readonly sourceIndex: number
413
437
  readonly leafIndex: number
414
438
  }
415
439
 
416
440
  type IndexedEvaluatedTransition =
417
441
  & Omit<
418
- EvaluatedTransition<any, any, any, any, any>,
442
+ EvaluatedTransition<any, IndexedEvent, any, any, IndexedTransitionContext>,
419
443
  "selection"
420
444
  >
421
445
  & {
@@ -447,14 +471,14 @@ export interface ExecutionMacrostep<State = unknown> {
447
471
  readonly output: unknown
448
472
  }
449
473
 
450
- const collectIndexedTransition = (
474
+ const collectIndexedTransition = <Context>(
451
475
  machine: Machine.Any,
452
- transition: TransitionHandler<any, any, any, any>,
453
- context: any,
454
- evaluate?: NonNullable<MicrostepTransition<any, any, any, any>["evaluate"]>
476
+ transition: TransitionHandler<any, any, any, Context>,
477
+ context: Context,
478
+ evaluate?: NonNullable<MicrostepTransition<any, any, any, Context>["evaluate"]>
455
479
  ) => {
456
480
  let commands: Array<RuntimeCommand> | undefined
457
- let raisedEvents: Array<any> | undefined
481
+ let raisedEvents: Array<IndexedEvent> | undefined
458
482
  let emittedEvents: Array<unknown> | undefined
459
483
  const enqueue = {
460
484
  raise: (event: unknown) => {
@@ -463,11 +487,11 @@ const collectIndexedTransition = (
463
487
  emit: (event: unknown) => {
464
488
  ;(emittedEvents ??= []).push(decodeEmitSync(machine, event))
465
489
  },
466
- sendTo: (child: unknown, event: unknown) => {
467
- ;(commands ??= []).push({ _tag: "SendTo", target: child as any, event })
490
+ sendTo: (target: SendToTarget, event: unknown) => {
491
+ ;(commands ??= []).push({ _tag: "SendTo", target, event })
468
492
  },
469
- stop: (child: unknown) => {
470
- ;(commands ??= []).push({ _tag: "Stop", child: child as any })
493
+ stop: (child: StopTarget) => {
494
+ ;(commands ??= []).push({ _tag: "Stop", child })
471
495
  }
472
496
  }
473
497
  const evaluated = evaluate === undefined
@@ -487,7 +511,7 @@ const selectIndexedEventTransitions = (
487
511
  machine: Machine.Any,
488
512
  descriptor: IndexedExecutionDescriptor,
489
513
  configuration: OwnedIndexedState,
490
- event: any,
514
+ event: IndexedEvent,
491
515
  machineReferences?: PlanningMachineReferences
492
516
  ): ReadonlyArray<IndexedSelectedTransition> => {
493
517
  const selected: Array<IndexedSelectedTransition> = []
@@ -537,7 +561,8 @@ const normalizeIndexedTargetStateSync = (
537
561
  const targetIndex = isTarget(target) ? descriptor.indexByPath.get(String(target.path)) : undefined
538
562
  if (
539
563
  targetIndex === activeLeafIndex && current.active[activeLeafIndex] === 1 && isTarget(target) &&
540
- target[TargetSnapshotTypeId] === undefined && target.values === undefined && current.completedOrder.length === 0 &&
564
+ (target as TargetSnapshotSlot)[TargetSnapshotTypeId] === undefined && target.values === undefined &&
565
+ current.completedOrder.length === 0 &&
541
566
  descriptor.valued[activeLeafIndex]
542
567
  ) {
543
568
  const values = current.values.slice()
@@ -597,8 +622,12 @@ const collectIndexedEvaluatedTransition = (
597
622
  selection.transition.targets,
598
623
  unresolvedTarget
599
624
  )
600
- const initialResolution = unresolvedTarget !== undefined && isInitialTarget(unresolvedTarget)
601
- ? resolveInitialTarget(
625
+ const initialTarget = unresolvedTarget !== undefined && isInitialTarget(unresolvedTarget)
626
+ ? unresolvedTarget
627
+ : undefined
628
+ const initialResolution = initialTarget === undefined
629
+ ? undefined
630
+ : resolveInitialTarget(
602
631
  machine,
603
632
  (() => {
604
633
  const current = activeConfigurationFromIndexedState(descriptor, state)
@@ -606,21 +635,24 @@ const collectIndexedEvaluatedTransition = (
606
635
  ? current
607
636
  : { ...current, values: new Map(current.values).set(update.path, update.value) }
608
637
  })(),
609
- unresolvedTarget,
610
- (selection.context as any).event
638
+ initialTarget,
639
+ selection.context.event
611
640
  )
612
- : undefined
613
641
  const target = initialResolution?.target ?? unresolvedTarget
614
642
  if (target !== undefined && !isTarget(target) && !isSnapshot(target)) {
615
643
  throw new Error("Machine expected indexed transition target to be a snapshot or target builder result")
616
644
  }
645
+ // Without an initial target, the validated target is the returned target.
646
+ const retainedTarget = initialTarget ?? target
647
+ // The shared initial-target resolver decodes raised events but erases their type.
648
+ const initialRaisedEvents = (initialResolution?.raisedEvents ?? emptyExecutionValues) as ReadonlyArray<IndexedEvent>
617
649
  let next = target === undefined
618
650
  ? update === undefined ? state : (() => {
619
651
  const next = copyOwnedIndexedState(state)
620
652
  next.values[descriptor.indexByPath.get(update.path)!] = update.value
621
653
  return next
622
654
  })()
623
- : normalizeIndexedTargetStateSync(machine, descriptor, state, target as any, selection.leafIndex)
655
+ : normalizeIndexedTargetStateSync(machine, descriptor, state, target, selection.leafIndex)
624
656
  if (combined !== undefined && update !== undefined) {
625
657
  const ownerIndex = descriptor.indexByPath.get(update.path)!
626
658
  if (next.active[ownerIndex] !== 1) {
@@ -637,12 +669,12 @@ const collectIndexedEvaluatedTransition = (
637
669
  selection,
638
670
  branchIndex: transitionResult.branchIndex,
639
671
  branchKey: transitionResult.branchKey,
640
- unresolvedTarget: unresolvedTarget as any,
641
- target: target as any,
672
+ unresolvedTarget: retainedTarget,
673
+ target,
642
674
  update,
643
675
  next,
644
676
  commands: [...transitionResult.commands, ...(initialResolution?.commands ?? [])],
645
- raisedEvents: [...transitionResult.raisedEvents, ...(initialResolution?.raisedEvents ?? [])],
677
+ raisedEvents: [...transitionResult.raisedEvents, ...initialRaisedEvents],
646
678
  emittedEvents: [...transitionResult.emittedEvents, ...(initialResolution?.emittedEvents ?? [])],
647
679
  changed: false,
648
680
  stabilize,
@@ -652,7 +684,7 @@ const collectIndexedEvaluatedTransition = (
652
684
  }
653
685
  }
654
686
 
655
- const targetPath = target === undefined ? undefined : getTargetNodePath(target as any)
687
+ const targetPath = target === undefined ? undefined : getTargetNodePath(target)
656
688
  const naturalBoundary = targetPath === undefined
657
689
  ? descriptor.nodes[selection.sourceIndex]!.parent
658
690
  : getLeastCommonAncestor(machine, selection.leafPath, targetPath)
@@ -664,12 +696,12 @@ const collectIndexedEvaluatedTransition = (
664
696
  selection,
665
697
  branchIndex: transitionResult.branchIndex,
666
698
  branchKey: transitionResult.branchKey,
667
- unresolvedTarget: unresolvedTarget as any,
668
- target: target as any,
699
+ unresolvedTarget: retainedTarget,
700
+ target,
669
701
  update,
670
702
  next,
671
703
  commands: [...transitionResult.commands, ...(initialResolution?.commands ?? [])],
672
- raisedEvents: [...transitionResult.raisedEvents, ...(initialResolution?.raisedEvents ?? [])],
704
+ raisedEvents: [...transitionResult.raisedEvents, ...initialRaisedEvents],
673
705
  emittedEvents: [...transitionResult.emittedEvents, ...(initialResolution?.emittedEvents ?? [])],
674
706
  changed: true,
675
707
  stabilize,
@@ -679,14 +711,19 @@ const collectIndexedEvaluatedTransition = (
679
711
  }
680
712
  }
681
713
 
714
+ /** Indexed microsteps keep decoded raised events typed for the next dispatch. */
715
+ type IndexedMicrostep = ExecutionMicrostep<OwnedIndexedState> & {
716
+ readonly raisedEvents: ReadonlyArray<IndexedEvent>
717
+ }
718
+
682
719
  const indexedMicrostep = (
683
720
  machine: Machine.Any,
684
721
  descriptor: IndexedExecutionDescriptor,
685
722
  state: OwnedIndexedState,
686
- event: any,
723
+ event: IndexedEvent,
687
724
  selections: ReadonlyArray<IndexedSelectedTransition>,
688
725
  retainTransitions: boolean
689
- ): ExecutionMicrostep<OwnedIndexedState> => {
726
+ ): IndexedMicrostep => {
690
727
  const retain = (transition: IndexedEvaluatedTransition): Machine.RetainedTransition => ({
691
728
  source: transition.selection.sourcePath,
692
729
  trigger: transition.selection.trigger,
@@ -720,10 +757,7 @@ const indexedMicrostep = (
720
757
  const evaluated = activeSelections.map((selection) =>
721
758
  collectIndexedEvaluatedTransition(machine, descriptor, state, selection)
722
759
  )
723
- const transitions = sortEvaluatedTransitions(
724
- machine,
725
- removeConflictingTransitions(machine, evaluated as any)
726
- ) as ReadonlyArray<IndexedEvaluatedTransition>
760
+ const transitions = sortEvaluatedTransitions(machine, removeConflictingTransitions(machine, evaluated))
727
761
 
728
762
  let next = state
729
763
  if (transitions.length === 1) {
@@ -774,14 +808,14 @@ const planIndexedFlatState = (
774
808
  machine: Machine.Any,
775
809
  descriptor: IndexedExecutionDescriptor,
776
810
  configuration: OwnedIndexedState,
777
- decoded: { readonly _tag: PropertyKey },
811
+ decoded: IndexedEvent,
778
812
  retainMicrosteps: boolean,
779
813
  machineReferences?: PlanningMachineReferences
780
814
  ): ExecutionMacrostep<OwnedIndexedState> => {
781
815
  let current = configuration
782
- let event: any = decoded
816
+ let event: IndexedEvent = decoded
783
817
  let commands: Array<RuntimeCommand> | undefined
784
- let raisedEvents: Array<any> | undefined
818
+ let raisedEvents: Array<IndexedEvent> | undefined
785
819
  let emittedEvents: Array<unknown> | undefined
786
820
  let microsteps: Array<ExecutionMicrostep<OwnedIndexedState>> | undefined
787
821
  let raisedIndex = 0
@@ -861,7 +895,7 @@ const planIndexedFlatState = (
861
895
  if (target !== undefined) {
862
896
  const targetIndex = descriptor.indexByPath.get(String(target.path))
863
897
  const isSimpleTarget = isTarget(target)
864
- ? target[TargetSnapshotTypeId] === undefined && target.values === undefined
898
+ ? (target as TargetSnapshotSlot)[TargetSnapshotTypeId] === undefined && target.values === undefined
865
899
  : !("state" in target) && !("states" in target) && !("completed" in target) && !("history" in target)
866
900
  if (
867
901
  targetIndex === sourceIndex && isSimpleTarget && current.completedOrder.length === 0 &&
@@ -877,7 +911,7 @@ const planIndexedFlatState = (
877
911
  )
878
912
  )
879
913
  } else {
880
- next = normalizeIndexedTargetStateSync(machine, descriptor, current, target as any, sourceIndex)
914
+ next = normalizeIndexedTargetStateSync(machine, descriptor, current, target, sourceIndex)
881
915
  }
882
916
  }
883
917
  const changed = transition.reenter || !hasSameIndexedActive(current, next)
@@ -896,8 +930,8 @@ const planIndexedFlatState = (
896
930
  reenter: transition.reenter,
897
931
  branchIndex: transitionResult.branchIndex,
898
932
  branchKey: transitionResult.branchKey,
899
- target: target === undefined ? undefined : getTargetNodePath(target as any),
900
- resolvedTarget: target === undefined ? undefined : getTargetNodePath(target as any),
933
+ target: target === undefined ? undefined : getTargetNodePath(target),
934
+ resolvedTarget: target === undefined ? undefined : getTargetNodePath(target),
901
935
  updates: update === undefined ? [] : [update.path]
902
936
  }]
903
937
  }
@@ -956,7 +990,7 @@ const planIndexedState = (
956
990
  // the indexed representation; only the private lifecycle macrostep crosses
957
991
  // the representation boundary.
958
992
  const planned = planConfiguration(
959
- machine as any,
993
+ machine,
960
994
  machineReferences === undefined
961
995
  ? activeConfigurationFromIndexedState(descriptor, configuration)
962
996
  : withMachineReferences(activeConfigurationFromIndexedState(descriptor, configuration), machineReferences),
@@ -1021,7 +1055,7 @@ const planIndexedState = (
1021
1055
 
1022
1056
  const first = indexedMicrostep(machine, descriptor, configuration, decoded, selections, retainMicrosteps)
1023
1057
  let current = first.next
1024
- let currentEvent: any = decoded
1058
+ let currentEvent: IndexedEvent = decoded
1025
1059
  const commands = [...first.commands]
1026
1060
  const raisedEvents = [...first.raisedEvents]
1027
1061
  const emittedEvents = [...first.emittedEvents]
@@ -1119,11 +1153,11 @@ const makeActiveExecutionPlan = (machine: Machine.Any): CompiledExecutionPlan =>
1119
1153
  snapshot: (state) => snapshotFromConfiguration(machine, state as ActiveConfiguration),
1120
1154
  plan: (state, event, _retainMicrosteps, machineReferences) =>
1121
1155
  planConfiguration(
1122
- machine as any,
1156
+ machine,
1123
1157
  machineReferences === undefined
1124
1158
  ? state as ActiveConfiguration
1125
1159
  : withMachineReferences(state as ActiveConfiguration, machineReferences),
1126
- event as any
1160
+ event
1127
1161
  )
1128
1162
  })
1129
1163
 
@@ -1149,7 +1183,7 @@ const makeIndexedExecutionPlan = (
1149
1183
  : args.length === 0
1150
1184
  ? (decodeInputSync(machine, machine.input, undefined), args)
1151
1185
  : [decodeInputSync(machine, machine.input, args[0])]
1152
- const initial = machine.initial(...inputArgs as any)
1186
+ const initial = toImpl(machine).initial(...inputArgs)
1153
1187
  const normalized = isInitialTarget(initial)
1154
1188
  ? resolveInitialConfiguration(
1155
1189
  machine,
@@ -34,7 +34,9 @@ export interface CapturedStateConfig {
34
34
  */
35
35
  export interface MachineInternal extends Machine.Any {
36
36
  readonly handlers: Readonly<Record<string, CapturedStateConfig | undefined>>
37
+ /** Startup constructor; callers decode its input first and validate its result. */
38
+ readonly initial: (...args: ReadonlyArray<unknown>) => unknown
37
39
  }
38
40
 
39
41
  /** The single conversion from an erased public machine to its captured implementation. */
40
- export const toImpl = (machine: Machine.Any): MachineInternal => machine as MachineInternal
42
+ export const toImpl = (machine: Machine.Any): MachineInternal => machine
@@ -34,6 +34,22 @@ export const construction = (value: unknown): Constructor => {
34
34
  return (context) => Topology.makeStateInput(value === undefined ? {} : resolve(context))
35
35
  }
36
36
 
37
+ /** One `update` record value: a make input, a function of the context, or a decoded construction. */
38
+ export const ownerUpdate = (value: unknown): Constructor => {
39
+ const descriptor = typeof value === "object" && value !== null &&
40
+ hasProperty(value, "decoded") && value.decoded === true && hasProperty(value, "data")
41
+ if (descriptor) {
42
+ const config = record(value, "decoded construction")
43
+ if (Reflect.ownKeys(config).some((key) => key !== "decoded" && key !== "data")) {
44
+ throw new Error("Machine decoded construction accepts only decoded and data")
45
+ }
46
+ const resolve = evaluate(config.data)
47
+ return (context) => ({ decoded: true, data: resolve(context) })
48
+ }
49
+ const resolve = evaluate(value)
50
+ return (context) => ({ data: resolve(context) })
51
+ }
52
+
37
53
  export const edgeConstruction = (config: Readonly<Record<string, unknown>>): Constructor => {
38
54
  if (config.decoded !== undefined && typeof config.decoded !== "boolean") {
39
55
  throw new Error("Machine decoded construction flag must be a boolean")