# AI Data Cleanup Suggestions

## Objective

Surface inconsistent or suspicious records and propose corrections a human approves.

A review queue of proposed data corrections, each showing the original value, the reason, and an approval step.

## 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. Identify the fields where dirty data actually hurts: names, addresses, phone numbers, currencies, categories, and any field used for matching or reporting. Scope the first pass to those rather than scanning every column.
2. Run deterministic checks first — casing, whitespace, duplicate punctuation, known formats, values outside an allowed set — and only send the residue to the model. Do not spend model tokens on problems a rule already solves.
3. Separate formatting cleanup from semantic correction and present them as two kinds of suggestion. Trimming a stray space and changing a country from one value to another carry very different risk and deserve different approval thresholds.
4. Write every suggestion to a review queue with the original value, the proposed value, the reason, and a confidence. Do not mutate source records directly, and do not auto-apply anything except rules an operator has explicitly promoted to automatic.
5. This brief owns data already stored in the app. Rows failing validation during an upload belong to AI Import Error Repair, and header-to-field matching belongs to AI Import Column Mapping; reuse their approval surface rather than building a second one.

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

- Nothing may overwrite a source record without a review step or an operator-authored rule. An unattended cleanup pass that guesses wrong is worse than the dirt it removed.
- Formatting cleanup and semantic correction must stay distinguishable in the queue, because a reviewer will approve a hundred whitespace fixes in a batch and must not sweep a changed value through with them.
- Each suggestion must show the original value alongside the proposal and state the reason plainly, or reviewers approve on trust and stop reading.
- Values that appear frequently in already-dirty data are not thereby correct. Validate proposals against the app's own reference data and allowed values, not against the majority pattern in the same dirty table.
- Applying a batch twice must produce the same result as applying it once, and every applied change must be recorded with who approved it and be reversible.
- Records edited by a user after a suggestion was generated must be re-checked before the fix lands, or approval silently reverts someone's work.
- Field values may be personal data. Send the minimum needed to judge the value, exclude fields the reviewer has no permission to see, and never send an entire record when one column is under review.
- When the model is unavailable or returns malformed structured output, keep the deterministic suggestions flowing and mark the semantic pass as unavailable rather than emptying the queue.

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

- [ ] Suggestions appear in a review queue and no source record changes without approval or an explicit operator rule.
- [ ] Formatting and semantic suggestions are labelled distinctly and can be filtered and approved separately.
- [ ] Every suggestion displays the original value, the proposed value, and a stated reason.
- [ ] Applying the same batch twice changes nothing the second time, and every applied change is recorded and reversible.
- [ ] Deterministic checks run before the model and the model only sees what rules could not resolve.
- [ ] A record modified since its suggestion was generated is re-checked before the change is applied.
- [ ] Model unavailability leaves the rule-based suggestions working.
- [ ] 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.
