@typeonce/effect-machine 0.46.0 → 0.47.1

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} +234 -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} +42 -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 +1195 -1474
  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 +25 -19
  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.d.ts.map +1 -1
  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 +1141 -2130
  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 +38 -44
  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 +2 -4
  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
@@ -218,7 +218,7 @@ type RefEmitted<Ref> = Ref extends Machine.MachineRef<any, any, any, any, infer
218
218
  export const emissions = <State, Event, Error, Output, StartError, Emitted>(
219
219
  self: MachineAtom<State, Event, Error, Output, StartError, Emitted>
220
220
  ): Stream.Stream<Emitted, StartError, AtomRegistry.AtomRegistry> => {
221
- const prepared = preparedByMachineAtom.get(self as object)
221
+ const prepared = preparedByMachineAtom.get(self)
222
222
  if (prepared === undefined) {
223
223
  return Atom.toStreamResult(self.ref).pipe(Stream.flatMap((ref) => ref.emissions))
224
224
  }
@@ -246,7 +246,7 @@ export const emissions = <State, Event, Error, Output, StartError, Emitted>(
246
246
  export const inspection = <State, Event, Error, Output, StartError, Emitted>(
247
247
  self: MachineAtom<State, Event, Error, Output, StartError, Emitted>
248
248
  ): Stream.Stream<Machine.Inspection.Event, StartError, AtomRegistry.AtomRegistry> => {
249
- const prepared = preparedByMachineAtom.get(self as object)
249
+ const prepared = preparedByMachineAtom.get(self)
250
250
  if (prepared === undefined) return Stream.empty
251
251
  return Stream.unwrap(
252
252
  Effect.gen(function*() {
@@ -599,7 +599,7 @@ const makeChildSelector = <StartError>(
599
599
  family = (id) => atoms(id) as ChildMachineAtom<Machine.ChildMachine.Any, StartError>
600
600
  byMachine.set(machine, family)
601
601
  }
602
- return family(descriptor.id) as ChildMachineAtom<Child, StartError>
602
+ return family(descriptor.id)
603
603
  }
604
604
  }
605
605
 
@@ -252,7 +252,7 @@ export const make = <
252
252
  )
253
253
  }
254
254
  current = initial.state
255
- emitted.push(...initial.emittedEvents as any)
255
+ emitted.push(...initial.emittedEvents)
256
256
  }
257
257
 
258
258
  if (!internalMachine.isFinal(rootMachine, current)) {
@@ -264,7 +264,7 @@ export const make = <
264
264
  )
265
265
  }
266
266
  current = planned.next
267
- emitted.push(...planned.emittedEvents as any)
267
+ emitted.push(...planned.emittedEvents)
268
268
  }
269
269
 
