Skip to content

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

Publication prefetch: a run's background checks end before its trace closes

Barack Sokullu0f3c5076bf49
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index ceede78b5..6906d9298 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -24745,7 +24745,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const hint = name === 'upload_file' ? hints.get(guard) || null : this._socialPublicationPrefetchHint(name, args);
if (!hint) return null;
hints.set(guard, hint);
- return this._socialPublicationPrefetchRun(tabId, guard, provider, hint).catch(() => null);
+ return this._trackSocialPublicationBackgroundRun(tabId,
+ this._socialPublicationPrefetchRun(tabId, guard, provider, hint).catch(() => null));
} catch {
return null;
}
@@ -24765,7 +24766,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
|| (social && (this._socialPublicationCompiles?.get(social)
|| !social.needsRecompile || Object.keys(social.outcomes || {}).length))
|| (pending && pending.previous === social)) return null;
- return this._socialPublicationPrefetchRun(tabId, guard, provider, hint, { contractOnly: true }).catch(() => null);
+ return this._trackSocialPublicationBackgroundRun(tabId,
+ this._socialPublicationPrefetchRun(tabId, guard, provider, hint, { contractOnly: true }).catch(() => null));
} catch {
return null;
}
@@ -24950,6 +24952,37 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return abort;
}
+ // A background prefetch kept per tab until it settles, so its run can wait
+ // for it before closing the trace (_settleSocialPublicationBackground).
+ _trackSocialPublicationBackgroundRun(tabId, run) {
+ const runs = (this._socialPublicationBackgroundRuns ||= new Map());
+ let open = runs.get(tabId);
+ if (!open) runs.set(tabId, (open = new Set()));
+ open.add(run);
+ run.finally(() => {
+ open.delete(run);
+ if (!open.size && runs.get(tabId) === open) runs.delete(tabId);
+ });
+ return run;
+ }
+
+ // Before a run's trace closes: its background publication checks are
+ // cancelled and briefly awaited, so a request that answers late is not
+ // counted after the run's totals, and each check's last note lands before
+ // the trace ends. A check that does not settle in time is cancelled anyway
+ // and records nothing more that matters to this run.
+ async _settleSocialPublicationBackground(tabId, timeoutMs = 2000) {
+ this._cancelSocialPublicationBackground(tabId);
+ const open = this._socialPublicationBackgroundRuns?.get(tabId);
+ if (!open?.size) return;
+ let timer = null;
+ await Promise.race([
+ Promise.allSettled([...open]),
+ new Promise(resolve => { timer = setTimeout(resolve, timeoutMs); }),
+ ]);
+ clearTimeout(timer);
+ }
+
// 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.
@@ -25245,6 +25278,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
* Shared by the streaming and non-streaming message paths. (#9)
*/
async _endTraceRun(tabId, runId, status, finalContent, { provider = null, messages = null, mode = '', shareRequest = null, shareResponse = null, hadProviderCompletion = false } = {}) {
+ // The run's background checks end before its trace and diagnostics do.
+ await this._settleSocialPublicationBackground(tabId);
if (String(provider?.config?.providerName || '').toLowerCase() === 'webbrain-cloud') {
try {
const sessionId = this.conversationIds.get(tabId) || null;
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 83671dbbd..26fd1a7f5 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -21927,7 +21927,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const hint = name === 'upload_file' ? hints.get(guard) || null : this._socialPublicationPrefetchHint(name, args);
if (!hint) return null;
hints.set(guard, hint);
- return this._socialPublicationPrefetchRun(tabId, guard, provider, hint).catch(() => null);
+ return this._trackSocialPublicationBackgroundRun(tabId,
+ this._socialPublicationPrefetchRun(tabId, guard, provider, hint).catch(() => null));
} catch {
return null;
}
@@ -21947,7 +21948,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
|| (social && (this._socialPublicationCompiles?.get(social)
|| !social.needsRecompile || Object.keys(social.outcomes || {}).length))
|| (pending && pending.previous === social)) return null;
- return this._socialPublicationPrefetchRun(tabId, guard, provider, hint, { contractOnly: true }).catch(() => null);
+ return this._trackSocialPublicationBackgroundRun(tabId,
+ this._socialPublicationPrefetchRun(tabId, guard, provider, hint, { contractOnly: true }).catch(() => null));
} catch {
return null;
}
@@ -22132,6 +22134,37 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return abort;
}
+ // A background prefetch kept per tab until it settles, so its run can wait
+ // for it before closing the trace (_settleSocialPublicationBackground).
+ _trackSocialPublicationBackgroundRun(tabId, run) {
+ const runs = (this._socialPublicationBackgroundRuns ||= new Map());
+ let open = runs.get(tabId);
+ if (!open) runs.set(tabId, (open = new Set()));
+ open.add(run);
+ run.finally(() => {
+ open.delete(run);
+ if (!open.size && runs.get(tabId) === open) runs.delete(tabId);
+ });
+ return run;
+ }
+
+ // Before a run's trace closes: its background publication checks are
+ // cancelled and briefly awaited, so a request that answers late is not
+ // counted after the run's totals, and each check's last note lands before
+ // the trace ends. A check that does not settle in time is cancelled anyway
+ // and records nothing more that matters to this run.
+ async _settleSocialPublicationBackground(tabId, timeoutMs = 2000) {
+ this._cancelSocialPublicationBackground(tabId);
+ const open = this._socialPublicationBackgroundRuns?.get(tabId);
+ if (!open?.size) return;
+ let timer = null;
+ await Promise.race([
+ Promise.allSettled([...open]),
+ new Promise(resolve => { timer = setTimeout(resolve, timeoutMs); }),
+ ]);
+ clearTimeout(timer);
+ }
+
// 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.
@@ -22421,6 +22454,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
* Shared by the streaming and non-streaming message paths. (#9)
*/
async _endTraceRun(tabId, runId, status, finalContent, { provider = null, messages = null, mode = '', shareRequest = null, shareResponse = null, hadProviderCompletion = false } = {}) {
+ // The run's background checks end before its trace and diagnostics do.
+ await this._settleSocialPublicationBackground(tabId);
if (String(provider?.config?.providerName || '').toLowerCase() === 'webbrain-cloud') {
try {
const sessionId = this.conversationIds.get(tabId) || null;
diff --git a/test/publication-prefetch.mjs b/test/publication-prefetch.mjs
index b9e36b0e1..7d88841db 100644
--- a/test/publication-prefetch.mjs
+++ b/test/publication-prefetch.mjs
@@ -651,6 +651,28 @@ for (const browser of ['chrome', 'firefox']) {
}
});
+ test(`${browser}: a run's background checks end before its trace closes`, 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);
+ let settled = false;
+ const prefetch = f.write().then(status => { settled = true; return status; });
+ await until(() => held.started, `${stage}: the background check started`);
+ const [request] = f.named(stage);
+ // _endTraceRun flushes its last records right before trace.endRun.
+ let atTraceEnd = null;
+ agent._flushAdapterMatchTraceRun = async () => { atTraceEnd = { aborted: request.aborted, settled }; };
+ await within(agent._endTraceRun(tabId, 'run-A', 'done', 'Posted.'), `${stage}: the trace closes`);
+ assert.deepEqual(atTraceEnd, { aborted: true, settled: true },
+ `${stage}: the check was still running when its run's trace closed`);
+ held.release();
+ assert.notEqual(await prefetch, 'authorized', stage);
+ assert.equal(agent._socialPublicationBackgroundRuns?.get(tabId)?.size || 0, 0, `${stage}: nothing is left running`);
+ }
+ });
+
test(`${browser}: a prefetch that reaches the draft the click is auditing defers to the click`, async () => {
const f = setup();
// The click compiles the contract and audits the draft itself; its audit