[GH-ISSUE #6125] The Game Master's lorebook search always finds something, even when the lorebook has nothing to do with the question #3402

Closed
opened 2026-10-21 06:02:44 +07:00 by bsakaguchi · 2 comments
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

When the Game Master searches an enabled lorebook, the search can never come back empty. If the lorebook has any vectorized entries at all, the search returns its best-ranked entries no matter how unrelated they are - so a book about an orchard answers a question about the docks, and the Game Master is told it found a match.

How to reproduce (try it yourself)

Before you start you need two things: an embedding connection the chat can use (Settings -> Connections, or the local embedder) - vectorize with the same connection you then chat on - and a chat connection that supports native tool calls.

  1. Lorebooks -> New lorebook, name it Orchard Valley. Add three entries, none of which mentions docks, harbours, ships or shipping:
  • Elena - keys elena, orchard - "Elena tends the orchard on the northern hill and bakes pear tarts."
  • Winter Festival - keys festival - "The village lights lanterns on the longest night."
  • The Quarry Road - keys quarry - "A rutted track climbs to the abandoned granite quarry."
  1. In the lorebook's Vectorize section, turn vectorization on and vectorize all three entries.
  2. Open a Game-mode chat and enable Orchard Valley for it. Go to Chat Settings -> Function Calling, pick a connection that supports native tool calls (the lore-search switch stays greyed out until you do), then turn on the lore-search switch ("Search enabled, vectorized lore by meaning…"). You can leave the general tools toggle off; this switch attaches the search on its own.
  3. Send exactly: Who runs the docks in this town?
  4. Expand the tool-call card for the lore search in that turn and read the result.

What you'll see: "count": 3 - all three entries handed back for a book that says nothing about docks, most of them with "similarity": 0. (Don't compare the query field in the result to the sentence you typed; it echoes whatever wording the Game Master chose.)

What should happen: "count": 0 with an empty result list, so the Game Master is told plainly that nothing in the book matched, instead of being handed the least-irrelevant entries.

There is a two-second version with no provider involved: a small script driving the real resolveGenerationTools and executeToolCallForModel through a deterministic mock embedder reproduces the same result (count: 3, scores 0.214 / 0 / 0).

Where it is

packages/server/src/services/generation/tool-resolution-runtime.ts, searchLorebookForTools (lines 832-845 at staging 52799569c): when any entry in scope has a vector, it calls semanticShortlistLorebookEntries(vectorized, query, { ...lorebookEmbeddingOptions, topK: 20 }) - no minimum similarity is passed - and returns immediately, before the substring branch at :856-871 ever runs.

packages/server/src/services/lorebook/embeddings.ts, semanticShortlistLorebookEntries (:255-270): scores, sorts, .slice(0, topK), then return matches.length > 0 ? matches : null. A non-empty list is the only gate. calibrateLorebookSimilarity (:114-118) clamps to [0,1], so an unrelated entry lands on exactly 0 and is still returned.

packages/server/src/services/tools/tool-executor.ts:804-806 passes { query, category, results, count } to the model verbatim.

What's happening

The semantic shortlist is a plain top-20 ranker with no notion of "not good enough". Similarity is calibrated against three fixed, mutually unrelated sentences, so genuine noise calibrates to about zero - and zero survives the ranker, because the only thing checked is whether the list is non-empty. Since this branch returns early, the keyword branch that would have produced an empty list never runs; and in Game mode the search is called with vectors required, so today the tool can report "no match" only when it has no vectors at all, never when it has vectors and none of them are relevant.

Suggested fix

Give the shortlist a minimum-relevance argument, and have the lore-search tool return an empty result set when nothing clears it, so the model can distinguish "nothing matched" from "here is the closest thing I have". Alternatively, or in addition, tell the model explicitly in the result when the top row did not beat the unrelated-text baseline.

On where the number comes from: the codebase already has two floors to look at - SEMANTIC_SUMMARY_MIN_SIMILARITY = 0.15 in roleplay-summary-retrieval.ts for auto-injected summaries, and the per-lorebook Score Threshold (default 0.3) that automatic activation applies in keyword-scanner.ts:643-655. Treat these as precedents for picking a value, not as settings to wire through: #5955 deliberately let this tool reach past the per-turn activation threshold and result cap, and that per-lorebook setting is worded and documented as governing activation, so honouring it verbatim here would work against what the tool was built for. A low baseline-relative floor keeps the feature's reach while letting it say no. Sanity-check the threshold against a real embedding backend rather than assuming the activation default transfers.

