Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
19.3 KB
Raw Blame
import { browserScript } from "@omnirush/testkit";
import { expect } from "vitest";
import {
  control,
  createAndSelectWorkspace,
  engineSessionProbe,
  evalIn,
  selectModel,
  waitFor,
  writeComposerText,
} from "@omnirush/behaviors";
import { resolveEvalEngine } from "@omnirush/env";
import { screenshot } from "@omnirush/test-evidence";
import {
  app,
  eventually,
  localMysqlIsRunning,
  localRedisIsRunning,
  mcpMock,
  needs,
  server,
  test,
} from "@omnirush/testkit";
import type { App } from "@omnirush/testkit";

const providerId = "live-tool-switch-mock";
const modelId = "live-tool-switch-model";
const modelName = "Live tool switch model";
const evalEngine = resolveEvalEngine();
const shellToolName = evalEngine === "v2" ? "shell" : "bash";
const e2eTestsEnabled = process.env.OMNIRUSH_EVAL_E2E_TESTS === "1";
const daytonaEnabled = process.env.OMNIRUSH_EVAL_DAYTONA === "1";
const configuredDen = Boolean(process.env.OMNIRUSH_EVAL_DEN_API_URL?.trim());
const localServicesRequired = !daytonaEnabled && !configuredDen;
const mysqlOpen = await localMysqlIsRunning();
const redisOpen = await localRedisIsRunning();
const runnable = e2eTestsEnabled && (!localServicesRequired || (mysqlOpen && redisOpen));
const skipSuffix = !e2eTestsEnabled
  ? " skipped — needs: set OMNIRUSH_EVAL_E2E_TESTS=1"
  : localServicesRequired && !mysqlOpen
    ? " skipped — needs MySQL on 127.0.0.1:3306"
    : localServicesRequired && !redisOpen
      ? " skipped — needs Redis on 127.0.0.1:6379"
      : "";

interface ToolFact {
  tool: string;
  callId: string;
  status: string;
  command: string;
  description: string;
}

interface SessionFacts {
  sessionId: string;
  text: string;
  tools: ToolFact[];
}

interface VisibleToolFact {
  currentSessionId: string;
  found: boolean;
  visible: boolean;
  text: string;
}

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

function parseVisibleToolFact(value: unknown): VisibleToolFact {
  if (!isRecord(value)) throw new Error(`Invalid visible tool fact: ${JSON.stringify(value)}`);
  return {
    currentSessionId: typeof value.currentSessionId === "string" ? value.currentSessionId : "",
    found: value.found === true,
    visible: value.visible === true,
    text: typeof value.text === "string" ? value.text : "",
  };
}

