ADR-015: Streaming Transport — Server-Sent Events & the Transport/Logic Split
Context
The generative endpoints (RAG Q&A, certificate chat) run a multi-second pipeline: segment the document, embed the passages (up to ~20s uncached), retrieve grounding chunks, then generate the answer token by token. A buffered response leaves the visitor staring at a blank panel — hiding exactly the pipeline the demos exist to showcase. Progress must appear within milliseconds, on a stateless backend with no job store to poll, across a cross-origin path with strict CSP and compression middleware — while pre-flight failures keep the problem+json contract and non-streaming consumers still get plain JSON.
Decision
Stream over Server-Sent Events, negotiated per request, behind a shared writer that splits the API into a transport layer and a demo-logic layer:
1. One route, two transports: a client sending Accept: text/event-stream gets a live stream; any other client gets a single JSON document from the same handler — no parallel /stream routes.
2. The shared writer commits headers lazily on the first event, so validation failures before streaming still answer with problem+json (ADR-018); once streaming, failures arrive as a terminal error event instead of a truncated body.
3. A semantic event vocabulary — stage (segment → embed → retrieve → generate, carrying flags like cached), delta (incremental text), done (final payload + run metadata), error. Handlers emit domain events and never touch headers or framing; the writer silently drops events for non-streaming clients.
4. Anti-buffering is explicit: no-cache, no-transform and X-Accel-Buffering: no keep compression middleware and intermediaries from holding events back.
Amended 2026-10-16: the Q&A stages now include evaluate and search (ADR-025). /api/terraform/plan and /api/terraform/apply also stream, and the contact concierge (/api/ai/contact) adds step and draft events. The concierge is SSE-only, with no JSON fallback. The shared writer is makeSseWriter in server/sse.ts; the Terraform route keeps its own copy.
Consequences
Benefits:
* Progress in milliseconds — visitors watch the pipeline run, and cache hits (ADR-007) show as an instant embed stage.
* Plain HTTP end to end: no upgrade handshake, CSP-friendly, debuggable with curl.
* The layer split keeps handlers transport-agnostic, and the JSON fallback keeps every route fully testable.
* Failure semantics stay on contract: problem+json before the stream, explicit error events after.
Trade-offs:
* One-directional by design; POST + SSE rules out the browser EventSource API, so the client parses the fetch stream manually.
* The event vocabulary is a hand-maintained mini-contract documented in OpenAPI (ADR-018).
* A streaming response holds a Cloud Run request slot for its duration — bounded by the per-IP rate limits (ADR-019).
Alternatives considered
1. WebSockets: Rejected — bidirectional capability nothing here needs, at the price of an upgrade handshake, stateful connections that sit poorly with scale-to-zero Cloud Run, and extra CSP/proxy surface.
2. NDJSON / chunked JSON: Rejected — no named-event semantics, hand-rolled framing on both ends, and intermediaries are prone to buffering it as one body.
3. Polling a job store: Rejected — requires persistent server-side job state, reversing the stateless model (ADR-006), and adds a database write per progress tick for a worse experience.
4. Keeping the buffered single response: Rejected — 10–20s of blank UI on uncached RAG runs, and the pipeline transparency goal is lost entirely.