[GH-ISSUE #3024] [Issue]: Chat-history marker assigned to a group is collapsed into one block, destroying message boundaries and mis-anchoring all depth/post-history injection #1595

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
If the Chat History marker section is assigned to a group in the preset editor (the UI permits it), the assembler routes it through buildGroupMessages, which collapses the entire chat history into one block (or one block per role-run) and overwrites each message's contextKind from "history" to "prompt". The per-message structure and the history anchor are lost, so depth-injected sections, depth lorebook entries, character depth prompts, and post-history instructions get placed relative to the end of the WHOLE prompt instead of the end of chat history.

How to reproduce (try it yourself)

  1. In the preset editor, create a group.
  2. Set the Chat History marker section's Group dropdown to that group (the Group selector is rendered for all sections, including markers, with no guard).
  3. Run a generation with a character that has post-history instructions and/or a preset section using depth injection.

You'll see: chat history collapsed into a single block (or per-role blocks) wrapped in the group tag, contextKind "history" gone, and depth/post-history blocks landing after the whole prompt instead of after the last chat message.
Expected: chat history preserved as discrete messages and depth/post-history blocks anchored to the end of chat history.

Where it is
packages/server/src/services/prompt/assembler.ts — group routing at line 398 (if (section.groupId)) is checked BEFORE the isChatHistory branch at line 424 (inside the else), and the inner member-collection loop at line 405 (if (next.groupId === section.groupId)) also absorbs chat-history sections. buildGroupMessages (692-738) flattens and re-tags as "prompt" (lines 705/720). findHistoryBounds (750-760) then returns null, and Phase-5 inject (499-507) calls injectAtDepth with no bounds, defaulting the anchor to the end of the whole prompt (packages/server/src/services/lorebook/prompt-injector.ts:95-96). UI reachability: packages/client/src/components/presets/PresetEditor.tsx:1575-1601 (Group selector, no marker guard).

What's happening
The group-routing condition takes precedence over the history-preserving branch, and buildGroupMessages has no chat-history awareness, so a grouped chat-history section is flattened and its contextKind: "history" markers are overwritten — which removes the anchor findHistoryBounds needs.

Suggested fix
Exclude chat-history sections from group collection in BOTH the outer head check and the inner member-collection loop in assembler.ts:

  1. Line 398: change if (section.groupId) { to if (section.groupId && !section.isChatHistory) {.
  2. Line 405: change if (next.groupId === section.groupId) { to if (next.groupId === section.groupId && !next.isChatHistory) {.
    With both guards, a chat-history section is never routed to or absorbed by buildGroupMessages; it falls into the existing else/isChatHistory path (lines 424-426) and emits section.messages verbatim, preserving each message's contextKind: "history" so findHistoryBounds finds the anchor and depth injection is placed correctly.

Note on the obvious one-guard version: guarding only the outer head check (line 398) is INSUFFICIENT — if any non-chat-history section sharing the same groupId is ordered before the chat-history section, that earlier section becomes the group head and the inner loop still pulls the chat-history section in. Both guards are required. Optionally also hide the Group selector for the chat_history marker in PresetEditor.tsx as defense-in-depth, but the two-line server guard is the load-bearing fix.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3024 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** If the Chat History marker section is assigned to a group in the preset editor (the UI permits it), the assembler routes it through `buildGroupMessages`, which collapses the entire chat history into one block (or one block per role-run) and overwrites each message's `contextKind` from `"history"` to `"prompt"`. The per-message structure and the history anchor are lost, so depth-injected sections, depth lorebook entries, character depth prompts, and post-history instructions get placed relative to the end of the WHOLE prompt instead of the end of chat history. **How to reproduce (try it yourself)** 1. In the preset editor, create a group. 2. Set the Chat History marker section's Group dropdown to that group (the Group selector is rendered for all sections, including markers, with no guard). 3. Run a generation with a character that has post-history instructions and/or a preset section using depth injection. You'll see: chat history collapsed into a single block (or per-role blocks) wrapped in the group tag, `contextKind "history"` gone, and depth/post-history blocks landing after the whole prompt instead of after the last chat message. Expected: chat history preserved as discrete messages and depth/post-history blocks anchored to the end of chat history. **Where it is** `packages/server/src/services/prompt/assembler.ts` — group routing at line 398 (`if (section.groupId)`) is checked BEFORE the `isChatHistory` branch at line 424 (inside the `else`), and the inner member-collection loop at line 405 (`if (next.groupId === section.groupId)`) also absorbs chat-history sections. `buildGroupMessages` (692-738) flattens and re-tags as `"prompt"` (lines 705/720). `findHistoryBounds` (750-760) then returns null, and Phase-5 inject (499-507) calls `injectAtDepth` with no bounds, defaulting the anchor to the end of the whole prompt (`packages/server/src/services/lorebook/prompt-injector.ts:95-96`). UI reachability: `packages/client/src/components/presets/PresetEditor.tsx:1575-1601` (Group selector, no marker guard). **What's happening** The group-routing condition takes precedence over the history-preserving branch, and `buildGroupMessages` has no chat-history awareness, so a grouped chat-history section is flattened and its `contextKind: "history"` markers are overwritten — which removes the anchor `findHistoryBounds` needs. **Suggested fix** Exclude chat-history sections from group collection in BOTH the outer head check and the inner member-collection loop in `assembler.ts`: 1. Line 398: change `if (section.groupId) {` to `if (section.groupId && !section.isChatHistory) {`. 2. Line 405: change `if (next.groupId === section.groupId) {` to `if (next.groupId === section.groupId && !next.isChatHistory) {`. With both guards, a chat-history section is never routed to or absorbed by `buildGroupMessages`; it falls into the existing `else`/`isChatHistory` path (lines 424-426) and emits `section.messages` verbatim, preserving each message's `contextKind: "history"` so `findHistoryBounds` finds the anchor and depth injection is placed correctly. Note on the obvious one-guard version: guarding only the outer head check (line 398) is INSUFFICIENT — if any non-chat-history section sharing the same groupId is ordered before the chat-history section, that earlier section becomes the group head and the inner loop still pulls the chat-history section in. Both guards are required. Optionally also hide the Group selector for the chat_history marker in `PresetEditor.tsx` as defense-in-depth, but the two-line server guard is the load-bearing fix. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

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

Fixed on staging in PR #3031 (merged as f542e282). Closing manually because GitHub auto-close keywords only close issues when merged into the default branch (main), not staging.

<!-- gh-comment-id:4834696517 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed on staging in PR #3031 (merged as f542e282). Closing manually because GitHub auto-close keywords only close issues when merged into the default branch (main), not 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#1595
No description provided.