import assert from "node:assert/strict"; import { test } from "node:test"; import { buildGraph } from "../code-index/graph.ts"; import type { ChangeSet } from "../contract/views.ts"; import { BUILTIN_AGENTS, mentionedHandles } from "../agents.ts"; import { changedSymbols, consumeDeadAgentRuns, listAgentRuns } from "../agent-runs.ts"; import { mentionStates } from "../mentions.ts"; import { d1, migrated } from "./d1.ts"; test("an agent mention counts only outside code and as a whole handle", () => { assert.deepEqual(mentionedHandles("@reviewer please look, and @security too"), ["reviewer", "security"]); assert.deepEqual(mentionedHandles("On pull request #3:\n\nthanks, @reviewer."), ["reviewer"]); assert.deepEqual(mentionedHandles("see `@reviewer` in the docs"), []); assert.deepEqual(mentionedHandles("```\n@reviewer\n```"), []); assert.deepEqual(mentionedHandles("mail ops@reviewer.dev"), []); assert.deepEqual(mentionedHandles("@reviewer/x and @agent/abc and @all"), []); }); test("each built-in agent has a handle that a mention finds", () => { for (const agent of BUILTIN_AGENTS) assert.deepEqual(mentionedHandles(`@${agent.handle} look`), [agent.handle]); assert.equal(new Set(BUILTIN_AGENTS.map((agent) => agent.handle)).size, BUILTIN_AGENTS.length); }); test("the changed declarations list each touched symbol with how often it is used", () => { const lib = ["export function total(items: number[]) {", " return items.reduce((a, b) => a + b, 0);", "}", "", "export function unused() {", " return 1;", "}"].join("\n"); const app = ['import { total } from "./lib.ts";', "export function run() {", " return total([1, 2]);", "}"].join("\n"); const graph = buildGraph("abc", [{ path: "lib.ts", text: lib }, { path: "app.ts", text: app }], [], 0); const before = lib.replace("a + b", "b + a"); const changes: ChangeSet = { base: "base", head: "abc", commits: [], files: [{ path: "lib.ts", status: "modified", kind: "text", additions: 1, deletions: 1, before, after: lib, truncated: false }], additions: 1, deletions: 1, truncated: false, }; const found = changedSymbols(changes, graph); assert.equal(found.length, 1); assert.match(found[0], /^lib\.ts#total \(function, exported\) lib\.ts:1-3 · used 1 times$/); }); test("the run list shows who started each run, where it stands, and the steps of a working run", async () => { const sqlite = migrated(); 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', '', 0); INSERT INTO repos (id, namespace_id, name, artifacts_name, default_branch, visibility, description, created_at, creator_id) VALUES ('r1', 'ns', 'source', 'a', 'main', 'private', '', 0, 'u1'); INSERT INTO agent_sessions (id, token_hash, user_id, repo_id, runtime, status, created_at, expires_at, last_seen_at) VALUES ('s1', 'h1', 'u1', 'r1', 'refs:reviewer', 'running', 10, 99999, 10), ('s2', 'h2', 'u1', 'r1', 'refs:security', 'finished', 5, 99999, 5), ('s3', 'h3', 'u1', 'r1', 'claude-code', 'running', 1, 99999, 1); UPDATE agent_sessions SET ended_at = 8 WHERE id = 's2'; INSERT INTO events (stream, kind, data, actor_kind, actor_id, session_id, created_at) VALUES ('repo:r1', 'agent.mentioned', '{"session":"s1","pull":2,"comment":7}', 'human', 'u1', NULL, 10), ('repo:r1', 'agent.mentioned', '{"session":"s2","pull":3,"comment":null}', 'human', 'u1', NULL, 5), ('repo:r1', 'agent.step', '{"session":"s1","step":1,"thinking":"Read the diff first.","tools":["read_file(a.ts)"]}', 'agent', 's1', 's1', 11), ('repo:r1', 'agent.run_finished', '{"session":"s2","ok":true,"summary":"No real problem."}', 'agent', 's2', 's2', 8);`); const runs = await listAgentRuns(d1(sqlite), "r1", null); assert.deepEqual(runs.map((run) => [run.handle, run.state, run.pull, run.via, run.summary]), [["reviewer", "working", 2, "mention", null], ["security", "answered", 3, "ci", "No real problem."]]); assert.deepEqual(runs[0].steps, [{ step: 1, at: 11, thinking: "Read the diff first.", tools: ["read_file(a.ts)"] }]); assert.equal(runs[1].steps, null); assert.deepEqual((await listAgentRuns(d1(sqlite), "r1", "s2"))[1].steps, []); }); test("a dead run is marked failed once, and a run that already ended stays as it is", async () => { const sqlite = migrated(); 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', '', 0); INSERT INTO repos (id, namespace_id, name, artifacts_name, default_branch, visibility, description, created_at, creator_id) VALUES ('r1', 'ns', 'source', 'a', 'main', 'private', '', 0, 'u1'); INSERT INTO agent_sessions (id, token_hash, user_id, repo_id, runtime, status, created_at, expires_at, last_seen_at) VALUES ('s1', 'h1', 'u1', 'r1', 'refs:reviewer', 'running', 10, 99999, 10), ('s2', 'h2', 'u1', 'r1', 'refs:reviewer', 'finished', 10, 99999, 10); UPDATE agent_sessions SET ended_at = 20 WHERE id = 's2';`); const acked: string[] = []; const batch = (session: string) => ({ messages: [{ body: { session, mention: 5 }, ack: () => acked.push(session) }] }) as unknown as MessageBatch<{ session: string; mention: number }>; const env = { DB: d1(sqlite) } as unknown as Env; await consumeDeadAgentRuns(batch("s1"), env); await consumeDeadAgentRuns(batch("s1"), env); await consumeDeadAgentRuns(batch("s2"), env); assert.deepEqual(acked, ["s1", "s1", "s2"]); assert.equal((sqlite.prepare("SELECT status FROM agent_sessions WHERE id = 's1'").get() as { status: string }).status, "failed"); const finished = sqlite.prepare("SELECT session_id, json_extract(data, '$.ok') AS ok, json_extract(data, '$.mention') AS mention FROM events WHERE kind = 'agent.run_finished'").all(); assert.deepEqual(finished.map((row) => ({ ...row })), [{ session_id: "s1", ok: 0, mention: "5" }]); }); test("a hosted agent's mention shows its handle and its latest step, and a connected agent's mention does not", async () => { const sqlite = migrated(); 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', '', 0); INSERT INTO repos (id, namespace_id, name, artifacts_name, default_branch, visibility, description, created_at, creator_id) VALUES ('r1', 'ns', 'source', 'a', 'main', 'private', '', 0, 'u1'); INSERT INTO agent_sessions (id, token_hash, user_id, repo_id, runtime, status, created_at, expires_at, last_seen_at) VALUES ('s1', 'h1', 'u1', 'r1', 'refs:security', 'running', 10, 99999, 10), ('s3', 'h3', 'u1', 'r1', 'claude-code', 'running', 1, 99999, 1); INSERT INTO events (stream, kind, data, actor_kind, actor_id, session_id, created_at) VALUES ('repo:r1', 'agent.mentioned', '{"session":"s1","pull":2,"comment":7,"run":true}', 'human', 'u1', NULL, 10), ('repo:r1', 'agent.mentioned', '{"session":"s3","pull":2,"comment":7}', 'human', 'u1', NULL, 10), ('repo:r1', 'agent.step', '{"session":"s1","step":1,"thinking":"","tools":["read_file(a.ts)"]}', 'agent', 's1', 's1', 11), ('repo:r1', 'agent.step', '{"session":"s1","step":2,"thinking":"","tools":["symbol_links(x)","read_file(b.ts)"]}', 'agent', 's1', 's1', 12);`); const [hosted, connected] = await mentionStates(d1(sqlite), "r1", 2); assert.equal(hosted.agent, "@security"); assert.equal(hosted.state, "working"); assert.deepEqual(hosted.hosted, { handle: "security", session: "s1", startedAt: 10, lastStep: { step: 2, tools: ["symbol_links(x)", "read_file(b.ts)"], at: 12 } }); assert.equal(connected.hosted, null); assert.equal(connected.agent, "claude-code · Aman · s3"); });