Skip to content

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

Ask cards: an approval covers only the content the user saw

Barack Sokullufc59f8c96c39
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index aaf7a2fe1..8be1c51fa 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -3355,10 +3355,13 @@ export class Agent extends LoopDetector {
if (binding?.metadataRequirementsIncomplete !== true && binding?.githubFileCommit) {
const stepKey = this._sha256TextSync(JSON.stringify(['github-commit-split-write-required',
this._normalizeUrl(pageUrl), this._formValidationActionKey(name, args) || name, this._taskTokens.get(tabId) || '']));
- if (this._unverifiedStepApproval(tabId, 'github-commit-split-write-required', stepKey, { consume: true })) return null;
- return { ...splitWriteRefusal, unverifiedStepConfirmation: { guardId: 'github-commit-split-write-required',
- hard: false, action: 'commit', reason: 'submit_unclear', host: normalizeHost(pageUrl) || 'github.com',
- contentLabel: 'editor', target: this._githubEditorReadTarget(tabId, pageUrl), expectKind: 'github-editor', stepKey } };
+ const ask = { guardId: 'github-commit-split-write-required', hard: false, action: 'commit', reason: 'submit_unclear',
+ host: normalizeHost(pageUrl) || 'github.com', contentLabel: 'editor', target: this._githubEditorReadTarget(tabId, pageUrl),
+ expectKind: 'github-editor', requireContent: true, stepKey };
+ // The answer covers the editor text the card showed: the editor is
+ // read again first, and changed or unreadable text is asked again.
+ if (await this._consumeContentBoundApproval(tabId, ask)) return null;
+ return { ...splitWriteRefusal, unverifiedStepConfirmation: ask };
}
}
return splitWriteRefusal;
@@ -30387,6 +30390,24 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return entry;
}
+ /**
+ * Use up the one-step 'once' for `ask` only while the page still shows the
+ * content the user saw on the card: the same target is read again now, and
+ * its text must match the answer's digest and length. The user's latest
+ * answer for the step decides; changed or unreadable content returns
+ * false, so the caller asks again with what the page holds now.
+ */
+ async _consumeContentBoundApproval(tabId, ask) {
+ const approval = this._unverifiedStepApproval(tabId, ask?.guardId, ask?.stepKey);
+ if (!approval) return false;
+ const reading = await this._unverifiedStepReading(tabId, ask);
+ if (!reading.readable || !approval.contentSha256 || reading.sha256 !== approval.contentSha256
+ || reading.length !== approval.contentLength) return false;
+ if (approval.consumed === true || approval.stale === true) return false;
+ approval.consumed = true;
+ return true;
+ }
+
// The answers a trusted Continue of the same task keeps: the user's own,
// newest last. An answer WebBrain chose (timeout, Instant, unattended, no
// card) is not carried; the continued run lets the user answer.
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index fff4ec74b..67604faeb 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -2912,10 +2912,13 @@ export class Agent extends LoopDetector {
if (binding?.metadataRequirementsIncomplete !== true && binding?.githubFileCommit) {
const stepKey = this._sha256TextSync(JSON.stringify(['github-commit-split-write-required',
this._normalizeUrl(pageUrl), this._formValidationActionKey(name, args) || name, this._taskTokens.get(tabId) || '']));
- if (this._unverifiedStepApproval(tabId, 'github-commit-split-write-required', stepKey, { consume: true })) return null;
- return { ...splitWriteRefusal, unverifiedStepConfirmation: { guardId: 'github-commit-split-write-required',
- hard: false, action: 'commit', reason: 'submit_unclear', host: normalizeHost(pageUrl) || 'github.com',
- contentLabel: 'editor', target: this._githubEditorReadTarget(tabId, pageUrl), expectKind: 'github-editor', stepKey } };
+ const ask = { guardId: 'github-commit-split-write-required', hard: false, action: 'commit', reason: 'submit_unclear',
+ host: normalizeHost(pageUrl) || 'github.com', contentLabel: 'editor', target: this._githubEditorReadTarget(tabId, pageUrl),
+ expectKind: 'github-editor', requireContent: true, stepKey };
+ // The answer covers the editor text the card showed: the editor is
+ // read again first, and changed or unreadable text is asked again.
+ if (await this._consumeContentBoundApproval(tabId, ask)) return null;
+ return { ...splitWriteRefusal, unverifiedStepConfirmation: ask };
}
}
return splitWriteRefusal;
@@ -27499,6 +27502,24 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return entry;
}
+ /**
+ * Use up the one-step 'once' for `ask` only while the page still shows the
+ * content the user saw on the card: the same target is read again now, and
+ * its text must match the answer's digest and length. The user's latest
+ * answer for the step decides; changed or unreadable content returns
+ * false, so the caller asks again with what the page holds now.
+ */
+ async _consumeContentBoundApproval(tabId, ask) {
+ const approval = this._unverifiedStepApproval(tabId, ask?.guardId, ask?.stepKey);
+ if (!approval) return false;
+ const reading = await this._unverifiedStepReading(tabId, ask);
+ if (!reading.readable || !approval.contentSha256 || reading.sha256 !== approval.contentSha256
+ || reading.length !== approval.contentLength) return false;
+ if (approval.consumed === true || approval.stale === true) return false;
+ approval.consumed = true;
+ return true;
+ }
+
// The answers a trusted Continue of the same task keeps: the user's own,
// newest last. An answer WebBrain chose (timeout, Instant, unattended, no
// card) is not carried; the continued run lets the user answer.
diff --git a/test/guard-ask-cards.mjs b/test/guard-ask-cards.mjs
index 0a9027c50..3e552b0e7 100644
--- a/test/guard-ask-cards.mjs
+++ b/test/guard-ask-cards.mjs
@@ -528,6 +528,61 @@ for (const { build, Agent, adapters, Scheduler } of builds) {
}
});
+ test(`${build}: a split-write answer covers only the editor text the user saw`, async () => {
+ const { agent, tab, page } = await githubFixture();
+ // A verified editor proof: the commit binding holds for POST_BODY.
+ const editorTarget = agent._textMutationTarget(tab, 'set_field', { ref_id: 'ref_editor' });
+ agent._unverifiedTextWrites.delete(tab);
+ agent._verifiedTextReplacements.get(tab).set(editorTarget.key, await agent._verifiedTextReplacementRecord(tab, editorTarget, POST_BODY, EDITOR_META));
+ const rerun = () => agent._workflowPreSubmitDispatchBlock(tab, 'execute_js', { code: 'void 0' }, null);
+ const block = await rerun();
+ assert.equal(block.unverifiedStepConfirmation.requireContent, true, 'the card needs the editor text it covers');
+ // The card shows text that differs from what the action would commit
+ // (the editor changed after the check), and the editor changes back while
+ // the card is open: the binding holds again, but the user approved other
+ // text.
+ const shown = `${POST_BODY}A line the user saw on the card.\n`;
+ page.editor = shown;
+ const cards = [];
+ const advisories = [];
+ const changed = await agent._resolveUnverifiedStepConfirmation(tab, block, (type, data) => {
+ if (type !== 'clarify') return;
+ cards.push(data);
+ page.editor = POST_BODY;
+ agent.submitClarifyResponse(tab, data.clarifyId, 'once', 'option');
+ }, rerun, advisories);
+ assert.equal(cards.length, 1);
+ assert.equal(cards[0].unverifiedStepConfirmation.content, shown);
+ assert.ok(changed, 'the action is not performed on text the user did not see');
+ assert.equal(changed.noDispatch, true);
+ assert.match(changed.error, /changed while the user was being asked/);
+ assert.deepEqual(advisories, []);
+ // The next attempt asks again with what the editor holds now, and that
+ // answer covers one action.
+ const next = await rerun();
+ assert.ok(next.unverifiedStepConfirmation, 'asked again');
+ const resolved = await agent._resolveUnverifiedStepConfirmation(tab, next, (type, data) => {
+ if (type !== 'clarify') return;
+ cards.push(data);
+ agent.submitClarifyResponse(tab, data.clarifyId, 'once', 'option');
+ }, rerun, advisories);
+ assert.equal(cards.length, 2);
+ assert.equal(cards[1].unverifiedStepConfirmation.content, POST_BODY, 'the card shows the editor as it reads now');
+ assert.equal(resolved, null, 'unchanged text goes ahead once');
+ assert.ok((await rerun()).unverifiedStepConfirmation, 'one answer covers one action');
+ // An editor that cannot be read cannot be approved.
+ const unreadable = await githubFixture();
+ const target = unreadable.agent._textMutationTarget(unreadable.tab, 'set_field', { ref_id: 'ref_editor' });
+ unreadable.agent._unverifiedTextWrites.delete(unreadable.tab);
+ unreadable.agent._verifiedTextReplacements.get(unreadable.tab).set(target.key,
+ await unreadable.agent._verifiedTextReplacementRecord(unreadable.tab, target, POST_BODY, EDITOR_META));
+ const unreadableBlock = await unreadable.agent._workflowPreSubmitDispatchBlock(unreadable.tab, 'execute_js', { code: 'void 0' }, null);
+ unreadable.agent._rememberUnverifiedStepDecision(unreadable.tab, unreadableBlock.unverifiedStepConfirmation,
+ { sha256: '', length: null }, 'once', 'user');
+ assert.equal(await unreadable.agent._consumeContentBoundApproval(unreadable.tab,
+ { ...unreadableBlock.unverifiedStepConfirmation, target: null }), false, 'no reading, no approval');
+ });
+
// ── social-publication-draft-quality ───────────────────────────────────
// The user asked to publish; the X composer's draft could not be confirmed
// against the contract. The card shows the composer's text; the