[Bug] decodeHtmlEntities corrupts numeric entities above U+FFFF and decodes escaped entities twice
Description
decodeHtmlEntities, which runs on every assistant message, gives the wrong text in two cases:
- Numeric entities above U+FFFF are decoded to a different character.
😀and😀(the grinning face emoji) come out as U+F600, a private-use character that shows as nothing or a box. - An escaped entity is decoded twice.
&lt;is the literal text<, but it is shown as<. Likewise&#169;becomes the copyright sign instead of©.
Environment
- OS: Windows 11
- Node version: v24.11.0
- Nanocoder version: 1.32.0 (main at bfd8b8e7)
- Provider: Not provider-specific
- Model: Not model-specific
Steps to Reproduce
- Ask any model to reply with exactly:
done 😀 and &lt; - Look at the rendered message.
The same can be seen by calling decodeHtmlEntities with that text directly.
Expected Behavior
done 😀 and <
Actual Behavior
done followed by a blank or box glyph, then and <.
The second case matters when a model explains HTML escaping in prose ("write &lt; to show <"): the terminal shows a sentence that says something else.
Logs/Screenshots
decodeHtmlEntities('done 😀') expected 'done 😀' actual 'done '
decodeHtmlEntities('done 😀') expected 'done 😀' actual 'done '
decodeHtmlEntities('write &lt; for <') expected 'write < for <' actual 'write < for <'
decodeHtmlEntities('&#169;') expected '©' actual '©'
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
Cause, in source/markdown-parser/html-entities.ts:
- Numeric entities use
String.fromCharCode, which keeps only the low 16 bits of the code point. - Named entities are replaced in one pass per entity with
&first, and the numeric passes run after all of them, so text produced by one replacement is decoded again by the next.
Suggested fix: decode in a single pass over the text, and use String.fromCodePoint with a guard for values above U+10FFFF (it throws a RangeError for those, so they should be left as written). This is separate from #1591, which was about decoding inside fenced code blocks.
I have this working locally with specs for each case, and I would like to work on it if you can assign it to me.
I'd like to take this. Both cases come from how the function works now: named entities are replaced one by one, then numeric ones, so the < that comes out of &lt; gets decoded again; and String.fromCharCode truncates code points above U+FFFF. My plan is a single regex pass over named, decimal and hex entities, using String.fromCodePoint and leaving out-of-range or unknown entities as they are. Tests for both cases plus a changeset.
Hi, I have already done the implementation in my local for this , just waiting for mod approval