# Prompt Variables

## Objective

Feed app data into prompts through declared, typed variables rather than string assembly.

A typed variable layer for prompt templates, with declarations, validation, escaping, and a preview of the rendered prompt.

## 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. Give every template an explicit declaration of the values it takes, each with a name, a type, and whether it is required, optional, secret, or a list. A template that silently accepts anything cannot be validated or previewed.
2. Validate every value against its declaration before rendering: type, length ceiling, and list size. Reject rather than truncate, since a value cut in half changes the meaning of the instruction that contains it.
3. Wrap substituted values in clear delimiters and neutralise anything that looks like an instruction, so text drawn from a record cannot redirect the model. Treat every value sourced from user input, uploaded files, or third-party records as hostile.
4. Give authors a preview of the rendered prompt with the task content and substituted values visible and the system portion withheld. Storage, scoping, and ownership of the templates themselves belong to Prompt Template Library; this feature owns declaration, substitution, and validation only.
5. Do not let a missing optional value collapse into an empty string inside a sentence. Define what the template says when a value is absent, or the model reads a truncated instruction as a complete one.

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

- Values coming from records, uploads, or third-party data must be delimited and escaped before substitution. Text that reads as a new instruction will be followed, and this is the most common way an app's own data attacks its prompts.
- The declaration must distinguish required, optional, secret, and list variables, because each needs different handling at validation, preview, and logging time.
- Type and length must be checked before rendering, so an oversized value fails loudly instead of consuming the context window and pushing the real instructions out of scope.
- The preview must show what will actually be substituted while withholding hidden system instructions, so an author can debug a template without being handed the safety rules to edit around.
- A missing value must never render as an empty gap in a sentence. Either block the run or substitute an explicit statement that the value is unavailable.
- Secret values must be excluded from previews, logs, error messages, and stored request records, and must never be placed in client-visible code or a template body a user can read back.
- A list variable of unbounded length must be capped, with the truncation stated in the prompt itself rather than performed silently.
- Values containing the delimiter characters must be handled so they cannot close the wrapper early and escape into the instruction body.

## 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 template declares its variables with a name, a type, and required, optional, secret, or list status.
- [ ] Values are validated for type, length, and list size before any request is made, and invalid input is rejected rather than truncated.
- [ ] Substituted values are delimited and neutralised so embedded instructions are not followed.
- [ ] Authors can preview the rendered prompt without seeing or editing system instructions.
- [ ] Missing values block the run or render an explicit unavailable statement, never a silent gap.
- [ ] Secret values never appear in previews, logs, error output, or stored requests.
- [ ] 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.
