Fix the charge counter for a retrying client

Review the AI's codeEasy10 minStripe style

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, written order_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; and late_error, where a 5xx came back after the charge. Any other outcome, such as failed, 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]) -> int
Example 1
Inputcount_committed_charges(["ord_1|k1|timeout", "ord_1|k1|timeout", "ord_1|k1|ok"])
Output1
Example 2
Inputcount_committed_charges(["ord_1|k1|timeout", "ord_1|k2|timeout", "ord_1|k3|ok"])
Output3
Example 3
Inputcount_committed_charges(["ord_1||timeout", "ord_1||ok"])
Output2