Skip to content

A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.

TypeScript
25.7 KB
Raw Blame
import assert from "node:assert/strict";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { after, before, test } from "node:test";
import { createApp } from "../apps/server/src/app.ts";
import { createStore, type Store } from "../apps/server/src/db.ts";
import type {
  AgentNotification,
  AgentTask,
  Goal,
  Idea,
  Monitor,
} from "../packages/domain/src/agent.ts";
import type { ActionProposal, Artifact } from "../packages/domain/src/index.ts";
import { createSamplePdf } from "../packages/integrations/src/pdf.ts";

let db: Store, server: Awaited<ReturnType<typeof createApp>>, directory: string;
const owner = "workflow-user";
const reconcile = () => (server.agent as unknown as { maintain(): Promise<void> }).maintain();
before(async () => {
  directory = await mkdtemp(join(tmpdir(), "openmuse-workflows-"));
  db = await createStore({ dataDir: join(directory, "db") });
  server = await createApp(db, {
    mode: "sample",
    port: 8787,
    host: "127.0.0.1",
    publicUrl: "http://localhost:8787",
    dataDir: directory,
    agentBackend: "sample",
    intelligenceApiKey: "test-project-key-never-sent",
    googleRedirectUri: "http://localhost:8787/api/google/callback",
    allowedOrigins: [],
  });
  await server.workspace.ensureSample(owner, server.actions);
});
after(async () => {
  await server.agent.stop();
  await db.close();
  await rm(directory, { recursive: true, force: true });
});

async function documentTask() {
  const w = await server.workspace.snapshot(owner);
  const mail = w.mail.find((m) => m.attachments.length);
  assert.ok(mail);
  const task = await server.agent.createTask(owner, {
    title: "Return school form",
    prompt: "Fill the school form and prepare a reply",
    kind: "document",
    input: { messageId: mail.id },
  });
  await server.agent.worker.tick();
  const waiting = await server.agent.getTask(owner, task.id);
  assert.equal(waiting.status, "waiting_input");
  assert.ok(Array.isArray(waiting.state.missingFields));
  await server.agent.answer(owner, task.id, "Use these fictional test values", {
    participant_name: "Test Student",
    guardian_name: "Test Guardian",
    permission_granted: true,
  });
  await server.agent.worker.tick();
  const reviewed = await server.agent.detail(owner, task.id);
  assert.equal(reviewed.task.status, "waiting_approval");
  assert.equal(reviewed.files.length, 1);
  assert.ok(reviewed.files[0].url);
  assert.ok(reviewed.task.actionId);
  const action = await db.get<ActionProposal>(owner, "actions", reviewed.task.actionId);
  assert.ok(action);
  assert.equal(action.taskId, task.id);
  return { task: reviewed.task, action, originalId: mail.attachments[0] };
}

test("document job runs without a client, waits for review, and resumes from its receipt", async () => {
  const initialIdeas = await server.agent.refreshIdeas(owner);
  const originalIdea = initialIdeas.find((idea) => idea.kind === "document");
  assert.ok(originalIdea);
  const { task, action, originalId } = await documentTask();
  assert.equal((await server.files.get(owner, originalId)).parentId, undefined);
  assert.equal(
    (await server.workspace.snapshot(owner)).mail.filter((m) => m.subject.startsWith("Re:")).length,
    0,
  );
  await server.agent.control(owner, task.id, "pause");
  await assert.rejects(
    server.actions.decide(owner, action.id, action.hash, "approve"),
    /Resume the task/,
  );
  await server.agent.control(owner, task.id, "resume");
  const receipt = await server.actions.decide(owner, action.id, action.hash, "approve");
  assert.equal(receipt.status, "succeeded");
  await server.agent.worker.tick();
  assert.equal((await server.agent.getTask(owner, task.id)).status, "succeeded");
  const notices = await db.list<AgentNotification>(owner, "notifications");
  assert.equal(notices.filter((n) => n.taskId === task.id && n.title === task.title).length, 1);
  await server.agent.worker.tick();
  assert.equal(
    (await db.list<ActionProposal>(owner, "actions")).filter((a) => a.taskId === task.id).length,
    1,
  );
  const refreshedIdeas = await server.agent.refreshIdeas(owner);
  assert.equal(refreshedIdeas.find((idea) => idea.id === originalIdea.id)?.status, "dismissed");
  assert.equal(
    refreshedIdeas.filter((idea) => idea.kind === "document" && idea.status === "new").length,
    0,
  );
});

