Repository navigation
fix(term): reject DECSTBM when the top margin is not above the bottom (bitty#3261) - #3264
Conversation
… the bottom (bitty#3261)
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Xuepoo
left a comment
There was a problem hiding this comment.
Independent review — head 04284a933df2dfe62dae776164667c0b8cb837a4
Reviewed SHA: 04284a93 (ctx-1908/fix-decstbm, base 83fda62f). Read-only on the lane worktree; every probe and the revert proof ran in separate git archive copies under my own scratch dir with their own CARGO_TARGET_DIR (rev-3264/{head,rev,base,probe}), so no artifact was shared between the fix and the reverted tree.
1. Semantics — PASS
crates/bitty-term-state/src/state.rs:3932-3952. The early return at state.rs:3944 sits before both margin writes (:3947-3948) and home_cursor() (:3949), so a rejected pair performs no partial write: t/b are locals, nothing else in the function mutates state before the guard. Confirmed behaviourally, not just by reading: after a live 4..9 region and the cursor parked at (6,2), CSI 6;6r leaves margins (4,9) and the cursor at (6,2); with DECOM set the rejected pair also leaves the cursor where it was (probe: origin=true, cursor (9,2) unchanged).
CSI r (no params) still restores full screen: parser dispatch.rs:999-1007 sets bottom = Row::SENTINEL when params.len() <= 1, set_scroll_region maps that to last — probe from a live (4,9) region lands on (0,23) on a 24-row grid; pinned by tests.rs:382.
2. Reference parity — FAIL (validity rule matches; parameter defaults do not)
Rule parity is exact. xterm charproc.c:4778-4787 (ThomasDickey/xterm-snapshots, rev 1.2134) applies the pair only inside if (bot > top), with set_tb_margins + CursorSet(0,0) both inside that branch. ghostty src/terminal/Terminal.zig:3330-3340 (setTopAndBottomMargin, main) has if (top >= bottom) return; with setCursorPos(1,1) after it. t >= b on 0-based rows is the same predicate as both, and rejection is wholesale in all three. 5;5r is correctly ignored (probe: margins survive a live 4..9 region); 0;24r and 1;2r are accepted.
The failure is parameter resolution, and it is pre-existing (crates/bitty-vt/src/parser/dispatch.rs:250-256 lead_value, :264-266 resolved_coordinate): a bottom parameter of 0 or an empty field resolves to Row(1), not to the screen bottom.
| sequence | xterm / ghostty | base 83fda62f |
this PR |
|---|---|---|---|
CSI r |
full screen | (0,23) |
(0,23) |
CSI 0;0r |
full screen (xterm bot == 0 → MaxRows, ghostty bottom_req == 0 → self.rows) |
(0,0) one-row region |
ignored, previous region kept |
CSI 1;0r |
full screen | (0,0) |
ignored |
CSI ;r |
full screen | (0,0) |
ignored |
CSI 5;5r |
ignored | (4,4) |
ignored |
So 0;0r does not resolve to full screen before or after this change. It is not a regression — the base wrote the exact degenerate 0..0 region this PR exists to eliminate, and the new outcome is strictly safer — but the "bitty now matches both exactly" claim holds for the validity predicate only, not for defaults. See residual R1.
3. Discrimination — PASS
Scratch copy, one character reverted (state.rs:3944 >= → >), separate target dir, no shared artifacts:
- reverted:
decstbm_rejects_a_one_row_regionFAILS attests.rs:322—left: (5, 5),right: (4, 9)(exactly the values the PR body predicts); full crate run on the reverted tree is 274 passed / 1 failed, i.e. this test is the only thing that moves. - head: same three tests pass (
3 passed; 272 filtered out), and the crate is 275 lib tests green.
Not tautological. decstbm_rejects_an_inverted_region and decstbm_accepts_a_two_row_region pass on both trees by design — the former pins the older branch, the latter the accepted boundary — and the body does not claim otherwise.
4. Boundary — PASS
tests.rs:355-386. CSI 6;7r on a 24-row grid → margins (5,6), cursor (0,0). CSI S then gives row7 at row 5, blank at row 6, row5 untouched at row 4 and row8 untouched at row 7 — the two-row region scrolls as a unit with its neighbours intact. CSI r afterwards restores (0, GRID_ROWS-1). Only nit: the test does not assert the cursor after that final CSI r (minor, R3).
5. Collateral — PASS
- Only two call sites exist:
state.rs:2005(theTerminalAction::SetScrollRegionarm) andstate.rs:3932. Nothing else writesscroll_region_top/bottomfrom a DECSTBM path; the other writers are resets (:774,:4022,:4133,:4412,:4433), theDECSTBM-independent save/clamp paths (:844-845,:4196-4197) andinvariants.rs:211which assertstop <= bottom. - Every DECSTBM sequence in the tree is multi-row:
paint_diff.rs:491\x1b[5;12r,bitty-vt/tests/replay.rs:205\x1b[2;10r,bitty-vt/src/parser/tests.rs:253,state/tests.rs:222,242,245,clear_residue.rs:132rows 5..20,scroll_region_per_screen.rs(whose header already documents the multi-row constraint). - Suites run:
bitty-term-statefull (275 + 2 + 19 + 3 + 4 + 8 + 1 + 4 + 5 + 19 + 4 + 4 + 3 + 4 + 4 + 22 + 16 + 2),bitty-term-state --test {clear_residue,scroll_region_per_screen,resize_reflow_2529 (incl. transient_one_row_height_never_squashes_output),tui_reference_grid_2873},bitty-vtfull (297 + 8 + 1 + 5 + 1),bitty-render --test paint_diff(3, exercises\x1b[5;12r…\x1b[rthrough parser+state). All green.
6. Gates — PASS
cargo test -p bitty-term-state --offline→ all suites ok, 0 failed.cargo test -p bitty-vt --offline→ 297 + 8 (1 ignored) + 1 + 5 + 1, 0 failed.cargo clippy -p bitty-term-state --all-targets --offline -- -D warnings→ exit 0, no diagnostics.cargo +1.85 check -p bitty-term-state --tests --offline→ exit 0 (no let-chains, no if-let guards).
7. PR body accuracy — PASS
git show 04284a93 --stat → exactly 2 files (state.rs +11/−2, state/tests.rs +78), no fixture or snapshot touched. grep -rn '1;1r' over the worktree → no hits; my wider sweep (\\x1b\[[0-9;]*r plus SetScrollRegion in fixtures) finds no other one-row region. The claimed pre-fix failure values match my revert run byte for byte.
CI at review time: macOS, MSRV 1.85, codeql, both compat matrices, Wayland/X11, supply chain, VM tier green; Windows check and test and Quality gates (1) still pending, so this verdict does not stand in for those legs.
Residual findings
- R1 (pre-existing, parser-level, not introduced here). A bottom parameter of
0/empty resolves toRow(1)(dispatch.rs:250-256, 264-266) instead of "screen bottom", soCSI 0;0r,CSI 1;0randCSI ;rare now ignored rather than restoring the full screen, where xterm (charproc.c:4779-4782,bot == 0 → MaxRows) and ghostty (Terminal.zig:3334,bottom_req == 0 → self.rows) both reset. The change still improves the base's degenerate0..0region, so it should not block this PR; worth a follow-up that maps an absent/zero bottom toRow::SENTINELin theb'r'arm, with the same probe matrix above as the acceptance check. - R2 (body precision). "a 1-row grid's region can only ever be
0..0" understates reachability: the clamp paths atstate.rs:845andstate.rs:4196-4197can also producetop == bottomon a taller grid when a stored region starts at the new last row (invariants.rs:211deliberately allows<=, so nothing breaks). The sentence is a note, not a guarantee the code now relies on — but it reads like one. - R3 (nit).
decstbm_accepts_a_two_row_regionverifies the margins after the finalCSI rbut not that the cursor homed; one assertion would close the only accepted-path behaviour the new tests don't pin.
None of these contradict the fix: the validity predicate matches both references, the rejection path is atomic, the pinning test genuinely discriminates, and nothing in the tree depended on a one-row region.
VERDICT: APPROVE
Priority: P2 | Area: term | Labels: fix,P2,area:term | Milestone: v0.1.0 | RFC: none | Task: CTX-1908
Closes #3261
Problem
set_scroll_regionacceptedDECSTBMwithtop == bottom, i.e. a one-row region. xterm and ghostty reject that pair: the margins are only applied when the top margin sits strictly above the bottom one, and the whole sequence is ignored otherwise. Bitty honoured it, producing a scroll region no reference terminal would ever enter — the shape that turns a leaked margin into a pane reduced to a single scrolling row instead of merely a misplaced one.Found while fixing #3259 (per-screen scroll margins, divergence D-1), whose reproductions originally used
CSI 1;1r; that fix moved them to a five-row region and left this compatibility change to be reviewed on its own.Fix
crates/bitty-term-state/src/state.rs,set_scroll_region: the validity check goes fromt > btot >= b.A rejected pair is still ignored wholesale — no margin write, no cursor home — the same path the inverted pair already took.
self.home_cursor()stays inside the accepted branch.Reference behaviour
charproc.c,CASE_DECSTBM): applies the pair only whentop < bottom; otherwise the sequence has no effect, cursor included.set_scroll_region): same rule,bottom > toprequired.No deliberate deviation: bitty now matches both exactly.
One consequence worth naming in review: on a transient 1-row viewport (a divider drag can solve a pane to a single row)
CSI ris now ignored as well, because0 >= 0there. That is xterm's behaviour too, and a 1-row grid's region can only ever be0..0, so no observable state differs —tests/resize_reflow_2529.rs::transient_one_row_height_never_squashes_outputstill passes untouched.Shapes covered
top == bottom, one rowCSI 5;5rtop > bottom, invertedCSI 15;6rtop + 1 == bottom, accepted boundaryCSI 6;7rCSI rTests
crates/bitty-term-state/src/state/tests.rs:decstbm_rejects_a_one_row_region— sets a live 4..9 region, parks the cursor at row 6 col 2 inside it, feedsCSI 6;6r, asserts the margins are still 4..9 and the cursor did not move (proving wholesale ignore, not a silent re-home).decstbm_rejects_an_inverted_region— thetop > bottomshape over a live 3..19 region, same assertions, so the older branch keeps its coverage through the parser.decstbm_accepts_a_two_row_region— the accepted boundary: rows 5..6 scroll as a unit while rows 4 and 7 stay put, the cursor homes to(0, 0), andCSI rthen restores the full screen.The one-row regression is load-bearing: run against the pre-fix
t > bcheck it fails withleft: (5, 5)vsright: (4, 9).Fixture audit
Nothing in the tree depended on a one-row region, so no test or fixture was changed to make this pass:
grep -rn '1;1r'across the whole worktree: no hits.\x1b[5;12rincrates/bitty-render/tests/paint_diff.rs,\x1b[1;10r/\x1b[1;5rinstate/tests.rs,\x1b[2;10rincrates/bitty-vt/tests/replay.rsand incrates/bitty-vt/src/parser/tests.rs,\x1b[4;20r/\x1b[5;10rin the new tests.scroll_region_per_screen.rs,clear_residue.rs), so they stay on the accepted side of the boundary.bitty-vtwas re-run because it owns the DECSTBM parse path.Gates
Run from the worktree with
CARGO_TARGET_DIR=/mnt/data/Workspace/Projects/bitty-terminal/.targets/ctx-1908, offline:just ci-localwas not run: github.com HTTPS is unreachable from this host, so act cannot clone its Nix action. The workspace-wide nextest/doc gates from #3259's checklist were therefore not re-run here; the crate-scoped gates above are the ones that cover this change.Docs
No compatibility-matrix or spec page asserts the old behaviour:
bitty-terminal-docshas no DECSTBM validity row (grep -rn 'top == bottom\|one-row region'over it returns nothing), andcrates/bitty-term-state/src/lib.rsdocuments only the per-screen margins divergence D-1, which this change does not touch. The new rule is recorded in theset_scroll_regiondoc comment, so no docs-only follow-up is needed.Summary by CodeRabbit