# Product event contracts before release

A synthetic subscription workout app whose true behaviour is known, a tracking plan, and release changes to how the
app and its servers send analytics events. The study asks which metric-breaking changes a contract checked before
release stops, which monitoring after release finds and how fast, and how many wrong metric-days each leaves. The page
is `index.html`; the design, written before any result, is `DESIGN.md`.

## What is here

- `ec/world.py`: the app's true behaviour on fixed seeds (installs, sessions, signups, workouts, paywall views,
  purchases, offline use, retries, payment settlement).
- `ec/emit.py`: turns true actions into the events a build sends under one release change; `ec/grammar.md` is the
  change grammar and `plan/tracking-plan.json` (from `ec/plan.py`) the tracking plan.
- `changes/declared.json`: the study's own 30 changes, with its prediction for each. `changes/heldout.json`: 12 changes
  written by an independent author who never saw the checks, kept as written.
- `ec/checks.py`: the contract (plain and extended scripts), the landed monitors, the semantic checks and the
  dashboard reader. `ec/measure.py`: daily aggregates and the five metrics on events and on the truth.
- `ec/run.py`: runs every change through every arm, per seed and variant, into `results/stages/`.
  `ec/summarize.py` builds `results/results.json`; `ec/report.py` builds the page and `results/page-numbers.json`.
- `tests/`: unit tests with a planted fault for every check, a seeded end-to-end run on a small world, and a replay
  that rebuilds the results and the page from the stage files and compares bytes.
- `tools/check_numbers.py`: every number a reader sees on the page must come from `results/results.json`.

## Reproduce

Python 3.12, standard library only; the tests need pytest.

```
EC_CACHE=cache python -B -m ec.run --prep   # clean builds for the chunked variants
python -B -m ec.run --only fast
EC_CACHE=cache python -B -m ec.run          # base, slow adoption and the larger app, in chunks
python -B -m ec.chance                      # the contract over 200 other sets of script ids
python -B -m ec.summarize && python -B -m ec.report
python -B -m pytest tests -q
python -I tools/check_numbers.py
```

Each job writes its stage file only when it finishes, so a stopped run resumes where it left off. The tests replay
the committed stage files and need no simulation run.

## Design summary

- 42 clean days, then 30 weekly releases, each carrying one tracking change scored on its own against the clean
  build, in 5 runs. A build reaches users gradually; server releases and remote settings reach everyone at once.
- Five ways of checking: dashboards only; monitoring after release (schema, volume and metric checks on landed data);
  a contract before release (strict schema plus three scripted sessions with identity and time assertions and the
  screen list); both; both plus semantic checks (scripts on every platform and signup method, a retried purchase and
  an offline workout, plus duplicate transaction ids, identity links and arrival times on landed data).
- A metric-day is wrong when the metric is more than 2% from the truth. A change found after release counts the
  wrong metric-days before the find; a change stopped before release counts none.

## Licence

The data is synthetic. The code may be reused. The fonts and design system under `vendor/` carry their own licences.

## The second round: a website's data layer and tags

The same question on a website, in a real browser. The page is `web-data-layer.html`; the design, written before any
release was scored, is `DESIGN-web.md`. It reuses the first round's way of scoring and none of its app, changes or results.
Readings added after the results are computed by `web/summarize.py`: a later fix, monitoring without the dashboard
reader, the floor, exact intervals and the money range (declared in `DESIGN-web-after.md`); each declared setting's days to a
find, with and without the dashboard reader, and the locked form's pipeline against margin; and runs by how many breaks
monitoring leaves unfound. None of them changes a scored number. Two design files for these later readings are not in the
download.

- `web/site/`: Fernhollow, a small fictional B2B site with a data layer, a consent banner, a tag layer and two forms. In the
  download its HTML pages end in `.html.txt`, because their links are root-relative; `web/build.py` reads them under their real names.
- `web/FRAMEWORK.md`: the measurement framework, event taxonomy and data layer specification, with schemas in
  `web/site/spec/`. `web/RELEASE_FORMAT.md`: how a release is written as code patches.
- `web/releases/declared.json`: the study's own 27 releases, with its predictions. `web/releases/heldout.json`: 12 written by an
  independent author who never saw the checks, kept as written. That author's run cannot be repeated from these files;
  everything after it can.
- `web/browser.py`, `web/run_traces.py`: run every scripted visit of a build in headless Chromium, into `results/web/traces/`.
- `web/checks.py`: the data layer check and the full browser check. `web/measure.py`: truth and metrics.
  `web/world.py`: how many visitors come. `web/score.py`, `web/summarize.py`, `web/example.py`, `web/report.py`: stages,
  results, the worked example and the page.

The browser runs need the packages in `web/requirements.txt` and a Chromium from `python -m playwright install chromium`
(set `WEB_CHROME` to use another binary). Scoring needs jsonschema and no browser.

```
python -B -m web.run_traces --workers 4 --only clean,clean-repeat,H01   # a chunk; resumes from what is stored
python -B -m web.score
python -B -m web.example
python -B -m web.summarize
python -B -m web.report
python -I tools/check_numbers.py --page web-data-layer.html --results results/web/results.json --claims results/web/page-numbers.json
python -B -m pytest -q tests
```

One test reads a private list of ruled-off characters and skips in this download; every other test runs from it.

Some files are packed in event-contracts-data-1.zip; unzip it here to restore the full export, which the manifest lists file by file.
