Skip to content

Commit 060a541

Browse files
steipetePeter Steinberger
andauthored
fix(canvas): honor paired-node invocation deadlines (#117316)
Co-authored-by: Peter Steinberger <[email protected]>
1 parent aa743c9 commit 060a541

4 files changed

Lines changed: 268 additions & 23 deletions

File tree

‎extensions/canvas/src/cli.test.ts‎

Lines changed: 154 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ function createCanvasCliDeps() {
1919
};
2020
const deps: CanvasCliDependencies = {
2121
defaultRuntime: runtime,
22-
nodesCallOpts: (cmd) =>
22+
nodesCallOpts: (cmd, defaults) =>
2323
cmd
2424
.option("--url <url>", "Gateway WebSocket URL")
2525
.option("--token <token>", "Gateway token")
26-
.option("--timeout <ms>", "Timeout in ms", "10000")
26+
.option("--timeout <ms>", "Timeout in ms", String(defaults?.timeoutMs ?? 10_000))
2727
.option("--json", "Output JSON", false),
2828
runNodesCommand: async (_label, action) => {
2929
await action();
@@ -99,6 +99,12 @@ const canvasAcknowledgementCommands = [
9999
},
100100
] as const;
101101

102+
const canvasInvocationCommands = [
103+
...canvasAcknowledgementCommands,
104+
{ args: ["snapshot"], command: "canvas.snapshot" },
105+
{ args: ["eval", "1 + 1"], command: "canvas.eval" },
106+
] as const;
107+
102108
describe("canvas CLI", () => {
103109
it("registers under nodes and captures a snapshot media path", async () => {
104110
const program = new Command();
@@ -117,7 +123,7 @@ describe("canvas CLI", () => {
117123
{
118124
node: "ios-node",
119125
format: "jpg",
120-
timeout: "10000",
126+
timeout: "60000",
121127
json: false,
122128
invokeTimeout: "20000",
123129
},
@@ -131,6 +137,7 @@ describe("canvas CLI", () => {
131137
},
132138
timeoutMs: 20000,
133139
},
140+
{ transportTimeoutMs: 60_000 },
134141
);
135142
expect(writtenFiles).toHaveLength(1);
136143
const [writtenFile] = writtenFiles;
@@ -222,6 +229,149 @@ describe("canvas CLI", () => {
222229
expect(runtime.log).not.toHaveBeenCalledWith("canvas eval ok");
223230
});
224231

232+
it.each(canvasInvocationCommands)(
233+
"keeps the default $command Gateway deadline longer than the node deadline",
234+
async ({ args, command }) => {
235+
const program = new Command();
236+
program.exitOverride();
237+
const { deps } = createCanvasCliDeps();
238+
239+
registerNodesCanvasCommands(program.command("nodes"), deps);
240+
await program.parseAsync(["nodes", "canvas", ...args, "--node", "ios-node"], {
241+
from: "user",
242+
});
243+
244+
const invokeTimeoutMs = command === "canvas.snapshot" ? 20_000 : 30_000;
245+
const transportTimeoutMs = command === "canvas.snapshot" ? 60_000 : 40_000;
246+
expect(deps.callGatewayCli).toHaveBeenCalledWith(
247+
"node.invoke",
248+
expect.any(Object),
249+
expect.objectContaining({ command, timeoutMs: invokeTimeoutMs }),
250+
{ transportTimeoutMs },
251+
);
252+
},
253+
);
254+
255+
it.each(canvasInvocationCommands)(
256+
"keeps an explicit $command Gateway deadline longer than the node deadline",
257+
async ({ args, command }) => {
258+
const program = new Command();
259+
program.exitOverride();
260+
const { deps } = createCanvasCliDeps();
261+
262+
registerNodesCanvasCommands(program.command("nodes"), deps);
263+
await program.parseAsync(
264+
["nodes", "canvas", ...args, "--node", "ios-node", "--invoke-timeout", "35000"],
265+
{ from: "user" },
266+
);
267+
268+
expect(deps.callGatewayCli).toHaveBeenCalledWith(
269+
"node.invoke",
270+
expect.any(Object),
271+
expect.objectContaining({ command, timeoutMs: 35_000 }),
272+
{ transportTimeoutMs: command === "canvas.snapshot" ? 60_000 : 45_000 },
273+
);
274+
},
275+
);
276+
277+
it("preserves an explicit Gateway timeout longer than the node deadline and grace", async () => {
278+
const program = new Command();
279+
program.exitOverride();
280+
const { deps } = createCanvasCliDeps();
281+
282+
registerNodesCanvasCommands(program.command("nodes"), deps);
283+
await program.parseAsync(
284+
[
285+
"nodes",
286+
"canvas",
287+
"present",
288+
"--node",
289+
"ios-node",
290+
"--invoke-timeout",
291+
"35000",
292+
"--timeout",
293+
"120000",
294+
],
295+
{ from: "user" },
296+
);
297+
298+
expect(deps.callGatewayCli).toHaveBeenCalledWith(
299+
"node.invoke",
300+
expect.objectContaining({ timeout: "120000" }),
301+
expect.objectContaining({ timeoutMs: 35_000 }),
302+
{ transportTimeoutMs: 120_000 },
303+
);
304+
});
305+
306+
it("preserves the snapshot command's existing longer Gateway timeout", async () => {
307+
const program = new Command();
308+
program.exitOverride();
309+
const { deps } = createCanvasCliDeps();
310+
deps.nodesCallOpts = createDefaultCanvasCliDependencies().nodesCallOpts;
311+
312+
registerNodesCanvasCommands(program.command("nodes"), deps);
313+
await program.parseAsync(["nodes", "canvas", "snapshot", "--node", "ios-node"], {
314+
from: "user",
315+
});
316+
317+
expect(deps.callGatewayCli).toHaveBeenCalledWith(
318+
"node.invoke",
319+
expect.objectContaining({ timeout: "60000" }),
320+
expect.objectContaining({ timeoutMs: 20_000 }),
321+
{ transportTimeoutMs: 60_000 },
322+
);
323+
});
324+
325+
it("preserves an invalid explicit Gateway timeout for its existing validation", async () => {
326+
const program = new Command();
327+
program.exitOverride();
328+
const { deps } = createCanvasCliDepsWithDefaultParsers();
329+
deps.callGatewayCli = vi.fn(async (_method, _opts, _params, callOpts) => {
330+
if (callOpts?.transportTimeoutMs !== undefined) {
331+
throw new Error("invalid Gateway timeout was replaced");
332+
}
333+
throw new Error('Invalid --timeout. Received: "20ms".');
334+
});
335+
336+
registerNodesCanvasCommands(program.command("nodes"), deps);
337+
338+
await expect(
339+
program.parseAsync(
340+
["nodes", "canvas", "present", "--node", "ios-node", "--timeout", "20ms"],
341+
{
342+
from: "user",
343+
},
344+
),
345+
).rejects.toThrow('Invalid --timeout. Received: "20ms".');
346+
});
347+
348+
it("caps oversized node and Gateway deadlines to the timer-safe maximum", async () => {
349+
const program = new Command();
350+
program.exitOverride();
351+
const { deps } = createCanvasCliDepsWithDefaultParsers();
352+
353+
registerNodesCanvasCommands(program.command("nodes"), deps);
354+
await program.parseAsync(
355+
[
356+
"nodes",
357+
"canvas",
358+
"present",
359+
"--node",
360+
"ios-node",
361+
"--invoke-timeout",
362+
String(Number.MAX_SAFE_INTEGER),
363+
],
364+
{ from: "user" },
365+
);
366+
367+
expect(deps.callGatewayCli).toHaveBeenCalledWith(
368+
"node.invoke",
369+
expect.any(Object),
370+
expect.objectContaining({ timeoutMs: 2_147_000_000 }),
371+
{ transportTimeoutMs: 2_147_000_000 },
372+
);
373+
});
374+
225375
it.each(canvasAcknowledgementCommands)(
226376
"prints the full successful $command Gateway response with --json",
227377
async ({ args, command }) => {
@@ -328,6 +478,7 @@ describe("canvas CLI", () => {
328478
quality: Number(quality),
329479
}),
330480
}),
481+
{ transportTimeoutMs: 60_000 },
331482
);
332483
});
333484

‎extensions/canvas/src/cli.ts‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
type NodeMatchCandidate,
1313
} from "openclaw/plugin-sdk/gateway-runtime";
1414
import {
15+
addTimerTimeoutGraceMs,
16+
clampPositiveTimerTimeoutMs,
1517
parseStrictFiniteNumber,
1618
parseStrictPositiveInteger,
1719
} from "openclaw/plugin-sdk/number-runtime";
@@ -80,6 +82,9 @@ export type CanvasCliDependencies = {
8082
type CanvasNodeCandidate = NodeMatchCandidate;
8183
type CanvasSnapshotRequestFormat = "png" | "jpeg";
8284

85+
const DEFAULT_CANVAS_NODE_INVOKE_TIMEOUT_MS = 30_000;
86+
const CANVAS_NODE_INVOKE_TRANSPORT_GRACE_MS = 10_000;
87+
8388
function parseCanvasSnapshotRequestFormat(raw: unknown): CanvasSnapshotRequestFormat {
8489
const format = normalizeLowercaseStringOrEmpty(normalizeOptionalString(raw) ?? "jpg");
8590
switch (format) {
@@ -257,18 +262,24 @@ async function invokeCanvas(
257262
command: string,
258263
params?: Record<string, unknown>,
259264
) {
260-
const timeoutMs = deps.parseTimeoutMs(opts.invokeTimeout);
265+
const timeoutMs =
266+
clampPositiveTimerTimeoutMs(
267+
deps.parseTimeoutMs(opts.invokeTimeout) ?? DEFAULT_CANVAS_NODE_INVOKE_TIMEOUT_MS,
268+
) ?? DEFAULT_CANVAS_NODE_INVOKE_TIMEOUT_MS;
261269
const nodeId = await deps.resolveNodeId(opts, normalizeOptionalString(opts.node) ?? "");
262-
return await deps.callGatewayCli(
263-
"node.invoke",
264-
opts,
265-
deps.buildNodeInvokeParams({
266-
nodeId,
267-
command,
268-
params,
269-
timeoutMs: typeof timeoutMs === "number" ? timeoutMs : undefined,
270-
}),
270+
const invokeParams = deps.buildNodeInvokeParams({ nodeId, command, params, timeoutMs });
271+
const configuredGatewayTimeoutMs = parseStrictPositiveInteger(opts.timeout ?? 10_000);
272+
if (configuredGatewayTimeoutMs === undefined) {
273+
// Preserve the existing Gateway parser's actionable invalid --timeout error.
274+
return await deps.callGatewayCli("node.invoke", opts, invokeParams);
275+
}
276+
// Node work owns its deadline; Gateway transport needs extra time to deliver that result.
277+
const transportTimeoutMs = Math.max(
278+
clampPositiveTimerTimeoutMs(configuredGatewayTimeoutMs) ??
279+
DEFAULT_CANVAS_NODE_INVOKE_TIMEOUT_MS,
280+
addTimerTimeoutGraceMs(timeoutMs, CANVAS_NODE_INVOKE_TRANSPORT_GRACE_MS) ?? timeoutMs,
271281
);
282+
return await deps.callGatewayCli("node.invoke", opts, invokeParams, { transportTimeoutMs });
272283
}
273284

274285
/** Prints the complete invocation response for machines or the existing human acknowledgement. */

‎extensions/canvas/src/tool.test.ts‎

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ const VALID_A2UI_V08_JSONL = [
2020
JSON.stringify({ beginRendering: { surfaceId: "main", root: "root" } }),
2121
].join("\n");
2222

23+
const canvasToolInvocationActions = [
24+
{ args: { action: "present" }, command: "canvas.present" },
25+
{ args: { action: "hide" }, command: "canvas.hide" },
26+
{ args: { action: "navigate", url: "https://example.com" }, command: "canvas.navigate" },
27+
{ args: { action: "eval", javaScript: "1 + 1" }, command: "canvas.eval" },
28+
{ args: { action: "snapshot" }, command: "canvas.snapshot" },
29+
{ args: { action: "a2ui_push", jsonl: VALID_A2UI_V08_JSONL }, command: "canvas.a2ui.pushJSONL" },
30+
{ args: { action: "a2ui_reset" }, command: "canvas.a2ui.reset" },
31+
] as const;
32+
2333
const mocks = vi.hoisted(() => ({
2434
callGatewayTool: vi.fn(),
2535
imageResultFromFile: vi.fn(async (params) => ({ content: [], details: params })),
@@ -57,6 +67,58 @@ describe("Canvas tool", () => {
5767
}
5868
});
5969

70+
it.each(canvasToolInvocationActions)(
71+
"forwards the default $command node deadline with Gateway transport grace",
72+
async ({ args, command }) => {
73+
mocks.callGatewayTool.mockResolvedValue({
74+
payload: { format: "png", base64: "aGk=" },
75+
});
76+
77+
await createCanvasTool().execute("tool-call", args);
78+
79+
expect(mocks.callGatewayTool).toHaveBeenCalledWith(
80+
"node.invoke",
81+
{ timeoutMs: 40_000 },
82+
expect.objectContaining({ command, timeoutMs: 30_000 }),
83+
);
84+
expect(mocks.listNodes).toHaveBeenCalledWith({ timeoutMs: undefined });
85+
},
86+
);
87+
88+
it.each(canvasToolInvocationActions)(
89+
"forwards an explicit $command node deadline with Gateway transport grace",
90+
async ({ args, command }) => {
91+
mocks.callGatewayTool.mockResolvedValue({
92+
payload: { format: "png", base64: "aGk=" },
93+
});
94+
95+
await createCanvasTool().execute("tool-call", { ...args, timeoutMs: 120_000 });
96+
97+
expect(mocks.callGatewayTool).toHaveBeenCalledWith(
98+
"node.invoke",
99+
{ timeoutMs: 130_000 },
100+
expect.objectContaining({ command, timeoutMs: 120_000 }),
101+
);
102+
expect(mocks.listNodes).toHaveBeenCalledWith({ timeoutMs: 120_000 });
103+
},
104+
);
105+
106+
it("caps oversized tool invocation and transport deadlines to the timer-safe maximum", async () => {
107+
mocks.callGatewayTool.mockResolvedValue({});
108+
109+
await createCanvasTool().execute("tool-call", {
110+
action: "hide",
111+
timeoutMs: Number.MAX_SAFE_INTEGER,
112+
});
113+
114+
expect(mocks.callGatewayTool).toHaveBeenCalledWith(
115+
"node.invoke",
116+
{ timeoutMs: 2_147_000_000 },
117+
expect.objectContaining({ command: "canvas.hide", timeoutMs: 2_147_000_000 }),
118+
);
119+
expect(mocks.listNodes).toHaveBeenCalledWith({ timeoutMs: Number.MAX_SAFE_INTEGER });
120+
});
121+
60122
it.skipIf(process.platform === "win32")(
61123
"rejects jsonlPath symlinks that resolve outside the workspace",
62124
async () => {
@@ -168,9 +230,10 @@ describe("Canvas tool", () => {
168230

169231
expect(mocks.callGatewayTool).toHaveBeenLastCalledWith(
170232
"node.invoke",
171-
{ timeoutMs: 1500 },
233+
{ timeoutMs: 11_500 },
172234
expect.objectContaining({
173235
command: "canvas.present",
236+
timeoutMs: 1500,
174237
params: {
175238
placement: {
176239
x: 10.5,
@@ -190,9 +253,10 @@ describe("Canvas tool", () => {
190253

191254
expect(mocks.callGatewayTool).toHaveBeenLastCalledWith(
192255
"node.invoke",
193-
{},
256+
{ timeoutMs: 40_000 },
194257
expect.objectContaining({
195258
command: "canvas.snapshot",
259+
timeoutMs: 30_000,
196260
params: {
197261
format: "png",
198262
maxWidth: 800,
@@ -228,11 +292,12 @@ describe("Canvas tool", () => {
228292
expect(mocks.callGatewayTool).toHaveBeenCalledTimes(1);
229293
expect(mocks.callGatewayTool).toHaveBeenCalledWith(
230294
"node.invoke",
231-
{},
295+
{ timeoutMs: 40_000 },
232296
{
233297
nodeId: "node-1",
234298
command: "canvas.a2ui.pushJSONL",
235299
params: { jsonl: VALID_A2UI_V08_JSONL },
300+
timeoutMs: 30_000,
236301
idempotencyKey: expect.any(String),
237302
sessionKey: "agent:main:canvas",
238303
},

0 commit comments

Comments
 (0)