Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Address review: restrict recipient clarify to messaging plans and verify single-message threads
Emre Sokullu6096ffcbf60a
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 7eb64006a..a266a6041 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -17824,17 +17824,26 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dreturn null;}- // Retain what the page actually showed. The block below tells the model to- // ask the user who the message is for, and that answer has to be matched- // against real observed identities rather than model-authored option text.+ // Retain what the page actually showed only when the plan already permits+ // messaging. The block below tells the model to ask the user who the+ // message is for, and that answer has to be matched against real observed+ // identities. A read-only plan (requiresSubmission=false or+ // requiresStateChange=false) must never stage recipient consent: an answer+ // to a recipient clarify must not elevate a read-only plan into a sendable one.if (guard) {- const observedRecipientCandidates = this._messageRecipientCandidates(probe);+ const messagingPermitted = guard.requiresSubmission === true+ && guard.requiresStateChange === true;+ const observedRecipientCandidates = messagingPermitted+ ? this._messageRecipientCandidates(probe)+ : [];guard.observedRecipientCandidates = observedRecipientCandidates.length? observedRecipientCandidates: null;// Offered to the next clarify only. The model chooses what it asks, so a// pending authorization must not survive into an unrelated question.- guard.pendingRecipientAuthorization = observedRecipientCandidates.length > 0;+ // Staged only when the plan already permits messaging.+ guard.pendingRecipientAuthorization = messagingPermitted+ && observedRecipientCandidates.length > 0;}return {@@ -17850,7 +17859,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d? 'Message action blocked: WebBrain could not conclusively resolve the target control and active composer. Re-read the page and retry with an exact visible control or fresh ref_id.': target? 'Message send blocked: the active conversation does not exactly match the recipient authorized by the user. Select the intended conversation, re-read its visible header, then retry the send action.'- : 'Message send blocked: the current task has no structured recipient authorization. Ask the user to name the recipient or explicitly authorize the currently open conversation before retrying.',+ : (guard?.requiresSubmission === false || guard?.requiresStateChange === false+ ? 'Message send blocked: the current plan does not authorize sending or submitting messages. Return the draft in chat or ask the user to authorize sending before attempting delivery.'+ : 'Message send blocked: the current task has no structured recipient authorization. Ask the user to name the recipient or explicitly authorize the currently open conversation before retrying.'),};}@@ -17865,7 +17876,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d* 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.+ * observed recipient set. A recipient answer may only bind under a plan+ * that already permits messaging (requiresSubmission=true and requiresStateChange=true).*/_bindClarifiedMessageRecipient(tabId, answer, source = 'user') {const guard = this._planExecutionGuards.get(tabId);@@ -17875,7 +17887,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// question is a different question and must not inherit it. Clearing on// 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 messagingPermitted = guard.requiresSubmission === true+ && guard.requiresStateChange === true;+ const pending = guard.pendingRecipientAuthorization === true && messagingPermitted;const observed = Array.isArray(guard.observedRecipientCandidates)? guard.observedRecipientCandidates: [];diff --git a/src/chrome/src/content/accessibility-tree.js b/src/chrome/src/content/accessibility-tree.jsindex 110afa451..8eb773994 100644--- a/src/chrome/src/content/accessibility-tree.js+++ b/src/chrome/src/content/accessibility-tree.js@@ -1282,6 +1282,17 @@return null;}+ 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;+ } catch (e) {}+ return false;+ }+function detectGmailConversationExpansionState(conversationRoot) {if (!isGmailConversationRoute() || !conversationRoot) return null;let collapsed = false;@@ -1304,8 +1315,9 @@// all. Report that as its own state so the read-completeness guard can tell// "nothing to expand" apart from "not checked yet"; conflating the two left// `done` permanently blocked on threads that were already fully readable.- // Only a scan that ran to completion may report it.- return scanned ? 'not_applicable' : null;+ // Only a scan that ran to completion on a verified single-message thread+ // may report it; a multi-message thread missing controls must stay unconfirmed.+ return (scanned && isSingleMessageGmailThread(conversationRoot)) ? 'not_applicable' : null;}function findGmailConversationExpandAll(conversationRoot) {diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 0373a962b..4a2bc24be 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -15577,17 +15577,26 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dreturn null;}- // Retain what the page actually showed. The block below tells the model to- // ask the user who the message is for, and that answer has to be matched- // against real observed identities rather than model-authored option text.+ // Retain what the page actually showed only when the plan already permits+ // messaging. The block below tells the model to ask the user who the+ // message is for, and that answer has to be matched against real observed+ // identities. A read-only plan (requiresSubmission=false or+ // requiresStateChange=false) must never stage recipient consent: an answer+ // to a recipient clarify must not elevate a read-only plan into a sendable one.if (guard) {- const observedRecipientCandidates = this._messageRecipientCandidates(probe);+ const messagingPermitted = guard.requiresSubmission === true+ && guard.requiresStateChange === true;+ const observedRecipientCandidates = messagingPermitted+ ? this._messageRecipientCandidates(probe)+ : [];guard.observedRecipientCandidates = observedRecipientCandidates.length? observedRecipientCandidates: null;// Offered to the next clarify only. The model chooses what it asks, so a// pending authorization must not survive into an unrelated question.- guard.pendingRecipientAuthorization = observedRecipientCandidates.length > 0;+ // Staged only when the plan already permits messaging.+ guard.pendingRecipientAuthorization = messagingPermitted+ && observedRecipientCandidates.length > 0;}return {@@ -15603,7 +15612,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d? 'Message action blocked: WebBrain could not conclusively resolve the target control and active composer. Re-read the page and retry with an exact visible control or fresh ref_id.': target? 'Message send blocked: the active conversation does not exactly match the recipient authorized by the user. Select the intended conversation, re-read its visible header, then retry the send action.'- : 'Message send blocked: the current task has no structured recipient authorization. Ask the user to name the recipient or explicitly authorize the currently open conversation before retrying.',+ : (guard?.requiresSubmission === false || guard?.requiresStateChange === false+ ? 'Message send blocked: the current plan does not authorize sending or submitting messages. Return the draft in chat or ask the user to authorize sending before attempting delivery.'+ : 'Message send blocked: the current task has no structured recipient authorization. Ask the user to name the recipient or explicitly authorize the currently open conversation before retrying.'),};}@@ -15618,7 +15629,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d* 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.+ * observed recipient set. A recipient answer may only bind under a plan+ * that already permits messaging (requiresSubmission=true and requiresStateChange=true).*/_bindClarifiedMessageRecipient(tabId, answer, source = 'user') {const guard = this._planExecutionGuards.get(tabId);@@ -15628,7 +15640,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// question is a different question and must not inherit it. Clearing on// 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 messagingPermitted = guard.requiresSubmission === true+ && guard.requiresStateChange === true;+ const pending = guard.pendingRecipientAuthorization === true && messagingPermitted;const observed = Array.isArray(guard.observedRecipientCandidates)? guard.observedRecipientCandidates: [];diff --git a/src/firefox/src/content/accessibility-tree.js b/src/firefox/src/content/accessibility-tree.jsindex 110afa451..8eb773994 100644--- a/src/firefox/src/content/accessibility-tree.js+++ b/src/firefox/src/content/accessibility-tree.js@@ -1282,6 +1282,17 @@return null;}+ 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;+ } catch (e) {}+ return false;+ }+function detectGmailConversationExpansionState(conversationRoot) {if (!isGmailConversationRoute() || !conversationRoot) return null;let collapsed = false;@@ -1304,8 +1315,9 @@// all. Report that as its own state so the read-completeness guard can tell// "nothing to expand" apart from "not checked yet"; conflating the two left// `done` permanently blocked on threads that were already fully readable.- // Only a scan that ran to completion may report it.- return scanned ? 'not_applicable' : null;+ // Only a scan that ran to completion on a verified single-message thread+ // may report it; a multi-message thread missing controls must stay unconfirmed.+ return (scanned && isSingleMessageGmailThread(conversationRoot)) ? 'not_applicable' : null;}function findGmailConversationExpandAll(conversationRoot) {diff --git a/test/run.js b/test/run.jsindex d46d5f46c..00f38dfd9 100644--- a/test/run.js+++ b/test/run.js@@ -4998,9 +4998,40 @@ test('direct-message recipient guard uses structured intent and exact active ide`${label}: read-only plan dispatched a conclusive message send`);assert.equal(readOnlySend?.reasonCode, 'authorized_recipient_missing');- // The block tells the model to ask the user who the message is for, so the- // answer has to be able to reach the guard. It binds to a page-observed- // identity, never to the model-authored option text.+ // A read-only plan must never stage recipient consent: an answer to a recipient+ // clarify must not elevate a read-only plan into a send-capable one.+ assert.equal(+ agent._planExecutionGuards.get(tabId)?.observedRecipientCandidates,+ null,+ `${label}: read-only plan staged recipient candidates`,+ );+ assert.equal(+ agent._planExecutionGuards.get(tabId)?.pendingRecipientAuthorization,+ false,+ `${label}: read-only plan staged pendingRecipientAuthorization`,+ );+ assert.equal(+ agent._bindClarifiedMessageRecipient(tabId, 'Reply to Alice ([email protected])'),+ false,+ `${label}: clarify answer elevated a read-only plan into an authorized sender`,+ );+ assert.equal(agent._planExecutionGuards.get(tabId)?.messaging, null);++ // A messaging-authorized plan does stage consent for the clarify it asks for,+ // and binds to the page-observed candidate rather than model-authored option text.+ agent._planExecutionGuards.set(tabId, {+ messaging: null,+ requiresSubmission: true,+ requiresStateChange: true,+ });+ const authorizedSendBlock = await agent._messageRecipientGuardBlock(+ tabId,+ 'click_ax',+ { ref_id: 'ref_send' },+ 'https://mail.google.com/mail/u/0/#inbox/thread-1',+ {},+ );+ assert.equal(authorizedSendBlock?.blocked, true);assert.deepEqual(agent._planExecutionGuards.get(tabId)?.observedRecipientCandidates,[{ identity: '[email protected]', role: 'to' }],@@ -10406,7 +10437,14 @@ test('Gmail expansion detection separates nothing-to-expand from not-yet-checkedclosest: () => null,__name: name,});- const rootWith = controls => ({ querySelectorAll: () => controls });+ const rootWith = (controls, messages = [{}]) => ({+ querySelectorAll: selector => {+ if (typeof selector === 'string' && (selector.includes('button') || selector.includes('[role="button"]'))) {+ return controls;+ }+ return messages;+ },+ });const build = ({ route = true, visible = () => true } = {}) => {const factory = new Function(@@ -10421,6 +10459,10 @@ test('Gmail expansion detection separates nothing-to-expand from not-yet-checked// nothing collapsed, not an unfinished check.assert.equal(detect(rootWith([control('Reply'), control('Print all')])), 'not_applicable',`${prefix}: a thread with no expansion control did not report not_applicable`);+ assert.equal(detect(rootWith([control('Reply'), control('Print all')], [{}, {}])), null,+ `${prefix}: multi-message thread missing controls should not report not_applicable`);+ assert.equal(detect(rootWith([control('Reply'), control('Print all')], [])), null,+ `${prefix}: thread with unknown message structure should not report not_applicable`);assert.equal(detect(rootWith([control('Expand all')])), 'collapsed',`${prefix}: a collapsed thread was misreported`);assert.equal(detect(rootWith([control('Collapse all')])), 'expanded',@@ -10456,7 +10498,7 @@ test('accessibility-tree schema and prompts preserve exact whole-document continassert.match(chromeSource, /conversationExpansionState/, 'Gmail expansion evidence is not returned as structured metadata');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 \? 'not_applicable' : null;/, 'Gmail expansion detection cannot report a thread with nothing to expand');+ assert.match(chromeSource, /return \(scanned && isSingleMessageGmailThread\(conversationRoot\)\) \? 'not_applicable' : null;/, 'Gmail expansion detection cannot report a thread with nothing to expand');for (const [label, getTools, prompt] of [['chrome', getToolsForModeCh, SYSTEM_PROMPT_ASK_CH],