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.
This commit is contained in:
Kyle Carberry
2026-02-28 16:51:04 -05:00
committed by GitHub
parent 34d9392e37
commit 22d4539a7a
+10
View File
@@ -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
}