Fix the retry policy for a Pub/Sub consumer

Review the AI's codeEasy12 minGoogle style

Your task

Your Pub/Sub triggered Cloud Function returned an error for every failure. An error tells Pub/Sub to deliver the message again, so one malformed event came back all day and ran up the bill. The new policy decides what to do with each delivery: ack it, retry it, or send it to a dead letter topic. A dead letter topic is a side queue where failed messages wait for a person.

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

Input and output:

  • error_kinds: the outcome of processing each delivery, as a string. "none" means it processed cleanly. Any other value names an error, such as "malformed", "validation", "transient" or "timeout".
  • delivery_attempts: for the delivery at the same index, which attempt this is, as an integer.
  • max_attempts: the delivery cap, as an integer. It can be 0.
  • Return a list of strings, one per delivery, in input order. Each is "ack", "retry" or "dead_letter".

Rules the tests check:

  • The number of deliveries is the length of the shorter list. Extra entries in the longer list are ignored.
  • A delivery's verdict is the first of these that applies:
    1. "ack" when its kind is "none".
    2. "dead_letter" when its kind is "malformed" or "validation". These errors never pass on a retry.
    3. "retry" when its attempt number is less than max_attempts. This covers every other kind, including kinds not named here.
    4. "dead_letter" otherwise.
  • Kinds compare exactly.

Examples

route_pubsub_deliveries(error_kinds: list[str], delivery_attempts: list[int], max_attempts: int) -> list[str]
Example 1
Inputroute_pubsub_deliveries(["none", "malformed", "transient"], [1, 1, 1], 5)
Output["ack", "dead_letter", "retry"]
Example 2
Inputroute_pubsub_deliveries(["transient"], [5], 5)
Output["dead_letter"]