From 5df004faff7c0bd43492bb07fa3eac622ed794dc Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 20:58:32 -0800 Subject: [PATCH] CI: the cold cache clear fails when the warm leg's save does not finish scripts/ci-clear-rolling-cache.py deletes the old rolling entries when the cache warm leg's Post Setup step starts. It used to exit 0 at that point, so a save that was canceled or failed afterwards left no rolling entry while the job passed. The script now keeps polling the leg until Post Setup completes or the leg ends, within the same 160-minute limit, and exits 1 unless the save succeeded. A failure in a later post step does not count once the save has succeeded. The header and the ci.yml comment describe this, and say that the next run then starts cold. Three new tests run main() with recorded API answers: a save that succeeds after the deletion exits 0, and a save that is canceled or fails after it exits 1. --- .github/workflows/ci.yml | 4 +- scripts/ci-clear-rolling-cache-tests.py | 54 +++++++++++++++++++++++++ scripts/ci-clear-rolling-cache.py | 39 +++++++++++++----- 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cdd51349fd..b61d2429d0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -88,8 +88,8 @@ jobs: # while the cold run saves its own: two entries of about 4.6 GB, which with the others go over the # 10 GB cache limit and get the corpus, SonarCloud and Windows ARM64 entries evicted. This deletes # the old entries when the `cache warm` leg starts saving (scripts/ci-clear-rolling-cache.py). - # When the leg fails or is canceled it saves nothing, and this job fails too, so "Re-run failed - # jobs" runs it again beside the re-run leg. The script's 160-minute wait starts when the leg + # When the leg fails or is canceled, before its save or during it, this job fails too, so "Re-run + # failed jobs" runs it again beside the re-run leg. The script's 160-minute wait starts when the leg # starts, which is after `gate / select legs` and a free runner, so this job's timeout leaves # 40 minutes for that. cold-cache-clear: diff --git a/scripts/ci-clear-rolling-cache-tests.py b/scripts/ci-clear-rolling-cache-tests.py index 815ae88a0f..734073aba3 100644 --- a/scripts/ci-clear-rolling-cache-tests.py +++ b/scripts/ci-clear-rolling-cache-tests.py @@ -3,10 +3,15 @@ python3 scripts/ci-clear-rolling-cache-tests.py """ +import contextlib from datetime import datetime, timedelta, timezone import importlib.util +import io +import os from pathlib import Path +import subprocess import unittest +from unittest import mock _spec = importlib.util.spec_from_file_location("clear", Path(__file__).with_name("ci-clear-rolling-cache.py")) clear = importlib.util.module_from_spec(_spec) @@ -138,5 +143,54 @@ class Caches(unittest.TestCase): self.assertEqual(clear.created_before([old, same_minute_push], "2026-11-28T03:37:10Z"), [old]) +class Clock(datetime): + @classmethod + def now(cls, tz=None): + return datetime(2026, 11, 28, 4, 0, tzinfo=timezone.utc) + + +class AfterDeletion(unittest.TestCase): + SAVING = [("Gate", "completed", "success"), ("Post Setup", "in_progress", None)] + + def run_main(self, after): + """Runs main() with the warm leg's save starting at the first poll and `after` at every later poll.""" + polls = [warm_job("in_progress", steps=self.SAVING), after] + deleted = [] + + def gh(url): + if "/jobs?" in url: + return {"jobs": ME + [polls.pop(0) if len(polls) > 1 else polls[0]]} + if "/caches?" in url: + old = {"id": 7, "key": "v2-rust-ab-gate-crates-Linux-x64-1", "created_at": "2026-11-27T03:40:00Z", "size_in_bytes": 4.6e9} + return {"actions_caches": [old]} + return {"run_started_at": "2026-11-28T03:37:10Z"} + + def run(args, **kwargs): + deleted.append(args[-1]) + return subprocess.CompletedProcess(args, 0, "", "") + + env = {"GITHUB_REPOSITORY": "storytold/vectorcraft", "GITHUB_RUN_ID": "1", "GITHUB_RUN_ATTEMPT": "1"} + with mock.patch.object(clear, "gh", gh), mock.patch.object(clear.subprocess, "run", run), mock.patch.object(clear.time, "sleep"), \ + mock.patch.object(clear, "datetime", Clock), mock.patch.dict(os.environ, env), contextlib.redirect_stdout(io.StringIO()): + status = clear.main() + self.assertEqual(deleted, ["repos/storytold/vectorcraft/actions/caches/7"]) + return status + + def test_a_save_that_succeeds_after_the_deletion_exits_0(self): + saved = warm_job("completed", "success", [("Gate", "completed", "success"), ("Post Setup", "completed", "success")]) + later_post_failure = warm_job("completed", "failure", [("Gate", "completed", "success"), ("Post Setup", "completed", "success"), + ("Post Harden runner", "completed", "failure")]) + self.assertFalse(self.run_main(saved)) + self.assertFalse(self.run_main(later_post_failure)) + + def test_a_save_canceled_after_the_deletion_exits_1(self): + canceled = warm_job("completed", "cancelled", [("Gate", "completed", "success"), ("Post Setup", "completed", "cancelled")]) + self.assertEqual(self.run_main(canceled), 1) + + def test_a_save_that_fails_after_the_deletion_exits_1(self): + failed = warm_job("completed", "failure", [("Gate", "completed", "success"), ("Post Setup", "completed", "failure")]) + self.assertEqual(self.run_main(failed), 1) + + if __name__ == "__main__": unittest.main(verbosity=2) diff --git a/scripts/ci-clear-rolling-cache.py b/scripts/ci-clear-rolling-cache.py index f8105a4973..e9b52d2493 100644 --- a/scripts/ci-clear-rolling-cache.py +++ b/scripts/ci-clear-rolling-cache.py @@ -7,14 +7,17 @@ The cold run (ci.yml, `cold-build`) never restores the rolling entry, so the old the cache while the new one is saved: two entries of about 4.6 GB each, beside the others, go over the repository's 10 GB limit and GitHub evicts the corpus, SonarCloud and Windows ARM64 entries. The gate's `cache warm` leg saves in its `Post Setup` step; this deletes the old entries when that step -starts. Pull requests and pushes that start in the minutes between restore nothing from the -deleted entry and build cold once. Deleting is the only write; the entry the cold run saves does not -exist yet when this runs. Only entries created before this run started are deleted: a push or pull -request run in the same minutes may have saved an entry of its own, and the cold run's own save is -never touched. A leg that failed or was canceled saves nothing: its old entries stay, and this -exits 1, so "Re-run failed jobs" runs it again beside the re-run leg. The same holds for a leg that -ends, canceled or failed, before its save step finished. It also exits 1 when the leg has not -reached its save GIVE_UP_SECONDS after the leg started. An absent warm leg is treated as finished only +starts, then waits for the step to finish. Pull requests and pushes that start in the minutes +between restore nothing from the deleted entry and build cold once. Deleting is the only write; the +entry the cold run saves does not exist yet when this runs. Only entries created before this run +started are deleted: a push or pull request run in the same minutes may have saved an entry of its +own, and the cold run's own save is never touched. A leg that failed or was canceled before its +save step started saves nothing: its old entries stay, and this exits 1, so "Re-run failed jobs" +runs it again beside the re-run leg. A save step that is canceled or fails after the old entries +were deleted leaves no rolling entry, so the next run starts cold, and this exits 1 too. A failure +in a later post step does not count once the save step has succeeded. This also exits 1 when the +leg has not reached its save, or its save has not finished, GIVE_UP_SECONDS after the leg started. +An absent warm leg is treated as finished only when the most recent earlier attempt that lists it reports success and the current attempt lists no `cache prune` job, which runs from the start of "Re-run all jobs". Otherwise this keeps polling because reusable-workflow dependencies may still be scheduling it. @@ -47,6 +50,7 @@ STOPPED = ('failure', 'cancelled', 'timed_out') # Steps with continue-on-error in craft-common-actions .github/workflows/gate.yml. TOLERATED_FAILURES = ('Trim the cache entry to this build',) NO_SAVE = 'the cache warm leg failed or was canceled and saves no entry; nothing deleted. Re-running the failed jobs runs this job again with the leg' +SAVE_LOST = 'the cache warm leg did not finish its save after the old rolling entries were deleted; the next run starts cold' def save_step(job): @@ -75,6 +79,12 @@ def saves_nothing(job): return job.get('status') == 'completed' +def save_finished(job): + """True once the save step has completed or the job has ended.""" + step = save_step(job) + return job.get('status') == 'completed' or (step is not None and step['status'] == 'completed') + + def gave_up(job, now, began): """True once GIVE_UP_SECONDS have passed since the warm leg started, or since `began` (this script's start) while the leg has not started.""" @@ -135,9 +145,10 @@ def gh(*args): def main(): repo, run, attempt = os.environ['GITHUB_REPOSITORY'], os.environ['GITHUB_RUN_ID'], os.environ.get('GITHUB_RUN_ATTEMPT', '1') began = datetime.now(timezone.utc) + jobs_url = f'repos/{repo}/actions/runs/{run}/attempts/{attempt}/jobs?per_page=100' earlier_jobs = {} while True: - jobs = gh(f'repos/{repo}/actions/runs/{run}/attempts/{attempt}/jobs?per_page=100') or {} + jobs = gh(jobs_url) or {} job = warm_job(jobs.get('jobs', [])) if job and saving(job): break @@ -178,6 +189,16 @@ def main(): else: print(result.stderr, end='', file=sys.stderr) return result.returncode + while not save_finished(job): + if gave_up(job, datetime.now(timezone.utc), began): + print(f'::error::{SAVE_LOST}') + return 1 + time.sleep(POLL_SECONDS) + job = warm_job((gh(jobs_url) or {}).get('jobs', [])) or job + if saves_nothing(job): + print(f'::error::{SAVE_LOST}') + return 1 + print('the cache warm leg saved its entry') if __name__ == '__main__':