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(social-publish): exclude negated format nouns from media intent
Barack Sokullu50e29cfc9869
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 5ed58fa98..a40efc51b 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -3122,9 +3122,25 @@ export class Agent extends LoopDetector {};}- const hasRawGif = RAW_GIF_NOUN_REGEX.test(text);- const hasRawOrdinaryVideo = RAW_VIDEO_NOUN_REGEX.test(text);- const hasRawImage = RAW_IMAGE_NOUN_REGEX.test(text);+ // Format-level negation (e.g. "no PNG images") does not set the+ // type-level negation flags, so positive media intent comes only from+ // affirmative occurrences: a media noun governed by no/not/without/zero+ // in its own clause names no media.+ const mediaOccurrenceIsNegated = (matchIndex) => {+ const precedingSegment = negationText.slice(0, matchIndex)+ .split(/(?:[,;]|\s+(?:and|but|plus|also|as\s+well\s+as)\s+)/iu)+ .pop() || '';+ return /(?<![\p{L}\p{N}_])(?:no|not(?!\s+only)|without(?:\s+any)?|zero|0)(?![\p{L}\p{N}_])/iu.test(precedingSegment);+ };+ const hasAffirmativeMediaMatch = (pattern) => {+ for (const match of text.matchAll(new RegExp(pattern.source, 'giu'))) {+ if (!mediaOccurrenceIsNegated(match.index || 0)) return true;+ }+ return false;+ };+ const hasRawGif = hasAffirmativeMediaMatch(RAW_GIF_NOUN_REGEX);+ const hasRawOrdinaryVideo = hasAffirmativeMediaMatch(RAW_VIDEO_NOUN_REGEX);+ const hasRawImage = hasAffirmativeMediaMatch(RAW_IMAGE_NOUN_REGEX);const wantsGif = hasRawGif && !isGifNegated;const wantsOrdinaryVideo = hasRawOrdinaryVideo && !isVideoNegated;@@ -4091,6 +4107,16 @@ export class Agent extends LoopDetector {|| (parsed.isVideoMinimum && (parsed.minimumVideoCount || parsed.expectedVideoCount || 1) > 0)|| (parsed.isGifMinimum && (parsed.minimumGifCount || parsed.expectedGifCount || 1) > 0)|| (parsed.minimumGenericCount || 0) > 0;+ // A purely prohibitive contract (forbidden formats/negated types with no+ // affirmative cardinality) requires nothing, so the empty set is valid.+ const hasAffirmativeMediaCardinality = (parsed.hasExplicitImageCount && !parsed.isImageNegated)+ || (parsed.hasExplicitVideoCount && !parsed.isVideoNegated)+ || (parsed.hasExplicitGifCount && !parsed.isGifNegated)+ || parsed.wantsImage || parsed.wantsOrdinaryVideo || parsed.wantsGif+ || parsed.hasExplicitGenericCount || (parsed.alternativeCounts?.length > 0);+ const hasProhibitiveMediaIntent = (parsed.forbiddenImageFormats?.length > 0)+ || (parsed.forbiddenVideoFormats?.length > 0)+ || parsed.isImageNegated || parsed.isVideoNegated || parsed.isGifNegated;const allowsNoAttachments = parsed.isGeneric && !hasPositiveMinimum&& !(parsed.hasBoundedCountRange && parsed.minimumCount > 0) && (parsed.isAlternative@@ -4101,7 +4127,8 @@ export class Agent extends LoopDetector {: (parsed.isMaximumCount|| parsed.isImageMaximum|| parsed.isVideoMaximum- || parsed.isGifMaximum)+ || parsed.isGifMaximum+ || (!hasAffirmativeMediaCardinality && hasProhibitiveMediaIntent))&& (!parsed.wantsImage || parsed.isImageMaximum || parsed.isImageNegated)&& (!parsed.wantsOrdinaryVideo || parsed.isVideoMaximum || parsed.isVideoNegated)&& (!parsed.wantsGif || parsed.isGifMaximum || parsed.isGifNegated)diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 2307048ed..d0225819d 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -2837,9 +2837,25 @@ export class Agent extends LoopDetector {};}- const hasRawGif = RAW_GIF_NOUN_REGEX.test(text);- const hasRawOrdinaryVideo = RAW_VIDEO_NOUN_REGEX.test(text);- const hasRawImage = RAW_IMAGE_NOUN_REGEX.test(text);+ // Format-level negation (e.g. "no PNG images") does not set the+ // type-level negation flags, so positive media intent comes only from+ // affirmative occurrences: a media noun governed by no/not/without/zero+ // in its own clause names no media.+ const mediaOccurrenceIsNegated = (matchIndex) => {+ const precedingSegment = negationText.slice(0, matchIndex)+ .split(/(?:[,;]|\s+(?:and|but|plus|also|as\s+well\s+as)\s+)/iu)+ .pop() || '';+ return /(?<![\p{L}\p{N}_])(?:no|not(?!\s+only)|without(?:\s+any)?|zero|0)(?![\p{L}\p{N}_])/iu.test(precedingSegment);+ };+ const hasAffirmativeMediaMatch = (pattern) => {+ for (const match of text.matchAll(new RegExp(pattern.source, 'giu'))) {+ if (!mediaOccurrenceIsNegated(match.index || 0)) return true;+ }+ return false;+ };+ const hasRawGif = hasAffirmativeMediaMatch(RAW_GIF_NOUN_REGEX);+ const hasRawOrdinaryVideo = hasAffirmativeMediaMatch(RAW_VIDEO_NOUN_REGEX);+ const hasRawImage = hasAffirmativeMediaMatch(RAW_IMAGE_NOUN_REGEX);const wantsGif = hasRawGif && !isGifNegated;const wantsOrdinaryVideo = hasRawOrdinaryVideo && !isVideoNegated;@@ -3806,6 +3822,16 @@ export class Agent extends LoopDetector {|| (parsed.isVideoMinimum && (parsed.minimumVideoCount || parsed.expectedVideoCount || 1) > 0)|| (parsed.isGifMinimum && (parsed.minimumGifCount || parsed.expectedGifCount || 1) > 0)|| (parsed.minimumGenericCount || 0) > 0;+ // A purely prohibitive contract (forbidden formats/negated types with no+ // affirmative cardinality) requires nothing, so the empty set is valid.+ const hasAffirmativeMediaCardinality = (parsed.hasExplicitImageCount && !parsed.isImageNegated)+ || (parsed.hasExplicitVideoCount && !parsed.isVideoNegated)+ || (parsed.hasExplicitGifCount && !parsed.isGifNegated)+ || parsed.wantsImage || parsed.wantsOrdinaryVideo || parsed.wantsGif+ || parsed.hasExplicitGenericCount || (parsed.alternativeCounts?.length > 0);+ const hasProhibitiveMediaIntent = (parsed.forbiddenImageFormats?.length > 0)+ || (parsed.forbiddenVideoFormats?.length > 0)+ || parsed.isImageNegated || parsed.isVideoNegated || parsed.isGifNegated;const allowsNoAttachments = parsed.isGeneric && !hasPositiveMinimum&& !(parsed.hasBoundedCountRange && parsed.minimumCount > 0) && (parsed.isAlternative@@ -3816,7 +3842,8 @@ export class Agent extends LoopDetector {: (parsed.isMaximumCount|| parsed.isImageMaximum|| parsed.isVideoMaximum- || parsed.isGifMaximum)+ || parsed.isGifMaximum+ || (!hasAffirmativeMediaCardinality && hasProhibitiveMediaIntent))&& (!parsed.wantsImage || parsed.isImageMaximum || parsed.isImageNegated)&& (!parsed.wantsOrdinaryVideo || parsed.isVideoMaximum || parsed.isVideoNegated)&& (!parsed.wantsGif || parsed.isGifMaximum || parsed.isGifNegated)diff --git a/test/run.js b/test/run.jsindex c322167a4..866855c79 100644--- a/test/run.js+++ b/test/run.js@@ -93107,6 +93107,26 @@ test('upper-bound attachment qualifiers verify as maximum counts', () => {assert.equal(agent._workflowSocialPublishedAttachmentObserved({ value: 'one video in MP4 format and no WebM videos' }, { attachments: [{ type: 'video', src: 'https://cdn.example/a.webm' }] }), false,AgentClass.name + ': an explicitly prohibited WebM satisfied the video contract');+ const gifWithoutPng = agent._parseWorkflowAttachmentRequirement('one GIF and no PNG images');+ assert.equal(gifWithoutPng.wantsImage, false,+ AgentClass.name + ': a negated PNG clause created positive image intent');+ assert.equal(gifWithoutPng.wantsGif, true,+ AgentClass.name + ': the affirmative GIF lost positive media intent');+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(+ { value: 'one GIF and no PNG images' }, { attachments: [gif(1)] }), true,+ AgentClass.name + ': a GIF-only publication was rejected by a negated PNG clause');+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(+ { value: 'one GIF and no PNG images' }, { attachments: [gif(1), image(1)] }), false,+ AgentClass.name + ': an image survived beside the required GIF despite the PNG prohibition');+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(+ { value: 'one GIF and no PNG images' }, { attachments: [image(1)] }), false,+ AgentClass.name + ': an image without the required GIF satisfied the GIF contract');+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(+ { value: 'no PNG or JPEG images' }, { attachments: [] }), true,+ AgentClass.name + ': the valid empty set was rejected by a format-only prohibition');+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(+ { value: 'no PNG or JPEG images' }, { attachments: [{ type: 'image', src: 'https://cdn.example/a.jpg' }] }), false,+ AgentClass.name + ': an explicitly prohibited JPEG satisfied a format-only prohibition');const pngOrJpegImage = agent._parseWorkflowAttachmentRequirement('one PNG or JPEG image');assert.equal(pngOrJpegImage.isGeneric, true,AgentClass.name + ': a coordinated image-format choice was parsed as filenames');