diff --git a/.github/scripts/pr-review.mjs b/.github/scripts/pr-review.mjs index 22d1fd9..14a1c77 100644 --- a/.github/scripts/pr-review.mjs +++ b/.github/scripts/pr-review.mjs @@ -191,9 +191,9 @@ export function effectiveDiffStats(numstat, patterns = DEFAULT_EXCLUDES) { } export function reviewProfile({ files, lines }) { - if (files <= 5 && lines <= 300) return { tier: "small", effort: "low", concurrency: 4, maxTokensBudget: 80_000 }; - if (files <= 20 && lines <= 1500) return { tier: "medium", effort: "medium", concurrency: 4, maxTokensBudget: 200_000 }; - return { tier: "large", effort: "medium", concurrency: 6, maxTokensBudget: 400_000 }; + if (files <= 5 && lines <= 300) return { tier: "small", effort: "low", concurrency: 4, maxTokensBudget: 400_000 }; + if (files <= 20 && lines <= 1500) return { tier: "medium", effort: "medium", concurrency: 4, maxTokensBudget: 1_200_000 }; + return { tier: "large", effort: "medium", concurrency: 6, maxTokensBudget: 3_000_000 }; } export function normalizeLLMBaseURL(value, protocol) { @@ -596,13 +596,24 @@ export async function reviewWithOCR({ binary, repo, from, to, rule, background, const args = ["review", "--repo", repo, "--from", from, "--to", to, "--rule", rule, "--background-file", background, "--format", "json", "--audience", "agent", "--effort", profile.effort, "--concurrency", String(profile.concurrency), "--timeout", "10", "--max-tokens-budget", String(profile.maxTokensBudget)]; - const result = processRunner(binary, args, { cwd: home, env: { ...baseEnv, ...credentials }, timeout: 720_000 }); + const result = processRunner(binary, args, { cwd: home, env: { ...baseEnv, ...credentials }, timeout: 1_500_000 }); if (result.error || result.status !== 0) { + // OCR exits non-zero when any item hits a provider error or timeout, but still emits the completed items. + let emitted; + try { + emitted = JSON.parse(result.stdout ?? ""); + } catch { + emitted = null; + } + if (!result.error && emitted?.status === "partial") return parseOCRJSON(result.stdout); + const failedItems = emitted?.manifest?.coverage?.failed; + const causes = new Set((Array.isArray(failedItems) ? failedItems : []).map((item) => item?.classification) + .filter((cause) => /^[a-z_]{1,30}$/.test(cause))); const stderr = result.stderr ?? ""; const hint = /background (?:file|content)|read background file/i.test(stderr) ? "; background rejected" : /resolve (?:OCR|LLM)|LLM endpoint|auth_token_cmd/i.test(stderr) ? "; LLM configuration rejected" : /unknown flag|invalid argument/i.test(stderr) ? "; CLI arguments rejected" : ""; - throw new Error("OCR review failed (" + safeExit(result) + hint + "; " + (result.stdout?.length ?? 0) + " stdout characters)"); + throw new Error("OCR review failed (" + safeExit(result) + hint + (causes.size ? "; failed items: " + [...causes].join(", ") : "") + "; " + (result.stdout?.length ?? 0) + " stdout characters)"); } return parseOCRJSON(result.stdout ?? ""); } @@ -788,6 +799,8 @@ export async function run() { const assessment = await reviewWithOCR({ binary: required("PR_REVIEW_OCR_BIN"), repo, from: range.from, to: range.to, rule, background, home, profile, language, }); + console.log("OpenCodeReview finished: " + assessment.status + ", " + (Number(assessment.summary?.total_tokens) || 0) + + " of " + profile.maxTokensBudget + " budgeted tokens"); const locations = new Map(); for (const file of files) { const anchors = diffAnchors(file.patch); diff --git a/.github/scripts/pr-review.test.mjs b/.github/scripts/pr-review.test.mjs index 1fca574..b2e3577 100644 --- a/.github/scripts/pr-review.test.mjs +++ b/.github/scripts/pr-review.test.mjs @@ -110,9 +110,9 @@ test("effective diff statistics exclude generated files and locks before determi "-\t-\tassets/logo.png", ].join("\0") + "\0"); assert.deepEqual(stats, { files: 2, lines: 15 }); - assert.deepEqual(reviewProfile({ files: 5, lines: 300 }), { tier: "small", effort: "low", concurrency: 4, maxTokensBudget: 80_000 }); - assert.deepEqual(reviewProfile({ files: 20, lines: 1500 }), { tier: "medium", effort: "medium", concurrency: 4, maxTokensBudget: 200_000 }); - assert.deepEqual(reviewProfile({ files: 21, lines: 1 }), { tier: "large", effort: "medium", concurrency: 6, maxTokensBudget: 400_000 }); + assert.deepEqual(reviewProfile({ files: 5, lines: 300 }), { tier: "small", effort: "low", concurrency: 4, maxTokensBudget: 400_000 }); + assert.deepEqual(reviewProfile({ files: 20, lines: 1500 }), { tier: "medium", effort: "medium", concurrency: 4, maxTokensBudget: 1_200_000 }); + assert.deepEqual(reviewProfile({ files: 21, lines: 1 }), { tier: "large", effort: "medium", concurrency: 6, maxTokensBudget: 3_000_000 }); assert.equal(reviewProfile({ files: 1, lines: 1501 }).tier, "large"); }); @@ -376,7 +376,7 @@ test("fake OCR executable receives complete arguments and an isolated credential assert.deepEqual(reviewCall.args, [ "review", "--repo", options.repo, "--from", A, "--to", B, "--rule", options.rule, "--background-file", options.background, "--format", "json", "--audience", "agent", "--effort", "low", - "--concurrency", "4", "--timeout", "10", "--max-tokens-budget", "80000", + "--concurrency", "4", "--timeout", "10", "--max-tokens-budget", "400000", ]); for (const call of calls) { assert.equal(call.env.HOME, options.home); @@ -407,6 +407,21 @@ test("fake OCR executable receives complete arguments and an isolated credential } }); +test("a nonzero OCR exit keeps the partial result it emitted", async (t) => { + const fake = await fakeOCR(t, { exitCode: 1, stdout: JSON.stringify({ status: "partial", comments: [{ path: "a.go" }], manifest: {} }) }); + await mkdir(join(fake.directory, "home"), { recursive: true }); + const result = await reviewWithOCR(ocrOptions(fake)); + assert.equal(result.status, "partial"); + assert.equal(result.comments.length, 1); +}); + +test("a fully failed OCR review reports why its items failed", async (t) => { + const failed = [{ classification: "budget" }, { classification: "budget" }, { classification: "llm-secret 1" }]; + const fake = await fakeOCR(t, { exitCode: 1, stdout: JSON.stringify({ status: "failed", comments: [], manifest: { coverage: { failed } } }) }); + await mkdir(join(fake.directory, "home"), { recursive: true }); + await assert.rejects(reviewWithOCR(ocrOptions(fake)), /exit 1; failed items: budget; /); +}); + test("fake OCR nonzero exits and invalid JSON fail with sanitized diagnostics", async (t) => { const failed = await fakeOCR(t, { exitCode: 7 }); await mkdir(join(failed.directory, "home"), { recursive: true }); diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml index e79a2fe..0d08873 100644 --- a/.github/workflows/pr-review.yml +++ b/.github/workflows/pr-review.yml @@ -26,7 +26,7 @@ jobs: name: Reims Go Reviewer if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && (github.event_name == 'workflow_dispatch' || !github.event.pull_request.draft) runs-on: ubuntu-latest - timeout-minutes: 15 + timeout-minutes: 30 steps: - name: Create reviewer App token id: app-token diff --git a/changes/pr-review-budget.json b/changes/pr-review-budget.json new file mode 100644 index 0000000..0531c22 --- /dev/null +++ b/changes/pr-review-budget.json @@ -0,0 +1,7 @@ +{ + "group": "chores", + "order": 0, + "authors": ["Nwflower"], + "zh": "提高 PR 自动审查的 token 预算,部分任务出错时仍发布已完成的审查结果。", + "en": "Raise the token budget for automated PR reviews and publish completed results when some review tasks fail." +} diff --git a/docs/pr-review-guidelines.md b/docs/pr-review-guidelines.md index 204f270..dbee270 100644 --- a/docs/pr-review-guidelines.md +++ b/docs/pr-review-guidelines.md @@ -69,7 +69,7 @@ 工作流从 `${{ github.workflow_sha }}` 检出审核脚本、规则和规范文档,不读取 PR 分支上的这些文件。PR 快照只检出到 Runner 临时目录,禁止 hooks、系统和全局 Git 配置,不执行 PR 中的脚本或构建命令。GitHub App token 通过短期 Git HTTP header 环境变量用于浅拉提交,OCR 子进程只获得隔离的 `HOME` / `XDG_*`、运行必需变量和 LLM 凭据。工作流不上传 artifact,也不使用外部持久化服务。 -上一轮 bot Review 的 `reims-pr-review` marker 记录已审 `head_sha` 与 `base_sha`。线性推进且基线兼容时只审上次提交到当前提交的增量区间;首次审核、上一轮部分完成、force-push、rebase、无法证明祖先关系及手动重审均回退到 merge-base 起的完整 PR 区间。相同快照自动跳过重复审核。审核范围排除生成文件和锁文件后,按有效文件数与变更行数选择 low/medium effort、4/6 路并发和 80,000/200,000/400,000 tokens 的总预算。 +上一轮 bot Review 的 `reims-pr-review` marker 记录已审 `head_sha` 与 `base_sha`。线性推进且基线兼容时只审上次提交到当前提交的增量区间;首次审核、上一轮部分完成、force-push、rebase、无法证明祖先关系及手动重审均回退到 merge-base 起的完整 PR 区间。相同快照自动跳过重复审核。审核范围排除生成文件和锁文件后,按有效文件数与变更行数选择 low/medium effort、4/6 路并发和 400,000/1,200,000/3,000,000 tokens 的总预算。预算用尽或部分任务出错时,已完成部分仍会发布,并标明覆盖不完整。Actions 日志记录每次审核的实际 token 用量。 OCR 输出中的 `critical`、`high`、`medium` 且非 style/documentation 问题按严重度排序,单轮最多发布六条行内评论。每条先写触发条件和实际后果,再给出可核对的证据和简短修复方向。仅在定位或核对问题时引用代码名称,正文省略代码块。默认省略可一键应用的代码建议。无法锚定到 GitHub 当前 PR diff 的有效问题及超出数量的问题写入摘要,`low` 级发现不发布。GitHub 返回 422 时自动改为纯摘要 Review,保留全部已准备的意见。