@@ -114,6 +114,7 @@ interface StoreState {
114114 readonly pending : Array < PendingRecord > ;
115115 readonly pendingBytes : number ;
116116 readonly sinks : ReadonlyMap < string , AsyncFileSink > ;
117+ readonly writtenSinceRetention : ReadonlySet < string > ;
117118 readonly flushScheduled : boolean ;
118119 readonly closed : boolean ;
119120 readonly lastRetentionAt : number ;
@@ -483,6 +484,7 @@ const drainPending = Effect.fnUntraced(function* (input: {
483484 }
484485
485486 const sinks = new Map ( input . state . sinks ) ;
487+ const writtenSegments = new Set ( input . state . writtenSinceRetention ) ;
486488 const failures : Array < FileOperationFailure > = [ ] ;
487489 const attributionByStream = new Map <
488490 EventNdjsonStream ,
@@ -508,6 +510,7 @@ const drainPending = Effect.fnUntraced(function* (input: {
508510 sink = created . success ;
509511 sinks . set ( threadSegment , sink ) ;
510512 }
513+ writtenSegments . add ( threadSegment ) ;
511514
512515 const written = yield * writeBatchedMessages (
513516 sink ,
@@ -534,6 +537,13 @@ const drainPending = Effect.fnUntraced(function* (input: {
534537
535538 const retentionDue =
536539 input . now - input . state . lastRetentionAt >= input . options . retentionCheckIntervalMs ;
540+ if ( retentionDue ) {
541+ // A sink idle for a whole retention interval reopens from disk on its next
542+ // write, so retention may remove its files without leaving a stale size.
543+ for ( const threadSegment of sinks . keys ( ) ) {
544+ if ( ! writtenSegments . has ( threadSegment ) ) sinks . delete ( threadSegment ) ;
545+ }
546+ }
537547 const retention = retentionDue
538548 ? yield * Effect . promise ( ( ) =>
539549 enforceRetention ( {
@@ -563,6 +573,7 @@ const drainPending = Effect.fnUntraced(function* (input: {
563573 pending : [ ] ,
564574 pendingBytes : 0 ,
565575 sinks,
576+ writtenSinceRetention : retentionDue ? new Set ( ) : writtenSegments ,
566577 flushScheduled : input . timerFired ? false : input . state . flushScheduled ,
567578 closed : input . close ,
568579 lastRetentionAt : retentionDue ? input . now : input . state . lastRetentionAt ,
@@ -629,6 +640,7 @@ export const makeEventNdjsonLogStore = Effect.fnUntraced(function* (
629640 pending : [ ] ,
630641 pendingBytes : 0 ,
631642 sinks : new Map ( ) ,
643+ writtenSinceRetention : new Set ( ) ,
632644 flushScheduled : false ,
633645 closed : false ,
634646 lastRetentionAt : initializedAt ,
0 commit comments