@@ -3,7 +3,6 @@ import * as Digest from "effect/crypto/Digest";
33import * as Duration from "effect/Duration" ;
44import * as Effect from "effect/Effect" ;
55import * as Base64Url from "effect/encoding/Base64Url" ;
6- import * as Hex from "effect/encoding/Hex" ;
76import * as Fiber from "effect/Fiber" ;
87import * as FetchHttpClient from "effect/http/FetchHttpClient" ;
98import * as HttpClient from "effect/http/HttpClient" ;
@@ -55,12 +54,6 @@ const billedEventIdentity = (...fields: readonly unknown[]) => JSON.stringify(fi
5554
5655const textEncoder = new TextEncoder ( ) ;
5756
58- // Untraced: each usage scan hashes every billed event.
59- const accountHash = Effect . fnUntraced ( function * ( value : string ) {
60- const digest = yield * Digest . Digest ;
61- return Hex . encode ( yield * digest . digest ( Digest . sha256 , textEncoder . encode ( value ) ) ) ;
62- } ) ;
63-
6457function canonicalJson ( value : unknown ) : string {
6558 if ( Array . isArray ( value ) ) return `[${ value . map ( canonicalJson ) . join ( "," ) } ]` ;
6659 if ( value !== null && typeof value === "object" ) {
@@ -93,6 +86,9 @@ export const readCursorAccountUsage = Effect.fn("readCursorAccountUsage")(functi
9386 endDate : number ,
9487) : Effect . fn . Return < CursorAccountUsageReadResult , never , Digest . Digest | HttpClient . HttpClient > {
9588 const httpClient = yield * HttpClient . HttpClient ;
89+ const digest = yield * Digest . Digest ;
90+ // Per-event hashes share the scan span and use the native hex encoder.
91+ const accountHash = ( value : string ) => digest . digestHex ( Digest . sha256 , textEncoder . encode ( value ) ) ;
9692 const credential = yield * Effect . result ( exchangeCursorApiKey ( credentialSource . apiKey ) ) ;
9793 if ( Result . isFailure ( credential ) ) {
9894 return {
@@ -124,8 +120,9 @@ export const readCursorAccountUsage = Effect.fn("readCursorAccountUsage")(functi
124120 ) ;
125121 const records : UsageRecord [ ] = [ ] ;
126122 const occurrences = new Map < string , number > ( ) ;
127- const pages : unknown [ ] [ ] = [ ] ;
128- let completed = false ;
123+ const normalized : Array < ( Omit < UsageRecord , "dedupeKey" > & { dedupeKey : string } ) | null > = [ ] ;
124+ const pages : Array < { start : number ; end : number ; overlap : number } > = [ ] ;
125+ let previousEvents : unknown [ ] = [ ] ;
129126 const pageSize = 1000 ;
130127 let total : number | undefined ;
131128 const readPage = Effect . fn ( "CursorAccountUsage.readPage" ) (
@@ -210,30 +207,23 @@ export const readCursorAccountUsage = Effect.fn("readCursorAccountUsage")(functi
210207 const next = ahead . shift ( ) ;
211208 const events = yield * next === undefined ? readPage ( page ) : Fiber . join ( next ) ;
212209 if ( ! Array . isArray ( events ) ) return events ;
213- pages . push ( events ) ;
214- if ( events . length < pageSize ) {
215- completed = true ;
216- break ;
217- }
218- }
219- if ( ! completed ) return yield * invalid ( "Account usage page limit exceeded" ) ;
220- const rawCount = pages . reduce ( ( sum , page ) => sum + page . length , 0 ) ;
221- if ( total !== undefined && rawCount < total )
222- return yield * invalid ( "Incomplete account usage pages" ) ;
223- let removalsRemaining = total === undefined ? 0 : rawCount - total ;
224- let previousKeys : string [ ] = [ ] ;
225- for ( const events of pages ) {
226- const eventKeys =
227- removalsRemaining > 0
228- ? yield * Effect . forEach ( events , ( event ) => accountHash ( canonicalJson ( event ) ) )
229- : [ ] ;
230- const removalCount = Math . min ( removalsRemaining , boundaryOverlap ( previousKeys , eventKeys ) ) ;
231- removalsRemaining -= removalCount ;
232- previousKeys = eventKeys ;
233- for ( const raw of events . slice ( removalCount ) ) {
210+ const start = normalized . length ;
211+ const firstTimestamp = object ( events [ 0 ] ) . timestamp ;
212+ // Equal raw events must have equal timestamps; avoid canonicalizing disjoint pages.
213+ const mayOverlap =
214+ ( typeof firstTimestamp !== "string" && typeof firstTimestamp !== "number" ) ||
215+ previousEvents . some ( ( event ) => object ( event ) . timestamp === firstTimestamp ) ;
216+ const overlap = mayOverlap
217+ ? boundaryOverlap ( previousEvents . map ( canonicalJson ) , events . map ( canonicalJson ) )
218+ : 0 ;
219+ previousEvents = events ;
220+ for ( const raw of events ) {
234221 const event = object ( raw ) ;
235222 const usage = object ( event . tokenUsage ) ;
236- if ( event . tokenUsage === undefined || event . tokenUsage === null ) continue ;
223+ if ( event . tokenUsage === undefined || event . tokenUsage === null ) {
224+ normalized . push ( null ) ;
225+ continue ;
226+ }
237227 for ( const key of [
238228 "inputTokens" ,
239229 "outputTokens" ,
@@ -260,7 +250,10 @@ export const readCursorAccountUsage = Effect.fn("readCursorAccountUsage")(functi
260250 ! event . model
261251 )
262252 return yield * invalid ( "Invalid account usage event" ) ;
263- if ( timestampMs < sinceMs || timestampMs > endDate ) continue ;
253+ if ( timestampMs < sinceMs || timestampMs > endDate ) {
254+ normalized . push ( null ) ;
255+ continue ;
256+ }
264257 const totals = {
265258 uncachedInputTokens : tokens ( usage . inputTokens ) ,
266259 cachedInputTokens : tokens ( usage . cacheReadTokens ) ,
@@ -275,9 +268,7 @@ export const readCursorAccountUsage = Effect.fn("readCursorAccountUsage")(functi
275268 const key = yield * accountHash (
276269 billedEventIdentity ( timestampMs , event . model , sessionId , totals , reportedCostUsd ) ,
277270 ) ;
278- const occurrence = occurrences . get ( key ) ?? 0 ;
279- occurrences . set ( key , occurrence + 1 ) ;
280- records . push ( {
271+ normalized . push ( {
281272 provider : "cursor" ,
282273 timestampMs,
283274 model : event . model ,
@@ -286,12 +277,39 @@ export const readCursorAccountUsage = Effect.fn("readCursorAccountUsage")(functi
286277 totals,
287278 reportedCostUsd,
288279 speed : "standard" ,
289- dedupeKey : `cursor-account: ${ accountKey } : ${ key } : ${ occurrence } ` ,
280+ dedupeKey : key ,
290281 } ) ;
291282 }
283+ pages . push ( { start, end : normalized . length , overlap } ) ;
284+ if ( events . length < pageSize ) break ;
285+ yield * Effect . yieldNow ;
286+ }
287+ const rawCount = normalized . length ;
288+ if ( total !== undefined && rawCount < total )
289+ return yield * invalid ( "Incomplete account usage pages" ) ;
290+ let removalsRemaining = total === undefined ? 0 : rawCount - total ;
291+ // The final count proves which boundary copies to remove, including non-token events.
292+ for ( const page of pages ) {
293+ const removalCount = Math . min ( removalsRemaining , page . overlap ) ;
294+ removalsRemaining -= removalCount ;
295+ for ( let index = page . start ; index < page . end ; index ++ ) {
296+ const record = normalized [ index ] ;
297+ normalized [ index ] = null ;
298+ if ( index < page . start + removalCount || record == null ) continue ;
299+ const key = record . dedupeKey ;
300+ const occurrence = occurrences . get ( key ) ?? 0 ;
301+ occurrences . set ( key , occurrence + 1 ) ;
302+ record . dedupeKey = `cursor-account:${ accountKey } :${ key } :${ occurrence } ` ;
303+ records . push ( record ) ;
304+ }
292305 yield * Effect . yieldNow ;
293306 }
294307 if ( removalsRemaining !== 0 ) return yield * invalid ( "Inconsistent account usage boundaries" ) ;
308+ yield * Effect . annotateCurrentSpan ( {
309+ "cursor.usage.pages" : pages . length ,
310+ "cursor.usage.events" : rawCount ,
311+ "cursor.usage.records" : records . length ,
312+ } ) ;
295313 return {
296314 accountKey,
297315 records,
0 commit comments