From 08c007dad81d9a15cd21596660d84572cd5cfcb4 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 12 Nov 2026 10:07:57 +0000 Subject: [PATCH] fix(image): prune the host build cache to INFINITO_BUILD_CACHE_MAX_SIZE after every build MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Main never bounded the host Docker build cache: on 2026-11-11 it held 99 GB, 63 GB of it unused for more than 12 h (15 overnight core image builds). The cap already exists on the IAM line as part of 4014385f6a (092 phase 1); this ports only its build-cache slice to main. - scripts/image/prune.sh (byte-identical to 4014385f6a) runs docker builder prune --force with --max-used-space, or --keep-storage on older engines, at INFINITO_BUILD_CACHE_MAX_SIZE, and fails loudly when the engine knows neither flag. - scripts/image/build.sh calls it after every build. - default.env sets the cap to 20g; the env passthrough exports it. Every existing checkout needs one make dotenv after this change: until then make build stops at the final prune step with Run 'make dotenv'. The shared cap also evicts other checkouts' build layers, which costs rebuild time, not correctness. 🎯 Fix: 90 % 💥 Break: 8 % 💸 Cost: 100 % ⏱️ Time: 100 % 🛡️ Security: ~100 % 🏆 Score: 83 🚦 Gate: ⛔ break 8 % > 5 (operator approved) Co-Authored-By: Claude Opus 5.5 --- default.env | 3 + scripts/image/build.sh | 2 + scripts/image/prune.sh | 17 +++ .../scripts/image/test_build_cache_prune.py | 106 ++++++++++++++++++ utils/env/handlers/passthrough.py | 1 + 5 files changed, 129 insertions(+) create mode 100755 scripts/image/prune.sh create mode 100644 tests/unit/python/scripts/image/test_build_cache_prune.py diff --git a/default.env b/default.env index bcf3451b4e..e6b34c83b6 100644 --- a/default.env +++ b/default.env @@ -55,6 +55,9 @@ INFINITO_BUILD=1 # `1` keeps BuildKit on (default); `0` forces the compose wrapper to use the legacy builder. INFINITO_BUILDKIT_ENABLED=1 +# Cap of the host Docker build cache; every image build ends with a builder prune that keeps at most this much. +INFINITO_BUILD_CACHE_MAX_SIZE=20g + # Pacman cache an arch stack mounts read-only at the same path; from pacman-conf on a host whose os-release ID is arch, else empty. INFINITO_CACHE_HOST_PACMAN_PATH= diff --git a/scripts/image/build.sh b/scripts/image/build.sh index af443b2483..07baa18e82 100755 --- a/scripts/image/build.sh +++ b/scripts/image/build.sh @@ -324,3 +324,5 @@ else echo "[build] Running: ${local_args[*]}" "${local_args[@]}" fi + +bash "${_build_script_dir}/prune.sh" diff --git a/scripts/image/prune.sh b/scripts/image/prune.sh new file mode 100755 index 0000000000..fd80495a7c --- /dev/null +++ b/scripts/image/prune.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -euo pipefail + +: "${INFINITO_BUILD_CACHE_MAX_SIZE:?Run 'make dotenv' to generate the .env single source of truth}" + +prune_help="$(docker builder prune --help)" +if grep -q -- '--max-used-space' <<<"${prune_help}"; then + keep_flag="--max-used-space" +elif grep -q -- '--keep-storage' <<<"${prune_help}"; then + keep_flag="--keep-storage" +else + echo "[build] ERROR: 'docker builder prune' accepts neither --max-used-space nor --keep-storage; cannot cap the build cache at INFINITO_BUILD_CACHE_MAX_SIZE=${INFINITO_BUILD_CACHE_MAX_SIZE}" >&2 + exit 1 +fi + +echo "[build] Pruning the build cache to INFINITO_BUILD_CACHE_MAX_SIZE=${INFINITO_BUILD_CACHE_MAX_SIZE} (${keep_flag})" +docker builder prune --force "${keep_flag}" "${INFINITO_BUILD_CACHE_MAX_SIZE}" diff --git a/tests/unit/python/scripts/image/test_build_cache_prune.py b/tests/unit/python/scripts/image/test_build_cache_prune.py new file mode 100644 index 0000000000..f79efa3e60 --- /dev/null +++ b/tests/unit/python/scripts/image/test_build_cache_prune.py @@ -0,0 +1,106 @@ +from __future__ import annotations + +import os +import subprocess +import tempfile +import unittest +from pathlib import Path + +from utils import PROJECT_ROOT +from utils.cache.files import read_text + +SCRIPT = PROJECT_ROOT / "scripts" / "image" / "build.sh" + +DOCKER_STUB = """#!/usr/bin/env bash +printf '%s\\n' "$*" >>"${STUB_LOG}" +if [ "$1 $2" = "image inspect" ]; then +\texit 1 +fi +if [ "$1 $2 $3" = "builder prune --help" ]; then +\tprintf '%s\\n' "${STUB_PRUNE_HELP}" +fi +""" + +MAX_USED = " --max-used-space bytes Maximum amount of disk space allowed to keep for cache" +KEEP_STORAGE = " --keep-storage bytes Amount of disk space to keep for cache" +NEITHER = " -a, --all Remove all unused build cache, not just dangling ones" + + +def _build(prune_help: str, cap: str) -> tuple[int, list[str], str]: + """Args: + prune_help: what `docker builder prune --help` prints on the stubbed engine. + cap: the INFINITO_BUILD_CACHE_MAX_SIZE of the build. + + Returns: + The exit code, every docker call and the combined output. + """ + with tempfile.TemporaryDirectory() as tmp: + bin_dir = Path(tmp) / "bin" + bin_dir.mkdir() + stub = bin_dir / "docker" + stub.write_text(DOCKER_STUB) + stub.chmod(0o755) + log = Path(tmp) / "calls" + env = dict(os.environ) + env.update( + { + "PATH": f"{bin_dir}{os.pathsep}{env['PATH']}", + "HOME": tmp, + "STUB_LOG": str(log), + "STUB_PRUNE_HELP": prune_help, + "INFINITO_DISTRO": "debian", + "INFINITO_PARENT_IMAGE": "debian:stable", + "INFINITO_IMAGE_REPOSITORY": "probe", + "INFINITO_SRC_DIR": "/opt/src/infinito", + "INFINITO_VENV_DIR": "/opt/venvs/infinito", + "INFINITO_PACKAGE_INSTALL_SCRIPT": "package.sh", + "INFINITO_PYTHON_INSTALL_SCRIPT": "python.sh", + "INFINITO_DOCKER_CLI_INSTALL_SCRIPT": "docker-cli.sh", + "INFINITO_BUILD_CACHE_MAX_SIZE": cap, + } + ) + for key in ("GITHUB_REPOSITORY", "GITHUB_REPOSITORY_OWNER", "OWNER"): + env.pop(key, None) + done = subprocess.run( + ["bash", str(SCRIPT), "--tag", "probe/debian:dev-1"], + cwd=tmp, + env=env, + capture_output=True, + text=True, + check=False, + ) + calls = read_text(str(log)).splitlines() if log.exists() else [] + return done.returncode, calls, done.stdout + done.stderr + + +class TestBuildCachePrune(unittest.TestCase): + def test_a_build_ends_with_a_prune_down_to_the_cap(self) -> None: + code, calls, _ = _build(MAX_USED, "20g") + self.assertEqual(code, 0) + builds = [i for i, call in enumerate(calls) if call.startswith("build ")] + self.assertEqual(len(builds), 1) + self.assertEqual(calls[-1], "builder prune --force --max-used-space 20g") + self.assertGreater(len(calls) - 1, builds[0]) + + def test_an_older_engine_prunes_with_keep_storage(self) -> None: + code, calls, _ = _build(KEEP_STORAGE, "20g") + self.assertEqual(code, 0) + self.assertEqual(calls[-1], "builder prune --force --keep-storage 20g") + + def test_the_prune_follows_the_cap(self) -> None: + _, calls, _ = _build(MAX_USED, "7g") + self.assertEqual(calls[-1], "builder prune --force --max-used-space 7g") + + def test_an_engine_without_a_keep_flag_fails_the_build_loudly(self) -> None: + code, calls, output = _build(NEITHER, "20g") + self.assertNotEqual(code, 0) + self.assertFalse( + any(call.startswith("builder prune --force") for call in calls) + ) + self.assertIn("--max-used-space", output) + self.assertIn("--keep-storage", output) + self.assertIn("INFINITO_BUILD_CACHE_MAX_SIZE", output) + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/env/handlers/passthrough.py b/utils/env/handlers/passthrough.py index b0c4124c80..fbbae5d4c3 100644 --- a/utils/env/handlers/passthrough.py +++ b/utils/env/handlers/passthrough.py @@ -153,6 +153,7 @@ STATIC_KEYS = ( "INFINITO_CACHE_PACKAGE_NPMRC", "INFINITO_CACHE_PACKAGE_PIP_CONF", "INFINITO_CACHE_REGISTRY_PROXY_CONF", + "INFINITO_BUILD_CACHE_MAX_SIZE", "INFINITO_IMAGE_TAG", "INFINITO_VARS_PAYLOAD", "INFINITO_APP_VARIANTS",