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
Publication prefetch: the end of a run cancels its background checks
Barack Sokulluc4a509a26c98
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 183123ae7..5c75bc2ca 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -6229,6 +6229,7 @@ export class Agent extends LoopDetector {_releaseRunEntry(tabId) {this._settleUnverifiedStepScope(tabId);+ this._cancelSocialPublicationBackground(tabId);const feedbackRun = this._pageFeedbackRuns?.get(tabId);if (feedbackRun?.monitoringRetry && !feedbackRun.monitoringRetry.started) {this._restoreFailureRecoveryStart(tabId, feedbackRun.monitoringRetry.requestId, feedbackRun.onUpdate, feedbackRun.monitoringRetry);@@ -24034,7 +24035,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// A background compile of another input can never be installed:// cancel its request (and an audit of what it compiled) first.if (prefetch) this._dropSocialPublicationContractPrefetch(guard, prefetch);- const abort = this._socialPublicationBackgroundAbort(binding);+ const abort = this._socialPublicationBackgroundAbort(binding, tabId);const call = this._socialPublicationBackgroundCall(tabId, guard, binding, abort);const entry = { previous, state, settled: false, compiled: null, done: null, abort };prefetches.set(guard, entry);@@ -24734,7 +24735,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// this one.if (running?.key === key && running.runSignal === binding.signal && !running.abort.signal.aborted) return running.done;if (running) this._dropSocialPublicationAuditPrefetch(social, running);- const abort = this._socialPublicationBackgroundAbort(binding);+ const abort = this._socialPublicationBackgroundAbort(binding, tabId);const call = this._socialPublicationBackgroundCall(tabId, guard, binding, abort);const entry = { key, done: null, abort, runSignal: binding.signal, settled: false };audits.set(social, entry);@@ -24786,9 +24787,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (!keepAudit && kept.compiled) this._dropSocialPublicationAuditPrefetch(kept.compiled);}- // A background publication check's own abort: the Stop of the run that- // started it, or a newer check that supersedes it.- _socialPublicationBackgroundAbort(binding) {+ // A background publication check's own abort: the Stop or the end of the+ // run that started it, or a newer check that supersedes it.+ _socialPublicationBackgroundAbort(binding, tabId = null) {const controller = new AbortController();const runSignal = binding?.signal || null;const onAbort = () => {@@ -24796,13 +24797,35 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d};if (runSignal?.aborted) onAbort();else runSignal?.addEventListener?.('abort', onAbort, { once: true });- return {+ // Kept per tab until it settles, so the end of its run cancels it too+ // (_cancelSocialPublicationBackground), not only a Stop.+ const open = tabId == null ? null : (this._socialPublicationBackgroundAborts ||= new Map());+ if (open && !open.has(tabId)) open.set(tabId, new Set());+ const abort = {controller,signal: controller.signal,dispose: () => {try { runSignal?.removeEventListener?.('abort', onAbort); } catch {}+ open?.get(tabId)?.delete(abort);},};+ open?.get(tabId).add(abort);+ return abort;+ }++ // The run on `tabId` ended, however it ended: the background publication+ // checks it started stop with it, so they neither keep spending nor hold a+ // provider's queue or the cost allowance for the next run.+ _cancelSocialPublicationBackground(tabId) {+ const open = this._socialPublicationBackgroundAborts?.get(tabId);+ if (!open) return;+ this._socialPublicationBackgroundAborts.delete(tabId);+ for (const abort of open) {+ if (abort.controller.signal.aborted) continue;+ const reason = new Error('The run that started this background check ended.');+ reason.name = 'AbortError';+ abort.controller.abort(reason);+ }}// What a background model call is bound to: its abort, the allowance of thediff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 1dec3a2b8..6bf253c4e 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -5775,6 +5775,7 @@ export class Agent extends LoopDetector {_releaseRunEntry(tabId) {this._settleUnverifiedStepScope(tabId);+ this._cancelSocialPublicationBackground(tabId);const feedbackRun = this._pageFeedbackRuns?.get(tabId);if (feedbackRun?.monitoringRetry && !feedbackRun.monitoringRetry.started) {this._restoreFailureRecoveryStart(tabId, feedbackRun.monitoringRetry.requestId, feedbackRun.onUpdate, feedbackRun.monitoringRetry);@@ -21217,7 +21218,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// A background compile of another input can never be installed:// cancel its request (and an audit of what it compiled) first.if (prefetch) this._dropSocialPublicationContractPrefetch(guard, prefetch);- const abort = this._socialPublicationBackgroundAbort(binding);+ const abort = this._socialPublicationBackgroundAbort(binding, tabId);const call = this._socialPublicationBackgroundCall(tabId, guard, binding, abort);const entry = { previous, state, settled: false, compiled: null, done: null, abort };prefetches.set(guard, entry);@@ -21917,7 +21918,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// this one.if (running?.key === key && running.runSignal === binding.signal && !running.abort.signal.aborted) return running.done;if (running) this._dropSocialPublicationAuditPrefetch(social, running);- const abort = this._socialPublicationBackgroundAbort(binding);+ const abort = this._socialPublicationBackgroundAbort(binding, tabId);const call = this._socialPublicationBackgroundCall(tabId, guard, binding, abort);const entry = { key, done: null, abort, runSignal: binding.signal, settled: false };audits.set(social, entry);@@ -21969,9 +21970,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (!keepAudit && kept.compiled) this._dropSocialPublicationAuditPrefetch(kept.compiled);}- // A background publication check's own abort: the Stop of the run that- // started it, or a newer check that supersedes it.- _socialPublicationBackgroundAbort(binding) {+ // A background publication check's own abort: the Stop or the end of the+ // run that started it, or a newer check that supersedes it.+ _socialPublicationBackgroundAbort(binding, tabId = null) {const controller = new AbortController();const runSignal = binding?.signal || null;const onAbort = () => {@@ -21979,13 +21980,35 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d};if (runSignal?.aborted) onAbort();else runSignal?.addEventListener?.('abort', onAbort, { once: true });- return {+ // Kept per tab until it settles, so the end of its run cancels it too+ // (_cancelSocialPublicationBackground), not only a Stop.+ const open = tabId == null ? null : (this._socialPublicationBackgroundAborts ||= new Map());+ if (open && !open.has(tabId)) open.set(tabId, new Set());+ const abort = {controller,signal: controller.signal,dispose: () => {try { runSignal?.removeEventListener?.('abort', onAbort); } catch {}+ open?.get(tabId)?.delete(abort);},};+ open?.get(tabId).add(abort);+ return abort;+ }++ // The run on `tabId` ended, however it ended: the background publication+ // checks it started stop with it, so they neither keep spending nor hold a+ // provider's queue or the cost allowance for the next run.+ _cancelSocialPublicationBackground(tabId) {+ const open = this._socialPublicationBackgroundAborts?.get(tabId);+ if (!open) return;+ this._socialPublicationBackgroundAborts.delete(tabId);+ for (const abort of open) {+ if (abort.controller.signal.aborted) continue;+ const reason = new Error('The run that started this background check ended.');+ reason.name = 'AbortError';+ abort.controller.abort(reason);+ }}// What a background model call is bound to: its abort, the allowance of thediff --git a/test/publication-prefetch.mjs b/test/publication-prefetch.mjsindex 7ed14a031..212eb218f 100644--- a/test/publication-prefetch.mjs+++ b/test/publication-prefetch.mjs@@ -546,6 +546,25 @@ for (const browser of ['chrome', 'firefox']) {}});+ test(`${browser}: the end of the run that started a background check cancels it, not only a Stop`, async () => {+ for (const stage of ['social_publication_contract', 'social_publication_authorization']) {+ const f = setup();+ const { agent, tabId } = f;+ agent._runAbortStates.set(tabId, { controller: new AbortController(), dispose() {} });+ const held = f.hold(stage);+ const prefetch = f.write();+ await until(() => held.started, `${stage}: the background check started`);+ const [request] = f.named(stage);+ // The run finishes normally: no Stop, its abort controller is only disposed.+ agent._releaseRunEntry(tabId);+ await settle();+ assert.equal(request.aborted, true, `${stage}: the request in flight was cancelled when its run ended`);+ held.release();+ assert.notEqual(await within(prefetch, `${stage}: the prefetch settles`), 'authorized', stage);+ assert.equal(agent._socialPublicationBackgroundAborts?.get(tabId)?.size || 0, 0, `${stage}: nothing is left to cancel`);+ }+ });+test(`${browser}: a newer draft cancels the background audit of the older one, and a click awaiting it audits on its own`, async () => {const f = setup();const audit = f.hold('social_publication_authorization');