Skip to content

Commit 01183cd

Browse files
committed
fix(tools): refine execute command background handling
Adjusts the logic for handling the `runInBackground` parameter in the `executeCommand` tool. Move the backgrounding logic into the process started callback instead of the online callback in case the process we're launching doesn't produce any output.
1 parent b8c112e commit 01183cd

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/core/tools/__tests__/executeCommandTimeout.integration.spec.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,15 +422,20 @@ describe("Command Execution Timeout Integration", () => {
422422

423423
mockTerminal = {
424424
runCommand: vitest.fn().mockImplementation((command, callbacks) => {
425+
// Simulate shell execution starting (this is where runInBackground logic triggers)
426+
setTimeout(() => {
427+
callbacks.onShellExecutionStarted(12345, mockProcess)
428+
}, 5)
429+
425430
// Simulate command producing output
426431
setTimeout(() => {
427432
callbacks.onLine("Starting development server...\n", mockProcess)
428433
}, 10)
429434

430435
// Simulate command completion
431436
setTimeout(() => {
432-
callbacks.onCompleted("Development server started successfully")
433-
callbacks.onShellExecutionComplete({ exitCode: 0 })
437+
callbacks.onCompleted("Development server started successfully", mockProcess)
438+
callbacks.onShellExecutionComplete({ exitCode: 0 }, mockProcess)
434439
}, 50)
435440

436441
return Promise.resolve()

src/core/tools/executeCommandTool.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ export async function executeCommand(
178178
}
179179

180180
let message: { text?: string; images?: string[] } | undefined
181-
let runInBackground = runInBackgroundRequested
182181
let completed = false
182+
let userChoseBackgroundMode = false
183183
let result: string = ""
184184
let exitDetails: ExitCodeDetails | undefined
185185
let shellIntegrationError: string | undefined
@@ -199,16 +199,13 @@ export async function executeCommand(
199199
const status: CommandExecutionStatus = { executionId, status: "output", output: compressedOutput }
200200
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(status) })
201201

202-
// If runInBackgroundRequested, automatically continue the process
203-
if (runInBackgroundRequested && !completed) {
204-
completed = true
205-
process.continue()
202+
if (userChoseBackgroundMode) {
206203
return
207204
}
208205

209206
try {
210207
const { response, text, images } = await task.ask("command_output", "")
211-
runInBackground = true
208+
userChoseBackgroundMode = true
212209

213210
if (response === "messageResponse") {
214211
message = { text, images }
@@ -226,10 +223,16 @@ export async function executeCommand(
226223
task.say("command_output", result)
227224
completed = true
228225
},
229-
onShellExecutionStarted: (pid: number | undefined) => {
226+
onShellExecutionStarted: (pid: number | undefined, process: RooTerminalProcess) => {
230227
console.log(`[executeCommand] onShellExecutionStarted: ${pid}`)
231228
const status: CommandExecutionStatus = { executionId, status: "started", pid, command }
232229
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(status) })
230+
231+
if (runInBackgroundRequested) {
232+
userChoseBackgroundMode = true
233+
process.continue()
234+
return
235+
}
233236
},
234237
onShellExecutionComplete: (details: ExitCodeDetails) => {
235238
const status: CommandExecutionStatus = { executionId, status: "exited", exitCode: details.exitCode }

0 commit comments

Comments
 (0)