Skip to content

Refuse fs_write with missing content so truncated tool calls don't clobber files - #41

Merged
FMXExpress merged 1 commit into
mainfrom
claude/pr41-fs-write-require-content
May 28, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/pr41-fs-write-require-content

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

Fixes the destructive behavior you saw in your debug log:

tool_call: name=fs_write args={"path":"...llmchat.Tui.pas"}
tool_result: name=fs_write err= result=wrote 0 bytes to ...llmchat.Tui.pas
(model retries the same truncated call for 6 more iterations,
 wiping the file to 0 bytes each round)

Root cause

The content was large enough that the model hit its per-turn max_tokens budget midway through generating the tool_use input JSON. Anthropic returned the partial JSON, our Anthropic parser ended up with just {"path":"..."} (no content field), and Tool_FSWrite happily treated the missing key as the empty string, overwrote the file with 0 bytes, and reported "wrote 0 bytes" as a success message. The model saw no error, retried, and the same thing happened — file destroyed each round.

Fix

Introduce HasJSONKey() which returns True only when the JSON object actually contains the key — distinct from ParseStringArg, which returns False both for missing AND empty-string values.

Tool_FSWrite now:

content in args Behavior
missing entirely Reject with missing required argument: content. If your previous response was truncated mid-tool_call (model hit max_tokens), re-emit fs_write with the full content as a string, or use fs_edit_hashline to apply incremental edits. Pass "content":"" explicitly to clear a file.
present but empty "" Allow (legitimate "clear file" request, unchanged)
present with value Existing path, unchanged

The defensive hashline-prefix stripping is untouched.

Separate concern (not in this PR)

The default max_tokens of 4096 is genuinely too low for code-writing tasks — a typical Pascal unit easily exceeds that in tokens. Worth a follow-up PR that either raises the default for agent/serve or steers the model toward fs_edit_hashline for incremental writes. With this PR the model at least gets a clear error pointing at the escape hatch instead of a silent file wipe.

About "bytes go up a little each time"

That's the conversation history accumulating each round-trip (assistant turn + tool result get appended). Not a bug.

Test plan

  • Send a tool_call to fs_write with {"path":"x.txt"} (no content) — get the error, file untouched
  • Send {"path":"x.txt","content":""} — succeeds, file truncated to 0 bytes (existing semantics)
  • Send {"path":"x.txt","content":"hello"} — succeeds as before
  • Long-form regression: the multi-iteration truncation scenario from the original bug report now surfaces the new error to the model after the first truncation, breaking the wipe-and-retry loop

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon


Generated by Claude Code

…obber files

Symptom (reported in pasclaw serve debug log over 7 iterations):

  tool_call: name=fs_write args={"path":"...llmchat.Tui.pas"}
  tool_result: name=fs_write err= result=wrote 0 bytes to ...llmchat.Tui.pas
  (model retries with the same truncated call; file stays at 0 bytes)

Root cause: the file content being written was large enough that the
model hit its per-turn max_tokens budget midway through generating
the tool_use block's input JSON. Anthropic returned the partial JSON,
our parser ended up with just {"path":"..."} (no content field), and
Tool_FSWrite cheerfully treated the missing key as the empty string,
overwrote the user's file with 0 bytes, and reported "wrote 0 bytes"
as a SUCCESS message. The model saw no error, didn't change behavior,
and tried again — wiping the file each round.

Fix: introduce HasJSONKey() which returns True only when the JSON
object actually contains the key (distinct from ParseStringArg's
behavior of returning False both for missing AND empty-string values).
Use it to gate Tool_FSWrite:

  - content key missing entirely  -> reject with an error message that
                                     tells the model what likely
                                     happened and offers two recovery
                                     paths: re-emit with full content,
                                     or switch to fs_edit_hashline for
                                     incremental edits.
  - content present but empty ""  -> allow (legitimate "clear file"
                                     request, no change in behavior).
  - content present with value    -> existing path, unchanged.

The defensive hashline-prefix stripping logic is unchanged.

Separate concern, not in this PR: default max_tokens of 4096 is too
low for code-writing tasks (a typical Pascal unit easily exceeds that
in token count). Worth raising the default for the agent / serve
endpoints or guiding the model toward fs_edit_hashline for large
writes. With this PR the model at least gets a clear error pointing
at the right escape hatch.

https://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
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.

2 participants