mirror of
https://github.com/stablyai/orca.git
synced 2026-10-20 08:03:28 +00:00
perf(terminal): eliminate adverse control and frame-gate cases (#11045)
* perf(terminal): eliminate dense control and frame gate regressions * test(terminal): keep gate labels in valid expect shape * test(terminal): expose the surviving sub-threshold control-density case The only adverse strip fixture sat at 50% control density, which is exactly where the fallback fires and wins. A shape at 31 controls per 64-unit block evades the trigger and still loses to the per-character legacy (0.67x), so the benchmark structurally could not show it. Add that fixture, pin both density literals in the staleness guard so a retune fails loudly instead of silently measuring a boundary that moved, and export the probe constant the equivalence test was hardcoding.
This commit is contained in:
@@ -7,6 +7,8 @@ import {
|
||||
import { TERMINAL_STREAM_CHUNK_BYTES } from '../../../shared/terminal-multiplex-flow-control'
|
||||
import { measureClipboardTextByteLength } from '../../../shared/clipboard-text'
|
||||
import {
|
||||
TERMINAL_STREAM_BYTE_PROBE_CODE_UNITS,
|
||||
exceedsTerminalStreamChunkBytes,
|
||||
iterateTerminalOutputFrameChunks,
|
||||
type TerminalOutputFrameChunk,
|
||||
type TerminalOutputMeta
|
||||
@@ -23,6 +25,19 @@ function legacyByteLengthExceeds(data: string, maxBytes: number): boolean {
|
||||
return measureClipboardTextByteLength(data, { stopAfterBytes: maxBytes }).exceededLimit
|
||||
}
|
||||
|
||||
function expectGateEquivalent(data: string, label: string): void {
|
||||
expect({ label, result: exceedsTerminalStreamChunkBytes(data) }).toEqual({
|
||||
label,
|
||||
result: legacyByteLengthExceeds(data, TERMINAL_STREAM_CHUNK_BYTES)
|
||||
})
|
||||
}
|
||||
|
||||
function makeExactByteLength(unit: string, byteLength: number): string {
|
||||
const unitBytes = Buffer.byteLength(unit, 'utf8')
|
||||
const repeats = Math.floor(byteLength / unitBytes)
|
||||
return unit.repeat(repeats) + 'a'.repeat(byteLength - repeats * unitBytes)
|
||||
}
|
||||
|
||||
function* legacyIterateTerminalOutputFrameChunks(
|
||||
data: string,
|
||||
meta?: TerminalOutputMeta
|
||||
@@ -216,6 +231,50 @@ function randomText(random: () => number, parts: number): string {
|
||||
}
|
||||
|
||||
describe('iterateTerminalOutputFrameChunks equivalence with the pre-optimization loop', () => {
|
||||
it('matches the legacy gate at the byte cap ±3 for every UTF-8 shape', () => {
|
||||
for (const [label, unit] of [
|
||||
['ascii', 'a'],
|
||||
['two-byte', '\u00e9'],
|
||||
['three-byte', '\u20ac'],
|
||||
['astral', SURROGATE_PAIR],
|
||||
['lone-high', LONE_HIGH],
|
||||
['lone-low', LONE_LOW],
|
||||
['reversed-surrogates', `${LONE_LOW}${LONE_HIGH}a`]
|
||||
] as const) {
|
||||
for (let delta = -3; delta <= 3; delta += 1) {
|
||||
const byteLength = TERMINAL_STREAM_CHUNK_BYTES + delta
|
||||
const data = makeExactByteLength(unit, byteLength)
|
||||
expect(Buffer.byteLength(data, 'utf8')).toBe(byteLength)
|
||||
expectGateEquivalent(data, `${label} delta=${delta}`)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('matches when probe boundaries bisect or surround surrogate pairs', () => {
|
||||
const probe = TERMINAL_STREAM_BYTE_PROBE_CODE_UNITS
|
||||
for (const offset of [-2, -1, 0, 1, 2]) {
|
||||
const pairStart = probe + offset
|
||||
const prefix = 'a'.repeat(pairStart)
|
||||
const suffix = '\u20ac'.repeat(12_000)
|
||||
for (const middle of [SURROGATE_PAIR, LONE_HIGH, LONE_LOW, LONE_LOW + LONE_HIGH]) {
|
||||
const data = prefix + middle + suffix
|
||||
expectGateEquivalent(data, `probe offset=${offset} middle=${escapeUnits(middle)}`)
|
||||
expectEquivalent(data, undefined, `probe frames offset=${offset}`)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('stops correctly when late wide text crosses the cap', () => {
|
||||
const asciiPrefix = 'a'.repeat(16_000)
|
||||
for (const wide of ['\u00e9', '\u20ac', SURROGATE_PAIR, LONE_HIGH]) {
|
||||
for (const wideParts of [8_000, 12_000, 16_000]) {
|
||||
const data = asciiPrefix + wide.repeat(wideParts)
|
||||
expectGateEquivalent(data, `late-wide ${escapeUnits(wide)} parts=${wideParts}`)
|
||||
expectEquivalent(data, undefined, `late-wide frames ${escapeUnits(wide)}`)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('matches on the small/no-chunking sizes', () => {
|
||||
for (const size of [0, 1, 2, 3, 7, 64, 1024, TERMINAL_STREAM_CHUNK_BYTES - 1]) {
|
||||
sweepAll('a'.repeat(size), `ascii ${size}`)
|
||||
@@ -416,7 +475,7 @@ describe('iterateTerminalOutputFrameChunks equivalence with the pre-optimization
|
||||
expectEquivalent(data, { seq: 88_888, rawLength: data.length }, `fuzz-cap seq trial=${trial}`)
|
||||
expectEquivalent(data, { seq: 88_888 }, `fuzz-cap delayed trial=${trial}`)
|
||||
}
|
||||
})
|
||||
}, 15_000)
|
||||
|
||||
it('keeps every emitted frame within the wire cap and reassembles to the input', () => {
|
||||
const data = `${'a'.repeat(200 * 1024)}${SURROGATE_PAIR.repeat(4096)}${LONE_HIGH}`
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
encodeTerminalStreamText
|
||||
} from '../../../shared/terminal-stream-protocol'
|
||||
import { TERMINAL_STREAM_CHUNK_BYTES } from '../../../shared/terminal-multiplex-flow-control'
|
||||
import { terminalStreamByteLength } from './terminal-stream-byte-length'
|
||||
|
||||
export type TerminalOutputMeta = {
|
||||
seq?: number
|
||||
@@ -18,12 +19,31 @@ export type TerminalOutputFrameChunk = {
|
||||
opcode?: TerminalStreamOpcode
|
||||
}
|
||||
|
||||
// Why: UTF-8 length is never below UTF-16 length, so a code-unit count over the cap already proves the byte count is.
|
||||
function exceedsTerminalStreamChunkBytes(data: string): boolean {
|
||||
return (
|
||||
data.length > TERMINAL_STREAM_CHUNK_BYTES ||
|
||||
Buffer.byteLength(data, 'utf8') > TERMINAL_STREAM_CHUNK_BYTES
|
||||
)
|
||||
export const TERMINAL_STREAM_BYTE_PROBE_CODE_UNITS = 8 * 1024
|
||||
const MAX_UTF8_BYTES_PER_CODE_UNIT = 3
|
||||
|
||||
export function exceedsTerminalStreamChunkBytes(data: string): boolean {
|
||||
if (data.length > TERMINAL_STREAM_CHUNK_BYTES) {
|
||||
return true
|
||||
}
|
||||
if (data.length * MAX_UTF8_BYTES_PER_CODE_UNIT <= TERMINAL_STREAM_CHUNK_BYTES) {
|
||||
return false
|
||||
}
|
||||
let byteLength = 0
|
||||
for (let start = 0; start < data.length; ) {
|
||||
let end = Math.min(start + TERMINAL_STREAM_BYTE_PROBE_CODE_UNITS, data.length)
|
||||
const high = data.charCodeAt(end - 1)
|
||||
const low = data.charCodeAt(end)
|
||||
if (end < data.length && high >= 0xd800 && high <= 0xdbff && low >= 0xdc00 && low <= 0xdfff) {
|
||||
end -= 1
|
||||
}
|
||||
byteLength += terminalStreamByteLength(data.slice(start, end))
|
||||
if (byteLength > TERMINAL_STREAM_CHUNK_BYTES) {
|
||||
return true
|
||||
}
|
||||
start = end
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export function* iterateTerminalOutputFrameChunks(
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { createCommandCodeOutputStatusDetector } from './command-code-output-status'
|
||||
import {
|
||||
createCommandCodeOutputStatusDetector,
|
||||
stripTerminalControl
|
||||
} from './command-code-output-status'
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
@@ -317,6 +320,44 @@ function maxStringContextLength(contexts: unknown[]): number {
|
||||
}
|
||||
|
||||
describe('terminal control stripping', () => {
|
||||
const esc = String.fromCharCode(0x1b)
|
||||
const bel = String.fromCharCode(0x07)
|
||||
const ansiEscape = new RegExp(
|
||||
`${esc}(?:[@-Z\\\\-_]|\\[[0-?]*[ -/]*[@-~]|\\][^${bel}]*(?:${bel}|${esc}\\\\))`,
|
||||
'g'
|
||||
)
|
||||
const incompleteAnsiEscape = new RegExp(
|
||||
`${esc}(?:\\[[0-?]*[ -/]*|\\][^${bel}${esc}]*|\\S?)?$`,
|
||||
'g'
|
||||
)
|
||||
|
||||
function legacyStripTerminalControl(data: string): string {
|
||||
const withoutAnsi = data.replace(ansiEscape, '').replace(incompleteAnsiEscape, '')
|
||||
let output = ''
|
||||
for (let index = 0; index < withoutAnsi.length; index += 1) {
|
||||
const code = withoutAnsi.charCodeAt(index)
|
||||
if ((code <= 0x1f && code !== 0x0a && code !== 0x0d) || (code >= 0x7f && code <= 0x9f)) {
|
||||
continue
|
||||
}
|
||||
output += withoutAnsi[index]
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
function makeRandom(seed: number): () => number {
|
||||
let state = seed >>> 0
|
||||
return () => {
|
||||
state ^= state << 13
|
||||
state ^= state >>> 17
|
||||
state ^= state << 5
|
||||
return (state >>> 0) / 0x1_0000_0000
|
||||
}
|
||||
}
|
||||
|
||||
function expectLegacyEquivalent(data: string): void {
|
||||
expect(stripTerminalControl(data)).toBe(legacyStripTerminalControl(data))
|
||||
}
|
||||
|
||||
function promptFrom(raw: string): string | null {
|
||||
let captured: string | null = null
|
||||
const detector = createCommandCodeOutputStatusDetector({
|
||||
@@ -347,4 +388,78 @@ describe('terminal control stripping', () => {
|
||||
'日本語 \u{1f389} prompt'
|
||||
)
|
||||
})
|
||||
|
||||
it('matches the legacy filter across ANSI, C0/C1, line breaks, and Unicode', () => {
|
||||
for (const data of [
|
||||
'',
|
||||
'plain text',
|
||||
'\x1b[35mstyled\x1b[0m',
|
||||
'\x1b[unterminated',
|
||||
'\x00leading',
|
||||
'trailing\x9f',
|
||||
'\x01\x02adjacent\x7f\x80',
|
||||
'keep\r\nline breaks',
|
||||
'日本語 \u{1f389} \ud83d \ude00',
|
||||
'\x1b]0;title\x07prompt',
|
||||
'\x1b]0;title\x1b\\prompt'
|
||||
]) {
|
||||
expectLegacyEquivalent(data)
|
||||
}
|
||||
})
|
||||
|
||||
it('matches legacy output at density thresholds and block resets', () => {
|
||||
const control = '\x01'
|
||||
const fixtures = [
|
||||
`${control.repeat(31)}${'a'.repeat(33)}`,
|
||||
`${control.repeat(32)}${'a'.repeat(32)}`,
|
||||
`${'a'.repeat(32)}${control.repeat(31)}a`,
|
||||
`${control.repeat(31)}${'a'.repeat(33)}${control.repeat(31)}z`,
|
||||
`${control.repeat(31)}${'a'.repeat(33)}${control.repeat(32)}z`,
|
||||
`${'a'.repeat(64 * 3)}${control.repeat(32)}tail`,
|
||||
`${'a\x01'.repeat(2048)}tail`
|
||||
]
|
||||
for (const data of fixtures) {
|
||||
expectLegacyEquivalent(data)
|
||||
}
|
||||
})
|
||||
|
||||
it('exhaustively matches short strings over control and Unicode code units', () => {
|
||||
const alphabet = ['a', '\r', '\n', '\x01', '\x7f', '\x80', '\u20ac', '\ud83d']
|
||||
for (let encoded = 0; encoded < alphabet.length ** 4; encoded += 1) {
|
||||
let cursor = encoded
|
||||
let data = ''
|
||||
for (let position = 0; position < 4; position += 1) {
|
||||
data += alphabet[cursor % alphabet.length]
|
||||
cursor = Math.floor(cursor / alphabet.length)
|
||||
}
|
||||
expectLegacyEquivalent(data)
|
||||
}
|
||||
})
|
||||
|
||||
it('matches seeded random terminal text', () => {
|
||||
const random = makeRandom(0xc0de_0727)
|
||||
const alphabet = [
|
||||
'a',
|
||||
'Z',
|
||||
'\r',
|
||||
'\n',
|
||||
'\x00',
|
||||
'\x1f',
|
||||
'\x7f',
|
||||
'\x9f',
|
||||
'\u00e9',
|
||||
'\u20ac',
|
||||
'\u{1f389}',
|
||||
'\x1b[35m',
|
||||
'\x1b[0m'
|
||||
]
|
||||
for (let trial = 0; trial < 2_000; trial += 1) {
|
||||
let data = ''
|
||||
const parts = Math.floor(random() * 256)
|
||||
for (let index = 0; index < parts; index += 1) {
|
||||
data += alphabet[Math.floor(random() * alphabet.length)]
|
||||
}
|
||||
expectLegacyEquivalent(data)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -9,21 +9,14 @@ import {
|
||||
cleanCommandCodePromptCandidate,
|
||||
isCommandCodeIdlePromptCandidate
|
||||
} from './command-code-prompt-text'
|
||||
import { stripTerminalControl } from './terminal-control-stripping'
|
||||
|
||||
export { stripTerminalControl } from './terminal-control-stripping'
|
||||
|
||||
type CommandCodeOutputStatusDetector = {
|
||||
observe: (data: string) => boolean
|
||||
}
|
||||
|
||||
const ESC = String.fromCharCode(0x1b)
|
||||
const BEL = String.fromCharCode(0x07)
|
||||
const ANSI_ESCAPE_RE = new RegExp(
|
||||
`${ESC}(?:[@-Z\\\\-_]|\\[[0-?]*[ -/]*[@-~]|\\][^${BEL}]*(?:${BEL}|${ESC}\\\\))`,
|
||||
'g'
|
||||
)
|
||||
const INCOMPLETE_ANSI_ESCAPE_RE = new RegExp(
|
||||
`${ESC}(?:\\[[0-?]*[ -/]*|\\][^${BEL}${ESC}]*|\\S?)?$`,
|
||||
'g'
|
||||
)
|
||||
const RECENT_TEXT_LIMIT = 300
|
||||
const STATUS_SCAN_TEXT_LIMIT = 4096
|
||||
const COMMAND_CODE_STATUS_GLYPH_RE_SOURCE = '[·○◇☆✧⌘✻⎿]'
|
||||
@@ -121,41 +114,6 @@ const ACTIVE_EXECUTION_STATUS_RE = new RegExp(
|
||||
const IDLE_PROMPT_RE = /(?:^|[\r\n])\s*[❯>]\s+Ask your question\.\.\./
|
||||
const COMMAND_CODE_BANNER_RE = /\bCommand Code\b/
|
||||
|
||||
function stripTerminalControl(data: string): string {
|
||||
if (!terminalControlMayAffectText(data)) {
|
||||
return data
|
||||
}
|
||||
const withoutAnsi = data.replace(ANSI_ESCAPE_RE, '').replace(INCOMPLETE_ANSI_ESCAPE_RE, '')
|
||||
// Four calls per PTY chunk favor copying sparse intact runs over per-character concatenation.
|
||||
let output = ''
|
||||
let runStart = 0
|
||||
for (let index = 0; index < withoutAnsi.length; index += 1) {
|
||||
const code = withoutAnsi.charCodeAt(index)
|
||||
if ((code <= 0x1f && code !== 0x0a && code !== 0x0d) || (code >= 0x7f && code <= 0x9f)) {
|
||||
if (index > runStart) {
|
||||
output += withoutAnsi.slice(runStart, index)
|
||||
}
|
||||
runStart = index + 1
|
||||
}
|
||||
}
|
||||
return runStart === 0 ? withoutAnsi : output + withoutAnsi.slice(runStart)
|
||||
}
|
||||
|
||||
function terminalControlMayAffectText(data: string): boolean {
|
||||
for (let index = 0; index < data.length; index += 1) {
|
||||
const code = data.charCodeAt(index)
|
||||
if (
|
||||
code === 0x0d ||
|
||||
code === 0x1b ||
|
||||
(code <= 0x1f && code !== 0x0a) ||
|
||||
(code >= 0x7f && code <= 0x9f)
|
||||
) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function cleanPromptCandidate(value: string): string {
|
||||
return cleanCommandCodePromptCandidate(stripTerminalControl(value))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
const ESC = String.fromCharCode(0x1b)
|
||||
const BEL = String.fromCharCode(0x07)
|
||||
const ANSI_ESCAPE_RE = new RegExp(
|
||||
`${ESC}(?:[@-Z\\\\-_]|\\[[0-?]*[ -/]*[@-~]|\\][^${BEL}]*(?:${BEL}|${ESC}\\\\))`,
|
||||
'g'
|
||||
)
|
||||
const INCOMPLETE_ANSI_ESCAPE_RE = new RegExp(
|
||||
`${ESC}(?:\\[[0-?]*[ -/]*|\\][^${BEL}${ESC}]*|\\S?)?$`,
|
||||
'g'
|
||||
)
|
||||
const CONTROL_DENSITY_BLOCK_CODE_UNITS = 64
|
||||
const CONTROL_DENSITY_FALLBACK_COUNT = 32
|
||||
|
||||
function isStrippedTerminalControl(code: number): boolean {
|
||||
return (code <= 0x1f && code !== 0x0a && code !== 0x0d) || (code >= 0x7f && code <= 0x9f)
|
||||
}
|
||||
|
||||
export function stripTerminalControl(data: string): string {
|
||||
if (!terminalControlMayAffectText(data)) {
|
||||
return data
|
||||
}
|
||||
const withoutAnsi = data.replace(ANSI_ESCAPE_RE, '').replace(INCOMPLETE_ANSI_ESCAPE_RE, '')
|
||||
// Four calls per PTY chunk favor copying sparse intact runs over per-character concatenation.
|
||||
let output = ''
|
||||
let runStart = 0
|
||||
let strippedInBlock = 0
|
||||
let blockEnd = CONTROL_DENSITY_BLOCK_CODE_UNITS
|
||||
for (let index = 0; index < withoutAnsi.length; index += 1) {
|
||||
if (index === blockEnd) {
|
||||
strippedInBlock = 0
|
||||
blockEnd += CONTROL_DENSITY_BLOCK_CODE_UNITS
|
||||
}
|
||||
if (isStrippedTerminalControl(withoutAnsi.charCodeAt(index))) {
|
||||
if (index > runStart) {
|
||||
output += withoutAnsi.slice(runStart, index)
|
||||
}
|
||||
runStart = index + 1
|
||||
strippedInBlock += 1
|
||||
if (strippedInBlock === CONTROL_DENSITY_FALLBACK_COUNT) {
|
||||
let tailOutput = ''
|
||||
for (let tailIndex = runStart; tailIndex < withoutAnsi.length; tailIndex += 1) {
|
||||
const tailCode = withoutAnsi.charCodeAt(tailIndex)
|
||||
// Inlined isStrippedTerminalControl: this tail runs per code unit on the shape that
|
||||
// already lost to the call overhead. Keep the two copies in sync.
|
||||
if (
|
||||
(tailCode <= 0x1f && tailCode !== 0x0a && tailCode !== 0x0d) ||
|
||||
(tailCode >= 0x7f && tailCode <= 0x9f)
|
||||
) {
|
||||
continue
|
||||
}
|
||||
tailOutput += withoutAnsi[tailIndex]
|
||||
}
|
||||
return output + tailOutput
|
||||
}
|
||||
}
|
||||
}
|
||||
return runStart === 0 ? withoutAnsi : output + withoutAnsi.slice(runStart)
|
||||
}
|
||||
|
||||
function terminalControlMayAffectText(data: string): boolean {
|
||||
for (let index = 0; index < data.length; index += 1) {
|
||||
const code = data.charCodeAt(index)
|
||||
if (
|
||||
code === 0x0d ||
|
||||
code === 0x1b ||
|
||||
(code <= 0x1f && code !== 0x0a) ||
|
||||
(code >= 0x7f && code <= 0x9f)
|
||||
) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user