test("document retry reuses the filled PDF after its task checkpoint is lost", async (t) => {
  const replayOwner = "interrupted-document";
  await server.workspace.ensureSample(replayOwner, server.actions);
  const workspace = await server.workspace.snapshot(replayOwner);
  const mail = workspace.mail.find((message) => message.attachments.length);
  assert.ok(mail);
  const task = await server.agent.createTask(replayOwner, {
    prompt: "Fill the sample form",
    kind: "document",
    input: { messageId: mail.id, fields: { participant_name: "Sample Student" } },
  });
  const compareAndSwap = db.compareAndSwap.bind(db);
  let interrupted = false;
  t.mock.method(db, "compareAndSwap", async (...args: Parameters<Store["compareAndSwap"]>) => {
    const [recordOwner, kind, id, , patch] = args;
    if (
      recordOwner === replayOwner &&
      kind === "tasks" &&
      id === task.id &&
      (patch.state as AgentTask["state"] | undefined)?.filledId &&
      !interrupted
    ) {
      interrupted = true;
      return null;
    }
    return compareAndSwap(...args);
  });
  await server.agent.worker.tick();
  assert.ok(interrupted);
  const retry = await server.agent.getTask(replayOwner, task.id);
  assert.equal(retry.status, "queued");
  assert.equal(retry.state.filledId, undefined);
  const outputs = (await db.list<Artifact>(replayOwner, "files")).filter((file) => file.parentId);
  assert.equal(outputs.length, 1);

  await server.agent.worker.tick();
  const resumed = await server.agent.getTask(replayOwner, task.id);
  assert.equal(resumed.status, "waiting_approval", resumed.error ?? resumed.question);
  assert.equal(resumed.state.filledId, outputs[0].id);
  assert.equal(
    (await db.list<Artifact>(replayOwner, "files")).filter((file) => file.parentId).length,
    1,
  );
});

test("attachment import recovers after losing its mapping and isolates reconnections", async (t) => {
  const attachmentOwner = "attachment-recovery";
  await server.workspace.ensureSample(attachmentOwner, server.actions);
  const workspace = await server.workspace.snapshot(attachmentOwner);
  const mail = workspace.mail[0];
  const reference = `${mail.id}:sample-attachment:sample.pdf`;
  await db.put(attachmentOwner, "mail", {
    ...mail,
    attachments: [reference],
    connectionId: "sample-google",
  });
  const google = server.workspace.google(attachmentOwner);
  const bytes = await createSamplePdf();
  t.mock.method(google, "getAttachment", async () => bytes);
  t.mock.method(server.workspace, "google", () => google);
  const put = db.put.bind(db);
  let interrupted = false;
  t.mock.method(db, "put", async (...args: Parameters<Store["put"]>) => {
    if (args[0] === attachmentOwner && args[1] === "imports" && !interrupted) {
      interrupted = true;
      throw new Error("mapping unavailable");
    }
    return put(...args);
  });
  const before = await server.files.list(attachmentOwner);
  await assert.rejects(
    server.workspace.importAttachment(attachmentOwner, reference),
    /mapping unavailable/,
  );
  const published = (await server.files.list(attachmentOwner)).find(
    (file) => !before.some((old) => old.id === file.id),
  );
  assert.ok(published);
  const recovered = await server.workspace.importAttachment(attachmentOwner, reference);
  assert.equal(recovered.id, published.id);
  assert.equal((await server.files.list(attachmentOwner)).length, before.length + 1);

  await db.put(attachmentOwner, "settings", { id: "google", connectionId: "new-connection" });
  await db.put(attachmentOwner, "mail", {
    ...mail,
    attachments: [reference],
    connectionId: "new-connection",
  });
  assert.notEqual(
    (await server.workspace.importAttachment(attachmentOwner, reference)).id,
    recovered.id,
  );
});

