[PR #3451] [MERGED] fix(macros): resolve #3449 relocation-{{#if}} regressions (context dup, slot over-match, else spacing) #5944

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

📋 Pull Request Information

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

Base: staging ← Head: fix/3449-followup-relocation-if


📝 Commits (1)

  • 9be2011 fix(macros): resolve #3449 relocation-{{#if}} regressions

📊 Changes

2 files changed (+75 additions, -30 deletions)

View changed files

📝 packages/server/src/routes/generate.routes.ts (+41 -27)
📝 packages/shared/src/utils/macro-engine.ts (+34 -3)

📄 Description

Linked issue

Closes #3450 (fast-follow to the merged #3449 / #3448)

Why this change

A post-merge adversarial review of #3449 (relocation macros in {{#if}}) found three regressions that shipped to staging:

  1. Context double-injection (medium). A preset referencing {{context}} only inside a deferred conditional leaves no literal {{context}} after deferral. The context tail-fallback keyed on replaceConversationContextMacro's return — which is false for a deferred token — so it pushed the context block as a trailing user message and the decode pass inlined it. The current-context block (latest user message, status, activity, timestamp, mentions) was sent twice, every turn.
  2. Slot-detection over-match drops retrieval (medium). Slot detection used a loose regex over {{#if …}} bodies. {{#if topic == "memories"}} (string literal) or {{#if user.status == "x"}} (dotted operand) matched the alias word and flipped the slot true, while the parser-based defer predicate correctly declined. With the slot wrongly set, the code took the "macro placed" branch (replacing a bare {{memories}} that isn't there) instead of the normal recall injection — silently dropping memory recall / react rules / commands / lorebook.
  3. Deferred else-branch blank line (low). trimDeferredStandaloneBranches trimmed the leading newline only for branch 0, so a selected {{else}}/{{else if}} branch kept a stray blank line the evaluate-now path strips.

What changed

Root cause of #1 and #2 is a slot-detection path that disagreed with the parser-based defer decision. Fix makes them share one parser:

  • packages/shared/src/utils/macro-engine.ts:
    • New collectDeferredRelocationConditionOperands(text) — reads the actual deferred tokens and returns their condition operands (same parseConditionExpression the deferral uses).
    • trimDeferredStandaloneBranches: trim the leading newline of whichever branch is selected, not just branch 0.
  • packages/server/src/routes/generate.routes.ts:
    • Replaced the loose conversationContextMacroConditionPattern regex with token-driven slot detection: after the conversation macro pass, map each deferred-token operand back to its key via relocationKeyForOperand (now shared with the defer predicate) and OR into the slots. Slot detection and deferral can no longer disagree — fixing both the over-match (dropped retrieval) and the inverse braced-operand miss. relocationKeyForOperand stays scoped to CONVERSATION_RELOCATION_MACRO_KEYS, preserving the About-me exclusion and covering replyRules.
    • The context fallback now keys on conversationContextMacroSlots.context instead of the replace() return, so a deferred {{#if context}} no longer double-injects.

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 + pnpm --filter @marinara-engine/server lint (tsc) — clean
  • pnpm regression:prompt — full suite passes (61 checks, incl. the deferred-relocation cases added with #3449)
  • A throwaway targeted script (not committed) confirming: deferred else/else-if branch collapses with no leading blank line; {{#if topic == "memories"}} and {{#if user.status …}} do not defer and yield no relocation operand (so retrieval is unaffected); {{#if char_about}} (about-me field) is not deferred; real bare / braced-mixed / {{#if replyRules}} references defer and collect their key operand.

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

  1. Preset with {{#if context != ""}}…{{context}}…{{/if}} and no bare {{context}} → context block appears once.
  2. Preset with {{#if topic == "memories"}}…{{/if}} and no bare {{memories}} → memory recall still injected.
  3. Deferred {{#if memories != ""}}…{{else}}…{{/if}} with empty memories → else branch renders with no leading blank line.

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)

No docs/version changes; behavior-only bug fixes.

UI evidence (if applicable)

N/A (prompt-assembly logic).

Summary by CodeRabbit

  • Bug Fixes
    • Improved conversation prompt handling for relocation macros used inside deferred conditional blocks.
    • Prevented duplicate context insertion when context is referenced conditionally.
    • Improved whitespace and newline handling across deferred conditional branches.

🔄 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/3451 **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/3449-followup-relocation-if` --- ### 📝 Commits (1) - [`9be2011`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9be2011b4ee253ee15432b363df5a6ac3f36d24a) fix(macros): resolve #3449 relocation-{{#if}} regressions ### 📊 Changes **2 files changed** (+75 additions, -30 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/generate.routes.ts` (+41 -27) 📝 `packages/shared/src/utils/macro-engine.ts` (+34 -3) </details> ### 📄 Description <!-- Target branch: `staging`. --> ## Linked issue Closes #3450 (fast-follow to the merged #3449 / #3448) ## Why this change A post-merge adversarial review of #3449 (relocation macros in `{{#if}}`) found three regressions that shipped to `staging`: 1. **Context double-injection (medium).** A preset referencing `{{context}}` only inside a deferred conditional leaves no literal `{{context}}` after deferral. The context tail-fallback keyed on `replaceConversationContextMacro`'s return — which is `false` for a deferred token — so it pushed the context block as a trailing user message **and** the decode pass inlined it. The current-context block (latest user message, status, activity, timestamp, mentions) was sent **twice, every turn**. 2. **Slot-detection over-match drops retrieval (medium).** Slot detection used a loose regex over `{{#if …}}` bodies. `{{#if topic == "memories"}}` (string literal) or `{{#if user.status == "x"}}` (dotted operand) matched the alias word and flipped the slot true, while the parser-based defer predicate correctly declined. With the slot wrongly set, the code took the "macro placed" branch (replacing a bare `{{memories}}` that isn't there) instead of the normal recall injection — **silently dropping memory recall / react rules / commands / lorebook**. 3. **Deferred else-branch blank line (low).** `trimDeferredStandaloneBranches` trimmed the leading newline only for branch 0, so a selected `{{else}}`/`{{else if}}` branch kept a stray blank line the evaluate-now path strips. ## What changed Root cause of #1 and #2 is a slot-detection path that disagreed with the parser-based defer decision. Fix makes them share one parser: - `packages/shared/src/utils/macro-engine.ts`: - New `collectDeferredRelocationConditionOperands(text)` — reads the **actual** deferred tokens and returns their condition operands (same `parseConditionExpression` the deferral uses). - `trimDeferredStandaloneBranches`: trim the leading newline of **whichever** branch is selected, not just branch 0. - `packages/server/src/routes/generate.routes.ts`: - Replaced the loose `conversationContextMacroConditionPattern` regex with token-driven slot detection: after the conversation macro pass, map each deferred-token operand back to its key via `relocationKeyForOperand` (now shared with the defer predicate) and OR into the slots. Slot detection and deferral can no longer disagree — fixing both the over-match (dropped retrieval) and the inverse braced-operand miss. `relocationKeyForOperand` stays scoped to `CONVERSATION_RELOCATION_MACRO_KEYS`, preserving the About-me exclusion and covering `replyRules`. - The context fallback now keys on `conversationContextMacroSlots.context` instead of the `replace()` return, so a deferred `{{#if context}}` no longer double-injects. ## 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` + `pnpm --filter @marinara-engine/server lint` (tsc) — clean - `pnpm regression:prompt` — full suite passes (61 checks, incl. the deferred-relocation cases added with #3449) - A throwaway targeted script (not committed) confirming: deferred else/else-if branch collapses with no leading blank line; `{{#if topic == "memories"}}` and `{{#if user.status …}}` do **not** defer and yield no relocation operand (so retrieval is unaffected); `{{#if char_about}}` (about-me field) is not deferred; real bare / braced-mixed / `{{#if replyRules}}` references defer and collect their key operand. Still to verify manually in the app (please tick above once done): 1. Preset with `{{#if context != ""}}…{{context}}…{{/if}}` and no bare `{{context}}` → context block appears **once**. 2. Preset with `{{#if topic == "memories"}}…{{/if}}` and no bare `{{memories}}` → memory recall still injected. 3. Deferred `{{#if memories != ""}}…{{else}}…{{/if}}` with empty memories → else branch renders with no leading blank line. ## 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) No docs/version changes; behavior-only bug fixes. ## UI evidence (if applicable) N/A (prompt-assembly logic). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved conversation prompt handling for relocation macros used inside deferred conditional blocks. * Prevented duplicate context insertion when context is referenced conditionally. * Improved whitespace and newline handling across deferred conditional branches. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <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:25 +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#5944
No description provided.