diff --git a/autohands/check_dataset_allowlist.py b/autohands/check_dataset_allowlist.py index 2dba1726..d6ee49e7 100644 --- a/autohands/check_dataset_allowlist.py +++ b/autohands/check_dataset_allowlist.py @@ -19,7 +19,8 @@ Leg 2 — the mirror-image failure. The check above asserts that nothing *generated* got committed. This one asserts that nothing *committed* gets -deleted: `should_simulate()` ends in `shutil.rmtree`, so a script that reaches an +deleted: `should_simulate()` (and its one-line wrapper `simulate_if_needed()`) +ends in `shutil.rmtree`, so a script that reaches an allowlisted dataset directory while `PYAUTO_SMALL_DATASETS=1` is still in force destroys data the allowlist exists to protect. The library-side stamp guard (`_is_capped_at_the_current_cap`) cannot cover this in general — it reads @@ -235,8 +236,31 @@ def _normalise(path_str: str) -> str: return "/".join(parts) +# Library calls that can end in ``shutil.rmtree`` of their dataset-path +# argument. ``simulate_if_needed(dataset_path, simulator)`` is the +# one-line wrapper around ``should_simulate`` + the simulator subprocess, so a +# script swept onto it reaches the same rmtree and must stay covered. +DELETING_CALLS = ("should_simulate", "simulate_if_needed") + + +def _dataset_path_arg(node): + """The dataset-path argument of a deleting call: first positional, else the + ``dataset_path=`` keyword. ``None`` if neither is present.""" + if node.args: + return node.args[0] + for kw in node.keywords: + if kw.arg == "dataset_path": + return kw.value + return None + + def should_simulate_sites(py_files): - """Yield ``(file, lineno, resolved_path_or_None)`` per ``should_simulate`` call.""" + """Yield ``(file, lineno, resolved_path_or_None)`` per deleting call site. + + Matches every name in ``DELETING_CALLS`` -- ``should_simulate(...)`` and its + one-line wrapper ``simulate_if_needed(...)`` -- since both delete the + dataset path under the capped regime. + """ for f in py_files: try: tree = ast.parse(Path(f).read_text(encoding="utf-8", errors="replace")) @@ -248,9 +272,12 @@ def should_simulate_sites(py_files): continue func = node.func fname = func.attr if isinstance(func, ast.Attribute) else getattr(func, "id", "") - if fname != "should_simulate" or not node.args: + if fname not in DELETING_CALLS: + continue + arg = _dataset_path_arg(node) + if arg is None: continue - resolved = _resolve(node.args[0], _names_before(assignments, node.lineno)) + resolved = _resolve(arg, _names_before(assignments, node.lineno)) yield f, node.lineno, ( _normalise(resolved) if resolved is not UNRESOLVED else UNRESOLVED ) @@ -264,7 +291,8 @@ def tracked_python_files(): def check_capped_deletion(prefixes, tracked) -> int: - """Leg 2 — allowlisted datasets reachable from a capped ``should_simulate``. + """Leg 2 — allowlisted datasets reachable from a capped ``should_simulate`` + (or ``simulate_if_needed``) call. ``prefixes``/``tracked`` are leg 1's already-computed allowlist and tracked file list, so this adds no extra git calls beyond the Python file listing. @@ -308,7 +336,7 @@ def deletes_tracked(resolved: str) -> bool: if violations: print( - f"[capped-deletion] FAIL — should_simulate() would delete COMMITTED " + f"[capped-deletion] FAIL — should_simulate()/simulate_if_needed() would delete COMMITTED " f"files without releasing PYAUTO_SMALL_DATASETS ({len(violations)}):", file=sys.stderr, ) @@ -316,7 +344,8 @@ def deletes_tracked(resolved: str) -> bool: declared = " ".join(tokens) if tokens else "" print(f" {f}:{lineno} -> {resolved} [ENV: {declared}]", file=sys.stderr) print( - "\nshould_simulate() ends in shutil.rmtree, and each path above holds " + "\nshould_simulate() (and simulate_if_needed(), which wraps it) ends in " + "shutil.rmtree, and each path above holds " "git-tracked files kept by design (they carry an `!dataset/...` allowlist " "line). Under PYAUTO_SMALL_DATASETS=1 those files are deleted and replaced " "with capped-simulator output. Add a releasing token to the script's " @@ -326,7 +355,8 @@ def deletes_tracked(resolved: str) -> bool: return 1 print( - f"[capped-deletion] OK — no capped should_simulate() call site would delete " + f"[capped-deletion] OK — no capped should_simulate()/simulate_if_needed() " + f"call site would delete " f"any of the {len(tracked)} committed dataset file(s)." ) return 0 diff --git a/tests/test_dataset_allowlist_capped_deletion.py b/tests/test_dataset_allowlist_capped_deletion.py index 921cd1e0..03f654f2 100644 --- a/tests/test_dataset_allowlist_capped_deletion.py +++ b/tests/test_dataset_allowlist_capped_deletion.py @@ -221,6 +221,69 @@ def test_unresolvable_call_site_is_reported_and_skipped( assert "script.py:3" in captured.out +# --- simulate_if_needed (one-line wrapper) --------------------------------- + + +def _sites(tmp_path, src): + from autohands.check_dataset_allowlist import should_simulate_sites + + f = tmp_path / "script.py" + f.write_text(src) + return [(lineno, resolved) for _, lineno, resolved in should_simulate_sites([f])] + + +def test_simulate_if_needed_call_site_is_found_like_should_simulate(tmp_path): + """Sweeping the 8-line ``should_simulate`` + ``subprocess.run`` block onto + ``simulate_if_needed`` must not drop the call site out of the guard: both + forms yield the same resolved dataset path.""" + old = ( + 'from pathlib import Path\n' + 'dataset_path = Path("dataset", "imaging", "lens_x2")\n' + 'if al.util.dataset.should_simulate(str(dataset_path)):\n' + ' import subprocess\n' + ' import sys\n' + ' subprocess.run([sys.executable, "scripts/simulator/lens_x2.py"], check=True)\n' + ) + new = ( + 'from pathlib import Path\n' + 'dataset_path = Path("dataset", "imaging", "lens_x2")\n' + 'al.util.dataset.simulate_if_needed(\n' + ' dataset_path, simulator="scripts/simulator/lens_x2.py"\n' + ')\n' + ) + assert _sites(tmp_path, old) == [(3, "dataset/imaging/lens_x2")] + assert _sites(tmp_path, new) == [(3, "dataset/imaging/lens_x2")] + + +def test_simulate_if_needed_dataset_path_keyword_is_resolved(tmp_path): + src = ( + 'simulate_if_needed(simulator="scripts/sim.py", dataset_path="dataset/x")\n' + ) + assert _sites(tmp_path, src) == [(1, "dataset/x")] + + +def test_simulate_if_needed_without_a_dataset_path_is_ignored(tmp_path): + assert _sites(tmp_path, "simulate_if_needed()\n") == [] + + +def test_simulate_if_needed_reaching_tracked_files_fails( + tmp_path, monkeypatch, capsys +): + """End to end: the swept form of the originating bug still fails the gate.""" + src = ( + '"""\n__Env__\n\nENV: real_plots\n"""\n' + 'from pathlib import Path\n' + 'p = Path("dataset", "overview")\n' + 'aa.util.dataset.simulate_if_needed(p, simulator="scripts/sim.py")\n' + ) + tracked = ["dataset/overview/ccd.gif"] + + code, captured = _run_check(tmp_path, monkeypatch, src, tracked, capsys) + + assert code == 1 + assert "script.py:8 -> dataset/overview" in captured.err + + # --- dual invocation context ------------------------------------------------