import assert from "node:assert/strict"; import { test } from "node:test"; import type { Actor } from "../contract/actor.ts"; import { workKey } from "../analytics.ts"; import { deliverMentions, onPull } from "../mentions.ts"; import { d1, migrated } from "./d1.ts"; const SESSION = "11111111-2222-3333-4444-555555555555"; function setup() { const sqlite = migrated(); const at = Math.floor(Date.now() / 1000); sqlite.exec(`INSERT INTO namespaces (id, slug, name, kind, created_at) VALUES ('ns', 'refs', 'refs', 'org', 0); INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@x', 'Aman', 'x', 0); INSERT INTO memberships (namespace_id, user_id, role, created_at) VALUES ('ns', 'u1', 'owner', 0)`); sqlite.exec("INSERT INTO repos (id, namespace_id, name, visibility, artifacts_name, creator_id, created_at) VALUES ('repo', 'ns', 'refs', 'private', 'refs', 'u1', 0)"); sqlite.prepare("INSERT INTO agent_sessions (id, token_hash, user_id, repo_id, runtime, created_at, expires_at, mcp_session) VALUES (?, 'h', 'u1', 'repo', 'codex', ?, ?, 'mcp')").run(SESSION, at, at + 3600); const env = { DB: d1(sqlite), POSTHOG_ENABLED: "true", POSTHOG_PROJECT_TOKEN: "project-token", POSTHOG_HOST: "https://us.i.posthog.com" } as unknown as Env; const pending: Promise[] = []; const ctx = { waitUntil: (promise: Promise) => void pending.push(promise) }; return { env, ctx, pending }; } const pull = onPull({ repo_id: "repo", number: 2, title: "Private title", head_ref: "private-branch" }); const human: Actor = { kind: "human", person: { id: "u1", name: "Aman", email: "a@x" }, via: "session" }; test("a person directing a live agent is captured once per mention, without the mention text", async (t) => { const payloads: { event: string; distinct_id: string; properties: Record }[] = []; t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { payloads.push(JSON.parse(String(init.body))); return new Response(null, { status: 204 }); }); const { env, ctx, pending } = setup(); await deliverMentions(env, ctx, human, pull, `@agent/${SESSION} private instruction`, 3, { thread: 5 }); await deliverMentions(env, ctx, human, pull, "no agent named here", 4); await Promise.all(pending); assert.equal(payloads.length, 1); const { event, distinct_id, properties } = payloads[0]; assert.deepEqual({ event, distinct_id }, { event: "agent.directed", distinct_id: "human:u1" }); assert.deepEqual({ channel: properties.channel, assigned: properties.assigned, task_number: properties.task_number, $groups: properties.$groups }, { channel: "thread", assigned: false, task_number: undefined, $groups: { workspace: "ns" } }); assert.equal(properties.work_key, await workKey("repo", "private-branch")); assert.match(String(properties.work_key), /^[0-9a-f]{16}$/); assert.doesNotMatch(JSON.stringify(payloads), /private instruction|Private title|private-branch|Aman|a@x/); }); test("an agent mentioning another agent is not human direction", async (t) => { const fetch = t.mock.method(globalThis, "fetch", async () => new Response(null, { status: 204 })); const { env, ctx, pending } = setup(); const agent: Actor = { kind: "agent", agent: { id: "other", name: "codex", runtime: "codex" }, onBehalfOf: human.person, sessionId: "other", grant: { repos: "all", org: "none" } }; await deliverMentions(env, ctx, agent, pull, `@agent/${SESSION} look`, null); await Promise.all(pending); assert.equal(fetch.mock.callCount(), 0); });