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 social submission preflight and activation key matching
Emre Sokullu1337a48508f7
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex ebe5c7892..a853518f5 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -11544,14 +11544,14 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d|| fnName === 'press_keys'|| (fnName === 'click' && !!fnArgs?.selector)|| (Array.isArray(coordinateFrames) && coordinateFrames.length > 0);- const shouldDetectSubmit = ['click', 'click_ax', 'iframe_click', 'press_keys', 'execute_js'].includes(fnName);- const detected = shouldDetectSubmit- ? await this._detectLikelySubmitAction(tabId, fnName, fnArgs, {- ...(Array.isArray(coordinateFrames)- ? { coordinateFrames }- : {}),- })- : null;+ // The detector owns its supported-tool filtering. Every form+ // validation candidate, including set_field({submit:true}), needs+ // the same resolved-target evidence before workflow guards run.+ const detected = await this._detectLikelySubmitAction(tabId, fnName, fnArgs, {+ ...(Array.isArray(coordinateFrames)+ ? { coordinateFrames }+ : {}),+ });this._throwIfAborted(abortSignal);const currentValidationBlock = this._formValidationBlocks.get(tabId) || null;const obviousSubmit = this._formValidationActionLooksSubmit(fnName, fnArgs, null, detected);@@ -18015,7 +18015,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dasync _socialPublicationPreSubmitBlock(tabId, name, args, detected, provider) {const guard = this._planExecutionGuards.get(tabId);- const activationKey = name === 'press_keys' && (/enter|return|space/i.test(JSON.stringify(args?.key ?? args?.keys ?? '')) || args?.key === ' ');+ const rawKeys = args?.key ?? args?.keys ?? '';+ const activationKey = name === 'press_keys' && (Array.isArray(rawKeys) ? rawKeys : [rawKeys])+ .some(key => typeof key === 'string' && /^(?:enter|return|space|spacebar| )$/i.test(key));if (!guard?.enabled || (!this._isFormValidationCandidate(name, args) && !activationKey&& name !== 'execute_webmcp_tool' && !isNetworkMutation(name, args))) return null;const pageUrl = await this._currentUrl(tabId);diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex abcbad3be..3ec95e354 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -10093,10 +10093,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dtry {const preparation = await this._withContentActionDeadline(async abortSignal => {const allFrames = fnName === 'iframe_click' || fnName === 'press_keys';- const shouldDetectSubmit = ['click', 'click_ax', 'iframe_click', 'press_keys', 'execute_js'].includes(fnName);- const detected = shouldDetectSubmit- ? await this._detectLikelySubmitAction(tabId, fnName, fnArgs)- : null;+ // The detector owns its supported-tool filtering. Every form+ // validation candidate, including set_field({submit:true}), needs+ // the same resolved-target evidence before workflow guards run.+ const detected = await this._detectLikelySubmitAction(tabId, fnName, fnArgs);this._throwIfAborted(abortSignal);const currentValidationBlock = this._formValidationBlocks.get(tabId) || null;const obviousSubmit = this._formValidationActionLooksSubmit(fnName, fnArgs, null, detected);@@ -15814,7 +15814,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dasync _socialPublicationPreSubmitBlock(tabId, name, args, detected, provider) {const guard = this._planExecutionGuards.get(tabId);- const activationKey = name === 'press_keys' && (/enter|return|space/i.test(JSON.stringify(args?.key ?? args?.keys ?? '')) || args?.key === ' ');+ const rawKeys = args?.key ?? args?.keys ?? '';+ const activationKey = name === 'press_keys' && (Array.isArray(rawKeys) ? rawKeys : [rawKeys])+ .some(key => typeof key === 'string' && /^(?:enter|return|space|spacebar| )$/i.test(key));if (!guard?.enabled || (!this._isFormValidationCandidate(name, args) && !activationKey&& name !== 'execute_webmcp_tool' && !isNetworkMutation(name, args))) return null;const pageUrl = await this._currentUrl(tabId);diff --git a/test/social-publish-contract-dom.mjs b/test/social-publish-contract-dom.mjsindex 11747ee27..bfc7b82b0 100644--- a/test/social-publish-contract-dom.mjs+++ b/test/social-publish-contract-dom.mjs@@ -53,6 +53,35 @@ try {assert.equal(scopeCalls,0);checked++;}}+ // Exercise the actual batch caller with the actual injected detector.+ // Only the final tool execution is simulated; the caller must collect+ // composer ownership before either publication guard sees set_field.+ scopeAgent._skipPermissionGate=true;scopeAgent._ensureGateSetting=async()=>true;+ scopeAgent._recordProgressObservation=async()=>null;scopeAgent._autoRecordProgressAction=()=>null;+ scopeAgent._progressWarningForAction=()=>'';scopeAgent._captureFormValidationState=async()=>[];+ scopeAgent._waitForFormValidationFailure=async()=>null;+ for(const kind of ['search','composer','missing']){+ for(const submit of [true,false]){+ scopeAgent._clearLoopState(scopeTab); // Each fixture is a separate attempted action.+ await page.setContent(kind==='search'+ ? '<form><input id="field" type="search" name="q"><button>Search</button></form>'+ : kind==='composer'+ ? `<form><textarea id="field">Draft</textarea><button data-testid="${publishId}">Post</button></form>`+ : '<main>No resolved field</main>');+ await page.evaluate(()=>{window.__wb_ax_lookup=ref=>document.getElementById(ref);});+ let executions=0;+ scopeAgent.executeTool=async()=>{executions++;return {success:true,dispatched:true};};+ const messages=[];+ await scopeAgent._executeToolBatch(scopeTab,[{id:'set_field_case',function:{name:'set_field',arguments:JSON.stringify({ref_id:'field',text:'Hello',submit})}}],messages,()=>{},providerScope,'',new Set(['set_field']),1);+ const result=JSON.parse(scopeAgent._unwrapUntrusted(messages.find(m=>m.tool_call_id==='set_field_case').content));+ const reachesDispatch=!submit || kind==='search';+ assert.equal(executions,reachesDispatch?1:0,kind+' submit='+submit+' batch dispatch');+ assert.equal(result.success,reachesDispatch,kind+' submit='+submit+' batch result');+ if(!reachesDispatch) assert.equal(result.noDispatch,true);+ assert.equal(scopeCalls,0,'no publication model call for ordinary forms or bundled submission rejection');+ checked++;+ }+ }// A localized composer, an unnamed Post button, and an incomplete// composer all stay guarded. Implicit Enter must not become a bypass.for(const kind of ['localized','unnamed','incomplete']){diff --git a/test/social-publish-contract.mjs b/test/social-publish-contract.mjsindex 643003fe1..aed32f866 100644--- a/test/social-publish-contract.mjs+++ b/test/social-publish-contract.mjs@@ -384,6 +384,46 @@ for (const browser of ['chrome', 'firefox']) {}});+ test(`${browser}: publication activation requires a complete key name`,async()=>{+ const f=setup();+ for(const key of ['Backspace','Workspace','ReturnToSender','Entertain','Escape','Tab','ArrowLeft',';','']) {+ assert.equal(await f.agent._socialPublicationPreSubmitBlock(f.tabId,'press_keys',{key},null,f.provider),null,key);+ }+ for(const key of ['Enter','Return','Space','Spacebar',' ','space']) {+ assert((await f.agent._socialPublicationPreSubmitBlock(f.tabId,'press_keys',{key},null,f.provider)).noDispatch,key);+ }+ assert.equal(await f.agent._socialPublicationPreSubmitBlock(f.tabId,'press_keys',{keys:['Backspace','Tab']},null,f.provider),null);+ assert((await f.agent._socialPublicationPreSubmitBlock(f.tabId,'press_keys',{keys:['Tab','Spacebar']},null,f.provider)).noDispatch);+ assert.equal(f.calls.length,0);+ });++ test(`${browser}: tool batches probe set_field before ordinary-form and composer submission gates`,async()=>{+ for(const publicationControl of [false,true,undefined]) {+ const f=setup('Search for Hello');+ f.agent._skipPermissionGate=true;+ f.agent._ensureGateSetting=async()=>true;+ f.agent._recordProgressObservation=async()=>null;+ f.agent._autoRecordProgressAction=()=>null;+ f.agent._progressWarningForAction=()=>'';+ f.agent._captureFormValidationState=async()=>[];+ f.agent._waitForFormValidationFailure=async()=>null;+ const events=[];+ f.agent._detectLikelySubmitAction=async(_tab,name,args)=>{+ events.push({kind:'probe',name,submit:args.submit});+ return publicationControl===undefined?null:{isSubmit:true,publicationControl};+ };+ f.agent.executeTool=async(_tab,name)=>{events.push({kind:'dispatch',name});return {success:true,dispatched:true};};+ const messages=[];+ await f.agent._executeToolBatch(f.tabId,[{id:'field',function:{name:'set_field',arguments:JSON.stringify({ref_id:'ref_search',text:'Hello',submit:true})}}],messages,()=>{},f.provider,'',new Set(['set_field']),1);+ assert.deepEqual(events[0],{kind:'probe',name:'set_field',submit:true});+ const result=JSON.parse(f.agent._unwrapUntrusted(messages.find(m=>m.tool_call_id==='field').content));+ assert.equal(events.some(e=>e.kind==='dispatch'),publicationControl===false);+ assert.equal(result.success,publicationControl===false);+ if(publicationControl!==false) assert.equal(result.noDispatch,true);+ assert.equal(f.calls.length,0,'ordinary forms and rejected bundled submissions need no publication model call');+ }+ });+test(`${browser}: a thread needs every exact post and verified parent order`,async()=>{const action=rawAction('p1','twitter');action.posts.push(rawAction('unused','twitter','Second').posts[0]);const composer=snapshot();composer.posts.push(snapshot('Second').posts[0]);