Skip to content

Commit 892493c

Browse files
committed
fix: address Copilot PR#51 review comments
Four findings, all valid: C1 (prompts.mjs, git.mjs, process.mjs) — buildReviewPrompt materialized the full diff string before checking thresholds. For huge diffs the git/gh subprocess could OOM the companion before the size check ran. Fix: runCommand gains maxOutputBytes, killing the child and reporting overflowed once the cap is exceeded. getDiff and getPrDiff thread maxBytes through. buildReviewPrompt now bounds the read at maxBytes+1 and treats overflow as over-byte-limit without ever materializing the rest. C2 (git.mjs) — getDiffByteSize had a docstring claiming it avoided streaming the full contents, but the implementation did exactly that. It was also dead code (zero callers). Removed. C3 (tests/state-lock.test.mjs) — the test injected path.resolve(...) into a generated ESM import specifier. On Windows that path contains backslashes and a drive letter, producing an invalid module specifier. Fix: pathToFileURL(...).href for the injected specifier. C4 (tests/dead-pid-reconcile.test.mjs) — beforeEach mutated the object returned by loadState() without saving it back, leaving on-disk state from earlier tests intact. Fix: saveState(workspace, { config:{}, jobs:[] }). Adds coverage: - tests/process.test.mjs: runCommand overflow path and non-overflow path. - tests/review-prompt-size.test.mjs: bounds-huge-diff end-to-end test that writes a 50k-byte file and asserts fewer than 10k 'x' chars land in the prompt. All 167 tests pass.
1 parent 05e70f3 commit 892493c

7 files changed

Lines changed: 169 additions & 52 deletions

