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
fix(agent): prefer authored body anchors and prioritize colon-introduced task bodies
Emre Sokullu47ca94312072
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 398f18a46..7c1551304 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -2323,19 +2323,46 @@ export class Agent extends LoopDetector {};for (const [index, requested] of requestedUrls.entries()) {+ const getDisplayed = (candidate) => {+ if (!candidate) return null;+ return ['text', 'title', 'ariaLabel', 'expandedUrl', 'href']+ .map(field => this._workflowMetadataValue(candidate[field]))+ .filter(value => value && observedBody.includes(value))+ .find(value => this._workflowSocialDisplayedUrlMatchesRequested(value, requested));+ };+let linkIndex = links.findIndex((candidate, candidateIndex) => (!consumedLinks.has(candidateIndex)+ && candidate?.authored&& this._workflowSocialLinkMatchesRequested(candidate, requested)+ && getDisplayed(candidate)));+ if (linkIndex < 0) {+ linkIndex = links.findIndex((candidate, candidateIndex) => (+ !consumedLinks.has(candidateIndex)+ && candidate?.authored+ && this._workflowSocialLinkMatchesRequested(candidate, requested)+ ));+ }+ if (linkIndex < 0) {+ linkIndex = links.findIndex((candidate, candidateIndex) => (+ !consumedLinks.has(candidateIndex)+ && this._workflowSocialLinkMatchesRequested(candidate, requested)+ && getDisplayed(candidate)+ ));+ }+ if (linkIndex < 0) {+ linkIndex = links.findIndex((candidate, candidateIndex) => (+ !consumedLinks.has(candidateIndex)+ && this._workflowSocialLinkMatchesRequested(candidate, requested)+ ));+ }if (linkIndex < 0) linkIndex = priorLinkByRequestedUrl.get(requested) ?? -1;const link = linkIndex >= 0 ? links[linkIndex] : null;if (!link) return false;consumedLinks.add(linkIndex);if (!priorLinkByRequestedUrl.has(requested)) priorLinkByRequestedUrl.set(requested, linkIndex);- const displayed = ['text', 'title', 'ariaLabel', 'expandedUrl', 'href']- .map(field => this._workflowMetadataValue(link[field]))- .filter(value => value && observedBody.includes(value))- .find(value => this._workflowSocialDisplayedUrlMatchesRequested(value, requested));+ const displayed = getDisplayed(link);if (!displayed) return false;const timesUsed = linkUsageCount.get(linkIndex) || 0;@@ -22397,28 +22424,55 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst colonPattern = new RegExp(`${publishVerbPattern}[\\s\\S]*?(?<!https?|ftp|sftp)[:οΌ](?!//)\\s*([\\s\\S]+)$`, 'iu');for (const text of candidates) {let body = '';- // 1. Quoted body: Post "..." on X, Tweet "...", Publish "...", etc.+ const colonMatch = text.match(colonPattern);+ let colonBody = '';+ let colonDelimIndex = -1;+ if (colonMatch && colonMatch[1]?.trim()) {+ let candidate = colonMatch[1].trim();+ const innerQuote = candidate.match(/^(?:β([\s\S]+)β|γ([\s\S]+)γ|γ([\s\S]+)γ|Β«([\s\S]+)Β»|"([\s\S]+)"|'([\s\S]+)')$/);+ const innerContent = innerQuote ? (innerQuote[1] || innerQuote[2] || innerQuote[3] || innerQuote[4] || innerQuote[5] || innerQuote[6]) : null;+ if (innerContent && innerContent.trim()) {+ candidate = innerContent.trim();+ }+ colonBody = candidate;+ const prefix = colonMatch[0].slice(0, colonMatch[0].length - colonMatch[1].length);+ const matchDelim = prefix.search(/[:οΌ][^\S\r\n]*$/);+ colonDelimIndex = colonMatch.index + (matchDelim >= 0 ? matchDelim : prefix.length - 1);+ }+const quotedMatch = text.match(quotedPattern);- const matchedContent = quotedMatch ? (quotedMatch[1] || quotedMatch[2] || quotedMatch[3] || quotedMatch[4] || quotedMatch[5]) : null;- if (matchedContent && matchedContent.trim()) {- body = matchedContent.trim();- } else {- // Single-quoted body (not contractions)- const singleQuoteMatch = text.match(singleQuotePattern);- if (singleQuoteMatch && singleQuoteMatch[1]?.trim()) {- body = singleQuoteMatch[1].trim();+ let quotedBody = '';+ let quoteOpenIndex = -1;+ if (quotedMatch) {+ const content = quotedMatch[1] || quotedMatch[2] || quotedMatch[3] || quotedMatch[4] || quotedMatch[5];+ if (content && content.trim()) {+ quotedBody = content.trim();+ quoteOpenIndex = quotedMatch.index + quotedMatch[0].indexOf(content) - 1;+ }+ }++ const singleQuoteMatch = text.match(singleQuotePattern);+ let singleQuoteBody = '';+ let singleQuoteOpenIndex = -1;+ if (singleQuoteMatch && singleQuoteMatch[1]?.trim()) {+ singleQuoteBody = singleQuoteMatch[1].trim();+ singleQuoteOpenIndex = singleQuoteMatch.index + singleQuoteMatch[0].indexOf(singleQuoteMatch[1]) - 1;+ }++ if (colonBody) {+ if (quotedBody) {+ body = (colonDelimIndex < quoteOpenIndex) ? colonBody : quotedBody;+ } else if (singleQuoteBody) {+ body = (colonDelimIndex < singleQuoteOpenIndex) ? colonBody : singleQuoteBody;} else {- // 2. Colon-introduced body: "Publish this exact post on X:\s*<body text>", "Tweet:\s*<body text>", "Post the following on X:\s*<body text>"- const colonMatch = text.match(colonPattern);- if (colonMatch && colonMatch[1]?.trim()) {- let candidateBody = colonMatch[1].trim();- const innerQuote = candidateBody.match(/^(?:β([\s\S]+)β|γ([\s\S]+)γ|γ([\s\S]+)γ|Β«([\s\S]+)Β»|"([\s\S]+)"|'([\s\S]+)')$/);- const innerContent = innerQuote ? (innerQuote[1] || innerQuote[2] || innerQuote[3] || innerQuote[4] || innerQuote[5] || innerQuote[6]) : null;- if (innerContent) candidateBody = innerContent.trim();- if (candidateBody) body = candidateBody;- }+ body = colonBody;}+ } else if (quotedBody) {+ body = quotedBody;+ } else if (singleQuoteBody) {+ body = singleQuoteBody;}+if (body && body.length > bestBody.length) {bestBody = body;}diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex a0cc799be..a23fd584d 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -2037,19 +2037,46 @@ export class Agent extends LoopDetector {};for (const [index, requested] of requestedUrls.entries()) {+ const getDisplayed = (candidate) => {+ if (!candidate) return null;+ return ['text', 'title', 'ariaLabel', 'expandedUrl', 'href']+ .map(field => this._workflowMetadataValue(candidate[field]))+ .filter(value => value && observedBody.includes(value))+ .find(value => this._workflowSocialDisplayedUrlMatchesRequested(value, requested));+ };+let linkIndex = links.findIndex((candidate, candidateIndex) => (!consumedLinks.has(candidateIndex)+ && candidate?.authored&& this._workflowSocialLinkMatchesRequested(candidate, requested)+ && getDisplayed(candidate)));+ if (linkIndex < 0) {+ linkIndex = links.findIndex((candidate, candidateIndex) => (+ !consumedLinks.has(candidateIndex)+ && candidate?.authored+ && this._workflowSocialLinkMatchesRequested(candidate, requested)+ ));+ }+ if (linkIndex < 0) {+ linkIndex = links.findIndex((candidate, candidateIndex) => (+ !consumedLinks.has(candidateIndex)+ && this._workflowSocialLinkMatchesRequested(candidate, requested)+ && getDisplayed(candidate)+ ));+ }+ if (linkIndex < 0) {+ linkIndex = links.findIndex((candidate, candidateIndex) => (+ !consumedLinks.has(candidateIndex)+ && this._workflowSocialLinkMatchesRequested(candidate, requested)+ ));+ }if (linkIndex < 0) linkIndex = priorLinkByRequestedUrl.get(requested) ?? -1;const link = linkIndex >= 0 ? links[linkIndex] : null;if (!link) return false;consumedLinks.add(linkIndex);if (!priorLinkByRequestedUrl.has(requested)) priorLinkByRequestedUrl.set(requested, linkIndex);- const displayed = ['text', 'title', 'ariaLabel', 'expandedUrl', 'href']- .map(field => this._workflowMetadataValue(link[field]))- .filter(value => value && observedBody.includes(value))- .find(value => this._workflowSocialDisplayedUrlMatchesRequested(value, requested));+ const displayed = getDisplayed(link);if (!displayed) return false;const timesUsed = linkUsageCount.get(linkIndex) || 0;@@ -20000,28 +20027,55 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst colonPattern = new RegExp(`${publishVerbPattern}[\\s\\S]*?(?<!https?|ftp|sftp)[:οΌ](?!//)\\s*([\\s\\S]+)$`, 'iu');for (const text of candidates) {let body = '';- // 1. Quoted body: Post "..." on X, Tweet "...", Publish "...", etc.+ const colonMatch = text.match(colonPattern);+ let colonBody = '';+ let colonDelimIndex = -1;+ if (colonMatch && colonMatch[1]?.trim()) {+ let candidate = colonMatch[1].trim();+ const innerQuote = candidate.match(/^(?:β([\s\S]+)β|γ([\s\S]+)γ|γ([\s\S]+)γ|Β«([\s\S]+)Β»|"([\s\S]+)"|'([\s\S]+)')$/);+ const innerContent = innerQuote ? (innerQuote[1] || innerQuote[2] || innerQuote[3] || innerQuote[4] || innerQuote[5] || innerQuote[6]) : null;+ if (innerContent && innerContent.trim()) {+ candidate = innerContent.trim();+ }+ colonBody = candidate;+ const prefix = colonMatch[0].slice(0, colonMatch[0].length - colonMatch[1].length);+ const matchDelim = prefix.search(/[:οΌ][^\S\r\n]*$/);+ colonDelimIndex = colonMatch.index + (matchDelim >= 0 ? matchDelim : prefix.length - 1);+ }+const quotedMatch = text.match(quotedPattern);- const matchedContent = quotedMatch ? (quotedMatch[1] || quotedMatch[2] || quotedMatch[3] || quotedMatch[4] || quotedMatch[5]) : null;- if (matchedContent && matchedContent.trim()) {- body = matchedContent.trim();- } else {- // Single-quoted body (not contractions)- const singleQuoteMatch = text.match(singleQuotePattern);- if (singleQuoteMatch && singleQuoteMatch[1]?.trim()) {- body = singleQuoteMatch[1].trim();+ let quotedBody = '';+ let quoteOpenIndex = -1;+ if (quotedMatch) {+ const content = quotedMatch[1] || quotedMatch[2] || quotedMatch[3] || quotedMatch[4] || quotedMatch[5];+ if (content && content.trim()) {+ quotedBody = content.trim();+ quoteOpenIndex = quotedMatch.index + quotedMatch[0].indexOf(content) - 1;+ }+ }++ const singleQuoteMatch = text.match(singleQuotePattern);+ let singleQuoteBody = '';+ let singleQuoteOpenIndex = -1;+ if (singleQuoteMatch && singleQuoteMatch[1]?.trim()) {+ singleQuoteBody = singleQuoteMatch[1].trim();+ singleQuoteOpenIndex = singleQuoteMatch.index + singleQuoteMatch[0].indexOf(singleQuoteMatch[1]) - 1;+ }++ if (colonBody) {+ if (quotedBody) {+ body = (colonDelimIndex < quoteOpenIndex) ? colonBody : quotedBody;+ } else if (singleQuoteBody) {+ body = (colonDelimIndex < singleQuoteOpenIndex) ? colonBody : singleQuoteBody;} else {- // 2. Colon-introduced body: "Publish this exact post on X:\s*<body text>", "Tweet:\s*<body text>", "Post the following on X:\s*<body text>"- const colonMatch = text.match(colonPattern);- if (colonMatch && colonMatch[1]?.trim()) {- let candidateBody = colonMatch[1].trim();- const innerQuote = candidateBody.match(/^(?:β([\s\S]+)β|γ([\s\S]+)γ|γ([\s\S]+)γ|Β«([\s\S]+)Β»|"([\s\S]+)"|'([\s\S]+)')$/);- const innerContent = innerQuote ? (innerQuote[1] || innerQuote[2] || innerQuote[3] || innerQuote[4] || innerQuote[5] || innerQuote[6]) : null;- if (innerContent) candidateBody = innerContent.trim();- if (candidateBody) body = candidateBody;- }+ body = colonBody;}+ } else if (quotedBody) {+ body = quotedBody;+ } else if (singleQuoteBody) {+ body = singleQuoteBody;}+if (body && body.length > bestBody.length) {bestBody = body;}diff --git a/test/run.js b/test/run.jsindex 2fa874dc9..6ef5412b0 100644--- a/test/run.js+++ b/test/run.js@@ -87216,6 +87216,31 @@ test('social body verification reads the authored post text, not the card chrome}});+test('social body verification prefers authored link over author-profile anchor when body links to own profile', () => {+ for (const AgentClass of [AgentCh, AgentFx]) {+ const agent = new AgentClass({});+ const ownProfileUrl = 'https://x.com/myhandle';+ const card = {+ bodyText: 'Follow my profile x.com/myhandle for updates',+ links: [+ // Earlier anchor on the card (e.g. author avatar/name linking to profile)+ { href: ownProfileUrl, text: 'My Author Name', authored: false },+ // Authored anchor inside the tweet text+ { href: ownProfileUrl, text: 'x.com/myhandle', expandedUrl: ownProfileUrl, authored: true },+ ],+ };+ assert.equal(+ agent._workflowSocialPublishedBodyObserved(+ { field: 'body', value: `Follow my profile ${ownProfileUrl} for updates` },+ card,+ ),+ true,+ AgentClass.name + ': should bind to authored anchor when linking to own profile'+ );+ }+});++test('a Bluesky DID and handle name one account only on the card that proves it', () => {for (const AgentClass of [AgentCh, AgentFx]) {const agent = new AgentClass({});@@ -87837,6 +87862,32 @@ test('attachment verification matches specific attachment names without substrin}});+test('post body extraction prefers colon-introduced body when colon precedes incidental quotes', () => {+ for (const AgentClass of [AgentCh, AgentFx]) {+ const agent = new AgentClass({});+ const longBodyWithQuotes = 'Post on X: Here is a long announcement containing "important news" and more updates today';+ assert.equal(+ agent._extractWorkflowTaskBody(longBodyWithQuotes),+ 'Here is a long announcement containing "important news" and more updates today',+ AgentClass.name + ': colon-introduced body should preserve text containing incidental quotes'+ );++ const quotedBeforeColon = 'Post "breaking news" on X: see updates now';+ assert.equal(+ agent._extractWorkflowTaskBody(quotedBeforeColon),+ 'breaking news',+ AgentClass.name + ': quote before colon should take precedence'+ );++ const colonWrappedInQuotes = 'Post on X: "Hello world with updates"';+ assert.equal(+ agent._extractWorkflowTaskBody(colonWrappedInQuotes),+ 'Hello world with updates',+ AgentClass.name + ': colon body completely wrapped in quotes should be unwrapped'+ );+ }+});+test('post body extraction preserves nested quotation marks and does not overwrite full body with prefix', async () => {for (const [index, AgentClass] of [AgentCh, AgentFx].entries()) {const agent = new AgentClass({});