Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

Stop rescanning the whole paste on every stdin chunk (#2782)

Sebastian Müller9c9f3d7eaf2a
diff --git a/packages/tui/.changes/stdin-buffer-scan.md b/packages/tui/.changes/stdin-buffer-scan.md
new file mode 100644
index 0000000000..08eaac6c08
--- /dev/null
+++ b/packages/tui/.changes/stdin-buffer-scan.md
@@ -0,0 +1 @@
+- Fixed very large bracketed pastes slowing input handling: the paste end marker is now found without rescanning the whole paste on every chunk.
diff --git a/packages/tui/src/stdin-buffer.ts b/packages/tui/src/stdin-buffer.ts
index c4d9d02381..b77877b037 100644
--- a/packages/tui/src/stdin-buffer.ts
+++ b/packages/tui/src/stdin-buffer.ts
@@ -235,6 +235,9 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
private readonly timeoutMs: number;
private pasteMode: boolean = false;
private pasteBuffer: string = "";
+ // Last BRACKETED_PASTE_END.length - 1 chars of pasteBuffer. Searching pasteTail + chunk finds an end
+ // marker split across chunks without flattening and rescanning the whole paste on every chunk.
+ private pasteTail: string = "";
private pendingKittyPrintableCodepoint: number | undefined;
constructor(options: StdinBufferOptions = {}) {
@@ -270,24 +273,9 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
this.buffer += str;
if (this.pasteMode) {
- this.pasteBuffer += this.buffer;
+ const chunk = this.buffer;
this.buffer = "";
-
- const endIndex = this.pasteBuffer.indexOf(BRACKETED_PASTE_END);
- if (endIndex !== -1) {
- const pastedContent = this.pasteBuffer.slice(0, endIndex);
- const remaining = this.pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length);
-
- this.pasteMode = false;
- this.pasteBuffer = "";
- this.pendingKittyPrintableCodepoint = undefined;
-
- this.emit("paste", pastedContent);
-
- if (remaining.length > 0) {
- this.process(remaining);
- }
- }
+ this.appendPaste(chunk);
return;
}
@@ -302,26 +290,10 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
}
this.pendingKittyPrintableCodepoint = undefined;
- this.buffer = this.buffer.slice(startIndex + BRACKETED_PASTE_START.length);
- this.pasteMode = true;
- this.pasteBuffer = this.buffer;
+ const afterStart = this.buffer.slice(startIndex + BRACKETED_PASTE_START.length);
this.buffer = "";
-
- const endIndex = this.pasteBuffer.indexOf(BRACKETED_PASTE_END);
- if (endIndex !== -1) {
- const pastedContent = this.pasteBuffer.slice(0, endIndex);
- const remaining = this.pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length);
-
- this.pasteMode = false;
- this.pasteBuffer = "";
- this.pendingKittyPrintableCodepoint = undefined;
-
- this.emit("paste", pastedContent);
-
- if (remaining.length > 0) {
- this.process(remaining);
- }
- }
+ this.pasteMode = true;
+ this.appendPaste(afterStart);
return;
}
@@ -351,6 +323,30 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
}
}
+ private appendPaste(chunk: string): void {
+ const searchWindow = this.pasteTail + chunk;
+ const windowIndex = searchWindow.indexOf(BRACKETED_PASTE_END);
+ if (windowIndex === -1) {
+ this.pasteBuffer += chunk;
+ this.pasteTail = searchWindow.slice(-(BRACKETED_PASTE_END.length - 1));
+ return;
+ }
+
+ const content = this.pasteBuffer + chunk;
+ const endIndex = this.pasteBuffer.length - this.pasteTail.length + windowIndex;
+ this.pasteMode = false;
+ this.pasteBuffer = "";
+ this.pasteTail = "";
+ this.pendingKittyPrintableCodepoint = undefined;
+
+ this.emit("paste", content.slice(0, endIndex));
+
+ const remaining = content.slice(endIndex + BRACKETED_PASTE_END.length);
+ if (remaining.length > 0) {
+ this.process(remaining);
+ }
+ }
+
private emitDataSequence(sequence: string): void {
const rawCodepoint = sequence.length === 1 ? sequence.codePointAt(0) : undefined;
if (rawCodepoint !== undefined && rawCodepoint === this.pendingKittyPrintableCodepoint) {
@@ -386,6 +382,7 @@ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
this.buffer = "";
this.pasteMode = false;
this.pasteBuffer = "";
+ this.pasteTail = "";
this.pendingKittyPrintableCodepoint = undefined;
}
diff --git a/packages/tui/test/stdin-buffer.test.ts b/packages/tui/test/stdin-buffer.test.ts
index 1e8f2fd00c..76807ab729 100644
--- a/packages/tui/test/stdin-buffer.test.ts
+++ b/packages/tui/test/stdin-buffer.test.ts
@@ -359,16 +359,17 @@ describe("StdinBuffer", () => {
assert.deepStrictEqual(emittedSequences, []); // No data events during paste
});
- it("should handle paste arriving in chunks", () => {
- processInput("\x1b[200~");
- assert.deepStrictEqual(emittedPaste, []);
-
- processInput("hello ");
- assert.deepStrictEqual(emittedPaste, []);
-
- processInput("world\x1b[201~");
- assert.deepStrictEqual(emittedPaste, ["hello world"]);
- assert.deepStrictEqual(emittedSequences, []);
+ it("should handle paste start and end markers split at any chunk boundary", () => {
+ const input = "a\x1b[200~hello\x1b[201~b";
+ for (let i = 1; i < input.length; i++) {
+ for (let j = i + 1; j < input.length; j++) {
+ buffer.clear();
+ emittedSequences.length = 0;
+ emittedPaste.length = 0;
+ for (const chunk of [input.slice(0, i), input.slice(i, j), input.slice(j)]) processInput(chunk);
+ assert.deepStrictEqual([emittedSequences, emittedPaste], [["a", "b"], ["hello"]]);
+ }
+ }
});
it("should handle paste with input before and after", () => {