Skip to content

Run the 16 US base steps from the capital-gains tail to education inputs in one child process - #1804

Draft
anth-volk wants to merge 11 commits into
mainfrom
claude/us-base-carry-out-1-5-6-1c5b65
Draft

anth-volk wants to merge 11 commits into
mainfrom
claude/us-base-carry-out-1-5-6-1c5b65

Conversation

@anth-volk

Copy link
Copy Markdown
Contributor

Tracker task 1.5 · 7 in project 15 (US build memory, CPU and run time), carrying out part A of decision 1.5 · 6.

What changes

tools/build_us_puf_support_base.py --stage all used to start one child process per step. It now starts one child process for those of the 16 steps from capital_gains_tail_transfer to education_inputs_post_clone (ONE_PROCESS_STEPS) that are still to run. Every other step keeps a process of its own.

  • Only the first of the 16 reads a checkpoint.
    • It reads its predecessor's checkpoint from disk. That read checks the file's SHA-256 and the frame identity, as before.
    • Each later step takes the frame the step before it completed (_CarriedCheckpoint). The holder hands the frame over and keeps no reference to it. So each step still frees its input frame before its own checkpoint write, as US base: free each step's input frame before its checkpoint write #1799 does.
  • Each step keeps its own outputs and checks. Each step still runs the same per-step code (_run_outer_step, the former body of _run_outer_stage), and still:
    • writes its checkpoint, its completion record, its stage_profile.json record and its saved observations of the us/gates.json checks, so a stopped run resumes after the last finished step;
    • compares the run config with the locked one before it runs, as each step process does at its start, so a change to the builder's source files during a run stops the next step (Hash the US base input files once per staged run #1747);
    • when the run resumes inside the 16, the child starts at the first unfinished step.
  • The child refuses NumPy scalars in memory.
    • Reading a checkpoint turns NumPy scalars into Python values (np.float32 into a 64-bit float, np.str_ into str) in these places:
      • object columns and indexes;
      • python-backed str columns and indexes;
      • the strata and the strata's index.
    • A frame handed over in memory keeps them.
    • So before a step takes a frame from memory, the child checks those values' types (_refuse_values_a_checkpoint_read_changes). It stops if reading the checkpoint would change any of them, naming the step, the place and the types.
    • The step that left them has completed, so a resume reads its checkpoint and goes on.
    • The check took 30 ms for a post-clone frame's 2.3 million such values, and 117 ms on a heavily loaded machine.
    • A review of the 16 steps' code found none that leaves such values; no run has shown it.
  • Telemetry: one stage change per step.
    • The parent opens a pipe and passes its file descriptor and its own process ID in MICROCOSM_US_BASE_STEP_REPORT. The child writes each step's name to the pipe as the step starts.
    • The parent records the first step's stage change before the child starts, as for any child. It records each later one when the child reports it, so a failure is recorded against the step that failed, and the error names it.
    • A report that is not the next step stops the child. So does an exception in the parent while the child runs. After a Ctrl-C the parent first waits 0.25 s for the child, as subprocess.run does.
    • A child that exits cleanly before its last step started is refused.
  • Single steps are unchanged.
    • A step process uses the variable only when its parent set it and its --stage is one of the 16. A named --stage run by hand still runs one step.
    • Single-step children get no report variable, even one the parent inherited.
    • A malformed variable stops the step process, as a malformed MICROCOSM_US_BASE_PARENT_DIGESTS does (Hash the US base input files once per staged run #1747). So does one that names a descriptor that is not an open pipe, where the variable would take effect.
    • The child makes the write end non-inheritable, and the pipe never takes descriptor 0, 1 or 2.
  • No new tool option. The Modal plan's list of the tool's flags is unchanged; only a comment there changes.

What one process gives up (accepted in the decision)

  • Memory between steps.
    • Memory is not returned to the system between these 16 steps.
    • None of their modules imports policyengine-us, so no tax-benefit system is built in them (from the 1.5 · 6 facts). What remains is memory the allocator keeps after earlier steps free it.
    • The first real run's stage_profile.json will show it, since Linux, including Modal, keeps a sampled peak per step.
  • Peak memory on macOS. Each step's recorded peak memory is the process's peak so far.
  • Read-back check. A frame handed over in memory is not read back. Its checkpoint's SHA-256 and identity are next checked when a later process reads the file: congressional_district_assignment or household_location_assignment, or a resume.
  • Shared-column records.
    • A frame handed over keeps the records of which column memory it shares with earlier frames; a frame read from disk has none.
    • So a step that wrote into shared column memory in place would be refused at its checkpoint write here, where a step that read its input from disk would not. No step does that.
  • Two differences that are not checked. A check could stop a run whose output would be the same.
    • A read gives each missing value in an object column its own NaN object, while a frame in memory may repeat one. So set and in on the raw values can differ.
    • A read gives each table's column labels the str dtype a new table gets.
  • Memory layout.
    • A frame handed over keeps its columns' memory layout, while a checkpoint read gives contiguous arrays.
    • pandas 3 copies a column into new, contiguous memory when it builds a table unless given copy=False. Review found no table built that way in the 16 steps' code, so this is not checked.

How this was checked

The base bit-equivalence test was not run: task 1.5 · 8 was cancelled. The finish condition is the fixture test below.

  • test_one_child_writes_the_same_files_as_16_separate_step_processes:

    • It runs the 16 steps on a small fixture frame in 16 real step processes, then in one real child started through the parent's own code (_run_one_process_steps).
    • Every file they write has the same SHA-256: the 16 checkpoints, the run context and the 16 saved observation files.
    • 15 of the 16 step functions are stand-ins. Each writes a digest and a floating-point sum of each person's numeric and boolean values. The digest covers:
      • every value with its type, and the count of distinct objects in each column;
      • every dtype, the indexes and the column labels;
      • the strata and the strata's index;
      • the weights, the mass log and the frame metadata.
    • Each stand-in also evaluates its step's us/gates.json entry, so the observation files hold entries.
    • capital_gain_distributions runs its real code.
    • Each process gets the test process's policyengine-us lock-file check record, as a --stage all parent's step processes do.
    • Measured on a 10-core Mac at load average 14–17, the test took 343 s and 226 CPU-s. Each step process took 11–19 s, 8.5–12 CPU-s and about 230 MB. Creating the lock-file check record once took 72–106 s, and the pytest process reached about 1.4 GB. The pytest process already pays that cost when it runs any test that calls _run_staged_all unstubbed.
  • test_one_child_hands_each_step_the_frame_its_checkpoint_holds: in one process, it checks that each frame handed over equals what reading the previous checkpoint gives, by value and dtype, and equals the frame a single-step run read.

  • Other tests cover:

    • the order in which each step reports, profiles, saves and records;
    • one profile record per step;
    • the per-step run-config comparison;
    • that each handed-over frame is freed before the next checkpoint write, with garbage collection off;
    • the NumPy-scalar check;
    • a failing step;
    • the report variable's conditions and malformed values;
    • the parent's grouping of steps into processes for a fresh run and for resumes;
    • telemetry per step;
    • failure attribution;
    • the loading of saved observations;
    • protocol errors, a child that exits early, Ctrl-C and other interruptions;
    • the pipe's descriptors;
    • one test with a real child process that finds the pipe its parent opened.
  • Parent-digest tests: in test_us_puf_support_base_parent_digests.py, the stand-in now also stands in for subprocess.Popen, and the tests that listed one process per step now expect one process for the 16.

  • How the tests were run: each new or changed test was run by node ID; no full suite was run.

  • Red-green commits:

    Failing tests Change or fix
    baa4f0f, 8b9436c 629bded (the change)
    0f4abe5 38d01ac (review round 1)
    06421bb 9dae858 (review round 2)
    f2bf5a7 6ccb5cf (the NumPy-scalar check)
    f54cf59 4cb3de3 (review round 3)

Estimated saving (inferred in the 1.5 · 6 decision, not measured): about 4 to 6 minutes and 4 to 6 CPU-minutes per Modal base run, less about 3 to 12 s for the 15 extra run-config comparisons.

Review

Three rounds of adversarial review by subagents. None found a change in the bytes the code writes.

  • Round 1, fixed in 38d01ac:
    • The 15 later steps did not compare the run config with the locked one; the comparison is restored.
    • The report pipe was inheritable by processes the child starts.
    • A report variable naming a descriptor that is not a pipe was not refused.
    • One error message was misleading.
    • Round 1 also prompted the test that each handed-over frame is freed before the next checkpoint write.
  • Round 2, fixed in 06421bb and 9dae858:
    • The fixture compared observation files that held no entries.
    • After a Ctrl-C the child was stopped without the 0.25 s wait subprocess.run gives.
    • The pipe could take descriptor 2 in a parent started with its standard streams closed.
    • The fixture baseline ran in the test process; it now runs in 16 real step processes.
    • It also showed that a step leaving NumPy scalars in an object column would write different bytes from the next step on. That led to the check above.
  • Round 3, fixed in 4cb3de3:
    • The check missed python-backed str columns and the strata's index.
    • The stand-ins could not see some kinds of value difference.
    • Doc wording claimed more than was shown.
  • Not changed: tools/modal_us_stage_plan.py does not refuse a plan that sets MICROCOSM_US_BASE_STEP_REPORT (as it does not refuse MICROCOSM_US_BASE_PARENT_DIGESTS). Plans only run --stage all, whose parent removes the variable from every child's environment.

🤖 Generated with Claude Code

anth-volk and others added 11 commits November 27, 2026 23:25
…n inputs

Failing tests for decision 1.5 · 6, part A: under --stage all, one child
process runs the 16 steps from capital_gains_tail_transfer to
education_inputs_post_clone instead of one process per step.

- The child reads only the first step's predecessor checkpoint; each later
  step takes the frame the step before it completed. Each step still writes
  its checkpoint, completion record, profile record and saved check
  observations. A fixture test compares every file the 16 steps write with
  the files 16 single-step runs write, and each in-memory input with what
  reading the previous checkpoint gives.
- Before each step the child writes the step's name to a pipe its parent
  opened (MICROCOSM_US_BASE_STEP_REPORT), and the parent records one
  telemetry stage change per step. A named --stage run by hand still runs
  one step.
- The parent-digest tests now expect one step process for the 16.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A report variable the --stage all parent inherited from its own
environment must reach none of its single-step children. The fixture test
also requires the 16 steps' saved check observations to exist before it
compares the files of both runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Decision 1.5 · 6, part A. Under --stage all, the parent starts one child
for the steps from capital_gains_tail_transfer to
education_inputs_post_clone that are still to run (ONE_PROCESS_STEPS),
instead of one child per step. Every other step keeps its own process.

- The child reads the first step's predecessor checkpoint from disk and
  hands each later step the frame the step before it completed. Each step
  still goes through the per-step code (_run_outer_step): its profile
  record, saved check observations, identity check, checkpoint and
  completion record, and the input frame is still freed before the
  checkpoint write. A resume inside the 16 starts the child at the first
  unfinished step.
- Before each step the child writes the step's name to a pipe whose file
  descriptor the parent passes in MICROCOSM_US_BASE_STEP_REPORT, with its
  process ID. The parent records one telemetry stage change per step and
  names the step that failed. A report that is not the next step stops
  the child; so does an exception in the parent, as with subprocess.run.
- A step process uses the variable only when its parent set it and its
  --stage is one of the 16. A named --stage run by hand runs one step.
  Single-step children get no report variable.
- Docs: the variable's row in docs/orrery-publication.md, and the Modal
  plan's comment on how --stage all runs its steps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Failing tests from review round 1:

- The child for the 16 steps compares the run config with the locked one
  before each step, as each step process does at its start, so a change to
  the builder's source files during the run stops it before the next step.
- The child makes the report pipe's write end non-inheritable, so a process
  it starts cannot keep the parent waiting for the end of the reports.
- A report variable naming a descriptor that is not an open pipe stops the
  step process instead of letting it write step names into another file.
- A child that exits cleanly without reporting any step gets an error that
  says so and names the variable.

Also a passing guard: each step frees the frame it was handed before its
checkpoint write. The stand-in steps now read every table, the strata and
all weights.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes from review round 1:

- The child for the 16 steps compares the run config with the locked one
  before each step after the first, as each step process does at its
  start (decision of 2026-11-26 in #1747), so a change to the builder's
  source files during a run stops it before the next step.
- The child makes the report pipe's write end non-inheritable, so a
  process a step starts cannot keep the parent waiting for the reports.
- A report variable naming a descriptor that is not an open pipe stops the
  step process. The variable's comment and docs row say that the
  process-ID comparison can match a copy where process IDs repeat, and
  when the variable takes effect.
- A child that exits cleanly without reporting any step gets an error that
  says so.
- The ONE_PROCESS_STEPS comment notes that a frame handed over keeps its
  records of shared column memory, which a frame read from disk lacks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 2 and Anthony's choice of a real-process baseline:

- test_one_child_writes_the_same_files_as_16_separate_step_processes runs
  the 16 steps in 16 real step processes, then in one real child started
  through _run_one_process_steps, and compares the SHA-256 of every file
  they write: the 16 checkpoints, the run context and the 16 saved
  observation files. Each process gets the test process's policyengine-us
  lock-file check record, as a --stage all parent's step processes do.
- The stand-in steps evaluate each step's us/gates.json entry through the
  shadow evaluation, so the compared observation files hold entries.
- Failing: after a Ctrl-C the parent waits 0.25 s for its child before
  stopping it, as subprocess.run does; the report pipe never takes
  descriptor 0, 1 or 2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes from review round 2:

- After a Ctrl-C, which the child gets too, the parent waits up to 0.25 s
  for it before stopping it, as Popen.wait does under subprocess.run, so
  the failing step can save its observations and profile record.
- The report pipe's ends are moved above descriptor 2 when os.pipe returns
  a standard stream's descriptor (a parent started with one closed);
  otherwise the write end could become the child's standard error.
- Docs: the descriptor check applies only where the variable takes
  effect; the changelog says how the change was checked, that 15 of the 16
  fixture steps are stand-ins, and that the base bit-equivalence test was
  not run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Anthony's choice (2026-11-28) for review round 2's open gap: reading a
checkpoint turns NumPy scalars in object columns, an object index or
object strata into Python values, and a frame handed over in memory keeps
them. Failing test: the child stops, once the step that left them has
completed, before the next step takes the frame. The last of the 16 may
leave them, since its frame is read from disk by the next process.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Before a step of the 16 takes the previous step's frame from memory, the
child checks the types of the values in its object columns, object index
values and object strata. If reading the checkpoint would turn any into
another type (NumPy scalars become Python values), it stops with an error
naming the step, the place and the types; the previous step has
completed, so a resume reads its checkpoint. About 30 ms on a post-clone
frame's 2.3 million object values. No step leaves such values today.

The ONE_PROCESS_STEPS comment also records why memory layout is not
checked: pandas 3 copies columns into new memory when it builds a table,
and no step builds one with copy=False.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 3. Failing: the child also stops before handing over
NumPy scalars in a python-backed str column (pandas keeps np.str_ there;
a read gives str) and in the strata's own index (Frame compares it with
the person index by value; a read replaces it with the person index).

The stand-in steps now write a digest of every value with its type, every
dtype (column labels included), the indexes, the strata and their index,
the weights, the mass log and the frame metadata, next to a
floating-point sum of each person's numeric and boolean values. Before,
boolean columns, non-string object values, index values and the column
labels' dtype did not reach their output.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fixes from review round 3:

- The check before a step takes a frame from memory now also covers
  python-backed str columns and indexes, which keep the np.str_ values
  they are given, and the strata's own index, which a read replaces with
  the person index.
- The ONE_PROCESS_STEPS comment and the changelog name the two
  differences that are not checked, because a check could stop a run
  whose output would be the same: a read gives each missing value in an
  object column its own NaN object, and gives column labels the str
  dtype.
- The changelog says how "no step leaves such values" was established
  (code review, not a run), what the fixture's stand-ins read, and the
  check's measured cost (30 to 117 ms per handover).

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant