[GH-ISSUE #2691] [Issue]: At-depth lorebook entries (role=system) are hoisted to the front by strictRoleFormatting; embedded-book round-trip flattens at-depth placement #1335

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

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

Lorebook entries configured for at-depth injection (position = at-depth, depth = N, role = system) are moved out of their computed depth slot to the front of the prompt — so an at-depth note meant to sit N messages from the end of history is collapsed into the leading system block. Because role defaults to system, this is the common case, not an edge case. A separate embedded-character-book round-trip path also flattens at-depth placement to before_char.

Expected behavior

An at-depth lorebook entry (position=2, depth=N, role=system) should land as an in-chat message N messages from the history end (SillyTavern @D parity), not as a global system prompt at the top.

Actual behavior

  1. enforceStrictRoles hoists at-depth system entries. Assembler Phase 5 injects lorebook depth entries via injectAtDepth, preserving role and contextKind:'injection'. Phase 6 (enforceStrictRoles, run when strictRoleFormatting is true — the default) unconditionally takes every role==='system' message after the leading block and merges/unshifts it to the front. There is no guard for contextKind==='injection', so a depth-injected system entry is yanked to the top. Since role defaults to system, most at-depth entries are affected.
  2. Embedded-book round-trip flattens position. Syncing an embedded character book (round-trip) flattens any position>=2 (at-depth/AN/EM) entry to before_char, silently moving @D/AN/EM content to the top of context.

How it breaks the pipeline

Code trace (staging @ b826f8f4):

  • (1) Depth injection: packages/server/src/services/prompt/assembler.ts:429-465 (injectAtDepth, role preserved, contextKind:'injection'). Hoist: assembler.ts:749-760 (enforceStrictRoles merges/unshifts every trailing role==='system' with no contextKind guard). strictRoleFormatting defaults true (shared/constants/defaults.ts:38, shipped default-preset.json). role defaults system (lorebook.schema.ts:182).
  • (2) Embedded round-trip flatten in packages/server/src/services/lorebook/character-book-sync.ts (position>=2 → before_char on sync).

Steps to reproduce

  1. Create a lorebook entry: position=at-depth, depth=2, role=system (defaults), with a keyword present in the last message; default preset (strictRoleFormatting=true), a world_info marker, and >2 history messages.
  2. Inspect the assembled array (real send path, not dry-run): the entry is merged into the leading system block at the top instead of appearing 2 messages from the end of history.
  3. For (2): attach the entry to a character as an embedded book, save/round-trip, and observe its position flatten to before_char.

Suggested fix

  • (1) In enforceStrictRoles (assembler.ts:749-760), skip hoisting for depth-injected content: when msg.contextKind === 'injection' (or a dedicated isDepthInjected flag), leave it in place — or run enforceStrictRoles before Phase 5 depth injection. Mirror ST, which keeps @D system entries at their depth.
  • (2) Preserve position (and depth/role) through the embedded-character-book round-trip in character-book-sync.ts instead of flattening position>=2 to before_char.

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 conditional prompt insertion. The lorebook-specific sibling of the post-history hoist (#2676), but it affects native and imported at-depth entries at the default role=system. Related to the ST position-import mismap 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 21, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2691 Originally assigned to: @SpicyMarinara on GitHub. ## Summary Lorebook entries configured for **at-depth** injection (`position = at-depth`, `depth = N`, `role = system`) are moved out of their computed depth slot to the **front** of the prompt — so an at-depth note meant to sit N messages from the end of history is collapsed into the leading system block. Because `role` **defaults to `system`**, this is the common case, not an edge case. A separate embedded-character-book round-trip path also flattens at-depth placement to `before_char`. ## Expected behavior An at-depth lorebook entry (`position=2`, `depth=N`, `role=system`) should land as an in-chat message N messages from the history end (SillyTavern `@D` parity), not as a global system prompt at the top. ## Actual behavior 1. **`enforceStrictRoles` hoists at-depth system entries.** Assembler Phase 5 injects lorebook depth entries via `injectAtDepth`, preserving role and `contextKind:'injection'`. Phase 6 (`enforceStrictRoles`, run when `strictRoleFormatting` is true — the default) unconditionally takes every `role==='system'` message after the leading block and merges/unshifts it to the front. There is **no guard for `contextKind==='injection'`**, so a depth-injected system entry is yanked to the top. Since `role` defaults to `system`, most at-depth entries are affected. 2. **Embedded-book round-trip flattens position.** Syncing an embedded character book (round-trip) flattens any `position>=2` (at-depth/AN/EM) entry to `before_char`, silently moving `@D`/AN/EM content to the top of context. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`): - (1) Depth injection: `packages/server/src/services/prompt/assembler.ts:429-465` (`injectAtDepth`, role preserved, `contextKind:'injection'`). Hoist: `assembler.ts:749-760` (`enforceStrictRoles` merges/`unshift`s every trailing `role==='system'` with no `contextKind` guard). `strictRoleFormatting` defaults true (`shared/constants/defaults.ts:38`, shipped `default-preset.json`). `role` defaults `system` (`lorebook.schema.ts:182`). - (2) Embedded round-trip flatten in `packages/server/src/services/lorebook/character-book-sync.ts` (position>=2 → before_char on sync). ## Steps to reproduce 1. Create a lorebook entry: `position=at-depth`, `depth=2`, `role=system` (defaults), with a keyword present in the last message; default preset (`strictRoleFormatting=true`), a `world_info` marker, and >2 history messages. 2. Inspect the assembled array (real send path, not dry-run): the entry is merged into the **leading system block at the top** instead of appearing 2 messages from the end of history. 3. For (2): attach the entry to a character as an embedded book, save/round-trip, and observe its `position` flatten to `before_char`. ## Suggested fix - (1) In `enforceStrictRoles` (`assembler.ts:749-760`), skip hoisting for depth-injected content: when `msg.contextKind === 'injection'` (or a dedicated `isDepthInjected` flag), leave it in place — or run `enforceStrictRoles` *before* Phase 5 depth injection. Mirror ST, which keeps `@D` system entries at their depth. - (2) Preserve `position` (and depth/role) through the embedded-character-book round-trip in `character-book-sync.ts` instead of flattening `position>=2` to `before_char`. ## 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 conditional prompt insertion. The lorebook-specific sibling of the post-history hoist (#2676), but it affects native and imported at-depth entries at the **default** `role=system`. Related to the ST position-import mismap 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

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

Addressed in branch codex/spicy-issue-sweep-2026-06-21.

Verification/fix notes:

  • The strict-role hoist part was already no longer present in current code: enforceStrictRoles no longer unshifts later system messages into the leading system block, so depth-injected system messages are not collapsed to the front there.
  • The still-valid embedded character-book sync flattening is fixed. Character-book entries can now retain numeric ST positions, depth, and role; syncing a standalone linked lorebook writes internal at-depth entries back as ST position: 4 with depth/role preserved.
  • ST character-book normalization now keeps valid numeric ST positions instead of flattening them to before/after.

Final repo checks will run after the full issue sweep.

<!-- gh-comment-id:4761524654 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Addressed in branch `codex/spicy-issue-sweep-2026-06-21`. Verification/fix notes: - The strict-role hoist part was already no longer present in current code: `enforceStrictRoles` no longer unshifts later system messages into the leading system block, so depth-injected system messages are not collapsed to the front there. - The still-valid embedded character-book sync flattening is fixed. Character-book entries can now retain numeric ST positions, depth, and role; syncing a standalone linked lorebook writes internal at-depth entries back as ST `position: 4` with depth/role preserved. - ST character-book normalization now keeps valid numeric ST positions instead of flattening them to before/after. Final repo checks will run after the full issue 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#1335
No description provided.