@@ -118,22 +118,29 @@ internal sealed class CachedAttachmentItem
118118 private readonly object _attachmentSaveGate = new ( ) ;
119119 private readonly string _toolCacheFilePath ;
120120 private readonly string _attachmentCacheFilePath ;
121+ private readonly Action < long > ? _attachmentSnapshotCaptured ;
121122 private Dictionary < string , List < CachedToolMeta > > _toolCache ;
122123 private Dictionary < string , List < CachedAttachmentMeta > > _attachmentCache ;
123124 private readonly Dictionary < string , long > _evictedResetGenerations = new ( StringComparer . Ordinal ) ;
124125 private Timer ? _toolSaveTimer ;
125126 private long _toolSaveVersion ;
127+ private long _attachmentSaveVersion ;
126128 private bool _toolCacheDirty ;
129+ private bool _attachmentCacheDirty ;
127130 private bool _disposed ;
128131
129- internal ChatMetadataStore ( string toolCacheFilePath , string ? attachmentCacheFilePath = null )
132+ internal ChatMetadataStore (
133+ string toolCacheFilePath ,
134+ string ? attachmentCacheFilePath = null ,
135+ Action < long > ? attachmentSnapshotCaptured = null )
130136 {
131137 _toolCacheFilePath = ! string . IsNullOrWhiteSpace ( toolCacheFilePath )
132138 ? toolCacheFilePath
133139 : throw new ArgumentException ( "Tool metadata cache path is required." , nameof ( toolCacheFilePath ) ) ;
134140 _attachmentCacheFilePath = ! string . IsNullOrWhiteSpace ( attachmentCacheFilePath )
135141 ? attachmentCacheFilePath
136142 : DefaultAttachmentMetaCacheFilePath ( _toolCacheFilePath ) ;
143+ _attachmentSnapshotCaptured = attachmentSnapshotCaptured ;
137144 _toolCache = LoadToolMetaCache ( _toolCacheFilePath ) ;
138145 _attachmentCache = LoadAttachmentMetaCache ( _attachmentCacheFilePath ) ;
139146 }
@@ -308,6 +315,7 @@ internal void CacheAttachments(
308315 if ( items . Count == 0 )
309316 return ;
310317
318+ long saveVersion ;
311319 lock ( _gate )
312320 {
313321 if ( _disposed || IsStaleResetGenerationLocked ( threadId , resetGeneration ) )
@@ -332,9 +340,11 @@ internal void CacheAttachments(
332340 } ) ;
333341 if ( list . Count > MaxAttachmentEntriesPerSession )
334342 list . RemoveRange ( 0 , list . Count - MaxAttachmentEntriesPerSession ) ;
343+ _attachmentCacheDirty = true ;
344+ saveVersion = ++ _attachmentSaveVersion ;
335345 }
336346
337- SaveAttachmentCache ( ) ;
347+ SaveAttachmentCache ( saveVersion ) ;
338348 }
339349
340350 internal Queue < CachedToolMeta > ? GetToolMetadata (
@@ -429,6 +439,7 @@ internal void EvictReset(string threadId, string? oldSessionId, long resetGenera
429439 {
430440 var saveTool = false ;
431441 var saveAttachments = false ;
442+ long attachmentSaveVersion = 0 ;
432443 lock ( _gate )
433444 {
434445 if ( _evictedResetGenerations . TryGetValue ( threadId , out var current ) &&
@@ -463,12 +474,17 @@ internal void EvictReset(string threadId, string? oldSessionId, long resetGenera
463474 _toolCacheDirty = true ;
464475 _toolSaveVersion ++ ;
465476 }
477+ if ( saveAttachments )
478+ {
479+ _attachmentCacheDirty = true ;
480+ attachmentSaveVersion = ++ _attachmentSaveVersion ;
481+ }
466482 }
467483
468484 if ( saveTool )
469485 SaveToolCache ( ) ;
470486 if ( saveAttachments )
471- SaveAttachmentCache ( ) ;
487+ SaveAttachmentCache ( attachmentSaveVersion ) ;
472488 }
473489
474490 private bool RemoveOlderToolEntries (
@@ -524,16 +540,18 @@ private bool RemoveOlderAttachmentEntries(
524540 internal void Flush ( )
525541 {
526542 Timer ? timer ;
543+ long attachmentSaveVersion ;
527544 lock ( _gate )
528545 {
529546 timer = _toolSaveTimer ;
530547 _toolSaveTimer = null ;
531548 _toolSaveVersion ++ ;
549+ attachmentSaveVersion = _attachmentSaveVersion ;
532550 }
533551
534552 timer ? . Dispose ( ) ;
535553 SaveToolCache ( ) ;
536- SaveAttachmentCache ( ) ;
554+ SaveAttachmentCache ( attachmentSaveVersion ) ;
537555 }
538556
539557 public void Dispose ( )
@@ -605,24 +623,42 @@ private void SaveToolCache(long? expectedVersion = null)
605623 }
606624 }
607625
608- private void SaveAttachmentCache ( )
626+ private void SaveAttachmentCache ( long expectedVersion )
609627 {
610628 try
611629 {
612630 Dictionary < string , List < CachedAttachmentMeta > > snapshot ;
613631 lock ( _gate )
614632 {
633+ if ( expectedVersion != _attachmentSaveVersion ||
634+ ! _attachmentCacheDirty )
635+ {
636+ return ;
637+ }
638+
615639 snapshot = _attachmentCache . ToDictionary (
616640 pair => pair . Key ,
617641 pair => pair . Value . Select ( Clone ) . ToList ( ) ,
618642 StringComparer . Ordinal ) ;
619643 }
620644
645+ _attachmentSnapshotCaptured ? . Invoke ( expectedVersion ) ;
621646 EvictOldestSessions ( snapshot ) ;
622647 var json = JsonSerializer . Serialize ( snapshot , CacheJsonOptions ) ;
623648 lock ( _attachmentSaveGate )
624649 {
650+ lock ( _gate )
651+ {
652+ if ( expectedVersion != _attachmentSaveVersion )
653+ return ;
654+ }
655+
625656 AtomicWrite ( _attachmentCacheFilePath , json , "attachment metadata" ) ;
657+ lock ( _gate )
658+ {
659+ if ( expectedVersion == _attachmentSaveVersion )
660+ _attachmentCacheDirty = false ;
661+ }
626662 }
627663 }
628664 catch ( Exception ex )
0 commit comments