Skip to content

fix(tui): ratatui buffer panic on undersized terminal + resume-path terminal-mode corruption #30

Closed

Description

@KooshaPari

Summary

Two related terminal-state defects surfaced during the 2026-09-18 'cricket' session when the TUI was rendered into a 57x1 pane:

Defect 1: index outside of buffer panic

thread '<unnamed>' panicked at ratatui-core-0.1.0/src/buffer/buffer.rs:250:13
index outside of buffer: the len is 57 but the index is 57

Buffer::index panics when called with (x, y) outside the buffer's Rect area. Upstream issue: ratatui/ratatui#2810.

Defect 2: resume-path terminal-mode corruption (CodeRabbit review 2026-10-05)

On the exec-resume path, init_tui_terminal_resume (src/cli/terminal.rs:623-645$) early-returns from the new L3 size gate (check_minimum_terminal_size()) without first restoring the inherited terminal modes that the *previous* jcode process preserved across the exec handoff via INHERITED_MODES_ENV/INHERITED_THEME_ENV`.

Consequence: when the new process aborts before claiming ownership via TuiRuntimeGuard, the user's terminal is left in raw mode + alternate screen + bracketed paste + focus events, with no one to clean it up. The panic hook doesn't cover this path because the gate returns an error rather than panicking.

Fix (PR #28)

KooshaPari/KCode#28 layers three defenses:

  • L1 — dep bump ratatui 0.30.0 -> 0.30.2 (pulls upstream's bounds-check helper).
  • L3 — pre-flight size gate in new jcode-terminal-guard crate: refuses to start TUI in terminals smaller than 60x20, and wraps ratatui::init in catch_unwind(AssertUnwindSafe(...)) to convert any future bounds-check regression into an anyhow::Error instead of a panic.
  • Resume-path terminal-mode restoration (commit 1ac499c9d on feature/ratatui-buffer-guard): extracted a private restore_inherited_terminal_modes_quietly() helper from the inline cleanup that was duplicated in the panic hook + signal handler, and called it from the size-gate early-return path in init_tui_terminal_resume before propagating the error. Single source of truth, no behavior change at the other call sites, idempotent and side-effect-safe on stderr.

Plus 5 regression tests in crates/jcode-tui/tests/ratatui_buffer_guard.rs + 1 unit test in src/cli/terminal.rs::tests.

Reproduction

# Resize pane to <60x20, then resume jcode via exec handoff
# -> panic on render, OR silent terminal corruption if size gate fires

Test plan

  • cargo check --workspace clean
  • cargo test -p jcode-tui --test ratatui_buffer_guard 5/5 green
  • cargo test --workspace --lib no regressions
  • cargo clippy --workspace -- -D warnings clean (pre-existing ratatui 0.30.2 cell.set_skip deprecations in jcode-tui-mermaid are out of scope; caused by L1 dep bump)

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions