@typeonce/effect-machine 0.33.0 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +272 -628
  2. package/dist/Machine.d.ts +1076 -896
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +141 -90
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/childRegistry.d.ts +1 -1
  7. package/dist/internal/machine/childRegistry.d.ts.map +1 -1
  8. package/dist/internal/machine/cluster.d.ts +2 -50
  9. package/dist/internal/machine/cluster.d.ts.map +1 -1
  10. package/dist/internal/machine/cluster.js +4 -27
  11. package/dist/internal/machine/cluster.js.map +1 -1
  12. package/dist/internal/machine/clusterProtocol.d.ts +35 -0
  13. package/dist/internal/machine/clusterProtocol.d.ts.map +1 -0
  14. package/dist/internal/machine/clusterProtocol.js +28 -0
  15. package/dist/internal/machine/clusterProtocol.js.map +1 -0
  16. package/dist/internal/machine/commandRuntime.d.ts +1 -1
  17. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  18. package/dist/internal/machine/configuration.d.ts +2 -2
  19. package/dist/internal/machine/configuration.d.ts.map +1 -1
  20. package/dist/internal/machine/configuration.js +38 -64
  21. package/dist/internal/machine/configuration.js.map +1 -1
  22. package/dist/internal/machine/declaration.d.ts +17 -0
  23. package/dist/internal/machine/declaration.d.ts.map +1 -0
  24. package/dist/internal/machine/declaration.js +120 -0
  25. package/dist/internal/machine/declaration.js.map +1 -0
  26. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  27. package/dist/internal/machine/executionPlan.js +15 -22
  28. package/dist/internal/machine/executionPlan.js.map +1 -1
  29. package/dist/internal/machine/implementation.d.ts +34 -0
  30. package/dist/internal/machine/implementation.d.ts.map +1 -0
  31. package/dist/internal/machine/implementation.js +3 -0
  32. package/dist/internal/machine/implementation.js.map +1 -0
  33. package/dist/internal/machine/initialDeclaration.d.ts +14 -0
  34. package/dist/internal/machine/initialDeclaration.d.ts.map +1 -0
  35. package/dist/internal/machine/initialDeclaration.js +137 -0
  36. package/dist/internal/machine/initialDeclaration.js.map +1 -0
  37. package/dist/internal/machine/invocation.d.ts +1 -1
  38. package/dist/internal/machine/invocation.d.ts.map +1 -1
  39. package/dist/internal/machine/invocation.js +1 -1
  40. package/dist/internal/machine/invocation.js.map +1 -1
  41. package/dist/internal/machine/invocationDefinition.d.ts +1 -1
  42. package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
  43. package/dist/internal/machine/machine.d.ts +13 -4
  44. package/dist/internal/machine/machine.d.ts.map +1 -1
  45. package/dist/internal/machine/machine.js +169 -388
  46. package/dist/internal/machine/machine.js.map +1 -1
  47. package/dist/internal/machine/planner.d.ts +8 -0
  48. package/dist/internal/machine/planner.d.ts.map +1 -1
  49. package/dist/internal/machine/planner.js +37 -78
  50. package/dist/internal/machine/planner.js.map +1 -1
  51. package/dist/internal/machine/process.d.ts +8 -7
  52. package/dist/internal/machine/process.d.ts.map +1 -1
  53. package/dist/internal/machine/process.js +16 -12
  54. package/dist/internal/machine/process.js.map +1 -1
  55. package/dist/internal/machine/protocol.d.ts +1 -1
  56. package/dist/internal/machine/protocol.d.ts.map +1 -1
  57. package/dist/internal/machine/protocol.js.map +1 -1
  58. package/dist/internal/machine/runtime.d.ts +2 -230
  59. package/dist/internal/machine/runtime.d.ts.map +1 -1
  60. package/dist/internal/machine/runtime.js +8 -1857
  61. package/dist/internal/machine/runtime.js.map +1 -1
  62. package/dist/internal/machine/runtimeCompiled.d.ts +5 -0
  63. package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -0
  64. package/dist/internal/machine/runtimeCompiled.js +867 -0
  65. package/dist/internal/machine/runtimeCompiled.js.map +1 -0
  66. package/dist/internal/machine/runtimeGeneric.d.ts +3 -0
  67. package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -0
  68. package/dist/internal/machine/runtimeGeneric.js +471 -0
  69. package/dist/internal/machine/runtimeGeneric.js.map +1 -0
  70. package/dist/internal/machine/runtimeProtocol.d.ts +312 -0
  71. package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -0
  72. package/dist/internal/machine/runtimeProtocol.js +561 -0
  73. package/dist/internal/machine/runtimeProtocol.js.map +1 -0
  74. package/dist/internal/machine/stateDefinition.d.ts +1 -1
  75. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  76. package/dist/internal/machine/stateDefinition.js +1 -13
  77. package/dist/internal/machine/stateDefinition.js.map +1 -1
  78. package/dist/internal/machine/targetReference.d.ts +15 -0
  79. package/dist/internal/machine/targetReference.d.ts.map +1 -0
  80. package/dist/internal/machine/targetReference.js +41 -0
  81. package/dist/internal/machine/targetReference.js.map +1 -0
  82. package/dist/internal/machine/topology.d.ts.map +1 -1
  83. package/dist/internal/machine/topology.js +5 -3
  84. package/dist/internal/machine/topology.js.map +1 -1
  85. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  86. package/dist/internal/testing/machine/finiteModel.js +53 -46
  87. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  88. package/dist/internal/testing/machine/probe.js +1 -1
  89. package/dist/internal/testing/machine/probe.js.map +1 -1
  90. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  91. package/dist/internal/testing/machine/verification.js +7 -1
  92. package/dist/internal/testing/machine/verification.js.map +1 -1
  93. package/dist/testing/MachineTest.d.ts +105 -73
  94. package/dist/testing/MachineTest.d.ts.map +1 -1
  95. package/dist/testing/MachineTest.js +105 -73
  96. package/dist/testing/MachineTest.js.map +1 -1
  97. package/dist/unstable/cluster/ClusterMachine.d.ts +18 -13
  98. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  99. package/dist/unstable/cluster/ClusterMachine.js +18 -25
  100. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  101. package/dist/unstable/reactivity/AtomMachine.d.ts +41 -24
  102. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  103. package/dist/unstable/reactivity/AtomMachine.js +41 -24
  104. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  105. package/docs/agent-guide.md +66 -91
  106. package/docs/machine-review.md +19 -49
  107. package/docs/root-api.md +62 -62
  108. package/package.json +1 -1
  109. package/src/Machine.ts +1836 -2907
  110. package/src/internal/machine/childRegistry.ts +1 -1
  111. package/src/internal/machine/cluster.ts +7 -73
  112. package/src/internal/machine/clusterProtocol.ts +56 -0
  113. package/src/internal/machine/commandRuntime.ts +1 -1
  114. package/src/internal/machine/configuration.ts +53 -65
  115. package/src/internal/machine/declaration.ts +140 -0
  116. package/src/internal/machine/executionPlan.ts +16 -22
  117. package/src/internal/machine/implementation.ts +41 -0
  118. package/src/internal/machine/initialDeclaration.ts +157 -0
  119. package/src/internal/machine/invocation.ts +3 -3
  120. package/src/internal/machine/invocationDefinition.ts +1 -1
  121. package/src/internal/machine/machine.ts +225 -576
  122. package/src/internal/machine/planner.ts +84 -118
  123. package/src/internal/machine/process.ts +40 -33
  124. package/src/internal/machine/protocol.ts +3 -3
  125. package/src/internal/machine/runtime.ts +20 -3037
  126. package/src/internal/machine/runtimeCompiled.ts +1166 -0
  127. package/src/internal/machine/runtimeGeneric.ts +799 -0
  128. package/src/internal/machine/runtimeProtocol.ts +1213 -0
  129. package/src/internal/machine/stateDefinition.ts +1 -15
  130. package/src/internal/machine/targetReference.ts +55 -0
  131. package/src/internal/machine/topology.ts +5 -3
  132. package/src/internal/testing/machine/finiteModel.ts +58 -53
  133. package/src/internal/testing/machine/probe.ts +1 -1
  134. package/src/internal/testing/machine/verification.ts +7 -1
  135. package/src/testing/MachineTest.ts +105 -73
  136. package/src/unstable/cluster/ClusterMachine.ts +22 -25
  137. package/src/unstable/reactivity/AtomMachine.ts +41 -24
