@typeonce/effect-machine 0.8.0 → 0.10.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 (108) hide show
  1. package/README.md +180 -40
  2. package/dist/Machine.d.ts +659 -481
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +86 -222
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/activities.d.ts +5 -12
  7. package/dist/internal/machine/activities.d.ts.map +1 -1
  8. package/dist/internal/machine/activities.js +47 -17
  9. package/dist/internal/machine/activities.js.map +1 -1
  10. package/dist/internal/machine/atom.d.ts +6 -2
  11. package/dist/internal/machine/atom.d.ts.map +1 -1
  12. package/dist/internal/machine/atom.js +5 -0
  13. package/dist/internal/machine/atom.js.map +1 -1
  14. package/dist/internal/machine/cluster.d.ts.map +1 -1
  15. package/dist/internal/machine/cluster.js +2 -1
  16. package/dist/internal/machine/cluster.js.map +1 -1
  17. package/dist/internal/machine/command.js +2 -2
  18. package/dist/internal/machine/command.js.map +1 -1
  19. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  20. package/dist/internal/machine/commandRuntime.js +6 -3
  21. package/dist/internal/machine/commandRuntime.js.map +1 -1
  22. package/dist/internal/machine/configuration.d.ts +8 -1
  23. package/dist/internal/machine/configuration.d.ts.map +1 -1
  24. package/dist/internal/machine/configuration.js +9 -4
  25. package/dist/internal/machine/configuration.js.map +1 -1
  26. package/dist/internal/machine/errors.d.ts +1 -1
  27. package/dist/internal/machine/errors.d.ts.map +1 -1
  28. package/dist/internal/machine/executionPlan.d.ts +3 -3
  29. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  30. package/dist/internal/machine/executionPlan.js +74 -24
  31. package/dist/internal/machine/executionPlan.js.map +1 -1
  32. package/dist/internal/machine/invocation.d.ts +10 -2
  33. package/dist/internal/machine/invocation.d.ts.map +1 -1
  34. package/dist/internal/machine/invocation.js +94 -34
  35. package/dist/internal/machine/invocation.js.map +1 -1
  36. package/dist/internal/machine/invocationEvent.d.ts +39 -0
  37. package/dist/internal/machine/invocationEvent.d.ts.map +1 -0
  38. package/dist/internal/machine/invocationEvent.js +43 -0
  39. package/dist/internal/machine/invocationEvent.js.map +1 -0
  40. package/dist/internal/machine/machine.d.ts +16 -65
  41. package/dist/internal/machine/machine.d.ts.map +1 -1
  42. package/dist/internal/machine/machine.js +18 -73
  43. package/dist/internal/machine/machine.js.map +1 -1
  44. package/dist/internal/machine/planner.d.ts +4 -2
  45. package/dist/internal/machine/planner.d.ts.map +1 -1
  46. package/dist/internal/machine/planner.js +104 -24
  47. package/dist/internal/machine/planner.js.map +1 -1
  48. package/dist/internal/machine/process.d.ts.map +1 -1
  49. package/dist/internal/machine/process.js +8 -8
  50. package/dist/internal/machine/process.js.map +1 -1
  51. package/dist/internal/machine/protocol.d.ts +8 -5
  52. package/dist/internal/machine/protocol.d.ts.map +1 -1
  53. package/dist/internal/machine/protocol.js +207 -15
  54. package/dist/internal/machine/protocol.js.map +1 -1
  55. package/dist/internal/machine/runtime.d.ts +8 -2
  56. package/dist/internal/machine/runtime.d.ts.map +1 -1
  57. package/dist/internal/machine/runtime.js +86 -8
  58. package/dist/internal/machine/runtime.js.map +1 -1
  59. package/dist/internal/machine/symbols.d.ts +2 -0
  60. package/dist/internal/machine/symbols.d.ts.map +1 -1
  61. package/dist/internal/machine/symbols.js +2 -0
  62. package/dist/internal/machine/symbols.js.map +1 -1
  63. package/dist/internal/machine/topology.d.ts +7 -0
  64. package/dist/internal/machine/topology.d.ts.map +1 -1
  65. package/dist/internal/machine/topology.js +29 -0
  66. package/dist/internal/machine/topology.js.map +1 -1
  67. package/dist/internal/testing/machine/finiteModel.js +1 -1
  68. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  69. package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
  70. package/dist/internal/testing/machine/runtime.js +2 -1
  71. package/dist/internal/testing/machine/runtime.js.map +1 -1
  72. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  73. package/dist/internal/testing/machine/verification.js +5 -3
  74. package/dist/internal/testing/machine/verification.js.map +1 -1
  75. package/dist/testing/MachineTest.d.ts +7 -7
  76. package/dist/testing/MachineTest.js +7 -7
  77. package/dist/unstable/cluster/ClusterMachine.d.ts +1 -1
  78. package/dist/unstable/cluster/ClusterMachine.js +1 -1
  79. package/dist/unstable/reactivity/AtomMachine.d.ts +25 -8
  80. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  81. package/dist/unstable/reactivity/AtomMachine.js +18 -3
  82. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  83. package/docs/agent-guide.md +242 -122
  84. package/package.json +2 -2
  85. package/src/Machine.ts +1453 -812
  86. package/src/internal/machine/activities.ts +48 -33
  87. package/src/internal/machine/atom.ts +38 -13
  88. package/src/internal/machine/cluster.ts +2 -1
  89. package/src/internal/machine/command.ts +2 -2
  90. package/src/internal/machine/commandRuntime.ts +9 -4
  91. package/src/internal/machine/configuration.ts +23 -5
  92. package/src/internal/machine/errors.ts +1 -1
  93. package/src/internal/machine/executionPlan.ts +102 -28
  94. package/src/internal/machine/invocation.ts +182 -50
  95. package/src/internal/machine/invocationEvent.ts +72 -0
  96. package/src/internal/machine/machine.ts +80 -387
  97. package/src/internal/machine/planner.ts +127 -32
  98. package/src/internal/machine/process.ts +18 -8
  99. package/src/internal/machine/protocol.ts +313 -25
  100. package/src/internal/machine/runtime.ts +135 -9
  101. package/src/internal/machine/symbols.ts +3 -0
  102. package/src/internal/machine/topology.ts +38 -0
  103. package/src/internal/testing/machine/finiteModel.ts +1 -1
  104. package/src/internal/testing/machine/runtime.ts +2 -1
  105. package/src/internal/testing/machine/verification.ts +5 -3
  106. package/src/testing/MachineTest.ts +7 -7
  107. package/src/unstable/cluster/ClusterMachine.ts +1 -1
  108. package/src/unstable/reactivity/AtomMachine.ts +45 -13
