Fix the effect count for a widened retry

Review the AI's codeEasy10 min

Your task

An agent refactored your Python retry helper "for clarity". The old version retried only TimeoutError. The new one also retries ConnectionError, and the tests still pass. But some connection errors happen after the server has already applied the request, and retrying those applies it twice. To show the change, you count how many times a call's effect lands.

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

Input and output:

  • attempt_outcomes: what each attempt does, in order.
  • retry_connection_errors: true for the new policy, false for the old one.
  • max_attempts: the most attempts the helper makes.
  • Return how many times the server applied the request.

Rules the tests check:

  • Make attempts in order, at most max_attempts of them, and only while outcomes are left.
  • Read each outcome with whitespace trimmed from both ends, ignoring case:
    • ok: the call succeeded and its effect applied once. Stop.
    • timeout: nothing reached the server. Retry.
    • dropped-after-send: the server applied the request, then the connection died. The effect counts once. Retry only when retry_connection_errors is true. Otherwise stop.
    • refused: the connection never opened, so nothing applied. Retry only when retry_connection_errors is true. Otherwise stop.
    • Anything else: nothing applied. Stop.

Examples

applied_effect_count(attempt_outcomes: list[str], retry_connection_errors: bool, max_attempts: int) -> int
Example 1
Inputapplied_effect_count(["dropped-after-send", "ok"], False, 3)
Output1
Example 2
Inputapplied_effect_count(["dropped-after-send", "ok"], True, 3)
Output2
Example 3
Inputapplied_effect_count(["timeout", "timeout", "ok"], True, 5)
Output1