# Write integration faults for a health data platform

You are writing test faults for a data platform that receives six synthetic feeds about the same people and publishes
four monthly metrics. You will not see how the platform processes the feeds or what it checks. Write faults the way
they happen in real integrations: an interface upgrade, a vendor change, a mapping table edit, a reprocessed batch, an
outage, a new registration screen, a file split differently. You decide what to break.

Everything here is synthetic. No real person or record is involved.

## What arrives

A year of records (about 165,000 envelopes), each a Python dict:

```
{"rid": "lab-3f2a9c01de", "feed": "lab", "source": "lab-north", "arrived": 1650411890, "payload": "..."}
```

- `rid`: a unique record id. `feed`: one of `adt`, `lab`, `device`, `enroll`, `claims`, `consent`. `source`: the
  interface that delivered it: `adt-north`, `adt-central`, `adt-south`, `lab-north`, `lab-central`, `lab-south`,
  `device`, `enroll`, `claims`, `consent`. `arrived`: when the platform received it, in Unix seconds (UTC).
- The sites are North, Central and South, all in the America/Toronto time zone. Every person belongs to one site.

The payloads:

- **adt** (HL7 version 2, segments separated by carriage returns, fields by `|`): `ADT^A28` registers a person (sent
  once for everyone before the year starts), `ADT^A01` an inpatient admission, `ADT^A03` a discharge, `ADT^A04` an
  outpatient visit at the in-system clinic. MSH-7 is the message time, EVN-2 the event time, PID-3 the MRN (eight
  digits, zero padded), PID-5 the name, PID-7 the date of birth, PID-8 the sex, PID-11 component 5 the postal code,
  PV1-2 the patient class (I inpatient, O outpatient), PV1-19 the visit number, PV1-44 the admit or visit time, PV1-45
  the discharge time, IN1-36 the plan member id when the clinic has it. Times look like `20220419092100-0400`.
- **lab** (HL7 version 2 `ORU^R01`): one lipid panel per message. PID as above, OBR-7 the draw time, then two OBX
  segments: LOINC 13457-7 (LDL cholesterol, calculated) and LOINC 2093-3 (total cholesterol), each with OBX-5 the
  value, OBX-6 the unit (`mg/dL`), OBX-7 the reference range, OBX-11 the result status and OBX-14 the observation
  time. Results arrive 2 to 36 hours after the draw.
- **device** (FHIR R4 Observation, JSON): one home blood pressure reading, LOINC 8480-6 (systolic), with the MRN in
  `subject.identifier.value`, `effectiveDateTime` with an offset, and `valueQuantity` in `mmHg`. Readings arrive
  within an hour.
- **enroll** (X12 834 layout, segments end with `~`, elements split by `*`): one coverage change per interchange.
  INS-03 is the change type: 021 add, 024 termination, 025 reinstatement of a terminated span. REF*0F is the member
  id (`M` and nine digits), NM1*IL the name, DMG the date of birth and sex, N4 the postal code, DTP*348 the span begin
  date, DTP*349 the end date on a termination. Files go out weekly on Mondays.
- **claims** (X12 837P layout): one professional claim from an outside provider. NM1*IL carries the member's name and
  member id, DMG the date of birth and sex, DTP*472 the service date. Claims arrive days to weeks after the visit.
- **consent** (FHIR R4 Consent, JSON): a person withdrawing consent for analytics use, with the MRN in
  `patient.identifier.value` and the withdrawal time in `provision.period.start`. After a withdrawal, the person's
  records must not be used in any metric.

Fourteen sample envelopes are in `samples.jsonl` (from a different synthetic year than the one your faults will run
on).

## What the platform publishes

Months 1 to 12 are the year; months 13 and 14 follow it. Metrics are published for months 3 to 12, once, from what had
arrived by the publication time:

1. **LDL control rate**: of people with an LDL result drawn in the month, the share whose last LDL in the month is under
   100 mg/dL. Published 7 days after month end.
2. **30-day follow-up rate**: of inpatient discharges in the month of people who were plan members that day, the share
   followed by an outpatient visit (an in-system clinic visit or an outside claim) within 30 days. Published 45 days
   after month end.
3. **Active enrolled members**: members with an active coverage span on the 15th. Published 7 days after month end.
4. **Monitoring adherence**: of people enrolled in home blood pressure monitoring, the share with readings on 16 or more
   days in the month. Published 3 days after month end.

## What to write

Write ten faults, as one Python file. Each fault is a function that receives the full list of envelopes (a copy you
may change), a seeded `random.Random` and a context dict, and returns the new list:

```python
import random

def my_fault(envelopes, rng, ctx):
    out = []
    for e in envelopes:
        ...
        out.append(e)
    return out

FAULTS = [
    {"id": "B01", "name": "a short plain name", "feeds": ["lab"],
     "what_breaks": "one or two sentences: what the integration did wrong",
     "expected_effect": "which metric you expect to move, which way, and roughly how far",
     "apply": my_fault},
    # B02 to B10
]
```

`ctx` holds `ctx["month_start"][m]`, the Unix time (UTC) when month m starts (m from 1 to 15), `ctx["months"]`,
`ctx["sources"]` and `ctx["tz"]`.

Rules:

- A fault changes what the feeds deliver, never the care that happened: you may change payloads, change `arrived`,
  drop envelopes, duplicate them or add new ones built from existing ones. Keep the `rid` of an envelope you change;
  give a new envelope a new unique `rid` that starts with `blind-`.
- Each fault starts somewhere in months 3 to 12 and touches a realistic share of records. Vary them: what they break
  (identity, the meaning of a value, time, completeness, coverage, consent, or anything else you think is real), how
  many records they touch, and how hard they would be to notice.
- Python standard library only. No files, no network, no clock: use `rng` for every random choice, so the same seed
  gives the same result. Each fault must finish in a few seconds on 165,000 envelopes.
- Return a list of dicts with the same five keys.

Reply with the complete Python file and nothing else, in one fenced code block. Use only what is in this message and
`samples.jsonl`: do not read any other file, list any folder or run any command.