@@ -4,7 +4,7 @@ import * as SchemaAST from "effect/SchemaAST"
4
4
  export const StateNodePropertyPolicy = {
5
5
  atomic: ["schema", "type", "annotations"],
6
6
  final: ["schema", "type", "output", "annotations"],
7
- compound: ["schema", "type", "initial", "states", "annotations"],
7
+ compound: ["schema", "type", "states", "annotations"],
8
8
  parallel: ["schema", "type", "output", "states", "annotations"],
9
9
  history: ["type", "history", "annotations"],
10
10
  choice: ["type", "annotations"]
@@ -266,9 +266,6 @@ const validateStateTree = (
266
266
  fail(boundary, `${path}.output`, "state output must be an Effect Schema")
267
267
  }
268
268
  if (kind === "compound") {
269
- if (typeof node.initial !== "string") {
270
- fail(boundary, `${path}.initial`, "compound states must declare an initial child key")
271
- }
272
269
  validateTargetSelectorChildKey(
273
270
  boundary,
274
271
  path,
@@ -285,18 +282,7 @@ const validateStateTree = (
285
282
  "schema-backed compound child states cannot use the reserved local target selector key \"with\""
286
283
  )
287
284
  }
288
- const initialKey = node.initial as string
289
285
  validateStateTree(boundary, node.states, path, true)
290
- if (!hasOwn(node.states as object, initialKey)) {
291
- fail(boundary, `${path}.initial`, `compound initial child "${initialKey}" does not exist`)
292
- }
293
- const initial = (node.states as Readonly<Record<PropertyKey, unknown>>)[initialKey]
294
- if (
295
- !Schema.isSchema(initial) && typeof initial === "object" && initial !== null &&
296
- (initial as Readonly<Record<PropertyKey, unknown>>).type === "history"
297
- ) {
298
- fail(boundary, `${path}.initial`, "compound initial children cannot be history states")
299
- }
300
286
  continue
301
287
  }
302
288
  if (kind === "parallel") {
@@ -0,0 +1,55 @@
1
+ import * as Schema from "effect/Schema"
2
+ import type { Machine, State } from "../../Machine.js"
3
+
4
+ /** Reference metadata is separate from child names, including names such as `path`. */
5
+ export const TypeId: unique symbol = Symbol.for("effect/Machine/TargetReference")
6
+
7
+ export interface Reference {
8
+ readonly [TypeId]: {
9
+ readonly root: State<Machine.StateNodeConfig>
10
+ readonly path: string
11
+ readonly kind: "state" | "choice" | "history"
12
+ }
13
+ }
14
+
15
+ /** Captures an immutable reference tree without allocating or executing a machine. */
16
+ export const make = (root: State<Machine.StateNodeConfig>): { readonly root: Reference } => {
17
+ const nodes: Array<{ path: string; key: string; parent: string | undefined; type: string }> = []
18
+ const visit = (
19
+ node: Machine.StateNodeConfig | Machine.TaggedSchema,
20
+ path: string,
21
+ key: string,
22
+ parent: string | undefined
23
+ ): void => {
24
+ const type = !Schema.isSchema(node) && "type" in node ? node.type ?? "active" : "active"
25
+ nodes.push({ path, key, parent, type })
26
+ if (!Schema.isSchema(node) && "states" in node) {
27
+ for (const [childKey, child] of Object.entries(node.states)) {
28
+ visit(child, path === "" ? childKey : `${path}.${childKey}`, childKey, path)
29
+ }
30
+ }
31
+ }
32
+ visit(root.node, "", "", undefined)
33
+ const references = new Map<string, Reference>()
34
+ for (const node of nodes) {
35
+ const reference = Object.create(null)
36
+ Object.defineProperty(reference, TypeId, {
37
+ value: Object.freeze({
38
+ root,
39
+ path: node.path,
40
+ kind: node.type === "history" || node.type === "choice" ? node.type : "state"
41
+ })
42
+ })
43
+ references.set(node.path, reference)
44
+ }
45
+ for (const node of nodes) {
46
+ if (node.parent !== undefined) {
47
+ Object.defineProperty(references.get(node.parent), node.key, {
48
+ value: references.get(node.path),
49
+ enumerable: true
50
+ })
51
+ }
52
+ }
53
+ for (const reference of references.values()) Object.freeze(reference)
54
+ return Object.freeze({ root: references.get("")! })
55
+ }
@@ -8,6 +8,7 @@ import * as Option from "effect/Option"
8
8
  import { hasProperty } from "effect/Predicate"
9
9
  import * as Schema from "effect/Schema"
10
10
  import type { Machine } from "../../Machine.js"
11
+ import { type CapturedStateConfig, toImpl } from "./implementation.js"
11
12
 
12
13
  export const TargetTypeId = "~effect/Machine/Target"
13
14
 
@@ -329,7 +330,8 @@ export const getStateNodeDefinition = (
329
330
  states: definition.states
330
331
  }
331
332
  }
332
- if (typeof definition.initial !== "string") {
333
+ const initial = "initial" in definition ? definition.initial : undefined
334
+ if (typeof initial !== "string") {
333
335
  throw new Error(`Machine.make expected compound state "${path}" to declare an initial child`)
334
336
  }
335
337
  return {
@@ -338,7 +340,7 @@ export const getStateNodeDefinition = (
338
340
  annotations,
339
341
  type: "compound",
340
342
  history: undefined,
341
- initial: definition.initial,
343
+ initial,
342
344
  states: definition.states
343
345
  }
344
346
  }
@@ -506,7 +508,7 @@ export const transitionDefinitions = (
506
508
  ): ReadonlyArray<Machine.TransitionDefinition> => {
507
509
  const definitions: Array<Machine.TransitionDefinition> = []
508
510
  for (const node of machine.stateNodes.byPath.values()) {
509
- const config = machine.handlers[node.path] as Machine.AnyStateConfig | undefined
511
+ const config = toImpl(machine).handlers[node.path] as CapturedStateConfig | undefined
510
512
  if (config === undefined) {
511
513
  continue
512
514
  }
@@ -1203,7 +1203,6 @@ const makeStateTree = (
1203
1203
  } else if (node._tag === "Compound") {
1204
1204
  tree[node.key] = {
1205
1205
  schema,
1206
- initial: node.initial,
1207
1206
  states: makeStateTree(node.states, path)
1208
1207
  }
1209
1208
  } else {
@@ -1315,13 +1314,6 @@ const findSnapshot = (snapshot: unknown, path: string): unknown => {
1315
1314
  return undefined
1316
1315
  }
1317
1316
 
1318
- const selectHistoryTarget = (builder: Record<string, any>, path: string): unknown => {
1319
- const parts = path.split(".")
1320
- let current: any = builder
1321
- for (let index = 0; index < parts.length - 1; index++) current = current[parts[index]!]
1322
- return current[parts[parts.length - 1]!]
1323
- }
1324
-
1325
1317
  const selectableDefinitionTarget = (
1326
1318
  source: string,
1327
1319
  target: string,
@@ -1340,26 +1332,20 @@ const selectableDefinitionTarget = (
1340
1332
  }
1341
1333
 
1342
1334
  const selectDefinitionTarget = (
1343
- selector: Record<string, any>,
1335
+ references: Record<string, any>,
1344
1336
  source: string,
1345
1337
  target: string,
1346
1338
  byPath: ReadonlyMap<string, FlatFiniteState>
1347
- ): unknown => {
1339
+ ): Readonly<Record<string, unknown>> => {
1348
1340
  const selected = byPath.get(target)!
1349
- if (selected.node._tag === "History") {
1350
- return selectHistoryTarget(selector.history, target)
1351
- }
1352
- const sourceRoot = byPath.get(source)!.root
1353
- if (selected.root !== sourceRoot) {
1354
- const root = target.split(".")[0]!
1355
- return selector.branch[root]()
1356
- }
1357
- const selectable = selectableDefinitionTarget(source, target, byPath)
1358
- let current = selector.branch
1359
- const parts = selectable.split(".")
1360
- for (const part of parts) current = current[part]
1361
- if (typeof current === "function") return current()
1362
- return current.initial
1341
+ const selectedPath = selected.node._tag === "History" ?
1342
+ target
1343
+ : selected.root !== byPath.get(source)!.root ?
1344
+ target.split(".")[0]!
1345
+ : selectableDefinitionTarget(source, target, byPath)
1346
+ let reference = references
1347
+ for (const part of selectedPath.split(".")) reference = reference[part]
1348
+ return selected.node._tag === "History" ? { history: reference } : { target: reference }
1363
1349
  }
1364
1350
 
1365
1351
  const resolveDefinitionTarget = (
@@ -1410,18 +1396,23 @@ const makeHandlers = (
1410
1396
  states: ReadonlyArray<FiniteState>,
1411
1397
  parent: string | undefined,
1412
1398
  byPath: ReadonlyMap<string, FlatFiniteState>,
1413
- transitions: ReadonlyMap<string, FiniteTransition>
1399
+ transitions: ReadonlyMap<string, FiniteTransition>,
1400
+ references: Record<string, any>,
1401
+ branches: Record<string, Readonly<Record<string, unknown>>>
1414
1402
  ): Record<string, unknown> => {
1415
1403
  const handlers: Record<string, unknown> = Object.create(null)
1416
1404
  for (const node of states) {
1417
1405
  const path = parent === undefined ? node.key : `${parent}.${node.key}`
1418
1406
  if (node._tag === "History") continue
1419
1407
  if (node._tag === "Choice") {
1408
+ const group = `choice:${path}`
1409
+ branches[group] = { destination: selectDefinitionTarget(references, path, node.selected, byPath) }
1420
1410
  handlers[node.key] = {
1421
- choice: (to: Record<string, any>) =>
1422
- (selectDefinitionTarget(to, path, node.selected, byPath) as any).resolve(
1423
- ({ target }: { readonly target: any }) => resolveDefinitionTarget(target, path, node.selected, byPath)
1424
- )
1411
+ choice: {
1412
+ branches: group,
1413
+ resolve: ({ select }: { readonly select: any }) =>
1414
+ resolveDefinitionTarget(select.destination, path, node.selected, byPath)
1415
+ }
1425
1416
  }
1426
1417
  continue
1427
1418
  }
@@ -1434,15 +1425,19 @@ const makeHandlers = (
1434
1425
  let onDone: unknown
1435
1426
  for (const transition of transitions.values()) {
1436
1427
  if (transition.source !== path) continue
1437
- const config = (to: Record<string, any>) => {
1438
- const selected = transition.target === undefined
1439
- ? to.none
1440
- : selectDefinitionTarget(to, path, transition.target, byPath)
1441
- const resolve = transition.target === undefined
1442
- ? () => undefined
1443
- : ({ target }: { readonly target: any }) =>
1444
- resolveDefinitionTarget(target, path, transition.target!, byPath, transition.targetValue)
1445
- return ("reenter" in transition && transition.reenter ? selected.reenter() : selected).resolve(resolve)
1428
+ const group = `transition:${path}:${triggerKey(transition.trigger)}`
1429
+ if (transition.target !== undefined) {
1430
+ branches[group] = {
1431
+ destination: selectDefinitionTarget(references, path, transition.target, byPath)
1432
+ }
1433
+ }
1434
+ const config = {
1435
+ ...(transition.target === undefined ? { none: true } : {
1436
+ branches: group,
1437
+ resolve: ({ select }: { readonly select: any }) =>
1438
+ resolveDefinitionTarget(select.destination, path, transition.target!, byPath, transition.targetValue)
1439
+ }),
1440
+ ...("reenter" in transition && transition.reenter ? { reenter: true } : {})
1446
1441
  }
1447
1442
  if (transition.trigger.type === "event") on[transition.trigger.event] = config
1448
1443
  else if (transition.trigger.type === "always") always = config
@@ -1477,19 +1472,22 @@ const makeHandlers = (
1477
1472
  ...(Object.keys(history).length === 0 ? {} : { history }),
1478
1473
  ...(node._tag === "Compound"
1479
1474
  ? {
1480
- initialize: ({ builder }: any) => builder.decoded(stateValue(byPath.get(`${path}.${node.initial}`)!))
1475
+ initial: {
1476
+ target: `${path}.${node.initial}`.split(".").reduce((ref, key) => ref[key], references),
1477
+ ...(byPath.get(`${path}.${node.initial}`)!.node._tag === "Choice" ? {} : {
1478
+ decoded: true,
1479
+ data: stateValue(byPath.get(`${path}.${node.initial}`)!)
1480
+ })
1481
+ }
1481
1482
  }
1482
1483
  : {
1483
- initialize: ({ builder }: any) =>
1484
+ initial: Object.fromEntries(
1484
1485
  node.states
1485
1486
  .filter((child) => child._tag !== "History" && child._tag !== "Choice")
1486
- .reduce(
1487
- (current: any, child) =>
1488
- current[child.key].decoded(stateValue(byPath.get(`${path}.${child.key}`)!)),
1489
- builder
1490
- )
1487
+ .map((child) => [child.key, { decoded: true, data: stateValue(byPath.get(`${path}.${child.key}`)!) }])
1488
+ )
1491
1489
  }),
1492
- states: makeHandlers(node.states, path, byPath, transitions)
1490
+ states: makeHandlers(node.states, path, byPath, transitions, references, branches)
1493
1491
  }
1494
1492
  : {})
1495
1493
  }
@@ -1512,21 +1510,28 @@ export const compileModel = (model: FiniteModel): Machine.Machine.Any => {
1512
1510
  const flattened = validateModel(model)
1513
1511
  const byPath = new Map(flattened.map((state) => [state.path, state]))
1514
1512
  const stateTree = makeStateTree(model.roots, undefined)
1515
- const defined = Machine.state({ initial: model.initial, states: stateTree } as any)
1513
+ const defined = Machine.state({ states: stateTree } as any)
1516
1514
  const eventSchemas = model.events.map((event) => Schema.TaggedStruct(event, {}))
1517
1515
  const initial = byPath.get(model.initial)!
1518
- const machine = Machine.make({
1519
- root: defined,
1520
- events: Machine.eventsFromSchemas(...eventSchemas) as any
1521
- } as any)
1522
1516
  const transitions = new Map(model.transitions.map((transition) => [
1523
1517
  `${transition.source}\u0000${triggerKey(transition.trigger)}`,
1524
1518
  transition
1525
1519
  ]))
1520
+ const branches: Record<string, Readonly<Record<string, unknown>>> = Object.create(null)
1521
+ const handlers = makeHandlers(model.roots, undefined, byPath, transitions, Machine.targets(defined).root, branches)
1522
+ const machine = Machine.make({
1523
+ root: defined,
1524
+ branches,
1525
+ events: Machine.eventsFromSchemas(...eventSchemas) as any
1526
+ } as any)
1526
1527
  return machine.handle(
1527
1528
  {
1528
- initialize: ({ builder }: any) => builder.decoded(stateValue(initial)),
1529
- states: makeHandlers(model.roots, undefined, byPath, transitions)
1529
+ initial: {
1530
+ target: (Machine.targets(defined).root as unknown as Record<string, unknown>)[model.initial],
1531
+ decoded: true,
1532
+ data: stateValue(initial)
1533
+ },
1534
+ states: handlers
1530
1535
  } as any
1531
1536
  ) as Machine.Machine.Any
1532
1537
  }
@@ -8,7 +8,7 @@ import * as Data from "effect/Data"
8
8
  import * as Effect from "effect/Effect"
9
9
  import type * as Machine from "../../../Machine.js"
10
10
  import type { Probe, ProbePlan } from "../../../testing/MachineTest.js"
11
- import * as Runtime from "../../machine/runtime.js"
11
+ import * as Runtime from "../../machine/runtimeProtocol.js"
12
12
 
13
13
  type AnyMachine = Machine.Machine.Any
14
14
 
@@ -1738,7 +1738,13 @@ export const verify = <M extends AnyMachine>(
1738
1738
  const node = byPath.get(path)
1739
1739
  if (node?.type !== "compound") continue
1740
1740
  const child = node.children.find((child) => starting.active.has(child))
1741
- if (byPath.get(node.initial ?? "")?.type !== "choice" && child !== node.initial) {
1741
+ const routed = initialChoiceRouteRoot() !== undefined &&
1742
+ trace.initial.plan.microsteps[0]?.transitions.some((transition) => {
1743
+ const target = transition.resolvedTarget ?? transition.target
1744
+ return target !== undefined && child !== undefined &&
1745
+ isDescendantOrSelf(String(target), child)
1746
+ })
1747
+ if (byPath.get(node.initial ?? "")?.type !== "choice" && child !== node.initial && !routed) {
1742
1748
  add(
1743
1749
  "definitions.initial",
1744
1750
  initialLocation,
@@ -232,26 +232,29 @@ export interface Scenarios<M extends AnyMachine> {
232
232
  * **Example**
233
233
  *
234
234
  * ```ts
235
- * import { Schema } from "effect"
236
235
  * import { Machine } from "@typeonce/effect-machine"
237
236
  * import { MachineTest } from "@typeonce/effect-machine/testing"
238
- *
239
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
240
- * class Reset extends Schema.TaggedClass<Reset>("Reset")("Reset", {}) {}
241
- * const States = Machine.state({ initial: "Idle", states: { Idle } })
237
+ * import { Schema } from "effect"
238
+ * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
239
+ * }
240
+ * class Reset extends Schema.TaggedClass<Reset>("Reset")("Reset", {}) {
241
+ * }
242
+ * const States = Machine.state({ states: { Idle } })
242
243
  * const machine = Machine.make({
243
244
  * root: States,
244
- * events: Machine.eventsFromSchemas(Reset),
245
- * initialConfiguration: root => root.resolve(({ target }) => target.from(tree => tree.Idle.from()))
246
- * }).handle({ states: {
247
- * Idle: {
248
- * on: {
249
- * Reset: (to) =>
250
- * to.branch.Idle().resolve(({ target }) => target.from())
245
+ * events: Machine.eventsFromSchemas(Reset)
246
+ * }).handle({
247
+ * initial: {
248
+ * target: Machine.targets(States).root.Idle
249
+ * },
250
+ * states: {
251
+ * Idle: {
252
+ * on: {
253
+ * Reset: (to) => to.branch.Idle().resolve(({ target }) => target.from())
254
+ * }
251
255
  * }
252
256
  * }
253
- * } })
254
- *
257
+ * })
255
258
  * const generated = MachineTest.scenarios(machine, { maxEvents: 5 })
256
259
  * ```
257
260
  *
@@ -491,18 +494,23 @@ export { ProbeUnavailableError } from "../internal/testing/machine/probe.js"
491
494
  * **Example**
492
495
  *
493
496
  * ```ts
494
- * import { Effect, Schema } from "effect"
495
497
  * import { Machine } from "@typeonce/effect-machine"
496
498
  * import { MachineTest } from "@typeonce/effect-machine/testing"
497
- *
498
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
499
- * const States = Machine.state({ initial: "Idle", states: { Idle } })
499
+ * import { Effect, Schema } from "effect"
500
+ * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
501
+ * }
502
+ * const States = Machine.state({ states: { Idle } })
500
503
  * const machine = Machine.make({
501
504
  * root: States,
502
- * events: Machine.eventsFromSchemas(),
503
- * initialConfiguration: root => root.resolve(({ target }) => target.from(tree => tree.Idle.from()))
504
- * }).handle({ states: { Idle: {} } })
505
- *
505
+ * events: Machine.eventsFromSchemas()
506
+ * }).handle({
507
+ * initial: {
508
+ * target: Machine.targets(States).root.Idle
509
+ * },
510
+ * states: {
511
+ * Idle: {}
512
+ * }
513
+ * })
506
514
  * const program = Effect.gen(function*() {
507
515
  * const ref = yield* Machine.start(machine)
508
516
  * return yield* MachineTest.probe(machine, ref)
@@ -1100,20 +1108,27 @@ export const Invariant: {
1100
1108
  * **Example**
1101
1109
  *
1102
1110
  * ```ts
1103
- * import { Schema } from "effect"
1104
1111
  * import { Machine } from "@typeonce/effect-machine"
1105
1112
  * import { MachineTest } from "@typeonce/effect-machine/testing"
1106
- *
1113
+ * import { Schema } from "effect"
1107
1114
  * class Count extends Schema.TaggedClass<Count>("Count")("Count", {
1108
1115
  * value: Schema.Number
1109
- * }) {}
1110
- * const States = Machine.state({ initial: "Count", states: { Count } })
1116
+ * }) {
1117
+ * }
1118
+ * const States = Machine.state({ states: { Count } })
1111
1119
  * const machine = Machine.make({
1112
1120
  * root: States,
1113
- * events: Machine.eventsFromSchemas(),
1114
- * initialConfiguration: root => root.resolve(({ target }) => target.from(tree => tree.Count.decoded(new Count({ value: 0 }))))
1115
- * }).handle({ states: { Count: {} } })
1116
- *
1121
+ * events: Machine.eventsFromSchemas()
1122
+ * }).handle({
1123
+ * initial: {
1124
+ * target: Machine.targets(States).root.Count,
1125
+ * decoded: true,
1126
+ * data: new Count({ value: 0 })
1127
+ * },
1128
+ * states: {
1129
+ * Count: {}
1130
+ * }
1131
+ * })
1117
1132
  * const nonNegative = MachineTest.invariants(machine).state(
1118
1133
  * "count is non-negative",
1119
1134
  * ({ snapshot }) => snapshot.value.value >= 0
@@ -1431,29 +1446,34 @@ export type ExploreOptions<M extends AnyMachine, Key extends ExplorationKey = Ex
1431
1446
  * **Example**
1432
1447
  *
1433
1448
  * ```ts
1434
- * import { Schema } from "effect"
1435
1449
  * import { Machine } from "@typeonce/effect-machine"
1436
1450
  * import { MachineTest } from "@typeonce/effect-machine/testing"
1437
- *
1451
+ * import { Schema } from "effect"
1438
1452
  * class Count extends Schema.TaggedClass<Count>("Count")("Count", {
1439
1453
  * value: Schema.Number
1440
- * }) {}
1441
- * class Increment extends Schema.TaggedClass<Increment>("Increment")("Increment", {}) {}
1442
- * const States = Machine.state({ initial: "Count", states: { Count } })
1454
+ * }) {
1455
+ * }
1456
+ * class Increment extends Schema.TaggedClass<Increment>("Increment")("Increment", {}) {
1457
+ * }
1458
+ * const States = Machine.state({ states: { Count } })
1443
1459
  * const machine = Machine.make({
1444
1460
  * root: States,
1445
- * events: Machine.eventsFromSchemas(Increment),
1446
- * initialConfiguration: root => root.resolve(({ target }) => target.from(tree => tree.Count.decoded(new Count({ value: 0 }))))
1447
- * }).handle({ states: {
1448
- * Count: {
1449
- * on: {
1450
- * Increment: (to) =>
1451
- * to.branch.Count().resolve(({ state, target }) =>
1452
- * target.decoded(new Count({ value: state.value + 1 })))
1461
+ * events: Machine.eventsFromSchemas(Increment)
1462
+ * }).handle({
1463
+ * initial: {
1464
+ * target: Machine.targets(States).root.Count,
1465
+ * decoded: true,
1466
+ * data: new Count({ value: 0 })
1467
+ * },
1468
+ * states: {
1469
+ * Count: {
1470
+ * on: {
1471
+ * Increment: (to) =>
1472
+ * to.branch.Count().resolve(({ state, target }) => target.decoded(new Count({ value: state.value + 1 })))
1473
+ * }
1453
1474
  * }
1454
1475
  * }
1455
- * } })
1456
- *
1476
+ * })
1457
1477
  * const explored = MachineTest.explore(machine, {
1458
1478
  * events: ({ snapshot }) => snapshot.value.value < 2 ? [new Increment({})] : [],
1459
1479
  * stateKey: ({ snapshot }) => snapshot.value.value
@@ -1623,18 +1643,23 @@ export type RunServices<M extends AnyMachine> = IsAny<
1623
1643
  * **Example**
1624
1644
  *
1625
1645
  * ```ts
1626
- * import { Schema } from "effect"
1627
1646
  * import { Machine } from "@typeonce/effect-machine"
1628
1647
  * import { MachineTest } from "@typeonce/effect-machine/testing"
1629
- *
1630
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
1631
- * const States = Machine.state({ initial: "Idle", states: { Idle } })
1648
+ * import { Schema } from "effect"
1649
+ * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
1650
+ * }
1651
+ * const States = Machine.state({ states: { Idle } })
1632
1652
  * const machine = Machine.make({
1633
1653
  * root: States,
1634
- * events: Machine.eventsFromSchemas(),
1635
- * initialConfiguration: root => root.resolve(({ target }) => target.from(tree => tree.Idle.from()))
1636
- * }).handle({ states: { Idle: {} } })
1637
- *
1654
+ * events: Machine.eventsFromSchemas()
1655
+ * }).handle({
1656
+ * initial: {
1657
+ * target: Machine.targets(States).root.Idle
1658
+ * },
1659
+ * states: {
1660
+ * Idle: {}
1661
+ * }
1662
+ * })
1638
1663
  * const trace = MachineTest.run(machine, { events: [] })
1639
1664
  * ```
1640
1665
  *
@@ -1889,22 +1914,24 @@ export interface Coverage<M extends AnyMachine> {
1889
1914
  * **Example**
1890
1915
  *
1891
1916
  * ```ts
1892
- * import { Effect, Schema } from "effect"
1893
1917
  * import { Machine } from "@typeonce/effect-machine"
1894
1918
  * import { MachineTest } from "@typeonce/effect-machine/testing"
1895
- *
1896
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
1897
- * const States = Machine.state({ initial: "Idle", states: { Idle } })
1919
+ * import { Effect, Schema } from "effect"
1920
+ * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
1921
+ * }
1922
+ * const States = Machine.state({ states: { Idle } })
1898
1923
  * const machine = Machine.make({
1899
1924
  * root: States,
1900
- * events: Machine.eventsFromSchemas(),
1901
- * initialConfiguration: root => root.resolve(({ target }) => target.from(tree => tree.Idle.from()))
1902
- * }).handle({ states: { Idle: {} } })
1903
- *
1904
- * const report = Effect.map(
1905
- * MachineTest.run(machine, { events: [] }),
1906
- * (trace) => MachineTest.coverage(machine, trace)
1907
- * )
1925
+ * events: Machine.eventsFromSchemas()
1926
+ * }).handle({
1927
+ * initial: {
1928
+ * target: Machine.targets(States).root.Idle
1929
+ * },
1930
+ * states: {
1931
+ * Idle: {}
1932
+ * }
1933
+ * })
1934
+ * const report = Effect.map(MachineTest.run(machine, { events: [] }), (trace) => MachineTest.coverage(machine, trace))
1908
1935
  * ```
1909
1936
  *
1910
1937
  * @category verification
@@ -2115,18 +2142,23 @@ export interface VerifyOptions {
2115
2142
  * **Example**
2116
2143
  *
2117
2144
  * ```ts
2118
- * import { Effect, Schema } from "effect"
2119
2145
  * import { Machine } from "@typeonce/effect-machine"
2120
2146
  * import { MachineTest } from "@typeonce/effect-machine/testing"
2121
- *
2122
- * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {}
2123
- * const States = Machine.state({ initial: "Idle", states: { Idle } })
2147
+ * import { Effect, Schema } from "effect"
2148
+ * class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
2149
+ * }
2150
+ * const States = Machine.state({ states: { Idle } })
2124
2151
  * const machine = Machine.make({
2125
2152
  * root: States,
2126
- * events: Machine.eventsFromSchemas(),
2127
- * initialConfiguration: root => root.resolve(({ target }) => target.from(tree => tree.Idle.from()))
2128
- * }).handle({ states: { Idle: {} } })
2129
- *
2153
+ * events: Machine.eventsFromSchemas()
2154
+ * }).handle({
2155
+ * initial: {
2156
+ * target: Machine.targets(States).root.Idle
2157
+ * },
2158
+ * states: {
2159
+ * Idle: {}
2160
+ * }
2161
+ * })
2130
2162
  * const checked = Effect.gen(function*() {
2131
2163
  * const trace = yield* MachineTest.run(machine, { events: [] })
2132
2164
  * yield* MachineTest.verify(machine, trace)