Skip to content

Commit 1e5f5db

Browse files
fix(server): history keeps only controls Ghostty runs inside a CSI query
Ghostty executes 0x00-0x0F inside a CSI but ignores 0x10-0x1F there, while it prints them in ground state. Saving a DLE from a stripped DSR made replay draw it and move the cursor one column right. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8840f61 commit 1e5f5db

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

‎apps/server/src/terminal/Manager.test.ts‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,23 +1417,26 @@ it.layer(
14171417
}),
14181418
);
14191419

1420-
// Ghostty executes C0 controls inside a CSI and ignores DEL, so neither changes the query.
1420+
// Ghostty executes 0x00-0x0F inside a CSI and ignores DEL and 0x10-0x1F; none change the query.
14211421
it.effect.each([
14221422
{ name: "NUL in DA", data: "A\u001b[0\u0000cB", history: "AB" },
14231423
{ name: "DEL in DA", data: "A\u001b[0\u007fcB", history: "AB" },
14241424
{ name: "NUL in DECRQM", data: "A\u001b[?2026\u0000$pB", history: "AB" },
14251425
{ name: "CR in DSR", data: "AB\u001b[5\rnC", history: "AB\rC" },
1426-
])("strips a query with an embedded control but keeps the control ($name)", ({ data, history }) =>
1427-
Effect.gen(function* () {
1428-
const { manager, ptyAdapter } = yield* createManager();
1429-
yield* manager.open(openInput());
1430-
ptyAdapter.processes[0]!.emitData(data);
1431-
yield* manager.close({ threadId: "thread-1" });
1426+
{ name: "DLE in DSR", data: "AB\u001b[5\u0010nC", history: "ABC" },
1427+
])(
1428+
"strips a query with an embedded control but keeps what the terminal ran ($name)",
1429+
({ data, history }) =>
1430+
Effect.gen(function* () {
1431+
const { manager, ptyAdapter } = yield* createManager();
1432+
yield* manager.open(openInput());
1433+
ptyAdapter.processes[0]!.emitData(data);
1434+
yield* manager.close({ threadId: "thread-1" });
14321435

1433-
const reopened = yield* manager.open(openInput());
1434-
const restored = reopened.history.replaceAll("\u0000", "");
1435-
expect(restored.split("\u001b[?1049h")[0]).toBe(history);
1436-
}),
1436+
const reopened = yield* manager.open(openInput());
1437+
const restored = reopened.history.replaceAll("\u0000", "");
1438+
expect(restored.split("\u001b[?1049h")[0]).toBe(history);
1439+
}),
14371440
);
14381441

14391442
it.effect.each([

‎apps/server/src/terminal/Manager.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,8 @@ function sanitizeTerminalHistoryChunk(
12651265

12661266
if (nextCodePoint === 0x5b) {
12671267
let cursor = index + 2;
1268-
// Ghostty executes C0 controls inside a CSI and ignores DEL; neither joins the parameters.
1268+
// Inside a CSI Ghostty executes 0x00-0x0F and ignores DEL and 0x10-0x1F, which it would
1269+
// print in ground state. None join the parameters; only executed ones survive a strip.
12691270
let body = "";
12701271
let executedControls = "";
12711272
while (cursor < input.length) {
@@ -1280,9 +1281,12 @@ function sanitizeTerminalHistoryChunk(
12801281
index = cursor + 1;
12811282
break;
12821283
}
1283-
if (cursorCodePoint < 0x20 && cursorCodePoint !== 0x1b) {
1284+
if (cursorCodePoint < 0x10) {
12841285
executedControls += input[cursor];
1285-
} else if (cursorCodePoint !== 0x7f) {
1286+
} else if (
1287+
(cursorCodePoint >= 0x20 || cursorCodePoint === 0x1b) &&
1288+
cursorCodePoint !== 0x7f
1289+
) {
12861290
body += input[cursor];
12871291
}
12881292
cursor += 1;

0 commit comments

Comments
 (0)