Skip to content

Commit a2dc754

Browse files
edelaunaroomote
authored andcommitted
fix(task): enqueue streaming input before child completion
1 parent 3772cd3 commit a2dc754

4 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
type ContextTruncation,
3030
type ClineMessage,
3131
type ClineSay,
32+
type ClineSayTool,
3233
type ClineAsk,
3334
type ToolProgressStatus,
3435
type HistoryItem,
@@ -1264,10 +1265,10 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
12641265
return undefined
12651266
}
12661267

1267-
private drainQueuedMessageIntoAskResponse(): void {
1268+
private drainQueuedMessageIntoAskResponse(allowResolvedAskOverride = false): void {
12681269
// A synchronous auto-approval may already have resolved the ask before the
12691270
// entry queue snapshot is acted on. Never replace that resolved response.
1270-
if (this.askResponse !== undefined) {
1271+
if (this.askResponse !== undefined && !allowResolvedAskOverride) {
12711272
return
12721273
}
12731274

@@ -1451,6 +1452,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
14511452
// Keep queued user messages intact during command_output asks. Those asks
14521453
// are terminal flow-control, not conversational turns.
14531454
const shouldDrainQueuedMessageForAsk = type !== "command_output"
1455+
let isFinishTaskAsk = false
1456+
if (type === "tool") {
1457+
try {
1458+
isFinishTaskAsk = (JSON.parse(text || "{}") as ClineSayTool).tool === "finishTask"
1459+
} catch {
1460+
// Invalid tool payloads are handled by their caller; they are not finishTask asks.
1461+
}
1462+
}
14541463
const isStatusMutable = !partial && isBlocking && !isMessageQueued && approval.decision === "ask"
14551464

14561465
let queuedMessageId: string | undefined

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,22 @@ describe("ReadFileTool", () => {
687687
})
688688
parseSpy.mockRestore()
689689
})
690+
691+
it("denies batch reads for a queued message response without text", async () => {
692+
const task = Object.create(Task.prototype) as Task
693+
Object.defineProperty(task, "cwd", { value: "/test/workspace", writable: true })
694+
Object.assign(task, createMockTask())
695+
task.ask = vi.fn().mockResolvedValue({ response: "messageResponse", text: undefined, images: undefined })
696+
const fileResults = [
697+
{ path: "one.ts", status: "pending" as const, entry: { path: "one.ts", mode: "slice" as const } },
698+
{ path: "two.ts", status: "pending" as const, entry: { path: "two.ts", mode: "slice" as const } },
699+
]
700+
701+
await readFileTool["requestApproval"](task, fileResults, () => {})
702+
703+
expect(task.say).not.toHaveBeenCalledWith("user_feedback", expect.anything(), expect.anything())
704+
expect(task.didRejectTool).toBe(true)
705+
})
690706
})
691707

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

‎src/extension/__tests__/api-send-message.spec.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,42 @@ describe("API - SendMessage Command", () => {
5656
})
5757
})
5858

59+
it("should enqueue directly when the current task is streaming", async () => {
60+
const addMessage = vi.fn()
61+
const messageText = "Use this before completing"
62+
const images = ["data:image/png;base64,image1data"]
63+
const currentTask = {
64+
isStreaming: true,
65+
messageQueueService: { addMessage },
66+
}
67+
mockProvider.getCurrentTask = vi.fn().mockReturnValue(currentTask)
68+
69+
await api.sendMessage(messageText, images)
70+
71+
expect(addMessage).toHaveBeenCalledWith(messageText, images)
72+
expect(mockPostMessageToWebview).not.toHaveBeenCalled()
73+
})
74+
75+
it("should retain webview routing when the current task is not streaming", async () => {
76+
const addMessage = vi.fn()
77+
const messageText = "Answer the current ask"
78+
const currentTask = {
79+
isStreaming: false,
80+
messageQueueService: { addMessage },
81+
}
82+
mockProvider.getCurrentTask = vi.fn().mockReturnValue(currentTask)
83+
84+
await api.sendMessage(messageText)
85+
86+
expect(addMessage).not.toHaveBeenCalled()
87+
expect(mockPostMessageToWebview).toHaveBeenCalledWith({
88+
type: "invoke",
89+
invoke: "sendMessage",
90+
text: messageText,
91+
images: undefined,
92+
})
93+
})
94+
5995
it("should handle SendMessage command with text and images", async () => {
6096
// Arrange
6197
const messageText = "Analyze this image"

‎src/extension/api.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,15 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
272272
public async sendMessage(text?: string, images?: string[]) {
273273
const currentTask = this.sidebarProvider.getCurrentTask()
274274

275+
// API callers need the returned promise to mean that sequencing-critical
276+
// input has reached the active task. During a stream, the webview would
277+
// only relay this message back as queueMessage asynchronously, so enqueue
278+
// it in the extension host instead of racing task completion.
279+
if (currentTask?.isStreaming) {
280+
currentTask.messageQueueService.addMessage(text ?? "", images)
281+
return
282+
}
283+
275284
// In headless/sandbox flows the webview may not be launched, so routing
276285
// through invoke=sendMessage drops the message. Deliver directly to the
277286
// task ask-response channel instead.

0 commit comments

Comments
 (0)