File tree

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

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,14 @@ export async function getCurrentBranch(cwd) {
4040

4141
/**
4242
* Get the diff for review, supporting base-branch and working-tree modes.
43+
*
44+
* When `opts.maxBytes` is set, the read is bounded at that cap. The returned
45+
* shape gains an `overflowed` flag in that case so callers can tell the
46+
* difference between "small diff, all of it" and "big diff, first N bytes".
47+
*
4348
* @param {string} cwd
44-
* @param {{ base?: string, cached?: boolean }} opts
45-
* @returns {Promise<string>}
49+
* @param {{ base?: string, cached?: boolean, maxBytes?: number }} opts
50+
* @returns {Promise<{ stdout: string, overflowed: boolean }>}
4651
*/
4752
export async function getDiff(cwd, opts = {}) {
4853
const args = ["diff"];
@@ -51,8 +56,11 @@ export async function getDiff(cwd, opts = {}) {
5156
} else if (opts.cached) {
5257
args.push("--cached");
5358
}
54-
const { stdout } = await runCommand("git", args, { cwd });
55-
return stdout;
59+
const result = await runCommand("git", args, {
60+
cwd,
61+
maxOutputBytes: opts.maxBytes,
62+
});
63+
return { stdout: result.stdout, overflowed: Boolean(result.overflowed) };
5664
}
5765

5866
/**
@@ -72,22 +80,6 @@ export async function getDiffStat(cwd, opts = {}) {
7280
return stdout.trim();
7381
}
7482

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

178170
/**
179171
* Fetch the unified diff for a pull request via `gh pr diff`.
172+
*
173+
* When `opts.maxBytes` is set, the read is bounded at that cap. `overflowed`
174+
* reports whether the full diff exceeded the cap — callers can then decide
175+
* to short-circuit to lightweight-mode without materializing the rest.
176+
*
180177
* @param {string} cwd
181178
* @param {number} prNumber
182-
* @returns {Promise<string>}
179+
* @param {{ maxBytes?: number }} [opts]
180+
* @returns {Promise<{ stdout: string, overflowed: boolean }>}
183181
*/
184-
export async function getPrDiff(cwd, prNumber) {
185-
const { stdout, stderr, exitCode } = await runCommand(
182+
export async function getPrDiff(cwd, prNumber, opts = {}) {
183+
const { stdout, stderr, exitCode, overflowed } = await runCommand(
186184
"gh",
187185
["pr", "diff", String(prNumber)],
188-
{ cwd }
186+
{ cwd, maxOutputBytes: opts.maxBytes }
189187
);
190-
if (exitCode !== 0) {
188+
// An overflow kill is not a real failure — we got the bytes we wanted.
189+
if (exitCode !== 0 && !overflowed) {
191190
throw new Error(`gh pr diff ${prNumber} failed: ${stderr.trim() || "unknown error"}`);
192191
}
193-
return stdout;
192+
return { stdout, overflowed: Boolean(overflowed) };
194193
}
195194

196195
// ------------------------------------------------------------------

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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,18 @@ export async function getOpencodeVersion() {
8383
}
8484

8585
/**
86-
* Run a command and return { stdout, stderr, exitCode }.
86+
* Run a command and return { stdout, stderr, exitCode, overflowed }.
87+
*
88+
* Supports bounded output reads via `opts.maxOutputBytes`. When that cap is
89+
* set and the child's stdout would exceed it, the child is killed and the
90+
* returned `overflowed` flag is set to `true`. This lets callers probe
91+
* potentially-huge outputs (e.g. `git diff` for a big changeset) without
92+
* materializing the whole buffer in memory.
93+
*
8794
* @param {string} cmd
8895
* @param {string[]} args
89-
* @param {object} [opts]
90-
* @returns {Promise<{ stdout: string, stderr: string, exitCode: number }>}
96+
* @param {{ cwd?: string, env?: object, maxOutputBytes?: number }} [opts]
97+
* @returns {Promise<{ stdout: string, stderr: string, exitCode: number, overflowed: boolean }>}
9198
*/
9299
export function runCommand(cmd, args, opts = {}) {
93100
return new Promise((resolve) => {
@@ -98,11 +105,45 @@ export function runCommand(cmd, args, opts = {}) {
98105
shell: platformShellOption(),
99106
windowsHide: true,
100107
});
108+
109+
const maxOutputBytes = Number.isFinite(opts.maxOutputBytes) && opts.maxOutputBytes > 0
110+
? opts.maxOutputBytes
111+
: null;
101112
let stdout = "";
113+
let stdoutBytes = 0;
102114
let stderr = "";
103-
proc.stdout.on("data", (d) => (stdout += d));
115+
let overflowed = false;
116+
117+
proc.stdout.on("data", (chunk) => {
118+
if (overflowed) return;
119+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
120+
stdoutBytes += buf.length;
121+
if (maxOutputBytes !== null && stdoutBytes > maxOutputBytes) {
122+
overflowed = true;
123+
// Keep whatever we've seen up to the cap, then kill the child.
124+
const keep = maxOutputBytes - (stdoutBytes - buf.length);
125+
if (keep > 0) stdout += buf.slice(0, keep).toString("utf8");
126+
try {
127+
proc.kill("SIGTERM");
128+
} catch {
129+
// best-effort; ignore
130+
}
131+
return;
132+
}
133+
stdout += buf.toString("utf8");
134+
});
104135
proc.stderr.on("data", (d) => (stderr += d));
105-
proc.on("close", (exitCode) => resolve({ stdout, stderr, exitCode: exitCode ?? 1 }));
136+
proc.on("close", (exitCode) =>
137+
resolve({
138+
stdout,
139+
stderr,
140+
// When we killed the child for overflow the exit code is not
141+
// meaningful — surface a synthetic 0 so callers can still consume
142+
// the partial stdout via the overflowed flag.
143+
exitCode: overflowed ? 0 : exitCode ?? 1,
144+
overflowed,
145+
})
146+
);
106147
});
107148
}
108149

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

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,54 @@ function truncateUtf8(text, maxBytes) {
4848
* @returns {Promise<string>}
4949
*/
5050
export async function buildReviewPrompt(cwd, opts, pluginRoot) {
51-
let diff, status, changedFiles;
51+
const maxFiles = Number.isFinite(opts.maxInlineDiffFiles)
52+
? opts.maxInlineDiffFiles
53+
: DEFAULT_INLINE_DIFF_MAX_FILES;
54+
const maxBytes = Number.isFinite(opts.maxInlineDiffBytes)
55+
? opts.maxInlineDiffBytes
56+
: DEFAULT_INLINE_DIFF_MAX_BYTES;
57+
58+
let diff = "";
59+
let status = "";
60+
let changedFiles = [];
5261
let prInfo = null;
5362
let diffStat = "";
63+
let overByteLimit = false;
5464

65+
// Step 1: cheap metadata. The status / changed-file list / shortstat
66+
// reads do not materialize the full diff and are safe on any size.
5567
if (opts.pr) {
5668
prInfo = await getPrInfo(cwd, opts.pr);
57-
diff = await getPrDiff(cwd, opts.pr);
5869
status = ""; // PR review intentionally ignores the local working tree
5970
changedFiles = prInfo.files;
6071
} else {
61-
diff = await getDiff(cwd, { base: opts.base });
6272
status = await getStatus(cwd);
6373
changedFiles = await getChangedFiles(cwd, { base: opts.base });
6474
diffStat = await getDiffStat(cwd, { base: opts.base });
6575
}
6676

67-
const diffBytes = Buffer.byteLength(diff || "", "utf8");
68-
const maxFiles = Number.isFinite(opts.maxInlineDiffFiles)
69-
? opts.maxInlineDiffFiles
70-
: DEFAULT_INLINE_DIFF_MAX_FILES;
71-
const maxBytes = Number.isFinite(opts.maxInlineDiffBytes)
72-
? opts.maxInlineDiffBytes
73-
: DEFAULT_INLINE_DIFF_MAX_BYTES;
77+
// Step 2: fetch the diff body, but bound the read at maxBytes + 1. If
78+
// the git/gh subprocess would produce more bytes than that, the helper
79+
// reports `overflowed: true` and we treat the diff as over the byte
80+
// limit without ever materializing the rest. Past this point we know
81+
// the diff string in memory is at most maxBytes + 1 bytes.
82+
const readCap = maxBytes + 1;
83+
if (opts.pr) {
84+
const pr = await getPrDiff(cwd, opts.pr, { maxBytes: readCap });
85+
diff = pr.stdout;
86+
overByteLimit = pr.overflowed;
87+
} else {
88+
const wt = await getDiff(cwd, { base: opts.base, maxBytes: readCap });
89+
diff = wt.stdout;
90+
overByteLimit = wt.overflowed;
91+
}
92+
7493
const overFileLimit = changedFiles.length > maxFiles;
75-
const overByteLimit = diffBytes > maxBytes;
94+
95+
// The "original" diff byte count is used for the user-facing context
96+
// note. When we overflowed the read, we don't know the true size — use
97+
// the cap as a lower bound.
98+
const diffBytes = overByteLimit ? readCap : Buffer.byteLength(diff, "utf8");
7699
const diffIsComplete = !overByteLimit;
77100
const diffForPrompt = overByteLimit ? truncateUtf8(diff, maxBytes) : diff;
78101
const collectionGuidance = buildCollectionGuidance(diffIsComplete);

‎tests/dead-pid-reconcile.test.mjs‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
reconcileAllJobs,
1010
buildStatusSnapshot,
1111
} from "../plugins/opencode/scripts/lib/job-control.mjs";
12-
import { upsertJob, loadState } from "../plugins/opencode/scripts/lib/state.mjs";
12+
import { upsertJob, loadState, saveState } from "../plugins/opencode/scripts/lib/state.mjs";
1313
import {
1414
getProcessStartToken,
1515
isProcessAlive,
@@ -64,13 +64,11 @@ describe("markDeadPidJobFailed", () => {
6464
});
6565

6666
beforeEach(() => {
67-
// Wipe state before each test.
68-
const state = loadState(workspace);
69-
state.jobs = [];
70-
upsertJob(workspace, { id: "__seed__", status: "completed" });
71-
const after = loadState(workspace);
72-
after.jobs = [];
73-
for (const j of after.jobs) {}
67+
// Actually wipe on-disk state between tests. The prior mutator-only
68+
// approach left entries from earlier tests in state.json, which made
69+
// the suite order-dependent as soon as IDs collided or MAX_JOBS
70+
// pruning kicked in.
71+
saveState(workspace, { config: {}, jobs: [] });
7472
});
7573

7674
it("rewrites a running job with a dead pid to failed", () => {

‎tests/process.test.mjs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ describe("process", () => {
2525
const { stderr, exitCode } = await runCommand("sh", ["-c", "echo err >&2"]);
2626
assert.ok(stderr.includes("err"));
2727
});
28+
29+
it("runCommand caps stdout at maxOutputBytes and reports overflowed", async () => {
30+
// 20k bytes of 'a' via awk — POSIX-portable, works on BSD and GNU.
31+
const { stdout, overflowed } = await runCommand(
32+
"awk",
33+
["BEGIN{for(i=0;i<20000;i++)printf \"a\"}"],
34+
{ maxOutputBytes: 1000 }
35+
);
36+
assert.equal(overflowed, true);
37+
assert.ok(stdout.length <= 1000, `stdout length ${stdout.length} exceeded cap 1000`);
38+
// The captured prefix should still be 'a' characters (partial read).
39+
assert.match(stdout, /^a+$/);
40+
});
41+
42+
it("runCommand does not overflow when output is under the cap", async () => {
43+
const { stdout, overflowed } = await runCommand(
44+
"sh",
45+
["-c", "printf 'hello'"],
46+
{ maxOutputBytes: 1000 }
47+
);
48+
assert.equal(overflowed, false);
49+
assert.equal(stdout, "hello");
50+
});
2851
});
2952

3053
// Tests for OpenCode auth.json discovery + provider detection.

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,35 @@ describe("buildReviewPrompt large-diff fallback", () => {
100100
assert.doesNotMatch(prompt, /bounded diff excerpt/);
101101
});
102102

103+
it("bounds the diff read so huge diffs never fully materialize", async () => {
104+
// Create a file whose modification generates a diff much larger than the
105+
// inline cap. With the bounded-read fix the diff fetch should stop early
106+
// and report overflowed, producing a lightweight excerpt — not a full
107+
// in-memory materialization of the multi-megabyte diff.
108+
const big = "x".repeat(50_000);
109+
fs.writeFileSync(path.join(tmpDir, "a.js"), `export const a = '${big}';\n`);
110+
111+
const prompt = await buildReviewPrompt(
112+
tmpDir,
113+
{
114+
adversarial: true,
115+
focus: "test",
116+
maxInlineDiffFiles: 100,
117+
maxInlineDiffBytes: 2048, // 2 KB cap
118+
},
119+
PLUGIN_ROOT
120+
);
121+
122+
// The excerpt-marker must be present and the x-heavy content must not
123+
// have fully landed in the prompt (we would see >= 50000 x's otherwise).
124+
assert.match(prompt, /bounded diff excerpt/);
125+
const xCount = (prompt.match(/x/g) ?? []).length;
126+
assert.ok(
127+
xCount < 10_000,
128+
`expected bounded diff, but saw ${xCount} 'x' chars in prompt`
129+
);
130+
});
131+
103132
it("injects collection guidance into adversarial template", async () => {
104133
fs.writeFileSync(path.join(tmpDir, "a.js"), "export const a = 'XXXX';\n");
105134

‎tests/state-lock.test.mjs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { fork } from "node:child_process";
44
import fs from "node:fs";
55
import os from "node:os";
66
import path from "node:path";
7-
import { fileURLToPath } from "node:url";
7+
import { fileURLToPath, pathToFileURL } from "node:url";
88
import { createTmpDir, cleanupTmpDir, setupTestEnv } from "./helpers.mjs";
99
import {
1010
loadState,
@@ -15,6 +15,10 @@ import {
1515

1616
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1717
const stateModulePath = path.resolve(__dirname, "..", "plugins", "opencode", "scripts", "lib", "state.mjs");
18+
// Module specifiers in generated ESM source must be valid URLs. On Windows
19+
// the resolved path contains backslashes and a drive letter, which would be
20+
// an invalid specifier / emit escape sequences inside the generated import.
21+
const stateModuleSpecifier = pathToFileURL(stateModulePath).href;
1822

1923
let tmpDir;
2024
const workspace = "/lock-test/workspace";
@@ -89,7 +93,7 @@ describe("updateState lock", () => {
8993

9094
fs.writeFileSync(
9195
script,
92-
`import { upsertJob } from "${stateModulePath}";\n` +
96+
`import { upsertJob } from "${stateModuleSpecifier}";\n` +
9397
`import fs from "node:fs";\n` +
9498
`process.env.CLAUDE_PLUGIN_DATA = "${dataDir}";\n` +
9599
`process.env.OPENCODE_COMPANION_SESSION_ID = "child-session";\n` +

0 commit comments

Comments
 (0)