# AI Recommendation Engine

## Objective

Suggest the next relevant item from what the user can actually see and act on.

A ranked set of suggested records, content, or actions built from an explicit candidate pool and the user's current context.

## 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. Build the candidate pool with ordinary queries the team can read and reason about — recent, related, popular in this workspace, similar by attribute — before any ranking happens. A pool nobody can explain produces recommendations nobody can debug.
2. Filter the pool for permission, availability, and state before ranking, not after. Ranking an item the user cannot open wastes a slot and undermines the whole surface.
3. Give each recommendation a short, honest reason drawn from the signal that produced it, and offer a dismissal that is remembered.
4. Serve recommendations from precomputed results refreshed on a schedule or on meaningful activity, so a page never waits on a model call. If the ranking is unavailable, fall back to the unranked candidate pool rather than showing nothing.
5. Behavioural signals gathered here must obey the app's existing consent and data-deletion paths. Deleting an account or withdrawing consent must remove the signals and the derived recommendations, not just hide them.

## 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 candidate pool must be defined in terms a person can state in a sentence. Anything opaque cannot be tuned, explained to a customer, or reproduced when someone reports a bad suggestion.
- Never recommend an item the user lacks permission to open, one that is archived, out of stock, or discontinued, or one they have already completed or dismissed. Each of these reads as the app not paying attention.
- Ranking purely by similarity collapses into a single category. Enforce diversity across type, category, or source so the set is useful rather than five variations of the last thing viewed.
- New users and empty workspaces have no history. Fall back to popular, recent, or onboarding-relevant items and make the surface useful from the first session rather than empty.
- Behavioural data is personal data. Honour consent, respect deletion and export requests, and never expose one user's activity through another user's recommendations.
- A recommendation set must degrade gracefully. If the ranking step fails or times out, show the filtered pool in a sensible default order and log the failure rather than rendering an empty panel.
- Refresh timing matters as much as ranking. Recommendations that never change look broken, and ones that change on every reload look random, so pick a refresh cadence and hold it.
- Feedback loops amplify whatever was shown first. Track impressions alongside clicks so a genuinely popular item can be told apart from one that was merely displayed most often.

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

- [ ] The candidate pool is produced by explicit, readable queries before ranking.
- [ ] Permission, availability, and completion filters are applied before ranking, not after.
- [ ] Each recommendation shows a reason and can be dismissed, and dismissals persist.
- [ ] Diversity constraints prevent a set dominated by one category.
- [ ] Users with no history receive a useful fallback set rather than an empty surface.
- [ ] A ranking failure falls back to the ordered candidate pool without an empty state.
- [ ] Behavioural signals honour consent and are removed on deletion, along with derived recommendations.
- [ ] 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.
