Skip to content

B1: Store and folder inputs - #156

Open
Claptar wants to merge 7 commits into
mainfrom
fix/b1
Open

Claptar wants to merge 7 commits into
mainfrom
fix/b1

Conversation

@Claptar

@Claptar Claptar commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

Part of #149 (wave 1, released as 0.17.11 with B2 to B6).

Closes #135.

What a store or a folder of outputs holds outside itself no longer reaches an output, and convert checks every input, and its disk, before it writes.

What changes

Ref Fix Where
F166 convert runs the HDF5 guard on the source and on --raw before any sniffer opens them to tell their format. A source, or a file of a folder source where an HDF5 file is read, that is no regular file (a pipe) is refused there too, rather than waited on. commands/convert.py, storage/__init__.py
F167 Every input of convert, --raw included, goes to the output check, under any alias (hard link, symlink, case). commands/convert.py
F168 A folder source (Space Ranger outs/, Xenium bundle, MTX folder, Parse run), or a Visium or Xenium matrix named on its own, is checked for free space, estimated from the files its converter's plan reads (matrix and tables, not images and BAMs), plus what Xenium unpacks beside the output. A Zarr store kept as links is measured by what they point at. commands/convert.py
F169 Xenium's plan only lists analysis.tar.gz; the tables are unpacked inside staging, read and checked there, and removed in a finally, so a refusal unpacks nothing and a failure leaves nothing, os._exit or not. conversion/xenium.py
F170, F171 The HDF5 guard's remaining gaps are closed, and what it reads is bounded. Details in the tests and, once released, the changelog. storage/__init__.py
F172 copy_path (import's copy of a Zarr store) reads a link through only where zarr reads a file, judged from each array's metadata; any other link is copied as a link; a link to a folder is refused, never followed; the codec check no longer walks into linked folders. storage/__init__.py
F173 The "inside a Zarr input" refusal is factored out of check_output (check_not_input), covers a --to 10x-mtx folder, and judges the output's folders as the filesystem resolves them, so a link into the store is caught too. util/path.py, commands/convert.py

Other finalizer-based cleanups were audited with F169: Xenium's TemporaryDirectory was the only disk cleanup left to a finalizer. Every scratch file (scratch_group, concat's buckets, RowScatter) is removed in a finally or a context manager, which run before _leave. The other weakref.finalize only frees memory (the shard index cache).

Zarr links: the decision and its trade-off

