@@ -50,6 +50,7 @@ export interface RefreshAllQuotaDeps {
5050 isOAuthAccountFn : typeof isOAuthAccount
5151 whamFn ?: typeof whamUsageFn
5252 respectBackoff ?: boolean
53+ skipFresherThanMs ?: number
5354}
5455
5556export interface RefreshAllQuotaResult {
@@ -65,53 +66,65 @@ export async function refreshAllQuota(
6566 if ( ! whamFn ) throw new Error ( 'whamFn is required for refreshAllQuota' )
6667
6768 const results : RefreshAllQuotaResult [ ] = [ ]
69+ const isFresh = ( checkedAt : number | undefined ) =>
70+ deps . skipFresherThanMs !== undefined &&
71+ checkedAt !== undefined &&
72+ deps . now ( ) - checkedAt < deps . skipFresherThanMs
6873
6974 // --- MAIN ---
7075 try {
7176 let auth = await deps . getAuth ( )
7277 if ( auth . type === 'oauth' ) {
73- if ( ! auth . access || ( auth . expires ?? 0 ) < deps . now ( ) ) {
74- const tokens = await deps . codexRefreshFn ( {
75- refreshToken : auth . refresh ?? '' ,
76- fetchImpl : deps . fetchImpl ,
77- now : deps . now ,
78- } )
79- await deps . client . auth . set ( {
80- path : { id : 'openai' } ,
81- body : {
82- type : 'oauth' ,
83- access : tokens . access ,
84- refresh : tokens . refresh ,
85- expires : tokens . expires ,
86- } ,
87- } )
88- auth = { ...auth , access : tokens . access , expires : tokens . expires }
89- }
90-
91- if ( auth . access ) {
92- if ( deps . respectBackoff && deps . quotaManager . isBackedOff ( ) ) {
93- results . push ( { account : 'main' , ok : true } )
94- } else {
95- const snap = await whamFn ( {
96- accessToken : auth . access ,
78+ const freshMainQuota = isFresh (
79+ deps . quotaManager . peekMainForPolicy ( deps . storageMainAccountId )
80+ ?. checkedAt ,
81+ )
82+ if ( freshMainQuota ) {
83+ results . push ( { account : 'main' , ok : true } )
84+ } else {
85+ if ( ! auth . access || ( auth . expires ?? 0 ) < deps . now ( ) ) {
86+ const tokens = await deps . codexRefreshFn ( {
87+ refreshToken : auth . refresh ?? '' ,
9788 fetchImpl : deps . fetchImpl ,
9889 now : deps . now ,
99- accountId : deps . storageMainAccountId ,
10090 } )
101- deps . quotaManager . setMain (
102- auth . access ,
103- {
104- quota : snap ,
105- refreshAfter : deps . now ( ) + 5 * 60 * 1000 ,
106- checkedAt : deps . now ( ) ,
91+ await deps . client . auth . set ( {
92+ path : { id : 'openai' } ,
93+ body : {
94+ type : 'oauth' ,
95+ access : tokens . access ,
96+ refresh : tokens . refresh ,
97+ expires : tokens . expires ,
10798 } ,
108- undefined ,
109- true ,
110- )
111- results . push ( { account : 'main' , ok : true } )
99+ } )
100+ auth = { ...auth , access : tokens . access , expires : tokens . expires }
101+ }
102+
103+ if ( auth . access ) {
104+ if ( deps . respectBackoff && deps . quotaManager . isBackedOff ( ) ) {
105+ results . push ( { account : 'main' , ok : true } )
106+ } else {
107+ const snap = await whamFn ( {
108+ accessToken : auth . access ,
109+ fetchImpl : deps . fetchImpl ,
110+ now : deps . now ,
111+ accountId : deps . storageMainAccountId ,
112+ } )
113+ deps . quotaManager . setMain (
114+ auth . access ,
115+ {
116+ quota : snap ,
117+ refreshAfter : deps . now ( ) + 5 * 60 * 1000 ,
118+ checkedAt : deps . now ( ) ,
119+ } ,
120+ undefined ,
121+ true ,
122+ )
123+ results . push ( { account : 'main' , ok : true } )
124+ }
125+ } else {
126+ results . push ( { account : 'main' , ok : false , error : 'no access token' } )
112127 }
113- } else {
114- results . push ( { account : 'main' , ok : false , error : 'no access token' } )
115128 }
116129 } else {
117130 results . push ( {
@@ -135,6 +148,13 @@ export async function refreshAllQuota(
135148 if ( acct . enabled === false || ! deps . isOAuthAccountFn ( acct ) ) continue
136149
137150 try {
151+ if (
152+ isFresh ( deps . quotaManager . peekFallbackForPolicy ( acct . id ) ?. checkedAt )
153+ ) {
154+ results . push ( { account : acct . id , ok : true } )
155+ continue
156+ }
157+
138158 if (
139159 deps . respectBackoff &&
140160 deps . quotaManager . isFallbackBackedOff (
0 commit comments