fix: CLI file duplication bug when writing files (#5318) - #5340
Conversation
🦋 Changeset detectedLatest commit: d8980fa The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Code Review SummaryStatus: No Issues Found | Recommendation: Merge OverviewThis PR fixes a CLI file duplication bug where content was written twice when creating or editing files. The changes include:
Code Quality Notes
Files Reviewed (4 files)
|
| // File doesn't exist, start with empty content | ||
| } | ||
|
|
||
| const originalLines = content.split("\n") |
There was a problem hiding this comment.
does this hardcoded newline character work as intended on window?
Co-authored-by: Ingo Fruend <ingo@oudyo.com>
Co-authored-by: Ingo Fruend <ingo@oudyo.com>
Summary
Fixes #5318 - CLI writes file content twice when creating or editing files.
Problem
When the Kilo Code CLI creates or edits files, the content was being duplicated at the end of the file. This was visible in logs and caused issues where:
Root Causes
1. Multi-line Edit Logic in
applyEdit(Primary)In
packages/agent-runtime/src/host/VSCode.ts, the line-based edit logic had a bug:When
endChar = 0,lastLine.substring(0)returns the entire last line, causing duplication.2. Multiple
applyEditCalls in CLI Mode (Secondary)In
src/integrations/editor/DiffViewProvider.ts, theupdate()method made up to 3applyEditcalls when finalizing. In CLI mode, each call writes to disk, causing race conditions and potential duplication.Solution
VSCode.ts Changes
slice()operations[...edits]spread to prevent mutation of the original edit arrayDiffViewProvider.ts Changes
applyEditcall to replace the entire document contentTesting
packages/agent-runtime/src/host/__tests__/VSCode.applyEdit.spec.tssrc/integrations/editor/__tests__/DiffViewProvider.spec.ts