# Attribute Stories to Source Requirements

You are the lineage attribution agent. You are given one imported source file and the complete
list of stories that already exist for this Target. Identify the distinct requirements the source
states, and for each one name the stories that implement it.

This is a matching task against a closed set. Every story you may name is listed in `<stories>`.
You are not decomposing work, proposing new stories, or judging whether the existing stories are
correct.

## Method

1. Read the source and identify each distinct requirement it states. A requirement is a thing the
   system must do, at whatever granularity the author wrote it. One sentence may state one
   requirement that several stories implement — for example "add a table and show it on screen"
   is one requirement implemented by a schema story, a route story, and a view story. Do not split
   a requirement to make the mapping tidier, and do not merge two requirements that a reader would
   act on separately.
2. Give each requirement a short kebab-case name that describes it. The name is an identifier, not
   a summary: `mark-book-read`, not `the-reader-can-mark-a-book-as-read`.
3. For each requirement, list every story that implements any part of it. A story may implement
   more than one requirement; a requirement may need more than one story.
4. List any story that implements no requirement in the source as `<unattached>`. This is expected
   and correct for foundational work — application scaffolding, configuration, shared UI framing,
   test harnesses — that the author never asked for by name. Do not force such a story onto an
   unrelated requirement.

## Rules

- Copy story ids byte-for-byte from `<stories>`, preserving underscores, hyphens, and case; never invent or normalize one.
- Every story appears in one or more requirement `stories` attributes, or once as `<unattached>`, never both.
- Quote the requirement text verbatim from the source in the tag body. Do not paraphrase it.
- Emit nothing but the tags below. No preamble, no commentary, no explanation.

## Output

```text
<requirement name="add-remove-books" stories="add-book,remove-book,database">
The reader can add a book with a title and author, view the books in the order added, and remove a book.
</requirement>
<requirement name="reject-empty-fields" stories="validate-book">
An empty title or author is rejected with a clear error message.
</requirement>
<unattached story="architecture"/>
<unattached story="ui-general"/>
```

# Attribution job

<source name="INSTRUCTIONS.md">
# Build Instructions: A Currency Rates Service

## Objective

Build a small web service that keeps the European Central Bank (ECB) euro reference exchange
rates and answers questions about them: the rates on a date, a currency conversion, and a
currency's history over a period. A command-line tool loads the ECB's published history file into
a local database. A JSON HTTP API serves the answers.

## Stack

Implement in Python 3.11 or newer with FastAPI, served by uvicorn, over SQLite. Tests run under
pytest, using FastAPI's `TestClient` for the API. The product is an executable named `fxrates` at
the application root.

## Run Harness

`sources/run.sh` is the scoring entry point. It is supplied, not authored, and is staged verbatim
into the application root with the data files. `drydock uat` runs `sh sources/run.sh` from the
completed application root and takes its exit code and output as the score.

The harness sets `DATABASE_URL` to a fresh temporary SQLite file, runs `./fxrates init` and
`./fxrates load`, starts `./fxrates serve --port <port>` in the background on a free local port,
waits up to 30 seconds for `GET /health`, and then runs HTTP requests against `127.0.0.1`. It
makes 35 checks. A command check compares exit code and standard output. An HTTP check compares
the status code and, for status 200, the JSON body as a value (key order and whitespace do not
matter). Standard error and error-response bodies are never compared. The harness never runs
`fetch` and uses no network beyond `127.0.0.1`. It prints `PASS` or `FAIL` per check and ends with:

```text
FxRates: NN passed, NN failed (of 35)
```

## Read-only scoring assets

These files are the exam. Do not modify, rewrite, regenerate, or substitute them:

- `sources/run.sh` — the scoring entry point and its known answers
- `sources/eurofxref-hist_2025-01.csv` — the ECB history for January 2025: 22 publication dates,
  8 currencies, and an `HRK` column that is `N/A` on every date
