@@ -102,6 +102,10 @@ const ScanCacheJson = Schema.fromJsonString(Schema.Unknown as unknown as Schema.
102102const decodeScanCacheFile = Schema . decodeUnknownEffect ( ScanCacheJson ) ;
103103const encodeScanCacheFile = Schema . encodeEffect ( ScanCacheJson ) ;
104104const encodeUsageRecordKey = Schema . encodeSync ( ScanCacheJson ) ;
105+ const CachedSource = Schema . Struct ( { dir : Schema . String , volumeId : Schema . String } ) ;
106+ const decodeCachedSources = Schema . decodeUnknownOption (
107+ Schema . Struct ( { sources : Schema . Record ( Schema . String , CachedSource ) } ) ,
108+ ) ;
105109
106110export class UsageService extends Context . Service <
107111 UsageService ,
@@ -148,6 +152,7 @@ export const make = Effect.gen(function* () {
148152 const hostEnvironment = yield * HostProcessEnvironment ;
149153
150154 const fileCache : ScanCache = new Map ( ) ;
155+ const sourceCache = new Map < string , typeof CachedSource . Type > ( ) ;
151156 let cacheDirty = false ;
152157
153158 const ratesCachePath = path . join ( config . stateDir , "usage-model-rates.json" ) ;
@@ -243,7 +248,12 @@ export const make = Effect.gen(function* () {
243248 const resolveTranscriptDirs = Effect . fn ( "UsageService.resolveTranscriptDirs" ) ( function * (
244249 settings : ServerSettingsValue ,
245250 ) {
246- const dirs : Array < { provider : UsageProviderKind ; dir : string ; fileName ?: string } > = [ ] ;
251+ const dirs : Array < {
252+ provider : UsageProviderKind ;
253+ dir : string ;
254+ volumeId : string ;
255+ fileName ?: string ;
256+ } > = [ ] ;
247257 const seen = new Set < string > ( ) ;
248258 for ( const driver of [ "claudeAgent" , "codex" , "grok" ] as const ) {
249259 // Disabled accounts still have history. Explicit default slots replace
@@ -281,14 +291,28 @@ export const make = Effect.gen(function* () {
281291 ) ;
282292 }
283293 const directory = path . resolve ( home , provider === "claude" ? "projects" : "sessions" ) ;
284- // Account aliases and Codex auth overlays can share the same history.
294+ const sourceKey = provider + "\0" + directory ;
295+ const previous = sourceCache . get ( sourceKey ) ;
296+ // Keep canonical paths and source fingerprints stable after root cleanup,
297+ // including aliases and clients merging pre-cleanup environment summaries.
285298 const dir = yield * fileSystem
286299 . realPath ( directory )
287- . pipe ( Effect . orElseSucceed ( ( ) => directory ) ) ;
300+ . pipe ( Effect . orElseSucceed ( ( ) => previous ?. dir ?? directory ) ) ;
301+ const currentVolumeId = yield * Effect . promise ( ( ) => readDirectoryVolumeId ( dir ) ) ;
302+ const volumeId = currentVolumeId || ( previous ?. dir === dir ? previous . volumeId : "" ) ;
303+ if ( previous ?. dir !== dir || previous . volumeId !== volumeId ) {
304+ sourceCache . set ( sourceKey , { dir, volumeId } ) ;
305+ cacheDirty = true ;
306+ }
288307 const key = `${ provider } \0${ dir } ` ;
289308 if ( seen . has ( key ) ) continue ;
290309 seen . add ( key ) ;
291- dirs . push ( { provider, dir, ...( provider === "grok" ? { fileName : "updates.jsonl" } : { } ) } ) ;
310+ dirs . push ( {
311+ provider,
312+ dir,
313+ volumeId,
314+ ...( provider === "grok" ? { fileName : "updates.jsonl" } : { } ) ,
315+ } ) ;
292316 }
293317 }
294318 return dirs ;
@@ -309,14 +333,22 @@ export const make = Effect.gen(function* () {
309333 ) ;
310334 if ( document === null ) return ;
311335 for ( const [ path , entry ] of decodeScanCache ( document ) ) fileCache . set ( path , entry ) ;
336+ const sources = decodeCachedSources ( document ) ;
337+ if ( Option . isSome ( sources ) ) {
338+ for ( const [ key , source ] of Object . entries ( sources . value . sources ) )
339+ sourceCache . set ( key , source ) ;
340+ }
312341 } ) ,
313342 ) ;
314343
315344 const persistScanCache = Effect . fn ( "UsageService.persistScanCache" ) ( function * ( ) {
316345 if ( ! cacheDirty ) return ;
317346 // Cleared only after the write lands, so a failed persist is retried on
318347 // the next scan instead of leaving disk permanently stale.
319- yield * encodeScanCacheFile ( encodeScanCache ( fileCache ) ) . pipe (
348+ yield * encodeScanCacheFile ( {
349+ ...encodeScanCache ( fileCache ) ,
350+ sources : Object . fromEntries ( sourceCache ) ,
351+ } ) . pipe (
320352 Effect . flatMap ( ( serialized ) => fileSystem . writeFileString ( scanCachePath , serialized ) ) ,
321353 Effect . map ( ( ) => {
322354 cacheDirty = false ;
@@ -412,8 +444,7 @@ export const make = Effect.gen(function* () {
412444 Effect . provideService ( Path . Path , path ) ,
413445 ) ;
414446 const scanned : ScannedDir [ ] = [ ] ;
415- for ( const { provider, dir, fileName } of dirs ) {
416- const volumeId = yield * Effect . promise ( ( ) => readDirectoryVolumeId ( dir ) ) ;
447+ for ( const { provider, dir, volumeId, fileName } of dirs ) {
417448 const exists = yield * fileSystem
418449 . exists ( dir )
419450 . pipe ( Effect . catchCause ( ( ) => Effect . succeed ( false ) ) ) ;
@@ -534,24 +565,25 @@ export const make = Effect.gen(function* () {
534565 continue ;
535566 }
536567 scannedFiles += 1 ;
568+ const codexEventOccurrences = new Map < string , number > ( ) ;
537569 for ( const record of file . records ) {
538- // Only sessions that contributed in-window count: the mtime slack
539- // admits boundary files whose records fall outside the range.
540- // A moved Codex rollout can coexist with its saved copy. Codex records
541- // have no provider event id, so identify the same session event here .
542- const usageRecord =
543- record . provider === "codex" && record . sessionId . length > 0
544- ? {
545- ... record ,
546- dedupeKey : encodeUsageRecordKey ( [
547- record . provider ,
548- record . sessionId ,
549- record . timestampMs ,
550- record . model ,
551- record . totals ,
552- ] ) ,
553- }
554- : record ;
570+ let usageRecord = record ;
571+ if ( record . provider === "codex" && record . sessionId . length > 0 ) {
572+ // Match moved rollout copies without collapsing repeated equal events
573+ // within one rollout (timestamps can have only second precision) .
574+ const key = encodeUsageRecordKey ( [
575+ record . provider ,
576+ record . sessionId ,
577+ record . timestampMs ,
578+ record . model ,
579+ record . totals ,
580+ ] ) ;
581+ const occurrence = ( codexEventOccurrences . get ( key ) ?? 0 ) + 1 ;
582+ codexEventOccurrences . set ( key , occurrence ) ;
583+ usageRecord = { ... record , dedupeKey : key + ":" + occurrence } ;
584+ }
585+ // Only sessions contributing in-window count; the mtime slack can
586+ // admit boundary files whose records fall outside the range.
555587 if ( aggregator . add ( usageRecord ) && record . sessionId . length > 0 ) {
556588 sessionIds . add ( record . sessionId ) ;
557589 }
0 commit comments