Skip to content

Commit b6341af

Browse files
committed
fix: keep headless task creation working when the webview never launches,
catch IPC start failures, harden two readiness/ordering tests Review round on this unit: (1) the view-state readiness bound rejected every startNewTask whose webview never launched, breaking headless API callers that previously created tasks against the shared-state fallback - the bound now only rejects when the webview has launched (a launched view whose readiness failed is a real failure); the IPC StartNewTask listener also catches rejections, since IpcServer does not await async listeners and an uncaught rejection surfaces as an unhandled rejection in the extension host. (2) The readiness-cancel test proved cancellation with a global clearTimeout spy, which also fires for the flush sleep and library internals - it now runs on fake timers and asserts the timer count drops to zero, pinning the readiness bound's own timer. (3) The mode-ordering test only checked the end state, so it passed even if submitUserMessage dropped its wait for the pending initialization - it now asserts the switch has not been called before the pending state is released. Proofs: Task.spec 190/190, api-task-control 26/26 (new headless-fallback and launched-stuck tests green); negative controls: dropping the waitForModeInitialization await reddens exactly the ordering test's new in-flight assertion, dropping the readiness timer's clearTimeout reddens exactly the timer-count assertion, making the timeout unconditional reddens exactly the headless-fallback test, restores byte-exact (hash match); eslint --max-warnings=0 clean; prettier clean.
1 parent 320fd43 commit b6341af

3 files changed

