Fix the partial batch failure report for SQS

Review the AI's codeEasy10 minAmazon style

Your task

Your SQS batch Lambda handles up to 10 messages per invocation. It caught every exception and returned success, so SQS deleted the whole batch, failed messages included, and they were lost. The fix uses partial batch responses: the handler returns the ids of the messages that failed, and SQS delivers only those again.

An assistant wrote collect_batch_item_failures(message_ids, succeeded). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • message_ids: the id of each message, in batch order. Each id is a string. An id can be empty and can repeat.
  • succeeded: for the message at the same index, true when it processed and false when it failed.
  • Return the list of ids to report as failed.

Rules the tests check:

  • The batch is as long as the shorter list. Extra entries in the longer list are ignored.
  • Report message_ids[i] for every index i where succeeded[i] is false, in batch order.
  • A repeated id is reported once for each index where it failed, and not for the indexes where it succeeded.
  • An empty id is reported like any other id.
  • When nothing failed, return an empty list.

Examples

collect_batch_item_failures(message_ids: list[str], succeeded: list[bool]) -> list[str]
Example 1
Inputcollect_batch_item_failures(["m1", "m2", "m3"], [True, False, True])
Output["m2"]
Example 2
Inputcollect_batch_item_failures(["m1", "m2"], [True, True])
Output[]