# AI Import Error Repair

## Objective

Propose fixes for rejected import rows and let the user retry only those.

A repair view for rows that failed validation, with suggested corrections, approval, and a partial retry.

## 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 where the importer records rejected rows and their validation errors, and build the repair view on that record. If failures are only logged, persist them first with the original row intact.
2. Attempt deterministic repair before the model: date and number formats, boolean spellings, trimmed identifiers, known aliases for enumerated values. Send only the rows those rules cannot fix.
3. Group identical failures and propose one rule for the group rather than one suggestion per row. A user approving the same date-format fix four hundred times will approve it without reading.
4. Never fabricate a value for a required field the file does not contain. Mark those rows as unrepairable and require the user to supply the data or drop the row.
5. Retry only the approved rows against the same import job, keyed so that a row that already imported cannot be written twice. Rows that fail again return to the repair view with the new error, not the old 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

- The original row and the exact validation error must stay visible next to every proposal, so a reviewer can tell a formatting fix from a changed meaning.
- Deterministic parsing must run before the model for dates, numbers, and identifiers. A model guessing between day-first and month-first ordering will be confidently wrong on the twelve days a month where both parse.
- A missing required value must never be invented. An identifier, price, or email that the file does not contain has to come from the user, not from a plausible-looking guess.
- A repair rule approved for a group applies uniformly to that group and only after approval, and the affected row count must be shown before it is applied.
- Retries must be idempotent against the original job. Rows that succeeded on the first pass must not be re-created when the repaired remainder is submitted.
- A repaired row that now collides with an existing record must surface as a duplicate decision, not be quietly merged or quietly skipped.
- Rows may contain personal data. Send only the failing fields and their error, exclude columns irrelevant to the failure, and do not log the payload sent to the provider.
- When the model is unavailable, times out, or returns structured output the app cannot parse, the deterministic repairs and the manual edit path must still work.

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

- [ ] Rejected rows persist with their original values and validation errors and are visible in a repair view.
- [ ] Deterministic parsing resolves format failures before any model call.
- [ ] Identical failures are grouped into a single reviewable rule with the affected row count shown.
- [ ] No suggestion supplies a value for a required field absent from the source file.
- [ ] Retrying repaired rows cannot duplicate rows that already imported.
- [ ] Rows failing a second time return with their current error rather than the original one.
- [ ] Model unavailability leaves rule-based repair and manual editing intact.
- [ ] 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.
