@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
@@ -0,0 +1,157 @@
1
+ /** Captures handler-owned initial edges before compiling executable topology. */
2
+ import { hasProperty } from "effect/Predicate"
3
+ import * as Schema from "effect/Schema"
4
+ import type { Machine, State } from "../../Machine.js"
5
+ import { SnapshotBuilderStateTypeId } from "./symbols.js"
6
+ import * as Reference from "./targetReference.js"
7
+ import * as Topology from "./topology.js"
8
+
9
+ const initializers = new WeakSet<object>()
10
+
11
+ export const isDataInitializer = (value: object): boolean => initializers.has(value)
12
+
13
+ type Context = Readonly<Record<string, unknown>>
14
+ type Constructor = (context: Context) => unknown
15
+ const record = (value: unknown, label: string): Readonly<Record<string, unknown>> => {
16
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
17
+ throw new Error(`Machine ${label} must be an object`)
18
+ }
19
+ return value as Readonly<Record<string, unknown>>
20
+ }
21
+ const evaluate = (value: unknown): Constructor => typeof value === "function" ? value as Constructor : () => value
22
+
23
+ /** Only the outer declaration is interpreted; callback results are ordinary data. */
24
+ export const construction = (value: unknown): Constructor => {
25
+ const descriptor = typeof value === "object" && value !== null &&
26
+ hasProperty(value, "decoded") && value.decoded === true && hasProperty(value, "data")
27
+ if (descriptor) {
28
+ const config = record(value, "decoded construction")
29
+ if (Reflect.ownKeys(config).some((key) => key !== "decoded" && key !== "data")) {
30
+ throw new Error("Machine decoded construction accepts only decoded and data")
31
+ }
32
+ return evaluate(config.data)
33
+ }
34
+ const resolve = evaluate(value)
35
+ return (context) => Topology.makeStateInput(value === undefined ? {} : resolve(context))
36
+ }
37
+
38
+ export const edgeConstruction = (config: Readonly<Record<string, unknown>>): Constructor => {
39
+ if (config.decoded !== undefined && typeof config.decoded !== "boolean") {
40
+ throw new Error("Machine decoded construction flag must be a boolean")
41
+ }
42
+ if (config.decoded === true) {
43
+ if (!hasProperty(config, "data")) throw new Error("Machine decoded construction requires data")
44
+ return evaluate(config.data)
45
+ }
46
+ const resolve = evaluate(config.data)
47
+ return (context) => Topology.makeStateInput(hasProperty(config, "data") ? resolve(context) : {})
48
+ }
49
+
50
+ interface Captured {
51
+ readonly node: Machine.StateNodeConfig | Machine.TaggedSchema
52
+ readonly handlers: Record<string, unknown>
53
+ }
54
+ export const capture = (
55
+ root: State<Machine.StateNodeConfig>,
56
+ handler: Readonly<Record<string, unknown>>
57
+ ): Captured => {
58
+ const visit = (
59
+ node: Machine.StateNodeConfig | Machine.TaggedSchema,
60
+ raw: Readonly<Record<string, unknown>>,
61
+ path: string
62
+ ): Captured => {
63
+ if (hasProperty(raw, "initialize")) throw new Error("Machine child construction belongs in initial declarations")
64
+ if (path !== "" && hasProperty(raw, "root")) {
65
+ throw new Error("Machine root construction belongs at the root handler")
66
+ }
67
+ const { initial, root: _root, states: rawChildren, ...handlers } = raw
68
+ if (Schema.isSchema(node) || !("states" in node)) {
69
+ if (initial !== undefined) throw new Error(`Machine state "${path}" cannot declare initial children`)
70
+ if (rawChildren !== undefined) throw new Error(`Machine state "${path}" has no children`)
71
+ return { node, handlers }
72
+ }
73
+ const childHandlers = rawChildren === undefined ? {} : record(rawChildren, "child handlers")
74
+ for (const key of Object.keys(childHandlers)) {
75
+ if (!Object.prototype.hasOwnProperty.call(node.states, key)) throw new Error(`Machine unknown child "${key}"`)
76
+ }
77
+ const states: Record<string, Machine.StateNodeConfig | Machine.TaggedSchema> = Object.create(null)
78
+ const children: Record<string, unknown> = Object.create(null)
79
+ const constructors: Record<string, Constructor> = Object.create(null)
80
+ const parallel = node.type === "parallel"
81
+ let initialKey: string | undefined
82
+ if (parallel) {
83
+ const regions = initial === undefined ? {} : record(initial, "parallel initial regions")
84
+ for (const [key, value] of Object.entries(regions)) {
85
+ const child = node.states[key]
86
+ if (child === undefined || (!Schema.isSchema(child) && (child.type === "history" || child.type === "choice"))) {
87
+ throw new Error(`Machine invalid initial region "${key}"`)
88
+ }
89
+ if (!Schema.isSchema(child) && (!("schema" in child) || child.schema === undefined)) {
90
+ throw new Error(`Machine schema-less region "${key}" cannot construct data`)
91
+ }
92
+ constructors[key] = construction(value)
93
+ }
94
+ } else {
95
+ const edge = record(initial, `initial edge for "${path}"`)
96
+ if (Reflect.ownKeys(edge).some((key) => !["target", "data", "decoded"].includes(String(key)))) {
97
+ throw new Error("Machine initial edge accepts only target, data, and decoded")
98
+ }
99
+ if (!hasProperty(edge.target, Reference.TypeId)) throw new Error("Machine initial requires a target reference")
100
+ const ref = (edge.target as Reference.Reference)[Reference.TypeId]
101
+ const prefix = path === "" ? "" : `${path}.`
102
+ const key = ref.path.slice(prefix.length)
103
+ if (
104
+ ref.root !== root || !ref.path.startsWith(prefix) || key.includes(".") || !(key in node.states) ||
105
+ ref.kind === "history"
106
+ ) {
107
+ throw new Error(`Machine initial target must be a direct child of "${path}"`)
108
+ }
109
+ initialKey = key
110
+ if (ref.kind === "choice" && (hasProperty(edge, "data") || hasProperty(edge, "decoded"))) {
111
+ throw new Error("Machine choice initial edges cannot construct data")
112
+ }
113
+ const construct = edgeConstruction(edge)
114
+ const child = node.states[key]!
115
+ if (
116
+ hasProperty(edge, "data") && !Schema.isSchema(child) && (!("schema" in child) || child.schema === undefined)
117
+ ) {
118
+ throw new Error(`Machine schema-less initial child "${key}" cannot construct data`)
119
+ }
120
+ if (hasProperty(edge, "data")) constructors[key] = construct
121
+ }
122
+ for (const [key, child] of Object.entries(node.states)) {
123
+ const childPath = path === "" ? key : `${path}.${key}`
124
+ const captured = visit(
125
+ child,
126
+ childHandlers[key] === undefined ? {} : record(childHandlers[key], "state handler"),
127
+ childPath
128
+ )
129
+ states[key] = captured.node
130
+ // History configuration is owned by its parent, not a state handler.
131
+ if (
132
+ (Schema.isSchema(child) || child.type !== "history") &&
133
+ (childHandlers[key] !== undefined || (!Schema.isSchema(child) && "states" in child))
134
+ ) children[key] = captured.handlers
135
+ }
136
+ const compiled = Object.freeze({
137
+ ...node,
138
+ states: Object.freeze(states),
139
+ ...(parallel ? {} : { initial: initialKey })
140
+ })
141
+ const entries = Object.entries(constructors)
142
+ const initialize = (context: Context) => {
143
+ const ancestors = typeof context.ancestors === "object" && context.ancestors !== null
144
+ ? context.ancestors as Readonly<Record<string, unknown>> :
145
+ {}
146
+ const scoped = { ...context, root: path === "" ? context.state : ancestors[""] }
147
+ const values = Object.fromEntries(entries.map(([key, construct]) => [key, construct(scoped)]))
148
+ return { [SnapshotBuilderStateTypeId]: values }
149
+ }
150
+ initializers.add(initialize)
151
+ return {
152
+ node: compiled,
153
+ handlers: { ...handlers, ...(entries.length === 0 ? {} : { initialize }), states: children }
154
+ }
155
+ }
156
+ return visit(root.node, handler, "")
157
+ }
@@ -13,7 +13,7 @@ import { InfiniteTransitionError, MachineSchemaDecodeError, StoppedError } from
13
13
  import * as InvocationDefinition from "./invocationDefinition.js"
