ci: find reusable app-host products by exact artifact name
Problem
select() in scripts/ci/reuse_app_host_products.py enumerated reuse candidates by scanning the 300 most recent repository artifacts (per_page=100, pages 1–3, unfiltered, stopping early after six prefix matches).
This repository churns artifacts fast — page 1 alone spans roughly half an hour, so the whole three-page scan covers only about three hours. The products themselves are uploaded with retention-days: 3 (.github/workflows/ci-macos.yml, "Upload compiled app-host test product"). A product that is still retained, still valid and an exact contract match was therefore unreachable purely because newer unrelated artifacts had pushed it off the scan. The reuse machinery was discarding most of the retention window it pays for.
Change
The artifact name is already deterministic: app-host-products-v1-<contract-key>-<run_attempt>. The list endpoint supports exact-name filtering (?name=<exact-name>), the same pattern scripts/ci/find_admitted_build.py already uses. Candidate enumeration now asks for each plausible attempt's name directly.
Before
prefix = PREFIX + key(value) + "-"
candidates = []
for page in range(1, 4):
batch = api.get(f"actions/artifacts?per_page=100&page={page}")["artifacts"]
if not isinstance(batch, list):
raise ValueError("invalid artifact listing")
candidates.extend(a for a in batch if ... a["name"].startswith(prefix))
if len(candidates) >= 6 or len(batch) < 100:
break
After
for attempt in range(1, LOOKUP_ATTEMPTS + 1): # 1, 2, 3
name = artifact_name(value, attempt) # PREFIX + key(value) + "-" + attempt
query = urlencode({"name": name, "per_page": ARTIFACTS_PER_PAGE})
try:
batch = api.get(f"actions/artifacts?{query}")["artifacts"]
except (...):
record_reason(reasons, "artifact_listing_unavailable")
continue
if not isinstance(batch, list):
record_reason(reasons, "artifact_listing_invalid")
continue
found.extend((attempt, a) for a in batch if isinstance(a, dict) and a.get("name") == name)
found.sort(key=lambda item: str(item[1].get("created_at") or ""), reverse=True)
return found[:MAX_CANDIDATES] # still at most six
A ~3-hour window becomes the full 3-day retention, using 3 requests instead of 3 pages of 100. Candidates are still capped at six, so the per-candidate API cost is unchanged. They are now ordered newest-first across attempts, so a rerun's own earlier attempt is considered before older runs of the same contract. The returned name is re-checked locally, so enumeration never depends on the server honoring the filter.
What did not change
scripts/ci/product_input_identity.pyis untouched. It hashes its own source intoalgorithm_fingerprint(), so editing it would invalidate every cached product repo-wide.contract()andkey()are untouched. Product identity is byte-identical and no cached product is invalidated;artifact_name()is just the existingPREFIX + key(value) + "-" + attemptconcatenation given a name.- Every producer-side validation still runs, in the same order, against every candidate: not expired,
size_in_bytessane and underMAX_ARCHIVE_BYTES, artifact bound to a workflow run, not the current run's own later attempt, exact producer attempt lookup,permitted_pair/trusted_ci_run, producer head SHA well-formed,github_product_identity(...) == value["product_inputs"], a completed successfulmacOS compile admissionjob, and thesha256:digest prefix. Only how candidates are enumerated changed, never how they are validated. record_reasonis preserved, so the step log still explains misses.
Two incidental consequences of exact-name lookup, both recorded misses rather than silent behavior changes:
artifact_attempt_invalidis gone — the attempt is now an input to the query rather than something parsed out of a scanned name, so it cannot be non-decimal.- A failed or malformed listing is now
artifact_listing_unavailable/artifact_listing_invalidand a normal compile, where the old code raisedValueError("invalid artifact listing")out ofselect()and relied on the job-levelexceptinmain()(which alsormtrees DerivedData and reportsfallback). This is the fail-open behavior the module docstring already promises.
Tests
tests/test_reuse_app_host_products.py: FakeGitHub now parses the query string and returns only exact name matches, and raises on an unfiltered listing so a regression back to page scanning fails loudly. New coverage:
test_exact_name_lookup_finds_artifact_outside_recent_listing_window— a hit found by exact name for an artifact far older than any recent-listing window, asserting the exact query paths.test_earlier_producer_attempt_is_reachable_by_name— an attempt-2 artifact is still found.test_artifact_of_another_contract_is_never_a_candidate— a different contract key is ano_matching_contract_artifactmiss with no download.test_named_candidate_still_requires_producer_validation— an exact name match is still a miss, with no download, for an untrusted producer, a failed compile job, changed product inputs, an oversize archive, an expired artifact and a missing digest.test_artifact_listing_errors_are_misses_not_failures—CalledProcessError,OSError, a JSONValueErrorand a malformed body each produce a recorded miss and a normal compile, never an exception.test_candidate_lookup_is_bounded— updated to assert three exact-name requests and the unchanged six-candidate cap.
All green locally, and all four also pass unchanged on main:
python3 tests/test_reuse_app_host_products.py— 49 tests OK (44 onmain)python3 tests/test_app_host_test_products.py— 7 tests OKpython3 tests/test_ci_persistent_mac_compile.py— 16 tests OKbash tests/test_ci_self_hosted_guard.sh— PASS
Related
Related to #13676, which changes the producer job-name comparison in the same file. That change is roughly thirty lines below this diff and is neither reverted nor duplicated here; the two should merge cleanly in either order.
🤖 Generated with Claude Code
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
Summary by cubic
Finds reusable app-host products by exact artifact name instead of scanning recent repository artifacts, so valid retained products aren't missed when newer unrelated artifacts push them out of the recent-listing window.
- Candidate enumeration now issues one exact-name request per plausible producer attempt (up to 3) rather than paging through 300 recent artifacts; validation logic is unchanged.
- A failed or malformed artifact listing is now a recorded miss rather than a raise, so an API outage compiles normally.
- Tests updated to assert exact-name queries and cover out-of-window, multi-attempt, and error cases.