threadsToMessageIndices can have the wrong index if some existingMessages get added to messageInfos before the new messages start being created. The existing code basically assumes messageInfos is empty at the start the message creation loop.
This scenario never occurs for us since an "already existing message" only occurs in the case of a client-composable message, and those messages only ever get passed to createMessages one-at-a-time. But it's still better to have this code fixed up than to have this bug.