Skip to content

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

fix(agent): preserve mutation debt across conversation clears

Emre Sokullua11a53c37736
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index b1d036183..630c540bc 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -20542,9 +20542,16 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
this._isPdfTabCache.delete(tabId);
this._lastTypeFieldIdent?.delete(tabId);
this._lastTypeFieldEpoch?.delete(tabId);
- this._uncertainTextMutations.delete(tabId);
+ // A conversation clear preserves the run guard while the document (and
+ // possibly landed text) is unchanged: document-scoped mutation debt and
+ // its recent-document backlog survive, or the next run could duplicate
+ // landed text with a blind retry. Proofs stay task-scoped and are
+ // discarded with the run; tab removal drops everything.
+ if (!preserveRunGuard) {
+ this._uncertainTextMutations.delete(tabId);
+ this._recentTextMutationDocuments.delete(tabId);
+ }
this._verifiedTextReplacements.delete(tabId);
- this._recentTextMutationDocuments.delete(tabId);
this._lastCdpClickIdent?.delete(tabId);
this._lastClickProgress?.delete(tabId);
this._clickAxCdpFallbacks?.delete(tabId);
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 1282c093c..cdbdc5fef 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -19020,9 +19020,16 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
this._isPdfTabCache.delete(tabId);
this._lastTypeFieldIdent?.delete(tabId);
this._lastTypeFieldEpoch?.delete(tabId);
- this._uncertainTextMutations.delete(tabId);
+ // A conversation clear preserves the run guard while the document (and
+ // possibly landed text) is unchanged: document-scoped mutation debt and
+ // its recent-document backlog survive, or the next run could duplicate
+ // landed text with a blind retry. Proofs stay task-scoped and are
+ // discarded with the run; tab removal drops everything.
+ if (!preserveRunGuard) {
+ this._uncertainTextMutations.delete(tabId);
+ this._recentTextMutationDocuments.delete(tabId);
+ }
this._verifiedTextReplacements.delete(tabId);
- this._recentTextMutationDocuments.delete(tabId);
this.progressPageScopes.delete(tabId);
this.progressSessions.delete(tabId);
this.progressExpectedItems.delete(tabId);
diff --git a/test/run.js b/test/run.js
index 5e8793e2f..efc263798 100644
--- a/test/run.js
+++ b/test/run.js
@@ -77525,6 +77525,38 @@ test('first live token bootstraps tokenless debt by URL', async () => {
}
});
+test('clearConversation preserves document-scoped mutation debt', async () => {
+ for (const [label, AgentClass, tabId] of [
+ ['chrome', AgentCh, 9524],
+ ['firefox', AgentFx, 9525],
+ ]) {
+ const agent = new AgentClass({});
+ agent._lastAxScopes.set(tabId, { documentToken: 'doc-conv', pageUrl: 'https://example.test/form' });
+ await agent._finalizeTextMutationResult(
+ tabId, 'type_text', { selector: '#field', text: 'maybe landed', clear: true },
+ { success: false, dispatched: true, verified: false },
+ );
+ assert.equal(agent._uncertainTextMutations.get(tabId)?.size, 1, `${label}: debt was not recorded`);
+ // A verified proof stands in for task-scoped state that must not cross.
+ agent._verifiedTextReplacements.set(tabId, new Map([['k', { pageUrl: 'https://example.test/form' }]]));
+ agent.clearConversation(tabId);
+ assert.equal(agent._uncertainTextMutations.get(tabId)?.size, 1,
+ `${label}: conversation clear dropped the document guard`);
+ assert.equal(agent._verifiedTextReplacements.has(tabId), false,
+ `${label}: task-scoped proofs survived the conversation clear`);
+ // The next run on the unchanged document stays guarded.
+ const blocked = await agent._uncertainTextMutationBlock(
+ tabId, 'type_text', { selector: '#field', text: 'maybe landed appended' },
+ );
+ assert.equal(blocked?.repeatBlocked, true,
+ `${label}: post-clear retry escaped the retained guard`);
+ // Tab removal still drops everything.
+ agent._cleanupTab(tabId);
+ assert.equal(agent._uncertainTextMutations.has(tabId), false,
+ `${label}: tab removal kept mutation debt`);
+ }
+});
+
test('sync SHA-256 helper matches the async subtle digest', async () => {
for (const [label, AgentClass] of [['chrome', AgentCh], ['firefox', AgentFx]]) {
const agent = new AgentClass({});