[GH-ISSUE #450] refactor(storage): per-message serializer + stable swipeId for chats.storage.ts patch queues #197

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

Originally created by @coderabbitai[bot] on GitHub (May 6, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/450

Originally assigned to: @SpicyMarinara on GitHub.

Summary

The patch queue introduced in PR #448 (withPatchQueue) addresses serialization for appendMessageAttachment and appendSwipeAttachment, but a broader set of race conditions remains in packages/server/src/services/storage/chats.storage.ts.

Problem

The current per-key queue only covers helper-to-helper paths. The following functions still read and overwrite the same JSON fields outside the queue and can therefore race with queued appends:

  • updateMetadata
  • addSwipe
  • setActiveSwipe
  • removeSwipe

Additionally, the swipe queue key is constructed as messageId:swipeIndex. Because removeSwipe() renumbers swipe indices, an enqueued patch can target the wrong swipe — or no swipe at all — after renumbering occurs.

Proposed Solution

  1. Single per-message serializer: Introduce a perMessagePatchQueues map and funnel every function that touches message or swipe extras (updateMetadata, addSwipe, setActiveSwipe, removeSwipe, appendMessageAttachment, appendSwipeAttachment, updateMessageExtra, updateSwipeExtra) through withPatchQueue keyed by messageId.
  2. Stable swipe identifier: Use swipeId (the database row ID, e.g., messageSwipes.id) instead of the mutable swipeIndex as part of the queue key, so that renumbering in removeSwipe does not invalidate or misdirect enqueued operations.
  3. Renumbering safety: Ensure removeSwipe either avoids renumbering indices entirely (e.g., using sparse indices or tombstoning), or properly invalidates/migrates queued operations for affected swipe IDs.

Context

Requested by @cha1latte.

Originally created by @coderabbitai[bot] on GitHub (May 6, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/450 Originally assigned to: @SpicyMarinara on GitHub. ## Summary The patch queue introduced in PR #448 (`withPatchQueue`) addresses serialization for `appendMessageAttachment` and `appendSwipeAttachment`, but a broader set of race conditions remains in `packages/server/src/services/storage/chats.storage.ts`. ## Problem The current per-key queue only covers helper-to-helper paths. The following functions still read and overwrite the same JSON fields **outside** the queue and can therefore race with queued appends: - `updateMetadata` - `addSwipe` - `setActiveSwipe` - `removeSwipe` Additionally, the swipe queue key is constructed as `messageId:swipeIndex`. Because `removeSwipe()` renumbers swipe indices, an enqueued patch can target the wrong swipe — or no swipe at all — after renumbering occurs. ## Proposed Solution 1. **Single per-message serializer**: Introduce a `perMessagePatchQueues` map and funnel every function that touches message or swipe extras (`updateMetadata`, `addSwipe`, `setActiveSwipe`, `removeSwipe`, `appendMessageAttachment`, `appendSwipeAttachment`, `updateMessageExtra`, `updateSwipeExtra`) through `withPatchQueue` keyed by `messageId`. 2. **Stable swipe identifier**: Use `swipeId` (the database row ID, e.g., `messageSwipes.id`) instead of the mutable `swipeIndex` as part of the queue key, so that renumbering in `removeSwipe` does not invalidate or misdirect enqueued operations. 3. **Renumbering safety**: Ensure `removeSwipe` either avoids renumbering indices entirely (e.g., using sparse indices or tombstoning), or properly invalidates/migrates queued operations for affected swipe IDs. ## Context - Deferred from: #448 (fix(#438): persist selfies to active swipe, fix group notification identity) - Relevant code: `packages/server/src/services/storage/chats.storage.ts` — `withPatchQueue`, `appendMessageAttachment`, `appendSwipeAttachment`, `updateSwipeExtra`, `addSwipe`, `setActiveSwipe`, `removeSwipe` - Discussion: https://github.com/Pasta-Devs/Marinara-Engine/pull/448#discussion_r3192239115 Requested by @cha1latte.
bsakaguchi 2026-10-20 17:26:09 +07:00
Author
Owner

@cha1latte commented on GitHub (May 6, 2026):

Triage note: labeling this as scope-risk because the request is tightly aligned with storage correctness, but it changes queue semantics across several message and swipe mutation paths in chats.storage.ts rather than one isolated helper.

Least risky next step is to keep this as a scoped storage follow-up and define the per-message serialization and stable swipeId behavior before patching all affected mutation paths.

<!-- gh-comment-id:4392842551 --> @cha1latte commented on GitHub (May 6, 2026): Triage note: labeling this as `scope-risk` because the request is tightly aligned with storage correctness, but it changes queue semantics across several message and swipe mutation paths in `chats.storage.ts` rather than one isolated helper. Least risky next step is to keep this as a scoped storage follow-up and define the per-message serialization and stable `swipeId` behavior before patching all affected mutation paths.
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#197
No description provided.