From 461ac836cefa7b2294ef97ddf9dcf719beb2e4f6 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Mon, 14 Sep 2026 21:18:38 +0100 Subject: [PATCH] fix: call analysis.save_results from the test-mode bypass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `_fit_bypass_test_mode` — the sampler bypass behind `PYAUTO_TEST_MODE=2` and `=3` — wrote `samples`, `samples_summary` and the `.completed` marker but never ran the analysis' own output hooks, which `start_resume_fit` calls unconditionally. Its docstring promised "all expected output files so that downstream code sees a complete result folder", so any script that writes a file in `save_results` and reads it back after the fit failed under smoke: autofit_workspace `overview/overview_2_scientific_workflow` (`.py` and `.ipynb`) died on a missing `science_summary.json` in Heart workspace-smoke run 34824535982. The gap predates the recent commits — it arrived with modes 2/3 in d41aa53e8. The bypass now calls `analysis.save_results` and `analysis.save_results_combined` between `make_result` and `paths.completed()`, in the same position and with the same arguments as the normal path. They are deliberately not gated on `skip_fit_output()`: the normal path does not gate them either, and the smoke profile that exposed the bug sets `PYAUTO_SKIP_FIT_OUTPUT=1`, so gating would reproduce the bug under exactly the conditions this fix targets. Tested by a new `TestBypassCallsSaveResults`, parametrised over modes 2 and 3, which asserts both hooks run once with the fit's own `paths` and `result`. It fails in both modes without the source change. Refs #1624 Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01RbJGue5FwUPbxvCFKwXNam --- autofit/non_linear/search/abstract_search.py | 7 ++ .../non_linear/search/test_abstract_search.py | 65 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/autofit/non_linear/search/abstract_search.py b/autofit/non_linear/search/abstract_search.py index 9abff5a0c..960edbef2 100644 --- a/autofit/non_linear/search/abstract_search.py +++ b/autofit/non_linear/search/abstract_search.py @@ -1203,6 +1203,13 @@ def _fit_bypass_test_mode( search_internal=None, ) + # Run the analysis' own output hooks, exactly as start_resume_fit does — + # a script that writes a file in save_results and reads it back after the + # fit (e.g. autofit_workspace overview_2's science_summary.json) sees a + # complete result folder only if the bypass calls them too. + analysis.save_results(paths=self.paths, result=result) + analysis.save_results_combined(paths=self.paths, result=result) + model.unfreeze() # Mark the fit complete, exactly as start_resume_fit does — a bypassed diff --git a/test_autofit/non_linear/search/test_abstract_search.py b/test_autofit/non_linear/search/test_abstract_search.py index 0d105c880..08a2f2fa0 100644 --- a/test_autofit/non_linear/search/test_abstract_search.py +++ b/test_autofit/non_linear/search/test_abstract_search.py @@ -423,6 +423,71 @@ def _poison(*args, **kwargs): assert result is not None +class _SaveResultsRecordingAnalysis(af.m.MockAnalysis): + """Records every call its output hooks receive, so a test can assert the + bypass ran them rather than inspecting files (the test config runs with + `remove_files: true`).""" + + def __init__(self): + super().__init__() + self.save_results_calls = [] + self.save_results_combined_calls = [] + + def log_likelihood_function(self, instance): + # A float, not `MockAnalysis`' `[1]`: mode 2 calls the likelihood once + # and casts the return with `float()`. + return 1.0 + + def save_results(self, paths, result): + self.save_results_calls.append((paths, result)) + + def save_results_combined(self, paths, result): + self.save_results_combined_calls.append((paths, result)) + + +class TestBypassCallsSaveResults: + """ + The bypass (PYAUTO_TEST_MODE=2/3) promises "all expected output files so + that downstream code sees a complete result folder", but it wrote only + samples, samples_summary and the .completed marker — it never ran the + analysis' own output hooks, which start_resume_fit calls unconditionally. + Any script that writes a file in `save_results` and reads it back after the + fit therefore failed under smoke: autofit_workspace + `overview_2_scientific_workflow` died on a missing `science_summary.json` + in Heart's workspace-smoke run (PyAutoFit #1624). + + The hooks are NOT gated on `skip_fit_output()` — the normal path does not + gate them either, and the smoke profile that exposed the bug sets + `PYAUTO_SKIP_FIT_OUTPUT=1`, so gating would reproduce the bug under exactly + the conditions this fix targets. + """ + + @pytest.mark.parametrize("mode", ["2", "3"]) + def test__bypassed_fit__calls_both_save_results_hooks_with_paths_and_result( + self, monkeypatch, mode + ): + monkeypatch.setenv("PYAUTO_TEST_MODE", mode) + + analysis = _SaveResultsRecordingAnalysis() + + search = af.DynestyStatic( + name=f"bypass_save_results_{mode}", + unique_tag=f"bypass_save_results_{mode}_test", + ) + + result = search.fit(model=af.Model(af.m.MockClassx2), analysis=analysis) + + assert len(analysis.save_results_calls) == 1 + assert len(analysis.save_results_combined_calls) == 1 + + for paths, recorded_result in ( + analysis.save_results_calls[0], + analysis.save_results_combined_calls[0], + ): + assert paths is search.paths + assert recorded_result is result + + class _FitExceptionAnalysis(af.m.MockAnalysis): """Analysis whose likelihood always raises `FitException`, mimicking a pathological instance (non-PD inversion / NaN Delaunay mesh) that a real