Repository navigation
Description
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 = Falsekwarg toAggregator.from_directoryandadd_directory; the default behaviour is byte-for-byte unchanged. - When on, zips with no extracted sibling are extracted into one
tempfile.TemporaryDirectorywhose 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/GridSearchOutputcreated from the temp tree holds a reference to theTemporaryDirectoryobject, so the temp tree is removed when the last object pointing into it is garbage-collected (slices, filters and groups all shareSearchOutputinstances, so no state threading through the other constructors).Aggregatoralso 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_directorykwargs.
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
autofit/aggregator/aggregator.py—from_directory(cls, directory, completed_only=False, reference=None, unzip_temporary=False). In the innerscan(scan_directory)zip branch:- sibling
extractedexists →continue(unchanged); unzip_temporary→ lazily create one sharedtempfile.TemporaryDirectory(prefix="autofit_agg_"), extract toPath(tmp.name) / Path(root).relative_to(scan_root) / filename[:-4], do not append todirs, record the path intemp_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 withtemporary_directory=tmp. Keep theis_test_mode()sibling retry as is. Keep theBadZipFilere-raise.
- sibling
Aggregator: storeself._temporary_directory; addclose()(cleanup + setNone),__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.autofit/aggregator/search_output.py—AbstractSearchOutput.__init__(self, directory, reference=None, temporary_directory=None)storesself._temporary_directory;SearchOutput/GridSearchOutputforward it.test_autofit/aggregator/test_from_directory.py— reuse thezipped_directoryfixture; addtest_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.- 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/ innerscan(),add_directory,remove_unzippedautofit/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 -xManual 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_outputalways callspaths.zip_remove(): the zip is always written; the unzipped folder is deleted only whengeneral.yamloutput: remove_filesis true (forced true in HPC mode).Aggregator.from_directory→ innerscan()extracts every.zippermanently to a sibling<name>/, appends it to theos.walkdirslist, and skips extraction when the sibling exists (perf commitafa67edb6, 2026-07-16). Notempfile, 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
Activity
Jammy2211 commentedon Sep 9, 2026
Implemented — parked at the ship gate on Heart RED
Branch
feature/aggregator-temp-unzip, committed locally, not pushed:5b5545ba0feat(aggregator): opt-in temporary extraction for zipped search outputs57d838c38test(aggregator): cover grid searches under temporary extractionWhat 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.TemporaryExtractionusesweakref.finalizerather thantempfile.TemporaryDirectory, because the latter emits aResourceWarningon 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_searchfixture before, and the grid branch is code this change touches.Why it is parked
pyauto-heart readinessis RED for one reason: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
Merge pull request #1592 from PyAutoLabs/feature/aggregator-temp-unzip
Jammy2211 commentedon Sep 9, 2026
Shipped
PR: #1592 — MERGED, merge commit
c03fa3f30, head57d838c38Aggregator.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
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