Skip to content

Commit 07c6630

Browse files
mchwangclaude
andcommitted
Close high-effort review findings on F2d: settle, head read-back, drafts
- The already-fixed check's default runner reuses runWithInput, so an aborted stage escalates to SIGKILL and the 12 s deadline holds. - runWithInput settles after exit even if a process gh started keeps the pipes open, closing them after a grace period. - A refresh read-back polls briefly until GitHub shows the pushed head, so a correct publish is not sent to needs human by a stale head. - When the check matches, the task's earlier ready PR becomes a draft, so it is never left ready for review. - #recover reads the task's PR records once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b32a330 commit 07c6630

7 files changed

Lines changed: 117 additions & 19 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ Each opening or refresh owns the task state version at the moment it passed step
6767
| Unchanged, needs human | Draft opened, or the earlier PR updated and turned back into a draft | needs human |
6868
| Changed (cancelled, reassigned, new attempt, new head) | Opened | Unchanged; the PR is recorded so it can be reused or closed later |
6969

70+
After an update, the read-back polls up to 5 times, half a second apart, until GitHub shows the pushed head, because GitHub updates a PR's head a moment after a push.
71+
72+
When the check matches (or is unknown) and the task's earlier PR is open and ready for review, publish turns it back into a draft. A task that is not being published as ready never leaves its PR ready for review.
73+
7074
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.
7175

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: it sends SIGTERM, then SIGKILL after 5 seconds if `gh` is still running. The already-fixed check also waits for both of its reads to settle before it returns.
76+
**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: it sends SIGTERM, then SIGKILL after 5 seconds if `gh` is still running, and if a process `gh` started keeps the output pipes open after `gh` exits, it closes them after 1 second. The already-fixed check uses the same runner, so its 12-second deadline always holds. The already-fixed check also waits for both of its reads to settle before it returns.
7377

7478
**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.
7579

‎github/already-fixed.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { execFile } from 'node:child_process';
2-
import { promisify } from 'node:util';
31
import type { RunGh } from './merge.ts';
42
import { ghEnvironment } from './gh-env.ts';
5-
6-
const runFile = promisify(execFile);
3+
import { runWithInput } from './run-with-input.ts';
74

85
/**
96
* The pre-PR "already fixed" check (design, "Checking whether the issue is already fixed"). It reports a match when
@@ -98,7 +95,8 @@ export class GhAlreadyFixedGateway implements AlreadyFixedGateway {
9895
if (config.deadlineMs !== undefined && (!Number.isSafeInteger(config.deadlineMs) || config.deadlineMs < 1)) throw new Error('Invalid check deadline.');
9996
this.repository = config.repository;
10097
this.deadlineMs = config.deadlineMs ?? DEFAULT_CHECK_DEADLINE_MS;
101-
this.run = run ?? (async (args, options) => (await runFile('gh', [...args], { timeout: 30_000, maxBuffer: 8 * 1024 * 1024, signal: options?.signal, env: ghEnvironment() })).stdout);
98+
// runWithInput escalates to SIGKILL, so an aborted stage always settles and the check's single deadline holds.
99+
this.run = run ?? ((args, options) => runWithInput('gh', args, { timeout: 30_000, maxBuffer: 8 * 1024 * 1024, signal: options?.signal, env: ghEnvironment() }));
102100
}
103101

104102
async #json(args: readonly string[], signal?: AbortSignal): Promise<unknown> {

‎github/pull-requests.ts‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ export interface PullRequestGateway {
2222
*/
2323
findOpened(input: { base: string; headBranch: string; markers: readonly string[] }, signal?: AbortSignal): Promise<(OpenedPullRequest & { marker: string }) | null>;
2424
/** Replaces the title and description of an open PR codeboost opened; marks it ready when `ready`, or a draft when `draft`. */
25-
refresh(number: number, input: OpenPullRequestInput & { ready: boolean }, signal?: AbortSignal): Promise<OpenedPullRequest>;
25+
refresh(number: number, input: OpenPullRequestInput & { ready: boolean; headSha?: string }, signal?: AbortSignal): Promise<OpenedPullRequest>;
26+
/** Turns an open PR codeboost opened back into a draft; a no-op for a draft. */
27+
markDraft(number: number, input: { base: string; headBranch: string; marker: string }, signal?: AbortSignal): Promise<OpenedPullRequest>;
2628
}
29+
/** GitHub updates a PR's head a moment after a push; the read-back waits up to this many polls for the pushed head. */
30+
export const HEAD_POLLS = 5, HEAD_POLL_MS = 500;
2731

