mirror of
https://github.com/stablyai/orca.git
synced 2026-09-21 16:02:20 +00:00
* 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>
230 lines
7.4 KiB
JavaScript
230 lines
7.4 KiB
JavaScript
import { createRequire } from 'node:module'
|
|
import { crc32, deflateRawSync, deflateSync } from 'node:zlib'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const require = createRequire(import.meta.url)
|
|
const { Terminal } = require('@xterm/xterm')
|
|
const { ImageAddon } = require('@xterm/addon-image')
|
|
|
|
class TrackedBitmap {
|
|
width = 1
|
|
height = 1
|
|
close = vi.fn()
|
|
}
|
|
|
|
function createTerminal() {
|
|
vi.stubGlobal('ImageBitmap', TrackedBitmap)
|
|
vi.stubGlobal('window', { ImageBitmap: TrackedBitmap })
|
|
const terminal = new Terminal({ allowProposedApi: true })
|
|
const addon = new ImageAddon({
|
|
enableSizeReports: false,
|
|
storageLimit: 32,
|
|
kittySizeLimit: 8 * 1024 * 1024
|
|
})
|
|
terminal.loadAddon(addon)
|
|
return { terminal, addon, storage: addon._storage, kitty: addon._handlers.get('kitty') }
|
|
}
|
|
|
|
afterEach(() => vi.unstubAllGlobals())
|
|
|
|
describe('xterm image allocation lifecycle', () => {
|
|
it('closes alternate-buffer bitmaps when images are reset', async () => {
|
|
const { terminal, addon, storage } = createTerminal()
|
|
try {
|
|
await new Promise((resolve) => terminal.write('\x1b[?1049h', resolve))
|
|
const bitmap = new TrackedBitmap()
|
|
storage.addImage(bitmap, { scrolling: true, layer: 'top', zIndex: 0, cursorPos: 'iip' })
|
|
expect(storage._images.size).toBe(1)
|
|
addon.reset()
|
|
expect(storage._images.size).toBe(0)
|
|
expect(bitmap.close).toHaveBeenCalledOnce()
|
|
} finally {
|
|
terminal.dispose()
|
|
}
|
|
})
|
|
|
|
it.each(['reset', 'dispose'])(
|
|
'discards IIP decode that finishes after addon %s',
|
|
async (action) => {
|
|
const { terminal, addon, storage } = createTerminal()
|
|
try {
|
|
let finishDecode
|
|
vi.stubGlobal(
|
|
'createImageBitmap',
|
|
() =>
|
|
new Promise((resolve) => {
|
|
finishDecode = resolve
|
|
})
|
|
)
|
|
const iip = addon._handlers.get('iip')
|
|
const png =
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIHWP4z8DwHwAFgAI/ScLbtAAAAABJRU5ErkJggg=='
|
|
const payload = Uint32Array.from(`File=inline=1:${png}`, (char) => char.codePointAt(0))
|
|
iip.start()
|
|
iip.put(payload, 0, payload.length)
|
|
const decoding = iip.end(true)
|
|
expect(finishDecode).toBeTypeOf('function')
|
|
addon[action]()
|
|
const bitmap = new TrackedBitmap()
|
|
finishDecode(bitmap)
|
|
await decoding
|
|
expect(storage._images.size).toBe(0)
|
|
expect(bitmap.close).toHaveBeenCalledOnce()
|
|
} finally {
|
|
terminal.dispose()
|
|
}
|
|
}
|
|
)
|
|
|
|
it.each(['hide', 'dispose'])('closes placeholder bitmap finishing after %s', async (action) => {
|
|
const { terminal, addon } = createTerminal()
|
|
try {
|
|
const renderer = addon._renderer
|
|
const context = {
|
|
createImageData: (width, height) => ({ data: new Uint8ClampedArray(width * height * 4) }),
|
|
putImageData: () => {},
|
|
drawImage: () => {}
|
|
}
|
|
vi.stubGlobal('document', { createElement: () => ({ getContext: () => context }) })
|
|
vi.stubGlobal('screen', { width: 800 })
|
|
let finishDecode
|
|
vi.stubGlobal(
|
|
'createImageBitmap',
|
|
() =>
|
|
new Promise((resolve) => {
|
|
finishDecode = resolve
|
|
})
|
|
)
|
|
renderer._createPlaceHolder(24)
|
|
if (action === 'dispose') {
|
|
addon.dispose()
|
|
} else {
|
|
renderer.showPlaceholder(false)
|
|
}
|
|
const bitmap = new TrackedBitmap()
|
|
finishDecode(bitmap)
|
|
await Promise.resolve()
|
|
expect(bitmap.close).toHaveBeenCalledOnce()
|
|
expect(renderer._placeholderBitmap).toBeUndefined()
|
|
} finally {
|
|
terminal.dispose()
|
|
}
|
|
})
|
|
|
|
it('rejects oversized PNG headers before native bitmap allocation', async () => {
|
|
const { terminal, kitty } = createTerminal()
|
|
try {
|
|
const decode = vi.fn(async () => new TrackedBitmap())
|
|
vi.stubGlobal('createImageBitmap', decode)
|
|
window.createImageBitmap = decode
|
|
const png = Buffer.from(
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIHWP4z8DwHwAFgAI/ScLbtAAAAABJRU5ErkJggg==',
|
|
'base64'
|
|
)
|
|
png.writeUInt32BE(100_000, 16)
|
|
png.writeUInt32BE(100_000, 20)
|
|
png.writeUInt32BE(crc32(png.subarray(12, 29)), 29)
|
|
const result = await kitty._createBitmap({ format: 100, data: new Blob([png]) }).then(
|
|
() => 'accepted',
|
|
() => 'rejected'
|
|
)
|
|
expect(decode).not.toHaveBeenCalled()
|
|
expect(result).toBe('rejected')
|
|
} finally {
|
|
terminal.dispose()
|
|
}
|
|
})
|
|
|
|
it.each([deflateSync, deflateRawSync])(
|
|
'preserves valid compressed image bytes (%#)',
|
|
async (compress) => {
|
|
const { terminal, kitty } = createTerminal()
|
|
try {
|
|
const original = Buffer.from([1, 2, 3, 4])
|
|
expect(Buffer.from(await kitty._decompressZlib(compress(original)))).toEqual(original)
|
|
} finally {
|
|
terminal.dispose()
|
|
}
|
|
}
|
|
)
|
|
|
|
it('contains malformed compressed streams without unhandled writer rejections', async () => {
|
|
const { terminal, kitty } = createTerminal()
|
|
try {
|
|
await expect(kitty._decompressZlib(new Uint8Array([1, 2, 3, 4]))).rejects.toThrow()
|
|
} finally {
|
|
terminal.dispose()
|
|
}
|
|
})
|
|
|
|
it('rejects compressed data expanding beyond the decoded-image budget', async () => {
|
|
const { terminal, kitty } = createTerminal()
|
|
try {
|
|
const compressed = deflateSync(Buffer.alloc(64 * 1024 * 1024))
|
|
expect(compressed.byteLength).toBeLessThan(100_000)
|
|
const result = await kitty._decompressZlib(compressed).then(
|
|
(bytes) => ({ decodedBytes: bytes.byteLength }),
|
|
() => ({ rejected: true })
|
|
)
|
|
expect(result).toEqual({ rejected: true })
|
|
} finally {
|
|
terminal.dispose()
|
|
}
|
|
})
|
|
|
|
it.each([{ x: 1 }, { columns: 2 }])(
|
|
'discards Kitty image reset during crop or resize (%#)',
|
|
async (command) => {
|
|
const { terminal, addon, storage, kitty } = createTerminal()
|
|
try {
|
|
const original = new TrackedBitmap()
|
|
original.width = original.height = 10
|
|
kitty._createBitmap = async () => original
|
|
let finishTransform
|
|
vi.stubGlobal(
|
|
'createImageBitmap',
|
|
() =>
|
|
new Promise((resolve) => {
|
|
finishTransform = resolve
|
|
})
|
|
)
|
|
const decoding = kitty._displayImage({ id: 1 }, command)
|
|
await Promise.resolve()
|
|
expect(finishTransform).toBeTypeOf('function')
|
|
addon.reset()
|
|
const transformed = new TrackedBitmap()
|
|
finishTransform(transformed)
|
|
await decoding
|
|
expect(storage._images.size).toBe(0)
|
|
expect(original.close).toHaveBeenCalledOnce()
|
|
expect(transformed.close).toHaveBeenCalledOnce()
|
|
} finally {
|
|
terminal.dispose()
|
|
}
|
|
}
|
|
)
|
|
|
|
it.each(['reset', 'dispose'])(
|
|
'discards Kitty decode that finishes after addon %s',
|
|
async (action) => {
|
|
const { terminal, addon, storage, kitty } = createTerminal()
|
|
try {
|
|
let finishDecode
|
|
kitty._createBitmap = () =>
|
|
new Promise((resolve) => {
|
|
finishDecode = resolve
|
|
})
|
|
const decoding = kitty._displayImage({ id: 1 }, {})
|
|
addon[action]()
|
|
const bitmap = new TrackedBitmap()
|
|
finishDecode(bitmap)
|
|
await decoding
|
|
expect(storage._images.size).toBe(0)
|
|
expect(bitmap.close).toHaveBeenCalledOnce()
|
|
} finally {
|
|
terminal.dispose()
|
|
}
|
|
}
|
|
)
|
|
})
|