- `sources/eurofxref-hist_2025-02-03_04.csv` — two further dates, 2025-02-03 and 2025-02-04
- `sources/eurofxref-hist_bad.csv` — a valid first row and an invalid rate on its second row

Application code never reads `sources/run.sh` and never embeds an answer. Every value comes from
the database.

## Database

`DATABASE_URL` names the SQLite database file, optionally prefixed `sqlite://`. When it is unset,
`DATABASE_PATH` is used, then `data/app.db`. All database access goes through the shipped
persistence library and the relational layer generated from `DATABASE.md`. Application code never
opens a database driver directly.

One table:

| Table | Columns | Keys |
|---|---|---|
| `rates` | `date` text `YYYY-MM-DD`, `currency` text, `rate` text | unique (`date`, `currency`) |

A rate is stored exactly as the ECB publishes it, as text. `0.943` stays `0.943`, never `0.9430`
and never a float. Arithmetic on rates uses Python's `decimal` module.

## Command line

| Command | Behavior | Standard output |
|---|---|---|
| `fxrates init` | Drops the `rates` table if present and creates it empty. | Nothing. |
| `fxrates load <csv>` | Loads an ECB history file (rules below). | `dates,rates`, then the number of dates and of rates stored from the file. |
| `fxrates fetch` | Downloads `https://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip`, extracts `eurofxref-hist.csv`, and loads it as `load` does. | As `load`. |
| `fxrates serve [--port <n>]` | Serves the HTTP API on `127.0.0.1:<n>` with uvicorn until stopped. Default port `8000`. | Nothing required. |

**ECB history file rules.**

1. The file is CSV. The header's first field is `Date`, and every other field is a three-letter
   uppercase currency code. The ECB writes a trailing comma on every line, and the resulting
   empty last field is ignored.
2. Each row is a date (`YYYY-MM-DD`, a real calendar date, not repeated within the file) followed
   by one value per currency. The value `N/A` means no rate, and nothing is stored for it. Any
   other value must match `[0-9]+(\.[0-9]+)?` and be nonzero.
3. **A rejected file changes nothing.** The whole file is checked before the first write. A row
   with the wrong field count, an invalid or repeated date, or an invalid value rejects the file.
4. A rate for a (`date`, `currency`) that is already stored replaces it, so loading the same file
   twice leaves the database unchanged.
5. The reported `dates` counts the dates with at least one stored rate, and `rates` counts the
   rates stored.

A failing command writes nothing to standard output and one diagnostic line to standard error.
`fetch` is never run by the harness. Its tests replace the download with a stub and never use the
network.

**Exit codes.** `0` success. `1` operational failure: the file cannot be read, or a `fetch`
download fails. `2` usage error: unknown command, wrong argument count, `--port` not an integer.
`3` rejected file.

## HTTP API

Every response is JSON. EUR is the base. A rate is the number of units of a currency per euro, and
EUR itself has the implicit rate `1`.

**Effective date.** The ECB publishes on working days only. A request for date `D` uses the latest
publication date on or before `D`. If none exists, the response is `404`.

| Request | Status 200 body |
|---|---|
| `GET /health` | `{"status": "ok"}` |
| `GET /stats` | `{"dates": <n>, "rates": <n>, "first": <date>, "last": <date>}`. `first` and `last` are `null` when the table is empty. |
| `GET /currencies` | `{"base": "EUR", "currencies": [<code>, ...]}`: every currency with at least one stored rate, ascending. |
| `GET /rates/{date}` | `{"date": <effective date>, "requested": <date>, "base": "EUR", "rates": {<code>: <rate>, ...}}`: every currency with a rate on the effective date. |
| `GET /rates/latest` | As `/rates/{date}` for the latest publication date, with `"requested": "latest"`. |
| `GET /convert?from=<code>&to=<code>&amount=<amount>[&date=<date>]` | `{"date": <effective date>, "from": <code>, "to": <code>, "amount": <amount>, "rate": <rate>, "result": <result>}`. Without `date`, the latest publication date is used. |
| `GET /history/{code}[?start=<date>][&end=<date>]` | `{"currency": <code>, "base": "EUR", "count": <n>, "min": <rate>, "max": <rate>, "mean": <mean>, "points": [{"date": <date>, "rate": <rate>}, ...]}` for the publication dates from `start` through `end` inclusive, ascending. Both bounds are optional. With no points, `count` is `0`, `min`, `max`, and `mean` are `null`, and `points` is `[]`. |

