From e6dd9978cb923168b4a267cabdcc838ff5f04676 Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Fri, 7 Mar 2025 16:00:40 -0800 Subject: [PATCH] refactor: Improve reasoning tag replacement with consistent newline handling --- aider/coders/base_coder.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index c91298ebf..c79d6a833 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -1695,6 +1695,7 @@ class Coder: def replace_reasoning_tags(self, text, tag_name): """ Replace opening and closing reasoning tags with standard formatting. + Ensures exactly one blank line before START and END markers. Args: text (str): The text containing the tags @@ -1706,12 +1707,24 @@ class Coder: if not text: return text - # Replace opening tag - text = re.sub(f"<{tag_name}>", REASONING_START, text) - - # Replace closing tag - text = re.sub(f"", REASONING_END, text) + # Helper function to ensure exactly one blank line before replacement + def ensure_one_blank_line(match): + content_before = match.string[:match.start()].rstrip() + # If we're not at the start of the text, add exactly one blank line + if content_before: + return f"{content_before}\n\n{match.group(1)}" + # If we're at the start, don't add extra newlines + return match.group(1) + # Replace opening tag with proper spacing + text = re.sub(f"\\s*<{tag_name}>\\s*", f"\n\n{REASONING_START}\n", text) + + # Replace closing tag with proper spacing + text = re.sub(f"\\s*\\s*", f"\n\n{REASONING_END}", text) + + # Clean up any excessive newlines (more than 2 consecutive) + text = re.sub(r'\n{3,}', '\n\n', text) + return text def show_send_output(self, completion):