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 <noreply@anthropic.com>
This commit is contained in:
NwflowerandClaude Opus 5.5 committed 2026-12-04 01:37:31 -07:00
1 parent fcbc77af57
commit d28b97c4ea
3 files changed
+60 -20

No files matched your search

+35 -16
View File
@@ -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);
+18
View File
@@ -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");
});