Recover WebGL glyph atlas on renderer-risk terminal output
Summary
Rapid TUI redraws — most visibly OMP's Todos / Subagents status HUD — could corrupt xterm's shared WebGL glyph atlas without firing a GPU context-loss event. The result was garbled / mis-rendered terminal glyphs (colored blocks, wrong glyphs, partial-word substitution) that only cleared after a tab switch or text selection forced a repaint.
This change schedules a debounced WebGL atlas recovery when the PTY stream emits renderer-risk ANSI:
- inverse-video cursor rewrite frames (
\x1b[7m…\x1b[27m) - background-fill SGR panels
- complex / wide / RTL scripts
It reuses the existing post-paste recovery path (scheduleWebglAtlasRecovery) so every live same-config terminal rebuilds its render model (the atlas is shared across same-config panes, so a single-manager reset would garble the others). Plain ASCII output stays on the fast path, so the hot write loop is unaffected.
Screenshots
This bug surfaced in the wild — the symptom is OMP's status HUD (and, in the worst case, the entire transcript) rendering as a "cursed mixture" of Latin, CJK, Arabic, and Cyrillic-looking glyphs that only clears on a tab switch or text selection:
1. Todos / Subagents HUD — static headings stay readable while the dynamic rows garble into mixed-script glyphs (source):

2. Full-screen variant — under heavier redraw pressure the whole transcript corrupts (source):

No visual change in the steady state; this fixes the intermittent rendering corruption shown above. It is timing/GPU-atlas dependent and not deterministically reproducible for a clean synthetic before/after capture, so coverage is provided via regression tests (see Testing).
Testing
- [x]
pnpm lint - [x]
pnpm typecheck - [x]
pnpm test— touched-file suites pass (198/198 across the 3 changed test files). 17 unrelated failures in the full suite (window.localStorage.*jsdom-shim errors in sidebar/pr-comment tests and onerunner-command-execgit-env assertion) reproduce on a cleanmainwithout this change and are pre-existing. - [x]
pnpm build— desktop + native build succeed (only the localout/clidev symlink permission warning, which is environmental). - [x] Added regression tests: renderer-risk SGR/CSI detection in
terminal-complex-script.test.ts, andscheduleTerminalWebglAtlasRecoverydebounce/reset behavior interminal-webgl-paste-recovery.test.ts.
AI Review Report
Reviewed by AI coding agent:
- Cross-platform (macOS/Linux/Windows): No platform-specific APIs added. The detector operates on PTY byte strings; the recovery reuses the existing cross-platform
resetAllTerminalWebglAtlasesregistry path. No shortcuts, labels, or filesystem paths touched. The existing Windows-rewrite refresh branch is preserved unchanged. - SSH / remote / local: Recovery is purely renderer-side reacting to terminal output bytes — identical for local, remote, and SSH worktrees; no assumption of a local process/file/shell.
- Agent / integration / git-provider neutrality: Detection keys off generic ANSI/SGR/script shape, not any specific agent or provider. OMP's HUD is only the motivating example.
- Performance: Hot write path short-circuits on ASCII-only output before any Unicode/regex walk; recovery is debounced via a scheduled flag so bursty output cannot queue redundant resets.
- Security: No input execution, no new IPC surface, no path/secret/auth handling. Detector only reads output strings.
Security Audit
No command execution, file/path handling, auth, secrets, dependency, or IPC changes. The change reads terminal output strings to decide whether to schedule an in-renderer atlas reset. No new external input boundaries.
Notes
- The atlas reset is intentionally a shared (all live same-config panes) reset — a single-manager reset would leave sibling terminals garbled.
- Pre-existing full-suite test failures noted above are unrelated to this change (verified against clean
main).
X handle: @wolfie_
Ready to review this PR? Stage has broken it down into 3 individual chapters for you:
| Title | |
|---|---|
| 1 | Detect renderer-risk ANSI sequences in terminal output |
| 2 | Consolidate and enhance WebGL atlas recovery logic |
| 3 | Wire recovery logic into terminal and PTY layers |
Chapters generated by Stage for commit ae24e5a4d0493234d3f6ee92ff2ac2dacd926036 on Jun 22, 2026 11:16am UTC.
📝 Walkthrough
Walkthrough
The PR refines terminal refresh detection in two code paths. One path now scans SGR state once and only treats inverse-video output as refresh-worthy when it appears with rewrite-in-place cursor or erase sequences; tests cover the new cases. The foreground terminal output path now preserves split CSI sequences, treats non-ASCII output as refresh-worthy, and schedules terminal WebGL atlas recovery when refresh is forced. A shared atlas recovery scheduler is added, reused for image paste recovery, and the related import sites and tests are updated.
🚥 Pre-merge checks | ✅ 3 | ❌ 2
❌ Failed checks (2 warnings)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | ⚠️ Warning | Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
| Description check | ⚠️ Warning | The description clearly explains the bug, implementation, testing, and platform considerations. However, it omits the required linked issue and does not provide verified after visual proof; it only in… | Add the required issue reference in the Linked Issue section, such as Fixes #123``. Provide a verified after screenshot or video, or use N/A with the required justification if visual proof is genuinely impossible. Complete the repository … |
✅ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| 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. |
| Title check | ✅ Passed | The title clearly and concisely describes the primary change: recovering the WebGL glyph atlas for renderer-risk terminal output. |
Full details: Description check
Explanation
The description clearly explains the bug, implementation, testing, and platform considerations. However, it omits the required linked issue and does not provide verified after visual proof; it only includes author-provided before evidence.
Resolution
Add the required issue reference in the Linked Issue section, such as Fixes #123``. Provide a verified after screenshot or video, or use N/A with the required justification if visual proof is genuinely impossible. Complete the repository template sections and checklist as applicable.
- [ ] Fix all pre-merge checks with AI
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.
Comment @coderabbitai help to get the list of available commands.
Pushed ae24e5a4d addressing the review. Notes on each point:
Already in the diff (no change needed):
- Global fanout —
scheduleTerminalWebglAtlasRecovery()routes throughresetAllTerminalWebglAtlases()(pane-manager-registry.ts), which iterates every live manager. Not a per-pane reset. - Failure isolation — per-manager
try/catchin the registry continues after a throw; per-pane reset no-ops when!webglAddon || webglDisabledAfterContextLoss. - Public API only — recovery calls
webglAddon.clearTextureAtlas(). No_core/private renderer access. - Split-chunk detection — the write path already carries a bounded tail (
foregroundRefreshRiskScanTail+trailingIncompleteCsiSequence) and prepends it to the next chunk before scanning.
Changed:
- Renamed
terminal-webgl-paste-recovery.{ts,test.ts}→terminal-webgl-atlas-recovery.{ts,test.ts}(and thedescribelabel) since it now serves renderer-risk output, not just paste. - Documented the heuristic: renderer-risk SGR (inverse-video
7, background fills) only schedules recovery when paired with an in-place CSI rewrite (cursor move / erase / CR / BS) — SGR alone is too broad. Also documented the[120, 500]msdelay window and the burst-coalescing debounce. - Added detector tests: risk-SGR-without-rewrite → false, rewrite-without-risk-SGR → false, split-chunk rejoin → one refresh, plus a recovery re-arm test after the debounce window closes.
On the integration-test ask: the scheduler debounce is covered by the coalescing + re-arm unit tests (exactly one reset per burst window). Bytes-forwarded-unchanged is enforced upstream — the detector is read-only and the write path passes the original chunk to terminal.write() independent of the refresh decision. Skipped a headful WebGL e2e since the corruption isn't deterministically reproducible; coverage is the detector + scheduler units.
Addressed the split-escape chunk boundary in b1baef444: trailingIncompleteCsiSequence now carries a dangling lone \x1b so a CSI start split across chunks (chunk ends on ESC, next begins [H/[K) survives and still triggers atlas recovery. Resolving this thread.
Addressed in ee6152665: collapsed the two SGR passes into a single scanSgrRiskFlags walk that tracks hasBackground and hasInverseVideo together and short-circuits once both are set. Each chunk now walks its SGR matches once on the foreground hot path. Semantics unchanged (background fills win; inverse video still requires a CSI rewrite).
The module is already named terminal-webgl-atlas-recovery.ts (with matching terminal-webgl-atlas-recovery.test.ts), and all importers reference that path. This thread is stale — resolving.
Addressed in 4c9747118: expanded the gate so non-ASCII chunks also reach terminalOutputPrefersRenderRefresh, so complex/wide/RTL script redraws without CSI sequences now schedule atlas recovery. Plain-ASCII output with no CSI still short-circuits to keep the foreground hot path cheap. This matches the complex-script framing in the PR summary/tests.
Thanks for digging into this and putting up the fix.
Since this PR was opened, we’ve landed a lot of terminal rendering and WebGL atlas recovery work on main, so I’m not sure whether this exact issue is still reproducible.
Could you check against current main and let us know if you still see the rendering breakage? If it still happens, a short repro command/session or screenshot would be really helpful so we can make sure the remaining fix lands in the right place.