#6414·odysseus

write_file truncates an existing file to 0 bytes on empty body, defensive guard needed (data loss)

Author: Thaw-jpgCreated Sep 27, 2026Updated Sep 28, 2026
Labelsbugready for review

Prerequisites

  • I searched open issues and discussions and did not find an existing report of this bug.
  • This is not a security vulnerability. (Vulnerabilities go to GitHub Security Advisories — see SECURITY.md.)
  • I am running the latest code from the dev branch (the default branch you get on clone, where fixes land first) and the bug still reproduces there. Please git pull the latest dev before filing.

Odysseus Revision

3b6c16916233 (2026-09-14)

Install Method

Docker (docker compose up)

Operating System

Linux

Steps to Reproduce

  1. Have write_file receive a call whose extracted body is empty. (In my case a #6013-class parser failure: a Cookbook-served gemma-4 model's <|"|>-wrapped call, where the parser kept the path but lost the large multiline content, so body came through as "".)
  2. Target a path that already contains content.
  3. The existing file is truncated to 0 bytes; the tool reports success (exit_code=0), and the previous content is gone.

Expected Behaviour

An empty body should not silently truncate or overwrite an existing file. write_file should return a non-zero exit code / error when body is empty (a deliberate empty-file creation can be made explicit), so existing content survives a failed or empty write.

Actual Behaviour

The target is truncated to 0 bytes and exit_code=0 is returned, so the caller believes the write succeeded. Any prior content at that path is lost.

Logs / Screenshots

src.tool_execution - INFO - Tool executed: write_file: /vault/Cookbook/Recipes/classic-banana-cake.md -> exit_code=0
src.agent_loop - INFO - Agent round 1 summary: 55 chars, 0 native calls, 0 tool blocks. Preview: Saved to ...

Resulting file on disk:
-rw-r--r-- 1 thaw thaw 0 Sep 26 09:38 classic-banana-cake.md

Model / Backend (if relevant)

Cookbook-served gemma-4-12B-it-qat-q4_0-gguf via llama.cpp/Vulkan. The empty body originated from this model's wrapper format (a #6013-class parser failure), but the write-path defect reproduces whenever any path hands write_file an empty body, regardless of model.

Are you willing to submit a fix?

No — I am only filing the report

Additional Information

Confirmed present on current dev (viewed on GitHub today): WriteFileTool._write() reads the existing file into old, then calls open(path, "w") — which truncates — and writes body, with no guard against body being empty:

def _write():
    old = ""
    try:
        with open(path, "r", encoding="utf-8") as f:
            old = f.read()          # existing content read here...
    except (FileNotFoundError, IsADirectoryError, UnicodeDecodeError, OSError):
        old = ""
    d = os.path.dirname(path)
    if d:
        os.makedirs(d, exist_ok=True)
    with open(path, "w", encoding="utf-8") as f:  # ...target truncated here, before body is checked
        f.write(body)               # body may be "" -> 0-byte file, old content lost
    return old, len(body)

Data-loss path: existing file has content -> tool reads it into old -> open("w") truncates -> empty body written -> reports "Wrote 0 bytes", exit_code=0. Prior content is gone. Note the method already has the old content in hand (it reads old for the diff), so a guard is trivial.

Relationship to other issues:

  • #6013 (parser drops content when closing markers appear inside the payload) is one source of empty bodies; its fix #6295 is welcome. This issue is complementary defense-in-depth: even with a perfect parser, any path that hands write_file an empty body destroys existing content, so the guard belongs in the write path itself. I am not re-reporting the parser bug.
  • Relevant to #6372: an empty body written atomically is still a 0-byte file, so atomic writes don't address this (per that PR's scope clarification).

Suggested fix direction (for maintainer validation, not tested by me): Add an empty-body guard before the truncating open. The simplest robust rule is to refuse any empty write rather than conditioning on whether the target already has content: if the body is empty, return an error / exit_code=1 and do not open the file for writing at all, an empty write from a tool call is almost always a malformed/lost-content failure, not intent. Whether "empty" should mean strictly "" or also whitespace-only (body.strip() == "") is a judgement call. A deliberate empty-file need can be served by a separate explicit path. Regression test: write real content to a path, then issue a call with an empty body to the same path, and assert the file is unchanged (not 0 bytes).

Per CONTRIBUTING's note on LLM-agent contributions, I'm filing this as an issue rather than opening a PR; the diagnosis was AI-assisted. Happy for a maintainer to take the fix, or to test a patch.