[PR #5760] [MERGED] fix(prompt): insert each lorebook position at most once across lorebook markers #6920

Closed
opened 2026-10-23 04:14:53 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5760
Author: @Romuromylus
Created: 9/2/2026
Status: ✅ Merged
Merged: 9/2/2026
Merged by: @Romuromylus

Base: staging ← Head: fix/5716-lorebook-marker-duplication


📝 Commits (4)

  • 49c79b2 fix(prompt): insert each lorebook position at most once across lorebook markers
  • c411ab8 docs(prompt): keep MarkerContext JSDoc attached and spell out lorebook marker claim order
  • d958ee0 Merge branch 'staging' into fix/5716-lorebook-marker-duplication
  • 5420b07 docs(prompt): document expandLorebook

📊 Changes

3 files changed (+89 additions, -4 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/services/prompt/marker-expander.ts (+31 -4)
📝 scripts/regressions/prompt.regression.ts (+57 -0)

📄 Description

Linked issue

Closes #5716

Why this change

A lorebook entry set to insert Before Char was showing up under both the "Before Char" and "After Char" placeholders of the preset in the chat's prompt inspection. That happens when a preset carries more than one lorebook marker covering the same world-info position: every Lorebook Marker (All) section expands to the full before + after content, so two of them (or an "All" marker sitting after a "Before" marker) repeat the same entries in the sent prompt. The dedicated Before / After markers were already fine on their own; the duplication comes from the combined marker not knowing that another marker already placed those entries.

Reproduced on my private deploy against current staging with a constant position-0 entry: a preset with Lorebook Marker (Before) + Lorebook Marker (After) inserts the entry once, while a preset with two Lorebook Marker (All) sections named "Before Char" / "After Char" inserts it twice.

What changed

  • packages/server/src/services/prompt/marker-expander.ts: expandLorebook now records which world-info positions (before / after) a lorebook marker has already placed on the shared MarkerContext for the current prompt build (lorebookPositionsEmitted). The first marker that covers a position owns it; a later marker covering the same position expands without it. Dedicated Before + After markers keep their own position each, and the set lives on the per-build context, so preview and generation each start clean. Same once-guard shape ensureLorebookScan already uses for depth entries.
  • scripts/regressions/prompt.regression.ts: new case "lorebook markers insert each world-info position at most once per prompt build" covering two "All" markers, Before-then-All, the typed Before + After pair (including a repeated Before), and a fresh build. Fails on the previous expander, passes with this change.
  • CHANGELOG.md: Unreleased › Fixed entry.

Out of scope: the SillyTavern preset importer still consolidates worldInfoBefore + worldInfoAfter into a single combined marker by design; this PR only stops duplicates when a preset ends up with several lorebook markers.

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

  • node ./scripts/run-regressions.mjs --filter scripts/regressions/prompt.regression.ts passes with the fix; the new case fails against the previous marker-expander.ts (stash-verified).
  • tsc --noEmit clean for packages/server; Prettier clean on the touched files.
  • Live check on a build of this branch deployed to my private instance (same two presets as the repro above, constant position-0 entry, roleplay chat, prompt inspection): the two-"All" preset went from 2 copies of the entry to 1, placed under the first placeholder ("Before Char", ahead of <character_info>), and the typed Before/After pair stayed at 1 copy in the same spot. Manually re-verify in the browser prompt inspection.
  • Manually verify a Game Mode and a Conversation Mode chat using a preset with a single Lorebook Marker (All) still receive their lorebook entries once.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

Not a UI change. Evidence is the Peek Prompt modal on the deployed branch for the two-"All" preset (sections named Intro / Before Char / Character Info / After Char / Chat History): the assembled prompt now lists INTRO, BEFORE CHAR (containing the single lorebook entry), CHARACTER INFO, SYSTEM, LAST MESSAGE - the "After Char" placeholder expands to nothing and is dropped, where before the fix it repeated the entry. The dedicated Before/After preset renders identically.

Summary by CodeRabbit

  • Bug Fixes

    • Prevented lorebook entries from appearing multiple times when prompts contain multiple lorebook markers.
    • Ensured “Before” and “After” entries are each inserted only once while preserving their intended placement.
  • Tests

    • Added regression coverage for repeated, combined, and dedicated lorebook markers.

🔄 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/5760 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 9/2/2026 **Status:** ✅ Merged **Merged:** 9/2/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `staging` ← **Head:** `fix/5716-lorebook-marker-duplication` --- ### 📝 Commits (4) - [`49c79b2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/49c79b204858785a8358b630215d2d818295fff8) fix(prompt): insert each lorebook position at most once across lorebook markers - [`c411ab8`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c411ab8053e137aedc4c3cb5a5b9b3ecdd3ac1a9) docs(prompt): keep MarkerContext JSDoc attached and spell out lorebook marker claim order - [`d958ee0`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d958ee0306de9ee97f9f5cd0b687b4c08a83ffd2) Merge branch 'staging' into fix/5716-lorebook-marker-duplication - [`5420b07`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5420b078a5284c59da91414a1e753f3e4191adcc) docs(prompt): document expandLorebook ### 📊 Changes **3 files changed** (+89 additions, -4 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/services/prompt/marker-expander.ts` (+31 -4) 📝 `scripts/regressions/prompt.regression.ts` (+57 -0) </details> ### 📄 Description ## Linked issue Closes #5716 ## Why this change A lorebook entry set to insert Before Char was showing up under both the "Before Char" and "After Char" placeholders of the preset in the chat's prompt inspection. That happens when a preset carries more than one lorebook marker covering the same world-info position: every `Lorebook Marker (All)` section expands to the full before + after content, so two of them (or an "All" marker sitting after a "Before" marker) repeat the same entries in the sent prompt. The dedicated Before / After markers were already fine on their own; the duplication comes from the combined marker not knowing that another marker already placed those entries. Reproduced on my private deploy against current `staging` with a constant position-0 entry: a preset with `Lorebook Marker (Before)` + `Lorebook Marker (After)` inserts the entry once, while a preset with two `Lorebook Marker (All)` sections named "Before Char" / "After Char" inserts it twice. ## What changed - `packages/server/src/services/prompt/marker-expander.ts`: `expandLorebook` now records which world-info positions (`before` / `after`) a lorebook marker has already placed on the shared `MarkerContext` for the current prompt build (`lorebookPositionsEmitted`). The first marker that covers a position owns it; a later marker covering the same position expands without it. Dedicated Before + After markers keep their own position each, and the set lives on the per-build context, so preview and generation each start clean. Same once-guard shape `ensureLorebookScan` already uses for depth entries. - `scripts/regressions/prompt.regression.ts`: new case "lorebook markers insert each world-info position at most once per prompt build" covering two "All" markers, Before-then-All, the typed Before + After pair (including a repeated Before), and a fresh build. Fails on the previous expander, passes with this change. - `CHANGELOG.md`: Unreleased › Fixed entry. Out of scope: the SillyTavern preset importer still consolidates `worldInfoBefore` + `worldInfoAfter` into a single combined marker by design; this PR only stops duplicates when a preset ends up with several lorebook markers. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes - `node ./scripts/run-regressions.mjs --filter scripts/regressions/prompt.regression.ts` passes with the fix; the new case fails against the previous `marker-expander.ts` (stash-verified). - `tsc --noEmit` clean for `packages/server`; Prettier clean on the touched files. - Live check on a build of this branch deployed to my private instance (same two presets as the repro above, constant position-0 entry, roleplay chat, prompt inspection): the two-"All" preset went from 2 copies of the entry to 1, placed under the first placeholder ("Before Char", ahead of `<character_info>`), and the typed Before/After pair stayed at 1 copy in the same spot. Manually re-verify in the browser prompt inspection. - Manually verify a Game Mode and a Conversation Mode chat using a preset with a single `Lorebook Marker (All)` still receive their lorebook entries once. ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) Not a UI change. Evidence is the Peek Prompt modal on the deployed branch for the two-"All" preset (sections named Intro / Before Char / Character Info / After Char / Chat History): the assembled prompt now lists INTRO, BEFORE CHAR (containing the single lorebook entry), CHARACTER INFO, SYSTEM, LAST MESSAGE - the "After Char" placeholder expands to nothing and is dropped, where before the fix it repeated the entry. The dedicated Before/After preset renders identically. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Prevented lorebook entries from appearing multiple times when prompts contain multiple lorebook markers. * Ensured “Before” and “After” entries are each inserted only once while preserving their intended placement. * **Tests** * Added regression coverage for repeated, combined, and dedicated lorebook markers. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:14:53 +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#6920
No description provided.