@@ -25,11 +25,14 @@ import { compactDoctorSessionSqliteTarget } from "./doctor-session-sqlite-compac
2525import {
2626 createSessionSqliteMigrationRun ,
2727 recordCompletedMigrationMove ,
28+ recordCompletedMigrationMoves ,
2829 recordPlannedMigrationMove ,
30+ recordPlannedMigrationMoves ,
2931 updateMigrationManifestTarget ,
3032 writeSessionSqliteMigrationFailureReports ,
3133 writeSessionSqliteMigrationManifest ,
3234 type ActiveSessionSqliteMigrationRun ,
35+ type SessionSqliteMigrationMove ,
3336 type SessionSqliteMigrationMoveKind ,
3437 type SessionSqliteMigrationTargetInput ,
3538} from "./doctor-session-sqlite-migration-run.js" ;
@@ -627,25 +630,46 @@ function archiveUnreferencedJsonlFiles(
627630 referencedPaths : readonly string [ ] ,
628631 activeRun : ActiveSessionSqliteMigrationRun | undefined ,
629632) : void {
630- for ( const sourcePath of listUnreferencedJsonlFiles ( target . storePath , referencedPaths ) ) {
631- try {
632- report . archivedUnreferencedJsonlFiles . push (
633- moveSessionJsonlToArchive ( {
634- activeRun ,
633+ const reservedArchivePaths = new Set < string > ( ) ;
634+ const plannedMoves = listUnreferencedJsonlFiles ( target . storePath , referencedPaths ) . flatMap (
635+ ( sourcePath ) => {
636+ try {
637+ const move = planSessionJsonlArchiveMove ( {
635638 archiveKey : "archive-tier" ,
636639 baseNameRaw : path . basename ( sourcePath ) ,
637640 kind : "unreferenced-jsonl" ,
641+ reservedArchivePaths,
638642 sourcePathRaw : sourcePath ,
639643 target,
640- } ) ,
641- ) ;
644+ } ) ;
645+ reservedArchivePaths . add ( move . archivePath ) ;
646+ return [ move ] ;
647+ } catch ( err ) {
648+ report . issues . push ( {
649+ code : "unreferenced_jsonl_archive_failed" ,
650+ message : `${ sourcePath } : ${ String ( err ) } ` ,
651+ } ) ;
652+ return [ ] ;
653+ }
654+ } ,
655+ ) ;
656+ // Persist every source/destination before the first rename. A crash can then
657+ // restore moved files even when the completion checkpoint was never written.
658+ recordPlannedMigrationMoves ( activeRun , createMigrationTargetInput ( target ) , plannedMoves ) ;
659+ const completedMoves : SessionSqliteMigrationMove [ ] = [ ] ;
660+ for ( const move of plannedMoves ) {
661+ try {
662+ fs . renameSync ( move . sourcePath , move . archivePath ) ;
663+ report . archivedUnreferencedJsonlFiles . push ( move . archivePath ) ;
664+ completedMoves . push ( move ) ;
642665 } catch ( err ) {
643666 report . issues . push ( {
644667 code : "unreferenced_jsonl_archive_failed" ,
645- message : `${ sourcePath } : ${ String ( err ) } ` ,
668+ message : `${ move . sourcePath } : ${ String ( err ) } ` ,
646669 } ) ;
647670 }
648671 }
672+ recordCompletedMigrationMoves ( activeRun , createMigrationTargetInput ( target ) , completedMoves ) ;
649673}
650674
651675function archiveImportedLegacySessionStores (
@@ -1035,48 +1059,48 @@ function moveSessionJsonlToArchive(params: {
10351059 sourcePathRaw : string ;
10361060 target : SessionStoreTarget ;
10371061} ) : string {
1038- const { archiveKey, baseNameRaw, sourcePathRaw } = params ;
1039- const sourcePath = path . resolve ( sourcePathRaw ) ;
1062+ const move = planSessionJsonlArchiveMove ( params ) ;
1063+ recordPlannedMigrationMove ( params . activeRun , createMigrationTargetInput ( params . target ) , move ) ;
1064+ fs . renameSync ( move . sourcePath , move . archivePath ) ;
1065+ recordCompletedMigrationMove ( params . activeRun , createMigrationTargetInput ( params . target ) , move ) ;
1066+ return move . archivePath ;
1067+ }
1068+
1069+ function planSessionJsonlArchiveMove ( params : {
1070+ archiveKey : string ;
1071+ baseNameRaw : string ;
1072+ kind : SessionSqliteMigrationMoveKind ;
1073+ reservedArchivePaths ?: ReadonlySet < string > ;
1074+ sessionKey ?: string ;
1075+ sourcePathRaw : string ;
1076+ target : SessionStoreTarget ;
1077+ } ) : SessionSqliteMigrationMove {
1078+ const sourcePath = path . resolve ( params . sourcePathRaw ) ;
10401079 const stat = fs . statSync ( sourcePath ) ;
10411080 if ( ! stat . isFile ( ) ) {
10421081 throw new Error ( "source is not a regular file" ) ;
10431082 }
10441083 const archiveDir = resolveImportedTranscriptArchiveDir ( params . target . storePath ) ;
10451084 fs . mkdirSync ( archiveDir , { recursive : true } ) ;
1046- const baseName = baseNameRaw . replace ( / [ ^ A - Z a - z 0 - 9 _ . - ] + / g, "_" ) . slice ( 0 , 160 ) || "artifact" ;
1047- const keySlug = archiveKey . replace ( / [ ^ A - Z a - z 0 - 9 _ . - ] + / g, "_" ) . slice ( 0 , 120 ) || "session" ;
1085+ const baseName = params . baseNameRaw . replace ( / [ ^ A - Z a - z 0 - 9 _ . - ] + / g, "_" ) . slice ( 0 , 160 ) || "artifact" ;
1086+ const keySlug = params . archiveKey . replace ( / [ ^ A - Z a - z 0 - 9 _ . - ] + / g, "_" ) . slice ( 0 , 120 ) || "session" ;
10481087 for ( let attempt = 0 ; attempt < 100 ; attempt += 1 ) {
10491088 const suffix = attempt === 0 ? "" : `.${ attempt } ` ;
10501089 const archivePath = path . join (
10511090 archiveDir ,
10521091 `${ keySlug } .${ baseName } .imported-${ Date . now ( ) } ${ suffix } ` ,
10531092 ) ;
1054- if ( fs . existsSync ( archivePath ) ) {
1093+ if ( fs . existsSync ( archivePath ) || params . reservedArchivePaths ?. has ( archivePath ) ) {
10551094 continue ;
10561095 }
1057- try {
1058- const move = {
1059- archivePath,
1060- kind : params . kind ,
1061- ...( params . sessionKey ? { sessionKey : params . sessionKey } : { } ) ,
1062- sourcePath,
1063- } ;
1064- recordPlannedMigrationMove ( params . activeRun , createMigrationTargetInput ( params . target ) , move ) ;
1065- fs . renameSync ( sourcePath , archivePath ) ;
1066- recordCompletedMigrationMove (
1067- params . activeRun ,
1068- createMigrationTargetInput ( params . target ) ,
1069- move ,
1070- ) ;
1071- return archivePath ;
1072- } catch ( err ) {
1073- if ( ( err as { code ?: unknown } ) ?. code === "EEXIST" ) {
1074- continue ;
1075- }
1076- throw err ;
1077- }
1078- }
1079- throw new Error ( `Could not archive ${ baseName } for ${ archiveKey } ` ) ;
1096+ return {
1097+ archivePath,
1098+ kind : params . kind ,
1099+ ...( params . sessionKey ? { sessionKey : params . sessionKey } : { } ) ,
1100+ sourcePath,
1101+ } ;
1102+ }
1103+ throw new Error ( `Could not archive ${ baseName } for ${ params . archiveKey } ` ) ;
10801104}
10811105
10821106function resolveImportedTranscriptArchiveDir ( storePath : string ) : string {
0 commit comments