Awesome Testing

API lesson 02 · Assemble event streams

How do partial events become one validated result?

Shared system · deterministic provider fixtures

Harbor Support Drafting API. One bounded request moves through typed items, tools, state, budgets, and evidence without live provider calls.

01 · Smallest useful mechanism

Treat a stream as a state machine with exactly one terminal outcome.

Streaming clients must correlate content blocks, accumulate deltas, tolerate keep-alives, handle cancellation, and finalize only after the documented terminal event.

Partial text and partial JSON are previews, not validated final values.

02 · Experiment

Test the prediction

Deterministic protocol fixture

This workbench uses inspectable provider-event fixtures and injected failures. It does not make live calls or execute external effects.

Protocol workbench · harbor-stream-v2

Select the assembler that survives a connection loss during tool arguments.

Injected failure

The stream ends after `{"policy_` without a completion event.

Choose the implementation

The trace has not run.

Commit to an implementation, then inject the failure and inspect the actual state transitions.

03 · Implementation brief

Build it, break it, prove the outcome.

Implement
Write an event reducer with explicit states and per-item buffers.
Break it on purpose
Close the connection mid-JSON, duplicate one text delta, and cancel after tool dispatch.
Completion evidence
No partial call runs, duplicate data is detected or correlated, and the outcome record distinguishes cancellation from outcome unknown.

Your learning artifact

Write the argument you would defend

Stored only in this browser. No account required; course reset does not delete it.

04 · Check your understanding

When may streamed tool arguments be executed?

Primary sources · verified 2026-08-02

What the fixture does not prove

A smooth typewriter effect can conceal duplicated text, malformed tool arguments, lost terminal usage, or work that continued after the user cancelled.

Common mistake: Every chunk is independently safe to render or parse. Chunks may split Unicode, words, citations, or JSON tokens. Accumulate according to the event contract and apply moderation and validation at the correct boundary.

Transfer exercise: Reorder, duplicate, and truncate the fixture events; make the assembler reject impossible transitions without losing the diagnostic trace.

Optional referenceDeep dive / reference chapterOpen the complete essay, diagrams, mathematics, exercises, glossary, and sources when you want more depth.

Next: A correct stream can deliver structured output, but structure alone does not make the values true or authorized.