Skip to content

Commit 9d2e612

Browse files
committed
perf(server): speed up large terminal clipboard sequences
1 parent 22840ab commit 9d2e612

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,43 @@ it.layer(
803803
}),
804804
);
805805

806+
it.effect("preserves large chunked OSC 52 and DCS strings across reopen", () =>
807+
Effect.gen(function* () {
808+
const { manager, ptyAdapter, logsDir } = yield* createManager();
809+
yield* manager.open(openInput());
810+
const process = ptyAdapter.processes[0]!;
811+
const outputSeen = yield* Deferred.make<void>();
812+
const unsubscribe = yield* manager.subscribe((event) =>
813+
event.type === "output" && event.data === "done\n"
814+
? Deferred.succeed(outputSeen, undefined).pipe(Effect.asVoid)
815+
: Effect.void,
816+
);
817+
yield* Effect.addFinalizer(() => Effect.sync(unsubscribe));
818+
const sequences = [
819+
{ prefix: `\u001b]52;c;${"YWJj".repeat(16_384)}\u001b`, terminator: "\\" },
820+
{ prefix: `\u0090q${"?".repeat(65_536)}`, terminator: "\u009c" },
821+
];
822+
const stripped = "\u001b]11;?\u0007\u0090$q m\u009c";
823+
const preserved = "\u001b[31m\u009d52;c;YWJj\u009c";
824+
const expected =
825+
sequences.map(({ prefix, terminator }) => prefix + terminator + preserved).join("") +
826+
"done\n";
827+
828+
for (const { prefix, terminator } of sequences) {
829+
for (let offset = 0; offset < prefix.length; offset += 4_096) {
830+
process.emitData(prefix.slice(offset, offset + 4_096));
831+
}
832+
process.emitData(terminator + stripped + preserved);
833+
}
834+
process.emitData("done\n");
835+
yield* Deferred.await(outputSeen);
836+
yield* manager.close({ threadId: "thread-1" });
837+
838+
expect(yield* readFileString(yield* historyLogPath(logsDir))).toBe(expected);
839+
expect((yield* manager.open(openInput())).history.startsWith(expected)).toBe(true);
840+
}),
841+
);
842+
806843
it.effect(
807844
"preserves chunk-split ESC sequences with intermediate bytes without leaking final bytes",
808845
() =>

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,8 @@ function sanitizeTerminalHistoryChunk(
11471147
}
11481148

11491149
const input = `${pendingControlSequence}${data}`;
1150+
// Only the pending prefix's final ESC can begin a terminator split across chunks.
1151+
const pendingScanStart = Math.max(0, pendingControlSequence.length - 1);
11501152
let visibleText = "";
11511153
let index = 0;
11521154

@@ -1189,7 +1191,10 @@ function sanitizeTerminalHistoryChunk(
11891191
nextCodePoint === 0x5e ||
11901192
nextCodePoint === 0x5f
11911193
) {
1192-
const terminatorIndex = findStringTerminatorIndex(input, index + 2);
1194+
const terminatorIndex = findStringTerminatorIndex(
1195+
input,
1196+
Math.max(index + 2, pendingScanStart),
1197+
);
11931198
if (terminatorIndex === null) {
11941199
return { visibleText, pendingControlSequence: input.slice(index) };
11951200
}
@@ -1235,7 +1240,10 @@ function sanitizeTerminalHistoryChunk(
12351240
}
12361241

12371242
if (codePoint === 0x9d || codePoint === 0x90 || codePoint === 0x9e || codePoint === 0x9f) {
1238-
const terminatorIndex = findStringTerminatorIndex(input, index + 1);
1243+
const terminatorIndex = findStringTerminatorIndex(
1244+
input,
1245+
Math.max(index + 1, pendingScanStart),
1246+
);
12391247
if (terminatorIndex === null) {
12401248
return { visibleText, pendingControlSequence: input.slice(index) };
12411249
}

0 commit comments

Comments
 (0)