# Render Stability Guardrails

## Objective

Make render optimisation provable, so it removes work instead of introducing stale UI.

A set of rules and checks governing memoisation, keys, and render boundaries on the app's busiest views.

## 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. Profile a real interaction on a real view before changing anything, and write down the cost being removed. An optimisation with no measured baseline cannot be shown to have worked and cannot be safely reverted.
2. Require every cached callback and derived value to declare each value it reads, and treat a missing declaration as a defect — a handler holding last render's values will act on data the user can no longer see.
3. Derive list keys from the stable identity of the record. A position-based key silently reuses the wrong element as soon as the list is sorted, filtered, or has an item removed from the middle.
4. Exercise the app under the framework's development double-render and its concurrent scheduling behaviour, because side effects performed during render and effects that assume they run exactly once only misbehave there.
5. Do not memoise every component as a matter of course. Comparison is not free, a component receiving a freshly built object or inline function on each render is never actually skipped, and blanket caching hides the one place that was genuinely slow.

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

- An optimisation added without a measurement is indistinguishable from a regression. Keep the before-and-after profile with the change so the next person can tell which it was.
- A cached handler that captured an earlier value will submit stale form data, act on a record the user already deleted, or ignore a filter that has since changed. Verify handlers against changing state, not just against a first render.
- Keys must be stable across reorders and meaningful across renders. Reusing a position as a key will leave typed input, selection, and scroll position attached to the wrong row.
- Development double-rendering and concurrent scheduling will run effects and render bodies more than once. Subscriptions, timers, and analytics events must tolerate that without duplicating, and cleanup must genuinely undo setup.
- Memoisation is defeated by props rebuilt on each render — objects, arrays, and inline functions declared in the parent. Confirm the boundary actually skips work rather than assuming it does.
- Deriving state from props into local state creates two sources of truth that drift; prefer computing during render over copying.
- Cell and row components inside a windowed list are the usual subject here — memoisation of those belongs to this brief, while the windowing itself belongs to the Windowed Grid Primitive.

## 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 memoisation in the codebase has a recorded measurement showing what it saves.
- [ ] Cached callbacks and derived values declare all the values they read.
- [ ] No list key is derived from array position on a list that can reorder, filter, or delete.
- [ ] The app behaves correctly under development double-rendering and concurrent scheduling, with no duplicated subscriptions, timers, or events.
- [ ] Memoised boundaries verifiably skip work rather than being defeated by freshly constructed props.
- [ ] No component copies props into local state where a derived value would do.
- [ ] 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.