test("ideas ignore sent replies while retaining unfinished incoming requests", async () => {
  const ideaOwner = "sent-reply-ideas";
  await server.workspace.ensureSample(ideaOwner, server.actions);
  const workspace = await server.workspace.snapshot(ideaOwner);
  const incoming = workspace.mail.find((mail) => mail.attachments.length);
  assert.ok(incoming);
  await server.workspace.execute(ideaOwner, {
    kind: "email.send",
    data: {
      to: [incoming.from],
      cc: [],
      bcc: [],
      subject: "Re: Complete the form and schedule a meeting",
      body: "Here is the completed permission form. Let's meet for coffee.",
      attachmentIds: incoming.attachments,
    },
  });
  const ideas = await server.agent.refreshIdeas(ideaOwner);
  const sent = (await server.workspace.snapshot(ideaOwner)).mail.find(
    (mail) => mail.sender === "You",
  );
  assert.ok(sent);
  assert.ok(ideas.some((idea) => idea.input.messageId === incoming.id));
  assert.ok(!ideas.some((idea) => idea.input.messageId === sent.id));
});

test("accepting a goal idea keeps the task attached to the original goal", async () => {
  const ideaOwner = "goal-plan-ideas";
  const goal = await server.agent.createGoal(ideaOwner, {
    title: "Plan a walking routine",
    description: "Walk three times each week",
    category: "Health",
  });
  const ideas = await server.agent.refreshIdeas(ideaOwner);
  const idea = ideas.find((candidate) => candidate.input.goalId === goal.id);
  assert.ok(idea);

  const accepted = await server.agent.decideIdea(ideaOwner, idea.id, "accept");
  assert.ok(accepted?.taskId);
  const task = await server.agent.getTask(ideaOwner, accepted.taskId);
  assert.equal(task.goalId, goal.id);
  assert.deepEqual(await db.list<Goal>(ideaOwner, "goals"), [goal]);

  const retried = await server.agent.decideIdea(ideaOwner, idea.id, "accept");
  assert.equal(retried?.taskId, task.id);
  assert.equal((await db.list<AgentTask>(ideaOwner, "tasks")).length, 1);
  assert.deepEqual(await db.list<Goal>(ideaOwner, "goals"), [goal]);

  await server.agent.updateGoal(ideaOwner, goal.id, { status: "paused" });
  assert.equal((await server.agent.getTask(ideaOwner, task.id)).status, "paused");
});

test("accepting an idea without a goal still creates one goal and task", async () => {
  const ideaOwner = "standalone-ideas";
  const idea: Idea = {
    id: "standalone-plan",
    title: "Plan a weekend walk",
    reason: "Make time to get outdoors",
    prompt: "Plan a weekend walk",
    kind: "plan",
    input: {},
    evidence: [],
    status: "new",
    createdAt: new Date().toISOString(),
  };
  await db.put(ideaOwner, "ideas", idea);
  const accepted = await server.agent.decideIdea(ideaOwner, idea.id, "accept");
  assert.ok(accepted?.taskId);
  await server.agent.decideIdea(ideaOwner, idea.id, "accept");

  const goals = await db.list<Goal>(ideaOwner, "goals");
  assert.equal(goals.length, 1);
  assert.equal(goals[0].title, idea.title);
  assert.equal(goals[0].description, idea.reason);
  const tasks = await db.list<AgentTask>(ideaOwner, "tasks");
  assert.equal(tasks.length, 1);
  assert.equal(tasks[0].id, accepted.taskId);
  assert.equal(tasks[0].goalId, goals[0].id);
});

