Skip to content

Commit d0af4c0

Browse files
zoomote[bot]navedmerchant
andauthored
[Fix] Zoo starts another step before terminal command finishes (#1145)
* fix: wait for foreground terminal command completion * test: preserve terminal provider context in completion test --------- Co-authored-by: Naved Merchant <[email protected]> Co-authored-by: Naved Merchant <[email protected]>
1 parent 64d6e37 commit d0af4c0

2 files changed

Lines changed: 48 additions & 332 deletions

File tree

‎src/core/tools/ExecuteCommandTool.ts‎

Lines changed: 3 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
ExitCodeDetails,
1818
RooTerminalCallbacks,
1919
RooTerminalProvider,
20-
RooTerminalProcess,
2120
ShellIntegrationError,
2221
ShellIntegrationErrorDetails,
2322
} from "../../integrations/terminal/types"
@@ -35,15 +34,6 @@ export function canRetryShellIntegrationError(error: unknown): error is ShellInt
3534
return error instanceof ShellIntegrationError && !error.commandSubmitted
3635
}
3736

38-
/**
39-
* Grace period before a foreground command may trigger a `command_output` ask.
40-
* Short commands that emit output and exit within this window never prompt the
41-
* user; the ask only fires when the command is still running once the delay
42-
* elapses, so users can still interrupt or provide feedback on long-running
43-
* commands.
44-
*/
45-
export const COMMAND_OUTPUT_ASK_DELAY_MS = 5_000
46-
4737
export function getTerminalProviderForExecution(terminalShellIntegrationDisabled: boolean): {
4838
terminalProvider: RooTerminalProvider
4939
isCmdExeFallback: boolean
@@ -290,14 +280,12 @@ export async function executeCommandInTerminal(
290280
return [false, `Working directory '${workingDir}' does not exist.`]
291281
}
292282

293-
let message: { text?: string; images?: string[] } | undefined
294283
let runInBackground = false
295284
let completed = false
296285
let result: string = ""
297286
let persistedResult: PersistedCommandOutput | undefined
298287
let exitDetails: ExitCodeDetails | undefined
299288
let shellIntegrationError: ShellIntegrationError | undefined
300-
let hasAskedForCommandOutput = false
301289

302290
const { terminalProvider, isCmdExeFallback } = getTerminalProviderForExecution(terminalShellIntegrationDisabled)
303291
const provider = await task.providerRef.deref()
@@ -390,60 +378,8 @@ export async function executeCommandInTerminal(
390378
resolveOnCompleted = resolve
391379
})
392380

