Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

fix(gmail): match replacements to compatible observed slots and count only top-level thread containers

Emre Sokulluba9c82a11e88
diff --git a/src/chrome/src/agent/message-recipient-guard.js b/src/chrome/src/agent/message-recipient-guard.js
index 4fc9997df..ec17f96c6 100644
--- a/src/chrome/src/agent/message-recipient-guard.js
+++ b/src/chrome/src/agent/message-recipient-guard.js
@@ -449,6 +449,21 @@ export function resolveClarifiedRecipients(observedCandidates, previousTarget, c
.map((r, idx) => ({ ...r, idx }))
.filter(r => !matchedPreviousIndices.has(r.idx));
+ // Match replacements to compatible observed recipient slots first to avoid
+ // swapping roles when multiple recipients with different roles are replaced.
+ for (let i = 0; i < rawList.length; i++) {
+ if (candidatePreviousRoles.has(i)) continue;
+ const candidate = rawList[i];
+ const legacy = typeof candidate === 'string' || typeof candidate === 'number';
+ const observedRole = compact(legacy ? 'to' : candidate?.role, 12).toLowerCase() || 'to';
+ const matchIdx = remainingPrevious.findIndex(r => r.role === observedRole);
+ if (matchIdx !== -1) {
+ const [matched] = remainingPrevious.splice(matchIdx, 1);
+ candidatePreviousRoles.set(i, matched.role);
+ }
+ }
+
+ // Assign any remaining unmatched replacement candidates by restrictive rank (bcc > cc > to).
const roleRank = { bcc: 3, cc: 2, to: 1 };
remainingPrevious.sort((a, b) => (roleRank[b.role] || 0) - (roleRank[a.role] || 0));
diff --git a/src/chrome/src/content/accessibility-tree.js b/src/chrome/src/content/accessibility-tree.js
index 8eb773994..7b881a054 100644
--- a/src/chrome/src/content/accessibility-tree.js
+++ b/src/chrome/src/content/accessibility-tree.js
@@ -1285,10 +1285,20 @@
function isSingleMessageGmailThread(conversationRoot) {
if (!conversationRoot || typeof conversationRoot.querySelectorAll !== 'function') return false;
try {
- const gmailMessages = conversationRoot.querySelectorAll('.adn,.ads,[data-message-id]');
- if (gmailMessages.length) return gmailMessages.length === 1;
- const semanticMessages = conversationRoot.querySelectorAll('[role="article"]');
- if (semanticMessages.length) return semanticMessages.length === 1;
+ const gmailMessages = Array.from(conversationRoot.querySelectorAll('.adn,.ads,[data-message-id]'));
+ if (gmailMessages.length) {
+ const topLevelGmailMessages = gmailMessages.filter(msg => (
+ !msg.closest?.('.a3s,.ii') && (!msg.parentElement || !msg.parentElement.closest?.('.adn,.ads,[data-message-id]'))
+ ));
+ return topLevelGmailMessages.length === 1;
+ }
+ const semanticMessages = Array.from(conversationRoot.querySelectorAll('[role="article"]'));
+ if (semanticMessages.length) {
+ const topLevelSemantic = semanticMessages.filter(msg => (
+ !msg.closest?.('.a3s,.ii') && (!msg.parentElement || !msg.parentElement.closest?.('[role="article"]'))
+ ));
+ return topLevelSemantic.length === 1;
+ }
} catch (e) {}
return false;
}
diff --git a/src/firefox/src/agent/message-recipient-guard.js b/src/firefox/src/agent/message-recipient-guard.js
index 4fc9997df..ec17f96c6 100644
--- a/src/firefox/src/agent/message-recipient-guard.js
+++ b/src/firefox/src/agent/message-recipient-guard.js
@@ -449,6 +449,21 @@ export function resolveClarifiedRecipients(observedCandidates, previousTarget, c
.map((r, idx) => ({ ...r, idx }))
.filter(r => !matchedPreviousIndices.has(r.idx));
+ // Match replacements to compatible observed recipient slots first to avoid
+ // swapping roles when multiple recipients with different roles are replaced.
+ for (let i = 0; i < rawList.length; i++) {
+ if (candidatePreviousRoles.has(i)) continue;
+ const candidate = rawList[i];
+ const legacy = typeof candidate === 'string' || typeof candidate === 'number';
+ const observedRole = compact(legacy ? 'to' : candidate?.role, 12).toLowerCase() || 'to';
+ const matchIdx = remainingPrevious.findIndex(r => r.role === observedRole);
+ if (matchIdx !== -1) {
+ const [matched] = remainingPrevious.splice(matchIdx, 1);
+ candidatePreviousRoles.set(i, matched.role);
+ }
+ }
+
+ // Assign any remaining unmatched replacement candidates by restrictive rank (bcc > cc > to).
const roleRank = { bcc: 3, cc: 2, to: 1 };
remainingPrevious.sort((a, b) => (roleRank[b.role] || 0) - (roleRank[a.role] || 0));
diff --git a/src/firefox/src/content/accessibility-tree.js b/src/firefox/src/content/accessibility-tree.js
index 8eb773994..7b881a054 100644
--- a/src/firefox/src/content/accessibility-tree.js
+++ b/src/firefox/src/content/accessibility-tree.js
@@ -1285,10 +1285,20 @@
function isSingleMessageGmailThread(conversationRoot) {
if (!conversationRoot || typeof conversationRoot.querySelectorAll !== 'function') return false;
try {
- const gmailMessages = conversationRoot.querySelectorAll('.adn,.ads,[data-message-id]');
- if (gmailMessages.length) return gmailMessages.length === 1;
- const semanticMessages = conversationRoot.querySelectorAll('[role="article"]');
- if (semanticMessages.length) return semanticMessages.length === 1;
+ const gmailMessages = Array.from(conversationRoot.querySelectorAll('.adn,.ads,[data-message-id]'));
+ if (gmailMessages.length) {
+ const topLevelGmailMessages = gmailMessages.filter(msg => (
+ !msg.closest?.('.a3s,.ii') && (!msg.parentElement || !msg.parentElement.closest?.('.adn,.ads,[data-message-id]'))
+ ));
+ return topLevelGmailMessages.length === 1;
+ }
+ const semanticMessages = Array.from(conversationRoot.querySelectorAll('[role="article"]'));
+ if (semanticMessages.length) {
+ const topLevelSemantic = semanticMessages.filter(msg => (
+ !msg.closest?.('.a3s,.ii') && (!msg.parentElement || !msg.parentElement.closest?.('[role="article"]'))
+ ));
+ return topLevelSemantic.length === 1;
+ }
} catch (e) {}
return false;
}
diff --git a/test/run.js b/test/run.js
index b2a528eea..cbb7d33bf 100644
--- a/test/run.js
+++ b/test/run.js
@@ -5012,6 +5012,28 @@ test('direct-message recipient guard uses structured intent and exact active ide
],
'answer with negated role for Alice and affirmative role for Bob must preserve Alice as BCC and Bob as To',
);
+ assert.deepEqual(
+ helper.resolveClarifiedRecipients(
+ [
+ { identity: '[email protected]', role: 'to', aliases: ['[email protected]', 'Bob'] },
+ { identity: '[email protected]', role: 'bcc', aliases: ['[email protected]', 'Dan'] },
+ ],
+ {
+ target_kind: 'named',
+ recipients: [
+ { identity: '[email protected]', role: 'to' },
+ { identity: '[email protected]', role: 'bcc' },
+ ],
+ },
+ null,
+ 'Bob and Dan',
+ ),
+ [
+ { identity: '[email protected]', role: 'to' },
+ { identity: '[email protected]', role: 'bcc' },
+ ],
+ 'replacement recipients must match compatible observed slots instead of swapping roles',
+ );
assert.equal(helper.messageTargetMatchesObservedIdentities(
{ target_kind: 'named', recipients: ['Alice', '[email protected]'] },
@@ -11112,6 +11134,7 @@ test('accessibility-tree schema and prompts preserve exact whole-document contin
assert.match(chromeSource, /function gmailConversationExpansionControlState\(control\) \{[\s\S]*?jsname === 'xvWlrc'[\s\S]*?jsname === 'tRarif'[\s\S]*?name === 'Collapse all'[\s\S]*?name === 'Expand all'/, 'Gmail expansion detection still depends exclusively on English labels');
assert.match(chromeSource, /closest\('\[role="listitem"\],\[role="article"\],\.adn,\.ads'\)/, 'message-body controls can spoof Gmail expansion evidence');
assert.match(chromeSource, /return \(scanned && isSingleMessageGmailThread\(conversationRoot\)\) \? 'not_applicable' : null;/, 'Gmail expansion detection cannot report a thread with nothing to expand');
+ assert.match(chromeSource, /!msg\.closest\?\.\('\.a3s,\.ii'\)[\s\S]*?!msg\.parentElement\.closest\?\.\('\.adn,\.ads,\[data-message-id\]'\)/, 'single-message thread detection must filter out nested descendants and message-body elements');
for (const [label, getTools, prompt] of [
['chrome', getToolsForModeCh, SYSTEM_PROMPT_ASK_CH],