From 2fefc79e4dde70d66abdeabebc9a55e0b3d5b205 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 19 Nov 2026 11:07:29 +0000 Subject: [PATCH] feat(smoke): --affected-by, --jobs N and --targeted for the shift smoke split (#352) Plan C, approved by the human 2026-11-19 ("approve C"): - --affected-by REPO[,REPO...]: only workspaces whose smoke `chain:` contains (or which are) one of the repos; names that select nothing are reported, not an error, so a close-out can pass any PR repo. - --jobs N (default 1 = unchanged serial): N workspaces at once, each as its own heart.smoke process with its own cached env and log under $HEART_STATE_DIR/smoke-runtime//smoke.log; OMP/MKL/OPENBLAS/NUMBA thread counts and XLA's CPU pool capped so N x threads <= cores (caps re-applied in the child's runtime env); fails if any workspace fails; per-workspace summary. - --targeted --changed-files PATH... [--names] [--base]: run only the curated smoke_tests.txt entries that are a changed file or mention a changed module's import path or a public def/class/NAME the diff touches; none match -> the cheapest curated script of the changed repo's own workspace as an import check. Runs through PyAutoHands run_python.py --list, reporting to test-results/targeted/; output/ is not wiped. - config: euclid workspace declares scripts_dir "." (its list is root-relative). A human `pyauto-heart smoke` with no flags still runs every workspace serially. Co-Authored-By: Claude Opus 5.5 --- bin/pyauto-heart | 25 ++ config/repos.yaml | 6 + heart/smoke.py | 523 +++++++++++++++++++++++++++++++++- skills/smoke_test/SKILL.md | 15 + tests/test_smoke_selection.py | 415 +++++++++++++++++++++++++++ 5 files changed, 976 insertions(+), 8 deletions(-) create mode 100644 tests/test_smoke_selection.py diff --git a/bin/pyauto-heart b/bin/pyauto-heart index 61b3c69e..80c864e8 100755 --- a/bin/pyauto-heart +++ b/bin/pyauto-heart @@ -454,6 +454,8 @@ cmd_verify_install() { help_smoke() { cat </smoke.log. Fails + if any workspace fails. + --targeted Run only the curated smoke_tests.txt scripts that reference + the change: the script is a changed file, or its source + names a changed library module's import path + (pkg.sub.module) or a public def/class/NAME the diff touches + in it. Nothing matches -> the cheapest curated script of the + changed repo's own workspace, as an import check. Results go + to /test-results/targeted/; output/ is not wiped. + --changed-files PATH... + The changed files (needed by --targeted). + --names N,... Extra public names a matching script may mention. + --base REF Ref the change is diffed against (default origin/main). EOF } cmd_smoke() { diff --git a/config/repos.yaml b/config/repos.yaml index 0adf9912..5ff44e50 100644 --- a/config/repos.yaml +++ b/config/repos.yaml @@ -202,6 +202,8 @@ dependencies: # proves. Read strictly by heart/smoke.py (a missing block fails loudly rather # than silently preparing nothing); an adopter replaces this with their own # workspaces. Repo IDENTITY here must match the body map, same as above. +# `chain:` is also what `pyauto-heart smoke --affected-by REPO` and +# `--targeted` select workspaces by (PyAutoHeart#352). smoke: import_names: PyAutoNerves: autonerves @@ -226,6 +228,10 @@ smoke: euclid: directory: euclid_strong_lens_modeling_pipeline chain: [PyAutoNerves, PyAutoFit, PyAutoArray, PyAutoGalaxy, PyAutoLens] + # Its smoke_tests.txt entries are relative to the repo root, not + # scripts/ (the "." its run_smoke.py passes). Read only by the targeted + # mode (`pyauto-heart smoke --targeted`); default "scripts". + scripts_dir: "." howtolens: directory: HowToLens chain: [PyAutoNerves, PyAutoFit, PyAutoArray, PyAutoGalaxy, PyAutoLens] diff --git a/heart/smoke.py b/heart/smoke.py index 11558720..6c4e8381 100644 --- a/heart/smoke.py +++ b/heart/smoke.py @@ -52,6 +52,12 @@ class WorkspaceSpec: #: rather than inferred from the chain's contents, so the local runner #: and CI are configured by the same explicit statement. arcticpy: bool = False + #: The directory ``smoke_tests.txt`` entries are relative to — the + #: ``directory`` argument the workspace's own ``run_smoke.py`` passes to + #: PyAutoHands' ``run_python.py``. Only the targeted mode reads it (it runs + #: a filtered list through that same entry point); a full run still goes + #: through the workspace-owned runner. + scripts_dir: str = "scripts" #: The one place the arcticpy recipe lives, shared with the CI composite action @@ -81,6 +87,7 @@ def load_smoke_config( spec["directory"], tuple(spec["chain"]), bool(spec.get("arcticpy", False)), + str(spec.get("scripts_dir", "scripts")), ) for key, spec in block["workspaces"].items() } @@ -248,6 +255,12 @@ def runtime_environment( "MPLCONFIGDIR": str(cache_dir / "matplotlib"), } ) + # A `--jobs N` parent hands its thread caps down explicitly and they are + # applied last: the interpreter running this child may itself pin thread + # variables at startup (a venv site hook), which must not undo the cap. + caps = os.environ.get(THREAD_CAPS_VARIABLE) + if caps: + env.update(json.loads(caps)) return env @@ -666,6 +679,408 @@ def run_workspace( return _run_legacy_workspace(python, workspace, organism_root, env) +# --------------------------------------------------------------------------- +# Selection: --affected-by and --targeted (PyAutoHeart#352) +# +# A shift worker runs a *targeted* smoke (only the curated scripts that touch +# the changed code); the shift close-out runs every *affected* workspace, in +# parallel, before an unattended merge. A human `pyauto-heart smoke` with no +# flags keeps the full serial sweep. +# --------------------------------------------------------------------------- + + +def affected_workspaces( + repos: Iterable[str], + workspaces: Mapping[str, WorkspaceSpec] | None = None, +) -> tuple[list[str], list[str]]: + """Workspace keys whose install ``chain`` contains any of ``repos`` (or + whose own ``directory`` is one of them), in config order. + + Returns ``(selected, unmatched)``: ``unmatched`` are the names that select + nothing — an organ or a typo. They are reported, never an error, so a + close-out can pass every changed repo without first filtering out the ones + no smoke workspace installs. + """ + workspaces = WORKSPACES if workspaces is None else workspaces + wanted = [r for r in (r.strip() for r in repos) if r] + selected = [ + key + for key, spec in workspaces.items() + if any(r in spec.chain or r == spec.directory for r in wanted) + ] + known = {r for spec in workspaces.values() for r in spec.chain} + known |= {spec.directory for spec in workspaces.values()} + return selected, [r for r in wanted if r not in known] + + +def own_workspace( + repo: str, workspaces: Mapping[str, WorkspaceSpec] | None = None +) -> str | None: + """The workspace that is ``repo``'s own: the workspace repo itself, else + the first (config order) whose chain ENDS in ``repo``, else the first whose + chain contains it.""" + workspaces = WORKSPACES if workspaces is None else workspaces + for key, spec in workspaces.items(): + if spec.directory == repo: + return key + for key, spec in workspaces.items(): + if spec.chain and spec.chain[-1] == repo: + return key + for key, spec in workspaces.items(): + if repo in spec.chain: + return key + return None + + +@dataclass +class ChangeSummary: + """What a set of changed files touches, for targeted script matching.""" + + #: repos the files live in — chain libraries or workspace directories + repos: list[str] + #: dotted import paths and public names; a script mentioning any matches + tokens: set[str] + #: changed files that are themselves workspace scripts (resolved paths) + scripts: set[Path] + + +_HUNK = re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@", re.M) + + +def _changed_ranges( + repo_dir: Path, relative: str, base: str +) -> list[tuple[int, int]] | None: + """New-side line ranges the branch changed in one file, against the merge + base of ``base`` and HEAD (working-tree edits included). ``None`` when git + cannot say — the caller then treats the whole file as touched.""" + try: + merge_base = subprocess.run( + ["git", "merge-base", base, "HEAD"], + cwd=repo_dir, capture_output=True, text=True, check=True, + ).stdout.strip() + diff = subprocess.run( + ["git", "diff", "-U0", merge_base, "--", relative], + cwd=repo_dir, capture_output=True, text=True, check=True, + ).stdout + except (subprocess.CalledProcessError, OSError): + return None + ranges = [] + for match in _HUNK.finditer(diff): + start = int(match.group(1)) + count = int(match.group(2)) if match.group(2) is not None else 1 + # A pure deletion (count 0) sits between two lines: mark the line it + # follows, so the enclosing definition still counts as touched. + ranges.append((max(start, 1), max(start + count - 1, start, 1))) + return ranges + + +def _public_names(source: str, ranges: list[tuple[int, int]] | None) -> set[str]: + """Public module-level names (``def``/``class``/``NAME =``) whose span + meets a changed range; every public one when ``ranges`` is None. A change + inside a method counts for its class: method names alone are too generic + (``fit``, ``image``) to select scripts by.""" + import ast + + try: + tree = ast.parse(source) + except SyntaxError: + return set() + names = set() + for node in tree.body: + if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)): + found = [node.name] + elif isinstance(node, (ast.Assign, ast.AnnAssign)): + targets = node.targets if isinstance(node, ast.Assign) else [node.target] + found = [t.id for t in targets if isinstance(t, ast.Name)] + else: + continue + start = min( + [node.lineno] + [d.lineno for d in getattr(node, "decorator_list", [])] + ) + end = node.end_lineno or node.lineno + if ranges is not None and not any(a <= end and b >= start for a, b in ranges): + continue + names.update(n for n in found if not n.startswith("_")) + return names + + +def summarise_changes( + organism_root: Path, + changed_files: Iterable[str | os.PathLike[str]], + *, + base: str = "origin/main", + names: Iterable[str] = (), + workspaces: Mapping[str, WorkspaceSpec] | None = None, + import_names: Mapping[str, str] | None = None, +) -> ChangeSummary: + """Map changed files to the repos they live in and the tokens a script + must mention to be affected: each changed library module's dotted import + path (``pkg.sub.module``) and the public names the diff touches in it, + plus any ``names`` given explicitly.""" + workspaces = WORKSPACES if workspaces is None else workspaces + import_names = IMPORT_NAMES if import_names is None else import_names + candidates = {r for spec in workspaces.values() for r in spec.chain} + candidates |= {spec.directory for spec in workspaces.values()} + homes = {} + for repo in sorted(candidates): + try: + homes[repo] = _workspace.repo_path(organism_root, repo).resolve() + except (RuntimeError, FileNotFoundError): + continue + repos: list[str] = [] + tokens = {n.strip() for n in names if n.strip()} + scripts: set[Path] = set() + for raw in changed_files: + path = Path(raw) + if not path.is_absolute() and not path.exists(): + path = organism_root / path + path = path.resolve() + owner = next( + (repo for repo, home in homes.items() if path.is_relative_to(home)), None + ) + if owner is None: + continue + if owner not in repos: + repos.append(owner) + if path.suffix != ".py": + continue + relative = path.relative_to(homes[owner]) + package = import_names.get(owner) + if package is None: + scripts.add(path) # a workspace file: it may itself be a curated script + continue + if not relative.parts or relative.parts[0] != package: + continue # tests/, docs, tooling — not importable library code + parts = list(relative.with_suffix("").parts) + if parts[-1] == "__init__": + parts = parts[:-1] + if len(parts) > 1: # the bare package name would match every script + tokens.add(".".join(parts)) + if path.is_file(): + ranges = _changed_ranges(homes[owner], relative.as_posix(), base) + tokens |= _public_names(path.read_text(errors="replace"), ranges) + return ChangeSummary(repos, tokens, scripts) + + +def curated_scripts(workspace: Path) -> list[str]: + """The entries of a workspace's ``smoke_tests.txt``, in list order.""" + return _load_nonempty_lines(workspace / "smoke_tests.txt") + + +def _mentions(source: str, tokens: Iterable[str]) -> bool: + for token in tokens: + if re.search(r"(? str | None: + """The curated entry with the smallest recorded passing time in the + workspace's ``test-results/smoke_timings.json``; the list's first entry + when there is no timing record.""" + entries = curated_scripts(workspace) + if not entries: + return None + timings = {} + try: + record = json.loads((workspace / "test-results" / "smoke_timings.json").read_text()) + for item in record.get("entries", []): + if item.get("kind", "script") == "script" and item.get("status") == "passed": + timings[str(item["entry"])] = float(item["seconds"]) + except (OSError, ValueError, TypeError, KeyError, AttributeError): + pass + prefix = "" if spec.scripts_dir in ("", ".") else spec.scripts_dir.rstrip("/") + "/" + timed = [ + (timings[key], index, entry) + for index, entry in enumerate(entries) + for key in (prefix + entry, entry) + if key in timings + ] + return min(timed)[2] if timed else entries[0] + + +def targeted_selection( + organism_root: Path, + changes: ChangeSummary, + keys: Iterable[str], + workspaces: Mapping[str, WorkspaceSpec] | None = None, +) -> tuple[dict[str, list[str]], str]: + """Per workspace, the curated entries that reference the changed code. + + An entry matches when the script IS a changed file, or its source mentions + a change token (a dotted module path or a touched public name, as a whole + word). List order is kept. When nothing matches anywhere, the single + cheapest curated script of the first changed repo's own workspace runs as + an import check. Returns ``(selection, reason)``; workspaces with no + ``smoke_tests.txt`` are not selectable.""" + workspaces = WORKSPACES if workspaces is None else workspaces + selection: dict[str, list[str]] = {} + for key in keys: + spec = workspaces[key] + workspace = _workspace.repo_path(organism_root, spec.directory) + script_root = workspace / spec.scripts_dir + matched = [] + for entry in curated_scripts(workspace): + script = (script_root / entry).resolve() + if script in changes.scripts: + matched.append(entry) + continue + try: + source = script.read_text(errors="replace") + except OSError: + continue + if _mentions(source, changes.tokens): + matched.append(entry) + if matched: + selection[key] = matched + if selection: + return selection, "scripts referencing the changed code" + for repo in changes.repos: + key = own_workspace(repo, workspaces) + if key is None: + continue + spec = workspaces[key] + entry = cheapest_script(_workspace.repo_path(organism_root, spec.directory), spec) + if entry: + return {key: [entry]}, f"no script references the change; import check in {key}" + return {}, "no curated script references the change and no own workspace to import-check" + + +def run_script_list( + environment: Path, + organism_root: Path, + state_root: Path, + spec: WorkspaceSpec, + list_file: Path, +) -> int: + """Run an explicit script list through the same PyAutoHands entry point the + workspace runners use, reporting to ``test-results/targeted/`` so the full + run's result files and timings are never overwritten. ``output/`` is not + wiped: a targeted script may read an earlier entry's committed or + previously generated output.""" + workspace = _workspace.repo_path(organism_root, spec.directory) + env = runtime_environment(environment, organism_root, spec, state_root) + python = _environment_python(environment) + runner = _workspace.repo_path(organism_root, "PyAutoHands") / "autohands" / "run_python.py" + command = [ + str(python), str(runner), spec.directory, spec.scripts_dir, + "--list", str(list_file), + "--report-dir", str(workspace / "test-results" / "targeted"), + ] + return subprocess.run(command, cwd=workspace, env=env).returncode + + +# --------------------------------------------------------------------------- +# Parallel workspaces: --jobs N +# --------------------------------------------------------------------------- + +#: Thread-pool variables capped in each concurrent workspace's process so N +#: workspaces never oversubscribe the machine. XLA is capped via XLA_FLAGS. +THREAD_CAP_VARIABLES = ( + "OMP_NUM_THREADS", + "MKL_NUM_THREADS", + "OPENBLAS_NUM_THREADS", + "NUMBA_NUM_THREADS", +) + + +#: Carries ``thread_caps`` from a ``--jobs N`` parent to each workspace child. +THREAD_CAPS_VARIABLE = "HEART_SMOKE_THREAD_CAPS" + + +def thread_caps( + jobs: int, cpu_count: int | None = None, xla_flags: str = "" +) -> dict[str, str]: + """Per-process thread limits so ``jobs`` × threads ≤ the machine's cores.""" + cores = cpu_count or os.cpu_count() or 1 + threads = max(1, cores // max(1, jobs)) + caps = {name: str(threads) for name in THREAD_CAP_VARIABLES} + xla = [f for f in xla_flags.split() if not f.startswith( + ("--xla_cpu_multi_thread_eigen", "intra_op_parallelism_threads") + )] + xla.append(f"--xla_cpu_multi_thread_eigen={'true' if threads > 1 else 'false'}") + xla.append(f"intra_op_parallelism_threads={threads}") + caps["XLA_FLAGS"] = " ".join(xla) + return caps + + +def _child_command(args: argparse.Namespace, key: str, list_file: Path | None) -> list[str]: + command = [ + sys.executable, "-m", "heart.smoke", key, "--jobs", "1", + "--python", str(args.python), + "--root", str(args.root.resolve()), + "--state-dir", str(args.state_dir.resolve()), + ] + if args.rebuild: + command.append("--rebuild") + if args.prepare_only: + command.append("--prepare-only") + if list_file is not None: + command += ["--script-list", str(list_file)] + return command + + +def _log_tail(path: Path, lines: int = 25) -> str: + try: + return "\n".join(path.read_text(errors="replace").splitlines()[-lines:]) + except OSError: + return "(no log)" + + +def run_parallel( + args: argparse.Namespace, + selected: Sequence[str], + jobs: int, + list_files: Mapping[str, Path] | None = None, +) -> int: + """Run each selected workspace as its own ``heart.smoke`` process, ``jobs`` + at a time, thread-capped, its output in its own log file. Each child + prepares (or reuses) its own cached environment and writes its own + ``test-results/``; the run fails when any workspace fails.""" + from concurrent.futures import ThreadPoolExecutor + import time + + state_root = args.state_dir.resolve() + env = os.environ.copy() + caps = thread_caps(jobs, xla_flags=env.get("XLA_FLAGS", "")) + env.update(caps) + env[THREAD_CAPS_VARIABLE] = json.dumps(caps) + env["PYTHONPATH"] = os.pathsep.join( + p for p in (str(HEART_HOME), env.get("PYTHONPATH", "")) if p + ) + list_files = list_files or {} + print( + f"running {len(selected)} workspace(s), {jobs} at a time, " + f"{env['OMP_NUM_THREADS']} thread(s) each" + ) + + def one(key: str) -> tuple[str, int, float, Path]: + log = state_root / "smoke-runtime" / key / "smoke.log" + log.parent.mkdir(parents=True, exist_ok=True) + started = time.monotonic() + with log.open("w") as stream: + code = subprocess.run( + _child_command(args, key, list_files.get(key)), + cwd=HEART_HOME, env=env, stdout=stream, stderr=subprocess.STDOUT, + ).returncode + result = (key, code, time.monotonic() - started, log) + print(f"[{key}] {'PASS' if code == 0 else f'FAIL ({code})'} " + f"in {result[2]:.0f}s — log {log}", flush=True) + return result + + with ThreadPoolExecutor(max_workers=jobs) as pool: + results = list(pool.map(one, selected)) + + failed = [r for r in results if r[1] != 0] + print("\n=== Smoke summary (parallel) ===") + for key, code, seconds, log in results: + print(f" {key:<16} {'PASS' if code == 0 else 'FAIL':<5} {seconds:7.0f}s {log}") + for key, _, _, log in failed: + print(f"\n--- {key}: last lines of {log} ---\n{_log_tail(log)}") + print(f"=== {len(results) - len(failed)}/{len(results)} workspaces passed ===") + return 1 if failed else 0 + + def _parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser( description="Prepare isolated workspace smoke environments and run their suites." @@ -701,13 +1116,103 @@ def _parser() -> argparse.ArgumentParser: default=Path(os.environ.get("HEART_STATE_DIR", Path.home() / ".pyauto-heart")), help="Heart state/cache directory", ) + parser.add_argument( + "--affected-by", + action="append", + default=[], + metavar="REPO[,REPO...]", + help="only workspaces whose install chain contains (or which are) one of these repos", + ) + parser.add_argument( + "--jobs", + type=int, + default=1, + help="workspaces to run at once (default 1 = serial); each runs in its own " + "process with thread pools capped so jobs x threads <= cores", + ) + parser.add_argument( + "--targeted", + action="store_true", + help="run only the curated scripts that reference the changed code " + "(needs --changed-files); none match -> the cheapest script of the " + "changed repo's own workspace, as an import check", + ) + parser.add_argument( + "--changed-files", + nargs="+", + default=[], + metavar="PATH", + help="files the change touches (absolute, or relative to cwd / --root)", + ) + parser.add_argument( + "--names", + action="append", + default=[], + metavar="NAME[,NAME...]", + help="extra public names a matching script may mention (--targeted)", + ) + parser.add_argument( + "--base", + default="origin/main", + help="ref the change is diffed against to find touched names (--targeted)", + ) + parser.add_argument("--script-list", type=Path, help=argparse.SUPPRESS) return parser +def _repo_list(values: Sequence[str]) -> list[str]: + return [r for value in values for r in value.split(",") if r.strip()] + + def main(argv: Sequence[str] | None = None) -> int: - args = _parser().parse_args(argv) + parser = _parser() + args = parser.parse_args(argv) root = args.root.resolve() - selected = args.workspaces or list(WORKSPACES) + state_root = args.state_dir.resolve() + if args.jobs < 1: + parser.error("--jobs must be at least 1") + if args.targeted and not args.changed_files: + parser.error("--targeted needs --changed-files") + if args.script_list and len(args.workspaces) != 1: + parser.error("--script-list runs exactly one workspace") + + selected = list(args.workspaces) or list(WORKSPACES) + if args.affected_by: + affected, unmatched = affected_workspaces(_repo_list(args.affected_by)) + for repo in unmatched: + print(f"--affected-by {repo}: no smoke workspace installs it") + selected = [key for key in selected if key in affected] + + list_files: dict[str, Path] = {} + if args.targeted: + changes = summarise_changes( + root, args.changed_files, base=args.base, names=_repo_list(args.names) + ) + affected, _ = affected_workspaces(changes.repos) + selected = [key for key in selected if key in affected] + selection, reason = targeted_selection(root, changes, selected) + print(f"targeted smoke: {reason}") + for key in selected: + if key not in selection: + print(f"[{key}] no curated script references the change; skipped") + for key, entries in selection.items(): + list_file = state_root / "smoke-runtime" / key / "targeted_smoke_tests.txt" + list_file.parent.mkdir(parents=True, exist_ok=True) + list_file.write_text("\n".join(entries) + "\n") + list_files[key] = list_file + print(f"[{key}] {len(entries)} script(s): " + ", ".join(entries)) + selected = [key for key in selected if key in selection] + elif args.script_list: + list_files[selected[0]] = args.script_list.resolve() + + if not selected: + print("no smoke workspace selected; nothing to run") + return 0 + + jobs = min(args.jobs, len(selected)) + if jobs > 1: + return run_parallel(args, selected, jobs, list_files) + failures = 0 for key in selected: spec = WORKSPACES[key] @@ -719,17 +1224,19 @@ def main(argv: Sequence[str] | None = None) -> int: try: environment, built = prepare_environment( root, - args.state_dir.resolve(), + state_root, spec, python=args.python, rebuild=args.rebuild, ) print(f"[{key}] {'built' if built else 'reused'} {environment}") - if not args.prepare_only: - failures += ( - run_workspace(environment, root, args.state_dir.resolve(), spec) - != 0 - ) + if args.prepare_only: + continue + if key in list_files: + code = run_script_list(environment, root, state_root, spec, list_files[key]) + else: + code = run_workspace(environment, root, state_root, spec) + failures += code != 0 except (SmokeEnvironmentError, subprocess.CalledProcessError, OSError) as exc: print( f"[{key}] environment FAILED before smoke execution: {exc}", diff --git a/skills/smoke_test/SKILL.md b/skills/smoke_test/SKILL.md index 372ab640..f62a046f 100644 --- a/skills/smoke_test/SKILL.md +++ b/skills/smoke_test/SKILL.md @@ -47,6 +47,21 @@ lists `.ipynb` notebooks under `notebooks/`. Notebook + env-var semantics: dependency chain, so never assume only one is affected. Run a subset only when the user explicitly passes workspace names. +Narrower and faster forms, for the shift (PyAutoHeart#352; the human default +above is unchanged): + +```bash +pyauto-heart smoke --affected-by PyAutoGalaxy --jobs 3 # close-out: every workspace whose chain has it, 3 at once +pyauto-heart smoke --targeted --changed-files # worker: only curated scripts that reference the change +``` + +`--affected-by` reads each workspace's `chain:` in Heart `config/repos.yaml`. +`--jobs N` caps each workspace's thread pools so N × threads ≤ cores and logs to +`$HEART_STATE_DIR/smoke-runtime//smoke.log`. `--targeted` keeps a +curated script when it is a changed file or names a changed module's import +path or a public name the diff touches (`--names` adds more); none match → the +cheapest script of the changed repo's own workspace runs as an import check. + ### 2. Prepare and preflight isolated environments Run `pyauto-heart smoke` with the selected workspace keys. Do not invoke the diff --git a/tests/test_smoke_selection.py b/tests/test_smoke_selection.py new file mode 100644 index 00000000..fda4ad4c --- /dev/null +++ b/tests/test_smoke_selection.py @@ -0,0 +1,415 @@ +"""Workspace selection, parallel runs and the targeted mode of heart/smoke.py +(PyAutoHeart#352). + +A shift worker runs a targeted smoke (only the curated scripts that touch the +change); the close-out runs every affected workspace, ``--jobs N`` at a time, +before an unattended merge. Fixture repo names are synthetic (LibraryA, +LibraryB) for the tenant-firewall reason given in test_smoke.py; the one test +against the real policy file reads library names out of the config rather +than spelling them. +""" + +from __future__ import annotations + +import json +import os +import subprocess +import sys +import textwrap +from pathlib import Path + +import pytest + +from heart import smoke + + +# -------------------------------------------------------------------------- +# --affected-by: chain-based workspace selection +# -------------------------------------------------------------------------- + + +def test_affected_by_the_real_chain_table(): + workspaces = smoke.WORKSPACES + # The galaxy library is the last link of the autogalaxy chain; the fit + # library the last link of the autofit chain. Read, not spelled. + galaxy = workspaces["autogalaxy"].chain[-1] + fit = workspaces["autofit"].chain[-1] + + selected, unmatched = smoke.affected_workspaces([galaxy]) + assert unmatched == [] + for skipped in ("autofit", "autocti", "autocti_test"): + assert skipped not in selected + assert {"autogalaxy", "autolens", "autolens_test"} <= set(selected) + + selected, _ = smoke.affected_workspaces([fit]) + assert selected == list(workspaces) + + +def test_affected_by_reports_names_that_select_nothing(): + workspaces = { + "a": smoke.WorkspaceSpec("a", "a_workspace", ("LibraryA",)), + "b": smoke.WorkspaceSpec("b", "b_workspace", ("LibraryA", "LibraryB")), + } + selected, unmatched = smoke.affected_workspaces( + ["LibraryB", "SomeOrgan", "a_workspace"], workspaces + ) + # a_workspace is selected by its own directory name, b by its chain. + assert selected == ["a", "b"] + assert unmatched == ["SomeOrgan"] + + +def test_affected_by_cli_selects_only_affected_workspaces(tmp_path, monkeypatch): + workspaces = { + "a": smoke.WorkspaceSpec("a", "a_workspace", ("LibraryA",)), + "b": smoke.WorkspaceSpec("b", "b_workspace", ("LibraryA", "LibraryB")), + } + for spec in workspaces.values(): + (tmp_path / spec.directory).mkdir() + monkeypatch.setattr(smoke, "WORKSPACES", workspaces) + prepared = [] + monkeypatch.setattr( + smoke, + "prepare_environment", + lambda root, state, spec, **_: prepared.append(spec.key) or (tmp_path, False), + ) + code = smoke.main( + ["--affected-by", "LibraryB", "--prepare-only", + "--root", str(tmp_path), "--state-dir", str(tmp_path / "state")] + ) + assert code == 0 + assert prepared == ["b"] + + +# -------------------------------------------------------------------------- +# --jobs N: concurrent workspaces, same verdict and result files as serial +# -------------------------------------------------------------------------- + +RUN_SMOKE = textwrap.dedent( + """ + import json, os, pathlib, sys + ws = pathlib.Path(__file__).resolve().parents[2] + out = ws / "test-results" + out.mkdir(exist_ok=True) + fail = (ws / "FAIL").exists() + caps = {k: os.environ.get(k) for k in + ("OMP_NUM_THREADS", "MKL_NUM_THREADS", "OPENBLAS_NUM_THREADS", + "NUMBA_NUM_THREADS", "XLA_FLAGS")} + (out / "result.json").write_text(json.dumps({"workspace": ws.name, "failed": fail})) + (out / "env.json").write_text(json.dumps(caps)) + print("ran", ws.name) + sys.exit(1 if fail else 0) + """ +) + +# The parallel path starts one `heart.smoke` process per workspace, so pytest's +# monkeypatching cannot reach it. This child re-creates the same fake world — +# the workspace table and a no-op environment build — then runs the real main. +FAKE_CHILD = textwrap.dedent( + """ + import json, os, pathlib, sys + from heart import smoke + table = json.loads(os.environ["FAKE_SMOKE_TABLE"]) + smoke.WORKSPACES = {k: smoke.WorkspaceSpec(k, v["directory"], tuple(v["chain"])) + for k, v in table.items()} + environment = pathlib.Path(os.environ["FAKE_SMOKE_ENV"]) + smoke.prepare_environment = lambda *a, **k: (environment, False) + sys.exit(smoke.main(sys.argv[1:])) + """ +) + + +def make_world(tmp_path: Path, failing: set[str] = frozenset()): + root = tmp_path / "organism" + workspaces = {} + for key, chain in { + "one": ("LibraryA",), + "two": ("LibraryA", "LibraryB"), + "three": ("LibraryA", "LibraryB"), + }.items(): + spec = smoke.WorkspaceSpec(key, f"{key}_workspace", chain) + workspaces[key] = spec + scripts = root / spec.directory / ".github" / "scripts" + scripts.mkdir(parents=True) + (scripts / "run_smoke.py").write_text(RUN_SMOKE) + if key in failing: + (root / spec.directory / "FAIL").write_text("") + for repo in ("LibraryA", "LibraryB"): + (root / repo).mkdir() + (root / "PyAutoHands" / "autohands").mkdir(parents=True) + environment = tmp_path / "environment" + python = smoke._environment_python(environment) + python.parent.mkdir(parents=True) + # The base interpreter, not a venv's: a venv may pin thread variables at + # startup (a site .pth), which would mask the caps under test. + python.symlink_to(getattr(sys, "_base_executable", sys.executable)) + return root, workspaces, environment + + +def results(root: Path, workspaces) -> dict[str, dict]: + return { + key: json.loads( + (root / spec.directory / "test-results" / "result.json").read_text() + ) + for key, spec in workspaces.items() + } + + +def run_both(tmp_path, monkeypatch, failing=frozenset()): + """Serial in-process, then --jobs 3 through real child processes.""" + root, workspaces, environment = make_world(tmp_path, set(failing)) + monkeypatch.setattr(smoke, "WORKSPACES", workspaces) + monkeypatch.setattr( + smoke, "prepare_environment", lambda *a, **k: (environment, False) + ) + common = ["--root", str(root), "--state-dir", str(tmp_path / "state")] + + serial_code = smoke.main(common) + serial = results(root, workspaces) + for spec in workspaces.values(): + (root / spec.directory / "test-results" / "result.json").unlink() + + child = tmp_path / "fake_child.py" + child.write_text(FAKE_CHILD) + monkeypatch.setenv( + "FAKE_SMOKE_TABLE", + json.dumps({k: {"directory": s.directory, "chain": list(s.chain)} + for k, s in workspaces.items()}), + ) + monkeypatch.setenv("FAKE_SMOKE_ENV", str(environment)) + real = smoke._child_command + monkeypatch.setattr( + smoke, + "_child_command", + lambda *a: [sys.executable, str(child), *real(*a)[3:]], + ) + parallel_code = smoke.main([*common, "--jobs", "3"]) + parallel = results(root, workspaces) + return root, workspaces, serial_code, serial, parallel_code, parallel + + +def test_jobs_gives_the_same_verdict_and_result_files_as_serial( + tmp_path, monkeypatch, capsys +): + root, workspaces, serial_code, serial, parallel_code, parallel = run_both( + tmp_path, monkeypatch + ) + assert serial_code == parallel_code == 0 + assert serial == parallel + assert set(parallel) == {"one", "two", "three"} + out = capsys.readouterr().out + assert "3/3 workspaces passed" in out + # Each workspace's output went to its own log, not interleaved stdout. + for key in workspaces: + log = tmp_path / "state" / "smoke-runtime" / key / "smoke.log" + assert f"ran {key}_workspace" in log.read_text() + + +def test_one_failing_workspace_fails_the_parallel_run(tmp_path, monkeypatch, capsys): + _, _, serial_code, serial, parallel_code, parallel = run_both( + tmp_path, monkeypatch, failing={"two"} + ) + assert serial_code == parallel_code == 1 + assert serial == parallel + assert [k for k, r in parallel.items() if r["failed"]] == ["two"] + out = capsys.readouterr().out + assert "2/3 workspaces passed" in out + assert "--- two: last lines of" in out + + +def test_parallel_children_get_thread_caps(tmp_path, monkeypatch): + monkeypatch.setattr(smoke.os, "cpu_count", lambda: 12) + monkeypatch.setenv("XLA_FLAGS", "--xla_dump_to=/x intra_op_parallelism_threads=64") + root, workspaces, *_ = run_both(tmp_path, monkeypatch) + for spec in workspaces.values(): + caps = json.loads( + (root / spec.directory / "test-results" / "env.json").read_text() + ) + for name in smoke.THREAD_CAP_VARIABLES: + assert caps[name] == "4", name + flags = caps["XLA_FLAGS"].split() + assert "intra_op_parallelism_threads=4" in flags + assert "intra_op_parallelism_threads=64" not in flags + assert "--xla_dump_to=/x" in flags + + +def test_thread_caps_keep_jobs_times_threads_within_cores(): + for jobs, cores in [(1, 8), (3, 8), (3, 12), (4, 2), (16, 16)]: + caps = smoke.thread_caps(jobs, cpu_count=cores) + threads = int(caps["OMP_NUM_THREADS"]) + assert threads >= 1 + assert jobs * threads <= max(cores, jobs) + assert "--xla_cpu_multi_thread_eigen=false" in smoke.thread_caps(8, 8)["XLA_FLAGS"] + + +# -------------------------------------------------------------------------- +# --targeted: curated scripts that reference the changed code +# -------------------------------------------------------------------------- + +LIBRARY_MODULE = textwrap.dedent( + """ + def alpha_profile(): + return 1 + + + def beta_profile(): + return 2 + + + class GammaModel: + def fit(self): + return 3 + """ +) + + +def git(cwd: Path, *args: str) -> str: + return subprocess.run( + ["git", "-c", "user.name=T", "-c", "user.email=t@example.invalid", *args], + cwd=cwd, check=True, capture_output=True, text=True, + ).stdout + + +def targeted_world(tmp_path: Path): + root = tmp_path / "organism" + library = root / "LibraryA" + module = library / "librarya" / "sub" / "mod.py" + module.parent.mkdir(parents=True) + module.write_text(LIBRARY_MODULE) + git(library, "init", "-b", "main") + git(library, "add", ".") + git(library, "commit", "-m", "base") + git(library, "checkout", "-b", "feature") + # Touch alpha_profile only. + module.write_text(LIBRARY_MODULE.replace("return 1", "return 10")) + git(library, "commit", "-am", "change alpha") + + workspaces = { + "own": smoke.WorkspaceSpec("own", "own_workspace", ("LibraryA",)), + "other": smoke.WorkspaceSpec("other", "other_workspace", ("LibraryB",)), + } + scripts = { + "uses_alpha.py": "import librarya as la\nla.sub.mod.alpha_profile()\n", + "uses_beta.py": "import librarya as la\nla.sub.mod.beta_profile()\n", + "imports_module.py": "from librarya.sub.mod import beta_profile\n", + "unrelated.py": "import librarya\nprint('alpha_profiles')\n", + "skipped.py": "alpha_profile()\n", + } + for spec in workspaces.values(): + folder = root / spec.directory / "scripts" + folder.mkdir(parents=True) + for name, body in scripts.items(): + (folder / name).write_text(body) + (root / spec.directory / "smoke_tests.txt").write_text( + "# curated\nunrelated.py\nuses_beta.py\nuses_alpha.py\n" + "imports_module.py\n# skipped.py\n" + ) + (root / "LibraryB").mkdir() + return root, workspaces, module + + +def test_targeted_selects_scripts_by_import_path_and_touched_names(tmp_path): + root, workspaces, module = targeted_world(tmp_path) + changes = smoke.summarise_changes( + root, [module], base="main", workspaces=workspaces, + import_names={"LibraryA": "librarya"}, + ) + assert changes.repos == ["LibraryA"] + # alpha_profile was touched; beta_profile and GammaModel were not. + assert changes.tokens == {"librarya.sub.mod", "alpha_profile"} + + keys, _ = smoke.affected_workspaces(changes.repos, workspaces) + assert keys == ["own"] + selection, reason = smoke.targeted_selection(root, changes, keys, workspaces) + # List order kept; commented-out entries never run; `alpha_profiles` is a + # different word, so `unrelated.py` does not match. + assert selection == {"own": ["uses_alpha.py", "imports_module.py"]} + assert "referencing" in reason + + +def test_targeted_falls_back_to_the_cheapest_own_script(tmp_path): + root, workspaces, _ = targeted_world(tmp_path) + timings = root / "own_workspace" / "test-results" / "smoke_timings.json" + timings.parent.mkdir() + timings.write_text(json.dumps({"entries": [ + {"entry": "scripts/unrelated.py", "kind": "script", "status": "passed", "seconds": 9.0}, + {"entry": "scripts/uses_beta.py", "kind": "script", "status": "passed", "seconds": 2.5}, + {"entry": "scripts/uses_alpha.py", "kind": "script", "status": "failed", "seconds": 0.1}, + ]})) + changes = smoke.ChangeSummary(["LibraryA"], {"nothing_mentions_this"}, set()) + selection, reason = smoke.targeted_selection(root, changes, ["own"], workspaces) + assert selection == {"own": ["uses_beta.py"]} + assert "import check" in reason + + +def test_a_changed_workspace_script_selects_itself(tmp_path): + root, workspaces, _ = targeted_world(tmp_path) + changes = smoke.summarise_changes( + root, [root / "other_workspace" / "scripts" / "unrelated.py"], + workspaces=workspaces, import_names={"LibraryA": "librarya"}, + ) + assert changes.repos == ["other_workspace"] + keys, _ = smoke.affected_workspaces(changes.repos, workspaces) + selection, _ = smoke.targeted_selection(root, changes, keys, workspaces) + assert selection == {"other": ["unrelated.py"]} + + +def test_targeted_cli_runs_the_filtered_list_through_hands(tmp_path, monkeypatch): + root, workspaces, module = targeted_world(tmp_path) + monkeypatch.setattr(smoke, "WORKSPACES", workspaces) + monkeypatch.setattr(smoke, "IMPORT_NAMES", {"LibraryA": "librarya"}) + environment = tmp_path / "environment" + monkeypatch.setattr( + smoke, "prepare_environment", lambda *a, **k: (environment, False) + ) + calls = [] + monkeypatch.setattr( + smoke.subprocess, + "run", + lambda command, **kwargs: calls.append((command, kwargs)) + or subprocess.CompletedProcess(command, 0), + ) + monkeypatch.setattr(smoke, "_changed_ranges", lambda *a: [(2, 3)]) + state = tmp_path / "state" + code = smoke.main( + ["--targeted", "--changed-files", str(module), + "--root", str(root), "--state-dir", str(state)] + ) + assert code == 0 + (command, kwargs), = calls + list_file = state / "smoke-runtime" / "own" / "targeted_smoke_tests.txt" + assert list_file.read_text().split() == ["uses_alpha.py", "imports_module.py"] + assert command[1].endswith(os.path.join("PyAutoHands", "autohands", "run_python.py")) + assert command[2:] == [ + "own_workspace", "scripts", "--list", str(list_file), + "--report-dir", str(root / "own_workspace" / "test-results" / "targeted"), + ] + assert kwargs["cwd"] == root / "own_workspace" + + +def test_targeted_needs_changed_files(): + with pytest.raises(SystemExit): + smoke.main(["--targeted"]) + + +def test_scripts_dir_is_read_from_config(tmp_path): + config = tmp_path / "repos.yaml" + config.write_text( + "smoke:\n" + " import_names: {}\n" + " workspaces:\n" + " flat: {directory: flat_ws, chain: [LibraryA], scripts_dir: '.'}\n" + " usual: {directory: usual_ws, chain: [LibraryA]}\n" + ) + workspaces, _ = smoke.load_smoke_config(config) + assert workspaces["flat"].scripts_dir == "." + assert workspaces["usual"].scripts_dir == "scripts" + + +def test_cli_help_documents_the_new_flags(): + root = Path(__file__).resolve().parents[1] + result = subprocess.run( + ["bash", str(root / "bin" / "pyauto-heart"), "help", "smoke"], + capture_output=True, text=True, + ) + for flag in ("--affected-by", "--jobs", "--targeted", "--changed-files"): + assert flag in result.stdout, flag