[PR #1565] [MERGED] Fix lorebook prompt activation settings #5121

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/1565
Author: @Xelvanis
Created: 5/29/2026
Status: ✅ Merged
Merged: 5/29/2026
Merged by: @Xelvanis

Base: refactor ← Head: codex/lorebook-activation


📝 Commits (2)

  • 1e79c31 Fix lorebook prompt activation settings
  • f7e699a Address lorebook activation failpaths

📊 Changes

4 files changed (+629 additions, -52 deletions)

View changed files

📝 src/engine/generation-core/lorebooks/prompt-injector.ts (+54 -17)
📝 src/engine/generation/active-lorebooks.ts (+3 -3)
📝 src/engine/generation/prompt-assembly.test.ts (+341 -1)
📝 src/engine/generation/prompt-assembly.ts (+231 -31)

📄 Description

Linked issue

Closes #1543
Closes #1544
Closes #1545
Closes #1546
Closes #1547

Why this change

  • Lorebook editor/chat settings saved several activation controls that prompt assembly did not honor, so prompt preview/generation could disagree with the UI and inject the wrong world-info entries.
  • Follow-up failure-path review found two hardening gaps: malformed recursion depth could bypass the schema max in prompt assembly, and budget-skipped active lore could be hidden from the active-world-info preview.

What changed

  • Prompt assembly now skips entries inside disabled lorebook folders.
  • Chat-scoped lorebooks now apply when lorebook.chatId matches the current chat, even when the book is not pinned in activeLorebookIds.
  • Character/persona additional matching sources are passed into lorebook scanning.
  • Lorebook-level scanDepth is inherited by entries without an entry-level override, and recursive lorebooks use recursiveScan with the schema max depth cap.
  • Per-lorebook and chat-level lorebook token budgets are applied before active prompt output.
  • Active-world-info scans now surface budget-skipped lore entries instead of silently reporting no active lore.
  • Added focused prompt-assembly regression tests for the covered issue rows and failpath fixes.

Refactor impact

Primary owner:

engine generation / generation-core lorebooks

Impact areas reviewed:

  • Prompt assembly active lorebook selection and injection.
  • Active lorebook preview output, which is backed by prompt assembly.
  • Existing lorebook folder update invalidation in useUpdateLorebookFolder.
  • Chat, roleplay, and game shared prompt assembly paths.
  • Agent/tool consumers of activatedLorebookEntries.

Boundary notes:

  • Engine-only behavior change. No shared API, Rust command, storage schema, import/export, or remote runtime boundary changes.
  • Catalog UI already invalidates active lorebook scans when folder enabled state changes, so no feature-layer patch was needed for #1543.

Pressure points touched:

  • None. No ModeSurface, GameSurface, shared mode UI, src-tauri registry, or import modules touched.

Validation

  • pnpm check passes locally
  • pnpm typecheck passes locally
  • pnpm build passes locally
  • pnpm check:architecture passes locally
  • pnpm check:docs passes locally
  • cargo check --manifest-path src-tauri/Cargo.toml --workspace passes locally
  • Rust clippy/tests were run for Rust behavior changes
  • Browser or Tauri app manual verification completed
  • Playwright, screenshot, or recording evidence added for UI changes
  • Remote runtime smoke checked when relevant

Manual verification notes

  • Reproduced before fix with focused Vitest rows: pnpm exec vitest run src/engine/generation/prompt-assembly.test.ts -t "lorebook activation settings" failed 7/7 expected rows.
  • Verified after initial fix:
    • pnpm exec vitest run src/engine/generation/prompt-assembly.test.ts -t "lorebook activation settings" passed.
    • pnpm exec vitest run src/engine/generation/prompt-assembly.test.ts passed.
    • pnpm exec vitest run src/engine/generation-core/lorebooks/keyword-scanner.test.ts src/engine/generation/start-generation.test.ts src/engine/generation/runtime-records.test.ts src/engine/generation/prompt-overrides.test.ts src/engine/generation/prompt-assembly.test.ts src/engine/generation/main-tool-loop.test.ts src/engine/generation/generate-route-utils.test.ts src/engine/generation/connected-commands.test.ts src/engine/generation/agent-runner.test.ts passed.
    • pnpm typecheck passed.
    • pnpm check:architecture passed.
    • pnpm build passed with the existing Vite large-chunk warning.
  • Verified after failpath fixes:
    • pnpm exec vitest run src/engine/generation/prompt-assembly.test.ts passed, 50 tests.
    • pnpm exec vitest run src/engine/generation-core/lorebooks/keyword-scanner.test.ts src/engine/generation/start-generation.test.ts src/engine/generation/runtime-records.test.ts src/engine/generation/prompt-overrides.test.ts src/engine/generation/prompt-assembly.test.ts src/engine/generation/main-tool-loop.test.ts src/engine/generation/generate-route-utils.test.ts src/engine/generation/connected-commands.test.ts src/engine/generation/agent-runner.test.ts passed, 173 tests.
    • pnpm typecheck passed.
    • pnpm check:architecture passed.
    • pnpm build passed with the existing Vite large-chunk warning.
  • Fanout failpath review fixed the confirmed recursion-depth and budget-skipped-preview findings.
  • Fanout Bunny review returned no confirmed findings after those fixes.
  • Native Tauri QA passed after the follow-up request:
    • Reused the already-running Marinara Engine QA window launched through pnpm tauri dev --no-watch --config scratch\tauri-qa.config.json against http://localhost:1420.
    • WebView2 CDP confirmed the React root mounted and Tauri internals were present in the native WebView.
    • Exercised focused lorebook prompt scenarios inside the native WebView: disabled-folder suppression, chat-scoped lorebook activation, character additional matching source activation, recursion-depth clamping, and active-world-info budget-skipped telemetry.
    • Saved local QA screenshots at scratch/tauri-qa-lorebook-activation.png and scratch/tauri-window-lorebook-activation.png.
  • #1548 remains separate because timing persistence needs per-chat runtime state writeback, not just prompt assembly scanning.

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

No CHANGELOG.md exists on this branch. No docs changes were made because this fixes existing prompt behavior without changing the public setup or architecture docs.

UI evidence

Native Tauri QA evidence was captured from the already-running QA window after the engine prompt scenarios passed. Local artifacts: scratch/tauri-qa-lorebook-activation.png and scratch/tauri-window-lorebook-activation.png.

Summary by CodeRabbit

  • Refactor

    • Enhanced lorebook entry management to properly track and distinguish entries skipped due to token budget constraints during prompt assembly.
  • Tests

    • Expanded test coverage for lorebook activation settings, token budget enforcement, and recursive scanning behavior.

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/1565 **Author:** [@Xelvanis](https://github.com/Xelvanis) **Created:** 5/29/2026 **Status:** ✅ Merged **Merged:** 5/29/2026 **Merged by:** [@Xelvanis](https://github.com/Xelvanis) **Base:** `refactor` ← **Head:** `codex/lorebook-activation` --- ### 📝 Commits (2) - [`1e79c31`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1e79c3179c697a5b05af16006d1e302d82e31bdb) Fix lorebook prompt activation settings - [`f7e699a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f7e699a974a116c08032742d2d0e44db50dd79bf) Address lorebook activation failpaths ### 📊 Changes **4 files changed** (+629 additions, -52 deletions) <details> <summary>View changed files</summary> 📝 `src/engine/generation-core/lorebooks/prompt-injector.ts` (+54 -17) 📝 `src/engine/generation/active-lorebooks.ts` (+3 -3) 📝 `src/engine/generation/prompt-assembly.test.ts` (+341 -1) 📝 `src/engine/generation/prompt-assembly.ts` (+231 -31) </details> ### 📄 Description ## Linked issue Closes #1543 Closes #1544 Closes #1545 Closes #1546 Closes #1547 ## Why this change - Lorebook editor/chat settings saved several activation controls that prompt assembly did not honor, so prompt preview/generation could disagree with the UI and inject the wrong world-info entries. - Follow-up failure-path review found two hardening gaps: malformed recursion depth could bypass the schema max in prompt assembly, and budget-skipped active lore could be hidden from the active-world-info preview. ## What changed - Prompt assembly now skips entries inside disabled lorebook folders. - Chat-scoped lorebooks now apply when `lorebook.chatId` matches the current chat, even when the book is not pinned in `activeLorebookIds`. - Character/persona additional matching sources are passed into lorebook scanning. - Lorebook-level `scanDepth` is inherited by entries without an entry-level override, and recursive lorebooks use `recursiveScan` with the schema max depth cap. - Per-lorebook and chat-level lorebook token budgets are applied before active prompt output. - Active-world-info scans now surface budget-skipped lore entries instead of silently reporting no active lore. - Added focused prompt-assembly regression tests for the covered issue rows and failpath fixes. ## Refactor impact Primary owner: engine generation / generation-core lorebooks Impact areas reviewed: - Prompt assembly active lorebook selection and injection. - Active lorebook preview output, which is backed by prompt assembly. - Existing lorebook folder update invalidation in `useUpdateLorebookFolder`. - Chat, roleplay, and game shared prompt assembly paths. - Agent/tool consumers of `activatedLorebookEntries`. Boundary notes: - Engine-only behavior change. No shared API, Rust command, storage schema, import/export, or remote runtime boundary changes. - Catalog UI already invalidates active lorebook scans when folder enabled state changes, so no feature-layer patch was needed for #1543. Pressure points touched: - None. No ModeSurface, GameSurface, shared mode UI, src-tauri registry, or import modules touched. ## Validation - [ ] `pnpm check` passes locally - [x] `pnpm typecheck` passes locally - [x] `pnpm build` passes locally - [x] `pnpm check:architecture` passes locally - [ ] `pnpm check:docs` passes locally - [ ] `cargo check --manifest-path src-tauri/Cargo.toml --workspace` passes locally - [ ] Rust clippy/tests were run for Rust behavior changes - [x] Browser or Tauri app manual verification completed - [ ] Playwright, screenshot, or recording evidence added for UI changes - [ ] Remote runtime smoke checked when relevant ### Manual verification notes - Reproduced before fix with focused Vitest rows: `pnpm exec vitest run src/engine/generation/prompt-assembly.test.ts -t "lorebook activation settings"` failed 7/7 expected rows. - Verified after initial fix: - `pnpm exec vitest run src/engine/generation/prompt-assembly.test.ts -t "lorebook activation settings"` passed. - `pnpm exec vitest run src/engine/generation/prompt-assembly.test.ts` passed. - `pnpm exec vitest run src/engine/generation-core/lorebooks/keyword-scanner.test.ts src/engine/generation/start-generation.test.ts src/engine/generation/runtime-records.test.ts src/engine/generation/prompt-overrides.test.ts src/engine/generation/prompt-assembly.test.ts src/engine/generation/main-tool-loop.test.ts src/engine/generation/generate-route-utils.test.ts src/engine/generation/connected-commands.test.ts src/engine/generation/agent-runner.test.ts` passed. - `pnpm typecheck` passed. - `pnpm check:architecture` passed. - `pnpm build` passed with the existing Vite large-chunk warning. - Verified after failpath fixes: - `pnpm exec vitest run src/engine/generation/prompt-assembly.test.ts` passed, 50 tests. - `pnpm exec vitest run src/engine/generation-core/lorebooks/keyword-scanner.test.ts src/engine/generation/start-generation.test.ts src/engine/generation/runtime-records.test.ts src/engine/generation/prompt-overrides.test.ts src/engine/generation/prompt-assembly.test.ts src/engine/generation/main-tool-loop.test.ts src/engine/generation/generate-route-utils.test.ts src/engine/generation/connected-commands.test.ts src/engine/generation/agent-runner.test.ts` passed, 173 tests. - `pnpm typecheck` passed. - `pnpm check:architecture` passed. - `pnpm build` passed with the existing Vite large-chunk warning. - Fanout failpath review fixed the confirmed recursion-depth and budget-skipped-preview findings. - Fanout Bunny review returned no confirmed findings after those fixes. - Native Tauri QA passed after the follow-up request: - Reused the already-running `Marinara Engine QA` window launched through `pnpm tauri dev --no-watch --config scratch\tauri-qa.config.json` against `http://localhost:1420`. - WebView2 CDP confirmed the React root mounted and Tauri internals were present in the native WebView. - Exercised focused lorebook prompt scenarios inside the native WebView: disabled-folder suppression, chat-scoped lorebook activation, character additional matching source activation, recursion-depth clamping, and active-world-info budget-skipped telemetry. - Saved local QA screenshots at `scratch/tauri-qa-lorebook-activation.png` and `scratch/tauri-window-lorebook-activation.png`. - #1548 remains separate because timing persistence needs per-chat runtime state writeback, not just prompt assembly scanning. ## Docs and release impact - [x] No docs changes needed - [ ] Updated `README.md` - [ ] Updated `CONTRIBUTING.md` - [ ] Updated `docs/developer/` - [ ] Updated repo skills or `AGENTS.md` - [x] Confirmed this PR does not restore old staging/package-workspace/release claims No `CHANGELOG.md` exists on this branch. No docs changes were made because this fixes existing prompt behavior without changing the public setup or architecture docs. ## UI evidence Native Tauri QA evidence was captured from the already-running QA window after the engine prompt scenarios passed. Local artifacts: scratch/tauri-qa-lorebook-activation.png and scratch/tauri-window-lorebook-activation.png. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Enhanced lorebook entry management to properly track and distinguish entries skipped due to token budget constraints during prompt assembly. * **Tests** * Expanded test coverage for lorebook activation settings, token budget enforcement, and recursive scanning behavior. <!-- 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/1565?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:13:30 +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#5121
No description provided.