[GH-ISSUE #6570] [Feature]: Let lorebook entries activate on a Decision model statement #3685

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

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

Originally assigned to: @kolacheee on GitHub.

What problem does this solve?

Lorebook entries activate on keywords, on being constant, or on semantic similarity. None of these can tell what is happening from what is mentioned:

  • Passing mentions pull in whole entries. An entry keyed on "dragon" is injected when someone says "I heard a dragon story once", the same as when a dragon lands in the courtyard. Authors work around this with secondary keys, which are brittle, or accept the wasted tokens.
  • Paraphrases miss. A "Blackwood Forest" entry does not fire when the latest message says "they walk under the dark pines north of the village". Semantic matching helps with topic, but it scores similarity, not whether a statement about the scene is true.
  • Situation-only entries have no trigger. "Combat rules", "Rules for night scenes" or "How the city guard reacts to crime" should load only when that situation is happening. Today they are either constant (always paying their tokens) or keyed on words that do not reliably appear.

Since #6569, {{#if decision:"..."}} already works inside an entry's content. But that only trims the text of an entry that has already activated. The entry still counts as active: it uses its share of the token budget, feeds recursion, and starts its sticky and cooldown timers. It cannot decide whether the entry activates at all.

Proposed solution

Give each entry an optional Decision activation setting: a statement, like an agent's activation question, and a mode.

Two modes

  • Require. The entry activates the way it does now (keywords, constant, semantic, recursion), and then also needs the statement to be true. This filters passing mentions: keys dragon, statement "In the latest message, a dragon is physically present". It also makes a constant entry situational: a constant "Combat rules" entry with the statement "A fight is happening in the latest message".
  • Trigger. The statement alone can activate the entry, as another activation source next to keywords and semantic matching. This catches paraphrases and situations: statement "The latest message takes place in the Blackwood Forest", with or without keys.

Mode Off (the default) keeps today's behavior. No answer reads as no, exactly like decision statements in prompts: a Require entry does not activate, and a Trigger entry is not triggered by the statement. With no Decision model, both modes behave as if the statement were false, so a Require entry never fires. The editor must say that plainly (see UI below).

Fields

On the entry (packages/shared/src/schemas/lorebook.schema.ts, createLorebookEntrySchema, and the matching columns in packages/server/src/db/schema/lorebooks.ts):

  • decisionStatement: string, default "", at most 500 characters like an activation question. Macros such as {{char}} and {{user}} work, and a statement naming {{char}} in a group is asked per character, as in prompts.
  • decisionMode: "off" | "require" | "trigger", default "off".

Adding optional columns with defaults has not needed a storage-format bump before (for example sourceMessageRefs, added in 2dde0f6c0 without one). Confirm that when implementing.

How it runs

Entry matching is one synchronous pass: scanForActivatedEntries() in packages/server/src/services/lorebook/keyword-scanner.ts, orchestrated by the async processLorebooks() in packages/server/src/services/lorebook/index.ts. Semantic matching already works around that by computing every embedding in one batch before the pass (buildLorebookSemanticEmbeddingsById() in services/lorebook/embeddings.ts), then only looking results up inside it. Decision activation follows the same shape:

  1. The scanner takes a decisionAnswers map, and reports the entries that would activate but for an unanswered statement. An entry is reported only after its cheap checks pass: enabled, filters, activation conditions, schedule, timing (sticky, cooldown, delay), keywords for Require, and its probability roll. So a statement is never paid for when the entry would be skipped anyway.
  2. processLorebooks() asks the reported statements in one batch, through the same backend, per-turn cache and Decision statements per turn limit as prompt statements (services/decision/prompt-decisions.ts), then runs the pass again with the answers. Probability rolls are already shared across passes (probabilityDecisions), so the second pass sees the same rolls.
  3. Recursion can surface new Require entries. Allow one more ask round for those, then treat anything still unanswered as no. Two rounds keeps the worst case at two decision requests per turn.

Trigger statements do not depend on the scan, so they can join the turn's first decision batch (the one prompt statements already use in generate.routes.ts), leaving the second batch for keyword-matched Require entries.

Other rules:

  • Sticky entries are not asked again while sticky. Cooldown and delay are checked before asking.
  • Once per turn. Answers come from the per-turn cache, so a regeneration or swipe activates the same entries.
  • Cost. Require entries are asked only on turns their keywords match. Trigger entries are asked every turn their lorebook is active and their cheap checks pass. The editor should say so, and the per-turn limit caps both.
  • Activation source. Record "decision" in activationSources, next to keyword, semantic, constant, sticky and recursive, and show it wherever those are shown (for example the lorebook activation list in packages/client/src/components/chat/ChatRoleplayPanels.tsx).
  • Previews never ask. Peek Prompt and dry runs read the cache only, as they do for prompt statements, and report unanswered lorebook statements the same way.
  • Debug logging of each statement and its answer, tagged with the entry name, like prompt statements.

UI

In the entry editor (packages/client/src/components/lorebooks/LorebookEntryRow.tsx, the activation area with the probability and timing fields), add a Decision row: an Off / Require / Trigger control copied from the existing selective-logic buttons, and a statement field. Reuse the existing pieces:

  • DecisionStatementNote for the "no Decision model is set" warning.
  • The activation question's wording advice, linking to "Writing statements" in docs/prompts/conditional-prompts.md.
  • A one-line cost hint for Trigger ("asked every turn while this lorebook is active").

All strings go through the English catalog.

Import and export

  • The native lorebook format carries the two fields.
  • SillyTavern world info and character-book (card V2/V3) exports put them under the entry's extensions so a round trip through Marinara keeps them; SillyTavern itself ignores them.
  • The import notice from #6569 (packages/client/src/lib/decision-import-notice.ts) should also recognise entries with a Decision mode set.

Docs

  • docs/lorebooks/entries.md: a section on Decision activation, with Require and Trigger examples, the cost difference, and the "no Decision model means a Require entry never fires" rule.
  • docs/connections/decision-models.md: add it to "Where Marinara uses it".
  • docs/prompts/conditional-prompts.md: point from the in-content {{#if decision}} section to this, and say when to use which (activation versus trimming an active entry's text).

Alternatives considered

  • Keep using {{#if decision}} inside the content. Works today, but the entry still activates: it takes budget, feeds recursion and starts timers. Fine for trimming text, wrong for deciding activation.
  • A new kind of activationConditions. Conditions today compare game-state fields (evaluateConditions() in keyword-scanner.ts) and pass when there is no game state. A decision is a different thing (asked, cached, cost-limited, can fail), and folding it into that list would hide the cost and the "no answer means no" rule.
  • Semantic matching only. Cheap and local, but it measures topic similarity, not whether a statement about the scene is true. The two complement each other: semantic for "this is about the forest", a decision for "they are in the forest right now".
  • Choice statements (decision_choice) for entries, for example activating one of several location entries from a single "current location" question. Useful later. Yes/no covers the cases above with one field.

Additional context

  • Depends on #6569 (prompt decision statements: the backend, per-turn cache, limit, preview behavior and import notice this reuses).
  • Decision models are optional, and most users will not have one. A lorebook that uses this must still work for them: Trigger entries should also have keys or be non-essential, and Require should filter, never carry, content the story depends on. See "Accuracy: plan for wrong answers" in docs/connections/decision-models.md (added in the #6569 PR).
  • Small decision models sometimes answer wrongly (Open-Jev 2B missed 1 of 32 well-worded statements in our tests), which is another reason Require should only filter.

Suggested slices

  1. Schema, columns, scanner support for decisionAnswers and the pending report, the two-round ask in processLorebooks(), the "decision" activation source, and regressions: Require blocks without a yes, Trigger activates, sticky skips the ask, a failing model reads as no, the limit applies, previews never ask.
  2. Editor UI, import and export in every format, the import notice, and docs.

Implementation owner: @kolacheee.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Sep 23, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6570 Originally assigned to: @kolacheee on GitHub. ## What problem does this solve? Lorebook entries activate on keywords, on being constant, or on semantic similarity. None of these can tell *what is happening* from *what is mentioned*: - **Passing mentions pull in whole entries.** An entry keyed on "dragon" is injected when someone says "I heard a dragon story once", the same as when a dragon lands in the courtyard. Authors work around this with secondary keys, which are brittle, or accept the wasted tokens. - **Paraphrases miss.** A "Blackwood Forest" entry does not fire when the latest message says "they walk under the dark pines north of the village". Semantic matching helps with topic, but it scores similarity, not whether a statement about the scene is true. - **Situation-only entries have no trigger.** "Combat rules", "Rules for night scenes" or "How the city guard reacts to crime" should load only when that situation is happening. Today they are either constant (always paying their tokens) or keyed on words that do not reliably appear. Since #6569, `{{#if decision:"..."}}` already works *inside* an entry's content. But that only trims the text of an entry that has already activated. The entry still counts as active: it uses its share of the token budget, feeds recursion, and starts its sticky and cooldown timers. It cannot decide whether the entry activates at all. ## Proposed solution Give each entry an optional **Decision** activation setting: a statement, like an agent's activation question, and a mode. ### Two modes - **Require.** The entry activates the way it does now (keywords, constant, semantic, recursion), and then *also* needs the statement to be true. This filters passing mentions: keys `dragon`, statement "In the latest message, a dragon is physically present". It also makes a constant entry situational: a constant "Combat rules" entry with the statement "A fight is happening in the latest message". - **Trigger.** The statement alone can activate the entry, as another activation source next to keywords and semantic matching. This catches paraphrases and situations: statement "The latest message takes place in the Blackwood Forest", with or without keys. Mode **Off** (the default) keeps today's behavior. No answer reads as no, exactly like decision statements in prompts: a Require entry does not activate, and a Trigger entry is not triggered by the statement. With no Decision model, both modes behave as if the statement were false, so a Require entry never fires. The editor must say that plainly (see UI below). ### Fields On the entry (`packages/shared/src/schemas/lorebook.schema.ts`, `createLorebookEntrySchema`, and the matching columns in `packages/server/src/db/schema/lorebooks.ts`): - `decisionStatement: string`, default `""`, at most 500 characters like an activation question. Macros such as `{{char}}` and `{{user}}` work, and a statement naming `{{char}}` in a group is asked per character, as in prompts. - `decisionMode: "off" | "require" | "trigger"`, default `"off"`. Adding optional columns with defaults has not needed a storage-format bump before (for example `sourceMessageRefs`, added in 2dde0f6c0 without one). Confirm that when implementing. ### How it runs Entry matching is one synchronous pass: `scanForActivatedEntries()` in `packages/server/src/services/lorebook/keyword-scanner.ts`, orchestrated by the async `processLorebooks()` in `packages/server/src/services/lorebook/index.ts`. Semantic matching already works around that by computing every embedding in one batch *before* the pass (`buildLorebookSemanticEmbeddingsById()` in `services/lorebook/embeddings.ts`), then only looking results up inside it. Decision activation follows the same shape: 1. The scanner takes a `decisionAnswers` map, and reports the entries that *would* activate but for an unanswered statement. An entry is reported only after its cheap checks pass: enabled, filters, activation conditions, schedule, timing (sticky, cooldown, delay), keywords for Require, and its probability roll. So a statement is never paid for when the entry would be skipped anyway. 2. `processLorebooks()` asks the reported statements in one batch, through the same backend, per-turn cache and **Decision statements per turn** limit as prompt statements (`services/decision/prompt-decisions.ts`), then runs the pass again with the answers. Probability rolls are already shared across passes (`probabilityDecisions`), so the second pass sees the same rolls. 3. Recursion can surface new Require entries. Allow one more ask round for those, then treat anything still unanswered as no. Two rounds keeps the worst case at two decision requests per turn. Trigger statements do not depend on the scan, so they can join the turn's first decision batch (the one prompt statements already use in `generate.routes.ts`), leaving the second batch for keyword-matched Require entries. Other rules: - **Sticky** entries are not asked again while sticky. **Cooldown** and **delay** are checked before asking. - **Once per turn.** Answers come from the per-turn cache, so a regeneration or swipe activates the same entries. - **Cost.** Require entries are asked only on turns their keywords match. Trigger entries are asked every turn their lorebook is active and their cheap checks pass. The editor should say so, and the per-turn limit caps both. - **Activation source.** Record `"decision"` in `activationSources`, next to `keyword`, `semantic`, `constant`, `sticky` and `recursive`, and show it wherever those are shown (for example the lorebook activation list in `packages/client/src/components/chat/ChatRoleplayPanels.tsx`). - **Previews never ask.** Peek Prompt and dry runs read the cache only, as they do for prompt statements, and report unanswered lorebook statements the same way. - **Debug logging** of each statement and its answer, tagged with the entry name, like prompt statements. ### UI In the entry editor (`packages/client/src/components/lorebooks/LorebookEntryRow.tsx`, the activation area with the probability and timing fields), add a **Decision** row: an Off / Require / Trigger control copied from the existing selective-logic buttons, and a statement field. Reuse the existing pieces: - `DecisionStatementNote` for the "no Decision model is set" warning. - The activation question's wording advice, linking to "Writing statements" in `docs/prompts/conditional-prompts.md`. - A one-line cost hint for Trigger ("asked every turn while this lorebook is active"). All strings go through the English catalog. ### Import and export - The native lorebook format carries the two fields. - SillyTavern world info and character-book (card V2/V3) exports put them under the entry's `extensions` so a round trip through Marinara keeps them; SillyTavern itself ignores them. - The import notice from #6569 (`packages/client/src/lib/decision-import-notice.ts`) should also recognise entries with a Decision mode set. ### Docs - `docs/lorebooks/entries.md`: a section on Decision activation, with Require and Trigger examples, the cost difference, and the "no Decision model means a Require entry never fires" rule. - `docs/connections/decision-models.md`: add it to "Where Marinara uses it". - `docs/prompts/conditional-prompts.md`: point from the in-content `{{#if decision}}` section to this, and say when to use which (activation versus trimming an active entry's text). ## Alternatives considered - **Keep using `{{#if decision}}` inside the content.** Works today, but the entry still activates: it takes budget, feeds recursion and starts timers. Fine for trimming text, wrong for deciding activation. - **A new kind of `activationConditions`.** Conditions today compare game-state fields (`evaluateConditions()` in `keyword-scanner.ts`) and pass when there is no game state. A decision is a different thing (asked, cached, cost-limited, can fail), and folding it into that list would hide the cost and the "no answer means no" rule. - **Semantic matching only.** Cheap and local, but it measures topic similarity, not whether a statement about the scene is true. The two complement each other: semantic for "this is about the forest", a decision for "they are in the forest right now". - **Choice statements** (`decision_choice`) for entries, for example activating one of several location entries from a single "current location" question. Useful later. Yes/no covers the cases above with one field. ## Additional context - Depends on #6569 (prompt decision statements: the backend, per-turn cache, limit, preview behavior and import notice this reuses). - Decision models are optional, and most users will not have one. A lorebook that uses this must still work for them: Trigger entries should also have keys or be non-essential, and Require should filter, never carry, content the story depends on. See "Accuracy: plan for wrong answers" in `docs/connections/decision-models.md` (added in the #6569 PR). - Small decision models sometimes answer wrongly (Open-Jev 2B missed 1 of 32 well-worded statements in our tests), which is another reason Require should only filter. ### Suggested slices 1. Schema, columns, scanner support for `decisionAnswers` and the pending report, the two-round ask in `processLorebooks()`, the `"decision"` activation source, and regressions: Require blocks without a yes, Trigger activates, sticky skips the ask, a failing model reads as no, the limit applies, previews never ask. 2. Editor UI, import and export in every format, the import notice, and docs. Implementation owner: @kolacheee. ## Template check - [ ] I DID NOT read this template and provide the requested details.
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#3685
No description provided.