Skip to content

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

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

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { recordEvents } from "../testing/events.ts";
import { buildTurnContext, replaysNatively } from "../turn-context.ts";
import { instanceConfigs } from "../config.ts";
import { OpenAICompatDriver, servesLocalModel } from "./openai-compat.ts";
import { flushNativeLogs } from "./native.ts";

describe("OpenAICompatDriver", () => {
  const savedUrl = process.env.OPENAI_COMPAT_URL;
  const savedKey = process.env.OPENAI_COMPAT_API_KEY;

  beforeEach(() => {
    delete process.env.OPENAI_COMPAT_URL;
    delete process.env.OPENAI_COMPAT_API_KEY;
  });

  afterEach(() => {
    if (savedUrl === undefined) delete process.env.OPENAI_COMPAT_URL;
    else process.env.OPENAI_COMPAT_URL = savedUrl;
    if (savedKey === undefined) delete process.env.OPENAI_COMPAT_API_KEY;
    else process.env.OPENAI_COMPAT_API_KEY = savedKey;
    vi.unstubAllGlobals();
    vi.restoreAllMocks();
  });

  it("registers with the openai-compat kind and a display name", () => {
    expect(OpenAICompatDriver.driverKind).toBe("openai-compat");
    expect(OpenAICompatDriver.metadata.displayName).toBe("Other (OpenAI-compatible)");
    expect(OpenAICompatDriver.metadata.access).toBe("api");
  });

  it("falls back to the OpenRouter endpoint by default", () => {
    const cfg = OpenAICompatDriver.defaultConfig();
    expect(cfg.url).toBe("https://openrouter.ai/api/v1");
    expect(cfg.apiKeyEnv).toBe("OPENAI_COMPAT_API_KEY");
  });

  it.each([
    ["I'll open the browser first.", {}, 2],
    ["I'm working in the Cua Local VM and will launch XFCE Terminal first.", {}, 2],
    ["The terminal is open and the task is complete.", {}, 1],
    ["Should I open the terminal?", {}, 1],
    ["I'll open the browser first.", { tools: false }, 1],
  ] as const)("bounds OpenRouter announcement nudges: %s %j", async (text, config, count) => {
    const bodies: Array<{ model: string; messages: Array<{ role: string; content: string }> }> = [];
    vi.stubGlobal("fetch", vi.fn(async (url, init) => {
      if (String(url).endsWith("/models")) return Response.json({ data: [] });
      bodies.push(JSON.parse(String(init?.body)));
      return new Response(`data: ${JSON.stringify({ choices: [{ delta: { content: text }, finish_reason: "stop" }] })}\n\ndata: [DONE]\n\n`,
        { headers: { "content-type": "text/event-stream" } });
    }));
    const instance = await OpenAICompatDriver.create({
      instanceId: "openrouter-nudge", displayName: "OpenRouter", enabled: true,
      config: OpenAICompatDriver.decodeConfig({ model: "qwen/qwen3.5-35b-a3b", ...config }),
      environment: { OPENAI_COMPAT_API_KEY: "synthetic-fixture-key" },
    });
    const recorder = recordEvents(instance.adapter);
    try {
      await instance.adapter.sendTurn({ threadId: "nudge", text: "Open the terminal." });
      expect(await recorder.until(event => event.type === "turn.completed")).toMatchObject({ ok: count !== 2 });
      expect(bodies).toHaveLength(count);
      expect(bodies[0]!.model).toBe("qwen/qwen3.5-35b-a3b");
      if (count === 2) {
        expect(bodies[1]!.messages.slice(0, -2)).toEqual(bodies[0]!.messages);
        expect(bodies[1]!.messages.at(-2)).toEqual({ role: "assistant", content: text });
        expect(bodies[1]!.messages.at(-1)).toMatchObject({ role: "user", content: expect.stringContaining("called no tool") });
      }
    } finally {
      recorder.stop();
      await instance.dispose();
    }
  });

  it("honours an explicit url and apiKeyEnv override", () => {
    const cfg = OpenAICompatDriver.decodeConfig({
      url: "https://api.groq.com/openai/v1/",
      apiKeyEnv: "GROQ_KEY",
    });
    expect(cfg.url).toBe("https://api.groq.com/openai/v1");
    expect(cfg.apiKeyEnv).toBe("GROQ_KEY");
  });

  it("allows an isolated connection to disable an inherited OpenRouter provider pin", () => {
    const before = process.env.OPENAI_COMPAT_PROVIDER;
    process.env.OPENAI_COMPAT_PROVIDER = "fixture-upstream";
    try {
      expect(OpenAICompatDriver.decodeConfig({}).provider).toBe("fixture-upstream");
      expect(OpenAICompatDriver.decodeConfig({ provider: "" }).provider).toBeUndefined();
      expect(OpenAICompatDriver.decodeConfig({ provider: "another-upstream" }).provider).toBe("another-upstream");
    } finally {
      if (before === undefined) delete process.env.OPENAI_COMPAT_PROVIDER;
      else process.env.OPENAI_COMPAT_PROVIDER = before;
    }
  });

  it("keeps a provider's own instance off the workspace key, URL and model", async () => {
    process.env.OPENAI_COMPAT_API_KEY = "workspace-key";
    process.env.OPENAI_COMPAT_URL = "https://openrouter.ai/api/v1";
    const before = process.env.OPENAI_COMPAT_MODEL;
    process.env.OPENAI_COMPAT_MODEL = "meta-llama/llama-3.3-70b-instruct";
    try {
      const config = OpenAICompatDriver.decodeConfig({ url: "https://api.openai.com/v1", apiKeyEnv: "OMB_OPENAI_API_KEY", catalog: "openai" });
      expect(config).toMatchObject({ url: "https://api.openai.com/v1", catalog: "openai" });
      expect(config.model).toBeUndefined();
      const inst = await OpenAICompatDriver.create({
        instanceId: "openai", displayName: "OpenAI", enabled: true, config,
        environment: { OPENAI_COMPAT_API_KEY: "workspace-key" },
      });
      expect((await inst.snapshot()).state).toBe("unavailable");
      expect(inst.models.default).toBe("gpt-5");
      await inst.dispose();
      expect(OpenAICompatDriver.decodeConfig({ apiKeyEnv: "GROQ_KEY" }).model).toBe("meta-llama/llama-3.3-70b-instruct");
    } finally {
      if (before === undefined) delete process.env.OPENAI_COMPAT_MODEL;
      else process.env.OPENAI_COMPAT_MODEL = before;
    }
  });

  it("keeps an added server at its own address off the workspace model and provider", () => {
    process.env.OPENAI_COMPAT_URL = "https://first.example.test/v1";
    const saved = { model: process.env.OPENAI_COMPAT_MODEL, provider: process.env.OPENAI_COMPAT_PROVIDER };
    process.env.OPENAI_COMPAT_MODEL = "fugu";
    process.env.OPENAI_COMPAT_PROVIDER = "first-provider";
    try {
      const added = OpenAICompatDriver.decodeConfig({ url: "http://127.0.0.1:1234/v1", key: "fixture-added", catalog: "lmstudio" });
      expect(added.model).toBeUndefined();
      expect(added.provider).toBeUndefined();
      const sameHost = OpenAICompatDriver.decodeConfig({ url: "https://first.example.test/v1/", key: "fixture-same" });
      expect(sameHost).toMatchObject({ model: "fugu", provider: "first-provider" });
      expect(OpenAICompatDriver.decodeConfig({})).toMatchObject({ url: "https://first.example.test/v1", model: "fugu", provider: "first-provider" });
    } finally {
      for (const [name, value] of [["OPENAI_COMPAT_MODEL", saved.model], ["OPENAI_COMPAT_PROVIDER", saved.provider]] as const) {
        if (value === undefined) delete process.env[name];
        else process.env[name] = value;
      }
    }
  });

  it("lists only OpenAI's chat models, newest first", async () => {
    vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({ data: [
      { id: "gpt-4.1", created: 1 },
      { id: "text-embedding-3-large", created: 9 },
      { id: "gpt-5", created: 3 },
      { id: "gpt-4o-realtime-preview", created: 8 },
      { id: "dall-e-3", created: 7 },
      { id: "o3", created: 2 },
      { id: "gpt-5-codex", created: 6 },
    ] }), { status: 200 })));
    const inst = await OpenAICompatDriver.create({
      instanceId: "openai", displayName: "OpenAI", enabled: true,
      config: OpenAICompatDriver.decodeConfig({ url: "https://api.openai.com/v1", apiKeyEnv: "OMB_OPENAI_API_KEY", catalog: "openai" }),
      environment: { OMB_OPENAI_API_KEY: "sk-fixture" },
    });
    await vi.waitFor(() => expect(inst.models.options.map((option) => option.id)).toEqual(["gpt-5", "o3", "gpt-4.1"]));
    expect(inst.models.options.every((option) => !option.custom)).toBe(true);
    await inst.dispose();
  });

  it.each(["https://api.z.ai/api/paas/v4", "https://open.bigmodel.cn/api/paas/v4"])("discovers GLM models when the %s setup joins the first catalog read", async (url) => {
    let respond!: (response: Response) => void;
    const response = new Promise<Response>((resolve) => { respond = resolve; });
    const fetchMock = vi.fn(() => response);
    vi.stubGlobal("fetch", fetchMock);
    const instance = await OpenAICompatDriver.create({
      instanceId: "openai-compat-glm", displayName: "GLM", enabled: true,
      config: OpenAICompatDriver.decodeConfig({ url, key: "glm-test-key" }), environment: {},
    });
    try {
      expect(instance.models.options).toEqual([]);
      const firstList = instance.refreshModels?.();
      respond(Response.json({ data: [{ id: "glm-5.3" }, { id: "glm-4.6" }] }));
      await firstList;
      expect(fetchMock).toHaveBeenCalledExactlyOnceWith(`${url}/models`, expect.objectContaining({ headers: { authorization: "Bearer glm-test-key" } }));
      expect(instance.models).toMatchObject({ default: "glm-5.3", options: [{ id: "glm-5.3" }, { id: "glm-4.6" }] });
      expect(instance.models.options.every((option) => !option.custom)).toBe(true);
    } finally {
      await instance.dispose();
    }
  });

  it("retries a failed GLM discovery when setup is awaiting an explicit refresh", async () => {
    let respond!: (response: Response) => void;
    const first = new Promise<Response>((resolve) => { respond = resolve; });
    const fetchMock = vi.fn().mockReturnValueOnce(first).mockResolvedValueOnce(Response.json({ data: [{ id: "glm-5.3" }] }));
    vi.stubGlobal("fetch", fetchMock);
    const instance = await OpenAICompatDriver.create({
      instanceId: "glm", displayName: "GLM", enabled: true,
      config: OpenAICompatDriver.decodeConfig({ url: "https://api.z.ai/api/paas/v4", key: "test" }), environment: {},
    });
    try {
      const refreshed = instance.refreshModels?.();
      respond(new Response(null, { status: 503 }));
      await refreshed;
      expect(instance.models.options.map((option) => option.id)).toEqual(["glm-5.3"]);
      expect(fetchMock).toHaveBeenCalledTimes(2);
    } finally {
      await instance.dispose();
    }
  });

  it("lists Gemini's chat models by their plain IDs", async () => {
    const fetchMock = vi.fn(async () => Response.json({ object: "list", data: [
      { id: "models/gemini-2.5-pro", object: "model", owned_by: "google" },
      { id: "models/gemini-embedding-001", object: "model", owned_by: "google" },
      { id: "models/gemini-2.5-flash", object: "model", owned_by: "google" },
      { id: "models/gemini-2.5-flash-preview-tts", object: "model", owned_by: "google" },
      { id: "models/imagen-4.0-generate-001", object: "model", owned_by: "google" },
      { id: "models/gemini-2.5-flash-image", object: "model", owned_by: "google" },
      { id: "models/gemini-live-2.5-flash-preview", object: "model", owned_by: "google" },
    ] }));
    vi.stubGlobal("fetch", fetchMock);
    const inst = await OpenAICompatDriver.create({
      instanceId: "openai-compat-1", displayName: "Google Gemini", enabled: true,
      config: OpenAICompatDriver.decodeConfig({ url: "https://generativelanguage.googleapis.com/v1beta/openai", key: "gemini-fixture" }),
      environment: {},
    });
    await vi.waitFor(() => expect(inst.models.options.map((option) => option.id)).toEqual(["gemini-2.5-pro", "gemini-2.5-flash"]));
    expect(fetchMock).toHaveBeenCalledWith("https://generativelanguage.googleapis.com/v1beta/openai/models", expect.anything());
    await inst.dispose();
  });

  it("sends an attached video to Gemini as the clip itself, and to no other server", async () => {
    const dir = mkdtempSync(join(tmpdir(), "omb-gemini-video-"));
    const clip = join(dir, "clip.mp4");
    writeFileSync(clip, Buffer.from("00000018667479706d703432", "hex"));
    const bodies: string[] = [];
    vi.stubGlobal("fetch", vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
      if (String(input).endsWith("/models")) return Response.json({ data: [{ id: "models/gemini-2.5-flash" }] });
      bodies.push(String(init?.body));
      return new Response('data: {"choices":[{"index":0,"delta":{"content":"seen"},"finish_reason":"stop"}]}\n\n', { headers: { "content-type": "text/event-stream" } });
    }));
    const gemini = await OpenAICompatDriver.create({
      instanceId: "gemini-video", displayName: "Google Gemini", enabled: true,
      config: OpenAICompatDriver.decodeConfig({ url: "https://generativelanguage.googleapis.com/v1beta/openai", key: "gemini-fixture" }),
      environment: {},
    });
    const proxy = await OpenAICompatDriver.create({
      instanceId: "proxy-video", displayName: "Proxy", enabled: true,
      config: OpenAICompatDriver.decodeConfig({ url: "https://llm.example.com/v1", key: "sk-fixture" }), environment: {},
    });
    try {
      expect(gemini.adapter.capabilities.nativeVideoBytes).toBeGreaterThan(0);
      expect(proxy.adapter.capabilities.nativeVideoBytes).toBeUndefined();
      const recorder = recordEvents(gemini.adapter);
      await gemini.adapter.sendTurn({ threadId: "video", text: "What happens in this clip?", model: "gemini-2.5-flash",
        videos: [{ path: clip, mime: "video/mp4", bytes: 12 }] });
      await recorder.until((event) => event.type === "turn.completed");
      recorder.stop();
      const content = JSON.parse(bodies[0]!).messages.at(-1).content;
      expect(content).toContainEqual({ type: "image_url", image_url: { url: `data:video/mp4;base64,${readFileSync(clip).toString("base64")}` } });
    } finally {
      await gemini.dispose();
      await proxy.dispose();
      rmSync(dir, { recursive: true, force: true });
    }
  });

  it("keeps other servers' model IDs as they list them", async () => {
    vi.stubGlobal("fetch", vi.fn(async () => Response.json({ data: [{ id: "models/gemini-2.5-pro" }, { id: "text-embedding-3-small" }] })));
    const inst = await OpenAICompatDriver.create({
      instanceId: "openai-compat-2", displayName: "Proxy", enabled: true,
      config: OpenAICompatDriver.decodeConfig({ url: "https://llm.example.com/v1", key: "sk-fixture" }),
      environment: {},
    });
    await vi.waitFor(() => expect(inst.models.options.map((option) => option.id)).toEqual(["models/gemini-2.5-pro", "text-embedding-3-small"]));
    await inst.dispose();
  });

  it("reports unavailable without an API key", async () => {
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-1",
      displayName: "Free",
      enabled: true,
      config: { url: "https://openrouter.ai/api/v1", apiKeyEnv: "OPENAI_COMPAT_API_KEY" },
      environment: {},
    });
    const snap = await inst.snapshot();
    expect(snap.state).toBe("unavailable");
    await inst.dispose();
  });

  it("sends setup to Settings → AI → Providers instead of a terminal", async () => {
    expect(OpenAICompatDriver.install?.command).toBeUndefined();
    expect(OpenAICompatDriver.install?.settings).toBe("connections");
    expect(OpenAICompatDriver.install?.signInCommand).toContain("Settings → AI → Providers");
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-setup", displayName: "Router", enabled: true,
      config: { url: "https://openrouter.ai/api/v1", apiKeyEnv: "OPENAI_COMPAT_API_KEY" }, environment: {},
    });
    const snap = await inst.snapshot();
    expect(snap).toMatchObject({ state: "unavailable", reason: expect.stringContaining("Settings → AI → Providers") });
    expect(JSON.stringify(snap)).not.toContain("config.json");
    await inst.dispose();
  });

  it.each([
    ["its own URL", { url: "https://third-party.example.test/v1" }, ["WORKSPACE"]],
    ["its own key variable", { url: "https://third-party.example.test/v1", apiKeyEnv: "THIRD_PARTY_KEY" }, ["WORKSPACE", "OPERATOR"]],
  ])("sends the workspace key nowhere near an instance with %s", async (_label, config, absent) => {
    process.env.OPENAI_COMPAT_API_KEY = "sk-or-OPERATOR";
    const seen: string[] = [];
    vi.stubGlobal("fetch", vi.fn(async (_url: string | URL | Request, init?: RequestInit) => {
      seen.push(JSON.stringify(init?.headers ?? {}));
      return new Response('{"data":[]}', { headers: { "content-type": "application/json" } });
    }));
    const map = instanceConfigs({
      openaiCompat: { key: "sk-or-WORKSPACE" },
      instances: { claude: { driver: "claudeAgent" }, router: { driver: "openai-compat", config } },
    });
    const entry = map.router!;
    const inst = await OpenAICompatDriver.create({
      instanceId: "router", displayName: "Router", enabled: true,
      config: OpenAICompatDriver.decodeConfig(entry.config), environment: entry.environment as Record<string, string>,
    });
    await inst.refreshModels?.();
    await inst.snapshot();
    for (const secret of absent) expect(JSON.stringify(seen)).not.toContain(secret);
    await inst.dispose();
  });

  it("keeps the desktop store's workspace key away from an instance with its own URL", async () => {
    vi.stubEnv("OMB_DESKTOP_PARENT", "1");
    process.env.OPENAI_COMPAT_API_KEY = "sk-or-STORE";
    const seen: string[] = [];
    vi.stubGlobal("fetch", vi.fn(async (_url: string | URL | Request, init?: RequestInit) => {
      seen.push(JSON.stringify(init?.headers ?? {}));
      return new Response('{"data":[]}', { headers: { "content-type": "application/json" } });
    }));
    const inst = await OpenAICompatDriver.create({
      instanceId: "router", displayName: "Router", enabled: true,
      config: OpenAICompatDriver.decodeConfig({ url: "https://third-party.example.test/v1" }), environment: {},
    });
    await inst.refreshModels?.();
    await inst.snapshot();
    expect(JSON.stringify(seen)).not.toContain("STORE");
    await inst.dispose();
  });

  it("rejects remote HTTP computer use before starting tools or a completion request", async () => {
    const request = vi.fn(async (_url: string | URL | Request) => new Response('{"data":[]}', { headers: { "content-type": "application/json" } }));
    vi.stubGlobal("fetch", request);
    const inst = await OpenAICompatDriver.create({ instanceId: "cleartext", displayName: "Fixture", enabled: true,
      config: { url: "http://remote.example.test/v1", apiKeyEnv: "FIXTURE_KEY" }, environment: { FIXTURE_KEY: "synthetic" } });
    try {
      await expect(inst.adapter.sendTurn({ threadId: "cleartext", text: "Inspect the screen", model: "fixture",
        integrations: { localComputer: { command: "must-not-start", args: [], env: {} } } })).rejects.toThrow("require HTTPS");
      expect(request.mock.calls.some(call => String(call[0]).includes("chat/completions"))).toBe(false);
    } finally { await inst.dispose(); }
  });
  it("smoke: offers ask_user and returns the person's reply verbatim", async () => {
    const askBody = 'data: {"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"ask1","type":"function","function":{"name":"ask_user","arguments":'
      + JSON.stringify(JSON.stringify({ questions: [{ question: "Ship the fixture?", options: [{ label: "Yes" }, { label: "No" }] }] }))
      + '}}]}}]}\n\n'
      + 'data: {"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}\n\n';
    const finalBody = 'data: {"choices":[{"index":0,"delta":{"content":"done"}}]}\n\n'
      + 'data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n';
    const bodies: string[] = [];
    vi.stubGlobal("fetch", vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
      if (String(input).endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
      bodies.push(String(init?.body));
      return new Response(bodies.length === 1 ? askBody : finalBody, { status: 200, headers: { "content-type": "text/event-stream" } });
    }));
    const inst = await OpenAICompatDriver.create({
      instanceId: "compat-ask", displayName: "Compat", enabled: true,
      config: { url: "https://api.example.com/v1", apiKeyEnv: "OPENAI_COMPAT_API_KEY" },
      environment: { OPENAI_COMPAT_API_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);
    await inst.adapter.sendTurn({ threadId: "thread", text: "hi", model: "vendor/model" });
    const opened = await recorder.until((event) => event.type === "request.opened");
    expect(opened).toMatchObject({ requestType: "question", tool: "ask_user", choices: ["Yes", "No"] });
    const reply = "The user answered your questions.\n\nQ: Ship the fixture?\nA: Yes";
    expect(await inst.adapter.respondToRequest("thread", opened.requestId!, { behavior: "answer", message: reply })).toBe("answered");
    const completed = await recorder.until((event) => event.type === "turn.completed");
    expect(completed).toMatchObject({ ok: true });
    expect(bodies[0]).toContain('"ask_user"');
    expect(JSON.parse(JSON.parse(bodies[1]!).messages.at(-1).content).result).toBe(reply);
    recorder.stop();
    await inst.dispose();
  }, 20_000);

  it.each([
    ["a network failure", () => { throw new TypeError("fetch failed"); }],
    ["a provider 503", () => new Response("overloaded", { status: 503 })],
  ])("retries %s instead of failing the turn", async (_label, firstAnswer) => {
    const reply = 'data: {"choices":[{"index":0,"delta":{"content":"done"}}]}\n\ndata: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n';
    let completions = 0;
    vi.stubGlobal("fetch", vi.fn(async (input: string | URL | Request) => {
      if (String(input).endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
      completions += 1;
      return completions === 1 ? firstAnswer() : new Response(reply, { status: 200, headers: { "content-type": "text/event-stream" } });
    }));
    const inst = await OpenAICompatDriver.create({
      instanceId: "compat-retry", displayName: "Compat", enabled: true,
      config: { url: "https://api.example.com/v1", apiKeyEnv: "OPENAI_COMPAT_API_KEY" }, environment: { OPENAI_COMPAT_API_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);
    await inst.adapter.sendTurn({ threadId: "thread", text: "hi", model: "vendor/model" });
    expect(await recorder.until((event) => event.type === "turn.retrying")).toMatchObject({ attempt: 1 });
    expect(await recorder.until((event) => event.type === "turn.completed")).toMatchObject({ ok: true });
    expect(completions).toBe(2);
    recorder.stop();
    await inst.dispose();
  });

  it("exposes a refreshed model catalog", async () => {
    vi.stubGlobal(
      "fetch",
      vi.fn(async () =>
        new Response(
          JSON.stringify({
            data: [
              { id: "vendor/model-a", name: "Model A" },
              { id: "vendor/model-b" },
            ],
          }),
          { status: 200, headers: { "content-type": "application/json" } },
        ),
      ),
    );
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-models",
      displayName: "Models",
      enabled: true,
      config: { url: "https://example.test/v1", apiKeyEnv: "TEST_KEY" },
      environment: { TEST_KEY: "secret" },
    });

    await inst.refreshModels?.();

    // An `access: "custom"` engine's picker shows only custom-flagged options.
    expect(inst.models).toEqual({
      default: "vendor/model-a",
      options: [
        { id: "vendor/model-a", label: "Model A", custom: true, variants: ["low", "medium", "high", "xhigh", "max"].map((id) => ({ id, label: id })) },
        { id: "vendor/model-b", label: "vendor/model-b", custom: true, variants: ["low", "medium", "high", "xhigh", "max"].map((id) => ({ id, label: id })) },
      ],
    });
    await inst.dispose();
  });

  it("lists LM Studio chat keys from the native catalog and skips the OpenAI variant id", async () => {
    const fetchMock = vi.fn(async (input: string | URL | Request) => {
      const url = String(input);
      if (url.endsWith("/api/v1/models")) {
        return Response.json({
          models: [
            { type: "llm", key: "google/gemma-4-26b-a4b" },
            { type: "embedding", key: "acme/vector-model" },
            { type: "decision", key: "acme/router" },
          ],
        });
      }
      if (url.endsWith("/v1/models")) return Response.json({ data: [{ id: "google/gemma-4-26b-a4b@q4_k_m" }] });
      return new Response("no", { status: 500 });
    });
    vi.stubGlobal("fetch", fetchMock);
    const inst = await OpenAICompatDriver.create({
      instanceId: "lm-studio",
      displayName: "LM Studio",
      enabled: true,
      config: { url: "http://127.0.0.1:1234/v1", key: "lm-studio", apiKeyEnv: "OPENAI_COMPAT_API_KEY", catalog: "lmstudio" },
      environment: {},
    });
    await inst.refreshModels?.();
    expect(inst.models.options.map((option) => option.id)).toEqual(["google/gemma-4-26b-a4b"]);
    expect(fetchMock.mock.calls.some((call) => {
      const url = String(call[0]);
      return url.endsWith("/v1/models") && !url.includes("/api/v1/");
    })).toBe(false);
    await inst.dispose();
  });

  it("keeps a configured LM Studio model when the native catalog omits it", async () => {
    vi.stubGlobal("fetch", vi.fn(async (input: string | URL | Request) => {
      const url = String(input);
      if (url.endsWith("/api/v1/models")) {
        return Response.json({ models: [{ type: "llm", key: "other/model" }] });
      }
      return new Response("no", { status: 500 });
    }));
    const inst = await OpenAICompatDriver.create({
      instanceId: "lm-studio",
      displayName: "LM Studio",
      enabled: true,
      config: {
        url: "http://127.0.0.1:1234/v1",
        key: "lm-studio",
        apiKeyEnv: "OPENAI_COMPAT_API_KEY",
        model: "my-loaded-instance",
        catalog: "lmstudio",
      },
      environment: {},
    });
    await inst.refreshModels?.();
    expect(inst.models.default).toBe("my-loaded-instance");
    expect(inst.models.options.map((option) => option.id)).toEqual(["my-loaded-instance", "other/model"]);
    await inst.dispose();
  });

  it("asks only a server added as LM Studio for LM Studio's own catalog", async () => {
    const fetchMock = vi.fn(async (_input: string | URL | Request) => Response.json({ data: [{ id: "local/model" }] }));
    vi.stubGlobal("fetch", fetchMock);
    const inst = await OpenAICompatDriver.create({
      instanceId: "local-server",
      displayName: "llama.cpp",
      enabled: true,
      config: { url: "http://127.0.0.1:8080/v1", key: "local", apiKeyEnv: "OPENAI_COMPAT_API_KEY" },
      environment: {},
    });
    await inst.refreshModels?.();
    const asked = fetchMock.mock.calls.map(([input]) => String(input));
    expect(asked).toContain("http://127.0.0.1:8080/v1/models");
    expect(asked.filter((url) => url.includes("/api/v1/models"))).toEqual([]);
    expect(inst.models.options.map((option) => option.id)).toContain("local/model");
    await inst.dispose();
  });

  it("falls back to OpenAI-compatible models when the native LM Studio catalog request fails", async () => {
    const fetchMock = vi.fn(async (input: string | URL | Request) => {
      const url = String(input);
      if (url.endsWith("/api/v1/models")) throw new Error("native offline");
      if (url.endsWith("/v1/models")) return Response.json({ data: [{ id: "local-chat" }] });
      return new Response("no", { status: 500 });
    });
    vi.stubGlobal("fetch", fetchMock);
    const inst = await OpenAICompatDriver.create({
      instanceId: "lm-studio",
      displayName: "LM Studio",
      enabled: true,
      config: { url: "http://127.0.0.1:1234/v1", key: "lm-studio", apiKeyEnv: "OPENAI_COMPAT_API_KEY", catalog: "lmstudio" },
      environment: {},
    });
    await inst.refreshModels?.();
    expect(inst.models.options.map((option) => option.id)).toEqual(["local-chat"]);
    expect(fetchMock.mock.calls.some((call) => String(call[0]).endsWith("/v1/models"))).toBe(true);
    await inst.dispose();
  });

  it("uses the OpenAI model list when the LM Studio catalog is not JSON", async () => {
    vi.stubGlobal("fetch", vi.fn(async (input: string | URL | Request) => {
      const url = String(input);
      if (url.endsWith("/api/v1/models")) return new Response("not-json", { status: 200, headers: { "content-type": "text/plain" } });
      if (url.endsWith("/v1/models")) return Response.json({ data: [{ id: "local-chat" }] });
      return new Response("no", { status: 500 });
    }));
    const inst = await OpenAICompatDriver.create({
      instanceId: "lm-studio",
      displayName: "LM Studio",
      enabled: true,
      config: { url: "http://127.0.0.1:1234/v1", key: "lm-studio", apiKeyEnv: "OPENAI_COMPAT_API_KEY", catalog: "lmstudio" },
      environment: {},
    });
    await inst.refreshModels?.();
    expect(inst.models.options.map((option) => option.id)).toEqual(["local-chat"]);
    await inst.dispose();
  });

  it("includes streamed token totals and bounds the cancellable stream", async () => {
    const anySignal = vi.spyOn(AbortSignal, "any");
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request) => {
        const url = String(input);
        if (url.endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
        return new Response(
          'data: {"choices":[{"delta":{"content":"hello"}}]}\n' +
            'data: {"choices":[],"usage":{"prompt_tokens":12,"completion_tokens":3}}\n' +
            "data: [DONE]\n",
          { status: 200, headers: { "content-type": "text/event-stream" } },
        );
      }),
    );
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-turn",
      displayName: "Turn",
      enabled: true,
      config: { url: "https://example.test/v1", apiKeyEnv: "TEST_KEY" },
      environment: { TEST_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);

    await inst.adapter.sendTurn({ threadId: "thread", text: "private prompt", model: "vendor/model" });
    const completed = await recorder.until((event) => event.type === "turn.completed");

    expect(completed).toMatchObject({ ok: true, usage: { input: 12, output: 3 } });
    expect(anySignal).toHaveBeenCalledTimes(1);
    recorder.stop();
    await inst.dispose();
  });

  it("streams reasoning separately and completes only actual assistant text", async () => {
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request) => {
        if (String(input).endsWith("/models")) {
          return new Response(JSON.stringify({ data: [] }), { status: 200 });
        }
        return new Response(
          'data: {"choices":[{"delta":{"reasoning_content":"thinking"}}]}\n' +
            'data: {"choices":[{"delta":{"content":"answer"}}]}\n' +
            "data: [DONE]\n",
          { status: 200, headers: { "content-type": "text/event-stream" } },
        );
      }),
    );
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-reasoning-stream",
      displayName: "Reasoning",
      enabled: true,
      config: { url: "https://example.test/v1", apiKeyEnv: "TEST_KEY" },
      environment: { TEST_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);

    await inst.adapter.sendTurn({ threadId: "reasoning-thread", text: "question", model: "vendor/model" });
    await recorder.until((event) => event.type === "turn.completed");

    expect(recorder.events).toEqual(expect.arrayContaining([
      expect.objectContaining({ type: "content.delta", streamKind: "reasoning_text", delta: "thinking" }),
      expect.objectContaining({ type: "content.delta", streamKind: "assistant_text", delta: "answer" }),
      expect.objectContaining({ type: "item.completed", itemType: "assistant_text", text: "answer" }),
    ]));
    expect(recorder.events).not.toContainEqual(
      expect.objectContaining({ type: "item.completed", itemType: "assistant_text", text: "thinking" }),
    );
    recorder.stop();
    await inst.dispose();
  });

  it("uses reasoning as a helper-model fallback when normal content is whitespace", async () => {
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request) => {
        if (String(input).endsWith("/models")) {
          return new Response(JSON.stringify({ data: [] }), { status: 200 });
        }
        return new Response(JSON.stringify({
          choices: [{ message: { content: "  ", reasoning_content: "usable result" } }],
        }), { status: 200, headers: { "content-type": "application/json" } });
      }),
    );
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-reasoning-helper",
      displayName: "Reasoning helper",
      enabled: true,
      config: { url: "https://example.test/v1", apiKeyEnv: "TEST_KEY" },
      environment: { TEST_KEY: "secret" },
    });

    await expect(inst.generateText?.("question")).resolves.toBe("usable result");
    await inst.dispose();
  });

  it("falls back to reasoning_content when content is empty (streaming)", async () => {
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request) => {
        if (String(input).endsWith("/models")) {
          return new Response(JSON.stringify({ data: [] }), { status: 200 });
        }
        return new Response(
          'data: {"choices":[{"delta":{"reasoning_content":"thinking through the problem"}}]}\n' +
            'data: {"choices":[{"delta":{"content":""}}]}\n' +
            'data: {"choices":[],"usage":{"prompt_tokens":10,"completion_tokens":5}}\n' +
            "data: [DONE]\n",
          { status: 200, headers: { "content-type": "text/event-stream" } },
        );
      }),
    );
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-reasoning-fallback-stream",
      displayName: "Reasoning Fallback",
      enabled: true,
      config: { url: "https://example.test/v1", apiKeyEnv: "TEST_KEY" },
      environment: { TEST_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);

    await inst.adapter.sendTurn({ threadId: "thread-rf", text: "prompt", model: "vendor/model" });
    const item = await recorder.until((e) => e.type === "item.completed");
    const completed = await recorder.until((e) => e.type === "turn.completed");

    expect(item).toMatchObject({
      type: "item.completed",
      itemType: "assistant_text",
      text: "thinking through the problem",
    });
    expect(completed).toMatchObject({ ok: true, usage: { input: 10, output: 5 } });

    const deltas = recorder.events.filter((e) => e.type === "content.delta");
    expect(deltas.some((d: any) => d.streamKind === "reasoning_text" && d.delta === "thinking through the problem")).toBe(true);

    recorder.stop();
    await inst.dispose();
  });

  it("decodes a default model and provider from config", () => {
    const cfg = OpenAICompatDriver.decodeConfig({
      model: "deepseek/deepseek-v4-flash-0731",
      provider: "fireworks",
    });
    expect(cfg.model).toBe("deepseek/deepseek-v4-flash-0731");
    expect(cfg.provider).toBe("fireworks");
  });

  it("seeds the picker with the configured default model", async () => {
    vi.stubGlobal(
      "fetch",
      vi.fn(async () => new Response(JSON.stringify({ data: [] }), { status: 200 })),
    );
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-default-model",
      displayName: "Default model",
      enabled: true,
      config: {
        url: "https://openrouter.ai/api/v1",
        apiKeyEnv: "TEST_KEY",
        model: "deepseek/deepseek-v4-flash-0731",
      },
      environment: { TEST_KEY: "secret" },
    });
    expect(inst.models.default).toBe("deepseek/deepseek-v4-flash-0731");
    expect(inst.models.options.some((o) => o.id === "deepseek/deepseek-v4-flash-0731")).toBe(true);
    await inst.dispose();
  });

  it("pins the OpenRouter upstream provider in the request body", async () => {
    let sentBody: any = null;
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
        const url = String(input);
        if (url.endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
        sentBody = JSON.parse(String(init?.body));
        return new Response(
          'data: {"choices":[{"delta":{"content":"hi"}}]}\n' + "data: [DONE]\n",
          { status: 200, headers: { "content-type": "text/event-stream" } },
        );
      }),
    );
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-provider-route",
      displayName: "Provider route",
      enabled: true,
      config: {
        url: "https://openrouter.ai/api/v1",
        apiKeyEnv: "TEST_KEY",
        provider: "fireworks",
      },
      environment: { TEST_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);

    await inst.adapter.sendTurn({
      threadId: "thread-p",
      text: "prompt",
      model: "deepseek/deepseek-v4-flash-0731",
    });
    await recorder.until((e) => e.type === "turn.completed");

    expect(sentBody?.model).toBe("deepseek/deepseek-v4-flash-0731");
    expect(sentBody?.provider).toEqual({ order: ["fireworks"], allow_fallbacks: false });
    recorder.stop();
    await inst.dispose();
  });

  it("asks OpenRouter to cache the prompt of a Claude model, keyed to the thread, and leaves every other model as it was", async () => {
    const sent: any[] = [];
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
        if (String(input).endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
        sent.push(JSON.parse(String(init?.body)));
        return new Response('data: {"choices":[{"delta":{"content":"hi"}}]}\n' + "data: [DONE]\n", { status: 200, headers: { "content-type": "text/event-stream" } });
      }),
    );
    const turn = async (url: string, model: string) => {
      const inst = await OpenAICompatDriver.create({
        instanceId: "test-cache", displayName: "Cache", enabled: true,
        config: { url, apiKeyEnv: "TEST_KEY", model }, environment: { TEST_KEY: "secret" },
      });
      const recorder = recordEvents(inst.adapter);
      await inst.adapter.sendTurn({ threadId: "thread-c", text: "prompt", model });
      await recorder.until((e) => e.type === "turn.completed");
      recorder.stop();
      await inst.dispose();
      return sent.at(-1);
    };

    const claude = await turn("https://openrouter.ai/api/v1", "anthropic/claude-sonnet-4.5");
    expect(claude.cache_control).toEqual({ type: "ephemeral" });
    expect(claude.session_id).toBe("thread-c");

    for (const body of [
      await turn("https://openrouter.ai/api/v1", "deepseek/deepseek-v4-flash-0731"),
      await turn("https://api.example.com/v1", "anthropic/claude-sonnet-4.5"),
    ]) {
      expect(body).not.toHaveProperty("cache_control");
      expect(body).not.toHaveProperty("session_id");
    }
  });

  it("U24 retest: requests stream_options.include_usage on a streamed turn, like minimax.ts does", async () => {
    let sentBody: any = null;
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
        const url = String(input);
        if (url.endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
        sentBody = JSON.parse(String(init?.body));
        return new Response(
          'data: {"choices":[{"delta":{"content":"hi"}}]}\n' +
            'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n' + "data: [DONE]\n",
          { status: 200, headers: { "content-type": "text/event-stream" } },
        );
      }),
    );
    // Local servers (oMLX in particular) emit stream usage only with stream_options.include_usage.
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-include-usage",
      displayName: "Include usage",
      enabled: true,
      config: { url: "http://host.lima.internal:9090/v1", apiKeyEnv: "TEST_KEY" },
      environment: { TEST_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);

    await inst.adapter.sendTurn({ threadId: "thread-u24", text: "prompt" });
    await recorder.until((e) => e.type === "turn.completed");

    expect(sentBody?.stream).toBe(true);
    expect(sentBody?.stream_options).toEqual({ include_usage: true });
    recorder.stop();
    await inst.dispose();
  });

  it("does not double the transcript when the thread was rewound", async () => {
    let sentBody: any = null;
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
        const url = String(input);
        if (url.endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
        sentBody = JSON.parse(String(init?.body));
        return new Response(
          'data: {"choices":[{"delta":{"content":"ok"}}]}\n' +
            'data: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n' + "data: [DONE]\n",
          { status: 200, headers: { "content-type": "text/event-stream" } },
        );
      }),
    );
    const transcript = [
      { role: "user" as const, text: "u25-sentinel-first-message" },
      { role: "assistant" as const, text: "u25-sentinel-first-reply" },
    ];
    // Mirrors server/index.ts: buildTurnContext inlines the transcript only for drivers that do not replay natively.
    const { turnText } = buildTurnContext({
      text: "second message",
      transcript,
      rewound: true,
      fresh: false,
      externallyUpdated: false,
      replaysNatively: replaysNatively("openai-compat"),
    });
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-u25",
      displayName: "U25",
      enabled: true,
      config: { url: "http://host.lima.internal:9090/v1", apiKeyEnv: "TEST_KEY" },
      environment: { TEST_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);

    await inst.adapter.sendTurn({ threadId: "thread-u25", text: turnText, transcript });
    await recorder.until((e) => e.type === "turn.completed");

    const serialized = JSON.stringify(sentBody?.messages);
    const occurrences = serialized.split("u25-sentinel-first-message").length - 1;
    expect(occurrences).toBe(1);
    recorder.stop();
    await inst.dispose();
  });

  it("keeps the system message to the stable half and carries the volatile half in the newest user message", async () => {
    let sentBody: any = null;
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
        const url = String(input);
        if (url.endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
        sentBody = JSON.parse(String(init?.body));
        return new Response(
          'data: {"choices":[{"delta":{"content":"hi"}}]}\n' + "data: [DONE]\n",
          { status: 200, headers: { "content-type": "text/event-stream" } },
        );
      }),
    );
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-prompt-split",
      displayName: "Prompt split",
      enabled: true,
      config: { url: "http://localhost:9/v1", apiKeyEnv: "TEST_KEY" },
      environment: { TEST_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);

    await inst.adapter.sendTurn({
      threadId: "thread-prompt-split",
      text: "hello",
      system: "Standing rules.\n\nMemory: likes quiet hours.",
      systemStable: "Standing rules.",
      systemVolatile: "Memory: likes quiet hours.",
      transcript: [
        { role: "user" as const, text: "earlier" },
        { role: "assistant" as const, text: "answer" },
      ],
    });
    await recorder.until((e) => e.type === "turn.completed");

    const messages: any[] = sentBody?.messages ?? [];
    expect(messages[0]).toEqual({ role: "system", content: "Standing rules." });
    expect(messages.slice(1, 3)).toEqual([
      { role: "user", content: "earlier" },
      { role: "assistant", content: "answer" },
    ]);
    expect(messages.at(-1)).toEqual({
      role: "user",
      content: "Context from OpenMausBot updated since this conversation started; it replaces any earlier copy:\n\nMemory: likes quiet hours.\n\nhello",
    });

    await inst.adapter.sendTurn({
      threadId: "thread-prompt-split-legacy",
      text: "bare",
      system: "Whole block.",
    });
    await recorder.until((e) => e.type === "turn.completed" && e.threadId === "thread-prompt-split-legacy");
    const legacy: any[] = sentBody?.messages ?? [];
    expect(legacy[0]).toEqual({ role: "system", content: "Whole block." });
    expect(legacy.at(-1)).toEqual({ role: "user", content: "bare" });
    recorder.stop();
    await inst.dispose();
  });

  describe("replays what each earlier user message actually carried", () => {
    const NOTE = "Context from OpenMausBot updated since this conversation started; it replaces any earlier copy:\n\n";
    // index.ts stamps the turn text (clock, recall) but stores only what the person typed.
    const clock = (minute: string) => `Current time when this message was sent: Friday, 2026-10-09 18:${minute} UTC (UTC+00:00).\n\n`;

    const fixture = async () => {
      const bodies: any[] = [];
      vi.stubGlobal(
        "fetch",
        vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
          if (String(input).endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
          bodies.push(JSON.parse(String(init?.body)));
          return new Response(
            'data: {"choices":[{"delta":{"content":"hi"}}]}\n' + "data: [DONE]\n",
            { status: 200, headers: { "content-type": "text/event-stream" } },
          );
        }),
      );
      const inst = await OpenAICompatDriver.create({
        instanceId: "test-replay-notes",
        displayName: "Replay notes",
        enabled: true,
        config: { url: "http://localhost:9/v1", apiKeyEnv: "TEST_KEY" },
        environment: { TEST_KEY: "secret" },
      });
      const recorder = recordEvents(inst.adapter);
      let turns = 0;
      const send = async (input: { text: string; volatile: string; transcript: Array<{ role: "user" | "assistant"; text: string }>; threadId?: string; mentionTurn?: boolean; recalled?: string }) => {
        const threadId = input.threadId ?? "thread-replay";
        const settled = ++turns;
        await inst.adapter.sendTurn({
          threadId,
          text: input.text,
          system: `Standing rules.\n\n${input.volatile}`,
          systemStable: "Standing rules.",
          systemVolatile: input.volatile,
          transcript: input.transcript,
          ...(input.mentionTurn ? { mentionTurn: true } : {}),
          ...(input.recalled ? { recalled: input.recalled } : {}),
        });
        await recorder.until((e) => e.type === "turn.completed" && e.threadId === threadId &&
          recorder.events.filter((x) => x.type === "turn.completed").length >= settled);
        return bodies.at(-1)!.messages as any[];
      };
      return { send, done: async () => { recorder.stop(); await inst.dispose(); } };
    };

    it("keeps the previous request a byte-identical prefix when the volatile half is unchanged", async () => {
      const { send, done } = await fixture();
      const first = await send({ text: clock("00") + "hello", volatile: "Memory: likes tea.", transcript: [] });
      const second = await send({
        text: clock("01") + "next",
        volatile: "Memory: likes tea.",
        transcript: [{ role: "user", text: "hello" }, { role: "assistant", text: "hi" }],
      });
      expect(second.slice(0, first.length)).toEqual(first);
      expect(first.at(-1)).toEqual({ role: "user", content: NOTE + "Memory: likes tea.\n\n" + clock("00") + "hello" });
      expect(second.slice(first.length)).toEqual([
        { role: "assistant", content: "hi" },
        { role: "user", content: clock("01") + "next" },
      ]);
      await done();
    });

    it("keeps the prefix and appends the new note when the volatile half changes", async () => {
      const { send, done } = await fixture();
      const first = await send({ text: clock("00") + "hello", volatile: "Memory: likes tea.", transcript: [] });
      const second = await send({
        text: clock("01") + "next",
        volatile: "Memory: likes coffee.",
        transcript: [{ role: "user", text: "hello" }, { role: "assistant", text: "hi" }],
      });
      expect(second.slice(0, first.length)).toEqual(first);
      expect(second.at(-1)).toEqual({ role: "user", content: NOTE + "Memory: likes coffee.\n\n" + clock("01") + "next" });
      const third = await send({
        text: "again",
        volatile: "",
        transcript: [
          { role: "user", text: "hello" }, { role: "assistant", text: "hi" },
          { role: "user", text: "next" }, { role: "assistant", text: "hi" },
        ],
      });
      expect(third.slice(0, second.length)).toEqual(second);
      expect(third.at(-1)).toEqual({
        role: "user",
        content: "The OpenMausBot context notes from earlier in this conversation have been cleared; the standing instructions still apply.\n\nagain",
      });
      await done();
    });

    it("re-delivers the note once the message that carried it leaves the replayed window", async () => {
      const { send, done } = await fixture();
      await send({ text: "hello", volatile: "Memory: likes tea.", transcript: [] });
      await send({ text: "next", volatile: "Memory: likes tea.", transcript: [{ role: "user", text: "hello" }, { role: "assistant", text: "hi" }] });
      const trimmed = await send({
        text: "third",
        volatile: "Memory: likes tea.",
        transcript: [
          { role: "assistant", text: "[2 earlier messages omitted from this context; full history remains in the app.]" },
          { role: "user", text: "next" }, { role: "assistant", text: "hi" },
        ],
      });
      expect(trimmed.slice(1, -1)).toEqual([
        { role: "assistant", content: "[2 earlier messages omitted from this context; full history remains in the app.]" },
        { role: "user", content: "next" },
        { role: "assistant", content: "hi" },
      ]);
      expect(trimmed.at(-1)).toEqual({ role: "user", content: NOTE + "Memory: likes tea.\n\nthird" });
      await done();
    });

    it("replays each turn's automatic recall and changing context as sent, so every request starts with the last", async () => {
      const { send, done } = await fixture();
      const transcript: Array<{ role: "user" | "assistant"; text: string }> = [];
      let previous: any[] = [];
      for (let turn = 0; turn < 20; turn++) {
        const minute = String(turn).padStart(2, "0");
        const recalled = `<recalled turn="${turn}">\n${"r".repeat(6000)}\n</recalled>`;
        const typed = `message ${turn}`;
        const messages = await send({
          text: `${clock(minute)}${recalled}\n\n${typed}`,
          recalled,
          volatile: `Recent work: ${"w".repeat(2000)} ${turn}`,
          transcript: [...transcript],
        });
        expect(messages.at(-1).content).toContain(recalled);
        if (turn > 0) expect(messages.slice(0, previous.length)).toEqual(previous);
        previous = messages;
        transcript.push({ role: "user", text: typed }, { role: "assistant", text: "hi" });
      }
      await done();
    });

    it("does not lend a record to a later identical message it did not send", async () => {
      const { send, done } = await fixture();
      await send({ text: clock("00") + "continue", volatile: "Memory: likes tea.", transcript: [] });
      const messages = await send({
        text: clock("05") + "next",
        volatile: "Memory: likes tea.",
        transcript: [
          { role: "user", text: "continue" }, { role: "assistant", text: "hi" },
          { role: "user", text: "continue" }, { role: "assistant", text: "done" },
        ],
      });
      expect(messages.slice(1, -1)).toEqual([
        { role: "user", content: NOTE + "Memory: likes tea.\n\n" + clock("00") + "continue" },
        { role: "assistant", content: "hi" },
        { role: "user", content: "continue" },
        { role: "assistant", content: "done" },
      ]);
      await done();
    });

    it("still delivers the note on a mention turn and on a thread it has not seen", async () => {
      const { send, done } = await fixture();
      await send({ text: "hello", volatile: "Tagged: @Testy", transcript: [] });
      const mention = await send({
        text: "@Testy again",
        volatile: "Tagged: @Testy",
        transcript: [{ role: "user", text: "hello" }, { role: "assistant", text: "hi" }],
        mentionTurn: true,
      });
      expect(mention.at(-1)).toEqual({ role: "user", content: NOTE + "Tagged: @Testy\n\n@Testy again" });
      const unseen = await send({
        threadId: "thread-unseen",
        text: "later",
        volatile: "Memory: likes tea.",
        transcript: [{ role: "user", text: "hello" }, { role: "assistant", text: "hi" }],
      });
      expect(unseen.slice(1)).toEqual([
        { role: "user", content: "hello" },
        { role: "assistant", content: "hi" },
        { role: "user", content: NOTE + "Memory: likes tea.\n\nlater" },
      ]);
      await done();
    });
  });

  it("omits provider routing on non-OpenRouter endpoints even when configured", async () => {
    let sentBody: any = null;
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
        const url = String(input);
        if (url.endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
        sentBody = JSON.parse(String(init?.body));
        return new Response(
          'data: {"choices":[{"delta":{"content":"hi"}}]}\n' + "data: [DONE]\n",
          { status: 200, headers: { "content-type": "text/event-stream" } },
        );
      }),
    );
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-groq-no-provider",
      displayName: "Groq strict",
      enabled: true,
      config: {
        url: "https://api.groq.com/openai/v1",
        apiKeyEnv: "TEST_KEY",
        provider: "fireworks",
      },
      environment: { TEST_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);

    await inst.adapter.sendTurn({ threadId: "thread-g", text: "prompt", model: "vendor/model" });
    await recorder.until((e) => e.type === "turn.completed");

    // Strict endpoints (Groq et al.) reject unknown top-level fields; `provider` is OpenRouter-only.
    expect(sentBody).not.toBeNull();
    expect("provider" in sentBody).toBe(false);
    recorder.stop();
    await inst.dispose();
  });

  it("does not treat a lookalike host as OpenRouter", async () => {
    let sentBody: any = null;
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
        const url = String(input);
        if (url.endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
        sentBody = JSON.parse(String(init?.body));
        return new Response(
          'data: {"choices":[{"delta":{"content":"hi"}}]}\n' + "data: [DONE]\n",
          { status: 200, headers: { "content-type": "text/event-stream" } },
        );
      }),
    );
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-lookalike",
      displayName: "Lookalike host",
      enabled: true,
      config: {
          // Not openrouter.ai: a lookalike domain or path segment must not match.
        url: "https://notopenrouter.ai/api/v1",
        apiKeyEnv: "TEST_KEY",
        provider: "fireworks",
      },
      environment: { TEST_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);

    await inst.adapter.sendTurn({ threadId: "thread-l", text: "prompt", model: "vendor/model" });
    await recorder.until((e) => e.type === "turn.completed");

    expect(sentBody).not.toBeNull();
    expect("provider" in sentBody).toBe(false);
    recorder.stop();
    await inst.dispose();
  });

  it("pins the provider on an OpenRouter subdomain", async () => {
    let sentBody: any = null;
    vi.stubGlobal(
      "fetch",
      vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
        const url = String(input);
        if (url.endsWith("/models")) return new Response(JSON.stringify({ data: [] }), { status: 200 });
        sentBody = JSON.parse(String(init?.body));
        return new Response(
          'data: {"choices":[{"delta":{"content":"hi"}}]}\n' + "data: [DONE]\n",
          { status: 200, headers: { "content-type": "text/event-stream" } },
        );
      }),
    );
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-subdomain",
      displayName: "OpenRouter subdomain",
      enabled: true,
      config: {
        url: "https://gateway.openrouter.ai/api/v1",
        apiKeyEnv: "TEST_KEY",
        provider: "fireworks",
      },
      environment: { TEST_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);

    await inst.adapter.sendTurn({ threadId: "thread-s", text: "prompt", model: "vendor/model" });
    await recorder.until((e) => e.type === "turn.completed");

    expect(sentBody?.provider).toEqual({ order: ["fireworks"], allow_fallbacks: false });
    recorder.stop();
    await inst.dispose();
  });

  it("aborts when the idle period elapses between stream chunks", async () => {
    vi.useFakeTimers();
    try {
      let controller: ReadableStreamDefaultController<Uint8Array>;
      const stream = new ReadableStream<Uint8Array>({
        start(c) {
          controller = c;
        },
      });

      const encoder = new TextEncoder();
      vi.stubGlobal(
        "fetch",
        vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
          if (String(input).endsWith("/models")) {
            return new Response(JSON.stringify({ data: [] }), { status: 200 });
          }
          init?.signal?.addEventListener("abort", () => {
            try {
              controller.error(init.signal?.reason ?? new DOMException("Aborted", "AbortError"));
            } catch {}
          });
          return new Response(stream, {
            status: 200,
            headers: { "content-type": "text/event-stream" },
          });
        }),
      );

      const inst = await OpenAICompatDriver.create({
        instanceId: "test-idle-timeout-fail",
        displayName: "Idle Timeout Fail",
        enabled: true,
        config: { url: "https://example.test/v1", apiKeyEnv: "TEST_KEY" },
        environment: { TEST_KEY: "secret" },
      });
      const recorder = recordEvents(inst.adapter);

      await inst.adapter.sendTurn({ threadId: "thread-idle-fail", text: "prompt", model: "vendor/model" });

      controller!.enqueue(encoder.encode('data: {"choices":[{"delta":{"content":"part 1"}}]}\n\n'));

      await vi.advanceTimersByTimeAsync(185_000);

      const completed = await recorder.until((e) => e.type === "turn.completed");
      expect(completed).toMatchObject({ ok: false, stopReason: "error" });

      recorder.stop();
      await inst.dispose();
    } finally {
      vi.useRealTimers();
    }
  });

  it("does not abort after 120s if the stream continues receiving active chunks (renewable idle timeout)", async () => {
    vi.useFakeTimers();
    try {
      let controller: ReadableStreamDefaultController<Uint8Array>;
      const stream = new ReadableStream<Uint8Array>({
        start(c) {
          controller = c;
        },
      });

      const encoder = new TextEncoder();
      vi.stubGlobal(
        "fetch",
        vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
          if (String(input).endsWith("/models")) {
            return new Response(JSON.stringify({ data: [] }), { status: 200 });
          }
          init?.signal?.addEventListener("abort", () => controller.error(init.signal?.reason));
          return new Response(stream, {
            status: 200,
            headers: { "content-type": "text/event-stream" },
          });
        }),
      );

      const inst = await OpenAICompatDriver.create({
        instanceId: "test-idle-timeout-renew",
        displayName: "Idle Timeout Renew",
        enabled: true,
        config: { url: "https://example.test/v1", apiKeyEnv: "TEST_KEY" },
        environment: { TEST_KEY: "secret" },
      });
      const recorder = recordEvents(inst.adapter);

      await inst.adapter.sendTurn({ threadId: "thread-idle-renew", text: "prompt", model: "vendor/model" });

      controller!.enqueue(encoder.encode('data: {"choices":[{"delta":{"content":"start "}}]}\n\n'));
      await vi.advanceTimersByTimeAsync(100_000);

      controller!.enqueue(encoder.encode('data: {"choices":[{"delta":{"content":"middle "}}]}\n\n'));
      await vi.advanceTimersByTimeAsync(100_000);

      controller!.enqueue(encoder.encode('data: {"choices":[{"delta":{"content":"end"}}]}\n\n'));
      await vi.advanceTimersByTimeAsync(100_000);

      controller!.enqueue(encoder.encode('data: [DONE]\n\n'));
      controller!.close();

      const completed = await recorder.until((e) => e.type === "turn.completed");
      expect(completed).toMatchObject({ ok: true });

      const item = recorder.events.find((e) => e.type === "item.completed");
      expect(item).toMatchObject({ text: "start middle end" });
      // The native log's flush timer is shared across tests and pending only sometimes; it is not the idle timer.
      flushNativeLogs();
      expect(vi.getTimerCount()).toBe(0);

      recorder.stop();
      await inst.dispose();
    } finally {
      vi.useRealTimers();
    }
  });

  it("omits provider routing when none is configured", async () => {
    let sentBody: any = null;
    vi.stubGlobal("fetch", vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
      if (String(input).endsWith("/models")) return new Response(JSON.stringify({ data: [] }));
      sentBody = JSON.parse(String(init?.body));
      return new Response('data: {"choices":[{"delta":{"content":"hi"}}]}\ndata: [DONE]\n');
    }));
    const inst = await OpenAICompatDriver.create({
      instanceId: "test-no-provider", displayName: "No provider", enabled: true,
      config: { url: "https://openrouter.ai/api/v1", apiKeyEnv: "TEST_KEY" }, environment: { TEST_KEY: "secret" },
    });
    const recorder = recordEvents(inst.adapter);
    try {
      await inst.adapter.sendTurn({ threadId: "thread-np", text: "prompt", model: "vendor/model" });
      await recorder.until((event) => event.type === "turn.completed");
      expect(sentBody).not.toBeNull();
      expect(sentBody).not.toHaveProperty("provider");
    } finally {
      recorder.stop();
      await inst.dispose();
    }
  });
});

