@@ -2036,7 +2036,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
20362036
20372037 const drainStreamInBackgroundToFindAllUsage = async ( apiReqIndex : number ) => {
20382038 const timeoutMs = DEFAULT_USAGE_COLLECTION_TIMEOUT_MS
2039- const startTime = Date . now ( )
2039+ const startTime = performance . now ( )
20402040 const modelId = getModelId ( this . apiConfiguration )
20412041
20422042 // Local variables to accumulate usage data without affecting the main flow
@@ -2107,7 +2107,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
21072107 // Use the same iterator that the main loop was using
21082108 while ( ! item . done ) {
21092109 // Check for timeout
2110- if ( Date . now ( ) - startTime > timeoutMs ) {
2110+ if ( performance . now ( ) - startTime > timeoutMs ) {
21112111 console . warn (
21122112 `[Background Usage Collection] Timed out after ${ timeoutMs } ms for model: ${ modelId } , processed ${ chunkCount } chunks` ,
21132113 )
@@ -2559,10 +2559,10 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
25592559 // Use the shared timestamp so that subtasks respect the same rate-limit
25602560 // window as their parent tasks.
25612561 if ( Task . lastGlobalApiRequestTime ) {
2562- const now = Date . now ( )
2562+ const now = performance . now ( )
25632563 const timeSinceLastRequest = now - Task . lastGlobalApiRequestTime
25642564 const rateLimit = apiConfiguration ?. rateLimitSeconds || 0
2565- rateLimitDelay = Math . ceil ( Math . max ( 0 , rateLimit * 1000 - timeSinceLastRequest ) / 1000 )
2565+ rateLimitDelay = Math . ceil ( Math . min ( rateLimit , Math . max ( 0 , rateLimit * 1000 - timeSinceLastRequest ) / 1000 ) )
25662566 }
25672567
25682568 // Only show rate limiting message if we're not retrying. If retrying, we'll include the delay there.
@@ -2577,7 +2577,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
25772577
25782578 // Update last request time before making the request so that subsequent
25792579 // requests — even from new subtasks — will honour the provider's rate-limit.
2580- Task . lastGlobalApiRequestTime = Date . now ( )
2580+ Task . lastGlobalApiRequestTime = performance . now ( )
25812581
25822582 const systemPrompt = await this . getSystemPrompt ( )
25832583 this . lastUsedInstructions = systemPrompt
0 commit comments