Skip to content

Commit e196f88

Browse files
committed
fix: avoid embedding large diffs in review prompts
Classify review scope before building the prompt. When the diff exceeds ~5 files or ~256 KB, fall back to a lightweight context (status, changed-files, diff_stat) and tell OpenCode to inspect the diff itself via read-only git commands. Prevents HTTP 400 / shallow findings on moderate-to-large changesets. Adversarial template grows a {{REVIEW_COLLECTION_GUIDANCE}} slot. Thresholds overridable via opts.maxInlineDiffFiles/Bytes. Closes #40. Port of openai/codex-plugin-cc#179.
1 parent a4e0097 commit e196f88

4 files changed

Lines changed: 211 additions & 7 deletions

File tree

‎plugins/opencode/prompts/adversarial-review.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ Before finalizing, check that each finding is:
7878
- actionable for an engineer fixing the issue
7979
</final_check>
8080

81+
<review_collection_guidance>
82+
{{REVIEW_COLLECTION_GUIDANCE}}
83+
</review_collection_guidance>
84+
8185
<repository_context>
8286
{{REVIEW_INPUT}}
8387
</repository_context>

‎plugins/opencode/scripts/lib/git.mjs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ export async function getDiffStat(cwd, opts = {}) {
6969
return stdout.trim();
7070
}
7171

