@@ -17,10 +17,10 @@ const SEGMENT_MAX_BYTES = 5 * 1024 * 1024 // 5 MiB
1717const TOTAL_MAX_BYTES = 100 * 1024 * 1024 // 100 MiB
1818
1919/** Segment file name prefix */
20- const SEGMENT_PREFIX = "events-"
20+ export const SEGMENT_PREFIX = "events-"
2121
2222/** Segment file extension */
23- const SEGMENT_EXT = ".ndjson"
23+ export const SEGMENT_EXT = ".ndjson"
2424
2525/** Manifest file name */
2626const MANIFEST_FILENAME = "manifest.json"
@@ -95,7 +95,7 @@ export interface QuarantineReportEntry {
9595 segment : string
9696 /** 1-based line number */
9797 line : number
98- /** SHA-256 hash of the corrupt line content (first 16 chars) */
98+ /** 32-bit rolling hash of the corrupt line content as an 8-character hex string */
9999 hash : string
100100 /** Discovery time (ISO 8601 UTC) */
101101 at : string
@@ -247,13 +247,18 @@ export class UsageEventStore {
247247 // Incremental cache update: only after durable success.
248248 if ( result && this . cachedEvents ) {
249249 // If a segment rotation happened during the append, the cached
250- // segment count no longer matches the manifest . Invalidate so the
250+ // segment count no longer matches the disk files . Invalidate so the
251251 // next readAll rescans from disk instead of returning stale data.
252+ const currentSegmentFiles = await this . listSegmentFiles ( )
252253 const manifest = await this . loadOrCreateManifest ( )
253- if ( this . cachedSegmentCount !== manifest . currentSegment ) {
254+ if ( this . cachedSegmentCount !== currentSegmentFiles . length ) {
254255 this . invalidateCache ( )
255256 } else {
256257 this . cachedEvents . push ( event )
258+ const activeSegmentPath = this . getSegmentPath ( manifest . currentSegment )
259+ const activeStat = await fs . stat ( activeSegmentPath ) . catch ( ( ) => null )
260+ this . cachedActiveSegmentSize = activeStat ?. size ?? - 1
261+ this . cachedActiveSegmentMtimeMs = activeStat ?. mtimeMs ?? - 1
257262 }
258263 }
259264 resolveFn ( result )
@@ -299,7 +304,7 @@ export class UsageEventStore {
299304 this . cachedActiveSegmentSize === activeSize &&
300305 this . cachedActiveSegmentMtimeMs === activeMtimeMs
301306 ) {
302- return this . cachedEvents
307+ return [ ... this . cachedEvents ]
303308 }
304309
305310 // Single-flight cold load: concurrent callers share one scan.
@@ -313,7 +318,7 @@ export class UsageEventStore {
313318 this . cachedSegmentCount = currentSegmentFiles . length
314319 this . cachedActiveSegmentSize = activeSize
315320 this . cachedActiveSegmentMtimeMs = activeMtimeMs
316- return events
321+ return [ ... events ]
317322 } )
318323
319324 try {
@@ -344,7 +349,7 @@ export class UsageEventStore {
344349 }
345350
346351 try {
347- const manifest = await this . loadOrCreateManifest ( )
352+ const manifest = await this . loadOrCreateManifest ( true )
348353
349354 // New generation number
350355 const newGeneration = manifest . generation + 1
@@ -456,31 +461,26 @@ export class UsageEventStore {
456461
457462 for ( const segmentFile of segmentFiles ) {
458463 const segmentPath = path . join ( this . statsDir , segmentFile )
459- let content : string
460464
465+ let content : string
461466 try {
462467 content = await fs . readFile ( segmentPath , "utf-8" )
463468 } catch ( err ) {
464- // Skip file read failures (ENOENT etc.)
465- if ( ( err as NodeJS . ErrnoException ) . code !== "ENOENT" ) {
466- console . warn ( `[UsageEventStore] failed to read segment ${ segmentFile } :` , err )
467- }
468- continue
469+ throw new StatsStoreError ( "STATS_STORE/readAll/002" , `Failed to read segment file: ${ segmentPath } ` , err )
469470 }
470471
471472 const lines = content . split ( "\n" )
472- // Remove the last empty line (trailing newline)
473+ // Remove the last element if it's empty (trailing newline)
473474 if ( lines . length > 0 && lines [ lines . length - 1 ] === "" ) {
474475 lines . pop ( )
475476 }
476477
477- // If the last line is unterminated/truncated, treat it as a crash tail and ignore
478- // (If the last line is valid JSON, it is parsed; otherwise it goes to quarantine)
479478 for ( let i = 0 ; i < lines . length ; i ++ ) {
480- const lineNum = i + 1
481479 const line = lines [ i ]
480+ const lineNum = i + 1
482481 const isLastLine = i === lines . length - 1
483482
483+ // Skip empty lines
484484 if ( ! line . trim ( ) ) {
485485 continue
486486 }
@@ -491,11 +491,13 @@ export class UsageEventStore {
491491 if ( result . success ) {
492492 events . push ( result . data )
493493 } else {
494- // zod validation failed: corrupt line
495- quarantineEntries . push ( this . makeQuarantineEntry ( segmentFile , lineNum , line ) )
496- // Validation failure of the last line may be a crash tail, so exclude from quarantine
497- if ( isLastLine ) {
498- quarantineEntries . pop ( )
494+ // Schema validation failed
495+ // If it's the last line, it might be an unterminated/truncated crash tail.
496+ // The architecture report states: "The last unterminated line is treated
497+ // as a crash tail and ignored; parse failures before EOF are recorded to
498+ // the quarantine report and skipped."
499+ if ( ! isLastLine ) {
500+ quarantineEntries . push ( this . makeQuarantineEntry ( segmentFile , lineNum , line ) )
499501 }
500502 }
501503 } catch {
@@ -560,7 +562,7 @@ export class UsageEventStore {
560562 }
561563
562564 try {
563- const manifest = await this . loadOrCreateManifest ( )
565+ const manifest = await this . loadOrCreateManifest ( true )
564566 const segmentPath = this . getSegmentPath ( manifest . currentSegment )
565567
566568 // Check if the segment file exists and its size
@@ -628,16 +630,26 @@ export class UsageEventStore {
628630 // ── Internal: Manifest ──────────────────────────────────────────────────
629631
630632 private manifest : UsageStatsManifest | null = null
633+ private manifestMtimeMs = - 1
631634
632635 /**
633636 * Loads the manifest or creates it with default values.
637+ * @param forceReload When true, bypasses the in-memory cached manifest and re-reads from disk.
634638 */
635- private async loadOrCreateManifest ( ) : Promise < UsageStatsManifest > {
636- if ( this . manifest ) {
637- return this . manifest
639+ private async loadOrCreateManifest ( forceReload = false ) : Promise < UsageStatsManifest > {
640+ if ( ! forceReload && this . manifest ) {
641+ try {
642+ const stat = await fs . stat ( this . manifestPath )
643+ if ( stat . mtimeMs === this . manifestMtimeMs ) {
644+ return this . manifest
645+ }
646+ } catch {
647+ // file stat failed, fall through to re-read or create
648+ }
638649 }
639650
640651 try {
652+ const stat = await fs . stat ( this . manifestPath ) . catch ( ( ) => null )
641653 const content = await fs . readFile ( this . manifestPath , "utf-8" )
642654 const parsed = JSON . parse ( content )
643655 // Basic field validation
@@ -647,6 +659,7 @@ export class UsageEventStore {
647659 typeof parsed . currentSegment === "number"
648660 ) {
649661 this . manifest = parsed as UsageStatsManifest
662+ this . manifestMtimeMs = stat ?. mtimeMs ?? - 1
650663 return this . manifest
651664 }
652665 // On validation failure, overwrite with default values
@@ -664,6 +677,7 @@ export class UsageEventStore {
664677 console . warn ( `[UsageEventStore] failed to load manifest, using default:` , err )
665678 const defaultManifest = { ...DEFAULT_MANIFEST , updatedAt : new Date ( ) . toISOString ( ) }
666679 this . manifest = defaultManifest
680+ this . manifestMtimeMs = - 1
667681 return defaultManifest
668682 }
669683 }
0 commit comments