A chat screen says “Done,” but its weather lookup never finished. Another screen combines arguments from two parallel calls and sends the wrong city to the tool. Both bugs are easier to isolate when the UI is a projection of recorded events instead of a collection of unrelated booleans.
This guide produces a replayable fixture and a small reducer for a tool status panel. Bring a sanitized failing stream, the provider/API version, the server's tool execution log, and the UI state you expected. You can exercise the example locally without a model call or API key.
Keep the protocol boundaries visible
OpenAI Responses streaming exposes typed lifecycle events. Its function-calling documentation identifies each function item and streams argument fragments before the complete arguments are available. Claude Messages streaming uses indexed content blocks and partial JSON deltas. Their event names and boundaries differ; write an adapter for the exact API you use.
The event vocabulary below is an application contract I use for debugging, not a provider wire format. The server adapter parses provider events and emits it. The server executor supplies execution results. Model generation ending does not prove a client-executed tool succeeded.
Capture a run ID, local event sequence, event kind, call ID, timestamp, and the relevant sanitized payload. Keep the original provider item ID, call ID and block index in adapter diagnostics so you can trace a mismatch. A network read can contain part of an SSE frame or multiple frames; use the SDK or a real SSE parser before mapping events. Never equate one network chunk with one semantic event.
Build the smallest failing replay
Suppose two lookups overlap. The first argument is split after the colon, the second call completes first, and generation ends before the first tool result arrives. Save this as stream-replay.ts and run npx tsx stream-replay.ts in a project that already has tsx:
import assert from "node:assert/strict";
type Event =
| { kind: "args"; call: string; text: string }
| { kind: "args-ready"; call: string }
| { kind: "result"; call: string; ok: boolean }
| { kind: "generation-ended" }
| { kind: "interrupted" };
type Call = { json: string; state: "receiving" | "ready" | "succeeded" | "failed" };
type State = { generation: "running" | "ended" | "interrupted"; calls: Record<string, Call> };
function reduce(state: State, event: Event): State {
if (event.kind === "generation-ended") return { ...state, generation: "ended" };
if (event.kind === "interrupted") return { ...state, generation: "interrupted" };
const previous = state.calls[event.call] ?? { json: "", state: "receiving" };
let next: Call;
if (event.kind === "args") {
assert.equal(previous.state, "receiving", "late argument fragment");
next = { ...previous, json: previous.json + event.text };
} else if (event.kind === "args-ready") {
assert.equal(previous.state, "receiving", "duplicate argument completion");
JSON.parse(previous.json); // Syntax check; the server also validates the tool schema.
next = { ...previous, state: "ready" };
} else {
assert.equal(previous.state, "ready", "result without validated arguments");
next = { ...previous, state: event.ok ? "succeeded" : "failed" };
}
return { ...state, calls: { ...state.calls, [event.call]: next } };
}
function status(state: State) {
const calls = Object.values(state.calls);
if (state.generation === "interrupted") return "Interrupted";
if (calls.some(call => call.state === "failed")) return "Tool failed";
if (state.generation === "running") return "Generating";
if (calls.some(call => call.state !== "succeeded")) return "Waiting for tools";
return "Complete";
}
const events: Event[] = [
{ kind: "args", call: "a", text: '{"city":' },
{ kind: "args", call: "b", text: '{"city":"Boston"}' },
{ kind: "args-ready", call: "b" },
{ kind: "args", call: "a", text: '"Austin"}' },
{ kind: "args-ready", call: "a" },
{ kind: "result", call: "b", ok: true },
{ kind: "generation-ended" },
];
let state: State = { generation: "running", calls: {} };
for (const event of events) state = reduce(state, event);
assert.equal(JSON.parse(state.calls.a.json).city, "Austin");
assert.equal(JSON.parse(state.calls.b.json).city, "Boston");
assert.equal(status(state), "Waiting for tools");
assert.equal(status(reduce(state, { kind: "result", call: "a", ok: true })), "Complete");
assert.equal(status(reduce(state, { kind: "result", call: "a", ok: false })), "Tool failed");
assert.equal(status(reduce(state, { kind: "interrupted" })), "Interrupted");
console.log("PASS: isolated arguments and honest terminal states");
Expected result: all assertions pass and the final line prints. At the end of the recorded stream the panel says “Waiting for tools.” Adding the first tool's success event makes it “Complete”; failure and interruption stay visible. No tool executes in this replay.
This intentionally strict reducer assumes the adapter emits ordered, unique application events. Production code should turn malformed or unknown events into a visible diagnostic state instead of crashing the component. Preserve unknown provider event types in diagnostics and handle protocol evolution deliberately. Tool progress, cancellation acknowledgement and partial-success states need explicit additions when the product supports them.
Separate a retry from a replay
Give every user attempt a new run ID. Key state by run ID plus call ID, and reject callbacks from an obsolete run after the user retries or navigates away. An abort closes the browser's subscription; it does not establish whether a server-side operation stopped. Reconcile with the executor before offering a retry that could repeat an effect.
If your transport supports event replay, define a stable sequence or event ID and deduplicate at the ingestion boundary. Do not deduplicate by payload text: two identical fragments can be legitimate. A reducer replay is for rendering and diagnosis; it must never dispatch the tool again. Dispatch belongs in the server executor with its own execution and idempotency controls.
Use the replay to locate the defect
- Replay the provider capture through the adapter. Verify that both calls retain independent IDs and that argument completion maps once per call.
- Replay the normalized fixture through the reducer. Inspect state after every event, including the first failure.
- Render the resulting states in the UI. A correct reducer with a stuck spinner points toward a stale closure, run selection, rendering or subscription cleanup bug.
- Compare the executor log. If the UI says “Waiting” and the server finished, investigate missing result delivery. If the server is still running, the UI is telling the truth.
Before accepting the fix, cover interleaved calls, empty output, invalid complete arguments, an explicit provider failure, disconnect before a terminal event, a late result from an older run, and a repeated transport event. Check keyboard access and screen-reader status announcements as well as visual spinners. A truncated stream stays interrupted or unknown until reconciled; silence is not success.
Keep the sanitized failing fixture with the bug fix. A replay that reproduces the exact transition is a more useful regression check than a screenshot of a spinner.