test("cancelling a task denies its pending action", async () => {
  const { task, action } = await documentTask();
  await server.agent.control(owner, task.id, "cancel");
  assert.equal(
    (await server.actions.decide(owner, action.id, action.hash, "approve")).status,
    "denied",
  );
  await server.agent.worker.tick();
  assert.equal((await server.agent.getTask(owner, task.id)).status, "cancelled");
});

test("failed page checks back off, expose the error, and pause after repeated failures", async () => {
  const monitor = await server.agent.createMonitor(owner, {
    title: "Public availability",
    url: "https://example.com",
  });
  for (let i = 1; i <= 5; i++) {
    await server.agent.worker.tick();
    const task = await server.agent.getTask(owner, monitor.taskId);
    assert.equal(task.status, i < 5 ? "scheduled" : "paused");
    assert.ok(task.error);
    assert.equal(task.state.failures, i);
    if (i < 5)
      await db.compareAndSwap(
        owner,
        "tasks",
        task.id,
        { status: "scheduled" },
        { nextRunAt: "2020-01-01T00:00:00Z" },
      );
  }
  assert.equal((await db.get<Monitor>(owner, "monitors", monitor.id))?.status, "paused");
});

test("each failure streak of a watch raises its own alerts after it is resumed", async () => {
  const monitor = await server.agent.createMonitor(owner, {
    title: "Resumed availability",
    url: "https://example.com",
  });
  const failUntilPaused = async () => {
    for (let i = 1; i <= 5; i++) {
      await server.agent.worker.tick();
      const task = await server.agent.getTask(owner, monitor.taskId);
      if (i < 5)
        await db.compareAndSwap(
          owner,
          "tasks",
          task.id,
          { status: "scheduled" },
          { nextRunAt: "2020-01-01T00:00:00Z" },
        );
    }
    assert.equal((await server.agent.getTask(owner, monitor.taskId)).status, "paused");
  };
  const alerts = async () =>
    (await db.list<AgentNotification>(owner, "notifications")).filter(
      (n) => n.taskId === monitor.taskId && n.title === "Watch needs attention",
    );
  await failUntilPaused();
  assert.equal((await alerts()).length, 2, "one retry alert and one paused alert");
  await server.agent.controlMonitor(owner, monitor.id, "resume");
  await failUntilPaused();
  assert.equal((await alerts()).length, 4, "the second streak alerts again");
  // Replaying the same outcome must not duplicate the alert.
  await server.agent.worker.tick();
  assert.equal((await alerts()).length, 4);
});

test("dismissal racing acceptance never creates work for a dismissed idea", async () => {
  for (let i = 0; i < 4; i++) {
    const idea: Idea = {
      id: `race-${i}`,
      title: `Plan a walk ${i}`,
      reason: "User context",
      prompt: "Plan a walk",
      kind: "plan",
      input: {},
      evidence: [],
      status: "new",
      createdAt: new Date().toISOString(),
    };
    await db.put(owner, "ideas", idea);
    await Promise.all([
      server.agent.decideIdea(owner, idea.id, "dismiss"),
      server.agent.decideIdea(owner, idea.id, "accept"),
    ]);
    const saved = await db.get<Idea>(owner, "ideas", idea.id);
    const tasks = await db.list<AgentTask>(owner, "tasks");
    if (saved?.status === "dismissed")
      assert.equal(tasks.filter((t) => t.title === idea.title).length, 0);
    else assert.ok(saved?.taskId && tasks.some((t) => t.id === saved.taskId));
  }
});

