@@ -7,6 +7,7 @@ import type { NativeProfileContext } from "./native-profile-store";
77
88const JOURNAL = ".opencodex-native-main-refresh.json" ;
99const TRANSACTION_ID = / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - 4 [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / ;
10+ let beforeRecoveryReplaceForTests : ( ( ) => void ) | null = null ;
1011
1112type Journal = {
1213 version : 1 ;
@@ -90,6 +91,22 @@ function cleanup(context: NativeProfileContext, journal: Journal): void {
9091 }
9192}
9293
94+ function settlePreparedReplacement (
95+ context : NativeProfileContext ,
96+ journal : Journal ,
97+ displacedPath : string ,
98+ rollbackPath : string ,
99+ ) : void {
100+ const displaced = readExact ( displacedPath ) ;
101+ if ( ! displaced ) throw new NativeMainRefreshPublicationError ( ) ;
102+ if ( digest ( displaced ) !== journal . expectedSha256 ) {
103+ restoreFilePreservingTarget ( displacedPath , journal . targetPath , rollbackPath ) ;
104+ const restored = readExact ( journal . targetPath ) ;
105+ if ( ! restored || ! restored . equals ( displaced ) ) throw new NativeMainRefreshPublicationError ( ) ;
106+ }
107+ cleanup ( context , journal ) ;
108+ }
109+
93110/** Recover only a transaction whose exact hashes prove one deterministic outcome. */
94111export function recoverNativeMainRefreshPublication ( context : NativeProfileContext ) : void {
95112 const path = journalPath ( context ) ;
@@ -103,31 +120,24 @@ export function recoverNativeMainRefreshPublication(context: NativeProfileContex
103120 const stagedBytes = readExact ( staged ) ;
104121 const displacedPath = process . platform === "win32" ? previous : staged ;
105122 const rollbackPath = process . platform === "win32" ? staged : previous ;
106- const displacedBytes = readExact ( displacedPath ) ;
107123 if ( ! canonical ) throw new NativeMainRefreshPublicationError ( ) ;
108124 if ( digest ( canonical ) === journal . expectedSha256 && stagedBytes && digest ( stagedBytes ) === journal . replacementSha256 ) {
109125 try {
126+ const hook = beforeRecoveryReplaceForTests ;
127+ beforeRecoveryReplaceForTests = null ;
128+ hook ?.( ) ;
110129 replaceFilePreservingTarget ( staged , journal . targetPath , previous ) ;
111- cleanup ( context , { ... journal , phase : "replaced" } ) ;
130+ settlePreparedReplacement ( context , journal , displacedPath , rollbackPath ) ;
112131 } catch ( cause ) {
113132 throw new NativeMainRefreshPublicationError ( { cause } ) ;
114133 }
115134 return ;
116135 }
117136 if ( digest ( canonical ) === journal . replacementSha256 ) {
118137 if ( journal . phase === "prepared" ) {
119- if ( ! displacedBytes ) throw new NativeMainRefreshPublicationError ( ) ;
120- if ( digest ( displacedBytes ) !== journal . expectedSha256 ) {
121- try {
122- restoreFilePreservingTarget ( displacedPath , journal . targetPath , rollbackPath ) ;
123- const restored = readExact ( journal . targetPath ) ;
124- if ( ! restored || ! restored . equals ( displacedBytes ) ) throw new NativeMainRefreshPublicationError ( ) ;
125- cleanup ( context , journal ) ;
126- } catch ( cause ) {
127- throw new NativeMainRefreshPublicationError ( { cause } ) ;
128- }
129- return ;
130- }
138+ try { settlePreparedReplacement ( context , journal , displacedPath , rollbackPath ) ; }
139+ catch ( cause ) { throw new NativeMainRefreshPublicationError ( { cause } ) ; }
140+ return ;
131141 }
132142 try { cleanup ( context , journal ) ; } catch ( cause ) { throw new NativeMainRefreshPublicationError ( { cause } ) ; }
133143 return ;
@@ -169,12 +179,12 @@ export function publishNativeMainRefresh(
169179 if ( ! displaced || digest ( displaced ) !== journal . expectedSha256 ) {
170180 const canonical = readExact ( targetPath ) ;
171181 if ( canonical && digest ( canonical ) === journal . replacementSha256 ) {
172- restoreFilePreservingTarget (
182+ settlePreparedReplacement (
183+ context ,
184+ journal ,
173185 process . platform === "win32" ? previous : staged ,
174- targetPath ,
175186 process . platform === "win32" ? staged : previous ,
176187 ) ;
177- cleanup ( context , journal ) ;
178188 }
179189 throw new NativeMainRefreshPublicationError ( ) ;
180190 }
@@ -194,3 +204,7 @@ export function publishNativeMainRefresh(
194204export function nativeMainRefreshJournalBasename ( ) : string {
195205 return basename ( JOURNAL ) ;
196206}
207+
208+ export function setNativeMainBeforeRecoveryReplaceHookForTests ( hook : ( ( ) => void ) | null ) : void {
209+ beforeRecoveryReplaceForTests = hook ;
210+ }
0 commit comments