Skip to content

Commit ef499ea

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 316d9da commit ef499ea

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
@@ -530,7 +530,13 @@ export class Task extends EventEmitter<ClineEvents> {
530530
let askTs: number
531531

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

535541
const isUpdatingPreviousPartial =
536542
lastMessage && lastMessage.partial && lastMessage.type === "ask" && lastMessage.ask === type
@@ -577,16 +583,24 @@ export class Task extends EventEmitter<ClineEvents> {
577583
// never altered after first setting it.
578584
askTs = lastMessage.ts
579585
this.lastMessageTs = askTs
580-
lastMessage.text = text
581-
lastMessage.partial = false
582-
lastMessage.progressStatus = progressStatus
583-
lastMessage.isProtected = isProtected
584586

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

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

0 commit comments

Comments
 (0)