nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] XML tool-call fallback re-serialises JSON-looking parameter values, so write_file content is reformatted or changed

Open addyCooks opened this issue 2 weeks ago • 5 comments

Description

In the XML fallback, a parameter whose text looks like JSON is parsed, and ensureString later turns it back into a string with JSON.stringify. A pretty-printed package.json is minified, and other values change too.

Environment

  • OS: Windows 11 Home 10.0.26300
  • Node version: v22.22.3
  • Nanocoder version: 1.32.0 (built from main at f92cce9c)
  • Provider: Any model that uses the XML fallback
  • Model: Not model-specific

Steps to Reproduce

  1. Call parseToolCalls with:

     <write_file><path>f</path><content>{
       "name": "x",
       "version": "1.0.0"
     }
     </content></write_file>
    
  2. Pass call.function.arguments.content through ensureString.

  3. Repeat with content null, 1.0, 1e3 and [1,\n 2].

Expected Behavior

The content is passed through unchanged.

Actual Behavior

The pretty JSON becomes a single minified line.

Logs/Screenshots

'{\n  "name": "x",\n  "version": "1.0.0"\n}\n' -> '{"name":"x","version":"1.0.0"}'
(earlier run on 5f193314, not repeated today:) 'null' -> '', '1.0' -> '1', '1e3' -> '1000', '[1,\n 2]' -> '[1,2]'

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/tool-calling/xml-parser.ts:226-232: only JSON.parse a value when the tool's schema says that parameter is an object or array. Keep string parameters (content, old_str, new_str, command) as the original text.
  • source/utils/type-helpers.ts:171-199 (ensureString): do not re-serialise strings.
  • Add tests through write_file and string_replace.

addyCooks avatar Nov 14 '26 06:11 addyCooks

hey would like to work on this

Srinidhi444 avatar Nov 14 '26 08:11 Srinidhi444

Hi @addyCooks , I would like to work on this issue!

Root Cause

In XMLToolCallParser.parseParameters (source/tool-calling/xml-parser.ts:302), every parameter value is unconditionally run through JSON.parse. When a parameter intended as plain text (such as content in write_file or old_str/new_str in string_replace) contains JSON, null, numbers, or arrays, it is coerced into an object or primitive. Downstream, ensureString runs JSON.stringify() on it, minifying pretty-printed JSON ({\n "name": "x"\n} $\rightarrow$ {"name":"x"}), dropping formatting, and mutating literals ("null" $\rightarrow$ "", "1.0" $\rightarrow$ "1", "1e3" $\rightarrow$ "1000").

Proposed Fix

  1. Preserve String Parameters in XML Parser: Ensure parameters intended as strings (e.g. content, old_str, new_str, command, path) retain their raw textual value and are not coerced via JSON.parse. Only parse JSON when expected (or when the tool schema defines the parameter as an object/array).
  2. Review ensureString (source/utils/type-helpers.ts): Ensure string parameters are passed through cleanly without lossy re-serialization.
  3. Comprehensive Tests:
    • Add test cases in source/tool-calling/xml-parser.spec.ts covering pretty-printed JSON, "null", "1.0", "1e3", and multiline arrays in string parameters.
    • Add regression tests through write_file and string_replace ensuring file content is written byte-for-byte identical to the original input.
  4. Changeset: Add a patch changeset.

Could you please assign this issue to me?

VishakhaSainani avatar Nov 15 '26 13:11 VishakhaSainani

I’d like to work on this. My plan: keep raw-text parameters (content, old_str, new_str, command, path) verbatim in XMLToolCallParser.parseParameters instead of running them through JSON.parse, while structured parameters still parse. I’ll add regression tests for byte-for-byte preservation (pretty-printed JSON, null, 1.0, 1e3). Could a maintainer assign this to me? PR shortly.

gaurav703 avatar Nov 15 '26 16:11 gaurav703

Hi @addyCooks, I’ve implemented the fix for this issue and opened PR #2230 (branch fix/xml-parser-preserve-string-params).

Summary of Fix

  • Root Cause: XMLToolCallParser.parseParameters previously passed every parameter value unconditionally to JSON.parse(). For parameters intended as raw text (content, old_str, new_str, command, path), any content containing JSON, arrays, "null", "1.0", or "1e3" was coerced into objects/primitives. Downstream ensureString() then re-stringified objects with JSON.stringify(), minifying formatted files (e.g. package.json), clearing "null" files to "", and turning old_str in string_replace into [object Object].
  • Fix:
    1. Added RAW_STRING_PARAMETERS and isRawStringParameter(paramName, toolName) to preserve raw textual parameters verbatim as strings without JSON.parse.
    2. Passed toolName into parseParameters(innerXml, toolName).
    3. Guarded generic parameters so "null" is never coerced to JS null primitive, and numeric precision like "1.0" / "1e3" is preserved.
    4. Structured parameters (e.g. batch_read files, configure options, boolean flags, numbers) continue to parse as before.
  • Verification:
    • Added unit tests in source/tool-calling/xml-parser.spec.ts covering pretty-printed JSON, "null", "1.0", "1e3", multiline arrays, string_replace, execute_bash, and generic parameters (83 tests passing).
    • Added end-to-end regression tests in source/tools/file-ops/write-file.spec.tsx and source/tools/file-ops/string-replace.spec.tsx asserting byte-for-byte identical writes and replacements (137 tests passing).
    • Clean runs for pnpm run test:types, pnpm run test:lint, pnpm run test:format, and pnpm run test:knip.
    • Added patch changeset for @nanocollective/nanocoder.

VishakhaSainani avatar Nov 15 '26 19:11 VishakhaSainani

working on it will raise pr sooon

Srinidhi444 avatar Nov 16 '26 16:11 Srinidhi444