Skip to content

Commit 303c47e

Browse files
committed
fix: reconcile uncertain comment creates instead of re-POSTing; add regressions
Retry now wraps the whole list -> write cycle rather than each request, so a create whose response was lost (network error / 5xx) is re-listed on the next attempt and turned into a PATCH of the marker comment instead of a duplicate POST. Splits the retry/classify helpers under the fallow complexity gate. Adds scripts/__tests__/size-report-post-comment.test.ts (unit-core): spawns the real script against a stubbed fetch and pins uncertain-create reconciliation, transient exhaustion (warn + exit 0), and fatal 4xx (nonzero, no retry). SIZE_REPORT_RETRY_BASE_MS lets the tests skip real backoff.
1 parent 2baae33 commit 303c47e

3 files changed

Lines changed: 162 additions & 27 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import assert from 'node:assert/strict';
2+
import { execFile } from 'node:child_process';
3+
import { mkdtemp, readFile, writeFile } from 'node:fs/promises';
4+
import { tmpdir } from 'node:os';
5+
import { join } from 'node:path';
6+
import { promisify } from 'node:util';
7+
import { test } from 'vitest';
8+
9+
const execFileAsync = promisify(execFile);
10+
const ROOT = join(import.meta.dirname, '..', '..');
11+
const SCRIPT = join(ROOT, 'scripts', 'size-report.mjs');
12+
const MARKER = '<!-- agent-device-size-report -->';
13+
14+
// Each entry answers one fetch call, in order. `net` rejects the fetch (a
15+
// dropped connection); a number is the HTTP status; `body` is the response
16+
// text (defaults to `[]` for 200 so a list call sees no comments).
17+
type StubResponse = { status: number | 'net'; body?: string };
18+
19+
const FETCH_STUB = `
20+
import fs from 'node:fs';
21+
const script = JSON.parse(process.env.SIZE_REPORT_FETCH_SCRIPT);
22+
let index = 0;
23+
globalThis.fetch = async (url, init) => {
24+
const step = script[Math.min(index, script.length - 1)];
25+
index += 1;
26+
fs.appendFileSync(process.env.SIZE_REPORT_FETCH_LOG, \`\${init?.method ?? 'GET'} \${url}\\n\`);
27+
if (step.status === 'net') throw new Error('ECONNRESET');
28+
const body = step.body ?? (step.status === 200 ? '[]' : \`{"message":"status \${step.status}"}\`);
29+
return new Response(body, { status: step.status });
30+
};
31+
`;
32+
33+
async function runPostComment(script: StubResponse[]) {
34+
const dir = await mkdtemp(join(tmpdir(), 'size-report-post-comment-'));
35+
const stubPath = join(dir, 'fetch-stub.mjs');
36+
const reportPath = join(dir, 'report.md');
37+
const logPath = join(dir, 'fetch.log');
38+
const summaryPath = join(dir, 'summary.md');
39+
await Promise.all([
40+
writeFile(stubPath, FETCH_STUB),
41+
writeFile(reportPath, `${MARKER}\n# report\n`),
42+
writeFile(logPath, ''),
43+
writeFile(summaryPath, ''),
44+
]);
45+
const env = {
46+
...process.env,
47+
GITHUB_TOKEN: 'token',
48+
GITHUB_REPOSITORY: 'owner/repo',
49+
GITHUB_PR_NUMBER: '1',
50+
GITHUB_STEP_SUMMARY: summaryPath,
51+
SIZE_REPORT_RETRY_BASE_MS: '1',
52+
SIZE_REPORT_FETCH_SCRIPT: JSON.stringify(script),
53+
SIZE_REPORT_FETCH_LOG: logPath,
54+
};
55+
let exitCode = 0;
56+
let stdout = '';
57+
let stderr = '';
58+
try {
59+
({ stdout, stderr } = await execFileAsync(
60+
process.execPath,
61+
['--import', stubPath, SCRIPT, '--post-comment', reportPath],
62+
{ env },
63+
));
64+
} catch (error) {
65+
const failure = error as { code?: number; stdout?: string; stderr?: string };
66+
exitCode = failure.code ?? 1;
67+
stdout = failure.stdout ?? '';
68+
stderr = failure.stderr ?? '';
69+
}
70+
const calls = (await readFile(logPath, 'utf8')).trim().split('\n').filter(Boolean);
71+
const summary = await readFile(summaryPath, 'utf8');
72+
return { exitCode, stdout, stderr, calls, summary };
73+
}
74+
75+
const COMMENTS_URL = 'https://api.github.com/repos/owner/repo/issues/1/comments';
76+
const EXISTING_COMMENT_URL = `${COMMENTS_URL}/42`;
77+
const existingMarkerComment = JSON.stringify([
78+
{ url: EXISTING_COMMENT_URL, body: `${MARKER}\nold` },
79+
]);
80+
81+
test('a create whose response was lost is reconciled into an update, not a duplicate', async () => {
82+
const result = await runPostComment([
83+
{ status: 200 }, // list: nothing yet
84+
{ status: 'net' }, // create: connection dropped, but it landed server-side
85+
{ status: 200, body: existingMarkerComment }, // re-list: marker comment now exists
86+
{ status: 200 }, // update
87+
]);
88+
assert.equal(result.exitCode, 0, result.stderr);
89+
assert.deepEqual(result.calls, [
90+
`GET ${COMMENTS_URL}?per_page=100`,
91+
`POST ${COMMENTS_URL}`,
92+
`GET ${COMMENTS_URL}?per_page=100`,
93+
`PATCH ${EXISTING_COMMENT_URL}`,
94+
]);
95+
});
96+
97+
test('transient failures that outlast the retries warn and exit 0', async () => {
98+
const result = await runPostComment([{ status: 503 }]);
99+
assert.equal(result.exitCode, 0, result.stderr);
100+
assert.match(
101+
result.stdout,
102+
/^::warning::Skipping PR size comment after transient GitHub failure: .*503/m,
103+
);
104+
assert.match(result.summary, /Skipping PR size comment/);
105+
assert.equal(result.calls.length, 4, 'one list call per attempt');
106+
});
107+
108+
test('a non-transient 4xx is fatal and is not retried', async () => {
109+
const result = await runPostComment([{ status: 200 }, { status: 401 }]);
110+
assert.notEqual(result.exitCode, 0);
111+
assert.match(result.stderr, /Failed to create PR comment: 401/);
112+
assert.doesNotMatch(result.stdout, /::warning::/);
113+
assert.equal(result.calls.length, 2, 'no retry after a fatal status');
114+
});

