[GH-ISSUE #5955] Let the GM search the lorebook for what a scene needs (and make that search semantic) #3297

Closed
opened 2026-10-21 06:01:46 +07:00 by bsakaguchi · 1 comment
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

Ask: the GM should be able to look things up in the lorebook — semantically, the way Professor Mari's [fetch:] does — before it writes narration, so it can pull in the entries the scene actually needs. Behind a toggle.

Reframe from recon: the tool already exists and already runs in Game Mode. search_lorebook is a shipped built-in, in no mode-restriction list, default-on whenever "Enable Tool Use" is on. Two things are genuinely missing.

1. It isn't semantic — it's substring matching. searchLorebookForTools (services/generation/tool-resolution-runtime.ts:807-833) lowercases and calls .includes() on name, content and keys, capped at 20 results. A GM asking for "the vampire guy" or "who runs the docks" gets nothing back unless those literal characters appear in an entry. Meanwhile the calibrated-cosine ranker already exists and is used elsewhere — semanticShortlistLorebookEntries (services/lorebook/embeddings.ts:222-271), which subtracts a measured baseline so thresholds mean something across embedding backends. Routing the tool through it is the small, self-contained half of this issue, and it's worth doing on its own merits: the substring behavior is a live shortcoming in every mode, today, not just Game.

(Adjacent, smaller: GET /search/entries is documented as searching "name/content/keys" but its implementation is like(name, …) only — services/storage/lorebooks.storage.ts:1359-1367.)

Prerequisite worth stating: semantic search over a book does nothing until that book has been vectorized (POST /lorebooks/:id/vectorize). Nothing vectorizes automatically.

2. Getting it today means arming everything. "Enable Tool Use" turns on the whole default-on tool set. #5924 added exactly the channel this needs — GAME_MODE_AUTO_ATTACH_TOOL_NAMES with its gate at generate.routes.ts:4953 — so search_lorebook could ride along on GM turns behind its own switch, following the gameUseMusicDj metadata-flag precedent (generate.routes.ts:1519-1524).

On "can it ride the thinking process?" — no, and not because of our plumbing. On both providers the model reasons, emits the call, and that ends the message; the result comes back in the next request. Anthropic puts thinking and tool_use in the same assistant message and takes the result as a tool_result in a fresh request (anthropic.provider.ts:594-612, :258-272); interleaved thinking isn't enabled here. Gemini carries reasoning state forward via thought signatures (google.provider.ts:793-801, :866-872) but still only into the next request — and Gemini with thinking on doesn't stream at all (:592, :853), so a lookup there means two buffered calls with no token flow. The honest framing is: the GM reasons, decides it needs lore, calls, and the result informs a second pass — which is where the narration gets written.

What this adds over what already runs, stated honestly. A keyword and semantic lorebook scan already happens automatically every turn (keyword-scanner.ts:607-690). But it embeds recent player messages (embeddings.ts:28-42), so the GM cannot make it search for something the player didn't mention. The tool adds: a query the GM authors, the ability to ask again with a better query, and reach past the per-turn threshold and result cap. That's the real gain — not "semantic search in Game Mode," which exists.

Costs to put in front of the user at the toggle: one extra full model round per call (the whole prompt is re-sent, so input tokens are paid again); Gemini-with-thinking loses streaming; and Claude/Grok subscription connections ignore tools entirely (grok-subscription.provider.ts:336; the Claude subscription provider implements no chatComplete), so it must degrade silently rather than appear broken for those users.

One alternative worth weighing before building the tool. knowledge-router (services/agents/knowledge-router.ts) already does exactly this shape as a pre-generation pass — it seeds candidates from the same semantic ranker, lets a model pick which entries are relevant, and injects them verbatim, with no mid-turn round trip. It's blocked in Game Mode by two allowlists (chat-mode-agent-policy.ts:17, plus knowledge-retrieval's own roleplay-only manifest), and unblocking is a small edit. Trade-off: the router is cheaper and doesn't interrupt the turn, but the GM can't ask a follow-up question mid-narration. The tool is iterative but costs a round each time.

Suggested order regardless of which shape wins: make search_lorebook semantic first (small, useful everywhere), then decide tool-vs-router for the Game Mode surfacing.

