Skip to content

Commit 3772cd3

Browse files
edelaunaroomote
authored andcommitted
fix(task): safely drain queued messages into pending asks
1 parent 928b020 commit 3772cd3

4 files changed

Lines changed: 56 additions & 4 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,19 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
12641264
return undefined
12651265
}
12661266

1267+
private drainQueuedMessageIntoAskResponse(): void {
1268+
// A synchronous auto-approval may already have resolved the ask before the
1269+
// entry queue snapshot is acted on. Never replace that resolved response.
1270+
if (this.askResponse !== undefined) {
1271+
return
1272+
}
1273+
1274+
const message = this.messageQueueService.dequeueMessage()
1275+
if (message) {
1276+
this.handleWebviewAskResponse("messageResponse", message.text, message.images)
1277+
}
1278+
}
1279+
12671280
// Note that `partial` has three valid states true (partial message),
12681281
// false (completion of partial message), undefined (individual complete
12691282
// message).

‎src/core/task/__tests__/ask-queued-message-drain.spec.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ describe("Task.ask queued message drain", () => {
5252
const task = await createTask()
5353

5454
const askPromise = task.ask("command_output", "command is still running...", false)
55-
task.messageQueueService.addMessage("1+1=?")
5655

5756
setTimeout(() => {
5857
task.approveAsk()

‎src/core/tools/ReadFileTool.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export class ReadFileTool extends BaseTool<"read_file"> {
292292
output = `IMPORTANT: File content truncated.
293293
Status: Showing lines ${start}-${end} of ${result.totalLines} total lines.
294294
To read more: Use the read_file tool with offset=${nextOffset} and limit=${effectiveLimit}.
295-
295+
296296
${result.content}`
297297
} else if (result.includedRanges.length > 0) {
298298
const rangeStr = result.includedRanges.map(([s, e]) => `${s}-${e}`).join(", ")
@@ -320,7 +320,7 @@ export class ReadFileTool extends BaseTool<"read_file"> {
320320
output = `IMPORTANT: File content truncated.
321321
Status: Showing lines ${startLine}-${endLine} of ${result.totalLines} total lines.
322322
To read more: Use the read_file tool with offset=${nextOffset} and limit=${limit}.
323-
323+
324324
${result.content}`
325325
} else if (result.returnedLines === 0) {
326326
output = "Note: File is empty"
@@ -453,7 +453,9 @@ export class ReadFileTool extends BaseTool<"read_file"> {
453453
filesToApprove.forEach((fr) => {
454454
updateFileResult(fr.path, { status: "approved", feedbackText: text, feedbackImages: images })
455455
})
456-
} else if (response === "noButtonClicked") {
456+
} else if (response === "noButtonClicked" || response === "messageResponse") {
457+
// A queued conversational message resolves the ask as messageResponse;
458+
// it is feedback, not the JSON payload used by per-file permissions.
457459
if (text) await task.say("user_feedback", text, images)
458460
task.didRejectTool = true
459461
filesToApprove.forEach((fr) => {

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import path from "path"
1717
import { isBinaryFile } from "isbinaryfile"
1818

1919
import { readFileTool, ReadFileTool } from "../ReadFileTool"
20+
import { Task } from "../../task/Task"
2021
import { formatResponse } from "../../prompts/responses"
2122
import {
2223
validateImageForProcessing,
@@ -649,6 +650,43 @@ describe("ReadFileTool", () => {
649650
expect(mockTask.say).toHaveBeenCalledWith("user_feedback", "This file contains secrets", undefined)
650651
expect(formatResponse.toolDeniedWithFeedback).toHaveBeenCalledWith("This file contains secrets")
651652
})
653+
654+
it("denies batch reads and reports queued message feedback without parsing it as permissions", async () => {
655+
const task = Object.create(Task.prototype) as Task
656+
Object.defineProperty(task, "cwd", { value: "/test/workspace", writable: true })
657+
Object.assign(task, createMockTask())
658+
const queuedImages = ["data:image/png;base64,queued"]
659+
task.ask = vi.fn().mockResolvedValue({
660+
response: "messageResponse",
661+
text: "Read a different file instead",
662+
images: queuedImages,
663+
})
664+
const fileResults = [
665+
{ path: "one.ts", status: "pending" as const, entry: { path: "one.ts", mode: "slice" as const } },
666+
{ path: "two.ts", status: "pending" as const, entry: { path: "two.ts", mode: "slice" as const } },
667+
]
668+
const updates = new Map<string, Record<string, unknown>>()
669+
const parseSpy = vi.spyOn(JSON, "parse")
670+
671+
await readFileTool["requestApproval"](task, fileResults, (filePath, update) => {
672+
updates.set(filePath, update)
673+
})
674+
675+
expect(parseSpy).not.toHaveBeenCalled()
676+
expect(task.say).toHaveBeenCalledWith("user_feedback", "Read a different file instead", queuedImages)
677+
expect(task.didRejectTool).toBe(true)
678+
expect(updates.get("one.ts")).toMatchObject({
679+
status: "denied",
680+
feedbackText: "Read a different file instead",
681+
feedbackImages: queuedImages,
682+
})
683+
expect(updates.get("two.ts")).toMatchObject({
684+
status: "denied",
685+
feedbackText: "Read a different file instead",
686+
feedbackImages: queuedImages,
687+
})
688+
parseSpy.mockRestore()
689+
})
652690
})
653691

654692
describe("output structure", () => {

0 commit comments

Comments
 (0)