Skip to content

Commit a95c46a

Browse files
committed
fix(task): snapshot child configuration during fan-out
1 parent 8cb6733 commit a95c46a

9 files changed

Lines changed: 255 additions & 28 deletions

File tree

‎apps/vscode-e2e/src/suite/subtasks.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,21 @@ suite("Roo Code Subtasks", function () {
385385
messageContentText(message.content).includes(SUBTASK_FANOUT_XPROFILE_CHILD_RESULT),
386386
),
387387
)
388+
389+
const parentHistory = await api.getTaskHistoryItem(parentTaskId)
390+
assert.strictEqual(parentHistory?.mode, "code", "Parent task mode should remain isolated")
391+
assert.strictEqual(
392+
parentHistory?.apiConfigName,
393+
"subtask-fanout-parent-profile",
394+
"Parent task profile should remain isolated",
395+
)
396+
const childHistory = await api.getTaskHistoryItem(childTaskId!)
397+
assert.strictEqual(childHistory?.mode, "ask", "Child task should retain its requested mode")
398+
assert.strictEqual(
399+
childHistory?.apiConfigName,
400+
"subtask-fanout-child-profile",
401+
"Child task should retain its resolved profile",
402+
)
388403
} finally {
389404
api.off(RooCodeEventName.Message, messageHandler)
390405
while (api.getCurrentTaskStack().length > 0) {

‎src/__tests__/helpers/provider-stub.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ type ProviderStubFields = {
1414
removeClineFromStack?: unknown
1515
evictCurrentTask?: unknown
1616
restoreParentOrReleasePermit?: unknown
17+
handleModeSwitchForChild?: unknown
18+
handleModeSwitch?: (newMode: string, targetTask?: unknown) => Promise<unknown>
1719
}
1820

1921
type PrivateProviderMethods = {
2022
runDelegationTransition: (this: unknown, ...args: unknown[]) => unknown
2123
removeClineFromStack: (this: unknown, ...args: unknown[]) => unknown
2224
evictCurrentTask: (this: unknown, ...args: unknown[]) => unknown
2325
restoreParentOrReleasePermit: (this: unknown, ...args: unknown[]) => unknown
26+
handleModeSwitchForChild: (this: unknown, ...args: unknown[]) => unknown
2427
}
2528

2629
export function bindRestoreParentOrReleasePermit(provider: ClineProvider): void {
@@ -29,6 +32,23 @@ export function bindRestoreParentOrReleasePermit(provider: ClineProvider): void
2932
s.restoreParentOrReleasePermit = proto.restoreParentOrReleasePermit.bind(s)
3033
}
3134

35+
export function bindHandleModeSwitchForChild(provider: ClineProvider): void {
36+
const s = provider as unknown as ProviderStubFields
37+
s.handleModeSwitchForChild = async (newMode: string, targetTask?: unknown) => {
38+
if (targetTask === undefined) {
39+
await s.handleModeSwitch?.(newMode)
40+
} else {
41+
await s.handleModeSwitch?.(newMode, targetTask)
42+
}
43+
return { apiConfiguration: {}, mode: newMode, apiConfigName: "default" }
44+
}
45+
}
46+
47+
export function bindDelegationMethods(provider: ClineProvider): void {
48+
bindRestoreParentOrReleasePermit(provider)
49+
bindHandleModeSwitchForChild(provider)
50+
}
51+
3252
/**
3353
* Augments a plain stub object with the instance fields and bound methods that
3454
* ClineProvider methods read from `this` (runDelegationTransition,
@@ -62,5 +82,8 @@ export function makeProviderStub<T extends object>(stub: T): ClineProvider {
6282
if (!s.restoreParentOrReleasePermit) {
6383
bindRestoreParentOrReleasePermit(s as unknown as ClineProvider)
6484
}
85+
if (!s.handleModeSwitchForChild) {
86+
bindHandleModeSwitchForChild(s as unknown as ClineProvider)
87+
}
6588
return s as unknown as ClineProvider
6689
}

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { HistoryItem } from "@roo-code/types"
55
import { RooCodeEventName } from "@roo-code/types"
66
import { ClineProvider } from "../core/webview/ClineProvider"
77
import { TaskScheduler } from "../core/task/TaskScheduler"
8-
import { bindRestoreParentOrReleasePermit } from "./helpers/provider-stub"
8+
import { bindDelegationMethods } from "./helpers/provider-stub"
99

1010
const parentHistoryItem: HistoryItem = {
1111
id: "parent-1",
@@ -44,6 +44,18 @@ const makeParentTask = () =>
4444
}) as any
4545

4646
describe("ClineProvider.delegateParentAndOpenChild()", () => {
47+
it("forwards an explicit target task through the typed child mode-switch binder", async () => {
48+
const handleModeSwitch = vi.fn().mockResolvedValue(undefined)
49+
const provider = { handleModeSwitch } as unknown as ClineProvider
50+
bindDelegationMethods(provider)
51+
const targetTask = makeParentTask()
52+
53+
const snapshot = await provider.handleModeSwitchForChild("architect", targetTask)
54+
55+
expect(handleModeSwitch).toHaveBeenCalledWith("architect", targetTask)
56+
expect(snapshot).toMatchObject({ mode: "architect" })
57+
})
58+
4759
it("persists parent delegation metadata via atomicReadAndUpdate and emits TaskDelegated", async () => {
4860
const providerEmit = vi.fn()
4961
const parentTask = makeParentTask()
@@ -66,6 +78,7 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
6678
recentTasksCache: undefined,
6779
taskHistoryStore,
6880
} as unknown as ClineProvider
81+
bindDelegationMethods(provider)
6982

7083
const child = await (ClineProvider.prototype as any).delegateParentAndOpenChild.call(provider, {
7184
parentTaskId: "parent-1",
@@ -81,11 +94,22 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
8194
expect(removeClineFromStack).toHaveBeenCalledTimes(1)
8295

8396
// Child task created with startTask: false and initialStatus: "active"
84-
expect(createTask).toHaveBeenCalledWith("Do something", undefined, parentTask, {
85-
initialTodos: [],
86-
initialStatus: "active",
87-
startTask: false,
88-
})
97+
expect(createTask).toHaveBeenCalledWith(
98+
"Do something",
99+
undefined,
100+
parentTask,
101+
{
102+
initialTodos: [],
103+
initialStatus: "active",
104+
startTask: false,
105+
},
106+
{},
107+
{
108+
apiConfiguration: {},
109+
mode: "code",
110+
apiConfigName: "default",
111+
},
112+
)
89113

90114
// Delegation metadata written via atomicReadAndUpdate with correct taskId
91115
expect(taskHistoryStore.atomicReadAndUpdate).toHaveBeenCalledTimes(1)
@@ -133,6 +157,7 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
133157
recentTasksCache: undefined,
134158
taskHistoryStore,
135159
} as unknown as ClineProvider
160+
bindDelegationMethods(provider)
136161

137162
await (ClineProvider.prototype as any).delegateParentAndOpenChild.call(provider, {
138163
parentTaskId: "parent-1",
@@ -167,6 +192,7 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
167192
recentTasksCache: undefined,
168193
taskHistoryStore,
169194
} as unknown as ClineProvider
195+
bindDelegationMethods(provider)
170196

171197
await (ClineProvider.prototype as any).delegateParentAndOpenChild.call(provider, {
172198
parentTaskId: "parent-1",
@@ -208,6 +234,7 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
208234
recentTasksCache: undefined,
209235
taskHistoryStore,
210236
} as unknown as ClineProvider
237+
bindDelegationMethods(provider)
211238

212239
await (ClineProvider.prototype as any).delegateParentAndOpenChild.call(provider, {
213240
parentTaskId: "parent-1",
@@ -255,6 +282,7 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
255282
recentTasksCache: undefined,
256283
taskHistoryStore,
257284
} as unknown as ClineProvider
285+
bindDelegationMethods(provider)
258286

259287
await (ClineProvider.prototype as any).delegateParentAndOpenChild.call(provider, {
260288
parentTaskId: "parent-1",
@@ -319,7 +347,7 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
319347
recentTasksCache: undefined,
320348
taskHistoryStore,
321349
} as unknown as ClineProvider
322-
bindRestoreParentOrReleasePermit(provider)
350+
bindDelegationMethods(provider)
323351

324352
await expect(
325353
(ClineProvider.prototype as any).delegateParentAndOpenChild.call(provider, {
@@ -375,7 +403,7 @@ describe("ClineProvider.delegateParentAndOpenChild()", () => {
375403
// still needs evicting, independent of current focus.
376404
taskRegistry: { getById: vi.fn((id: string) => (id === "child-1" ? child : undefined)) },
377405
} as unknown as ClineProvider
378-
bindRestoreParentOrReleasePermit(provider)
406+
bindDelegationMethods(provider)
379407

380408
await expect(
381409
(ClineProvider.prototype as any).delegateParentAndOpenChild.call(provider, {

‎src/core/task/Task.ts‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ const MAX_CONTEXT_WINDOW_RETRIES = 3 // Maximum retries for context window error
145145
export interface TaskOptions extends CreateTaskOptions {
146146
provider: ClineProvider
147147
apiConfiguration: ProviderSettings
148+
/** Immutable mode/profile values captured by the provider before task construction. */
149+
startupSnapshot?: TaskStartupSnapshot
148150
enableCheckpoints?: boolean
149151
checkpointTimeout?: number
150152
consecutiveMistakeLimit?: number
@@ -165,6 +167,12 @@ export interface TaskOptions extends CreateTaskOptions {
165167
diffFuzzyThreshold?: number
166168
}
167169

170+
export interface TaskStartupSnapshot {
171+
apiConfiguration: ProviderSettings
172+
mode: string
173+
apiConfigName?: string
174+
}
175+
168176
export class Task extends EventEmitter<TaskEvents> implements TaskLike {
169177
readonly taskId: string
170178
readonly rootTaskId?: string
@@ -479,6 +487,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
479487
initialStatus,
480488
rateLimitClock,
481489
diffFuzzyThreshold,
490+
startupSnapshot,
482491
}: TaskOptions) {
483492
super()
484493

@@ -527,12 +536,15 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
527536
console.error("Failed to initialize RooIgnoreController:", error)
528537
})
529538

