@@ -71,6 +71,7 @@ export type ActiveSessionSqliteMigrationRun = {
7171
7272const SESSION_SQLITE_MIGRATION_RUNS_DIR = "session-sqlite-migration-runs" ;
7373const COMPLETED_MIGRATION_RUN_RETENTION = 50 ;
74+ const RESTORE_ARCHIVE_HASH_CHUNK_BYTES = 64 * 1024 ;
7475const AbsolutePathSchema = z
7576 . string ( )
7677 . min ( 1 )
@@ -548,6 +549,21 @@ type RestoreArchiveInspection =
548549 | { state : "invalid" ; reason : string }
549550 | { state : "missing" } ;
550551
552+ function hashRestoreArchive ( fd : number , size : number ) : string {
553+ const hash = createHash ( "sha256" ) ;
554+ const buffer = Buffer . allocUnsafe ( RESTORE_ARCHIVE_HASH_CHUNK_BYTES ) ;
555+ let offset = 0 ;
556+ while ( offset < size ) {
557+ const read = fs . readSync ( fd , buffer , 0 , Math . min ( buffer . length , size - offset ) , offset ) ;
558+ if ( read === 0 ) {
559+ throw new Error ( "archive changed while it was inspected" ) ;
560+ }
561+ hash . update ( buffer . subarray ( 0 , read ) ) ;
562+ offset += read ;
563+ }
564+ return hash . digest ( "hex" ) ;
565+ }
566+
551567function inspectRestoreArchive ( move : SessionSqliteMigrationMove ) : RestoreArchiveInspection {
552568 if ( hasSymbolicLinkInDirectoryPath ( path . dirname ( move . archivePath ) ) ) {
553569 return { state : "invalid" , reason : "archive parent is a symbolic link; refusing restore" } ;
@@ -583,20 +599,11 @@ function inspectRestoreArchive(move: SessionSqliteMigrationMove): RestoreArchive
583599 reason : "archive changed while it was inspected; refusing restore" ,
584600 } ;
585601 }
586- const content = readFileDescriptorBoundedSync ( fd , descriptorStat . size ) ;
587- const finalPathStat = fs . lstatSync ( move . archivePath ) ;
588- if (
589- finalPathStat . dev !== descriptorStat . dev ||
590- finalPathStat . ino !== descriptorStat . ino ||
591- finalPathStat . size !== descriptorStat . size
592- ) {
593- return {
594- state : "invalid" ,
595- reason : "archive changed while it was inspected; refusing restore" ,
596- } ;
597- }
602+ let digest : string ;
598603 let legacyEntryCount : number | undefined ;
599604 if ( move . kind === "legacy-store" ) {
605+ const content = readFileDescriptorBoundedSync ( fd , descriptorStat . size ) ;
606+ digest = createHash ( "sha256" ) . update ( content ) . digest ( "hex" ) ;
600607 let parsed : unknown ;
601608 try {
602609 parsed = JSON . parse ( content . toString ( "utf-8" ) ) ;
@@ -613,11 +620,26 @@ function inspectRestoreArchive(move: SessionSqliteMigrationMove): RestoreArchive
613620 } ;
614621 }
615622 legacyEntryCount = Object . keys ( parsed ) . length ;
623+ } else {
624+ // Transcript-like archives can be arbitrarily large. Hash them incrementally so duplicate
625+ // planning cannot turn a Doctor restore into a synchronous whole-file allocation.
626+ digest = hashRestoreArchive ( fd , descriptorStat . size ) ;
627+ }
628+ const finalPathStat = fs . lstatSync ( move . archivePath ) ;
629+ if (
630+ finalPathStat . dev !== descriptorStat . dev ||
631+ finalPathStat . ino !== descriptorStat . ino ||
632+ finalPathStat . size !== descriptorStat . size
633+ ) {
634+ return {
635+ state : "invalid" ,
636+ reason : "archive changed while it was inspected; refusing restore" ,
637+ } ;
616638 }
617639 return {
618640 state : "available" ,
619641 snapshot : {
620- digest : createHash ( "sha256" ) . update ( content ) . digest ( "hex" ) ,
642+ digest,
621643 ...( legacyEntryCount === undefined ? { } : { legacyEntryCount } ) ,
622644 size : descriptorStat . size ,
623645 } ,
0 commit comments