[GH-ISSUE #2278] [Issue]: Budget-dropped lorebook entries leak setvar mutations into the shared macro context (no variable-snapshot rollback) #1115

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

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

Originally assigned to: @cha1latte on GitHub.

Summary

When a lorebook entry that runs {{setvar}}/{{addvar}} is dropped because it is over the token budget, the refactor still applies that dropped entry's variable mutation to the shared macro context. Main rolls this back so only injected entries affect variables.

Expected behavior

Only the lorebook entries that are actually kept and injected should mutate macro variables. An entry that is budget-dropped should leave no variable side effect, so a later kept entry's {{getvar}} reads the value it would have read on main.

Actual behavior

The refactor resolves an entry's macros (mutating shared ctx.variables in place) before the budget check, then drops the entry without restoring the variables. Later entries and prompt sections that read those variables see a value set by an entry that was never injected.

Steps to reproduce

  1. Set a tight lorebook token budget.
  2. Add an entry A (lower selection priority, large) that runs {{setvar::flag::X}}, and a kept entry B (or any later prompt section) that reads {{getvar::flag}}.
  3. Generate so A is budget-dropped. Observe B reads X in the refactor; on main it reads the prior value.

Impact area

  • React-free engine behavior, contracts, agents, or capability ports
  • Runtime systems: generation, world-state, visuals, tracker, or haptics

Regression notes

Worked on main. Main wraps content resolution in a variable-snapshot transaction: resolveMacrosWithVariableSnapshot (packages/server/src/services/prompt/macro-context.ts:50-58) and selectBudgetedLorebookEntryBatch rollback (packages/server/src/services/lorebook/index.ts:514-545), wired at assembler.ts:304, generate.routes.ts:2328, game.routes.ts:3222. The refactor's selectBudgetedLorebookEntries (src/engine/generation/active-lorebook-scanner.ts:595-627) resolves before the budget check (line 599) and continues an over-budget entry (line 626) with no rollback; the resolver shares one macros object (src/engine/generation/prompt-assembly.ts:2862,2885-2887) and resolveMacros mutates ctx.variables in place (src/engine/shared/macros/macro-engine.ts:729-730,851-868). Grepping the whole tree (src + src-tauri + crates) found no macro-variable snapshot/rollback; the only rollbacks are storage/import. The chosen entry set is byte-identical, so this is purely the variable side effect, hence minor.

Environment

  • Branch or commit: refactor @ 5ddb9840 (compared against main @ b4ab97f)
  • Run mode: Tauri desktop
  • OS + version: Windows 11
  • Node + pnpm versions: not relevant (static source comparison)
  • Rust version, if relevant: not relevant (static source comparison)
  • Remote Runtime URL configured? not relevant
  • Provider, model, or integration, if relevant: see body

Template check

Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2278 Originally assigned to: @cha1latte on GitHub. ## Summary When a lorebook entry that runs {{setvar}}/{{addvar}} is dropped because it is over the token budget, the refactor still applies that dropped entry's variable mutation to the shared macro context. Main rolls this back so only injected entries affect variables. ## Expected behavior Only the lorebook entries that are actually kept and injected should mutate macro variables. An entry that is budget-dropped should leave no variable side effect, so a later kept entry's {{getvar}} reads the value it would have read on main. ## Actual behavior The refactor resolves an entry's macros (mutating shared `ctx.variables` in place) before the budget check, then drops the entry without restoring the variables. Later entries and prompt sections that read those variables see a value set by an entry that was never injected. ## Steps to reproduce 1. Set a tight lorebook token budget. 2. Add an entry A (lower selection priority, large) that runs {{setvar::flag::X}}, and a kept entry B (or any later prompt section) that reads {{getvar::flag}}. 3. Generate so A is budget-dropped. Observe B reads X in the refactor; on main it reads the prior value. ## Impact area - [x] React-free engine behavior, contracts, agents, or capability ports - [x] Runtime systems: generation, world-state, visuals, tracker, or haptics ## Regression notes Worked on main. Main wraps content resolution in a variable-snapshot transaction: `resolveMacrosWithVariableSnapshot` (`packages/server/src/services/prompt/macro-context.ts:50-58`) and `selectBudgetedLorebookEntryBatch` rollback (`packages/server/src/services/lorebook/index.ts:514-545`), wired at `assembler.ts:304`, `generate.routes.ts:2328`, `game.routes.ts:3222`. The refactor's `selectBudgetedLorebookEntries` (`src/engine/generation/active-lorebook-scanner.ts:595-627`) resolves before the budget check (line 599) and `continue`s an over-budget entry (line 626) with no rollback; the resolver shares one macros object (`src/engine/generation/prompt-assembly.ts:2862,2885-2887`) and `resolveMacros` mutates `ctx.variables` in place (`src/engine/shared/macros/macro-engine.ts:729-730,851-868`). Grepping the whole tree (src + src-tauri + crates) found no macro-variable snapshot/rollback; the only rollbacks are storage/import. The chosen entry set is byte-identical, so this is purely the variable side effect, hence minor. ## Environment - Branch or commit: refactor @ 5ddb9840 (compared against main @ b4ab97f) - Run mode: Tauri desktop - OS + version: Windows 11 - Node + pnpm versions: not relevant (static source comparison) - Rust version, if relevant: not relevant (static source comparison) - Remote Runtime URL configured? not relevant - Provider, model, or integration, if relevant: see body ## Template check Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:27:00 +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#1115
No description provided.