{"record":{"id":"4ce1278427e0aac4","repo":"xai-org/grok-build","slug":"invalid-viewport-height-terminal-height","errorCode":null,"errorMessage":"Invalid viewport height: {} (terminal height: {})","messagePattern":"Invalid viewport height: (.+?) \\(terminal height: (.+?)\\)","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"warning","filePath":"crates/codegen/xai-ratatui-inline/src/resize.rs","lineNumber":123,"sourceCode":") -> io::Result<()> {\n    macro_rules! queue {\n        ($($command:expr),* $(,)?) => {{\n            $(crossterm::queue!(terminal.writer_mut(), $command)?;)*\n            Ok::<(), io::Error>(())\n        }};\n    }\n\n    let size = terminal.size()?;\n    let current_viewport = terminal.viewport_area();\n    let old_height = current_viewport.height;\n\n    if new_height == old_height {\n        return Ok(());\n    }\n\n    // Ensure new height is valid\n    if new_height == 0 || new_height >= size.height {\n        return Err(io::Error::new(\n            io::ErrorKind::InvalidInput,\n            format!(\n                \"Invalid viewport height: {} (terminal height: {})\",\n                new_height, size.height\n            ),\n        ));\n    }\n\n    if new_height > old_height {\n        // Growing: Smart expansion - try to expand down first, then push content up if needed\n        let growth = new_height - old_height;\n        let bottom_edge = current_viewport.y + current_viewport.height;\n        let space_below = size.height.saturating_sub(bottom_edge);\n\n        // Calculate the new y position\n        let new_y = if space_below >= growth {\n            // We have enough space below - expand down, keep same y\n            current_viewport.y","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-ratatui-inline/src/resize.rs#L105-L141","documentation":"resize_viewport_height validates the requested viewport height: it must be nonzero and strictly less than the current terminal height. Out-of-range requests are rejected with io::ErrorKind::InvalidInput so the inline viewport never consumes the whole terminal or collapses to nothing.","triggerScenarios":"Calling resize_viewport_height(0), or with a height >= the terminal's current row count (queried via terminal size), including when the terminal shrank between calls and a stale cached height is now too large.","commonSituations":"Handling a terminal-resize event with a stale cached terminal height; clamping math producing 0; running in a tiny/unset terminal (height 1) where any nonzero viewport is invalid; test harnesses with a mocked size.","solutions":["Clamp the requested height to 1..terminal_height before calling: let h = h.max(1).min(term_height - 1).max(1);","Re-query terminal size at resize time instead of using a cached value","Skip the resize (no-op) when the clamped height equals the current height"],"exampleFix":"// before\nresize_viewport_height(new_height)?;\n// after\nlet size = terminal::size()?;\nlet clamped = new_height.clamp(1, size.height.saturating_sub(1));\nif clamped != new_height { /* log/adjust */ }\nresize_viewport_height(clamped)?;","handlingStrategy":"validation","validationCode":"fn valid_viewport_height(h: u16, term_height: u16) -> bool {\n    h > 0 && h < term_height\n}\nlet size = crossterm::terminal::size()?;\nlet new_height = new_height.clamp(1, size.height.saturating_sub(1).max(1));","typeGuard":"fn valid_viewport_height(h: u16, term_height: u16) -> bool {\n    h > 0 && h < term_height\n}","tryCatchPattern":"if let Err(e) = resize_viewport_height(new_height) {\n    if e.kind() == io::ErrorKind::InvalidInput {\n        let size = crossterm::terminal::size()?;\n        resize_viewport_height(new_height.clamp(1, size.height.saturating_sub(1)))?;\n    } else { return Err(e.into()); }\n}","preventionTips":["Re-query terminal size on every resize event instead of caching it","Clamp user/config-supplied heights into 1..(term_height - 1)","Handle tiny terminals (height <= 2) by skipping or minimizing the viewport"],"tags":["tui","terminal","validation","rust","ratatui"],"backgroundTag":"invalid-viewport-size","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}