393-
// Delay the `command_output` ask so short foreground commands that emit
394-
// output and exit normally never prompt the user. The ask only fires if the
395-
// command is still running once COMMAND_OUTPUT_ASK_DELAY_MS has elapsed
396-
// since execution started, preserving the interrupt/feedback path for
397-
// long-running commands. The anchor is re-based to onShellExecutionStarted
398-
// (falling back to the pre-runCommand timestamp when that event never
399-
// fires) so shell-integration startup on cold terminals does not consume
400-
// the grace period.
401-
let commandStartedAt = 0
402-
let commandOutputAskTimer: NodeJS.Timeout | undefined
403-
404-
const askForCommandOutput = async (process: RooTerminalProcess): Promise<void> => {
405-
if (runInBackground || hasAskedForCommandOutput || completed) {
406-
return
407-
}
408-
409-
// Mark that we've asked to prevent multiple concurrent asks
410-
hasAskedForCommandOutput = true
411-
412-
try {
413-
const { response, text, images } = await task.ask("command_output", "")
414-
runInBackground = true
415-
416-
if (response === "messageResponse") {
417-
message = { text, images }
418-
}
419-
420-
// Any answer means the command should keep running in the background;
421-
// continue the process so the tool resolves now instead of blocking
422-
// until the command actually completes.
423-
process.continue()
424-
} catch (_error) {
425-
// Silently handle ask errors (e.g., "Current ask promise was ignored")
426-
}
427-
}
428-
429-
const scheduleCommandOutputAsk = (process: RooTerminalProcess): void => {
430-
if (runInBackground || hasAskedForCommandOutput || completed || commandOutputAskTimer) {
431-
return
432-
}
433-
434-
const remainingDelay = COMMAND_OUTPUT_ASK_DELAY_MS - (Date.now() - commandStartedAt)
435-
436-
commandOutputAskTimer = setTimeout(
437-
() => {
438-
commandOutputAskTimer = undefined
439-
void askForCommandOutput(process)
440-
},
441-
Math.max(remainingDelay, 0),
442-
)
443-
}
444-
445381
const callbacks: RooTerminalCallbacks = {
446-
onLine: async (lines: string, process: RooTerminalProcess) => {
382+
onLine: async (lines: string) => {
447383
accumulatedOutput += lines
448384

449385
// Trim accumulated output to prevent unbounded memory growth
@@ -460,20 +396,8 @@ export async function executeCommandInTerminal(
460396
const status: CommandExecutionStatus = { executionId, status: "output", output: compressedOutput }
461397
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(status) })
462398
schedulePartialCommandOutputUpdate()
463-
464-
scheduleCommandOutputAsk(process)
465399
},
466400
onCompleted: async (output: string | undefined) => {
467-
clearTimeout(commandOutputAskTimer)
468-
commandOutputAskTimer = undefined
469-
470-
// If an interactive command_output ask is still pending, supersede it
471-
// so it resolves immediately instead of lingering until the next
472-
// interactive message bumps lastMessageTs.
473-
if (hasAskedForCommandOutput && !runInBackground) {
474-
task.supersedePendingAsk()
475-
}
476-
477401
clearTimeout(pendingCommandOutputEmitTimer)
478402
pendingCommandOutputEmitTimer = undefined
479403

@@ -507,21 +431,9 @@ export async function executeCommandInTerminal(
507431
console.error("[ExecuteCommandTool] Failed to flush final command_output:", error)
508432
})
509433
},
510-
onShellExecutionStarted: (pid: number | undefined, process: RooTerminalProcess) => {
434+
onShellExecutionStarted: (pid: number | undefined) => {
511435
const status: CommandExecutionStatus = { executionId, status: "started", pid, command }
512436
provider?.postMessageToWebview({ type: "commandExecutionStatus", text: JSON.stringify(status) })
513-
514-
// Re-anchor the ask delay to actual execution start so the shell
515-
// integration startup wait does not count against the grace period.
516-
commandStartedAt = Date.now()
517-
518-
// Output should not precede this event, but if it did, reschedule
519-
// the pending ask against the corrected anchor.
520-
if (commandOutputAskTimer) {
521-
clearTimeout(commandOutputAskTimer)
522-
commandOutputAskTimer = undefined
523-
scheduleCommandOutputAsk(process)
524-
}
525437
},
526438
onShellExecutionComplete: (details: ExitCodeDetails) => {
527439
const status: CommandExecutionStatus = { executionId, status: "exited", exitCode: details.exitCode }
@@ -548,8 +460,6 @@ export async function executeCommandInTerminal(
548460
workingDir = terminal.getCurrentWorkingDirectory()
549461
}
550462

551-
// Fallback anchor for providers that never fire onShellExecutionStarted.
552-
commandStartedAt = Date.now()
553463
const process = terminal.runCommand(command, callbacks)
554464
task.terminalProcess = process
555465

@@ -571,8 +481,6 @@ export async function executeCommandInTerminal(
571481
new Promise<void>((resolve) => {
572482
agentTimeoutId = setTimeout(() => {
573483
runInBackground = true
574-
clearTimeout(commandOutputAskTimer)
575-
commandOutputAskTimer = undefined
576484
process.continue()
577485
task.supersedePendingAsk()
578486
resolve()
@@ -612,7 +520,6 @@ export async function executeCommandInTerminal(
612520
} finally {
613521
clearTimeout(agentTimeoutId)
614522
clearTimeout(userTimeoutId)
615-
clearTimeout(commandOutputAskTimer)
616523
clearTimeout(pendingCommandOutputEmitTimer)
617524
task.terminalProcess = undefined
618525
}
@@ -637,22 +544,7 @@ export async function executeCommandInTerminal(
637544
await onCompletedPromise
638545
}
639546

640-
if (message) {
641-
const { text, images } = message
642-
await task.say("user_feedback", text, images)
643-
644-
return [
645-
true,
646-
formatResponse.toolResult(
647-
[
648-
`Command is still running in terminal from '${terminal.getCurrentWorkingDirectory().toPosix()}'.`,
649-
result.length > 0 ? `Here's the output so far:\n${result}\n` : "\n",
650-
`<user_message>\n${text}\n</user_message>`,
651-
].join("\n"),
652-
images,
653-
),
654-
]
655-
} else if (completed || exitDetails) {
547+
if (completed || exitDetails) {
656548
const currentWorkingDir = terminal.getCurrentWorkingDirectory().toPosix()
657549

658550
// Use persisted output format when output was truncated and spilled to disk

0 commit comments

Comments
 (0)