Skip to content

Idle animation blanks the rest of the screen: seeded partial repaint survives a swap_buffers reset #913

Closed

Description

@YalmutairiAisc

Summary

On an idle screen the TUI blanks everything except the animation rows: transcript, header and composer disappear. Typing brings them back, so the reported symptom is "the TUI blinks: I can see it while I write, it's gone when I stop".

Still present on master at v0.75.5 (crates/jcode-tui/src/tui/app/run_shell.rs, the seeded_animation_area path at ~line 402, and the swap_buffers site at ~line 429).

Root cause

draw_idle_animation_only ends in terminal.swap_buffers(). ratatui's swap_buffers resets the now-inactive buffer (Buffer::reset blanks every cell) before flipping, and that inactive buffer is the one the next tick draws into.

The seeding optimization assumes the opposite. Once seeded_animation_area == Some(area) it copies only the animated rectangle and trusts every cell outside it:

if self.seeded_animation_area == Some(area) {
    copy_cells_in(previous_frame, current_buffer, area);
} else {
    current_buffer.clone_from(previous_frame);
    self.seeded_animation_area = Some(area);
}

Against a freshly reset buffer that trust is misplaced for the whole rest of the screen. The flushed diff therefore erases the transcript, header and composer and leaves the animation alone on screen. Typing repaints it only because a changed composer trips the input_changed guard and forces a full frame.

invalidate() clears the seed, but nothing clears it at the swap, which is the moment the seed stops being valid.

Why the existing tests missed it

Every test in that module compares one rendered buffer against another. None model what ratatui does to its own buffers between frames, which is where the bug lives. It is a buffer-lifecycle mismatch, not a rendering error.

Fix

Clear the seed at the swap, so a seed is only ever reused within a tick, never across one:

/// Record that a frame was handed to the terminal and the buffers swapped.
///
/// `Terminal::swap_buffers` resets the now-inactive buffer, which is the
/// one the next tick draws into, so no part of the previous frame survives
/// there. Any partial-repaint path that skips cells because they are
/// "already correct" must call this after swapping, or it will skip cells
/// that are in fact blank and erase the screen around its own rectangle.
fn note_animation_frame_presented(&mut self) {
    self.seeded_animation_area = None;
}

called immediately after terminal.swap_buffers() in draw_idle_animation_only.

I have this running locally as a single commit against v0.75.5 (+137/-6 in run_shell.rs, all but ~10 lines being comments and two regression tests). I do not have push access to open the PR; happy to attach the patch or have someone apply it.

The two tests it adds:

  1. the_idle_animation_repaint_does_not_blank_the_rest_of_the_screen drives the two real production helpers through the real buffer lifecycle (seed, swap/reset, copy) for three consecutive ticks and asserts the cells outside the animated rectangle survive. It takes the seeding branch from the renderer's own state rather than assuming an answer, so the fix is what makes it pass.
  2. a_presented_animation_frame_requires_reseeding_the_next_tick pins the invariant on the renderer itself, so a future edit cannot reinstate the optimization without keeping the seed valid across a swap.

Environment

Windows 11 x86_64, jcode built from source. Originally hit on v0.65.0, verified by inspection that master (994b8d3, v0.75.5) still contains the unguarded path.

Related, though distinct: #289 "Flashing violently after auto updating" (closed).

