From 22d4539a7a444fd75472c6e6ce43a1052c15eb84 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Sat, 28 Feb 2026 16:51:04 -0500 Subject: [PATCH] fix(chatd): clear stream buffer after each step is persisted (#22445) The in-memory stream buffer accumulated message-part events for the entire duration of a chat run. Late-joining subscribers received all buffered parts even though the backing messages had already been committed to the database, wasting memory and potentially duplicating content. Clear the buffer at the end of each `persistStep` call so that only in-flight (uncommitted) parts remain in the buffer. --- coderd/chatd/chatd.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/coderd/chatd/chatd.go b/coderd/chatd/chatd.go index 7cf8ca1dfa..aa698fa068 100644 --- a/coderd/chatd/chatd.go +++ b/coderd/chatd/chatd.go @@ -1967,6 +1967,16 @@ func (p *Server) runChat( p.publishMessage(chat.ID, toolMessage) } + + // Clear the stream buffer now that the step is + // persisted. Late-joining subscribers will load + // these messages from the database instead. + p.streamMu.Lock() + if state, ok := p.chatStreams[chat.ID]; ok { + state.buffer = nil + } + p.streamMu.Unlock() + return nil }