From 1216e96038938d2165de16bbfff9d37b47fe3f43 Mon Sep 17 00:00:00 2001 From: wuyang <5700876+banisherwy@user.noreply.gitee.com> Date: Sun, 26 Jul 2026 14:37:56 +0800 Subject: [PATCH] fix: decode repeated source layout escapes --- agent_platform/runtime/loop.py | 21 +++++++++++++-------- tests/test_agent_loop.py | 12 ++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/agent_platform/runtime/loop.py b/agent_platform/runtime/loop.py index cdfbc58..f5cd524 100644 --- a/agent_platform/runtime/loop.py +++ b/agent_platform/runtime/loop.py @@ -257,19 +257,22 @@ def decode_code_layout_escapes(content: str) -> str: index += 1 continue - if content.startswith("\\r\\n", index): + escaped_crlf = re.match(r"\\+r\\+n", content[index:]) + if escaped_crlf: output.append("\n") - index += 4 + index += len(escaped_crlf.group(0)) line_comment = False continue - if content.startswith("\\n", index): + escaped_newline = re.match(r"\\+n", content[index:]) + if escaped_newline: output.append("\n") - index += 2 + index += len(escaped_newline.group(0)) line_comment = False continue - if content.startswith("\\t", index): + escaped_tab = re.match(r"\\+t", content[index:]) + if escaped_tab: output.append("\t") - index += 2 + index += len(escaped_tab.group(0)) continue if line_comment: if content[index] == "\n": @@ -297,13 +300,15 @@ def decode_code_layout_escapes(content: str) -> str: def normalize_write_file_content(path: str, content: str) -> str: literal_newlines = content.count("\\n") + content.count("\\r\\n") - actual_newlines = content.count("\n") - if literal_newlines < 2 or literal_newlines <= max(2, actual_newlines * 2): + if literal_newlines < 2: return content suffix = PurePosixPath(path).suffix.lower() if suffix in CODE_FILE_SUFFIXES: return decode_code_layout_escapes(content) if suffix in PLAIN_TEXT_FILE_SUFFIXES: + actual_newlines = content.count("\n") + if literal_newlines <= max(2, actual_newlines * 2): + return content return content.replace("\\r\\n", "\n").replace("\\n", "\n").replace("\\t", "\t") return content diff --git a/tests/test_agent_loop.py b/tests/test_agent_loop.py index f1d29e7..cd61cd7 100644 --- a/tests/test_agent_loop.py +++ b/tests/test_agent_loop.py @@ -34,6 +34,18 @@ def test_write_file_normalizes_double_escaped_code_layout() -> None: ) +def test_write_file_normalizes_mixed_code_layout_without_touching_strings() -> None: + escaped = 'import time\nvalue = "keep\\\\ninside"\\nprint(value)\\n' + assert normalize_write_file_content("benchmark.py", escaped) == ( + 'import time\nvalue = "keep\\\\ninside"\nprint(value)\n' + ) + + +def test_write_file_consumes_multiple_layout_escape_layers() -> None: + escaped = r"import time\\n\\ndef main():\\n print('ok')\\n" + assert normalize_write_file_content("benchmark.py", escaped) == ("import time\n\ndef main():\n print('ok')\n") + + def test_write_file_normalizes_double_escaped_plain_text() -> None: assert normalize_write_file_content("report.md", r"# Report\n\nMeasured: 1.2 s\n") == ( "# Report\n\nMeasured: 1.2 s\n"