@@ -89,41 +89,40 @@ export class UsageService extends Context.Service<
8989 {
9090 readonly readSummary : ( input : UsageSummaryInput ) => Effect . Effect < UsageSummary , UsageReadError > ;
9191 }
92- > ( ) ( "t3/usage/UsageService" ) {
93- /** Empty summary, for suites that only need the RPC surface to resolve. */
94- static readonly layerTest = Layer . succeed (
95- UsageService ,
96- UsageService . of ( {
97- readSummary : ( input ) =>
98- Effect . succeed ( {
99- contractVersion : USAGE_CONTRACT_VERSION ,
100- readAt : "1970-01-01T00:00:00.000Z" ,
101- timeZone : input . timeZone ,
102- sinceDay : input . sinceDay ,
103- untilDay : input . untilDay ,
104- buckets : [ ] ,
105- sources : [ ] ,
106- pricing : {
107- status : "unavailable" ,
108- source : LITELLM_RATES_URL ,
109- fetchedAt : null ,
110- knownModels : 0 ,
111- } ,
112- scanDurationMs : 0 ,
113- } ) ,
114- } ) ,
115- ) ;
116- }
92+ > ( ) ( "t3/usage/UsageService" ) { }
93+
94+ /** Empty summary, for suites that only need the RPC surface to resolve. */
95+ export const layerTest = Layer . succeed (
96+ UsageService ,
97+ UsageService . of ( {
98+ readSummary : ( input ) =>
99+ Effect . succeed ( {
100+ contractVersion : USAGE_CONTRACT_VERSION ,
101+ readAt : "1970-01-01T00:00:00.000Z" ,
102+ timeZone : input . timeZone ,
103+ sinceDay : input . sinceDay ,
104+ untilDay : input . untilDay ,
105+ buckets : [ ] ,
106+ sources : [ ] ,
107+ pricing : {
108+ status : "unavailable" ,
109+ source : LITELLM_RATES_URL ,
110+ fetchedAt : null ,
111+ knownModels : 0 ,
112+ } ,
113+ scanDurationMs : 0 ,
114+ } ) ,
115+ } ) ,
116+ ) ;
117117
118- const make = Effect . gen ( function * ( ) {
118+ export const make = Effect . gen ( function * ( ) {
119119 const fileSystem = yield * FileSystem . FileSystem ;
120120 const path = yield * Path . Path ;
121121 const config = yield * ServerConfig ;
122122 const settingsService = yield * ServerSettings . ServerSettingsService ;
123123 const httpClient = yield * HttpClient . HttpClient ;
124124
125125 const fileCache : ScanCache = new Map ( ) ;
126- let cacheLoaded = false ;
127126 let cacheDirty = false ;
128127
129128 const ratesCachePath = path . join ( config . stateDir , "usage-model-rates.json" ) ;
@@ -163,7 +162,12 @@ const make = Effect.gen(function* () {
163162 Effect . timeout ( 10_000 ) ,
164163 Effect . catchCause ( ( ) => Effect . succeed ( null ) ) ,
165164 ) ;
166- if ( fetched === null ) return ;
165+ if ( fetched === null ) {
166+ // The refresh failed; whatever we are serving is now past its TTL and
167+ // must not keep claiming to be fresh.
168+ if ( rates . size > 0 ) ratesStatus = "cached" ;
169+ return ;
170+ }
167171
168172 const parsed = parseRateTable ( fetched ) ;
169173 if ( parsed . size === 0 ) return ;
@@ -193,12 +197,17 @@ const make = Effect.gen(function* () {
193197
194198 /** Resolves the transcript directory for each provider. */
195199 const resolveTranscriptDirs = Effect . fn ( "UsageService.resolveTranscriptDirs" ) ( function * ( ) {
200+ // A settings failure must surface as an error: swallowing it here would
201+ // present "zero usage from every provider" as a valid answer.
196202 const settings = yield * settingsService . getSettings . pipe (
197- Effect . catchCause ( ( ) => Effect . succeed ( null ) ) ,
203+ Effect . catchCause (
204+ ( cause ) =>
205+ new UsageReadError ( {
206+ reason : "scanFailed" ,
207+ detail : `Server settings could not be read: ${ String ( cause ) } ` ,
208+ } ) ,
209+ ) ,
198210 ) ;
199- if ( settings === null ) {
200- return [ ] as readonly { provider : UsageProviderKind ; dir : string } [ ] ;
201- }
202211
203212 const claudeHome = yield * resolveClaudeHomePath ( settings . providers . claudeAgent ) ;
204213 const claudeDir = yield * resolveClaudeTranscriptDir ( claudeHome ) ;
@@ -210,25 +219,33 @@ const make = Effect.gen(function* () {
210219 ] ;
211220 } ) ;
212221
213- /** Loads the persisted scan cache once per process. */
214- const ensureScanCacheLoaded = Effect . fn ( "UsageService.ensureScanCacheLoaded" ) ( function * ( ) {
215- if ( cacheLoaded ) return ;
216- cacheLoaded = true ;
217-
218- const document = yield * fileSystem . readFileString ( scanCachePath ) . pipe (
219- Effect . flatMap ( ( raw ) => decodeScanCacheFile ( raw ) ) ,
220- Effect . catchCause ( ( ) => Effect . succeed ( null ) ) ,
221- ) ;
222- if ( document === null ) return ;
223-
224- for ( const [ path , entry ] of decodeScanCache ( document ) ) fileCache . set ( path , entry ) ;
225- } ) ;
222+ /**
223+ * Loads the persisted scan cache exactly once per process.
224+ *
225+ * `Effect.cached` makes concurrent first readers await the same load rather
226+ * than each seeing a "loaded" flag set before the read finished and cold
227+ * scanning against an empty cache.
228+ */
229+ const ensureScanCacheLoaded = yield * Effect . cached (
230+ Effect . gen ( function * ( ) {
231+ const document = yield * fileSystem . readFileString ( scanCachePath ) . pipe (
232+ Effect . flatMap ( ( raw ) => decodeScanCacheFile ( raw ) ) ,
233+ Effect . catchCause ( ( ) => Effect . succeed ( null ) ) ,
234+ ) ;
235+ if ( document === null ) return ;
236+ for ( const [ path , entry ] of decodeScanCache ( document ) ) fileCache . set ( path , entry ) ;
237+ } ) ,
238+ ) ;
226239
227240 const persistScanCache = Effect . fn ( "UsageService.persistScanCache" ) ( function * ( ) {
228241 if ( ! cacheDirty ) return ;
229- cacheDirty = false ;
242+ // Cleared only after the write lands, so a failed persist is retried on
243+ // the next scan instead of leaving disk permanently stale.
230244 yield * encodeScanCacheFile ( encodeScanCache ( fileCache ) ) . pipe (
231245 Effect . flatMap ( ( serialized ) => fileSystem . writeFileString ( scanCachePath , serialized ) ) ,
246+ Effect . map ( ( ) => {
247+ cacheDirty = false ;
248+ } ) ,
232249 // A cache we cannot write is a slower next start, not a failed read.
233250 Effect . catchCause ( ( ) => Effect . void ) ,
234251 ) ;
@@ -246,6 +263,9 @@ const make = Effect.gen(function* () {
246263 if ( cached && cached . size === size && cached . mtimeMs === mtimeMs ) return cached . records ;
247264
248265 const parsed = yield * Effect . promise ( ( ) => readTranscriptRecords ( filePath , provider ) ) ;
266+ // A read failure is not an empty transcript: caching it under this
267+ // (size, mtime) would silently drop the file's usage until it changes.
268+ if ( parsed === null ) return [ ] ;
249269 // Stored already de-duplicated within the file, which is 99% of all
250270 // duplicates. The aggregator still runs the cross-file dedupe pass.
251271 const records = dedupeWithinFile ( parsed ) ;
@@ -265,7 +285,7 @@ const make = Effect.gen(function* () {
265285
266286 const startedAtMs = yield * Clock . currentTimeMillis ;
267287 yield * ensureRates ( ) ;
268- yield * ensureScanCacheLoaded ( ) ;
288+ yield * ensureScanCacheLoaded ;
269289
270290 const hostId = NodeOS . hostname ( ) ;
271291 // The home resolvers ask for `Path` themselves; satisfy them from the
@@ -289,6 +309,7 @@ const make = Effect.gen(function* () {
289309
290310 const sources : UsageSource [ ] = [ ] ;
291311 const livePaths = new Set < string > ( ) ;
312+ const walkedRoots : string [ ] = [ ] ;
292313
293314 for ( const { provider, dir } of dirs ) {
294315 const volumeId = yield * Effect . promise ( ( ) => readDirectoryVolumeId ( dir ) ) ;
@@ -303,14 +324,19 @@ const make = Effect.gen(function* () {
303324 scannedFiles : 0 ,
304325 skippedFiles : 0 ,
305326 malformedRecords : 0 ,
327+ distinctSessions : 0 ,
306328 message : "No transcript directory on this environment." ,
307329 } ) ;
308330 continue ;
309331 }
310332
333+ walkedRoots . push ( dir ) ;
311334 const files = yield * Effect . promise ( ( ) => listTranscriptFiles ( dir , windowStartMs ) ) ;
312335 let scannedFiles = 0 ;
313336 let skippedFiles = 0 ;
337+ // Distinct per directory. Buckets carry per-cell session counts, but a
338+ // session spans days and models, so clients total this figure instead.
339+ const sessionIds = new Set < string > ( ) ;
314340
315341 for ( const file of files ) {
316342 livePaths . add ( file . path ) ;
@@ -320,7 +346,10 @@ const make = Effect.gen(function* () {
320346 continue ;
321347 }
322348 scannedFiles += 1 ;
323- for ( const record of records ) aggregator . add ( record ) ;
349+ for ( const record of records ) {
350+ aggregator . add ( record ) ;
351+ if ( record . sessionId . length > 0 ) sessionIds . add ( record . sessionId ) ;
352+ }
324353 }
325354
326355 sources . push ( {
@@ -329,12 +358,14 @@ const make = Effect.gen(function* () {
329358 scannedFiles,
330359 skippedFiles,
331360 malformedRecords : 0 ,
361+ distinctSessions : sessionIds . size ,
332362 message : null ,
333363 } ) ;
334364 }
335365
336366 const pruned = pruneScanCache ( fileCache , {
337367 livePaths,
368+ walkedRoots,
338369 windowStartMs,
339370 retentionCutoffMs : startedAtMs - CACHE_RETENTION_DAYS * 24 * 60 * 60 * 1000 ,
340371 } ) ;
0 commit comments