From 475256e1a4aed11e1973848af51e48aca50023b1 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 3 Oct 2026 23:11:50 -0700 Subject: [PATCH 1/3] Speed up terminal test oracles without reducing replay coverage --- docs/reference/ci-runner-efficiency.md | 43 ++- .../serialize-grid-cell-descriptors.test.ts | 282 +++++++++++++----- .../daemon/serialize-grid-cell-descriptors.ts | 164 ++++++++-- src/main/daemon/serialize-grid-roundtrip.ts | 29 +- ...al-output-frame-chunks-equivalence.test.ts | 41 +-- src/shared/terminal-restore-parity-fixture.ts | 15 +- .../terminal-restore-parity-writes.test.ts | 138 +++++++++ 7 files changed, 560 insertions(+), 152 deletions(-) create mode 100644 src/shared/terminal-restore-parity-writes.test.ts diff --git a/docs/reference/ci-runner-efficiency.md b/docs/reference/ci-runner-efficiency.md index 8130b770dd94..3918c33d638e 100644 --- a/docs/reference/ci-runner-efficiency.md +++ b/docs/reference/ci-runner-efficiency.md @@ -1912,6 +1912,47 @@ The source was restored afterward. Extra collection turns therefore preserve the strong-retention oracle. Hosted qualification is still required; these observations do not prove a particular VM-retention cause or quantify avoided retries. +## October 4 terminal oracle execution + +Three measured test-support changes preserve the original seeds, payloads, +chunk boundaries and meaningful assertions. Serializer comparisons reuse cells +and format only the first mismatch instead of allocating descriptors for every +cell. The terminal parity writer submits every original chunk in FIFO order and +awaits the final parser callback. The independent legacy frame oracle memoizes +measured code-point widths. Its discarded algebra-only case never called +production and still passed when production always threw. + +Three alternating one-worker hosted ARM pairs measured complete invocations: + +| Cohort | Baseline median | Candidate median | Saving | +| --- | --- | --- | --- | +| Serializer replay/fuzz/descriptor checks | 71.675s | 46.581s | 35.0% | +| Emulator/reconciliation/color parity | 24.095s | 5.411s | 77.5% | +| Frame equivalence | 18.472s | 13.736s | 25.6% | + +[37180517143](https://github.com/stablyai/orca/actions/runs/37180517143) +retained 116 timed serializer passes and three existing/paired-control skips. +Separate captures matched all 190,796,645 raw bytes over 1,611 scenarios and +8,617 checkpoints (SHA256 `00ab219cfb31456af2ecd5e766d1b82d47abc751f6f2de0d7f795e36a936d3c7`), +including complete outputs and diagnostic payloads. Twenty candidate controls +passed; formatting/color/blank/clipping fault controls detected regressions. + +[37181073275](https://github.com/stablyai/orca/actions/runs/37181073275) +retained all 16 parity cases and default fuzz counts. Captures matched 2,325 +batches, 28,182 original chunks and 1,698,285 input bytes, with identical +terminal state and serialization per terminal/batch. Independent terminal +completion order differs, so comparison uses canonical per-terminal ordering +(SHA256 `c38ac1dbbefb9f6dc33ecfe7c495d65b707c1664614544622af93cfc1850e421`). +All 73 callback/parser/other-consumer controls passed; first-callback, reversed +chunks, missing empty boundary and early-completion faults failed. + +The frame candidate passed all 19 retained cases directly against the original +uncached legacy oracle, preserving 4,000 short and 800 near-cap seeded trials. +Sequence, surrogate width, byte width and span-transform faults failed real +assertions. A part-array alternative was rejected after adding time locally. +Hosted Node typecheck passed. These are focused workload savings, not measured +whole-shard or queue-delay improvements; application behavior is unchanged. + ## October 3 unit-selection evidence: include failed references The caller's `needs.test.result == 'success'` condition prevented the advisory @@ -1958,4 +1999,4 @@ pending and passed; resetting before waiting lost the tracked job and failed the same ordering assertion. The gate was released, both controls drained the captured job, and the instrumentation was removed. Changed-code quality passed. This proves the teardown ordering mechanism, not a measured avoided-retry saving. Final-head -hosted qualification remains required. +hosted qualification remains required. \ No newline at end of file diff --git a/src/main/daemon/serialize-grid-cell-descriptors.test.ts b/src/main/daemon/serialize-grid-cell-descriptors.test.ts index 490b40a1c9d9..8aec878b7909 100644 --- a/src/main/daemon/serialize-grid-cell-descriptors.test.ts +++ b/src/main/daemon/serialize-grid-cell-descriptors.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it, vi } from 'vitest' import type { Terminal } from '@xterm/headless' -import { bufferRows, cellDescriptor } from './serialize-grid-cell-descriptors' +import { cellDescriptor, compareBufferRows } from './serialize-grid-cell-descriptors' import { createFuzzTerminal, writeTerminal } from './serialize-grid-roundtrip' // Frozen allocating oracle from f69052e; a reused cell must preserve every descriptor. @@ -78,48 +78,6 @@ function allocatingBufferRows( } describe('serialize oracle cell reuse', () => { - it.each([false, true])( - 'matches allocating cells across SGR, wide text, buffers and resize (ConPTY=%s)', - (conpty) => { - const terminal = createFuzzTerminal({ cols: 14, rows: 4, scrollback: 30, conpty }) - try { - const writes = [ - 'plain \x1b[1;2;3;4;7;8;9mstyled\x1b[0m\r\n', - '\x1b[38;5;2;48;5;10m palette \x1b[38;2;11;22;33;48;2;44;55;66m RGB \x1b[0m\r\n', - '\x1b[4:3;9;53m \x1b[0m\x1b[7m \x1b[0m界👩‍💻é\r\n', - 'scroll1\r\nscroll2\r\nscroll3\r\n', - '\x1b[?1049h\x1b[1;2;3;4;7;8;9malt界\x1b[0m', - '\x1b[?1049l\x1b[2J\x1b[Hclear' - ] - for (const data of writes) { - writeTerminal(terminal, data) - for (const buffer of [ - terminal.buffer.normal, - terminal.buffer.alternate, - terminal.buffer.active - ]) { - expect(bufferRows(buffer, -1, buffer.length + 1, terminal.cols + 2)).toEqual( - allocatingBufferRows(buffer, -1, buffer.length + 1, terminal.cols + 2) - ) - } - terminal.resize(terminal.cols === 14 ? 9 : 14, 4) - expect( - bufferRows(terminal.buffer.active, 0, terminal.buffer.active.length, terminal.cols) - ).toEqual( - allocatingBufferRows( - terminal.buffer.active, - 0, - terminal.buffer.active.length, - terminal.cols - ) - ) - } - } finally { - terminal.dispose() - } - } - ) - it('preserves the order of every text flag combination while reloading a plain cell', () => { const terminal = createFuzzTerminal({ cols: 4, rows: 1, scrollback: 0 }) try { @@ -167,50 +125,11 @@ describe('serialize oracle cell reuse', () => { const scratch = terminal.buffer.active.getNullCell() expect(cellDescriptor(line, 3, 4, scratch)).toBe('CLIPPED') expect(cellDescriptor(line, 3, 4, scratch)).toBe(allocatingCellDescriptor(line, 3, 4)) - expect(bufferRows(terminal.buffer.active, 0, 1, 4)).toEqual( - allocatingBufferRows(terminal.buffer.active, 0, 1, 4) - ) } finally { terminal.dispose() } }) - it('loads every cell into one traversal-local scratch object and retains immutable descriptors', () => { - const terminal = createFuzzTerminal({ cols: 4, rows: 2, scrollback: 0 }) - try { - writeTerminal(terminal, '\x1b[1mA\x1b[0m B界') - const buffer = terminal.buffer.active - const scratch = buffer.getNullCell() - const allocate = vi.spyOn(buffer, 'getNullCell').mockReturnValue(scratch) - const getLine = buffer.getLine.bind(buffer) - const loaded: unknown[] = [] - vi.spyOn(buffer, 'getLine').mockImplementation((y) => { - const line = getLine(y) - if (line) { - const getCell = line.getCell.bind(line) - vi.spyOn(line, 'getCell').mockImplementation((x, cell) => { - loaded.push(cell) - return getCell(x, cell) - }) - } - return line - }) - const expected = allocatingBufferRows(buffer, 0, 2, 4) - loaded.length = 0 - const actual = bufferRows(buffer, 0, 2, 4) - expect(actual).toEqual(expected) - expect(allocate).toHaveBeenCalledTimes(1) - expect(loaded).toHaveLength(8) - expect(loaded.every((cell) => cell === scratch)).toBe(true) - writeTerminal(terminal, '\x1b[2J\x1b[Hnew') - bufferRows(buffer, 0, 2, 4) - expect(actual).toEqual(expected) - } finally { - terminal.dispose() - vi.restoreAllMocks() - } - }) - it('keeps missing lines and invalid columns blank after a styled cell occupied the scratch', () => { const terminal = createFuzzTerminal({ cols: 4, rows: 2, scrollback: 0 }) try { @@ -230,3 +149,202 @@ describe('serialize oracle cell reuse', () => { } }) }) + +function allocatingRowDiff(stage: string, expected: string[][], actual: string[][]) { + for (let y = 0; y < Math.max(expected.length, actual.length); y++) { + const expectedRow = expected[y] + const actualRow = actual[y] + if ( + !expectedRow || + !actualRow || + expectedRow.length !== actualRow.length || + !expectedRow.every( + (cell, x) => cell === actualRow[x] || (cell === CLIPPED && actualRow[x]?.startsWith('▯')) + ) + ) { + return { stage, row: y, expected: expectedRow?.join('|'), actual: actualRow?.join('|') } + } + } + return null +} + +function expectComparisonParity( + expected: Buffer, + actual: Buffer, + cols: number, + expectedStart = 0, + expectedEnd = expected.length, + actualStart = 0, + actualEnd = actual.length +): ReturnType { + const frozen = allocatingRowDiff( + 'parity', + allocatingBufferRows(expected, expectedStart, expectedEnd, cols), + allocatingBufferRows(actual, actualStart, actualEnd, cols) + ) + expect( + compareBufferRows( + 'parity', + expected, + expectedStart, + expectedEnd, + actual, + actualStart, + actualEnd, + cols + ) + ).toEqual(frozen) + return frozen +} + +describe('serialize oracle streaming comparison', () => { + it('reuses one cell per buffer and stops before rows after the first difference', () => { + const source = createFuzzTerminal({ cols: 8, rows: 4, scrollback: 0 }) + const replay = createFuzzTerminal({ cols: 8, rows: 4, scrollback: 0 }) + try { + writeTerminal(source, 'first\r\nsecond\r\nthird\r\nlast') + writeTerminal(replay, 'wrong\r\nsecond\r\nthird\r\nlast') + for (const buffer of [source.buffer.active, replay.buffer.active]) { + const scratch = buffer.getNullCell() + vi.spyOn(buffer, 'getNullCell').mockReturnValue(scratch) + const getLine = buffer.getLine.bind(buffer) + vi.spyOn(buffer, 'getLine').mockImplementation((y) => { + const line = getLine(y) + if (line) { + const getCell = line.getCell.bind(line) + vi.spyOn(line, 'getCell').mockImplementation((x, cell) => { + expect(cell).toBe(scratch) + return getCell(x, cell) + }) + } + return line + }) + } + const diff = compareBufferRows( + 'visible-grid', + source.buffer.active, + 0, + 4, + replay.buffer.active, + 0, + 4, + 8 + ) + expect(diff?.row).toBe(0) + for (const buffer of [source.buffer.active, replay.buffer.active]) { + expect(buffer.getNullCell).toHaveBeenCalledTimes(1) + expect(buffer.getLine).toHaveBeenCalledTimes(2) + expect(buffer.getLine).toHaveBeenCalledWith(0) + expect(buffer.getLine).toHaveBeenCalledWith(3) + } + writeTerminal(source, '\x1b[2J\x1b[Hchanged') + expect(diff?.expected).toContain('f·w1·f0:-1·b0:-1·0000000') + } finally { + source.dispose() + replay.dispose() + vi.restoreAllMocks() + } + }) + + it.each([false, true])( + 'preserves first-row diagnostics through buffer changes (ConPTY=%s)', + (conpty) => { + const source = createFuzzTerminal({ cols: 14, rows: 4, scrollback: 30, conpty }) + const replay = createFuzzTerminal({ cols: 14, rows: 4, scrollback: 30, conpty }) + try { + for (const [index, data] of [ + 'plain \x1b[1;2;3;4;7;8;9mstyled\x1b[0m\r\n', + '\x1b[38;5;2;48;5;10m palette \x1b[38;2;11;22;33;48;2;44;55;66m RGB \x1b[0m\r\n', + '\x1b[4:3;9;53m \x1b[0m\x1b[7m \x1b[0m界👩‍💻é\r\n', + 'scroll1\r\nscroll2\r\nscroll3\r\n', + '\x1b[?1049h\x1b[1;2;3;4;7;8;9malt界\x1b[0m', + '\x1b[?1049l\x1b[2J\x1b[Hclear' + ].entries()) { + writeTerminal(source, data) + writeTerminal(replay, data) + for (const [expected, actual] of [ + [source.buffer.active, replay.buffer.active], + [source.buffer.normal, replay.buffer.normal], + [source.buffer.alternate, replay.buffer.alternate] + ]) { + expect(expectComparisonParity(expected!, actual!, source.cols)).toBeNull() + expectComparisonParity(expected!, actual!, source.cols + 2, -1, expected!.length + 1) + } + const corruption = `\x1b[H\x1b[0mFAULT${index}` + writeTerminal(replay, corruption) + expect( + expectComparisonParity(source.buffer.active, replay.buffer.active, source.cols) + ).not.toBeNull() + writeTerminal(source, corruption) + source.resize(source.cols === 14 ? 9 : 14, 4) + replay.resize(source.cols, 4) + expectComparisonParity(source.buffer.active, replay.buffer.active, source.cols) + } + } finally { + source.dispose() + replay.dispose() + } + } + ) + + it('detects every text-flag loss and compares all combinations against the allocating oracle', () => { + const source = createFuzzTerminal({ cols: 4, rows: 1, scrollback: 0 }) + const replay = createFuzzTerminal({ cols: 4, rows: 1, scrollback: 0 }) + try { + const sgr = [1, 2, 3, 4, 7, 8, 9] + const writeFlags = (terminal: Terminal, mask: number): void => { + const codes = sgr.filter((_code, bit) => (mask & (1 << bit)) !== 0) + writeTerminal(terminal, `\x1b[H\x1b[0m\x1b[${codes.length ? codes.join(';') : 0}mA\x1b[0mB`) + } + for (let mask = 0; mask < 128; mask++) { + writeFlags(source, mask) + writeFlags(replay, mask) + expect(expectComparisonParity(source.buffer.active, replay.buffer.active, 4)).toBeNull() + for (let bit = 0; bit < sgr.length; bit++) { + if ((mask & (1 << bit)) !== 0) { + writeFlags(replay, mask & ~(1 << bit)) + expect( + expectComparisonParity(source.buffer.active, replay.buffer.active, 4) + ).not.toBeNull() + } + } + } + } finally { + source.dispose() + replay.dispose() + } + }) + + it.each([ + ['abc界', 'abc ', 4, false], + ['abc界', 'abc\x1b[48;2;1;2;3m ', 4, false], + ['abc ', 'abc界', 4, true], + ['é', 'è', 8, true], + ['\x1b[32mA', '\x1b[38;5;2mA', 8, false], + ['\x1b[42m ', '\x1b[48;5;2m ', 8, false], + ['\x1b[7;31m ', '\x1b[7;32m ', 8, true], + ['\x1b[4m\x1b[2J', '\x1b[2J', 8, false], + ['\x1b[4m ', ' ', 8, true], + ['\x1b[4m \x1b[0mB', ' B', 8, true], + ['text\r\n', 'text', 8, false], + ['text\r\n\x1b[48;2;1;2;3m\x1b[2K', 'text', 8, true] + ] as const)( + 'preserves blank, clipped and color policy for %j / %j', + (expected, actual, cols, differs) => { + const source = createFuzzTerminal({ cols: 8, rows: 4, scrollback: 10 }) + const replay = createFuzzTerminal({ cols: 8, rows: 4, scrollback: 10 }) + try { + writeTerminal(source, expected) + writeTerminal(replay, actual) + expect( + Boolean(expectComparisonParity(source.buffer.active, replay.buffer.active, cols)) + ).toBe(differs) + expectComparisonParity(source.buffer.active, replay.buffer.active, cols, -1, 6, -1, 5) + expectComparisonParity(source.buffer.active, replay.buffer.active, cols, 1, 5, 0, 4) + } finally { + source.dispose() + replay.dispose() + } + } + ) +}) diff --git a/src/main/daemon/serialize-grid-cell-descriptors.ts b/src/main/daemon/serialize-grid-cell-descriptors.ts index 8b6c331f5fc2..9adbbc6c694c 100644 --- a/src/main/daemon/serialize-grid-cell-descriptors.ts +++ b/src/main/daemon/serialize-grid-cell-descriptors.ts @@ -7,6 +7,7 @@ export type GridDiff = { stage: string; row?: number; expected: unknown; actual: type BufferLine = NonNullable> type Buffer = Terminal['buffer']['active'] +type Cell = ReturnType const COLOR_MODE_P16 = 16777216 const COLOR_MODE_P256 = 33554432 @@ -64,33 +65,158 @@ function cellsMatch(expected: string, actual: string): boolean { return expected === actual || (expected === CLIPPED && actual.startsWith('▯')) } -function rowsMatch(expected: string[], actual: string[]): boolean { - return expected.length === actual.length && expected.every((e, i) => cellsMatch(e, actual[i]!)) +function sameColor( + expectedMode: number, + expectedColor: number, + actualMode: number, + actualColor: number +): boolean { + return ( + expectedColor === actualColor && + canonicalColorMode(expectedMode, expectedColor) === canonicalColorMode(actualMode, actualColor) + ) } -export function bufferRows(buffer: Buffer, start: number, end: number, cols: number): string[][] { - const rows: string[][] = [] - const reusableCell = buffer.getNullCell() - for (let y = start; y < end; y++) { - rows.push(rowCells(buffer.getLine(y), cols, reusableCell)) +// Equal public fields imply identical descriptors; differing fields still use the frozen policy. +function sameCellFields(expected: Cell, actual: Cell): boolean { + if ( + expected.getWidth() !== actual.getWidth() || + !sameColor( + expected.getBgColorMode(), + expected.getBgColor(), + actual.getBgColorMode(), + actual.getBgColor() + ) + ) { + return false } - while (rows.length > 0 && rows.at(-1)!.every((c) => c === DEFAULT_BLANK)) { - rows.pop() + const expectedChars = expected.getChars() + const actualChars = actual.getChars() + const expectedBlank = expectedChars === '' || expectedChars === ' ' + const actualBlank = actualChars === '' || actualChars === ' ' + if (expectedBlank || actualBlank) { + return ( + expectedBlank && + actualBlank && + (expectedChars === ' ' && expected.isUnderline() !== 0) === + (actualChars === ' ' && actual.isUnderline() !== 0) && + (expectedChars === ' ' && expected.isStrikethrough() !== 0) === + (actualChars === ' ' && actual.isStrikethrough() !== 0) && + (expectedChars === ' ' && expected.isOverline() !== 0) === + (actualChars === ' ' && actual.isOverline() !== 0) && + (expected.isInverse() !== 0) === (actual.isInverse() !== 0) && + (expected.isInverse() === 0 || + sameColor( + expected.getFgColorMode(), + expected.getFgColor(), + actual.getFgColorMode(), + actual.getFgColor() + )) + ) } - return rows + return ( + expectedChars === actualChars && + sameColor( + expected.getFgColorMode(), + expected.getFgColor(), + actual.getFgColorMode(), + actual.getFgColor() + ) && + (expected.isBold() !== 0) === (actual.isBold() !== 0) && + (expected.isDim() !== 0) === (actual.isDim() !== 0) && + (expected.isItalic() !== 0) === (actual.isItalic() !== 0) && + (expected.isUnderline() !== 0) === (actual.isUnderline() !== 0) && + (expected.isInverse() !== 0) === (actual.isInverse() !== 0) && + (expected.isInvisible() !== 0) === (actual.isInvisible() !== 0) && + (expected.isStrikethrough() !== 0) === (actual.isStrikethrough() !== 0) + ) } -export function compareRowSets( +function trimmedEnd( + buffer: Buffer, + start: number, + end: number, + cols: number, + scratch: Cell +): number { + while (end > start) { + const line = buffer.getLine(end - 1) + let blank = true + for (let x = 0; x < cols; x++) { + if (cellDescriptor(line, x, cols, scratch) !== DEFAULT_BLANK) { + blank = false + break + } + } + if (!blank) { + break + } + end-- + } + return end +} + +function lineCellsMatch( + expected: BufferLine | undefined, + actual: BufferLine | undefined, + cols: number, + expectedScratch: Cell, + actualScratch: Cell +): boolean { + for (let x = 0; x < cols; x++) { + const expectedCell = + expected && x < expected.length ? expected.getCell(x, expectedScratch) : null + const actualCell = actual && x < actual.length ? actual.getCell(x, actualScratch) : null + const clipped = + x === cols - 1 && + ((expected && expected.length > cols && (expectedCell?.getWidth() ?? 0) > 1) || + (actual && actual.length > cols && (actualCell?.getWidth() ?? 0) > 1)) + if (expectedCell && actualCell && !clipped && sameCellFields(expectedCell, actualCell)) { + continue + } + if ( + !cellsMatch( + cellDescriptor(expected, x, cols, expectedScratch), + cellDescriptor(actual, x, cols, actualScratch) + ) + ) { + return false + } + } + return true +} + +/** Compares in place and formats only the first differing row, preserving diagnostic bytes. */ +export function compareBufferRows( stage: string, - expected: string[][], - actual: string[][] + expected: Buffer, + expectedStart: number, + expectedEnd: number, + actual: Buffer, + actualStart: number, + actualEnd: number, + cols: number ): GridDiff | null { - const length = Math.max(expected.length, actual.length) - for (let y = 0; y < length; y++) { - const e = expected[y] - const a = actual[y] - if (!e || !a || !rowsMatch(e, a)) { - return { stage, row: y, expected: e?.join('|'), actual: a?.join('|') } + const expectedScratch = expected.getNullCell() + const actualScratch = actual.getNullCell() + const expectedLength = + trimmedEnd(expected, expectedStart, expectedEnd, cols, expectedScratch) - expectedStart + const actualLength = trimmedEnd(actual, actualStart, actualEnd, cols, actualScratch) - actualStart + for (let y = 0; y < Math.max(expectedLength, actualLength); y++) { + const expectedLine = expected.getLine(expectedStart + y) + const actualLine = actual.getLine(actualStart + y) + if ( + y >= expectedLength || + y >= actualLength || + !lineCellsMatch(expectedLine, actualLine, cols, expectedScratch, actualScratch) + ) { + return { + stage, + row: y, + expected: + y < expectedLength ? rowCells(expectedLine, cols, expectedScratch).join('|') : undefined, + actual: y < actualLength ? rowCells(actualLine, cols, actualScratch).join('|') : undefined + } } } return null diff --git a/src/main/daemon/serialize-grid-roundtrip.ts b/src/main/daemon/serialize-grid-roundtrip.ts index d22bee6e5113..26176c9ff5ad 100644 --- a/src/main/daemon/serialize-grid-roundtrip.ts +++ b/src/main/daemon/serialize-grid-roundtrip.ts @@ -18,12 +18,7 @@ import { variantTrailingBackgroundRows } from './serialize-grid-variant-scope' import type { GridDiff } from './serialize-grid-cell-descriptors' -import { - bufferRows, - cellDescriptor, - CLIPPED, - compareRowSets -} from './serialize-grid-cell-descriptors' +import { cellDescriptor, CLIPPED, compareBufferRows } from './serialize-grid-cell-descriptors' export type NamedSerializer = { name: string; create: () => SerializeAddon } @@ -156,15 +151,25 @@ async function compareReplay( (src.type === dst.type ? null : { stage: 'active-buffer', expected: src.type, actual: dst.type }) ?? - compareRowSets( + compareBufferRows( 'visible-grid', - bufferRows(src, src.baseY, src.baseY + rows, cols), - bufferRows(dst, dst.baseY, dst.baseY + rows, cols) + src, + src.baseY, + src.baseY + rows, + dst, + dst.baseY, + dst.baseY + rows, + cols ) ?? - compareRowSets( + compareBufferRows( 'normal-buffer', - bufferRows(source.buffer.normal, normalStart, source.buffer.normal.length, cols), - bufferRows(replay.buffer.normal, 0, replay.buffer.normal.length, cols) + source.buffer.normal, + normalStart, + source.buffer.normal.length, + replay.buffer.normal, + 0, + replay.buffer.normal.length, + cols ) ?? (src.cursorX === dst.cursorX && src.cursorY === dst.cursorY ? null diff --git a/src/main/runtime/rpc/terminal-output-frame-chunks-equivalence.test.ts b/src/main/runtime/rpc/terminal-output-frame-chunks-equivalence.test.ts index 85e01116c3cd..e143535d6594 100644 --- a/src/main/runtime/rpc/terminal-output-frame-chunks-equivalence.test.ts +++ b/src/main/runtime/rpc/terminal-output-frame-chunks-equivalence.test.ts @@ -14,9 +14,7 @@ import { type TerminalOutputMeta } from './terminal-output-frame-chunks' -// Byte-for-byte reference: the pre-optimization implementation, copied verbatim. -// It accumulates `chunk += part` over `for (const part of data)` and measures each -// code point through the shared clipboard measurer. +// The legacy splitter, caching only its pure byte counts for repeated code points. function legacyByteLength(data: string): number { return measureClipboardTextByteLength(data).byteLength } @@ -64,6 +62,7 @@ function* legacyIterateTerminalOutputFrameChunks( let chunkStartOffset = 0 let offset = 0 let delayedChunk: { text: string; seq?: number } | null = null + const partByteLengths = new Map() const takeChunk = (): { text: string; seq?: number } | null => { if (!chunk) { @@ -78,7 +77,11 @@ function* legacyIterateTerminalOutputFrameChunks( } for (const part of data) { - const partBytes = legacyByteLength(part) + let partBytes = partByteLengths.get(part) + if (partBytes === undefined) { + partBytes = legacyByteLength(part) + partByteLengths.set(part, partBytes) + } if (chunkBytes > 0 && chunkBytes + partBytes > TERMINAL_STREAM_CHUNK_BYTES) { const nextChunk = takeChunk() if (nextChunk) { @@ -522,34 +525,4 @@ describe('iterateTerminalOutputFrameChunks equivalence with the pre-optimization expect([...iterateTerminalOutputFrameChunks(overByOne)].length).toBeGreaterThan(1) expect([...legacyIterateTerminalOutputFrameChunks(overByOne)].length).toBeGreaterThan(1) }) - - // The chunking loop is only reached when rawLength === data.length and transformed - // is falsy, which makes canPreserveChunkSeq === (typeof meta.seq === 'number') and - // therefore shouldDelayFinalSeq unconditionally false. The branch survives here - // (it also survived in the pre-optimization code) purely as defence in depth. - it('never reaches the delayed-final-seq branch for any meta shape', () => { - for (const data of ['', 'a', 'abc', 'x'.repeat(200)]) { - for (const seq of [undefined, 0, 5] as (number | undefined)[]) { - for (const rawDelta of [undefined, 0, 1, -1] as (number | undefined)[]) { - for (const transformed of [undefined, false, true] as (boolean | undefined)[]) { - const meta: TerminalOutputMeta = {} - if (seq !== undefined) { - meta.seq = seq - } - if (rawDelta !== undefined) { - meta.rawLength = data.length + rawDelta - } - if (transformed !== undefined) { - meta.transformed = transformed - } - const rawLength = meta.rawLength ?? data.length - const reachesChunkLoop = !meta.transformed && rawLength === data.length - const canPreserveChunkSeq = typeof meta.seq === 'number' && rawLength === data.length - const shouldDelayFinalSeq = !canPreserveChunkSeq && typeof meta.seq === 'number' - expect(reachesChunkLoop && shouldDelayFinalSeq, JSON.stringify(meta)).toBe(false) - } - } - } - } - }) }) diff --git a/src/shared/terminal-restore-parity-fixture.ts b/src/shared/terminal-restore-parity-fixture.ts index 10fb0cd6e583..4fab319ab5b8 100644 --- a/src/shared/terminal-restore-parity-fixture.ts +++ b/src/shared/terminal-restore-parity-fixture.ts @@ -47,10 +47,17 @@ export function writeToTerminal(terminal: Terminal, data: string): Promise return new Promise((resolve) => terminal.write(data, resolve)) } -export async function writeChunksToTerminal(terminal: Terminal, chunks: string[]): Promise { - for (const chunk of chunks) { - await writeToTerminal(terminal, chunk) - } +export function writeChunksToTerminal(terminal: Terminal, chunks: string[]): Promise { + return new Promise((resolve) => { + if (chunks.length === 0) { + resolve() + return + } + // The final FIFO callback also fences async parser handlers in earlier chunks. + for (let index = 0; index < chunks.length; index++) { + terminal.write(chunks[index]!, index === chunks.length - 1 ? resolve : undefined) + } + }) } /** Bottom-anchored visible screen rows (baseY, not viewportY — scroll intent diff --git a/src/shared/terminal-restore-parity-writes.test.ts b/src/shared/terminal-restore-parity-writes.test.ts new file mode 100644 index 000000000000..443d7c1e5a4c --- /dev/null +++ b/src/shared/terminal-restore-parity-writes.test.ts @@ -0,0 +1,138 @@ +import { describe, expect, it, vi } from 'vitest' +import { + createRendererParityTerminal, + cursorPosition, + normalBufferRowsTrimmed, + normalBufferStylesTrimmed, + visibleRowStyles, + visibleRowWraps, + visibleRows, + writeChunksToTerminal +} from './terminal-restore-parity-fixture' + +describe('terminal parity fixture writes', () => { + it('queues every original chunk and resolves only after the last callback', async () => { + const { terminal } = createRendererParityTerminal({ cols: 8, rows: 2 }) + const writes: { data: string | Uint8Array; callback?: () => void }[] = [] + try { + vi.spyOn(terminal, 'write').mockImplementation((data, callback) => { + writes.push({ data, callback }) + }) + const chunks = ['A\x1b[', '', '31mB', '\x1b[0mC'] + let completed = false + const pending = writeChunksToTerminal(terminal, chunks).then(() => { + completed = true + }) + expect(writes.map(({ data }) => data)).toEqual(chunks) + expect(writes.slice(0, -1).every(({ callback }) => callback === undefined)).toBe(true) + await Promise.resolve() + expect(completed).toBe(false) + writes.at(-1)?.callback?.() + await pending + expect(completed).toBe(true) + } finally { + terminal.dispose() + vi.restoreAllMocks() + } + }) + + it('completes an empty batch without writing or scheduling work', async () => { + const { terminal } = createRendererParityTerminal({ cols: 8, rows: 2 }) + try { + const write = vi.spyOn(terminal, 'write') + await writeChunksToTerminal(terminal, []) + expect(write).not.toHaveBeenCalled() + } finally { + terminal.dispose() + vi.restoreAllMocks() + } + }) + + it('waits for an asynchronous parser barrier before exposing later chunks', async () => { + const { terminal } = createRendererParityTerminal({ cols: 8, rows: 2 }) + let signalEntered = (): void => {} + const entered = new Promise((resolve) => { + signalEntered = resolve + }) + let releaseParser = (): void => {} + const barrier = new Promise((resolve) => { + releaseParser = () => resolve(true) + }) + // xterm supports async handlers; its shipped public typings still say boolean. + const registration: unknown = Reflect.apply( + terminal.parser.registerOscHandler, + terminal.parser, + [ + 777, + () => { + signalEntered() + return barrier + } + ] + ) + try { + let completed = false + const pending = writeChunksToTerminal(terminal, [ + 'A\x1b]777;pause', + '\x07B', + '\x1b[31mC', + 'D' + ]).then(() => { + completed = true + }) + await entered + expect(visibleRows(terminal)[0]).toBe('A') + expect(completed).toBe(false) + releaseParser() + await pending + expect(visibleRows(terminal)[0]).toBe('ABCD') + expect(terminal.buffer.active.getLine(0)?.getCell(3)?.getFgColor()).toBe(1) + } finally { + releaseParser() + if ( + typeof registration === 'object' && + registration !== null && + 'dispose' in registration && + typeof registration.dispose === 'function' + ) { + registration.dispose() + } + terminal.dispose() + } + }) + + it.each([ + ['A\x1b[', '38;2;11;22;33m界', 'é👩‍💻', '\x1b[0m\r\nnext'], + ['history\r\n'.repeat(8), '\x1b[?1049h\x1b[2J\x1b[H', '\x1b[48;5;2m ALT ', '\x1b[0m'], + ['\x1b[?1049hALT', '\x1b[?1049l', '\x1b[7;4;9;53m ', '\x1b[0mnormal'], + ['\x1b]8;;https://example.com\x07', 'link', '\x1b]8;;', '\x07 tail'], + ['0123456789ABCDEFGHIJKLMN', '\x1b[2A\x1b[2K', '\x1b[3;5H', 'end\x1b[?2004h'] + ])( + 'matches serial callbacks for split controls, styles and buffer transitions: %j', + async (...chunks) => { + const batched = createRendererParityTerminal({ cols: 12, rows: 4 }) + const serial = createRendererParityTerminal({ cols: 12, rows: 4 }) + try { + for (const chunk of chunks) { + await new Promise((resolve) => serial.terminal.write(chunk, resolve)) + } + await writeChunksToTerminal(batched.terminal, chunks) + expect(visibleRows(batched.terminal)).toEqual(visibleRows(serial.terminal)) + expect(visibleRowStyles(batched.terminal)).toEqual(visibleRowStyles(serial.terminal)) + expect(visibleRowWraps(batched.terminal)).toEqual(visibleRowWraps(serial.terminal)) + expect(normalBufferRowsTrimmed(batched.terminal)).toEqual( + normalBufferRowsTrimmed(serial.terminal) + ) + expect(normalBufferStylesTrimmed(batched.terminal)).toEqual( + normalBufferStylesTrimmed(serial.terminal) + ) + expect(cursorPosition(batched.terminal)).toEqual(cursorPosition(serial.terminal)) + expect(batched.terminal.modes).toEqual(serial.terminal.modes) + expect(batched.serializeAddon.serialize()).toBe(serial.serializeAddon.serialize()) + } finally { + batched.terminal.dispose() + serial.terminal.dispose() + } + } + ) +}) From e771acc83e315aa68309267ad6987f88e10fa395 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 3 Oct 2026 23:20:12 -0700 Subject: [PATCH 2/3] Call asynchronous parser through its checked test interface --- .../terminal-restore-parity-writes.test.ts | 32 ++++++++----------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/shared/terminal-restore-parity-writes.test.ts b/src/shared/terminal-restore-parity-writes.test.ts index 443d7c1e5a4c..342e7effd542 100644 --- a/src/shared/terminal-restore-parity-writes.test.ts +++ b/src/shared/terminal-restore-parity-writes.test.ts @@ -10,6 +10,13 @@ import { writeChunksToTerminal } from './terminal-restore-parity-fixture' +type AsyncOscParser = { + registerOscHandler( + identifier: number, + callback: (data: string) => boolean | Promise + ): { dispose(): void } +} + describe('terminal parity fixture writes', () => { it('queues every original chunk and resolves only after the last callback', async () => { const { terminal } = createRendererParityTerminal({ cols: 8, rows: 2 }) @@ -59,17 +66,11 @@ describe('terminal parity fixture writes', () => { releaseParser = () => resolve(true) }) // xterm supports async handlers; its shipped public typings still say boolean. - const registration: unknown = Reflect.apply( - terminal.parser.registerOscHandler, - terminal.parser, - [ - 777, - () => { - signalEntered() - return barrier - } - ] - ) + const parser: AsyncOscParser = terminal.parser + const registration = parser.registerOscHandler(777, () => { + signalEntered() + return barrier + }) try { let completed = false const pending = writeChunksToTerminal(terminal, [ @@ -89,14 +90,7 @@ describe('terminal parity fixture writes', () => { expect(terminal.buffer.active.getLine(0)?.getCell(3)?.getFgColor()).toBe(1) } finally { releaseParser() - if ( - typeof registration === 'object' && - registration !== null && - 'dispose' in registration && - typeof registration.dispose === 'function' - ) { - registration.dispose() - } + registration.dispose() terminal.dispose() } }) From 4ec64385182199891621d7c9b848fd10881041a8 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 3 Oct 2026 23:23:57 -0700 Subject: [PATCH 3/3] Preserve evidence document final newline for concurrent merges --- docs/reference/ci-runner-efficiency.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/ci-runner-efficiency.md b/docs/reference/ci-runner-efficiency.md index 3918c33d638e..714711b60248 100644 --- a/docs/reference/ci-runner-efficiency.md +++ b/docs/reference/ci-runner-efficiency.md @@ -1999,4 +1999,4 @@ pending and passed; resetting before waiting lost the tracked job and failed the same ordering assertion. The gate was released, both controls drained the captured job, and the instrumentation was removed. Changed-code quality passed. This proves the teardown ordering mechanism, not a measured avoided-retry saving. Final-head -hosted qualification remains required. \ No newline at end of file +hosted qualification remains required.