nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] string_replace writes the text "undefined" into the file when new_str is missing

Open addyCooks opened this issue 2 weeks ago • 3 comments

Description

If the model calls string_replace without new_str, the validator accepts the call and the file gets the literal text "undefined" where the old text was.

Environment

  • OS: Windows 11 Home 10.0.26300
  • Node version: v22.22.3
  • Nanocoder version: 1.32.0 (built from main at f92cce9c)
  • Provider: Not provider-specific
  • Model: Not model-specific

Steps to Reproduce

  1. Create a.txt containing alpha\nbeta\n and read it with read_file.
  2. Call string_replace with {"path": "a.txt", "old_str": "beta"} (no new_str).
  3. Read a.txt.

Expected Behavior

The validator rejects the call, for example "new_str is required (use an empty string to delete text)". The file is unchanged.

Actual Behavior

The validator returns {valid: true}. The tool then throws, but the file has already been changed.

Logs/Screenshots

execute: Cannot read properties of undefined (reading 'split')
file:    "alpha\nundefined\n"

With a CRLF file the validator itself fails with Cannot read properties of undefined (reading 'replace') and execute throws the same error.

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder (main)

Suggested fix:

  • source/tools/file-ops/string-replace.tsx:243 (stringReplaceValidator): return an error unless typeof new_str === 'string' (an empty string stays valid for deleting text). Also guard lines 101 and 124.
  • source/tools/runtime/schema-validate.ts:76 skips undefined values and leaves presence checks to each tool's validator, so this tool must check it itself.
  • Add a test: a missing new_str is rejected and the file is not modified.

addyCooks avatar Nov 14 '26 06:11 addyCooks

Hi, I'd like to work on this issue. I'll follow the suggested approach: require new_str to be a string in both the validator and execution entry point, while preserving empty-string deletion. I'll add regression tests for missing and non-string values, unchanged file bytes on rejection, and LF/CRLF behavior. Could you assign this to me (@VIOLETee0)? I'll wait for assignment before starting work.

VIOLETee0 avatar Nov 14 '26 10:11 VIOLETee0

I’d like to work on this. I’ll add a typeof new_str === 'string' check in the validator and execute, with regression tests. PR shortly.

gaurav703 avatar Nov 15 '26 12:11 gaurav703

PR is up: Nano-Collective/nanocoder#2227. Could a maintainer assign this issue to me?

gaurav703 avatar Nov 15 '26 16:11 gaurav703