Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

5.8 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";

const stream = vi.fn();
const dispatch = vi.fn();
const device = vi.hoisted(() => vi.fn());
vi.mock("@/lib/auth/device-store", () => ({ getApprovedDevice: device }));
vi.mock("./session-store", () => ({
  getAgentSession: vi.fn(async () => ({ id: "session-a", cwd: "/srv/project", title: "parent", history: [{ role: "user", text: "TOP SECRET PARENT TRANSCRIPT" }], memorySnapshot: { user: "TOP SECRET MEMORY" } })),
}));
vi.mock("@/lib/ai/selected-model-stream", () => ({
  prepareSelectedModel: vi.fn(async () => ({ kind: "openai", provider: "x", model: "x", resolved: {}, customProvider: false })),
  streamPreparedSelectedModel: stream,
}));
const capabilities = {
  list: vi.fn(() => [
    { name: "fs_read", description: "read", scope: "read" as const, inputSchema: { type: "object" as const, properties: {} } },
    { name: "fs_write", description: "write", scope: "write" as const, inputSchema: { type: "object" as const, properties: {} } },
    { name: "local_agent_message_send", description: "local", scope: "write" as const, inputSchema: { type: "object" as const, properties: {} } },
    { name: "agent_subagent_run", description: "nested", scope: "exec" as const, inputSchema: { type: "object" as const, properties: {} } },
    { name: "project_agent_run", description: "nested project wrapper", scope: "exec" as const, inputSchema: { type: "object" as const, properties: {} } },
  ]),
  invoke: dispatch,
};

const { runSessionSubagent } = await import("./subagent-runner");

describe("same-session subagent runner", () => {
  beforeEach(() => { stream.mockReset(); dispatch.mockReset(); });
  it("cancels model streaming when the initiating task signal is aborted", async () => {
    const controller = new AbortController();
    stream.mockImplementationOnce(async ({ signal }) => {
      expect(signal.aborted).toBe(false);
      controller.abort(new Error("parent task revoked"));
      expect(signal.aborted).toBe(true);
      throw signal.reason;
    });
    await expect(runSessionSubagent({ principal: "mcp-client:x", parentSessionId: "session-a", objective: "inspect", capabilities,
      authority: { principal: "mcp-client:x", scope: "read", signal: controller.signal },
    })).rejects.toThrow(/revoked/);
    expect(dispatch).not.toHaveBeenCalled();
  });
  it.each([null, { role: "operator" }])("stops after original Owner authority changes during a model round: %j", async changed => {
    device.mockResolvedValue({ role: "owner" });
    stream.mockImplementationOnce(async ({ emit }) => {
      device.mockResolvedValue(changed);
      emit("tool_use", { id: "write", name: "fs_write", input: {} });
    });
    await expect(runSessionSubagent({ principal: "cli:device", parentSessionId: "session-a", objective: "inspect", maxScope: "exec", capabilities, authority: { principal: "web:device", scope: "exec" } })).rejects.toThrow(/revoked|demoted/);
    expect(stream).toHaveBeenCalledTimes(1); expect(dispatch).not.toHaveBeenCalled();
    expect(device).toHaveBeenCalledWith("device");
  });

  it("uses isolated context, read scope by default, and returns only final result", async () => {
    let seenTools: string[] = [], firstPayload = "";
    stream.mockImplementationOnce(async (opts) => {
      seenTools = opts.tools.map((tool: { name: string }) => tool.name);
      firstPayload = JSON.stringify({ system: opts.system, messages: opts.messages });
      opts.emit("tool_use", { id: "tool-1", name: "fs_read", input: {} });
    }).mockImplementationOnce(async ({ emit }) => emit("delta", "review complete"));
    dispatch.mockResolvedValue({ content: [{ type: "text", text: "file data" }] });
    const result = await runSessionSubagent({ principal: "mcp-client:x", parentSessionId: "session-a", objective: "review auth", capabilities, authority: { principal: "mcp-client:x", scope: "read" } });
    expect(seenTools).toEqual(["fs_read"]);
    expect(result).toMatchObject({ status: "completed", text: "review complete", rounds: 2, maxScope: "read" });
    expect(result.toolCalls).toEqual([{ name: "fs_read", ok: true }]);
    expect(firstPayload).not.toContain("TOP SECRET");
    expect(dispatch).toHaveBeenCalledWith(expect.objectContaining({
      name: "fs_read", scope: "read", actor: expect.stringContaining("#subagent_"), principal: "mcp-client:x",
    }));
  });

  it("never exposes local-agent or recursive subagent tools even with exec scope", async () => {
    let names: string[] = [];
    stream.mockImplementationOnce(async ({ tools, emit }) => { names = tools.map((tool: { name: string }) => tool.name); emit("delta", "done"); });
    const result = await runSessionSubagent({ principal: "mcp-client:x", parentSessionId: "session-a", objective: "inspect", maxScope: "exec", capabilities, authority: { principal: "mcp-client:x", scope: "exec" } });
    expect(names).toContain("fs_read");
    expect(names).toContain("fs_write");
    expect(names).not.toContain("local_agent_message_send");
    expect(names).not.toContain("agent_subagent_run");
    expect(names).not.toContain("project_agent_run");
    expect(result.status).toBe("completed");
  });
  it("preserves the existing subagent behavior beyond the profile-only tool budget", async () => {
    stream.mockImplementationOnce(async ({ emit }) => {
      for (let index = 0; index < 17; index++) emit("tool_use", { id: `read-${index}`, name: "fs_read", input: {} });
    }).mockImplementationOnce(async ({ emit }) => emit("delta", "done"));
    dispatch.mockResolvedValue({ content: [{ type: "text", text: "evidence" }] });
    const result = await runSessionSubagent({ principal: "mcp-client:x", parentSessionId: "session-a", objective: "inspect", capabilities, authority: { principal: "mcp-client:x", scope: "read" } });
    expect(dispatch).toHaveBeenCalledTimes(17); expect(result.toolCalls).toHaveLength(17);
    expect(result).toMatchObject({ status: "completed", rounds: 2, text: "done" });
  });
});