@devries-cameron20 / t3code-1 / commits / 9f660d3845
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

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

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Akshar Patel committed Oct 23, 2026 at 13:44 UTC 9f660d38456f5234b6b1c359732746597cfc83e4
2 files changed +66 -2
apps/server/src/terminal/Manager.test.ts
+56
@@ -803,6 +803,62 @@ it.layer(
803 }),
804 );
805
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 + // Counts reads of strings larger than one emitted chunk, so rescanning the
829 + // pending prefix on every chunk (#23817) shows up as quadratic work.
830 + const charCodeAt = String.prototype.charCodeAt;
831 + let largeStringReads = 0;
832 + yield* Effect.gen(function* () {
833 + // eslint-disable-next-line no-extend-native -- Count reads without retaining millions of spy call records.
834 + String.prototype.charCodeAt = function (this: string, index: number) {
835 + if (this.length > 4_096) largeStringReads += 1;
836 + return charCodeAt.call(this, index);
837 + };
838 + for (const { prefix, terminator } of sequences) {
839 + for (let offset = 0; offset < prefix.length; offset += 4_096) {
840 + process.emitData(prefix.slice(offset, offset + 4_096));
841 + }
842 + process.emitData(terminator + stripped + preserved);
843 + }
844 + process.emitData("done\n");
845 + yield* Deferred.await(outputSeen);
846 + }).pipe(
847 + Effect.ensuring(
848 + Effect.sync(() => {
849 + // eslint-disable-next-line no-extend-native -- Restore the counting hook even when the test fails.
850 + String.prototype.charCodeAt = charCodeAt;
851 + }),
852 + ),
853 + );
854 + yield* manager.close({ threadId: "thread-1" });
855 +
856 + expect(largeStringReads).toBeLessThan(2 * expected.length);
857 + expect(yield* readFileString(yield* historyLogPath(logsDir))).toBe(expected);
858 + expect((yield* manager.open(openInput())).history.startsWith(expected)).toBe(true);
859 + }),
860 + );
861 +
862 it.effect(
863 "preserves chunk-split ESC sequences with intermediate bytes without leaking final bytes",
864 () =>
apps/server/src/terminal/Manager.ts
+10 -2
@@ -1147,6 +1147,8 @@ function sanitizeTerminalHistoryChunk(
1147 }
1148
1149 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);
1152 let visibleText = "";
1153 let index = 0;
1154
@@ -1189,7 +1191,10 @@ function sanitizeTerminalHistoryChunk(
1191 nextCodePoint === 0x5e ||
1192 nextCodePoint === 0x5f
1193 ) {
1192 - const terminatorIndex = findStringTerminatorIndex(input, index + 2);
1194 + const terminatorIndex = findStringTerminatorIndex(
1195 + input,
1196 + Math.max(index + 2, pendingScanStart),
1197 + );
1198 if (terminatorIndex === null) {
1199 return { visibleText, pendingControlSequence: input.slice(index) };
1200 }
@@ -1235,7 +1240,10 @@ function sanitizeTerminalHistoryChunk(
1240 }
1241
1242 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 + );
1247 if (terminatorIndex === null) {
1248 return { visibleText, pendingControlSequence: input.slice(index) };
1249 }