Repository navigation
Resolve a household's county from its ZIP code - #47
Merged
Merged
Conversation
The form sends a ZIP and no county, so policyengine-us gave every household its state's first county. A Cambridge, Massachusetts household read Barnstable County's benchmark premiums. The API now maps the ZIP to its county, keeps an explicit county from a share link, passes the ZIP on to the engine, and returns a `location` block saying which county it used and why. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
The results page now names the county the chart used. When a ZIP spans counties it states the rule, shows each county's share of the ZIP's residents and offers a picker, whose choice goes in the share link. Tests cover the whole table, the resolution rule and the builder, with property tests, and a differential test that a Middlesex ZIP gives the same series as the explicit county link. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A county picked on the results page goes through the calculator's county normalization, so each code in the table must come out unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
In a ZIP with no residents, the note said a county the user picked had the most land. Only the first county does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
policyengine-us's premium table has no row for South Carolina's rating areas 9 and 19 or Florida's 22, which serve Calhoun, Edgefield and Gulf counties. Once the API read the ZIP, households there went from another county's premium tax credit to none, with nothing on the page to explain it. The location block now reports has_benchmark_premium, and the results page states the gap. A test compares the list with the engine across every county, so an engine update that adds the rows fails until the note goes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MaxGhenis
marked this pull request as ready for review
November 5, 2026 16:43
The note read "in a District of Columbia county" and "in District of Columbia". DC is the state the form sends most mismatched ZIPs to, so it is the note's most common reader. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README.md conflicted: keep main's Cold-start install section and this branch's County from ZIP section, in that order. Everything else merged cleanly. The next commit adapts this branch to #40's Vitest tests, payload invariants and variable-graph snapshot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The note for a link naming a county outside the ZIP said "ZIP 02467 lies mostly in Middlesex County", but Middlesex holds 40% of its residents. It now says the ZIP covers more than one county and that the chart would otherwise use Middlesex County, home to 40% of its residents, or the county with the largest share of its land when no one lives there. A fast-check property checks that a note never says "mostly" and that every share it quotes matches that county's option. The tests import `test` from Vitest, as main's do since #40. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Housing: the ZIP and county let pha_payment_standard find a ZIP-level standard, a small-area rent or Los Angeles County's schedule, and with housing assistance among a household's programs the engine then pays a voucher's subsidy to renters and homeowners alike wherever that standard exceeds the household's required payment. CliffWatch has no voucher input (#30), so both builders set takes_up_housing_assistance_if_eligible to False. Housing assistance stays at $0, as on main for every household but links naming Los Angeles County, while receives_housing_assistance keeps feeding the state programs that read it. Rating areas: policyengine-us joins a county's code to aca_rating_areas.csv and gives rating area 1 to a county the join misses. 31 counties appear there under other names (BAYLOR_TX, the former Valdez-Cordova and Wade Hampton rows, and so on), so the builder now sends slcsp_rating_area for them. Oglala Lakota County has no row under any name and keeps rating area 1. Tests: every table county has an exact row, an override or no row; each override holds its row's area; each override county prices like a twin county the join matches; the override reaches every point of the series; housing stays $0 where the engine computes a voucher; the API cache keeps two ZIPs' series apart; a ZIP alone equals its county named in every state; and the builder ranks a ZCTA with no residents by land. The payload invariants now draw ZIPs and counties, and the JS comparison leaves out the ZIP and the override counties, which only the Python builder handles. The variable-graph snapshot adds zip_code, slcsp_rating_area and the take-up flag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The second review found three gaps in the tests: - The ZIP-alone-equals-named-county property drew at most 300 examples from a fixed 2,000-ZIP sample. It now runs every (ZIP, state) pair the table holds, 41,120 in all, per state; so does the check that a ZIP's county options lead with the county it resolves to. - The rating-area test saw only exact codes, so a new row for Oglala Lakota under another spelling would have passed while the county kept area 1. A new test looks for rows under its own and its former name, Shannon. - The frontend's form-vs-table check exempted every 201xx and 569xx ZIP. Those ranges now pass only with the known disagreement: form DC where the table says Virginia, and form Minnesota where it says DC. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The coverage test checked that every ZIP and every state appear in the per-state lists, so lists holding each ZIP under its first state only (40,982 pairs instead of 41,120) passed it while the 138 pairs of ZIPs that cross a state line went untested. It now compares the listed pairs, each exactly once, with a fresh walk of the table. Also formats this PR's files with ruff (one blank line in zip_county.py), sorts an import block, and passes the builder property's order check a measure dict instead of lambdas that closed over loop variables (B023). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
This PR landed at a48741b as squash commit db78f41, after these checks and reviews:
Production has served this code since 19:27Z. Its answers to 31 requests match, byte for byte, the answers the API gave locally at 12f4df1, whose code a48741b changes only in formatting. The requests cover:
Before the merge, production matched local main on all 31. The deploy changed 29 of the 31 answers. The 16 landing answers, from households that give no ZIP, gained the new |
MaxGhenis
added a commit
that referenced
this pull request
Nov 8, 2026
Conflicts: - cliff_watch/calculator.py keeps the engine bridge's import beside #47's zip_county import. - cliff_watch/variable_graph.py lists is_tanf_enrolled and slcsp_rating_area. This branch already keeps ssi in REVIEWED_TOTAL_INPUTS, so main's duplicate entry goes. #47's engine test built its direct Simulation through calculator._load_simulation, which this branch removed. It now imports Simulation from policyengine_us, as the other engine tests do, and its 27 tests pass on policyengine-us 2.2.1. The README's Maryland note now follows the county the API resolves from the ZIP. Regenerating the variable-graph snapshot on 2.2.1 reproduces the merged file, which adds #47's zip_code, slcsp_rating_area and takes_up_housing_assistance_if_eligible. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 8, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
CliffWatch's form asks for a ZIP and sends no county:
InputPanel.jsxsetscounty: null. Before this PR, nothing in the calculator read the ZIP, so policyengine-us gave each household the first county alphabetically in its state (first_county_in_state). A household in Cambridge, Massachusetts, read Barnstable County's premiums: rating area 7, where Middlesex County has rating area 3.323,511,506 of the 331,440,751 residents in the table (97.6%) live in a ZIP that resolves to a county other than that default, and 72 variable files in policyengine-us name
countyorcounty_str.The fix
The API now resolves the county, so the form, share links, direct API calls and the Modal fallback follow one rule:
county, such as a share link'scounty=MIDDLESEX_COUNTY_MA, always wins.A ZIP that spans counties goes to the county with the largest share of its residents. 9,981 of the 40,982 ZIPs (24.4%) touch more than one county, but 97.48% of residents live in their ZIP's largest county. The largest county holds under half of the residents in 118 ZIPs and under 90% in 4,420. So the page says what it did in those cases: "ZIP 02467 covers more than one county. This chart uses Middlesex County, home to 40% of its residents." A picker lists each county with its share. The page writes the choice into the share link, and editing the ZIP clears it. When a link names a county outside the ZIP, the page says so and quotes the ZIP's own county the same way: "This link names Barnstable County, so this chart uses it. ZIP 02467 covers more than one county, and the chart would otherwise use Middlesex County, home to 40% of its residents."
A series response gains a
locationblock with the county, its name, its source (explicit,zipornone), whether the engine holds a benchmark premium for it, and the ZIP's counties. The chart subtitle now reads "Middlesex County, Massachusetts · 1 adult · 2 dependents".Two engine gaps would turn a correct county into wrong numbers, so the situation builder also handles them.
It sends the ACA rating area for 31 counties the engine's join misses.
slcsp_rating_area_defaultjoins the county's code toaca_rating_areas.csvand gives rating area 1 to any county the join misses. That file spells 31 counties differently from their codes, such asBAYLOR_TXforBAYLOR_COUNTY_TXandDONA_ANA_NMforDOÑA_ANA_COUNTY_NM, and it holds Chugach, Copper River and Kusilvak only under the former Valdez-Cordova and Wade Hampton rows.RATING_AREA_OVERRIDESincliff_watch/zip_county.pysends those counties' rating areas asslcsp_rating_area. 28 of the 31 differ from rating area 1. 108 ZIPs resolve to those 28, with 593,430 residents in those counties' parts of them, 212,900 of them in Doña Ana County.Each cell gives the rating area the engine reads and its age-0 benchmark premium a month. On main a link that named one of these counties got rating area 1, and the ZIP alone got the state's default county.
Oglala Lakota County, South Dakota, has no row under any name, including its former name Shannon County, and keeps rating area 1.
It keeps housing assistance at $0. With a county and a ZIP,
pha_payment_standardfinds a PHA's ZIP-level standard, a metro small-area rent or Los Angeles County's schedule. The default "all programs" mode includes housing assistance. With it among a household's programs,receives_housing_assistancemakes the household eligible whatever its tenure, andhousing_assistancepayshud_hap. CliffWatch has no input for whether a household holds a voucher (#30), and its rent field also takes mortgage payments, so the engine would pay a voucher's subsidy to renters and homeowners alike. Both builders now settakes_up_housing_assistance_if_eligibleto False.housing_assistancemultiplies by that flag, so it reads $0, whilereceives_housing_assistancekeeps feeding the programs that read it: Massachusetts LIHEAP, Connecticut TFA, Rhode Island Works and Orange County general relief.Change in cliffs for the example household
The example household, a single parent, 33, with children 6 and 10, lives in Cambridge:
?s=MA&zip=02139&ms=unmarried&p=33:a::0,6:c::0,10:c::0.The link with
&county=MIDDLESEX_COUNTY_MAgives the same four zones on main and on this PR, with sourceexplicithere.We measured these by running each link through the app's own client code (
decodeInputs, the draft adapters, the coarse series, refinement and the cliff report) against the realapi/*.pyhandlers. We ran main at 7cb8f2d and this PR at 12f4df1, both on policyengine-us 1.755.5. The later commits change only tests and formatting, so the figures hold for the merged code.Other places, same household
The page computes each figure as the net resources lost across one bracket of its refined series. The drops at $67,500 and above span $625 brackets, so each nets the cliff against what the rest of the bracket's earnings add after taxes and benefits, $344 in New Mexico's case. At $25 steps, NM 88001's Medicaid loss comes to $7,979 between $66,925 and $66,950, and Chicago's comes to $12,460 between $86,875 and $86,900. In the Los Angeles ZIPs the loss comes to $10,289 (90802) and $8,791 (90001) between $72,650 and $72,675. The brackets come from the API, which counts its 201-point cap from $0 rather than from a window's start. To refine New Mexico's coarse drop between $66,500 and $67,000, the page asks for $100 steps from $66,250 to $67,250. The API serves three points $625 apart: $66,250, $66,875 and $67,500. That predates this PR.
What else moves
In Los Angeles County, the engine reads the rating area from the ZIP. The calculator sends
zip_codetoo, becauseslcsp_rating_area_la_countyreads the ZIP's first three digits. For a link that names Los Angeles County, adding ZIP 90802 moves the rating area from 16 to 15, and net resources differ by up to $658 at 9 of 25 earnings points from $0 to $60,000. ZIP 90001 keeps rating area 16.A link that names Los Angeles County loses its housing subsidy. On main, only such a link got housing assistance, because Los Angeles County's schedule needs neither a ZIP nor
county_fips.county=LOS_ANGELES_COUNTY_CA, no ZIPWithout the take-up flag, the first head of this PR paid up to $22,913 a year at those ZIPs.
The example household enters no rent.
test_naming_the_default_county_reproduces_the_old_seriesconfirms that its series with ZIP 02139 and an explicit Barnstable County matches its old series at every point, so the ZIP moves its chart only through the county.The table
scripts/build_zip_county.pybuildscliff_watch/data/zip_county.json.gz(323 KB, SHA-256710185a1920d20feec42733a8f7133998f9a66df248df2b0bccf0071756139d7). Loading it takes about 0.2 s on the first request that needs it, and the module imports no engine code.GeoNames gives the table's first county for 32,776 of the 33,363 ZCTA ZIPs where it names a county the engine knows (98.24%), and one of the ZIP's counties for 33,291 (99.78%). The first review rebuilt the table in memory from the saved source cache and matched the table this PR adds.
We ran all 3,143 counties through one engine simulation with no error or NaN on 19 variables, and the engine read each county back. We also ran 91 real ZIPs, one per state and DC plus 40 at random, through the API's series path with no failure.
The preview of 12f4df1, whose code the merged head changes only in formatting, returned the same
locationand series as the local API for 9 households: a ZIP alone, an explicit county, a ZIP across counties, a picked county, a ZIP with no county in the state,DOÑA_ANA_COUNTY_NM, a Los Angeles ZIP in rating area 15, Baylor County's override, and a Los Angeles County link with rent, which read $0 of housing assistance.Invariants
Each holds for every input, and a test executes it.
The rule and the table (
tests/test_zip_county.py):county_for_zipreturns a county code with that state's suffix or nothing, and never raises on arbitrary text. (Hypothesis)county=MIDDLESEX_COUNTY_MA, and a negative control confirms that the no-county series differs and that the engine reads Barnstable.county_for_zipreturns each ZIP's first in-state row. (Exhaustive)County, survives the explicit county path unchanged, and serves a ZIP. The 50 states and DC all appear. (Exhaustive)normalize_zipgives five ASCII digits or nothing, and a second call changes nothing. (Hypothesis)The engine gaps (
tests/test_zip_county.py,tests/test_zip_county_engine.py):slcsp_rating_areaexactly when its county has an override, with that override's area. (Hypothesis, plus all 31 counties by name and by ZIP)slcsp_rating_areaand age-0 benchmark equal those of a county in the same state whose name the join matches in the same area. Doña Ana, alone in New Mexico's rating area 3, has no such twin, so the test checks that its benchmark differs from the rating area 1 benchmark the join gives it otherwise. Baylor's premium tax credits equal Archer County's at every point of the earnings series and differ from what rating area 1 gave. (Exhaustive over the 31)takes_up_housing_assistance_if_eligibleto False in every situation. Through the engine,housing_assistancereads $0 at four ZIPs and on a Los Angeles County link wherehud_hapexceeds $0. (Hypothesis, plus the engine examples)COUNTIES_WITHOUT_BENCHMARK. A series in each of the three shows no premium tax credit at any point, where the same household in its state's default county gets one. (Exhaustive)The builders (
tests/test_payload_invariants.py, #40's suite, whose payloads now draw ZIPs and counties):zip_code, and sendsslcsp_rating_areaonly with an override. (Hypothesis)KNOWN_COMPUTED_INPUTSlists with reasons (countyandslcsp_rating_areaamong them) and puts every variable on its entity, and the frontend snapshot covers it.The frontend (
frontend/src/utils/*.test.js):county: nulland gains no county when the page re-encodes it, a link that names a county keeps it, and the two links' payloads differ only in the county.getZipStatewith the table across all ZIPs and fails on any disagreement outside its list of 10 known ZIPs and two known prefixes. In those prefixes, 201xx and 569xx, only the known disagreement passes: form DC where the table says Virginia, and form Minnesota where it says DC.Intended exception: a ZIP whose form state disagrees with the table resolves to
none, by rule 5.Known limits
gov.aca.state_rating_area_costhas no row for South Carolina's rating areas 9 and 19 or Florida's 22, so Calhoun and Edgefield counties in South Carolina and Gulf County in Florida read a $0 benchmark. 9 ZIPs resolve to those counties, with 47,410 residents in the counties' parts of them (59,295 across the whole ZIPs). On main those households read their state's first county, so a single adult, 40, at $30,000 goes from $3,623 to $0 in South Carolina and from $6,951 to $0 in Florida. Adding those rows to policyengine-us closes the gap. Until then, the page says "CliffWatch has no benchmark premium for Edgefield County, so this chart shows no premium tax credit." The test that compares the list with the engine fails when an engine update adds the rows, so the note goes when the gap does.RATING_AREA_OVERRIDEShas no work left, andtest_every_county_has_a_rating_area_row_or_an_overridefails until the list goes. Oglala Lakota keeps rating area 1 until the file gains its row: 8 ZIPs, 13,351 residents.county_fipsfor the county fair market rent. That calls for a product decision, so it gets its own PR.policyengine-household-wizard, has gaps.getZipStatedisagrees with this table on 106 ZIPs (1,015,529 residents). It sends 80 of them, Northern Virginia's 201xx ZIPs (1,014,526 residents), to DC. Those households get DC's rules, as on main, and the page now says it can't place the ZIP. 78 other ZIPs get no state, so the form won't calculate them.SituationParsingError, as on main.Order with the other open PRs
Before this PR, no main commit or open PR mapped ZIP to county. #46 and #40 landed first, and this branch merged main at 7cb8f2d:
countyContext.test.jsandzipCounty.test.jsnow importtestfrom Vitest, and #40's payload invariants draw ZIPs and counties. #45 moves to policyengine-us 2.2.1, so its rebase should reruntests/test_zip_county_engine.py, which checks the rating-area overrides and the no-benchmark counties against the installed engine. The rebase should also update #45's README line about the form reading no county.Reviews
The first review, by GPT-6.1 Sol at 463716d, requested changes for the voucher subsidy, the rating areas and the "mostly" wording in the override note. 12f4df1 fixes all three and adds the tests that review found missing: the cache test now fails without the ZIP in the key, the builder property checks land order for ZCTAs with no residents, and a test covers every state.
The second review, by an Opus 5.5 agent at 12f4df1, approved and found no defect in the code. Its notes on test coverage led to 711201c, which changes only tests: the ZIP property runs all 41,120 (ZIP, state) pairs, a test catches a rating-area row for Oglala Lakota under another spelling, and the 201xx and 569xx exemption passes only the known disagreement. Its notes on the figures led to the benchmark table's three columns, the bracket note under "Other places" and the no-benchmark reach in its known limit.
The same reviewer approved 711201c after running a negative control for each new test and rechecking every changed figure. Its two notes led to a48741b. The coverage test now compares the (ZIP, state) pairs the loops list with a fresh walk of the table, so it fails if the 138 pairs of ZIPs that cross a state line drop out. The bracket note now says what each bracket nets. That commit also applies ruff's formatting to the three files this PR had left unformatted and replaces two lambdas that closed over loop variables in the builder property test. The reviewer approved a48741b, and the PR merged at that head.
axiom: n/a: CliffWatch input mapping; no policy encoding change
🤖 Generated with Claude Code