@@ -130,8 +130,8 @@ final class MacNodeModeCoordinator: NSObject {
130130
131131 override private convenience init ( ) {
132132 let session = GatewayNodeSession ( )
133- let nodeHostWorker = MacNodeHostWorker ( session : session ) {
134- NotificationCenter . default . post ( name : . openclawNodeHostWorkerFailed , object : nil )
133+ let nodeHostWorker = MacNodeHostWorker ( session : session ) { generation in
134+ NotificationCenter . default . post ( name : . openclawNodeHostWorkerFailed , object : NSNumber ( value : generation ) )
135135 }
136136 self . init (
137137 session : session ,
@@ -259,7 +259,6 @@ final class MacNodeModeCoordinator: NSObject {
259259 }
260260
261261 func prepareForCuaDaemonStop ( ) async {
262- self . resetNodeHostWorkerRetryState ( )
263262 await self . enqueueRouteInvalidation ( mode : . workerRestart ) . value
264263 }
265264
@@ -279,7 +278,6 @@ final class MacNodeModeCoordinator: NSObject {
279278 self . endpointRefreshTask = nil
280279 self . reconnectProbeTask ? . cancel ( )
281280 self . reconnectProbeTask = nil
282- self . resetNodeHostWorkerRetryState ( )
283281 }
284282
285283 func setPreferredGatewayStableID (
@@ -380,6 +378,15 @@ final class MacNodeModeCoordinator: NSObject {
380378 private func enqueueRouteInvalidation (
381379 mode : RouteInvalidationMode ) -> Task < Void , Never >
382380 {
381+ // Worker replacement advances synchronously so a queued exit from the
382+ // old process cannot revoke or consume retry budget from its successor.
383+ switch mode {
384+ case . workerRestart , . terminalStop :
385+ self . nodeHostWorkerConfigurationGeneration &+= 1
386+ self . resetNodeHostWorkerRetryState ( )
387+ case . ordinaryDisconnect , . reconnectRefresh :
388+ break
389+ }
383390 self . revokeRouteAuthority ( )
384391 let invalidationGeneration = self . endpointAttemptGeneration
385392 let invalidatedRouteAuthorityGeneration = self . routeAuthorityGeneration
@@ -836,14 +843,16 @@ final class MacNodeModeCoordinator: NSObject {
836843 throw MacNodeHostWorkerRetryPolicy . RetryBackoffPending ( )
837844 }
838845 let input = MacNodeHostWorkerRetryPolicy . Input (
839- launch : MacNodeHostWorkerLaunch ( command : command ) ,
840- configurationGeneration : self . nodeHostWorkerConfigurationGeneration )
846+ launch : MacNodeHostWorkerLaunch (
847+ command : command ,
848+ configurationGeneration : self . nodeHostWorkerConfigurationGeneration ) )
841849 try self . nodeHostWorkerRetryPolicy . prepareForStart ( input )
842850 self . activeNodeHostWorkerInput = input
843851 }
844852
845- func handleNodeHostWorkerFailureForTesting ( ) {
846- self . handleNodeHostWorkerFailure ( )
853+ func handleNodeHostWorkerFailureForTesting ( configurationGeneration : UInt64 ? = nil ) {
854+ self . handleNodeHostWorkerFailure (
855+ configurationGeneration : configurationGeneration ?? self . nodeHostWorkerConfigurationGeneration )
847856 }
848857
849858 func waitForNodeHostWorkerRetryForTesting ( ) async {
@@ -866,9 +875,10 @@ final class MacNodeModeCoordinator: NSObject {
866875 }
867876 }
868877
869- @ objc private nonisolated func nodeHostWorkerFailed ( _ : Notification ) {
878+ @ objc private nonisolated func nodeHostWorkerFailed ( _ notification : Notification ) {
879+ guard let generation = ( notification . object as? NSNumber ) ? . uint64Value else { return }
870880 Task { @MainActor [ weak self ] in
871- self ? . handleNodeHostWorkerFailure ( )
881+ self ? . handleNodeHostWorkerFailure ( configurationGeneration : generation )
872882 }
873883 }
874884
@@ -880,11 +890,9 @@ final class MacNodeModeCoordinator: NSObject {
880890
881891 @ discardableResult
882892 private func handleNodeHostConfigurationChange ( ) -> Task < Void , Never > {
883- self . nodeHostWorkerConfigurationGeneration &+= 1
884- self . resetNodeHostWorkerRetryState ( )
885893 // Worker code, plugin availability, and its manifest are startup-scoped.
886894 // Replace the process before reconnecting so updates cannot leave a stale route.
887- return self . enqueueRouteInvalidation ( mode : . workerRestart )
895+ self . enqueueRouteInvalidation ( mode : . workerRestart )
888896 }
889897}
890898
@@ -951,16 +959,16 @@ extension MacNodeModeCoordinator {
951959 let effectiveLaunch = MacNodeHostWorkerLaunch (
952960 command : launch . command ,
953961 currentDirectoryURL : launch . currentDirectoryURL ,
954- environment : workerEnvironment )
955- let input = MacNodeHostWorkerRetryPolicy . Input (
956- launch : effectiveLaunch ,
962+ environment : workerEnvironment ,
957963 configurationGeneration : self . nodeHostWorkerConfigurationGeneration )
964+ let input = MacNodeHostWorkerRetryPolicy . Input ( launch : effectiveLaunch )
958965 try self . nodeHostWorkerRetryPolicy . prepareForStart ( input )
959966 self . activeNodeHostWorkerInput = input
960967 return try await nodeHostWorker . start ( launch : effectiveLaunch )
961968 }
962969
963- private func handleNodeHostWorkerFailure ( ) {
970+ private func handleNodeHostWorkerFailure ( configurationGeneration : UInt64 ) {
971+ guard configurationGeneration == self . nodeHostWorkerConfigurationGeneration else { return }
964972 guard let input = self . activeNodeHostWorkerInput else {
965973 self . logger . error ( " node-host worker exited without an active startup input " )
966974 self . enqueueRouteInvalidation ( mode : . ordinaryDisconnect )
0 commit comments