Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

19.4 KB
Raw Blame
import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createOrchestrator, type OrchestratorInput, type OrchestratorDeps } from "../src/core/orchestrator.ts";
import { createIdentityService } from "../src/identity/identity-service.ts";
import { createMemoryConfigStore, type OrgBranding } from "../src/resolution/config-store.ts";
import { createAclStore } from "../src/acl/acl-store.ts";
import { createResolutionService } from "../src/resolution/resolution-service.ts";
import { createMemorySessionStore } from "../src/sessions/memory-session-store.ts";
import { createLocalWorkspaceStore } from "../src/workspace/workspace-store.ts";
import { createMemoryService } from "../src/memory/memory-service.ts";
import { createModelGateway } from "../src/model/model-gateway.ts";
import { createAuditLog } from "../src/audit/audit-log.ts";
import { createRateLimiter } from "../src/ratelimit/rate-limiter.ts";
import { createMockHarness } from "./support/mock-harness.ts";
import { createDeployStore } from "../src/deploy/deploy-store.ts";
import { createDockerDeployProvider } from "../src/deploy/docker-deploy-provider.ts";
import { createDeployService } from "../src/deploy/deploy-service.ts";
import { createMemoryFileArtifactStore } from "../src/files/file-artifact-store.ts";
import { createMemoryDurableByteStore } from "../src/files/durable-byte-store.ts";
import type { Sandbox } from "../src/sandbox/sandbox.ts";
import type { ConnectorStatusCache } from "../src/credentials/connector-status.ts";
import type { ConnectorTokenStore } from "../src/credentials/keychain.ts";
import type { SkillStore } from "../src/skills/skill-store.ts";
import { scopeId, type Conversation, type Principal } from "../src/types.ts";
import { singleProcessDeps } from "./support/single-process.ts";

const ORG = "default-org";

const actor: Principal = { id: "U1", type: "internal", displayName: "Alice" };

function fakeSandbox(): Sandbox {
  const unreached = () => {
    throw new Error("fakeSandbox: a !sysprompt turn must not touch the sandbox");
  };
  return {
    profile: {
      backend: "fake",
      writablePersistence: "snapshot_to_workspace",
      processSessions: false,
      spec: { os: "Debian 12 (bookworm)", tools: ["git", "jq"], workdir: "/workspace", homeDir: "/root" },
    },
    provision: unreached as never,
    run: unreached as never,
    readFile: unreached as never,
    writeFile: unreached as never,
    writeFileBytes: unreached as never,
    readFileBytes: unreached as never,
    listDir: unreached as never,
    removeDir: unreached as never,
    teardown: unreached as never,
  };
}

const connectorStatusCache: ConnectorStatusCache = {
  get: async () => ({ principalId: actor.id, checkedAt: Date.now(), providers: {} }),
  put: async () => {},
};

const connectorTokens = {
  listConnectorsByOwners: async () => new Map(),
  connectorAccessToken: async () => null,
  connectorTokenStatus: () => {
    throw new Error("connector tokens must not be swept when the status cache is fresh");
  },
} as unknown as ConnectorTokenStore;

const skills = {
  visibleFor: async () => [],
} as unknown as SkillStore;

