Invisible duplicate when copying messages in Mail.app?

I have written a script that copies a message from Gmail account 1 to Gmail account 2. Currently I am just trying it out so I runt it multiple times and make minor changes. This testing process has now three times in row ended up with an invisible duplicate message in 2.

This is what I do:

  1. Copy message from 1 to 2
  2. Copy the same message from the source folder in 1 to a backup folder in 1.
  3. I then compare the number of message in 2 and the backup folder in 1. It should be the same, but it isn’t (There are two messages in 2 but three messages in the backup folder).

Both when I look in the Gmail web UI and Mail.app only two messages are visible but when I execute

get the count of messages of dstMbx

(dstMbx is the destination mailbox in 2)

it returns three when it should be two.

Furthermore, if I get a list of the messages in dstMbx and loop over this list:

	set theMessages to messages of dstMbx
	repeat with theMsg in theMessages
		get subject of theMsg
		get message id of theMsg
	end repeat

it lists three messages, but two of them are duplicates. A simplified log output from this loop looks like this:

get message id of message id 2063 of mailbox “[Gmail]/All Mail” of account id “0337E09E-C424-45AE-8BED-17BCDDF6791D”
→ “f73ac05e-57b3-11ed-8143-95caf7eb3c53@facebookmail.com
get message id of message id 2061 of mailbox “[Gmail]/All Mail” of account id “0337E09E-C424-45AE-8BED-17BCDDF6791D”
→ “f73ac05e-57b3-11ed-8143-95caf7eb3c53@facebookmail.com

Note:

  1. The UUID is identical (I excluded the third message, it has another UUID)
  2. There seem to be two different message IDs - one that is the RFC8222msgid (the UUID) and one that I guess is internal to Mail.app, Mail.app’s mailbox or so. The latter message IDs are different, 2063 and 2061.

Can someone explain what is going on here?

Footnote: Gmail automatically handles duplicates based on RFC8222msgid. That is, if you copy a message to Gmail from an outside source, it checks that the message-ID doesn’t already exist. If it does, Gmail just discards the duplicate.