diff --git a/.gitignore b/.gitignore index 95c9bf17c..2ad26b6af 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,6 @@ dist/ # Legacy drive-local snapshots; the preparing launcher no longer executes them. .drive-gate/ .flows/build.key + +# workflows/pr-review.flow.ts scratch dir (findings, diff, publisher script) +review/ diff --git a/packages/sdk/tests/pr-review-post.test.ts b/packages/sdk/tests/pr-review-post.test.ts new file mode 100644 index 000000000..896708b22 --- /dev/null +++ b/packages/sdk/tests/pr-review-post.test.ts @@ -0,0 +1,235 @@ +// Exercises workflows/pr-review-post.cjs (this repo's own PR reviewer's +// publisher) against a fake GitHub API, one branch per case, and pins the +// copy embedded in workflows/pr-review.flow.ts to the file. +import { createServer, type IncomingMessage, type Server } from 'node:http'; +import { execFile } from 'node:child_process'; +import { mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join, resolve } from 'node:path'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; + +const ROOT = resolve(__dirname, '../../..'); +const SCRIPT = join(ROOT, 'workflows/pr-review-post.cjs'); +const SHA_A = 'a'.repeat(40); +const SHA_B = 'b'.repeat(40); +const SHA_C = 'c'.repeat(40); + +interface Comment { id: number; body: string; user: { login: string; type: string }; updated_at: string } +interface Fake { + comments: Comment[]; + user: { login: string } | null; + compare: Record; + rereadStatus: number | null; + dates: Record; + log: string[]; + nextId: number; +} + +let server: Server; let base: string; let fake: Fake; let dir: string; + +function body(req: IncomingMessage): Promise { + return new Promise((res) => { let s = ''; req.on('data', (d) => { s += d; }); req.on('end', () => res(s)); }); +} +beforeEach(async () => { + fake = { comments: [], user: null, compare: {}, dates: {}, log: [], nextId: 100, rereadStatus: null }; + dir = mkdtempSync(join(tmpdir(), 'pr-review-post-')); + writeFileSync(join(dir, 'body.md'), 'verdict body\n'); + server = createServer(async (req, res) => { + const url = new URL(req.url ?? '/', 'http://x'); + const path = url.pathname; const method = req.method ?? 'GET'; + fake.log.push(`${method} ${path}`); + const json = (code: number, value: unknown) => { res.writeHead(code, { 'content-type': 'application/json' }); res.end(value === null ? '' : JSON.stringify(value)); }; + if (method === 'GET' && path === '/user') return fake.user ? json(200, fake.user) : json(403, { message: 'Resource not accessible by integration' }); + if (method === 'GET' && path === '/repos/o/r/issues/7/comments') { + const page = Number(url.searchParams.get('page') ?? '1'); const per = Number(url.searchParams.get('per_page') ?? '100'); + return json(200, fake.comments.slice((page - 1) * per, page * per)); + } + if (method === 'POST' && path === '/repos/o/r/issues/7/comments') { + const { body: b } = JSON.parse(await body(req)) as { body: string }; + const c: Comment = { id: fake.nextId++, body: b, user: { login: 'relay[bot]', type: 'Bot' }, updated_at: new Date().toISOString() }; + fake.comments.push(c); return json(201, c); + } + const one = path.match(/^\/repos\/o\/r\/issues\/comments\/(\d+)$/); + if (one) { + const c = fake.comments.find((x) => x.id === Number(one[1])); + if (!c) return json(404, { message: 'Not Found' }); + if (method === 'GET') return fake.rereadStatus ? json(fake.rereadStatus, { message: 'x' }) : json(200, c); + if (method === 'PATCH') { c.body = (JSON.parse(await body(req)) as { body: string }).body; c.updated_at = new Date().toISOString(); return json(200, c); } + if (method === 'DELETE') { fake.comments = fake.comments.filter((x) => x.id !== c.id); return json(204, null); } + } + const cmp = path.match(/^\/repos\/o\/r\/compare\/([0-9a-f]{40})\.\.\.([0-9a-f]{40})$/); + if (cmp) { const st = fake.compare[`${cmp[1]}...${cmp[2]}`]; return st === undefined || st === 404 ? json(404, { message: 'Not Found' }) : st === 500 ? json(500, { message: 'boom' }) : json(200, { status: st }); } + const commit = path.match(/^\/repos\/o\/r\/commits\/([0-9a-f]{40})$/); + if (commit) { const d = fake.dates[commit[1]]; return d ? json(200, { commit: { committer: { date: d } } }) : json(404, { message: 'Not Found' }); } + return json(500, { message: `unhandled ${method} ${path}` }); + }); + await new Promise((r) => server.listen(0, '127.0.0.1', r)); + const addr = server.address(); base = `http://127.0.0.1:${typeof addr === 'object' && addr ? addr.port : 0}`; +}); +afterEach(() => { server.close(); }); + +// Async on purpose: a sync spawn would block the loop that serves the fake API. +function run(headSha: string, opts: { token?: string } = {}): Promise<{ code: number; out: string; err: string }> { + return new Promise((done) => { + execFile(process.execPath, [SCRIPT, 'o', 'r', '7', headSha, join(dir, 'body.md')], { + env: { ...process.env, GITHUB_API: base, GH_TOKEN: opts.token ?? 't' }, encoding: 'utf8', timeout: 10_000, + }, (error, out, err) => done({ code: error && typeof (error as { code?: unknown }).code === 'number' ? (error as { code: number }).code : (error ? -1 : 0), out, err })); + }); +} +const mark = (sha: string, login?: string) => ``; +const seed = (sha: string, opts: { login?: string | null; author?: string; text?: string; updated?: string } = {}): Comment => { + const c: Comment = { id: fake.nextId++, body: `${mark(sha, opts.login === null ? undefined : (opts.login ?? 'relay[bot]'))}\n${opts.text ?? 'old'}`, user: { login: opts.author ?? 'relay[bot]', type: 'Bot' }, updated_at: opts.updated ?? '2026-01-01T00:00:00Z' }; + fake.comments.push(c); return c; +}; +const ours = () => fake.comments.filter((c) => c.body.startsWith('" is ours only +// when the comment's author IS that login — anyone can type the marker, +// nobody else can post as our identity. Legacy markers without "by:" (from +// the previously deployed copy) are adopted when authored by the same login +// as a comment we own, or by the login we just posted as. +// +// Ordering: a verdict for an older head never overwrites a newer one. GitHub's +// compare API decides (ahead/identical → theirs stays; behind → ours wins; +// diverged, i.e. rebase → the more recently committed head wins). A head the +// repo can no longer resolve loses. +// +// Delivery: only the POST/PATCH that carries the verdict is fatal. Cleanup, +// compare and re-read calls are best-effort and log instead of failing. + +const [owner, repo, number, headSha, bodyPath] = process.argv.slice(2); +const token = process.env.GH_TOKEN; +if (!owner || !repo || !number || !headSha || !bodyPath) { console.error("usage: pr-review-post.cjs "); process.exit(2); } +if (!token) { console.error("GH_TOKEN is not set; cannot post the review"); process.exit(1); } +const fs = require("fs"); +const API = (process.env.GITHUB_API || "https://api.github.com") + "/repos/" + owner + "/" + repo; +const MARK = ""; +/** {sha, login|null} for any marker line (new or legacy form), else null. */ +function readMark(c) { + if (!c || typeof c.body !== "string" || !c.user) return null; + const m = c.body.match(/^/); + return m ? { sha: m[1], login: m[2] || null } : null; +} +/** Marker comments that are provably ours; `knownLogins` adopts legacy/pending markers by author. */ +async function ours(knownLogins) { + const all = []; + for (let page = 1; page <= 50; page += 1) { + const list = await gh("GET", API + "/issues/" + number + "/comments?per_page=100&page=" + page); + for (const c of list) { const m = readMark(c); if (m) all.push({ c, m }); } + if (list.length < 100) break; + if (page === 50) { console.error("more than 5000 comments; refusing to post without a complete scan"); process.exit(1); } + } + const logins = new Set(knownLogins || []); + for (const x of all) if (x.m.login && x.m.login !== "pending" && x.c.user.login === x.m.login) logins.add(x.m.login); + return all.filter((x) => (x.m.login && x.m.login !== "pending") ? x.c.user.login === x.m.login : logins.has(x.c.user.login)); +} +/** Committer date in ms; null when the commit is gone (404); undefined on a transient failure. */ +async function committerDate(sha) { + const c = await gh("GET", API + "/commits/" + sha, undefined, { soft: true }); + if (failed(c)) return transient(c) ? undefined : null; + return Date.parse(c.commit.committer.date); +} +/** + * Does head `a` outrank head `b`? true / false, or "unknown" on a transient + * failure. A head the repo cannot resolve (404) loses; when compare fails + * for the pair, each head's existence decides. + */ +async function headWins(a, b) { + if (a === b) return true; + const cmp = await gh("GET", API + "/compare/" + b + "..." + a, undefined, { soft: true }); + if (failed(cmp)) { + if (transient(cmp)) return "unknown"; + const [da, db] = [await committerDate(a), await committerDate(b)]; + if (da === undefined || db === undefined) return "unknown"; + if (da === null && db === null) return "unknown"; + if (da === null) return false; + if (db === null) return true; + return da >= db; + } + if (cmp.status === "ahead" || cmp.status === "identical") return true; + if (cmp.status === "behind") return false; + const [da, db] = [await committerDate(a), await committerDate(b)]; + if (da === undefined || db === undefined) return "unknown"; + if (da === null) return false; + if (db === null) return true; + return da >= db; +} +/** Should the comment carrying `theirSha` stay? Unknown → yes (never overwrite on doubt). */ +async function theirsWins(theirSha) { const w = await headWins(theirSha, headSha); return w === true || w === "unknown"; } +/** Among our comments, the one whose head is newest; same head → lowest id. Unknown → keep both (returns null). */ +async function keeperOf(cands) { + let keep = cands[0]; + for (const other of cands.slice(1)) { + if (other.m.sha === keep.m.sha) { if (other.c.id < keep.c.id) keep = other; continue; } + const w = await headWins(other.m.sha, keep.m.sha); + if (w === "unknown") return null; + if (w === true) keep = other; + } + return keep; +} +async function softDelete(id) { await gh("DELETE", API + "/issues/comments/" + id, undefined, { soft: true }); console.log("deleted duplicate comment " + id); } + +(async () => { + let text = fs.readFileSync(bodyPath, "utf8"); + if (Buffer.byteLength(text) > 60000) text = Buffer.from(text).subarray(0, 60000).toString() + "\n\n_…truncated; the full review is in the run artifacts._\n"; + + // Identity first when the token allows (a user token does; an installation + // token may 403): it lets legacy markers be adopted on the first scan and + // makes the create path a single write. + const me = await gh("GET", (process.env.GITHUB_API || "https://api.github.com") + "/user", undefined, { soft: true }); + let found = await ours(me && me.login ? [me.login] : []); + if (found.length > 1) { + // Converge duplicates to one comment without discarding a newer verdict: + // keep the one whose head is newest (never "ours" by identity). If that + // cannot be decided right now, leave them all and try again next wake. + const keep = await keeperOf(found); + if (keep === null) { console.log("could not order the existing comments (transient); leaving them"); process.exit(0); } + for (const dup of found) if (dup.c.id !== keep.c.id) await softDelete(dup.c.id); + found = [keep]; + } + const existing = found[0]; + + if (!existing) { + const posted = await gh("POST", API + "/issues/" + number + "/comments", { body: markerFor(me && me.login ? me.login : "pending") + "\n" + text }); + const login = posted.user.login; + if (!(me && me.login)) await gh("PATCH", API + "/issues/comments/" + posted.id, { body: markerFor(login) + "\n" + text }, { soft: true }); + // Two wakes can both scan-then-create. Re-scan (adopting any by:pending + // orphans this login left) and converge on the comment whose head wins. + const again = (await ours([login])).filter((x) => x.c.user.login === login); + if (again.length > 1) { + const keep = await keeperOf(again); + if (keep === null) { console.log("posted comment " + posted.id + " for " + headSha + " (duplicates left for the next wake: transient)"); return; } + for (const other of again) if (other.c.id !== keep.c.id) await softDelete(other.c.id); + if (keep.c.id !== posted.id) { console.log("raced: kept " + keep.c.id + " (" + keep.m.sha.slice(0, 8) + "), deleted ours " + posted.id); return; } + } + console.log("posted comment " + posted.id + " for " + headSha); return; + } + + // Re-read right before writing; a vanished comment means "none exists". + const fresh = await gh("GET", API + "/issues/comments/" + existing.c.id, undefined, { soft: true }); + if (transient(fresh)) { console.log("could not re-read comment " + existing.c.id + " (transient); not writing"); return; } + if (failed(fresh)) { + const posted = await gh("POST", API + "/issues/" + number + "/comments", { body: markerFor(existing.c.user.login) + "\n" + text }); + console.log("re-posted comment " + posted.id + " for " + headSha + " (previous one vanished)"); return; + } + const cur = readMark(fresh); + if (cur && (await theirsWins(cur.sha))) { console.log("comment already carries " + cur.sha + "; not overwriting with " + headSha); return; } + await gh("PATCH", API + "/issues/comments/" + existing.c.id, { body: markerFor(existing.c.user.login) + "\n" + text }); + console.log("updated comment " + existing.c.id + " for " + headSha); +})(); diff --git a/workflows/pr-review.flow.ts b/workflows/pr-review.flow.ts index 95c6e904c..be8140dc1 100644 --- a/workflows/pr-review.flow.ts +++ b/workflows/pr-review.flow.ts @@ -6,8 +6,9 @@ // the same diff through lenses that match this codebase — the Rust kernel, // the TypeScript SDK/surface, and the examples + docs that make claims about // them — each gated on writing findings. A reconciliation step then looks for -// places two lenses disagree and posts the reconciled verdict as one PR -// comment, so the review is on the PR, not only in run artifacts. +// places two lenses disagree and publishes the reconciled verdict as the +// PR's single review comment (created once, edited in place on later wakes; +// see workflows/pr-review-post.cjs), so the review is on the PR itself. // // Local: flows run workflows/pr-review.flow.ts --local-agent \ // --input '{"diffRange":"origin/main...HEAD"}' @@ -115,11 +116,11 @@ export default flow( range = `FETCH_HEAD...${pr.headSha}`; } - // `review/` is not a dot-directory on purpose: the worker journals the - // artifacts it finds by scanning the tree, and that scan skips dot - // entries. It is recreated per run because `artifact_exists` records - // presence on content change, so a re-run writing the same "No findings" - // text into a leftover file would fail its gate. + // `review/` (gitignored) is not a dot-directory on purpose: the worker + // journals the artifacts it finds by scanning the tree, and that scan + // skips dot entries. It is recreated per run because `artifact_exists` + // records presence on content change, so a re-run writing the same "No + // findings" text into a leftover file would fail its gate. await f.run("rm -rf review && mkdir -p review"); // The diff goes through a file, not through f.run's return value: that // value is the kernel's stdout tail (64 KiB) and a task string is one @@ -182,26 +183,192 @@ export default flow( // The comment goes through the REST API with the repository token Cloud puts // in the sandbox as GH_TOKEN (the same credential git uses there). The // relayfile GitHub mount behind `f.github.*` is not attached to authored -// runs on Cloud — its first live run failed with -// helper_provider.mount_required at exactly this step. A deterministic step -// is not exactly-once, so the post is idempotent: the marker carries the -// head SHA and an existing comment for it means a re-dispatched step skips. -// The body is bounded from the front (f.run returns a 64 KiB stdout *tail*, -// which would drop the verdict) under GitHub's 65,536-char comment limit. -const shellNum = (n: number): string => String(n); +// runs on Cloud (cloud #3812) — its first live run failed with +// helper_provider.mount_required at exactly this step. +// +// One review comment per PR, edited in place: the marker names the head SHA +// reviewed; an existing marker comment is PATCHed (skipped when it already +// carries this SHA, so a re-dispatched step never double-posts), else one is +// POSTed. The body is bounded from the front (f.run returns a 64 KiB stdout +// *tail*, which would drop the verdict) under GitHub's 65,536-char limit. +// Embedded verbatim from workflows/pr-review-post.cjs — packages/sdk/tests/pr-review-post.test.ts +// fails if the two drift. The file is the source of truth; edit it, not this. +const POST_SCRIPT = ` +#!/usr/bin/env node +// pr-review-post — publish flows-pr-review's verdict as ONE comment per PR. +// +// Used by workflows/pr-review.flow.ts (which embeds this file verbatim and +// writes it into the run's scratch dir; packages/sdk/tests/pr-review-post.test.ts +// keeps the two in sync and exercises every branch against a fake GitHub). +// +// node pr-review-post.cjs +// env: GH_TOKEN (required), GITHUB_API (default https://api.github.com) +// +// Ownership: a marker "" is ours only +// when the comment's author IS that login — anyone can type the marker, +// nobody else can post as our identity. Legacy markers without "by:" (from +// the previously deployed copy) are adopted when authored by the same login +// as a comment we own, or by the login we just posted as. +// +// Ordering: a verdict for an older head never overwrites a newer one. GitHub's +// compare API decides (ahead/identical → theirs stays; behind → ours wins; +// diverged, i.e. rebase → the more recently committed head wins). A head the +// repo can no longer resolve loses. +// +// Delivery: only the POST/PATCH that carries the verdict is fatal. Cleanup, +// compare and re-read calls are best-effort and log instead of failing. + +const [owner, repo, number, headSha, bodyPath] = process.argv.slice(2); +const token = process.env.GH_TOKEN; +if (!owner || !repo || !number || !headSha || !bodyPath) { console.error("usage: pr-review-post.cjs "); process.exit(2); } +if (!token) { console.error("GH_TOKEN is not set; cannot post the review"); process.exit(1); } +const fs = require("fs"); +const API = (process.env.GITHUB_API || "https://api.github.com") + "/repos/" + owner + "/" + repo; +const MARK = ""; +/** {sha, login|null} for any marker line (new or legacy form), else null. */ +function readMark(c) { + if (!c || typeof c.body !== "string" || !c.user) return null; + const m = c.body.match(/^/); + return m ? { sha: m[1], login: m[2] || null } : null; +} +/** Marker comments that are provably ours; \`knownLogins\` adopts legacy/pending markers by author. */ +async function ours(knownLogins) { + const all = []; + for (let page = 1; page <= 50; page += 1) { + const list = await gh("GET", API + "/issues/" + number + "/comments?per_page=100&page=" + page); + for (const c of list) { const m = readMark(c); if (m) all.push({ c, m }); } + if (list.length < 100) break; + if (page === 50) { console.error("more than 5000 comments; refusing to post without a complete scan"); process.exit(1); } + } + const logins = new Set(knownLogins || []); + for (const x of all) if (x.m.login && x.m.login !== "pending" && x.c.user.login === x.m.login) logins.add(x.m.login); + return all.filter((x) => (x.m.login && x.m.login !== "pending") ? x.c.user.login === x.m.login : logins.has(x.c.user.login)); +} +/** Committer date in ms; null when the commit is gone (404); undefined on a transient failure. */ +async function committerDate(sha) { + const c = await gh("GET", API + "/commits/" + sha, undefined, { soft: true }); + if (failed(c)) return transient(c) ? undefined : null; + return Date.parse(c.commit.committer.date); +} +/** + * Does head \`a\` outrank head \`b\`? true / false, or "unknown" on a transient + * failure. A head the repo cannot resolve (404) loses; when compare fails + * for the pair, each head's existence decides. + */ +async function headWins(a, b) { + if (a === b) return true; + const cmp = await gh("GET", API + "/compare/" + b + "..." + a, undefined, { soft: true }); + if (failed(cmp)) { + if (transient(cmp)) return "unknown"; + const [da, db] = [await committerDate(a), await committerDate(b)]; + if (da === undefined || db === undefined) return "unknown"; + if (da === null && db === null) return "unknown"; + if (da === null) return false; + if (db === null) return true; + return da >= db; + } + if (cmp.status === "ahead" || cmp.status === "identical") return true; + if (cmp.status === "behind") return false; + const [da, db] = [await committerDate(a), await committerDate(b)]; + if (da === undefined || db === undefined) return "unknown"; + if (da === null) return false; + if (db === null) return true; + return da >= db; +} +/** Should the comment carrying \`theirSha\` stay? Unknown → yes (never overwrite on doubt). */ +async function theirsWins(theirSha) { const w = await headWins(theirSha, headSha); return w === true || w === "unknown"; } +/** Among our comments, the one whose head is newest; same head → lowest id. Unknown → keep both (returns null). */ +async function keeperOf(cands) { + let keep = cands[0]; + for (const other of cands.slice(1)) { + if (other.m.sha === keep.m.sha) { if (other.c.id < keep.c.id) keep = other; continue; } + const w = await headWins(other.m.sha, keep.m.sha); + if (w === "unknown") return null; + if (w === true) keep = other; + } + return keep; +} +async function softDelete(id) { await gh("DELETE", API + "/issues/comments/" + id, undefined, { soft: true }); console.log("deleted duplicate comment " + id); } + +(async () => { + let text = fs.readFileSync(bodyPath, "utf8"); + if (Buffer.byteLength(text) > 60000) text = Buffer.from(text).subarray(0, 60000).toString() + "\n\n_…truncated; the full review is in the run artifacts._\n"; + + // Identity first when the token allows (a user token does; an installation + // token may 403): it lets legacy markers be adopted on the first scan and + // makes the create path a single write. + const me = await gh("GET", (process.env.GITHUB_API || "https://api.github.com") + "/user", undefined, { soft: true }); + let found = await ours(me && me.login ? [me.login] : []); + if (found.length > 1) { + // Converge duplicates to one comment without discarding a newer verdict: + // keep the one whose head is newest (never "ours" by identity). If that + // cannot be decided right now, leave them all and try again next wake. + const keep = await keeperOf(found); + if (keep === null) { console.log("could not order the existing comments (transient); leaving them"); process.exit(0); } + for (const dup of found) if (dup.c.id !== keep.c.id) await softDelete(dup.c.id); + found = [keep]; + } + const existing = found[0]; + + if (!existing) { + const posted = await gh("POST", API + "/issues/" + number + "/comments", { body: markerFor(me && me.login ? me.login : "pending") + "\n" + text }); + const login = posted.user.login; + if (!(me && me.login)) await gh("PATCH", API + "/issues/comments/" + posted.id, { body: markerFor(login) + "\n" + text }, { soft: true }); + // Two wakes can both scan-then-create. Re-scan (adopting any by:pending + // orphans this login left) and converge on the comment whose head wins. + const again = (await ours([login])).filter((x) => x.c.user.login === login); + if (again.length > 1) { + const keep = await keeperOf(again); + if (keep === null) { console.log("posted comment " + posted.id + " for " + headSha + " (duplicates left for the next wake: transient)"); return; } + for (const other of again) if (other.c.id !== keep.c.id) await softDelete(other.c.id); + if (keep.c.id !== posted.id) { console.log("raced: kept " + keep.c.id + " (" + keep.m.sha.slice(0, 8) + "), deleted ours " + posted.id); return; } + } + console.log("posted comment " + posted.id + " for " + headSha); return; + } + + // Re-read right before writing; a vanished comment means "none exists". + const fresh = await gh("GET", API + "/issues/comments/" + existing.c.id, undefined, { soft: true }); + if (transient(fresh)) { console.log("could not re-read comment " + existing.c.id + " (transient); not writing"); return; } + if (failed(fresh)) { + const posted = await gh("POST", API + "/issues/" + number + "/comments", { body: markerFor(existing.c.user.login) + "\n" + text }); + console.log("re-posted comment " + posted.id + " for " + headSha + " (previous one vanished)"); return; + } + const cur = readMark(fresh); + if (cur && (await theirsWins(cur.sha))) { console.log("comment already carries " + cur.sha + "; not overwriting with " + headSha); return; } + await gh("PATCH", API + "/issues/comments/" + existing.c.id, { body: markerFor(existing.c.user.login) + "\n" + text }); + console.log("updated comment " + existing.c.id + " for " + headSha); +})(); +`; async function postComment(f: Ctx, pr: Pr): Promise { - const api = `https://api.github.com/repos/${REPO.owner}/${REPO.repo}/issues/${shellNum(pr.number)}/comments`; - const marker = ``; + await f.run("cat > review/post.cjs <<'EOF'\n" + POST_SCRIPT + "EOF\n"); await f.run( - `[ -n "$GH_TOKEN" ] || { echo "GH_TOKEN is not set; cannot post the review" >&2; exit 1; }; ` - // Walk every page of comments (busy PRs exceed one page) before posting. - + `page=1; while :; do out=$(curl -sf -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" ${shellWord(`${api}?per_page=100&page=`)}"$page") || exit 1; ` - + `if printf '%s' "$out" | grep -qF ${shellWord(marker)}; then echo "already posted for ${pr.headSha}"; exit 0; fi; ` - // The page is one JSON line: count occurrences, not lines. - + `[ "$(printf '%s' "$out" | grep -o '"node_id"' | wc -l)" -ge 100 ] || break; page=$((page+1)); [ "$page" -le 50 ] || { echo "more than 5000 comments; refusing to post without a complete marker scan" >&2; exit 1; }; done; ` - + `{ printf '%s\n' ${shellWord(marker)}; if [ "$(wc -c < ${CONSENSUS})" -gt 60000 ]; then head -c 60000 ${CONSENSUS}; printf '\n\n_…truncated; the full review is in the run artifacts (${CONSENSUS})._\n'; else cat ${CONSENSUS}; fi; } > review/comment.md && ` - + `node -e 'const fs=require("fs");process.stdout.write(JSON.stringify({body:fs.readFileSync("review/comment.md","utf8")}))' > review/comment.json && ` - + `curl -sf -X POST -H "Authorization: Bearer $GH_TOKEN" -H "Accept: application/vnd.github+json" ${shellWord(api)} --data-binary @review/comment.json > /dev/null`, + `node review/post.cjs ${shellWord(REPO.owner)} ${shellWord(REPO.repo)} ${shellWord(String(pr.number))} ${shellWord(pr.headSha)} ${shellWord(CONSENSUS)}`, { timeout: "2m" }, ); }