replace_lines writes twice, so a failed insert leaves the deletion behind
replace_lines performed one replacement as two separate durable writes, so a failure in the second write leaves the first one behind.
# src/serena/repl/api/edit_api.py
code_editor = self._create_code_editor()
code_editor.delete_lines(relative_path, start_line, end_line)
code_editor.insert_at_line(relative_path, start_line, self._normalize_inserted_content(content))Each of those opens its own edited_file_context, which reads the file and saves it on exit. So if the second one fails — an encoding error for a file whose project_config.encoding is not UTF-8, a full disk, a language-server SolidLSPException — the caller gets an error while the file on disk has already lost start_line..end_line. tools_base.py then retries the whole tool call when the language server died, which runs delete_lines a second time against the already-shortened file and removes a different range.
This is the only tool in the repository that splits one user request into two saves. replace_content and replace_symbol_body both compute the result inside one EditedFileContext / edited_file_context and write once, and so does MultiFileReplacement.apply.
Reproduction
main at 7a296833, with only a test added and no source change:
FAILED test/serena/test_edit_api.py::test_replace_lines_keeps_the_file_when_the_insert_fails
1 failed, 4 passed
E AssertionError: assert 'one\nthree\nfour\n' == 'one\ntwo\nthree\nfour\n'The file had lost lines 1 and 2 while the call raised. The test drives EditApi.replace_lines against a stub CodeEditor that applies the same TextUtils primitives the SolidLSP and JetBrains backends use, so it does not need a language server to run.
The fix
Add CodeEditor.replace_lines, which deletes the range and inserts the replacement inside a single edited_file_context, mirroring replace_symbol_body. The line-range arithmetic moves into a small _delete_line_range helper that delete_lines then shares, so there is still one place that turns (start_line, end_line) into positions. The result of a successful replacement is unchanged — only the number of writes is.
On the branch, with that change in place:
$ pytest test/serena/test_edit_api.py -q
5 passed
$ pytest test/solidlsp/test_text_utils.py test/serena/test_code_editor_atomic_writes.py test/serena/test_file_tools.py -q
44 passed
$ ruff check src/serena/code_editor.py src/serena/repl/api/edit_api.py test/serena/test_edit_api.py
All checks passed!pytest test/serena -q gives 11 failed, 884 passed, 8 skipped, 20 errors, and that line is identical on main and on the branch — I diffed the two failure sets. They are language servers missing in my environment (.NET runtime, gopls, pwsh, the vue/ts servers), so the end-to-end tool path is not covered by my runs; that limitation is why I am describing the change here rather than claiming it is verified end to end.
Why an issue rather than a pull request
My branch with the fix and the test is at feiiiiii5/serena → fix/replace-lines-atomic (d9af4955), including a CHANGELOG.md entry, if that is useful to cherry-pick. I tried to open the PR four times today and each attempt was refused before creation with feiiiiii5 does not have the correct permissions to execute CreatePullRequest, while the repository reports pull_request_creation_policy: all and I have opened PRs against other repositories with the same account in the same session. I have not found a policy in CONTRIBUTING.md that would explain it — small bug fixes are listed as welcome directly by pull request — so I am treating it as an account-side state I cannot change and reporting here instead. If a pull request is preferred, the branch is ready and I will open it as soon as I can.
Source: oraios/serena