async function buildOrchestrator(
  opts: {
    orgSoul?: string;
    sessionSyscalls?: OrchestratorDeps["sessionSyscalls"];
    featureFlags?: OrchestratorDeps["featureFlags"];
    scopeSoulFor?: { conversation: Conversation; soul: string };
    branding?: OrgBranding;
    brandingDefault?: OrgBranding;
  } = {},
) {
  const config = createMemoryConfigStore(ORG);
  if (opts.orgSoul !== undefined) await config.setSoul(scopeId("org", ORG), opts.orgSoul);
  if (opts.branding) await config.setBranding(scopeId("org", ORG), opts.branding);

  const acl = createAclStore();
  const auditLog = createAuditLog();
  const workspace = createLocalWorkspaceStore(mkdtempSync(join(tmpdir(), "pm-")));
  const memory = createMemoryService(workspace);
  const deploy = createDeployService({
    ...singleProcessDeps(),
    deployStore: createDeployStore(),
    provider: createDockerDeployProvider(),
    deployDir: join(tmpdir(), "pm-deploy"),
    auditLog,
    acl,
  });
  const resolution = createResolutionService(ORG, config, acl);

  if (opts.scopeSoulFor) {
    const scope = resolution.scopeFor(opts.scopeSoulFor.conversation, actor);
    await config.setSoul(scope, opts.scopeSoulFor.soul);
  }

  return createOrchestrator({
    ...singleProcessDeps(),
    identity: createIdentityService(),
    sessionSyscalls: opts.sessionSyscalls,
    featureFlags: opts.featureFlags,
    resolution,
    sessions: createMemorySessionStore(),
    workspace,
    files: createMemoryFileArtifactStore(createMemoryDurableByteStore()),
    sandbox: fakeSandbox(),
    modelGateway: createModelGateway(),
    auditLog,
    rateLimiter: createRateLimiter({ maxPerWindow: 1000, windowMs: 60_000 }),
    harness: createMockHarness(),
    memory,
    deploy,
    acl,
    config,
    ...(opts.brandingDefault ? { brandingDefault: opts.brandingDefault } : {}),
    skills,
    connectorTokens,
    connectorStatusCache,
    signingSecret: "test-signing-secret",
    apiBaseUrl: "https://api.test",
  });
}

const dmConversation: Conversation = { kind: "dm", threadRef: "dm:U1:pm1", audience: [actor] };
const channelConversation: Conversation = {
  kind: "channel",
  threadRef: "ch:C1:pm1",
  channelRef: "C1",
  audience: [actor],
};

const slackDm = (text: string, extra: Partial<OrchestratorInput> = {}): OrchestratorInput => ({
  surface: "slack",
  actor,
  conversation: dmConversation,
  text,
  ...extra,
  origin: extra.origin ?? { kind: "direct" },
});

const spineChannelTurn = (text: string, extra: Partial<OrchestratorInput> = {}): OrchestratorInput => ({
  surface: "slack",
  actor,
  conversation: channelConversation,
  text,
  surfaceTools: true,
  ...extra,
  origin: extra.origin ?? { kind: "direct" },
});

async function sysprompt(orch: ReturnType<typeof createOrchestrator>, input: OrchestratorInput): Promise<string> {
  const res = await orch.handleTurn({ ...input, text: "!sysprompt" });
  assert.equal(res.status, "ok", (res as { reason?: string }).reason);
  const prompt = res.reply ?? "";
  assert.notEqual(prompt, "", "expected !sysprompt to echo the assembled system prompt as the reply");
  return prompt;
}

