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83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
 * computer tool node-resolution tests.
 *
 * Cover which paired node a call binds to: capability eligibility, explicit
 * node selectors, and the id-before-display-name precedence that keeps input
 * off the wrong machine.
 */
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createDeferredCore } from "../../shared/deferred.js";
import type { ComputerToolTransport } from "./computer-tool.js";

const listNodesMock = vi.fn();
const callGatewayToolMock = vi.fn();
const sleepMock = vi.hoisted(() => vi.fn());
const gatewayComputerStatusMock = vi.hoisted(() => vi.fn());
const TINY_PNG_BASE64 =
  "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII=";

vi.mock("./nodes-utils.js", async (importOriginal) => {
  const actual = await importOriginal<typeof import("./nodes-utils.js")>();
  return { ...actual, listNodes: listNodesMock };
});

vi.mock("./gateway.js", async (importOriginal) => {
  const actual = await importOriginal<typeof import("./gateway.js")>();
  return { ...actual, callGatewayTool: callGatewayToolMock };
});

vi.mock("../../utils/sleep.js", () => ({ sleep: sleepMock }));
vi.mock("./computer-tool-gateway.js", () => ({
  loadGatewayComputerStatus: gatewayComputerStatusMock,
  bindGatewayComputerCleanup: async () => undefined,
}));

const { createComputerTool } = await import("./computer-tool.js");

function macComputerNode(overrides?: Record<string, unknown>) {
  return {
    nodeId: "mac-1",
    displayName: "Studio",
    platform: "macos",
    connected: true,
    commands: ["screen.snapshot", "computer.act"],
    ...overrides,
  };
}

function screenshotPayload(screenIndex = 0, base64 = TINY_PNG_BASE64) {
  return {
    payload: {
      format: "png",
      base64,
      displayFrameId: `display-${screenIndex}-frame`,
      width: 1280,
      height: 800,
      screenIndex,
    },
  };
}

