Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

39.1 KB
Raw Blame
import { chmodSync, existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import { afterEach, describe, expect, it, vi } from "vitest";
import { removeTempDir } from "../testing/cleanup.ts";
import { startFakeHttpMcp } from "../testing/fake-http-mcp-server.ts";
import { whopLikeCatalog } from "../testing/whop-like-catalog.ts";
import { buildMcpServers } from "./pi-mcp-servers.ts";
import { mcpStdioServer } from "../mcp-gate-config.ts";
import { agentsStartupMs } from "./chat-mcp-tools.ts";

import extension, {
  allocateToolName,
  StdioMcp,
  toTypebox,
  truncateToolText,
} from "./pi-mcp-extension.ts";
import { mcpContentToPiContent } from "../mcp-tool-content.ts";

const tempDirs: string[] = [];
const clients: StdioMcp[] = [];
const originalMcpConfig = process.env.OMB_MCP_CONFIG;

type ExtensionApi = Parameters<typeof extension>[0];
type RegisteredTool = Parameters<ExtensionApi["registerTool"]>[0];
type ShutdownHandler = Parameters<ExtensionApi["on"]>[1];

interface SchemaNode {
  type?: string;
  const?: unknown;
  enum?: unknown[];
  anyOf?: SchemaNode[];
  required?: string[];
  additionalProperties?: boolean | SchemaNode;
  properties?: Record<string, SchemaNode>;
}

function tempDir(): string {
  const dir = mkdtempSync(join(tmpdir(), "omb-pi-mcp-ext-"));
  tempDirs.push(dir);
  return dir;
}

function fakeMcpScript(source: string): string {
  const dir = tempDir();
  const path = join(dir, "fake-mcp.mjs");
  writeFileSync(path, source, "utf8");
  chmodSync(path, 0o755);
  return path;
}

function createClient(source: string, env: Record<string, string> = {}): StdioMcp {
  const client = new StdioMcp({ command: process.execPath, args: [fakeMcpScript(source)], env });
  clients.push(client);
  return client;
}

afterEach(async () => {
  vi.restoreAllMocks();
  for (const client of clients.splice(0)) client.dispose();
  for (const dir of tempDirs.splice(0)) await removeTempDir(dir);
  if (originalMcpConfig === undefined) delete process.env.OMB_MCP_CONFIG;
  else process.env.OMB_MCP_CONFIG = originalMcpConfig;
});

describe("MCP content conversion", () => {
  it("keeps every content kind and never drops a block silently", () => {
    expect(mcpContentToPiContent([
      { type: "text", text: "hi" },
      { type: "image", data: "aW1n", mimeType: "image/png" },
      { type: "audio", data: "YXVk", mimeType: "audio/mpeg" },
      { type: "resource_link", name: "note", uri: "voicenote://audio/1" },
      { type: "resource", resource: { uri: "x://text", mimeType: "text/plain", text: "body" } },
      { type: "resource", resource: { uri: "x://img", mimeType: "image/jpeg", blob: "aW1n" } },
      { type: "resource", resource: { uri: "x://bin", mimeType: "application/pdf", blob: "Ymlu" } },
      { type: "weird", foo: 1 },
      { type: "image", data: 5, mimeType: "image/png" },
      "nonsense",
    ])).toEqual([
      { type: "text", text: "hi" },
      { type: "image", data: "aW1n", mimeType: "image/png" },
      { type: "text", text: "[audio audio/mpeg omitted]" },
      { type: "text", text: "note: voicenote://audio/1" },
      { type: "text", text: "body" },
      { type: "image", data: "aW1n", mimeType: "image/jpeg" },
      { type: "text", text: "[binary resource x://bin (application/pdf) omitted]" },
      { type: "text", text: "[unsupported MCP content weird]" },
      { type: "text", text: "[unsupported MCP content image]" },
      { type: "text", text: "[unsupported MCP content]" },
    ]);
  });
});

describe("Pi MCP JSON Schema conversion", () => {
  it("keeps the cua browser_prepare regression schema object-shaped at the root", () => {
    const schema = toTypebox({
      type: "object",
      additionalProperties: true,
      anyOf: [
        { required: ["pid"] },
        {
          properties: {
            allow_launch: { const: true },
            profile: { properties: { mode: { enum: ["isolated_new", "isolated_named"] } } },
          },
          required: ["allow_launch", "profile"],
        },
      ],
      properties: {
        allow_launch: { type: "boolean" },
        pid: { type: "integer" },
        profile: {
          type: "object",
          properties: { mode: { type: "string", enum: ["isolated_new", "isolated_named"] } },
          required: ["mode"],
        },
      },
      required: [],
    });
    const inspected = schema as SchemaNode;

    expect(inspected.type).toBe("object");
    expect(inspected.additionalProperties).toBe(true);
    expect(inspected.properties?.pid.type).toBe("integer");
    expect(inspected.properties?.profile.type).toBe("object");
    expect(inspected.properties?.profile.required).toEqual(["mode"]);
  });

  it("preserves nested combinators, nullability, const and Google-compatible string enums", () => {
    const schema = toTypebox({
      type: "object",
      properties: {
        value: { anyOf: [{ type: "string" }, { type: "null" }] },
        flag: { type: ["boolean", "null"] },
        mode: { type: "string", enum: ["ax", "vision"] },
        enabled: { const: true },
      },
      required: ["value"],
    });
    const inspected = schema as SchemaNode;

    expect(inspected.type).toBe("object");
    expect(inspected.required).toEqual(["value"]);
    expect(inspected.properties?.value.anyOf?.map((item) => item.type)).toEqual(["string", "null"]);
    expect(inspected.properties?.flag.anyOf?.map((item) => item.type)).toEqual(["boolean", "null"]);
    expect(inspected.properties?.mode).toMatchObject({ type: "string", enum: ["ax", "vision"] });
    expect(inspected.properties?.enabled).toMatchObject({ type: "boolean", const: true });
  });

  it("collects properties declared only inside root combinator branches", () => {
    const schema = toTypebox({
      oneOf: [
        { type: "object", properties: { query: { type: "string" } }, required: ["query"] },
        { type: "object", properties: { id: { type: "integer" } }, required: ["id"] },
      ],
    });
    const inspected = schema as SchemaNode;

    expect(inspected.type).toBe("object");
    expect(Object.keys(inspected.properties ?? {})).toEqual(["query", "id"]);
    expect(inspected.required).toBeUndefined();
  });

  it("always returns an object schema for malformed, nullable or absent root schemas", () => {
    expect(toTypebox(undefined)).toMatchObject({ type: "object" });
    expect(toTypebox({ anyOf: [] })).toMatchObject({ type: "object" });
    expect(toTypebox({ type: "object", nullable: true })).toMatchObject({ type: "object" });
  });
});

describe("Pi MCP tool names and output bounds", () => {
  it("keeps collision suffixes inside the provider 64-character limit", () => {
    const used = new Set<string>();
    const long = "x".repeat(100);
    const names = Array.from({ length: 12 }, () => {
      const name = allocateToolName("computer", long, used);
      used.add(name);
      return name;
    });

    expect(new Set(names).size).toBe(names.length);
    expect(names.every((name) => name.length <= 64)).toBe(true);
    expect(names.at(-1)).toMatch(/_12$/);
  });

  it("truncates by lines and UTF-8 bytes without splitting a code point", () => {
    const manyLines = Array.from({ length: 2_100 }, (_, index) => `line ${index}`).join("\n");
    expect(truncateToolText(manyLines)).toContain("2000-line limit");

    const manyAccents = "á".repeat(30_000);
    const truncated = truncateToolText(manyAccents);
    expect(truncated).toContain("50KB limit");
    expect(truncated).not.toContain("�");
  });
});

describe("StdioMcp", () => {
  it("advertises the MCP Apps extension and retries without it when a server refuses", async () => {
    const dump = join(tempDir(), "init.jsonl");
    const script = (`
      import { appendFileSync } from "node:fs";
      let buffer = "";
      process.stdin.setEncoding("utf8");
      const send = (value) => process.stdout.write(JSON.stringify(value) + "\\n");
      process.stdin.on("data", (chunk) => {
        buffer += chunk;
        let newline;
        while ((newline = buffer.indexOf("\\n")) !== -1) {
          const line = buffer.slice(0, newline); buffer = buffer.slice(newline + 1);
          if (!line.trim()) continue;
          const msg = JSON.parse(line);
          if (msg.id === undefined) continue;
          if (msg.method === "initialize") {
            const extensions = msg.params?.capabilities?.extensions;
            const hasUi = Boolean(extensions && Object.prototype.hasOwnProperty.call(extensions, "io.modelcontextprotocol/ui"));
            appendFileSync(process.env.DUMP, JSON.stringify(hasUi) + "\\n");
            if (hasUi) send({ jsonrpc: "2.0", id: msg.id, error: { code: -32602, message: "unknown capability" } });
            else send({ jsonrpc: "2.0", id: msg.id, result: { capabilities: { tools: {} } } });
          } else if (msg.method === "tools/list") send({ jsonrpc: "2.0", id: msg.id, result: { tools: [] } });
        }
      });
    `);
    const client = createClient(script, { DUMP: dump });
    await client.init(true);
    expect(readFileSync(dump, "utf8").trim().split("\n")).toEqual(["true", "false"]);
    await expect(client.listTools()).resolves.toEqual([]);
  });

  it("initializes, paginates tools/list, preserves images and truncates text", async () => {
    const client = createClient(`
      let buffer = "";
      process.stdin.setEncoding("utf8");
      const send = (value) => process.stdout.write(JSON.stringify(value) + "\\n");
      process.stdin.on("data", (chunk) => {
        buffer += chunk;
        let newline;
        while ((newline = buffer.indexOf("\\n")) !== -1) {
          const line = buffer.slice(0, newline); buffer = buffer.slice(newline + 1);
          if (!line.trim()) continue;
          const msg = JSON.parse(line);
          if (msg.method === "initialize") send({ jsonrpc: "2.0", id: msg.id, result: { capabilities: { tools: {} } } });
          else if (msg.method === "tools/list" && !msg.params?.cursor) send({ jsonrpc: "2.0", id: msg.id, result: { tools: [{ name: "first", inputSchema: { type: "object" } }], nextCursor: "page-2" } });
          else if (msg.method === "tools/list") send({ jsonrpc: "2.0", id: msg.id, result: { tools: [{ name: "second", inputSchema: { type: "object" } }] } });
          else if (msg.method === "tools/call") send({ jsonrpc: "2.0", id: msg.id, result: { content: [
            { type: "text", text: "x".repeat(60 * 1024) },
            { type: "image", data: "aW1hZ2U=", mimeType: "image/png" }
          ] } });
        }
      });
    `);

    await client.init();
    await expect(client.listTools()).resolves.toMatchObject([{ name: "first" }, { name: "second" }]);
    const result = await client.callTool("first", {});
    expect(result.content[0]).toMatchObject({ type: "text", text: expect.stringContaining("50KB limit") });
    expect(result.content[1]).toEqual({ type: "image", data: "aW1hZ2U=", mimeType: "image/png" });
  });

  it("propagates abort and sends MCP notifications/cancelled", async () => {
    const dir = tempDir();
    const dump = join(dir, "cancel.json");
    const client = createClient(
      `
        import { writeFileSync } from "node:fs";
        let buffer = "";
        process.stdin.setEncoding("utf8");
        const send = (value) => process.stdout.write(JSON.stringify(value) + "\\n");
        process.stdin.on("data", (chunk) => {
          buffer += chunk;
          let newline;
          while ((newline = buffer.indexOf("\\n")) !== -1) {
            const line = buffer.slice(0, newline); buffer = buffer.slice(newline + 1);
            if (!line.trim()) continue;
            const msg = JSON.parse(line);
            if (msg.method === "initialize") send({ jsonrpc: "2.0", id: msg.id, result: { capabilities: { tools: {} } } });
            else if (msg.method === "notifications/cancelled") writeFileSync(process.env.DUMP, JSON.stringify(msg.params));
          }
        });
      `,
      { DUMP: dump },
    );
    await client.init();
    const controller = new AbortController();
    const pending = client.callTool("slow", {}, controller.signal);
    controller.abort();
    await expect(pending).rejects.toThrow(/aborted/);
    await vi.waitFor(() => expect(JSON.parse(readFileSync(dump, "utf8"))).toMatchObject({ requestId: 2 }));
  });
});

describe("Pi MCP startup budgets", () => {
  /** Lets real I/O run, the faked clock standing still, until `condition`. */
  async function untilReal(condition: () => boolean): Promise<void> {
    const started = Date.now();
    while (!condition() && Date.now() - started < 15_000) await new Promise((resolve) => setImmediate(resolve));
    expect(condition()).toBe(true);
  }
  function watch<T>(pending: Promise<T>) {
    const state: { done: boolean; value?: T; error?: unknown } = { done: false };
    void pending.then((value) => { state.done = true; state.value = value; }, (error: unknown) => { state.done = true; state.error = error; });
    return state;
  }

  it("gives a searched URL server 30 seconds to start, the same server mounted plainly 8", async () => {
    vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
    const remote = await startFakeHttpMcp({ tools: whopLikeCatalog(300), toolsDelayMs: 20_000 });
    try {
      const descriptor = mcpStdioServer({ type: "http", url: remote.url, headers: {} }, { directory: { name: "whop" } })!;
      const server = { command: descriptor.command, args: descriptor.args, env: descriptor.env };
      const searched = new StdioMcp({ ...server, directory: true });
      clients.push(searched);
      const listing = watch(searched.init().then(() => searched.listTools()));
      await untilReal(() => remote.delayedToolsLists === 1);
      await vi.advanceTimersByTimeAsync(20_000);
      await untilReal(() => listing.done);
      expect(listing.error).toBeUndefined();
      expect(listing.value!.map((tool) => tool.name)).toEqual(["search_tools", "describe_tool", "call_tool"]);

      const plain = new StdioMcp(server);
      clients.push(plain);
      const failing = watch(plain.init().then(() => plain.listTools()));
      await untilReal(() => remote.delayedToolsLists === 2);
      await vi.advanceTimersByTimeAsync(8_000);
      await untilReal(() => failing.done);
      expect(String(failing.error)).toMatch(/timed out/);
    } finally {
      vi.useRealTimers();
      await remote.close();
    }
  });

  /** A command server that answers initialize only once the test creates `<name>.answer` in `dir`. */
  function slowToStart(dir: string, name: string): string {
    const asked = JSON.stringify(join(dir, `${name}.asked`)), answer = JSON.stringify(join(dir, `${name}.answer`));
    return fakeMcpScript(`
      import { existsSync, writeFileSync } from "node:fs";
      let buffer = "";
      process.stdin.setEncoding("utf8");
      const send = (value) => process.stdout.write(JSON.stringify(value) + "\\n");
      process.stdin.on("data", (chunk) => {
        buffer += chunk;
        let newline;
        while ((newline = buffer.indexOf("\\n")) !== -1) {
          const line = buffer.slice(0, newline); buffer = buffer.slice(newline + 1);
          if (!line.trim()) continue;
          const msg = JSON.parse(line);
          if (msg.method === "initialize") {
            writeFileSync(${asked}, "");
            const wait = setInterval(() => {
              if (!existsSync(${answer})) return;
              clearInterval(wait);
              send({ jsonrpc: "2.0", id: msg.id, result: { capabilities: { tools: {} } } });
            }, 10);
          } else if (msg.method === "tools/list") send({ jsonrpc: "2.0", id: msg.id, result: { tools: [{ name: "coordinate_bots", inputSchema: { type: "object" } }] } });
        }
      });
    `);
  }

  it("gives the agents server the start time other engines give it, a plain command server 8 seconds", async () => {
    const { startupMs } = buildMcpServers({ threadId: "t", text: "hi", integrations: { agents: { command: "node", args: ["agents"], env: {} } } })!.agents as { startupMs?: number };
    expect(startupMs).toBe(agentsStartupMs());
    vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
    try {
      const dir = tempDir();
      const agents = new StdioMcp({ command: process.execPath, args: [slowToStart(dir, "agents")], startupMs });
      const plain = new StdioMcp({ command: process.execPath, args: [slowToStart(dir, "plain")] });
      clients.push(agents, plain);
      const mounting = watch(agents.init().then(() => agents.listTools()));
      const failing = watch(plain.init().then(() => plain.listTools()));
      await untilReal(() => existsSync(join(dir, "agents.asked")) && existsSync(join(dir, "plain.asked")));
      await vi.advanceTimersByTimeAsync(12_000);
      await untilReal(() => failing.done);
      expect(String(failing.error)).toMatch(/timed out/);
      expect(mounting.done).toBe(false);
      writeFileSync(join(dir, "agents.answer"), "");
      await untilReal(() => mounting.done);
      expect(mounting.error).toBeUndefined();
      expect(mounting.value!.map((tool) => tool.name)).toEqual(["coordinate_bots"]);
    } finally {
      vi.useRealTimers();
    }
  });
});

describe("Pi MCP extension registration", () => {
  const scopedApi = (initial: string[]) => {
    let active = initial;
    const tools: RegisteredTool[] = [];
    const handlers = new Map<string, (event?: any) => any>();
    return { tools, handlers, current: () => active, restore: (names: string[]) => { active = names; }, api: {
      registerTool(tool: RegisteredTool) { tools.push(tool); },
      on(event: string, handler: (event?: any) => any) { handlers.set(event, handler); },
      getActiveTools: () => [...active], setActiveTools: (names: string[]) => { active = names; },
    } };
  };
  const scopeConfig = (toolScope: unknown, other = {}) => {
    const dir = tempDir();
    process.env.OMB_MCP_CONFIG = join(dir, "scope.json");
    writeFileSync(process.env.OMB_MCP_CONFIG, JSON.stringify({ toolScope, ...other }));
    return dir;
  };

  it.each([undefined, { allow: ["native:bash", "native:powershell"] }])(
    "rejects shell timeouts mistaken for milliseconds with scope %j",
    async (toolScope) => {
      scopeConfig(toolScope);
      const f = scopedApi(["bash", "powershell"]);
      await extension(f.api);
      for (const toolName of ["bash", "powershell"]) {
        const input = { command: "echo should-not-run", timeout: 90_000 };
        expect(await f.handlers.get("tool_call")?.({ toolName, input })).toMatchObject({
          block: true,
          reason: expect.stringMatching(/seconds.*900/),
        });
        expect(input.timeout).toBe(90_000);
      }
    },
  );

  it("bounds shell calls without a timeout and preserves valid seconds", async () => {
    scopeConfig(undefined);
    const f = scopedApi(["bash", "powershell", "read"]);
    await extension(f.api);
    for (const toolName of ["bash", "powershell"]) {
      const input: { command: string; timeout?: number } = { command: "echo bounded" };
      expect(await f.handlers.get("tool_call")?.({ toolName, input })).toBeUndefined();
      expect(input.timeout).toBe(120);
      for (const timeout of [0.1, 90, 900]) {
        const explicit = { command: "echo valid", timeout };
        expect(await f.handlers.get("tool_call")?.({ toolName, input: explicit })).toBeUndefined();
        expect(explicit.timeout).toBe(timeout);
      }
    }
    const input = { path: "fixture.txt" };
    expect(await f.handlers.get("tool_call")?.({ toolName: "read", input })).toBeUndefined();
    expect(input).toEqual({ path: "fixture.txt" });
  });

  it.each([0, -1, NaN, Infinity, null, "90"])("rejects invalid shell timeout %s", async (timeout) => {
    scopeConfig(undefined);
    const f = scopedApi(["bash"]);
    await extension(f.api);
    expect(await f.handlers.get("tool_call")?.({ toolName: "bash", input: { command: "echo invalid", timeout } }))
      .toMatchObject({ block: true, reason: expect.stringMatching(/seconds/) });
  });

  it("intersects native and package tools before the first turn and after restoration", async () => {
    scopeConfig({ allow: ["native:read", "native:edit", "native:write"] });
    const f = scopedApi(["read", "write", "bash", "package_tool"]);
    await extension(f.api);
    await f.handlers.get("session_start")?.({});
    expect(f.current()).toEqual(["read", "write"]);
    f.restore(["read", "write", "bash", "late_package"]);
    await f.handlers.get("before_agent_start")?.({});
    expect(f.current()).toEqual(["read", "write"]);
    f.restore(["edit", "bash"]);
    await f.handlers.get("session_switch")?.({});
    await f.handlers.get("model_select")?.({});
    expect(f.current()).toEqual(["edit"]);
    expect(await f.handlers.get("tool_call")?.({ toolName: "bash" })).toMatchObject({ block: true });
    expect(await f.handlers.get("tool_call")?.({ toolName: "read" })).toBeUndefined();
    f.restore(["read", "bash"]);
    const payload = { tools: ["read", "bash", "late_package"].map((name) => ({ type: "function", function: { name, parameters: { type: "object" } } })) };
    await f.handlers.get("before_provider_request")?.({ payload });
    expect(payload.tools.map((tool) => tool.function.name)).toEqual(["read"]);
  });

  it("does not confirm a refused active-tool clamp or restore a tool another extension disabled", async () => {
    const directory = scopeConfig({ allow: ["native:read", "native:write"] });
    const readyPath = join(directory, "ready.json");
    scopeConfig({ allow: ["native:read", "native:write"] }, { scopeReadyPath: readyPath });
    const refused = scopedApi(["read", "bash"]);
    refused.api.setActiveTools = () => {};
    await extension(refused.api);
    expect(() => refused.handlers.get("session_start")?.({})).toThrow(/enforcement/i);
    expect(JSON.parse(readFileSync(readyPath, "utf8"))).toEqual({ ok: false });
    const f = scopedApi(["write"]);
    await extension(f.api);
    const payload = { tools: ["read", "write"].map((name) => ({ name, input_schema: { type: "object" } })) };
    await f.handlers.get("before_provider_request")?.({ payload });
    expect(payload.tools.map((tool) => tool.name)).toEqual(["write"]);
  });

  it("makes no-tools explicit and refuses unavailable enforcement APIs or corrupt configuration", async () => {
    scopeConfig({ allow: [] });
    const f = scopedApi(["read", "bash"]);
    await extension(f.api);
    await f.handlers.get("session_start")?.({});
    expect(f.current()).toEqual([]);
    expect(await f.handlers.get("tool_call")?.({ toolName: "read" })).toMatchObject({ block: true });
    await expect(extension({ registerTool() {}, on() {} })).rejects.toThrow(/enforcement/i);
    writeFileSync(process.env.OMB_MCP_CONFIG!, "not json");
    await expect(extension(f.api)).rejects.toThrow(/configuration/i);
  });

  it("removes declarations if enforcement becomes unavailable after startup", async () => {
    scopeConfig({ allow: ["native:read"] });
    const f = scopedApi(["read"]);
    await extension(f.api);
    await f.handlers.get("session_start")?.({});
    f.api.setActiveTools = () => {};
    f.restore(["read", "bash"]);
    const payload = { tools: ["read", "bash"].map((name) => ({ type: "function", function: { name } })) };
    // Pi catches extension exceptions and can keep sending the old payload: return deny-all, never throw.
    await f.handlers.get("before_provider_request")?.({ payload });
    expect(payload.tools).toEqual([]);
  });

  it("carries an app tool's _meta, structuredContent and every content kind, filtering app-only tools", async () => {
    const script = fakeMcpScript(`
      let buffer = "";
      process.stdin.setEncoding("utf8");
      const send = (value) => process.stdout.write(JSON.stringify(value) + "\\n");
      process.stdin.on("data", (chunk) => {
        buffer += chunk;
        let newline;
        while ((newline = buffer.indexOf("\\n")) !== -1) {
          const line = buffer.slice(0, newline); buffer = buffer.slice(newline + 1);
          if (!line.trim()) continue;
          const msg = JSON.parse(line);
          if (msg.id === undefined) continue;
          if (msg.method === "initialize") send({ jsonrpc: "2.0", id: msg.id, result: { capabilities: { tools: {} }, serverInfo: { name: "voicenote", title: "MausBot Voice" } } });
          else if (msg.method === "tools/list") send({ jsonrpc: "2.0", id: msg.id, result: { tools: [
            { name: "show_voice_note", description: "play", inputSchema: { type: "object" }, _meta: { ui: { resourceUri: "ui://voicenote/player.html" } } },
            { name: "app_only", inputSchema: { type: "object" }, _meta: { ui: { resourceUri: "ui://voicenote/other", visibility: ["app"] } } }
          ] } });
          else if (msg.method === "tools/call") send({ jsonrpc: "2.0", id: msg.id, result: { content: [{ type: "audio", data: "TURU", mimeType: "audio/mpeg" }], structuredContent: { id: "abc" }, _meta: { voicenote: { id: "abc" } } } });
        }
      });
    `);
    scopeConfig(undefined, { mcpServers: { voicenote: { command: process.execPath, args: [script], scope: "custom" } }, approvalMode: "full" });
    const tools: RegisteredTool[] = [];
    const handlers = new Map<string, ShutdownHandler>();
    await extension({ registerTool(tool) { tools.push(tool); }, on(event, handler) { handlers.set(event, handler); } });
    try {
      expect(tools.map((tool) => tool.name)).toEqual(["voicenote_show_voice_note"]);
      const result = await tools[0].execute("call", {}, undefined, undefined, { ui: { confirm: async () => true } });
      expect(result.structuredContent).toEqual({ id: "abc" });
      expect(result.details).toMatchObject({ server: "voicenote", tool: "show_voice_note", meta: { voicenote: { id: "abc" } } });
      expect(result.content[0]).toMatchObject({ type: "text", text: "[audio audio/mpeg omitted]" });
    } finally {
      await handlers.get("session_shutdown")?.();
    }
  });

  it("filters raw MCP identities and still asks before custom execution", async () => {
    const receipt = join(tempDir(), "execution.txt");
    const script = fakeMcpScript(`
      import { createInterface } from "node:readline";
      import { writeFileSync } from "node:fs";
      createInterface({input:process.stdin}).on("line", line => {
        const m = JSON.parse(line); if (m.id === undefined) return;
        let result = {};
        if (m.method === "tools/list") result = {tools:[{name:"read-notes",inputSchema:{type:"object"}},{name:"read_notes",inputSchema:{type:"object"}}]};
        if (m.method === "tools/call") { writeFileSync(process.env.RECEIPT, m.params.name); result = {content:[{type:"text",text:"selected"}]}; }
        process.stdout.write(JSON.stringify({jsonrpc:"2.0",id:m.id,result}) + "\\n");
      });
    `);
    scopeConfig({ allow: ["mcp:mail:read-notes"] }, { mcpServers: { mail: { command: process.execPath, args: [script], env: { RECEIPT: receipt }, scope: "custom" } } });
    const f = scopedApi(["read", "mail_read_notes"]);
    await extension(f.api);
    try {
      expect(f.tools.map((tool) => tool.name)).toEqual(["mail_read_notes"]);
      const blocked = await f.tools[0].execute("denied", {}, undefined, undefined, { ui: { confirm: async () => false } });
      expect(blocked.content).toMatchObject([{ text: "Blocked by the user." }]);
      expect(existsSync(receipt)).toBe(false);
      await f.tools[0].execute("allowed", {}, undefined, undefined, { ui: { confirm: async () => true } });
      expect(readFileSync(receipt, "utf8")).toBe("read-notes");
      expect(await f.handlers.get("tool_call")?.({ toolName: "mail_read_notes_2" })).toMatchObject({ block: true });
    } finally { await f.handlers.get("session_shutdown")?.(); }
  });
  it("registers a big URL server as three tools, asking only about the tool call_tool runs", async () => {
    const remote = await startFakeHttpMcp({ tools: whopLikeCatalog(300) });
    try {
      const servers = buildMcpServers({ threadId: "pi-whop", text: "Go", integrations: { custom: { whop: { type: "http", url: remote.url, headers: {} } } } });
      scopeConfig(undefined, { mcpServers: servers, approvalMode: "ask" });
      const tools: RegisteredTool[] = [];
      const handlers = new Map<string, ShutdownHandler>();
      await extension({ registerTool(tool) { tools.push(tool); }, on(event, handler) { handlers.set(event, handler); } });
      try {
        expect(tools.map((tool) => tool.name)).toEqual(["whop_search_tools", "whop_describe_tool", "whop_call_tool"]);
        const asked: string[] = [];
        const ui = { confirm: async (title: string) => { asked.push(title); return true; } };
        const search = await tools[0].execute("search", { query: "list payments" }, undefined, undefined, { ui });
        expect(JSON.parse((search.content[0] as { text: string }).text).matches[0].name).toBe("payments_list");
        await tools[1].execute("describe", { name: "payments_list" }, undefined, undefined, { ui });
        expect(asked).toEqual([]);
        await tools[2].execute("run", { name: "payments_list", arguments: { company_id: "biz_1" } }, undefined, undefined, { ui });
        expect(asked).toEqual(["Allow whop:payments_list?"]);
        expect(remote.calls).toEqual([{ name: "payments_list", arguments: { company_id: "biz_1" } }]);
      } finally { await handlers.get("session_shutdown")?.(); }
    } finally { await remote.close(); }
  });

  it("keeps a searched URL server to the bot's selection", async () => {
    const catalog = whopLikeCatalog(300);
    const selected = [...catalog.filter((tool) => tool.name.endsWith("_list")).map((tool) => tool.name), "payments_get", "stats_get"];
    const toolScope = { allow: selected.map((name) => `mcp:whop:${name}`) };
    const remote = await startFakeHttpMcp({ tools: catalog });
    try {
      const servers = buildMcpServers({ threadId: "pi-whop-scoped", text: "Go", toolScope, integrations: { custom: { whop: { type: "http", url: remote.url, headers: {} } } } });
      scopeConfig(toolScope, { mcpServers: servers, approvalMode: "ask" });
      const f = scopedApi(["read", "whop_search_tools", "whop_describe_tool", "whop_call_tool"]);
      await extension(f.api);
      try {
        expect(f.tools.map((tool) => tool.name)).toEqual(["whop_search_tools", "whop_describe_tool", "whop_call_tool"]);
        await f.handlers.get("session_start")?.({});
        expect(f.current()).toEqual(["whop_search_tools", "whop_describe_tool", "whop_call_tool"]);
        expect(await f.handlers.get("tool_call")?.({ toolName: "whop_call_tool" })).toBeUndefined();
        const asked: string[] = [];
        const ui = { confirm: async (title: string) => { asked.push(title); return true; } };
        await expect(f.tools[2].execute("excluded", { name: "payments_create", arguments: {} }, undefined, undefined, { ui })).rejects.toThrow("Tool selection excludes this tool");
        expect(asked).toEqual([]);
        expect(remote.calls).toEqual([]);
        const found = await f.tools[0].execute("search", { query: "create payments", limit: 20 }, undefined, undefined, { ui });
        const names = JSON.parse((found.content[0] as { text: string }).text).matches.map((match: { name: string }) => match.name);
        expect(names.every((name: string) => selected.includes(name))).toBe(true);
        await f.tools[2].execute("selected", { name: "payments_list", arguments: { company_id: "biz_1" } }, undefined, undefined, { ui });
        expect(asked).toEqual(["Allow whop:payments_list?"]);
        expect(remote.calls).toEqual([{ name: "payments_list", arguments: { company_id: "biz_1" } }]);
      } finally { await f.handlers.get("session_shutdown")?.(); }
    } finally { await remote.close(); }
  });

  it("keeps earlier tools alive when a later registration fails", async () => {
    const script = fakeMcpScript(`
      let buffer = "";
      process.stdin.setEncoding("utf8");
      const send = (value) => process.stdout.write(JSON.stringify(value) + "\\n");
      process.stdin.on("data", (chunk) => {
        buffer += chunk;
        let newline;
        while ((newline = buffer.indexOf("\\n")) !== -1) {
          const line = buffer.slice(0, newline); buffer = buffer.slice(newline + 1);
          if (!line.trim()) continue;
          const msg = JSON.parse(line);
          if (msg.method === "initialize") send({ jsonrpc: "2.0", id: msg.id, result: { capabilities: { tools: {} } } });
          else if (msg.method === "tools/list") send({ jsonrpc: "2.0", id: msg.id, result: { tools: [
            { name: "good", inputSchema: { type: "object", properties: {} } },
            { name: "bad", inputSchema: { type: "object", properties: {} } }
          ] } });
          else if (msg.method === "tools/call") send({ jsonrpc: "2.0", id: msg.id, result: { content: [{ type: "text", text: "still alive" }] } });
        }
      });
    `);
    const dir = tempDir();
    const config = join(dir, "mcp.json");
    writeFileSync(config, JSON.stringify({ mcpServers: { test: { command: process.execPath, args: [script] } } }));
    process.env.OMB_MCP_CONFIG = config;

    const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
    const tools: RegisteredTool[] = [];
    const handlers = new Map<string, ShutdownHandler>();
    await extension({
      registerTool(tool) {
        if (tool.name.endsWith("_bad")) throw new Error("synthetic registration failure");
        tools.push(tool);
      },
      on(event, handler) {
        handlers.set(event, handler);
      },
    });

    expect(tools).toHaveLength(1);
    expect(stderr).toHaveBeenCalledWith(expect.stringContaining("synthetic registration failure"));
    await expect(
      tools[0].execute("call-1", {}, undefined, undefined, { ui: { confirm: async () => true } }),
    ).resolves.toMatchObject({ content: [{ type: "text", text: "still alive" }] });
    await handlers.get("session_shutdown")?.();
  });

  it("throws when an MCP tool result declares isError", async () => {
    const script = fakeMcpScript(`
      let buffer = "";
      process.stdin.setEncoding("utf8");
      const send = (value) => process.stdout.write(JSON.stringify(value) + "\\n");
      process.stdin.on("data", (chunk) => {
        buffer += chunk;
        let newline;
        while ((newline = buffer.indexOf("\\n")) !== -1) {
          const line = buffer.slice(0, newline); buffer = buffer.slice(newline + 1);
          if (!line.trim()) continue;
          const msg = JSON.parse(line);
          if (msg.method === "initialize") send({ jsonrpc: "2.0", id: msg.id, result: { capabilities: { tools: {} } } });
          else if (msg.method === "tools/list") send({ jsonrpc: "2.0", id: msg.id, result: { tools: [{ name: "fail", inputSchema: { type: "object", properties: {} } }] } });
          else if (msg.method === "tools/call") send({ jsonrpc: "2.0", id: msg.id, result: { isError: true, content: [{ type: "text", text: "remote failure" }] } });
        }
      });
    `);
    const dir = tempDir();
    const config = join(dir, "mcp.json");
    writeFileSync(config, JSON.stringify({ mcpServers: { test: { command: process.execPath, args: [script] } } }));
    process.env.OMB_MCP_CONFIG = config;

    const tools: RegisteredTool[] = [];
    const handlers = new Map<string, ShutdownHandler>();
    await extension({
      registerTool(tool) {
        tools.push(tool);
      },
      on(event, handler) {
        handlers.set(event, handler);
      },
    });

    expect(tools).toHaveLength(1);
    await expect(
      tools[0].execute("call-1", {}, undefined, undefined, { ui: { confirm: async () => true } }),
    ).rejects.toThrow("remote failure");
    await handlers.get("session_shutdown")?.();
  });
});

describe("Pi ask_user tool", () => {
  const load = async (config: Record<string, unknown>, active = ["read", "ask_user"]) => {
    const dir = tempDir();
    process.env.OMB_MCP_CONFIG = join(dir, "mcp.json");
    writeFileSync(process.env.OMB_MCP_CONFIG, JSON.stringify(config));
    const tools: RegisteredTool[] = [];
    let current = active;
    await extension({
      registerTool(tool) { tools.push(tool); },
      on() {},
      getActiveTools: () => [...current],
      setActiveTools: (names) => { current = names; },
    });
    return tools.find((tool) => tool.name === "ask_user");
  };
  // What pi's RPC mode does with each dialog: the driver answers with the
  // picked label, the person's typed words, or a cancel (undefined).
  const dialogs = (replies: Array<string | undefined>) => {
    const asked: Array<{ method: string; title: string; options?: string[] }> = [];
    const next = async () => replies.shift();
    return { asked, ctx: { ui: {
      confirm: async () => false,
      select: async (title: string, options: string[]) => { asked.push({ method: "select", title, options }); return next(); },
      input: async (title: string) => { asked.push({ method: "input", title }); return next(); },
    } } };
  };
  const QUESTIONS = { questions: [
    { question: "Which color?", options: [{ label: "Blue" }, { label: "Green", description: "the calm one" }] },
    { question: "Any notes?" },
  ] };

  it("registers only when the driver allows it and the selection includes native:ask_user", async () => {
    expect(await load({})).toBeUndefined();
    expect(await load({ askUser: true, toolScope: { allow: ["native:read"] } })).toBeUndefined();
    const tool = await load({ askUser: true, toolScope: { allow: ["native:ask_user"] } });
    expect(tool).toMatchObject({ name: "ask_user", exposure: "model-only" });
  });

  it("asks each question in turn and returns the answers in the shared Q:/A: form", async () => {
    const tool = (await load({ askUser: true }))!;
    const { asked, ctx } = dialogs(["Green", "Ship it on Friday"]);
    const result = await tool.execute("call-1", QUESTIONS, undefined, undefined, ctx);
    expect(asked).toEqual([
      // the description shows under the question; the labels stay exact
      { method: "select", title: "Which color?\n\nGreen: the calm one", options: ["Blue", "Green"] },
      { method: "input", title: "Any notes?" },
    ]);
    expect(result.content).toEqual([{ type: "text", text:
      "The user answered your questions.\n\nQ: Which color?\nA: Green\n\nQ: Any notes?\nA: Ship it on Friday" }]);
  });

  it("returns the person's own words when they answer a select in free text", async () => {
    const tool = (await load({ askUser: true }))!;
    const result = await tool.execute("call-2", { questions: [QUESTIONS.questions[0]] }, undefined, undefined, dialogs(["Purple, please"]).ctx);
    expect(result.content).toEqual([{ type: "text", text: "The user answered your questions.\n\nQ: Which color?\nA: Purple, please" }]);
  });

  it("says plainly when nobody answered, and stops at the first unanswered question", async () => {
    const tool = (await load({ askUser: true }))!;
    await expect(tool.execute("call-3", QUESTIONS, undefined, undefined, dialogs([undefined]).ctx)).rejects.toThrow(/did not answer/);
    const partial = dialogs(["Blue", undefined]);
    const result = await tool.execute("call-4", QUESTIONS, undefined, undefined, partial.ctx);
    expect(partial.asked).toHaveLength(2);
    expect(result.content[0]).toMatchObject({ text: expect.stringContaining("Q: Which color?\nA: Blue\n\nThe person did not answer the other questions.") });
  });

  it("refuses multi-select and malformed calls with words the model can act on", async () => {
    const tool = (await load({ askUser: true }))!;
    const { asked, ctx } = dialogs([]);
    await expect(tool.execute("call-5", { questions: [{ question: "Which?", multiSelect: true, options: [{ label: "A" }, { label: "B" }] }] }, undefined, undefined, ctx))
      .rejects.toThrow(/one answer per question/);
    await expect(tool.execute("call-6", { questions: [] }, undefined, undefined, ctx)).rejects.toThrow(/malformed/);
    expect(asked).toEqual([]);
  });
});