@statelyai/agent 2.0.0-alpha.33 → 2.0.0-alpha.35

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 (64) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/dist/ai-sdk.cjs +114 -63
  3. package/dist/ai-sdk.d.cts +11 -2
  4. package/dist/ai-sdk.d.mts +11 -2
  5. package/dist/ai-sdk.mjs +115 -64
  6. package/dist/{decision-BPdEaxPf.cjs → decision-BEsATcr1.cjs} +18 -2
  7. package/dist/{decision-CV7WiV5C.mjs → decision-COkJNjOW.mjs} +18 -2
  8. package/dist/{event-log-CKWeC7H-.cjs → event-log-BWSSLUff.cjs} +139 -10
  9. package/dist/{event-log-DAqsalT1.mjs → event-log-C7jeH0y8.mjs} +123 -12
  10. package/dist/{event-log-store-C2NjVlWU.d.mts → event-log-store-BzXehP7R.d.cts} +28 -3
  11. package/dist/{event-log-store-s73r1Hxf.d.cts → event-log-store-CWjN6lmM.d.mts} +28 -3
  12. package/dist/{event-log-store-DKyE7JKr.cjs → event-log-store-CleWpsp5.cjs} +1 -1
  13. package/dist/{event-log-store-BGehvVTb.mjs → event-log-store-W49TCJXi.mjs} +1 -1
  14. package/dist/index.cjs +5 -5
  15. package/dist/index.d.cts +7 -7
  16. package/dist/index.d.mts +7 -7
  17. package/dist/index.mjs +5 -5
  18. package/dist/log.cjs +2 -2
  19. package/dist/log.d.cts +1 -1
  20. package/dist/log.d.mts +1 -1
  21. package/dist/log.mjs +2 -2
  22. package/dist/machines.cjs +1 -1
  23. package/dist/machines.mjs +1 -1
  24. package/dist/{model-source-BilDEyqk.mjs → model-source-CK3F4YuU.mjs} +1 -1
  25. package/dist/{model-source-BQ97_l1-.cjs → model-source-DmhTMK39.cjs} +1 -1
  26. package/dist/openai.cjs +377 -95
  27. package/dist/openai.d.cts +55 -20
  28. package/dist/openai.d.mts +55 -20
  29. package/dist/openai.mjs +375 -96
  30. package/dist/otel.d.cts +1 -1
  31. package/dist/otel.d.mts +1 -1
  32. package/dist/{run-agent-B5QhabdY.d.cts → run-agent-B1-Eqviq.d.cts} +13 -5
  33. package/dist/{run-agent-Atu-wEk7.d.mts → run-agent-BGdx5913.d.mts} +13 -5
  34. package/dist/{run-agent-2UnSUhDb.mjs → run-agent-Ct_GZw_f.mjs} +124 -38
  35. package/dist/{run-agent-Cg0xd3K-.cjs → run-agent-s5D32w99.cjs} +123 -37
  36. package/dist/{setup-agent-CmhB-U5P.mjs → setup-agent-05uw4dPZ.mjs} +2 -2
  37. package/dist/{setup-agent-BiF1N3td.d.cts → setup-agent-BtfeFfF6.d.cts} +2 -2
  38. package/dist/{setup-agent-J9f_Wu5T.d.mts → setup-agent-C6nqzv_X.d.mts} +2 -2
  39. package/dist/{setup-agent-CwUJyqKB.cjs → setup-agent-CXEBURdo.cjs} +2 -2
  40. package/dist/tanstack-ai.cjs +91 -59
  41. package/dist/tanstack-ai.d.cts +13 -2
  42. package/dist/tanstack-ai.d.mts +13 -2
  43. package/dist/tanstack-ai.mjs +91 -59
  44. package/dist/testing.cjs +629 -13
  45. package/dist/testing.d.cts +278 -6
  46. package/dist/testing.d.mts +278 -6
  47. package/dist/testing.mjs +626 -14
  48. package/dist/{text-logic-CYP502VT.d.cts → text-logic-CV_4KGr6.d.cts} +28 -3
  49. package/dist/{text-logic-DuK9HIFM.d.mts → text-logic-Ckh-rJFV.d.mts} +28 -3
  50. package/dist/{usage-D7JKAxLc.d.mts → usage-BDiJKyv9.d.cts} +11 -1
  51. package/dist/{usage-DVw-646x.d.cts → usage-DOoVUGVq.d.mts} +11 -1
  52. package/dist/validate.cjs +3 -3
  53. package/dist/validate.d.cts +2 -2
  54. package/dist/validate.d.mts +2 -2
  55. package/dist/validate.mjs +3 -3
  56. package/dist/{verify-Dk3Vwnha.d.cts → verify-BQpE8hUz.d.cts} +1 -1
  57. package/dist/{verify-DgB8P-Wx.mjs → verify-Bpahmr34.mjs} +2 -2
  58. package/dist/{verify-BVz4cURF.d.mts → verify-CqeuEmIk.d.mts} +1 -1
  59. package/dist/{verify-CS_WLRXn.cjs → verify-DCLZa9R0.cjs} +2 -2
  60. package/dist/with-model-calls-BXYdBdcu.cjs +45 -0
  61. package/dist/with-model-calls-Bv4V4EHC.mjs +34 -0
  62. package/package.json +5 -5
  63. package/readme.md +14 -13
  64. package/skills/loop-to-machine/SKILL.md +42 -2
@@ -1,7 +1,7 @@
1
- import { P as AgentExecutorDecisionRequest, c as AgentRequestExecutorInfo, f as AgentTextRequest, s as AgentRequestExecutor, t as AgentCallUsage, u as AgentRequestExecutors } from "./text-logic-CYP502VT.cjs";
2
- import { f as AgentRuntimeOptions, l as AgentRunResult, o as AgentRunInit } from "./run-agent-B5QhabdY.cjs";
1
+ import { F as AgentExecutorDecisionRequest, c as AgentRequestExecutorInfo, f as AgentTextRequest, s as AgentRequestExecutor, t as AgentCallUsage, u as AgentRequestExecutors } from "./text-logic-CV_4KGr6.cjs";
2
+ import { f as AgentRuntimeOptions, l as AgentRunResult, o as AgentRunInit } from "./run-agent-B1-Eqviq.cjs";
3
3
  import { t as MetaOfSnapshot } from "./utils-ebVpCYoF.cjs";
