Skip to content

Commit 579f035

Browse files
pnupuclaude
andcommitted
fix(server): reset keypad and key encoding modes in inherited history
libghostty-vt stores DEC modes 66, 67, 1035, and 1036 and the key encoder reads them when the client syncs it, so a dead process that left them set changes backspace, alt, and keypad encoding for the new shell. Track them like the other modes, including DECKPAM/DECKPNM (`ESC =` / `ESC >`) which terminfo smkx/rmkx use for mode 66. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent ac78e60 commit 579f035

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,28 @@ it.layer(
14671467
}),
14681468
);
14691469

1470+
it.effect("resets keypad and key encoding modes a dead process left in inherited history", () =>
1471+
Effect.gen(function* () {
1472+
const { manager, ptyAdapter } = yield* createManager();
1473+
yield* manager.open(openInput());
1474+
const process = ptyAdapter.processes[0];
1475+
expect(process).toBeDefined();
1476+
if (!process) return;
1477+
1478+
// terminfo smkx sends DECKPAM as a plain escape, not a CSI mode, and the
1479+
// key encoder reads modes 66, 67, and 1036 when the client syncs it.
1480+
process.emitData("prompt % vim\n\u001b[?1h\u001b=\u001b[?67h\u001b[?1036l");
1481+
1482+
yield* manager.close({ threadId: "thread-1" });
1483+
1484+
const reopened = yield* manager.open(openInput());
1485+
assert.equal(
1486+
reopened.history,
1487+
"prompt % vim\n\u001b[?1h\u001b=\u001b[?67h\u001b[?1036l\u001b[?1l\u001b[?66l\u001b[?67l\u001b[?1036h",
1488+
);
1489+
}),
1490+
);
1491+
14701492
it.effect("leaves the alternate screen before restoring the cursor in inherited history", () =>
14711493
Effect.gen(function* () {
14721494
const { manager, ptyAdapter } = yield* createManager();

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,13 +1012,17 @@ const INHERITED_DEC_MODE_DEFAULTS = new Map<number, boolean>([
10121012
[9, false], // X10 mouse reporting
10131013
[25, true], // cursor visible
10141014
[47, false], // legacy alternate screen
1015+
[66, false], // application keypad (also set by DECKPAM, `ESC =`)
1016+
[67, false], // backarrow key sends BS
10151017
[1000, false], // mouse press/release tracking
10161018
[1002, false], // mouse button-event tracking
10171019
[1003, false], // mouse any-event tracking
10181020
[1004, false], // focus reporting
10191021
[1005, false], // UTF-8 mouse encoding
10201022
[1006, false], // SGR mouse encoding
10211023
[1015, false], // urxvt mouse encoding
1024+
[1035, true], // numlock overrides application keypad
1025+
[1036, true], // alt sends ESC prefix
10221026
[1047, false], // alternate screen buffer
10231027
[1049, false], // alternate screen with cursor save
10241028
[2004, false], // bracketed paste
@@ -1033,13 +1037,14 @@ const ALTERNATE_SCREEN_DEC_MODES = [47, 1047, 1049];
10331037
const KITTY_KEYBOARD_CLEAR = "\u001b[=0;1u";
10341038
// Every sequence of interest starts with ESC, so the history is split there
10351039
// and each fragment's head is matched: DEC mode set/reset, Kitty keyboard
1036-
// push/pop/set, or RIS (`c`). The scanner tracks only what the client's
1037-
// replay parser acts on: libghostty-vt ignores the 8-bit CSI byte (U+009B,
1038-
// which the UTF-8 history would carry as C2 9B) and leaves every tracked
1039-
// mode alone on DECSTR (`CSI ! p`), so neither is treated as a sequence.
1040+
// push/pop/set, DECKPAM/DECKPNM (`ESC =` / `ESC >`, which toggle mode 66),
1041+
// or RIS (`c`). The scanner tracks only what the client's replay parser acts
1042+
// on: libghostty-vt ignores the 8-bit CSI byte (U+009B, which the UTF-8
1043+
// history would carry as C2 9B) and leaves every tracked mode alone on DECSTR
1044+
// (`CSI ! p`), so neither is treated as a sequence.
10401045
const ESCAPE = "\u001b";
10411046
const INHERITED_MODE_SEQUENCE =
1042-
/^(?:\[(?:\?([0-9;]+)([hl])|([<>=])([0-9]*)(?:;([0-9]*))?[0-9;]*u)|(c))/u;
1047+
/^(?:\[(?:\?([0-9;]+)([hl])|([<>=])([0-9]*)(?:;([0-9]*))?[0-9;]*u)|(c)|([=>]))/u;
10431048

10441049
function neutralizeInheritedHistory(history: string): string {
10451050
if (history.length === 0) return history;
@@ -1056,6 +1061,10 @@ function neutralizeInheritedHistory(history: string): string {
10561061
screen = "main";
10571062
continue;
10581063
}
1064+
if (match[7] !== undefined) {
1065+
modes.set(66, match[7] === "=");
1066+
continue;
1067+
}
10591068
if (match[3] !== undefined) {
10601069
const stack = kittyStacks[screen];
10611070
const parameter = Number.parseInt(match[4] ?? "", 10);

0 commit comments

Comments
 (0)