Skip to content

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

Address PR #343 review findings on message recipient guard

Emre Sokullu5dbfa0be098b
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 07d7193ec..7eb64006a 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -17758,17 +17758,6 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
...(target?.target_kind === 'named' ? { expectedRecipients: target.recipients } : {}),
});
if (probe?.success === true && probe?.conclusive === true && probe.messageSend === false) return null;
- // A plan that declares neither submission nor state change cannot legitimately
- // dispatch a message, so an unclassifiable click under it is a read rather than
- // a send. Fail open for those instead of blocking every control the classifier
- // cannot resolve. A conclusive send stays blocked: a read-only plan still may
- // not deliver a message without recipient authorization.
- const explicitlyReadOnly = guard?.requiresSubmission === false
- && guard?.requiresStateChange === false;
- const conclusiveMessageSend = probe?.success === true
- && probe?.conclusive === true
- && probe.messageSend === true;
- if (explicitlyReadOnly && !conclusiveMessageSend) return null;
// Gmail reply editors can be collapsed when planning begins. Allow only a
// content-verified Reply/Reply all/Forward control to open the editor;
// every other unresolved click remains blocked. Recipient authorization
@@ -17873,16 +17862,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
* "receive", approval for any identity it liked. Authorization is therefore
* the intersection of what the user picked and what the page actually shows.
* The bound target is always an observed candidate, never the answer text,
- * and an answer matching no candidate or more than one authorizes nothing.
+ * and an answer matching no candidate or only a subset of observed
+ * candidates authorizes nothing. When multiple recipients are observed on
+ * the page (e.g. Reply all), the answer must explicitly authorize the full
+ * observed recipient set.
*/
- _bindClarifiedMessageRecipient(tabId, answer) {
+ _bindClarifiedMessageRecipient(tabId, answer, source = 'user') {
const guard = this._planExecutionGuards.get(tabId);
if (!guard) return false;
// Read and clear together. The consent the guard staged belongs to exactly
// one clarify, the one its own error told the model to ask; every later
// question is a different question and must not inherit it. Clearing on
- // the failing paths too keeps a non-matching answer from leaving consent
- // parked for some unrelated answer to pick up.
+ // all paths (including timeout, auto, mismatch, cancellation) keeps staged
+ // consent from remaining parked for an unrelated later clarify to pick up.
const pending = guard.pendingRecipientAuthorization === true;
const observed = Array.isArray(guard.observedRecipientCandidates)
? guard.observedRecipientCandidates
@@ -17890,13 +17882,14 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
guard.pendingRecipientAuthorization = false;
guard.observedRecipientCandidates = null;
if (!pending || observed.length === 0) return false;
+ if (!answer || source === 'timeout' || source === 'auto') return false;
const normalizedAnswer = normalizeRecipientIdentity(answer);
if (!normalizedAnswer) return false;
const matched = observed.filter(candidate => answerNamesIdentity(
normalizedAnswer,
normalizeRecipientIdentity(candidate?.identity),
));
- if (matched.length !== 1) return false;
+ if (matched.length === 0 || matched.length !== observed.length) return false;
const target = normalizeMessageTarget({ target_kind: 'named', recipients: matched });
if (!target) return false;
guard.messaging = target;
@@ -26180,6 +26173,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (tabPending.size === 0) this._pendingClarifications.delete(tabId);
if (response && response.cancelled) {
+ this._bindClarifiedMessageRecipient(tabId, '', 'cancelled');
return { success: false, cancelled: true, reason: response.reason || 'clarify cancelled' };
}
const answer = String(response?.answer || '').trim();
@@ -26191,9 +26185,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// never sees it, and the run loops until it exhausts its step budget.
// A timed-out or auto-selected answer is not a confirmation and binds
// nothing, matching how research escalation treats those sources below.
- if (answer && source !== 'timeout' && source !== 'auto') {
- this._bindClarifiedMessageRecipient(tabId, answer);
- }
+ // Every clarification outcome (human answer, timeout, auto) must consume
+ // the staged recipient consent so a later unrelated clarify does not inherit it.
+ this._bindClarifiedMessageRecipient(tabId, answer, source);
const explicitResearchApproval = isResearchEscalation
&& source !== 'timeout'
&& source !== 'auto'
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 6c43169b7..0373a962b 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -15511,17 +15511,6 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
...(target?.target_kind === 'named' ? { expectedRecipients: target.recipients } : {}),
});
if (probe?.success === true && probe?.conclusive === true && probe.messageSend === false) return null;
- // A plan that declares neither submission nor state change cannot legitimately
- // dispatch a message, so an unclassifiable click under it is a read rather than
- // a send. Fail open for those instead of blocking every control the classifier
- // cannot resolve. A conclusive send stays blocked: a read-only plan still may
- // not deliver a message without recipient authorization.
- const explicitlyReadOnly = guard?.requiresSubmission === false
- && guard?.requiresStateChange === false;
- const conclusiveMessageSend = probe?.success === true
- && probe?.conclusive === true
- && probe.messageSend === true;
- if (explicitlyReadOnly && !conclusiveMessageSend) return null;
// Gmail reply editors can be collapsed when planning begins. Allow only a
// content-verified Reply/Reply all/Forward control to open the editor;
// every other unresolved click remains blocked. Recipient authorization
@@ -15626,16 +15615,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
* "receive", approval for any identity it liked. Authorization is therefore
* the intersection of what the user picked and what the page actually shows.
* The bound target is always an observed candidate, never the answer text,
- * and an answer matching no candidate or more than one authorizes nothing.
+ * and an answer matching no candidate or only a subset of observed
+ * candidates authorizes nothing. When multiple recipients are observed on
+ * the page (e.g. Reply all), the answer must explicitly authorize the full
+ * observed recipient set.
*/
- _bindClarifiedMessageRecipient(tabId, answer) {
+ _bindClarifiedMessageRecipient(tabId, answer, source = 'user') {
const guard = this._planExecutionGuards.get(tabId);
if (!guard) return false;
// Read and clear together. The consent the guard staged belongs to exactly
// one clarify, the one its own error told the model to ask; every later
// question is a different question and must not inherit it. Clearing on
- // the failing paths too keeps a non-matching answer from leaving consent
- // parked for some unrelated answer to pick up.
+ // all paths (including timeout, auto, mismatch, cancellation) keeps staged
+ // consent from remaining parked for an unrelated later clarify to pick up.
const pending = guard.pendingRecipientAuthorization === true;
const observed = Array.isArray(guard.observedRecipientCandidates)
? guard.observedRecipientCandidates
@@ -15643,13 +15635,14 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
guard.pendingRecipientAuthorization = false;
guard.observedRecipientCandidates = null;
if (!pending || observed.length === 0) return false;
+ if (!answer || source === 'timeout' || source === 'auto') return false;
const normalizedAnswer = normalizeRecipientIdentity(answer);
if (!normalizedAnswer) return false;
const matched = observed.filter(candidate => answerNamesIdentity(
normalizedAnswer,
normalizeRecipientIdentity(candidate?.identity),
));
- if (matched.length !== 1) return false;
+ if (matched.length === 0 || matched.length !== observed.length) return false;
const target = normalizeMessageTarget({ target_kind: 'named', recipients: matched });
if (!target) return false;
guard.messaging = target;
@@ -22944,6 +22937,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (tabPending.size === 0) this._pendingClarifications.delete(tabId);
if (response && response.cancelled) {
+ this._bindClarifiedMessageRecipient(tabId, '', 'cancelled');
return { success: false, cancelled: true, reason: response.reason || 'clarify cancelled' };
}
const answer = String(response?.answer || '').trim();
@@ -22955,9 +22949,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// never sees it, and the run loops until it exhausts its step budget.
// A timed-out or auto-selected answer is not a confirmation and binds
// nothing, matching how research escalation treats those sources below.
- if (answer && source !== 'timeout' && source !== 'auto') {
- this._bindClarifiedMessageRecipient(tabId, answer);
- }
+ // Every clarification outcome (human answer, timeout, auto) must consume
+ // the staged recipient consent so a later unrelated clarify does not inherit it.
+ this._bindClarifiedMessageRecipient(tabId, answer, source);
const explicitResearchApproval = isResearchEscalation
&& source !== 'timeout'
&& source !== 'auto'
diff --git a/test/run.js b/test/run.js
index e1803d059..d46d5f46c 100644
--- a/test/run.js
+++ b/test/run.js
@@ -4956,24 +4956,24 @@ test('direct-message recipient guard uses structured intent and exact active ide
assert.equal(unsafe?.reasonCode, 'recipient_unverifiable_dispatch_path');
}
- // A plan declaring neither submission nor state change cannot legitimately
- // send, so an unclassifiable click under it is a read. Blocking those made
- // a draft-only Gmail task unable to operate the thread it was reading.
+ // A read-only plan cannot bypass the recipient guard on an inconclusive click.
+ // Planner flags describe requested intent, not DOM effects: an unclassified
+ // control might be a Send button, so only a conclusively non-send action may proceed.
agent._planExecutionGuards.set(tabId, {
messaging: null, requiresSubmission: false, requiresStateChange: false,
});
probe = { success: true, conclusive: false, messageSend: null, identityCandidates: [] };
- assert.equal(
- await agent._messageRecipientGuardBlock(
- tabId,
- 'click_ax',
- { ref_id: 'ref_expand_all' },
- 'https://mail.google.com/mail/u/0/#inbox/thread-1',
- {},
- ),
- null,
- `${label}: read-only plan still blocked an unclassifiable thread click`,
+ const readOnlyInconclusive = await agent._messageRecipientGuardBlock(
+ tabId,
+ 'click_ax',
+ { ref_id: 'ref_expand_all' },
+ 'https://mail.google.com/mail/u/0/#inbox/thread-1',
+ {},
);
+ assert.equal(readOnlyInconclusive?.blocked, true,
+ `${label}: read-only plan allowed an inconclusive click to bypass recipient guard`);
+ assert.equal(readOnlyInconclusive?.reasonCode, 'message_send_classification_inconclusive',
+ `${label}: inconclusive click did not fail closed`);
// The same read-only plan must still not deliver an actual message.
probe = {
@@ -5043,14 +5043,67 @@ test('direct-message recipient guard uses structured intent and exact active ide
`${label}: a later clarify inherited consent staged for an earlier one`,
);
+ // A timed-out or auto-selected clarify must consume staged consent without binding.
+ agent._planExecutionGuards.set(tabId, clarifyGuardState([{ identity: '[email protected]', role: 'to' }]));
+ assert.equal(
+ agent._bindClarifiedMessageRecipient(tabId, '[email protected]', 'timeout'),
+ false,
+ `${label}: timed-out clarify bound a recipient`,
+ );
+ assert.equal(agent._planExecutionGuards.get(tabId)?.pendingRecipientAuthorization, false,
+ `${label}: timed-out clarify left pendingRecipientAuthorization true`);
+ assert.equal(agent._planExecutionGuards.get(tabId)?.observedRecipientCandidates, null,
+ `${label}: timed-out clarify left observedRecipientCandidates intact`);
+ assert.equal(
+ agent._bindClarifiedMessageRecipient(tabId, '[email protected]'),
+ false,
+ `${label}: subsequent clarify inherited consent from timed-out clarify`,
+ );
+
+ agent._planExecutionGuards.set(tabId, clarifyGuardState([{ identity: '[email protected]', role: 'to' }]));
+ assert.equal(
+ agent._bindClarifiedMessageRecipient(tabId, '[email protected]', 'auto'),
+ false,
+ `${label}: auto-selected clarify bound a recipient`,
+ );
+ assert.equal(agent._planExecutionGuards.get(tabId)?.pendingRecipientAuthorization, false,
+ `${label}: auto-selected clarify left pendingRecipientAuthorization true`);
+ assert.equal(agent._planExecutionGuards.get(tabId)?.observedRecipientCandidates, null,
+ `${label}: auto-selected clarify left observedRecipientCandidates intact`);
+ assert.equal(
+ agent._bindClarifiedMessageRecipient(tabId, '[email protected]'),
+ false,
+ `${label}: subsequent clarify inherited consent from auto clarify`,
+ );
+
+ // Multi-recipient threads (e.g. Reply all): an answer naming all observed recipients
+ // authorizes the full recipient set.
agent._planExecutionGuards.set(tabId, clarifyGuardState([
{ identity: '[email protected]', role: 'to' },
{ identity: '[email protected]', role: 'to' },
]));
assert.equal(
agent._bindClarifiedMessageRecipient(tabId, '[email protected] and [email protected]'),
+ true,
+ `${label}: an answer naming all observed recipients was rejected`,
+ );
+ assert.deepEqual(agent._planExecutionGuards.get(tabId)?.messaging, {
+ target_kind: 'named',
+ recipients: [
+ { identity: '[email protected]', role: 'to' },
+ { identity: '[email protected]', role: 'to' },
+ ],
+ }, `${label}: clarify authorization did not set full recipient set`);
+
+ // But naming only a subset of the observed recipients authorizes nothing.
+ agent._planExecutionGuards.set(tabId, clarifyGuardState([
+ { identity: '[email protected]', role: 'to' },
+ { identity: '[email protected]', role: 'to' },
+ ]));
+ assert.equal(
+ agent._bindClarifiedMessageRecipient(tabId, 'Send it to [email protected]'),
false,
- `${label}: an ambiguous answer authorized a recipient`,
+ `${label}: an incomplete recipient subset authorized a send target`,
);
assert.equal(agent._planExecutionGuards.get(tabId)?.messaging, null);