Skip to content

fix: test-mode bypass never calls analysis.save_results #1624

Closed

Description

@Jammy2211

Overview

AbstractSearch._fit_bypass_test_mode (autofit/non_linear/search/abstract_search.py:1091) is the sampler bypass used by PYAUTO_TEST_MODE=2 and =3. Its docstring promises it "writes all expected output files so that downstream code sees a complete result folder", and it does write samples, samples_summary and the .completed marker — but it never calls analysis.save_results / analysis.save_results_combined, which the normal path (start_resume_fit, lines 830-831) calls unconditionally.

Any script that defines a custom Analysis.save_results and reads its own output file back therefore fails under smoke. Heart workspace-smoke cloud run 34824535982 fails autofit_workspace overview/overview_2_scientific_workflow (.py and .ipynb) with FileNotFoundError: .../files/science_summary.json at overview_2_scientific_workflow.py:279. The gap predates the recent PyAutoFit commits — it was introduced with modes 2/3 in d41aa53e8.

Fixing it clears both autofit smoke failures with no workspace edit and no API change.

Plan

  • _fit_bypass_test_mode calls analysis.save_results and analysis.save_results_combined after building the result and before paths.completed(), matching start_resume_fit.
  • Add a unit test asserting a bypassed fit (mode 2 and mode 3) invokes both hooks with the result and paths.
  • Verify the failing workspace script passes under PYAUTO_TEST_MODE=2 locally; no workspace edits.
  • Ship via ship_library; downstream impact: autofit_workspace overview_2 smoke goes green, no API change.
Detailed implementation plan

Work Classification

Library

Worktree root

~/Code/PyAutoLabs-wt/test-mode-bypass-save-results/

Affected Repositories

  • PyAutoFit (primary — the only repo edited)
  • autofit_workspace (downstream impact target, not edited — its overview_2 smoke is the witness)

Branch Survey

Repository Current Branch Dirty?
./PyAutoFit main clean
./autofit_workspace main clean

worktree_check_conflict test-mode-bypass-save-results PyAutoFit exits 0 (no conflict). No worktree or *bypass* branch exists, and no claim is registered on either repo.

Suggested branch: feature/test-mode-bypass-save-results