2832
const SHA = /^[a-f0-9]{40}$/;
2933
const BRANCH = /^(?!-)(?!.*\.\.)(?!.*\/\/)[A-Za-z0-9._/-]+(?<![./])$/;
@@ -90,7 +94,7 @@ export class GhPullRequestGateway implements PullRequestGateway {
9094
return { ...pr, marker: found[0]! };
9195
}
9296

93-
async refresh(number: number, input: OpenPullRequestInput & { ready: boolean }, signal?: AbortSignal): Promise<OpenedPullRequest> {
97+
async refresh(number: number, input: OpenPullRequestInput & { ready: boolean; headSha?: string }, signal?: AbortSignal): Promise<OpenedPullRequest> {
9498
this.#validate(input);
9599
if (!Number.isSafeInteger(number) || number < 1) throw new Error('Invalid pull request number.');
96100
if (!input.body.includes(input.marker)) throw new Error('The pull request description must carry its marker.');
@@ -100,8 +104,27 @@ export class GhPullRequestGateway implements PullRequestGateway {
100104
// A ready PR whose task went back to needs human becomes a draft again; a draft whose task is ready leaves draft.
101105
if (input.ready && patched.draft) await this.run(['pr', 'ready', String(number), '--repo', this.repository], { signal });
102106
else if (input.draft && !patched.draft) await this.run(['pr', 'ready', String(number), '--undo', '--repo', this.repository], { signal });
103-
const { body, ...pr } = this.#pull(await this.#json(['api', '-H', 'Accept: application/vnd.github+json', `repos/${this.repository}/pulls/${number}`], signal), input);
104-
if (pr.number !== number || !body.includes(input.marker)) throw new Error('GitHub returned a different pull request.');
105-
return pr;
107+
return this.#readBack(number, input, input.headSha, signal);
108+
}
109+
110+
async markDraft(number: number, input: { base: string; headBranch: string; marker: string }, signal?: AbortSignal): Promise<OpenedPullRequest> {
111+
this.#validate(input);
112+
if (!Number.isSafeInteger(number) || number < 1) throw new Error('Invalid pull request number.');
113+
const current = await this.#readBack(number, input, undefined, signal);
114+
if (!current.draft) await this.run(['pr', 'ready', String(number), '--undo', '--repo', this.repository], { signal });
115+
return current.draft ? current : this.#readBack(number, input, undefined, signal);
116+
}
117+
118+
/** Reads the PR back; when `headSha` is given, polls briefly until GitHub shows it, then returns the last answer. */
119+
async #readBack(number: number, input: { base: string; headBranch: string; marker: string }, headSha: string | undefined, signal?: AbortSignal): Promise<OpenedPullRequest> {
120+
for (let poll = 1; ; poll++) {
121+
const { body, ...pr } = this.#pull(await this.#json(['api', '-H', 'Accept: application/vnd.github+json', `repos/${this.repository}/pulls/${number}`], signal), input);
122+
if (pr.number !== number || !body.includes(input.marker)) throw new Error('GitHub returned a different pull request.');
123+
if (headSha === undefined || pr.headSha === headSha || poll >= HEAD_POLLS) return pr;
124+
await new Promise((resolve, reject) => {
125+
const timer = setTimeout(resolve, HEAD_POLL_MS);
126+
signal?.addEventListener('abort', () => { clearTimeout(timer); reject(signal.reason); }, { once: true });
127+
});
128+
}
106129
}
107130
}