test("milestone-linked outcomes preserve manual progress and legacy goal tasks still append once", async () => {
  const goal = await server.agent.createGoal(owner, {
    title: "Budget",
    milestones: ["Review spending", "Save"],
  });
  const input = {
    prompt: "Review spending",
    kind: "finance",
    goalId: goal.id,
    milestoneId: goal.milestones[0].id,
    input: { csv: "date,description,amount,category\n2026-09-01,Groceries,54.20,Food" },
  };
  const task = await server.agent.createTask(owner, input, "linked-finance");
  const reordered = [
    { ...goal.milestones[1], done: true },
    { ...goal.milestones[0], title: "Review September" },
  ];
  await server.agent.updateGoal(owner, goal.id, { milestones: reordered });
  await server.agent.worker.tick();
  const detail = await server.agent.detail(owner, task.id);
  assert.equal(detail.task.status, "succeeded");
  assert.equal(detail.task.milestoneId, goal.milestones[0].id);
  assert.ok(detail.artifacts.length);
  assert.deepEqual((await db.get<Goal>(owner, "goals", goal.id))?.milestones, reordered);
  await server.agent.setMilestoneDone(owner, goal.id, goal.milestones[0].id, true);
  // Startup maintenance replays durable outcomes after process interruptions.
  await reconcile();
  const completed = reordered.map((m) => ({ ...m, done: true }));
  assert.deepEqual((await db.get<Goal>(owner, "goals", goal.id))?.milestones, completed);
  await server.agent.updateGoal(owner, goal.id, { milestones: [completed[0]] });
  await reconcile();
  assert.deepEqual((await db.get<Goal>(owner, "goals", goal.id))?.milestones, [completed[0]]);
  assert.ok((await server.agent.detail(owner, task.id)).artifacts.length);
  const legacy = await server.agent.createTask(
    owner,
    { ...input, milestoneId: undefined },
    "legacy-finance",
  );
  await server.agent.worker.tick();
  await reconcile();
  await reconcile();
  const milestones = (await db.get<Goal>(owner, "goals", goal.id))?.milestones;
  assert.equal(milestones?.length, 2);
  assert.equal(milestones?.filter((m) => m.id === legacy.id && m.done).length, 1);
});

test("failed, cancelled and review-blocked tasks do not complete milestones", async () => {
  const goal = await server.agent.createGoal(owner, {
    title: "Documents",
    milestones: ["Return form"],
  });
  const link = { goalId: goal.id, milestoneId: goal.milestones[0].id };
  const failed = await server.agent.createTask(owner, {
    ...link,
    prompt: "Analyze invalid CSV",
    kind: "finance",
    input: { csv: "invalid" },
  });
  await server.agent.worker.tick();
  assert.equal((await server.agent.getTask(owner, failed.id)).status, "failed");
  const cancelled = await server.agent.createTask(owner, { ...link, prompt: "Cancelled" });
  await server.agent.control(owner, cancelled.id, "cancel");
  const mail = (await server.workspace.snapshot(owner)).mail.find((m) => m.attachments.length);
  assert.ok(mail);
  const document = await server.agent.createTask(owner, {
    ...link,
    prompt: "Return form",
    kind: "document",
    input: { messageId: mail.id },
  });
  await server.agent.worker.tick();
  assert.equal((await server.agent.getTask(owner, document.id)).status, "waiting_input");
  assert.deepEqual((await db.get<Goal>(owner, "goals", goal.id))?.milestones, goal.milestones);
  await server.agent.answer(owner, document.id, "Fictional test values", {
    participant_name: "Test Student",
    guardian_name: "Test Guardian",
    permission_granted: true,
  });
  await server.agent.worker.tick();
  const waiting = await server.agent.getTask(owner, document.id);
  assert.equal(waiting.status, "waiting_approval");
  assert.deepEqual((await db.get<Goal>(owner, "goals", goal.id))?.milestones, goal.milestones);
  await server.agent.updateGoal(owner, goal.id, { status: "paused" });
  assert.ok(waiting.actionId);
  const action = await db.get<ActionProposal>(owner, "actions", waiting.actionId);
  assert.ok(action);
  await assert.rejects(
    server.actions.decide(owner, action.id, action.hash, "approve"),
    /Resume the task/,
  );
  await server.agent.control(owner, document.id, "cancel");
  await reconcile();
  assert.deepEqual((await db.get<Goal>(owner, "goals", goal.id))?.milestones, goal.milestones);
});

