fix: prevent repeat chat message rendering on reconnect/replay

Root cause: server persists chat to disk (chat.jsonl) and replays on restart.
Client had no dedup, so every reconnect re-rendered the entire history.
Messages from an old HN session would repeat endlessly on the SF Chronicle tab.

Fix: renderedEntryIds Set tracks which entry IDs have been rendered. addChatEntry
skips entries already in the set. Entries without an id (local notifications)
bypass the check. Clear chat resets the set.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-03-30 00:20:02 -07:00
parent 561ce40c35
commit 3df29fccb7
2 changed files with 45 additions and 0 deletions
+11
View File
@@ -102,7 +102,17 @@ let agentContainer = null; // The container for the current agent response
let agentTextEl = null; // The text accumulator element
let agentText = ''; // Accumulated text
// Dedup: track which entry IDs have already been rendered to prevent
// repeat rendering on reconnect or tab switch (server replays from disk)
const renderedEntryIds = new Set();
function addChatEntry(entry) {
// Dedup by entry ID — prevent repeat rendering on reconnect/replay
if (entry.id !== undefined) {
if (renderedEntryIds.has(entry.id)) return;
renderedEntryIds.add(entry.id);
}
// Remove welcome message on first real message
const welcome = chatMessages.querySelector('.chat-welcome');
if (welcome) welcome.remove();
@@ -577,6 +587,7 @@ document.getElementById('clear-chat').addEventListener('click', async () => {
} catch {}
// Reset local state
chatLineCount = 0;
renderedEntryIds.clear();
agentContainer = null;
agentTextEl = null;
agentText = '';