@typeonce/effect-machine 0.33.0 → 0.34.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 +216 -636
  2. package/dist/Machine.d.ts +815 -583
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +20 -8
  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 -1
  19. package/dist/internal/machine/configuration.d.ts.map +1 -1
  20. package/dist/internal/machine/configuration.js +38 -45
  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 +5 -4
  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/invocation.d.ts +1 -1
  34. package/dist/internal/machine/invocation.d.ts.map +1 -1
  35. package/dist/internal/machine/invocation.js +1 -1
  36. package/dist/internal/machine/invocation.js.map +1 -1
  37. package/dist/internal/machine/invocationDefinition.d.ts +1 -1
  38. package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
  39. package/dist/internal/machine/machine.d.ts +13 -2
  40. package/dist/internal/machine/machine.d.ts.map +1 -1
  41. package/dist/internal/machine/machine.js +140 -278
  42. package/dist/internal/machine/machine.js.map +1 -1
  43. package/dist/internal/machine/planner.d.ts.map +1 -1
  44. package/dist/internal/machine/planner.js +12 -11
  45. package/dist/internal/machine/planner.js.map +1 -1
  46. package/dist/internal/machine/process.d.ts +8 -7
  47. package/dist/internal/machine/process.d.ts.map +1 -1
  48. package/dist/internal/machine/process.js +16 -12
  49. package/dist/internal/machine/process.js.map +1 -1
  50. package/dist/internal/machine/runtime.d.ts +2 -230
  51. package/dist/internal/machine/runtime.d.ts.map +1 -1
  52. package/dist/internal/machine/runtime.js +8 -1857
  53. package/dist/internal/machine/runtime.js.map +1 -1
  54. package/dist/internal/machine/runtimeCompiled.d.ts +5 -0
  55. package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -0
  56. package/dist/internal/machine/runtimeCompiled.js +867 -0
  57. package/dist/internal/machine/runtimeCompiled.js.map +1 -0
  58. package/dist/internal/machine/runtimeGeneric.d.ts +3 -0
  59. package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -0
  60. package/dist/internal/machine/runtimeGeneric.js +471 -0
  61. package/dist/internal/machine/runtimeGeneric.js.map +1 -0
  62. package/dist/internal/machine/runtimeProtocol.d.ts +312 -0
  63. package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -0
  64. package/dist/internal/machine/runtimeProtocol.js +561 -0
  65. package/dist/internal/machine/runtimeProtocol.js.map +1 -0
  66. package/dist/internal/machine/targetReference.d.ts +15 -0
  67. package/dist/internal/machine/targetReference.d.ts.map +1 -0
  68. package/dist/internal/machine/targetReference.js +31 -0
  69. package/dist/internal/machine/targetReference.js.map +1 -0
  70. package/dist/internal/machine/topology.d.ts.map +1 -1
  71. package/dist/internal/machine/topology.js +2 -1
  72. package/dist/internal/machine/topology.js.map +1 -1
  73. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  74. package/dist/internal/testing/machine/finiteModel.js +38 -40
  75. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  76. package/dist/internal/testing/machine/probe.js +1 -1
  77. package/dist/internal/testing/machine/probe.js.map +1 -1
  78. package/dist/unstable/cluster/ClusterMachine.d.ts +5 -5
  79. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  80. package/dist/unstable/cluster/ClusterMachine.js +5 -17
  81. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  82. package/docs/agent-guide.md +66 -91
  83. package/docs/machine-review.md +19 -49
  84. package/docs/root-api.md +62 -62
  85. package/package.json +1 -1
  86. package/src/Machine.ts +1514 -2312
  87. package/src/internal/machine/childRegistry.ts +1 -1
  88. package/src/internal/machine/cluster.ts +7 -73
  89. package/src/internal/machine/clusterProtocol.ts +56 -0
  90. package/src/internal/machine/commandRuntime.ts +1 -1
  91. package/src/internal/machine/configuration.ts +53 -45
  92. package/src/internal/machine/declaration.ts +140 -0
  93. package/src/internal/machine/executionPlan.ts +6 -5
  94. package/src/internal/machine/implementation.ts +41 -0
  95. package/src/internal/machine/invocation.ts +3 -3
  96. package/src/internal/machine/invocationDefinition.ts +1 -1
  97. package/src/internal/machine/machine.ts +199 -409
  98. package/src/internal/machine/planner.ts +54 -35
  99. package/src/internal/machine/process.ts +40 -33
  100. package/src/internal/machine/runtime.ts +20 -3037
  101. package/src/internal/machine/runtimeCompiled.ts +1166 -0
  102. package/src/internal/machine/runtimeGeneric.ts +799 -0
  103. package/src/internal/machine/runtimeProtocol.ts +1213 -0
  104. package/src/internal/machine/targetReference.ts +40 -0
  105. package/src/internal/machine/topology.ts +2 -1
  106. package/src/internal/testing/machine/finiteModel.ts +42 -43
  107. package/src/internal/testing/machine/probe.ts +1 -1
  108. package/src/unstable/cluster/ClusterMachine.ts +9 -17
