[PR #3449] [MERGED] feat(macros): support relocation macros in {{#if}} conditions (route-level deferral) #5943

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/3449
Author: @kolacheee
Created: 7/10/2026
Status: ✅ Merged
Merged: 7/10/2026
Merged by: @SpicyMarinara

Base: staging ← Head: feat/3448-relocation-if-conditions


📝 Commits (5)

  • 552b631 feat(macros): support relocation macros in {{#if}} via route-level deferral
  • 45ed2f7 Merge remote-tracking branch 'origin/staging' into HEAD
  • b0b5532 Support reply rules in deferred relocation conditions
  • 5fc4eb1 Merge remote-tracking branch 'origin/staging' into HEAD
  • 7cbd51f Keep About Me fields out of relocation deferral

📊 Changes

3 files changed (+274 additions, -33 deletions)

View changed files

📝 packages/server/src/routes/generate.routes.ts (+138 -10)
📝 packages/shared/src/utils/macro-engine.ts (+97 -19)
📝 scripts/regressions/prompt.regression.ts (+39 -4)

📄 Description

Linked issue

Closes #3448

Stacked on #3440. This branch builds on #3440 (both edit resolveConditionalBlocks), so its diff includes #3440's commits until that merges. Merge #3440 first, then this rebases to just its own changes.

Why this change

The conversation relocation macros — {{context}}/{{status}}, {{commands}}, {{reactRules}}, {{memories}}, {{lorebook}} — could not be tested in {{#if}}. Their value is injected by the route (replaceConversationContextMacro) after the macro engine runs, so at condition-evaluation time there's nothing to compare and {{#if memories != ""}} was always true. A preset like:

{{#if memories != ""}}
## Memories
{{memories}}
{{/if}}

always rendered the ## Memories header, even with no memories. (Follow-up to #3435, which fixed the value macros.)

What changed

A generic, opt-in deferral hook that reuses the engine's proven per-character-deferral mechanism.

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

  • New ResolveMacroOptions.deferConditionalOperand?: (operand) => boolean. When a {{#if}} condition references such an operand, the block is encoded as a deferred token (distinct DEFERRED_RELOCATION_... prefix so it never collides with the character-macro decode) with the standalone-block whitespace trim baked into the stored branches, instead of being evaluated. The engine stays mode-agnostic — it never hardcodes relocation names.
  • Exposes selectConditionalPayloadBranch, parseDeferredConditionalPayload, DEFERRED_RELOCATION_CONDITIONAL_TOKEN_RE, and a hasDeferredRelocationConditionals guard for the route decode.

packages/server/src/routes/generate.routes.ts:

  • Supplies the relocation predicate at the conversation prompt pass.
  • Extends slot detection to recognize a relocation alias used only as a {{#if}} operand (a header-only block with no bare {{memories}}), so retrieval still runs.
  • After every relocation value is known (post memory recall), decodeDeferredRelocationConditionals evaluates each token against the real values — seeded as variables under every alias spelling so bare and braced operands both resolve — and substitutes the inner relocation macros. Nested relocation conditionals inside a deferred block resolve too.
  • A defensive strip before the provider request guarantees no control-sentinel can leak.

Scope / known limitations

  • A single condition mixing a character macro and a relocation macro (e.g. {{#if memories contains char}}) is unsupported — character deferral takes precedence. In Conversation mode character deferral is off, so this doesn't arise there.
  • The dry-run debug preview does not evaluate these conditionals (it has no live relocation content), so such blocks always appear in the preview. Documented; a preview mirror can be a fast-follow.
  • Unusual casing of a relocation macro in a condition (e.g. {{#if MEMORIES}}) stays always-true (no regression) rather than resolving.

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/server lint and pnpm --filter @marinara-engine/shared lint (tsc) — clean
  • pnpm regression:prompt — full prompt/macro regression suite passes
  • A throwaway targeted script (not committed) exercising the engine defer + a route-equivalent decode:
    • {{#if memories != ""}}…{{memories}}{{/if}} renders flush when content exists, is hidden (no blank line) when empty
    • braced {{#if {{lorebook}} != ""}}, operators (contains), aliases ({{memoryRecall}}), inner {{char}}, and nested relocation conditionals all resolve
    • non-relocation conditions ({{#if char == "Kat"}}) are NOT deferred; a quoted literal that looks like a macro name does not defer

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

  1. Conversation preset with {{#if memories != ""}}## Memories\n{{memories}}{{/if}} → confirm the header appears only when memories exist, with clean spacing.
  2. Same for {{#if lorebook != ""}} / {{#if commands != ""}} / {{#if reactRules != ""}} / {{#if context != ""}}.
  3. A header-only block ({{#if memories}}…{{/if}} with no bare {{memories}}) still shows memories when present (retrieval triggered).
  4. Impersonate: {{#if commands}} is empty (commands are suppressed when impersonating).

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)

The macro reference (#3437/#3446) can gain a note that relocation macros are now {{#if}}-testable; 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/3449 **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:** `feat/3448-relocation-if-conditions` --- ### 📝 Commits (5) - [`552b631`](https://github.com/Pasta-Devs/Marinara-Engine/commit/552b6314dc7f18e9aa8cd76c01fdfaafdce94d4d) feat(macros): support relocation macros in {{#if}} via route-level deferral - [`45ed2f7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/45ed2f759ce4c28c46c01fb821bf4eb7792ae164) Merge remote-tracking branch 'origin/staging' into HEAD - [`b0b5532`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b0b553276808693c6f77b5f4de0fc8b5f8345281) Support reply rules in deferred relocation conditions - [`5fc4eb1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5fc4eb110a6d903931d562af01052f3db4ffa99d) Merge remote-tracking branch 'origin/staging' into HEAD - [`7cbd51f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7cbd51f3e441f1626a71ecac78c06914ee593de5) Keep About Me fields out of relocation deferral ### 📊 Changes **3 files changed** (+274 additions, -33 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/routes/generate.routes.ts` (+138 -10) 📝 `packages/shared/src/utils/macro-engine.ts` (+97 -19) 📝 `scripts/regressions/prompt.regression.ts` (+39 -4) </details> ### 📄 Description <!-- Target branch: `staging`. --> ## Linked issue Closes #3448 > **Stacked on #3440.** This branch builds on #3440 (both edit `resolveConditionalBlocks`), so its diff includes #3440's commits until that merges. **Merge #3440 first**, then this rebases to just its own changes. ## Why this change The conversation relocation macros — `{{context}}`/`{{status}}`, `{{commands}}`, `{{reactRules}}`, `{{memories}}`, `{{lorebook}}` — could not be tested in `{{#if}}`. Their value is injected by the route (`replaceConversationContextMacro`) **after** the macro engine runs, so at condition-evaluation time there's nothing to compare and `{{#if memories != ""}}` was always true. A preset like: ``` {{#if memories != ""}} ## Memories {{memories}} {{/if}} ``` always rendered the `## Memories` header, even with no memories. (Follow-up to #3435, which fixed the *value* macros.) ## What changed A generic, opt-in deferral hook that reuses the engine's proven per-character-deferral mechanism. **`packages/shared/src/utils/macro-engine.ts`:** - New `ResolveMacroOptions.deferConditionalOperand?: (operand) => boolean`. When a `{{#if}}` condition references such an operand, the block is encoded as a deferred token (distinct `DEFERRED_RELOCATION_...` prefix so it never collides with the character-macro decode) with the standalone-block whitespace trim baked into the stored branches, instead of being evaluated. The engine stays mode-agnostic — it never hardcodes relocation names. - Exposes `selectConditionalPayloadBranch`, `parseDeferredConditionalPayload`, `DEFERRED_RELOCATION_CONDITIONAL_TOKEN_RE`, and a `hasDeferredRelocationConditionals` guard for the route decode. **`packages/server/src/routes/generate.routes.ts`:** - Supplies the relocation predicate at the conversation prompt pass. - Extends slot detection to recognize a relocation alias used **only** as a `{{#if}}` operand (a header-only block with no bare `{{memories}}`), so retrieval still runs. - After every relocation value is known (post memory recall), `decodeDeferredRelocationConditionals` evaluates each token against the real values — seeded as variables under **every** alias spelling so bare and braced operands both resolve — and substitutes the inner relocation macros. Nested relocation conditionals inside a deferred block resolve too. - A defensive strip before the provider request guarantees no control-sentinel can leak. ## Scope / known limitations - A single condition mixing a **character** macro and a **relocation** macro (e.g. `{{#if memories contains char}}`) is unsupported — character deferral takes precedence. In Conversation mode character deferral is off, so this doesn't arise there. - The **dry-run debug preview** does not evaluate these conditionals (it has no live relocation content), so such blocks always appear in the preview. Documented; a preview mirror can be a fast-follow. - Unusual casing of a relocation macro in a condition (e.g. `{{#if MEMORIES}}`) stays always-true (no regression) rather than resolving. ## 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/server lint` and `pnpm --filter @marinara-engine/shared lint` (tsc) — clean - `pnpm regression:prompt` — full prompt/macro regression suite passes - A throwaway targeted script (not committed) exercising the engine defer + a route-equivalent decode: - `{{#if memories != ""}}…{{memories}}{{/if}}` renders flush when content exists, is hidden (no blank line) when empty - braced `{{#if {{lorebook}} != ""}}`, operators (`contains`), aliases (`{{memoryRecall}}`), inner `{{char}}`, and nested relocation conditionals all resolve - non-relocation conditions (`{{#if char == "Kat"}}`) are NOT deferred; a quoted literal that looks like a macro name does not defer Still to verify manually in the app (please tick above once done): 1. Conversation preset with `{{#if memories != ""}}## Memories\n{{memories}}{{/if}}` → confirm the header appears only when memories exist, with clean spacing. 2. Same for `{{#if lorebook != ""}}` / `{{#if commands != ""}}` / `{{#if reactRules != ""}}` / `{{#if context != ""}}`. 3. A header-only block (`{{#if memories}}…{{/if}}` with no bare `{{memories}}`) still shows memories when present (retrieval triggered). 4. Impersonate: `{{#if commands}}` is empty (commands are suppressed when impersonating). ## 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) The macro reference (#3437/#3446) can gain a note that relocation macros are now `{{#if}}`-testable; 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: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#5943
No description provided.