[GH-ISSUE #3377] [Issue]: Persona/character card info is duplicated in the prompt when a description starts with a macro (conversation / preset-less modes) #1825

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

Persona (and character) card info can be injected into the system prompt twice — once from your own prompt/macro, and once from an automatic "identity fallback" that fails to notice the info is already there. You get a duplicated description/personality block.

The report is right that this happens, but two of its details don't hold up in the code, so here's the corrected version:

  • It is not persona-specific. Persona and character run the identical code path, so a character whose description starts with a macro duplicates the same way. There is no "character macros suppress, persona doesn't" asymmetry.
  • The persona section marker in a preset does not trigger it. In roleplay/VN with a preset, the marker (preset assembler) path and the auto-inserter are mutually exclusive. The real trigger is inline text/field macros — in conversation mode (where the fallback always runs), or in a non-game mode with no preset selected.

The everyday trigger: personas very commonly start their description with {{user}} (or {{char}}), which is exactly what defeats the duplicate-detection.

How to reproduce (try it yourself)

  1. Create a persona whose Description starts with a macro, e.g. {{user}} is a rival journalist who ….
  2. Start a conversation-mode chat (or any non-game chat with no prompt preset selected).
  3. Put the persona text macro ({{persona}}, or just {{personaDescription}}) into your system/prompt text.
  4. Send a message and inspect the assembled prompt (Peek Prompt / debug).

You'll see: the persona description appears twice — once where your macro resolved it, and once in an auto-injected system block.
Expected: exactly once.
Control that proves the cause: change the description to plain text with no leading macro → it appears once. (The same duplication reproduces for a character whose description starts with a macro.)

Where it is

packages/server/src/services/generation/character-prompt-context.ts — injectIdentityFallbackMessages (133-217) re-injects card info unless hasProfileBlock (123-130) says it's already present. The character gate is line 151 and the persona gate line 188 — identical logic. hasProfileBlock's only mode-independent check compares the unresolved description (args.personaDescription, macros intact) against the resolved prompt text:

content.includes(description.split("\n")[0].trim().slice(0, 80))   // line 126

so a macro in the description's first line makes the two strings differ and the check misses. The other checks — <xmlTag> (127), <name> (128), # name (129) — only match when the info was wrapped with the entity name, but inline text macros never emit a name wrapper and wrapFormat="none" emits none. Detection returns false and the block is spliced in a second time (persona 213-216, character 182-185).

This fallback runs (so the bug is reachable) only where shouldInjectIdentityFallback returns true (packages/server/src/routes/generate/generate-route-utils.ts:1160-1169: game → never, conversation → always, other modes → only when no preset). A parallel copy of the gate lives in packages/server/src/routes/chats.routes.ts (~2393-2415).

What's happening

The fallback decides "is this identity already in the prompt?" by fuzzy, after-the-fact text matching — comparing an unresolved description against resolved prompt text. When the description leads with a macro (or was surfaced without a name wrapper), the match fails and the card info is added again.

Suggested fix

Make detection macro-aware — but do it carefully so it doesn't reintroduce the macro-resolution cost problem from PR #3118 (see the note below). Two options, preferred first:

  • Preferred — deterministic "already presented" flag (zero extra macro resolution). Instead of re-deriving presence by substring, track whether the identity was surfaced and skip the fallback when it was. This mirrors what the assembler already does for chat_summary (assembler.ts:362 / 380-387 / 524). In conversation mode (no marker pipeline) this can be a check for the persona/char macro token in the pre-resolution prompt text — still no macro resolution. This is the most robust and the least risky performance-wise.
  • Otherwise — resolve once and reuse. If you resolve the description for the substring check, resolve it a single time with the resolver the block path already uses (args.resolvePromptMacros(personaDescription) for persona; the loop's resolveCharacterMacros(character.description) for characters — note the character check at line 151 must move to after characterMacroContext is built at 153-166) and reuse that same resolved string for both the hasProfileBlock detection and the injected block (persona line 192, character 170). Do not resolve twice, and do not touch resolveMacros in packages/shared or any client display path.

Apply the same fix to the parallel gate in chats.routes.ts (~2393-2415), and extend scripts/regressions/prompt.regression.ts (it already imports this fallback and guards the #3118 lazy-persona behavior) with: a macro-leading description → identity appears exactly once; a plain description → unchanged; and byte-identical macro output preserved.

Performance & regression note (re: PR #3118)

#3118 fixed a super-linear freeze caused by resolveMacros eagerly rebuilding persona text per message on every display render (client-side, ChatMessage/ConversationMessage/GameNarration). This fix is not on that surface: injectIdentityFallbackMessages runs server-side, once per generation during prompt assembly (generate.routes.ts:2177), so at most it adds one description-resolve per identity per generation — negligible next to generation itself, and post-#3118 a single resolve is ~1 ms even for a pathological self-referential persona. The guardrails to keep it that way: (1) resolve at most once and reuse (or use the deterministic flag and resolve nothing extra); (2) use the resolver the block path already uses, so deferred-character-macro (deferCharacterMacros) and budget-cap semantics are unchanged and macro output stays byte-identical; (3) make no change to packages/shared/src/utils/macro-engine.ts or any per-render display path, so #3118's early-out and lazy-persona optimizations are untouched.


AI-investigated against the current tree (v2.0.9); the fallback gate, hasProfileBlock, shouldInjectIdentityFallback, the persona/character symmetry, the server-once-per-generation call site, and the existing regression harness all read and confirmed. The original report's two inaccuracies (persona-only; marker-triggered) are corrected above. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jul 8, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3377 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** Persona (and character) card info can be injected into the system prompt **twice** — once from your own prompt/macro, and once from an automatic "identity fallback" that fails to notice the info is already there. You get a duplicated description/personality block. The report is right that this happens, but two of its details don't hold up in the code, so here's the corrected version: - **It is not persona-specific.** Persona and character run the *identical* code path, so a character whose description starts with a macro duplicates the same way. There is no "character macros suppress, persona doesn't" asymmetry. - **The persona section *marker* in a preset does not trigger it.** In roleplay/VN with a preset, the marker (preset assembler) path and the auto-inserter are mutually exclusive. The real trigger is inline text/field macros — in **conversation mode** (where the fallback always runs), or in a non-game mode with **no preset** selected. The everyday trigger: personas very commonly start their description with `{{user}}` (or `{{char}}`), which is exactly what defeats the duplicate-detection. **How to reproduce (try it yourself)** 1. Create a persona whose **Description starts with a macro**, e.g. `{{user}} is a rival journalist who …`. 2. Start a **conversation-mode** chat (or any non-game chat with **no** prompt preset selected). 3. Put the persona text macro (`{{persona}}`, or just `{{personaDescription}}`) into your system/prompt text. 4. Send a message and inspect the assembled prompt (Peek Prompt / debug). **You'll see:** the persona description appears **twice** — once where your macro resolved it, and once in an auto-injected system block. **Expected:** exactly once. **Control that proves the cause:** change the description to plain text with no leading macro → it appears once. (The same duplication reproduces for a *character* whose description starts with a macro.) **Where it is** `packages/server/src/services/generation/character-prompt-context.ts` — `injectIdentityFallbackMessages` (133-217) re-injects card info unless `hasProfileBlock` (123-130) says it's already present. The character gate is line 151 and the persona gate line 188 — identical logic. `hasProfileBlock`'s only mode-independent check compares the **unresolved** description (`args.personaDescription`, macros intact) against the **resolved** prompt text: ``` content.includes(description.split("\n")[0].trim().slice(0, 80)) // line 126 ``` so a macro in the description's first line makes the two strings differ and the check misses. The other checks — `<xmlTag>` (127), `<name>` (128), `# name` (129) — only match when the info was wrapped with the entity name, but inline text macros never emit a name wrapper and `wrapFormat="none"` emits none. Detection returns false and the block is spliced in a second time (persona 213-216, character 182-185). This fallback runs (so the bug is reachable) only where `shouldInjectIdentityFallback` returns true (`packages/server/src/routes/generate/generate-route-utils.ts:1160-1169`: game → never, conversation → always, other modes → only when no preset). A parallel copy of the gate lives in `packages/server/src/routes/chats.routes.ts` (~2393-2415). **What's happening** The fallback decides "is this identity already in the prompt?" by fuzzy, after-the-fact text matching — comparing an *unresolved* description against *resolved* prompt text. When the description leads with a macro (or was surfaced without a name wrapper), the match fails and the card info is added again. **Suggested fix** Make detection macro-aware — but do it carefully so it doesn't reintroduce the macro-resolution cost problem from PR #3118 (see the note below). Two options, preferred first: - **Preferred — deterministic "already presented" flag (zero extra macro resolution).** Instead of re-deriving presence by substring, track whether the identity was surfaced and skip the fallback when it was. This mirrors what the assembler already does for `chat_summary` (`assembler.ts:362 / 380-387 / 524`). In conversation mode (no marker pipeline) this can be a check for the persona/char macro token in the **pre-resolution** prompt text — still no macro resolution. This is the most robust and the least risky performance-wise. - **Otherwise — resolve once and reuse.** If you resolve the description for the substring check, resolve it a **single time** with the resolver the block path already uses (`args.resolvePromptMacros(personaDescription)` for persona; the loop's `resolveCharacterMacros(character.description)` for characters — note the character check at line 151 must move to after `characterMacroContext` is built at 153-166) and reuse that same resolved string for **both** the `hasProfileBlock` detection **and** the injected block (persona line 192, character 170). Do **not** resolve twice, and do **not** touch `resolveMacros` in `packages/shared` or any client display path. Apply the same fix to the parallel gate in `chats.routes.ts` (~2393-2415), and extend `scripts/regressions/prompt.regression.ts` (it already imports this fallback and guards the #3118 lazy-persona behavior) with: a macro-leading description → identity appears exactly once; a plain description → unchanged; and byte-identical macro output preserved. **Performance & regression note (re: [PR #3118](https://github.com/Pasta-Devs/Marinara-Engine/pull/3118))** #3118 fixed a super-linear freeze caused by `resolveMacros` eagerly rebuilding persona text **per message on every display render** (client-side, `ChatMessage`/`ConversationMessage`/`GameNarration`). This fix is **not** on that surface: `injectIdentityFallbackMessages` runs **server-side, once per generation** during prompt assembly (`generate.routes.ts:2177`), so at most it adds **one** description-resolve per identity per generation — negligible next to generation itself, and post-#3118 a single resolve is ~1 ms even for a pathological self-referential persona. The guardrails to keep it that way: (1) resolve at most once and reuse (or use the deterministic flag and resolve nothing extra); (2) use the resolver the block path already uses, so deferred-character-macro (`deferCharacterMacros`) and budget-cap semantics are unchanged and macro output stays byte-identical; (3) make no change to `packages/shared/src/utils/macro-engine.ts` or any per-render display path, so #3118's early-out and lazy-persona optimizations are untouched. --- *AI-investigated against the current tree (v2.0.9); the fallback gate, `hasProfileBlock`, `shouldInjectIdentityFallback`, the persona/character symmetry, the server-once-per-generation call site, and the existing regression harness all read and confirmed. The original report's two inaccuracies (persona-only; marker-triggered) are corrected above. Please verify before fixing.*
bsakaguchi 2026-10-21 05:43:02 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Jul 8, 2026):

Addressed in #3388 and merged to staging.

<!-- gh-comment-id:4918159211 --> @SpicyMarinara commented on GitHub (Jul 8, 2026): Addressed in #3388 and merged to staging.
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#1825
No description provided.