# Contextual Page Copilot

## Objective

Let the assistant see the record and screen the user is on before it answers.

A context layer that assembles a permission-filtered, compact description of the current route, record, and available actions and hands it to the assistant.

## 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 context by re-reading the record through the app's normal authorization path, not from what the client sent. The copilot must never see a field the signed-in user could not open directly.
2. Send a compact representation rather than the whole record: the fields shown on screen, the identifiers, the recent history entries, and counts in place of long collections. Do not attach entire attachment bodies or full audit logs.
3. Rebuild the context when the route, the record, the workspace, or the user's permissions change, and discard the previous one. A copilot answering about the record the user left is worse than one that admits it has nothing.
4. Split the copilot's replies into advice, which is free, and actions, which are presented as a proposal the user approves before anything changes.
5. The conversation surface, message storage, streaming, and cost ceilings belong to AI Chat Assistant. This feature owns only the assembly and freshness of the context passed into it.

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

- Context must be filtered by the current user's permissions, not the record owner's. A shared record that hides certain fields from this viewer must hide them from the copilot too.
- Navigating between records without a full page load must invalidate the context. If it does not, the copilot silently answers about the previous record and sounds confident doing it.
- Large records will blow the token budget. Truncate deliberately, in a documented order, and say in the reply that the view was partial rather than quietly dropping half the data.
- Read-only advice and state-changing actions must be visually and structurally separate. An action must never fire as a side effect of the model mentioning it.
- When the context could not be fully assembled — a slow related query, a permission check that failed, a record still loading — tell the user the copilot is working from partial information instead of pretending otherwise.
- Permissions revoked mid-session must take effect on the next turn, not on the next page load.
- A record deleted while the conversation is open must produce a clear statement that it is gone, not answers drawn from the stale copy in context.

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

- [ ] Context is assembled server-side from an authorized read and contains no field the viewer cannot see.
- [ ] Changing route, record, or workspace replaces the context before the next turn is sent.
- [ ] Records are summarised into a bounded representation rather than serialized in full.
- [ ] Actions are always proposals requiring explicit approval, and are distinct from advice in the interface.
- [ ] Incomplete or stale context is disclosed to the user in the reply.
- [ ] Conversation storage and streaming are delegated to AI Chat Assistant rather than reimplemented.
- [ ] 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.
