Skip to content

fix(core): stop write tool from overwriting existing files with empty content#33091

Open
anisches wants to merge 2 commits into
anomalyco:devfrom
anisches:fix-write-tool-empty-string
Open

fix(core): stop write tool from overwriting existing files with empty content#33091
anisches wants to merge 2 commits into
anomalyco:devfrom
anisches:fix-write-tool-empty-string

Conversation

@anisches

@anisches anisches commented Jun 20, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #33078

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The write tool would overwrite an existing file when the model passed empty (or whitespace-only) content, silently destroying the file. Now write refuses that case when the file already has content, while still allowing empty new files (e.g. __init__.py, .gitkeep). Deleting a file is unaffected — that still goes through rm.

My first attempt just made the schema NonEmptyString, but that also blocked creating empty files and missed whitespace-only content, so I moved the check into the tool's execute step instead.

How did you verify your code works?

  • Added tests in packages/core/test/tool-write.test.ts: creating a new empty file succeeds; overwriting a non-empty file with "" or whitespace is refused and the original content is left intact.
  • bun test packages/core/test/tool-write.test.ts and bun run typecheck pass.

To reproduce: write { path: "existing.txt", content: "" } to a non-empty file — before, it wiped the file; now it returns an error and the file is untouched.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Jun 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jun 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

… content

Move the empty-content check out of the schema and into the tool's execute
step. Empty or whitespace-only content is now refused only when the target
already exists with real content; creating a new empty file (e.g. __init__.py,
.gitkeep) still works, and deleting via rm is unaffected.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@anisches anisches changed the title fix(core): prevent write tool from accepting empty strings and overwriting files fix(core): stop write tool from overwriting existing files with empty content Jun 20, 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.

[bug] Write tool silently writes empty content, overwriting existing files with nothing

1 participant