#17677: fix(tui): prevent crash when rendered line exceeds terminal width
stale
size: S
## Summary
Fixes #17525 — the TUI crashes with `Rendered line N exceeds terminal width (X > Y)` when any rendered line overshoots the terminal width by even 1 character.
- Adds a `SafeContainer` class that extends `Container` and truncates any overwide line after rendering, using pi-tui's own `visibleWidth()` / `truncateToWidth()` — the same truncation technique pi-tui already applies to overlay compositing
- Replaces the TUI root container with `SafeContainer` so **all** components are protected regardless of source
- No behavior change for lines that fit within the terminal width
## AI Disclosure
AI-assisted (Claude). Fully tested — build, lint, format, and unit tests pass. The fix was verified at the unit level (Container passes overwide lines through → crash; SafeContainer truncates them → no crash), though the exact end-to-end crash from #17525 could not be reproduced on the test environment (Linux/ARM vs reporter's macOS/Node v24).
## Test plan
- [x] `pnpm build` passes
- [x] `pnpm check` passes (lint + format)
- [x] New unit tests pass (4 cases: ASCII overflow, CJK boundary, ANSI-styled overflow, passthrough for normal lines)
- [ ] Manual: run TUI, resize terminal to odd width, have agent output long lines — should not crash
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Introduces a `SafeContainer` class that wraps the TUI root container to prevent pi-tui's hard crash when any rendered line exceeds the terminal width. The fix uses pi-tui's own `visibleWidth()` and `truncateToWidth()` utilities — the same approach already used in `searchable-select-list.ts` — to silently truncate overwide lines before they reach pi-tui's render verification.
- Adds `SafeContainer` extending `Container` with a `render()` override that truncates overwide lines
- Replaces the root container in `tui.ts` with `SafeContainer`, protecting all child components
- Includes 4 unit tests covering ASCII overflow, CJK boundary, ANSI-styled overflow, and passthrough for normal lines
- Note: overlays rendered via `tui.showOverlay()` bypass the root container and are not protected by this fix, though overlay components already handle truncation internally
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge — it adds a defensive truncation layer with no behavioral change for lines that fit the terminal width.
- The change is minimal (18 lines of implementation + 75 lines of tests), uses well-established pi-tui utilities already employed elsewhere in the codebase, and is a purely defensive fix. The SafeContainer only alters behavior for lines that would otherwise crash pi-tui, making it a strict improvement with no risk of regression. No logic, syntax, or style issues found.
- No files require special attention.
<sub>Last reviewed commit: 5dba97b</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#6260: fix(tui): prevent width overflow crashes from nested ANSI escape codes
by 0xktn · 2026-02-01
84.9%
#12913: TUI: truncate lines to fit terminal width on narrow terminals
by kutayilmaaz · 2026-02-09
84.9%
#5509: Fix TUI crash when terminal window is too narrow
by omar-khaled-2 · 2026-01-31
80.1%
#17777: fix(tui): prevent URL breaking during line wrap
by proto-genesys-x · 2026-02-16
72.9%
#20516: fix(tui): preserve streamed text on finalize for pure text responses
by MisterGuy420 · 2026-02-19
72.3%
#17686: fix(memory): support non-ASCII characters in FTS query tokenization
by Phineas1500 · 2026-02-16
72.2%
#9220: Fix: TUI drops API responses silently when runID already finalized
by vishaltandale00 · 2026-02-05
71.8%
#4479: fix(tui): prevent crash when search matches ANSI escape sequences
by bee4come · 2026-01-30
71.6%
#17297: fix(tui): copy-paste inserts spaces into long tokens at wrap points...
by yinghaosang · 2026-02-15
71.6%
#23764: feat(tui): render inline images via MEDIA: protocol and pi-tui Image
by ademczuk · 2026-02-22
71.5%