Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

fix(agent): revalidate focused proofs via stable selectors pre-submit

Emre Sokulluf1251037b0f3
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index a98940157..29e6ce236 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -20319,6 +20319,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
ambiguous: false,
focusedProof: true,
focusedKind: kind,
+ // Stable pre-submit revalidation locator (focus moves before submit,
+ // so {focused:true} would digest the wrong field). The edit-file page
+ // owns a single file editor; the commit-message input has a stable id.
+ refreshSelector: kind === 'editor' ? '[contenteditable="true"]' : '#commit-message-input',
};
}
@@ -20356,14 +20360,40 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
for (const [key, record] of records) {
if (this._normalizeUrl(record?.pageUrl || '') !== this._normalizeUrl(pageUrl)) continue;
// Focused proofs are bound at write time to the then-focused field's
- // live digest (focus stays on the edited field through verification).
- // Re-digesting via {focused:true} after focus moved (e.g. editor proof
- // while the commit-message input is focused) would compare the wrong
- // field and delete a good proof, permanently blocking the documented
- // click-then-type_text flow. External mutations after the proof are
- // still caught fail-closed by the byte-exact raw-blob check post-commit,
- // and any later write to the same focused kind overwrites this record.
- if (record?.focusedProof === true) continue;
+ // live digest, but focus moves before submit (editor β†’ commit dialog β†’
+ // commit-message input), so re-digesting via {focused:true} would
+ // compare the wrong field. Revalidate via the stable refreshSelector
+ // captured at mint instead; a mismatch or missing element deletes the
+ // proof fail-closed. The byte-exact raw-blob check post-commit stays as
+ // backstop for the file; the commit message has no post-commit check,
+ // so its stable-id revalidation is the gate.
+ if (record?.focusedProof === true) {
+ const refreshSelector = typeof record.refreshSelector === 'string' && record.refreshSelector.trim()
+ ? record.refreshSelector.trim()
+ : (record.focusedKind === 'editor' ? '[contenteditable="true"]' : '#commit-message-input');
+ let current = null;
+ try {
+ current = await this._textMutationValueDigest(tabId, {
+ locatorType: 'selector', selector: refreshSelector, ambiguous: false,
+ });
+ } catch { current = null; }
+ const kindOk = record.focusedKind === 'editor'
+ ? (current?.fieldMeta?.contentEditable === true
+ || /\bediting\b[\s\S]*\bfile contents\b/i.test(String(
+ current?.fieldMeta?.ariaLabelledByText || current?.fieldMeta?.ariaLabel || '',
+ )))
+ : /^(?:commit-message-input|commit_message)$/i.test(String(
+ current?.fieldMeta?.id || current?.fieldMeta?.name || '',
+ ));
+ if (!current
+ || !record.readbackSha256
+ || !kindOk
+ || current.valueLength !== record.readbackLength
+ || current.valueSha256 !== record.readbackSha256) {
+ records.delete(key);
+ }
+ 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 || ''));
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 96c11d976..4fcf607f5 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -17890,6 +17890,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
ambiguous: false,
focusedProof: true,
focusedKind: kind,
+ // Stable pre-submit revalidation locator (focus moves before submit,
+ // so {focused:true} would digest the wrong field). The edit-file page
+ // owns a single file editor; the commit-message input has a stable id.
+ refreshSelector: kind === 'editor' ? '[contenteditable="true"]' : '#commit-message-input',
};
}
@@ -17927,14 +17931,40 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
for (const [key, record] of records) {
if (this._normalizeUrl(record?.pageUrl || '') !== this._normalizeUrl(pageUrl)) continue;
// Focused proofs are bound at write time to the then-focused field's
- // live digest (focus stays on the edited field through verification).
- // Re-digesting via {focused:true} after focus moved (e.g. editor proof
- // while the commit-message input is focused) would compare the wrong
- // field and delete a good proof, permanently blocking the documented
- // click-then-type_text flow. External mutations after the proof are
- // still caught fail-closed by the byte-exact raw-blob check post-commit,
- // and any later write to the same focused kind overwrites this record.
- if (record?.focusedProof === true) continue;
+ // live digest, but focus moves before submit (editor β†’ commit dialog β†’
+ // commit-message input), so re-digesting via {focused:true} would
+ // compare the wrong field. Revalidate via the stable refreshSelector
+ // captured at mint instead; a mismatch or missing element deletes the
+ // proof fail-closed. The byte-exact raw-blob check post-commit stays as
+ // backstop for the file; the commit message has no post-commit check,
+ // so its stable-id revalidation is the gate.
+ if (record?.focusedProof === true) {
+ const refreshSelector = typeof record.refreshSelector === 'string' && record.refreshSelector.trim()
+ ? record.refreshSelector.trim()
+ : (record.focusedKind === 'editor' ? '[contenteditable="true"]' : '#commit-message-input');
+ let current = null;
+ try {
+ current = await this._textMutationValueDigest(tabId, {
+ locatorType: 'selector', selector: refreshSelector, ambiguous: false,
+ });
+ } catch { current = null; }
+ const kindOk = record.focusedKind === 'editor'
+ ? (current?.fieldMeta?.contentEditable === true
+ || /\bediting\b[\s\S]*\bfile contents\b/i.test(String(
+ current?.fieldMeta?.ariaLabelledByText || current?.fieldMeta?.ariaLabel || '',
+ )))
+ : /^(?:commit-message-input|commit_message)$/i.test(String(
+ current?.fieldMeta?.id || current?.fieldMeta?.name || '',
+ ));
+ if (!current
+ || !record.readbackSha256
+ || !kindOk
+ || current.valueLength !== record.readbackLength
+ || current.valueSha256 !== record.readbackSha256) {
+ records.delete(key);
+ }
+ 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 || ''));
diff --git a/test/run.js b/test/run.js
index 553dbb3a1..456e415b3 100644
--- a/test/run.js
+++ b/test/run.js
@@ -75256,32 +75256,54 @@ test('focused editor and commit-message writes authorize a commit', async () =>
try {
const body = '# Focused document\n\nOne complete copy.\n';
const commitMsg = 'Update docs/plan.md';
- // Digest probe answers for the then-focused field at write time. Focus
- // stays on the edited field through verification, so {focused:true}
- // observes the right element here; refresh skips focused proofs because
- // focus moves before submit.
- const makeTabs = (agent) => ({
+ // Digest probe answers for the then-focused field at write time (focus
+ // stays on the edited field through verification), and for the stable
+ // refresh selectors pre-submit (focus has moved by then). Mutating the
+ // live values below simulates a user/page edit between proof and submit,
+ // which refresh must catch fail-closed.
+ const makeTabs = (agent, live) => ({
async sendMessage(_tabId, message) {
assert.equal(message.action, 'field_value_digest');
const expected = message.params?.expected;
+ const selector = message.params?.selector;
+ const editorMeta = { contentEditable: true, ariaLabelledByText: 'Editing file contents' };
+ const messageMeta = { id: 'commit-message-input', name: 'commit-message-input' };
+ // Write-time focused mint (focus is still on the edited field).
if (expected === body) {
return {
success: true, verified: true,
- valueLength: body.length,
- valueSha256: await agent._sha256Text(body),
- fieldMeta: { contentEditable: true, ariaLabelledByText: 'Editing file contents' },
+ valueLength: live.body.length,
+ valueSha256: await agent._sha256Text(live.body),
+ fieldMeta: editorMeta,
documentToken: 'doc-focused', refScopeUrl: 'https://github.com/example/repo/edit/main/docs/plan.md',
};
}
if (expected === commitMsg) {
return {
success: true, verified: true,
- valueLength: commitMsg.length,
- valueSha256: await agent._sha256Text(commitMsg),
- fieldMeta: { id: 'commit-message-input', name: 'commit-message-input' },
+ valueLength: live.msg.length,
+ valueSha256: await agent._sha256Text(live.msg),
+ fieldMeta: messageMeta,
documentToken: 'doc-focused', refScopeUrl: 'https://github.com/example/repo/edit/main/docs/plan.md',
};
}
+ // Pre-submit stable-selector revalidation (focus has moved).
+ if (selector === '[contenteditable="true"]') {
+ return {
+ success: true,
+ valueLength: live.body.length,
+ valueSha256: await agent._sha256Text(live.body),
+ fieldMeta: editorMeta,
+ };
+ }
+ if (selector === '#commit-message-input') {
+ return {
+ success: true,
+ valueLength: live.msg.length,
+ valueSha256: await agent._sha256Text(live.msg),
+ fieldMeta: messageMeta,
+ };
+ }
return { success: false, documentToken: 'doc-focused', refScopeUrl: 'https://github.com/example/repo/edit/main/docs/plan.md' };
},
});
@@ -75299,7 +75321,8 @@ test('focused editor and commit-message writes authorize a commit', async () =>
});
agent._taskTokens.set(tabId, 'task-focused');
agent._lastAxScopes.set(tabId, { documentToken: 'doc-focused', pageUrl });
- const tabs = makeTabs(agent);
+ const live = { body, msg: commitMsg };
+ const tabs = makeTabs(agent, live);
globalThis.chrome = { tabs };
globalThis.browser = { tabs };
// Documented flow: click the editor, then type_text({text, clear:true})
@@ -75326,10 +75349,22 @@ test('focused editor and commit-message writes authorize a commit', async () =>
`${label}: focused editor and commit-message proofs collided (keys: ${keys})`);
const binding = agent._workflowSubmitBindingForAttempt(tabId, pageUrl, {});
assert.ok(binding?.githubFileCommit, `${label}: focused proofs did not authorize a commit`);
- // Pre-submit refresh (focus now elsewhere) must keep focused proofs.
+ // Pre-submit refresh (focus now elsewhere) must keep focused proofs
+ // via their stable selectors, and must drop them fail-closed when the
+ // live field no longer matches (user/page edited after the proof).
await agent._refreshGithubTextReplacementProofs(tabId, pageUrl);
assert.ok(agent._workflowSubmitBindingForAttempt(tabId, pageUrl, {})?.githubFileCommit,
`${label}: refresh dropped a focused proof after focus moved`);
+ live.body = `${body}\nExternal edit.\n`;
+ await agent._refreshGithubTextReplacementProofs(tabId, pageUrl);
+ assert.equal(agent._workflowSubmitBindingForAttempt(tabId, pageUrl, {})?.githubFileCommit, undefined,
+ `${label}: stale focused editor proof authorized a commit after an external edit`);
+ live.body = body;
+ // Re-verify the editor to restore the binding for the append check.
+ await agent._finalizeTextMutationResult(tabId, 'type_text', { text: body, clear: true }, editorResult);
+ await agent._finalizeTextMutationResult(tabId, 'type_text', { text: commitMsg, clear: true }, messageResult);
+ assert.ok(agent._workflowSubmitBindingForAttempt(tabId, pageUrl, {})?.githubFileCommit,
+ `${label}: re-verified focused proofs did not restore the binding`);
// Same-kind focused append invalidates only its own kind.
await agent._finalizeTextMutationResult(tabId, 'type_text', { text: ' typo' }, {
success: true, verified: true,