Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

12.0 KB
Raw Blame
import { test } from "node:test";
import assert from "node:assert/strict";
import { randomBytes, randomUUID } from "node:crypto";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { readFile } from "node:fs/promises";
import { createLocalDurableByteStore } from "../src/files/durable-byte-store.ts";
import { applyPgMigrations, createPgPool, registeredPgMigrations } from "../src/persistence/pg-pool.ts";
import { createPostgresSessionStore } from "../src/sessions/postgres-session-store.ts";
import { DuplicateCallError } from "../src/sessions/session-store.ts";
import {
  createPostgresToolOutputStore,
  cutToolOutput,
  DEFAULT_INLINE_TOKENS,
  parkedOutputKey,
} from "../src/sessions/tool-output.ts";
import { scopeId } from "../src/types.ts";

const URL = process.env.DATABASE_URL;
const skip = URL ? false : "set DATABASE_URL (a Postgres) to run the items contract test";

async function withSchema(
  prefix: string,
  fn: (ctx: {
    raw: import("pg").Pool;
    url: string;
    store: ReturnType<typeof createPostgresSessionStore>;
  }) => Promise<void>,
): Promise<void> {
  const pg = (await import("pg")).default;
  const admin = new pg.Pool({ connectionString: URL });
  const schema = `${prefix}_${randomUUID().replaceAll("-", "")}`;
  await admin.query(`CREATE SCHEMA ${schema}`);
  const url = new globalThis.URL(URL!);
  url.searchParams.set("options", `-c search_path=${schema}`);
  const raw = new pg.Pool({ connectionString: url.toString() });
  try {
    await fn({ raw, url: url.toString(), store: createPostgresSessionStore(url.toString()) });
  } finally {
    await raw.end();
    await admin.query(`DROP SCHEMA ${schema} CASCADE`);
    await admin.end();
  }
}

test("pg items keep turn, author and time in typed columns", { skip }, async () => {
  await withSchema("items_columns", async ({ raw, store }) => {
    const scope = scopeId("personal", "alice");
    const session = await store.getOrCreateByThread("web:columns", "dm", scope);
    const { lease } = await store.acquireLease(session.id);
    assert.ok(lease);
    const runId = randomUUID();
    const turn = { ...lease, turnId: runId };
    const user = await store.append(turn, {
      type: "user",
      payload: { text: "hi" },
      scopeLabel: scope,
      author: "alice",
    });
    await store.append(turn, {
      type: "user",
      payload: { text: "also", steered: true },
      scopeLabel: scope,
      author: "bob",
    });
    await store.append(turn, {
      type: "assistant",
      payload: { text: "hello" },
      scopeLabel: scope,
      author: "ignored-on-agent-items",
    });
    await store.append(
      { ...lease, turnId: "not-a-uuid" },
      {
        type: "tool_call",
        payload: { tool: "execute", callId: "c1" },
        scopeLabel: scope,
      },
    );
    await store.append(lease, { type: "system", payload: { kind: "turn_failure", message: "x" }, scopeLabel: scope });
    const stopped = await store.append(turn, {
      type: "system",
      payload: { kind: "turn_stopped", reason: "user", text: "Stopped" },
      scopeLabel: scope,
      author: "alice",
    });
    assert.deepEqual((await store.getEntry(session.id, stopped.seq))?.payload, {
      kind: "turn_stopped",
      reason: "user",
      text: "Stopped",
      runId,
    });
    await assert.rejects(
      store.append(lease, { type: "system", payload: { kind: "goal", goal: {} }, scopeLabel: scope }),
      /goal/,
    );
    await store.releaseLease(lease);

    const rows = (
      await raw.query(
        `SELECT seq::int, kind, turn_id::text, author, error_code, (extract(epoch FROM created_at) * 1000)::bigint AS ms
           FROM items WHERE session_id = $1 ORDER BY seq`,
        [session.id],
      )
    ).rows;
    assert.deepEqual(
      rows.map((row) => [row.seq, row.kind, row.turn_id, row.author, row.error_code]),
      [
        [0, "UserMessage", runId, "alice", null],
        [1, "SteeringMessage", runId, "bob", null],
        [2, "AgentMessage", runId, null, null],
        [3, "ToolCall", null, null, null],
        [4, "Error", null, null, "turn_failure"],
        [5, "Stop", runId, "alice", null],
      ],
    );
    assert.equal(Number(rows[0].ms), user.createdAt);
    assert.equal((await store.getEntries(session.id))[0]!.createdAt, user.createdAt);
    await assert.rejects(
      raw.query(
        "INSERT INTO items(tenant_id, session_id, seq, kind, author, body, created_at) VALUES (qm_tenant_id(), $1, 9, 'AgentMessage', 'x', '{}', now())",
        [session.id],
      ),
      /items_author_kind/,
    );
  });
});