270
270
  const encoded = yield* internalMachine.encodeSnapshot(rootMachine, current).pipe(
@@ -9,6 +9,12 @@ import { decodeEmitSync, decodeEventSync } from "./protocol.js"
9
9
 
10
10
  export type RuntimeCommand = Command
11
11
 
12
+ /** Destination accepted by `Enqueue.sendTo`, erased over the child's event type. */
13
+ export type SendToTarget = Extract<RuntimeCommand, { readonly _tag: "SendTo" }>["target"]
14
+
15
+ /** Child accepted by `Enqueue.stop`, erased over the child's event type. */
16
+ export type StopTarget = Extract<RuntimeCommand, { readonly _tag: "Stop" }>["child"]
17
+
12
18
  export interface Collected<Event> {
13
19
  readonly enqueue: Enqueue<Event, unknown>
14
20
  readonly commands: Array<RuntimeCommand>
@@ -31,11 +37,11 @@ export const makeCollector = <Event>(machine: Machine.Any): Collected<Event> =>
31
37
  emit: (event) => {
32
38
  emittedEvents.push(decodeEmitSync(machine, event))
33
39
  },
34
- sendTo: (target: unknown, event: unknown) => {
35
- commands.push({ _tag: "SendTo", target: target as any, event })
40
+ sendTo: (target: SendToTarget, event: unknown) => {
41
+ commands.push({ _tag: "SendTo", target, event })
36
42
  },
37
- stop: (child: unknown) => {
38
- commands.push({ _tag: "Stop", child: child as any })
43
+ stop: (child: StopTarget) => {
44
+ commands.push({ _tag: "Stop", child })
39
45
  }
40
46
  }
41
47
  }
@@ -24,6 +24,7 @@ import {
24
24
  type InitialTarget as InitialTargetInstruction,
25
25
  isSnapshot,
26
26
  isTarget,
27
+ type TargetSnapshotSlot,
27
28
  TargetSnapshotTypeId
28
29
  } from "./topology.js"
29
30
 
@@ -577,9 +578,9 @@ export const normalizeConfiguration = <const States extends Machine.StateSchemas
577
578
  state: Machine.Snapshot<States>
578
579
  ): ActiveConfiguration => configurationFromSnapshot(machine, state)
579
580
 
580
- export const normalizeConfigurationSync = <const States extends Machine.StateSchemas>(
581
+ export const normalizeConfigurationSync = (
581
582
  machine: Machine.Any,
582
- state: Machine.Snapshot<States>
583
+ state: Machine.AtomicSnapshot<string, unknown>
583
584
  ): ActiveConfiguration => {
584
585
  try {
585
586
  return configurationFromSnapshot(machine, state)
@@ -642,7 +643,7 @@ export const configurationFromSnapshotEffect = Effect.fnUntraced(function*(
642
643
  values,
643
644
  outputs,
644
645
  history: yield* historyFromSnapshotEffect(machine, snapshot)
645
- } as ActiveConfiguration
646
+ }
646
647
  })
647
648
 
648
649
  export const normalizeConfigurationEffect = <const States extends Machine.StateSchemas>(
@@ -838,7 +839,7 @@ export const configurationFromTargetPathEffect = Effect.fnUntraced(function*(
838
839
  values,
839
840
  outputs,
840
841
  history: current.history
841
- } as ActiveConfiguration
842
+ }
842
843
  })
843
844
 
844
845
  export const configurationFromTargetSnapshotEffect = Effect.fnUntraced(function*(
@@ -911,7 +912,7 @@ export const configurationFromTargetSnapshotEffect = Effect.fnUntraced(function*
911
912
  values,
912
913
  outputs,
913
914
  history: new Map([...current.history, ...subtree.history])
914
- } as ActiveConfiguration
915
+ }
915
916
  })
916
917
 