test("linked task results and manual completion survive a database restart", async () => {
  const root = await mkdtemp(join(tmpdir(), "openmuse-milestone-restart-"));
  const config = { ...server.agent.config, dataDir: root };
  let store = await createStore({ dataDir: join(root, "db") });
  let app = await createApp(store, config);
  try {
    const goal = await app.agent.createGoal(owner, {
      title: "Budget",
      milestones: ["Review spending"],
    });
    const task = await app.agent.createTask(
      owner,
      {
        prompt: "Review spending",
        kind: "finance",
        goalId: goal.id,
        milestoneId: goal.milestones[0].id,
        input: { csv: "date,description,amount,category\n2026-09-01,Groceries,54.20,Food" },
      },
      "restart-delegation",
    );
    await app.agent.worker.tick();
    assert.equal((await app.agent.getTask(owner, task.id)).status, "succeeded");
    await app.agent.stop();
    await store.close();
    store = await createStore({ dataDir: join(root, "db") });
    app = await createApp(store, config);
    app.agent.start();
    await app.agent.stop();
    assert.deepEqual((await store.get<Goal>(owner, "goals", goal.id))?.milestones, goal.milestones);
    const restored = await app.agent.detail(owner, task.id);
    assert.equal(restored.task.milestoneId, goal.milestones[0].id);
    assert.ok(restored.artifacts.length);
    await app.agent.setMilestoneDone(owner, goal.id, goal.milestones[0].id, true);
    app.agent.start();
    await app.agent.stop();
    assert.deepEqual((await store.get<Goal>(owner, "goals", goal.id))?.milestones, [
      { ...goal.milestones[0], done: true },
    ]);
  } finally {
    await app.agent.stop();
    await store.close();
    await rm(root, { recursive: true, force: true });
  }
});

