Skip to content

Fix three latent ingest bugs: computed counts, childcare lock order, AB2025 rebuild cost - #263

Merged
MaxGhenis merged 6 commits into
mainfrom
fix/ingest-robustness-20261127
Nov 27, 2026
Merged

MaxGhenis merged 6 commits into
mainfrom
fix/ingest-robustness-20261127

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

This PR fixes three latent bugs in the ingest modules. Independent reviews of #261 found them; they sit outside #261's scope, and #261 fixed the same first two patterns in its own ingest. The built database does not change.

Changes

  1. Computed counts (62bc0ce). The NZ Treasury ingest counted a claim as computed when its latest result was anything but not_computed. Harvest's advance_computed_lanes counted any claim with any result.
    • New rule: both count a claim only when its latest result (by computed_at, then id) has a value (computed_value IS NOT NULL). That is the "with a value" rule in export_populations.py and app/src/populations.ts, and the one Add Fiscal Lab's Memo Bill Scores as an external source #261's _write_lane uses.
    • Note wording: NZ's regressed-lane note now says the claims "retain a PolicyEngine value" instead of "retain results".
    • Not live today: no harvest or NZ claim has a valueless latest result, so nothing changes.
  2. Lock order (a1f0f24). ingest_uk_childcare_100k.ingest read the registry's old claims before taking any write lock. Two overlapping ingests could both read an empty registry, and each insert every result, leaving each claim's result twice per run.
  3. Rebuild cost (9352b8d). ingest_uk_ab2025.stage tallied merged republications by rescanning the kept claims for every row and rehashing each one: 1,943,155 claim_id() hashes for 8,881 rows.
    • Fix: it now hashes each score once and indexes the kept claims by id. The condition and the family each tally goes to are unchanged.
    • Speed: stage() drops from 14.8 s to 0.26 s, and a full build_db from 27 s to 10–11 s.
    • Helper (429037d): the tally now lives in _merged_twins(mine, kept), so a test can measure its scaling directly. stage()'s output is unchanged.

Tests (each fails on the old code)

  • Fix 1
    • test_lane_counts_only_a_latest_result_with_a_value (NZ) checks a latest pe_gap or concept_mismatch with no value, against ones with a value, and checks the lane's count against the populations feed.
    • test_a_newer_valueless_gap_regresses_a_computed_lane (NZ) checks a newer valueless gap after a value.
    • test_counts_a_claim_only_when_its_latest_result_has_a_value (harvest) covers four cases: a gap only, a newer gap after a value, a tie on computed_at, and a newer value after a gap.
    • cd91477 adds harvest cases that insert a newer-dated row before an older one, either way round, plus mixed-lane tests (harvest and NZ). These check "1 of N", the lane's date and the feed's count.
    • With the old modules restored, 10 tests fail. A latest-row rule ordered by id alone fails 3, and an NZ date taken over every latest row fails 1.
  • Fix 2: test_overlapping_ingests_leave_what_one_ingest_leaves holds two ingests at their registry reads and compares the rows with a single ingest's. On the old code it failed 3 of 3 runs, and every pe_results (claim, run) row was doubled. It never hit a lock timeout.
  • Fix 3
    • test_the_twin_tally_grows_linearly counts the Python steps _merged_twins takes (via sys.settrace) on 500 and 2,000 rows, every claim printed twice, and allows at most a 6x rise for the 4x input. The helper rises 4.0x. The old nested lookup rises 15.7x. An identity scan, an in scan, a cached-id scan and a cached-id index all rise 15–16x and fail. An extra linear uniqueness pass passes. The test catches scans written in Python only: a scan that runs in C, such as id(s) not in [id(k) for k in kept], fires no trace events and passes.
    • test_merged_twins_charge_the_last_row_s_family pins which family each tally goes to, in either row order.
    • test_stage_hashes_each_score_a_bounded_number_of_times counts claim_id() calls through stage() (at most 8 per score, 4 per row read). The old code hashes one score 1,147 times.

