Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

5.9 KB
Raw Blame
import { test } from "node:test";
import assert from "node:assert/strict";
import { createToolContext, type ToolContextDeps, type SkillResult } from "../src/tools/primitives.ts";
import type { Sandbox, SandboxHandle } from "../src/sandbox/sandbox.ts";
import { scopeId } from "../src/types.ts";

function ctx(useSkill: (name: string, path: string, sandboxId?: string) => Promise<SkillResult>) {
  const calls: string[] = [];
  const sandbox = {
    readFile: async (_h: SandboxHandle, _p: string) => null,
    run: async (_h: SandboxHandle, command: string) => {
      calls.push(command);
      return { stdout: "ran", stderr: "", code: 0, timedOut: false };
    },
  } as unknown as Sandbox;
  const backgroundBroker = {
    start: async (_h: SandboxHandle, command: string) => {
      calls.push(command);
      return { processId: "p1", output: "ran", cursor: 0, status: { state: "exited", exitCode: 0 }, reattached: false };
    },
  };
  const tc = createToolContext({
    sandbox,
    provision: async () => ({ id: "h", rootDir: "/workspace" }) as SandboxHandle,
    useSkill,
    backgroundBroker: backgroundBroker as never,
    layers: [{ scopeId: scopeId("personal", "U1"), mountPath: "", mode: "rw" }],
    commandPolicy: () => ({ mode: "denylist", rules: [] }),
    authorizeCommand: () => false,
    grantedHandles: [],
    workspace: {} as never,
    deploy: {} as never,
    acl: {} as never,
    createdBy: "U1",
  });
  return { tc, calls };
}

test("skill loads SKILL.md by default and passes the requested path through", async () => {
  const seen: Array<[string, string, string | undefined]> = [];
  const { tc } = ctx(async (name, path, sandboxId) => {
    seen.push([name, path, sandboxId]);
    return { content: `${name}:${path}`, sourceScopeId: scopeId("personal", "U1") };
  });
  assert.equal((await tc.skill("send")).content, "send:SKILL.md");
  assert.equal((await tc.skill("send", { path: "references/notes.md" })).content, "send:references/notes.md");
  assert.deepEqual(seen, [
    ["send", "SKILL.md", undefined],
    ["send", "references/notes.md", undefined],
  ]);
});

test("skill reports the synced directory when the skill ships files", async () => {
  const { tc } = ctx(async () => ({
    content: "BODY",
    sourceScopeId: scopeId("personal", "U1"),
    dir: "skills/google-workspace",
    packDir: "skills/.packs/workspace",
  }));
  const r = await tc.skill("google-workspace");
  assert.equal(r.dir, "skills/google-workspace");
  assert.equal(r.packDir, "skills/.packs/workspace");
});

test("execute, read, write and background never trigger skill synchronization", async () => {
  let loads = 0;
  const { tc, calls } = ctx(async () => {
    loads++;
    return { content: "BODY", sourceScopeId: scopeId("personal", "U1") };
  });
  await tc.execute("cd skills/google-workspace && python scripts/gmail.py");
  await tc.read("skills/google-workspace/SKILL.md");
  await tc.backgroundStart("sh skills/.packs/selected/job.sh", { purpose: "Run background tests" });
  assert.equal(loads, 0);
  assert.equal(calls.length, 2);
});

test("skill without a materializer reports no file", async () => {
  const { tc } = createToolContextWithout();
  assert.deepEqual(await tc.skill("send"), { content: null, sourceScopeId: null });
});

function createToolContextWithout(extra: Partial<ToolContextDeps> = {}) {
  const tc = createToolContext({
    sandbox: {} as unknown as Sandbox,
    provision: async () => ({ id: "h", rootDir: "/workspace" }) as SandboxHandle,
    layers: [{ scopeId: scopeId("personal", "U1"), mountPath: "", mode: "rw" }],
    commandPolicy: () => ({ mode: "denylist", rules: [] }),
    authorizeCommand: () => false,
    grantedHandles: [],
    workspace: {} as never,
    deploy: {} as never,
    acl: {} as never,
    createdBy: "U1",
    ...extra,
  });
  return { tc };
}

test("skill discovery pages the current visible catalog with ids, without provisioning or exposing other metadata", async () => {
  const skills = Array.from({ length: 3 }, (_, i) => ({
    skill: {
      manifest: {
        name: `skill-${i}`,
        description: i === 2 ? "Telescope planning" : "General task",
        body: "Instructions",
        requiredCapabilities: [],
      },
      id: `id-${i}`,
      signature: "",
      status: "published" as const,
      createdBy: "someone",
      version: 1,
      grantedCapabilities: [],
      approvals: [],
      lastUsedAt: 100,
      scopeId: scopeId("personal", "someone"),
    },
    shadowed: [],
  }));
  const { tc } = createToolContextWithout({
    provision: async () => {
      throw new Error("must not provision");
    },
    listSkills: async (source) => skills.filter(({ skill }) => skill.scopeId === (source ?? "personal:U1")),
  });
  assert.deepEqual(await tc.listSkills(), { sourceScopeId: null, skills: [] });
  assert.deepEqual(await tc.listSkills("", 0, 2, "personal:someone"), {
    sourceScopeId: "personal:someone",
    skills: [0, 1].map((i) => ({
      id: `id-${i}`,
      name: `skill-${i}`,
      description: "General task",
      source: "personal:someone",
    })),
    nextOffset: 2,
  });
  assert.deepEqual(await tc.listSkills("", 2, 2, "personal:someone"), {
    sourceScopeId: "personal:someone",
    skills: [{ id: "id-2", name: "skill-2", description: "Telescope planning", source: "personal:someone" }],
  });
  assert.deepEqual(await tc.listSkills("telescope", 0, 50, "personal:someone"), {
    sourceScopeId: "personal:someone",
    skills: [{ id: "id-2", name: "skill-2", description: "Telescope planning", source: "personal:someone" }],
  });
  skills[1]!.skill.scopeId = scopeId("personal", "U1");
  const native = await tc.listSkills();
  assert.deepEqual(native, {
    sourceScopeId: null,
    skills: [{ id: "id-1", name: "skill-1", description: "General task", source: "personal:U1" }],
  });
  assert.deepEqual(await tc.listSkills("", 0, 100, "personal:missing"), { sourceScopeId: null, skills: [] });
  skills.pop();
  assert.deepEqual(await tc.listSkills("telescope", 0, 50, "personal:someone"), { sourceScopeId: null, skills: [] });
});