function assertNoTemplateTokens(prompt: string, label: string) {
  assert.doesNotMatch(
    prompt,
    /\{\{/,
    `${label}: an unreplaced {{...}} template token leaked into the assembled prompt`,
  );
}

function countOccurrences(haystack: string, needle: string): number {
  if (needle.length === 0) return 0;
  let count = 0;
  let at = haystack.indexOf(needle);
  while (at !== -1) {
    count++;
    at = haystack.indexOf(needle, at + needle.length);
  }
  return count;
}

test("Mode 1 (DM): live-conversation frame, org policy once, no template leaks, no Mode-2 language", async () => {
  const ORG_SOUL = "ORG-POLICY-MARKER: be terse and never promise a refund.";
  const orch = await buildOrchestrator({ orgSoul: ORG_SOUL });
  const prompt = await sysprompt(orch, slackDm(""));

  assertNoTemplateTokens(prompt, "Mode 1 (DM)");

  assert.match(
    prompt,
    /You are QM, in a live private 1:1 with Alice(?: \([^)]+\))? over Slack\./,
    "expected the mode-conversation.md opening sentence, vars filled in for this DM turn",
  );

  assert.match(prompt, /What you write IS your reply/);

  assert.equal(
    countOccurrences(prompt, ORG_SOUL),
    1,
    "org policy text must appear exactly once in a DM prompt with no scope soul",
  );

  assert.doesNotMatch(prompt, /no one ever reads this transcript/);
  assert.doesNotMatch(prompt, /stay_silent/);
  assert.match(prompt, /You are QM/);
});

test("Mode 1 (DM): org policy still renders exactly once when the scope soul duplicates it verbatim", async () => {
  const ORG_SOUL = "ORG-POLICY-MARKER-2: escalate security incidents immediately.";
  const orch = await buildOrchestrator({
    orgSoul: ORG_SOUL,
    scopeSoulFor: { conversation: dmConversation, soul: ORG_SOUL },
  });
  const prompt = await sysprompt(orch, slackDm(""));

  assertNoTemplateTokens(prompt, "Mode 1 (DM, duplicate scope soul)");
  assert.equal(
    countOccurrences(prompt, ORG_SOUL),
    1,
    "resolve() must dedupe when the scope soul is string-equal (after trim) to the org soul — " +
      "CONTRACT.md §3 'Org-policy dedupe'",
  );
});

test("Mode 2 (spine channel): autonomous-worklog frame, org policy once, no template leaks, no Mode-1 language", async () => {
  const ORG_SOUL = "ORG-POLICY-MARKER-3: never share customer PII outside the org.";
  const orch = await buildOrchestrator({ orgSoul: ORG_SOUL });
  const prompt = await sysprompt(orch, spineChannelTurn(""));

  assertNoTemplateTokens(prompt, "Mode 2 (spine channel)");
  assert.match(prompt, /Once you.ve posted, just stop/);
  assert.doesNotMatch(prompt, /stay_silent|Nothing to add|end with a short log line/);

  assert.doesNotMatch(prompt, /no one ever reads this transcript|private worklog/);

  assert.equal(
    countOccurrences(prompt, ORG_SOUL),
    1,
    "org policy text must appear exactly once in a spine-channel prompt with no scope soul",
  );

  assert.doesNotMatch(prompt, /your reply is what the person reads/);
  assert.doesNotMatch(prompt, /\/v1\/reach/);
});

test("Mode 2 (spine channel): org policy still renders exactly once when the scope soul duplicates it verbatim", async () => {
  const ORG_SOUL = "ORG-POLICY-MARKER-4: all deploys require a second approver.";
  const orch = await buildOrchestrator({
    orgSoul: ORG_SOUL,
    scopeSoulFor: { conversation: channelConversation, soul: ORG_SOUL },
  });
  const prompt = await sysprompt(orch, spineChannelTurn(""));

  assertNoTemplateTokens(prompt, "Mode 2 (spine channel, duplicate scope soul)");
  assert.equal(
    countOccurrences(prompt, ORG_SOUL),
    1,
    "resolve() must dedupe the channel's own soul against the org soul the same way it does for a DM",
  );
});

test("Mode 1 and Mode 2 frames are mutually exclusive within one prompt", async () => {
  const dmPrompt = await sysprompt(await buildOrchestrator(), slackDm(""));
  const spinePrompt = await sysprompt(await buildOrchestrator(), spineChannelTurn(""));

  assert.match(dmPrompt, /live private 1:1/);
  assert.doesNotMatch(dmPrompt, /no one ever reads this transcript/);

  assert.match(spinePrompt, /Your words reach people ONLY through/);
  assert.doesNotMatch(spinePrompt, /live private 1:1/);
});

test("shared-core platform guidance reaches both the DM and the spine prompt", async () => {
  for (const prompt of [
    await sysprompt(await buildOrchestrator(), slackDm("")),
    await sysprompt(await buildOrchestrator(), spineChannelTurn("")),
  ]) {
    assert.match(prompt, /## System orientation/);
    assert.match(prompt, /## Files/);
    assert.match(prompt, /## Auth/);
    assert.match(prompt, /## Crons/);
    assert.match(prompt, /use `history` for this conversation/);
    assert.doesNotMatch(prompt, /`sessions` search for other accessible conversations/);
    assert.doesNotMatch(prompt, /## Scheduling & self-configuration/);
  }
});

test("session search guidance follows the callable surface and feature gate", async () => {
  for (const surface of ["web", "slack", "monitor"] as const) {
    for (const enabled of [false, true]) {
      for (const destination of [
        undefined,
        { type: "principal" as const, target: "U1", audienceScopeId: scopeId("personal", "U1"), onBehalfOf: "U1" },
      ]) {
        const orchestrator = await buildOrchestrator({
          sessionSyscalls: { forTurn: () => ({}) } as unknown as OrchestratorDeps["sessionSyscalls"],
          featureFlags: { enabled: async () => enabled } as unknown as OrchestratorDeps["featureFlags"],
        });
        const prompt = await sysprompt(
          orchestrator,
          slackDm("", {
            surface,
            ...(destination
              ? { origin: { kind: "automation", source: "cron", destination } as OrchestratorInput["origin"] }
              : {}),
          }),
        );
        assert.equal(
          prompt.includes("`sessions` search for other accessible conversations"),
          surface === "web" && enabled && !destination,
        );
      }
    }
  }
});

test("identity defaults to QM and 'this organization' when no branding is configured", async () => {
  const prompt = await sysprompt(await buildOrchestrator(), slackDm(""));
  assert.match(prompt, /# QM\n/);
  assert.match(prompt, /You are QM, a helpful org-wide assistant for this organization\./);
});

test("org branding renames the assistant and the organization across both modes", async () => {
  const branding: OrgBranding = { selfLabel: "straylight", orgName: "Straylight Industries" };
  const dmPrompt = await sysprompt(await buildOrchestrator({ branding }), slackDm(""));
  assert.match(dmPrompt, /# straylight\n/);
  assert.match(dmPrompt, /You are straylight, a helpful org-wide assistant for Straylight Industries\./);
  assert.match(dmPrompt, /You are straylight, in a live private 1:1 with Alice/);
  assert.doesNotMatch(dmPrompt, /You are QM/);

  const spinePrompt = await sysprompt(await buildOrchestrator({ branding }), spineChannelTurn(""));
  assert.match(spinePrompt, /You are straylight, in a channel conversation via Slack\./);
  assert.doesNotMatch(spinePrompt, /You are QM/);
});

test("web conversation surface label carries the configured name", async () => {
  const branding: OrgBranding = { selfLabel: "straylight" };
  const webPrompt = await sysprompt(await buildOrchestrator({ branding }), {
    surface: "web",
    actor,
    conversation: dmConversation,
    text: "",
    origin: { kind: "direct" },
  });
  assert.match(webPrompt, /over the straylight web app\./);
});

test("a Slack handle differing from the identity name appears as a mention adjunct in every mode", async () => {
  const branding: OrgBranding = { selfLabel: "straylight" };
  const withHandle = await sysprompt(
    await buildOrchestrator({ branding }),
    spineChannelTurn("", { gatewayContext: { botHandle: "qm-bot" } }),
  );
  assert.match(withHandle, /You are straylight \(@qm-bot in Slack\), a helpful org-wide assistant/);

  const dmWithHandle = await sysprompt(
    await buildOrchestrator({ branding }),
    slackDm("", { gatewayContext: { botHandle: "qm-bot" } }),
  );
  assert.match(dmWithHandle, /You are straylight \(@qm-bot in Slack\), a helpful org-wide assistant/);

  const matchingHandle = await sysprompt(
    await buildOrchestrator({ branding }),
    spineChannelTurn("", { gatewayContext: { botHandle: "Straylight" } }),
  );
  assert.match(matchingHandle, /You are straylight, a helpful org-wide assistant/);
  assert.doesNotMatch(matchingHandle, /in Slack\)/);

  const atHandle = await sysprompt(
    await buildOrchestrator({ branding }),
    spineChannelTurn("", { gatewayContext: { botHandle: "@qm-bot" } }),
  );
  assert.match(atHandle, /\(@qm-bot in Slack\)/);
  assert.doesNotMatch(atHandle, /@@/);

  const atMatchingHandle = await sysprompt(
    await buildOrchestrator({ branding }),
    spineChannelTurn("", { gatewayContext: { botHandle: "@straylight" } }),
  );
  assert.doesNotMatch(atMatchingHandle, /in Slack\)/);

  const hostileHandle = await sysprompt(
    await buildOrchestrator({ branding }),
    spineChannelTurn("", { gatewayContext: { botHandle: "{{qm-bot}}" } }),
  );
  assert.match(hostileHandle, /\(@qm-bot in Slack\)/);
  assert.doesNotMatch(hostileHandle, /\{\{/);
});

test("a display name containing template tokens cannot break prompt rendering", async () => {
  const hostileActor: Principal = { id: "U9", type: "internal", displayName: "Al{{ice}}" };
  const prompt = await sysprompt(await buildOrchestrator(), {
    surface: "slack",
    actor: hostileActor,
    conversation: { kind: "dm", threadRef: "dm:U9:pm9", audience: [hostileActor] },
    text: "",
    origin: { kind: "direct" },
  });
  const systemPrompt = prompt.split("\n\n<environment>")[0]!;
  assert.match(systemPrompt, /1:1 with Alice/);
  assert.doesNotMatch(systemPrompt, /\{\{/);
});

test("template tokens in a stored branding value are stripped, never rendered or thrown", async () => {
  const prompt = await sysprompt(
    await buildOrchestrator({ branding: { selfLabel: "{{straylight}}", orgName: "Acme {{Corp}}" } }),
    slackDm(""),
  );
  assert.match(prompt, /You are straylight, a helpful org-wide assistant for Acme Corp\./);
  assert.doesNotMatch(prompt, /\{\{/);
});

test("env brandingDefault names the assistant when the store has no branding, and the store wins over it", async () => {
  const brandingDefault: OrgBranding = { selfLabel: "envbot", orgName: "Env Org" };
  const fromEnv = await sysprompt(await buildOrchestrator({ brandingDefault }), slackDm(""));
  assert.match(fromEnv, /You are envbot, a helpful org-wide assistant for Env Org\./);

  const fromStore = await sysprompt(
    await buildOrchestrator({ brandingDefault, branding: { selfLabel: "storebot" } }),
    slackDm(""),
  );
  assert.match(fromStore, /You are storebot, a helpful org-wide assistant for Env Org\./);
});

test("Mode 2 (spine channel): static prose stays within the word-count ceiling (excl. live tail + soul)", async () => {
  const ORG_SOUL = "ORG-POLICY-MARKER-5: keep it short.";
  const orch = await buildOrchestrator({ orgSoul: ORG_SOUL });
  const prompt = await sysprompt(orch, spineChannelTurn("", { timezone: "America/New_York" }));

  const systemPrompt = prompt.split("\n\n<environment>")[0]!;
  assert.match(
    prompt,
    /## The user's local time/,
    "the volatile tail rides the environment note, not the system prompt",
  );

  const staticProse = systemPrompt.split(ORG_SOUL).join("");

  const wordCount = staticProse.trim().split(/\s+/).filter(Boolean).length;

  assert.ok(
    wordCount <= 1400,
    `spine-channel static prose is ${wordCount} words, over the 1400-word ceiling (CONTRACT.md S13). ` +
      "This is expected to fail until the menu deletions in CONTRACT.md S5 land.",
  );
});

for (const surface of ["web", "slack"]) {
  for (const mode of ["conversation", "autonomous", "fallback"]) {
    test(`${surface} ${mode} turns receive their surface formatting contract`, async () => {
      const prompt = await sysprompt(await buildOrchestrator(), {
        surface,
        actor,
        conversation: mode === "conversation" ? dmConversation : channelConversation,
        surfaceTools: mode === "autonomous",
        text: "",
        origin: mode === "fallback" ? { kind: "automation" } : { kind: "direct" },
      });
      if (surface === "web") {
        assert.match(prompt, /Use Markdown/);
        assert.match(prompt, /\[label\]\(url\)/);
        assert.doesNotMatch(prompt, /Slack-style links/);
      } else {
        assert.match(prompt, /Use Markdown text; Slack-style links are also supported/);
        assert.match(prompt, /<https:\/\/example.com\|label>/);
      }
      assert.equal(countOccurrences(prompt, "Use Markdown"), 1);
      const stablePrompt = prompt.split("\n\n<environment>")[0]!.trim();
      assert.equal(countOccurrences(stablePrompt, "## Rules for talking to users"), 1);
      assert.ok(stablePrompt.endsWith("4. Describe work in human terms and not system machinery."));
      assertNoTemplateTokens(prompt, `${surface} ${mode}`);
    });
  }
}

test("web group conversations are not described as private 1:1s", async () => {
  const prompt = await sysprompt(await buildOrchestrator(), {
    surface: "web",
    actor,
    conversation: { kind: "group", channelRef: "G1", threadRef: "G1:t", audience: [actor] },
    text: "",
    origin: { kind: "direct" },
  });
  assert.match(prompt, /live group conversation with Alice/);
  assert.doesNotMatch(prompt, /private 1:1/);
});