{"draft":null,"from":59,"to":318,"files":[{"path":"client/src/issues.ts","added":199,"removed":14,"diff":"--- a/client/src/issues.ts\n+++ b/client/src/issues.ts\n@@ -23,24 +23,34 @@\n import { bool, type Parsed, str, UsageError } from \"./args.ts\";\n import { type Env, type Output, open, plural } from \"./commands.ts\";\n import { type Config, readMachineServers, trustedServers } from \"./config.ts\";\n-import { issuesServerFor, tokenGoesTo } from \"./core.ts\";\n+import { issuesServerFor, tokenGoesToIssues } from \"./core.ts\";\n import { fromEnv } from \"./env-names.ts\";\n import { ignoredHere } from \"./fetch.ts\";\n import { loadIgnore, relativeTo } from \"./folder.ts\";\n+import type { AskReply } from \"./issues-wire.ts\";\n+import { isLocal, LOCAL_BASE } from \"./local-store.ts\";\n import { queuedLines, standing } from \"./queue.ts\";\n import { seenText, shortWhen } from \"./times.ts\";\n \n const q = (s: string): string => JSON.stringify(s);\n \n /** The folder, with its Api pointed at the Issues Worker: the token goes only where tokenGoesTo allows, as for the avcs server. */\n-function openIssues(env: Env): { root: string; config: Config; api: Api } {\n+export function openIssues(env: Env): { root: string; config: Config; api: Api } {\n \tconst { root, config, api } = open(env);\n-\tconst server = fromEnv(env.env, \"ISSUES_SERVER\") || issuesServerFor(config.server, readMachineServers(env.env));\n+\t// A local project's issues are in its store, where the open server keeps them: at /issues.\n+\tif (isLocal(config.server)) {\n+\t\tapi.issuesServer = `${LOCAL_BASE}/issues`;\n+\t\treturn { root, config, api };\n+\t}\n+\t// An explicit --server goes to that server's own Issues Worker: ABSO_ISSUES_SERVER doesn't redirect it.\n+\tconst server =\n+\t\t(env.server === undefined ? fromEnv(env.env, \"ISSUES_SERVER\") : undefined) ||\n+\t\tissuesServerFor(config.server, readMachineServers(env.env));\n \tif (server === undefined)\n \t\tthrow new UsageError(\n \t\t\t`${config.server} has no Issues Worker beside it that abso knows; ~/.abso/servers.json or ABSO_ISSUES_SERVER names one`,\n \t\t);\n-\tif (config.token !== undefined && !tokenGoesTo(server, trustedServers(env.env))) {\n+\tif (config.token !== undefined && !tokenGoesToIssues(server, trustedServers(env.env))) {\n \t\tthrow new UsageError(\n \t\t\t`the Issues Worker is ${server}, and abso sends your token only to its own servers and this machine. If ${server} is yours, name it in ~/.abso/servers.json or ABSO_TRUSTED_SERVERS.`,\n \t\t);\n@@ -70,16 +80,35 @@\n \treturn `  ${String(i.id).padStart(3)}  ${i.status.padEnd(11)}  ${q(i.title)}${detail.length > 0 ? ` (${detail.join(\"; \")})` : \"\"}`;\n }\n \n-function askLine(a: Ask & { title?: string | null }, api?: Pick<Api, \"attachmentUrl\">): string {\n+/** The first line of a reply, for lists; abso ask <N> shows it whole. */\n+const firstLine = (text: string): string => {\n+\tconst line = text.split(\"\\n\")[0]!;\n+\treturn line.length < text.length ? `${line} ...` : line;\n+};\n+\n+/** One reply in an ask's thread. */\n+function replyLine(r: AskReply, indent: string, whole: boolean): string {\n+\treturn `${indent}${r.by}, ${shortWhen(r.at)}${r.question ? \", a question\" : \"\"}: ${q(whole ? r.text : firstLine(r.text))}`;\n+}\n+\n+/**\n+ * An ask: its head line, its files and options, then, in a list, its newest reply, or with `thread`, every\n+ * reply, oldest first; then its answer. Older servers send no thread, only the one asked's acknowledgement.\n+ */\n+function askLine(a: Ask & { title?: string | null }, api?: Pick<Api, \"attachmentUrl\">, thread = false): string {\n \tconst what = a.review ? \"a review\" : a.kind === \"task\" ? \"a task\" : \"a question\";\n \tconst where = a.issue === null ? \"\" : ` on issue ${a.issue}${a.title ? ` (${q(a.title)})` : \"\"}`;\n+\tconst replies = a.replies;\n \tconst extra = [\n \t\t// Who answered, and when, on the ask's own line, so a handled ask can't pass for an open one.\n \t\ta.answer\n \t\t\t? `${a.outcome === \"withdrawn\" ? \"withdrawn\" : \"answered\"} by ${a.answer.by}, ${shortWhen(a.answer.at)}`\n \t\t\t: \"\",\n-\t\t!a.answer && a.ack ? `acknowledged ${shortWhen(a.ack.at)}, not answered yet` : \"\",\n+\t\t!a.answer && a.ack && replies === undefined ? `acknowledged ${shortWhen(a.ack.at)}, not answered yet` : \"\",\n \t\t!a.answer && !a.ack && a.seen !== undefined ? seenText(a.seen) : \"\",\n+\t\treplies && replies.length > 0 ? (replies.length === 1 ? \"1 reply\" : `${replies.length} replies`) : \"\",\n+\t\t// Whose turn it is, when it isn't the one asked's: they asked something back.\n+\t\t!a.answer && a.waitingOn && a.waitingOn !== a.of ? `waiting on ${a.waitingOn}` : \"\",\n \t\ta.due ? `due ${a.due}` : \"\",\n \t\ta.blocks.length > 0 ? `blocks ${a.blocks.join(\", \")}` : \"\",\n \t].filter((x) => x !== \"\");\n@@ -87,7 +116,10 @@\n \tconst lines = [head, ...attachmentLines(api, a.attachments, \"    \")];\n \tif (a.options.length > 0) lines.push(`    options: ${a.options.map((o, i) => `${i + 1}. ${q(o)}`).join(\", \")}`);\n \tif (a.recommendation) lines.push(`    ${a.recommendation.by} recommends: ${q(a.recommendation.text)}`);\n-\tif (!a.answer && a.ack && a.ack.note !== \"\") lines.push(`    ${a.of} acknowledged: ${q(a.ack.note)}`);\n+\tif (replies === undefined) {\n+\t\tif (!a.answer && a.ack && a.ack.note !== \"\") lines.push(`    ${a.of} acknowledged: ${q(a.ack.note)}`);\n+\t} else if (thread) lines.push(...replies.map((r) => replyLine(r, \"    \", true)));\n+\telse if (replies.length > 0) lines.push(replyLine(replies[replies.length - 1]!, \"    last: \", false));\n \tif (a.answer && a.outcome === \"withdrawn\") {\n \t\tif (a.answer.words !== \"\") lines.push(`    why: ${q(a.answer.words)}`);\n \t} else if (a.answer)\n@@ -581,11 +613,27 @@\n \tif (str(args, \"attach\") !== undefined && comment === undefined && object === undefined)\n \t\tthrow new UsageError(\"--attach goes with abso issue new or --comment (or --object)\");\n \tif (comment !== undefined || object !== undefined) {\n+\t\t// Refused before anything goes up: an objection is one comment, and a part of one would read as a whole.\n+\t\tif (object !== undefined && object.length > COMMENT_MAX)\n+\t\t\tthrow new UsageError(\n+\t\t\t\t`an objection holds up to ${COMMENT_MAX} characters, and this one has ${object.length}: say it shorter, and put the rest in a --comment`,\n+\t\t\t);\n+\t\tconst parts = object !== undefined ? [object] : commentParts(comment!);\n \t\tconst attachments = await uploadAttachments(api, env.cwd, str(args, \"attach\"));\n-\t\tconst r = await api.issueComment(id, (comment ?? object)!, object !== undefined, attachments);\n+\t\tconst ids: number[] = [];\n+\t\tfor (const [i, part] of parts.entries()) {\n+\t\t\t// The attachments go on the first part, which the rest follow.\n+\t\t\tids.push((await api.issueComment(id, part, object !== undefined, i === 0 ? attachments : [])).id);\n+\t\t}\n+\t\tconst inParts = parts.length > 1 ? ` in ${parts.length} parts (comments ${ids.join(\", \")})` : \"\";\n \t\treturn {\n-\t\t\ttext: `${object !== undefined ? \"Objected\" : \"Commented\"} on issue ${id}${attachments.length > 0 ? ` with ${plural(attachments.length, \"attachment\")} (${attachments.join(\", \")})` : \"\"}. Its agents hear it.`,\n-\t\t\tjson: { id: r.id, issue: id, ...(attachments.length > 0 ? { attachments } : {}) },\n+\t\t\ttext: `${object !== undefined ? \"Objected\" : \"Commented\"} on issue ${id}${inParts}${attachments.length > 0 ? ` with ${plural(attachments.length, \"attachment\")} (${attachments.join(\", \")})` : \"\"}. Its agents hear it.`,\n+\t\t\tjson: {\n+\t\t\t\tid: ids[0],\n+\t\t\t\tissue: id,\n+\t\t\t\t...(parts.length > 1 ? { parts: ids } : {}),\n+\t\t\t\t...(attachments.length > 0 ? { attachments } : {}),\n+\t\t\t},\n \t\t};\n \t}\n \tif (bool(args, \"drop\")) {\n@@ -600,7 +648,7 @@\n \t\tlet version: number | null = null;\n \t\tif (doneBy !== undefined) {\n \t\t\tversion = issueNumber(doneBy, \"--done-by\");\n-\t\t\tif (version < 1) throw new UsageError(\"--done-by is a published version, 1 or more\");\n+\t\t\tif (version < 1) throw new UsageError(\"--done-by is a pushed version, 1 or more\");\n \t\t\ttry {\n \t\t\t\tawait api.show(version, { draft: null });\n \t\t\t} catch {\n@@ -624,6 +672,7 @@\n \tif (str(args, \"note\") !== undefined)\n \t\tthrow new UsageError('--note goes with --withdraw: abso ask --withdraw <ask> --note \"why\"');\n \tconst [of, ...words] = args.positionals;\n+\tif (of !== undefined && words.length === 0 && /^\\d+$/.test(of)) return showAsk(args, env, of);\n \tif (of === undefined || words.length === 0)\n \t\tthrow new UsageError(\n \t\t\t'abso 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>',\n@@ -659,6 +708,49 @@\n \t};\n }\n \n+/** abso ask <N>: one ask, open or closed, with its whole thread, its answer and whose turn it is. */\n+async function showAsk(args: Parsed, env: Env, word: string): Promise<Output> {\n+\tconst others = [\"issue\", \"due\", \"blocks\", \"options\", \"attach\"].filter((f) => str(args, f) !== undefined);\n+\tif (others.length > 0 || bool(args, \"review\") || bool(args, \"task\"))\n+\t\tthrow new UsageError('abso ask <N> shows ask N; to ask, say who and what: abso ask <who> \"<question>\"');\n+\tconst id = issueNumber(word, \"the ask number\");\n+\tconst { api, config } = openIssues(env);\n+\tconst { ask } = await api.askThread(id);\n+\tconst side = ask.of === config.agent || ask.by === config.agent;\n+\tconst turn = ask.waitingOn === config.agent ? \"you\" : ask.waitingOn;\n+\tconst steps = !side\n+\t\t? \"\"\n+\t\t: ask.answer\n+\t\t\t? `abso reply ${id} \"...\" still adds to it.`\n+\t\t\t: ask.of === config.agent\n+\t\t\t\t? `abso reply ${id} \"...\" adds to it; abso answer ${id} \"...\" answers and closes it.`\n+\t\t\t\t: `abso reply ${id} \"...\" adds to it.`;\n+\tconst next = [!ask.answer && turn ? `It waits on ${turn}.` : \"\", steps].join(\" \");\n+\treturn {\n+\t\ttext: [askLine(ask, api, true), next.trim()].filter((l) => l !== \"\").join(\"\\n\"),\n+\t\tjson: { ask },\n+\t};\n+}\n+\n+/** abso reply <ask> \"<words>\" [--question]: add to an ask's thread, as its asker or the one asked. */\n+export async function runReply(args: Parsed, env: Env): Promise<Output> {\n+\tconst [first, ...words] = args.positionals;\n+\tconst id = issueNumber(first, \"the ask number\");\n+\tif (words.length === 0)\n+\t\tthrow new UsageError(\n+\t\t\t`abso reply <ask> \"<words>\" [--question]: adds to an ask's thread, as its asker or the one asked`,\n+\t\t);\n+\tconst { api, config } = openIssues(env);\n+\tconst question = bool(args, \"question\");\n+\tconst r = await api.reply(id, { text: words.join(\" \"), ...(question ? { question: true } : {}) });\n+\tconst other = r.ask.by === config.agent ? r.ask.of : r.ask.by;\n+\tconst turn =\n+\t\t!r.ask.answer && r.ask.waitingOn\n+\t\t\t? ` It waits on ${r.ask.waitingOn === config.agent ? \"you\" : r.ask.waitingOn}.`\n+\t\t\t: \"\";\n+\treturn { text: `Replied on ask ${id}. ${other} hears it.${turn}`, json: { ask: r.ask } };\n+}\n+\n /** abso ask --withdraw <ask> [--note \"why\"]: the asker takes an ask back. It closes like an answer, and the one asked hears it. */\n async function withdrawAsk(args: Parsed, env: Env, word: string): Promise<Output> {\n \tconst others = [\"issue\", \"due\", \"blocks\", \"options\", \"attach\"].filter((f) => str(args, f) !== undefined);\n@@ -708,7 +800,8 @@\n \n export async function runWaiting(args: Parsed, env: Env): Promise<Output> {\n \tif (args.positionals.length > 0)\n-\t\tthrow new UsageError(\"waiting takes no words: abso waiting [--of <agent or person:id>] [--mine]\");\n+\t\tthrow new UsageError(\"waiting takes no words: abso waiting [--of <agent or person:id>] [--mine] [--asked]\");\n+\tif (bool(args, \"asked\")) return askedOf(args, env);\n \tconst { api, config } = openIssues(env);\n \tconst mine = bool(args, \"mine\") || str(args, \"of\") === config.agent;\n \tconst of = mine ? config.agent : str(args, \"of\");\n@@ -728,7 +821,7 @@\n \t\t\t\t: [\n \t\t\t\t\t\t`${plural(asks.length, \"open ask\")}${of ? ` of ${of}` : \"\"}, soonest due first:`,\n \t\t\t\t\t\t...asks.map((a) => askLine(a, api)),\n-\t\t\t\t\t\t'abso answer <ask> \"...\" answers one asked of you.',\n+\t\t\t\t\t\t'abso ask <N> shows one with its thread; abso reply <N> \"...\" adds to it; abso answer <N> \"...\" answers one asked of you.',\n \t\t\t\t\t]),\n \t\t].join(\"\\n\"),\n \t\tjson: {\n@@ -740,6 +833,35 @@\n \t};\n }\n \n+/** abso waiting --asked: the asks you sent, open ones first, then the latest answered, each with its newest reply. */\n+async function askedOf(args: Parsed, env: Env): Promise<Output> {\n+\tif (bool(args, \"mine\") || str(args, \"of\") !== undefined)\n+\t\tthrow new UsageError(\"--asked lists the asks you sent; it takes no --mine or --of\");\n+\tconst { api, config } = openIssues(env);\n+\tconst { asks } = await api.asked();\n+\tconst open = asks.filter((a) => !a.answer);\n+\tconst closed = asks.filter((a) => a.answer);\n+\treturn {\n+\t\ttext: [\n+\t\t\t...(asks.length === 0\n+\t\t\t\t? [`${config.agent} hasn't asked anything yet.`]\n+\t\t\t\t: [\n+\t\t\t\t\t\t...(open.length > 0\n+\t\t\t\t\t\t\t? [`${plural(open.length, \"open ask\")} you sent:`, ...open.map((a) => askLine(a, api))]\n+\t\t\t\t\t\t\t: []),\n+\t\t\t\t\t\t...(closed.length > 0\n+\t\t\t\t\t\t\t? [\n+\t\t\t\t\t\t\t\t\t`${closed.length === 1 ? \"The latest answered\" : `The latest ${closed.length} answered`}:`,\n+\t\t\t\t\t\t\t\t\t...closed.map((a) => askLine(a, api)),\n+\t\t\t\t\t\t\t\t]\n+\t\t\t\t\t\t\t: []),\n+\t\t\t\t\t\t\"abso ask <N> shows one with its whole thread.\",\n+\t\t\t\t\t]),\n+\t\t].join(\"\\n\"),\n+\t\tjson: { asks },\n+\t};\n+}\n+\n /** abso ack <ask> [\"note\"]: tell the asker you have it, and answer later. The ask stays open until abso answer. */\n export async function runAck(args: Parsed, env: Env): Promise<Output> {\n \tconst [first, ...words] = args.positionals;\n@@ -748,7 +870,7 @@\n \tconst note = words.join(\" \");\n \tconst r = await api.ack(id, note !== \"\" ? { note } : {});\n \treturn {\n-\t\ttext: `Acknowledged ask ${id}. ${r.ask.by} hears it; the ask stays open until abso answer ${id} \"...\".`,\n+\t\ttext: `Acknowledged ask ${id}. ${r.ask.by} hears it; abso reply ${id} \"...\" adds more as you go, and the ask stays open until abso answer ${id} \"...\".`,\n \t\tjson: { ask: r.ask },\n \t};\n }\n@@ -780,3 +902,66 @@\n \t\tjson: { ask: r.ask, ...(lost ? { notKept: lost } : {}) },\n \t};\n }\n+\n+/** What the Issues Worker takes in one comment (its LIMITS.text). */\n+export const COMMENT_MAX = 4000;\n+\n+/** Room a part keeps for its \"(i/n)\" line and blank line, and for closing a code block it ends inside. */\n+const PART_MARK = 12;\n+const FENCE_CLOSE = 5;\n+\n+/**\n+ * A comment too long for one, as parts that each fit, each starting with \"(i/n)\". A part ends at the last\n+ * paragraph break that fits, else the last line break, else the last space, else at the limit. A part that\n+ * would end inside a fenced code block closes it, and the next part opens it again with the same fence line.\n+ */\n+export function commentParts(text: string, max = COMMENT_MAX): string[] {\n+\tif (text.length <= max) return [text];\n+\tconst bodies: string[] = [];\n+\tlet rest = text;\n+\tlet reopen = \"\";\n+\twhile (rest !== \"\") {\n+\t\tconst room = max - PART_MARK - FENCE_CLOSE - reopen.length;\n+\t\tlet body: string;\n+\t\tif (reopen.length + rest.length <= max - PART_MARK) {\n+\t\t\tbody = rest;\n+\t\t\trest = \"\";\n+\t\t} else {\n+\t\t\tconst cut = cutAt(rest, room);\n+\t\t\tbody = rest.slice(0, cut).trimEnd();\n+\t\t\t// A space cut at goes with neither part; the line breaks between paragraphs go too.\n+\t\t\trest = rest.slice(rest[cut] === \" \" ? cut + 1 : cut).replace(/^\\n+/, \"\");\n+\t\t\tif (rest.trim() === \"\") rest = \"\";\n+\t\t}\n+\t\tlet part = reopen + body;\n+\t\tconst fence = openFence(part);\n+\t\tif (fence && rest !== \"\") {\n+\t\t\tpart += `\\n${fence.marker}`;\n+\t\t\treopen = `${fence.line}\\n`;\n+\t\t} else reopen = \"\";\n+\t\tbodies.push(part);\n+\t}\n+\treturn bodies.map((b, i) => `(${i + 1}/${bodies.length})\\n\\n${b}`);\n+}\n+\n+/** Where to end a part of at most `room` characters of `text`: a paragraph break, a line break, a space, or the limit. */\n+function cutAt(text: string, room: number): number {\n+\tconst head = text.slice(0, room);\n+\tconst floor = room / 4;\n+\tfor (const at of [head.lastIndexOf(\"\\n\\n\"), head.lastIndexOf(\"\\n\"), head.lastIndexOf(\" \")]) if (at > floor) return at;\n+\t// Not between the two halves of a character outside the Basic Multilingual Plane.\n+\tconst code = text.charCodeAt(room - 1);\n+\treturn code >= 0xd800 && code <= 0xdbff ? room - 1 : room;\n+}\n+\n+/** The fence line of a code block still open at the end of `text`, and the marker that closes it, or undefined. */\n+function openFence(text: string): { line: string; marker: string } | undefined {\n+\tlet open: { line: string; marker: string } | undefined;\n+\tfor (const line of text.split(\"\\n\")) {\n+\t\tconst m = /^\\s*(`{3,}|~{3,})/.exec(line);\n+\t\tif (!m) continue;\n+\t\tif (!open) open = { line, marker: m[1]! };\n+\t\telse if (m[1]!.startsWith(open.marker) && line.trim() === m[1]) open = undefined;\n+\t}\n+\treturn open;\n+}"}],"unmatched":[]}