From 343abeb6fa223cd7cd8cbcfdc91a6a5b5b265456 Mon Sep 17 00:00:00 2001 From: Olivier Mattelaer Date: Sun, 22 Nov 2026 23:36:08 +0100 Subject: [PATCH] CI: save the madtrex cache on main only, build it in warm_cache acceptancetest_mg7_madtrex saved its content-addressed madtrex/install cache from every branch and PR on a miss. It is the only consumer of that build, so the save is now gated on refs/heads/main: a PR with main's madtrex/madspace sources restores main's entry, any other PR builds it in the job without leaving a per-PR cache behind. warm_cache.yml gets a main-only madtrex_cache job (same key), which restores it -- keeping it warm on the scheduled run -- and on a miss builds madspace + madtrex, checks the install with reweight_backend.madtrex_is_installed/madtrex_build_problem and saves it. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/acceptancetest_mg7.yml | 9 ++- .github/workflows/warm_cache.yml | 84 +++++++++++++++++++++++- 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/.github/workflows/acceptancetest_mg7.yml b/.github/workflows/acceptancetest_mg7.yml index e10246d0a2..f52929250f 100644 --- a/.github/workflows/acceptancetest_mg7.yml +++ b/.github/workflows/acceptancetest_mg7.yml @@ -770,6 +770,13 @@ jobs: # on the madtrex sources and on the madspace sources it is built against # (not madspace/install, restored above). Saved right after the build, so # that a failing test below does not throw the build away. + # Saved on main only (here on a main push, and by the madtrex_cache job of + # warm_cache.yml, which also keeps it warm): a PR restores main's entry + # when it touches neither madtrex/ nor madspace/, and otherwise builds it + # in this job without leaving a per-PR cache behind. This job is the only + # consumer, so nothing else in the run needs the PR's build. + # The key expression is duplicated in warm_cache.yml (madtrex_cache): + # keep the two in sync. - name: madtrex cache key run: | echo "MADTREX_KEY=madtrex-${{ runner.os }}-py$(python3 -c 'import sys;print(f"{sys.version_info.major}{sys.version_info.minor}")')-${{ hashFiles('madtrex/**', 'madspace/**', '!madspace/install/**') }}" >> $GITHUB_ENV @@ -785,7 +792,7 @@ jobs: cd $GITHUB_WORKSPACE python3 madtrex/install.py --yes -j 4 - name: Save madtrex install - if: steps.cache-madtrex.outputs.cache-hit != 'true' + if: steps.cache-madtrex.outputs.cache-hit != 'true' && github.ref == 'refs/heads/main' uses: actions/cache/save@v5 with: path: madtrex/install diff --git a/.github/workflows/warm_cache.yml b/.github/workflows/warm_cache.yml index dfe363483a..9340ae4c9f 100644 --- a/.github/workflows/warm_cache.yml +++ b/.github/workflows/warm_cache.yml @@ -991,8 +991,8 @@ jobs: # On the scheduled run the heptools rebuild chain is skipped (the *_cache jobs # are guarded with github.event_name != 'schedule'). Restore the combined # heptools cache here so its 7-day eviction timer is reset and it stays warm - # without recompiling anything. ufo/numpy/root/delphes already keep themselves - # warm via their own restores on the scheduled run. + # without recompiling anything. ufo/numpy/root/delphes/madtrex already keep + # themselves warm via their own restores on the scheduled run. keep-warm-heptools: if: github.event_name == 'schedule' runs-on: ${{ matrix.os }} @@ -1191,3 +1191,83 @@ jobs: with: path: ~/.cache/Delphes key: delphes-${{ env.CACHE_KEY }}-${{ github.run_id }} + + # --------------------------------------------------------------------------- + # MadtRex install prefix (madtrex/install), read by the + # acceptancetest_mg7_madtrex job of acceptancetest_mg7.yml. Unlike the caches + # above its key is content-addressed (hash of the madtrex and madspace + # sources), not generation-suffixed: the same sources always give the same + # build, so an exact-key restore is both the "already cached?" test and the + # keep-warm touch on the scheduled run, and a rebuild only happens on a real + # miss (new sources on main, or eviction). + # + # Main only, whatever the trigger, like root_cache/delphes_cache: the + # acceptance job saves it on a main push only too, so no branch or PR ever + # writes a madtrex entry. A PR whose madtrex/madspace sources equal main's + # restores this one (a PR can read its base branch's caches). + # + # The madspace install it is built against comes from the build_madspace + # action, which restores (and so keeps warm) main's madspace entry, or + # rebuilds and saves it on a miss. + # --------------------------------------------------------------------------- + madtrex_cache: + if: github.ref == 'refs/heads/main' + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v5 + + # Must match the key of acceptancetest_mg7.yml (acceptancetest_mg7_madtrex) + # exactly. Computed before anything is built, so no build tree can leak + # into the madspace/** hash; madspace/install is excluded as it is there. + - name: madtrex cache key + run: | + echo "MADTREX_KEY=madtrex-${{ runner.os }}-py$(python3 -c 'import sys;print(f"{sys.version_info.major}{sys.version_info.minor}")')-${{ hashFiles('madtrex/**', 'madspace/**', '!madspace/install/**') }}" >> $GITHUB_ENV + + - name: Restore (touch) the madtrex install + id: cache-madtrex + uses: actions/cache/restore@v5 + with: + path: madtrex/install + key: ${{ env.MADTREX_KEY }} + + - name: Build (or restore) madspace + if: steps.cache-madtrex.outputs.cache-hit != 'true' + uses: ./.github/actions/build_madspace + + - name: Put madspace on PYTHONPATH + if: steps.cache-madtrex.outputs.cache-hit != 'true' + run: echo "PYTHONPATH=$GITHUB_WORKSPACE/madspace/install:$PYTHONPATH" >> "$GITHUB_ENV" + + # numpy from the pip cache, as in the acceptance job, so install.py finds + # it importable and does not put its own copy into madtrex/install. + - name: Restore the pip cache (numpy) + if: steps.cache-madtrex.outputs.cache-hit != 'true' + uses: ./.github/actions/restore-pip-cache + + - name: Install madtrex + if: steps.cache-madtrex.outputs.cache-hit != 'true' + run: | + cd $GITHUB_WORKSPACE + python3 madtrex/install.py --yes -j 4 + + # Check the result with the same tests MadGraph uses to accept an + # installed madtrex before publishing it (the acceptance tests skip, and + # so fail, without a usable one). + - name: Verify madtrex landed in the install prefix + if: steps.cache-madtrex.outputs.cache-hit != 'true' + run: | + cd $GITHUB_WORKSPACE + python3 - <<'EOF' + from madgraph.various import reweight_backend as rb + assert rb.madtrex_is_installed(), 'no madtrex/install/madtrex' + problem = rb.madtrex_build_problem() + assert not problem, problem + print('madtrex install OK') + EOF + + - name: Save the madtrex install + if: steps.cache-madtrex.outputs.cache-hit != 'true' + uses: actions/cache/save@v5 + with: + path: madtrex/install + key: ${{ env.MADTREX_KEY }}