Skip to content

Commit a6ba955

Browse files
olweraltuvedaniel-lxsmrubens
authored andcommitted
Rate limit when starting a subtask (RooCodeInc#4453)
Co-authored-by: Daniel Riccio <[email protected]> Co-authored-by: Matt Rubens <[email protected]>
1 parent 2749055 commit a6ba955

File tree

3 files changed

+410
-35
lines changed

3 files changed

+410
-35
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
"dist": true // set this to false to include "dist" folder in search results
1010
},
1111
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
12-
"typescript.tsc.autoDetect": "off"
12+
"typescript.tsc.autoDetect": "off",
13+
"vitest.disableWorkspaceWarning": true
1314
}

src/core/task/Task.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,17 @@ export class Task extends EventEmitter<ClineEvents> {
150150
// API
151151
readonly apiConfiguration: ProviderSettings
152152
api: ApiHandler
153-
private lastApiRequestTime?: number
153+
private static lastGlobalApiRequestTime?: number
154154
private consecutiveAutoApprovedRequestsCount: number = 0
155155

156+
/**
157+
* Reset the global API request timestamp. This should only be used for testing.
158+
* @internal
159+
*/
160+
static resetGlobalApiRequestTime(): void {
161+
Task.lastGlobalApiRequestTime = undefined
162+
}
163+
156164
toolRepetitionDetector: ToolRepetitionDetector
157165
rooIgnoreController?: RooIgnoreController
158166
rooProtectedController?: RooProtectedController
@@ -1677,10 +1685,11 @@ export class Task extends EventEmitter<ClineEvents> {
16771685

16781686
let rateLimitDelay = 0
16791687

1680-
// Only apply rate limiting if this isn't the first request
1681-
if (this.lastApiRequestTime) {
1688+
// Use the shared timestamp so that subtasks respect the same rate-limit
1689+
// window as their parent tasks.
1690+
if (Task.lastGlobalApiRequestTime) {
16821691
const now = Date.now()
1683-
const timeSinceLastRequest = now - this.lastApiRequestTime
1692+
const timeSinceLastRequest = now - Task.lastGlobalApiRequestTime
16841693
const rateLimit = apiConfiguration?.rateLimitSeconds || 0
16851694
rateLimitDelay = Math.ceil(Math.max(0, rateLimit * 1000 - timeSinceLastRequest) / 1000)
16861695
}
@@ -1695,8 +1704,9 @@ export class Task extends EventEmitter<ClineEvents> {
16951704
}
16961705
}
16971706

1698-
// Update last request time before making the request
1699-
this.lastApiRequestTime = Date.now()
1707+
// Update last request time before making the request so that subsequent
1708+
// requests — even from new subtasks — will honour the provider's rate-limit.
1709+
Task.lastGlobalApiRequestTime = Date.now()
17001710

17011711
const systemPrompt = await this.getSystemPrompt()
17021712
const { contextTokens } = this.getTokenUsage()

0 commit comments

Comments
 (0)