Strip all ANSI control sequences in bounded terminal output buffer - #1266
Strip all ANSI control sequences in bounded terminal output buffer#1266nordicnode wants to merge 2 commits into
Conversation
- Switch BoundedOutputBuffer.append from stripColors to stripAnsi in sdk/src/tools/run-terminal-command.ts. - Update INCOMPLETE_ESCAPE_SEQUENCE_REGEX to correctly match split ECMA-48 CSI and OSC sequences across chunk boundaries. - Rename internal tracker pendingColorSequence to pendingEscapeSequence. - Add unit tests in sdk/src/__tests__/run-terminal-command.test.ts verifying stripping of line erases, cursor movement, cursor visibility, and chunk-split sequences.
|
Good catch on the underlying bug: One correctness concern with the new /\x1B(?:\[[0-?]*[ -/]*|\][^\x1B]*)?$/The OSC branch Worth either tightening the OSC alternative to require it not already contain a BEL/ST (i.e. only match genuinely truncated sequences), or adding a test that appends an OSC sequence terminated by BEL as the final chunk and asserting it doesn't vanish from Otherwise this is a well-scoped, testable change in |
- Tighten INCOMPLETE_ESCAPE_SEQUENCE_REGEX in sdk/src/tools/run-terminal-command.ts to require incomplete OSC sequences not contain BEL (\x07) or ST (\x1B\), preventing fully-terminated OSC codes from being buffered as incomplete and dropping trailing text. - Reorder ansiRegex in common/src/util/string.ts so CSI and OSC take precedence over generic 2-character Fe escapes, and support both BEL and ST terminators. - Add unit tests in sdk/src/__tests__/run-terminal-command.test.ts verifying fully-terminated OSC stripping, preservation of trailing text in the same chunk and final chunk, and chunk-split OSC. - Add unit tests in common/src/util/__tests__/string.test.ts verifying stripAnsi on colors, CSI controls, OSC BEL, OSC ST, Fe escapes, and plain text.
|
Thanks for catching this edge case! We've updated the regex, corrected
All tests, typechecks, SDK build, and binary smoke tests pass cleanly. |
Summary
BoundedOutputBuffer.appendfromstripColorstostripAnsiinsdk/src/tools/run-terminal-command.ts.INCOMPLETE_ESCAPE_SEQUENCE_REGEXto match split ECMA-48 CSI (\x1b[...) and OSC (\x1b]...) sequences across chunk boundaries, tightened to require that incomplete OSC sequences do not already contain BEL (\x07) or ST (\x1B\\), preventing fully-terminated OSC codes from buffering and dropping trailing text.ansiRegexincommon/src/util/string.tsso CSI and OSC sequences take precedence over generic 2-character Fe escapes, and support both BEL (\x07) and ST (\x1B\\) terminators.pendingColorSequencetopendingEscapeSequence.sdk/src/__tests__/run-terminal-command.test.tsverifying stripping of line clears (\x1b[2K), cursor movement (\x1b[1A), cursor visibility (\x1b[?25h), fully-terminated OSC sequences (BEL and ST), preservation of trailing text in the same chunk and final chunk, and chunk-split sequences.common/src/util/__tests__/string.test.tsverifyingstripAnsiacross colors, CSI control codes, OSC with BEL, OSC with ST, and Fe escapes.Test plan
bun test --config=/dev/null --preload test/setup-env.ts src/__tests__/run-terminal-command.test.tsinsdk/(17/17 pass)bun test src/util/__tests__/string.test.tsincommon/(26/26 pass)bun run build:sdkbun run --cwd cli typecheck(0 errors)bun freebuff/cli/build.ts 0.0.0-cibun cli/scripts/smoke-binary.ts cli/bin/freebuff(attempt 1/3 OK)bun x prettier --check sdk/src/tools/run-terminal-command.ts sdk/src/__tests__/run-terminal-command.test.ts common/src/util/string.ts common/src/util/__tests__/string.test.ts