4
- import { _ as assertAgentMachine, a as AgentPathTerminal, b as lintAgentMachine, c as CanReachResult, d as ExploreSnapshot, f as LintAgentMachineOptions, g as SimulationTrailEntry, h as SimulationScript, i as AgentPathReport, l as CanReachTarget, m as SimulateAgentResult, n as AgentLintError, o as AgentUnknownStateError, p as SimulateAgentOptions, r as AgentLintSeverity, s as AssertAgentMachineOptions, t as AgentLintDiagnostic, u as ExplorePathsOptions, v as canReach, x as simulateAgent, y as explorePaths } from "./verify-Dk3Vwnha.cjs";
4
+ import { _ as assertAgentMachine, a as AgentPathTerminal, b as lintAgentMachine, c as CanReachResult, d as ExploreSnapshot, f as LintAgentMachineOptions, g as SimulationTrailEntry, h as SimulationScript, i as AgentPathReport, l as CanReachTarget, m as SimulateAgentResult, n as AgentLintError, o as AgentUnknownStateError, p as SimulateAgentOptions, r as AgentLintSeverity, s as AssertAgentMachineOptions, t as AgentLintDiagnostic, u as ExplorePathsOptions, v as canReach, x as simulateAgent, y as explorePaths } from "./verify-BQpE8hUz.cjs";
5
5
  import { AnyStateMachine, EventFromLogic, InputFrom, SnapshotFrom, StateValue } from "xstate";
6
6
  //#region src/trajectory.d.ts
7
7
  /** An event-shaped trajectory item: anything with a string `type`. */
@@ -418,7 +418,8 @@ interface TraceCoverageOptions {
418
418
  * bare type (`xstate.done.actor`), one per state however many invokes; a
419
419
  * definition's `matches` (an invoke's `actorId`) must accept the traced event
420
420
  * for its state to be credited. Ignored steps credit no handler. `always`
421
- * handlers are listed as `unobservable`, never as unvisited.
421
+ * handlers are listed as `unobservable`, never as unvisited; a state one
422
+ * passes through is visited, re-derived from each step's microsteps.
422
423
  *
423
424
  * @example