14
14
  import * as InvocationEvent from "./invocationEvent.js"
15
15
  import * as Planner from "./planner.js"
16
- import * as Runtime from "./runtime.js"
16
+ import * as Runtime from "./runtimeProtocol.js"
17
17
  import { ChildMachineLogicTypeId } from "./symbols.js"
18
18
 
19
19
  /** @internal */
@@ -61,7 +61,7 @@ const streamLogic = (
61
61
  }
62
62
  })
63
63
 
64
- const resolveValue = (value: unknown, context: Machine.InvokeContext<any, any, any, any>): unknown =>
64
+ const resolveValue = (value: unknown, context: Omit<Machine.InvokeContext<any, any, any, any>, "root">): unknown =>
65
65
  typeof value === "function" ? value(context) : value
66
66
 
67
67
  const makeChildOwner = (scope: Runtime.ProcessScope<any>): ChildOwner<any> => ({
@@ -76,7 +76,7 @@ const makeChildOwner = (scope: Runtime.ProcessScope<any>): ChildOwner<any> => ({
76
76
 
77
77
  const resolveOne = (
78
78
  raw: InvocationDefinition.InvocationDefinition,
79
- context: Machine.InvokeContext<any, any, any, any>,
79
+ context: Omit<Machine.InvokeContext<any, any, any, any>, "root">,
80
80
  path: string
81
81
  ): AnyConfig => {
82
82
  if ("effect" in raw) {
@@ -6,7 +6,7 @@ import type { EventTransition } from "./transition.js"
6
6
 
7
7
  // Source callbacks cross the erased machine-definition boundary here. Their
8
8
  // state, event, error and service relationships are checked by the public builder.
9
- type Context = Machine.InvokeContext<any, any, any, any>
9
+ type Context = Omit<Machine.InvokeContext<any, any, any, any>, "root">
10
10
  interface Outcomes {
11
11
  readonly onDone?: EventTransition<any, any, any, any>
12
12
  readonly onFailure?: EventTransition<any, any, any, any>