Skip to content

Commit 9003042

Browse files
authored
fix(cli): explain how to recover from device approve deadlock (#98146)
* fix(cli): explain how to recover from device approve deadlock `openclaw devices approve` could fail two ways with no path forward: - When the calling device can't approve its own scope upgrade (it lacks operator.approvals) and no loopback local fallback is available (e.g. a remote --url gateway), the raw "scope upgrade pending approval" error propagated with no guidance. - When the request id wasn't found in pending state (already approved, expired, or superseded), it printed only "unknown requestId". Surface actionable guidance instead: - On an authorization failure, explain that the device can't approve its own upgrade and point to `--token`/`--password` (gateway owner creds) or approving from a device that already holds operator.approvals. - On a missing request, point to `openclaw devices list` and `openclaw devices approve --latest`. AI-assisted (Claude Code). * fix(cli): clarify device approval recovery * fix(cli): avoid unusable approval credential advice
1 parent ad59492 commit 9003042

2 files changed

Lines changed: 53 additions & 9 deletions

File tree

‎src/cli/devices-cli.runtime.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ function isUnknownRequestIdError(error: unknown): boolean {
324324
return normalizeLowercaseStringOrEmpty(message).includes("unknown requestid");
325325
}
326326

327+
function isScopeUpgradePendingApproval(error: unknown): boolean {
328+
return (
329+
readConnectPairingRequiredMessage(normalizeErrorMessage(error))?.reason === "scope-upgrade"
330+
);
331+
}
332+
327333
function resolveLocalPairingFallback(
328334
opts: DevicesRpcOpts,
329335
error: unknown,
@@ -1102,9 +1108,23 @@ export async function runDevicesApproveCommand(
11021108
defaultRuntime.exit(1);
11031109
return;
11041110
}
1105-
const result = await approvePairingWithFallback(opts, resolvedRequestId);
1111+
let result: Record<string, unknown> | null;
1112+
try {
1113+
result = await approvePairingWithFallback(opts, resolvedRequestId);
1114+
} catch (error) {
1115+
if (isScopeUpgradePendingApproval(error)) {
1116+
defaultRuntime.error(
1117+
"This device can't approve its own scope upgrade. Approve it from the Control UI or another authorized device.",
1118+
);
1119+
defaultRuntime.exit(1);
1120+
return;
1121+
}
1122+
throw error;
1123+
}
11061124
if (!result) {
1107-
defaultRuntime.error("unknown requestId");
1125+
defaultRuntime.error(
1126+
`No pending device request matches ${sanitizeForLog(resolvedRequestId)}. Run ${formatCliCommand("openclaw devices list")} and retry with the current request ID.`,
1127+
);
11081128
const nodeApprovalNotices = await findQueryPendingNodeApprovalNotices(opts, resolvedRequestId);
11091129
for (const notice of nodeApprovalNotices) {
11101130
defaultRuntime.error(formatNodeApprovalNotice(notice));

‎src/cli/devices-cli.test.ts‎

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ describe("devices cli approve", () => {
534534
expectGatewayCall(3, { method: "node.list" });
535535
expectGatewayCall(4, { method: "device.pair.list" });
536536
const errorOutput = readRuntimeErrorOutput();
537-
expect(errorOutput).toContain("unknown requestId");
537+
expect(errorOutput).toContain("No pending device request matches");
538538
expect(errorOutput).toContain("Node reapproval pending for Colin's S25");
539539
expect(errorOutput).toContain("openclaw nodes approve node-req-1");
540540
expect(errorOutput).toContain(
@@ -590,7 +590,7 @@ describe("devices cli approve", () => {
590590
expectGatewayCall(3, { method: "node.list" });
591591
expectGatewayCall(4, { method: "device.pair.list" });
592592
const errorOutput = readRuntimeErrorOutput();
593-
expect(errorOutput).toContain("unknown requestId");
593+
expect(errorOutput).toContain("No pending device request matches");
594594
expect(errorOutput).not.toContain("node-req-unrelated");
595595
expect(errorOutput).not.toContain("openclaw nodes approve");
596596
});
@@ -634,7 +634,7 @@ describe("devices cli approve", () => {
634634
expectGatewayCall(2, { method: "node.list" });
635635
expectGatewayCall(3, { method: "device.pair.list" });
636636
const errorOutput = readRuntimeErrorOutput();
637-
expect(errorOutput).toContain("unknown requestId");
637+
expect(errorOutput).toContain("No pending device request matches");
638638
expect(errorOutput).not.toContain("node-req-display-name");
639639
expect(errorOutput).not.toContain("openclaw nodes approve");
640640
});
@@ -1368,7 +1368,7 @@ describe("devices cli local fallback", () => {
13681368
expect(approveDevicePairing).not.toHaveBeenCalled();
13691369
});
13701370

1371-
it("keeps unknown requestId behavior when neither the original nor replacement request remains pending", async () => {
1371+
it("explains how to recover when neither the original nor replacement request remains pending", async () => {
13721372
rejectGatewayForLocalFallback("scope upgrade pending approval (requestId: req-new)");
13731373
rejectGatewayForLocalFallback("scope upgrade pending approval (requestId: req-new)");
13741374
listDevicePairing
@@ -1406,11 +1406,33 @@ describe("devices cli local fallback", () => {
14061406

14071407
await runDevicesApprove(["req-old"]);
14081408

1409-
expect(runtime.error).toHaveBeenCalledWith("unknown requestId");
1409+
const errorOutput = stripAnsi(readRuntimeErrorOutput());
1410+
expect(errorOutput).toContain("No pending device request matches req-old");
1411+
expect(errorOutput).toContain("openclaw devices list");
1412+
expect(errorOutput).not.toContain("unknown requestId");
14101413
expect(runtime.exit).toHaveBeenCalledWith(1);
14111414
expect(approveDevicePairing).not.toHaveBeenCalled();
14121415
});
14131416

1417+
it("explains how to approve an upgrade from another device", async () => {
1418+
// Explicit --url disables the loopback local fallback, so the scope-upgrade
1419+
// denial propagates as the authorization error the user must resolve. The
1420+
// first rejection is consumed by the pre-approve context lookup, the second
1421+
// by the approve call itself.
1422+
rejectGatewayForLocalFallback("scope upgrade pending approval (requestId: req-remote)");
1423+
rejectGatewayForLocalFallback("scope upgrade pending approval (requestId: req-remote)");
1424+
1425+
await runDevicesApprove(["req-remote", "--url", "wss://gateway.example.com/ws"]);
1426+
1427+
const errorOutput = stripAnsi(readRuntimeErrorOutput());
1428+
expect(errorOutput).toContain("can't approve its own scope upgrade");
1429+
expect(errorOutput).toContain("Control UI");
1430+
expect(errorOutput).toContain("another authorized device");
1431+
expect(errorOutput).not.toContain("--token");
1432+
expect(errorOutput).not.toContain("--password");
1433+
expect(runtime.exit).toHaveBeenCalledWith(1);
1434+
});
1435+
14141436
it("falls back to local pairing list when gateway returns a scope upgrade message on loopback", async () => {
14151437
mockLocalPairingFallback("scope upgrade pending approval (requestId: req-1)");
14161438

@@ -1445,14 +1467,16 @@ describe("devices cli local fallback", () => {
14451467
expect(readRuntimeOutput()).not.toContain(fallbackNotice);
14461468
});
14471469

1448-
it("keeps unknown requestId behavior instead of approving a different local request", async () => {
1470+
it("explains recovery instead of approving a different local request", async () => {
14491471
rejectGatewayForLocalFallback("device pairing required (requestId: req-profile)");
14501472
rejectGatewayForLocalFallback("device pairing required (requestId: req-profile)");
14511473

14521474
await runDevicesApprove(["req-default"]);
14531475

14541476
expect(approveDevicePairing).not.toHaveBeenCalled();
1455-
expect(runtime.error).toHaveBeenCalledWith("unknown requestId");
1477+
const errorOutput = stripAnsi(readRuntimeErrorOutput());
1478+
expect(errorOutput).toContain("No pending device request matches req-default");
1479+
expect(errorOutput).toContain("openclaw devices list");
14561480
expect(runtime.exit).toHaveBeenCalledWith(1);
14571481
});
14581482

0 commit comments

Comments
 (0)