Skip to content

ACS local: full-size materialize and calibrate refuse a staging H5 that is not the one run_identity.json records - #1855

Merged
anth-volk merged 15 commits into
mainfrom
claude/us-local-staging-digest-check
Dec 2, 2026
Merged

anth-volk merged 15 commits into
mainfrom
claude/us-local-staging-digest-check

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Fixes #1854

What this changes

tools/build_us_acs_local_release.py --stage materialize hashes the staging H5 before it reads it and records the digest in run_identity.json as staging_sha256. Two reads of the staging H5 did not compare the file with that digest when they opened it. Both now do, so a staging file replaced after materialize hashed it is refused before any table is read for the run.

  • Full-size materialize (--sample-fraction 1.0). _staging_rung passes staging_sha256 to _load_staging_frame, which passes it to load_legacy_calibrated_us_h5(..., expected_sha256=...) (the keyword materialize --sample-fraction: load only the sampled households instead of the whole staging frame #1840 adds). Before, a staging file replaced after it was hashed, and put back once the whole load returned, gave a checkpoint built from one file while run_identity.json and every receipt named the other. The reduced-size reads compare since materialize --sample-fraction: load only the sampled households instead of the whole staging frame #1840, so every read of the staging H5 by materialize now compares.
  • Calibrate's read for the calibrated H5. _write_calibrated_artifact passes identity["staging_sha256"] to _open_staging_for_calibrated_write, and _StagingRead calls refuse_unexpected_h5_digest right after the check against refused pool files and before it opens the HDF store. Before, do_calibrate compared the digest only when the stage started (_verify_run_identity), and the write opens the file again after the solve, which can take hours. This covers both calls of the write: after a solve, and on a resume whose calibration is already complete.

The comparison uses the digest each read already computes when it opens the file, so it adds no hashing. For a staging file that was not replaced, every output is byte-identical: H5 datasets, calibrated weights, target values and diagnostic values.

Interface

Other files

  • docs/us-acs-local-soi-target-surface.md: the "Refusals" bullet says which reads compare the staging H5 with staging_sha256.
  • changelog.d/us-local-staging-digest-check.changed.md.

Built on #1840

This branch is built on #1840's branch claude/us-local-sample-fraction-load (tracker task 1.7 · 4, head e1dc7ef18), which adds the expected_sha256 keyword and _staging_rung. The PR targets main, so until #1840 merges its diff also shows #1840's commits. This PR's own commits are the four after e1dc7ef18:

  • 9b8f6348a: the tests that fail before the change;
  • 802f712f8: the change;
  • 97c3bad15: tests strengthened after review round 1;
  • f52d38eb0: wording after review round 1.

Tests

Added

  • test_us_acs_local_sampled_materialize.py::test_full_size_materialize_refuses_a_file_swapped_and_restored_around_its_load: a full-size run whose staging H5 is replaced after materialize hashed it, and put back once the whole load returns, is refused with is not the file the caller hashed. ACS take-up never runs and run_identity.json is not written.
  • test_us_acs_local_calibrated_h5_stream.py::test_a_staging_file_replaced_since_materialize_hashed_it_is_refused: the calibrated write is given a valid staging H5 with other values than the one whose digest the run identity records. It is refused before the frame builder runs, and no fill manifest, consumer manifest or calibrated H5 is written.

Changed

  • The do_calibrate tests' stand-in for _open_staging_for_calibrated_write (_calibrate_through_the_write) asserts that the write passes the run identity's staging_sha256, on the solve's path and on a completed resume's.
  • Stand-ins for _load_staging_frame and _open_staging_for_calibrated_write accept the keyword (county-surface and no-county regression tests).
  • The tests that drive _write_calibrated_artifact to the real staging read pass a run identity holding the staging file's digest, as materialize records it. This covers the calibrated-H5 streaming tests, the block-location writer helper and the county export fixture. The county fixture writes the staging H5 after its materialize stand-in hashed placeholder bytes, so its identity now names the written file.

Local results

Each test was run by pytest node ID, only those added or changed; no full suite was run.

  • At 9b8f6348a (tests only): both added tests failed with Failed: DID NOT RAISE <class 'ValueError'>.
  • At 802f712f8 (the change): the 34 added or changed tests passed, 59 cases with parameters (by test: streaming 20, release tool 8, county surface 4, sampled materialize 1, no-county regression 1).
  • Strengthened tests (97c3bad15): with the release tool from e1dc7ef18 put back in the working tree, all 8 cases that reach the write failed on the new assertions, for example assert {} == {'expected_sha256': 's'}. At the change, all 9 cases passed.
  • ruff check and ruff format are clean on every changed file.

CI

No CI results read yet.

Review

One round, in a fresh subagent following the review-code skill: round 1 at 802f712f8. No bugs found (high, medium or low). The reviewer also ran the added and changed tests by node ID, at the tests-only commit and at the head. Since the round found no bugs, review stopped there.

I acted on three of its code-quality findings:

  • the do_calibrate stand-in now asserts the digest it is given, where it used to discard it;
  • the calibrate refusal test also checks that consumer_export.json is absent;
  • the wording now matches the order of checks: the check against refused pool files reads the stored household weights before the digest comparison, so the refusal comes "before any table is read for the frame (or the write)". The changelog fragment no longer describes the unreleased state between materialize --sample-fraction: load only the sampled households instead of the whole staging frame #1840 and this PR.

Left as they are

  • A missing or None digest. identity["staging_sha256"] missing would give a bare KeyError, and None would turn the comparison off. No path reaches either: do_calibrate's _verify_run_identity exits unless the recorded value equals a fresh digest of the file.
  • The refusal after the solve has no remedy in its message. It is h5_io's ValueError, as materialize --sample-fraction: load only the sampled households instead of the whole staging frame #1840's reads and the existing "changed while being read" refusal are; the check when the stage starts names the remedy (re-run materialize).

What the merge waits for

The task has no dependency of its own, and substage 1.7 depends on none. Nothing waits for a release or for a manual step.

🤖 Generated with Claude Code

anth-volk and others added 15 commits December 1, 2026 22:52
…olds

`materialize --sample-fraction` loads the whole staging H5 before it draws
the 1%, 4%, 10% or 25% household sample. These tests write a staging H5
fixture and check that a reduced-size run:

- hands the steps after the draw the frame, take-up receipt and sampling
  receipt the whole-file load gives, at every fraction and at full size;
- writes the same run identity, target matrix, roles, registry, assignment
  file and lean checkpoint tables (the lean checkpoint's file digest is left
  out: PyTables records modification times in it);
- never calls the whole-file staging load;
- uses less traced memory at smaller fractions;
- still refuses hours and self-employment rows outside the sample, a staging
  file replaced between its reads, and the sampling refusals.

Before the change, the whole-load, peak-memory and replaced-file tests fail
on their assertions; the others pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`materialize --sample-fraction` below 1 now reads the staging H5 in two
parts instead of loading it whole before the draw:

1. `_load_staging_columns` reads the columns the hours and self-employment
   refusals, ACS take-up and the draw read (`_materialize_columns_before_draw`),
   for every row, through `load_legacy_calibrated_us_h5_columns`. The
   refusals, take-up and `sample_staging_frame` run on that frame unchanged
   (`_checked_staging_rung`), so they still cover every household.
2. `_with_drawn_households` reads every stored column of the drawn
   households' rows only, through `LegacyCalibratedUSH5Households.select`,
   and puts the drawn frame's columns (take-up's filled cells among them),
   weights, strata, mass log and metadata on it.