424
425
  * ```ts no-check
@@ -459,8 +460,14 @@ interface FirstDivergenceOptions {
459
460
  * `from|event|to`. Override to leave out what may differ between equivalent
460
461
  * runs, such as a parallel region that only logs. Steps are still compared
461
462
  * in order. Trace references only.
463
+ *
464
+ * `"payload"` also compares the event's payload, such as an actor's output,
465
+ * as key-order-independent JSON without its `sessionId`, and with errors as
466
+ * `{ name, message, code?, cause? }` (an `Error`, or a serialized one with `stack` or
467
+ * `code`). Use it for deterministic or mocked runs; live model outputs usually differ
468
+ * between runs.
462
469
  */
463
- key?: (step: TraceStep) => string;
470
+ key?: ((step: TraceStep) => string) | "payload";
464
471
  }
465
472
  /** Where a run first departs from its reference. See {@link firstDivergence}. */
466
473
  interface Divergence {
@@ -822,4 +829,269 @@ export declare function compareRuns<TScenario extends {
822
829
  expected?: string;
823
830
  }>(options: CompareRunsOptions<TScenario>): Promise<ParityReport<TScenario>>;
824
831
  //#endregion
825
- export { type AgentLintDiagnostic, AgentLintError, type AgentLintSeverity, type AgentPathReport, type AgentPathTerminal, AgentUnknownStateError, type AgreementReport, type AgreementRow, type AgreementSummary, type AssertAgentMachineOptions, type CanReachResult, type CanReachTarget, type CompareRunsOptions, type CoverageReport, type Divergence, type ExplorePathsOptions, type ExploreSnapshot, type FirstDivergenceOptions, type FoldTracesOptions, type GetDecisionRequestsOptions, type HandlerCoverage, type HandlerRef, type JudgeAgreementOptions, type LintAgentMachineOptions, type MatchTrajectoryOptions, type ParityLabel, type ParityReport, type ParityResult, type ParityRun, type RepeatGroup, type RepeatReport, type RepeatResult, type RepeatRun, type RepeatRunsOptions, type RunSeamOptions, type RunSeamResult, type SeamCall, type SeamRef, type SeamScriptEntry, type SeamSlice, type SeamTurn, type SimulateAgentOptions, type SimulateAgentResult, type SimulationScript, type SimulationTrailEntry, type StateCoverage, type StateStats, type TraceCoverageOptions, type TraceStep, type TracedDecisionRequest, type TrajectoryEvent, type TrajectoryItem, type TrajectoryMatch, type TrajectoryMiss, assertAgentMachine, canReach, explorePaths, lintAgentMachine, simulateAgent };
832
+ //#region src/equivalence.d.ts
833
+ /**
834
+ * Verified equivalence — prove a port behaves like the code it replaces.
835
+ *
836
+ * A port is equivalent when, given the same model answers and the same tool
837
+ * results, it produces the same OBSERVABLE EFFECTS: the same number of model
838
+ * calls, the same tool calls with the same arguments, and the same output.
839
+ * Prompts, internal steps, and states are free to differ.
840
+ *
841
+ * 1. Record the original: wrap its model (`recorder.model`, an AI SDK
842
+ * `LanguageModelV3`, or `recorder.openai`, an `openai` client) and its tools
843
+ * (`recorder.tool` / `recorder.tools`), run a scenario, call
844
+ * `recorder.output(result)`, and save `recorder.recording`.
845
+ * 2. Replay it on the port: `createRecorder({ replay: recording })` answers
846
+ * every model call and tool call from the recording, with no API key and no
847
+ * side effects, and records what the port did.
848
+ * 3. `compareRecordings(original, port)` reports the first effect that differs.
849
+ *
850
+ * A port that changes how it calls the model on purpose (a model-picked action
851
+ * that became `agent.decide`, a stream that became `doGenerate`) still replays:
852
+ * `translate` turns the recorded answer into one that fits, `fallback` answers
853
+ * the calls the original never made, and `compareRecordings(…, { kinds })`
854
+ * compares only tool calls and output.
855
+ *
856
+ * Provider-free: both seams are wrapped structurally, so this module imports
857
+ * neither `ai` nor `openai`.
858
+ * @module
859
+ */
860
+ /** An error as recorded: JSON-safe. */
861
+ interface RecordedError {
862
+ name: string;
863
+ message: string;
864
+ code?: unknown;
865
+ /** The error's own number and boolean fields, such as `statusCode` and
866
+ * `isRetryable`; replay restores them. */
867
+ data?: Record<string, number | boolean>;
868
+ /** `Symbol.for` keys set to `true` on the error, such as the AI SDK's
869
+ * `vercel.ai.error.AI_APICallError`; replay restores them, so checks like
870
+ * `APICallError.isInstance` still pass. */
871
+ markers?: string[];
872
+ }
873
+ /** One model call. `call` is its 0-based position among model calls. */
874
+ interface RecordedModelCall {
875
+ kind: "model";
876
+ /** Which wrapper saw it. */
877
+ seam: "ai-sdk" | "openai";
878
+ /** `doGenerate`/`doStream`/`doDecide`, or `chat.completions.create`/`responses.create`. */
879
+ method: string;
880
+ call: number;
881
+ /** The call options or params, JSON-safe (abort signals and headers dropped). */
882
+ request: unknown;
883
+ /** The non-streaming result. */
884
+ response?: unknown;
885
+ /** The stream's parts or chunks, in order. */
886
+ stream?: unknown[];
887
+ error?: RecordedError;
888
+ /** In replay, the recorded call (its `call`) whose answer served this one. */
889
+ replayed?: number;
890
+ /** In replay, `translate` adapted the recorded answer to this call. */
891
+ translated?: true;
892
+ /** In replay, no recorded call fit, so `fallback` answered it. */
893
+ unrecorded?: true;
894
+ }
895
+ /** One tool call. `step` is the number of model calls made before it. */
896
+ interface RecordedToolCall {
897
+ kind: "tool";
898
+ name: string;
899
+ step: number;
900
+ input: unknown;
901
+ output?: unknown;
902
+ error?: RecordedError;
903
+ /** The tool returned without a Promise; replay answers synchronously too. */
904
+ sync?: true;
905
+ /** Started while an earlier tool call was still running. Concurrent calls
906
+ * may finish in either order; sequential ones must keep theirs. */
907
+ concurrent?: true;
908
+ /** In replay, no recorded call matched, so `fallback.tools` ran the tool. */
909
+ unrecorded?: true;
910
+ }
911
+ /** The run's final result. */
912
+ interface RecordedOutput {
913
+ kind: "output";
914
+ value: unknown;
915
+ }
916
+ type RecordedEffect = RecordedModelCall | RecordedToolCall | RecordedOutput;
917
+ /** A run's effects, in order. JSON-serializable: save it as a golden file. */
918
+ interface Recording {
919
+ version: 1;
920
+ effects: RecordedEffect[];
921
+ }
922
+ /** Options for {@link createRecorder}. Omit for record mode. */
923
+ interface CreateRecorderOptions {
924
+ /** Answer model and tool calls from this recording instead of running them. */
925
+ replay?: Recording;
926
+ /**
927
+ * In replay, `"replay"` (default) returns each tool's recorded result
928
+ * without running it; `"live"` runs the wrapped tool (your stub) and records
929
+ * what it returned.
930
+ */
931
+ tools?: "replay" | "live";
932
+ /**
933
+ * In replay, which model calls a recorded answer may serve. `"shape"`
934
+ * (default) refuses a call whose offered tools or response format changed,
935
+ * since the recorded answer may not fit it; see {@link ModelCallMatch}.
936
+ */
937
+ modelCalls?: ModelCallMatch;
938
+ /**
939
+ * In replay, maps the recorded and the new tool call before matching them,
940
+ * like {@link CompareRecordingsOptions.normalize}: redact a generated id in a
941
+ * tool's input so the recorded result still answers it. A dropped
942
+ * (`undefined`) effect matches as recorded.
943
+ */
944
+ normalize?: NormalizeEffect;
945
+ /**
946
+ * In replay, adapts a recorded answer to a model call you changed on
947
+ * purpose. Called with each unused recorded call, in order, until it
948
+ * returns an answer; that answer serves the call regardless of method and
949
+ * shape, and {@link compareRecordings} counts the pair as the same call.
950
+ * Return `undefined` to leave a recorded call to the usual matching.
951
+ *
952
+ * @example
953
+ * ```ts
954
+ * // The original streamed; the port generates.
955
+ * translate: (recorded, actual) =>
956
+ * actual.method === "doGenerate" && recorded.method === "doStream"
957
+ * ? { response: toGenerateResult(recorded.stream) }
958
+ * : undefined,
959
+ * ```
960
+ */
961
+ translate?: (recorded: RecordedModelCall, actual: RecordedModelCall) => ModelAnswer | undefined;
962
+ /**
963
+ * In replay, answers what the recording cannot, instead of throwing
964
+ * {@link RecordingDivergenceError}: a model call no recorded call fits (the
965
+ * port's retry or an added call) goes to `model` (AI SDK seam) or `openai`
966
+ * (client seam), and with `tools: true` a tool call no recorded call matches
967
+ * runs the wrapped tool. Such effects are marked `unrecorded` and still
968
+ * count as extra in {@link compareRecordings}.
969
+ */
970
+ fallback?: {
971
+ model?: RecordableModel;
972
+ openai?: object;
973
+ tools?: boolean;
974
+ };
975
+ }
976
+ /** A model call's answer: the result, the stream's parts, or an error. */
977
+ interface ModelAnswer {
978
+ response?: unknown;
979
+ stream?: unknown[];
980
+ error?: RecordedError;
981
+ }
982
+ /**
983
+ * Maps an effect before matching or comparing; `undefined` drops it.
984
+ * `index` and `effects` are its position in the whole recording, so a repeat
985
+ * of an earlier effect (an idempotent write made again) can be dropped.
986
+ */
987
+ type NormalizeEffect = (effect: RecordedEffect, index: number, effects: readonly RecordedEffect[]) => RecordedEffect | undefined;
988
+ /**
989
+ * When two model calls count as the same: `"shape"` compares offered tool
990
+ * names and response format, `"none"` only that a call happened, and a
991
+ * function decides per call (allow the changes you made on purpose and fall
992
+ * back to {@link sameModelCallShape}).
993
+ */
994
+ type ModelCallMatch = "shape" | "none" | ((expected: RecordedModelCall, actual: RecordedModelCall) => boolean);
995
+ /** The structural slice of an AI SDK `LanguageModelV3` the recorder wraps. */
996
+ interface RecordableLanguageModel {
997
+ doGenerate(options: never): PromiseLike<unknown>;
998
+ doStream(options: never): PromiseLike<{
999
+ stream: ReadableStream<unknown>;
1000
+ }>;
1001
+ }
1002
+ /** The structural slice of an AI SDK decision model (`experimental_decide`). */
1003
+ interface RecordableDecisionModel {
1004
+ doDecide(options: never): PromiseLike<unknown>;
1005
+ }
1006
+ /** An AI SDK model the recorder can wrap. */
1007
+ type RecordableModel = RecordableLanguageModel | RecordableDecisionModel;
1008
+ type AnyFunction = (...args: never[]) => unknown;
1009
+ /** Records (or replays) the effects of one run. */
1010
+ interface Recorder {
1011
+ /**
1012
+ * Wraps an AI SDK `LanguageModelV3` (`openai("gpt-…")`, `anthropic(…)`) or
1013
+ * decision model (`doDecide`), keeping its type. In replay the model is
1014
+ * never called, so a provider model built with no API key works.
1015
+ */
1016
+ model<TModel extends RecordableModel>(model: TModel): TModel;
1017
+ /**
1018
+ * Wraps an `openai` client's `chat.completions.create` and
1019
+ * `responses.create`, keeping its type. A streamed result is an async
1020
+ * iterable only. In replay the client is never called, so
1021
+ * `new OpenAI({ apiKey: "replay" })` works.
1022
+ */
1023
+ openai<TClient extends object>(client: TClient): TClient;
1024
+ /** Wraps one tool function. Its arguments are the recorded input. */
1025
+ tool<TFn extends AnyFunction>(name: string, fn: TFn): TFn;
1026
+ /** Wraps every `execute` in an AI SDK tool set, named by key. */
1027
+ tools<TTools extends Record<string, {
1028
+ execute?: AnyFunction;
1029
+ }>>(tools: TTools): TTools;
1030
+ /** Records the run's final result. */
1031
+ output(value: unknown): void;
1032
+ /** What this run did so far. */
1033
+ readonly recording: Recording;
1034
+ }
1035
+ /**
1036
+ * Thrown in replay when the port asks for something the recording cannot
1037
+ * answer: a model call past the last recorded one, or a tool call the original
1038
+ * never made. The effect is still recorded, so {@link compareRecordings}
1039
+ * reports it.
1040
+ */
1041
+ export declare class RecordingDivergenceError extends Error {
1042
+ name: string;
1043
+ }
1044
+ /**
1045
+ * Creates a {@link Recorder}. With no options it records: every wrapped call
1046
+ * runs for real. With `replay` it answers from a recording instead.
1047
+ */
1048
+ export declare function createRecorder(options?: CreateRecorderOptions): Recorder;
1049
+ /** Why two effects differ. */
1050
+ type EffectDivergenceReason = "missing-effect" | "extra-effect" | "kind" | "model-method" | "model-call-shape" | "model-error" | "tool-name" | "tool-input" | "tool-result" | "output";
1051
+ /** The first effect where two recordings differ. */
1052
+ interface EffectDivergence {
1053
+ /** Position in the compared (tool-order-normalized) effect lists. */
1054
+ index: number;
1055
+ reason: EffectDivergenceReason;
1056
+ expected?: RecordedEffect;
1057
+ actual?: RecordedEffect;
1058
+ /** One sentence, for test failures. */
1059
+ message: string;
1060
+ }
1061
+ /** The result of {@link compareRecordings}. */
1062
+ interface EquivalenceReport {
1063
+ equivalent: boolean;
1064
+ /** The first divergence, by position in the original's effects. */
1065
+ divergence?: EffectDivergence;
1066
+ /** Every divergence, in order. Effects both runs made line up between them. */
1067
+ divergences: EffectDivergence[];
1068
+ }
1069
+ /** Options for {@link compareRecordings}. */
1070
+ interface CompareRecordingsOptions {
1071
+ /** When two model calls count as the same. Default `"shape"`. */
1072
+ modelCalls?: ModelCallMatch;
1073
+ /**
1074
+ * Maps each effect before comparing, on both sides: redact values that
1075
+ * differ on every run (generated ids, timestamps), project a tool's input
1076
+ * to the fields that matter, or return `undefined` to drop the effect.
1077
+ */
1078
+ normalize?: NormalizeEffect;
1079
+ /**
1080
+ * Which kinds of effect to compare. Default all. `["tool", "output"]`
1081
+ * compares a port that calls the model differently (another protocol, a
1082
+ * structured draft instead of a tool loop) by what it did, not how it asked.
1083
+ */
1084
+ kinds?: readonly RecordedEffect["kind"][];
1085
+ }
1086
+ /** True when two model calls offer the same tool names and response format. */
1087
+ export declare function sameModelCallShape(expected: RecordedModelCall, actual: RecordedModelCall): boolean;
1088
+ /**
1089
+ * Compares two recordings and reports every effect that differs. `expected`
1090
+ * is the original's recording, `actual` the port's. Effects that are the same
1091
+ * on both sides are lined up first (longest common subsequence), so one extra
1092
+ * call does not shift every effect after it; between them, effects of the same
1093
+ * kind pair up and report what changed, and the rest are missing or extra.
1094
+ */
1095
+ export declare function compareRecordings(expected: Recording, actual: Recording, options?: CompareRecordingsOptions): EquivalenceReport;
1096
+ //#endregion
1097
+ export { type AgentLintDiagnostic, AgentLintError, type AgentLintSeverity, type AgentPathReport, type AgentPathTerminal, AgentUnknownStateError, type AgreementReport, type AgreementRow, type AgreementSummary, type AssertAgentMachineOptions, type CanReachResult, type CanReachTarget, type CompareRecordingsOptions, type CompareRunsOptions, type CoverageReport, type CreateRecorderOptions, type Divergence, type EffectDivergence, type EffectDivergenceReason, type EquivalenceReport, type ExplorePathsOptions, type ExploreSnapshot, type FirstDivergenceOptions, type FoldTracesOptions, type GetDecisionRequestsOptions, type HandlerCoverage, type HandlerRef, type JudgeAgreementOptions, type LintAgentMachineOptions, type MatchTrajectoryOptions, type ModelAnswer, type ModelCallMatch, type NormalizeEffect, type ParityLabel, type ParityReport, type ParityResult, type ParityRun, type RecordableDecisionModel, type RecordableLanguageModel, type RecordableModel, type RecordedEffect, type RecordedError, type RecordedModelCall, type RecordedOutput, type RecordedToolCall, type Recorder, type Recording, type RepeatGroup, type RepeatReport, type RepeatResult, type RepeatRun, type RepeatRunsOptions, type RunSeamOptions, type RunSeamResult, type SeamCall, type SeamRef, type SeamScriptEntry, type SeamSlice, type SeamTurn, type SimulateAgentOptions, type SimulateAgentResult, type SimulationScript, type SimulationTrailEntry, type StateCoverage, type StateStats, type TraceCoverageOptions, type TraceStep, type TracedDecisionRequest, type TrajectoryEvent, type TrajectoryItem, type TrajectoryMatch, type TrajectoryMiss, assertAgentMachine, canReach, explorePaths, lintAgentMachine, simulateAgent };
@@ -1,7 +1,7 @@
1
- import { P as AgentExecutorDecisionRequest, c as AgentRequestExecutorInfo, f as AgentTextRequest, s as AgentRequestExecutor, t as AgentCallUsage, u as AgentRequestExecutors } from "./text-logic-DuK9HIFM.mjs";
2
- import { f as AgentRuntimeOptions, l as AgentRunResult, o as AgentRunInit } from "./run-agent-Atu-wEk7.mjs";
1
+ import { F as AgentExecutorDecisionRequest, c as AgentRequestExecutorInfo, f as AgentTextRequest, s as AgentRequestExecutor, t as AgentCallUsage, u as AgentRequestExecutors } from "./text-logic-Ckh-rJFV.mjs";
2
+ import { f as AgentRuntimeOptions, l as AgentRunResult, o as AgentRunInit } from "./run-agent-BGdx5913.mjs";
3
3
  import { t as MetaOfSnapshot } from "./utils-BnXdWih3.mjs";
4
- import { _ as assertAgentMachine, a as AgentPathTerminal, b as lintAgentMachine, c as CanReachResult, d as ExploreSnapshot, f as LintAgentMachineOptions, g as SimulationTrailEntry, h as SimulationScript, i as AgentPathReport, l as CanReachTarget, m as SimulateAgentResult, n as AgentLintError, o as AgentUnknownStateError, p as SimulateAgentOptions, r as AgentLintSeverity, s as AssertAgentMachineOptions, t as AgentLintDiagnostic, u as ExplorePathsOptions, v as canReach, x as simulateAgent, y as explorePaths } from "./verify-BVz4cURF.mjs";
4
+ import { _ as assertAgentMachine, a as AgentPathTerminal, b as lintAgentMachine, c as CanReachResult, d as ExploreSnapshot, f as LintAgentMachineOptions, g as SimulationTrailEntry, h as SimulationScript, i as AgentPathReport, l as CanReachTarget, m as SimulateAgentResult, n as AgentLintError, o as AgentUnknownStateError, p as SimulateAgentOptions, r as AgentLintSeverity, s as AssertAgentMachineOptions, t as AgentLintDiagnostic, u as ExplorePathsOptions, v as canReach, x as simulateAgent, y as explorePaths } from "./verify-CqeuEmIk.mjs";
5
5
  import { AnyStateMachine, EventFromLogic, InputFrom, SnapshotFrom, StateValue } from "xstate";
6
6
  //#region src/trajectory.d.ts
7
7
  /** An event-shaped trajectory item: anything with a string `type`. */
@@ -418,7 +418,8 @@ interface TraceCoverageOptions {
418
418
  * bare type (`xstate.done.actor`), one per state however many invokes; a
419
419
  * definition's `matches` (an invoke's `actorId`) must accept the traced event
420
420
  * for its state to be credited. Ignored steps credit no handler. `always`
421
- * handlers are listed as `unobservable`, never as unvisited.
421
+ * handlers are listed as `unobservable`, never as unvisited; a state one
422
+ * passes through is visited, re-derived from each step's microsteps.
422
423
  *
423
424
  * @example
424
425
  * ```ts no-check
