Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(tui): disable newline translation when restoring files

wncbdjf76b2c46fe58
diff --git a/jiuwenswarm/agents/harness/common/session_ops_service.py b/jiuwenswarm/agents/harness/common/session_ops_service.py
index 973ade9248..7662e2a5a5 100644
--- a/jiuwenswarm/agents/harness/common/session_ops_service.py
+++ b/jiuwenswarm/agents/harness/common/session_ops_service.py
@@ -586,7 +586,9 @@ def restore_session_files(
if info["action"] == "write":
# 文件在目标 turn 前已有内容,写回 old_content
path.parent.mkdir(parents=True, exist_ok=True)
- path.write_text(info["restore_content"], encoding="utf-8")
+ path.write_text(
+ info["restore_content"], encoding="utf-8", newline=""
+ )
restored.append(file_path)
elif info["action"] == "delete":
# 文件由 agent 在目标 turn 后创建,删除
diff --git a/tests/unit_tests/test_restore_session_files_newline.py b/tests/unit_tests/test_restore_session_files_newline.py
new file mode 100644
index 0000000000..021fcc2643
--- /dev/null
+++ b/tests/unit_tests/test_restore_session_files_newline.py
@@ -0,0 +1,62 @@
+"""Regression tests for /rewind file restore line-ending preservation (issue #2244)."""
+
+from __future__ import annotations
+
+from pathlib import Path
+from unittest.mock import MagicMock, patch
+
+from jiuwenswarm.agents.harness.common.session_ops_service import restore_session_files
+
+
+def test_restore_session_files_preserves_crlf_bytes(tmp_path: Path) -> None:
+ """CRLF old_content must be written back verbatim, not translated to CRCRLF."""
+ target = tmp_path / "notes.txt"
+ target.write_bytes(b"MODIFIED-A\r\nMODIFIED-B\r\n")
+
+ original_crlf = "ORIGINAL-A\r\nORIGINAL-B\r\n"
+ files_to_restore = {
+ str(target): {
+ "restore_content": original_crlf,
+ "action": "write",
+ }
+ }
+
+ mock_diff = MagicMock()
+ mock_diff.get_files_to_restore.return_value = files_to_restore
+
+ with patch(
+ "jiuwenswarm.server.utils.diff_service.get_diff_service",
+ return_value=mock_diff,
+ ):
+ result = restore_session_files(session_id="sess-1", turn_index=1)
+
+ assert result["errors"] == []
+ assert str(target) in result["restored_files"]
+ assert target.read_bytes() == original_crlf.encode("utf-8")
+ assert b"\r\r\n" not in target.read_bytes()
+
+
+def test_restore_session_files_preserves_lf_bytes(tmp_path: Path) -> None:
+ """LF old_content must stay LF (not rewritten to platform linesep)."""
+ target = tmp_path / "notes.txt"
+ target.write_bytes(b"MODIFIED-A\nMODIFIED-B\n")
+
+ original_lf = "ORIGINAL-A\nORIGINAL-B\n"
+ files_to_restore = {
+ str(target): {
+ "restore_content": original_lf,
+ "action": "write",
+ }
+ }
+
+ mock_diff = MagicMock()
+ mock_diff.get_files_to_restore.return_value = files_to_restore
+
+ with patch(
+ "jiuwenswarm.server.utils.diff_service.get_diff_service",
+ return_value=mock_diff,
+ ):
+ result = restore_session_files(session_id="sess-1", turn_index=1)
+
+ assert result["errors"] == []
+ assert target.read_bytes() == original_lf.encode("utf-8")