# AI Chat Attachments

## Objective

Attach files to a conversation and let the assistant use what it is allowed to read.

File upload on an AI conversation, with parsing, retrieval of relevant sections, and per-conversation access scoping.

## 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. Reuse the app's existing upload, storage, and virus-scanning path rather than adding a second one, and declare an explicit list of accepted file types and a size ceiling per file and per conversation.
2. Parse each attachment into text and structure in a background job, store the result, and show the attachment as pending until parsing succeeds so the user is never told the assistant has read something it has not.
3. Retrieve and send only the sections relevant to the current question. Sending whole documents on every turn burns the context window and the budget for no gain.
4. Scope attachment access to the conversation and the workspace it belongs to, and re-check that scope on every read, including when an older turn is regenerated.
5. Tell the user which attachments and which sections informed each answer, using the app's existing citation surface. This entry owns upload, storage, parsing, scoping, and retrieval; hand image files to Multimodal Image Analysis rather than interpreting them here.

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

- Accepted types and size limits must be enforced on the server against the real content, not the declared extension, and every upload must pass the app's scanning step before it is readable.
- A file readable in one conversation must not become readable in another, and a user removed from the workspace must lose access immediately, including to answers being regenerated from older turns.
- An answer that does not say which files it used cannot be checked. Cite the specific attachments and sections, and say plainly when none were used.
- Sending whole files on every turn is the tempting shortcut and the wrong one: it exhausts the context window, raises cost per message, and buries the relevant passage. Retrieve the relevant sections instead.
- An attachment deleted, replaced, or failed to parse must degrade the answer honestly. Say the file is unavailable rather than answering from a stale copy or silently ignoring it.
- Encrypted, corrupt, or image-only documents will fail to yield text. Report the specific reason so the user can act, rather than a generic upload error.
- Retention must be defined: how long parsed text and derived indexes live, and whether deleting the attachment removes them too.
- A very large document must be refused with a clear limit rather than accepted and then truncated invisibly mid-way through parsing.

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

- [ ] Uploads reuse the app's existing storage and scanning path, and type and size limits are enforced server-side against real content.
- [ ] Parsing runs in the background and attachments are marked pending until they are genuinely readable.
- [ ] Only relevant sections are retrieved and sent to the model, not entire files on every turn.
- [ ] Attachment access is scoped to the conversation and workspace and re-checked on every read, including regenerations.
- [ ] Answers cite which attachments and sections were used, and state when none were.
- [ ] Deleted, replaced, and unparseable attachments produce an explicit message rather than a silently degraded answer.
- [ ] Retention of parsed text and derived indexes is defined and tied to deletion of the attachment.
- [ ] 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.
