Skip to content

Commit 0a153dd

Browse files
committed
fix(nodes): show pending requestIds when approve fails with unknown requestId
When openclaw nodes approve fails with "unknown requestId", the error message gave users no actionable guidance. This adds a hint that lists current pending requestIds and points to the openclaw nodes pending command for inspection. - Add isUnknownNodePairRequestIdError helper to detect the specific GatewayClientRequestError / INVALID_REQUEST unknown requestId error - Add buildUnknownRequestIdHint to fetch pending requests and build a descriptive error message - Wrap the approve call in try/catch to intercept unknown requestId errors and surface the hint with pending IDs Closes #94040
1 parent bf872b3 commit 0a153dd

2 files changed

Lines changed: 217 additions & 10 deletions

File tree

‎src/cli/nodes-cli/register.pairing.ts‎

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,43 @@ async function resolveApproveScopesForRequest(
6565
}
6666
}
6767

68+
function isUnknownNodePairRequestIdError(error: unknown): boolean {
69+
return (
70+
error instanceof Error &&
71+
error.name === "GatewayClientRequestError" &&
72+
(error as Error & { gatewayCode?: unknown }).gatewayCode === "INVALID_REQUEST" &&
73+
error.message.includes("unknown requestId")
74+
);
75+
}
76+
77+
async function buildUnknownRequestIdHint(
78+
opts: NodesRpcOpts,
79+
requestId: string,
80+
): Promise<string> {
81+
let pendingIds: string[] = [];
82+
try {
83+
const result = await callNodePairApprovalGatewayCli(
84+
"node.pair.list",
85+
opts,
86+
{},
87+
{ scopes: DEFAULT_NODE_PAIR_APPROVE_SCOPES },
88+
);
89+
pendingIds = parsePairingList(result)
90+
.pending.map((request) => request.requestId)
91+
.filter((id): id is string => typeof id === "string" && id.length > 0);
92+
} catch {
93+
// If we can't list pending requests, skip the hint listing
94+
}
95+
const lines = [`Unknown node pairing requestId: ${requestId}`];
96+
if (pendingIds.length > 0) {
97+
lines.push(`Currently pending requestIds: ${pendingIds.join(", ")}`);
98+
} else {
99+
lines.push("No pending node pairing requests are currently visible.");
100+
}
101+
lines.push(`Run ${formatCliCommand("openclaw nodes pending")} to inspect current requests.`);
102+
return lines.join("\n");
103+
}
104+
68105
/** Register node pairing management commands. */
69106
export function registerNodesPairingCommands(nodes: Command) {
70107
nodesCallOpts(
@@ -107,16 +144,24 @@ export function registerNodesPairingCommands(nodes: Command) {
107144
.action(async (requestId: string, opts: NodesRpcOpts) => {
108145
await runNodesCommand("approve", async () => {
109146
const scopes = await resolveApproveScopesForRequest(opts, requestId);
110-
const result = await callNodePairApprovalGatewayCli(
111-
"node.pair.approve",
112-
opts,
113-
{
114-
requestId,
115-
},
116-
{
117-
scopes,
118-
},
119-
);
147+
let result: unknown;
148+
try {
149+
result = await callNodePairApprovalGatewayCli(
150+
"node.pair.approve",
151+
opts,
152+
{
153+
requestId,
154+
},
155+
{
156+
scopes,
157+
},
158+
);
159+
} catch (error) {
160+
if (!isUnknownNodePairRequestIdError(error)) {
161+
throw error;
162+
}
163+
throw new Error(await buildUnknownRequestIdHint(opts, requestId));
164+
}
120165
defaultRuntime.writeJson(result);
121166
});
122167
}),
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Tests for node.pair.approve unknown requestId hint (fixes #94040).
2+
import { Command } from "commander";
3+
import { beforeEach, describe, expect, it, vi } from "vitest";
4+
5+
const mockCallPairApprovalGateway = vi.fn();
6+
const mockParsePairingList = vi.fn(() => ({ pending: [], paired: [] }));
7+
const mockError = vi.fn();
8+
const mockExit = vi.fn();
9+
const mockWriteJson = vi.fn();
10+
const mockWriteStdout = vi.fn();
11+
const mockLog = vi.fn();
12+
13+
vi.mock("./rpc.js", () => ({
14+
callGatewayCli: vi.fn(),
15+
callNodePairApprovalGatewayCli: (...args: unknown[]) =>
16+
mockCallPairApprovalGateway(...args),
17+
nodesCallOpts: (cmd: Command) => cmd,
18+
resolveNodeId: vi.fn(),
19+
}));
20+
21+
vi.mock("./format.js", () => ({
22+
parsePairingList: (...args: unknown[]) => mockParsePairingList(...args),
23+
}));
24+
25+
vi.mock("../../runtime.js", () => ({
26+
defaultRuntime: {
27+
log: (...args: unknown[]) => mockLog(...args),
28+
error: (...args: unknown[]) => mockError(...args),
29+
writeJson: (...args: unknown[]) => mockWriteJson(...args),
30+
writeStdout: (...args: unknown[]) => mockWriteStdout(...args),
31+
exit: (...args: unknown[]) => mockExit(...args),
32+
},
33+
}));
34+
35+
const { registerNodesPairingCommands } =
36+
await import("./register.pairing.js");
37+
38+
function createApproveProgram() {
39+
const program = new Command();
40+
program.name("test");
41+
program.exitOverride();
42+
const nodes = program.command("nodes");
43+
registerNodesPairingCommands(nodes);
44+
return program;
45+
}
46+
47+
function createGatewayUnknownRequestIdError(): Error {
48+
const error = new Error("unknown requestId");
49+
error.name = "GatewayClientRequestError";
50+
(error as Error & { gatewayCode: string }).gatewayCode = "INVALID_REQUEST";
51+
return error;
52+
}
53+
54+
describe("nodes approve unknown requestId hint", () => {
55+
beforeEach(() => {
56+
mockCallPairApprovalGateway.mockReset();
57+
mockParsePairingList.mockReset();
58+
mockParsePairingList.mockReturnValue({ pending: [], paired: [] });
59+
mockError.mockReset();
60+
mockExit.mockReset();
61+
mockWriteJson.mockReset();
62+
mockWriteStdout.mockReset();
63+
mockLog.mockReset();
64+
});
65+
66+
it("shows pending requestIds when approve fails with unknown requestId", async () => {
67+
// First call: resolveApproveScopesForRequest → node.pair.list
68+
mockCallPairApprovalGateway.mockResolvedValueOnce({});
69+
mockParsePairingList.mockReturnValueOnce({
70+
pending: [
71+
{ requestId: "req-abc", nodeId: "n1" },
72+
{ requestId: "req-def", nodeId: "n2" },
73+
],
74+
paired: [],
75+
});
76+
// Second call: approve throws unknown requestId
77+
mockCallPairApprovalGateway.mockRejectedValueOnce(
78+
createGatewayUnknownRequestIdError(),
79+
);
80+
// Third call: buildUnknownRequestIdHint → node.pair.list
81+
mockCallPairApprovalGateway.mockResolvedValueOnce({});
82+
mockParsePairingList.mockReturnValueOnce({
83+
pending: [
84+
{ requestId: "req-abc", nodeId: "n1" },
85+
{ requestId: "req-def", nodeId: "n2" },
86+
],
87+
paired: [],
88+
});
89+
90+
const program = createApproveProgram();
91+
try {
92+
await program.parseAsync(["nodes", "approve", "stale-request"], {
93+
from: "user",
94+
});
95+
} catch {
96+
// exitOverride converts exit(1) to CommanderError
97+
}
98+
99+
expect(mockError).toHaveBeenCalled();
100+
const errorCalls = mockError.mock.calls.flat() as string[];
101+
const errorText = errorCalls.join(" ");
102+
expect(errorText).toContain(
103+
"Unknown node pairing requestId: stale-request",
104+
);
105+
expect(errorText).toContain("req-abc");
106+
expect(errorText).toContain("req-def");
107+
expect(errorText).toContain("openclaw nodes pending");
108+
});
109+
110+
it("shows fallback when list fails during hint building", async () => {
111+
// resolveApproveScopesForRequest list fails silently
112+
mockCallPairApprovalGateway.mockRejectedValueOnce(new Error("gateway down"));
113+
// approve fails with unknown requestId
114+
mockCallPairApprovalGateway.mockRejectedValueOnce(
115+
createGatewayUnknownRequestIdError(),
116+
);
117+
// buildUnknownRequestIdHint list also fails
118+
mockCallPairApprovalGateway.mockRejectedValueOnce(new Error("gateway down"));
119+
120+
const program = createApproveProgram();
121+
try {
122+
await program.parseAsync(["nodes", "approve", "orphan-request"], {
123+
from: "user",
124+
});
125+
} catch {
126+
// exitOverride
127+
}
128+
129+
expect(mockError).toHaveBeenCalled();
130+
const errorCalls = mockError.mock.calls.flat() as string[];
131+
const errorText = errorCalls.join(" ");
132+
expect(errorText).toContain(
133+
"Unknown node pairing requestId: orphan-request",
134+
);
135+
expect(errorText).toContain(
136+
"No pending node pairing requests are currently visible",
137+
);
138+
});
139+
140+
it("does not intercept non-unknown-requestId errors", async () => {
141+
mockCallPairApprovalGateway.mockResolvedValueOnce({});
142+
mockParsePairingList.mockReturnValueOnce({ pending: [], paired: [] });
143+
mockCallPairApprovalGateway.mockRejectedValueOnce(
144+
new Error("some other error"),
145+
);
146+
147+
const program = createApproveProgram();
148+
try {
149+
await program.parseAsync(["nodes", "approve", "req-1"], {
150+
from: "user",
151+
});
152+
} catch {
153+
// exitOverride
154+
}
155+
156+
expect(mockError).toHaveBeenCalled();
157+
const errorCalls = mockError.mock.calls.flat() as string[];
158+
const errorText = errorCalls.join(" ");
159+
expect(errorText).toContain("some other error");
160+
expect(errorText).not.toContain("openclaw nodes pending");
161+
});
162+
});

0 commit comments

Comments
 (0)