diff --git a/.github/scripts/pr-review.mjs b/.github/scripts/pr-review.mjs index 3340725..0633d50 100644 --- a/.github/scripts/pr-review.mjs +++ b/.github/scripts/pr-review.mjs @@ -8,7 +8,8 @@ const MAX_FILES = 300; const MAX_DIFF_LENGTH = 400_000; const MAX_FILE_LENGTH = 36_000; const CI_WAIT_MS = 240_000; -const MAX_FINDINGS = 8; +const MAX_FINDINGS = 5; +const MAX_PENDING_CHECKS = 3; const HUNK = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/; const REVIEW_SECTIONS = { value: "合并价值与功能完整性", @@ -111,12 +112,13 @@ const instructions = [ "5. 结合该 PR 当前提交的 CI 记录核对已执行的关键流程,列出尚需人工或真机实测的用例,每条写清入口、旧数据条件或预期结果。只承认输入中明确通过的步骤。", "CI 记录对应 PR 的 head 提交。若主线同一流程已有新改动,或 PR 修改了 CI 本身,检查合入当前主线还需要哪些验证。", "最后用实际证据给出合并建议。缺少关键测试、主线对照或重要文件时标明限制。存在阻塞问题时指出合并前必须解决的事项。", - "仅输出 JSON 对象,包含 value、upgrade、ui、implementation 四个简体中文短段落,decision、reason、pending_checks 和 findings。", - "decision 为 merge、changes、verify 或 decline,reason 用一句话给出依据。pending_checks 为最多 5 条待实测项目,全部有证据时为空数组。无行内发现时 findings 为空数组。", + "只返回一个 JSON 对象,不加前言、后记或 Markdown。包含 value、upgrade、ui、implementation 四项简体中文短结论,以及 decision、reason、pending_checks 和 findings。", + "四项短结论每项最多 75 字。decision 为 merge、changes、verify 或 decline,reason 用一句话给出依据,最多 110 字。", + "pending_checks 最多 3 条,每条最多 75 字。全部有证据时返回空数组。无行内发现时 findings 为空数组。", "行内发现只报告有证据的可操作问题,避免格式偏好、猜测或可执行的攻击步骤。", "每条包含 path、side、line、priority、body。side 为 RIGHT(新增)或 LEFT(删除)。", "line 使用 diff hunk 对应的文件行号,只能锚定到改动行。priority 为 P1、P2 或 P3。", - "最多 8 条。body 简体中文,简述触发条件、影响及建议,使用纯文本,不抄录代码或外部链接。", + "行内发现按影响从大到小排列,最多 5 条。body 每条最多 130 字,简述触发条件、影响及建议,使用纯文本,不抄录代码或外部链接。", ].join("\n"); async function reviewDiffs(context, includeDesignGuidelines) { @@ -146,7 +148,8 @@ async function reviewDiffs(context, includeDesignGuidelines) { }, }, agent: { - "pr-review": { mode: "primary", steps: 1, prompt: instructions + "\n\n" + guidelines }, + // OpenCode injects a prose summary instruction on the last configured step. + "pr-review": { mode: "primary", steps: 2, prompt: guidelines + "\n\n" + instructions }, }, permission: { "*": "deny" }, share: "disabled", @@ -217,13 +220,7 @@ async function reviewDiffs(context, includeDesignGuidelines) { throw new Error("OpenCode did not return a complete text-only review " + JSON.stringify(diagnostic)); } const answer = text.map((event) => event.part.text).join("\n"); - let decoded; - try { - const content = answer.trim().replace(/^```(?:json)?\s*\n([\s\S]*?)\n```$/i, "$1"); - decoded = JSON.parse(content); - } catch { - throw new Error("LLM review is not valid JSON"); - } + const decoded = parseReviewJSON(answer); if (!Object.keys(REVIEW_SECTIONS).every((key) => typeof decoded?.[key] === "string" && decoded[key].trim()) || typeof decoded.decision !== "string" || !Object.hasOwn(DECISIONS, decoded.decision) || typeof decoded.reason !== "string" || !decoded.reason.trim() @@ -232,6 +229,24 @@ async function reviewDiffs(context, includeDesignGuidelines) { return decoded; } +function parseReviewJSON(answer) { + const fenced = [...answer.matchAll(/```(?:json)?[^\S\n]*\n([\s\S]*?)\n```/gi)].map((match) => match[1]); + const object = answer.slice(answer.indexOf("{"), answer.lastIndexOf("}") + 1); + for (const candidate of [answer, ...fenced, object]) { + try { + return JSON.parse(candidate); + } catch { + continue; + } + } + throw new Error("LLM review is not valid JSON (" + answer.length + " text characters)"); +} + +function shortText(value, limit) { + const characters = Array.from(value.trim()); + return characters.length > limit ? characters.slice(0, limit - 1).join("") + "…" : characters.join(""); +} + function plainMarkdown(value) { return value .replace(/[\u0000-\u001f\u007f-\u009f\u202a-\u202e\u2066-\u2069]+/g, " ") @@ -256,7 +271,7 @@ function inlineComments(findings, locations) { seen.add(key); comments.push({ path, side, line, - body: "**[" + finding.priority + "]** " + plainMarkdown(finding.body.trim().slice(0, 900)), + body: "**[" + finding.priority + "]** " + plainMarkdown(shortText(finding.body, 130)), }); } return comments; @@ -345,7 +360,7 @@ function reviewSummary(assessment, ci, incomplete, mainMoved, mergeable) { const lines = []; if (assessment) { for (const [key, label] of Object.entries(REVIEW_SECTIONS)) { - lines.push("- **" + label + "** " + plainMarkdown(assessment[key].trim().slice(0, 1000))); + lines.push("- **" + label + "** " + plainMarkdown(shortText(assessment[key], 75))); } } @@ -356,14 +371,14 @@ function reviewSummary(assessment, ci, incomplete, mainMoved, mergeable) { lines.push("- " + plainMarkdown(step.name) + " · " + plainMarkdown(step.conclusion ?? "未执行")); } - const pending = (assessment?.pending_checks ?? []).map((check) => check.trim()).filter(Boolean).slice(0, 5); + const pending = (assessment?.pending_checks ?? []).map((check) => check.trim()).filter(Boolean).slice(0, MAX_PENDING_CHECKS); if (pending.length) { lines.push("\n**待实测**"); - for (const check of pending) lines.push("- " + plainMarkdown(check.slice(0, 800))); + for (const check of pending) lines.push("- " + plainMarkdown(shortText(check, 75))); } let decision = assessment?.decision ?? "verify"; - let reason = assessment?.reason.trim().slice(0, 800) ?? "没有可审核的文本差异。"; + let reason = assessment ? shortText(assessment.reason, 110) : "没有可审核的文本差异。"; const hasTestEvidence = ci.conclusion === "success" && ci.steps?.some((step) => step.conclusion === "success"); if (mergeable === false) { decision = "changes"; diff --git a/docs/pr-review-guidelines.md b/docs/pr-review-guidelines.md index b0a8918..9f9c2dd 100644 --- a/docs/pr-review-guidelines.md +++ b/docs/pr-review-guidelines.md @@ -68,4 +68,4 @@ 如果公开仓库的 Actions 事件策略阻止 `pull_request_target`,由仓库管理员在 **Settings → Actions → Policies** 为 `.github/workflows/pr-review.yml` 配置允许该事件的策略。工作流从默认分支加载审核脚本,将 GitHub API 返回的差异作为文本交给 [OpenCode](https://docs.bigmodel.cn/cn/coding-plan/tool/opencode),模型工具全部禁用。GitHub 写入权限仅用于提交审核意见。[GitHub 事件策略文档](https://docs.github.com/en/actions/how-tos/administer/control-workflow-execution) -工作流合入默认分支并配置密钥后,可在 **Actions → PR Review → Run workflow** 输入一个开放 PR 的编号。单次读取最多 300 个变更文件,PR 文本差异总量约 40 万字符,单文件约 3.6 万字符。审核会显示范围,使用 512,000 tokens 的模型上下文预算和 32,768 tokens 的输出上限。请求采用低档推理强度,给审核正文保留输出预算。PR 版本、对照主线和 CI 结果相同时会跳过重复审核。缺少密钥时跳过,模型请求或发布失败时在 Actions 中显示失败。 +工作流合入默认分支并配置密钥后,可在 **Actions → PR Review → Run workflow** 输入一个开放 PR 的编号。单次读取最多 300 个变更文件,PR 文本差异总量约 40 万字符,单文件约 3.6 万字符。审核会显示范围,使用 512,000 tokens 的模型上下文预算和 32,768 tokens 的输出上限。请求采用低档推理强度,给审核正文保留输出预算。公开审核摘要包含四项短结论和最多三条待实测项,随附最多五条行内意见。PR 版本、对照主线和 CI 结果相同时会跳过重复审核。缺少密钥时跳过,模型请求或发布失败时在 Actions 中显示失败。