Skip to content

Commit e6fb7e4

Browse files
author
daewoongoh
committed
fix: revalidate rate-limit before agent dispatch after preparation and during retries
When agent preparation takes longer than the configured rate-limit window or when title generation dispatches during retry backoff, the agent can dispatch too soon without waiting. Add revalidation logic that defers dispatch when another request has recorded on the shared clock since the agent's initial reservation. Track this task's own rate-limit reservations in ownRateLimitReservation. Before dispatch (both in the rate-limited path and skipProviderRateLimit path), call reserveAgentDispatchSlot() which silently waits out the window from any other request that recorded meanwhile, then records synchronously. Added 4 concurrent-dispatch tests: - Title while agent prepares longer than window - No wait when only agent's own reservation exists - Retry during rate-limit wait (rate-limited path) - Retry during rate-limit wait (skipProviderRateLimit path) These tests detect when the fix is reverted, failing the dispatch-timing assertions for all scenarios except when only the agent's reservation exists.
1 parent 76c4c47 commit e6fb7e4

2 files changed

Lines changed: 149 additions & 7 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
437437
/** Handlers replaced during in-flight work; disposed when that work settles, or with the task. */
438438
private retiredApis = new Set<ApiHandler>()
439439
private rateLimitClock: RateLimitClock
440+
/** Shared-clock timestamp this task's agent loop last recorded; background requests never set it. */
441+
private ownRateLimitReservation?: number
440442
private autoApprovalHandler: AutoApprovalHandler
441443

442444
toolRepetitionDetector: ToolRepetitionDetector
@@ -5808,7 +5810,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
58085810
let lastRequestTime = this.rateLimitClock.getLastRequestTime()
58095811
// Only show the countdown UX on the first attempt. Retry flows have their own delay messaging.
58105812
if (rateLimitSeconds <= 0 || !lastRequestTime || retryAttempt !== 0) {
5811-
this.rateLimitClock.recordRequest()
5813+
await this.reserveAgentDispatchSlot()
58125814
return
58135815
}
58145816

@@ -5840,14 +5842,46 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
58405842
lastRequestTime = latestRequestTime
58415843
}
58425844

5843-
this.rateLimitClock.recordRequest()
5845+
this.recordAgentRateLimitReservation()
58445846

58455847
if (waited) {
58465848
// Finalize the partial message so the UI doesn't keep rendering an in-progress spinner.
58475849
await this.say("api_req_rate_limit_wait", undefined, undefined, false)
58485850
}
58495851
}
58505852

