[PR #2538] [MERGED] fix(import): clamp Marinara lorebook entry role to the system/user/assistant union #5642

Closed
opened 2026-10-22 04:18:04 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2538
Author: @Romuromylus
Created: 6/9/2026
Status: ✅ Merged
Merged: 6/9/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/lorebook-import-role-allowlist


📝 Commits (2)

  • 4c33dd3 fix(import): clamp Marinara lorebook entry role to the system/user/assistant union
  • 7c26135 Merge branch 'staging' into fix/lorebook-import-role-allowlist

📊 Changes

3 files changed (+23 additions, -12 deletions)

View changed files

➕ packages/server/src/services/import/lorebook-role.ts (+19 -0)
📝 packages/server/src/services/import/marinara.importer.ts (+2 -1)
📝 packages/server/src/services/import/st-lorebook.importer.ts (+2 -11)

📄 Description

Linked issue

Closes #2530

Why this change

The Marinara native importer wrote the lorebook entry role verbatim:

role: (e.role as any) ?? "system",

The bulk-import path (bulkCreateEntries → createEntry) inserts role with no zod parse, so an arbitrary string from a hand-crafted .marinara.json persisted as the entry role and later propagated into a provider PromptMessage role. The ST lorebook importer already normalizes the identical field via resolveRole, and the manual entry-create routes run createLorebookEntrySchema.parse() (which clamps to z.enum(["system","user","assistant"])). The Marinara bulk path was the one site that skipped the clamp.

What changed

  • Extracted the ST importer's local resolveRole into a shared resolveLorebookEntryRole helper (services/import/lorebook-role.ts), with the parameter widened to unknown so the Marinara importer (where e.role is unknown) can call it without a cast. The body already narrows safely, so this is behavior-preserving for the ST path.
  • Re-pointed the ST importer's single call site to the shared helper and removed the now-duplicated local definition.
  • Replaced the unclamped assignment in marinara.importer.ts with resolveLorebookEntryRole(e.role).

Legitimate roles ("system"/"user"/"assistant" and ST numeric 0/1/2) round-trip identically; only out-of-enum garbage now falls back to "system".

Out of scope (noted for transparency): marinara.importer.ts has a second role: (s.role as any) ?? "system" at the preset-section import path (createSection). That is a different entity with its own storage/validation path, not a lorebook entry role, so it's left untouched here.

Not a regression: main has the byte-identical unclamped assignment; long-standing shared-code bug.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

pnpm check passes locally (confirms no dangling resolveRole reference after the extraction). Added a local-only node:test (lorebook-role.test.ts, gitignored): clamps out-of-enum/missing roles to "system" (the fix), preserves legitimate string roles, and maps ST numeric 0/1/2 (parity with the old resolveRole). Local run: 3/3 pass.

Still to verify manually against the deploy:

  • Hand-edit a .marinara.json lorebook so one entry has "role": "hacker-role", import it, and confirm the entry reads "system" (clamped) while a sibling entry whose role was legitimately "assistant" still reads "assistant".

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2538 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/9/2026 **Status:** ✅ Merged **Merged:** 6/9/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/lorebook-import-role-allowlist` --- ### 📝 Commits (2) - [`4c33dd3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4c33dd36962012796f1da0b82ded521266dcd6eb) fix(import): clamp Marinara lorebook entry role to the system/user/assistant union - [`7c26135`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7c2613532ab6649c517925b9168f04b7cbd2a86f) Merge branch 'staging' into fix/lorebook-import-role-allowlist ### 📊 Changes **3 files changed** (+23 additions, -12 deletions) <details> <summary>View changed files</summary> ➕ `packages/server/src/services/import/lorebook-role.ts` (+19 -0) 📝 `packages/server/src/services/import/marinara.importer.ts` (+2 -1) 📝 `packages/server/src/services/import/st-lorebook.importer.ts` (+2 -11) </details> ### 📄 Description ## Linked issue Closes #2530 ## Why this change The Marinara native importer wrote the lorebook entry role verbatim: ```ts role: (e.role as any) ?? "system", ``` The bulk-import path (`bulkCreateEntries` → `createEntry`) inserts `role` with no zod parse, so an arbitrary string from a hand-crafted `.marinara.json` persisted as the entry role and later propagated into a provider `PromptMessage` role. The ST lorebook importer already normalizes the identical field via `resolveRole`, and the manual entry-create routes run `createLorebookEntrySchema.parse()` (which clamps to `z.enum(["system","user","assistant"])`). The Marinara bulk path was the one site that skipped the clamp. ## What changed - Extracted the ST importer's local `resolveRole` into a shared `resolveLorebookEntryRole` helper (`services/import/lorebook-role.ts`), with the parameter widened to `unknown` so the Marinara importer (where `e.role` is `unknown`) can call it without a cast. The body already narrows safely, so this is behavior-preserving for the ST path. - Re-pointed the ST importer's single call site to the shared helper and removed the now-duplicated local definition. - Replaced the unclamped assignment in `marinara.importer.ts` with `resolveLorebookEntryRole(e.role)`. Legitimate roles (`"system"`/`"user"`/`"assistant"` and ST numeric `0`/`1`/`2`) round-trip identically; only out-of-enum garbage now falls back to `"system"`. **Out of scope (noted for transparency):** `marinara.importer.ts` has a second `role: (s.role as any) ?? "system"` at the preset-section import path (`createSection`). That is a different entity with its own storage/validation path, not a lorebook entry role, so it's left untouched here. Not a regression: `main` has the byte-identical unclamped assignment; long-standing shared-code bug. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes `pnpm check` passes locally (confirms no dangling `resolveRole` reference after the extraction). Added a local-only node:test (`lorebook-role.test.ts`, gitignored): clamps out-of-enum/missing roles to `"system"` (the fix), preserves legitimate string roles, and maps ST numeric `0/1/2` (parity with the old `resolveRole`). Local run: **3/3 pass**. Still to verify manually against the deploy: - Hand-edit a `.marinara.json` lorebook so one entry has `"role": "hacker-role"`, import it, and confirm the entry reads `"system"` (clamped) while a sibling entry whose role was legitimately `"assistant"` still reads `"assistant"`. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:18:04 +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#5642
No description provided.