# Edit Conflict Detection

## Objective

Catch it when someone else saved the record while you were editing it.

Detects that a record changed underneath an open editor and gives the user a real choice about what to keep.

## 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. Give every editable record a version token — a counter or updated-at value — and send it back with the save. Compare it inside the same transaction as the write, not before it.
2. When the token does not match, reject the write and show a conflict screen: what the user typed, what is now stored, and which fields actually differ.
3. Offer three explicit outcomes — merge the non-overlapping fields, overwrite with the user's version, or discard the user's version and reload.
4. Persist the losing draft somewhere the user can get it back before applying any resolution. A conflict must never be the reason work disappears.
5. Do NOT build snapshots, diffs of past revisions, or a restore timeline here — that is Version History's job. This feature only handles the collision at the moment of writing.

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

- Autosave must not throw a modal every few seconds. Detect the conflict once, pause autosave, and hold the draft locally until the user resolves it.
- Two users editing different fields of the same record should merge cleanly without either being asked anything.
- A conflict caused by the same user in a second tab still needs resolving — do not skip the check because the author matches.
- If the record was deleted while the editor was open, that is a conflict too, and 'overwrite' must not resurrect it silently.
- Comparing serialized JSON or rich text will report conflicts on whitespace and key order. Normalize before you diff.
- The version token must come from the server. A client-generated timestamp on a machine with a wrong clock defeats the whole mechanism.

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

- [ ] A stale write is rejected by a server-side version check inside the write transaction.
- [ ] The conflict UI shows the user's version, the stored version, and the specific differing fields.
- [ ] Merge, overwrite, and discard are all available and all work.
- [ ] The losing draft is recoverable after any resolution.
- [ ] Non-overlapping edits merge without prompting.
- [ ] Autosave surfaces a conflict once, not repeatedly.
- [ ] 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.
