Skip to content

Commit 5341dbd

Browse files
committed
fix: fix the mode switch when continue with the parent task
1 parent 7e25625 commit 5341dbd

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/core/task/Task.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,13 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
372372
if (historyItem) {
373373
this._taskMode = historyItem.mode || defaultModeSlug
374374
this.taskModeReady = Promise.resolve()
375+
provider.log(`[Task#${this.taskId}] Initialized from history with mode: ${this._taskMode}`)
375376
TelemetryService.instance.captureTaskRestarted(this.taskId)
376377
} else {
377378
// For new tasks, don't set the mode yet - wait for async initialization.
378379
this._taskMode = undefined
379380
this.taskModeReady = this.initializeTaskMode(provider)
381+
provider.log(`[Task#${this.taskId}] New task - will initialize mode asynchronously`)
380382
TelemetryService.instance.captureTaskCreated(this.taskId)
381383
}
382384

@@ -455,12 +457,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
455457
try {
456458
const state = await provider.getState()
457459
this._taskMode = state?.mode || defaultModeSlug
460+
provider.log(`[Task#${this.taskId}] Initialized mode from provider state: ${this._taskMode}`)
458461
} catch (error) {
459462
// If there's an error getting state, use the default mode
460463
this._taskMode = defaultModeSlug
461464
// Use the provider's log method for better error visibility
462465
const errorMessage = `Failed to initialize task mode: ${error instanceof Error ? error.message : String(error)}`
463466
provider.log(errorMessage)
467+
provider.log(`[Task#${this.taskId}] Fell back to default mode: ${this._taskMode}`)
464468
}
465469
}
466470

@@ -1607,6 +1611,13 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
16071611
const newTask = await provider.createTask(message, undefined, this, { initialTodos })
16081612

16091613
if (newTask) {
1614+
// Store the current task's mode before pausing
1615+
const currentTaskMode = await this.getTaskMode()
1616+
this.pausedModeSlug = currentTaskMode
1617+
provider.log(
1618+
`[startSubtask] Storing parent task mode '${currentTaskMode}' in pausedModeSlug before pausing`,
1619+
)
1620+
16101621
this.isPaused = true // Pause parent.
16111622
this.childTaskId = newTask.taskId
16121623

@@ -1756,6 +1767,9 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
17561767

17571768
if (currentMode !== this.pausedModeSlug) {
17581769
// The mode has changed, we need to switch back to the paused mode.
1770+
provider.log(
1771+
`[subtasks] task ${this.taskId}.${this.instanceId} switching back to paused mode '${this.pausedModeSlug}' from current mode '${currentMode}'`,
1772+
)
17591773
await provider.handleModeSwitch(this.pausedModeSlug)
17601774

17611775
// Delay to allow mode change to take effect before next tool is executed.
@@ -1764,6 +1778,8 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
17641778
provider.log(
17651779
`[subtasks] task ${this.taskId}.${this.instanceId} has switched back to '${this.pausedModeSlug}' from '${currentMode}'`,
17661780
)
1781+
} else {
1782+
provider.log(`[subtasks] task ${this.taskId}.${this.instanceId} mode unchanged: '${currentMode}'`)
17671783
}
17681784
}
17691785

src/core/webview/ClineProvider.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,34 @@ export class ClineProvider
15531553
this.log(`[continueParentTask] Found parent task ${parentTask.taskId}, isPaused: ${parentTask.isPaused}`)
15541554
this.log(`[continueParentTask] Parent task isInitialized: ${parentTask.isInitialized}`)
15551555

1556+
// Log mode information for debugging
1557+
const currentProviderMode = (await this.getState()).mode
1558+
const parentTaskMode =
1559+
typeof parentTask.getTaskMode === "function"
1560+
? await parentTask.getTaskMode()
1561+
: (parentTask as any)._taskMode || parentTask.pausedModeSlug || "unknown"
1562+
this.log(`[continueParentTask] Current provider mode: ${currentProviderMode}`)
1563+
this.log(`[continueParentTask] Parent task mode: ${parentTaskMode}`)
1564+
this.log(`[continueParentTask] Parent task pausedModeSlug: ${parentTask.pausedModeSlug}`)
1565+
15561566
try {
1567+
// Switch provider mode to match parent task's mode if they differ
1568+
if (currentProviderMode !== parentTaskMode && parentTaskMode !== "unknown") {
1569+
this.log(
1570+
`[continueParentTask] Provider mode (${currentProviderMode}) differs from parent task mode (${parentTaskMode})`,
1571+
)
1572+
this.log(`[continueParentTask] Switching provider mode to match parent task: ${parentTaskMode}`)
1573+
1574+
try {
1575+
await this.handleModeSwitch(parentTaskMode as any)
1576+
this.log(`[continueParentTask] Successfully switched provider mode to: ${parentTaskMode}`)
1577+
} catch (error) {
1578+
this.log(
1579+
`[continueParentTask] Failed to switch provider mode: ${error instanceof Error ? error.message : String(error)}`,
1580+
)
1581+
}
1582+
}
1583+
15571584
// If the parent task is not initialized, we need to initialize it properly
15581585
if (!parentTask.isInitialized) {
15591586
this.log(`[continueParentTask] Initializing parent task from history`)
@@ -1679,6 +1706,35 @@ export class ClineProvider
16791706
}
16801707

16811708
this.log(`[reconstructTaskStack] Successfully reconstructed stack with ${createdTasks.length} tasks`)
1709+
1710+
// Log final mode state after reconstruction
1711+
const finalProviderMode = (await this.getState()).mode
1712+
const targetTask = createdTasks[createdTasks.length - 1]
1713+
// Safety check for getTaskMode method (may not exist in tests)
1714+
const targetTaskMode =
1715+
typeof targetTask.getTaskMode === "function"
1716+
? await targetTask.getTaskMode()
1717+
: (targetTask as any)._taskMode || "unknown"
1718+
this.log(`[reconstructTaskStack] Final provider mode after reconstruction: ${finalProviderMode}`)
1719+
this.log(`[reconstructTaskStack] Target task final mode: ${targetTaskMode}`)
1720+
1721+
// Check if provider mode matches target task mode
1722+
if (finalProviderMode !== targetTaskMode) {
1723+
this.log(
1724+
`[reconstructTaskStack] WARNING: Provider mode (${finalProviderMode}) does not match target task mode (${targetTaskMode})`,
1725+
)
1726+
this.log(`[reconstructTaskStack] Switching provider mode to match target task mode: ${targetTaskMode}`)
1727+
1728+
// Switch provider mode to match the target task's mode
1729+
try {
1730+
await this.handleModeSwitch(targetTaskMode as any)
1731+
this.log(`[reconstructTaskStack] Successfully switched provider mode to: ${targetTaskMode}`)
1732+
} catch (error) {
1733+
this.log(
1734+
`[reconstructTaskStack] Failed to switch provider mode: ${error instanceof Error ? error.message : String(error)}`,
1735+
)
1736+
}
1737+
}
16821738
} catch (error) {
16831739
this.log(
16841740
`[reconstructTaskStack] ERROR during reconstruction: ${error instanceof Error ? error.message : String(error)}`,

0 commit comments

Comments
 (0)