Current policy after applying all updates:
- duplicate_event: first
- inclusive_limit: true
- max_refund_age_days: 30
- rounding: per_line
- order: id
- refund_overflow: cap
- refunds_first: true
- void_is_tombstone: true
- min_invoice_cents: 10
- status_case: lower
- currency: USD
- fee_refundable: true

I assume the payload shape is {invoices: [...]}, with the policy keys taken from the policy module rather than the payload. Each invoice's currency field is compared to the policy currency. Duplicate eids keep the first value, and the same key position either way.

Refund handling: the ceiling is principal plus fee, where principal is the sum of the lines (the charge minus the fee). Because fee_refundable is true, the ceiling equals the charge. Under cap overflow, a refund is limited to the remaining ceiling minus what has already been refunded. A refund that is capped to zero changes nothing.

Status: void if inactive, else refunded if refunded equals charge, else open. The charge is always at least 10 cents, so it is nonzero. Status is lowercase.

A void event sets active to false. A restore re-enables refunds only if the policy is not a tombstone and no void has occurred. With the tombstone policy, once a void has occurred, restore is ignored. Without a void, restore does nothing, since the invoice is already active. Because the status is void whenever inactive, I treat active as the state flag.

```json
{"files": {"main.py": "from core import run\nfrom policy import POLICY\ndef evaluate(payload):\n    return run(payload, POLICY)\n", "policy.py": "POLICY = {\n    'duplicate_event': 'first',\n    'inclusive_limit': True,\n    'max_refund_age_days': 30,\n    'rounding': 'per_line',\n    'order': 'id',\n    'refund_overflow': 'cap',\n    'refunds_first': True,\n    'void_is_tombstone': True,\n    'min_invoice_cents': 10,\n    'status_case': 'lower',\n    'currency': 'USD',\n    'fee_refundable': True,\n}\n", "helpers.py": "from decimal import Decimal, ROUND_HALF_UP\n\ndef to_cents(s):\n    return int((Decimal(str(s)) * 100).quantize(Decimal('1'), rounding=ROUND_HALF_UP))\n\ndef dec100(s):\n    return Decimal(str(s)) * 100\n", "core.py": "from decimal import Decimal, ROUND_HALF_UP\nfrom helpers import to_cents, dec100\n\ndef run(payload, policy):\n    if isinstance(payload, dict):\n        invs = payload.get('invoices') or []\n    else:\n        invs = payload or []\n    out = []\n    for inv in invs:\n        if inv.get('currency') != policy['currency']:\n            continue\n        lines = inv.get('lines', [])\n        if policy['rounding'] == 'per_line':\n            principal = sum(to_cents(x) for x in lines)\n        else:\n            tot = sum((dec100(x) for x in lines), Decimal(0))\n            principal = int(tot.quantize(Decimal('1'), rounding=ROUND_HALF_UP))\n        fee = int(inv.get('fee_cents', 0))\n        charge = principal + fee\n        if charge < policy['min_invoice_cents']:\n            continue\n        d = {}\n        for e in inv.get('events', []):\n            k = e['eid']\n            if k in d:\n                if policy['duplicate_event'] == 'last':\n                    d[k] = e\n            else:\n                d[k] = e\n        evs = list(d.values())\n        if policy['refunds_first']:\n            evs = [e for e in evs if e['kind'] == 'refund'] + [e for e in evs if e['kind'] != 'refund']\n        ceiling = principal + fee if policy['fee_refundable'] else principal\n        active = True\n        voided = False\n        refunded = 0\n        created = inv['created']\n        for e in evs:\n            k = e['kind']\n            if k == 'void':\n                active = False\n                voided = True\n            elif k == 'restore':\n                if voided and policy['void_is_tombstone']:\n                    continue\n                active = True\n            elif k == 'refund':\n                if not active:\n                    continue\n                age = e['day'] - created\n                lim = policy['max_refund_age_days']\n                if age < 0:\n                    continue\n                if not (age < lim or (policy['inclusive_limit'] and age == lim)):\n                    continue\n                amt = int(e['cents'])\n                room = ceiling - refunded\n                if amt > room:\n                    if policy['refund_overflow'] == 'cap':\n                        amt = room\n                    else:\n                        continue\n                refunded += amt\n        if not active:\n            st = 'void'\n        elif refunded == charge:\n            st = 'refunded'\n        else:\n            st = 'open'\n        if policy['status_case'] == 'upper':\n            st = st.upper()\n        out.append({'id': inv['id'], 'charge': charge, 'refunded': refunded, 'balance': charge - refunded, 'status': st, '_c': created})\n    if policy['order'] == 'created':\n        out.sort(key=lambda r: (r['_c'], r['id']))\n    else:\n        out.sort(key=lambda r: r['id'])\n    for r in out:\n        del r['_c']\n    return {'invoices': out}\n"}}
```