# Cache Invalidation Controls

## Objective

Clear stale cached data without flushing everything or restarting.

An operator control that invalidates cache entries by namespace, tenant, or resource — scoped, propagated across processes, and recorded.

## 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. Define named invalidation scopes rather than free-text key entry. An operator picks a namespace, a tenant, or a specific resource; they do not type raw cache keys.
2. Enforce tenancy on every scope. One workspace's operator must be structurally incapable of clearing another workspace's entries — check the scope against their permissions before building the key pattern, not after.
3. Propagate invalidation to every process and region that holds a copy. In-process and edge caches will not hear about a change made in a shared store unless you tell them, and a clear that only affects one server is worse than no clear because it looks like it worked.
4. Report completion honestly. Show the operator when the invalidation has been acknowledged everywhere, and show it as pending until then.
5. Record every manual clear: who, what scope, when, and how many entries were affected. Manual cache clears are a common step in incidents and need to appear in the timeline.
6. Do NOT expose a global flush-everything button to ordinary operators. A full flush of a warm cache stampedes the database at exactly the moment the system is already unhealthy.

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

- Clearing a hot key causes a thundering herd of identical recomputes. Rebuild behind a single-flight lock rather than letting every request recompute.
- A clear issued while a slow write is in flight can be immediately re-poisoned by the older value finishing last. Invalidate after the write commits, not before.
- Wildcard or prefix scans are expensive or unsupported on some cache backends. Prefer versioned namespace keys so invalidation is a single increment.
- If the cache backend is unreachable, the control must fail loudly. A clear that silently did nothing is the worst possible outcome.
- Repeated clears of the same scope in quick succession should be rate-limited — an anxious operator clicking during an incident can keep the cache permanently cold.
- Derived and dependent caches must be considered. Clearing a record often requires clearing the lists and counts that included it.
- The audit record must name the scope, never dump the cached values, which may contain personal data.

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

- [ ] Invalidation is issued by named scope, never by free-text key.
- [ ] A scope outside the operator's permissions is rejected before any keys are touched.
- [ ] Invalidation reaches every process and region, and the UI shows pending until acknowledged.
- [ ] Every manual clear is recorded with actor, scope, time, and effect.
- [ ] Rebuilds after a clear are single-flighted rather than stampeding.
- [ ] An unreachable cache backend produces a visible error, not a false success.
- [ ] Global flush requires elevated permission and an explicit confirmation.
- [ ] 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.
