Skip to content

Commit fc5887b

Browse files
mchwangclaude
andcommitted
Address Copilot round 4 on F2d: stdin transport, exact problem cut, settle both reads
- Send the PR title and description as a JSON body on stdin (gh api --input -), so a large multibyte description or a NUL never hits the per-argument limit. The runner settles after gh exits. - Cut each shown problem to exactly 2,000 characters. - The already-fixed check aborts the other read on the first failure but returns only after both have settled. - AGENTS.md: pass input-sized text on stdin, not as an argument. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9980445 commit fc5887b

8 files changed

Lines changed: 106 additions & 23 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Every reproduced race requires a failing-before and passing-after regression. As
6565

6666
- A bounded safety scan must fail closed when its limit is exceeded. Never truncate evidence and report the result as clear.
6767
- Align subprocess output limits with every payload the schema accepts, or tighten the upstream page and field bounds; valid bounded input must not fail only because the transport budget is smaller.
68+
- Pass text whose size follows user or agent input to a subprocess on stdin, never as an argument: the OS limits one argument's size and cannot pass a NUL, so valid bounded input can fail to spawn.
6869
- Exclude the subject of a duplicate or supersession check by stable identity only. A shared branch name or other mutable attribute does not prove two records are the same subject.
6970
- Preserve repository identity with pull request numbers in cross-reference scans. Never resolve or exclude a repository-qualified reference by number alone.
7071
- When a relation can be added and removed (a manually linked PR, a label, an assignment), replay its add and remove events in order and count only its latest state. An add event alone does not prove the relation still holds.

