@statelyai/agent 2.0.0-alpha.24 → 2.0.0-alpha.26

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 (53) hide show
  1. package/dist/ai-sdk.cjs +30 -34
  2. package/dist/ai-sdk.d.cts +27 -31
  3. package/dist/ai-sdk.d.mts +27 -31
  4. package/dist/ai-sdk.mjs +27 -31
  5. package/dist/decision-CM9CYZ6t.cjs +519 -0
  6. package/dist/decision-DzSv1NpD.mjs +453 -0
  7. package/dist/event-log-store-Ba2fsc4E.mjs +576 -0
  8. package/dist/event-log-store-DQmImLYk.cjs +702 -0
  9. package/dist/event-log-store-DvOcs10J.d.cts +338 -0
  10. package/dist/event-log-store-DxyVgnas.d.mts +338 -0
  11. package/dist/index.cjs +429 -4272
  12. package/dist/index.d.cts +104 -1005
  13. package/dist/index.d.mts +104 -1005
  14. package/dist/index.mjs +389 -4202
  15. package/dist/log.cjs +392 -0
  16. package/dist/log.d.cts +32 -0
  17. package/dist/log.d.mts +32 -0
  18. package/dist/log.mjs +371 -0
  19. package/dist/machines.cjs +27 -16
  20. package/dist/machines.d.cts +1 -1
  21. package/dist/machines.d.mts +1 -1
  22. package/dist/machines.mjs +27 -16
  23. package/dist/openai.cjs +16 -15
  24. package/dist/openai.d.cts +9 -9
  25. package/dist/openai.d.mts +9 -9
  26. package/dist/openai.mjs +10 -9
  27. package/dist/otel.d.cts +1 -1
  28. package/dist/otel.d.mts +1 -1
  29. package/dist/{run-agent-Ck7Sa3Tb.d.mts → run-agent-BOfoJg5B.d.cts} +86 -387
  30. package/dist/{run-agent-DWqA8KiI.d.cts → run-agent-CG343kFs.d.mts} +86 -387
  31. package/dist/run-agent-Cb1-LS5P.cjs +1783 -0
  32. package/dist/run-agent-DvDQd4If.mjs +1669 -0
  33. package/dist/{setup-agent-CeO2fARv.mjs → setup-agent-CFW-jPI8.mjs} +9 -20
  34. package/dist/{setup-agent-Dl2VELLi.cjs → setup-agent-CPf7MxnG.cjs} +20 -37
  35. package/dist/testing.cjs +1088 -0
  36. package/dist/testing.d.cts +617 -0
  37. package/dist/testing.d.mts +617 -0
  38. package/dist/testing.mjs +1079 -0
  39. package/dist/{text-logic-DJBK3LbG.d.mts → text-logic-BFEOxyxv.d.cts} +71 -147
  40. package/dist/text-logic-Bl_ZRxDi.mjs +833 -0
  41. package/dist/text-logic-C8xyr6Ag.cjs +1091 -0
  42. package/dist/{text-logic-C7bSnH_B.d.cts → text-logic-hZSiRUo1.d.mts} +71 -147
  43. package/dist/{types-CjHk5OdH.d.cts → types-B1Qs2q3q.d.cts} +2 -1
  44. package/dist/{types-DWA6X9R-.d.mts → types-tA3SdczG.d.mts} +2 -1
  45. package/dist/usage-BdEjFuIi.mjs +5 -0
  46. package/dist/usage-efOQPdf_.cjs +10 -0
  47. package/dist/utils-C2DPqomn.d.mts +109 -0
  48. package/dist/utils-ceStNCOh.d.cts +109 -0
  49. package/package.json +26 -5
  50. package/readme.md +5 -3
  51. package/skills/generate-machine/SKILL.md +4 -6
  52. package/dist/decision-BBZ2jMO1.mjs +0 -1490
  53. package/dist/decision-Cv6SfNWl.cjs +0 -1856
