Skip to content

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

Keep where a post breaks lines in the stabilized resource text

Claude8bdd52bc55d2
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 770393993..90404a3c8 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -33925,22 +33925,43 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// datetime, not as the relative time it renders ("8 min" turns into
// "9 min" by itself while a deletion waits on WebBrain's card), so a
// ticking timestamp does not make the same post another one.
- const stableText = node => {
+ // Where the page breaks a line (a block box, a <br>) is part of what the
+ // text reads as: it is kept as a separator, so an edit that only changes
+ // the layout of the same text nodes is still another version of the post.
+ const stableText = (node, breaks = new WeakMap()) => {
+ const breaksLine = element => {
+ if (element.tagName === 'BR') return true;
+ if (!breaks.has(element)) {
+ let display = 'inline';
+ try { display = doc.defaultView.getComputedStyle(element).display || 'inline'; } catch { /* keep inline */ }
+ breaks.set(element, display !== 'inline' && display !== 'contents' && display !== 'none');
+ }
+ return breaks.get(element);
+ };
let text = '';
const pending = [node];
while (pending.length) {
const current = pending.pop();
- if (current.nodeType === 3) text += current.data;
+ if (typeof current === 'string') text += current;
+ else if (current.nodeType === 3) text += current.data;
else if (current.nodeType === 1 && current.matches('time[datetime]')) text += `\u0000${current.getAttribute('datetime')}\u0000`;
- else if (current.nodeType === 1) for (let index = current.childNodes.length - 1; index >= 0; index -= 1) pending.push(current.childNodes[index]);
+ else if (current.nodeType === 1) {
+ const separator = breaksLine(current) ? '\n' : '';
+ if (separator) pending.push(separator);
+ for (let index = current.childNodes.length - 1; index >= 0; index -= 1) pending.push(current.childNodes[index]);
+ if (separator) text += separator;
+ }
}
return text;
};
// 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]'))
- .map(stableText).filter(value => value.length <= 25000))];
+ const stableTexts = nodes => {
+ const breaks = new WeakMap();
+ return [...new Set(nodes
+ .filter(node => !node.parentElement?.closest('time[datetime]'))
+ .map(node => stableText(node, breaks)).filter(value => value.length <= 25000))];
+ };
const nativeResourceProofs = () => {
if (args.__wbNativeResourceAdapter !== 'mastodon') return [];
const signedIn = nativeSignedInAccount();
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index febd6b348..7d65a1ad8 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -30793,22 +30793,43 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// datetime, not as the relative time it renders ("8 min" turns into
// "9 min" by itself while a deletion waits on WebBrain's card), so a
// ticking timestamp does not make the same post another one.
- const stableText = node => {
+ // Where the page breaks a line (a block box, a <br>) is part of what the
+ // text reads as: it is kept as a separator, so an edit that only changes
+ // the layout of the same text nodes is still another version of the post.
+ const stableText = (node, breaks = new WeakMap()) => {
+ const breaksLine = element => {
+ if (element.tagName === 'BR') return true;
+ if (!breaks.has(element)) {
+ let display = 'inline';
+ try { display = doc.defaultView.getComputedStyle(element).display || 'inline'; } catch { /* keep inline */ }
+ breaks.set(element, display !== 'inline' && display !== 'contents' && display !== 'none');
+ }
+ return breaks.get(element);
+ };
let text = '';
const pending = [node];
while (pending.length) {
const current = pending.pop();
- if (current.nodeType === 3) text += current.data;
+ if (typeof current === 'string') text += current;
+ else if (current.nodeType === 3) text += current.data;
else if (current.nodeType === 1 && current.matches('time[datetime]')) text += `\u0000${current.getAttribute('datetime')}\u0000`;
- else if (current.nodeType === 1) for (let index = current.childNodes.length - 1; index >= 0; index -= 1) pending.push(current.childNodes[index]);
+ else if (current.nodeType === 1) {
+ const separator = breaksLine(current) ? '\n' : '';
+ if (separator) pending.push(separator);
+ for (let index = current.childNodes.length - 1; index >= 0; index -= 1) pending.push(current.childNodes[index]);
+ if (separator) text += separator;
+ }
}
return text;
};
// 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]'))
- .map(stableText).filter(value => value.length <= 25000))];
+ const stableTexts = nodes => {
+ const breaks = new WeakMap();
+ return [...new Set(nodes
+ .filter(node => !node.parentElement?.closest('time[datetime]'))
+ .map(node => stableText(node, breaks)).filter(value => value.length <= 25000))];
+ };
const nativeResourceProofs = () => {
if (args.__wbNativeResourceAdapter !== 'mastodon') return [];
const signedIn = nativeSignedInAccount();
diff --git a/test/native-resource-deletion-binding.mjs b/test/native-resource-deletion-binding.mjs
index 0ffead0ab..c6d515c69 100644
--- a/test/native-resource-deletion-binding.mjs
+++ b/test/native-resource-deletion-binding.mjs
@@ -341,6 +341,29 @@ for (const build of ['chrome', 'firefox']) {
} finally { await f.page.close(); }
});
+ // The same text nodes, laid out differently (a line break appears), read
+ // as another version of the post.
+ await t.test('an edit that only changes where the post breaks lines is another post', 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__content__text--visible p').innerHTML
+ = '<span id="first-part">Exact owned</span><span id="second-part"> post 200</span>';
+ });
+ await f.showConfirmation();
+ const detected = await f.probe('#confirm');
+ assert.equal(detected.actionResource.url, resource);
+ const key = found => agent._resourceMutationActionKey(1, 'click', { selector: '#confirm' }, found);
+ assert.equal(key(await f.probe('#confirm')), key(detected), 'Unchanged layout is the same post');
+ await f.page.evaluate(() => { document.getElementById('second-part').style.display = 'block'; });
+ assert.notEqual(key(await f.probe('#confirm')), key(detected), 'A new line break is another version');
+ } finally { await f.page.close(); }
+ });
+
await t.test('a relative time with nested markup ticking over keeps the same post', async () => {
const f = await fixture();
try {