‎github/run-with-input.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { spawn } from 'node:child_process';
44
* Runs a command with literal argv and writes `input` to its stdin, so large text never travels as an OS argument
55
* (Linux limits one argument to 128 KiB, and a NUL cannot be passed at all). Settles only after the process closed:
66
* a timeout, an abort or an output past `maxBuffer` sends SIGTERM, then SIGKILL after `killGraceMs` if the process is
7-
* still running, and the promise rejects once it has exited.
7+
* still running, and the promise rejects once it has exited. If a process it started keeps the output pipes open after
8+
* it exits, the pipes are closed after `pipeGraceMs` so the promise still settles.
89
*/
910
export function runWithInput(command: string, args: readonly string[], options: {
10-
input?: string; signal?: AbortSignal; env?: NodeJS.ProcessEnv; timeout?: number; maxBuffer?: number; killGraceMs?: number;
11+
input?: string; signal?: AbortSignal; env?: NodeJS.ProcessEnv; timeout?: number; maxBuffer?: number; killGraceMs?: number; pipeGraceMs?: number;
1112
}): Promise<string> {
1213
return new Promise((resolve, reject) => {
1314
if (options.signal?.aborted) { reject(options.signal.reason ?? new Error('Aborted.')); return; }
@@ -33,14 +34,23 @@ export function runWithInput(command: string, args: readonly string[], options:
3334
child.stdin.on('error', () => { /* the exit status reports a process that stopped reading */ });
3435
child.stdin.end(options.input ?? '');
3536
child.on('error', error => stop(error));
36-
child.on('close', (code, signal) => {
37+
let settled = false, pipes: NodeJS.Timeout | null = null;
38+
const finish = (code: number | null, signal: NodeJS.Signals | null) => {
39+
if (settled) return;
40+
settled = true;
3741
if (timer) clearTimeout(timer);
3842
if (escalation) clearTimeout(escalation);
43+
if (pipes) clearTimeout(pipes);
3944
options.signal?.removeEventListener('abort', onAbort);
4045
const stderr = Buffer.concat(err).toString('utf8').trim();
4146
if (failure) reject(failure);
4247
else if (code !== 0) reject(new Error(`${command} failed (${signal ?? `exit ${code}`}): ${stderr.slice(0, 2000)}`));
4348
else resolve(Buffer.concat(out).toString('utf8'));
49+
};
50+
child.on('close', finish);
51+
// 'close' also waits for the pipes; a grandchild that inherited them can hold them open after this process exits.
52+
child.on('exit', (code, signal) => {
53+
pipes = setTimeout(() => { child.stdout.destroy(); child.stderr.destroy(); finish(code, signal); }, options.pipeGraceMs ?? 1_000);
4454
});
4555
});
4656
}

‎runner/publish.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,15 @@ export class PullRequestPublisher {
9696
}, signal);
9797
signal?.throwIfAborted();
9898
const check = this.#store.recordAlreadyFixed(identity, task.stateVersion, { snapshotId: snapshot.id, draft, result });
99-
if (result.outcome !== 'clear') return draft ? { kind: 'draft skipped', result } : { kind: 'possibly already fixed', result };
99+
if (result.outcome !== 'clear') {
100+
// A task that is not published as ready never leaves its PR ready for review: its earlier ready PR becomes a draft.
101+
// No await since recordAlreadyFixed, which re-read the task; the change only lowers what the PR offers.
102+
if (earlier && live && !live.draft) {
103+
const pr = await this.#pulls.markDraft(live.number, { base: this.#config.baseBranch, headBranch: branch, marker: marker(earlier.openingId) }, signal);
104+
if (earlier.state === 'opened') this.#store.recordPullRequestDraft(identity, earlier.openingId, pr.number, pr.draft);
105+
}
106+
return draft ? { kind: 'draft skipped', result } : { kind: 'possibly already fixed', result };
107+
}
100108
// Checked before the push: a refused publish must not move the branch.
101109
if (earlier && live && earlier.state === 'opened' && live.number !== earlier.number) throw new GuardRefusal('GitHub returned a different pull request for this branch.');
102110
// No await since recordAlreadyFixed, whose transaction re-read the task: the push follows it directly.
@@ -106,7 +114,7 @@ export class PullRequestPublisher {
106114
const stateVersion = this.#store.beginRefresh(identity, { checkId: check.id, openingId: earlier.openingId, headSha: snapshot.head, draft,
107115
...(earlier.state === 'abandoned' ? { adopt: { number: live.number, url: live.url } } : {}) });
108116
const pr = await this.#pulls.refresh(live.number, {
109-
base: earlier.base, headBranch: branch, draft, ready: !draft, marker: marker(earlier.openingId),
117+
base: earlier.base, headBranch: branch, draft, ready: !draft, headSha: snapshot.head, marker: marker(earlier.openingId),
110118
title: pullRequestTitle(plan), body: pullRequestBody({ plan, marker: marker(earlier.openingId), problems: input.problems }),
111119
}, signal);
112120
const status = this.#store.recordPullRequestOpened(identity, earlier.openingId, pr, { head: snapshot.head, stateVersion });
@@ -138,10 +146,12 @@ export class PullRequestPublisher {
138146
// An update whose confirmation was lost is repeated, not adopted: its description may or may not have landed.
139147
const refreshing = this.#store.taskPullRequests(identity).find(pr => pr.refresh !== null);
140148
if (refreshing) this.#store.abandonRefresh(identity, refreshing.openingId);
141-
const lost = this.#store.taskPullRequests(identity).find((pr: TaskPullRequest) => pr.state === 'opening');
149+
// Read once: abandonRefresh above is the only write before this point.
150+
const prs = this.#store.taskPullRequests(identity);
151+
const lost = prs.find((pr: TaskPullRequest) => pr.state === 'opening');
142152
if (!lost) return null;
143153
if (lost.repository.toLowerCase() !== this.#config.repository.toLowerCase()) throw new GuardRefusal('A pull request was being opened in another repository.');
144-
const rows = this.#branchRows(this.#store.taskPullRequests(identity), lost.headBranch, lost.base);
154+
const rows = this.#branchRows(prs, lost.headBranch, lost.base);
145155
const pr = await this.#pulls.findOpened({ base: lost.base, headBranch: lost.headBranch, markers: rows.map(row => marker(row.openingId)) }, signal);
146156
signal?.throwIfAborted();
147157
if (pr && pr.marker !== marker(lost.openingId)) {

‎runner/store.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,15 @@ export class Store {
10341034
return this.#task(key).status as TaskStatus;
10351035
});
10361036
}
1037+
/** Records that the task's open PR is now a draft (after a check matched). The task status does not change. */
1038+
recordPullRequestDraft(identity: PlanIdentity, openingId: string, number: number, draft: boolean): void {
1039+
const key = identityKey(identity);
1040+
this.#transaction(() => {
1041+
if (this.#run("UPDATE task_pull_requests SET draft=?, updated_at=? WHERE plan_key=? AND opening_id=? AND state='opened' AND number=?",
1042+
draft ? 1 : 0, new Date().toISOString(), key, openingId, number).changes !== 1) throw new GuardRefusal('Unknown pull request.');
1043+
this.#touch(key);
1044+
});
1045+
}
10371046
/** Recovery found no PR for an opening whose outcome was lost; a new check and opening follow. */
10381047
abandonPullRequestOpening(identity: PlanIdentity, openingId: string): void {
10391048
const key = identityKey(identity);

0 commit comments

Comments
 (0)