diff --git a/.github/scripts/ci-plan.mjs b/.github/scripts/ci-plan.mjs index 262536d5a72..b07fa958b51 100644 --- a/.github/scripts/ci-plan.mjs +++ b/.github/scripts/ci-plan.mjs @@ -9,6 +9,67 @@ const releaseNote = /^releases\/(beta\/)?v[^/]+\.md$/; const validPath = (file) => typeof file === "string" && file.length > 0 && !file.startsWith("/") && !file.split("/").includes("..") && !file.includes("\0"); +export const HOSTED_SHARD_COUNTS = { + "ci:server": 8, + "ci:chat": 1, + "ci:workspaces-a": 1, + "ci:workspaces-b": 1, + "ci:serialized": 4, + "ci:e2e": 4, + "ci:runner-e2e-support": 2, + "ci:runner-package": 3, +}; + +const packageLanePattern = /--package-lane=\d+\/\d+/; +const labelFractionPattern = /\(\d+\/\d+\)/; + +export function compactHostedEntries(entries, hostedShardCounts = HOSTED_SHARD_COUNTS) { + const segments = []; + const byKey = new Map(); + for (const entry of entries) { + const isSharded = Number.isInteger(entry.shard_count) && entry.shard_count > 1; + const isPackageLane = !isSharded && typeof entry.command === "string" && packageLanePattern.test(entry.command); + if (!isSharded && !isPackageLane) { + segments.push({ kind: "single", entry }); + continue; + } + const key = isSharded ? `shard:${entry.task}:${entry.group ?? ""}` : `package-lane:${entry.task}`; + let segment = byKey.get(key); + if (!segment) { + segment = { kind: isSharded ? "shard" : "package-lane", task: entry.task, entries: [] }; + byKey.set(key, segment); + segments.push(segment); + } + segment.entries.push(entry); + } + const compact = []; + for (const segment of segments) { + if (segment.kind === "single") { + compact.push(segment.entry); + continue; + } + const awsCount = segment.entries.length; + const configured = hostedShardCounts[segment.task]; + const target = Number.isInteger(configured) && configured >= 1 ? configured : Math.floor(awsCount / 2); + const hostedCount = Math.min(awsCount, Math.max(1, target)); + const template = segment.entries[0]; + for (let index = 0; index < hostedCount; index += 1) { + const entry = { ...template }; + if (segment.kind === "shard") { + entry.shard_index = index; + entry.shard_count = hostedCount; + if (entry.shard_label) entry.shard_label = `${index + 1}/${hostedCount}`; + if (entry.group_label) entry.group_label = entry.group_label.replace(labelFractionPattern, `(${index + 1}/${hostedCount})`); + } else { + entry.command = entry.command.replace(packageLanePattern, `--package-lane=${index + 1}/${hostedCount}`); + if (entry.lane_label) entry.lane_label = entry.lane_label.replace(labelFractionPattern, `(${index + 1}/${hostedCount})`); + } + compact.push(entry); + } + } + return compact; +} + function validateGlobs(globs) { if (!Array.isArray(globs) || globs.some((glob) => !validPath(glob) || /[!{}[\]?$\\]/.test(glob))) { throw new Error("Unsupported task input pattern"); @@ -86,15 +147,28 @@ export function planTasks({ config, lanes, paths, mode = "enabled", forceReason const jobs = Object.fromEntries(Object.entries(lanes).map(([job, entries]) => { const affected = entries.filter((entry) => reasons[entry.task].length > 0); const selected = mode === "shadow" ? entries : affected; + const hostedAll = compactHostedEntries(entries); + const hostedSelected = compactHostedEntries(selected); + const hosted = hostedAll.length < entries.length + ? { + hosted_matrix: { include: selected.length ? hostedSelected : hostedAll }, + hosted_selected: hostedSelected.length, + hosted_total: hostedAll.length, + } + : {}; return [job, { run: selected.length > 0, matrix: { include: selected.length ? selected : entries }, - selected: selected.length, proposed: affected.length, total: entries.length }]; + selected: selected.length, proposed: affected.length, total: entries.length, ...hosted }]; })); return { version: 1, mode, forceReason, changed, documentation: changed.filter(documentationInput), reasons, jobs }; } export function planSummary(plan) { - const rows = Object.entries(plan.jobs).map(([job, lane]) => - `| ${job} | ${lane.proposed}/${lane.total} | ${lane.selected}/${lane.total} |`); + const rows = Object.entries(plan.jobs).map(([job, lane]) => { + const scheduled = lane.hosted_total !== undefined + ? `${lane.selected}/${lane.total} (hosted: ${lane.hosted_selected}/${lane.hosted_total})` + : `${lane.selected}/${lane.total}`; + return `| ${job} | ${lane.proposed}/${lane.total} | ${scheduled} |`; + }); return ["### Incremental CI", "", `Mode: **${plan.mode}**${plan.forceReason ? ` — ${plan.forceReason}` : ""}`, ...(plan.diff ? [`Diff: \`${plan.diff.base}\` → \`${plan.diff.head}\` (complete Git tree comparison)`, ""] : []), `Documentation inputs: ${(plan.documentation ?? []).join(", ") || "none"}`, diff --git a/.github/scripts/tests/ci-plan.test.mjs b/.github/scripts/tests/ci-plan.test.mjs index c7c163a2242..8cd83ecc22c 100644 --- a/.github/scripts/tests/ci-plan.test.mjs +++ b/.github/scripts/tests/ci-plan.test.mjs @@ -4,7 +4,8 @@ import { execFileSync, spawnSync } from "node:child_process"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; -import { comparisonPaths, planTasks } from "../ci-plan.mjs"; +import vm from "node:vm"; +import { compactHostedEntries, comparisonPaths, HOSTED_SHARD_COUNTS, planSummary, planTasks } from "../ci-plan.mjs"; import { mergeDiff } from "../ci-diff.mjs"; import { planLane } from "../../../scripts/ci-typecheck-lane.mjs"; @@ -16,19 +17,97 @@ const selected = (result, job) => result.jobs[job].run; const selectedTasks = (result) => new Set(Object.values(result.jobs).filter((job) => job.run) .flatMap((job) => job.matrix.include.map((entry) => entry.task))); -test("trusted workflow fallbacks preserve every declared partition and command", () => { +test("trusted workflow fallbacks preserve every declared partition and command for both AWS and hosted runners", () => { const workflow = readFileSync(new URL(".github/workflows/pr-trusted.yml", root), "utf8"); + const fullPlan = plan(["package.json"], { forceReason: "Full suite requested by label" }); + const legacyPlan = structuredClone(fullPlan); + for (const lane of Object.values(legacyPlan.jobs)) delete lane.hosted_matrix; + const awsRunner = "runs-on/fleet=paperclip-public-pr-x64/env=public-ci"; + const evalMatrix = (expression, { runner, planJson }) => vm.runInNewContext(expression, { + fromJSON: JSON.parse, + toJSON: JSON.stringify, + needs: { gate: { outputs: { runner, plan: planJson } }, test_selection: { outputs: { plan: "" } } }, + }); for (const [job, entries] of Object.entries(lanes)) { const block = workflow.split(`\n ${job}:\n`)[1].split(/\n [a-z0-9_]+:\n/)[0]; const normalized = block.replaceAll("(needs.test_selection.outputs.plan || needs.gate.outputs.plan)", "needs.gate.outputs.plan"); assert.ok(normalized.includes(`fromJSON(needs.gate.outputs.plan).jobs.${job}.run`), job); if (!block.includes(" matrix:")) { assert.equal(entries.length, 1); continue; } - const fallback = block.match(/matrix:.*\|\| '(.*)'\) \}\}/)?.[1]; - assert.ok(fallback, `${job}: missing full fallback`); - assert.deepEqual(JSON.parse(fallback), { include: entries }, job); + const expression = block.match(/ matrix: \$\{\{ (.+) \}\}$/m)?.[1]; + assert.ok(expression, `${job}: missing matrix expression`); + const expectedAws = { include: entries }; + const expectedHosted = { include: compactHostedEntries(entries) }; + assert.deepEqual(evalMatrix(expression, { runner: awsRunner, planJson: "" }), expectedAws, `${job} aws fallback`); + assert.deepEqual(evalMatrix(expression, { runner: "ubuntu-latest", planJson: "" }), expectedHosted, `${job} hosted fallback`); + assert.deepEqual(evalMatrix(expression, { runner: awsRunner, planJson: JSON.stringify(fullPlan) }), fullPlan.jobs[job].matrix, `${job} aws plan`); + assert.deepEqual(evalMatrix(expression, { runner: "ubuntu-latest", planJson: JSON.stringify(fullPlan) }), fullPlan.jobs[job].hosted_matrix, `${job} hosted plan`); + assert.deepEqual(evalMatrix(expression, { runner: "ubuntu-latest", planJson: JSON.stringify(legacyPlan) }), legacyPlan.jobs[job].matrix, `${job} legacy plan compat`); } }); +test("compactHostedEntries halves ubuntu-latest fan-out while preserving 100% of tasks, groups, and contiguous shard coverage", () => { + const full = plan(["package.json"], { forceReason: "Full suite requested by label" }); + const awsJobs = Object.values(full.jobs).reduce((sum, lane) => sum + lane.total, 0); + const hostedJobs = Object.values(full.jobs).reduce((sum, lane) => sum + (lane.hosted_total ?? lane.total), 0); + assert.equal(awsJobs, 66); + assert.equal(hostedJobs, 33); + assert.ok(Buffer.byteLength(JSON.stringify(full)) < 32_768, "dual-profile plan stays compact for GITHUB_OUTPUT/env"); + + for (const [job, lane] of Object.entries(full.jobs)) { + if (!lane.hosted_matrix) continue; + const awsEntries = lane.matrix.include; + const hostedEntries = lane.hosted_matrix.include; + assert.ok(hostedEntries.length < awsEntries.length, `${job} should compact on hosted`); + const partitionKey = (entry) => `${entry.task}::${entry.group ?? ""}`; + assert.deepEqual( + [...new Set(hostedEntries.map(partitionKey))], + [...new Set(awsEntries.map(partitionKey))], + `${job} must preserve every task and group`, + ); + const byPartition = new Map(); + for (const entry of hostedEntries) { + const key = partitionKey(entry); + if (!byPartition.has(key)) byPartition.set(key, []); + byPartition.get(key).push(entry); + } + for (const [key, groupEntries] of byPartition) { + if (groupEntries[0].shard_count !== undefined) { + assert.deepEqual( + groupEntries.map((e) => [e.shard_index, e.shard_count]), + groupEntries.map((_, i) => [i, groupEntries.length]), + `${job}/${key} must have contiguous shard indices`, + ); + } else if (typeof groupEntries[0].command === "string" && groupEntries[0].command.includes("--package-lane=")) { + assert.deepEqual( + groupEntries.map((e) => e.command.match(/--package-lane=(\d+\/\d+)/)?.[1]), + groupEntries.map((_, i) => `${i + 1}/${groupEntries.length}`), + `${job}/${key} must have contiguous package lanes`, + ); + } + } + } + + const uiPr = plan(["ui/src/components/Sidebar.tsx"]); + assert.equal(uiPr.jobs.general_tests.selected, 2); + assert.equal(uiPr.jobs.general_tests.hosted_selected, 1); + assert.deepEqual(uiPr.jobs.general_tests.hosted_matrix.include, [ + { group: "general-workspaces-a", group_label: "workspaces-a (1/1)", shard_index: 0, shard_count: 1, task: "ci:workspaces-a" }, + ]); + assert.equal(uiPr.jobs.e2e_shards.selected, 12); + assert.equal(uiPr.jobs.e2e_shards.hosted_selected, 4); + + const futureEntries = Array.from({ length: 10 }, (_, shard_index) => ({ + task: "ci:future", shard_index, shard_count: 10, shard_label: `${shard_index + 1}/10`, + })); + const futureCompact = compactHostedEntries(futureEntries, HOSTED_SHARD_COUNTS); + assert.equal(futureCompact.length, 5); + assert.equal(futureCompact[4].shard_label, "5/5"); + + const summary = planSummary(full); + assert.match(summary, /27\/27 \(hosted: 12\/12\)/); + assert.match(summary, /12\/12 \(hosted: 4\/4\)/); +}); + test("a UI component retains browser and build coverage without unrelated server/Rust jobs", () => { const result = plan(["ui/src/components/Sidebar.tsx"]); assert.equal(selected(result, "build"), true); diff --git a/.github/workflows/pr-trusted.yml b/.github/workflows/pr-trusted.yml index 4bf4ef3b2aa..af6fa83611b 100644 --- a/.github/workflows/pr-trusted.yml +++ b/.github/workflows/pr-trusted.yml @@ -43,6 +43,7 @@ jobs: plan: ${{ steps.publish.outputs.plan }} env: CI_PLAN: ${{ needs.gate.outputs.plan }} + CI_RUNNER_PROFILE: ${{ needs.gate.outputs.runner == 'ubuntu-latest' && 'hosted' || 'aws' }} steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 with: @@ -562,7 +563,7 @@ jobs: strategy: fail-fast: false max-parallel: ${{ needs.gate.outputs.runner == 'ubuntu-latest' && 4 || 256 }} - matrix: ${{ fromJSON((needs.test_selection.outputs.plan || needs.gate.outputs.plan) != '' && toJSON(fromJSON((needs.test_selection.outputs.plan || needs.gate.outputs.plan)).jobs.general_tests.matrix) || '{"include":[{"group":"general-server-without-chat","group_label":"server (1/20)","shard_index":0,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (2/20)","shard_index":1,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (3/20)","shard_index":2,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (4/20)","shard_index":3,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (5/20)","shard_index":4,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (6/20)","shard_index":5,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (7/20)","shard_index":6,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (8/20)","shard_index":7,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (9/20)","shard_index":8,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (10/20)","shard_index":9,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (11/20)","shard_index":10,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (12/20)","shard_index":11,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (13/20)","shard_index":12,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (14/20)","shard_index":13,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (15/20)","shard_index":14,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (16/20)","shard_index":15,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (17/20)","shard_index":16,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (18/20)","shard_index":17,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (19/20)","shard_index":18,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (20/20)","shard_index":19,"shard_count":20,"task":"ci:server"},{"group":"general-chat","group_label":"chat (1/2)","shard_index":0,"shard_count":2,"task":"ci:chat"},{"group":"general-chat","group_label":"chat (2/2)","shard_index":1,"shard_count":2,"task":"ci:chat"},{"group":"general-workspaces-a","group_label":"workspaces-a (1/2)","shard_index":0,"shard_count":2,"task":"ci:workspaces-a"},{"group":"general-workspaces-a","group_label":"workspaces-a (2/2)","shard_index":1,"shard_count":2,"task":"ci:workspaces-a"},{"group":"general-workspaces-b","group_label":"workspaces-b (1/2)","shard_index":0,"shard_count":2,"task":"ci:workspaces-b"},{"group":"general-workspaces-b","group_label":"workspaces-b (2/2)","shard_index":1,"shard_count":2,"task":"ci:workspaces-b"},{"group":"general-workspaces-c","group_label":"workspaces-c","task":"ci:db"}]}') }} + matrix: ${{ fromJSON((needs.test_selection.outputs.plan || needs.gate.outputs.plan) != '' && toJSON((needs.gate.outputs.runner == 'ubuntu-latest' && fromJSON((needs.test_selection.outputs.plan || needs.gate.outputs.plan)).jobs.general_tests.hosted_matrix) || fromJSON((needs.test_selection.outputs.plan || needs.gate.outputs.plan)).jobs.general_tests.matrix) || (needs.gate.outputs.runner == 'ubuntu-latest' && '{"include":[{"group":"general-server-without-chat","group_label":"server (1/8)","shard_index":0,"shard_count":8,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (2/8)","shard_index":1,"shard_count":8,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (3/8)","shard_index":2,"shard_count":8,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (4/8)","shard_index":3,"shard_count":8,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (5/8)","shard_index":4,"shard_count":8,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (6/8)","shard_index":5,"shard_count":8,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (7/8)","shard_index":6,"shard_count":8,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (8/8)","shard_index":7,"shard_count":8,"task":"ci:server"},{"group":"general-chat","group_label":"chat (1/1)","shard_index":0,"shard_count":1,"task":"ci:chat"},{"group":"general-workspaces-a","group_label":"workspaces-a (1/1)","shard_index":0,"shard_count":1,"task":"ci:workspaces-a"},{"group":"general-workspaces-b","group_label":"workspaces-b (1/1)","shard_index":0,"shard_count":1,"task":"ci:workspaces-b"},{"group":"general-workspaces-c","group_label":"workspaces-c","task":"ci:db"}]}' || '{"include":[{"group":"general-server-without-chat","group_label":"server (1/20)","shard_index":0,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (2/20)","shard_index":1,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (3/20)","shard_index":2,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (4/20)","shard_index":3,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (5/20)","shard_index":4,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (6/20)","shard_index":5,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (7/20)","shard_index":6,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (8/20)","shard_index":7,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (9/20)","shard_index":8,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (10/20)","shard_index":9,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (11/20)","shard_index":10,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (12/20)","shard_index":11,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (13/20)","shard_index":12,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (14/20)","shard_index":13,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (15/20)","shard_index":14,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (16/20)","shard_index":15,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (17/20)","shard_index":16,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (18/20)","shard_index":17,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (19/20)","shard_index":18,"shard_count":20,"task":"ci:server"},{"group":"general-server-without-chat","group_label":"server (20/20)","shard_index":19,"shard_count":20,"task":"ci:server"},{"group":"general-chat","group_label":"chat (1/2)","shard_index":0,"shard_count":2,"task":"ci:chat"},{"group":"general-chat","group_label":"chat (2/2)","shard_index":1,"shard_count":2,"task":"ci:chat"},{"group":"general-workspaces-a","group_label":"workspaces-a (1/2)","shard_index":0,"shard_count":2,"task":"ci:workspaces-a"},{"group":"general-workspaces-a","group_label":"workspaces-a (2/2)","shard_index":1,"shard_count":2,"task":"ci:workspaces-a"},{"group":"general-workspaces-b","group_label":"workspaces-b (1/2)","shard_index":0,"shard_count":2,"task":"ci:workspaces-b"},{"group":"general-workspaces-b","group_label":"workspaces-b (2/2)","shard_index":1,"shard_count":2,"task":"ci:workspaces-b"},{"group":"general-workspaces-c","group_label":"workspaces-c","task":"ci:db"}]}')) }} env: PAPERCLIP_CI_FULL: ${{ (contains(github.event.pull_request.labels.*.name, 'full-ci') || vars.CI_INCREMENTAL_MODE == 'full') && '1' || '' }} @@ -807,7 +808,7 @@ jobs: strategy: fail-fast: false max-parallel: ${{ needs.gate.outputs.runner == 'ubuntu-latest' && 4 || 256 }} - matrix: ${{ fromJSON(needs.gate.outputs.plan != '' && toJSON(fromJSON(needs.gate.outputs.plan).jobs.verify_runner_e2e_support.matrix) || '{"include":[{"shard_index":0,"shard_count":4,"shard_label":"1/4","task":"ci:runner-e2e-support"},{"shard_index":1,"shard_count":4,"shard_label":"2/4","task":"ci:runner-e2e-support"},{"shard_index":2,"shard_count":4,"shard_label":"3/4","task":"ci:runner-e2e-support"},{"shard_index":3,"shard_count":4,"shard_label":"4/4","task":"ci:runner-e2e-support"}]}') }} + matrix: ${{ fromJSON(needs.gate.outputs.plan != '' && toJSON((needs.gate.outputs.runner == 'ubuntu-latest' && fromJSON(needs.gate.outputs.plan).jobs.verify_runner_e2e_support.hosted_matrix) || fromJSON(needs.gate.outputs.plan).jobs.verify_runner_e2e_support.matrix) || (needs.gate.outputs.runner == 'ubuntu-latest' && '{"include":[{"shard_index":0,"shard_count":2,"shard_label":"1/2","task":"ci:runner-e2e-support"},{"shard_index":1,"shard_count":2,"shard_label":"2/2","task":"ci:runner-e2e-support"}]}' || '{"include":[{"shard_index":0,"shard_count":4,"shard_label":"1/4","task":"ci:runner-e2e-support"},{"shard_index":1,"shard_count":4,"shard_label":"2/4","task":"ci:runner-e2e-support"},{"shard_index":2,"shard_count":4,"shard_label":"3/4","task":"ci:runner-e2e-support"},{"shard_index":3,"shard_count":4,"shard_label":"4/4","task":"ci:runner-e2e-support"}]}')) }} env: PAPERCLIP_CI_PROFILE: ${{ needs.gate.outputs.profile_ci }} @@ -909,7 +910,7 @@ jobs: strategy: fail-fast: false max-parallel: ${{ needs.gate.outputs.runner == 'ubuntu-latest' && 4 || 256 }} - matrix: ${{ fromJSON(needs.gate.outputs.plan != '' && toJSON(fromJSON(needs.gate.outputs.plan).jobs.verify_paperclip_runner.matrix) || '{"include":[{"lane_label":"static checks","command":"check:static","release_crate":true,"task":"ci:runner-static"},{"lane_label":"rust","command":"check:runner","release_crate":true,"task":"ci:runner-rust"},{"lane_label":"vitest package (1/6)","command":"test:typescript:vitest --package-lane=1/6","task":"ci:runner-package"},{"lane_label":"vitest package (2/6)","command":"test:typescript:vitest --package-lane=2/6","task":"ci:runner-package"},{"lane_label":"vitest package (3/6)","command":"test:typescript:vitest --package-lane=3/6","task":"ci:runner-package"},{"lane_label":"vitest package (4/6)","command":"test:typescript:vitest --package-lane=4/6","task":"ci:runner-package"},{"lane_label":"vitest package (5/6)","command":"test:typescript:vitest --package-lane=5/6","task":"ci:runner-package"},{"lane_label":"vitest package (6/6)","command":"test:typescript:vitest --package-lane=6/6","task":"ci:runner-package"},{"lane_label":"vitest native server","command":"test:typescript:vitest --native-server","release_crate":true,"task":"ci:runner-vitest"}]}') }} + matrix: ${{ fromJSON(needs.gate.outputs.plan != '' && toJSON((needs.gate.outputs.runner == 'ubuntu-latest' && fromJSON(needs.gate.outputs.plan).jobs.verify_paperclip_runner.hosted_matrix) || fromJSON(needs.gate.outputs.plan).jobs.verify_paperclip_runner.matrix) || (needs.gate.outputs.runner == 'ubuntu-latest' && '{"include":[{"lane_label":"static checks","command":"check:static","release_crate":true,"task":"ci:runner-static"},{"lane_label":"rust","command":"check:runner","release_crate":true,"task":"ci:runner-rust"},{"lane_label":"vitest package (1/3)","command":"test:typescript:vitest --package-lane=1/3","task":"ci:runner-package"},{"lane_label":"vitest package (2/3)","command":"test:typescript:vitest --package-lane=2/3","task":"ci:runner-package"},{"lane_label":"vitest package (3/3)","command":"test:typescript:vitest --package-lane=3/3","task":"ci:runner-package"},{"lane_label":"vitest native server","command":"test:typescript:vitest --native-server","release_crate":true,"task":"ci:runner-vitest"}]}' || '{"include":[{"lane_label":"static checks","command":"check:static","release_crate":true,"task":"ci:runner-static"},{"lane_label":"rust","command":"check:runner","release_crate":true,"task":"ci:runner-rust"},{"lane_label":"vitest package (1/6)","command":"test:typescript:vitest --package-lane=1/6","task":"ci:runner-package"},{"lane_label":"vitest package (2/6)","command":"test:typescript:vitest --package-lane=2/6","task":"ci:runner-package"},{"lane_label":"vitest package (3/6)","command":"test:typescript:vitest --package-lane=3/6","task":"ci:runner-package"},{"lane_label":"vitest package (4/6)","command":"test:typescript:vitest --package-lane=4/6","task":"ci:runner-package"},{"lane_label":"vitest package (5/6)","command":"test:typescript:vitest --package-lane=5/6","task":"ci:runner-package"},{"lane_label":"vitest package (6/6)","command":"test:typescript:vitest --package-lane=6/6","task":"ci:runner-package"},{"lane_label":"vitest native server","command":"test:typescript:vitest --native-server","release_crate":true,"task":"ci:runner-vitest"}]}')) }} env: PAPERCLIP_CI_FULL: ${{ (contains(github.event.pull_request.labels.*.name, 'full-ci') || vars.CI_INCREMENTAL_MODE == 'full') && '1' || '' }} @@ -1373,7 +1374,7 @@ jobs: strategy: fail-fast: false max-parallel: ${{ needs.gate.outputs.runner == 'ubuntu-latest' && 4 || 256 }} - matrix: ${{ fromJSON((needs.test_selection.outputs.plan || needs.gate.outputs.plan) != '' && toJSON(fromJSON((needs.test_selection.outputs.plan || needs.gate.outputs.plan)).jobs.verify_serialized_server.matrix) || '{"include":[{"shard_index":0,"shard_count":9,"shard_label":"1/9","task":"ci:serialized"},{"shard_index":1,"shard_count":9,"shard_label":"2/9","task":"ci:serialized"},{"shard_index":2,"shard_count":9,"shard_label":"3/9","task":"ci:serialized"},{"shard_index":3,"shard_count":9,"shard_label":"4/9","task":"ci:serialized"},{"shard_index":4,"shard_count":9,"shard_label":"5/9","task":"ci:serialized"},{"shard_index":5,"shard_count":9,"shard_label":"6/9","task":"ci:serialized"},{"shard_index":6,"shard_count":9,"shard_label":"7/9","task":"ci:serialized"},{"shard_index":7,"shard_count":9,"shard_label":"8/9","task":"ci:serialized"},{"shard_index":8,"shard_count":9,"shard_label":"9/9","task":"ci:serialized"}]}') }} + matrix: ${{ fromJSON((needs.test_selection.outputs.plan || needs.gate.outputs.plan) != '' && toJSON((needs.gate.outputs.runner == 'ubuntu-latest' && fromJSON((needs.test_selection.outputs.plan || needs.gate.outputs.plan)).jobs.verify_serialized_server.hosted_matrix) || fromJSON((needs.test_selection.outputs.plan || needs.gate.outputs.plan)).jobs.verify_serialized_server.matrix) || (needs.gate.outputs.runner == 'ubuntu-latest' && '{"include":[{"shard_index":0,"shard_count":4,"shard_label":"1/4","task":"ci:serialized"},{"shard_index":1,"shard_count":4,"shard_label":"2/4","task":"ci:serialized"},{"shard_index":2,"shard_count":4,"shard_label":"3/4","task":"ci:serialized"},{"shard_index":3,"shard_count":4,"shard_label":"4/4","task":"ci:serialized"}]}' || '{"include":[{"shard_index":0,"shard_count":9,"shard_label":"1/9","task":"ci:serialized"},{"shard_index":1,"shard_count":9,"shard_label":"2/9","task":"ci:serialized"},{"shard_index":2,"shard_count":9,"shard_label":"3/9","task":"ci:serialized"},{"shard_index":3,"shard_count":9,"shard_label":"4/9","task":"ci:serialized"},{"shard_index":4,"shard_count":9,"shard_label":"5/9","task":"ci:serialized"},{"shard_index":5,"shard_count":9,"shard_label":"6/9","task":"ci:serialized"},{"shard_index":6,"shard_count":9,"shard_label":"7/9","task":"ci:serialized"},{"shard_index":7,"shard_count":9,"shard_label":"8/9","task":"ci:serialized"},{"shard_index":8,"shard_count":9,"shard_label":"9/9","task":"ci:serialized"}]}')) }} env: PAPERCLIP_CI_FULL: ${{ (contains(github.event.pull_request.labels.*.name, 'full-ci') || vars.CI_INCREMENTAL_MODE == 'full') && '1' || '' }} @@ -1701,7 +1702,7 @@ jobs: strategy: fail-fast: false max-parallel: ${{ needs.gate.outputs.runner == 'ubuntu-latest' && 4 || 256 }} - matrix: ${{ fromJSON(needs.gate.outputs.plan != '' && toJSON(fromJSON(needs.gate.outputs.plan).jobs.e2e_shards.matrix) || '{"include":[{"shard_index":0,"shard_count":12,"shard_label":"1/12","task":"ci:e2e"},{"shard_index":1,"shard_count":12,"shard_label":"2/12","task":"ci:e2e"},{"shard_index":2,"shard_count":12,"shard_label":"3/12","task":"ci:e2e"},{"shard_index":3,"shard_count":12,"shard_label":"4/12","task":"ci:e2e"},{"shard_index":4,"shard_count":12,"shard_label":"5/12","task":"ci:e2e"},{"shard_index":5,"shard_count":12,"shard_label":"6/12","task":"ci:e2e"},{"shard_index":6,"shard_count":12,"shard_label":"7/12","task":"ci:e2e"},{"shard_index":7,"shard_count":12,"shard_label":"8/12","task":"ci:e2e"},{"shard_index":8,"shard_count":12,"shard_label":"9/12","task":"ci:e2e"},{"shard_index":9,"shard_count":12,"shard_label":"10/12","task":"ci:e2e"},{"shard_index":10,"shard_count":12,"shard_label":"11/12","task":"ci:e2e"},{"shard_index":11,"shard_count":12,"shard_label":"12/12","task":"ci:e2e"}]}') }} + matrix: ${{ fromJSON(needs.gate.outputs.plan != '' && toJSON((needs.gate.outputs.runner == 'ubuntu-latest' && fromJSON(needs.gate.outputs.plan).jobs.e2e_shards.hosted_matrix) || fromJSON(needs.gate.outputs.plan).jobs.e2e_shards.matrix) || (needs.gate.outputs.runner == 'ubuntu-latest' && '{"include":[{"shard_index":0,"shard_count":4,"shard_label":"1/4","task":"ci:e2e"},{"shard_index":1,"shard_count":4,"shard_label":"2/4","task":"ci:e2e"},{"shard_index":2,"shard_count":4,"shard_label":"3/4","task":"ci:e2e"},{"shard_index":3,"shard_count":4,"shard_label":"4/4","task":"ci:e2e"}]}' || '{"include":[{"shard_index":0,"shard_count":12,"shard_label":"1/12","task":"ci:e2e"},{"shard_index":1,"shard_count":12,"shard_label":"2/12","task":"ci:e2e"},{"shard_index":2,"shard_count":12,"shard_label":"3/12","task":"ci:e2e"},{"shard_index":3,"shard_count":12,"shard_label":"4/12","task":"ci:e2e"},{"shard_index":4,"shard_count":12,"shard_label":"5/12","task":"ci:e2e"},{"shard_index":5,"shard_count":12,"shard_label":"6/12","task":"ci:e2e"},{"shard_index":6,"shard_count":12,"shard_label":"7/12","task":"ci:e2e"},{"shard_index":7,"shard_count":12,"shard_label":"8/12","task":"ci:e2e"},{"shard_index":8,"shard_count":12,"shard_label":"9/12","task":"ci:e2e"},{"shard_index":9,"shard_count":12,"shard_label":"10/12","task":"ci:e2e"},{"shard_index":10,"shard_count":12,"shard_label":"11/12","task":"ci:e2e"},{"shard_index":11,"shard_count":12,"shard_label":"12/12","task":"ci:e2e"}]}')) }} env: PAPERCLIP_CI_PROFILE: ${{ needs.gate.outputs.profile_ci }} diff --git a/scripts/ci/select-tests.mjs b/scripts/ci/select-tests.mjs index 9348074b733..6ef2a898ae8 100644 --- a/scripts/ci/select-tests.mjs +++ b/scripts/ci/select-tests.mjs @@ -37,7 +37,7 @@ function runnerInventory(group) { { env, encoding: "utf8", maxBuffer: 16 * 1024 * 1024 })); } -export async function createTestSelection(plan, head) { +export async function createTestSelection(plan, head, { runnerProfile = process.env.CI_RUNNER_PROFILE || "aws" } = {}) { const reason = selectionEligibility(plan); if (reason) throw new Error(reason); const groups = new Set(); @@ -82,7 +82,7 @@ export async function createTestSelection(plan, head) { server.inventory, "Server inventory must be completely covered by existing lane ownership"); } const excludedSuites = activeNightlySuitesForEnv(); - return { ...refineTestPlan({ plan, head, owners, graphs, durations: selectionDurations(), excludedSuites }), graphs }; + return { ...refineTestPlan({ plan, head, owners, graphs, durations: selectionDurations(), excludedSuites, runnerProfile }), graphs }; } async function main() { diff --git a/scripts/ci/test-selection-plan.mjs b/scripts/ci/test-selection-plan.mjs index ef6970653d7..c274a469ec7 100644 --- a/scripts/ci/test-selection-plan.mjs +++ b/scripts/ci/test-selection-plan.mjs @@ -12,14 +12,18 @@ export function partitionSelection(inventory, selected, maxShards, durations = { return selected.length ? partitionGeneralServerSuites(selected, count, durations).map((shard) => shard.files) : []; } -export function refineTestPlan({ plan, head, owners, graphs, durations = {}, excludedSuites = new Set() }) { +export function refineTestPlan({ + plan, head, owners, graphs, durations = {}, excludedSuites = new Set(), runnerProfile = "aws", +}) { const result = structuredClone(plan); const manifest = { version: 1, head, lanes: {} }; const stats = []; const byProject = new Map(graphs.map((graph) => [graph.project, graph])); for (const job of ["general_tests", "verify_serialized_server"]) { if (!plan.jobs[job].run) continue; - const entries = plan.jobs[job].matrix.include; + const entries = runnerProfile === "hosted" && plan.jobs[job].hosted_matrix?.include + ? plan.jobs[job].hosted_matrix.include + : plan.jobs[job].matrix.include; const groups = [...new Set(entries.map((entry) => entry.group ?? "serialized"))]; const matrix = []; for (const group of groups) { @@ -60,8 +64,15 @@ export function refineTestPlan({ plan, head, owners, graphs, durations = {}, exc } stats.push({ group, total: projectByFile.size, selected: selected.length, beforeJobs: original.length, jobs: partitions.length }); } - result.jobs[job] = { ...result.jobs[job], run: matrix.length > 0, selected: matrix.length, proposed: matrix.length, - matrix: { include: matrix.length ? matrix : entries } }; + const refinedMatrix = { include: matrix.length ? matrix : entries }; + result.jobs[job] = { + ...result.jobs[job], + run: matrix.length > 0, + selected: matrix.length, + proposed: matrix.length, + matrix: refinedMatrix, + ...(plan.jobs[job].hosted_matrix ? { hosted_matrix: refinedMatrix, hosted_selected: matrix.length } : {}), + }; } validateSelection(manifest); result.testSelection = { version: 1, head, hash: selectionHash(manifest), stats }; diff --git a/scripts/ci/test-selection-workflow.test.mjs b/scripts/ci/test-selection-workflow.test.mjs index 3e76205685a..129dc53db69 100644 --- a/scripts/ci/test-selection-workflow.test.mjs +++ b/scripts/ci/test-selection-workflow.test.mjs @@ -6,6 +6,7 @@ import path from "node:path"; import vm from "node:vm"; import test from "node:test"; import { planTasks } from "../../.github/scripts/ci-plan.mjs"; +import { refineTestPlan } from "./test-selection-plan.mjs"; const workflow = readFileSync(".github/workflows/pr-trusted.yml", "utf8"); const block = (job) => workflow.split(`\n ${job}:\n`)[1].split(/\n [a-z0-9_]+:\n/)[0]; @@ -99,3 +100,47 @@ test("failed-job retries use the original selector artifact, not the retry attem assert.notEqual(run(selected, "").status, 0, "a selection cannot be published without its uploaded artifact"); assert.equal(run(plan, "").status, 0, "fallback plans need no selection artifact"); }); + +test("ubuntu-latest uses compact hosted matrices in test_selection and on selector fallback", () => { + const selection = block("test_selection"); + assert.match(selection, /CI_RUNNER_PROFILE: \$\{\{ needs\.gate\.outputs\.runner == 'ubuntu-latest' && 'hosted' \|\| 'aws' \}\}/); + + const serverPlan = planTasks({ + config: JSON.parse(readFileSync("turbo.json", "utf8")), + lanes: JSON.parse(readFileSync(".github/ci-task-matrix.json", "utf8")), + paths: ["server/src/routes/issues.ts"], + }); + const hostedFallbackNeeds = { + gate: { result: "success", outputs: { runner: "ubuntu-latest", full_ci: "true", plan: JSON.stringify(serverPlan) } }, + test_selection: { result: "failure", outputs: { plan: "" } }, + }; + const generalMatrix = evaluate(block("general_tests").match(/matrix: \$\{\{ (.*) \}\}/)[1], hostedFallbackNeeds); + const serializedMatrix = evaluate(block("verify_serialized_server").match(/matrix: \$\{\{ (.*) \}\}/)[1], hostedFallbackNeeds); + assert.deepEqual(JSON.parse(JSON.stringify(generalMatrix)), serverPlan.jobs.general_tests.hosted_matrix); + assert.deepEqual(JSON.parse(JSON.stringify(serializedMatrix)), serverPlan.jobs.verify_serialized_server.hosted_matrix); + assert.equal(generalMatrix.include.filter((e) => e.group === "general-server-without-chat").length, 8); + assert.equal(serializedMatrix.include.length, 4); + + const files = Array.from({ length: 40 }, (_, i) => `server/src/s${i}.test.ts`); + const serialFiles = Array.from({ length: 20 }, (_, i) => `server/src/z${i}.test.ts`); + const chatFiles = ["server/src/c0.test.ts"]; + const wsFiles = ["packages/shared/src/a.test.ts"]; + const owners = { + "general-server-without-chat": { "@paperclipai/server": files }, + "general-chat": { "@paperclipai/server": chatFiles }, + "general-workspaces-b": { "@paperclipai/shared": wsFiles }, + serialized: { "@paperclipai/server": serialFiles }, + }; + const graphs = [ + { project: "@paperclipai/server", inventory: [...files, ...serialFiles, ...chatFiles], selected: [...files, ...serialFiles, ...chatFiles] }, + { project: "@paperclipai/shared", inventory: wsFiles, selected: wsFiles }, + ]; + const hostedRefined = refineTestPlan({ plan: serverPlan, head: "a".repeat(40), owners, graphs, runnerProfile: "hosted" }); + const awsRefined = refineTestPlan({ plan: serverPlan, head: "a".repeat(40), owners, graphs, runnerProfile: "aws" }); + assert.equal(hostedRefined.manifest.lanes["general-server-without-chat"].shards.length, 8); + assert.equal(hostedRefined.manifest.lanes.serialized.shards.length, 4); + assert.equal(awsRefined.manifest.lanes["general-server-without-chat"].shards.length, 20); + assert.equal(awsRefined.manifest.lanes.serialized.shards.length, 9); + assert.deepEqual(hostedRefined.plan.jobs.general_tests.hosted_matrix, hostedRefined.plan.jobs.general_tests.matrix); + assert.deepEqual(hostedRefined.plan.jobs.verify_serialized_server.hosted_matrix, hostedRefined.plan.jobs.verify_serialized_server.matrix); +});