ci: measure what the CI cache bucket actually stores

Merged
TE
2mo ago
+449-0

Nothing in this repo deletes from the CI cache bucket. scripts/ci/r2-cache.sh has restore and save and no delete path, ci-stale-run-janitor.yml cancels stale runs but never touches R2 objects, and lifecycle rules exist only for the per-run canary bucket. Archive keys under v1/<os>-<arch>/objects/ embed a content hash, so every dependency bump mints a new object and keeps the old one indefinitely.

After this change you can measure that footprint before deciding anything about it:

python3 scripts/ci/r2_cache_census.py report --endpoint-url "$CI_CACHE_R2_ENDPOINT" --bucket "$CI_CACHE_R2_BUCKET"
python3 scripts/ci/r2_cache_census.py report ... --max-age-days 30

It walks v1/ with ListObjectsV2 and prints per-namespace object counts, bytes, and oldest entry. --max-age-days additionally models what an age-based rule would reclaim. It never writes to the bucket.

One mechanism detail worth knowing before anyone writes a retention rule: save skips re-upload when the key already exists, so an object's LastModified never refreshes. An age rule would therefore eventually evict still-hot caches. That is survivable, because r2-cache.sh treats every restore error as a miss and re-saves on 404, but it makes the window a cold-build tradeoff rather than free cleanup. The model counts archives only: expiring a latest/ pointer costs a restore miss and negligible bytes, so counting pointers would overstate the saving.

There is deliberately no apply mode. Retention is a cost decision and should be made against measured bytes.

Validation

python3 tests/test_ci_r2_cache_census.py passes: 11 tests covering pagination, a repeated continuation token, a truncated page with no token, archive/pointer accounting, the age model, unparseable timestamps, and read-only enforcement. Registered in ci-guards.yml under the ci group so guard ownership resolves. The full tests/test_ci_* sweep adds no failures; test_ci_change_areas.py, test_ci_sparkle_build_monotonic.sh, and test_ci_universal_release_settings.sh fail identically on clean main in a Linux sandbox, where they cannot reach gh or macOS.

Example output below is rendered from synthetic objects in a local harness, to show the format. It is not a measurement of the real bucket:

CI cache bucket census
  objects: 53 (52 archives, 1 pointer)
  size:    19.8 GiB
  an age>30d rule over archives would reclaim 16.6 GiB (84%) across 42 objects

  v1/linux-x64: 12 objects, 1.1 GiB, oldest 99d, reclaimable 720.0 MiB
  v1/macos-arm64: 41 objects, 18.8 GiB, oldest 234d, reclaimable 15.9 GiB

Remaining gap

The bucket has not been measured yet: that needs a run with the cache credentials, which this PR does not perform. A lifecycle rule configured by hand in the Cloudflare dashboard would not be visible to this tool or to the audit above, so the census run is also what would confirm whether cleanup already exists out of band.

🤖 Generated with Claude Code