ExAgent.Fixture (ExAgent v3.0.0)

View Source

A per-case model cassette, recorded and replayed at the ExAgent.Model dispatch, never on the wire.

Stability — Stable.

Pass an open fixture to ExAgent.run/3 or ExAgent.run_stream/3 as fixture:. It belongs to the run's execution scope, so every descendant uses it: ExAgent.run_child/4 (delegation, ExAgent.Judge.output_validator/1, Advisor), workflow steps given root_options: [fixture: f], and the runs that an :approve callback or a compaction summarizer starts synchronously with ExAgent.run/3 while the run executes.

Each request is matched by a SHA-256 fingerprint of its canonical form: the model's system, name and static ExAgent.Model.continuation_binding/1 (for ReqLLM: endpoint, profile, reasoning mode and effort, routing), the messages without timestamps, identifiers of the run or usage, the instructions, tool definitions, output configuration and every model setting except :timeout. Stream and buffered requests share recordings. Enable :stream_events when recording to replay the captured sequence as well as the terminal. Each entry also has a path and a 1-based ordinal for that fingerprint on that path. Flow and Composition supply the workflow identity and step ID. with_label/2 distinguishes independent roots and appends child labels to the inherited path in ExAgent.run_child/4.

:replay never calls a model. An unrecorded request fails with %ExAgent.RequestError{provider: :fixture, reason: {:fixture_miss, %{fingerprint: fp, nearest: pointer}}}. A fingerprint shared by independent runs on the same path fails with {:fixture_ambiguous, %{fingerprint: fp}}. Eval treats it as a replay error. :record calls every model and replaces the file on close/1; :record_missing calls models only for unmatched requests and drops unused entries. A recording whose request fails is not written.

Close the fixture after the last request to commit a recording and release its process. Treat the fixture returned by open/2 as a handle and use the public functions to work with it.

Summary

Functions

Remove the guard of deny_live_models/1 and reset denials/0.

Release the fixture. A recording mode writes the file atomically, unless a recorded request failed (also when a capability recovered the run) or an entry limit was reached ({:error, :not_written} or {:error, {:fixture_limit, :bytes}}; the previous file is kept). Returns the counts of replayed (hits), newly recorded and unmatched (misses) requests, and the entries pruned by :record_missing.

The number of model dispatches the guard rejected since deny_live_models/1 first installed it (0 without a guard).

Reject every model dispatch outside a fixture, VM-wide, before any IO.

Open the cassette at path.

Return a labeled view of an open fixture.

Types

mode()

@type mode() :: :replay | :record | :record_missing

stats()

@type stats() :: %{
  hits: non_neg_integer(),
  recorded: non_neg_integer(),
  misses: non_neg_integer(),
  pruned: non_neg_integer()
}

t()

@type t() :: %ExAgent.Fixture{
  attempt: term(),
  child_path: term(),
  label: term(),
  mode: term(),
  owner: term(),
  path: term(),
  pid: term(),
  redact_request: term(),
  redact_response: term(),
  root_label: term(),
  section: term(),
  store_requests: term(),
  stream_events: term(),
  test_model_only: term(),
  tool_stubs: term()
}

Functions

allow_live_models()

@spec allow_live_models() :: :ok

Remove the guard of deny_live_models/1 and reset denials/0.

close(f)

@spec close(t()) :: {:ok, stats()} | {:error, term()}

Release the fixture. A recording mode writes the file atomically, unless a recorded request failed (also when a capability recovered the run) or an entry limit was reached ({:error, :not_written} or {:error, {:fixture_limit, :bytes}}; the previous file is kept). Returns the counts of replayed (hits), newly recorded and unmatched (misses) requests, and the entries pruned by :record_missing.

denials()

@spec denials() :: non_neg_integer()

The number of model dispatches the guard rejected since deny_live_models/1 first installed it (0 without a guard).

deny_live_models(opts \\ [])

@spec deny_live_models(keyword()) :: :ok

Reject every model dispatch outside a fixture, VM-wide, before any IO.

A model whose module is not in :allow fails with %ExAgent.RequestError{provider: :fixture, reason: :live_model_denied}. The default allowlist is ExAgent.Models.Test and ExAgent.Models.Fallback, whose candidates are checked one by one. Replayed requests never reach the guard. Calling it again replaces the allowlist and keeps the count of denials/0. Use it in test_helper.exs or before a programmatic replay.

open(path, opts \\ [])

@spec open(Path.t(), keyword()) :: {:ok, t()} | {:error, term()}

Open the cassette at path.

Options:

  • :mode — :replay (default), :record or :record_missing. Replay and :record_missing read the file; :record ignores it and replaces it on close/1. A missing file is {:error, :fixture_missing} in replay.
  • :case — the UTF-8 string stored in the file as its case name and checked on reading (default Path.basename(path)). A file of another case is {:error, :invalid_fixture}.
  • :tool_stubs — opt in to strict tool dispatch for this fixture using a map of tool names to test callbacks. See ExAgent.Testing.with_tool_stubs/2.
  • :store_requests — also store each canonical request, after :redact_request, so a miss reports the JSON Pointer where it differs from the nearest recording (default false: fingerprints only).
  • :redact_request — (map -> map) applied to the canonical request before its fingerprint, when recording and when replaying.
  • :redact_response — (ExAgent.Message.Response.t() -> ExAgent.Message.Response.t()) applied to complete responses before the run sees them. The recorded response keeps the model's finish reason. Inherited citation excerpts and ranges are cleared before the callback. It may return citations with redacted excerpts and ranges for the replacement.
  • :stream_events — opt in to capturing stream events. Use :verbatim to keep normalized model events as delivered, or a unary callback that returns a replacement of the same event type or :drop. This includes text, thinking, tool argument fragments and usage snapshots. Tool fragments require the usual preview opt-in. The callback runs before the recording run receives the event. If you also set :redact_response, supply a callback for :stream_events. By default, recordings contain only terminals. Replay uses the stored events when present. Recording and replay use cassette format v1.

Other errors: {:unsupported_fixture_version, v}, {:fixture_limit, :bytes} above 32 MiB, {:fixture_limit, :interactions} above 1,024 entries per attempt. Each captured stream accepts up to 4,096 provisional events before redaction and up to 1 MiB of retained event JSON. Exceeding these bounds returns {:fixture_limit, :stream_events} or {:fixture_limit, :stream_bytes}. Malformed events and invalid event redactor returns fail the request with :invalid_fixture_stream_event. An aborted recording keeps the previous file. Unknown options or invalid values raise ArgumentError.

with_label(f, label)

@spec with_label(t(), String.t()) :: t()

Return a labeled view of an open fixture.

Use stable labels for roots or sibling runs that may issue identical requests. Pass the view as fixture: in recording and replay, or in a workflow's :root_options to label its root. ExAgent.run_child/4 appends the label when the view belongs to the fixture inherited from its parent. The label is written to the cassette and must be a non-empty UTF-8 string of at most 512 bytes.