@@ -7,7 +7,7 @@
7
7
  import * as Cause from "effect/Cause"
8
8
  import * as Effect from "effect/Effect"
9
9
  import type * as Schema from "effect/Schema"
10
- import type { Enqueue, InitialEvent as MachineInitialEvent, Machine } from "../../Machine.js"
10
+ import type { ActorContext, ActorRef, Enqueue, InitialEvent as MachineInitialEvent, Machine } from "../../Machine.js"
11
11
  import { getTargetBuilder, makeCollector, type RuntimeCommand } from "./command.js"
12
12
  import {
13
13
  type ActiveConfiguration,
@@ -37,13 +37,15 @@ import {
37
37
  snapshotFromConfigurationAtPath,
38
38
  validateInitialConfiguration
39
39
  } from "./configuration.js"
40
- import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError } from "./errors.js"
40
+ import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError, StoppedError } from "./errors.js"
41
+ import * as InvocationEvent from "./invocationEvent.js"
41
42
  import { decodeEventSync, decodeInputSync, decodeStateValueSync } from "./protocol.js"
42
43
  import { InitialEventTypeId } from "./symbols.js"
43
44
  import {
44
45
  getNode,
45
46
  isChoiceTarget,
46
47
  isHistoryTarget,
48
+ isNoTarget,
47
49
  isSnapshot,
48
50
  isTarget,
49
51
  makeChoiceTarget,
@@ -92,6 +94,38 @@ export type TransitionHandler<States extends Machine.StateSchemas, E, R, Context
92
94
  enqueue: Enqueue<any, any>
93
95
  ) => Machine.HandlerResult<States, E, R>
94
96
 
