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: honor saved Outlook row selectors during sync and refresh (#11021)

Adam Eivyc37c3fb15973
diff --git a/server/services/messagePlaywrightSync.js b/server/services/messagePlaywrightSync.js
index 183966defc..ade3060288 100644
--- a/server/services/messagePlaywrightSync.js
+++ b/server/services/messagePlaywrightSync.js
@@ -21,6 +21,42 @@ const SELECTORS_FILE = join(PATHS.messages, 'selectors.json');
const OUTLOOK_URL = 'https://outlook.office.com/mail/';
const TEAMS_URL = 'https://teams.microsoft.com/';
+const DEFAULT_OUTLOOK_ROW_SELECTOR = "[role='listbox'] [role='option']";
+const outlookRowSelector = sels => sels.messageRow ?? DEFAULT_OUTLOOK_ROW_SELECTOR;
+
+// Serialized into the provider page for Test, summary extraction and detail lookup.
+// Selectors always address the document, including on subsequent virtualized paints.
+function outlookRowContext(selector, requireScroll = true) {
+ const rows = () => Array.from(document.querySelectorAll(selector));
+ let initialRows;
+ try {
+ if (typeof selector !== 'string' || !selector.trim()) throw new Error('Empty selector');
+ initialRows = rows();
+ } catch {
+ return { error: 'invalid-selector', message: 'Invalid Outlook messageRow selector. Edit it in Messages > Sync and test again.' };
+ }
+ if (!requireScroll || initialRows.length === 0) return { rows };
+
+ const first = initialRows[0];
+ const list = first.closest('[role="listbox"], [role="list"]');
+ let scrollContainer = first.closest('[role="region"]') || list?.parentElement;
+ // Custom layouts may have an ordinary overflow container instead of ARIA roles.
+ if (!scrollContainer) {
+ for (let parent = first.parentElement; parent; parent = parent.parentElement) {
+ if (parent.scrollHeight > parent.clientHeight && /auto|scroll/.test(getComputedStyle(parent).overflowY)) {
+ scrollContainer = parent;
+ break;
+ }
+ }
+ }
+ if (!scrollContainer || typeof scrollContainer.scrollBy !== 'function'
+ || typeof scrollContainer.scrollTo !== 'function'
+ || initialRows.some(row => !scrollContainer.contains(row))) {
+ return { error: 'invalid-container', message: 'Outlook messageRow must match rows in one scrollable list or region. Edit it in Messages > Sync and test again.' };
+ }
+ return { rows, scrollContainer };
+}
+
function makeExternalId(date, sender, subject) {
const hash = crypto.createHash('md5')
.update(`${date}|${sender}|${subject}`)
@@ -93,6 +129,9 @@ export async function syncPlaywright(account, cache, io, options = {}) {
const extractScript = buildExtractionScript(account.type, sels, mode);
const extracted = await evaluateOnPage(page, extractScript);
+ if (extracted?.error) {
+ return { messages: [], status: 'extraction-failed', error: extracted.message };
+ }
if (!extracted || !Array.isArray(extracted)) {
console.log(`📧 No messages extracted from ${account.type} page`);
io?.emit('messages:sync:progress', { accountId: account.id, current: 0, total: 0 });
@@ -153,7 +192,8 @@ export async function syncPlaywright(account, cache, io, options = {}) {
// Click into conversation to get full body + thread
if (account.type === 'outlook') {
- const detail = await fetchOutlookConversationDetail(page, msg.subject, msg.from, msg.date, msg.providerRowId);
+ const detail = await fetchOutlookConversationDetail(page, msg.subject, msg.from, msg.date, msg.providerRowId, outlookRowSelector(sels));
+ if (detail?.error) return { messages, inboxComplete: false, status: 'extraction-failed', error: detail.message };
if (detail && detail.length > 0) {
detailsFetched++;
const threadKey = `thread-${extId}`;
@@ -197,43 +237,47 @@ export async function syncPlaywright(account, cache, io, options = {}) {
* > h3[aria-label^="Cc:"] (cc)
* Returns an array of { from, fromEmail, to, cc, date, body } for each message in the thread.
*/
-async function fetchOutlookConversationDetail(page, subject, sender, date, providerRowId) {
+async function fetchOutlookConversationDetail(page, subject, sender, date, providerRowId, rowSelector) {
// Preserve the same provider row identity when loading detail from a virtualized list.
const safeSubject = JSON.stringify(subject || '');
const safeSender = JSON.stringify(sender || '');
const clickResult = await evaluateOnPage(page, `
(async function() {
- const listbox = document.querySelector("[role='listbox']");
- if (!listbox) return { found: false, hasListbox: false };
+ const context = (${outlookRowContext.toString()})(${JSON.stringify(rowSelector)});
+ if (context.error) return context;
+ if (!context.scrollContainer) return { found: false };
const targetSubject = ${safeSubject};
const targetSender = ${safeSender};
const targetDate = ${JSON.stringify(date || '')};
const targetId = ${JSON.stringify(providerRowId || null)};
const readRow = ${readOutlookMessageRow.toString()};
- const scrollContainer = listbox.closest('[role="region"]') || listbox.parentElement;
+ const { scrollContainer } = context;
- function findMatch() {
- const matches = [...listbox.querySelectorAll('[role="option"]')].filter(row => {
+ function findMatches() {
+ return context.rows().filter(row => {
const data = readRow(row);
if (targetId) return data.providerRowId === targetId;
return targetSubject && targetSender && targetDate
&& data.subject === targetSubject && data.from === targetSender && data.date === targetDate;
});
- return matches.length === 1 ? matches[0] : null;
}
- // Check visible rows first, then scroll to find the message
- let matched = findMatch();
+ // Refuse ambiguity immediately; scrolling must not turn it into a unique click.
+ let matches = findMatches();
+ if (matches.length > 1) return { found: false };
+ let matched = matches[0];
if (!matched && scrollContainer) {
const maxScroll = 30;
for (let i = 0; i < maxScroll; i++) {
scrollContainer.scrollBy(0, 600);
await new Promise(r => setTimeout(r, 300));
- matched = findMatch();
+ matches = findMatches();
+ if (matches.length > 1) return { found: false };
+ matched = matches[0];
if (matched) break;
}
}
- if (!matched) return { found: false, hasListbox: true };
+ if (!matched) return { found: false };
matched.scrollIntoView({ block: 'center' });
await new Promise(r => setTimeout(r, 200));
var urlBefore = location.href;
@@ -251,8 +295,9 @@ async function fetchOutlookConversationDetail(page, subject, sender, date, provi
})()
`);
+ if (clickResult?.error) return clickResult;
if (clickResult && typeof clickResult === 'object' && !clickResult.found) {
- console.log(`📧 Detail click: message not found (listbox=${clickResult.hasListbox})`);
+ console.log('📧 Detail click: message not uniquely found');
return null;
}
if (!clickResult) {
@@ -361,8 +406,9 @@ function buildExtractionScript(type, sels, mode = 'unread') {
// Scrolling extraction: scrapes visible rows, scrolls, repeats
return `
(async function() {
- const listbox = document.querySelector("[role='listbox']");
- if (!listbox) return [];
+ const context = (${outlookRowContext.toString()})(${JSON.stringify(outlookRowSelector(sels))});
+ if (context.error) return context;
+ if (!context.scrollContainer) return [];
const seen = new Map();
let scrollAttempts = 0;
const maxMsg = ${maxMessages};
@@ -372,7 +418,7 @@ function buildExtractionScript(type, sels, mode = 'unread') {
const extractRow = ${readOutlookMessageRow.toString()};
function scrapeVisible() {
- const rows = listbox.querySelectorAll('[role="option"]');
+ const rows = context.rows();
let added = 0;
for (const row of rows) {
if (seen.size >= maxMsg) break;
@@ -388,7 +434,7 @@ function buildExtractionScript(type, sels, mode = 'unread') {
}
scrapeVisible();
- const scrollContainer = listbox.closest('[role="region"]') || listbox.parentElement;
+ const { scrollContainer } = context;
while (scrollAttempts < maxScroll && seen.size < maxMsg) {
scrollContainer.scrollBy(0, 600);
await new Promise(r => setTimeout(r, 500));
@@ -454,7 +500,8 @@ export async function refreshMessageDetail(account, message) {
}
console.log(`📧 Refresh: clicking into ${message.id}`);
- const detail = await fetchOutlookConversationDetail(page, message.subject, message.from?.name, message.date, message.providerRowId);
+ const detail = await fetchOutlookConversationDetail(page, message.subject, message.from?.name, message.date, message.providerRowId, outlookRowSelector((await getSelectors()).outlook || {}));
+ if (detail?.error) return detail;
if (!detail) {
console.log(`📧 Refresh: click/extraction failed for ${message.id}`);
} else {
@@ -599,10 +646,16 @@ export async function testSelectors(provider) {
const results = {};
for (const [name, selector] of Object.entries(sels)) {
- const count = await evaluateOnPage(page,
- `document.querySelectorAll(${JSON.stringify(selector)}).length`
- );
- results[name] = { selector, matches: count ?? 0 };
+ const effective = provider === 'outlook' && name === 'messageRow' ? outlookRowSelector(sels) : selector;
+ const script = provider === 'outlook' && name === 'messageRow'
+ ? `(function() {
+ const context = (${outlookRowContext.toString()})(${JSON.stringify(effective)}, false);
+ return context.error ? context : context.rows().length;
+ })()`
+ : `document.querySelectorAll(${JSON.stringify(effective)}).length`;
+ const count = await evaluateOnPage(page, script);
+ results[name] = { selector: effective, matches: typeof count === 'number' ? count : 0,
+ ...(count?.error ? { error: count.message } : {}) };
}
const entries = Object.values(results);
diff --git a/server/services/messagePlaywrightSync.test.js b/server/services/messagePlaywrightSync.test.js
index 9862e93ccf..e7dd272afe 100644
--- a/server/services/messagePlaywrightSync.test.js
+++ b/server/services/messagePlaywrightSync.test.js
@@ -13,7 +13,7 @@ vi.mock('../lib/fileUtils.js', async () => {
return { ...actual, tryReadFile };
});
-const { testSelectors, syncPlaywright, sendPlaywright } = await import('./messagePlaywrightSync.js');
+const { testSelectors, syncPlaywright, sendPlaywright, refreshMessageDetail } = await import('./messagePlaywrightSync.js');
const OPEN_PAGE = { url: 'https://outlook.office.com/mail/', webSocketDebuggerUrl: 'ws://x' };
@@ -129,6 +129,140 @@ it('records Teams read state as unknown for both sync modes', async () => {
expect(scripts[0]).toBe(scripts[1]);
});
+// These fixtures expose only the configured document selector, run the actual
+// production row reader and browser scripts, and model virtualized paints/clicks.
+describe('Outlook configured row extraction', () => {
+ const account = { id: 'example-account', type: 'outlook' };
+ const defaultSelector = "[role='listbox'] [role='option']";
+ const customSelector = '.example-mail[data-label="quoted\\value"]';
+ const record = (id, isUnread = true) => ({ providerRowId: id, isUnread,
+ from: 'Example Sender', subject: 'Example subject', date: '2026-10-10', preview: 'Example preview' });
+
+ function mailbox(paints, { selector = customSelector, container = 'region' } = {}) {
+ let paint = 0;
+ let opened = false;
+ const clicked = [];
+ const attr = values => ({ getAttribute: name => values[name] ?? null });
+ const span = (text, title) => ({ ...attr({ title }), textContent: text });
+ const scroll = {
+ scrollHeight: 1000, clientHeight: 100,
+ contains: () => true,
+ scrollBy: vi.fn(() => { paint = Math.min(paint + 1, paints.length - 1); }),
+ scrollTo: vi.fn(() => { paint = 0; })
+ };
+ const list = { parentElement: scroll };
+ const rows = paints.map(records => records.map(data => ({
+ ...attr({ 'data-itemid': data.providerRowId }), parentElement: container === 'overflow' ? scroll : null,
+ closest: sel => container === 'region' && sel === '[role="region"]' ? scroll
+ : container === 'list' && sel.includes('[role="listbox"]') ? list : null,
+ scrollIntoView() {},
+ click() { clicked.push(data.providerRowId); opened = true; },
+ querySelector(sel) {
+ if (sel === 'button[aria-label="Mark as read"]') return data.isUnread ? {} : null;
+ if (sel === 'div[aria-label="Select a conversation"] > span[aria-label]') return attr({ 'aria-label': data.from });
+ if (sel === 'div[aria-label="Select a conversation"]') return { parentElement: { nextElementSibling: { children: [
+ { querySelector: () => attr({ title: '[email protected]' }) },
+ { querySelectorAll: () => [span(data.subject), span(data.date, data.date)] }, span(data.preview)
+ ] } } };
+ return null;
+ }
+ })));
+ const body = { innerText: 'Example full body' };
+ const pane = {
+ innerText: 'Example subject', querySelector: () => body,
+ querySelectorAll: sel => sel === '[aria-label="Email message"]' ? [] : [body]
+ };
+ const document = {
+ querySelector: sel => opened && sel === 'main[aria-label="Reading Pane"]' ? pane : null,
+ querySelectorAll(sel) {
+ if (sel === '[' || sel === '') throw new SyntaxError('Invalid selector');
+ return sel === selector ? rows[paint] : [];
+ }
+ };
+ evaluateOnPage.mockImplementation((_page, script) => vm.runInNewContext(script, {
+ document, location: { href: 'https://example.com/mail/' },
+ getComputedStyle: () => ({ overflowY: 'auto' }), setTimeout: callback => callback()
+ }));
+ return { clicked, scroll };
+ }
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ evaluateOnPage.mockReset();
+ findOrOpenPage.mockResolvedValue(OPEN_PAGE);
+ listCdpPages.mockResolvedValue([OPEN_PAGE]);
+ isAuthPage.mockReturnValue(false);
+ tryReadFile.mockResolvedValue(JSON.stringify({ outlook: { messageRow: customSelector } }));
+ });
+
+ it('uses the tested override for sync detail and refresh, preserving the exact provider row ID', async () => {
+ // Identical subject/sender/date must not pull the other conversation's detail.
+ const { clicked } = mailbox([[record('first'), record('second')]]);
+ expect(await testSelectors('outlook')).toMatchObject({ status: 'ok', results: { messageRow: { matches: 2 } } });
+ const result = await syncPlaywright(account, { messages: [] });
+ expect(result).toMatchObject({ status: 'success', inboxComplete: false });
+ expect(result.messages.map(m => m.providerRowId)).toEqual(['first', 'second']);
+ expect(result.messages.every(m => m.bodyFull && m.bodyText === 'Example full body')).toBe(true);
+ expect(clicked).toEqual(['first', 'second']);
+ expect(await refreshMessageDetail(account, result.messages[1])).toEqual([
+ expect.objectContaining({ body: 'Example full body' })
+ ]);
+ expect(clicked).toEqual(['first', 'second', 'second']);
+ });
+
+ it.each(['region', 'list', 'overflow'])('retains bounded full/unread virtualized scrolling with a %s container', async container => {
+ for (const selector of [defaultSelector, customSelector]) {
+ tryReadFile.mockResolvedValue(selector === defaultSelector ? '{}' : JSON.stringify({ outlook: { messageRow: selector } }));
+ for (const mode of ['unread', 'full']) {
+ mailbox([[record('unread')], [record('read', false)]], { selector, container });
+ // First get IDs at the service boundary, then use that cache to isolate list scrolling.
+ const cache = { messages: [] };
+ const first = await syncPlaywright(account, cache, null, { mode });
+ cache.messages = first.messages;
+ const { scroll } = mailbox([[record('unread')], [record('read', false)]], { selector, container });
+ const result = await syncPlaywright(account, cache, null, { mode });
+ expect(result).toMatchObject({ status: 'success', inboxComplete: false });
+ expect(result.messages.map(m => m.providerRowId)).toEqual(mode === 'full' ? ['unread', 'read'] : ['unread']);
+ expect(result.messages.map(m => m.isRead)).toEqual(mode === 'full' ? [false, true] : [false]);
+ expect(scroll.scrollBy).toHaveBeenCalledTimes(mode === 'full' ? 21 : 10);
+ expect(scroll.scrollTo).toHaveBeenLastCalledWith(0, 0);
+ }
+ }
+ });
+
+ it('caps full/unread extraction even when more rows are visible', async () => {
+ for (const [mode, cap] of [['unread', 100], ['full', 200]]) {
+ const { scroll } = mailbox([Array.from({ length: 210 }, (_, i) => record(`row-${i}`))]);
+ const result = await syncPlaywright(account, { messages: [] }, null, { mode });
+ expect(result.messages).toHaveLength(cap);
+ expect(result.inboxComplete).toBe(false);
+ expect(scroll.scrollBy).not.toHaveBeenCalled();
+ }
+ });
+
+ it('reports invalid selectors in Test, sync and refresh instead of using default rows', async () => {
+ mailbox([[record('unrelated')]], { selector: defaultSelector });
+ tryReadFile.mockResolvedValue(JSON.stringify({ outlook: { messageRow: '[' } }));
+ expect(await testSelectors('outlook')).toMatchObject({ status: 'partial', results: { messageRow: { matches: 0, error: expect.stringContaining('Invalid Outlook') } } });
+ expect(await syncPlaywright(account, { messages: [] })).toMatchObject({ status: 'extraction-failed', messages: [], error: expect.stringContaining('Messages > Sync') });
+ expect(await refreshMessageDetail(account, { providerRowId: 'unrelated' })).toMatchObject({ error: 'invalid-selector' });
+ });
+
+ it('reports unusable containers rather than reporting a successful empty inbox', async () => {
+ const { clicked } = mailbox([[record('first')]], { container: 'none' });
+ expect(await syncPlaywright(account, { messages: [] })).toMatchObject({ status: 'extraction-failed', error: expect.stringContaining('scrollable list') });
+ expect(await refreshMessageDetail(account, { providerRowId: 'first' })).toMatchObject({ error: 'invalid-container' });
+ expect(clicked).toEqual([]);
+ });
+
+ it('refuses ambiguous detail immediately even if scrolling would leave a unique target', async () => {
+ const { clicked, scroll } = mailbox([[record('duplicate'), record('duplicate')], [record('duplicate')]]);
+ expect(await refreshMessageDetail(account, { providerRowId: 'duplicate' })).toBeNull();
+ expect(clicked).toEqual([]);
+ expect(scroll.scrollBy).not.toHaveBeenCalled();
+ });
+});
+
// Browser-delivered drafts. evaluateOnPage is scripted per compose phase, so the
// whole success / failure / ambiguity matrix runs with no mailbox and no recipient.
describe('sendPlaywright', () => {