Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([ansistrip-osc-alternative-unreachable]): anchor streaming OSC buffer on `\x1B]`, not last `\x1B`

[._.]/ Adam Eivy5b661ceae980
diff --git a/server/lib/ansiStrip.js b/server/lib/ansiStrip.js
index 704878f808..de0badab5e 100644
--- a/server/lib/ansiStrip.js
+++ b/server/lib/ansiStrip.js
@@ -26,7 +26,10 @@
export const ANSI_PATTERN = /\x1B(?:\](?:[^\x07\x1B]|\x1B(?!\\))*(?:\x07|\x1B\\)|[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g;
const INCOMPLETE_CSI = /^\x1B\[[0-?]*[ -/]*$/;
-const INCOMPLETE_OSC = /^\x1B\][^\x07\x1B]*$/;
+// Body grammar mirrors ANSI_PATTERN so an in-progress OSC whose body contains
+// a bare ESC (e.g. `\x1B]0;foo\x1Bbar`) is still recognised as incomplete and
+// buffered until its terminator arrives.
+const INCOMPLETE_OSC = /^\x1B\](?:[^\x07\x1B]|\x1B(?!\\))*$/;
const INCOMPLETE_ESC_2BYTE = /^\x1B$/;
// `[@-_]` (0x40-0x5F) is the byte range that legitimately follows an `\x1B`
@@ -44,17 +47,26 @@ export function createStreamingAnsiStripper() {
return (text) => {
const combined = tail + text;
tail = '';
+ // OSC anchor must come first: an in-progress OSC body may contain a bare
+ // `\x1B` (allowed by ANSI_PATTERN's body grammar), and a plain
+ // `lastIndexOf('\x1B')` would land on that body byte instead of the
+ // `\x1B]` opener, missing the incomplete sequence. Bodies longer than
+ // 4096 bytes are treated as terminated — an unbounded OSC leaks its
+ // body to display rather than pinning memory forever.
+ const lastOsc = combined.lastIndexOf('\x1B]');
+ if (lastOsc !== -1 && combined.length - lastOsc <= 4096) {
+ const candidate = combined.slice(lastOsc);
+ if (INCOMPLETE_OSC.test(candidate)) {
+ tail = candidate;
+ return STRIP(combined.slice(0, lastOsc));
+ }
+ }
+ // Fall back to the rightmost `\x1B` for CSI / bare-ESC fragments that
+ // are unambiguous (CSI parameter bytes exclude `\x1B`).
const lastEsc = combined.lastIndexOf('\x1B');
- // Only consider the trailing fragment if it lives near the end — older
- // unterminated bytes belong to a previous repaint and would never
- // resolve. Bodies longer than 4096 bytes are treated as terminated; an
- // unbounded OSC (e.g. very long hyperlink) would leak its body to
- // display rather than buffer forever.
if (lastEsc !== -1 && combined.length - lastEsc <= 4096) {
const candidate = combined.slice(lastEsc);
- if (INCOMPLETE_ESC_2BYTE.test(candidate)
- || INCOMPLETE_CSI.test(candidate)
- || INCOMPLETE_OSC.test(candidate)) {
+ if (INCOMPLETE_ESC_2BYTE.test(candidate) || INCOMPLETE_CSI.test(candidate)) {
tail = candidate;
return STRIP(combined.slice(0, lastEsc));
}
diff --git a/server/lib/ansiStrip.test.js b/server/lib/ansiStrip.test.js
index 626004c97a..8e988dfdf6 100644
--- a/server/lib/ansiStrip.test.js
+++ b/server/lib/ansiStrip.test.js
@@ -145,6 +145,16 @@ describe('createStreamingAnsiStripper', () => {
expect(strip('\x07Z')).toBe('Z');
});
+ it('buffers an in-progress OSC whose body contains a bare ESC, split before the terminator', () => {
+ // The OSC opener anchor must use `\x1B]` (not `\x1B`) so a body byte
+ // doesn't masquerade as the start of the candidate fragment. Without
+ // this, `\x1B]0;foo\x1Bbar` then `\x07after` would leak the OSC body to
+ // the cleaned stream.
+ const strip = createStreamingAnsiStripper();
+ expect(strip('\x1B]0;foo\x1Bbar')).toBe('');
+ expect(strip('\x07after')).toBe('after');
+ });
+
it('does NOT buffer an OSC body longer than 4096 bytes — it leaks instead of pinning memory', () => {
const strip = createStreamingAnsiStripper();
// OSC opens, then 4097 chars of body with no terminator. The 4096-byte