# ARR reconciliation

A small dbt project on DuckDB that builds one ARR number for a software company selling three ways:
free workspaces, self-serve seat plans billed by card with metered usage, and sales-led contracts
invoiced from the CRM. It defines ARR on one page, builds it with two components (contracted recurring
and usage run rate) and three motions, explains every month with a bridge and a walk to recognised
revenue, and reconciles card and usage balances to the documents. Its tests are then run against seven
planted faults, each written down with a prediction before anything ran. The same author wrote the checks
and the faults, so the catch count is a self-test.

A second page, `price-realization.html`, uses the same company to measure price realization (list
price against what customers paid, with a discount waterfall) and net revenue retention (a bridge for
the customers present at the start of a year, built from the same monthly ARR bridge).

A third page, `blind-faults.html`, runs the same frozen checks against 12 faults written by an independent author who
never saw them: it was given only the definition, the source schemas, a description of the systems and 20 sample rows
per table. The page reports how many were caught and which kinds slipped through.

A fourth page, `independent-checks.html`, adds checks built on records the pipeline never writes (signed order forms,
the rate card and discount approvals, the coupon registry, raw product telemetry) and on last month's account links. It
scores them on the blind round's five misses (a self-test, built after seeing them), counts their false alarms on six
clean companies, and tests them, beside the original checks, on 12 new faults the same model wrote blind.

A fifth page, `payment-rails.html`, follows the money past billing. It adds explicitly synthetic card processor records
(captures, settlements, fees, refunds, disputes, payouts, currency conversion), a bank statement and a general ledger
posted by written rules, then five checks a finance team runs at a month-end close: a three-way match from invoice to
capture to settlement, payout reconciliation, cash application for bank transfers, month-end cut-off and a ledger tie-out
by account. It scores them on eight declared faults (a self-test), for false alarms on six clean companies, and on 10
faults between the processor, the bank and the books that the same second model wrote blind, beside the study's existing
checks.

A sixth page, `lifecycle-metrics.html`, goes upstream of revenue. It adds explicitly synthetic product activity (free-day
runs, failed runs, support tickets), chooses an activation metric among four candidates by a rule written before the
results, on sign-up cohorts the choice never saw, and reports time to first payment and to churn, logo against
usage-weighted retention, four guardrails tested on two planted onboarding changes and on no-change splits, and the
price realization page's retention bridge split by motion. `METRIC_TREE.md` defines each metric.

A seventh page, `price-waterfall.html`, follows list price to recognised revenue. It walks the price realization page's
realized value to the study's recognised revenue by motion and segment (onboarding fees, straight-lining and timing,
tied to the study's own revenue walk to the cent), splits the lifecycle page's retention bridge by motion into price and
volume by cause (seats, ramp steps, list changes at renewal, moves to a contract), and counts renewal decisions by
discount depth, with a sweep of 200 generated companies per planted gap to show how many decisions a link would need.

An eighth page, `bordereaux.html`, runs the same method on a different kind of record. A synthetic book for an MGA (a managing general agent, which issues
policies for an insurer) sends monthly reports called bordereaux and converts once from an old system to a new one. A small dbt project and 40
declared checks in seven families are scored on false alarms on six clean books, on eight faults from the same author as the checks (a self-test), and on 11 faults
an independent author who never saw the checks wrote blind (12 written, 11 applied: one could not be applied to this book). It is insurance-shaped data: it shows a method and is not insurance experience.

It is for finance and data leads who own revenue reporting, and for analytics engineers who build it.

## What is synthetic

Everything. Northwind Workspace, its workspaces, customers, contracts, invoices, usage and every fault
are generated by `warehouse/generate.py` and `warehouse/faults.py`. No real company, customer or
employer schema, naming or data is used. The growth, churn, price and billing rates are assumptions,
so every figure describes the generator, not a market.

## What to read

- `index.html`: the study, with the answer first and the evidence under it.
- `ARR_DEFINITION.md`: the definition, one page.
- `DESIGN.md`: the protocol (sources, definition, checks, faults with predictions, scoring, limits),
  written before any model or fault code. A test fails on any edit to it; later corrections are
  labelled amendments at the end.
