Fix the retry policy for a charge request

Review the AI's codeEasy12 min

Your task

Your Java service retries a flaky call to the payments API, POST /charges. The first version retried on every exception, charged some customers twice and kept retrying a 400 that could never succeed. The request now carries one idempotency key across attempts, and the policy decides per attempt what to do.

An assistant wrote plan_retry_actions(outcomes, max_attempts). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • outcomes: the result of each attempt, in order: an HTTP status such as "200" or "503", "timeout" for a connect timeout, or "retry_after" for a response that names a time to retry.
  • max_attempts: the most attempts allowed.
  • Return one action per attempt made: "retry", "done", "fail" or "exhausted".

Rules the tests check:

  • Let n be the smaller of max_attempts and the number of outcomes. Make at most n attempts. If n is 0 or less, return [].
  • An outcome of exactly 3 digits that starts with 2 is a success: "done", and stop.
  • Only timeout, 502, 503, 504 and retry_after are retriable. For one of these at attempt i, counting from 0: "retry" when i + 1 is less than n, otherwise "exhausted", and stop.
  • Anything else, 500 and every 4xx status included: "fail", and stop.

Examples

plan_retry_actions(outcomes: list[str], max_attempts: int) -> list[str]
Example 1
Inputplan_retry_actions(["timeout", "503", "200"], 5)
Output["retry", "retry", "done"]
Example 2
Inputplan_retry_actions(["400"], 3)
Output["fail"]
Example 3
Inputplan_retry_actions(["timeout", "timeout"], 2)
Output["retry", "exhausted"]