Skip to content

Commit 4b2b05f

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

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
@@ -140,9 +140,17 @@ export class Task extends EventEmitter<ClineEvents> {
140140
// API
141141
readonly apiConfiguration: ProviderSettings
142142
api: ApiHandler
143-
private lastApiRequestTime?: number
143+
private static lastGlobalApiRequestTime?: number
144144
private consecutiveAutoApprovedRequestsCount: number = 0
145145

146+
/**
147+
* Reset the global API request timestamp. This should only be used for testing.
148+
* @internal
149+
*/
150+
static resetGlobalApiRequestTime(): void {
151+
Task.lastGlobalApiRequestTime = undefined
152+
}
153+
146154
toolRepetitionDetector: ToolRepetitionDetector
147155
rooIgnoreController?: RooIgnoreController
148156
rooProtectedController?: RooProtectedController
@@ -1657,10 +1665,11 @@ export class Task extends EventEmitter<ClineEvents> {
16571665

16581666
let rateLimitDelay = 0
16591667

1660-
// Only apply rate limiting if this isn't the first request
1661-
if (this.lastApiRequestTime) {
1668+
// Use the shared timestamp so that subtasks respect the same rate-limit
1669+
// window as their parent tasks.
1670+
if (Task.lastGlobalApiRequestTime) {
16621671
const now = Date.now()
1663-
const timeSinceLastRequest = now - this.lastApiRequestTime
1672+
const timeSinceLastRequest = now - Task.lastGlobalApiRequestTime
16641673
const rateLimit = apiConfiguration?.rateLimitSeconds || 0
16651674
rateLimitDelay = Math.ceil(Math.max(0, rateLimit * 1000 - timeSinceLastRequest) / 1000)
16661675
}
@@ -1675,8 +1684,9 @@ export class Task extends EventEmitter<ClineEvents> {
16751684
}
16761685
}
16771686

1678-
// Update last request time before making the request
1679-
this.lastApiRequestTime = Date.now()
1687+
// Update last request time before making the request so that subsequent
1688+
// requests — even from new subtasks — will honour the provider's rate-limit.
1689+
Task.lastGlobalApiRequestTime = Date.now()
16801690

16811691
const systemPrompt = await this.getSystemPrompt()
16821692
const { contextTokens } = this.getTokenUsage()

0 commit comments

Comments
 (0)