perf(chat): replay a stored stream a segment at a time

continue-stream replays every stored event as its own SSE frame, and
events are stored at the granularity the model emits them: one chunk per
AppendEvent, no accumulation. A long answer with a dozen length-limited
continuations is tens of thousands of chunks, and every reconnect during
it (a refresh, a re-entered page) replays them all, one write, one flush
and one client-side pass each; the report on #3362 counts 47,196 events
for a single replay, with the page frozen while they arrive.

Coalesce them on the way out. Each run of consecutive, unfinished
thinking or answer chunks of one segment (same type and event ID) is
replayed as one event carrying the run's concatenated content, its last
timestamp and the union of its data. A done chunk, a chunk of another
type or of another segment ends the run and passes through untouched:
the client marks a segment finished on its done chunk without appending
that chunk's content, and it accumulates content by event_id, so what it
assembles is the same text in the same order, in a few frames per
segment instead of one per token. The live path, the stored events and
the completion marker are unchanged.
main
po-et 2026-09-17 19:50:31 +08:00 committed by lyingbug
parent a5bdf82a02
commit 1b36030502
3 changed files with 294 additions and 3 deletions

View File

@ -0,0 +1,76 @@
package session
import (
"strings"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
// coalesceReplayEvents merges each run of consecutive, unfinished delta
// chunks of one segment (same type and event ID; the types are
// deltaResponseTypes, the ones clients accumulate by event_id) into one event
// that carries the run's concatenated content, its last timestamp and the
// union of its data (later keys win). A done chunk, a chunk of another type
// or of another segment ends the run and passes through untouched, so
// completion markers, durations and references keep their own frames and
// the client's per-event_id accumulation ends with the same text.
//
// Events are stored at the granularity the model emits them, so one long
// answer can mean tens of thousands of frames on replay, each an SSE write, a
// flush and a client-side pass (#3362); this brings it down to a few frames
// per segment. The stored events and their data maps are not modified.
func coalesceReplayEvents(events []interfaces.StreamEvent) []interfaces.StreamEvent {
if len(events) < 2 {
return events
}
out := make([]interfaces.StreamEvent, 0, 16)
var run strings.Builder // content of the open run, written to out's last event when it ends
open := false // whether out's last event is an unfinished run
ownsData := false // whether that event's Data is already a private copy
closeRun := func() {
if open {
out[len(out)-1].Content = run.String()
run.Reset()
open = false
}
}
for _, evt := range events {
mergeable := deltaResponseTypes[evt.Type] && !evt.Done
if open {
last := &out[len(out)-1]
if mergeable && last.Type == evt.Type && last.ID == evt.ID {
run.WriteString(evt.Content)
last.Timestamp = evt.Timestamp
if len(evt.Data) > 0 {
if !ownsData {
last.Data = copyEventData(last.Data, len(evt.Data))
ownsData = true
}
for key, value := range evt.Data {
last.Data[key] = value
}
}
continue
}
closeRun()
}
out = append(out, evt)
if mergeable {
run.WriteString(evt.Content)
open = true
ownsData = false
}
}
closeRun()
return out
}
// copyEventData returns a private copy of a stored event's data map with room
// for extra keys; the original may be shared with the store's own buffer.
func copyEventData(data map[string]interface{}, extra int) map[string]interface{} {
copied := make(map[string]interface{}, len(data)+extra)
for key, value := range data {
copied[key] = value
}
return copied
}

View File

@ -0,0 +1,214 @@
package session
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/Tencent/WeKnora/internal/middleware"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
)
func chunk(id string, kind types.ResponseType, content string) interfaces.StreamEvent {
return interfaces.StreamEvent{ID: id, Type: kind, Content: content, Data: map[string]interface{}{"event_id": id}}
}
func doneChunk(id string, kind types.ResponseType, content string) interfaces.StreamEvent {
evt := chunk(id, kind, content)
evt.Done = true
evt.Data["duration_ms"] = int64(1200)
return evt
}
func TestCoalesceReplayEvents_MergesTheChunksOfOneSegmentIntoOneFrame(t *testing.T) {
t1, t2, t3 := time.Unix(1, 0), time.Unix(2, 0), time.Unix(3, 0)
a := chunk("think-1", types.ResponseTypeThinking, "Let ")
b := chunk("think-1", types.ResponseTypeThinking, "me ")
c := chunk("think-1", types.ResponseTypeThinking, "see.")
a.Timestamp, b.Timestamp, c.Timestamp = t1, t2, t3
b.Data["is_fallback"] = true
got := coalesceReplayEvents([]interfaces.StreamEvent{a, b, c})
require.Len(t, got, 1)
assert.Equal(t, "Let me see.", got[0].Content)
assert.Equal(t, "think-1", got[0].ID)
assert.False(t, got[0].Done)
assert.Equal(t, t3, got[0].Timestamp)
assert.Equal(t, map[string]interface{}{"event_id": "think-1", "is_fallback": true}, got[0].Data)
// The stored events keep their own content and data.
assert.Equal(t, "Let ", a.Content)
assert.Equal(t, map[string]interface{}{"event_id": "think-1"}, a.Data)
assert.Equal(t, map[string]interface{}{"event_id": "think-1"}, c.Data)
}
func TestCoalesceReplayEvents_ADoneChunkKeepsItsOwnFrame(t *testing.T) {
// The client marks a segment finished on its done chunk without appending
// that chunk's content, so a done chunk must never absorb earlier text.
got := coalesceReplayEvents([]interfaces.StreamEvent{
chunk("ans-1", types.ResponseTypeAnswer, "The answer "),
chunk("ans-1", types.ResponseTypeAnswer, "is 42."),
doneChunk("ans-1", types.ResponseTypeAnswer, ""),
chunk("ans-2", types.ResponseTypeAnswer, "Next."),
})
require.Len(t, got, 3)
assert.Equal(t, "The answer is 42.", got[0].Content)
assert.False(t, got[0].Done)
assert.True(t, got[1].Done)
assert.Equal(t, int64(1200), got[1].Data["duration_ms"])
assert.Equal(t, "Next.", got[2].Content)
}
func TestCoalesceReplayEvents_AnotherSegmentOrTypeEndsTheRun(t *testing.T) {
got := coalesceReplayEvents([]interfaces.StreamEvent{
chunk("think-1", types.ResponseTypeThinking, "a"),
chunk("think-2", types.ResponseTypeThinking, "b"),
chunk("think-1", types.ResponseTypeThinking, "c"),
chunk("ans-1", types.ResponseTypeAnswer, "d"),
{
ID: "tool-1", Type: types.ResponseTypeToolCall, Content: "Calling tool: search",
Data: map[string]interface{}{"tool": "search"},
},
chunk("ans-1", types.ResponseTypeAnswer, "e"),
// Same event ID, different type: two segments as far as the client
// is concerned, so two frames.
chunk("ans-1", types.ResponseTypeThinking, "f"),
{ID: "complete-1", Type: types.ResponseTypeComplete, Done: true},
})
require.Len(t, got, 8)
for i, want := range []string{"a", "b", "c", "d", "Calling tool: search", "e", "f", ""} {
assert.Equal(t, want, got[i].Content, "frame %d", i)
}
assert.Equal(t, types.ResponseTypeToolCall, got[4].Type)
assert.Equal(t, types.ResponseTypeThinking, got[6].Type)
assert.Equal(t, types.ResponseTypeComplete, got[7].Type)
}
func TestCoalesceReplayEvents_MergesReflectionChunksToo(t *testing.T) {
got := coalesceReplayEvents([]interfaces.StreamEvent{
chunk("refl-1", types.ResponseTypeReflection, "Looks "),
chunk("refl-1", types.ResponseTypeReflection, "right."),
})
require.Len(t, got, 1)
assert.Equal(t, "Looks right.", got[0].Content)
}
func TestCoalesceReplayEvents_LeavesShortInputsAlone(t *testing.T) {
assert.Empty(t, coalesceReplayEvents(nil))
one := []interfaces.StreamEvent{chunk("ans-1", types.ResponseTypeAnswer, "x")}
assert.Equal(t, one, coalesceReplayEvents(one))
}
// A long turn stored chunk by chunk replays as a handful of frames, and the
// client still receives every character in order.
func TestContinueStream_ReplaysALongAnswerInAFewFrames(t *testing.T) {
var events []interfaces.StreamEvent
var thought, answer strings.Builder
for i := 0; i < 2000; i++ {
piece := fmt.Sprintf("t%d ", i)
thought.WriteString(piece)
events = append(events, chunk("think-1", types.ResponseTypeThinking, piece))
}
events = append(events, doneChunk("think-1", types.ResponseTypeThinking, ""))
for i := 0; i < 3000; i++ {
piece := fmt.Sprintf("a%d ", i)
answer.WriteString(piece)
events = append(events, chunk("ans-1", types.ResponseTypeAnswer, piece))
}
events = append(events,
doneChunk("ans-1", types.ResponseTypeAnswer, ""),
interfaces.StreamEvent{ID: "complete-1", Type: types.ResponseTypeComplete, Done: true})
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(middleware.ErrorHandler())
h := &Handler{
sessionService: &stubSessionService{},
messageService: &stubMessageServiceForStream{},
streamManager: &stubStreamManager{events: events},
fileService: &stubResourceFileService{},
}
r.GET("/sessions/continue-stream/:session_id", h.ContinueStream)
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/sessions/continue-stream/sess1?message_id=msg1", nil))
require.Equal(t, http.StatusOK, recorder.Code)
body := recorder.Body.String()
// thinking run, thinking done, answer run, answer done, complete.
assert.Equal(t, 5, strings.Count(body, "event:message"), body)
assert.Contains(t, body, thought.String())
assert.Contains(t, body, answer.String())
assert.Less(t, strings.Index(body, thought.String()), strings.Index(body, answer.String()))
}
// growingStreamManager shows `visible` events on the first read and the whole
// log afterwards, so the handler replays a prefix and polls for the rest.
type growingStreamManager struct {
stubStreamManager
mu sync.Mutex
visible int
reads int
}
func (g *growingStreamManager) GetEvents(
_ context.Context, _, _ string, from int,
) ([]interfaces.StreamEvent, int, error) {
g.mu.Lock()
defer g.mu.Unlock()
g.reads++
limit := g.visible
if g.reads > 1 {
limit = len(g.events)
}
if from >= limit {
return nil, from, nil
}
return g.events[from:limit], limit, nil
}
// A storage reference cut between the replayed prefix and the chunks that
// arrive through the poll loop is still reassembled: the merged replay frame
// goes through the same holdback buffer as the live chunks after it.
func TestContinueStream_ReplayHoldbackSurvivesToThePollLoop(t *testing.T) {
events := []interfaces.StreamEvent{
chunk("answer-1", types.ResponseTypeAnswer, "The diagram "),
chunk("answer-1", types.ResponseTypeAnswer, "![fig](resource://xifDo7"),
// replay ends here (visible: 2); the rest arrives through the poll loop
chunk("answer-1", types.ResponseTypeAnswer, "NTSL300Lp1goVutw) shows the flow."),
doneChunk("answer-1", types.ResponseTypeAnswer, ""),
{ID: "complete-1", Type: types.ResponseTypeComplete, Done: true},
}
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(middleware.ErrorHandler())
h := &Handler{
sessionService: &stubSessionService{},
messageService: &stubMessageServiceForStream{},
streamManager: &growingStreamManager{stubStreamManager: stubStreamManager{events: events}, visible: 2},
fileService: &stubResourceFileService{},
}
r.GET("/sessions/continue-stream/:session_id", h.ContinueStream)
recorder := httptest.NewRecorder()
r.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet,
"/sessions/continue-stream/sess1?message_id=msg1&resource_urls=public", nil))
require.Equal(t, http.StatusOK, recorder.Code)
body := recorder.Body.String()
assert.NotContains(t, body, "resource://")
assert.Contains(t, body, `![fig](https://cdn.example.com/signed.png) shows the flow.`)
assert.Less(t, strings.LastIndex(body, `"response_type":"answer"`),
strings.Index(body, `"response_type":"complete"`))
}

View File

@ -146,9 +146,10 @@ func (h *Handler) ContinueStream(c *gin.Context) {
}
}
// Replay existing events
logger.Debugf(ctx, "Replaying %d existing events", len(events))
for _, evt := range events {
// Replay existing events, a segment's worth of chunks per frame
replay := coalesceReplayEvents(events)
logger.Debugf(ctx, "Replaying %d existing events as %d frames", len(events), len(replay))
for _, evt := range replay {
emitStreamEvent(ctx, c, evt, message.RequestID, resourceRewriter)
}