[PR #2250] [MERGED] Fix cross-lorebook lorebook recursion #5511

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

📋 Pull Request Information

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

Base: refactor ← Head: fix/issue-2185-lorebook-cross-recursion


📝 Commits (6)

  • 2db6c5d Fix cross-lorebook recursive scanning
  • 0844ef3 Record PR metadata for issue 2185 proof
  • e9d5de7 Address lorebook recursion review findings
  • 580b4cf Merge upstream/refactor into issue 2185 fix
  • 127e8a0 Preserve regenerate metadata on target read failure
  • 71ea0f2 Merge latest refactor lorebook marker changes

📊 Changes

8 files changed (+562 additions, -199 deletions)

View changed files

📝 scratch/bugfix-verification.json (+73 -55)
📝 src/engine/generation-core/lorebooks/keyword-scanner.ts (+0 -40)
📝 src/engine/generation-core/lorebooks/prompt-injector.test.ts (+3 -3)
📝 src/engine/generation-core/lorebooks/prompt-injector.ts (+1 -1)
📝 src/engine/generation/active-lorebook-scanner.test.ts (+251 -0)
📝 src/engine/generation/active-lorebook-scanner.ts (+202 -99)
📝 src/engine/generation/start-generation.test.ts (+31 -0)
📝 src/engine/generation/start-generation.ts (+1 -1)

📄 Description

Linked issue

Closes #2185

Why this change

  • Refactor lorebook recursion scanned each active lorebook independently, so selected content from lorebook A could not recursively activate a matching entry in lorebook B.
  • Recursion also ran before the final chat lorebook budget, which let entries excluded by that budget still act as recursive frontier content.

What changed

  • Reworked prompt-time active lorebook scanning to select budgeted candidates from the combined active entry set, then recurse only from entries that were actually selected.
  • Preserved lorebook and chat budget diagnostics while applying recursion across all active lorebooks.
  • Removed the now-obsolete exported recursive scanner helper and moved its supported behavior into the budget-aware active scanner.
  • Added regression rows for cross-lorebook activation, preventRecursion, configured recursion depth, and budget-skipped frontier behavior.

Refactor impact

Primary owner:

engine generation

Impact areas reviewed:

  • Chat, roleplay, and game prompt assembly all use the shared scanActiveLorebooks engine path.
  • Lorebook token budgets, chat lorebook budget, preventRecursion, and entry timing state updates.

Boundary notes:

  • React-free engine change only. No feature UI, shared API wrapper, Tauri/Rust storage, remote runtime, schema, dependency, auth, or release metadata changes.

Pressure points touched:

  • Shared engine lorebook activation in src/engine/generation/active-lorebook-scanner.ts.

Validation

  • Matching validation command passes locally (for example pnpm typecheck, pnpm build, pnpm check:architecture, pnpm check:docs, or full pnpm check when warranted)
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • Codex proof: before the production fix, pnpm test src/engine/generation/active-lorebook-scanner.test.ts failed on the new regression rows: cross-book recursion omitted entry-b, and a budget-skipped frontier activated entry-recursive-only.
  • Codex proof after fix: pnpm test src/engine/generation/active-lorebook-scanner.test.ts src/engine/generation-core/lorebooks/prompt-injector.test.ts passed with 7 tests.
  • Codex lane validation: pnpm typecheck passed.
  • Codex full gate: pnpm check passed after the final diff.
  • Codex proof gate: node C:\Users\celia\.codex\tools\marinara-proof-gate.mjs scratch/bugfix-verification.json passed.
  • Bunny review: pass. No blocking manual verification remains for the core claim.

Feature Discoverability

Check exactly one:

  • Updated src/features/shell/discovery/ because this PR adds or materially changes a user-discoverable feature, workflow, setting, mode, panel, import path, agent, media capability, or advanced tool.
  • N/A because this PR is only a bugfix, refactor, test, docs, internal wiring, visual polish, copy edit, or compatibility fix and does not add a new thing users need to find.

Reason:

  • Bugfix for existing lorebook runtime behavior; no new user-discoverable product surface.

Docs and release impact

  • No docs changes needed
  • Updated README.md
  • Updated CONTRIBUTING.md
  • Updated docs/developer/
  • Updated repo skills or AGENTS.md
  • Confirmed this PR does not restore old staging/package-workspace/release claims

UI evidence

N/A. This is React-free engine prompt assembly logic with command proof; there is no meaningful visible UI state to screenshot.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed recursive lorebook scanning and budget-selection behavior so cross-lorebook recursion, recursion depth and preventRecursion are honored.
  • Refactor

    • Unified lorebook scanning and token-budget selection into a single flow; improved how skipped entries and budgets are tracked and reported.
  • Tests

    • Added coverage for recursive scanning across active lorebooks, budget skipping, priority ordering, and recursion depth limits.

🔄 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/2250 **Author:** [@cha1latte](https://github.com/cha1latte) **Created:** 6/4/2026 **Status:** ✅ Merged **Merged:** 6/5/2026 **Merged by:** [@cha1latte](https://github.com/cha1latte) **Base:** `refactor` ← **Head:** `fix/issue-2185-lorebook-cross-recursion` --- ### 📝 Commits (6) - [`2db6c5d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2db6c5d99ad53559e590d028e09bd58e6bb272f3) Fix cross-lorebook recursive scanning - [`0844ef3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/0844ef33fd368690c2c6926336de695a0a3a6b6b) Record PR metadata for issue 2185 proof - [`e9d5de7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e9d5de7771b0287627043db17d99e5164a1334f4) Address lorebook recursion review findings - [`580b4cf`](https://github.com/Pasta-Devs/Marinara-Engine/commit/580b4cfcc11986cf6c10fc14d96cab1274a919c0) Merge upstream/refactor into issue 2185 fix - [`127e8a0`](https://github.com/Pasta-Devs/Marinara-Engine/commit/127e8a0065ee28776c3209e1285d6bfe8ca54569) Preserve regenerate metadata on target read failure - [`71ea0f2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/71ea0f2fb32b45abcd3044381cd8d545f362cfd8) Merge latest refactor lorebook marker changes ### 📊 Changes **8 files changed** (+562 additions, -199 deletions) <details> <summary>View changed files</summary> 📝 `scratch/bugfix-verification.json` (+73 -55) 📝 `src/engine/generation-core/lorebooks/keyword-scanner.ts` (+0 -40) 📝 `src/engine/generation-core/lorebooks/prompt-injector.test.ts` (+3 -3) 📝 `src/engine/generation-core/lorebooks/prompt-injector.ts` (+1 -1) 📝 `src/engine/generation/active-lorebook-scanner.test.ts` (+251 -0) 📝 `src/engine/generation/active-lorebook-scanner.ts` (+202 -99) 📝 `src/engine/generation/start-generation.test.ts` (+31 -0) 📝 `src/engine/generation/start-generation.ts` (+1 -1) </details> ### 📄 Description <!-- Target branch: `refactor`. Change the base to `refactor` before submitting unless a maintainer explicitly asked for another base. --> <!-- Keep all checkboxes unchecked until a human has actually verified them. --> ## Linked issue <!-- Every user-facing PR should reference a feature request or issue report when practical. --> Closes #2185 ## Why this change <!-- What user problem, bug, refactor goal, or maintenance need does this solve? --> - Refactor lorebook recursion scanned each active lorebook independently, so selected content from lorebook A could not recursively activate a matching entry in lorebook B. - Recursion also ran before the final chat lorebook budget, which let entries excluded by that budget still act as recursive frontier content. ## What changed <!-- List the key changes in this PR. --> - Reworked prompt-time active lorebook scanning to select budgeted candidates from the combined active entry set, then recurse only from entries that were actually selected. - Preserved lorebook and chat budget diagnostics while applying recursion across all active lorebooks. - Removed the now-obsolete exported recursive scanner helper and moved its supported behavior into the budget-aware active scanner. - Added regression rows for cross-lorebook activation, `preventRecursion`, configured recursion depth, and budget-skipped frontier behavior. ## Refactor impact Primary owner: <!-- Examples: app shell, catalog, chat mode, roleplay mode, game mode, runtime generation, world-state, shared API, engine generation, Rust storage, imports, remote runtime. --> engine generation Impact areas reviewed: - Chat, roleplay, and game prompt assembly all use the shared `scanActiveLorebooks` engine path. - Lorebook token budgets, chat lorebook budget, `preventRecursion`, and entry timing state updates. Boundary notes: <!-- Note engine/shared-api/feature/Rust/remote-runtime boundaries. Say "none" only if you checked. --> - React-free engine change only. No feature UI, shared API wrapper, Tauri/Rust storage, remote runtime, schema, dependency, auth, or release metadata changes. Pressure points touched: <!-- Mention ModeSurface, GameSurface, shared mode UI, src-tauri/src/lib.rs command registration, or import modules if touched. --> - Shared engine lorebook activation in `src/engine/generation/active-lorebook-scanner.ts`. ## Validation <!-- Check only what you personally ran or manually verified. Treat unchecked items as explicit TODOs. --> <!-- Do not submit *.test.ts or *.test.tsx files as PR proof; cite existing checks, command output, app/browser/Tauri verification, or manual notes instead. --> - [ ] Matching validation command passes locally (for example `pnpm typecheck`, `pnpm build`, `pnpm check:architecture`, `pnpm check:docs`, or full `pnpm check` when warranted) - [ ] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes <!-- Describe exact commands and manual steps, including typecheck/build/docs/architecture/Rust/browser/remote-runtime checks when they apply. If an AI agent filled this out, verify it yourself before ticking boxes. --> - Codex proof: before the production fix, `pnpm test src/engine/generation/active-lorebook-scanner.test.ts` failed on the new regression rows: cross-book recursion omitted `entry-b`, and a budget-skipped frontier activated `entry-recursive-only`. - Codex proof after fix: `pnpm test src/engine/generation/active-lorebook-scanner.test.ts src/engine/generation-core/lorebooks/prompt-injector.test.ts` passed with 7 tests. - Codex lane validation: `pnpm typecheck` passed. - Codex full gate: `pnpm check` passed after the final diff. - Codex proof gate: `node C:\Users\celia\.codex\tools\marinara-proof-gate.mjs scratch/bugfix-verification.json` passed. - Bunny review: pass. No blocking manual verification remains for the core claim. ## Feature Discoverability Check exactly one: - [ ] Updated `src/features/shell/discovery/` because this PR adds or materially changes a user-discoverable feature, workflow, setting, mode, panel, import path, agent, media capability, or advanced tool. - [x] N/A because this PR is only a bugfix, refactor, test, docs, internal wiring, visual polish, copy edit, or compatibility fix and does not add a new thing users need to find. Reason: - Bugfix for existing lorebook runtime behavior; no new user-discoverable product surface. ## Docs and release impact - [x] No docs changes needed - [ ] Updated `README.md` - [ ] Updated `CONTRIBUTING.md` - [ ] Updated `docs/developer/` - [ ] Updated repo skills or `AGENTS.md` - [ ] Confirmed this PR does not restore old staging/package-workspace/release claims ## UI evidence <!-- Add before/after screenshots or recordings for visible UI changes. --> N/A. This is React-free engine prompt assembly logic with command proof; there is no meaningful visible UI state to screenshot. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed recursive lorebook scanning and budget-selection behavior so cross-lorebook recursion, recursion depth and preventRecursion are honored. * **Refactor** * Unified lorebook scanning and token-budget selection into a single flow; improved how skipped entries and budgets are tracked and reported. * **Tests** * Added coverage for recursive scanning across active lorebooks, budget skipping, priority ordering, and recursion depth limits. <!-- 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:16:57 +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#5511
No description provided.