- `docs/models.md`: the data dictionary, generated from the dbt YAML.
- `results.json`: every scenario's test outcomes, the clean marts and the comparison with the
  generator's independent truth (the straight-line rule). `results-v1.json`: the same for the billing-period rule (each charge over its own period, fees on their day), frozen.
- `price-realization.html`: price realization and net revenue retention. `DESIGN-realization.md` is
  its protocol, written before its models, checks and planted faults; `results-realization.json` holds
  its results, every check's outcome on clean data and on each planted fault.
- `DESIGN-sensitivity.md`: the protocol for one sweep, written before its code: naive ARR against
  defined ARR on twelve billing mixes and five seeds of the same generator. `results-sensitivity.json`
  holds every company's monthly figures and the ranges the entry page reports.
- `DESIGN-shortcut.md`: the protocol for one comparison added after the results: contracted ARR against invoice lines
  read by their service period. `results-shortcut.json` holds every month's figures and the range the entry page
  reports; `shortcut/run.py` computes it from the generator, with no dbt run.
- `results-blind-faults.json`: the blind round's results without its run record (per fault: its kind, caught or
  missed, the tests that failed, the effect on ARR), projected from the full results file after the results by
  `blind/public_results.py`. The entry page reads it.
- `blind-faults.html`: the blind round. `blind/author-prompt.txt` is what the author was asked,
  `blind/author-specs.json` its 12 specs verbatim and `blind/JUDGEMENTS.md` the readings taken while applying them. The
  round's design file and run record are kept out of these downloads, so the round cannot be re-run from them alone.
- `independent-checks.html`: the independent checks. `independent/author-prompt.txt`, `independent/author-specs.json`
  and `independent/JUDGEMENTS.md` are the held-out prompt, specs and readings. The round's design file and run record
  are kept out of these downloads, so the round cannot be re-run from them alone.
- `payment-rails.html`: the payment rails. `rails/author-prompt.txt`, `rails/author-specs.json` and
  `rails/JUDGEMENTS.md` are the held-out prompt, specs and readings. The round's design file and run record are kept out
  of these downloads, so the round cannot be re-run from them alone.
- `lifecycle-metrics.html`: lifecycle metrics. `DESIGN-lifecycle.md` is its protocol, written before any code or
  outcome, with four labelled amendments (the first before any check ran, the other three after the results);
  `results-lifecycle.json` holds every check's outcome on clean data and on each planted fault, the analysis, the 100
  event seeds and the figures decided after the results. `METRIC_TREE.md` is the metric tree with definitions, grains,
  the models that build each metric and its guardrails.
- `price-waterfall.html`: the price waterfall. `DESIGN-pricing.md` is its protocol, written before any code or outcome,
  with one labelled amendment made before any stage ran; `results-pricing.json` holds every check's outcome on clean
  data and on each planted fault, the analysis, the scored predictions and every simulated company of the sweep.
  `results-pricing-trace.json` is the one contract the page traces through the walk and the split, added after the
  results by `site/trace_pricing.py` from the clean run's database; it changes nothing in the declared run.
- `bordereaux.html`: the bordereaux round. `DESIGN-bordereaux.md` is its protocol, written before any code or outcome, with the frozen code hashed in section 11;
  `results-bordereaux.json` holds every check's outcome on clean data and on each fault, and the scored predictions. `results-bordereaux-trace.json` is the worked example's
  records and each group's closes, added after the results by `site/trace_bordereaux.py`. `bordereaux/readings-after-results.json`, written by
  `site/readings_bordereaux.py`, holds what was added after the results: the split by area, each fault's cover sheet moves, the checks that fired on no fault, and four rules built for
  two misses, which count toward no score. `bordereaux/author-prompt.txt`, `bordereaux/author-specs.json` and `bordereaux/JUDGEMENTS.md` are the blind prompt, specs and readings.

## Layout

- `warehouse/generate.py`: seeded generator for the five source systems' extracts, and the true ARR,
  revenue and naive ARR computed separately from the warehouse.
