Skip to content

feat(aggregator): temporary-directory unzip mode so scraping .zip results does not double disk usage #1584

Closed

Description

@Jammy2211

Overview

Aggregator.from_directory extracts every <name>.zip it meets permanently to a sibling <name>/ folder, so aggregating a results tree that holds only zips (HPC / remove_files: true runs) doubles the disk and file count forever. There is no opt-out and no automatic cleanup (remove_unzipped() is opt-in and blunt). Reading members straight from the zip was assessed and rejected for now (~20 path-based loader call sites plus lazy per-query re-reads that would regress the July 2026 aggregator speed-up, #1375 arc). This task adds the "cheaper middle ground": an opt-in mode that extracts into a temporary directory and cleans it up automatically.

Plan

  • Add an opt-in unzip_temporary: bool = False kwarg to Aggregator.from_directory and add_directory; the default behaviour is byte-for-byte unchanged.
  • When on, zips with no extracted sibling are extracted into one tempfile.TemporaryDirectory whose layout mirrors the scanned tree; that tree is scanned for search outputs exactly as the main tree is. An existing extracted sibling still wins (no re-extraction).
  • Lifetime: every SearchOutput / GridSearchOutput created from the temp tree holds a reference to the TemporaryDirectory object, so the temp tree is removed when the last object pointing into it is garbage-collected (slices, filters and groups all share SearchOutput instances, so no state threading through the other constructors). Aggregator also gains __enter__/__exit__ + close() for eager cleanup.
  • Tests: no sibling written, identical outputs to the default mode, cleanup on release and on close(), existing-sibling short-cut.
  • Docstrings only; workspace cookbook note only if it already documents the from_directory kwargs.
Detailed implementation plan

Work Classification

Library

Affected Repositories

  • PyAutoFit (primary)

Branch Survey

Repository Current Branch Dirty?
./PyAutoFit main clean

Suggested branch: feature/aggregator-temp-unzip
Worktree root: ~/Code/PyAutoLabs-wt/aggregator-temp-unzip/

Blocked by: aggregator-search-json-sentinel (#1582) is rewriting the same inner scan() loop in autofit/aggregator/aggregator.py (metadata → search.json sentinel). Start once that PR merges and build on the new sentinel.

Implementation Steps

  1. autofit/aggregator/aggregator.py — from_directory(cls, directory, completed_only=False, reference=None, unzip_temporary=False). In the inner scan(scan_directory) zip branch:
    • sibling extracted exists → continue (unchanged);
    • unzip_temporary → lazily create one shared tempfile.TemporaryDirectory(prefix="autofit_agg_"), extract to Path(tmp.name) / Path(root).relative_to(scan_root) / filename[:-4], do not append to dirs, record the path in temp_roots;
    • else → today's extract-alongside + dirs.append (unchanged).
      After the walk, scan(temp_root) for each recorded root (recursion covers nested zips) and extend the result lists; construct those outputs with temporary_directory=tmp. Keep the is_test_mode() sibling retry as is. Keep the BadZipFile re-raise.
  2. Aggregator: store self._temporary_directory; add close() (cleanup + set None), __enter__ (return self), __exit__ (close). add_directory(directory, unzip_temporary=False) passes the kwarg through. remove_unzipped() docstring notes it applies to the default mode only.
  3. autofit/aggregator/search_output.py — AbstractSearchOutput.__init__(self, directory, reference=None, temporary_directory=None) stores self._temporary_directory; SearchOutput / GridSearchOutput forward it.
  4. test_autofit/aggregator/test_from_directory.py — reuse the zipped_directory fixture; add test_zip_temporary_no_sibling_written, test_zip_temporary_same_outputs, test_zip_temporary_cleanup_on_release (del agg + gc.collect()), test_zip_temporary_close (context manager), test_zip_temporary_existing_sibling_wins. Existing zip tests stay untouched and green.
  5. Out of scope, stated in the PR: flipping the default; reading members directly from the zip; database scrape (autofit/database/aggregator/scrape.py) and MCP tools keep the default.

Key Files

  • autofit/aggregator/aggregator.py — from_directory / inner scan(), add_directory, remove_unzipped
  • autofit/aggregator/search_output.py — AbstractSearchOutput.__init__
  • test_autofit/aggregator/test_from_directory.py — zip fixtures and tests

Verification

pytest test_autofit/aggregator -q
pytest test_autofit -q -x

Manual witness: Aggregator.from_directory(<dir>, unzip_temporary=True) on a zip-only results tree leaves no new folders, len(agg) / values("samples") match the default mode, and the autofit_agg_* temp tree is gone after del agg.

Original Prompt

Click to expand starting prompt

Prompt: PyAutoMind/draft/feature/autofit/aggregator_temporary_unzip.md

The aggregator scrapes directories and enables things like .fits, .csv generation, and a while back we did profiling to speed it up. My understanding is that when it runs, it automatically unzips the .zip files results are stored in, which then doubles the amount of files and hard disk space in use, but I could be wrong on that. Is it possible for it to not unzip .zip files and still do all catalogue operations? Would this lead to slower performance?

Ok lets go with the cheaper middle ground

Findings (2026-09-08 survey):

  • AbstractSearch.post_fit_output always calls paths.zip_remove(): the zip is always written; the unzipped folder is deleted only when general.yaml output: remove_files is true (forced true in HPC mode).
  • Aggregator.from_directory → inner scan() extracts every .zip permanently to a sibling <name>/, appends it to the os.walk dirs list, and skips extraction when the sibling exists (perf commit afa67edb6, 2026-07-16). No tempfile, no cleanup, no opt-out kwarg.
  • Aggregator.remove_unzipped() is the only cleanup: opt-in, never called internally, rmtrees the parent of each search directory.
  • Reading straight from the zip rejected for now: ~20 path-based loader/enumeration call sites across file_output.py, search_output.py, aggregator.py, plus lazy per-query re-reads.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EwQS9x9Ls1NBUpbASL7n1s

Activity

  1. Jammy2211 commented on Sep 9, 2026

    @Jammy2211
    CollaboratorAuthor

    Implemented — parked at the ship gate on Heart RED

    Branch feature/aggregator-temp-unzip, committed locally, not pushed:

    • 5b5545ba0 feat(aggregator): opt-in temporary extraction for zipped search outputs
    • 57d838c38 test(aggregator): cover grid searches under temporary extraction

    What shipped

    Aggregator.from_directory(..., unzip_temporary=True) extracts zips into a temporary directory mirroring the scanned layout instead of a permanent sibling folder. Ownership rides on the search outputs: each holds a reference to the extraction, so the files live exactly as long as something can read them and go when the last one is collected. close() and the context manager remove them eagerly. The default is unchanged, and the existing-sibling short-cut still wins in temporary mode.

    TemporaryExtraction uses weakref.finalize rather than tempfile.TemporaryDirectory, because the latter emits a ResourceWarning on implicit cleanup and implicit cleanup is the normal path here.

    Measured

    On a three-result zip-only tree, default mode grew the directory 1.85x in bytes and 10x in files; temporary mode left it byte-identical and removed the extraction on release.

    The cost is re-extracting per call instead of once. At 200 results: 0.33s vs 0.04s on a repeat call, against ~45 ms per result to parse samples, so it is small next to the catalogue work itself.

    Tests

    Full PyAutoFit suite green: 2553 passed, 2 skipped. Seven new tests plus a grid-search regression test; the aggregator tests carried no .is_grid_search fixture before, and the grid branch is code this change touches.

    Why it is parked

    pyauto-heart readiness is RED for one reason:

    release validation FAILED (stage integrate)

    That is organism-scope, from the PyAutoHeart Release Integrate run of 2026-09-09T07:23Z; nothing in this branch is in the release chain. Acknowledging it is the human's call, so nothing has been pushed and no PR is open. On acknowledgement: push the branch, open the PR, then /prm.

    Not taken

    Flipping the default to temporary extraction (a one-line follow-up if wanted), and reading members straight from the zip.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01EwQS9x9Ls1NBUpbASL7n1s

  2. added a commit that references this issue on Sep 9, 2026
  3. Jammy2211 commented on Sep 9, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    PR: #1592 — MERGED, merge commit c03fa3f30, head 57d838c38

    Aggregator.from_directory(..., unzip_temporary=True) extracts zipped search outputs into a temporary directory mirroring the scanned layout, instead of a permanent sibling beside each zip. Ownership rides on the search outputs, so the files live exactly as long as something can still read them; close() and the context manager remove them eagerly. The default is unchanged, and the existing-sibling short-cut still wins in the new mode.

    Measured

    bytes files
    Default mode 1.85x 10x
    Temporary mode unchanged unchanged

    Repeat-call cost of the new mode is 0.33s against 0.04s at 200 results, versus roughly 45 ms per result to parse samples.

    CI

    Every leg green on 57d838c38: unittest (3.12), unittest (3.13), unittest-nojax, docs-build.
    Full PyAutoFit suite locally: 2553 passed, 2 skipped.

    Gate

    Heart was RED on one organism-scope reason, "release validation FAILED (stage integrate)", from the Release Integrate run of 2026-09-09T07:23Z. Nothing in this branch is in the release chain. Acknowledged in-session by the human. No freeze window was open.

    Pending release: this is merged, not published. It reaches users on the next PyAutoFit release.

    Follow-up left open

    Flipping the default to temporary extraction is a one-line change, deliberately not taken here.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_01EwQS9x9Ls1NBUpbASL7n1s

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