test("pg items reject a second call with the same call id, leaving the log untouched", { skip }, async () => {
  await withSchema("items_calls", async ({ store }) => {
    const scope = scopeId("personal", "alice");
    const session = await store.getOrCreateByThread("web:calls", "dm", scope);
    const { lease } = await store.acquireLease(session.id);
    assert.ok(lease);
    const callId = "cron:1";
    const call = {
      type: "tool_call" as const,
      payload: { tool: "deliver", callId, text: "hi" },
      scopeLabel: scope,
    };
    const result = {
      type: "tool_result" as const,
      payload: { tool: "deliver", callId, result: "delivered" },
      scopeLabel: scope,
    };
    await store.appendMany(lease, [call, result]);
    await assert.rejects(store.appendMany(lease, [call, result]), DuplicateCallError);
    const entries = await store.getEntries(session.id);
    assert.equal(entries.length, 2);
    assert.equal((entries[0]!.payload as { callId: string }).callId, callId);
    const next = await store.append(lease, { type: "assistant", payload: { text: "after" }, scopeLabel: scope });
    assert.equal(next.seq, 2);
    await store.releaseLease(lease);
  });
});

test(
  "pg tool output: the inline cap comes from qm_settings and parked text reads back by a gateway call id carrying a large thought signature",
  { skip },
  async () => {
    await withSchema("items_tool_output", async ({ raw, url, store }) => {
      const dir = await mkdtemp(join(tmpdir(), "tool-output-"));
      const pool = createPgPool(url);
      try {
        const scope = scopeId("personal", "alice");
        const session = await store.getOrCreateByThread("web:tool-output", "dm", scope);
        let clock = Date.now();
        const outputs = createPostgresToolOutputStore({
          pool,
          bytesFor: (prefix) => createLocalDurableByteStore(join(dir, prefix)),
          now: () => clock,
        });
        assert.equal(await outputs.inlineTokens(), DEFAULT_INLINE_TOKENS);
        await raw.query(
          "INSERT INTO qm_settings(key, value) VALUES ('tool_result_inline_tokens', '0') ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value",
        );
        assert.equal(await outputs.inlineTokens(), DEFAULT_INLINE_TOKENS, "cached for a short while");
        clock += 60_000;
        assert.equal(await outputs.inlineTokens(), 0);

        const full = "ünï line\n".repeat(10_000);
        const blob = await outputs.save(full);
        const callId = `call_${randomUUID()}__thought__${randomBytes(4_500).toString("base64")}`;
        const { lease } = await store.acquireLease(session.id);
        assert.ok(lease);
        const call = { type: "tool_call" as const, payload: { tool: "execute", callId }, scopeLabel: scope };
        await store.append(lease, call);
        await store.append(lease, {
          type: "tool_result",
          payload: {
            tool: "execute",
            callId,
            result: "head…tail",
            output: { blob, chars: full.length, shownChars: 8 },
          },
          scopeLabel: scope,
        });
        await assert.rejects(store.append(lease, call), DuplicateCallError);
        await store.append(lease, {
          type: "tool_result",
          payload: { tool: "execute", callId: "toolu_short", result: "head…tail", output: { blob } },
          scopeLabel: scope,
        });
        await store.releaseLease(lease);
        const entries = await store.getEntries(session.id);
        assert.deepEqual(
          entries.map((entry) => (entry.payload as { callId?: string }).callId),
          [callId, callId, "toolu_short"],
        );
        const markerPath = /tool-output\/\S+(?=;)/.exec(cutToolOutput(full, 1, callId)!.text)![0];
        assert.deepEqual(await outputs.read(session.id, parkedOutputKey(markerPath)!), { text: full });
        assert.deepEqual(await outputs.read(session.id, parkedOutputKey("tool-output/toolu_short")!), { text: full });
        assert.equal(await outputs.read(session.id, parkedOutputKey("tool-output/missing")!), null);
        assert.equal(await outputs.read("other-session", parkedOutputKey(markerPath)!), null);
        const row = (
          await raw.query(
            "SELECT size, storage_key, (extract(epoch FROM expires_at - created_at) / 86400)::int AS days FROM blobs",
          )
        ).rows[0];
        assert.deepEqual([Number(row.size), row.days], [Buffer.byteLength(full), 30]);
        assert.match(row.storage_key, /^tool-output\/[0-9a-f-]{36}\/files\/[0-9a-f]{64}$/);
      } finally {
        await pool.close();
        await rm(dir, { recursive: true, force: true });
      }
    });
  },
);

