Skip to content

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

fix(social-publish): preserve scoped media conjuncts

Barack Sokullu3d2014b2ad62
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 328ab97b6..feaaf628a 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -315,7 +315,7 @@ const GENERIC_ATTACHMENT_WORDS = new Set([
'item', 'items', 'piece', 'pieces', 'upload', 'uploads', 'asset', 'assets',
'enclosure', 'enclosures', 'document', 'documents', 'documento', 'documentos',
'a', 'an', 'the', 'of', 'in', 'with', 'some', 'any',
- 'and', 'but', 'or', 'either', 'plus', 'also', 'as', 'well', 'between', 'from', 'to',
+ 'and', 'but', 'or', 'either', 'neither', 'nor', 'plus', 'also', 'as', 'well', 'between', 'from', 'to',
'no', 'not', 'without', 'zero', 'none',
'only', 'just', 'solely', 'exactly', 'exact', 'precisely',
'sin', 'sans', 'sem', 'senza', 'ohne', 'kein', 'keine', 'keinen', 'aucun', 'aucune', 'ningun', 'ninguna', 'ningún', 'nenhum', 'nenhuma', 'nessun', 'nessuno', 'nessuna', 'nie', 'без', 'нет',
@@ -3089,7 +3089,7 @@ export class Agent extends LoopDetector {
const coordinatedMediaNoun = '(?:images?|photos?|pictures?|pics?|videos?|clips?|recordings?|gifs?|animated[-_ ]gifs?)';
const negationText = normalizeAttachmentNegationArticles(text);
const coordinatedMediaListNegation = negationText.match(new RegExp(
- `(?<![${SOCIAL_WORD_EDGE}])(?:no|not|without(?:\\s+any)?)\\s+(?:any\\s+)?(${coordinatedMediaNoun}(?:(?:\\s*[,;]\\s*(?:(?:and|or|nor)\\s+)?|\\s+(?:and|or|nor)\\s+)(?:any\\s+)?${coordinatedMediaNoun})+)`,
+ `(?<![${SOCIAL_WORD_EDGE}])(?:no|not|without(?:\\s+any)?|neither)\\s+(?:(?:any|a|an|the)\\s+)?(${coordinatedMediaNoun}(?:(?:\\s*[,;]\\s*(?:(?:and|or|nor)\\s+)?|\\s+(?:and|or|nor)\\s+)(?:(?:any|a|an|the)\\s+)?${coordinatedMediaNoun})+)`,
'iu',
));
const coordinatedNegatedMedia = coordinatedMediaListNegation?.[1] || '';
@@ -3624,7 +3624,43 @@ export class Agent extends LoopDetector {
: commaMediaBranches;
const hasEnumeratedMediaChoice = enumeratedMediaBranches.length > 1;
const mediaAlternativeBranchTexts = hasEnumeratedMediaChoice ? enumeratedMediaBranches : [];
- if (!hasEnumeratedMediaChoice && hasUnscopedDisjunction) {
+ const scopedEitherMatch = isGeneric
+ ? unscopedDisjunctionText.match(new RegExp(`(?<![${SOCIAL_WORD_EDGE}])either(?![${SOCIAL_WORD_EDGE}])`, 'iu'))
+ : null;
+ const disjunctionsBeforeEither = scopedEitherMatch
+ ? unscopedDisjunctionMatches.filter(match => (match.index || 0) < (scopedEitherMatch.index || 0))
+ : [];
+ const scopedEitherDisjunctions = scopedEitherMatch && disjunctionsBeforeEither.length === 0
+ ? unscopedDisjunctionMatches.filter(match => (
+ (match.index || 0) >= (scopedEitherMatch.index || 0) + scopedEitherMatch[0].length
+ ))
+ : [];
+ if (!hasEnumeratedMediaChoice && scopedEitherDisjunctions.length > 0) {
+ const eitherStart = scopedEitherMatch.index || 0;
+ const choiceStart = eitherStart + scopedEitherMatch[0].length;
+ const lastDisjunction = scopedEitherDisjunctions[scopedEitherDisjunctions.length - 1];
+ const afterLastDisjunction = text.slice((lastDisjunction.index || 0) + lastDisjunction[0].length);
+ const suffixMatch = afterLastDisjunction.match(/^([\s\S]*?)\s*[,;]\s*(?:and|plus|also|as\s+well\s+as)\s+([\s\S]+)$/iu);
+ const choiceEnd = suffixMatch
+ ? (lastDisjunction.index || 0) + lastDisjunction[0].length + suffixMatch[1].length
+ : text.length;
+ const sharedPrefix = text.slice(0, eitherStart)
+ .replace(/(?:\s*[,;]\s*)?(?:and|plus|also|as\s+well\s+as)\s*$/iu, '')
+ .trim();
+ const sharedSuffix = suffixMatch?.[2]?.trim() || '';
+ let branchStart = choiceStart;
+ const choices = [];
+ for (const match of scopedEitherDisjunctions) {
+ if ((match.index || 0) >= choiceEnd) break;
+ choices.push(text.slice(branchStart, match.index || 0).trim());
+ branchStart = (match.index || 0) + match[0].length;
+ }
+ choices.push(text.slice(branchStart, choiceEnd).trim());
+ for (const choice of choices.filter(Boolean)) {
+ mediaAlternativeBranchTexts.push([sharedPrefix, choice, sharedSuffix].filter(Boolean).join(' and '));
+ }
+ }
+ if (!hasEnumeratedMediaChoice && mediaAlternativeBranchTexts.length === 0 && hasUnscopedDisjunction) {
let branchStart = 0;
for (const match of unscopedDisjunctionMatches) {
mediaAlternativeBranchTexts.push(text.slice(branchStart, match.index || 0).trim());
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 325ba7925..a505c1629 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -249,7 +249,7 @@ const GENERIC_ATTACHMENT_WORDS = new Set([
'item', 'items', 'piece', 'pieces', 'upload', 'uploads', 'asset', 'assets',
'enclosure', 'enclosures', 'document', 'documents', 'documento', 'documentos',
'a', 'an', 'the', 'of', 'in', 'with', 'some', 'any',
- 'and', 'but', 'or', 'either', 'plus', 'also', 'as', 'well', 'between', 'from', 'to',
+ 'and', 'but', 'or', 'either', 'neither', 'nor', 'plus', 'also', 'as', 'well', 'between', 'from', 'to',
'no', 'not', 'without', 'zero', 'none',
'only', 'just', 'solely', 'exactly', 'exact', 'precisely',
'sin', 'sans', 'sem', 'senza', 'ohne', 'kein', 'keine', 'keinen', 'aucun', 'aucune', 'ningun', 'ninguna', 'ningún', 'nenhum', 'nenhuma', 'nessun', 'nessuno', 'nessuna', 'nie', 'без', 'нет',
@@ -2804,7 +2804,7 @@ export class Agent extends LoopDetector {
const coordinatedMediaNoun = '(?:images?|photos?|pictures?|pics?|videos?|clips?|recordings?|gifs?|animated[-_ ]gifs?)';
const negationText = normalizeAttachmentNegationArticles(text);
const coordinatedMediaListNegation = negationText.match(new RegExp(
- `(?<![${SOCIAL_WORD_EDGE}])(?:no|not|without(?:\\s+any)?)\\s+(?:any\\s+)?(${coordinatedMediaNoun}(?:(?:\\s*[,;]\\s*(?:(?:and|or|nor)\\s+)?|\\s+(?:and|or|nor)\\s+)(?:any\\s+)?${coordinatedMediaNoun})+)`,
+ `(?<![${SOCIAL_WORD_EDGE}])(?:no|not|without(?:\\s+any)?|neither)\\s+(?:(?:any|a|an|the)\\s+)?(${coordinatedMediaNoun}(?:(?:\\s*[,;]\\s*(?:(?:and|or|nor)\\s+)?|\\s+(?:and|or|nor)\\s+)(?:(?:any|a|an|the)\\s+)?${coordinatedMediaNoun})+)`,
'iu',
));
const coordinatedNegatedMedia = coordinatedMediaListNegation?.[1] || '';
@@ -3339,7 +3339,43 @@ export class Agent extends LoopDetector {
: commaMediaBranches;
const hasEnumeratedMediaChoice = enumeratedMediaBranches.length > 1;
const mediaAlternativeBranchTexts = hasEnumeratedMediaChoice ? enumeratedMediaBranches : [];
- if (!hasEnumeratedMediaChoice && hasUnscopedDisjunction) {
+ const scopedEitherMatch = isGeneric
+ ? unscopedDisjunctionText.match(new RegExp(`(?<![${SOCIAL_WORD_EDGE}])either(?![${SOCIAL_WORD_EDGE}])`, 'iu'))
+ : null;
+ const disjunctionsBeforeEither = scopedEitherMatch
+ ? unscopedDisjunctionMatches.filter(match => (match.index || 0) < (scopedEitherMatch.index || 0))
+ : [];
+ const scopedEitherDisjunctions = scopedEitherMatch && disjunctionsBeforeEither.length === 0
+ ? unscopedDisjunctionMatches.filter(match => (
+ (match.index || 0) >= (scopedEitherMatch.index || 0) + scopedEitherMatch[0].length
+ ))
+ : [];
+ if (!hasEnumeratedMediaChoice && scopedEitherDisjunctions.length > 0) {
+ const eitherStart = scopedEitherMatch.index || 0;
+ const choiceStart = eitherStart + scopedEitherMatch[0].length;
+ const lastDisjunction = scopedEitherDisjunctions[scopedEitherDisjunctions.length - 1];
+ const afterLastDisjunction = text.slice((lastDisjunction.index || 0) + lastDisjunction[0].length);
+ const suffixMatch = afterLastDisjunction.match(/^([\s\S]*?)\s*[,;]\s*(?:and|plus|also|as\s+well\s+as)\s+([\s\S]+)$/iu);
+ const choiceEnd = suffixMatch
+ ? (lastDisjunction.index || 0) + lastDisjunction[0].length + suffixMatch[1].length
+ : text.length;
+ const sharedPrefix = text.slice(0, eitherStart)
+ .replace(/(?:\s*[,;]\s*)?(?:and|plus|also|as\s+well\s+as)\s*$/iu, '')
+ .trim();
+ const sharedSuffix = suffixMatch?.[2]?.trim() || '';
+ let branchStart = choiceStart;
+ const choices = [];
+ for (const match of scopedEitherDisjunctions) {
+ if ((match.index || 0) >= choiceEnd) break;
+ choices.push(text.slice(branchStart, match.index || 0).trim());
+ branchStart = (match.index || 0) + match[0].length;
+ }
+ choices.push(text.slice(branchStart, choiceEnd).trim());
+ for (const choice of choices.filter(Boolean)) {
+ mediaAlternativeBranchTexts.push([sharedPrefix, choice, sharedSuffix].filter(Boolean).join(' and '));
+ }
+ }
+ if (!hasEnumeratedMediaChoice && mediaAlternativeBranchTexts.length === 0 && hasUnscopedDisjunction) {
let branchStart = 0;
for (const match of unscopedDisjunctionMatches) {
mediaAlternativeBranchTexts.push(text.slice(branchStart, match.index || 0).trim());
diff --git a/test/run.js b/test/run.js
index b44beff45..45f92a7dc 100644
--- a/test/run.js
+++ b/test/run.js
@@ -93105,6 +93105,48 @@ test('upper-bound attachment qualifiers verify as maximum counts', () => {
assert.equal(agent._workflowSocialPublishedAttachmentObserved(
{ value: 'one image but no video' }, { attachments: [image(1), video(1)] },
), false, AgentClass.name + ': a video passed a contrastive no-video contract');
+ const sharedImageChoice = agent._parseWorkflowAttachmentRequirement(
+ 'one image and either one video or one GIF',
+ );
+ assert.deepEqual(sharedImageChoice.mediaAlternativeBranches.map(branch => branch.normalized), [
+ 'one image and one video',
+ 'one image and one gif',
+ ], AgentClass.name + ': shared media conjunct was not distributed into every scoped alternative');
+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'one image and either one video or one GIF' }, { attachments: [image(1), video(1)] },
+ ), true, AgentClass.name + ': image-plus-video scoped alternative was rejected');
+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'one image and either one video or one GIF' }, { attachments: [image(1), gif(1)] },
+ ), true, AgentClass.name + ': image-plus-GIF scoped alternative was rejected');
+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'one image and either one video or one GIF' }, { attachments: [gif(1)] },
+ ), false, AgentClass.name + ': GIF-only evidence omitted the shared image conjunct');
+ const sharedSuffixChoice = agent._parseWorkflowAttachmentRequirement(
+ 'either one video or one GIF, and one image',
+ );
+ assert.deepEqual(sharedSuffixChoice.mediaAlternativeBranches.map(branch => branch.normalized), [
+ 'one video and one image',
+ 'one gif and one image',
+ ], AgentClass.name + ': shared media suffix was not distributed into every scoped alternative');
+ const neitherVideoNorGif = agent._parseWorkflowAttachmentRequirement(
+ 'one image but neither a video nor a GIF',
+ );
+ assert.equal(neitherVideoNorGif.isGeneric, true,
+ AgentClass.name + ': neither/nor media prohibition was parsed as a filename');
+ assert.equal(neitherVideoNorGif.isVideoNegated, true);
+ assert.equal(neitherVideoNorGif.isGifNegated, true);
+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'one image but neither a video nor a GIF' }, { attachments: [image(1)] },
+ ), true, AgentClass.name + ': image-only evidence failed a neither-video-nor-GIF contract');
+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'one image but neither a video nor a GIF' }, { attachments: [image(1), gif(1)] },
+ ), false, AgentClass.name + ': a GIF passed a neither-video-nor-GIF contract');
+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'neither images nor videos' }, { attachments: [] },
+ ), true, AgentClass.name + ': empty evidence failed a bare neither-images-nor-videos contract');
+ assert.equal(agent._workflowSocialPublishedAttachmentObserved(
+ { value: 'neither images nor videos' }, { attachments: [image(1)] },
+ ), false, AgentClass.name + ': an image passed a bare neither-images-nor-videos contract');
for (const [requirement, makeAttachment, minField, maxField] of [
['at least one image and at most two images', image, 'minimumImageCount', 'maximumImageCount'],
['at least one video and at most two videos', video, 'minimumVideoCount', 'maximumVideoCount'],