perf(agent-status): strip terminal control bytes by run, not per character (#10866)
* perf(agent-status): strip terminal control bytes by run, not per character stripTerminalControl built its result with a per-character `+=`, allocating a fresh string for every retained character. The Command Code status detector calls it four times per PTY chunk — the scan text, the chunk-boundary variant, and both previous-text lengths — so an agent pane paid that on every write. Control bytes are sparse in real output, so copy the spans between them instead: 2.3x-2.6x from 5 KiB to 106 KiB chunks. Output is byte-identical, checked exhaustively over every string up to length 4 across a 13-symbol control/unicode alphabet plus 200k random strings (224,831 inputs, 0 mismatches). * docs(agent-status): condense the run-copy rationale comments Review feedback: both comments walked through the implementation. Keep one line of non-obvious rationale each, per the repo's comment guidelines. Co-authored-by: Orca <help@stably.ai> * test(agent-status): correct terminal strip benchmark * test(agent-status): bound terminal strip benchmark --------- Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -315,3 +315,36 @@ function maxStringContextLength(contexts: unknown[]): number {
|
||||
...contexts.map((context) => (typeof context === 'string' ? context.length : 0))
|
||||
)
|
||||
}
|
||||
|
||||
describe('terminal control stripping', () => {
|
||||
function promptFrom(raw: string): string | null {
|
||||
let captured: string | null = null
|
||||
const detector = createCommandCodeOutputStatusDetector({
|
||||
startupCommand: 'command-code',
|
||||
onWorking: (prompt) => {
|
||||
captured = prompt
|
||||
}
|
||||
})
|
||||
detector.observe(raw)
|
||||
return captured
|
||||
}
|
||||
|
||||
it('strips control bytes wherever they sit around the prompt text', () => {
|
||||
expect(promptFrom('❯ plain prompt\r\n\x1b[35m✻ Thinking...\x1b[0m')).toBe('plain prompt')
|
||||
expect(promptFrom('\x07❯ leading control\r\n\x1b[35m✻ Thinking...\x1b[0m')).toBe(
|
||||
'leading control'
|
||||
)
|
||||
expect(promptFrom('❯ trailing control\x07\r\n\x1b[35m✻ Thinking...\x1b[0m')).toBe(
|
||||
'trailing control'
|
||||
)
|
||||
expect(promptFrom('❯ adjacent\x07\x01\x02controls\r\n\x1b[35m✻ Thinking...\x1b[0m')).toBe(
|
||||
'adjacentcontrols'
|
||||
)
|
||||
})
|
||||
|
||||
it('preserves multi-byte text and newlines while stripping', () => {
|
||||
expect(promptFrom('❯ 日本語 \u{1f389} prompt\r\n\x1b[35m✻ Thinking...\x1b[0m')).toBe(
|
||||
'日本語 \u{1f389} prompt'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user