# Client Version Compatibility

## Objective

Handle the tab that has been open since two deploys ago.

The server tells the client which build it expects, and the client responds — quietly for an optional update, insistently when the old code can no longer talk to the API.

## 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. Stamp every build with a version identifier and return it on API responses, either in a header or a small metadata endpoint. The client compares it against the version it was built as.
2. Separate two cases and treat them differently. A newer build being available is an optional update — offer it and let the user finish what they are doing. An API contract the running client cannot satisfy is a hard incompatibility and must block further writes.
3. For an optional update, show an unobtrusive, dismissible prompt and apply the new version at the next natural navigation. Do not interrupt.
4. For a hard incompatibility, block with an explanation of what happened and a reload action. Say the app updated; do not show a version string or an API error as the primary message.
5. Never reload while unsaved work exists. Detect dirty forms and in-flight requests, tell the user what will be lost, and give them a chance to save or copy it out.
6. Do NOT reload automatically on a timer or on a failed request. A reload loop against a stale asset host is indistinguishable from an outage to the user.
7. Data-age display, freshness labels, and manual data refresh are owned by Stale Data Indicator. This feature is about the code being old, not the data — do not build a second freshness surface.

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

- Several tabs may run different versions. Broadcast the detection between them so the user is not prompted separately in each, and do not reload a background tab silently.
- A cached HTML shell or service worker can serve the old build after a reload, producing a loop. Bound the reload attempts, and clear or update the service worker before reloading.
- A rollback means the server version goes backwards. The client must treat a mismatch as a mismatch in either direction rather than assuming newer is always ahead.
- During a rolling deploy, requests alternate between old and new servers. Do not raise a hard incompatibility on a single mismatched response — require it to persist.
- Version checks must not add a request per API call. Read the header already coming back on normal traffic rather than polling.
- The check itself failing — offline, blocked, no header — must be silent. An unknown version is not an incompatible version.
- A user who dismisses an optional update must not be re-prompted every few minutes; back off, and only escalate if the update becomes mandatory.

## 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 build carries a version identifier the client can compare against the server's.
- [ ] Optional updates prompt unobtrusively and can be dismissed; breaking incompatibility blocks writes with an explanation.
- [ ] No reload occurs while unsaved work or in-flight requests exist without a warning.
- [ ] Multiple tabs coordinate detection and background tabs are not reloaded silently.
- [ ] Reload attempts are bounded and stale service workers are cleared before reloading, so no loop occurs.
- [ ] A rolling deploy does not trigger a false hard incompatibility.
- [ ] A failed or unavailable version check is silent to the user.
- [ ] 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.
