From 4b4cb80dbc3201ed399f6dd20530eda879ef9025 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Wed, 21 Oct 2026 10:52:47 +0100 Subject: [PATCH] fix: emit one ordered shard for order-dependent smoke lists script_matrix.py shards a project per scripts/, so the Euclid pipeline's only job was scripts/tools, running diagnose_latent.py without the fits it replays (cloud#39224774724). Add an explicit, caller-owned --ordered PROJECT flag: such a project becomes a single entry {name, directory: ".", script_list: "smoke_tests.txt"} for run_python.py --list. Other projects' entries are unchanged, and an --ordered name outside the project list is an error. Refs #317 Co-Authored-By: Claude Opus 5.5 --- autohands/script_matrix.py | 73 +++++++++++++++++--- tests/test_script_matrix.py | 133 ++++++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+), 8 deletions(-) create mode 100644 tests/test_script_matrix.py diff --git a/autohands/script_matrix.py b/autohands/script_matrix.py index 39949ef8..1698dc1a 100755 --- a/autohands/script_matrix.py +++ b/autohands/script_matrix.py @@ -1,9 +1,25 @@ #!/usr/bin/env python +""" +Build the workspace-validation script matrix. + +By default a project is sharded by directory: one entry per +``/scripts//``, each run as its own job. + +A project named with ``--ordered`` is instead emitted as ONE entry that carries +its ordered smoke list (``smoke_tests.txt``), to be run in a single job via +``run_python.py --list``. This is for projects whose smoke scripts depend on +each other's output (the Euclid pipeline: ``scripts/tools/diagnose_latent.py`` +replays fits that ``start_here.py`` and the top-level ``scripts/*.py`` produce +earlier in the list), so per-directory sharding would run a consumer without +its producers. The opt-in is explicit and owned by the caller (the workflow), +not inferred from ``smoke_tests.txt`` being present — every workspace has one. +""" import json -import os -import sys +from argparse import ArgumentParser from pathlib import Path +SMOKE_LIST = "smoke_tests.txt" + def make_script_matrix(project): return [ @@ -16,12 +32,53 @@ def make_script_matrix(project): ] -def make_combined_script_matrix(projects): - return [item for project in projects for item in make_script_matrix(project)] +def make_ordered_entry(project): + """ + The single matrix entry for a project whose smoke list is order-dependent. + ``directory`` is ``"."`` because the list's entries are paths relative to + the project root (``start_here.py``, ``scripts/.py``); the runner + resolves them against ``/`` with the project root as cwd. + """ + return { + "name": project, + "directory": ".", + "script_list": SMOKE_LIST, + } -if __name__ == "__main__": - projects = sys.argv[1:] - output = make_combined_script_matrix(projects) - print(json.dumps(output)) +def make_combined_script_matrix(projects, ordered=()): + ordered = set(ordered) + unknown = ordered - set(projects) + if unknown: + raise ValueError( + f"--ordered names project(s) not in the matrix: {sorted(unknown)}" + ) + matrix = [] + for project in projects: + if project in ordered: + matrix.append(make_ordered_entry(project)) + else: + matrix.extend(make_script_matrix(project)) + return matrix + + +def main(argv=None): + parser = ArgumentParser(description=__doc__.strip().splitlines()[0]) + parser.add_argument("projects", nargs="*", help="Project directories to shard") + parser.add_argument( + "--ordered", + action="append", + default=[], + metavar="PROJECT", + help=( + f"Run PROJECT's ordered {SMOKE_LIST} as one shard instead of one " + "shard per scripts/. Repeatable." + ), + ) + args = parser.parse_args(argv) + return make_combined_script_matrix(args.projects, ordered=args.ordered) + + +if __name__ == "__main__": + print(json.dumps(main())) diff --git a/tests/test_script_matrix.py b/tests/test_script_matrix.py new file mode 100644 index 00000000..bed24eb4 --- /dev/null +++ b/tests/test_script_matrix.py @@ -0,0 +1,133 @@ +""" +Cover the two shapes of the workspace-validation script matrix. + +Per-directory (the default): one entry per ``/scripts//``. +Ordered (``--ordered PROJECT``): one entry carrying the project's +``smoke_tests.txt``, so an order-dependent suite (the Euclid pipeline, whose +``scripts/tools/diagnose_latent.py`` replays fits run earlier in the list) runs +in one job instead of a consumer shard without its producers (PyAutoHands#317). +""" + +import json +import subprocess +import sys +from pathlib import Path + +import pytest + +PROJECT_ROOT = Path(__file__).parent.parent +AUTOHANDS_DIR = PROJECT_ROOT / "autohands" +sys.path.insert(0, str(AUTOHANDS_DIR)) + +from script_matrix import ( # noqa: E402 + main, + make_combined_script_matrix, + make_script_matrix, +) + + +@pytest.fixture +def projects(tmp_path, monkeypatch): + """A sharded workspace and an order-dependent pipeline, side by side.""" + for sub in ("imaging", "interferometer"): + (tmp_path / "ws" / "scripts" / sub).mkdir(parents=True) + (tmp_path / "ws" / "smoke_tests.txt").write_text("imaging/a.py\n") + (tmp_path / "pipe" / "scripts" / "tools").mkdir(parents=True) + (tmp_path / "pipe" / "scripts" / "full_model.py").write_text("") + (tmp_path / "pipe" / "start_here.py").write_text("") + (tmp_path / "pipe" / "smoke_tests.txt").write_text( + "start_here.py\nscripts/full_model.py\nscripts/tools/diagnose_latent.py\n" + ) + monkeypatch.chdir(tmp_path) + return tmp_path + + +def _sorted(entries): + return sorted(entries, key=lambda e: (e["name"], e["directory"])) + + +def test_default_shape_is_one_entry_per_scripts_subdir(projects): + assert _sorted(make_script_matrix("ws")) == [ + {"name": "ws", "directory": "imaging"}, + {"name": "ws", "directory": "interferometer"}, + ] + + +def test_smoke_list_presence_alone_does_not_opt_in(projects): + """Every workspace has a smoke_tests.txt; only --ordered changes the shape.""" + matrix = make_combined_script_matrix(["ws", "pipe"]) + assert all("script_list" not in e for e in matrix) + assert {"name": "pipe", "directory": "tools"} in matrix + + +def test_ordered_project_is_one_entry_carrying_its_list(projects): + matrix = make_combined_script_matrix(["ws", "pipe"], ordered=["pipe"]) + pipe = [e for e in matrix if e["name"] == "pipe"] + assert pipe == [ + {"name": "pipe", "directory": ".", "script_list": "smoke_tests.txt"} + ] + + +def test_ordering_one_project_leaves_the_others_byte_identical(projects): + before = [e for e in make_combined_script_matrix(["ws", "pipe"]) if e["name"] == "ws"] + after = [ + e + for e in make_combined_script_matrix(["ws", "pipe"], ordered=["pipe"]) + if e["name"] == "ws" + ] + assert json.dumps(before) == json.dumps(after) + + +def test_ordered_name_outside_the_matrix_is_an_error(projects): + """A typo in the opt-in must not silently fall back to per-dir shards.""" + with pytest.raises(ValueError, match="not in the matrix"): + make_combined_script_matrix(["ws"], ordered=["pipe"]) + + +def test_cli_without_flag_matches_the_historical_positional_call(projects): + assert main(["ws", "pipe"]) == make_combined_script_matrix(["ws", "pipe"]) + + +def test_cli_ordered_flag_after_positionals(projects): + """The workflow appends the flag after the project list; both orders parse.""" + out = subprocess.run( + [sys.executable, str(AUTOHANDS_DIR / "script_matrix.py"), + "ws", "pipe", "--ordered", "pipe"], + cwd=projects, capture_output=True, text=True, check=True, + ).stdout + matrix = json.loads(out) + assert {"name": "pipe", "directory": ".", "script_list": "smoke_tests.txt"} in matrix + assert sum(e["name"] == "pipe" for e in matrix) == 1 + assert sum(e["name"] == "ws" for e in matrix) == 2 + + +def test_ordered_entry_feeds_the_list_runner_and_keeps_per_script_overrides( + projects, monkeypatch +): + """The entry's (directory, script_list) resolve every list line from the + project root, in order, and a profile override keyed on a path pattern + (euclid's diagnose_latent PYAUTO_OUTPUT_DIR) still matches the resolved path + — the `.` directory must not leak into the path the pattern is tested on.""" + from build_util import files_from_list + from env_config import apply_profile + + (entry,) = make_combined_script_matrix(["pipe"], ordered=["pipe"]) + root = projects / "pipe" + monkeypatch.chdir(root) + files = files_from_list(entry["directory"], entry["script_list"]) + assert [str(f.relative_to(root)) for f in files] == [ + "start_here.py", + "scripts/full_model.py", + "scripts/tools/diagnose_latent.py", + ] + profile = { + "defaults": {"PYAUTO_TEST_MODE": "2"}, + "overrides": [ + { + "pattern": "scripts/tools/diagnose_latent.py", + "set": {"PYAUTO_OUTPUT_DIR": "output/test_mode"}, + } + ], + } + assert apply_profile({}, files[-1], profile)["PYAUTO_OUTPUT_DIR"] == "output/test_mode" + assert "PYAUTO_OUTPUT_DIR" not in apply_profile({}, files[0], profile)