@@ -31,40 +31,74 @@ const runRetryableFinalFenceStep = async (operation: () => Promise<void>): Promi
3131const runResultPreservingFinalFenceStep = async ( operation : ( ) => Promise < void > ) : Promise < void > =>
3232 await runFinalFenceStep ( operation , "preserve-result" ) ;
3333
34+ type WorkspaceReconcileOutcome = "failed" | "succeeded" ;
35+
36+ const workspaceReconcileReporters = new WeakMap <
37+ WorkerWorkspaceReconcileResult ,
38+ ( outcome : WorkspaceReconcileOutcome ) => void
39+ > ( ) ;
40+
41+ export function registerWorkspaceReconcileReporter (
42+ reconciliation : WorkerWorkspaceReconcileResult ,
43+ reporter : ( outcome : WorkspaceReconcileOutcome ) => void ,
44+ ) : void {
45+ workspaceReconcileReporters . set ( reconciliation , reporter ) ;
46+ }
47+
48+ function reportWorkspaceReconcile (
49+ reconciliation : WorkerWorkspaceReconcileResult ,
50+ outcome : WorkspaceReconcileOutcome ,
51+ ) : void {
52+ const reporter = workspaceReconcileReporters . get ( reconciliation ) ;
53+ workspaceReconcileReporters . delete ( reconciliation ) ;
54+ reporter ?.( outcome ) ;
55+ }
56+
3457/** Rechecks both owners after renewing the remote quiescence lease. */
3558export async function verifyReconciledWorkspaceFinal (
3659 reconciliation : WorkerWorkspaceReconcileResult ,
3760 quiescence : WorkerWorkspaceQuiescence ,
3861) : Promise < WorkerWorkspaceApplyResult | undefined > {
39- if ( reconciliation . applyPreparedStagedResult && reconciliation . publishStagedResult ) {
40- try {
41- // Fence the prepared remote capture before quiescence renewal can enroll late writers.
42- await runRetryableFinalFenceStep ( async ( ) => await reconciliation . verifyStable ( ) ) ;
43- // Renew quiescence and freeze any writers that appeared after the prepared capture.
44- await runRetryableFinalFenceStep ( async ( ) => await quiescence . assertActive ( ) ) ;
45- // Keep this fence: a late writer can mutate before renewal enrolls and SIGSTOPs it.
46- await runRetryableFinalFenceStep ( async ( ) => await reconciliation . verifyStable ( ) ) ;
47- await reconciliation . applyPreparedStagedResult ( ) ;
48- await reconciliation . verifyLocalStable ( ) ;
49- // Renew after apply so lease expiry cannot race the final publish gate.
50- await runResultPreservingFinalFenceStep ( async ( ) => await quiescence . assertActive ( ) ) ;
51- // Recheck the remote owner after apply before publishing the prepared result.
52- await runResultPreservingFinalFenceStep ( async ( ) => await reconciliation . verifyStable ( ) ) ;
53- await runResultPreservingFinalFenceStep ( async ( ) => await reconciliation . verifyLocalStable ( ) ) ;
54- await reconciliation . publishStagedResult ( ) ;
55- return reconciliation . getAppliedWorkspaceResult ?.( ) ;
56- } catch ( error ) {
57- await reconciliation . discardPreparedStagedResult ?.( ) . catch ( ( ) => undefined ) ;
58- throw error ;
62+ let succeeded = false ;
63+ try {
64+ if ( reconciliation . applyPreparedStagedResult && reconciliation . publishStagedResult ) {
65+ try {
66+ // Fence the prepared remote capture before quiescence renewal can enroll late writers.
67+ await runRetryableFinalFenceStep ( async ( ) => await reconciliation . verifyStable ( ) ) ;
68+ // Renew quiescence and freeze any writers that appeared after the prepared capture.
69+ await runRetryableFinalFenceStep ( async ( ) => await quiescence . assertActive ( ) ) ;
70+ // Keep this fence: a late writer can mutate before renewal enrolls and SIGSTOPs it.
71+ await runRetryableFinalFenceStep ( async ( ) => await reconciliation . verifyStable ( ) ) ;
72+ await reconciliation . applyPreparedStagedResult ( ) ;
73+ await reconciliation . verifyLocalStable ( ) ;
74+ // Renew after apply so lease expiry cannot race the final publish gate.
75+ await runResultPreservingFinalFenceStep ( async ( ) => await quiescence . assertActive ( ) ) ;
76+ // Recheck the remote owner after apply before publishing the prepared result.
77+ await runResultPreservingFinalFenceStep ( async ( ) => await reconciliation . verifyStable ( ) ) ;
78+ await runResultPreservingFinalFenceStep (
79+ async ( ) => await reconciliation . verifyLocalStable ( ) ,
80+ ) ;
81+ await reconciliation . publishStagedResult ( ) ;
82+ const applied = reconciliation . getAppliedWorkspaceResult ?.( ) ;
83+ succeeded = true ;
84+ return applied ;
85+ } catch ( error ) {
86+ await reconciliation . discardPreparedStagedResult ?.( ) . catch ( ( ) => undefined ) ;
87+ throw error ;
88+ }
5989 }
90+ const runFenceStep = reconciliation . changed
91+ ? runResultPreservingFinalFenceStep
92+ : runRetryableFinalFenceStep ;
93+ await runFenceStep ( async ( ) => await reconciliation . verifyStable ( ) ) ;
94+ await runFenceStep ( async ( ) => await reconciliation . verifyLocalStable ( ) ) ;
95+ await runFenceStep ( async ( ) => await quiescence . assertActive ( ) ) ;
96+ await runFenceStep ( async ( ) => await reconciliation . verifyStable ( ) ) ;
97+ await runFenceStep ( async ( ) => await reconciliation . verifyLocalStable ( ) ) ;
98+ const applied = reconciliation . getAppliedWorkspaceResult ?.( ) ;
99+ succeeded = true ;
100+ return applied ;
101+ } finally {
102+ reportWorkspaceReconcile ( reconciliation , succeeded ? "succeeded" : "failed" ) ;
60103 }
61- const runFenceStep = reconciliation . changed
62- ? runResultPreservingFinalFenceStep
63- : runRetryableFinalFenceStep ;
64- await runFenceStep ( async ( ) => await reconciliation . verifyStable ( ) ) ;
65- await runFenceStep ( async ( ) => await reconciliation . verifyLocalStable ( ) ) ;
66- await runFenceStep ( async ( ) => await quiescence . assertActive ( ) ) ;
67- await runFenceStep ( async ( ) => await reconciliation . verifyStable ( ) ) ;
68- await runFenceStep ( async ( ) => await reconciliation . verifyLocalStable ( ) ) ;
69- return reconciliation . getAppliedWorkspaceResult ?.( ) ;
70104}
0 commit comments