Skip to content

Fix stale terminal frames on worktree return: clear alt screen on snapshot restore + WebGL reveal hardening - #7133

Merged
brennanb2025 merged 3 commits into
mainfrom
brennanb2025/terminal-webgl-reveal-repaint
Jul 2, 2026
Merged

brennanb2025 merged 3 commits into
mainfrom
brennanb2025/terminal-webgl-reveal-repaint

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Symptom

Start two agents in a worktree's terminal tabs, switch to another worktree while they work, come back when they're done, and check the tabs: one shows the frozen/overlapping mid-work frame (old prompt echo and banner text bleeding through the final output) until you select text or resize. Reported by @brennanb2025; reproduced twice, and the second reproduction was captured live and dissected layer by layer.

Root cause (proven on the live specimen)

The corrupted pane is a cell-level merge inside the renderer's xterm buffer: the pre-hide frame (banner + prompt echo) bleeding through every cell the final frame leaves blank. The render model matched the buffer exactly, the WebGL context was healthy, and a forced full redraw reproduced the same pixels — the rendering stack was faithfully painting a merged buffer.

The merge happens in the hidden-output restore (applyMainBufferSnapshot):

  1. While a pane is hidden, risky TUI bytes are skipped renderer-side; at reveal, the pane is restored from the main-process buffer snapshot.
  2. For alternate-screen snapshots the destructive clear (\x1b[2J\x1b[3J\x1b[H) is deliberately skipped, to protect the normal buffer's scrollback.
  3. But the snapshot stream's own ?1049h is a no-op on a pane that's already on the alternate screen, and the serialized frame skips blank cells (cursor-forward jumps) — so nothing ever erases the pre-hide frame. Every skipped cell keeps its stale content.
  4. On a fresh terminal, ?1049h enters-and-clears the alt screen — which is why this never reproduced in fresh windows and survived earlier fix attempts (Prevent hidden terminal TUI overlap #7054, reverted Prevent stale terminal pixels on tab reveal #7058).

Live proof both ways: byte capture of the snapshot stream shows ?1049h + modes + [H + cursor-jump content with no clear; and writing the same snapshot prefixed with an alt-screen clear repaired the corrupted pane pixel-perfectly.

Fix

Primary — pty-connection.ts: for alternate-screen snapshots, write \x1b[?1049h\x1b[2J\x1b[H before the snapshot data. \x1b[2J on the alt buffer clears only the alt screen; the normal buffer's scrollback (what the original guard was protecting) is untouched, and there is still no \x1b[3J.

Hardening — WebGL reveal path (first commit, kept): a related render-level variant (buffer clean, pixels stale) was captured in an earlier session. Three defects fixed: attachWebgl could orphan a live addon on its bail path (making every recovery sweep a silent no-op for exactly the broken pane), resetWebglTextureAtlas early-returned without refreshing when the addon ref was null, and reveal recovery could fire redraw requests before the revealed pane was attached/measured (where the renderer drops them without retry). Now: dispose-on-bail + single-addon invariant, refresh-always, and a settled-frame pane-scoped repaint on tab reveal / worktree resume / window wake. No PTY resize anywhere, so the grid never drifts.

Relationship to #7054 / #7058 / #7073

#7054's hidden-output skip machinery is what makes the restore path load-bearing; #7058 (reverted in #7073) retriggered global atlas resets on reveal, which cannot repair either variant (the buffer merge is below the renderer; the render variant's per-pane reset no-op'd on orphaned refs). This PR supersedes that approach and does not re-land it.

Tests

  • pty-connection.test.ts — alt-screen restore now asserts the alt-only clear is written before the snapshot data, and still asserts the destructive 3J clear is not.
  • pane-webgl-renderer.test.ts, pane-reveal-repaint.test.ts, terminal-visibility-resume.test.ts — reveal-hardening regression tests (dispose-on-bail, repaint ordering, all three reveal seams).
  • Full terminal-pane suite: 1437 tests passing; pane-manager suite: 349 passing; typecheck + oxlint clean (verify:localization-catalog fails on main for an unrelated WorktreeList key).
  • Live validation on the reproduced specimen: fixed preamble + identical snapshot bytes → pixel-perfect final frame.

Returning to a worktree (or clicking its non-active terminal tab) while an
agent had been writing to the hidden pane could composite pre-hide pixels
over the final frame until a selection or resize forced a repaint.

Three defects lined up:
- attachWebgl's bail path nulled the addon reference without disposing, so
  a still-loaded addon kept painting stale frames while atlas resets,
  reattach checks, and diagnostics all treated the pane as DOM-rendered.
- resetWebglTextureAtlas returned early for panes without an addon
  reference, skipping even the plain refresh.
- Reveal recovery fired redraw requests before the revealed pane was
  attached and measured, where the WebGL renderer drops them silently.

Fix: dispose on every attach bail and before attaching a replacement
(single-addon invariant), refresh even when no addon is referenced, and
schedule a pane-scoped clear+refresh on a settled frame after every tab
reveal, worktree resume, and window wake. No PTY resize, so the grid
never drifts.

Co-authored-by: Orca <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 8bab14af-ce8f-4020-bbbf-5454fe1cdf08

📥 Commits

Reviewing files that changed from the base of the PR and between 64f76be and a07496d.

📒 Files selected for processing (3)
  • src/renderer/src/lib/pane-manager/pane-manager.ts
  • src/renderer/src/lib/pane-manager/pane-reveal-repaint.test.ts
  • src/renderer/src/lib/pane-manager/pane-reveal-repaint.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/renderer/src/lib/pane-manager/pane-reveal-repaint.ts
  • src/renderer/src/lib/pane-manager/pane-manager.ts
  • src/renderer/src/lib/pane-manager/pane-reveal-repaint.test.ts

📝 Walkthrough

Walkthrough

This change adds a settled-frame repaint scheduler for revealed terminal panes, exposes it through PaneManager, and calls it from terminal visibility recovery paths after WebGL atlas resets. It also hardens WebGL addon disposal and refresh behavior. Separately, alternate-screen PTY snapshot replay now clears the alternate buffer before restoring content, and the related test assertions were updated.

Related Issues: None specified.
Related PRs: None specified.
Suggested labels: area/terminal, renderer
Suggested reviewers: N/A

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description has useful detail but misses the required template sections, including Screenshots, AI Review Report, Security Audit, and Notes. Restructure it to match the template, adding Summary, Screenshots, Testing checklist, AI Review Report with macOS/Linux/Windows coverage, Security Audit, and Notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is specific and accurately summarizes the two main fixes: alt-screen restore and WebGL reveal hardening.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 723c98ee-46ac-4c81-9c48-182701d753ac

📥 Commits

Reviewing files that changed from the base of the PR and between 1ac6de1 and d318e89.

📒 Files selected for processing (8)
  • src/renderer/src/components/terminal-pane/terminal-visibility-resume.test.ts
  • src/renderer/src/components/terminal-pane/terminal-visibility-resume.ts
  • src/renderer/src/components/terminal-pane/use-terminal-pane-global-effects.test.ts
  • src/renderer/src/lib/pane-manager/pane-manager.ts
  • src/renderer/src/lib/pane-manager/pane-reveal-repaint.test.ts
  • src/renderer/src/lib/pane-manager/pane-reveal-repaint.ts
  • src/renderer/src/lib/pane-manager/pane-webgl-renderer.test.ts
  • src/renderer/src/lib/pane-manager/pane-webgl-renderer.ts

Live capture of a reproduced case showed the frozen/overlapping tab is a
cell-level merge INSIDE the renderer's buffer: the pre-hide frame (banner,
prompt echo) bleeding through every blank cell of the restored final frame.
The rendering stack was innocent — it faithfully painted a merged buffer.

The hidden-output restore deliberately skips the destructive clear for
alternate-screen snapshots to protect normal-buffer scrollback. But the
snapshot's own ?1049h is a no-op on a pane that is already on the alternate
screen, and the serialized frame skips blank cells entirely (cursor-forward
jumps), so nothing ever erases the pre-hide frame. Fresh terminals enter
the alt screen via ?1049h — which clears it — which is why the bug never
reproduced in fresh windows.

Fix: for alternate-screen snapshots, write \x1b[?1049h\x1b[2J\x1b[H before
the snapshot data. \x1b[2J on the alt buffer clears only the alt screen;
the normal buffer's scrollback (what the skip was protecting) is untouched.

Verified live on the reproduced specimen: writing the same snapshot with
this preamble repaired the pane pixel-perfectly.

Co-authored-by: Orca <[email protected]>
@brennanb2025 brennanb2025 changed the title Repaint revealed terminal panes after hidden agent output Fix stale terminal frames on worktree return: clear alt screen on snapshot restore + WebGL reveal hardening Jul 2, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/pty-connection.ts (1)

4397-4401: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Comment exceeds the 1–2 line guideline.

This "why" comment spans 5 lines. As per coding guidelines: "Keep comments short — one or two lines, capturing only the non-obvious reason."

✏️ Suggested tightening
-        // Why: the snapshot's own ?1049h is a no-op when the pane is already on
-        // the alternate screen, and the serialized frame skips blank cells — so
-        // without clearing the alt screen the pre-hide frame bleeds through
-        // every cell the final frame leaves blank. \x1b[2J on the alt buffer
-        // does not touch the normal buffer's scrollback the TUI returns to.
+        // Why: snapshot's ?1049h is a no-op if already alt; clear the alt
+        // buffer directly so blank cells don't leak the pre-hide frame.

Source: Coding guidelines


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: e58a20f5-197d-49bc-8f3c-3463473275c2

📥 Commits

Reviewing files that changed from the base of the PR and between d318e89 and 64f76be.

📒 Files selected for processing (2)
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts

One pane throwing mid-loop would silently skip repainting the remaining
panes in the same reveal batch. Also skip the settled-frame repaint after
manager destroy: repainting disposed panes could throw in attach and latch
the global WebGL attach backoff for unrelated new panes.

Co-authored-by: Orca <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant