@@ -47,19 +47,22 @@ const RATE_LIMITS: Record<string, { limit: number; windowHours: number }> = {
4747/** Fetch the caller's current quota snapshot for `model`, or undefined if the
4848 * model isn't rate-limited. Used by both POST (after admit) and GET polls so
4949 * the CLI's "N of M sessions used" line stays live instead of disappearing
50- * after the first poll. Also returns the oldest admit in-window so callers
51- * that need `retryAfterMs` don't have to re-query. */
50+ * after the first poll. Also returns the oldest admit in-window and the
51+ * window duration so callers that need `retryAfterMs` don't have to re-query
52+ * or duplicate the window math. */
5253async function fetchRateLimitSnapshot (
5354 userId : string ,
5455 model : string ,
5556 deps : SessionDeps ,
5657) : Promise <
57- { info : FreebuffSessionRateLimit ; oldest : Date | null } | undefined
58+ | { info : FreebuffSessionRateLimit ; oldest : Date | null ; windowMs : number }
59+ | undefined
5860> {
5961 const cfg = RATE_LIMITS [ model ]
6062 if ( ! cfg ) return undefined
6163 const now = nowOf ( deps )
62- const since = new Date ( now . getTime ( ) - cfg . windowHours * 60 * 60 * 1000 )
64+ const windowMs = cfg . windowHours * 60 * 60 * 1000
65+ const since = new Date ( now . getTime ( ) - windowMs )
6366 const admits = await deps . listRecentAdmits ( {
6467 userId,
6568 model,
@@ -74,6 +77,7 @@ async function fetchRateLimitSnapshot(
7477 recentCount : admits . length ,
7578 } ,
7679 oldest : admits [ 0 ] ?? null ,
80+ windowMs,
7781 }
7882}
7983
@@ -271,10 +275,9 @@ export async function requestSession(params: {
271275 if ( snapshot && snapshot . info . recentCount >= snapshot . info . limit ) {
272276 // Oldest admit's window-anniversary is when one slot opens back up.
273277 // Clamped at 0 so a clock skew can't surface a negative retry-after.
274- const windowMs = snapshot . info . windowHours * 60 * 60 * 1000
275278 const retryAfterMs = Math . max (
276279 0 ,
277- ( snapshot . oldest ?. getTime ( ) ?? 0 ) + windowMs - now . getTime ( ) ,
280+ ( snapshot . oldest ?. getTime ( ) ?? 0 ) + snapshot . windowMs - now . getTime ( ) ,
278281 )
279282 return {
280283 status : 'rate_limited' ,
0 commit comments