# Streaming AI Responses

## Objective

Show AI output as it is generated so long answers do not look frozen.

Incremental rendering of model output as it arrives, with a persisted authoritative copy once the stream closes.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Find the AI surfaces where the user waits on a long response and stream those. Short classification or extraction calls do not benefit and should stay as plain requests.
2. Buffer incoming bytes until they form complete characters and complete structural units before rendering, so partial output never appears as broken glyphs or half-open markup.
3. Treat the streamed text as provisional. When the stream closes, persist the provider's final message as the authoritative record and reconcile what is on screen against it.
4. Give every stream a wall-clock ceiling and a stall detector, and surface a stalled or dropped stream as a clear failed state with whatever partial text arrived kept and labelled.
5. Do not build a second, non-streaming code path for the same feature. Stop and Regenerate Controls owns cancellation and re-running; this entry owns transport and rendering, and the two must share one request lifecycle.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Chunk boundaries fall in the middle of multi-byte characters and inside code fences, tables, and structured blocks. Hold incomplete units in a buffer rather than rendering whatever arrived.
- A user who cancels expects the work to stop, not just the text to disappear. Abort the upstream request so generation actually ends and no further usage accrues.
- The rendered stream is not the record. Persist the final response when the stream closes, and if the connection dropped before that, persist the partial text explicitly marked as incomplete.
- A reconnecting client must resume from a known position or replay from the persisted record. Naively re-subscribing produces duplicated paragraphs in the transcript.
- Safety flags, citations, and attribution often arrive after or alongside the text they describe. Hold rendering of the affected span, or attach them retroactively, so a citation never points at the wrong sentence.
- Proxies and load balancers buffer responses, which turns a stream into one long pause followed by a wall of text. Confirm the response actually flushes end to end in the deployed environment.
- A stream that produces no tokens for a long interval is indistinguishable from a hung connection unless a heartbeat or stall timeout exists.
- When streaming is unavailable, the feature must degrade to a normal request with a pending state rather than failing outright.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Long AI responses render incrementally and no partial chunk produces corrupted characters or broken structural blocks.
- [ ] Cancelling a stream aborts the upstream request rather than only hiding text.
- [ ] The final response is persisted as the authoritative record when the stream closes, and incomplete streams are stored and labelled as such.
- [ ] Reconnecting after a dropped connection produces no duplicated or missing text.
- [ ] Citations and safety metadata stay attached to the correct spans of streamed content.
- [ ] Streaming and non-streaming paths share one request lifecycle, and streaming failure degrades to a plain pending request.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
