Skip to content

Commit 428db77

Browse files
bkudiessCopilotkarkarl
authored
feat(connection): enforce gateway protocol compatibility (#1127)
* feat(connection): enforce gateway protocol compatibility Co-authored-by: Copilot App <[email protected]> * test(connection): align protocol coverage after restack Co-authored-by: Copilot App <[email protected]> Copilot-Session: 3e9101e4-2a42-47ea-97c4-b60603f4e45f * fix(connection): sanitize protocol mismatch diagnostics Avoid a redundant node compatibility snapshot rebuild while preserving the paired transition. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 3e9101e4-2a42-47ea-97c4-b60603f4e45f --------- Co-authored-by: Copilot App <[email protected]> Co-authored-by: Karen Lai <[email protected]> Copilot-Session: 3e9101e4-2a42-47ea-97c4-b60603f4e45f
1 parent 12b8445 commit 428db77

46 files changed

Lines changed: 2681 additions & 65 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/ARCHITECTURE.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ These are the canonical homes. Do not reintroduce private copies elsewhere.
8282
| Windows node connection generation, cancellation, start ordering, recovery, events, and telemetry | `NodeConnectionCoordinator` | authoritative |
8383
| Bootstrap/shared/device credential handoff, durable clear gate, and operator token recovery timing | `BootstrapTokenLifecycle` | authoritative |
8484
| Device role-upgrade approval, confirmation, and bounded node reconnect queue | `DevicePairApprovalCoordinator` | authoritative |
85+
| Gateway wire protocol range, minimal `hello-ok` validation, and sanitized compatibility state | `GatewayProtocolContract` + `GatewayProtocolCompatibility` | authoritative |
8586
| Capability UI metadata | `NodeCapabilityUiCatalog` (planned) | planned |
8687
| Capability registration/gating | `NodeCapabilityRegistrationPolicy` (planned) | planned |
8788
| Local MCP exposure policy | `McpCapabilityPolicy` (planned) | planned |
@@ -196,6 +197,8 @@ leading and trailing pipe. Columns, in order:
196197
| connect-envelope | authoritative | src/OpenClaw.Shared/OpenClawGatewayClient.cs + src/OpenClaw.Shared/WindowsNodeClient.cs | connect envelope wire shape, auth field mapping, and v3/v2 signing arguments | ConnectEnvelopeBuilder | callers explicitly select role, scopes, credential profile, and lifecycle/fallback state | the builder cannot infer credential precedence; exact v3/v2 signing bytes and protocol 3/4 remain unchanged | ConnectEnvelopeBuilderTests.Build_CompleteProfileMatrix_PreservesWireShapeAndSigningArguments | golden | - |
197198
| gateway-connect-inline-closed | closed | src/OpenClaw.Shared/OpenClawGatewayClient.cs + src/OpenClaw.Shared/WindowsNodeClient.cs | anonymous connect envelope, auth dictionary, and direct signature/payload construction | ConnectEnvelopeBuilder | explicit role/scope/credential profile selection, lifecycle/fallback state and persistence, redacted logging, transport send | both clients delegate connect construction without moving credential precedence into the builder | GatewayProtocolCoreClosureTests.GatewayClients_DoNotReintroduce_InlineConnectEnvelopeConstruction | source-shape | when both clients are removed or no longer initiate gateway connect handshakes |
198199
| gateway-pending-inline-closed | closed | src/OpenClaw.Shared/OpenClawGatewayClient.cs | pending maps and locks plus Track-Take-Clear and chat-send helper families | PendingRequestRegistry | request-id creation, timeout policy, response parsing/routing, transport | the client delegates registration, take, removal, and disconnect drain so request ids cannot leak and only one completion wins | GatewayProtocolCoreClosureTests.OpenClawGatewayClient_DoesNotReintroduce_InlinePendingRequestTracking | source-shape | when OpenClawGatewayClient is removed or no longer issues correlated requests |
200+
| gateway-protocol-contract | authoritative | ConnectEnvelopeBuilder + src/OpenClaw.Shared/OpenClawGatewayClient.cs + src/OpenClaw.Shared/WindowsNodeClient.cs | duplicated wire-version range, ad hoc successful hello acceptance, and raw mismatch interpretation | GatewayProtocolContract + GatewayProtocolCompatibility | the builder retains exact envelope construction; clients retain credential precedence, signature fallback, generation fencing, and role-specific success side effects | Windows advertises Gateway protocols 3 through 4 and validates the minimal hello-ok shape before success side effects; after the Gateway accepts that range, an integer hello-ok protocol at or above 3 is accepted because it reports the Gateway current constant rather than a negotiated selection; structured mismatch details remain finite and sanitized relative to the advertised range | GatewayProtocolContractTests.SupportedRange_IsThreeThroughFour | behavioral | - |
201+
| gateway-protocol-literals-closed | closed | ConnectEnvelopeBuilder | private minProtocol and maxProtocol numeric literals | GatewayProtocolContract | the builder serializes its existing envelopes using the shared constants | operator and node connect envelopes advertise the same 3-through-4 protocol range | GatewayProtocolContractTests.Clients_use_shared_contract_for_protocol_range | source-shape | when connect envelopes no longer carry a Gateway protocol range |
199202
| ui-dispatcher | authoritative | src/OpenClaw.Tray.WinUI/App.xaml.cs | UI-thread marshaling abstraction for presentation code | IUiDispatcher | App and existing WinUI code may call DispatcherQueue directly until the view-model migration | presentation view models depend on IUiDispatcher not a concrete DispatcherQueue | UiDispatcherContractTests.PageViewModel_ReceivesRegisteredDispatcher | behavioral | - |
200203
| navigation-scope | authoritative | src/OpenClaw.Tray.WinUI/Windows/HubWindow.xaml.cs | page view-model activation/deactivation and disposal lifetime | NavigationScopeManager | HubWindow keeps frame navigation back-stack and rail selection | transient page view models are activated on navigation and deactivated then disposed on navigate-away | NavigationScopeManagerTests.NavigatingAway_DeactivatesAndDisposesPreviousViewModel | behavioral | - |
201204
| composition-root | authoritative | src/OpenClaw.Tray.WinUI/App.xaml.cs | presentation-layer service construction and wiring | AppServiceRegistration | App remains the composition root and owns non-DI service lifetimes | one validated root ServiceProvider; App-owned singletons registered as instances are never disposed by the container | AppServiceRegistrationTests.Dispose_DoesNotDisposeAppOwnedInstanceSingletons | behavioral | - |

‎docs/CONNECTION_PROTOCOL_RESEARCH.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ stable release-validation evidence.
7171
The managed gateway release pin is not the WebSocket protocol pin. Windows
7272
currently advertises `minProtocol: 3` and `maxProtocol: 4`; the gateway reports
7373
its current protocol constant in `hello-ok.protocol`, not a per-connection
74-
negotiated value. Windows records that value for diagnostics but does not
75-
currently branch behavior on it. Current upstream gateways use the protocol-3
74+
negotiated value. After the gateway accepts the advertised range, Windows records
75+
that integer constant for diagnostics and requires it to be at least
76+
`MinimumSupportedVersion`; a lower or malformed value is a terminal protocol
77+
failure. A structured `PROTOCOL_MISMATCH` rejection determines which side falls
78+
outside the advertised range. Current upstream gateways use the protocol-3
7679
N-1 node window only when both `role` and `client.mode` are `node` and the
7780
client range does not support the gateway's current protocol. Because Windows
7881
advertises `maxProtocol: 4`, its node connection uses protocol 4 against a

‎docs/TELEMETRY.md‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,25 @@ The tray exports gateway lifecycle diagnostics when an endpoint is configured:
100100
- structured state logs in the `OpenClaw.Telemetry.Connection` category
101101

102102
Lifecycle attributes are limited to role, operation, outcome, coarse error
103-
category, and finite operator/node/overall states. Gateway URLs, IDs, device
104-
IDs, pairing request IDs, credentials, error messages, and diagnostic-ring
103+
category, finite operator/node/overall states, and these protocol fields:
104+
105+
- `openclaw.protocol.client`: the current client wire protocol integer; Windows
106+
currently advertises support from protocol 3 through protocol 4
107+
- `openclaw.protocol.gateway`: the finite relative bucket `older`, `current`,
108+
`newer`, or `unknown`, relative to the advertised 3-through-4 range; this
109+
bucket is not itself a compatibility verdict
110+
- `openclaw.protocol.compatibility`: `unknown`, `compatible`,
111+
`gateway_too_old`, `gateway_too_new`, or `mismatch`
112+
113+
Gateway package versions are not protocol values and are not exported by this
114+
instrumentation. Gateway URLs, IDs, device IDs, pairing request IDs,
115+
credentials, integrity values, error messages, raw frames, and diagnostic-ring
105116
text are not exported.
106117

118+
Only an explicit protocol mismatch maps to the `protocolmismatch` error
119+
category. Unknown or otherwise unclassified Gateway errors map to
120+
`internalerror`; they are never promoted to protocol mismatch.
121+
107122
The operator phase spans distinguish local credential/client/tunnel preparation,
108123
WebSocket transport establishment, and the gateway challenge/hello handshake.
109124
The Windows node initiates its gateway connection: its prepare span includes
87.7 KB
Loading
83.2 KB
Loading

‎src/OpenClaw.Connection/ConnectionStateMachine.cs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ internal sealed class ConnectionStateMachine
1414
private string? _operatorError;
1515
private OpenClaw.Shared.GatewayErrorKind? _operatorErrorKind;
1616
private string? _nodeError;
17+
private OpenClaw.Shared.GatewayErrorKind? _nodeErrorKind;
18+
private OpenClaw.Shared.GatewayProtocolCompatibility _operatorProtocolCompatibility =
19+
OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
20+
private OpenClaw.Shared.GatewayProtocolCompatibility _nodeProtocolCompatibility =
21+
OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
1722
private string? _operatorCredentialSource;
1823
private string? _nodeCredentialSource;
1924
private GatewayCredentialResolutionStatus? _operatorCredentialStatus;
@@ -135,6 +140,8 @@ public void SetNodeEnabled(bool enabled)
135140
{
136141
_nodeState = RoleConnectionState.Disabled;
137142
_nodeError = null;
143+
_nodeErrorKind = null;
144+
_nodeProtocolCompatibility = OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
138145
_nodeCredentialSource = null;
139146
_nodeCredentialStatus = null;
140147
_nodeCredentialFallbackUsed = false;
@@ -145,6 +152,8 @@ public void SetNodeEnabled(bool enabled)
145152
{
146153
_nodeState = RoleConnectionState.Idle;
147154
_nodeError = null;
155+
_nodeErrorKind = null;
156+
_nodeProtocolCompatibility = OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
148157
}
149158
RebuildSnapshot();
150159
}
@@ -157,6 +166,9 @@ public void Reset()
157166
_operatorError = null;
158167
_operatorErrorKind = null;
159168
_nodeError = null;
169+
_nodeErrorKind = null;
170+
_operatorProtocolCompatibility = OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
171+
_nodeProtocolCompatibility = OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
160172
_operatorCredentialSource = null;
161173
_nodeCredentialSource = null;
162174
_operatorCredentialStatus = null;
@@ -177,6 +189,8 @@ public void StartNodeConnecting()
177189
{
178190
_nodeState = RoleConnectionState.Connecting;
179191
_nodeError = null;
192+
_nodeErrorKind = null;
193+
_nodeProtocolCompatibility = OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
180194
RebuildSnapshot();
181195
}
182196
}
@@ -215,6 +229,26 @@ internal void SetOperatorErrorKind(OpenClaw.Shared.GatewayErrorKind? kind)
215229
RebuildSnapshot();
216230
}
217231

