[Bug] XML tool-call validation errors discard parser diagnostics and mislead model with "Tool does not exist"
Description
When models (especially local or fallback models) emit XML tool calls with syntax or argument errors, the XML parser emits a synthetic tool call named __xml_validation_error__ containing the detailed parser error message in toolCall.function.arguments.error.
However, in source/hooks/chat-handler/utils/tool-filters.ts, partitionToolCalls marks __xml_validation_error__ as an unknown tool (isUnknown = true). When filterValidToolCalls and partitionUnknownToolCalls construct the feedback for the model, they completely ignore toolCall.function.arguments.error and instead return:
"The tool \"__xml_validation_error__\" does not exist. Use only the tools that are available in the system."
The UI also displays:
Model called an unavailable tool ("__xml_validation_error__") — asking it to retry with a valid tool.
The model receives no explanation of what was wrong with its XML syntax, becomes confused about a ghost tool called __xml_validation_error__, and often gets stuck in an unrecoverable hallucination loop.
Environment
- OS: Linux / macOS / Windows
- Node version: v20.0.0+
- Nanocoder version: 1.30.0
- Provider: Ollama / OpenRouter
- Model: Local models using XML tool calling (e.g., Qwen-2.5-Coder, DeepSeek-R1)
Steps to Reproduce
- Configure an OpenAI-compatible or Ollama model that falls back to XML tool calling.
- Ask the model to execute a command or edit a file where the model's output produces an XML syntax error (e.g. mismatched tags or unescaped characters in argument body).
- Observe the chat output and the follow-up prompt sent to the model.
Expected Behavior
The tool error result sent back to the model should contain the parser's specific error message from arguments.error (e.g. XML tool call syntax error: unclosed tag <parameter name="path">), allowing the model to repair its formatting.
Actual Behavior
The diagnostics are discarded. The model is told that __xml_validation_error__ does not exist, prompting it to apologize for calling __xml_validation_error__ and repeatedly re-emitting broken XML.
Logs/Screenshots
In source/hooks/chat-handler/utils/tool-filters.ts:
export const filterValidToolCalls = (
toolCalls: ToolCall[],
toolManager: ToolManager | null,
): PartitionedToolCalls =>
partitionToolCalls(toolCalls, toolManager, toolCall => {
// ...
return {
tool_call_id: toolCall.id,
role: 'tool' as const,
name: toolCall.function.name,
content: `The tool "${toolCall.function.name}" does not exist. Use only the tools that are available in the system.${availableHint}`,
};
});
And in source/message-handler.ts:
if (toolCall.function.name === '__xml_validation_error__') {
const args = toolCall.function.arguments as {error: string};
throw new Error(args.error); // Discarded or throws unhandled outside try/catch!
}
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
Picking this up - a fix will follow shortly.
Hi @addyCooks, can I take this along with #1303? The earlier PRs were closed. I checked both against current main, and I think the fix differs from what the issues describe, so I'd like to run the approach by you first.
Hi @me-adityaraj8, I've assigned this one to you. For #1303, please comment there too and I'll assign it once you have. Please wait for the assignment before opening a PR on either one. Thanks!
@addyCooks thanks! Here's what I found on current main: nothing outside the tests creates __xml_validation_error__ any more. XML the parser rejects now returns success: false and goes through the retry path. The marker only shows up if a model literally writes that name, and it's then treated as an unknown tool. So #1300 can't happen as described.
#1303 is partly the same. The marker case is unreachable, and the TUI, ACP and plain paths all filter unknown tools before calling processToolUse. The Unknown tool and registry throws are still outside the try though.
I'd suggest removing the leftover marker checks and their tests, and moving those two throws inside the try so they return an error result. Does that work for you, or would you rather bring the marker back?
Thanks @me-adityaraj8, that works for me. I checked your findings against current main:
- Nothing outside the specs creates
__xml_validation_error__any more. - XML the parser rejects returns
success: falsewith the parser's error and examples, andconversation-loop.tsxsends${parseResult.error}\n\n${parseResult.examples}back to the model. So #1300 as described no longer happens. - In
processToolUse(message-handler.ts), the marker throw, "Tool registry not initialized" and "Unknown tool: …" are all thrown before thetry, which is what #1303 is about.
Please remove all of the leftover marker handling, not just the check in message-handler.ts:
message-handler.ts(the marker check) and its two tests inmessage-handler.spec.tsconversation-loop.tsx: two marker checks, in the confirmation classification and inbeforeExecutetool-filters.ts(theXML_VALIDATION_ERROR_TOOLconstant and the always-unknown rule) and its test intool-filters.spec.ts- the marker test in
acp-conversation.spec.ts
A model that writes that name literally should still hit the normal unknown-tool path, so please keep a test for that. The existing malformed tool-call recovery tests in conversation-loop.spec.ts should keep passing.
Then move the two throws inside the try so they return an error result. Please say in the PR description which issue each change closes, and wait for #1303 to be assigned before including it.