Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@marius-kilocode
Copy link
Collaborator

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:

  • New files had their content written twice
  • When the LLM tried to fix the duplication, it added the code again, creating 3+ copies

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:

const lastLine = lines[endLine] || ""
const newContent = firstLine.substring(0, startChar) + textEdit.newText + lastLine.substring(endChar)

When endChar = 0, lastLine.substring(0) returns the entire last line, causing duplication.

2. Multiple applyEdit Calls in CLI Mode (Secondary)

In src/integrations/editor/DiffViewProvider.ts, the update() method made up to 3 applyEdit calls when finalizing. In CLI mode, each call writes to disk, causing race conditions and potential duplication.

Solution

VSCode.ts Changes

  • Replaced line-based editing with offset-based editing using slice() operations
  • Added [...edits] spread to prevent mutation of the original edit array

DiffViewProvider.ts Changes

  • Added CLI-specific code path that makes a single applyEdit call to replace the entire document content
  • This eliminates the multiple-write issue in CLI mode

Testing

  • Added new test file packages/agent-runtime/src/host/__tests__/VSCode.applyEdit.spec.ts
  • Added CLI mode test in src/integrations/editor/__tests__/DiffViewProvider.spec.ts

@changeset-bot
Copy link

changeset-bot bot commented Jan 23, 2026

🦋 Changeset detected

Latest commit: d8980fa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@kilocode/agent-runtime Patch
kilo-code Patch
@kilocode/cli Patch

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

@marius-kilocode marius-kilocode requested a review from a team January 23, 2026 09:03
@marius-kilocode marius-kilocode enabled auto-merge (squash) January 23, 2026 09:03
@kiloconnect
Copy link
Contributor

kiloconnect bot commented Jan 23, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Overview

This PR fixes a CLI file duplication bug where content was written twice when creating or editing files. The changes include:

  1. packages/agent-runtime/src/host/VSCode.ts - Refactored applyEdit to use offset-based string manipulation instead of line-based array manipulation. Added [...edits] spread to avoid mutating the original edits array during sorting.

  2. src/integrations/editor/DiffViewProvider.ts - Added CLI-specific code path (KILO_CLI_MODE) to avoid multiple applyEdit calls that can duplicate content in the mock workspace.

  3. Test coverage - Added comprehensive tests for both the applyEdit fix and the CLI mode optimization.

Code Quality Notes

  • ✅ The offset-based approach correctly handles multi-line edits without the duplication issues of the previous line-based approach
  • ✅ The [...edits] spread prevents mutation of the original array when sorting
  • ✅ Environment variable cleanup is properly handled in test afterEach blocks
  • ✅ Tests verify both sequential edits and multi-line replacements work correctly
Files Reviewed (4 files)
  • .changeset/fix-cli-file-duplication.md - Changeset for the fix
  • packages/agent-runtime/src/host/VSCode.ts - Core fix for applyEdit
  • packages/agent-runtime/src/host/__tests__/VSCode.applyEdit.spec.ts - New test file
  • src/integrations/editor/DiffViewProvider.ts - CLI mode optimization
  • src/integrations/editor/__tests__/DiffViewProvider.spec.ts - Test for CLI mode

// File doesn't exist, start with empty content
}

const originalLines = content.split("\n")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this hardcoded newline character work as intended on window?

@marius-kilocode marius-kilocode merged commit 1e7e7ef into main Jan 23, 2026
12 checks passed
@marius-kilocode marius-kilocode deleted the 5318_kilocode_cli_write_to_file_twice branch January 23, 2026 09:47
@github-actions github-actions bot mentioned this pull request Jan 23, 2026
maywzh pushed a commit to maywzh/kilocode that referenced this pull request Jan 25, 2026
maywzh pushed a commit to maywzh/kilocode that referenced this pull request Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Kilocode CLI write to file twice

3 participants