11using System ;
2- using System . Collections . Concurrent ;
32using System . Collections . Frozen ;
43using System . Collections . Generic ;
54using System . Diagnostics ;
@@ -18,11 +17,6 @@ namespace OpenClaw.Shared;
1817/// </summary>
1918public class WindowsNodeClient : WebSocketClientBase
2019{
21- private const string NodeInvokeSessionKeyEnvelopeProtocolFeature =
22- "node-invoke-session-key-envelope-v1" ;
23- private static readonly TimeSpan s_protocolFeatureNegotiationTimeout =
24- TimeSpan . FromSeconds ( 5 ) ;
25-
2620 private readonly DeviceIdentity _deviceIdentity ;
2721
2822 // Node capabilities registry
@@ -68,13 +62,6 @@ public class WindowsNodeClient : WebSocketClientBase
6862 // at 8. When full, the gateway receives an immediate "node busy, retry" error response.
6963 private readonly SemaphoreSlim _invokeSemaphore = new ( 8 , 8 ) ;
7064 private readonly InvocationCancellationRegistry _activeInvocations = new ( ) ;
71- private readonly ConcurrentDictionary <
72- string ,
73- TaskCompletionSource < NodeInvokeSessionEnvelopeMode > > _protocolFeatureRequests = new ( ) ;
74- private Task < NodeInvokeSessionEnvelopeMode > _nodeInvokeSessionEnvelopeMode =
75- Task . FromResult ( NodeInvokeSessionEnvelopeMode . Authoritative ) ;
76- private volatile bool _nodeInvokeSessionEnvelopeNegotiationComplete = true ;
77- private int _gatewayConnectionGeneration ;
7865
7966 // Events
8067 public event EventHandler < NodeInvokeRequest > ? InvokeReceived ;
@@ -543,8 +530,7 @@ await SendGatewayResultAndCompleteTelemetryAsync(
543530 }
544531 }
545532
546- var envelopeMode = ResolveNodeInvokeSessionEnvelopeModeAtReceipt ( payload ) ;
547- var sessionKey = ExtractGatewayNodeInvokeSessionKey ( payload , args , envelopeMode ) ;
533+ var sessionKey = ExtractGatewayNodeInvokeSessionKey ( payload ) ;
548534
549535 _logger . Info ( $ "[NODE] Invoking command: { command } ") ;
550536
@@ -801,26 +787,6 @@ private string BuildNodeConnectMessage(
801787
802788 internal void HandleResponse ( JsonElement root )
803789 {
804- if ( root . TryGetProperty ( "id" , out var featureIdProp ) &&
805- featureIdProp . ValueKind == JsonValueKind . String &&
806- featureIdProp . GetString ( ) is { } featureResponseId &&
807- _protocolFeatureRequests . TryRemove ( featureResponseId , out var negotiation ) )
808- {
809- var mode = IsUnsupportedNodeProtocolFeaturesResponse ( root )
810- ? NodeInvokeSessionEnvelopeMode . Legacy
811- : NodeInvokeSessionEnvelopeMode . Authoritative ;
812- negotiation . TrySetResult ( mode ) ;
813- if ( ReferenceEquals ( _nodeInvokeSessionEnvelopeMode , negotiation . Task ) )
814- {
815- _nodeInvokeSessionEnvelopeNegotiationComplete = true ;
816- }
817- if ( mode == NodeInvokeSessionEnvelopeMode . Legacy )
818- {
819- _logger . Debug ( "[NODE] Gateway does not accept node protocol feature publication" ) ;
820- }
821- return ;
822- }
823-
824790 var responseId = root . TryGetProperty ( "id" , out var idProp )
825791 ? idProp . GetString ( )
826792 : null ;
@@ -855,7 +821,6 @@ internal void HandleResponse(JsonElement root)
855821
856822 Volatile . Write ( ref _pendingConnectRequestId , null ) ;
857823 _logger . Info ( "[HANDSHAKE] Received hello-ok!" ) ;
858- _gatewayConnectionGeneration ++ ;
859824 PublishGatewaySelf ( GatewaySelfInfo . FromHelloOk ( payload ) ) ;
860825 var reconnectingAfterApproval = _pairingApprovedAwaitingReconnect ;
861826 _isConnected = true ;
@@ -935,97 +900,10 @@ internal void HandleResponse(JsonElement root)
935900 }
936901
937902 RaiseStatusChanged ( ConnectionStatus . Connected ) ;
938- _ = PublishNodeProtocolFeaturesAsync ( ) ;
939903 HandshakeSucceeded ? . Invoke ( this , EventArgs . Empty ) ;
940904 }
941905 }
942906
943- private async Task PublishNodeProtocolFeaturesAsync ( )
944- {
945- var requestId = Guid . NewGuid ( ) . ToString ( ) ;
946- var negotiation = new TaskCompletionSource < NodeInvokeSessionEnvelopeMode > (
947- TaskCreationOptions . RunContinuationsAsynchronously ) ;
948- _nodeInvokeSessionEnvelopeMode = negotiation . Task ;
949- _nodeInvokeSessionEnvelopeNegotiationComplete = false ;
950- var request = new
951- {
952- type = "req" ,
953- id = requestId ,
954- method = "node.protocolFeatures.update" ,
955- @params = new
956- {
957- features = new [ ] { NodeInvokeSessionKeyEnvelopeProtocolFeature }
958- }
959- } ;
960-
961- try
962- {
963- _protocolFeatureRequests . TryAdd ( requestId , negotiation ) ;
964- _ = ResolveNodeProtocolFeaturesTimeoutAsync ( requestId , negotiation ) ;
965- await SendRawAsync ( JsonSerializer . Serialize ( request ) ) ;
966- }
967- catch ( Exception ex )
968- {
969- _protocolFeatureRequests . TryRemove ( requestId , out _ ) ;
970- negotiation . TrySetResult ( NodeInvokeSessionEnvelopeMode . Authoritative ) ;
971- if ( ReferenceEquals ( _nodeInvokeSessionEnvelopeMode , negotiation . Task ) )
972- {
973- _nodeInvokeSessionEnvelopeNegotiationComplete = true ;
974- }
975- // Only an explicit unknown-method response enables legacy attribution.
976- // Transport failures keep omitted envelopes fail-closed.
977- _logger . Warn ( $ "[NODE] Failed to publish protocol features: { ex . Message } ") ;
978- }
979- }
980-
981- private async Task ResolveNodeProtocolFeaturesTimeoutAsync (
982- string requestId ,
983- TaskCompletionSource < NodeInvokeSessionEnvelopeMode > negotiation )
984- {
985- await Task . Delay ( s_protocolFeatureNegotiationTimeout ) ;
986- if ( _protocolFeatureRequests . TryGetValue ( requestId , out var pending ) &&
987- ReferenceEquals ( pending , negotiation ) )
988- {
989- _logger . Warn ( "[NODE] Protocol feature publication timed out; using fail-closed envelopes" ) ;
990- negotiation . TrySetResult ( NodeInvokeSessionEnvelopeMode . Authoritative ) ;
991- if ( ReferenceEquals ( _nodeInvokeSessionEnvelopeMode , negotiation . Task ) )
992- {
993- _nodeInvokeSessionEnvelopeNegotiationComplete = true ;
994- }
995- }
996- }
997-
998- private static bool IsUnsupportedNodeProtocolFeaturesResponse ( JsonElement response )
999- {
1000- if ( ! response . TryGetProperty ( "ok" , out var ok ) ||
1001- ok . ValueKind != JsonValueKind . False ||
1002- ! response . TryGetProperty ( "error" , out var error ) )
1003- {
1004- return false ;
1005- }
1006-
1007- string ? message ;
1008- if ( error . ValueKind == JsonValueKind . String )
1009- {
1010- message = error . GetString ( ) ;
1011- }
1012- else if ( error . ValueKind == JsonValueKind . Object &&
1013- error . TryGetProperty ( "code" , out var code ) &&
1014- string . Equals ( code . GetString ( ) , "INVALID_REQUEST" , StringComparison . OrdinalIgnoreCase ) &&
1015- error . TryGetProperty ( "message" , out var objectMessage ) )
1016- {
1017- message = objectMessage . GetString ( ) ;
1018- }
1019- else
1020- {
1021- return false ;
1022- }
1023-
1024- return message ? . Contains (
1025- "unknown method: node.protocolFeatures.update" ,
1026- StringComparison . OrdinalIgnoreCase ) == true ;
1027- }
1028-
1029907 private void TryStoreHandshakeDeviceToken ( string token , string [ ] ? scopes )
1030908 {
1031909 try
@@ -1391,10 +1269,7 @@ await SendGatewayResultAndCompleteTelemetryAsync(
13911269 Id = requestId ,
13921270 Command = command ,
13931271 Args = args ,
1394- SessionKey = ExtractGatewayNodeInvokeSessionKey (
1395- paramsEl ,
1396- args ,
1397- NodeInvokeSessionEnvelopeMode . Authoritative ) ,
1272+ SessionKey = ExtractGatewayNodeInvokeSessionKey ( paramsEl ) ,
13981273 Telemetry = telemetry
13991274 } ;
14001275
@@ -1771,10 +1646,7 @@ private static bool TryGetCancellationTargetId(JsonElement container, out string
17711646 return false ;
17721647 }
17731648
1774- private static string ? ExtractGatewayNodeInvokeSessionKey (
1775- JsonElement envelope ,
1776- JsonElement args ,
1777- NodeInvokeSessionEnvelopeMode envelopeMode )
1649+ private static string ? ExtractGatewayNodeInvokeSessionKey ( JsonElement envelope )
17781650 {
17791651 if ( envelope . TryGetProperty ( "sessionKey" , out var envelopeSessionKey ) )
17801652 {
@@ -1787,46 +1659,9 @@ private static bool TryGetCancellationTargetId(JsonElement container, out string
17871659
17881660 return null ;
17891661 }
1790-
1791- if ( envelopeMode == NodeInvokeSessionEnvelopeMode . Legacy &&
1792- args . ValueKind == JsonValueKind . Object &&
1793- args . TryGetProperty ( "sessionKey" , out var legacySessionKey ) &&
1794- legacySessionKey . ValueKind == JsonValueKind . String )
1795- {
1796- var sessionKey = legacySessionKey . GetString ( ) ;
1797- if ( ! string . IsNullOrWhiteSpace ( sessionKey ) )
1798- return sessionKey ;
1799- }
1800-
18011662 return null ;
18021663 }
18031664
1804- private NodeInvokeSessionEnvelopeMode ResolveNodeInvokeSessionEnvelopeModeAtReceipt (
1805- JsonElement envelope )
1806- {
1807- if ( envelope . TryGetProperty ( "sessionKey" , out _ ) )
1808- {
1809- return NodeInvokeSessionEnvelopeMode . Authoritative ;
1810- }
1811-
1812- // An explicit unknown-method response is the only authority for trusting the
1813- // legacy nested binding. Omission remains fail-closed while negotiation is pending.
1814- if ( ! _nodeInvokeSessionEnvelopeNegotiationComplete )
1815- {
1816- return NodeInvokeSessionEnvelopeMode . Authoritative ;
1817- }
1818-
1819- return _nodeInvokeSessionEnvelopeMode . IsCompletedSuccessfully
1820- ? _nodeInvokeSessionEnvelopeMode . Result
1821- : NodeInvokeSessionEnvelopeMode . Authoritative ;
1822- }
1823-
1824- private enum NodeInvokeSessionEnvelopeMode
1825- {
1826- Authoritative ,
1827- Legacy
1828- }
1829-
18301665 private sealed record CommandDispatchEntry (
18311666 INodeCapability Capability ,
18321667 string CanonicalName ) ;
@@ -1978,7 +1813,6 @@ protected override bool ShouldAutoReconnect()
19781813 protected override void OnDisconnected ( )
19791814 {
19801815 _activeInvocations . CancelAll ( ) ;
1981- ResetNodeInvokeSessionEnvelopeNegotiation ( ) ;
19821816 _isConnected = false ;
19831817 Volatile . Write ( ref _pendingConnectRequestId , null ) ;
19841818 // Don't reset pairing state when disconnected due to pairing — gateway
@@ -1993,7 +1827,6 @@ protected override void OnDisconnected()
19931827 protected override void OnError ( Exception ex )
19941828 {
19951829 _activeInvocations . CancelAll ( ) ;
1996- ResetNodeInvokeSessionEnvelopeNegotiation ( ) ;
19971830 _isConnected = false ;
19981831 if ( ! _pairingBlocked )
19991832 {
@@ -2005,19 +1838,5 @@ protected override void OnError(Exception ex)
20051838 protected override void OnDisposing ( )
20061839 {
20071840 _activeInvocations . CancelAll ( ) ;
2008- ResetNodeInvokeSessionEnvelopeNegotiation ( ) ;
2009- }
2010-
2011- private void ResetNodeInvokeSessionEnvelopeNegotiation ( )
2012- {
2013- _gatewayConnectionGeneration ++ ;
2014- foreach ( var request in _protocolFeatureRequests . Values )
2015- {
2016- request . TrySetResult ( NodeInvokeSessionEnvelopeMode . Authoritative ) ;
2017- }
2018- _protocolFeatureRequests . Clear ( ) ;
2019- _nodeInvokeSessionEnvelopeMode =
2020- Task . FromResult ( NodeInvokeSessionEnvelopeMode . Authoritative ) ;
2021- _nodeInvokeSessionEnvelopeNegotiationComplete = true ;
20221841 }
20231842}
0 commit comments