Files
orca/tests/e2e/terminal-inline-images-memory.spec.ts
09073086a8 feat(terminal): inline images via @xterm/addon-image (perf-first) (#19512)
* feat(terminal): inline images via @xterm/addon-image, perf-first

Add opt-in inline terminal images (SIXEL, iTerm2 IIP, Kitty graphics)
through @xterm/addon-image, designed to keep idle terminals unaffected.

Performance:
- The addon (base64-inlined wasm decoders + protocol handlers) loads off
  the boot critical path via a deferred loader that mirrors the WebGL
  addon: primed after first paint only when the setting is on, read back
  synchronously at attach, with a 3-attempt cap so a transient failure
  never disables images for the session and a missing chunk never
  refetches per pane. renderer-boot-graph guards against eager import.
- enableSizeReports:false so the addon never sets windowOptions and
  double-answers Orca's own CSI 14t/16t responder.
- Perf-tuned decode/storage limits (storageLimit, sixel/iip/kitty size
  caps) in one place.

Correctness:
- Orca's DA1 handler wins over the addon's (last-registered-first), and
  the default DA1 response never advertised Sixel (;4), so DA1-detecting
  tools (chafa, img2sixel, viu, timg) never emitted it. The winning
  handler now appends ;4 while the setting is on, resolved per query so a
  live toggle changes the next DA1; idempotent against the ConPTY
  response that already lists it.
- ORCA_IMAGE_PROTOCOL=kitty is exported to spawned shells (local, daemon,
  relay/SSH) and forwarded across the WSL boundary, so image-capable
  agents can pick an encoder. Unknown image sequences are swallowed by
  xterm when the addon is detached, so this never garbles output.
- Settings toggle (default on) gates rendering and DA1 advertisement.

Cross-checked against community PRs #7775, #11706, and #19201 at the end;
credited below.

Co-authored-by: s546126 <s546126@users.noreply.github.com>
Co-authored-by: XRX193 <XRX193@users.noreply.github.com>
Co-authored-by: lmsh7 <lmsh7@users.noreply.github.com>

* fix(terminal): bound inline image memory and classify Kitty replies

* fix(terminal): bound image decode and release image resources on cleanup

* fix(terminal): address image addon review feedback

* test(terminal): stub setPaneInlineImagesEnabled in appearance manager fakes

* fix(terminal): evict unplaced kitty payloads before displayed images

Byte-budget eviction dropped the oldest transmitted blob regardless of
placement, so a new upload could erase a visible image while abandoned
blobs still held budget. Unplaced payloads now go first and displayed
ones only when that is not enough. The incoming image is always stored,
so an oversized one overshoots the cap by one payload instead of being
dropped after the protocol already acked OK.

* fix(terminal): gate DA1 Sixel on real addon attachment; claim SSH image spec in CI

- DA1 advertised Sixel from the setting alone, so a pane whose lazy addon
  chunk was still loading (or had failed all three attempts) told
  feature-detecting tools to emit DCS that nothing could render. Track the
  attached decoder per terminal and require it before setting the ;4 bit.
- tests/e2e/terminal-inline-images-ssh.spec.ts was Docker-gated but claimed
  by no lane runner, so pr-e2e-gate-contract failed and the spec would have
  self-skipped green forever.
- Reject non-positive PNG IHDR dimensions before decode: they are parsed with
  signed shifts, so a dimension >= 0x80000000 came back negative and slipped
  past the pixel-limit comparison.
- One resolveTerminalInlineImagesEnabled() for the default-on setting; the
  four call sites mixed '?? true' with '!== false', which disagree on null.
- One readInlineImageResources() walk of the addon internals instead of two
  copies that could drift against the patched dependency.
- Isolate the deferred-attach drain per pane; make the zoom-invariance and
  backing-storage e2e assertions fail when the feature is dead.

* refactor(terminal): one lazy xterm addon loader for webgl and image

terminal-image-addon-loader was a structural clone of the webgl one — same
memo, attempt cap, and .then(ok,err)-clears-memo recovery. Both now wrap
createLazyXtermAddonLoader; each keeps its literal import() specifier so the
bundler still splits the chunk (verified against a fresh build: addon-image
stays out of the boot graph).

* refactor(terminal): name openTerminal's addon flags; pin image addon limits

Two adjacent optional booleans could be swapped without a type error once
inline images added the second one.

* docs(terminal): state the real per-pane image ceiling; drop test ordering dependency

storageLimit:32 reads like the pane's budget but keys three pools — decoded
pixels, retained encoded Kitty blobs, and pending WASM decoders — so the worst
case is ~98 MB per pane with no cross-pane governor. Say so at the constant.

pane-inline-images.test.ts's deferred case needed to run first; it now takes a
fresh module instead, and the rest prime in beforeAll. Verified by running the
file with that test moved last.

* fix(terminal): satisfy rebased static analysis gate

* fix(terminal): complete casting gate cleanup

* fix(terminal): recover failed image addon loads

* fix(terminal): bound image decoder allocations

---------

Co-authored-by: m4air <m4air@m4airs-MacBook-Air.local>
Co-authored-by: s546126 <s546126@users.noreply.github.com>
Co-authored-by: XRX193 <XRX193@users.noreply.github.com>
Co-authored-by: lmsh7 <lmsh7@users.noreply.github.com>
Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
Co-authored-by: Neil <neil@stably.ai>
2026-09-18 16:32:49 -07:00

185 lines
7.1 KiB
TypeScript

import type { Page } from '@stablyai/playwright-test'
import { writeFileSync } from 'node:fs'
import { test, expect } from './helpers/orca-app'
import { ensureTerminalVisible, waitForActiveWorktree, waitForSessionReady } from './helpers/store'
import {
execInTerminal,
waitForActiveTerminalManager,
waitForPaneIdentitySnapshot,
waitForTerminalOutput
} from './helpers/terminal'
import {
assertInlineImagePixels,
enableInlineImages,
inlineImageProducer,
readInlineImageResources,
readInlineImageState
} from './helpers/terminal-inline-image-proof'
import { nodeTerminalCommand } from './terminal-node-command'
const TAB_COUNT = 12
const CYCLES = 2
test.use({ orcaAppExtraArgs: ['--enable-precise-memory-info'] })
async function activateTab(page: Page, tabId: string): Promise<void> {
await page.evaluate((id) => window.__store!.getState().setActiveTab(id), tabId)
await expect.poll(() => page.evaluate(() => window.__store!.getState().activeTabId)).toBe(tabId)
await waitForActiveTerminalManager(page, 30_000)
}
test('twelve image terminals release decoder and image storage across reset and close cycles', async ({
orcaPage
}, testInfo) => {
test.setTimeout(360_000)
await waitForSessionReady(orcaPage)
const worktreeId = await waitForActiveWorktree(orcaPage)
await ensureTerminalVisible(orcaPage)
await waitForActiveTerminalManager(orcaPage, 30_000)
await orcaPage.evaluate(async () => {
await window.__store!.getState().updateSettings({ terminalHiddenViewParking: false })
})
await enableInlineImages(orcaPage)
const baselineTabId = (await waitForPaneIdentitySnapshot(orcaPage, 1)).tabId
const producerPath = testInfo.outputPath('image-retention-producer.cjs')
writeFileSync(
producerPath,
[
inlineImageProducer(),
'for (let id = 100; id < 106; id++) {',
"process.stdout.write('\\x1b_Ga=t,f=32,s=1,v=1,i=' + id + ',m=1,q=2;AAAA\\x1b\\\\')",
'}',
"console.log('RETENTION_DONE_' + process.argv[2])"
].join('\n')
)
const cdp = await orcaPage.context().newCDPSession(orcaPage)
const samples: unknown[] = []
const sampleHeap = async () => {
await cdp.send('HeapProfiler.collectGarbage')
return cdp.send('Runtime.getHeapUsage')
}
const baseline = await sampleHeap()
samples.push({ stage: 'baseline', heap: baseline })
const outstandingTabs = new Set<string>()
try {
for (let cycle = 0; cycle < CYCLES; cycle++) {
const tabs: { id: string; ptyId: string }[] = []
for (let index = 0; index < TAB_COUNT; index++) {
const id = await orcaPage.evaluate((worktree) => {
const state = window.__store!.getState()
const tab = state.createTab(worktree, undefined, undefined, { activate: true })
state.setActiveTab(tab.id)
state.setActiveTabType('terminal')
return tab.id
}, worktreeId)
outstandingTabs.add(id)
await activateTab(orcaPage, id)
const identity = await waitForPaneIdentitySnapshot(orcaPage, 1)
expect(identity.tabId).toBe(id)
const ptyId = identity.panes[0]?.ptyId
if (!ptyId) {
throw new Error('Image stress terminal did not bind its PTY')
}
tabs.push({ id, ptyId })
await expect.poll(() => readInlineImageState(orcaPage), { timeout: 30_000 }).not.toBeNull()
const marker = `${cycle}_${index}`
await execInTerminal(orcaPage, ptyId, nodeTerminalCommand([producerPath, marker]))
await waitForTerminalOutput(orcaPage, `RETENTION_DONE_${marker}`, 30_000)
await expect.poll(async () => (await readInlineImageState(orcaPage))?.pending).toBe(2)
if (index === TAB_COUNT - 1) {
await assertInlineImagePixels(orcaPage, testInfo.outputPath(`cycle-${cycle}-images.png`))
}
}
const loaded = await readInlineImageResources(
orcaPage,
tabs.map((tab) => tab.id)
)
expect(loaded).toHaveLength(TAB_COUNT)
for (const resource of loaded) {
expect(resource.mounted).toBe(true)
expect(resource.addon).toBe(true)
expect(resource.images).toBeGreaterThanOrEqual(3)
expect(resource.pending).toBe(2)
expect(resource.decoderBytes).toBeGreaterThan(0)
expect(resource.decoderBytes).toBeLessThanOrEqual(32_000_000)
expect(resource.blobBytes).toBeLessThanOrEqual(32_000_000)
expect(resource.storageMB).toBeLessThanOrEqual(32)
}
samples.push({ stage: `cycle-${cycle}-loaded`, resources: loaded, heap: await sampleHeap() })
for (const tab of tabs) {
await activateTab(orcaPage, tab.id)
await execInTerminal(
orcaPage,
tab.ptyId,
nodeTerminalCommand([
'-e',
"process.stdout.write('\\x1bc'); console.log('RESET_' + 'DONE')"
])
)
await waitForTerminalOutput(orcaPage, 'RESET_DONE', 30_000)
await expect
.poll(async () => {
const [resource] = await readInlineImageResources(orcaPage, [tab.id])
return {
images: resource.images,
pending: resource.pending,
decoderBytes: resource.decoderBytes,
blobBytes: resource.blobBytes
}
})
.toEqual({ images: 0, pending: 0, decoderBytes: 0, blobBytes: 0 })
}
samples.push({
stage: `cycle-${cycle}-reset`,
resources: await readInlineImageResources(
orcaPage,
tabs.map((tab) => tab.id)
),
heap: await sampleHeap()
})
await activateTab(orcaPage, baselineTabId)
for (const tab of tabs) {
await orcaPage.evaluate((id) => window.__store!.getState().closeTab(id), tab.id)
}
await expect
.poll(
async () =>
(
await readInlineImageResources(
orcaPage,
tabs.map((tab) => tab.id)
)
).filter((resource) => resource.mounted).length,
{ timeout: 30_000 }
)
.toBe(0)
for (const tab of tabs) {
outstandingTabs.delete(tab.id)
}
const closed = await sampleHeap()
samples.push({ stage: `cycle-${cycle}-closed`, heap: closed })
// GC heap/backing storage catch retained owners without requiring allocator RSS to fall.
expect(closed.usedSize).toBeLessThanOrEqual(baseline.usedSize + 64_000_000)
// Assert presence rather than guarding on it: a CDP field that stops being
// reported would otherwise delete this leak check and still pass.
expect(baseline.backingStorageSize).toBeDefined()
expect(closed.backingStorageSize).toBeDefined()
expect(closed.backingStorageSize!).toBeLessThanOrEqual(
baseline.backingStorageSize! + 32_000_000
)
}
} finally {
for (const id of outstandingTabs) {
await orcaPage
.evaluate((tab) => window.__store!.getState().closeTab(tab), id)
.catch(() => undefined)
}
writeFileSync(testInfo.outputPath('memory-measurements.json'), JSON.stringify(samples, null, 2))
await testInfo.attach('inline-image-retention-measurements', {
body: JSON.stringify(samples, null, 2),
contentType: 'application/json'
})
await cdp.detach()
}
})