Activity

  1. YalmutairiAisc commented on Aug 12, 2026

    @YalmutairiAisc
    Author

    Patch against master (rebased onto 994b8d3, v0.75.5), applies clean with git am:

    0001-fix-tui-idle-animation-blanked-everything-outside-its.patch
    From ecd54d92068a188b99c0ff8d3cc4fbd1e203db33 Mon Sep 17 00:00:00 2001
    From: YalmutairiAisc <[email protected]>
    Date: Sun, 2 Aug 2026 22:53:57 +0300
    Subject: [PATCH] fix(tui): idle animation blanked everything outside its own
     rows
    
    Reported as: the TUI blinks, visible while typing, gone when idle.
    
    draw_idle_animation_only ends in terminal.swap_buffers(), and ratatui's swap_buffers resets the now-inactive buffer (Buffer::reset blanks every cell) before flipping. The next animation tick therefore starts from a blank buffer, not from the previous frame's cells.
    
    The seeding optimization assumed the opposite: once seeded_animation_area matched, it copied only the animated rectangle and trusted every cell outside it. Against a freshly reset buffer that trust is misplaced for the whole rest of the screen, so the flushed diff erased the transcript, header and composer and left the animation alone. Typing brought it back only because a changed composer trips the input_changed guard and forces a full frame.
    
    Clear the seed at the swap, so a seed is only ever reused within a tick.
    ---
     crates/jcode-tui/src/tui/app/run_shell.rs | 143 +++++++++++++++++++++-
     1 file changed, 137 insertions(+), 6 deletions(-)
    
    diff --git a/crates/jcode-tui/src/tui/app/run_shell.rs b/crates/jcode-tui/src/tui/app/run_shell.rs
    index ad3d84e1f..35206c481 100644
    --- a/crates/jcode-tui/src/tui/app/run_shell.rs
    +++ b/crates/jcode-tui/src/tui/app/run_shell.rs
    @@ -312,6 +312,17 @@ impl StatusSpinnerRenderer {
             self.last_full_frame_input.clear();
         }
     
    +    /// Record that a frame was handed to the terminal and the buffers swapped.
    +    ///
    +    /// `Terminal::swap_buffers` resets the now-inactive buffer, which is the
    +    /// one the next tick draws into, so no part of the previous frame survives
    +    /// there. Any partial-repaint path that skips cells because they are
    +    /// "already correct" must call this after swapping, or it will skip cells
    +    /// that are in fact blank and erase the screen around its own rectangle.
    +    fn note_animation_frame_presented(&mut self) {
    +        self.seeded_animation_area = None;
    +    }
    +
         /// Whether the decorative idle-animation rows can be repainted on their own,
         /// reusing the rest of the previous frame.
         ///
    @@ -393,12 +404,25 @@ impl StatusSpinnerRenderer {
                 if current_buffer.area != previous_frame.area {
                     return Ok(false);
                 }
    -            // Seed the working buffer from the last known frame. Only the
    -            // animated rows can differ once this path owns the screen (it is the
    -            // only writer between full frames), so after the first pass we copy
    -            // just those rows instead of the whole screen. `seeded_animation_area`
    -            // records which rows that invariant covers, and any change to it (or
    -            // to the frame identity) forces one full seed again.
    +            // Seed the working buffer from the last known frame.
    +            //
    +            // Only the animated rows can differ once this path owns the screen
    +            // (it is the only writer between full frames), so in principle
    +            // later ticks could copy just those rows. That only holds while the
    +            // buffer being written still *contains* the previous tick's cells,
    +            // and after a presented frame it does not: this path ends in
    +            // `terminal.swap_buffers()`, which resets the inactive buffer to
    +            // blank cells before flipping. The next tick therefore starts from
    +            // a blank screen, and copying only the animated rectangle into it
    +            // left every other cell blank, so the flushed diff erased the
    +            // transcript, header and composer and left the animation alone on
    +            // screen. Typing brought it back only because a changed composer
    +            // trips the `input_changed` guard and forces a full frame, which is
    +            // exactly the reported "visible while I type, gone when I stop".
    +            //
    +            // `seeded_animation_area` records the rectangle a *live* seed
    +            // covers; `note_animation_frame_presented` clears it at the swap,
    +            // so a seed is only ever reused within a tick, never across one.
                 if self.seeded_animation_area == Some(area) {
                     copy_cells_in(previous_frame, current_buffer, area);
                 } else {
    @@ -427,6 +451,9 @@ impl StatusSpinnerRenderer {
                 EndSynchronizedUpdate
             )?;
             terminal.swap_buffers();
    +        // `swap_buffers` blanked the buffer the next tick will draw into, so
    +        // nothing outside the animated rectangle is seeded any more.
    +        self.note_animation_frame_presented();
             terminal.backend_mut().flush()?;
             // Keep the remembered frame current without cloning the whole screen: it
             // already matches everywhere except the rows just re-rendered. Re-render
    @@ -1354,4 +1381,108 @@ mod tests {
                 "late overlays own the status cell until the next full frame"
             );
         }
    +
    +    /// The screen must not go blank between animation ticks.
    +    ///
    +    /// Reported as "the TUI blinks: when I write I can see it, when I don't I
    +    /// don't see it". The cause is a buffer-lifecycle mismatch, not a rendering
    +    /// bug, which is why every existing test missed it: they all compare a
    +    /// single rendered buffer against another buffer, never modelling what
    +    /// ratatui does to its *own* buffers between frames.
    +    ///
    +    /// `draw_idle_animation_only` ends with `terminal.swap_buffers()`, and
    +    /// ratatui's `swap_buffers` **resets the inactive buffer** (every cell back
    +    /// to a blank space) before flipping. So on tick N+1 the buffer returned by
    +    /// `current_buffer_mut()` is not "what tick N left there", it is blank.
    +    ///
    +    /// The seeding optimization bets the opposite: once `seeded_animation_area`
    +    /// matches, it copies only the animated rectangle and assumes every cell
    +    /// outside it is still correct. Against a freshly reset buffer that bet is
    +    /// wrong for the entire rest of the screen, so the flushed diff blanks the
    +    /// transcript, header, and composer, leaving only the animation rows.
    +    ///
    +    /// Typing repaints it because a keystroke changes the composer, which trips
    +    /// the `input_changed` guard and forces a full frame. That is exactly the
    +    /// reported symptom: visible while typing, blank when idle.
    +    ///
    +    /// This test drives the two real production helpers through the real
    +    /// buffer lifecycle (seed, swap/reset, copy) and asserts the cells outside
    +    /// the animated rectangle survive.
    +    #[test]
    +    fn the_idle_animation_repaint_does_not_blank_the_rest_of_the_screen() {
    +        use super::idle_animation_repaint::copy_cells_in;
    +
    +        let area = Rect::new(0, 0, 20, 6);
    +        // A "previous full frame": chat text everywhere, animation in rows 4-5.
    +        let mut previous_frame = Buffer::empty(area);
    +        previous_frame.set_string(0, 0, "hello from the user", Style::default());
    +        previous_frame.set_string(0, 1, "an assistant reply.", Style::default());
    +        previous_frame.set_string(0, 3, "> composer text", Style::default());
    +        let animation_area = Rect::new(0, 4, 20, 2);
    +
    +        let mut renderer = StatusSpinnerRenderer {
    +            last_frame: Some(previous_frame.clone()),
    +            ..Default::default()
    +        };
    +        let mut working = Buffer::empty(area);
    +
    +        // Three consecutive animation ticks. Each one reproduces exactly what
    +        // `draw_idle_animation_only` does to the buffers, taking the seeding
    +        // branch from the renderer's own state rather than assuming an answer,
    +        // so the fix is what makes this pass.
    +        for tick in 0..3 {
    +            if renderer.seeded_animation_area == Some(animation_area) {
    +                copy_cells_in(&previous_frame, &mut working, animation_area);
    +            } else {
    +                working.clone_from(&previous_frame);
    +                renderer.seeded_animation_area = Some(animation_area);
    +            }
    +
    +            // Everything outside the animated rectangle must match the frame
    +            // this tick is reusing. Before the fix, tick 1 finds a buffer that
    +            // `swap_buffers` blanked and copies only the animation rows into
    +            // it, so these rows come back empty: the reported vanishing screen.
    +            for y in [0u16, 1, 3] {
    +                let row: String = (0..area.width).map(|x| working[(x, y)].symbol()).collect();
    +                let expected: String = (0..area.width)
    +                    .map(|x| previous_frame[(x, y)].symbol())
    +                    .collect();
    +                assert_eq!(
    +                    row.trim_end(),
    +                    expected.trim_end(),
    +                    "tick {tick}: row {y} outside the animated rectangle was \
    +                     blanked, so the idle repaint left only the animation on \
    +                     screen"
    +                );
    +            }
    +
    +            // ratatui presents the frame: `swap_buffers` resets the buffer the
    +            // next tick draws into. The renderer must be told, so it knows the
    +            // seed no longer holds.
    +            working.reset();
    +            renderer.note_animation_frame_presented();
    +        }
    +    }
    +
    +    /// The seeding invariant stated as a property of the renderer itself.
    +    ///
    +    /// The test above proves the buffer arithmetic is unsound; this one pins
    +    /// the rule that keeps it sound, so a future edit cannot quietly reinstate
    +    /// the optimization without also keeping the seed valid across a swap.
    +    #[test]
    +    fn a_presented_animation_frame_requires_reseeding_the_next_tick() {
    +        let mut renderer = StatusSpinnerRenderer {
    +            last_frame: Some(Buffer::empty(Rect::new(0, 0, 10, 3))),
    +            seeded_animation_area: Some(Rect::new(0, 1, 10, 2)),
    +            ..Default::default()
    +        };
    +
    +        renderer.note_animation_frame_presented();
    +
    +        assert_eq!(
    +            renderer.seeded_animation_area, None,
    +            "`swap_buffers` resets the buffer the next tick writes into, so \
    +             nothing outside the animated rectangle is seeded any more"
    +        );
    +    }
     }
    -- 
    2.51.0.windows.1
    
    
  2. added
    bug
    priority: high
    triage: reproducible
    autonomous: clear
    regression
    on Aug 13, 2026
  3. 1jehuang commented on Aug 13, 2026

    @1jehuang
    Owner

    I fixed the stale seeded-animation state after swap_buffers and added two real-buffer lifecycle regressions. Fix: #918.

    --- — Jcode agent (automated triage), on behalf of @1jehuang

  4. added
    triage: fixed-pending-release
    on Aug 13, 2026
  5. added a commit that references this issue on Aug 13, 2026
    95219cf
  6. added a commit that references this issue on Aug 15, 2026
    a8e0afd
  7. added a commit that references this issue on Aug 28, 2026
    877e57f
  8. added 5 commits that reference this issue on Aug 30, 2026
    4b43e21
    cd13b79
    63d64ee
    6015166
    03dce6b
  9. added a commit that references this issue on Sep 18, 2026
    c3bb610
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    autonomous: clearbugpriority: highregressiontriage: fixed-pending-releasetriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions