# Design: product event contracts before release

Written and committed before any result was computed. Anything settled after a result was read is listed on the page
under "Added later, and why" and never changes a number declared here.

## Question

When a release changes how a product emits analytics events, how many metric-breaking changes does a pre-release
contract check catch, against monitoring after release, and how many days of wrong metrics does each leave?

The published data quality scorecard asks whether checks on a daily load catch a broken feed after it lands. This study
asks the earlier question, about the product emitting the evidence: which release changes can be stopped before they
ship, which only show up after, and which no check here sees. The data, the faults and the checks are different, and the
monitoring arm stands in for the scorecard kind of check rather than re-measuring it.

## Data: explicitly synthetic, fixed seeds

A small subscription workout app. The generator (`ec/world.py`) holds the true behaviour behind every event; the
emitter (`ec/emit.py`) turns true actions into the events a build sends.

- 280 days, after 90 days of history so that day 0 is in steady state. New installs a day: Poisson with mean 300, times a weekday factor (Saturday and Sunday 0.85, Monday 1.1).
- Each install has an engagement level (Beta 2, 3), a platform (iOS 55%, Android 45%) and a lifetime of at most 90 days.
  Chance of being active on a day: 0.85 times engagement times exp(minus age over 21 days), at least 0.9 on install day.
- An active day has 1 session, or 2 with chance 0.35. On install day, the first session may show the onboarding
  paywall (chance 0.5, before signup), then signup (chance 0.65 on install day, 0.08 a day afterwards). Method: email
  40%, Apple 35%, Google 25%. Apple signups have engagement raised by 0.1 (capped at 1).
- Signed-up users start a workout on an active day with chance 0.25 plus 0.6 times engagement, and finish it with
  chance 0.8. A workout is done offline with chance 0.2: its events queue on the device and upload when the phone
  reconnects in the background (chance 0.6, 0.5 to 3 hours later) or else at the next session; a user who never
  opens the app again loses the queue.
- Signed-up non-subscribers see the paywall on an active day with chance 0.14 (from home, workout end or settings) and
  buy with chance 0.13: monthly US$12.99 (65%) or annual US$79.99 (35%). A purchase request times out and is retried
  with chance 0.08. Payment settles after an exponential delay with mean 4 hours.
- Online events reach the server 0.3 to 4 seconds after the action; events sent by the server, 0.05 to 0.5 seconds.

Seeds 1 to 5. Every random draw comes from a stream keyed by the seed and the day, so a change applied from a release
day leaves every earlier day byte-identical.

### Tracking plan and metrics

The tracking plan (`ec/plan.py`, `plan/tracking-plan.json`) lists six events, their required properties, types,
enumerations and ranges, the identity rules and the screens. The four metrics a product team reads, each computed the
same way on the events and on the true actions:

- **Active users**, a day: distinct people with a `session_started`. Used as the base for revenue per user.
- **Activation**, per signup day: share of the day's signups with a `workout_completed` within 72 hours of signup.
- **Paywall conversion**, a day: share of the day's paywall viewers who bought the same day (the same person on both
  events, after identity is resolved).
- **Week-1 retention**, per signup day: share of the day's signups with a session on day 7.
- **Revenue per active user**, a day: the day's purchase revenue over active users.

Identity: events before signup carry an anonymous id; the signup event carries both ids and links them; later events
carry the user id. The warehouse keeps one typed column per property, so a value of another type lands as null in the
metric's column. Events are deduplicated by event id when they land and are dated by their event time.

A metric is **wrong** on a day when it differs from the truth by more than its tolerance: 2% relative, or the 99th
percentile of the clean build's own daily error on that metric over the 42 warm-up days, whichever is larger. A change
is **metric-breaking** when, left alone, it makes at least 3 metric-days wrong in the 28 days from its release (mean over
the 5 seeds). Metric-days count each of the five series (active users, activation, conversion, retention, revenue per
active user) on each data day.

## Releases

42 warm-up days, then 30 releases, one every 7 days (release k on day 35 + 7k). Each release carries one tracking
change, scored on its own against the clean build, so changes never mask one another. This is a declared assumption:
real releases bundle changes. A build reaches users gradually (30% on release day, 50% after 1 day, 75% after 3, 90%
after 7, 97% after 14; installs after the release get it at once). Server and remote-setting changes reach everyone on
the day.

The 30 declared changes are in `changes/declared.json` with my prediction for each: whether it breaks a metric, and
which arms I expect to raise an alarm (`contract`, `monitoring`, both, `neither` for a break no arm 2 or 3 check sees,
`none` for a harmless change that should raise nothing). An alarm on a harmless change is reported as such.
A held-out set of 12 changes is written by an independent author who never saw the checks (below).

## Arms

1. **No contract, watch dashboards.** Someone notices when a metric moves more than 30% from its mean over the
   previous 28 readable days.
