mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2693] [Issue]: Lorebook activation gates fire wrong — probability re-rolled on recursion, cooldown ticks during sticky, deterministic group selection, gt/lt NaN passes #1338
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1338
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @kolacheee on GitHub (Jun 21, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2693
Originally assigned to: @SpicyMarinara on GitHub.
Summary
Four lorebook activation gates fire entries incorrectly: (a) the probability gate is re-rolled fresh on every recursion pass, inflating activation for
<100%entries; (b) cooldown counts down during the sticky window, so cooldown expires earlier than configured; (c) inclusion-group selection is deterministic max-weight (not SillyTavern's weighted-random), so low-weight group members can never fire; (d) numericgt/ltactivation conditions pass when the game-state field is missing/non-numeric (a NaN comparison never returns false).Expected behavior
Probability should be rolled once per generation; cooldown should block for exactly its configured count regardless of sticky; group members should be selectable by weight (not permanently excluded); and a numeric
gt/ltcondition against a missing/non-numeric field should fail (not pass).Actual behavior
recursiveScancallsscanForActivatedEntriesfresh on each depth, and the per-entryprobabilityDecisionscache is local to each call. So a<100%entry that failed its roll on the primary pass gets new dice on each recursion pass — extra chances to fire.updateTimingStatesForScan, when an entry is not freshly activated,cooldownRemainingandstickyCountare both decremented in the same branch. While a sticky entry is held active (re-activated via the sticky path), its cooldown still ticks down, so cooldown expires earlier than the configured count when sticky+cooldown are both set.applyGroupSelectionsorts each group bygroupWeightdesc thenorderand keeps only the top entry, discarding the rest. ST inclusion groups pick by weighted probability, so a high-weight member always wins here and low-weight members can never fire.evaluateConditionsdoesparseFloat(fieldValue) <= parseFloat(condition.value)forgt(and the inverse forlt). When the game-state field is missing/non-numeric,parseFloatyieldsNaN, andNaN <= xisfalse, so thereturn falseis skipped and the condition passes — the entry fires when its numeric condition can't be evaluated.How it breaks the pipeline
Code trace (staging @
b826f8f4) —packages/server/src/services/lorebook/keyword-scanner.ts:recursiveScan:537-572(freshscanForActivatedEntriesper depth);probabilityDecisionsis local inscanForActivatedEntries(:417-424); gatepassesProbabilityGate:197-202.updateTimingStatesForScan:248-258(non-activation branch decrementscooldownRemainingandstickyCounttogether); sticky re-activation at:429-439.applyGroupSelection:318-351(keepsgroupEntries[0]only).evaluateConditions:105-110(gt/ltparseFloatcomparisons; NaN neverfalse).Steps to reproduce
probability=30with recursion enabled and an activated entry whose content matches it on recursion → it gets multiple 30% rolls across depths, so its effective activation rate exceeds 30%.sticky=3, cooldown=5; observe the cooldown reaching 0 before 5 messages because it ticked during the 3 sticky messages.lorewith membersA(weight 100)andB(weight 1);Aactivates every time andBnever does, even though ST would pickB~1% of the time.field=hp, operator=gt, value=50on a chat with nohpin game state →parseFloat("")=NaN,NaN<=50is false, so the condition passes and the entry fires.Suggested fix
probabilityDecisionsmap (or pre-rolled results) throughrecursiveScanso each entry is decided once across all passes.cooldownRemainingwhile the entry is held by sticky (only tick cooldown once the sticky window ends), or track them on independent clocks.groupWeightproportionally), matching ST inclusion-group behavior; keep deterministic only as an opt-in.evaluateConditions, returnfalseforgt/ltwhen either side isNaN(e.g.if (!Number.isFinite(a) || !Number.isFinite(b)) return false).Environment
b826f8f4)Additional context
Found via a systematic multi-agent audit of conditional prompt insertion. (a), (b), (d) make entries fire when they shouldn't; (c) prevents low-weight entries from ever firing. All four live in the lorebook scanner. Part of a set of lorebook-triggering issues filed together.
Template check
Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:
@kolacheee commented on GitHub (Jun 21, 2026):
Correction on sub-claim (a): the cited locus is dead code.
recursiveScan(keyword-scanner.ts:537-572) has ZERO callers anywhere in the repo — a repo-wide search finds only its definition; every other "recursiveScan" hit is the unrelatedrecursiveScanningconfig flag. It is not in the live pipeline, so threadingprobabilityDecisionsthrough it (as the suggested fix says) would change nothing observable.The probability-re-roll defect is real and shipped, but it lives in the production recursion path:
resolveBudgetAndRecursivelyActivateLorebookEntriesWithDiagnosticsin packages/server/src/services/lorebook/index.ts (called fromprocessLorebooksat index.ts:914 when recursion is enabled). That function callsscanForActivatedEntriesfresh on the initial scan (index.ts:705) and on every recursion depth (index.ts:740); each call builds its own localprobabilityDecisionsmap (keyword-scanner.ts:417). An entry that fails its probability roll is dropped inside the scan (continueat keyword-scanner.ts:446/492/514) and is never returned infrontier, so it is never added toprocessedIds(index.ts:712-714, which only iterates thecandidatestaken fromfrontier) and remains inremaining(index.ts:737) — getting a brand-new probability roll on the next depth at index.ts:740.So (a) is a genuine bug, but the fix must be applied in index.ts, not to
recursiveScan. Concretely: thread a sharedprobabilityDecisionsMap (or pre-rolled per-entry results) into eachscanForActivatedEntriescall across depths via ScanOptions, or pre-roll all entry probabilities once before the recursion loop.randomis already injectable via ScanOptions (keyword-scanner.ts:377), so adding a shared decision cache there is straightforward. Recommend also deleting the unusedrecursiveScanto prevent future confusion.Sub-claims (b), (c), (d) are accurate and their line citations (105-110, 248-258 + 429-439, 318-351) all match the current tree exactly. Two minor refinements: (b) is slightly understated — when cooldown <= sticky the cooldown fully expires inside the sticky window (effectively zero post-sticky cooldown), not just "earlier". (c) note: ST's opt-in deterministic mode tiebreaks on highest Order ("Prioritize Inclusion"); Marinara has no equivalent field today (only
groupWeight), so any deterministic opt-in should tiebreak on Order to match ST rather than on weight.@SpicyMarinara commented on GitHub (Jun 21, 2026):
Fixed in branch
codex/spicy-issue-sweep-2026-06-21.What changed:
gt/ltactivation conditions fail when either side is missing or non-numeric.recursiveScanhelper honor the same probability-decision cache so it does not preserve the old pattern.Final repo checks will run after the full issue sweep.