Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: preserve browser message and activity identity (#11150)

Adam Eivy86183c2f1d20
diff --git a/server/lib/README.md b/server/lib/README.md
index cceb194705..1914b624da 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -819,7 +819,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
| `launchVideoValidation.js` | `validateLaunchVideoAssets` checks frozen launch composition text, storyboard reading time, duration, and poster position before rendering. |
-| `messageBrowserIdentity.js` | Shared Outlook row extraction and identity-safe browser action scripts; exact unique targets and confirmed row removal. |
+| `messageBrowserIdentity.js` | Shared Outlook row extraction, canonical row/member ingestion identity, conservative legacy matching, detail mapping, and identity-safe browser action scripts; exact unique targets and confirmed row removal. |
| `messageBrowserCompose.js` | Confirmed Outlook/Teams draft delivery through the authenticated browser tab, as pure helpers: `planDelivery` (deliverability + intended recipients/thread, run BEFORE a draft is claimed), `buildComposePhaseScript` (probe/open/fill/submit/discard CDP scripts, args as JSON), `classifySendOutcome` (confirmed only on a closed compose surface plus a fresh provider signal; anything else after the single submit click is `unknown`), `recipientsMatch`, `checkAccountIdentity`, and overridable `COMPOSE_SELECTOR_DEFAULTS`/`resolveComposeConfig`. Defaults are unvalidated against a live mailbox. |
| `messageTransport.js` | `MESSAGE_ACCOUNT_TRANSPORT` — the one table of what each message-account type delivers (`sendVia`, email-capable): `sendViaForAccountType`, `accountTypeCanSend`, and `pickEmailSenderAccount` (privacy email lanes prefer an API transport, never pick a Teams chat). |
diff --git a/server/lib/messageBrowserIdentity.js b/server/lib/messageBrowserIdentity.js
index 9479358aa3..fbe1abe089 100644
--- a/server/lib/messageBrowserIdentity.js
+++ b/server/lib/messageBrowserIdentity.js
@@ -1,3 +1,5 @@
+import { createHash } from 'node:crypto';
+
/** DOM reader shared by Outlook ingestion and destructive browser actions.
* Self-contained so it can be serialized into a CDP evaluation. */
export function readOutlookMessageRow(row) {
@@ -131,3 +133,92 @@ async function actOnMessageRow(provider, identity, action, readOutlookRow) {
}
return { code: 'MESSAGE_ACTION_UNCONFIRMED' };
}
+
+/** Compatibility hash: existing no-provider-ID messages keep their IDs. */
+export function browserSummaryId(date, sender, subject) {
+ return 'pw-' + createHash('md5').update(`${date}|${sender}|${subject}`).digest('hex').slice(0, 12);
+}
+
+/** A conversation row and its expanded messages are different identity domains. */
+export function messageIngestionIdentity(message) {
+ if (!message.providerRowId) return message.externalId || message.id;
+ const row = message.providerRowId;
+ const member = message.browserMessageKind === 'member'
+ || (!message.browserMessageKind && message.bodyFull && message.threadId);
+ if (!member) return `browser-row:${JSON.stringify(row)}`;
+ if (message.providerMessageId) return `browser-member:${JSON.stringify([row, message.providerMessageId])}`;
+ const parsed = Date.parse(message.date);
+ const date = Number.isFinite(parsed) ? new Date(parsed).toISOString() : message.date;
+ const sender = message.from?.email?.trim().toLowerCase() || message.from?.name || '';
+ return `browser-member:${JSON.stringify([row, date || '', sender, message.browserMemberDisambiguator || ''])}`;
+}
+
+/** Shared detail mapping for initial browser sync and explicit refresh. */
+export function browserDetailIdentities(summary, details) {
+ const members = details.map(detail => {
+ const date = detail.date || summary.date || '';
+ const name = detail.from || summary.from?.name || '';
+ return {
+ externalId: browserSummaryId(date, name, summary.subject || ''),
+ providerRowId: summary.providerRowId || null,
+ providerMessageId: detail.providerMessageId || null,
+ browserMessageKind: 'member',
+ from: { name, email: detail.fromEmail || summary.from?.email || '' },
+ date
+ };
+ });
+ const counts = new Map();
+ for (const member of members) {
+ const key = messageIngestionIdentity(member);
+ counts.set(key, (counts.get(key) || 0) + 1);
+ }
+ // A timestamp/sender collision is not permission to overwrite a sibling.
+ // Exact body + occurrence preserves distinct observed members on replay;
+ // a changed ambiguous body gets a new record instead of stealing an old one.
+ const occurrences = new Map();
+ return members.map((member, index) => {
+ const key = messageIngestionIdentity(member);
+ if (member.providerRowId && !member.providerMessageId && counts.get(key) > 1) {
+ const body = createHash('sha256').update(details[index].body || '').digest('hex');
+ const occurrenceKey = `${key}:${body}`;
+ const occurrence = occurrences.get(occurrenceKey) || 0;
+ occurrences.set(occurrenceKey, occurrence + 1);
+ member.browserMemberDisambiguator = `${body}:${occurrence}`;
+ }
+ return member;
+ });
+}
+
+/** Match only unique identities; a summary fallback may never cross known rows. */
+export function matchBrowserMessage(messages, incoming, batch = [incoming]) {
+ const key = messageIngestionIdentity(incoming);
+ const exact = messages.filter(message => messageIngestionIdentity(message) === key);
+ if (exact.length === 1 && batch.filter(message => messageIngestionIdentity(message) === key).length === 1) return exact[0];
+ if (exact.length) return null;
+ const compatible = (message, candidate) => {
+ const summaryBecomesMember = message.providerRowId && message.providerRowId === candidate.providerRowId
+ && candidate.browserMessageKind === 'member' && !message.bodyFull
+ && Number.isFinite(Date.parse(message.date)) && Date.parse(message.date) === Date.parse(candidate.date)
+ && (message.from?.email && candidate.from?.email
+ ? message.from.email.trim().toLowerCase() === candidate.from.email.trim().toLowerCase()
+ : message.from?.name && message.from.name === candidate.from?.name);
+ return (message.externalId === candidate.externalId || summaryBecomesMember)
+ && !(candidate.browserMessageKind === 'row' && (message.browserMessageKind === 'member' || (message.bodyFull && message.threadId)))
+ && (!message.providerRowId || !candidate.providerRowId || message.providerRowId === candidate.providerRowId)
+ && !message.browserMemberDisambiguator && !candidate.browserMemberDisambiguator;
+ };
+ const fallback = messages.filter(message => compatible(message, incoming));
+ return fallback.length === 1 && batch.filter(candidate => compatible(fallback[0], candidate)).length === 1 ? fallback[0] : null;
+}
+
+/** Pin unambiguous legacy activity keys before changing display-derived hashes.
+ * This additive cache alias keeps the existing DB event and its ID intact. */
+export function retainLegacyActivityIdentities(messages) {
+ const counts = new Map();
+ for (const message of messages) counts.set(message.externalId, (counts.get(message.externalId) || 0) + 1);
+ for (const message of messages) {
+ if (!message.browserMessageKind && !message.activityIdentity && message.externalId && counts.get(message.externalId) === 1) {
+ message.activityIdentity = message.externalId;
+ }
+ }
+}
diff --git a/server/services/humanActivity.js b/server/services/humanActivity.js
index b1ece68a6f..60209b2c27 100644
--- a/server/services/humanActivity.js
+++ b/server/services/humanActivity.js
@@ -1,3 +1,4 @@
+import { messageIngestionIdentity } from '../lib/messageBrowserIdentity.js';
/**
* Human Activity timeline (#2150) — unified, machine-local event store.
*
@@ -162,7 +163,7 @@ export function messageActivityCandidates(account, messages = []) {
for (const message of messages || []) {
const when = message?.date || message?.happenedAt;
if (!when) continue;
- const externalId = message.externalId || message.id;
+ const externalId = message.activityIdentity || messageIngestionIdentity(message);
if (!externalId) continue;
const fromEmail = participantEmail(message.from);
// Direction: prefer Gmail's authoritative SENT label when the provider supplies
diff --git a/server/services/messagePlaywrightSync.js b/server/services/messagePlaywrightSync.js
index 8e894ccc48..8280ab2c1a 100644
--- a/server/services/messagePlaywrightSync.js
+++ b/server/services/messagePlaywrightSync.js
@@ -1,6 +1,5 @@
-import { readOutlookMessageRow } from '../lib/messageBrowserIdentity.js';
+import { readOutlookMessageRow, browserSummaryId, browserDetailIdentities, messageIngestionIdentity } from '../lib/messageBrowserIdentity.js';
import { join } from 'path';
-import crypto from 'crypto';
import { v4 as uuidv4 } from '../lib/uuid.js';
import { ensureDir, PATHS, safeJSONParse, tryReadFile, atomicWrite } from '../lib/fileUtils.js';
import { isPlainObject } from '../lib/objects.js';
@@ -61,14 +60,6 @@ function outlookRowContext(selector, requireScroll = true) {
return { rows, scrollContainer };
}
-function makeExternalId(date, sender, subject) {
- const hash = crypto.createHash('md5')
- .update(`${date}|${sender}|${subject}`)
- .digest('hex')
- .slice(0, 12);
- return `pw-${hash}`;
-}
-
export async function getSelectors() {
const content = await tryReadFile(SELECTORS_FILE);
if (!content) return {};
@@ -146,7 +137,7 @@ export async function syncPlaywright(account, cache, io, options = {}) {
// Phase 2: Click into each conversation to get full body + thread messages
// Only fetch detail for messages we haven't already cached with full body
- const existingMap = new Map(cache.messages.filter(m => m.externalId && m.bodyFull).map(m => [m.externalId, true]));
+ const fullMessages = cache.messages.filter(m => m.externalId && m.bodyFull);
const messages = [];
let detailsFetched = 0;
@@ -155,6 +146,7 @@ export async function syncPlaywright(account, cache, io, options = {}) {
id: uuidv4(),
externalId: extId,
providerRowId: msg.providerRowId || null,
+ browserMessageKind: 'row',
threadId: null,
from: { name: msg.from || '', email: msg.fromEmail || '' },
to: [], cc: [],
@@ -183,14 +175,19 @@ export async function syncPlaywright(account, cache, io, options = {}) {
for (let i = 0; i < extracted.length; i++) {
const msg = extracted[i];
- const extId = makeExternalId(msg.date || '', msg.from || '', msg.subject || '');
+ const extId = browserSummaryId(msg.date || '', msg.from || '', msg.subject || '');
io?.emit('messages:sync:progress', { accountId: account.id, current: i + 1, total: extracted.length });
// Skip detail fetch if we already have full body cached
- if (existingMap.has(extId)) {
- const m = buildMessage(msg, extId, { threadId: msg.threadKey || null });
- messages.push(m);
- emitMessages([m]);
+ const cached = fullMessages.filter(m => msg.providerRowId
+ ? m.providerRowId === msg.providerRowId && m.browserSummaryExternalId === extId
+ : !m.providerRowId && m.externalId === extId);
+ if (cached.length && (msg.providerRowId || (cached.length === 1
+ && extracted.filter(row => browserSummaryId(row.date || '', row.from || '', row.subject || '') === extId).length === 1))) {
+ const batch = cached.map(m => ({ ...m, isRead: !msg.isUnread, isUnread: msg.isUnread,
+ isPinned: msg.isPinned, isFlagged: msg.isFlagged, isReplied: msg.isReplied }));
+ messages.push(...batch);
+ emitMessages(batch);
continue;
}
@@ -200,15 +197,19 @@ export async function syncPlaywright(account, cache, io, options = {}) {
if (detail?.error) return { messages, inboxComplete: false, status: 'extraction-failed', error: detail.message };
if (detail && detail.length > 0) {
detailsFetched++;
- const threadKey = `thread-${extId}`;
- const batch = detail.map(threadMsg => buildMessage(msg, makeExternalId(threadMsg.date || msg.date || '', threadMsg.from || msg.from || '', msg.subject || ''), {
+ const summary = buildMessage(msg, extId);
+ const threadKey = `thread-${messageIngestionIdentity(summary)}`;
+ const identities = browserDetailIdentities(summary, detail);
+ const batch = detail.map((threadMsg, index) => buildMessage(msg, identities[index].externalId, {
threadId: threadKey,
from: { name: threadMsg.from || msg.from || '', email: threadMsg.fromEmail || msg.fromEmail || '' },
to: threadMsg.to || [],
cc: threadMsg.cc || [],
bodyText: threadMsg.body || msg.preview || '',
bodyFull: true,
- date: threadMsg.date || msg.date || new Date().toISOString()
+ date: threadMsg.date || msg.date || new Date().toISOString(),
+ ...identities[index],
+ browserSummaryExternalId: extId
}));
messages.push(...batch);
emitMessages(batch);
@@ -377,7 +378,8 @@ async function fetchOutlookConversationDetail(page, subject, sender, date, provi
}
}
- results.push({ from, fromEmail, to, cc, date, body });
+ const providerMessageId = container.getAttribute('data-message-id') || container.getAttribute('data-itemid') || null;
+ results.push({ providerMessageId, from, fromEmail, to, cc, date, body });
}
// Fallback: no Email message containers found — try grabbing role="document" directly
diff --git a/server/services/messageSync.js b/server/services/messageSync.js
index 10cd22fb7a..42abad8c6c 100644
--- a/server/services/messageSync.js
+++ b/server/services/messageSync.js
@@ -1,6 +1,6 @@
import { messageLogError } from '../lib/messageLogError.js';
-import { createHash } from 'crypto';
+import { browserSummaryId, browserDetailIdentities, messageIngestionIdentity, matchBrowserMessage, retainLegacyActivityIdentities } from '../lib/messageBrowserIdentity.js';
import { join } from 'path';
import { atomicWrite, ensureDir, filterBySearch as genericFilterBySearch, PATHS, safeDate, safeJSONParse, UUID_RE, tryReadFile } from '../lib/fileUtils.js';
import { getAccount, updateSyncStatus, markSentIngested, updateSendAsAliases } from './messageAccounts.js';
@@ -228,28 +228,20 @@ export async function syncAccount(accountId, io, options = {}) {
// next sync + the outreach detector's own reads.
const sendAsAliases = Array.isArray(providerResult) ? null : (providerResult?.sendAsAliases ?? null);
- // Deduplicate by externalId; update flags and body on existing messages
- const existingMap = new Map(cache.messages.filter(m => m.externalId).map(m => [m.providerRowId ? `${m.externalId}|${m.providerRowId}` : m.externalId, m]));
- const uniqueByExternalId = new Map();
- for (const message of cache.messages) {
- uniqueByExternalId.set(message.externalId, uniqueByExternalId.has(message.externalId) ? null : message);
- }
- const incomingCounts = new Map();
- for (const message of newMessages) {
- incomingCounts.set(message.externalId, (incomingCounts.get(message.externalId) || 0) + 1);
- }
+ // Preserve the DB identity of unambiguous pre-upgrade cache entries.
+ retainLegacyActivityIdentities(cache.messages);
const uniqueNew = [];
for (const msg of newMessages) {
- const identityKey = msg.providerRowId ? `${msg.externalId}|${msg.providerRowId}` : msg.externalId;
- // A missing row ID on either side permits a summary fallback only
- // when both sides are unique; never overwrite a different known row ID.
- const fallback = uniqueByExternalId.get(msg.externalId);
- const canMatchSummary = fallback && incomingCounts.get(msg.externalId) === 1
- && (!msg.providerRowId || !fallback.providerRowId);
- const existing = existingMap.get(identityKey) || (canMatchSummary ? fallback : null);
+ const existing = matchBrowserMessage(cache.messages, msg, newMessages);
if (!msg.externalId || !existing) {
uniqueNew.push(msg);
} else {
+ // A row that becomes one unambiguous child keeps its already-recorded event.
+ existing.activityIdentity ||= messageIngestionIdentity(existing);
+ // Adopt canonical identity without replacing local ID or evaluation.
+ for (const key of ['externalId', 'browserMessageKind', 'providerMessageId', 'browserMemberDisambiguator', 'browserSummaryExternalId', 'from', 'date', 'subject']) {
+ if (msg[key] !== undefined) existing[key] = msg[key];
+ }
// Update flags and provider identity on existing message
if (msg.providerRowId) existing.providerRowId = msg.providerRowId;
if (msg.isUnread !== undefined) existing.isUnread = msg.isUnread;
@@ -383,67 +375,34 @@ export async function syncAccount(accountId, io, options = {}) {
return result;
}
-function mergeRefreshedMessage(cache, existingByExternalId, accountId, source, message, detail) {
- function makeExternalId(date, sender, subject) {
- return 'pw-' + createHash('md5').update(`${date}|${sender}|${subject}`).digest('hex').slice(0, 12);
- }
-
- const threadKey = message.threadId || `thread-${message.externalId || message.id}`;
- const updatedMessages = [];
-
- for (const threadMsg of detail) {
- const extId = makeExternalId(threadMsg.date || message.date || '', threadMsg.from || message.from?.name || '', message.subject || '');
- const existing = existingByExternalId.get(extId);
- if (existing) {
- // Use ?? not ||: a genuinely empty-body message (body === '') is a valid
- // full extraction and must overwrite the old text, not collapse back to it
- // (absent-vs-cleared, AGENTS.md). Only an absent body (null/undefined) keeps
- // the prior text.
- existing.bodyText = threadMsg.body ?? existing.bodyText;
- existing.bodyFull = true;
- if (!existing.threadId) existing.threadId = threadKey;
- if (threadMsg.to?.length) existing.to = threadMsg.to;
- if (threadMsg.cc?.length) existing.cc = threadMsg.cc;
- updatedMessages.push(existing);
- } else {
- const newMsg = {
- id: uuidv4(),
- externalId: extId,
- threadId: threadKey,
- from: { name: threadMsg.from || message.from?.name || '', email: threadMsg.fromEmail || message.from?.email || '' },
- to: threadMsg.to || [],
- cc: threadMsg.cc || [],
- subject: message.subject || '',
- bodyText: threadMsg.body || '',
- bodyFull: true,
- date: threadMsg.date || message.date || new Date().toISOString(),
- // null = read state unknown (Teams cannot measure it) — keep it null, not "read"
- isRead: message.isRead === undefined ? true : message.isRead,
- isUnread: message.isUnread === undefined ? false : message.isUnread,
- isPinned: message.isPinned ?? false,
- isFlagged: message.isFlagged ?? false,
- isReplied: message.isReplied ?? false,
- hasMeetingInvite: message.hasMeetingInvite ?? false,
- labels: [],
- source,
- syncedAt: new Date().toISOString()
- };
- cache.messages.push(newMsg);
- existingByExternalId.set(extId, newMsg);
- updatedMessages.push(newMsg);
- }
- }
-
- // Update the original message too if it wasn't matched by externalId
- if (!updatedMessages.find(m => m.id === message.id)) {
- // ?? not ||: an empty extracted body ('') is a valid clear, not a fall-back
- // to the stale text (absent-vs-cleared, AGENTS.md).
- message.bodyText = detail[0]?.body ?? message.bodyText;
- message.bodyFull = true;
- if (!message.threadId) message.threadId = threadKey;
- updatedMessages.push(message);
- }
-
+function mergeRefreshedMessage(cache, accountId, source, message, detail) {
+ retainLegacyActivityIdentities(cache.messages);
+ const identities = browserDetailIdentities(message, detail);
+ const threadKey = message.threadId || `thread-${messageIngestionIdentity(message)}`;
+ const updatedMessages = identities.map((identity, index) => {
+ const threadMsg = detail[index];
+ const existing = matchBrowserMessage(cache.messages, identity, identities)
+ || (!message.providerRowId && identities.length === 1
+ && browserSummaryId(message.date || '', message.from?.name || '', message.subject || '') === identity.externalId
+ ? message : null);
+ // API IDs remain authoritative; a browser refresh supplies content only.
+ if (existing?.externalId && !existing.providerRowId && !identity.providerRowId) identity.externalId = existing.externalId;
+ const updated = existing || {
+ id: uuidv4(), labels: [], source, isRead: message.isRead === undefined ? true : message.isRead,
+ isUnread: message.isUnread === undefined ? false : message.isUnread, isPinned: message.isPinned ?? false,
+ isFlagged: message.isFlagged ?? false, isReplied: message.isReplied ?? false,
+ hasMeetingInvite: message.hasMeetingInvite ?? false, subject: message.subject || ''
+ };
+ if (existing) updated.activityIdentity ||= messageIngestionIdentity(existing);
+ Object.assign(updated, identity, {
+ bodyText: threadMsg.body ?? updated.bodyText ?? '', bodyFull: true,
+ threadId: threadKey, to: threadMsg.to || updated.to || [], cc: threadMsg.cc || updated.cc || [],
+ syncedAt: new Date().toISOString()
+ });
+ if (!existing) cache.messages.push(updated);
+ return updated;
+ });
+ // Do not copy an arbitrary child's body onto an unmatched conversation row.
return updatedMessages.map(m => ({ ...m, accountId }));
}
@@ -463,7 +422,6 @@ export async function refreshMessages(accountId, messageIds) {
const { refreshMessageDetail } = await import('./messagePlaywrightSync.js');
const messagesById = new Map(cache.messages.map(message => [message.id, message]));
- const existingByExternalId = new Map(cache.messages.filter(message => message.externalId).map(message => [message.externalId, message]));
const results = [];
let updated = 0;
@@ -488,7 +446,7 @@ export async function refreshMessages(accountId, messageIds) {
continue;
}
- results.push(mergeRefreshedMessage(cache, existingByExternalId, accountId, account.type, message, detail));
+ results.push(mergeRefreshedMessage(cache, accountId, account.type, message, detail));
updated++;
}
diff --git a/server/services/messageSync.test.js b/server/services/messageSync.test.js
index 84eb59920d..35056c0264 100644
--- a/server/services/messageSync.test.js
+++ b/server/services/messageSync.test.js
@@ -52,6 +52,9 @@ vi.mock('./messageGmailSync.js', () => ({
syncGmail: vi.fn()
}));
+const browser = vi.hoisted(() => ({ findOrOpenPage: vi.fn(), evaluateOnPage: vi.fn(), isAuthPage: vi.fn(() => false), listCdpPages: vi.fn() }));
+vi.mock('./browserService.js', () => browser);
+
vi.mock('./messagePlaywrightSync.js', () => ({
syncPlaywright: vi.fn(),
refreshMessageDetail: vi.fn()
@@ -833,9 +836,8 @@ describe('refreshMessage', () => {
expect(merged.bodyText).toBe('keep me');
});
- it('clears the original message body when the extracted body is empty (fallback path)', async () => {
- // No externalId on the cached message → it is not matched in the loop and
- // falls through to the original-message update (line ~284).
+ it('preserves an unmatched original rather than borrowing another conversation member body', async () => {
+ // Different member evidence must not clear an unrelated legacy record.
readFile.mockResolvedValue(JSON.stringify({
syncCursor: null,
messages: [{ id: 'msg-1', bodyText: 'stale original', from: { name: 'Bob' }, subject: 'Re: x', date: '2026-02-01' }]
@@ -845,8 +847,8 @@ describe('refreshMessage', () => {
const result = await refreshMessage(VALID_UUID, 'msg-1');
- const original = result.find(m => m.id === 'msg-1');
- expect(original.bodyText).toBe('');
+ expect(result.every(m => m.id !== 'msg-1')).toBe(true);
+ expect(atomicWrite.mock.calls[0][1].messages.find(m => m.id === 'msg-1').bodyText).toBe('stale original');
});
it('loads and saves the cache once when refreshing multiple messages', async () => {
@@ -1097,3 +1099,111 @@ describe('full inbox coverage contract', () => {
expect(saved.messages).toHaveLength(3);
});
});
+
+
+describe('browser ingestion identity across sync, cache, and activity (#11150)', () => {
+ async function fixture(initial = []) {
+ let stored = { messages: initial };
+ const events = new Map();
+ const { recordEvents, messageActivityCandidates } = await import('./humanActivity.js');
+ const account = { id: VALID_UUID, type: 'outlook', enabled: true };
+ for (const candidate of messageActivityCandidates(account, initial.map(m => ({ ...m, activityIdentity: m.externalId })))) events.set(candidate.dedupeKey, candidate);
+ recordEvents.mockImplementation(async candidates => {
+ for (const candidate of candidates) if (!events.has(candidate.dedupeKey)) events.set(candidate.dedupeKey, candidate);
+ });
+ readFile.mockImplementation(async path => path.endsWith('selectors.json') ? '{}' : JSON.stringify(stored));
+ atomicWrite.mockImplementation(async (_path, data) => { stored = structuredClone(data); });
+ getAccount.mockResolvedValue(account);
+ browser.findOrOpenPage.mockResolvedValue({ url: 'https://outlook.office.com/mail/' });
+ const actual = await vi.importActual('./messagePlaywrightSync.js');
+ syncPlaywright.mockImplementation(actual.syncPlaywright);
+ return { events, stored: () => stored };
+ }
+
+ const summary = { from: 'Example Sender', fromEmail: '[email protected]', subject: 'Same subject', date: '2026-01-01T12:00:00Z', isUnread: true };
+ function scrape(rows, details) {
+ browser.evaluateOnPage.mockImplementation(async (_page, script) => {
+ if (script.includes('const seen = new Map()')) return rows;
+ if (script.includes('const targetId =')) return details ? true : { found: false };
+ return details;
+ });
+ }
+
+ it('keeps colliding rows distinct and replays mutable summaries without losing local evaluations or activity identity', async () => {
+ const f = await fixture();
+ scrape([{ ...summary, providerRowId: 'row-a' }, { ...summary, providerRowId: 'row-b' }]);
+ await syncAccount(VALID_UUID);
+ expect(f.stored().messages).toHaveLength(2);
+ expect(f.events.size).toBe(2);
+ const original = f.stored().messages.find(m => m.providerRowId === 'row-a');
+ original.evaluation = { score: 9 };
+ scrape([{ ...summary, providerRowId: 'row-a', from: 'Renamed Sender', date: '2026-01-01T07:00:00-05:00' }, { ...summary, providerRowId: 'row-b' }]);
+ await syncAccount(VALID_UUID);
+ await syncAccount(VALID_UUID);
+ expect(f.stored().messages).toHaveLength(2);
+ expect(f.stored().messages.find(m => m.providerRowId === 'row-a')).toMatchObject({ id: original.id, evaluation: { score: 9 }, from: { name: 'Renamed Sender' } });
+ expect(f.events.size).toBe(2);
+ });
+
+ it('retains the event key and local evaluation of an unambiguous legacy row', async () => {
+ const f = await fixture([{ id: 'legacy-local', externalId: 'pw-legacy', providerRowId: 'row-a', from: { name: summary.from }, date: summary.date, subject: summary.subject, evaluation: { score: 8 } }]);
+ scrape([{ ...summary, providerRowId: 'row-a', from: 'New display name' }]);
+ await syncAccount(VALID_UUID);
+ await syncAccount(VALID_UUID);
+ expect(f.stored().messages).toHaveLength(1);
+ expect(f.stored().messages[0]).toMatchObject({ id: 'legacy-local', evaluation: { score: 8 }, activityIdentity: 'pw-legacy' });
+ expect([...f.events.keys()]).toEqual([`msg:${VALID_UUID}:pw-legacy`]);
+ });
+
+ it('upgrades a summary to its unambiguous child without adding a second activity event', async () => {
+ const f = await fixture();
+ scrape([{ ...summary, providerRowId: 'row-a' }]);
+ await syncAccount(VALID_UUID);
+ const original = f.stored().messages[0];
+ original.evaluation = { score: 7 };
+ const details = [
+ { from: 'Renamed Sender', fromEmail: summary.fromEmail, date: '2026-01-01T07:00:00-05:00', body: 'Current message' },
+ { from: summary.from, fromEmail: summary.fromEmail, date: '2025-12-31T12:00:00Z', body: 'Earlier message' }
+ ];
+ scrape([{ ...summary, providerRowId: 'row-a' }], details);
+ await syncAccount(VALID_UUID);
+ await syncAccount(VALID_UUID);
+ expect(f.stored().messages).toHaveLength(2);
+ expect(f.stored().messages.find(m => m.id === original.id)).toMatchObject({ bodyText: 'Current message', evaluation: { score: 7 }, browserMessageKind: 'member' });
+ expect(f.events.size).toBe(2);
+ });
+
+ it('retains every expanded member on cached replay and isolates refresh bodies by provider row', async () => {
+ const f = await fixture();
+ const details = [
+ { from: summary.from, fromEmail: summary.fromEmail, date: summary.date, body: 'First body', providerMessageId: 'child-a' },
+ { from: summary.from, fromEmail: summary.fromEmail, date: summary.date, body: 'Second body', providerMessageId: 'child-b' }
+ ];
+ scrape([{ ...summary, providerRowId: 'row-a' }, { ...summary, providerRowId: 'row-b' }], details);
+ await syncAccount(VALID_UUID);
+ expect(f.stored().messages).toHaveLength(4);
+ expect(f.events.size).toBe(4);
+ await syncAccount(VALID_UUID);
+ expect(f.stored().messages).toHaveLength(4);
+ expect(f.events.size).toBe(4);
+ const first = f.stored().messages.find(m => m.providerRowId === 'row-a');
+ refreshMessageDetail.mockResolvedValue(details.map(d => ({ ...d, body: 'Refreshed ' + d.body })));
+ await refreshMessage(VALID_UUID, first.id);
+ expect(f.stored().messages.filter(m => m.providerRowId === 'row-a').map(m => m.bodyText)).toEqual(['Refreshed First body', 'Refreshed Second body']);
+ expect(f.stored().messages.filter(m => m.providerRowId === 'row-b').map(m => m.bodyText)).toEqual(['First body', 'Second body']);
+ });
+
+ it('preserves ambiguous no-child-ID members rather than overwriting one with another', async () => {
+ const f = await fixture();
+ const details = ['Body one', 'Body two'].map(body => ({ from: summary.from, fromEmail: summary.fromEmail, date: summary.date, body }));
+ scrape([{ ...summary, providerRowId: 'row-a' }], details);
+ await syncAccount(VALID_UUID);
+ await syncAccount(VALID_UUID);
+ expect(f.stored().messages.map(m => m.bodyText)).toEqual(['Body one', 'Body two']);
+ expect(f.events.size).toBe(2);
+ const first = f.stored().messages[0];
+ refreshMessageDetail.mockResolvedValue([details[0], { ...details[1], body: 'Changed ambiguous body' }]);
+ await refreshMessage(VALID_UUID, first.id);
+ expect(f.stored().messages.map(m => m.bodyText)).toEqual(['Body one', 'Body two', 'Changed ambiguous body']);
+ });
+});