fix(terminal): cap VirtualTerminal scrollback to bound memory and render cost - #387
Open
RonenMars wants to merge 1 commit into
Open
Conversation
…der cost An append-only session grew the VirtualTerminal grid without limit; only the rendered copy was capped (slice(-maxLines)), so getLines() stayed an O(total-lines) scan on every terminal_output frame. Combined with retained background session screens this compounded the cumulative slowdown. Trim the oldest rows past a 10k cap and shift the cursor to match, so memory and per-frame cost stay bounded regardless of transcript length.
RonenMars
added a commit
that referenced
this pull request
Jul 23, 2026
RonenMars
added a commit
that referenced
this pull request
Jul 24, 2026
RonenMars
added a commit
that referenced
this pull request
Jul 26, 2026
RonenMars
added a commit
that referenced
this pull request
Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
VirtualTerminalgrew its internalgridwithout bound for an append-only session.Only the rendered copy was capped (
getLines().slice(-maxLines)), sogetLines()stayed an O(total-lines) scan run on everyterminal_outputframe, and memory grew with the full transcript.Combined with retained background session screens (see the stacked base PR) this compounded the cumulative slowdown after several sessions.
Fix
Trim the oldest rows once the grid passes a 10k cap and shift the cursor (
this.row) by the same amount so absolute row addressing stays correct.The cap sits well above any TUI screen height, so absolute cursor positioning never triggers the trim; only unbounded append-growth does.
Tests
Added a scrollback-cap case to
__tests__/unit/services/virtual-terminal.test.ts: an append-only stream past the cap stays bounded, keeps the newest line, and drops the oldest.Full VirtualTerminal suite green (220 tests).
Compatibility
Mobile-only, internal to the terminal emulator.
No server, API, or WS-contract changes.
Stacking
This PR is based on
perf/freeze-hidden-session-screens(#386), so its diff shows only the VirtualTerminal change.Merge #386 first, then this one, in integration-branch order.