Fix the charge counter for a retrying client
Your task
An AI wrapped a Go POST to a payment provider in a retry loop. Tests against a mock that never timed out passed, but in production some customers were charged twice. A request can charge the card and still look like a failure to the client. You count the charges the provider really creates for a log of attempts.
An assistant wrote count_committed_charges(attempts). It is in the editor. It has a bug. Find it and fix it.
Input and output:
attempts: one attempt per string, writtenorder_id|idempotency_key|outcome. An empty key means the request carried no idempotency key.- Return how many charges the provider created.
Rules the tests check:
- Skip an attempt that does not split on
|into exactly 3 parts. - These outcomes reached the provider and charged the card:
ok;timeout, where the client never saw the response; andlate_error, where a 5xx came back after the charge. Any other outcome, such asfailed, created nothing. - For an attempt that charged: if its key is not empty and an earlier attempt that charged used the same key, the provider returns the stored result and creates nothing new. Otherwise it creates a new charge. An empty key never matches anything.
Examples
count_committed_charges(attempts: list[str]) -> intExample 1
Input
count_committed_charges(["ord_1|k1|timeout", "ord_1|k1|timeout", "ord_1|k1|ok"])Output
1Example 2
Input
count_committed_charges(["ord_1|k1|timeout", "ord_1|k2|timeout", "ord_1|k3|ok"])Output
3Example 3
Input
count_committed_charges(["ord_1||timeout", "ord_1||ok"])Output
2