Skip to content

Commit 5ad8d06

Browse files
pnupuclaude
andcommitted
fix(server): track only the Kitty and CSI input libghostty-vt acts on
The scanner folded the 8-bit CSI byte into ESC `[` and treated any Kitty set mode as a replacement. libghostty-vt does neither: U+009B, whether raw or as the UTF-8 pair the history carries, is not a sequence introducer, and set modes other than 1 (replace), 2 (set bits), and 3 (clear bits) are ignored. Both let the tracker record flags as zero while the replayed emulator kept release reporting on. Match the emulator: split only on ESC and skip unsupported set modes. Regression tests cover both histories. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 5c216bd commit 5ad8d06

2 files changed

Lines changed: 42 additions & 14 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,33 @@ it.layer(
15761576
yield* cleared.manager.close({ threadId: "thread-1" });
15771577
const afterCleared = yield* cleared.manager.open(openInput());
15781578
assert.equal(afterCleared.history, "\u001b[>7u\u001b[=7;3u");
1579+
1580+
// libghostty-vt ignores set modes other than 1, 2, and 3, so the flags stay.
1581+
const unsupported = yield* createManager();
1582+
yield* unsupported.manager.open(openInput());
1583+
unsupported.ptyAdapter.processes[0]!.emitData("\u001b[>7u\u001b[=0;4u");
1584+
yield* unsupported.manager.close({ threadId: "thread-1" });
1585+
const afterUnsupported = yield* unsupported.manager.open(openInput());
1586+
assert.equal(afterUnsupported.history, "\u001b[>7u\u001b[=0;4u\u001b[=0;1u");
1587+
}),
1588+
);
1589+
1590+
it.effect("ignores the 8-bit CSI byte like the client's replay parser does", () =>
1591+
Effect.gen(function* () {
1592+
const { manager, ptyAdapter } = yield* createManager();
1593+
yield* manager.open(openInput());
1594+
const process = ptyAdapter.processes[0];
1595+
expect(process).toBeDefined();
1596+
if (!process) return;
1597+
1598+
// A U+009B introducer is not a sequence to libghostty-vt, so a "clear"
1599+
// written that way never happened and the push still needs its reset.
1600+
process.emitData("\u001b[>7u\u009b=0;1u");
1601+
1602+
yield* manager.close({ threadId: "thread-1" });
1603+
1604+
const reopened = yield* manager.open(openInput());
1605+
assert.equal(reopened.history, "\u001b[>7u\u009b=0;1u\u001b[=0;1u");
15791606
}),
15801607
);
15811608

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,18 +1027,17 @@ const INHERITED_DEC_MODE_DEFAULTS = new Map<number, boolean>([
10271027
const ALTERNATE_SCREEN_DEC_MODES = [47, 1047, 1049];
10281028
// Kitty keyboard flags live on a stack: `CSI > flags u` pushes, `CSI < n u`
10291029
// pops, `CSI = flags ; mode u` edits the top (mode 1 replaces, 2 sets bits,
1030-
// 3 clears bits). The main and alternate screens keep separate stacks, and
1031-
// the encoder only reads the active top, so zeroing it is enough; only RIS
1032-
// clears the stacks.
1030+
// 3 clears bits; libghostty-vt ignores any other mode). The main and
1031+
// alternate screens keep separate stacks, and the encoder only reads the
1032+
// active top, so zeroing it is enough; only RIS clears the stacks.
10331033
const KITTY_KEYBOARD_CLEAR = "\u001b[=0;1u";
1034-
// Every sequence of interest starts with ESC (after folding the 8-bit CSI
1035-
// byte into ESC `[`), so the history is split there and each fragment's head
1036-
// is matched: DEC mode set/reset, Kitty keyboard push/pop/set, or RIS (`c`).
1037-
// RIS is the only reset that touches these in libghostty-vt; DECSTR
1038-
// (`CSI ! p`) leaves every one of them alone, so it is deliberately not
1039-
// tracked.
1034+
// Every sequence of interest starts with ESC, so the history is split there
1035+
// 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.
10401040
const ESCAPE = "\u001b";
1041-
const CSI_8BIT = "\u009b";
10421041
const INHERITED_MODE_SEQUENCE =
10431042
/^(?:\[(?:\?([0-9;]+)([hl])|([<>=])([0-9]*)(?:;([0-9]*))?[0-9;]*u)|(c))/u;
10441043

@@ -1047,8 +1046,7 @@ function neutralizeInheritedHistory(history: string): string {
10471046
const modes = new Map<number, boolean>();
10481047
const kittyStacks = { main: [0], alternate: [0] };
10491048
let screen: keyof typeof kittyStacks = "main";
1050-
const fragments = history.replaceAll(CSI_8BIT, `${ESCAPE}[`).split(ESCAPE);
1051-
for (const fragment of fragments.slice(1)) {
1049+
for (const fragment of history.split(ESCAPE).slice(1)) {
10521050
const match = INHERITED_MODE_SEQUENCE.exec(fragment);
10531051
if (match === null) continue;
10541052
if (match[6] !== undefined) {
@@ -1067,8 +1065,11 @@ function neutralizeInheritedHistory(history: string): string {
10671065
const flags = Number.isNaN(parameter) ? 0 : parameter;
10681066
const top = stack.length - 1;
10691067
const current = stack[top] ?? 0;
1070-
const setMode = Number.parseInt(match[5] ?? "", 10);
1071-
stack[top] = setMode === 2 ? current | flags : setMode === 3 ? current & ~flags : flags;
1068+
const setMode =
1069+
match[5] === undefined || match[5] === "" ? 1 : Number.parseInt(match[5], 10);
1070+
if (setMode === 1) stack[top] = flags;
1071+
else if (setMode === 2) stack[top] = current | flags;
1072+
else if (setMode === 3) stack[top] = current & ~flags;
10721073
} else {
10731074
const count = Number.isNaN(parameter) ? 1 : parameter;
10741075
stack.length = Math.max(1, stack.length - count);

0 commit comments

Comments
 (0)