@@ -459,8 +460,14 @@ interface FirstDivergenceOptions {
459
460
  * `from|event|to`. Override to leave out what may differ between equivalent
460
461
  * runs, such as a parallel region that only logs. Steps are still compared
461
462
  * in order. Trace references only.
463
+ *
464
+ * `"payload"` also compares the event's payload, such as an actor's output,
465
+ * as key-order-independent JSON without its `sessionId`, and with errors as
466
+ * `{ name, message, code?, cause? }` (an `Error`, or a serialized one with `stack` or
467
+ * `code`). Use it for deterministic or mocked runs; live model outputs usually differ
468
+ * between runs.
462
469
  */
463
- key?: (step: TraceStep) => string;
470
+ key?: ((step: TraceStep) => string) | "payload";
464
471
  }
465
472
  /** Where a run first departs from its reference. See {@link firstDivergence}. */
466
473
  interface Divergence {
@@ -822,4 +829,269 @@ export declare function compareRuns<TScenario extends {
822
829
  expected?: string;
823
830
  }>(options: CompareRunsOptions<TScenario>): Promise<ParityReport<TScenario>>;
824
831
  //#endregion
825
- export { type AgentLintDiagnostic, AgentLintError, type AgentLintSeverity, type AgentPathReport, type AgentPathTerminal, AgentUnknownStateError, type AgreementReport, type AgreementRow, type AgreementSummary, type AssertAgentMachineOptions, type CanReachResult, type CanReachTarget, type CompareRunsOptions, type CoverageReport, type Divergence, type ExplorePathsOptions, type ExploreSnapshot, type FirstDivergenceOptions, type FoldTracesOptions, type GetDecisionRequestsOptions, type HandlerCoverage, type HandlerRef, type JudgeAgreementOptions, type LintAgentMachineOptions, type MatchTrajectoryOptions, type ParityLabel, type ParityReport, type ParityResult, type ParityRun, type RepeatGroup, type RepeatReport, type RepeatResult, type RepeatRun, type RepeatRunsOptions, type RunSeamOptions, type RunSeamResult, type SeamCall, type SeamRef, type SeamScriptEntry, type SeamSlice, type SeamTurn, type SimulateAgentOptions, type SimulateAgentResult, type SimulationScript, type SimulationTrailEntry, type StateCoverage, type StateStats, type TraceCoverageOptions, type TraceStep, type TracedDecisionRequest, type TrajectoryEvent, type TrajectoryItem, type TrajectoryMatch, type TrajectoryMiss, assertAgentMachine, canReach, explorePaths, lintAgentMachine, simulateAgent };
832
+ //#region src/equivalence.d.ts
833
+ /**
834
+ * Verified equivalence — prove a port behaves like the code it replaces.
835
+ *
836
+ * A port is equivalent when, given the same model answers and the same tool
837
+ * results, it produces the same OBSERVABLE EFFECTS: the same number of model
838
+ * calls, the same tool calls with the same arguments, and the same output.
839
+ * Prompts, internal steps, and states are free to differ.
840
+ *
841
+ * 1. Record the original: wrap its model (`recorder.model`, an AI SDK
842
+ * `LanguageModelV3`, or `recorder.openai`, an `openai` client) and its tools
843
+ * (`recorder.tool` / `recorder.tools`), run a scenario, call
844
+ * `recorder.output(result)`, and save `recorder.recording`.
845
+ * 2. Replay it on the port: `createRecorder({ replay: recording })` answers
846
+ * every model call and tool call from the recording, with no API key and no
847
+ * side effects, and records what the port did.
848
+ * 3. `compareRecordings(original, port)` reports the first effect that differs.
849
+ *
850
+ * A port that changes how it calls the model on purpose (a model-picked action
851
+ * that became `agent.decide`, a stream that became `doGenerate`) still replays:
852
+ * `translate` turns the recorded answer into one that fits, `fallback` answers
853
+ * the calls the original never made, and `compareRecordings(…, { kinds })`
854
+ * compares only tool calls and output.
855
+ *
856
+ * Provider-free: both seams are wrapped structurally, so this module imports
857
+ * neither `ai` nor `openai`.
858
+ * @module
859
+ */
860
+ /** An error as recorded: JSON-safe. */
861
+ interface RecordedError {
862
+ name: string;
863
+ message: string;
864
+ code?: unknown;
865
+ /** The error's own number and boolean fields, such as `statusCode` and
866
+ * `isRetryable`; replay restores them. */
867
+ data?: Record<string, number | boolean>;
868
+ /** `Symbol.for` keys set to `true` on the error, such as the AI SDK's
869
+ * `vercel.ai.error.AI_APICallError`; replay restores them, so checks like
870
+ * `APICallError.isInstance` still pass. */
871
+ markers?: string[];
872
+ }
873
+ /** One model call. `call` is its 0-based position among model calls. */
874
+ interface RecordedModelCall {
875
+ kind: "model";
876
+ /** Which wrapper saw it. */
877
+ seam: "ai-sdk" | "openai";
878
+ /** `doGenerate`/`doStream`/`doDecide`, or `chat.completions.create`/`responses.create`. */
879
+ method: string;
880
+ call: number;
881
+ /** The call options or params, JSON-safe (abort signals and headers dropped). */
882
+ request: unknown;
883
+ /** The non-streaming result. */
884
+ response?: unknown;
885
+ /** The stream's parts or chunks, in order. */
886
+ stream?: unknown[];
887
+ error?: RecordedError;
888
+ /** In replay, the recorded call (its `call`) whose answer served this one. */
889
+ replayed?: number;
890
+ /** In replay, `translate` adapted the recorded answer to this call. */
891
+ translated?: true;
892
+ /** In replay, no recorded call fit, so `fallback` answered it. */
893
+ unrecorded?: true;
894
+ }
895
+ /** One tool call. `step` is the number of model calls made before it. */
896
+ interface RecordedToolCall {
897
+ kind: "tool";
898
+ name: string;
899
+ step: number;
900
+ input: unknown;
901
+ output?: unknown;
902
+ error?: RecordedError;
903
+ /** The tool returned without a Promise; replay answers synchronously too. */
904
+ sync?: true;
905
+ /** Started while an earlier tool call was still running. Concurrent calls
906
+ * may finish in either order; sequential ones must keep theirs. */
907
+ concurrent?: true;
908
+ /** In replay, no recorded call matched, so `fallback.tools` ran the tool. */
909
+ unrecorded?: true;
910
+ }
911
+ /** The run's final result. */
912
+ interface RecordedOutput {
913
+ kind: "output";
914
+ value: unknown;
915
+ }
916
+ type RecordedEffect = RecordedModelCall | RecordedToolCall | RecordedOutput;
917
+ /** A run's effects, in order. JSON-serializable: save it as a golden file. */
918
+ interface Recording {
919
+ version: 1;
920
+ effects: RecordedEffect[];
921
+ }
922
+ /** Options for {@link createRecorder}. Omit for record mode. */
923
+ interface CreateRecorderOptions {
924
+ /** Answer model and tool calls from this recording instead of running them. */
925
+ replay?: Recording;
926
+ /**
927
+ * In replay, `"replay"` (default) returns each tool's recorded result
928
+ * without running it; `"live"` runs the wrapped tool (your stub) and records
929
+ * what it returned.
930
+ */
931
+ tools?: "replay" | "live";
932
+ /**
933
+ * In replay, which model calls a recorded answer may serve. `"shape"`
934
+ * (default) refuses a call whose offered tools or response format changed,
935
+ * since the recorded answer may not fit it; see {@link ModelCallMatch}.
936
+ */
937
+ modelCalls?: ModelCallMatch;
938
+ /**
939
+ * In replay, maps the recorded and the new tool call before matching them,
940
+ * like {@link CompareRecordingsOptions.normalize}: redact a generated id in a
941
+ * tool's input so the recorded result still answers it. A dropped
942
+ * (`undefined`) effect matches as recorded.
943
+ */
944
+ normalize?: NormalizeEffect;
945
+ /**
946
+ * In replay, adapts a recorded answer to a model call you changed on
947
+ * purpose. Called with each unused recorded call, in order, until it
948
+ * returns an answer; that answer serves the call regardless of method and
949
+ * shape, and {@link compareRecordings} counts the pair as the same call.
950
+ * Return `undefined` to leave a recorded call to the usual matching.
951
+ *
952
+ * @example
953
+ * ```ts
954
+ * // The original streamed; the port generates.
955
+ * translate: (recorded, actual) =>
956
+ * actual.method === "doGenerate" && recorded.method === "doStream"
957
+ * ? { response: toGenerateResult(recorded.stream) }
958
+ * : undefined,
959
+ * ```
960
+ */
961
+ translate?: (recorded: RecordedModelCall, actual: RecordedModelCall) => ModelAnswer | undefined;
962
+ /**
963
+ * In replay, answers what the recording cannot, instead of throwing
964
+ * {@link RecordingDivergenceError}: a model call no recorded call fits (the
965
+ * port's retry or an added call) goes to `model` (AI SDK seam) or `openai`
966
+ * (client seam), and with `tools: true` a tool call no recorded call matches
967
+ * runs the wrapped tool. Such effects are marked `unrecorded` and still
968
+ * count as extra in {@link compareRecordings}.
969
+ */
970
+ fallback?: {
971
+ model?: RecordableModel;
972
+ openai?: object;
973
+ tools?: boolean;
974
+ };
975
+ }
976
+ /** A model call's answer: the result, the stream's parts, or an error. */
977
+ interface ModelAnswer {
978
+ response?: unknown;
979
+ stream?: unknown[];
980
+ error?: RecordedError;
981
+ }
982
+ /**
983
+ * Maps an effect before matching or comparing; `undefined` drops it.
984
+ * `index` and `effects` are its position in the whole recording, so a repeat
985
+ * of an earlier effect (an idempotent write made again) can be dropped.
986
+ */
987
+ type NormalizeEffect = (effect: RecordedEffect, index: number, effects: readonly RecordedEffect[]) => RecordedEffect | undefined;
988
+ /**
989
+ * When two model calls count as the same: `"shape"` compares offered tool
990
+ * names and response format, `"none"` only that a call happened, and a
991
+ * function decides per call (allow the changes you made on purpose and fall
992
+ * back to {@link sameModelCallShape}).
993
+ */
994
+ type ModelCallMatch = "shape" | "none" | ((expected: RecordedModelCall, actual: RecordedModelCall) => boolean);
995
+ /** The structural slice of an AI SDK `LanguageModelV3` the recorder wraps. */
996
+ interface RecordableLanguageModel {
997
+ doGenerate(options: never): PromiseLike<unknown>;
998
+ doStream(options: never): PromiseLike<{
999
+ stream: ReadableStream<unknown>;
1000
+ }>;
1001
+ }
1002
+ /** The structural slice of an AI SDK decision model (`experimental_decide`). */
1003
+ interface RecordableDecisionModel {
1004
+ doDecide(options: never): PromiseLike<unknown>;
1005
+ }
1006
+ /** An AI SDK model the recorder can wrap. */
1007
+ type RecordableModel = RecordableLanguageModel | RecordableDecisionModel;
1008
+ type AnyFunction = (...args: never[]) => unknown;
1009
+ /** Records (or replays) the effects of one run. */
1010
+ interface Recorder {
1011
+ /**
1012
+ * Wraps an AI SDK `LanguageModelV3` (`openai("gpt-…")`, `anthropic(…)`) or
1013
+ * decision model (`doDecide`), keeping its type. In replay the model is
1014
+ * never called, so a provider model built with no API key works.
1015
+ */
1016
+ model<TModel extends RecordableModel>(model: TModel): TModel;
1017
+ /**
1018
+ * Wraps an `openai` client's `chat.completions.create` and
1019
+ * `responses.create`, keeping its type. A streamed result is an async
1020
+ * iterable only. In replay the client is never called, so
1021
+ * `new OpenAI({ apiKey: "replay" })` works.
1022
+ */
1023
+ openai<TClient extends object>(client: TClient): TClient;
1024
+ /** Wraps one tool function. Its arguments are the recorded input. */
1025
+ tool<TFn extends AnyFunction>(name: string, fn: TFn): TFn;
1026
+ /** Wraps every `execute` in an AI SDK tool set, named by key. */
1027
+ tools<TTools extends Record<string, {
1028
+ execute?: AnyFunction;
1029
+ }>>(tools: TTools): TTools;
1030
+ /** Records the run's final result. */
1031
+ output(value: unknown): void;
1032
+ /** What this run did so far. */
1033
+ readonly recording: Recording;
1034
+ }
1035
+ /**
1036
+ * Thrown in replay when the port asks for something the recording cannot
1037
+ * answer: a model call past the last recorded one, or a tool call the original
1038
+ * never made. The effect is still recorded, so {@link compareRecordings}
1039
+ * reports it.
1040
+ */
1041
+ export declare class RecordingDivergenceError extends Error {
1042
+ name: string;
1043
+ }
1044
+ /**
1045
+ * Creates a {@link Recorder}. With no options it records: every wrapped call
1046
+ * runs for real. With `replay` it answers from a recording instead.
1047
+ */
1048
+ export declare function createRecorder(options?: CreateRecorderOptions): Recorder;
1049
+ /** Why two effects differ. */
1050
+ type EffectDivergenceReason = "missing-effect" | "extra-effect" | "kind" | "model-method" | "model-call-shape" | "model-error" | "tool-name" | "tool-input" | "tool-result" | "output";
1051
+ /** The first effect where two recordings differ. */
1052
+ interface EffectDivergence {
1053
+ /** Position in the compared (tool-order-normalized) effect lists. */
1054
+ index: number;
1055
+ reason: EffectDivergenceReason;
1056
+ expected?: RecordedEffect;
1057
+ actual?: RecordedEffect;
1058
+ /** One sentence, for test failures. */
1059
+ message: string;
1060
+ }
1061
+ /** The result of {@link compareRecordings}. */
1062
+ interface EquivalenceReport {
1063
+ equivalent: boolean;
1064
+ /** The first divergence, by position in the original's effects. */
1065
+ divergence?: EffectDivergence;
1066
+ /** Every divergence, in order. Effects both runs made line up between them. */
1067
+ divergences: EffectDivergence[];
1068
+ }
1069
+ /** Options for {@link compareRecordings}. */
1070
+ interface CompareRecordingsOptions {
1071
+ /** When two model calls count as the same. Default `"shape"`. */
1072
+ modelCalls?: ModelCallMatch;
1073
+ /**
1074
+ * Maps each effect before comparing, on both sides: redact values that
1075
+ * differ on every run (generated ids, timestamps), project a tool's input
1076
+ * to the fields that matter, or return `undefined` to drop the effect.
1077
+ */
1078
+ normalize?: NormalizeEffect;
1079
+ /**
1080
+ * Which kinds of effect to compare. Default all. `["tool", "output"]`
1081
+ * compares a port that calls the model differently (another protocol, a
1082
+ * structured draft instead of a tool loop) by what it did, not how it asked.
1083
+ */
1084
+ kinds?: readonly RecordedEffect["kind"][];
1085
+ }
1086
+ /** True when two model calls offer the same tool names and response format. */
1087
+ export declare function sameModelCallShape(expected: RecordedModelCall, actual: RecordedModelCall): boolean;
1088
+ /**
1089
+ * Compares two recordings and reports every effect that differs. `expected`
1090
+ * is the original's recording, `actual` the port's. Effects that are the same
1091
+ * on both sides are lined up first (longest common subsequence), so one extra
1092
+ * call does not shift every effect after it; between them, effects of the same
1093
+ * kind pair up and report what changed, and the rest are missing or extra.
1094
+ */
1095
+ export declare function compareRecordings(expected: Recording, actual: Recording, options?: CompareRecordingsOptions): EquivalenceReport;
1096
+ //#endregion
1097
+ export { type AgentLintDiagnostic, AgentLintError, type AgentLintSeverity, type AgentPathReport, type AgentPathTerminal, AgentUnknownStateError, type AgreementReport, type AgreementRow, type AgreementSummary, type AssertAgentMachineOptions, type CanReachResult, type CanReachTarget, type CompareRecordingsOptions, type CompareRunsOptions, type CoverageReport, type CreateRecorderOptions, type Divergence, type EffectDivergence, type EffectDivergenceReason, type EquivalenceReport, type ExplorePathsOptions, type ExploreSnapshot, type FirstDivergenceOptions, type FoldTracesOptions, type GetDecisionRequestsOptions, type HandlerCoverage, type HandlerRef, type JudgeAgreementOptions, type LintAgentMachineOptions, type MatchTrajectoryOptions, type ModelAnswer, type ModelCallMatch, type NormalizeEffect, type ParityLabel, type ParityReport, type ParityResult, type ParityRun, type RecordableDecisionModel, type RecordableLanguageModel, type RecordableModel, type RecordedEffect, type RecordedError, type RecordedModelCall, type RecordedOutput, type RecordedToolCall, type Recorder, type Recording, type RepeatGroup, type RepeatReport, type RepeatResult, type RepeatRun, type RepeatRunsOptions, type RunSeamOptions, type RunSeamResult, type SeamCall, type SeamRef, type SeamScriptEntry, type SeamSlice, type SeamTurn, type SimulateAgentOptions, type SimulateAgentResult, type SimulationScript, type SimulationTrailEntry, type StateCoverage, type StateStats, type TraceCoverageOptions, type TraceStep, type TracedDecisionRequest, type TrajectoryEvent, type TrajectoryItem, type TrajectoryMatch, type TrajectoryMiss, assertAgentMachine, canReach, explorePaths, lintAgentMachine, simulateAgent };