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
Stabilize X message history baselines
Emre Sokullu24f80a3054f5
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.jsindex 238b73b33..d4c5af1d0 100644--- a/src/chrome/src/content/content.js+++ b/src/chrome/src/content/content.js@@ -4416,35 +4416,47 @@}const _messageRecipientDispatchBindings = new Map();- const _twitterEmptyLogObservations = new WeakMap();+ const _twitterHistoryObservations = new WeakMap();- function _settledEmptyTwitterLog(log) {+ function _settledTwitterHistory(log, messageIds = []) {if (!log?.isConnected) return false;let loading = false;- let historyComplete = false;+ let completionSignal = false;try {const ariaBusy = log.getAttribute('aria-busy');loading = ariaBusy === 'true'|| !!log.querySelector('[aria-busy="true"],[role="progressbar"]');- // An empty log alone is ambiguous: history may still mount after the- // submit. Require X to expose either its finished busy state or its- // app-owned empty-state marker before pinning a first-message baseline.- historyComplete = ariaBusy === 'false'- || !!log.querySelector('[data-testid="dm-empty-state"],[data-testid="empty_state"],[data-testid="empty-state"]');+ // X's explicit idle state is a direct history-completion signal. Empty+ // logs may alternatively use X's own empty-state marker.+ completionSignal = ariaBusy === 'false'+ || (!(Array.isArray(messageIds) && messageIds.length > 0)+ && !!log.querySelector('[data-testid="dm-empty-state"],[data-testid="empty_state"],[data-testid="empty-state"]'));} catch {}- if (loading || !historyComplete) {- _twitterEmptyLogObservations.delete(log);+ if (loading) {+ _twitterHistoryObservations.delete(log);+ return false;+ }+ if (completionSignal) {+ _twitterHistoryObservations.delete(log);+ return true;+ }+ // Without an explicit X completion marker, an empty history remains+ // ambiguous. A nonempty history must retain the same row identities long+ // enough to rule out an in-progress history append.+ if (!Array.isArray(messageIds) || messageIds.length === 0) {+ _twitterHistoryObservations.delete(log);return false;}const now = Date.now();- const prior = _twitterEmptyLogObservations.get(log);- const observation = prior+ const signature = Array.isArray(messageIds) ? messageIds.join('\u001f') : '';+ const prior = _twitterHistoryObservations.get(log);+ const observation = prior?.signature === signature? { firstSeenAt: prior.firstSeenAt, count: prior.count + 1 }- : { firstSeenAt: now, count: 1 };- _twitterEmptyLogObservations.set(log, observation);- // A first-message conversation has no historic tail to pin. Require the- // same positively-complete empty log to survive two reads before treating- // that exceptional baseline as settled.+ : { firstSeenAt: now, count: 1, signature };+ _twitterHistoryObservations.set(log, { ...observation, signature });+ // A nonempty history without an explicit idle marker can append old+ // messages. Require the same snapshot to survive two reads before using+ // it as a dispatch baseline.return observation.count >= 2 && now - observation.firstSeenAt >= 300;}@@ -5088,15 +5100,15 @@.filter(row => /^message-(?!text-)[a-zA-Z0-9_-]{1,128}$/.test(row.getAttribute('data-testid') || '')): [];const twitterMessageIds = twitterRows.map(row => row.getAttribute('data-testid'));- const twitterEmptyConversationBaseline = twitterLogs.length === 1 && twitterRows.length === 0- && _settledEmptyTwitterLog(twitterLogs[0]);- if (twitterRows.length > 0 && twitterLogs[0]) _twitterEmptyLogObservations.delete(twitterLogs[0]);- // An empty mounted log can mean X is still loading older history. A- // first-message conversation is eligible only after the same non-loading- // empty log has settled across repeated observations.+ const twitterHistorySettled = twitterLogs.length === 1+ && _settledTwitterHistory(twitterLogs[0], twitterMessageIds);+ const twitterEmptyConversationBaseline = twitterRows.length === 0 && twitterHistorySettled;+ // A mounted X log can still append older history. Empty conversations+ // need a positive X completion signal; other histories need that signal+ // or a stable row snapshot before they become dispatch baselines.const twitterBaselineComplete = twitterLogs.length === 1 && twitterRows.length <= 2000&& new Set(twitterMessageIds).size === twitterMessageIds.length- && (twitterRows.length > 0 || twitterEmptyConversationBaseline);+ && (twitterRows.length > 0 ? twitterHistorySettled : twitterEmptyConversationBaseline);const matchingTwitterMessageIds = expectedBody => {const expected = normalizedMessageBody(expectedBody);if (!expected || !twitterBaselineComplete) return [];diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.jsindex a7243b34b..ccfac1b82 100644--- a/src/firefox/src/content/content.js+++ b/src/firefox/src/content/content.js@@ -3728,35 +3728,47 @@}const _messageRecipientDispatchBindings = new Map();- const _twitterEmptyLogObservations = new WeakMap();+ const _twitterHistoryObservations = new WeakMap();- function _settledEmptyTwitterLog(log) {+ function _settledTwitterHistory(log, messageIds = []) {if (!log?.isConnected) return false;let loading = false;- let historyComplete = false;+ let completionSignal = false;try {const ariaBusy = log.getAttribute('aria-busy');loading = ariaBusy === 'true'|| !!log.querySelector('[aria-busy="true"],[role="progressbar"]');- // An empty log alone is ambiguous: history may still mount after the- // submit. Require X to expose either its finished busy state or its- // app-owned empty-state marker before pinning a first-message baseline.- historyComplete = ariaBusy === 'false'- || !!log.querySelector('[data-testid="dm-empty-state"],[data-testid="empty_state"],[data-testid="empty-state"]');+ // X's explicit idle state is a direct history-completion signal. Empty+ // logs may alternatively use X's own empty-state marker.+ completionSignal = ariaBusy === 'false'+ || (!(Array.isArray(messageIds) && messageIds.length > 0)+ && !!log.querySelector('[data-testid="dm-empty-state"],[data-testid="empty_state"],[data-testid="empty-state"]'));} catch {}- if (loading || !historyComplete) {- _twitterEmptyLogObservations.delete(log);+ if (loading) {+ _twitterHistoryObservations.delete(log);+ return false;+ }+ if (completionSignal) {+ _twitterHistoryObservations.delete(log);+ return true;+ }+ // Without an explicit X completion marker, an empty history remains+ // ambiguous. A nonempty history must retain the same row identities long+ // enough to rule out an in-progress history append.+ if (!Array.isArray(messageIds) || messageIds.length === 0) {+ _twitterHistoryObservations.delete(log);return false;}const now = Date.now();- const prior = _twitterEmptyLogObservations.get(log);- const observation = prior+ const signature = Array.isArray(messageIds) ? messageIds.join('\u001f') : '';+ const prior = _twitterHistoryObservations.get(log);+ const observation = prior?.signature === signature? { firstSeenAt: prior.firstSeenAt, count: prior.count + 1 }- : { firstSeenAt: now, count: 1 };- _twitterEmptyLogObservations.set(log, observation);- // A first-message conversation has no historic tail to pin. Require the- // same positively-complete empty log to survive two reads before treating- // that exceptional baseline as settled.+ : { firstSeenAt: now, count: 1, signature };+ _twitterHistoryObservations.set(log, { ...observation, signature });+ // A nonempty history without an explicit idle marker can append old+ // messages. Require the same snapshot to survive two reads before using+ // it as a dispatch baseline.return observation.count >= 2 && now - observation.firstSeenAt >= 300;}@@ -4260,15 +4272,15 @@.filter(row => /^message-(?!text-)[a-zA-Z0-9_-]{1,128}$/.test(row.getAttribute('data-testid') || '')): [];const twitterMessageIds = twitterRows.map(row => row.getAttribute('data-testid'));- const twitterEmptyConversationBaseline = twitterLogs.length === 1 && twitterRows.length === 0- && _settledEmptyTwitterLog(twitterLogs[0]);- if (twitterRows.length > 0 && twitterLogs[0]) _twitterEmptyLogObservations.delete(twitterLogs[0]);- // An empty mounted log can mean X is still loading older history. A- // first-message conversation is eligible only after the same non-loading- // empty log has settled across repeated observations.+ const twitterHistorySettled = twitterLogs.length === 1+ && _settledTwitterHistory(twitterLogs[0], twitterMessageIds);+ const twitterEmptyConversationBaseline = twitterRows.length === 0 && twitterHistorySettled;+ // A mounted X log can still append older history. Empty conversations+ // need a positive X completion signal; other histories need that signal+ // or a stable row snapshot before they become dispatch baselines.const twitterBaselineComplete = twitterLogs.length === 1 && twitterRows.length <= 2000&& new Set(twitterMessageIds).size === twitterMessageIds.length- && (twitterRows.length > 0 || twitterEmptyConversationBaseline);+ && (twitterRows.length > 0 ? twitterHistorySettled : twitterEmptyConversationBaseline);const matchingTwitterMessageIds = expectedBody => {const expected = normalizedMessageBody(expectedBody);if (!expected || !twitterBaselineComplete) return [];diff --git a/test/fixtures/message-recipient-navigation.mjs b/test/fixtures/message-recipient-navigation.mjsindex ecb412330..546b793c6 100644--- a/test/fixtures/message-recipient-navigation.mjs+++ b/test/fixtures/message-recipient-navigation.mjs@@ -62,7 +62,7 @@ export function registerMessageRecipientNavigationFixtures({#send {position:fixed;left:820px;bottom:20px}[role=log] {position:fixed;left:400px;top:100px;width:400px;height:300px;overflow:auto}</style><a id="header" href="https://x.com/altryne"><div data-testid="dm-conversation-username">Alex Volkov</div></a>- <div role="log" data-testid="dm-message-scroller"></div>+ <div role="log" data-testid="dm-message-scroller" aria-busy="false"></div><textarea data-testid="dm-composer-textarea" aria-label="Unencrypted message"></textarea><button id="send" type="button">Send</button>`, kind);const body = 'Hey Alex, check out webbrain.one.\nHappy to share a demo.';@@ -176,7 +176,8 @@ export function registerMessageRecipientNavigationFixtures({delete submit.workflowBinding.preDispatchMessageIds;assert.equal(evidence(probe),null, 'a legacy count-only binding cannot prove an X send');submit.workflowBinding.preDispatchMessageIds = [];- assert.equal(evidence(probe)?.verificationKind, 'message_sent', 'a first message has an empty baseline');+ assert.equal(evidence(probe), null,+ 'relabeling a nonempty X history as empty cannot fabricate a first-message baseline');await addRow('new');assert.equal(evidence(await observe()),null, 'duplicate message identities fail closed');});@@ -191,7 +192,7 @@ export function registerMessageRecipientNavigationFixtures({#send {position:fixed;left:820px;bottom:20px}[role=log] {position:fixed;left:400px;top:100px;width:400px;height:300px}</style><a id="header" href="/altryne"><span data-testid="dm-conversation-username">Alex Volkov</span></a>- <div role="log" data-testid="dm-message-scroller"></div>+ <div role="log" data-testid="dm-message-scroller" aria-busy="false"></div><textarea data-testid="dm-composer-textarea"></textarea><button id="send" type="button">Send</button>`, kind);await page.evaluate(() => {window.fixtureSends = 0;@@ -404,9 +405,17 @@ export function registerMessageRecipientNavigationFixtures({await page.evaluate(modal=>{document.querySelector('main').innerHTML=`<section id="public" ${modal?'role="dialog" aria-modal="true"':''}><div role="button">Post to Anyone</div><div contenteditable="true" role="textbox">Announcement</div>- <button id="publish" type="button"><span>Post</span></button></section>`;+ <button id="publish" type="button" data-control-name="share.post"><span>Post</span></button></section>`;document.getElementById('chat').hidden=true;},modal);+ const publishWorkflow = agent._resolvePlannerSiteWorkflow(page.url(), {+ request_kind: 'execute', site_job: 'publish-post', requires_submission: true,+ });+ assert.equal(publishWorkflow?.job?.id, 'publish-post');+ agent._startPlanExecutionGuard(1, 'act', {+ requestKind: 'execute', requiresStateChange: true, requiresSubmission: true,+ siteWorkflow: publishWorkflow,+ });const target=await page.evaluate(()=>{const span=document.querySelector('#publish span'),rect=span.getBoundingClientRect();return {ref_id:window.__wb_ax_ref(span),x:rect.x+2,y:rect.y+2};@@ -429,7 +438,7 @@ export function registerMessageRecipientNavigationFixtures({document.getElementById('send').textContent='Post';});assert.equal((await guard('click',{selector:'#send'}))?.noDispatch,true);- assert.equal(agent._planExecutionGuards.get(1)?.messaging,undefined);+ assert.equal(agent._planExecutionGuards.get(1)?.messaging,null);}}});diff --git a/test/run.js b/test/run.jsindex 0dbeda523..6dedec4c6 100644--- a/test/run.js+++ b/test/run.js@@ -7902,9 +7902,9 @@ test('empty X history baselines require a positive completion signal', () => {: '\n\n function _releaseDispatchBinding', start);assert.ok(start >= 0 && end > start, `${label}: empty-X helper should remain independently testable`);let now = 0;- const { settledEmptyTwitterLog } = vm.runInNewContext(`(() => {+ const { settledTwitterHistory } = vm.runInNewContext(`(() => {${source.slice(start, end)}- return { settledEmptyTwitterLog: _settledEmptyTwitterLog };+ return { settledTwitterHistory: _settledTwitterHistory };})()`, {Date: { now: () => now },});@@ -7917,25 +7917,37 @@ test('empty X history baselines require a positive completion signal', () => {},});const ambiguousEmptyLog = log();- assert.equal(settledEmptyTwitterLog(ambiguousEmptyLog), false,+ assert.equal(settledTwitterHistory(ambiguousEmptyLog), false,`${label}: a bare empty X log was accepted before history completion`);now += 300;- assert.equal(settledEmptyTwitterLog(ambiguousEmptyLog), false,+ assert.equal(settledTwitterHistory(ambiguousEmptyLog), false,`${label}: a bare empty X log became a first-message baseline`);const completedEmptyLog = log({ ariaBusy: 'false' });- assert.equal(settledEmptyTwitterLog(completedEmptyLog), false,- `${label}: the first completed empty-X observation was accepted`);- now += 300;- assert.equal(settledEmptyTwitterLog(completedEmptyLog), true,- `${label}: a stable aria-busy=false X log did not become a baseline`);+ assert.equal(settledTwitterHistory(completedEmptyLog), true,+ `${label}: an explicit aria-busy=false X empty log did not become a baseline`);const markedEmptyLog = log({ emptyState: true });- assert.equal(settledEmptyTwitterLog(markedEmptyLog), false,- `${label}: the first explicit X empty-state observation was accepted`);+ assert.equal(settledTwitterHistory(markedEmptyLog), true,+ `${label}: an explicit X empty-state marker did not become a baseline`);++ const loadingHistory = log({ ariaBusy: 'true' });+ assert.equal(settledTwitterHistory(loadingHistory, ['message-existing']), false,+ `${label}: an explicitly loading X history became a baseline`);++ const completedHistory = log({ ariaBusy: 'false' });+ assert.equal(settledTwitterHistory(completedHistory, ['message-existing']), true,+ `${label}: an explicit aria-busy=false X history did not become a baseline`);++ const unmarkedHistory = log();+ assert.equal(settledTwitterHistory(unmarkedHistory, ['message-existing']), false,+ `${label}: the first unmarked X history observation was accepted`);+ now += 300;+ assert.equal(settledTwitterHistory(unmarkedHistory, ['message-existing']), true,+ `${label}: a stable unmarked X history did not become a baseline`);now += 300;- assert.equal(settledEmptyTwitterLog(markedEmptyLog), true,- `${label}: a stable X empty-state marker did not become a baseline`);+ assert.equal(settledTwitterHistory(unmarkedHistory, ['message-existing', 'message-late']), false,+ `${label}: an appended historical X row kept the previous baseline`);}});