fix(tools): keep apply_patch additions line-separated

This commit is contained in:
yu-xin-c 2026-06-09 22:50:08 +08:00 committed by Xubin Ren
parent 1b5f5b94d5
commit fd9fc38f41
2 changed files with 76 additions and 3 deletions

View File

@ -75,6 +75,18 @@ def _line_diff_stats(before: str, after: str) -> tuple[int, int]:
return added, deleted return added, deleted
def _append_text(content: str, addition: str) -> str:
"""Append text without merging it into an unterminated final line."""
base = content.replace("\r\n", "\n")
extra = addition.replace("\r\n", "\n")
if base and extra and not base.endswith("\n") and not extra.startswith("\n"):
base += "\n"
combined = base + extra
if combined and not combined.endswith("\n"):
combined += "\n"
return combined
def _format_summary(summary: _PatchSummary) -> str: def _format_summary(summary: _PatchSummary) -> str:
stats = "" stats = ""
if summary.added or summary.deleted: if summary.added or summary.deleted:
@ -177,9 +189,7 @@ class ApplyPatchTool(_FsTool):
if exists: if exists:
uses_crlf = "\r\n" in content uses_crlf = "\r\n" in content
new_norm = content.replace("\r\n", "\n") + new_text.replace("\r\n", "\n") new_norm = _append_text(content, new_text)
if new_norm and not new_norm.endswith("\n"):
new_norm += "\n"
if uses_crlf: if uses_crlf:
new_norm = new_norm.replace("\n", "\r\n") new_norm = new_norm.replace("\n", "\r\n")
writes[source] = new_norm writes[source] = new_norm

View File

@ -89,6 +89,69 @@ def test_apply_patch_edits_add_to_existing_file(tmp_path):
) )
def test_apply_patch_edits_add_to_existing_file_without_final_newline(tmp_path):
target = tmp_path / "notes.txt"
target.write_text("alpha", encoding="utf-8")
tool = ApplyPatchTool(workspace=tmp_path)
result = asyncio.run(
tool.execute(
edits=[
{
"path": "notes.txt",
"action": "add",
"new_text": "beta",
}
]
)
)
assert "update notes.txt" in result
assert target.read_text(encoding="utf-8") == "alpha\nbeta\n"
def test_apply_patch_edits_add_to_existing_crlf_file_without_final_newline(tmp_path):
target = tmp_path / "notes.txt"
target.write_bytes(b"alpha\r\nbravo")
tool = ApplyPatchTool(workspace=tmp_path)
result = asyncio.run(
tool.execute(
edits=[
{
"path": "notes.txt",
"action": "add",
"new_text": "charlie",
}
]
)
)
assert "update notes.txt" in result
assert target.read_bytes() == b"alpha\r\nbravo\r\ncharlie\r\n"
def test_apply_patch_edits_add_to_existing_file_respects_leading_newline(tmp_path):
target = tmp_path / "notes.txt"
target.write_text("alpha", encoding="utf-8")
tool = ApplyPatchTool(workspace=tmp_path)
result = asyncio.run(
tool.execute(
edits=[
{
"path": "notes.txt",
"action": "add",
"new_text": "\nbeta",
}
]
)
)
assert "update notes.txt" in result
assert target.read_text(encoding="utf-8") == "alpha\nbeta\n"
def test_apply_patch_rejects_delete_action(tmp_path): def test_apply_patch_rejects_delete_action(tmp_path):
target = tmp_path / "utils.py" target = tmp_path / "utils.py"
target.write_text("def unused():\n pass\ndef used():\n return 1\n") target.write_text("def unused():\n pass\ndef used():\n return 1\n")