Repository navigation
Refuse fs_write with missing content so truncated tool calls don't clobber files - #41
Merged
Merged
Conversation
…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
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the destructive behavior you saw in your debug log:
Root cause
The content was large enough that the model hit its per-turn
max_tokensbudget midway through generating the tool_use input JSON. Anthropic returned the partial JSON, our Anthropic parser ended up with just{"path":"..."}(nocontentfield), andTool_FSWritehappily 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 fromParseStringArg, which returns False both for missing AND empty-string values.Tool_FSWritenow:contentin argsmissing 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.""The defensive hashline-prefix stripping is untouched.
Separate concern (not in this PR)
The default
max_tokensof 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 foragent/serveor steers the model towardfs_edit_hashlinefor 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
{"path":"x.txt"}(no content) — get the error, file untouched{"path":"x.txt","content":""}— succeeds, file truncated to 0 bytes (existing semantics){"path":"x.txt","content":"hello"}— succeeds as beforehttps://claude.ai/code/session_01TBcLtmpj7dqA5tyFbGnQon
Generated by Claude Code