diff --git a/.github/workflows/runner-image.yml b/.github/workflows/runner-image.yml index aa45620b05f..db9f71cb4ea 100644 --- a/.github/workflows/runner-image.yml +++ b/.github/workflows/runner-image.yml @@ -212,6 +212,9 @@ jobs: # Multi-arch by native runner, not QEMU, as in docker.yml. GitHub-hosted # runners: the protected release fleets admit only docker.yml on master. + # Canonical master amd64 pushes use the larger GitHub-hosted runner + # (cloud-release-32) to avoid 4-core CPU bottlenecks in archive compression + # and OCI export; PRs, forks, schedules, and arm64 stay on standard runners. # # These legs cannot write to the registry on any event, and they # write no registry build cache. The install runs dependency lifecycle @@ -235,7 +238,7 @@ jobs: - platform: linux/arm64 runner: ubuntu-24.04-arm arch: arm64 - runs-on: ${{ matrix.runner }} + runs-on: ${{ matrix.arch == 'amd64' && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.event_name == 'push' && github.ref == 'refs/heads/master' && (vars.CORE_PUBLICATION_RUNNER || 'cloud-release-32') || matrix.runner }} timeout-minutes: 60 permissions: contents: read @@ -729,7 +732,7 @@ jobs: - platform: linux/arm64 runner: ubuntu-24.04-arm arch: arm64 - runs-on: ${{ matrix.runner }} + runs-on: ${{ matrix.arch == 'amd64' && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.event_name == 'push' && github.ref == 'refs/heads/master' && (vars.CORE_PUBLICATION_RUNNER || 'cloud-release-32') || matrix.runner }} timeout-minutes: 30 permissions: contents: read diff --git a/scripts/create-provider-pack-archive.mjs b/scripts/create-provider-pack-archive.mjs index 684849050de..b49ba6f0460 100644 --- a/scripts/create-provider-pack-archive.mjs +++ b/scripts/create-provider-pack-archive.mjs @@ -1,6 +1,7 @@ import { createHash } from 'node:crypto'; import { createReadStream, createWriteStream } from 'node:fs'; import { lstat, mkdir, readFile, readdir, stat, writeFile } from 'node:fs/promises'; +import { availableParallelism } from 'node:os'; import { spawn } from 'node:child_process'; import { pipeline } from 'node:stream/promises'; import { constants, createZstdCompress } from 'node:zlib'; @@ -39,9 +40,32 @@ export async function createProviderPackArchive(root) { return receipt; } +async function runBoundedPool(tasks, concurrency) { + if (tasks.length === 0) return []; + const results = new Array(tasks.length); + const workerCount = Math.max(1, Math.min(concurrency, tasks.length)); + let nextIndex = 0; + let failed = false; + const worker = async () => { + while (!failed) { + const index = nextIndex; + nextIndex += 1; + if (index >= tasks.length) return; + try { + results[index] = await tasks[index](); + } catch (error) { + failed = true; + throw error; + } + } + }; + await Promise.all(Array.from({ length: workerCount }, () => worker())); + return results; +} + // Keep large immutable files separate so a fresh sandbox can reuse identical // image bytes without trusting that image's older provider-pack identity. -export async function createProviderPackLayers(root) { +export async function createProviderPackLayers(root, options = {}) { const manifest = JSON.parse(await readFile(resolve(root, 'provider-pack.json'), 'utf8')); if (manifest.schema !== 'paperclip-runner/remote-provider-pack/v1' || !/^sha256:[a-f0-9]{64}$/.test(manifest.digest ?? '')) throw Error('Invalid provider pack identity'); @@ -100,8 +124,16 @@ export async function createProviderPackLayers(root) { for await (const chunk of createReadStream(target)) hash.update(chunk); return { sha256: hash.digest('hex'), bytes: (await stat(target)).size }; }; - const overlay = await archive('overlay.tar.zst', small); - for (const [index, file] of files.entries()) file.archive = await archive(`${index}.tar.zst`, [file.path]); + const requestedConcurrency = Number(options.concurrency); + const concurrency = Number.isInteger(requestedConcurrency) && requestedConcurrency >= 1 + ? requestedConcurrency + : Math.max(1, Math.min(availableParallelism(), 8)); + const tasks = [ + () => archive('overlay.tar.zst', small), + ...files.map((file, index) => () => archive(`${index}.tar.zst`, [file.path])), + ]; + const [overlay, ...fileArchives] = await runBoundedPool(tasks, concurrency); + for (const [index, file] of files.entries()) file.archive = fileArchives[index]; const receipt = { schema: 'paperclip-runner/provider-pack-archive/v2', encoding: 'tar+zstd', providerPackDigest: manifest.digest, overlay, files }; await writeFile(`${root}.archive.json`, JSON.stringify(receipt) + '\n'); diff --git a/server/src/__tests__/runner-image-workflow.test.ts b/server/src/__tests__/runner-image-workflow.test.ts index 8f055443061..a4b10f150c7 100644 --- a/server/src/__tests__/runner-image-workflow.test.ts +++ b/server/src/__tests__/runner-image-workflow.test.ts @@ -282,6 +282,14 @@ describe("Runner image workflow", () => { } }); + it("routes only canonical master amd64 pushes to the larger GitHub-hosted runner", () => { + const runsOn = /^ {4}runs-on: (.+)$/m.exec(jobs.build)?.[1] ?? ""; + expect(runsOn).toBe( + `\${{ matrix.arch == 'amd64' && ${canonical} && (vars.CORE_PUBLICATION_RUNNER || 'cloud-release-32') || matrix.runner }}`, + ); + expect(jobs.build).not.toMatch(/runs-on\/fleet=/); + }); + it("is registered with the merge-review notifier under its own name", () => { const name = /^name: (.+)$/m.exec(workflow)![1]; const listed = /workflow_run:\n {4}workflows:\n((?: {6}- .+\n)+)/.exec(mergeReview)![1].split("\n").filter(Boolean).map((line) => line.trim().slice(2)); diff --git a/server/src/services/native-runtime/provider-pack-archive.test.ts b/server/src/services/native-runtime/provider-pack-archive.test.ts index 000d935e53b..85cc7b8707e 100644 --- a/server/src/services/native-runtime/provider-pack-archive.test.ts +++ b/server/src/services/native-runtime/provider-pack-archive.test.ts @@ -536,6 +536,61 @@ it.skipIf(!tar || !hasZstd).each([ await expect(fs.lstat(targetPath)).rejects.toMatchObject({ code: "ENOENT" }); }); +it("produces byte-identical layered archives and receipts across serial and parallel compression", async () => { + const fixedTime = new Date("2026-01-01T00:00:00.000Z"); + const makeDeterministicPack = async (prefix: string) => { + const root = await fs.mkdtemp(path.join(os.tmpdir(), prefix)); + roots.push(root); + const pack = path.join(root, "provider-pack"); + const distDir = path.join(pack, "dist"); + const nodeModulesDir = path.join(pack, "node_modules"); + const pkgDir = path.join(nodeModulesDir, "pkg"); + await fs.mkdir(distDir, { recursive: true }); + await fs.mkdir(pkgDir, { recursive: true }); + + const files: Array<[string, string | Buffer]> = [ + [path.join(pack, "provider-pack.json"), JSON.stringify({ schema: "paperclip-runner/remote-provider-pack/v1", digest })], + [path.join(pkgDir, "dep-a.bin"), Buffer.alloc(1024 * 1024 + 17, 11)], + [path.join(pkgDir, "dep-b.bin"), Buffer.alloc(1024 * 1024 + 29, 22)], + [path.join(pkgDir, "dep-c.bin"), Buffer.alloc(1024 * 1024 + 41, 33)], + [path.join(distDir, "runner.bin"), Buffer.alloc(1024 * 1024 + 53, 44)], + [path.join(distDir, "small.js"), "export const ok = true;\n"], + ]; + for (const [filePath, content] of files) { + await fs.writeFile(filePath, content); + await fs.utimes(filePath, fixedTime, fixedTime); + } + const linkPath = path.join(distDir, "link.js"); + await fs.symlink("small.js", linkPath); + await fs.lutimes(linkPath, fixedTime, fixedTime); + for (const dirPath of [pkgDir, nodeModulesDir, distDir, pack]) { + await fs.utimes(dirPath, fixedTime, fixedTime); + } + return pack; + }; + + const packSerial = await makeDeterministicPack("provider-archive-serial-"); + const packParallel = await makeDeterministicPack("provider-archive-parallel-"); + const serial = await createProviderPackLayers(packSerial, { concurrency: 1 }); + const parallel = await createProviderPackLayers(packParallel, { concurrency: 4 }); + + expect(parallel).toEqual(serial); + expect(parallel.files.map((file: { path: string }) => file.path)).toEqual([ + "node_modules/pkg/dep-a.bin", + "node_modules/pkg/dep-b.bin", + "node_modules/pkg/dep-c.bin", + "dist/runner.bin", + ]); + expect(await fs.readFile(`${packParallel}.archive.json`, "utf8")).toBe( + await fs.readFile(`${packSerial}.archive.json`, "utf8"), + ); + for (const name of ["overlay.tar.zst", "0.tar.zst", "1.tar.zst", "2.tar.zst", "3.tar.zst"]) { + const bytesSerial = await fs.readFile(path.join(`${packSerial}.archives`, name)); + const bytesParallel = await fs.readFile(path.join(`${packParallel}.archives`, name)); + expect(bytesParallel.equals(bytesSerial), `${name} bytes must match`).toBe(true); + } +}); + async function multiProviderFixture() { const { root, pack } = await fixture(); const large = (fill: number) => Buffer.alloc(1024 * 1024, fill);