2. **Monitoring after release.** Daily checks on the landed data, run the morning after each data day on events
   received by then (activation is readable 4 days after the signup day, retention 8):
   - schema: per planned event, more than 1% of the day's events (and at least 5) failing a required property, type,
     enumeration or range; or an unplanned event name with at least 20 events. Unplanned properties are ignored.
   - volume: per planned event, the day's count under 0.7 or over 1.4 times the mean of the same weekday over the
     previous 4 weeks (when that mean is at least 20).
   - metrics: a metric more than 3 standard deviations from its mean over the previous 28 readable days.
3. **Pre-release contract.** Run on every build and server release (a remote setting ships without a release, so it is
   never tested). The tracking plan is a strict machine-checked schema (unplanned properties fail), plus three scripted
   sessions replayed against the build and a staging server on a good network:
   - S1, iOS, email signup: session, onboarding paywall, signup, workout started and finished, paywall, monthly purchase.
   - S2, iOS, signed-up subscriber: session, workout started and finished, back to the background and returning.
   - S3, Android, anonymous: session, onboarding paywall, leaves.
   The test asserts that every event passes the schema; that each script emits exactly the planned events, each the
   planned number of times; that ids follow the identity rules (the signup event carries the device's anonymous id,
   later events and server events carry the same user id); that event time is within 5 seconds of the action; and that
   every screen in the build has a tracking plan entry. Staging settles payments at once.
4. **Both** (3, then 2 for what ships).

Every arm also keeps the dashboard reader of arm 1 after release, so arm 3 alone is the contract, then dashboards.
5. **Both, plus semantic checks.** Declared now as the arm that targets what a schema cannot see; it is a self-test
   for the declared changes, because I wrote both, and the held-out set is its real test.
   - The contract replays every script on both platforms and with each signup method, plus two more scripts: a
     purchase whose first request times out and is retried (exactly one purchase must land), and a workout done
     offline and uploaded 3 hours later (it must land once, with the action's time).
   - Landed checks: more than 1% of a day's purchases (and at least 2) sharing a transaction id; for workouts, paywall
     views and purchases, the share of the day's people who also have a session that day falling more than 5 points
     under its mean over the previous 28 days; for each event, the share arriving less than a second after its event
     time rising more than 15 points over its mean over the previous 28 days.

**Found.** A pre-release catch means the change never ships: no wrong day. After release, a change is found on the
first day an alarm fires on an event or metric the change affected: an event whose landed count over the 28 days
differs from the clean build by more than 1%, whose schema failures, identity linkage share or fast-arrival share
differ by more than 0.5 points, a new event name it introduced, or a metric it made wrong. For a real behaviour change,
the affected metrics are those whose true value moved by more than the tolerance. Alarms on anything
else are false alarms, counted apart. An alarm is assumed to lead the team to the change the same day, which is
generous to monitoring.

## Measures

Per change, arm and seed: caught before release, found after (days after the release) or not found in 28 days; wrong
metric-days on data days before it was found (28 when never found); alarms on changes that broke no metric; and false
alarms on the 42 clean warm-up days and on unaffected elements. Reported per change type and per arm, never pooled across
the declared and held-out sets.

Where an arm catches a change by construction (a renamed event fails a schema that lists event names), the page says
so. The measured part is what each arm lets through, how long monitoring takes, and the damage meanwhile.

## Sensitivity (declared)

On all seeds: the volume band (0.8 to 1.25, 0.7 to 1.4, 0.6 to 1.6), the metric band (2, 3, 4 standard deviations),
the tolerance floor (1%, 2%, 5%) and the build adoption curve (twice as fast, as declared, twice as slow). On seeds 1
and 2: the app three times as large (900 installs a day). A wrong metric-day is valued in the money section only as a
breakeven against the contract's declared cost, never as a measured saving.

## Held-out changes

An independent author who never saw the checks, the arms, the thresholds or my 30 changes gets only the app
description, the tracking plan, the metric definitions, the network facts, the release channels and the change grammar
(`ec/grammar.md`), and writes 12 changes as a team might ship them, with a description and its own expectation for
each. It runs once, read-only, from a clean folder holding only those files; inputs and outputs are hashed. Its changes
are scored by the same code as mine. Any change it writes that the grammar cannot express is reported, not dropped.

## Predictions (declared)

- The contract catches every declared change that a schema, the scripts' event counts, the identity rules or the screen
  list can see, and none of the retry, offline, platform, sampling or settlement changes.
- Monitoring finds the schema-visible breaks the day after release and misses the retry duplicate.
- Changes neither arm 2 nor arm 3 sees: at least 3 of the declared breaking changes.
- Monitoring raises an alarm on at least 2 of the 3 real behaviour changes, which broke no metric.
- On the held-out set the contract catches a smaller share of the breaking changes than on mine.

## What would change the conclusion

If monitoring found most semantic breaks within 2 days, the case for a pre-release contract would rest only on the
schema-visible changes and the days they cost. If the held-out set's breaking changes were mostly schema-visible, the
contract's share would rise. If the larger app's monitoring found subtle breaks within a few days, the result would be
about app size rather than check placement.
