[GH-ISSUE #2618] [Issue]: Native .marinara.json lorebook importer does not normalize numeric selectiveLogic/position (ST sibling does) #1292

Closed
opened 2026-10-21 05:30:47 +07:00 by bsakaguchi · 1 comment
Owner

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

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Low — correctness. The native .marinara.json lorebook importer passes selectiveLogic (and position) through verbatim, unlike its ST sibling which normalizes them. A foreign/hand-edited native file carrying a numeric selectiveLogic lands an invalid enum value that the keyword scanner's default: return true treats as "always pass", silently disabling selective secondary-key gating (over-injection of lorebook content).

Expected behavior

selectiveLogic should be normalized to the and/or/not union on import (numeric ST values 0/1/2 mapped), the same way the ST importer does, and position clamped to a valid value — so the keyword scanner gates correctly.

Actual behavior

// packages/server/src/services/import/marinara.importer.ts
selectiveLogic: (e.selectiveLogic as any) ?? "and",   // ~479 — no normalization
position: Number(e.position ?? 0),                     // ~494 — raw Number(); a string like "before_char" yields NaN

The sibling ST importer normalizes via resolveSelectiveLogic() (st-lorebook.importer.ts:398, mapping 0/1/2 -> and/or/not) and resolvePosition() (st-lorebook.importer.ts:380). The DB write does not enforce the enum (lorebooks.storage.ts:525 writes the value with no zod parse; the Drizzle enum is type-only), and read-back (parseEntryRow) does not coerce it. At activation, keyword-scanner.ts:487 → testSecondaryKeys() (packages/shared/src/utils/lorebook-keyword-matching.ts:91) has no case for a numeric value and hits default: return true, so a SELECTIVE entry with secondary keys always passes instead of being gated by AND/OR/NOT. Marinara's own exporter emits the string form, so the standard round-trip is unaffected — this only bites foreign/hand-crafted native files. Fails safe (no crash, no data loss); degrades to over-permissive matching.

Steps to reproduce

  1. Import a marinara_lorebook envelope whose entries[].selectiveLogic is a number (e.g. 1 or 2) on a SELECTIVE entry that has secondaryKeys.
  2. Run a chat so the keyword scanner evaluates that entry.
  3. Observe the entry activates in contexts where AND/OR/NOT gating should have filtered it out.

Environment

  • Marinara Engine version: staging @ 56f700cd (v2.0.0)
  • Install type: source
  • OS + version: n/a — static source audit
  • Browser or app shell: n/a

Logs, screenshots, or video

Static source audit, cited to file:line above.

Additional context

Fix sketch. Route selectiveLogic through the existing resolveSelectiveLogic() normalizer in marinara.importer.ts:479 (the same one st-lorebook.importer.ts:398 uses), and route position through resolvePosition() at marinara.importer.ts:494 to clamp non-numeric values to 0-2. Fix-pattern-already-in-repo: the ST importer is the reference implementation.

Regression notes. Not a regression — main's marinara.importer.ts has the byte-identical selectiveLogic and position lines. Long-standing on both branches. Distinct from #2530 (the ST importer role allowlist — different field, different importer).

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 18, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2618 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Low — correctness.** The native `.marinara.json` lorebook importer passes `selectiveLogic` (and `position`) through verbatim, unlike its ST sibling which normalizes them. A foreign/hand-edited native file carrying a numeric `selectiveLogic` lands an invalid enum value that the keyword scanner's `default: return true` treats as "always pass", silently disabling selective secondary-key gating (over-injection of lorebook content). ## Expected behavior `selectiveLogic` should be normalized to the `and`/`or`/`not` union on import (numeric ST values `0`/`1`/`2` mapped), the same way the ST importer does, and `position` clamped to a valid value — so the keyword scanner gates correctly. ## Actual behavior ```ts // packages/server/src/services/import/marinara.importer.ts selectiveLogic: (e.selectiveLogic as any) ?? "and", // ~479 — no normalization position: Number(e.position ?? 0), // ~494 — raw Number(); a string like "before_char" yields NaN ``` The sibling ST importer normalizes via `resolveSelectiveLogic()` (`st-lorebook.importer.ts:398`, mapping `0/1/2 -> and/or/not`) and `resolvePosition()` (`st-lorebook.importer.ts:380`). The DB write does not enforce the enum (`lorebooks.storage.ts:525` writes the value with no zod parse; the Drizzle enum is type-only), and read-back (`parseEntryRow`) does not coerce it. At activation, `keyword-scanner.ts:487` → `testSecondaryKeys()` (`packages/shared/src/utils/lorebook-keyword-matching.ts:91`) has no case for a numeric value and hits `default: return true`, so a SELECTIVE entry with secondary keys always passes instead of being gated by AND/OR/NOT. Marinara's own exporter emits the string form, so the standard round-trip is unaffected — this only bites foreign/hand-crafted native files. Fails safe (no crash, no data loss); degrades to over-permissive matching. ## Steps to reproduce 1. Import a `marinara_lorebook` envelope whose `entries[].selectiveLogic` is a number (e.g. `1` or `2`) on a SELECTIVE entry that has `secondaryKeys`. 2. Run a chat so the keyword scanner evaluates that entry. 3. Observe the entry activates in contexts where AND/OR/NOT gating should have filtered it out. ## Environment - Marinara Engine version: `staging @ 56f700cd` (v2.0.0) - Install type: source - OS + version: n/a — static source audit - Browser or app shell: n/a ## Logs, screenshots, or video Static source audit, cited to file:line above. ## Additional context **Fix sketch.** Route `selectiveLogic` through the existing `resolveSelectiveLogic()` normalizer in `marinara.importer.ts:479` (the same one `st-lorebook.importer.ts:398` uses), and route `position` through `resolvePosition()` at `marinara.importer.ts:494` to clamp non-numeric values to `0-2`. Fix-pattern-already-in-repo: the ST importer is the reference implementation. **Regression notes.** Not a regression — `main`'s `marinara.importer.ts` has the byte-identical `selectiveLogic` and `position` lines. Long-standing on both branches. Distinct from #2530 (the ST importer role allowlist — different field, different importer). ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:30:47 +07:00
  • closed this issue
  • added the
    server
    bug
    labels
Author
Owner

@Romuromylus commented on GitHub (Jun 18, 2026):

Fixed in #2623, merged to staging (e1132e5b). Closing manually since the PR targeted staging rather than the default branch.

<!-- gh-comment-id:4742296588 --> @Romuromylus commented on GitHub (Jun 18, 2026): Fixed in #2623, merged to `staging` (e1132e5b). Closing manually since the PR targeted `staging` rather than the default branch.
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#1292
No description provided.