imessage: iCloud-synced copies of old messages are re-delivered as new inbound (poll ignores date)
Plugin: external_plugins/imessage 0.1.0 (the poll code is unchanged on current main)
What happens
With Messages in iCloud turned on, macOS sometimes imports another device's copy of a message into chat.db hours after the original arrived. The import is a new row: it has a new guid and a higher ROWID, but the same date and text as the row that is already there. On these imported rows, account is empty and date_delivered is 0.
poll() delivers every row with ROWID > watermark and never looks at date. So each imported copy reaches the session as a new inbound message. On one Mac, a batch of 22 self-chat messages, sent between 1 and 16 hours earlier, was re-delivered all at once, and one message from two days earlier had been re-delivered the same way before that.
This is more than noise:
- A replayed "yes" or "go ahead", or an old instruction, arrives looking exactly like a fresh message from the user. A channel session can act on it a second time.
- A replayed permission reply (
yes <code>) is only harmless because codes are unique.
Self-chat is the most exposed case because every message has a copy on each device.
Evidence (column values from chat.db; content omitted)
| ROWID | is_from_me | account | date | date_delivered | guid |
|---|---|---|---|---|---|
| 578885 | 1 | E:<apple id> |
T | set | A |
| 578886 | 0 | E:<apple id> |
T | set | B |
| 579091 | 0 | (empty) | T | 0 | C (inserted ~2 days later) |
Proposed fix
Before delivering an inbound row, skip it when the same chat already has an earlier inbound row (lower ROWID, different guid) within 2 seconds of its date and with the same decoded text.
I checked this against a real chat.db:
- 181 recent inbound rows
- all 23 synced copies were flagged
- 0 legitimate messages were flagged
Patch:
@@ -128,6 +128,19 @@
ORDER BY m.ROWID ASC
`)
+// Earlier inbound copies of a message: same chat, lower ROWID, within 2s.
+// Messages in iCloud can import another device's copy of a message hours
+// later as a new row (new guid, same date and text); ROWID alone calls it new.
+const qEarlierCopies = db.query<Row, [string, number, number, number]>(`
+ SELECT m.ROWID AS rowid, m.guid, m.text, m.attributedBody, m.date, m.is_from_me,
+ m.cache_has_attachments, m.service, h.id AS handle_id, c.guid AS chat_guid, c.style AS chat_style
+ FROM message m
+ JOIN chat_message_join cmj ON cmj.message_id = m.ROWID
+ JOIN chat c ON c.ROWID = cmj.chat_id
+ LEFT JOIN handle h ON h.ROWID = m.handle_id
+ WHERE c.guid = ? AND m.ROWID < ? AND m.is_from_me = 0 AND m.date BETWEEN ? AND ?
+`)
+
const qHistory = db.query<Row, [string, number]>(`
SELECT m.ROWID AS rowid, m.guid, m.text, m.attributedBody, m.date, m.is_from_me,
m.cache_has_attachments, m.service, h.id AS handle_id, c.guid AS chat_guid, c.style AS chat_style
@@ -774,6 +787,14 @@
return p.startsWith('~/') ? join(homedir(), p.slice(2)) : p
}
+const SYNC_COPY_SLACK_NS = 2_000_000_000
+
+function isSyncedCopy(r: Row, text: string): boolean {
+ return qEarlierCopies
+ .all(r.chat_guid, r.rowid, r.date - SYNC_COPY_SLACK_NS, r.date + SYNC_COPY_SLACK_NS)
+ .some(o => o.guid !== r.guid && messageText(o) === text)
+}
+
function handleInbound(r: Row): void {
if (!r.chat_guid) return
if (!ALLOW_SMS && r.service !== 'iMessage') return
@@ -795,6 +816,10 @@
// Never deliver our own sends. In self-chat the is_from_me=1 rows are empty
// sent-receipts anyway — the content lands on the is_from_me=0 copy below.
if (r.is_from_me) return
+ if (isSyncedCopy(r, text)) {
+ process.stderr.write(`imessage channel: skipping synced copy of an earlier message (rowid ${r.rowid})\n`)
+ return
+ }
if (!r.handle_id) return
const sender = r.handle_id
Related: #3993 (persisting the watermark). It doesn't cause this and it doesn't fix it. Once a watermark survives restarts, late imports still arrive with a higher ROWID, so a date/content check is needed either way.
Source: anthropics/claude-plugins-official