Skip to content

Commit 6df47fe

Browse files
committed
fix(execute_command): show a parse-rejected command and its error in chat
When parseCommand rejects a command (for example an unterminated quote from a response the provider cut off), ExecuteCommandTool returned before askApproval. The command row that handlePartial had streamed stayed partial, often with empty text, and the error only appeared as a red-dot tooltip. Now the tool fills that row with the full command, marks it complete and answered, and posts a visible error saying the command was not run and why. The model still gets the same tool error. This adds Task.completePartialMessage(): upstream's finalizePartialToolAsk() only matches `tool` asks, and this row is a `command` ask. Like upstream's method, it skips the webview update when persisting fails, so the webview and the on-disk record cannot diverge. See also Zoo-Code-Org#1820.
1 parent 6c993fd commit 6df47fe

22 files changed

Lines changed: 154 additions & 0 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2894,6 +2894,27 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
28942894
})
28952895
}
28962896

2897+
/**
2898+
* Marks a given partial message complete so the webview stops rendering it as in progress.
2899+
*
2900+
* For callers that have already located the message, including non-`tool` asks that
2901+
* finalizePartialToolAsk does not match. Persistence failures are handled the same way.
2902+
*/
2903+
public async completePartialMessage(message: ClineMessage | undefined): Promise<void> {
2904+
if (!message?.partial) {
2905+
return
2906+
}
2907+
2908+
message.partial = false
2909+
if (!(await this.saveClineMessages())) {
2910+
console.error("[Task#completePartialMessage] saveClineMessages failed; skipping webview update")
2911+
return
2912+
}
2913+
await this.updateClineMessage(message).catch((error) => {
2914+
console.error("[Task#completePartialMessage] updateClineMessage failed:", error)
2915+
})
2916+
}
2917+
28972918
// Lifecycle
28982919
// Start / Resume / Abort / Dispose
28992920

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6725,6 +6725,67 @@ describe("Cline", () => {
67256725
saveSpy.mockRestore()
67266726
})
67276727

