From 5961484cb19125c87d91ace9df9b250471cd94f5 Mon Sep 17 00:00:00 2001
From: Tristan Partin <tristan@partin.io>
Date: Wed, 17 Jun 2026 19:38:08 +0000
Subject: [PATCH v1 5/7] Provide ccache target rate as an input to the script

This makes it much easier to test locally. Now, we don't depend on an
ON_DEFAULT_BRANCH environment variable within the script. We pull the
decision making up to the CI step itself to tell the script what rate to
target.

Signed-off-by: Tristan Partin <tristan@partin.io>
---
 .github/workflows/pg-ci.yml       |  8 +++++++-
 src/tools/ci/gha_ccache_decide.py | 29 +++++++++++++++--------------
 2 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/.github/workflows/pg-ci.yml b/.github/workflows/pg-ci.yml
index 5bc5292d2a..6ccd8c2475 100644
--- a/.github/workflows/pg-ci.yml
+++ b/.github/workflows/pg-ci.yml
@@ -368,7 +368,13 @@ jobs:
         if: |
           always() &&
           steps.ccache-restore-branch.conclusion == 'success'
-        run: python3 src/tools/ci/gha_ccache_decide.py
+        run: |
+          # Decide the target hit percentage below which we decide to upload a
+          # new cache. On non-default branches a few misses aren't that bad.
+          # But, as the caches of the default branch are shared with all
+          # branches, it's worth aiming for a higher ratio there.
+          python3 src/tools/ci/gha_ccache_decide.py \
+            --target-rate $([ "$ON_DEFAULT_BRANCH" = "true" ] && echo 95 || echo 80)
 
       - &ccache_save_step
         name: "ccache: Upload cache"
diff --git a/src/tools/ci/gha_ccache_decide.py b/src/tools/ci/gha_ccache_decide.py
index 58b1d14c96..fe9db62891 100644
--- a/src/tools/ci/gha_ccache_decide.py
+++ b/src/tools/ci/gha_ccache_decide.py
@@ -1,5 +1,6 @@
 #!/usr/bin/env python3
 
+import argparse
 import json
 import os
 import subprocess
@@ -27,15 +28,7 @@ def group(name):
     finally:
         print("::endgroup::")
 
-def main():
-    on_default_branch = os.environ["ON_DEFAULT_BRANCH"] == "true"
-
-    # Decide the target hit percentage below which we decide to upload a new
-    # cache. On non-default branches a few misses aren't that bad. But, as the
-    # caches of the default branch are shared with all branches, it's worth
-    # aiming for a higher ratio there.
-    target_rate = 95 if on_default_branch else 80
-
+def main(args):
     # Log ccache stats, useful for more in-depth understanding. To avoid
     # swamping the output, collapse it in a group.
     with group("ccache_stats"):
@@ -48,19 +41,19 @@ def main():
     total = hits + misses
     hit_pct = int((hits / total) * 100) if total > 0 else 100
 
-    print(f"hits: {hits}, misses: {misses}, hit_pct: {hit_pct}, target rate: {target_rate}")
+    print(f"hits: {hits}, misses: {misses}, hit_pct: {hit_pct}, target rate: {args.target_rate}")
 
     # If the cache hit ratio was high, or the absolute number of misses
     # (e.g. in case of a failed build) was low, there is no point in
     # generating a new cache entry. We have limited cache space.
-    if hit_pct >= target_rate:
-        print(f"hit rate {hit_pct} is above target of {target_rate}, skip creating new cache entry")
+    if hit_pct >= args.target_rate:
+        print(f"hit rate {hit_pct} is above target of {args.target_rate}, skip creating new cache entry")
         should_save = False
     elif misses <= 10:
         print(f"only {misses} misses, skip creating new cache entry")
         should_save = False
     else:
-        print(f"hit rate {hit_pct} is below target of {target_rate}, create new cache entry")
+        print(f"hit rate {hit_pct} is below target of {args.target_rate}, create new cache entry")
         should_save = True
 
     append_github_output("should_save", str(should_save).lower())
@@ -93,4 +86,12 @@ def main():
     return 0
 
 if __name__ == "__main__":
-    exit(main())
+    parser = argparse.ArgumentParser(description="Decide whether to save cache")
+    parser.add_argument(
+        "--target-rate",
+        type=int,
+        default=80,
+        help="target hit rate below which to save cache",
+    )
+
+    exit(main(parser.parse_args()))
-- 
Tristan Partin
https://tristan.partin.io