- `warehouse/load.py`: lands the extracts as text in DuckDB (`raw` schema).
- `dbt/`: staging, intermediate (identity bridge, rebuilt billing schedules, daily ARR spine, usage
  run rate) and marts (`fct_arr_monthly`, `fct_arr_bridge`, `fct_revenue_monthly`,
  `fct_deferred_rollforward`, `fct_contract_balances`, `fct_arr_to_revenue`, `fct_usage_restatements`,
  `fct_naive_vs_defined_arr`, `dim_identity_bridge`), with generic and singular tests.
- `warehouse/faults.py`, `warehouse/run.py`: the declared faults and the runner that writes `results.json`.
- `site/render.py`: builds `index.html` and `docs/models.md` with the vendored design system 2.3.0.
- `realization/`: a second dbt project (`realization/dbt`) that reads the main warehouse and builds
  price realization (`fct_price_realization`), the retention bridge (`fct_nrr_bridge`) and NRR by
  window (`fct_nrr_windows`), with its tests; `truth.py` computes the same figures from the
  generator's records, `controls.py` declares the planted faults, and `run.py` writes
  `results-realization.json`. It is separate so the main project's code and tests, and so
  `results.json`, stay exactly as they were.
- `sensitivity/sweep.py`: builds each company of the sweep with the generator and writes
  `results-sensitivity.json`. It runs no dbt and edits nothing in `warehouse/` or `dbt/`.
- `blind/`: the blind round: the bundle builder and its file-list check (`bundle.py`, with the schema and systems notes
  in `blind/bundle/`), the frozen-check hashes (`freeze.py`), the recovery of the author's answer (`recover.py`), the
  12 faults as code (`faults.py`). It uses the main study's runner and checks unchanged.
- `independent/`: the independent checks: the outside records (`sources.py`, a new generator entry point with its own
  random draws), the six check families (`checks.py`), the held-out bundle and its file-list check (`bundle.py`, with
  `bundle/OUTSIDE_RECORDS.md`), recovery (`recover.py`), and the 12 held-out faults (`faults.py`).
- `rails/`: the payment rails: the processor and bank records (`events.py`, a new generator entry point with its own
  random draws), the ledger and its posting rules (`ledger.py`), the five check families (`checks.py`), the held-out
  bundle and its file-list check (`bundle.py`, with `bundle/RAILS.md`), recovery (`recover.py`), the declared and
  held-out faults (`faults.py`).
- `lifecycle/`: the lifecycle round: the product activity (`events.py`, a new generator entry point with its own random
  draws, and `facts.py`, the workspace facts it reads), a third dbt project (`lifecycle/dbt`: `fct_workspace_lifecycle`,
  `fct_lifecycle_monthly`, `fct_nrr_bridge_by_motion`, with its tests) that reads the other two, the generator-side
  calculation (`truth.py`), the planted faults (`controls.py`), the analysis (`analysis.py`) and the staged runner
  (`run.py`). It edits nothing in `warehouse/`, `dbt/` or `realization/`. `site/check_lifecycle_numbers.py` checks every
  number on the page against the results.
- `pricing/`: the price waterfall round: a fourth dbt project (`pricing/dbt`: `int_line_month_end`,
  `fct_price_to_revenue`, `fct_retention_price_volume`, `fct_renewal_decisions`, with four singular checks) that reads
  the other three, the generator-side calculation and statistics (`truth.py`), the planted faults (`controls.py`), the
  renewal sweep (`sweep.py`) and the staged runner (`run.py`). It edits nothing in the other rounds' folders.
  `site/check_pricing_numbers.py` checks every number on the page against the results.
- `bordereaux/`: the bordereaux round: the generator (`book.py`, its own entry point, touching nothing in `warehouse/`), a small dbt project with the checks (`dbt/`), the
  engine (`engine.py`), positive controls (`controls.py`), the frozen-file guard (`freeze.py`), the bundle and its file-list check (`bundle.py`, with `bundle/BOOK.md`), recovery
  (`recover.py`), the declared and blind faults (`faults.py`, `blind.py`) and the staged runner (`run.py`). `site/render_bordereaux.py` writes the page and
  `site/check_bordereaux_numbers.py` checks every number on it against the results.
- `tests/`: the protocol pins, generator, fault, results and page tests.

## Reproduce

