@@ -112,20 +112,56 @@ export function collectExternalUsageLinks(presentations: LimitPresentations) {
112112 return [ ...links . values ( ) ] ;
113113}
114114
115- /** Workspace identity takes precedence: one email can own several separate quotas. */
116- function accountKey (
115+ function fallbackAccountKey (
117116 driver : ServerProvider [ "driver" ] ,
118117 email : string | undefined ,
119118 limits ?: ServerProviderUsageLimits ,
120119) : string | null {
121- if ( limits ?. accountId ) return `${ driver } :account:${ limits . accountId } ` ;
122120 const normalizedEmail = email ?. trim ( ) . toLowerCase ( ) ;
123121 if ( normalizedEmail ) return `${ driver } :${ normalizedEmail } ` ;
124122 return limits ?. credentialFingerprint
125123 ? `${ driver } :credential:${ limits . credentialFingerprint } `
126124 : null ;
127125}
128126
127+ /** Resolve legacy reports only when their fallback identifies one explicit account. */
128+ function accountKeyResolver (
129+ providers : readonly ServerProvider [ ] ,
130+ sources : UsageLimitSourceSnapshots ,
131+ ) {
132+ const reports = [
133+ ...providersWithLimits ( providers ) . map ( ( provider ) => ( {
134+ driver : provider . driver ,
135+ email : provider . auth . email ,
136+ limits : provider . usageLimits ,
137+ } ) ) ,
138+ ...sources . flatMap ( ( source ) =>
139+ source . accounts . map ( ( account ) => ( {
140+ driver : account . driver ,
141+ email : account . email ,
142+ limits : account . usageLimits ,
143+ } ) ) ,
144+ ) ,
145+ ] ;
146+ const aliases = new Map < string , string | null > ( ) ;
147+ for ( const { driver, email, limits } of reports ) {
148+ if ( ! limits ?. accountId || limitsNotice ( limits ) !== null ) continue ;
149+ const fallback = fallbackAccountKey ( driver , email , limits ) ;
150+ if ( ! fallback ) continue ;
151+ const key = `${ driver } :account:${ limits . accountId } ` ;
152+ aliases . set ( fallback , aliases . has ( fallback ) && aliases . get ( fallback ) !== key ? null : key ) ;
153+ }
154+ return (
155+ driver : ServerProvider [ "driver" ] ,
156+ email : string | undefined ,
157+ limits ?: ServerProviderUsageLimits ,
158+ ) => {
159+ if ( limits ?. accountId ) return `${ driver } :account:${ limits . accountId } ` ;
160+ const fallback = fallbackAccountKey ( driver , email , limits ) ;
161+ return fallback ? ( aliases . get ( fallback ) ?? fallback ) : null ;
162+ } ;
163+ }
164+
129165/**
130166 * One subscription account as the pooled views see it, whichever way it was
131167 * reported. Matching account ids, or emails and credentials when no id is
@@ -161,6 +197,14 @@ export interface LimitAccount {
161197 * native instances supply names and environment labels.
162198 */
163199export function collectLimitAccounts ( presentations : LimitPresentations ) : readonly LimitAccount [ ] {
200+ const accountKey = accountKeyResolver (
201+ [ ...presentations . values ( ) ] . flatMap (
202+ ( presentation ) => presentation . serverConfig ?. providers ?? [ ] ,
203+ ) ,
204+ [ ...presentations . values ( ) ] . flatMap (
205+ ( presentation ) => presentation . serverConfig ?. usageLimitSources ?? [ ] ,
206+ ) ,
207+ ) ;
164208 const accounts = new Map < string , LimitAccount > ( ) ;
165209 const creditSources = new Map < string , LimitAccount > ( ) ;
166210 const hubRedeems = new Map < string , LimitAccount > ( ) ;
@@ -188,7 +232,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl
188232 }
189233 const previous = accounts . get ( key ) ;
190234 if ( ! previous ) {
191- accounts . set ( key , next ) ;
235+ accounts . set ( key , { ... next , key : next . limits . accountId ? key : next . key } ) ;
192236 return ;
193237 }
194238 const fresher = Date . parse ( next . limits . checkedAt ) > Date . parse ( previous . limits . checkedAt ) ;
@@ -206,6 +250,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl
206250 const creditSource = creditSources . get ( key ) ;
207251 accounts . set ( key , {
208252 ...previous ,
253+ key : next . limits . accountId ? key : previous . key ,
209254 displayName : previous . displayName ?? next . displayName ,
210255 plan : previous . plan ?? next . plan ,
211256 accentColor : previous . accentColor ?? next . accentColor ,
@@ -634,6 +679,7 @@ export function collectProviderUsageLimits(
634679 const native = providersWithLimits ( providers ) . filter (
635680 ( provider ) => provider . driver === selected . driver ,
636681 ) ;
682+ const accountKey = accountKeyResolver ( native , sources ) ;
637683 const nativeAccounts = new Set (
638684 native . flatMap ( ( provider ) => {
639685 const key = accountKey ( provider . driver , provider . auth . email , provider . usageLimits ) ;
0 commit comments