@@ -0,0 +1,40 @@
1
+ import type { Machine, State } from "../../Machine.js"
2
+ import * as Topology from "./topology.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 = Topology.compileStateNodes({ "": root.node })
18
+ const references = new Map<string, Reference>()
19
+ for (const node of nodes.byPath.values()) {
20
+ const reference = Object.create(null)
21
+ Object.defineProperty(reference, TypeId, {
22
+ value: Object.freeze({
23
+ root,
24
+ path: node.path,
25
+ kind: node.type === "history" || node.type === "choice" ? node.type : "state"
26
+ })
27
+ })
28
+ references.set(node.path, reference)
29
+ }
30
+ for (const node of nodes.byPath.values()) {
31
+ if (node.parent !== undefined) {
32
+ Object.defineProperty(references.get(node.parent), node.key, {
33
+ value: references.get(node.path),
34
+ enumerable: true
35
+ })
36
+ }
37
+ }
38
+ for (const reference of references.values()) Object.freeze(reference)
39
+ return Object.freeze({ root: references.get("")! })
40
+ }
@@ -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
 
@@ -506,7 +507,7 @@ export const transitionDefinitions = (
506
507
  ): ReadonlyArray<Machine.TransitionDefinition> => {
507
508
  const definitions: Array<Machine.TransitionDefinition> = []
508
509
  for (const node of machine.stateNodes.byPath.values()) {
509
- const config = machine.handlers[node.path] as Machine.AnyStateConfig | undefined
510
+ const config = toImpl(machine).handlers[node.path] as CapturedStateConfig | undefined
510
511
  if (config === undefined) {
511
512
  continue
512
513
  }
@@ -1315,13 +1315,6 @@ const findSnapshot = (snapshot: unknown, path: string): unknown => {
1315
1315
  return undefined
1316
1316
  }
1317
1317
 
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
1318
  const selectableDefinitionTarget = (
1326
1319
  source: string,
1327
1320
  target: string,
@@ -1340,26 +1333,20 @@ const selectableDefinitionTarget = (
1340
1333
  }
1341
1334
 
1342
1335
  const selectDefinitionTarget = (
1343
- selector: Record<string, any>,
1336
+ references: Record<string, any>,
1344
1337
  source: string,
1345
1338
  target: string,
1346
1339
  byPath: ReadonlyMap<string, FlatFiniteState>
1347
- ): unknown => {
1340
+ ): Readonly<Record<string, unknown>> => {
1348
1341
  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
1342
+ const selectedPath = selected.node._tag === "History" ?
1343
+ target
1344
+ : selected.root !== byPath.get(source)!.root ?
1345
+ target.split(".")[0]!
1346
+ : selectableDefinitionTarget(source, target, byPath)
1347
+ let reference = references
1348
+ for (const part of selectedPath.split(".")) reference = reference[part]
1349
+ return selected.node._tag === "History" ? { history: reference } : { target: reference }
1363
1350
  }
1364
1351
 
1365
1352
  const resolveDefinitionTarget = (
@@ -1410,18 +1397,23 @@ const makeHandlers = (
1410
1397
  states: ReadonlyArray<FiniteState>,
1411
1398
  parent: string | undefined,
1412
1399
  byPath: ReadonlyMap<string, FlatFiniteState>,
1413
- transitions: ReadonlyMap<string, FiniteTransition>
1400
+ transitions: ReadonlyMap<string, FiniteTransition>,
1401
+ references: Record<string, any>,
1402
+ branches: Record<string, Readonly<Record<string, unknown>>>
1414
1403
  ): Record<string, unknown> => {
1415
1404
  const handlers: Record<string, unknown> = Object.create(null)
1416
1405
  for (const node of states) {
1417
1406
  const path = parent === undefined ? node.key : `${parent}.${node.key}`
1418
1407
  if (node._tag === "History") continue
1419
1408
  if (node._tag === "Choice") {
1409
+ const group = `choice:${path}`
1410
+ branches[group] = { destination: selectDefinitionTarget(references, path, node.selected, byPath) }
1420
1411
  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
- )
1412
+ choice: {
1413
+ branches: group,
1414
+ resolve: ({ select }: { readonly select: any }) =>
1415
+ resolveDefinitionTarget(select.destination, path, node.selected, byPath)
1416
+ }
1425
1417
  }
1426
1418
  continue
1427
1419
  }
@@ -1434,15 +1426,19 @@ const makeHandlers = (
1434
1426
  let onDone: unknown
1435
1427
  for (const transition of transitions.values()) {
1436
1428
  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)
1429
+ const group = `transition:${path}:${triggerKey(transition.trigger)}`
1430
+ if (transition.target !== undefined) {
1431
+ branches[group] = {
1432
+ destination: selectDefinitionTarget(references, path, transition.target, byPath)
1433
+ }
1434
+ }
1435
+ const config = {
1436
+ ...(transition.target === undefined ? { none: true } : {
1437
+ branches: group,
1438
+ resolve: ({ select }: { readonly select: any }) =>
1439
+ resolveDefinitionTarget(select.destination, path, transition.target!, byPath, transition.targetValue)
1440
+ }),
1441
+ ...("reenter" in transition && transition.reenter ? { reenter: true } : {})
1446
1442
  }
1447
1443
  if (transition.trigger.type === "event") on[transition.trigger.event] = config
1448
1444
  else if (transition.trigger.type === "always") always = config
@@ -1489,7 +1485,7 @@ const makeHandlers = (
1489
1485
  builder
1490
1486
  )
1491
1487
  }),
1492
- states: makeHandlers(node.states, path, byPath, transitions)
1488
+ states: makeHandlers(node.states, path, byPath, transitions, references, branches)
1493
1489
  }
1494
1490
  : {})
1495
1491
  }
