MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / loadStatsCache

Function loadStatsCache

source code/utils/statsCache.ts:149–210  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

147 * Returns an empty cache if the file doesn't exist or is invalid.
148 */
149export async function loadStatsCache(): Promise<PersistedStatsCache> {
150 const fs = getFsImplementation()
151 const cachePath = getStatsCachePath()
152
153 try {
154 const content = await fs.readFile(cachePath, { encoding: 'utf-8' })
155 const parsed = jsonParse(content) as PersistedStatsCache
156
157 // Validate version
158 if (parsed.version !== STATS_CACHE_VERSION) {
159 const migrated = migrateStatsCache(parsed)
160 if (!migrated) {
161 logForDebugging(
162 `Stats cache version ${parsed.version} not migratable (expected ${STATS_CACHE_VERSION}), returning empty cache`,
163 )
164 return getEmptyCache()
165 }
166 logForDebugging(
167 `Migrated stats cache from v${parsed.version} to v${STATS_CACHE_VERSION}`,
168 )
169 // Persist migration so we don't re-migrate on every load.
170 // aggregateClaudeCodeStats() skips its save when lastComputedDate is
171 // already current, so without this the on-disk file stays at the old
172 // version indefinitely.
173 await saveStatsCache(migrated)
174 if (feature('SHOT_STATS') && !migrated.shotDistribution) {
175 logForDebugging(
176 'Migrated stats cache missing shotDistribution, forcing recomputation',
177 )
178 return getEmptyCache()
179 }
180 return migrated
181 }
182
183 // Basic validation
184 if (
185 !Array.isArray(parsed.dailyActivity) ||
186 !Array.isArray(parsed.dailyModelTokens) ||
187 typeof parsed.totalSessions !== 'number' ||
188 typeof parsed.totalMessages !== 'number'
189 ) {
190 logForDebugging(
191 'Stats cache has invalid structure, returning empty cache',
192 )
193 return getEmptyCache()
194 }
195
196 // If SHOT_STATS is enabled but cache doesn't have shotDistribution,
197 // force full recomputation to get historical shot data
198 if (feature('SHOT_STATS') && !parsed.shotDistribution) {
199 logForDebugging(
200 'Stats cache missing shotDistribution, forcing recomputation',
201 )
202 return getEmptyCache()
203 }
204
205 return parsed
206 } catch (error) {

Callers 1

aggregateClaudeCodeStatsFunction · 0.85

Calls 9

getFsImplementationFunction · 0.85
getStatsCachePathFunction · 0.85
jsonParseFunction · 0.85
migrateStatsCacheFunction · 0.85
logForDebuggingFunction · 0.85
getEmptyCacheFunction · 0.85
saveStatsCacheFunction · 0.85
readFileMethod · 0.80
errorMessageFunction · 0.70

Tested by

no test coverage detected