Agent Zero AI framework
Fix prompt fence stripping policy
Rafael Uzarowskicc7af97ed35a
diff --git a/agent.py b/agent.pyindex f6001098c..0f53c6549 100644--- a/agent.py+++ b/agent.py@@ -564,7 +564,7 @@ class Agent:self.handle_critical_exception(e)error_message = errors.format_error(e)-+self.context.log.log(type="warning", content="Critical error occurred, retrying...")@@ -628,7 +628,10 @@ class Agent:def read_prompt(self, file: str, **kwargs) -> str:dirs = subagents.get_paths(self, "prompts")prompt = files.read_prompt_file(file, _directories=dirs, _agent=self, **kwargs)- prompt = files.remove_code_fences(prompt)+ # Only strip code fences when the *entire* prompt is a JSON template (e.g. fw.initial_message.md),+ # so embedded fenced examples in markdown prompts remain intact.+ if files.is_full_json_template(prompt):+ prompt = files.remove_code_fences(prompt)return promptdef get_data(self, field: str):diff --git a/python/helpers/files.py b/python/helpers/files.pyindex 0ed9cb06d..ac2e51a9e 100644--- a/python/helpers/files.py+++ b/python/helpers/files.py@@ -96,7 +96,10 @@ def parse_file(content = f.read()is_json = is_full_json_template(content)- content = remove_code_fences(content)+ # Only strip code fences for full JSON templates - embedded fenced blocks in markdown prompts+ # should remain intact, otherwise examples/instructions lose structure.+ if is_json:+ content = remove_code_fences(content)variables = load_plugin_variables(absolute_path, _directories, **kwargs) or {} # type: ignorevariables.update(kwargs)if is_json:@@ -333,17 +336,16 @@ def get_unique_filenames_in_dirs(dir_paths: list[str], pattern: str = "*"):def remove_code_fences(text):- # Pattern to match code fences with optional language specifier- pattern = r"(```|~~~)(.*?\n)(.*?)(\1)"+ # Strip fenced blocks (``` / ~~~) only when the fence marker is at the start of a line.+ #+ # This prevents accidental stripping when the fence marker appears inline, e.g.+ # "... do not wrap ~~~markdown" (which should remain literal text).+ pattern = r"(?ms)^[ \t]*(```|~~~)[^\n]*\n(.*?)(?:^[ \t]*\1[ \t]*$)"- # Function to replace the code fencesdef replacer(match):- return match.group(3) # Return the code without fences-- # Use re.DOTALL to make '.' match newlines- result = re.sub(pattern, replacer, text, flags=re.DOTALL)+ return match.group(2) # Return the code without fences- return result+ return re.sub(pattern, replacer, text)def is_full_json_template(text):@@ -597,4 +599,3 @@ def list_files_in_dir_recursively(relative_path: str) -> list[str]:rel_path = os.path.relpath(file_path, abs_path)result.append(rel_path)return result-\ No newline at end of filediff --git a/tests/test_prompt_fence_policy.py b/tests/test_prompt_fence_policy.pynew file mode 100644index 000000000..624aedee9--- /dev/null+++ b/tests/test_prompt_fence_policy.py@@ -0,0 +1,40 @@+import sys+import json+from pathlib import Path+from types import SimpleNamespace++PROJECT_ROOT = Path(__file__).resolve().parents[1]+if str(PROJECT_ROOT) not in sys.path:+ sys.path.insert(0, str(PROJECT_ROOT))++from agent import Agent+++class _DummyContext:+ def get_data(self, key: str, recursive: bool = True):+ return None+++def _dummy_agent(profile: str = "agent0"):+ # Agent.read_prompt only needs config.profile and context.get_data() to resolve prompt paths.+ return SimpleNamespace(config=SimpleNamespace(profile=profile), context=_DummyContext())+++def test_agent_read_prompt_preserves_embedded_fenced_examples_in_markdown_prompts():+ dummy = _dummy_agent()+ text = Agent.read_prompt(dummy, "agent.system.tool.response.md")++ assert "usage:" in text+ assert "~~~json" in text+ assert "~~~" in text+++def test_agent_read_prompt_strips_full_json_template_fences_for_json_consumers():+ dummy = _dummy_agent()+ text = Agent.read_prompt(dummy, "fw.initial_message.md")++ assert "```" not in text+ assert "~~~" not in text++ parsed = json.loads(text)+ assert parsed.get("tool_name") == "response"diff --git a/tests/test_remove_code_fences.py b/tests/test_remove_code_fences.pynew file mode 100644index 000000000..4ea7d114d--- /dev/null+++ b/tests/test_remove_code_fences.py@@ -0,0 +1,29 @@+import sys+from pathlib import Path++PROJECT_ROOT = Path(__file__).resolve().parents[1]+if str(PROJECT_ROOT) not in sys.path:+ sys.path.insert(0, str(PROJECT_ROOT))++from python.helpers.files import remove_code_fences+++def test_remove_code_fences_does_not_strip_inline_tildes_or_join_lines():+ src = (+ "full message is automatically markdown do not wrap ~~~markdown\n"+ "use emojis as icons improve readability\n"+ "usage:\n"+ "~~~json\n"+ "{\n"+ ' \"a\": 1\n'+ "}\n"+ "~~~\n"+ )++ out = remove_code_fences(src)++ assert "wrap ~~~markdown\nuse emojis" in out+ assert "wrap ~~~markdownuse emojis" not in out+ assert "~~~json" not in out+ assert "\n~~~\n" not in out+ assert '"a": 1' in out