knowledge-router.ts:264-280 calls the same floor-less shortlist; the fix should decide explicitly whether the floor applies there too (the consequence is milder, since an LLM then picks from the shortlist). A negative-case regression is cheap: scripts/regressions/game-tool-controls.regression.ts:300-306 already builds this exact fixture and asserts only the positive direction.

Context

This is how the semantic lore-search path shipped in PR #5982, which implemented issue #5955 (both merged after v2.4.4, so this is unreleased on staging). Neither the issue nor the PR mentions a relevance floor, and no tracker exists for it; related-but-separate floor bugs were handled in #2287 (memory recall) and #3627 (automatic activation). One reading is that a scored top-k list is simply the ordinary shape of a search tool and the model can discount a zero - against that: this is the one feature meant to stop the Game Master inventing lore, nothing in the tool's description tells the model what a similarity of 0 means, and a retrieval tool that never returns nothing teaches the model to treat every row as an answer. A sibling issue about the same function - the search answering only from the vectorized part of a partly-vectorized book - is filed separately; it is a different fix.

Found by an AI-assisted sweep of merged PR #5982 against issue #5955, then re-verified against staging 52799569c on 2026-09-13. Please verify before fixing.

Related issue from the same sweep: #6124 (the partly-vectorized book case in the same search function).

