Skip to content

Bounded pure-TypeScript HDF5 metadata reader (A3) - #2378

Merged
neuromechanist merged 7 commits into
feature/issue-2182-epic-pq-evidencefrom
feature/issue-2186-hdf5-meta
Nov 25, 2026
Merged

neuromechanist merged 7 commits into
feature/issue-2182-epic-pq-evidencefrom
feature/issue-2186-hdf5-meta

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Part of #2366
Part of #2182

Card A3 of the publication-queue plan: a dependency-free TypeScript walker that lists every group, dataset and attribute of an HDF5 file through random-access reads, returns strings and small numerics, and never reads a large numeric dataset. Cards A4 (SNIRF) and A5 (MATLAB v7.3) build on it.

What it does

  • shared/hdf5-meta.ts exports walkHdf5(source, { limits }), returning { entries, complete, incomplete?, tally }; the format decoding lives in shared/hdf5/ (io.ts, messages.ts, structures.ts).
  • Supported: superblocks 0 to 3 (user block found at 0, 512, 1024, ...), object headers 1 and 2 with continuation chunks, old-style groups (symbol table, version 1 B-tree, local heap), compact and dense links and attributes (fractal heap with direct and indirect blocks, version 2 B-tree), fixed and variable-length strings, integers, floats, enums, arrays, compact, contiguous and chunked layouts with deflate and shuffle.
  • Bounds: 8 MiB read, 20,000 logical reads (calls to the source's read; the fetch cap that bounds the network is the source's, A1's page-cached source and the reader registry limits), 20,000 entries, depth 32; every read is checked against the file size before the source is asked; each object address is visited once. An unsupported construct is skipped and recorded; a limit, a failing source read, or any error no decoder foresaw (recorded as corrupt) ends the walk with the entries found so far: walkHdf5 never throws.
  • scripts/dev/hdf5-oracle.py (run with uv run --with 'h5py==3.16.0') prints the same entry list from h5py; the tests compare against the committed oracle JSON, so CI needs no Python.

Fixtures

Three real recordings (public-domain snirf-samples Simple_Probe written by EasyH5/JSNIRFY, a BSD-2 Homer3 export, and the BSD-2 EEGLAB tutorial .set saved as MATLAB v7.3 from sccn/eegprep) and four small h5py-written files (dense storage under libver latest, chunked deflate strings, two unsupported constructs). 6,128,873 bytes in total, committed as plain blobs; sources, commits, licenses and hashes are in test/fixtures/hdf5/README.md.

Deviations from the card

  • RangeSource is defined locally in shared/hdf5/io.ts (A1, Coverage contract and reader registry (A1) #2374, is not merged); A4 switches to A1's.
  • Hdf5Incomplete gains read-failed: a source read that throws ends the walk with the entries so far instead of losing them.
  • value can also be string[] (a non-scalar string dataset); skipped can also be type (reference, compound and the like) and error (the reason is in incomplete); dataset entries carry storage (layout, data address and size), which the no-signal test uses.
  • MATLAB's variable-length sequence of one-byte strings (MATLAB_fields) is read as strings (vlen:string), and datatype version 5 (what libhdf5 2.x writes under libver latest) is accepted; references are listed with skipped: "type" and do not make a walk incomplete.
  • Version 4 chunk indexes (single chunk, fixed and extensible arrays, version 2 B-tree) are listed but their values are not read.
  • The read limit is 20,000 logical reads (lead ruling), not 2,000: the real v7.3 .set (1,558 object headers) takes about 2,060 exact reads, and its oracle test runs at the default; a separate test shows a 2,000 cap stopping it with limit-reads and a partial list. Headers packed back to back are read one per read (the read carries the next header's 16-byte prefix only when that header starts exactly there), so no byte outside a known structure is fetched.

Review round 1

Fixed: walkHdf5 never throws (unforeseen errors are recorded as corrupt); the shuffle filter's element size must equal the datatype's and be nonzero, checked before any loop; a zero-byte datatype is corrupt, and the chunk path refuses an element size below 1 and bounds the per-chunk element count before any per-element loop; the fixture total is corrected; the module docblock states the chunk-cap and skipped: "type" rules.

Tests

bun test test/hdf5-meta.test.ts: 26 tests; every fixture equals its oracle (the Fletcher-32 dataset's value is the one expected difference). Bounds: half-truncated file (corrupt, no throw, no read past the end), the end-of-file check alone, 4 KiB byte limit, default read limit, object and depth limits, a member patched to point at the root, a failing source, a source that breaks its contract (an unforeseen error, recorded as corrupt), a non-HDF5 file, 300 seeds of random bytes behind a valid signature and 200 seeds of random byte flips in a real SNIRF (always returns), a shuffle element size patched to 0xFFFFFFFF (corrupt within a 1 s bound), a datatype patched to zero bytes (corrupt). On the SNIRF fixture no read overlaps /nirs/data1/dataTimeSeries and the walk reads 39,358 bytes in 319 reads. Every rule was reverted or widened one line at a time and each mutant fails a test (17 mutants, plus 3 for review round 1; the element-size floor and element-count bound in the chunk path are kept as defense in depth and cannot be reached once a zero-size datatype is refused, so their mutants are equivalent). The live tier was not run (phase-PR gate is local).

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR2378

Version: 0.10.40-PR2378.df1a9eb
Commit: df1a9eb


🤖 Updated on each push.

@neuromechanist

Copy link
Copy Markdown
Contributor Author

Merge gate (lead): head df1a9eb. Every remote check passes after one rerun of unit-pure shard 2, which failed on the zarr-source-check stall-timeout flake (#2377), a file this diff does not touch. Review: round 1 panel (code+tests as an adversarial parser review; errors+comments+types), 2 important findings (the walker could throw; unshuffle could hang) and 4 more fixed; round 2 delta review resolved every item; its fuzz time-bound suggestion is fixed in df1a9eb, and the other two are accepted as designed. Lead ruling: the walker's logical read cap is 20,000, and the real bounds are at the source.

@neuromechanist
neuromechanist merged commit d767b62 into feature/issue-2182-epic-pq-evidence Nov 25, 2026
51 of 53 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-2186-hdf5-meta branch November 25, 2026 23:18
neuromechanist added a commit that referenced this pull request Nov 26, 2026
* Add text-header kinds, copy and helpers

* Register BrainVision and NIRx readers

* Add the reader false-positive measure script

* Bounded pure-TypeScript HDF5 metadata reader (A3) (#2378)

* Add the h5py oracle for HDF5 metadata

* Add HDF5 fixtures and their h5py oracle output

* Add a bounded HDF5 metadata walker

* Test the HDF5 walker against h5py

* Count logical reads; default cap 20,000

* Review round 1: never throw, shuffle, zero sizes

* Bound each fuzz walk at 200 ms

* Add not-applicable and notes to readers

* Apply lead rulings to text-header readers

* Bound free text; decode UTF-16 headers

* Scan unplaced text; narrow not-applicable

* Require three signs for not-applicable

This branch was previously deployed

1 inactive deployment
npm — df1a9eb5 Deployed Nov 25, 2026 by neuromechanist via publish #4894
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.

1 participant