diff --git a/scripts/profiling/bump.py b/scripts/profiling/bump.py index 41579dabd..d42d1f485 100644 --- a/scripts/profiling/bump.py +++ b/scripts/profiling/bump.py @@ -18,8 +18,7 @@ from model.registry import list_registered_platforms from profiling import version_basis from profiling.anchoring import REVIEW_STATUSES from profiling.rebase import pending_recale -from profiling.reports import ProfileReport -from profiling.resolver import NO_SOURCE_REF +from profiling.reports import ProfileReport, SkipKind from profiling.yamledit import apply_edit, insert_after_line, replace_field_line, set_version_fields from resolve.cores import resolve_platform_cores @@ -73,7 +72,7 @@ def _needs_review(report: ProfileReport, accept_changed: bool) -> bool: """Whether the report leaves a ref to read before the pin may move.""" # A profile citing nothing has nothing to read again: anarch's pin sat # behind a CI-only commit through every bulk advance. - if report.skipped not in (None, NO_SOURCE_REF) or not report.head or report.pinned_tag: + if report.skip_kind not in (None, SkipKind.NO_SOURCE_REF) or not report.head or report.pinned_tag: return True blocking = REVIEW_STATUSES if not accept_changed else ( s for s in REVIEW_STATUSES if s != "CHANGED" @@ -164,7 +163,7 @@ def bump_commit( report.upstream_head and report.upstream_pin != report.upstream_head and not report.upstream_tag - and report.skipped != NO_SOURCE_REF + and report.skip_kind is not SkipKind.NO_SOURCE_REF ) if document.get("source_commit") == report.head and not upstream_moves: # Rewriting the pin to the value it already holds is not a bump, and diff --git a/scripts/profiling/reports.py b/scripts/profiling/reports.py index 6886e3c99..47f209994 100644 --- a/scripts/profiling/reports.py +++ b/scripts/profiling/reports.py @@ -3,6 +3,7 @@ from __future__ import annotations from dataclasses import asdict, dataclass, field +from enum import Enum from core import upstream from model.sourceref import ANNOTATION_RE, trim_prose @@ -58,6 +59,42 @@ class Attention: detail: str +class SkipKind(str, Enum): + """Why a profile was not compared, set where the skip is decided. + + The sentence in ``skipped`` is for a reader. Every verdict on a skip + reads this field: the JSON attention, --strict, the triage and the exit + code, so rewording a sentence moves no profile out of any of them. + """ + + NO_SOURCE_REF = "no_source_ref" + UNSUPPORTED_HOST = "unsupported_host" + # The profile declares the forge gone and the forge agrees. + GONE_DECLARED = "gone_declared" + # The forge says it is gone and the profile does not. + GONE = "gone" + # The profile declares the forge gone and the forge answers. + GONE_CONTRADICTED = "gone_contradicted" + # No declared repository answers, or none has a pin to read. + UNANSWERED = "unanswered" + # A forge failed this time: the profile is unread, not settled. + UPSTREAM_ERROR = "upstream_error" + RATE_LIMITED = "rate_limited" + OFFLINE = "offline" + + +# Skips that say the profile cannot be checked as written: a repository +# that no longer answers, no pin to read, a death the forge contradicts. +# --strict passed them, and the CI job meant to stop an unchecked profile +# let a mistyped repository through. +UNCHECKABLE_SKIPS = frozenset({ + SkipKind.UNANSWERED, SkipKind.GONE_CONTRADICTED, SkipKind.GONE, +}) +# Skips that leave the run incomplete: a forge that failed or refused this +# time. The profile fails the run, and its JSON row says it was not read. +UNREACHED_SKIPS = frozenset({SkipKind.UPSTREAM_ERROR, SkipKind.RATE_LIMITED}) + + @dataclass class ProfileReport: name: str @@ -72,6 +109,7 @@ class ProfileReport: head: str | None = None entries: list[EntryReport] = field(default_factory=list) skipped: str | None = None + skip_kind: SkipKind | None = None counts: dict[str, int] = field(default_factory=dict) # Declared repositories whose forge the tool cannot read. They are named # rather than dropped: a `source` on an unknown host used to fall back to @@ -135,20 +173,18 @@ class ProfileReport: # profile was read from does not carry. Not part of the JSON form. cited_sources: list[CitedSource] = field(default_factory=list) - # Skips that say the profile cannot be checked as written: a repository - # that no longer answers, no pin to read, a death the forge contradicts. - # --strict passed them, and the CI job meant to stop an unchecked profile - # let a mistyped repository through. - UNCHECKABLE = ( - "no declared repository answers", - "no source_commit and no resolvable profiled_date", - "upstream_gone is declared but the forge answers", - "upstream gone:", - ) + def skip(self, kind: SkipKind, reason: str) -> None: + """Mark the profile skipped, for a verdict and for a reader.""" + self.skip_kind = kind + self.skipped = reason def uncheckable(self) -> bool: """Whether the skip is a defect of the profile rather than its nature.""" - return bool(self.skipped and self.skipped.startswith(self.UNCHECKABLE)) + return self.skip_kind in UNCHECKABLE_SKIPS + + def unreached(self) -> bool: + """Whether a forge failing this time left the profile unread.""" + return self.skip_kind in UNREACHED_SKIPS def needs_review(self) -> int: counts = self.counts or {} @@ -200,6 +236,8 @@ class ProfileReport: reasons = [] if self.uncheckable(): reasons.append(Attention("uncheckable", UNKNOWN_LEVEL, self.skipped or "")) + if self.unreached(): + reasons.append(Attention("unreached", UNKNOWN_LEVEL, self.skipped or "")) if self.page_version: # Off any forge, the release page is the feed: a version there # beyond the declared one is a release the profile does not cover. @@ -409,6 +447,8 @@ def report_to_dict(report: ProfileReport) -> dict: """Serialisable form of one report.""" payload = asdict(report) del payload["cited_sources"] + # The verdicts it decides are in "attention". + del payload["skip_kind"] payload["needs_review"] = report.needs_review() payload["attention"] = [asdict(reason) for reason in report.attention()] return payload diff --git a/scripts/profiling/resolver.py b/scripts/profiling/resolver.py index 7001c5067..4f668e5a3 100644 --- a/scripts/profiling/resolver.py +++ b/scripts/profiling/resolver.py @@ -39,7 +39,7 @@ from profiling.citations import ( strip_repo_word, symlink_target, ) -from profiling.reports import CitedSource, EntryReport, ProfileReport +from profiling.reports import CitedSource, EntryReport, ProfileReport, SkipKind from model.profilerepos import declared_repositories from profiling.repos import ( PIN, @@ -237,7 +237,9 @@ def _readable_views( ] if select_repo(profile) is None: declared = str(profile.get("source") or profile.get("upstream") or "") - report.skipped = f"unsupported host: {declared or 'none declared'}" + report.skip( + SkipKind.UNSUPPORTED_HOST, f"unsupported host: {declared or 'none declared'}" + ) return None stated_gone = str(profile.get("upstream_gone") or "").strip() @@ -253,21 +255,22 @@ def _readable_views( # The profile says the forge is gone and the forge agrees. That is # a recorded fact, not an open problem: the refs describe the last # revision anyone can reach, and nothing further is possible. - report.skipped = f"upstream gone (declared): {stated_gone}" + report.skip(SkipKind.GONE_DECLARED, f"upstream gone (declared): {stated_gone}") return None raise if stated_gone and any(view.field != "source_mirror" for view in views): # Declared dead, yet it answered. A profile that keeps saying so stops # being read, so the contradiction is the finding. - report.skipped = ( - f"upstream_gone is declared but the forge answers: {stated_gone}" + report.skip( + SkipKind.GONE_CONTRADICTED, + f"upstream_gone is declared but the forge answers: {stated_gone}", ) return None if not views and stated_gone: - report.skipped = f"upstream gone (declared): {stated_gone}" + report.skip(SkipKind.GONE_DECLARED, f"upstream gone (declared): {stated_gone}") return None if not views: - report.skipped = _unanswered_skip(profile) + report.skip(SkipKind.UNANSWERED, _unanswered_skip(profile)) return None return views @@ -918,7 +921,7 @@ def build_report( # A profile carrying no source_ref still has a pin worth writing and a # version worth checking, so the revisions above are resolved first. if not refs: - report.skipped = NO_SOURCE_REF + report.skip(SkipKind.NO_SOURCE_REF, NO_SOURCE_REF) return report resolver = _Resolver( diff --git a/scripts/profiling/run.py b/scripts/profiling/run.py index 5740a0d6c..1f15fdc0b 100644 --- a/scripts/profiling/run.py +++ b/scripts/profiling/run.py @@ -18,6 +18,7 @@ from profiling.realign import realign_prose from profiling.rebase import attribute_refs, rebase_refs from profiling.reports import ( ProfileReport, + SkipKind, fetch_plan, format_markdown, format_report, @@ -368,13 +369,11 @@ def read_one_profile( # nothing about the 600 profiles on GitHub. upstream stops asking # that host, so the rest of the pass costs it nothing. print(f"{name}: {exc}", file=sys.stderr) - reports.append(ProfileReport( - name=name, entries=[], counts={}, - skipped=f"rate limited: {exc.host or exc}", - )) - failed.add(name) - return + report = _skipped(name, SkipKind.RATE_LIMITED, f"rate limited: {exc.host or exc}") + writes = False reports.append(report) + if report.unreached(): + failed.add(name) if writes: try: with record_write(profile_path, name, written): @@ -391,6 +390,13 @@ def read_one_profile( failed.add(name) +def _skipped(name: str, kind: SkipKind, reason: str) -> ProfileReport: + """The report of a profile no forge comparison could be made for.""" + report = ProfileReport(name=name, entries=[], counts={}) + report.skip(kind, reason) + return report + + def read_selected( args: argparse.Namespace, selected: dict, writes: bool ) -> tuple[list[ProfileReport], set[str], set[str], bool]: @@ -446,24 +452,17 @@ def _build_full_report( # Offline with a cache that never stored this file: the profile # is unverified, not wrong. Reporting GONE sent readers after # files that exist. - report = ProfileReport( - name=name, entries=[], counts={}, skipped=f"offline: {exc}" - ) + report = _skipped(name, SkipKind.OFFLINE, f"offline: {exc}") except upstream.GoneError as exc: # The forge is not coming back: a takedown, or a host that no # longer resolves. Saying so once in the summary beats repeating # it on stderr every pass, and it keeps the profile out of the # backlog, where nobody can act on it anyway. - report = ProfileReport( - name=name, entries=[], counts={}, skipped=f"upstream gone: {exc}" - ) + report = _skipped(name, SkipKind.GONE, f"upstream gone: {exc}") except upstream.UpstreamError as exc: # One unreachable forge must not abandon the other profiles. print(f"{name}: {exc}", file=sys.stderr) - report = ProfileReport( - name=name, entries=[], counts={}, skipped=f"upstream error: {exc}" - ) - failed.add(name) + report = _skipped(name, SkipKind.UPSTREAM_ERROR, f"upstream error: {exc}") if not report.skipped and profile_path.is_file(): report.stale_prose = _read_stale_prose(name, profile_path, args, failed) if args.check_version and profile.get("version_source"): diff --git a/tests/test_check_freshness.py b/tests/test_check_freshness.py index d67baa331..e22b1418a 100644 --- a/tests/test_check_freshness.py +++ b/tests/test_check_freshness.py @@ -69,7 +69,7 @@ from model.registry import load_platform_registry from model.romfolders import load_sources, sources_dir from model.safeparse import yaml_load from model.targets import platform_overrides -from profiling.reports import ProfileReport, report_to_dict +from profiling.reports import ProfileReport, SkipKind, report_to_dict from scripts.scraper import game_dat_importer from tests.sources import SCRIPTS @@ -658,8 +658,14 @@ class ProfileSummaryTests(unittest.TestCase): ProfileReport("a", pin="1" * 40, head="1" * 40), ProfileReport("b", pin="1" * 40, head="2" * 40), ProfileReport("c", pin="1" * 40, head="2" * 40, counts={"CHANGED": 3}), - ProfileReport("d", skipped="no declared repository answers"), - ProfileReport("e", skipped="upstream gone (declared): archived 2019"), + ProfileReport("d", skipped="no declared repository answers", + skip_kind=SkipKind.UNANSWERED), + ProfileReport("e", skipped="upstream gone (declared): archived 2019", + skip_kind=SkipKind.GONE_DECLARED), + ProfileReport("f", skipped="upstream error: github.com/x/y answered 500", + skip_kind=SkipKind.UPSTREAM_ERROR), + ProfileReport("g", skipped="rate limited: github.com", + skip_kind=SkipKind.RATE_LIMITED), ) findings = summarize_profile_sync(report) by_subject = {f.subject: f for f in findings} @@ -667,6 +673,10 @@ class ProfileSummaryTests(unittest.TestCase): self.assertEqual(by_subject["profile_sync"].local, "1 at tip, 1 anchored past the tip") self.assertEqual(by_subject["c"].status, STALE) self.assertEqual(by_subject["d"].status, UNKNOWN) + # A forge that failed this time fails profile_sync's exit code: the + # row says the profile was not read, it is not skipped by nature. + self.assertEqual(by_subject["f"].status, UNKNOWN) + self.assertEqual(by_subject["g"].status, UNKNOWN) # Never checkable by nature: it does not hold the exit code. self.assertEqual(by_subject["e"].status, SKIPPED) self.assertNotIn("a", by_subject) diff --git a/tests/test_profile_sync.py b/tests/test_profile_sync.py index 5e3091595..103548fbb 100644 --- a/tests/test_profile_sync.py +++ b/tests/test_profile_sync.py @@ -41,6 +41,7 @@ from profiling.reports import ( CitedSource, EntryReport, ProfileReport, + SkipKind, fetch_plan, format_markdown, format_report, @@ -348,15 +349,18 @@ class TestGoneUpstreamIsNotAnError(unittest.TestCase): report = ProfileReport( name="yuzu", entries=[], counts={}, skipped="upstream gone: https://host/x: HTTP 451, withdrawn for legal reasons", + skip_kind=SkipKind.GONE, ) self.assertEqual(report.needs_review(), 0) self.assertTrue(report.skipped.startswith("upstream gone:")) def test_gone_and_failing_forges_land_in_different_buckets(self): gone = ProfileReport(name="a", entries=[], counts={}, - skipped="upstream gone: h: HTTP 451, withdrawn for legal reasons") + skipped="upstream gone: h: HTTP 451, withdrawn for legal reasons", + skip_kind=SkipKind.GONE) failing = ProfileReport(name="b", entries=[], counts={}, - skipped="upstream error: h: HTTP 403") + skipped="upstream error: h: HTTP 403", + skip_kind=SkipKind.UPSTREAM_ERROR) buffer = io.StringIO() with contextlib.redirect_stdout(buffer): profiling_run._print_elided([gone, failing], 0) @@ -2311,7 +2315,8 @@ class TestReportToDict(unittest.TestCase): from profiling.reports import STRICT_KINDS, ProfileReport self.assertTrue(_sample_report().fails(STRICT_KINDS)) - dead = ProfileReport("x", skipped="no declared repository answers") + dead = ProfileReport("x", skipped="no declared repository answers", + skip_kind=SkipKind.UNANSWERED) self.assertTrue(dead.fails(STRICT_KINDS)) warned = ProfileReport("y", pin="a", head="a", version_warning="behind") self.assertFalse(warned.fails(STRICT_KINDS)) @@ -2821,7 +2826,8 @@ class TestWriteDryRun(unittest.TestCase): from unittest import mock before = self.path.read_bytes() - skipped = ProfileReport(name="p", entries=[], counts={}, skipped="offline: not cached") + skipped = ProfileReport(name="p", entries=[], counts={}, skipped="offline: not cached", + skip_kind=SkipKind.OFFLINE) cold = upstream.NotCachedError("o/n@pin:a.c is not in the offline cache") reports: list[ProfileReport] = [] failed: set[str] = set() @@ -3438,7 +3444,7 @@ class TestBumpCommit(unittest.TestCase): # report skips a profile with no source_ref, and the skip blocked it. report = ProfileReport( name="p", repo="o/n", pin="pin", head="newhead", entries=[], counts={}, - skipped=resolver.NO_SOURCE_REF, + skipped=resolver.NO_SOURCE_REF, skip_kind=SkipKind.NO_SOURCE_REF, ) report.upstream_head, report.upstream_pin = "uphead", "uppin" self.assertTrue(bump_commit(self.path, report).moved) @@ -3461,7 +3467,7 @@ class TestBumpCommit(unittest.TestCase): ) report = ProfileReport( name="advanced_drastic", repo="o/n", pin="pin", head="newhead", entries=[], - counts={}, skipped=resolver.NO_SOURCE_REF, + counts={}, skipped=resolver.NO_SOURCE_REF, skip_kind=SkipKind.NO_SOURCE_REF, ) platforms = str(root / "platforms") self.assertEqual( @@ -3481,7 +3487,7 @@ class TestBumpCommit(unittest.TestCase): def test_any_other_skip_still_holds_the_pin(self): report = ProfileReport( name="p", repo="o/n", pin="pin", head="newhead", entries=[], counts={}, - skipped="rate limited: gitlab.com", + skipped="rate limited: gitlab.com", skip_kind=SkipKind.RATE_LIMITED, ) self.assertFalse(bump_commit(self.path, report).moved) @@ -6179,18 +6185,32 @@ class StrictRefusesAnUncheckableProfile(unittest.TestCase): was skipped with empty counts and --strict passed it in CI.""" def test_defects_are_told_from_the_profiles_nature(self): - from profiling.reports import ProfileReport + def report(kind): + return ProfileReport(name="p", entries=[], counts={}, skipped="any words", + skip_kind=kind) - def report(reason): - return ProfileReport(name="p", entries=[], counts={}, skipped=reason) + self.assertTrue(report(SkipKind.UNANSWERED).uncheckable()) + self.assertTrue(report(SkipKind.GONE).uncheckable()) + self.assertTrue(report(SkipKind.GONE_CONTRADICTED).uncheckable()) + self.assertFalse(report(SkipKind.UNSUPPORTED_HOST).uncheckable()) + self.assertFalse(report(SkipKind.GONE_DECLARED).uncheckable()) + self.assertFalse(report(SkipKind.NO_SOURCE_REF).uncheckable()) + self.assertFalse(report(SkipKind.OFFLINE).uncheckable()) - self.assertTrue(report("no declared repository answers (moved)").uncheckable()) - self.assertTrue(report( - "no source_commit and no resolvable profiled_date on any declared repository" - ).uncheckable()) - self.assertFalse(report("unsupported host: https://example.org").uncheckable()) - self.assertFalse(report("upstream gone (declared): taken down").uncheckable()) - self.assertFalse(report("no source_ref").uncheckable()) + def test_a_forge_failing_this_time_is_unread_in_the_json_too(self): + """profile_sync exited 1 on a forge answering 500 or 429 while its + JSON row carried no attention, so check_freshness --profiles showed + SKIPPED and exited 0.""" + for kind in (SkipKind.UPSTREAM_ERROR, SkipKind.RATE_LIMITED): + report = ProfileReport(name="p", skipped="x", skip_kind=kind) + self.assertTrue(report.unreached()) + self.assertEqual( + [(r.kind, r.level) for r in report.attention()], + [("unreached", profiling_reports.UNKNOWN_LEVEL)], + ) + self.assertNotIn("skip_kind", report_to_dict(report)) + self.assertFalse(ProfileReport(name="p", skipped="x", + skip_kind=SkipKind.OFFLINE).unreached()) class AnOffForgeProfileWatchesItsReleasePage(unittest.TestCase): @@ -6558,6 +6578,7 @@ class TheTriageCountsWhatBlocksThePin(unittest.TestCase): while the triage put them at 0 under the clean profiles.""" gone = ProfileReport( name="gone", pin="p", skipped="no declared repository answers (o/n)", + skip_kind=SkipKind.UNANSWERED, ) reports = [self._report("clean", 1), gone] selected = {r.name: {"source": "https://github.com/o/n"} for r in reports} diff --git a/tests/test_version_basis.py b/tests/test_version_basis.py index 9be9f76da..15ddc4d97 100644 --- a/tests/test_version_basis.py +++ b/tests/test_version_basis.py @@ -30,7 +30,7 @@ from core import upstream from profiling import version_basis from model.profilerepos import pin_field from model.profiles import load_emulator_profiles -from profiling.reports import ProfileReport +from profiling.reports import ProfileReport, SkipKind from profiling.bump import bump_commit ROOT = Path(__file__).resolve().parent.parent @@ -381,7 +381,7 @@ class AnAdvanceReadsTheVersionAgain(unittest.TestCase): self.path.write_text(SAMPLE, encoding="utf-8") self.report = ProfileReport( name="p", repo="o/n", pin="aaaaaaa", head="bbbbbbb", entries=[], counts={}, - skipped=resolver.NO_SOURCE_REF, + skipped=resolver.NO_SOURCE_REF, skip_kind=SkipKind.NO_SOURCE_REF, ) def tearDown(self):