Linux, Python 3.12 and uv. Every Python package is pinned in `requirements.lock` (dbt-core 1.12.5,
dbt-duckdb 1.11.0, DuckDB 1.5.6). Bootstrap needs internet access for the packages; nothing else is
downloaded and no credentials are needed.

```sh
uv venv --python python3.12 .venv
uv pip sync --python .venv/bin/python requirements.lock
make build      # generate, land and dbt build on clean data
make test       # protocol pin, generator, fault and results tests
make faults     # the clean control and seven faults, each in a fresh database; rewrites results.json
make realization  # price realization and NRR, the clean run and each planted fault; rewrites results-realization.json
make sensitivity  # naive ARR on other billing mixes and seeds, generator only; rewrites results-sensitivity.json
make shortcut   # invoice lines read by service period against contracted ARR, generator only; rewrites results-shortcut.json
make lifecycle  # the lifecycle round in stages (clean, four faults, analysis, 100 event seeds, post); rewrites results-lifecycle.json
make pricing    # the price waterfall round in stages (clean, five faults, four sweep gaps, assembly); rewrites results-pricing.json
make pricing-trace  # the traced contract, from make pricing's clean database; rewrites results-pricing-trace.json
make page       # rebuilds the pages and docs/models.md from the results files, then checks the lifecycle and price waterfall pages' numbers
make bordereaux  # the bordereaux round: clean books, positive controls, faults, results; no key needed
```

`make bordereaux` can write a `results-bordereaux.json` that differs from the committed one in one self-test fault's flag count: one frozen
check reads two duplicate rows in no fixed order (`DESIGN-bordereaux.md`, the amendment on a count that depends on row order). Every catch, score and prediction comes out the same,
but the results pin in `tests/test_bordereaux.py` then fails.

The blind, independent-checks and payment-rails rounds are not in these downloads as runnable steps: their design
files, run records and runners are kept out, so they cannot be re-run from here alone; the pages describe each round.
Here `make page` rebuilds the other pages, the entry page among them (it reads `results-blind-faults.json`), and keeps those three as shipped.

The same seed gives byte-identical extracts, and `results.json` carries no run identity, so a rerun
from the same code writes the same bytes. Databases, dbt targets and logs go under `runtime/`.

## Limits

- The same author wrote the definition, the checks and the faults. A held prediction shows the
  checks do what they were built to do, not how often they would catch failures in real data.
- One currency, no tax. Revenue recognition is a stated policy, not an accounting opinion: contracts
  and onboarding fees straight-line over the term (declared in the amendments at the end of `DESIGN.md`), with no
  allocation across performance obligations and no variable consideration. Version 1 (each charge
  over its own period, fees on their day) is kept as `results-v1.json`. To rerun it without touching
  `results.json`, write elsewhere: `RECOGNITION_POLICY=billing_period .venv/bin/python -B -m
  warehouse.run runtime/fault-run-v1-rerun results-v1-rerun.json`. Its fault outcomes and predictions
  should match `results-v1.json`; its walk gains a straight-lining line of zero, and it adds a
  `recognition_policy` key and the current code hash.
- Under straight-line revenue the contract leg of the roll-forward holds by construction, like the
  bridge and the walk: all three are bookkeeping that catches code errors, never bad data. The
  roll-forward still checks card plans and usage. Of the 96 tests, 7 failed under at least one planted
  fault; only those have a recorded positive control.
- The company is one seeded draw, and the faults and checks were not re-run on other seeds. The naive
  ARR gap moves with the generator's assumed billing mix and with the seed (`results-sensitivity.json`).
- No general ledger in the main study: deferred revenue reconciles to the invoices here. In a real close the
  roll-forward is the subledger behind the journal entry and ties to the trial balance. The payment rails page adds a
  synthetic ledger for billings, cash and the processor, posted by written rules; it does not split recognised and
  deferred revenue.
- A fault that keeps every system consistent with every other (the discount fault) is invisible to
  checks inside those systems; only an outside document such as the signed order form shows it. The blind round
  found the same: none of the four such faults a second model wrote was caught. Checks against records outside the
  pipeline caught 4 of 5 such faults in a second blind set, at a cost in review time and false alarms that
  `independent-checks.html` sets out.

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