Lines changed: 96 additions & 19 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3839,6 +3839,14 @@ describe("Cline", () => {
38393839

38403840
const submitted = task.submitUserMessage("select while initializing", undefined, "architect")
38413841

3842+
// Flush microtasks: the switch must still wait for the pending
3843+
// initialization. Without the await, the mocked switch would already have
3844+
// run here and the end-state assertion could not tell the orders apart.
3845+
for (let i = 0; i < 5; i++) {
3846+
await Promise.resolve()
3847+
}
3848+
expect(mockProvider.handleModeSwitch).not.toHaveBeenCalled()
3849+
38423850
// Resolve the pending provider state only after the submission has started,
38433851
// carrying the provider's pre-switch mode.
38443852
releaseState(Object.assign({} as ProviderState, { mode: "ask" }))

‎src/extension/__tests__/api-task-control.spec.ts‎

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,68 @@ describe("API task controls", () => {
198198
})
199199

200200
it("cancels the readiness timeout when the view becomes ready within the bound", async () => {
201-
const clearSpy = vi.spyOn(globalThis, "clearTimeout")
201+
// Fake timers pin the exact timer surface: a global clearTimeout spy would
202+
// also fire for the flush sleep and library internals, so it could not
203+
// prove the readiness bound's own timer was cancelled.
204+
vi.useFakeTimers()
202205
let resolveReady!: () => void
203206
sidebarProvider.viewStateReadiness = new Promise<void>((resolve) => {
204207
resolveReady = resolve
205208
})
206209

207210
const started = api.startNewTask({ configuration, text: "ready task" })
208-
await new Promise((resolve) => setTimeout(resolve, 50))
211+
await vi.advanceTimersByTimeAsync(50)
209212
resolveReady()
210213

211-
await expect(started).resolves.toBe("sidebar-task")
212-
// The bound's timer must be cancelled once readiness wins: a live timer would
213-
// later reject an unawaited promise and surface as an unhandled rejection.
214-
expect(clearSpy).toHaveBeenCalled()
215-
clearSpy.mockRestore()
214+
try {
215+
await expect(started).resolves.toBe("sidebar-task")
216+
// The readiness bound's timer must be gone once readiness wins: a live
217+
// timer would later reject an unawaited promise and surface as an
218+
// unhandled rejection.
219+
expect(vi.getTimerCount()).toBe(0)
220+
// No late rejection can fire after the bound either.
221+
await vi.advanceTimersByTimeAsync(3000)
222+
} finally {
223+
vi.useRealTimers()
224+
}
225+
})
226+
227+
it("continues with the shared-state fallback when the webview never launches", async () => {
228+
// Headless API callers create tasks without ever launching a webview: the
229+
// readiness bound expiring must not reject them - the pre-existing
230+
// shared-state fallback still applies.
231+
vi.useFakeTimers()
232+
sidebarProvider.viewLaunched = false
233+
sidebarProvider.viewStateReadiness = new Promise<void>(() => {})
234+
235+
const started = api.startNewTask({ configuration, text: "headless task" })
236+
await vi.advanceTimersByTimeAsync(3000)
237+
238+
try {
239+
await expect(started).resolves.toBe("sidebar-task")
240+
} finally {
241+
vi.useRealTimers()
242+
}
243+
})
244+
245+
it("still rejects when the webview launched but the readiness bound expires", async () => {
246+
// A launched view whose readiness never settles is a real failure: the
247+
// task must not consume shared defaults behind a live view.
248+
vi.useFakeTimers()
249+
sidebarProvider.viewStateReadiness = new Promise<void>(() => {})
250+
251+
const started = api.startNewTask({ configuration, text: "stuck view task" })
252+
// Attach the rejection handler before advancing: with fake timers the
253+
// bound fires during the advance, and a handler attached afterwards would
254+
// see the rejection briefly unhandled.
255+
const settled = expect(started).rejects.toThrow("Timed out waiting for the view state to become ready")
256+
await vi.advanceTimersByTimeAsync(3000)
257+
258+
try {
259+
await settled
260+
} finally {
261+
vi.useRealTimers()
262+
}
216263
})
217264

218265
it("fails task creation when the provider is disposed while the readiness wait is pending", async () => {

‎src/extension/api.ts‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,15 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI, RooC
107107
this.log(
108108
`[API] StartNewTask -> ${command.data.text}, ${JSON.stringify(command.data.configuration)}`,
109109
)
110-
await this.startNewTask(command.data)
110+
try {
111+
await this.startNewTask(command.data)
112+
} catch (error) {
113+
// IpcServer does not await async listeners: an uncaught rejection here
114+
// would surface as an unhandled rejection in the extension host.
115+
this.log(
116+
`[API] StartNewTask failed: ${error instanceof Error ? error.message : String(error)}`,
117+
)
118+
}
111119
break
112120
case TaskCommandName.CancelTask:
113121
this.log(`[API] CancelTask`)
@@ -231,17 +239,31 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI, RooC
231239
// hang task creation; if the bound expires, fail task creation rather than start
232240
// the task against stale shared defaults.
233241
let readinessTimer: ReturnType<typeof setTimeout> | undefined
234-
await Promise.race([
235-
provider.viewStateReadiness,
236-
new Promise<never>((_, reject) => {
237-
readinessTimer = setTimeout(
238-
() => reject(new Error("Timed out waiting for the view state to become ready")),
239-
3000,
240-
)
241-
}),
242-
]).finally(() => {
243-
clearTimeout(readinessTimer)
244-
})
242+
try {
243+
await Promise.race([
244+
provider.viewStateReadiness,
245+
new Promise<never>((_, reject) => {
246+
readinessTimer = setTimeout(
247+
() => reject(new Error("Timed out waiting for the view state to become ready")),
248+
3000,
249+
)
250+
}),
251+
]).finally(() => {
252+
clearTimeout(readinessTimer)
253+
})
254+
} catch (error) {
255+
if (
256+
provider.viewLaunched ||
257+
!(error instanceof Error) ||
258+
error.message !== "Timed out waiting for the view state to become ready"
259+
) {
260+
throw error
261+
}
262+
// Headless API callers create tasks without ever launching a webview: the
263+
// readiness bound expiring keeps the pre-existing shared-state fallback.
264+
// A launched view whose readiness failed is a real failure and rethrows.
265+
this.log("[API#startNewTask] webview not launched; continuing in headless mode")
266+
}
245267
// Disposal resolves viewStateReadiness to release waiters: a provider torn down while
246268
// the wait was pending must fail task creation rather than create a task against the
247269
// disposed view (its posts and task registration would be orphaned).

0 commit comments

Comments
 (0)