1+ import { getSidebarState , type SidebarState } from '../sidebar-state'
12import type {
23 FallbackAccountManager ,
34 isOAuthAccount ,
@@ -50,6 +51,8 @@ export interface RefreshAllQuotaDeps {
5051 isOAuthAccountFn : typeof isOAuthAccount
5152 whamFn ?: typeof whamUsageFn
5253 respectBackoff ?: boolean
54+ skipFresherThanMs ?: number
55+ readSidebarState ?: ( ) => Promise < SidebarState >
5356}
5457
5558export interface RefreshAllQuotaResult {
@@ -65,53 +68,83 @@ export async function refreshAllQuota(
6568 if ( ! whamFn ) throw new Error ( 'whamFn is required for refreshAllQuota' )
6669
6770 const results : RefreshAllQuotaResult [ ] = [ ]
71+ const freshnessMs = deps . skipFresherThanMs
72+ let sharedSidebarState : SidebarState | undefined
73+ if ( freshnessMs !== undefined ) {
74+ try {
75+ sharedSidebarState = await ( deps . readSidebarState ?? getSidebarState ) ( )
76+ } catch { }
77+ }
78+ const sharedFallbackCheckedAt = new Map (
79+ sharedSidebarState ?. fallbacks . map ( ( account ) => [
80+ account . id ,
81+ account . quota ?. primary ?. checkedAt ,
82+ ] ) ?? [ ] ,
83+ )
84+ const isFresh = ( ...checkedAts : unknown [ ] ) =>
85+ freshnessMs !== undefined &&
86+ checkedAts . some (
87+ ( checkedAt ) =>
88+ typeof checkedAt === 'number' &&
89+ Number . isFinite ( checkedAt ) &&
90+ deps . now ( ) - checkedAt < freshnessMs ,
91+ )
6892
6993 // --- MAIN ---
7094 try {
7195 let auth = await deps . getAuth ( )
7296 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 ,
97+ const freshMainQuota = isFresh (
98+ deps . quotaManager . peekMainForPolicy ( deps . storageMainAccountId )
99+ ?. checkedAt ,
100+ sharedSidebarState ?. main . quota ?. primary ?. checkedAt ,
101+ )
102+ if ( freshMainQuota ) {
103+ results . push ( { account : 'main' , ok : true } )
104+ } else {
105+ if ( ! auth . access || ( auth . expires ?? 0 ) < deps . now ( ) ) {
106+ const tokens = await deps . codexRefreshFn ( {
107+ refreshToken : auth . refresh ?? '' ,
97108 fetchImpl : deps . fetchImpl ,
98109 now : deps . now ,
99- accountId : deps . storageMainAccountId ,
100110 } )
101- deps . quotaManager . setMain (
102- auth . access ,
103- {
104- quota : snap ,
105- refreshAfter : deps . now ( ) + 5 * 60 * 1000 ,
106- checkedAt : deps . now ( ) ,
111+ await deps . client . auth . set ( {
112+ path : { id : 'openai' } ,
113+ body : {
114+ type : 'oauth' ,
115+ access : tokens . access ,
116+ refresh : tokens . refresh ,
117+ expires : tokens . expires ,
107118 } ,
108- undefined ,
109- true ,
110- )
111- results . push ( { account : 'main' , ok : true } )
119+ } )
120+ auth = { ...auth , access : tokens . access , expires : tokens . expires }
121+ }
122+
123+ if ( auth . access ) {
124+ if ( deps . respectBackoff && deps . quotaManager . isBackedOff ( ) ) {
125+ results . push ( { account : 'main' , ok : true } )
126+ } else {
127+ const snap = await whamFn ( {
128+ accessToken : auth . access ,
129+ fetchImpl : deps . fetchImpl ,
130+ now : deps . now ,
131+ accountId : deps . storageMainAccountId ,
132+ } )
133+ deps . quotaManager . setMain (
134+ auth . access ,
135+ {
136+ quota : snap ,
137+ refreshAfter : deps . now ( ) + 5 * 60 * 1000 ,
138+ checkedAt : deps . now ( ) ,
139+ } ,
140+ undefined ,
141+ true ,
142+ )
143+ results . push ( { account : 'main' , ok : true } )
144+ }
145+ } else {
146+ results . push ( { account : 'main' , ok : false , error : 'no access token' } )
112147 }
113- } else {
114- results . push ( { account : 'main' , ok : false , error : 'no access token' } )
115148 }
116149 } else {
117150 results . push ( {
@@ -135,6 +168,16 @@ export async function refreshAllQuota(
135168 if ( acct . enabled === false || ! deps . isOAuthAccountFn ( acct ) ) continue
136169
137170 try {
171+ if (
172+ isFresh (
173+ deps . quotaManager . peekFallbackForPolicy ( acct . id ) ?. checkedAt ,
174+ sharedFallbackCheckedAt . get ( acct . id ) ,
175+ )
176+ ) {
177+ results . push ( { account : acct . id , ok : true } )
178+ continue
179+ }
180+
138181 if (
139182 deps . respectBackoff &&
140183 deps . quotaManager . isFallbackBackedOff (
0 commit comments