72+
/**
73+
* Measure the byte size of a git diff without streaming the full contents
74+
* back to the caller. Useful for "is this diff too big to inline?" checks.
75+
*
76+
* @param {string} cwd
77+
* @param {{ base?: string, cached?: boolean }} [opts]
78+
* @returns {Promise<number>}
79+
*/
80+
export async function getDiffByteSize(cwd, opts = {}) {
81+
const args = ["diff"];
82+
if (opts.base) args.push(`${opts.base}...HEAD`);
83+
else if (opts.cached) args.push("--cached");
84+
const { stdout } = await runCommand("git", args, { cwd });
85+
return Buffer.byteLength(stdout, "utf8");
86+
}
87+
7288
/**
7389
* Get git status (short format).
7490
* @param {string} cwd

‎plugins/opencode/scripts/lib/prompts.mjs‎

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,27 @@
77

88
import fs from "node:fs";
99
import path from "node:path";
10-
import { getDiff, getStatus, getChangedFiles, getPrInfo, getPrDiff } from "./git.mjs";
10+
import {
11+
getDiff,
12+
getStatus,
13+
getChangedFiles,
14+
getDiffStat,
15+
getPrInfo,
16+
getPrDiff,
17+
} from "./git.mjs";
18+
19+
// Inline-diff thresholds. When a review exceeds either, we fall back to a
20+
// "self-collect" context that omits the full diff and asks OpenCode to
21+
// inspect the change itself via read-only git commands. See issue #40 and
22+
// openai/codex-plugin-cc#179.
23+
const DEFAULT_INLINE_DIFF_MAX_FILES = 5;
24+
const DEFAULT_INLINE_DIFF_MAX_BYTES = 256 * 1024;
25+
26+
function buildCollectionGuidance(includeDiff) {
27+
return includeDiff
28+
? "Use the repository context below as primary evidence."
29+
: "The repository context below is a lightweight summary. The full diff is intentionally omitted — inspect the target yourself with read-only git commands (git diff, git log, git show) before finalizing findings.";
30+
}
1131

1232
/**
1333
* Build the review prompt for OpenCode.
@@ -23,6 +43,7 @@ import { getDiff, getStatus, getChangedFiles, getPrInfo, getPrDiff } from "./git
2343
export async function buildReviewPrompt(cwd, opts, pluginRoot) {
2444
let diff, status, changedFiles;
2545
let prInfo = null;
46+
let diffStat = "";
2647

2748
if (opts.pr) {
2849
prInfo = await getPrInfo(cwd, opts.pr);
@@ -33,23 +54,50 @@ export async function buildReviewPrompt(cwd, opts, pluginRoot) {
3354
diff = await getDiff(cwd, { base: opts.base });
3455
status = await getStatus(cwd);
3556
changedFiles = await getChangedFiles(cwd, { base: opts.base });
57+
diffStat = await getDiffStat(cwd, { base: opts.base });
3658
}
3759

60+
// Classify the review scope. Large reviews fall back to a lightweight
61+
// context (stat + changed-files list + status) so the prompt doesn't blow
62+
// past model/provider input limits. OpenCode is then instructed to
63+
// inspect the diff itself via read-only git commands.
64+
const diffBytes = Buffer.byteLength(diff || "", "utf8");
65+
const maxFiles = Number.isFinite(opts.maxInlineDiffFiles)
66+
? opts.maxInlineDiffFiles
67+
: DEFAULT_INLINE_DIFF_MAX_FILES;
68+
const maxBytes = Number.isFinite(opts.maxInlineDiffBytes)
69+
? opts.maxInlineDiffBytes
70+
: DEFAULT_INLINE_DIFF_MAX_BYTES;
71+
const includeDiff = changedFiles.length <= maxFiles && diffBytes <= maxBytes;
72+
const collectionGuidance = buildCollectionGuidance(includeDiff);
73+
3874
const targetLabel = prInfo
3975
? `Pull request #${prInfo.number} "${prInfo.title}" (${prInfo.headRefName} -> ${prInfo.baseRefName})`
4076
: opts.base
4177
? `Branch diff against ${opts.base}`
4278
: "Working tree changes";
4379

80+
const reviewContext = buildReviewContext(diff, status, changedFiles, prInfo, {
81+
includeDiff,
82+
diffStat,
83+
});
84+
4485
let systemPrompt;
4586
if (opts.adversarial) {
4687
const templatePath = path.join(pluginRoot, "prompts", "adversarial-review.md");
4788
systemPrompt = fs.readFileSync(templatePath, "utf8")
4889
.replace("{{TARGET_LABEL}}", targetLabel)
4990
.replace("{{USER_FOCUS}}", opts.focus || "General review")
50-
.replace("{{REVIEW_INPUT}}", buildReviewContext(diff, status, changedFiles, prInfo));
91+
.replace("{{REVIEW_COLLECTION_GUIDANCE}}", collectionGuidance)
92+
.replace("{{REVIEW_INPUT}}", reviewContext);
5193
} else {
52-
systemPrompt = buildStandardReviewPrompt(diff, status, changedFiles, { ...opts, targetLabel, prInfo });
94+
systemPrompt = buildStandardReviewPrompt(diff, status, changedFiles, {
95+
...opts,
96+
targetLabel,
97+
prInfo,
98+
reviewContext,
99+
collectionGuidance,
100+
});
53101
}
54102

55103
return systemPrompt;
@@ -62,6 +110,11 @@ function buildStandardReviewPrompt(diff, status, changedFiles, opts) {
62110
const targetLabel = opts.targetLabel
63111
?? (opts.base ? `branch diff against ${opts.base}` : "working tree changes");
64112

113+
const reviewContext =
114+
opts.reviewContext
115+
?? buildReviewContext(diff, status, changedFiles, opts.prInfo, { includeDiff: true });
116+
const collectionGuidance = opts.collectionGuidance ?? buildCollectionGuidance(true);
117+
65118
return `You are performing a code review of ${targetLabel}.
66119
67120
Review the following changes and provide structured feedback in JSON format matching the review-output schema.
@@ -75,13 +128,21 @@ Focus on:
75128
76129
Be concise and actionable. Only report real issues, not style preferences.
77130
78-
${buildReviewContext(diff, status, changedFiles, opts.prInfo)}`;
131+
${collectionGuidance}
132+
133+
${reviewContext}`;
79134
}
80135

81136
/**
82137
* Build the repository context block for review prompts.
138+
*
139+
* When `opts.includeDiff` is false (large-review fallback), the full `<diff>`
140+
* block is omitted and a `<diff_stat>` summary is emitted instead. OpenCode
141+
* is expected to inspect the diff itself via read-only git commands — the
142+
* collection-guidance line above the context makes this explicit.
83143
*/
84-
function buildReviewContext(diff, status, changedFiles, prInfo) {
144+
function buildReviewContext(diff, status, changedFiles, prInfo, opts = {}) {
145+
const includeDiff = opts.includeDiff !== false;
85146
const sections = [];
86147

87148
if (prInfo) {
@@ -105,8 +166,16 @@ function buildReviewContext(diff, status, changedFiles, prInfo) {
105166
sections.push(`<changed_files>\n${changedFiles.join("\n")}\n</changed_files>`);
106167
}
107168

108-
if (diff) {
109-
sections.push(`<diff>\n${diff}\n</diff>`);
169+
if (includeDiff) {
170+
if (diff) {
171+
sections.push(`<diff>\n${diff}\n</diff>`);
172+
}
173+
} else {
174+
const statBody =
175+
opts.diffStat && opts.diffStat.length > 0
176+
? opts.diffStat
177+
: `(diff too large to inline: ${changedFiles.length} file(s))`;
178+
sections.push(`<diff_stat>\n${statBody}\n</diff_stat>`);
110179
}
111180

112181
return sections.join("\n\n");

‎tests/review-prompt-size.test.mjs‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { describe, it, beforeEach, afterEach } from "node:test";
2+
import assert from "node:assert/strict";
3+
import fs from "node:fs";
4+
import path from "node:path";
5+
import { fileURLToPath } from "node:url";
6+
import { createTmpDir, cleanupTmpDir } from "./helpers.mjs";
7+
import { runCommand } from "../plugins/opencode/scripts/lib/process.mjs";
8+
import { buildReviewPrompt } from "../plugins/opencode/scripts/lib/prompts.mjs";
9+
10+
const PLUGIN_ROOT = path.resolve(
11+
path.dirname(fileURLToPath(import.meta.url)),
12+
"..",
13+
"plugins",
14+
"opencode"
15+
);
16+
17+
let tmpDir;
18+
19+
beforeEach(async () => {
20+
tmpDir = createTmpDir("review-prompt-size-");
21+
await runCommand("git", ["init", "-q"], { cwd: tmpDir });
22+
await runCommand("git", ["config", "user.email", "[email protected]"], { cwd: tmpDir });
23+
await runCommand("git", ["config", "user.name", "t"], { cwd: tmpDir });
24+
// Initial commit with 4 tracked files so subsequent tests can modify
25+
// without needing to stage new files.
26+
for (const name of ["a.js", "b.js", "c.js", "d.js"]) {
27+
fs.writeFileSync(path.join(tmpDir, name), `export const ${name[0]} = 1;\n`);
28+
}
29+
await runCommand("git", ["add", "."], { cwd: tmpDir });
30+
await runCommand("git", ["commit", "-q", "-m", "init"], { cwd: tmpDir });
31+
});
32+
33+
afterEach(() => {
34+
cleanupTmpDir(tmpDir);
35+
});
36+
37+
describe("buildReviewPrompt large-diff fallback", () => {
38+
it("inlines the diff for a small working-tree change", async () => {
39+
fs.writeFileSync(path.join(tmpDir, "a.js"), "export const a = 'INLINE_MARKER';\n");
40+
41+
const prompt = await buildReviewPrompt(
42+
tmpDir,
43+
{ adversarial: true, focus: "test" },
44+
PLUGIN_ROOT
45+
);
46+
47+
assert.match(prompt, /<diff>/);
48+
assert.match(prompt, /INLINE_MARKER/);
49+
assert.match(prompt, /primary evidence/);
50+
assert.doesNotMatch(prompt, /diff too large to inline/);
51+
});
52+
53+
it("falls back to lightweight context when maxInlineDiffBytes is exceeded", async () => {
54+
fs.writeFileSync(
55+
path.join(tmpDir, "a.js"),
56+
`export const a = '${"x".repeat(2048)}';\n`
57+
);
58+
59+
const prompt = await buildReviewPrompt(
60+
tmpDir,
61+
{
62+
adversarial: true,
63+
focus: "test",
64+
maxInlineDiffBytes: 128,
65+
maxInlineDiffFiles: 100,
66+
},
67+
PLUGIN_ROOT
68+
);
69+
70+
assert.doesNotMatch(prompt, /<diff>\n/);
71+
assert.match(prompt, /<diff_stat>/);
72+
assert.match(prompt, /lightweight summary/);
73+
assert.match(prompt, /read-only git commands/);
74+
// The x-heavy content should not be inlined.
75+
assert.doesNotMatch(prompt, /xxxxxxxxxxxxxx/);
76+
});
77+
78+
it("falls back when maxInlineDiffFiles is exceeded", async () => {
79+
// Modify 3 already-tracked files; cap at 2.
80+
for (const name of ["b.js", "c.js", "d.js"]) {
81+
fs.writeFileSync(path.join(tmpDir, name), `export const ${name[0]} = 'modified';\n`);
82+
}
83+
84+
const prompt = await buildReviewPrompt(
85+
tmpDir,
86+
{
87+
adversarial: true,
88+
focus: "test",
89+
maxInlineDiffFiles: 2,
90+
maxInlineDiffBytes: 10_000_000,
91+
},
92+
PLUGIN_ROOT
93+
);
94+
95+
assert.doesNotMatch(prompt, /<diff>\n/);
96+
assert.match(prompt, /<diff_stat>|\(diff too large to inline/);
97+
assert.match(prompt, /lightweight summary/);
98+
});
99+
100+
it("injects collection guidance into adversarial template", async () => {
101+
fs.writeFileSync(path.join(tmpDir, "a.js"), "export const a = 'XXXX';\n");
102+
103+
const prompt = await buildReviewPrompt(
104+
tmpDir,
105+
{ adversarial: true, focus: "test" },
106+
PLUGIN_ROOT
107+
);
108+
109+
// The template still has both opening/closing tags.
110+
assert.match(prompt, /<review_collection_guidance>/);
111+
assert.match(prompt, /<\/review_collection_guidance>/);
112+
// The placeholder must be replaced, not left as literal.
113+
assert.doesNotMatch(prompt, /\{\{REVIEW_COLLECTION_GUIDANCE\}\}/);
114+
});
115+
});

0 commit comments

Comments
 (0)