async function configureWorkspaces(appSurface: App, workspaceIds: string[], baseUrl: string): Promise<void> {
  const result = await evalIn(appSurface, browserScript(async (workspaceIds, providerId, modelName, value, modelId, inputModelName, inputProviderId, inputModelId, inputValue) => {
    const info = await window.__OMNIRUSH_ELECTRON__?.invokeDesktop?.("omnirushServerInfo");
    if (!info?.running || !info.baseUrl) return "local_server_unavailable";
    const root = String(info.baseUrl).replace(/\/+$/, "");
    const headers = {
      Authorization: "Bearer " + String(info.ownerToken ?? info.clientToken ?? ""),
      "Content-Type": "application/json",
    };
    for (const workspaceId of workspaceIds) {
      const configured = await fetch(root + "/workspace/" + encodeURIComponent(workspaceId) + "/config", {
        method: "PATCH",
        headers,
        body: JSON.stringify({
          opencode: {
            permission: { bash: "allow" },
            provider: {
              [providerId]: {
                npm: "@ai-sdk/openai-compatible",
                name: modelName,
                options: { baseURL: value, apiKey: "sk-live-tool-switch" },
                models: {
                  [modelId]: { name: inputModelName, tool_call: true },
                },
              },
            },
          },
        }),
        signal: AbortSignal.timeout(30000),
      });
      if (!configured.ok) return "config:" + configured.status + ":" + (await configured.text()).slice(0, 300);
      const reloaded = await fetch(root + "/workspace/" + encodeURIComponent(workspaceId) + "/engine/reload", {
        method: "POST",
        headers,
        signal: AbortSignal.timeout(60000),
      });
      if (!reloaded.ok) return "reload:" + reloaded.status + ":" + (await reloaded.text()).slice(0, 300);
    }
    const raw = localStorage.getItem("omnirush.preferences");
    let preferences: Record<string, unknown> = {};
    try { preferences = raw ? JSON.parse(raw) : {}; } catch { preferences = {}; }
    if (!preferences || typeof preferences !== "object" || Array.isArray(preferences)) preferences = {};
    localStorage.setItem("omnirush.preferences", JSON.stringify({
      ...preferences,
      defaultModel: { providerID: inputProviderId, modelID: inputModelId },
      modelVariant: null,
      providerStepCompleted: true,
    }));
    localStorage.setItem("omnirush.defaultModel", inputValue);
    return "ok";
  }, [workspaceIds, providerId, modelName, `${baseUrl}/v1`, modelId, modelName, providerId, modelId, `${providerId}/${modelId}`]), { awaitPromise: true, timeoutMs: 120_000 });
  expect(result).toBe("ok");

  await evalIn(appSurface, () => { location.reload(); return true; });
  await waitFor(appSurface, () => (Boolean(window.__omnirushControl)), {
    timeoutMs: 60_000,
    label: "desktop restored after mock provider configuration",
  });
}

async function createSession(appSurface: App): Promise<string> {
  const deadline = Date.now() + 60_000;
  let lastError: unknown = null;
  while (Date.now() < deadline) {
    try {
      const created = await control(appSurface, "session.create_task", undefined, { timeoutMs: 30_000 });
      if (typeof created === "string" && created.startsWith("ses_")) return created;
      lastError = new Error(`session.create_task returned ${JSON.stringify(created)}`);
    } catch (error) {
      lastError = error;
    }
    await new Promise((resolve) => setTimeout(resolve, 500));
  }
  throw new Error(`session.create_task did not return a session id: ${lastError instanceof Error ? lastError.message : String(lastError)}`);
}

async function clickSessionRow(appSurface: App, workspaceId: string, sessionId: string): Promise<void> {
  const clicked = await evalIn(appSurface, browserScript((value, inputValue) => {
    const row = document.querySelector<HTMLElement>(value);
    const control = row?.querySelector<HTMLElement>(inputValue);
    if (!(row instanceof HTMLElement) || !(control instanceof HTMLElement)) return false;
    row.scrollIntoView({ block: "center" });
    control.click();
    return true;
  }, [`[data-sidebar-session-id="${sessionId}"][data-sidebar-session-workspace-id="${workspaceId}"]`, `[data-session-tab-id="${sessionId}"]`]));
  expect(clicked).toBe(true);
  await waitFor(appSurface, browserScript((sessionId, workspaceId) => {
    const surface = document.querySelector<HTMLElement>("[data-session-surface-id]");
    return surface?.getAttribute("data-session-surface-id") === sessionId
      && (localStorage.getItem("omnirush.react.activeWorkspace") ?? "") === workspaceId;
  }, [sessionId, workspaceId]), { timeoutMs: 60_000, label: `workspace ${workspaceId} session ${sessionId} visible after sidebar click` });
}

async function readSessionFacts(appSurface: App, workspaceId: string, sessionId: string): Promise<SessionFacts> {
  const probe = engineSessionProbe({
    engine: evalEngine,
    surface: appSurface,
    workspaceId,
  });
  const snapshot = await probe.snapshot(sessionId);
  if (!snapshot.ok) return { sessionId: "", text: "", tools: [] };
  const parts = snapshot.data.messages.flatMap((message) => message.parts);
  return {
    sessionId: snapshot.data.session?.id ?? "",
    text: parts.flatMap((part) => part.text ? [part.text] : []).join("\n"),
    tools: parts.flatMap((part) => {
      if (!part.tool) return [];
      return [{
        tool: part.tool,
        callId: part.callId,
        status: part.status,
        command: typeof part.input.command === "string" ? part.input.command : "",
        description: typeof part.input.description === "string" ? part.input.description : "",
      }];
    }),
  };
}