Originally created by @kolacheee on GitHub (Sep 13, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6125 Originally assigned to: @SpicyMarinara on GitHub. When the Game Master searches an enabled lorebook, the search can never come back empty. If the lorebook has any vectorized entries at all, the search returns its best-ranked entries no matter how unrelated they are - so a book about an orchard answers a question about the docks, and the Game Master is told it found a match. ## How to reproduce (try it yourself) Before you start you need two things: an embedding connection the chat can use (Settings -> Connections, or the local embedder) - vectorize with the same connection you then chat on - and a chat connection that supports native tool calls. 1. Lorebooks -> New lorebook, name it `Orchard Valley`. Add three entries, none of which mentions docks, harbours, ships or shipping: - `Elena` - keys `elena, orchard` - "Elena tends the orchard on the northern hill and bakes pear tarts." - `Winter Festival` - keys `festival` - "The village lights lanterns on the longest night." - `The Quarry Road` - keys `quarry` - "A rutted track climbs to the abandoned granite quarry." 2. In the lorebook's Vectorize section, turn vectorization on and vectorize all three entries. 3. Open a Game-mode chat and enable `Orchard Valley` for it. Go to Chat Settings -> Function Calling, pick a connection that supports native tool calls (the lore-search switch stays greyed out until you do), then turn on the lore-search switch ("Search enabled, vectorized lore by meaning…"). You can leave the general tools toggle off; this switch attaches the search on its own. 4. Send exactly: `Who runs the docks in this town?` 5. Expand the tool-call card for the lore search in that turn and read the result. **What you'll see:** `"count": 3` - all three entries handed back for a book that says nothing about docks, most of them with `"similarity": 0`. (Don't compare the `query` field in the result to the sentence you typed; it echoes whatever wording the Game Master chose.) **What should happen:** `"count": 0` with an empty result list, so the Game Master is told plainly that nothing in the book matched, instead of being handed the least-irrelevant entries. There is a two-second version with no provider involved: a small script driving the real `resolveGenerationTools` and `executeToolCallForModel` through a deterministic mock embedder reproduces the same result (`count: 3`, scores `0.214 / 0 / 0`). ## Where it is `packages/server/src/services/generation/tool-resolution-runtime.ts`, `searchLorebookForTools` (lines 832-845 at staging `52799569c`): when any entry in scope has a vector, it calls `semanticShortlistLorebookEntries(vectorized, query, { ...lorebookEmbeddingOptions, topK: 20 })` - no minimum similarity is passed - and returns immediately, before the substring branch at :856-871 ever runs. `packages/server/src/services/lorebook/embeddings.ts`, `semanticShortlistLorebookEntries` (:255-270): scores, sorts, `.slice(0, topK)`, then `return matches.length > 0 ? matches : null`. A non-empty list is the only gate. `calibrateLorebookSimilarity` (:114-118) clamps to `[0,1]`, so an unrelated entry lands on exactly `0` and is still returned. `packages/server/src/services/tools/tool-executor.ts:804-806` passes `{ query, category, results, count }` to the model verbatim. ## What's happening The semantic shortlist is a plain top-20 ranker with no notion of "not good enough". Similarity is calibrated against three fixed, mutually unrelated sentences, so genuine noise calibrates to about zero - and zero survives the ranker, because the only thing checked is whether the list is non-empty. Since this branch returns early, the keyword branch that would have produced an empty list never runs; and in Game mode the search is called with vectors required, so today the tool can report "no match" only when it has no vectors at all, never when it has vectors and none of them are relevant. ## Suggested fix Give the shortlist a minimum-relevance argument, and have the lore-search tool return an empty result set when nothing clears it, so the model can distinguish "nothing matched" from "here is the closest thing I have". Alternatively, or in addition, tell the model explicitly in the result when the top row did not beat the unrelated-text baseline. On where the number comes from: the codebase already has two floors to look at - `SEMANTIC_SUMMARY_MIN_SIMILARITY = 0.15` in `roleplay-summary-retrieval.ts` for auto-injected summaries, and the per-lorebook Score Threshold (default 0.3) that automatic activation applies in `keyword-scanner.ts:643-655`. Treat these as precedents for picking a value, not as settings to wire through: #5955 deliberately let this tool reach past the per-turn activation threshold and result cap, and that per-lorebook setting is worded and documented as governing activation, so honouring it verbatim here would work against what the tool was built for. A low baseline-relative floor keeps the feature's reach while letting it say no. Sanity-check the threshold against a real embedding backend rather than assuming the activation default transfers. `knowledge-router.ts:264-280` calls the same floor-less shortlist; the fix should decide explicitly whether the floor applies there too (the consequence is milder, since an LLM then picks from the shortlist). A negative-case regression is cheap: `scripts/regressions/game-tool-controls.regression.ts:300-306` already builds this exact fixture and asserts only the positive direction. ## Context This is how the semantic lore-search path shipped in PR #5982, which implemented issue #5955 (both merged after v2.4.4, so this is unreleased on staging). Neither the issue nor the PR mentions a relevance floor, and no tracker exists for it; related-but-separate floor bugs were handled in #2287 (memory recall) and #3627 (automatic activation). One reading is that a scored top-k list is simply the ordinary shape of a search tool and the model can discount a zero - against that: this is the one feature meant to stop the Game Master inventing lore, nothing in the tool's description tells the model what a similarity of 0 means, and a retrieval tool that never returns nothing teaches the model to treat every row as an answer. A sibling issue about the same function - the search answering only from the vectorized part of a partly-vectorized book - is filed separately; it is a different fix. Found by an AI-assisted sweep of merged PR #5982 against issue #5955, then re-verified against staging 52799569c on 2026-09-13. Please verify before fixing. Related issue from the same sweep: #6124 (the partly-vectorized book case in the same search function).
bsakaguchi 2026-10-21 06:02:44 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (Sep 13, 2026):

@SpicyMarinara is taking this report through the issue sweep in draft PR #6137, with Codex assisting. The affected current-code path has been checked; implementation and focused regression verification are in progress.

<!-- gh-comment-id:5651923510 --> @SpicyMarinara commented on GitHub (Sep 13, 2026): @SpicyMarinara is taking this report through the issue sweep in draft PR #6137, with Codex assisting. The affected current-code path has been checked; implementation and focused regression verification are in progress.
Author
Owner

@SpicyMarinara commented on GitHub (Sep 13, 2026):

Fixed in #6137, now merged to staging (e044997a2). Semantic lore matches must meet a relevance floor calibrated against the real embedding model.

<!-- gh-comment-id:5652783838 --> @SpicyMarinara commented on GitHub (Sep 13, 2026): Fixed in #6137, now merged to staging (`e044997a2`). Semantic lore matches must meet a relevance floor calibrated against the real embedding model.
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#3402
No description provided.