Skip to content

fix(aggregator): prefer a .completed zip over an incomplete sibling dir; preserve_in_zip leaves no loose copy #1601

Closed

Description

@Jammy2211

Overview

Aggregator.from_directory(root, completed_only=True) silently returned 5 of 20 searches on the pulled euclid_dr1_prelim RAL job 342398: lens_mass.csv came out with 5 of 10 rows and build_inspection_bundle.sh aborted with "aggregator is empty". The cause is a <hash>/ directory recreated beside a completed <hash>.zip by post-completion cache writes — it holds only the cached artifacts and no .completed, and the aggregator prefers it over the zip unconditionally. The same write path also leaves a loose copy of every preserved file outside the zip even when remove_files is set, which is what recreates the shadowing directory in the first place.

Plan

  • Make Aggregator.from_directory skip a zip only when its extracted sibling actually carries .completed; otherwise extract the zip (merging in place, or to the temporary path under unzip_temporary) and warn, naming the path.
  • Surface the shortfall: report how many outputs completed_only dropped in the aggregator's summary print, so a silent 5-of-20 becomes visible.
  • Make preserve_in_zip honour remove_files: once the member is in the archive, delete the loose file and prune the directories that emptied, so the zip really is the only store.
  • Rewrite the two tests that lock in the unconditional skip to the new contract, and add a preserve_in_zip test covering both remove_files settings.
  • File a follow-up so the two post-completion cache readers read the preserved zip member instead of recomputing once the loose copy is gone.
Detailed implementation plan

Affected Repositories

  • PyAutoFit (primary; sole repo edited)

Branch Survey

Repository Current Branch Dirty?
./PyAutoFit main clean

worktree_check_conflict aggregator-sibling-dir-zip PyAutoFit exits 1: PyAutoFit is claimed by the live task start-here-mode (worktree ~/Code/PyAutoLabs-wt/start-here-mode, branch feature/start-here-mode). That task's PyAutoFit diff is documentation only (docs/overview/natural_language.md, docs/overview/quick_start.md); this task edits autofit/aggregator/aggregator.py, autofit/non_linear/paths/abstract.py and two test modules. The file sets are disjoint, so this task takes its own worktree and own branch rather than serialising or folding, recorded as a parallel-claim: block on the active.md entry.

Suggested branch: feature/aggregator-sibling-dir-zip

Worktree root: ~/Code/PyAutoLabs-wt/aggregator-sibling-dir-zip/

Work classification: Library

Root cause

After a search is zipped and its output directory removed under hpc_mode (autofit/non_linear/paths/abstract.py:181-187 forces remove_files whenever general.hpc.hpc_mode is set), post-completion caches write back into paths._files_path: PyAutoLens Result._cached_multiple_image_positions_from (autolens/analysis/result.py:299-323) and the PyAutoGalaxy adapt-image SNR cache (autogalaxy/analysis/adapt_images/adapt_images.py:14-27,134-135). _files_path mkdirs on access (abstract.py:352-358), so a <hash>/files/ tree reappears beside the zip. The writers then call paths.preserve_in_zip(path) (abstract.py:383-423), which adds or replaces the archive member but leaves the loose file in place — correct under remove_files=False, wrong under remove_files=True.

Aggregator.from_directory (autofit/aggregator/aggregator.py:239-266) then skips any zip whose <hash>/ sibling exists (:243-245). That check sits before the unzip_temporary branch, so temporary-extraction mode has the same hole. The walk descends into the sibling, _is_search_output accepts it because files/search.json is present (:101-113 — itself a preserved member), and completed_only drops it for lacking .completed (:261-262), with nothing printed.

