Skip to content

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

fix(agent): preserve incomplete publication metadata, ignore embedded numbers in attachment names, and scope URL publication clauses

Emre Sokullu9a1cf9deb58f
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 7fa189091..4382c653e 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -2370,6 +2370,15 @@ export class Agent extends LoopDetector {
const videoCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+])(${countPrefix})\\s+(?:videos?|clips?|recordings?|gifs?|animated[-_ ]gifs?|vidéo|vidéos|動画|视频|影片|видео)`, 'i'));
const expectedVideoCount = videoCountMatch ? parseCountWord(videoCountMatch[1]) : 0;
+ let isGeneric = false;
+ if (CJK_GENERIC_ATTACHMENT_REGEX.test(text)) {
+ isGeneric = true;
+ } else {
+ const nonPunctuation = text.replace(/[\s\-_,.:;!?/\\()]+/g, ' ').trim();
+ const words = nonPunctuation ? nonPunctuation.split(/\s+/) : [];
+ isGeneric = words.length > 0 && words.every(w => /^\d+$/.test(w) || GENERIC_ATTACHMENT_WORDS.has(w) || CJK_GENERIC_ATTACHMENT_REGEX.test(w));
+ }
+
let expectedCount = 1;
if (expectedImageCount > 0 && expectedVideoCount > 0) {
expectedCount = expectedImageCount + expectedVideoCount;
@@ -2378,22 +2387,16 @@ export class Agent extends LoopDetector {
} else if (expectedVideoCount > 0) {
expectedCount = expectedVideoCount;
} else {
- const generalCountMatch = text.match(new RegExp(`\\b(${countPrefix})\\b`, 'i')) || text.match(/([一二两三四五六七八九十])/);
+ const genericNounCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+])(${countPrefix})\\s+(?:attachments?|files?|media|uploads?|pieces?|items?|assets?|enclosures?|documents?|fichiers?|archivos?|dateien?|allegati?|anexos?|вложения?|вложение)`, 'i'))
+ || text.match(new RegExp(`(?:^|[\\s,;and+])(${countPrefix})\\s*(?:枚|つ|本|张|條|条|个|個|장)`, 'i'));
+ const generalCountMatch = genericNounCountMatch
+ || (isGeneric && (text.match(new RegExp(`\\b(${countPrefix})\\b`, 'i')) || text.match(/([一二两三四五六七八九十])/)));
if (generalCountMatch) {
const parsedNum = parseCountWord(generalCountMatch[1]);
if (parsedNum > 0) expectedCount = parsedNum;
}
}
- let isGeneric = false;
- if (CJK_GENERIC_ATTACHMENT_REGEX.test(text)) {
- isGeneric = true;
- } else {
- const nonPunctuation = text.replace(/[\s\-_,.:;!?/\\()]+/g, ' ').trim();
- const words = nonPunctuation ? nonPunctuation.split(/\s+/) : [];
- isGeneric = words.length > 0 && words.every(w => /^\d+$/.test(w) || GENERIC_ATTACHMENT_WORDS.has(w) || CJK_GENERIC_ATTACHMENT_REGEX.test(w));
- }
-
return { isGeneric, expectedCount, expectedImageCount, expectedVideoCount, wantsImage, wantsVideo, wantsGif, normalized: text };
}
@@ -15026,11 +15029,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
match.index + match[0].length + 60,
);
const beforeClauses = this._socialPublicationClauses(before);
- const lastBeforeClause = beforeClauses[beforeClauses.length - 1];
+ let lastBeforeClause = beforeClauses[beforeClauses.length - 1];
+ if (lastBeforeClause && !lastBeforeClause.text.trim()) {
+ if (lastBeforeClause.delim === ':' && beforeClauses.length > 1) {
+ lastBeforeClause = beforeClauses[beforeClauses.length - 2];
+ } else {
+ lastBeforeClause = null;
+ }
+ }
if (lastBeforeClause?.isNegated) continue;
+ const afterClauses = this._socialPublicationClauses(after);
+ const firstAfterClause = afterClauses.find(c => c.text.trim().length > 0);
const governed = /\/(?:compose|intent|i\/flow)\//.test(url)
- || this._socialPublicationCommandIn(before)
- || (SOCIAL_PUBLISH_DESTINATION_LEAD.test(before) && this._socialPublicationCommandIn(after));
+ || this._socialPublicationCommandIn(lastBeforeClause?.text || '')
+ || (SOCIAL_PUBLISH_DESTINATION_LEAD.test(lastBeforeClause?.text || '')
+ && this._socialPublicationCommandIn(firstAfterClause?.text || ''));
if (!governed) continue;
const workflow = resolveAdapterWorkflowJob(url, 'publish-post');
if (workflow?.job && workflow.adapterName === destination) targets.add(destination);
@@ -22236,8 +22249,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
}
guard.workflowMetadataRequirements = details.items;
- guard.workflowMetadataRequirementsIncomplete = details.incomplete && details.items.length === 0;
- guard.workflowMetadataRequirementsResolved = true;
+ guard.workflowMetadataRequirementsIncomplete = details.incomplete === true;
+ guard.workflowMetadataRequirementsResolved = details.incomplete !== true;
}
}
if (siteWorkflow?.job?.template === 'form') {
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index ae38e70d6..2605ca4b0 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -2084,6 +2084,15 @@ export class Agent extends LoopDetector {
const videoCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+])(${countPrefix})\\s+(?:videos?|clips?|recordings?|gifs?|animated[-_ ]gifs?|vidéo|vidéos|動画|视频|影片|видео)`, 'i'));
const expectedVideoCount = videoCountMatch ? parseCountWord(videoCountMatch[1]) : 0;
+ let isGeneric = false;
+ if (CJK_GENERIC_ATTACHMENT_REGEX.test(text)) {
+ isGeneric = true;
+ } else {
+ const nonPunctuation = text.replace(/[\s\-_,.:;!?/\\()]+/g, ' ').trim();
+ const words = nonPunctuation ? nonPunctuation.split(/\s+/) : [];
+ isGeneric = words.length > 0 && words.every(w => /^\d+$/.test(w) || GENERIC_ATTACHMENT_WORDS.has(w) || CJK_GENERIC_ATTACHMENT_REGEX.test(w));
+ }
+
let expectedCount = 1;
if (expectedImageCount > 0 && expectedVideoCount > 0) {
expectedCount = expectedImageCount + expectedVideoCount;
@@ -2092,22 +2101,16 @@ export class Agent extends LoopDetector {
} else if (expectedVideoCount > 0) {
expectedCount = expectedVideoCount;
} else {
- const generalCountMatch = text.match(new RegExp(`\\b(${countPrefix})\\b`, 'i')) || text.match(/([一二两三四五六七八九十])/);
+ const genericNounCountMatch = text.match(new RegExp(`(?:^|[\\s,;and+])(${countPrefix})\\s+(?:attachments?|files?|media|uploads?|pieces?|items?|assets?|enclosures?|documents?|fichiers?|archivos?|dateien?|allegati?|anexos?|вложения?|вложение)`, 'i'))
+ || text.match(new RegExp(`(?:^|[\\s,;and+])(${countPrefix})\\s*(?:枚|つ|本|张|條|条|个|個|장)`, 'i'));
+ const generalCountMatch = genericNounCountMatch
+ || (isGeneric && (text.match(new RegExp(`\\b(${countPrefix})\\b`, 'i')) || text.match(/([一二两三四五六七八九十])/)));
if (generalCountMatch) {
const parsedNum = parseCountWord(generalCountMatch[1]);
if (parsedNum > 0) expectedCount = parsedNum;
}
}
- let isGeneric = false;
- if (CJK_GENERIC_ATTACHMENT_REGEX.test(text)) {
- isGeneric = true;
- } else {
- const nonPunctuation = text.replace(/[\s\-_,.:;!?/\\()]+/g, ' ').trim();
- const words = nonPunctuation ? nonPunctuation.split(/\s+/) : [];
- isGeneric = words.length > 0 && words.every(w => /^\d+$/.test(w) || GENERIC_ATTACHMENT_WORDS.has(w) || CJK_GENERIC_ATTACHMENT_REGEX.test(w));
- }
-
return { isGeneric, expectedCount, expectedImageCount, expectedVideoCount, wantsImage, wantsVideo, wantsGif, normalized: text };
}
@@ -12826,11 +12829,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
match.index + match[0].length + 60,
);
const beforeClauses = this._socialPublicationClauses(before);
- const lastBeforeClause = beforeClauses[beforeClauses.length - 1];
+ let lastBeforeClause = beforeClauses[beforeClauses.length - 1];
+ if (lastBeforeClause && !lastBeforeClause.text.trim()) {
+ if (lastBeforeClause.delim === ':' && beforeClauses.length > 1) {
+ lastBeforeClause = beforeClauses[beforeClauses.length - 2];
+ } else {
+ lastBeforeClause = null;
+ }
+ }
if (lastBeforeClause?.isNegated) continue;
+ const afterClauses = this._socialPublicationClauses(after);
+ const firstAfterClause = afterClauses.find(c => c.text.trim().length > 0);
const governed = /\/(?:compose|intent|i\/flow)\//.test(url)
- || this._socialPublicationCommandIn(before)
- || (SOCIAL_PUBLISH_DESTINATION_LEAD.test(before) && this._socialPublicationCommandIn(after));
+ || this._socialPublicationCommandIn(lastBeforeClause?.text || '')
+ || (SOCIAL_PUBLISH_DESTINATION_LEAD.test(lastBeforeClause?.text || '')
+ && this._socialPublicationCommandIn(firstAfterClause?.text || ''));
if (!governed) continue;
const workflow = resolveAdapterWorkflowJob(url, 'publish-post');
if (workflow?.job && workflow.adapterName === destination) targets.add(destination);
@@ -19839,8 +19852,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
}
guard.workflowMetadataRequirements = details.items;
- guard.workflowMetadataRequirementsIncomplete = details.incomplete && details.items.length === 0;
- guard.workflowMetadataRequirementsResolved = true;
+ guard.workflowMetadataRequirementsIncomplete = details.incomplete === true;
+ guard.workflowMetadataRequirementsResolved = details.incomplete !== true;
}
}
if (siteWorkflow?.job?.template === 'form') {
diff --git a/test/run.js b/test/run.js
index 3482a3301..756b4076e 100644
--- a/test/run.js
+++ b/test/run.js
@@ -86289,6 +86289,8 @@ test('social publication workflow follows the live X or Bluesky destination and
'independent affirmative clause after negated non-publish clause separated by comma was wrongly rejected'],
['Do not post on Bluesky, post this on X', ['twitter'],
'independent affirmative publish clause separated by comma from negated publish clause was wrongly rejected'],
+ ['Publish on Bluesky; read https://x.com/home', ['bluesky'],
+ 'unrelated publication command in prior clause was wrongly attributed to read-only social URL'],
]) {
assert.deepEqual(
[...agent._trustedSocialPublishTargetAdapters({ taskText })],
@@ -87410,6 +87412,65 @@ test('classifier fallback leaves metadata requirements incomplete and unresolved
}
});
+test('attachment requirement parser ignores numbers in specific attachment filenames', () => {
+ for (const AgentClass of [AgentCh, AgentFx]) {
+ const agent = new AgentClass({});
+ const req1 = agent._parseWorkflowAttachmentRequirement('quarterly-chart-2.png');
+ assert.equal(req1.expectedCount, 1, AgentClass.name + ': quarterly-chart-2.png should expect 1 attachment');
+ assert.equal(req1.isGeneric, false, AgentClass.name + ': quarterly-chart-2.png should not be generic');
+
+ const req2 = agent._parseWorkflowAttachmentRequirement('photo-2024-12.jpg');
+ assert.equal(req2.expectedCount, 1, AgentClass.name + ': photo-2024-12.jpg should expect 1 attachment');
+ assert.equal(req2.isGeneric, false, AgentClass.name + ': photo-2024-12.jpg should not be generic');
+
+ const req3 = agent._parseWorkflowAttachmentRequirement('2 attachments');
+ assert.equal(req3.expectedCount, 2, AgentClass.name + ': 2 attachments should expect 2 attachments');
+ assert.equal(req3.isGeneric, true, AgentClass.name + ': 2 attachments should be generic');
+
+ const req4 = agent._parseWorkflowAttachmentRequirement('2枚');
+ assert.equal(req4.expectedCount, 2, AgentClass.name + ': 2枚 should expect 2 attachments');
+ assert.equal(req4.isGeneric, true, AgentClass.name + ': 2枚 should be generic');
+ }
+});
+
+test('partially classified metadata retains incomplete state despite extracted body', async () => {
+ for (const [index, AgentClass] of [AgentCh, AgentFx].entries()) {
+ const agent = new AgentClass({});
+ const tabId = 9880 + index;
+ const siteWorkflow = agent._resolvePlannerSiteWorkflow('https://x.com/home', {
+ request_kind: 'execute',
+ site_job: 'publish-post',
+ });
+ const guard = agent._startPlanExecutionGuard(tabId, 'act', {
+ requestKind: 'execute',
+ requiresStateChange: true,
+ requiresSubmission: true,
+ siteWorkflow,
+ });
+ const partialProvider = {
+ chat: async () => ({
+ content: JSON.stringify({
+ workflowFields: [
+ { field: 'body', value: 'Hello world' },
+ { field: 'attachment' }, // omitted value -> discarded
+ ],
+ }),
+ }),
+ };
+ await agent._classifyProgressIntentWithProvider(tabId, {
+ provider: partialProvider,
+ taskText: 'Publish this on X: Hello world with quarterly-chart-2.png',
+ pageScope: 'https://x.com/home',
+ });
+ assert.equal(guard.workflowMetadataRequirementsIncomplete, true,
+ AgentClass.name + ': partial classifier output should mark workflowMetadataRequirementsIncomplete as true');
+ assert.notEqual(guard.workflowMetadataRequirementsResolved, true,
+ AgentClass.name + ': partial classifier output should not mark workflowMetadataRequirementsResolved as true');
+ assert.equal(guard.workflowMetadataRequirements?.length, 1,
+ AgentClass.name + ': valid body field should be preserved');
+ }
+});
+
test('YouTube metadata success requires exact app-classified post-save readback', async () => {
for (const [index, AgentClass] of [AgentCh, AgentFx].entries()) {
const agent = new AgentClass({ getActive: () => ({ chat: async () => ({ content: '{}' }) }) });