AI & Agents

How to Prevent Cline Agents from Overwriting Code

Preventing Cline from overwriting code requires a combination of strict instructions in .clinerules, prompting the model to use incremental replacement tools, and git-based safety checks. This guide explains how to secure files using versioned team workspaces like Fastio to checkpoint and restore files.

Fast.io Editorial Team 9 min read
Prevent accidental code loss by configuring Cline rules, disabling auto-approvals, and utilizing version-controlled workspaces.

How to Prevent Cline Agents from Overwriting Code

When working with an AI coding agent like Cline, developers often experience situations where the model overwrites an entire file instead of editing a few lines. This issue is not a symptom of a weak language model, but rather a consequence of tool design and user settings. Cline primarily uses 2 file-editing tools: write_to_file and replace_in_file. The write_to_file tool takes a file path and a complete block of text, replacing the entire content of the file on disk. If the agent is working on a long source file, rewriting the entire file consumes a massive number of tokens. When the model encounters output token limits or experiences a minor network glitch, the write operation is cut short. This results in a truncated file, compile errors, and lost source code.

In contrast, the replace_in_file tool is designed for surgical modifications. It searches for a specific block of code and replaces it with a new block, leaving the rest of the file untouched. While replace_in_file is much safer, it requires the model to identify a search block that matches the file content character-for-character. If there is a slight discrepancy in whitespace, indentation, or commenting, the tool fails and returns a match error. When Cline encounters multiple match errors, it often gets frustrated and falls back to write_to_file, replacing the entire file and wiping out surrounding code.

The situation becomes dangerous when developers enable auto-approve write settings. In VS Code, Cline allows developers to auto-approve tool calls to speed up the workflow. When the "Edit project files" permission is auto-approved, the model executes write_to_file or replace_in_file without presenting a diff for human review. If the model chooses the wrong tool or hallucinates a change, the file is overwritten instantly, removing the human-in-the-loop verification that prevents major code loss.

Why Rules Enforce Safer Cline File Operations

The most effective way to prevent Cline from overwriting code is to guide its behavior using a .clinerules file. When you start a coding session, Cline scans the project root directory for a .clinerules file or a .clinerules/ directory containing markdown files. The agent reads these files and appends them to its system prompt, making the instructions part of its primary reasoning loop. By defining strict guidelines for file editing, you can prevent the agent from attempting destructive overwrites.

A production-grade .clinerules configuration should contain explicit instructions that limit write actions and enforce clean editing habits:

### File Editing Rules
- Always prioritize the `replace_in_file` tool for updating existing code. Do not use `write_to_file` on existing files unless you are creating a new file or the target file is under 50 lines.
- Before editing any file, you must call the `read_file` tool to inspect its current contents. This ensures that you have the exact indentation, spacing, and comment style in your context.
- Your search block in `replace_in_file` must match the target content character-for-character. If you receive a match error, do not switch to `write_to_file`. Instead, read the file again, locate the mismatch, and retry the edit with a corrected search block.
- For complex changes, break your edits into multiple, targeted `replace_in_file` calls rather than trying to modify the entire file in one operation.

These rules force the LLM to verify its targets before making modifications. They prevent the model from choosing the lazy path of rewriting the entire file to insert a few lines of code. For teams sharing a repository, storing these files in a version-controlled .clinerules/ directory ensures that every team member benefits from the same agent safety rules.

Steps to Disable Cline Auto-Approve Permissions

While .clinerules provides instructions, you must configure your VS Code settings to establish safety gates. Cline includes an auto-approve feature that allows the model to run tools automatically without prompting you. If you auto-approve file writes, the agent runs in an autonomous mode where it can write to files directly. This setting is the primary cause of automated coding agent overwriting errors.

To disable auto-approve for file writes in the VS Code extension:

  1. Open the Cline panel in your VS Code sidebar.
  2. Click the gear icon in the top right corner of the extension panel to open the settings interface.
  3. Scroll down to the Auto-approve section.
  4. Ensure that the Edit project files option is unchecked.

If this option is checked, the agent can call write_to_file and overwrite code without showing you the changes first. With the option unchecked, Cline is forced to present a side-by-side diff in VS Code. The diff highlights every addition, deletion, and line change. If you notice that the agent is attempting to overwrite the entire file when it should only be modifying a small block of text, you can reject the tool execution. You can then prompt the model to use replace_in_file with a more precise search block.

If you drive Cline through its CLI or SDK instead of the sidebar, review the approval controls exposed by that runtime and leave file edits gated there too. One thing to rule out while you look: approval settings do not live in cline_mcp_settings.json. That file holds Model Context Protocol server definitions only. By keeping file writes set to manual approval, you establish a human gate that stops destructive overwrites before they are written to disk.

Fastio features

Protect your codebase during Cline development runs

Set up a shared Fastio workspace with remote MCP access, automatic version history, and complete audit logging. Protect your files from agent errors with a 14-day free trial.