97
+ const rootActorScopes = new WeakMap<Machine.Any, ActorContext<any, any>>()
98
+ const machineActorScopes = new WeakMap<Machine.Any, ActorContext<any, any>>()
99
+
100
+ export const withActorScope = (
101
+ machine: Machine.Any,
102
+ actorScope: ActorContext<any, any>
103
+ ): Machine.Any => {
104
+ const scoped = Object.create(machine) as Machine.Any
105
+ machineActorScopes.set(scoped, { self: actorScope.self, parent: actorScope.parent })
106
+ return scoped
107
+ }
108
+
109
+ const getActorContext = (
110
+ machine: Machine.Any,
111
+ configuration: ActiveConfiguration
112
+ ): ActorContext<any, any> => {
113
+ const scope = configuration.actorScope
114
+ if (scope !== undefined) return scope
115
+ const machineScope = machineActorScopes.get(machine)
116
+ if (machineScope !== undefined) return machineScope
117
+ const cached = rootActorScopes.get(machine)
118
+ if (cached !== undefined) return cached
119
+ const self: ActorRef<unknown> = {
120
+ id: machine.id ?? "Machine",
121
+ sessionId: "Machine.plan",
122
+ send: () => Effect.fail(new StoppedError())
123
+ }
124
+ const root = { self, parent: undefined }
125
+ rootActorScopes.set(machine, root)
126
+ return root
127
+ }
128
+
95
129
  type EventTransition<States extends Machine.StateSchemas, E, R, Context> =
96
130
  | TransitionHandler<States, E, R, Context>
