[GH-ISSUE #2677] [Issue]: Assistant prefill mis-applied during impersonation and Google user-message regeneration (wrong voice / corrupted saved message) #1325

Closed
opened 2026-10-21 05:31:34 +07:00 by bsakaguchi · 2 comments
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

The assistant prefill is appended/seeded without accounting for a following user-role message, so it is mis-applied in two cases: (1) impersonation — the prefill becomes the trailing assistant turn after the "write as {{user}}" instruction, making the model continue in the character's voice and leaking the prefill text into the saved user message; (2) Google user-message regeneration — the prefill is seeded into the saved swipe even though it is not the trailing turn, corrupting the regenerated user message. SillyTavern does not apply the assistant prefill during impersonation or user-message regeneration.

Expected behavior

  • Impersonate: the impersonate instruction should be the final turn so the model writes the user's line; the character prefill should not be appended (matching ST).
  • Google regen: when the prefill is not actually the trailing message the model continues from, it should not be prepended to / saved as the regenerated user message.

Actual behavior

appendGenerationTailMessages pushes the assistant prefill and only early-returns on follow-up iterations — it never consults options.impersonate, and in the Google-regeneration branch it pushes the prefill first and then the user rewrite instruction, yet still returns assistantPrefillInjected: true.

  • Impersonate: final array becomes […history, user:"write as {{user}}", assistant:<character prefill>]. The trailing assistant prefill makes the model extend the character's voice (recency-dominant), contradicting the impersonate instruction; the prefill is also prepended to fullResponse and persisted as the user's message.
  • Google regen: the prefill is mid-array (a user rewrite instruction follows it), so the model never continues from it — but it is still seeded into fullResponse and saved as the regenerated user swipe.

How it breaks the pipeline

Code trace (staging @ b826f8f4):

  • packages/server/src/routes/generate/generate-route-utils.ts:534-555 — appendGenerationTailMessages: early-returns only on followUpIteration !== 0 (line 535); pushes the prefill (line ~547); in the Google branch pushes regenerateUserMessage after the prefill (line ~551); returns assistantPrefillInjected: true regardless of trailing position.
  • Impersonate: packages/server/src/routes/generate.routes.ts:5259 pushes the {role:"user", content: impersonateInstruction} (DEFAULT_IMPERSONATE_PROMPT, packages/shared/src/constants/impersonate.ts) then calls the tail helper (:5262). fullResponse is seeded from the prefill (:5694) and saved with role: input.impersonate ? "user" : "assistant" (~:6402). assistantPrefill is set from preset/params (lines 1975/3275) and never cleared for impersonate.
  • Google regen: fullResponse seeding at :5694 is gated only on assistantPrefillInjected (true here), and the swipe is saved via addSwipe (:6396). The prefill-dedup at ~:6147-6157 only collapses an exact double-echo, not a non-trailing prefill.

Steps to reproduce

Impersonate: With a preset/connection/chat assistantPrefill set (e.g. \n\n{{char}}: *she leans in*) on an Anthropic connection, click Impersonate. The impersonated "user" message comes back in the character's voice / begins with the prefill text, and the saved user message starts with the prefill.

Google regen: On a Google/Gemini connection with a non-empty assistantPrefill, regenerate one of your own user-authored messages as a swipe. The regenerated user swipe text starts with the assistant prefill string.

Suggested fix

In appendGenerationTailMessages, skip the assistant-prefill push when options.impersonate is true (it is already passed in), or clear assistantPrefill on the impersonate branch (generate.routes.ts:5252) before the tail call. For the Google-regeneration case, have the helper report whether the prefill actually ended up trailing, and gate the fullResponse seeding (:5694) on that (e.g. only seed when !googleUserRegenerationInjected && !impersonate) so a non-continued prefill is never seeded or persisted. Matches SillyTavern, which omits the prefill during impersonation and user-message regeneration.

Environment

  • Marinara Engine version: 2.0.0 (staging @ b826f8f4)
  • Install type: source
  • OS + version: All (code-level defect, platform-independent)
  • Browser or app shell: All

Additional context

Found via a systematic multi-agent audit of the prompt-assembly / prefill path. Both cases share one root cause (the tail helper injects/seeds the prefill without accounting for a following user-role message). Related to the prefill trailing-whitespace fix in #2673 / #2674 and the Claude-subscription prefill issue filed separately.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jun 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2677 Originally assigned to: @SpicyMarinara on GitHub. ## Summary The assistant **prefill** is appended/seeded without accounting for a following user-role message, so it is mis-applied in two cases: **(1) impersonation** — the prefill becomes the trailing assistant turn after the "write as {{user}}" instruction, making the model continue in the *character's* voice and leaking the prefill text into the saved user message; **(2) Google user-message regeneration** — the prefill is seeded into the saved swipe even though it is not the trailing turn, corrupting the regenerated user message. SillyTavern does not apply the assistant prefill during impersonation or user-message regeneration. ## Expected behavior - **Impersonate:** the impersonate instruction should be the final turn so the model writes the user's line; the character prefill should not be appended (matching ST). - **Google regen:** when the prefill is not actually the trailing message the model continues from, it should not be prepended to / saved as the regenerated user message. ## Actual behavior `appendGenerationTailMessages` pushes the assistant prefill and only early-returns on follow-up iterations — it never consults `options.impersonate`, and in the Google-regeneration branch it pushes the prefill *first* and then the user rewrite instruction, yet still returns `assistantPrefillInjected: true`. - **Impersonate:** final array becomes `[…history, user:"write as {{user}}", assistant:<character prefill>]`. The trailing assistant prefill makes the model extend the character's voice (recency-dominant), contradicting the impersonate instruction; the prefill is also prepended to `fullResponse` and persisted as the user's message. - **Google regen:** the prefill is mid-array (a user rewrite instruction follows it), so the model never continues from it — but it is still seeded into `fullResponse` and saved as the regenerated user swipe. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`): - `packages/server/src/routes/generate/generate-route-utils.ts:534-555` — `appendGenerationTailMessages`: early-returns only on `followUpIteration !== 0` (line 535); pushes the prefill (line ~547); in the Google branch pushes `regenerateUserMessage` *after* the prefill (line ~551); returns `assistantPrefillInjected: true` regardless of trailing position. - Impersonate: `packages/server/src/routes/generate.routes.ts:5259` pushes the `{role:"user", content: impersonateInstruction}` (`DEFAULT_IMPERSONATE_PROMPT`, `packages/shared/src/constants/impersonate.ts`) then calls the tail helper (~`:5262`). `fullResponse` is seeded from the prefill (~`:5694`) and saved with `role: input.impersonate ? "user" : "assistant"` (~`:6402`). `assistantPrefill` is set from preset/params (lines 1975/3275) and never cleared for impersonate. - Google regen: `fullResponse` seeding at ~`:5694` is gated only on `assistantPrefillInjected` (true here), and the swipe is saved via `addSwipe` (~`:6396`). The prefill-dedup at ~`:6147-6157` only collapses an exact double-echo, not a non-trailing prefill. ## Steps to reproduce **Impersonate:** With a preset/connection/chat `assistantPrefill` set (e.g. `\n\n{{char}}: *she leans in*`) on an Anthropic connection, click **Impersonate**. The impersonated "user" message comes back in the character's voice / begins with the prefill text, and the saved user message starts with the prefill. **Google regen:** On a Google/Gemini connection with a non-empty `assistantPrefill`, regenerate one of your own user-authored messages as a swipe. The regenerated user swipe text starts with the assistant prefill string. ## Suggested fix In `appendGenerationTailMessages`, skip the assistant-prefill push when `options.impersonate` is true (it is already passed in), or clear `assistantPrefill` on the impersonate branch (~`generate.routes.ts:5252`) before the tail call. For the Google-regeneration case, have the helper report whether the prefill actually ended up trailing, and gate the `fullResponse` seeding (~`:5694`) on that (e.g. only seed when `!googleUserRegenerationInjected && !impersonate`) so a non-continued prefill is never seeded or persisted. Matches SillyTavern, which omits the prefill during impersonation and user-message regeneration. ## Environment - Marinara Engine version: 2.0.0 (staging @ `b826f8f4`) - Install type: source - OS + version: All (code-level defect, platform-independent) - Browser or app shell: All ## Additional context Found via a systematic multi-agent audit of the prompt-assembly / prefill path. Both cases share one root cause (the tail helper injects/seeds the prefill without accounting for a following user-role message). Related to the prefill trailing-whitespace fix in #2673 / #2674 and the Claude-subscription prefill issue filed separately. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
Author
Owner

@kolacheee commented on GitHub (Jun 21, 2026):

Veracity self-review — correction / clarification
Automated post-filing review of this issue against the current working tree. The reported bug remains valid; the notes below refine its scope, accuracy, or suggested fix for whoever implements it.

Clarification on the code trace (verified against b826f8f4 and the current fix/anthropic-prefill-trailing-whitespace tree):

  1. The "Actual behavior" wording "it never consults options.impersonate" is imprecise. The helper DOES reference options.impersonate at generate-route-utils.ts:538-539 (shouldAppendGoogleUserRegeneration = !options.impersonate && options.isGoogleProvider && !!options.regenerateUserMessage). The precise defect is that the prefill push itself (generate-route-utils.ts:547) is unconditional and ignores impersonate, and the fullResponse seeding (generate.routes.ts:5694) is gated only on assistantPrefillInjected.

  2. The suggested fix is correct as written and needs no new helper return value — note for fixers that the helper already returns googleUserRegenerationInjected, which is exactly the "prefill is non-trailing" signal, and the issue's own example gate already uses it. Minimal fix: (a) Impersonate — skip the prefill push when options.impersonate is true (or clear assistantPrefill before the tail call at generate.routes.ts:5252); this makes assistantPrefillInjected false and automatically prevents both the 5694 seeding and the 6404 save. (b) Google regen — gate the seeding at generate.routes.ts:5694 on the existing return value, e.g. if (tailMessages.assistantPrefillInjected && !tailMessages.googleUserRegenerationInjected && assistantPrefill). The && !impersonate in the issue's example is harmless but redundant once (a) is in place.

  3. Confirmed unaffected by #2673/#2679: #2673 only added .trimEnd() to the prefill push (generate-route-utils.ts + dry-run-route.ts) and #2679 only changed anthropic.provider.ts; git diff b826f8f4..HEAD shows no prefill changes in generate.routes.ts. The push/seed/save sites are unchanged and the bug is still present.

<!-- gh-comment-id:4761102135 --> @kolacheee commented on GitHub (Jun 21, 2026): > **Veracity self-review — correction / clarification** > _Automated post-filing review of this issue against the current working tree. The reported bug remains valid; the notes below refine its scope, accuracy, or suggested fix for whoever implements it._ Clarification on the code trace (verified against b826f8f4 and the current fix/anthropic-prefill-trailing-whitespace tree): 1. The "Actual behavior" wording "it never consults options.impersonate" is imprecise. The helper DOES reference options.impersonate at generate-route-utils.ts:538-539 (`shouldAppendGoogleUserRegeneration = !options.impersonate && options.isGoogleProvider && !!options.regenerateUserMessage`). The precise defect is that the *prefill push* itself (generate-route-utils.ts:547) is unconditional and ignores impersonate, and the fullResponse seeding (generate.routes.ts:5694) is gated only on assistantPrefillInjected. 2. The suggested fix is correct as written and needs no new helper return value — note for fixers that the helper already returns googleUserRegenerationInjected, which is exactly the "prefill is non-trailing" signal, and the issue's own example gate already uses it. Minimal fix: (a) Impersonate — skip the prefill push when options.impersonate is true (or clear assistantPrefill before the tail call at generate.routes.ts:5252); this makes assistantPrefillInjected false and automatically prevents both the 5694 seeding and the 6404 save. (b) Google regen — gate the seeding at generate.routes.ts:5694 on the existing return value, e.g. `if (tailMessages.assistantPrefillInjected && !tailMessages.googleUserRegenerationInjected && assistantPrefill)`. The `&& !impersonate` in the issue's example is harmless but redundant once (a) is in place. 3. Confirmed unaffected by #2673/#2679: #2673 only added .trimEnd() to the prefill push (generate-route-utils.ts + dry-run-route.ts) and #2679 only changed anthropic.provider.ts; git diff b826f8f4..HEAD shows no prefill changes in generate.routes.ts. The push/seed/save sites are unchanged and the bug is still present.
Author
Owner

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

Addressed on the issue-sweep branch. The tail helper now skips assistant prefill injection during impersonation, dry-run mirrors that behavior, and the stream/save prefill seed only runs when the prefill remains the trailing assistant message, not when Gemini user-message regeneration adds a final user rewrite instruction after it. Final repo checks will run after the sweep.

<!-- gh-comment-id:4761448520 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Addressed on the issue-sweep branch. The tail helper now skips assistant prefill injection during impersonation, dry-run mirrors that behavior, and the stream/save prefill seed only runs when the prefill remains the trailing assistant message, not when Gemini user-message regeneration adds a final user rewrite instruction after it. Final repo checks will run after the sweep.
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#1325
No description provided.