[GH-ISSUE #3108] [Issue]: In XML wrap format, user-typed < and > are escaped to &lt;/&gt; before the model sees them (breaks blockquotes, wastes tokens) #1667

Closed
opened 2026-10-21 05:41:31 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @kolacheee on GitHub (Jun 30, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3108

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

When the prompt wrap format is set to XML, any <, >, and & you typed yourself — in a message, a lorebook entry, a character field, a summary, etc. — is converted to HTML entities (&lt;, &gt;, &amp;) before the model sees it. So a lorebook heading <Setting> is sent as &lt;Setting&gt;, and a markdown blockquote > ... is sent as &gt; .... Marinara's own structural tags (<lorebook_all>, <last_message>, …) are not escaped, which is why those look fine while your typed text doesn't.

This has two real costs the reporting user called out:

  • It confuses the model. The model receives literal &gt; / &lt;Setting&gt; rather than > / <Setting>. (Confirmed live: a user asked the model to echo their message back verbatim and the reply contained &gt;, so the model genuinely received the entity, not the character.) Markdown blockquotes (>), a staple of roleplay formatting, are broken this way on every turn.
  • It wastes tokens. &gt; / &lt; tokenize to several tokens each versus one for > / <, so heavy <...>/blockquote users pay an ongoing token tax.

The biggest single offender is >: XML does not require > to be escaped in text content at all (only < and & are mandatory; > only needs escaping inside the literal ]]> sequence). So escaping every > is unnecessary and it's the part that breaks the very common markdown blockquote.

How to reproduce (try it yourself)

  1. Use a connection/preset whose prompt wrap format is "XML" (the assembled prompt shows <lorebook_all>, <last_message> style tags).
  2. Either: add a lorebook entry whose text contains <Setting>, or send a chat message that starts with a markdown blockquote, e.g. > hello.
  3. Open Peek Prompt (or have the model echo your message back verbatim).

You'll see: the typed <Setting> appears as &lt;Setting&gt;, and the > appears as &gt; — and an echo test shows the model received &gt;, not >.
Expected: your </> reach the model as the characters you typed (at minimum > should never be escaped), so blockquotes work and no extra tokens are spent. Marinara's own wrapper tags stay literal as today.

(Workaround for now: set the preset's wrap format to Markdown or None — those paths don't XML-escape.)

Where it is

packages/server/src/services/prompt/prompt-escaping.ts — escapeXmlText (line 4) does .replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"); sanitizePromptLeaf (11-15) applies it to every leaf when wrapFormat === "xml". It's called for essentially all user-authored leaf content during assembly: messages via services/prompt/macro-context.ts:411, lorebook / world-info via services/prompt/marker-expander.ts:356/366/368, character fields via services/generation/character-prompt-context.ts:120/206, summaries via services/prompt/assembler.ts:775, plus memory/scene contexts that call escapeXmlText directly.

What's happening

In XML wrap mode the assembler escapes leaf content so a user's literal text can't break the well-formedness of the XML wrapper (and can't inject a closing tag). That intent is reasonable for < and &, but: (a) > is swept in even though XML never requires it, which is what breaks markdown blockquotes; and (b) the escaped result is what the model literally reads, so even the </& escaping shows up as visible entities and extra tokens.

Suggested fix

  • At minimum, stop escaping > in escapeXmlText — drop the .replace(/>/g, "&gt;"). This immediately fixes markdown blockquotes and removes the most common token waste, with no loss of well-formedness (a bare > is valid XML text). If you want to be fully strict, escape only the literal ]]> sequence instead.
  • Reconsider escaping < / & for leaf content, or make it less aggressive: escaping < is a deliberate anti-injection / well-formedness measure, but its side effect is that the model reads &lt;...&gt; for any legitimately-typed <tag> (the <Setting> case) and pays extra tokens. Options: only escape < when it actually forms a sequence that could be mistaken for one of the framework's own wrapper tags; make XML-escaping opt-in; or document the tradeoff so users know to pick Markdown/None when they rely on literal angle brackets.

AI-investigated against v2.0.6 from the user's report (Peek Prompt + a model echo-back test). The repro is the user's; the code location is from a code read — please confirm before fixing.

