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
Retire stale CAPTCHA answers and correct Enterprise rqdata guidance
Emre Sokulluda63507eeff7
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 96fcdbfab..385c75f78 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -10845,6 +10845,15 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dreturn record?.solution !== undefined && record.applied !== true;}+ _retireNativeCaptchaAnswerIfPageChanged(tabId, pageUrl) {+ const record = this._nativeCaptchaSolutions?.get(tabId);+ if (record?.pageUrl && pageUrl && record.pageUrl !== pageUrl) {+ // Keep dispatchedTimeOrigins to prevent a second paid solve if the old+ // document is restored, but never apply its answer to the new page.+ delete record.solution;+ }+ }+_captchaRoutingMessage(tabId, captchaGateDecision, captchaSolveOutcome, toolResult = {}, onUpdate = () => {}) {let resultContent = '';const gate = captchaGateDecision || captchaSolveOutcome;@@ -10985,11 +10994,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d_clearCaptchaGateAfterNavigation(tabId, toolName, beforeUrl, afterUrl, toolResult) {if (!Agent.NAV_TOOLS.has(toolName)) return null;- const gate = this._captchaGateStates.get(tabId);- if (!gate) return null;const beforeDocument = this._normalizeUrlPath(beforeUrl);const afterDocument = this._normalizeUrlPath(afterUrl);if (!beforeDocument || !afterDocument || beforeDocument === afterDocument) return null;+ this._retireNativeCaptchaAnswerIfPageChanged(tabId, afterUrl);+ const gate = this._captchaGateStates.get(tabId);+ if (!gate) return null;const clearedGate = {...gate.publicGate,status: 'cleared',@@ -11222,6 +11232,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (!pageUrl) {try { pageUrl = await this._currentUrl(tabId); } catch {}}+ if (pageUrl && this._nativeCaptchaSolutions?.get(tabId)?.pageUrl !== pageUrl) {+ try { this._retireNativeCaptchaAnswerIfPageChanged(tabId, await this._currentUrl(tabId)); } catch {}+ }let detection = null;let detectionFailed = false;let failedDiagnostics = null;diff --git a/src/chrome/src/agent/tools.js b/src/chrome/src/agent/tools.jsindex 27677f479..bd3ad8106 100644--- a/src/chrome/src/agent/tools.js+++ b/src/chrome/src/agent/tools.js@@ -1372,7 +1372,7 @@ export const AGENT_TOOLS = [},additionalProperties: false,},- rqdata: { type: 'string', description: 'hCaptcha only β exact rqdata observed in the widget configuration; never invent it. Required when isEnterprise is true.' },+ rqdata: { type: 'string', description: 'hCaptcha only β optional; preserve the exact rqdata when the widget exposes it. Never invent it.' },imageBase64: { type: 'string', description: 'image_to_text only β base64-encoded image bytes (no data: prefix).' },inject: { type: 'boolean', description: 'After solving, inject the token into the detected frame\'s response field (textarea[name=g-recaptcha-response] etc.) and fire the widget\'s callback. Default true and requires a detected frame target. If the widget cannot be detected but type/websiteKey are known, set false to get only the token without page injection.' },},diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex f6f5ebdd5..5d69707f6 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -9459,6 +9459,15 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dreturn record?.solution !== undefined && record.applied !== true;}+ _retireNativeCaptchaAnswerIfPageChanged(tabId, pageUrl) {+ const record = this._nativeCaptchaSolutions?.get(tabId);+ if (record?.pageUrl && pageUrl && record.pageUrl !== pageUrl) {+ // Keep dispatchedTimeOrigins to prevent a second paid solve if the old+ // document is restored, but never apply its answer to the new page.+ delete record.solution;+ }+ }+_captchaRoutingMessage(tabId, captchaGateDecision, captchaSolveOutcome, toolResult = {}, onUpdate = () => {}) {let resultContent = '';const gate = captchaGateDecision || captchaSolveOutcome;@@ -9599,11 +9608,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d_clearCaptchaGateAfterNavigation(tabId, toolName, beforeUrl, afterUrl, toolResult) {if (!Agent.NAV_TOOLS.has(toolName)) return null;- const gate = this._captchaGateStates.get(tabId);- if (!gate) return null;const beforeDocument = this._normalizeUrlPath(beforeUrl);const afterDocument = this._normalizeUrlPath(afterUrl);if (!beforeDocument || !afterDocument || beforeDocument === afterDocument) return null;+ this._retireNativeCaptchaAnswerIfPageChanged(tabId, afterUrl);+ const gate = this._captchaGateStates.get(tabId);+ if (!gate) return null;const clearedGate = {...gate.publicGate,status: 'cleared',@@ -9827,6 +9837,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (!pageUrl) {try { pageUrl = await this._currentUrl(tabId); } catch {}}+ if (pageUrl && this._nativeCaptchaSolutions?.get(tabId)?.pageUrl !== pageUrl) {+ try { this._retireNativeCaptchaAnswerIfPageChanged(tabId, await this._currentUrl(tabId)); } catch {}+ }let detection = null;let detectionFailed = false;let failedDiagnostics = null;diff --git a/src/firefox/src/agent/tools.js b/src/firefox/src/agent/tools.jsindex cf184ef75..7993b8e9c 100644--- a/src/firefox/src/agent/tools.js+++ b/src/firefox/src/agent/tools.js@@ -1226,7 +1226,7 @@ export const AGENT_TOOLS = [},additionalProperties: false,},- rqdata: { type: 'string', description: 'hCaptcha only β exact rqdata observed in the widget configuration; never invent it. Required when isEnterprise is true.' },+ rqdata: { type: 'string', description: 'hCaptcha only β optional; preserve the exact rqdata when the widget exposes it. Never invent it.' },imageBase64: { type: 'string', description: 'image_to_text only β base64-encoded image bytes (no data: prefix).' },inject: { type: 'boolean', description: 'After solving, inject the token into the detected frame\'s response field (textarea[name=g-recaptcha-response] etc.) and fire the widget\'s callback. Default true and requires a detected frame target. If the widget cannot be detected but type/websiteKey are known, set false to get only the token without page injection.' },},diff --git a/test/captcha-runtime-policy.mjs b/test/captcha-runtime-policy.mjsindex 865b27001..7b50d6e8b 100644--- a/test/captcha-runtime-policy.mjs+++ b/test/captcha-runtime-policy.mjs@@ -14,6 +14,11 @@ test('CAPTCHA tool matrix matches Ask, Compact, Mid, Full and Dev availability',for (const build of ['chrome', 'firefox']) {const { Agent } = await import(`../src/${build}/src/agent/agent.js`);const { getToolsForMode } = await import(`../src/${build}/src/agent/tools.js`);+ test(`${build}: hCaptcha Enterprise rqdata is optional in the model-visible tool schema`, () => {+ const solve = getToolsForMode('act', { tier: 'full' }).find(tool => tool.function.name === 'solve_captcha');+ assert.match(solve.function.parameters.properties.rqdata.description, /optional.*when the widget exposes it/i);+ assert.doesNotMatch(solve.function.parameters.properties.rqdata.description, /required/i);+ });function agentFor(mode, tier, enabled = true) {const agent = new Agent({ getActive: () => ({ promptTier: tier }) });agent.conversationModes.set(1, mode);@@ -98,4 +103,29 @@ for (const build of ['chrome', 'firefox']) {}}});+ test(`${build}: navigation retires an unapplied answer but retains its paid-dispatch history`, async () => {+ const agent = agentFor('act', 'full');+ const from = 'https://example.test/first';+ const to = 'https://example.test/second';+ const record = { pageUrl: from, solution: false, applied: false, dispatchedTimeOrigins: new Set([1000]) };+ agent._nativeCaptchaSolutions = new Map([[1, record]]);+ agent._captchaGateStates.set(1, { status: 'verification_pending', publicGate: { status: 'verification_pending' } });+ assert.equal(agent._hasUnappliedNativeCaptchaSolution(1), true);+ agent._clearCaptchaGateAfterNavigation(1, 'navigate', from, to, {});+ assert.equal(agent._hasUnappliedNativeCaptchaSolution(1), false);+ assert.equal(record.dispatchedTimeOrigins.has(1000), true);+ assert.equal(agent._captchaGateStates.has(1), false);+ agent._captchaGateStates.set(1, { status: 'solve_required', publicGate: { status: 'solve_required' } });+ assert.equal(agent._captchaGateBlockResult(1, 'apply_captcha_solution', {}).denied, true);+ assert.equal(agent._captchaGateBlockResult(1, 'solve_captcha', {}), null);++ record.solution = 0;+ agent._captchaGateStates.delete(1);+ agent._currentUrl = async () => to;+ agent._activeCloudflareManagedChallengeGate = () => null;+ agent._checkVerificationChallengeLoop = () => ({ kind: 'none' });+ await agent._observeCaptchaChallenge(1, 'get_accessibility_tree', { pageUrl: to, pageContent: 'heading "Next page"' });+ assert.equal(agent._hasUnappliedNativeCaptchaSolution(1), false);+ assert.equal(record.dispatchedTimeOrigins.has(1000), true);+ });}