Skip to content

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

Publication prefetch: a caption read in flight is joined, not asked again

Barack Sokulluee5610445c44
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index fae68fff6..94df1077e 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -25035,15 +25035,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const list = [...new Set((Array.isArray(captions) ? captions : [])
.map(caption => String(caption ?? '').replace(/\s+/g, ' ').trim())
.filter(caption => caption && caption.length <= 200))];
- // A caption an earlier prefetch is still reading is not asked twice.
- const reading = this._socialPublicationPrefetchReading ||= new Set();
- const unread = kind => list.filter(caption => this._pageTextReading(kind, caption) === null
- && !reading.has(kind + '\u0000' + caption));
- const effects = unread('control_effect').slice(0, 24);
+ // 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.
+ const reading = this._socialPublicationPrefetchReading ||= new Map();
+ const unread = kind => list.filter(caption => this._pageTextReading(kind, caption) === null);
let guarded = false;
try { guarded = getMessageRecipientGuardPolicy(pageUrl)?.verifyActiveRecipient === true; } catch {}
- const controls = guarded ? unread('message_control').slice(0, 48) : [];
- if (!effects.length && !controls.length) return Promise.resolve([]);
+ const unreadEffects = unread('control_effect');
+ const unreadControls = guarded ? unread('message_control') : [];
+ const joined = [...new Set([...unreadEffects.map(caption => reading.get('control_effect\u0000' + caption)),
+ ...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(() => []);
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
@@ -25052,11 +25056,15 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
|| this._socialPublicationCaptionReads.set(guard, new Set()).get(guard)) : null;
captionReads?.add(abort);
const read = (kind, texts, request) => {
- for (const text of texts) reading.add(kind + '\u0000' + text);
- return Promise.resolve().then(request)
- .finally(() => { for (const text of texts) reading.delete(kind + '\u0000' + text); });
+ const pending = Promise.resolve().then(request);
+ for (const text of texts) reading.set(kind + '\u0000' + text, pending);
+ return pending.finally(() => {
+ for (const text of texts) {
+ if (reading.get(kind + '\u0000' + text) === pending) reading.delete(kind + '\u0000' + text);
+ }
+ });
};
- const reads = [];
+ const reads = [...joined];
if (effects.length) reads.push(read('control_effect', effects, () => this._readControlEffects(tabId, effects, { background })));
if (controls.length) {
reads.push(read('message_control', controls, () => this._readPageTexts(tabId, 'message_control', controls, {},
@@ -28795,6 +28803,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
.map(caption => String(caption ?? '').replace(/\s+/g, ' ').trim())
.filter(caption => caption && caption.length <= 200))].slice(0, 24);
if (!list.length) return;
+ // A caption a background prefetch is reading now is joined, not read a
+ // second time: _readPageTexts below asks only what is still unread, and
+ // makes a held reading one of the most recent the submit probe gets.
+ if (!background) await this._joinBackgroundPageTextReads('control_effect', list);
try {
await this._readPageTexts(tabId, 'control_effect', list, {}, { maxChars: 200, mainTimeoutMs: CONTROL_READING_TIMEOUT_MS,
...(background ? { background } : {}) });
@@ -28884,6 +28896,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
let read = false;
for (const [kind, list] of byKind) {
const pending = [...list].slice(0, 48);
+ // Texts a background prefetch is reading now are joined first, so
+ // _readPageTexts asks only what is still unread (and still makes a
+ // held reading one of the most recent the page gets).
+ await this._joinBackgroundPageTextReads(kind, pending);
try {
await this._readPageTexts(tabId, kind, pending, {}, { maxChars: 400, mainTimeoutMs: CONTROL_READING_TIMEOUT_MS });
} catch (error) {
@@ -28894,6 +28910,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return read;
}
+ // Waits for the reads of these texts that a background publication
+ // prefetch has in flight (_socialPublicationPrefetchReadings), so a click
+ // joins them instead of asking a model the same question again. The wait
+ // is capped: a read that does not settle in time is read again here.
+ async _joinBackgroundPageTextReads(kind, texts) {
+ const reading = this._socialPublicationPrefetchReading;
+ if (!reading?.size) return;
+ const pending = [...new Set(texts.map(text => reading.get(kind + '\u0000' + text)).filter(Boolean))];
+ if (!pending.length) return;
+ let timer = null;
+ await Promise.race([Promise.allSettled(pending),
+ new Promise(resolve => { timer = setTimeout(resolve, 2 * CONTROL_READING_TIMEOUT_MS); })]);
+ clearTimeout(timer);
+ }
+
// The recipient guard's check in the page, with the agent's readings of the
// messaging page's texts. Texts it consulted unread are read, and the check
// runs again with them; a check that then reaches further texts (a send
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 72dcb03c7..b04274330 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -22217,15 +22217,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const list = [...new Set((Array.isArray(captions) ? captions : [])
.map(caption => String(caption ?? '').replace(/\s+/g, ' ').trim())
.filter(caption => caption && caption.length <= 200))];
- // A caption an earlier prefetch is still reading is not asked twice.
- const reading = this._socialPublicationPrefetchReading ||= new Set();
- const unread = kind => list.filter(caption => this._pageTextReading(kind, caption) === null
- && !reading.has(kind + '\u0000' + caption));
- const effects = unread('control_effect').slice(0, 24);
+ // 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.
+ const reading = this._socialPublicationPrefetchReading ||= new Map();
+ const unread = kind => list.filter(caption => this._pageTextReading(kind, caption) === null);
let guarded = false;
try { guarded = getMessageRecipientGuardPolicy(pageUrl)?.verifyActiveRecipient === true; } catch {}
- const controls = guarded ? unread('message_control').slice(0, 48) : [];
- if (!effects.length && !controls.length) return Promise.resolve([]);
+ const unreadEffects = unread('control_effect');
+ const unreadControls = guarded ? unread('message_control') : [];
+ const joined = [...new Set([...unreadEffects.map(caption => reading.get('control_effect\u0000' + caption)),
+ ...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(() => []);
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
@@ -22234,11 +22238,15 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
|| this._socialPublicationCaptionReads.set(guard, new Set()).get(guard)) : null;
captionReads?.add(abort);
const read = (kind, texts, request) => {
- for (const text of texts) reading.add(kind + '\u0000' + text);
- return Promise.resolve().then(request)
- .finally(() => { for (const text of texts) reading.delete(kind + '\u0000' + text); });
+ const pending = Promise.resolve().then(request);
+ for (const text of texts) reading.set(kind + '\u0000' + text, pending);
+ return pending.finally(() => {
+ for (const text of texts) {
+ if (reading.get(kind + '\u0000' + text) === pending) reading.delete(kind + '\u0000' + text);
+ }
+ });
};
- const reads = [];
+ const reads = [...joined];
if (effects.length) reads.push(read('control_effect', effects, () => this._readControlEffects(tabId, effects, { background })));
if (controls.length) {
reads.push(read('message_control', controls, () => this._readPageTexts(tabId, 'message_control', controls, {},
@@ -25908,6 +25916,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
.map(caption => String(caption ?? '').replace(/\s+/g, ' ').trim())
.filter(caption => caption && caption.length <= 200))].slice(0, 24);
if (!list.length) return;
+ // A caption a background prefetch is reading now is joined, not read a
+ // second time: _readPageTexts below asks only what is still unread, and
+ // makes a held reading one of the most recent the submit probe gets.
+ if (!background) await this._joinBackgroundPageTextReads('control_effect', list);
try {
await this._readPageTexts(tabId, 'control_effect', list, {}, { maxChars: 200, mainTimeoutMs: CONTROL_READING_TIMEOUT_MS,
...(background ? { background } : {}) });
@@ -25997,6 +26009,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
let read = false;
for (const [kind, list] of byKind) {
const pending = [...list].slice(0, 48);
+ // Texts a background prefetch is reading now are joined first, so
+ // _readPageTexts asks only what is still unread (and still makes a
+ // held reading one of the most recent the page gets).
+ await this._joinBackgroundPageTextReads(kind, pending);
try {
await this._readPageTexts(tabId, kind, pending, {}, { maxChars: 400, mainTimeoutMs: CONTROL_READING_TIMEOUT_MS });
} catch (error) {
@@ -26007,6 +26023,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return read;
}
+ // Waits for the reads of these texts that a background publication
+ // prefetch has in flight (_socialPublicationPrefetchReadings), so a click
+ // joins them instead of asking a model the same question again. The wait
+ // is capped: a read that does not settle in time is read again here.
+ async _joinBackgroundPageTextReads(kind, texts) {
+ const reading = this._socialPublicationPrefetchReading;
+ if (!reading?.size) return;
+ const pending = [...new Set(texts.map(text => reading.get(kind + '\u0000' + text)).filter(Boolean))];
+ if (!pending.length) return;
+ let timer = null;
+ await Promise.race([Promise.allSettled(pending),
+ new Promise(resolve => { timer = setTimeout(resolve, 2 * CONTROL_READING_TIMEOUT_MS); })]);
+ clearTimeout(timer);
+ }
+
// The recipient guard's check in the page, with the agent's readings of the
// messaging page's texts. Texts it consulted unread are read, and the check
// runs again with them; a check that then reaches further texts (a send
diff --git a/test/publication-prefetch.mjs b/test/publication-prefetch.mjs
index b3d85c5ff..728526a1d 100644
--- a/test/publication-prefetch.mjs
+++ b/test/publication-prefetch.mjs
@@ -982,6 +982,51 @@ for (const browser of ['chrome', 'firefox']) {
for (const reading of f.readings) assert.deepEqual(reading.texts, ['Post', 'Close', 'Choose audience']);
});
+ test(`${browser}: a caption read in flight is joined by a later prefetch and by the click, never asked twice`, async () => {
+ const f = setup();
+ const { agent, tabId } = f;
+ agent._runAbortStates.set(tabId, { controller: new AbortController(), dispose() {} });
+ f.detected.publicationPrefetchCaptions = ['Post'];
+ // A model's answers, stored when a read settles; reads stay in flight
+ // until released. As in _readPageTexts, a text read before is not asked
+ // of a model again.
+ const answers = new Map();
+ agent._pageTextReading = (kind, text) => answers.get(`${kind}:${text}`) ?? null;
+ const reads = [];
+ agent._readPageTexts = (_tabId, kind, texts, _context, options = {}) => {
+ const unread = texts.filter(text => !answers.has(`${kind}:${text}`));
+ if (!unread.length) return Promise.resolve();
+ return new Promise(resolve => {
+ reads.push({ kind, background: !!options.background, release: () => {
+ for (const text of unread) answers.set(`${kind}:${text}`, 'publish');
+ resolve();
+ } });
+ });
+ };
+ const first = f.write();
+ await until(() => reads.length === 2, 'the first prefetch reads the caption');
+ assert.deepEqual(reads.map(read => [read.kind, read.background]).sort(),
+ [['control_effect', true], ['message_control', true]]);
+ // A second prefetch, and the click's submit probe and recipient guard,
+ // join the reads in flight instead of asking a model again.
+ const settled = { second: false, effect: false, control: false };
+ const second = f.write({ ref_id: 'ref_7', text: 'Hello', clear: true }).then(value => { settled.second = true; return value; });
+ const effect = agent._readControlEffects(tabId, ['Post']).then(() => { settled.effect = true; });
+ const control = agent._readUnreadPageTexts(tabId, [{ kind: 'message_control', text: 'Post' }])
+ .then(value => { settled.control = true; return value; });
+ await settle();
+ assert.equal(reads.length, 2, 'nothing was asked twice');
+ assert.deepEqual(settled, { second: false, effect: false, control: false }, 'each waits for the read in flight');
+ for (const read of reads) read.release();
+ await within(effect, 'the click has its caption reading');
+ assert.equal(await within(control, 'the recipient guard has its reading'), true);
+ await within(first, 'the first prefetch ends');
+ await within(second, 'the second prefetch ends');
+ assert.equal(reads.length, 2, 'still nothing was asked twice');
+ assert.equal(agent._controlEffectReading('Post'), 'publish');
+ assert.equal(agent._socialPublicationPrefetchReading?.size || 0, 0, '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;
diff --git a/test/run.js b/test/run.js
index 22c5c0b98..e1c299ff0 100644
--- a/test/run.js
+++ b/test/run.js
@@ -18526,7 +18526,8 @@ test('a reading older than the ones handed to the page still reaches it when the
const wall = 'Bu içerik abonelere özeldir Okumaya devam etmek için abone olun. Abone ol';
const agent = new AgentClass({});
const asked = stubPageTextReadings(agent, (kind, item) => (kind === 'page_blocker'
- ? (item.text === wall ? 'subscription' : 'other') : kind === 'message_control' ? 'other' : null));
+ ? (item.text === wall ? 'subscription' : 'other') : kind === 'message_control' ? 'other'
+ : kind === 'control_effect' ? (item.text === 'Paylaş' ? 'publish' : 'view') : null));
await agent._readPageTexts(1, 'page_blocker', [wall], {}, { maxChars: 400 });
// Many texts read since push it out of the readings a page read carries.
await agent._readPageTexts(1, 'page_blocker', Array.from({ length: 450 }, (_, index) => `Bülten kutusu ${index}`), {}, { maxChars: 400 });
@@ -18550,6 +18551,14 @@ test('a reading older than the ones handed to the page still reaches it when the
assert.equal(agent._messagingPageReadings().message_control['Gönder'], undefined);
assert.equal(await agent._readUnreadPageTexts(1, [{ kind: 'message_control', text: 'Gönder' }]), true);
assert.equal(agent._messagingPageReadings().message_control['Gönder'], 'other', `${build}: a held control reading did not reach the page`);
+ // So does what a clicked control does, which the submit probe reads.
+ await agent._readPageTexts(1, 'control_effect', ['Paylaş'], {}, { maxChars: 200 });
+ await agent._readPageTexts(1, 'control_effect', Array.from({ length: 450 }, (_, index) => `Sekme ${index}`), {}, { maxChars: 200 });
+ assert.equal(agent._controlEffectReadingsForProbe()['Paylaş'], undefined);
+ asked.length = 0;
+ await agent._readControlEffects(1, ['Paylaş']);
+ assert.equal(agent._controlEffectReadingsForProbe()['Paylaş'], 'publish', `${build}: a held caption reading did not reach the submit probe`);
+ assert.deepEqual(asked, [], `${build}: a held caption reading was asked of a model again`);
// Overlay and article-block texts stay out of the dialog-title readings
// the CAPTCHA scans carry, which are at most 200 characters.
const window = `${'Önizleme paragrafı. '.repeat(12)}Okumaya devam etmek için abone olun.`;