[GH-ISSUE #2974] [Issue]: Per-lorebook maxRecursionDepth below 3 is silently ignored because the recursion-depth reduce is floored at 3 #1548

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

A user who configures a recursive lorebook with maxRecursionDepth of 1 or 2 (to deliberately limit how deep recursive activation chains go) gets 3 recursion passes anyway. Their setting is silently overridden upward, activating more entries, pulling in more chained content, and consuming more tokens than they asked for. Only values >= 3 are honored.

How to reproduce (try it yourself)

  1. Create a lorebook with recursiveScanning = true and maxRecursionDepth = 1 (valid per schema; range 1-10).
  2. Build a 4-deep chain: entry A's content contains a keyword for B, B's for C, C's for D.
  3. Trigger a generation that activates A via a chat keyword.

You'll see: B, C, and D all activate (effective depth 3).
Expected with maxRecursionDepth = 1: A activates, one recursion pass activates B, then it stops — C and D do not activate.

Where it is

  • packages/server/src/services/lorebook/index.ts:934-940 — the reduce computing the effective recursion depth seeds its accumulator with the literal 3 and combines with Math.max(max, b.maxRecursionDepth ?? 3).
  • Consumed at :947 as the maxDepth argument (param at :697); loop break at :742 (if (depth >= maxDepth) break;).
  • Schema range at packages/shared/src/schemas/lorebook.schema.ts:81 (.min(1).max(10).default(3)).

What's happening

The reduce seeds its accumulator with 3 (the default) and combines values with Math.max. Since the seed equals the default and Math.max never drops below it, any per-lorebook maxRecursionDepth less than 3 is discarded: Math.max(3, 1) === 3.

Suggested fix

Seed the reduce with a neutral lower bound and apply the per-lorebook default only to lorebooks that actually have recursion on, while preserving the depth-3 default for the global enableRecursive path:

const recursiveBooks = relevantLorebooks.filter((b) => b.recursiveScanning);
const maxRecursionDepth = recursiveBooks.length > 0
  ? recursiveBooks.reduce((max, b) => Math.max(max, b.maxRecursionDepth ?? 3), 1)
  : 3;

This honors an explicit maxRecursionDepth of 1 or 2 while still defaulting unset per-lorebook values to 3, and keeps the depth-3 default for the enableRecursive (recurse-everything) option, which has no per-lorebook book to read a value from.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2974 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** A user who configures a recursive lorebook with `maxRecursionDepth` of 1 or 2 (to deliberately limit how deep recursive activation chains go) gets 3 recursion passes anyway. Their setting is silently overridden upward, activating more entries, pulling in more chained content, and consuming more tokens than they asked for. Only values >= 3 are honored. **How to reproduce (try it yourself)** 1. Create a lorebook with `recursiveScanning = true` and `maxRecursionDepth = 1` (valid per schema; range 1-10). 2. Build a 4-deep chain: entry A's content contains a keyword for B, B's for C, C's for D. 3. Trigger a generation that activates A via a chat keyword. **You'll see:** B, C, and D all activate (effective depth 3). **Expected with `maxRecursionDepth = 1`:** A activates, one recursion pass activates B, then it stops — C and D do **not** activate. **Where it is** - `packages/server/src/services/lorebook/index.ts:934-940` — the `reduce` computing the effective recursion depth seeds its accumulator with the literal `3` and combines with `Math.max(max, b.maxRecursionDepth ?? 3)`. - Consumed at `:947` as the `maxDepth` argument (param at `:697`); loop break at `:742` (`if (depth >= maxDepth) break;`). - Schema range at `packages/shared/src/schemas/lorebook.schema.ts:81` (`.min(1).max(10).default(3)`). **What's happening** The reduce seeds its accumulator with `3` (the default) and combines values with `Math.max`. Since the seed equals the default and `Math.max` never drops below it, any per-lorebook `maxRecursionDepth` less than 3 is discarded: `Math.max(3, 1) === 3`. **Suggested fix** Seed the reduce with a neutral lower bound and apply the per-lorebook default only to lorebooks that actually have recursion on, while preserving the depth-3 default for the global `enableRecursive` path: ``` const recursiveBooks = relevantLorebooks.filter((b) => b.recursiveScanning); const maxRecursionDepth = recursiveBooks.length > 0 ? recursiveBooks.reduce((max, b) => Math.max(max, b.maxRecursionDepth ?? 3), 1) : 3; ``` This honors an explicit `maxRecursionDepth` of 1 or 2 while still defaulting unset per-lorebook values to 3, and keeps the depth-3 default for the `enableRecursive` (recurse-everything) option, which has no per-lorebook book to read a value from. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

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

Fixed in the v2.0.7 branch: recursive depth is now reduced from recursive-enabled lorebooks with a floor of 1, so maxRecursionDepth values of 1 or 2 are honored instead of being raised to 3. Validated with pnpm check and pnpm regression:prompt.

<!-- gh-comment-id:4831161990 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in the v2.0.7 branch: recursive depth is now reduced from recursive-enabled lorebooks with a floor of 1, so maxRecursionDepth values of 1 or 2 are honored instead of being raised to 3. Validated with pnpm check and pnpm regression:prompt.
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#1548
No description provided.