diff --git a/pkgs/chat/main.go b/pkgs/chat/main.go index b52a8f7314..dcd8bb64c9 100644 --- a/pkgs/chat/main.go +++ b/pkgs/chat/main.go @@ -27,6 +27,12 @@ const ( host = "localhost" port = 23234 recipientQueueDepth = 16 + + // maxHistory is the maximum number of chat messages retained per session. + // The viewport is 5 lines tall; capping history at 100 provides generous + // scrollback (20 full viewports) while bounding slice growth and preventing + // quadratic strings.Join re-render costs in long-lived sessions (Fixes #3268). + maxHistory = 100 ) // sender is the subset of tea.Program used for broadcast. Keeping the @@ -335,7 +341,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } case chatMsg: - m.messages = append(m.messages, m.senderStyle.Render(msg.id)+": "+msg.text) + formatted := m.senderStyle.Render(msg.id) + ": " + msg.text + // Maintain a fixed-size history sliding window once maxHistory is reached. + // Avoids unbounded slice allocations and keeps strings.Join work bounded at O(maxHistory). + if len(m.messages) >= maxHistory { + copy(m.messages, m.messages[1:]) + m.messages[len(m.messages)-1] = formatted + } else { + m.messages = append(m.messages, formatted) + } m.viewport.SetContent(strings.Join(m.messages, "\n")) m.viewport.GotoBottom() diff --git a/pkgs/chat/main_test.go b/pkgs/chat/main_test.go index 73120f2e41..da1ce9d0fe 100644 --- a/pkgs/chat/main_test.go +++ b/pkgs/chat/main_test.go @@ -7,6 +7,7 @@ import ( "io" "os" "runtime" + "strings" "sync" "testing" "time" @@ -445,3 +446,76 @@ func TestSelfSendFromUpdateIsDeliveredOnceInOrderWithoutBlocking(t *testing.T) { t.Fatalf("self received %d Send calls for two updates and one barrier, want exactly 3", got) } } + +func TestChatHistoryCap(t *testing.T) { + m := initialModel() + + // Initial model starts with 0 messages + if len(m.messages) != 0 { + t.Fatalf("expected initial messages length 0, got %d", len(m.messages)) + } + + totalMessages := maxHistory + 50 + for i := 1; i <= totalMessages; i++ { + text := fmt.Sprintf("message %d", i) + updatedModel, _ := m.Update(chatMsg{ + id: "alice", + text: text, + }) + m = updatedModel.(model) + } + + // Verify messages slice is capped at maxHistory + if len(m.messages) != maxHistory { + t.Errorf("expected len(m.messages) == %d, got %d", maxHistory, len(m.messages)) + } + + // Oldest retained message should be totalMessages - maxHistory + 1 + oldestExpected := fmt.Sprintf("message %d", totalMessages-maxHistory+1) + if !strings.Contains(m.messages[0], oldestExpected) { + t.Errorf("expected oldest message to contain %q, got %q", oldestExpected, m.messages[0]) + } + + // Newest message should be totalMessages + newestExpected := fmt.Sprintf("message %d", totalMessages) + if !strings.Contains(m.messages[len(m.messages)-1], newestExpected) { + t.Errorf("expected newest message to contain %q, got %q", newestExpected, m.messages[len(m.messages)-1]) + } + + // Verify the newest message is visible in viewport view + viewportContent := m.viewport.View() + if !strings.Contains(viewportContent, newestExpected) { + t.Errorf("expected viewport to show newest message %q, got viewport view:\n%s", newestExpected, viewportContent) + } + + // Verify earlier dropped message is not in retained messages + droppedMsg := "message 1" + if strings.Contains(m.messages[0], droppedMsg) { + t.Errorf("expected dropped message %q to not be in m.messages[0], got %q", droppedMsg, m.messages[0]) + } +} + +func BenchmarkChatMsgUpdate(b *testing.B) { + m := initialModel() + // Pre-fill history to maxHistory + for i := 0; i < maxHistory; i++ { + updatedModel, _ := m.Update(chatMsg{ + id: "benchuser", + text: fmt.Sprintf("setup message %d", i), + }) + m = updatedModel.(model) + } + + b.ResetTimer() + b.ReportAllocs() + + msg := chatMsg{ + id: "benchuser", + text: "streaming bench message content", + } + + for i := 0; i < b.N; i++ { + updatedModel, _ := m.Update(msg) + m = updatedModel.(model) + } +}