@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
@@ -7,7 +7,7 @@
7
7
  import * as Cause from "effect/Cause"
8
8
  import * as Effect from "effect/Effect"
9
9
  import * as Stream from "effect/Stream"
10
- import type { ChildMachine, ChildOwner, Inspection, Logic, Machine } from "../../Machine.js"
10
+ import type { ChildMachine, ChildOwner, Inspection, Machine } from "../../Machine.js"
11
11
  import * as ChildRegistry from "./childRegistry.js"
12
12
  import * as Configuration from "./configuration.js"
13
13
  import { ChildAlreadyExistsError, InfiniteTransitionError, MachineSchemaDecodeError, StoppedError } from "./errors.js"
@@ -15,7 +15,7 @@ import * as InvocationDefinition from "./invocationDefinition.js"
15
15
  import * as InvocationEvent from "./invocationEvent.js"
16
16
  import * as Planner from "./planner.js"
17
17
  import * as Runtime from "./runtimeProtocol.js"
18
- import { ChildMachineLogicTypeId } from "./symbols.js"
18
+ import { type ChildMachineLogicSlot, ChildMachineLogicTypeId } from "./symbols.js"
19
19
 
20
20
  /** @internal */
21
21
  export interface AnyConfig {
@@ -37,7 +37,9 @@ export const makeKey = (path: string, id: string): string => `${path.length}:${p
37
37
  export const makeChildId = (path: string, id: string): string =>
38
38
  `${ChildRegistry.invocationChildIdPrefix}${makeKey(path, id)}`
39
39
 
40
- const oneShot = (effect: Effect.Effect<any, any, any>): Logic<void, never, any, any, any> => ({
40
+ const oneShot = (
41
+ effect: Effect.Effect<unknown, unknown, unknown>
42
+ ): Runtime.ProcessLogic<void, unknown, unknown, unknown, unknown> => ({
41
43
  initial: () => Effect.void,
42
44
  run: () => effect
43
45
  })
@@ -45,8 +47,9 @@ const oneShot = (effect: Effect.Effect<any, any, any>): Logic<void, never, any,
45
47
  const streamLogic = (
46
48
  stream: Stream.Stream<any, any, any>,
47
49
  path: string,
48
- id: string
49
- ): Runtime.ProcessLogic<void, never, any, any, void> => ({
50
+ id: string,
51
+ generation: InvocationEvent.Generation
52
+ ): Runtime.ProcessLogic<void, unknown, unknown, unknown, void> => ({
50
53
  initial: () => Effect.void,
51
54
  run: ({ parent }) => {
52
55
  const send = parent?.[Runtime.acknowledgedSend]
@@ -56,7 +59,7 @@ const streamLogic = (
56
59
  return Stream.runForEach(
57
60
  stream,
58
61
  (element) =>
59
- send(InvocationEvent.element(path, id, element)).pipe(
62
+ send(InvocationEvent.element(path, id, generation, element)).pipe(
60
63
  Effect.asVoid,
61
64
  Effect.catchCause(() => Effect.interrupt)
62
65
  )
@@ -68,33 +71,25 @@ const resolveValue = (value: unknown, context: Omit<Machine.InvokeContext<any, a
68
71
  typeof value === "function" ? value(context) : value
69
72
 
70
73
  const makeChildOwner = (scope: Runtime.ProcessScope<any>): ChildOwner<any> => ({
71
- spawn:
72
- ((descriptor: ChildMachine.Any, options?: { readonly input?: unknown }) =>
73
- (scope.spawn as any)(descriptor, options)) as ChildOwner<any>["spawn"],
74
- sendTo: ((descriptor: ChildMachine.Any, event: unknown) => scope.sendTo(descriptor, event)) as ChildOwner<
75
- any
76
- >["sendTo"],
77
- stop: ((descriptor: ChildMachine.Any) => scope.stopChild(descriptor)) as ChildOwner<any>["stop"]
74
+ spawn: <const Child extends ChildMachine.Any>(
75
+ child: Child & ChildMachine.Executable<Child> & ChildMachine.ParentCompatibility<Child, any>,
76
+ ...options: ChildMachine.SpawnArgs<Child>
77
+ ) => scope.spawn<Child>(child, ...options),
78
+ sendTo: (descriptor: ChildMachine.Any, event: unknown) => scope.sendTo(descriptor, event),
79
+ stop: (descriptor: ChildMachine.Any) => scope.stopChild(descriptor)
78
80
  })
79
81
 
80
82
  const resolveOne = (
81
83
  raw: InvocationDefinition.InvocationDefinition,
82
84
  context: Omit<Machine.InvokeContext<any, any, any, any>, "root">,
83
- path: string
85
+ path: string,
86
+ generation: InvocationEvent.Generation
84
87
  ): AnyConfig => {
85
88
  if ("effect" in raw) {
86
89
  return {
87
90
  id: String(raw.id),
88
91
  sourceName: raw.sourceName,
89
- src: () =>
90
- oneShot(raw.effect(context) as Effect.Effect<any, any, any>) as unknown as Runtime.ProcessLogic<
91
- any,
92
- any,
93
- any,
94
- any,
95
- any,
96
- any
97
- >,
92
+ src: () => oneShot(raw.effect(context)),
98
93
  onDone: raw.onDone,
99
94
  onFailure: raw.onFailure,
100
95
  onSnapshot: raw.onSnapshot,
@@ -105,15 +100,7 @@ const resolveOne = (
105
100
  return {
106
101
  id: String(raw.id),
107
102
  sourceName: raw.sourceName,
108
- src: () =>
109
- oneShot(Effect.sleep(resolveValue(raw.after, context) as any)) as unknown as Runtime.ProcessLogic<
110
- any,
111
- any,
112
- any,
113
- any,
114
- any,
115
- any
116
- >,
103
+ src: () => oneShot(Effect.sleep(typeof raw.after === "function" ? raw.after(context) : raw.after)),
117
104
  onDone: raw.onDone,
118
105
  activityKind: "Timer"
119
106
  }
@@ -123,8 +110,7 @@ const resolveOne = (
123
110
  return {
124
111
  id,
125
112
  sourceName: raw.sourceName,
126
- src: () =>
127
- streamLogic(raw.stream(context), path, id) as unknown as Runtime.ProcessLogic<any, any, any, any, any, any>,
113
+ src: () => streamLogic(raw.stream(context), path, id, generation),
128
114
  onDone: raw.onDone,
129
115
  onFailure: raw.onFailure,
130
116
  activityKind: "Stream"
@@ -147,7 +133,7 @@ const resolveOne = (
147
133
  address: descriptor.id,
148
134
  descriptor,
149
135
  src: () =>
150
- descriptor[ChildMachineLogicTypeId](
136
+ (descriptor as unknown as ChildMachineLogicSlot)[ChildMachineLogicTypeId](
151
137
  "input" in raw ? resolveValue(raw.input, context) : undefined
152
138
  ) as Runtime.ProcessLogic<any, any, any, any, any, any>,
153
139
  onDone: raw.onDone,
@@ -191,6 +177,7 @@ const startResolved = (
191
177
  scope: Runtime.ProcessScope<any>,
192
178
  ownedChildren: Runtime.OwnedChildRuntime,
193
179
  path: string,
180
+ generation: InvocationEvent.Generation,
194
181
  invokeId: string,
195
182
  childId: string,
196
183
  descriptor: ChildMachine.Any | undefined,
@@ -236,39 +223,48 @@ const startResolved = (
236
223
  ...(descriptor === undefined ? undefined : { descriptor }),
237
224
  ...(activityKind === undefined ? undefined : { activityKind }),
238
225
  sendParent: (isCurrent, event) => isCurrent() ? scope.self.send(event) : Effect.void,
239
- onOutcome: (isCurrent, outcome, activitySessionId) => {
240
- if (outcome._tag === "Stopped" || !isCurrent()) return Effect.void
241
- if (outcome._tag === "Done") {
242
- if (onDone === undefined) {
243
- return scope.failCause(Cause.die(
244
- new Error(
245
- `Invocation "${invokeId}" completed without the required onDone handler`
226
+ // The work starts while the event that entered its state is processed,
227
+ // and its outcome is handled in a later turn, even when the work
228
+ // completes without suspending. The entered state is therefore always
229
+ // observed before the outcome leaves it, and the outcome of a state that
230
+ // was exited in between is discarded.
231
+ onOutcome: (isCurrent, outcome, activitySessionId) =>
232
+ Effect.andThen(
233
+ Effect.yieldNow,
234
+ Effect.suspend(() => {
235
+ if (outcome._tag === "Stopped" || !isCurrent()) return Effect.void
236
+ if (outcome._tag === "Done") {
237
+ if (onDone === undefined) {
238
+ return scope.failCause(Cause.die(
239
+ new Error(
240
+ `Invocation "${invokeId}" completed without the required onDone handler`
241
+ )
242
+ ))
243
+ }
244
+ return sendLifecycle(
245
+ scope,
246
+ InvocationEvent.done(path, invokeId, generation, outcome.output),
247
+ activityKind === undefined ? undefined : activitySessionId
246
248
  )
247
- ))
248
- }
249
- return sendLifecycle(
250
- scope,
251
- InvocationEvent.done(path, invokeId, outcome.output),
252
- activityKind === undefined ? undefined : activitySessionId
253
- )
254
- }
255
- if (
256
- outcome._tag === "Failure" &&
257
- onFailure !== undefined &&
258
- (descriptor === undefined || !isFrameworkFailure(outcome.error))
259
- ) {
260
- return sendLifecycle(
261
- scope,
262
- InvocationEvent.failure(path, invokeId, outcome.error),
263
- activityKind === undefined ? undefined : activitySessionId
264
- )
265
- }
266
- return scope.failCause(outcome.cause)
267
- },
249
+ }
250
+ if (
251
+ outcome._tag === "Failure" &&
252
+ onFailure !== undefined &&
253
+ (descriptor === undefined || !isFrameworkFailure(outcome.error))
254
+ ) {
255
+ return sendLifecycle(
256
+ scope,
257
+ InvocationEvent.failure(path, invokeId, generation, outcome.error),
258
+ activityKind === undefined ? undefined : activitySessionId
259
+ )
260
+ }
261
+ return scope.failCause(outcome.cause)
262
+ })
263
+ ),
268
264
  ...(onSnapshot === undefined ? undefined : {
269
265
  onSnapshot: (isCurrent: () => boolean, snapshot: any) =>
270
266
  isCurrent()
271
- ? sendLifecycle(scope, InvocationEvent.snapshot(path, invokeId, snapshot))
267
+ ? sendLifecycle(scope, InvocationEvent.snapshot(path, invokeId, generation, snapshot))
272
268
  : Effect.void
273
269
  })
274
270
  })
@@ -287,6 +283,7 @@ const start = (
287
283
  scope: Runtime.ProcessScope<any>,
288
284
  ownedChildren: Runtime.OwnedChildRuntime,
289
285
  path: string,
286
+ generation: InvocationEvent.Generation,
290
287
  config: AnyConfig
291
288
  ): Effect.Effect<void, any, any> => {
292
289
  const invokeId = String(config.id)
@@ -296,6 +293,7 @@ const start = (
296
293
  scope,
297
294
  ownedChildren,
298
295
  path,
296
+ generation,
299
297
  invokeId,
300
298
  config.address === undefined ? makeChildId(path, invokeId) : String(config.address),
301
299
  config.descriptor,
@@ -312,6 +310,7 @@ const startStaticChild = (
312
310
  scope: Runtime.ProcessScope<any>,
313
311
  ownedChildren: Runtime.OwnedChildRuntime,
314
312
  path: string,
313
+ generation: InvocationEvent.Generation,
315
314
  raw: Extract<InvocationDefinition.InvocationDefinition, { readonly child: ChildMachine.Any }>
316
315
  ): Effect.Effect<void, any, any> => {
317
316
  // A zero-input child has no entry-context dependency. Reuse the descriptor's
@@ -324,10 +323,11 @@ const startStaticChild = (
324
323
  scope,
325
324
  ownedChildren,
326
325
  path,
326
+ generation,
327
327
  descriptor.id,
328
328
  descriptor.id,
329
329
  descriptor,
330
- descriptor[ChildMachineLogicTypeId],
330
+ (descriptor as unknown as ChildMachineLogicSlot)[ChildMachineLogicTypeId],
331
331
  raw.onDone,
332
332
  raw.onFailure,
333
333
  raw.onSnapshot,
@@ -353,6 +353,11 @@ export const startAll = (
353
353
  const effects = Planner.sortEntryPaths(machine, paths)
354
354
  .filter((path) => configuration.active.has(path))
355
355
  .flatMap((path) => {
356
+ const definitions = InvocationDefinition.definitions(Configuration.getStateConfigByPath(machine, path)?.invoke)
357
+ if (definitions.length === 0) return []
358
+ // Every invocation started by this entry shares one generation, which
359
+ // its outcomes carry until the state exits.
360
+ const generation = ownedChildren.beginGeneration(path)
356
361
  const context = {
357
362
  ...(Configuration.getMachineReferences(configuration) ?? { self: scope.self, parent: scope.parent }),
358
363
  children,
@@ -361,12 +366,10 @@ export const startAll = (
361
366
  ancestors: Configuration.getParentValues(machine, configuration, path),
362
367
  event
363
368
  }
364
- return InvocationDefinition.definitions(Configuration.getStateConfigByPath(machine, path)?.invoke).map((
365
- definition
366
- ) =>
369
+ return definitions.map((definition) =>
367
370
  "child" in definition && !("input" in definition)
368
- ? startStaticChild(scope, ownedChildren, path, definition)
369
- : start(scope, ownedChildren, path, resolveOne(definition, context, path))
371
+ ? startStaticChild(scope, ownedChildren, path, generation, definition)
372
+ : start(scope, ownedChildren, path, generation, resolveOne(definition, context, path, generation))
370
373
  )
371
374
  })
372
375
  return effects.length === 0 ? undefined : runSequentialDiscard(effects)
@@ -8,40 +8,56 @@
8
8
  /** @internal */
9
9
  export const InvocationEventTypeId: unique symbol = Symbol("effect/Machine/InvocationEvent")
10
10
 
11
+ /** @internal */
12
+ export const GenerationId: unique symbol = Symbol("effect/Machine/InvocationEvent/Generation")
13
+
14
+ /**
15
+ * One activation of an invoking state.
16
+ *
17
+ * The owned child runtime begins a generation when the state is entered and
18
+ * retires it when the state exits, so `current` is mutable by contract. Every
19
+ * outcome of an invocation started during that activation carries the
20
+ * generation, which lets the planner discard outcomes that were queued before
21
+ * the state exited, even after the same path and id are entered again.
22
+ *
23
+ * @internal
24
+ */
25
+ export interface Generation {
26
+ current: boolean
27
+ }
28
+
29
+ interface Base {
30
+ readonly [InvocationEventTypeId]: true
31
+ // Keyed by a symbol so the runtime handle stays out of inspection payloads,
32
+ // JSON, and canonical value formatting, like the type id above.
33
+ readonly [GenerationId]: Generation
34
+ readonly path: string
35
+ readonly id: string
36
+ }
37
+
11
38
  /** @internal */
12
39
  export type InvocationEvent =
13
- | {
14
- readonly [InvocationEventTypeId]: true
15
- readonly path: string
16
- readonly id: string
40
+ | Base & {
17
41
  readonly type: "element"
18
42
  readonly element: unknown
19
43
  }
20
- | {
21
- readonly [InvocationEventTypeId]: true
22
- readonly path: string
23
- readonly id: string
44
+ | Base & {
24
45
  readonly type: "done"
25
46
  readonly output: unknown
26
47
  }
27
- | {
28
- readonly [InvocationEventTypeId]: true
29
- readonly path: string
30
- readonly id: string
48
+ | Base & {
31
49
  readonly type: "failure"
32
50
  readonly error: unknown
33
51
  }
34
- | {
35
- readonly [InvocationEventTypeId]: true
36
- readonly path: string
37
- readonly id: string
52
+ | Base & {
38
53
  readonly type: "snapshot"
39
54
  readonly snapshot: unknown
40
55
  }
41
56
 
42
57
  /** @internal */
43
- export const done = (path: string, id: string, output: unknown): InvocationEvent => ({
58
+ export const done = (path: string, id: string, generation: Generation, output: unknown): InvocationEvent => ({
44
59
  [InvocationEventTypeId]: true,
60
+ [GenerationId]: generation,
45
61
  path,
46
62
  id,
47
63
  type: "done",
@@ -49,8 +65,9 @@ export const done = (path: string, id: string, output: unknown): InvocationEvent
49
65
  })
50
66
 
51
67
  /** @internal */
52
- export const element = (path: string, id: string, value: unknown): InvocationEvent => ({
68
+ export const element = (path: string, id: string, generation: Generation, value: unknown): InvocationEvent => ({
53
69
  [InvocationEventTypeId]: true,
70
+ [GenerationId]: generation,
54
71
  path,
55
72
  id,
56
73
  type: "element",
@@ -58,8 +75,9 @@ export const element = (path: string, id: string, value: unknown): InvocationEve
58
75
  })
59
76
 
60
77
  /** @internal */
61
- export const failure = (path: string, id: string, error: unknown): InvocationEvent => ({
78
+ export const failure = (path: string, id: string, generation: Generation, error: unknown): InvocationEvent => ({
62
79
  [InvocationEventTypeId]: true,
80
+ [GenerationId]: generation,
63
81
  path,
64
82
  id,
65
83
  type: "failure",
@@ -67,14 +85,22 @@ export const failure = (path: string, id: string, error: unknown): InvocationEve
67
85
  })
68
86
 
69
87
  /** @internal */
70
- export const snapshot = (path: string, id: string, value: unknown): InvocationEvent => ({
88
+ export const snapshot = (path: string, id: string, generation: Generation, value: unknown): InvocationEvent => ({
71
89
  [InvocationEventTypeId]: true,
90
+ [GenerationId]: generation,
72
91
  path,
73
92
  id,
74
93
  type: "snapshot",
75
94
  snapshot: value
76
95
  })
77
96
 
97
+ /**
98
+ * Whether the state activation that started the invocation is still active.
99
+ *
100
+ * @internal
101
+ */
102
+ export const isCurrent = (event: InvocationEvent): boolean => event[GenerationId].current
103
+
78
104
  /** @internal */
79
105
  export const isInvocationEvent = (value: unknown): value is InvocationEvent =>
80
106
  typeof value === "object" && value !== null && InvocationEventTypeId in value