From 4ca8490fcfb0e1748978ef50a396529c98b631a1 Mon Sep 17 00:00:00 2001 From: Ben Hearsum Date: Fri, 27 Nov 2026 09:23:02 -0500 Subject: [PATCH] fix: make All optimization strategy work for replacement again Replacement requires the result of the first optimization strategy to work correctly. https://phabricator.services.mozilla.com/D83715 & https://phabricator.services.mozilla.com/D83849 changed this composite strategy to return the first result when it returns a non-truthy value (to avoid unnecessarily calling additional strategies), and return a plain `True` when all strategies return a truthy-value. This works fine for removal (it only requires True/False), but replacement requires the result from the underlying strategy to work correctly. This change restores that behaviour, while preserving the early bail out if an optimization strategy rejects the task. This is _technically_ a breaking change despite being a bug fix. I'm indifferent on whether or not it should require a major version bump. It doesn't appear to actually change anything for Gecko graphs. --- src/taskgraph/optimize/base.py | 14 +++++++++++++- test/test_optimize.py | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/taskgraph/optimize/base.py b/src/taskgraph/optimize/base.py index c425bb23e..9c578848b 100644 --- a/src/taskgraph/optimize/base.py +++ b/src/taskgraph/optimize/base.py @@ -571,10 +571,22 @@ def description(self): @classmethod def reduce(cls, results): + # Use an iterate to collect results so we can bail as soon as one + # result gives us a decision. + results = iter(results) + # We need to keep track of the first result specifically, because in + # the case of replacement, we need to return that value specifically. + first = next(results, True) + # First result says no? Bail. + if not first: + return first + # Check the remaining results for rv in results: + # If a result says no, bail. if not rv: return rv - return True + # If no result says no, return the first result. + return first class Alias(CompositeStrategy): diff --git a/test/test_optimize.py b/test/test_optimize.py index bfc2e9709..dbd6b069b 100644 --- a/test/test_optimize.py +++ b/test/test_optimize.py @@ -487,3 +487,29 @@ def test_register_strategy(mocker): func = register_strategy("foo", args=("one", "two"), kwargs={"n": 1}) func(m) m.assert_called_with("one", "two", n=1) + + +def test_all_replaces_with_first_substrategy_value(): + graph = make_graph(make_task("t1", optimization={"all": "e1"})) + strategies = default_strategies() + strategies["all"] = All( + Replace(), Replace(), split_args=lambda arg, substrategies: [arg, "e2"] + ) + label_to_taskid = {} + removed_tasks = set() + + replaced = optimize_mod.replace_tasks( + target_task_graph=graph, + params={}, + optimizations=optimize_mod._get_optimizations(graph, strategies), + do_not_optimize=set(), + label_to_taskid=label_to_taskid, + removed_tasks=removed_tasks, + existing_tasks={}, + index_to_taskid={}, + taskid_to_status={}, + ) + + assert removed_tasks == set() + assert replaced == {"t1"} + assert label_to_taskid == {"t1": "e1"}