Skip to content

Commit 6716e41

Browse files
committed
Fix: Ensure correct mode is persisted for resumed tasks
Introduces a `modeIsFrozen` flag in the Task class. This flag is set to `true` when a task completes or a completed task is resumed. When `modeIsFrozen` is true, the task's `currentModeSlug` is prevented from being updated if the user only changes the mode in the UI without sending a new message. This avoids saving accidental mode changes during history browsing. If a new message is sent to continue a task that was frozen: 1. `modeIsFrozen` is reset to `false`. 2. The task's `currentModeSlug` is updated to match the actual mode selected in the UI (obtained from `ClineProvider.getState()`). 3. This updated `currentModeSlug` is then used when saving task messages and metadata, ensuring the persisted history accurately reflects the mode in which the interaction occurred. This resolves a bug where re-opening a completed task, changing the mode, and then sending a message would result in the original (stale) mode being persisted in the task history, instead of the new mode used for the interaction.
1 parent 694bfd0 commit 6716e41

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/core/task/Task.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class Task extends EventEmitter<ClineEvents> {
125125
readonly taskNumber: number
126126
readonly workspacePath: string
127127
currentModeSlug: string
128-
128+
modeIsFrozen: boolean = false
129129
providerRef: WeakRef<ClineProvider>
130130
private readonly globalStoragePath: string
131131
abort: boolean = false
@@ -299,12 +299,16 @@ export class Task extends EventEmitter<ClineEvents> {
299299
}
300300

301301
public async updateCurrentModeSlug(newModeSlug: string) {
302-
this.currentModeSlug = newModeSlug
303-
// NOTE: Consider if a mode switch should immediately trigger a save of task metadata.
304-
// For now, the updated currentModeSlug will be saved with the next natural
305-
// save operation (e.g., when a new message is added).
302+
if (this.modeIsFrozen) {
303+
// If the mode is frozen, ignore any attempts to change it.
304+
return // Crucial: Exit without updating the mode
305+
}
306+
// Only update if the new mode is actually different to avoid unnecessary assignments
307+
if (this.currentModeSlug !== newModeSlug) {
308+
this.currentModeSlug = newModeSlug
309+
}
306310
}
307-
311+
308312
async overwriteApiConversationHistory(newHistory: ApiMessage[]) {
309313
this.apiConversationHistory = newHistory
310314
await this.saveApiConversationHistory()
@@ -484,6 +488,14 @@ export class Task extends EventEmitter<ClineEvents> {
484488
await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text })
485489
}
486490

491+
// Set modeIsFrozen if the task just completed
492+
if (
493+
(type === "completion_result" || type === "resume_completed_task") &&
494+
(partial === false || partial === undefined)
495+
) {
496+
this.modeIsFrozen = true
497+
}
498+
487499
await pWaitFor(() => this.askResponse !== undefined || this.lastMessageTs !== askTs, { interval: 100 })
488500

489501
if (this.lastMessageTs !== askTs) {
@@ -1081,6 +1093,20 @@ export class Task extends EventEmitter<ClineEvents> {
10811093
throw new Error(`[RooCode#recursivelyMakeRooRequests] task ${this.taskId}.${this.instanceId} aborted`)
10821094
}
10831095

1096+
// Reset modeIsFrozen when a new message is sent
1097+
if (this.modeIsFrozen) {
1098+
this.modeIsFrozen = false
1099+
// Get the actual current mode from the provider and update the task's mode
1100+
const provider = this.providerRef.deref()
1101+
if (provider) {
1102+
const providerState = await provider.getState()
1103+
if (providerState.mode !== this.currentModeSlug) {
1104+
this.currentModeSlug = providerState.mode
1105+
}
1106+
}
1107+
await this.saveClineMessages() // Save messages now reflects the unfreezing and potential mode update
1108+
}
1109+
10841110
if (this.consecutiveMistakeCount >= this.consecutiveMistakeLimit) {
10851111
const { response, text, images } = await this.ask(
10861112
"mistake_limit_reached",

0 commit comments

Comments
 (0)