[GH-ISSUE #226] [Feature]: Add knowledge-router agent: lower-cost alternative to knowledge-retrieval #87

Closed
opened 2026-10-20 17:24:59 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @cha1latte on GitHub (Apr 25, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/226

Background

In Discord, a user proposed a leaner alternative to the existing knowledge-retrieval agent. SpicyMarinara replied:

"This is a potential for a separate agent or an extension, but as of now, I am not going to bother. If anyone makes this, I will add to the main branch."

(link to Discord thread)

I'd like to take this on. Opening this issue as a tracking hub for the design and the PRs that will land it.

Problem

The current knowledge-retrieval agent has two costs that grow with lorebook size:

  1. Redundant context. Entries marked constant: true are already injected by standard lorebook activation, but they also get re-summarized by the retrieval agent. Same content, two copies, two token bills.
  2. Full extraction pass. All enabled entries from the configured source lorebooks are fed into an LLM call that summarizes them. For large lorebooks (One Piece, Wheel of Time, sprawling worldbuilding), this gets chunked into multiple passes — multiplying tokens and latency before main generation has even started.

Proposed solution

A new agent type knowledge-router that routes to entries instead of extracting from them.

Pipeline:

  1. Build a compact catalog from the configured source lorebooks: one line per entry containing name + description. Skip constant: true entries (already in context).
  2. Send one LLM call: chat history + catalog + system prompt asking for a JSON array of relevant entry IDs.
  3. Load those entries' full content from the DB and inject verbatim as a context block — same shape as standard lorebook activation, no LLM-generated summary in between.
  4. Return an AgentResult of type context_injection.

Both agents coexist. Users who prefer the current behavior keep it. The agent settings UI will warn if both are enabled in the same pipeline.

Schema change

Lorebook entries don't currently have a description field. Adding one:

  • New column description: text on lorebookEntries, default empty string.
  • Migration handles existing databases (no data migration needed; empty descriptions fall back to entry name + first key when building the catalog).
  • Entry editor in the client gets a small textarea with a placeholder explaining the field's purpose.

Implementation plan

Splitting into three reviewable PRs that land in order:

# Scope Depends on
1 Schema + migration + entry editor textarea for description —
2 knowledge-router agent: prompt, type registration, server wiring, tests, benchmark PR 1
3 Agent settings panel + "both agents running" warning + description-coverage badge PR 2

Each PR will reference this issue.

Out of scope

  • Knowledge-graph / linked-entry exploration (different feature, different thread).
  • The existing embedding column on entries (separate purpose, not repurposed here).
  • Auto-generating descriptions for existing entries (could be a future "lorebook description backfill" agent).
  • Modifying knowledge-retrieval itself.

Design decisions

Empty descriptions: fall back to name + first 1-2 keys, don't skip.
Skipping silently makes entries invisible to the router — a footgun users won't notice until a scene breaks. Names + keys usually carry enough signal ("Luffy" + keys luffy, monkey d luffy, straw hat tells the LLM what it is). The description-coverage badge in PR 3 nudges users toward filling them in over time.

Recommended model: pinned capable model, soft warning on auto-model.
The agent emits strict JSON, and auto-model routes opaquely (currently to Gemini Pro, but that can change). The settings panel defaults the agent's model picker to a pinned recommendation (Claude Sonnet+, GPT-4 class, Gemini Pro) and shows a soft warning — not a block — if the user picks auto-model. Mirrors how other strict-JSON agents in the codebase are already handled.

Description field is opt-in, not required. The router catalog uses entry.description when filled in, and falls back to the first ~60 tokens of entry.content (then name + keys) when blank. This keeps the router useful out of the box for casual users while letting power users tune precision by writing tight summaries.

Benchmarks: split between CI and manual.

  • CI (unit tests): mock the provider, verify catalog construction, ID parsing, fallback when descriptions are empty, graceful handling of malformed JSON / unknown IDs. Behavior, not numbers.
  • Manual (benchmark script): real provider, run by the contributor, captures the comparison table (tokens-sent, tokens-received, wall-clock latency, cost) for N = 20 / 100 / 500. Results paste into the PR description.

This keeps CI fast and deterministic while still producing the data that turns a claim into evidence.

Originally created by @cha1latte on GitHub (Apr 25, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/226 ## Background In Discord, a user proposed a leaner alternative to the existing `knowledge-retrieval` agent. SpicyMarinara replied: > "This is a potential for a separate agent or an extension, but as of now, I am not going to bother. If anyone makes this, I will add to the main branch." *[(link to Discord thread)](https://discord.com/channels/1417099416812392641/1491088609950957669)* I'd like to take this on. Opening this issue as a tracking hub for the design and the PRs that will land it. ## Problem The current `knowledge-retrieval` agent has two costs that grow with lorebook size: 1. **Redundant context.** Entries marked `constant: true` are already injected by standard lorebook activation, but they also get re-summarized by the retrieval agent. Same content, two copies, two token bills. 2. **Full extraction pass.** All enabled entries from the configured source lorebooks are fed into an LLM call that summarizes them. For large lorebooks (One Piece, Wheel of Time, sprawling worldbuilding), this gets chunked into multiple passes — multiplying tokens and latency before main generation has even started. ## Proposed solution A new agent type `knowledge-router` that *routes to* entries instead of *extracting from* them. Pipeline: 1. Build a compact catalog from the configured source lorebooks: one line per entry containing `name + description`. Skip `constant: true` entries (already in context). 2. Send one LLM call: chat history + catalog + system prompt asking for a JSON array of relevant entry IDs. 3. Load those entries' full content from the DB and inject verbatim as a context block — same shape as standard lorebook activation, no LLM-generated summary in between. 4. Return an `AgentResult` of type `context_injection`. Both agents coexist. Users who prefer the current behavior keep it. The agent settings UI will warn if both are enabled in the same pipeline. ## Schema change Lorebook entries don't currently have a description field. Adding one: - New column `description: text` on `lorebookEntries`, default empty string. - Migration handles existing databases (no data migration needed; empty descriptions fall back to entry name + first key when building the catalog). - Entry editor in the client gets a small textarea with a placeholder explaining the field's purpose. ## Implementation plan Splitting into three reviewable PRs that land in order: | # | Scope | Depends on | |---|---|---| | 1 | Schema + migration + entry editor textarea for `description` | — | | 2 | `knowledge-router` agent: prompt, type registration, server wiring, tests, benchmark | PR 1 | | 3 | Agent settings panel + "both agents running" warning + description-coverage badge | PR 2 | Each PR will reference this issue. ## Out of scope - Knowledge-graph / linked-entry exploration (different feature, different thread). - The existing `embedding` column on entries (separate purpose, not repurposed here). - Auto-generating descriptions for existing entries (could be a future "lorebook description backfill" agent). - Modifying `knowledge-retrieval` itself. ## Design decisions **Empty descriptions: fall back to `name + first 1-2 keys`, don't skip.** Skipping silently makes entries invisible to the router — a footgun users won't notice until a scene breaks. Names + keys usually carry enough signal ("Luffy" + keys `luffy, monkey d luffy, straw hat` tells the LLM what it is). The description-coverage badge in PR 3 nudges users toward filling them in over time. **Recommended model: pinned capable model, soft warning on `auto-model`.** The agent emits strict JSON, and `auto-model` routes opaquely (currently to Gemini Pro, but that can change). The settings panel defaults the agent's model picker to a pinned recommendation (Claude Sonnet+, GPT-4 class, Gemini Pro) and shows a soft warning — not a block — if the user picks `auto-model`. Mirrors how other strict-JSON agents in the codebase are already handled. **Description field is opt-in, not required.** The router catalog uses `entry.description` when filled in, and falls back to the first ~60 tokens of `entry.content` (then name + keys) when blank. This keeps the router useful out of the box for casual users while letting power users tune precision by writing tight summaries. **Benchmarks: split between CI and manual.** - **CI (unit tests):** mock the provider, verify catalog construction, ID parsing, fallback when descriptions are empty, graceful handling of malformed JSON / unknown IDs. Behavior, not numbers. - **Manual (benchmark script):** real provider, run by the contributor, captures the comparison table (tokens-sent, tokens-received, wall-clock latency, cost) for N = 20 / 100 / 500. Results paste into the PR description. This keeps CI fast and deterministic while still producing the data that turns a claim into evidence.
bsakaguchi 2026-10-20 17:24:59 +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#87
No description provided.