Originally created by @kolacheee on GitHub (Jun 30, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3108 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** When the prompt **wrap format is set to XML**, any `<`, `>`, and `&` you typed yourself — in a message, a lorebook entry, a character field, a summary, etc. — is converted to HTML entities (`&lt;`, `&gt;`, `&amp;`) *before the model sees it*. So a lorebook heading `<Setting>` is sent as `&lt;Setting&gt;`, and a markdown blockquote `> ...` is sent as `&gt; ...`. Marinara's own structural tags (`<lorebook_all>`, `<last_message>`, …) are **not** escaped, which is why those look fine while your typed text doesn't. This has two real costs the reporting user called out: - **It confuses the model.** The model receives literal `&gt;` / `&lt;Setting&gt;` rather than `>` / `<Setting>`. (Confirmed live: a user asked the model to echo their message back verbatim and the reply contained `&gt;`, so the model genuinely received the entity, not the character.) Markdown blockquotes (`>`), a staple of roleplay formatting, are broken this way on every turn. - **It wastes tokens.** `&gt;` / `&lt;` tokenize to several tokens each versus one for `>` / `<`, so heavy `<...>`/blockquote users pay an ongoing token tax. The biggest single offender is `>`: **XML does not require `>` to be escaped in text content at all** (only `<` and `&` are mandatory; `>` only needs escaping inside the literal `]]>` sequence). So escaping every `>` is unnecessary *and* it's the part that breaks the very common markdown blockquote. **How to reproduce (try it yourself)** 1. Use a connection/preset whose **prompt wrap format is "XML"** (the assembled prompt shows `<lorebook_all>`, `<last_message>` style tags). 2. Either: add a lorebook entry whose text contains `<Setting>`, **or** send a chat message that starts with a markdown blockquote, e.g. `> hello`. 3. Open **Peek Prompt** (or have the model echo your message back verbatim). **You'll see:** the typed `<Setting>` appears as `&lt;Setting&gt;`, and the `>` appears as `&gt;` — and an echo test shows the model received `&gt;`, not `>`. **Expected:** your `<`/`>` reach the model as the characters you typed (at minimum `>` should never be escaped), so blockquotes work and no extra tokens are spent. Marinara's own wrapper tags stay literal as today. (Workaround for now: set the preset's wrap format to **Markdown** or **None** — those paths don't XML-escape.) **Where it is** `packages/server/src/services/prompt/prompt-escaping.ts` — `escapeXmlText` (line 4) does `.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")`; `sanitizePromptLeaf` (11-15) applies it to every leaf when `wrapFormat === "xml"`. It's called for essentially all user-authored leaf content during assembly: messages via `services/prompt/macro-context.ts:411`, lorebook / world-info via `services/prompt/marker-expander.ts:356/366/368`, character fields via `services/generation/character-prompt-context.ts:120/206`, summaries via `services/prompt/assembler.ts:775`, plus memory/scene contexts that call `escapeXmlText` directly. **What's happening** In XML wrap mode the assembler escapes leaf content so a user's literal text can't break the well-formedness of the XML wrapper (and can't inject a closing tag). That intent is reasonable for `<` and `&`, but: (a) `>` is swept in even though XML never requires it, which is what breaks markdown blockquotes; and (b) the escaped result is what the model literally reads, so even the `<`/`&` escaping shows up as visible entities and extra tokens. **Suggested fix** - **At minimum, stop escaping `>`** in `escapeXmlText` — drop the `.replace(/>/g, "&gt;")`. This immediately fixes markdown blockquotes and removes the most common token waste, with no loss of well-formedness (a bare `>` is valid XML text). If you want to be fully strict, escape only the literal `]]>` sequence instead. - **Reconsider escaping `<` / `&` for leaf content**, or make it less aggressive: escaping `<` is a deliberate anti-injection / well-formedness measure, but its side effect is that the model reads `&lt;...&gt;` for any legitimately-typed `<tag>` (the `<Setting>` case) and pays extra tokens. Options: only escape `<` when it actually forms a sequence that could be mistaken for one of the framework's own wrapper tags; make XML-escaping opt-in; or document the tradeoff so users know to pick Markdown/None when they rely on literal angle brackets. --- *AI-investigated against v2.0.6 from the user's report (Peek Prompt + a model echo-back test). The repro is the user's; the code location is from a code read — please confirm before fixing.*
bsakaguchi 2026-10-21 05:41:31 +07:00
  • closed this issue
  • added the
    in-pr
    label
Author
Owner

@SpicyMarinara commented on GitHub (Jun 30, 2026):

Addressed in draft PR #3109. The fix keeps user-authored > literal in XML-wrapped prompt leaves so blockquotes reach the model as typed, while < and & remain escaped for prompt-boundary safety; prompt regression coverage was added.

<!-- gh-comment-id:4846158362 --> @SpicyMarinara commented on GitHub (Jun 30, 2026): Addressed in draft PR #3109. The fix keeps user-authored `>` literal in XML-wrapped prompt leaves so blockquotes reach the model as typed, while `<` and `&` remain escaped for prompt-boundary safety; prompt regression coverage was added.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1667
No description provided.