3D engine that AI agents drive
Compare CI size checks with the base the merge was built on
Nguyen Xuan And459030de3f2
diff --git a/.github/workflows/test.yml b/.github/workflows/test.ymlindex acbcc610..0f38ceea 100644--- a/.github/workflows/test.yml+++ b/.github/workflows/test.yml@@ -14,10 +14,10 @@ jobs:test:runs-on: ubuntu-latesttimeout-minutes: 20- env:- SIZE_BASE_REF: origin/${{ github.base_ref }}steps:- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1+ with:+ fetch-depth: 2- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0with:node-version: '24'@@ -25,11 +25,14 @@ jobs:- run: npm install --global [email protected]- run: npm ci --ignore-scripts- run: npm audit --audit-level=high- - name: Fetch the base branch for the size check+ # The tested tree is the merge of the pull request into the base branch as it was when the merge was built. Its first parent+ # is the base of every size, tool cost and archive check, not the newest tip, which may hold work this tree lacks.+ - name: Find the base of the size checksid: baserun: |- git fetch --no-tags --depth=1 origin "${GITHUB_BASE_REF}:refs/remotes/${SIZE_BASE_REF}"- echo "sha=$(git rev-parse "${SIZE_BASE_REF}")" >> "$GITHUB_OUTPUT"+ sha=$(node scripts/ci-base-sha.mjs)+ echo "sha=${sha}" >> "$GITHUB_OUTPUT"+ echo "SIZE_BASE_REF=${sha}" >> "$GITHUB_ENV"- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0with:path: node_modules/.cache/size-basediff --git a/bench/size/README.md b/bench/size/README.mdindex b2e81747..4571ea3f 100644--- a/bench/size/README.md+++ b/bench/size/README.md@@ -47,7 +47,7 @@ Still in the lit box and worth moving next, with rough gzip sizes: skin and morp## How the check decides-The check measures the base branch with the same script, then compares each bundle. In CI the base is the branch that the pull request targets. On your machine it is `origin/main`, from the commit where your branch left it. The base numbers are cached by base commit in `node_modules/.cache/size-base`, and CI caches that folder, so a run on an unchanged base skips the second measurement. The base is measured beside your own bundles, so a cold run costs little more than one measurement.+The check measures the base branch with the same script, then compares each bundle. In CI the tested tree is the merge of the pull request into the target branch, and the base is the first parent of that merge. It is not the newest tip of the target branch, which may hold work that the tree lacks and would charge the pull request for it. `scripts/ci-base-sha.mjs` prints it, and the tool cost and archive checks use the same base. On your machine it is `origin/main`, from the commit where your branch left it. The base numbers are cached by base commit in `node_modules/.cache/size-base`, and CI caches that folder, so a run on an unchanged base skips the second measurement. The base is measured beside your own bundles, so a cold run costs little more than one measurement.A bundle fails when it grows by more than its allowance:diff --git a/scripts/ci-base-sha.mjs b/scripts/ci-base-sha.mjsnew file mode 100644index 00000000..6882acec--- /dev/null+++ b/scripts/ci-base-sha.mjs@@ -0,0 +1,10 @@+// Prints the commit that the size, tool cost and archive checks compare a pull request with: node scripts/ci-base-sha.mjs+// It needs a checkout of the merge commit with at least two commits of history. The Test workflow runs it.+import { pullRequestBaseOf } from './size-base.mjs';++const base = pullRequestBaseOf();+if (!base) {+ console.error('HEAD is not a merge commit, so the pull request base is unknown. Check out the merge of the pull request with fetch-depth 2.');+ process.exit(1);+}+console.log(base);diff --git a/scripts/size-base.mjs b/scripts/size-base.mjsindex c460e6bf..ce23bc6d 100644--- a/scripts/size-base.mjs+++ b/scripts/size-base.mjs@@ -27,7 +27,15 @@ export function baseRefExists(baseRef, cwd = process.cwd()) {return run('git', ['rev-parse', '--verify', '--quiet', `${baseRef}^{commit}`], cwd).status === 0;}-// The commit this branch grew from. On CI the checkout is the pull request merged into the base, so this is the base tip.+// On a pull request, CI checks out the merge of the pull request into the base branch. The first parent of that merge is the+// exact base tip it was built on. Comparing with it charges the pull request only for its own change, even when the base+// branch has moved on since, which the newest tip would count against it. Null when HEAD is not a merge of two parents.+export function pullRequestBaseOf(cwd = process.cwd()) {+ const parents = run('git', ['rev-list', '--parents', '-n', '1', 'HEAD'], cwd).stdout.trim().split(' ');+ return parents.length === 3 ? parents[1] : null;+}++// The commit this branch grew from. On CI the base is the first parent of the merge (see pullRequestBaseOf), so this is that commit.// On a branch that has not caught up with the base, it keeps work merged by others out of this branch's numbers.export function mergeBaseOf(baseRef, cwd = process.cwd()) {const result = run('git', ['merge-base', baseRef, 'HEAD'], cwd);diff --git a/tests/size-growth.test.js b/tests/size-growth.test.jsindex 44755e5c..7087fef1 100644--- a/tests/size-growth.test.js+++ b/tests/size-growth.test.js@@ -4,7 +4,7 @@ import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'nodimport { tmpdir } from 'node:os';import { join } from 'node:path';import test from 'node:test';-import { readAcceptedBundles } from '../scripts/size-base.mjs';+import { mergeBaseOf, pullRequestBaseOf, readAcceptedBundles } from '../scripts/size-base.mjs';import { archiveAllowance, archiveCeilingProblem, archiveGrowthProblem, compareToBase, growthAllowance, parseSizeNote } from '../scripts/size-growth.mjs';const size = gzip => ({ min: gzip * 3, gzip });@@ -109,3 +109,36 @@ test('two pull requests that grow different entries merge in sequence without tormSync(folder, { recursive: true, force: true });}});++test('a pull request behind the base branch is compared with the base it was merged into, not the newest tip', () => {+ const folder = mkdtempSync(join(tmpdir(), 'size-behind-'));+ try {+ git(folder, 'init', '-q', '-b', 'main');+ commitFile(folder, 'src/core.js', 'export const core = 1;\n', 'base');+ const mergedInto = git(folder, 'rev-parse', 'HEAD').trim();++ git(folder, 'checkout', '-q', '-b', 'feature');+ commitFile(folder, 'src/model.js', 'export const model = 2;\n', 'grow model');+ commitFile(folder, 'changes/401.size.md', '- entry:model: new joints\n', 'accept model');++ // The base branch moves on after the merge was built, as when another pull request shrinks core.+ git(folder, 'checkout', '-q', 'main');+ commitFile(folder, 'src/core.js', 'export const core = 0;\n', 'shrink core');+ commitFile(folder, 'changes/402.size.md', '- entry:core: another pull request\n', 'another note');+ git(folder, 'update-ref', 'refs/remotes/origin/main', 'HEAD');++ // CI tests the merge of the pull request into the base as it was: the first parent is the base, the second the pull request.+ git(folder, 'checkout', '-q', '--detach', mergedInto);+ git(folder, 'merge', '-q', '--no-ff', '-m', 'merge ref', 'feature');++ assert.equal(pullRequestBaseOf(folder), mergedInto);+ assert.equal(mergeBaseOf(mergedInto, folder), mergedInto);+ assert.deepEqual([...readAcceptedBundles(mergedInto, folder).keys()], ['entry:model']);+ assert.equal(git(folder, 'diff', '--name-only', mergedInto, 'HEAD').includes('src/core.js'), false);++ git(folder, 'checkout', '-q', 'main');+ assert.equal(pullRequestBaseOf(folder), null);+ } finally {+ rmSync(folder, { recursive: true, force: true });+ }+});