Skip to content

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

fix(social-publish): bind inline replies to the active thread

Emre Sokullub7f35bd87a97
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 2db980677..8f007f9c0 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -22645,6 +22645,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const publicationComposerSnapshot = (root, account) => {
if (!root || !account.complete || !['x.com', 'twitter.com', 'bsky.app'].includes(host.replace(/^www\./, ''))) return null;
try {
+ const permalink = value => {
+ if (!value) return null;
+ try {
+ const parsed = new URL(value, url);
+ const linkHost = parsed.hostname.toLowerCase().replace(/^www\./, '');
+ const pageHost = host.toLowerCase().replace(/^www\./, '');
+ const path = parsed.pathname.replace(/\/+$/, '');
+ if (!/^https?:$/.test(parsed.protocol) || parsed.username || parsed.password || parsed.port) return null;
+ const isX = ['x.com', 'twitter.com'].includes(pageHost) && ['x.com', 'twitter.com'].includes(linkHost)
+ && /^\/[A-Za-z0-9_]{1,15}\/status\/\d+$/.test(path);
+ const isBluesky = pageHost === 'bsky.app' && linkHost === 'bsky.app'
+ && /^\/profile\/[^/]+\/post\/[^/]+$/.test(path);
+ return isX || isBluesky ? parsed.origin + path : null;
+ } catch { return null; }
+ };
const editors = Array.from(root.querySelectorAll('textarea,[contenteditable="true"],[role="textbox"]'))
.filter(isVisible).filter((el, _i, all) => !all.some(other => other !== el && other.contains(el)));
if (!editors.length || editors.length > 12) return { complete: false };
@@ -22682,16 +22697,25 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return { type: video ? 'video' : 'image', src: String(source),
alt: node.getAttribute('alt') ?? node.querySelector('img')?.getAttribute('alt') ?? '' };
});
- // Only embedded context cards supply a quote/reply target. Authored
- // links and link previews cannot turn into relationship evidence.
- const contextCards = Array.from(scope?.querySelectorAll('article,[data-testid="quoteTweet"],[data-testid="replyToPost"]') || []).filter(card => !card.contains(editor));
+ // Authored links and link previews are not relationship evidence.
+ const contextCards = Array.from(scope?.querySelectorAll('article,[data-testid="quoteTweet"],[data-testid="replyToPost"]') || [])
+ .filter(isVisible).filter(card => !card.contains(editor) && !editor.contains(card));
const contextUrls = [...new Set(contextCards.flatMap(card => Array.from(card.querySelectorAll('a[href]')))
- .map(a => { try { return new URL(a.getAttribute('href'), url).href; } catch { return ''; } })
- .filter(href => /\/status\/\d+|\/profile\/[^/]+\/post\/[^/]+/.test(href)))];
- const reply = !!scope?.querySelector('[data-testid="replyToPost"],[data-testid="replyingTo"]');
+ .map(a => permalink(a.getAttribute('href'))).filter(Boolean))];
+ const reply = Array.from(scope?.querySelectorAll('[data-testid="replyToPost"],[data-testid="replyingTo"]') || []).some(isVisible);
+ // Inline replies on a permalink page use the active thread as their
+ // parent; replyingTo commonly contains only a profile link. Modals
+ // and composers nested inside another post can target a different
+ // reply without changing the background route, so require their
+ // explicit context. Never substitute the requested contract target
+ // for an observed parent: the runtime compares those separately.
+ const inlineThreadTarget = reply && !contextUrls.length && editors.length === 1
+ && !editor.closest('dialog,[role="dialog"],[aria-modal="true"],[data-testid="tweet"],[data-testid^="feedItem-by-"],[data-testid^="postThreadItem-by-"]')
+ ? permalink(url) : null;
+ const target = contextUrls.length === 1 ? contextUrls[0] : inlineThreadTarget;
return { bodyText, attachments,
- context: { kind: reply ? 'reply' : contextUrls.length ? 'quote' : 'post', target: contextUrls.length === 1 ? contextUrls[0] : null },
- complete: bodyText.length <= 25000 && mediaNodes.length <= 20 && contextUrls.length <= 1 && (!reply || contextUrls.length === 1) };
+ context: { kind: reply ? 'reply' : contextUrls.length ? 'quote' : 'post', target },
+ complete: bodyText.length <= 25000 && mediaNodes.length <= 20 && contextUrls.length <= 1 && (!reply || !!target) };
});
return { complete: posts.every(p => p.complete) && !sharedMedia && allMedia.every(node => ownedMedia.has(node)), account: account.identity, posts };
} catch { return { complete: false }; }
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 1765ee316..a04dc434e 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -20304,6 +20304,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const publicationComposerSnapshot = (root, account) => {
if (!root || !account.complete || !['x.com', 'twitter.com', 'bsky.app'].includes(host.replace(/^www\./, ''))) return null;
try {
+ const permalink = value => {
+ if (!value) return null;
+ try {
+ const parsed = new URL(value, url);
+ const linkHost = parsed.hostname.toLowerCase().replace(/^www\./, '');
+ const pageHost = host.toLowerCase().replace(/^www\./, '');
+ const path = parsed.pathname.replace(/\/+$/, '');
+ if (!/^https?:$/.test(parsed.protocol) || parsed.username || parsed.password || parsed.port) return null;
+ const isX = ['x.com', 'twitter.com'].includes(pageHost) && ['x.com', 'twitter.com'].includes(linkHost)
+ && /^\/[A-Za-z0-9_]{1,15}\/status\/\d+$/.test(path);
+ const isBluesky = pageHost === 'bsky.app' && linkHost === 'bsky.app'
+ && /^\/profile\/[^/]+\/post\/[^/]+$/.test(path);
+ return isX || isBluesky ? parsed.origin + path : null;
+ } catch { return null; }
+ };
const editors = Array.from(root.querySelectorAll('textarea,[contenteditable="true"],[role="textbox"]'))
.filter(isVisible).filter((el, _i, all) => !all.some(other => other !== el && other.contains(el)));
if (!editors.length || editors.length > 12) return { complete: false };
@@ -20341,16 +20356,25 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return { type: video ? 'video' : 'image', src: String(source),
alt: node.getAttribute('alt') ?? node.querySelector('img')?.getAttribute('alt') ?? '' };
});
- // Only embedded context cards supply a quote/reply target. Authored
- // links and link previews cannot turn into relationship evidence.
- const contextCards = Array.from(scope?.querySelectorAll('article,[data-testid="quoteTweet"],[data-testid="replyToPost"]') || []).filter(card => !card.contains(editor));
+ // Authored links and link previews are not relationship evidence.
+ const contextCards = Array.from(scope?.querySelectorAll('article,[data-testid="quoteTweet"],[data-testid="replyToPost"]') || [])
+ .filter(isVisible).filter(card => !card.contains(editor) && !editor.contains(card));
const contextUrls = [...new Set(contextCards.flatMap(card => Array.from(card.querySelectorAll('a[href]')))
- .map(a => { try { return new URL(a.getAttribute('href'), url).href; } catch { return ''; } })
- .filter(href => /\/status\/\d+|\/profile\/[^/]+\/post\/[^/]+/.test(href)))];
- const reply = !!scope?.querySelector('[data-testid="replyToPost"],[data-testid="replyingTo"]');
+ .map(a => permalink(a.getAttribute('href'))).filter(Boolean))];
+ const reply = Array.from(scope?.querySelectorAll('[data-testid="replyToPost"],[data-testid="replyingTo"]') || []).some(isVisible);
+ // Inline replies on a permalink page use the active thread as their
+ // parent; replyingTo commonly contains only a profile link. Modals
+ // and composers nested inside another post can target a different
+ // reply without changing the background route, so require their
+ // explicit context. Never substitute the requested contract target
+ // for an observed parent: the runtime compares those separately.
+ const inlineThreadTarget = reply && !contextUrls.length && editors.length === 1
+ && !editor.closest('dialog,[role="dialog"],[aria-modal="true"],[data-testid="tweet"],[data-testid^="feedItem-by-"],[data-testid^="postThreadItem-by-"]')
+ ? permalink(url) : null;
+ const target = contextUrls.length === 1 ? contextUrls[0] : inlineThreadTarget;
return { bodyText, attachments,
- context: { kind: reply ? 'reply' : contextUrls.length ? 'quote' : 'post', target: contextUrls.length === 1 ? contextUrls[0] : null },
- complete: bodyText.length <= 25000 && mediaNodes.length <= 20 && contextUrls.length <= 1 && (!reply || contextUrls.length === 1) };
+ context: { kind: reply ? 'reply' : contextUrls.length ? 'quote' : 'post', target },
+ complete: bodyText.length <= 25000 && mediaNodes.length <= 20 && contextUrls.length <= 1 && (!reply || !!target) };
});
return { complete: posts.every(p => p.complete) && !sharedMedia && allMedia.every(node => ownedMedia.has(node)), account: account.identity, posts };
} catch { return { complete: false }; }
diff --git a/test/social-publish-contract-dom.mjs b/test/social-publish-contract-dom.mjs
index e27e59b05..6d2148558 100644
--- a/test/social-publish-contract-dom.mjs
+++ b/test/social-publish-contract-dom.mjs
@@ -55,6 +55,39 @@ try {
const reply=await probe();
assert.equal(reply.publicationSnapshot.complete,false);
checked++;
+ // Standard inline replies expose replyingTo, but no descendant status
+ // link. Their parent comes from the active permalink route.
+ const parent=platform==='twitter'?'https://x.com/bob/status/1111111111111111111':'https://bsky.app/profile/bob.bsky.social/post/3parent';
+ const otherParent=platform==='twitter'?'https://x.com/carol/status/3333333333333333333':'https://bsky.app/profile/carol.bsky.social/post/3other';
+ await page.evaluate(parent=>history.replaceState({},'',parent+'?source=fixture#reply'),parent);
+ const inlineReply=await probe();
+ assert.equal(inlineReply.publicationSnapshot.complete,true,'active thread identifies an inline reply parent');
+ assert.deepEqual(inlineReply.publicationSnapshot.posts[0].context,{kind:'reply',target:parent});checked++;
+ // A modal can target a different reply in the same thread. Its explicit
+ // context wins; the background route must not fill missing modal proof.
+ await page.locator('#composer').evaluate(el=>el.setAttribute('role','dialog'));
+ assert.equal((await probe()).publicationSnapshot.complete,false);checked++;
+ await page.locator('#composer').evaluate((el,parent)=>{
+ const card=document.createElement('article');card.dataset.testid='replyToPost';card.id='reply-parent';
+ const link=document.createElement('a');link.href=parent;link.textContent='Parent';card.append(link);el.append(card);
+ },otherParent);
+ const modalReply=await probe();
+ assert.equal(modalReply.publicationSnapshot.complete,true);
+ assert.equal(modalReply.publicationSnapshot.posts[0].context.target,otherParent);checked++;
+ // A second possible parent stays ambiguous even on a known thread route.
+ await page.locator('#reply-parent').evaluate((el,parent)=>{const a=document.createElement('a');a.href=parent;a.textContent='Other parent';el.append(a);},parent);
+ assert.equal((await probe()).publicationSnapshot.complete,false);checked++;
+ await page.locator('#reply-parent').evaluate(el=>el.remove());
+ await page.locator('#composer').evaluate(el=>el.removeAttribute('role'));
+ await page.evaluate(()=>history.replaceState({},'','/home?next=/bob/status/1111111111111111111'));
+ // An authored body link cannot supply missing reply relationship proof.
+ await page.locator('#body').evaluate((el,parent)=>{const a=document.createElement('a');a.href=parent;a.textContent='link';el.append(a);},parent);
+ assert.equal((await probe()).publicationSnapshot.complete,false);checked++;
+ await page.evaluate(parent=>history.replaceState({},'',parent),parent);
+ await page.locator('[data-testid="replyingTo"]').evaluate(el=>el.remove());
+ const ordinary=await probe();
+ assert.deepEqual(ordinary.publicationSnapshot.posts[0].context,{kind:'post',target:null});checked++;
+
// Each attachment must have exactly one observed owner in a thread.
await page.setContent(`<nav><a ${platform==='twitter'?'data-testid="AppTabBar_Profile_Link" href="/alice"':'href="/profile/alice.bsky.social"'}>Profile</a></nav><div id="composer"><section id="first"><div contenteditable="true" role="textbox">First</div><img src="https://cdn.example/one.png" width="40" height="40"></section><section><div contenteditable="true" role="textbox">Second</div><img src="https://cdn.example/two.png" width="40" height="40"></section><button id="publish" data-testid="${platform==='twitter'?'tweetButtonInline':'composerPublishBtn'}">Post</button></div>`);
const thread=await probe();
diff --git a/test/social-publish-contract.mjs b/test/social-publish-contract.mjs
index a6ca90cdd..f02048c79 100644
--- a/test/social-publish-contract.mjs
+++ b/test/social-publish-contract.mjs
@@ -284,6 +284,30 @@ for (const browser of ['chrome', 'firefox']) {
assert.equal(f.terminal(),null);
});
+ test(`${browser}: observed reply parent must match the contract and survive the final recheck`,async()=>{
+ const parent='https://x.com/bob/status/1111111111111111111';
+ const other='https://x.com/carol/status/3333333333333333333';
+ const action=rawAction('p1','twitter');action.posts[0].context={kind:'reply',target:ref(parent)};
+ for(const mode of ['matching','different','missing','changed']){
+ const f=setup(`Reply exactly Hello to ${parent} on X`,rawContract([action]));
+ f.agent._currentUrl=async()=>parent;
+ f.detected.publicationSnapshot.posts[0].context={kind:'reply',target:mode==='different'?other:mode==='missing'?null:parent};
+ if(mode==='missing') f.detected.publicationSnapshot.complete=false;
+ if(mode==='changed') f.agent._detectLikelySubmitAction=async()=>{
+ const fresh=structuredClone(f.detected);fresh.publicationSnapshot.posts[0].context.target=other;return fresh;
+ };
+ const block=await f.agent._workflowPreSubmitDispatchBlock(f.tabId,'click_ax',{},f.detected,f.provider);
+ if(mode==='matching'){
+ assert.equal(block,null);
+ assert.equal(f.guard.socialPublication.dispatch.snapshot.posts[0].context.target,parent);
+ } else {
+ assert.equal(block.noDispatch,true,mode);
+ assert.equal(f.guard.socialPublication.dispatch,null,mode);
+ }
+ assert.equal(f.calls.length,mode==='matching'||mode==='changed'?2:1,'a missing/wrong parent cannot be supplied by the contract or audit');
+ }
+ });
+
test(`${browser}: reply and quote completion require the correct relationship`,async()=>{
const target='https://x.com/bob/status/1111111111111111111';
for(const kind of ['reply','quote']){