[GH-ISSUE #5641] Chat metadata edits can be transiently reverted by mutations that write the chat cache without the version guard #3122

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

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

Originally assigned to: @kolacheee on GitHub.

What happens

Chat metadata edits are protected against stale server echoes by a per-field version guard, but only when the write goes through useUpdateChatMetadata. Several other mutations write the chat detail cache with a raw server snapshot, bypassing that guard:

  • useClearAutonomousUnread — packages/client/src/hooks/use-chats.ts:819 (qc.setQueryData(chatKeys.detail(chatId), data))
  • the summary-entry mutation — use-chats.ts:857 (via syncCachedChat, which does no version merging)
  • packages/client/src/hooks/use-connections.ts:124
  • packages/client/src/hooks/use-background-autonomous.ts:286
  • packages/client/src/hooks/use-game.ts:301/454/485/529

If one of these responses was produced by the server before an in-flight metadata PATCH (the server serializes per-chat metadata writes, so a request queued first deterministically returns a pre-PATCH snapshot) but is consumed by the client after it, the cache momentarily regresses recently-changed metadata — including booleans like narrativeDirectorSecretPlotEnabled, enableAgents, and activeAgentIds that mount/unmount whole sections of the Chat Settings drawer. The section unmounts for a moment and remounts when the guarded PATCH echo restores the value.

Why it matters

A remount in that window used to silently destroy an in-progress edit (that symptom is now absorbed by the DraftNumberInput fix in PR #5640 — the input commits its pending value on unmount). But the underlying flicker is still real: any component state inside the flickered section is reset, and any other unguarded consumer of the regressed metadata briefly acts on stale values.

Concrete scenario

An autonomous chat has unread residue, so AppShell fires clearAutonomousUnread for the active chat (packages/client/src/components/AppShell.tsx:626-640) around the same time the user is editing agent settings in the drawer. The DELETE's response carries a chat snapshot from before the user's just-committed toggle; when it lands, the toggled section blinks. This is a response-ordering race, so it needs unlucky timing (slow machine, slow storage flush) — the same class of window that produced #5636's CI failure.

Suggested direction

Route every detail-cache write through the same per-field version merge useUpdateChatMetadata uses (e.g. give syncCachedChat a merging variant for server snapshots), rather than trusting raw snapshots to be current. The game-surface writers only affect game chats and the Mari writers only the internal Mari chat, so the two use-chats.ts writers are the highest-value ones.

Found while verifying the #5636 fix (adversarial review pass on PR #5640).

Originally created by @kolacheee on GitHub (Aug 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5641 Originally assigned to: @kolacheee on GitHub. **What happens** Chat metadata edits are protected against stale server echoes by a per-field version guard, but only when the write goes through `useUpdateChatMetadata`. Several other mutations write the chat detail cache with a raw server snapshot, bypassing that guard: - `useClearAutonomousUnread` — `packages/client/src/hooks/use-chats.ts:819` (`qc.setQueryData(chatKeys.detail(chatId), data)`) - the summary-entry mutation — `use-chats.ts:857` (via `syncCachedChat`, which does no version merging) - `packages/client/src/hooks/use-connections.ts:124` - `packages/client/src/hooks/use-background-autonomous.ts:286` - `packages/client/src/hooks/use-game.ts:301/454/485/529` If one of these responses was produced by the server *before* an in-flight metadata PATCH (the server serializes per-chat metadata writes, so a request queued first deterministically returns a pre-PATCH snapshot) but is *consumed* by the client after it, the cache momentarily regresses recently-changed metadata — including booleans like `narrativeDirectorSecretPlotEnabled`, `enableAgents`, and `activeAgentIds` that mount/unmount whole sections of the Chat Settings drawer. The section unmounts for a moment and remounts when the guarded PATCH echo restores the value. **Why it matters** A remount in that window used to silently destroy an in-progress edit (that symptom is now absorbed by the `DraftNumberInput` fix in PR #5640 — the input commits its pending value on unmount). But the underlying flicker is still real: any component state inside the flickered section is reset, and any other unguarded consumer of the regressed metadata briefly acts on stale values. **Concrete scenario** An autonomous chat has unread residue, so `AppShell` fires `clearAutonomousUnread` for the active chat (`packages/client/src/components/AppShell.tsx:626-640`) around the same time the user is editing agent settings in the drawer. The DELETE's response carries a chat snapshot from before the user's just-committed toggle; when it lands, the toggled section blinks. This is a response-ordering race, so it needs unlucky timing (slow machine, slow storage flush) — the same class of window that produced #5636's CI failure. **Suggested direction** Route every detail-cache write through the same per-field version merge `useUpdateChatMetadata` uses (e.g. give `syncCachedChat` a merging variant for server snapshots), rather than trusting raw snapshots to be current. The game-surface writers only affect game chats and the Mari writers only the internal Mari chat, so the two `use-chats.ts` writers are the highest-value ones. Found while verifying the #5636 fix (adversarial review pass on PR #5640).
bsakaguchi 2026-10-21 05:59:47 +07:00
Author
Owner

@kolacheee commented on GitHub (Aug 29, 2026):

Taking this one (kolacheee + Claude agent) on branch fix/5641-guarded-cache-writers; draft PR to follow with the first commit. #5631 will follow separately after.

<!-- gh-comment-id:5463815439 --> @kolacheee commented on GitHub (Aug 29, 2026): Taking this one (kolacheee + Claude agent) on branch fix/5641-guarded-cache-writers; draft PR to follow with the first commit. #5631 will follow separately after.
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#3122
No description provided.