[PR #3440] [MERGED] fix(macros): resolve flat-pass macros in {{#if}} conditions + trim standalone blocks #5936

Closed
opened 2026-10-23 04:05:22 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/3440
Author: @kolacheee
Created: 7/10/2026
Status: ✅ Merged
Merged: 7/10/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/3435-convo-macros-in-if


📝 Commits (2)

  • 666cba8 fix(macros): resolve conversation macros in {{#if}} and trim standalone blocks
  • ded115e fix(macros): resolve any flat-pass macro in bare {{#if}} conditions

📊 Changes

1 file changed (+56 additions, -6 deletions)

View changed files

📝 packages/shared/src/utils/macro-engine.ts (+56 -6)

📄 Description

Linked issue

Closes #3435

Why this change

Two problems with the new (and old) preset macros inside {{#if}} blocks:

  1. (functional) {{#if MACRO != ""}} was always true for a whole class of macros. The {{#if}} condition evaluator resolved bare operands from a hand-maintained switch in resolveConditionalOperand, and any macro not listed fell through to the literal-token fallback ("MACRO" != "" → always true). That switch had drifted out of sync with the flat {{...}} pass. The reported case was the conversation macros ({{char_about}}, {{persona_about}}, {{convo_display}}, {{convo_behavior}}), but an audit found the same bug for {{date}}/{{time}}/{{weekday}}/{{timezone}}/{{datetime}}, {{lastGenerationType}}, {{idle_duration}}, {{agent::TYPE}}, {{random}}/{{roll}}, and {{getvar::name}} — all broken in bare form (they worked only in the braced {{ {{macro}} }} form).

  2. (cosmetic) A standalone {{#if}} block left an extra blank line — whether removed or rendered.

What changed

packages/shared/src/utils/macro-engine.ts:

  • General fix for #1: instead of adding switch cases one by one (which is how the drift happened), resolveConditionalOperand now routes any unresolved bare operand through the same flat pass used for {{token}}. So every read macro valid in {{...}} is automatically testable bare in {{#if}}, and it stays in sync forever. Guards:
    • never runs a variable write macro (setvar/addvar/incvar/decvar) as an operand side-effect;
    • unknown tokens stay literal (the flat pass leaves them as-is), so plain words/numbers still compare as themselves — unchanged behavior;
    • forces concrete resolution (deferCharacterMacros off) so a group chat compares the real value, not a deferred per-character placeholder.
  • Fix for #2: standalone {{#if}}/{{/if}} tags get Jinja-style trim_blocks/lstrip_blocks, judged from the raw layout, so a removed or rendered block no longer leaves a blank line. Inline tags and else branches are left as authored. Pre-tag text is resolved before the condition, preserving {{setvar}}-before-condition ordering.

Out of scope (handled separately): the relocation macros ({{memories}}, {{lorebook}}, {{commands}}, {{context}}, {{reactRules}}) still can't be {{#if}}-tested here — their value is injected by the route after macro resolution, so there's nothing to compare at this layer. That needs route-level handling and is being done in a follow-up.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Automated checks I ran locally (green):

  • pnpm --filter @marinara-engine/shared build (tsc) and … lint — clean
  • pnpm regression:prompt — full prompt/macro regression suite passes
  • A throwaway targeted script (not committed) exercising the fix directly:
    • bare {{#if char_about != ""}} / persona_about / convo_behavior, {{#if lastGenerationType == "regenerate"}}, {{#if idle_duration != ""}}, {{#if agent::tracker != ""}}, {{#if date != ""}} all test the real value now
    • guards: unknown bare word still always-true (unchanged); {{#if setvar::x::1}} does not execute the write; set variables resolve; {{#if var:mood == "happy"}} and {{#if char == "Kat"}} unchanged; braced form still works
    • Bug 4: removed/rendered standalone blocks leave no blank line

Still to verify manually in the app (please tick above once done):

  1. In a Conversation-mode preset, gate a block on {{#if char_about != ""}} and generate with an empty vs. non-empty about-me — confirm hide vs. render and no blank-line gaps.
  2. Confirm {{#if lastGenerationType == "regenerate"}} / {{#if {{weekday}} == "Monday"}} behave as expected.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

Macro-reference docs (including which macros are {{#if}}-testable) are tracked in #3437; no version bump here.

UI evidence (if applicable)

N/A (prompt-assembly logic; no UI surface changed).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/3440 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 7/10/2026 **Status:** ✅ Merged **Merged:** 7/10/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/3435-convo-macros-in-if` --- ### 📝 Commits (2) - [`666cba8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/666cba879d30f7e5f5f37f022b6372177e625f79) fix(macros): resolve conversation macros in {{#if}} and trim standalone blocks - [`ded115e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ded115e5309729379b2d74f8130a4b03c23796e0) fix(macros): resolve any flat-pass macro in bare {{#if}} conditions ### 📊 Changes **1 file changed** (+56 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `packages/shared/src/utils/macro-engine.ts` (+56 -6) </details> ### 📄 Description <!-- Target branch: `staging`. --> ## Linked issue Closes #3435 ## Why this change Two problems with the new (and old) preset macros inside `{{#if}}` blocks: 1. **(functional)** `{{#if MACRO != ""}}` was **always true** for a whole class of macros. The `{{#if}}` condition evaluator resolved bare operands from a **hand-maintained switch** in `resolveConditionalOperand`, and any macro not listed fell through to the literal-token fallback (`"MACRO" != ""` → always true). That switch had drifted out of sync with the flat `{{...}}` pass. The reported case was the conversation macros (`{{char_about}}`, `{{persona_about}}`, `{{convo_display}}`, `{{convo_behavior}}`), but an audit found the same bug for `{{date}}`/`{{time}}`/`{{weekday}}`/`{{timezone}}`/`{{datetime}}`, `{{lastGenerationType}}`, `{{idle_duration}}`, `{{agent::TYPE}}`, `{{random}}`/`{{roll}}`, and `{{getvar::name}}` — all broken in **bare** form (they worked only in the braced `{{ {{macro}} }}` form). 2. **(cosmetic)** A standalone `{{#if}}` block left an extra **blank line** — whether removed or rendered. ## What changed `packages/shared/src/utils/macro-engine.ts`: - **General fix for #1:** instead of adding switch cases one by one (which is how the drift happened), `resolveConditionalOperand` now routes any unresolved bare operand through the **same flat pass** used for `{{token}}`. So every read macro valid in `{{...}}` is automatically testable bare in `{{#if}}`, and it stays in sync forever. Guards: - never runs a variable **write** macro (`setvar`/`addvar`/`incvar`/`decvar`) as an operand side-effect; - unknown tokens stay literal (the flat pass leaves them as-is), so plain words/numbers still compare as themselves — unchanged behavior; - forces concrete resolution (`deferCharacterMacros` off) so a group chat compares the real value, not a deferred per-character placeholder. - **Fix for #2:** standalone `{{#if}}`/`{{/if}}` tags get Jinja-style `trim_blocks`/`lstrip_blocks`, judged from the raw layout, so a removed **or** rendered block no longer leaves a blank line. Inline tags and `else` branches are left as authored. Pre-tag text is resolved before the condition, preserving `{{setvar}}`-before-condition ordering. **Out of scope (handled separately):** the **relocation** macros (`{{memories}}`, `{{lorebook}}`, `{{commands}}`, `{{context}}`, `{{reactRules}}`) still can't be `{{#if}}`-tested here — their value is injected by the route *after* macro resolution, so there's nothing to compare at this layer. That needs route-level handling and is being done in a follow-up. ## Validation <!-- Required: include commands run and/or manual checks performed --> - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes Automated checks I ran locally (green): - `pnpm --filter @marinara-engine/shared build` (tsc) and `… lint` — clean - `pnpm regression:prompt` — full prompt/macro regression suite passes - A throwaway targeted script (not committed) exercising the fix directly: - bare `{{#if char_about != ""}}` / `persona_about` / `convo_behavior`, `{{#if lastGenerationType == "regenerate"}}`, `{{#if idle_duration != ""}}`, `{{#if agent::tracker != ""}}`, `{{#if date != ""}}` all test the real value now - guards: unknown bare word still always-true (unchanged); `{{#if setvar::x::1}}` does **not** execute the write; set variables resolve; `{{#if var:mood == "happy"}}` and `{{#if char == "Kat"}}` unchanged; braced form still works - Bug 4: removed/rendered standalone blocks leave no blank line Still to verify manually in the app (please tick above once done): 1. In a Conversation-mode preset, gate a block on `{{#if char_about != ""}}` and generate with an empty vs. non-empty about-me — confirm hide vs. render and no blank-line gaps. 2. Confirm `{{#if lastGenerationType == "regenerate"}}` / `{{#if {{weekday}} == "Monday"}}` behave as expected. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) Macro-reference docs (including which macros are `{{#if}}`-testable) are tracked in #3437; no version bump here. ## UI evidence (if applicable) N/A (prompt-assembly logic; no UI surface changed). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:05:22 +07:00
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#5936
No description provided.