fix(post): only a trailing Capy badge splits off; other marker blocks stay in their section; tighter run-id exemptions (GST-1227 review)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
garrytanandcapy-ai[bot] committed 2026-11-03 15:01:49 +00:00
1 parent 637b60bc2f
commit 899ee9a8a9
6 files changed
+108 -46

No files matched your search

+25 -22
View File
@@ -160,7 +160,7 @@ function parseArgs(argv: string[]): Request {
return req;
}
/** One piece of a PR body: the leading text, a `## ` section, the ship-receipt block, a marker block, or text after a receipt or marker block. */
/** One piece of a PR body: the leading text, a `## ` section, the ship-receipt block, text after a receipt block, or a Capy badge that ends the body. */
interface Section { key: string; label: string; text: string }
/**
@@ -175,38 +175,42 @@ interface Section { key: string; label: string; text: string }
* nothing; a receipt fence that never closes makes the body from that line
* on one section the merge never matches (`unclosed-receipt`), so it is kept
* byte for byte instead of being read as a receipt or as replaceable text.
* After the first receipt or heading, a `<!-- <name>:start -->` line outside
* a fence opens a marker block (a bot's badge, such as Capy's
* `<!-- capy-badge:start -->…<!-- capy-badge:end -->`) that runs through its
* `<!-- <name>:end -->` line, or up to the next heading when it never
* closes, so a badge at the end of the body never leaves with the section
* above it (GST-1227). A marker block in the leading text stays part of it.
* A Capy badge (`<!-- capy-badge:start -->` through `<!-- capy-badge:end -->`,
* both outside a fence) that ends the body after its first receipt or
* heading, with only blank lines after it, is a piece of its own
* (`badge:capy-badge`), so it never leaves with the section above it
* (GST-1227). A badge anywhere else, an unclosed badge marker and every other
* marker block stay inside the section they sit in.
*/
function splitSections(body: string): Section[] {
const out: Section[] = [];
let fence: string | null = null;
let block: string | null = null;
let badgeAt = -1;
let inBadge = false;
let plain = 0;
const open = (key: string, label: string) => out.push({ key, label, text: "" });
const open = (key: string, label: string) => { badgeAt = -1; inBadge = false; out.push({ key, label, text: "" }); };
const openText = () => out.length === 0 ? open("text:lead", "leading text") : open(`text:${plain++}`, "trailing text");
const closes = (run: string) => new RegExp(`^ {0,3}${run[0] === "`" ? "`" : "~"}{${run.length},}\\s*$`).test(bare);
let bare = "";
const endsBlock = () => { if (block !== null && bare.includes(`<!-- ${block}:end -->`)) { block = null; openText(); } };
for (const line of body.split(/(?<=\n)/)) {
bare = line.replace(/\r?\n$/, "");
if (block !== null && /^ {0,3}## /.test(bare)) block = null;
if (block !== null) { out.at(-1)!.text += line; endsBlock(); continue; }
const fenceOpen: RegExpExecArray | null = fence === null ? /^ {0,3}(`{3,}|~{3,})/.exec(bare) : null;
const marker = fence === null ? /^ {0,3}<!-- ([\w.-]+):start -->/.exec(bare) : null;
const outside = fence === null && !fenceOpen;
if (fenceOpen && bare.trim() === "```" + RECEIPT_FENCE) { open("receipt", "ship receipt"); fence = "receipt"; }
else if (fence === "receipt") { out.at(-1)!.text += line; if (closes("```")) { fence = null; openText(); } continue; }
else if (fenceOpen) fence = fenceOpen[1]!;
else if (fence && closes(fence)) fence = null;
else if (fence === null && /^ {0,3}## /.test(bare)) { const title = bare.replace(/^ {0,3}## /, "").trim(); open(`h2:${title.toLowerCase()}`, title); }
else if (marker && out.some(s => s.key === "receipt" || s.key.startsWith("h2:"))) { open(`block:${marker[1]}`, `${marker[1]} block`); block = marker[1]!; }
if (out.length === 0) openText();
if (outside && /^ {0,3}<!-- capy-badge:start -->/.test(bare)) { badgeAt = out.at(-1)!.text.length; inBadge = true; }
else if (inBadge && outside && /<!-- capy-badge:end -->/.test(bare)) inBadge = false;
else if (!inBadge && bare.trim() !== "") badgeAt = -1;
out.at(-1)!.text += line;
endsBlock();
}
const last = out.at(-1);
if (last && badgeAt >= 0 && !inBadge && fence === null && last.key !== "text:lead") {
out.push({ key: "badge:capy-badge", label: "capy-badge block", text: last.text.slice(badgeAt) });
last.text = last.text.slice(0, badgeAt);
}
if (fence === "receipt") Object.assign(out.at(-1)!, { key: "unclosed-receipt", label: "unclosed receipt fence" });
return out.filter(s => s.text !== "");
@@ -216,8 +220,8 @@ function splitSections(body: string): Section[] {
* The body to send for `pr-body` without `--replace`: each section of the
* file replaces the same-keyed section of the current body in place, current
* sections the file does not rewrite are kept byte for byte, new `## `
* sections are appended after them (above a marker block that ends the
* body), new leading text goes first, and a new receipt goes right after the
* sections are appended after them (above a Capy badge that ends the body;
* a badge only the file brings goes last), new leading text goes first, and a new receipt goes right after the
* current leading text, or first when there is none (where
* lib/ship-receipt.ts puts the receipt). Text outside sections
* matches by role (the file's leading text replaces the leading text; text
@@ -273,13 +277,12 @@ export function mergeBodySections(current: string, incoming: string): { body: st
}
const fresh = next.filter(n => !used.has(n));
for (const n of next) if (fresh.includes(n) || (ours.includes(n) && !paired.has(n))) added.push(n.label);
const first = fresh.filter(n => !n.key.startsWith("h2:"));
const badge = (s: Section) => s.key === "badge:capy-badge";
const first = fresh.filter(n => !n.key.startsWith("h2:") && !badge(n));
if (now.length === 0) return { body: incoming, replaced, kept, added, dropped };
const lead = merged[0]?.key === "text:lead" && merged[0].text.trim() ? 1 : 0;
let tail = merged.length;
while (tail > lead && (merged[tail - 1]!.key.startsWith("block:") || !merged[tail - 1]!.text.trim())) tail--;
if (!merged.slice(tail).some(s => s.key.startsWith("block:"))) tail = merged.length;
const all = [...merged.slice(0, lead), ...first, ...merged.slice(lead, tail), ...fresh.filter(n => n.key.startsWith("h2:")), ...merged.slice(tail)];
const tail = merged.length && badge(merged.at(-1)!) ? merged.length - 1 : merged.length;
const all = [...merged.slice(0, lead), ...first, ...merged.slice(lead, tail), ...fresh.filter(n => n.key.startsWith("h2:")), ...merged.slice(tail), ...fresh.filter(badge)];
for (const t of [...ours].reverse()) all.splice(all.indexOf(next[next.indexOf(t) - 1]!) + 1, 0, t);
const fromFile = (s: Section) => next.includes(s);
const body = all.map((s, i) => {
+9 -7
View File
@@ -357,11 +357,13 @@ export function isNumericMetadataValue(span: string, match: RegExpExecArray): bo
* command. Only the position decides: a run/job flag (`--run 37235771700`,
* `--job=111534615007`), a `gh run view|watch|rerun|download|cancel`
* argument, an `actions/runs/<id>` or `/job/<id>` URL segment (with or
* without a host or a leading `/`), a `run_id` / `job_id` key, or any id
* of a `runs=<id>/<attempt>[,<id>/<attempt>…]` list (`run=` too), the
* receipt a CI carry or run clear prints (lib/run-evidence.ts
* runReceiptFields); the `/<attempt>` of one to three digits after every id
* is what tells that list from a number (GST-1227). An 11-digit id (every GitHub Actions run id since
* without a host: `actions/runs/` at the line start or after whitespace,
* `/`, `(`, `[`, `<` or a quote, so `sales-actions/runs/<n>` is not one),
* a `run_id` / `job_id` key, or any 11- or 12-digit id of a
* `runs=<id>/<attempt>[,<id>/<attempt>…]` list (`run=` too), the receipt a
* CI carry or run clear prints (lib/run-evidence.ts runReceiptFields); the
* `/<attempt>` of one to three digits after every id is what tells that list
* from a number (GST-1227). An 11-digit id (every GitHub Actions run id since
* 2025) also counts right after a run word on the same line, the way a ship
* receipt, a READY block or an approval record names one (`screen 40477638211
* GREEN`, `gate=40323989742`, `rerun passed 40287239206`, `run #19368712345`);
@@ -373,8 +375,8 @@ export function isNumericMetadataValue(span: string, match: RegExpExecArray): bo
* else still report.
*/
const CI_ID_POSITION_BEFORE =
/(?:--(?:run|job)(?:-id)?(?:[ \t]+|[ \t]*=[ \t]*)|\bgh[ \t]+run[ \t]+(?:view|watch|rerun|download|cancel)[ \t]+|\bactions\/runs\/|\/jobs?\/|(?:run|job)_id["']?[ \t]*[:=][ \t]*["']?)$/i;
const CI_RUN_LIST = /(?<![\w-])runs?=\d+\/\d{1,3}(?:,\d+\/\d{1,3})*(?![\w\/])/gi;
/(?:--(?:run|job)(?:-id)?(?:[ \t]+|[ \t]*=[ \t]*)|\bgh[ \t]+run[ \t]+(?:view|watch|rerun|download|cancel)[ \t]+|(?:^|[\s\/(\[<"'])actions\/runs\/|\/jobs?\/|(?:run|job)_id["']?[ \t]*[:=][ \t]*["']?)$/i;
const CI_RUN_LIST = /(?<![\w-])runs?=\d{11,12}\/\d{1,3}(?:,\d{11,12}\/\d{1,3})*(?![\w\/])/gi;
/** A run word with an optional ':' '#' '=' right before an 11-digit token; shared with gstack-post's chained-id allowlist. */
export const CI_RUN_WORD_BEFORE = /\b(?:runs?|screens?|gates?|backstop|bisect|rerun|dispatch(?:ed)?|failed|passed)\s*[:#=]?\s*$/i;
export function isCiRunIdentifier(span: string, match: RegExpExecArray): boolean {