5853+
private recordAgentRateLimitReservation(): void {
5854+
this.rateLimitClock.recordRequest()
5855+
this.ownRateLimitReservation = this.rateLimitClock.getLastRequestTime()
5856+
}
5857+
5858+
/**
5859+
* Reserve the agent's dispatch slot without the countdown UX. When the
5860+
* latest timestamp on the shared clock is this task's own earlier
5861+
* reservation (e.g. one taken before building environment details), the
5862+
* slot is recorded immediately. When another request — such as background
5863+
* title generation, during preparation or retry backoff — recorded since,
5864+
* the window is silently waited out from that dispatch first.
5865+
*/
5866+
private async reserveAgentDispatchSlot(): Promise<void> {
5867+
const rateLimitSeconds = this.apiConfiguration?.rateLimitSeconds ?? 0
5868+
5869+
while (!this.abort && !this.abandoned) {
5870+
const lastRequestTime = this.rateLimitClock.getLastRequestTime()
5871+
const remainingMs =
5872+
lastRequestTime && lastRequestTime !== this.ownRateLimitReservation
5873+
? rateLimitSeconds * 1000 - (performance.now() - lastRequestTime)
5874+
: 0
5875+
if (remainingMs <= 0) {
5876+
break
5877+
}
5878+
5879+
await delay(remainingMs)
5880+
}
5881+
5882+
this.recordAgentRateLimitReservation()
5883+
}
5884+
58515885
/**
58525886
* Silent companion to `reserveProviderRateLimitSlot` for background provider
58535887
* requests (e.g. title generation): waits out the remaining provider
@@ -5917,11 +5951,11 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
59175951
// requests — even from new subtasks — will honour the provider's rate-limit.
59185952
//
59195953
// NOTE: When recursivelyMakeClineRequests handles rate limiting, it reserves the
5920-
// slot earlier to include the environment details build. We still record it
5921-
// here for direct callers (tests) and for the case where we didn't rate-limit
5922-
// in the caller.
5954+
// slot earlier to include the environment details build. Re-reserving here
5955+
// waits out any request (e.g. title generation) that dispatched on the shared
5956+
// clock since then, including during retry backoff.
59235957
if (options.skipProviderRateLimit) {
5924-
this.rateLimitClock.recordRequest()
5958+
await this.reserveAgentDispatchSlot()
59255959
} else {
59265960
await this.reserveProviderRateLimitSlot(retryAttempt)
59275961
}

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

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { TelemetryService } from "@roo-code/telemetry"
2525

2626
import { MODEL_FETCH_TIMEOUT_MS, Task } from "../Task"
2727
import { SYSTEM_PROMPT } from "../../prompts/system"
28-
import { createRateLimitClock } from "../RateLimitClock"
28+
import { createRateLimitClock, type RateLimitClock } from "../RateLimitClock"
2929
import { summarizeConversation } from "../../condense"
3030
import { getEnvironmentDetails } from "../../environment/getEnvironmentDetails"
3131
import { ClineProvider } from "../../webview/ClineProvider"
@@ -56,6 +56,7 @@ type TaskTestAccess = {
5656
saveClineMessages: () => Promise<boolean>
5757
safeEnsureModelFetched: () => Promise<ModelInfo>
5858
backoffAndAnnounce: (retryAttempt: number, error: unknown) => Promise<void>
59+
reserveProviderRateLimitSlot: (retryAttempt: number) => Promise<void>
5960
requestPolicySnapshot?: RequestPolicySnapshot
6061
getFilesReadByRooSafely: (context: string) => Promise<string[] | undefined>
6162
addToApiConversationHistory: (message: unknown, reasoning?: string) => Promise<void>
@@ -4024,6 +4025,113 @@ describe("Cline", () => {
40244025
expect(lastTime).toBeDefined()
40254026
expect(lastTime).toBeGreaterThan(0)
40264027
})
4028+
4029+
describe("dispatch revalidation against other requests on the shared clock", () => {
4030+
// `delay` advances a controlled `performance.now()` like a real wait,
4031+
// so dispatch times can be compared against the 5s window.
4032+
let now: number
4033+
4034+
beforeEach(() => {
4035+
now = 1_000_000
4036+
vi.spyOn(performance, "now").mockImplementation(() => now)
4037+
mockDelay.mockImplementation((ms: number) => {
4038+
now += ms
4039+
return Promise.resolve(undefined)
4040+
})
4041+
})
4042+
4043+
afterEach(() => {
4044+
vi.mocked(performance.now).mockRestore()
4045+
mockDelay.mockReset().mockResolvedValue(undefined)
4046+
})
4047+
4048+
function createDispatchTask(clock: RateLimitClock) {
4049+
const task = new Task({
4050+
provider: mockProvider,
4051+
apiConfiguration: mockApiConfig,
4052+
task: "test task",
4053+
startTask: false,
4054+
rateLimitClock: clock,
4055+
})
4056+
vi.spyOn(getTaskTestAccess(task), "getSystemPrompt").mockResolvedValue("mock system prompt")
4057+
vi.spyOn(task, "say").mockResolvedValue(undefined)
4058+
const dispatchedAt: number[] = []
4059+
const createMessage = vi.spyOn(task.api, "createMessage")
4060+
createMessage.mockImplementation(() => {
4061+
dispatchedAt.push(now)
4062+
return asyncStreamFrom<ApiStreamChunk>([{ type: "text", text: "response" }])
4063+
})
4064+
return { task, dispatchedAt, createMessage }
4065+
}
4066+
4067+
it("waits out a request that dispatched while the agent prepared longer than the window", async () => {
4068+
const clock = createRateLimitClock()
4069+
const { task, dispatchedAt } = createDispatchTask(clock)
4070+
4071+
// The agent reserves before building environment details...
4072+
await getTaskTestAccess(task).reserveProviderRateLimitSlot(0)
4073+
// ...which takes longer than the window, so a title request dispatches meanwhile.
4074+
now += 7_000
4075+
clock.recordRequest()
4076+
const otherDispatchAt = now
4077+
4078+
await task.attemptApiRequest(0, { skipProviderRateLimit: true }).next()
4079+
4080+
expect(dispatchedAt).toHaveLength(1)
4081+
expect((dispatchedAt[0] ?? 0) - otherDispatchAt).toBeGreaterThanOrEqual(5_000)
4082+
})
4083+
4084+
it("dispatches without waiting when only its own earlier reservation is on the clock", async () => {
4085+
const clock = createRateLimitClock()
4086+
const { task, dispatchedAt } = createDispatchTask(clock)
4087+
4088+
await getTaskTestAccess(task).reserveProviderRateLimitSlot(0)
4089+
const reservedAt = now
4090+
now += 1_000
4091+
4092+
await task.attemptApiRequest(0, { skipProviderRateLimit: true }).next()
4093+
4094+
expect(mockDelay).not.toHaveBeenCalled()
4095+
expect(dispatchedAt).toEqual([reservedAt + 1_000])
4096+
})
4097+
4098+
it.each([
4099+
{ path: "the rate-limited path", options: {} },
4100+
{ path: "the inherited skipProviderRateLimit path", options: { skipProviderRateLimit: true } },
4101+
])("waits out a request that dispatched during retry backoff on $path", async ({ options }) => {
4102+
mockProvider.getState.mockResolvedValue({
4103+
apiConfiguration: mockApiConfig,
4104+
mcpEnabled: false,
4105+
autoApprovalEnabled: true,
4106+
requestDelaySeconds: 1,
4107+
})
4108+
const clock = createRateLimitClock()
4109+
const { task, dispatchedAt, createMessage } = createDispatchTask(clock)
4110+
createMessage.mockImplementationOnce(() => {
4111+
dispatchedAt.push(now)
4112+
return (async function* (): AsyncGenerator<ApiStreamChunk> {
4113+
yield* []
4114+
throw Object.assign(new Error("server error"), { status: 500 })
4115+
})()
4116+
})
4117+
// A title request dispatches on the first backoff tick.
4118+
let otherDispatchAt: number | undefined
4119+
mockDelay.mockImplementation((ms: number) => {
4120+
now += ms
4121+
if (otherDispatchAt === undefined) {
4122+
clock.recordRequest()
4123+
otherDispatchAt = now
4124+
}
4125+
return Promise.resolve(undefined)
4126+
})
4127+
4128+
await task.attemptApiRequest(0, options).next()
4129+
4130+
expect(dispatchedAt).toHaveLength(2)
4131+
expect(otherDispatchAt).toBeDefined()
4132+
expect((dispatchedAt[1] ?? 0) - (otherDispatchAt ?? 0)).toBeGreaterThanOrEqual(5_000)
4133+
})
4134+
})
40274135
})
40284136

40294137
describe("Dynamic Strategy Selection", () => {

0 commit comments

Comments
 (0)