it("openai-compat preserves an explicit tools-off connection and rejects ambiguous flags", () => {
  expect(OpenAICompatDriver.decodeConfig({})).not.toHaveProperty("tools");
  expect(OpenAICompatDriver.decodeConfig({ tools: false })).toMatchObject({ tools: false });
  expect(OpenAICompatDriver.decodeConfig({ tools: true })).toMatchObject({ tools: true });
  expect(() => OpenAICompatDriver.decodeConfig({ tools: "false" })).toThrow("tools must be a boolean");
});

describe("a server marked local", () => {
  it("keeps the mark only when it is true, and refuses a non-boolean", () => {
    expect(OpenAICompatDriver.decodeConfig({ url: "https://tunnel.example.test/v1", local: true })).toMatchObject({ local: true });
    expect(OpenAICompatDriver.decodeConfig({ url: "https://tunnel.example.test/v1", local: false })).not.toHaveProperty("local");
    expect(OpenAICompatDriver.decodeConfig({ url: "https://tunnel.example.test/v1" })).not.toHaveProperty("local");
    expect(() => OpenAICompatDriver.decodeConfig({ local: "yes" })).toThrow("local must be a boolean");
  });

  it("counts as local at any address, and an unmarked one only on this network", () => {
    expect(servesLocalModel({ url: "https://tunnel.example.test/v1", local: true })).toBe(true);
    expect(servesLocalModel({ url: "https://tunnel.example.test/v1" })).toBe(false);
    expect(servesLocalModel({ url: "http://10.0.0.5:11434/v1" })).toBe(true);
  });
});

