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
Guard policy: a Firefox native field read back as another value ends the batch
Barack Sokullu5dfef895b74f
diff --git a/src/firefox/src/bidi/client.js b/src/firefox/src/bidi/client.jsindex 087cdd4a2..a39066f9e 100644--- a/src/firefox/src/bidi/client.js+++ b/src/firefox/src/bidi/client.js@@ -210,12 +210,22 @@ export class FirefoxBidiClient {deadlineAt: message.actionDeadlineAt || 0,}, { onDispatch });// The companion answers verified:false only from its settled readback- // after trusted typing went out. Its rich-text reader falls back to- // innerText, so the agent's policy decides whether that is a failure- // (Strict checks) or a noted, unverified success.+ // after trusted typing went out. A native <input> or <textarea> it+ // reads back by its exact value, so there a mismatch is known: the+ // field holds another value, and a batched submit must not run on it+ // (browser-batch.js textWriteRefuted). A contenteditable editor it+ // reads through the editor's model or, failing that, its rendered text+ // (innerText), so there the agent's policy decides whether that is a+ // failure (Strict checks) or a noted, unverified success.+ const meta = prepared.fieldMeta || null;+ const exactValue = !!meta && meta.contentEditable !== true+ && ['input', 'textarea'].includes(String(meta.tag || '').toLowerCase());const unverified = (action === 'type' || action === 'field') && performed?.success === false&& performed.dispatched === true && performed.verified === false && !performed.unverified- ? { unverified: { reason: 'readback_mismatch', reader: 'unknown' } } : {};+ ? (exactValue+ ? { unverified: { reason: 'readback_mismatch', reader: 'exact' }, readbackExact: true }+ : { unverified: { reason: 'readback_mismatch', reader: 'unknown' } })+ : {};return { ...metadata, ...performed, ...unverified, ...(prepared.rect ? { rect: prepared.rect } : {}), ...(prepared._filePickerGuardId ? { _filePickerGuardId: prepared._filePickerGuardId } : {}) };} catch (error) {if (error?.name === 'AbortError' && isBidiRequestNotSentError(error)) throw error;diff --git a/test/guard-advisory-writes.mjs b/test/guard-advisory-writes.mjsindex bb25671a4..5300926db 100644--- a/test/guard-advisory-writes.mjs+++ b/test/guard-advisory-writes.mjs@@ -659,6 +659,7 @@ test('firefox: a trusted BiDi readback mismatch is an unverified write, not debtretryable: false, error: 'Trusted typing did not match the requested value; inspect before another write.' });const mismatch = await client.sendContent(tab, { target: 'content', action: 'set_field', params: { ...args } });assert.deepEqual(mismatch.unverified, { reason: 'readback_mismatch', reader: 'unknown' });+ assert.equal(mismatch.readbackExact, undefined, 'a contenteditable readback may be the rendered text');client.perform = async () => { throw Object.assign(new Error('companion disconnected'), {}); };const lost = await client.sendContent(tab, { target: 'content', action: 'set_field', params: { ...args } });assert.equal(lost.unverified, undefined, 'a transport failure is an unknown outcome, not an unverified write');@@ -674,6 +675,34 @@ test('firefox: a trusted BiDi readback mismatch is an unverified write, not debtassert.equal(agent._uncertainTextMutations.get(tabId)?.size, 1);});+test('firefox: a trusted BiDi mismatch in a native input or textarea is known and ends the batch', async () => {+ // The companion reads a native field back by its exact value: a mismatch+ // there means the field holds another value, so a batched submit must not+ // run on it.+ const { Agent, batch } = builds.find(item => item.build === 'firefox');+ const tab = 78;+ for (const fieldMeta of [{ tag: 'input', type: 'text', contentEditable: false, name: 'title' },+ { tag: 'textarea', type: 'textarea', contentEditable: false, name: 'body' }]) {+ const client = new FirefoxBidiClient({ tabs: { sendMessage: async () => ({ bidiPrepared: true, fieldMeta, url: scope.pageUrl }) } });+ client.runs.set(tab, { runId: 'run-1', bound: true });+ client.bindRun = async () => {};+ client.perform = async () => ({ success: false, dispatched: true, verified: false, submitted: false, outcomeUnknown: true,+ retryable: false, error: 'Trusted typing did not match the requested value; inspect before another write.' });+ const mismatch = await client.sendContent(tab, { target: 'content', action: 'set_field', params: { ...args } });+ assert.deepEqual(mismatch.unverified, { reason: 'readback_mismatch', reader: 'exact' }, fieldMeta.tag);+ assert.equal(mismatch.readbackExact, true, fieldMeta.tag);+ const agent = setup(Agent);+ const advisory = await agent._finalizeTextMutationResult(tabId, 'set_field', args, mismatch);+ assert.equal(advisory.success, true, fieldMeta.tag);+ assert.equal(advisory.unverifiedWrite, true, fieldMeta.tag);+ assert.match(advisoryOf(advisory, 'text-write-readback-unverified')?.note || '', /reads back a different value/);+ assert.equal(batch.textWriteRefuted(advisory), true, `${fieldMeta.tag}: the value is known to be wrong`);+ const status = batch.browserBatchResultStatus({ role: 'tool', content: JSON.stringify(advisory) });+ assert.equal(status.failed, true, `${fieldMeta.tag}: the next batched action must not run`);+ assert.equal(status.reason, 'action_unverified');+ }+});+// The real content-script readback handlers, run against a minimal DOM.function functionSource(source, name) {let start = source.indexOf(`function ${name}(`);