Skip to content

fix(terminal): lay out sustained output incrementally - #869

Merged
tt-a1i merged 1 commit into
mainfrom
cursor/incremental-terminal-layout-ace9
Oct 16, 2026
Merged

tt-a1i merged 1 commit into
mainfrom
cursor/incremental-terminal-layout-ace9

Conversation

@tt-a1i

@tt-a1i tt-a1i commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

Fixes #836. Tracking context: #841.

/ps detail already coalesced output-driven repaints to 50ms and reused a layout when totalBytes did not change. Any new chunk changed that version, so the next render split, sanitized, and wrapped the entire retained stream — up to 2MiB — before taking the visible rows. At the same time, head eviction used Array.prototype.shift() on both the raw and model-safe chunk arrays, and the terminal list requested a render on every output notification.

Value

Under sustained output the per-refresh cost now follows the new tail and the lines dropped from the head, instead of the whole retention. A chatty process can keep the 50ms repaint budget, and small-chunk eviction no longer slides a 32k-element array on every write. List output bursts share one repaint; status changes still render immediately. UTF-8 boundaries, cross-chunk ANSI, carriage-return progress lines, and the model-safe projection are unchanged.

Approach

  • The detail layout cache keeps logical lines and replays a contiguous delta: code units removed from the head, plus the appended suffix. Carriage-return lines collapse to the segment that display keeps, so a progress update does not retain earlier states. A width change or a non-contiguous replacement lays the retained text out once. Sanitization still runs on every line that is laid out.
  • OutputBuffer evicts with a head index and compacts the dead prefix in batches. view().text stays lazy, and slice() can read a tail without joining the retained history. truncatedChars / retainedChars are the code-unit cursors the layout uses.
  • The /ps list coalesces output-only notifications at the same 50ms delay and renders lifecycle changes (status, errors, membership) immediately. bg_watch chunk delivery, settlement, and result delivery are untouched because coalescing is local to the dashboard subscription.

Validation

Runtime for every number below: Node v22.23.3, linux x64, this checkout. Input: the issue's ASCII row (log output plain ascii 1234567890 × 3 + newline, 100 columns) and 64-byte chunks. Samples are single-threaded performance.now() timings, not a Pi TUI CPU or keyboard-latency measurement.

Before (full buildOutputLines / shift() eviction):

Case Result
2MiB buildOutputLines, 7 calls median 62.75ms (62.60–71.58)
128KiB buildOutputLines, 7 calls median 4.20ms
1000 cache hits, unchanged 2MiB version 0.110ms
10,000 pushes after a 2MiB fill of 64-byte chunks 552.6ms
One ~100-char row after the 2MiB cap, join + full layout, 7 calls median 65.75ms (63.79–79.87)

After (same inputs on the final revision):

Case Result
2MiB cold buildOutputLines, 7 calls median 63.49ms (first paint and resize still lay the retained text out once)
1000 cache hits, unchanged version 0.153ms
10,000 pushes after a 2MiB fill of 64-byte chunks, 3 runs 17.42 / 20.80 / 23.46ms (median 20.80ms)
One ~100-char row after the 2MiB cap: incremental sync + last 30 display lines, 7 calls 0.296, 0.030, 0.020, 0.020, 0.073, 0.020, 0.013ms, median 0.020ms
That incremental result vs a full buildOutputLines of the retained text identical (20,971 lines, 2,097,100 retained chars)

bun run check passed. bun run test passed: 2662 Node tests and 1520 Vitest tests, 0 failures. CI run 38911816912 completed with conclusion success on eb459a0641e45f894445ad9f2710d52ce070f27c. No real Pi TUI CPU or input-latency measurement.

Impact

  • User-visible behavior: /ps detail still tails, scrolls, resizes, switches stdout/stderr, and shows the truncation note plus spill path. Output-only list updates may wait up to 50ms; status changes do not.
  • Model-visible context/tools: none. Model-safe text is still the stateful sanitized projection.
  • Runtime/lifecycle: detail and dashboard timers are cleared on dispose, together with the existing subscriptions.
  • Persisted config/data: none. OutputView gains truncatedChars, retainedChars, and slice for in-memory layout.
  • Compatibility or risk: a non-contiguous retention change falls back to one full layout of that frame. No real Pi TUI CPU or input-latency claim.
Open in Web Open in Cursor 

Continuous /ps refreshes rewrapped the whole 2MiB retention whenever new
output arrived, and small-chunk eviction shifted both chunk arrays. Extend
only the appended tail and the lines dropped from the head, and coalesce
output-only list repaints.

Co-authored-by: tt-a1i <[email protected]>
@github-actions github-actions Bot added the area:background-terminals Background terminal runtime, skill, or tests label Oct 16, 2026
@tt-a1i
tt-a1i marked this pull request as ready for review October 16, 2026 14:07
@tt-a1i
tt-a1i merged commit a5cb822 into main Oct 16, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:background-terminals Background terminal runtime, skill, or tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(terminal): 持续输出时全量排版 2MiB 缓冲,刷新成本超过调度间隔

2 participants