From 1bb91a18af43a0736ff037b955f1946fb345b661 Mon Sep 17 00:00:00 2001 From: Vasily Ilin Date: Fri, 27 Nov 2026 19:50:40 +0000 Subject: [PATCH] Hold the cold shard plan to the bound greedy assignment guarantees The balance test compared the heaviest shard with 1.25 times the mean shard. No assignment satisfies that once one project costs more, and FiniteIntegralRelationAlgebras (#1035) is predicted at 31,100 CPU seconds against a mean shard of 24,091. Content PRs do not run pytest, so the required check failed on main afterwards for every PR that does. Assert instead that no shard exceeds the mean by more than its cheapest project, which greedy assignment guarantees for any pool. Co-Authored-By: Claude Fable 5.2 --- python/tests/test_build_shard_planner.py | 44 +++++++++++++++++++++--- scripts/ci/plan-build-shards.py | 20 +++++++---- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/python/tests/test_build_shard_planner.py b/python/tests/test_build_shard_planner.py index d770d166ec..682b208881 100644 --- a/python/tests/test_build_shard_planner.py +++ b/python/tests/test_build_shard_planner.py @@ -3,6 +3,7 @@ from __future__ import annotations import json +import math import runpy from pathlib import Path @@ -164,6 +165,22 @@ def _plan( return json.loads(capsys.readouterr().out) +def _assert_greedy_balance(shards: list[list[str]], costs: dict[str, float]) -> None: + """No shard exceeds the mean load by more than its cheapest project. + + Greedy assignment gives each project to the least loaded shard, which + holds at most the mean at that moment, and a shard's last project is its + cheapest. Unlike a fixed ratio to the mean, this holds for every pool, + including one whose costliest project outweighs the mean shard: the pool's + contents change in PRs that do not run this test. + """ + loads = [math.fsum(costs[name] for name in names) for names in shards] + mean = math.fsum(loads) / len(loads) + for names, load in zip(shards, loads): + cheapest = min(costs[name] for name in names) + assert load - cheapest <= mean * (1 + 1e-9), names + + def test_cold_plan_covers_pool_in_balanced_shards( monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str] ) -> None: @@ -172,11 +189,28 @@ def test_cold_plan_covers_pool_in_balanced_shards( shards = plan["matrix"]["include"] assert plan["mode"] == "sharded" assert len(shards) == plan["shard_count"] == PLANNER["MAX_SHARDS"] - projects = [name for shard in shards for name in shard["projects"].split()] - assert sorted(projects) == sorted(PLANNER["unit_sources"]()) - loads = plan["shard_seconds"] - assert len(loads) == len(shards) - assert max(loads) <= 1.25 * sum(loads) / len(loads) + members = [shard["projects"].split() for shard in shards] + sources = PLANNER["unit_sources"]() + assert sorted(name for names in members for name in names) == sorted(sources) + costs = PLANNER["predicted_costs"](sources) + reported = plan["shard_seconds"] + assert len(reported) == len(shards) + for names, seconds in zip(members, reported): + assert abs(seconds - math.fsum(costs[name] for name in names)) <= 1 + _assert_greedy_balance(members, costs) + + +def test_balance_bound_holds_when_one_project_outweighs_a_shard() -> None: + """LeanPool #1035: one import cost more than 1.25 times the mean shard.""" + costs = {"Dominant": 44_000.0, "MovingSofa": 21_000.0} + costs |= {f"Light{index:03d}": 700.0 for index in range(400)} + shards = PLANNER["assign_shards"](costs, 14) + loads = [load for load, _ in shards] + assert max(loads) > 1.25 * sum(loads) / len(loads) + assert [names for _, names in shards if "Dominant" in names] == [["Dominant"]] + _assert_greedy_balance([names for _, names in shards], costs) + with pytest.raises(AssertionError): + _assert_greedy_balance([["Dominant", "MovingSofa"], ["Light000"]], costs) def test_checker_input_change_fans_validation_out_over_the_pool( diff --git a/scripts/ci/plan-build-shards.py b/scripts/ci/plan-build-shards.py index 9514b8b0a0..99fdb5d7c9 100644 --- a/scripts/ci/plan-build-shards.py +++ b/scripts/ci/plan-build-shards.py @@ -48,8 +48,9 @@ from pathlib import Path # Shards also validate their projects, so a whole-pool run is bounded by its -# slowest shard. Beyond this the costliest project (MovingSofa, about 350 -# CPU-minutes cold) fills a shard alone and more shards stop helping. +# slowest shard. Beyond this the costliest project fills a shard alone and +# more shards stop helping (MovingSofa, about 350 CPU-minutes cold, when this +# was set; FiniteIntegralRelationAlgebras, about 730, since 2026-11-27). MAX_SHARDS = 14 FILES_PER_SHARD = 250 # Below either threshold a serial build is already fast; sharding would @@ -161,6 +162,15 @@ def project_costs( return {name: float(size) for name, size in source_bytes.items()} +def predicted_costs(sources: dict[str, list[Path]]) -> dict[str, float]: + """Predict every unit's build cost from its sources and the measurements.""" + source_bytes = { + name: sum(path.stat().st_size for path in paths) + for name, paths in sources.items() + } + return project_costs(source_bytes, load_measured_costs()) + + def dirty_projects(changed_files: list[str], known: set[str]) -> set[str]: """Projects whose directory or umbrella module appears in the diff.""" dirty: set[str] = set() @@ -197,11 +207,7 @@ def sharded_plan( sources: dict[str, list[Path]], targets: set[str], shard_count: int, reason: str ) -> dict: """Bin-pack `targets` into cost-balanced shards and describe the plan.""" - source_bytes = { - name: sum(path.stat().st_size for path in paths) - for name, paths in sources.items() - } - costs = project_costs(source_bytes, load_measured_costs()) + costs = predicted_costs(sources) shards = assign_shards({name: costs[name] for name in targets}, shard_count) return { "mode": "sharded",