Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(terminal): use webgl for animated terminal output (#106387)

Marius Andraaa29c404da0d
diff --git a/docs/published/handbook/engineering/project-structure.md b/docs/published/handbook/engineering/project-structure.md
index 9e842f19f980..6b591b40bed8 100644
--- a/docs/published/handbook/engineering/project-structure.md
+++ b/docs/published/handbook/engineering/project-structure.md
@@ -52,20 +52,6 @@ App rows show a direct star toggle when starring is their only action; rows with
The Starred section's menu opens **Configure starred**, a flat app picker that saves star changes immediately.
Drag starred items in the sidebar to change their order.
-The app-level terminal lives in `src/scenes/terminal`.
-While the terminal is open, folder menus in Files offer **Open in terminal** to change its working directory.
-SQL insights appear as editable `.sql` files; their full JSON remains editable under `/posthog/api/insight`.
-Saving SQL preserves the insight's other query options, and JSON saves send only changed fields through the existing APIs.
-`run report.sql` executes a SQL file in the current project and prints a Markdown table; `--json`, `--csv`, and `--tsv` select export formats.
-`hogql "select 1"` executes SQL directly, `echo "select 1" | hogql` reads a query from stdin, and `hogql` opens an interactive prompt that executes one query per line.
-`hogql --help` lists output formats, `--connection-id`, JSON query options, and additional fields supplied with `--field name=JSON`.
-CSV and TSV exports escape text that spreadsheets could interpret as formulas.
-Query warnings go to stderr without changing exported tables. Ctrl+C cancels a running query.
-Use `--json` to inspect result metadata, including `hasMore`, and `/tmp` for export files.
-The interactive Bash shell completes `ph` command names, aliases, connected tools, and argument names with Tab.
-The terminal follows the current resource's folder while its prompt is empty.
-Running commands, editors, and partially typed input prevent a folder change.
-
### `posthog`
The Django backend application. Key subdirectories:
diff --git a/frontend/package.json b/frontend/package.json
index cb8aa0d4fb64..6ad5ea5163dd 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -201,6 +201,7 @@
"@vapi-ai/web": "2.5.2",
"@vitejs/plugin-react": "catalog:",
"@xterm/addon-fit": "0.10.0",
+ "@xterm/addon-webgl": "0.18.0",
"@xterm/xterm": "5.5.0",
"@xyflow/react": "^12.6.0",
"bloommx": "^1.0.3",
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 7f7108dd870c..b3c38443a0c3 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -11773,9 +11773,9 @@ snapshots:
scenes-app-tasks--unified-navigation--light:
hash: v1.k794b7964.e0a6374da2da7262d08d1992bea3898259837679f5676f4330400d27559ac5d2.0GaInhE5K4q2-DGSz6KrZX6vsXX_WN_S8oNY3a1_ZzQ
scenes-app-terminal--default--dark:
- hash: v1.k794b7964.0647acd4cc4525c6a5f0f5792805408120e874f6092a92db0ecf559725e47646.ve96neC-YTgo1mBSm1W7F7LOcJAz4-F6qUaAiXrjy2E
+ hash: v1.k794b7964.785377f2897238307839a806bf86aca2d057ccd7bd6d018e1fa595beb710a588.cYamnhF72EsPyagpHu6BWCyiiQ3Vf3iTpOvluV_EncQ
scenes-app-terminal--default--light:
- hash: v1.k794b7964.09ecf1a6454895b4cc8336ea75615e27733a6f0e83e6a195fac566de956d06ba.l93c7xJK2992oHa4ojwl2B886SAirPasa7wWkpDN7L4
+ hash: v1.k794b7964.0203870eddb9785ec168788f88d4e265baf96dcf60b654c3a763b01276ad6300.zLugny6Fxd0oBZfLSlTfqIJ8qx4mhH8rPTS-RtByjwM
scenes-app-terminal--delete-confirmation--dark:
hash: v1.k794b7964.13b9d8c6669e2bb84c689af7f37e383cbd0129dbb16c8f81265683d5b470f980.6AYk7xKLc2QtwETUCrUbvudsLrkZqYxhQNzeBKcMrNY
scenes-app-terminal--delete-confirmation--light:
@@ -11789,13 +11789,17 @@ snapshots:
scenes-app-terminal--docked--light:
hash: v1.k794b7964.a34850192db6d63249686b7ef1922b1b479312471ce974d91295757a4fce059b.2_jI8KKm2oYMSRxI2Fo5n-3tOzjyxTJ3t_YS8iAH1I0
scenes-app-terminal--framebuffer--dark:
- hash: v1.k794b7964.9ef178a4201b6cf281b8832cc7862430c1129268217b2e1910613b6956e38aa0.oAMSaPpYkgMPxmGbBzUbMfvIkSxzAVh6J1uBxr6FYzw
+ hash: v1.k794b7964.52141d09fbc4787a3bc750eccbd63b990ea4a4c0b764534626ff23fd4909e33b.rG8e19jjQlcoMWnS29Ty3UNZmH9al2gXoSPgJyki70E
scenes-app-terminal--framebuffer--light:
- hash: v1.k794b7964.58823621cdfcdfd56eee0611758c30646885a202836a2508fed186c968c5f216.oMMYoKiPGFmJ8yRG_uLpZ3JVdUG5sv1GwgUcFuC9dKc
+ hash: v1.k794b7964.f83feceec8d9964b13afca4a074ff5f2dad6410fddad6ee2e7558b2e77e80a6f.4On6QTIa8J3cXMsqOALb2ryq30pSzC4dLOzlMGZCuyA
scenes-app-terminal--narrow--dark:
- hash: v1.k794b7964.36aebe98c0f44e2bfd0b6223bfc7fc33ece3cf81d6391d9baaaef157a06693f5.88obmOSvpcyTNe5SBWsYApI9WFSS4pyaJF68cZQ2MU4
+ hash: v1.k794b7964.89bb68825db8235e70e4cbd015688d51eabcabc133f1eaba23f76a0a5f5b719e.LDX1WkP7kvYWTpuNHoEbKqgO1vKDL8YC54SANoq6jsc
scenes-app-terminal--narrow--light:
- hash: v1.k794b7964.40e632562614b2fe983176d4bf14e5f204c526bd638d6a6ca778329e27dd59ae.N38an_Fe-D_BhL7P4XXobQ__tv-dkQJx6efpeH8Ep1c
+ hash: v1.k794b7964.5a0a0b9913f856809fe3c69f8752c4db457863d0f4a8318f3d733a240d14cfe0.wdB-m-LyS8gK4B83utCrE8_C6MhPUmlYG8Ox97jlSME
+ scenes-app-terminal--renderer-context-loss--dark:
+ hash: v1.k794b7964.c2ed67dc2615790a775eaf285f9263fc068c2f710ed12772f3b4b831d7c6772e.ibMmF30TNZQDRt-LvWnM96Svs7Y_zqQ0SmOFszVZLsM
+ scenes-app-terminal--renderer-context-loss--light:
+ hash: v1.k794b7964.f1952a6ffadbb4127062bb4b14aa1c256b68c49d266e318ce396f474144679d6.LJV3SHO2OvkIoN0UftLoBg9ayKvOezgGWbmTwV7PgX0
scenes-app-tracing-tracingimpactcounts--no-coverage--dark:
hash: v1.k794b7964.fcfa5be29c5721d0db9fa21ecab20f16c04a4074c9af7fb3a58311900e168a48.449yuARgMYf4b4FJpXH8evt5uiuySFxQx1sco2E2Qs0
scenes-app-tracing-tracingimpactcounts--no-coverage--light:
diff --git a/frontend/src/scenes/terminal/TerminalScene.stories.tsx b/frontend/src/scenes/terminal/TerminalScene.stories.tsx
index 05ac4373ca5a..03ce466dbb85 100644
--- a/frontend/src/scenes/terminal/TerminalScene.stories.tsx
+++ b/frontend/src/scenes/terminal/TerminalScene.stories.tsx
@@ -313,6 +313,25 @@ export default meta
export const Default: StoryObj<typeof TerminalScene> = {}
+export const RendererContextLoss: StoryObj<typeof TerminalScene> = {
+ play: async ({ canvasElement }) => {
+ await waitFor(() => expect(terminalLogic.values.status).toBe('ready'))
+ const canvas = canvasElement.querySelector<HTMLCanvasElement>('.xterm-screen canvas:not(.xterm-link-layer)')
+ const gl = canvas?.getContext('webgl2')
+ if (gl) {
+ const extension = gl.getExtension('WEBGL_lose_context')
+ expect(extension).not.toBeNull()
+ extension!.loseContext()
+ await waitFor(() => expect(canvas!.isConnected).toBe(false), { timeout: 5000 })
+ }
+ const { view } = terminalLogic.cache.session as TerminalSession
+ await new Promise<void>((resolve) => view.write('\r\nRenderer fallback ready', resolve))
+ await waitFor(() =>
+ expect(canvasElement.querySelector('.xterm-rows')?.textContent).toContain('Renderer fallback ready')
+ )
+ },
+}
+
export const LiveRuntime: StoryObj<typeof TerminalScene> = {
tags: ['!test'],
parameters: { liveRuntime: true },
diff --git a/frontend/src/scenes/terminal/TerminalSession.test.ts b/frontend/src/scenes/terminal/TerminalSession.test.ts
index c63fbcbb5431..089da38bd95c 100644
--- a/frontend/src/scenes/terminal/TerminalSession.test.ts
+++ b/frontend/src/scenes/terminal/TerminalSession.test.ts
@@ -1,7 +1,9 @@
+import { waitFor } from '@testing-library/react'
+
import { TerminalSession } from './TerminalSession'
describe('TerminalSession', () => {
- it('keeps the terminal surface out of session replay before any output renders', () => {
+ it('renders without WebGL and keeps the terminal surface out of session replay', async () => {
const session = new TerminalSession(
() => {},
() => {},
@@ -14,6 +16,8 @@ describe('TerminalSession', () => {
session.attach(container)
expect(container.firstElementChild?.classList.contains('ph-no-capture')).toBe(true)
expect(container.firstElementChild?.classList.contains('ph-replay-block')).toBe(true)
+ await new Promise<void>((resolve) => session.view.write('Terminal ready', resolve))
+ await waitFor(() => expect(container.querySelector('.xterm-rows')?.textContent).toContain('Terminal ready'))
} finally {
session.dispose()
container.remove()
diff --git a/frontend/src/scenes/terminal/TerminalSession.ts b/frontend/src/scenes/terminal/TerminalSession.ts
index d8a4712a1058..1d14844de56a 100644
--- a/frontend/src/scenes/terminal/TerminalSession.ts
+++ b/frontend/src/scenes/terminal/TerminalSession.ts
@@ -1,6 +1,7 @@
import '@xterm/xterm/css/xterm.css'
import { FitAddon } from '@xterm/addon-fit'
+import { WebglAddon } from '@xterm/addon-webgl'
import { Terminal } from '@xterm/xterm'
import { copyToClipboard } from 'lib/utils/copyToClipboard'
@@ -42,6 +43,18 @@ export class TerminalSession {
private readonly fit = new FitAddon()
private readonly observer = new ResizeObserver(() => this.resize())
+ private loadWebglRenderer(): void {
+ let renderer: WebglAddon | undefined
+ try {
+ renderer = new WebglAddon()
+ renderer.onContextLoss(() => renderer?.dispose())
+ this.view.loadAddon(renderer)
+ } catch {
+ // Unsupported or exhausted GPU contexts must leave the DOM renderer usable.
+ renderer?.dispose()
+ }
+ }
+
constructor(
write: (data: string) => void,
resize: (columns: number, rows: number) => void,
@@ -54,6 +67,7 @@ export class TerminalSession {
this.element.dataset.shortcutsAllowKeys = '` ~'
this.view.loadAddon(this.fit)
this.view.open(this.element)
+ this.loadWebglRenderer()
this.view.onData(write)
this.view.onResize(({ cols, rows }) => resize(cols, rows))
this.view.onSelectionChange(() => {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 3cc6f3a32e45..8bee00590b1e 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1004,6 +1004,9 @@ importers:
'@xterm/addon-fit':
specifier: 0.10.0
version: 0.10.0(@xterm/[email protected])
+ '@xterm/addon-webgl':
+ specifier: 0.18.0
+ version: 0.18.0(@xterm/[email protected])
'@xterm/xterm':
specifier: 5.5.0
version: 5.5.0
@@ -14131,6 +14134,11 @@ packages:
peerDependencies:
'@xterm/xterm': ^5.0.0
+ '@xterm/[email protected]':
+ resolution: {integrity: sha512-xCnfMBTI+/HKPdRnSOHaJDRqEpq2Ugy8LEj9GiY4J3zJObo3joylIFaMvzBwbYRg8zLtkO0KQaStCeSfoaI2/w==}
+ peerDependencies:
+ '@xterm/xterm': ^5.0.0
+
resolution: {integrity: sha512-hqJHYaQb5OptNunnyAnkHyM8aCjZ1MEIDTQu1iIbbTD/xops91NB5yq1ZK/dC2JDbVWtF23zUtl9JE2NqwT87A==}
@@ -34300,6 +34308,10 @@ snapshots:
dependencies:
'@xterm/xterm': 5.5.0
+ dependencies:
+ '@xterm/xterm': 5.5.0
+
'@xterm/[email protected]': {}
'@xtuc/[email protected]': {}