Skip to content

Commit 45dccec

Browse files
shanselmanCopilot
andcommitted
Remove operator-side node pair auto-approve
Roll back the PR #457 operator-side NodePairListUpdated auto-approve path because it can race the easy-button WSL setup node-pairing phase by restarting the node connection while setup is waiting on it. Keep the zero-state chat session-key fix and the read-only pending-pair UI surfacing. Co-authored-by: Copilot <[email protected]>
1 parent 928d385 commit 45dccec

4 files changed

Lines changed: 3 additions & 274 deletions

File tree

‎src/OpenClaw.Connection/GatewayConnectionManager.cs‎

Lines changed: 3 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,6 @@ private async Task ConnectCoreAsync(string? gatewayId = null)
239239
_gatewayNeedsV2Signature = true;
240240
};
241241

242-
// Operator-side auto-approve: the node-side WindowsNodeClient
243-
// only sees its own pairing state. But when the gateway broadcasts
244-
// a node.pair.requested event, the OPERATOR client gets a fresh
245-
// NodePairListUpdated push. If our own node deviceId shows up in
246-
// that pending list and we have approval scopes, approve it
247-
// immediately — otherwise the user is stuck looking at a connected
248-
// node with empty capabilities until they manually approve.
249-
// This complements (not replaces) the node-side flow on line ~810
250-
// which still handles the case where the node knows it's pending
251-
// before any operator event lands.
252-
lifecycle.DataClient.NodePairListUpdated += (s, info) =>
253-
{
254-
if (Interlocked.Read(ref _generation) != gen) return;
255-
_ = TryOperatorAutoApproveOwnNodePairAsync(info, gen);
256-
};
257-
258242
// If we already know this gateway needs v2, tell the client upfront
259243
if (_gatewayNeedsV2Signature)
260244
lifecycle.DataClient.UseV2Signature = true;
@@ -903,9 +887,9 @@ private async void OnNodePairingStatusChanged(object? sender, PairingStatusEvent
903887
finally
904888
{
905889
// Only dedupe after an actual approve attempt. If the operator
906-
// client was disconnected or lacked scope, the operator-side
907-
// NodePairListUpdated path must still be able to approve this
908-
// same requestId once the operator is ready.
890+
// client was disconnected or lacked scope, do not burn the
891+
// requestId; a later Pending event can still retry once the
892+
// operator client is ready or has approval scope.
909893
if (attemptedApprove && Interlocked.Read(ref _generation) == approvalGeneration)
910894
_lastAutoApprovedRequestId = e.RequestId;
911895
Interlocked.Exchange(ref _autoApproveInFlight, null);
@@ -923,99 +907,6 @@ private async void OnNodePairingStatusChanged(object? sender, PairingStatusEvent
923907
}
924908
}
925909

926-
/// <summary>
927-
/// Operator-side auto-approve. When the gateway pushes
928-
/// <see cref="OpenClawGatewayClient.NodePairListUpdated"/> and there is a
929-
/// pending entry for our OWN node's deviceId, approve it. The node-side
930-
/// auto-approve at <see cref="OnNodePairingStatusChanged"/> handles the
931-
/// case where the node already knows it is pending; this method handles
932-
/// the case where the node is device-paired (its WindowsNodeClient sees
933-
/// itself as Paired) but its node-sub-pairing hasn't been approved yet —
934-
/// the only signal for that case is the operator-side broadcast.
935-
/// </summary>
936-
private async Task TryOperatorAutoApproveOwnNodePairAsync(PairingListInfo? info, long gen)
937-
{
938-
if (info?.Pending == null || info.Pending.Count == 0) return;
939-
940-
var ownNodeId = _nodeConnector?.NodeDeviceId;
941-
if (string.IsNullOrWhiteSpace(ownNodeId)) return;
942-
943-
var operatorClient = _activeLifecycle?.DataClient;
944-
if (operatorClient?.IsConnectedToGateway != true) return;
945-
if (!OperatorScopeHelper.CanApproveDevices(operatorClient.GrantedOperatorScopes)) return;
946-
947-
// Track whether ANY approve succeeded so we know to schedule one
948-
// reconnect at the end (rather than reconnecting per-entry, which
949-
// would race with itself).
950-
string? lastApprovedRequestId = null;
951-
952-
foreach (var req in info.Pending)
953-
{
954-
if (Interlocked.Read(ref _generation) != gen) return;
955-
if (string.IsNullOrWhiteSpace(req.RequestId)) continue;
956-
if (req.RequestId == _lastAutoApprovedRequestId) continue;
957-
if (!string.Equals(req.NodeId, ownNodeId, StringComparison.OrdinalIgnoreCase)) continue;
958-
959-
// CAS guard scoped to JUST the approve RPC. Release before the
960-
// post-approve reconnect so unrelated approvals are not starved
961-
// (e.g. another own-node pending with a different requestId in
962-
// the same or next snapshot).
963-
if (Interlocked.CompareExchange(ref _autoApproveInFlight, req.RequestId, null) != null)
964-
continue;
965-
966-
bool approved = false;
967-
try
968-
{
969-
_diagnostics.Record("node", $"Operator-side auto-approving own node pairing (requestId={req.RequestId})");
970-
try
971-
{
972-
approved = await operatorClient.NodePairApproveAsync(req.RequestId);
973-
if (!approved)
974-
_diagnostics.Record("node", "Operator-side node pair approval rejected by gateway");
975-
}
976-
catch (Exception ex)
977-
{
978-
_logger.Warn($"[ConnMgr] Operator-side node auto-approve failed: {ex.Message}");
979-
_diagnostics.Record("node", $"Operator-side auto-approve error: {ex.Message}");
980-
}
981-
}
982-
finally
983-
{
984-
// Always record the requestId — both on success (prevent
985-
// re-approving the same id after the gateway re-broadcasts)
986-
// and on failure (prevent a spin loop on a rejected id).
987-
// Re-check generation: if a reconnect happened during the
988-
// await above, DisposeActiveClient already cleared
989-
// _lastAutoApprovedRequestId for the new generation; we must
990-
// not overwrite that null with a stale id from the old gen.
991-
if (Interlocked.Read(ref _generation) == gen)
992-
_lastAutoApprovedRequestId = req.RequestId;
993-
Interlocked.Exchange(ref _autoApproveInFlight, null);
994-
}
995-
996-
if (approved)
997-
{
998-
lastApprovedRequestId = req.RequestId;
999-
// Continue scanning so a second own-pending in the same
1000-
// snapshot (e.g. stale requestId from prior session) also
1001-
// gets attempted — broken only by an explicit failure to
1002-
// approve, which we still try the next entry for.
1003-
}
1004-
// On failure/rejection, fall through to next own-pending entry
1005-
// rather than break — the gateway may not re-broadcast if the
1006-
// approve frame round-tripped and was rejected mid-flight.
1007-
}
1008-
1009-
// Single reconnect after the snapshot is fully processed.
1010-
if (lastApprovedRequestId is not null)
1011-
{
1012-
_diagnostics.Record("node", $"Operator-side approved {lastApprovedRequestId} — reconnecting node so caps propagate");
1013-
await Task.Delay(1000);
1014-
if (Interlocked.Read(ref _generation) == gen)
1015-
await StartNodeConnectionAsync();
1016-
}
1017-
}
1018-
1019910
// ─── Helpers ───
1020911

1021912
private void EmitStateChanged(OverallConnectionState previousOverall)

‎src/OpenClaw.Shared/OpenClaw.Shared.csproj‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
<ItemGroup>
1111
<InternalsVisibleTo Include="OpenClaw.Shared.Tests" />
12-
<InternalsVisibleTo Include="OpenClaw.Connection.Tests" />
1312
</ItemGroup>
1413

1514
<ItemGroup>

‎src/OpenClaw.Shared/OpenClawGatewayClient.cs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,6 @@ protected override void OnDisposing()
208208
public event EventHandler<JsonElement>? AgentFilesListUpdated;
209209
public event EventHandler<JsonElement>? AgentFileContentUpdated;
210210

211-
// ─── Test-only event raisers ───
212-
// Exposed via [InternalsVisibleTo("OpenClaw.Connection.Tests")] so unit
213-
// tests can drive event-handler code paths without a live WebSocket.
214-
// Avoids the previous reflection-on-private-backing-field approach which
215-
// silently breaks the moment the events grow explicit add/remove blocks.
216-
internal void RaiseNodePairListUpdatedForTests(PairingListInfo info)
217-
=> NodePairListUpdated?.Invoke(this, info);
218211
/// <summary>
219212
/// Raised when the gateway broadcasts a "chat" event (assistant or user
220213
/// message echo). Use this to drive a chat-UI timeline; the existing

‎tests/OpenClaw.Connection.Tests/NodePairAutoApproveTests.cs‎

Lines changed: 0 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -131,150 +131,6 @@ await FireAndWait(manager, () =>
131131
.Count(m => m == "node.pair.approve"));
132132
}
133133

