|
| 1 | +/** |
| 2 | + * Runtime proof for the plain node plugin tool invocation budget. |
| 3 | + * Drives a real Gateway and a real paired node that accepts a command and never |
| 4 | + * answers, then asserts the agent receives the Gateway's structured timeout with |
| 5 | + * its dispatch provenance instead of a client-side gateway timeout. |
| 6 | + */ |
| 7 | +import { describe, expect, it } from "vitest"; |
| 8 | +import type { WebSocket } from "ws"; |
| 9 | +import { createNodePluginTools } from "../agents/node-plugin-tools.js"; |
| 10 | +import { approveDevicePairing } from "../infra/device-pairing-approval.js"; |
| 11 | +import { approveNodePairing, requestNodePairing } from "../infra/device-pairing-node.js"; |
| 12 | +import { listDevicePairing } from "../infra/device-pairing.js"; |
| 13 | +import { getActiveRuntimePluginRegistry } from "../plugins/active-runtime-registry.js"; |
| 14 | +import { captureEnv, setTestEnvValue } from "../test-utils/env.js"; |
| 15 | +import { GATEWAY_CLIENT_MODES, GATEWAY_CLIENT_NAMES } from "../utils/message-channel.js"; |
| 16 | +import { replaceConnectedNodePluginTools } from "./node-plugin-tool-snapshot.js"; |
| 17 | +import { connectGatewayClient } from "./test-helpers.e2e.js"; |
| 18 | +import { installGatewayTestHooks, rpcReq } from "./test-helpers.js"; |
| 19 | +import { installConnectedControlUiServerSuite } from "./test-with-server.js"; |
| 20 | + |
| 21 | +installGatewayTestHooks({ scope: "suite" }); |
| 22 | + |
| 23 | +const NODE_DISPLAY_NAME = "Slow Studio Node"; |
| 24 | +const NODE_COMMAND = "remote.echo"; |
| 25 | +// One Gateway deadline (30s) plus the caller grace, plus room for pairing setup. |
| 26 | +const PROOF_TIMEOUT_MS = 240_000; |
| 27 | + |
| 28 | +let ws: WebSocket; |
| 29 | +let port = 0; |
| 30 | + |
| 31 | +installConnectedControlUiServerSuite((started) => { |
| 32 | + ws = started.ws; |
| 33 | + port = started.port; |
| 34 | +}); |
| 35 | + |
| 36 | +/** Registers the node-invoke policy a node-hosted plugin would contribute. */ |
| 37 | +function installNodeCommandPolicy(): void { |
| 38 | + const registry = getActiveRuntimePluginRegistry(); |
| 39 | + if (!registry) { |
| 40 | + throw new Error("active plugin registry is required for node invoke policy tests"); |
| 41 | + } |
| 42 | + if (registry.nodeInvokePolicies.some((entry) => entry.policy.commands.includes(NODE_COMMAND))) { |
| 43 | + return; |
| 44 | + } |
| 45 | + registry.nodeInvokePolicies.push({ |
| 46 | + pluginId: "remote-demo", |
| 47 | + pluginName: "Remote Demo", |
| 48 | + source: "test", |
| 49 | + rootDir: "extensions/remote-demo", |
| 50 | + pluginConfig: {}, |
| 51 | + policy: { |
| 52 | + commands: [NODE_COMMAND], |
| 53 | + defaultPlatforms: ["ios", "android", "macos", "windows", "unknown"], |
| 54 | + foregroundRestrictedOnIos: false, |
| 55 | + handle: (ctx) => ctx.invokeNode(), |
| 56 | + }, |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +describe("plain node plugin tool invocation deadline", () => { |
| 61 | + it( |
| 62 | + "returns the Gateway timeout with dispatch provenance when the node never answers", |
| 63 | + async () => { |
| 64 | + installNodeCommandPolicy(); |
| 65 | + const dispatchedToNode: { timeoutMs?: number }[] = []; |
| 66 | + |
| 67 | + const connectNode = async () => |
| 68 | + await connectGatewayClient({ |
| 69 | + url: `ws://127.0.0.1:${port}`, |
| 70 | + token: process.env.OPENCLAW_GATEWAY_TOKEN, |
| 71 | + role: "node", |
| 72 | + clientName: GATEWAY_CLIENT_NAMES.NODE_HOST, |
| 73 | + clientVersion: "1.0.0", |
| 74 | + clientDisplayName: NODE_DISPLAY_NAME, |
| 75 | + platform: "macos", |
| 76 | + mode: GATEWAY_CLIENT_MODES.NODE, |
| 77 | + scopes: [], |
| 78 | + commands: [NODE_COMMAND], |
| 79 | + // The node accepts the forwarded command and never sends a result. |
| 80 | + onEvent: (event) => { |
| 81 | + if (event.event === "node.invoke.request") { |
| 82 | + dispatchedToNode.push(event.payload as { timeoutMs?: number }); |
| 83 | + } |
| 84 | + }, |
| 85 | + timeoutMessage: "timeout waiting for node to connect", |
| 86 | + }); |
| 87 | + |
| 88 | + const connectWithDevicePairing = async () => { |
| 89 | + try { |
| 90 | + return await connectNode(); |
| 91 | + } catch (error) { |
| 92 | + const message = error instanceof Error ? error.message : String(error); |
| 93 | + if (!message.includes("pairing required")) { |
| 94 | + throw error; |
| 95 | + } |
| 96 | + const pairings = await listDevicePairing(); |
| 97 | + for (const pending of pairings.pending) { |
| 98 | + await approveDevicePairing(pending.requestId, { |
| 99 | + callerScopes: pending.scopes ?? ["operator.admin"], |
| 100 | + }); |
| 101 | + } |
| 102 | + return await connectNode(); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + const findNodeId = async () => { |
| 107 | + const listed = await rpcReq<{ |
| 108 | + nodes?: { nodeId: string; displayName?: string; connected?: boolean }[]; |
| 109 | + }>(ws, "node.list", {}); |
| 110 | + return (listed.payload?.nodes ?? []).find( |
| 111 | + (entry) => entry.connected && entry.displayName === NODE_DISPLAY_NAME, |
| 112 | + )?.nodeId; |
| 113 | + }; |
| 114 | + |
| 115 | + const provisional = await connectWithDevicePairing(); |
| 116 | + const provisionalNodeId = await findNodeId(); |
| 117 | + if (!provisionalNodeId) { |
| 118 | + throw new Error("expected a connected node id before pairing"); |
| 119 | + } |
| 120 | + await provisional.stopAndWait(); |
| 121 | + const pairing = await requestNodePairing({ |
| 122 | + nodeId: provisionalNodeId, |
| 123 | + displayName: NODE_DISPLAY_NAME, |
| 124 | + platform: "macos", |
| 125 | + commands: [NODE_COMMAND], |
| 126 | + }); |
| 127 | + await approveNodePairing(pairing.request.requestId, { |
| 128 | + callerScopes: ["operator.admin", "operator.write"], |
| 129 | + }); |
| 130 | + const node = await connectNode(); |
| 131 | + // The shared Gateway fixture snapshots no such key, so leaving it set would |
| 132 | + // point later tests in this worker at this stopped ephemeral server. |
| 133 | + const gatewayUrlEnv = captureEnv(["OPENCLAW_GATEWAY_URL"]); |
| 134 | + |
| 135 | + try { |
| 136 | + const nodeId = (await findNodeId()) ?? provisionalNodeId; |
| 137 | + replaceConnectedNodePluginTools({ |
| 138 | + nodeId, |
| 139 | + displayName: NODE_DISPLAY_NAME, |
| 140 | + tools: [ |
| 141 | + { |
| 142 | + descriptor: { |
| 143 | + pluginId: "remote-demo", |
| 144 | + name: "remote_echo", |
| 145 | + description: "Echo through a remote node", |
| 146 | + parameters: { type: "object", properties: { text: { type: "string" } } }, |
| 147 | + command: NODE_COMMAND, |
| 148 | + }, |
| 149 | + registered: false, |
| 150 | + }, |
| 151 | + ], |
| 152 | + }); |
| 153 | + setTestEnvValue("OPENCLAW_GATEWAY_URL", `ws://127.0.0.1:${port}`); |
| 154 | + |
| 155 | + const tool = createNodePluginTools({})[0]; |
| 156 | + if (!tool) { |
| 157 | + throw new Error("expected a materialized node plugin tool"); |
| 158 | + } |
| 159 | + |
| 160 | + const failure = await tool |
| 161 | + .execute("deadline-proof-call", { text: "ping" }) |
| 162 | + .then(() => undefined) |
| 163 | + .catch((error: unknown) => error); |
| 164 | + |
| 165 | + // The command really reached the node, so the answer must not claim retry safety. |
| 166 | + expect(dispatchedToNode).toHaveLength(1); |
| 167 | + expect(dispatchedToNode[0]?.timeoutMs).toBeGreaterThan(0); |
| 168 | + |
| 169 | + const message = failure instanceof Error ? failure.message : String(failure); |
| 170 | + // Without the payload budget the Gateway arms no deadline and this reads |
| 171 | + // "gateway timeout after 30000ms" instead, with no provenance to inspect. |
| 172 | + expect(message).toContain("node invoke timed out"); |
| 173 | + expect(failure).toMatchObject({ details: { nodeCommandDispatched: true } }); |
| 174 | + } finally { |
| 175 | + gatewayUrlEnv.restore(); |
| 176 | + await node.stopAndWait(); |
| 177 | + } |
| 178 | + }, |
| 179 | + PROOF_TIMEOUT_MS, |
| 180 | + ); |
| 181 | +}); |
0 commit comments