nanocoder
nanocoder copied to clipboard
[Bug] The XML tool-call parser takes quadratic time when a reply has many unclosed tags
Summary
Parsing a large reply that contains many opening tags without closing tags (such as <br>) takes seconds and blocks the UI thread. The JSON tool-call scan already has a budget, but the XML scan does not.
Steps to reproduce
- Build
text = "Here is the html:\n" + "line<br>\n".repeat(n). - Time
parseToolCalls(text)for n = 2000, 5000, 10000 and 20000.
Expected behaviour
Parse time grows roughly linearly. 180 KB takes well under 300 ms.
Actual behaviour
n=2000 18 KB 21 ms
n=5000 45 KB 103 ms
n=10000 90 KB 405 ms
n=20000 180 KB 1664 ms (926 ms in a second run) ~4x per doubling
A spec asserting under 300 ms fails with took 926 ms.
Environment
Windows 11, Node v22.22.3, Nanocoder 1.31.0 (main at 948a4e74)
Suggested fix
source/tool-calling/xml-parser.ts:17:TOOL_CALL_REGEX = /<(\w+)>(.*?)<\/\1>/gsrescans to the end of the reply for every unclosed opener. Skip openers whose name is not a registered tool, or apply a scan budget like the one #1815 added for the JSON path.
Hi! I'd like to take this one if it's still unassigned.
Before I start, here's my plan, and please tell me if you'd prefer a different approach:
- Reproduce the slowdown with a regression spec (about 180 KB of unclosed tags such as
<br>), asserting parse time stays well under the 300 ms in the report. - Fix
source/tool-calling/xml-parser.tsso parsing scales roughly linearly. I'm looking at either skipping openers that don't match a registered tool name, or adding a scan budget like the one #1815 added for the JSON path. I'll read the code and pick whichever keeps behavior unchanged for valid tool calls. If you already prefer one, let me know. - Add tests that valid XML tool calls still parse the same, run
pnpm test:all, and add a patch changeset.
I'll open a PR that references this issue once it's ready.