[GH-ISSUE #2939] [Issue]: {{#if}} blocks leak their raw tag text (e.g. {{else if}}) into the model's prompt and pick the wrong branch #1515

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

{{#if …}} conditional blocks can leak their own raw tag text (like {{else if …}}) straight into the prompt that gets sent to the model, and pick the wrong branch. So the model literally sees {{else if x}} in its context, and the character behaves oddly.

How to reproduce (try it yourself)

Case 1 — {{else if}} leaks. Put this in a character's Description or a lorebook entry:

{{#if 1==1}}It is one{{else if 2==2}}It is two{{/if}}

You'll see (in the prompt): It is one{{else if 2==2}}It is two — the {{else if 2==2}} tag is left in verbatim.
Expected: just It is one.

Case 2 — a macro inside the condition breaks it. First set a variable, then:

{{setvar::name::Bob}}{{#if {{getvar::name}} == "Bob"}}Hi Bob{{/if}}

You'll see: the block doesn't resolve correctly — leftover text like == "Bob"}}Hi Bob leaks through.
Expected: Hi Bob.

(Also affected: a second {{else}} in one block, and a stray/literal {{#if}} with no condition, both desync the parser and leak text.)

Where it is

packages/shared/src/utils/macro-engine.ts — findConditionalStart (line 562) and findConditionalEnd (571), used by resolveConditionalBlocks (600).

What's happening

The conditional parser uses plain regexes that stop at the first }} rather than the matching one, and it only knows three tags: {{#if}}, {{else}}, {{/if}}.

  • It has no concept of {{else if}}, so that tag is treated as ordinary text and leaks.
  • {{#if {{getvar::x}} == …}} — the condition regex [\s\S]*? stops at the inner macro's }}, so the condition is read as {{getvar::x and the rest spills out.
  • A bare {{#if}} (no condition) isn't recognized as a start but is counted by the end-scanner, throwing off the open/close depth count.

Suggested fix

Make the conditional parser brace-aware (reuse the existing balanced-brace matcher findBalancedMacroEnd that the rest of the engine already uses), resolve any macros inside the condition before matching the closing tag, and add support for {{else if …}} chains. At minimum, recognize and strip {{else if}} so it can never leak.


Found by a code review of v2.0.6; please verify the reproductions before fixing.

Originally created by @kolacheee on GitHub (Jun 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2939 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** `{{#if …}}` conditional blocks can leak their own raw tag text (like `{{else if …}}`) straight into the prompt that gets sent to the model, and pick the wrong branch. So the model literally sees `{{else if x}}` in its context, and the character behaves oddly. **How to reproduce (try it yourself)** Case 1 — `{{else if}}` leaks. Put this in a character's Description or a lorebook entry: ``` {{#if 1==1}}It is one{{else if 2==2}}It is two{{/if}} ``` **You'll see (in the prompt):** `It is one{{else if 2==2}}It is two` — the `{{else if 2==2}}` tag is left in verbatim. **Expected:** just `It is one`. Case 2 — a macro inside the condition breaks it. First set a variable, then: ``` {{setvar::name::Bob}}{{#if {{getvar::name}} == "Bob"}}Hi Bob{{/if}} ``` **You'll see:** the block doesn't resolve correctly — leftover text like `== "Bob"}}Hi Bob` leaks through. **Expected:** `Hi Bob`. (Also affected: a second `{{else}}` in one block, and a stray/literal `{{#if}}` with no condition, both desync the parser and leak text.) **Where it is** `packages/shared/src/utils/macro-engine.ts` — `findConditionalStart` (line 562) and `findConditionalEnd` (571), used by `resolveConditionalBlocks` (600). **What's happening** The conditional parser uses plain regexes that stop at the **first** `}}` rather than the matching one, and it only knows three tags: `{{#if}}`, `{{else}}`, `{{/if}}`. - It has no concept of `{{else if}}`, so that tag is treated as ordinary text and leaks. - `{{#if {{getvar::x}} == …}}` — the condition regex `[\s\S]*?` stops at the inner macro's `}}`, so the condition is read as `{{getvar::x` and the rest spills out. - A bare `{{#if}}` (no condition) isn't recognized as a start but *is* counted by the end-scanner, throwing off the open/close depth count. **Suggested fix** Make the conditional parser brace-aware (reuse the existing balanced-brace matcher `findBalancedMacroEnd` that the rest of the engine already uses), resolve any macros inside the condition before matching the closing tag, and add support for `{{else if …}}` chains. At minimum, recognize and strip `{{else if}}` so it can never leak. --- *Found by a code review of v2.0.6; please verify the reproductions before fixing.*
Author
Owner

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

Fixed on staging: conditional parsing is now brace-aware, supports else-if chains, resolves macros inside condition expressions, and avoids leaking raw conditional tags into the prompt. Added prompt regression coverage for else-if and nested getvar conditions. Validation: pnpm regression:prompt; pnpm check.

<!-- gh-comment-id:4827607978 --> @SpicyMarinara commented on GitHub (Jun 28, 2026): Fixed on staging: conditional parsing is now brace-aware, supports else-if chains, resolves macros inside condition expressions, and avoids leaking raw conditional tags into the prompt. Added prompt regression coverage for else-if and nested getvar conditions. Validation: pnpm regression:prompt; pnpm check.
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#1515
No description provided.