‎scripts/size-report.mjs‎

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { gzipSync } from 'node:zlib';
77

88
const COMMENT_MARKER = '<!-- agent-device-size-report -->';
99
const GITHUB_REQUEST_ATTEMPTS = 4;
10-
const GITHUB_RETRY_BASE_MS = 1000;
10+
// Overridable so the regression tests do not sleep through real backoff.
11+
const GITHUB_RETRY_BASE_MS = Number(process.env.SIZE_REPORT_RETRY_BASE_MS ?? 1000);
1112
class TransientGitHubError extends Error {}
1213
const VALUE_ARGS = new Map([
1314
['--cwd', 'cwd'],
@@ -387,9 +388,16 @@ async function postGitHubComment(markdownPath, explicitPrNumber) {
387388
const config = readGitHubCommentConfig(explicitPrNumber);
388389
const body = fs.readFileSync(markdownPath, 'utf8');
389390
const commentsUrl = buildCommentsUrl(config.repository, config.prNumber);
390-
const comments = await listGitHubComments(commentsUrl, config.headers);
391+
await retryTransient(() => syncGitHubComment(commentsUrl, config.headers, body));
392+
}
393+
394+
// Every attempt re-lists before writing: a create whose response was lost
395+
// (network error / 5xx) may still have landed server-side, and re-listing turns
396+
// that into an update of the existing marker comment instead of a duplicate.
397+
async function syncGitHubComment(commentsUrl, headers, body) {
398+
const comments = await listGitHubComments(commentsUrl, headers);
391399
const existing = comments.find((comment) => comment.body?.includes(COMMENT_MARKER));
392-
await writeGitHubComment(commentsUrl, config.headers, body, existing?.url);
400+
await writeGitHubComment(commentsUrl, headers, body, existing?.url);
393401
}
394402

395403
function readGitHubCommentConfig(explicitPrNumber) {
@@ -453,40 +461,50 @@ function commentWriteTarget(commentsUrl, existingUrl) {
453461
return { url: commentsUrl, method: 'POST', action: 'create' };
454462
}
455463

456-
// Retries 5xx / 429 / network errors with exponential backoff; any other
457-
// non-OK status is a configuration problem and throws a plain (fatal) Error.
458-
async function githubRequest(url, init, action) {
459-
let failure = '';
460-
for (let attempt = 1; attempt <= GITHUB_REQUEST_ATTEMPTS; attempt += 1) {
461-
const result = await attemptGitHubRequest(url, init, action);
462-
if (result.response) return result.response;
463-
failure = result.failure;
464-
if (attempt < GITHUB_REQUEST_ATTEMPTS) {
465-
const delayMs = GITHUB_RETRY_BASE_MS * 2 ** (attempt - 1);
466-
process.stderr.write(`${failure} (retrying in ${delayMs}ms)\n`);
467-
await new Promise((resolve) => setTimeout(resolve, delayMs));
464+
// Re-runs `operation` with exponential backoff while it throws
465+
// TransientGitHubError; any other error (a non-transient HTTP status, i.e. a
466+
// configuration problem) propagates immediately and fails the job.
467+
async function retryTransient(operation) {
468+
for (let attempt = 1; ; attempt += 1) {
469+
try {
470+
return await operation();
471+
} catch (error) {
472+
await backoffOrRethrow(error, attempt);
468473
}
469474
}
470-
throw new TransientGitHubError(`${failure} after ${GITHUB_REQUEST_ATTEMPTS} attempts`);
471475
}
472476

473-
// Resolves to { response } on success or { failure } on a transient failure;
474-
// throws a plain Error on a non-transient one.
475-
async function attemptGitHubRequest(url, init, action) {
476-
let response;
477+
async function backoffOrRethrow(error, attempt) {
478+
if (!(error instanceof TransientGitHubError)) throw error;
479+
if (attempt >= GITHUB_REQUEST_ATTEMPTS) {
480+
throw new TransientGitHubError(`${error.message} after ${attempt} attempts`);
481+
}
482+
const delayMs = GITHUB_RETRY_BASE_MS * 2 ** (attempt - 1);
483+
process.stderr.write(`${error.message} (retrying in ${delayMs}ms)\n`);
484+
await new Promise((resolve) => setTimeout(resolve, delayMs));
485+
}
486+
487+
// One attempt: network errors and 5xx / 429 throw TransientGitHubError;
488+
// any other non-OK status throws a plain (fatal) Error.
489+
async function githubRequest(url, init, action) {
490+
const response = await fetchOrTransient(url, init, action);
491+
if (response.ok) return response;
492+
throw await githubStatusError(response, action);
493+
}
494+
495+
async function fetchOrTransient(url, init, action) {
477496
try {
478-
response = await fetch(url, init);
497+
return await fetch(url, init);
479498
} catch (error) {
480-
return { failure: `Failed to ${action}: ${error?.message ?? error}` };
499+
throw new TransientGitHubError(`Failed to ${action}: ${error?.message ?? error}`);
481500
}
482-
return await classifyGitHubResponse(response, action);
483501
}
484502

485-
async function classifyGitHubResponse(response, action) {
486-
if (response.ok) return { response };
503+
async function githubStatusError(response, action) {
487504
const failure = `Failed to ${action}: ${response.status} ${await response.text()}`;
488-
if (isTransientGitHubStatus(response.status)) return { failure };
489-
throw new Error(failure);
505+
return isTransientGitHubStatus(response.status)
506+
? new TransientGitHubError(failure)
507+
: new Error(failure);
490508
}
491509

492510
function isTransientGitHubStatus(status) {

‎vitest.config.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ export default defineConfig({
8484
// only place the gate's failure direction is exercised at all (the gate itself needs a
8585
// real `npm pack`, so CI can only watch a healthy package pass).
8686
'scripts/__tests__/package-closure-audit.test.ts',
87+
// The Bundle Size lane's PR-comment path: spawns the real script against a
88+
// stubbed fetch, so it needs no network; pins retry/reconcile/fatal outcomes.
89+
'scripts/__tests__/size-report-post-comment.test.ts',
8790
// Parses CI configuration only, so this action guard needs no device or subprocess lane.
8891
'test/ci/upload-agent-device-artifacts.test.ts',
8992
// The frozen replay-compat corpus (#1417): parse-only, no device or

0 commit comments

Comments
 (0)