What Version Control Steps Protect Your Repository

Even with strict rules and disabled auto-approvals, agents can still make errors during a complex coding run. Because of this risk, version control is a developer's first line of defense. You should establish a git workflow that isolates the agent's changes and allows you to restore code instantly.

First, never allow Cline to run on a dirty workspace. Before starting a session with the agent, commit your current changes or stash them. If the agent makes a mistake, corrupts a file, or performs an unwanted overwrite, you can discard all changes in the workspace by running:

git reset --hard HEAD

If you only want to restore a specific file that was overwritten, run:

git restore <filepath>

Second, instruct Cline to work in a dedicated feature branch. By keeping the agent's work isolated from your main branch, you ensure that no unverified changes are merged into production. Once the agent completes its run, you can review the branch using standard code comparison tools and run your local tests before merging.

Third, configure pre-commit hooks to run a linter or test suite on files modified by the agent. If the agent writes invalid syntax or truncates a file, the pre-commit hook will fail, preventing the broken code from being committed. This is especially useful when using coding agents in continuous integration environments, where unverified commits can trigger broken builds. By combining git branch isolation, clean working states, and automated hooks, you create multiple layers of protection against automated coding errors.

Why Versioned Cloud Workspaces Protect Codebases

While git and local storage protect source code files inside a local repository, developers often need to manage files that sit outside version control. These include large datasets, configuration assets, design documents, and project notes. If you rely on standard cloud storage platforms like Google Drive, Dropbox, or Amazon S3, you will find that these services lack the version controls, audit trails, and agent-native APIs required for multi-agent development. When coding agents write to these services, they can overwrite files without leaving a clear trace, making it difficult to debug runs or coordinate handoffs.

Fastio provides an intelligent workspace platform designed for agentic teams, offering a secure middle ground. Every organization starts with a 14-day free trial (credit card required). Plans include Starter at $29/mo, Business at $99/mo, and Growth at $299/mo. Human users can invite agents into paid workspaces using long-lived scoped API keys.

By using Fastio as your agentic storage layer, you gain several advantages that prevent file loss and coordinate work:

  • Per-file version history: Every file inside a Fastio workspace maintains a complete version history. If an agent overwrites a file during a run, Fastio preserves the prior version automatically. You can compare changes and restore previous versions from the web console.
  • Granular permissions: You can configure read and write permissions at the organization, workspace, folder, or file level. This allows you to restrict Cline to a specific directory, preventing it from touching sensitive project files.
  • Append-only audit log: Fastio records an immutable log of all operations performed in the workspace. Every file upload, deletion, and modification is logged with the timestamp and the identity of the API key or member who performed it, making debugging simple.
  • Collaborative Notes: Humans and agents can co-edit notes in real time using live multiplayer cursors. This provides a safe environment to write specifications and instructions without risking code files.
  • Hosted MCP server: Fastio hosts a Model Context Protocol server that exposes its storage tools programmatically. The remote endpoint is https://mcp.fast.io/mcp/key, allowing Cline to connect using Streamable HTTP with a Bearer token header. This hosted approach removes the need to run local background processes or manage npm packages.
  • Ownership transfer: An agent account can set up a workspace, import data, and organize files. The agent then transfers the organization to a human lead via a claim link, preserving the admin structure.

By combining strict rule instructions in .clinerules, manual VS Code change approvals, clean git branches, and Fastio versioned workspaces, you create a secure development workflow that keeps your codebase protected from automated editing errors. Detailed guidelines are available on the Fastio pricing page and the Fastio workspace storage for agents page.

Immutable audit log showing agent activities and version history

Frequently Asked Questions

Why does Cline keep overwriting my files?

Cline keeps overwriting files because it defaults to using the `write_to_file` tool when its target search blocks in the `replace_in_file` tool fail to match. Additionally, if you have auto-approve settings enabled for file edits, the agent will execute these destructive writes immediately without presenting a diff for your review.

How do I make files read-only for Cline?

You can make files read-only for Cline by configuring granular permissions in your workspace. If you use a versioned cloud storage platform like Fastio, you can assign read-only access to specific folders or files for the agent's API key. This prevents the model from executing write operations while still allowing it to read and analyze project files.

What settings prevent Cline from deleting code?

To prevent Cline from deleting code, open the extension settings in the VS Code sidebar and uncheck the "Edit project files" option under the auto-approve menu. This forces the agent to request manual approval and display a side-by-side diff in your editor before writing any changes to disk.

How can I recover files if Cline overwrites them?

If a file is overwritten, you can recover it by running `git restore <filepath>` in your local terminal, provided you committed your work before starting the agent run. If the file is not under version control, you can restore prior versions from your Fastio workspace version history.

Related Resources

Fastio features

Protect your codebase during Cline development runs

Set up a shared Fastio workspace with remote MCP access, automatic version history, and complete audit logging. Protect your files from agent errors with a 14-day free trial.