‎core/pull-request-body.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function pullRequestTitle(plan: Plan): string {
6363
export function pullRequestBody(input: { plan: Plan; marker: string; problems?: readonly string[] }): string {
6464
const { plan, marker } = input;
6565
// The full problems stay in codeboost; the description shows a bounded summary of them.
66-
const all = input.problems ?? [], shown = all.slice(0, MAX_PROBLEMS).map(problem => neutralizeReferences(cut(problem, MAX_PROBLEM + 1)));
66+
const all = input.problems ?? [], shown = all.slice(0, MAX_PROBLEMS).map(problem => neutralizeReferences(cut(problem, MAX_PROBLEM)));
6767
const problems = all.length > shown.length ? [...shown, `(${all.length - shown.length} more in codeboost)`] : shown;
6868
const build = (full: boolean) => [
6969
marker,

‎docs/implementation/pull-request-opening.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ Each opening or refresh owns the task state version at the moment it passed step
6969

7070
The adapter refuses any answer for a PR that is not open. A PR closed between the lookup and the update is never recorded as the task's review PR.
7171

72+
**Transport.** The PR title and description go to `gh api --input -` as a JSON body on stdin (`github/run-with-input.ts`), never as arguments: Linux limits one argument to 128 KiB, and a 60,000-character description of multibyte text is larger. The runner settles only after `gh` has exited, including on a timeout or abort. The already-fixed check also waits for both of its reads to settle before it returns.
73+
7274
**Environment.** Each `gh` process gets only an allowlist of variables: the path, home and locale; GitHub tokens, host and configuration directories; the D-Bus session bus that Linux keyring sign-in uses; and proxy and CA settings (`github/gh-env.ts`). Prompts, the pager and update checks are turned off.
7375

7476
## The PR description

‎github/already-fixed.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,16 @@ export class GhAlreadyFixedGateway implements AlreadyFixedGateway {
113113
if (!/^(?!-)(?!.*\.\.)(?!.*\/\/)[A-Za-z0-9._/-]+(?<![./])$/.test(input.baseBranch)) throw new Error('Invalid base branch name.');
114114
if (input.ownPullRequests.some(number => !Number.isSafeInteger(number) || number < 1)) throw new Error('Invalid pull request number.');
115115
// Every stage shares one deadline; reaching it aborts the running `gh` call and makes the check unknown.
116-
// The two stages are independent and run together; the first failure stops the other.
116+
// The two stages are independent and run together. The first failure stops the other, and the check still waits for
117+
// both to settle, so it never returns while a `gh` process it started is running.
117118
const deadline = AbortSignal.timeout(this.deadlineMs), failed = new AbortController();
118119
const stages = AbortSignal.any([...(signal ? [signal] : []), deadline, failed.signal]);
119-
const stop = (error: unknown) => { failed.abort(); throw error; };
120+
let first: { error: unknown } | null = null;
121+
const stop = (error: unknown) => { first ??= { error }; failed.abort(); throw error; };
120122
try {
121-
const [matches, { baseHead, commits }] = await Promise.all([this.#timeline(input, stages).catch(stop), this.#baseCommits(input, stages).catch(stop)]);
123+
const settled = await Promise.allSettled([this.#timeline(input, stages).catch(stop), this.#baseCommits(input, stages).catch(stop)]);
124+
if (first) throw (first as { error: unknown }).error;
125+
const [matches, { baseHead, commits }] = settled.map(result => (result as PromiseFulfilledResult<unknown>).value) as [AlreadyFixedMatch[], { baseHead: string; commits: { sha: string; message: string }[] }];
122126
for (const commit of commits) {
123127
if (input.ownCommits.has(commit.sha) || !mentionsIssue(commit.message, this.repository, input.issue)) continue;
124128
matches.push({ kind: 'commit', sha: commit.sha, subject: commit.message.split('\n', 1)[0]!.slice(0, 200) });

‎github/pull-requests.ts‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { execFile } from 'node:child_process';
2-
import { promisify } from 'node:util';
3-
import type { RunGh } from './merge.ts';
41
import { ghEnvironment } from './gh-env.ts';
2+
import { runWithInput } from './run-with-input.ts';
53

6-
const runFile = promisify(execFile);
4+
/** A `gh` runner that can also write a request body to stdin (`gh api --input -`). */
5+
export type RunGhWithInput = (args: readonly string[], options?: { signal?: AbortSignal; input?: string }) => Promise<string>;
76

87
export interface OpenPullRequestInput {
98
base: string;
@@ -29,18 +28,21 @@ export interface PullRequestGateway {
2928
const SHA = /^[a-f0-9]{40}$/;
3029
const BRANCH = /^(?!-)(?!.*\.\.)(?!.*\/\/)[A-Za-z0-9._/-]+(?<![./])$/;
3130

32-
/** GitHub CLI adapter for opening a task's PR. All arguments are literal argv; no shell is involved. */
31+
/**
32+
* GitHub CLI adapter for opening a task's PR. All arguments are literal argv; no shell is involved. The title and
33+
* description go as a JSON request body on stdin, never as arguments.
34+
*/
3335
export class GhPullRequestGateway implements PullRequestGateway {
3436
readonly repository: string;
35-
readonly run: RunGh;
36-
constructor(config: { repository: string }, run?: RunGh) {
37+
readonly run: RunGhWithInput;
38+
constructor(config: { repository: string }, run?: RunGhWithInput) {
3739
if (!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(config.repository)) throw new Error('A GitHub repository is required to open pull requests.');
3840
this.repository = config.repository;
39-
this.run = run ?? (async (args, options) => (await runFile('gh', [...args], { timeout: 30_000, maxBuffer: 8 * 1024 * 1024, signal: options?.signal, env: ghEnvironment() })).stdout);
41+
this.run = run ?? ((args, options) => runWithInput('gh', args, { input: options?.input, timeout: 30_000, maxBuffer: 8 * 1024 * 1024, signal: options?.signal, env: ghEnvironment() }));
4042
}
4143

42-
async #json(args: readonly string[], signal?: AbortSignal): Promise<unknown> {
43-
const output = await this.run(args, { signal });
44+
async #json(args: readonly string[], signal?: AbortSignal, body?: Record<string, unknown>): Promise<unknown> {
45+
const output = await this.run(body ? [...args, '--input', '-'] : args, { signal, ...(body ? { input: JSON.stringify(body) } : {}) });
4446
try { return JSON.parse(output); }
4547
catch { throw new Error('GitHub returned invalid JSON.'); }
4648
}
@@ -67,8 +69,8 @@ export class GhPullRequestGateway implements PullRequestGateway {
6769
async open(input: OpenPullRequestInput, signal?: AbortSignal): Promise<OpenedPullRequest> {
6870
this.#validate(input);
6971
if (!input.body.includes(input.marker)) throw new Error('The pull request description must carry its marker.');
70-
const response = await this.#json(['api', '-X', 'POST', '-H', 'Accept: application/vnd.github+json', `repos/${this.repository}/pulls`,
71-
'-f', `title=${input.title}`, '-f', `body=${input.body}`, '-f', `head=${input.headBranch}`, '-f', `base=${input.base}`, '-F', `draft=${input.draft}`], signal);
72+
const response = await this.#json(['api', '-X', 'POST', '-H', 'Accept: application/vnd.github+json', `repos/${this.repository}/pulls`], signal,
73+
{ title: input.title, body: input.body, head: input.headBranch, base: input.base, draft: input.draft });
7274
const { body, ...pr } = this.#pull(response, input);
7375
if (!body.includes(input.marker)) throw new Error('GitHub returned a pull request without its marker.');
7476
return pr;
@@ -92,8 +94,8 @@ export class GhPullRequestGateway implements PullRequestGateway {
9294
this.#validate(input);
9395
if (!Number.isSafeInteger(number) || number < 1) throw new Error('Invalid pull request number.');
9496
if (!input.body.includes(input.marker)) throw new Error('The pull request description must carry its marker.');
95-
const patched = this.#pull(await this.#json(['api', '-X', 'PATCH', '-H', 'Accept: application/vnd.github+json', `repos/${this.repository}/pulls/${number}`,
96-
'-f', `title=${input.title}`, '-f', `body=${input.body}`], signal), input);
97+
const patched = this.#pull(await this.#json(['api', '-X', 'PATCH', '-H', 'Accept: application/vnd.github+json', `repos/${this.repository}/pulls/${number}`], signal,
98+
{ title: input.title, body: input.body }), input);
9799
if (patched.number !== number || !patched.body.includes(input.marker)) throw new Error('GitHub returned a different pull request.');
98100
// A ready PR whose task went back to needs human becomes a draft again; a draft whose task is ready leaves draft.
99101
if (input.ready && patched.draft) await this.run(['pr', 'ready', String(number), '--repo', this.repository], { signal });

‎github/run-with-input.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { spawn } from 'node:child_process';
2+
3+
/**
4+
* Runs a command with literal argv and writes `input` to its stdin, so large text never travels as an OS argument
5+
* (Linux limits one argument to 128 KiB, and a NUL cannot be passed at all). Settles only after the process closed:
6+
* a timeout, an abort or an output past `maxBuffer` kills it, and the promise rejects once it has exited.
7+
*/
8+
export function runWithInput(command: string, args: readonly string[], options: {
9+
input?: string; signal?: AbortSignal; env?: NodeJS.ProcessEnv; timeout?: number; maxBuffer?: number;
10+
}): Promise<string> {
11+
return new Promise((resolve, reject) => {
12+
if (options.signal?.aborted) { reject(options.signal.reason ?? new Error('Aborted.')); return; }
13+
const child = spawn(command, [...args], { env: options.env, stdio: ['pipe', 'pipe', 'pipe'] });
14+
const out: Buffer[] = [], err: Buffer[] = [];
15+
let size = 0, failure: Error | null = null;
16+
const stop = (error: Error) => { failure ??= error; child.kill('SIGTERM'); };
17+
const timer = options.timeout ? setTimeout(() => stop(new Error(`${command} timed out.`)), options.timeout) : null;
18+
const onAbort = () => stop(options.signal?.reason instanceof Error ? options.signal.reason : new Error('Aborted.'));
19+
options.signal?.addEventListener('abort', onAbort, { once: true });
20+
const collect = (chunks: Buffer[]) => (chunk: Buffer) => {
21+
size += chunk.length;
22+
if (options.maxBuffer !== undefined && size > options.maxBuffer) stop(new Error(`${command} output exceeded its limit.`));
23+
else chunks.push(chunk);
24+
};
25+
child.stdout.on('data', collect(out));
26+
child.stderr.on('data', collect(err));
27+
child.stdin.on('error', () => { /* the exit status reports a process that stopped reading */ });
28+
child.stdin.end(options.input ?? '');
29+
child.on('error', error => stop(error));
30+
child.on('close', (code, signal) => {
31+
if (timer) clearTimeout(timer);
32+
options.signal?.removeEventListener('abort', onAbort);
33+
const stderr = Buffer.concat(err).toString('utf8').trim();
34+
if (failure) reject(failure);
35+
else if (code !== 0) reject(new Error(`${command} failed (${signal ?? `exit ${code}`}): ${stderr.slice(0, 2000)}`));
36+
else resolve(Buffer.concat(out).toString('utf8'));
37+
});
38+
});
39+
}

‎test/already-fixed.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ describe('the pre-PR already-fixed check', () => {
126126
expect(DEFAULT_CHECK_DEADLINE_MS).toBeLessThan(15_000);
127127
expect(new GhAlreadyFixedGateway({ repository: repo }).deadlineMs).toBe(DEFAULT_CHECK_DEADLINE_MS);
128128
});
129+
it('returns only after the stopped stage has settled', async () => {
130+
let timelineSettled = false;
131+
const gh = new GhAlreadyFixedGateway({ repository: repo, deadlineMs: 10_000 }, async (args, options) => {
132+
if (args[1] === 'graphql') return new Promise((_, reject) => options?.signal?.addEventListener('abort', () => setTimeout(() => { timelineSettled = true; reject(new Error('killed')); }, 100)));
133+
throw new Error('HTTP 502');
134+
});
135+
expect(await gh.check(input())).toMatchObject({ outcome: 'unknown' });
136+
expect(timelineSettled).toBe(true);
137+
});
129138
it('passes cancellation through instead of reporting it as unknown', async () => {
130139
const controller = new AbortController();
131140
const gh = new GhAlreadyFixedGateway({ repository: repo }, async () => { controller.abort(); throw new Error('aborted'); });

‎test/publish.test.ts‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Store } from '../runner/store.ts';
33
import { GuardRefusal } from '../runner/lifecycle.ts';
44
import { OpeningUnsettled, PullRequestPublisher, type BranchPusher, type PublishConfig } from '../runner/publish.ts';
55
import { GH_ENV_ALLOWLIST, ghEnvironment } from '../github/gh-env.ts';
6+
import { runWithInput } from '../github/run-with-input.ts';
67
import { GhPullRequestGateway, type OpenPullRequestInput, type OpenedPullRequest, type PullRequestGateway } from '../github/pull-requests.ts';
78
import type { AlreadyFixedGateway, AlreadyFixedInput, AlreadyFixedResult } from '../github/already-fixed.ts';
89
import { fenced, neutralizeReferences, pullRequestBody, pullRequestTitle, MAX_BODY } from '../core/pull-request-body.ts';
@@ -412,6 +413,11 @@ describe('the PR description', () => {
412413
const body = pullRequestBody({ plan, marker: 'm', problems: Array.from({ length: 20 }, () => '😀'.repeat(2500)) });
413414
expect(body.length).toBeLessThan(MAX_BODY);
414415
});
416+
it('cuts each shown problem to exactly the documented 2,000 characters', () => {
417+
const body = pullRequestBody({ plan, marker: 'm', problems: ['a'.repeat(3000)] });
418+
expect(body).toContain(`${'a'.repeat(1999)}…\n`);
419+
expect(body).not.toContain('a'.repeat(2000));
420+
});
415421
it('bounds the open problems it shows', () => {
416422
const body = pullRequestBody({ plan, marker: 'm', problems: Array.from({ length: 25 }, (_, i) => `problem ${i} ${'z'.repeat(3000)}`) });
417423
expect(body).toContain('(5 more in codeboost)');
@@ -442,6 +448,25 @@ describe('the PR description', () => {
442448
});
443449
});
444450

451+
describe('running gh with a request body on stdin', () => {
452+
it('passes a body far past the per-argument limit, including a NUL, through stdin', async () => {
453+
const body = `${'€'.repeat(60_000)}\u0000end`;
454+
const out = await runWithInput(process.execPath, ['-e', 'let n=0;process.stdin.on("data",c=>n+=c.length).on("end",()=>process.stdout.write(String(n)))'], { input: body });
455+
expect(Number(out)).toBe(Buffer.byteLength(body));
456+
});
457+
it('rejects on abort only after the process has exited', async () => {
458+
const controller = new AbortController();
459+
const started = runWithInput(process.execPath, ['-e', 'process.on("SIGTERM",()=>setTimeout(()=>process.exit(1),150));setInterval(()=>{},1000)'], { signal: controller.signal });
460+
await new Promise(resolve => setTimeout(resolve, 200));
461+
const aborted = Date.now(); controller.abort(new Error('stop'));
462+
await expect(started).rejects.toThrow('stop');
463+
expect(Date.now() - aborted).toBeGreaterThanOrEqual(100);
464+
});
465+
it('reports a failing exit with its stderr', async () => {
466+
await expect(runWithInput(process.execPath, ['-e', 'console.error("HTTP 422");process.exit(1)'], {})).rejects.toThrow(/exit 1\): HTTP 422/);
467+
});
468+
});
469+
445470
describe('gh subprocess environment', () => {
446471
it('passes only the allowlisted variables, and turns prompts off', () => {
447472
const env = ghEnvironment({ PATH: '/bin', GH_TOKEN: 't', AWS_SECRET_ACCESS_KEY: 'x', ANTHROPIC_API_KEY: 'y', HOME: '/h' });
@@ -457,12 +482,13 @@ describe('GitHub PR adapter', () => {
457482
const response = (over: Record<string, unknown> = {}) => ({ number: 7, html_url: 'https://github.com/owner/repo/pull/7', state: 'open', draft: true, body: `${marker}\nplan`,
458483
head: { sha: oid(2), ref: 'codeboost/issue-12-task', repo: { full_name: 'Owner/Repo' } }, base: { ref: 'main', repo: { full_name: 'owner/repo' } }, ...over });
459484
const input = { base: 'main', headBranch: 'codeboost/issue-12-task', title: 'T', body: `${marker}\nplan`, draft: true, marker };
460-
it('opens with literal argv and validates the answer', async () => {
461-
const calls: string[][] = [];
462-
const gh = new GhPullRequestGateway({ repository: 'owner/repo' }, async args => { calls.push([...args]); return JSON.stringify(response()); });
485+
it('opens with literal argv, sends the title and description as a JSON body on stdin, and validates the answer', async () => {
486+
const calls: string[][] = [], inputs: (string | undefined)[] = [];
487+
const gh = new GhPullRequestGateway({ repository: 'owner/repo' }, async (args, options) => { calls.push([...args]); inputs.push(options?.input); return JSON.stringify(response()); });
463488
expect(await gh.open(input)).toEqual({ number: 7, url: 'https://github.com/owner/repo/pull/7', headSha: oid(2), draft: true });
464-
expect(calls[0]).toEqual(['api', '-X', 'POST', '-H', 'Accept: application/vnd.github+json', 'repos/owner/repo/pulls',
465-
'-f', 'title=T', '-f', `body=${marker}\nplan`, '-f', 'head=codeboost/issue-12-task', '-f', 'base=main', '-F', 'draft=true']);
489+
expect(calls[0]).toEqual(['api', '-X', 'POST', '-H', 'Accept: application/vnd.github+json', 'repos/owner/repo/pulls', '--input', '-']);
490+
expect(JSON.parse(inputs[0]!)).toEqual({ title: 'T', body: `${marker}\nplan`, head: 'codeboost/issue-12-task', base: 'main', draft: true });
491+
expect(calls[0]!.join(' ')).not.toContain('plan');
466492
});
467493
it('refuses answers for another branch or repository, and bodies without the marker', async () => {
468494
for (const over of [{ head: { sha: oid(2), ref: 'other', repo: { full_name: 'owner/repo' } } }, { head: { sha: oid(2), ref: 'codeboost/issue-12-task', repo: { full_name: 'fork/repo' } } },

0 commit comments

Comments
 (0)