[GH-ISSUE #2960] [Issue]: Switching swipes while a message is still finishing generation can attach its thinking/sprite/reactions to the wrong swipe #1536

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

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

Structural swipe operations (switch active swipe, add swipe, edit content, delete swipe) write the message row outside the per-message lock that serializes streaming extra updates. So if you switch swipes in the brief window where a generation is still persisting its post-stream extra (thinking / sprite expression / CYOA / reactions), the two writes can interleave and pin that extra onto the wrong swipe (or lose it).

How to reproduce (try it yourself)

  1. Regenerate an assistant message M so the server is just finishing the visible stream and is about to persist post-stream extra (sprite expression / CYOA / reactions).
  2. In that brief window — after the text finishes but before the generation lock releases — rapidly click a swipe arrow on M, firing the active-swipe switch.

You'll see (intermittently, latency-dependent): the streamed thinking/sprite/reaction ends up attached to the swipe you switched to rather than the one that produced it, or goes missing.
Expected: streaming extra always lands on the swipe that produced it.

(This is a narrow, single-threaded-Node timing window and the corrupted data is extra JSON metadata, not the message text body — but it's a real interleave.)

Where it is

packages/server/src/services/storage/chats.storage.ts — setActiveSwipe (1070-1102), addSwipe (1017-1068), updateMessageContent (843-859), and removeSwipe write messages.extra / content / activeSwipeIndex with bare db.update, while updateMessageExtra (862-874) and appendMessageAttachment serialize through withPatchQueue(messageExtraPatchQueues, id). A related un-queued spot is chats.routes.ts:1701, which calls updateSwipeExtra using a previously-captured activeSwipeIndex.

What's happening

db.transaction() is intentionally avoided in this store (libSQL stateful-transaction crash, noted at ~934), so the patch queue is the only mutual-exclusion mechanism for a message's row — and the structural swipe mutators don't join it. The realistically-fireable window is the sprite/CYOA/reactions post-stream persistence block, during which the unguarded active-swipe route is freely callable.

Suggested fix

Route the messages-row writes in setActiveSwipe, addSwipe, updateMessageContent, and removeSwipe (and the chats.routes.ts:1701 updateSwipeExtra) through withPatchQueue(messageExtraPatchQueues, id), so all mutations of a given message's row are mutually exclusive with the streaming extra patches.


AI-investigated against v2.0.6; the queued vs un-queued writers confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2960 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** Structural swipe operations (switch active swipe, add swipe, edit content, delete swipe) write the message row **outside** the per-message lock that serializes streaming `extra` updates. So if you switch swipes in the brief window where a generation is still persisting its post-stream extra (thinking / sprite expression / CYOA / reactions), the two writes can interleave and pin that extra onto the wrong swipe (or lose it). **How to reproduce (try it yourself)** 1. Regenerate an assistant message M so the server is just finishing the visible stream and is about to persist post-stream extra (sprite expression / CYOA / reactions). 2. In that brief window — after the text finishes but before the generation lock releases — rapidly click a swipe arrow on M, firing the active-swipe switch. **You'll see (intermittently, latency-dependent):** the streamed thinking/sprite/reaction ends up attached to the swipe you switched *to* rather than the one that produced it, or goes missing. **Expected:** streaming extra always lands on the swipe that produced it. (This is a narrow, single-threaded-Node timing window and the corrupted data is `extra` JSON metadata, not the message text body — but it's a real interleave.) **Where it is** `packages/server/src/services/storage/chats.storage.ts` — `setActiveSwipe` (1070-1102), `addSwipe` (1017-1068), `updateMessageContent` (843-859), and `removeSwipe` write `messages.extra` / `content` / `activeSwipeIndex` with bare `db.update`, while `updateMessageExtra` (862-874) and `appendMessageAttachment` serialize through `withPatchQueue(messageExtraPatchQueues, id)`. A related un-queued spot is `chats.routes.ts:1701`, which calls `updateSwipeExtra` using a previously-captured `activeSwipeIndex`. **What's happening** `db.transaction()` is intentionally avoided in this store (libSQL stateful-transaction crash, noted at ~934), so the patch queue is the *only* mutual-exclusion mechanism for a message's row — and the structural swipe mutators don't join it. The realistically-fireable window is the sprite/CYOA/reactions post-stream persistence block, during which the unguarded active-swipe route is freely callable. **Suggested fix** Route the `messages`-row writes in `setActiveSwipe`, `addSwipe`, `updateMessageContent`, and `removeSwipe` (and the `chats.routes.ts:1701` `updateSwipeExtra`) through `withPatchQueue(messageExtraPatchQueues, id)`, so all mutations of a given message's row are mutually exclusive with the streaming extra patches. --- *AI-investigated against v2.0.6; the queued vs un-queued writers confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in staging: structural swipe mutations and generated extras are serialized per message; generation/retry-agent extras now patch the intended swipe index and only mirror to the message row when that swipe is active. Validated with pnpm check and pnpm regression:prompt.

<!-- gh-comment-id:4830956538 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in staging: structural swipe mutations and generated extras are serialized per message; generation/retry-agent extras now patch the intended swipe index and only mirror to the message row when that swipe is active. Validated with pnpm check and pnpm regression:prompt.
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#1536
No description provided.