Skip to content

Commit 2663615

Browse files
committed
fix: preserve parent task mode when child tasks are created
- Add taskMode property to Task class to store each task's mode independently - Update saveClineMessages to use task's own mode instead of provider's current state - Add comprehensive test coverage for sticky mode functionality - Fix error handling for provider.getState() failures This ensures that when a child task is created and switches modes, it doesn't affect the parent task's saved mode in the task history.
1 parent 0a27227 commit 2663615

File tree

2 files changed

+170
-99
lines changed

2 files changed

+170
-99
lines changed

src/core/task/Task.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export class Task extends EventEmitter<ClineEvents> {
137137
readonly parentTask: Task | undefined = undefined
138138
readonly taskNumber: number
139139
readonly workspacePath: string
140+
taskMode: string
140141

141142
providerRef: WeakRef<ClineProvider>
142143
private readonly globalStoragePath: string
@@ -268,12 +269,31 @@ export class Task extends EventEmitter<ClineEvents> {
268269
this.parentTask = parentTask
269270
this.taskNumber = taskNumber
270271

272+
// Store the task's mode when it's created
273+
// For history items, use the stored mode; for new tasks, we'll set it after getting state
274+
this.taskMode = historyItem?.mode || defaultModeSlug
275+
271276
if (historyItem) {
272277
TelemetryService.instance.captureTaskRestarted(this.taskId)
273278
} else {
274279
TelemetryService.instance.captureTaskCreated(this.taskId)
275280
}
276281

282+
// If no historyItem, get the current mode from provider
283+
if (!historyItem) {
284+
provider
285+
.getState()
286+
.then((state) => {
287+
if (state?.mode) {
288+
this.taskMode = state.mode
289+
}
290+
})
291+
.catch((error) => {
292+
// If there's an error getting state, keep the default mode
293+
console.error("Failed to get mode from provider state:", error)
294+
})
295+
}
296+
277297
// Only set up diff strategy if diff is enabled
278298
if (this.diffEnabled) {
279299
// Default to old strategy, will be updated if experiment is enabled
@@ -405,17 +425,13 @@ export class Task extends EventEmitter<ClineEvents> {
405425
globalStoragePath: this.globalStoragePath,
406426
})
407427

408-
const provider = this.providerRef.deref()
409-
const state = await provider?.getState()
410-
const currentMode = state?.mode
411-
412428
const { historyItem, tokenUsage } = await taskMetadata({
413429
messages: this.clineMessages,
414430
taskId: this.taskId,
415431
taskNumber: this.taskNumber,
416432
globalStoragePath: this.globalStoragePath,
417433
workspace: this.cwd,
418-
mode: currentMode,
434+
mode: this.taskMode, // Use the task's own mode, not the current provider mode
419435
})
420436

421437
this.emit("taskTokenUsageUpdated", this.taskId, tokenUsage)

0 commit comments

Comments
 (0)