- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/issues.ts?version=0&format=json
446 linesFirst import 100%Last change First import in version 0, 1 version changed it
You're reading version 0. The newest is 318, and 21 versions since then changed this file.
1// Issues (#54): avcs issues, avcs issue, avcs ask, avcs waiting and avcs
2// answer. The Issues Worker keeps them (avcs-issues beside avcs, or
3// AVCS_ISSUES_SERVER), and takes the same token. Statuses come from the work: a start note naming
4// an issue (avcs start --issue N) puts it In progress, an open ask Blocked or
5// In review, and publish --done closes it. Other agents' and people's words
6// are quoted as JSON, so none can print as another line (#30).
8import type { Api, Ask, Draft, DraftBody, IssuePage, IssueRow } from "./api.ts";
9import { bool, type Parsed, str, UsageError } from "./args.ts";
10import { type Env, type Output, open, plural } from "./commands.ts";
11import { type Config, readMachineServers, trustedServers } from "./config.ts";
12import { issuesServerFor, tokenGoesTo } from "./core.ts";
13import { seenText, shortWhen } from "./times.ts";
15const q = (s: string): string => JSON.stringify(s);
17/** The folder, with its Api pointed at the Issues Worker: the token goes only where tokenGoesTo allows, as for the avcs server. */
18function openIssues(env: Env): { config: Config; api: Api } {
19 const { config, api } = open(env);
20 const server = env.env.AVCS_ISSUES_SERVER || issuesServerFor(config.server, readMachineServers(env.env));
21 if (server === undefined)
22 throw new UsageError(
23 `${config.server} has no Issues Worker beside it that abso knows; ~/.avcs/servers.json or AVCS_ISSUES_SERVER names one`,
24 );
25 if (config.token !== undefined && !tokenGoesTo(server, trustedServers(env.env))) {
26 throw new UsageError(
27 `the Issues Worker is ${server}, and avcs sends your token only to its own servers and this machine. If ${server} is yours, name it in ~/.avcs/servers.json or AVCS_TRUSTED_SERVERS.`,
28 );
29 }
30 api.issuesServer = server;
31 return { config, api };
32}
34function issueNumber(word: string | undefined, what = "an issue number"): number {
35 if (word === undefined || !/^#?\d+$/.test(word)) throw new UsageError(`${what} is a number, like 12`);
36 return Number(word.replace("#", ""));
37}
39/** One issue as a line. */
40export function issueLine(i: IssueRow): string {
41 const detail = [
42 i.agent !== null && !i.agents.includes(i.agent) ? `for ${i.agent}` : "",
43 i.agents.length > 0 ? i.agents.join(", ") : "",
44 i.blockedBy.length > 0 ? `blocked by ${i.blockedBy.join(", ")}` : "",
45 (i.labels ?? []).length > 0 ? `labels ${i.labels.join(", ")}` : "",
46 i.versions.length > 0 ? `${i.versions.length === 1 ? "version" : "versions"} ${i.versions.join(", ")}` : "",
47 i.waitingOn.length > 0 ? `waiting on ${i.waitingOn.join(", ")}` : "",
48 i.parent !== null ? `under ${i.parent}` : "",
49 i.children > 0 ? plural(i.children, "sub-issue") : "",
50 `updated ${shortWhen(i.updated)}`,
51 ].filter((d) => d !== "");
52 return ` ${String(i.id).padStart(3)} ${i.status.padEnd(11)} ${q(i.title)}${detail.length > 0 ? ` (${detail.join("; ")})` : ""}`;
53}
55function askLine(a: Ask & { title?: string | null }): string {
56 const what = a.review ? "a review" : a.kind === "task" ? "a task" : "a question";
57 const where = a.issue === null ? "" : ` on issue ${a.issue}${a.title ? ` (${q(a.title)})` : ""}`;
58 const extra = [
59 // Who answered, and when, on the ask's own line, so a handled ask can't pass for an open one.
60 a.answer ? `answered by ${a.answer.by}, ${shortWhen(a.answer.at)}` : "",
61 !a.answer && a.seen !== undefined ? seenText(a.seen) : "",
62 a.due ? `due ${a.due}` : "",
63 a.blocks.length > 0 ? `blocks ${a.blocks.join(", ")}` : "",
64 ].filter((x) => x !== "");
65 const head = ` ask ${a.id}: ${a.by}, ${shortWhen(a.at)}, asks ${a.of} ${what}${where}${extra.length > 0 ? ` (${extra.join("; ")})` : ""}: ${q(a.question)}`;
66 const lines = [head];
67 if (a.options.length > 0) lines.push(` options: ${a.options.map((o, i) => `${i + 1}. ${q(o)}`).join(", ")}`);
68 if (a.recommendation) lines.push(` ${a.recommendation.by} recommends: ${q(a.recommendation.text)}`);
69 if (a.answer) lines.push(` answer: ${q(a.answer.words)}`);
70 return lines.join("\n");
71}
73/** One edit in an issue's activity: what changed, and who was told. */
74function editLine(e: IssuePage["edits"][number]): string {
75 const c = e.changes;
76 const said = [
77 c.title ? `title ${q(String(c.title.from))} to ${q(String(c.title.to))}` : "",
78 c.description ? "the description" : "",
79 c.parent ? (c.parent.to === null ? "no parent" : `parent issue ${String(c.parent.to)}`) : "",
80 c.agent ? (c.agent.to === null ? "no Agent" : `Agent ${String(c.agent.to)}`) : "",
81 c.blockedBy
82 ? (c.blockedBy.to as number[]).length === 0
83 ? "blocked by nothing"
84 : `blocked by ${(c.blockedBy.to as number[]).join(", ")}`
85 : "",
86 c.labels
87 ? (c.labels.to as string[]).length === 0
88 ? "no labels"
89 : `labels ${(c.labels.to as string[]).join(", ")}`
90 : "",
91 ].filter((x) => x !== "");
92 return ` ${e.by}, ${shortWhen(e.at)}: ${said.join("; ")} (${e.notified.length > 0 ? `told ${e.notified.join(", ")}` : "told nobody"})`;
93}
95/** Labels as typed, lower-cased, since the Worker refuses upper case (Security): --label Friction means friction. */
96function labelList(value: string): string[] {
97 return listOf(value).map((l) => l.toLowerCase());
98}
100/** A comma-separated list of names or issue numbers; "none" is the empty list. */
101function listOf(value: string): string[] {
102 return value.trim() === "none"
103 ? []
104 : value
105 .split(",")
106 .map((v) => v.trim())
107 .filter((v) => v !== "");
108}
110/** An issue's page, as text. */
111export function issuePage(p: IssuePage): string[] {
112 const i = p.issue;
113 const facts = [
114 i.parent !== null ? `Under issue ${i.parent}.` : "",
115 `Opened by ${i.opened.by}, ${shortWhen(i.opened.at)}.`,
116 i.agent !== null ? `Agent: ${i.agent}.` : "",
117 i.agents.length > 0 ? `Working on it: ${i.agents.join(", ")}.` : "",
118 i.blockedBy.length > 0 ? `Blocked by: issue${i.blockedBy.length === 1 ? "" : "s"} ${i.blockedBy.join(", ")}.` : "",
119 (i.labels ?? []).length > 0 ? `Labels: ${i.labels.join(", ")}.` : "",
120 i.versions.length > 0 ? `Versions: ${i.versions.join(", ")}.` : "",
121 i.waitingOn.length > 0 ? `Waiting on: ${i.waitingOn.join(", ")}.` : "",
122 i.closed
123 ? `${i.closed.how === "done" ? "Done" : "Dropped"} by ${i.closed.by}${i.closed.version !== null ? ` in version ${i.closed.version}` : ""}, ${shortWhen(i.closed.at)}.`
124 : "",
125 ].filter((f) => f !== "");
126 return [
127 `Issue ${i.id}: ${q(i.title)} [${i.status}]`,
128 facts.join(" "),
129 ...(i.lines ? [`From ${i.lines.file} lines ${i.lines.from}-${i.lines.to} at version ${i.lines.version}.`] : []),
130 ...(i.description !== "" ? ["", ...i.description.split("\n").map((l) => ` ${l}`)] : []),
131 ...(p.asks.length > 0 ? ["", "Asks:", ...p.asks.map(askLine)] : []),
132 ...(p.comments.length > 0
133 ? [
134 "",
135 "Comments:",
136 ...p.comments.map(
137 (c) => ` ${c.by}${c.kind === "objection" ? " objects" : ""}, ${shortWhen(c.at)}: ${q(c.text)}`,
138 ),
139 ]
140 : []),
141 ...((p.edits ?? []).length > 0 ? ["", "Edits:", ...p.edits.map(editLine)] : []),
142 ...(p.children.length > 0 ? ["", "Sub-issues:", ...p.children.map(issueLine)] : []),
143 ...((i.watchers ?? []).length > 0 ? ["", `Watchers: ${i.watchers.join(", ")}.`] : []),
144 "",
145 `avcs start "..." --issue ${i.id} starts on it; avcs issue ${i.id} --comment "..." comments; avcs ask <who> "..." --issue ${i.id} asks.`,
146 ];
147}
149export async function runIssues(args: Parsed, env: Env): Promise<Output> {
150 if (args.positionals.length > 0)
151 throw new UsageError(
152 "issues takes no words: avcs issues [--open] [--status <s>] [--agent <a>] [--parent <N>] [--label <l,l>] | avcs issues --labels; avcs issue <N> shows one",
153 );
154 const { api } = openIssues(env);
155 if (bool(args, "drafts")) {
156 const { drafts } = await api.drafts();
157 return {
158 text:
159 drafts.length === 0
160 ? 'No drafts. avcs issue draft "<title>" saves one: only you see it, and nobody hears of it.'
161 : [
162 `${plural(drafts.length, "draft")}, newest first (only you see them):`,
163 ...drafts.map(draftLine),
164 "avcs issue draft <D> --open opens one as an issue; --delete deletes it.",
165 ].join("\n"),
166 json: { drafts },
167 };
168 }
169 if (bool(args, "labels")) {
170 const { labels } = await api.issueLabels();
171 return {
172 text:
173 labels.length === 0
174 ? 'No labels yet. avcs issue new "<title>" --label friction labels a new issue; avcs issue <N> --label <l> labels one.'
175 : [
176 `${plural(labels.length, "label")} in use:`,
177 ...labels.map((l) => ` ${l.label}: ${l.open} open of ${l.total}`),
178 "avcs issues --label <label> lists a label's issues.",
179 ].join("\n"),
180 json: { labels },
181 };
182 }
183 const parent = str(args, "parent");
184 const filter = {
185 ...(str(args, "status") ? { status: str(args, "status")! } : {}),
186 ...(str(args, "agent") ? { agent: str(args, "agent")! } : {}),
187 ...(parent !== undefined ? { parent: issueNumber(parent, "--parent") } : {}),
188 ...(str(args, "label") !== undefined ? { labels: labelList(str(args, "label")!) } : {}),
189 open: bool(args, "open"),
190 };
191 const { issues } = await api.issues(filter);
192 const scope = [
193 filter.open ? "open " : "",
194 filter.status ? `${filter.status} ` : "",
195 filter.labels ? `${filter.labels.join(", ")} ` : "",
196 ].join("");
197 return {
198 text:
199 issues.length === 0
200 ? `No ${scope}issues${filter.agent ? ` for ${filter.agent}` : ""}${parent !== undefined ? ` under ${parent}` : ""}. avcs issue new "<title>" opens one.`
201 : [
202 `${plural(issues.length, `${scope}issue`)}, most recently updated first:`,
203 ...issues.map(issueLine),
204 "avcs issue <N> shows one.",
205 ].join("\n"),
206 json: { issues },
207 };
208}
210/** A new issue's fields from the command line, as open and drafts take them; `title` only when words were given. */
211function openFields(args: Parsed, title: string): DraftBody {
212 const lines = str(args, "lines");
213 const at = lines === undefined ? undefined : /^(.+):(\d+)(?:-(\d+))?@(\d+)$/.exec(lines);
214 if (lines !== undefined && !at)
215 throw new UsageError("--lines is <file>:<from>-<to>@<version>, like src/a.ts:10-14@140");
216 const parent = str(args, "parent");
217 return {
218 ...(title !== "" ? { title } : {}),
219 ...(str(args, "description") ? { description: str(args, "description")! } : {}),
220 ...(parent !== undefined ? { parent: issueNumber(parent, "--parent") } : {}),
221 ...(at ? { lines: { file: at[1]!, from: Number(at[2]), to: Number(at[3] ?? at[2]), version: Number(at[4]) } } : {}),
222 ...(str(args, "agent") ? { agent: str(args, "agent")! } : {}),
223 ...(str(args, "blocked-by") !== undefined
224 ? { blockedBy: listOf(str(args, "blocked-by")!).map((b) => issueNumber(b, "--blocked-by")) }
225 : {}),
226 ...(str(args, "notify") !== undefined ? { notify: listOf(str(args, "notify")!) } : {}),
227 ...(str(args, "label") !== undefined ? { labels: labelList(str(args, "label")!) } : {}),
228 };
229}
231/** One draft as a line: its number, title, and what else it holds. */
232function draftLine(d: Draft): string {
233 const b = d.body;
234 const more = [
235 b.agent ? `for ${b.agent}` : "",
236 b.parent ? `under ${b.parent}` : "",
237 (b.labels ?? []).length > 0 ? `labels ${b.labels!.join(", ")}` : "",
238 b.description ? "a description" : "",
239 ].filter((m) => m !== "");
240 return ` draft ${d.id}: ${b.title ? q(b.title) : "(no title yet)"}${more.length > 0 ? ` (${more.join("; ")})` : ""}, saved ${shortWhen(d.saved)}`;
241}
243/**
244 * Drafts (abso issue 3): an issue not opened yet, this agent's alone, with no
245 * number and no news. `abso issue draft "<title>" [fields]` saves a new one;
246 * `abso issue draft <D> [fields]` changes one; --open opens it as an issue;
247 * --delete deletes it. `abso issues --drafts` lists them.
248 */
249async function runDraft(api: Api, args: Parsed, words: string[]): Promise<Output> {
250 const [d, ...more] = words;
251 if (d !== undefined && /^\d+$/.test(d) && more.length === 0) {
252 const id = Number(d);
253 if (bool(args, "delete") && bool(args, "open")) throw new UsageError("--open or --delete, not both");
254 if (bool(args, "delete")) {
255 await api.deleteDraft(id);
256 return { text: `Deleted draft ${id}.`, json: { draft: id, deleted: true } };
257 }
258 if (bool(args, "open")) {
259 const r = await api.openDraft(id, openFields(args, ""));
260 return {
261 text: `Opened draft ${id} as issue ${r.id}. avcs start "..." --issue ${r.id} starts on it.`,
262 json: { draft: id, id: r.id },
263 };
264 }
265 // Saving replaces the draft's body, so the fields given go over what it holds.
266 const found = (await api.drafts()).drafts.find((x) => x.id === id);
267 if (!found) throw new UsageError(`you have no draft ${id}; avcs issues --drafts lists yours`);
268 await api.saveDraft({ ...found.body, ...openFields(args, ""), id });
269 return { text: `Saved draft ${id}.`, json: { draft: id } };
270 }
271 if (bool(args, "open") || bool(args, "delete"))
272 throw new UsageError("--open and --delete take a draft's number: avcs issue draft <D> --open");
273 const r = await api.saveDraft(openFields(args, words.join(" ")));
274 return {
275 text: `Saved draft ${r.id}. Only you see it, and nobody hears of it. avcs issue draft ${r.id} --open opens it as an issue.`,
276 json: { draft: r.id },
277 };
278}
280export async function runIssue(args: Parsed, env: Env): Promise<Output> {
281 const [first, ...rest] = args.positionals;
282 const { api } = openIssues(env);
283 if (first === "draft") return runDraft(api, args, rest);
284 if (first === "new") {
285 if (rest.length === 0)
286 throw new UsageError(
287 'avcs issue new "<title>" [--description "..."] [--parent <N>] [--lines <file>:<from>-<to>@<version>] [--agent <agent>] [--blocked-by <N,N>] [--notify <who,who>] [--label <l,l>]',
288 );
289 const r = await api.issueOpen({ ...openFields(args, rest.join(" ")), title: rest.join(" ") });
290 return { text: `Opened issue ${r.id}. avcs start "..." --issue ${r.id} starts on it.`, json: { id: r.id } };
291 }
292 if (first === undefined || rest.length > 0)
293 throw new UsageError(
294 'avcs issue <N> [--comment "..." | --object "..." | --drop | --reopen | --done [--done-by <version>] | an edit], or avcs issue new "<title>"',
295 );
296 const id = issueNumber(first);
297 const comment = str(args, "comment");
298 const object = str(args, "object");
299 const doneBy = str(args, "done-by");
300 const markingDone = bool(args, "done") || doneBy !== undefined;
301 const edits = ["title", "description", "parent", "agent", "blocked-by", "label", "unlabel"].filter(
302 (f) => str(args, f) !== undefined,
303 );
304 if (
305 [
306 comment !== undefined,
307 object !== undefined,
308 bool(args, "drop"),
309 bool(args, "reopen"),
310 markingDone,
311 edits.length > 0,
312 ].filter(Boolean).length > 1
313 )
314 throw new UsageError("one of --comment, --object, --drop, --reopen, --done or an edit at a time");
315 if (edits.length > 0) {
316 const parent = str(args, "parent");
317 const agent = str(args, "agent");
318 const blockedBy = str(args, "blocked-by");
319 const r = await api.issueEdit(id, {
320 ...(str(args, "title") !== undefined ? { title: str(args, "title")! } : {}),
321 ...(str(args, "description") !== undefined ? { description: str(args, "description")! } : {}),
322 ...(parent !== undefined ? { parent: parent === "none" ? null : issueNumber(parent, "--parent") } : {}),
323 ...(agent !== undefined ? { agent: agent === "none" ? null : agent } : {}),
324 ...(blockedBy !== undefined ? { blockedBy: listOf(blockedBy).map((b) => issueNumber(b, "--blocked-by")) } : {}),
325 ...(bool(args, "quiet") ? { notify: false } : {}),
326 ...(str(args, "notify") !== undefined ? { also: listOf(str(args, "notify")!) } : {}),
327 ...(str(args, "label") !== undefined ? { addLabels: labelList(str(args, "label")!) } : {}),
328 ...(str(args, "unlabel") !== undefined ? { removeLabels: labelList(str(args, "unlabel")!) } : {}),
329 // A labels-only edit tells nobody unless --notify names people.
330 ...(edits.every((f) => f === "label" || f === "unlabel") && str(args, "notify") !== undefined
331 ? { notify: true }
332 : {}),
333 });
334 return {
335 text: `Edited issue ${id}. ${r.notified.length > 0 ? `Told ${r.notified.join(", ")}.` : "Told nobody."}`,
336 json: { issue: id, edit: r.edit, notified: r.notified },
337 };
338 }
339 if (bool(args, "quiet") || str(args, "notify") !== undefined)
340 throw new UsageError(
341 "--quiet and --notify go with an edit (--title, --description, --parent, --agent, --blocked-by, --label or --unlabel)",
342 );
343 if (comment !== undefined || object !== undefined) {
344 const r = await api.issueComment(id, (comment ?? object)!, object !== undefined);
345 return {
346 text: `${object !== undefined ? "Objected" : "Commented"} on issue ${id}. Its agents hear it.`,
347 json: { id: r.id, issue: id },
348 };
349 }
350 if (bool(args, "drop")) {
351 await api.issueDrop(id);
352 return { text: `Dropped issue ${id}.`, json: { issue: id, dropped: true } };
353 }
354 if (bool(args, "reopen")) {
355 await api.issueReopen(id);
356 return { text: `Reopened issue ${id}.`, json: { issue: id, reopened: true } };
357 }
358 if (markingDone) {
359 let version: number | null = null;
360 if (doneBy !== undefined) {
361 version = issueNumber(doneBy, "--done-by");
362 if (version < 1) throw new UsageError("--done-by is a published version, 1 or more");
363 try {
364 await api.show(version, { draft: null });
365 } catch {
366 throw new UsageError(`no version ${version}`);
367 }
368 }
369 await api.issueDone(id, version);
370 return {
371 text: version === null ? `Marked issue ${id} done.` : `Marked issue ${id} done in version ${version}.`,
372 json: { issue: id, done: true, ...(version !== null ? { version } : {}) },
373 };
374 }
375 const page = await api.issue(id);
376 return { text: issuePage(page).join("\n"), json: { ...page } };
377}
379export async function runAsk(args: Parsed, env: Env): Promise<Output> {
380 const [of, ...words] = args.positionals;
381 if (of === undefined || words.length === 0)
382 throw new UsageError(
383 'avcs ask <who> "<question>" [--issue <N>] [--review | --task] [--due <YYYY-MM-DD>] [--blocks <N,N>] [--options "A|B|C"]; who is an agent, or person:<id>',
384 );
385 const { api } = openIssues(env);
386 if (bool(args, "review") && bool(args, "task")) throw new UsageError("an ask is a review or a task, not both");
387 const issue = str(args, "issue");
388 const blocks = str(args, "blocks");
389 const options = str(args, "options");
390 const r = await api.ask({
391 of,
392 question: words.join(" "),
393 ...(issue !== undefined ? { issue: issueNumber(issue, "--issue") } : {}),
394 ...(bool(args, "review") ? { review: true } : {}),
395 ...(bool(args, "task") ? { kind: "task" as const } : {}),
396 ...(str(args, "due") ? { due: str(args, "due")! } : {}),
397 ...(blocks !== undefined ? { blocks: blocks.split(",").map((b) => issueNumber(b.trim(), "--blocks")) } : {}),
398 ...(options !== undefined
399 ? {
400 options: options
401 .split("|")
402 .map((o) => o.trim())
403 .filter((o) => o !== ""),
404 }
405 : {}),
406 });
407 return { text: `Asked ${of} (ask ${r.id}). They hear it; avcs waiting shows what's open.`, json: { id: r.id } };
408}
410export async function runWaiting(args: Parsed, env: Env): Promise<Output> {
411 if (args.positionals.length > 0)
412 throw new UsageError("waiting takes no words: avcs waiting [--of <agent or person:id>] [--mine]");
413 const { api, config } = openIssues(env);
414 const mine = bool(args, "mine") || str(args, "of") === config.agent;
415 const of = mine ? config.agent : str(args, "of");
416 const { asks } = mine ? await api.waitingMine() : await api.waiting(of);
417 return {
418 text:
419 asks.length === 0
420 ? `Nothing is waiting${of ? ` on ${of}` : ""}.`
421 : [
422 `${plural(asks.length, "open ask")}${of ? ` of ${of}` : ""}, soonest due first:`,
423 ...asks.map(askLine),
424 'avcs answer <ask> "..." answers one asked of you.',
425 ].join("\n"),
426 json: { asks },
427 };
428}
430export async function runAnswer(args: Parsed, env: Env): Promise<Output> {
431 const [first, ...words] = args.positionals;
432 const id = issueNumber(first, "the ask number");
433 const option = str(args, "option");
434 const done = bool(args, "done");
435 if (words.length === 0 && option === undefined && !done)
436 throw new UsageError('avcs answer <ask> "<words>" | --option <N> [--note "..."] | --done [--note "..."]');
437 const { api } = openIssues(env);
438 const note = str(args, "note");
439 const r = await api.answer(id, {
440 ...(words.length > 0 ? { words: words.join(" ") } : {}),
441 ...(option !== undefined ? { option: /^\d+$/.test(option) ? Number(option) - 1 : option } : {}),
442 ...(done ? { done: true } : {}),
443 ...(note !== undefined ? { note } : {}),
444 });
445 return { text: `Answered ask ${id}. ${r.ask.by} hears it.`, json: { ask: r.ask } };
446}