Repository navigation
[CTX-2896] fix(runtime): re-derive the grid when a DPI cell change lands (bitty#2896) - #2897
Conversation
A scale change with no physical extent at hand (the ScaleFactorChanged path without a window handle) adopted the new cell metric but left the layout container at the previous cell count. present_frames multiplies that container back into pixels with the live cell, so the grid outgrew the painted content rect by the metric ratio: the cursor-follow viewport clipped the top rows of a full-screen TUI while the PTY kept advertising the stale row count (bitty#2896). apply_dpi_scale now falls back to the configured surface extent when no physical extent is passed, the same reconcile set_window_padding already performs. An explicit zero extent (minimized) still skips the reflow.
|
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 41 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. |
Xuepoo
left a comment
There was a problem hiding this comment.
Review — PR #2897 fix(runtime): re-derive the grid when a DPI cell change lands (bitty#2896)
Independent review — I did not write this code.
- head:
4be776d1b2e4025e6ec253f95c245eee765fb335 - base:
9d57df003f5c61e5d04641189e13d92007e5343e(origin/main, fetched during review; matches the PR'sbaseRefOid) - Tree reviewed:
.worktrees/ctx-2896-fractional-scale. Working tree was clean before and after the failure-sensitivity probes (git status --shortempty).
1. Scope — PASS
M crates/bitty-runtime/src/runtime/resize.rs (+25/−…)
A crates/bitty-runtime/tests/fractional_scale_grid_fit_2896.rs (+265)
M crates/bitty-runtime/tests/runtime_resize.rs (+35/−…)
Only the resize fix, the new test file, the renamed/adjusted existing test and the doc paragraph. Cargo.lock untouched (git diff --name-only origin/main...HEAD | grep -i cargo.lock → no match).
2. Root cause — PASS (one residual noted)
The None path is real and is the runtime's own event route, not just the embedder's:
crates/bitty-runtime/src/runtime/resize.rs:845-852—WindowEventKind::ScaleFactorChanged(factor)→self.apply_dpi_scale(factor.get(), None). Every runtime-routed scale change hits theNonepath regardless of window handle.crates/bitty-terminal/src/terminal_app.rs:4990-4991— the embedder separately passesself.window.handle.as_ref().map(|win| win.inner_size()), i.e.Nonewhen no handle exists — exactly as claimed.
Before, apply_dpi_scale reflowed only under physical_extent.and_then(map_resize_to_surface_extent), so a None adoption kept the old container while the renderer cell already changed. present_frames (layout_focus.rs:1406-1411) then rebuilds area as container * live_cell, so the painted content rect and the painted row count use the new metric while self.rows/self.cols/the PTY winsize keep the old count. The claimed mechanism holds.
self.surface.extent() is the right fallback: set_window_padding does the identical reconcile (resize.rs:231-236 — grid_from_physical(extent) then reflow_to_grid), and it is the only size the runtime knows when no handle exists.
Every path after the change (resize.rs:345-351):
| input | behaviour |
|---|---|
Some(non-zero) |
reflow from that extent (unchanged) |
None + configured surface |
reflow from self.surface.extent() (new) |
Some(0,0) (minimized) |
or_else does not fire, map_resize_to_surface_extent returns None → still skips |
None + no configured surface |
still skips → the same divergence is theoretically reachable, but such a runtime has no window to clip. Acceptable, worth a one-line comment. |
Residual, not blocking: surface.extent() is the old physical size when the scale change arrives (winit delivers Resized after ScaleFactorChanged). Reflowing the new (larger) cell into the old extent under-fills until Resized arrives — no clipping, and the doc says the Resized "only refines it".
Minor doc inconsistency: resize.rs:297-300 still says the grid is derived "when physical_extent carries a non-zero extent", which the new sentence at 310-313 contradicts.
3. Invariant strength — FAIL (asserted, but unfalsifiable)
Both halves of the required assertion are present. tests/fractional_scale_grid_fit_2896.rs:73-88:
assert!(grid_h <= content.height, "scale {scale}: {0} rows * {cell_h}px = {grid_h}px overflows the {1}px content height by {2}px", frame.rows, content.height, grid_h - content.height);
// "No unused row": one more row would not fit.
assert!(content.height < grid_h + cell_h, "scale {scale}: content height {}px leaves room for another {cell_h}px row past {grid_h}px", content.height);The padding term is not written explicitly; it is folded in because grid_from_physical subtracts 2*pad before dividing (resize.rs:83-88) and content is the post-padding rect. That part is fine.
The problem is that both assertions are arithmetic identities: frame.rows is computed as content.height / ch two lines away (layout_focus.rs:1741-1744, with ch == live_cell_metrics().height, i.e. the cell_h the test multiplies back). rows*cell_h <= content.height and content.height < rows*cell_h + cell_h are therefore true for every possible input; they cannot fail. The same holds for the column pair (98-102) and for pty_size_equals_the_painted_grid_at_every_scale (126-131) under the bug, because present_frames derives the content rect from self.container, the same container the state grid follows — a stale container moves both sides together.
4. Failure sensitivity — FAIL
Reverting only the fallback (let reflow_extent = physical_extent;):
running 6 tests
test a_cell_grown_under_a_stale_container_would_overflow ... ok
test scale_adopted_without_an_extent_still_fits_the_surface ... FAILED
test pointer_in_the_last_cell_maps_to_the_last_cell_at_every_scale ... ok
test pty_size_equals_the_painted_grid_at_every_scale ... ok
test grid_fits_the_content_rect_at_every_scale ... ok
test numbered_fixture_keeps_row_one_visible_at_every_scale ... ok
thread 'scale_adopted_without_an_extent_still_fits_the_surface' panicked at crates/bitty-runtime/tests/fractional_scale_grid_fit_2896.rs:227:5:
adopting 1.6 without an explicit extent left a 2240x1320 grid in a 1500x900 surface (was 161x44 cells)
test result: FAILED. 5 passed; 1 failed
runtime_resize's renamed test goes red too (runtime_resize.rs:101, left: (80, 24) vs right: (46, 12)). So 5 of the 6 new tests pass against the code they are supposed to guard — the PR body admits this ("turns scale_adopted_… red", only that one), but it means the four-scale matrix the issue asks for is not a regression test.
The matrix can be made to have teeth with a one-line change, which I verified in the same tree:
# runtime_at(): apply_dpi_scale(scale, Some(physical_for(scale))) -> apply_dpi_scale(scale, None)
HEAD (with fallback): 6 passed; 0 failed
base (fallback reverted): 3 passed; 3 failed
pty_size_equals_the_painted_grid… left: (80, 24) right: (77, 22)
numbered_fixture_keeps_row_one… left: 24 right: 22
scale_adopted_without_an_extent… 2240x1320 in 1500x900
Driving the matrix through None is also the correct fixture: the runtime's own ScaleFactorChanged route (resize.rs:851) always passes None.
a_cell_grown_under_a_stale_container_would_overflow (245-264) exercises no product code — stale_rows = content.height / stale_cell_h, then asserts stale_rows * cell_h > content.height. It is a property of the constants and passes on every revision.
Restored: git checkout -- both files, git status --short empty, git rev-parse HEAD = 4be776d1.
5. Honesty — PASS
The body states the limitation plainly under "Scope note":
The headless default-config path (cell 9x19 → 14x30 at 1.6, window 2506x1496) already fits on
origin/mainat all four scales — that is what the new matrix records.
It does not claim the reporter's session. The 44 rows * 30 px = 1320 px in a 900 px surface figure is labelled "Cheap proof from the pre-fix code path" and is explicitly the synthetic 1.0→1.6 case ("at a 1.0 → 1.6 adoption the same hole measures 1.47x"); the reporter's 43/40 rows and 34.3 vs 37.1 px are quoted as symptom, not as a reproduction. The "6 passed / 14 passed / 19 passed" counts match my runs.
6. Changed existing test — PASS
dpi_rescale_without_extent_reconciles_from_the_surface (runtime_resize.rs:83-120) is the right contract: a no-extent adoption must reach the same grid as the physical path, and it also asserts the follow-up Resized still lands on (173, 47).
The minimized branch is not broken: Some(0,0) is Some, so or_else never consults the surface, and map_resize_to_surface_extent(0,0) returns None (bitty-platform/src/surface.rs:276-278). zero_extent_dpi_adoption_rescales_but_skips_reflow (runtime_resize.rs:158-174) still passes: ok in the 14-passed run.
7. Gates — PASS
| gate | result |
|---|---|
cargo fmt --all -- --check |
exit 0 |
cargo clippy -p bitty-runtime --all-targets -- -D warnings |
exit 0 |
cargo +1.85 check -p bitty-runtime --tests (rustc 1.85.1) |
exit 0 |
RUSTDOCFLAGS="-D warnings" cargo doc -p bitty-runtime --no-deps |
exit 0 |
cargo test -p bitty-runtime --test fractional_scale_grid_fit_2896 |
6 passed, 0 failed |
cargo test -p bitty-runtime --test runtime_resize |
14 passed, 0 failed |
cargo test -p bitty-runtime --test pointer_selection_scale |
19 passed, 0 failed |
(plugin_runtime::spawn UnresolvableTool('git') is the known container issue; I did not run the lib suite for this review.)
8. Closes discipline — PARTIAL
Closes #2896 is present (last line). Against the issue's acceptance list:
- scales 1.0/1.25/1.5/1.6 — present, but "fits exactly (no row clipped, no unused row)" is asserted vacuously (check 3).
- PTY size equals the painted grid — present (test 120), but non-discriminating under the bug (check 4).
- numbered fixture with row 1 visible — present and real (139-172).
- "a pointer test on the same fixture" — the pointer test (175-205) runs on a fresh runtime with no numbered content, not on the fixture.
Required changes (small)
tests/fractional_scale_grid_fit_2896.rs:37— adopt through the production path:rt.apply_dpi_scale(scale, None);. Verified: 6/6 green on head, 3 red against the pre-fix reflow. Without it the four-scale matrix is not a regression test.tests/fractional_scale_grid_fit_2896.rs:175— feed the01..=rowsfixture before the pointer assertion, per "on the same fixture".resize.rs:297-300— the "whenphysical_extentcarries a non-zero extent" sentence now contradicts the new paragraph at 310-313; say "when a non-zero extent is available (argument or configured surface)".- Optional: drop or replace
a_cell_grown_under_a_stale_container_would_overflow— it asserts arithmetic on constants, not behaviour.
The fix itself is correct, minimal, matches set_window_padding, and I could not break the minimized path.
VERDICT: CHANGES-REQUESTED
|
All four required changes land in 1. Scale matrix drives the production Restored: The count is 5, not 6, because item 4 removed the constants-only test. The three tests you measured red are still red, and the pointer test now goes red too now that it runs on the fixture (item 2) — it was green against the pre-fix code before. 2. Pointer test runs on the numbered fixture — fixture factored to 3. Contradictory doc sentence fixed — 4. Unfalsifiable test dropped — Gates on
|
Xuepoo
left a comment
There was a problem hiding this comment.
Re-review — PR #2897 fix(runtime): re-derive the grid when a DPI cell change lands (bitty#2896)
Independent re-review — I did not write this code.
- head:
72c88f359d5de8b3ca9242604c381b1a227bc1d8 - base:
9d57df003f5c61e5d04641189e13d92007e5343e(origin/main, matches the PR'sbaseRefOid); merge-base2f1be92ade92644cbf345129c79d716e502082ac - Tree:
.worktrees/ctx-2896-fractional-scale, clean before and after every probe (git status --shortempty at start and end,HEADstill72c88f35). - Branch is 1 commit behind
origin/main;git merge-tree --write-tree HEAD origin/mainis clean.
1. Scale matrix drives the production path — PASS
crates/bitty-runtime/tests/fractional_scale_grid_fit_2896.rs:35-46:
fn runtime_at(scale: f64) -> Runtime {
let mut rt = Runtime::with_defaults().expect("default runtime builds");
// Drive the production route, not a convenience one: the runtime's own
// `WindowEventKind::ScaleFactorChanged` handler calls
// `apply_dpi_scale(factor, None)` (resize.rs), i.e. the no-extent
// adoption whose container re-derivation is the #2896 fix. Passing the
// physical extent explicitly here would bypass the fallback and make
// every assertion below an arithmetic identity.
rt.apply_dpi_scale(scale, None);
assert_eq!(rt.dpi_scale(), scale, "scale must be adopted verbatim");
rt
}The call site it matches, crates/bitty-runtime/src/runtime/resize.rs:848-853:
WindowEventKind::ScaleFactorChanged(factor) => {
...
self.apply_dpi_scale(factor.get(), None);Same None. Every one of the four matrix tests goes through runtime_at, so the fixture is the real route, not a convenience wrapper. (The line is 852 in this head; the previous review cited 851 — one-line shift from the doc edit, same statement.)
2. Discrimination — PASS
Reverted only the fallback (resize.rs:346, physical_extent.or_else(|| self.surface.extent()) → physical_extent), then ran the matrix:
test pty_size_equals_the_painted_grid_at_every_scale ... FAILED
test scale_adopted_without_an_extent_still_fits_the_surface ... FAILED
test numbered_fixture_keeps_row_one_visible_at_every_scale ... FAILED
test grid_fits_the_content_rect_at_every_scale ... ok
test pointer_in_the_last_cell_maps_to_the_last_cell_at_every_scale ... FAILED
test result: FAILED. 1 passed; 4 failed
assertion `left == right` failed: scale 1: the state grid (what the PTY winsize follows) must match the presented frame exactly
left: (80, 24) right: (77, 22)
numbered_fixture…: left: 24 right: 22
pointer…: left: 24 right: 22
scale_adopted…: adopting 1.6 without an explicit extent left a 2240x1320 grid in a 1500x900 surface (was 161x44 cells)
4 failures (requirement: ≥3), up from 1 of 6 at the previous head. Restored with git checkout --; git status --short empty, git rev-parse HEAD = 72c88f359d5de8b3ca9242604c381b1a227bc1d8; matrix re-ran 5 passed; 0 failed.
3. Pointer test on the same numbered fixture — PASS
fractional_scale_grid_fit_2896.rs:186-233 builds its runtime with the same helper as the visibility test (:192 let (rt, rows) = numbered_fixture_at(scale);), i.e. the 01..=rows fixture from :148-160, and then asserts the resolved cell's text is the fixture's last line (:226-231):
assert_eq!(
row_text(&snapshot, cell.row as usize),
format!("{rows:02}"),
"scale {scale}: the bottom-right hit must land on the numbered \
fixture's last row"
);preceded by (cell.row, cell.col) == (frame.rows - 1, frame.cols - 1) (:218-223) and snapshot.height == rows (:194-197). Both halves of the previous gap are closed.
4. Doc agrees with the #2896 paragraph and the code — PASS
resize.rs:297-301:
297| /// `GridRenderer::apply_dpi_scale`, and — when a non-zero extent is
298| /// available (the `physical_extent` argument, else the configured surface
299| /// extent) — derives the grid from physical pixels over the scaled cells
and :311-314:
311| /// Passing `None` rescales the renderer and re-derives the grid from the
312| /// configured surface extent (issue #2896), ...
The two paragraphs now say the same thing, and both match :346-347 (physical_extent.or_else(|| self.surface.extent()) then and_then(map_resize_to_surface_extent)). The old "leaving the grid for the next Resized" sentence is gone. cargo doc with -D warnings is clean, so the intra-doc links resolve.
5. No constants-only test; tests are falsifiable — PASS, with one residual
a_cell_grown_under_a_stale_container_would_overflowis gone:grep -rn 'a_cell_grown_under_a_stale_container' crates/→ no match. The file is now 5 tests (was 6).- Pointer inversion probe (swapped the expected tuple to
(frame.cols - 1, frame.rows - 1)): red,left: (21, 76) right: (76, 21). - Fixture inversion probe (row 0 expected
"02"instead of"01"): red,left: "01" right: "02". - Both restored;
git status --shortempty,HEAD=72c88f35.
Residual (non-blocking, already noted at the previous head): grid_fits_the_content_rect_at_every_scale (:69-123) is still the one test that stays green under the reverted fallback. Its four grid assertions are arithmetic identities — PresentFrame::rows is documented as "Content grid rows derived from content (floor, at least 1)" (layout_focus.rs:163-166), and content itself is self.container * live_cell (layout_focus.rs:1406-1411), so rows*ch <= content.height < rows*ch + ch holds for every input. Its fifth assertion (painted_bottom <= physical_for(scale).height(), :116-121) is an external bound, but the bound is loose enough that the stale 24-row container never reaches it at these scales.
I am not treating this as blocking: the same invariant is pinned with teeth by pty_size_equals_the_painted_grid_at_every_scale and numbered_fixture_keeps_row_one_visible_at_every_scale, both of which go red on the revert, and the issue's "fits exactly" clause is independently pinned by the fixture test's snapshot.height == rows + last-row-in-full assertions. Worth a follow-up to rebase this test on snapshot.width/height instead of frame.rows/cols so it stops being tautological.
6. Gates on 72c88f35 — PASS
| gate | result |
|---|---|
cargo fmt --all -- --check |
exit 0 |
cargo clippy -p bitty-runtime --all-targets -- -D warnings |
exit 0, no diagnostics |
cargo +1.85 check -p bitty-runtime --tests (cargo 1.85.1) |
exit 0 |
RUSTDOCFLAGS="-D warnings" cargo doc -p bitty-runtime --no-deps |
exit 0 |
cargo test --test fractional_scale_grid_fit_2896 |
5 passed, 0 failed |
cargo test --test runtime_resize |
14 passed, 0 failed |
cargo test --test pointer_selection_scale |
19 passed, 0 failed |
The 2896 count is 5, not 6 — the constants-only test was removed. That is a reduction by design, but the PR body still reports 6 (see the notes below).
7. Scope and commit hygiene — PASS
M crates/bitty-runtime/src/runtime/resize.rs (+34/−…, 9 in the re-review commit)
A crates/bitty-runtime/tests/fractional_scale_grid_fit_2896.rs (+270)
M crates/bitty-runtime/tests/runtime_resize.rs (+35/−…)
git diff --name-only origin/main...HEAD | grep -i cargo.lock → no match; Cargo.lock untouched. Same three files against the PR's own base 9d57df0. Both commits carry the tag:
72c88f35 [CTX-2896] test(runtime): drive the scale matrix through the real dpi-scale path
4be776d1 [CTX-2896] fix(runtime): re-derive the grid when a DPI cell change lands
No unrelated refactors, no formatting churn in untouched code.
8. Closes #2896 is honest — PASS
Every acceptance clause of the issue maps to a named, executing test at all four scales (SCALES = [1.0, 1.25, 1.5, 1.6], :26):
| issue acceptance clause | test |
|---|---|
| headless test at 1.0/1.25/1.5/1.6 | SCALES, looped by all five tests |
| grid fits the content rect exactly (no row clipped, no unused row) | grid_fits_the_content_rect_at_every_scale :69-123 (both directions asserted); exactness double-pinned by snapshot.height == rows in :168-171 |
| PTY size equals the painted grid | pty_size_equals_the_painted_grid_at_every_scale :125-142 — red on revert |
| numbered fixture, row 01 visible | numbered_fixture_keeps_row_one_visible_at_every_scale :162-184 — red on revert and on inversion |
| pointer on the same fixture, last row/column → correct cell | pointer_in_the_last_cell_maps_to_the_last_cell_at_every_scale :186-233 — red on revert and on inversion |
Four of these five are genuine regression tests (proven above); the clause coverage is real, not aspirational. The "Scope note" that disclaims the reporter's session (the headless default-config path already fits on origin/main) is still present and still accurate, so the body does not overclaim a reproduction.
Non-blocking notes
- The PR body is stale relative to
72c88f35. "Failure sensitivity" still says reverting the fallback "turnsscale_adopted_without_an_extent_still_fits_the_surfacered" (four now go red, which is a better result than claimed) and still creditsa_cell_grown_under_a_stale_container_would_overflowwith pinning the 8% ratio — that test no longer exists. The Gates block reports "fractional_scale_grid_fit_2896: 6 passed"; it is 5. Worth correcting before merge so the record matches the tree. grid_fits_the_content_rect_at_every_scale— see the residual under check 5. Non-blocking, follow-up sized.
Nothing else moved: the minimized path (Some(0,0) short-circuits or_else) is still covered by zero_extent_dpi_adoption_rescales_but_skips_reflow, which passes, and dpi_rescale_without_extent_reconciles_from_the_surface now asserts parity with the explicit-extent path rather than the old "grid waits" behaviour.
All four requested changes landed, the fix is correct and minimal, the matrix now has teeth, and the gates are green on the exact head.
VERDICT: APPROVE
Root cause
Runtime::live_cell_metrics()is a single metric —grid_from_physical(sizing),present_frames/paint, andpresent_cell_in(hit-testing) all read the renderer cell, so the per-frame pixel math itself is consistent. What can diverge is when the layout container is derived from that metric.crates/bitty-runtime/src/runtime/resize.rs(apply_dpi_scale) adopted the rescaled cell and then reflowed only if the caller passed a physical extent:ScaleFactorChangedreaches that call withNonewhenever no window handle is at hand (crates/bitty-terminal/src/terminal_app.rs:self.window.handle.as_ref().map(|win| win.inner_size())), and the runtime contract doc said the grid "waits for the nextResized". In the meantime:present_framesmultiplies that container back into pixels with the new cell (area = container * live_cell), so the frame content rect, the row count painted, and the pointer mapping all use the new metric;self.cols/self.rows/ the PTY winsize stay at the old count.The grid therefore exceeds the painted content rect by the metric ratio.
viewport_snapshotfollows the cursor and clips the top rows, and the PTY keeps advertising the old row count — the exact #2896 symptom (stty sizesays 43 rows, ~40 paint, bottom row cut). The~8%in the report is the ratio between the two metrics in that session; at a 1.0 → 1.6 adoption the same hole measures 1.47x (see the red run below).Cheap proof from the pre-fix code path:
44 rows * 30 px = 1320 px of grid in a 900 px surface: 44 PTY rows vs 30 painted, top 14 rows clipped.
Invariant established
rows * painted_cell_height + 2 * padding <= content_height, withrows,painted_cell_heightand the pointer divisor taken from one metric that is re-derived together with the container:crates/bitty-runtime/tests/fractional_scale_grid_fit_2896.rs::grid_fits_the_content_rect_at_every_scale— at scale 1.0 / 1.25 / 1.5 / 1.6 on the reported 1566x935 logical window: grid fits the content rect and one more row/column would not fit (no clipped row, no unused row), and the painted grid (padding + content origin + rows*cell) stays inside the window.…::pty_size_equals_the_painted_grid_at_every_scale— the state grid the PTY winsize sizes equals the presented frame exactly.…::numbered_fixture_keeps_row_one_visible_at_every_scale— a one-screenful01..=rowsfixture keeps01on the top painted row and the last row in full (the reported clipping).…::pointer_in_the_last_cell_maps_to_the_last_cell_at_every_scale— a physical pixel in the last row/column maps to(rows-1, cols-1), so hit-testing and painting share the metric.…::scale_adopted_without_an_extent_still_fits_the_surface— the fix itself: a no-extent adoption re-derives the grid from the configured surface and the state grid follows it.Fix
apply_dpi_scalefalls back toself.surface.extent()when the caller passes no physical extent — the same reconcileset_window_paddingalready performs. An explicit zero extent (minimized/occluded) still skips the reflow, and a followingResizedstill refines the grid with the live physical size. The public doc paragraph onapply_dpi_scaleis updated to match;runtime_resize::dpi_rescale_without_extent_reconciles_from_the_surfacereplaces the old "grid waits for the next Resized" assertion with "no-extent adoption reaches the same grid as the physical path".Failure sensitivity
Reverting the one-line fallback (
let reflow_extent = physical_extent;) turnsscale_adopted_without_an_extent_still_fits_the_surfacered with the 2240x1320-in-1500x900 numbers above; restoring it is green.a_cell_grown_under_a_stale_container_would_overflowadditionally pins the reported 8% ratio: a metric 8% shorter than the painting cell produces a grid that overflows the content rect, so the matrix is tight rather than merely satisfied.Gates (real output, MSRV 1.85.0 checked separately)
Scope note
The headless default-config path (cell 9x19 → 14x30 at 1.6, window 2506x1496) already fits on
origin/mainat all four scales — that is what the new matrix records. This PR closes the reachable way that invariant could be broken at steady state: a cell metric adopted without re-deriving the container, which is what produced "PTY advertises more rows than are painted".Closes #2896