Skip to content

Commit 0a488bd

Browse files
authored
fix: device pairing floods stacked approval alerts for a retrying device and stale approvals no-op (#100976)
* fix(gateway): stop device pairing approval alert floods from retrying devices A device retrying with a broken token minted a new pending requestId (and approval alert broadcast) every 5-minute TTL window because refresh preserved ts and expiry keyed on ts. Pending requests now stay alive via an internal refreshedAtMs keepalive (ts still owns ordering/--latest), superseded device requests broadcast device.pair.resolved like node pairing already did, and the Mac app keeps one alert per device, closes superseded visible alerts, and resyncs its queue when approving a stale request. Closes #100974 * chore: satisfy swiftformat and refresh native i18n inventory for pairing prompter changes
1 parent 5e22787 commit 0a488bd

11 files changed

Lines changed: 200 additions & 31 deletions

File tree

‎apps/.i18n/native-source.json‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16299,63 +16299,63 @@
1629916299
},
1630016300
{
1630116301
"kind": "conditional-branch",
16302-
"line": 239,
16302+
"line": 263,
1630316303
"path": "apps/macos/Sources/OpenClaw/DevicePairingApprovalPrompter.swift",
1630416304
"source": "New Mac wants to connect",
1630516305
"surface": "apple",
1630616306
"id": "native.apple.135336820846e999"
1630716307
},
1630816308
{
1630916309
"kind": "conditional-branch",
16310-
"line": 239,
16310+
"line": 263,
1631116311
"path": "apps/macos/Sources/OpenClaw/DevicePairingApprovalPrompter.swift",
1631216312
"source": "New device wants to connect",
1631316313
"surface": "apple",
1631416314
"id": "native.apple.18c2594347b2c96e"
1631516315
},
1631616316
{
1631716317
"kind": "conditional-branch",
16318-
"line": 243,
16318+
"line": 267,
1631916319
"path": "apps/macos/Sources/OpenClaw/DevicePairingApprovalPrompter.swift",
1632016320
"source": "this Mac app",
1632116321
"surface": "apple",
1632216322
"id": "native.apple.fbd59df61769188f"
1632316323
},
1632416324
{
1632516325
"kind": "conditional-branch",
16326-
"line": 243,
16326+
"line": 267,
1632716327
"path": "apps/macos/Sources/OpenClaw/DevicePairingApprovalPrompter.swift",
1632816328
"source": "this device",
1632916329
"surface": "apple",
1633016330
"id": "native.apple.172ef5ed341ad73a"
1633116331
},
1633216332
{
1633316333
"kind": "conditional-branch",
16334-
"line": 248,
16334+
"line": 272,
1633516335
"path": "apps/macos/Sources/OpenClaw/DevicePairingApprovalPrompter.swift",
1633616336
"source": "Approve Device",
1633716337
"surface": "apple",
1633816338
"id": "native.apple.809e76375508d27d"
1633916339
},
1634016340
{
1634116341
"kind": "conditional-branch",
16342-
"line": 248,
16342+
"line": 272,
1634316343
"path": "apps/macos/Sources/OpenClaw/DevicePairingApprovalPrompter.swift",
1634416344
"source": "Approve Mac",
1634516345
"surface": "apple",
1634616346
"id": "native.apple.b5182817237683bc"
1634716347
},
1634816348
{
1634916349
"kind": "conditional-branch",
16350-
"line": 284,
16350+
"line": 308,
1635116351
"path": "apps/macos/Sources/OpenClaw/DevicePairingApprovalPrompter.swift",
1635216352
"source": "New device",
1635316353
"surface": "apple",
1635416354
"id": "native.apple.40a3483823dbbd36"
1635516355
},
1635616356
{
1635716357
"kind": "conditional-branch",
16358-
"line": 284,
16358+
"line": 308,
1635916359
"path": "apps/macos/Sources/OpenClaw/DevicePairingApprovalPrompter.swift",
1636016360
"source": "OpenClaw Mac app",
1636116361
"surface": "apple",
@@ -17995,15 +17995,15 @@
1799517995
},
1799617996
{
1799717997
"kind": "conditional-branch",
17998-
"line": 395,
17998+
"line": 399,
1799917999
"path": "apps/macos/Sources/OpenClaw/NodePairingApprovalPrompter.swift",
1800018000
"source": "Node pairing approved",
1800118001
"surface": "apple",
1800218002
"id": "native.apple.7eb0a88b8274ced8"
1800318003
},
1800418004
{
1800518005
"kind": "conditional-branch",
18006-
"line": 395,
18006+
"line": 399,
1800718007
"path": "apps/macos/Sources/OpenClaw/NodePairingApprovalPrompter.swift",
1800818008
"source": "Node pairing rejected",
1800918009
"surface": "apple",

‎apps/macos/Sources/OpenClaw/DevicePairingApprovalPrompter.swift‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ final class DevicePairingApprovalPrompter {
146146

147147
switch response {
148148
case .alertFirstButtonReturn:
149-
_ = await self.approve(requestId: request.requestId)
149+
if await !(self.approve(requestId: request.requestId)) {
150+
// Stale request (expired or superseded on the gateway): re-sync the
151+
// queue with gateway truth so accumulated stale alerts collapse at once.
152+
await self.loadPendingRequestsFromGateway()
153+
}
150154
case .alertSecondButtonReturn:
151155
shouldRemove = false
152156
if let idx = self.queue.firstIndex(of: request) {
@@ -211,9 +215,29 @@ final class DevicePairingApprovalPrompter {
211215
}
212216
}
213217

218+
/// The gateway keeps at most one live pending request per device, so a new
219+
/// requestId for the same device supersedes anything still queued for it.
220+
/// Without this, missed/dropped resolve pushes pile up as alerts whose
221+
/// approval can no longer succeed. Returns nil when the request is already queued.
222+
static func coalescedQueue(_ queue: [PendingRequest], adding req: PendingRequest) -> [PendingRequest]? {
223+
guard !queue.contains(where: { $0.requestId == req.requestId }) else { return nil }
224+
return queue.filter { $0.deviceId != req.deviceId } + [req]
225+
}
226+
214227
private func enqueue(_ req: PendingRequest) {
215-
guard !self.queue.contains(req) else { return }
216-
self.queue.append(req)
228+
guard let next = Self.coalescedQueue(self.queue, adding: req) else { return }
229+
let supersededActiveId = self.alertState.activeRequestId.flatMap { activeId in
230+
self.queue.contains(where: {
231+
$0.requestId == activeId && $0.deviceId == req.deviceId
232+
}) ? activeId : nil
233+
}
234+
self.queue = next
235+
if let supersededActiveId {
236+
// The visible alert is for a superseded requestId; close it so the
237+
// fresh request presents instead of an approve that would no-op.
238+
self.resolvedByRequestId.insert(supersededActiveId)
239+
self.endActiveAlert()
240+
}
217241
self.updatePendingCounts()
218242
self.presentNextIfNeeded()
219243
}

‎apps/macos/Sources/OpenClaw/NodePairingApprovalPrompter.swift‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,11 @@ final class NodePairingApprovalPrompter {
265265
}
266266

267267
private func enqueue(_ req: PendingRequest) {
268-
if self.queue.contains(req) { return }
268+
if self.queue.contains(where: { $0.requestId == req.requestId }) { return }
269+
// The gateway keeps at most one live pending request per node; a newer
270+
// request supersedes queued ones so missed resolve pushes cannot stack
271+
// stale alerts. The actively presented request stays owned by its alert.
272+
self.queue.removeAll { $0.nodeId == req.nodeId && $0.requestId != self.alertState.activeRequestId }
269273
self.queue.append(req)
270274
self.updatePendingCounts()
271275
self.presentNextIfNeeded()

‎apps/macos/Tests/OpenClawIPCTests/NodePairingApprovalPrompterTests.swift‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,37 @@ struct NodePairingApprovalPrompterTests {
9191
#expect(accessory.frame.width >= 380)
9292
#expect(accessory.frame.height > 80)
9393
}
94+
95+
@Test func `a newer device pairing request supersedes queued requests for the same device`() {
96+
func request(_ requestId: String, deviceId: String, ts: Double) -> DevicePairingApprovalPrompter
97+
.PendingRequest
98+
{
99+
DevicePairingApprovalPrompter.PendingRequest(
100+
requestId: requestId,
101+
deviceId: deviceId,
102+
publicKey: "pub",
103+
displayName: nil,
104+
platform: "MacIntel",
105+
clientId: nil,
106+
clientMode: nil,
107+
role: "node",
108+
scopes: nil,
109+
remoteIp: nil,
110+
silent: nil,
111+
isRepair: true,
112+
ts: ts)
113+
}
114+
115+
let stale1 = request("req-1", deviceId: "device-a", ts: 1)
116+
let stale2 = request("req-2", deviceId: "device-a", ts: 2)
117+
let other = request("req-3", deviceId: "device-b", ts: 3)
118+
let fresh = request("req-4", deviceId: "device-a", ts: 4)
119+
120+
// Stale requests for the same device collapse; other devices are untouched.
121+
let coalesced = DevicePairingApprovalPrompter.coalescedQueue([stale1, stale2, other], adding: fresh)
122+
#expect(coalesced?.map(\.requestId) == ["req-3", "req-4"])
123+
124+
// Re-delivery of an already queued requestId is a no-op.
125+
#expect(DevicePairingApprovalPrompter.coalescedQueue([stale1, other], adding: stale1) == nil)
126+
}
94127
}

‎docs/gateway/pairing.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ handshake. Only clients that explicitly call `node.pair.*` use this flow.
3030
4. On approval, the Gateway issues a **new token** (tokens rotate on re-pair).
3131
5. The node reconnects using the token and is now paired.
3232

33-
Pending requests expire automatically after **5 minutes**.
33+
Pending requests expire automatically **5 minutes after the node's last
34+
retry** — an actively reconnecting node keeps its one pending request alive
35+
rather than generating a fresh request (and approval prompt) per attempt.
3436

3537
## CLI workflow (headless friendly)
3638

‎docs/nodes/index.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ openclaw nodes status
2929
openclaw nodes describe --node <idOrNameOrIp>
3030
```
3131

32-
Pending pairing requests expire after 5 minutes; see [Gateway-owned pairing](/gateway/pairing) for the full request/approve/token lifecycle. If a node retries with changed auth details (role/scopes/public key), the prior pending request is superseded and a new `requestId` is created — re-run `openclaw devices list` before approving.
32+
Pending pairing requests expire 5 minutes after the device's last retry — a device that keeps reconnecting keeps its one pending request (and `requestId`) alive instead of minting a new prompt every few minutes; see [Gateway-owned pairing](/gateway/pairing) for the full request/approve/token lifecycle. If a node retries with changed auth details (role/scopes/public key), the prior pending request is superseded and a new `requestId` is created — clients get a `device.pair.resolved` event for the superseded request, and you should re-run `openclaw devices list` before approving.
3333

3434
- `nodes status` marks a node as **paired** when its device pairing role includes `node`.
3535
- The device pairing record is the durable approved-role contract. Token rotation stays inside that contract; it cannot upgrade a paired node into a role that pairing approval never granted.

‎src/gateway/server/ws-connection/message-handler.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,21 @@ export function attachGatewayWsMessageHandler(params: GatewayWsMessageHandlerPar
14431443
allowSetupCodeMobileBootstrapPairing,
14441444
});
14451445
const context = buildRequestContext();
1446+
// A replacement request obsoletes older pending requestIds; tell approval
1447+
// UIs so they drop the stale prompts instead of stacking alerts forever.
1448+
const supersededResolvedAt = Date.now();
1449+
for (const superseded of pairing.superseded ?? []) {
1450+
context.broadcast(
1451+
"device.pair.resolved",
1452+
{
1453+
requestId: superseded.requestId,
1454+
deviceId: superseded.deviceId,
1455+
decision: "rejected",
1456+
ts: supersededResolvedAt,
1457+
},
1458+
{ dropIfSlow: true },
1459+
);
1460+
}
14461461
let approved: Awaited<ReturnType<typeof approveDevicePairing>> | undefined;
14471462
let resolvedByConcurrentApproval = false;
14481463
let recoveryRequestId: string | undefined;

‎src/infra/device-pairing-churn.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ describe("device pairing requestId churn", () => {
9292

9393
expect(approveReconnect.created).toBe(true);
9494
expect(approveReconnect.request.requestId).not.toBe(readRepair.request.requestId);
95+
expect(approveReconnect.superseded).toEqual([
96+
{ requestId: readRepair.request.requestId, deviceId: DEVICE_ID },
97+
]);
9598

9699
const staleApprove = await approveDevicePairing(
97100
readRepair.request.requestId,

‎src/infra/device-pairing.test.ts‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,50 @@ describe("device pairing tokens", () => {
293293
expect(second.request.ts).toBe(originalTs);
294294
});
295295

296+
test("re-requests keep one pending request alive past the pending TTL without churning requestIds", async () => {
297+
// Regression: a device retrying all night must not mint a new requestId (and a
298+
// new approval prompt broadcast) every TTL window. Refreshes stamp refreshedAtMs
299+
// as a TTL keepalive while ts stays the creation time for approval ordering.
300+
const baseDir = await makeDevicePairingDir();
301+
const req = {
302+
deviceId: "device-1",
303+
publicKey: "public-key-1",
304+
role: "operator" as const,
305+
scopes: ["operator.read"],
306+
};
307+
const first = await requestDevicePairing(req, baseDir);
308+
const refreshed = await requestDevicePairing(req, baseDir);
309+
expect(refreshed.created).toBe(false);
310+
311+
// Simulate hours of aging since creation while retries kept the keepalive fresh.
312+
const paths = resolvePairingPaths(baseDir, "devices");
313+
const pendingById = JSON.parse(await readFile(paths.pendingPath, "utf8")) as Record<
314+
string,
315+
{ ts: number; refreshedAtMs?: number }
316+
>;
317+
const pending = requireValue(
318+
pendingById[first.request.requestId],
319+
"expected pending pairing request",
320+
);
321+
expect(pending.refreshedAtMs).toBeGreaterThanOrEqual(pending.ts);
322+
const createdTs = Date.now() - 60 * 60 * 1000;
323+
pending.ts = createdTs;
324+
await writeFile(paths.pendingPath, JSON.stringify(pendingById, null, 2));
325+
326+
const third = await requestDevicePairing(req, baseDir);
327+
expect(third.created).toBe(false);
328+
expect(third.request.requestId).toBe(first.request.requestId);
329+
expect(third.request.ts).toBe(createdTs);
330+
// The keepalive is store-internal; it must not leak into protocol payloads.
331+
expect("refreshedAtMs" in third.request).toBe(false);
332+
333+
// A stale keepalive still expires the request.
334+
pending.refreshedAtMs = createdTs;
335+
pendingById[first.request.requestId] = pending;
336+
await writeFile(paths.pendingPath, JSON.stringify(pendingById, null, 2));
337+
expect((await listDevicePairing(baseDir)).pending).toHaveLength(0);
338+
});
339+
296340
test("supersedes pending requests when requested roles/scopes change", async () => {
297341
const baseDir = await makeDevicePairingDir();
298342
const first = await requestDevicePairing(
@@ -316,6 +360,9 @@ describe("device pairing tokens", () => {
316360

317361
expect(second.created).toBe(true);
318362
expect(second.request.requestId).not.toBe(first.request.requestId);
363+
expect(second.superseded).toEqual([
364+
{ requestId: first.request.requestId, deviceId: "device-1" },
365+
]);
319366
expect(second.request.role).toBe("operator");
320367
expectArrayIncludesAll(second.request.roles, ["node", "operator"], "request roles");
321368
expectArrayIncludesAll(
@@ -1934,5 +1981,4 @@ describe("device pairing tokens", () => {
19341981
).rejects.toThrow(/paired\.json/);
19351982
await expect(readFile(pairedPath, "utf8")).resolves.toBe("{not-json}");
19361983
});
1937-
19381984
});

0 commit comments

Comments
 (0)