[GH-ISSUE #1978] Prioritize latest user-message lorebook matches during budget selection #970

Closed
opened 2026-10-21 05:24:15 +07:00 by bsakaguchi · 0 comments
Owner

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

Originally assigned to: @cha1latte on GitHub.

Problem

Target 7 lorebooks parityscan found a budget-selection regression for lorebook entries that match the current user turn.

Legacy marked entries whose primary key matched the latest user message and selected those ahead of older-context matches under lorebook token budget. Refactor no longer carries that match metadata and sorts budget candidates only by constant status, then entry order.

Refactor evidence

  • src/engine/generation-core/lorebooks/keyword-scanner.ts:33-43 defines ActivatedEntry without latest-user match metadata.
  • src/engine/generation-core/lorebooks/prompt-injector.ts:162-166 sorts budget candidates by constant status, then entry.order.
  • No current runtime scanner path sets a matchedLatestUserMessage equivalent before budget selection.

Legacy behavior

  • packages/server/src/services/lorebook/keyword-scanner.ts:45-46 stored matchedLatestUserMessage on activated entries.
  • packages/server/src/services/lorebook/keyword-scanner.ts:412-478 computed the flag from the latest user message.
  • packages/server/src/services/lorebook/index.ts:319-324 sorted latest-user matches ahead of older-context matches during budget selection.

Impact

When the lorebook budget is tight, an entry matched only by older chat context can consume budget before an entry that directly matches the newest user message. This can make current-turn lore fail to inject even though legacy prioritized it.

Expected behavior

Budget selection should preserve the legacy priority: constants first, latest-user-message primary-key matches next, then entry insertion order.

Suggested fix

Reintroduce latest-user match metadata in the scanner and include it in applyTokenBudgetWithSkipped ordering before entry.order. Add focused local proof with two activated non-constant entries where only the later-order entry matches the latest user message and budget allows one entry.

Parent: #1904 target 7 lorebooks parityscan.

Originally created by @Xelvanis on GitHub (Jun 2, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1978 Originally assigned to: @cha1latte on GitHub. ## Problem Target 7 lorebooks parityscan found a budget-selection regression for lorebook entries that match the current user turn. Legacy marked entries whose primary key matched the latest user message and selected those ahead of older-context matches under lorebook token budget. Refactor no longer carries that match metadata and sorts budget candidates only by constant status, then entry order. ## Refactor evidence - `src/engine/generation-core/lorebooks/keyword-scanner.ts:33-43` defines `ActivatedEntry` without latest-user match metadata. - `src/engine/generation-core/lorebooks/prompt-injector.ts:162-166` sorts budget candidates by constant status, then `entry.order`. - No current runtime scanner path sets a `matchedLatestUserMessage` equivalent before budget selection. ## Legacy behavior - `packages/server/src/services/lorebook/keyword-scanner.ts:45-46` stored `matchedLatestUserMessage` on activated entries. - `packages/server/src/services/lorebook/keyword-scanner.ts:412-478` computed the flag from the latest user message. - `packages/server/src/services/lorebook/index.ts:319-324` sorted latest-user matches ahead of older-context matches during budget selection. ## Impact When the lorebook budget is tight, an entry matched only by older chat context can consume budget before an entry that directly matches the newest user message. This can make current-turn lore fail to inject even though legacy prioritized it. ## Expected behavior Budget selection should preserve the legacy priority: constants first, latest-user-message primary-key matches next, then entry insertion order. ## Suggested fix Reintroduce latest-user match metadata in the scanner and include it in `applyTokenBudgetWithSkipped` ordering before `entry.order`. Add focused local proof with two activated non-constant entries where only the later-order entry matches the latest user message and budget allows one entry. Parent: #1904 target 7 lorebooks parityscan.
bsakaguchi 2026-10-21 05:24:15 +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#970
No description provided.