917
918
  export const normalizeTargetConfigurationEffect = <const States extends Machine.StateSchemas>(
@@ -920,7 +921,7 @@ export const normalizeTargetConfigurationEffect = <const States extends Machine.
920
921
  target: Machine.Snapshot<States> | Machine.Target<States, Machine.StateIdentifier<States>>
921
922
  ): Effect.Effect<ActiveConfiguration, MachineSchemaDecodeError> => {
922
923
  if (isTarget(target)) {
923
- const snapshot = target[TargetSnapshotTypeId]
924
+ const snapshot = (target as TargetSnapshotSlot)[TargetSnapshotTypeId]
924
925
  if (snapshot !== undefined) {
925
926
  if (String(snapshot.path) !== String(target.path)) {
926
927
  throw new Error(`Machine expected target snapshot path to be "${target.path}"`)
@@ -929,7 +930,7 @@ export const normalizeTargetConfigurationEffect = <const States extends Machine.
929
930
  machine,
930
931
  current,
931
932
  snapshot,
932
- target.values as Readonly<Record<string, unknown>> | undefined
933
+ target.values
933
934
  )
934
935
  }
935
936
  return configurationFromTargetPathEffect(
@@ -937,7 +938,7 @@ export const normalizeTargetConfigurationEffect = <const States extends Machine.
937
938
  current,
938
939
  target.path,
939
940
  target.value,
940
- target.values as Readonly<Record<string, unknown>> | undefined
941
+ target.values
941
942
  )
942
943
  }
943
944
  if (isSnapshot(target)) {
@@ -1095,13 +1096,14 @@ const configurationFromTargetSnapshotSync = (
1095
1096
  }
1096
1097
  }
1097
1098
 
1098
- export const normalizeTargetConfigurationSync = <const States extends Machine.StateSchemas>(
1099
+ /** Applies a transition target, failing for anything other than a target builder result or snapshot. */
1100
+ export const normalizeTargetConfigurationSync = (
1099
1101
  machine: Machine.Any,
1100
1102
  current: ActiveConfiguration,
1101
- target: Machine.Snapshot<States> | Machine.Target<States, Machine.StateIdentifier<States>>
1103
+ target: unknown
1102
1104
  ): ActiveConfiguration => {
1103
1105
  if (isTarget(target)) {
1104
- const snapshot = target[TargetSnapshotTypeId]
1106
+ const snapshot = (target as TargetSnapshotSlot)[TargetSnapshotTypeId]
1105
1107
  if (snapshot !== undefined) {
1106
1108
  if (String(snapshot.path) !== String(target.path)) {
1107
1109
  throw new Error(`Machine expected target snapshot path to be "${target.path}"`)
@@ -1110,7 +1112,7 @@ export const normalizeTargetConfigurationSync = <const States extends Machine.St
1110
1112
  machine,
1111
1113
  current,
1112
1114
  snapshot,
1113
- target.values as Readonly<Record<string, unknown>> | undefined
1115
+ target.values
1114
1116
  )
1115
1117
  }
1116
1118
  return configurationFromTargetPathSync(
@@ -1118,7 +1120,7 @@ export const normalizeTargetConfigurationSync = <const States extends Machine.St
1118
1120
  current,
1119
1121
  target.path,
1120
1122
  target.value,
1121
- target.values as Readonly<Record<string, unknown>> | undefined
1123
+ target.values
1122
1124
  )
1123
1125
  }
1124
1126
  if (isSnapshot(target)) {
@@ -1363,10 +1365,7 @@ export const completeConfigurationEffect: <
1363
1365
  ) {
1364
1366
  yield* completeActiveFinalNodeEffect(machine, completed, path, event, outputs, completions)
1365
1367
  }
1366
- return { configuration: completed, completions } as {
1367
- readonly configuration: ActiveConfiguration
1368
- readonly completions: ReadonlyArray<FinalCompletion>
1369
- }
1368
+ return { configuration: completed, completions }
1370
1369
  })
1371
1370
 
1372
1371
  const resolveFinalOutputSync = <const Events extends ReadonlyArray<Machine.TaggedSchema>>(
@@ -129,7 +129,7 @@ export const snapshot = (nodes: Machine.StateNodes, raw: unknown, path = ""): un
129
129
  const node = nodes.byPath.get(path)!
130
130
  target(nodes, path, config)
131
131
  const value = nodeValue(node, config)
132
- if (node.type === "choice") return Topology.makeChoiceTarget(path, node.parent!)
132
+ if (node.type === "choice") return Topology.makeChoiceTarget(path, node.parent)
133
133
  if (node.type === "atomic" || node.type === "final") return { path, value }
134
134
  if (config.states === undefined) {
135
135
  throw new Error("Machine history fallback requires a complete explicit tree")
@@ -19,7 +19,6 @@ export interface Declaration {
19
19
  /** Declared state paths keyed by internal identifier; the root is `""`. */
20
20
  readonly paths: ReadonlyMap<string, PathKind>
21
21
  readonly sources: ReadonlyMap<string, Source>
22
- readonly branches: ReadonlyMap<string, Readonly<Record<string, Readonly<Record<string, unknown>>>>>
23
22
  }
24
23
  const record = (value: unknown, message: string): Record<string, unknown> => {
25
24
  if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error(message)
@@ -56,43 +55,7 @@ export const capture = (
56
55
  sources.set(name, Object.freeze({ kind, value }))
57
56
  }
58
57
  }
59
- const branches = new Map<string, Readonly<Record<string, Readonly<Record<string, unknown>>>>>()
60
- if (config.branches !== undefined) {
61
- for (const [name, group] of Object.entries(record(config.branches, "Machine branches must be a map"))) {
62
- const branchRecord = record(group, `Machine branches "${name}" must be a branch record`)
63
- for (const key of Reflect.ownKeys(branchRecord)) {
64
- if (typeof key !== "string") throw new Error("Machine branches cannot use symbol keys")
65
- if (key.length === 0 || String(Number(key)) === key) {
66
- throw new Error("Machine branches require non-index string branch keys")
67
- }
68
- }
69
- const entries = Object.entries(branchRecord)
70
- if (entries.length === 0) throw new Error(`Machine branches "${name}" requires a branch`)
71
- branches.set(
72
- name,
73
- Object.freeze(Object.fromEntries(entries.map(([key, value]) => {
74
- const spec = { ...record(value, `Machine branch "${name}.${key}" must be a declaration`) }
75
- for (const field of Reflect.ownKeys(spec)) {
76
- if (
77
- typeof field !== "string" ||
78
- !["target", "update", "history", "none", "initialize", "title"].includes(field)
79
- ) {
80
- throw new Error(`Machine branch "${name}.${key}" contains an unknown declaration field`)
81
- }
82
- }
83
- if (spec.initialize !== undefined && spec.initialize !== true) {
84
- throw new Error(`Machine branch "${name}.${key}" declares initialize: true; its resolver supplies input`)
85
- }
86
- if (spec.title !== undefined && (typeof spec.title !== "string" || spec.title.length === 0)) {
87
- throw new Error("Machine branch title must be a non-empty string")
88
- }
89
- selection({ root, input, paths, sources, branches }, spec)
90
- return [key, Object.freeze(spec)]
91
- })))
92
- )
93
- }
94
- }
95
- return { root, input, paths, sources, branches }
58
+ return { root, input, paths, sources }
96
59
  }
97
60
  const reference = (
98
61
  value: unknown,
@@ -105,26 +68,38 @@ const reference = (
105
68
  }
106
69
  return { path, kind }
107
70
  }
71
+ /** The single owner named by a transition's `update` record. */
72
+ export const updateOwner = (update: unknown): string => {
73
+ const keys = record(update, "Machine update must be a record with one owner")
74
+ const owners = Object.keys(keys)
75
+ if (owners.length !== 1 || Object.getOwnPropertySymbols(keys).length > 0) {
76
+ throw new Error(`Machine update takes one owner, got: ${owners.join(", ")}`)
77
+ }
78
+ return owners[0]!
79
+ }
108
80
  export const selection = (
109
81
  declaration: Declaration,
110
82
  config: Readonly<Record<string, unknown>>
111
83
  ): Topology.TargetSelection => {
112
- const keys = ["target", "history", "none", "initialize"].filter((key) => config[key] !== undefined)
113
- if (keys.length === 0 && config.update !== undefined) {
114
- const owner = reference(config.update, declaration)
115
- if (owner.kind !== "state") throw new Error("Machine update requires an active state reference")
116
- return Topology.makeTargetSelection("update", owner.path, "branch")
84
+ const keys = ["target", "history", "stay", "initialize"].filter((key) => config[key] !== undefined)
85
+ const owner = typeof config.update === "string" || config.update === undefined
86
+ ? config.update
87
+ : updateOwner(config.update)
88
+ if (keys.length === 0 && owner !== undefined) {
89
+ const reference_ = reference(owner, declaration)
90
+ if (reference_.kind !== "state") throw new Error("Machine update requires an active state reference")
91
+ return Topology.makeTargetSelection("update", reference_.path, "branch")
117
92
  }
93
+ if (keys.length === 0 && config.reenter === true) return Topology.noneTargetSelection
118
94
  if (keys.length !== 1) throw new Error("Machine transition must declare exactly one destination operation")
119
95
  const key = keys[0]!
120
- if (key === "none") {
121
- if (config.none !== true || config.update !== undefined) {
122
- throw new Error("Machine targetless transition requires none: true")
123
- }
96
+ if (key === "stay") {
97
+ if (config.stay !== true || owner !== undefined) throw new Error("Machine stay requires stay: true alone")
98
+ if (config.reenter !== undefined) throw new Error("Machine stay cannot reenter; use reenter: true alone")
124
99
  return Topology.noneTargetSelection
125
100
  }
126
101
  if (key === "initialize") {
127
- if (config.update !== undefined) throw new Error("Machine initialize cannot update a retained owner")
102
+ if (owner !== undefined) throw new Error("Machine initialize cannot update a retained owner")
128
103
  return Topology.makeTargetSelection("state", "", "branch")
129
104
  }
130
105
  const ref = reference(config[key], declaration)
@@ -134,7 +109,7 @@ export const selection = (
134
109
  if ((key === "history") !== (ref.kind === "history")) {
135
110
  throw new Error("Machine history references require a history transition")
136
111
  }
137
- const update = config.update === undefined ? undefined : reference(config.update, declaration).path
112
+ const update = owner === undefined ? undefined : reference(owner, declaration).path
138
113
  if (update !== undefined && key !== "target") throw new Error("Machine owner updates require an ordinary destination")
139
114
  return Topology.makeTargetSelection(
140
115
  ref.kind,
@@ -161,7 +136,7 @@ export const invocation = (
161
136
  !(source.kind === "effects" && Effect.isEffect(source.value)) &&
162
137
  !(source.kind === "streams" && Stream.isStream(source.value))
163
138
  if (parameterized !== (typeof input === "function")) {
164
- throw new Error(`Machine invocation "${src}" requires input exactly when its source takes an argument`)
139
+ throw new Error(`Machine invocation "${String(src)}" requires input exactly when its source takes an argument`)
165
140
  }
166
141
  const resolve = parameterized
167
142
  ? (context: unknown) =>