# AI Conversation Branching

## Objective

Explore an alternate path from an earlier message without losing the original.

A message graph that lets a conversation fork at any point, with each branch keeping its own history and active state.

## 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. Change the conversation's storage from an ordered list to a graph where every message names its parent, and migrate existing conversations into that shape as single-path graphs.
2. Define the active branch as a stored pointer to a leaf message, and derive everything rendered from the path between the root and that pointer.
3. Give each branch its own summary, memory, and derived context. Carrying one summary across siblings leaks the abandoned path back into the new one.
4. Show the user, at the fork point, that alternatives exist, which one is active, and how to move between them, without turning the transcript into a diagram.
5. AI Message Editing owns editing an ancestor message and invalidating what follows. This entry owns the graph and navigation. Editing should create a branch through this model rather than mutating history in place.

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

- Every message must carry an unambiguous parent, including the first. A graph where the parent is inferred from timestamps will scramble under concurrent writes.
- Summaries, retrieved context, and remembered facts are branch-scoped. Reusing the parent conversation's memory in a new branch reintroduces exactly the content the user branched away from.
- Branching from a point after a tool call must reuse the recorded result rather than re-running the tool, because re-running it repeats side effects such as sending, charging, or writing.
- Without a visible indicator of which branch is active and a way back, users lose work they believe they can find again. Show the alternatives count at the fork and keep navigation reversible.
- Editing a message that has descendants must not corrupt them. Fork at the edited message rather than rewriting it, so the original subtree stays intact and reachable.
- Deleting a message must have a defined effect on its descendants, and deleting a fork point must not orphan the branches hanging from it.
- Sharing or exporting a branched conversation needs a defined scope: the active path by default, with any other choice made explicit to the person receiving it.
- Usage and cost accounting must attribute each run to its branch, or a heavily explored conversation becomes impossible to explain on a bill.

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

- [ ] Conversations are stored as a parent-child message graph and existing linear conversations are migrated into it.
- [ ] The active branch is an explicit stored pointer, and the rendered transcript is derived from the root-to-pointer path.
- [ ] Summaries, memory, and retrieved context are scoped per branch and do not leak across siblings.
- [ ] Branching past a tool call reuses the recorded result and never re-executes the action.
- [ ] The interface shows where forks exist, which branch is active, and how to return to the others.
- [ ] Editing an ancestor forks the graph and leaves the original subtree intact and reachable.
- [ ] Runs are attributed to the branch they belong to for usage and cost reporting.
- [ ] 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.
