[GH-ISSUE #3079] [Issue]: Author's Note from one chat overwrites the previous chat on fast switch #1644

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

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

Originally assigned to: @SpicyMarinara on GitHub.

Summary

In v2.0.6, quickly switching between chats can cause the outgoing chat's Author's Note cleanup to save the incoming chat's text into the wrong room. This results in one chat permanently showing another chat's note.

Expected behavior

Switching chats should save each room's own Author's Note data to the correct chat, without cross-room overwrites.

Actual behavior

When switching from chat A to chat B around the save timing, chat A can end up storing B's Author's Note text. The wrong note is written to the previous room and persists after returning to it.

Steps to reproduce

  1. Open chat A and edit its Author's Note without closing the chat.
  2. Quickly switch to chat B, which has a different Author's Note, around the save timing.
  3. Return to chat A and observe that A now shows and has saved B's text.

Environment

  • Marinara Engine version: v2.0.6 (49164fec)
  • Install type: Not provided in the Discord context.
  • OS + version: Not provided in the Discord context.
  • Browser or app shell: Not provided in the Discord context.

Logs, screenshots, or video

No logs, screenshots, or video provided in the Discord context.

Additional context

Reported root cause points to AuthorNotesPanel in packages/client/src/components/chat/ChatRoleplayPanels.tsx. The save-on-leave cleanup reads from a ref that is updated on every render, so the outgoing chat can save the incoming chat's latest notes. Marinara replied asking whether this is present on Staging and said it is likely already addressed.

Discord thread: Author's Note bleeds across chats on fast switch (v2.0.6)
Discord thread link: https://discord.com/channels/1417099416812392641/1521355128517431416
Requested by: Marinara (marinara_spaghetti, Discord ID 169561011928432653)

Discord thread transcript (5 messages)
[2026-06-30T03:22:08.050Z] Evie: Found another Author's Note bug in v2.0.6 (49164fec): switching chats fast can overwrite the *previous* room's note with the *new* room's text. A room that already had its own note ends up holding another room's content, and since it's a write, it sticks.

**Repro**
1. Open chat A, type in the Author's Note, leave it open.
2. Quickly switch to chat B (different note) right around the save.
3. Go back to A — A now shows (and has saved) B's text.

**Cause (short version)**
In `AuthorNotesPanel` (`packages/client/src/components/chat/ChatRoleplayPanels.tsx`), the save-on-leave effect reads `latestRef.current` inside its cleanup. That ref is reassigned on *every render*, not scoped per-chat. On A→B, React re-renders with B's values first, so by the time A's cleanup runs, `latestRef.current` already holds B's notes — and it gets written under `capturedChatId = A`. `baselineRef` is B-based at that point too, so the "changed?" guard misfires as well.

Note: the earlier `key={chatId}` remount fix (≤2.0.5) doesn't catch this — the bad write happens in the *outgoing* room's cleanup, after the ref has already advanced to the incoming room.

Full root-cause walkthrough + a suggested diff in the replies below 👇
[Discord message: https://discord.com/channels/1417099416812392641/1521355128517431416/1521355128517431416]

[2026-06-30T03:22:39.013Z] Evie: Root cause (packages/client/src/components/chat/ChatRoleplayPanels.tsx, AuthorNotesPanel)
The save-on-leave effect reads "latest" values from a ref inside its cleanup:
`const latestRef = useRef({ notes, depthStr });
latestRef.current = { notes, depthStr };   // updated every render, OUTSIDE any effect
...
useEffect(() => {
  const capturedChatId = chatId;
  return () => {
    const { notes: n, depthStr: d } = latestRef.current;   // <-- not guaranteed to be A's
    const base = baselineRef.current;
    if (n !== base.notes || nextDepth !== base.depth) {
      mutateRef.current({ id: capturedChatId, authorNotes: n, authorNotesDepth: nextDepth });
    }
  };
}, [chatId]);`

latestRef.current (and baselineRef.current) are reassigned on every render,
not scoped to a chat. On an A→B switch React re-renders with B's values
first, so latestRef.current already holds B's notes by the time A's
effect cleanup runs. The cleanup then writes B's text under capturedChatId = A
→ A is corrupted with B's note. baselineRef is likewise B-based at cleanup
time, so the "changed?" guard misfires too.
[Discord message: https://discord.com/channels/1417099416812392641/1521355128517431416/1521355258385661962]

[2026-06-30T03:23:29.979Z] Evie: Suggested fix

Record the chatId alongside the latest values, and in cleanup only trust
latestRef if it still belongs to the room being saved; otherwise fall back to a
snapshot captured at effect-setup time (when it was still A's):
`
const latestRef = useRef({ notes, depthStr, chatId });
latestRef.current = { notes, depthStr, chatId };
...
useEffect(() => {
  const capturedChatId = chatId;
  const snapshot = { notes: latestRef.current.notes, depthStr: latestRef.current.depthStr };
  const snapBase = { notes: baselineRef.current.notes, depth: baselineRef.current.depth };
  return () => {
    const latest = latestRef.current;
    const sameRoom = latest.chatId === capturedChatId;
    const n = sameRoom ? latest.notes : snapshot.notes;
    const d = sameRoom ? latest.depthStr : snapshot.depthStr;
    const nextDepth = Math.max(0, parseInt(d, 10) || 0);
    if (n !== snapBase.notes || nextDepth !== snapBase.depth) {
      mutateRef.current({ id: capturedChatId, authorNotes: n, authorNotesDepth: nextDepth });
    }
  };
}, [chatId]);`

This stops the cross-room write. A cleaner alternative would be to capture the
notes/depth/baseline fully at effect-setup time and save those, so the cleanup
never reads render-time refs at all — whichever fits your state model better.

Notes


Seen on v2.0.6 (49164fec). The earlier key={chatId} remount fix (≤2.0.5)
doesn't cover this because the bad write happens in the outgoing room's
cleanup, after the ref has already advanced to the incoming room.
Anchor lines may have shifted, but the latestRef/baselineRef definitions
and the [chatId] cleanup effect in AuthorNotesPanel are the spots.
[Discord message: https://discord.com/channels/1417099416812392641/1521355128517431416/1521355472152821772]

[2026-06-30T05:35:48.640Z] Marinara: Is this present on Staging?
[Discord message: https://discord.com/channels/1417099416812392641/1521355128517431416/1521388769310413102]

[2026-06-30T05:36:39.984Z] Marinara: Pretty sure this is already addressed, but checking won’t hurt.
[Discord message: https://discord.com/channels/1417099416812392641/1521355128517431416/1521388984662753360]

Template check

Please uncheck (untick) the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @SpicyMarinara on GitHub (Jun 30, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3079 Originally assigned to: @SpicyMarinara on GitHub. ## Summary In v2.0.6, quickly switching between chats can cause the outgoing chat's Author's Note cleanup to save the incoming chat's text into the wrong room. This results in one chat permanently showing another chat's note. ## Expected behavior Switching chats should save each room's own Author's Note data to the correct chat, without cross-room overwrites. ## Actual behavior When switching from chat A to chat B around the save timing, chat A can end up storing B's Author's Note text. The wrong note is written to the previous room and persists after returning to it. ## Steps to reproduce 1. Open chat A and edit its Author's Note without closing the chat. 2. Quickly switch to chat B, which has a different Author's Note, around the save timing. 3. Return to chat A and observe that A now shows and has saved B's text. ## Environment - Marinara Engine version: v2.0.6 (49164fec) - Install type: Not provided in the Discord context. - OS + version: Not provided in the Discord context. - Browser or app shell: Not provided in the Discord context. ## Logs, screenshots, or video No logs, screenshots, or video provided in the Discord context. ## Additional context Reported root cause points to `AuthorNotesPanel` in `packages/client/src/components/chat/ChatRoleplayPanels.tsx`. The save-on-leave cleanup reads from a ref that is updated on every render, so the outgoing chat can save the incoming chat's latest notes. Marinara replied asking whether this is present on Staging and said it is likely already addressed. Discord thread: Author's Note bleeds across chats on fast switch (v2.0.6) Discord thread link: https://discord.com/channels/1417099416812392641/1521355128517431416 Requested by: Marinara (marinara_spaghetti, Discord ID 169561011928432653) <details> <summary>Discord thread transcript (5 messages)</summary> ```text [2026-06-30T03:22:08.050Z] Evie: Found another Author's Note bug in v2.0.6 (49164fec): switching chats fast can overwrite the *previous* room's note with the *new* room's text. A room that already had its own note ends up holding another room's content, and since it's a write, it sticks. **Repro** 1. Open chat A, type in the Author's Note, leave it open. 2. Quickly switch to chat B (different note) right around the save. 3. Go back to A — A now shows (and has saved) B's text. **Cause (short version)** In `AuthorNotesPanel` (`packages/client/src/components/chat/ChatRoleplayPanels.tsx`), the save-on-leave effect reads `latestRef.current` inside its cleanup. That ref is reassigned on *every render*, not scoped per-chat. On A→B, React re-renders with B's values first, so by the time A's cleanup runs, `latestRef.current` already holds B's notes — and it gets written under `capturedChatId = A`. `baselineRef` is B-based at that point too, so the "changed?" guard misfires as well. Note: the earlier `key={chatId}` remount fix (≤2.0.5) doesn't catch this — the bad write happens in the *outgoing* room's cleanup, after the ref has already advanced to the incoming room. Full root-cause walkthrough + a suggested diff in the replies below 👇 [Discord message: https://discord.com/channels/1417099416812392641/1521355128517431416/1521355128517431416] [2026-06-30T03:22:39.013Z] Evie: Root cause (packages/client/src/components/chat/ChatRoleplayPanels.tsx, AuthorNotesPanel) The save-on-leave effect reads "latest" values from a ref inside its cleanup: `const latestRef = useRef({ notes, depthStr }); latestRef.current = { notes, depthStr }; // updated every render, OUTSIDE any effect ... useEffect(() => { const capturedChatId = chatId; return () => { const { notes: n, depthStr: d } = latestRef.current; // <-- not guaranteed to be A's const base = baselineRef.current; if (n !== base.notes || nextDepth !== base.depth) { mutateRef.current({ id: capturedChatId, authorNotes: n, authorNotesDepth: nextDepth }); } }; }, [chatId]);` latestRef.current (and baselineRef.current) are reassigned on every render, not scoped to a chat. On an A→B switch React re-renders with B's values first, so latestRef.current already holds B's notes by the time A's effect cleanup runs. The cleanup then writes B's text under capturedChatId = A → A is corrupted with B's note. baselineRef is likewise B-based at cleanup time, so the "changed?" guard misfires too. [Discord message: https://discord.com/channels/1417099416812392641/1521355128517431416/1521355258385661962] [2026-06-30T03:23:29.979Z] Evie: Suggested fix Record the chatId alongside the latest values, and in cleanup only trust latestRef if it still belongs to the room being saved; otherwise fall back to a snapshot captured at effect-setup time (when it was still A's): ` const latestRef = useRef({ notes, depthStr, chatId }); latestRef.current = { notes, depthStr, chatId }; ... useEffect(() => { const capturedChatId = chatId; const snapshot = { notes: latestRef.current.notes, depthStr: latestRef.current.depthStr }; const snapBase = { notes: baselineRef.current.notes, depth: baselineRef.current.depth }; return () => { const latest = latestRef.current; const sameRoom = latest.chatId === capturedChatId; const n = sameRoom ? latest.notes : snapshot.notes; const d = sameRoom ? latest.depthStr : snapshot.depthStr; const nextDepth = Math.max(0, parseInt(d, 10) || 0); if (n !== snapBase.notes || nextDepth !== snapBase.depth) { mutateRef.current({ id: capturedChatId, authorNotes: n, authorNotesDepth: nextDepth }); } }; }, [chatId]);` This stops the cross-room write. A cleaner alternative would be to capture the notes/depth/baseline fully at effect-setup time and save those, so the cleanup never reads render-time refs at all — whichever fits your state model better. Notes Seen on v2.0.6 (49164fec). The earlier key={chatId} remount fix (≤2.0.5) doesn't cover this because the bad write happens in the outgoing room's cleanup, after the ref has already advanced to the incoming room. Anchor lines may have shifted, but the latestRef/baselineRef definitions and the [chatId] cleanup effect in AuthorNotesPanel are the spots. [Discord message: https://discord.com/channels/1417099416812392641/1521355128517431416/1521355472152821772] [2026-06-30T05:35:48.640Z] Marinara: Is this present on Staging? [Discord message: https://discord.com/channels/1417099416812392641/1521355128517431416/1521388769310413102] [2026-06-30T05:36:39.984Z] Marinara: Pretty sure this is already addressed, but checking won’t hurt. [Discord message: https://discord.com/channels/1417099416812392641/1521355128517431416/1521388984662753360] ``` </details> ## Template check Please **uncheck (untick)** the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:41:15 +07:00
  • closed this issue
  • added the
    in-pr
    bug
    labels
Author
Owner

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

Fixed in PR #3082: Author's Notes cleanup now reads the outgoing chat's own saved draft/baseline by chat ID, preventing fast-switch cross-chat overwrites. Validation passed: focused client lint and pnpm check.

<!-- gh-comment-id:4840785028 --> @SpicyMarinara commented on GitHub (Jun 30, 2026): Fixed in PR #3082: Author's Notes cleanup now reads the outgoing chat's own saved draft/baseline by chat ID, preventing fast-switch cross-chat overwrites. Validation passed: focused client lint and pnpm check.
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#1644
No description provided.