Skip to content

abso-core

Public

abso is version control for agents: the client, the engine and a server you can run on your own Cloudflare account.

Check passed
MCP
code on this site's /mcp
CLI
none: the JSON below is the answer
JSON
/christopher/abso-core/files/client/test/issues.test.ts?version=96&format=json
477 linespinch 66% · First import 28% · devops 3% · infra-split 3% · review 1%Last change pinch in version 89, 9 versions changed it

You're reading version 96. The newest is 318, and 4 versions since then changed this file. Go to the newestCompare with 318

1// The Issues commands (#54) as far as the client alone decides them: which
2// Issues Worker a folder talks to, whether the token may go there, the MCP
3// tools, and the usage mistakes refused before any call. The Issues server
4// here is a stand-in that only counts calls; the commands against the real
5// store are issues/test/client-commands.test.ts, on the platform side (abso
6// issue 141).
7
8import assert from "node:assert/strict";
9import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
10import { createServer, type Server } from "node:http";
11import type { AddressInfo } from "node:net";
12import { tmpdir } from "node:os";
13import { join } from "node:path";
14import { after, before, describe, it } from "node:test";
15import { readConfig, readMachineServers } from "../src/config.ts";
16import { issuesServerFor, tokenGoesTo } from "../src/core.ts";
17import { issuePage, searchLines } from "../src/issues.ts";
18import type { IssuePage } from "../src/issues-wire.ts";
19import { main } from "../src/main.ts";
20import { argvFor, mcpTools } from "../src/mcp.ts";
21import { type Fake, startFake } from "./fake-server.ts";
23const TOKEN = "test-token-not-secret";
25let fake: Fake;
26let issues: Server;
27let issuesUrl: string;
28/** Every call the stand-in Issues server took. */
29const seen: string[] = [];
30/** The bodies of the copy requests it took. */
31const copies: Record<string, unknown>[] = [];
32/** The bodies of the replies it took. */
33const replies: { text: string; question?: boolean }[] = [];
34/** An open ask of fay's from desk with a thread: fay's ack, then a question back, so it waits on desk. */
35const threaded = {
36 id: 7,
37 issue: 3,
38 title: "Sync is slow",
39 by: "desk",
40 of: "fay",
41 kind: "question",
42 question: "Which way?",
43 review: false,
44 due: null,
45 recommendation: null,
46 options: [],
47 blocks: [],
48 at: 0,
49 answer: null,
50 seen: 0,
51 ack: { at: 0, note: "ok, let me check" },
52 replies: [
53 { id: 1, by: "fay", text: "ok, let me check", at: 0 },
54 { id: 2, by: "fay", text: "staging or live?\nlive is 906", at: 0, question: true },
55 ],
56 waitingOn: "desk",
57};
58const dirs: string[] = [];
60before(async () => {
61 fake = await startFake(0, TOKEN);
62 issues = createServer((req, res) => {
63 seen.push(`${req.method} ${req.url}`);
64 const send = (status: number, body: unknown) =>
65 res.writeHead(status, { "content-type": "application/json" }).end(JSON.stringify(body));
66 if (req.method === "POST" && /\/copy$/.test(req.url ?? "")) {
67 let raw = "";
68 req.on("data", (c: Buffer) => (raw += c.toString("utf8")));
69 req.on("end", () => {
70 const body = JSON.parse(raw) as Record<string, unknown>;
71 copies.push(body);
72 // Issue 403 stands for a live server refusing an agent's copy.
73 if (body.issue === 403 && body.dryRun !== true)
74 return send(403, { error: "copying an issue out of its project takes an owner of that project" });
75 const skipped = { comments: [4], asks: [2] };
76 if (body.dryRun === true)
77 return send(200, {
78 dryRun: true,
79 plan: {
80 from: { project: body.from, issue: body.issue },
81 title: "Sync is slow",
82 edited: ["description"],
83 labels: ["client"],
84 closed: { how: "done" },
85 comments: [{ edited: false }, { edited: true }],
86 asks: [{ edited: false }],
87 skipped,
88 },
89 });
90 send(200, { ok: true, id: 31, from: { project: body.from, issue: body.issue }, skipped });
91 });
92 return;
93 }
94 // An ack answers with the ask, acknowledged and still open.
95 const ack = /\/a\/([^/]+)\/ack\/(\d+)$/.exec(req.url ?? "");
96 if (req.method === "POST" && ack)
97 return send(200, {
98 ask: { id: Number(ack[2]), by: "desk", of: ack[1], answer: null, ack: { at: Date.now(), note: "" } },
99 });
100 // Ask 404 stands for a server from before ask threads: neither route is there.
101 if (/\/(ask|reply)\/404$/.test(req.url ?? "")) return send(404, { error: "no such issues route" });
102 // A reply answers with the ask; from the one asked, a question makes it wait on the asker.
103 const reply = /\/a\/([^/]+)\/reply\/(\d+)$/.exec(req.url ?? "");
104 if (req.method === "POST" && reply) {
105 let raw = "";
106 req.on("data", (c: Buffer) => (raw += c.toString("utf8")));
107 req.on("end", () => {
108 const body = JSON.parse(raw) as { text: string; question?: boolean };
109 replies.push(body);
110 send(200, {
111 ask: {
112 id: Number(reply[2]),
113 by: "desk",
114 of: reply[1],
115 answer: null,
116 waitingOn: body.question ? "desk" : reply[1],
117 },
118 });
119 });
120 return;
121 }
122 // One ask with its thread, and the asks an agent sent.
123 const one = /\/ask\/(\d+)$/.exec(req.url ?? "");
124 if (req.method === "GET" && one) return send(200, { ask: { ...threaded, id: Number(one[1]) } });
125 if (req.method === "GET" && /\/a\/[^/]+\/asked$/.test(req.url ?? ""))
126 return send(200, {
127 asks: [
128 threaded,
129 { ...threaded, id: 8, replies: [], answer: { by: "fay", words: "yes", at: 0 }, waitingOn: null },
130 ],
131 });
132 send(500, { error: "a stand-in" });
133 });
134 await new Promise<void>((ok) => issues.listen(0, "127.0.0.1", ok));
135 issuesUrl = `http://127.0.0.1:${(issues.address() as AddressInfo).port}`;
136});
137after(async () => {
138 await fake.close();
139 await new Promise((ok) => issues.close(ok));
140 for (const d of dirs) rmSync(d, { recursive: true, force: true, maxRetries: 5 });
141});
143const ENV = (): Record<string, string> => ({
144 AVCS_TOKEN: TOKEN,
145 AVCS_HOOK_FORCE: "1",
146 AVCS_ISSUES_SERVER: issuesUrl,
147 AVCS_SERVERS_FILE: "no-servers.json",
148});
150async function avcs(
151 cwd: string,
152 argv: string[],
153 env: Record<string, string> = ENV(),
154): Promise<{ exit: number; out: string }> {
155 const r = await main([...argv, "--json"], { cwd, env, write: () => {} });
156 return { exit: r.exit, out: r.stdout + r.stderr };
159let projects = 0;
160async function folder(agent: string): Promise<string> {
161 const dir = mkdtempSync(join(tmpdir(), "avcs-issues-"));
162 dirs.push(dir);
163 const r = await avcs(dir, ["init", "--server", fake.url, "--project", `issues${++projects}`, "--agent", agent]);
164 assert.equal(r.exit, 0, r.out);
165 return dir;
168describe("issues commands, the client's part (#54)", () => {
169 it("sends the token only to an Issues Worker it trusts", async () => {
170 const dave = await folder("dave");
171 const before = seen.length;
172 const r = await avcs(dave, ["issues"], { ...ENV(), AVCS_ISSUES_SERVER: "https://issues.example.com" });
173 assert.equal(r.exit, 1, r.out);
174 assert.match(r.out, /sends your token only to its own servers and this machine/);
175 assert.equal(seen.length, before);
176 // A server with no Issues Worker beside it, and none named.
177 const { AVCS_ISSUES_SERVER: _, ...without } = ENV();
178 const none = await avcs(dave, ["waiting"], without);
179 assert.notEqual(none.exit, 0);
180 assert.match(
181 none.out,
182 /has no Issues Worker beside it that abso knows; ~\/\.abso\/servers\.json or ABSO_ISSUES_SERVER names one/,
183 );
184 });
186 it("finds a server's Issues Worker in this machine's servers.json, and may send it the token", async () => {
187 const dir = mkdtempSync(join(tmpdir(), "abso-servers-"));
188 dirs.push(dir);
189 const file = join(dir, "servers.json");
190 writeFileSync(file, JSON.stringify({ servers: { [fake.url]: { issues: issuesUrl } } }));
191 const erin = await folder("erin");
192 const before = seen.length;
193 const { AVCS_ISSUES_SERVER: _, ...without } = ENV();
194 await avcs(erin, ["waiting"], { ...without, AVCS_SERVERS_FILE: file });
195 assert.ok(seen.length > before, "the call went to the Issues Worker servers.json names");
196 const machine = readMachineServers({ AVCS_SERVERS_FILE: file });
197 assert.equal(issuesServerFor(fake.url, machine), issuesUrl);
198 assert.equal(issuesServerFor("https://abso.example", machine), undefined);
199 // abso's own server and its Issues Worker are built in.
200 assert.ok(tokenGoesTo("https://api.abso.dev"));
201 assert.ok(tokenGoesTo("https://issues.abso.dev"));
202 assert.equal(issuesServerFor("https://api.abso.dev/"), "https://issues.abso.dev");
203 });
205 it("are MCP tools", () => {
206 const names = mcpTools().map((t) => t.name);
207 for (const n of ["issues", "issue", "search", "ask", "waiting", "answer", "ack", "reply"])
208 assert.ok(names.includes(n), n);
209 assert.deepEqual(argvFor("ask", { args: ["alice", "Which login?"], issue: "3", review: true }), [
210 "ask",
211 "--issue=3",
212 "--review",
213 "--",
214 "alice",
215 "Which login?",
216 ]);
217 });
219 it("refuses usage mistakes before any call", async () => {
220 const erin = await folder("erin");
221 const before = seen.length;
222 for (const argv of [
223 ["issue"],
224 ["issue", "x"],
225 ["ask", "alice"],
226 ["ask", "a", "q", "--review", "--task"],
227 ["answer", "3"],
228 ["issue", "1", "--comment", "a", "--drop"],
229 ["issues", "extra"],
230 ["ask", "--withdraw", "x"],
231 ["ask", "--withdraw", "3", "alice", "q"],
232 ["ask", "--withdraw", "3", "--issue", "2"],
233 ["ask", "--withdraw", "3", "--task"],
234 ["ask", "alice", "q", "--note", "a note without --withdraw"],
235 ["search"],
236 ["search", "words", "--limit", "0"],
237 ["search", "words", "--limit", "51"],
238 ]) {
239 const r = await avcs(erin, argv);
240 assert.equal(r.exit, 1, `${argv.join(" ")}: ${r.out}`);
241 }
242 assert.equal(seen.length, before);
243 });
245 it("shows a search hit as the issue, then where it matched, without the match markers", () => {
246 const issue = { id: 12, title: "Sync is slow", status: "open" as const, updated: 0, labels: [] };
247 assert.deepEqual(
248 searchLines({ issue, where: "comment", comment: 40, snippet: "the \u0001network\u0002 drops mid-sync" }),
249 [' 12 open "Sync is slow"', ' in comment 40: "the network drops mid-sync"'],
250 );
251 assert.deepEqual(searchLines({ issue, where: "description", snippet: "Pages load late.", meaning: true }), [
252 ' 12 open "Sync is slow"',
253 ' by meaning: "Pages load late."',
254 ]);
255 assert.deepEqual(searchLines({ issue, where: "title", snippet: "Sync is slow" })[1], " in the title");
256 });
258 it("withdraw is one POST to the asker's own withdraw/<ask> route", async () => {
259 const erin = await folder("erin");
260 const before = seen.length;
261 await avcs(erin, ["ask", "--withdraw", "#3", "--note", "overtaken by later decisions"]);
262 const posts = seen.slice(before).filter((s) => s.startsWith("POST"));
263 assert.equal(posts.length, 1, posts.join("\n"));
264 assert.match(posts[0]!, /^POST \/p\/issues\d+\/a\/erin\/withdraw\/3$/);
265 });
267 it("ack is one POST to the one asked's ack/<ask> route, and says the ask stays open", async () => {
268 const fay = await folder("fay");
269 const before = seen.length;
270 const r = await main(["ack", "7", "on it after the deploy"], { cwd: fay, env: ENV(), write: () => {} });
271 assert.equal(r.exit, 0, r.stdout + r.stderr);
272 assert.equal(
273 r.stdout,
274 'Acknowledged ask 7. desk hears it; abso reply 7 "..." adds more as you go, and the ask stays open until abso answer 7 "...".\n',
275 );
276 const posts = seen.slice(before).filter((s) => s.startsWith("POST"));
277 assert.deepEqual(posts.length, 1, posts.join("\n"));
278 assert.match(posts[0]!, /^POST \/p\/issues\d+\/a\/fay\/ack\/7$/);
279 assert.equal((await main(["ack"], { cwd: fay, env: ENV(), write: () => {} })).exit, 1);
280 });
282 it("reply is one POST to reply/<ask> with the words, and says whose turn it is", async () => {
283 const fay = await folder("fay");
284 const before = seen.length;
285 const r = await main(["reply", "7", "staging or live?", "--question"], { cwd: fay, env: ENV(), write: () => {} });
286 assert.equal(r.exit, 0, r.stdout + r.stderr);
287 const posts = seen.slice(before).filter((s) => s.startsWith("POST"));
288 assert.equal(posts.length, 1, posts.join("\n"));
289 assert.match(posts[0]!, /^POST \/p\/issues\d+\/a\/fay\/reply\/7$/);
290 assert.deepEqual(replies.at(-1), { text: "staging or live?", question: true });
291 assert.equal(r.stdout, "Replied on ask 7. desk hears it. It waits on desk.\n");
292 assert.equal((await main(["reply", "7"], { cwd: fay, env: ENV(), write: () => {} })).exit, 1);
293 });
295 it("names a server from before ask threads instead of a bare 404", async () => {
296 const fay = await folder("fay");
297 for (const argv of [
298 ["reply", "404", "still there?"],
299 ["ask", "404"],
300 ]) {
301 const r = await main(argv, { cwd: fay, env: ENV(), write: () => {} });
302 assert.equal(r.exit, 1, argv.join(" "));
303 assert.match(r.stderr, /doesn't have ask threads yet/, argv.join(" "));
304 assert.doesNotMatch(r.stderr, /no such issues route/, argv.join(" "));
305 }
306 });
308 it("ask <N> reads one ask with its whole thread, oldest first, and whose turn it is", async () => {
309 const fay = await folder("fay");
310 const before = seen.length;
311 const r = await main(["ask", "7"], { cwd: fay, env: ENV(), write: () => {} });
312 assert.equal(r.exit, 0, r.stdout + r.stderr);
313 assert.match(seen.slice(before).join("\n"), /^GET \/p\/issues\d+\/ask\/7$/m);
314 assert.match(
315 r.stdout,
316 /^ {2}ask 7: desk, .* asks fay a question on issue 3 \("Sync is slow"\) \(2 replies; waiting on desk\): "Which way\?"$/m,
317 );
318 const ack = r.stdout.indexOf(' fay, Jan 1 1970 00:00 UTC: "ok, let me check"');
319 const back = r.stdout.indexOf(' fay, Jan 1 1970 00:00 UTC, a question: "staging or live?\\nlive is 906"');
320 assert.ok(ack > 0 && back > ack, r.stdout);
321 assert.match(
322 r.stdout,
323 /^It waits on desk\. abso reply 7 "\.\.\." adds to it; abso answer 7 "\.\.\." answers and closes it\.$/m,
324 );
325 // The asker reads it as their turn, and can't answer it.
326 const desk = await folder("desk");
327 const d = await main(["ask", "7"], { cwd: desk, env: ENV(), write: () => {} });
328 assert.match(d.stdout, /^It waits on you\. abso reply 7 "\.\.\." adds to it\.$/m);
329 });
331 it("waiting --asked lists the asks you sent, open first, then the latest answered", async () => {
332 const fay = await folder("fay");
333 const before = seen.length;
334 const r = await main(["waiting", "--asked"], { cwd: fay, env: ENV(), write: () => {} });
335 assert.equal(r.exit, 0, r.stdout + r.stderr);
336 assert.match(seen.slice(before).join("\n"), /^GET \/p\/issues\d+\/a\/fay\/asked$/m);
337 assert.match(r.stdout, /^1 open ask you sent:$/m);
338 assert.match(r.stdout, /^ {4}last: fay, .*, a question: "staging or live\? \.\.\."$/m);
339 assert.ok(r.stdout.indexOf("ask 7:") < r.stdout.indexOf("The latest answered:"), r.stdout);
340 assert.ok(r.stdout.indexOf("The latest answered:") < r.stdout.indexOf("ask 8:"), r.stdout);
341 assert.match(r.stdout, /^ {4}answer: "yes"$/m);
342 });
344 it("an acknowledged ask reads as acknowledged, not answered, with the note under it", () => {
345 const ask = {
346 id: 7,
347 issue: 3,
348 by: "desk",
349 of: "fay",
350 kind: "question",
351 question: "Which way?",
352 review: false,
353 due: null,
354 recommendation: null,
355 options: [],
356 blocks: [],
357 at: 0,
358 answer: null,
359 seen: 0,
360 ack: { at: 0, note: "on it after the deploy" },
361 };
362 const page = {
363 issue: {
364 id: 3,
365 title: "t",
366 status: "blocked",
367 parent: null,
368 opened: { by: "desk", at: 0 },
369 agent: null,
370 agents: [],
371 blockedBy: [],
372 labels: [],
373 versions: [],
374 waitingOn: ["fay"],
375 closed: null,
376 description: "",
377 lines: null,
378 watchers: [],
379 },
380 comments: [],
381 edits: [],
382 asks: [ask],
383 claims: [],
384 children: [],
385 } as unknown as IssuePage;
386 const text = issuePage(page).join("\n");
387 assert.match(
388 text,
389 /\n {2}ask 7: desk, [^\n]*asks fay a question on issue 3 \(acknowledged [^;)]+, not answered yet\): "Which way\?"\n {4}fay acknowledged: "on it after the deploy"/,
390 );
391 assert.doesNotMatch(text, /answer:/);
392 const answered = { ...ask, answer: { by: "fay", words: "This way", at: 0 } };
393 const later = issuePage({ ...page, asks: [answered] } as unknown as IssuePage).join("\n");
394 assert.doesNotMatch(later, /acknowledged/, "an answer replaces the ack");
395 });
396});
398describe("abso issue <N> --copy-to <project>", () => {
399 const text = async (cwd: string, argv: string[]) => {
400 const r = await main(argv, { cwd, env: ENV(), write: () => {} });
401 return { exit: r.exit, out: r.stdout + r.stderr };
402 };
404 it("a dry run asks the other project's copy route for the plan, and says nothing was written", async () => {
405 const gil = await folder("gil");
406 const before = seen.length;
407 const r = await text(gil, ["issue", "12", "--copy-to", "core", "--dry-run"]);
408 assert.equal(r.exit, 0, r.out);
409 assert.deepEqual(seen.slice(before), ["POST /p/core/a/gil/copy"]);
410 const from = (copies.at(-1) as { from: string }).from;
411 assert.match(from, /^issues\d+$/);
412 assert.deepEqual(copies.at(-1), { from, issue: 12, dryRun: true });
413 assert.equal(
414 r.out.trimEnd(),
415 [
416 `A dry run: nothing was written. Copying ${from} issue 12 into core would make:`,
417 ` "Sync is slow", done (description edited when copied)`,
418 " 2 comments (1 edited), 1 answered ask, labelled client",
419 " not copied: comments 4; asks 2 (open asks, and what --clean left out)",
420 "Run it again without --dry-run to copy it.",
421 ].join("\n"),
422 );
423 });
425 it("copies with the cleaned texts --clean gives, by source id", async () => {
426 const gil = await folder("gil");
427 writeFileSync(
428 join(gil, "clean.json"),
429 JSON.stringify({ description: "Cleaned.", comments: [{ id: 1 }, { id: 3, text: "Cleaned too." }] }),
430 );
431 const r = await text(gil, ["issue", "12", "--copy-to", "core", "--clean", "clean.json"]);
432 assert.equal(r.exit, 0, r.out);
433 const from = (copies.at(-1) as { from: string }).from;
434 assert.deepEqual(copies.at(-1), {
435 description: "Cleaned.",
436 comments: [{ id: 1 }, { id: 3, text: "Cleaned too." }],
437 from,
438 issue: 12,
439 });
440 assert.match(
441 r.out,
442 new RegExp(`^Copied ${from} issue 12 to core issue 31\\. Its comments and asks keep their authors`),
443 );
444 });
446 it("says a dry run still works when the server refuses the copy", async () => {
447 const gil = await folder("gil");
448 const r = await text(gil, ["issue", "403", "--copy-to", "core"]);
449 assert.equal(r.exit, 1, r.out);
450 assert.match(r.out, /takes an owner of that project\. --dry-run still shows what would go\./);
451 });
453 it("refuses mistakes before any call", async () => {
454 const gil = await folder("gil");
455 writeFileSync(join(gil, "bad.json"), JSON.stringify({ from: "elsewhere" }));
456 const before = seen.length;
457 for (const [argv, why] of [
458 [["issue", "12", "--copy-to", "core", "--comment", "x"], /--copy-to goes alone .* not with --comment/],
459 [["issue", "12", "--dry-run"], /--clean and --dry-run go with --copy-to <project>/],
460 [["issue", "12", "--copy-to", "core", "--clean", "nope.json"], /--clean nope\.json: no such file/],
461 [
462 ["issue", "12", "--copy-to", "core", "--clean", "bad.json"],
463 /from isn't one of title, description, comments, asks/,
464 ],
465 ] as const) {
466 const r = await text(gil, [...argv]);
467 assert.notEqual(r.exit, 0, argv.join(" "));
468 assert.match(r.out, why);
469 }
470 const { project } = readConfig(gil);
471 assert.match(
472 (await text(gil, ["issue", "12", "--copy-to", project])).out,
473 /issue 12 is already in issues\d+; --copy-to names another project/,
474 );
475 assert.equal(seen.length, before, "nothing reached the Issues server");
476 });
477});