perf(terminal): find OSC 133 markers by their rare ']' instead of ESC

This commit is contained in:
Jinwoo-H
2026-11-09 20:38:34 -05:00
parent 8573f7ce86
commit b3fef58adc
@@ -8,6 +8,7 @@ type OscTerminator = {
}
const OSC_133_PREFIX = '\x1b]133;'
const OSC_133_BODY = OSC_133_PREFIX.slice(1)
const MAX_OSC_CARRY_LENGTH = 4096
function findOscTerminator(data: string, startIndex: number): OscTerminator | null {
@@ -31,6 +32,16 @@ function parseBestEffortExitCode(value: string | undefined): number | null {
return Number.isNaN(parsed) ? null : parsed
}
// Why: ESC starts nearly every TUI escape, so searching the rare ']' first is ~4x faster on agent output.
function findOsc133Prefix(data: string): number {
for (let i = data.indexOf(OSC_133_BODY, 1); i !== -1; i = data.indexOf(OSC_133_BODY, i + 1)) {
if (data.charCodeAt(i - 1) === 0x1b) {
return i - 1
}
}
return -1
}
function findPrefixCarry(data: string): string {
const maxCarryLength = Math.min(data.length, OSC_133_PREFIX.length - 1)
for (let length = maxCarryLength; length > 0; length -= 1) {
@@ -96,7 +107,7 @@ export function createOsc133CommandFinishedScanner(
carry = ''
while (combined.length > 0) {
const start = combined.indexOf(OSC_133_PREFIX)
const start = findOsc133Prefix(combined)
if (start === -1) {
carry = findPrefixCarry(combined)
if (carry.length < combined.length) {