Skip to content

Agent overwrites files using write_file without reading existing content, leading to data loss. #2499

@Thhoho

Description

@Thhoho

What happened?

Summary:
During file editing (especially for .md or large source files), the agent frequently calls write_file to
overwrite the entire file with incorrect or truncated content, skipping the necessary read_file step.

Actual Behavior:
Instead of performing surgical edits, the agent "hallucinates" the current content of a file and writes a new
version over it. This often results in the loss of existing code, documentation, or formatting that wasn't part
of the specific change request.

What did you expect to happen?

Expected Behavior:
The agent should follow a strict "Read-Plan-Act" cycle:

  1. Read: Must read the target file (or relevant sections) to understand the current state.
  2. Plan: Propose the change based on actual content.
  3. Act: Use granular replacement tools (like a string-replace tool) instead of full-file write_file whenever
    possible.

Suggested Improvement:

  1. Introduce a more granular replace tool (similar to Gemini CLI's implementation) that requires matching an
    old_string to ensure the agent has actually read and understood the target area.
  2. Add a guardrail in the system prompt: "Never overwrite a file without reading its current content first."

Client information

Client Information

Run qwen to enter the interactive CLI, then run the /about command.

$ qwen /about
# paste output here

Login information

No response

Anything else we need to know?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    category/toolsTool integration and executiontype/bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions