Repository navigation
Calculate households through the dashboard API - #234
Merged
Merged
Conversation
usePolicyEngineAPI posts the baseline and reform households to the Public API's /us/calculate and reads its /us/metadata from the browser. These tests fail until both go through www.policyengine.org's dashboard API, with the same household and policy, and the results, model version and error messages reach the page as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
usePolicyEngineAPI now sends the baseline and reform households, and its model-version read, through @policyengine/api-client to www.policyengine.org's dashboard API instead of the Public API. The household and policy are the same, ResultsDisplay still reads each answer's result, and errors still carry the message. The tracker's pages are served from www.policyengine.org, so these are same-origin requests there. Bumps APP_VERSION so the Modal image rebuilds the app with this code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
From review round 1: the model version gates only structural reforms, but every analyzer mount streams the whole US metadata through www.policyengine.org. The first test fails until the hook can skip that read; the second pins that a failed read leaves the version unknown, which keeps structural reforms' household tab disabled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
usePolicyEngineAPI takes readModelVersion, and ReformAnalyzer passes it only for structural reforms, the only ones gated on the deployed policyengine-us version. Other analyses no longer download the whole US metadata through www.policyengine.org. Also corrects the comment on the shape runCalculation resolves with. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
www.policyengine.org's firewall allows each IP address 30 household calculations a minute and answers 429 over that limit, without CORS headers. On www.policyengine.org the page reads the 429; on the tracker's own Modal host the browser reports a network failure instead. The hook tests check that a 429, or no answer on another origin, gives a message explaining that everyone on the same network shares the limit, marks the error as rate-limited, pauses calculations for a minute (including when the other of the two parallel calculations failed first), and holds back calculations during the pause without sending them; that no answer on www.policyengine.org is reported as a network failure without a pause; and that a 504 keeps its message, is sent once and does not pause. The ReformAnalyzer tests check that after a rate-limited failure the last answer stays on screen without the error box, and the Calculate button is disabled and counts down beside a notice whose explanation alone has role="status"; and that an answer already replaced by another failure's error box does not come back. All of these tests fail on their assertions until the handling is added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
www.policyengine.org's firewall allows each IP address 30 household calculations a minute. Over that limit it answers 429 without CORS headers, so the tracker reads the 429 when served from www.policyengine.org, and sees a failed request (status null) when served from its own Modal host. usePolicyEngineAPI now classifies a failed calculation: - 429: everyone on the network shares a limit on calculations a minute, and it has been reached. - No answer on another origin: the calculation service could not be reached, and the shared limit may have been reached. - No answer on www.policyengine.org: a network failure; check the connection and try again. No pause. - Anything else keeps the dashboard API's own message. The error is a HouseholdCalculationError with a rateLimited flag. A failure the limit may have caused pauses household calculations for the firewall's one-minute window at once, before the error is rethrown, so a refusal of either of the two parallel calculations starts the pause. During the pause a calculation is refused without sending a request. No automatic retries are added. The module exports SHARED_LIMIT_EXPLANATION, pauseHouseholdCalculations, householdCalculationPauseRemaining, onHouseholdCalculationPause and a useHouseholdCalculationPause hook that counts the pause down in seconds. After a rate-limited failure the hook's error stays null, so ReformAnalyzer keeps the last answer on screen instead of the error box. While calculations are paused, HouseholdForm's Calculate button is disabled and reads "Calculate again in N s", and a notice below it gives the explanation (the only part with role="status") and the same countdown. Another failure still shows its error box, and now also forgets the answer it replaced, so that a later rate-limited failure does not bring back an answer that was no longer on screen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After a refusal for the dashboard API's rate limit, the page keeps the answer it showed. The new test expects a note above it saying the results are for the household calculated before, until the next answer replaces them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After a refusal for the dashboard API's rate limit, the household tab keeps the answer it showed. A note above it now says the results are for the household calculated before, since the refused household may differ. The next answer, or any other failure, removes the note. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After review, the notice beside the Calculate button gives the message of the rate-limited failure that started the pause, so that "could not be reached" stays visible on hosts other than www.policyengine.org. The pause test now checks that pauseHouseholdCalculations stores a reason, the message "Everyone on your network, ..., which may have been reached." by default, and that householdCalculationPauseReason returns it. The ReformAnalyzer notice tests now expect the 429's message ("..., and it has been reached.") in the role="status" element, followed by the countdown. Two tests are new: on the tracker's own host a request with no answer shows "PolicyEngine's calculation service could not be reached." followed by the shared-limit explanation; and when the baseline fails for another reason and the reform is refused for the limit afterwards, the notice gives the refusal's message, which ReformAnalyzer never received. These tests fail on their assertions until the reason is kept with the pause. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The notice beside the Calculate button gave only the shared-limit explanation, so on hosts other than www.policyengine.org, such as the tracker's Modal host, a request with no answer no longer said that the calculation service could not be reached. pauseHouseholdCalculations(milliseconds, reason) now stores the user-facing message of the refusal that started the pause (by default "Everyone on your network, ..., which may have been reached."), and householdCalculationPauseReason() returns it. A rate-limited failure passes its own message when it pauses calculations, so the notice gives the right message even when ReformAnalyzer received the other parallel calculation's failure first. useHouseholdCalculationPause now returns { secondsRemaining, reason }, and HouseholdForm's notice reads "<reason> This page can calculate again in N s.", with only the reason in the role="status" element. The error box still does not appear for a rate-limited failure. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes #233
The reform analyzer's household calculations and its model-version read now go through
@policyengine/api-clientto www.policyengine.org's dashboard API, instead of calling the PolicyEngine Public API (api.policyengine.org) from the browser. The Public API will soon answer only PolicyEngine's own servers. Not stacked on another PR.Change
src/hooks/usePolicyEngineAPI.js:runCalculationsends{country: "us", household}, pluspolicy: buildApiPolicy(reform)for the reform, throughcalculateHousehold./us/calculatewithpolicy: {}for the baseline, which the Public API reads the same as the old missingpolicy.{status: "ok", message: null, result}, soResultsDisplayreads.resultas before.getMetadata("us"), cached for the page as before. A failure still givesnull, which keeps a structural reform's household tab disabled.readModelVersionoption:ReformAnalyzersets it only for structural reforms, the only ones whose household tab depends on the model version. Other analyses no longer download the whole US metadata. Before, every analyzer mount did, and that download now passes through www.policyengine.org's server.Deployment:
modal_app.py'sAPP_VERSIONgoes from v41 to v42, so the Modal image rebuilds the app with this code. The build runsnpm installwithout a lockfile, so it installs@policyengine/api-clientfrompackage.json.Origins: readers use www.policyengine.org/us/bill-tracker, where these requests are same-origin and need nothing else. Direct visits to
policyengine--state-legislative-tracker.modal.runare allowed by PolicyEngine/policyengine-app-v2#1552, merged and deployed on 2026-11-12.Red-green runs
Each test was run alone with
npx vitest run src/hooks/usePolicyEngineAPI.calls.test.js -t "<name>".972e0f73017476663796433a0354ESLint passes on the three changed source files. A production
next buildsucceeds, and the exportedout/names no api.policyengine.org.Browser check
On
http://localhost:3115/UT/ut-sb60-bill(next dev --webpack, with the public Supabase URL and anon key frommodal_app.py), "Your Household" → "Calculate Impact" with the default household:3017476:GET https://www.policyengine.org/api/metadata/usand twoPOST https://www.policyengine.org/api/household/calculate, all 200.33a0354: only the two calculations (200, 200), since SB60 is not a structural reform.The production page, which calls the Public API directly, showed the same: −$25, federal tax $3,820 in both, state tax $1,667 → $1,692.
Review
Two rounds of adversarial self-review, each in a fresh clone; neither found a bug.
Rate-limit handling (added 2026-11-13)
www.policyengine.org's firewall allows each address 30 household calculations a minute. Over that it answers 429 with no CORS headers, so the Modal host sees a request with no answer instead. The
policyengine-api-accessskill asks a page refused for the limit to keep its last answer, say that everyone on the same network shares the limit, and send nothing more for a minute. These commits do that.src/hooks/usePolicyEngineAPI.js:HouseholdCalculationErrorwithstatusandrateLimited:Promise.allreports the other failure. The pause keeps the failure's message as its reason.useHouseholdCalculationPause()returns the seconds left and the reason, and counts down once a second.ReformAnalyzerandHouseholdForm:role="status"span, followed by "This page can calculate again in N s." When the minute ends, the button and notice return to normal.Red-green runs
Each test was run alone with
bunx vitest run <file> -t "<name>". Every test committed first failed on an assertion and passed after the code that followed it.97d6491622ef81usePolicyEngineAPI.rateLimit.test.js(classification, pause, holding back, 504 message, ending a pause) and 3 component tests inReformAnalyzer.rateLimit.test.jsx(answer kept with countdown; no answer yet; an earlier error's answer doesn't come back)e290f7d91cf6be92b6207d0f7856ESLint on the changed files exits 0 (its one warning, on
HouseholdForm's year-sync effect, is onmaintoo).bun run buildexits 0, and the exported files name no api.policyengine.org.Browser check
On
http://localhost:3115/UT/ut-sb60-billatd0f7856:/api/household/calculatemade to fail, which is how localhost sees the firewall's 429, the next click sent two requests and both were refused.622ef81the button returned to "Calculate Impact", enabled, after the minute.Review
Two rounds of adversarial self-review, each in a fresh clone:
622ef81): no bugs. Applied from it: the note above a kept answer.d0f7856): no bugs. It confirmed all claims, including that a 400 still shows the dashboard API's message.Left as it is
ReformAnalyzer's wiring ofreadModelVersion(no component test);role="status"span is added with its text already in it, which some screen readers don't announce.mainitself and installs without a lockfile, so after merge the only check is that a served_trackerchunk contains/api/household/calculateand no api.policyengine.org..claudeagent and command docs still tell developers to call the Public API directly.Environment and secrets
No variable or secret is added or read. The client sends no credential (
credentials: "omit"). The deployment change is theAPP_VERSIONbump only.MODAL_TOKEN_ID,MODAL_TOKEN_SECRETdeploy.ymlstepenv:on pushes tomain, formodal deploy modal_app.pyNEXT_PUBLIC_SUPABASE_URL,NEXT_PUBLIC_SUPABASE_ANON_KEY,NEXT_PUBLIC_POSTHOG_KEYmodal_app.py(public values)npm run buildcommand in the image build (build time)SUPABASE_URL,SUPABASE_KEY, and the other runtime valuesstate-research-tracker-runtimewebfunction (run time)policyengineworkspace; its keys weren't read🤖 Generated with Claude Code