Originally created by @kolacheee on GitHub (Sep 8, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5955 Originally assigned to: @SpicyMarinara on GitHub. Ask: the GM should be able to look things up in the lorebook — semantically, the way Professor Mari's `[fetch:]` does — before it writes narration, so it can pull in the entries the scene actually needs. Behind a toggle. **Reframe from recon: the tool already exists and already runs in Game Mode.** `search_lorebook` is a shipped built-in, in no mode-restriction list, default-on whenever "Enable Tool Use" is on. Two things are genuinely missing. **1. It isn't semantic — it's substring matching.** `searchLorebookForTools` (`services/generation/tool-resolution-runtime.ts:807-833`) lowercases and calls `.includes()` on name, content and keys, capped at 20 results. A GM asking for "the vampire guy" or "who runs the docks" gets nothing back unless those literal characters appear in an entry. Meanwhile the calibrated-cosine ranker already exists and is used elsewhere — `semanticShortlistLorebookEntries` (`services/lorebook/embeddings.ts:222-271`), which subtracts a measured baseline so thresholds mean something across embedding backends. Routing the tool through it is the small, self-contained half of this issue, and it's worth doing on its own merits: the substring behavior is a live shortcoming in **every** mode, today, not just Game. (Adjacent, smaller: `GET /search/entries` is documented as searching "name/content/keys" but its implementation is `like(name, …)` only — `services/storage/lorebooks.storage.ts:1359-1367`.) Prerequisite worth stating: semantic search over a book does nothing until that book has been vectorized (`POST /lorebooks/:id/vectorize`). Nothing vectorizes automatically. **2. Getting it today means arming everything.** "Enable Tool Use" turns on the whole default-on tool set. #5924 added exactly the channel this needs — `GAME_MODE_AUTO_ATTACH_TOOL_NAMES` with its gate at `generate.routes.ts:4953` — so `search_lorebook` could ride along on GM turns behind its own switch, following the `gameUseMusicDj` metadata-flag precedent (`generate.routes.ts:1519-1524`). **On "can it ride the thinking process?" — no, and not because of our plumbing.** On both providers the model reasons, emits the call, and *that ends the message*; the result comes back in the next request. Anthropic puts thinking and `tool_use` in the same assistant message and takes the result as a `tool_result` in a fresh request (`anthropic.provider.ts:594-612`, `:258-272`); interleaved thinking isn't enabled here. Gemini carries reasoning state forward via thought signatures (`google.provider.ts:793-801`, `:866-872`) but still only into the next request — and Gemini with thinking on doesn't stream at all (`:592`, `:853`), so a lookup there means two buffered calls with no token flow. The honest framing is: the GM reasons, decides it needs lore, calls, and the result informs a second pass — which is where the narration gets written. **What this adds over what already runs, stated honestly.** A keyword *and semantic* lorebook scan already happens automatically every turn (`keyword-scanner.ts:607-690`). But it embeds *recent player messages* (`embeddings.ts:28-42`), so the GM cannot make it search for something the player didn't mention. The tool adds: a query the GM authors, the ability to ask again with a better query, and reach past the per-turn threshold and result cap. That's the real gain — not "semantic search in Game Mode," which exists. **Costs to put in front of the user at the toggle:** one extra full model round per call (the whole prompt is re-sent, so input tokens are paid again); Gemini-with-thinking loses streaming; and Claude/Grok *subscription* connections ignore tools entirely (`grok-subscription.provider.ts:336`; the Claude subscription provider implements no `chatComplete`), so it must degrade silently rather than appear broken for those users. **One alternative worth weighing before building the tool.** `knowledge-router` (`services/agents/knowledge-router.ts`) already does exactly this shape as a **pre-generation** pass — it seeds candidates from the same semantic ranker, lets a model pick which entries are relevant, and injects them verbatim, with no mid-turn round trip. It's blocked in Game Mode by two allowlists (`chat-mode-agent-policy.ts:17`, plus `knowledge-retrieval`'s own roleplay-only manifest), and unblocking is a small edit. Trade-off: the router is cheaper and doesn't interrupt the turn, but the GM can't ask a follow-up question mid-narration. The tool is iterative but costs a round each time. Suggested order regardless of which shape wins: make `search_lorebook` semantic first (small, useful everywhere), then decide tool-vs-router for the Game Mode surfacing.
bsakaguchi 2026-10-21 06:01:46 +07:00
  • closed this issue
  • added the
    in-pr
    label
Author
Owner

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

@SpicyMarinara owns this sweep implementation in draft #5982. I am reusing the calibrated semantic lorebook shortlist and existing Game tool attachment path, with a separate opt-in and no automatic vectorization.

<!-- gh-comment-id:5594005666 --> @SpicyMarinara commented on GitHub (Sep 9, 2026): @SpicyMarinara owns this sweep implementation in draft #5982. I am reusing the calibrated semantic lorebook shortlist and existing Game tool attachment path, with a separate opt-in and no automatic vectorization.
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#3297
No description provided.