diff --git a/cli/administration/deploy/ci/runs.py b/cli/administration/deploy/ci/runs.py index 88691be765..1d793a14bd 100644 --- a/cli/administration/deploy/ci/runs.py +++ b/cli/administration/deploy/ci/runs.py @@ -47,8 +47,8 @@ SELECTION_INPUTS = ("priority", "offset") """The two inputs a retrigger decides itself rather than carrying over. ``priority`` IS the retrigger: it names what failed. ``offset`` follows from -it -- the source run already has a verdict for everything up to where its -budget ran out, so the regular line resumes behind that window +it -- the source run got the head of the ranking green before its budget ran +out, so the regular line resumes behind that green window (:func:`resume_offset`) instead of repeating it. Everything else is carried verbatim, including ``whitelist``, so a retrigger of a scoped run stays inside that scope instead of quietly widening to the whole repository.""" diff --git a/cli/administration/deploy/ci/selections.py b/cli/administration/deploy/ci/selections.py index 90d6443d8c..aeef2dbe18 100644 --- a/cli/administration/deploy/ci/selections.py +++ b/cli/administration/deploy/ci/selections.py @@ -90,11 +90,12 @@ def collapse_to_roles(tokens: Iterable[str]) -> list[str]: return sorted({selection.parse(token).app for token in tokens}) -def deployed_selections(jobs: list[dict]) -> set[str]: - """Every selection the source run actually deployed, green or red. +def _selections(jobs: list[dict], states: set[str] | None = None) -> set[str]: + """The deploy jobs of *jobs* as tokens, narrowed to *states* when given. - The verdict is irrelevant here: what matters is that the run reached the - row at all, because that is what a retrigger no longer has to repeat. + Args: + jobs: the source run's jobs. + states: effective job states to keep; ``None`` keeps every deploy job. """ return { selection.describe( @@ -107,10 +108,20 @@ def deployed_selections(jobs: list[dict]) -> set[str]: ) ) for app, _mode, job in _iter_deploy_jobs(jobs) - if (label := axes.parse_label(str(job.get("name", "")))) is not None + if (states is None or _effective(job) in states) + and (label := axes.parse_label(str(job.get("name", "")))) is not None } +def deployed_selections(jobs: list[dict]) -> set[str]: + """Every selection the source run actually deployed, green or red. + + The verdict is irrelevant here: what matters is that the run reached the + row at all, because that is what a retrigger no longer has to repeat. + """ + return _selections(jobs) + + def settled_selections(jobs: list[dict]) -> set[str]: """Every selection the source run reached a verdict on. @@ -123,20 +134,18 @@ def settled_selections(jobs: list[dict]) -> set[str]: aborts and the still-running, is owed another attempt at the same combination. """ - return { - selection.describe( - selection.Pin( - app, - tuple(int(part) for part in label.variant.split(",") if part), - label.mode, - label.tor, - label.distro or None, - ) - ) - for app, _mode, job in _iter_deploy_jobs(jobs) - if _effective(job) in {"success", "failure"} - and (label := axes.parse_label(str(job.get("name", "")))) is not None - } + return _selections(jobs, {"success", "failure"}) + + +def passed_selections(jobs: list[dict]) -> set[str]: + """Every selection the source run deployed green. + + The narrowest of the three, and what :func:`resume_offset` counts: a row + the regular line may walk past has to be proven, not merely attempted. A + red or aborted row does come back on the priority line, but only for the + combination it was recorded in -- the regular line owes it every other one. + """ + return _selections(jobs, {"success"}) def row_selection(entry: Mapping[str, Any]) -> str: @@ -199,29 +208,30 @@ def unrun_selections(regular: list[dict[str, str]], deployed: set[str]) -> list[ ) -def resume_offset(regular: list[dict[str, str]], deployed: set[str]) -> str: +def resume_offset(regular: list[dict[str, str]], passed: set[str]) -> str: """Where a retrigger should pick the regular line up again. The source run deployed a window of the ranking and stopped at its budget. - Everything inside that window has a verdict -- the red rows come back on - the priority line anyway -- so the regular line has no reason to walk it a - second time. The answer is the last row of the leading run of deployed - rows, as a selection token: a token still names the same row after the - ranking shifts, a row count does not. + Only the green part of that window is settled, so only it may be skipped: + the answer is the last row of the leading run of *successful* rows, as a + selection token -- a token still names the same row after the ranking + shifts, a row count does not. - Stops at the first gap. A hole inside the window means that row was - filtered out, not that the run got further, and resuming past it would skip - whatever follows. + Stops at the first row that is not green. A red or aborted row does return + on the priority line, but pinned to the one combination it was recorded in; + resuming past it would drop every other combination of that row and + everything the run never reached behind it. Args: regular: the regular line of the retrigger's own discovery, in ranking order. - deployed: tokens the source run deployed (:func:`deployed_selections`). + passed: tokens the source run deployed green + (:func:`passed_selections`). Returns: the ``role#variant`` token to resume at, or ``''`` when the source run - deployed nothing of this line -- then the retrigger starts at the head, - as it would without an offset. + got nothing of this line green -- then the retrigger starts at the + head, as it would without an offset. The returned token names a place in the ranking, so it carries no axes. Membership is still tested on the full deploy token, because that is @@ -233,7 +243,7 @@ def resume_offset(regular: list[dict[str, str]], deployed: set[str]) -> str: """ resume = "" for entry in regular: - if row_selection(entry) not in deployed: + if row_selection(entry) not in passed: break variants = tuple( int(part) for part in str(entry.get("variant", "")).split(",") if part diff --git a/cli/administration/deploy/ci/trigger/__main__.py b/cli/administration/deploy/ci/trigger/__main__.py index 8ff0526407..a27451d4da 100644 --- a/cli/administration/deploy/ci/trigger/__main__.py +++ b/cli/administration/deploy/ci/trigger/__main__.py @@ -210,7 +210,9 @@ def main(argv: list[str] | None = None) -> int: print("Nothing to re-trigger from that run.") return 0 priority = " ".join(sorted(priority_entries)) - config["offset"] = selections.resume_offset(ranking, deployed) + config["offset"] = selections.resume_offset( + ranking, selections.passed_selections(source["jobs"]) + ) if config["offset"]: print(f"Regular line resumes at: {config['offset']}") else: diff --git a/tests/unit/python/cli/administration/deploy/ci/test_selections.py b/tests/unit/python/cli/administration/deploy/ci/test_selections.py index 24c24eabde..3cb7e41013 100644 --- a/tests/unit/python/cli/administration/deploy/ci/test_selections.py +++ b/tests/unit/python/cli/administration/deploy/ci/test_selections.py @@ -41,43 +41,48 @@ class TestResumeOffset(unittest.TestCase): self.assertEqual(selections.resume_offset(self._REGULAR, set()), "") def test_the_offset_is_the_last_row_of_the_leading_run(self) -> None: - deployed = { + passed = { "web-app-a#0@compose+clearnet" + _AXES, "web-app-b#1@swarm+tor" + _AXES, } + self.assertEqual(selections.resume_offset(self._REGULAR, passed), "web-app-b#1") + + def test_a_red_row_stops_the_scan_so_the_line_walks_it_again(self) -> None: + jobs = [ + _job(deploy_job_name("docker", "web-app-a", "0"), "success"), + _job(deploy_job_name("swarm", "web-app-b", "1", tor=True), "failure"), + _job(deploy_job_name("host", "web-app-c", "0"), "success"), + ] self.assertEqual( - selections.resume_offset(self._REGULAR, deployed), "web-app-b#1" + selections.resume_offset(self._REGULAR, selections.passed_selections(jobs)), + "web-app-a#0", ) def test_the_offset_carries_no_axes_so_it_survives_a_new_sweep(self) -> None: """Every axis rotates with the sweep number and a retrigger gets a fresh one, so an axis-pinned offset would resolve only in the sweep it was computed at and abort every chunk's discovery in the others.""" - deployed = {"web-app-a#0@compose+clearnet" + _AXES} - offset = selections.resume_offset(self._REGULAR, deployed) + passed = {"web-app-a#0@compose+clearnet" + _AXES} + offset = selections.resume_offset(self._REGULAR, passed) self.assertNotIn("@", offset) self.assertNotIn("+", offset) self.assertNotIn("%", offset) self.assertNotIn("/", offset) def test_a_gap_stops_the_scan_rather_than_skipping_past_it(self) -> None: - deployed = { + passed = { "web-app-a#0@compose+clearnet" + _AXES, "web-app-c#0@host+clearnet" + _AXES, } - self.assertEqual( - selections.resume_offset(self._REGULAR, deployed), "web-app-a#0" - ) + self.assertEqual(selections.resume_offset(self._REGULAR, passed), "web-app-a#0") def test_a_run_that_covered_everything_resumes_at_the_last_row(self) -> None: - deployed = { + passed = { "web-app-a#0@compose+clearnet" + _AXES, "web-app-b#1@swarm+tor" + _AXES, "web-app-c#0@host+clearnet" + _AXES, } - self.assertEqual( - selections.resume_offset(self._REGULAR, deployed), "web-app-c#0" - ) + self.assertEqual(selections.resume_offset(self._REGULAR, passed), "web-app-c#0") def test_the_verdict_does_not_matter_only_that_the_row_ran(self) -> None: jobs = [ @@ -161,6 +166,22 @@ class TestSettledSelections(unittest.TestCase): ) +class TestPassedSelections(unittest.TestCase): + """Only a green row lets the regular line walk past it.""" + + def test_only_the_successes_are_kept(self) -> None: + jobs = [ + _job(deploy_job_name("docker", "web-app-a", "0"), "success"), + _job(deploy_job_name("swarm", "web-app-b", "1", tor=True), "failure"), + _job(deploy_job_name("host", "web-app-c", "0"), "cancelled"), + ] + self.assertEqual( + selections.passed_selections(jobs), + {"web-app-a#0@compose+clearnet" + _AXES}, + ) + self.assertEqual(len(selections.settled_selections(jobs)), 2) + + class TestCollapseToRoles(unittest.TestCase): """Trading the exact red combination for covering the role."""