Repository navigation
Conversation
Failing tests for a server-side helper that calculates an inline household on API v1's /v2/household-calculations: submit, read every 2 seconds until the calculation succeeds or fails, a 10-minute limit, retries of brief outages, and which submission failures are safe to retry. Also tests that fetchFromServer takes the caller's AbortSignal and says whether a failed request may have reached API v1. The helper module is a stub for now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds createHouseholdCalculator to @policyengine/public-api-forwarding. Its calculateHousehold submits /calculate's household and policy to API v1's POST /v2/household-calculations, which keeps the calculation as a household job only, then reads it every 2 seconds until it succeeds or fails, for up to 10 minutes. Brief outages are retried for 90 seconds; a submission is retried only when API v1 cannot have created the job. fetchFromServer now takes the caller's AbortSignal, and its PublicApiUnavailableError says whether the request may have reached API v1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A time limit that is not a number, or is negative, should reject with RangeError before anything is submitted; today NaN and a string make the helper read with no wait. Infinity stays allowed (that test passes already). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
calculateHousehold now rejects with RangeError, before submitting, when timeLimitMilliseconds is not a number, is NaN or is negative. Infinity is still allowed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Failing tests for: - the household's axes travelling as a separate top-level axes field, as API v1's inline request takes them; - the submission's timeout covering the time left before the deadline, at least 30 seconds, so a starting API v1 instance doesn't fail it; - a 503 that isn't API v1's own (Cloud Run's plain-text one) not being resubmitted, since the job may exist; - a submission retry that would wait until the deadline throwing instead; - a time limit of 0 still reading the accepted calculation once; - an abort while the server token is fetched rejecting with the signal's reason. Also covers branches that had no test (they pass already): lost answer bodies, read credential failures, submission 429s and 90 seconds of 503s, Retry-After on a 429 and as a date, unreadable read answers, a failure with no code, an abort while an answer arrives. The forwarder's abort test now aborts after the request has started. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The household's axes go in the request's own axes field, as API v1's inline request takes them, without changing the caller's object. - The submission may take until the deadline (at least 30 seconds), so a request that waits for an API v1 instance to start doesn't time out and leave a job that may exist. Reads keep 30 seconds. - Only API v1's own 503, with its error body, is resubmitted; Cloud Run's plain-text 503 is not. - A submission retry that would wait until the deadline throws instead, and an accepted calculation is read at least once, even with a time limit of 0. - fetchFromServer checks the caller's signal before throwing a failed connection, so an abort while the token is fetched rejects with the signal's reason. - Exported types no longer name unexported ones; the result-item casts are checks; requestMayHaveArrived and lastStatus are documented as they behave; the README's unrelated table reformatting is undone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Failing tests for: - a submission answer that already says succeeded or failed (API v1's submitted item allows every status, and the simulation service answers with the job's current status when Modal doesn't confirm the call): read it at once rather than refusing it; - a time limit that isn't a whole number: the submission's timeout must be whole milliseconds, which AbortSignal.timeout requires (the fake fetchFromServer now checks each timeout the same way); - PublicApiUnavailableError naming why API v1 can't be called (unknown route, no configuration, no credential, unreachable, timed out), so the helper's resubmission rule no longer depends on which status a failure gets; - an abort while the server token is fetched rejecting at once, without waiting for the token request. The abort-while-an-answer-arrives test now aborts during the body read, after the submission was sent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A submission answer that already says succeeded or failed is read at once, for its result or error, instead of being refused as unreadable. - The submission's timeout is rounded up to whole milliseconds, which AbortSignal.timeout requires, so a fractional time limit no longer fails every call. - PublicApiUnavailableError names its reason (unknown_route, not_configured, credential_unavailable, unreachable, timed_out, answer_lost); requestMayHaveArrived follows from it, and the helper's resubmission and retry rules check the reason, not the status code. - fetchFromServer rejects with the signal's reason as soon as the caller aborts while the token is fetched; the shared token request goes on. - warnings are checked as strings by a type predicate. - The README differs from main only by the new sections, which say that "API v1" is the service it calls the Public API, and give the token time in the worst-case call length. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 3 found no bugs, but tests that would pass without the behaviour they name, and untested branches. These cover: - a read at the deadline that answers 503, or can't reach API v1, ending with HouseholdCalculationTimeoutError rather than reading again; - 90 seconds of reads that can't reach API v1 ending with that failure; - the helper's own timer: the first read 2 seconds after submitting, and an abort during the wait rejecting at once and leaving no timer. The abort-during-a-wait test now uses a wait that ends only through the signal it is given, and the forwarder's abort test uses a 60-second request timeout, so each fails if the signal stops being passed on. They cover existing behaviour, so they pass already. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review round 3's documentation findings: the PublicApiUnavailableError doc comment sits on the class again, not above the new reason type; and the notes now say a call can end up to about 80 seconds after its deadline (a submission started just before it, then the read it is owed), and that Node's fetch and API v1's Cloud Run service each end a submission after 300 seconds. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…helper POST /api/household/calculate should submit each household to API v1's /v2/household-calculations through createHouseholdCalculator, read it until it finishes, and answer with /calculate's envelope: streamed for a large result, with /calculate's 400 for inputs the model can't read, 504 after the helper's time limit, and no more reads once the browser leaves. Refs #1614 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lculations
POST /api/household/calculate no longer forwards to API v1's synchronous
/{country}/calculate. The website's server submits the household (and the
policy's parameter values, when there are any) to
/v2/household-calculations through createHouseholdCalculator, reads it
every 2 seconds for up to 10 minutes, and answers as /calculate did:
- 200 with {status: "ok", message: null, result}, plus warnings and
spm_provenance when API v1 gives them, streamed in 64 KiB parts so a
large result isn't held to Vercel's 4.5 MB limit on buffered answers;
- 400 with errors for inputs the model can't calculate, and for a
household API v1 refuses as invalid (422);
- 500 for a calculation that fails otherwise;
- 504 when the time limit passes, and API v1's own status otherwise.
A browser that goes away stops the reads. The requests go as the
website server's own, with the server credential and without the
browser's token or address.
Fixes #1614
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- a calculation_failed message already carries /calculate's prefix, and /calculate's 500 had no result field; - a redirect from API v1 is the route's own logged 502, as the forwarder answered it; - a refusal keeps API v1's request and observability IDs, and an error from in front of API v1 that isn't its JSON is marked as the route's; - a refusal's errors list reaches the caller; - a lost answer is logged. Also: a real input error type in the fixture, API v1's name in the new test text, and the duration sum without counting token waits twice. Refs #1614 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A calculation_failed message keeps the one prefix the household function gave it, and the 500 has no result field, as /calculate's had none. - A redirect from API v1 is the route's own logged 502, as the forwarder answered it. - Refusals carry API v1's request and observability IDs; an error from in front of API v1 that isn't its JSON is marked as the route's own. - A refusal's errors list reaches the caller. - A lost answer is logged. - OperationResult's type parameter no longer hides the global Request, and the operations module's new comment says API v1. Refs #1614 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- a calculated household's answer, and a failed calculation's, carry the request and observability IDs of API v1's answer they came from; - a 4xx on reading a calculation API v1 accepted is the route's own logged 502, except a rate limit; - an answer the helper can't read says so in plain words, and the helper's reason goes to the log; - the slowest recorded submission is timed out on the fake clock, so the test fails if the submission's timeout were 30 seconds; - the duration test works out the helper's overrun from its timeouts. Refs #1614 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Each calculation gets its own calculator, over a forwarder that notes the request and observability IDs of API v1's last answer, so a calculated household's answer and a failed calculation's carry them, as /calculate's did. - A 4xx on reading a calculation API v1 accepted, other than a rate limit, is the route's own logged 502: the caller's input was accepted. - An answer the helper can't read gets a plain message; the helper's reason goes to the log. Refs #1614 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- the answers carry the IDs of API v1's answer to the submission, the one request with an observability ID, not the last read's; - two calculations at once each keep their own submission's IDs; - the slow-submission test waits less than the 300 s Node's fetch waits for an answer's headers, which the recorded 396 s answer exceeds. Refs #1614 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The answers carry the IDs of API v1's answer to the submission, the request that started the calculation and the only one API v1 gives an observability ID, rather than the last read's. - The IDs' type and header names move to their own publicApi module, so the website's calculator no longer depends on the dashboard API's answer module. - The route's comments say what stays the same: the envelope and the status of each outcome of a calculation. Refs #1614 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Fixes #1614
What changes
POST /api/household/calculate, the website's dashboard route for household calculations, no longer forwards to API v1's synchronousPOST /{country}/calculate. The website's server calculates each household throughcreateHouseholdCalculatorfrom #1613:POST /v2/household-calculationsas an inline household:{country_id, household, policy?, axes?}. The policy's parameter values go only when there are any. An absent or empty policy means current law. API v1 keeps the calculation as a household job only, with no stored household or policy.GET /v2/household-calculations/{calculation_id}every 2 seconds, for up to 10 minutes./calculatedid, so@policyengine/api-clientand the dashboards that call the route don't change.{status: "ok", message: null, result}, withwarningswhen there are any andspm_provenancewhen API v1 gives one, as/calculatedidinvalid_inputs)errorsinvalid_household), or SPM settings it can't use (invalid_spm_selection, with the SPM error inerrors)errorsit lists, as/calculateanswered a household it couldn't parse{status: "error", message: "Error calculating household under policy: …"}, as/calculatedidRetry-After,X-PolicyEngine-Request-IdandX-PolicyEngine-Observability-Idpass throughEvery answer is
no-storeand carries the dashboard API's CORS headers. The success body is streamed in 64 KiB parts, as the forwarder streams API v1's answers, so a result with axes isn't held to Vercel's 4.5 MB limit on buffered function answers. Failures the route writes itself carryX-PolicyEngine-Route-Error: true, and the outcomes API v1 decided don't, as before. Each calculation gets its own calculator, over a forwarder that notes the request and observability IDs of API v1's last answer. So a calculated household's answer and a failed calculation's carry theX-PolicyEngine-Request-IdandX-PolicyEngine-Observability-Idof the read that gave the outcome, as/calculate's answers carried its own. The helper needs no change for this.The route's
maxDurationstays 800 s. That covers the helper's worst case: the 10-minute limit, up to about 80 s after it for the last submission or read and their tokens, then the streamed answer. A test now checks that sum.What the route no longer sends
The requests now go as the website server's own (
fetchFromServer): the server credential andUser-Agent: policyengine-website. Previouslyforwardalso sent:X-Forwarded-For;PolicyEngine-User-Authorization;PolicyEngine-Unauthenticated-User.API v1's
/v2/household-calculationsroutes need no account, and an inline calculation stores no user association. So nothing reads those headers there.Built on unmerged work
apiv2-d3-5-submit-and-poll. This PR's base ismain, so its diff includes Calculate households on API v1 by submitting and polling (server helper) #1613's commits until Calculate households on API v1 by submitting and polling (server helper) #1613 merges. After it merges, mergemainin. Nothing needs adapting unless the helper's names or shapes change before it merges.POST /v2/household-calculationsdoesn't yet take an inline household; that is being added in API v1 (D3 · 2 to D3 · 4 of the detailed-save API migration). This PR uses the inline body as recorded for that work. Until that reaches an API v1 deployment, this route answers whatever API v1 answers an inline body there, most likely a 422 (passed on as 400). So this PR merges only after API v1's inline route is deployed to production.Parity with
/calculateThe comparison against
/calculate's answers for a sample of households runs once API v1's inline route reaches API v1's staging service. Its results will be added here.Tests
The tests added or changed here, run one by one with Vitest's
-t:website/src/__tests__/lib/householdCalculationAnswer.test.ts(new): each outcome in the table above, the streamed body for a result over 4.5 MB, the browser's signal passed to the helper, and what is logged.website/src/__tests__/app/dashboard-api.test.ts, the household calculation's tests: submit and read until succeeded, with the last read's request ID; the policy sent, and an empty one left out; the server's own request without the browser's token or address; a submission accepted after 396 s, the slowest household answer API v1's load balancer recorded, with request timeouts on the fake clock, so the test fails if the submission's timeout were 30 s; 504 at the helper's 10-minute limit and not before; no reads after the browser goes away; the function duration. The CORS, origin and credential tests now run against a fake API v1 that calculates through/v2/household-calculations.🤖 Generated with Claude Code