Repository navigation
[CTX-1456] [0.0.27] fix(term-state): preserve rows and anchors across resize (#2529) - #2534
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 2 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
685c74d to
9ea2502
Compare
Xuepoo
left a comment
There was a problem hiding this comment.
Independent review — NEEDS-FIX
Reviewed head acb425cb (base 23a7acb1, 6 files, +894/−19) for 0.0.27 / #2529. No changes pushed, nothing merged.
Method: full diff read, targeted suites on the PR head, plus a pre-fix spot check in a separate worktree where source was reverted to 23a7acb1 and only the new test files were kept (the lone new accessor row_soft_wrapped was shimmed so the tests compile). Scoped CARGO_TARGET_DIR=.targets/review-2534.
Verified behavior
- Height-only shrink/grow keeps rows and order. Shrink absorbs unused rows below the used height first, then pushes the oldest rows into scrollback with their wrap flags; grow re-enters the newest history rows bottom-anchored. The newest viewport rows are never dropped.
push ≤ used − new_rowsandabsorb ≤ old_rows − used, soremove_top_rows/truncate_bottomcan never empty the grid — verified by construction and by invariant checks on every probe resize. - Cursor / saved cursor / primary-save. Live cursor,
saved_cursors[0], and the alt-screenprimary_savecursor all shift with the primary content; the live alt cursor is clamped.enforce_cursor_invariants+ the post-resizecheck_invariantsdebug assert hold on all tested paths. - Extremes.
resize(usize::MAX, usize::MAX)clamps to 1000×1000,resize(0,0)clamps to 1×1, and 80×24 → 1000×1000 → 1×1 → 80×24 round-trips all 50 fixture lines with no panic and no invariant failure. Same with the alt screen active (1×1 and back, primary content preserved). - No reflow when the width is unchanged. The
cols == old_colsbranch never callstake_logical_lines; wrap flags and row text are unchanged across a height shrink/grow (probe over 40 wrapped lines). - Bounded. 10 000-line scrollback width reflow: 8.7 ms narrow / 17.4 ms widen (release, 79→80) vs 2 000 lines: 1.5 ms / 2.7 ms — ≈linear in cells, no quadratic term. Height-only shrink+grow: ~14 µs per pair at both 2 k and 10 k scrollback lines (independent of history depth). Height-only time is O(rows·cols) for the used-height scan plus the row moves; width reflow is unchanged O(total cells).
- Images. Height shifts primary anchors with the content, restores on grow, clips rows that left the viewport; width reflow clears anchors and keeps
U=1virtual prototypes and image data. Alt placements are only clipped. Test and probe agree. - Alt screen. Keeps the application-owned truncate/pad path and stays bounded; primary history survives repeated aggressive resizes.
- Pre-fix spot check (source reverted, tests kept): 5 of 9 new tests fail —
height_only_shrink_keeps_newest_output_and_roundtrips,transient_one_row_height_never_squashes_output,width_roundtrip_preserves_wrap_boundary_spaces,width_roundtrip_preserves_ls_and_markdown_output,image_placement_follows_height_and_clears_on_width_reflow; 4 pass pre-fix (alt screen, hyperlink, wide-count, top-aligned). The tests genuinely gate the fix.
Blocker — progressive space insertion into wide-glyph (CJK/emoji) text
take_logical_lines now keeps every trailing blank on a soft-wrapped segment (trim_owned(&mut segment, !wrapped)), but rewrap_one_logical itself synthesizes a padding blank when a width-2 glyph straddles the right margin (if w == 2 && used + 1 == new_cols { push erased; flush wrapped }). The next unwrap therefore reads that padding as real content, and every pass through a width where a wide glyph lands at the margin materializes one space.
Deterministic repro (bitty-term-state; logical() joins rows via row_soft_wrapped and keeps intermediate boundary spaces):
let mut s = State::new();
prints(&mut s, "这是一段很长的中文段落,用来检查窗口宽度变化时是否会在字符之间插入多余的空格,从而破坏可复制文本。");
newline(&mut s);
let start = logical(&s); // exact printed paragraph
for _ in 0..5 { s.resize(37, 24); s.resize(80, 24); }
let end = logical(&s);
// end: "…中文段落,用来检查窗口 宽度变化时是否会在字符之间插入多余的 空格…"
// start != end — two spaces insertedSimpler one-step form: print AAAA中 at width 5, then resize(80, 24) → the line becomes AAAA 中.
- The same cycles on the pre-fix source are exact (that probe passes at
23a7acb1), so this is a regression introduced by this PR, not pre-existing behavior. - It changes visible and copy/search text — the exact “logical line corrupted by resize” class the issue targets — and issue #2529 explicitly names wide glyphs.
- The new
wide_and_combining_glyphs_survive_odd_width_roundtriptest cannot see it: it only counts中and asserts no orphan halves, not exact text.
Suggested direction: keep synthetic padding distinguishable from content. Either drop at most one trailing blank per wrapped segment only when the next segment starts with a width-2 lead at column 0 (this case; reflow padding is always exactly one blank), or mark padded cells in rewrap_one_logical and trim only marked cells. Both keep the PR's 79×'a' + space + "tail" case intact. Add an exact-text test (long CJK paragraph, repeated 37↔80 cycles, assert equality with the printed line) and tighten the existing wide test to compare exact logical text, not glyph counts.
Non-blocking findings (follow-ups)
- Selection does not follow the height-driven content shift.
clamp_view_bindings_to_geometryonly clamps to the new bounds;Selectionis stored in grid/viewport coordinates, so after a shrink/grow a held selection keeps its coordinates while the content moves under it (copy/search text silently changes identity). Zone/search anchors already fail closed viabuffer_epoch; either shift the selection by the resize delta or drop it on height change. The PR body’s “selection anchors continue through the existing path” is only true for bounds, not content. - Stale
scrollback.rsmodule doc: “Lines enter scrollback only via scroll-under-region operations” and “contents are immutable once written. The single exception is wholesale removal byED 3/FullReset” are now false — height-only resize pushes rows,clear_last_wrapmutates the newest line, andpop_back_lineremoves it. resize_scrollbackis not in the nextestlive-ptygroup even though it now spawns a real/bin/shPTY;.config/nextest.tomlstates that group covers “runtime integration harnesses that spawn shells”. Low flake risk today (no polling), but the grouping should be consistent.- Cursor-detach edge case: when non-blank content exists below the cursor, a shrink larger than the absorbable padding pushes the cursor’s own row into scrollback and the cursor clamps to row 0 (content intact, cursor no longer on its row). Rare; document or clamp against the cursor’s materialized row.
- 1-column reflow + line-count scrollback cap: a 1×1 round trip repacks history into ~80× more physical rows, so deep scrollback is pruned by
max_lines(500-line fixture: 586 → 225 rows after 1×1). Bounded-memory tradeoff, not new, but worth a note next to the existing wide-pair limitation.
Gates run on acb425cb
cargo test -p bitty-term-state # 224 lib + all integration suites: pass
cargo test -p bitty-runtime --test resize_scrollback # 5 passed (incl. new two-pane test)
cargo test -p bitty-ui --test resize_scrollback # 13 passed
cargo test -p bitty-terminal --bin bitty reflow # 2 passed
rustfmt --check (6 changed files); git diff --check # clean
cargo clippy -p bitty-term-state -p bitty-runtime -p bitty-ui --all-targets -- -D warnings # exit 0
Remote CI is green on this head and CodeRabbit was rate-limited (no review comments to disposition). Approval is blocked only on the wide-glyph padding item above.
|
Fixed the wide-glyph padding blocker in head |
acb425c to
7cc3fd9
Compare
|
Addressed the canonical-hash finding in head |
…2529) Height-only resizes used the truncate/pad primitive on the primary grid, dropping the newest viewport rows. A divider drag that momentarily solved a pane to one row destroyed everything above the cursor row, and growing back only padded blanks, so the content never recovered. - Shrink now absorbs unused padding below the used height, then moves the remaining oldest rows into scrollback with their wrap flags. Grow is bottom-anchored when the viewport is full: the newest history rows re-enter the grid (blank filler only when history is exhausted). - Width reflow keeps spaces at soft-wrap boundaries (only the final segment of a logical line drops trailing padding), so narrowing/widening no longer merges words. - Kitty placements follow height shifts, clip when they no longer fit, and are cleared safely on width reflow while virtual prototypes survive. - Add State::row_soft_wrapped for logical-line inspection. - Regression tests: height-only round-trip, transient one-row viewport, width round-trip with boundary spaces, ls/Markdown fixtures, wide glyphs plus combining marks at odd widths, image placement, alt-screen bounds, and a two-pane runtime divider round-trip.
…exact (#2529) The 37<->80 reflow regression inserted spaces between CJK/emoji glyphs: `rewrap_one_logical` synthesizes a margin pad when a width-2 glyph does not fit in the last column, and the previous trailing-blank preservation treated that pad as content on the next unwrap. Distinguish padding from printed content at the cell level: - `Cell::padding` is true for erased/grid-fill/rewrap filler blanks and false for application output (including printed spaces). - `take_logical_lines` drops trailing padding cells from every physical segment, so synthetic margin pads vanish while real spaces at a wrap boundary survive exactly. - Rewrap copies printed cells verbatim; only fillers are erased/padding. Tests added first (they failed on the prior head, including the reviewer's paragraph repro and the `AAAA中` one-step case): exact CJK 37<->80 cycles, emoji + mixed ASCII + real space runs through boundary widths and a one-column pass, and the synthetic-pad versus printed-space distinction. The wide/combining test now also compares exact logical text.
dedd6e5 to
3e5f37f
Compare
Summary
Fixes #2529: resizing a panel width/height permanently corrupted terminal output. A height-only resize used the primary grid's truncate/pad primitive, which drops the newest viewport rows (the bottom) instead of moving the oldest rows into scrollback. A divider drag that momentarily solved a pane to a single row therefore kept only that one row and destroyed everything above it; growing back just padded blanks, so the content never recovered. Width reflow additionally trimmed spaces at soft-wrap boundaries, merging words on the next unwrap.
Priority: P1 | Area: render | Labels: fix, P1, area:render | Milestone: v0.0.27 | RFC: — | Task: CTX-1456
Closes #2529
Root cause
State::resizeheight-only branch calledGrid::resizefor the primary grid, which copies the topnew_rowsand discards the bottom. No scrollback capture, no round-trip.take_logical_linestrimmed trailing blanks on every physical segment, including soft-wrap continuations, so a space printed in the last column disappeared when the logical line was unwrapped at a wider size.Changes
State::resize_height_only):U=1virtual prototypes (and image data) survive.State::row_soft_wrapped(row)exposes the continuation bit used by tests/accessibility to join physical rows into logical lines.Selection anchors continue through the existing
clamp_view_bindings_to_geometrypath; a width reflow bumpsbuffer_epoch, so stale zone anchors fail closed instead of resolving onto new content.Tests
New deterministic coverage in
crates/bitty-term-state/tests/resize_reflow_2529.rs:ls/Markdown fixture survives80 -> 33 -> 120 -> 41 -> 80with exact logical lines;New runtime test
two_pane_height_resize_roundtrips_pane_contentreproduces the multi-panel divider drag on a real pane session and proves the content round-trips.Commands run (scoped
CARGO_TARGET_DIR=/mnt/data/Workspace/Projects/bitty-terminal/.targets/ctx-reflow):Live evidence
Headless runtime with a real
/bin/shPTY (isolated HOME/XDG under/tmp),ls --color=neverplus a Markdowncatthat soft-wraps, resized through three geometries and back:Notes