Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions apps/server/src/terminal/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -1618,15 +1618,23 @@ 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" });

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);
}),
Expand Down
25 changes: 25 additions & 0 deletions apps/server/src/terminal/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
Loading