[GH-ISSUE #1408] Apply the parseChatCharacterIds runtime-cast pattern to message.extra and chat.metadata consumers #698

Closed
opened 2026-10-20 17:32:00 +07:00 by bsakaguchi · 1 comment
Owner

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

Why

#1407 collapsed the slim Chat type duplicate in features/catalog/chats/types.ts onto the canonical engine type. To preserve the existing parseChatCharacterIds runtime defense against SQLite serialization handing back JSON-stringified arrays, that PR added one local widening cast at src/features/modes/shared/chat-ui/hooks/use-chat-surface-data.ts:54.

The same rationale applies to two other fields that are typed as strict objects on the engine but pass through the same normalize_typed_json_fields codepath in src-tauri/src/commands/storage/shared.rs (only normalized on write, not on read). Several consumers still have defensive typeof X === "string" ? JSON.parse(...) : X branches that are now type-unreachable under the engine contract:

message.extra

  • src/features/modes/shared/chat-ui/hooks/use-chat-timeline-actions.ts:37-44
  • src/features/modes/shared/chat-ui/hooks/use-sprite-metadata-state.ts:24-30
  • src/features/modes/shared/chat-ui/components/ChatMessage.tsx:920-922

chat.metadata

  • src/features/modes/shared/chat-ui/components/SummariesEditorModal.tsx:115
  • src/features/modes/shared/chat-ui/components/ChatSettingsDrawer.tsx:264,330

What to decide

Three reasonable directions. Each has trade-offs:

  1. Same local widening cast as parseChatCharacterIds got in #1407. Minimal change, keeps the runtime fallback honest. Localizes the trade-off to consumers.
  2. Move the JSON-string defensive parse to the storage read boundary (where the column is read out of FileStorage) so consumers can rely on engine types. Single source of truth, no per-consumer cast.
  3. Delete the dead branches. Trust that Rust's normalize_json_array_fields and normalize_nullable_json_object_fields (shared.rs:178,184) already coerce columns to typed shapes on write, and that any pre-refactor legacy data already passes through import_legacy_profile_tables_with_restored_assets which applies the same normalization. Risk: any unmigrated runtime path that bypasses the importer would crash on the dead branch's absence.

Option 2 feels most aligned with the engine-contract direction the refactor branch has been moving in, but it touches the storage capability rather than UI code.

Acceptance

  • One unit test exercises whichever path is chosen (e.g. round-trip a message.extra that arrives as a JSON string and asserts the consumer reads the parsed object).
  • All five sites listed above are addressed consistently.

Linked

Refs #1344 (parent audit), follow-up to #1407.

Originally created by @Romuromylus on GitHub (May 27, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/1408 ## Why #1407 collapsed the slim `Chat` type duplicate in `features/catalog/chats/types.ts` onto the canonical engine type. To preserve the existing `parseChatCharacterIds` runtime defense against SQLite serialization handing back JSON-stringified arrays, that PR added one local widening cast at `src/features/modes/shared/chat-ui/hooks/use-chat-surface-data.ts:54`. The same rationale applies to two other fields that are typed as strict objects on the engine but pass through the same `normalize_typed_json_fields` codepath in `src-tauri/src/commands/storage/shared.rs` (only normalized on write, not on read). Several consumers still have defensive `typeof X === "string" ? JSON.parse(...) : X` branches that are now type-unreachable under the engine contract: ### `message.extra` - `src/features/modes/shared/chat-ui/hooks/use-chat-timeline-actions.ts:37-44` - `src/features/modes/shared/chat-ui/hooks/use-sprite-metadata-state.ts:24-30` - `src/features/modes/shared/chat-ui/components/ChatMessage.tsx:920-922` ### `chat.metadata` - `src/features/modes/shared/chat-ui/components/SummariesEditorModal.tsx:115` - `src/features/modes/shared/chat-ui/components/ChatSettingsDrawer.tsx:264,330` ## What to decide Three reasonable directions. Each has trade-offs: 1. Same local widening cast as `parseChatCharacterIds` got in #1407. Minimal change, keeps the runtime fallback honest. Localizes the trade-off to consumers. 2. Move the JSON-string defensive parse to the storage read boundary (where the column is read out of FileStorage) so consumers can rely on engine types. Single source of truth, no per-consumer cast. 3. Delete the dead branches. Trust that Rust's `normalize_json_array_fields` and `normalize_nullable_json_object_fields` (`shared.rs:178,184`) already coerce columns to typed shapes on write, and that any pre-refactor legacy data already passes through `import_legacy_profile_tables_with_restored_assets` which applies the same normalization. Risk: any unmigrated runtime path that bypasses the importer would crash on the dead branch's absence. Option 2 feels most aligned with the engine-contract direction the refactor branch has been moving in, but it touches the storage capability rather than UI code. ## Acceptance - One unit test exercises whichever path is chosen (e.g. round-trip a `message.extra` that arrives as a JSON string and asserts the consumer reads the parsed object). - All five sites listed above are addressed consistently. ## Linked Refs #1344 (parent audit), follow-up to #1407.
bsakaguchi 2026-10-20 17:32:00 +07:00
Author
Owner

@SpicyMarinara commented on GitHub (May 27, 2026):

Implemented in refactor commit a49f3fb4. Storage reads now normalize JSON-stringified chat.metadata and message.extra at the storage API boundary, the individual ad hoc parser sites were collapsed back to object reads, and storage-api tests cover round-tripping both shapes.

<!-- gh-comment-id:4554453818 --> @SpicyMarinara commented on GitHub (May 27, 2026): Implemented in refactor commit a49f3fb4. Storage reads now normalize JSON-stringified chat.metadata and message.extra at the storage API boundary, the individual ad hoc parser sites were collapsed back to object reads, and storage-api tests cover round-tripping both shapes.
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#698
No description provided.