Skip to content

Commit a4c663a

Browse files
author
Eric Wheeler
committed
refactor: make ask() message updates atomic
The `ask()` method was directly mutating the `lastMessage` object before calling `modifyClineMessages`, which is not an atomic operation. This refactor moves the mutation logic inside the `modifyClineMessages` callback to ensure that finding, updating, and saving the message occurs as a single, transactional operation. Signed-off-by: Eric Wheeler <[email protected]>
1 parent 05f6a60 commit a4c663a

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/core/task/Task.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,13 @@ export class Task extends EventEmitter<ClineEvents> {
528528
let askTs: number
529529

530530
if (partial !== undefined) {
531-
const lastMessage = this.clineMessages.at(-1)
531+
let lastMessage = this.clineMessages.at(-1)
532+
533+
if (lastMessage === undefined) {
534+
throw new Error(
535+
`[RooCode#ask] task ${this.taskId}.${this.instanceId}: clineMessages is empty? Please report this bug.`,
536+
)
537+
}
532538

533539
const isUpdatingPreviousPartial =
534540
lastMessage && lastMessage.partial && lastMessage.type === "ask" && lastMessage.ask === type
@@ -575,16 +581,24 @@ export class Task extends EventEmitter<ClineEvents> {
575581
// never altered after first setting it.
576582
askTs = lastMessage.ts
577583
this.lastMessageTs = askTs
578-
lastMessage.text = text
579-
lastMessage.partial = false
580-
lastMessage.progressStatus = progressStatus
581-
lastMessage.isProtected = isProtected
582584

583-
await this.modifyClineMessages(async () => {
584-
return this.clineMessages
585-
})
585+
await this.modifyClineMessages(async (messages) => {
586+
lastMessage = messages.at(-1) // update ref for transaction
586587

587-
this.updateClineMessage(lastMessage)
588+
if (lastMessage) {
589+
// update these again in case of a race to guarantee flicker-free:
590+
askTs = lastMessage.ts
591+
this.lastMessageTs = askTs
592+
593+
lastMessage.text = text
594+
lastMessage.partial = false
595+
lastMessage.progressStatus = progressStatus
596+
lastMessage.isProtected = isProtected
597+
598+
this.updateClineMessage(lastMessage)
599+
}
600+
return messages
601+
})
588602
} else {
589603
// This is a new and complete message, so add it like normal.
590604
this.askResponse = undefined

0 commit comments

Comments
 (0)