Skip to content
refs
History
98 lines · 7.8 KBRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { buildGraph } from "../code-index/graph.ts";
4import type { ChangeSet } from "../contract/views.ts";
5import { BUILTIN_AGENTS, mentionedHandles } from "../agents.ts";
6import { changedSymbols, consumeDeadAgentRuns, listAgentRuns } from "../agent-runs.ts";
7import { mentionStates } from "../mentions.ts";
8import { d1, migrated } from "./d1.ts";
9
10test("an agent mention counts only outside code and as a whole handle", () => {
11 assert.deepEqual(mentionedHandles("@reviewer please look, and @security too"), ["reviewer", "security"]);
12 assert.deepEqual(mentionedHandles("On pull request #3:\n\nthanks, @reviewer."), ["reviewer"]);
13 assert.deepEqual(mentionedHandles("see `@reviewer` in the docs"), []);
14 assert.deepEqual(mentionedHandles("```\n@reviewer\n```"), []);
15 assert.deepEqual(mentionedHandles("mail ops@reviewer.dev"), []);
16 assert.deepEqual(mentionedHandles("@reviewer/x and @agent/abc and @all"), []);
17});
18
19test("each built-in agent has a handle that a mention finds", () => {
20 for (const agent of BUILTIN_AGENTS) assert.deepEqual(mentionedHandles(`@${agent.handle} look`), [agent.handle]);
21 assert.equal(new Set(BUILTIN_AGENTS.map((agent) => agent.handle)).size, BUILTIN_AGENTS.length);
22});
23
24test("the changed declarations list each touched symbol with how often it is used", () => {
25 const lib = ["export function total(items: number[]) {", " return items.reduce((a, b) => a + b, 0);", "}", "", "export function unused() {", " return 1;", "}"].join("\n");
26 const app = ['import { total } from "./lib.ts";', "export function run() {", " return total([1, 2]);", "}"].join("\n");
27 const graph = buildGraph("abc", [{ path: "lib.ts", text: lib }, { path: "app.ts", text: app }], [], 0);
28 const before = lib.replace("a + b", "b + a");
29 const changes: ChangeSet = {
30 base: "base",
31 head: "abc",
32 commits: [],
33 files: [{ path: "lib.ts", status: "modified", kind: "text", additions: 1, deletions: 1, before, after: lib, truncated: false }],
34 additions: 1,
35 deletions: 1,
36 truncated: false,
37 };
38 const found = changedSymbols(changes, graph);
39 assert.equal(found.length, 1);
40 assert.match(found[0], /^lib\.ts#total \(function, exported\) lib\.ts:1-3 · used 1 times$/);
41});
42
43test("the run list shows who started each run, where it stands, and the steps of a working run", async () => {
44 const sqlite = migrated();
45 sqlite.exec(`INSERT INTO namespaces (id, slug, name, kind, created_at) VALUES ('ns', 'refs', 'refs', 'org', 0);
46 INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@x', 'Aman', '', 0);
47 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');
48 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);
49 UPDATE agent_sessions SET ended_at = 8 WHERE id = 's2';
50 INSERT INTO events (stream, kind, data, actor_kind, actor_id, session_id, created_at) VALUES
51 ('repo:r1', 'agent.mentioned', '{"session":"s1","pull":2,"comment":7}', 'human', 'u1', NULL, 10),
52 ('repo:r1', 'agent.mentioned', '{"session":"s2","pull":3,"comment":null}', 'human', 'u1', NULL, 5),
53 ('repo:r1', 'agent.step', '{"session":"s1","step":1,"thinking":"Read the diff first.","tools":["read_file(a.ts)"]}', 'agent', 's1', 's1', 11),
54 ('repo:r1', 'agent.run_finished', '{"session":"s2","ok":true,"summary":"No real problem."}', 'agent', 's2', 's2', 8);`);
55 const runs = await listAgentRuns(d1(sqlite), "r1", null);
56 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."]]);
57 assert.deepEqual(runs[0].steps, [{ step: 1, at: 11, thinking: "Read the diff first.", tools: ["read_file(a.ts)"] }]);
58 assert.equal(runs[1].steps, null);
59 assert.deepEqual((await listAgentRuns(d1(sqlite), "r1", "s2"))[1].steps, []);
60});
61
62test("a dead run is marked failed once, and a run that already ended stays as it is", async () => {
63 const sqlite = migrated();
64 sqlite.exec(`INSERT INTO namespaces (id, slug, name, kind, created_at) VALUES ('ns', 'refs', 'refs', 'org', 0);
65 INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@x', 'Aman', '', 0);
66 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');
67 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);
68 UPDATE agent_sessions SET ended_at = 20 WHERE id = 's2';`);
69 const acked: string[] = [];
70 const batch = (session: string) => ({ messages: [{ body: { session, mention: 5 }, ack: () => acked.push(session) }] }) as unknown as MessageBatch<{ session: string; mention: number }>;
71 const env = { DB: d1(sqlite) } as unknown as Env;
72 await consumeDeadAgentRuns(batch("s1"), env);
73 await consumeDeadAgentRuns(batch("s1"), env);
74 await consumeDeadAgentRuns(batch("s2"), env);
75 assert.deepEqual(acked, ["s1", "s1", "s2"]);
76 assert.equal((sqlite.prepare("SELECT status FROM agent_sessions WHERE id = 's1'").get() as { status: string }).status, "failed");
77 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();
78 assert.deepEqual(finished.map((row) => ({ ...row })), [{ session_id: "s1", ok: 0, mention: "5" }]);
79});
80
81test("a hosted agent's mention shows its handle and its latest step, and a connected agent's mention does not", async () => {
82 const sqlite = migrated();
83 sqlite.exec(`INSERT INTO namespaces (id, slug, name, kind, created_at) VALUES ('ns', 'refs', 'refs', 'org', 0);
84 INSERT INTO users (id, email, name, password_hash, created_at) VALUES ('u1', 'a@x', 'Aman', '', 0);
85 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');
86 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);
87 INSERT INTO events (stream, kind, data, actor_kind, actor_id, session_id, created_at) VALUES
88 ('repo:r1', 'agent.mentioned', '{"session":"s1","pull":2,"comment":7,"run":true}', 'human', 'u1', NULL, 10),
89 ('repo:r1', 'agent.mentioned', '{"session":"s3","pull":2,"comment":7}', 'human', 'u1', NULL, 10),
90 ('repo:r1', 'agent.step', '{"session":"s1","step":1,"thinking":"","tools":["read_file(a.ts)"]}', 'agent', 's1', 's1', 11),
91 ('repo:r1', 'agent.step', '{"session":"s1","step":2,"thinking":"","tools":["symbol_links(x)","read_file(b.ts)"]}', 'agent', 's1', 's1', 12);`);
92 const [hosted, connected] = await mentionStates(d1(sqlite), "r1", 2);
93 assert.equal(hosted.agent, "@security");
94 assert.equal(hosted.state, "working");
95 assert.deepEqual(hosted.hosted, { handle: "security", session: "s1", startedAt: 10, lastStep: { step: 2, tools: ["symbol_links(x)", "read_file(b.ts)"], at: 12 } });
96 assert.equal(connected.hosted, null);
97 assert.equal(connected.agent, "claude-code · Aman · s3");
98});