Skip to content

feat(pipeline): share insights and dedupe reports across mailboxes - #11

Merged
gopkg-dev merged 3 commits into
mainfrom
feat/cross-mailbox-insights
Nov 21, 2026
Merged

gopkg-dev merged 3 commits into
mainfrom
feat/cross-mailbox-insights

Conversation

@gopkg-dev

@gopkg-dev gopkg-dev commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Closes #9

Why

When the same email reaches several of your mailboxes, each copy was analyzed separately (two AI calls, possibly different summaries and to-dos), pushed to the same channel twice, and listed twice in daily and weekly reports.

What changed

A copy counts as the same email when Message-ID, sender (case-insensitive) and subject all match, so a forged Message-ID can't pick up another email's insight or hide another email in reports.

AI analysis (server/src/services/pipeline.ts)

  • Before calling the AI, look for another copy that already has an insight and copy it, along with its AI labels. The job result records copiedFrom.
  • Copies are processed one at a time per Message-ID (in-process lock), so copies that arrive together still call the AI only once.
  • Reanalysis (reanalyze) updates every other copy that already has an insight (syncedCopies in the job result).

Notifications: the issue left this open. I went with push once per email.

  • A channel gets one push per email, from rule notify actions and event subscriptions alike.
  • Rule actions tied to a mailbox (mark read, star, move, AI reply) still run on every copy.
  • Channels already pushed for a copy come from finished process_message jobs, plus a 60-second in-memory record covering the gap before a job's result is written.

Reports (server/src/services/reports.ts)

  • receivedCte takes a new acrossAccounts option. Reports use it for totals, previous-period count, categories, daily series, top senders, important, needs reply and bills.
  • Per-account received counts still count each mailbox's copy.
  • When picking which copy represents an email, prefer one in the inbox, then one that has been analyzed.
  • answered is true when any copy was replied to, so a reply sent from another mailbox clears "needs reply".

All-mailboxes view (server/src/services/mail.ts, web/)

  • In the all-mailboxes list (no account or folder selected), a thread whose latest email also appears in another mailbox's thread is folded into that thread. The row shows every mailbox it is in; unread and starred combine across copies.
  • A copy with extra replies absorbs the shorter one. If both threads' latest emails are the same email, the one with more matching messages is kept.
  • The row and the reading pane carry copyThreadIds. Read, unread, star, unstar, archive, trash, delete and labels apply to every copy. Opening a merged thread marks every copy read.
  • The reading pane's star button also counts stars on the copies (copiesFlagged), so it matches the list row, and unstar clears a star held only by a copy.
  • Per-mailbox and per-folder views are unchanged.

Schema: migration 0025_message_id_global adds a partial index on messages.message_id (where not null) for the cross-account lookup.

Reviewer notes

  • The statistics page (analytics.ts) also uses receivedCte in per-account mode. Two things change there:
    • Within one mailbox, deduplication now also requires the sender and subject to match.
    • answered is true if any folder copy was replied to.
  • The per-Message-ID lock and in-flight push record live in memory, which fits the current single-process server. After a restart, a copy synced later still finds earlier pushes in the jobs table, which is kept for KEEP_DAYS.
  • Mail sent between two of your own connected mailboxes also merges in the all-mailboxes view (the sender's Sent copy and the recipient's Inbox copy). Trashing that row trashes both; archive only affects inbox copies.
  • The merge query is written with LEFT JOIN / GROUP BY rather than correlated NOT EXISTS. The correlated version made Postgres overestimate the cost and spend ~360ms in JIT compilation; now the all-mailboxes list takes about as long as a single inbox.

Tests

  • New server/test/cross-mailbox.test.ts covers:
    • reusing an insight with a single push
    • copies arriving at the same time
    • a forged Message-ID (different sender or subject) not being reused
    • sender address case differences
    • reanalysis syncing to other copies
    • report deduplication, per-account counts, and replying from another mailbox
    • all-mailboxes merge, including a forged sender, per-account views, scope limits, and a copy with extra replies, and stars held only by a copy
  • pnpm test in server/: 177/177 pass. tsc --noEmit is clean. web/ type-checks and builds.

🤖 Generated with Claude Code

gopkg-dev and others added 3 commits November 20, 2026 19:40
When the same email reaches several mailboxes, each copy was analyzed
on its own (two AI calls, possibly different summaries and to-dos),
pushed to the same channel twice, and listed twice in daily and weekly
reports.

A copy is the same email when Message-ID, sender (case-insensitive)
and subject all match, so a forged Message-ID can't pick up another
email's insight.

- Reuse an existing insight and its AI labels from another copy
  instead of calling the AI; copies are processed one at a time per
  Message-ID so simultaneous arrivals still call the AI once
- Reanalysis updates every copy that already has an insight
- Push each email to a channel once; per-mailbox rule actions (mark
  read, star, move) still run on every copy
- Reports count an email once in totals, categories, important,
  needs reply and bills; per-account counts stay per mailbox, and a
  reply from any copy clears needs reply
- Add a partial index on messages.message_id for cross-account lookup

Closes #9

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In the all-mailboxes list, a thread whose latest email also appears in
another mailbox's thread is folded into that thread, so the same
conversation shows once with every mailbox it is in. When both latest
emails are the same, the thread with more matching messages is kept.
Per-mailbox and per-folder views are unchanged.

The merged row and the reading pane carry copyThreadIds; read, star,
archive, trash, delete and label actions apply to every copy.

The containment query uses LEFT JOIN / GROUP BY instead of correlated
NOT EXISTS so the planner estimate stays low and JIT is not triggered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In the all-mailboxes reader, the star button only looked at the open
thread, so it showed unstarred while the merged list row showed a star
when only another mailbox's copy was starred. Clicking it did nothing,
because the open thread had no starred message to clear.

getThread now returns copiesFlagged, scoped to visible mailboxes, and
the reader counts it. When the open thread has nothing to change, the
action still applies to the copy threads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gopkg-dev
gopkg-dev merged commit 0387a47 into main Nov 21, 2026
1 check passed
@gopkg-dev
gopkg-dev deleted the feat/cross-mailbox-insights branch November 21, 2026 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Merge AI insights and report entries for the same email received in several mailboxes

1 participant