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 only usable, rendered timestamps and count prior text with its block separators
Claudebe3cc93019e6
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex f3b877802..247356af6 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -33930,14 +33930,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// white-space as styled), with each <time datetime> in it standing for its// datetime, so an edit that only changes the presentation of the same text// is still another version of the post.+ // A timestamp stands for its datetime only when it has a usable one: not+ // empty and short, or the rendered text stays what binds the post.+ const stampOf = element => element?.matches?.('time[datetime]')+ && (() => { const value = String(element.getAttribute('datetime') || '').trim(); return value.length > 0 && value.length <= 64; })();+ const inStamp = node => { for (let up = node.parentElement; up; up = up.parentElement) if (stampOf(up)) return true; return false; };const stableText = node => {- const marker = time => `\u0000${time.getAttribute('datetime')}\u0000`;- if (node.matches?.('time[datetime]')) return marker(node);+ const marker = time => `\u0000${time.getAttribute('datetime').trim()}\u0000`;const rendered = element => (typeof element.innerText === 'string' ? element.innerText : element.textContent) || '';+ if (stampOf(node)) return marker(node);let text = rendered(node);let cursor = 0;+ // Only a timestamp the page renders: a hidden one is not in the text.const times = [...node.querySelectorAll?.('time[datetime]') || []]- .filter(time => !time.parentElement?.closest('time[datetime]'));+ .filter(time => stampOf(time) && !inStamp(time)+ && (typeof time.checkVisibility !== 'function' || time.checkVisibility({ visibilityProperty: true })));// Beyond this many timestamps the text is left as it is rendered: it// still binds the post, and a ticking clock only makes it refuse.if (times.length > 16) return text;@@ -33960,7 +33967,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (time.compareDocumentPosition(part) & 4) break;if (previous && (!(previous.compareDocumentPosition(part) & 4) || previous.compareDocumentPosition(part) & 16)) continue;const owner = part.parentElement;- if (owner && (typeof owner.checkVisibility !== 'function' || owner.checkVisibility({ visibilityProperty: true }))) between += part.data;+ if (owner && (typeof owner.checkVisibility !== 'function' || owner.checkVisibility({ visibilityProperty: true }))) {+ // A block box separates its text from the text around it.+ const display = node.ownerDocument.defaultView.getComputedStyle(owner).display;+ const separator = display === 'inline' || display === 'contents' ? '' : ' ';+ between += separator + part.data + separator;+ }}skip = (between.match(pattern()) || []).length;} catch { skip = 0; }@@ -33982,7 +33994,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// A node inside a <time datetime> is part of the timestamp that node already// stands for, so it adds no text of its own (<time><span>8 min</span></time>).const stableTexts = nodes => [...new Set(nodes- .filter(node => !node.parentElement?.closest('time[datetime]'))+ .filter(node => !inStamp(node)).map(stableText).filter(value => value.length <= 25000))];const nativeResourceProofs = () => {if (args.__wbNativeResourceAdapter !== 'mastodon') return [];diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex d2c91e8dd..62c8b761c 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -30798,14 +30798,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// white-space as styled), with each <time datetime> in it standing for its// datetime, so an edit that only changes the presentation of the same text// is still another version of the post.+ // A timestamp stands for its datetime only when it has a usable one: not+ // empty and short, or the rendered text stays what binds the post.+ const stampOf = element => element?.matches?.('time[datetime]')+ && (() => { const value = String(element.getAttribute('datetime') || '').trim(); return value.length > 0 && value.length <= 64; })();+ const inStamp = node => { for (let up = node.parentElement; up; up = up.parentElement) if (stampOf(up)) return true; return false; };const stableText = node => {- const marker = time => `\u0000${time.getAttribute('datetime')}\u0000`;- if (node.matches?.('time[datetime]')) return marker(node);+ const marker = time => `\u0000${time.getAttribute('datetime').trim()}\u0000`;const rendered = element => (typeof element.innerText === 'string' ? element.innerText : element.textContent) || '';+ if (stampOf(node)) return marker(node);let text = rendered(node);let cursor = 0;+ // Only a timestamp the page renders: a hidden one is not in the text.const times = [...node.querySelectorAll?.('time[datetime]') || []]- .filter(time => !time.parentElement?.closest('time[datetime]'));+ .filter(time => stampOf(time) && !inStamp(time)+ && (typeof time.checkVisibility !== 'function' || time.checkVisibility({ visibilityProperty: true })));// Beyond this many timestamps the text is left as it is rendered: it// still binds the post, and a ticking clock only makes it refuse.if (times.length > 16) return text;@@ -30828,7 +30835,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dif (time.compareDocumentPosition(part) & 4) break;if (previous && (!(previous.compareDocumentPosition(part) & 4) || previous.compareDocumentPosition(part) & 16)) continue;const owner = part.parentElement;- if (owner && (typeof owner.checkVisibility !== 'function' || owner.checkVisibility({ visibilityProperty: true }))) between += part.data;+ if (owner && (typeof owner.checkVisibility !== 'function' || owner.checkVisibility({ visibilityProperty: true }))) {+ // A block box separates its text from the text around it.+ const display = node.ownerDocument.defaultView.getComputedStyle(owner).display;+ const separator = display === 'inline' || display === 'contents' ? '' : ' ';+ between += separator + part.data + separator;+ }}skip = (between.match(pattern()) || []).length;} catch { skip = 0; }@@ -30850,7 +30862,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// A node inside a <time datetime> is part of the timestamp that node already// stands for, so it adds no text of its own (<time><span>8 min</span></time>).const stableTexts = nodes => [...new Set(nodes- .filter(node => !node.parentElement?.closest('time[datetime]'))+ .filter(node => !inStamp(node)).map(stableText).filter(value => value.length <= 25000))];const nativeResourceProofs = () => {if (args.__wbNativeResourceAdapter !== 'mastodon') return [];diff --git a/test/native-resource-deletion-binding.mjs b/test/native-resource-deletion-binding.mjsindex 36055341a..5019b0af5 100644--- a/test/native-resource-deletion-binding.mjs+++ b/test/native-resource-deletion-binding.mjs@@ -426,6 +426,51 @@ for (const build of ['chrome', 'firefox']) {} finally { await f.page.close(); }});+ await t.test('a timestamp without a usable datetime keeps its rendered text in the key, and a hidden one is not the visible one', async () => {+ const f = await fixture();+ try {+ const request = `Delete only ${resource}.`;+ const provider = { chat() {} }, agent = new Agent({ getActive: () => provider });+ agent.conversations.set(1, [{ role: 'system', content: 'system' }, { role: 'user', content: request }]);+ agent._startPlanExecutionGuard(1, 'act', { requestKind: 'execute', requiresStateChange: true, requiresSubmission: true });+ await f.page.evaluate(() => {+ document.querySelector('#record-200 .status__relative-time').innerHTML+ = '<time datetime="">8 dk</time>';+ });+ await f.showConfirmation();+ const detected = await f.probe('#confirm');+ const key = found => agent._resourceMutationActionKey(1, 'click', { selector: '#confirm' }, found);+ await f.page.evaluate(() => { document.querySelector('#record-200 .status__relative-time time').textContent = '9 dk'; });+ assert.notEqual(key(await f.probe('#confirm')), key(detected), 'Without a datetime the shown time is part of the post');+ await f.page.evaluate(() => {+ document.querySelector('#record-200 .status__relative-time').innerHTML+ = '<time datetime="2026-10-18T10:00:00.000Z" style="display:none">8 dk</time><time datetime="2026-10-18T19:22:00.000Z">8 dk</time>';+ });+ const hidden = await f.probe('#confirm');+ await f.page.evaluate(() => { document.querySelectorAll('#record-200 .status__relative-time time')[1].textContent = '9 dk'; });+ assert.equal(key(await f.probe('#confirm')), key(hidden), 'The visible timestamp stands for its own datetime');+ } finally { await f.page.close(); }+ });++ await t.test('text that layout separates into lines like the relative time does not take the timestamp\'s place', async () => {+ const f = await fixture();+ try {+ const request = `Delete only ${resource}.`;+ const provider = { chat() {} }, agent = new Agent({ getActive: () => provider });+ agent.conversations.set(1, [{ role: 'system', content: 'system' }, { role: 'user', content: request }]);+ agent._startPlanExecutionGuard(1, 'act', { requestKind: 'execute', requiresStateChange: true, requiresSubmission: true });+ await f.page.evaluate(() => {+ document.querySelector('#record-200 .status__relative-time').innerHTML+ = '<div>8</div><div>dk</div><time datetime="2026-10-18T19:22:00.000Z" title="18 Eki 2026 22:22">8 dk</time>';+ });+ await f.showConfirmation();+ const detected = await f.probe('#confirm');+ const key = found => agent._resourceMutationActionKey(1, 'click', { selector: '#confirm' }, found);+ await f.page.evaluate(() => { document.querySelector('#record-200 .status__relative-time time').textContent = '9 dk'; });+ assert.equal(key(await f.probe('#confirm')), key(detected), 'Only the timestamp ticked');+ } finally { await f.page.close(); }+ });+await t.test('hidden text equal to the relative time does not hide the timestamp from the key', async () => {const f = await fixture();try {