Skip to content

Commit d798435

Browse files
author
daewoongoh
committed
feat: coordinate title generation with provider rate limit
Make the background title request wait out the shared provider rate-limit window and record on the shared clock, without emitting task-loop UX.
1 parent b84b4f6 commit d798435

2 files changed

Lines changed: 147 additions & 2 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3223,6 +3223,20 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
32233223
this.titleAbortController = controller
32243224

32253225
try {
3226+
// The title request participates in the same provider rate-limit
3227+
// coordination as task requests: it waits out the configured window
3228+
// (silently — the countdown say UX belongs to the task loop) and
3229+
// records on the shared clock so subsequent task/subtask requests
3230+
// honour it. `startTask` schedules this method with `void`, so the
3231+
// agent never waits on the title result.
3232+
await this.waitForProviderRateLimitWindow(controller.signal)
3233+
3234+
if (controller.signal.aborted || this.abort || this.abandoned) {
3235+
return
3236+
}
3237+
3238+
this.rateLimitClock.recordRequest()
3239+
32263240
const generated = await generateTaskTitle(taskText, this.apiConfiguration, {
32273241
abortSignal: controller.signal,
32283242
timeoutMs: DEFAULT_TITLE_TIMEOUT_MS,
@@ -5792,6 +5806,33 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
57925806
}
57935807
}
57945808

5809+
/**
5810+
* Silent companion to `maybeWaitForProviderRateLimit` for background provider
5811+
* requests (e.g. title generation): waits out the remaining provider
5812+
* rate-limit window without emitting the task-loop countdown UX, since a
5813+
* second concurrent partial `api_req_rate_limit_wait` say would interleave
5814+
* with the task's own countdown. Resolves early (not rejects) when the given
5815+
* signal aborts; callers must still re-check abort state after the wait and
5816+
* call `rateLimitClock.recordRequest()` right before issuing the request,
5817+
* mirroring the task loop.
5818+
*/
5819+
private async waitForProviderRateLimitWindow(signal: AbortSignal): Promise<void> {
5820+
const rateLimitSeconds = this.apiConfiguration?.rateLimitSeconds ?? 0
5821+
const lastRequestTime = this.rateLimitClock.getLastRequestTime()
5822+
if (rateLimitSeconds <= 0 || !lastRequestTime) {
5823+
return
5824+
}
5825+
5826+
const remainingMs = rateLimitSeconds * 1000 - (performance.now() - lastRequestTime)
5827+
if (remainingMs <= 0) {
5828+
return
5829+
}
5830+
5831+
await delay(remainingMs, { signal }).catch(() => {
5832+
// Aborted mid-wait: cancellation, not a failure.
5833+
})
5834+
}
5835+
57955836
public async *attemptApiRequest(
57965837
retryAttempt: number = 0,
57975838
options: { skipProviderRateLimit?: boolean; requestModelInfo?: ModelInfo } = {},

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

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { providerIdentifiers } from "@roo-code/types"
44

55
import { Task } from "../Task"
66
import * as taskTitleGen from "../taskTitleGenerator"
7+
import { createRateLimitClock, type RateLimitClock } from "../RateLimitClock"
78
import { 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+
1929
type 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

Comments
 (0)