diff --git a/ts/src/agents/pr_assistant/fix-deletions.ts b/ts/src/agents/pr_assistant/fix-deletions.ts new file mode 100644 index 00000000..176e54e9 --- /dev/null +++ b/ts/src/agents/pr_assistant/fix-deletions.ts @@ -0,0 +1,26 @@ +/** Destructive automated fixes: a review fix once replaced 155 lines of zplint's src/fix.rs with 15 (zplint#44). */ +import { run } from "../../utils/proc.ts"; + +/** A file loses at least this many lines and over GUT_RATIO times what it gains: the fix rewrote it, not patched it. */ +export const MIN_GUTTED_LINES = 40; +export const GUT_RATIO = 4; + +const diff = (dir: string, cached: boolean, extra: string[]) => + run(["git", "diff", ...(cached ? ["--cached"] : []), ...extra], { cwd: dir, timeoutMs: 30_000 }); + +/** Reason to reject, or "" when the fix keeps every tracked file and only trims code in proportion. */ +export async function deletionReason(dir: string, cached = false): Promise { + const removed = (await diff(dir, cached, ["--diff-filter=D", "--name-only"])).stdout.split(/\r?\n/).filter(Boolean); + if (removed.length) return `the fix deletes tracked file(s) ${removed.slice(0, 8).join(", ")}; fixes must edit code, never remove files`; + const gutted: string[] = []; + for (const line of (await diff(dir, cached, ["--numstat"])).stdout.split(/\r?\n/)) { + const [a, d, file] = line.split("\t"); + const added = parseInt(a ?? "", 10), deleted = parseInt(d ?? "", 10); + // Binary files report "-": nothing to judge line-wise. + if (!file || Number.isNaN(added) || Number.isNaN(deleted)) continue; + if (deleted >= MIN_GUTTED_LINES && deleted > added * GUT_RATIO) gutted.push(`${file} (-${deleted}/+${added})`); + } + return gutted.length + ? `the fix removes far more code than it adds in ${gutted.slice(0, 8).join(", ")}; change only the lines the finding needs` + : ""; +} diff --git a/ts/src/agents/pr_assistant/pipeline-fixer.ts b/ts/src/agents/pr_assistant/pipeline-fixer.ts index d8ca8344..e1213bce 100644 --- a/ts/src/agents/pr_assistant/pipeline-fixer.ts +++ b/ts/src/agents/pr_assistant/pipeline-fixer.ts @@ -12,6 +12,7 @@ import { authConfigArgs, githubCloneUrl, scrubEnv } from "../../utils/secure-git import { semanticMessage } from "../core/commit-message.ts"; import { runProjectChecks } from "./build-check.ts"; import { churnReason } from "./fix-churn.ts"; +import { deletionReason } from "./fix-deletions.ts"; import { literalReason } from "./fix-literals.ts"; import { forbiddenPaths, generatedPaths, isForbiddenPath, pruneUntrackedForbidden } from "./forbidden-paths.ts"; import { getFreeOpencodeModels, opencodeCmd, setupCloneEnvironment } from "./conflict-resolver.ts"; @@ -114,6 +115,8 @@ export async function validateChanges(dir: string, changed: string[], pr?: PR | if (generated.length) return [false, `Automated fix must not hand-edit generated files (edit the source and regenerate): ${JSON.stringify(generated.slice(0, 8))}`]; const churn = await churnReason(dir); if (churn) return [false, `Automated fix rejected: ${churn}`]; + const deletion = await deletionReason(dir); + if (deletion) return [false, `Automated fix rejected: ${deletion}`]; const literal = await literalReason(dir); if (literal) return [false, `Automated fix rejected: ${literal}`]; const chk = await run(["git", "diff", "--check"], { cwd: dir, timeoutMs: 60_000 }); diff --git a/ts/src/agents/pr_assistant/review-git.ts b/ts/src/agents/pr_assistant/review-git.ts index 7a0bed4c..b89047b6 100644 --- a/ts/src/agents/pr_assistant/review-git.ts +++ b/ts/src/agents/pr_assistant/review-git.ts @@ -4,6 +4,7 @@ import { run } from "../../utils/proc.ts"; import { scanClone } from "../../utils/secret-scan.ts"; import { authConfigArgs } from "../../utils/secure-git.ts"; import { churnReason } from "./fix-churn.ts"; +import { deletionReason } from "./fix-deletions.ts"; import { literalReason } from "./fix-literals.ts"; import { forbiddenPaths, generatedPaths, pruneUntrackedForbidden } from "./forbidden-paths.ts"; @@ -34,6 +35,8 @@ export async function commitAndPush(cloneDir: string, headRef: string, message: if (generated.length) return [false, "generated-file: " + generated.slice(0, 8).join("; ")]; const churn = await churnReason(cloneDir, true); if (churn) return [false, "whitespace-churn: " + churn]; + const deletion = await deletionReason(cloneDir, true); + if (deletion) return [false, "destructive-fix: " + deletion]; const literal = await literalReason(cloneDir, true); if (literal) return [false, "template-literal: " + literal]; const findings = await scanClone(cloneDir); diff --git a/ts/tests/fix-deletions.test.ts b/ts/tests/fix-deletions.test.ts new file mode 100644 index 00000000..ccc85fb9 --- /dev/null +++ b/ts/tests/fix-deletions.test.ts @@ -0,0 +1,49 @@ +import { expect, test } from "bun:test"; +import { unlinkSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { run } from "../src/utils/proc.ts"; +import { validateChanges } from "../src/agents/pr_assistant/pipeline-fixer.ts"; +import { deletionReason } from "../src/agents/pr_assistant/fix-deletions.ts"; +import { tmp } from "./helpers.ts"; + +// zplint#44: the review fix replaced 155 lines of src/fix.rs with 15 and gutted the feature. +const src = Array.from({ length: 155 }, (_, i) => `fn f${i}() -> u32 { ${i} }`).join("\n") + "\n"; +async function repo(): Promise { + const dir = tmp(); + for (const a of [["init", "-q"], ["config", "user.email", "t@t"], ["config", "user.name", "t"]]) await run(["git", ...a], { cwd: dir }); + writeFileSync(join(dir, "fix.rs"), src); + writeFileSync(join(dir, "other.rs"), "fn x() {}\n"); + await run(["git", "add", "-A"], { cwd: dir }); await run(["git", "commit", "-qm", "init"], { cwd: dir }); + return dir; +} +const gut = (dir: string) => writeFileSync(join(dir, "fix.rs"), src.split("\n").slice(0, 15).join("\n") + "\nfn stub() {}\n"); + +test("a fix that guts a file is rejected", async () => { + const dir = await repo(); + gut(dir); + const [ok, reason] = await validateChanges(dir, ["fix.rs"]); + expect(ok).toBe(false); + expect(reason).toContain("fix.rs (-140/+1)"); +}); +test("a focused fix passes", async () => { + const dir = await repo(); + writeFileSync(join(dir, "fix.rs"), src.replace("f3() -> u32 { 3 }", "f3() -> u32 { 33 }")); + expect(await deletionReason(dir)).toBe(""); +}); +test("removing a small block in proportion passes", async () => { + const dir = await repo(); + writeFileSync(join(dir, "fix.rs"), src.split("\n").slice(30).join("\n")); + expect(await deletionReason(dir)).toBe(""); +}); +test("deleting a tracked file is rejected", async () => { + const dir = await repo(); + unlinkSync(join(dir, "other.rs")); + expect(await deletionReason(dir)).toContain("deletes tracked file(s) other.rs"); +}); +test("the staged diff is checked before commit", async () => { + const dir = await repo(); + gut(dir); + expect(await deletionReason(dir, true)).toBe(""); + await run(["git", "add", "-A"], { cwd: dir }); + expect(await deletionReason(dir, true)).toContain("fix.rs"); +});