From d28b97c4eae0f571a7ec6597018bcd6a6099ecd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=B7=9D=E7=99=BD=E6=B5=AA?= <1679659@qq.com> Date: Fri, 4 Dec 2026 01:37:31 -0700 Subject: [PATCH] ci: fix the review continuation the reviewer found The review switch read the previous-review record before it was fetched, so every automatic review would have thrown before publishing anything. Also fixed: a reused checkout now drops files the new head deleted; the round loop merges every round's findings instead of keeping only the last; the session cleanup honours its keep switch and leaves other files alone; the state cache keeps one entry per pull request instead of one per run; and the job timeout covers all three review rounds. A resumed review still keeps its full budget. Co-Authored-By: Claude Opus 5.5 --- .github/scripts/pr-review.mjs | 51 ++++++++++++++++++++---------- .github/scripts/pr-review.test.mjs | 18 +++++++++++ .github/workflows/pr-review.yml | 11 ++++--- 3 files changed, 60 insertions(+), 20 deletions(-) diff --git a/.github/scripts/pr-review.mjs b/.github/scripts/pr-review.mjs index cea3a04..2cd68a5 100644 --- a/.github/scripts/pr-review.mjs +++ b/.github/scripts/pr-review.mjs @@ -609,7 +609,9 @@ export function configureOCR({ binary, home, language, parentEnv = process.env, // Where OCR keeps its own state for one PR, and where the run's checkout lives. // The key carries the PR number: one checkout holds every PR number in turn, so -// the repository directory alone means "the last run, whoever it was". +// the repository directory alone means "the last run, whoever it was". The +// workflow serializes runs of one PR (cancel-in-progress), so two runs never +// share these directories at once. export function resumeSessionDir(settings = process.env) { const stateDirectory = settings.PR_REVIEW_STATE_DIR; const home = settings.PR_REVIEW_SESSION_HOME?.trim(); @@ -674,14 +676,12 @@ async function deleteOldSessions(home, repo, session) { } catch { return; } - for (const name of names) { - if (name === session + ".jsonl" || name.endsWith(".lock")) continue; - await rm(join(directory, name), { force: true, recursive: true }); - } - // Earlier session files of this PR still hold reviewed file content. + // Only session files are managed here. Anything else in the directory is + // left alone, and keeping the records can be asked for while debugging. if (process.env.PR_REVIEW_KEEP_SESSIONS === "true") return; for (const name of names) { - if (/^[0-9a-f-]{36}\.jsonl$/.test(name) && name !== session + ".jsonl") await rm(join(directory, name), { force: true }); + if (name === session + ".jsonl" || !name.endsWith(".jsonl")) continue; + await rm(join(directory, name), { force: true }); } } @@ -736,6 +736,21 @@ function checkedSHA(value, name) { return value.toLowerCase(); } +// Continuing an incomplete review keeps its range, so it is allowed only for +// the very revision that review was computed over. Findings it already +// published cover that revision; a new push starts a full review instead. +export function resumeTarget(previous, headSha, enabled) { + return enabled && previous?.incomplete === true && (previous.headSha ?? "").toLowerCase() === String(headSha ?? "").toLowerCase(); +} + +// An incomplete review never anchors an incremental run: it did not finish the +// range it started, so a later commit range would leave that work undone. It +// may still be continued over its own range. +export function previousReviewFor(previous, resume) { + if (!previous) return null; + return !previous.incomplete || resume ? previous : null; +} + export async function selectReviewRange({ previousReview, isManual, baseSha, headSha, mergeBase, fetchCheckpoint, isAncestor }) { const full = { from: mergeBase, to: headSha, incremental: false }; const lastHeadSha = previousReview?.headSha; @@ -792,6 +807,9 @@ async function prepareGitWorkspace({ repo, home, repository, number, pull, token if (!mergeBase) throw new Error("Git could not determine the PR merge base"); git(repo, ["config", "core.symlinks", "false"], env); git(repo, ["checkout", "--force", "--detach", headSha], env); + // A checkout reused between runs keeps files the new head deleted; OCR scans + // the worktree, so only tracked files of this head may remain. + git(repo, ["clean", "-ffdx"], env); // Keep PR-owned OCR settings out of the filesystem configuration search. await rm(join(repo, ".opencodereview"), { recursive: true, force: true }); return { @@ -862,15 +880,6 @@ export async function run() { return; } const isManual = !event.pull_request; - // A review that stopped short still settled some files, and its findings were - // already published. It can be continued only for the very same revision: a - // new push moves the range, and a resumed run must review exactly the range - // its checkpoints were computed over. Anything else starts a full review. - const resumePrevious = process.env.PR_REVIEW_REUSE_INCOMPLETE === "true" && history.previousReview?.incomplete === true - && (history.previousReview.headSha ?? "").toLowerCase() === pull.head.sha.toLowerCase(); - // An incomplete review never anchors an incremental run: it did not finish the - // range it started, so a later commit range would leave that work undone. - const rangePrevious = history.previousReview && (!history.previousReview.incomplete || resumePrevious) ? history.previousReview : null; const headCommitMessage = !isManual && pull.draft ? "" : await fetchHeadCommitMessage(github, repoPath, pull.head.sha); const skipReason = shouldSkipAutoReview({ isManual, draft: Boolean(pull.draft), commitMessage: headCommitMessage }); if (skipReason) { @@ -886,6 +895,8 @@ export async function run() { console.log("This PR revision has already been reviewed"); return; } + const resumePrevious = resumeTarget(history.previousReview, pull.head.sha, process.env.PR_REVIEW_REUSE_INCOMPLETE === "true"); + const rangePrevious = previousReviewFor(history.previousReview, resumePrevious); const trustedRoot = process.cwd(); const rule = join(trustedRoot, ".opencodereview", "rule.json"); const ruleConfig = JSON.parse(await readFile(rule, "utf8")); @@ -927,6 +938,10 @@ export async function run() { // Resuming keeps the range, which is why an explicitly reused incomplete // review continues over the range that produced it. const earlier = resumePrevious ? revalidationOf(readResumeState(stateDirectory), range, home, repo) : null; + // Each round returns what the session knows, so the rounds are merged by + // identity. A finding published once is published once, whichever round + // reported it. + const findings = new Map(); let assessment; let resumed = 0; for (;;) { @@ -937,6 +952,9 @@ export async function run() { }); console.log("OpenCodeReview finished: " + assessment.status + ", " + (Number(assessment.summary?.total_tokens) || 0) + " of " + profile.maxTokensBudget + " budgeted tokens" + (session ? " (resumed)" : "")); + for (const finding of assessment.comments) { + findings.set([finding.path, finding.line ?? finding.original_line ?? "", finding.content].join("\u0000"), finding); + } if (assessment.session_id) { await writeResumeState(stateDirectory, { session: assessment.session_id, from: range.from, to: range.to }); } @@ -947,6 +965,7 @@ export async function run() { console.log("Continuing the review from session " + assessment.session_id); } if (assessment.session_id) await deleteOldSessions(home, repo, assessment.session_id); + assessment = { ...assessment, comments: [...findings.values()] }; 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 c17c03c..b6f1403 100644 --- a/.github/scripts/pr-review.test.mjs +++ b/.github/scripts/pr-review.test.mjs @@ -17,7 +17,9 @@ import { normalizeLLMBaseURL, encodeRepoPath, parseOCRJSON, + previousReviewFor, readResumeState, + resumeTarget, resumeSessionDir, revalidationOf, writeResumeState, @@ -527,3 +529,19 @@ test("an incomplete previous review is recognised and never anchors an increment const complete = [{ user: { login: "reims-go-reviewer[bot]" }, state: "COMMENTED", commit_id: B, body: marker.replace(',"incomplete":true', '') + "\n## 自动代码审核" }]; assert.equal(extractPreviousReview(complete, [], "reims-go-reviewer[bot]").incomplete, false); }); + +test("only the same revision may continue an incomplete review", () => { + const incomplete = { incomplete: true, headSha: B }; + assert.equal(resumeTarget(incomplete, B, true), true); + assert.equal(resumeTarget(incomplete, B.toUpperCase(), true), true); + assert.equal(resumeTarget(incomplete, B, false), false, "the switch is off by default"); + assert.equal(resumeTarget(incomplete, A, true), false, "a new push starts a full review"); + assert.equal(resumeTarget({ incomplete: false, headSha: B }, B, true), false); + assert.equal(resumeTarget(null, B, true), false); + assert.equal(previousReviewFor(incomplete, false), null, "an unfinished review cannot anchor an incremental range"); + assert.equal(previousReviewFor(incomplete, true), incomplete); + assert.equal(previousReviewFor({ incomplete: false }, false)?.incomplete, false); + assert.equal(previousReviewFor(null, true), null); + const range = previousReviewFor(incomplete, resumeTarget(incomplete, B, true)); + assert.equal(range, incomplete, "continuing keeps the range the review was computed over"); +}); diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml index d9f322b..9e366a1 100644 --- a/.github/workflows/pr-review.yml +++ b/.github/workflows/pr-review.yml @@ -26,7 +26,8 @@ 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: 30 + # Up to three review rounds, each bounded by the OCR timeout. + timeout-minutes: 90 steps: - name: Create reviewer App token id: app-token @@ -62,13 +63,15 @@ jobs: # Keep the OCR session records and the checkout between runs. A review # that stops at its token budget continues from here on the next run # instead of paying for the same files again. + # One entry per pull request: a cache entry cannot be overwritten, so a key + # with the run id would add a full checkout per run until the quota runs + # out. The entry seeds the next run; the staying-ahead work happens inside + # a single job. - name: Cache PR review state uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4 with: path: ${{ runner.temp }}/pr-review-state - key: pr-review-state-${{ github.repository }}-${{ github.event.pull_request.number || inputs.pull_number }}-${{ github.run_id }} - restore-keys: | - pr-review-state-${{ github.repository }}-${{ github.event.pull_request.number || inputs.pull_number }}- + key: pr-review-state-${{ github.repository }}-${{ github.event.pull_request.number || inputs.pull_number }} - name: Ensure PR review state directories shell: bash