test("accepting an idea opens the task already handling its email instead of starting another", async () => {
  const ideaOwner = "idea-in-progress";
  await server.workspace.ensureSample(ideaOwner, server.actions);
  const idea = (await server.agent.refreshIdeas(ideaOwner)).find(
    (candidate) => candidate.kind === "document" && candidate.status === "new",
  );
  assert.ok(idea);
  const existing = await server.agent.createTask(
    ideaOwner,
    { kind: "document", prompt: "Complete the permission slip", input: idea.input },
    "chat-request",
  );
  const accepted = await server.agent.decideIdea(ideaOwner, idea.id, "accept");
  assert.equal(accepted?.taskId, existing.id);
  const again = await server.agent.decideIdea(ideaOwner, idea.id, "accept");
  assert.equal(again?.taskId, existing.id);
  const tasks = (await db.list<AgentTask>(ideaOwner, "tasks")).filter(
    (task) => task.kind === "document" && task.input.messageId === idea.input.messageId,
  );
  assert.equal(tasks.length, 1);
});
test("a cancelled task does not stop its idea from starting fresh work", async () => {
  const ideaOwner = "idea-after-cancel";
  await server.workspace.ensureSample(ideaOwner, server.actions);
  const idea = (await server.agent.refreshIdeas(ideaOwner)).find(
    (candidate) => candidate.kind === "document" && candidate.status === "new",
  );
  assert.ok(idea);
  const cancelled = await server.agent.createTask(
    ideaOwner,
    { kind: "document", prompt: "Complete the permission slip", input: idea.input },
    "chat-request",
  );
  await server.agent.control(ideaOwner, cancelled.id, "cancel");
  const accepted = await server.agent.decideIdea(ideaOwner, idea.id, "accept");
  assert.ok(accepted?.taskId && accepted.taskId !== cancelled.id);
  assert.equal((await server.agent.getTask(ideaOwner, accepted.taskId)).status, "queued");
});
test("task detail reads only the rows the task names, not the whole workspace", async () => {
  // A workspace keeps every run event, artifact and file it has ever produced, so detail() — which
  // the app calls once per task card — must return the rows the task names and read nothing else.
  const noiseOwner = "detail-scope";
  const created = await server.agent.createTask(noiseOwner, {
    kind: "agent",
    title: "Scoped detail",
    prompt: "Answer from the workspace",
    input: {},
  });
  const now = new Date().toISOString();
  await db.put(noiseOwner, "files", {
    id: "detail-file",
    name: "detail.pdf",
    mimeType: "application/pdf",
    size: 10,
    pageCount: 1,
    url: "",
    createdAt: now,
    source: "Gmail",
  });
  await db.put(noiseOwner, "browsers", {
    id: "detail-browser",
    title: "Example",
    url: "https://example.com",
    status: "active",
    updatedAt: now,
  });
  await db.put(noiseOwner, "run-events", {
    id: "detail-event",
    taskId: created.id,
    date: now,
    kind: "step",
    title: "Named",
    detail: "",
  });
  await db.put(noiseOwner, "agent-artifacts", {
    id: "detail-artifact",
    taskId: created.id,
    kind: "report",
    title: "Named",
    summary: "",
    data: {},
    createdAt: now,
  });
  await db.put(noiseOwner, "tasks", {
    ...created,
    artifactIds: ["detail-file"],
    state: { ...created.state, browserId: "detail-browser" },
  });
  for (let index = 0; index < 200; index++) {
    await db.put(noiseOwner, "run-events", {
      id: `noise-event-${index}`,
      taskId: `other-task-${index}`,
      date: now,
      kind: "step",
      title: "Noise",
      detail: "x".repeat(1000),
    });
    await db.put(noiseOwner, "agent-artifacts", {
      id: `noise-artifact-${index}`,
      taskId: `other-task-${index}`,
      kind: "report",
      title: "Noise",
      summary: "",
      data: {},
      createdAt: now,
    });
    await db.put(noiseOwner, "files", {
      id: `noise-file-${index}`,
      name: `noise-${index}.pdf`,
      mimeType: "application/pdf",
      size: 10,
      pageCount: 1,
      url: "",
      createdAt: now,
      source: "Gmail",
    });
  }

  // Count every row the store hands back while detail() runs, and put the store back afterwards.
  const store = db as unknown as Record<string, (...args: unknown[]) => Promise<unknown>>;
  const wrapped: string[] = [];
  let rows = 0;
  for (const name of Object.getOwnPropertyNames(Object.getPrototypeOf(db))) {
    if (
      /^(put|remove|close|compareAndSwap|insertIfAbsent|updateCredential|recoverInterrupted)/.test(
        name,
      )
    )
      continue;
    const original = store[name].bind(db);
    store[name] = async (...args: unknown[]) => {
      const result = await original(...args);
      rows += Array.isArray(result) ? result.length : result ? 1 : 0;
      return result;
    };
    wrapped.push(name);
  }
  let detail: Awaited<ReturnType<typeof server.agent.detail>>;
  try {
    detail = await server.agent.detail(noiseOwner, created.id);
  } finally {
    for (const name of wrapped) delete store[name];
  }

  assert.equal(detail.task.id, created.id);
  assert.deepEqual(
    detail.files.map((file) => file.id),
    ["detail-file"],
  );
  assert.deepEqual(
    detail.browsers.map((browser) => browser.id),
    ["detail-browser"],
  );
  assert.deepEqual(
    detail.events.map((event) => event.id),
    ["detail-event"],
  );
  assert.deepEqual(
    detail.artifacts.map((artifact) => artifact.id),
    ["detail-artifact"],
  );
  assert.ok(rows < 20, `detail() read ${rows} rows to show one task`);
});