Skip to content

Commit 6224135

Browse files
SebTardifsteipete
andauthored
fix(repair): bound GitHub CLI calls with per-call deadlines (#1302)
Use the child environment for timeout selection and preserve native process failure semantics. Keep fractional explicit budgets positive after rounding. Co-authored-by: Peter Steinberger <[email protected]>
1 parent 1b90866 commit 6224135

4 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ checkpoint, and status-only commits are intentionally omitted.
7272

7373
### Fixed
7474

75+
- Bound shared repair GitHub CLI calls with native process deadlines and honor per-call timeout settings. Thanks @SebTardif.
7576
- Bound standalone webhook GitHub requests to 15 seconds, including response bodies, so stalled calls return a retryable response. Thanks @SebTardif.
7677
- Completed obsolete exact-review publications when apply verifies a strictly newer durable review for the same revision, preserving retry behavior for unproven results. Thanks @vincentkoc. (#1249)
7778

‎docs/repair/README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,10 @@ The workflow needs:
395395
request fails in time for the executor to write a blocked report and upload
396396
debug artifacts. `CLAWSWEEPER_GIT_NETWORK_TIMEOUT_MS` and
397397
`CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS` can override the Git and GitHub CLI
398-
portions separately.
398+
portions separately. Shared repair GitHub CLI calls use the merged per-call
399+
environment for these settings, default to two minutes, and enforce a
400+
30-second minimum for environment-configured budgets. An explicit `timeoutMs`
401+
call option takes precedence and may select a shorter positive deadline.
399402
- optional `CLAWSWEEPER_CODEX_REVIEW_ATTEMPTS` and `CLAWSWEEPER_RESOLVE_REVIEW_THREADS` variables for agentic merge-prep review loops; the review attempt default is `4`, with the last failed internal review converted into one final Codex review-fix pass when changed-surface validation can still prove the branch safe to push for exact-head review
400403
- optional `CLAWSWEEPER_MAX_REPAIRS_PER_PR` and
401404
`CLAWSWEEPER_MAX_REPAIRS_PER_HEAD` variables for trusted

‎src/repair/github-cli.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type GhRunOptions = {
1515
cwd?: string;
1616
env?: NodeJS.ProcessEnv;
1717
input?: string;
18+
timeoutMs?: number;
1819
};
1920

2021
export type GhRetryOptions = GhRunOptions & {
@@ -164,6 +165,7 @@ export function ghText(ghArgs: string[], options: GhRunOptions = {}): string {
164165
const command = ghCommand(ghArgs, env);
165166
const text = execFileSync(command.command, command.args, {
166167
cwd: options.cwd ?? repoRoot(),
168+
timeout: ghRunTimeoutMs(options, env),
167169
env,
168170
encoding: "utf8",
169171
input: options.input,
@@ -256,6 +258,7 @@ export async function ghTextAsync(ghArgs: string[], options: GhRunOptions = {}):
256258
const command = ghCommand(ghArgs, env);
257259
const { stdout } = await execFileAsync(command.command, command.args, {
258260
cwd: options.cwd ?? repoRoot(),
261+
timeout: ghRunTimeoutMs(options, env),
259262
env,
260263
encoding: "utf8",
261264
maxBuffer: 64 * 1024 * 1024,
@@ -287,6 +290,7 @@ export function ghSpawn(ghArgs: string[], options: GhRunOptions = {}) {
287290
const command = ghCommand(ghArgs, env);
288291
return spawnSync(command.command, command.args, {
289292
cwd: options.cwd ?? repoRoot(),
293+
timeout: ghRunTimeoutMs(options, env),
290294
encoding: "utf8",
291295
env,
292296
input: options.input,
@@ -376,6 +380,22 @@ export function ghStdoutFromError(error: unknown): string {
376380
).trim();
377381
}
378382

383+
function ghRunTimeoutMs(options: GhRunOptions, env: NodeJS.ProcessEnv): number {
384+
if (
385+
options.timeoutMs !== undefined &&
386+
Number.isFinite(options.timeoutMs) &&
387+
options.timeoutMs > 0
388+
) {
389+
return Math.max(1, Math.floor(options.timeoutMs));
390+
}
391+
const configured = Number(
392+
env.CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS ?? env.CLAWSWEEPER_NETWORK_COMMAND_TIMEOUT_MS,
393+
);
394+
return Number.isFinite(configured) && configured > 0
395+
? Math.max(30_000, Math.floor(configured))
396+
: 120_000;
397+
}
398+
379399
function resolveRetryOptions(options: GhRetryOptions | number): GhRetryOptions {
380400
if (typeof options === "number") return { attempts: options };
381401
if (options.attempts !== undefined) return options;

‎test/repair/github-cli.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import childProcess from "node:child_process";
2+
import { syncBuiltinESMExports } from "node:module";
13
import assert from "node:assert/strict";
24
import test from "node:test";
35

@@ -6,6 +8,8 @@ import {
68
ghJsonWithRetry,
79
ghJsonWithRetryAsync,
810
ghSpawn,
11+
ghText,
12+
ghTextAsync,
913
githubLimitedPagePath,
1014
githubPaginatedPath,
1115
} from "../../dist/repair/github-cli.js";
@@ -183,3 +187,71 @@ test("public read throttles fall back once to the target App token", async () =>
183187
}
184188
}
185189
});
190+
191+
test("GitHub CLI deadlines stop stalled children and preserve successful output", async () => {
192+
const env = {
193+
GH_BIN: process.execPath,
194+
GH_BIN_ARGS: JSON.stringify(["--eval", "setTimeout(() => {}, 10_000)", "--"]),
195+
};
196+
const options = { env, timeoutMs: 100 };
197+
assert.throws(() => ghText(["api", "user"], options), { code: "ETIMEDOUT" });
198+
await assert.rejects(ghTextAsync(["api", "user"], options), {
199+
killed: true,
200+
signal: "SIGTERM",
201+
});
202+
assert.equal(ghSpawn(["api", "user"], options).error?.code, "ETIMEDOUT");
203+
204+
env.GH_BIN_ARGS = JSON.stringify(["--eval", "process.stdout.write('ok')", "--"]);
205+
assert.equal(ghText(["api", "user"], { env, timeoutMs: 2_000 }), "ok");
206+
assert.equal(await ghTextAsync(["api", "user"], { env, timeoutMs: 2_000 }), "ok");
207+
assert.equal(ghSpawn(["api", "user"], { env, timeoutMs: 2_000 }).stdout, "ok");
208+
});
209+
210+
test("GitHub CLI deadline selection follows the child environment and explicit budget", (t) => {
211+
let observedTimeout: number | undefined;
212+
t.mock.method(childProcess, "spawnSync", (_file, _args, options) => {
213+
observedTimeout = options.timeout;
214+
return { status: 0, stdout: "", stderr: "" };
215+
});
216+
syncBuiltinESMExports();
217+
t.after(() => {
218+
t.mock.restoreAll();
219+
syncBuiltinESMExports();
220+
});
221+
const previous = process.env.CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS;
222+
process.env.CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS = "90000";
223+
t.after(() => {
224+
if (previous === undefined) delete process.env.CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS;
225+
else process.env.CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS = previous;
226+
});
227+
const cases = [
228+
{ env: {}, expected: 90_000 },
229+
{ env: { CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS: "45000" }, expected: 45_000 },
230+
{ env: { CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS: "10" }, expected: 30_000 },
231+
{
232+
env: {
233+
CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS: undefined,
234+
CLAWSWEEPER_NETWORK_COMMAND_TIMEOUT_MS: "60000",
235+
},
236+
expected: 60_000,
237+
},
238+
{
239+
env: {
240+
CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS: undefined,
241+
CLAWSWEEPER_NETWORK_COMMAND_TIMEOUT_MS: undefined,
242+
},
243+
expected: 120_000,
244+
},
245+
...["", "invalid", "0", "-1", "Infinity"].map((value) => ({
246+
env: { CLAWSWEEPER_GH_COMMAND_TIMEOUT_MS: value },
247+
expected: 120_000,
248+
})),
249+
{ timeoutMs: 250, env: {}, expected: 250 },
250+
{ timeoutMs: 0.5, env: {}, expected: 1 },
251+
...[0, -1, NaN, Infinity].map((timeoutMs) => ({ timeoutMs, env: {}, expected: 90_000 })),
252+
];
253+
for (const { expected, ...options } of cases) {
254+
ghSpawn(["api", "user"], { ...options, env: { ...options.env, GH_BIN: process.execPath } });
255+
assert.equal(observedTimeout, expected);
256+
}
257+
});

0 commit comments

Comments
 (0)