Skip to content

[BUG]: write tool silently overwrites existing files (no stale-content check) #30765

Closed

Description

Description

packages/core/src/tool/write.ts uses writeTextPreservingBom() which has NO stale-content check. edit uses writeIfUnchanged() (compares bytes before writing, file-mutation.ts:158-167), but write silently overwrites existing files with zero protection.

Chain from #30376 — at least edit checks content hasn't changed. write doesn't.

Also edit.ts:127-131 error says "oldString must not be empty. Use write to create or overwrite a file." — instructs AI to use the unsafe tool.

Also file-mutation.ts:137-138: writeTextPreservingBom reads the entire existing file just to check first 3 bytes for UTF-8 BOM (hasUtf8Bom), then immediately overwrites it. Wasteful on large files.

Consolidated findings (merged from closed issues):

Plugins

N/A (source code analysis)

OpenCode version

main

Steps to reproduce

  1. Read source code at packages/core/src/tool/write.ts
  2. Notice writeTextPreservingBom() has no call to writeIfUnchanged() or any stale-content verification
  3. Compare with packages/core/src/tool/edit.ts which does call writeIfUnchanged() before writing
  4. AI can overwrite any existing file without having read it first

Screenshot and/or share link

N/A (we read the source code)

Operating System

N/A (code analysis, not environment-specific)

Terminal

N/A

Activity

  1. github-actions commented on Jun 4, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions