Skip to content

feat(aggregator): opt-in temporary extraction for zipped search outputs (#1584) - #1592

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/aggregator-temp-unzip
Sep 9, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/aggregator-temp-unzip

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Aggregator.from_directory extracts every .zip it meets into a sibling directory beside the zip and leaves it there. Aggregating a results tree that holds only zips — an HPC run, or any run with output: remove_files — therefore doubles the disk and file count permanently, with no opt-out and no automatic cleanup (remove_unzipped() is opt-in and removes the parent of each search directory).

On a three-result zip-only tree the default scan grew the directory 1.85x in bytes and 10x in files.

unzip_temporary=True extracts into a temporary directory instead, mirroring the scanned layout so that same-named zips in different directories cannot collide. The scanned tree is left byte-identical. Every search output read out of the extraction holds a reference to it, so the files live exactly as long as something can still read them and are removed when the last such object is garbage collected; close() and the context manager remove them eagerly.

TemporaryExtraction uses weakref.finalize rather than tempfile.TemporaryDirectory, because the latter emits a ResourceWarning whenever it is cleaned up implicitly, and implicit cleanup is the normal path here.

The default is unchanged, including the short-cut that skips a zip whose extracted directory already exists. That short-cut still wins in temporary mode, so a tree that is already unpacked is never re-extracted.

Cost: the new mode re-extracts on every call rather than once — 0.33s vs 0.04s on a repeat call over a 200-result tree, against ~45 ms per result to parse samples. Extraction is not where aggregation time goes.

Not taken: reading members straight from the zip. That is ~20 path-based loader and enumeration call sites across file_output.py, search_output.py and aggregator.py, plus lazy per-query re-reads that would re-open the archive repeatedly and regress the 2026-07 aggregator speed-up (#1375 arc). Flipping the default to temporary extraction is left as a one-line follow-up.

API Changes

Purely additive. Aggregator.from_directory and add_directory gain an opt-in unzip_temporary keyword defaulting to False; Aggregator gains close() and the context-manager protocol; a new TemporaryExtraction class owns the extracted files. The search-output constructors gain an optional temporary_directory keyword. No symbol was removed or renamed, and no default changed, so every existing call behaves exactly as before.

See full details below.

Test Plan

  • Full PyAutoFit suite green: 2553 passed, 2 skipped
  • Temporary mode leaves the scanned tree byte-identical and writes no sibling directory
  • Temporary mode returns the same search outputs as the default mode
  • Extraction removed when the aggregator is released, and on close() / context-manager exit
  • An existing extracted sibling is still used, and no temporary directory is created
  • Two same-named zips in different directories do not collide
  • A search output kept after the aggregator is dropped can still be read
  • Grid searches pair with their children under temporary extraction
  • Existing zip tests (test_zip_extracted_and_loaded, test_zip_not_re_extracted) unchanged and green

To verify by hand, point the aggregator at a tree of .zip results and compare du -s before and after with and without the flag.

Full API Changes (for automation & release notes)

Added

  • autofit.aggregator.search_output.TemporaryExtraction — a temporary directory owning extracted zip contents; removed via weakref.finalize when the last referent is collected. Public attributes: path, is_alive, cleanup().
  • Aggregator.close() — remove the temporary extraction eagerly; a no-op when there is none.
  • Aggregator.__enter__ / Aggregator.__exit__ — context-manager protocol, exiting via close().

Changed Signature

  • Aggregator.from_directory(directory, completed_only=False, reference=None, unzip_temporary=False) — new trailing keyword, default preserves existing behaviour.
  • Aggregator.add_directory(directory, unzip_temporary=False) — same.
  • Aggregator.__init__(search_outputs, grid_search_outputs, temporary_directory=None) — new trailing keyword.
  • AbstractSearchOutput.__init__(directory, reference=None, temporary_directory=None) — new trailing keyword.
  • SearchOutput.__init__(directory, reference=None, temporary_directory=None) — new trailing keyword.

Changed Behaviour

  • None by default. With unzip_temporary=True, extraction targets a temporary directory instead of a sibling of the zip, and that directory is removed automatically.

Migration

  • None required. To adopt: Aggregator.from_directory(path, unzip_temporary=True), or with Aggregator.from_directory(path, unzip_temporary=True) as agg: to release the extraction at a known point.

Closes #1584

Generated by the PyAutoLabs agent workflow.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EwQS9x9Ls1NBUpbASL7n1s

Jammy2211 and others added 2 commits September 9, 2026 13:16
…ts (#1584)

`Aggregator.from_directory` extracts every `.zip` it meets into a sibling
directory beside the zip and leaves it there, so aggregating a results tree
that holds only zips (an HPC run, or any run with `output: remove_files`)
permanently doubles the disk and file count. On a three-result zip-only tree
the scan grew the directory 1.85x in bytes and 10x in files.

`unzip_temporary=True` extracts into a temporary directory instead, mirroring
the scanned layout so same-named zips in different directories cannot collide.
The scanned tree is left byte-identical. Every search output read out of the
extraction holds a reference to it, so the files live exactly as long as
something can still read them and are removed once the last such object is
collected; `close()` and the context manager remove them eagerly.

`TemporaryExtraction` uses `weakref.finalize` rather than
`tempfile.TemporaryDirectory` because the latter emits a `ResourceWarning`
whenever it is cleaned up implicitly, which is the normal path here.

The default is unchanged: extraction beside the zip, skipped when the
extracted directory already exists. That short-cut still wins in temporary
mode, so a tree that is already unpacked is never re-extracted. The cost of
the new mode is re-extracting on every call rather than once: 0.33s vs 0.04s
for a 200-result tree, against ~45ms per result to parse samples.

Reading members straight from the zip was assessed and not taken: ~20
path-based loader and enumeration call sites, plus lazy per-query re-reads
that would re-open the archive repeatedly and regress the 2026-07 aggregator
speed-up.

Full PyAutoFit suite green: 2552 passed, 2 skipped.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwQS9x9Ls1NBUpbASL7n1s
The grid-search branch of the scan gained the same ownership reference as the
search-output branch, and nothing exercised it: the aggregator tests carried no
`.is_grid_search` fixture at all.

The assertion that matters is `children`. Pairing a grid search with its child
searches goes through `is_relative_to` on their directories, which only holds
if both were extracted into the same mirrored temporary tree; a flat or
per-zip extraction would silently return a grid search with no children.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EwQS9x9Ls1NBUpbASL7n1s
@Jammy2211
Jammy2211 merged commit c03fa3f into main Sep 9, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/aggregator-temp-unzip branch September 9, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant