Turn a Studio run into a test
Studio can hand you any recorded run as a Go test that replays it. Save as test on the run page downloads a zip: a generated <agent>_replay_test.go beside the run’s fixtures under testdata/TestReplay_<run>/. Unzip it into a package directory named after the agent, or rename the generated package clause, and go test passes against the recorded run, offline. From the terminal, weft export <run id> --wefttest ./testdata --test Name writes the same fixtures where wefttest.Replay reads them, for a test you write against your real agent.
That test, from testing/support/support_test.go, replays a run Studio recorded:
agt := support.New(wefttest.Replay(t, "testdata"))steers := wefttest.NewSteers().At(0, weft.User("Sorry, I meant order 5678."))res, err := agt.Generate(context.Background(), weft.Prompt("Where is order 1234?"), steers.Option())if err != nil { t.Fatal(err)}if got, want := res.Text(), "Order 5678 is still pending."; got != want { t.Errorf("answer = %q, want %q", got, want)}The fixtures came from the run with weft export; the agent and its lookup_order tool are the real ones. The run was steered after step 0, so the test steers it the same way.
Save as test
Section titled “Save as test”On a run’s page in Studio, the header’s save as test fetches GET /api/runs/{id}/export?format=wefttest with the bearer in the Authorization header, never a token in a URL, and saves the zip under its Content-Disposition name, <run>.zip. The toast says saved <file> — unzip into a package directory and go test, or save as test refused: <reason> in the server’s words. In the devtools panel, a turn’s save as test is a link to the run page with ?save=test, which downloads there.


The same request from a shell:
curl -sD - -H "Authorization: Bearer $WEFT_STUDIO_TOKEN" -o run.zip \ "$WEFT_STUDIO_URL/api/runs/f4ec869326ccfa66cb405e00efdaa035/export?format=wefttest"unzip run.zipHTTP/1.1 200 OKContent-Disposition: attachment; filename="f4ec869326ccfa66cb405e00efdaa035.zip"Content-Length: 4175Content-Security-Policy: …Content-Type: application/zip…
Archive: run.zip inflating: support_replay_test.go inflating: testdata/TestReplay_f4ec869326ccfa66cb405e00efdaa035/00001-58a3644d032e0bef.json inflating: testdata/TestReplay_f4ec869326ccfa66cb405e00efdaa035/00002-18d5ff15226507cf.json inflating: testdata/TestReplay_f4ec869326ccfa66cb405e00efdaa035/00003-5073ba88275e102e.jsonA replay is saved as its whole turn. A playground replay from step n > 0 kept the source’s steps 0 to n−1 without re-running them, so its test composes them. For each kept step it takes the source’s own fixtures (followed up a replay of a replay, at most 8 links), then the replay’s own fixtures renumbered after them. The generated test replays the turn from its start. A scripted replay’s fixtures are stamped with the source’s model. The test’s header says Kept: steps 0–n−1 are <source>'s fixtures (not re-run by this replay), the export answers a Weft-Kept-Prefix header naming the chain from the root (<source>#<n>, or <root>#<k>,<replay>#<m>), and Studio’s toast says how many steps came from the source (0.19.0): saved <file> (steps 0–n−1 from the source <run>, n on from the replay) — unzip into a package directory and go test, where n counts every kept step. For a replay of a replay it names the chain from the root, from the sources <root> → <replay>. A source that is gone, derived, or did not record each kept step is a 409 saying so, never a partial test. An agentless run’s test file is run_replay_test.go, package run_test.
The route needs the export capability and an identity that reads prompts: a read-scoped panel token is refused with 403 and the hidden badge, because fixtures carry the system prompt and the tool catalog. A run with nothing to replay is refused: a content-off run, with no transcript, is 409 with the stripped badge; a run that recorded no answered step is 409 with no badge; a run derived from stock OpenTelemetry spans is 400 with the derived badge. &tree=1, which exports a run with its subagents in the other formats, is a 400 here: the replay substitutes the subagents’ recorded answers as tool results.
What the generated test does
Section titled “What the generated test does”This is the file Studio generated for the run above, unchanged except for its directory:
// Generated by weft Studio's "save as test" from run "f4ec869326ccfa66cb405e00efdaa035". Edit freely: it is yours now.//// Agent: "support"// Run: "f4ec869326ccfa66cb405e00efdaa035"// Model: "wefttest/script"// Prompt: "Where is order 1234?"// Recorded on: 2026-10-18// Steps: 3// Step 1's request carries 1 message(s) steered in after step 0: replayed through wefttest.NewSteers.//// Drop this file and testdata/ into the package that builds the agent, and rename// the package clause to that package's name plus _test. The fixtures live in// testdata/TestReplay_f4ec869326ccfa66cb405e00efdaa035/, named after the test: rename the test and rename the directory.// wefttest.Replay answers every model call from the fixture keyed on its request// (messages, tool names, thinking, tool choice, the sequential flag; never the// system prompt), so the test passes as long as the requests stay the recorded// ones. The tools below are stubs answering with the recorded results: swap in// the real tools and agent, and a changed request fails with wefttest.ErrNoFixture// naming it. Re-record with wefttest.Record.package support_test
import ( "context" "encoding/json" "errors" "fmt" "os" "path/filepath" "testing"
"github.com/weftgo/weft" "github.com/weftgo/weft/wefttest")
// TestReplay_f4ec869326ccfa66cb405e00efdaa035 replays run "f4ec869326ccfa66cb405e00efdaa035" of agent "support" from its fixtures.func TestReplay_f4ec869326ccfa66cb405e00efdaa035(t *testing.T) { const fixtures = "testdata/TestReplay_f4ec869326ccfa66cb405e00efdaa035"
// requestMessages reads one fixture's request messages back. requestMessages := func(file string) ([]weft.Message, error) { b, err := os.ReadFile(file) if err != nil { return nil, err } var fx struct { Request struct { Messages []weft.Message `json:"messages"` } `json:"request"` } if err := json.Unmarshal(b, &fx); err != nil { return nil, fmt.Errorf("%s: %w", file, err) } return fx.Request.Messages, nil }
// The results the run recorded, by step and call id (a provider may // reuse a call id across steps): fixture k+1's request ends with step // k's reply and the tool results that answered it. files, err := filepath.Glob(filepath.Join(fixtures, "*.json")) if err != nil || len(files) == 0 { t.Fatalf("no fixtures in %s: %v", fixtures, err) } type stepCall struct { step int id string } recorded := map[stepCall]weft.ToolResultPart{} for k, f := range files[1:] { // files sort in step order msgs, err := requestMessages(f) if err != nil { t.Fatal(err) } last := -1 for i, m := range msgs { if m.Role == weft.RoleAssistant { last = i } } for _, m := range msgs[last+1:] { for _, part := range m.Content { if r, ok := part.(weft.ToolResultPart); ok { recorded[stepCall{k, r.CallID}] = r } } } } // answer is every stub's handler: the recorded result for the call, // an error result exactly as the run recorded it. answer := func(ctx context.Context, _ json.RawMessage) (string, error) { call, _ := weft.CallFromContext(ctx) r, ok := recorded[stepCall{call.Step, call.CallID}] switch { case !ok: return "", errors.New("no recorded result for call " + call.CallID + " (the recording never ran it)") case r.IsError: return "", errors.New(r.Content) } return r.Content, nil } const stub = "recorded tool: replace with the real one" tools := map[string]*weft.ToolDef{ "lookup_order": weft.RawTool("lookup_order", stub, nil, answer), }
opts := []weft.Option{ weft.Name("support"), weft.Instructions("You are a support agent. Look an order up before you answer."), weft.MaxSteps(3), weft.StopWhen(weft.StepCountIs(3)), // the recorded run's length weft.MaxResultBytes(0), // the recorded results are already capped } for _, name := range []string{"lookup_order"} { opts = append(opts, tools[name]) } agt := weft.New(wefttest.Replay(t, "testdata"), opts...)
// The messages the recorded run was steered with, at the steps they followed. steers := wefttest.NewSteers(). At(0, weft.User("Sorry, I meant order 5678.")) res, err := agt.Generate(context.Background(), weft.Prompt("Where is order 1234?"), steers.Option()) if err != nil { t.Fatalf("replay: %v", err) } if got, want := len(res.Steps), 3; got != want { t.Errorf("steps = %d, want %d", got, want) } if got, want := res.Text(), "Order 5678 is still pending."; got != want { t.Errorf("final text = %q, want %q", got, want) }}It compiles against the public github.com/weftgo/weft API only, and rebuilds the requests the run made so each one finds its fixture:
- The input is the run’s prompt,
weft.Prompt(…), when it was one user text; otherwise the first fixture’s messages, read back. - The tools are stubs. One
weft.RawToolper tool name the requests offered, each answering with the result the run recorded for that call, keyed by step and call id, so a provider that reuses call ids across steps still replays. An error result answers as an error, so the text is byte-identical. A call the recording never ran gets an error naming it. A run that offered no tools gets no stubs, and its test declares and imports only what it uses, so it compiles as it stands (since 0.20.2). - The settings are step 0’s. Thinking, tool choice and the sequential flag become agent options. A later step whose tools or settings differ gets a generated
weft.PrepareStepcase. - The end is the recorded length.
weft.MaxSteps(n)andweft.StopWhen(weft.StepCountIs(n)), so a run that ended on a stop condition, a parked call or its step budget ends there too.weft.MaxResultBytes(0)keeps the recorded results uncapped: they were capped when they were recorded. - The system prompt is step 0’s composed text. It is there to read; the replay key never includes it.
The test’s name is TestReplay_ plus the run id, every byte outside [A-Za-z0-9_] spelled _; the fixture directory has the same name, because Replay(t, "testdata") reads testdata/<t.Name()>/. The package clause is <agent>_test, the agent’s name lowercased (replay with no name; prefixed replay_ when it would start with a digit or _, or is a Go keyword). Rename the clause to your package’s name plus _test, and rename the test and its directory together.
The stubs are where to start. Replace them with the real tools, and the agent with the real one, and the test keeps answering from the recording until a request changes; then it fails with wefttest.ErrNoFixture naming the request.
Steered and compacted steps
Section titled “Steered and compacted steps”The generator reads the recorded transcript step by step and adds what the replay needs for the requests to match:
- A steered step is replayed through a generated
wefttest.NewSteerssource,At(step, messages…)keyed by the step the messages followed. The header names it:Step 1's request carries 1 message(s) steered in after step 0: replayed through wefttest.NewSteers.A steer can only add user messages, so that is what the generator looks for. - A compacted step, one whose request carried a compaction view, keys on the compacted messages the model saw. Its fixture carries a
compacted_atheader (index,step,from_seq,to_seq,hash,replaced,entries), the test’s header saysStep N replays from the compacted view (…), and the generatedPrepareStepswaps that fixture’s messages into the request. - A step whose transcript the run changed any other way replays its recorded messages from that step on, through the same
PrepareStep:Step N replays the recorded messages: the recorded run rewrote them.That replays the record; it does not re-derive the change. - A last step with tool calls that never ran (parked, or ended by a stop condition) is noted in the header; their stubs answer with an error, and
StepCountIs(n)ends the replay before any request sees them.
Each fixture is the step its records were stamped with (weft.step.index), not the Nth assistant message, and its key takes the tool names, thinking, tool choice and sequential flag from the step’s answering request record. A step whose attempts all failed gets no fixture; a failed attempt that a retry answered is invisible to the replay, which answers the step’s first call under any middleware.
Export fixtures with the CLI
Section titled “Export fixtures with the CLI”weft export <id> --wefttest <dir> writes Studio’s save-as-test tree into the package directory <dir>, byte for byte the zip above, a replay’s composed with its source’s kept steps as above: <agent>_replay_test.go beside testdata/TestReplay_<run>/*.json, a test that passes under go test there. It prints one line:
weft: wrote <agent>_replay_test.go and <n> fixtures under testdata/TestReplay_<run> in <dir>; go test there replays run <id> (rename the package clause to the directory's package plus _test)A test file or fixtures already there need --force; a symlinked testdata or testdata/<test> is refused before any write. Before 0.19.0 --wefttest wrote the fixtures alone; that is now --fixtures-only.
--fixtures-only writes only the fixtures, into <dir>/<test>/, for a test you write yourself: your test’s name, given with --test, need not match the skeleton’s. From docs-site/examples/testing/support, against the Studio that recorded the run:
weft export f4ec869326ccfa66cb405e00efdaa035 --wefttest ./testdata --fixtures-only --test TestSteeredTurnweft: wrote 3 fixtures to testdata/TestSteeredTurn; a test named TestSteeredTurn replays them with wefttest.Replay(t, "./testdata")| Flag | Effect |
|---|---|
--wefttest <dir> | Write the save-as-test tree into the package directory <dir> |
--fixtures-only | With --wefttest: write the fixtures alone under <dir>/<test>, <dir> a suite’s testdata |
--test <name> | With --fixtures-only: the test that replays them, so the directory <dir>/<name>; a subtest’s TestX/case is a nested directory. Default: the run id. A name that climbs out of <dir> is refused. Without --fixtures-only it is a usage error: the tree names its test TestReplay_<run> |
--force | Write over what is there. Without it: weft: testdata/TestSteeredTurn is not empty: --force writes into it, exit 1 |
--force replaces, never merges: the *.json files already in the target are removed first, as wefttest.Record replaces a test’s directory, because Replay loads every *.json there and a stale one would answer in place of a miss. --format wefttest is refused (--format wefttest is a zip of fixtures: use --wefttest <dir>, exit 2), and --wefttest cannot be combined with --format. The CLI finds Studio through --url, else WEFT_STUDIO_URL, else the discovery file, and its token through --token, WEFT_STUDIO_TOKEN or the discovery file’s.
Then write the test against your real agent, as TestSteeredTurn does above. It passes as long as your agent makes the requests the run made: the same tool names, the same transcript, the same settings. Change a tool’s name or the prompt and the request misses.
Playground fixtures
Section titled “Playground fixtures”With the playground on (studio.Playground(true), the default of weft studio), POST /api/playground/fixtures returns a run’s fixtures as JSON instead of a zip. The body is {"run_id": "…", "tools": […]}; tools is used only for a run recorded before request records existed. The answer is {"run_id", "agent", "files": [{"name", "body"}]}, each file named the way Replay loads it. It shares the export’s builder, so the files are byte for byte the zip’s fixtures. A read-scoped panel token is refused with 403; a run with nothing to fixture is refused as the export refuses it (409 stripped, 409 with no badge, or 400 derived).
Complete example
Section titled “Complete example”testing/support/ is the package that builds the agent. Beside support.go it holds the generated test with its fixtures, and the hand-written test with the fixtures weft export wrote:
package support_test
import ( "context" "testing"
"github.com/weftgo/weft" "github.com/weftgo/weft/wefttest" "weftgo.dev/docs/examples/testing/support")
// TestSteeredTurn replays the fixtures `weft export --wefttest ./testdata// --test TestSteeredTurn` wrote, against the real agent and its real tool.func TestSteeredTurn(t *testing.T) { agt := support.New(wefttest.Replay(t, "testdata")) steers := wefttest.NewSteers().At(0, weft.User("Sorry, I meant order 5678.")) res, err := agt.Generate(context.Background(), weft.Prompt("Where is order 1234?"), steers.Option()) if err != nil { t.Fatal(err) } if got, want := res.Text(), "Order 5678 is still pending."; got != want { t.Errorf("answer = %q, want %q", got, want) }}testing/support/├── support.go├── support_replay_test.go├── support_test.go└── testdata/ ├── TestReplay_f4ec869326ccfa66cb405e00efdaa035/ │ ├── 00001-58a3644d032e0bef.json │ ├── 00002-18d5ff15226507cf.json │ └── 00003-5073ba88275e102e.json └── TestSteeredTurn/ ├── 00001-58a3644d032e0bef.json ├── 00002-18d5ff15226507cf.json └── 00003-5073ba88275e102e.jsonThe run was recorded from support.New over a scripted model, steered with wefttest.NewSteers().At(0, weft.User("Sorry, I meant order 5678.")), and sent to a weft studio through otel.Studio.
View on GitHub · run it from docs-site/examples:
WEFT_MODEL_REQUESTS=deny go test ./testing/support -v=== RUN TestReplay_f4ec869326ccfa66cb405e00efdaa035--- PASS: TestReplay_f4ec869326ccfa66cb405e00efdaa035 (0.00s)=== RUN TestSteeredTurn--- PASS: TestSteeredTurn (0.00s)PASSok weftgo.dev/docs/examples/testing/support 0.003sBest practices & gotchas
Section titled “Best practices & gotchas”- Swap the stubs for your tools. A test over stubs proves the recording replays; a test over your agent proves your agent still makes the recorded requests. (Source:
weft/studio/savetest.go:51-56) - Rename the test and its directory together.
Replayreadstestdata/<t.Name()>/; a renamed test misses every fixture. (Source:weft/studio/savetest.go:56-62,weft/core/wefttest/replay.go:194-195) - Save a test with the dev token or a playground-scoped panel token. A read-scoped panel token cannot read the prompts the fixtures carry; the run page’s save as test is refused under one. (Source:
weft/studio/export.go:76-81,weft/studio/requests.go:187-190,weft/studio/README.md:841-848) - Content-off runs cannot be saved. With no transcript there is nothing to replay, and the export answers 409
stripped. (Source:weft/studio/export.go:342-346,weft/studio/fixtures.go:601-622) - Keep the steer in the test. Without
NewSteers, step 1’s request lacks the steered message and misses its fixture. (Source:weft/studio/savetest.go:174-216,weft/core/wefttest/steer.go:11-20) - Keep a step’s prompt as the agent’s, not as the model saw it. keep as prompt, beside a step’s system prompt in the Request pane, strips the offered tools’ PromptSnippets only when the manifest verifies the run’s agent. Otherwise it proposes the recorded text, which may hold snippets or a
PrepareSteprewrite, and its toast says so. Pasting that text intoweft.Instructionscomposes the snippets twice (Proposed prompts). (Source:weft/studio/web/src/lib/request-pane.ts:261-301;weft/studio/README.md:2574-2591) - Studio writes nothing to your files. The download is the hand-off;
weft exportwrites only under the directory you name. (Source:weft/studio/README.md:2566,weft/cmd/weft/api.go:792-803)
See also
Section titled “See also”Symbols used: wefttest.Replay · wefttest.NewSteers · wefttest.ErrNoFixture · wefttest.Record · weft.RawTool · weft.PrepareStep · weft.StopWhen · weft.StepCountIs · weft.MaxSteps · weft.MaxResultBytes · weft.CallFromContext · studio.Playground · otel.Studio