It's a feature of the tomcat/mailboxd backend.
Rather than writing your own, look at
freedups file listing
Unless the other end of the imapsync was also a single-instance store, you're unlikely to get much. The messages truly need to be *identical*, down to the final Received: header and delivery time.