@@ -2051,6 +2051,48 @@ public async Task SetupWizard_RestoreReloadModeRestartsAndVerifiesGateway()
20512051 AssertReloadRestorationCompleted ( commands ) ;
20522052 }
20532053
2054+ [ Fact ]
2055+ public async Task SetupWizard_RestoreReloadModeRetriesExactStartupMigrationLeaseContention ( )
2056+ {
2057+ var restoreAttempts = 0 ;
2058+ var delays = new List < TimeSpan > ( ) ;
2059+ var commands = new FakeCommandRunner (
2060+ _ => Ok ( ) ,
2061+ ( _ , command , _ ) => command switch
2062+ {
2063+ var value when value . Contains ( "config set gateway.reload.mode 'hybrid'" ) =>
2064+ ++ restoreAttempts < 3
2065+ ? Fail ( $ "{ SetupWizardRunner . StartupMigrationLeaseDiagnostic } retry after the other OpenClaw process finishes. (held by pid 266)")
2066+ : Ok ( ) ,
2067+ var value when value . Contains ( "openclaw gateway restart" ) => Ok ( ) ,
2068+ var value when value . Contains ( "curl -s" ) => Ok ( "200" ) ,
2069+ _ => Fail ( $ "Unexpected command: { command } ") ,
2070+ } ) ;
2071+ var ctx = CreateContext ( commands : commands ) ;
2072+ ctx . DistroName = "test-distro" ;
2073+ TrustManagedEndpoint ( ctx ) ;
2074+ var runner = new SetupWizardRunner (
2075+ ctx ,
2076+ ( delay , cancellationToken ) =>
2077+ {
2078+ Assert . False ( cancellationToken . CanBeCanceled ) ;
2079+ delays . Add ( delay ) ;
2080+ return Task . CompletedTask ;
2081+ } ) ;
2082+
2083+ var result = await runner . RestoreReloadModeAsync ( ) ;
2084+
2085+ Assert . True ( result . IsSuccess , result . Message ) ;
2086+ Assert . Equal ( 3 , restoreAttempts ) ;
2087+ Assert . Equal ( 2 , delays . Count ) ;
2088+ Assert . Equal ( TimeSpan . FromMilliseconds ( 250 ) , delays [ 0 ] ) ;
2089+ Assert . Equal ( TimeSpan . FromMilliseconds ( 500 ) , delays [ 1 ] ) ;
2090+ Assert . Single (
2091+ commands . WslCalls ,
2092+ call => call . Command . Contains ( "openclaw gateway restart" ) ) ;
2093+ AssertReloadRestorationCompleted ( commands ) ;
2094+ }
2095+
20542096 [ Fact ]
20552097 public async Task SetupWizard_OrchestratorRestoresAfterSuccessfulWizard ( )
20562098 {
@@ -2071,13 +2113,32 @@ public async Task SetupWizard_OrchestratorRestoresAfterSuccessfulWizard()
20712113 }
20722114
20732115 [ Fact ]
2074- public async Task SetupWizard_OrchestratorRestoresBeforePropagatingCancellation ( )
2116+ public async Task SetupWizard_OrchestratorRestoresThroughLeaseContentionBeforePropagatingCancellation ( )
20752117 {
2076- var commands = CreateReloadRestorationRunner ( ) ;
2118+ var restoreAttempts = 0 ;
2119+ var delayTokens = new List < CancellationToken > ( ) ;
2120+ var commands = new FakeCommandRunner (
2121+ _ => Ok ( ) ,
2122+ ( _ , command , _ ) => command switch
2123+ {
2124+ var value when value . Contains ( "config set gateway.reload.mode 'hybrid'" ) =>
2125+ ++ restoreAttempts == 1
2126+ ? Fail ( $ "{ SetupWizardRunner . StartupMigrationLeaseDiagnostic } retry after the other OpenClaw process finishes.")
2127+ : Ok ( ) ,
2128+ var value when value . Contains ( "openclaw gateway restart" ) => Ok ( ) ,
2129+ var value when value . Contains ( "curl -s" ) => Ok ( "200" ) ,
2130+ _ => Fail ( $ "Unexpected command: { command } ") ,
2131+ } ) ;
20772132 var ctx = CreateContext ( commands : commands ) ;
20782133 ctx . DistroName = "test-distro" ;
20792134 TrustManagedEndpoint ( ctx ) ;
2080- var runner = new SetupWizardRunner ( ctx ) ;
2135+ var runner = new SetupWizardRunner (
2136+ ctx ,
2137+ ( _ , cancellationToken ) =>
2138+ {
2139+ delayTokens . Add ( cancellationToken ) ;
2140+ return Task . CompletedTask ;
2141+ } ) ;
20812142
20822143 await Assert . ThrowsAsync < OperationCanceledException > ( ( ) =>
20832144 runner . RunWithReloadRestorationAsync ( async ( ) =>
@@ -2087,11 +2148,14 @@ await Assert.ThrowsAsync<OperationCanceledException>(() =>
20872148 throw new OperationCanceledException ( ) ;
20882149 } ) ) ;
20892150
2151+ Assert . Equal ( 2 , restoreAttempts ) ;
2152+ Assert . Single ( delayTokens ) ;
2153+ Assert . False ( delayTokens [ 0 ] . CanBeCanceled ) ;
20902154 AssertReloadRestorationCompleted ( commands ) ;
20912155 }
20922156
20932157 [ Fact ]
2094- public async Task SetupWizard_OrchestratorReturnsRestorationFailure ( )
2158+ public async Task SetupWizard_OrchestratorDoesNotRetryUnrelatedRestorationFailure ( )
20952159 {
20962160 var commands = new FakeCommandRunner (
20972161 _ => Ok ( ) ,
@@ -2111,23 +2175,37 @@ public async Task SetupWizard_OrchestratorReturnsRestorationFailure()
21112175
21122176 Assert . False ( result . IsSuccess ) ;
21132177 Assert . Contains ( "Failed to restore gateway.reload.mode" , result . Message ) ;
2178+ Assert . Single (
2179+ commands . WslCalls ,
2180+ call => call . Command . Contains ( "config set gateway.reload.mode 'hybrid'" ) ) ;
21142181 Assert . DoesNotContain (
21152182 commands . WslCalls ,
21162183 call => call . Command . Contains ( "openclaw gateway restart" ) ) ;
21172184 }
21182185
21192186 [ Fact ]
2120- public async Task SetupWizard_OrchestratorPreservesWizardAndRestorationFailures ( )
2187+ public async Task SetupWizard_LeaseContentionExhaustionPreservesWizardAndRestorationFailures ( )
21212188 {
2189+ var restoreAttempts = 0 ;
2190+ var timeProvider = new ManualTimeProvider ( ) ;
21222191 var commands = new FakeCommandRunner (
21232192 _ => Ok ( ) ,
21242193 ( _ , command , _ ) =>
21252194 command . Contains ( "config set gateway.reload.mode 'hybrid'" )
2126- ? Fail ( "restore failed" )
2195+ ? FailWithStdout (
2196+ $ "{ SetupWizardRunner . StartupMigrationLeaseDiagnostic } retry after the other OpenClaw process finishes. (held by pid 247)")
21272197 : Fail ( $ "Unexpected command: { command } ") ) ;
21282198 var ctx = CreateContext ( commands : commands ) ;
21292199 ctx . DistroName = "test-distro" ;
2130- var runner = new SetupWizardRunner ( ctx ) ;
2200+ var runner = new SetupWizardRunner (
2201+ ctx ,
2202+ ( delay , _ ) =>
2203+ {
2204+ restoreAttempts ++ ;
2205+ timeProvider . Advance ( delay ) ;
2206+ return Task . CompletedTask ;
2207+ } ,
2208+ timeProvider ) ;
21312209
21322210 var result = await runner . RunWithReloadRestorationAsync ( ( ) =>
21332211 {
@@ -2137,7 +2215,21 @@ public async Task SetupWizard_OrchestratorPreservesWizardAndRestorationFailures(
21372215
21382216 Assert . False ( result . IsSuccess ) ;
21392217 Assert . Contains ( "Failed to restore gateway.reload.mode" , result . Message ) ;
2218+ Assert . Contains ( SetupWizardRunner . StartupMigrationLeaseDiagnostic , result . Message ) ;
21402219 Assert . Contains ( "wizard failed" , result . Message ) ;
2220+ Assert . Equal ( 10 , restoreAttempts ) ;
2221+ Assert . Equal (
2222+ 11 ,
2223+ commands . WslCalls . Count (
2224+ call => call . Command . Contains ( "config set gateway.reload.mode 'hybrid'" ) ) ) ;
2225+ Assert . Equal ( TimeSpan . FromSeconds ( 14.5 ) , timeProvider . Elapsed ) ;
2226+ Assert . All (
2227+ commands . WslCalls . Where (
2228+ call => call . Command . Contains ( "config set gateway.reload.mode 'hybrid'" ) ) ,
2229+ call => Assert . True ( call . Timeout >= TimeSpan . FromMilliseconds ( 500 ) ) ) ;
2230+ Assert . DoesNotContain (
2231+ commands . WslCalls ,
2232+ call => call . Command . Contains ( "openclaw gateway restart" ) ) ;
21412233 }
21422234
21432235 [ Fact ]
@@ -4363,6 +4455,19 @@ public Task<CommandResult> RunInWslAsync(
43634455 }
43644456 }
43654457
4458+ private sealed class ManualTimeProvider : TimeProvider
4459+ {
4460+ private long _timestamp ;
4461+
4462+ public TimeSpan Elapsed => TimeSpan . FromTicks ( _timestamp ) ;
4463+
4464+ public override long TimestampFrequency => TimeSpan . TicksPerSecond ;
4465+
4466+ public override long GetTimestamp ( ) => _timestamp ;
4467+
4468+ public void Advance ( TimeSpan duration ) => _timestamp += duration . Ticks ;
4469+ }
4470+
43664471 private sealed class RecordingAuthorizationPresenter : IExternalAuthorizationPresenter
43674472 {
43684473 public ExternalAuthorizationRequest ? Request { get ; private set ; }
0 commit comments