nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] The model gets no parser error for unclosed or mismatched XML tool-call tags

Open me-adityaraj8 opened this issue 3 weeks ago • 3 comments

Description

Split out of #1300, as @addyCooks asked in #1989.

Some broken XML gets a parser error that goes back to the model so it can retry, such as <parameter=name>. An unclosed tag or a mismatched closing tag doesn't:

  • Unclosed tag: no tool call and no error. The raw XML becomes the final reply and the turn ends.
  • Mismatched closing tag: the outer tags still match, so the tool is called with {} and its arguments are lost. A tool with no required arguments still runs, so list_directory lists the wrong folder with no error.

Environment

  • OS: macOS 27.0
  • Node version: v24.22.1
  • Nanocoder version: main at d0d6ecf2 (xml-parser.ts and tool-parser.ts are identical in v1.32.0)
  • Provider: none, see below
  • Model: none, I used a stub client rather than a live model

Steps to Reproduce

  1. Run processAssistantResponse with a real ToolManager and a stub client whose first reply is one of the strings in the table below. For the last row, sub is a folder in the working directory.
  2. Check how many times the client is called and which messages go back to it.

The results are the same with toolsDisabled true or false.

Expected Behavior

The model is told its XML was broken and can retry. <read_file><parameter=path>/test/file.txt</parameter></read_file> already works this way: it gets "Your previous response contained a malformed tool call...".

Actual Behavior

First reply Tool call What happens next
<read_file><path>/test/file.txt</path> none The client is called once. The XML is shown as the final reply and the turn ends.
<read_file><path>/test/file.txt</wrong_close></read_file> read_file with {} Tool result: ⚒ Validation failed: Invalid file path: "undefined". Path must be within the project directory.
<list_directory><path>sub</wrong_close></list_directory> list_directory with {} Lists . instead of sub, as a normal result.

Logs/Screenshots

parseToolCalls('<read_file><path>/test/file.txt</path>')
success: true, toolCalls: [], cleanedContent: '<read_file><path>/test/file.txt</path>'

parseToolCalls('<read_file><path>/test/file.txt</wrong_close></read_file>')
success: true, toolCalls: [read_file with {}], cleanedContent: ''

Additional Context

Both parse results are pinned in source/tool-calling/xml-parser.spec.ts (handles unclosed tags gracefully at line 339, handles mismatched tags gracefully at line 347), so this may be deliberate. A normal reply that mentions a tag such as <br> should stay plain text. #1940 is about the same parser, but only its speed.

I can take this one if you want to assign it to me.

  • [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

me-adityaraj8 avatar Nov 08 '26 03:11 me-adityaraj8

Thanks @me-adityaraj8, confirmed on main: an unclosed tag ends the turn with the raw XML as the reply, and a mismatched closing tag runs the tool with {}. Assigning this to you.

Before the PR, please comment with your detection rule so it doesn't misfire on prose like
, code fences, or XML quoted inside another call's arguments. Add tests for those, update the two specs that pin today's behaviour, and keep it separate from #1940.

addyCooks avatar Nov 10 '26 07:11 addyCooks

@addyCooks here's the rule. It only looks at calls to the tools offered to the model, and runs in the interactive loop and --plain.

  1. Unclosed call: an opening tag named after an offered tool, followed by another tag, with no closing tag for it anywhere after. For <read_file><path>a.ts</path> the model gets Incomplete tool call: <read_file> is never closed with </read_file>.
  2. Parameter left open: in a call that does parse, a tag that's left once every complete <param>value</param> is taken out. For <read_file><path>a.ts</wrong_close></read_file> the model gets Invalid tool call: <path> inside <read_file> is never closed with </path>. A stray closing tag gets ... does not close any parameter.

If either one fires, nothing in that reply runs, so the model doesn't act on half of what it sent.

What it leaves alone:

  • Prose: <br> isn't a tool name, and "I'll use <read_file> next" has no tag after it.
  • Code: fenced blocks (including an unclosed fence) and inline code are skipped by the unclosed check. The parameter check only looks at calls the parser already runs. The parser reads the first fenced block as a wrapper today, so a broken call inside it now gets the error instead of running with {}.
  • XML inside another call's arguments: calls that parse (XML, JSON or <function=…>) are blanked out before the unclosed check, and each parameter's value is taken out whole before the parameter check.

Tests cover each case, and the two specs in xml-parser.spec.ts now pin the new behaviour. The extra scan only runs when a reply has an offered tool tag followed by another tag, so the #1940 input costs nothing extra. ACP isn't changed: it ends the turn on any parse error instead of retrying, so it would need its own change.

Does this work for you? If so, I'll open the PR.

me-adityaraj8 avatar Nov 11 '26 05:11 me-adityaraj8

Thanks @me-adityaraj8, the rule looks good, go ahead and open the PR. Four things to cover (from reading main):

  1. ACP does retry on a parse error. Interactive, --plain, ACP and subagents all run through runTurnEngine, whose shared malformed-call path retries up to maxMalformedRetries. If you saw ACP end the turn, please point me to where.
  2. Add the check in that shared path in turn-engine.ts, passing in the offered tool names (prepared.availableNames), so all four loops get it.
  3. The parse runs before the finishReason === 'length' check, so a reply cut off mid-call would hit the malformed path instead of the "carry on" continuation. Please handle that and add a test.
  4. Please add a test for prose outside code formatting, like "use <read_file>x to read a file".

addyCooks avatar Nov 12 '26 07:11 addyCooks