Checks

  • stage() output: its full return (score rows in order, and the accounting dict in key order) is byte-identical to origin/main's.
  • Builds: at 9352b8d and again at cd91477, two fresh builds give content hash e451ad6b…, the same as origin/main 771c028, and git diff --exit-code is clean.
  • Formatting: uvx ruff format --check be nz scorecard_db tests pipeline is clean.
  • Full suite: 5,811 passed and 19 skipped at 9352b8d, after a fresh build (CI's serial command, run with --python 3.12 and a scratch --basetemp). The tree had no drift afterwards. At cd91477, which adds only tests, a .get and a docstring rewrap, the touched test files pass (75); CI runs the full suite on that head.

Review

  • Round 1 (Subfleet 20261127-095844-scorecard-263-review, codex-2 sol62, at cd91477). The verdict line read APPROVE, but the round raised two in-scope P2s, both about the new tests, so by the levels it did not agree. Both are fixed in 30e9da8:
    • AB2025 test. It counted hashes only, so a scan over cached ids still passed. 30e9da8 added a count of claim-id comparisons; round 2 showed that still missed an identity scan, and 429037d replaces it (below).
    • Overlap test. It could pass on the old code when one ingest started over a second late. Each ingest now holds at its read until the other has read or started BEGIN IMMEDIATE, and a timed-out wait fails the test. The old module fails all three timing cases, a 1.5 s delay included.
    • Out of scope: its P1 (a constructor race on a brand-new DB file) is filed as Ingest follow-ups from #263: constructor race, rerun duplicates, lane counts #264.
    • The review and dispositions are in ~/reviews/scorecard-ingest-robustness-2026-11-27/runs/review-1/ and fix-1/.
  • Round 2 (full review, Subfleet 20261127-115544-scorecard-263-review-r2, at 30e9da8): REQUEST_CHANGES on one P2, with no P0 or P1. Under the PR guide's round rule the review has finished; the P2 is fixed, not filed.
    • Overlap test (round 1's P2): closed. Base fails all three cases and both 1.5 s delay probes; head passes.
    • AB2025 test (P2): the comparison count missed an identity scan of kept per row (980,662 uncounted comparisons passed) and failed a harmless extra linear pass (11,597 counted against a limit of 8,881). 429037d moves the tally into _merged_twins and tests its traced steps' growth instead; see Fix 3 above.
    • Constructor race (P1, out of scope): still open in Ingest follow-ups from #263: constructor race, rerun duplicates, lane counts #264.
    • Head: the lane reviewed 30e9da8. 429037d (the helper and its tests) has had no lane review; the round rule calls for none after a round with no P0 or P1.
    • Files: runs/review-2/ and runs/fix-2/ under ~/reviews/scorecard-ingest-robustness-2026-11-27/.
  • In-session check. An adversarial workflow (8 agents, read-only) found no P0 or P1. Its two in-scope test-gap P2s are fixed in cd91477. This is supporting evidence, not the independent review.
  • Checks at 429037d.
    • stage()'s full return is byte-identical to origin/main's.
    • Two fresh builds give e451ad6b…, and git diff --exit-code is clean.
    • The four touched test files: 353 passed. ruff format --check is clean.

Not in this PR

🤖 Generated with Claude Code

The NZ Treasury ingest counted a claim as computed when its latest result
was anything but not_computed, and harvest's advance_computed_lanes counted
any claim with any result. Both now count a claim only when its latest
result, by computed_at then id, carries a PolicyEngine value: the rule
export_populations and populations.ts use for "with a value", and the one
PR #261's _write_lane follows. A valueless pe_gap or mismatch as the latest
row no longer makes a lane computed, and NZ's regressed note now says the
claims retain no PolicyEngine value.

No harvest or NZ claim has a valueless latest result today, so the built
database and feeds do not change.

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

vercel Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
policyengine-scorecard Ready Ready Preview Nov 27, 2026 11:42am UTC

Request Review

ingest_uk_childcare_100k read the registry's old claims before any write
lock, so two overlapping ingests could both read an empty registry. Both
then deleted nothing and each inserted every result, leaving each claim's
result twice per run. The ingest now takes BEGIN IMMEDIATE before the read,
as ingest_fiscal_lab does in PR #261, so overlapping runs serialise. The
connection now closes in a finally block, so a lock timeout or a failed
write no longer leaves it open.

The new test starts two ingests on one database and holds each at its
registry read until the other reads, then compares the rows with a single
ingest's. On the old code the two reads meet and every pe_results row is
doubled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
stage() tallied each source's merged twins by scanning the kept claims for
every row and rehashing each one: about 1.9 million claim_id hashes for
8,881 rows. That took about 14 of a full build_db's 27 seconds. Each score
is now hashed once, and the kept claims are indexed by id.

The tally is a transliteration of the old one: a row counts as a merged
twin when its claim id is kept by a different row (the first kept row with
that id), and the tally goes to the family of the last row with that id.
stage()'s full return (every score row in order, and the accounting dict in
its key order) is byte-identical to origin/main's, and stage() now takes
0.26 s.

The new test counts claim_id calls through stage(). The old code hashes
one score 1,147 times; the new code hashes each at most four times.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An in-session adversarial check of 62bc0ce found two gaps (P2):
- Every test inserted rows in date order, so a latest-row rule ordered by
  id alone still passed.
- No test had a lane with both valued and valueless claims, so nothing
  checked that N and updated_at come from the counted claims alone.

New harvest cases insert a newer-dated row before an older one, either
way round. New mixed-lane tests (harvest and NZ) check "1 of N", the
lane's date and, for NZ, the populations feed's count. The old modules
fail 10 of these tests, an id-only ORDER BY fails 3, and an NZ date taken
over every latest row fails 1.

Two small cleanups: the second harvest read skips a claim missing from
the first, as the old code did, rather than raising KeyError; and an
over-long NZ docstring line is rewrapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Round 1 (Subfleet 20261127-095844-scorecard-263-review) raised two P2s
against the new tests:

1. The AB2025 test counted claim_id() calls only. A tally that hashes
   each score once but scans cached ids per row is still quadratic, and
   it passed. The test now also counts comparisons between claim ids,
   through a str subclass. The bounds are loosened to allow another linear
   pass (at most 8 hashes per score, 4 per row read, and 1 comparison per
   row read). The original code fails at 1,147 hashes of one score, a
   cached-id scan at 961,087 comparisons, and an extra uniqueness pass
   still passes.

2. The childcare overlap test waited at most a second for the other
   ingest's registry read. When one ingest started over a second late,
   the old code passed. Each ingest now holds at its read until the other
   has either read or started BEGIN IMMEDIATE, and a wait that times out
   fails the test. A new parameter starts either ingest half a second
   late. The old module fails all three cases, with a 1.5 s delay too, on
   doubled pe_results rows; the fixed module passes them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 2 found that the stage-level test, which counts claim-id
hashes and comparisons, passes an identity scan of `kept` per row
(quadratic, uncounted) and fails a harmless extra linear pass.

Move the tally into `_merged_twins(mine, kept)`, which stage() calls
when rows were merged. Its output and stage()'s are unchanged: the
staged scores and accounting match main byte for byte, and two builds
give content hash e451ad6b... with no feed drift.

Test the helper directly:
- the tally goes to the last row's family, in either row order;
- its traced Python steps grow at most 6x when the input grows 4x.
  The helper grows 4.0x. The old nested lookup grows 15.7x; an identity scan, an `in` scan,
  a cached-id scan and a cached-id index grow 15-16x; an extra linear
  uniqueness pass passes.

The stage-level hash bound stays, loosened to 8 per score and 4 per
row read, so a regression in stage() itself still fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MaxGhenis
MaxGhenis marked this pull request as ready for review November 27, 2026 13:54
@MaxGhenis
MaxGhenis merged commit c789a3b into main Nov 27, 2026
15 checks passed
@MaxGhenis
MaxGhenis deleted the fix/ingest-robustness-20261127 branch November 27, 2026 13:57

This branch was successfully deployed

1 active deployment
Preview — 429037d8 Deployed Nov 27, 2026 by vercel[bot]
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