Implementation Steps

  1. autofit/aggregator/aggregator.py:243-245 — replace the unconditional if extracted.exists(): continue with a skip only when (extracted / ".completed").exists(). Otherwise extract: in place, extractall onto the existing directory (which merges; the zip's copies are authoritative); under extract_temporary, to the temporary path as today. Emit logger.warning naming the path — a sibling directory without .completed beside a completed zip, and that the zip was used.
  2. autofit/aggregator/aggregator.py:320-325 — count the outputs completed_only dropped and include it in the summary print.
  3. autofit/non_linear/paths/abstract.py:383-423 (preserve_in_zip) — after the member is written or replaced, if self.remove_files delete the loose file and prune now-empty parent directories up to and including output_path. Docstring records that under remove_files the zip is the only store and post-completion caches are recomputed on next access.
  4. test_autofit/aggregator/test_from_directory.py — rewrite test_zip_not_re_extracted (:42-49) and test_zip_temporary_uses_an_existing_extracted_directory (:110-116) to the new contract: a sibling with .completed still wins and the zip is not re-extracted; a sibling without .completed loses to the zip in both modes and completed_only=True returns 1.
  5. test_autofit/non_linear/paths/test_paths.py (beside test__preserve_in_zip__file_survives_restore, :120) — assert that under remove_files=True no loose file and no emptied directory remain after preserve_in_zip, and that the loose file does remain under remove_files=False.
  6. Follow-up prompt filed at PyAutoMind/draft/refactor/autolens/cache_readers_fall_back_to_zip_member.md (small, supervised; PyAutoLens + PyAutoGalaxy + PyAutoFit): the two cache readers should read the preserved zip member when the loose file is absent instead of recomputing — today one SNR-image set plus one point solve per stage boundary.

Key Files

  • autofit/aggregator/aggregator.py — the zip/sibling precedence and the summary print.
  • autofit/non_linear/paths/abstract.py — preserve_in_zip, _files_path, the hpc_mode → remove_files forcing.
  • test_autofit/aggregator/test_from_directory.py — the two tests that lock in the current skip.
  • test_autofit/non_linear/paths/test_paths.py — preserve_in_zip coverage.

Witness

An aggregator over <hash>.zip (containing .completed) plus a sibling <hash>/files/x.json with no .completed returns 1 output under completed_only=True in both in-place and unzip_temporary=True modes; after preserve_in_zip under remove_files=True no file remains outside the zip; Aggregator.from_directory over the pulled euclid_dr1_prelim 342398 tree returns 20 outputs (today 5).

Original Prompt

Click to expand starting prompt

Aggregator prefers an incomplete sibling directory over a .completed zip, and preserve_in_zip leaves a loose copy under remove_files

Type: bug
Target: PyAutoFit
Repos:

  • PyAutoFit
    Themes:
  • aggregator
  • paths
    Difficulty: medium
    Autonomy: safe
    Priority: high
    Status: formalised
    Consequence: judge
    Witness: an aggregator over <hash>.zip (containing .completed) plus a sibling <hash>/files/x.json with no .completed returns 1 output under completed_only=True in both in-place and unzip_temporary=True modes; after preserve_in_zip under remove_files=True no file remains outside the zip; Aggregator.from_directory over the pulled euclid_dr1_prelim 342398 tree returns 20 outputs (today 5).
    Review-minutes: 20
    Unattended: ready
    Filed: 2026-09-10

Symptom

Found 2026-09-10 on the euclid_dr1_prelim RAL job 342398. Aggregator.from_directory(root, completed_only=True)
returned 5 of the 20 searches present in the pulled output tree; the derived
lens_mass.csv carried 5 of 10 rows, and build_inspection_bundle.sh aborted
with "aggregator is empty". Nothing was printed to warn that fifteen completed
searches had been silently dropped.

Every dropped search had a <hash>/ directory sitting beside its <hash>.zip.
That directory held only two files — files/galaxy_images_snr.fits and
files/multiple_image_positions.json — whose mtimes matched the zip's, and both
of which were also members of the zip. Crucially the directory contained no
.completed marker, while the zip did.

Root cause (verified)

After a search is zipped and its output directory removed under hpc_mode
(@PyAutoFit/autofit/non_linear/paths/abstract.py:181-187 forces remove_files
whenever general.hpc.hpc_mode is set), post-completion caches write back into
paths._files_path:

  • PyAutoLens Result._cached_multiple_image_positions_from
    (@PyAutoLens/autolens/analysis/result.py:299-323)
  • the PyAutoGalaxy adapt-image SNR cache
    (@PyAutoGalaxy/autogalaxy/analysis/adapt_images/adapt_images.py:14-27,134-135)

The _files_path property mkdirs the directory on access
(@PyAutoFit/autofit/non_linear/paths/abstract.py:352-358), so a <hash>/files/
tree is recreated beside the zip. The writers then call
paths.preserve_in_zip(path)
(@PyAutoFit/autofit/non_linear/paths/abstract.py:383-423), which adds or
replaces the archive member but leaves the loose file exactly where it was —
correct under remove_files=False, wrong under remove_files=True, where the
zip is meant to be the only store.

Aggregator.from_directory
(@PyAutoFit/autofit/aggregator/aggregator.py:239-266) then skips any zip whose
<hash>/ sibling already exists (:243-245). That check sits before the
unzip_temporary branch, so temporary-extraction mode has the same hole. The
walk descends into the sibling, _is_search_output accepts it because
files/search.json is present (:101-113 — the sentinel is also a preserved
zip member), and completed_only then drops it for lacking .completed
(:261-262). No warning is printed anywhere along that path.

test_autofit/aggregator/test_from_directory.py::test_zip_not_re_extracted
(:42-49) currently locks in the unconditional skip, and
test_zip_temporary_uses_an_existing_extracted_directory (:110-116) is its
temporary-mode twin.

What to do

  1. aggregator.py:243-245 — skip the zip only when
    (extracted / ".completed").exists(). Otherwise extract the zip: in place,
    extractall onto the existing directory (which merges, and the zip's copies
    are authoritative); in temporary mode, to the temporary path as today. Emit a
    logger.warning naming the path — a sibling directory without .completed
    beside a completed zip, and that the zip was used. Add a count of outputs
    dropped by completed_only to the summary print at :320-325 so a silent
    shortfall becomes visible.

  2. preserve_in_zip — after the member is written or replaced, if
    self.remove_files is true delete the loose file and prune now-empty parent
    directories up to and including output_path. State in the docstring that
    under remove_files the zip is the only store, and that post-completion
    caches are recomputed on next access.

  3. Tests — rewrite test_zip_not_re_extracted and its temporary-mode twin
    to the new contract: a sibling with .completed still wins and the zip is
    not re-extracted; a sibling without .completed loses to the zip in both
    modes, and completed_only=True returns 1. Add a preserve_in_zip test
    beside test__preserve_in_zip__file_survives_restore
    (@PyAutoFit/test_autofit/non_linear/paths/test_paths.py:120) asserting that
    under remove_files=True no loose file and no empty directory remain, and
    that the loose file does remain under remove_files=False.

  4. Follow-up prompt — file
    PyAutoMind/draft/refactor/autolens/cache_readers_fall_back_to_zip_member.md
    (small, supervised; Repos: PyAutoLens, PyAutoGalaxy, PyAutoFit): the two
    cache readers should read the preserved zip member when the loose file is
    absent rather than recomputing it. Today that costs one SNR-image set plus
    one point solve per stage boundary.

Activity

  1. Jammy2211 commented on Sep 10, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created — #1602 (pending-release)

    Branch feature/aggregator-sibling-dir-zip, commit d6fa9cdc8.

    Witness met. Aggregator.from_directory(".../euclid_dr1_prelim/output/dr1_prelim_grade_ab", completed_only=True, unzip_temporary=True) returns 20 on the branch and 5 on main. Fifteen warnings are logged, one per shadowed search; the science tree is untouched.

    Tests. pytest test_autofit -q -p no:cacheprovider (serial) — 2567 passed, 2 skipped, 0 failed. On main in the same worktree: 5 failed, 2557 passed, 2 skipped.

    A third fix, beyond the plan. Those five test_aggregate_csv failures are a genuine library bug, not the environment difference hypothesised in bug/autofit/aggregate_csv_tests_fail_locally_pass_in_ci.md. scan collected outputs in raw os.walk order — hash order on ext4, insertion order on tmpfs — so the fixture searches came back fit_2, fit_1 locally and fit_1, fit_2 on CI, and everything that pairs a row to an output by index (the summary CSV writers, add_label_column) paired them differently per machine. The collected lists are now sorted by path before they are returned, with a regression test that monkeypatches os.walk to reverse its entries. That prompt can be closed against this PR.

    Downstream workspace impact: none. Every consumer already calls the unchanged signature Aggregator.from_directory(directory=..., completed_only=True, unzip_temporary=True) — euclid_strong_lens_modeling_pipeline/workflow/csv_make.py:91 and catalogue/scripts/{lens_mass,lens_sersic,source_sersic,magnitudes,deblending,multi_wavelength}.py. They pick the fix up without an edit.

    Follow-up filed. PyAutoMind/draft/refactor/autolens/cache_readers_fall_back_to_zip_member.md — the two post-completion cache readers should read the preserved zip member instead of recomputing now that the loose copy is gone.

    Heart: YELLOW, no RED. Four reasons, all organism-scope and none touched by this branch (workspace validation not passing, three profiling-drift rows) — the same set acknowledged in-session earlier today for a100-pixelized-baseline.

    Next: CI, then /prm 1602.

  2. Jammy2211 commented on Sep 10, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    PyAutoFit#1602 merged — #1602 (merge e354dbb6).

    • Aggregator.from_directory now prefers a <hash>.zip carrying .completed over an incomplete <hash>/ sibling, warns when it does, and reports the completed_only drop count.
    • preserve_in_zip removes the loose copy and prunes empty directories under remove_files, so nothing is left outside the zip.
    • Search outputs are sorted by path — the real cause of the five test_aggregate_csv failures that reproduced locally while CI stayed green.

    Witness met: the pulled euclid_dr1_prelim 342398 tree aggregates 20 outputs (5 on main). CI green on docs-build, unittest 3.12, unittest 3.13, unittest-nojax.

    Pending release: the fix is on main but not yet in a published PyAutoFit.

    Follow-up filed: draft/refactor/autolens/cache_readers_fall_back_to_zip_member.md (the post-completion caches in PyAutoLens Result._cached_multiple_image_positions_from and the PyAutoGalaxy adapt-image SNR cache write through paths._files_path, whose property mkdirs beside the zip — the sibling dirs this fix now tolerates).

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