Skip to content

Commit 5c22b8d

Browse files
committed
fix: keep nested subtask delegation active
1 parent 5e8fcc8 commit 5c22b8d

3 files changed

Lines changed: 65 additions & 10 deletions

File tree

‎src/__tests__/provider-delegation.spec.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,9 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
249249
// Provider-level event
250250
expect(providerEmit).toHaveBeenCalledWith(RooCodeEventName.TaskDelegated, "parent-1", "child-1")
251251

252-
// Mode switch
253-
expect(handleModeSwitch).toHaveBeenCalledWith("code")
252+
// The parent has already been removed, so the mode switch must not publish a
253+
// transient empty-task state before the child is created.
254+
expect(handleModeSwitch).toHaveBeenCalledWith("code", undefined, { preparePendingTask: true })
254255
})
255256

256257
it("posts taskHistoryItemUpdated to the webview when isViewLaunched is true", async () => {

‎src/core/webview/ClineProvider.ts‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,15 +1696,20 @@ export class ClineProvider
16961696
* @param targetTask The task whose in-memory mode should be updated. Defaults to the
16971697
* current task. Pass null to apply only global mode/profile effects for a pending child.
16981698
*/
1699-
public async handleModeSwitch(newMode: Mode, targetTask: Task | null | undefined = this.getCurrentTask()) {
1699+
public async handleModeSwitch(
1700+
newMode: Mode,
1701+
targetTask: Task | null | undefined = this.getCurrentTask(),
1702+
options: { preparePendingTask?: boolean } = {},
1703+
) {
17001704
return this.enqueueProviderProfileMutation((signal) =>
1701-
this.handleModeSwitchUnlocked(newMode, targetTask, signal),
1705+
this.handleModeSwitchUnlocked(newMode, targetTask, options, signal),
17021706
)
17031707
}
17041708

17051709
private async handleModeSwitchUnlocked(
17061710
newMode: Mode,
17071711
targetTask: Task | null | undefined,
1712+
options: { preparePendingTask?: boolean },
17081713
signal?: AbortSignal,
17091714
): Promise<void> {
17101715
const task = targetTask
@@ -1744,7 +1749,7 @@ export class ClineProvider
17441749
// If workspace lock is on, keep the current API config — don't load mode-specific config
17451750
const lockApiConfigAcrossModes = this.context.workspaceState.get("lockApiConfigAcrossModes", false)
17461751
if (lockApiConfigAcrossModes) {
1747-
if (targetTask !== null) {
1752+
if (targetTask !== null && !options.preparePendingTask) {
17481753
await this.postStateToWebview()
17491754
}
17501755
return
@@ -1778,7 +1783,15 @@ export class ClineProvider
17781783
if (hasActualSettings) {
17791784
await this.activateProviderProfileUnlocked(
17801785
{ name: profile.name },
1781-
targetTask === null ? { skipCurrentTaskRebuild: true } : undefined,
1786+
targetTask === null
1787+
? { skipCurrentTaskRebuild: true }
1788+
: options.preparePendingTask
1789+
? {
1790+
skipCurrentTaskRebuild: true,
1791+
applyProviderSettingsToContext: true,
1792+
suppressStatePost: true,
1793+
}
1794+
: undefined,
17821795
signal,
17831796
)
17841797
} else {
@@ -1800,7 +1813,7 @@ export class ClineProvider
18001813
}
18011814
}
18021815

1803-
if (targetTask !== null) {
1816+
if (targetTask !== null && !options.preparePendingTask) {
18041817
await this.postStateToWebview()
18051818
}
18061819
}
@@ -1978,6 +1991,8 @@ export class ClineProvider
19781991
persistModeConfig?: boolean
19791992
persistTaskHistory?: boolean
19801993
skipCurrentTaskRebuild?: boolean
1994+
applyProviderSettingsToContext?: boolean
1995+
suppressStatePost?: boolean
19811996
},
19821997
) {
19831998
return this.enqueueProviderProfileMutation((signal) =>
@@ -1991,6 +2006,8 @@ export class ClineProvider
19912006
persistModeConfig?: boolean
19922007
persistTaskHistory?: boolean
19932008
skipCurrentTaskRebuild?: boolean
2009+
applyProviderSettingsToContext?: boolean
2010+
suppressStatePost?: boolean
19942011
},
19952012
signal?: AbortSignal,
19962013
): Promise<void> {
@@ -2001,8 +2018,10 @@ export class ClineProvider
20012018
const persistModeConfig = options?.persistModeConfig ?? true
20022019
const persistTaskHistory = options?.persistTaskHistory ?? true
20032020
const skipCurrentTaskRebuild = options?.skipCurrentTaskRebuild ?? false
2021+
const applyProviderSettingsToContext = options?.applyProviderSettingsToContext ?? !skipCurrentTaskRebuild
2022+
const suppressStatePost = options?.suppressStatePost ?? false
20042023

2005-
if (!skipCurrentTaskRebuild) {
2024+
if (applyProviderSettingsToContext) {
20062025
// See `upsertProviderProfile` for a description of what this is doing.
20072026
await Promise.all([
20082027
this.contextProxy.setValue("listApiConfigMeta", await this.providerSettingsManager.listConfig()),
@@ -2026,7 +2045,7 @@ export class ClineProvider
20262045
await this.persistStickyProviderProfileToCurrentTask(name, { skipCurrentTaskRebuild })
20272046
}
20282047

2029-
if (!skipCurrentTaskRebuild) {
2048+
if (!skipCurrentTaskRebuild && !suppressStatePost) {
20302049
await this.postStateToWebview()
20312050
}
20322051

@@ -3885,7 +3904,7 @@ export class ClineProvider
38853904
// The mode switch must happen before createTask() because the Task constructor
38863905
// initializes its mode from provider.getState() during initializeTaskMode().
38873906
try {
3888-
await this.handleModeSwitch(mode as any)
3907+
await this.handleModeSwitch(mode as any, undefined, { preparePendingTask: true })
38893908
} catch (e) {
38903909
this.log(
38913910
`[delegateParentAndOpenChild] handleModeSwitch failed for mode '${mode}': ${

‎src/core/webview/__tests__/ClineProvider.apiHandlerRebuild.spec.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,41 @@ describe("ClineProvider - API Handler Rebuild Guard", () => {
612612
expect(provider["providerSettingsManager"].activateProfile).toHaveBeenCalledWith({ name: "ask-profile" })
613613
})
614614

615+
test("pending child preparation applies its profile without posting an empty task state", async () => {
616+
const unrelatedTask = new Task(defaultTaskOptions)
617+
await provider.addClineToStack(unrelatedTask)
618+
provider["providerSettingsManager"].getModeConfigId = vi.fn().mockResolvedValue("ask-id")
619+
provider["providerSettingsManager"].listConfig = vi
620+
.fn()
621+
.mockResolvedValue([{ name: "ask-profile", id: "ask-id", apiProvider: providerIdentifiers.openrouter }])
622+
provider["providerSettingsManager"].getProfile = vi.fn().mockResolvedValue({
623+
name: "ask-profile",
624+
id: "ask-id",
625+
apiProvider: providerIdentifiers.openrouter,
626+
openRouterModelId: "openai/gpt-4.1-mini",
627+
})
628+
provider["providerSettingsManager"].activateProfile = vi.fn().mockResolvedValue({
629+
name: "ask-profile",
630+
id: "ask-id",
631+
apiProvider: providerIdentifiers.openrouter,
632+
openRouterModelId: "openai/gpt-4.1-mini",
633+
})
634+
const postStateSpy = vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
635+
const setValueSpy = vi.spyOn(provider.contextProxy, "setValue")
636+
const setProviderSettingsSpy = vi.spyOn(provider.contextProxy, "setProviderSettings")
637+
postStateSpy.mockClear()
638+
639+
await provider["handleModeSwitchUnlocked"]("ask" as Mode, undefined, { preparePendingTask: true })
640+
641+
expect(setValueSpy).toHaveBeenCalledWith("currentApiConfigName", "ask-profile")
642+
expect(setProviderSettingsSpy).toHaveBeenCalledWith(
643+
expect.objectContaining({ openRouterModelId: "openai/gpt-4.1-mini" }),
644+
)
645+
expect(unrelatedTask.updateApiConfiguration).not.toHaveBeenCalled()
646+
expect(unrelatedTask.setTaskApiConfigName).not.toHaveBeenCalled()
647+
expect(postStateSpy).not.toHaveBeenCalled()
648+
})
649+
615650
test("calls updateApiConfiguration when provider/model unchanged but settings differ (explicit profile switch)", async () => {
616651
const mockTask = new Task({
617652
...defaultTaskOptions,

0 commit comments

Comments
 (0)