[PR #1063] [MERGED] Fix lorebook latest-message priority #4910

Closed
opened 2026-10-22 04:11:38 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/1063
Author: @cha1latte
Created: 5/21/2026
Status: ✅ Merged
Merged: 5/21/2026
Merged by: @cha1latte

Base: staging ← Head: fix/issue-1060-lorebook-priority


📝 Commits (1)

  • dbb6adc Fix lorebook latest-message priority

📊 Changes

3 files changed (+19 additions, -1 deletions)

View changed files

➕ docs/pr-evidence/issue-1060-lorebook-proof.png (+0 -0)
📝 packages/server/src/services/lorebook/index.ts (+12 -1)
📝 packages/server/src/services/lorebook/keyword-scanner.ts (+7 -0)

📄 Description

Linked issue

Closes #1060

Why this change

  • Latest user-message lorebook keyword matches could lose the token budget to older/history-only matches with lower injection order, causing a large relevant entry to be skipped even when the user typed its key directly.
  • The skipped-entry panel expected full budget diagnostics, but the scan result only returned id and matchedKeys, which could leave the panel unable to render skipped rows.

What changed

  • Marks activated lorebook entries when their primary key matched the latest user message directly.
  • Prioritizes latest-user-message matches during lorebook budget selection while preserving final prompt injection order.
  • Returns complete skipped-entry budget diagnostics so the active world info panel has names, token estimates, budget caps, and skip reasons.

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

  • Codex scratch repro before the fix reproduced the budget bug: the selected entries were small-a and small-b, while the latest user-message match large-latest was skipped by chat budget.
  • Codex scratch repro after the fix passed: large-latest is selected, older small entries are skipped with complete diagnostics, constant entries retain first priority, and latest-message ties keep injection order.
  • Command: pnpm --filter @marinara-engine/server exec tsx ..\..\scratch\issue-1060-lorebook-priority-repro.ts
  • Baseline: pnpm check passed locally.
  • Bunny review: passed for the current diff before opening this PR.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

Issue 1060 verification output

Summary by CodeRabbit

  • New Features
    • Lorebook diagnostics now include detailed metadata for skipped entries, including identifiers, token estimates, budget usage, and skip reasons.
    • Improved lorebook selection logic to prioritize entries matching the latest user message.

Review Change Stack


🔄 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/1063 **Author:** [@cha1latte](https://github.com/cha1latte) **Created:** 5/21/2026 **Status:** ✅ Merged **Merged:** 5/21/2026 **Merged by:** [@cha1latte](https://github.com/cha1latte) **Base:** `staging` ← **Head:** `fix/issue-1060-lorebook-priority` --- ### 📝 Commits (1) - [`dbb6adc`](https://github.com/Pasta-Devs/Marinara-Engine/commit/dbb6adc6836e3d44f6e8b3fc3e82faa42dba56c8) Fix lorebook latest-message priority ### 📊 Changes **3 files changed** (+19 additions, -1 deletions) <details> <summary>View changed files</summary> ➕ `docs/pr-evidence/issue-1060-lorebook-proof.png` (+0 -0) 📝 `packages/server/src/services/lorebook/index.ts` (+12 -1) 📝 `packages/server/src/services/lorebook/keyword-scanner.ts` (+7 -0) </details> ### 📄 Description <!-- Target branch: `staging`. The default base in this UI is `main` (release branch). --> <!-- Change the base to `staging` before submitting unless this is a maintainer-approved mainline change. --> <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue <!-- Every PR should reference a feature request or issue report. If there isn't one yet, open one first to gather opinions: --> <!-- https://github.com/Pasta-Devs/Marinara-Engine/issues/new/choose --> Closes #1060 ## Why this change <!-- What user problem, bug, or goal does this solve? --> - Latest user-message lorebook keyword matches could lose the token budget to older/history-only matches with lower injection order, causing a large relevant entry to be skipped even when the user typed its key directly. - The skipped-entry panel expected full budget diagnostics, but the scan result only returned `id` and `matchedKeys`, which could leave the panel unable to render skipped rows. ## What changed <!-- List the key changes in this PR --> - Marks activated lorebook entries when their primary key matched the latest user message directly. - Prioritizes latest-user-message matches during lorebook budget selection while preserving final prompt injection order. - Returns complete skipped-entry budget diagnostics so the active world info panel has names, token estimates, budget caps, and skip reasons. ## Validation <!-- Required: include commands run and/or manual checks performed --> - [ ] `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 <!-- Describe exactly what you tested in a real browser/app, step by step. --> <!-- ⚠️ If an AI agent filled out or ticked these boxes for you, treat them --> <!-- as a TO-DO LIST, not as proof the work is done. Verify each item yourself --> <!-- before submitting. If you haven't verified it, untick the box. --> - Codex scratch repro before the fix reproduced the budget bug: the selected entries were `small-a` and `small-b`, while the latest user-message match `large-latest` was skipped by chat budget. - Codex scratch repro after the fix passed: `large-latest` is selected, older small entries are skipped with complete diagnostics, constant entries retain first priority, and latest-message ties keep injection order. - Command: `pnpm --filter @marinara-engine/server exec tsx ..\..\scratch\issue-1060-lorebook-priority-repro.ts` - Baseline: `pnpm check` passed locally. - Bunny review: passed for the current diff before opening this PR. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) <!-- Add before/after screenshots or recordings for UI changes --> ![Issue 1060 verification output](https://raw.githubusercontent.com/cha1latte/Marinara-Engine/dbb6adc6836e3d44f6e8b3fc3e82faa42dba56c8/docs/pr-evidence/issue-1060-lorebook-proof.png) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Lorebook diagnostics now include detailed metadata for skipped entries, including identifiers, token estimates, budget usage, and skip reasons. * Improved lorebook selection logic to prioritize entries matching the latest user message. <!-- review_stack_entry_start --> [![Review Change Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/Pasta-Devs/Marinara-Engine/pull/1063?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- 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-22 04:11:38 +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#4910
No description provided.