diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts index c9f492d152187..c6f45a60ab75e 100644 --- a/apps/server/src/terminal/Manager.test.ts +++ b/apps/server/src/terminal/Manager.test.ts @@ -1602,7 +1602,7 @@ it.layer( }), ); - it.effect("strips replayable CSI and DCS traffic while preserving setters", () => + it.effect("strips replayable CSI, DCS and OSC traffic while preserving setters", () => Effect.gen(function* () { const { manager, ptyAdapter } = yield* createManager(); yield* manager.open(openInput()); @@ -1618,7 +1618,13 @@ it.layer( // BEL is DCS payload, not a terminator: only ST ends the string. process.emitData("\u001bP$qm\u0007hidden\u001b\\\u001bP+q544e\u0007hidden\u001b\\"); process.emitData('\u001b[!p\u001b["p\u001b[4 q\u001b[u'); + // Ghostty sets a colour beside a query, skips an LF, an empty field and a last index with no + // colour in an OSC, and runs an OSC that CAN ends or that the next ESC cuts short. + process.emitData("\u001b]4;1;red;2;?\u0007\u001b]0;t\u001b]\n4;1;?;;2;?;3\u0018\u001b]4;3;?"); process.emitData("\u001b]0;x\u001b\u001b[?2026$p"); + // A long setter split across chunks is kept whole. + process.emitData(`\u001b[${"0".repeat(5000)}`); + process.emitData("31mX"); process.emitData("done\n"); yield* manager.close({ threadId: "thread-1" }); @@ -1626,7 +1632,9 @@ it.layer( const reopened = yield* manager.open(openInput()); expect( reopened.history.startsWith( - 'prompt \u001b[!p\u001b["p\u001b[4 q\u001b[u\u001b]0;x\u001bdone\n', + 'prompt \u001b[!p\u001b["p\u001b[4 q\u001b[u' + + "\u001b]4;1;red;2;?\u0007\u001b]0;t\u001b\\\u001b]4;3;?\u001b]0;x\u001b" + + `\u001b[${"0".repeat(5000)}31mXdone\n`, ), ).toBe(true); }), diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index dd578d2c27fb0..86ccc1d3e0dd2 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -1274,6 +1274,19 @@ function shouldStripOscSequence(content: string): boolean { return /^(10|11|12);(?:\?|rgb:)/.test(content); } +// An OSC 4 whose every colour is "?" reports palette entries and sets none. Ghostty skips C0 +// controls inside an OSC, runs one that CAN or SUB ends, and reads the pairs past empty fields. +function isOscPaletteQuery(content: string): boolean { + if (!/^[\0-\x1f]*4/.test(content) || !content.includes("?")) return false; + const [code, ...pairs] = content + .replace(/[\0-\x1f]+/g, "") + .split(";") + .filter((field) => field !== ""); + return ( + code === "4" && pairs.length > 1 && pairs.every((field, at) => at % 2 === 0 || field === "?") + ); +} + function stripStringTerminator(value: string): string { if (value.endsWith("\u001b\\")) { return value.slice(0, -2); @@ -1488,6 +1501,18 @@ function sanitizeTerminalHistoryChunk( const content = stripStringTerminator( prefix.slice(2) + input.slice(contentStart, c1?.index ?? end), ); + // ST ends what the query's ESC cut short and leaves ground state, as the query does when it + // has its own terminator. Without one, the query ends at the next ESC, which history may + // strip, so ST would not replay the same and the query stays. + if ( + nextCodePoint === 0x5d && + isOscPaletteQuery(content) && + /[\x07\x18\x1a]$|\x1b\\$/.test(input.slice(index, end)) + ) { + append("\u001b\\"); + index = end; + continue; + } const strip = (nextCodePoint === 0x5d && shouldStripOscSequence(content)) || (nextCodePoint === 0x50 && shouldStripDcsSequence(content));