Implementation Steps

  1. autofit/non_linear/search/abstract_search.py — in _fit_bypass_test_mode (def at line 1091), insert between the make_result(...) call ending at line 1204 and model.unfreeze() at line 1206:

    analysis.save_results(paths=self.paths, result=result)
    analysis.save_results_combined(paths=self.paths, result=result)

    byte-identical to lines 830-831 in start_resume_fit, same position relative to make_result / model.unfreeze() / self.paths.completed(). Do not gate the new calls on skip_fit_output(): the normal path does not, and the smoke profile sets PYAUTO_SKIP_FIT_OUTPUT=1 — gating them would reproduce the bug under exactly the conditions the fix targets.

  2. test_autofit/non_linear/search/test_abstract_search.py — add class TestBypassCallsSaveResults parametrised over PYAUTO_TEST_MODE "2" and "3", mirroring TestBypassWritesCompleted (line 379), TestBypassToleratesFitException (435) and the mode-parametrised TestBypassToleratesAssertionTies (516). Pattern: monkeypatch.setenv("PYAUTO_TEST_MODE", mode), af.Model(af.m.MockClassx2), af.DynestyStatic(name=..., unique_tag=...), a local af.m.MockAnalysis subclass (the _FitExceptionAnalysis idiom at 426) that records paths and result from overridden save_results / save_results_combined. Assert on the recorder, not on files (test config runs remove_files: true).

  3. Verify: python3 -m pytest test_autofit/non_linear/search/test_abstract_search.py -k bypass -q, then the full suite (testpaths = ["test_autofit"]; CI runs pytest via PyAutoHeart's reusable lib-tests.yml), then the witness from the autofit_workspace root:

    PYAUTO_TEST_MODE=2 PYAUTO_SKIP_FIT_OUTPUT=1 PYAUTO_SKIP_VISUALIZATION=1 PYAUTO_SKIP_CHECKS=1 PYAUTO_DISABLE_JAX=1 JAX_ENABLE_X64=True NUMBA_CACHE_DIR=/tmp/numba_cache MPLCONFIGDIR=/tmp/matplotlib python3 scripts/overview/overview_2_scientific_workflow.py

    and again with PYAUTO_TEST_MODE=3.

Why this is safe

  • Analysis.save_results (autofit/non_linear/analysis/analysis.py:239) and save_results_combined (:242) are bare no-ops; nothing gates them on test mode.
  • save_results_combined has no override anywhere in the stack; its only references are the base def and the call sites at 831 and 1058.
  • Under mode 3 the stored parameter vector is a valid representative point, so result.max_log_likelihood_instance reconstructs; the -1.0e99 sentinel is only the likelihood value.
  • Downstream overrides (PyAutoGalaxy/autogalaxy/analysis/analysis/dataset.py:157, PyAutoLens/autolens/analysis/analysis/dataset.py:170) are cheap JSON dumps already guarded per PyAutoFit fix: Result.instance falls back to a valid sample; persist samples before materialisation #1535.

Key Files

  • autofit/non_linear/search/abstract_search.py — _fit_bypass_test_mode (1091), insertion 1204→1206; reference start_resume_fit 823-835
  • autofit/non_linear/analysis/analysis.py — base hooks 239 / 242
  • test_autofit/non_linear/search/test_abstract_search.py — new test beside the bypass classes
  • autofit/non_linear/mock/mock_analysis.py:18 — MockAnalysis

Downstream impact

No API change. autofit_workspace overview/overview_2_scientific_workflow.{py,ipynb} go green in Heart workspace-smoke. Note overview_2 is absent from autofit_workspace/smoke_tests.txt, so the list-driven local smoke does not cover it; adding it is a separate workspace task.

Original Prompt

Click to expand starting prompt

Test-mode bypass never calls analysis.save_results, breaking scripts that read their own result files

Type: bug
Target: autofit
Repos:

  • PyAutoFit
    Themes:
  • test-mode
  • workspace-smoke
    Difficulty: easy
    Autonomy: supervised
    Priority: high
    Status: draft
    Consequence: judge
    Witness: PYAUTO_TEST_MODE=2 python autofit_workspace/scripts/overview/overview_2_scientific_workflow.py completes and prints the science_summary it wrote; a PyAutoFit unit test asserts the bypass calls save_results / save_results_combined
    Review-minutes: 10
    Filed: 2026-09-14

User request (verbatim, 2026-09-14):

"""
can you fix the PyAutoHeart being red
"""
(follow-up after the RED cleared to YELLOW: "yes do next step" — file the PyAutoFit
bypass fix through start_dev, which also clears both autofit smoke failures.)

Context

Heart workspace-smoke cloud run 34824535982 (PyAutoHeart, workspace-smoke.yml,
PYAUTO_TEST_MODE=2) fails on autofit_workspace overview/overview_2_scientific_workflow
(.py and .ipynb) with

FileNotFoundError: .../files/science_summary.json
  overview_2_scientific_workflow.py:279  print(search.paths.load_json("science_summary"))

The script defines Analysis.save_results to paths.save_json("science_summary", …)
and reads it back after the fit (autofit_workspace 424555d, 2026-09-09).

AbstractSearch._fit_bypass_test_mode (autofit/non_linear/search/abstract_search.py,
modes 2 and 3) writes samples + samples_summary, builds the result and calls
paths.completed(), but never calls analysis.save_results / save_results_combined,
unlike the normal path (start_resume_fit, ~line 829). Its docstring promises
"all expected output files so that downstream code sees a complete result folder".
Any workspace script that round-trips a custom save_results file therefore fails
under smoke. The gap predates the recent PyAutoFit commits (introduced with modes 2/3
in d41aa53).

Downstream save_results implementations (autogalaxy galaxies.json, autolens
tracer.json) are cheap JSON dumps already guarded against instance-build failures
(PyAutoFit #1535), so calling them from the bypass is safe in both modes.

Activity

  1. Jammy2211 commented on Sep 14, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created

    PR: #1626 (feature/test-mode-bypass-save-results, commit 461ac836c, label pending-release)

    _fit_bypass_test_mode now calls analysis.save_results and analysis.save_results_combined between make_result and paths.completed(), matching start_resume_fit and ungated by skip_fit_output() — the smoke profile sets PYAUTO_SKIP_FIT_OUTPUT=1, so gating them would reproduce the bug under exactly the conditions the fix targets. Two source lines plus their comment, and a new parametrised test.

    Tests

    • pytest test_autofit/non_linear/search/test_abstract_search.py -k bypass → 18 passed, 20 deselected.
    • pytest test_autofit (full suite) → 2850 passed, 2 skipped, 255s.
    • Control on the new test: with the source change stashed, TestBypassCallsSaveResults fails in both modes 2 and 3 — it is a real falsifier, not a passing bystander.

    Witness — the script Heart fails on, run from the canonical autofit_workspace root with this branch's autofit on PYTHONPATH (verified via autofit.__file__), output cleared between runs so the .completed marker could not shortcut the bypass:

    • Control, canonical PyAutoFit main, PYAUTO_TEST_MODE=2 → reproduces FileNotFoundError: .../files/science_summary.json at overview_2_scientific_workflow.py:279.
    • Branch, PYAUTO_TEST_MODE=2 → exit 0, line 279 prints the summary it wrote.
    • Branch, PYAUTO_TEST_MODE=3 → exit 0, same print.

    Workspace impact: option (iii) — no workspace work needed. API Changes are None — internal changes only. Three autofit_workspace scripts define a custom save_results (overview/overview_2_scientific_workflow.py, cookbooks/analysis.py, cookbooks/result.py); only overview_2 round-trips its own file, which is why it is the only smoke failure. Expected Heart effect: both autofit entries (.py and .ipynb) go green in workspace-smoke, clearing 2 of the 3 workspace-validation failures in the current YELLOW (the third, autolens notebooks/imaging/slam.ipynb, is unrelated). Separate follow-up: overview_2 is absent from autofit_workspace/smoke_tests.txt, so only Heart's cloud run covers it.

    Readiness: Heart is YELLOW with no RED reasons, the same set acknowledged at approval and recorded as heart-ack: on the active.md row — workspace validation not passing (3 failed, cloud#34824535982); manifest drift x2; profiling drift x6; release validation incomplete.

    Next: merge stays human — run /prm once CI is green.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01RbJGue5FwUPbxvCFKwXNam

  2. Jammy2211 commented on Sep 14, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    Fixed in PyAutoFit#1626, merged as 559699fb1531ffe2205ae3b824bd29aadb39c145.

    AbstractSearch._fit_bypass_test_mode 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
    (start_resume_fit), and deliberately ungated by skip_fit_output() — the
    normal path does not gate them, and the smoke profile that exposed the bug sets
    PYAUTO_SKIP_FIT_OUTPUT=1.

    Two files, +72/-0: autofit/non_linear/search/abstract_search.py and
    test_autofit/non_linear/search/test_abstract_search.py.

    Evidence

    • New TestBypassCallsSaveResults, parametrised over PYAUTO_TEST_MODE "2" and "3"; with the source change stashed it fails in both modes (real falsifier). -k bypass → 18 passed.
    • Witness, from the canonical autofit_workspace root: canonical PyAutoFit main reproduces FileNotFoundError: .../files/science_summary.json at overview_2_scientific_workflow.py:279; this branch exits 0 and prints the science_summary it wrote under modes 2 and 3.
    • CI green on every leg: unittest (3.12), unittest (3.13), unittest-nojax, docs-build.

    Workspace impact: none (option (iii)) — no API change, no workspace edit. Expected Heart effect: the two autofit overview/overview_2_scientific_workflow.{py,ipynb} workspace-smoke failures clear.

    Note for a separate task: overview_2 is absent from autofit_workspace/smoke_tests.txt, so only Heart's cloud run covers it.

    PyAutoFit is pending-release — merged is not published; a release build clears that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions