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
Fix repeated message approvals and chat dispatch
Emre Sokullu1d40cd182425
diff --git a/docs/page-feedback-monitor.md b/docs/page-feedback-monitor.mdindex 7902edf06..0bdbea054 100644--- a/docs/page-feedback-monitor.md+++ b/docs/page-feedback-monitor.md@@ -150,6 +150,20 @@ Steer or grant authorization. Enter and Alt+Enter retain their existing behaviorCommunication loss after dispatch and a recipient change during an inputhandler produce an uncertain result without an automatic retry. Attaching afile does not establish that the website sent or received it.+- Generic chat recipients can use a unique contact-name button in the local+ conversation header above the composer's footer. Sidebar, message-history+ and toolbar buttons do not establish the recipient. The private dispatch+ proof binds the contact, header, pane and composer destination label so a+ same-name conversation replacement still invalidates the action.+ Pre-inference action capture uses the same isolated, read-only evidence and+ scopes editor/control state to that footer; unrelated history updates do not+ invalidate its certified actions.+ Existing recipient approvals survive trusted Continue within the same task+ and execution scope; independent tasks and changed scope clear them. Missing live+ identity, draft, baseline or dispatch proof produces a technical blocker;+ another approval question cannot repair missing evidence. Recipient+ clarifications report `recipientBinding` separately from general user+ authorization and suppress repeated questions for known technical blockers.- A single explicit `done` candidate survives passive DOM updates before andafter its tool batch. Success still requires the completion verifier's freshevidence; partial/failure outcomes remain incomplete. The summary does notdiff --git a/package.json b/package.jsonindex 52b6c98e1..2d37eb493 100644--- a/package.json+++ b/package.json@@ -53,8 +53,8 @@"benchmark:relevance": "node scripts/benchmark-offline-relevance.mjs --verbose","test:social-contract": "node --test test/social-publish-contract.mjs","test:social-contract:dom": "node test/social-publish-contract-dom.mjs",- "test:runtime-lifecycle": "node --test test/agent-lifecycle.mjs test/runtime-lifecycle.mjs test/collection-read-progress.mjs test/network-lifecycle.mjs test/text-write-cancellation.mjs test/chat-steering.mjs test/page-feedback.mjs test/page-feedback-progress.mjs test/page-feedback-policy.mjs test/page-feedback-transport-races.mjs test/page-feedback-recovery.mjs test/media-download-agent-handler.mjs && node test/ui-content-lifecycle.mjs && node --test test/chat-steering-ui.mjs test/page-monitor-ui.mjs test/page-monitor-passive-progress.mjs test/media-download-binding.mjs test/media-download-capture-integration.mjs && node test/message-recipient-upload.mjs",- "test:page-feedback": "node --test test/page-feedback.mjs test/page-feedback-progress.mjs test/page-feedback-policy.mjs test/page-feedback-transport-races.mjs test/page-feedback-recovery.mjs test/media-download-agent-handler.mjs test/page-monitor-ui.mjs test/page-monitor-passive-progress.mjs test/media-download-binding.mjs test/media-download-capture-integration.mjs && node test/message-recipient-upload.mjs",+ "test:runtime-lifecycle": "node --test test/agent-lifecycle.mjs test/runtime-lifecycle.mjs test/collection-read-progress.mjs test/network-lifecycle.mjs test/text-write-cancellation.mjs test/chat-steering.mjs test/page-feedback.mjs test/page-feedback-progress.mjs test/page-feedback-policy.mjs test/page-feedback-transport-races.mjs test/page-feedback-recovery.mjs test/media-download-agent-handler.mjs && node test/ui-content-lifecycle.mjs && node --test test/chat-steering-ui.mjs test/page-monitor-ui.mjs test/page-monitor-passive-progress.mjs test/media-download-binding.mjs test/media-download-capture-integration.mjs && node test/message-recipient-upload.mjs && node --test test/message-recipient-whatsapp.mjs",+ "test:page-feedback": "node --test test/page-feedback.mjs test/page-feedback-progress.mjs test/page-feedback-policy.mjs test/page-feedback-transport-races.mjs test/page-feedback-recovery.mjs test/media-download-agent-handler.mjs test/page-monitor-ui.mjs test/page-monitor-passive-progress.mjs test/media-download-binding.mjs test/media-download-capture-integration.mjs && node test/message-recipient-upload.mjs && node --test test/message-recipient-whatsapp.mjs","test:chat-steering": "node --test test/chat-steering.mjs test/chat-steering-ui.mjs","test:rich-text-dom": "node test/ui-content-lifecycle.mjs --dom","test:firefox-bidi": "node --test test/firefox-bidi.mjs test/firefox-full-page-screenshot.mjs",diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex abda47c1a..55a57f3cf 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -24497,7 +24497,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d&& guard?.requiresSubmission === true && guard?.requiresStateChange === true? this._resolveTwitterNamedMessagingTarget(target, probe) : null;if (resolvedTwitterTarget) target = resolvedTwitterTarget;- const messageBodyBaselineCount = Number(probe?.messageBodyBaselineCount);+ const messageBodyBaselineCount = probe?.messageBodyBaselineCount;const targetMatchesObserved = messageTargetMatchesObservedIdentities(target,this._messageRecipientCandidates(probe),@@ -24514,6 +24514,34 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst approvedByUser = guard?.messageRecipientApprovedAll === true|| (observedIdentities.length > 0&& observedIdentities.every(identity => approvedRecipients.includes(identity)));+ const technicalBlock = (reasonCode, error) => {+ if (guard) {+ guard.pendingRecipientAuthorization = false;+ guard.observedRecipientCandidates = null;+ guard.messageRecipientTechnicalBlock = reasonCode;+ }+ return { success: false, blocked: true, noDispatch: true, dispatched: false,+ messageRecipientGuard: true, retryable: false, recipientAuthorizationRequired: false,+ reasonCode, error };+ };+ const conclusiveSend = probe?.success === true && probe?.conclusive === true && probe?.messageSend === true;+ if (conclusiveSend && observedIdentities.length === 0) {+ return technicalBlock('recipient_identity_unavailable',+ 'Message send blocked because WebBrain cannot establish the active recipient identity from this page. Re-read the active conversation header or open the intended chat. If identity remains unavailable, report this technical blocker; additional recipient approval cannot resolve missing page evidence.');+ }+ if (conclusiveSend && observedIdentities.length > 0 && guard) delete guard.messageRecipientTechnicalBlock;+ // An already authorized recipient can still lack draft or delivery+ // evidence. Asking to authorize that recipient again cannot repair it.+ if (conclusiveSend && (targetMatchesObserved || approvedByUser)) {+ if (name !== 'upload_file' && !this._workflowMessageBody(probe?.messageBody)) {+ return technicalBlock('message_body_unverified',+ 'Message send blocked because WebBrain could not verify the draft body for the authorized recipient. Re-read the current composer and verify the intended draft. If the body remains unavailable, report this technical blocker instead of asking for recipient approval again.');+ }+ if (!Number.isInteger(messageBodyBaselineCount) || messageBodyBaselineCount < 0) {+ return technicalBlock('message_body_baseline_unavailable',+ 'Message send blocked because WebBrain could not establish the existing-message baseline needed to verify a new send. Re-read the active conversation before attempting delivery. If the baseline remains unavailable, report this technical blocker instead of asking for recipient approval again.');+ }+ }const verified = probe?.success === true&& probe.messageSend === true&& (name === 'upload_file'@@ -24526,15 +24554,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (verified) {const binding = probe?.messageRecipientDispatchBinding;if (!binding?.token) {- return {- success: false,- blocked: true,- noDispatch: true,- dispatched: false,- messageRecipientGuard: true,- reasonCode: 'recipient_dispatch_binding_unavailable',- error: 'Message send blocked because WebBrain could not bind recipient verification to the final action dispatch. Re-read the active conversation and retry once.',- };+ return technicalBlock('recipient_dispatch_binding_unavailable',+ 'Message send blocked because WebBrain could not bind recipient verification to the final action dispatch. Re-read the active conversation and retry once. If binding remains unavailable, report this technical blocker instead of asking for recipient approval again.');}if (resolvedTwitterTarget) guard.messaging = resolvedTwitterTarget;if (executionContext && typeof executionContext === 'object') {@@ -30617,7 +30638,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai? this._continuationExecutionEvidence.get(tabId): null;this._continuationExecutionEvidence.delete(tabId);- const carryMatches = enabled+ const carryScopeMatches = enabled&& carried?.requestKind === 'execute'&& carried.requiresStateChange === requiresStateChange&& carried.requiresSubmission === requiresSubmission@@ -30630,8 +30651,12 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai&& JSON.stringify(carried.siteWorkflow || null) === JSON.stringify(siteWorkflow || null)&& JSON.stringify(carried.conditionalSiteWorkflow || null) === JSON.stringify(gateOutcome.conditionalSiteWorkflow || null)&& carried.taskKey === taskKey- && carried.evidenceTaskKey === taskKey&& carried.conversationId === (this.conversationIds.get(tabId) || null);+ const carryMatches = carryScopeMatches && carried.evidenceTaskKey === taskKey;+ // Human recipient grants are task authorization, not execution evidence.+ // They can survive Continue even when the last step only approved a send.+ const carryRecipientAuthorization = carryScopeMatches && requiresSubmission === true+ && requiresStateChange === true && !!messaging;const approvedPlanAnchor = gateApprovedPlanAnchor|| (carryMatches ? carried.approvedPlanAnchor || '' : '');const approvedPlanText = String(gateOutcome?.approvedScratchpadText || '')@@ -30644,6 +30669,9 @@ If the user has already named or confirmed this exact recipient, do NOT ask agaisiteWorkflowUrl: String(gateOutcome?.siteWorkflowUrl || ''),messaging,messagingConversationScope,+ approvedRecipients: carryRecipientAuthorization && Array.isArray(carried.approvedRecipients)+ ? [...carried.approvedRecipients] : [],+ messageRecipientApprovedAll: carryRecipientAuthorization && carried.messageRecipientApprovedAll === true,draftRecipients,requiresDownload,allowsPlannerShapedResult: gateOutcome?.allowsPlannerShapedResult === true,@@ -31045,8 +31073,12 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai_storeContinuationExecutionEvidence(tabId) {const guard = this._planExecutionGuards.get(tabId);+ const hasRecipientAuthorization = guard?.requiresSubmission === true && guard?.requiresStateChange === true+ && !!normalizeMessageTarget(guard.messaging)+ && (guard.messageRecipientApprovedAll === true || (guard.approvedRecipients || []).length > 0);if (guard?.enabled && !guard.taskDrifted && (- guard.successfulTaskToolCalls > 0+ hasRecipientAuthorization+ || guard.successfulTaskToolCalls > 0|| guard.successfulConsequentialToolCalls > 0|| guard.pendingDownloadIds.length > 0|| guard.verifiedSubmissionEvidence === true@@ -31070,6 +31102,9 @@ If the user has already named or confirmed this exact recipient, do NOT ask agairequiresSubmission: guard.requiresSubmission,messaging: guard.messaging,messagingConversationScope: guard.messagingConversationScope,+ approvedRecipients: hasRecipientAuthorization && Array.isArray(guard.approvedRecipients)+ ? [...guard.approvedRecipients] : [],+ messageRecipientApprovedAll: hasRecipientAuthorization && guard.messageRecipientApprovedAll === true,draftRecipients: guard.draftRecipients,requiresDownload: guard.requiresDownload,allowsAppStateToolEvidence: guard.allowsAppStateToolEvidence,@@ -35205,6 +35240,25 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai: (args?.purpose === 'recipient_change'? 'recipient_change': (args?.purpose === 'message_recipient' ? 'message_recipient' : null));+ const explicitRecipientQuestion = purpose === 'message_recipient' || purpose === 'recipient_change';+ const recipientBindingRequired = explicitRecipientQuestion+ || (!!clarificationGuard?.pendingRecipientAuthorization+ && this._isRecipientClarification({ question, options, reason, purpose },+ typeof clarificationGuard.pendingRecipientAuthorization === 'string'+ ? clarificationGuard.pendingRecipientAuthorization : 'message_recipient',+ clarificationGuard.observedRecipientCandidates || []));+ const technicalRecipientReasons = new Set(['recipient_identity_unavailable', 'message_body_unverified',+ 'message_body_baseline_unavailable', 'recipient_dispatch_binding_unavailable']);+ const technicalRecipientReason = technicalRecipientReasons.has(clarificationGuard?.messageRecipientTechnicalBlock)+ ? clarificationGuard.messageRecipientTechnicalBlock : '';+ if (technicalRecipientReason && (explicitRecipientQuestion+ || this._isRecipientClarification({ question, options, reason, purpose }, 'recipient_change', [])+ || this._isRecipientClarification({ question, options, reason, purpose }, 'message_recipient', []))) {+ return { success: false, blocked: true, noDispatch: true, dispatched: false, retryable: false,+ messageRecipientGuard: true, reasonCode: technicalRecipientReason,+ recipientBinding: { required: true, bound: false, reasonCode: technicalRecipientReason },+ error: 'Recipient confirmation cannot repair the unresolved technical message guard. No clarification was shown. Re-read the active recipient and composer evidence; if it remains unavailable, report the blocker instead of requesting the same approval again.' };+ }const isResearchEscalation = purpose === 'research_escalation';const requireExplicitAnswer = isResearchEscalation || args?.require_explicit_answer === true;const researchRequest = isResearchEscalation ? normalizeResearchRequest(args?.research_request) : '';@@ -35307,7 +35361,9 @@ If the user has already named or confirmed this exact recipient, do NOT ask agaiif (response && response.cancelled) {this._bindClarifiedMessageRecipient(tabId, '', 'cancelled', { question, options, reason, purpose });- return { success: false, cancelled: true, reason: response.reason || 'clarify cancelled' };+ return { success: false, cancelled: true, reason: response.reason || 'clarify cancelled',+ ...(recipientBindingRequired ? { recipientBinding: { required: true, bound: false,+ reasonCode: 'recipient_clarification_cancelled' } } : {}) };}const answer = String(response?.answer || '').trim();const source = response?.source || 'user';@@ -35320,7 +35376,11 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai// nothing, matching how research escalation treats those sources below.// 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, { question, options, reason, purpose, safeFirst: entrySafeFirst });+ const recipientBound = this._bindClarifiedMessageRecipient(tabId, answer, source,+ { question, options, reason, purpose, safeFirst: entrySafeFirst });+ const recipientBinding = recipientBindingRequired ? { required: true, bound: recipientBound,+ ...(!recipientBound ? { reasonCode: technicalRecipientReasons.has(clarificationGuard?.messageRecipientTechnicalBlock)+ ? clarificationGuard.messageRecipientTechnicalBlock : 'recipient_authorization_not_bound' } : {}) } : null;this._recordSocialPublicationClarification(tabId, clarificationGuard, question, answer, source);const explicitResearchApproval = isResearchEscalation&& source !== 'timeout'@@ -35350,6 +35410,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agaisource,authorized,requiresExplicitConfirmation: !authorized,+ ...(recipientBinding ? { recipientBinding } : {}),...(isResearchEscalation ? {researchEscalationApproved: explicitResearchApproval,authorization_token: authorizationToken,diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.jsindex 4bac82855..fdc3cffd0 100644--- a/src/chrome/src/content/content.js+++ b/src/chrome/src/content/content.js@@ -5061,6 +5061,7 @@)).filter(value => value.identity && /^(?:to|cc|bcc)$/.test(value.role)).slice(0, 16): [],supportsRecipientSets: dispatch.supportsRecipientSets === true,+ conversationRecipientEvidence: dispatch.conversationRecipientEvidence || null,identityKey,messageBody: String(dispatch.messageBody || ''),messageBodyBaselineCount: Number(dispatch.messageBodyBaselineCount || 0),@@ -5137,6 +5138,7 @@expectedRecipients: expected.expectedRecipients,supportsRecipientSets: expected.supportsRecipientSets,uploadSourceKey: expected.uploadSourceKey,+ expectedConversationRecipientEvidence: expected.conversationRecipientEvidence,});if (live?.dispatchTargetChanged === true) {return {@@ -5695,7 +5697,36 @@// conversation before a message can be sent. It deliberately ignores input// values and ordinary page text: a searched recipient name is not proof that// the corresponding conversation is active.- function _probeMessageRecipientGuard(params = {}) {+ // Isolated-world, read-only bridge for private pre-inference footprints.+ // Only controls in the verified composer's own native footer are eligible;+ // neither runtime params nor the page's main world receive DOM references.+ window.__wb_get_message_conversation_context = target => {+ try {+ if (WEBBRAIN_GENERATION !== (Number(window.__webbrain_generation) || 0)+ || target?.nodeType !== 1 || !target.isConnected || target.ownerDocument !== document) return null;+ const footer = target.closest('footer');+ const control = target.closest('button,[role="button"],input,select,textarea,'+ + '[contenteditable="true"],[contenteditable=""],[role="textbox"]');+ const excluded = 'aside,nav,search,[role="navigation"],[role="complementary"],[role="search"],'+ + '[role="searchbox"],[role="log"],[role="listbox"],[role="menu"],'+ + '[data-message-id],[data-message-direction],[data-outgoing],article';+ if (!footer || !control || !footer.contains(control) || target.closest(excluded)+ || /^search$/i.test(control.getAttribute('type') || '')) return null;+ let context = null;+ _probeMessageRecipientGuard({ tool: 'observe_active_conversation', adapterName: 'generic-messaging', args: {} }, evidence => {+ if (evidence?.footer !== footer || evidence.composer?.closest('footer') !== footer+ || !footer.contains(target) || !evidence.pane.contains(evidence.carrier)) return null;+ context = Object.freeze({ ...evidence, name: evidence.aliases[0],+ aliases: Object.freeze([...evidence.aliases]),+ labelledByNodes: Object.freeze([...evidence.labelledByNodes]),+ contextNodes: Object.freeze([...evidence.contextNodes]) });+ return null;+ });+ return context;+ } catch { return null; }+ };++ function _probeMessageRecipientGuard(params = {}, receiveConversationContext = null) {try {const tool = String(params.tool || '');const observationOnly = tool === 'observe_active_conversation';@@ -6653,6 +6684,7 @@const strongRecipients = [];const strongSeen = new Set();const observedRecipientCandidates = [];+ let conversationRecipientEvidence = null;const gmailRecipientMode = params.adapterName === 'gmail';const inConversationHeaderBand = (el) => {if (headerBandBottom <= 0) return false;@@ -6940,13 +6972,101 @@addStrongIdentity(el);if (strongIdentities.length >= 8) break;}+ if (!strongIdentities.length && params.adapterName === 'generic-messaging'+ && params.supportsRecipientSets !== true) {+ // Some conversation headers expose the contact name as a button+ // rather than a heading. A shared local header/footer pane proves+ // ownership; a page-wide button or a sidebar name proves nothing.+ try {+ const footer = composer.closest?.('footer');+ const excluded = 'aside,nav,[role="navigation"],[role="complementary"],[role="log"],'+ + '[role="listbox"],[role="menu"],[role="toolbar"],[data-message-id],[data-message-direction],[data-outgoing],article';+ const actionLabel = /^(?:call(?:\s|$)|(?:voice|video|audio)\s+call(?:\s|$)|search(?:\s|$)|menu(?:\s|$)|more(?:\s|$)|settings(?:\s|$)|back(?:\s|$)|close(?:\s|$)|arama(?:\s|$)|ara(?:\s|$)|suchen(?:\s|$)|rechercher(?:\s|$)|buscar(?:\s|$)|llamar(?:\s|$)|appel(?:\s|$))/iu;+ if (footer && !footer.closest(excluded)) {+ for (let pane = footer.parentElement, depth = 0; pane && depth < 8+ && pane !== document.body && pane !== document.documentElement; pane = pane.parentElement, depth++) {+ if (pane.closest(excluded)) break;+ const ownedHeader = header => {+ let branch = header;+ for (let level = 0; level < 4; level++) {+ const parent = branch.parentElement;+ if (parent === pane) return true;+ if (parent?.tagName !== 'DIV' || parent.children.length !== 1) return false;+ branch = parent;+ }+ return false;+ };+ const headers = Array.from(pane.querySelectorAll('header')).filter(header => (+ ownedHeader(header) && visible(header) && !footer.contains(header) && !header.closest(excluded)+ && inConversationHeaderBand(header) && !independentScrollableRegion(header, composer)+ ));+ if (!headers.length) continue;+ // Stop at the closest pane with a header, including when its+ // evidence is ambiguous. A broader app header cannot rescue it.+ if (headers.length !== 1 || composerCandidates.some(editor => editor !== composer+ && pane.contains(editor) && !composer.contains(editor) && !editor.contains(composer)+ && !verifiedNavigationEditable(editor))) break;+ const header = headers[0];+ const modal = typeof _findTopmostBlockingModal === 'function' ? _findTopmostBlockingModal() : null;+ if (modal && (!modal.contains(composer) || !modal.contains(header))) break;+ const contacts = Array.from(header.querySelectorAll('button,[role="button"]')).filter(control => {+ if (!visible(control) || editable(control) || control.closest('header') !== header+ || control.closest(excluded) || control.disabled || control.getAttribute('aria-disabled') === 'true'+ || control.form || control.hasAttribute('form') || !inConversationHeaderBand(control)+ || independentScrollableRegion(control, composer)) return false;+ const name = String(control.innerText || '').replace(/[\u200b-\u200d\ufeff]/g, '').replace(/\s+/g, ' ').trim();+ const labels = [name, control.getAttribute('aria-label'), control.getAttribute('title')].filter(Boolean);+ return !!name && name.length <= 120 && !labels.some(label => actionLabel.test(String(label).trim())+ || _hasMessageCommitName(label) || _COMPOSER_UTILITY_LABEL_RE.test(String(label).trim()));+ });+ if (contacts.length !== 1) break;+ const carrier = contacts[0];+ const name = compact(carrier.innerText, 120);+ const labelledByNodes = [];+ let labelledByValid = true;+ for (const id of String(composer.getAttribute('aria-labelledby') || '').split(/\s+/).filter(Boolean)) {+ const matches = composer.getRootNode().querySelectorAll(`[id="${CSS.escape(id)}"]`);+ if (matches.length !== 1) { labelledByValid = false; break; }+ labelledByNodes.push(matches[0]);+ }+ if (!labelledByValid) break;+ const identityAttributes = ['id','role','aria-label','title','aria-labelledby','aria-hidden','inert',+ 'data-recipient-id','data-conversation-id','data-thread-id','data-chat-id'];+ const contextNodes = new Set([carrier, header, pane, footer, composer]);+ for (const start of [carrier, composer]) {+ for (let node = start.parentElement; node && node !== pane; node = node.parentElement) contextNodes.add(node);+ }+ conversationRecipientEvidence = { carrier, header, pane, footer, composer, labelledByNodes, contextNodes: [...contextNodes],+ signature: JSON.stringify([name,+ [...contextNodes].map(node => identityAttributes.map(attribute => node.getAttribute(attribute))),+ ['aria-label','aria-labelledby','placeholder','title'].map(attribute => composer.getAttribute(attribute)),+ labelledByNodes.map(node => node.textContent)]) };+ strongIdentities.push(name);+ const phone = String(composer.getAttribute('aria-label') || '').trim()+ .match(/^(?:type|write|send)\s+(?:a\s+)?message\s+to\s+(\+?[\d ()-]{7,32})$/i)?.[1]?.trim() || '';+ const phoneDigits = phone.replace(/\D/g, '').length;+ conversationRecipientEvidence.aliases = phone && phoneDigits >= 7 && phoneDigits <= 15+ ? [name, phone] : [name];+ break;+ }+ }+ } catch { /* An unsupported header remains unverified. */ }+ }for (const identity of strongIdentities) {- const item = { identity, role: 'to', aliases: [identity] };+ const item = { identity, role: 'to', aliases: conversationRecipientEvidence?.aliases || [identity] };strongRecipients.push({ identity, role: 'to' });observedRecipientCandidates.push(item);}}+ // The isolated monitor reads this exact local proof synchronously. Its+ // closure-only callback never participates in the public message API,+ // authorizes an action, or needs the expensive message-history scan.+ if (typeof receiveConversationContext === 'function') {+ return receiveConversationContext(observationOnly && params.adapterName === 'generic-messaging'+ && params.supportsRecipientSets !== true ? conversationRecipientEvidence : null);+ }+// ── Generic recipient discovery (every site) ────────────────────────// The Gmail branch above reads Google's own DOM. Every other webmail,// CRM, ticketing tool, and messaging app needs the same answer: who would@@ -7045,6 +7165,19 @@} catch { /* chip read is best-effort */ }}+ const expectedConversation = params.expectedConversationRecipientEvidence;+ if (expectedConversation && (!conversationRecipientEvidence+ || ['carrier','header','pane','footer'].some(key => expectedConversation[key] !== conversationRecipientEvidence[key])+ || expectedConversation.signature !== conversationRecipientEvidence.signature+ || expectedConversation.contextNodes.length !== conversationRecipientEvidence.contextNodes.length+ || expectedConversation.contextNodes.some((node, index) => node !== conversationRecipientEvidence.contextNodes[index])+ || expectedConversation.labelledByNodes.length !== conversationRecipientEvidence.labelledByNodes.length+ || expectedConversation.labelledByNodes.some((node, index) => node !== conversationRecipientEvidence.labelledByNodes[index]))) {+ return { success: false, messageSend: null, conclusive: false, dispatchTargetChanged: true,+ identityCandidates: [], strongIdentityCandidates: [],+ error: 'The verified conversation header or composer destination changed before dispatch.' };+ }+const composerText = (() => {try {if ('value' in composer) return String(composer.value || '');@@ -7092,6 +7225,7 @@adapterName: params.adapterName,expectedRecipients: params.expectedRecipients,supportsRecipientSets: params.supportsRecipientSets,+ conversationRecipientEvidence,messageBody,messageBodyBaselineCount,messageAttachment: tool === 'upload_file',diff --git a/src/chrome/src/content/page-monitor.js b/src/chrome/src/content/page-monitor.jsindex 7b31af0c0..c27986de0 100644--- a/src/chrome/src/content/page-monitor.js+++ b/src/chrome/src/content/page-monitor.js@@ -612,6 +612,26 @@}return [...labels].map(node => ({ node, value: modelSemantic(node, { text: true }) }));}+ function currentConversationContext(target) {+ if (!target.closest('footer') || typeof window.__wb_get_message_conversation_context !== 'function') return null;+ try {+ const context = window.__wb_get_message_conversation_context(target);+ if (!context || typeof context.signature !== 'string' || context.signature.length > 8192+ || !Array.isArray(context.contextNodes) || context.contextNodes.length > 32+ || !Array.isArray(context.labelledByNodes) || context.labelledByNodes.length > 32+ || !Array.isArray(context.aliases) || context.aliases.length > 8+ || context.aliases.some(alias => typeof alias !== 'string' || alias.length > 256)) return null;+ const nodes = [context.carrier, context.header, context.pane, context.footer, context.composer,+ ...context.contextNodes, ...context.labelledByNodes];+ if (nodes.some(node => !(node instanceof Element) || node.ownerDocument !== document || !node.isConnected)+ || context.header.tagName !== 'HEADER' || context.footer.tagName !== 'FOOTER'+ || context.pane === document.body || context.pane === document.documentElement+ || !context.header.contains(context.carrier) || !context.footer.contains(target)+ || !context.footer.contains(context.composer) || !context.pane.contains(context.header)+ || !context.pane.contains(context.footer)) return null;+ return context;+ } catch { return null; }+ }function actionFootprint(target) {const structuralFile = target instanceof Element && target.tagName === 'INPUT' && target.type === 'file';if (!(target instanceof Element) || target.ownerDocument !== document || !target.isConnected@@ -624,9 +644,11 @@if (!action || (!structuralFile && !visible(action))) return null;const form = action.form || target.closest('form,[role="form"]');const ownerBase = form || action;- let owner = ownerBase.matches(entityOwnerSelector) ? ownerBase : ownerBase.closest(entityOwnerSelector);+ const conversation = currentConversationContext(target);+ if (conversation && form && !conversation.footer.contains(form)) return null;+ let owner = conversation?.pane || (ownerBase.matches(entityOwnerSelector) ? ownerBase : ownerBase.closest(entityOwnerSelector));let unstructuredOwner = false;- let meaningfulOwner = false;+ let meaningfulOwner = !!conversation;if (!owner && form && [...form.querySelectorAll('h1,h2,h3,h4,h5,h6,[role="heading"],'+modelEntityIdentitySelector)].some(node => node.closest('form,[role="form"]') === form && !node.closest(entityOwnerSelector)&& !node.closest('aside,nav,[role="navigation"],[role="complementary"]'))) owner = form;@@ -679,7 +701,20 @@const controls = form.elements ? [...form.elements] : [...form.querySelectorAll(modelControlSelector)];for (const control of controls) if (!addNode(control, { text: true, control: true })) return null;}- if (owner) {+ if (conversation) {+ // Reuse the recipient guard's exact local conversation proof. Its+ // matcher is re-run for every comparison; history and sidebar controls+ // are outside this footprint, including when their counters update.+ for (const node of new Set([conversation.carrier, conversation.header, conversation.pane,+ conversation.footer, conversation.composer, ...conversation.contextNodes])) {+ if (!addNode(node, node === conversation.carrier || node === conversation.composer+ ? { text: true, control: true, observedName: true } : {})) return null;+ }+ for (const node of conversation.labelledByNodes) if (!addNode(node, { text: true })) return null;+ const controls = conversation.footer.querySelectorAll(modelControlSelector);+ if (controls.length > 64) return null;+ for (const control of controls) if (!addNode(control, { text: true, control: true })) return null;+ } else if (owner) {if (!addNode(owner, { directText: true })) return null;const belongs = node => {const closest = node.closest(entityOwnerSelector);@@ -705,7 +740,7 @@// An arbitrary container/toolbar with no entity anchor cannot establish// the recipient or item. Such targets retain strict legacy dispatch only.if (!owner || !meaningfulOwner) return null;- if (!owner || unstructuredOwner) {+ if (!conversation && (!owner || unstructuredOwner)) {// Without an explicit entity boundary, an outside conversation heading// may identify the recipient. Never infer that a small form/toolbar owns// it; broad heading binding is a conservative fallback for these pages.@@ -717,8 +752,10 @@const hit = structuralFile ? null : deepElementFromPoint(rect.left + rect.width / 2, rect.top + rect.height / 2);if (!structuralFile && (!hit || !(target === hit || target.contains(hit) || (action.contains(target) && action.contains(hit))))) return null;return { model: true, structuralFile, target, scope: owner || form || action, ancestors, nodes, hit,+ ...(conversation ? { conversationCarrier: conversation.carrier, conversationComposer: conversation.composer } : {}),context: JSON.stringify([location.href, document.baseURI, currentViewport(), window.scrollX, window.scrollY,- rect ? [rect.left, rect.top, rect.width, rect.height] : null]) };+ rect ? [rect.left, rect.top, rect.width, rect.height] : null,+ ...(conversation ? [[conversation.signature, conversation.aliases]] : [])]) };}function operationBinding(op, target = op?.preparedBinding?.target) {return op?.preparedBinding?.model === true ? actionFootprint(target) : preparedBinding(target);@@ -855,16 +892,17 @@if (node instanceof Element) { refs.set(ref, node); publicNodes.add(node); }}let evidenceLength = 0;- const exposeIdentity = node => {- if (publicNodes.has(node)) return true;+ const exposedEditableNames = new WeakMap();+ const exposeIdentity = (node, { authoredEditableName = false } = {}) => {+ if (publicNodes.has(node) && !authoredEditableName) return true;// Emit only current rendered page labels. Editable contents, input// values, data identities and private control/file state stay private.- if (!visible(node) || node.matches('input,textarea,select,[contenteditable]:not([contenteditable="false"])')+ if (!visible(node) || (!authoredEditableName && node.matches('input,textarea,select,[contenteditable]:not([contenteditable="false"])'))|| node.closest('[aria-hidden="true"]')) return false;const walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT);const parts = [];let visited = 0, textNode;- while ((textNode = walker.nextNode())) {+ while (!authoredEditableName && (textNode = walker.nextNode())) {if (visited++ >= 128) return false;const parent = textNode.parentElement;if (parent && visible(parent) && !ignored(parent) && !parent.closest(@@ -875,10 +913,12 @@}}const rendered = parts.join(' ');- const authored = String(node.getAttribute('aria-label') || node.getAttribute('title') || node.getAttribute('alt') || '')+ const authored = String(node.getAttribute('aria-label') || node.getAttribute('title')+ || (authoredEditableName ? node.getAttribute('placeholder') : node.getAttribute('alt')) || '').replace(/\s+/g, ' ').trim();const name = authored || rendered;if (!name || authored.length > 512) return false;+ if (authoredEditableName && exposedEditableNames.get(node) === authored) return true;const ref = window.__wb_ax_ref?.(node);if (!ref || window.__wb_ax_lookup?.(ref) !== node) return false;const header = evidenceLength ? '' : '\n\n[CURRENT VISIBLE ACTION IDENTITY]\n';@@ -889,13 +929,26 @@page.pageContent += line;evidenceLength += line.length;refs.set(ref, node); publicNodes.add(node);+ if (authoredEditableName) exposedEditableNames.set(node, authored);return true;};const observedFootprint = footprint => {if (!page) return true;if (!footprint.structuralFile && !publicNodes.has(footprint.target)) return false;+ if (footprint.conversationComposer) {+ const composer = footprint.conversationComposer;+ const authored = String(composer.getAttribute('aria-label') || composer.getAttribute('title')+ || composer.getAttribute('placeholder') || '').replace(/\s+/g, ' ').trim();+ // The private recipient proof may bind a destination label omitted+ // from a large AX tree. Expose only that complete authored label,+ // never editor contents, before certifying the footer action.+ if (authored && (!publicNodes.has(composer) || authored.length > 120)) {+ if (!exposeIdentity(composer, { authoredEditableName: true })) return false;+ } else if (!publicNodes.has(composer)) return false;+ }const evidence = footprint.nodes.filter(item => item.node !== footprint.target- && item.node.matches('h1,h2,h3,h4,h5,h6,[role="heading"],[aria-current],'+modelEntityIdentitySelector)+ && (item.node === footprint.conversationCarrier+ || item.node.matches('h1,h2,h3,h4,h5,h6,[role="heading"],[aria-current],'+modelEntityIdentitySelector))&& visible(item.node));return evidence.every(item => exposeIdentity(item.node))&& (publicNodes.has(footprint.scope) || evidence.some(item => publicNodes.has(item.node)));diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex dd994c858..a45f22cce 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -22289,7 +22289,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d&& guard?.requiresSubmission === true && guard?.requiresStateChange === true? this._resolveTwitterNamedMessagingTarget(target, probe) : null;if (resolvedTwitterTarget) target = resolvedTwitterTarget;- const messageBodyBaselineCount = Number(probe?.messageBodyBaselineCount);+ const messageBodyBaselineCount = probe?.messageBodyBaselineCount;const targetMatchesObserved = messageTargetMatchesObservedIdentities(target,this._messageRecipientCandidates(probe),@@ -22301,6 +22301,34 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst approvedByUser = guard?.messageRecipientApprovedAll === true|| (observedIdentities.length > 0&& observedIdentities.every(identity => approvedRecipients.includes(identity)));+ const technicalBlock = (reasonCode, error) => {+ if (guard) {+ guard.pendingRecipientAuthorization = false;+ guard.observedRecipientCandidates = null;+ guard.messageRecipientTechnicalBlock = reasonCode;+ }+ return { success: false, blocked: true, noDispatch: true, dispatched: false,+ messageRecipientGuard: true, retryable: false, recipientAuthorizationRequired: false,+ reasonCode, error };+ };+ const conclusiveSend = probe?.success === true && probe?.conclusive === true && probe?.messageSend === true;+ if (conclusiveSend && observedIdentities.length === 0) {+ return technicalBlock('recipient_identity_unavailable',+ 'Message send blocked because WebBrain cannot establish the active recipient identity from this page. Re-read the active conversation header or open the intended chat. If identity remains unavailable, report this technical blocker; additional recipient approval cannot resolve missing page evidence.');+ }+ if (conclusiveSend && observedIdentities.length > 0 && guard) delete guard.messageRecipientTechnicalBlock;+ // An already authorized recipient can still lack draft or delivery+ // evidence. Asking to authorize that recipient again cannot repair it.+ if (conclusiveSend && (targetMatchesObserved || approvedByUser)) {+ if (name !== 'upload_file' && !this._workflowMessageBody(probe?.messageBody)) {+ return technicalBlock('message_body_unverified',+ 'Message send blocked because WebBrain could not verify the draft body for the authorized recipient. Re-read the current composer and verify the intended draft. If the body remains unavailable, report this technical blocker instead of asking for recipient approval again.');+ }+ if (!Number.isInteger(messageBodyBaselineCount) || messageBodyBaselineCount < 0) {+ return technicalBlock('message_body_baseline_unavailable',+ 'Message send blocked because WebBrain could not establish the existing-message baseline needed to verify a new send. Re-read the active conversation before attempting delivery. If the baseline remains unavailable, report this technical blocker instead of asking for recipient approval again.');+ }+ }const verified = probe?.success === true&& probe.messageSend === true&& (name === 'upload_file'@@ -22313,15 +22341,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (verified) {const binding = probe?.messageRecipientDispatchBinding;if (!binding?.token) {- return {- success: false,- blocked: true,- noDispatch: true,- dispatched: false,- messageRecipientGuard: true,- reasonCode: 'recipient_dispatch_binding_unavailable',- error: 'Message send blocked because WebBrain could not bind recipient verification to the final action dispatch. Re-read the active conversation and retry once.',- };+ return technicalBlock('recipient_dispatch_binding_unavailable',+ 'Message send blocked because WebBrain could not bind recipient verification to the final action dispatch. Re-read the active conversation and retry once. If binding remains unavailable, report this technical blocker instead of asking for recipient approval again.');}if (resolvedTwitterTarget) guard.messaging = resolvedTwitterTarget;if (executionContext && typeof executionContext === 'object') {@@ -28225,7 +28246,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d? this._continuationExecutionEvidence.get(tabId): null;this._continuationExecutionEvidence.delete(tabId);- const carryMatches = enabled+ const carryScopeMatches = enabled&& carried?.requestKind === 'execute'&& carried.requiresStateChange === requiresStateChange&& carried.requiresSubmission === requiresSubmission@@ -28238,8 +28259,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d&& JSON.stringify(carried.siteWorkflow || null) === JSON.stringify(siteWorkflow || null)&& JSON.stringify(carried.conditionalSiteWorkflow || null) === JSON.stringify(gateOutcome.conditionalSiteWorkflow || null)&& carried.taskKey === taskKey- && carried.evidenceTaskKey === taskKey&& carried.conversationId === (this.conversationIds.get(tabId) || null);+ const carryMatches = carryScopeMatches && carried.evidenceTaskKey === taskKey;+ // Human recipient grants are task authorization, not execution evidence.+ // They can survive Continue even when the last step only approved a send.+ const carryRecipientAuthorization = carryScopeMatches && requiresSubmission === true+ && requiresStateChange === true && !!messaging;const approvedPlanAnchor = gateApprovedPlanAnchor|| (carryMatches ? carried.approvedPlanAnchor || '' : '');const approvedPlanText = String(gateOutcome?.approvedScratchpadText || '')@@ -28252,6 +28277,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dsiteWorkflowUrl: String(gateOutcome?.siteWorkflowUrl || ''),messaging,messagingConversationScope,+ approvedRecipients: carryRecipientAuthorization && Array.isArray(carried.approvedRecipients)+ ? [...carried.approvedRecipients] : [],+ messageRecipientApprovedAll: carryRecipientAuthorization && carried.messageRecipientApprovedAll === true,draftRecipients,requiresDownload,allowsPlannerShapedResult: gateOutcome?.allowsPlannerShapedResult === true,@@ -28653,8 +28681,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d_storeContinuationExecutionEvidence(tabId) {const guard = this._planExecutionGuards.get(tabId);+ const hasRecipientAuthorization = guard?.requiresSubmission === true && guard?.requiresStateChange === true+ && !!normalizeMessageTarget(guard.messaging)+ && (guard.messageRecipientApprovedAll === true || (guard.approvedRecipients || []).length > 0);if (guard?.enabled && !guard.taskDrifted && (- guard.successfulTaskToolCalls > 0+ hasRecipientAuthorization+ || guard.successfulTaskToolCalls > 0|| guard.successfulConsequentialToolCalls > 0|| guard.pendingDownloadIds.length > 0|| guard.verifiedSubmissionEvidence === true@@ -28678,6 +28710,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout drequiresSubmission: guard.requiresSubmission,messaging: guard.messaging,messagingConversationScope: guard.messagingConversationScope,+ approvedRecipients: hasRecipientAuthorization && Array.isArray(guard.approvedRecipients)+ ? [...guard.approvedRecipients] : [],+ messageRecipientApprovedAll: hasRecipientAuthorization && guard.messageRecipientApprovedAll === true,draftRecipients: guard.draftRecipients,requiresDownload: guard.requiresDownload,allowsAppStateToolEvidence: guard.allowsAppStateToolEvidence,@@ -31938,6 +31973,25 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d: (args?.purpose === 'recipient_change'? 'recipient_change': (args?.purpose === 'message_recipient' ? 'message_recipient' : null));+ const explicitRecipientQuestion = purpose === 'message_recipient' || purpose === 'recipient_change';+ const recipientBindingRequired = explicitRecipientQuestion+ || (!!clarificationGuard?.pendingRecipientAuthorization+ && this._isRecipientClarification({ question, options, reason, purpose },+ typeof clarificationGuard.pendingRecipientAuthorization === 'string'+ ? clarificationGuard.pendingRecipientAuthorization : 'message_recipient',+ clarificationGuard.observedRecipientCandidates || []));+ const technicalRecipientReasons = new Set(['recipient_identity_unavailable', 'message_body_unverified',+ 'message_body_baseline_unavailable', 'recipient_dispatch_binding_unavailable']);+ const technicalRecipientReason = technicalRecipientReasons.has(clarificationGuard?.messageRecipientTechnicalBlock)+ ? clarificationGuard.messageRecipientTechnicalBlock : '';+ if (technicalRecipientReason && (explicitRecipientQuestion+ || this._isRecipientClarification({ question, options, reason, purpose }, 'recipient_change', [])+ || this._isRecipientClarification({ question, options, reason, purpose }, 'message_recipient', []))) {+ return { success: false, blocked: true, noDispatch: true, dispatched: false, retryable: false,+ messageRecipientGuard: true, reasonCode: technicalRecipientReason,+ recipientBinding: { required: true, bound: false, reasonCode: technicalRecipientReason },+ error: 'Recipient confirmation cannot repair the unresolved technical message guard. No clarification was shown. Re-read the active recipient and composer evidence; if it remains unavailable, report the blocker instead of requesting the same approval again.' };+ }const isResearchEscalation = purpose === 'research_escalation';const requireExplicitAnswer = isResearchEscalation || args?.require_explicit_answer === true;const researchRequest = isResearchEscalation ? normalizeResearchRequest(args?.research_request) : '';@@ -32040,7 +32094,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (response && response.cancelled) {this._bindClarifiedMessageRecipient(tabId, '', 'cancelled', { question, options, reason, purpose });- return { success: false, cancelled: true, reason: response.reason || 'clarify cancelled' };+ return { success: false, cancelled: true, reason: response.reason || 'clarify cancelled',+ ...(recipientBindingRequired ? { recipientBinding: { required: true, bound: false,+ reasonCode: 'recipient_clarification_cancelled' } } : {}) };}const answer = String(response?.answer || '').trim();const source = response?.source || 'user';@@ -32053,7 +32109,11 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// nothing, matching how research escalation treats those sources below.// 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, { question, options, reason, purpose, safeFirst: entrySafeFirst });+ const recipientBound = this._bindClarifiedMessageRecipient(tabId, answer, source,+ { question, options, reason, purpose, safeFirst: entrySafeFirst });+ const recipientBinding = recipientBindingRequired ? { required: true, bound: recipientBound,+ ...(!recipientBound ? { reasonCode: technicalRecipientReasons.has(clarificationGuard?.messageRecipientTechnicalBlock)+ ? clarificationGuard.messageRecipientTechnicalBlock : 'recipient_authorization_not_bound' } : {}) } : null;this._recordSocialPublicationClarification(tabId, clarificationGuard, question, answer, source);const explicitResearchApproval = isResearchEscalation&& source !== 'timeout'@@ -32083,6 +32143,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dsource,authorized,requiresExplicitConfirmation: !authorized,+ ...(recipientBinding ? { recipientBinding } : {}),...(isResearchEscalation ? {researchEscalationApproved: explicitResearchApproval,authorization_token: authorizationToken,diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.jsindex 86668d772..843a26930 100644--- a/src/firefox/src/content/content.js+++ b/src/firefox/src/content/content.js@@ -2512,6 +2512,11 @@} catch {}}+ // Firefox prepares focus before this click. Attribute only that exact+ // intended target, while retaining the existing preparation fences.+ if (_isTypeableElement(el) || _isFocusableElement(el)) {+ window.__wbPageMonitor?.beforeLocalDispatch({ preparation: true, kind: 'focus', target: el });+ }if (_isTypeableElement(el)) {_focusElement(el);_rememberEditableTarget(el);@@ -4395,6 +4400,7 @@)).filter(value => value.identity && /^(?:to|cc|bcc)$/.test(value.role)).slice(0, 16): [],supportsRecipientSets: dispatch.supportsRecipientSets === true,+ conversationRecipientEvidence: dispatch.conversationRecipientEvidence || null,identityKey,messageBody: String(dispatch.messageBody || ''),messageBodyBaselineCount: Number(dispatch.messageBodyBaselineCount || 0),@@ -4471,6 +4477,7 @@expectedRecipients: expected.expectedRecipients,supportsRecipientSets: expected.supportsRecipientSets,uploadSourceKey: expected.uploadSourceKey,+ expectedConversationRecipientEvidence: expected.conversationRecipientEvidence,});if (live?.dispatchTargetChanged === true) {return {@@ -4785,7 +4792,36 @@return !!text && _MESSAGE_COMMIT_LABEL_RE.test(text);}- function _probeMessageRecipientGuard(params = {}) {+ // Isolated-world, read-only bridge for private pre-inference footprints.+ // Only controls in the verified composer's own native footer are eligible;+ // neither runtime params nor the page's main world receive DOM references.+ window.__wb_get_message_conversation_context = target => {+ try {+ if (WEBBRAIN_GENERATION !== (Number(window.__webbrain_generation) || 0)+ || target?.nodeType !== 1 || !target.isConnected || target.ownerDocument !== document) return null;+ const footer = target.closest('footer');+ const control = target.closest('button,[role="button"],input,select,textarea,'+ + '[contenteditable="true"],[contenteditable=""],[role="textbox"]');+ const excluded = 'aside,nav,search,[role="navigation"],[role="complementary"],[role="search"],'+ + '[role="searchbox"],[role="log"],[role="listbox"],[role="menu"],'+ + '[data-message-id],[data-message-direction],[data-outgoing],article';+ if (!footer || !control || !footer.contains(control) || target.closest(excluded)+ || /^search$/i.test(control.getAttribute('type') || '')) return null;+ let context = null;+ _probeMessageRecipientGuard({ tool: 'observe_active_conversation', adapterName: 'generic-messaging', args: {} }, evidence => {+ if (evidence?.footer !== footer || evidence.composer?.closest('footer') !== footer+ || !footer.contains(target) || !evidence.pane.contains(evidence.carrier)) return null;+ context = Object.freeze({ ...evidence, name: evidence.aliases[0],+ aliases: Object.freeze([...evidence.aliases]),+ labelledByNodes: Object.freeze([...evidence.labelledByNodes]),+ contextNodes: Object.freeze([...evidence.contextNodes]) });+ return null;+ });+ return context;+ } catch { return null; }+ };++ function _probeMessageRecipientGuard(params = {}, receiveConversationContext = null) {try {const tool = String(params.tool || '');const observationOnly = tool === 'observe_active_conversation';@@ -5711,6 +5747,7 @@const strongRecipients = [];const strongSeen = new Set();const observedRecipientCandidates = [];+ let conversationRecipientEvidence = null;const gmailRecipientMode = params.adapterName === 'gmail';const inConversationHeaderBand = (el) => {if (headerBandBottom <= 0) return false;@@ -5990,13 +6027,101 @@addStrongIdentity(el);if (strongIdentities.length >= 8) break;}+ if (!strongIdentities.length && params.adapterName === 'generic-messaging'+ && params.supportsRecipientSets !== true) {+ // Some conversation headers expose the contact name as a button+ // rather than a heading. A shared local header/footer pane proves+ // ownership; a page-wide button or a sidebar name proves nothing.+ try {+ const footer = composer.closest?.('footer');+ const excluded = 'aside,nav,[role="navigation"],[role="complementary"],[role="log"],'+ + '[role="listbox"],[role="menu"],[role="toolbar"],[data-message-id],[data-message-direction],[data-outgoing],article';+ const actionLabel = /^(?:call(?:\s|$)|(?:voice|video|audio)\s+call(?:\s|$)|search(?:\s|$)|menu(?:\s|$)|more(?:\s|$)|settings(?:\s|$)|back(?:\s|$)|close(?:\s|$)|arama(?:\s|$)|ara(?:\s|$)|suchen(?:\s|$)|rechercher(?:\s|$)|buscar(?:\s|$)|llamar(?:\s|$)|appel(?:\s|$))/iu;+ if (footer && !footer.closest(excluded)) {+ for (let pane = footer.parentElement, depth = 0; pane && depth < 8+ && pane !== document.body && pane !== document.documentElement; pane = pane.parentElement, depth++) {+ if (pane.closest(excluded)) break;+ const ownedHeader = header => {+ let branch = header;+ for (let level = 0; level < 4; level++) {+ const parent = branch.parentElement;+ if (parent === pane) return true;+ if (parent?.tagName !== 'DIV' || parent.children.length !== 1) return false;+ branch = parent;+ }+ return false;+ };+ const headers = Array.from(pane.querySelectorAll('header')).filter(header => (+ ownedHeader(header) && visible(header) && !footer.contains(header) && !header.closest(excluded)+ && inConversationHeaderBand(header) && !independentScrollableRegion(header, composer)+ ));+ if (!headers.length) continue;+ // Stop at the closest pane with a header, including when its+ // evidence is ambiguous. A broader app header cannot rescue it.+ if (headers.length !== 1 || composerCandidates.some(editor => editor !== composer+ && pane.contains(editor) && !composer.contains(editor) && !editor.contains(composer)+ && !verifiedNavigationEditable(editor))) break;+ const header = headers[0];+ const modal = typeof _findTopmostBlockingModal === 'function' ? _findTopmostBlockingModal() : null;+ if (modal && (!modal.contains(composer) || !modal.contains(header))) break;+ const contacts = Array.from(header.querySelectorAll('button,[role="button"]')).filter(control => {+ if (!visible(control) || editable(control) || control.closest('header') !== header+ || control.closest(excluded) || control.disabled || control.getAttribute('aria-disabled') === 'true'+ || control.form || control.hasAttribute('form') || !inConversationHeaderBand(control)+ || independentScrollableRegion(control, composer)) return false;+ const name = String(control.innerText || '').replace(/[\u200b-\u200d\ufeff]/g, '').replace(/\s+/g, ' ').trim();+ const labels = [name, control.getAttribute('aria-label'), control.getAttribute('title')].filter(Boolean);+ return !!name && name.length <= 120 && !labels.some(label => actionLabel.test(String(label).trim())+ || _hasMessageCommitName(label) || _COMPOSER_UTILITY_LABEL_RE.test(String(label).trim()));+ });+ if (contacts.length !== 1) break;+ const carrier = contacts[0];+ const name = compact(carrier.innerText, 120);+ const labelledByNodes = [];+ let labelledByValid = true;+ for (const id of String(composer.getAttribute('aria-labelledby') || '').split(/\s+/).filter(Boolean)) {+ const matches = composer.getRootNode().querySelectorAll(`[id="${CSS.escape(id)}"]`);+ if (matches.length !== 1) { labelledByValid = false; break; }+ labelledByNodes.push(matches[0]);+ }+ if (!labelledByValid) break;+ const identityAttributes = ['id','role','aria-label','title','aria-labelledby','aria-hidden','inert',+ 'data-recipient-id','data-conversation-id','data-thread-id','data-chat-id'];+ const contextNodes = new Set([carrier, header, pane, footer, composer]);+ for (const start of [carrier, composer]) {+ for (let node = start.parentElement; node && node !== pane; node = node.parentElement) contextNodes.add(node);+ }+ conversationRecipientEvidence = { carrier, header, pane, footer, composer, labelledByNodes, contextNodes: [...contextNodes],+ signature: JSON.stringify([name,+ [...contextNodes].map(node => identityAttributes.map(attribute => node.getAttribute(attribute))),+ ['aria-label','aria-labelledby','placeholder','title'].map(attribute => composer.getAttribute(attribute)),+ labelledByNodes.map(node => node.textContent)]) };+ strongIdentities.push(name);+ const phone = String(composer.getAttribute('aria-label') || '').trim()+ .match(/^(?:type|write|send)\s+(?:a\s+)?message\s+to\s+(\+?[\d ()-]{7,32})$/i)?.[1]?.trim() || '';+ const phoneDigits = phone.replace(/\D/g, '').length;+ conversationRecipientEvidence.aliases = phone && phoneDigits >= 7 && phoneDigits <= 15+ ? [name, phone] : [name];+ break;+ }+ }+ } catch { /* An unsupported header remains unverified. */ }+ }for (const identity of strongIdentities) {- const item = { identity, role: 'to', aliases: [identity] };+ const item = { identity, role: 'to', aliases: conversationRecipientEvidence?.aliases || [identity] };strongRecipients.push({ identity, role: 'to' });observedRecipientCandidates.push(item);}}+ // The isolated monitor reads this exact local proof synchronously. Its+ // closure-only callback never participates in the public message API,+ // authorizes an action, or needs the expensive message-history scan.+ if (typeof receiveConversationContext === 'function') {+ return receiveConversationContext(observationOnly && params.adapterName === 'generic-messaging'+ && params.supportsRecipientSets !== true ? conversationRecipientEvidence : null);+ }+// Generic mail surfaces expose recipient fields and chips without a// provider-specific adapter. Read only explicit recipient semantics;// an ordinary contact form's email field must not become a message@@ -6085,6 +6210,19 @@} catch { /* structural read is best-effort */ }}+ const expectedConversation = params.expectedConversationRecipientEvidence;+ if (expectedConversation && (!conversationRecipientEvidence+ || ['carrier','header','pane','footer'].some(key => expectedConversation[key] !== conversationRecipientEvidence[key])+ || expectedConversation.signature !== conversationRecipientEvidence.signature+ || expectedConversation.contextNodes.length !== conversationRecipientEvidence.contextNodes.length+ || expectedConversation.contextNodes.some((node, index) => node !== conversationRecipientEvidence.contextNodes[index])+ || expectedConversation.labelledByNodes.length !== conversationRecipientEvidence.labelledByNodes.length+ || expectedConversation.labelledByNodes.some((node, index) => node !== conversationRecipientEvidence.labelledByNodes[index]))) {+ return { success: false, messageSend: null, conclusive: false, dispatchTargetChanged: true,+ identityCandidates: [], strongIdentityCandidates: [],+ error: 'The verified conversation header or composer destination changed before dispatch.' };+ }+const composerText = (() => {try {if ('value' in composer) return String(composer.value || '');@@ -6132,6 +6270,7 @@adapterName: params.adapterName,expectedRecipients: params.expectedRecipients,supportsRecipientSets: params.supportsRecipientSets,+ conversationRecipientEvidence,messageBody,messageBodyBaselineCount,messageAttachment: tool === 'upload_file',diff --git a/src/firefox/src/content/page-monitor.js b/src/firefox/src/content/page-monitor.jsindex 7b31af0c0..c27986de0 100644--- a/src/firefox/src/content/page-monitor.js+++ b/src/firefox/src/content/page-monitor.js@@ -612,6 +612,26 @@}return [...labels].map(node => ({ node, value: modelSemantic(node, { text: true }) }));}+ function currentConversationContext(target) {+ if (!target.closest('footer') || typeof window.__wb_get_message_conversation_context !== 'function') return null;+ try {+ const context = window.__wb_get_message_conversation_context(target);+ if (!context || typeof context.signature !== 'string' || context.signature.length > 8192+ || !Array.isArray(context.contextNodes) || context.contextNodes.length > 32+ || !Array.isArray(context.labelledByNodes) || context.labelledByNodes.length > 32+ || !Array.isArray(context.aliases) || context.aliases.length > 8+ || context.aliases.some(alias => typeof alias !== 'string' || alias.length > 256)) return null;+ const nodes = [context.carrier, context.header, context.pane, context.footer, context.composer,+ ...context.contextNodes, ...context.labelledByNodes];+ if (nodes.some(node => !(node instanceof Element) || node.ownerDocument !== document || !node.isConnected)+ || context.header.tagName !== 'HEADER' || context.footer.tagName !== 'FOOTER'+ || context.pane === document.body || context.pane === document.documentElement+ || !context.header.contains(context.carrier) || !context.footer.contains(target)+ || !context.footer.contains(context.composer) || !context.pane.contains(context.header)+ || !context.pane.contains(context.footer)) return null;+ return context;+ } catch { return null; }+ }function actionFootprint(target) {const structuralFile = target instanceof Element && target.tagName === 'INPUT' && target.type === 'file';if (!(target instanceof Element) || target.ownerDocument !== document || !target.isConnected@@ -624,9 +644,11 @@if (!action || (!structuralFile && !visible(action))) return null;const form = action.form || target.closest('form,[role="form"]');const ownerBase = form || action;- let owner = ownerBase.matches(entityOwnerSelector) ? ownerBase : ownerBase.closest(entityOwnerSelector);+ const conversation = currentConversationContext(target);+ if (conversation && form && !conversation.footer.contains(form)) return null;+ let owner = conversation?.pane || (ownerBase.matches(entityOwnerSelector) ? ownerBase : ownerBase.closest(entityOwnerSelector));let unstructuredOwner = false;- let meaningfulOwner = false;+ let meaningfulOwner = !!conversation;if (!owner && form && [...form.querySelectorAll('h1,h2,h3,h4,h5,h6,[role="heading"],'+modelEntityIdentitySelector)].some(node => node.closest('form,[role="form"]') === form && !node.closest(entityOwnerSelector)&& !node.closest('aside,nav,[role="navigation"],[role="complementary"]'))) owner = form;@@ -679,7 +701,20 @@const controls = form.elements ? [...form.elements] : [...form.querySelectorAll(modelControlSelector)];for (const control of controls) if (!addNode(control, { text: true, control: true })) return null;}- if (owner) {+ if (conversation) {+ // Reuse the recipient guard's exact local conversation proof. Its+ // matcher is re-run for every comparison; history and sidebar controls+ // are outside this footprint, including when their counters update.+ for (const node of new Set([conversation.carrier, conversation.header, conversation.pane,+ conversation.footer, conversation.composer, ...conversation.contextNodes])) {+ if (!addNode(node, node === conversation.carrier || node === conversation.composer+ ? { text: true, control: true, observedName: true } : {})) return null;+ }+ for (const node of conversation.labelledByNodes) if (!addNode(node, { text: true })) return null;+ const controls = conversation.footer.querySelectorAll(modelControlSelector);+ if (controls.length > 64) return null;+ for (const control of controls) if (!addNode(control, { text: true, control: true })) return null;+ } else if (owner) {if (!addNode(owner, { directText: true })) return null;const belongs = node => {const closest = node.closest(entityOwnerSelector);@@ -705,7 +740,7 @@// An arbitrary container/toolbar with no entity anchor cannot establish// the recipient or item. Such targets retain strict legacy dispatch only.if (!owner || !meaningfulOwner) return null;- if (!owner || unstructuredOwner) {+ if (!conversation && (!owner || unstructuredOwner)) {// Without an explicit entity boundary, an outside conversation heading// may identify the recipient. Never infer that a small form/toolbar owns// it; broad heading binding is a conservative fallback for these pages.@@ -717,8 +752,10 @@const hit = structuralFile ? null : deepElementFromPoint(rect.left + rect.width / 2, rect.top + rect.height / 2);if (!structuralFile && (!hit || !(target === hit || target.contains(hit) || (action.contains(target) && action.contains(hit))))) return null;return { model: true, structuralFile, target, scope: owner || form || action, ancestors, nodes, hit,+ ...(conversation ? { conversationCarrier: conversation.carrier, conversationComposer: conversation.composer } : {}),context: JSON.stringify([location.href, document.baseURI, currentViewport(), window.scrollX, window.scrollY,- rect ? [rect.left, rect.top, rect.width, rect.height] : null]) };+ rect ? [rect.left, rect.top, rect.width, rect.height] : null,+ ...(conversation ? [[conversation.signature, conversation.aliases]] : [])]) };}function operationBinding(op, target = op?.preparedBinding?.target) {return op?.preparedBinding?.model === true ? actionFootprint(target) : preparedBinding(target);@@ -855,16 +892,17 @@if (node instanceof Element) { refs.set(ref, node); publicNodes.add(node); }}let evidenceLength = 0;- const exposeIdentity = node => {- if (publicNodes.has(node)) return true;+ const exposedEditableNames = new WeakMap();+ const exposeIdentity = (node, { authoredEditableName = false } = {}) => {+ if (publicNodes.has(node) && !authoredEditableName) return true;// Emit only current rendered page labels. Editable contents, input// values, data identities and private control/file state stay private.- if (!visible(node) || node.matches('input,textarea,select,[contenteditable]:not([contenteditable="false"])')+ if (!visible(node) || (!authoredEditableName && node.matches('input,textarea,select,[contenteditable]:not([contenteditable="false"])'))|| node.closest('[aria-hidden="true"]')) return false;const walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT);const parts = [];let visited = 0, textNode;- while ((textNode = walker.nextNode())) {+ while (!authoredEditableName && (textNode = walker.nextNode())) {if (visited++ >= 128) return false;const parent = textNode.parentElement;if (parent && visible(parent) && !ignored(parent) && !parent.closest(@@ -875,10 +913,12 @@}}const rendered = parts.join(' ');- const authored = String(node.getAttribute('aria-label') || node.getAttribute('title') || node.getAttribute('alt') || '')+ const authored = String(node.getAttribute('aria-label') || node.getAttribute('title')+ || (authoredEditableName ? node.getAttribute('placeholder') : node.getAttribute('alt')) || '').replace(/\s+/g, ' ').trim();const name = authored || rendered;if (!name || authored.length > 512) return false;+ if (authoredEditableName && exposedEditableNames.get(node) === authored) return true;const ref = window.__wb_ax_ref?.(node);if (!ref || window.__wb_ax_lookup?.(ref) !== node) return false;const header = evidenceLength ? '' : '\n\n[CURRENT VISIBLE ACTION IDENTITY]\n';@@ -889,13 +929,26 @@page.pageContent += line;evidenceLength += line.length;refs.set(ref, node); publicNodes.add(node);+ if (authoredEditableName) exposedEditableNames.set(node, authored);return true;};const observedFootprint = footprint => {if (!page) return true;if (!footprint.structuralFile && !publicNodes.has(footprint.target)) return false;+ if (footprint.conversationComposer) {+ const composer = footprint.conversationComposer;+ const authored = String(composer.getAttribute('aria-label') || composer.getAttribute('title')+ || composer.getAttribute('placeholder') || '').replace(/\s+/g, ' ').trim();+ // The private recipient proof may bind a destination label omitted+ // from a large AX tree. Expose only that complete authored label,+ // never editor contents, before certifying the footer action.+ if (authored && (!publicNodes.has(composer) || authored.length > 120)) {+ if (!exposeIdentity(composer, { authoredEditableName: true })) return false;+ } else if (!publicNodes.has(composer)) return false;+ }const evidence = footprint.nodes.filter(item => item.node !== footprint.target- && item.node.matches('h1,h2,h3,h4,h5,h6,[role="heading"],[aria-current],'+modelEntityIdentitySelector)+ && (item.node === footprint.conversationCarrier+ || item.node.matches('h1,h2,h3,h4,h5,h6,[role="heading"],[aria-current],'+modelEntityIdentitySelector))&& visible(item.node));return evidence.every(item => exposeIdentity(item.node))&& (publicNodes.has(footprint.scope) || evidence.some(item => publicNodes.has(item.node)));diff --git a/test/message-recipient-whatsapp.mjs b/test/message-recipient-whatsapp.mjsnew file mode 100644index 000000000..20ff3e427--- /dev/null+++ b/test/message-recipient-whatsapp.mjs@@ -0,0 +1,306 @@+import assert from 'node:assert/strict';+import { readFile } from 'node:fs/promises';+import path from 'node:path';+import { test } from 'node:test';+import { chromium, firefox } from 'playwright';+import { Agent as ChromeAgent } from '../src/chrome/src/agent/agent.js';+import { Agent as FirefoxAgent } from '../src/firefox/src/agent/agent.js';++// These local pages exercise the real content observer and one-use dispatch+// validator. Every navigation is intercepted; no external message is sent.+const sendArgs = { selector: '#send' };+const phone = '+90 555 000 00 01';+const call = (page, action, params = {}) => page.evaluate(message => new Promise(resolve => {+ for (const handler of window.fixtureHandlers) handler(message, {}, resolve);+}), { target: 'content', action, params });++async function fixture(kind, engine, Agent, options = {}) {+ const browser = await engine.launch({ headless: true });+ const page = await browser.newPage({ viewport: { width: 1280, height: 800 } });+ await page.route('**/*', route => route.fulfill({ contentType: 'text/html', body: '<!doctype html>' }));+ await page.goto(options.url || 'https://web.whatsapp.com/');+ await page.setContent(`<!doctype html><style>+ body{margin:0;font:16px sans-serif}aside{position:absolute;left:0;top:0;width:300px;height:790px;overflow:auto}+ #main{position:absolute;left:320px;top:0;width:940px;height:790px}+ #main>header{height:70px;display:flex;align-items:center;gap:30px;padding-left:70px}+ #contact{width:278px;height:20px}#messages{height:580px;overflow:auto}+ footer{display:flex;gap:15px;align-items:center}#body{width:550px;min-height:32px}+ button{min-height:28px}#alien-header{position:absolute;left:330px;top:80px}+ </style><aside><header><h1>WhatsApp</h1></header><button>Anne</button><h2>Sidebar recipient</h2></aside>+ <main id="main"><header><img alt="" width="40" height="40">+ <div id="contact" role="button" tabindex="0"><span title="Anne">Anne</span></div>+ <button id="call" aria-label="Voice call">Call</button><button aria-label="Search">Search</button>+ </header><section id="messages"><div>Anne</div><button>Other person</button></section>+ <footer><button id="attach">Attach</button>+ <div id="body" contenteditable="true" role="textbox" aria-label="Type a message to ${phone}">Selam</div>+ <input id="file" type="file" accept="image/*" hidden><button id="send" aria-label="Send">Send</button>+ </footer></main>`);+ await page.evaluate(() => {+ window.fixtureHandlers = [];+ const runtime = { onMessage: { addListener(fn) { window.fixtureHandlers.push(fn); } }, getURL: p => p };+ window.chrome = { runtime }; window.browser = { runtime };+ window.sent = 0;+ document.querySelector('#send').addEventListener('click', () => window.sent++);+ });+ for (const file of ['rich-text-toolbar-heuristic.js', 'accessibility-tree.js', 'content.js']) {+ await page.addScriptTag({ content: await readFile(path.resolve('src', kind, 'src/content', file), 'utf8') });+ }+ const agent = new Agent({ getActive: () => ({ supportsVision: false }) });+ agent._currentUrl = async () => page.url();+ agent._messageRecipientContentProbe = (_, params) => call(page, 'probe_message_recipient_guard', params);+ agent._planExecutionGuards.set(71, { messaging: { target_kind: 'named', recipients: ['anne'] },+ requiresStateChange: true, requiresSubmission: true });+ const probe = (tool = 'click', args = sendArgs) => call(page, 'probe_message_recipient_guard', {+ adapterName: 'generic-messaging', tool, args, bindDispatch: true, expectedRecipients: ['anne'],+ });+ const bind = async (tool = 'click', args = sendArgs) => {+ const context = {};+ const blocked = await agent._messageRecipientGuardBlock(71, tool, args, page.url(), context);+ return { blocked, context };+ };+ return { browser, page, agent, probe, bind };+}++for (const [kind, engine, Agent] of [['chrome', chromium, ChromeAgent], ['firefox', firefox, FirefoxAgent]]) {+ test(`${kind}: WhatsApp contact button permits the named chat without a clarification`, async () => {+ const f = await fixture(kind, engine, Agent);+ try {+ const observed = await f.probe();+ assert.equal(observed.success, true);+ assert.equal(observed.conclusive, true);+ assert.ok(observed.strongRecipientCandidates.some(recipient => recipient.identity === 'Anne'), JSON.stringify(observed));+ assert.equal(observed.strongRecipientCandidates.length, 1, 'sidebar, app title and toolbar must not become recipients');+ assert.ok(observed.observedRecipientCandidates[0].aliases.includes(phone), 'the authored destination must stay tied to the observed contact');+ const { blocked, context } = await f.bind();+ assert.equal(blocked, null, JSON.stringify(blocked));+ assert.equal(context.messageRecipientGuardRequired, true);+ assert.ok(context.messageRecipientDispatchBinding?.token);+ const result = await call(f.page, 'click', { ...sendArgs, ...context });+ assert.equal(result.success, true, JSON.stringify(result));+ assert.equal(await f.page.evaluate(() => window.sent), 1);+ assert.equal(f.agent._planExecutionGuards.get(71).pendingRecipientAuthorization, undefined);+ } finally { await f.browser.close(); }+ });++ test(`${kind}: one real recipient approval persists for repeated sends`, async () => {+ const f = await fixture(kind, engine, Agent);+ try {+ f.agent._planExecutionGuards.get(71).messaging = { target_kind: 'named', recipients: ['Different recipient'] };+ const first = await f.bind();+ assert.equal(first.blocked?.reasonCode, 'active_recipient_unverified', JSON.stringify(first.blocked));+ f.agent.clarifyTimeoutSec = -1;+ f.agent._recordClarificationAuthorization = async () => true;+ let questions = 0;+ const answer = 'Send all remaining messages in this task without asking again';+ const approval = await f.agent._executeToolImpl(71, 'clarify', {+ purpose: 'message_recipient', question: 'Send to Anne instead?',+ options: ['Anne', answer, 'Cancel'],+ }, (status, details) => {+ if (status !== 'clarify') return;+ questions++;+ f.agent._settleClarification(f.agent._pendingClarifications.get(71).get(details.clarifyId),+ { answer, source: 'option' });+ });+ assert.equal(approval.authorized, true);+ assert.deepEqual(approval.recipientBinding, { required: true, bound: true });+ assert.equal(questions, 1);+ for (let attempt = 0; attempt < 2; attempt++) {+ const next = await f.bind();+ assert.equal(next.blocked, null, JSON.stringify(next.blocked));+ const result = await call(f.page, 'click', { ...sendArgs, ...next.context });+ assert.equal(result.success, true, JSON.stringify(result));+ }+ assert.equal(await f.page.evaluate(() => window.sent), 2);+ assert.equal(f.agent._planExecutionGuards.get(71).messageRecipientApprovedAll, true);+ } finally { await f.browser.close(); }+ });++ test(`${kind}: the scoped header button pattern also works on a generic chat site`, async () => {+ const f = await fixture(kind, engine, Agent, { url: 'https://example.test/chat/rooms/one' });+ try {+ const { blocked, context } = await f.bind();+ assert.equal(blocked, null, JSON.stringify(blocked));+ const result = await call(f.page, 'click', { ...sendArgs, ...context });+ assert.equal(result.success, true, JSON.stringify(result));+ assert.equal(await f.page.evaluate(() => window.sent), 1);+ } finally { await f.browser.close(); }+ });++ for (const drift of ['recipient', 'same-name phone', 'contact replacement', 'composer replacement', 'body']) {+ test(`${kind}: ${drift} change blocks the previously bound send`, async () => {+ const f = await fixture(kind, engine, Agent);+ try {+ const { blocked, context } = await f.bind();+ assert.equal(blocked, null, JSON.stringify(blocked));+ await f.page.evaluate(drift => {+ if (drift === 'recipient') document.querySelector('#contact span').textContent = 'Different recipient';+ if (drift === 'same-name phone') document.querySelector('#body').setAttribute('aria-label', 'Type a message to +90 555 000 00 02');+ if (drift === 'contact replacement') {+ const old = document.querySelector('#contact'); old.replaceWith(old.cloneNode(true));+ }+ if (drift === 'composer replacement') {+ const old = document.querySelector('#body'); old.replaceWith(old.cloneNode(true));+ }+ if (drift === 'body') document.querySelector('#body').textContent = 'Different message';+ }, drift);+ const result = await call(f.page, 'click', { ...sendArgs, ...context });+ assert.equal(result.success, false, JSON.stringify(result));+ assert.equal(result.messageRecipientGuard, true, JSON.stringify(result));+ assert.equal(result.dispatched, false);+ assert.equal(await f.page.evaluate(() => window.sent), 0);+ } finally { await f.browser.close(); }+ });+ }++ test(`${kind}: unrelated message updates preserve the current recipient proof`, async () => {+ const f = await fixture(kind, engine, Agent);+ try {+ const { blocked, context } = await f.bind();+ assert.equal(blocked, null, JSON.stringify(blocked));+ await f.page.evaluate(() => {+ document.querySelector('#messages').append('new unrelated incoming message');+ document.querySelector('aside button').textContent = 'Sidebar updated';+ });+ const result = await call(f.page, 'click', { ...sendArgs, ...context });+ assert.equal(result.success, true, JSON.stringify(result));+ assert.equal(await f.page.evaluate(() => window.sent), 1);+ } finally { await f.browser.close(); }+ });++ for (const missing of ['contact missing', 'contact hidden', 'ambiguous contacts', 'message impersonation', 'nested toolbar', 'foreign owner']) {+ test(`${kind}: ${missing} cannot authorize a send or stage another approval`, async () => {+ const f = await fixture(kind, engine, Agent);+ try {+ await f.page.evaluate(missing => {+ const contact = document.querySelector('#contact');+ if (missing === 'contact hidden') contact.hidden = true;+ if (missing === 'contact missing' || missing === 'message impersonation') contact.remove();+ if (missing === 'ambiguous contacts') {+ const second = contact.cloneNode(true); second.id = 'another-contact';+ second.querySelector('span').textContent = 'Other recipient'; contact.after(second);+ }+ if (missing === 'message impersonation') {+ document.querySelector('#main > header').remove();+ document.querySelector('#messages').innerHTML = '<header><div role="button">Anne</div></header>';+ }+ if (missing === 'nested toolbar') {+ const toolbar = document.createElement('div'); toolbar.setAttribute('role', 'toolbar');+ contact.replaceWith(toolbar); toolbar.append(contact);+ }+ if (missing === 'foreign owner') {+ const owner = document.createElement('section'); owner.id = 'alien-header';+ contact.closest('header').replaceWith(owner); owner.append(contact);+ }+ }, missing);+ const { blocked } = await f.bind();+ assert.equal(blocked?.success, false, JSON.stringify(blocked));+ assert.equal(blocked?.noDispatch, true);+ assert.equal(blocked?.reasonCode, 'recipient_identity_unavailable', JSON.stringify(blocked));+ assert.equal(blocked?.recipientAuthorizationRequired, false);+ assert.equal(f.agent._planExecutionGuards.get(71).pendingRecipientAuthorization, false);+ assert.doesNotMatch(blocked.error, /clarify\(|Ask the user to authorize/);+ const updates = [];+ const repeatedApproval = await f.agent._executeToolImpl(71, 'clarify', {+ purpose: 'message_recipient', question: 'Send to Anne instead?', options: ['Anne', 'Cancel'],+ }, status => updates.push(status));+ assert.equal(repeatedApproval.success, false);+ assert.deepEqual(repeatedApproval.recipientBinding, { required: true, bound: false,+ reasonCode: 'recipient_identity_unavailable' });+ assert.deepEqual(updates, [], 'technical failures must not present an unusable approval question');+ assert.equal(f.agent._pendingClarifications.has(71), false);+ if (missing === 'contact missing') {+ f.agent.clarifyTimeoutSec = 0;+ const unscopedRepeat = await f.agent._executeToolImpl(71, 'clarify', {+ question: 'Send to Anne instead?', options: ['Anne', 'Cancel'],+ }, status => updates.push(status));+ assert.equal(unscopedRepeat.success, false);+ assert.equal(unscopedRepeat.recipientBinding.bound, false);+ assert.deepEqual(updates, [], 'omitting purpose must not reopen the same recipient approval');+ f.agent.clarifyTimeoutSec = -1;+ f.agent._recordClarificationAuthorization = async () => true;+ const unrelated = await f.agent._executeToolImpl(71, 'clarify', {+ question: 'Which image size do you prefer?', options: ['Original', 'Small'],+ }, (status, details) => {+ if (status !== 'clarify') return;+ updates.push(status);+ f.agent._settleClarification(f.agent._pendingClarifications.get(71).get(details.clarifyId),+ { answer: 'Original', source: 'option' });+ });+ assert.equal(unrelated.success, true);+ assert.equal(unrelated.recipientBinding, undefined);+ assert.deepEqual(updates, ['clarify'], 'unrelated questions must retain their normal behavior');+ }+ assert.equal(f.agent._bindClarifiedMessageRecipient(71, 'Send all remaining messages without asking again', 'option', {+ purpose: 'message_recipient', question: 'Send to Anne?', options: ['Anne', 'Cancel'],+ }), false, 'model-authored recipient options cannot supply missing page identity');+ assert.equal(await f.page.evaluate(() => window.sent), 0);+ } finally { await f.browser.close(); }+ });+ }++ test(`${kind}: a fresh observed contact clears the technical blocker`, async () => {+ const f = await fixture(kind, engine, Agent);+ try {+ await f.page.evaluate(() => { document.querySelector('#contact').hidden = true; });+ assert.equal((await f.bind()).blocked?.reasonCode, 'recipient_identity_unavailable');+ await f.page.evaluate(() => { document.querySelector('#contact').hidden = false; });+ const { blocked, context } = await f.bind();+ assert.equal(blocked, null, JSON.stringify(blocked));+ assert.equal(f.agent._planExecutionGuards.get(71).messageRecipientTechnicalBlock, undefined);+ const result = await call(f.page, 'click', { ...sendArgs, ...context });+ assert.equal(result.success, true, JSON.stringify(result));+ assert.equal(await f.page.evaluate(() => window.sent), 1);+ } finally { await f.browser.close(); }+ });++ test(`${kind}: missing delivery evidence preserves consent and never asks to approve again`, async () => {+ const f = await fixture(kind, engine, Agent);+ try {+ const guard = f.agent._planExecutionGuards.get(71);+ guard.approvedRecipients = ['anne'];+ const realProbe = f.agent._messageRecipientContentProbe;+ for (const [field, value, reasonCode] of [+ ['messageBody', '', 'message_body_unverified'],+ ['messageBodyBaselineCount', null, 'message_body_baseline_unavailable'],+ ['messageBodyBaselineCount', '', 'message_body_baseline_unavailable'],+ ['messageRecipientDispatchBinding', undefined, 'recipient_dispatch_binding_unavailable'],+ ]) {+ f.agent._messageRecipientContentProbe = async (...args) => ({ ...await realProbe(...args), [field]: value });+ const { blocked } = await f.bind();+ assert.equal(blocked?.reasonCode, reasonCode, JSON.stringify(blocked));+ assert.equal(blocked.recipientAuthorizationRequired, false);+ assert.deepEqual(guard.approvedRecipients, ['anne']);+ assert.equal(guard.pendingRecipientAuthorization, false);+ const updates = [];+ const confirmation = await f.agent._executeToolImpl(71, 'clarify', {+ purpose: 'message_recipient', question: 'Send to Anne?', options: ['Anne', 'Cancel'],+ }, status => updates.push(status));+ assert.equal(confirmation.success, false);+ assert.equal(confirmation.recipientBinding.reasonCode, reasonCode);+ assert.deepEqual(updates, []);+ }+ f.agent._messageRecipientContentProbe = realProbe;+ assert.equal((await f.bind()).blocked, null);+ assert.equal(guard.messageRecipientTechnicalBlock, undefined);+ assert.deepEqual(guard.approvedRecipients, ['anne']);+ assert.equal(await f.page.evaluate(() => window.sent), 0);+ } finally { await f.browser.close(); }+ });++ test(`${kind}: protected screenshot attachment binds the WhatsApp contact`, async () => {+ const f = await fixture(kind, engine, Agent);+ try {+ const { blocked, context } = await f.bind('upload_file', { selector: '#file', downloadId: 2027 });+ assert.equal(blocked, null, JSON.stringify(blocked));+ const result = await call(f.page, 'attach_message_recipient_bound_upload', {+ selector: '#file', ...context, uploadSourceKey: context.messageRecipientUploadSourceKey,+ filename: 'fixture.png', mimeType: 'image/png', base64: Buffer.from('synthetic image bytes').toString('base64'),+ });+ assert.equal(result.success, true, JSON.stringify(result));+ assert.equal(result.verified, false);+ assert.equal(result.remoteStateVerified, false);+ assert.equal(await f.page.evaluate(() => document.querySelector('#file').files.length), 1);+ assert.equal(await f.page.evaluate(() => window.sent), 0);+ } finally { await f.browser.close(); }+ });+}diff --git a/test/page-monitor-passive-progress.mjs b/test/page-monitor-passive-progress.mjsindex af36fd024..f915ac9b4 100644--- a/test/page-monitor-passive-progress.mjs+++ b/test/page-monitor-passive-progress.mjs@@ -26,7 +26,7 @@ const modelHtml = html.replace('</aside>', '</aside><aside id="sidebar"><h2 id="sidebar-heading">Unrelated sidebar</h2></aside>');async function fixture(browser, build, body = html, { realAccessibilityTree = false, contentLibrary = false,- fixtureUrl = 'https://monitor.test/start' } = {}) {+ richTextLibrary = false, fixtureUrl = 'https://monitor.test/start' } = {}) {const context = await browser.newContext({ viewport: { width: 1000, height: 700 } });await context.route(`${new URL(fixtureUrl).origin}/**`, route => route.fulfill({ contentType: 'text/html', body }));await context.addInitScript(realAccessibilityTree => {@@ -49,6 +49,9 @@ async function fixture(browser, build, body = html, { realAccessibilityTree = fa}});}, realAccessibilityTree);+ if (richTextLibrary) await context.addInitScript({+ content: fs.readFileSync(new URL(`../src/${build}/src/content/rich-text-toolbar-heuristic.js`, import.meta.url), 'utf8'),+ });if (realAccessibilityTree) await context.addInitScript({content: fs.readFileSync(new URL(`../src/${build}/src/content/accessibility-tree.js`, import.meta.url), 'utf8'),});@@ -573,6 +576,172 @@ for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {});}+const conversationPaneHtml = `<!doctype html><style>+body { margin:0;font:16px sans-serif; } aside { position:absolute;left:0;top:0;width:300px;height:650px;overflow:auto; }+#main { position:absolute;left:320px;top:0;width:670px;height:650px; }+#main>header { height:70px;display:flex;align-items:center;gap:20px;padding-left:30px; }+#contact { width:180px;height:24px; } #messages { height:440px;overflow:auto; }+footer { display:flex;gap:15px;align-items:center; } #body { width:380px;min-height:32px; }+button { min-height:28px; } #footer-clock { position:absolute;left:550px;top:570px; }+</style><aside><header><h1>App title</h1></header><button>Anne</button><h2 id="sidebar-heading">Sidebar recipient</h2></aside>+<main id="main"><header><img alt="" width="40" height="40">+<div id="contact" role="button" tabindex="0"><span title="Anne">Anne</span></div>+<button aria-label="Voice call">Call</button><button aria-label="Search">Search</button></header>+<section id="messages"><span id="history-counter">100</span><div>Anne</div><button id="history-action">Other person</button></section>+<footer><button id="attach">Attach</button><div id="body" contenteditable="true" role="textbox" aria-label="Type a message to +90 555 000 00 01">Selam</div>+<input id="file" type="file" accept="image/*" hidden><button id="send" aria-label="Send">Send</button><span id="footer-clock">12:00</span></footer></main>`;++async function conversationPaneFixture(browser, build, body = conversationPaneHtml, fixtureUrl = 'https://web.whatsapp.com/') {+ const result = await fixture(browser, build, body, { realAccessibilityTree: true, contentLibrary: true, richTextLibrary: true, fixtureUrl });+ await result.page.evaluate(() => {+ window.submissions = 0; document.getElementById('send').addEventListener('click', () => submissions++);+ });+ return result;+}++async function conversationRecipientBinding(page) {+ const result = await modelMessage(page, 'probe_message_recipient_guard', {+ adapterName: 'generic-messaging', tool: 'click', args: { selector: '#send' }, bindDispatch: true, expectedRecipients: ['anne'],+ });+ assert.equal(result.conclusive, true);+ assert.equal(result.strongRecipientCandidates?.length, 1);+ assert.ok(result.messageRecipientDispatchBinding?.token);+ return { messageRecipientGuardRequired: true, messageRecipientDispatchBinding: result.messageRecipientDispatchBinding };+}++for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {+ test(`${build}: heading-free native conversation panes share exact recipient evidence with model snapshots`, { timeout: 60000 }, async t => {+ const browser = await engine.launch();+ try {+ for (const [name, body, fixtureUrl, mutate] of [+ ['history counter', conversationPaneHtml, 'https://web.whatsapp.com/', () => { document.getElementById('history-counter').textContent = '101'; }],+ ['history action and sidebar heading', conversationPaneHtml, 'https://example.test/chat/one', () => {+ document.getElementById('history-action').textContent = 'An unrelated history action';+ document.getElementById('sidebar-heading').textContent = 'Another sidebar heading';+ }],+ ['footer clock', conversationPaneHtml, 'https://web.whatsapp.com/', () => { document.getElementById('footer-clock').textContent = '12:01'; }],+ ['inner footer action toolbar', conversationPaneHtml.replace('<button id="send" aria-label="Send">Send</button>', '<div role="toolbar"><button id="send" aria-label="Send">Send</button></div>'), 'https://web.whatsapp.com/', () => { document.getElementById('history-counter').textContent = '101'; }],+ ]) await t.test(`${name} can update around a captured contact-button send`, async () => {+ const { context, page } = await conversationPaneFixture(browser, build, body, fixtureUrl);+ try {+ const snapshot = await captureTreeModel(page);+ assert.ok(snapshot.page.pageContent.includes('Anne'));+ assert.ok(snapshot.page.pageContent.includes('Type a message to +90 555 000 00 01'));+ for (const privateField of ['signature', 'aliases', 'contextNodes', 'labelledByNodes']) {+ assert.equal(JSON.stringify(snapshot).includes(`"${privateField}"`), false);+ }+ const target = { tool: 'click', selector: '#send' };+ assert.equal((await validateModel(page, snapshot, target)).ready, true);+ await page.evaluate(mutate);+ assert.equal((await validateModel(page, snapshot, target)).ready, true);+ assert.equal((await prepareModel(page, snapshot, target)).modelBindingValid, true);+ const recipient = name === 'inner footer action toolbar' ? null : await conversationRecipientBinding(page);+ await page.evaluate(() => { document.getElementById('history-counter').textContent = '102'; });+ // The monitor also covers an exact footer action in a small inner+ // toolbar. Preserve the recipient classifier's existing utility+ // outcome for that shape; exercise its certified dispatch directly.+ if (recipient) {+ const result = await modelMessage(page, 'click', { selector: '#send', ...recipient });+ assert.equal(result.success, true, JSON.stringify(result));+ } else assert.equal((await dispatchModel(page)).ready, true);+ assert.equal(await page.evaluate(() => submissions), 1);+ assert.equal((await page.evaluate(() => feedback)).some(event => event.source === 'user'), false);+ } finally { await context.close(); }+ });++ await t.test('priority capture exposes the complete composer destination when history truncates its AX node', async () => {+ const history = Array.from({ length: 250 }, (_, index) => `<button type="submit" style="position:absolute;left:20px;top:140px">Unrelated history action ${index} with enough rendered text to truncate this pane observation</button>`).join('');+ const body = conversationPaneHtml.replace('<section id="messages">', `<section id="messages">${history}`);+ const { context, page } = await conversationPaneFixture(browser, build, body);+ try {+ const target = { tool: 'click', selector: '#send' };+ const snapshot = await captureDeepPriority(page, target);+ assert.match(snapshot.page.pageContent, /visible identity "Type a message to \+90 555 000 00 01" \[ref_/);+ assert.ok(snapshot.page.pageContent.includes('Anne'));+ assert.equal(snapshot.page.pageContent.includes('Selam'), false, 'The supplemental label must not serialize the omitted editor contents');+ assert.equal((await validateModel(page, snapshot, target)).ready, true);+ await page.evaluate(() => { document.getElementById('history-counter').textContent = '101'; });+ assert.equal((await prepareModel(page, snapshot, target)).modelBindingValid, true);+ assert.equal((await dispatchModel(page)).ready, true);+ assert.equal(await page.evaluate(() => submissions), 1);++ const current = await captureDeepPriority(page, target);+ await page.evaluate(() => { document.getElementById('body').setAttribute('aria-label', 'Type a message to +90 555 000 00 02'); });+ assert.equal((await validateModel(page, current, target)).ready, false);+ assert.equal((await prepareModel(page, current, target)).modelBindingValid, false);+ assert.equal(await page.evaluate(() => submissions), 1);+ } finally { await context.close(); }+ });++ await t.test('a complete long composer label is exposed once per capture across multiple footer controls', async () => {+ const authoredLabel = `Type a message to +90 555 000 00 01 ${'complete destination details '.repeat(10).trim()}`;+ assert.ok(authoredLabel.length > 120 && authoredLabel.length <= 512);+ const controls = Array.from({ length: 8 }, (_, index) => `<button id="footer-action-${index}" style="position:absolute;left:400px;top:${120 + index * 35}px">Post action ${index}</button>`).join('');+ const body = conversationPaneHtml.replace('aria-label="Type a message to +90 555 000 00 01"', `aria-label="${authoredLabel}"`)+ .replace('<footer>', `<footer>${controls}`);+ const { context, page } = await conversationPaneFixture(browser, build, body);+ try {+ const target = { tool: 'click', selector: '#send' };+ for (const actionTarget of [undefined, target]) {+ const snapshot = await captureDeepPriority(page, actionTarget);+ const completeLines = snapshot.page.pageContent.split('\n').filter(line => line.startsWith(`visible identity ${JSON.stringify(authoredLabel)} `));+ assert.equal(completeLines.length, 1, 'Each new snapshot must publish one complete authored label despite repeated footer proofs');+ assert.equal((await validateModel(page, snapshot, target)).ready, true);+ for (let index = 0; index < 8; index++) {+ assert.equal((await validateModel(page, snapshot, { tool: 'click', selector: `#footer-action-${index}` })).ready, true);+ }+ await page.evaluate(() => { document.getElementById('history-counter').textContent += '1'; });+ assert.equal((await prepareModel(page, snapshot, target)).modelBindingValid, true);+ }+ assert.equal((await dispatchModel(page)).ready, true);+ assert.equal(await page.evaluate(() => submissions), 1);+ } finally { await context.close(); }+ });++ for (const [name, mutate] of [+ ['same-name destination phone', () => { document.getElementById('body').setAttribute('aria-label', 'Type a message to +90 555 000 00 02'); }],+ ['same-name contact replacement', () => { const contact = document.getElementById('contact'); contact.replaceWith(contact.cloneNode(true)); }],+ ['composer replacement', () => { const editor = document.getElementById('body'); editor.replaceWith(editor.cloneNode(true)); }],+ ['header scope', () => { document.getElementById('messages').appendChild(document.querySelector('#main>header')); }],+ ['new ambiguous header contact', () => { const contact = document.createElement('div'); contact.setAttribute('role', 'button'); contact.textContent = 'Bob'; document.querySelector('#main>header').appendChild(contact); }],+ ]) await t.test(`${name} changes reject the original contact-button proof before dispatch`, async () => {+ const { context, page } = await conversationPaneFixture(browser, build);+ try {+ const snapshot = await captureTreeModel(page);+ const target = { tool: 'click', selector: '#send' };+ assert.equal((await validateModel(page, snapshot, target)).ready, true);+ assert.equal((await prepareModel(page, snapshot, target)).modelBindingValid, true);+ await page.evaluate(mutate);+ assert.equal((await validateModel(page, snapshot, target)).ready, false);+ assert.equal((await modelMessage(page, 'page_monitor_dispatch', { operationId: 'model-prepared', kind: 'click', fenceOnly: true })).ready, false);+ assert.equal(await page.evaluate(() => submissions), 0);+ } finally { await context.close(); }+ });++ await t.test('the original public contact and private conversation proof are captured in the same task', async () => {+ const { context, page } = await conversationPaneFixture(browser, build);+ try {+ await page.evaluate(() => {+ const original = window.__generateAccessibilityTree; let queued = false;+ window.__generateAccessibilityTree = (...args) => {+ const result = original(...args);+ if (!queued) { queued = true; queueMicrotask(() => { const label = document.querySelector('#contact span'); label.textContent = 'Bob'; label.title = 'Bob'; }); }+ return result;+ };+ });+ const snapshot = await captureTreeModel(page);+ assert.ok(snapshot.page.pageContent.includes('Anne'));+ assert.equal(snapshot.page.pageContent.includes('Bob'), false);+ assert.equal(await page.evaluate(() => document.getElementById('contact').innerText), 'Bob');+ assert.equal((await validateModel(page, snapshot, { tool: 'click', selector: '#send' })).ready, false);+ assert.equal((await prepareModel(page, snapshot, { tool: 'click', selector: '#send' })).modelBindingValid, false);+ assert.equal(await page.evaluate(() => submissions), 0);+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });+}+const ownerIdentityHtml = modelHtml.replace('<form id="form"', `<div id="recipient-chip" data-recipient-id="Alice" style="position:absolute;left:350px;top:0;width:160px;height:25px">Alice</div><label id="recipient-control-label" for="recipient-control" style="position:absolute;left:350px;top:30px">Recipient</label><input id="recipient-control" value="Alice" style="position:absolute;left:350px;top:55px;width:140px">diff --git a/test/run.js b/test/run.jsindex f5824d474..835500891 100644--- a/test/run.js+++ b/test/run.js@@ -92230,6 +92230,103 @@ test('execution evidence and trusted continuation are scoped to the authorized t}});+async function recipientContinuationFixture(AgentClass, tabId, allowAll = false) {+ const agent = new AgentClass({});+ agent.conversationIds.set(tabId, `recipient_continuation_${tabId}`);+ agent.conversations.set(tabId, [+ { role: 'system', content: 'sys' },+ { role: 'user', content: 'Send the updates to the contacts I confirm.' },+ ]);+ const gate = { requestKind: 'execute', requiresStateChange: true, requiresSubmission: true,+ messaging: { target_kind: 'named', recipients: ['Original contact'] } };+ const guard = agent._startPlanExecutionGuard(tabId, 'act', gate);+ let identity = 'Anne';+ agent._messageRecipientContentProbe = async () => ({+ success: true, conclusive: true, messageSend: true, messageBody: 'Hello', messageBodyBaselineCount: 0,+ strongRecipientCandidates: [{ identity, role: 'to' }],+ messageRecipientDispatchBinding: { token: 'local-recipient-proof' },+ });+ const check = () => agent._messageRecipientGuardBlock(tabId, 'click', { selector: '#send' },+ 'https://example.test/chat/one', {});+ const grant = async (recipient, all = false) => {+ identity = recipient;+ assert.equal((await check())?.reasonCode, 'active_recipient_unverified');+ assert.equal(agent._bindClarifiedMessageRecipient(tabId,+ all ? 'Send all remaining messages in this task without asking again' : recipient,+ 'option', { purpose: 'recipient_change', question: `Send to ${recipient} instead?`,+ options: [recipient, 'Send all remaining messages in this task without asking again', 'Cancel'] }), true);+ };+ await grant('Anne', allowAll);+ return { agent, guard, check, grant, gate: { ...gate, messaging: structuredClone(guard.messaging) },+ observe: recipient => { identity = recipient; } };+}++test('trusted continuation retains authentic recipient grants without treating approval as execution evidence', async () => {+ for (const [index, AgentClass] of [AgentCh, AgentFx].entries()) {+ for (const allowAll of [false, true]) for (const hasProgress of [false, true]) {+ const tabId = 8680 + index;+ const f = await recipientContinuationFixture(AgentClass, tabId, allowAll);+ if (!allowAll) {+ await f.grant('Beatrice');+ f.gate.messaging = structuredClone(f.guard.messaging);+ }+ if (hasProgress) f.agent._markPlanExecutionToolCall(tabId, 'read_page', { success: true });+ f.guard.pendingRecipientAuthorization = 'recipient_change';+ f.guard.observedRecipientCandidates = [{ identity: 'Unapproved contact', role: 'to' }];+ f.guard.messageRecipientDispatchBinding = { token: 'must-not-carry' };+ f.agent._storeContinuationExecutionEvidence(tabId);+ const continued = f.agent._startPlanExecutionGuard(tabId, 'act', f.gate, { trustedContinuation: true });+ const label = `${AgentClass.name}: all=${allowAll}, prior progress=${hasProgress}`;+ assert.deepEqual(continued.approvedRecipients, allowAll ? ['anne'] : ['anne', 'beatrice'], label);+ assert.equal(continued.messageRecipientApprovedAll, allowAll, label);+ assert.equal(continued.successfulTaskToolCalls, hasProgress ? 1 : 0, label);+ assert.equal(continued.successfulConsequentialToolCalls, 0, label);+ assert.equal(continued.verifiedSubmissionEvidence, false, label);+ assert.equal(f.agent._executionEvidenceSatisfied(continued), false, `${label}: approval proved completion`);+ assert.equal(continued.pendingRecipientAuthorization, undefined, label);+ assert.equal(continued.observedRecipientCandidates, undefined, label);+ assert.equal(continued.messageRecipientDispatchBinding, undefined, label);+ // A named grant remains valid when returning to an earlier contact;+ // the all-remaining grant covers another freshly observed recipient.+ f.observe(allowAll ? 'Beatrice' : 'Anne');+ assert.equal(await f.check(), null, `${label}: Continue requested the same approval again`);+ // Approval still requires fresh physical recipient evidence.+ f.observe('');+ assert.equal((await f.check())?.reasonCode, 'recipient_identity_unavailable', label);+ }+ }+});++test('recipient continuation grants reject changed tasks, steering, gates, conversations and ordinary turns', async () => {+ for (const [index, AgentClass] of [AgentCh, AgentFx].entries()) {+ for (const change of ['ordinary turn', 'task', 'steering', 'conversation', 'recipient gate', 'submission gate', 'task drift']) {+ const tabId = 8682 + index;+ const f = await recipientContinuationFixture(AgentClass, tabId, true);+ if (change === 'task drift') f.guard.taskDrifted = true;+ f.agent._storeContinuationExecutionEvidence(tabId);+ let runOptions = { trustedContinuation: true };+ if (change === 'ordinary turn') runOptions = {};+ if (change === 'task') f.agent.conversations.get(tabId).push({ role: 'user', content: 'Send a different report to a new contact.' });+ if (change === 'steering') f.agent.conversations.get(tabId).push({ role: 'user', content: 'Only send to Carol now.',+ webbrainSteering: { requestId: 'local-steer', messageId: 'steer-turn', revision: 1,+ parentTaskKey: f.agent._progressTaskKeyHash(tabId) } });+ if (change === 'conversation') f.agent.conversationIds.set(tabId, 'different-conversation');+ if (change === 'recipient gate') f.gate.messaging = { target_kind: 'named', recipients: ['Carol'] };+ if (change === 'submission gate') f.gate.requiresSubmission = false;+ // Planner output cannot create recipient grants in a fresh guard.+ f.gate.approvedRecipients = ['anne'];+ f.gate.messageRecipientApprovedAll = true;+ const next = f.agent._startPlanExecutionGuard(tabId, 'act', f.gate, runOptions);+ const label = `${AgentClass.name}: ${change}`;+ assert.deepEqual(next.approvedRecipients, [], label);+ assert.equal(next.messageRecipientApprovedAll, false, label);+ assert.equal(next.successfulTaskToolCalls, 0, label);+ f.observe('Beatrice');+ assert.equal((await f.check())?.noDispatch, true, `${label}: a stale grant authorized another contact`);+ }+ }+});+test('empty-step planner JSON remains plan-only after successful task evidence', () => {for (const [index, AgentClass] of [AgentCh, AgentFx].entries()) {const agent = new AgentClass({});