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): shared editor identity, locale-independent structure
Emre Sokullu932b0afe78e6
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex f459d9626..e4d791962 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -1704,15 +1704,12 @@ export class Agent extends LoopDetector {// one identity).const activeTaskToken = this._taskTokens.get(tabId);const activeTaskTokenValid = typeof activeTaskToken === 'string' && activeTaskToken.length > 0;- // Editor identity must be GitHub-specific: the file editor exposes an- // accessible "Editing file contents" linkage (labelledby text, label, or- // wrapping label). A bare contentEditable flag is not enough β any other- // contenteditable on the edit route could otherwise mint a commit while- // the real editor still holds stale content. Records store the live- // digest metadata, so legit flows always carry the linkage.- const isGithubFileEditor = (record) => /\bediting\b[\s\S]*\bfile contents\b/i.test(String(- record?.fieldMeta?.ariaLabelledByText || record?.fieldMeta?.ariaLabel || record?.fieldMeta?.labelText || '',- ));+ // Editor identity must be GitHub-specific (see _isGithubFileEditorRecord,+ // shared with the refresh classifier so the two cannot drift): a bare+ // contentEditable flag is not enough β any other contenteditable on the+ // edit route could otherwise mint a commit while the real editor still+ // holds stale content. Records store the live digest metadata, so legit+ // flows always carry the linkage.const githubEditorReplacements = replacementRecordValues.filter(record => record?.ambiguous !== true&& activeTaskTokenValid@@ -1720,7 +1717,7 @@ export class Agent extends LoopDetector {&& !!record?.expectedSha256&& !!record?.readbackSha256&& this._normalizeUrl(record.pageUrl || '') === this._normalizeUrl(pageUrl)- && isGithubFileEditor(record));+ && this._isGithubFileEditorRecord(record));const githubEditorPayloads = new Set(githubEditorReplacements.map(record => `${record.expectedLength}:${record.expectedSha256}`));const verifiedReplacement = githubEditorPayloads.size === 1@@ -20373,6 +20370,20 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}}+ // GitHub file-editor identity shared by the commit-authorization filter+ // and the pre-submit refresh classifier, so the two can never drift apart+ // again: the editor exposes an accessible "Editing file contents" linkage+ // or locale-independent CodeMirror structure. A bare contentEditable flag+ // is not enough β any other contenteditable on the edit route could+ // otherwise mint or keep a commit for stale editor content.+ _isGithubFileEditorRecord(record) {+ if (!record || typeof record !== 'object') return false;+ if (/\bediting\b[\s\S]*\bfile contents\b/i.test(String(+ record?.fieldMeta?.ariaLabelledByText || record?.fieldMeta?.ariaLabel || record?.fieldMeta?.labelText || '',+ ))) return true;+ return record?.fieldMeta?.contentEditable === true && record?.fieldMeta?.codeMirror === true;+ }+_focusedGithubFieldKind(meta = null) {if (!meta || typeof meta !== 'object') return '';if (/^(?:commit-message-input|commit_message)$/i.test(String(meta.id || meta.name || ''))) {@@ -20508,9 +20519,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}continue;}- const githubEditor = record?.fieldMeta?.contentEditable === true- || /contenteditable/i.test(String(record?.key || key))- || /\bediting\b[\s\S]*\bfile contents\b/i.test(String(record?.fieldMeta?.ariaLabelledByText || ''));+ // Same identity as the authorization filter by construction β every+ // editor proof the gate can authorize is re-digested here.+ const githubEditor = this._isGithubFileEditorRecord(record);const commitMessage = /^(?:commit-message-input|commit_message)$/i.test(String(record?.fieldMeta?.id || record?.fieldMeta?.name || '',));diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.jsindex 675126134..e0a1560fa 100644--- a/src/chrome/src/content/content.js+++ b/src/chrome/src/content/content.js@@ -4023,6 +4023,14 @@tag,type: fieldType,contentEditable: !!el.isContentEditable,+ // Locale-independent file-editor structure: CodeMirror (GitHub's+ // file editor) marks its editable lineage with stable classes that+ // survive UI translation, unlike accessible labels.+ codeMirror: (() => {+ try {+ return !!el.isContentEditable && !!el.closest?.('.cm-content, .cm-editor, .CodeMirror');+ } catch { return false; }+ })(),name: el.getAttribute ? el.getAttribute('name') : null,id: elId,role: el.getAttribute ? el.getAttribute('role') : null,diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 7719d32fd..dc31ea183 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -1418,15 +1418,12 @@ export class Agent extends LoopDetector {// one identity).const activeTaskToken = this._taskTokens.get(tabId);const activeTaskTokenValid = typeof activeTaskToken === 'string' && activeTaskToken.length > 0;- // Editor identity must be GitHub-specific: the file editor exposes an- // accessible "Editing file contents" linkage (labelledby text, label, or- // wrapping label). A bare contentEditable flag is not enough β any other- // contenteditable on the edit route could otherwise mint a commit while- // the real editor still holds stale content. Records store the live- // digest metadata, so legit flows always carry the linkage.- const isGithubFileEditor = (record) => /\bediting\b[\s\S]*\bfile contents\b/i.test(String(- record?.fieldMeta?.ariaLabelledByText || record?.fieldMeta?.ariaLabel || record?.fieldMeta?.labelText || '',- ));+ // Editor identity must be GitHub-specific (see _isGithubFileEditorRecord,+ // shared with the refresh classifier so the two cannot drift): a bare+ // contentEditable flag is not enough β any other contenteditable on the+ // edit route could otherwise mint a commit while the real editor still+ // holds stale content. Records store the live digest metadata, so legit+ // flows always carry the linkage.const githubEditorReplacements = replacementRecordValues.filter(record => record?.ambiguous !== true&& activeTaskTokenValid@@ -1434,7 +1431,7 @@ export class Agent extends LoopDetector {&& !!record?.expectedSha256&& !!record?.readbackSha256&& this._normalizeUrl(record.pageUrl || '') === this._normalizeUrl(pageUrl)- && isGithubFileEditor(record));+ && this._isGithubFileEditorRecord(record));const githubEditorPayloads = new Set(githubEditorReplacements.map(record => `${record.expectedLength}:${record.expectedSha256}`));const verifiedReplacement = githubEditorPayloads.size === 1@@ -17944,6 +17941,20 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}}+ // GitHub file-editor identity shared by the commit-authorization filter+ // and the pre-submit refresh classifier, so the two can never drift apart+ // again: the editor exposes an accessible "Editing file contents" linkage+ // or locale-independent CodeMirror structure. A bare contentEditable flag+ // is not enough β any other contenteditable on the edit route could+ // otherwise mint or keep a commit for stale editor content.+ _isGithubFileEditorRecord(record) {+ if (!record || typeof record !== 'object') return false;+ if (/\bediting\b[\s\S]*\bfile contents\b/i.test(String(+ record?.fieldMeta?.ariaLabelledByText || record?.fieldMeta?.ariaLabel || record?.fieldMeta?.labelText || '',+ ))) return true;+ return record?.fieldMeta?.contentEditable === true && record?.fieldMeta?.codeMirror === true;+ }+_focusedGithubFieldKind(meta = null) {if (!meta || typeof meta !== 'object') return '';if (/^(?:commit-message-input|commit_message)$/i.test(String(meta.id || meta.name || ''))) {@@ -18079,9 +18090,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}continue;}- const githubEditor = record?.fieldMeta?.contentEditable === true- || /contenteditable/i.test(String(record?.key || key))- || /\bediting\b[\s\S]*\bfile contents\b/i.test(String(record?.fieldMeta?.ariaLabelledByText || ''));+ // Same identity as the authorization filter by construction β every+ // editor proof the gate can authorize is re-digested here.+ const githubEditor = this._isGithubFileEditorRecord(record);const commitMessage = /^(?:commit-message-input|commit_message)$/i.test(String(record?.fieldMeta?.id || record?.fieldMeta?.name || '',));diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.jsindex c6538fa93..5c73bb2c7 100644--- a/src/firefox/src/content/content.js+++ b/src/firefox/src/content/content.js@@ -3334,6 +3334,14 @@tag,type: fieldType,contentEditable: !!el.isContentEditable,+ // Locale-independent file-editor structure: CodeMirror (GitHub's+ // file editor) marks its editable lineage with stable classes that+ // survive UI translation, unlike accessible labels.+ codeMirror: (() => {+ try {+ return !!el.isContentEditable && !!el.closest?.('.cm-content, .cm-editor, .CodeMirror');+ } catch { return false; }+ })(),name: el.getAttribute ? el.getAttribute('name') : null,id: elId,role: el.getAttribute ? el.getAttribute('role') : null,diff --git a/test/run.js b/test/run.jsindex 7d8678285..f4c9004f5 100644--- a/test/run.js+++ b/test/run.js@@ -75605,6 +75605,132 @@ test('commit gate requires editor-specific identity', async () => {}});+test('commit gate accepts locale-independent editor structure', 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, /codeMirror/,+ `${rel}: field metadata lost its locale-independent editor flag`);+ assert.match(source, /cm-content/,+ `${rel}: editor structure probe misses the CodeMirror lineage`);+ }+ for (const [label, AgentClass, resolveJob, tabId] of [+ ['chrome', AgentCh, resolveAdapterWorkflowJob, 9504],+ ['firefox', AgentFx, resolveAdapterWorkflowJobFx, 9505],+ ]) {+ const agent = new AgentClass({});+ const pageUrl = 'https://github.com/Example/Repo/edit/main/docs/plan.md';+ agent._planExecutionGuards.set(tabId, {+ enabled: true,+ siteWorkflow: resolveJob(pageUrl, 'edit-file-and-commit'),+ workflowMetadataRequirements: [],+ workflowMetadataRequirementsResolved: true,+ });+ agent._taskTokens.set(tabId, 'task-locale');+ const body = '# Doc\n';+ const bodySha256 = await agent._sha256Text(body);+ const recordFor = (key, fieldMeta) => ({+ key,+ locatorType: 'ax',+ refId: 'ref_x',+ documentToken: 'doc',+ pageUrl,+ ambiguous: false,+ expectedLength: body.length,+ expectedSha256: bodySha256,+ expectedFp: agent._workflowInventoryFingerprint(body),+ fieldMeta,+ readbackLength: body.length,+ readbackSha256: bodySha256,+ verifiedAt: Date.now(),+ taskToken: 'task-locale',+ });+ // Localized UI: no English accessible label anywhere, but CodeMirror+ // editor structure on a contenteditable β must authorize.+ agent._verifiedTextReplacements.set(tabId, new Map([+ ['ax:doc:ref_editor', recordFor('ax:doc:ref_editor',+ { contentEditable: true, codeMirror: true, ariaLabelledByText: 'Dateiinhalt bearbeiten' })],+ ]));+ assert.ok(agent._workflowSubmitBindingForAttempt(tabId, pageUrl, {})?.githubFileCommit,+ `${label}: locale-independent editor structure did not authorize`);+ // Structure without editability (e.g. gutter chrome inside cm-editor)+ // must not.+ agent._verifiedTextReplacements.set(tabId, new Map([+ ['ax:doc:ref_other', recordFor('ax:doc:ref_other',+ { contentEditable: false, codeMirror: true })],+ ]));+ assert.equal(agent._workflowSubmitBindingForAttempt(tabId, pageUrl, {})?.githubFileCommit, undefined,+ `${label}: non-editable CodeMirror chrome authorized a commit`);+ }+});++test('refresh revalidates label-identified editor proofs', async () => {+ const originalChrome = globalThis.chrome;+ const originalBrowser = globalThis.browser;+ try {+ const pageUrl = 'https://github.com/Example/Repo/edit/main/docs/plan.md';+ const body = '# Labelled document\n\nOne complete copy.\n';+ for (const [label, AgentClass, tabId] of [+ ['chrome', AgentCh, 9506],+ ['firefox', AgentFx, 9507],+ ]) {+ const agent = new AgentClass({});+ // Textarea-backed editor identified through aria-label (not+ // labelledby, not contentEditable): the binding gate accepts it, so+ // refresh must re-digest it too instead of skipping it as stale-safe.+ const ariaMeta = { contentEditable: false, tag: 'textarea', id: 'file-editor', ariaLabel: 'Editing file contents' };+ const live = { text: body };+ const tabs = {+ async sendMessage(_tabId, message) {+ assert.equal(message.action, 'field_value_digest');+ assert.equal(message.params?.ref_id, 'ref_aria');+ return { success: true,+ valueLength: live.text.length,+ valueSha256: await agent._sha256Text(live.text),+ fieldMeta: ariaMeta };+ },+ };+ globalThis.chrome = { tabs };+ globalThis.browser = { tabs };+ const record = {+ key: 'ax:doc:ref_aria',+ locatorType: 'ax',+ refId: 'ref_aria',+ documentToken: 'doc',+ pageUrl,+ ambiguous: false,+ expectedLength: body.length,+ expectedSha256: await agent._sha256Text(body),+ expectedFp: agent._workflowInventoryFingerprint(body),+ fieldMeta: ariaMeta,+ readbackLength: body.length,+ readbackSha256: await agent._sha256Text(body),+ verifiedAt: Date.now(),+ taskToken: 'task-refresh-sync',+ };+ agent._taskTokens.set(tabId, 'task-refresh-sync');+ agent._verifiedTextReplacements.set(tabId, new Map([['ax:doc:ref_aria', record]]));+ // Live bytes match: kept.+ await agent._refreshGithubTextReplacementProofs(tabId, pageUrl);+ assert.ok(agent._verifiedTextReplacements.get(tabId)?.has('ax:doc:ref_aria'),+ `${label}: matching label-identified proof was dropped`);+ // Page changed after verification: must be re-digested and dropped,+ // not skipped as unauthorized-yet-authorized.+ live.text = `${body}\nExternal edit.\n`;+ await agent._refreshGithubTextReplacementProofs(tabId, pageUrl);+ assert.ok(!agent._verifiedTextReplacements.get(tabId)?.has('ax:doc:ref_aria'),+ `${label}: stale label-identified proof skipped refresh`);+ }+ } finally {+ if (originalChrome === undefined) delete globalThis.chrome;+ else globalThis.chrome = originalChrome;+ if (originalBrowser === undefined) delete globalThis.browser;+ else globalThis.browser = originalBrowser;+ }+});+test('selector writes to proven-distinct fields bypass unrelated debt', async () => {const originalChrome = globalThis.chrome;const originalBrowser = globalThis.browser;