# Related Posts

## Objective

Show a few genuinely relevant articles at the end of a post so readers keep going.

A block of suggested articles below each post, chosen from tags, topic, and recency.

## 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. Score candidates from signals the app already has: shared tags from Labels and Tags or AI Topic Tagging, the same category, and closeness in publish date. Combine them into one ranking rather than picking the first tag match.
2. If the app already has Semantic Search or an AI Recommendation Engine, use it as the similarity source and treat tags as a tiebreaker. Do not build a second relevance system beside the one that already exists.
3. Compute relations in the background and store the result. Ranking the whole archive on every page view will be the slowest part of the article page.
4. Give the block a graceful fallback: when nothing scores well, show recent posts from the same category, and when there is still nothing, render nothing rather than an empty heading.
5. Do not suggest posts purely by popularity. A view-count ranking sends every reader to the same three evergreen articles and the rest of the archive never surfaces.

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

- A newly published post has no tags and no signal to match on. Fall back to the same category and to recency rather than showing an empty block on every new article.
- The current post must be excluded from its own suggestions. It scores highest against itself on every similarity measure.
- Drafts, scheduled posts, and posts behind a paywall or permission check must never appear as suggestions. Filter by the same visibility rules the index page uses, not just by a published flag.
- Without a diversity rule, a handful of heavily tagged evergreen posts will appear under every article. Cap how often any single post can be suggested, or mix in a slot chosen from less-surfaced content.
- Retagging a large archive triggers recomputation for every affected post. Queue that work in batches so a bulk tag edit does not stall the site or the job queue.
- A deleted or unpublished post must drop out of stored relations immediately, not on the next scheduled recompute.
- Suggestion links need their own tracking or link attribution if the app measures navigation, otherwise there is no way to tell whether the block works.
- Reserve the block's height before the suggestions render so the end of the article does not jump under a reader who has just scrolled there.

## 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 published post shows relevant suggestions or nothing at all, never an empty block.
- [ ] A post never suggests itself.
- [ ] Drafts, scheduled, and access-restricted posts are absent from all suggestions.
- [ ] No single post appears in more than a capped share of suggestion blocks across the archive.
- [ ] Relations are precomputed and the article page does not run a ranking query on request.
- [ ] A bulk retag recomputes affected relations in batches without blocking publishing.
- [ ] The block occupies reserved space and does not shift the page when it loads.
- [ ] 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.