6728+
it("completePartialMessage clears partial, persists, and posts the update", async () => {
6729+
const task = new Task({
6730+
provider: mockProvider,
6731+
apiConfiguration: mockApiConfig,
6732+
task: "test task",
6733+
startTask: false,
6734+
})
6735+
const saveSpy = vi.spyOn(getTaskTestAccess(task), "saveClineMessages").mockResolvedValue(true)
6736+
const message = { ts: 1, type: "ask" as const, ask: "command" as const, text: "ls", partial: true }
6737+
6738+
await task.completePartialMessage(message)
6739+
6740+
expect(message.partial).toBe(false)
6741+
expect(saveSpy).toHaveBeenCalledOnce()
6742+
expect(mockProvider.postMessageToWebview).toHaveBeenCalledWith({
6743+
type: "messageUpdated",
6744+
clineMessage: expect.objectContaining({ ts: 1, partial: false }),
6745+
})
6746+
})
6747+
6748+
it("completePartialMessage leaves complete or missing messages untouched", async () => {
6749+
const task = new Task({
6750+
provider: mockProvider,
6751+
apiConfiguration: mockApiConfig,
6752+
task: "test task",
6753+
startTask: false,
6754+
})
6755+
const saveSpy = vi.spyOn(getTaskTestAccess(task), "saveClineMessages").mockResolvedValue(true)
6756+
6757+
await task.completePartialMessage(undefined)
6758+
await task.completePartialMessage({ ts: 2, type: "say", say: "text", text: "done", partial: false })
6759+
6760+
expect(saveSpy).not.toHaveBeenCalled()
6761+
expect(mockProvider.postMessageToWebview).not.toHaveBeenCalled()
6762+
})
6763+
6764+
it("completePartialMessage skips the webview update when persistence fails", async () => {
6765+
const task = new Task({
6766+
provider: mockProvider,
6767+
apiConfiguration: mockApiConfig,
6768+
task: "test task",
6769+
startTask: false,
6770+
})
6771+
vi.spyOn(getTaskTestAccess(task), "saveClineMessages").mockResolvedValue(false)
6772+
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
6773+
const message = { ts: 3, type: "ask" as const, ask: "command" as const, text: "ls", partial: true }
6774+
6775+
try {
6776+
await task.completePartialMessage(message)
6777+
6778+
expect(mockProvider.postMessageToWebview).not.toHaveBeenCalledWith(
6779+
expect.objectContaining({ type: "messageUpdated" }),
6780+
)
6781+
expect(errorSpy).toHaveBeenCalledWith(
6782+
"[Task#completePartialMessage] saveClineMessages failed; skipping webview update",
6783+
)
6784+
} finally {
6785+
errorSpy.mockRestore()
6786+
}
6787+
})
6788+
67286789
it("finalizePartialToolAsk persists and updates a non-last partial tool ask", async () => {
67296790
let updateSnapshot: Record<string, unknown> | undefined
67306791
const updateSpy = vi

‎src/core/tools/ExecuteCommandTool.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ export function resolveAgentTimeoutMs(timeoutSeconds: number | null | undefined)
7878
return process.env.ROO_CLI_RUNTIME === "1" ? 0 : requestedAgentTimeout
7979
}
8080

81+
/**
82+
* Fills in and completes the command row that `handlePartial` streamed.
83+
* Without this, a command rejected before `askApproval` leaves that row
84+
* partial with whatever text it had last, often empty (see #1820).
85+
*/
86+
async function finalizeStreamingCommandRow(task: Task, command: string): Promise<void> {
87+
const last = task.clineMessages.at(-1)
88+
if (last?.type !== "ask" || last.ask !== "command" || !last.partial) {
89+
return
90+
}
91+
last.text = command
92+
last.isAnswered = true
93+
await task.completePartialMessage(last)
94+
}
95+
8196
// Fire-and-forget: some call sites are synchronous terminal callbacks that cannot await,
8297
// and postMessageToWebview swallows its own errors, so void is enough.
8398
function postCommandExecutionStatus(provider: ClineProvider | undefined, status: CommandExecutionStatus): void {
@@ -117,6 +132,7 @@ export class ExecuteCommandTool extends BaseTool<"execute_command"> {
117132
// command, rather than receiving a generic denial from the approval dialog.
118133
const { parseError } = parseCommand(canonicalCommand)
119134
if (parseError !== null) {
135+
await finalizeStreamingCommandRow(task, canonicalCommand)
120136
const executionId = task.lastMessageTs?.toString() ?? Date.now().toString()
121137
const provider = await task.providerRef.deref()
122138
const errorStatus: CommandExecutionStatus = {
@@ -125,6 +141,7 @@ export class ExecuteCommandTool extends BaseTool<"execute_command"> {
125141
message: parseError.message,
126142
}
127143
postCommandExecutionStatus(provider, errorStatus)
144+
await task.say("error", t("tools:executeCommand.parseRejected", { error: parseError.message }))
128145
task.didToolFailInCurrentTurn = true
129146
pushToolResult(formatResponse.toolError(parseError.message))
130147
return

‎src/core/tools/__tests__/executeCommandTool.spec.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ describe("executeCommandTool", () => {
9696
mockCline = {
9797
ask: vitest.fn().mockResolvedValue(undefined),
9898
say: vitest.fn().mockResolvedValue(undefined),
99+
clineMessages: [],
99100
sayAndCreateMissingParamError: vitest.fn().mockResolvedValue("Missing parameter error"),
100101
consecutiveMistakeCount: 0,
101102
didRejectTool: false,
@@ -249,6 +250,42 @@ describe("executeCommandTool", () => {
249250
}),
250251
)
251252
expect(mockAskApproval).not.toHaveBeenCalled()
253+
expect(mockCline.say).toHaveBeenCalledWith("error", "executeCommand.parseRejected")
254+
})
255+
256+
it("completes the streamed command row with the rejected command", async () => {
257+
const streamedRow = { type: "ask", ask: "command", ts: 1, text: "", partial: true }
258+
mockCline.clineMessages = [streamedRow]
259+
mockCline.completePartialMessage = vitest.fn().mockResolvedValue(undefined)
260+
const command = "git commit -m 'unterminated"
261+
mockToolUse.params.command = command
262+
mockToolUse.nativeArgs = { command }
263+
264+
await executeCommandTool.handle(mockCline as unknown as Task, mockToolUse, {
265+
askApproval: mockAskApproval as unknown as AskApproval,
266+
handleError: mockHandleError as unknown as HandleError,
267+
pushToolResult: mockPushToolResult as unknown as PushToolResult,
268+
})
269+
270+
expect(streamedRow).toMatchObject({ text: command, isAnswered: true })
271+
expect(mockCline.completePartialMessage).toHaveBeenCalledWith(streamedRow)
272+
})
273+
274+
it("leaves a non-command last row alone on a parse error", async () => {
275+
const otherRow = { type: "say", say: "text", ts: 1, text: "hi", partial: true }
276+
mockCline.clineMessages = [otherRow]
277+
mockCline.completePartialMessage = vitest.fn().mockResolvedValue(undefined)
278+
mockToolUse.params.command = 'echo "unterminated'
279+
mockToolUse.nativeArgs = { command: 'echo "unterminated' }
280+
281+
await executeCommandTool.handle(mockCline as unknown as Task, mockToolUse, {
282+
askApproval: mockAskApproval as unknown as AskApproval,
283+
handleError: mockHandleError as unknown as HandleError,
284+
pushToolResult: mockPushToolResult as unknown as PushToolResult,
285+
})
286+
287+
expect(otherRow.text).toBe("hi")
288+
expect(mockCline.completePartialMessage).not.toHaveBeenCalled()
252289
})
253290

254291
it("posts fallback status when retrying a pre-submission shell integration failure", async () => {

‎src/i18n/locales/ca/tools.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/de/tools.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/en/tools.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"approval": "Searching for '{{query}}' in codebase..."
1616
},
1717
"executeCommand": {
18+
"parseRejected": "The command was not run because it could not be parsed: {{error}}",
1819
"destructiveCommandGuard": {
1920
"blocked": "Destructive Command Guard blocked this command.",
2021
"blockedWithReason": "Destructive Command Guard blocked this command. Message from DCG: {{reason}}",

‎src/i18n/locales/es/tools.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/fr/tools.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/i18n/locales/hi/tools.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)