Skip to content

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

Publication prefetch: a wait that only joins another run's caption reads stops with its run

Barack Sokullud9a3cf5cd2fa
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index ca12a30d9..0cbef29e1 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -25049,6 +25049,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
.filter(caption => caption && caption.length <= 200))];
// A caption an earlier prefetch is still reading is not asked twice: its
// read in flight is joined, so this prefetch ends with its answer too.
+ // The wait for it belongs to this prefetch's run like its own reads: that
+ // run's Stop, end or correction stops it, even when every caption is
+ // joined and the read belongs to another tab's run.
const reading = this._socialPublicationPrefetchReading ||= new Map();
const unread = kind => list.filter(caption => this._pageTextReading(kind, caption) === null);
let guarded = false;
@@ -25059,7 +25062,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
...unreadControls.map(caption => reading.get('message_control\u0000' + caption))].filter(Boolean))];
const effects = unreadEffects.filter(caption => !reading.has('control_effect\u0000' + caption)).slice(0, 24);
const controls = unreadControls.filter(caption => !reading.has('message_control\u0000' + caption)).slice(0, 48);
- if (!effects.length && !controls.length) return Promise.allSettled(joined).then(() => []);
+ if (!effects.length && !controls.length && !joined.length) return Promise.resolve([]);
const abort = this._socialPublicationBackgroundAbort(binding, tabId);
const background = this._socialPublicationBackgroundCall(tabId, guard, binding, abort);
// A correction or clarification cancels them with the contract and the
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 884f3e7e2..995070b7a 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -22231,6 +22231,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
.filter(caption => caption && caption.length <= 200))];
// A caption an earlier prefetch is still reading is not asked twice: its
// read in flight is joined, so this prefetch ends with its answer too.
+ // The wait for it belongs to this prefetch's run like its own reads: that
+ // run's Stop, end or correction stops it, even when every caption is
+ // joined and the read belongs to another tab's run.
const reading = this._socialPublicationPrefetchReading ||= new Map();
const unread = kind => list.filter(caption => this._pageTextReading(kind, caption) === null);
let guarded = false;
@@ -22241,7 +22244,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
...unreadControls.map(caption => reading.get('message_control\u0000' + caption))].filter(Boolean))];
const effects = unreadEffects.filter(caption => !reading.has('control_effect\u0000' + caption)).slice(0, 24);
const controls = unreadControls.filter(caption => !reading.has('message_control\u0000' + caption)).slice(0, 48);
- if (!effects.length && !controls.length) return Promise.allSettled(joined).then(() => []);
+ if (!effects.length && !controls.length && !joined.length) return Promise.resolve([]);
const abort = this._socialPublicationBackgroundAbort(binding, tabId);
const background = this._socialPublicationBackgroundCall(tabId, guard, binding, abort);
// A correction or clarification cancels them with the contract and the
diff --git a/test/publication-prefetch.mjs b/test/publication-prefetch.mjs
index 8b46e3f43..593536082 100644
--- a/test/publication-prefetch.mjs
+++ b/test/publication-prefetch.mjs
@@ -1027,6 +1027,37 @@ for (const browser of ['chrome', 'firefox']) {
assert.equal(agent._socialPublicationPrefetchReading?.size || 0, 0, 'no read is left in flight');
});
+ test(`${browser}: a prefetch that only joins another run's caption reads stops waiting when its own run stops or ends`, async () => {
+ for (const end of ['run end', 'Stop']) {
+ const f = setup();
+ const { agent, tabId } = f;
+ const otherTab = tabId + 1;
+ agent._pageTextReading = () => null;
+ const reads = [];
+ agent._readPageTexts = (_tabId, kind) => new Promise(resolve => { reads.push({ kind, resolve }); });
+ const runA = new AbortController(), runB = new AbortController();
+ const binding = (signal, runId) => ({ signal, costState: null, current: () => !signal.aborted, runId });
+ // One run reads the caption; another tab's prefetch only joins its reads.
+ const first = agent._socialPublicationPrefetchReadings(tabId, ['Post'], f.pageUrl, null, binding(runA.signal, 'run-A'));
+ await until(() => reads.length === 2, `${end}: the first run reads the caption`);
+ let joinedSettled = false;
+ const joined = agent._socialPublicationPrefetchReadings(otherTab, ['Post'], f.pageUrl, null, binding(runB.signal, 'run-B'))
+ .then(value => { joinedSettled = true; return value; });
+ await settle();
+ assert.equal(reads.length, 2, `${end}: the caption is not asked twice`);
+ assert.equal(joinedSettled, false, `${end}: the joining prefetch waits for the read in flight`);
+ // The joining run stops or ends: its wait stops, while the read goes on.
+ if (end === 'Stop') runB.abort();
+ else agent._cancelSocialPublicationBackground(otherTab);
+ assert.deepEqual(await within(joined, `${end}: the joining prefetch stops waiting`), []);
+ assert.equal(agent._socialPublicationBackgroundAborts?.get(otherTab)?.size || 0, 0, `${end}: nothing is left to cancel`);
+ assert.equal(runA.signal.aborted, false, `${end}: the reading run is unaffected`);
+ for (const read of reads) read.resolve();
+ await within(first, `${end}: the reading run ends with its reads`);
+ assert.equal(agent._socialPublicationPrefetchReading?.size || 0, 0, `${end}: no read is left in flight`);
+ }
+ });
+
test(`${browser}: the prefetch's caption reads belong to the run that started it`, async () => {
const f = setup();
const { agent, tabId } = f;