134-
[Fact]
135-
public async Task OperatorSideAutoApprove_ApprovesPendingForOwnNodeId()
136-
{
137-
// Operator-side regression test for the scenario where:
138-
// 1. The Windows node device is already paired (so the node-side
139-
// WindowsNodeClient sees PairingStatus.Paired and never fires
140-
// Pending — the existing OnNodePairingStatusChanged path can't
141-
// kick in).
142-
// 2. The gateway nevertheless broadcasts node.pair.requested
143-
// because the node-sub-pairing record is empty (gateway's
144-
// /home/openclaw/.openclaw/nodes/paired.json = "{}").
145-
// 3. The operator client receives the resulting
146-
// NodePairListUpdated push containing OUR own nodeId.
147-
// Manager must auto-approve via the operator path, otherwise the
148-
// node sits as "connected with 0 capabilities" indefinitely.
149-
using var manager = CreateConnectedManager();
150-
151-
var lifecycle = _factory.CreatedClients[0];
152-
lifecycle.TrackingClient.SetGrantedScopes(["operator.admin"]);
153-
lifecycle.TrackingClient.SetIsConnected(true);
154-
155-
const string ownNodeId = "f52d5187f33563a00947012c8de63f489cd0127bf008017e77090e218918a9f6";
156-
_nodeConnector.NodeDeviceId = ownNodeId;
157-
158-
var approvalDone = lifecycle.TrackingClient.WaitForApprovalCallAsync();
159-
lifecycle.TrackingClient.FireNodePairListUpdated(new PairingListInfo
160-
{
161-
Pending =
162-
[
163-
new PairingRequest { RequestId = "op-side-req-1", NodeId = ownNodeId }
164-
]
165-
});
166-
await approvalDone;
167-
168-
Assert.Contains("node.pair.approve", lifecycle.TrackingClient.ApprovalMethodsCalled);
169-
}
170-
171-
[Fact]
172-
public async Task OperatorSideAutoApprove_IgnoresPendingForOtherNodeId()
173-
{
174-
// Defensive: when the gateway has multiple pending node-pair
175-
// requests (e.g. another tray on the same gateway), the operator
176-
// path must only approve OUR own deviceId — never silently
177-
// approve a peer.
178-
using var manager = CreateConnectedManager();
179-
180-
var lifecycle = _factory.CreatedClients[0];
181-
lifecycle.TrackingClient.SetGrantedScopes(["operator.admin"]);
182-
lifecycle.TrackingClient.SetIsConnected(true);
183-
_nodeConnector.NodeDeviceId = "f52d5187...own";
184-
185-
lifecycle.TrackingClient.FireNodePairListUpdated(new PairingListInfo
186-
{
187-
Pending =
188-
[
189-
new PairingRequest { RequestId = "req-someone-else", NodeId = "different-node-id" }
190-
]
191-
});
192-
await Task.Delay(200);
193-
194-
Assert.Empty(lifecycle.TrackingClient.ApprovalMethodsCalled);
195-
}
196-
197-
[Fact]
198-
public async Task OperatorSideAutoApprove_WithoutScope_DoesNotApprove()
199-
{
200-
using var manager = CreateConnectedManager();
201-
202-
var lifecycle = _factory.CreatedClients[0];
203-
lifecycle.TrackingClient.SetGrantedScopes(["operator.read"]); // no admin/pairing
204-
lifecycle.TrackingClient.SetIsConnected(true);
205-
_nodeConnector.NodeDeviceId = "own-id";
206-
207-
lifecycle.TrackingClient.FireNodePairListUpdated(new PairingListInfo
208-
{
209-
Pending =
210-
[
211-
new PairingRequest { RequestId = "req-no-scope", NodeId = "own-id" }
212-
]
213-
});
214-
await Task.Delay(200);
215-
216-
Assert.Empty(lifecycle.TrackingClient.ApprovalMethodsCalled);
217-
}
218-
219-
[Fact]
220-
public async Task OperatorSideAutoApprove_NodeSideSkippedWithoutScope_CanStillApproveSameRequestId()
221-
{
222-
using var manager = CreateConnectedManager();
223-
224-
var lifecycle = _factory.CreatedClients[0];
225-
lifecycle.TrackingClient.SetGrantedScopes(["operator.read"]); // no admin/pairing yet
226-
lifecycle.TrackingClient.SetIsConnected(true);
227-
_nodeConnector.NodeDeviceId = "own-id";
228-
229-
await FireAndWait(manager, () =>
230-
_nodeConnector.FireStatusChanged(ConnectionStatus.Connecting));
231-
232-
_nodeConnector.FirePairingStatusChanged(PairingStatus.Pending, requestId: "req-same-later-scope");
233-
await Task.Delay(200);
234-
Assert.Empty(lifecycle.TrackingClient.ApprovalMethodsCalled);
235-
236-
lifecycle.TrackingClient.SetGrantedScopes(["operator.admin"]);
237-
var approvalDone = lifecycle.TrackingClient.WaitForApprovalCallAsync();
238-
lifecycle.TrackingClient.FireNodePairListUpdated(new PairingListInfo
239-
{
240-
Pending = [new PairingRequest { RequestId = "req-same-later-scope", NodeId = "own-id" }]
241-
});
242-
await approvalDone;
243-
244-
Assert.Contains("node.pair.approve", lifecycle.TrackingClient.ApprovalMethodsCalled);
245-
}
246-
247-
[Fact]
248-
public async Task OperatorSideAutoApprove_SameRequestId_DoesNotApproveTwice()
249-
{
250-
using var manager = CreateConnectedManager();
251-
252-
var lifecycle = _factory.CreatedClients[0];
253-
lifecycle.TrackingClient.SetGrantedScopes(["operator.admin"]);
254-
lifecycle.TrackingClient.SetIsConnected(true);
255-
_nodeConnector.NodeDeviceId = "own-id";
256-
257-
var firstDone = lifecycle.TrackingClient.WaitForApprovalCallAsync();
258-
lifecycle.TrackingClient.FireNodePairListUpdated(new PairingListInfo
259-
{
260-
Pending = [new PairingRequest { RequestId = "dedupe-1", NodeId = "own-id" }]
261-
});
262-
await firstDone;
263-
// Wait for the post-approve sequence to complete before firing the
264-
// second event; _lastAutoApprovedRequestId must be set first.
265-
await Task.Delay(1100);
266-
267-
// Re-broadcast (same id) — must be skipped via _lastAutoApprovedRequestId.
268-
lifecycle.TrackingClient.FireNodePairListUpdated(new PairingListInfo
269-
{
270-
Pending = [new PairingRequest { RequestId = "dedupe-1", NodeId = "own-id" }]
271-
});
272-
await Task.Delay(200);
273-
274-
Assert.Equal(1, lifecycle.TrackingClient.ApprovalMethodsCalled
275-
.Count(m => m == "node.pair.approve"));
276-
}
277-
278134
// ─── Helpers ───
279135

280136
private GatewayConnectionManager CreateConnectedManager()
@@ -422,16 +278,6 @@ public override Task<bool> DevicePairApproveAsync(string requestId)
422278
return Task.FromResult(true);
423279
}
424280

425-
/// <summary>
426-
/// Raises NodePairListUpdated on this client so tests can drive the
427-
/// operator-side auto-approve path without a live WebSocket. Uses the
428-
/// internal test-only raiser exposed via
429-
/// [InternalsVisibleTo("OpenClaw.Connection.Tests")] — earlier this
430-
/// reached the private event backing field by reflection, which
431-
/// silently broke the moment the event got refactored.
432-
/// </summary>
433-
public void FireNodePairListUpdated(PairingListInfo info)
434-
=> RaiseNodePairListUpdatedForTests(info);
435281
}
436282

437283
private sealed class ScriptedNodeConnector : INodeConnector

0 commit comments

Comments
 (0)