Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
27.7 KB
Raw Blame
import { spawn, spawnSync } from "node:child_process";
import { copyFile, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { createInterface } from "node:readline";
import { createServer } from "node:net";
import { randomBytes } from "node:crypto";
import { createRequire } from "node:module";
import { needs, SkipError } from "@omnirush/env";
import type { Place, Seed } from "@omnirush/env";
import { desktop } from "@omnirush/hosts";

function record(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}

function pipeClient(executable: string, args: string[], environment: Record<string, string> = {}) {
  const child = spawn(executable, args, { stdio: ["pipe", "pipe", "pipe"], env: { ...process.env, ...environment } });
  let stderr = "";
  child.stderr.on("data", (chunk: Buffer) => { stderr = (stderr + chunk.toString()).slice(-2000); });
  const pending = new Map<number, { resolve(value: unknown): void; reject(error: Error): void; timer: NodeJS.Timeout }>();
  let nextId = 0;
  const lines = createInterface({ input: child.stdout });
  lines.on("line", (line) => {
    const message: unknown = JSON.parse(line);
    if (!record(message) || typeof message.id !== "number") return;
    const request = pending.get(message.id);
    if (!request) return;
    pending.delete(message.id); clearTimeout(request.timer);
    if (message.error) request.reject(new Error(JSON.stringify(message.error)));
    else request.resolve(message.result);
  });
  const fail = () => {
    for (const value of pending.values()) { clearTimeout(value.timer); value.reject(new Error(`Fixture process exited: ${stderr}`)); }
    pending.clear();
  };
  child.on("error", fail); child.on("exit", fail);
  return {
    pid: child.pid,
    cancelPending() {
      for (const requestId of pending.keys()) child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", method: "notifications/cancelled", params: { requestId } })}\n`);
    },
    request(method: string, params: Record<string, unknown> = {}, timeoutMs = 15_000) {
      const id = ++nextId;
      return new Promise<unknown>((resolve, reject) => {
        const timer = setTimeout(() => { pending.delete(id); reject(new Error(`Timed out: ${method}`)); }, timeoutMs);
        pending.set(id, { resolve, reject, timer });
        child.stdin.write(`${JSON.stringify({ jsonrpc: "2.0", id, method, params })}\n`);
      });
    },
    async close() {
      child.stdin.end(); lines.close();
      await new Promise<void>((resolve) => {
        if (child.exitCode !== null || child.signalCode !== null) { resolve(); return; }
        const timeout = setTimeout(() => { child.kill("SIGKILL"); resolve(); }, 2000);
        child.once("exit", () => { clearTimeout(timeout); resolve(); });
      });
      fail();
    },
  };
}

// Electron's Windows GUI executable does not provide a reliable stdio channel.
// Use an authenticated loopback socket for the disposable witness only.
async function nativeFixtureClient(executable: string, args: string[]) {
  const token = randomBytes(32).toString("hex");
  const server = createServer();
  await new Promise<void>((resolve, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", resolve); });
  const address = server.address();
  if (!address || typeof address === "string") throw new Error("No fixture endpoint.");
  const child = spawn(executable, args, { stdio: ["ignore", "ignore", "pipe"], env: { ...process.env, OMNIRUSH_FIXTURE_PORT: String(address.port), OMNIRUSH_FIXTURE_TOKEN: token } });
  let stderr = "";
  child.stderr.on("data", (chunk: Buffer) => { stderr = (stderr + chunk.toString()).slice(-2000); });
  const pending = new Map<number, { resolve(value: unknown): void; reject(error: Error): void; timer: NodeJS.Timeout }>();
  let nextId = 0;
  const socket = await new Promise<import("node:net").Socket>((resolve, reject) => {
    const timer = setTimeout(() => { server.close(); child.kill(); reject(new Error("Fixture connection timed out: " + stderr)); }, 15_000);
    child.once("error", (error) => { clearTimeout(timer); server.close(); reject(error); });
    child.once("exit", () => { clearTimeout(timer); server.close(); reject(new Error("Fixture exited: " + stderr)); });
    server.on("connection", (connected) => {
      const lines = createInterface({ input: connected });
      let authenticated = false;
      lines.on("line", (line) => {
        let message: unknown;
        try { message = JSON.parse(line); } catch { connected.destroy(); return; }
        if (!record(message)) { connected.destroy(); return; }
        if (!authenticated) {
          if (message.token !== token) { connected.destroy(); return; }
          authenticated = true; clearTimeout(timer); server.close(); resolve(connected); return;
        }
        if (typeof message.id !== "number") return;
        const request = pending.get(message.id); if (!request) return;
        pending.delete(message.id); clearTimeout(request.timer);
        if (message.error) request.reject(new Error(JSON.stringify(message.error))); else request.resolve(message.result);
      });
      connected.on("error", () => connected.destroy());
      connected.once("close", () => { lines.close(); for (const request of pending.values()) { clearTimeout(request.timer); request.reject(new Error("Fixture connection closed: " + stderr)); } pending.clear(); });
    });
  });
  return {
    pid: child.pid,
    request(method: string, params: Record<string, unknown> = {}, timeoutMs = 15_000) {
      const id = ++nextId;
      return new Promise<unknown>((resolve, reject) => {
        const timer = setTimeout(() => { pending.delete(id); reject(new Error("Fixture operation timed out: " + method + "; " + stderr)); }, timeoutMs);
        pending.set(id, { resolve, reject, timer }); socket.write(JSON.stringify({ id, method, params }) + "\n");
      });
    },
    async close() {
      socket.end();
      await new Promise<void>((resolve) => {
        if (child.exitCode !== null || child.signalCode !== null) { resolve(); return; }
        const timer = setTimeout(() => { child.kill(); resolve(); }, 2000);
        child.once("exit", () => { clearTimeout(timer); resolve(); });
      });
      socket.destroy();
    },
  };
}

export function toolState(value: unknown): Record<string, unknown> {
  if (!record(value) || !Array.isArray(value.content)) throw new Error("No tool content");
  const text = value.content.find((item: unknown) => record(item) && item.type === "text");
  if (!record(text) || typeof text.text !== "string") throw new Error("No tool state");
  const result: unknown = JSON.parse(text.text);
  if (!record(result)) throw new Error("Invalid tool state");
  return result;
}

export async function computerUseWorld(_seed: Seed, { place }: { place: Place }) {
  // Do not silently run local Mac resources after the CLI selected Daytona.
  if (place.kind !== "local" || process.platform !== "darwin") throw new SkipError("macOS native desktop placement; the selected host cannot run AppKit");
  needs({ commands: ["swift", "swiftc"] });
  const root = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
  const native = join(root, "packages/computer-use/native");
  const build = spawnSync("swift", ["build", "--package-path", native, "--product", "ComputerUse"], { encoding: "utf8", timeout: 120_000 });
  if (build.status !== 0) throw new Error(`Computer Use build failed: ${build.stderr.slice(-4000)}`);
  const executable = join(native, ".build/debug/ComputerUse");
  const checked = spawnSync(executable, ["--check"], { encoding: "utf8", timeout: 5000 });
  const permissions: unknown = JSON.parse(checked.stdout);
  if (!record(permissions) || permissions.ok !== true) throw new SkipError("macOS Accessibility and Screen Recording granted to the native helper by a person");
  const directory = await mkdtemp(join(tmpdir(), "omnirush-computer-use-"));
  const contents = join(directory, "Computer Use Fixture.app/Contents");
  await mkdir(join(contents, "MacOS"), { recursive: true });
  await writeFile(join(contents, "Info.plist"), `<?xml version="1.0"?><plist version="1.0"><dict><key>CFBundleIdentifier</key><string>org.example.omnirush.computer-use-fixture</string><key>CFBundleName</key><string>Computer Use Fixture</string><key>CFBundleExecutable</key><string>Fixture</string><key>CFBundlePackageType</key><string>APPL</string><key>CFBundleVersion</key><string>1</string><key>CFBundleShortVersionString</key><string>1.0</string><key>LSMinimumSystemVersion</key><string>14.0</string></dict></plist>`);
  const fixtureExecutable = join(contents, "MacOS/Fixture");
  const compiled = spawnSync("swiftc", ["-target", `${process.arch === "arm64" ? "arm64" : "x86_64"}-apple-macosx14.0`, "-parse-as-library", join(root, "evals/packages/labs/fixtures/computer-use-app.swift"), "-o", fixtureExecutable], { encoding: "utf8", timeout: 90_000 });
  if (compiled.status !== 0) { await rm(directory, { recursive: true }); throw new Error(compiled.stderr); }
  const fixture = pipeClient(fixtureExecutable, []);
  const helper = pipeClient(executable, ["mcp"]);
  const peer = pipeClient(executable, ["mcp"]);
  const close = async () => { await Promise.all([helper.close(), peer.close(), fixture.close()]); await rm(directory, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 }); };
  try {
    await fixture.request("state");
    const fixturePermissions = await fixture.request("permissions");
    if (!record(fixturePermissions) || fixturePermissions.accessibility !== true) throw new SkipError("Accessibility permission for the disposable native person-input fixture");
    await helper.request("initialize", { protocolVersion: "2025-11-25", clientInfo: { name: "native-journey", version: "1" }, capabilities: {} });
    await peer.request("initialize", { protocolVersion: "2025-11-25", clientInfo: { name: "peer-journey", version: "1" }, capabilities: {} });
    return {
      async launchableFixture() {
        const appId = "org.example.omnirush.launch-fixture." + directory.split("-").at(-1);
        // Launch Services deliberately excludes apps in the OS temporary directory.
        const appPath = join(root, "evals/results/.native-apps", `${appId}.app`);
        const launchContents = join(appPath, "Contents");
        await mkdir(join(launchContents, "MacOS"), { recursive: true });
        await copyFile(fixtureExecutable, join(launchContents, "MacOS/Fixture"));
        await writeFile(join(launchContents, "Info.plist"), (await readFile(join(contents, "Info.plist"), "utf8")).replace("org.example.omnirush.computer-use-fixture", appId));
        const register = "/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister";
        const result = spawnSync(register, ["-f", appPath], { encoding: "utf8", timeout: 10_000 });
        if (result.status !== 0) throw new Error(`Could not register disposable launch fixture: ${result.stderr}`);
        return { appId, async [Symbol.asyncDispose]() {
          const discovery = toolState(await helper.request("tools/call", { name: "computer_discover", arguments: {} }));
          if (Array.isArray(discovery.apps)) for (const entry of discovery.apps) {
            if (record(entry) && entry.app_id === appId && typeof entry.pid === "number") {
              try { process.kill(entry.pid, "SIGTERM"); } catch { /* Already closed. */ }
            }
          }
          spawnSync(register, ["-u", appPath], { timeout: 10_000 });
          await rm(appPath, { recursive: true, force: true });
        } };
      },
      async hostedClient(command: unknown) {
        if (!Array.isArray(command) || command.length !== 3 || !command.every((part): part is string => typeof part === "string") || command[1] !== "relay") throw new Error("Desktop did not provide the hosted relay command");
        const client = pipeClient(command[0], command.slice(1));
        await client.request("initialize", { protocolVersion: "2025-11-25", clientInfo: { name: "desktop-journey", version: "1" }, capabilities: {} });
        return {
          call: (name: string, args: Record<string, unknown> = {}) => client.request("tools/call", { name, arguments: args }, 60_000),
          request: client.request,
          cancelPending: client.cancelPending,
          close: client.close,
          [Symbol.asyncDispose]: () => client.close(),
        };
      },
      async electronFixture() {
        const main = join(directory, "electron-fixture.cjs");
        await writeFile(main, `
          const { app, BrowserWindow } = require('electron');
          app.setPath('userData', ${JSON.stringify(join(directory, "electron-profile"))});
          const readline = require('node:readline');
          let window;
          const ready = app.whenReady().then(async () => {
            window = new BrowserWindow({ width: 600, height: 420, title: 'Electron Fixture' });
            await window.loadURL('data:text/html,' + encodeURIComponent('<title>Electron Fixture</title><main><h1>Fixture workspace</h1><button>Fixture action</button><input aria-label="Fixture draft" value="Fixture draft value"></main>'));
          });
          readline.createInterface({ input: process.stdin }).on('line', async (line) => {
            const request = JSON.parse(line); await ready;
            process.stdout.write(JSON.stringify({ id: request.id, result: { accessibility: app.isAccessibilitySupportEnabled() } }) + '\\n');
          }).on('close', () => app.quit());
        `);
        const require = createRequire(join(root, "apps/desktop/package.json"));
        const electron: unknown = require("electron");
        if (typeof electron !== "string") throw new Error("Electron executable unavailable");
        const client = pipeClient(electron, [main]);
        try {
          await client.request("state");
          const discovered = toolState(await helper.request("tools/call", { name: "computer_discover", arguments: {} }));
          const identity = Array.isArray(discovered.apps) ? discovered.apps.find((entry: unknown) => record(entry) && entry.pid === client.pid) : undefined;
          if (!record(identity) || typeof identity.app_id !== "string") throw new Error("Electron fixture was not discoverable");
          return { appId: identity.app_id, pid: client.pid, state: () => client.request("state"), [Symbol.asyncDispose]: () => client.close() };
        } catch (error) { await client.close(); throw error; }
      },
      desktop: () => desktop({ name: "computer-use-setup", host: place.host(), env: {
        OMNIRUSH_COMPUTER_USE_BINARY: executable,
        OPENCODE_DB: join(directory, "opencode.db"),
      } }),
      workspacePath: join(directory, "workspace"),
      appId: "org.example.omnirush.computer-use-fixture",
      appPid: fixture.pid,
      call: (name: string, args: Record<string, unknown> = {}) => helper.request("tools/call", { name, arguments: args }),
      peerCall: (name: string, args: Record<string, unknown> = {}) => peer.request("tools/call", { name, arguments: args }),
      list: () => helper.request("tools/list"),
      state: () => fixture.request("state"),
      refreshChanges: (continuous: boolean) => fixture.request("refresh_changes", { continuous }),
      refreshStable: () => fixture.request("refresh_stable"),
      interruptNextRead: () => fixture.request("interrupt_next_read"),
      refreshState: () => fixture.request("refresh_state"),
      resize: () => fixture.request("resize"),
      async setupPanel() {
        const setup = spawn(executable, ["setup"], { stdio: ["ignore", "ignore", "pipe"] });
        let stderr = "";
        setup.stderr.on("data", (data: Buffer) => { stderr = (stderr + data.toString()).slice(-2000); });
        try {
          await new Promise<void>((resolve, reject) => { setup.once("spawn", resolve); setup.once("error", reject); });
          const deadline = Date.now() + 5_000;
          let lastPanel: unknown;
          while (Date.now() < deadline) {
            const result = await fixture.request("helper_panel", { name: "", pid: setup.pid, executable });
            lastPanel = result;
            if (record(result) && typeof result.text === "string" && result.text.includes("Accessibility")) return result;
            await new Promise((resolve) => setTimeout(resolve, 100));
          }
          throw new Error(`The setup window did not show permission status: ${JSON.stringify(lastPanel)}; exit ${setup.exitCode}; ${stderr}`);
        } finally {
          setup.kill("SIGTERM");
          await new Promise<void>((resolve) => {
            if (setup.exitCode !== null || setup.signalCode !== null || !setup.pid) { resolve(); return; }
            const timeout = setTimeout(() => { setup.kill("SIGKILL"); resolve(); }, 2_000);
            setup.once("exit", () => { clearTimeout(timeout); resolve(); });
          });
        }
      },
      async hostedControl(pid: unknown, name: "Hide" | "Stop") {
        if (typeof pid !== "number") throw new Error("Missing hosted preview process");
        const result = await fixture.request("press_helper_button", { name, pid, executable });
        if (!record(result) || result.ok !== true) throw new Error(`Preview ${name} button unavailable`);
      },
      hostedPanel: (pid: unknown) => fixture.request("helper_panel", { name: "", pid, executable }),
      panel: () => fixture.request("helper_panel", { name: "", pid: helper.pid, executable }),
      foregroundWindow: () => fixture.request("foreground_window"),
      minimized: () => fixture.request("minimized"),
      minimize: () => fixture.request("minimize"),
      restore: () => fixture.request("restore"),
      humanEdit: async () => {
        const activated = spawnSync("/usr/bin/osascript", ["-e", `tell application "System Events" to set frontmost of (first application process whose unix id is ${fixture.pid}) to true`], { encoding: "utf8", timeout: 5000 });
        if (activated.status !== 0) throw new Error(activated.stderr);
        return fixture.request("human_edit");
      },
      hover: () => fixture.request("hover"),
      prepareDrag: () => fixture.request("prepare_drag"),
      dragState: () => fixture.request("drag_state"),
      front: () => fixture.request("front"),
      async selectWindow() {
        const deadline = Date.now() + 10_000;
        while (Date.now() < deadline) {
          const result = await fixture.request("select_helper_window", { name: "Workspace window", pid: helper.pid, executable });
          if (record(result) && result.ok === true) return result;
          await new Promise((resolve) => setTimeout(resolve, 100));
        }
        throw new Error("The native window picker did not become available.");
      },
      async pressControl(name: "Allow and start" | "Allow this session" | "Cancel" | "Take over" | "Continue" | "Stop" | "Hide panel" | "Show Computer Use task") {
        const deadline = Date.now() + 10_000;
        while (Date.now() < deadline) {
          const result = await fixture.request("press_helper_button", { name, pid: helper.pid, executable });
          if (record(result) && result.ok === true) return;
          await new Promise((resolve) => setTimeout(resolve, 100));
        }
        throw new Error(`The native ${name} control did not become available; the person-input fixture may need Accessibility permission.`);
      },
      [Symbol.asyncDispose]: close,
    };
  } catch (error) { await close(); throw error; }
}

/** Desktop-native Windows/X11 journey with disposable Electron windows. */
export async function portableComputerUseWorld(_seed: Seed, { place }: { place: Place }) {
  if (place.kind !== "local" || !["win32", "linux"].includes(process.platform)) throw new SkipError("Windows or Linux X11 graphical desktop placement");
  if (process.platform === "linux") {
    if (process.env.WAYLAND_DISPLAY || process.env.XDG_SESSION_TYPE === "wayland") throw new SkipError("Linux X11; Wayland portal control is not implemented");
    if (!process.env.DISPLAY) throw new SkipError("Linux graphical display");
  }
  const root = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
  const requireDesktop = createRequire(join(root, "apps/desktop/package.json"));
  const executable = requireDesktop("electron");
  if (typeof executable !== "string") throw new Error("Electron executable unavailable.");
  const directory = await mkdtemp(join(tmpdir(), "omnirush-native-journey-"));
  const launchFlags = process.platform === "linux" ? ["--no-sandbox"] : [];
  const fixture = await nativeFixtureClient(executable, [...launchFlags, join(root, "evals/packages/labs/fixtures/portable-computer-use-app.mjs"), join(directory, "fixture")]);
  const controller = await nativeFixtureClient(executable, [...launchFlags, join(root, "evals/packages/labs/fixtures/portable-computer-use-host.mjs"), join(directory, "host")]).catch(async (error) => { await fixture.close(); await rm(directory, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 }); throw error; });
  let client: ReturnType<typeof pipeClient> | undefined;
  let peer: ReturnType<typeof pipeClient> | undefined;
  try {
    await fixture.request("state");
    const permissions = await controller.request("permissions", {}, 20_000);
    if (!record(permissions) || permissions.ok !== true) throw new Error("Desktop readiness failed: " + JSON.stringify(permissions));
    const launch = await controller.request("command");
    if (!record(launch) || !Array.isArray(launch.command) || !launch.command.every((v): v is string => typeof v === "string") || !record(launch.environment)) throw new Error("Missing desktop MCP command.");
    const environment: Record<string, string> = {};
    for (const [key, value] of Object.entries(launch.environment)) { if (typeof value !== "string") throw new Error("Invalid MCP environment."); environment[key] = value; }
    const command = launch.command;
    client = pipeClient(command[0], command.slice(1), environment);
    peer = pipeClient(command[0], command.slice(1), environment);
    await client.request("initialize", { protocolVersion: "2025-11-25", clientInfo: { name: "native-journey", version: "1" }, capabilities: {} });
    await peer.request("initialize", { protocolVersion: "2025-11-25", clientInfo: { name: "native-peer", version: "1" }, capabilities: {} });
    const discovery = toolState(await client.request("tools/call", { name: "computer_discover", arguments: {} }));
    if (!Array.isArray(discovery.apps)) throw new Error("No app discovery.");
    const appInfo = discovery.apps.find((value: unknown) => record(value) && value.pid === fixture.pid);
    if (!record(appInfo) || typeof appInfo.app_id !== "string") throw new Error("Fixture was not discoverable: " + JSON.stringify(discovery));
    const main = client, other = peer;
    return {
      desktop: () => desktop({ name: "native-computer-use-setup", host: place.host(), profileDir: join(directory, "desktop-profile"), env: { OPENCODE_DB: join(directory, "desktop.db"), OMNIRUSH_GATEWAY_URL: "http://127.0.0.1:1", COREPACK_ENABLE_DOWNLOAD_PROMPT: "0", ELECTRON_DISABLE_SANDBOX: "1", OMNIRUSH_ELECTRON_USE_MOCK_KEYCHAIN: "1" } }),
      workspacePath: join(directory, "workspace"),
      async hostedClient(command: unknown, environment: unknown) {
        if (!Array.isArray(command) || !command.every((part): part is string => typeof part === "string") || !record(environment)) throw new Error("Missing hosted MCP command.");
        const env: Record<string, string> = {};
        for (const [key, value] of Object.entries(environment)) { if (typeof value !== "string") throw new Error("Invalid hosted environment."); env[key] = value; }
        const hosted = pipeClient(command[0], command.slice(1), env);
        await hosted.request("initialize", { protocolVersion: "2025-11-25", clientInfo: { name: "desktop-native-journey", version: "1" }, capabilities: {} });
        return { call: (name: string, args: Record<string, unknown> = {}) => hosted.request("tools/call", { name, arguments: args }, 70_000), [Symbol.asyncDispose]: hosted.close };
      },
      permissions,
      appId: appInfo.app_id, pid: fixture.pid,
      call: (name: string, args: Record<string, unknown> = {}) => main.request("tools/call", { name, arguments: args }, 70_000),
      peerCall: (name: string, args: Record<string, unknown> = {}) => other.request("tools/call", { name, arguments: args }),
      raw: (method: string, params: Record<string, unknown> = {}) => main.request(method, params),
      hostState: () => controller.request("state"),
      state: () => fixture.request("state"),
      change: () => fixture.request("change"),
      focusOther: () => fixture.request("focus_other"),
      cover: () => fixture.request("cover"),
      outsideInput: () => fixture.request("outside_input"),
      pointerState: () => fixture.request("pointer_state"),
      inputEvents: () => fixture.request("input_events"),
      imagePixel(reply: unknown) {
        if (!record(reply) || !Array.isArray(reply.content)) throw new Error("Missing image content.");
        const image = reply.content.find((item: unknown) => record(item) && item.type === "image");
        if (!record(image) || typeof image.data !== "string") throw new Error("Missing image data.");
        return fixture.request("image_pixel", { data: image.data });
      },
      minimize: () => fixture.request("minimize"),
      restore: () => fixture.request("restore"),
      cancel: () => main.cancelPending(),
      previewAction: (action: string) => controller.request("preview_action", { action }),
      async approve(action = "approve") {
        const deadline = Date.now() + 15_000;
        for (;;) {
          const states = await controller.request("state");
          if (Array.isArray(states)) {
            const state = states.find((value: unknown) => record(value) && (action === "approve" ? value.phase === "approval" : true));
            if (record(state) && Array.isArray(state.windows)) {
              const window = state.windows.find((value: unknown) => record(value) && value.title === "Workspace window");
              if (!record(window) || typeof window.id !== "number") throw new Error("No workspace window in approval.");
              await controller.request("action", { connectionId: state.connectionId, id: state.id, action, windowId: window.id });
              return;
            }
          }
          if (Date.now() >= deadline) throw new Error("Timed out waiting for native consent.");
          await new Promise((resolve) => setTimeout(resolve, 50));
        }
      },
      async point(observation: Record<string, unknown>, x: number, y: number) {
        const bounds = await fixture.request("bounds");
        if (!Array.isArray(bounds) || !record(bounds[0]) || !record(bounds[0].contentBounds) || typeof bounds[0].scale !== "number") throw new Error("Missing fixture bounds.");
        const native = observation.window_bounds, image = observation.image_size, contentBounds = bounds[0].contentBounds;
        if (!record(native) || !record(image) || typeof native.x !== "number" || typeof native.y !== "number" || typeof native.width !== "number" || typeof native.height !== "number" || typeof image.width !== "number" || typeof image.height !== "number" || typeof contentBounds.x !== "number" || typeof contentBounds.y !== "number") throw new Error("Missing image bounds.");
        return { x: ((contentBounds.x + x) * bounds[0].scale - native.x) * image.width / native.width, y: ((contentBounds.y + y) * bounds[0].scale - native.y) * image.height / native.height };
      },
      async closeClient() { await main.close(); },
      async [Symbol.asyncDispose]() { await Promise.all([main.close(), other.close(), controller.close(), fixture.close()]); await rm(directory, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 }); },
    };
  } catch (error) {
    await Promise.all([client?.close(), peer?.close(), controller.close(), fixture.close()]);
    await rm(directory, { recursive: true, force: true, maxRetries: 10, retryDelay: 200 }); throw error;
  }
}