@@ -1515,18 +1511,21 @@ export const compileModel = (model: FiniteModel): Machine.Machine.Any => {
1515
1511
  const defined = Machine.state({ initial: model.initial, states: stateTree } as any)
1516
1512
  const eventSchemas = model.events.map((event) => Schema.TaggedStruct(event, {}))
1517
1513
  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
1514
  const transitions = new Map(model.transitions.map((transition) => [
1523
1515
  `${transition.source}\u0000${triggerKey(transition.trigger)}`,
1524
1516
  transition
1525
1517
  ]))
1518
+ const branches: Record<string, Readonly<Record<string, unknown>>> = Object.create(null)
1519
+ const handlers = makeHandlers(model.roots, undefined, byPath, transitions, Machine.targets(defined).root, branches)
1520
+ const machine = Machine.make({
1521
+ root: defined,
1522
+ branches,
1523
+ events: Machine.eventsFromSchemas(...eventSchemas) as any
1524
+ } as any)
1526
1525
  return machine.handle(
1527
1526
  {
1528
1527
  initialize: ({ builder }: any) => builder.decoded(stateValue(initial)),
1529
- states: makeHandlers(model.roots, undefined, byPath, transitions)
1528
+ states: handlers
1530
1529
  } as any
1531
1530
  ) as Machine.Machine.Any
1532
1531
  }
@@ -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
 
@@ -6,11 +6,12 @@
6
6
  import type * as Effect from "effect/Effect"
7
7
  import type * as Layer from "effect/Layer"
8
8
  import type * as Option from "effect/Option"
9
- import * as Schema from "effect/Schema"
9
+ import type * as Schema from "effect/Schema"
10
10
  import type { Entity, MessageStorage, Sharding, Snowflake } from "effect/unstable/cluster"
11
11
  import type { Rpc } from "effect/unstable/rpc"
12
12
  import * as internal from "../../internal/machine/cluster.js"
13
- import { Accepted, Rejected, Storage } from "../../internal/machine/cluster.js"
13
+ import * as Protocol from "../../internal/machine/clusterProtocol.js"
14
+ import { Accepted, Rejected, Storage } from "../../internal/machine/clusterProtocol.js"
14
15
  import type { EnsureExecutable } from "../../internal/machine/readiness.js"
15
16
  import type { ExcludeCompatibleRuntime } from "../../internal/machine/requirements.js"
16
17
  import type * as Machine from "../../Machine.js"
@@ -77,10 +78,10 @@ export type CommitResult = CommitResult.Committed | CommitResult.Duplicate
77
78
  * @category models
78
79
  * @since 0.4.0
79
80
  */
80
- export const CommitResult = {
81
- Committed: (): CommitResult.Committed => ({ _tag: "Committed" }),
82
- Duplicate: (): CommitResult.Duplicate => ({ _tag: "Duplicate" })
83
- }
81
+ export const CommitResult: {
82
+ Committed: () => CommitResult.Committed
83
+ Duplicate: () => CommitResult.Duplicate
84
+ } = Protocol.CommitResult
84
85
 
85
86
  /**
86
87
  * Types for Cluster machine commit results.
@@ -148,16 +149,7 @@ export { Accepted }
148
149
  * @category models
149
150
  * @since 0.4.0
150
151
  */
151
- export const RejectionReason = Schema.Literals([
152
- "MachineIdMismatch",
153
- "VersionMismatch",
154
- "InvalidCheckpoint",
155
- "UnsupportedProcessLocal",
156
- "TransitionFailure",
157
- "SnapshotEncodeFailure",
158
- "PersistenceFailure",
159
- "EmissionFailure"
160
- ])
152
+ export const RejectionReason: typeof Protocol.RejectionReason = Protocol.RejectionReason
161
153
 
162
154
  /**
163
155
  * Type of {@link RejectionReason}.
@@ -182,7 +174,7 @@ export { Rejected }
182
174
  * @category schemas
183
175
  * @since 0.4.0
184
176
  */
185
- export const SendResult = Schema.Union([Accepted, Rejected])
177
+ export const SendResult: typeof Protocol.SendResult = Protocol.SendResult
186
178
 
187
179
  type SendRpc<Events extends ReadonlyArray<Machine.Machine.TaggedSchema>> = Rpc.Rpc<
188
180
  "send",