# AI PII Redaction

## Objective

Catch personal information in text before it leaves the app, and mask it.

A redaction pass over text and attachments on the paths that share, export, or forward content to another system.

## 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 every path where text leaves its original context: exports, share links, support handoffs, outbound webhooks, and anything sent to a model provider. Redaction belongs on those boundaries, not scattered through the UI.
2. Run two detectors together and merge their results. Pattern matching handles the formats with a fixed shape, and a model pass handles the cases only context reveals, such as a name and a home address written into a free-text note.
3. Treat the model as one signal, not the decision. Anything the model alone flags must carry lower confidence than a deterministic match, and the feature must keep working with pattern detection only when the model is unavailable.
4. Decide per destination whether redaction is one-way or reversible. Reversible placeholders that map back to the original value are only acceptable when the mapping is stored server-side and unlocking it is a permissioned, audited action.
5. Do not send the whole document to a model to find personal data if a narrower slice will do, and do not log the raw text of anything the detectors matched. Redaction that leaks its own findings into a log line has moved the problem, not solved it.

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

- Pattern detectors and model detectors fail in opposite directions, so run both and combine them. Fixed formats such as card numbers, national identifiers, and email addresses should be caught deterministically, while a model pass catches the personal detail that is only identifiable from the sentence around it.
- A reversible placeholder is a stored link back to the original value, so only issue one where the destination is authorized to unmask, and require an explicit permission check plus an audit entry every time someone reverses it.
- Over-redaction destroys the document. An order quantity, a version number, a product code, or a common first name used as a label must survive, or the recipient gets a page of black boxes and stops trusting the feature.
- The same rules must apply to structured fields, uploaded attachments, and any text the app itself generated. A record redacted in its notes field but exported with an untouched CSV column has not been redacted.
- High-impact documents need a review step where a person sees the proposed masks and can add or remove them before the content is released, rather than the redaction being applied silently on the way out.
- A model refusal, timeout, or malformed response must not let unredacted text through. Fail the outbound operation with a clear message and keep the deterministic matches applied.
- Redaction must be applied to the copy being sent, never to the stored record. Overwriting the source destroys data the workspace still needs.
- Detection quality varies by language and locale. State which locales are covered and be explicit in the UI rather than implying uniform coverage.

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

- [ ] Every path that exports, shares, or forwards content runs the redaction pass before the content leaves.
- [ ] Deterministic pattern matches and model-derived matches are both applied and are distinguishable by confidence.
- [ ] Reversible placeholders exist only where unmasking is permission-checked and recorded.
- [ ] Structured fields, attachments, and generated output are covered by the same rules as free text.
- [ ] High-impact documents present a review step before release.
- [ ] A model failure blocks the outbound operation rather than releasing unredacted text.
- [ ] No raw matched value appears in application logs.
- [ ] 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.