test(
  "pg items cutover drops legacy participant-activity triggers so participant windows stay exact",
  { skip },
  async () => {
    await withSchema("items_residue", async ({ raw, url, store }) => {
      await applyPgMigrations(
        raw,
        registeredPgMigrations(url).filter((migration) => migration.id < "sessions/store/0029"),
      );
      await raw.query(
        await readFile(
          new globalThis.URL("./fixtures/legacy-participant-activity-triggers.sql", import.meta.url),
          "utf8",
        ),
      );
      const scope = scopeId("personal", "alice");
      const session = await store.getOrCreateByThread("web:residue", "dm", scope);
      const { lease } = await store.acquireLease(session.id);
      assert.ok(lease);
      await store.append(lease, { type: "user", payload: { text: "one" }, scopeLabel: scope });
      await store.releaseLease(lease);
      await store.addParticipant(session.id, "bob", undefined, { includeHistory: true });
      await store.addParticipant(session.id, "carol");
      assert.deepEqual(
        (
          await raw.query("SELECT principal_id, valid_from_seq FROM participants WHERE session_id = $1 ORDER BY 1", [
            session.id,
          ])
        ).rows.map((row) => [row.principal_id, row.valid_from_seq]),
        [
          ["bob", 0],
          ["carol", 1],
        ],
      );
    });
  },
);

test("pg items hide a Slack reply receipt once the real message arrives", { skip }, async () => {
  await withSchema("items_receipts", async ({ store }) => {
    const scope = scopeId("personal", "alice");
    const session = await store.getOrCreateByThread("slack:T1:C1:1.0", "dm", scope);
    const { lease } = await store.acquireLease(session.id);
    assert.ok(lease);
    const receipt = {
      type: "user" as const,
      payload: { text: "sent from web", name: "Alice", ts: "2.0", slackReplyReceipt: true, deliveryKey: "k-1" },
      scopeLabel: scope,
    };
    await store.appendMany({ ...lease, turnId: randomUUID() }, [receipt]);
    assert.equal(await store.hasDeliveryEntry(session.id, "k-1"), true);
    assert.equal(await store.hasDeliveryEntry(session.id, "k-2", "2.0"), true);
    assert.equal(await store.hasDeliveryEntry(session.id, "k-2", "3.0"), false);
    const texts = async () =>
      (await store.getEntries(session.id, { transcript: true })).map((e) => {
        const payload = e.payload as { text?: string; slackReplyReceipt?: boolean };
        return [payload.text, payload.slackReplyReceipt === true];
      });
    assert.deepEqual(await texts(), [["sent from web", true]]);
    await store.append(lease, { type: "user", payload: { text: "sent from web", ts: "2.0" }, scopeLabel: scope });
    assert.deepEqual(await texts(), [["sent from web", false]]);
    assert.equal(await store.countEntriesBefore(session.id, 10), 1);
    assert.equal((await store.getEntries(session.id)).length, 2);
  });
});