[GH-ISSUE #2368] [Issue]: marinara-chat-bulk import skips the entity-defaults normalization and role allowlisting every sibling import path applies #1169

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

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

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Low — validation gap.

The marinara-chat-bulk importer stores the imported chat record and its messages without the with_entity_defaults / typed-field normalization and the message-role allowlisting that every sibling import path applies. A hand-edited / cross-version / externally-produced bulk file can therefore persist a chat whose characterIds / activeLorebookIds / metadata / gameState are TEXT-encoded strings (the pre-refactor shape) and whose messages carry an arbitrary role string, both violating their contracts.

Expected behavior

The bulk-chat importer should run the chat record through with_entity_defaults("chats", ...) (which applies normalize_typed_json_fields) and normalize each message role to the allowlist, exactly as the lorebook/character/persona/preset and ST-JSONL import paths do.

Actual behavior

Chat record — no normalization (src-tauri/src/commands/storage/imports/marinara_chat_bulk.rs:117-118):

chat.insert("folderId".to_string(), Value::Null);
let chat_record = state.storage.create("chats", Value::Object(chat))?;
let chat_id = created_record_id(&chat_record, "chat")?;

FileStorage::create (crates/storage/src/lib.rs:293-361) only assigns id/createdAt/updatedAt; it does not run normalize_typed_json_fields, so normalization is the caller's job and this caller omits it. CHAT_FIELDS (contracts.rs:43-52) declares characterIds/activeLorebookIds as arrays and metadata/gameState as objects, so a record with those fields stored as "[\"abc\"]" / "{...}" strings is left contract-violating. Non-defensive consumers misbehave: ChatSettingsDrawer.tsx:610 does chat.characterIds ?? [] typed string[], then .length and useCharacterSummariesByIds(chatCharIds) — a string yields a wrong count and a malformed query. (Many consumers are defensive — parseChatCharacterIds, Array.isArray guards — which is why impact is bounded to silent-wrong rather than a hard crash.)

Message role — no allowlist (marinara_chat_bulk.rs:126-134):

let mut message = ensure_object(message.clone())?;
message.remove("id");
message.remove("rowid");
message.insert("chatId".to_string(), Value::String(chat_id.clone()));
let created = crate::storage_commands::message_swipes::create_message(
    state,
    Value::Object(message),
)?;

MESSAGE_FIELDS (contracts.rs:53) normalizes only swipes/images/extra, never role, and create_message / prepare_message_create_row does not constrain it. The ST JSONL importer normalizes role to an allowlist via imported_jsonl_message_role (bulk_imports.rs:47), so the two paths diverge — a {"role":"narrator"} or junk string is persisted verbatim. Downstream normalizeRole (prompt-assembly.ts:2490) coerces unknown roles to "user" at generation time, so this is silent-wrong / inconsistent render-export, not injection.

Steps to reproduce

  1. Create a .json with format: "marinara-chat-bulk" whose chat object has "characterIds": "[\"abc\"]" (string) and whose messages include {"role":"narrator","content":"..."}.
  2. Import it via the marinara file import.
  3. Open the imported chat's settings drawer — observe the wrong character count / malformed summary query; inspect storage to see the string-typed characterIds and the non-allowlisted role.

Impact area

  • Chat mode
  • Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime

Environment

  • Branch or commit: refactor @ 330f69a8
  • Run mode: Tauri desktop
  • OS + version: not relevant (static source audit)
  • Node + pnpm versions: not relevant
  • Rust version, if relevant: not relevant
  • Remote Runtime URL configured? not relevant
  • Provider, model, or integration, if relevant: not relevant — triggered by an imported chat-bulk file

Logs, screenshots, or video

Static source audit. Cited: imports/marinara_chat_bulk.rs:117-118 (chat record), :126-134 (message role); crates/storage/src/lib.rs:293-361 (create skips normalization); contracts contracts.rs:43-52/:53; sibling-normalizing paths imports/marinara.rs:396-409 (lorebook), :259/:328/:509 (character/persona/preset), bulk_imports.rs:47 (ST role allowlist); defensive consumer use-chat-surface-data.ts:57, non-defensive ChatSettingsDrawer.tsx:610-613.

Regression notes

Found via static audit; not a regression — refactor-only surface. The marinara-chat-bulk format, the array-typed characterIds contract, and the Rust FileStorage normalizer are all refactor-only. In main, characterIds is a text("character_ids") column that stores a JSON string by design and consumers parse it, and main has no equivalent bulk-chat importer, so there is no prior-correct main behavior the refactor inverted.

Additional context

Insert with_entity_defaults("chats", Value::Object(chat))? (or the legacy text-array/bool normalizers plus object normalization) before storage.create at marinara_chat_bulk.rs:118, mirroring the lorebook path at marinara.rs:396-409; and normalize the message role to the allowlist before create_message in the bulk loop, reusing bulk_imports.rs imported_jsonl_message_role. Every other import path already does both — only the bulk-chat path skips them.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2368 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Low — validation gap.** The `marinara-chat-bulk` importer stores the imported chat record and its messages without the `with_entity_defaults` / typed-field normalization and the message-`role` allowlisting that every sibling import path applies. A hand-edited / cross-version / externally-produced bulk file can therefore persist a chat whose `characterIds` / `activeLorebookIds` / `metadata` / `gameState` are TEXT-encoded strings (the pre-refactor shape) and whose messages carry an arbitrary `role` string, both violating their contracts. ## Expected behavior The bulk-chat importer should run the chat record through `with_entity_defaults("chats", ...)` (which applies `normalize_typed_json_fields`) and normalize each message `role` to the allowlist, exactly as the lorebook/character/persona/preset and ST-JSONL import paths do. ## Actual behavior **Chat record — no normalization** (`src-tauri/src/commands/storage/imports/marinara_chat_bulk.rs:117-118`): ```rust chat.insert("folderId".to_string(), Value::Null); let chat_record = state.storage.create("chats", Value::Object(chat))?; let chat_id = created_record_id(&chat_record, "chat")?; ``` `FileStorage::create` (`crates/storage/src/lib.rs:293-361`) only assigns `id`/`createdAt`/`updatedAt`; it does **not** run `normalize_typed_json_fields`, so normalization is the caller's job and this caller omits it. `CHAT_FIELDS` (`contracts.rs:43-52`) declares `characterIds`/`activeLorebookIds` as arrays and `metadata`/`gameState` as objects, so a record with those fields stored as `"[\"abc\"]"` / `"{...}"` strings is left contract-violating. Non-defensive consumers misbehave: `ChatSettingsDrawer.tsx:610` does `chat.characterIds ?? []` typed `string[]`, then `.length` and `useCharacterSummariesByIds(chatCharIds)` — a string yields a wrong count and a malformed query. (Many consumers are defensive — `parseChatCharacterIds`, `Array.isArray` guards — which is why impact is bounded to silent-wrong rather than a hard crash.) **Message role — no allowlist** (`marinara_chat_bulk.rs:126-134`): ```rust let mut message = ensure_object(message.clone())?; message.remove("id"); message.remove("rowid"); message.insert("chatId".to_string(), Value::String(chat_id.clone())); let created = crate::storage_commands::message_swipes::create_message( state, Value::Object(message), )?; ``` `MESSAGE_FIELDS` (`contracts.rs:53`) normalizes only `swipes`/`images`/`extra`, never `role`, and `create_message` / `prepare_message_create_row` does not constrain it. The ST JSONL importer normalizes `role` to an allowlist via `imported_jsonl_message_role` (`bulk_imports.rs:47`), so the two paths diverge — a `{"role":"narrator"}` or junk string is persisted verbatim. Downstream `normalizeRole` (`prompt-assembly.ts:2490`) coerces unknown roles to `"user"` at generation time, so this is silent-wrong / inconsistent render-export, not injection. ## Steps to reproduce 1. Create a `.json` with `format: "marinara-chat-bulk"` whose `chat` object has `"characterIds": "[\"abc\"]"` (string) and whose messages include `{"role":"narrator","content":"..."}`. 2. Import it via the marinara file import. 3. Open the imported chat's settings drawer — observe the wrong character count / malformed summary query; inspect storage to see the string-typed `characterIds` and the non-allowlisted `role`. ## Impact area - [x] Chat mode - [x] Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime ## Environment - Branch or commit: refactor @ 330f69a8 - Run mode: Tauri desktop - OS + version: not relevant (static source audit) - Node + pnpm versions: not relevant - Rust version, if relevant: not relevant - Remote Runtime URL configured? not relevant - Provider, model, or integration, if relevant: not relevant — triggered by an imported chat-bulk file ## Logs, screenshots, or video Static source audit. Cited: `imports/marinara_chat_bulk.rs:117-118` (chat record), `:126-134` (message role); `crates/storage/src/lib.rs:293-361` (create skips normalization); contracts `contracts.rs:43-52`/`:53`; sibling-normalizing paths `imports/marinara.rs:396-409` (lorebook), `:259/:328/:509` (character/persona/preset), `bulk_imports.rs:47` (ST role allowlist); defensive consumer `use-chat-surface-data.ts:57`, non-defensive `ChatSettingsDrawer.tsx:610-613`. ## Regression notes Found via static audit; **not a regression** — refactor-only surface. The `marinara-chat-bulk` format, the array-typed `characterIds` contract, and the Rust `FileStorage` normalizer are all refactor-only. In main, `characterIds` is a `text("character_ids")` column that stores a JSON string by design and consumers parse it, and main has no equivalent bulk-chat importer, so there is no prior-correct main behavior the refactor inverted. ## Additional context Insert `with_entity_defaults("chats", Value::Object(chat))?` (or the legacy text-array/bool normalizers plus object normalization) before `storage.create` at `marinara_chat_bulk.rs:118`, mirroring the lorebook path at `marinara.rs:396-409`; and normalize the message `role` to the allowlist before `create_message` in the bulk loop, reusing `bulk_imports.rs` `imported_jsonl_message_role`. Every other import path already does both — only the bulk-chat path skips them. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:28:16 +07:00
Author
Owner

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

Fixed by #2418, merged into refactor.

<!-- gh-comment-id:4637901465 --> @Romuromylus commented on GitHub (Jun 6, 2026): Fixed by #2418, merged into `refactor`.
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#1169
No description provided.