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(agent): proven no-ops for empty appends
Emre Sokulluf335d77bab87
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 75085937b..fede759a7 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -21272,6 +21272,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (!['set_field', 'type_ax', 'type_text'].includes(name) || !result || typeof result !== 'object') {return result;}+ // Proven no-ops (e.g. empty appends) change nothing: they create no debt+ // and invalidate no proofs. Only a fully no-dispatch success qualifies β+ // anything that may have dispatched takes the normal paths below.+ if (result.noop === true && result.success === true && result.noDispatch === true) {+ return result;+ }let target = this._textMutationTarget(tabId, name, args);const replacesValue = this._textMutationReplacesValue(name, args);const text = typeof args.text === 'string' ? args.text : '';@@ -32521,6 +32527,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}const typeFieldEpoch = this._captureLastTypeFieldEpoch(tabId);+ // Empty appends mutate nothing: report a proven no-op without+ // touching the field, so no uncertainty debt is recorded.+ // (clear:true still empties below and takes the verified path.)+ if ((args.text ?? '') === '' && !args.clear) {+ return {+ success: true,+ dispatched: false,+ noDispatch: true,+ noop: true,+ method: 'cdp-insert-focused',+ text: '',+ };+ }if (args.clear) {const selectAllModifiers = await cdpClient.selectAllModifier(tabId);throwIfEarlyCdpAborted();@@ -32805,6 +32824,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}const typeFieldEpoch = this._captureLastTypeFieldEpoch(tabId);+ // Empty appends mutate nothing: report a proven no-op without+ // touching the field, so no uncertainty debt is recorded.+ // (clear:true still empties below and takes the verified path.)+ if ((args.text ?? '') === '' && !args.clear) {+ return {+ success: true,+ dispatched: false,+ noDispatch: true,+ noop: true,+ method: 'cdp-insert-focused',+ text: '',+ };+ }const beforeSignature = await cdpClient.textEntrySignature(tabId, { focused: true });throwIfEarlyCdpAborted();if (args.clear) {diff --git a/src/chrome/src/cdp/cdp-client.js b/src/chrome/src/cdp/cdp-client.jsindex 390182c51..945e724db 100644--- a/src/chrome/src/cdp/cdp-client.js+++ b/src/chrome/src/cdp/cdp-client.js@@ -4553,6 +4553,21 @@ export class CDPClient {throwIfAborted();};throwIfAborted();+ // Empty appends mutate nothing: inserting zero characters cannot change+ // any value, so skip resolution and dispatch entirely and report a+ // proven no-op. (A clear:true call still empties the field and takes the+ // normal verified path below.) Without this, the append proof β which+ // rejects an empty expected string β reports uncertainty debt that+ // blocks later legitimate typing.+ if (String(text ?? '') === '' && clear !== true) {+ return {+ success: true,+ dispatched: false,+ noDispatch: true,+ noop: true,+ method: 'cdp-insert-text',+ };+ }const expectedNodeId = Number(expectedBackendNodeId);if (Number.isInteger(expectedNodeId) && expectedNodeId > 0) {const currentInfo = await this.resolveSelector(tabId, selector, resolveOptions);diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.jsindex 6e505629e..1d7a31210 100644--- a/src/chrome/src/content/content.js+++ b/src/chrome/src/content/content.js@@ -1881,6 +1881,13 @@return noDispatchFailure(`Cannot type into <${tag}> β it is not an editable field. If you wanted to activate it, use click instead. If the real target is a nearby input, click the input first, then call type_text({text: "..."}) with no selector.`);}+ // Empty appends mutate nothing: skip focus/dispatch entirely and report+ // a proven no-op so no uncertainty debt is recorded downstream.+ // (clear:true still empties below and takes the verified path.)+ if (!typedText && !params.clear) {+ return { success: true, dispatched: false, noDispatch: true, noop: true, method: 'noop-empty-append' };+ }+if (actionDeadlineExpired()) return deadlineFailure();el.focus();if (actionDeadlineExpired()) return deadlineFailure();diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 80b40a8b4..237333a02 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -18840,6 +18840,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (!['set_field', 'type_ax', 'type_text'].includes(name) || !result || typeof result !== 'object') {return result;}+ // Proven no-ops (e.g. empty appends) change nothing: they create no debt+ // and invalidate no proofs. Only a fully no-dispatch success qualifies β+ // anything that may have dispatched takes the normal paths below.+ if (result.noop === true && result.success === true && result.noDispatch === true) {+ return result;+ }let target = this._textMutationTarget(tabId, name, args);const replacesValue = this._textMutationReplacesValue(name, args);const text = typeof args.text === 'string' ? args.text : '';diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.jsindex ba4e2b5ef..1e8807d20 100644--- a/src/firefox/src/content/content.js+++ b/src/firefox/src/content/content.js@@ -2197,6 +2197,13 @@return noDispatchFailure(`Cannot type into <${tag}> β it is not an editable field. If you wanted to activate it, use click instead. If the real target is a nearby input, click the input first, then call type_text({text: "..."}) with no selector.`);}+ // Empty appends mutate nothing: skip focus/dispatch entirely and report+ // a proven no-op so no uncertainty debt is recorded downstream.+ // (clear:true still empties below and takes the verified path.)+ if (!typedText && !params.clear) {+ return { success: true, dispatched: false, noDispatch: true, noop: true, method: 'noop-empty-append' };+ }+if (actionDeadlineExpired()) return deadlineFailure();el.focus();if (actionDeadlineExpired()) return deadlineFailure();diff --git a/test/run.js b/test/run.jsindex 410b26c7a..b0ec7bd12 100644--- a/test/run.js+++ b/test/run.js@@ -77523,6 +77523,80 @@ test('focused field identity matching is strict', () => {}});+test('empty selector appends report a proven no-op without dispatch', async () => {+ for (const rel of [+ 'src/chrome/src/content/content.js',+ 'src/firefox/src/content/content.js',+ ]) {+ const source = fs.readFileSync(path.join(ROOT, rel), 'utf8');+ assert.match(source, /!typedText && !params\.clear/,+ `${rel}: empty appends still dispatch instead of short-circuiting`);+ assert.match(source, /noop: true/,+ `${rel}: empty appends do not report a proven no-op`);+ }+ const cdpSource = fs.readFileSync(path.join(ROOT, 'src/chrome/src/cdp/cdp-client.js'), 'utf8');+ assert.match(cdpSource, /String\(text \?\? ''\) === '' && clear !== true/,+ 'chrome: CDP typeText still dispatches empty appends');+ const originalSendCommand = cdpClientCh.sendCommand;+ const originalAttach = cdpClientCh.attach;+ const originalEvaluate = cdpClientCh.evaluate;+ try {+ let calls = 0;+ cdpClientCh.sendCommand = async () => { calls += 1; return {}; };+ cdpClientCh.attach = async () => { calls += 1; return { attached: true }; };+ cdpClientCh.evaluate = async () => { calls += 1; return { result: { value: null } }; };+ const noop = await cdpClientCh.typeText(9991, '#field', '', false);+ assert.equal(noop?.success, true, 'empty append was not successful');+ assert.equal(noop?.noDispatch, true, 'empty append was dispatched');+ assert.equal(noop?.noop, true, 'empty append was not marked as a no-op');+ assert.equal(noop?.dispatched ?? false, false, 'empty append claimed a dispatch');+ assert.equal(calls, 0, 'empty append touched the protocol');+ // clear:true with empty text still empties the field: not a no-op.+ const cleared = await cdpClientCh.typeText(9991, '#field', '', true);+ assert.equal(cleared?.noop ?? false, false, 'clear-only call was misreported as a no-op');+ } finally {+ cdpClientCh.sendCommand = originalSendCommand;+ cdpClientCh.attach = originalAttach;+ cdpClientCh.evaluate = originalEvaluate;+ }+});++test('proven no-ops create no debt and keep proofs', async () => {+ for (const [label, AgentClass, tabId] of [+ ['chrome', AgentCh, 9528],+ ['firefox', AgentFx, 9529],+ ]) {+ const agent = new AgentClass({});+ agent._lastAxScopes.set(tabId, { documentToken: 'doc-noop', pageUrl: 'https://example.test/form' });+ const proof = {+ key: 'selector:doc-noop:#ed',+ locatorType: 'selector',+ selector: '#ed',+ documentToken: 'doc-noop',+ pageUrl: 'https://example.test/form',+ ambiguous: false,+ expectedLength: 4,+ expectedSha256: await agent._sha256Text('body'),+ fieldMeta: { contentEditable: true },+ readbackLength: 4,+ readbackSha256: await agent._sha256Text('body'),+ verifiedAt: Date.now(),+ taskToken: 'task-noop',+ };+ agent._verifiedTextReplacements.set(tabId, new Map([[proof.key, { ...proof }]]));+ const input = { success: true, dispatched: false, noDispatch: true, noop: true };+ const output = await agent._finalizeTextMutationResult(+ tabId, 'type_text', { selector: '#ed', text: '' }, input,+ );+ assert.deepEqual(output, input, `${label}: proven no-op was rewritten`);+ assert.equal(output.mutationMayHaveOccurred, undefined, `${label}: no-op recorded mutation doubt`);+ assert.equal(output.repeatBlocked, undefined, `${label}: no-op blocked repeats`);+ assert.equal(agent._uncertainTextMutations.has(tabId), false, `${label}: no-op recorded debt`);+ assert.ok(agent._verifiedTextReplacements.get(tabId)?.has(proof.key),+ `${label}: no-op invalidated an unrelated proof`);+ }+});+test('first live token bootstraps tokenless debt by URL', async () => {const originalChrome = globalThis.chrome;const originalBrowser = globalThis.browser;