# AI Topic Tagging

## Objective

Tag content against a controlled vocabulary instead of letting labels multiply.

Automatic assignment of topics from a managed tag catalogue, applied as suggestions with an approval path for new terms.

## 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. Treat the tag catalogue as the authority. Suggested tags must resolve to existing terms, and anything else goes into a proposal queue where an administrator accepts, renames, or rejects it.
2. Cap the number of tags per item and apply a relevance threshold, so only topics genuinely central to the content are applied. Three accurate tags beat twelve that are technically defensible.
3. Apply tags as suggestions the user can accept or dismiss on first rollout, and record acceptance rates before considering automatic application anywhere.
4. Store the catalogue version with each tagging run and give administrators a way to re-tag affected content when terms are merged, renamed, or retired, without re-tagging the entire corpus.
5. Where this sits beside AI Intent Detection, keep the two catalogues separate: intents describe what someone wants done and topics describe what the content is about. Merging them produces a vocabulary that serves neither.

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

- New terms must never be created by the model directly. Route every unmatched suggestion to an approval queue, because an auto-growing vocabulary becomes unusable within weeks and cannot be un-grown.
- Tagging everything a document mentions in passing makes tags useless for filtering. Enforce a per-item ceiling and a relevance threshold, and prefer omission to a weak match.
- Content spanning several topics should receive several tags, and content matching nothing should receive none. An untagged item is a valid outcome and must not be forced into the nearest available term.
- When terms are merged, renamed, or retired, existing assignments must be migrated or invalidated. Stale tags pointing at removed terms should disappear from filters rather than render as blanks.
- Private, restricted, or draft content must not shape tags visible on public surfaces, and tag counts on public pages must not reveal the existence of records the viewer cannot see.
- Re-running the tagger must be idempotent for unchanged content. Repeated runs must not accumulate near-duplicate tags or overwrite tags a person applied by hand.
- Human-applied tags outrank model-applied ones. Record which is which and never let a later automatic run remove a tag a user chose.
- When the model is unavailable, tagging is skipped and the item is queued for a later pass. Publishing must never block on the tagger.

## 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 applied tag exists in the controlled catalogue at the recorded version.
- [ ] Unmatched suggestions enter an approval queue and are never created automatically.
- [ ] A per-item tag ceiling and relevance threshold are enforced, and zero tags is a supported result.
- [ ] Catalogue changes trigger targeted re-tagging rather than a full corpus pass.
- [ ] Manual tags are distinguished from automatic ones and are never removed by a later run.
- [ ] Restricted content does not influence tags or counts on surfaces the viewer cannot access.
- [ ] 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.
