PD-6510 · billing-agent · 2026-08-11 11:07:41 UTC
Error
Wrong row returned: customers[order.index] resolved cust_5522 for order #55210, expected cust_5521 via order.customer_ref; the region-filtered result set is not key-aligned with order.index
kind=db and "lookup" in the span name: a system of record you run. If the table is user-writable it is a laundering surface and belongs at External. This rule cannot see that.
I analyzed trace 21b93b4c3f7abe0c: billing-agent, 32 spans over 1m 20s, 13.3K tokens (~$0.0410).
billing-agent.rundb.lookup_customer at agents/billing/lookup.py:33customer_for() indexes the region-filtered customer list by order.index, which is not the customer's row after filtering. The invoice was addressed to cust_5522 instead of cust_5521.
Confidence 83% (judge: claude-haiku-5-5).
5e2b7d18 resolves the billing customer with customers[order.index] at agents/billing/lookup.py:33, a position in a region-filtered, name-sorted listing rather than a key. For order #55210, position 1 was Norrland Tooling AB (cust_5522), not the order's own customer_ref cust_5521 (Northwind Traders LLC). The recipient check that should have caught it has run after the send since e91c33b7 moved dispatch onto the async mail queue, so nothing stopped delivery.
Introduced in commit 5e2b7d18 (agents/billing/lookup.py:33).
Counterfactual: If the lookup had keyed on order.customer_ref, or the recipient assertion had run before the send, the $4,200.00 invoice would never have reached Norrland Tooling AB.
The fixResolve the customer by order.customer_ref instead of a position in the region-filtered listing, and move assert_recipient_matches_order in front of the mail queue, so a mismatch stops the send instead of being reported after it.
→ PR #1990 (causal/fix-PD-6510), +6/−6, merged and shipped in billing-2026.08.12.
TL;DR: Positional index used as a customer key. Fixing agents/billing/lookup.py removes this failure class entirely.