-
Notifications
You must be signed in to change notification settings - Fork 897
[codex] Stabilize renderer stress with minimal terminal changes #4570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kitenite
wants to merge
18
commits into
main
Choose a base branch
from
instant-replay-switch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5e5d88e
Reapply renderer stress fixes with instant terminal restore
Kitenite aa53739
Avoid sync terminal drain on workspace switch
Kitenite 7a91cd1
Revert risky terminal lifecycle changes
Kitenite 9116740
Fix electron-log mock shape in desktop tests
Kitenite 84c8bcb
Limit sidebar diff stat queries to active workspace
Kitenite 1b8d97a
Restore immediate workspace navigation
Kitenite 383eadc
Restore eager changes sidebar rendering
Kitenite fbef707
Stabilize renderer stress and prune terminal replay changes
Kitenite 89ec385
Remove switch-time diff stat work
Kitenite de350fb
Stabilize rapid workspace switching
Kitenite 230c7c0
Cap retained browser runtimes
Kitenite acc77c6
Merge origin/main into instant-replay-switch
Kitenite ced3bd9
Document terminal WebGL recovery
Kitenite 98df68c
Bound terminal attach close reasons
Kitenite d9ea846
Fallback terminal WebGL after context loss
Kitenite b5dc410
Guard terminal WebGL atlas pressure
Kitenite 3f7ee5e
Remove speculative renderer fixes
Kitenite c1313b3
Merge remote-tracking branch 'origin/main' into instant-replay-switch
Kitenite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Terminal WebGL Rendering | ||
|
|
||
| This note covers the terminal rendering fixes in this PR. These are renderer | ||
| fixes only; they do not change PTY lifetime, replay, serialized terminal | ||
| buffers, pane ownership, or terminal parking. | ||
|
|
||
| ## Implemented Fixes | ||
|
|
||
| - Terminal WebGL addon setup is centralized in | ||
| `apps/desktop/src/renderer/lib/terminal/terminal-webgl-addon.ts`, so both | ||
| terminal creation paths use the same lifecycle. | ||
| - WebGL loading remains optional and delayed by one animation frame. | ||
| - If `WebglAddon` fails to load, later terminals in the same renderer process | ||
| skip WebGL and use xterm's non-WebGL renderer. | ||
| - If WebGL reports context loss, the current terminal disposes its WebGL addon, | ||
| refreshes visible rows immediately, refreshes again on the next animation | ||
| frame, and marks future terminals in that renderer process for DOM fallback. | ||
| - If xterm's WebGL atlas reaches pressure, the code calls the public | ||
| `WebglAddon.clearTextureAtlas()` API and refreshes visible rows. Detection is | ||
| limited to xterm renderer state: `_charAtlas._requestClearModel === true` or | ||
| an atlas page at least `2048px`. | ||
|
|
||
| ## Why This Fix Is Local | ||
|
|
||
| Glyph corruption can happen while the copied terminal text and backend session | ||
| are still correct. That points at stale WebGL glyph atlas pixels rather than a | ||
| PTY stream or replay bug. Clearing the texture atlas forces xterm to rasterize | ||
| the visible glyphs again without rebuilding the terminal runtime or touching | ||
| the session. | ||
|
|
||
| ## Related Upstream Issue | ||
|
|
||
| `xtermjs/xterm.js#5847` | ||
| (https://github.com/xtermjs/xterm.js/issues/5847) reports WebGL row ghosting | ||
| and glyph substitution under heavy true-color output. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
apps/desktop/src/renderer/lib/terminal/terminal-image-addon.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { describe, expect, it, mock } from "bun:test"; | ||
|
|
||
| const imageAddonOptions: unknown[] = []; | ||
|
|
||
| class FakeImageAddon { | ||
| constructor(options: unknown) { | ||
| imageAddonOptions.push(options); | ||
| } | ||
| } | ||
|
|
||
| mock.module("@xterm/addon-image", () => ({ | ||
| ImageAddon: FakeImageAddon, | ||
| })); | ||
|
|
||
| const { createTerminalImageAddon, TERMINAL_IMAGE_ADDON_OPTIONS } = await import( | ||
| "./terminal-image-addon" | ||
| ); | ||
|
|
||
| describe("createTerminalImageAddon", () => { | ||
| it("bounds per-terminal image decoder memory", () => { | ||
| imageAddonOptions.length = 0; | ||
|
|
||
| const addon = createTerminalImageAddon(); | ||
|
|
||
| expect(addon).toBeInstanceOf(FakeImageAddon); | ||
| expect(imageAddonOptions).toEqual([TERMINAL_IMAGE_ADDON_OPTIONS]); | ||
| expect(TERMINAL_IMAGE_ADDON_OPTIONS).toMatchObject({ | ||
| iipSupport: true, | ||
| kittySupport: true, | ||
| pixelLimit: 1_048_576, | ||
| sixelSupport: false, | ||
| storageLimit: 16, | ||
| }); | ||
| }); | ||
| }); |
17 changes: 17 additions & 0 deletions
17
apps/desktop/src/renderer/lib/terminal/terminal-image-addon.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { type IImageAddonOptions, ImageAddon } from "@xterm/addon-image"; | ||
|
|
||
| export const TERMINAL_IMAGE_ADDON_OPTIONS = { | ||
| enableSizeReports: true, | ||
| iipSizeLimit: 8_000_000, | ||
| iipSupport: true, | ||
| kittySizeLimit: 8_000_000, | ||
| kittySupport: true, | ||
| pixelLimit: 1_048_576, | ||
| showPlaceholder: true, | ||
| sixelSupport: false, | ||
| storageLimit: 16, | ||
| } satisfies IImageAddonOptions; | ||
|
|
||
| export function createTerminalImageAddon(): ImageAddon { | ||
| return new ImageAddon(TERMINAL_IMAGE_ADDON_OPTIONS); | ||
| } |
140 changes: 140 additions & 0 deletions
140
apps/desktop/src/renderer/lib/terminal/terminal-webgl-addon.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| import { WebglAddon } from "@xterm/addon-webgl"; | ||
| import type { Terminal as XTerm } from "@xterm/xterm"; | ||
|
|
||
| type Disposable = { dispose: () => void }; | ||
|
|
||
| const ATLAS_GUARD_MIN_PAGE_SIZE = 2048; | ||
|
|
||
| // Once WebGL fails, skip it for all subsequent runtimes in this renderer. | ||
| let suggestedRendererType: "webgl" | "dom" | undefined; | ||
|
|
||
| function getObjectProperty(source: unknown, key: string): unknown { | ||
| if (!source || (typeof source !== "object" && typeof source !== "function")) { | ||
| return undefined; | ||
| } | ||
| return (source as Record<string, unknown>)[key]; | ||
| } | ||
|
|
||
| function getNumberProperty(source: unknown, key: string): number | null { | ||
| const value = getObjectProperty(source, key); | ||
| return typeof value === "number" ? value : null; | ||
| } | ||
|
|
||
| function getBooleanProperty(source: unknown, key: string): boolean | null { | ||
| const value = getObjectProperty(source, key); | ||
| return typeof value === "boolean" ? value : null; | ||
| } | ||
|
|
||
| function getTerminalRenderer(terminal: XTerm): unknown { | ||
| const core = getObjectProperty(terminal, "_core"); | ||
| const renderService = getObjectProperty(core, "_renderService"); | ||
| const rendererHolder = getObjectProperty(renderService, "_renderer"); | ||
| return getObjectProperty(rendererHolder, "value") ?? rendererHolder; | ||
| } | ||
|
|
||
| function shouldClearTextureAtlas(terminal: XTerm): boolean { | ||
| const renderer = getTerminalRenderer(terminal); | ||
| const charAtlas = getObjectProperty(renderer, "_charAtlas"); | ||
| const pendingAtlasClear = | ||
| getBooleanProperty(charAtlas, "_requestClearModel") === true; | ||
| const rawPages = getObjectProperty(charAtlas, "pages"); | ||
| if (!Array.isArray(rawPages)) return false; | ||
|
|
||
| const pageSizes = rawPages | ||
| .map((page) => | ||
| getNumberProperty(getObjectProperty(page, "canvas"), "width"), | ||
| ) | ||
| .filter((size): size is number => size !== null); | ||
|
|
||
| return ( | ||
| pendingAtlasClear || | ||
| pageSizes.some((size) => size >= ATLAS_GUARD_MIN_PAGE_SIZE) | ||
| ); | ||
| } | ||
|
|
||
| function refreshTerminal(terminal: XTerm): void { | ||
| terminal.refresh(0, Math.max(0, terminal.rows - 1)); | ||
| } | ||
|
|
||
| export function scheduleWebglAddon( | ||
| terminal: XTerm, | ||
| options: { isDisposed?: () => boolean } = {}, | ||
| ): () => void { | ||
| let disposed = false; | ||
| let webglAddon: WebglAddon | null = null; | ||
| let loadRafId: number | null = null; | ||
| let clearRafId: number | null = null; | ||
| const disposables: Disposable[] = []; | ||
|
|
||
| const isDisposed = () => disposed || (options.isDisposed?.() ?? false); | ||
|
|
||
| const cleanupWebgl = () => { | ||
| while (disposables.length > 0) { | ||
| try { | ||
| disposables.pop()?.dispose(); | ||
| } catch {} | ||
| } | ||
| try { | ||
| webglAddon?.dispose(); | ||
| } catch {} | ||
| webglAddon = null; | ||
| }; | ||
|
|
||
| const clearAtlasIfNeeded = () => { | ||
| clearRafId = null; | ||
| if (isDisposed() || !webglAddon) return; | ||
| if (!shouldClearTextureAtlas(terminal)) return; | ||
|
|
||
| try { | ||
| webglAddon.clearTextureAtlas(); | ||
| refreshTerminal(terminal); | ||
| requestAnimationFrame(() => { | ||
| if (!isDisposed()) refreshTerminal(terminal); | ||
| }); | ||
| } catch {} | ||
| }; | ||
|
|
||
| const scheduleAtlasClear = () => { | ||
| if (isDisposed() || clearRafId !== null) return; | ||
| clearRafId = requestAnimationFrame(clearAtlasIfNeeded); | ||
| }; | ||
|
|
||
| loadRafId = requestAnimationFrame(() => { | ||
| loadRafId = null; | ||
| if (isDisposed() || suggestedRendererType === "dom") return; | ||
|
|
||
| try { | ||
| webglAddon = new WebglAddon(); | ||
| disposables.push( | ||
| webglAddon.onContextLoss(() => { | ||
| suggestedRendererType = "dom"; | ||
| cleanupWebgl(); | ||
| refreshTerminal(terminal); | ||
| requestAnimationFrame(() => { | ||
| if (!isDisposed()) refreshTerminal(terminal); | ||
| }); | ||
| }), | ||
| webglAddon.onAddTextureAtlasCanvas(scheduleAtlasClear), | ||
| webglAddon.onRemoveTextureAtlasCanvas(scheduleAtlasClear), | ||
| webglAddon.onChangeTextureAtlas(scheduleAtlasClear), | ||
| ); | ||
| terminal.loadAddon(webglAddon); | ||
| } catch { | ||
| suggestedRendererType = "dom"; | ||
| cleanupWebgl(); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| disposed = true; | ||
| if (loadRafId !== null) { | ||
| cancelAnimationFrame(loadRafId); | ||
| loadRafId = null; | ||
| } | ||
| if (clearRafId !== null) { | ||
| cancelAnimationFrame(clearRafId); | ||
| clearRafId = null; | ||
| } | ||
| cleanupWebgl(); | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Gating
useDiffStatsbyopencan leave hover-card diff stats stale after reopening, because updates are event-driven and the query cache is never considered stale.Prompt for AI agents