diff --git a/extensions/cli/src/services/BackgroundJobService.ts b/extensions/cli/src/services/BackgroundJobService.ts index e4350231e..1fe85bd85 100644 --- a/extensions/cli/src/services/BackgroundJobService.ts +++ b/extensions/cli/src/services/BackgroundJobService.ts @@ -68,12 +68,15 @@ export class BackgroundJobService { const child = spawn(shell, args, { stdio: "pipe" }); this.processes.set(jobId, child); - child.stdout?.on("data", (data: Buffer) => { - this.appendOutput(jobId, data.toString()); + child.stdout?.setEncoding("utf8"); + child.stderr?.setEncoding("utf8"); + + child.stdout?.on("data", (data: string) => { + this.appendOutput(jobId, data); }); - child.stderr?.on("data", (data: Buffer) => { - this.appendOutput(jobId, data.toString()); + child.stderr?.on("data", (data: string) => { + this.appendOutput(jobId, data); }); child.on("close", (code: number | null) => { @@ -114,12 +117,15 @@ export class BackgroundJobService { this.jobs.set(id, job); this.processes.set(id, child); - child.stdout?.on("data", (data: Buffer) => { - this.appendOutput(id, data.toString()); + child.stdout?.setEncoding("utf8"); + child.stderr?.setEncoding("utf8"); + + child.stdout?.on("data", (data: string) => { + this.appendOutput(id, data); }); - child.stderr?.on("data", (data: Buffer) => { - this.appendOutput(id, data.toString()); + child.stderr?.on("data", (data: string) => { + this.appendOutput(id, data); }); child.on("close", (code: number | null) => { diff --git a/extensions/cli/src/services/index.ts b/extensions/cli/src/services/index.ts index 778c8625a..314688754 100644 --- a/extensions/cli/src/services/index.ts +++ b/extensions/cli/src/services/index.ts @@ -13,6 +13,7 @@ import { AuthService } from "./AuthService.js"; import { ChatHistoryService } from "./ChatHistoryService.js"; import { ConfigService } from "./ConfigService.js"; import { FileIndexService } from "./FileIndexService.js"; +import { backgroundJobService } from "./BackgroundJobService.js"; import { GitAiIntegrationService } from "./GitAiIntegrationService.js"; import { MCPService } from "./MCPService.js"; import { ModelService } from "./ModelService.js"; diff --git a/extensions/cli/src/tools/checkBackgroundJob.ts b/extensions/cli/src/tools/checkBackgroundJob.ts index 08968b91f..67e5c991d 100644 --- a/extensions/cli/src/tools/checkBackgroundJob.ts +++ b/extensions/cli/src/tools/checkBackgroundJob.ts @@ -1,3 +1,4 @@ +import type { BackgroundJob } from "../services/BackgroundJobService.js"; import { services } from "../services/index.js"; import { Tool } from "./types.js"; @@ -27,7 +28,7 @@ If the job is still running, returns partial output with "running" status.`, if (!job) { return JSON.stringify({ error: `Job ${job_id} not found`, - available_jobs: services.backgroundJobs.getAllJobs().map((j) => ({ + available_jobs: services.backgroundJobs.getAllJobs().map((j: BackgroundJob) => ({ id: j.id, status: j.status, command: diff --git a/extensions/cli/src/tools/runTerminalCommand.ts b/extensions/cli/src/tools/runTerminalCommand.ts index b6d8bb10f..6b640c6a1 100644 --- a/extensions/cli/src/tools/runTerminalCommand.ts +++ b/extensions/cli/src/tools/runTerminalCommand.ts @@ -231,6 +231,12 @@ IMPORTANT: To edit files, use Edit/MultiEdit tools instead of bash commands (sed } backgroundSignalManager.off("backgroundRequested", moveToBackground); + // Detach stdout/stderr listeners so they don't accumulate in local + // buffers or trigger chat history updates after the tool call resolves. + // BackgroundJobService.createJobWithProcess attaches its own listeners. + child.stdout.removeListener("data", onStdout); + child.stderr.removeListener("data", onStderr); + const job = backgroundJobService.createJobWithProcess( command, child as ChildProcess, @@ -238,8 +244,15 @@ IMPORTANT: To edit files, use Edit/MultiEdit tools instead of bash commands (sed ); if (job) { + const truncationResult = truncateOutputFromStart(stdout, { + maxChars, + maxLines, + }); + const outputSoFar = truncationResult.wasTruncated + ? appendParallelLimitNote(truncationResult.output) + : truncationResult.output; resolve( - `Command moved to background. Job ID: ${job.id}\nOutput so far:\n${stdout}\nUse CheckBackgroundJob("${job.id}") to check status.`, + `Command moved to background. Job ID: ${job.id}\nOutput so far:\n${outputSoFar}\nUse CheckBackgroundJob("${job.id}") to check status.`, ); } else { resolve( @@ -289,17 +302,20 @@ IMPORTANT: To edit files, use Edit/MultiEdit tools instead of bash commands (sed // Start the initial timeout resetTimeout(); - child.stdout.on("data", (data) => { + const onStdout = (data: Buffer) => { stdout += data.toString(); resetTimeout(); showCurrentOutput(); - }); + }; - child.stderr.on("data", (data) => { + const onStderr = (data: Buffer) => { stderr += data.toString(); resetTimeout(); showCurrentOutput(); - }); + }; + + child.stdout.on("data", onStdout); + child.stderr.on("data", onStderr); child.on("close", (code) => { if (isResolved) return; diff --git a/extensions/cli/src/ui/JobsSelector.tsx b/extensions/cli/src/ui/JobsSelector.tsx index f39136dde..a356771c3 100644 --- a/extensions/cli/src/ui/JobsSelector.tsx +++ b/extensions/cli/src/ui/JobsSelector.tsx @@ -46,7 +46,7 @@ export function JobsSelector({ onCancel }: JobsSelectorProps) { }, 1000); return () => clearInterval(interval); - }, []); + }, [viewMode, selectedJob]); useInput((input, key) => { // Handle key input when viewing job details