async function approvePendingPermission(appSurface: App, workspaceId: string, sessionId: string): Promise<number> {
  const statuses = await engineSessionProbe({
    engine: evalEngine,
    surface: appSurface,
    workspaceId,
  }).approvePendingPermissions(sessionId);
  if (statuses.some((status) => status < 200 || status >= 300)) {
    throw new Error(`Permission approval failed: ${JSON.stringify(statuses)}`);
  }
  return statuses.length;
}

async function expectLeftSessionIndicator(appSurface: App, sessionId: string, kind: "loading" | "attention"): Promise<void> {
  const fact = await eventually(() => evalIn(appSurface, browserScript((sessionId, value) => {
    const row = document.querySelector<HTMLElement>('[data-sidebar-session-id="' + CSS.escape(sessionId) + '"]');
    const title = row?.querySelector<HTMLElement>("[data-session-title-slot]");
    const indicators = row?.querySelectorAll<HTMLElement>("[data-session-loading-indicator], [data-session-attention-indicator]");
    const indicator = row?.querySelector<HTMLElement>(value);
    if (!(title instanceof HTMLElement) || !(indicator instanceof HTMLElement)) return false;
    const box = indicator.getBoundingClientRect();
    const style = getComputedStyle(indicator);
    return indicators?.length === 1 && box.width > 0 && box.height > 0
      && box.right <= title.getBoundingClientRect().left
      && style.visibility === "visible" && style.opacity === "1";
  }, [sessionId, `[data-session-${kind}-indicator]`])), {
    within: 15_000,
    intervalMs: 250,
    label: `exactly one visible ${kind} indicator before the session title`,
    until: (value) => value === true,
  });
  expect(fact).toBe(true);
}

async function readVisibleTool(
  appSurface: App,
  sessionId: string,
  toolCallId: string,
): Promise<VisibleToolFact> {
  const value = await evalIn(appSurface, browserScript((value, toolCallId) => {
    const surface = document.querySelector<HTMLElement>(value);
    const currentSessionId = document.querySelector<HTMLElement>("[data-session-surface-id]")?.getAttribute("data-session-surface-id") ?? "";
    if (!(surface instanceof HTMLElement)) return { currentSessionId, found: false, visible: false, text: "" };
    const row = surface.querySelector<HTMLElement>('[data-tool-aggregate="' + CSS.escape(toolCallId) + '"]');
    if (!(row instanceof HTMLElement)) return { currentSessionId, found: false, visible: false, text: "" };
    const style = getComputedStyle(row);
    const rect = row.getBoundingClientRect();
    const surfaceRect = surface.getBoundingClientRect();
    const visible = row.isConnected
      && rect.width > 0
      && rect.height > 0
      && style.display !== "none"
      && style.visibility !== "hidden"
      && style.opacity !== "0"
      && rect.bottom > Math.max(0, surfaceRect.top)
      && rect.top < Math.min(window.innerHeight, surfaceRect.bottom)
      && rect.right > Math.max(0, surfaceRect.left)
      && rect.left < Math.min(window.innerWidth, surfaceRect.right);
    return { currentSessionId, found: true, visible, text: row.innerText ?? "" };
  }, [`[data-session-surface-id="${sessionId}"]`, toolCallId]));
  return parseVisibleToolFact(value);
}

