-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat: bash tool execution in background #10285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
uinstinct
wants to merge
13
commits into
continuedev:main
Choose a base branch
from
uinstinct:cli-background-bash-exec
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+675
−4
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b4ae98e
add background job manager and output display
uinstinct 5a479ca
added checkBackgroundJob tool
uinstinct 561f85e
move process to background
uinstinct 60bef4b
add the jobs slash command
uinstinct 0070946
kill all background jobs on exit
uinstinct 149a27c
show ctrl+b to move to background
uinstinct a87d44c
show terminal output during streaming
uinstinct c1e79cd
remove unused events in backgroundjobmanager
uinstinct f89de1b
keep background job output upto 1000 lines
uinstinct 5a5a908
truncate job output to 1000 lines
uinstinct c7d6a73
show separate screen for background jobs
uinstinct 001e553
refactor backgroundsignalmanger
uinstinct 1d2cba8
rename background job manager to background job service
uinstinct File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| import { ChildProcess, spawn } from "child_process"; | ||
|
|
||
| import { logger } from "../util/logger.js"; | ||
|
|
||
| export type BackgroundJobStatus = | ||
| | "pending" | ||
| | "running" | ||
| | "completed" | ||
| | "failed" | ||
| | "cancelled"; | ||
|
|
||
| export interface BackgroundJob { | ||
| id: string; | ||
| status: BackgroundJobStatus; | ||
| command: string; | ||
| output: string; | ||
| exitCode: number | null; | ||
| startTime: Date; | ||
| endTime: Date | null; | ||
| error?: string; | ||
| } | ||
|
|
||
| const MAX_CONCURRENT_JOBS = 5; | ||
| const MAX_OUTPUT_LINES = 1000; | ||
|
|
||
| /** | ||
| * Service for managing background job execution and lifecycle | ||
| * Handles spawning, tracking, and cleanup of background processes | ||
| */ | ||
| export class BackgroundJobService { | ||
| private jobs: Map<string, BackgroundJob> = new Map(); | ||
| private processes: Map<string, ChildProcess> = new Map(); | ||
| private jobCounter = 0; | ||
|
|
||
| createJob(command: string): BackgroundJob | null { | ||
| const runningCount = this.getRunningJobCount(); | ||
| if (runningCount >= MAX_CONCURRENT_JOBS) { | ||
| logger.warn( | ||
| `Cannot create background job: limit of ${MAX_CONCURRENT_JOBS} reached`, | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| const id = `bg-${++this.jobCounter}-${Date.now()}`; | ||
| const job: BackgroundJob = { | ||
| id, | ||
| status: "pending", | ||
| command, | ||
| output: "", | ||
| exitCode: null, | ||
| startTime: new Date(), | ||
| endTime: null, | ||
| }; | ||
|
|
||
| this.jobs.set(id, job); | ||
| return job; | ||
| } | ||
|
|
||
| startJob(jobId: string, shell: string, args: string[]): ChildProcess | null { | ||
| const job = this.jobs.get(jobId); | ||
| if (!job) { | ||
| logger.error(`Cannot start job ${jobId}: job not found`); | ||
| return null; | ||
| } | ||
|
|
||
| job.status = "running"; | ||
|
|
||
| const child = spawn(shell, args, { stdio: "pipe" }); | ||
| this.processes.set(jobId, child); | ||
|
|
||
| child.stdout?.on("data", (data: Buffer) => { | ||
| this.appendOutput(jobId, data.toString()); | ||
| }); | ||
|
|
||
| child.stderr?.on("data", (data: Buffer) => { | ||
| this.appendOutput(jobId, data.toString()); | ||
| }); | ||
|
|
||
| child.on("close", (code: number | null) => { | ||
| this.completeJob(jobId, code ?? 0); | ||
| }); | ||
|
|
||
| child.on("error", (error: Error) => { | ||
| this.failJob(jobId, error.message); | ||
| }); | ||
|
|
||
| return child; | ||
| } | ||
|
|
||
| createJobWithProcess( | ||
| command: string, | ||
| child: ChildProcess, | ||
| existingOutput: string = "", | ||
| ): BackgroundJob | null { | ||
| const runningCount = this.getRunningJobCount(); | ||
| if (runningCount >= MAX_CONCURRENT_JOBS) { | ||
| logger.warn( | ||
| `Cannot create background job: limit of ${MAX_CONCURRENT_JOBS} reached`, | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| const id = `bg-${++this.jobCounter}-${Date.now()}`; | ||
| const job: BackgroundJob = { | ||
| id, | ||
| status: "running", | ||
| command, | ||
| output: existingOutput, | ||
| exitCode: null, | ||
| startTime: new Date(), | ||
| endTime: null, | ||
| }; | ||
|
|
||
| this.jobs.set(id, job); | ||
| this.processes.set(id, child); | ||
|
|
||
| child.stdout?.on("data", (data: Buffer) => { | ||
| this.appendOutput(id, data.toString()); | ||
| }); | ||
|
|
||
| child.stderr?.on("data", (data: Buffer) => { | ||
| this.appendOutput(id, data.toString()); | ||
| }); | ||
|
|
||
| child.on("close", (code: number | null) => { | ||
| this.completeJob(id, code ?? 0); | ||
| }); | ||
|
|
||
| child.on("error", (error: Error) => { | ||
| this.failJob(id, error.message); | ||
| }); | ||
|
|
||
| return job; | ||
| } | ||
|
|
||
| // todo: improve write efficiency with ring buffer or similar | ||
| appendOutput(jobId: string, data: string): void { | ||
| const job = this.jobs.get(jobId); | ||
| if (job) { | ||
| job.output += data; | ||
| const lines = job.output.split("\n"); | ||
| if (lines.length > MAX_OUTPUT_LINES) { | ||
| job.output = lines.slice(-MAX_OUTPUT_LINES).join("\n"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| completeJob(jobId: string, exitCode: number): void { | ||
| const job = this.jobs.get(jobId); | ||
| if (job) { | ||
| if (job.status === "cancelled") { | ||
| return; | ||
| } | ||
| job.status = exitCode === 0 ? "completed" : "failed"; | ||
| job.exitCode = exitCode; | ||
| job.endTime = new Date(); | ||
| this.processes.delete(jobId); | ||
| } | ||
| } | ||
|
|
||
| failJob(jobId: string, error: string): void { | ||
| const job = this.jobs.get(jobId); | ||
| if (job) { | ||
| job.status = "failed"; | ||
| job.error = error; | ||
| job.endTime = new Date(); | ||
| this.processes.delete(jobId); | ||
| } | ||
| } | ||
|
|
||
| cancelJob(jobId: string): boolean { | ||
| const job = this.jobs.get(jobId); | ||
| const process = this.processes.get(jobId); | ||
|
|
||
| if (!job) return false; | ||
|
|
||
| if (process) { | ||
| process.kill(); | ||
| this.processes.delete(jobId); | ||
| } | ||
|
|
||
| job.status = "cancelled"; | ||
| job.endTime = new Date(); | ||
| return true; | ||
| } | ||
|
|
||
| getJob(jobId: string): BackgroundJob | undefined { | ||
| return this.jobs.get(jobId); | ||
| } | ||
|
|
||
| getRunningJobs(): BackgroundJob[] { | ||
| return Array.from(this.jobs.values()).filter( | ||
| (job) => job.status === "running" || job.status === "pending", | ||
| ); | ||
| } | ||
|
|
||
| getAllJobs(): BackgroundJob[] { | ||
| return Array.from(this.jobs.values()); | ||
| } | ||
|
|
||
| getRunningJobCount(): number { | ||
| return this.getRunningJobs().length; | ||
| } | ||
|
|
||
| killAllJobs(): void { | ||
| for (const [jobId, process] of this.processes) { | ||
| process.kill(); | ||
| const job = this.jobs.get(jobId); | ||
| if (job) { | ||
| job.status = "cancelled"; | ||
| job.endTime = new Date(); | ||
| } | ||
| } | ||
| this.processes.clear(); | ||
| } | ||
| } | ||
|
|
||
| export const backgroundJobService = new BackgroundJobService(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { services } from "../services/index.js"; | ||
|
|
||
| import { Tool } from "./types.js"; | ||
|
|
||
| export const checkBackgroundJobTool: Tool = { | ||
| name: "CheckBackgroundJob", | ||
| displayName: "Check Background Job", | ||
| description: `Check the status and output of a background job. | ||
| Returns the current status, exit code (if finished), and all available output. | ||
| If the job is still running, returns partial output with "running" status.`, | ||
| parameters: { | ||
| type: "object", | ||
| required: ["job_id"], | ||
| properties: { | ||
| job_id: { | ||
| type: "string", | ||
| description: | ||
| "The ID of the background job to check (e.g., bg-1-1234567890)", | ||
| }, | ||
| }, | ||
| }, | ||
| readonly: true, | ||
| isBuiltIn: true, | ||
| run: async ({ job_id }: { job_id: string }): Promise<string> => { | ||
| const job = services.backgroundJobs.getJob(job_id); | ||
|
|
||
| if (!job) { | ||
| return JSON.stringify({ | ||
| error: `Job ${job_id} not found`, | ||
| available_jobs: services.backgroundJobs.getAllJobs().map((j) => ({ | ||
| id: j.id, | ||
| status: j.status, | ||
| command: | ||
| j.command.substring(0, 50) + (j.command.length > 50 ? "..." : ""), | ||
| })), | ||
| }); | ||
| } | ||
|
|
||
| const result: Record<string, unknown> = { | ||
| job_id: job.id, | ||
| status: job.status, | ||
| command: job.command, | ||
| output: job.output, | ||
| started_at: job.startTime.toISOString(), | ||
| }; | ||
|
|
||
| if (job.endTime) { | ||
| result.ended_at = job.endTime.toISOString(); | ||
| result.duration_seconds = Math.round( | ||
| (job.endTime.getTime() - job.startTime.getTime()) / 1000, | ||
| ); | ||
| } | ||
|
|
||
| if (job.exitCode !== null) { | ||
| result.exit_code = job.exitCode; | ||
| } | ||
|
|
||
| if (job.error) { | ||
| result.error = job.error; | ||
| } | ||
|
|
||
| return JSON.stringify(result, null, 2); | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Output capture decodes each Buffer chunk with toString(), which can corrupt multi‑byte characters split across chunks. Use StringDecoder or setEncoding on the streams to preserve UTF‑8 sequences across chunk boundaries.
Prompt for AI agents