feat: sanitize review decisions before merge policy (4.3)
- drop findings targeting unknown files or invalid lines with warnings - force canMerge=false conservatively when blocking changes exist
This commit is contained in:
1 parent
3f793d746e
commit
ec0d5572ff
3 files changed
+130
-1
No files matched your search
@@ -1,9 +1,11 @@
|
|||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { runGoosePrompt } from "../ai/gooseCli.js";
|
import { runGoosePrompt } from "../ai/gooseCli.js";
|
||||||
import { config } from "../config.js";
|
import { config } from "../config.js";
|
||||||
|
import { logger } from "../logger.js";
|
||||||
import { withRetry } from "../retry.js";
|
import { withRetry } from "../retry.js";
|
||||||
import type { PullRequestFile, ReviewDecision, ReviewMode } from "../types.js";
|
import type { PullRequestFile, ReviewDecision, ReviewMode } from "../types.js";
|
||||||
import type { PreviousReview } from "./cache.js";
|
import type { PreviousReview } from "./cache.js";
|
||||||
|
import { normalizeReviewDecision } from "./normalize.js";
|
||||||
|
|
||||||
const findingSchema = z.object({
|
const findingSchema = z.object({
|
||||||
path: z.string(),
|
path: z.string(),
|
||||||
@@ -37,7 +39,15 @@ export class GooseReviewer {
|
|||||||
"goose.run.review",
|
"goose.run.review",
|
||||||
async () => {
|
async () => {
|
||||||
const raw = await runGoosePrompt(buildPrompt(input));
|
const raw = await runGoosePrompt(buildPrompt(input));
|
||||||
return reviewDecisionSchema.parse(JSON.parse(raw));
|
const parsed = reviewDecisionSchema.parse(JSON.parse(raw));
|
||||||
|
const { decision, warnings } = normalizeReviewDecision(
|
||||||
|
parsed,
|
||||||
|
input.files.map((file) => file.filename)
|
||||||
|
);
|
||||||
|
if (warnings.length > 0) {
|
||||||
|
logger.warn({ warnings }, "Review output contained findings that could not be posted inline.");
|
||||||
|
}
|
||||||
|
return decision;
|
||||||
},
|
},
|
||||||
{ maxAttempts: 3 }
|
{ maxAttempts: 3 }
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import type { ReviewDecision } from "../types.js";
|
||||||
|
|
||||||
|
export type NormalizedReviewDecision = {
|
||||||
|
decision: ReviewDecision;
|
||||||
|
warnings: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize a model-produced review decision before it drives merge policy:
|
||||||
|
* - drop findings that point at files outside the PR or at invalid lines
|
||||||
|
* (they cannot be posted inline and would otherwise be lost silently),
|
||||||
|
* - force the conservative canMerge=false when blocking findings exist,
|
||||||
|
* even if the model claimed the result was safe.
|
||||||
|
*/
|
||||||
|
export function normalizeReviewDecision(
|
||||||
|
decision: ReviewDecision,
|
||||||
|
knownPaths: string[]
|
||||||
|
): NormalizedReviewDecision {
|
||||||
|
const warnings: string[] = [];
|
||||||
|
const known = new Set(knownPaths);
|
||||||
|
|
||||||
|
const keepFinding = (
|
||||||
|
finding: { path: string; line: number; title: string },
|
||||||
|
category: "review" | "change"
|
||||||
|
) => {
|
||||||
|
if (!known.has(finding.path)) {
|
||||||
|
warnings.push(
|
||||||
|
`${category} finding for unknown file ${finding.path}:${finding.line} (${finding.title})`
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!Number.isInteger(finding.line) || finding.line <= 0) {
|
||||||
|
warnings.push(
|
||||||
|
`${category} finding with invalid line ${finding.path}:${finding.line} (${finding.title})`
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const review = decision.review.filter((finding) => keepFinding(finding, "review"));
|
||||||
|
const change = decision.change.filter((finding) => keepFinding(finding, "change"));
|
||||||
|
|
||||||
|
const canMerge = change.length > 0 ? false : decision.result.canMerge;
|
||||||
|
|
||||||
|
return {
|
||||||
|
decision: {
|
||||||
|
...decision,
|
||||||
|
review,
|
||||||
|
change,
|
||||||
|
result: { ...decision.result, canMerge }
|
||||||
|
},
|
||||||
|
warnings
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
import test from "node:test";
|
||||||
|
import { normalizeReviewDecision } from "../src/review/normalize.js";
|
||||||
|
|
||||||
|
function finding(path: string, line: number) {
|
||||||
|
return { path, line, title: "t", body: "b" };
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseResult = {
|
||||||
|
canMerge: true,
|
||||||
|
summary: "s",
|
||||||
|
shouldClosePullRequest: false,
|
||||||
|
closeReason: ""
|
||||||
|
};
|
||||||
|
|
||||||
|
test("keeps findings whose paths exist and lines are positive integers", () => {
|
||||||
|
const decision = {
|
||||||
|
review: [finding("src/a.ts", 3)],
|
||||||
|
change: [finding("src/b.ts", 1)],
|
||||||
|
comment: "c",
|
||||||
|
result: { ...baseResult }
|
||||||
|
};
|
||||||
|
const normalized = normalizeReviewDecision(decision, ["src/a.ts", "src/b.ts"]);
|
||||||
|
assert.deepEqual(normalized.decision.review, decision.review);
|
||||||
|
assert.deepEqual(normalized.decision.change, decision.change);
|
||||||
|
assert.deepEqual(normalized.warnings, []);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("moves findings with unknown paths or bad lines into warnings", () => {
|
||||||
|
const decision = {
|
||||||
|
review: [finding("ghost.ts", 2), finding("src/a.ts", -1), finding("src/a.ts", 0)],
|
||||||
|
change: [],
|
||||||
|
comment: "c",
|
||||||
|
result: { ...baseResult }
|
||||||
|
};
|
||||||
|
const normalized = normalizeReviewDecision(decision, ["src/a.ts"]);
|
||||||
|
assert.equal(normalized.decision.review.length, 0);
|
||||||
|
assert.equal(normalized.warnings.length, 3);
|
||||||
|
assert.match(normalized.warnings[0] ?? "", /unknown file/);
|
||||||
|
assert.match(normalized.warnings[1] ?? "", /invalid line/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("a non-empty change list forces canMerge=false conservatively", () => {
|
||||||
|
const decision = {
|
||||||
|
review: [],
|
||||||
|
change: [finding("src/a.ts", 1)],
|
||||||
|
comment: "c",
|
||||||
|
result: { ...baseResult, canMerge: true }
|
||||||
|
};
|
||||||
|
const normalized = normalizeReviewDecision(decision, ["src/a.ts"]);
|
||||||
|
assert.equal(normalized.decision.result.canMerge, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("shouldClosePullRequest=true forces a non-empty closeReason requirement warning-free pass", () => {
|
||||||
|
const decision = {
|
||||||
|
review: [],
|
||||||
|
change: [],
|
||||||
|
comment: "c",
|
||||||
|
result: { ...baseResult, shouldClosePullRequest: true, closeReason: "backdoor" }
|
||||||
|
};
|
||||||
|
const normalized = normalizeReviewDecision(decision, []);
|
||||||
|
assert.equal(normalized.decision.result.shouldClosePullRequest, true);
|
||||||
|
assert.equal(normalized.warnings.length, 0);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user