@@ -423,27 +423,21 @@ function extractFirstUserMessageFromTranscriptChunk(
423423 return null ;
424424}
425425
426- export function readFirstUserMessageFromTranscript (
426+ function findExistingTranscriptPath (
427427 sessionId : string ,
428428 storePath : string | undefined ,
429429 sessionFile ?: string ,
430430 agentId ?: string ,
431- opts ?: { includeInterSession ?: boolean } ,
432431) : string | null {
433432 const candidates = resolveSessionTranscriptCandidates ( sessionId , storePath , sessionFile , agentId ) ;
434- const filePath = candidates . find ( ( p ) => fs . existsSync ( p ) ) ;
435- if ( ! filePath ) {
436- return null ;
437- }
433+ return candidates . find ( ( p ) => fs . existsSync ( p ) ) ?? null ;
434+ }
438435
436+ function withOpenTranscriptFd < T > ( filePath : string , read : ( fd : number ) => T | null ) : T | null {
439437 let fd : number | null = null ;
440438 try {
441439 fd = fs . openSync ( filePath , "r" ) ;
442- const chunk = readTranscriptHeadChunk ( fd ) ;
443- if ( ! chunk ) {
444- return null ;
445- }
446- return extractFirstUserMessageFromTranscriptChunk ( chunk , opts ) ;
440+ return read ( fd ) ;
447441 } catch {
448442 // file read error
449443 } finally {
@@ -454,6 +448,27 @@ export function readFirstUserMessageFromTranscript(
454448 return null ;
455449}
456450
451+ export function readFirstUserMessageFromTranscript (
452+ sessionId : string ,
453+ storePath : string | undefined ,
454+ sessionFile ?: string ,
455+ agentId ?: string ,
456+ opts ?: { includeInterSession ?: boolean } ,
457+ ) : string | null {
458+ const filePath = findExistingTranscriptPath ( sessionId , storePath , sessionFile , agentId ) ;
459+ if ( ! filePath ) {
460+ return null ;
461+ }
462+
463+ return withOpenTranscriptFd ( filePath , ( fd ) => {
464+ const chunk = readTranscriptHeadChunk ( fd ) ;
465+ if ( ! chunk ) {
466+ return null ;
467+ }
468+ return extractFirstUserMessageFromTranscriptChunk ( chunk , opts ) ;
469+ } ) ;
470+ }
471+
457472const LAST_MSG_MAX_BYTES = 16384 ;
458473const LAST_MSG_MAX_LINES = 20 ;
459474
@@ -495,29 +510,19 @@ export function readLastMessagePreviewFromTranscript(
495510 sessionFile ?: string ,
496511 agentId ?: string ,
497512) : string | null {
498- const candidates = resolveSessionTranscriptCandidates ( sessionId , storePath , sessionFile , agentId ) ;
499- const filePath = candidates . find ( ( p ) => fs . existsSync ( p ) ) ;
513+ const filePath = findExistingTranscriptPath ( sessionId , storePath , sessionFile , agentId ) ;
500514 if ( ! filePath ) {
501515 return null ;
502516 }
503517
504- let fd : number | null = null ;
505- try {
506- fd = fs . openSync ( filePath , "r" ) ;
518+ return withOpenTranscriptFd ( filePath , ( fd ) => {
507519 const stat = fs . fstatSync ( fd ) ;
508520 const size = stat . size ;
509521 if ( size === 0 ) {
510522 return null ;
511523 }
512524 return readLastMessagePreviewFromOpenTranscript ( { fd, size } ) ;
513- } catch {
514- // file error
515- } finally {
516- if ( fd !== null ) {
517- fs . closeSync ( fd ) ;
518- }
519- }
520- return null ;
525+ } ) ;
521526}
522527
523528const PREVIEW_READ_SIZES = [ 64 * 1024 , 256 * 1024 , 1024 * 1024 ] ;
0 commit comments