Three Presses, Three Remittances — Python Bug Hunt
Modelled on the "Dalmellington bug" in the Post Office's Horizon system, one of the defects examined in the High Court's 2019 Horizon Issues judgment (Bates…
- Language: Python
- Layer: Database
- Difficulty: Medium
- Concepts: Idempotency, Money
- Modelled on: Post Office Horizon · Dalmellington
- Visible tests: a single remittance is recorded; pressing confirm three times books one remittance
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-1 (priority: Critical (branch accounts))
- Ticket: BUG-HNG-DAL
- Reported by: branch support
- Time to fix: 20 minutes, the target for a medium incident
Failing checks on the shipped build
- pressing confirm three times books one remittance — expected 1 but got 3
How to work this incident
- Read the report: write down the expected behaviour and the actual one in a sentence each.
- Reproduce it: run the visible tests and find the failing check that matches the report.
- Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
- Follow the data through the query or the repository code; compare the rows it returns with the rows the report expects.
- Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.
Briefing
Modelled on the "Dalmellington bug" in the Post Office's Horizon system, one of the defects examined in the High Court's 2019 Horizon Issues judgment (Bates v Post Office). At the Dalmellington branch a cash remittance to an outreach branch was recorded several times: the terminal was slow to respond, the confirm button was pressed again, and each press was booked as another transfer. The duplicated entries appeared as a cash shortfall at the branch. Across the wider Horizon scandal, hundreds of subpostmasters were held liable — many prosecuted — for shortfalls the system produced.
remittance.py books a remittance. The counter terminal sends one call per press of the confirm button, each carrying the request_key generated when the remittance screen was opened.
Make record_remittance idempotent on that key.
Bug report
BUG-HNG-DAL · Priority: Critical (branch accounts) · Reported by: branch support
record_remittance(ledger, branch, request_key, amount_pence):
- amount_pence <= 0 raises ValueError (unchanged)
- the first call for a (branch, request_key) inserts one row and returns {"status": "recorded", "id": <row id>}
- a repeat call with the same branch, key and amount inserts nothing and returns {"status": "duplicate", "id": <the original row id>}
- a repeat with the same branch and key but a DIFFERENT amount raises ValueError and inserts nothing
- keys are scoped to a branch: the same key at another branch is a separate remittance
Observed: a remittance confirmed three times while the terminal hung is booked three times; the branch shows a shortfall of twice the amount.
Logs
[horizon] branch=DAL rem_out key=R-5510 amount=800000 row=41
[horizon] branch=DAL rem_out key=R-5510 amount=800000 row=42
[horizon] branch=DAL rem_out key=R-5510 amount=800000 row=43
[balancing] branch=DAL discrepancy=-1600000The code as shipped
src/branch/remittance.py (editable)
# Books a cash remittance sent from a branch.
def record_remittance(ledger, branch, request_key, amount_pence):
if amount_pence <= 0:
raise ValueError("amount must be positive")
row = ledger.insert(branch, request_key, amount_pence)
return {"status": "recorded", "id": row["id"]}
Read-only context: src/branch/ledger.py.
Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More Python bug hunts.