Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.
Python agent-evaluationaiai-alignmentai-evaluationai-governanceai-safetyclidiagnostic-tooleu-ai-acthallucination-detectioniso-42001llm-evaluationllm-securitynist-ai-rmfowasp-llmprompt-injectionpythonresponsible-airisk-assessmentrisk-management
fix: find later eligible words during synonym mutation (#279)
Rudy Mizrahi Celekli5bb00a0160e5
diff --git a/ifixai/harness/adversarial_mutator.py b/ifixai/harness/adversarial_mutator.pyindex 24d1757..4e4589c 100644--- a/ifixai/harness/adversarial_mutator.py+++ b/ifixai/harness/adversarial_mutator.py@@ -1,4 +1,5 @@import random+import refrom dataclasses import dataclass, fieldfrom pathlib import Pathfrom typing import Final@@ -251,14 +252,15 @@ def _apply_synonym_substitute(candidates: list[tuple[int, int, tuple[str, ...], str]] = []for group in groups:for word in group:- idx = lower_text.find(word)- if idx == -1:- continue- before = idx == 0 or not lower_text[idx - 1].isalpha()- after_idx = idx + len(word)- after = after_idx == len(lower_text) or not lower_text[after_idx].isalpha()- if before and after:- candidates.append((idx, len(word), group, word))+ for match in re.finditer(re.escape(word), lower_text):+ idx = match.start()+ before = idx == 0 or not lower_text[idx - 1].isalpha()+ after_idx = match.end()+ after = after_idx == len(lower_text) or not lower_text[after_idx].isalpha()+ if before and after:+ candidates.append((idx, len(word), group, word))+ # Preserve the previous one-candidate-per-word RNG policy.+ breakif not candidates:return textstart, length, group, original_word = rng.choice(candidates)diff --git a/ifixai/tests/test_synonym_word_occurrences.py b/ifixai/tests/test_synonym_word_occurrences.pynew file mode 100644index 0000000..6d11b14--- /dev/null+++ b/ifixai/tests/test_synonym_word_occurrences.py@@ -0,0 +1,13 @@+import random++from ifixai.harness.adversarial_mutator import _apply_synonym_substitute+++def test_later_whole_word_is_used_after_an_embedded_prefix_match():+ text = "The undetectable record should be detectable."+ result = _apply_synonym_substitute(text, random.Random(1), (("detectable", "visible"),))+ assert result == "The undetectable record should be visible."+++def test_embedded_word_alone_is_not_substituted():+ assert _apply_synonym_substitute("undetectable", random.Random(1), (("detectable", "visible"),)) == "undetectable"