Each read refuses every file the whole load refuses. After the second read,
`_require_staging_unchanged` refuses a staging file whose bytes are not the
ones hashed for the run identity, so a file replaced between the reads is
not mixed into one frame. A full-size run keeps the whole load.

The county materialize fixture runs a 1% rung with a stand-in frame, so it
now stands that frame in for both reads.

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

Real builds evaluate the hours and self-employment refusals a second time
through the check registry, on the frame materialize hands them. The test
switches that evaluation on (the test suite turns it off) and checks that a
10% run records the same observations for both entries as a full-size run of
the same staging file, with no evaluation error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The staging fixture gives some ACS persons a complete source identity and
  some ACS SPM units a source id, so ACS take-up's draws for them are keyed
  on those columns. Removing the person source-identity columns from
  `_materialize_columns_before_draw` now changes ACS TANF take-up and fails
  the identity test; removing `spm_unit_source_id` is refused by the column
  loader's guard.
- The whole-load test also watches `load_legacy_calibrated_us_h5`, which
  both partial loaders call when a table cannot be read in parts.
- `_staging_rung`'s docstring, the target-surface doc and the changelog say
  which memory does not shrink with the fraction: the listed columns for
  every row and, during the second read, every string, object and
  nullable-Boolean column for every row.
- The memory test says `tracemalloc` does not count pyarrow or HDF5 buffers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 2 found no bugs. Its notes on the wording:

- The household reader keeps whole only the nullable-Boolean columns that
  hold a missing value (with every string and object column), not every
  nullable-Boolean column.
- `_with_drawn_households` decodes string, object and nullable-Boolean
  columns whole and keeps the drawn rows; only number and Boolean columns
  are read for the drawn rows.
- The check after the second read refuses a file whose bytes then differ
  from the run identity's; it does not catch a file replaced before one read
  and restored before the next.
- Read time does not shrink with the fraction.
- The county fixture helper's comment no longer names a tracker task.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`test_release_checks_self_employment_wherever_it_loads_staging` read
`do_materialize`'s statements for the hours check followed by the
self-employment check. Materialize now loads the staging H5 only in
`_staging_rung` (whole at full size, a column subset for a reduced-size run)
and hands each load straight to `_checked_staging_rung`, which runs both
checks. The test now checks that `do_materialize` reaches the staging only
through `_staging_rung`, that both of its loads are passed to
`_checked_staging_rung`, and that the self-employment check follows the hours
check there, as it still checks for the calibrated-H5 write.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Anthony decided on 1 December that the shared loaders' `expected_sha256`
argument is added in this pull request. These tests fail before it exists:

