xonecas.io
60255
Zn

Zoea-Nova / commit 3658823

fix(tui): prevent overflow in AI and reasoning messages with long words

- Add hard-wrapping to wrapText for words exceeding maxWidth
- Long unbreakable strings (UUIDs, URLs) now wrap mid-word
- Prevents overflow in assistant messages and reasoning content
- Add comprehensive tests for long word wrapping
- Fixes overflow issue for all message types, not just JSON

xonecas2026-02-05 36588235d0ca0a0444ec46fed03057ed2ed5cbd8 parent a6e5d7f

3 files changed

internal/tui/focus.go+40−9
@@ -22,6 +22,7 @@ type LogEntry struct {
2222
2323 // wrapText wraps text to fit within maxWidth display columns, preserving words.
2424 // Uses lipgloss.Width() for proper Unicode character width calculation.
25+// Long words that exceed maxWidth are hard-wrapped to prevent overflow.
2526 func wrapText(text string, maxWidth int) []string {
2627 if maxWidth <= 0 {
2728 maxWidth = 80
@@ -42,19 +43,49 @@ func wrapText(text string, maxWidth int) []string {
4243 continue
4344 }
4445
45- currentLine := words[0]
46- for _, word := range words[1:] {
47- // Use lipgloss.Width() for proper Unicode width calculation
48- lineWidth := lipgloss.Width(currentLine)
46+ currentLine := ""
47+ for _, word := range words {
4948 wordWidth := lipgloss.Width(word)
50- if lineWidth+1+wordWidth <= maxWidth {
51- currentLine += " " + word
52- } else {
53- lines = append(lines, currentLine)
49+
50+ // If word itself is too long, hard-wrap it
51+ if wordWidth > maxWidth {
52+ // Flush current line first
53+ if currentLine != "" {
54+ lines = append(lines, currentLine)
55+ currentLine = ""
56+ }
57+ // Hard-wrap the long word
58+ for len(word) > 0 {
59+ chunk := truncateToWidth(word, maxWidth)
60+ lines = append(lines, chunk)
61+ // Remove the chunk from word
62+ chunkRunes := []rune(chunk)
63+ wordRunes := []rune(word)
64+ if len(chunkRunes) < len(wordRunes) {
65+ word = string(wordRunes[len(chunkRunes):])
66+ } else {
67+ word = ""
68+ }
69+ }
70+ continue
71+ }
72+
73+ // Normal word wrapping
74+ if currentLine == "" {
5475 currentLine = word
76+ } else {
77+ lineWidth := lipgloss.Width(currentLine)
78+ if lineWidth+1+wordWidth <= maxWidth {
79+ currentLine += " " + word
80+ } else {
81+ lines = append(lines, currentLine)
82+ currentLine = word
83+ }
5584 }
5685 }
57- lines = append(lines, currentLine)
86+ if currentLine != "" {
87+ lines = append(lines, currentLine)
88+ }
5889 }
5990
6091 return lines
internal/tui/focus_test.go+26−0
@@ -177,6 +177,32 @@ func TestRenderLogEntryWithReasoningTruncation(t *testing.T) {
177177 }
178178 }
179179
180+func TestRenderLogEntryWithLongWord(t *testing.T) {
181+ lipgloss.SetColorProfile(termenv.TrueColor)
182+ defer lipgloss.SetColorProfile(termenv.Ascii)
183+
184+ // Simulate an AI message with a long unbreakable word (like a UUID)
185+ longID := "4c8abe29-6e71-45f4-9809-b0fba48053d3-extra-long-identifier-continuation"
186+
187+ entry := LogEntry{
188+ Role: "assistant",
189+ Source: "llm",
190+ Content: "I found the ship with ID " + longID + " and it has valuable cargo.",
191+ Timestamp: time.Now(),
192+ }
193+
194+ maxWidth := 50
195+ lines := renderLogEntryImpl(entry, maxWidth, false)
196+
197+ // Verify no line exceeds maxWidth
198+ for i, line := range lines {
199+ width := lipgloss.Width(line)
200+ if width > maxWidth {
201+ t.Errorf("Line %d exceeds maxWidth %d: got %d", i, maxWidth, width)
202+ }
203+ }
204+}
205+
180206 func TestRenderLogEntryToolWithJSON(t *testing.T) {
181207 lipgloss.SetColorProfile(termenv.TrueColor)
182208 defer lipgloss.SetColorProfile(termenv.Ascii)
internal/tui/tui_test.go+34−11
@@ -303,13 +303,13 @@ func TestRenderFocusView(t *testing.T) {
303303 {Role: "assistant", Content: "Hi there! This is a longer response that might span multiple lines when properly wrapped in the terminal window."},
304304 }
305305
306- view := RenderFocusView(mysis, logs, 80, 24, false, "⠋")
306+ view := RenderFocusView(mysis, logs, 80, 24, false, "⠋", false)
307307 if view == "" {
308308 t.Error("expected non-empty focus view")
309309 }
310310
311311 // Test with loading state
312- viewLoading := RenderFocusView(mysis, logs, 80, 24, true, "⠋")
312+ viewLoading := RenderFocusView(mysis, logs, 80, 24, true, "⠋", false)
313313 if viewLoading == "" {
314314 t.Error("expected non-empty focus view with loading")
315315 }
@@ -451,7 +451,7 @@ func TestRenderLogEntry(t *testing.T) {
451451 for _, tt := range tests {
452452 t.Run(tt.name, func(t *testing.T) {
453453 entry := LogEntry{Role: tt.role, Content: tt.content}
454- lines := renderLogEntry(entry, tt.maxWidth)
454+ lines := renderLogEntryImpl(entry, tt.maxWidth, false)
455455 if len(lines) == 0 {
456456 t.Error("expected at least one line of output")
457457 }
@@ -467,7 +467,7 @@ func TestRenderLogEntryPadding(t *testing.T) {
467467 // Test that log entries have proper padding on all sides
468468 entry := LogEntry{Role: "user", Content: "Test message"}
469469 maxWidth := 80
470- lines := renderLogEntry(entry, maxWidth)
470+ lines := renderLogEntryImpl(entry, maxWidth, false)
471471
472472 // Should have at least 2 lines: empty line (top padding) + content
473473 if len(lines) < 2 {
@@ -506,7 +506,7 @@ func TestRenderLogEntryLineWidthConsistent(t *testing.T) {
506506 Content: "First line\nSecond line\nThird line that is longer",
507507 }
508508 maxWidth := 60
509- lines := renderLogEntry(entry, maxWidth)
509+ lines := renderLogEntryImpl(entry, maxWidth, false)
510510
511511 if len(lines) < 2 {
512512 t.Fatalf("expected multiple lines, got %d", len(lines))
@@ -534,7 +534,7 @@ func TestRenderLogEntryBackgroundApplied(t *testing.T) {
534534 Content: "First line\nSecond line\nThird line",
535535 }
536536 maxWidth := 60
537- lines := renderLogEntry(entry, maxWidth)
537+ lines := renderLogEntryImpl(entry, maxWidth, false)
538538
539539 if len(lines) < 3 {
540540 t.Fatalf("expected at least 3 lines, got %d", len(lines))
@@ -564,7 +564,7 @@ func TestRenderLogEntryPaddingAllRoles(t *testing.T) {
564564 for _, role := range roles {
565565 t.Run(role, func(t *testing.T) {
566566 entry := LogEntry{Role: role, Content: "Test content"}
567- lines := renderLogEntry(entry, 80)
567+ lines := renderLogEntryImpl(entry, 80, false)
568568
569569 if len(lines) < 2 {
570570 t.Fatalf("expected at least 2 lines, got %d", len(lines))
@@ -728,7 +728,7 @@ func TestRenderFocusViewWithAllRoles(t *testing.T) {
728728 {Role: "tool", Content: "Tool result"},
729729 }
730730
731- view := RenderFocusView(mysis, logs, 100, 30, false, "⠋")
731+ view := RenderFocusView(mysis, logs, 100, 30, false, "⠋", false)
732732 if view == "" {
733733 t.Error("expected non-empty focus view")
734734 }
@@ -754,6 +754,7 @@ func TestWrapText(t *testing.T) {
754754 {"needs wrap", "Hello World", 6, 2},
755755 {"multiple paragraphs", "Line one\n\nLine three", 80, 3},
756756 {"zero width defaults", "Test", 0, 1},
757+ {"long word hard wrap", strings.Repeat("a", 100), 50, 2}, // Should hard-wrap long words
757758 }
758759
759760 for _, tt := range tests {
@@ -766,6 +767,28 @@ func TestWrapText(t *testing.T) {
766767 }
767768 }
768769
770+func TestWrapTextLongWordNoOverflow(t *testing.T) {
771+ // Test that long unbreakable words don't cause overflow
772+ longWord := strings.Repeat("a", 200)
773+ text := "Normal text " + longWord + " more text"
774+ maxWidth := 50
775+
776+ lines := wrapText(text, maxWidth)
777+
778+ // Verify no line exceeds maxWidth
779+ for i, line := range lines {
780+ width := lipgloss.Width(line)
781+ if width > maxWidth {
782+ t.Errorf("Line %d exceeds maxWidth %d: got %d\nLine: %s", i, maxWidth, width, line)
783+ }
784+ }
785+
786+ // Should have multiple lines due to hard-wrapping
787+ if len(lines) < 3 {
788+ t.Errorf("Expected at least 3 lines for long word, got %d", len(lines))
789+ }
790+}
791+
769792 func TestRenderHelpContent(t *testing.T) {
770793 help := RenderHelp(100, 40)
771794
@@ -1091,7 +1114,7 @@ func TestLogPanelWidthWithStyledContent(t *testing.T) {
10911114
10921115 var lines []string
10931116 for _, entry := range entries {
1094- entryLines := renderLogEntry(entry, panelContentWidth)
1117+ entryLines := renderLogEntryImpl(entry, panelContentWidth, false)
10951118 lines = append(lines, entryLines...)
10961119 }
10971120 viewportContent := strings.Join(lines, "\n")
@@ -1133,7 +1156,7 @@ func TestLogEntryWidthMatchesPanelContent(t *testing.T) {
11331156
11341157 // Log entries should be rendered at exactly panelInnerWidth
11351158 entry := LogEntry{Role: "assistant", Content: "Test message with enough content"}
1136- lines := renderLogEntry(entry, panelInnerWidth)
1159+ lines := renderLogEntryImpl(entry, panelInnerWidth, false)
11371160
11381161 for i, line := range lines {
11391162 lineWidth := lipgloss.Width(line)
@@ -1156,7 +1179,7 @@ func TestFocusViewAllSectionsMatchWidth(t *testing.T) {
11561179 logTitle := renderSectionTitleWithSuffix("CONVERSATION LOG", "", width)
11571180
11581181 entry := LogEntry{Role: "assistant", Content: "Test message content here"}
1159- entryLines := renderLogEntry(entry, width-4)
1182+ entryLines := renderLogEntryImpl(entry, width-4, false)
11601183 viewportContent := strings.Join(entryLines, "\n")
11611184 vpView := logStyle.Width(width - 2).Render(viewportContent)