[GH-ISSUE #5943] Constant forced lorebook entries escape the location budget via the scan-then-merge seam, and the skip records overstate drops #3286

Closed
opened 2026-10-21 06:01:41 +07:00 by bsakaguchi · 3 comments
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

Found while verifying the #5942 branch; the defect predates it and affects existing callers.

In processLorebooks (packages/server/src/services/lorebook/index.ts, ~:1199-1200 on staging 0d66e67a4), forced entries go through applyCurrentLocationLoreBudget — but the ordinary scan runs right after over allEntries, and a constant entry activates there with zero messages (keyword-scanner.ts:~562). mergeActivatedEntries then re-admits the very entry the location budget just dropped, while the skip record for it is still emitted unconditionally.

Consequences for the existing forced-entry callers (/setup's block and the spatial-projection caller, both on the 2,048-token default budget):

  • The location budget is not actually a ceiling for constant forced entries — they escape it through the scan-then-merge seam.
  • Any diagnostics derived from the skip records overstate what was dropped: an entry can be reported as set aside and be present in the prompt.

The #5942 branch closes this for its own route (the new forcedEntriesOnly mode skips the ordinary scan outright), but the shared shape stays as-is for the older callers — fixing it for them changes long-standing prompt-assembly behavior and deserves its own decision rather than riding along.

Originally created by @kolacheee on GitHub (Sep 8, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5943 Originally assigned to: @SpicyMarinara on GitHub. Found while verifying the #5942 branch; the defect predates it and affects existing callers. In `processLorebooks` (packages/server/src/services/lorebook/index.ts, ~:1199-1200 on staging `0d66e67a4`), forced entries go through `applyCurrentLocationLoreBudget` — but the ordinary scan runs right after over `allEntries`, and a **constant** entry activates there with zero messages (keyword-scanner.ts:~562). `mergeActivatedEntries` then re-admits the very entry the location budget just dropped, while the skip record for it is still emitted unconditionally. Consequences for the existing forced-entry callers (`/setup`'s block and the spatial-projection caller, both on the 2,048-token default budget): - The location budget is not actually a ceiling for constant forced entries — they escape it through the scan-then-merge seam. - Any diagnostics derived from the skip records overstate what was dropped: an entry can be reported as set aside *and* be present in the prompt. The #5942 branch closes this for its own route (the new `forcedEntriesOnly` mode skips the ordinary scan outright), but the shared shape stays as-is for the older callers — fixing it for them changes long-standing prompt-assembly behavior and deserves its own decision rather than riding along.
bsakaguchi 2026-10-21 06:01:41 +07:00
  • closed this issue
  • added the
    in-pr
    label
Author
Owner

@kolacheee commented on GitHub (Sep 8, 2026):

Additional measured instance of the same shape, found while closing the #5942 branch — folding it here so the issue carries the full map:

resolveBudgetAndRecursivelyActivateLorebookEntriesWithDiagnostics (index.ts:907-910) opens with the same scan-then-merge — frontier = mergeActivatedEntries(scanForActivatedEntries(messages, entries, scanOptions), initialActivatedEntries) — one level below the site described above. So when any participating book has recursiveScanning on, anyRecursive is legitimately true and the location-budget drops are re-admitted there, even after the top-level scan is suppressed.

Measured live through the experience-generation route on the #5942 branch (10 forced constants × 400 tokens against a 3,000-token wall, book budget 8,000, picked book with recursiveScanning: true): present [0..9], includedEntries 10, skipped ["vault 7","vault 8","vault 9"] — 13 accounted for out of a 10-id selection; the non-recursive control gives the correct present [0..6], included 7, skipped 3.

The branch documents this door in its route comment rather than patching it, because the only local gate (forcing anyRecursive off under the exact-selection flag) would leave the book-filter suppression with no observable and the shape genuinely belongs to this issue: it hits /setup and the spatial-projection caller identically today.

<!-- gh-comment-id:5578043453 --> @kolacheee commented on GitHub (Sep 8, 2026): Additional measured instance of the same shape, found while closing the #5942 branch — folding it here so the issue carries the full map: `resolveBudgetAndRecursivelyActivateLorebookEntriesWithDiagnostics` (index.ts:907-910) opens with the same scan-then-merge — `frontier = mergeActivatedEntries(scanForActivatedEntries(messages, entries, scanOptions), initialActivatedEntries)` — one level below the site described above. So when any participating book has `recursiveScanning` on, `anyRecursive` is legitimately true and the location-budget drops are re-admitted **there**, even after the top-level scan is suppressed. Measured live through the experience-generation route on the #5942 branch (10 forced constants × 400 tokens against a 3,000-token wall, book budget 8,000, picked book with `recursiveScanning: true`): `present [0..9], includedEntries 10, skipped ["vault 7","vault 8","vault 9"]` — 13 accounted for out of a 10-id selection; the non-recursive control gives the correct `present [0..6], included 7, skipped 3`. The branch documents this door in its route comment rather than patching it, because the only local gate (forcing `anyRecursive` off under the exact-selection flag) would leave the book-filter suppression with no observable and the shape genuinely belongs to this issue: it hits `/setup` and the spatial-projection caller identically today.
Author
Owner

@SpicyMarinara commented on GitHub (Sep 8, 2026):

The new Experience exact-selection path is now guarded in #5944 (6b611eb88): forcedEntriesOnly skips recursive activation, because explicitly chosen entries have already passed the selection budget. A red/green route regression covers a selected recursive book as well as a non-recursive one and requires included/skipped totals to reconcile. This does not change legacy /setup or spatial caller behavior, so this issue remains open for those shared scan-then-merge paths.

<!-- gh-comment-id:5582437261 --> @SpicyMarinara commented on GitHub (Sep 8, 2026): The new Experience exact-selection path is now guarded in #5944 (6b611eb88): forcedEntriesOnly skips recursive activation, because explicitly chosen entries have already passed the selection budget. A red/green route regression covers a selected recursive book as well as a non-recursive one and requires included/skipped totals to reconcile. This does not change legacy /setup or spatial caller behavior, so this issue remains open for those shared scan-then-merge paths.
Author
Owner

@SpicyMarinara commented on GitHub (Sep 8, 2026):

Ownership: @SpicyMarinara, implemented by Codex for this issue sweep. No existing open PR or issue-linked branch covers this fix. Work is on fix/lorebook-location-budget-5943 with a draft PR targeting staging; the patch will preserve the shared location budget across ordinary and recursive scans.

<!-- gh-comment-id:5592441338 --> @SpicyMarinara commented on GitHub (Sep 8, 2026): Ownership: @SpicyMarinara, implemented by Codex for this issue sweep. No existing open PR or issue-linked branch covers this fix. Work is on fix/lorebook-location-budget-5943 with a draft PR targeting staging; the patch will preserve the shared location budget across ordinary and recursive scans.
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#3286
No description provided.