[GH-ISSUE #2690] [Issue]: SillyTavern lorebook import mis-maps position, selectiveLogic, recursion flags, order, regex keys & vectorized mode (entries mis-trigger and mis-place) #1337

Closed
opened 2026-10-21 05:31:45 +07:00 by bsakaguchi · 3 comments
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

st-lorebook.importer.ts maps several core SillyTavern World Info fields incorrectly, so an imported ST world book triggers under the wrong conditions and lands in the wrong place in Marinara. This is the most likely cause of widespread "my ST lorebook works in ST but behaves weirdly here" reports. Six distinct field mappings are wrong, all in one importer.

Expected behavior

Importing an ST world book should preserve each entry's triggering and placement semantics (position, selective logic, recursion behavior, vector-only mode, priority order, regex keys) so it behaves the same as in SillyTavern.

Actual behavior (six distinct mismaps)

  1. Position enum collapses. resolvePosition only passes numeric ST positions 0..2 and returns 0 for everything else. ST's enum is 0=before_char, 1=after_char, 2=ANTop, 3=ANBottom, 4=at_depth(@D), 5=EMTop, 6=EMBottom; Marinara has only 0=before, 1=after, 2=at-depth. So ST @D (4) → 0 (before_char) (at-depth lost), ST ANTop (2) → Marinara at-depth, ST 3/5/6 → before_char. At-depth is extremely common in ST books, so they silently land at the top of the prompt with depth/role inert.
  2. selectiveLogic enum inverted. resolveSelectiveLogic uses {0:'and', 1:'or', 2:'not'}, but ST is AND_ANY=0, NOT_ALL=1, NOT_ANY=2, AND_ALL=3. So ST AND_ANY (require any secondary) → and (require all); ST NOT_ALL (suppress when all present) → or (activate when any present — a logical inversion); ST AND_ALL (3) is unrecognized → falls back to and. The selective gate fires under the wrong condition.
  3. Recursion flags merged. ST has two independent flags — prevent_recursion (this entry can't be a recursion source) and exclude_recursion (this entry can't be activated by recursion) — plus delayUntilRecursion. The importer collapses them into one preventRecursion and drops delayUntilRecursion, so recursion activates entries ST would suppress.
  4. vectorized mode dropped. STWorldInfoEntry.vectorized is declared but never applied; the entry keeps its keys, so an ST vector-only entry keyword-triggers here when ST would only match it semantically.
  5. Entry order falls back to uid/id. asNumber(entry.order ?? entry.insertion_order ?? entry.uid ?? entry.id, 100) — when an ST entry has no order (common), it uses the sequential uid (0,1,2,…) as priority, imposing an arbitrary injection order/group tie-break ST never had.
  6. ST /regex/ keys import as literals. ST infers regex from a slash-delimited key (/pattern/flags) at match time; the importer sets useRegex only from a (usually absent) entry.useRegex/entry.regex boolean, so a /dragon(s)?/ key is matched as the literal string and never fires.

How it breaks the pipeline

Code trace (staging @ b826f8f4) — packages/server/src/services/import/st-lorebook.importer.ts:

  • Position: resolvePosition :214-222 (consumed :380/:404).
  • selectiveLogic: resolveSelectiveLogic :208-212 (consumed :398); the round-trip exporter mirrors the wrong table (lorebooks.routes.ts:105, backup.routes.ts:200).
  • Recursion: the merge into preventRecursion; delayUntilRecursion unimplemented. Scanner recursion at keyword-scanner.ts:537-572.
  • Vectorized: :49 declared, :388-421 never applied.
  • Order: :378 (asNumber(... ?? uid ?? id, 100)), consumed :406; priority sort at keyword-scanner.ts:498/529, group tie-break :341.
  • Regex keys: :403 (Boolean(entry.useRegex ?? entry.regex ?? false)); matcher lorebook-keyword-matching.ts testKeyword.

Steps to reproduce

Import an ST world_info JSON with: a numeric position:4 (@D) entry, a selective:true, keysecondary:['x'], selectiveLogic:1 (NOT_ALL) entry, a vectorized:true entry, an entry with only uid and no order, and an entry whose key is '/dragon(s)?/'. Compare placement/triggering to SillyTavern — each diverges as described above.

Suggested fix

  • Position: map the full ST enum explicitly, e.g. {0:0,1:1,2:0,3:1,4:2,5:0,6:1} (at minimum 4→2); decide deliberate targets for AN/EM.
  • selectiveLogic: map 0 AND_ANY→'or', 3 AND_ALL→'and', 1/2 NOT_*→'not' (closest); fix the exporter too. (Full parity needs a 4-mode SelectiveLogic — see the companion scanner issue.)
  • Recursion: import prevent_recursion and exclude_recursion to distinct flags (add the missing "cannot be activated by recursion" field); implement or explicitly skip delayUntilRecursion.
  • Vectorized: honor entry.vectorized (clear keys / add a vector-only flag) instead of dropping it.
  • Order: drop uid/id from the fallback chain → asNumber(entry.order ?? entry.insertion_order, 100).
  • Regex keys: detect /pattern/flags keys on import, strip slashes, set useRegex=true.

Environment

  • Marinara Engine version: 2.0.0 (staging @ b826f8f4)
  • Install type: source
  • OS + version: All (code-level defect, platform-independent)
  • Browser or app shell: All

Additional context

Found via a systematic multi-agent audit of conditional prompt insertion (ST-parity focus). These six mismaps share one file and are the highest-impact lorebook parity win. Related: the scanner-side selectiveLogic mode count and the at-depth placement hoist are filed separately.

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jun 21, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2690 Originally assigned to: @SpicyMarinara on GitHub. ## Summary `st-lorebook.importer.ts` maps several core SillyTavern World Info fields incorrectly, so an imported ST world book **triggers under the wrong conditions and lands in the wrong place** in Marinara. This is the most likely cause of widespread "my ST lorebook works in ST but behaves weirdly here" reports. Six distinct field mappings are wrong, all in one importer. ## Expected behavior Importing an ST world book should preserve each entry's triggering and placement semantics (position, selective logic, recursion behavior, vector-only mode, priority order, regex keys) so it behaves the same as in SillyTavern. ## Actual behavior (six distinct mismaps) 1. **Position enum collapses.** `resolvePosition` only passes numeric ST positions `0..2` and returns `0` for everything else. ST's enum is `0=before_char, 1=after_char, 2=ANTop, 3=ANBottom, 4=at_depth(@D), 5=EMTop, 6=EMBottom`; Marinara has only `0=before, 1=after, 2=at-depth`. So ST `@D` (4) → `0 (before_char)` (at-depth lost), ST ANTop (2) → Marinara at-depth, ST 3/5/6 → before_char. **At-depth is extremely common in ST books**, so they silently land at the top of the prompt with depth/role inert. 2. **`selectiveLogic` enum inverted.** `resolveSelectiveLogic` uses `{0:'and', 1:'or', 2:'not'}`, but ST is `AND_ANY=0, NOT_ALL=1, NOT_ANY=2, AND_ALL=3`. So ST `AND_ANY` (require *any* secondary) → `and` (require *all*); ST `NOT_ALL` (suppress when all present) → `or` (activate when any present — a logical inversion); ST `AND_ALL` (3) is unrecognized → falls back to `and`. The selective gate fires under the wrong condition. 3. **Recursion flags merged.** ST has two independent flags — `prevent_recursion` (this entry can't be a recursion *source*) and `exclude_recursion` (this entry can't be *activated by* recursion) — plus `delayUntilRecursion`. The importer collapses them into one `preventRecursion` and drops `delayUntilRecursion`, so recursion activates entries ST would suppress. 4. **`vectorized` mode dropped.** `STWorldInfoEntry.vectorized` is declared but never applied; the entry keeps its keys, so an ST vector-only entry **keyword-triggers** here when ST would only match it semantically. 5. **Entry `order` falls back to `uid`/`id`.** `asNumber(entry.order ?? entry.insertion_order ?? entry.uid ?? entry.id, 100)` — when an ST entry has no `order` (common), it uses the sequential `uid` (0,1,2,…) as priority, imposing an arbitrary injection order/group tie-break ST never had. 6. **ST `/regex/` keys import as literals.** ST infers regex from a slash-delimited key (`/pattern/flags`) at match time; the importer sets `useRegex` only from a (usually absent) `entry.useRegex`/`entry.regex` boolean, so a `/dragon(s)?/` key is matched as the literal string and never fires. ## How it breaks the pipeline Code trace (staging @ `b826f8f4`) — `packages/server/src/services/import/st-lorebook.importer.ts`: - Position: `resolvePosition` `:214-222` (consumed `:380`/`:404`). - selectiveLogic: `resolveSelectiveLogic` `:208-212` (consumed `:398`); the round-trip **exporter** mirrors the wrong table (`lorebooks.routes.ts:105`, `backup.routes.ts:200`). - Recursion: the merge into `preventRecursion`; `delayUntilRecursion` unimplemented. Scanner recursion at `keyword-scanner.ts:537-572`. - Vectorized: `:49` declared, `:388-421` never applied. - Order: `:378` (`asNumber(... ?? uid ?? id, 100)`), consumed `:406`; priority sort at `keyword-scanner.ts:498/529`, group tie-break `:341`. - Regex keys: `:403` (`Boolean(entry.useRegex ?? entry.regex ?? false)`); matcher `lorebook-keyword-matching.ts` `testKeyword`. ## Steps to reproduce Import an ST `world_info` JSON with: a numeric `position:4` (@D) entry, a `selective:true, keysecondary:['x'], selectiveLogic:1` (NOT_ALL) entry, a `vectorized:true` entry, an entry with only `uid` and no `order`, and an entry whose key is `'/dragon(s)?/'`. Compare placement/triggering to SillyTavern — each diverges as described above. ## Suggested fix - Position: map the full ST enum explicitly, e.g. `{0:0,1:1,2:0,3:1,4:2,5:0,6:1}` (at minimum `4→2`); decide deliberate targets for AN/EM. - selectiveLogic: map `0 AND_ANY→'or'`, `3 AND_ALL→'and'`, `1/2 NOT_*→'not'` (closest); fix the exporter too. (Full parity needs a 4-mode SelectiveLogic — see the companion scanner issue.) - Recursion: import `prevent_recursion` and `exclude_recursion` to distinct flags (add the missing "cannot be activated by recursion" field); implement or explicitly skip `delayUntilRecursion`. - Vectorized: honor `entry.vectorized` (clear keys / add a vector-only flag) instead of dropping it. - Order: drop `uid`/`id` from the fallback chain → `asNumber(entry.order ?? entry.insertion_order, 100)`. - Regex keys: detect `/pattern/flags` keys on import, strip slashes, set `useRegex=true`. ## Environment - Marinara Engine version: 2.0.0 (staging @ `b826f8f4`) - Install type: source - OS + version: All (code-level defect, platform-independent) - Browser or app shell: All ## Additional context Found via a systematic multi-agent audit of conditional prompt insertion (ST-parity focus). These six mismaps share one file and are the highest-impact lorebook parity win. Related: the scanner-side `selectiveLogic` mode count and the at-depth placement hoist are filed separately. ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
Author
Owner

@kolacheee commented on GitHub (Jun 21, 2026):

Veracity self-review — correction / clarification
Automated post-filing review of this issue against the current working tree. The reported bug remains valid; the notes below refine its scope, accuracy, or suggested fix for whoever implements it.

Four clarifications for accuracy/actionability (the six core mismaps are all confirmed present at the cited lines on the current tree):

  1. Regex-keys fix (Claim #6) is unsafe as written. useRegex is a single per-ENTRY boolean: the scanner sets matchOptions.useRegex = entry.useRegex (keyword-scanner.ts:473) and testKeyword applies it to EVERY primary and secondary key (lorebook-keyword-matching.ts:36-46). ST instead infers regex per-KEY at match time. Blindly setting useRegex=true because one /pattern/ key is present forces the entry's other literal keys through new RegExp(literalKey) — literal keys containing regex metacharacters (e.g. (parens), a+b, $5) then match differently or throw and silently fall back to substring. Recommend either (a) per-key handling: strip ^/(.+)/([gimsuy]*)$ into the stored key and only set entry-level useRegex=true when it is safe for the remaining keys (e.g. by escaping the literals), or (b) explicitly document that mixed literal+regex entries are partially lossy until per-key regex support exists.

  2. Field-name precision (Claim #3): ST's exported world_info JSON uses camelCase preventRecursion / excludeRecursion / delayUntilRecursion, NOT snake_case prevent_recursion/exclude_recursion. The importer already reads the camelCase names (st-lorebook.importer.ts:419: entry.preventRecursion ?? entry.excludeRecursion); a fixer adding the missing flag must keep reading camelCase. The real defect stands: the two distinct ST flags collapse into one, and delayUntilRecursion is referenced nowhere in the importer. Marinara's schema has only one recursion flag (preventRecursion, lorebook.ts:193), which gates recursion SOURCES at keyword-scanner.ts:550 — there is no "cannot be activated by recursion" concept, so the fix needs a new schema column plus a scanner change, not just an import remap.

  3. Frequency note (Claim #5): ST exports almost always include order (default 100; Marinara's own compatible exporter always writes order too), so the ?? uid ?? id fallback (st-lorebook.importer.ts:378) fires only for entries genuinely missing order. The fix is correct, but impact is lower-frequency than "common."

  4. Scope: the position round-trip is broken symmetrically, same as the selectiveLogic exporter you already flagged. Both compatible exporters write position raw — lorebooks.routes.ts:144 and backup.routes.ts:221 (position: Number(entry.position ?? 0)) — so Marinara at-depth (2) exports as ST ANTop (2). Fix the exporters' position mapping alongside the importer's. (lorebooks.routes.ts:186/199 are the internal Marinara→Marinara transfer helper buildTransferredEntryInput, not an exporter, and are out of scope.)

<!-- gh-comment-id:4761102225 --> @kolacheee commented on GitHub (Jun 21, 2026): > **Veracity self-review — correction / clarification** > _Automated post-filing review of this issue against the current working tree. The reported bug remains valid; the notes below refine its scope, accuracy, or suggested fix for whoever implements it._ Four clarifications for accuracy/actionability (the six core mismaps are all confirmed present at the cited lines on the current tree): 1. Regex-keys fix (Claim #6) is unsafe as written. `useRegex` is a single per-ENTRY boolean: the scanner sets `matchOptions.useRegex = entry.useRegex` (keyword-scanner.ts:473) and `testKeyword` applies it to EVERY primary and secondary key (lorebook-keyword-matching.ts:36-46). ST instead infers regex per-KEY at match time. Blindly setting `useRegex=true` because one `/pattern/` key is present forces the entry's other literal keys through `new RegExp(literalKey)` — literal keys containing regex metacharacters (e.g. `(parens)`, `a+b`, `$5`) then match differently or throw and silently fall back to substring. Recommend either (a) per-key handling: strip `^/(.+)/([gimsuy]*)$` into the stored key and only set entry-level `useRegex=true` when it is safe for the remaining keys (e.g. by escaping the literals), or (b) explicitly document that mixed literal+regex entries are partially lossy until per-key regex support exists. 2. Field-name precision (Claim #3): ST's exported world_info JSON uses camelCase `preventRecursion` / `excludeRecursion` / `delayUntilRecursion`, NOT snake_case `prevent_recursion`/`exclude_recursion`. The importer already reads the camelCase names (st-lorebook.importer.ts:419: `entry.preventRecursion ?? entry.excludeRecursion`); a fixer adding the missing flag must keep reading camelCase. The real defect stands: the two distinct ST flags collapse into one, and `delayUntilRecursion` is referenced nowhere in the importer. Marinara's schema has only one recursion flag (`preventRecursion`, lorebook.ts:193), which gates recursion SOURCES at keyword-scanner.ts:550 — there is no "cannot be activated by recursion" concept, so the fix needs a new schema column plus a scanner change, not just an import remap. 3. Frequency note (Claim #5): ST exports almost always include `order` (default 100; Marinara's own compatible exporter always writes `order` too), so the `?? uid ?? id` fallback (st-lorebook.importer.ts:378) fires only for entries genuinely missing `order`. The fix is correct, but impact is lower-frequency than "common." 4. Scope: the position round-trip is broken symmetrically, same as the selectiveLogic exporter you already flagged. Both compatible exporters write position raw — lorebooks.routes.ts:144 and backup.routes.ts:221 (`position: Number(entry.position ?? 0)`) — so Marinara at-depth (2) exports as ST ANTop (2). Fix the exporters' position mapping alongside the importer's. (lorebooks.routes.ts:186/199 are the internal Marinara→Marinara transfer helper `buildTransferredEntryInput`, not an exporter, and are out of scope.)
Author
Owner

@SpicyMarinara commented on GitHub (Jun 21, 2026):

Fixed in branch codex/spicy-issue-sweep-2026-06-21.

What changed:

  • Corrected ST import mappings for position, selective logic, order fallback, vectorized-only entries, slash-delimited regex keys, and recursion flags.
  • Added storage/schema/migration/scanner support for excludeRecursion and delayUntilRecursion, so the imported ST recursion semantics are actually enforced.
  • Corrected ST-compatible lorebook/profile exports for selective logic and at-depth position, and exports the recursion flags.
  • Kept mixed slash-regex/literal entries safe by escaping literal keys when entry-level regex has to represent mixed ST per-key regex behavior.
  • Kept native Marinara import on native position/selective semantics so this ST fix does not break native backups.

Final repo checks will run after the full issue sweep.

<!-- gh-comment-id:4761517136 --> @SpicyMarinara commented on GitHub (Jun 21, 2026): Fixed in branch `codex/spicy-issue-sweep-2026-06-21`. What changed: - Corrected ST import mappings for position, selective logic, order fallback, vectorized-only entries, slash-delimited regex keys, and recursion flags. - Added storage/schema/migration/scanner support for `excludeRecursion` and `delayUntilRecursion`, so the imported ST recursion semantics are actually enforced. - Corrected ST-compatible lorebook/profile exports for selective logic and at-depth position, and exports the recursion flags. - Kept mixed slash-regex/literal entries safe by escaping literal keys when entry-level regex has to represent mixed ST per-key regex behavior. - Kept native Marinara import on native position/selective semantics so this ST fix does not break native backups. Final repo checks will run after the full issue sweep.
Author
Owner

@kolacheee commented on GitHub (Jun 21, 2026):

Reopening — partially fixed by #2730. A post-merge verification against the merged staging branch confirms the main fix landed, but a cited part of this issue remains unfixed. Residual below, verified by direct read at file:line.

Claim #4 (vectorized mode) is not fixed: an ST vectorized:true (vector/semantic-only) entry still keyword-triggers in the merged worktree. st-lorebook.importer.ts:481 maps ST vectorized onto excludeFromVectorization, the wrong axis (embedding eligibility, not triggering mode); for vectorized:true it yields excludeFromVectorization=false. Keys are preserved unconditionally (importer :434/:452, never cleared) and the scanner's main keyword loop (keyword-scanner.ts:469-532) never checks vectorization, so the entry keyword-triggers. excludeFromVectorization only gates the semantic fallback (keyword-scanner.ts:540). No vector-only/semantic-only flag exists anywhere. Notably this regressed: PR #2730's original commit 3118174b correctly cleared keys for vector-only entries, but follow-up commit e6a967f8 reverted that. Suggested fix: restore the key-clearing (vectorizedOnly => keys=[]) or add a true vector-only/semantic-only entry flag and gate the main keyword loop on it. The other 5 core mismaps and all 4 veracity refinements are correctly fixed.

<!-- gh-comment-id:4762835057 --> @kolacheee commented on GitHub (Jun 21, 2026): > **Reopening — partially fixed by #2730.** A post-merge verification against the merged staging branch confirms the main fix landed, but a cited part of this issue remains unfixed. Residual below, verified by direct read at file:line. Claim #4 (vectorized mode) is not fixed: an ST `vectorized:true` (vector/semantic-only) entry still keyword-triggers in the merged worktree. st-lorebook.importer.ts:481 maps ST `vectorized` onto `excludeFromVectorization`, the wrong axis (embedding eligibility, not triggering mode); for `vectorized:true` it yields `excludeFromVectorization=false`. Keys are preserved unconditionally (importer :434/:452, never cleared) and the scanner's main keyword loop (keyword-scanner.ts:469-532) never checks vectorization, so the entry keyword-triggers. `excludeFromVectorization` only gates the semantic fallback (keyword-scanner.ts:540). No vector-only/semantic-only flag exists anywhere. Notably this regressed: PR #2730's original commit 3118174b correctly cleared keys for vector-only entries, but follow-up commit e6a967f8 reverted that. Suggested fix: restore the key-clearing (vectorizedOnly => keys=[]) or add a true vector-only/semantic-only entry flag and gate the main keyword loop on it. The other 5 core mismaps and all 4 veracity refinements are correctly fixed.
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#1337
No description provided.