Agreed on 2026-12-04: links are read through as anndata reads them, because datalad, git-annex and DVC keep every file of a store as a link into an object store outside it. So:

  • Reading elements (convert, subset, export, ...) still follows links, wherever they point, as zarr's LocalStore does for anndata. A chunk linked to a file outside the store is read through, by design: the output holds what anndata reads. test_convert_reads_a_chunk_through_its_link_as_anndata_does checks that against anndata.
  • import's copy (copy_path) copies a link as the file it points at only where zarr reads a file: among the files of the nodes zarr reaches going down the store's groups from its root, in the store's own Zarr version, with metadata zarr could read. Those are each node's metadata (zarr.json; in v2 .zarray or .zgroup, .zattrs, and the root's .zmetadata) and each array's chunks, under the key its chunk key encoding gives a chunk of its grid (v2 0.0 or 0/0; v3 c/0/0, c.0.0, or v2-style keys; 0-d keys too). Any other link is copied as the link it is, whatever it is named and whatever metadata sits beside it: in.zarr/stolen, uns/notes.txt, X/README, a key past the grid or of another rank or encoding, .zattrs in a folder that is no node, or metadata planted inside an array, below a folder that is no group, or of the other version. So a file outside the store never becomes part of the output. A link among the chunks of an array whose metadata names a chunk grid or key encoding zarr does not have is refused rather than guessed at.
  • A link to a folder is refused by the copy, inside the store or not: following it can lead out of the store (a link to / copied the disk). datalad, git-annex and DVC link files, not folders, so their stores copy as before (test_a_store_kept_as_links_is_copied_as_the_store_they_link_to, v2 and v3, read back by anndata).
  • Where zarr reads a file, a link to nothing (content git-annex never fetched) is refused, since zarr would read the missing chunk as fill values. So is a link to anything but a regular file, and a store entry that is neither a file nor a folder (a FIFO).
  • Cost: nothing new at open, so no walk of every chunk file on Lustre. The copy still goes through shutil.copytree and its own copy2, so a file that is no link costs what it did (3.1 stat calls per file on main and here, counted on a 204-file store); the checks add one more listing of each folder and look through links only. Each array's metadata is read only if a link sits among its chunks. B0's codec checks stay as they were, before and after the copy (tests/test_zarr_safety.py is untouched and green); check_zarr_store no longer follows linked folders, since the copy refuses them.

What remains: a crafted store whose chunk or metadata link points at a private file still has that file read through, by convert as by anndata, and copied by import. That is the agreed read-through, and leaks only if the output is sent back.

Changelog entry (for the 0.17.11 release PR)

Fixed

  • convert checks every input before it reads one, and refuses an output that is any of them (#135). It used to open a file to tell its format before checking it for data held in another file, so an external link to a FIFO hung the run; a pipe given as the source, or where a folder source keeps its matrix, is now refused too. It also checked the output against the source alone, so -o naming CellBender's --raw file replaced the raw matrix it was reading, exit 0. A --to 10x-mtx folder inside a Zarr input was written among its chunks, and any output reaching into a Zarr input through a link among its folders was written there, one replacing an array of the input with a whole store; both are refused, as every other output inside an input is.
  • Folder sources are checked for free space (#135). A Space Ranger outs/, a Xenium bundle, an MTX folder or a Parse run converted to .h5ad was written however full the disk was, and a full disk can stop an HDF5 write inside the library, where nothing cleans up. The estimate is the files the conversion reads, its matrix and tables, not the images and BAMs beside them, for a Visium or Xenium matrix named on its own too. A Zarr store kept as links (datalad, git-annex, DVC) was measured by the links' own few bytes; it is measured by the files they point at.
  • Xenium's analysis.tar.gz is unpacked only while writing, and always removed (#135). It was unpacked while planning, before the output was checked, and a refused or failed run left .adata-xenium-* beside the output.
  • The HDF5 guard's remaining gaps are closed (#135). Some HDF5 references escaped the checks added in 0.17.5, and a crafted file could make the check itself take gigabytes of memory, in ls and view too.
  • import copies a Zarr store's links as zarr reads them (#135). It copied with every link followed, so a link to a file outside the store, part of no element, came out in the output as a copy of that file, and a link to / copied the disk. A link zarr reads through, such as a chunk of a datalad, git-annex or DVC store, is still copied as the file it points at; any other link stays a link, whatever it is named; a link to a folder is refused.

Tests

Each finding has a regression test whose docstring starts with its ref, checked against anndata, scipy, zarr or h5py where there is an output. The tests of the first commits were run against unmodified main: 44 fail there. The 8 that pass there test behaviour the links decision keeps: a link store copied and read back by anndata (v2, v3), chunk links read through in all five key encodings, and convert reading a chunk through its link. The 14 tests added for the second review fail on the commit before them.

Ref Tests
F166 test_convert.py::test_an_external_link_is_refused_before_a_sniffer_follows_it, ::test_a_pipe_where_an_hdf5_file_is_read_is_refused (FIFOs, run as the command, killed if it hangs)
F167 test_cellbender.py::test_an_output_naming_the_raw_input_is_refused (itself, hard link, symlink; the raw counts checked with scipy)
F168 test_convert.py::test_a_folder_source_is_checked_for_free_space (Visium, Xenium, MTX, Parse; a BAM beside them is not counted; X read back by anndata against each fixture's counts), ::test_a_store_kept_as_links_is_measured_by_what_they_point_at, ::test_the_estimate_walks_no_list_of_names; test_xenium.py::test_a_bundle_named_by_its_matrix_is_measured_as_a_whole
F169 test_xenium.py::test_a_refused_conversion_unpacks_nothing, ::test_a_conversion_failing_after_the_unpack_leaves_nothing (both run as the command, so through os._exit), ::test_the_free_space_check_counts_the_tables_an_archive_unpacks (sizes from tarfile; read back by anndata)
F170 test_storage.py, the two tests whose docstrings start with F170
F171 test_storage.py, the tests whose docstrings start with F171 (one a perf memory guard, run as the audit measured it)
F172 test_storage.py::test_a_link_that_is_no_file_of_an_element_stays_a_link_in_a_copy, ::test_a_link_named_like_a_file_zarr_reads_elsewhere_stays_a_link (8 names), ::test_metadata_planted_where_zarr_reaches_no_node_reads_no_link_through (11 cases), ::test_a_link_to_a_folder_is_refused_before_the_copy_walks_it, ::test_a_chunk_linked_to_nothing_is_refused_by_a_copy, ::test_a_link_among_an_array_s_chunks_is_refused_when_its_keys_cannot_be_told; what stays: ::test_a_store_kept_as_links_is_copied_as_the_store_they_link_to, ::test_chunks_linked_out_are_read_through_in_every_key_encoding (5 encodings, against zarr), ::test_convert_reads_a_chunk_through_its_link_as_anndata_does
F173 test_mtx.py::test_a_folder_output_inside_the_zarr_input_is_refused (Zarr v2 and v3), ::test_an_output_through_a_link_into_the_zarr_input_is_refused (an MTX folder, an h5ad, and over one of the input's own arrays), ::test_an_output_beside_the_zarr_input_named_through_it_is_written (-o in.zarr/../out.h5ad, refused before by name); the store read back by anndata

One existing test changed: test_xenium.py::test_an_archive_that_cannot_be_unpacked_beside_the_output_is_refused_by_name (F112). The unpack now happens inside staging, so it patches tempfile.mkdtemp, and the output's folder, made by staging, is left empty rather than not made.

Review

An independent subagent reviewed the diff twice, from the code alone, and reran its own triggers against each round of fixes. What it found, and what came of it:

  • Fixed, with tests: an output reaching into a Zarr input through a symlinked folder (it overwrote one of the input's arrays); links with chunk-like names outside the grid, of another rank or encoding, or beside metadata planted where zarr reaches no node, copied as files; a Visium or Xenium matrix named on its own, and a store kept as links, under-measured for free space; further bounds on what the HDF5 guard reads from a crafted file; a pipe where a folder source keeps its matrix hanging the sniffer; extra stat calls per copied file; the Xenium docs.
  • Left as follow-ups, outside this batch's files or by design: below.

Follow-ups (not in this PR)

  • storage.check_hdf5_inputs walks a folder source through linked folders (deliberate since F041: the converters' globs go there), so a link to / inside a Space Ranger outs/ walks the disk, and an unrelated HDF5 file with an external link behind a linked folder refuses the conversion.
  • commands/import_data.py's precheck (_source_root, _precheck) opens the source with h5py before any guard runs, so import array/import sparse on an h5ad whose external link names a FIFO hangs.
  • util/staging.disk_size measures a link's own size, so import of a Zarr store kept as links under-estimates its copy (convert now measures through links on its own).
  • storage.open_store stamps the AnnData root attributes on any store opened to write, an .h5mu on import included (from B6's notes).
  • Xenium planning decompresses analysis.tar.gz once to list its members, and writing again to unpack them.

Local results (macOS, on the PR's last commit)

  • uv run pytest -m "not integration": 4,151 passed, 9 skipped.
  • uv run pytest -m perf: 210 passed, one more than main (the F171 memory guard).
  • tests/test_zarr_safety.py (B0's codec guard) is unchanged and green.
  • The audit's repro scripts, rerun with their worktree pointed at this branch, all print OK except F172's: its import half is fixed, and its convert half still prints DEFECT. That half asserts convert must not read a chunk linked out of the store, which the links decision keeps, as anndata reads it. A copy of the script checked against anndata instead confirms three things: convert's output equals anndata.read_zarr of the same store, the stray link stays a link in import's output, and a link to / is refused without being walked.
  • test_performance.py::test_split_memory_stops_growing_with_the_group_count failed in two full local runs on macOS. Its 128-group peak RSS read 76 to 81 MB against about 119 MB for 320 groups, and the limit on the difference is 32 MB. Measured interleaved with main, main gave the same kind of low reading (86 against 119 MB). The test passes alone, in the perf rerun above and in the final unit run. It looks like a noisy reading on macOS, not this change; CI runs on Linux.

🤖 Generated with Claude Code

Claptar and others added 7 commits December 4, 2026 22:37
convert now runs the HDF5 guard on the source and on --raw before any
sniffer opens them to tell their format (F166), and passes every input,
--raw included, to the output check, under any alias (F167). The check
that refuses an output inside a Zarr input is factored out of
check_output (check_not_input) and covers a --to 10x-mtx folder too
(F173).

The free-space check now covers folder sources (Space Ranger outs/,
Xenium bundles, MTX folders, Parse runs), estimated from the files the
converter's plan reads rather than skipped (F168).

Part of #135.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Planning used to unpack the archive's tables beside the output before
convert checked anything, into a folder only a finalizer removed; a
refused or failed run, which ends in os._exit, left .adata-xenium-*
behind. Planning now lists the archive's members only; the tables are
unpacked inside staging, read and checked there, and removed in a
finally. The free-space check counts them (F169).

Part of #135.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…them

The HDF5 guard now covers the cases it missed, and its reads are
bounded (F170, F171).

copy_path copies a Zarr store with links read through only where zarr
reads a file (a node's metadata, an array's chunk), as anndata would;
any other link is kept as a link, a link to a folder is refused rather
than followed out of the store, and a link to nothing where zarr reads
is refused. The codec check no longer walks into linked folders
(F172).

Part of #135.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The output check and every input of convert, folder sources' free
space, Xenium's archive, references in HDF5 files, links in Zarr
stores, and the new refusals in troubleshooting.

Part of #135.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…imates

- An output whose folders reach into a Zarr input through a link is
  refused too: the output's folders are judged as the filesystem
  resolves them, not only by name (F173).
- import's copy reads a link through only for a file zarr reads: a
  chunk under the key its array's chunk key encoding gives a chunk of
  its grid, a v2 node's .zattrs, a v2 group's .zmetadata, and the
  metadata naming a node. Any other link stays a link, whatever its
  name; a link among the chunks of an array whose key encoding cannot
  be read is refused. The copy goes back to copytree's own copy2, so a
  file that is no link costs no more than before (F172).
- The HDF5 guard bounds what it reads by what it follows, judged from a
  dataset's layout and index before reading it (F171).
- convert's estimate counts the files a plan reads beside a matrix file
  named on its own (Visium, Xenium), and measures a Zarr store kept as
  links by what they point at (F168, F169).
- A convert source, or a file of a folder source where an HDF5 file is
  read, that is no regular file is refused before a sniffer can wait on
  it for ever (F166).

Part of #135.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ributes

- import's copy reads a link through only among the files of a node zarr
  reaches, going down the store's groups from its root in the store's own
  Zarr version: metadata planted inside an array, below a folder that is
  no group, or of the other version no longer makes a link beside it a
  file zarr reads (F172).
- The HDF5 guard refuses an attribute of more references than it
  follows, from its shape, before reading it (F171).
- convert's estimate walks no list of names (the text importer's rows).
- An output's folders are judged as the filesystem resolves them only,
  so `-o in.zarr/../out.h5ad`, beside the store, is no longer refused
  as inside it (F173).

Part of #135.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A folder is a node of the copied store only if its metadata is a JSON
object of the store's Zarr version saying what node it is, so a link
named `.zgroup` to some other JSON file makes no node of its folder.
v2's consolidated index is read through only at the root, where zarr
opens a store and reads it (F172).

Part of #135.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings December 6, 2026 14:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T14:56:07.674917Z 64db8f4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

Test Results (py3.13)

3 950 tests   3 937 ✅  6m 41s ⏱️
    1 suites     13 💤
    1 files        0 ❌

Results for commit 64db8f4.

@github-actions

github-actions Bot commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

Test Results (py3.12)

3 950 tests   3 937 ✅  9m 59s ⏱️
    1 suites     13 💤
    1 files        0 ❌

Results for commit 64db8f4.

This branch has not been deployed

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

B1: Store and folder inputs

2 participants