530-
this.apiConfiguration = apiConfiguration
539+
this.apiConfiguration = startupSnapshot?.apiConfiguration ?? apiConfiguration
531540
this.api = buildApiHandler(this.apiConfiguration)
532541
this.rateLimitClock = rateLimitClock ?? createRateLimitClock()
533542
this.autoApprovalHandler = new AutoApprovalHandler()
534543

535-
this.consecutiveMistakeLimit = consecutiveMistakeLimit ?? DEFAULT_CONSECUTIVE_MISTAKE_LIMIT
544+
this.consecutiveMistakeLimit =
545+
startupSnapshot?.apiConfiguration.consecutiveMistakeLimit ??
546+
consecutiveMistakeLimit ??
547+
DEFAULT_CONSECUTIVE_MISTAKE_LIMIT
536548
this.providerRef = new WeakRef(provider)
537549
this.globalStoragePath = provider.context.globalStorageUri.fsPath
538550
this.diffViewProvider = new DiffViewProvider(this.cwd, this)
@@ -552,6 +564,12 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
552564
this.taskModeReady = Promise.resolve()
553565
this.taskApiConfigReady = Promise.resolve()
554566
TelemetryService.instance.captureTaskRestarted(this.taskId)
567+
} else if (startupSnapshot) {
568+
this._taskMode = startupSnapshot.mode
569+
this._taskApiConfigName = startupSnapshot.apiConfigName
570+
this.taskModeReady = Promise.resolve()
571+
this.taskApiConfigReady = Promise.resolve()
572+
TelemetryService.instance.captureTaskCreated(this.taskId)
555573
} else {
556574
// For new tasks, don't set the mode/apiConfigName yet - wait for async initialization.
557575
this._taskMode = undefined

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,47 @@ describe("Cline", () => {
479479
expect(cline.diffStrategy).toBeDefined()
480480
})
481481

482+
it("uses startupSnapshot configuration, mode, profile, and mistake-limit precedence", async () => {
483+
const startupApiConfiguration: ProviderSettings = {
484+
...mockApiConfig,
485+
apiModelId: "snapshot-model",
486+
consecutiveMistakeLimit: 11,
487+
}
488+
const cline = new Task({
489+
provider: mockProvider,
490+
apiConfiguration: mockApiConfig,
491+
consecutiveMistakeLimit: 5,
492+
task: "test task",
493+
startTask: false,
494+
startupSnapshot: {
495+
apiConfiguration: startupApiConfiguration,
496+
mode: "architect",
497+
apiConfigName: "child-profile",
498+
},
499+
})
500+
501+
expect(cline.apiConfiguration).toEqual(startupApiConfiguration)
502+
expect(cline.consecutiveMistakeLimit).toBe(11)
503+
expect(await cline.getTaskMode()).toBe("architect")
504+
expect(await cline.getTaskApiConfigName()).toBe("child-profile")
505+
})
506+
507+
it("falls back to the constructor mistake limit when the snapshot has none", () => {
508+
const cline = new Task({
509+
provider: mockProvider,
510+
apiConfiguration: mockApiConfig,
511+
consecutiveMistakeLimit: 7,
512+
task: "test task",
513+
startTask: false,
514+
startupSnapshot: {
515+
apiConfiguration: { ...mockApiConfig },
516+
mode: "code",
517+
},
518+
})
519+
520+
expect(cline.consecutiveMistakeLimit).toBe(7)
521+
})
522+
482523
it("should use default consecutiveMistakeLimit when not provided", () => {
483524
const cline = new Task({
484525
provider: mockProvider,

‎src/core/task/__tests__/delegation-concurrent.spec.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,16 @@ function baseStubFields(parent: Task) {
5252
return {
5353
contextProxy: { globalStorageUri: { fsPath: "/tmp" } },
5454
getCurrentTask: vi.fn(() => parent),
55-
handleModeSwitch: vi.fn().mockResolvedValue(undefined),
55+
handleModeSwitch: vi.fn().mockResolvedValue({
56+
apiConfiguration: {},
57+
mode: "code",
58+
apiConfigName: "default",
59+
}),
60+
handleModeSwitchForChild: vi.fn().mockResolvedValue({
61+
apiConfiguration: {},
62+
mode: "code",
63+
apiConfigName: "default",
64+
}),
5665
taskHistoryStore: {
5766
get: vi.fn(() => undefined),
5867
atomicReadAndUpdate,
@@ -195,6 +204,7 @@ describe("delegateParentAndOpenChild — fan-out (Story 3.2b)", () => {
195204
const provider = makeProviderStub({
196205
...baseStubFields(parent),
197206
handleModeSwitch: vi.fn().mockRejectedValue(modeSwitchError),
207+
handleModeSwitchForChild: vi.fn().mockRejectedValue(modeSwitchError),
198208
tasks: [parent],
199209
taskScheduler: scheduler,
200210
removeClineFromStack: vi.fn().mockResolvedValue(undefined),
@@ -235,6 +245,7 @@ describe("delegateParentAndOpenChild — fan-out (Story 3.2b)", () => {
235245
const provider = makeProviderStub({
236246
...baseStubFields(parent),
237247
handleModeSwitch: vi.fn().mockRejectedValue(modeSwitchError),
248+
handleModeSwitchForChild: vi.fn().mockRejectedValue(modeSwitchError),
238249
tasks: [parent],
239250
taskScheduler: new TaskScheduler(1),
240251
removeClineFromStack: vi.fn().mockResolvedValue(undefined),

0 commit comments

Comments
 (0)