# The ranking model behind an endpoint, released weekly: reproduce it

The serving part of the ranking lifecycle study. The page is `serving.html`; the protocol it follows is
`protocol-serving.json`. The same model as the quarterly study (`index.html`) is served by a local HTTP endpoint,
retrained and packaged every week for 52 weeks of MovieLens 32M activity (October 2022 to September 2023), and
promoted by six declared release rules, with five planted failures.

## Check the published results with no data

```
python -m pytest tests -q -p no:cacheprovider
python check_numbers_serving.py
python check_numbers_serving.py --control
python render_serving.py
```

`tests/test_serving_replay.py` replays all 42 runs (six rules and an observe-only run, each clean and with each planted
failure) from the aggregates in `results-serving.json` and requires them to match the stored runs exactly; it also pins
the reported numbers. `check_numbers_serving.py` traces every number on `serving.html` to `results-serving.json` or the
protocol. One check in `tests/test_serving_render.py` reads a word list from a private folder that is not part of the
download, and is skipped without it.

## Run it from the ratings

Set up the environment and download the archive as in `README.md`, then run the stages in order. Each stage resumes from
its own done-markers under the work folder, so it can be stopped and started again.

```
python run_serving.py prep         # visits, labels and the weekly movie tables
python run_serving.py als          # the factor model, refit every fourth week (15 fits)
python run_serving.py rows         # the re-ranker's training rows, through the training path
python run_serving.py releases     # train, evaluate, package and register all 82 releases
python run_serving.py latency      # load-test every release through the HTTP endpoint
python run_serving.py endpoint     # the endpoint against the batch replay on 200 visits
python run_serving.py simulate     # score every (release, week) pair the rules ask for, run the rules
python run_serving.py results      # replay every decision again, write results-serving.json and registry-serving/
python render_serving.py
```

`RLC_DATA_DIR` and `RLC_WORK_DIR` choose where the download and the working files go, as in `README.md`. Nothing derived
from the ratings is written into the study folder except the aggregates in `results-serving.json` and the release
manifests.

## Files

- `serve/data.py`: the weekly calendar, visits, the label job, the weekly movie table.
- `serve/features.py`: the ten features on two paths, the serving path (one request) and the training path (in bulk).
- `serve/train.py`, `serve/pipeline.py`: factor-model fits, training rows, releases, latency, the endpoint check and the
  simulation that scores exactly the (release, week) pairs the rules need.
- `serve/package.py`: release packages with a manifest of file fingerprints, and the check that refuses a bad one.
- `serve/endpoint.py`: the HTTP endpoint (Python standard library): `POST /rank`, `GET /health`.
- `serve/sim.py`: the release rules and the monitors, as a pure function of the stored aggregates.
- `serve/results.py`: the summaries, paired bootstrap intervals and the registry index.
- `registry-serving/`: one manifest per release and an index. Model files are not published.

Latency figures depend on the machine and on what else runs on it. `results-serving.json` publishes them only as ratios
(each release to the first release at the same percentile, the in-line shadow to its champion alone); every release rule
compares ratios, so the decisions replay unchanged from them. They are never a production figure.

The design document that the code comments cite (DESIGN-serving.md) is not in the download, because it records how long
one fit took; its constants are all in `protocol-serving.json`, and `serving.html` describes what it declared. MovieLens 32M is used under the GroupLens research licence; see `NOTICE.txt`.
