@@ -28,7 +28,6 @@ export type ArchivedSessionTranscript = {
2828} ;
2929
3030const MAX_RESET_ARCHIVE_DISCOVERY_CACHE_ENTRIES = 2048 ;
31- const MAX_RESET_ARCHIVE_HEADER_MATCH_CACHE_ENTRIES = 4096 ;
3231const MAX_RESET_ARCHIVE_CANDIDATES_PER_TRANSCRIPT = 128 ;
3332
3433const resetArchiveDiscoveryCache = new Map <
@@ -39,63 +38,8 @@ const resetArchiveDiscoveryCache = new Map<
3938 archives : ResetArchiveCandidate [ ] ;
4039 }
4140> ( ) ;
42- const resetArchiveHeaderMatchCache = new Map <
43- string ,
44- {
45- mtimeMs : number ;
46- size : number ;
47- matches : boolean ;
48- }
49- > ( ) ;
50-
5141function clearSessionTranscriptResetArchiveDiscoveryCache ( ) : void {
5242 resetArchiveDiscoveryCache . clear ( ) ;
53- resetArchiveHeaderMatchCache . clear ( ) ;
54- }
55-
56- function deleteResetArchiveHeaderMatchesForArchives ( archives : ResetArchiveCandidate [ ] ) : void {
57- if ( archives . length === 0 || resetArchiveHeaderMatchCache . size === 0 ) {
58- return ;
59- }
60- const archivePaths = new Set ( archives . map ( ( archive ) => archive . archivePath ) ) ;
61- for ( const cacheKey of resetArchiveHeaderMatchCache . keys ( ) ) {
62- const archivePath = cacheKey . slice ( cacheKey . indexOf ( "\0" ) + 1 ) ;
63- if ( archivePaths . has ( archivePath ) ) {
64- resetArchiveHeaderMatchCache . delete ( cacheKey ) ;
65- }
66- }
67- }
68-
69- function setResetArchiveDiscoveryCacheEntry (
70- cacheKey : string ,
71- entry : { dirMtimeMs : number ; dirSize : number ; archives : ResetArchiveCandidate [ ] } ,
72- ) : void {
73- resetArchiveDiscoveryCache . set ( cacheKey , entry ) ;
74- while ( resetArchiveDiscoveryCache . size > MAX_RESET_ARCHIVE_DISCOVERY_CACHE_ENTRIES ) {
75- const oldestKey = resetArchiveDiscoveryCache . keys ( ) . next ( ) . value ;
76- if ( typeof oldestKey !== "string" ) {
77- break ;
78- }
79- const oldestEntry = resetArchiveDiscoveryCache . get ( oldestKey ) ;
80- if ( oldestEntry ) {
81- deleteResetArchiveHeaderMatchesForArchives ( oldestEntry . archives ) ;
82- }
83- resetArchiveDiscoveryCache . delete ( oldestKey ) ;
84- }
85- }
86-
87- function setResetArchiveHeaderMatchCacheEntry (
88- cacheKey : string ,
89- entry : { mtimeMs : number ; size : number ; matches : boolean } ,
90- ) : void {
91- resetArchiveHeaderMatchCache . set ( cacheKey , entry ) ;
92- while ( resetArchiveHeaderMatchCache . size > MAX_RESET_ARCHIVE_HEADER_MATCH_CACHE_ENTRIES ) {
93- const oldestKey = resetArchiveHeaderMatchCache . keys ( ) . next ( ) . value ;
94- if ( typeof oldestKey !== "string" ) {
95- break ;
96- }
97- resetArchiveHeaderMatchCache . delete ( oldestKey ) ;
98- }
9943}
10044
10145function classifySessionTranscriptCandidate (
@@ -193,15 +137,6 @@ async function resetArchiveHeaderMatchesSessionId(
193137 if ( ! stat ?. isFile ( ) ) {
194138 return false ;
195139 }
196- const cacheKey = `${ sessionId } \0${ archivePath } ` ;
197- const cached = resetArchiveHeaderMatchCache . get ( cacheKey ) ;
198- if ( cached && cached . mtimeMs === stat . mtimeMs && cached . size === stat . size ) {
199- resetArchiveHeaderMatchCache . delete ( cacheKey ) ;
200- resetArchiveHeaderMatchCache . set ( cacheKey , cached ) ;
201- return cached . matches ;
202- }
203-
204- let matches = false ;
205140 const handle = await fs . promises . open ( probePath , "r" ) . catch ( ( ) => null ) ;
206141 if ( ! handle ) {
207142 return false ;
@@ -216,24 +151,19 @@ async function resetArchiveHeaderMatchesSessionId(
216151 continue ;
217152 }
218153 const record = JSON . parse ( trimmed ) as unknown ;
219- matches =
154+ return (
220155 Boolean ( record ) &&
221156 typeof record === "object" &&
222157 ! Array . isArray ( record ) &&
223158 ( record as { type ?: unknown ; id ?: unknown } ) . type === "session" &&
224- ( record as { type ?: unknown ; id ?: unknown } ) . id === sessionId ;
225- return matches ;
159+ ( record as { type ?: unknown ; id ?: unknown } ) . id === sessionId
160+ ) ;
226161 }
227162 return false ;
228163 } catch {
229164 return false ;
230165 } finally {
231166 await handle . close ( ) . catch ( ( ) => undefined ) ;
232- setResetArchiveHeaderMatchCacheEntry ( cacheKey , {
233- mtimeMs : stat . mtimeMs ,
234- size : stat . size ,
235- matches,
236- } ) ;
237167 }
238168}
239169
@@ -276,11 +206,14 @@ async function listResetArchiveCandidatesForTranscriptAsync(
276206 ( left , right ) => right . timestamp - left . timestamp || right . name . localeCompare ( left . name ) ,
277207 ) ;
278208 const boundedArchives = archives . slice ( 0 , MAX_RESET_ARCHIVE_CANDIDATES_PER_TRANSCRIPT ) ;
279- setResetArchiveDiscoveryCacheEntry ( cacheKey , {
209+ resetArchiveDiscoveryCache . set ( cacheKey , {
280210 dirMtimeMs : dirStat . mtimeMs ,
281211 dirSize : dirStat . size ,
282212 archives : boundedArchives ,
283213 } ) ;
214+ if ( resetArchiveDiscoveryCache . size > MAX_RESET_ARCHIVE_DISCOVERY_CACHE_ENTRIES ) {
215+ resetArchiveDiscoveryCache . delete ( resetArchiveDiscoveryCache . keys ( ) . next ( ) . value ?? "" ) ;
216+ }
284217 return boundedArchives ;
285218}
286219
0 commit comments