@@ -129,10 +129,50 @@ export interface AggregateResult {
129129 readonly outOfWindow : number ;
130130}
131131
132+ /**
133+ * Partial copies share a billed message's key: keep each token channel's maximum and the larger
134+ * reported cost, and the first copy's time, model, session and speed so it stays in its bucket.
135+ */
136+ export function mergeUsageCopies ( held : UsageRecord , copy : UsageRecord ) : UsageRecord {
137+ const a = held . totals ;
138+ const b = copy . totals ;
139+ const reportedCostUsd =
140+ held . reportedCostUsd === null || copy . reportedCostUsd === null
141+ ? ( held . reportedCostUsd ?? copy . reportedCostUsd )
142+ : Math . max ( held . reportedCostUsd , copy . reportedCostUsd ) ;
143+ if (
144+ b . uncachedInputTokens <= a . uncachedInputTokens &&
145+ b . cachedInputTokens <= a . cachedInputTokens &&
146+ b . cacheCreationTokens <= a . cacheCreationTokens &&
147+ b . outputTokens <= a . outputTokens &&
148+ b . reasoningTokens <= a . reasoningTokens &&
149+ reportedCostUsd === held . reportedCostUsd
150+ ) {
151+ return held ;
152+ }
153+ return {
154+ ...held ,
155+ totals : {
156+ uncachedInputTokens : Math . max ( a . uncachedInputTokens , b . uncachedInputTokens ) ,
157+ cachedInputTokens : Math . max ( a . cachedInputTokens , b . cachedInputTokens ) ,
158+ cacheCreationTokens : Math . max ( a . cacheCreationTokens , b . cacheCreationTokens ) ,
159+ outputTokens : Math . max ( a . outputTokens , b . outputTokens ) ,
160+ reasoningTokens : Math . max ( a . reasoningTokens , b . reasoningTokens ) ,
161+ } ,
162+ reportedCostUsd,
163+ } ;
164+ }
165+
166+ interface Contribution {
167+ record : UsageRecord ;
168+ /** Null when the first copy fell outside the window. */
169+ readonly bucket : MutableBucket | null ;
170+ }
171+
132172/** Deduplicates across the whole scan; Claude copies records into resumed/forked transcripts. */
133173export class UsageAggregator {
134174 readonly #buckets = new Map < string , MutableBucket > ( ) ;
135- readonly #seen = new Set < string > ( ) ;
175+ readonly #seen = new Map < string , Contribution > ( ) ;
136176 readonly #toDay: ( timestampMs : number ) => string ;
137177 readonly #hourlyWindow: { readonly sinceTimeMs : number ; readonly untilTimeMs : number } | null ;
138178 readonly #options: AggregateOptions ;
@@ -167,37 +207,51 @@ export class UsageAggregator {
167207 if ( ! keepsUsageRecord ( this . #options. modelFilter , input ) ) return false ;
168208 const record = this . #mapModel( input ) ;
169209 if ( record . dedupeKey !== null ) {
170- if ( this . #seen. has ( record . dedupeKey ) ) {
210+ const held = this . #seen. get ( record . dedupeKey ) ;
211+ if ( held !== undefined ) {
171212 this . #duplicatesDropped += 1 ;
213+ if ( held . bucket !== null ) held . record = mergeUsageCopies ( held . record , record ) ;
172214 return false ;
173215 }
174- this . #seen. add ( record . dedupeKey ) ;
175216 }
176217
218+ const bucket = this . #placeInWindow( record , sourcePath ?? "" ) ;
219+ if ( record . dedupeKey !== null ) this . #seen. set ( record . dedupeKey , { record, bucket } ) ;
220+ if ( bucket === null ) {
221+ this . #outOfWindow += 1 ;
222+ return false ;
223+ }
224+ // Keyed usage is counted in finish(), once all its copies are merged.
225+ if ( record . dedupeKey === null ) this . #accumulate( bucket , record ) ;
226+ if ( record . sessionId . length > 0 ) bucket . sessions . add ( record . sessionId ) ;
227+ return true ;
228+ }
229+
230+ #placeInWindow( record : UsageRecord , source : string ) : MutableBucket | null {
177231 if (
178232 this . #hourlyWindow !== null &&
179233 ( record . timestampMs < this . #hourlyWindow. sinceTimeMs ||
180234 record . timestampMs >= this . #hourlyWindow. untilTimeMs )
181235 ) {
182- this . #outOfWindow += 1 ;
183- return false ;
236+ return null ;
184237 }
185238
186239 const day = this . #toDay( record . timestampMs ) ;
187240 if (
188241 this . #hourlyWindow === null &&
189242 ( day < this . #options. sinceDay || day > this . #options. untilDay )
190243 ) {
191- this . #outOfWindow += 1 ;
192- return false ;
244+ return null ;
193245 }
194246
195247 const hourIndex =
196248 this . #hourlyWindow === null
197249 ? - 1
198250 : Math . floor ( ( record . timestampMs - this . #hourlyWindow. sinceTimeMs ) / HOUR_MS ) ;
199- const bucket = this . #bucketFor( day , hourIndex , record . provider , record . model , sourcePath ?? "" ) ;
251+ return this . #bucketFor( day , hourIndex , record . provider , record . model , source ) ;
252+ }
200253
254+ #accumulate( bucket : MutableBucket , record : UsageRecord ) : void {
201255 const priced = priceUsage ( this . #options. rates , record , this . #options. priceOverrides ) ;
202256
203257 const totals = bucket . totals ;
@@ -231,8 +285,6 @@ export class UsageAggregator {
231285 bucket . records += 1 ;
232286 if ( priced . costSource === "unpriced" ) bucket . unpricedRecords += 1 ;
233287 if ( priced . costSource === "providerReported" ) bucket . providerReportedRecords += 1 ;
234- if ( record . sessionId . length > 0 ) bucket . sessions . add ( record . sessionId ) ;
235- return true ;
236288 }
237289
238290 // Aliases use the target's rate, replacing provider-specific rateModel.
@@ -288,6 +340,10 @@ export class UsageAggregator {
288340 }
289341
290342 finish ( ) : AggregateResult {
343+ for ( const { record, bucket } of this . #seen. values ( ) ) {
344+ if ( bucket !== null ) this . #accumulate( bucket , record ) ;
345+ }
346+ this . #seen. clear ( ) ;
291347 const buckets : UsageBucket [ ] = [ ] ;
292348 for ( const [ key , bucket ] of this . #buckets) {
293349 const [ day = "" , hourStart = "" , provider = "" , model = "" , sourcePath = "" ] =
0 commit comments