describe("createComputerTool node resolution", () => {
  beforeEach(() => {
    listNodesMock.mockReset();
    callGatewayToolMock.mockReset();
    gatewayComputerStatusMock.mockReset();
    gatewayComputerStatusMock.mockResolvedValue({ configured: false, available: false });
    sleepMock.mockReset();
    sleepMock.mockResolvedValue(undefined);
  });

  it("errors when no computer-capable node is connected", async () => {
    listNodesMock.mockResolvedValue([
      macComputerNode({ connected: false }),
      { nodeId: "phone", platform: "ios", connected: true, commands: [] },
    ]);
    const tool = createComputerTool({ modelHasVision: true });
    await expect(tool.execute("call", { action: "screenshot" })).rejects.toThrow(
      /no connected computer-capable node/,
    );
    expect(callGatewayToolMock).not.toHaveBeenCalled();
  });

  it("keeps a bound session desktop's frames, actions, and cleanup on its transport", async () => {
    listNodesMock.mockResolvedValue([macComputerNode()]);
    callGatewayToolMock.mockResolvedValue(screenshotPayload());
    const computerUse: NonNullable<ComputerToolTransport["computerUse"]> = {
      contractVersion: 2,
      provider: { id: "fixture", label: "Fixture", generation: "generation-1" },
      actions: ["screenshot", "left_click", "get_window_state"],
      targets: ["screen", "window"],
      deliveryModes: ["foreground"],
      observations: ["image"],
      features: { recording: false, agentCursor: false, multiDisplay: false },
    };
    const resolveNode = vi.fn<ComputerToolTransport["resolveNode"]>(async (query) => {
      if (query !== undefined && query !== "session-desktop") {
        throw new Error("Computer target is bound to this session desktop");
      }
      return { nodeId: "session-desktop" };
    });
    const invoke = vi.fn<ComputerToolTransport["invoke"]>(async ({ command }) =>
      command === "screen.snapshot" ? screenshotPayload().payload : { ok: true },
    );
    let cleanup: ((reason: string) => Promise<void>) | undefined;
    const tool = createComputerTool({
      modelHasVision: true,
      transport: { computerUse, resolveNode, invoke },
      registerRunCleanup: (registered) => {
        cleanup = registered;
      },
    });
    expect(tool.description).toContain("this session's desktop");
    expect(tool.description).toContain("get_window_state");
    const selectors = ["target", "node", "gatewayUrl", "gatewayToken", "timeoutMs"];
    const schema = tool.parameters as { properties: Record<string, unknown> };
    expect(schema.properties.action).toMatchObject({ enum: [...computerUse.actions, "wait"] });
    for (const selector of selectors) {
      expect(schema.properties).not.toHaveProperty(selector);
    }

    const screenshot = await tool.execute("observe", { action: "wait", duration: 0 });
    expect(sleepMock).toHaveBeenCalledWith(0, undefined);
    expect(screenshot.details).toMatchObject({ node: "session-desktop" });
    const frameId = (screenshot.details as { frameId: string }).frameId;
    await tool.execute("click", { action: "left_click", coordinate: [0, 0], frameId });
    await expect(
      tool.execute("wrong-desktop", { action: "screenshot", node: "mac-1" }),
    ).rejects.toThrow("bound to this session desktop");
    await expect(
      tool.execute("wrong-host", { action: "screenshot", target: "gateway" }),
    ).rejects.toThrow("bound to this session's desktop");
    expect(gatewayComputerStatusMock).not.toHaveBeenCalled();
    await cleanup?.("completion");

    expect(invoke.mock.calls.map(([request]) => request.command)).toEqual([
      "screen.snapshot",
      "computer.act",
      "screen.snapshot",
      "computer.act",
    ]);
    const snapshotRequest = invoke.mock.calls[0]?.[0];
    expect(invoke.mock.calls[1]?.[0]).toMatchObject({
      nodeId: "session-desktop",
      commandParams: {
        action: "left_click",
        displayFrameId: "display-0-frame",
        x: 0,
        y: 0,
      },
    });
    expect(invoke.mock.calls[3]?.[0]).toMatchObject({
      nodeId: "session-desktop",
      commandParams: {
        action: "__close_execution",
        executionId: snapshotRequest?.commandParams.executionId,
        reason: "completion",
      },
    });
    expect(listNodesMock).not.toHaveBeenCalled();
    expect(callGatewayToolMock).not.toHaveBeenCalled();
    expect(tool.description).toContain("this session's desktop");
    expect(tool.description).toContain("get_window_state");
    for (const selector of selectors) {
      expect(schema.properties).not.toHaveProperty(selector);
    }
    await expect(tool.execute("after-close", { action: "wait", duration: 0 })).rejects.toThrow(
      "computer: execution is closed",
    );
    expect(invoke).toHaveBeenCalledTimes(4);
  });

  it("fences retained and queued calls while awaiting an in-flight capture before cleanup", async () => {
    const capture = createDeferredCore<unknown>();
    const captureStarted = createDeferredCore();
    const invoke = vi.fn<ComputerToolTransport["invoke"]>(async ({ command }) => {
      if (command === "screen.snapshot") {
        captureStarted.resolve();
        return await capture.promise;
      }
      return { ok: true };
    });
    let cleanup: ((reason: string) => Promise<void>) | undefined;
    const tool = createComputerTool({
      modelHasVision: true,
      transport: { resolveNode: async () => ({ nodeId: "session-desktop" }), invoke },
      registerRunCleanup: (registered) => {
        cleanup = registered;
      },
    });
    if (!cleanup) {
      throw new Error("Computer execution did not register cleanup");
    }

    const first = tool.execute("capture", { action: "screenshot" });
    await captureStarted.promise;
    const queued = expect(
      tool.execute("queued", { action: "type", text: "unsafe" }),
    ).rejects.toThrow("computer: execution is closed");
    const closing = cleanup("cancellation");
    const retained = expect(
      tool.execute("retained", { action: "type", text: "unsafe" }),
    ).rejects.toThrow("computer: execution is closed");
    expect(invoke).toHaveBeenCalledOnce();
    capture.resolve(screenshotPayload().payload);
    await first;
    await Promise.all([queued, retained, closing, cleanup("cancellation")]);

    expect(invoke).toHaveBeenCalledTimes(2);
    expect(invoke.mock.calls[1]?.[0]).toMatchObject({
      nodeId: "session-desktop",
      command: "computer.act",
      commandParams: { action: "__close_execution", reason: "cancellation" },
    });
    expect(callGatewayToolMock).not.toHaveBeenCalled();
    expect(listNodesMock).not.toHaveBeenCalled();
  });

  it.each(["paired", "session"] as const)(
    "reports cleanup failure only to the bound owner of a %s desktop",
    async (targetScope) => {
      const failure = new Error("desktop disconnected during cleanup");
      listNodesMock.mockResolvedValue([macComputerNode()]);
      callGatewayToolMock.mockImplementation(async (_method, _opts, body) => {
        if (body.command === "computer.act") {
          throw failure;
        }
        return screenshotPayload();
      });
      const invoke = vi.fn<ComputerToolTransport["invoke"]>(async ({ command }) => {
        if (command === "computer.act") {
          throw failure;
        }
        return screenshotPayload().payload;
      });
      let cleanup: ((reason: string) => Promise<void>) | undefined;
      const tool = createComputerTool({
        modelHasVision: true,
        transport:
          targetScope === "session"
            ? { resolveNode: async () => ({ nodeId: "session-desktop" }), invoke }
            : undefined,
        registerRunCleanup: (registered) => {
          cleanup = registered;
        },
      });
      if (!cleanup) {
        throw new Error("Computer execution did not register cleanup");
      }
      await tool.execute("observe", { action: "screenshot" });

      if (targetScope === "session") {
        await expect(cleanup("completion")).rejects.toMatchObject({
          message: "computer: session desktop cleanup failed",
          errors: [failure],
        });
      } else {
        await expect(cleanup("completion")).resolves.toBeUndefined();
      }
    },
  );

  it.each(["windows", "linux"])("resolves and executes on a capable %s node", async (platform) => {
    const nodeId = `${platform}-1`;
    listNodesMock.mockResolvedValue([
      {
        nodeId,
        displayName: `${platform} desktop`,
        platform,
        connected: true,
        commands: ["computer.act", "screen.snapshot"],
      },
    ]);
    callGatewayToolMock.mockImplementation(async (_method, _opts, body) =>
      (body as { command?: string }).command === "computer.act"
        ? { payload: { ok: true } }
        : screenshotPayload(),
    );
    const tool = createComputerTool({ modelHasVision: true });

    await expect(tool.execute("call", { action: "type", text: "hello" })).resolves.toBeDefined();
    expect(callGatewayToolMock).toHaveBeenCalledWith(
      "node.invoke",
      expect.anything(),
      expect.objectContaining({ nodeId, command: "computer.act" }),
      { signal: undefined },
    );
  });

  it("rejects a named node that is not computer-capable", async () => {
    listNodesMock.mockResolvedValue([
      { nodeId: "mac-2", platform: "macos", connected: true, commands: ["screen.snapshot"] },
    ]);
    const tool = createComputerTool({ modelHasVision: true });
    await expect(tool.execute("call", { action: "screenshot", node: "mac-2" })).rejects.toThrow(
      /not computer-capable/,
    );
  });

  it("reports the eligible node ids when an exact id names an ineligible machine", async () => {
    listNodesMock.mockResolvedValue([
      macComputerNode({ nodeId: "mac-disabled", commands: ["screen.snapshot"] }),
      macComputerNode({ nodeId: "mac-ready" }),
    ]);
    const tool = createComputerTool({ modelHasVision: true });
    await expect(
      tool.execute("call", { action: "screenshot", node: "mac-disabled" }),
    ).rejects.toThrow(/node "mac-disabled" is not computer-capable.*eligible node ids: mac-ready/);
    expect(callGatewayToolMock).not.toHaveBeenCalled();
  });

  it("never redirects an ineligible exact id to an eligible node with that display name", async () => {
    listNodesMock.mockResolvedValue([
      macComputerNode({
        nodeId: "requested-desktop",
        displayName: "Disabled",
        commands: ["screen.snapshot"],
      }),
      macComputerNode({ nodeId: "mac-ready", displayName: "requested-desktop" }),
    ]);
    const tool = createComputerTool({ modelHasVision: true });
    await expect(
      tool.execute("call", { action: "screenshot", node: "requested-desktop" }),
    ).rejects.toThrow(/node "requested-desktop" is not computer-capable/);
    expect(callGatewayToolMock).not.toHaveBeenCalled();
  });

  it("rejects a case-insensitive ineligible id before an eligible display-name match", async () => {
    listNodesMock.mockResolvedValue([
      macComputerNode({
        nodeId: "Requested-Desktop",
        displayName: "Disabled",
        commands: ["screen.snapshot"],
      }),
      macComputerNode({ nodeId: "mac-ready", displayName: "requested-desktop" }),
    ]);
    const tool = createComputerTool({ modelHasVision: true });
    await expect(
      tool.execute("call", { action: "screenshot", node: "requested-desktop" }),
    ).rejects.toThrow(/is not computer-capable/);
    expect(callGatewayToolMock).not.toHaveBeenCalled();
  });

  it("rejects an ambiguous eligible display-name match across current clients", async () => {
    listNodesMock.mockResolvedValue([
      macComputerNode({
        nodeId: "mac-a",
        displayName: "Shared Desktop",
        clientId: "openclaw-macos",
      }),
      macComputerNode({ nodeId: "mac-b", displayName: "Shared Desktop", clientId: "node-host" }),
    ]);
    const tool = createComputerTool({ modelHasVision: true });
    await expect(
      tool.execute("call", { action: "screenshot", node: "Shared Desktop" }),
    ).rejects.toThrow(
      /ambiguous node: Shared Desktop.*node=mac-a.*node=mac-b.*eligible computer-capable node ids: mac-a, mac-b/,
    );
    expect(callGatewayToolMock).not.toHaveBeenCalled();
  });

  it("resolves an eligible node by display name", async () => {
    listNodesMock.mockResolvedValue([
      macComputerNode({ nodeId: "mac-other", displayName: "Other Desktop" }),
      macComputerNode({ nodeId: "mac-ready", displayName: "Studio Desktop" }),
    ]);
    callGatewayToolMock.mockResolvedValue(screenshotPayload());
    const tool = createComputerTool({ modelHasVision: true });

    await expect(
      tool.execute("call", { action: "screenshot", node: "Studio Desktop" }),
    ).resolves.toBeDefined();
    expect(callGatewayToolMock).toHaveBeenCalledWith(
      "node.invoke",
      expect.anything(),
      expect.objectContaining({ nodeId: "mac-ready", command: "screen.snapshot" }),
      { signal: undefined },
    );
  });

  it("selects an exact eligible id over an ineligible display-name collision", async () => {
    listNodesMock.mockResolvedValue([
      macComputerNode({ nodeId: "mac-ready", displayName: "Studio" }),
      macComputerNode({ nodeId: "mac-off", displayName: "mac-ready", commands: [] }),
    ]);
    callGatewayToolMock.mockResolvedValue(screenshotPayload());
    const tool = createComputerTool({ modelHasVision: true });

    await expect(
      tool.execute("call", { action: "screenshot", node: "mac-ready" }),
    ).resolves.toBeDefined();
    expect(callGatewayToolMock).toHaveBeenCalledWith(
      "node.invoke",
      expect.anything(),
      expect.objectContaining({ nodeId: "mac-ready", command: "screen.snapshot" }),
      { signal: undefined },
    );
  });

  it("requires an explicit node when several computer-capable nodes are connected", async () => {
    listNodesMock.mockResolvedValue([
      macComputerNode({ nodeId: "mac-a" }),
      macComputerNode({ nodeId: "mac-b" }),
    ]);
    const tool = createComputerTool({ modelHasVision: true });
    await expect(tool.execute("call", { action: "screenshot" })).rejects.toThrow(
      /multiple computer-capable nodes connected; pass node explicitly: mac-a, mac-b/,
    );
    expect(callGatewayToolMock).not.toHaveBeenCalled();
  });

  it("rejects a node advertising computer.act without screen.snapshot", async () => {
    listNodesMock.mockResolvedValue([
      { nodeId: "desktop-1", platform: "windows", connected: true, commands: ["computer.act"] },
    ]);
    const tool = createComputerTool({ modelHasVision: true });
    await expect(tool.execute("call", { action: "screenshot", node: "desktop-1" })).rejects.toThrow(
      /advertising computer\.act and screen\.snapshot/,
    );
    expect(callGatewayToolMock).not.toHaveBeenCalled();
  });
});