@@ -127,6 +127,8 @@ function createConfigObserveAuditRecord(params: {
127127 clobberedPath : string | null ;
128128 restoredFromBackup : boolean ;
129129 restoredBackupPath : string | null ;
130+ restoreErrorCode ?: string | null ;
131+ restoreErrorMessage ?: string | null ;
130132} ) : ConfigObserveAuditRecord {
131133 return {
132134 ts : params . ts ,
@@ -175,6 +177,8 @@ function createConfigObserveAuditRecord(params: {
175177 clobberedPath : params . clobberedPath ,
176178 restoredFromBackup : params . restoredFromBackup ,
177179 restoredBackupPath : params . restoredBackupPath ,
180+ restoreErrorCode : params . restoreErrorCode ?? null ,
181+ restoreErrorMessage : params . restoreErrorMessage ?? null ,
178182 } ;
179183}
180184
@@ -192,6 +196,35 @@ function createConfigObserveAuditAppendParams(
192196 } ;
193197}
194198
199+ function extractRestoreErrorDetails ( error : unknown ) : {
200+ code : string | null ;
201+ message : string | null ;
202+ } {
203+ if ( ! error || typeof error !== "object" ) {
204+ return { code : null , message : typeof error === "string" ? error : null } ;
205+ }
206+ const code =
207+ "code" in error && typeof ( error as { code ?: unknown } ) . code === "string"
208+ ? ( error as { code : string } ) . code
209+ : null ;
210+ const message =
211+ "message" in error && typeof ( error as { message ?: unknown } ) . message === "string"
212+ ? ( error as { message : string } ) . message
213+ : null ;
214+ return { code, message } ;
215+ }
216+
217+ function createConfigObserveAnomalyAuditAppendParams (
218+ deps : ObserveRecoveryDeps ,
219+ params : Omit < ConfigObserveAuditRecordParams , "restoredFromBackup" | "restoredBackupPath" > ,
220+ ) {
221+ return createConfigObserveAuditAppendParams ( deps , {
222+ ...params ,
223+ restoredFromBackup : false ,
224+ restoredBackupPath : null ,
225+ } ) ;
226+ }
227+
195228function hashConfigRaw ( raw : string | null ) : string {
196229 return crypto
197230 . createHash ( "sha256" )
@@ -637,26 +670,43 @@ export async function maybeRecoverSuspiciousConfigRead(params: {
637670 } ) ;
638671
639672 let restoredFromBackup = false ;
673+ let restoreError : unknown ;
640674 try {
641675 await params . deps . fs . promises . copyFile ( backupPath , params . configPath ) ;
642676 restoredFromBackup = true ;
643- } catch { }
677+ } catch ( error ) {
678+ restoreError = error ;
679+ }
644680
645- params . deps . logger . warn (
646- `Config auto-restored from backup: ${ params . configPath } (${ suspicious . join ( ", " ) } )` ,
647- ) ;
681+ const restoreErrorDetails = restoredFromBackup
682+ ? { code : null , message : null }
683+ : extractRestoreErrorDetails ( restoreError ) ;
684+
685+ if ( restoredFromBackup ) {
686+ params . deps . logger . warn (
687+ `Config auto-restored from backup: ${ params . configPath } (${ suspicious . join ( ", " ) } )` ,
688+ ) ;
689+ } else {
690+ params . deps . logger . warn (
691+ `Config auto-restore from backup failed: ${ params . configPath } (${ suspicious . join ( ", " ) } )${
692+ restoreErrorDetails . message ? `: ${ restoreErrorDetails . message } ` : ""
693+ } `,
694+ ) ;
695+ }
648696 await appendConfigAuditRecord (
649697 createConfigObserveAuditAppendParams ( params . deps , {
650698 ts : now ,
651699 configPath : params . configPath ,
652- valid : true ,
700+ valid : restoredFromBackup ,
653701 current,
654702 suspicious,
655703 lastKnownGood : entry . lastKnownGood ,
656704 backup,
657705 clobberedPath,
658706 restoredFromBackup,
659707 restoredBackupPath : backupPath ,
708+ restoreErrorCode : restoreErrorDetails . code ,
709+ restoreErrorMessage : restoreErrorDetails . message ,
660710 } ) ,
661711 ) ;
662712
@@ -727,26 +777,43 @@ export function maybeRecoverSuspiciousConfigReadSync(params: {
727777 } ) ;
728778
729779 let restoredFromBackup = false ;
780+ let restoreError : unknown ;
730781 try {
731782 params . deps . fs . copyFileSync ( backupPath , params . configPath ) ;
732783 restoredFromBackup = true ;
733- } catch { }
784+ } catch ( error ) {
785+ restoreError = error ;
786+ }
734787
735- params . deps . logger . warn (
736- `Config auto-restored from backup: ${ params . configPath } (${ suspicious . join ( ", " ) } )` ,
737- ) ;
788+ const restoreErrorDetails = restoredFromBackup
789+ ? { code : null , message : null }
790+ : extractRestoreErrorDetails ( restoreError ) ;
791+
792+ if ( restoredFromBackup ) {
793+ params . deps . logger . warn (
794+ `Config auto-restored from backup: ${ params . configPath } (${ suspicious . join ( ", " ) } )` ,
795+ ) ;
796+ } else {
797+ params . deps . logger . warn (
798+ `Config auto-restore from backup failed: ${ params . configPath } (${ suspicious . join ( ", " ) } )${
799+ restoreErrorDetails . message ? `: ${ restoreErrorDetails . message } ` : ""
800+ } `,
801+ ) ;
802+ }
738803 appendConfigAuditRecordSync (
739804 createConfigObserveAuditAppendParams ( params . deps , {
740805 ts : now ,
741806 configPath : params . configPath ,
742- valid : true ,
807+ valid : restoredFromBackup ,
743808 current,
744809 suspicious,
745810 lastKnownGood : entry . lastKnownGood ,
746811 backup,
747812 clobberedPath,
748813 restoredFromBackup,
749814 restoredBackupPath : backupPath ,
815+ restoreErrorCode : restoreErrorDetails . code ,
816+ restoreErrorMessage : restoreErrorDetails . message ,
750817 } ) ,
751818 ) ;
752819
0 commit comments