bdxgate is an intake gate for delegated-authority risk bordereaux. It maps each coverholder's
spreadsheet onto one standard schema, checks every row against the binder terms, and files each row
in a SQLite ledger in exactly one of four states: accepted, quarantined (with reason codes),
superseded or duplicate. Resent files change nothing, and correction files in any order converge
to the same accepted set. A premium bridge ties the ledger to the cash the coverholder paid, in
Decimal, and reports whatever does not close. All data in this repository is synthetic.
In delegated-authority insurance a coverholder (a managing general agent) sends the carrier a monthly risk bordereau, each with its own column names, date formats and mistakes. The carrier's data team repairs the files by hand. Corrected files are sent again and loaded twice, quarantined rows are forgotten, and the premium in the warehouse never quite ties to the cash received. bdxgate makes the repair loop explicit: nothing is dropped, nothing is loaded twice, and every month has a bridge that either closes to the cent or says by how much it does not.
flowchart LR
A["Coverholder files<br/>CSV or XLSX"] --> B["Alias mapping and<br/>date parsing"]
B --> C["Row rules and<br/>cross-row rules"]
C --> D{"SQLite ledger"}
D --> E["accepted"]
D --> F["quarantined<br/>with reason codes"]
D --> G["superseded"]
D --> H["duplicate"]
I["Correction files"] -->|"re-validate"| C
F -.->|"fix, or the NEW arrives"| C
E --> J["Premium bridge"]
F --> J
K["Cash statements"] --> J
J --> L["residual 0.00<br/>or the gap"]
mapping.pyresolves each header through the coverholder's alias list (case, accents and punctuation ignored) and parses dates in the configured format:dd/mm/yyyy,mm/dd/yyyy,isoorexcel_serial(no bundled coverholder usesmm/dd/yyyy; unit tests cover it). When no format is configured and every day and month value is 12 or less, the file is reported ambiguous and refused.rules.pyapplies the row rules (types,inception < expiry,net = gross - commissionwithin 0.01, commission rate within the binder limit, sum insured within binder authority, ISO 4217 currency, inception within the binder period) and the cross-row rules (a NEWpolicy_refis unique, an END or CAN needs an accepted NEW).ledger.pystores every row once. After each load the ledger recomputes every row's state from the whole set of rows with the pure functionrules.settle, so the outcome does not depend on arrival order.corrections.pyloads correction files: full replacement rows with arevision. A correction supersedes the rows it corrects and is validated like any other row.bridge.pycomputes accepted net premium + quarantined net premium + listed timing items against cash received and reports the residual.
Python 3.12 or newer.
python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"
bdxgate generate --seed 8 --out data/
bdxgate ingest data/CH-02_2026-03.csv --coverholder CH-02 --month 2026-03
bdxgate quarantine --open
bdxgate bridge --month 2026-03 --coverholder CH-02 --cash data/cash.json
bdxgate correct data/corrections/fixes-01.csv
bdxgate status
python scripts/demo.py # a year of files, a resend and two correction files
pytest -qThe ledger lives in bdxgate.db (override with --db). Column aliases, date formats and binder
terms are in src/bdxgate/coverholders.yaml; pass another file with --config. bridge exits 1
when any month does not close.
bdxgate ingest on the CH-02 March file, where the coverholder computed commission on net
premium:
CH-02 2026-03: ingested 150 rows from CH-02_2026-03.csv
accepted 5, quarantined 145 (inception_after_expiry=3, net_mismatch=140, sum_insured_over_authority=2), superseded 0, duplicate 0
python scripts/demo.py, replaying the generated year (36 files, about 5,400 rows):
resent file CH-03_2026-07.xlsx: ledger unchanged
corrections/fixes-01.csv: 154 correction rows, 154 accepted, 0 still quarantined
corrections/fixes-02.csv: 95 correction rows, 95 accepted, 0 still quarantined
quarantined at ingest: {'inception_after_expiry': 108, 'invalid_currency': 6, 'net_mismatch': 140, 'orphan_endorsement': 5, 'sum_insured_over_authority': 54}
rows ingested: 5649
final states: {'accepted': 5326, 'quarantined': 62, 'superseded': 249, 'duplicate': 12}
accepted + quarantined + superseded + duplicate = 5649
coverholder month accepted quarantined reasons superseded duplicate
CH-01 2026-03 148 2 invalid_currency=1 sum_insured_over_authority=1 4 0
CH-01 2026-12 150 0 - 4 0
CH-02 2026-03 121 29 net_mismatch=28 sum_insured_over_authority=1 118 0
CH-02 2026-12 149 1 inception_after_expiry=1 3 0
CH-03 2026-03 148 1 inception_after_expiry=1 4 1
CH-03 2026-12 150 0 - 4 0
CH-02 2026-03
accepted net premium 1,432,417.23
quarantined net premium 298,304.37
timing: Remittance in transit -4,917.81
timing: Bank charges and FX -7.84
explained 1,725,795.95
cash received 1,725,795.95
residual 0.00
bridges closed to 0.00: 36 of 36
The status table is cut to two months. The 12 duplicates are NEW policies re-sent in a later month; the 5 orphan endorsements were accepted when their NEW arrived in the next file, so they no longer show as quarantined.
- State is a pure function of the set of rows.
settletakes every non-duplicate row and returns every state, so loading or correcting in a different order cannot change the result. The alternative, updating states incrementally on each load, is cheaper but order-dependent. The price here is a full pass over the ledger per load (about 5,600 rows for the generated year). Where two clean NEW rows claim onepolicy_ref, the earlier month wins, then the lower revision, then the lower content hash. - Idempotence at two levels. A file whose mapped content was already loaded for the same
coverholder and month is skipped entirely, so a resend or a re-saved copy leaves the ledger
identical. A row whose content hash (coverholder plus canonical standard values) matches an
earlier row is a
duplicate. Which copy is the original is settled with everything else on each load, not fixed at insert time: the earliest month wins, then the lower revision, then file name, sheet and source row. Loading the 36 files in reverse gives the same state for every row and the same bridges as loading them in order. - Corrections are revisioned replacement rows. A correction supersedes any row on its natural key
(coverholder, policy_ref, type, effective date) with a lower revision, whatever that row's state,
so a correction arriving before its original still wins. Two corrections at the same revision on
one key are ordered by content hash, not by file order, so give a re-fix a higher revision. A
correction that is itself still wrong therefore demotes a clean accepted original to
quarantined(its premium moves from accepted to quarantined, so the bridge still closes) and nothing flags the demotion beyond the new quarantine row. Changing the effective date would change the key and is therefore a new row, not a correction. - Money is
Decimaland stored as canonical text. SQLite never sums premiums (it would use floats); the bridge parses and adds them in Python. XLSX floats are converted throughrepr, so1234.56stays1234.56. - The bridge assumes a coverholder reports in one currency (set in the config) and that a correction keeps a row's net premium. A correction that changes net premium moves the ledger away from the cash statement, and the bridge reports that as a residual instead of hiding it. A correction that fixes a premium therefore always shows as a residual, because the cash statement was built from the original figures.
- CSV files must be UTF-8 (a byte order mark is fine); any other encoding is refused with an
error naming the file. Send an
.xlsxor re-save as UTF-8. - Date formats are configured; inference is a fallback that refuses to guess. For an
unconfigured coverholder a day above 12 settles the format. If every value fits both DD/MM and
MM/DD the file is refused with an
AmbiguousDateError.
pytest -q94 tests, including three property-based tests (100 examples each, derandomize=True) over random
sequences of loads, resends and corrections:
- every row is in exactly one state after every step, and the states add up to the rows ingested;
- repeating a load or a correction file leaves the ledger dump unchanged;
- every permutation of the correction files, and every interleaving of loads and corrections, yields the same accepted set, the same state for every row in every month, and the same premiums per coverholder-month and state (the inputs of the bridge). The accepted set alone leaves out the month, so these checks are what catch a copy of a row swapping months.
The generated-year tests check planted counts exactly (including 140 net_mismatch rows in CH-02
March), that accepted + quarantined + superseded + duplicate equals the rows ingested, and that all
36 coverholder-month bridges close to Decimal("0.00"), before and after corrections, and that
loading the year's files in reverse order gives identical states and bridges.
src/bdxgate/
mapping.py aliases, date formats, standard row, config
readers.py CSV and XLSX readers with sheet and row numbers
rules.py row rules and the pure settle function
ledger.py SQLite ledger, events, status queries
pipeline.py file to ledger entries
corrections.py correction files
bridge.py premium bridge in Decimal
generate.py seeded synthetic year with planted errors
cli.py bdxgate command
coverholders.yaml bundled mapping and binder terms
scripts/demo.py replay a year, a resend and two correction files
tests/ unit, property and CLI tests; tests/fixtures holds hand-made files
MIT
