@@ -4,6 +4,7 @@ import { providerIdentifiers } from "@roo-code/types"
44
55import { Task } from "../Task"
66import * as taskTitleGen from "../taskTitleGenerator"
7+ import { createRateLimitClock , type RateLimitClock } from "../RateLimitClock"
78import { setAbortLatch } from "../../../test-utils/run-state"
89
910// Keep the real constants and the fallback helper; only the LLM call is mocked
@@ -16,6 +17,15 @@ vi.mock("../taskTitleGenerator", async (importOriginal) => {
1617 }
1718} )
1819
20+ // Mock delay so the title request's rate-limit wait can be observed without
21+ // real timers (same pattern as the rate-limit tests in Task.spec).
22+ vi . mock ( "delay" , ( ) => ( {
23+ __esModule : true ,
24+ default : vi . fn ( ) . mockResolvedValue ( undefined ) ,
25+ } ) )
26+
27+ import delay from "delay "
28+
1929type TaskTitleAccess = {
2030 generateTitleInBackground ( taskText : string ) : Promise < void >
2131 startTask ( task ?: string , images ?: string [ ] ) : Promise < void >
@@ -55,18 +65,25 @@ describe("Task title generation", () => {
5565 }
5666 } )
5767
58- function createTestTask ( options : { parentTask ?: Task } = { } ) {
68+ function createTestTask (
69+ options : {
70+ parentTask ?: Task
71+ rateLimitClock ?: RateLimitClock
72+ apiConfiguration ?: ProviderSettings
73+ } = { } ,
74+ ) {
5975 const task = Object . create ( Task . prototype ) as Task
6076 setAbortLatch ( task , false )
6177 const taskId = "task-title-1"
6278
6379 Object . assign ( task , {
6480 taskId,
6581 instanceId : "inst-1" ,
66- apiConfiguration : mockApiConfig ,
82+ apiConfiguration : options . apiConfiguration ?? mockApiConfig ,
6783 providerRef : { deref : ( ) => mockProvider } ,
6884 abandoned : false ,
6985 parentTask : options . parentTask ,
86+ rateLimitClock : options . rateLimitClock ?? createRateLimitClock ( ) ,
7087 metadata : { task : "Initial prompt message" } ,
7188 say : vi . fn ( ) . mockResolvedValue ( undefined ) ,
7289 getEnabledMcpToolsCount : vi . fn ( ) . mockResolvedValue ( { enabledToolCount : 0 , enabledServerCount : 0 } ) ,
@@ -191,6 +208,93 @@ describe("Task title generation", () => {
191208 } )
192209 } )
193210
211+ describe ( "generateTitleInBackground rate-limit coordination" , ( ) => {
212+ it ( "waits out the remaining provider rate-limit window before the title request" , async ( ) => {
213+ const clock = createRateLimitClock ( )
214+ const task = createTestTask ( {
215+ rateLimitClock : clock ,
216+ apiConfiguration : { ...mockApiConfig , rateLimitSeconds : 10 } ,
217+ } )
218+ vi . mocked ( taskTitleGen . generateTaskTitle ) . mockResolvedValueOnce ( "Summarized Title" )
219+ const mockDelay = vi . mocked ( delay )
220+ // Record the wait options while the controller still exists; it is
221+ // cleaned up in the method's `finally` block.
222+ let observedSignal : unknown
223+ mockDelay . mockImplementationOnce ( ( _ms : number , opts ?: { signal ?: AbortSignal } ) => {
224+ observedSignal = opts ?. signal
225+ return Promise . resolve ( undefined )
226+ } )
227+
228+ // A prior request (e.g. the previous task's) leaves a timestamp on the
229+ // shared clock, so the title request must not bypass the window.
230+ clock . recordRequest ( )
231+
232+ await getTaskTitleAccess ( task ) . generateTitleInBackground ( "Initial prompt message" )
233+
234+ expect ( mockDelay ) . toHaveBeenCalledTimes ( 1 )
235+ const waitedMs = mockDelay . mock . calls [ 0 ] ?. [ 0 ]
236+ expect ( waitedMs ) . toBeGreaterThan ( 0 )
237+ expect ( waitedMs ) . toBeLessThanOrEqual ( 10_000 )
238+ // The wait is abort-aware so cancellation can interrupt it.
239+ expect ( observedSignal ) . toBeInstanceOf ( AbortSignal )
240+ // The wait happens before the provider request is issued.
241+ expect ( mockDelay . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
242+ vi . mocked ( taskTitleGen . generateTaskTitle ) . mock . invocationCallOrder [ 0 ] ,
243+ )
244+ } )
245+
246+ it ( "records the title request on the shared rate-limit clock" , async ( ) => {
247+ const clock = createRateLimitClock ( )
248+ const task = createTestTask ( { rateLimitClock : clock } )
249+ vi . mocked ( taskTitleGen . generateTaskTitle ) . mockResolvedValueOnce ( "Summarized Title" )
250+
251+ expect ( clock . getLastRequestTime ( ) ) . toBeUndefined ( )
252+
253+ await getTaskTitleAccess ( task ) . generateTitleInBackground ( "Initial prompt message" )
254+
255+ // Subsequent task/subtask requests see this timestamp and honour it.
256+ expect ( clock . getLastRequestTime ( ) ) . toBeDefined ( )
257+ } )
258+
259+ it ( "does not wait when no rate limit is configured or the clock is empty" , async ( ) => {
260+ const task = createTestTask ( { apiConfiguration : { ...mockApiConfig , rateLimitSeconds : 10 } } )
261+ vi . mocked ( taskTitleGen . generateTaskTitle ) . mockResolvedValueOnce ( "Summarized Title" )
262+ const mockDelay = vi . mocked ( delay )
263+
264+ // rateLimitSeconds is set, but the shared clock has no recorded
265+ // request yet — nothing to wait for.
266+ await getTaskTitleAccess ( task ) . generateTitleInBackground ( "Initial prompt message" )
267+
268+ expect ( mockDelay ) . not . toHaveBeenCalled ( )
269+ expect ( taskTitleGen . generateTaskTitle ) . toHaveBeenCalledTimes ( 1 )
270+ } )
271+
272+ it ( "skips the title request when the task is aborted during the rate-limit wait" , async ( ) => {
273+ const clock = createRateLimitClock ( )
274+ const task = createTestTask ( {
275+ rateLimitClock : clock ,
276+ apiConfiguration : { ...mockApiConfig , rateLimitSeconds : 10 } ,
277+ } )
278+ const access = getTaskTitleAccess ( task )
279+ clock . recordRequest ( )
280+ const timestampBeforeWait = clock . getLastRequestTime ( )
281+ // Simulate the wait being interrupted by cancellation: the delay
282+ // promise rejects like `delay` does on an aborted signal.
283+ vi . mocked ( delay ) . mockImplementationOnce ( ( ) => {
284+ access . titleAbortController ?. abort ( )
285+ return Promise . reject ( new Error ( "The operation was aborted" ) )
286+ } )
287+
288+ await access . generateTitleInBackground ( "Initial prompt message" )
289+
290+ // No provider request, no phantom clock timestamp that would delay
291+ // the next request.
292+ expect ( taskTitleGen . generateTaskTitle ) . not . toHaveBeenCalled ( )
293+ expect ( clock . getLastRequestTime ( ) ) . toBe ( timestampBeforeWait )
294+ expect ( mockSetGeneratedTaskTitle ) . not . toHaveBeenCalled ( )
295+ } )
296+ } )
297+
194298 describe ( "cancellation and abort handling" , ( ) => {
195299 it ( "aborts titleAbortController in cancelCurrentRequest" , ( ) => {
196300 const task = createTestTask ( )
0 commit comments