fix: retain the original line break style of the file and fix cross-platform CRLF/LF issues#2362
Conversation
|
There is a line-ending edge case worth covering before merge. For CRLF files, both the Example shape: "a\r\nb\r\n".replace("\n", "\r\n") == "a\r\r\nb\r\r\n"This can happen if the tool input already carries CRLF text, or if |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8174eceea8
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
Thanks to all review bots for your feedback. This commit
The above changes have passed all 50 tests (including 12 EOL-related tests). |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5a075014e3
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
Related Issue
Resolve #1952
Resolve #2191
Description
Fix
StrReplaceFileandWriteFiletools corrupting the original newline style when editing files.Root Cause:
readtext()uses universal newlines mode by default (newline=None), which silently converts\r\nto\nwhen reading files;writetext()was already fixed in PR #1693 (by addingnewline=""). This causes the entire read→edit→write pipeline to convert CRLF files to LF, so even if only one line is modified,git diffshows the entire file as changed.Fix Details:
packages/kaos/src/kaos/local.pynewline=""toreadtext()andreadlines()to disable Python's universal newlines auto-conversion, aligning with the already-fixedwritetext().packages/kaos/src/kaos/ssh.pyreadtext()to usereadbytes()+decode()to avoid newline conversion by asyncssh SFTP text mode.splitlines(keepends=True)inreadlines()to preserve trailing newlines.writetext()to byte mode ("wb"/"ab") to prevent newline conversion by the remote server.src/kimi_cli/tools/file/replace.pyStrReplaceFileto read raw bytes and detect the file's dominant newline style (CRLF / LF / CR).\nfor model matching (model-generatedold/newstrings use\n).write_bytes().src/kimi_cli/tools/file/write.pyWriteFile, when in overwrite mode and the file already exists, detect its newline style and write the new content in that style.Test Results:
packages/kaos/tests/test_local_kaos.py: 16 passedChecklist
make gen-changelogto update the changelog.make gen-docsto update the user documentation.