@@ -0,0 +1,1079 @@
1
+ import { H as AgentError, P as getStateMeta, S as getRegisteredAgentExecutionOptions, d as getCallUsage, f as isTextLogic, p as normalizeGeneratorResult, y as executorBoundLogics } from "./text-logic-Bl_ZRxDi.mjs";
2
+ import { a as isDecisionLogic, d as isRecord, l as getAcceptedEvents, s as resolveDecision, t as AgentDecisionExhaustedError } from "./decision-DzSv1NpD.mjs";
3
+ import { _ as rejectAgentStep, d as runAgent, g as initialAgentStep, h as getPendingInvokes, v as resolveAgentStep, y as transitionAgentStep } from "./run-agent-DvDQd4If.mjs";
4
+ //#region src/verify.ts
5
+ const DECIDE_SRC = "agent.decide";
6
+ function normalizeInvokes(invoke) {
7
+ if (invoke === void 0 || invoke === null) return [];
8
+ return Array.isArray(invoke) ? invoke : [invoke];
9
+ }
10
+ function buildStateIndex(rootConfig) {
11
+ const index = /* @__PURE__ */ new Map();
12
+ const walk = (states, parentPath) => {
13
+ for (const [name, config] of Object.entries(states ?? {})) {
14
+ const path = parentPath ? `${parentPath}.${name}` : name;
15
+ const hasChildren = !!config.states && Object.keys(config.states).length > 0;
16
+ index.set(path, {
17
+ path,
18
+ config,
19
+ parentPath,
20
+ isFinal: config.type === "final",
21
+ isParallel: config.type === "parallel",
22
+ isCompound: hasChildren && config.type !== "parallel",
23
+ invokes: normalizeInvokes(config.invoke)
24
+ });
25
+ if (hasChildren) walk(config.states, path);
26
+ }
27
+ };
28
+ walk(rootConfig.states, "");
29
+ return index;
30
+ }
31
+ function ancestorChain(node, index) {
32
+ const chain = [];
33
+ let parent = node.parentPath;
34
+ while (parent) {
35
+ const parentNode = index.get(parent);
36
+ if (!parentNode) break;
37
+ chain.push(parentNode);
38
+ parent = parentNode.parentPath;
39
+ }
40
+ return chain;
41
+ }
42
+ function hasNonEmptyOn(config) {
43
+ return !!config.on && Object.keys(config.on).length > 0;
44
+ }
45
+ function isDecisionInvoke(src, actors) {
46
+ if (typeof src === "string") return src === DECIDE_SRC || isDecisionLogic(actors[src]);
47
+ return isDecisionLogic(src);
48
+ }
49
+ function isAgentLogicNeedingBinding(src) {
50
+ return (isTextLogic(src) || isDecisionLogic(src)) && !executorBoundLogics.has(src);
51
+ }
52
+ function checkDecideWithoutEvents(ctx) {
53
+ const out = [];
54
+ for (const node of ctx.index.values()) for (const invoke of node.invokes) {
55
+ if (!isDecisionInvoke(invoke.src, ctx.actors)) continue;
56
+ const selfHandles = hasNonEmptyOn(node.config);
57
+ const ancestorHandles = ancestorChain(node, ctx.index).some((ancestor) => hasNonEmptyOn(ancestor.config));
58
+ const invokeObserves = invoke.onDone !== void 0;
59
+ if (selfHandles || ancestorHandles || invokeObserves) continue;
60
+ const srcName = typeof invoke.src === "string" ? invoke.src : "(inline logic)";
61
+ out.push({
62
+ code: "decide-without-events",
63
+ severity: "error",
64
+ path: node.path,
65
+ message: `State '${node.path}' invokes decision source '${srcName}', but neither it nor any ancestor handles any event (no 'on:'), so the decision's chosen event can never be delivered. Add an 'on:' handler for the candidate events.`
66
+ });
67
+ }
68
+ return out;
69
+ }
70
+ function checkDirectObjectSrc(ctx) {
71
+ const out = [];
72
+ for (const node of ctx.index.values()) for (const invoke of node.invokes) {
73
+ const src = invoke.src;
74
+ if (typeof src === "string" || !src || typeof src !== "object") continue;
75
+ if (!isAgentLogicNeedingBinding(src)) continue;
76
+ out.push({
77
+ code: "direct-object-src",
78
+ severity: "warning",
79
+ path: node.path,
80
+ message: `State '${node.path}' invokes a direct-object agent logic. Direct-object invoke srcs cannot be rebound by runAgent, so they inherit no host executors — call '.withExecutor(...)' on the logic, or register it as a string-keyed actor source (machine.provide({ actors: { name: logic } })) and invoke it by name.`
81
+ });
82
+ }
83
+ return out;
84
+ }
85
+ function handlesActorError(config) {
86
+ if (config["onError"] !== void 0) return true;
87
+ return Object.keys(config.on ?? {}).some((type) => type === "*" || type.startsWith("xstate.error"));
88
+ }
89
+ function checkInvokeWithoutOnError(ctx) {
90
+ const out = [];
91
+ for (const node of ctx.index.values()) for (const invoke of node.invokes) {
92
+ if (invoke.onError !== void 0 || handlesActorError(node.config)) continue;
93
+ if (handlesActorError(ctx.config) || ancestorChain(node, ctx.index).some((ancestor) => handlesActorError(ancestor.config))) continue;
94
+ const srcName = typeof invoke.src === "string" ? invoke.src : "(inline logic)";
95
+ out.push({
96
+ code: "invoke-without-on-error",
97
+ severity: "warning",
98
+ path: node.path,
99
+ message: `State '${node.path}' invokes '${srcName}' with no 'onError' transition, and no ancestor handles an actor error. A rejected request or actor puts the machine in an error state with no modeled recovery. Add 'onError' (a 'failed' final state, or a bounded retry).`
100
+ });
101
+ }
102
+ return out;
103
+ }
104
+ const LINT_CHECKS = [
105
+ checkDecideWithoutEvents,
106
+ checkDirectObjectSrc,
107
+ checkInvokeWithoutOnError
108
+ ];
109
+ /**
110
+ * Runs static structural checks over a built agent machine and returns the
111
+ * findings ({@link AgentLintDiagnostic}[], empty when clean). Works for
112
+ * TS-authored (`setupAgent(...).createMachine(...)`) and
113
+ * `setupAgent.fromConfig(...)`-compiled machines alike, reading `machine.config`
114
+ * plus the schemas/actor sources the library already retains per machine.
115
+ *
116
+ * No model calls, no API keys — a coding agent that emits an agent machine can
117
+ * call this to catch agent-specific mistakes such as undeliverable decisions,
118
+ * un-rebindable request sources, and an unhandled transcript event. General
119
+ * state-machine lint belongs to XState tooling.
120
+ *
121
+ * Pass `{ throw: true }` for the one-liner form used in tests and generation
122
+ * loops: it returns silently when the machine is clean and throws
123
+ * {@link AgentLintError} (findings on `.diagnostics`) on error-severity
124
+ * findings, or on warnings too with `{ throw: true, warnings: true }`.
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * const errors = lintAgentMachine(machine).filter((d) => d.severity === 'error');
129
+ * if (errors.length) throw new Error(errors.map((e) => `${e.path}: ${e.message}`).join('\n'));
130
+ * ```
131
+ *
132
+ * @example Throwing form
133
+ * ```ts
134
+ * test('agent machine is structurally sound', () => {
135
+ * lintAgentMachine(machine, { throw: true });
136
+ * });
137
+ * ```
138
+ */
139
+ function lintAgentMachine(machine, options = {}) {
140
+ const config = machine.config ?? {};
141
+ const index = buildStateIndex(config);
142
+ const registered = getRegisteredAgentExecutionOptions(machine);
143
+ const ctx = {
144
+ config,
145
+ index,
146
+ schemas: registered.schemas,
147
+ actors: registered.actors ?? machine.sources?.actors ?? {}
148
+ };
149
+ const disabled = new Set(options.disable ?? []);
150
+ const diagnostics = LINT_CHECKS.flatMap((check) => check(ctx)).filter((d) => !disabled.has(d.code));
151
+ if (options.throw) {
152
+ const failing = options.warnings ? diagnostics : diagnostics.filter((d) => d.severity === "error");
153
+ if (failing.length > 0) throw new AgentLintError(machine.id ?? "(machine)", failing);
154
+ }
155
+ return diagnostics;
156
+ }
157
+ /**
158
+ * Thrown by `lintAgentMachine(machine, { throw: true })` (and its
159
+ * {@link assertAgentMachine} alias) when lint finds failing diagnostics.
160
+ * `diagnostics` holds the findings; the message lists them one per finding,
161
+ * so a test runner's failure output reads like the CLI's lint report.
162
+ */
163
+ var AgentLintError = class extends AgentError {
164
+ diagnostics;
165
+ constructor(machineId, diagnostics) {
166
+ const lines = diagnostics.map((d) => ` ${d.severity === "error" ? "error" : "warn "} ${d.code} ${d.path}\n ${d.message}`);
167
+ super("lint-failed", `Agent machine '${machineId}' failed lint (${diagnostics.length} finding(s)):\n${lines.join("\n")}`);
168
+ this.name = "AgentLintError";
169
+ this.diagnostics = diagnostics;
170
+ }
171
+ };
172
+ /**
173
+ * Asserts a machine passes {@link lintAgentMachine}: returns silently when
174
+ * clean, throws {@link AgentLintError} (with the findings on `.diagnostics`)
175
+ * otherwise — sugar for `lintAgentMachine(machine, { ...options, throw: true })`.
176
+ * Fails on error-severity findings; set `warnings: true` to fail on warnings
177
+ * too. The one-liner for tests and generation loops:
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * test('agent machine is structurally sound', () => {
182
+ * assertAgentMachine(machine);
183
+ * });
184
+ * ```
185
+ */
186
+ function assertAgentMachine(machine, options = {}) {
187
+ lintAgentMachine(machine, {
188
+ ...options,
189
+ throw: true
190
+ });
191
+ }
192
+ function pendingInvokes(step) {
193
+ const out = [];
194
+ for (const metadata of getPendingInvokes(step.snapshot)) if (typeof metadata.src === "string") out.push({
195
+ id: metadata.id,
196
+ src: metadata.src
197
+ });
198
+ return out;
199
+ }
200
+ function takeFromQueue(channel, src) {
201
+ const queue = channel?.[src];
202
+ if (queue && queue.length > 0) return {
203
+ found: true,
204
+ value: queue.shift()
205
+ };
206
+ return { found: false };
207
+ }
208
+ /**
209
+ * Deterministically plays a machine through, resolving each request from a
210
+ * {@link SimulationScript} instead of a model — no API keys, no model calls.
211
+ * Runs on the pure step path ({@link initialAgentStep} etc.), so it exercises
212
+ * the real transition logic. Returns the terminal `status`, final `snapshot`,
213
+ * and a `trail` of every step taken.
214
+ *
215
+ * Decisions run through the live run's own validation/retry core,
216
+ * {@link resolveDecision}, with the script standing in for the model: each
217
+ * attempt consumes the next queued {@link ChosenEvent} for that src, so an
218
+ * unknown, payload-invalid, or guard-rejected event is NOT silently swallowed
219
+ * — the next queued decision is tried, exactly as a live run re-asks the
220
+ * model. The decision logic's `maxRetries` caps attempts as it would live;
221
+ * when retries continue past the end of the queue, the last queued decision
222
+ * repeats (a scripted model that insists). The repeat applies only within one
223
+ * decision request's retries — each new decision request must have its own
224
+ * queued entry, or the dry-script error throws as usual. Exhausting all
225
+ * attempts delivers
226
+ * the resulting {@link AgentDecisionExhaustedError} to the machine as the
227
+ * decision invoke's error (so an `onError` transition observes it, as it
228
+ * would live); with no `onError` to catch it, it is thrown.
229
+ *
230
+ * Throws a descriptive error when the script runs dry mid-request, naming the
231
+ * pending request's kind, src, and id so the missing scripted response is
232
+ * obvious.
233
+ *
234
+ * @example
235
+ * ```ts
236
+ * const { status, snapshot } = simulateAgent(machine, {
237
+ * input: { topic: 'state machines' },
238
+ * script: {
239
+ * decisions: { 'agent.decide': [{ type: 'ESCALATE' }] },
240
+ * events: [{ type: 'APPROVE' }], // crosses the human gate
241
+ * },
242
+ * });
243
+ * ```
244
+ */
245
+ async function simulateAgent(machine, options) {
246
+ const maxSteps = options.maxSteps ?? 100;
247
+ const script = {
248
+ text: mapValues(options.script.text ?? {}, (arr) => [...arr]),
249
+ decisions: mapValues(options.script.decisions ?? {}, (arr) => [...arr]),
250
+ invokes: mapValues(options.script.invokes ?? {}, (arr) => [...arr]),
251
+ errors: mapValues(options.script.errors ?? {}, (arr) => [...arr]),
252
+ events: [...options.script.events ?? []]
253
+ };
254
+ let step = initialAgentStep(machine, options.input);
255
+ const trail = [{ state: step.snapshot.value }];
256
+ for (let i = 0; i < maxSteps; i++) {
257
+ if (step.done) return {
258
+ status: "done",
259
+ snapshot: step.snapshot,
260
+ trail
261
+ };
262
+ const request = step.requests[0];
263
+ if (request) {
264
+ if (request.kind === "decision") {
265
+ const invokeMeta = findInvokeMetadata(step, request.id);
266
+ const src = invokeMeta?.src ?? request.id;
267
+ const decisionSrc = src in (script.decisions ?? {}) ? src : request.id in (script.decisions ?? {}) ? request.id : src;
268
+ const errorSrc = queueKey(script.errors, src, request.id);
269
+ const failure = takeFromQueue(script.errors, errorSrc);
270
+ if (failure.found) {
271
+ step = rejectScriptedRequest(machine, step, request.id, failure.value, {
272
+ kind: "decision",
273
+ src: errorSrc,
274
+ trail
275
+ });
276
+ continue;
277
+ }
278
+ step = await applyScriptedDecision(machine, step, request, decisionSrc, invokeMeta?.logic, script, trail);
279
+ continue;
280
+ }
281
+ const failure = takeFromQueue(script.errors, request.src);
282
+ if (failure.found) {
283
+ step = rejectScriptedRequest(machine, step, request, failure.value, {
284
+ kind: "text",
285
+ src: request.src,
286
+ trail
287
+ });
288
+ continue;
289
+ }
290
+ const taken = takeFromQueue(script.text, request.src);
291
+ if (!taken.found) throw scriptDryError("text", request.src, request.id);
292
+ step = resolveAgentStep(machine, step, request, asTextResult(taken.value));
293
+ trail.push({
294
+ state: step.snapshot.value,
295
+ resolvedRequest: {
296
+ kind: "text",
297
+ src: request.src,
298
+ id: request.id,
299
+ outcome: "output"
300
+ }
301
+ });
302
+ continue;
303
+ }
304
+ const [invoke] = pendingInvokes(step);
305
+ if (invoke) {
306
+ const failure = takeFromQueue(script.errors, invoke.src);
307
+ if (failure.found) {
308
+ step = rejectScriptedRequest(machine, step, invoke.id, failure.value, {
309
+ kind: "invoke",
310
+ src: invoke.src,
311
+ trail
312
+ });
313
+ continue;
314
+ }
315
+ const taken = takeFromQueue(script.invokes, invoke.src);
316
+ if (!taken.found) throw scriptDryError("invoke", invoke.src, invoke.id);
317
+ step = resolveAgentStep(machine, step, invoke.id, taken.value);
318
+ trail.push({
319
+ state: step.snapshot.value,
320
+ resolvedRequest: {
321
+ kind: "invoke",
322
+ src: invoke.src,
323
+ id: invoke.id,
324
+ outcome: "output"
325
+ }
326
+ });
327
+ continue;
328
+ }
329
+ const external = script.events;
330
+ if (external.length > 0) {
331
+ const event = external.shift();
332
+ if (!step.snapshot.can(event)) throw new Error(`simulateAgent: scripted external event '${event.type}' cannot be taken in state ${JSON.stringify(step.snapshot.value)} (no handler, or its guard rejected it). A live run's send would be silently dropped here; fix the script's \`events\` queue.`);
333
+ step = transitionAgentStep(machine, step, event);
334
+ trail.push({
335
+ state: step.snapshot.value,
336
+ appliedEvent: event,
337
+ external: true
338
+ });
339
+ continue;
340
+ }
341
+ return {
342
+ status: "idle",
343
+ snapshot: step.snapshot,
344
+ trail
345
+ };
346
+ }
347
+ return {
348
+ status: "exhausted",
349
+ snapshot: step.snapshot,
350
+ trail
351
+ };
352
+ }
353
+ function queueKey(channel, src, id) {
354
+ return channel && !(src in channel) && id in channel ? id : src;
355
+ }
356
+ function rejectScriptedRequest(machine, step, request, error, entry) {
357
+ const id = typeof request === "string" ? request : request.id;
358
+ const next = rejectAgentStep(machine, step, request, error);
359
+ if (next.snapshot.status === "error") throw unhandledScriptedError(error, entry.kind, entry.src, id);
360
+ entry.trail.push({
361
+ state: next.snapshot.value,
362
+ resolvedRequest: {
363
+ kind: entry.kind,
364
+ src: entry.src,
365
+ id,
366
+ outcome: "error",
367
+ error
368
+ }
369
+ });
370
+ return next;
371
+ }
372
+ function unhandledScriptedError(error, kind, src, id) {
373
+ if (error instanceof Error) return error;
374
+ return new Error(`simulateAgent: the scripted error for the ${kind} request '${src}' (id '${id}') reached no onError transition, so the machine errored. Add an onError to that invoke, or drop the errors['${src}'] entry.`, { cause: error });
375
+ }
376
+ async function applyScriptedDecision(machine, step, request, decisionSrc, invokeLogic, script, trail) {
377
+ const dequeued = [];
378
+ const decide = async () => {
379
+ const taken = takeFromQueue(script.decisions, decisionSrc);
380
+ if (taken.found) {
381
+ dequeued.push(taken.value);
382
+ return { event: taken.value };
383
+ }
384
+ if (dequeued.length === 0) throw scriptDryError("decision", decisionSrc, request.id, request);
385
+ return { event: dequeued[dequeued.length - 1] };
386
+ };
387
+ const maxRetries = (isDecisionLogic(invokeLogic) ? invokeLogic : resolveRegisteredDecisionLogic(machine, decisionSrc))?.maxRetries;
388
+ let chosen;
389
+ try {
390
+ chosen = await resolveDecision(request, { decide }, {
391
+ maxRetries,
392
+ canTake: (event) => step.snapshot.can(event)
393
+ });
394
+ } catch (error) {
395
+ if (!(error instanceof AgentDecisionExhaustedError)) throw error;
396
+ const next = rejectAgentStep(machine, step, request.id, error);
397
+ if (next.snapshot.status === "error") throw error;
398
+ trail.push({
399
+ state: next.snapshot.value,
400
+ rejectedEvents: dequeued
401
+ });
402
+ return next;
403
+ }
404
+ const next = transitionAgentStep(machine, step, chosen);
405
+ const rejected = dequeued.slice(0, -1);
406
+ trail.push({
407
+ state: next.snapshot.value,
408
+ appliedEvent: chosen,
409
+ ...rejected.length > 0 ? { rejectedEvents: rejected } : {}
410
+ });
411
+ return next;
412
+ }
413
+ function findInvokeMetadata(step, id) {
414
+ for (const metadata of getPendingInvokes(step.snapshot)) {
415
+ if (metadata.id !== id) continue;
416
+ return {
417
+ ...typeof metadata.src === "string" ? { src: metadata.src } : {},
418
+ logic: metadata.logic ?? (typeof metadata.src === "object" ? metadata.src : void 0)
419
+ };
420
+ }
421
+ }
422
+ function resolveRegisteredDecisionLogic(machine, src) {
423
+ const candidate = getRegisteredAgentExecutionOptions(machine).actors?.[src] ?? machine.sources?.actors?.[src];
424
+ return isDecisionLogic(candidate) ? candidate : void 0;
425
+ }
426
+ function asTextResult(value) {
427
+ return {
428
+ result: value,
429
+ messages: []
430
+ };
431
+ }
432
+ function mapValues(obj, fn) {
433
+ return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, fn(value)]));
434
+ }
435
+ function scriptDryError(kind, src, id, request) {
436
+ const events = request?.kind === "decision" ? ` Candidate events: ${request.events.map((e) => e.type).join(", ") || "(none)"}.` : "";
437
+ const key = kind === "text" ? `text['${src}']` : kind === "decision" ? `decisions['${src}']` : `invokes['${src}']`;
438
+ return /* @__PURE__ */ new Error(`simulateAgent: script ran dry on a pending ${kind} request for src '${src}' (id '${id}'). Add an entry to the script's \`${key}\` queue.${events}`);
439
+ }
440
+ const MAX_ADVANCE_STEPS = 1e3;
441
+ function invokeFork(id, src, outputs) {
442
+ return src in outputs ? {
443
+ id,
444
+ src,
445
+ hasOutput: true,
446
+ output: outputs[src]
447
+ } : {
448
+ id,
449
+ src,
450
+ hasOutput: false
451
+ };
452
+ }
453
+ async function explore(machine, options, stopWhen) {
454
+ const maxDepth = options.maxDepth ?? 8;
455
+ const maxPaths = options.maxPaths ?? 200;
456
+ const textScript = options.text ?? {};
457
+ const invokeOutputs = { ...options.invokes };
458
+ const errorScript = options.errors ?? {};
459
+ const reachedStates = /* @__PURE__ */ new Set();
460
+ const reachedValues = [];
461
+ const terminals = [];
462
+ const unexplored = [];
463
+ let prunedByGuard = 0;
464
+ let pathsExplored = 0;
465
+ let hitPathCap = false;
466
+ let witness;
467
+ const recordState = (snapshot) => {
468
+ const key = JSON.stringify(snapshot.value);
469
+ if (!reachedStates.has(key)) {
470
+ reachedStates.add(key);
471
+ reachedValues.push(snapshot.value);
472
+ }
473
+ };
474
+ const initial = initialAgentStep(machine, options.input);
475
+ recordState(initial.snapshot);
476
+ if (stopWhen?.(initial.snapshot)) witness = [];
477
+ const advance = (step) => {
478
+ let current = step;
479
+ for (let i = 0; i < MAX_ADVANCE_STEPS; i++) {
480
+ if (stopWhen?.(current.snapshot)) return {
481
+ step: current,
482
+ hit: true
483
+ };
484
+ if (current.done) return { step: current };
485
+ const request = current.requests[0];
486
+ if (request && request.kind === "text") {
487
+ if (request.src in errorScript) return {
488
+ step: current,
489
+ fork: invokeFork(request.id, request.src, textScript)
490
+ };
491
+ if (!(request.src in textScript)) return {
492
+ step: current,
493
+ blockedSrc: request.src
494
+ };
495
+ current = resolveAgentStep(machine, current, request, asTextResult(textScript[request.src]));
496
+ recordState(current.snapshot);
497
+ continue;
498
+ }
499
+ if (request && request.kind === "decision") {
500
+ const src = findInvokeMetadata(current, request.id)?.src;
501
+ const errorSrc = src !== void 0 && src in errorScript ? src : request.id in errorScript ? request.id : void 0;
502
+ return errorSrc !== void 0 ? {
503
+ step: current,
504
+ fork: {
505
+ id: request.id,
506
+ src: errorSrc,
507
+ hasOutput: false,
508
+ isDecision: true
509
+ }
510
+ } : { step: current };
511
+ }
512
+ const [invoke] = pendingInvokes(current);
513
+ if (invoke) {
514
+ if (invoke.src in errorScript) return {
515
+ step: current,
516
+ fork: invokeFork(invoke.id, invoke.src, invokeOutputs)
517
+ };
518
+ if (!(invoke.src in invokeOutputs)) return {
519
+ step: current,
520
+ blockedSrc: invoke.src
521
+ };
522
+ current = resolveAgentStep(machine, current, invoke.id, invokeOutputs[invoke.src]);
523
+ recordState(current.snapshot);
524
+ continue;
525
+ }
526
+ return { step: current };
527
+ }
528
+ return { step: current };
529
+ };
530
+ const visit = async (step, path, startDepth) => {
531
+ let depth = startDepth;
532
+ if (witness !== void 0) return;
533
+ if (pathsExplored >= maxPaths) {
534
+ hitPathCap = true;
535
+ return;
536
+ }
537
+ const { step: settled, blockedSrc, hit, fork } = advance(step);
538
+ if (hit) {
539
+ witness = path;
540
+ return;
541
+ }
542
+ if (fork) {
543
+ if (depth >= maxDepth) {
544
+ pathsExplored++;
545
+ terminals.push({
546
+ status: "max-depth",
547
+ path,
548
+ state: settled.snapshot.value
549
+ });
550
+ unexplored.push(`max-depth: stopped at path [${path.map((e) => e.type).join(", ")}]`);
551
+ return;
552
+ }
553
+ const failed = rejectAgentStep(machine, settled, fork.id, errorScript[fork.src]);
554
+ recordState(failed.snapshot);
555
+ await visit(failed, [...path, { type: `xstate.error.actor.${fork.id}` }], depth + 1);
556
+ if (!fork.isDecision) {
557
+ if (fork.hasOutput) {
558
+ const resolved = resolveAgentStep(machine, settled, fork.id, fork.output);
559
+ recordState(resolved.snapshot);
560
+ await visit(resolved, path, depth + 1);
561
+ }
562
+ return;
563
+ }
564
+ depth += 1;
565
+ if (witness !== void 0) return;
566
+ }
567
+ if (blockedSrc) {
568
+ pathsExplored++;
569
+ terminals.push({
570
+ status: "needs-output",
571
+ path,
572
+ state: settled.snapshot.value,
573
+ missingSrc: blockedSrc
574
+ });
575
+ unexplored.push(`needs-output: no canned output for src '${blockedSrc}' at path [${path.map((e) => e.type).join(", ")}]`);
576
+ return;
577
+ }
578
+ if (settled.done) {
579
+ pathsExplored++;
580
+ terminals.push({
581
+ status: "done",
582
+ path,
583
+ state: settled.snapshot.value
584
+ });
585
+ return;
586
+ }
587
+ if (settled.snapshot.status === "error") {
588
+ pathsExplored++;
589
+ terminals.push({
590
+ status: "error",
591
+ path,
592
+ state: settled.snapshot.value,
593
+ error: settled.snapshot.error
594
+ });
595
+ return;
596
+ }
597
+ const request = settled.requests[0];
598
+ const branchEvents = request?.kind === "decision" ? request.events.map((descriptor) => ({ type: descriptor.type })) : getAcceptedEvents(settled.snapshot).map((descriptor) => ({ type: descriptor.type }));
599
+ if (branchEvents.length === 0) {
600
+ pathsExplored++;
601
+ terminals.push({
602
+ status: "idle",
603
+ path,
604
+ state: settled.snapshot.value
605
+ });
606
+ return;
607
+ }
608
+ if (depth >= maxDepth) {
609
+ pathsExplored++;
610
+ terminals.push({
611
+ status: "max-depth",
612
+ path,
613
+ state: settled.snapshot.value
614
+ });
615
+ unexplored.push(`max-depth: stopped at path [${path.map((e) => e.type).join(", ")}]`);
616
+ return;
617
+ }
618
+ for (const event of branchEvents) {
619
+ if (witness !== void 0 || pathsExplored >= maxPaths) {
620
+ if (pathsExplored >= maxPaths) hitPathCap = true;
621
+ return;
622
+ }
623
+ if (!settled.snapshot.can(event)) {
624
+ prunedByGuard++;
625
+ continue;
626
+ }
627
+ const next = transitionAgentStep(machine, settled, event);
628
+ recordState(next.snapshot);
629
+ await visit(next, [...path, event], depth + 1);
630
+ }
631
+ };
632
+ if (witness === void 0) await visit(initial, [], 0);
633
+ return {
634
+ report: {
635
+ reachedStates: reachedValues,
636
+ terminals,
637
+ prunedByGuard,
638
+ unexplored,
639
+ pathsExplored,
640
+ hitPathCap
641
+ },
642
+ witness
643
+ };
644
+ }
645
+ /**
646
+ * Enumerates a machine's decision and external-event branches to a bounded
647
+ * depth, model-free, and reports which states are reached and how each path
648
+ * terminates. At each decision request it forks one branch per candidate event
649
+ * (guard-rejected candidates are counted in `prunedByGuard`, not explored); at
650
+ * an idle wait it forks per externally-accepted event. Text requests resolve
651
+ * from `text`, other invokes from `invokes` — all by-src canned-output maps, and a missing src halts
652
+ * that branch with a `needs-output` terminal rather than throwing. A src in
653
+ * `errors` forks an extra branch where that invoke is rejected, so states
654
+ * behind an `onError` are explored too.
655
+ *
656
+ * Combinatorics are bounded by `maxDepth` (default 8) and `maxPaths` (default
657
+ * 200, reported via `hitPathCap`).
658
+ *
659
+ * @example
660
+ * ```ts
661
+ * const report = await explorePaths(refundMachine, { input: { request: 'x', amount: 5000 } });
662
+ * // report.terminals → both 'refunded' and 'denied'; report.prunedByGuard → 1
663
+ * ```
664
+ */
665
+ async function explorePaths(machine, options = {}) {
666
+ return (await explore(machine, options)).report;
667
+ }
668
+ /** Thrown when a string reachability target is not a state in the machine. */
669
+ var AgentUnknownStateError = class extends AgentError {
670
+ target;
671
+ constructor(machineId, target) {
672
+ super("unknown-state", `canReach: machine '${machineId}' has no state matching '${target}'.`);
673
+ this.name = "AgentUnknownStateError";
674
+ this.target = target;
675
+ }
676
+ };
677
+ function resolveStateTarget(machine, target) {
678
+ const stack = [machine.root];
679
+ while (stack.length > 0) {
680
+ const node = stack.pop();
681
+ if (node.id === target.replace(/^#/, "") || node.path.join(".") === target) return node.id;
682
+ stack.push(...Object.values(node.states));
683
+ }
684
+ throw new AgentUnknownStateError(machine.id, target);
685
+ }
686
+ /**
687
+ * Answers "can the machine reach this?" by exploring its branches (a thin
688
+ * wrapper over {@link explorePaths}). The target is either a state path string
689
+ * (`snapshot.matches(...)` semantics) or a snapshot predicate — the predicate
690
+ * form checks any property (a context invariant, a tag, a state+context
691
+ * combination) without reifying a sentinel state for it. Returns
692
+ * `{ reachable: true, witness }` with the event sequence that reaches it, or
693
+ * `{ reachable: false }`.
694
+ *
695
+ * @example
696
+ * ```ts
697
+ * const { reachable, witness } = await canReach(refundMachine, 'denied', { input: { request: 'x', amount: 5000 } });
698
+ * // reachable → true; witness → [{ type: 'NEEDS_REVIEW' }, { type: 'DENY' }]
699
+ * ```
700
+ *
701
+ * @example Predicate target — a violation property, no sentinel state needed
702
+ * ```ts
703
+ * const violation = await canReach(
704
+ * refundMachine,
705
+ * (snapshot) => snapshot.matches('issued') && !snapshot.context.approved,
706
+ * { input: { amount: 5000 } },
707
+ * );
708
+ * // violation.reachable → false is the safety proof
709
+ * ```
710
+ */
711
+ async function canReach(machine, target, options = {}) {
712
+ const resolvedTarget = typeof target === "string" ? resolveStateTarget(machine, target) : void 0;
713
+ const { witness } = await explore(machine, options, typeof target === "function" ? target : (snapshot) => snapshot.nodes.some((node) => node.id === resolvedTarget));
714
+ return witness !== void 0 ? {
715
+ reachable: true,
716
+ witness,
717
+ ...resolvedTarget ? { target: resolvedTarget } : {}
718
+ } : {
719
+ reachable: false,
720
+ ...resolvedTarget ? { target: resolvedTarget } : {}
721
+ };
722
+ }
723
+ //#endregion
724
+ //#region src/trajectory.ts
725
+ /**
726
+ * Classifies one item. Event-shaped things (log entries, events) become events;
727
+ * everything else is a state value. A snapshot is unwrapped to its `value`.
728
+ * @internal
729
+ */
730
+ function normalize(item) {
731
+ if (isRecord(item)) {
732
+ const nested = item["event"];
733
+ if (isRecord(nested) && typeof nested["type"] === "string") return {
734
+ kind: "event",
735
+ event: nested
736
+ };
737
+ if (typeof item["type"] === "string") return {
738
+ kind: "event",
739
+ event: item
740
+ };
741
+ if ("value" in item) return {
742
+ kind: "state",
743
+ value: item["value"]
744
+ };
745
+ }
746
+ return {
747
+ kind: "state",
748
+ value: item
749
+ };
750
+ }
751
+ /** Every leaf of a state value as a dot path (`{ review: 'editing' }` → `review.editing`). @internal */
752
+ function statePaths(value) {
753
+ if (typeof value === "string") return [value];
754
+ if (!isRecord(value)) return [];
755
+ return Object.entries(value).flatMap(([key, child]) => statePaths(child).map((path) => `${key}.${path}`));
756
+ }
757
+ /**
758
+ * A string expectation matches a state value when it names one of its leaves or
759
+ * an ancestor of one, so `'review'` matches `{ review: 'editing' }`. @internal
760
+ */
761
+ function stateMatchesPath(path, value) {
762
+ return statePaths(value).some((leaf) => leaf === path || leaf.startsWith(`${path}.`));
763
+ }
764
+ function deepEqual(a, b) {
765
+ if (Object.is(a, b)) return true;
766
+ if (Array.isArray(a) || Array.isArray(b)) return Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((item, index) => deepEqual(item, b[index]));
767
+ if (!isRecord(a) || !isRecord(b)) return false;
768
+ const keys = Object.keys(a);
769
+ return keys.length === Object.keys(b).length && keys.every((key) => deepEqual(a[key], b[key]));
770
+ }
771
+ /**
772
+ * Normalizes ONE expected item into a predicate over actual items, so the
773
+ * expectation is classified once instead of once per candidate:
774
+ *
775
+ * - A string expectation matches an event's `type`, or a state value it names.
776
+ * - An event expectation matches an event with the same `type` whose other
777
+ * declared keys deep-equal, so payload details are opt-in.
778
+ * - A state-value expectation matches structurally.
779
+ * @internal
780
+ */
781
+ function matcherFor(expected) {
782
+ if (typeof expected === "string") return (actual) => {
783
+ const actualItem = normalize(actual);
784
+ return actualItem.kind === "event" ? actualItem.event.type === expected : stateMatchesPath(expected, actualItem.value);
785
+ };
786
+ const expectedItem = normalize(expected);
787
+ switch (expectedItem.kind) {
788
+ case "event": return (actual) => {
789
+ const actualItem = normalize(actual);
790
+ return actualItem.kind === "event" && Object.keys(expectedItem.event).every((key) => deepEqual(expectedItem.event[key], actualItem.event[key]));
791
+ };
792
+ case "state": return (actual) => {
793
+ const actualItem = normalize(actual);
794
+ return actualItem.kind === "state" && deepEqual(expectedItem.value, actualItem.value);
795
+ };
796
+ }
797
+ }
798
+ /**
799
+ * Matches a run's trajectory against an expected one.
800
+ *
801
+ * Both trajectories may be state values or events collected from
802
+ * `onTransition`;
803
+ * items are compared by shape, so `['prompting', 'drafting']` scores a state
804
+ * path and `['PROMPT_SUBMITTED', 'SEND']` scores an event log with the same
805
+ * call.
806
+ *
807
+ * Semantics are an ordered subsequence: every expected item must appear in
808
+ * `actual`, in order, with gaps allowed. Pass `{ exact: true }` to require
809
+ * equality instead.
810
+ *
811
+ * @example Trajectory assertions in a test
812
+ * ```ts
813
+ * const statePath: unknown[] = [];
814
+ * const result = await runAgent(machine, {
815
+ * input,
816
+ * executors,
817
+ * onTransition: (snapshot) => statePath.push(snapshot.value),
818
+ * });
819
+ *
820
+ * const path = matchesTrajectory(statePath, ['prompting', 'drafting', 'sent']);
821
+ * expect(path.matched, JSON.stringify(path.firstMiss)).toBe(true);
822
+ * expect(matchesTrajectory(events, ['PROMPT_SUBMITTED', 'SEND']).matched).toBe(true);
823
+ * ```
824
+ *
825
+ * @example Partial credit in an eval scorer
826
+ * ```ts
827
+ * const match = matchesTrajectory(output.statePath, expected.statePath);
828
+ * return { name: 'state_path', score: match.score, metadata: { ...match.firstMiss } };
829
+ * ```
830
+ */
831
+ function matchesTrajectory(actual, expected, options = {}) {
832
+ const expectedCount = expected.length;
833
+ if (expectedCount === 0) throw new AgentError("empty-trajectory", "matchesTrajectory: expected trajectory must contain at least one item.");
834
+ const exact = options.exact === true;
835
+ let cursor = 0;
836
+ let matchedCount = 0;
837
+ let firstMiss;
838
+ for (let index = 0; index < expectedCount; index++) {
839
+ const want = expected[index];
840
+ const matches = matcherFor(want);
841
+ const searchedFrom = exact ? index : cursor;
842
+ let found = -1;
843
+ for (let at = searchedFrom; at < actual.length; at++) {
844
+ if (matches(actual[at])) {
845
+ found = at;
846
+ break;
847
+ }
848
+ if (exact) break;
849
+ }
850
+ if (found === -1) {
851
+ firstMiss = {
852
+ index,
853
+ expected: want,
854
+ searchedFrom
855
+ };
856
+ break;
857
+ }
858
+ matchedCount++;
859
+ cursor = found + 1;
860
+ }
861
+ const matched = !firstMiss && (!exact || actual.length === expectedCount);
862
+ const denominator = exact ? Math.max(expectedCount, actual.length) : expectedCount;
863
+ return {
864
+ matched,
865
+ matchedCount,
866
+ expectedCount,
867
+ score: matched ? 1 : denominator === 0 ? 1 : matchedCount / denominator,
868
+ ...firstMiss ? { firstMiss } : {}
869
+ };
870
+ }
871
+ //#endregion
872
+ //#region src/seam.ts
873
+ /** The only own keys an executor result carries. @internal */
874
+ const TEXT_RESULT_KEYS = new Set([
875
+ "result",
876
+ "messages",
877
+ "usage",
878
+ "raw"
879
+ ]);
880
+ /**
881
+ * True when a scripted entry is the executor result rather than the output
882
+ * value: it owns a `result` key and owns NOTHING outside the result's own
883
+ * vocabulary. Bare `'result' in value` would swallow an output object's
884
+ * siblings (`{ result: 'draft', confidence: 0.9 }` would lose `confidence`)
885
+ * and would also match an inherited `result`. @internal
886
+ */
887
+ function isTextResult(value) {
888
+ return Object.hasOwn(value, "result") && Object.keys(value).every((key) => TEXT_RESULT_KEYS.has(key));
889
+ }
890
+ /**
891
+ * Resolves ONE scripted entry to an executor result: a function entry is
892
+ * called with the request, and the value is taken as the executor result only
893
+ * when it is one (see {@link isTextResult}). @internal
894
+ */
895
+ async function resolveSeamScriptEntry(entry, request, info) {
896
+ const value = typeof entry === "function" ? await entry(request, info) : entry;
897
+ return isRecord(value) && isTextResult(value) ? value : { result: value };
898
+ }
899
+ /** Names a pending text request in an error message. @internal */
900
+ function describeText(request) {
901
+ return request.name ? `'${request.name}' (model '${request.model}')` : `(model '${request.model}')`;
902
+ }
903
+ /**
904
+ * Stream semantics with no model: the whole text lands as one chunk. @internal
905
+ */
906
+ function emitScriptedChunk(result, info) {
907
+ const text = isRecord(result) ? result["result"] : void 0;
908
+ if (typeof text === "string") info?.onChunk?.(text);
909
+ }
910
+ /**
911
+ * The seam's own answer, for scoring: the executor result's `result`.
912
+ * @internal
913
+ */
914
+ async function seamOutputOf(result, request) {
915
+ if (!isRecord(result)) return;
916
+ return await normalizeGeneratorResult(result, `seam '${request.name ?? request.model}'`);
917
+ }
918
+ /**
919
+ * Runs a machine end to end with one model call under test, and slices the run
920
+ * at that call.
921
+ *
922
+ * The `before`/`after` slices are the point: `after.statePath` is the branch
923
+ * the seam chose and `after.events` is the same question against the durable
924
+ * log, both ready for `matchesTrajectory`. The state slice splits where the
925
+ * state path stood when the seam answered; the event slice splits at the
926
+ * seam's own effect completion (the first `xstate.done.*`/`xstate.error.*`
927
+ * entry appended after the call was made).
928
+ *
929
+ * @example No API key: the seam is scripted too, so the whole thing runs offline.
930
+ * ```ts
931
+ * const run = await runSeam(emailDrafter, {
932
+ * scripts: { writeFollowUps: [followUps], emailDrafter: [draft] },
933
+ * seam: { request: 'writeFollowUps' },
934
+ * respond: ({ state }) => (state === 'prompting' ? { type: 'PROMPT_SUBMITTED', prompt } : null),
935
+ * });
936
+ *
937
+ * matchesTrajectory(run.after.statePath, ['needsMoreInfo', 'drafting']);
938
+ * ```
939
+ *
940
+ * @example A candidate prompt at the seam, scored against the same rows.
941
+ * ```ts
942
+ * const { generateText } = createAiSdkExecutors({ models });
943
+ * const run = await runSeam(emailDrafter, { ...row, candidate: generateText });
944
+ * ```
945
+ */
946
+ async function runSeam(machine, options) {
947
+ const { seam, candidate } = options;
948
+ const queues = new Map(Object.entries(options.scripts ?? {}).map(([key, answers]) => [key, [...answers]]));
949
+ const statePath = [];
950
+ let calls = 0;
951
+ let seamMatches = 0;
952
+ let seamOutput;
953
+ let seamUsage;
954
+ const callLog = [];
955
+ let seamReached = false;
956
+ let callsBeforeSeam = -1;
957
+ let seamStateAt = 0;
958
+ let seamEventAt = 0;
959
+ let liveEvents = 0;
960
+ const queueKeyOf = (request) => request.name !== void 0 && queues.has(request.name) ? request.name : request.model;
961
+ /**
962
+ * Consumes this request's slot in the call plan, or resolves `undefined` when
963
+ * its queue is dry. With `repeatLast`, the last entry is replayed instead of
964
+ * running dry.
965
+ */
966
+ const takeScriptedSlot = async (request, info) => {
967
+ const queue = queues.get(queueKeyOf(request));
968
+ if (!queue?.length) return;
969
+ return resolveSeamScriptEntry(options.repeatLast && queue.length === 1 ? queue[0] : queue.shift(), request, info);
970
+ };
971
+ const scriptedAnswer = async (request, info) => {
972
+ const scripted = await takeScriptedSlot(request, info);
973
+ if (!scripted) throw new AgentError("seam-script-exhausted", `runSeam: no scripted answer left for request ${describeText(request)}. Add an entry to \`scripts.${queueKeyOf(request)}\`, or pass \`repeatLast: true\` to replay its last entry down a longer branch.`);
974
+ return scripted;
975
+ };
976
+ const route = async (request, info) => {
977
+ const callIndex = calls++;
978
+ const isSeam = request.name === seam.request && seamMatches++ === (seam.occurrence ?? 0);
979
+ const ledger = (source) => {
980
+ callLog.push({
981
+ ...request.name !== void 0 ? { name: request.name } : {},
982
+ model: request.model,
983
+ key: queueKeyOf(request),
984
+ source,
985
+ seam: isSeam
986
+ });
987
+ };
988
+ if (isSeam && candidate) {
989
+ await takeScriptedSlot(request, info);
990
+ seamReached = true;
991
+ callsBeforeSeam = callIndex;
992
+ seamStateAt = statePath.length;
993
+ seamEventAt = liveEvents;
994
+ const result = await candidate(request, info);
995
+ seamOutput = await seamOutputOf(result, request);
996
+ seamUsage = getCallUsage(result);
997
+ ledger("candidate");
998
+ return result;
999
+ }
1000
+ const scripted = await scriptedAnswer(request, info);
1001
+ ledger("script");
1002
+ if (!isSeam) return scripted;
1003
+ seamReached = true;
1004
+ callsBeforeSeam = callIndex;
1005
+ seamStateAt = statePath.length;
1006
+ seamEventAt = liveEvents;
1007
+ seamOutput = scripted.result;
1008
+ seamUsage = scripted.usage;
1009
+ return scripted;
1010
+ };
1011
+ const executors = {
1012
+ ...options.executors,
1013
+ generateText: route,
1014
+ streamText: async (request, info) => {
1015
+ const result = await route(request, info);
1016
+ emitScriptedChunk(result, info);
1017
+ return result;
1018
+ }
1019
+ };
1020
+ let snapshot;
1021
+ let event;
1022
+ const events = [];
1023
+ let result;
1024
+ const maxTurns = options.maxTurns ?? 12;
1025
+ for (let turn = 0; turn <= maxTurns; turn++) {
1026
+ result = await runAgent(machine, {
1027
+ ...snapshot ? { snapshot } : { input: options.input },
1028
+ ...event ? { event } : {},
1029
+ ...options.actors ? { actors: options.actors } : {},
1030
+ executors,
1031
+ onTransition: (next, causedBy) => {
1032
+ if (turn > 0 && causedBy.type === "@xstate.init") return;
1033
+ events.push(causedBy);
1034
+ liveEvents = events.length;
1035
+ statePath.push(next.value);
1036
+ }
1037
+ });
1038
+ if (result.status !== "idle" || turn === maxTurns) break;
1039
+ const next = options.respond?.({
1040
+ snapshot: result.snapshot,
1041
+ state: result.snapshot.value,
1042
+ meta: getStateMeta(result.snapshot),
1043
+ turn,
1044
+ result
1045
+ });
1046
+ if (!next) break;
1047
+ event = next;
1048
+ snapshot = result.persist();
1049
+ }
1050
+ let splitAt = events.length;
1051
+ if (seamReached) {
1052
+ splitAt = seamEventAt;
1053
+ for (let index = seamEventAt; index < events.length; index++) {
1054
+ const type = events[index].type;
1055
+ if (type.startsWith("xstate.done") || type.startsWith("xstate.error")) {
1056
+ splitAt = index;
1057
+ break;
1058
+ }
1059
+ }
1060
+ }
1061
+ const splitStateAt = seamReached ? seamStateAt : statePath.length;
1062
+ return {
1063
+ result,
1064
+ seamOutput,
1065
+ ...seamUsage !== void 0 ? { seamUsage } : {},
1066
+ callsBeforeSeam,
1067
+ calls: callLog,
1068
+ before: {
1069
+ statePath: statePath.slice(0, splitStateAt),
1070
+ events: events.slice(0, splitAt)
1071
+ },
1072
+ after: {
1073
+ statePath: statePath.slice(splitStateAt),
1074
+ events: events.slice(splitAt)
1075
+ }
1076
+ };
1077
+ }
1078
+ //#endregion
1079
+ export { AgentLintError, AgentUnknownStateError, assertAgentMachine, canReach, explorePaths, lintAgentMachine, matchesTrajectory, runSeam, simulateAgent };