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: a prefetch defers to the click's own audit
Barack Sokullu550ecfe21b6a
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 5b3e88940..dd919e453 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -24494,26 +24494,37 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (social.dispatch?.key !== key) {if (!bypassMatches) {const auditStarted = Date.now();- // A background prefetch audited this exact key (or still is): its- // verdict decides once, exactly as this audit would have. Any other- // key, or no verdict, runs the audit now.- const prefetched = await this._socialPublicationPrefetchedVerdict(tabId, guard, social, key, taskKey);- if (prefetched?.changed) return blocked('Publication context changed during authorization.');- if (prefetched) {- this._predispatchStage(tabId, prefetched.awaited ? 'publicationAuditAwaitedMs' : 'publicationAuditReusedMs', Date.now() - auditStarted);- if (!prefetched.authorized) {- social.deniedAuditKey = key;- social.deniedAudit = { status: 'denied', reason: prefetched.reason };- return auditBlocked();+ // The click decides this draft's audit from here on: a background+ // prefetch of the same draft that reaches its own check meanwhile+ // defers to it instead of sending a second request+ // (_socialPublicationPrefetchAudit).+ const foreground = this._socialPublicationForegroundAudits ||= new WeakMap();+ const auditing = { key };+ foreground.set(social, auditing);+ try {+ // A background prefetch audited this exact key (or still is): its+ // verdict decides once, exactly as this audit would have. Any other+ // key, or no verdict, runs the audit now.+ const prefetched = await this._socialPublicationPrefetchedVerdict(tabId, guard, social, key, taskKey);+ if (prefetched?.changed) return blocked('Publication context changed during authorization.');+ if (prefetched) {+ this._predispatchStage(tabId, prefetched.awaited ? 'publicationAuditAwaitedMs' : 'publicationAuditReusedMs', Date.now() - auditStarted);+ if (!prefetched.authorized) {+ social.deniedAuditKey = key;+ social.deniedAudit = { status: 'denied', reason: prefetched.reason };+ return auditBlocked();+ }+ delete social.deniedAudit;+ delete social.deniedAuditKey;+ } else {+ const outcome = await this._socialPublicationAuditRun(tabId, provider || this._activeProvider(tabId),+ guard, social, action, snapshot, key, taskKey, { record: true });+ this._predispatchStage(tabId, 'publicationAuditMs', Date.now() - auditStarted);+ if (outcome.status === 'changed') return blocked('Publication context changed during authorization.');+ if (outcome.status !== 'authorized') return auditBlocked();}- delete social.deniedAudit;- delete social.deniedAuditKey;- } else {- const outcome = await this._socialPublicationAuditRun(tabId, provider || this._activeProvider(tabId),- guard, social, action, snapshot, key, taskKey, { record: true });- this._predispatchStage(tabId, 'publicationAuditMs', Date.now() - auditStarted);- if (outcome.status === 'changed') return blocked('Publication context changed during authorization.');- if (outcome.status !== 'authorized') return auditBlocked();+ } finally {+ if (foreground.get(social) === auditing) foreground.delete(social);}}social.dispatch = { key, actionId: action.id, snapshot: JSON.parse(JSON.stringify(snapshot)), pageUrl,@@ -24779,6 +24790,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst frameId = Number.isInteger(detected.frameId) ? detected.frameId : 0;const key = this._sha256TextSync(JSON.stringify({ contract: social.key, action, snapshot, pageUrl, documentIdentity, frameId }));if (social.dispatch?.key === key || social.deniedAuditKey === key || social.prefetchedAudit?.key === key) return 'cached';+ // The click is already deciding this draft's audit itself: its verdict+ // decides, and a second request would bill the same check twice.+ if (this._socialPublicationForegroundAudits?.get(social)?.key === key) return 'cached';const audits = this._socialPublicationAuditPrefetches ||= new WeakMap();const running = audits.get(social);// The same draft is already being audited for this run: join it. Andiff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 602ad7fb3..87f67bc5a 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -21677,26 +21677,37 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (social.dispatch?.key !== key) {if (!bypassMatches) {const auditStarted = Date.now();- // A background prefetch audited this exact key (or still is): its- // verdict decides once, exactly as this audit would have. Any other- // key, or no verdict, runs the audit now.- const prefetched = await this._socialPublicationPrefetchedVerdict(tabId, guard, social, key, taskKey);- if (prefetched?.changed) return blocked('Publication context changed during authorization.');- if (prefetched) {- this._predispatchStage(tabId, prefetched.awaited ? 'publicationAuditAwaitedMs' : 'publicationAuditReusedMs', Date.now() - auditStarted);- if (!prefetched.authorized) {- social.deniedAuditKey = key;- social.deniedAudit = { status: 'denied', reason: prefetched.reason };- return auditBlocked();+ // The click decides this draft's audit from here on: a background+ // prefetch of the same draft that reaches its own check meanwhile+ // defers to it instead of sending a second request+ // (_socialPublicationPrefetchAudit).+ const foreground = this._socialPublicationForegroundAudits ||= new WeakMap();+ const auditing = { key };+ foreground.set(social, auditing);+ try {+ // A background prefetch audited this exact key (or still is): its+ // verdict decides once, exactly as this audit would have. Any other+ // key, or no verdict, runs the audit now.+ const prefetched = await this._socialPublicationPrefetchedVerdict(tabId, guard, social, key, taskKey);+ if (prefetched?.changed) return blocked('Publication context changed during authorization.');+ if (prefetched) {+ this._predispatchStage(tabId, prefetched.awaited ? 'publicationAuditAwaitedMs' : 'publicationAuditReusedMs', Date.now() - auditStarted);+ if (!prefetched.authorized) {+ social.deniedAuditKey = key;+ social.deniedAudit = { status: 'denied', reason: prefetched.reason };+ return auditBlocked();+ }+ delete social.deniedAudit;+ delete social.deniedAuditKey;+ } else {+ const outcome = await this._socialPublicationAuditRun(tabId, provider || this._activeProvider(tabId),+ guard, social, action, snapshot, key, taskKey, { record: true });+ this._predispatchStage(tabId, 'publicationAuditMs', Date.now() - auditStarted);+ if (outcome.status === 'changed') return blocked('Publication context changed during authorization.');+ if (outcome.status !== 'authorized') return auditBlocked();}- delete social.deniedAudit;- delete social.deniedAuditKey;- } else {- const outcome = await this._socialPublicationAuditRun(tabId, provider || this._activeProvider(tabId),- guard, social, action, snapshot, key, taskKey, { record: true });- this._predispatchStage(tabId, 'publicationAuditMs', Date.now() - auditStarted);- if (outcome.status === 'changed') return blocked('Publication context changed during authorization.');- if (outcome.status !== 'authorized') return auditBlocked();+ } finally {+ if (foreground.get(social) === auditing) foreground.delete(social);}}social.dispatch = { key, actionId: action.id, snapshot: JSON.parse(JSON.stringify(snapshot)), pageUrl,@@ -21962,6 +21973,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst frameId = Number.isInteger(detected.frameId) ? detected.frameId : 0;const key = this._sha256TextSync(JSON.stringify({ contract: social.key, action, snapshot, pageUrl, documentIdentity, frameId }));if (social.dispatch?.key === key || social.deniedAuditKey === key || social.prefetchedAudit?.key === key) return 'cached';+ // The click is already deciding this draft's audit itself: its verdict+ // decides, and a second request would bill the same check twice.+ if (this._socialPublicationForegroundAudits?.get(social)?.key === key) return 'cached';const audits = this._socialPublicationAuditPrefetches ||= new WeakMap();const running = audits.get(social);// The same draft is already being audited for this run: join it. Andiff --git a/test/publication-prefetch.mjs b/test/publication-prefetch.mjsindex 63f409961..5e17283e0 100644--- a/test/publication-prefetch.mjs+++ b/test/publication-prefetch.mjs@@ -630,6 +630,28 @@ for (const browser of ['chrome', 'firefox']) {}});+ 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+ // is still in flight.+ const audit = f.hold('social_publication_authorization');+ const click = f.click();+ await until(() => audit.started, 'the click\'s own audit started');+ try {+ // A write's prefetch, slower than the click, reaches the same draft now.+ assert.equal(await within(f.write(), 'the prefetch does not wait for the click'), 'cached', 'the prefetch defers to the click');+ assert.equal(f.named('social_publication_authorization').length, 1, 'no second checker request for the same draft');+ } finally {+ audit.release();+ }+ assert.equal(await within(click, 'the click finishes'), null);+ assert.equal(f.named('social_publication_authorization').length, 1);+ // Once the click decided, a prefetch of another draft audits as before.+ f.detected.frameDocumentIdentity = 'https://x.com/compose/post|fixture-document-2';+ assert.equal(await f.write(), 'authorized');+ assert.equal(f.named('social_publication_authorization').length, 2);+ });+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');