{"draft":null,"from":95,"to":96,"files":[{"path":"client/src/issues.ts","added":82,"removed":3,"diff":"--- a/client/src/issues.ts\n+++ b/client/src/issues.ts\n@@ -604,11 +604,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@@ -877,3 +893,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":[]}