@@ -29,9 +29,9 @@ import {
2929} from "../../usage/log" ;
3030import {
3131 HISTORY_DDL ,
32- HISTORY_DB_FILENAME ,
3332 HISTORY_META_KEYS ,
3433 HISTORY_SCHEMA_VERSION ,
34+ historyIndexPath ,
3535} from "./schema" ;
3636import {
3737 decodeHistoryCursor ,
@@ -83,8 +83,9 @@ let openPromise: Promise<RequestHistoryIndexMeta> | null = null;
8383
8484function indexDbPath ( ) : string {
8585 const dir = getConfigDir ( ) ;
86- recordOwnedConfigPath ( dir , `${ dir } /${ HISTORY_DB_FILENAME } ` ) ;
87- return `${ dir } /${ HISTORY_DB_FILENAME } ` ;
86+ const path = historyIndexPath ( dir ) ;
87+ recordOwnedConfigPath ( dir , path ) ;
88+ return path ;
8889}
8990
9091function metaValue ( dbHandle : Database , key : string ) : string | null {
@@ -125,11 +126,22 @@ function sourceIdentity(): UsageLogRevision | null {
125126 return currentUsageLogRevision ( ) ;
126127}
127128
129+ function readStoredMetaField ( dbHandle : Database , key : string ) : string {
130+ return metaValue ( dbHandle , key ) ?? "" ;
131+ }
132+
128133function sourceIdentityMatches ( dbHandle : Database , revision : UsageLogRevision | null ) : boolean {
129134 const stored = readIndexedMeta ( dbHandle ) ;
130135 if ( revision === null ) return stored . sourceSize === 0 ;
131- return stored . sourceSize === Number ( revision . size )
132- && stored . sourceMtimeMs === Number ( revision . mtimeMs ) ;
136+ const storedPath = readStoredMetaField ( dbHandle , HISTORY_META_KEYS . sourcePath ) ;
137+ if ( ! storedPath ) return false ;
138+ const storedDev = Number ( readStoredMetaField ( dbHandle , HISTORY_META_KEYS . sourceDev ) ) ;
139+ const storedIno = Number ( readStoredMetaField ( dbHandle , HISTORY_META_KEYS . sourceIno ) ) ;
140+ const storedBirthtimeMs = Number ( readStoredMetaField ( dbHandle , HISTORY_META_KEYS . sourceBirthtimeMs ) ) ;
141+ return storedPath === revision . path
142+ && storedDev === Number ( revision . dev )
143+ && storedIno === Number ( revision . ino )
144+ && storedBirthtimeMs === Number ( revision . birthtimeMs ) ;
133145}
134146
135147/** Extract the `requests` row columns from a canonical persisted entry. */
@@ -301,6 +313,9 @@ function destroyAndRecreate(path: string, reason: string): Database {
301313 // the file briefly after the throw. Retry the unlink before recreating.
302314 for ( let attempt = 0 ; attempt < 5 ; attempt ++ ) {
303315 try {
316+ for ( const suffix of [ "-wal" , "-shm" ] as const ) {
317+ try { unlinkSync ( `${ path } ${ suffix } ` ) ; } catch { /* sidecar may not exist */ }
318+ }
304319 unlinkSync ( path ) ;
305320 break ;
306321 } catch {
@@ -449,12 +464,18 @@ export async function rebuildRequestHistoryIndex(): Promise<RequestHistoryIndexM
449464 return metaFor ( handle ) ;
450465}
451466
467+ type HistoryQueryRow = {
468+ row_json : string ;
469+ timestamp ?: number ;
470+ request_id ?: string ;
471+ } ;
472+
452473function queryRows (
453474 handle : Database ,
454475 filters : RequestHistoryFilters ,
455476 cursor : HistoryCursor | null ,
456477 limit : number ,
457- ) : { rows : Array < { row_json : string } > ; total : number } {
478+ ) : { rows : Array < HistoryQueryRow > ; total : number } {
458479 const where : string [ ] = [ ] ;
459480 const values : Array < string | number > = [ ] ;
460481 const add = ( clause : string , value : string | number ) => {
@@ -479,12 +500,18 @@ function queryRows(
479500 }
480501 const whereSql = where . length > 0 ? ` WHERE ${ where . join ( " AND " ) } ` : "" ;
481502 const rows = handle . query (
482- `SELECT row_json FROM requests${ whereSql } ORDER BY timestamp DESC, request_id DESC LIMIT ?` ,
483- ) . all ( ...values , limit + 1 ) as Array < { row_json : string } > ;
503+ `SELECT row_json, timestamp, request_id FROM requests${ whereSql } ORDER BY timestamp DESC, request_id DESC LIMIT ?` ,
504+ ) . all ( ...values , limit + 1 ) as Array < HistoryQueryRow > ;
484505 return { rows, total : rows . length } ;
485506}
486507
487- function hydrateRow ( row : { row_json : string } | undefined ) : PersistedUsageEntry | null {
508+ function requireDb ( ) : Database {
509+ const handle = db ;
510+ if ( ! handle ) throw new Error ( "request-history index is not open" ) ;
511+ return handle ;
512+ }
513+
514+ function hydrateRow ( row : HistoryQueryRow | undefined ) : PersistedUsageEntry | null {
488515 if ( ! row ) return null ;
489516 try {
490517 const parsed = JSON . parse ( row . row_json ) as PersistedUsageEntry ;
@@ -511,7 +538,7 @@ export async function queryRequestHistory(
511538 Math . max ( 1 , Math . trunc ( pageSize ?? REQUEST_HISTORY_DEFAULT_PAGE_SIZE ) ) ,
512539 REQUEST_HISTORY_MAX_PAGE_SIZE ,
513540 ) ;
514- const handle = db ! ;
541+ const handle = requireDb ( ) ;
515542 const { rows, total } = queryRows ( handle , filters , cursor , limit ) ;
516543 const hasMore = total > limit ;
517544 const pageRows = rows . slice ( 0 , limit ) ;
@@ -523,15 +550,14 @@ export async function queryRequestHistory(
523550 let nextCursor : string | undefined ;
524551 if ( hasMore && pageRows . length > 0 ) {
525552 const last = pageRows [ pageRows . length - 1 ] ! ;
526- const parsed = JSON . parse ( last . row_json ) as PersistedUsageEntry ;
527- nextCursor = encodeHistoryCursor ( { t : parsed . timestamp , i : parsed . requestId } ) ;
553+ nextCursor = encodeHistoryCursor ( { t : last . timestamp ! , i : last . request_id ! } ) ;
528554 }
529555 return { rows : entries , ...( nextCursor ? { nextCursor } : { } ) , hasMore, meta } ;
530556}
531557
532558export async function requestHistoryRowById ( requestId : string ) : Promise < PersistedUsageEntry | null > {
533559 await openRequestHistoryIndex ( ) ;
534- const handle = db ! ;
560+ const handle = requireDb ( ) ;
535561 const row = handle . query ( "SELECT row_json FROM requests WHERE request_id = ?" ) . get ( requestId ) as
536562 | { row_json : string }
537563 | undefined ;
0 commit comments