97
131
  | {
@@ -154,9 +188,9 @@ const collectTransition = <
154
188
  context: Context
155
189
  ) => {
156
190
  const collected = makeCollector<Event>(machine)
157
- const state = transition(context, collected.enqueue)
191
+ const result = transition(context, collected.enqueue)
158
192
  return {
159
- state,
193
+ state: isNoTarget(result) ? undefined : result,
160
194
  commands: collected.commands,
161
195
  raisedEvents: collected.raisedEvents,
162
196
  emittedEvents: collected.emittedEvents
@@ -205,7 +239,8 @@ const completeHistoryConfiguration = (
205
239
  active,
206
240
  values,
207
241
  outputs: configuration.outputs,
208
- history: configuration.history
242
+ history: configuration.history,
243
+ actorScope: configuration.actorScope
209
244
  } as ActiveConfiguration
210
245
  active.add(child.path)
211
246
  if (child.schema !== undefined) {
@@ -214,9 +249,10 @@ const completeHistoryConfiguration = (
214
249
  throw new Error(`Machine shallow history requires an initial value implementation for state "${path}"`)
215
250
  }
216
251
  const initialized = collectStateInitializer(machine, initializer, {
252
+ ...getActorContext(machine, current),
217
253
  state: current.values.get(path),
218
- parent: getParentValue(machine, current, path),
219
- parents: getParentValues(machine, current, path),
254
+ containingState: getParentValue(machine, current, path),
255
+ ancestors: getParentValues(machine, current, path),
220
256
  event
221
257
  })
222
258
  values.set(child.path, decodeStateValueSync(machine, child, initialized.value))
@@ -234,16 +270,18 @@ const completeHistoryConfiguration = (
234
270
  active,
235
271
  values,
236
272
  outputs: configuration.outputs,
237
- history: configuration.history
273
+ history: configuration.history,
274
+ actorScope: configuration.actorScope
238
275
  } as ActiveConfiguration
239
276
  const initializer = valuedMissing.length === 0 ? undefined : machine.handlers[path]?.initial
240
277
  if (valuedMissing.length > 0 && initializer === undefined) {
241
278
  throw new Error(`Machine shallow history requires an initial value implementation for state "${path}"`)
242
279
  }
243
280
  const initialized = initializer === undefined ? undefined : collectStateInitializer(machine, initializer, {
281
+ ...getActorContext(machine, current),
244
282
  state: current.values.get(path),
245
- parent: getParentValue(machine, current, path),
246
- parents: getParentValues(machine, current, path),
283
+ containingState: getParentValue(machine, current, path),
284
+ ancestors: getParentValues(machine, current, path),
247
285
  event
248
286
  })
249
287
  if (initialized !== undefined && (typeof initialized.value !== "object" || initialized.value === null)) {
@@ -327,7 +365,7 @@ const resolveHistoryTarget = (
327
365
  const collected = collectTransition(machine, fallback, {
328
366
  event,
329
367
  target: getTargetBuilder(machine, target.parent).full,
330
- parent: target.parent
368
+ owner: target.parent
331
369
  })
332
370
  if (collected.state === undefined || isHistoryTarget(collected.state) || !isSnapshot(collected.state)) {
333
371
  throw new Error(`Machine history default for "${target.path}" must return a complete snapshot containing its owner`)
@@ -504,9 +542,10 @@ const makeStateActionContext = <
504
542
  path: string,
505
543
  event: Machine.LifecycleEvent<Events>
506
544
  ): Machine.StateActionContext<States, Events, Emits, StateId> => ({
545
+ ...getActorContext(machine, configuration),
507
546
  state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
508
- parent: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
509
- parents: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
547
+ containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
548
+ ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
510
549
  event
511
550
  })
512
551
 
@@ -523,9 +562,10 @@ const makeTransitionContext = <
523
562
  event: Machine.EventByTag<Events, EventTag>,
524
563
  snapshot: Machine.Snapshot<States>
525
564
  ): Machine.HandlerContext<States, Events, Emits, StateId, EventTag, any, any> => ({
565
+ ...getActorContext(machine, configuration),
526
566
  state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
527
- parent: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
528
- parents: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
567
+ containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
568
+ ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
529
569
  event,
530
570
  snapshot,
531
571
  target: getTargetBuilder(machine, path)
@@ -544,9 +584,10 @@ const makeDoneContext = <
544
584
  output: unknown,
545
585
  snapshot: Machine.Snapshot<States>
546
586
  ): Machine.DoneContext<States, Events, Emits, StateId> => ({
587
+ ...getActorContext(machine, configuration),
547
588
  state: configuration.values.get(path) as Machine.StateByIdentifier<States, StateId>,
548
- parent: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
549
- parents: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
589
+ containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
590
+ ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
550
591
  event,
551
592
  output: output as Machine.CompletionOutputByIdentifier<States, StateId>,
552
593
  snapshot,
@@ -638,15 +679,16 @@ const selectAlwaysTransitions = <
638
679
  Machine.AlwaysContext<States, Events, Emits, Machine.StateIdentifier<States>>
639
680
  >,
640
681
  context: {
682
+ ...getActorContext(machine, configuration),
641
683
  state: configuration.values.get(path) as Machine.StateByIdentifier<
642
684
  States,
643
685
  Machine.StateIdentifier<States>
644
686
  >,
645
- parent: getParentValue(machine, configuration, path) as Machine.ParentStateValue<
687
+ containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<
646
688
  States,
647
689
  Machine.StateIdentifier<States>
648
690
  >,
649
- parents: getParentValues(machine, configuration, path) as Machine.ParentStateValues<
691
+ ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<
650
692
  States,
651
693
  Machine.StateIdentifier<States>
652
694
  >,
@@ -798,6 +840,55 @@ const selectEventTransitions = <
798
840
  return selected
799
841
  }
800
842
 
843
+ const selectInvocationTransition = <
844
+ const States extends Machine.StateSchemas,
845
+ const Events extends ReadonlyArray<Machine.TaggedSchema>,
846
+ const Emits extends ReadonlyArray<Machine.TaggedSchema>,
847
+ E,
848
+ R
849
+ >(
850
+ machine: Machine.Any,
851
+ configuration: ActiveConfiguration,
852
+ event: InvocationEvent.InvocationEvent
853
+ ): ReadonlyArray<SelectedTransition<States, E, R, any>> => {
854
+ if (!configuration.active.has(event.path)) return []
855
+ const config = machine.handlers[event.path] as Machine.AnyStateConfig | undefined
856
+ const invoke = InvocationEvent.definitions(config?.invoke).find((definition) => {
857
+ const id = "child" in definition ? definition.child?.id : definition.id
858
+ return String(id) === event.id
859
+ })
860
+ if (invoke === undefined) return []
861
+ const handler = event.type === "done"
862
+ ? invoke.onDone
863
+ : event.type === "failure"
864
+ ? invoke.onFailure
865
+ : invoke.onSnapshot
866
+ const transition = normalizeTransition(handler)
867
+ if (transition === undefined) return []
868
+ const snapshot = snapshotFromConfiguration<States>(machine, configuration)
869
+ const context = {
870
+ ...getActorContext(machine, configuration),
871
+ state: configuration.values.get(event.path),
872
+ containingState: getParentValue(machine, configuration, event.path),
873
+ ancestors: getParentValues(machine, configuration, event.path),
874
+ snapshot,
875
+ target: getTargetBuilder(machine, event.path),
876
+ id: event.id,
877
+ ...(event.type === "done"
878
+ ? { output: event.output }
879
+ : event.type === "failure"
880
+ ? { error: event.error }
881
+ : { snapshot: event.snapshot })
882
+ }
883
+ return [{
884
+ sourcePath: event.path,
885
+ leafPath: getActiveLeafPathFrom(machine, configuration, event.path),
886
+ trigger: { type: "invoke", id: event.id, outcome: event.type },
887
+ transition: transition as unknown as MicrostepTransition<States, E, R, any>,
888
+ context
889
+ }]
890
+ }
891
+
801
892
  export const getTargetNodePath = <const States extends Machine.StateSchemas>(
802
893
  target:
803
894
  | Machine.Snapshot<States>
@@ -1015,11 +1106,13 @@ const resolveChoiceTarget = (
1015
1106
  ...Object.entries(extracted.values)
1016
1107
  ]),
1017
1108
  outputs: configuration.outputs,
1018
- history: configuration.history
1109
+ history: configuration.history,
1110
+ ...(configuration.actorScope === undefined ? {} : { actorScope: configuration.actorScope })
1019
1111
  }
1020
1112
  const collected = collectTransition(machine, choice.transition, {
1021
- parent: getParentValue(machine, provisional, node.path),
1022
- parents: getParentValues(machine, provisional, node.path),
1113
+ ...getActorContext(machine, provisional),
1114
+ containingState: getParentValue(machine, provisional, node.path),
1115
+ ancestors: getParentValues(machine, provisional, node.path),
1023
1116
  event,
1024
1117
  target: getTargetBuilder(machine, node.path)
1025
1118
  })
@@ -1812,11 +1905,11 @@ const macrostepConfiguration = <
1812
1905
  >(
1813
1906
  machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
1814
1907
  configuration: ActiveConfiguration,
1815
- event: Machine.EventOf<Events>
1908
+ event: Machine.EventOf<Events> | InvocationEvent.InvocationEvent
1816
1909
  ) => {
1817
- const decodedEvent = decodeEventSync<Events>(machine, event)
1910
+ const decodedEvent = InvocationEvent.isInvocationEvent(event) ? event : decodeEventSync<Events>(machine, event)
1818
1911
  if (isActiveFinalConfiguration(machine, configuration)) {
1819
- const completed = completeConfigurationSync(machine, configuration, decodedEvent)
1912
+ const completed = completeConfigurationSync(machine, configuration, decodedEvent as any)
1820
1913
  const root = getRootPath(machine, completed.configuration)
1821
1914
  if (!completed.configuration.outputs.has(root)) {
1822
1915
  throw new Error("Machine reached a terminal configuration without a completed root output")
@@ -1831,11 +1924,13 @@ const macrostepConfiguration = <
1831
1924
  }
1832
1925
  }
1833
1926
 
1834
- const selections = selectEventTransitions<States, Events, Emits, E, R>(
1835
- machine,
1836
- configuration,
1837
- decodedEvent as Machine.EventByTag<Events, Machine.TagOf<Events[number]>>
1838
- )
1927
+ const selections = InvocationEvent.isInvocationEvent(decodedEvent)
1928
+ ? selectInvocationTransition<States, Events, Emits, E, R>(machine, configuration, decodedEvent)
1929
+ : selectEventTransitions<States, Events, Emits, E, R>(
1930
+ machine,
1931
+ configuration,
1932
+ decodedEvent as Machine.EventByTag<Events, Machine.TagOf<Events[number]>>
1933
+ )
1839
1934
  if (selections.length === 0) {
1840
1935
  return {
1841
1936
  next: configuration,
@@ -1849,14 +1944,14 @@ const macrostepConfiguration = <
1849
1944
  const step = microstep(
1850
1945
  machine,
1851
1946
  configuration,
1852
- decodedEvent,
1947
+ decodedEvent as any,
1853
1948
  selections
1854
1949
  )
1855
1950
  const commands = [...step.commands]
1856
1951
  const raisedEvents = [...step.raisedEvents]
1857
1952
  const emittedEvents = [...step.emittedEvents]
1858
1953
  const microsteps = [step]
1859
- return settle(machine, step.next, decodedEvent, commands, raisedEvents, emittedEvents, microsteps)
1954
+ return settle(machine, step.next, decodedEvent as any, commands, raisedEvents, emittedEvents, microsteps)
1860
1955
  }
1861
1956
 
1862
1957
  const snapshotMacrostep = <
@@ -131,7 +131,8 @@ const makeChildlessCompiledDrain = (
131
131
  planned = executionPlan.plan(
132
132
  configuration ?? executionPlan.fromConfiguration(Configuration.normalizeConfigurationSync(machine, current)),
133
133
  event,
134
- acknowledged
134
+ acknowledged,
135
+ context.scope
135
136
  )
136
137
  } catch (error) {
137
138
  return error instanceof InfiniteTransitionError || error instanceof MachineSchemaDecodeError
@@ -255,7 +256,8 @@ const makeInvokingCompiledDrain = (
255
256
  configuration ??
256
257
  executionPlan.fromConfiguration(Configuration.normalizeConfigurationSync(machine, current)),
257
258
  event,
258
- acknowledged
259
+ acknowledged,
260
+ scope
259
261
  )
260
262
  } catch (error) {
261
263
  return error instanceof InfiniteTransitionError || error instanceof MachineSchemaDecodeError
@@ -426,9 +428,11 @@ const makeProcessLogic: <
426
428
  const executionPlan = ExecutionPlan.compileExecutionPlan(machine)
427
429
  const initialArgs = entry._tag === "Initial" ? entry.args : []
428
430
  const compiledInitial = entry._tag === "Initial" ? executionPlan.initial : undefined
429
- const makeCompiledInitial = compiledInitial === undefined ? undefined : () => {
431
+ const makeCompiledInitial = compiledInitial === undefined ? undefined : (
432
+ scope: internalRuntime.ProcessScope<Machine.EventOf<Events>>
433
+ ) => {
430
434
  try {
431
- const planned = compiledInitial(initialArgs)
435
+ const planned = compiledInitial(initialArgs, scope)
432
436
  const result = {
433
437
  state: planned.state as Machine.Snapshot<States>,
434
438
  done: planned.done,
@@ -455,7 +459,7 @@ const makeProcessLogic: <
455
459
  ) =>
456
460
  compiledInitial === undefined
457
461
  ? internalRuntime.provideMachineRuntime(
458
- internalPlanner.planInitial(machine, ...initialArgs).pipe(
462
+ internalPlanner.planInitial(internalPlanner.withActorScope(machine, scope), ...initialArgs).pipe(
459
463
  Effect.flatMap((planned) => {
460
464
  const commands = planned.commands.length === 0
461
465
  ? undefined
@@ -480,7 +484,7 @@ const makeProcessLogic: <
480
484
  ),
481
485
  scope
482
486
  )
483
- : Effect.try({ try: makeCompiledInitial!, catch: (error) => error as any })
487
+ : Effect.try({ try: () => makeCompiledInitial!(scope), catch: (error) => error as any })
484
488
  return ({
485
489
  execution: {
486
490
  _tag: "Compiled",
@@ -536,7 +540,10 @@ const makeProcessLogic: <
536
540
  try {
537
541
  planned = internalPlanner.planConfiguration(
538
542
  machine,
539
- configuration ?? Configuration.normalizeConfigurationSync(machine, current),
543
+ Configuration.withActorScope(
544
+ configuration ?? Configuration.normalizeConfigurationSync(machine, current),
545
+ context
546
+ ),
540
547
  event
541
548
  )
542
549
  } catch (error) {
@@ -644,7 +651,10 @@ const makeProcessLogic: <
644
651
  try {
645
652
  planned = internalPlanner.planConfiguration(
646
653
  machine,
647
- configuration ?? Configuration.normalizeConfigurationSync(machine, current),
654
+ Configuration.withActorScope(
655
+ configuration ?? Configuration.normalizeConfigurationSync(machine, current),
656
+ context
657
+ ),
648
658
  event
649
659
  )
650
660
  } catch (error) {