From bc26dbb4c3e2a450dacdeaa2b04084d2361ec391 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 1 Nov 2026 01:41:35 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20bound=20chat=20history=20to?= =?UTF-8?q?=20cap=20quadratic=20viewport=20rendering?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #3268 Co-authored-by: dezren39 <11225574+dezren39@users.noreply.github.com> --- pkgs/chat/main.go | 16 ++++++++- pkgs/chat/main_test.go | 80 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 pkgs/chat/main_test.go diff --git a/pkgs/chat/main.go b/pkgs/chat/main.go index 67fb049127..fe8360d006 100644 --- a/pkgs/chat/main.go +++ b/pkgs/chat/main.go @@ -24,6 +24,12 @@ import ( const ( host = "localhost" port = 23234 + + // 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 ) // app contains a wish server and the list of running programs. @@ -175,7 +181,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 new file mode 100644 index 0000000000..46fa7ab212 --- /dev/null +++ b/pkgs/chat/main_test.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "strings" + "testing" +) + +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) + } +}