From fb3105f7bfdc835d776b429a17b0fd68cba2264c Mon Sep 17 00:00:00 2001 From: canvrno <46584286+canvrno@users.noreply.github.com> Date: Thu, 17 Apr 2025 16:22:27 -0700 Subject: [PATCH] ENG-470/Chunking for large terminal outputs (#2935) * initial terminal output chunking * changeset * Update src/core/task/index.ts Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> * cleanup --------- Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com> --- .changeset/fast-readers-care.md | 5 +++ src/core/task/index.ts | 63 +++++++++++++++++++++++++++++---- 2 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 .changeset/fast-readers-care.md diff --git a/.changeset/fast-readers-care.md b/.changeset/fast-readers-care.md new file mode 100644 index 00000000..5fecc2c3 --- /dev/null +++ b/.changeset/fast-readers-care.md @@ -0,0 +1,5 @@ +--- +"claude-dev": minor +--- + +Added chunking to terminal outputs diff --git a/src/core/task/index.ts b/src/core/task/index.ts index 7bac31fa..be3e40df 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -1137,34 +1137,85 @@ export class Task { let userFeedback: { text?: string; images?: string[] } | undefined let didContinue = false - const sendCommandOutput = async (line: string): Promise => { + + // Chunked terminal output buffering + const CHUNK_LINE_COUNT = 20 + const CHUNK_BYTE_SIZE = 2048 // 2KB + const CHUNK_DEBOUNCE_MS = 100 + + let outputBuffer: string[] = [] + let outputBufferSize: number = 0 + let chunkTimer: NodeJS.Timeout | null = null + let chunkEnroute = false + + const flushBuffer = async (force = false) => { + if (chunkEnroute || outputBuffer.length === 0) { + if (force && !chunkEnroute && outputBuffer.length > 0) { + // If force is true and no chunkEnroute, flush anyway + } else { + return + } + } + const chunk = outputBuffer.join("\n") + outputBuffer = [] + outputBufferSize = 0 + chunkEnroute = true try { - const { response, text, images } = await this.ask("command_output", line) + const { response, text, images } = await this.ask("command_output", chunk) if (response === "yesButtonClicked") { // proceed while running } else { userFeedback = { text, images } } didContinue = true - process.continue() // continue past the await + process.continue() } catch { - // This can only happen if this ask promise was ignored, so ignore this error + // ask promise was ignored + } finally { + chunkEnroute = false + // If more output accumulated while chunkEnroute, flush again + if (outputBuffer.length > 0) { + await flushBuffer() + } } } + const scheduleFlush = () => { + if (chunkTimer) { + clearTimeout(chunkTimer) + } + chunkTimer = setTimeout(() => flushBuffer(), CHUNK_DEBOUNCE_MS) + } + let result = "" process.on("line", (line) => { result += line + "\n" + if (!didContinue) { - sendCommandOutput(line) + outputBuffer.push(line) + outputBufferSize += Buffer.byteLength(line, "utf8") + // Flush if buffer is large enough + if (outputBuffer.length >= CHUNK_LINE_COUNT || outputBufferSize >= CHUNK_BYTE_SIZE) { + flushBuffer() + } else { + scheduleFlush() + } } else { this.say("command_output", line) } }) let completed = false - process.once("completed", () => { + process.once("completed", async () => { completed = true + // Flush any remaining buffered output + if (!didContinue && outputBuffer.length > 0) { + if (chunkTimer) { + clearTimeout(chunkTimer) + chunkTimer = null + } + await flushBuffer(true) + } }) process.once("no_shell_integration", async () => {