Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(server): strip replayable terminal queries from history (#5319)
Wout Stiens1cbd88aba042
diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.tsindex 1cf7e8dff..e60d15ab3 100644--- a/apps/server/src/terminal/Manager.test.ts+++ b/apps/server/src/terminal/Manager.test.ts@@ -991,6 +991,61 @@ it.layer(}),);+ it.effect("strips replayable CSI and DCS traffic while preserving setters", () =>+ Effect.gen(function* () {+ const { manager, ptyAdapter } = yield* createManager();+ yield* manager.open(openInput());+ const process = ptyAdapter.processes[0];+ expect(process).toBeDefined();+ if (!process) return;++ process.emitData("prompt ");+ // DECRQM/DECRPM, XTVERSION, and kitty-keyboard CSI query/reply traffic.+ process.emitData("\u001b[?2026$p\u001b[?2026;2$y\u001b[>q\u001b[?u\u001b[?31u");+ // DECRQSS and XTGETTCAP query/reply traffic in 7-bit DCS form.+ process.emitData("\u001bP$q m\u001b\\\u001bP1$r0m\u001b\\");+ process.emitData("\u001bP+q544e\u001b\\\u001bP1+r544e=1b\u001b\\");+ // The same DCS traffic in 8-bit form.+ process.emitData("\u0090$q m\u009c\u00901$r0m\u009c");+ process.emitData("\u0090+q544e\u009c\u00901+r544e=1b\u009c");+ // Setters and cursor movement share final bytes with query families but+ // have visible terminal-state value and must survive replay.+ process.emitData('\u001b[!p\u001b["p\u001b[4 q\u001b[u');+ process.emitData("done\n");++ yield* manager.close({ threadId: "thread-1" });++ const reopened = yield* manager.open(openInput());+ assert.equal(reopened.history, 'prompt \u001b[!p\u001b["p\u001b[4 q\u001b[udone\n');+ }),+ );++ it.effect("handles CSI and DCS query sequences split across output chunks", () =>+ Effect.gen(function* () {+ const { manager, ptyAdapter } = yield* createManager();+ yield* manager.open(openInput());+ const process = ptyAdapter.processes[0];+ expect(process).toBeDefined();+ if (!process) return;++ process.emitData("before ");+ process.emitData("\u001b[?2026$");+ process.emitData("pafter ");+ process.emitData("\u001bP$q ");+ process.emitData("m\u001b");+ process.emitData("\\after ");+ process.emitData("\u009b?3");+ process.emitData("1uafter ");+ process.emitData("\u0090+q544e");+ process.emitData("\u009cafter\n");++ yield* manager.close({ threadId: "thread-1" });++ const reopened = yield* manager.open(openInput());+ assert.equal(reopened.history, "before after after after after\n");+ }),+ );+it.effect("preserves clear and style control sequences while dropping chunk-split query traffic",() =>diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.tsindex caa5106bb..0b68d20f6 100644--- a/apps/server/src/terminal/Manager.ts+++ b/apps/server/src/terminal/Manager.ts@@ -878,9 +878,31 @@ function shouldStripCsiSequence(body: string, finalByte: string): boolean {if (finalByte === "c" && /^[>0-9;?]*$/.test(body)) {return true;}+ // DECRQM mode queries (…$p) and DECRPM replies (…$y): replaying a stored+ // query makes the terminal answer again, and the shell echoes the answer as+ // junk at the prompt. The `$` guard keeps setters like DECSTR (!p) and+ // DECSCL ("p) intact.+ if ((finalByte === "p" || finalByte === "y") && /^[0-9;?]*\$$/.test(body)) {+ return true;+ }+ // XTVERSION query (>q). DECSCUSR (space-intermediate q) stays.+ if (finalByte === "q" && /^>[0-9;]*$/.test(body)) {+ return true;+ }+ // Kitty keyboard protocol query/reply (?u). Restore-cursor (bare u) stays.+ if (finalByte === "u" && body.startsWith("?")) {+ return true;+ }return false;}+// DECRQSS ($q) and XTGETTCAP (+q) queries plus their replies ([01]$r / [01]+r):+// pure request/response traffic with no visual value, and replaying a stored+// query triggers a fresh reply.+function shouldStripDcsSequence(content: string): boolean {+ return /^[01]?[$+][qr]/.test(content);+}+function shouldStripOscSequence(content: string): boolean {return /^(10|11|12);(?:\?|rgb:)/.test(content);}@@ -981,7 +1003,10 @@ function sanitizeTerminalHistoryChunk(}const sequence = input.slice(index, terminatorIndex);const content = stripStringTerminator(input.slice(index + 2, terminatorIndex));- if (nextCodePoint !== 0x5d || !shouldStripOscSequence(content)) {+ const strip =+ (nextCodePoint === 0x5d && shouldStripOscSequence(content)) ||+ (nextCodePoint === 0x50 && shouldStripDcsSequence(content));+ if (!strip) {append(sequence);}index = terminatorIndex;@@ -1024,7 +1049,10 @@ function sanitizeTerminalHistoryChunk(}const sequence = input.slice(index, terminatorIndex);const content = stripStringTerminator(input.slice(index + 1, terminatorIndex));- if (codePoint !== 0x9d || !shouldStripOscSequence(content)) {+ const strip =+ (codePoint === 0x9d && shouldStripOscSequence(content)) ||+ (codePoint === 0x90 && shouldStripDcsSequence(content));+ if (!strip) {append(sequence);}index = terminatorIndex;