The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(ci): packaged-update acceptance retries a download whose connection resets
ethernetdb4a6a9e264e
diff --git a/tests-js/bundle-inputs.test.mjs b/tests-js/bundle-inputs.test.mjsindex 01589bf75a2..19b4ce2af02 100644--- a/tests-js/bundle-inputs.test.mjs+++ b/tests-js/bundle-inputs.test.mjs@@ -6,7 +6,7 @@ import os from 'node:os'import path from 'node:path'import { fileURLToPath } from 'node:url'import { afterEach, expect, test } from 'vitest'-import { stageBundleInputs } from '../tests/install/e2e-assets/bundle-inputs.mjs'+import { downloadArtifact, stageBundleInputs } from '../tests/install/e2e-assets/bundle-inputs.mjs'import { validateBundleInputs, validateDownloadedBundle } from '../tests/install/e2e-assets/bundle-manifest.cjs'import { validateBundledManifest } from '../tests/install/e2e-assets/windows-bundled-helpers.mjs'import { bundledMatrix, renderMarkdownResults } from '../scripts/sandbox/generate-e2e-matrix.mjs'@@ -151,3 +151,29 @@ test('explicit bundled routes refuse absent package inputs instead of reportingexpect(await readFile(env.GITHUB_STEP_SUMMARY, 'utf8')).toContain('no signed package pair supplied')expect(await readFile(env.GITHUB_OUTPUT, 'utf8')).toContain('windows={"include":[]}')})++test('a download whose connection resets mid-body is retried, a wrong digest is not', async () => {+ const bytes = Buffer.from('x'.repeat(4096))+ let requests = 0+ const server = createServer((req, res) => {+ requests+++ if (requests === 1) {+ res.writeHead(200, { 'content-length': String(bytes.length) })+ res.write(bytes.subarray(0, 100), () => res.socket.destroy())+ return+ }+ res.end(bytes)+ })+ await new Promise(resolve => server.listen(0, '127.0.0.1', resolve))+ cleanup.push(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve) }))+ const directory = await mkdtemp(path.join(os.tmpdir(), 'bundle-retry-test-'))+ cleanup.push(() => rm(directory, { recursive: true, force: true }))+ const url = `http://127.0.0.1:${server.address().port}/new.zip`+ const sha256 = createHash('sha256').update(bytes).digest('hex')+ await downloadArtifact({ url, sha256 }, path.join(directory, 'new.zip'))+ expect(requests).toBe(2)+ expect((await readFile(path.join(directory, 'new.zip'))).equals(bytes)).toBe(true)+ requests = 1+ await expect(downloadArtifact({ url, sha256: 'f'.repeat(64) }, path.join(directory, 'bad.zip'))).rejects.toThrow('SHA-256 mismatch')+ expect(requests).toBe(2)+})diff --git a/tests/install/e2e-assets/bundle-inputs.mjs b/tests/install/e2e-assets/bundle-inputs.mjsindex 462b49ae2d6..e0814a412a9 100644--- a/tests/install/e2e-assets/bundle-inputs.mjs+++ b/tests/install/e2e-assets/bundle-inputs.mjs@@ -10,28 +10,59 @@ import { parseArgs } from 'node:util'import { pathToFileURL } from 'node:url'import { FORMATS, SHA256, artifactUrl, validateBundleInputs } from './bundle-manifest.cjs'-export async function downloadArtifact(artifact, destination) {- const url = artifactUrl(artifact.url)- const response = await fetch(url, { signal: AbortSignal.timeout(30 * 60 * 1000) })- if (!response.ok || !response.body) throw new Error(`Package download returned HTTP ${response.status}`)- const digest = createHash('sha256')- const temporary = `${destination}.partial`+// A reset connection mid-body (ECONNRESET from the CDN) surfaces as `TypeError: terminated`+// and fails the whole acceptance leg for a network blip. A wrong digest or an HTTP error+// answer is a real defect and is never retried.+const TRANSIENT = Symbol('transient')++async function withRetries(attempt, tries = 3) {+ for (let n = 1; ; n++) {+ try {+ return await attempt()+ } catch (error) {+ if (!error?.[TRANSIENT] || n >= tries) throw error+ console.error(`transient network failure (attempt ${n} of ${tries}): ${error.cause?.code ?? error.message}`)+ }+ }+}++async function transient(work) {try {- await pipeline(Readable.fromWeb(response.body), new Transform({- transform(chunk, encoding, done) { digest.update(chunk); done(null, chunk) }- }), createWriteStream(temporary, { flags: 'wx' }))- if (digest.digest('hex') !== artifact.sha256) throw new Error('Package SHA-256 mismatch')- await rename(temporary, destination)+ return await work()} catch (error) {- await rm(temporary, { force: true })+ if (error instanceof TypeError || error?.name === 'TimeoutError') error[TRANSIENT] = truethrow error}}+export async function downloadArtifact(artifact, destination) {+ const url = artifactUrl(artifact.url)+ await withRetries(async () => {+ const digest = createHash('sha256')+ const temporary = `${destination}.partial`+ try {+ await transient(async () => {+ const response = await fetch(url, { signal: AbortSignal.timeout(30 * 60 * 1000) })+ if (!response.ok || !response.body) throw new Error(`Package download returned HTTP ${response.status}`)+ await pipeline(Readable.fromWeb(response.body), new Transform({+ transform(chunk, encoding, done) { digest.update(chunk); done(null, chunk) }+ }), createWriteStream(temporary, { flags: 'wx' }))+ })+ if (digest.digest('hex') !== artifact.sha256) throw new Error('Package SHA-256 mismatch')+ await rename(temporary, destination)+ } catch (error) {+ await rm(temporary, { force: true })+ throw error+ }+ })+}+export async function stageBundleInputs({ manifestUrl, platform, arch, out, expectedCommit, manifestSha256 }) {- const response = await fetch(artifactUrl(manifestUrl), { signal: AbortSignal.timeout(60_000) })- if (!response.ok) throw new Error(`Manifest download returned HTTP ${response.status}`)- const text = await response.text()+ const text = await withRetries(() => transient(async () => {+ const response = await fetch(artifactUrl(manifestUrl), { signal: AbortSignal.timeout(60_000) })+ if (!response.ok) throw new Error(`Manifest download returned HTTP ${response.status}`)+ return response.text()+ }))if (text.length > 1024 * 1024) throw new Error('Bundle input manifest is too large')if (manifestSha256 && (!SHA256.test(manifestSha256) || createHash('sha256').update(text).digest('hex') !== manifestSha256)) {throw new Error('Transition manifest SHA-256 mismatch')