Skip to content
TypeScript

perf(server): speed up large terminal clipboard sequences (#23819)

Akshar Patel9f660d38456f
diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.ts
index 413436ea452..49ee2db831a 100644
--- a/apps/server/src/terminal/Manager.test.ts
+++ b/apps/server/src/terminal/Manager.test.ts
@@ -803,6 +803,62 @@ it.layer(
}),
);
+ it.effect("preserves large chunked OSC 52 and DCS strings across reopen", () =>
+ Effect.gen(function* () {
+ const { manager, ptyAdapter, logsDir } = yield* createManager();
+ yield* manager.open(openInput());
+ const process = ptyAdapter.processes[0]!;
+ const outputSeen = yield* Deferred.make<void>();
+ const unsubscribe = yield* manager.subscribe((event) =>
+ event.type === "output" && event.data === "done\n"
+ ? Deferred.succeed(outputSeen, undefined).pipe(Effect.asVoid)
+ : Effect.void,
+ );
+ yield* Effect.addFinalizer(() => Effect.sync(unsubscribe));
+ const sequences = [
+ { prefix: `\u001b]52;c;${"YWJj".repeat(16_384)}\u001b`, terminator: "\\" },
+ { prefix: `\u0090q${"?".repeat(65_536)}`, terminator: "\u009c" },
+ ];
+ const stripped = "\u001b]11;?\u0007\u0090$q m\u009c";
+ const preserved = "\u001b[31m\u009d52;c;YWJj\u009c";
+ const expected =
+ sequences.map(({ prefix, terminator }) => prefix + terminator + preserved).join("") +
+ "done\n";
+
+ // Counts reads of strings larger than one emitted chunk, so rescanning the
+ // pending prefix on every chunk (#23817) shows up as quadratic work.
+ const charCodeAt = String.prototype.charCodeAt;
+ let largeStringReads = 0;
+ yield* Effect.gen(function* () {
+ // eslint-disable-next-line no-extend-native -- Count reads without retaining millions of spy call records.
+ String.prototype.charCodeAt = function (this: string, index: number) {
+ if (this.length > 4_096) largeStringReads += 1;
+ return charCodeAt.call(this, index);
+ };
+ for (const { prefix, terminator } of sequences) {
+ for (let offset = 0; offset < prefix.length; offset += 4_096) {
+ process.emitData(prefix.slice(offset, offset + 4_096));
+ }
+ process.emitData(terminator + stripped + preserved);
+ }
+ process.emitData("done\n");
+ yield* Deferred.await(outputSeen);
+ }).pipe(
+ Effect.ensuring(
+ Effect.sync(() => {
+ // eslint-disable-next-line no-extend-native -- Restore the counting hook even when the test fails.
+ String.prototype.charCodeAt = charCodeAt;
+ }),
+ ),
+ );
+ yield* manager.close({ threadId: "thread-1" });
+
+ expect(largeStringReads).toBeLessThan(2 * expected.length);
+ expect(yield* readFileString(yield* historyLogPath(logsDir))).toBe(expected);
+ expect((yield* manager.open(openInput())).history.startsWith(expected)).toBe(true);
+ }),
+ );
+
it.effect(
"preserves chunk-split ESC sequences with intermediate bytes without leaking final bytes",
() =>
diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts
index a75f6efccf0..9f3c6387834 100644
--- a/apps/server/src/terminal/Manager.ts
+++ b/apps/server/src/terminal/Manager.ts
@@ -1147,6 +1147,8 @@ function sanitizeTerminalHistoryChunk(
}
const input = `${pendingControlSequence}${data}`;
+ // Only the pending prefix's final ESC can begin a terminator split across chunks.
+ const pendingScanStart = Math.max(0, pendingControlSequence.length - 1);
let visibleText = "";
let index = 0;
@@ -1189,7 +1191,10 @@ function sanitizeTerminalHistoryChunk(
nextCodePoint === 0x5e ||
nextCodePoint === 0x5f
) {
- const terminatorIndex = findStringTerminatorIndex(input, index + 2);
+ const terminatorIndex = findStringTerminatorIndex(
+ input,
+ Math.max(index + 2, pendingScanStart),
+ );
if (terminatorIndex === null) {
return { visibleText, pendingControlSequence: input.slice(index) };
}
@@ -1235,7 +1240,10 @@ function sanitizeTerminalHistoryChunk(
}
if (codePoint === 0x9d || codePoint === 0x90 || codePoint === 0x9e || codePoint === 0x9f) {
- const terminatorIndex = findStringTerminatorIndex(input, index + 1);
+ const terminatorIndex = findStringTerminatorIndex(
+ input,
+ Math.max(index + 1, pendingScanStart),
+ );
if (terminatorIndex === null) {
return { visibleText, pendingControlSequence: input.slice(index) };
}