test.skipIf(!runnable)(
  `a tool started while away is visible after returning to its chat${skipSuffix}`,
  { timeout: 12 * 60_000 },
  async ({ evidence, place }) => {
    needs({ optIn: ["OMNIRUSH_EVAL_E2E_TESTS"] });
    const runId = `${Date.now().toString(36)}-${process.pid}`;
    const promptMarker = `LIVE-TOOL-SWITCH-${runId}`;
    const firstMarker = `FIRST-${promptMarker}`;
    const firstToolDescription = `First tool in chat A — ${promptMarker}`;
    const toolDescription = `Waiting in chat A — ${promptMarker}`;
    const completionMarker = `DONE-${promptMarker}`;
    const firstCommand = `sleep 15 && printf '%s\\n' '${firstMarker}'`;
    const command = `sleep 45 && printf '%s\\n' '${completionMarker}'`;
    const matchesDescription = (tool: ToolFact, description: string) =>
      evalEngine === "v2" || tool.description === description;

    await using den = await server({
      place,
      mocks: {
        agent: mcpMock({
          agentWorkloads: [{
            promptMarker,
            finalReply: completionMarker,
            steps: [
              {
                tool: shellToolName,
                arguments: {
                  command: firstCommand,
                  timeout: 30_000,
                  ...(evalEngine === "v1" ? { description: firstToolDescription } : {}),
                },
              },
              {
                tool: shellToolName,
                arguments: {
                  command,
                  timeout: 90_000,
                  ...(evalEngine === "v1" ? { description: toolDescription } : {}),
                },
              },
            ],
          }],
        }),
      },
      org: {
        name: "Live Tool Switch",
        admin: { name: "Switch Admin" },
        members: { member: { name: "Switch Member" } },
      },
    });
    await using desktopApp = await app({ den, as: "member", place });

    const workspaceB = await createAndSelectWorkspace(desktopApp, {
      path: `/tmp/omnirush-live-tool-switch-${runId}-b`,
    });
    const chatB = await createSession(desktopApp);
    await control(desktopApp, "session.rename", { sessionId: chatB, title: "Chat B" });

    const workspaceA = await createAndSelectWorkspace(desktopApp, {
      path: `/tmp/omnirush-live-tool-switch-${runId}-a`,
    });
    await configureWorkspaces(desktopApp, [workspaceA.workspaceId, workspaceB.workspaceId], den.mocks.agent.url);
    const chatA = await createSession(desktopApp);
    await control(desktopApp, "session.rename", { sessionId: chatA, title: "Chat A" });
    expect(chatA).not.toBe(chatB);

    await clickSessionRow(desktopApp, workspaceA.workspaceId, chatA);
    const selected = await selectModel(desktopApp, modelId);
    expect(selected.id).toBe(modelId);
    await writeComposerText(desktopApp, `Run the deterministic tool identified by ${promptMarker}.`);
    await control(desktopApp, "composer.send", undefined, { timeoutMs: 120_000 });

    const running = await eventually(async () => {
      const approved = await approvePendingPermission(desktopApp, workspaceA.workspaceId, chatA);
      const facts = await readSessionFacts(desktopApp, workspaceA.workspaceId, chatA);
      return { approved, facts };
    }, {
      within: 90_000,
      intervalMs: 500,
      label: `chat A first ${shellToolName} tool running`,
      until: ({ approved, facts }) => approved === 0
        && facts.sessionId === chatA
        && facts.tools.some((tool) =>
        tool.tool === shellToolName
          && tool.status === "running"
          && tool.command === firstCommand
          && matchesDescription(tool, firstToolDescription)),
    });
    expect(running.facts.sessionId).toBe(chatA);
    const runningTool = running.facts.tools.find((tool) => tool.command === firstCommand);
    if (!runningTool?.callId) throw new Error(`The running ${shellToolName} tool had no call ID: ${JSON.stringify(running.facts)}`);

    const visibleBeforeSwitch = await eventually(
      () => readVisibleTool(desktopApp, chatA, runningTool.callId),
      {
        within: 30_000,
        intervalMs: 250,
        label: "running tool visibly rendered before switching",
        until: (fact) => fact.currentSessionId === chatA && fact.found && fact.visible,
      },
    );
    expect(visibleBeforeSwitch.visible).toBe(true);
    await expectLeftSessionIndicator(desktopApp, chatA, "loading");

    await clickSessionRow(desktopApp, workspaceB.workspaceId, chatB);
    const absentFromChatB = await readVisibleTool(desktopApp, chatB, runningTool.callId);
    expect(absentFromChatB.currentSessionId).toBe(chatB);
    expect(absentFromChatB.found).toBe(false);

    const laterRunning = await eventually(async () => {
      await approvePendingPermission(desktopApp, workspaceA.workspaceId, chatA);
      return readSessionFacts(desktopApp, workspaceA.workspaceId, chatA);
    }, {
      within: 30_000,
      intervalMs: 500,
      label: "second chat A tool started while workspace B is visible",
      until: (facts) => facts.tools.some((tool) => tool.tool === shellToolName
        && tool.status === "completed" && tool.callId === runningTool.callId)
        && facts.tools.some((tool) => tool.tool === shellToolName
          && tool.status === "running" && tool.command === command && matchesDescription(tool, toolDescription)),
    });
    const laterTool = laterRunning.tools.find((tool) => tool.command === command);
    if (!laterTool?.callId) throw new Error(`The later ${shellToolName} tool had no call ID: ${JSON.stringify(laterRunning)}`);
    await clickSessionRow(desktopApp, workspaceA.workspaceId, chatA);
    const stillRunning = await readSessionFacts(desktopApp, workspaceA.workspaceId, chatA);
    expect(stillRunning.tools.some((tool) =>
      tool.tool === shellToolName
        && tool.status === "running"
        && tool.callId === laterTool.callId
        && matchesDescription(tool, toolDescription)), JSON.stringify(stillRunning)).toBe(true);

    const visibleAfterReturn = await eventually(
      () => readVisibleTool(desktopApp, chatA, laterTool.callId),
      {
        within: 30_000,
        intervalMs: 250,
        label: "tool started while away visibly rendered after returning",
        until: (fact) => fact.currentSessionId === chatA && fact.found && fact.visible,
      },
    );
    expect(visibleAfterReturn.currentSessionId).toBe(chatA);
    expect(visibleAfterReturn.found, JSON.stringify(visibleAfterReturn)).toBe(true);
    expect(visibleAfterReturn.visible, JSON.stringify(visibleAfterReturn)).toBe(true);
    expect(visibleAfterReturn.text).toContain(completionMarker);
    evidence.recordAssertionEvidence(
      "A tool that started while away is visible when the user returns to its chat",
      `The first tool completed and tool ${laterTool.callId} started while workspace B chat ${chatB} was visible; after returning to workspace A chat ${chatA}, scoped CDP found its visible row with text ${JSON.stringify(visibleAfterReturn.text)}.`,
      true,
    );
    await screenshot(desktopApp);

    await clickSessionRow(desktopApp, workspaceB.workspaceId, chatB);
    const completed = await eventually(
      () => readSessionFacts(desktopApp, workspaceA.workspaceId, chatA),
      {
        within: 90_000,
        intervalMs: 500,
        label: `chat A unique ${shellToolName} tool completed`,
        until: (facts) => facts.text.includes(completionMarker)
          && facts.tools.some((tool) => tool.tool === shellToolName
            && tool.status === "completed" && tool.command === command),
      },
    );
    expect(completed.text).toContain(completionMarker);
    await expectLeftSessionIndicator(desktopApp, chatA, "attention");
    evidence.recordAssertionEvidence(
      "Session activity and unread completion use the same left indicator slot",
      "During the run and after completion in another chat, exactly one visible indicator was positioned before chat A's title; no second status indicator remained on the right.",
      true,
    );
    await screenshot(desktopApp);
    await clickSessionRow(desktopApp, workspaceA.workspaceId, chatA);
    const visibleAfterCompletion = await eventually(
      () => readVisibleTool(desktopApp, chatA, laterTool.callId),
      {
        within: 30_000,
        intervalMs: 250,
        label: "completed tool remains visibly rendered",
        until: (fact) => fact.currentSessionId === chatA && fact.found && fact.visible,
      },
    );
    expect(visibleAfterCompletion.visible).toBe(true);
  },
);