232+
internal void SetOperatorProtocolCompatibility(
233+
OpenClaw.Shared.GatewayProtocolCompatibility compatibility)
234+
{
235+
_operatorProtocolCompatibility = compatibility;
236+
RebuildSnapshot();
237+
}
238+
239+
internal void SetNodeErrorKind(OpenClaw.Shared.GatewayErrorKind? kind)
240+
{
241+
_nodeErrorKind = kind;
242+
RebuildSnapshot();
243+
}
244+
245+
internal void SetNodeProtocolCompatibility(
246+
OpenClaw.Shared.GatewayProtocolCompatibility compatibility)
247+
{
248+
_nodeProtocolCompatibility = compatibility;
249+
RebuildSnapshot();
250+
}
251+
218252
/// <summary>Update node info (device ID, pairing status, optional request ID) in the snapshot.</summary>
219253
internal void SetNodeInfo(
220254
string? deviceId,
@@ -302,6 +336,7 @@ private void ApplyTransition(ConnectionTrigger trigger, string? detail)
302336
_operatorState = RoleConnectionState.Connecting;
303337
_operatorError = null;
304338
_operatorErrorKind = null;
339+
_operatorProtocolCompatibility = OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
305340
break;
306341

307342
case ConnectionTrigger.ConnectRequestSent:
@@ -369,6 +404,9 @@ private void ApplyTransition(ConnectionTrigger trigger, string? detail)
369404
_operatorError = null;
370405
_operatorErrorKind = null;
371406
_nodeError = null;
407+
_nodeErrorKind = null;
408+
_operatorProtocolCompatibility = OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
409+
_nodeProtocolCompatibility = OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
372410
_operatorCredentialSource = null;
373411
_nodeCredentialSource = null;
374412
_operatorCredentialStatus = null;
@@ -385,6 +423,7 @@ private void ApplyTransition(ConnectionTrigger trigger, string? detail)
385423
_operatorState = RoleConnectionState.Connecting;
386424
_operatorError = null;
387425
_operatorErrorKind = null;
426+
_operatorProtocolCompatibility = OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
388427
break;
389428

390429
case ConnectionTrigger.ReconnectSuppressed:
@@ -401,21 +440,29 @@ private void ApplyTransition(ConnectionTrigger trigger, string? detail)
401440
case ConnectionTrigger.NodeConnected:
402441
_nodeState = RoleConnectionState.Connected;
403442
_nodeError = null;
443+
_nodeErrorKind = null;
404444
break;
405445

406446
case ConnectionTrigger.NodeDisconnected:
447+
if (_nodeErrorKind == OpenClaw.Shared.GatewayErrorKind.ProtocolMismatch)
448+
break;
449+
407450
_nodeState = RoleConnectionState.Idle;
408451
_nodeError = null;
452+
_nodeErrorKind = null;
453+
_nodeProtocolCompatibility = OpenClaw.Shared.GatewayProtocolCompatibility.Unknown;
409454
break;
410455

411456
case ConnectionTrigger.NodePairingRequired:
412457
_nodeState = RoleConnectionState.PairingRequired;
413458
_nodeError = null;
459+
_nodeErrorKind = null;
414460
break;
415461

416462
case ConnectionTrigger.NodePaired:
417463
_nodeState = RoleConnectionState.Connected;
418464
_nodeError = null;
465+
_nodeErrorKind = null;
419466
break;
420467

421468
case ConnectionTrigger.NodePairingRejected:
@@ -437,6 +484,11 @@ private void ApplyTransition(ConnectionTrigger trigger, string? detail)
437484

438485
private void RebuildSnapshot()
439486
{
487+
var (protocolCompatibility, protocolRole) =
488+
GatewayConnectionSnapshot.DeriveProtocolCompatibility(
489+
_operatorProtocolCompatibility,
490+
_nodeProtocolCompatibility,
491+
_nodeEnabled);
440492
Current = Current with
441493
{
442494
OverallState = GatewayConnectionSnapshot.DeriveOverall(_operatorState, _nodeState, _nodeEnabled),
@@ -448,18 +500,23 @@ private void RebuildSnapshot()
448500
OperatorCredentialFallbackUsed = _operatorCredentialFallbackUsed,
449501
OperatorCredentialBootstrapRequired = _operatorCredentialBootstrapRequired,
450502
OperatorCredentialDetail = _operatorCredentialDetail,
503+
OperatorProtocolCompatibility = _operatorProtocolCompatibility,
451504
OperatorPairingRequired = _operatorState == RoleConnectionState.PairingRequired,
452505
// Clear requestId when no longer in PairingRequired to prevent stale reads
453506
OperatorPairingRequestId = _operatorState == RoleConnectionState.PairingRequired
454507
? Current.OperatorPairingRequestId : null,
455508
NodeConnectionIntended = _nodeEnabled,
456509
NodeState = _nodeState,
457510
NodeError = _nodeError,
511+
NodeErrorKind = _nodeErrorKind,
458512
NodeCredentialSource = _nodeCredentialSource,
459513
NodeCredentialStatus = _nodeCredentialStatus,
460514
NodeCredentialFallbackUsed = _nodeCredentialFallbackUsed,
461515
NodeCredentialBootstrapRequired = _nodeCredentialBootstrapRequired,
462516
NodeCredentialDetail = _nodeCredentialDetail,
517+
NodeProtocolCompatibility = _nodeProtocolCompatibility,
518+
ProtocolCompatibility = protocolCompatibility,
519+
ProtocolCompatibilityRole = protocolRole,
463520
// Clear requestId when no longer in PairingRequired to prevent stale reads
464521
NodePairingRequestId = _nodeState == RoleConnectionState.PairingRequired
465522
? Current.NodePairingRequestId : null,

0 commit comments

Comments
 (0)