Skip to content

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

fix(gmail-guard): return independently observed Gmail candidates on expected recipient mismatch

Emre Sokullude3fa1812b3b
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 06e5f0fa5..902d716b9 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -17654,11 +17654,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
_messageRecipientCandidates(probe = {}) {
- const values = Array.isArray(probe.strongRecipientCandidates)
+ const values = Array.isArray(probe.strongRecipientCandidates) && probe.strongRecipientCandidates.length > 0
? probe.strongRecipientCandidates
- : (Array.isArray(probe.strongIdentityCandidates)
- ? probe.strongIdentityCandidates
- : []);
+ : (Array.isArray(probe.observedRecipientCandidates) && probe.observedRecipientCandidates.length > 0
+ ? probe.observedRecipientCandidates
+ : (Array.isArray(probe.strongIdentityCandidates)
+ ? probe.strongIdentityCandidates
+ : []));
return normalizeMessageTarget({ target_kind: 'named', recipients: values })?.recipients || [];
}
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.js
index b5c088141..d1693fbd6 100644
--- a/src/chrome/src/content/content.js
+++ b/src/chrome/src/content/content.js
@@ -5052,6 +5052,7 @@
const strongIdentities = [];
const strongRecipients = [];
const strongSeen = new Set();
+ const observedRecipientCandidates = [];
const gmailRecipientMode = params.adapterName === 'gmail';
const inConversationHeaderBand = (el) => {
if (headerBandBottom <= 0) return false;
@@ -5171,6 +5172,13 @@
})();
gmailComposeRoot = composeRoot;
const recipients = collectGmailRecipients(composeRoot);
+ for (const [recipientKey, recipient] of recipients) {
+ const emailKey = recipientKey.slice(recipientKey.indexOf(':') + 1);
+ const identity = recipient.aliases.get(emailKey) || [...recipient.aliases.values()][0] || recipient.identity || '';
+ if (identity) {
+ observedRecipientCandidates.push({ identity, role: recipient.role });
+ }
+ }
// Match the complete authorized To/CC/BCC set. Each expected identity
// must resolve to exactly one distinct chip and no additional chip may
// remain; duplicate display names therefore fail closed.
@@ -5234,7 +5242,11 @@
addStrongIdentity(el);
if (strongIdentities.length >= 8) break;
}
- for (const identity of strongIdentities) strongRecipients.push({ identity, role: 'to' });
+ for (const identity of strongIdentities) {
+ const item = { identity, role: 'to' };
+ strongRecipients.push(item);
+ observedRecipientCandidates.push(item);
+ }
}
const composerText = (() => {
@@ -5309,12 +5321,22 @@
identityCandidates: strongIdentities.slice(0, 16),
strongIdentityCandidates: strongIdentities.slice(0, 16),
strongRecipientCandidates: strongRecipients.slice(0, 16),
+ observedRecipientCandidates: observedRecipientCandidates.slice(0, 16),
...(messageRecipientDispatchToken
? { messageRecipientDispatchBinding: { token: messageRecipientDispatchToken } }
: {}),
};
} catch (error) {
- return { success: false, messageSend: null, conclusive: false, identityCandidates: [], error: error?.message || String(error) };
+ return {
+ success: false,
+ messageSend: null,
+ conclusive: false,
+ identityCandidates: [],
+ strongIdentityCandidates: [],
+ strongRecipientCandidates: [],
+ observedRecipientCandidates: [],
+ error: error?.message || String(error),
+ };
}
}
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index fb5766c9d..f2d874145 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -15407,11 +15407,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
_messageRecipientCandidates(probe = {}) {
- const values = Array.isArray(probe.strongRecipientCandidates)
+ const values = Array.isArray(probe.strongRecipientCandidates) && probe.strongRecipientCandidates.length > 0
? probe.strongRecipientCandidates
- : (Array.isArray(probe.strongIdentityCandidates)
- ? probe.strongIdentityCandidates
- : []);
+ : (Array.isArray(probe.observedRecipientCandidates) && probe.observedRecipientCandidates.length > 0
+ ? probe.observedRecipientCandidates
+ : (Array.isArray(probe.strongIdentityCandidates)
+ ? probe.strongIdentityCandidates
+ : []));
return normalizeMessageTarget({ target_kind: 'named', recipients: values })?.recipients || [];
}
diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.js
index 632b343cc..52718785d 100644
--- a/src/firefox/src/content/content.js
+++ b/src/firefox/src/content/content.js
@@ -4255,6 +4255,7 @@
const strongIdentities = [];
const strongRecipients = [];
const strongSeen = new Set();
+ const observedRecipientCandidates = [];
const gmailRecipientMode = params.adapterName === 'gmail';
const inConversationHeaderBand = (el) => {
if (headerBandBottom <= 0) return false;
@@ -4374,6 +4375,13 @@
})();
gmailComposeRoot = composeRoot;
const recipients = collectGmailRecipients(composeRoot);
+ for (const [recipientKey, recipient] of recipients) {
+ const emailKey = recipientKey.slice(recipientKey.indexOf(':') + 1);
+ const identity = recipient.aliases.get(emailKey) || [...recipient.aliases.values()][0] || recipient.identity || '';
+ if (identity) {
+ observedRecipientCandidates.push({ identity, role: recipient.role });
+ }
+ }
// Match the complete authorized To/CC/BCC set. Each expected identity
// must resolve to exactly one distinct chip and no additional chip may
// remain; duplicate display names therefore fail closed.
@@ -4437,7 +4445,11 @@
addStrongIdentity(el);
if (strongIdentities.length >= 8) break;
}
- for (const identity of strongIdentities) strongRecipients.push({ identity, role: 'to' });
+ for (const identity of strongIdentities) {
+ const item = { identity, role: 'to' };
+ strongRecipients.push(item);
+ observedRecipientCandidates.push(item);
+ }
}
const composerText = (() => {
@@ -4512,12 +4524,22 @@
identityCandidates: strongIdentities.slice(0, 16),
strongIdentityCandidates: strongIdentities.slice(0, 16),
strongRecipientCandidates: strongRecipients.slice(0, 16),
+ observedRecipientCandidates: observedRecipientCandidates.slice(0, 16),
...(messageRecipientDispatchToken
? { messageRecipientDispatchBinding: { token: messageRecipientDispatchToken } }
: {}),
};
} catch (error) {
- return { success: false, messageSend: null, conclusive: false, identityCandidates: [], error: error?.message || String(error) };
+ return {
+ success: false,
+ messageSend: null,
+ conclusive: false,
+ identityCandidates: [],
+ strongIdentityCandidates: [],
+ strongRecipientCandidates: [],
+ observedRecipientCandidates: [],
+ error: error?.message || String(error),
+ };
}
}
diff --git a/test/run.js b/test/run.js
index 6b8f81368..a620765b8 100644
--- a/test/run.js
+++ b/test/run.js
@@ -5442,6 +5442,55 @@ test('direct-message recipient guard uses structured intent and exact active ide
target_kind: 'named',
recipients: [{ identity: '[email protected]', role: 'to' }],
}, `${label}: explicit role answer must adopt the authorized To role`);
+
+ // Gmail recipient mismatch preserves observed candidates and stages recipient_change:
+ // When composer chips differ from the plan's authorized target, strongRecipientCandidates
+ // is empty, but observedRecipientCandidates carries the observed chips.
+ // The mismatch blocks dispatch, stages 'recipient_change', and clarification rebinds the target.
+ const mismatchPlanState = {
+ messaging: { target_kind: 'named', recipients: [{ identity: '[email protected]', role: 'to' }] },
+ requiresSubmission: true,
+ requiresStateChange: true,
+ };
+ agent._planExecutionGuards.set(tabId, { ...mismatchPlanState });
+ probe = {
+ success: true,
+ conclusive: true,
+ composerAvailable: true,
+ messageSend: true,
+ messageBody: 'Hello Bob',
+ messageBodyBaselineCount: 0,
+ strongRecipientCandidates: [],
+ observedRecipientCandidates: [{ identity: '[email protected]', role: 'to' }],
+ };
+ const mismatchSendBlock = await agent._messageRecipientGuardBlock(
+ tabId,
+ 'click_ax',
+ { ref_id: 'ref_send' },
+ 'https://mail.google.com/mail/u/0/#inbox/thread-1',
+ {},
+ );
+ assert.equal(mismatchSendBlock?.blocked, true, `${label}: mismatched Gmail recipient allowed dispatch`);
+ assert.equal(mismatchSendBlock?.reasonCode, 'active_recipient_unverified');
+ assert.equal(agent._planExecutionGuards.get(tabId)?.pendingRecipientAuthorization, 'recipient_change',
+ `${label}: mismatched Gmail recipient did not stage recipient_change authorization`);
+ assert.deepEqual(agent._planExecutionGuards.get(tabId)?.observedRecipientCandidates, [
+ { identity: '[email protected]', role: 'to' },
+ ], `${label}: mismatched Gmail recipient did not preserve observed candidates in guard`);
+
+ // Clarification answer naming the observed candidate successfully rebinds target:
+ assert.equal(
+ agent._bindClarifiedMessageRecipient(tabId, 'Send to [email protected]', 'user', {
+ question: 'The composer is addressed to Bob. Should I send to [email protected] instead?',
+ purpose: 'recipient_change',
+ }),
+ true,
+ `${label}: clarify answer failed to rebind observed mismatched recipient`,
+ );
+ assert.deepEqual(agent._planExecutionGuards.get(tabId)?.messaging, {
+ target_kind: 'named',
+ recipients: [{ identity: '[email protected]', role: 'to' }],
+ }, `${label}: target was not rebound to clarified recipient`);
}
});
@@ -5860,16 +5909,27 @@ test('direct-message recipient probe accepts only a unique active-thread header
assert.deepEqual(Array.from(gmailMatchingNameResult.strongIdentityCandidates), ['Alice']);
assert.deepEqual(Array.from(gmailWrongRecipientResult.strongIdentityCandidates), [],
`${prefix}: mismatched Gmail recipient chip authorized dispatch`);
+ assert.deepEqual(JSON.parse(JSON.stringify(gmailWrongRecipientResult.observedRecipientCandidates)), [
+ { identity: '[email protected]', role: 'to' },
+ ], `${prefix}: mismatched Gmail recipient chip was not preserved as an observed candidate`);
assert.deepEqual(Array.from(gmailMatchingSetResult.strongIdentityCandidates), ['Bob', '[email protected]'],
`${prefix}: exact authorized Gmail recipient set was rejected`);
assert.deepEqual(Array.from(gmailExtraRecipientResult.strongIdentityCandidates), [],
`${prefix}: an unreviewed extra Gmail recipient authorized dispatch`);
+ assert.deepEqual(JSON.parse(JSON.stringify(gmailExtraRecipientResult.observedRecipientCandidates)), [
+ { identity: '[email protected]', role: 'to' },
+ { identity: '[email protected]', role: 'to' },
+ ], `${prefix}: extra Gmail recipient chips were not preserved as observed candidates`);
assert.deepEqual(JSON.parse(JSON.stringify(gmailMatchingRoleResult.strongRecipientCandidates)), [
{ identity: '[email protected]', role: 'to' },
{ identity: 'Bob', role: 'bcc' },
], `${prefix}: exact Gmail To/BCC authorization was rejected`);
assert.deepEqual(Array.from(gmailWrongRoleResult.strongRecipientCandidates), [],
`${prefix}: moving a Gmail BCC recipient into To authorized dispatch`);
+ assert.deepEqual(JSON.parse(JSON.stringify(gmailWrongRoleResult.observedRecipientCandidates)), [
+ { identity: '[email protected]', role: 'to' },
+ { identity: '[email protected]', role: 'bcc' },
+ ], `${prefix}: Gmail role mismatch did not preserve observed candidates`);
assert.equal(gmailManyRecipientsResult.strongRecipientCandidates.length, 12,
`${prefix}: Gmail probe truncated 12 supported recipients to 8`);
assert.equal(gmailManyRecipientsResult.strongIdentityCandidates.length, 12);