fix: address TypeScript errors and review comments

- Add missing import for backgroundJobService in services/index.ts
- Add explicit type annotation for BackgroundJob parameter in checkBackgroundJob.ts
- Fix stale closure in JobsSelector useEffect by adding viewMode/selectedJob to deps
- Truncate stdout when moving command to background to prevent context overflow
- Detach stdout/stderr listeners after backgrounding to prevent memory growth
- Use setEncoding('utf8') on streams to prevent multi-byte character corruption

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Nate 2026-02-18 12:35:05 -08:00
parent 1d2cba889c
commit 1313806901
5 changed files with 39 additions and 15 deletions

View file

@ -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) => {

View file

@ -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";

View file

@ -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:

View file

@ -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;

View file

@ -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