- `load_legacy_calibrated_us_h5`, `load_legacy_calibrated_us_h5_columns` and
  `LegacyCalibratedUSH5Households` take `expected_sha256`; given the file's
  digest they load what they load without it, and given another digest they
  refuse the file, naming it and both digests. Each is checked on a file
  read in parts and on one read whole because its family table cannot be
  read in parts.
- A reduced-size materialize refuses a staging file replaced after the
  draw, and one replaced after materialize hashed it and restored after the
  draw (each read is then consistent with itself, and the hash after both
  reads matches), with the loaders' new refusal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The shared legacy calibrated US H5 loaders take an optional
`expected_sha256` (Anthony's decision of 1 December: added in this pull
request). `refuse_unexpected_h5_digest` refuses a file whose digest when the
loader opens it is not the expected one, before any table is read:

- `load_legacy_calibrated_us_h5`;
- `load_legacy_calibrated_us_h5_columns`, when it reads in parts and when it
  falls back to the whole load;
- `LegacyCalibratedUSH5Households`, likewise.

Each loader already refuses a file that changes while it holds it open, so
a read that is not refused read exactly the bytes the caller hashed.

A reduced-size materialize passes the run identity's digest to both of its
reads, so a staging file replaced before the first read or between the two
is refused instead of being mixed into one frame. The extra full-file hash
after both reads (`_require_staging_unchanged`) is no longer needed, so a
reduced-size run hashes the file five times instead of six.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 3 found one low-priority bug, left as it is: a full-size
materialize, like calibrate, still loads the whole staging H5 without
comparing it with the run identity's digest, as before this pull request.
Its other findings:

- A test now reaches each partial loader's whole-load fallback with a wrong
  digest: the file's digest is right when the partial loader opens it and
  wrong when the fallback does, and the fallback must refuse it. With the
  fallbacks not passing the digest on, both cases fail.
- The materialize structure test now checks every loading call in
  `_staging_rung`, not only their names: each is a staging load and each is
  the frame `_checked_staging_rung` receives.
- Docstrings no longer claim more than the code does: the denied-pool check
  reads the stored household weights before the digest is compared, both
  digests are taken by path, the digest is compared as given (lower-case
  hex), and a full-size run is not compared.

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

One conflict, in `test_release_checks_self_employment_wherever_it_loads_staging`:
main now searches every nested block of a stage for the hours check followed
by the self-employment check (the calibrated write runs them inside a try);
this branch moved materialize's checks into `_checked_staging_rung` and adds
checks that `do_materialize` reaches the staging H5 only through
`_staging_rung`. Both are kept: the structure checks, then main's nested-block
search over `_checked_staging_rung` and `_write_calibrated_artifact`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since the calibrated H5 write merged, calibrate no longer loads the whole
staging frame to re-draw a development rung: it reads the person columns its
steps read and the other tables whole. The development-rung memory note said
it still loaded the whole frame.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ng file (1.7 · 5)

Two tests that fail before the change:
- a full-size materialize whose staging H5 is replaced after it was hashed
  for the run identity, and put back once the whole load returns, must be
  refused before ACS take-up runs and before run_identity.json is written;
- calibrate's write must refuse a staging H5 whose digest is not the one
  run_identity.json records, before any table is read.

Stand-ins for _load_staging_frame and _open_staging_for_calibrated_write
now accept keyword arguments, and the calibrated-write tests pass a run
identity holding the staging file's digest, as materialize records it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… (1.7 · 5)

A full-size materialize now passes the staging digest it hashed for the
run identity to _load_staging_frame, which passes it to
load_legacy_calibrated_us_h5; calibrate's _StagingRead compares the
digest it computes when it opens the staging H5 with the staging_sha256
run_identity.json records, before it opens the HDF store. A staging file
replaced after materialize hashed it is refused before any table is read.

_load_staging_frame calls load_legacy_calibrated_us_h5 directly (the
multispine base tool's _load_base_frame is that call) and keeps the
keyword optional, since the county AGI support check loads a staging file
with no run identity. _StagingRead and _open_staging_for_calibrated_write
require it. The comparison reuses the digest each read already computes,
so it adds no hashing and leaves outputs unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ead (1.7 · 5)

Review round 1 found no bugs; these strengthen the tests it named. The
stand-in for _open_staging_for_calibrated_write in the do_calibrate tests
now asserts that the write passes the run identity's staging_sha256, on
the solve's path and on a completed resume's; and the calibrate refusal
test also checks that consumer_export.json is not written. Against the
release tool before the change, every do_calibrate test that reaches the
write fails on that assertion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The check against refused pool files reads the stored household weights
before the digest comparison, so the docstrings and the target-surface
document now say the refusal comes before any table is read for the frame
or the write, as #1840's loaders say. The changelog fragment no longer
describes the unreleased state between #1840 and this change, and names
no private functions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@anth-volk
anth-volk marked this pull request as ready for review December 2, 2026 20:51
@anth-volk
anth-volk merged commit 879cef1 into main Dec 2, 2026
12 checks passed
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.

ACS local: full-size materialize and calibrate's staging read do not compare the staging H5 with run_identity.json's digest

1 participant