@@ -727,6 +727,63 @@ describe("runDoctorSessionSqlite", () => {
727727 ) . toHaveLength ( 2 ) ;
728728 } ) ;
729729
730+ it ( "uses target-bounded validation reads for multi-session imports" , async ( ) => {
731+ const countTargetReads = async ( sessionCount : number ) => {
732+ const store = createLegacyStore ( ) ;
733+ const sessions = Object . fromEntries (
734+ Array . from ( { length : sessionCount } , ( _ , offset ) => {
735+ const index = offset + 1 ;
736+ return [
737+ index === 1 ? "agent:main:main" : `agent:main:session-${ index } ` ,
738+ {
739+ sessionFile : `session-${ index } .jsonl` ,
740+ sessionId : `session-${ index } ` ,
741+ updatedAt : 2000 + index ,
742+ } ,
743+ ] ;
744+ } ) ,
745+ ) ;
746+ fs . writeFileSync ( store . storePath , `${ JSON . stringify ( sessions ) } \n` , { mode : 0o600 } ) ;
747+ for ( let index = 2 ; index <= sessionCount ; index += 1 ) {
748+ fs . writeFileSync (
749+ path . join ( store . sessionDir , `session-${ index } .jsonl` ) ,
750+ `{"type":"session","sessionId":"session-${ index } "}\n{"type":"event","id":"evt-${ index } "}\n` ,
751+ { mode : 0o600 } ,
752+ ) ;
753+ }
754+
755+ const sqlitePath = path . resolve (
756+ resolveTargetSqlitePath ( { agentId : "main" , storePath : store . storePath } ) ,
757+ ) ;
758+ const openSqlite = vi . spyOn ( nodeSqlite , "openNodeSqliteDatabase" ) ;
759+ try {
760+ const report = await runDoctorSessionSqlite ( {
761+ env : store . env ,
762+ mode : "import" ,
763+ store : store . storePath ,
764+ } ) ;
765+ expect ( report . totals ) . toMatchObject ( {
766+ importedEntries : sessionCount ,
767+ importedTranscriptEvents : sessionCount * 2 ,
768+ issues : 0 ,
769+ sqliteEntries : sessionCount ,
770+ } ) ;
771+ return openSqlite . mock . calls . filter (
772+ ( [ location , options ] ) =>
773+ path . resolve ( location ) === sqlitePath && options ?. readOnly === true ,
774+ ) . length ;
775+ } finally {
776+ openSqlite . mockRestore ( ) ;
777+ }
778+ } ;
779+
780+ const singleSessionReads = await countTargetReads ( 1 ) ;
781+ const multiSessionReads = await countTargetReads ( 3 ) ;
782+
783+ expect ( singleSessionReads ) . toBeGreaterThan ( 0 ) ;
784+ expect ( multiSessionReads ) . toBe ( singleSessionReads ) ;
785+ } ) ;
786+
730787 it ( "archives legacy stores with valid sessions and invalid cron stubs without failing" , async ( ) => {
731788 const store = createLegacyStore ( ) ;
732789 const legacyStore = JSON . parse ( fs . readFileSync ( store . storePath , "utf-8" ) ) as Record <
0 commit comments