describe("how long a turn waits for a reply", () => {
  it("accepts 1 to 60 whole minutes and refuses anything else", () => {
    expect(OpenAICompatDriver.decodeConfig({ url: "http://127.0.0.1:11434/v1", replyWaitMinutes: 20 })).toMatchObject({ replyWaitMinutes: 20 });
    expect(OpenAICompatDriver.decodeConfig({ url: "http://127.0.0.1:11434/v1" })).not.toHaveProperty("replyWaitMinutes");
    for (const bad of [0, 61, 2.5, "10", null]) {
      expect(() => OpenAICompatDriver.decodeConfig({ url: "http://127.0.0.1:11434/v1", replyWaitMinutes: bad })).toThrow(/replyWaitMinutes/);
    }
  });
});

describe("opt-in custom OpenAI-compatible request headers", () => {
  it("preserves standard headers when disabled and injects per-thread/bot/turn/provider headers consistently across threads, compaction, retries, non-streaming, and secret scrubbing", async () => {
    const seenRequests: Array<{ url: string; redirect?: RequestInit["redirect"]; headers: Record<string, string>; body: Record<string, unknown> }> = [];
    let chatAttempt = 0;
    vi.stubGlobal("fetch", vi.fn(async (url: string | URL, init?: RequestInit) => {
      const target = String(url);
      if (target.endsWith("/models")) return Response.json({ data: [] });
      const rawHeaders = Object.fromEntries(new Headers(init?.headers).entries());
      seenRequests.push({ url: target, redirect: init?.redirect, headers: rawHeaders, body: JSON.parse(String(init?.body ?? "{}")) as Record<string, unknown> });
      chatAttempt++;
      if (chatAttempt === 4) {
        return new Response("", { status: 200, headers: { "content-type": "text/event-stream" } });
      }
      if (JSON.parse(String(init?.body ?? "{}")).stream === false) {
        return new Response(JSON.stringify({ choices: [{ message: { content: "non-stream ok" }, finish_reason: "stop" }] }), { headers: { "content-type": "application/json" } });
      }
      return new Response(
        `data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: "ok gw-secret-token-xyz " } }] })}\n\n` +
        `data: ${JSON.stringify({ choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 1, completion_tokens: 1 } })}\n\n`,
        { headers: { "content-type": "text/event-stream" } },
      );
    }));

      // 1. Disabled by default: standard headers only
      const offInst = await OpenAICompatDriver.create({
        instanceId: "gw-off", displayName: "Off", enabled: true,
        config: OpenAICompatDriver.decodeConfig({
          url: "https://gw.example/v1", model: "m1",
          customHeaders: { enabled: false, headers: [{ name: "X-Maus-Thread-ID", value: "{threadId}" }] },
        }),
        environment: { OPENAI_COMPAT_API_KEY: "test-api-key-123456" },
      });
      const recOff = recordEvents(offInst.adapter);
      await offInst.adapter.sendTurn({ threadId: "t-off", botId: "b1", text: "hi" });
      await recOff.until((e) => e.type === "turn.completed");
      expect(seenRequests[0]!.headers["x-maus-thread-id"]).toBeUndefined();
      expect(seenRequests[0]!.redirect).toBeUndefined();
      recOff.stop();
      await offInst.dispose();

      // 2. Enabled: two separate threads -> distinct headers; second turn / compacted turn on thread-1 keeps same threadId; retry preserves turnId
      const onInst = await OpenAICompatDriver.create({
        instanceId: "gw-on", displayName: "On", enabled: true,
        config: OpenAICompatDriver.decodeConfig({
          url: "https://gw.example/v1", model: "m1",
          customHeaders: {
            enabled: true,
            headers: [
              { name: "X-Maus-Thread-ID", value: "{threadId}", required: true },
              { name: "X-Maus-Bot-ID", value: "{botId}", required: false },
              { name: "X-Maus-Turn-ID", value: "{turnId}", required: false },
              { name: "X-Provider-ID", value: "{providerInstanceId}", required: true },
              { name: "X-Gateway-Token", value: "gw-secret-token-xyz", secret: true },
            ],
          },
        }),
        environment: { OPENAI_COMPAT_API_KEY: "test-api-key-123456" },
      });
      const recOn = recordEvents(onInst.adapter);
      const completedCount = () => recOn.events.filter((e) => e.type === "turn.completed").length;
      await onInst.adapter.sendTurn({ threadId: "thread-alpha", botId: "bot-one", text: "first" });
      await recOn.until(() => completedCount() >= 1);
      await onInst.adapter.sendTurn({ threadId: "thread-beta", botId: "bot-two", text: "second" });
      await recOn.until(() => completedCount() >= 2);
      await onInst.adapter.sendTurn({ threadId: "thread-alpha", botId: "bot-one", text: "compacted", transcript: [{ role: "user", text: "summary" }] });
      await recOn.until(() => completedCount() >= 3);

      const chatReqs = seenRequests.slice(1);
      expect(chatReqs[0]!.headers["x-maus-thread-id"]).toBe("thread-alpha");
      expect(chatReqs[0]!.headers["x-maus-bot-id"]).toBe("bot-one");
      expect(chatReqs[0]!.headers["x-provider-id"]).toBe("gw-on");
      expect(chatReqs[0]!.headers["x-gateway-token"]).toBe("gw-secret-token-xyz");
      expect(chatReqs[0]!.redirect).toBe("error");
      expect(chatReqs[1]!.headers["x-maus-thread-id"]).toBe("thread-beta");
      // chatAttempt === 4 was truncated, so retried once on attempt 5 for thread-alpha: same turnId!
      expect(chatReqs[2]!.headers["x-maus-thread-id"]).toBe("thread-alpha");
      expect(chatReqs[3]!.headers["x-maus-thread-id"]).toBe("thread-alpha");
      expect(chatReqs[2]!.headers["x-maus-turn-id"]).toBe(chatReqs[3]!.headers["x-maus-turn-id"]);
      // Echoed sensitive custom header value must be scrubbed from streamed deltas.
      const completed = recOn.events.filter((e) => e.type === "item.completed");
      expect(completed.some((e) => JSON.stringify(e).includes("gw-secret-token-xyz"))).toBe(false);
      expect(completed.some((e) => JSON.stringify(e).includes("[redacted]"))).toBe(true);

      // 3. Non-streaming generateText without threadId fails cleanly when {threadId} is required, and succeeds when compaction passes threadId
      const countBeforeFail = seenRequests.length;
      await expect(onInst.generateText!("summarize")).rejects.toThrow(/threadId/);
      expect(seenRequests).toHaveLength(countBeforeFail);
      await expect(onInst.generateText!("summarize", { threadId: "thread-compact" })).resolves.toBe("non-stream ok");
      expect(seenRequests.at(-1)!.headers["x-maus-thread-id"]).toBe("thread-compact");

      // Non-streaming generateText with optional {threadId} omits it cleanly and still sends static/{providerInstanceId} headers
      const optInst = await OpenAICompatDriver.create({
        instanceId: "gw-opt", displayName: "Opt", enabled: true,
        config: OpenAICompatDriver.decodeConfig({
          url: "https://gw.example/v1", model: "m1",
          customHeaders: {
            enabled: true,
            headers: [
              { name: "X-Maus-Thread-ID", value: "{threadId}", required: false },
              { name: "X-Gateway-Client", value: "maus", required: true },
            ],
          },
        }),
        environment: { OPENAI_COMPAT_API_KEY: "test-api-key-123456" },
      });
      await expect(optInst.generateText!("summarize")).resolves.toBe("non-stream ok");
      const nonStreamReq = seenRequests.at(-1)!;
      expect(nonStreamReq.headers["x-gateway-client"]).toBe("maus");
      expect(nonStreamReq.headers["x-maus-thread-id"]).toBeUndefined();
      expect(nonStreamReq.redirect).toBe("error");
      await optInst.dispose();
      recOn.stop();
      await onInst.dispose();
  });

  it("masks secret custom header values in generateText replies, reasoning, and propagated errors", async () => {
    let mode: "text" | "reasoning" | "error" | "clean" = "text";
    vi.stubGlobal("fetch", vi.fn(async (url: string | URL) => {
      if (String(url).endsWith("/models")) return Response.json({ data: [] });
      if (mode === "error") return new Response(JSON.stringify({ error: { message: "bad token gw-secret-helper-999" } }), { status: 500, headers: { "content-type": "application/json" } });
      if (mode === "reasoning") return Response.json({ choices: [{ message: { content: null, reasoning_content: "think gw-secret-helper-999 done" }, finish_reason: "stop" }] });
      if (mode === "clean") return Response.json({ choices: [{ message: { content: "plain clean output" }, finish_reason: "stop" }] });
      return Response.json({ choices: [{ message: { content: "echo gw-secret-helper-999 ok" }, finish_reason: "stop" }] });
    }));
    const inst = await OpenAICompatDriver.create({
      instanceId: "gw-helper", displayName: "Helper", enabled: true,
      config: OpenAICompatDriver.decodeConfig({
        url: "https://gw.example/v1", model: "m1",
        customHeaders: {
          enabled: true,
          headers: [{ name: "X-Gateway-Token", value: "gw-secret-helper-999", secret: true }],
        },
      }),
      environment: { OPENAI_COMPAT_API_KEY: "test-api-key-123456" },
    });
    mode = "text";
    await expect(inst.generateText!("say hi")).resolves.toBe("echo [redacted] ok");
    mode = "reasoning";
    await expect(inst.generateText!("say hi")).resolves.toBe("think [redacted] done");
    mode = "clean";
    await expect(inst.generateText!("say hi")).resolves.toBe("plain clean output");
    mode = "error";
    await expect(inst.generateText!("say hi")).rejects.toThrow(/bad token \[redacted\]/);
    await inst.dispose();
  });

  it("masks secret custom header values in ask_user question events without altering answer handling", async () => {
    const askBody = 'data: {"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_ask_1","type":"function","function":{"name":"ask_user","arguments":'
      + JSON.stringify(JSON.stringify({
        questions: [{
          header: "Token gw-secret-question-999",
          question: "Confirm token gw-secret-question-999?",
          options: [
            { label: "Use gw-secret-question-999", description: "Header gw-secret-question-999" },
            { label: "Skip", description: "Continue without token" },
          ],
        }],
      }))
      + '}}]}}]}\n\n'
      + 'data: {"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}\n\n';
    const finalBody = 'data: {"choices":[{"index":0,"delta":{"content":"done"}}]}\n\n'
      + 'data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}\n\n';
    let call = 0;
    vi.stubGlobal("fetch", vi.fn(async (url: string | URL) => {
      if (String(url).endsWith("/models")) return Response.json({ data: [] });
      call++;
      return new Response(call === 1 ? askBody : finalBody, { status: 200, headers: { "content-type": "text/event-stream" } });
    }));
    const inst = await OpenAICompatDriver.create({
      instanceId: "gw-ask", displayName: "Ask", enabled: true,
      config: OpenAICompatDriver.decodeConfig({
        url: "https://gw.example/v1", model: "m1",
        customHeaders: {
          enabled: true,
          headers: [{ name: "X-Gateway-Token", value: "gw-secret-question-999", secret: true }],
        },
      }),
      environment: { OPENAI_COMPAT_API_KEY: "test-api-key-123456" },
    });
    const rec = recordEvents(inst.adapter);
    await inst.adapter.sendTurn({ threadId: "t-ask", botId: "b1", text: "ask me" });
    const asked = await rec.until((e) => e.type === "request.opened" && e.requestType === "question");
    expect(asked.type === "request.opened" && asked.requestType === "question").toBe(true);
    if (asked.type === "request.opened" && asked.requestType === "question") {
      expect(JSON.stringify(asked)).not.toContain("gw-secret-question-999");
      expect(asked.summary).toContain("[redacted]");
      expect(asked.questions?.[0]?.question).toContain("[redacted]");
      expect(asked.questions?.[0]?.header).toContain("[redacted]");
      expect(asked.questions?.[0]?.options?.[0]?.label).toContain("[redacted]");
      expect(asked.questions?.[0]?.options?.[0]?.description).toContain("[redacted]");
      expect(asked.choices?.[0]).toContain("[redacted]");
      expect(asked.requestId).toBeTruthy();
      const outcome = await inst.adapter.respondToRequest("t-ask", asked.requestId!, { behavior: "answer", message: "Skip" });
      expect(outcome).toBe("answered");
    }
    await rec.until((e) => e.type === "turn.completed");
    rec.stop();
    await inst.dispose();
  }, 20_000);
});