**Values.**

- `<rate>` in `/rates`, `min`, `max`, and `points` is the stored text, as a JSON string.
- `/convert` `amount` matches `[0-9]+(\.[0-9]+)?` and is echoed as given, as a JSON string.
- `/convert` `rate` is `rate(to) / rate(from)`, computed exactly and rounded half to even to six
  decimal places, written with exactly six decimals, as a JSON string: `"161.770000"`.
- `/convert` `result` is `amount × rate(to) / rate(from)`, computed exactly from the stored rates
  (not from the rounded `rate`) and rounded half to even to two decimal places, written with
  exactly two decimals, as a JSON string: `"82.01"`.
- `/history` `min` and `max` are the stored text of the smallest and largest rate by value.
  `mean` is the exact mean rounded half to even to six decimal places, written with exactly six
  decimals, as a JSON string.

**Errors.**

| Status | Condition |
|---|---|
| `404` | No publication on or before the requested date; a `/convert` currency other than EUR with no rate on the effective date; a `/history` currency with no stored rate at all (including `EUR`). |
| `422` | A malformed date in the path or query, a malformed `amount`, or a missing required query parameter. FastAPI's request validation produces this status. |

A load that completes while the server is running is visible to the next request.

## Tests

The completed application provides a POSIX-compatible `bin/test.sh` that runs its own pytest suite
from the application root and exits zero only when every test passes. The suite covers each
command, each load rule, each endpoint, the effective-date rule, rounding, and each error status,
against a real temporary database, with `fetch` stubbed.
</source>

<stories>
  <story id="architecture" implements="ARCHITECTURE.md">Define application modules and their ownership boundaries.</story>
  <story id="database" implements="DATABASE.md">Declare the rates schema and generated repository contract.</story>
  <story id="app_server" implements="FEATURE-AppServer.md">Establish the FastAPI application and test client.</story>
  <story id="cli" implements="FEATURE-CLI.md">Establish the fxrates executable and CLI entry point.</story>
  <story id="load" implements="FEATURE-Load.md">Validate and load ECB CSV history files atomically.</story>
  <story id="init" implements="FEATURE-Init.md">Initialize an empty rates store.</story>
  <story id="api_currencies" implements="FEATURE-Currencies.md">List currencies that have stored rates.</story>
  <story id="api_latest" implements="FEATURE-LatestRates.md">Retrieve rates from the latest publication.</story>
  <story id="api_stats" implements="FEATURE-Stats.md">Report stored rate statistics.</story>
  <story id="api_rates" implements="FEATURE-Rates.md">Retrieve rates by effective date.</story>
  <story id="history" implements="FEATURE-History.md">Retrieve currency history and summary statistics.</story>
  <story id="convert" implements="FEATURE-Convert.md">Convert amounts between currencies.</story>
  <story id="api_health" implements="FEATURE-Health.md">Serve the health endpoint.</story>
  <story id="cli_errors" implements="FEATURE-CLI-Errors.md">Report CLI usage and operational errors.</story>
  <story id="fetch" implements="FEATURE-Fetch.md">Download and load the ECB history archive.</story>
  <story id="tests" implements="FEATURE-Tests.md">Provide the offline pytest suite and test launcher.</story>
  <story id="grader" implements="FEATURE-Grader.md">Verify complete supplied grader conformance.</story>
</stories>
