# Stop and Regenerate Controls

## Objective

Let users halt a bad answer and ask again without losing the conversation.

Stop and regenerate actions on an in-progress or completed AI response, with the prior context preserved.

## 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. Wire stop to an actual cancellation of the server-side run, propagated to the provider request, so generation ends rather than continuing invisibly.
2. Keep whatever text arrived before the stop, store it marked as stopped by the user, and make that state visible in the transcript rather than deleting it.
3. Build regenerate from the conversation state as it stood before the response being replaced, never from the abandoned partial output.
4. Retain previous attempts and let the user move between them, so regenerating does not destroy an answer that was better than its replacement.
5. Streaming AI Responses owns transport and incremental rendering. This entry owns the user-facing controls and the run lifecycle around them. Do not reimplement stream handling here.

## 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

- Hiding incoming text without aborting the request leaves the model generating and the account paying. Stop must cancel the run at the server and at the provider where the provider supports it.
- Partial output that is silently discarded looks like a bug, and partial output shown as if complete is worse. Keep it, label it as stopped, and disable any action that treats it as a finished answer.
- Regenerating from the unfinished response feeds the model its own truncated text and produces a continuation rather than a fresh answer. Rebuild the request from the ancestor messages only.
- Usage accounting must reflect what actually happened: a stopped run consumes what it generated, and a regeneration is a new run, not a free retry. Reconcile against the provider's reported usage rather than assuming.
- A tool call that already wrote data, sent a message, or charged something cannot be undone by stopping. Detect completed side effects, surface them to the user, and never replay them on regenerate.
- Rapid repeated presses of regenerate must not launch parallel runs against the same message. Disable the control while a run is in flight.
- Stopping must leave the conversation in a state the user can continue from, with the input available and no orphaned pending row in the transcript.
- A run that has already finished server-side when stop is pressed should complete cleanly rather than being recorded as stopped.

## 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

- [ ] Stop cancels the run at the server and the provider, and generation verifiably ends.
- [ ] Partial output is retained in the transcript, clearly marked as stopped, and cannot be mistaken for a complete answer.
- [ ] Regeneration rebuilds the request from prior context and never includes the abandoned partial response.
- [ ] Stopped runs and regenerations are accounted for accurately against real reported usage.
- [ ] Tool calls with completed side effects are surfaced and never re-executed by a regeneration.
- [ ] Repeated presses cannot start parallel runs, and previous attempts remain reachable.
- [ ] 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.
